浏览代码

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.
Nicolas Goaziou 10 年之前
父节点
当前提交
4235718d79
共有 1 个文件被更改,包括 26 次插入25 次删除
  1. 26 25
      lisp/org-element.el

+ 26 - 25
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)