bug#19281: 11.86; environment update fails when environment name contains \

2014-12-09 Thread Tassilo Horn
Mosè Giordano  writes:

>> I tried to update an environment in a latex document via C-u C-c C-e
>> or LaTeX-modify-environment but mistyped the name as 'theorem\'. I
>> tried to correct my error via C-u C-c C-e again but I got a search
>> failed message:
>>
>> LaTeX-modify-environment: Search failed: "end{ *\\([a-zA-Z*]*\\)}"
>
> I can confirm the bug you reported.  Indeed, `\begin' and `\end'
> accept as valid environment names macros (not your case though) and
> macros with arguments,

Oh, indeed.

> while `LaTeX-modify-environment' searches for names matching
> "[a-zA-Z*]".  So, I'm not sure how to fix it: Just add `TeX-esc' to
> the regexp?

That and TeX-grop, TeX-grcl, and [] for optional args, no?

> Or matching everything between the opening and the closing brace
> (allowing at least one level of braces)?

Either that, or we could also rely on syntax tables.  I.e., since
`LaTeX-find-matching-end' already brings us to the end of the
environment name,

  (let ((end (point)))
(forward-char)
(backward-sexp)
(forward-char)
(buffer-substring-no-properties (point) end))

gives us the environment name.

Bye,
Tassilo



___
bug-auctex mailing list
bug-auctex@gnu.org
https://lists.gnu.org/mailman/listinfo/bug-auctex


bug#19281: 11.86; environment update fails when environment name contains \

2014-12-09 Thread Mosè Giordano
Hi Tassilo,

2014-12-09 10:34 GMT+01:00 Tassilo Horn :
>> Or matching everything between the opening and the closing brace
>> (allowing at least one level of braces)?
>
> Either that, or we could also rely on syntax tables.  I.e., since
> `LaTeX-find-matching-end' already brings us to the end of the
> environment name,
>
>   (let ((end (point)))
> (forward-char)
> (backward-sexp)
> (forward-char)
> (buffer-substring-no-properties (point) end))
>
> gives us the environment name.

Good idea, except that `LaTeX-find-matching-end' fails to find the
closing brace of \end when the environment name is a macro with
arguments.  The attached patch should fix this problem.
`LaTeX-environment-name-regexp' needs to be a function because if it
were a variable it would always keep the original value, also in
buffers with different values for `TeX-grop' and `TeX-grcl' (or not?).
Probably `LaTeX-modify-environment' can be simplified following your
suggestion, the patch sketches an idea how to fix
`LaTeX-find-matching-end', but can be improved.

Bye,
Mosè
diff --git a/latex.el b/latex.el
index 0022ae2..477ef66 100644
--- a/latex.el
+++ b/latex.el
@@ -729,6 +729,15 @@ environment just inserted, the buffer position just before
 (run-hook-with-args 'LaTeX-after-insert-env-hooks
 			environment env-start env-end)))
 
+(defun LaTeX-environment-name-regexp ()
+  "Return the regexp matching the name of a LaTeX environment.
+This matches everything different from a TeX closing brace but
+allowing one level of TeX group braces."
+  (concat "\\([^" (regexp-quote TeX-grcl) (regexp-quote TeX-grop) "]*\\("
+	  (regexp-quote TeX-grop) "[^" (regexp-quote TeX-grcl)
+	  (regexp-quote TeX-grop) "]*" (regexp-quote TeX-grcl) "\\)*[^"
+	  (regexp-quote TeX-grcl) (regexp-quote TeX-grop) "]*\\)"))
+
 (defun LaTeX-modify-environment (environment)
   "Modify current ENVIRONMENT."
   (save-excursion
@@ -736,7 +745,7 @@ environment just inserted, the buffer position just before
 (re-search-backward (concat (regexp-quote TeX-esc)
 "end"
 (regexp-quote TeX-grop)
-" *\\([a-zA-Z*]*\\)"
+(LaTeX-environment-name-regexp)
 (regexp-quote TeX-grcl))
 			(save-excursion (beginning-of-line 1) (point)))
 (replace-match (concat TeX-esc "end" TeX-grop environment TeX-grcl) t t)
@@ -745,7 +754,7 @@ environment just inserted, the buffer position just before
 (re-search-forward (concat (regexp-quote TeX-esc)
 			   "begin"
 			   (regexp-quote TeX-grop)
-			   " *\\([a-zA-Z*]*\\)"
+			   (LaTeX-environment-name-regexp)
 			   (regexp-quote TeX-grcl))
 		   (save-excursion (end-of-line 1) (point)))
 (replace-match (concat TeX-esc "begin" TeX-grop environment TeX-grcl) t t)))
