Browse Source

Normalize comma-escaping of src-blocks and example-blocks

* lisp/org-src.el (org-escape-code-in-string,
  org-unescape-code-in-string, org-escape-code-in-region,
  org-unescape-code-in-region): New functions.
(org-edit-src-code, org-edit-src-exit): Use new functions.
* lisp/org.el (org-strip-protective-commas): Removed function.
* lisp/org-exp.el (org-export-select-backend-specific-text): Use new
  function.
* lisp/ob.el (org-babel-parse-src-block-match,
  org-babel-parse-inline-src-block-match, org-babel-insert-result):
  Always escape produced blocks, independently on the language of the
  block, if any.  Use new functions.
* doc/org.texi: Update documentation.
* testing/lisp/test-ob.el: Update test.
Nicolas Goaziou 8 years ago
parent
commit
fac86b03fe
6 changed files with 59 additions and 73 deletions
  1. 10 10
      doc/org.texi
  2. 7 21
      lisp/ob.el
  3. 2 1
      lisp/org-exp.el
  4. 39 24
      lisp/org-src.el
  5. 0 16
      lisp/org.el
  6. 1 1
      testing/lisp/test-ob.el

+ 10 - 10
doc/org.texi

@@ -9387,16 +9387,16 @@ so often, shortcuts are provided using the Easy Templates facility
 @item C-c '
 Edit the source code example at point in its native mode.  This works by
 switching to a temporary buffer with the source code.  You need to exit by
