Browse Source

marginally less hacky comma escaping of org blocks

* ob.el (org-babel-insert-result): Replace key sequence with
  function call.  Use a more informative flag to the local function.
  (org-add-protective-commas): Declare a new external function.
* org-src.el (org-add-protective-commas): This should be its own
  function.
  (org-edit-src-exit): Use the new function.
Eric Schulte 12 years ago
parent
commit
7a06ed790a
2 changed files with 20 additions and 7 deletions
  1. 3 2
      lisp/ob.el
  2. 17 5
      lisp/org-src.el

+ 3 - 2
lisp/ob.el

@@ -90,6 +90,7 @@
 (declare-function org-strip-protective-commas "org" (beg end))
 (declare-function org-remove-if "org" (predicate seq))
 (declare-function org-completing-read "org" (&rest args))
+(declare-function org-add-protective-commas "org-src" (beg end))
 
 (defgroup org-babel nil
   "Code block evaluation and management in `org-mode' documents."
@@ -1939,7 +1940,7 @@ code ---- the results are extracted in the syntax of the source
 	(let ((wrap (lambda (start finish &optional escape)
 		      (goto-char end) (insert (concat finish "\n"))
 		      (goto-char beg) (insert (concat start "\n"))
-		      (if escape (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
+		      (if escape (org-add-protective-commas (point) end))
 		      (goto-char end) (goto-char (point-at-eol))
 		      (setq end (point-marker))))
 	      (proper-list-p (lambda (it) (and (listp it) (null (cdr (last it)))))))
@@ -1986,7 +1987,7 @@ code ---- the results are extracted in the syntax of the source
 	   ((member "latex" result-params)
 	    (funcall wrap "#+BEGIN_LaTeX" "#+END_LaTeX"))
 	   ((member "org" result-params)
-	    (funcall wrap "#+BEGIN_SRC org" "#+END_SRC" t))
+	    (funcall wrap "#+BEGIN_SRC org" "#+END_SRC" 'escape))
 	   ((member "code" result-params)
 	    (funcall wrap (format "#+BEGIN_SRC %s%s" (or lang "none") results-switches)
 		     "#+END_SRC"))

+ 17 - 5
lisp/org-src.el

@@ -590,6 +590,21 @@ the language, a switch telling if the content should be in a single line."
     (goto-char pos)
     (org-get-indentation)))
 
+(defun org-add-protective-commas (beg end &optional line)
+  "Add protective commas in region.
+Return the delta in size of the region."
+  (interactive "r")
+  (let ((org-re "^\\(.\\)")
+	(other-re "^\\([*]\\|[ \t]*#\\+\\)")
+	(delta 0))
+    (save-excursion
+      (goto-char beg)
+      (while (re-search-forward (if (derived-mode-p 'org-mode) org-re other-re)
+				end t)
+	(if (and line (eq (org-current-line) line)) (setq delta (1+ delta)))
+	(replace-match ",\\1")))
+    delta))
+
 (defun org-edit-src-exit (&optional context)
   "Exit special edit and protect problematic lines."
   (interactive)
@@ -634,11 +649,8 @@ the language, a switch telling if the content should be in a single line."
 	(goto-char (point-min))
 	(if (looking-at "\\s-*") (replace-match " ")))
       (when (org-bound-and-true-p org-edit-src-from-org-mode)
-	(goto-char (point-min))
-	(while (re-search-forward
-		(if (derived-mode-p 'org-mode) "^\\(.\\)" "^\\([*]\\|[ \t]*#\\+\\)") nil t)
-	  (if (eq (org-current-line) line) (setq delta (1+ delta)))
-	  (replace-match ",\\1")))
+	(setq delta (+ delta (org-add-protective-commas
+			      (point-min) (point-max) line))))
       (when (org-bound-and-true-p org-edit-src-picture)
 	(setq preserve-indentation nil)
 	(untabify (point-min) (point-max))