From 3771f35f8087328be37b233fd627a04c912ef726 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Wed, 17 Aug 2016 23:48:53 +0200 Subject: [PATCH] org-src: Fix coderef regexp * lisp/org-src.el (org-src-coderef-regexp): A coderef label cannot be consist of white spaces only. * testing/lisp/test-org-src.el (test-org-src/coderef-regexp): Add test. --- lisp/org-src.el | 2 +- testing/lisp/test-org-src.el | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lisp/org-src.el b/lisp/org-src.el index 172c33b01..fd60bde22 100644 --- a/lisp/org-src.el +++ b/lisp/org-src.el @@ -744,7 +744,7 @@ A coderef format regexp can only match at the end of a line." (format "\\S-\\([ \t]*\\(%s\\)[ \t]*\\)$" (replace-regexp-in-string "%s" - (if label (regexp-quote label) "\\([-a-zA-Z0-9_ ]+\\)") + (if label (regexp-quote label) "\\([-a-zA-Z0-9_][-a-zA-Z0-9_ ]*\\)") (regexp-quote fmt) nil t))) diff --git a/testing/lisp/test-org-src.el b/testing/lisp/test-org-src.el index 656a5d73e..1386f2237 100644 --- a/testing/lisp/test-org-src.el +++ b/testing/lisp/test-org-src.el @@ -237,5 +237,34 @@ This is a tab:\t. (setq-local org-coderef-label-format "foo") (org-src-coderef-format element)))))) +(ert-deftest test-org-src/coderef-regexp () + "Test `org-src-coderef-regexp' specifications." + ;; Regular test. + (should + (string-match-p (org-src-coderef-regexp "; ref:%s") + "#+BEGIN_SRC emacs-lisp\n0; ref:label\n#+END_SRC")) + ;; Ignore white space around the coderef. + (should + (string-match-p (org-src-coderef-regexp "; ref:%s") + "#+BEGIN_SRC emacs-lisp\n0 ; ref:label\n#+END_SRC")) + (should + (string-match-p (org-src-coderef-regexp "; ref:%s") + "#+BEGIN_SRC emacs-lisp\n0 ; ref:label \n#+END_SRC")) + ;; Only match regexp at the end of the line. + (should-not + (string-match-p (org-src-coderef-regexp "; ref:%s") + "#+BEGIN_SRC emacs-lisp\n0; ref:label (+ 1 2)\n#+END_SRC")) + ;; Do not match an empty label. + (should-not + (string-match-p (org-src-coderef-regexp "; ref:%s") + "#+BEGIN_SRC emacs-lisp\n0; ref:\n#+END_SRC")) + ;; When optional argument LABEL is provided, match given label only. + (should + (string-match-p (org-src-coderef-regexp "; ref:%s" "label") + "#+BEGIN_SRC emacs-lisp\n0; ref:label\n#+END_SRC")) + (should-not + (string-match-p (org-src-coderef-regexp "; ref:%s" "label2") + "#+BEGIN_SRC emacs-lisp\n0; ref:label\n#+END_SRC"))) + (provide 'test-org-src) ;;; test-org-src.el ends here