From 827c17d1645cce8d37a4a65369bea29e36681f3e Mon Sep 17 00:00:00 2001 From: Daniel Mendler Date: Mon, 19 Apr 2021 13:06:54 +0200 Subject: [PATCH 4/6] completion-all-sorted-completions: Respect completion boundaries * lisp/minibuffer.el (completion-all-sorted-completions): When building the hash of history positions drop the prefix as determined by `completion-boundaries`. For file completions drop everything behind the first "/". --- lisp/minibuffer.el | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el index d728c791d3..c7aec9665e 100644 --- a/lisp/minibuffer.el +++ b/lisp/minibuffer.el @@ -1396,14 +1396,36 @@ completion-all-sorted-completions (let* ((hist (symbol-value minibuffer-history-variable)) (hash (make-hash-table :test #'equal :size (length hist))) (index 0) - (def (car-safe minibuffer-default))) - ;; Record history positions in hash - (dolist (c hist) - (unless (gethash c hash) - (puthash c index hash)) - (cl-incf index)) + (def (car-safe minibuffer-default)) + (bounds (completion-boundaries + (substring string 0 (- (point) start)) + minibuffer-completion-table + minibuffer-completion-predicate + "")) + (pre (substring string 0 (car bounds))) + (pre-len (length pre))) + ;; Default comes first. (when (stringp def) - (puthash def -1 hash)) + (setq hist (cons def hist))) + ;; Record history positions in hash + (if (equal "" pre) + (progn + (dolist (c hist) + (unless (gethash c hash) + (puthash c index hash)) + (cl-incf index))) + ;; Remove prefix from history strings, in order to + ;; handle completion boundaries. + (dolist (c hist) + (when (string-prefix-p pre c) + ;; Special handling of file name candidates: + ;; Drop everything after the first / after the prefix. + (let ((pos (and minibuffer-completing-file-name + (string-match-p "/" c pre-len)))) + (setq c (substring c pre-len (and pos (1+ pos))))) + (unless (gethash c hash) + (puthash c index hash))) + (cl-incf index))) ;; Decorate elements with history position (let ((c all)) (while c -- 2.20.1