Hi all, I'd like to apply the attached fix for this issue. If no objection, I'll install it with some regression tests.
Regards, Ikumi Keita #StandWithUkraine #StopWarInUkraine
>From a496210874133b219d8e881f9a7178d7c9e23e91 Mon Sep 17 00:00:00 2001 From: Ikumi Keita <ik...@ikumi.que.jp> Date: Fri, 6 Jan 2023 01:24:56 +0900 Subject: [PATCH] Support detached arguments * tex.el (TeX-find-macro-boundaries): Skip white spaces between a macro argument and comment starter. Fix doc string. Add FIXME comment. (TeX-find-macro-end-helper): Skip white spaces at the beginning of line even when the preceding line doesn't end with comment. --- tex.el | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tex.el b/tex.el index e032d8c5..6d854c89 100644 --- a/tex.el +++ b/tex.el @@ -5573,10 +5573,18 @@ in the buffer." (TeX-find-balanced-brace -1 depth limit)) (defun TeX-find-macro-boundaries (&optional lower-bound) - "Return a list containing the start and end of a macro. + "Return a cons containing the start and end of a macro. If LOWER-BOUND is given, do not search backward further than this point in buffer. Arguments enclosed in brackets or braces are considered part of the macro." + ;; FIXME: Pay attention to `texmathp-allow-detached-args' and + ;; `reftex-allow-detached-macro-args'. + ;; Should we handle cases like \"{o} and \\[3mm] (that is, a macro + ;; whose name is a symbol and takes some arguments) as well? Note + ;; that amsmath package arranges the macro \\ so that white spaces + ;; between \\ and [something] prevents the latter to be interpreted + ;; as an optional argument. mathtools package has similar + ;; arrangement for some environments including gathered. (save-restriction (when lower-bound (narrow-to-region lower-bound (point-max))) @@ -5603,6 +5611,7 @@ considered part of the macro." (condition-case nil (backward-sexp) (error (throw 'abort nil))) (forward-comment -1) + (skip-chars-backward " \t") (and (memq (char-before) '(?\] ?\})) (not (TeX-escaped-p (1- (point))))))) (skip-chars-backward "A-Za-z@*") @@ -5632,7 +5641,7 @@ those will be considered part of it." (while (not (eobp)) (cond ;; Skip over pairs of square brackets - ((or (looking-at "[ \t]*\n?\\(\\[\\)") ; Be conservative: Consider + ((or (looking-at "[ \t]*\n?[ \t]*\\(\\[\\)") ; Be conservative: Consider ; only consecutive lines. (and (looking-at (concat "[ \t]*" TeX-comment-start-regexp)) (save-excursion @@ -5643,7 +5652,7 @@ those will be considered part of it." (forward-sexp) (scan-error (throw 'found (point))))) ;; Skip over pairs of curly braces - ((or (looking-at "[ \t]*\n?{") ; Be conservative: Consider + ((or (looking-at "[ \t]*\n?[ \t]*{") ; Be conservative: Consider ; only consecutive lines. (and (looking-at (concat "[ \t]*" TeX-comment-start-regexp)) (save-excursion -- 2.38.1
>>>>> Ikumi Keita <ik...@ikumi.que.jp> writes: > Hi Enrico, >>>>> Enrico Flor <enr...@eflor.net> writes: >> Here are the positions where point lands after evaluating >> (goto-char (TeX-find-macro-end-helper)) >> with point at the beginning of the macro: >> \macro{first} >> {second}█ >> \macro{first}█ >> {second} >> \macro{first} % comment >> {second}█ >> What happens is that whitespace in the second line is skipped only if >> the previous line ended with some comment. Since "second" is parsed as >> the second argument of the macro in all three cases, I think >> TeX-find-macro-end-helper should return the position right after >> `{second}` in all three cases. If this is correct, this patch should >> implement the behavior (it just modifies two regexps in that function). > Indeed. (The form to evaluate should actually be > (goto-char (TeX-find-macro-end-helper (point))) > , but that doesn't matter much.) >> Please forgive the noise if this is in fact an intentional behavior. > I suppose that the reported difference is just accidental one without > any particular intention. So I think AUCTeX can accept your patch. > Here are some related thoughts: > (1) The former half of `TeX-find-macro-boundaries' has similar subtle > inconsistency. When I evaluate > (let (start-point) > (save-excursion > (catch 'abort > (let ((parse-sexp-ignore-comments t)) > (when (condition-case nil (progn (up-list) t) (error nil)) > (while (progn > (condition-case nil (backward-sexp) > (error (throw 'abort nil))) > (forward-comment -1) > (and (memq (char-before) '(?\] ?\})) > (not (TeX-escaped-p (1- (point))))))) > (skip-chars-backward "A-Za-z@*") > (when (and (eq (char-before) (aref TeX-esc 0)) > (not (TeX-escaped-p (1- (point))))) > (setq start-point (1- (point)))))))) > (goto-char start-point)) > inside the "{second}" in the following examples, the third one leads to > error caused by `(goto-char nil)'. > \macro{first} > {second} > \macro{first} > {second} > \macro{first} % comment > {second} > \macro{first}% comment > {second} > Thus I think `TeX-find-macro-boundaries' should be fixed as well. > (2) There are customize options slightly related to the current topic, > namely `texmathp-allow-detached-args' and > `reftex-allow-detached-macro-args'. They both default to nil, so maybe > we should introduce a new customize option to do similar fine-control > over the behavior of `TeX-find-macro-boundaries' and > `TeX-find-macro-end-helper'. > (3) There are two packages related to the current topic: > The document of amsmath tells, with respect to \\[<dimension>] inside > display math environment, that > ,---- > | Do not type a space between the \\ and the following [; only for display > | environments defined by amsmath the space is interpreted to mean that > | the bracketed material is part of the document content. > `---- > The document of mathtools says that it allows users to write > \[ > \begin{gathered} > [t] = 100 \\ > [v] = 200 > \end{gathered} > \] > where [t] isn't interpreted as an optional argument for gathered > environment. (This is package default behavior; "allowspaces" package > option can disable it.) > Thus if we are to be very strict, AUCTeX shouldn't consider separated > brackets as optional arguments for these cases. (I'm not sure whether > it's worth discriminating such strictly.)