Вопрос

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))))))
Это было полезно?

Решение 2

Running a hook after you truly exited the minibuffer is rather pointless: you could be in any kind of buffer (since minibuffer use can be triggered from anywhere) and you hence know very little about the current context (unless you use a buffer-local exit-hook, I guess).

If you want to run a hook when the selected window changes, then your best option is probably to use a post-command-hook that stores the current selected-window in an auxiliary variable and uses it to compare to the previous selected-window.

Другие советы

The doc string is not exact; that's all. The hook is run when inputting text in the minibuffer is done (no longer possible). The buffer that is current when it is run is still the minibuffer. (And that's the way it should be, FWIW.)

Note that the Elisp manual puts it slightly differently (but again, not very precisely):

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

("Whenever", meaning about the same time as, not necessarily after.)

If you want to do something after every use of read-string in your code, then define a function that does the following: first (read-string...), then whatever you want done next. And use that function.

If you need to affect also other invocations of read-string, besides those you write in your code, then advise function read-string to perform whatever action after the vanilla code finishes.

For example:

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

[Note: Yes, you can advise primitives (functions written in C). You used to even be able to advise special forms, but they regressively took away that feature.]

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top