reddit / emacs

Ability to search/pull elisp snippets from within emacs?

Lucky_Mike_D · 2022-05-10 19:18
I've been using emacs for several years now (coming from vim) and I've found that understanding the basics of elisp can be very helpful, and essential whenever I want to do any customization. Typically I will google what I am trying to do, come across some posts on reddit or stackoverflow, find some sample code (ie. This is the function I wrote and bound to this key to do x and y), and incorporate that into my own emacs config. Here's a simple example, say I want to figure out how to copy the full path of the current filename into the buffer so I can later make a reference to it in my org notes. If I google that, I'll see a dozen different ways to do this and they mostly all work great, but in the spirit of doing everything inside emacs I can't help but wonder... there must be a better way? I feel like there must be some repository of very useful and common elisp code "snippets" where I could just search for "get current filename" and be presented with elisp code I can immediately use, and I feel like that could all be done directly inside my current document and just inserted as a code snippet!? Is something like this at all possible, or are there any useful tools/packages that do something similar? Appreciate any tips :-)
emoarmy · 2022-05-10 23:01
Like most things with Emacs, I don't think there is an official repo of coding patterns. But you could look at what others do for Yasnippets? https://github.com/AndreaCrotti/yasnippet-snippets/tree/master/snippets/emacs-lisp-mode The emacs package developer handbook may have some tidbits of information for you: https://github.com/alphapapa/emacs-package-dev-handbook Or you could search for function name or docstring that contain some keyword of what you want to do. `C-h f` for `describe function` and `C-h d` for `apropos documentation`. Or you could search the elisp manual for tips and tricks `C-h R elisp <return>`
redguardtoo · 2022-05-10 23:58
You can start by reading books ("Writing GNU Emacs Extensions") and reading packages' code (company-mode, counsel/ivy, wgrep-mode, ...). Finalyy, you can create your own package.
rswgnu · 2022-05-11 00:20
There is also the EmacsWiki which contains all sorts of useful snippets and descriptions if extension packages. You could start to add to this and easily maintain a list of topics and references there.
dpoggio · 2022-05-11 04:16
Elisp also changes and is extended in many ways. It’s flexibility maybe needs the Perl’s slogan: There’s more than one way to do it.
mickeyp · 2022-05-11 06:50
You could give `shortdoc-display-group` a whirl. It's a new feature in Emacs 28. It's a cheat sheet of elisp commands with example code and hyperlinks to the Info manual. Have a look in `M-x find-library RET shortdoc` for examples. It's quite easy to extend.
Phil-Hudson · 2022-05-11 14:00
Addressing the use case you base your wider question on: org-capture is what you want, not yasnippet, *in this case*. yasnippet is fantastic for lots of other things though.
emoarmy · 2022-05-11 19:41
Holy hell, this is cool and obviosuly the only real right answer. /u/mickeyp coming through again :)
harizvi · 2022-05-12 18:23
shortdoc looks so useful, thanks /u/mickeyp for pointing to it. --
Lucky_Mike_D · 2022-05-12 19:32
This is exactly the type of thing I was looking for and didn't know existed, thank you very much!
Lucky_Mike_D · 2022-05-12 19:42
To add a bit more clarity to what I'm looking for ( /u/mickeyp has a really good suggestion that lines up with this ): Right now I can go to the gist website and use the search bar for example to pull up a bunch of buffer related code snippets. From there, I filter by "Emacs Lisp" and will get almost 8k 'gists' of elisp code presumably relating to buffers. I know emacs has easy ways to search and pull in 'gists' directly into the editor which is amazing, and it shouldn't be too hard to figure out how to automatically filter my searches by "Emacs Lisp" as a language. [https://gist.github.com/search?l=Emacs+Lisp&q=buffer](https://gist.github.com/search?l=Emacs+Lisp&q=buffer) Something exactly like gist, but totally focused on elisp, was wondering if such a thing exists as gist is great but I find it hard to find results relevant to what I'm searching for when its emacs specific sometimes. So far 'gist' seems like the best option, along with shortdoc-display-group