Automatically narrow consult buffer for certain major-modes #1072
-
Hi there, I have some custom sources: ;; Build an rcirc-specific consult source
(defun consult-rcirc--buffer-list ()
"Return the list of current rcirc buffers."
(let ((rcirc-buffers
(cl-remove-if-not
(lambda (buffer)
(with-current-buffer buffer
(eq major-mode 'rcirc-mode)))
(buffer-list))))
(cl-remove-duplicates rcirc-buffers :test #'equal)))
(defvar rcirc-buffer-source
`(:name "All IRC buffers"
:hidden nil
:narrow ?c
:face consult-buffer
:category rcirc-buffer
:history buffer-name-history
:state ,#'consult--buffer-state
:annotate ,#'rcirc-annotate-buffer
;; TODO: Sort by unread, then in alphabetic order
:items ,(lambda () (mapcar #'buffer-name (consult-rcirc--buffer-list)))))
(defvar rcirc-unread-buffer-source
`(:name "Unread IRC buffers"
:hidden nil
:narrow ?C
:face doom-modeline-notification ;; Nice red face for unread messages
:category rcirc-buffer
:history buffer-name-history
:state ,#'consult--buffer-state
:annotate ,#'rcirc-annotate-buffer
:items ,(lambda () (when (bound-and-true-p rcirc-activity)
(mapcar #'buffer-name rcirc-activity)))))
(defun rcirc-annotate-buffer (cand)
"Annotate the rcirc buffer CAND with its type, server, and unread status."
;; Find the buffer associate with the candidate
(let* ((cand-buffer (seq-find (lambda (item)
(eq (buffer-name item) cand))
(consult-rcirc--buffer-list)))
;; Find out if it's a channel or server buffer
(buf-type (if (string-match-p "^#" cand)
(propertize "Channel" 'face 'consult-buffer)
(propertize "Server" 'face 'rcirc-server)))
(unread (if (member cand (mapcar #'buffer-name rcirc-activity))
(propertize "Unread" 'face 'doom-modeline-notification)
""))
(server (if (string-equal buf-type "Channel")
(with-current-buffer cand-buffer
(when rcirc-server-buffer
;; If rcirc-color is available, use it to
;; colourise the server column. Otherwise,
;; just return it plainly.
(if (functionp 'rcirc-color-make-face)
(propertize
(buffer-name rcirc-server-buffer)
'face (rcirc-color-make-face (buffer-name rcirc-server-buffer)))
(buffer-name rcirc-server-buffer))))
"")))
;; Return whether there's activity in this buffer or not
(format "%5s\t%16s\t%16s" buf-type server `unread)))`
(add-to-list 'consult-buffer-sources 'rcirc-buffer-source 'append)
(add-to-list 'consult-buffer-sources 'rcirc-unread-buffer-source 'append))
And I can narrow to them by using |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
With a little bit of help from others on Mastodon, I was able to come up with this: (defvar consult-initial-narrow-per-mode-config
'((rcirc-mode . ((consult-buffer . ?c)))))
(defun consult-initial-narrow ()
"Narrow consult buffers differently for different major modes.
Allows consult to have initial narrowing for configurable buffer types
and consult command types, contained in
`consult-initial-narrow-per-mode-config'."
(when minibuffer--original-buffer
(when-let* ((original-mode (with-current-buffer minibuffer--original-buffer major-mode))
(mode-config (alist-get original-mode consult-initial-narrow-per-mode-config))
(command-prefix (alist-get this-command mode-config)))
(setq-local unread-command-events (append unread-command-events (list command-prefix 32))))))
(add-hook 'minibuffer-setup-hook #'consult-initial-narrow)) Which does exactly what I'm looking for. I'll add it to the wiki and mark this discussion as resolved. |
Beta Was this translation helpful? Give feedback.
With a little bit of help from others on Mastodon, I was able to come up with this: