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?

2 Answers 2

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'.")
2
  • 1
    Perfect!, +1, Unfortunately, my reputation does not allow me to up-vote you. Mar 3, 2022 at 3:35
  • Never mind, glab to see you understand this abstract answer Mar 3, 2022 at 6:52
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.