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.
This commit is contained in:
Nicolas Goaziou 2016-08-17 23:48:53 +02:00
parent fea6dd167a
commit 3771f35f80
2 changed files with 30 additions and 1 deletions

View File

@ -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)))

View File

@ -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