Skip to content

Commit

Permalink
Use (cons 'buffer . buffer-object) as multi-category for candidates (#…
Browse files Browse the repository at this point in the history
…980)

This fixes the problem where buffers are renamed during completion.
Renaming can happen during buffer name uniquify or due to EXWM
renaming. See the issues #978 and #979.

Consult buffer preview magically continues to work since `get-buffer`
accepts both string and buffer arguments. I haven't tested Embark yet.
  • Loading branch information
minad authored Mar 23, 2024
1 parent e222aac commit ec232fa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.org
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
#+author: Daniel Mendler
#+language: en

* Development

- Bugfix: Handle buffer renaming during minibuffer completion gracefully, by
attaching the actual buffer objects to the completion candidate strings.

* Version 1.4 (2024-03-08)

- Bugfix: File preview: Ensure that binary files are not previewed partially.
Expand Down
2 changes: 1 addition & 1 deletion README.org
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ ones.
(consult--buffer-action (current-buffer))))
:items
(lambda ()
(consult--buffer-query :mode 'org-mode :as #'buffer-name))))
(consult--buffer-query :mode 'org-mode :as #'consult--buffer-pair))))

(add-to-list 'consult-buffer-sources 'org-source 'append)
#+end_src
Expand Down
24 changes: 15 additions & 9 deletions consult.el
Original file line number Diff line number Diff line change
Expand Up @@ -2808,13 +2808,15 @@ KEYMAP is a command-specific keymap."
(items (plist-get src :items))
(items (if (functionp items) (funcall items) items)))
(dolist (item items)
(let ((cand (consult--tofu-append item idx)))
(let* ((str (or (car-safe item) item))
(cand (consult--tofu-append str idx)))
;; Preserve existing `multi-category' datum of the candidate.
(if (get-text-property 0 'multi-category cand)
(when face (add-text-properties 0 (length item) face cand))
(if (and (eq str item) (get-text-property 0 'multi-category str))
(when face (add-text-properties 0 (length str) face cand))
;; Attach `multi-category' datum and face.
(add-text-properties 0 (length item)
`(multi-category (,cat . ,item) ,@face) cand))
(add-text-properties
0 (length str)
`(multi-category (,cat . ,(or (cdr-safe item) item)) ,@face) cand))
(push cand candidates))))
(cl-incf idx))
(nreverse candidates)))
Expand Down Expand Up @@ -4433,6 +4435,10 @@ AS is a conversion function."
"Return hash table of all buffer file names."
(consult--string-hash (consult--buffer-query :as #'buffer-file-name)))

(defun consult--buffer-pair (buffer)
"Return a pair of name of BUFFER and BUFFER."
(cons (buffer-name buffer) buffer))

(defun consult--buffer-preview ()
"Buffer preview function."
(let ((orig-buf (window-buffer (consult--original-window)))
Expand Down Expand Up @@ -4488,7 +4494,7 @@ If NORECORD is non-nil, do not record the buffer switch in the buffer list."
(when-let (root (consult--project-root))
(consult--buffer-query :sort 'visibility
:directory root
:as #'buffer-name))))
:as #'consult--buffer-pair))))
"Project buffer candidate source for `consult-buffer'.")

(defvar consult--source-project-recent-file
Expand Down Expand Up @@ -4545,7 +4551,7 @@ If NORECORD is non-nil, do not record the buffer switch in the buffer list."
:items
,(lambda () (consult--buffer-query :sort 'visibility
:filter 'invert
:as #'buffer-name)))
:as #'consult--buffer-pair)))
"Hidden buffer candidate source for `consult-buffer'.")

(defvar consult--source-modified-buffer
Expand All @@ -4558,7 +4564,7 @@ If NORECORD is non-nil, do not record the buffer switch in the buffer list."
:state ,#'consult--buffer-state
:items
,(lambda () (consult--buffer-query :sort 'visibility
:as #'buffer-name
:as #'consult--buffer-pair
:predicate
(lambda (buf)
(and (buffer-modified-p buf)
Expand All @@ -4575,7 +4581,7 @@ If NORECORD is non-nil, do not record the buffer switch in the buffer list."
:default t
:items
,(lambda () (consult--buffer-query :sort 'visibility
:as #'buffer-name)))
:as #'consult--buffer-pair)))
"Buffer candidate source for `consult-buffer'.")

(defun consult--file-register-p (reg)
Expand Down

0 comments on commit ec232fa

Please sign in to comment.