@@ -4124,7 +4133,7 @@ environment in commented regions with the same comment prefix."
 	 (comment-prefix (and in-comment (TeX-comment-prefix)))
 	 (case-fold-search nil))
 (save-excursion
-  (skip-chars-backward "a-zA-Z \t{")
+  (skip-chars-backward (concat "a-zA-Z \t" (regexp-quote TeX-grop)))
   (unless (bolp)
 	(backward-char 1)
 	(and (looking-at regexp)
@@ -4143,7 +4152,8 @@ environment in commented regions with the same comment prefix."
 	(setq level (1+ level))
 	  (setq level (1- level)
 (if (= level 0)
-	(search-forward "}")
+	(re-search-forward
+	 (concat TeX-grop (LaTeX-environment-name-regexp) TeX-grcl))
   (error "Can't locate end of current environment"
 
 (defun LaTeX-find-matching-begin ()
@@ -4158,7 +4168,7 @@ environment in commented regions with the same comment prefix."
 	 (in-comment (TeX-in-commented-line))
 	 (comment-prefix (and in-comment (TeX-comment-prefix)))
 	 (case-fold-search nil))
-(skip-chars-backward "a-zA-Z \t{")
+(skip-chars-backward (concat "a-zA-Z \t" (regexp-quote TeX-grop)))
 (unless (bolp)
   (backward-char 1)
   (and (looking-at regexp)
___
bug-auctex mailing list
bug-auctex@gnu.org
https://lists.gnu.org/mailman/listinfo/bug-auctex


bug#19281: 11.86; environment update fails when environment name contains \

2014-12-09 Thread Tassilo Horn
Mosè Giordano  writes:

Hi!

> 2014-12-09 10:34 GMT+01:00 Tassilo Horn :
>>> Or matching everything between the opening and the closing brace
>>> (allowing at least one level of braces)?
>>
>> Either that, or we could also rely on syntax tables.  I.e., since
>> `LaTeX-find-matching-end' already brings us to the end of the
>> environment name,
>>
>>   (let ((end (point)))
>> (forward-char)
>> (backward-sexp)
>> (forward-char)
>> (buffer-substring-no-properties (point) end))
>>
>> gives us the environment name.
>
> Good idea, except that `LaTeX-find-matching-end' fails to find the
> closing brace of \end when the environment name is a macro with
> arguments.

Ah, indeed.  During my testing it always stopped at
\end{\whatever{foo}|} with | being point, so I assumed that's where it's
supposed to stop, i.e., right after the environment name, not right
after the environment.

I guess it's better to also test the simple cases, not only the complex
ones. ;-)

> The attached patch should fix this problem.
> `LaTeX-environment-name-regexp' needs to be a function because if it
> were a variable it would always keep the original value, also in
> buffers with different values for `TeX-grop' and `TeX-grcl' (or not?).

Correct.  Well, it could also be a variable that's initialized in
`LaTeX-common-initialization' or so after a call to
`hack-local-variables' to apply file local variables first.  But IMO
it's better to have it as a function.

> Probably `LaTeX-modify-environment' can be simplified following your
> suggestion,

On a second thought, the syntax-table approach will fail in case the
environment name is something unbalanced like \foo[{] which is unlikely
but possible.  So better stick to the regexp-search.

> the patch sketches an idea how to fix `LaTeX-find-matching-end', but
> can be improved.

Looks good to me.

Bye,
Tassilo



___
bug-auctex mailing list
bug-auctex@gnu.org
https://lists.gnu.org/mailman/listinfo/bug-auctex