Split `org-emph-re' and `org-verbatim-re'

* lisp/org.el (org-set-emph-re): Refactor code.  Rip "~" and "="
  markers off `org-emph-re'.
(org-do-emphasis-faces):
(org-sort-remove-invisible): Handle both `org-emph-re' and
`org-verbatim-re'.
(org-in-verbatim-emphasis): Use `org-verbatim-re' instead of
`org-emph-re'.

* lisp/org-element.el (org-element-code-parser):
(org-element-verbatim-parser): Use `org-verbatim-re' instead of
`org-emph-re'.
* testing/lisp/test-org-element.el (test-org-element/bold-parser):

(test-org-element/code-parser):
(test-org-element/italic-parser):
(test-org-element/strike-through-parser):
(test-org-element/underline-parser):
(test-org-element/verbatim-parser): Update tests, which no longer need
to bind `org-emph-re'.
This commit is contained in:
Nicolas Goaziou 2016-12-08 09:44:26 +01:00
parent 05223fc6fa
commit 9fb2e047d2
3 changed files with 74 additions and 130 deletions

View File

@ -2754,7 +2754,7 @@ keywords. Otherwise, return nil.
Assume point is at the first tilde marker." Assume point is at the first tilde marker."
(save-excursion (save-excursion
(unless (bolp) (backward-char 1)) (unless (bolp) (backward-char 1))
(when (looking-at org-emph-re) (when (looking-at org-verbatim-re)
(let ((begin (match-beginning 2)) (let ((begin (match-beginning 2))
(value (match-string-no-properties 4)) (value (match-string-no-properties 4))
(post-blank (progn (goto-char (match-end 2)) (post-blank (progn (goto-char (match-end 2))
@ -3765,7 +3765,7 @@ and cdr is a plist with `:value', `:begin', `:end' and
Assume point is at the first equal sign marker." Assume point is at the first equal sign marker."
(save-excursion (save-excursion
(unless (bolp) (backward-char 1)) (unless (bolp) (backward-char 1))
(when (looking-at org-emph-re) (when (looking-at org-verbatim-re)
(let ((begin (match-beginning 2)) (let ((begin (match-beginning 2))
(value (match-string-no-properties 4)) (value (match-string-no-properties 4))
(post-blank (progn (goto-char (match-end 2)) (post-blank (progn (goto-char (match-end 2))

View File

@ -4462,8 +4462,10 @@ After a match, the match groups contain these elements:
3 The leading marker like * or /, indicating the type of highlighting 3 The leading marker like * or /, indicating the type of highlighting
4 The text between the emphasis markers, not including the markers 4 The text between the emphasis markers, not including the markers
5 The character after the match, empty at the end of a line") 5 The character after the match, empty at the end of a line")
(defvar org-verbatim-re nil (defvar org-verbatim-re nil
"Regular expression for matching verbatim text.") "Regular expression for matching verbatim text.")
(defvar org-emphasis-regexp-components) ; defined just below (defvar org-emphasis-regexp-components) ; defined just below
(defvar org-emphasis-alist) ; defined just below (defvar org-emphasis-alist) ; defined just below
(defun org-set-emph-re (var val) (defun org-set-emph-re (var val)
@ -4472,54 +4474,17 @@ After a match, the match groups contain these elements:
(when (and (boundp 'org-emphasis-alist) (when (and (boundp 'org-emphasis-alist)
(boundp 'org-emphasis-regexp-components) (boundp 'org-emphasis-regexp-components)
org-emphasis-alist org-emphasis-regexp-components) org-emphasis-alist org-emphasis-regexp-components)
(let* ((e org-emphasis-regexp-components) (pcase-let*
(pre (car e)) ((`(,pre ,post ,border ,body ,nl) org-emphasis-regexp-components)
(post (nth 1 e)) (body (if (<= nl 0) body
(border (nth 2 e)) (format "%s*?\\(?:\n%s*?\\)\\{0,%d\\}" body body nl)))
(body (nth 3 e)) (template
(nl (nth 4 e)) (format (concat "\\([%s]\\|^\\)" ;before markers
(body1 (concat body "*?")) "\\(\\([%%s]\\)\\([^%s]\\|[^%s]%s[^%s]\\)\\3\\)"
(markers (mapconcat 'car org-emphasis-alist "")) "\\([%s]\\|$\\)") ;after markers
(vmarkers (mapconcat pre border border body border post)))
(lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) "")) (setq org-emph-re (format template "*/_+"))
org-emphasis-alist ""))) (setq org-verbatim-re (format template "=~")))))
;; make sure special characters appear at the right position in the class
(if (string-match "\\^" markers)
(setq markers (concat (replace-match "" t t markers) "^")))
(if (string-match "-" markers)
(setq markers (concat (replace-match "" t t markers) "-")))
(if (string-match "\\^" vmarkers)
(setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
(if (string-match "-" vmarkers)
(setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
(if (> nl 0)
(setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
(int-to-string nl) "\\}")))
;; Make the regexp
(setq org-emph-re
(concat "\\([" pre "]\\|^\\)"
"\\("
"\\([" markers "]\\)"
"\\("
"[^" border "]\\|"
"[^" border "]"
body1
"[^" border "]"
"\\)"
"\\3\\)"
"\\([" post "]\\|$\\)"))
(setq org-verbatim-re
(concat "\\([" pre "]\\|^\\)"
"\\("
"\\([" vmarkers "]\\)"
"\\("
"[^" border "]\\|"
"[^" border "]"
body1
"[^" border "]"
"\\)"
"\\3\\)"
"\\([" post "]\\|$\\)")))))
;; This used to be a defcustom (Org <8.0) but allowing the users to ;; This used to be a defcustom (Org <8.0) but allowing the users to
;; set this option proved cumbersome. See this message/thread: ;; set this option proved cumbersome. See this message/thread:
@ -5876,32 +5841,29 @@ This should be called after the variable `org-link-parameters' has changed."
(defun org-do-emphasis-faces (limit) (defun org-do-emphasis-faces (limit)
"Run through the buffer and emphasize strings." "Run through the buffer and emphasize strings."
(let (rtn a) (let ((quick-re (format "\\([%s]\\|^\\)\\([~=*/_+]\\)"
(while (and (not rtn) (re-search-forward org-emph-re limit t)) (car org-emphasis-regexp-components))))
(let* ((border (char-after (match-beginning 3))) (catch :exit
(bre (regexp-quote (char-to-string border)))) (while (re-search-forward quick-re limit t)
(when (and (not (= border (char-after (match-beginning 4)))) (let* ((marker (match-string 2))
(not (string-match-p (concat bre ".*" bre) (verbatim? (member marker '("~" "="))))
(replace-regexp-in-string (when (save-excursion
"\n" " " (goto-char (match-beginning 0))
(substring (match-string 2) 1 -1))))) (looking-at (if verbatim? org-verbatim-re org-emph-re)))
(setq rtn t) (pcase-let ((`(,_ ,face ,_) (assoc marker org-emphasis-alist)))
(setq a (assoc (match-string 3) org-emphasis-alist)) (font-lock-prepend-text-property
(font-lock-prepend-text-property (match-beginning 2) (match-end 2) (match-beginning 2) (match-end 2) 'face face)
'face (when verbatim?
(nth 1 a)) (org-remove-flyspell-overlays-in
(and (nth 2 a) (match-beginning 0) (match-end 0)))
(org-remove-flyspell-overlays-in (add-text-properties (match-beginning 2) (match-end 2)
(match-beginning 0) (match-end 0))) '(font-lock-multiline t org-emphasis t))
(add-text-properties (match-beginning 2) (match-end 2) (when org-hide-emphasis-markers
'(font-lock-multiline t org-emphasis t)) (add-text-properties (match-end 4) (match-beginning 5)
(when org-hide-emphasis-markers '(invisible org-link))
(add-text-properties (match-end 4) (match-beginning 5) (add-text-properties (match-beginning 3) (match-end 3)
'(invisible org-link)) '(invisible org-link)))
(add-text-properties (match-beginning 3) (match-end 3) (throw :exit t))))))))
'(invisible org-link)))))
(goto-char (1+ (match-beginning 0))))
rtn))
(defun org-emphasize (&optional char) (defun org-emphasize (&optional char)
"Insert or change an emphasis, i.e. a font like bold or italic. "Insert or change an emphasis, i.e. a font like bold or italic.
@ -9006,18 +8968,14 @@ Optional argument WITH-CASE means sort case-sensitively."
(org-call-with-arg 'org-sort-entries with-case)))) (org-call-with-arg 'org-sort-entries with-case))))
(defun org-sort-remove-invisible (s) (defun org-sort-remove-invisible (s)
"Remove invisible links from string S." "Remove invisible part of links and emphasis markers from string S."
(remove-text-properties 0 (length s) org-rm-props s) (remove-text-properties 0 (length s) org-rm-props s)
(while (string-match org-bracket-link-regexp s) (replace-regexp-in-string
(setq s (replace-match (if (match-end 2) org-verbatim-re (lambda (m) (format "%s " (match-string 4 m)))
(match-string 3 s) (replace-regexp-in-string
(match-string 1 s)) org-emph-re (lambda (m) (format " %s " (match-string 4 m)))
t t s))) (org-link-display-format s)
(let ((st (format " %s " s))) t t) t t))
(while (string-match org-emph-re st)
(setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
(setq s (substring st 1 -1)))
s)
(defvar org-priority-regexp) ; defined later in the file (defvar org-priority-regexp) ; defined later in the file
@ -22004,10 +21962,9 @@ With prefix arg UNCOMPILED, load the uncompiled versions."
(defun org-in-verbatim-emphasis () (defun org-in-verbatim-emphasis ()
(save-match-data (save-match-data
(and (org-in-regexp org-emph-re 2) (and (org-in-regexp org-verbatim-re 2)
(>= (point) (match-beginning 3)) (>= (point) (match-beginning 3))
(<= (point) (match-end 4)) (<= (point) (match-end 4)))))
(member (match-string 3) '("=" "~")))))
(defun org-overlay-display (ovl text &optional face evap) (defun org-overlay-display (ovl text &optional face evap)
"Make overlay OVL display TEXT with face FACE." "Make overlay OVL display TEXT with face FACE."

View File

@ -456,16 +456,14 @@ Some other text
"Test `bold' parser." "Test `bold' parser."
;; Standard test. ;; Standard test.
(should (should
(let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)")) (org-test-with-temp-text "*bold*"
(org-test-with-temp-text "*bold*" (org-element-map (org-element-parse-buffer) 'bold #'identity nil t)))
(org-element-map (org-element-parse-buffer) 'bold 'identity nil t))))
;; Multi-line markup. ;; Multi-line markup.
(should (should
(equal (equal
(org-element-contents (org-element-contents
(let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)")) (org-test-with-temp-text "*first line\nsecond line*"
(org-test-with-temp-text "*first line\nsecond line*" (org-element-map (org-element-parse-buffer) 'bold #'identity nil t)))
(org-element-map (org-element-parse-buffer) 'bold 'identity nil t))))
'("first line\nsecond line")))) '("first line\nsecond line"))))
@ -523,18 +521,16 @@ Some other text
"Test `code' parser." "Test `code' parser."
;; Regular test. ;; Regular test.
(should (should
(let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)")) (org-test-with-temp-text "~code~"
(org-test-with-temp-text "~code~" (org-element-map (org-element-parse-buffer) 'code #'identity)))
(org-element-map (org-element-parse-buffer) 'code 'identity))))
;; Multi-line markup. ;; Multi-line markup.
(should (should
(equal (equal
(org-element-property (org-element-property
:value :value
(let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)")) (org-test-with-temp-text "~first line\nsecond line~"
(org-test-with-temp-text "~first line\nsecond line~" (org-element-map
(org-element-map (org-element-parse-buffer) 'code #'identity nil t)))
(org-element-parse-buffer) 'code 'identity nil t))))
"first line\nsecond line"))) "first line\nsecond line")))
@ -1369,16 +1365,14 @@ DEADLINE: <2012-03-29 thu.>"
"Test `italic' parser." "Test `italic' parser."
;; Regular test. ;; Regular test.
(should (should
(let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)")) (org-test-with-temp-text "/italic/"
(org-test-with-temp-text "/italic/" (org-element-map (org-element-parse-buffer) 'italic #'identity nil t)))
(org-element-map (org-element-parse-buffer) 'italic 'identity nil t))))
;; Multi-line markup. ;; Multi-line markup.
(should (should
(equal (equal
(org-element-contents (org-element-contents
(let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)")) (org-test-with-temp-text "/first line\nsecond line/"
(org-test-with-temp-text "/first line\nsecond line/" (org-element-map (org-element-parse-buffer) 'italic #'identity nil t)))
(org-element-map (org-element-parse-buffer) 'italic 'identity nil t))))
'("first line\nsecond line")))) '("first line\nsecond line"))))
@ -2184,17 +2178,15 @@ Outside list"
"Test `strike-through' parser." "Test `strike-through' parser."
;; Regular test. ;; Regular test.
(should (should
(let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)")) (org-test-with-temp-text "+strike-through+"
(org-test-with-temp-text "+strike-through+" (org-element-map (org-element-parse-buffer) 'strike-through #'identity)))
(org-element-map (org-element-parse-buffer) 'strike-through 'identity))))
;; Multi-line markup. ;; Multi-line markup.
(should (should
(equal (equal
(org-element-contents (org-element-contents
(let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)")) (org-test-with-temp-text "+first line\nsecond line+"
(org-test-with-temp-text "+first line\nsecond line+" (org-element-map
(org-element-map (org-element-parse-buffer) 'strike-through #'identity nil t)))
(org-element-parse-buffer) 'strike-through 'identity nil t))))
'("first line\nsecond line")))) '("first line\nsecond line"))))
@ -2375,17 +2367,15 @@ Outside list"
"Test `underline' parser." "Test `underline' parser."
;; Regular test. ;; Regular test.
(should (should
(let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)")) (org-test-with-temp-text "_underline_"
(org-test-with-temp-text "_underline_" (org-element-map (org-element-parse-buffer) 'underline #'identity)))
(org-element-map (org-element-parse-buffer) 'underline 'identity))))
;; Multi-line markup. ;; Multi-line markup.
(should (should
(equal (equal
(org-element-contents (org-element-contents
(let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)")) (org-test-with-temp-text "_first line\nsecond line_"
(org-test-with-temp-text "_first line\nsecond line_" (org-element-map
(org-element-map (org-element-parse-buffer) 'underline #'identity nil t)))
(org-element-parse-buffer) 'underline 'identity nil t))))
'("first line\nsecond line")))) '("first line\nsecond line"))))
@ -2395,18 +2385,15 @@ Outside list"
"Test `verbatim' parser." "Test `verbatim' parser."
;; Regular test. ;; Regular test.
(should (should
(let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)")) (org-test-with-temp-text "=verbatim="
(org-test-with-temp-text "=verbatim=" (org-element-map (org-element-parse-buffer) 'verbatim #'identity)))
(org-element-map (org-element-parse-buffer) 'verbatim 'identity))))
;; Multi-line markup. ;; Multi-line markup.
(should (should
(equal (equal
(org-element-property (org-element-property
:value :value
(let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)")) (org-test-with-temp-text "=first line\nsecond line="
(org-test-with-temp-text "=first line\nsecond line=" (org-element-map (org-element-parse-buffer) 'verbatim #'identity nil t)))
(org-element-map
(org-element-parse-buffer) 'verbatim 'identity nil t))))
"first line\nsecond line"))) "first line\nsecond line")))