I was relying on company-reftex for doing completion of LaTeX references. I recently made the switch to corfu/cape for the completion, and I miss the context that company-reftex provided. LaTeX-mode provides a basic completion but only with label name information, reftex brings more context.
company-reftex is simple enough for the Elisp-Padawan I am to understand, so I started to adapt it to an independent capf function:
(defvar cape-reftex-max-annotation-length 20)
(defvar cape-reftex-ref-regexp
(rx "\\"
;; List taken from `reftex-ref-style-alist'
(or "autoref"
"autopageref"
"Cpageref"
"cpageref"
"Cref"
"cref"
"eqref"
"Fref"
"fref"
"pageref"
"Ref"
"ref"
"vpageref"
"Vref"
"vref")
"{"
(group (* (not (any "}"))))
(regexp "\\="))
"Regular expression to use when looking for the ref prefix.
Group number 1 should be the prefix itself."
)
(defun cape-reftex-prefix (regexp)
"Return the prefix for matching given REGEXP."
(and (derived-mode-p 'latex-mode)
reftex-mode
(when (looking-back regexp nil)
(match-string-no-properties 1))))
(defun cape-reftex-annotate (key annotation)
"Annotate KEY with ANNOTATION if the latter is not nil.
Obeys the setting of `company-reftex-max-annotation-length'."
(cond
((not annotation) key)
((not cape-reftex-max-annotation-length)
(propertize key 'reftex-annotation annotation))
(t (propertize key 'reftex-annotation
(s-truncate cape-reftex-max-annotation-length annotation)))))
(defun cape-reftex-label-candidates (prefix)
"Find all label candidates matching PREFIX."
(reftex-access-scan-info)
(reftex-parse-all)
(cl-loop for entry in (symbol-value reftex-docstruct-symbol)
if (and (stringp (car entry)) (string-prefix-p prefix (car entry)))
collect
;; TODO (cape-reftex-annotate (car entry) (cl-caddr entry))))
(car entry)))
(defun cape-reftex-label ()
(interactive "*")
(when-let*
((trig (and reftex-mode (looking-back cape-reftex-ref-regexp nil)))
(prefix (match-string-no-properties 1))
(beg (match-beginning 1))
(end (match-end 1))
(candidate (-uniq (cape-reftex-label-candidates prefix)))
)
(list beg end candidate . nil )))
;; add
(add-hook 'LaTeX-mode-hook #'(lambda () (add-hook 'completion-at-point-functions #'cape-reftex-label 0 t)))
However I have several question regarding completion framework that I could not answer on my own:
how can I add info to the completion list (icons, extra annotation, documentation popup) to my capf to make it better. Ideally when completing I would like this to appear
(cherry on top would be to have the preview of the equation in the "documentation" popup, but I am not even sure it is possible)
Currently my capf function scan the full document with reftex-parse-all at every keystroke, and thus is quite sluggish, once it hits it. Is there some cache mechanism I am not aware of ?
Once these issues are resolved, would you be interested for a PR to add cape-reftex into cape ?
Thank you for all your contribution to the emacs ecosystem.
Hi there,
I was relying on
company-reftex
for doing completion of LaTeX references. I recently made the switch to corfu/cape for the completion, and I miss the context that company-reftex provided. LaTeX-mode provides a basic completion but only with label name information, reftex brings more context.company-reftex
is simple enough for the Elisp-Padawan I am to understand, so I started to adapt it to an independent capf function:However I have several question regarding completion framework that I could not answer on my own:
how can I add info to the completion list (icons, extra annotation, documentation popup) to my capf to make it better. Ideally when completing I would like this to appear (cherry on top would be to have the preview of the equation in the "documentation" popup, but I am not even sure it is possible)
Currently my capf function scan the full document with reftex-parse-all at every keystroke, and thus is quite sluggish, once it hits it. Is there some cache mechanism I am not aware of ?
Once these issues are resolved, would you be interested for a PR to add cape-reftex into cape ?
Thank you for all your contribution to the emacs ecosystem.