From: Juri Linkov Date: Tue, 5 Apr 2022 18:54:11 +0000 (+0300) Subject: Use base prefix and suffix instead of completion-base-position (bug#49931) X-Git-Tag: emacs-29.0.90~1931^2~759 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=7aaffe25eb178f69027fb0af844a89a86db4b1f2;p=emacs.git Use base prefix and suffix instead of completion-base-position (bug#49931) * lisp/minibuffer.el (minibuffer-completion-help): Set completion-base-affixes from base-prefix and base-suffix. In completion-list-insert-choice-function handle string values of start/end as prefix/suffix. * lisp/simple.el (completion-base-affixes) (completion-use-base-affixes): New variables. (choose-completion): Let-bind base-affixes to completion-base-affixes. Use base-affixes when completion-use-base-affixes is non-nil. (completion-setup-function): Sync values of base-affixes and completion-base-affixes. --- diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el index 5ad44a7a2d8..393555fc62f 100644 --- a/lisp/minibuffer.el +++ b/lisp/minibuffer.el @@ -2284,6 +2284,9 @@ variables.") (let* ((last (last completions)) (base-size (or (cdr last) 0)) (prefix (unless (zerop base-size) (substring string 0 base-size))) + (base-prefix (buffer-substring (minibuffer--completion-prompt-end) + (+ start base-size))) + (base-suffix (buffer-substring (point) (point-max))) (all-md (completion--metadata (buffer-substring-no-properties start (point)) base-size md @@ -2375,20 +2378,28 @@ variables.") ;; completion-all-completions does not give us the ;; necessary information. end)) + (setq-local completion-base-affixes + (list base-prefix base-suffix)) (setq-local completion-list-insert-choice-function (let ((ctable minibuffer-completion-table) (cpred minibuffer-completion-predicate) (cprops completion-extra-properties)) (lambda (start end choice) - (unless (or (zerop (length prefix)) - (equal prefix - (buffer-substring-no-properties - (max (point-min) - (- start (length prefix))) - start))) - (message "*Completions* out of date")) - ;; FIXME: Use `md' to do quoting&terminator here. - (completion--replace start end choice) + (if (and (stringp start) (stringp end)) + (progn + (delete-minibuffer-contents) + (insert start choice) + ;; Keep point after completion before suffix + (save-excursion (insert end))) + (unless (or (zerop (length prefix)) + (equal prefix + (buffer-substring-no-properties + (max (point-min) + (- start (length prefix))) + start))) + (message "*Completions* out of date")) + ;; FIXME: Use `md' to do quoting&terminator here. + (completion--replace start end choice)) (let* ((minibuffer-completion-table ctable) (minibuffer-completion-predicate cpred) (completion-extra-properties cprops) diff --git a/lisp/simple.el b/lisp/simple.el index e49a0ff0f65..5bf1c32e1db 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -9119,6 +9119,16 @@ Its value is a list of the form (START END) where START is the place where the completion should be inserted and END (if non-nil) is the end of the text to replace. If END is nil, point is used instead.") +(defvar completion-base-affixes nil + "Base context of the text corresponding to the shown completions. +This variable is used in the *Completions* buffer. +Its value is a list of the form (PREFIX SUFFIX) where PREFIX is the text +before the place where completion should be inserted, and SUFFIX is the text +after the completion.") + +(defvar completion-use-base-affixes nil + "Non-nil means to restore original prefix and suffix in the minibuffer.") + (defvar completion-list-insert-choice-function #'completion--replace "Function to use to insert the text chosen in *Completions*. Called with three arguments (BEG END TEXT), it should replace the text @@ -9245,6 +9255,7 @@ minibuffer, but don't quit the completions window." (with-current-buffer (window-buffer (posn-window (event-start event))) (let ((buffer completion-reference-buffer) (base-position completion-base-position) + (base-affixes completion-base-affixes) (insert-function completion-list-insert-choice-function) (completion-no-auto-exit (if no-exit t completion-no-auto-exit)) (choice @@ -9270,7 +9281,8 @@ minibuffer, but don't quit the completions window." (with-current-buffer buffer (choose-completion-string choice buffer - (or base-position + (or (and completion-use-base-affixes base-affixes) + base-position ;; If all else fails, just guess. (list (choose-completion-guess-base-position choice))) insert-function))))) @@ -9424,9 +9436,11 @@ Called from `temp-buffer-show-hook'." (buffer-substring (minibuffer-prompt-end) (point))))))) (with-current-buffer standard-output (let ((base-position completion-base-position) + (base-affixes completion-base-affixes) (insert-fun completion-list-insert-choice-function)) (completion-list-mode) (setq-local completion-base-position base-position) + (setq-local completion-base-affixes base-affixes) (setq-local completion-list-insert-choice-function insert-fun)) (setq-local completion-reference-buffer mainbuf) (if base-dir (setq default-directory base-dir))