Answers:
“最简单的方法”是只定义您自己的版本describe-function
,并将其绑定到C-h f
。
使用原始代码,并仅更改对的调用,completing-read
以使其使用M-x
(execute-extended-command
)使用的相同历史记录列表,即extended-command-history
。
(defun my-describe-function (function)
"Display the full documentation of FUNCTION (a symbol)."
(interactive
(let ((fn (function-called-at-point))
(enable-recursive-minibuffers t)
val)
(setq val (completing-read
(if fn
(format "Describe function (default %s): " fn)
"Describe function: ")
obarray 'fboundp t nil
'extended-command-history ; <======================
(and fn (symbol-name fn))))
(list (if (equal val "") fn (intern val)))))
(if (null function)
(message "You didn't specify a function")
(help-setup-xref (list #'describe-function function)
(called-interactively-p 'interactive))
(save-excursion
(with-help-window (help-buffer)
(prin1 function)
(princ " is ")
(describe-function-1 function)
(with-current-buffer standard-output
(buffer-string))))))
(global-set-key "\C-hf" 'my-describe-function)
我如何找到原始代码?C-h f describe-function
,C-h k M-x
,C-h f execute-extended-command
。在execute-extended-command
我的代码中,我看到它使用读取命令名称read-extended-command
,并调用completing-read
pass extended-command-history
作为HISTORY
参数。
我不能为您的问题添加确切的答案,而是可以消除该问题的工作流程。
我用smex
代替execute-extended-command
。一旦进入的迷你缓冲区smex
:
execute-extended-command
smex-describe-function
smex-find-function
我不喜欢默认绑定,所以我对它们进行了自定义:
(eval-after-load 'smex
`(defun smex-prepare-ido-bindings ()
(define-key ido-completion-map (kbd "TAB") 'minibuffer-complete)
(define-key ido-completion-map (kbd "C-,") 'smex-describe-function)
(define-key ido-completion-map (kbd "C-w") 'smex-where-is)
(define-key ido-completion-map (kbd "C-.") 'smex-find-function)
(define-key ido-completion-map (kbd "C-a") 'move-beginning-of-line)
(define-key ido-completion-map "\C-i" 'smex-helm)))
请注意,从其帮助缓冲区中调用命令非常容易。键入后,C-h f
只需键入M-x M-n RET
。之所以有效,是因为在新的“帮助”缓冲区中,命令名称位于光标下方的缓冲区的顶部,并将其M-n
检索到迷你缓冲区中。
但是,如果您想在extended-command-history
每次访问其文档时都添加一个命令,则可以通过一个小的建议来做到这一点:
(defun describe-function-extended-command-history (function)
"Add command name to the history."
(when (commandp function)
(add-to-history 'extended-command-history (symbol-name function))))
(advice-add 'describe-function :before #'describe-function-extended-command-history)
或使用define-advice
25.0.50中刚刚添加的新宏:
(define-advice describe-function (:before (function))
"Add command name to the history."
(when (commandp function)
(add-to-history 'extended-command-history (symbol-name function))))
(interactive)
怎么办?
(commandp function)
检查查找功能是否是交互式的,因为仅应将命令添加到中extended-command-history
。因此,如果查找功能不是交互式的,则不会添加到中extended-command-history
。
如果使用helm-M-x
,则无需键入C-h f
来查找命令的文档,只需使用C-j
或C-z
在运行helm-M-x
时切换文档的显示即可。
另请参阅Helm Mx的功能。
smex
和helm-M-x
?前者在MELPA中,后者helm
在MELPA中。