Stefan Monnier <[email protected]> writes:

> Because as you fill a completion, the text considered as the
> "to-be-completed" area jumps around.
>
> The design of CAPF is that the function returns 2 things: the text area
> where completion takes place and the completion-table holding the set
> of candidates.  You currently have 2 competing notions of "the area to
> be completed" in `\setlength{\FOO}`: one says that `\FOO` is the area to
> be completed as an arg of `\setlength` and the other says that `FOO` is
> the area to be completed as a backslashed command name.
>
> Pick one, i.e. either never complete arguments of `\setlength` or never
> complete the argument of `\setlength` as a backslashed command name.
>
> As long as you sometimes use one sometimes the other, some UI out there
> will get confused.

Thanks, I think I understand now what you mean.

>> I think the issue is the value of `completion-at-point-functions' in
>> LaTeX-mode:
>>
>>   (TeX--completion-at-point t LaTeX--arguments-completion-at-point
>>                             ispell-completion-at-point)
>
> That might make things worse, indeed, but even if you swap the two
> (which does sound like a good idea, BTW), you'll still see
> similar problems because `LaTeX--arguments-completion-at-point` also
> returns different boundaries for `\setlength{\ba}`.

Yes, you're right about this.  I will rethink this, I have to remember
the reasons back then why I went this route.  One reason was performance
I think, I wanted to avoid running the whole machinery behind
`LaTeX--arguments-completion-at-point' each time the completion is
requested inside { } etc.

> See below my quick attempt at tracking down the problem.

Thanks, I will have a look.

I promise this is the last time I'm saying this, but I still don't
understand the change of behavior in Emacs that it now inserts something
and exits :-[

> diff --git a/latex.el b/latex.el
> index 9973c1a483..5daa2ca073 100644
> --- a/latex.el
> +++ b/latex.el
> @@ -7859,11 +7859,12 @@ COLLECTION is an list of strings."
>                                  (LaTeX-completion-macro-delimiters))
>          (up-list -1))
>        (forward-char)
> +      ;; FIXME: When completing the arg of \setlength{\ba}, this skips
> +      ;; the backslash which is wrong because the COLLECTION is a set of 
> lengths
> +      ;; each one of them starting with a backslash.
>        (skip-chars-forward "^a-zA-Z0-9" end)
>        (setq beg (point)))
> -    (list beg end (completion-table-dynamic
> -                   (lambda (_)
> -                     collection)))))
> +    (list beg end collection)))

I suppose wrapping collection inside `completion-table-dynamic' is
useless here since it is variable and not a function?

>  (defun LaTeX-completion-documentclass-usepackage (entry)
>    "Return completion candidates for \\usepackage and \\documentclass 
> arguments.
> @@ -8163,11 +8164,16 @@ taken."
>  (defun LaTeX--arguments-completion-at-point ()
>    "Capf for arguments of LaTeX macros and environments.
>  Completion for macros starting with `\\' is provided by the
> -function `TeX--completion-at-point' which should come first in
> +function `TeX--completion-at-point' which should come later in
>  `completion-at-point-functions'."
>    ;; Exit if not inside an argument or in a comment:
>    (when (and (LaTeX-completion-find-argument-boundaries)
>               (not (nth 4 (syntax-ppss))))
> +    ;; FIXME: AFAICT, `LaTeX-what-macro` already has to do a lot of the 
> parsing
> +    ;; needed to know the boundaries we should return.  But it doesn't
> +    ;; return the info, so we end up re-computing it in places
> +    ;; like `LaTeX-completion-candidates-completing-read` where we don't
> +    ;; know any more what it is we're parsing so we risk doing it 
> incorrectly.

`LaTeX-what-macro' is a general purpose function returning the position
of point and information about the type of argument (mandatory,
optional) and number of arguments before the current one.  The relevant
boundaries are calculated and returend by the functions
`LaTeX-completion-candidates-key-val',
`LaTeX-completion-candidates-completing-read-multiple' and
`LaTeX-completion-candidates-completing-read'.

Again, thanks for looking at this 🙏

Best, Arash

Reply via email to