--- MoinMoin/macro/Include.py.orig	Mon Oct 16 13:21:40 2006
+++ MoinMoin/macro/Include.py	Tue Feb 13 16:48:00 2007
@@ -219,7 +219,7 @@
         try:
             cid = request.makeUniqueID("Include_%s" % wikiutil.quoteWikinameURL(inc_page.page_name))
             inc_page.send_page(request, content_only=1, content_id=cid,
-                               omit_footnotes=True)
+                               omit_footnotes=True, do_cache=False)
             result.append(strfile.getvalue())
         finally:
             request.redirect()
--- MoinMoin/PageEditor.py.orig	Mon Oct  2 21:09:04 2006
+++ MoinMoin/PageEditor.py	Tue Feb 13 16:50:27 2007
@@ -149,6 +149,29 @@
         preview = kw.get('preview', None)
         staytop = kw.get('staytop', 0)
 
+        # for section editing
+        issectionedit = kw.get('issectionedit', 1)
+        pagetext = kw.get('pagetext', None)
+        startline = int(form.get('startline', ['0'])[0])
+        endline = int(form.get('endline', ['0'])[0])
+        srev = int(form.get('srev', ['0'])[0])
+        
+        if startline or endline:
+            if not startline:
+                startline = 1
+        
+            if not endline:
+                endline = -1    
+        else:
+            issectionedit = 0
+
+        if issectionedit:
+            # need to add config
+            self._allow_section_edit = self.cfg.allow_section_edit
+            # self._allow_section_edit = 1
+
+        # end of section editing
+        
         from MoinMoin.formatter.text_html import Formatter
         self.request.formatter = Formatter(self.request, store_pagelinks=1)
 
@@ -187,7 +210,16 @@
             title = _('Edit "%(pagename)s"')
         else:
             title = _('Preview of "%(pagename)s"')
-            self.set_raw_body(preview, modified=1)
+            
+            # section-editing
+            
+            #self.set_raw_body(preview, modified=1)
+            if issectionedit and pagetext is not None:
+                self.set_raw_body(pagetext, modified=1)
+            else:
+                self.set_raw_body(preview, modified=1)
+
+            # end of section-editing
 
         # send header stuff
         lock_timeout = self.lock.timeout / 60
@@ -227,9 +259,27 @@
                 # We don't show preview when in conflict
                 preview = None
                 
+                # section-editing
+                
+                # no section-editing any more
+                if issectionedit:
+                    conflict_msg = u'%s %s' % (conflict_msg, _('Section editing is canceled.'))
+                issectionedit = 0
+
+                # end of section-editing
+                
         elif self.exists():
             # revision of existing page
             rev = self.current_rev()
+            
+            # section-editing
+
+            if issectionedit and preview is None:
+                if not (srev and srev == rev):
+                    conflict_msg = u'%s %s' % (_('Section editing is canceled.'), _('Someone else updated this page. You are editing the updated revision.'))
+                    issectionedit = 0
+
+            # end of section-editing
         else:
             # page creation
             rev = 0
@@ -288,6 +338,17 @@
         if not raw_body:
             raw_body = _('Describe %s here.') % (self.page_name,)
 
