Fix fontification of LaTeX environments

* lisp/org.el (org-compute-latex-and-related-regexp):
(org-do-latex-and-related): Fix fontification of LaTeX environments.
This commit is contained in:
Nicolas Goaziou 2018-12-13 21:28:14 +01:00
parent a60a0edc6f
commit a86b14253b
1 changed files with 58 additions and 32 deletions

View File

@ -6226,24 +6226,31 @@ Also refresh fontification if needed."
(defun org-compute-latex-and-related-regexp () (defun org-compute-latex-and-related-regexp ()
"Compute regular expression for LaTeX, entities and sub/superscript. "Compute regular expression for LaTeX, entities and sub/superscript.
Result depends on variable `org-highlight-latex-and-related'." Result depends on variable `org-highlight-latex-and-related'."
(setq-local (let ((re-sub
org-latex-and-related-regexp (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
(let* ((re-sub ((eq org-use-sub-superscripts '{})
(cond ((not (memq 'script org-highlight-latex-and-related)) nil) (list org-match-substring-with-braces-regexp))
((eq org-use-sub-superscripts '{}) (org-use-sub-superscripts (list org-match-substring-regexp))))
(list org-match-substring-with-braces-regexp)) (re-latex
(org-use-sub-superscripts (list org-match-substring-regexp)))) (when (memq 'latex org-highlight-latex-and-related)
(re-latex (let* ((matchers (plist-get org-format-latex-options :matchers))
(when (memq 'latex org-highlight-latex-and-related) (regexps (and (member "begin" matchers)
(let ((matchers (plist-get org-format-latex-options :matchers))) '("\\\\end{[a-zA-Z0-9\\*]+}[ \t]*$"))))
(delq nil (dolist (matcher matchers)
(mapcar (lambda (x) (pcase (assoc matcher org-latex-regexps)
(and (member (car x) matchers) (nth 1 x))) (`("begin" . ,_) (push "^[ \t]*\\\\begin{[a-zA-Z0-9\\*]+}"
org-latex-regexps))))) regexps))
(re-entities (`(,_ ,regexp . ,_) (push regexp regexps))
(when (memq 'entities org-highlight-latex-and-related) (_ nil)))
(list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")))) (nreverse regexps))))
(mapconcat 'identity (append re-latex re-entities re-sub) "\\|")))) (re-entities
(when (memq 'entities org-highlight-latex-and-related)
(list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\
\\($\\|{}\\|[^[:alpha:]]\\)"))))
(setq-local org-latex-and-related-regexp
(mapconcat #'identity
(append re-latex re-entities re-sub)
"\\|"))))
(defun org-do-latex-and-related (limit) (defun org-do-latex-and-related (limit)
"Highlight LaTeX snippets and environments, entities and sub/superscript. "Highlight LaTeX snippets and environments, entities and sub/superscript.
@ -6253,22 +6260,41 @@ done, nil otherwise."
(when (org-string-nw-p org-latex-and-related-regexp) (when (org-string-nw-p org-latex-and-related-regexp)
(catch 'found (catch 'found
(while (re-search-forward org-latex-and-related-regexp limit t) (while (re-search-forward org-latex-and-related-regexp limit t)
(unless (unless (cl-some
(cl-some (lambda (f) (memq f '(org-code org-verbatim underline
(lambda (f) org-special-keyword)))
(memq f '(org-code org-verbatim underline org-special-keyword))) (save-excursion
(save-excursion (goto-char (1+ (match-beginning 0)))
(goto-char (1+ (match-beginning 0))) (face-at-point nil t)))
(face-at-point nil t))) (let* ((start (if (memq (char-after (1+ (match-beginning 0)))
(let ((offset (if (memq (char-after (1+ (match-beginning 0)))
'(?_ ?^)) '(?_ ?^))
1 (1+ (match-beginning 0))
0))) (match-beginning 0)))
(end
(let* ((b (match-beginning 0))
(e (match-end 0))
(m (buffer-substring-no-properties b e)))
(cond
((string-match "\\`[ \t]*\\\\begin{\\([a-zA-Z0-9\\*]+\\)}"
m)
(let ((closing
(format "\\\\end{%s}[ \t]*$"
(regexp-quote (match-string 1 m)))))
(or (re-search-forward closing nil t) e)))
((string-match "\\\\end{\\([a-zA-Z0-9\\*]+\\)}[ \t]*\\'" m)
(let ((opening
(format "^[ \t]*\\\\begin{%s}"
(regexp-quote (match-string 1 m)))))
(setq start (or (save-excursion
(re-search-backward opening nil t))
b))
(line-end-position)))
((string-match "\\\\[a-zA-Z]+\\*?{" m)
(search-forward "}" nil t))
(t e)))))
(font-lock-prepend-text-property (font-lock-prepend-text-property
(+ offset (match-beginning 0)) (match-end 0) start end 'face 'org-latex-and-related)
'face 'org-latex-and-related) (add-text-properties start end '(font-lock-multiline t)))
(add-text-properties (+ offset (match-beginning 0)) (match-end 0)
'(font-lock-multiline t)))
(throw 'found t))) (throw 'found t)))
nil))) nil)))