0

I am currently an ivy user, trying out vertico + consult.

For ivy I am using the following

(defun rit-check-buffer-mode (str mode)
  "Check whether the buffer's major mode is `mode'."
  (let ((buf (get-buffer str)))
    (and buf (eq (buffer-local-value 'major-mode buf) mode))))

(defun rit-ivy-switch-buffer (ignore-list)
  (let ((ivy-ignore-buffers ignore-list))
    (ivy-switch-buffer)))

(defun rit-ivy-buffer-switch-file ()
  "Switch to any non-dired and non-virtual buffer."
  (interactive)
  (rit-ivy-switch-buffer '((lambda (str)
                 (or
                  (string-match-p "^ " str)
                  (string-match-p "^\\*" str)
                  (rit-check-buffer-mode str 'dired-mode))))))

How to achieve the same in vertico + consult?

Drew
  • 75,699
  • 9
  • 109
  • 225

2 Answers2

0

For buffers that don't begin with * or a space char you can modify consult-buffer-filter.

For non-Dired buffers, consult--buffer-query accepts a predicate argument.

So you can define you own consult buffer source like this:

(defvar consult--source-non-virtual-non-dired-buffer
  `(:name     "Buffer"
              :narrow   ?b
              :category buffer
              :face     consult-buffer
              :history  buffer-name-history
              :state    ,#'consult--buffer-state
              :default  t
              :items
              ,(lambda () (consult--buffer-query :sort 'visibility
                                            :exclude '("^ "
                                                       "^\\*")
                                            :predicate #'function-to-filte-dired-buffer
                                            :as #'buffer-name)))
  "Buffer candidate source for `consult-buffer'.")
Drew
  • 75,699
  • 9
  • 109
  • 225
Tianshu Wang
  • 1,724
  • 4
  • 7
0

I find another way to do this without any additional package.

(defun rit-check-buffer-mode (str mode)
  "Check whether the buffer's major mode is `mode'."
  (let ((buf (get-buffer str)))
    (and buf (eq (buffer-local-value 'major-mode buf) mode))))


(defun rit-switch-to-file-buffer ()
  "Switch to non-virtual and non-dired buffer."
  (interactive)
  (switch-to-buffer (read-buffer "Buffer: " nil t #'(lambda (arg)
                                                      (let ((str (car arg)))
                                                        (not (or
                                                          (string-match-p "^ " str)
                                                          (string-match-p "^\\*" str)
                                                          (rit-check-buffer-mode str 'dired-mode))))))))

This way one can use any package that hooks compliting-read, like vertico, ivy etc.