+        # section-editing
+        
+        elif issectionedit:
+            # for section editing
+            if pagetext is not None:
+                raw_body = preview
+            else:
+                raw_body = self.fetchsection(raw_body, startline, endline)
+
+        # end of section-editing
+        
         # send form
         self.request.write('<form id="editor" method="post" action="%s/%s#preview" onSubmit="flgChange = false;">' % (
             self.request.getScriptname(),
@@ -304,6 +365,15 @@
         # Send revision of the page our edit is based on
         self.request.write('<input type="hidden" name="rev" value="%d">' % (rev,))
 
+        # section-editing
+
+        # Send section startline and endline for section-editing
+        if issectionedit:
+            self.request.write('<input type="hidden" name="startline" value="%d">' % startline)
+            self.request.write('<input type="hidden" name="endline" value="%d">' % endline)
+        
+        # end of section-editing
+        
         # Create and send a ticket, so we can check the POST
         self.request.write('<input type="hidden" name="ticket" value="%s">' % wikiutil.createTicket(self.request))
 
@@ -406,6 +476,14 @@
 
         badwords_re = None
         if preview is not None:
+            
+            # section-editing
+            
+            if issectionedit:
+                self.set_raw_body(preview)
+            
+            # end of section-editing    
+            
             if SpellCheck and (
                     form.has_key('button_spellcheck') or
                     form.has_key('button_newwords')):
@@ -427,9 +505,22 @@
                 content_id = 'previewbelow'
             else:
                 content_id = 'preview'
-            self.send_page(self.request, content_id=content_id, content_only=1,
+            
+            
+            # section-editing
+            
+            #self.send_page(self.request, content_id=content_id, content_only=1,
+            #                hilite_re=badwords_re)
+
+            if issectionedit:
+                self.send_page(self.request, content_id=content_id, content_only=1,
+                           hilite_re=badwords_re, do_cache=False)
+            else:
+                self.send_page(self.request, content_id=content_id, content_only=1,
                            hilite_re=badwords_re)
 
+            # end of section-editing
+
         self.request.write(self.request.formatter.endContent())
         wikiutil.send_footer(self.request, self.page_name)
         
@@ -1009,6 +1100,20 @@
         return msg
             
             
+    # section-editing
+    
+    def fetchsection(self, pagetext, startline, endline):
+        
+        pagetext = pagetext.split('\n')
+        if endline == -1:
+            sectiontext = u'\n'.join(pagetext[startline-1:])
+        else:
+            sectiontext = u'\n'.join(pagetext[startline-1:endline])
+        
+        return sectiontext
+    
+    # end of section-editing
+
 class PageLock:
     """
     PageLock - Lock pages
--- MoinMoin/PageGraphicalEditor.py.orig	Wed Apr 18 14:55:58 2007
+++ MoinMoin/PageGraphicalEditor.py	Wed Apr 18 14:57:33 2007
@@ -65,6 +65,29 @@
         preview = kw.get('preview', None)
         staytop = kw.get('staytop', 0)
 
+        # for section editing
+        issectionedit = kw.get('issectionedit', 1)
+        pagetext = kw.get('pagetext', None)
+        startline = int(form.get('startline', ['0'])[0])
+        endline = int(form.get('endline', ['0'])[0])
+        srev = int(form.get('srev', ['0'])[0])
+        
+        if startline or endline:
+            if not startline:
+                startline = 1
+        
+            if not endline:
+                endline = -1    
+        else:
+            issectionedit = 0
+
+        if issectionedit:
+            # need to add config
+            self._allow_section_edit = self.cfg.allow_section_edit
+            # self._allow_section_edit = 1  
+
+        # end of section editing
+        
         # check edit permissions
         if not self.request.user.may.write(self.page_name):
             msg = _('You are not allowed to edit this page.')
@@ -95,7 +118,12 @@
             title = _('Edit "%(pagename)s"')
         else:
             title = _('Preview of "%(pagename)s"')
-            self.set_raw_body(preview, modified=1)
+            #self.set_raw_body(preview, modified=1)
+            
+            if issectionedit and pagetext is not None:
+                self.set_raw_body(pagetext, modified=1)
+            else:
+                self.set_raw_body(preview, modified=1)
 
         # send header stuff
         lock_timeout = self.lock.timeout / 60
@@ -135,9 +163,20 @@
                 # We don't show preview when in conflict
                 preview = None
                 
+                # no section-editing any more
+                if issectionedit:
+                    conflict_msg = u'%s %s' % (conflict_msg, _('Section editing is canceled.'))
+                issectionedit = 0
+                
         elif self.exists():
             # revision of existing page
             rev = self.current_rev()
+            
+            if issectionedit and preview is None:
+                if not (srev and srev == rev):
+                    conflict_msg = u'%s %s' % (_('Section editing is canceled.'), _('Someone else updated this page. You are editing the updated revision.'))
+                    issectionedit = 0
+                    
         else:
             # page creation
             rev = 0
@@ -196,6 +235,13 @@
         if not raw_body:
             raw_body = _('Describe %s here.') % (self.page_name,)
 
+        elif issectionedit:
+            # for section editing
+            if pagetext is not None:
+                raw_body = preview
+            else:
+                raw_body = self.fetchsection(raw_body, startline, endline)
+        
         # send form
         self.request.write('<form id="editor" method="post" action="%s/%s#preview">' % (
             self.request.getScriptname(),
@@ -215,6 +261,11 @@
         # Create and send a ticket, so we can check the POST
         self.request.write('<input type="hidden" name="ticket" value="%s">' % wikiutil.createTicket(self.request))
 
+        # Send section startline and endline for section-editing
+        if issectionedit:
+            self.request.write('<input type="hidden" name="startline" value="%d">' % startline)
+            self.request.write('<input type="hidden" name="endline" value="%d">' % endline)
+        
         # Save backto in a hidden input
         backto = form.get('backto', [None])[0]
         if backto:
@@ -339,6 +390,10 @@
 
         badwords_re = None
         if preview is not None:
+        
+            if issectionedit:
+                self.set_raw_body(preview)
+        
             if SpellCheck and (
                     form.has_key('button_spellcheck') or
                     form.has_key('button_newwords')):
@@ -353,7 +408,14 @@
                 content_id = 'previewbelow'
             else:
                 content_id = 'preview'
-            self.send_page(self.request, content_id=content_id, content_only=1,
+            #self.send_page(self.request, content_id=content_id, content_only=1,
+            #               hilite_re=badwords_re)
+
+            if issectionedit:
+                self.send_page(self.request, content_id=content_id, content_only=1,
+                           hilite_re=badwords_re, do_cache=False)
+            else:
+                self.send_page(self.request, content_id=content_id, content_only=1,
                            hilite_re=badwords_re)
 
         self.request.write(self.request.formatter.endContent()) # end content div
--- MoinMoin/multiconfig.py.orig	Sun Sep 17 15:37:04 2006
+++ MoinMoin/multiconfig.py	Tue Feb 13 16:48:00 2007
@@ -342,6 +342,7 @@
     show_login = 1
     show_names = True
     show_section_numbers = 0
+    allow_section_edit = 1
     show_timings = 0
     show_version = 0
     siteid = 'default'
--- MoinMoin/formatter/text_html.py.orig	Thu May 11 12:24:00 2006
+++ MoinMoin/formatter/text_html.py	Tue Feb 13 16:48:00 2007
@@ -197,6 +197,16 @@
         self.request = request
         self.cfg = request.cfg
 
+        # for section editing
+        self._allow_section_edit = None
+        if not hasattr(request, 'sectionindex'):
+            self.request.sectionindex = 0
+        
+        self.sectionstack = []
+        self.sectionpool = {}
+        self.sectionlastdepth = 0
+        self.lineno = 0
+        
         if not hasattr(request, '_fmt_hd_counters'):
             request._fmt_hd_counters = []
 
@@ -584,6 +594,7 @@
         return '<span class="anchor" id="%s"></span>' % wikiutil.escape(id, 1)
 
     def line_anchordef(self, lineno):
+        self.lineno = lineno
         if line_anchors:
             return self.anchordef("line-%d" % lineno)
         else:
@@ -1175,6 +1186,21 @@
         if not self._base_depth:
             self._base_depth = depth
 
+        # section editing configuration
+        if self._allow_section_edit is None:
+            # need to add config
+            self._allow_section_edit = self.cfg.allow_section_edit
+            # self._allow_section_edit = 1
+            sectionediting = self.request.getPragma('section-edit', '').lower()
+            if sectionediting in ['off']:
+                self._allow_section_edit = 0
+            elif sectionediting in ['on']:
+                self._allow_section_edit = 1
+        
+        if self._allow_section_edit:
+            if not self.request.user.may.write(self.page.page_name):
+                self._allow_section_edit = 0
+        
         count_depth = max(depth - (self._base_depth - 1), 1)
 
         # check numbering, possibly changing the default
@@ -1193,7 +1219,35 @@
 
         # closing tag, with empty line after, to make source more readable
         if not on:
-            return self._close('h%d' % heading_depth) + '\n'
+            # section-editing
+            #return self._close('h%d' % heading_depth) + '\n'
+            result = self._close('h%d' % heading_depth) + '\n'
+            
+            sectionscript = ''
+            if self._allow_section_edit:
+                
+                self.request.sectionindex += 1
+                sectionindex = self.request.sectionindex
+                sectionscript = self.savesectioninfor(sectionindex, depth, self.lineno)
+                
+                section_attr = 'sectionedit%d' % sectionindex
+                
+                backto = u''
+                if self._is_included and hasattr(self.request, "_Include_backto"):
+                    backto = u'&backto=%s' % wikiutil.quoteWikinameURL(self.request._Include_backto)
+                
+                sectioneditpage = u'%s/%s' % (self.request.getScriptname(), wikiutil.quoteWikinameURL(self.page.page_name))
+                srev = self.page.current_rev()
+                querystring = u'%s?action=edit%s&srev=%d&startline=%d' % (sectioneditpage, backto, srev, self.lineno)
+                
+                result = ('%s%s%s%s' %
+                    (self.url(1, querystring, unescaped=1, title='Edit this section', id=section_attr),
+                     self.icon('edit'),
+                     self.url(0),
+                     result))
+
+            return ' %s%s' % (result, sectionscript)
+            # end of section-editing
             
         # create section number
         number = ''
@@ -1325,6 +1379,55 @@
                              allowed_attrs=self._allowed_table_attrs['row'],
                              **kw)
         return self._close(tag) + '\n'
+    
+    # section-editing
+    
+    def savesectioninfor(self, index, depth, lineno, save=1):
+        # store section information
+        section = {}
+        sectionscriptlist = []
+        sectionscript = u'document.getElementById("sectionedit%d").href += "&endline=%d";'
+        scriptresult = ''
+        
+        lastdepth = self.sectionlastdepth
+        sectionindex = index
+        
+        if lastdepth >= depth:
+            while 1:
+                if len(self.sectionstack):
+                    lastsection = self.sectionstack.pop()
+                    lastdepth = lastsection['depth']    
+                    if lastdepth >= depth:
+                        self.sectionpool[lastsection['index']]['endlineno'] = lineno - 1
+                        sectionscriptlist.append(sectionscript % (lastsection['index'], lineno - 1))
+                    else:
+                        self.sectionstack.append(lastsection)
+                        break
+                else:
+                    break
+                    
+        if save:
+            section['index'] = sectionindex
+            section['startlineno'] = lineno
+            section['depth'] = depth
+            section['endlineno'] = -1
+            self.sectionlastdepth = depth
+            self.sectionpool[sectionindex] = section
+            self.sectionstack.append(section)
+        
+        if len(sectionscriptlist) > 0:
+            
+            scriptresult = u"""
+<script type="text/javascript">
+<!--
+%s
+//-->
+</script>
+""" % u'\n'.join(sectionscriptlist)
+
+        return scriptresult
+
+    # end of section-editing 
     
     def table_cell(self, on, attrs=None, **kw):
         tag = 'td'
--- MoinMoin/wikiaction.py.orig	Sun Sep 17 15:37:26 2006
+++ MoinMoin/wikiaction.py	Tue Feb 13 16:49:49 2007
@@ -606,6 +606,28 @@
         if not cancelled:
             raise
 
+        # section editing
+    startline = int(request.form.get('startline', ['0'])[0])
+    endline = int(request.form.get('endline', ['0'])[0])
+        
+    if startline or endline:
+        issectionedit = 1
+        
+        if not startline:
+            startline = 1
+        
+        if not endline:
+            endline = -1    
+
+    else:
+        issectionedit = 0
+
+    if issectionedit:
+        savetext = pg.normalizeText(savetext, stripspaces=rstrip)
+        sectiontext = savetext
+        savetext = mergesection(pg.get_raw_body(), savetext, startline, endline)
+
+
     if cancelled:
         pg.sendCancel(savetext or "", rev)
         return
@@ -647,11 +669,21 @@
     if (request.form.has_key('button_preview') or
         request.form.has_key('button_spellcheck') or
         request.form.has_key('button_newwords')):
-        pg.sendEditor(preview=savetext, comment=comment)
+        #pg.sendEditor(preview=savetext, comment=comment)
+        
+        if issectionedit:
+            pg.sendEditor(preview=sectiontext, comment=comment, pagetext=savetext)
+        else:
+            pg.sendEditor(preview=savetext, comment=comment)
     
     # Preview with mode switch
     elif request.form.has_key('button_switch'):
-        pg.sendEditor(preview=savetext, comment=comment, staytop=1)
+        #pg.sendEditor(preview=savetext, comment=comment, staytop=1)
+        
+        if issectionedit:
+            pg.sendEditor(preview=sectiontext, comment=comment, staytop=1, pagetext=savetext)
+        else:
+            pg.sendEditor(preview=savetext, comment=comment, staytop=1)
     
     # Save new text
     else:
@@ -672,7 +704,13 @@
                                            querystr='action=diff&rev=%d' % rev)
                     }
                 # We don't send preview when we do merge conflict
-                pg.sendEditor(msg=conflict_msg, comment=comment)
+                #pg.sendEditor(msg=conflict_msg, comment=comment)
+                
+                if issectionedit:
+                    conflict_msg = u'%s %s' % (conflict_msg, _('Section editing is canceled.'))
+                
+                pg.sendEditor(msg=conflict_msg, comment=comment, issectionedit=0)
+                
                 return
             else:
                 savemsg = conflict_msg
@@ -915,4 +953,17 @@
         handler = globals().get('do_' + action)
         
     return handler
+
+def mergesection(pagetext, newsectiontext, startline, endline):
+    
+    pagetext = pagetext.split('\n')
+    
+    prevtext = u'%s\n' % u'\n'.join(pagetext[:startline-1])
+    if endline == -1:
+        nexttext = ''
+    else:
+        nexttext = u'\n%s' % u'\n'.join(pagetext[endline:])
+    
+    return u'%s%s%s' % (prevtext, newsectiontext, nexttext)
+
 
