--- PageGraphicalEditor-1.5b2.py	2005-11-06 06:35:56.000000000 +0900
+++ PageGraphicalEditor.py	2005-11-24 09:50:32.149867300 +0900
@@ -63,6 +63,27 @@
         edit_lock_message = None
         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  
 
         # check edit permissions
         if not self.request.user.may.write(self.page_name):
@@ -94,7 +115,10 @@
             title = _('Edit "%(pagename)s"')
         else:
             title = _('Preview of "%(pagename)s"')
-            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
@@ -134,9 +158,19 @@
                 # 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
@@ -193,13 +227,19 @@
         # Generate default content for new pages
         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(),
             wikiutil.quoteWikinameURL(self.page_name),
             ))
-
+        
         # yet another weird workaround for broken IE6 (it expands the text
         # editor area to the right after you begin to type...). IE sucks...
         # http://fplanque.net/2003/Articles/iecsstextarea/
@@ -210,6 +250,11 @@
         # Send revision of the page our edit is based on
         self.request.write('<input type="hidden" name="rev" value="%d">' % (rev,))
 
+        # 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:
@@ -328,9 +373,12 @@
             })
 
         self.request.write("</p>")
-
+        
         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')):
@@ -345,10 +393,16 @@
                 content_id = 'previewbelow'
             else:
                 content_id = 'preview'
-            self.send_page(self.request, content_id=content_id, content_only=1,
+            
+            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
         self.request.theme.emit_custom_html(self.cfg.page_footer1)
         self.request.theme.emit_custom_html(self.cfg.page_footer2)
 
+    
