Browse Source

Editing code examples: No longer use indirect buffer

Editing source code examples and pictures is not done in a truly
separate buffer, not in an indirect buffer.  Indirect buffers had
caused problems with fontification, for example.
Carsten Dominik 16 years ago
parent
commit
fc7b9e8d8c
2 changed files with 60 additions and 31 deletions
  1. 4 0
      lisp/ChangeLog
  2. 56 31
      lisp/org.el

+ 4 - 0
lisp/ChangeLog

@@ -1,5 +1,9 @@
 2009-05-05  Carsten Dominik  <carsten.dominik@gmail.com>
 
+	* org.el (org-edit-src-code, org-edit-fixed-width-region): Use
+	separate buffer instead of indirect buffer to edit source code.
+	(org-edit-src-exit): Make this function work with the new setup.
+
 	* org-clock.el (org-clock-insert-selection-line): Make sure tasks
 	are properly fontified before shown in the selection menu.
 

+ 56 - 31
lisp/org.el

@@ -6309,6 +6309,8 @@ If WITH-CASE is non-nil, the sorting will be case-sensitive."
 (defvar org-edit-src-force-single-line nil)
 (defvar org-edit-src-from-org-mode nil)
 (defvar org-edit-src-picture nil)
+(defvar org-edit-src-beg-marker nil)
+(defvar org-edit-src-end-marker nil)
 
 (define-minor-mode org-exit-edit-mode
   "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
@@ -6325,25 +6327,28 @@ exit by killing the buffer with \\[org-edit-src-exit]."
 	      "Edit, then exit with C-c ' (C-c and single quote)"))
 	(info (org-edit-src-find-region-and-lang))
 	(org-mode-p (eq major-mode 'org-mode))
-	beg end lang lang-f single lfmt)
+	(beg (make-marker))
+	(end (make-marker))
+	lang lang-f single lfmt code begline)
     (if (not info)
 	nil
-      (setq beg (nth 0 info)
-	    end (nth 1 info)
+      (setq beg (move-marker beg (nth 0 info))
+	    end (move-marker end (nth 1 info))
+	    code (buffer-substring-no-properties beg end)
 	    lang (nth 2 info)
 	    single (nth 3 info)
 	    lfmt (nth 4 info)
-	    lang-f (intern (concat lang "-mode")))
+	    lang-f (intern (concat lang "-mode"))
+	    begline (save-excursion (goto-char beg) (org-current-line)))
       (unless (functionp lang-f)
 	(error "No such language mode: %s" lang-f))
       (goto-line line)
       (if (get-buffer "*Org Edit Src Example*")
 	  (kill-buffer "*Org Edit Src Example*"))
-      (switch-to-buffer (make-indirect-buffer (current-buffer)
-					      "*Org Edit Src Example*"))
-      (narrow-to-region beg end)
-      (remove-text-properties beg end '(display nil invisible nil
-						intangible nil))
+      (switch-to-buffer (get-buffer-create "*Org Edit Src Example*"))
+      (insert code)
+      (remove-text-properties (point-min) (point-max)
+			      '(display nil invisible nil intangible nil))
       (let ((org-inhibit-startup t))
 	(funcall lang-f))
       (set (make-local-variable 'org-edit-src-force-single-line) single)
@@ -6354,8 +6359,10 @@ exit by killing the buffer with \\[org-edit-src-exit]."
 	(goto-char (point-min))
 	(while (re-search-forward "^," nil t)
 	  (replace-match "")))
-      (goto-line line)
+      (goto-line (1+ (- line begline)))
       (org-exit-edit-mode)
+      (org-set-local 'org-edit-src-beg-marker beg)
+      (org-set-local 'org-edit-src-end-marker end)
       (org-set-local 'header-line-format msg)
       (message "%s" msg)
       t)))
@@ -6373,30 +6380,33 @@ exit by killing the buffer with \\[org-edit-src-exit]."
 	(msg (substitute-command-keys
 	      "Edit, then exit with C-c ' (C-c and single quote)"))
 	(org-mode-p (eq major-mode 'org-mode))
-	beg end)
+	(beg (make-marker))
+	(end (make-marker))
+	beg1 end1 code begline)
     (beginning-of-line 1)
     (if (looking-at "[ \t]*[^:\n \t]")
 	nil
       (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
-	  (setq beg (point) end beg)
+	  (setq beg1 (point) end1 beg1)
 	(save-excursion
 	  (if (re-search-backward "^[ \t]*[^:]" nil 'move)
-	      (setq beg (point-at-bol 2))
-	    (setq beg (point))))
+	      (setq beg1 (point-at-bol 2))
+	    (setq beg1 (point))))
 	(save-excursion
 	  (if (re-search-forward "^[ \t]*[^:]" nil 'move)
-	      (setq end (1- (match-beginning 0)))
-	    (setq end (point))))
+	      (setq end1 (1- (match-beginning 0)))
+	    (setq end1 (point))))
 	(goto-line line))
+      (setq beg (move-marker beg beg1)
+	    end (move-marker end end1)
+	    code (buffer-substring-no-properties beg end)
+	    begline (save-excursion (goto-char beg) (org-current-line)))
       (if (get-buffer "*Org Edit Picture*")
 	  (kill-buffer "*Org Edit Picture*"))
-      (switch-to-buffer (make-indirect-buffer (current-buffer)
-					      "*Org Edit Picture*"))
-      (narrow-to-region beg end)
-      (remove-text-properties beg end '(display nil invisible nil
-						intangible nil))
-      (when (fboundp 'font-lock-unfontify-region)
-	(font-lock-unfontify-region (point-min) (point-max)))
+      (switch-to-buffer (get-buffer-create "*Org Edit Picture*"))
+      (insert code)
+      (remove-text-properties (point-min) (point-max)
+			      '(display nil invisible nil intangible nil))
       (cond
        ((eq org-edit-fixed-width-region-mode 'artist-mode)
 	(fundamental-mode)
@@ -6408,13 +6418,14 @@ exit by killing the buffer with \\[org-edit-src-exit]."
       (goto-char (point-min))
       (while (re-search-forward "^[ \t]*: ?" nil t)
 	(replace-match ""))
-      (goto-line line)
+      (goto-line (1+ (- line begline)))
       (org-exit-edit-mode)
+      (org-set-local 'org-edit-src-beg-marker beg)
+      (org-set-local 'org-edit-src-end-marker end)
       (org-set-local 'header-line-format msg)
       (message "%s" msg)
       t)))
 
-
 (defun org-edit-src-find-region-and-lang ()
   "Find the region and language for a local edit.
 Return a list with beginning and end of the region, a string representing
@@ -6494,10 +6505,16 @@ the language, a switch telling of the content should be in a single line."
 (defun org-edit-src-exit ()
   "Exit special edit and protect problematic lines."
   (interactive)
-  (unless (buffer-base-buffer (current-buffer))
-    (error "This is not an indirect buffer, something is wrong..."))
-  (unless (> (point-min) 1)
-    (error "This buffer is not narrowed, something is wrong..."))
+  (unless (member (buffer-name)
+		  '("*Org Edit Src Example*" "*Org Edit Picture*"))
+    (error "This is not an sub-editing buffer, something is wrong..."))
+  (let ((line (if (org-bound-and-true-p org-edit-src-force-single-line)
+		  1
+		(org-current-line)))
+	(beg org-edit-src-beg-marker)
+	(end org-edit-src-end-marker)
+	(buffer (current-buffer))
+	code)
   (goto-char (point-min))
   (if (looking-at "[ \t\n]*\n") (replace-match ""))
   (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
@@ -6524,8 +6541,16 @@ the language, a switch telling of the content should be in a single line."
     (when font-lock-mode
       (font-lock-unfontify-region (point-min) (point-max)))
     (put-text-property (point-min) (point-max) 'font-lock-fontified t))
-  (kill-buffer (current-buffer))
-  (and (org-mode-p) (org-restart-font-lock)))
+  (setq code (buffer-string))
+  (switch-to-buffer (marker-buffer beg))
+  (kill-buffer buffer)
+  (goto-char beg)
+  (delete-region beg end)
+  (insert code)
+  (goto-char beg)
+  (goto-line (1- (+ (org-current-line) line)))
+  (move-marker beg nil)
+  (move-marker end nil)))
 
 
 ;;; The orgstruct minor mode