Dear AUCTeX developers and users,
Hi there,
First, I'd like to express my immense appreciation for your incredible
work on AUCTeX. It is an indispensable tool.
I am writing to seek your guidance on what is considered the best
practice for achieving reliable file path completion within the
arguments of LaTeX commands.
**My Environment:**
* Emacs 31.0.50
* AUCTeX (latest from Git)
* Modern completion setup: Corfu, Cape, lsp-mode (with texlab)
**The Problem:**
In my setup, the standard automatic completion chain
(`completion-at-point-functions`) is unreliable for file paths inside
commands like `\include{./}` or `\addbibresource{./}`.
My extensive debugging suggests this is due to a "short-circuiting" effect:
1. Higher-priority completion backends (like `lsp-mode` or
`cape-dabbrev`) are triggered first.
2. Even if they find no relevant candidates for a prefix like `./`,
they seem to return a "successful but empty" result.
3. This "successful" result prevents the completion chain from
proceeding to lower-priority, more specialized backends like
`completion-file-name-table`. The file completion backend is never
given a chance to run.
**My Current Solution:**
After trying and failing to solve this within the automatic completion
chain, I have developed a manual, interactive command that is 100%
reliable. It works by completely bypassing the automatic chain.
Here is the implementation:
```emacs-lisp
(defun my/complete-file-path-force ()
"Force file path completion at point by calling Emacs's official engine.
This function manually calculates the completion boundaries to bypass any
syntactic interference from the surrounding LaTeX command."
(interactive)
(let* (;; 1. Manually calculate the correct completion boundaries.
(end (point))
(start (save-excursion (re-search-backward "[ \t\n\\({]" nil t)))
(start (if start
(1+ start)
(line-beginning-position))))
;; 2. Invoke completion directly with the official file completion table.
(completion-in-region start end #'completion-file-name-table)))
```
I then bind this command to a key within `LaTeX-mode-map`, for example:
```emacs-lisp
(define-key LaTeX-mode-map (kbd "C-c l f") #'my/complete-file-path-force)
```
This manual approach works perfectly every time.
**My Questions:**
While my solution is effective, I am keen to know if it is the optimal
or most idiomatic approach.
1. Is my analysis of the "short-circuit" problem in the automatic
completion chain correct?
2. Is this manual, key-bound command the recommended way to solve
this problem within the AUCTeX ecosystem?
3. Does AUCTeX perhaps have a built-in command, variable, or
mechanism to handle this exact scenario, which I may have missed in
the documentation?
4. Is there a more elegant way to configure
`completion-at-point-functions` itself to grant file completion higher
priority in these specific command-argument contexts?
Thank you for creating such a powerful tool and for any guidance you
can provide on this matter. Your insights would be greatly
appreciated.
Best regards,
Zhao
--
Assoc. Prof. Hongsheng Zhao <[email protected]>
Theory and Simulation of Materials
Hebei Vocational University of Technology and Engineering
No. 473, Quannan West Street, Xindu District, Xingtai, Hebei province