From mboxrd@z Thu Jan 1 00:00:00 1970 From: Rasmus Subject: Re: [bug?, org-element] latex-environment delimiters must be at BOL Date: Tue, 22 Jul 2014 15:28:33 +0200 Message-ID: <874my9ldz2.fsf@gmx.us> References: <877g3ltomt.fsf@gmx.us> <87iomxjvyv.fsf@nicolasgoaziou.fr> <87vbqu1xb6.fsf@gmx.us> <87mwc3gbtj.fsf@nicolasgoaziou.fr> <87egxf12n7.fsf@gmx.us> <87ha29g76m.fsf@nicolasgoaziou.fr> <87fvhtlq0p.fsf@gmx.us> <87d2cxg16m.fsf@nicolasgoaziou.fr> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:54540) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X9a7Y-00011h-OC for emacs-orgmode@gnu.org; Tue, 22 Jul 2014 09:28:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1X9a7T-0003f0-8x for emacs-orgmode@gnu.org; Tue, 22 Jul 2014 09:28:44 -0400 Received: from mout.gmx.net ([212.227.17.21]:60538) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1X9a7S-0003eh-UA for emacs-orgmode@gnu.org; Tue, 22 Jul 2014 09:28:39 -0400 Received: from W530 ([213.23.238.209]) by mail.gmx.com (mrgmx103) with ESMTPSA (Nemesis) id 0MA9FV-1XGQgd3y9Z-00BLu9 for ; Tue, 22 Jul 2014 15:28:35 +0200 In-Reply-To: <87d2cxg16m.fsf@nicolasgoaziou.fr> (Nicolas Goaziou's message of "Tue, 22 Jul 2014 12:03:45 +0200") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain Hi, Nicolas Goaziou writes: >> +(defconst org-element--latex-begin-environment >> + "^[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}" > > I overlooked this in the previous patch. This regexp is not correct, as > it matches, e.g. > > \begin{ab*cd} > > A more accurate regexp is > > "^[ \t]*\\\\begin{\\([A-Za-z0-9]+\\*?\\)}" Note that the maint/master version of org-element--current-element does not including a trailing "^". I > I realize that `org-element--current-element' is wrong here. I fixed it > in maint, which probably means that you will need to deal with a merge > conflict. I'm not sure the location of stars matter much in a technical sense, though I have never come across any environments using stars other than for the last character. This document compiles fine, although it is an exercise is obscurity: \documentclass{minimal} \newenvironment{ab*cd}[1]{\bfseries{#1}}{} \newenvironment{*abcd}[1]{\bfseries{#1}}{} \begin{document} \begin{ab*cd} x \end{ab*cd} \begin{*abcd} x \end{*abcd} \end{document} I'm happy to accept only stars in the end, though. >> + ;; The following format string also matches optional arguments: >> + ;; "^[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}\\(\\[.*?\\]\\|{.*?}\\)*[ \t]*?" > > I think this comment is not necessary. It's a nice regexp. . . >> + "Format string matching the beginning of a LaTeX environment. >> + >> +Usage example: >> + (format org-element--latex-begin-environment ENV) >> +where ENV is a LaTeX environment. > > `org-element--latex-begin-environment' is not a format string. > `org-element--latex-end-environment' is. True. > You can remove this part. Add that the environment is put in group 1, > though. > > Otherwise, the patch looks good. Feel free to apply it with suggested > changes. Sorry, I can't push to Org-git. But a patch is attached. Thanks again, Rasmus -- This is the kind of tedious nonsense up with which I will not put --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-org-element.el-Allow-one-line-LaTeX-environments.patch >From fce1bff7789f90a9fa16cc318233166faf961a29 Mon Sep 17 00:00:00 2001 From: Rasmus Date: Tue, 22 Jul 2014 15:09:03 +0200 Subject: [PATCH] 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. --- lisp/org-element.el | 23 +++++++++++++++++------ testing/lisp/test-org-element.el | 22 +++++++++++++++++++++- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/lisp/org-element.el b/lisp/org-element.el index bfbba5e..75fccc5 100644 --- a/lisp/org-element.el +++ b/lisp/org-element.el @@ -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]+\\*?\\)}") + ((looking-at org-element--latex-begin-environment) (org-element-latex-environment-parser limit affiliated)) ;; Drawer and Property Drawer. ((looking-at org-drawer-regexp) diff --git a/testing/lisp/test-org-element.el b/testing/lisp/test-org-element.el index 04ef1ce..0ca0f8a 100644 --- a/testing/lisp/test-org-element.el +++ b/testing/lisp/test-org-element.el @@ -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\\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 "\\begin{env}{arg} something\nvalue\n\\end{env}" + (org-test-with-temp-text "y \\begin{env} x \\end{env}" (org-element-type (org-element-at-point))))) ;; Handle non-empty blank line at the end of buffer. (should -- 2.0.2 --=-=-=--