如何使Mx和Ch f共享历史?


11

通常,我会使用查看命令的文档C-h f,然后M-x立即调用该命令。

现在,我发现“调用最后一个视图命令”的最短方法是复制其名称(从帮助缓冲区或历史记录中复制)并将其拖入M-x的minibuffer提示符。

我真正想要的是使命令名称 describe-function成为的历史一部分 execute-extended-command。所以我可以做M-x M-p RET

最简单的方法是什么?


1
没有一个答案,但会提高您的工作流程:你有没有听说过的smexhelm-M-x?前者在MELPA中,后者helm在MELPA中。
Ehvince 2014年

Answers:


7

“最简单的方法”是只定义您自己的版本describe-function,并将其绑定到C-h f

使用原始代码,并仅更改对的调用,completing-read以使其使用M-xexecute-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-functionC-h k M-xC-h f execute-extended-command。在execute-extended-command我的代码中,我看到它使用读取命令名称read-extended-command,并调用completing-readpass extended-command-history作为HISTORY参数。


8

我不能为您的问题添加确切的答案,而是可以消除该问题的工作流程。

我用smex代替execute-extended-command。一旦进入的迷你缓冲区smex

  • RET 来电 execute-extended-command
  • C-h f 来电 smex-describe-function
  • M-. 来电 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)))

6

请注意,从其帮助缓冲区中调用命令非常容易。键入后,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-advice25.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)怎么办?
mbork 2014年

(commandp function)检查查找功能是否是交互式的,因为仅应将命令添加到中extended-command-history。因此,如果查找功能不是交互式的,则不会添加到中extended-command-history
link0ff

啊,我错过了。感谢您的解释!
mbork 2014年

1

如果使用helm-M-x,则无需键入C-h f来查找命令的文档,只需使用C-jC-z在运行helm-M-x时切换文档的显示即可。

另请参阅Helm Mx的功能

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.