Strange behavior in occur-mode hook in Emacs

瘦欲@ 提交于 2020-01-04 05:17:16

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!