[This is continuation of the unfinished fix from bug#34517] >> Stefan, please advise shouldn't selecting a completion from the >> *Completions* buffer clear the minibuffer's content before >> inserting the selected completion? > > No, for example when you complete file name "C-x C-f ~/.e TAB" the > *Completions* buffer will only show ".emacs" so we should clear the > minibuffer before inserting ".emacs" because that would lose the leading > "~/". There are other circumstances where trailing text needs to > be preserved. Another example that occurred to me is shell-command M-! minibuffer where TAB can complete on commands and file names. > The completion code handles this with `completion-base-position` which > holds the beginning and end of the text that should be replaced when you > choose an item in *Completions*. > >>> 0. emacs -Q >>> 1. ‘C-h f TAB’ displays a list of completions >>> 2. type a nonexistent function name, i.e. some random text >>> in the minibuffer, e.g. “blabla” > > The *Completions* content is now "out of date" compared to the minibuffer. > >>> 3. click on an existing valid completion in the *Completions* buffer, >>> e.g. on “append” > > completion-base-position was set at step (1) to cover the empty text > after the prompt, so this empty text (which is now right in front of > "blabla") is replaced with "append" resulting in "appendblabla". > > Obviously, the result is not what we want. > Now sure how to change which part, tho. Maybe instead of > completion-base-position we should store the prefix and suffix strings, > so when you select an entry from *Completions* we just clear the > minibuffer and replace it with (concat prefix selection suffix)? Now I tried this, and it works correctly. But not sure how to make this change as backward-compatible as possible. One variant would be to save '("prefix" "suffix") instead of '(10 11) in 'completion-base-position' but this might fail in some existing code. So maybe better to add a new variable 'completion-base-affixes'. Then whether to use 'completion-base-position' or 'completion-base-affixes' could be defined by the new user option 'completion-use-base-affixes'. Then it can be used in the new command from emacs-devel: ``` (defun minibuffer-completion-choose (&optional arg) "Run `choose-completion' from the minibuffer in its scrolling window." (interactive "P") (with-minibuffer-scroll-window (let ((completion-use-base-affixes t)) (choose-completion nil arg)))) ```