使`previous-buffer`和`next-buffer`忽略一些缓冲区


10

在Helm中,我在使用时会忽略一些缓冲区C-x b,我正在寻找一种方法来使命令previous-buffernext-buffer忽略某些缓冲区的行为相同。

Answers:


8

有两种方法:

  1. 如果看一下函数的定义switch-to-next-buffer(在library中window.el),您会发现它通过谓词(如果有的话)来过滤缓冲区,谓词是frame参数的值buffer-predicate。该功能由next-buffer

    您可以将该frame参数设置为过滤所需方式的谓词。

  2. 您只需建议函数switch-to-next-buffer即可过滤所需的方式。实际上,您将用几乎相同的定义替换其定义,但是还要按需要过滤。

同样适用于switch-to-previous-buffer


7
(defvar my-skippable-buffers '("*Messages*" "*scratch*" "*Help*")
  "Buffer names ignored by `my-next-buffer' and `my-previous-buffer'.")

(defun my-change-buffer (change-buffer)
  "Call CHANGE-BUFFER until current buffer is not in `my-skippable-buffers'."
  (let ((initial (current-buffer)))
    (funcall change-buffer)
    (let ((first-change (current-buffer)))
      (catch 'loop
        (while (member (buffer-name) my-skippable-buffers)
          (funcall change-buffer)
          (when (eq (current-buffer) first-change)
            (switch-to-buffer initial)
            (throw 'loop t)))))))

(defun my-next-buffer ()
  "Variant of `next-buffer' that skips `my-skippable-buffers'."
  (interactive)
  (my-change-buffer 'next-buffer))

(defun my-previous-buffer ()
  "Variant of `previous-buffer' that skips `my-skippable-buffers'."
  (interactive)
  (my-change-buffer 'previous-buffer))

(global-set-key [remap next-buffer] 'my-next-buffer)
(global-set-key [remap previous-buffer] 'my-previous-buffer)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.