21.4 迷你缓冲历史

迷你缓冲历史列表(minibuffer history list) 用于记录之前的迷你缓冲输入,方便用户重复使用。它是一个变量,值为字符串列表(之前的输入),最近输入排在最前。

Emacs 提供多个独立的迷你缓冲历史列表,用于不同类型的输入。Lisp 程序员需要为每次迷你缓冲使用指定正确的历史列表。

可以通过 read-from-minibuffercompleting-read 的可选参数 history 指定低迷缓冲历史列表,可选值如下:

variable

使用变量 variable(符号)作为历史列表。

(variable . startpos)

使用变量 variable(符号)作为历史列表,并指定初始历史位置为 startpos(非负整数)。

startpos 为 0 等价于只指定符号 variableprevious-history-element 会在小缓冲中显示历史列表最近的元素。若指定正整数 startpos,小缓冲历史函数的行为如同当前小缓冲显示的是历史元素 (elt variable (1- startpos))

为保持一致,应同时通过小缓冲输入函数的 initial 参数,将该历史元素设为小缓冲初始内容(see 初始输入)。

若不指定 history,则使用默认历史列表 minibuffer-history。其他标准历史列表见下文。你也可以创建自定义历史列表变量,只需在首次使用前将其初始化为 nil。若该变量是缓冲区局部变量,则每个缓冲区拥有独立的输入历史列表。

read-from-minibuffercompleting-read 都会自动向历史列表添加新元素,并提供命令让用户重复使用列表中的条目(see Minibuffer Commands)。程序使用历史列表只需完成初始化,并在需要时将其名称传给输入函数即可。在小缓冲输入函数未使用时,手动修改历史列表也是安全的。

默认情况下,当 M-nnext-history-element,see next-history-element)到达发起小缓冲输入的命令所提供的默认值列表末尾时,M-n 会将 minibuffer-completion-table 指定的所有补全候选(see Minibuffer Commands that Do Completion)添加到默认值列表,使这些候选作为 “未来历史(future history)” 可用。程序可通过变量 minibuffer-default-add-function 控制这一行为:若其值不是函数,则禁用自动添加;也可将该变量设为自定义函数,只添加部分候选或其他值到 “未来历史(future history)”。

向历史列表添加新元素的 Emacs 函数会在列表过长时删除旧元素。变量 history-length 指定大多数历史列表的最大长度。若要为特定历史列表指定不同最大长度,可将长度存入该历史列表符号的 history-length 属性中。变量 history-delete-duplicates 指定是否删除历史中的重复项。

Function: add-to-history history-var newelt &optional maxelt keep-all

若新元素 newelt 非空字符串,此函数将其添加到变量 history-var 存储的历史列表,并返回更新后的历史列表。列表长度会被限制为 maxelt(若非 nil)或 history-length(见下文)。maxelt 的取值含义与 history-length 相同。 history-var 不能指向词法变量。

通常,若 history-delete-duplicatesniladd-to-history 会删除历史列表中的重复成员。但若 keep-allnil,则不删除重复项,即使 newelt 为空也会添加到列表。

Variable: history-add-new-input

若此变量值为 nil,从小缓冲读取的标准函数不会向历史列表添加新元素。这允许 Lisp 程序通过 add-to-history 显式管理输入历史。默认值为 t

User Option: history-length

此变量的值指定所有未自行设置最大长度的历史列表的最大长度。若值为 t,表示无长度限制(不删除旧元素)。若某历史列表变量的符号拥有非 nilhistory-length 属性,则该属性会覆盖此变量对该历史列表的作用。

User Option: history-delete-duplicates

若此变量值为 t,则添加新历史元素时,会删除所有之前相同的元素。

以下是部分标准小缓冲历史列表变量:

Variable: minibuffer-history

小缓冲历史输入的默认历史列表。

Variable: query-replace-history

query-replace 参数(及其他命令类似参数)的历史列表。

Variable: file-name-history

文件名参数的历史列表。

Variable: buffer-name-history

缓冲区名参数的历史列表。

Variable: regexp-history

正则表达式参数的历史列表。

Variable: extended-command-history

扩展命令名参数的历史列表。

Variable: shell-command-history

Shell 命令参数的历史列表。

Variable: read-expression-history

待求值 Lisp 表达式参数的历史列表。

Variable: face-name-history

面孔参数的历史列表。

Variable: custom-variable-history

read-variable 读取的变量名参数的历史列表。

Variable: read-number-history

read-number 读取的数字的历史列表。

Variable: goto-line-history

goto-line 参数的历史列表。通过自定义用户选项 goto-line-history-local,可使该变量在每个缓冲区中局部化。


emacs

Emacs

org-mode

Orgmode

Donations

打赏

Copyright

© 2025 Jasper Hsu

Creative Commons

Creative Commons

Attribute

Attribute

Noncommercial

Noncommercial

Share Alike

Share Alike