From 3aa4d44ba8e5a4d02fbc2ebcf3dc73b796a8780a Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Tue, 24 Oct 2017 09:51:43 +0200 Subject: [PATCH] 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 --- lisp/org-src.el | 9 +++++---- 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 99d7c6f7f..4191d9aad 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 1d683ec9c..86f08eccb 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