【发布时间】:2021-06-03 15:39:40
【问题描述】:
我需要一些帮助来理解即时完成。
我有这个最小的例子,我想:
- 当我输入“@”时激活
- 搜索/完成候选车...
- ...但返回 cdr,因此此时的结果是,例如“@doe”(尽管在某些情况下,我可能需要扩展它以删除“@”,例如使用 LaTeX)。
actual use case 用于在文档中插入引文关键字,但搜索作者、标题等。其目的是与corfu 和company-capf 等解决方案一起使用。
在该代码中,它是 bibtex-completion 的前端,如 helm-bibtex 和 ivy-bibtex,我有一个基于 completing-read-multiple 的核心 bibtex-actions-read 函数,用于完成 minibuffer。
有了这个 capf,我想使用相同的缓存数据来完成对点完成。
通过这个测试示例,我得到 1 和 2,这正是我想要的 UI 端。
(defun test-capf ()
"My capf."
(when (looking-back "@[a-zA-Z]*")
(list
(save-excursion
(backward-word)
(point))
(point)
(lambda (str pred action)
(let ((candidates '(("a title doe" . "doe")
("different title jones" . "jones")
("nothing smith" . "smith"))))
(complete-with-action action candidates str pred))))))
但是我如何适应它来添加 3?也就是说,如果我输入“@not”,corfu 或 company 应该显示“nothing smith”,如果我选择该项目,它应该返回“@smith”。
注意:我的包很大程度上依赖于像orderless 这样的完成样式,所以顺序当然不重要。
我需要在这里使用:exit-function 吗?
为了完整起见,这是当前的实际功能,当我尝试使用它时,它现在显示“不匹配”。
(defun bibtex-actions-complete-key-at-point ()
"Complete citation key at point.
When inserting '@' in a buffer the capf UI will present user with
a list of entries, from which they can narrow against a string
which includes title, author, etc., and then select one. This
function will then return the key 'key', resulting in '@key' at
point."
;; FIX current function only returns "no match"
;; TODO this regex needs to adapt for mode/citation syntax
(when (looking-back "@[a-zA-Z]+" 5)
(let* ((candidates (bibtex-actions--get-candidates))
(begin (save-excursion (backward-word) (point)))
(end (point)))
(list begin end candidates :exclusive 'no
;; I believe I need an exit-function so I can insert the key instead
;; of the candidate string.
:exit-function
(lambda (chosen status)
(when (eq status 'finished)
(cdr (assoc chosen candidates))))))))
还有其他提示或建议吗?
This Q&A 是相关的,但我不知道如何适应它。
【问题讨论】:
标签: emacs tab-completion