reddit / emacs

Trigger when a symbol is inserted OR complete my citations when I insert @

stevebarratt · 2022-03-29 07:48
I suspect this is possible but am having trouble googling what I'm looking for. Can I have emacs trigger a command when I type a specific string in insert mode\*? In my use-case I would like my citations keys to become auto-completed somehow when I type '@'. '@' is the prefix for citation and references in pandocs markdown. Citation look like '@Nurmagomedov2020' for example, and they are stored in a bibtex file. I currently search and add citations by calling `citar-insert-citation`, which provides an interface to search my bibtex file and prefix the '@' symbol so that is recognised by pandoc, when I create a document. Using completion plugins clearly it is possible for typed text to trigger a function (the completion). Is there a way I can make this work for my citation entry? Alternatively, is there an alternative approach for my specific use case that someone has already implemented? Any guidance appreciated, even just a link in the right direction. \*(I'm `evil`)
BettaGeorge · 2022-03-29 08:55
Is there a specific reason you do not just want to bind something like M-@ to "insert an @ and then do the citation search thingy"?
meain · 2022-03-29 09:20
I use aas (auto-activating-snippets) to do something similar. https://github.com/ymarco/auto-activating-snippets You can map @(or any key sequence in insert mode) to insert @ and then call the completing function.
stevebarratt · 2022-03-29 09:42
Yes, the other way (which I'm asking about) is my personal preference. Another editor I used years ago worked this way and it felt very natural, and less disruptive to writing flow. What I'm doing right now is very close to what you're saying, it works.. just feels janky having experienced a 'better way' before.
bespokey · 2022-03-29 11:55
You can bind "@" to completing read, vertico is integrated a with citar and can provide candidates. If you don't use vertico / embark then you can advise around self-insert-command and invoke your preferred completion system or citar's completion functions.
mclearc · 2022-03-29 13:26
https://github.com/gbgar/company-bibtex seems like what you want.
xenodium · 2022-03-29 14:25
I’m no company expert, but I managed to do it with < in https://github.com/xenodium/company-org-block (gifs w/ demos included in link)
BettaGeorge · 2022-03-29 16:01
That sounds great! I am bookmarking that.
BettaGeorge · 2022-03-29 16:02
The aas looks good. Good luck!
oantolin · 2022-03-29 21:11
I'm a little confused about the fine details of what you want to do, but does binding `@` to `citar-insert-citation` do what you want?
stevebarratt · 2022-03-31 09:35
I'm using vertico, what do you mean completing read? This sounds cool, thank you.
stevebarratt · 2022-03-31 09:35
Thanks! yes I found that actually after I posted this. Didn't get a chance to try it yet, I was gonna see what else was suggested, first.
stevebarratt · 2022-03-31 09:37
I would rather retain optional use of @ if I really need it, rather than it trigger this always. Also, I'm not super sure how this will play with evils insert mode.
nandryshak · 2022-03-31 11:55
How does this differ from abbrev mode?
oantolin · 2022-03-31 13:07
You can use quoted-insert to insert an @, I don't know what evil binds that too, but it's bound by default to C-q.
meain · 2022-03-31 15:56
Abbrev mode needs an extra key like a space at the end. This does not need that.
oldjawbone · 2022-04-01 14:59
If I understand your request, it is similar to something I've worked up for other applications. Does the following do what you have in mind? (defun citation-key-completion-at-point () "Completion-at-point function for citation-keys." (let ((keys (hash-table-keys (parsebib-parse citar-bibliography))) (candidates) (case-fold-search t) (pt (point))) (dolist (key keys) (push (format "@%s" key) candidates)) (save-excursion (when (re-search-backward "@.?" nil t) (list (match-beginning 0) pt candidates :exclusive 'no))))) (add-to-list 'completion-at-point-functions 'citation-key-completion-at-point) [Demo gif!](https://imgur.com/a/ddzjcp0)
bdarcus · 2022-05-02 14:07
I would think it would be better to use the citar completion table, or something similar, so that one completes against the author, title, etc., and inserts the key?
oldjawbone · 2022-05-02 14:32
You mean something like this, for obtaining a list keys? (mapcar (lambda (x) (nth 1 x)) citar--candidates-cache) Or maybe there is a better way?
bdarcus · 2022-05-02 14:34
I'm not sure myself on the details, but rather than accessing the cache directly, I'd use the `citar--get-candidates` function. Maybe we should add this to the citar wiki.