Index: /home/exo/java/projects/xwiki/src/main/java/com/xpn/xwiki/render/filter/XWikiHeadingFilter.java
===================================================================
--- /home/exo/java/projects/xwiki/src/main/java/com/xpn/xwiki/render/filter/XWikiHeadingFilter.java	(revision 1054)
+++ /home/exo/java/projects/xwiki/src/main/java/com/xpn/xwiki/render/filter/XWikiHeadingFilter.java	(working copy)
@@ -1,25 +1,25 @@
-/*
- * Copyright 2006, XpertNet SARL, and individual contributors as indicated
- * by the contributors.txt.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- *
- * @author sdumitriu
- * @author tepich
- */
+/*
+ * Copyright 2006, XpertNet SARL, and individual contributors as indicated
+ * by the contributors.txt.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ * @author sdumitriu
+ * @author tepich
+ */
 package com.xpn.xwiki.render.filter;
 
 import java.text.MessageFormat;
@@ -36,6 +36,7 @@
 import org.radeox.regex.MatchResult;
 
 import com.xpn.xwiki.XWikiContext;
+import com.xpn.xwiki.doc.XWikiDocument;
 import com.xpn.xwiki.render.XWikiRadeoxRenderEngine;
 import com.xpn.xwiki.util.TOCGenerator;
 
@@ -46,7 +47,7 @@
 public class XWikiHeadingFilter extends LocaleRegexTokenFilter implements CacheFilter {
   private final String TOC_NUMBERED = "tocNumbered";
   private final String TOC_DATA = "tocData";
-  
+
   private MessageFormat formatter;
 
 
@@ -70,10 +71,12 @@
     String level = result.group(1);
     String text = result.group(3);
     String numbering = "";
-    
+
     RenderContext rcontext = context.getRenderContext();
     XWikiContext xcontext  = ((XWikiRadeoxRenderEngine) rcontext.getRenderEngine()).getContext();
-    
+    XWikiDocument doc = xcontext.getDoc();
+    String content = doc.getContent();
+
     // generate unique ID of the heading  
     List processedHeadings = (List) rcontext.get("processedHeadings");
     if (processedHeadings == null) {
@@ -84,7 +87,7 @@
     for (Iterator iter = processedHeadings.iterator(); iter.hasNext();) if (iter.next().equals(text)) occurence++;
     id = TOCGenerator.makeHeadingID(text, occurence, xcontext);
     processedHeadings.add(text);
-    
+
     //  add numbering if the flag is set
     if (xcontext.containsKey(TOC_NUMBERED) && ((Boolean)xcontext.get(TOC_NUMBERED)).booleanValue()) {
       if (xcontext.containsKey(TOC_DATA)) {
@@ -92,7 +95,48 @@
         if (tocEntry != null) numbering = (String) tocEntry.get(TOCGenerator.TOC_DATA_NUMBERING) + " ";
       }
     }
-    
-    return formatter.format(new Object[]{id, level.replace('.', '-'), numbering, text});
+
+
+      int index = content.indexOf(text);
+      // remove all Section
+      Object ob = xcontext.get("beforAction");
+      if(ob != null){
+         String beforAction = ob.toString();
+          if(!beforAction.equals("HeadingFilter")){
+              doc.removeSections();
+              xcontext.put("beforAction","HeadingFilter");
+          }
+      }
+
+      if(level.equals("1") || level.equals("1.1") && text.indexOf("<a href=") == -1 && content.indexOf(text) != -1 ){
+
+          String[] obSection = new String[4];
+          // set id of section
+          obSection[0]=""+(doc.getSplisSections().size()+1);
+          if(level.equals("1.1"))
+            // set index content of section
+            obSection[1]=""+(index-5);
+          else
+            obSection[1]=""+(index-3);
+          // set level of section
+          obSection[2] = level ;
+          obSection[3] = text ; // title of heading
+          doc.setSplitSections(obSection);
+
+          String textfomat = formatter.format(new Object[]{id, level.replace('.', '-'), numbering, text});
+          String editorPreference = xcontext.getWiki().getEditorPreference(xcontext);
+          String url =xcontext.getDoc().getURL("edit",xcontext);
+
+          if(editorPreference.equals("wysiwyg")){
+            url += "?xpage=wysiwyg&?section="+obSection[0];
+          }else{
+              url +="?section="+id;
+          }
+
+          textfomat +="<p align='right'> <a href='"+ url+"'>"+"edit"+"</a></p>\n";
+          return textfomat;
+      }
+
+     return formatter.format(new Object[]{id, level.replace('.', '-'), numbering, text});
   } 
-}
+}
\ No newline at end of file
