Browse Source

Reorganize org-edit-src-code; allow buffer-local `org-edit-src-content-indentation'

* lisp/org-src.el (org-edit-src-code): When generating the code edit
buffer, it is necessary for several variables to inherit their values
from the parent org buffer. These changes collect all such variables
together into a single association list of (variable-name value)
pairs. In addition, a new variable is added to the list:
`org-edit-src-content-indentation'. This has the effect that a buffer
local value can be used for that variable.
Dan Davison 10 years ago
1 changed files with 24 additions and 21 deletions
  1. 24 21

+ 24 - 21

@@ -214,14 +214,12 @@ buffer."
   (let ((mark (and (org-region-active-p) (mark)))
 	(case-fold-search t)
 	(info (org-edit-src-find-region-and-lang))
-	(babel-info (org-babel-get-src-block-info 'light))
-	(org-mode-p (eq major-mode 'org-mode))
+	(org-mode-p (org-mode-p))
 	(beg (make-marker))
 	(end (make-marker))
-	(preserve-indentation org-src-preserve-indentation)
 	(allow-write-back-p (null code))
 	block-nindent total-nindent ovl lang lang-f single lfmt buffer msg
-	begline markline markcol line col)
+	begline markline markcol line col transmitted-variables)
     (if (not info)
       (setq beg (move-marker beg (nth 0 info))
@@ -235,10 +233,22 @@ buffer."
                      (nth 2 info))
 	    lang (if (symbolp lang) (symbol-name lang) lang)
 	    single (nth 3 info)
-	    lfmt (nth 4 info)
 	    block-nindent (nth 5 info)
 	    lang-f (intern (concat lang "-mode"))
-	    begline (save-excursion (goto-char beg) (org-current-line)))
+	    begline (save-excursion (goto-char beg) (org-current-line))
+	    transmitted-variables
+	    `((org-edit-src-content-indentation
+	       ,org-edit-src-content-indentation)
+	      (org-edit-src-force-single-line ,single)
+	      (org-edit-src-from-org-mode ,org-mode-p)
+	      (org-edit-src-allow-write-back-p ,allow-write-back-p)
+	      (org-src-preserve-indentation ,org-src-preserve-indentation)
+	      (org-src-babel-info ,(org-babel-get-src-block-info 'light))
+	      (org-coderef-label-format
+	       ,(or (nth 4 info) org-coderef-label-format))
+	      (org-edit-src-beg-marker ,beg)
+	      (org-edit-src-end-marker ,end)
+	      (org-edit-src-block-indentation ,block-nindent)))
       (if (and mark (>= mark beg) (<= mark (1+ end)))
 	  (save-excursion (goto-char (min mark end))
 			  (setq markline (org-current-line)
@@ -278,27 +288,23 @@ buffer."
 		       (define-key map [mouse-1] 'org-edit-src-continue)
 	(overlay-put ovl :read-only "Leave me alone")
+	(setq transmitted-variables
+	      (append transmitted-variables `((org-edit-src-overlay ,ovl))))
 	(org-src-switch-to-buffer buffer 'edit)
 	(if (eq single 'macro-definition)
 	    (setq code (replace-regexp-in-string "\\\\n" "\n" code t t)))
 	(insert code)
 	(remove-text-properties (point-min) (point-max)
 				'(display nil invisible nil intangible nil))
-	(unless preserve-indentation
+	(unless (cadr (assq 'org-src-preserve-indentation transmitted-variables))
 	  (setq total-nindent (or (org-do-remove-indentation) 0)))
 	(let ((org-inhibit-startup t))
 	  (condition-case e
 	      (funcall lang-f)
 	     (error "Language mode `%s' fails with: %S" lang-f (nth 1 e)))))
-	(set (make-local-variable 'org-edit-src-force-single-line) single)
-	(set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
-	(set (make-local-variable 'org-edit-src-allow-write-back-p) allow-write-back-p)
-	(set (make-local-variable 'org-src-preserve-indentation) preserve-indentation)
-	(when babel-info
-	  (set (make-local-variable 'org-src-babel-info) babel-info))
-	(when lfmt
-	  (set (make-local-variable 'org-coderef-label-format) lfmt))
+	(dolist (pair transmitted-variables)
+	  (org-set-local (car pair) (cadr pair)))
 	(when org-mode-p
 	  (goto-char (point-min))
 	  (while (re-search-forward "^," nil t)
@@ -307,16 +313,13 @@ buffer."
 	(when markline
 	  (org-goto-line (1+ (- markline begline)))
-	   (if preserve-indentation markcol (max 0 (- markcol total-nindent))))
+	   (if org-src-preserve-indentation markcol
+	     (max 0 (- markcol total-nindent))))
 	  (push-mark (point) 'no-message t)
 	  (setq deactivate-mark nil))
 	(org-goto-line (1+ (- line begline)))
-	 (if preserve-indentation col (max 0 (- col total-nindent))))
-	(org-set-local 'org-edit-src-beg-marker beg)
-	(org-set-local 'org-edit-src-end-marker end)
-	(org-set-local 'org-edit-src-overlay ovl)
-	(org-set-local 'org-edit-src-block-indentation block-nindent)
+	 (if org-src-preserve-indentation col (max 0 (- col total-nindent))))
 	(set-buffer-modified-p nil)
 	(and org-edit-src-persistent-message