有时我会为同一个缓冲区(或类似的缓冲区)打开多个窗口,并且在决定终止它或删除窗口之前,我必须区分窗口中的缓冲区是否与另一个相同。
仅当缓冲区已存在于另一个窗口中时,emacs 中是否有办法简单地删除一个窗口?理想情况下,如果它是窗口中缓冲区的唯一实例,我希望同样的函数也杀死缓冲区和窗口。
(defun delete-extra-windows ()
(interactive)
(let* ((selwin (selected-window))
(buf (window-buffer selwin)))
(walk-windows (lambda (ww)
(unless (eq ww selwin)
(when (eq (window-buffer ww) buf)
(delete-window ww))))
'NO-MINI 'THIS-FRAME)))
我在15 年前添加quit-window
(通常绑定到q非自插入 - AKA - 缓冲区)以解决类似问题。special
你可以试试它或它的兄弟姐妹quit-windows-on
。
您对所需内容的说明尚不清楚。您说“仅当缓冲区已存在于另一个窗口中时才删除一个窗口”。这意味着如果缓冲区不存在于另一个窗口中,则不要删除该窗口。然而,您还说“如果缓冲区和窗口是窗口中缓冲区的唯一实例,则终止缓冲区和窗口”,这与第一个要求相矛盾。
我猜“仅在...时删除窗口”实际上是指“仅在...时删除窗口(而不是缓冲区)”。
(defun delete-window-maybe-kill-buffer ()
"Delete selected window.
If no other window shows its buffer, kill the buffer too."
(interactive)
(let* ((selwin (selected-window))
(buf (window-buffer selwin)))
(delete-window selwin)
(unless (get-buffer-window buf 'visible) (kill-buffer buf))))
这是我一直在寻找的行为。感谢您对基本功能布局和逻辑的帮助。使用 Elisp 仍然非常令人困惑,我很感激能帮助您度过难关。
请参阅解释该行为的代码部分。您还应该能够直接从源头上理解它。
我已经对您之前的答案投了赞成票,其中包括我使用的大部分代码。
;;; Delete the selected window without killing the buffer if the buffer is open
;;; in another; otherwise close the window and its buffer. If called on the
;;; last visible window then the buffer will simply be killed and replaced by
;;; the next available buffer.
(defun delete-window-maybe-kill-buffer ()
"Delete selected window.
If no other window shows its buffer, kill the buffer too."
(interactive)
(let* ((selwin (selected-window))
(buf (window-buffer selwin)))
(if (> (length (window-list)) 1)
(delete-window selwin)
(unless (get-buffer-window buf 'visible) (kill-buffer buf))
(kill-buffer buf))))