diff options
author | Nicolas Goaziou <mail@nicolasgoaziou.fr> | 2017-10-24 09:51:43 +0200 |
---|---|---|
committer | Nicolas Goaziou <mail@nicolasgoaziou.fr> | 2017-10-24 09:51:43 +0200 |
commit | 3aa4d44ba8e5a4d02fbc2ebcf3dc73b796a8780a (patch) | |
tree | 0c68b9d12195b6788dd279844858e46d97f09c2a | |
parent | a2a034f5b9088175c9fbcf034bc1094982ae739c (diff) | |
download | org-mode-3aa4d44ba8e5a4d02fbc2ebcf3dc73b796a8780a.tar.gz |
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>
-rw-r--r-- | lisp/org-src.el | 9 | ||||
-rw-r--r-- | testing/lisp/test-org-src.el | 38 |
2 files changed, 43 insertions, 4 deletions
diff --git a/lisp/org-src.el b/lisp/org-src.el index 99d7c6f..4191d9a 100644 --- a/lisp/org-src.el +++ b/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)) diff --git a/testing/lisp/test-org-src.el b/testing/lisp/test-org-src.el index 1d683ec..86f08ec 100644 --- a/testing/lisp/test-org-src.el +++ b/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 |