emacs - 在emacs中选择之前选择的窗口

标签 emacs elisp

我需要一个 emacs 内置函数或 elisp 函数,可以将我带到先前选择的窗口。我认为 (select-window (get-lru-window)) 会这样做,但如果我运行它几次,似乎只是在窗口之间循环而不是在它们之间来回交换,这是我所期望的。

还有其他想法吗?

最佳答案

似乎没有办法在 emacs 中获取最近选择的窗口(与 get-lru-window 返回的最近最少使用的窗口相反)。在内部,emacs 跟踪 windows 上的 use_time,get-lru-window 使用它来查找“最旧”的窗口。但不幸的是,这并没有在 elisp 中公开。

窗口列表按cyclic window ordering 排序这对您的情况没有帮助。

但是,缓冲区列表是按最近使用的缓冲区从最多到最少的顺序排列的(或者不是很严格,有一个 (bury-buffer) 函数可以最后移动缓冲区)。

这意味着,如果您可以将问题转换为“如何切换到最近选择的缓冲区的不同窗口中的缓冲区”之类的问题,那应该是可能的。

一种方法是这样做:

(defun switch-to-previous-buffer-in-a-different-window ()
  (interactive)
  (let* ((otherbuf (other-buffer (current-buffer) t))
     (otherwin (get-buffer-window otherbuf)))
(if otherwin
    (select-window otherwin)
  (message "Last buffer (%s) is not currently visible" (buffer-name otherbuf)))))

或者更短、更有特色的:

(defun switch-to-previous-buffer-possibly-creating-new-window ()
  (interactive)
  (pop-to-buffer (other-buffer (current-buffer) t)))

这里other-buffer用来获取最近使用的buffer(current-buffer除外)。只要您不在窗口中切换缓冲区,这应该可以正常工作,因为 other-buffer 将不再返回另一个窗口中的缓冲区,而是您在当前窗口中切换的缓冲区。

因此,与其使用 other-buffer,不如让我们自己遍历缓冲区列表以找到最佳候选者:

(defun switch-to-the-window-that-displays-the-most-recently-selected-buffer ()
  (interactive)
  (let* ((buflist (buffer-list (selected-frame)))      ; get buffer list in this frames ordered
     (buflist (delq (current-buffer) buflist))     ; if there are multiple windows showing same buffer.
     (winlist (mapcar 'get-buffer-window buflist)) ; buf->win
     (winlist (delq nil winlist))                  ; remove non displayed windows
     (winlist (delq (selected-window) winlist)))   ; remove current-window
(if winlist
    (select-window (car winlist))
  (message "Couldn't find a suitable window to switch to"))))

希望这会有所帮助。

关于emacs - 在emacs中选择之前选择的窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7937395/

相关文章:

emacs - 有没有办法在 Emacs 中自动关闭文件名完成缓冲区?

browser - Emacs 和 eww,在新窗口中打开链接吗?

emacs - 如何通过缩进更改和颜色更改来扩展 emacs lisp 模式

emacs - 格式化 Emacs 函数中的 header 以将缓冲区打印到 PDF(带换行)

python - Emacs 24.3 python : Can't guess python-indent-offset, 使用默认值 4

r - ESS和针织/编织: How to source the Rnw file into an interactive session?

python - Emacs 组织模式 : Executing simple python code

elisp - 如何在elisp中增加局部变量

firefox - 在不关注浏览器的情况下在 emacs 中运行 browse-url

emacs - gud-gdb emacs 24 不工作