问题
I am trying to resize the occur-mode buffer window to fit the contents of its buffer.
See Resize occur window in Emacs for more information.
I have added the following hook:
(add-hook 'occur-mode-hook
(lambda ()
(save-selected-window
(pop-to-buffer "*Occur*")
(message-box "ok")
(fit-window-to-buffer nil 10))))
Then I have the following buffer window:
and I now execute (occur "test") which gives me first
and after pressing the "ok" button I get
Notice that the occur window has shrunk to a single line in height at the bottom of the frame. This was obviously not what I wanted..
I now enter (occur "test") once more in the "t.txt" buffer, and after pressing "ok" to the message-box I get the following:
So now it suddenly works perfectly. Why is this not working the first time?
回答1:
This works:
(defadvice occur (after occur-advice activate)
"Resize window."
(save-selected-window
(pop-to-buffer "*Occur*")
(fit-window-to-buffer nil 10)))
回答2:
IIUC, the important part is to keep the call to fit-window-to-buffer within the save-selected-window.
来源:https://stackoverflow.com/questions/20919308/strange-behavior-in-occur-mode-hook-in-emacs