Browse Source

Blocks: Make editing indented blocks work correctly

Carsten Dominik 16 years ago
parent
commit
1fc138838a
3 changed files with 43 additions and 22 deletions
  1. 10 0
      lisp/ChangeLog
  2. 5 2
      lisp/org-exp.el
  3. 28 20
      lisp/org.el

+ 10 - 0
lisp/ChangeLog

@@ -1,3 +1,13 @@
+2009-05-26  Carsten Dominik  <carsten.dominik@gmail.com>
+
+	* org-exp.el (org-export-format-source-code-or-example): Make
+	editing indented blocks work correctly.
+
+	* org.el (org-edit-src-nindent): New variable.
+	(org-edit-src-code, org-edit-fixed-width-region)
+	(org-edit-src-find-region-and-lang, org-edit-src-exit): Make
+	editing indented blocks work correctly.
+
 2009-05-24  Carsten Dominik  <carsten.dominik@gmail.com>
 2009-05-24  Carsten Dominik  <carsten.dominik@gmail.com>
 
 
 	* org-exp.el (org-export-replace-src-segments-and-examples): FInd
 	* org-exp.el (org-export-replace-src-segments-and-examples): FInd

+ 5 - 2
lisp/org-exp.el

@@ -2170,13 +2170,16 @@ Numbering lines works for all three major backends (html, latex, and ascii)."
 	(setq num nil cont nil lang nil))
 	(setq num nil cont nil lang nil))
       (if keepp (setq rpllbl 'keep))
       (if keepp (setq rpllbl 'keep))
       (setq rtn (org-remove-indentation code))
       (setq rtn (org-remove-indentation code))
-      (when (equal lang "org")
+      (when (string-match "^," rtn)
 	(setq rtn (with-temp-buffer
 	(setq rtn (with-temp-buffer
 		    (insert rtn)
 		    (insert rtn)
 		    ;; Free up the protected lines
 		    ;; Free up the protected lines
 		    (goto-char (point-min))
 		    (goto-char (point-min))
 		    (while (re-search-forward "^," nil t)
 		    (while (re-search-forward "^," nil t)
-		      (replace-match "")
+		      (if (or (equal lang "org")
+			      (save-match-data
+				(looking-at "\\([*#]\\|[ \t]*#\\+\\)")))
+			  (replace-match ""))
 		      (end-of-line 1))
 		      (end-of-line 1))
 		    (buffer-string))))
 		    (buffer-string))))
       ;; Now backend-specific coding
       ;; Now backend-specific coding

+ 28 - 20
lisp/org.el

@@ -6460,6 +6460,7 @@ If WITH-CASE is non-nil, the sorting will be case-sensitive."
 (defvar org-edit-src-beg-marker nil)
 (defvar org-edit-src-beg-marker nil)
 (defvar org-edit-src-end-marker nil)
 (defvar org-edit-src-end-marker nil)
 (defvar org-edit-src-overlay nil)
 (defvar org-edit-src-overlay nil)
+(defvar org-edit-src-nindent nil)
 
 
 (define-minor-mode org-exit-edit-mode
 (define-minor-mode org-exit-edit-mode
   "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
   "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
@@ -6479,7 +6480,7 @@ the edited version."
 	(org-mode-p (eq major-mode 'org-mode))
 	(org-mode-p (eq major-mode 'org-mode))
 	(beg (make-marker))
 	(beg (make-marker))
 	(end (make-marker))
 	(end (make-marker))
-	ovl lang lang-f single lfmt code begline buffer)
+	nindent	ovl lang lang-f single lfmt code begline buffer)
     (if (not info)
     (if (not info)
 	nil
 	nil
       (setq beg (move-marker beg (nth 0 info))
       (setq beg (move-marker beg (nth 0 info))
@@ -6517,6 +6518,7 @@ the edited version."
 	(insert code)
 	(insert code)
 	(remove-text-properties (point-min) (point-max)
 	(remove-text-properties (point-min) (point-max)
 				'(display nil invisible nil intangible nil))
 				'(display nil invisible nil intangible nil))
+	(setq nindent (org-do-remove-indentation))
 	(let ((org-inhibit-startup t))
 	(let ((org-inhibit-startup t))
 	  (funcall lang-f))
 	  (funcall lang-f))
 	(set (make-local-variable 'org-edit-src-force-single-line) single)
 	(set (make-local-variable 'org-edit-src-force-single-line) single)
@@ -6532,6 +6534,7 @@ the edited version."
 	(org-set-local 'org-edit-src-beg-marker beg)
 	(org-set-local 'org-edit-src-beg-marker beg)
 	(org-set-local 'org-edit-src-end-marker end)
 	(org-set-local 'org-edit-src-end-marker end)
 	(org-set-local 'org-edit-src-overlay ovl)
 	(org-set-local 'org-edit-src-overlay ovl)
+	(org-set-local 'org-edit-src-nindent nindent)
 	(and org-edit-src-persistent-message
 	(and org-edit-src-persistent-message
 	     (org-set-local 'header-line-format msg)))
 	     (org-set-local 'header-line-format msg)))
       (message "%s" msg)
       (message "%s" msg)
@@ -6575,7 +6578,7 @@ the fragment in the Org-mode buffer."
 	(org-mode-p (eq major-mode 'org-mode))
 	(org-mode-p (eq major-mode 'org-mode))
 	(beg (make-marker))
 	(beg (make-marker))
 	(end (make-marker))
 	(end (make-marker))
-	ovl beg1 end1 code begline buffer)
+	nindent ovl beg1 end1 code begline buffer)
     (beginning-of-line 1)
     (beginning-of-line 1)
     (if (looking-at "[ \t]*[^:\n \t]")
     (if (looking-at "[ \t]*[^:\n \t]")
 	nil
 	nil
@@ -6618,6 +6621,7 @@ the fragment in the Org-mode buffer."
 	(insert code)
 	(insert code)
 	(remove-text-properties (point-min) (point-max)
 	(remove-text-properties (point-min) (point-max)
 				'(display nil invisible nil intangible nil))
 				'(display nil invisible nil intangible nil))
+	(setq nindent (org-do-remove-indentation))
 	(cond
 	(cond
 	 ((eq org-edit-fixed-width-region-mode 'artist-mode)
 	 ((eq org-edit-fixed-width-region-mode 'artist-mode)
 	  (fundamental-mode)
 	  (fundamental-mode)
@@ -6634,6 +6638,7 @@ the fragment in the Org-mode buffer."
 	(org-set-local 'org-edit-src-beg-marker beg)
 	(org-set-local 'org-edit-src-beg-marker beg)
 	(org-set-local 'org-edit-src-end-marker end)
 	(org-set-local 'org-edit-src-end-marker end)
 	(org-set-local 'org-edit-src-overlay ovl)
 	(org-set-local 'org-edit-src-overlay ovl)
+	(org-set-local 'org-edit-src-nindent nindent)
 	(and org-edit-src-persistent-message
 	(and org-edit-src-persistent-message
 	     (org-set-local 'header-line-format msg)))
 	     (org-set-local 'header-line-format msg)))
       (message "%s" msg)
       (message "%s" msg)
@@ -6654,14 +6659,16 @@ the language, a switch telling of the content should be in a single line."
 	    ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
 	    ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
 	    ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
 	    ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
 	    ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
 	    ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
-	    ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
-	    ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
-	    ("^#\\+html:" "\n" "html" single-line)
-	    ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
-	    ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
-	    ("^#\\+latex:" "\n" "latex" single-line)
-	    ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
-	    ("^#\\+ascii:" "\n" "ascii" single-line)
+	    ("^[ \t]*#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n[ \t]*#\\+end_src" 2)
+	    ("^[ \t]*#\\+begin_example.*\n" "\n[ \t]*#\\+end_example" "fundamental")
+	    ("^[ \t]*#\\+html:" "\n" "html" single-line)
+	    ("^[ \t]*#\\+begin_html.*\n" "\n[ \t]*#\\+end_html" "html")
+	    ("^[ \t]*#\\+latex:" "\n" "latex" single-line)
+	    ("^[ \t]*#\\+begin_latex.*\n" "\n[ \t]*#\\+end_latex" "latex")
+	    ("^[ \t]*#\\+ascii:" "\n" "fundamental" single-line)
+	    ("^[ \t]*#\\+begin_ascii.*\n" "\n[ \t]*#\\+end_ascii" "fundamental")
+	    ("^[ \t]*#\\+docbook:" "\n" "xml" single-line)
+	    ("^[ \t]*#\\+begin_docbook.*\n" "\n[ \t]*#\\+end_docbook" "xml")
 	    )))
 	    )))
 	(pos (point))
 	(pos (point))
 	re1 re2 single beg end lang lfmt match-re1)
 	re1 re2 single beg end lang lfmt match-re1)
@@ -6727,6 +6734,7 @@ the language, a switch telling of the content should be in a single line."
 	(end org-edit-src-end-marker)
 	(end org-edit-src-end-marker)
 	(ovl org-edit-src-overlay)
 	(ovl org-edit-src-overlay)
 	(buffer (current-buffer))
 	(buffer (current-buffer))
+	(nindent org-edit-src-nindent)
 	code)
 	code)
   (goto-char (point-min))
   (goto-char (point-min))
   (if (looking-at "[ \t\n]*\n") (replace-match ""))
   (if (looking-at "[ \t\n]*\n") (replace-match ""))
@@ -6741,19 +6749,19 @@ the language, a switch telling of the content should be in a single line."
 	(replace-match "")))
 	(replace-match "")))
   (when (org-bound-and-true-p org-edit-src-from-org-mode)
   (when (org-bound-and-true-p org-edit-src-from-org-mode)
     (goto-char (point-min))
     (goto-char (point-min))
-    (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
-      (replace-match ",\\1"))
-    (when font-lock-mode
-      (font-lock-unfontify-region (point-min) (point-max)))
-    (put-text-property (point-min) (point-max) 'font-lock-fontified t))
+    (while (re-search-forward
+	    (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\|[ \t]*#\\+\\)") nil t)
+      (replace-match ",\\1")))
   (when (org-bound-and-true-p org-edit-src-picture)
   (when (org-bound-and-true-p org-edit-src-picture)
     (untabify (point-min) (point-max))
     (untabify (point-min) (point-max))
     (goto-char (point-min))
     (goto-char (point-min))
     (while (re-search-forward "^" nil t)
     (while (re-search-forward "^" nil t)
-      (replace-match ": "))
-    (when font-lock-mode
-      (font-lock-unfontify-region (point-min) (point-max)))
-    (put-text-property (point-min) (point-max) 'font-lock-fontified t))
+      (replace-match ": ")))
+  (when nindent
+    (setq nindent (make-string nindent ?\ ))
+    (goto-char (point-min))
+    (while (re-search-forward "^" nil t)
+      (replace-match nindent)))
   (setq code (buffer-string))
   (setq code (buffer-string))
   (switch-to-buffer (marker-buffer beg))
   (switch-to-buffer (marker-buffer beg))
   (kill-buffer buffer)
   (kill-buffer buffer)
@@ -15479,7 +15487,7 @@ N may optionally be the number of spaces to remove."
       (while (re-search-forward re nil t)
       (while (re-search-forward re nil t)
 	(replace-match "")
 	(replace-match "")
 	(end-of-line 1))
 	(end-of-line 1))
-      n)))
+      min)))
 
 
 (defun org-base-buffer (buffer)
 (defun org-base-buffer (buffer)
   "Return the base buffer of BUFFER, if it has one.  Else return the buffer."
   "Return the base buffer of BUFFER, if it has one.  Else return the buffer."