Browse Source

Fix comma escaping with multiple leading commas

* lisp/org-src.el (org-escape-code-in-region):
(org-escape-code-in-string):
(org-unescape-code-in-region):
(org-unescape-code-in-string): Fix comma escaping with multiple
leading commas.

* testing/lisp/test-org-src.el (test-org-src/escape-code-in-string):
(test-org-src/unescape-code-in-string): New tests.

Reported-by: Michal Politowski <mpol@meep.pl>
<http://lists.gnu.org/archive/html/emacs-orgmode/2017-10/msg00359.html>
Nicolas Goaziou 1 year ago
parent
commit
3aa4d44ba8
2 changed files with 43 additions and 4 deletions
  1. 5 4
      lisp/org-src.el
  2. 38 0
      testing/lisp/test-org-src.el

+ 5 - 4
lisp/org-src.el

@@ -581,14 +581,15 @@ Escaping happens when a line starts with \"*\", \"#+\", \",*\" or
   (interactive "r")
   (save-excursion
     (goto-char end)
-    (while (re-search-backward "^[ \t]*,?\\(\\*\\|#\\+\\)" beg t)
+    (while (re-search-backward "^[ \t]*\\(,*\\(?:\\*\\|#\\+\\)\\)" beg t)
       (save-excursion (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))
+  (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.
@@ -597,7 +598,7 @@ with \",*\", \",#+\", \",,*\" and \",,#+\"."
   (interactive "r")
   (save-excursion
     (goto-char end)
-    (while (re-search-backward "^[ \t]*,?\\(,\\)\\(?:\\*\\|#\\+\\)" beg t)
+    (while (re-search-backward "^[ \t]*,*\\(,\\)\\(?:\\*\\|#\\+\\)" beg t)
       (save-excursion (replace-match "" nil nil nil 1)))))
 
 (defun org-unescape-code-in-string (s)
@@ -605,7 +606,7 @@ with \",*\", \",#+\", \",,*\" and \",,#+\"."
 Un-escaping happens by removing the first comma on lines starting
 with \",*\", \",#+\", \",,*\" and \",,#+\"."
   (replace-regexp-in-string
-   "^[ \t]*,?\\(,\\)\\(?:\\*\\|#\\+\\)" "" s nil nil 1))
+   "^[ \t]*,*\\(,\\)\\(?:\\*\\|#\\+\\)" "" s nil nil 1))
 
 
 

+ 38 - 0
testing/lisp/test-org-src.el

@@ -443,5 +443,43 @@ This is a tab:\t.
 	 (org-edit-special)
 	 (prog1 foo (org-edit-src-exit))))))
 
+;;; Code escaping
+
+(ert-deftest test-org-src/escape-code-in-string ()
+  "Test `org-escape-code-in-string' specifications."
+  ;; Escape lines starting with "*" or "#+".
+  (should (equal ",*" (org-escape-code-in-string "*")))
+  (should (equal ",#+" (org-escape-code-in-string "#+")))
+  ;; Escape lines starting with ",*" and ",#+".  Number of leading
+  ;; commas does not matter.
+  (should (equal ",,*" (org-escape-code-in-string ",*")))
+  (should (equal ",,#+" (org-escape-code-in-string ",#+")))
+  (should (equal ",,,*" (org-escape-code-in-string ",,*")))
+  (should (equal ",,,#+" (org-escape-code-in-string ",,#+")))
+  ;; Indentation does not matter.
+  (should (equal " ,*" (org-escape-code-in-string " *")))
+  (should (equal " ,#+" (org-escape-code-in-string " #+")))
+  ;; Do nothing on other cases.
+  (should (equal "a" (org-escape-code-in-string "a")))
+  (should (equal "#" (org-escape-code-in-string "#")))
+  (should (equal "," (org-escape-code-in-string ","))))
+
+(ert-deftest test-org-src/unescape-code-in-string ()
+  "Test `org-unescape-code-in-string' specifications."
+  ;; Unescape lines starting with ",*" or ",#+".  Number of leading
+  ;; commas does not matter.
+  (should (equal "*" (org-unescape-code-in-string ",*")))
+  (should (equal "#+" (org-unescape-code-in-string ",#+")))
+  (should (equal ",*" (org-unescape-code-in-string ",,*")))
+  (should (equal ",#+" (org-unescape-code-in-string ",,#+")))
+  ;; Indentation does not matter.
+  (should (equal " *" (org-unescape-code-in-string " ,*")))
+  (should (equal " #+" (org-unescape-code-in-string " ,#+")))
+  ;; Do nothing on other cases.
+  (should (equal "a" (org-unescape-code-in-string "a")))
+  (should (equal "#" (org-unescape-code-in-string "#")))
+  (should (equal "," (org-unescape-code-in-string ","))))
+
+
 (provide 'test-org-src)
 ;;; test-org-src.el ends here