reddit / emacs

Custom company backends: different insert value to selection value

codethrasher · 2022-07-31 22:19
Hey all! I'm attempting to write my own custom backend which reads from an org file, grabs all the top-level headings and displays them in the company-mode popup as selection candidates. I have this working pretty well, but I was wondering if it was possible to (once the candidate has been selected) have the insert text (what actually gets placed _into_ the buffer) changed or otherwise manipulated in some other way so that the value is different from the selection candidate. I was under the impression `post-completion` would allow this, but it doesn't seem to work. Here's my `defun`: (defun company-sample-backend (command &optional arg &rest ignored) (interactive (list 'interactive)) (let ((people (org-ql-select (concat org-dir "/contacts.org") '(level 1) :action #'org-get-heading))) (cl-case command (interactive (company-begin-backend 'company-sample-backend)) (prefix (and (eq major-mode 'org-mode) (company-grab-symbol))) (candidates (my/contacts-candidates arg)) (post-completion (company-thing arg)))))
cottasteel · 2022-09-02 15:53
I've been experimenting with custom company backends as well. The `arg` supplied to each command in a custom company backend is specific to the command. so for `candidates`, `arg` is the prefix argument to be completed. From the documentation for `company-backends`: `post-completion': Called after a completion candidate has been inserted into the buffer. The second argument is the candidate. Can be used to modify it, e.g. to expand a snippet. I made my post-completion code be something like `(message "Arg=%s" arg)` so that I could see what was being sent to the `post-completion` command. Once I understood that , the challenge for me was that when `post-completion` is invoked, I no longer have access to the prefix argument, so I made my `candidates` command save the prefix to a global variable. I hope this helps.