【分享】为`switch-to-buffer`提供过滤buffer的功能

起因是在Windows下使用consult包特别是其预览功能感觉有一点点卡,另外consult-buffer对bookmark、recent file等类型“buffer”的聚合对我而言有点繁杂。最近把consultdrop掉了,但是切换buffer时具备的过滤功能还是很有必要的,所以对switch-to-buffer进行了配置。

 (defvar buffer:skip-regexp
    (rx bos
        ?*
        (or
         "Messages"
         "Output"
         "Compile-Log"
         "Completions"
         "Flymake log"
         "Warnings"
         "Flymake diagnostics"
         "Async Shell Command"
         "Async-native-compile-log"
         "Native-compile-Log"
         "Apropos"
         "Backtrace"
         "prodigy"
         "help"
         )
        (* anything)
        ?*
        eos))
  (advice-add 'read-buffer-to-switch :around #'buffer:skip-read-buffer-to-switch)
  (defun buffer:skip-read-buffer-to-switch (fn &rest args)
    ;; 避免 `other-buffer' 选取需要忽略的buffer
    (unless (frame-parameter nil 'buffer-predicate)
      (set-frame-parameter nil 'buffer-predicate
                           (lambda (buffer)
                             (not (string-match-p buffer:skip-regexp
                                                  (buffer-name buffer))))))
    (minibuffer-with-setup-hook
        (lambda ()
          ;; 将需要忽略的buffer从 `minibuffer-completion-table' 中过滤
          (setq-local minibuffer-completion-predicate
                      (lambda (name)
                        (not (string-match-p buffer:skip-regexp
                                             (if (consp name) (car name) name))))))
      (apply fn args)))

目前用了下好像还行,就是不知道有没有其它副作用,特别是设置了buffer-predicate这个frame参数。

1 个赞

我換 buffer 的方式應該是最奇怪的那一類… :sweat_smile:

19-09-00

我是用 buffer menu 加自己的過濾器.

1 个赞

关掉consult的preview功能就好了呀

(setq consult-preview-key nil)
3 个赞