Selectrum: completion-at-point

Created on 7 Apr 2020  路  7Comments  路  Source: raxod502/selectrum

I wonder if it's possible to use selectrum for completion-at-point? It's pretty much "magically" took over all selection inputs, except this one: it shows a new buffer with completion-list-mode.

Most helpful comment

Try this from my (currently private) package ;)

(defun citre-completion-in-region (start end collection &optional predicate)
  "A function that replace default `completion-in-region-function'.
This completes the text between START and END using COLLECTION.
PREDICATE says when to exit.

When there are multiple candidates, this uses standard
`completing-read' interface, while the default one in Emacs pops
a *Completions* buffer to show them.  When combined with some
minibuffer completion framework, it's more user-friendly then the
default one.

Notice when `completing-read-function' is
`completing-read-default' (i.e., not enhanced by a minibuffer
completion framework), this falls back to
`completion--in-region'."
  (if (eq completing-read-function #'completing-read-default)
      (completion--in-region start end collection predicate)
    (let* ((str (buffer-substring-no-properties start end))
           (completion-ignore-case (string= str (downcase str)))
           (candidates
            (nconc
             (completion-all-completions str collection predicate (- end start))
             nil))
           (completion nil))
      (pcase (length candidates)
        (0 (message "No completions"))
        (1 (setq completion (car candidates)))
        (_ (setq completion (completing-read (format "(%s): " str)
                                             candidates predicate t))))
      (when completion
        (delete-region start end)
        (insert (substring-no-properties completion))))))

and set completion-in-region-function to it.

All 7 comments

I agree, that default interface looks pretty icky (I had never used completion-at-point before). Should be not too hard to implement.

Try this from my (currently private) package ;)

(defun citre-completion-in-region (start end collection &optional predicate)
  "A function that replace default `completion-in-region-function'.
This completes the text between START and END using COLLECTION.
PREDICATE says when to exit.

When there are multiple candidates, this uses standard
`completing-read' interface, while the default one in Emacs pops
a *Completions* buffer to show them.  When combined with some
minibuffer completion framework, it's more user-friendly then the
default one.

Notice when `completing-read-function' is
`completing-read-default' (i.e., not enhanced by a minibuffer
completion framework), this falls back to
`completion--in-region'."
  (if (eq completing-read-function #'completing-read-default)
      (completion--in-region start end collection predicate)
    (let* ((str (buffer-substring-no-properties start end))
           (completion-ignore-case (string= str (downcase str)))
           (candidates
            (nconc
             (completion-all-completions str collection predicate (- end start))
             nil))
           (completion nil))
      (pcase (length candidates)
        (0 (message "No completions"))
        (1 (setq completion (car candidates)))
        (_ (setq completion (completing-read (format "(%s): " str)
                                             candidates predicate t))))
      (when completion
        (delete-region start end)
        (insert (substring-no-properties completion))))))

and set completion-in-region-function to it.

@AmaiKinono Would you be alright with my making some amendments and then including a version of your code in Selectrum?

Of course ;) Please do it.

And, I have some suggestions. A completion-at-point-function backend can have an :annotation-function property in its return value. The default completion--in-region uses it to display an annotation for each candidate. I didn't look into how it handles this. And, if you look into the code of elisp-completion-at-point, you'll see it offers some extension properties that's useful for company-capf. I think it would be good if selectrum could make use of :annotation-function and :company-docsig. We can use a prefix and a right-margin text to display them.

I haven't implemented any extensions at this time, but would be happy to see a pull request.

I feel like ya'll were reading my mind. I went through the pain of setting up Company so I could use completions from LSP without the _terrible_ default completion framework...this is all I wanted, to select completions using Selectrum.

Thank you! 馃槏

This thread is being closed automatically by Tidier because it is labeled with "waiting on response" and has not seen any activity for 90 days. But don't worry鈥攊f you have any information that might advance the discussion, leave a comment and I will be happy to reopen the thread :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Luis-Henriquez-Perez picture Luis-Henriquez-Perez  路  11Comments

tinglycraniumplacidly picture tinglycraniumplacidly  路  13Comments

Vurp picture Vurp  路  4Comments

WorldsEndless picture WorldsEndless  路  9Comments

clemera picture clemera  路  5Comments