org-element.el: Allow one-line LaTeX environments

* org-element.el (org-element--latex-begin-environment,
org-element--latex-end-environment): New format strings to identify
beginning and ending of LaTeX environments.
(org-element-latex-environment-parser, org-element-paragraph-parser,
org-element--current-element): Use `org-element--latex-begin-environment'
and `org-element--latex-end-environment'.

* test-org-element.el (test-org-element/latex-environment-parser):
Add tests.
This commit is contained in:
Rasmus 2014-07-22 15:09:03 +02:00 committed by Nicolas Goaziou
parent 29f3662035
commit 0be96db56e
2 changed files with 38 additions and 7 deletions

View File

@ -2084,6 +2084,18 @@ CONTENTS is nil."
;;;; Latex Environment
(defconst org-element--latex-begin-environment
"^[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}"
"Regexp matching the beginning of a LaTeX environment.
The environment is captured by the first group.
See also `org-element--latex-end-environment'.")
(defconst org-element--latex-end-environment
"\\\\end{%s}[ \t]*$"
"Format string matching the ending of a LaTeX environment.
See also `org-element--latex-begin-environment'.")
(defun org-element-latex-environment-parser (limit affiliated)
"Parse a LaTeX environment.
@ -2100,8 +2112,8 @@ Assume point is at the beginning of the latex environment."
(save-excursion
(let ((case-fold-search t)
(code-begin (point)))
(looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
(if (not (re-search-forward (format "^[ \t]*\\\\end{%s}[ \t]*$"
(looking-at org-element--latex-begin-environment)
(if (not (re-search-forward (format org-element--latex-end-environment
(regexp-quote (match-string 1)))
limit t))
;; Incomplete latex environment: parse it as a paragraph.
@ -2219,11 +2231,10 @@ Assume point is at the beginning of the paragraph."
(org-match-string-no-properties 1)))
limit t)))
;; Stop at valid latex environments.
(and (looking-at
"[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}")
(and (looking-at org-element--latex-begin-environment)
(save-excursion
(re-search-forward
(format "^[ \t]*\\\\end{%s}[ \t]*$"
(format org-element--latex-end-environment
(regexp-quote
(org-match-string-no-properties 1)))
limit t)))
@ -3707,7 +3718,7 @@ element it has to parse."
(goto-char (car affiliated))
(org-element-keyword-parser limit nil))
;; LaTeX Environment.
((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}\\(\\[.*?\\]\\|{.*?}\\)*[ \t]*$")
((looking-at org-element--latex-begin-environment)
(org-element-latex-environment-parser limit affiliated))
;; Drawer and Property Drawer.
((looking-at org-drawer-regexp)

View File

@ -1310,9 +1310,29 @@ e^{i\\pi}+1=0
(eq 'latex-environment
(org-test-with-temp-text "\\begin{env}{arg}\nvalue\n\\end{env}"
(org-element-type (org-element-at-point)))))
;; Allow environments without newline after \begin{.}.
(should
(eq 'latex-environment
(org-test-with-temp-text "\\begin{env}{arg}something\nvalue\n\\end{env}"
(org-element-type (org-element-at-point)))))
;; Allow one-line environments.
(should
(eq 'latex-environment
(org-test-with-temp-text "\\begin{env}{arg}something\\end{env}"
(org-element-type (org-element-at-point)))))
;; Should not allow different tags.
(should-not
(eq 'latex-environment
(org-test-with-temp-text "\\begin{env}{arg} something\nvalue\n\\end{env}"
(org-test-with-temp-text "\\begin{env*}{arg}something\\end{env}"
(org-element-type (org-element-at-point)))))
;; LaTeX environments must be on separate lines.
(should-not
(eq 'latex-environment
(org-test-with-temp-text "\\begin{env} x \\end{env} y"
(org-element-type (org-element-at-point)))))
(should-not
(eq 'latex-environment
(org-test-with-temp-text "y \\begin{env} x<point> \\end{env}"
(org-element-type (org-element-at-point)))))
;; Handle non-empty blank line at the end of buffer.
(should