以编程方式更改emacs窗口大小

6

我希望实现编译缓冲区自动折叠为小尺寸(但不会在删除窗口时关闭),这样,在成功编译后,窗口会缩小到最小尺寸。

get-buffer-create 返回一个缓冲区。如何在与该缓冲区关联的窗口上使用 shrink-window?还有一种方法可以存储先前的窗口大小吗?

这是我第一次尝试emacs lisp编程,感谢您的帮助。

1个回答

8
我相信有两种方法可以解决这个问题。
第一种方法是使用钩子`'compilation-finish-functions',它是:

当编译过程完成时调用的函数列表。 每个函数都带有两个参数:编译缓冲区和描述进程如何完成的字符串。

这导致一个像这样的解决方案:
(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))))

我对这种解决方案唯一的问题是它假定成功可以通过在结果字符串中找到字符串“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))

注意:如果在第二次编译时仍然可见*compilation*窗口,则在失败时它不会被调整为更大的尺寸。如果您想要调整大小,您需要指定一个高度而不是nil。也许这会符合您的喜好(更改第一个示例):
(if (string-match-p "finished" resstring) 5 (/ (frame-height) 2))

"

nil

"被替换为"(/ (frame-height) 2)"。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接