如何使用显示下一个缓冲区的新窗口自定义Emacs split-window-X?

时间:2013-10-10 15:03:23

标签: emacs split window buffer

在Emacs 21.x中我不知道是否通过特定的分割窗口定制或由于Emacs的不同默认行为,除了拆分窗口之外调用拆分窗口,它还切换了非聚焦窗口到下一个缓冲区。

目前(Emacs 24.x),拆分窗口和兄弟姐妹拆分窗口在下方和拆分窗口右侧似乎不允许这样的自定义。这是真的吗?

如果是这样,如何调整Emacs以获得此行为?重新定义分割窗口或分割窗口 - 下方和分割窗口右侧,以便在非聚焦窗口上切换到下一个窗口的额外步骤。这可以通过建议完成:

(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))

根据法律清单所显示的更正已经在建议之上已经可用并且我得到了预期的行为,但是不能自定义旧的行为。

1 个答案:

答案 0 :(得分:3)

在回答问题时,原始海报可能需要尝试在发布的代码中更改单词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)