我相信有两种方法可以解决这个问题.
第一种是使用钩子''compilation-finish-functions',它是:
[编译过程完成时要调用的f]一系列代码.每个函数都使用两个参数调用:编译缓冲区和描述进程如何完成的字符串.
这导致了这样的解决方案:
(add-hook 'compilation-finish-functions 'my-compilation-finish-function)
(defun my-compilation-finish-function (buffer resstring)
"Shrink the window if the process finished successfully."
(let ((compilation-window-height (if (string-match-p "finished" resstring) 5 nil)))
(compilation-set-window-height (get-buffer-window buffer 0))))
Run Code Online (Sandbox Code Playgroud)
我对该解决方案的唯一问题是它假定可以通过在结果字符串中找到字符串"finished"来确定成功.
另一种方法是建议'compilation-handle-exit
- 明确地传递退出状态.我写了这个建议,当退出状态为非零时缩小窗口.
(defadvice compilation-handle-exit (around my-compilation-handle-exit-shrink-height activate)
(let ((compilation-window-height (if (zerop (car (ad-get-args 1))) 5 nil)))
(compilation-set-window-height (get-buffer-window (current-buffer) 0))
ad-do-it))
Run Code Online (Sandbox Code Playgroud)
注意:如果*compilation*
在进行第二次编译时窗口仍然可见,则在失败时不会调整大小.如果要调整大小,则需要指定高度而不是nil
.也许这是你喜欢的(改变第一个例子):
(if (string-match-p "finished" resstring) 5 (/ (frame-height) 2))
Run Code Online (Sandbox Code Playgroud)
将nil
被替换(/ (frame-height) 2)