2

I have not been able to get the minibuffer-exit-hook to play nice with read-string. As far as I can tell, I should no longer be in the minibuffer after finishing up with read-string. However, the condition (minibufferp) says I'm still in the minibuffer even though read-string finished. read-string is written in C, so I can't add the hook there (i.e., at the tail end of the read-string function).

"Documentation [minibuffer-exit-hook]:  Normal hook run just after exit from minibuffer.

[After thinking a little more about this, I'm pretty sure it's a bug -- so I filed a bug report: bug#16524. As I learn more, I'll update this thread.

(defun test ()
  (interactive)
  (read-string "Prompt: " "testing"))

(add-hook 'minibuffer-exit-hook (lambda ()
  (cond
    ((minibufferp)
      (message "Focus is still in the minibuffer:  %s" (buffer-name)))
    (t (message "Contragulations -- focus is now in: %s." (buffer-name))))))
4

2 回答 2

4

文档字符串不准确;就这样。当在 minibuffer 中输入文本完成(不再可能)时,钩子运行。运行时当前的缓冲区仍然是迷你缓冲区。(这就是它应该的方式,FWIW。)

请注意,Elisp 手册的说法略有不同(但同样,不是很准确):

 This is a normal hook that is run whenever the minibuffer is
 entered.

(“无论何时”,意思是大约在同一时间,不一定在之后。)

如果您想read-string在代码中每次使用后执行某项操作,请定义一个执行以下操作的函数:首先(read-string...),然后是您接下来想要执行的任何操作。并使用该功能。

如果您还需要影响 的其他调用read-string,除了您在代码中编写的调用之外,请建议函数read-string在原版代码完成后执行任何操作。

例如:

(defadvice read-string (after fooness activate)
   (message "buffer: %S" (current-buffer)))

[注意:是的,您可以建议原语(用 C 编写的函数)。你以前甚至可以建议特殊形式,但他们逐渐取消了该功能。]

于 2014-01-23T05:07:02.717 回答
1

在你真正退出 minibuffer 之后运行 hook 是毫无意义的:你可以在任何类型的缓冲区中(因为 minibuffer 的使用可以从任何地方触发),因此你对当前上下文知之甚少(除非你使用缓冲区本地出口-钩子,我猜)。

如果您想在选定窗口更改时运行挂钩,那么您最好的选择可能是使用post-command-hook将当前选定窗口存储在辅助变量中并使用它与前一个选定窗口进行比较。

于 2014-01-23T13:58:12.297 回答