From 4235718d7901a243086288bdae32475b558ec1ce Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Sun, 6 Jul 2014 23:15:44 +0200 Subject: [PATCH] org-element: Small optimizations * lisp/org-element.el (org-element-latex-fragment-parser): Avoid matching twice regexps in some cases. (org-element--object-lex): Avoid making a funcall if a line break isn't possible. This patch also removes the limit on the number of lines a latex fragment with a single dollar can span over. --- lisp/org-element.el | 51 +++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/lisp/org-element.el b/lisp/org-element.el index ff45c7b4b..f5208d4fe 100644 --- a/lisp/org-element.el +++ b/lisp/org-element.el @@ -2911,27 +2911,26 @@ Assume point is at the beginning of the LaTeX fragment." (catch 'no-object (save-excursion (let* ((begin (point)) - (substring-match - (or (catch 'exit - (dolist (e (cdr org-latex-regexps)) - (let ((latex-regexp (nth 1 e))) - (when (or (looking-at latex-regexp) - (and (not (bobp)) - (save-excursion - (backward-char) - (looking-at latex-regexp)))) - (throw 'exit (nth 2 e)))))) - ;; Macro. - (and (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*") - 0) - ;; No fragment found. - (throw 'no-object nil))) - (value (org-match-string-no-properties substring-match)) - (post-blank (progn (goto-char (match-end substring-match)) - (skip-chars-forward " \t"))) + (after-fragment + (if (eq (char-after) ?$) + (if (eq (char-after (1+ (point))) ?$) + (search-forward "$$" nil t 2) + (and (not (eq (char-before) ?$)) + (looking-at "\\$[^ \t\n,;.$]\\(?:[^$]*?[^ \t\n,.$]\\)?\\$\\([- \t.,?;:'\")]\\|$\\)") + (match-beginning 1))) + (case (char-after (1+ (point))) + (?\( (search-forward "\\)" nil t)) + (?\[ (search-forward "\\]" nil t)) + (otherwise + ;; Macro. + (and (looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*") + (match-end 0)))))) + (post-blank (if (not after-fragment) (throw 'no-object nil) + (goto-char after-fragment) + (skip-chars-forward " \t"))) (end (point))) (list 'latex-fragment - (list :value value + (list :value (buffer-substring-no-properties begin after-fragment) :begin begin :end end :post-blank post-blank)))))) @@ -4277,12 +4276,14 @@ to an appropriate container (e.g., a paragraph)." (org-element-timestamp-parser)) (and (memq 'link restriction) (org-element-link-parser))))) - (?\\ (or (and (memq 'line-break restriction) - (org-element-line-break-parser)) - (and (memq 'entity restriction) - (org-element-entity-parser)) - (and (memq 'latex-fragment restriction) - (org-element-latex-fragment-parser)))) + (?\\ + (if (eq (aref result 1) ?\\) + (and (memq 'line-break restriction) + (org-element-line-break-parser)) + (or (and (memq 'entity restriction) + (org-element-entity-parser)) + (and (memq 'latex-fragment restriction) + (org-element-latex-fragment-parser))))) (?\[ (if (eq (aref result 1) ?\[) (and (memq 'link restriction)