21.6.2 补全与小缓冲

本节描述用于带补全功能从小缓冲读取输入的基础接口。

Function: completing-read prompt collection &optional predicate require-match initial history default inherit-input-method

该函数在小缓冲中读取字符串,并通过提供补全辅助用户输入。它使用提示字符串 prompt(必须为字符串)激活小缓冲。

实际补全通过将补全表 collection 和补全谓词 predicate 传给函数 try-completion 完成(see 基础补全函数)。这一过程在补全所使用的局部键映射中绑定的特定命令里执行。其中部分命令也会调用 test-completion。因此,若 predicatenil,它必须与 collectioncompletion-ignore-case 兼容。See Definition of test-completion

关于 collection 为函数时的详细要求,参见 see 可编程补全

可选参数 require-match 的值决定用户如何退出小缓冲:

  • 若为 nil,常规小缓冲退出命令无论小缓冲中输入内容如何均可生效。
  • 若为 t,常规小缓冲退出命令仅在输入可补全为 collection 中元素时才允许退出。
  • 若为 confirm,用户可使用任意输入退出,但如果输入不是 collection 中的元素,则会要求确认。
  • 若为 confirm-after-completion,用户可使用任意输入退出,但如果前一条命令是补全命令(即 minibuffer-confirm-exit-commands 中的命令之一)且结果输入不是 collection 中的元素,则会要求确认。See 执行补全的小缓冲命令
  • 若为函数,则以输入作为唯一参数调用该函数。若输入可接受,函数应返回非 nil
  • require-match 的其他值均按 t 处理,区别在于如果执行了补全操作,退出命令不会直接退出。

但无论 require-match 取值如何,空输入始终允许;此时若 default 为列表,completing-read 返回 default 的第一个元素;若 defaultnil,返回 "";否则返回 defaultdefault 中的字符串也可通过历史命令供用户使用(see Minibuffer 命令)。此外,当用户通过 M-n 遍历完 default 中的值后,补全候选会被添加到 “未来历史(future history)” 中;参见 minibuffer-default-add-function

require-matchnil,函数 completing-read 使用 minibuffer-local-completion-map 作为键映射;若非 nil,则使用 minibuffer-local-must-match-map。See 执行补全的小缓冲命令

参数 history 指定用于保存输入和小缓冲历史命令的历史列表变量,默认为 minibuffer-history。若 history 为符号 t,则不记录历史。See 迷你缓冲历史

参数 initial 基本已不推荐使用;我们建议仅在为 history 指定 cons 单元时才使用非 nil 值。See 初始输入。默认输入请使用 default

若参数 inherit-input-methodnil,则小缓冲从进入小缓冲前的当前缓冲区继承当前输入法(see Input Methods)和 enable-multibyte-characters 设置(see Text Representations)。

若变量 completion-ignore-casenil,补全时将输入与可能匹配项对比不区分大小写。See 基础补全函数。在此模式下,predicate 也必须不区分大小写,否则会出现意外结果。

使用 completing-read 的示例:

(completing-read
 "Complete a foo: "
 '(("foobar1" 1) ("barfoo" 2) ("foobaz" 3) ("foobar2" 4))
 nil t "fo")

;; After evaluation of the preceding expression,
;;   the following appears in the minibuffer:

---------- Buffer: Minibuffer ----------
Complete a foo: fo∗
---------- Buffer: Minibuffer ----------

若用户随后输入 DEL DEL b RETcompleting-read 返回 barfoo

completing-read 会绑定若干变量,用于向实际执行补全的命令传递信息。这些变量将在下一节描述。

Variable: completing-read-function

此变量的值必须是一个函数,由 completing-read 调用以实际执行工作。它应接收与 completing-read 相同的参数。可将其绑定到其他函数,以完全覆盖 completing-read 的常规行为。

若需要提示用户输入多个字符串,例如列表的多个元素或连接的多个参数(如用户、主机、端口),可使用 completing-read-multiple。它允许输入由分隔符字符串分隔的多个字符串(默认为制表符和逗号;可自定义 crm-separator 修改),并为用户输入的每个单独字符串提供补全。返回读取到的字符串列表。


emacs

Emacs

org-mode

Orgmode

Donations

打赏

Copyright

© Jasper Hsu

Creative Commons

Creative Commons

Attribute

Attribute

Noncommercial

Noncommercial

Share Alike

Share Alike