【问题标题】:How to customize Emacs split-window-X with the new window showing the next buffer?如何使用显示下一个缓冲区的新窗口自定义 Emacs split-window-X?
【发布时间】:2013-10-10 15:03:23
【问题描述】:

在 Emacs 21.x 中,我不知道是通过对拆分窗口的特定自定义还是由于 Emacs 的不同默认行为,除了拆分窗口之外还调用了以下拆分窗口,它在非焦点窗口到下一个缓冲区。

目前(Emacs 24.x),split-window 和兄弟姐妹 split-window-below 和 split-window-right 似乎不允许这样的自定义。这是真的吗?

如果是这样,如何调整 Emacs 以使其具有这种行为?重新定义 split-window 或 split-window-below 和 split-window-right 以在非聚焦窗口上切换到下一个的额外步骤。这可以通过建议来完成:

(defun split-window-and-next-buffer (new-window)
  (let ((old-window (selected-window)))
    (select-window new-window)
    (next-buffer)
    (select-window old-window)
    new-window))

(defadvice split-window-right (after split-window-right-and-next-buffer
                     activate protect compile)
  (split-window-and-next-buffer ad-return-value))

(defadvice split-window-below (after split-window-bellow-and-next-buffer
                      activate protect compile)
  (split-window-and-next-buffer ad-return-value))

根据 lawlist 指示的更正,这些建议已经在上面提供,并且我得到了预期的行为,但是不能自定义旧的行为。

【问题讨论】:

  • 在几天前构建的当前版本的 Emacs Trunk 中,split-window-below 垂直拆分窗口,两个窗口中出现相同的缓冲区 -- Split the selected window into two windows, one above the other. The selected window is above. The newly split-off window is below, and displays the same buffer. Return the new window.。顺便说一句——你拼错了几次below
  • @lawlist,感谢您的回答和指出错误。我已经更新了问题,现在我的基于建议的简单解决方案已经奏效。

标签: emacs split window buffer


【解决方案1】:

在回答这个问题时,原始发帖人可能想尝试在发布的代码中更改单词below 的拼写。


此函数在当前版本的 Emacs Trunk split-window-below 中添加三行代码(在末尾),并将函数重命名为 lawlist-split-window-below 并带有 defalias。一个右括号被移到函数的末尾,以允许使用在函数中更远位置定义的两个let 绑定。如果用户想要将焦点放在new-window(退出函数后),那么只需删除最后一行代码(select-window old-window)

(defun lawlist-split-window-below (&optional size)
  "Split the selected window into two windows, one above the other.
The selected window is above.  The newly split-off window is
below, and displays the 'next-buffer'.  Return the new window.

If optional argument SIZE is omitted or nil, both windows get the
same height, or close to it.  If SIZE is positive, the upper
\(selected) window gets SIZE lines.  If SIZE is negative, the
lower (new) window gets -SIZE lines.

If the variable `split-window-keep-point' is non-nil, both
windows get the same value of point as the selected window.
Otherwise, the window starts are chosen so as to minimize the
amount of redisplay; this is convenient on slow terminals."
  (interactive "P")
  (let ((old-window (selected-window))
    (old-point (window-point))
    (size (and size (prefix-numeric-value size)))
        moved-by-window-height moved new-window bottom)
    (when (and size (< size 0) (< (- size) window-min-height))
      ;; `split-window' would not signal an error here.
      (error "Size of new window too small"))
    (setq new-window (split-window nil size))
    (unless split-window-keep-point
      (with-current-buffer (window-buffer)
    ;; Use `save-excursion' around vertical movements below
    ;; (Bug#10971).  Note: When the selected window's buffer has a
    ;; header line, up to two lines of the buffer may not show up
    ;; in the resulting configuration.
    (save-excursion
      (goto-char (window-start))
      (setq moved (vertical-motion (window-height)))
      (set-window-start new-window (point))
      (when (> (point) (window-point new-window))
        (set-window-point new-window (point)))
      (when (= moved (window-height))
        (setq moved-by-window-height t)
        (vertical-motion -1))
      (setq bottom (point)))
    (and moved-by-window-height
         (<= bottom (point))
         (set-window-point old-window (1- bottom)))
    (and moved-by-window-height
         (<= (window-start new-window) old-point)
         (set-window-point new-window old-point)
         (select-window new-window)))
    ;; Always copy quit-restore parameter in interactive use.
    (let ((quit-restore (window-parameter old-window 'quit-restore)))
      (when quit-restore
    (set-window-parameter new-window 'quit-restore quit-restore)))
    new-window)
  (select-window new-window)
  (next-buffer)
  (select-window old-window)))

(defalias 'split-window-below 'lawlist-split-window-below)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多