-pressing @kbd{C-c '} again@footnote{Upon exit, lines starting with @samp{*}
-or @samp{#} will get a comma prepended, to keep them from being interpreted
-by Org as outline nodes or special comments.  These commas will be stripped
-for editing with @kbd{C-c '}, and also for export.}.  The edited version will
-then replace the old version in the Org buffer.  Fixed-width regions
-(where each line starts with a colon followed by a space) will be edited
-using @code{artist-mode}@footnote{You may select a different-mode with the
-variable @code{org-edit-fixed-width-region-mode}.} to allow creating ASCII
-drawings easily.  Using this command in an empty line will create a new
-fixed-width region.
+pressing @kbd{C-c '} again@footnote{Upon exit, lines starting with @samp{*},
+@samp{,*}, @samp{#+} and @samp{,#+} will get a comma prepended, to keep them
+from being interpreted by Org as outline nodes or special syntax.  These
+commas will be stripped for editing with @kbd{C-c '}, and also for export.}.
+The edited version will then replace the old version in the Org buffer.
+Fixed-width regions (where each line starts with a colon followed by a space)
+will be edited using @code{artist-mode}@footnote{You may select
+a different-mode with the variable @code{org-edit-fixed-width-region-mode}.}
+to allow creating ASCII drawings easily.  Using this command in an empty line
+will create a new fixed-width region.
 @kindex C-c l
 @item C-c l
 Calling @code{org-store-link} while editing a source code example in a

+ 7 - 21
lisp/ob.el

@@ -39,7 +39,6 @@
 (declare-function show-all "outline" ())
 (declare-function org-reduce "org" (CL-FUNC CL-SEQ &rest CL-KEYS))
 (declare-function org-mark-ring-push "org" (&optional pos buffer))
-(declare-function org-strip-protective-commas "org" (beg end))
 (declare-function tramp-compat-make-temp-file "tramp-compat"
                   (filename &optional dir-flag))
 (declare-function tramp-dissect-file-name "tramp" (name &optional nodefault))
@@ -64,7 +63,6 @@
 (declare-function org-cycle "org" (&optional arg))
 (declare-function org-uniquify "org" (list))
 (declare-function org-current-level "org" ())
-(declare-function org-strip-protective-commas "org" (beg end))
 (declare-function org-table-import "org-table" (file arg))
 (declare-function org-add-hook "org-compat"
 		  (hook function &optional append local))
@@ -87,10 +85,10 @@
 (declare-function org-list-struct "org-list" ())
 (declare-function org-list-prevs-alist "org-list" (struct))
 (declare-function org-list-get-list-end "org-list" (item struct prevs))
-(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))
+(declare-function org-escape-code-in-region "org-src" (beg end))
+(declare-function org-unescape-code-in-string "org-src" (s))
 
 (defgroup org-babel nil
   "Code block evaluation and management in `org-mode' documents."
@@ -1241,7 +1239,7 @@ may be specified in the properties of the current outline entry."
           ;; get block body less properties, protective commas, and indentation
           (with-temp-buffer
             (save-match-data
-              (insert (org-babel-strip-protective-commas body lang))
+              (insert (org-unescape-code-in-string body))
 	      (unless preserve-indentation (org-do-remove-indentation))
               (buffer-string)))
 	  (org-babel-merge-params
@@ -1258,8 +1256,7 @@ may be specified in the properties of the current outline entry."
   (let* ((lang (org-no-properties (match-string 2)))
          (lang-headers (intern (concat "org-babel-default-header-args:" lang))))
     (list lang
-          (org-babel-strip-protective-commas
-           (org-no-properties (match-string 5)) lang)
+          (org-unescape-code-in-string (org-no-properties (match-string 5)))
           (org-babel-merge-params
            org-babel-default-inline-header-args
            (org-babel-params-from-properties lang)
@@ -1937,10 +1934,10 @@ code ---- the results are extracted in the syntax of the source
 	   ((member "prepend" result-params)))) ; already there
 	(setq results-switches
 	      (if results-switches (concat " " results-switches) ""))
-	(let ((wrap (lambda (start finish &optional escape)
+	(let ((wrap (lambda (start finish)
 		      (goto-char end) (insert (concat finish "\n"))
 		      (goto-char beg) (insert (concat start "\n"))
-		      (if escape (org-add-protective-commas (point) end))
+		      (org-escape-code-in-region (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)))))))
@@ -1987,7 +1984,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" 'escape))
+	    (funcall wrap "#+BEGIN_SRC org" "#+END_SRC"))
 	   ((member "code" result-params)
 	    (funcall wrap (format "#+BEGIN_SRC %s%s" (or lang "none") results-switches)
 		     "#+END_SRC"))
@@ -2370,17 +2367,6 @@ block but are passed literally to the \"example-block\"."
       (funcall nb-add (buffer-substring index (point-max))))
     new-body))
 
-(defun org-babel-strip-protective-commas (body &optional lang)
-  "Strip protective commas from bodies of source blocks."
-  (with-temp-buffer
-    (insert body)
-    (if (and lang (string= lang "org"))
-	(progn (goto-char (point-min))
-	       (while (re-search-forward "^[ \t]*\\(,\\)" nil t)
-		 (replace-match "" nil nil nil 1)))
-      (org-strip-protective-commas (point-min) (point-max)))
-    (buffer-string)))
-
 (defun org-babel-script-escape (str &optional force)
   "Safely convert tables into elisp lists."
   (let (in-single in-double out)

+ 2 - 1
lisp/org-exp.el

@@ -48,6 +48,7 @@
 (declare-function org-table-colgroup-line-p "org-table" (line))
 (declare-function org-pop-to-buffer-same-window "org-compat"
 		  (&optional buffer-or-name norecord label))
+(declare-function org-unescape-code-in-region "org-src" (beg end))
 
 (autoload 'org-export-generic "org-export-generic" "Export using the generic exporter" t)
 
@@ -1790,7 +1791,7 @@ from the buffer."
 		 beg-content end-content
 		 `(org-protected t original-indentation ,ind org-native-text t))
 		;; strip protective commas
-		(org-strip-protective-commas beg-content end-content)
+		(org-unescape-code-in-region beg-content end-content)
 		(delete-region (match-beginning 0) (match-end 0))
 		(save-excursion
 		  (goto-char beg)

+ 39 - 24
lisp/org-src.el

@@ -41,10 +41,8 @@
 (declare-function org-at-table.el-p "org" ())
 (declare-function org-get-indentation "org" (&optional line))
 (declare-function org-switch-to-buffer-other-window "org" (&rest args))
-(declare-function org-strip-protective-commas "org" (beg end))
 (declare-function org-pop-to-buffer-same-window
 		  "org-compat" (&optional buffer-or-name norecord label))
-(declare-function org-strip-protective-commas "org" (beg end))
 (declare-function org-base-buffer "org" (buffer))
 
 (defcustom org-edit-src-region-extra nil
@@ -311,13 +309,8 @@ buffer."
 	     (error "Language mode `%s' fails with: %S" lang-f (nth 1 e)))))
 	(dolist (pair transmitted-variables)
 	  (org-set-local (car pair) (cadr pair)))
-	(if (derived-mode-p 'org-mode)
-	    (progn
-	      (goto-char (point-min))
-	      (while (re-search-forward "^," nil t)
-		(if (eq (org-current-line) line) (setq total-nindent (1+ total-nindent)))
-		(replace-match "")))
-	  (org-strip-protective-commas (point-min) (point-max)))
+	;; Remove protecting commas from visible part of buffer.
+	(org-unescape-code-in-region (point-min) (point-max))
 	(when markline
 	  (org-goto-line (1+ (- markline begline)))
 	  (org-move-to-column
@@ -590,20 +583,38 @@ 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."
+(defun org-escape-code-in-region (beg end)
+  "Escape lines between BEG and END.
+Escaping happens when a line starts with \"*\", \"#+\", \",*\" or
+\",#+\" by appending a comma to it."
   (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))
+  (save-excursion
+    (goto-char beg)
+    (while (re-search-forward "^[ \t]*,?\\(\\*\\|#\\+\\)" end t)
+      (replace-match ",\\1" nil nil nil 1))))
+
+(defun org-escape-code-in-string (s)
+  "Escape lines in string S.
+Escaping happens when a line starts with \"*\", \"#+\", \",*\" or
+\",#+\" by appending a comma to it."
+  (replace-regexp-in-string "^[ \t]*,?\\(\\*\\|#\\+\\)" ",\\1" s nil nil 1))
+
+(defun org-unescape-code-in-region (beg end)
+  "Un-escape lines between BEG and END.
+Un-escaping happens by removing the first comma on lines starting
+with \",*\", \",#+\", \",,*\" and \",,#+\"."
+  (interactive "r")
+  (save-excursion
+    (goto-char beg)
+    (while (re-search-forward "^[ \t]*,?\\(,\\)\\(?:\\*\\|#\\+\\)" end t)
+      (replace-match "" nil nil nil 1))))
+
+(defun org-unescape-code-in-string (s)
+  "Un-escape lines in string S.
+Un-escaping happens by removing the first comma on lines starting
+with \",*\", \",#+\", \",,*\" and \",,#+\"."
+  (replace-regexp-in-string
+   "^[ \t]*,?\\(,\\)\\(?:\\*\\|#\\+\\)" "" s nil nil 1))
 
 (defun org-edit-src-exit (&optional context)
   "Exit special edit and protect problematic lines."
@@ -649,8 +660,12 @@ Return the delta in size of the region."
 	(goto-char (point-min))
 	(if (looking-at "\\s-*") (replace-match " ")))
       (when (org-bound-and-true-p org-edit-src-from-org-mode)
-	(setq delta (+ delta (org-add-protective-commas
-			      (point-min) (point-max) line))))
+	(org-escape-code-in-region (point-min) (point-max))
+	(setq delta (+ delta
+		       (save-excursion
+			 (org-goto-line line)
+			 (if (looking-at "[ \t]*\\(,,\\)?\\(\\*\\|#+\\)") 1
+			   0)))))
       (when (org-bound-and-true-p org-edit-src-picture)
 	(setq preserve-indentation nil)
 	(untabify (point-min) (point-max))

+ 0 - 16
lisp/org.el

@@ -5560,22 +5560,6 @@ by a #."
 	    t)
 	   (t nil))))))
 
-(defun org-strip-protective-commas (beg end)
-  "Strip protective commas between BEG and END in the current buffer."
-  (interactive "r")
-  (save-excursion
-    (save-match-data
-      (goto-char beg)
-      (let ((front-line (save-excursion
-			  (re-search-forward
-			   "[^[:space:]]" end t)
-			  (goto-char (match-beginning 0))
-			  (current-column))))
-	(while (re-search-forward "^[ \t]*\\(,\\)\\([*]\\|#\\)" end t)
-	  (goto-char (match-beginning 1))
-	  (when (= (current-column) front-line)
-	    (replace-match "" nil nil nil 1)))))))
-
 (defun org-activate-angle-links (limit)
   "Run through the buffer and add overlays to links."
   (if (re-search-forward org-angle-link-re limit t)

+ 1 - 1
testing/lisp/test-ob.el

@@ -932,7 +932,7 @@ hello there
    "#+BEGIN_SRC org
 ,* heading
 ,** subheading
-,content
+content
 #+END_SRC"
 
 "* org-babel-remove-result