Просмотр исходного кода

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.
Nicolas Goaziou 8 лет назад
Родитель
Сommit
3771f35f80
2 измененных файлов с 30 добавлено и 1 удалено
  1. 1 1
      lisp/org-src.el
  2. 29 0
      testing/lisp/test-org-src.el

+ 1 - 1
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)))
 

+ 29 - 0
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