--- wikiaction-1.5b2.py	2005-10-23 21:50:50.000000000 +0900
+++ wikiaction.py	2005-11-24 09:47:28.196742300 +0900
@@ -563,10 +563,6 @@
         from MoinMoin.PageEditor import PageEditor
         pg = PageEditor(request, pagename)
 
-    # Edit was canceled
-    if request.form.has_key('button_cancel'):
-        pg.sendCancel(savetext, rev)
-        return
 
     # is invoked without savetext start editing
     if savetext is None:
@@ -577,11 +573,39 @@
     if lasteditor == 'gui':
         from MoinMoin.converter.text_html_text_x_moin import convert
         savetext = convert(request, pagename, savetext) # XXX error handling
+    
+    # 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)
+
+
+    # Edit was canceled
+    if request.form.has_key('button_cancel'):
+        pg.sendCancel(savetext, rev)
+        return
 
     # IMPORTANT: normalize text from the form. This should be done in
     # one place before we manipulate the text.
-    savetext = pg.normalizeText(savetext, stripspaces=rstrip)
-
+    if not issectionedit:
+        savetext = pg.normalizeText(savetext, stripspaces=rstrip)
+    
     # Add category
 
     # TODO: this code does not work with extended links, and is doing
@@ -630,11 +654,17 @@
     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)
+        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)
+        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:
@@ -655,7 +685,10 @@
                                            querystr='action=diff&rev=%d' % rev)
                     }
                 # We don't send preview when we do merge conflict
-                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
@@ -949,6 +982,15 @@
         
     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)
