reddit / emacs
Auto-complete previews while typing w/ plug-able back-ends?
ideasman_42 · 2022-09-19 11:38
Currently I'm using [`fancy-dabbrev`](https://github.com/jrosdahl/fancy-dabbrev) and quite like it's behavior (aside from an [issue with macros](https://github.com/jrosdahl/fancy-dabbrev/pull/18#issuecomment-1250267761)).
For anyone who hasn't used it - it can preview completion while typing, pressing TAB can execute the completion or indent - based on the context.
----
While this works well sometimes I'd like to use completion besides `dabbrev`, configurable based on the context (editing code/comments/strings ... etc).
Ideally there would be a way to use context sensitive compilation back-ends which I could write & register.
At this point I'm considering writing my own package but thought there might be some solutions out there.
Key features I'm looking for:
- Preview completion while typing.
- Ability to define my own completion functions.
- Non-blocking interface, a single keystroke to accept and keep typing.
Does anything like this already exist?
nandryshak · 2022-09-19 13:15
company-mode
__nautilus__ · 2022-09-19 13:16
company?
Fastest_draw · 2022-09-19 13:55
Another besides company is corfu
eleven_cupfuls · 2022-09-19 16:52
> Ability to define my own completion functions.
This is built in to Emacs, though admittedly with an API on the more intricate side. The feature generally is called "completion at point", and the relevant setting is `completion-at-point-functions`. https://with-emacs.com/posts/tutorials/customize-completion-at-point/ https://www.gnu.org/software/emacs/manual/html_node/elisp/Completion-in-Buffers.html
In the simplest case you can just supply a "completion table", a list of strings, such as keywords:
(defun my-vegetables/completion-table ()
(let ((bounds (bounds-of-thing-at-point 'symbol)))
(list (car bounds)
(cdr bounds)
'("artichoke"
"arugula"
"beet"
"broccoli"
"carrot"
"cauliflower"))))
(add-to-list 'completion-at-point-functions #'my-vegetables/completion-table)
This is the "backend", and when you trigger completion at point that table will be consulted for suggestions to complete.
The frontend is also variable. By default I believe the minibuffer is used. Company is a very popular alternative, with a few different options for the display (it also provides its own backends on top of the CAPFs). Corfu is another, that uses a child frame popup. Ivy also has its own frontend(s), and I'm sure Helm does too.
ideasman_42 · 2022-09-19 23:45
The screen-shots of this only show a large popup menu which I find too disruptive for general use - is there the ability to show text after the word: [like this](https://github.com/jrosdahl/fancy-dabbrev/blob/master/images/fancy-dabbrev-1.png)?
nandryshak · 2022-09-19 23:47
Yes, that's called a "preview" in the context of completion frameworks. See the "preview frontends" section of the company-mode manual.
Also company's default popup menu is quite small relative to others and can be shrunken or disabled with configuration.
ideasman_42 · 2022-09-19 23:48
Are you aware of any packages/existing-functionality to "preview" the result of `completion-at-point` while typing?
ideasman_42 · 2022-09-20 00:11
None of the preview front-ends seem to work like fancy-dabbrev.
Checking:
http://company-mode.github.io/manual/Frontends.html#Preview-Frontends I just want to see some text after what I'm typing (the fish shell also does this). It seems something close to this is supported, but when there are multiple options - those show too in a popup.
Further, this is blocking, I can't just edit as normal (the key-map for the popup kicks in and pressing Return no longer moves to the next line).
Unless I'm missing something, I can write my own preview front-end, and then clear the key-map and make the TAB key (for example) execute the completion. But this gets quite awkward as I already use company-mode with manually activated popup, having the changes to the behavior when viewing changes will likely impact the popup that I manually activate.
nandryshak · 2022-09-20 00:30
I think I understand what you're looking for, I use a plugin for zsh that emulates the fish behavior. I think all you have to do is investigate this a little further and do some configuration. Try to configure the company frontends to disable the tooltips and enable the previews, then replace company-mode-map with a new map that only maps TAB to company-complete.
ideasman_42 · 2022-09-20 01:44
Right, the difference is not great in some sense but the details combine to something which works fairly differently.
As it happens I want different:
- back-ends.
- preview.
- keymap.
.. for preview while typing, this may seem like a strange thing to want different behavior, in practice I find `dabbrev` nice to be able to complete text that lsp/code auto-complete might miss for - whatever reason, also typing in comments it's nice to complete with known words.
The one thing I'm not sure how to solve is using a different key-map, Emacs doesn't seem to support dynamically changing key-maps. Doing this will interfere with macro recording and replaying. So I might end up writing a new packages as it looks like less hassle than configuring company mode to do something which it's not especially aimed at doing.
nandryshak · 2022-09-20 01:52
> The one thing I'm not sure how to solve is using a different key-map
(setq company-active-map (make-sparse-keymap))
Will make a blank keymap. So after requiring company mode, do something like this:
(let ((map (make-sparse-keymap)))
(define-key map (kbd "TAB") 'company-complete)
(setq company-active-map map))
akirakom · 2022-09-20 03:29
Pyim, a Chinese input method, has preview: https://github.com/tumashu/pyim/blob/master/pyim-preview.el. it's not pluggable though.
[deleted] · 2022-09-20 07:04
[deleted]
jangid · 2022-09-20 08:49
Corfu, company
ideasman_42 · 2022-09-20 11:38
Do either of them support:
- Non blocking popups?
- Previews [like this](https://github.com/jrosdahl/fancy-dabbrev/blob/master/images/fancy-dabbrev-1.png)?
My impression is I would need to do some heavier configuration that approaches the complexity of writing my own package from scratch.
mickeyp · 2022-09-20 15:09
Pretty sure company-mode does this. I *know* the old-school auto-complete package did.
eleven_cupfuls · 2022-09-20 19:25
I did think that corfu did this with certain settings, but apparently not, since [u/minad has just started a draft for it](https://github.com/minad/corfu/pull/232)! :D