reddit / emacs

Can tempel replace yasnippet?

linwaytin · 2022-11-14 13:02
Yasnippet is a powerful tool, but it is also a heavy one. I only have a few simple snippets, and am thinking about replacing yasnippet with a simpler solution. How is tempel compared to yasnippet? Or are there other simple tools for code snippets? Thanks!
akirakom · 2022-11-14 13:15
You should have a look at tempo, which is shipped with Emacs. Tempo is like skeleton but more powerful. It supports multiple placeholders and should be sufficient for typical needs. A good thing with tempo is that its templates are S expressions, so you don't have to learn the syntax of yasnippet. I personally find yasnippet difficult. Downside is that it is tedious to convert an example code into a snippet. For large snippets, yasnippet may be easier. Tempel is a wrapper around tempo which make it easy to define and run tempo snippets.
J-ky · 2022-11-14 13:50
The only thing stops me from fully adopted to tempel is that eglot does not give function template without yasnippet, since eglot and yasnippet are written by the same guy. I already have a large yasnippet library written by myself. I have no intention to convert all of them to tempel. But newer snippets are all in tempel only. IMO, you can live fine with tempel, but installing yasnippet on the side even without any snippets makes your life easier as several popular packages assume the existence of yasnippet.
linwaytin · 2022-11-14 14:30
That is a good reason to keep using yasnippet, since it is the de facto template system. Thank you.
thetemp_ · 2022-11-14 14:32
Yes, if you don't care for the TextMate snippet syntax that YASnippet uses, and if you don't care about the vast array of prewritten snippets available for it. I'm not sure why YASnippet is as large as it is. It might be that implementing TextMate's snippet syntax requires more code, since it isn't s-expression based. Tempel implements and extends a syntax already used by Tempo.el (an older built-in package that was not very well documented). But Tempel has more features than Tempo (such as integration with `abbrev-mode`). It also puts the snippets (aka "templates") in a single file arranged by mode, where most templates can fit on one line. Another option is Skeleton.el, which like Tempo.el is built-in. The "Autotype" section of the Info directory covers Skeleton. It can do all the same things as Tempel, is in wide use, and it also uses an s-expression based syntax. However, its syntax is not as succinct as Tempel's. Of the 3, I found that I liked Tempel the most, but YMMV. There are several other packages available for this purpose. Tempel is from the same author who wrote Vertico and Consult.
alecStewart1 · 2022-11-14 15:58
> It supports multiple placeholders and should be sufficient for typical needs. What does "placeholders" mean here? Do you mean like this? if(PLACEHOLDER_TEXT) { // a comment as a place holder } Because that's easily done in Skeleton and Tempo, there's no difference. If you mean as in // underscores are placeholders if(_) { _ } Then, again, this is available in Skeleton. You can do `@ _` in the skeleton template. > A good thing with tempo is that its templates are S expressions. This is, again, the same as Skeleton. The immediate difference is that Tempo has the ability to go between tempo "marks", positions in Skeleton, out of the box with Emacs but it's not hard to create with skeletons. Tempos can have a `TAGLIST` for completion, however, if I understand correctly, that if you just make the tempo an abbrev, there's no need to give it a tag or list of tags. You can also just called it with `M-x name-of-your-tempo`. Again, all the same as Skeleton. The other differences are that you could argue the code from Tempo is a bit cleaner and that for `auto-insert` it's the Skeleton template language that's use, not Tempo. There's a reason in the [Emacs TODO](https://github.com/emacs-mirror/emacs/blob/master/etc/TODO#L649) there's this line: > ** Improve the "code snippets" support > Consolidate skeleton.el, tempo.el, and expand.el (any other?) and then advertise/use/improve it. Because Tempo and Skeleton are more or less functionally the same.
akirakom · 2022-11-14 16:38
> What does "placeholders" mean here? I mean `p`/`P` tags in tempo. > Because that's easily done in Skeleton and Tempo, there's no difference. Sorry for the inaccuracy, but tempo is generally more powerful than skeleton, so I only use the latter for auto insertion.
alecStewart1 · 2022-11-14 16:57
> but tempo is generally more powerful than skeleton How so?
akirakom · 2022-11-14 17:46
I didn't know much about skeleton, but it seems to be quite capable than I expected. Thanks for the info. However, this thread was originally about tempel vs. yasnippet, and skeleton is less relevant. I personally use tempo more often than skeleton, and according to your comments, both are mostly interchangeable. I don't find a need to learn skeketon.
alecStewart1 · 2022-11-14 18:23
Oh I don't care if you prefer one or the other, it's just one of those funny Emacs things that some people don't seem to know. I only wanted to point out that tempo and skeleton are basically the same, one isn't more or less powerful. Unless Daniel Mendler (minad) has deeper insight into tempo that made him choose to use it for tempel over skeleton.
linwaytin · 2022-11-14 18:29
I personally think it is easier to write templates in yasnippet, but maybe that is just because I'm familiar with the syntax. However, I'm not happy about yasnippet's file structure. Right now I use `yankpad` so that I can have all snippets in a single file. About the syntax, do you feel writing templates in tempo or tempel is difficult, for not-too-long snippets?
arthurno1 · 2022-11-14 19:09
> I'm not sure why YASnippet is as large as it is. ~5000 sloc. > It might be that implementing TextMate's snippet syntax requires more code, since it isn't s-expression based. On contrary; Yasnippet let you use the domain language with some minor hacks for placeholders, and almost trivial and non-existent syntax to declare some stuff. I suggest to look at some skeleton snippet that define similar and see yourself which one is shorter. In skeleton you are using lisp + string manipulation (lots of concats) to express the same, which is normally more to write, looks uglier and is harder to maintain compared to writing the domain code sprinkled with occasional placeholder instead of real variable names. I think there is a reason why yasnippet become de facto standard overnight. Yasnippet should have definitely been included in the core. The only thing I personally disslike is the idea to use one file per snippet. I would prefer all snippets for a particular mode in one file, but it is not a dealbreaker. Just my two cents.
biglotrspider · 2022-11-14 19:59
Tangling an org mode file would also work well to keep all the snippets in a single file.
thetemp_ · 2022-11-14 20:57
I find Tempel/Tempo's syntax to be easier, because it's essentially just Lisp data, which I'm familiar with through configuring Emacs. And I don't have a history of using YASnippet nor TextMate, so that syntax seems alien to me. If the only thing that bothers you about YASnippet is the way it spreads the snippets out in individual files, then you should probably keep using it with yankpad, or do as u/biglotrspider suggests and store your snippets in an org file and tangle out updated snippet files whenever you change them.
thetemp_ · 2022-11-14 21:15
> On contrary; Yasnippet let you use the domain language with some minor hacks for placeholders I was going on about the size of the codebase there, not opining about the syntax of the templating language. As you pointed out, YASnippet is over 5,000 lines of code. But Tempel is just over 700, close to the same length as Tempo.el, which it replaces. While I do prefer the Tempel/Tempo syntax to that of YASnippet, that's more to do with its familiarity. I like Elisp. > I suggest to look at some skeleton snippet that define similar and see yourself which one is shorter. Skeletons are another matter, and I think they're too clunky for simple templating. AFAICT, Tempel does everything that Skeletons do in a clearer and friendlier way. And you can fit shorter ones on a single line, making them convenient to browse through. I don't think that's practical with Skeletons or YASnippet snippets.
cradlemann · 2022-11-14 21:45
I prepare a [collection](https://github.com/Crandel/tempel-collection) repository for Tempel. Fill free to use it or make PRs. Some templates based on `yasnippet-collection` repository
Extension_Try_6870 · 2022-11-14 23:54
I found this but didn't try it yet so I'm not sure it does what you want. https://github.com/fejfighter/eglot-tempel
FreeAd7233 · 2022-11-15 01:47
I am planning to make the switch from yasnippet to tempel… and the only reason is I am now using corfu and cape cannot convert company-yasnippet successfully due to some special mechanism… and tempel in the contrary, provide a completion-at-point-function which allows you to just use corfu with it…
a-hausmann · 2022-11-15 05:27
Good info! Thanks.
arthurno1 · 2022-11-15 09:07
> I was going on about the size of the codebase there > Tempel is just over 700 I understand, but 5k is not much for what it does. It gives you templating, expansion, major mode to write templates in etc. If you patch all that together by combining different packages, you will probably end up at the same. Yasnippet is a well written code by someone who is quite familiar with Elisp. Maybe you don't need all that if you are writing snippets in elisp, but in my opinion, there is quite a value in being able to just copy a piece of code, paste it into a Yasnippet buffer and replace variables with placeholders, instead of writing long lists of strings patches to get concatenated. > While I do prefer the Tempel/Tempo syntax to that of YASnippet, that's more to do with its familiarity. I am not sure that we are talking about the same thing with "domain languages". Domain language = target language. If you write a snippet for C, or Java, or whatever, in Yasnippet, is just a piece of that language with some placeholders for variable names. If you're going to write it in tempel/tempo/skeleton, you will have to write Elisp sprinkled with string patches to produce the snippet string in the domain language, so per definition, it will be longer then Yasnippet snippet (minus metadata). For people familiar with Emacs Lisp that is probably fine, but for people not so familiar with Lisp or Emacs Lisp, Yasnippet is probably way easier to pick up. At least in that regard, I don't see how any of templating systems based on Lisp can replace Yasnippet, at least of the existing ones. Yes, I have myself being looking at Yasnippet and wondered if all 5k sloc are needed, but after looking at it, I think it is a very fine package. > Skeletons are another matter Indeed, your original post is about Tempel. I was reading the entire thread before I posted, so my answer was more a reflection on the entire thread, sorry if I didn't make it more clear. > AFAICT, Tempel does everything that Skeletons do in a clearer and friendlier way. Probably; but looking at examples on the GitHub, one still has to be quite familiar with both Lisp and Emacs Lisp, to basically do string manipulations in lisp code. That has strengths on its own, but I am not sure if it is *always* advantage. > I don't think that's practical with Skeletons or YASnippet snippets. Yasnippet divides snippets intentionally in two parts; some simple metadata and the snippet itself. For example: # -*- mode: snippet -*- # name: cls # key: cls # -- class ${1:Class} { $0 } Compare that to Tempel version: (class "public class " (p (file-name-base (or (buffer-file-name) (buffer-name)))) " {" n> r> n "}") Yes, you put that in one line, but does it matter? It is not like you are passing snippets around in the code. You will be putting them into some configuration file, and expanding them in your code. The two snippets above differ in readability by a horse length, in my opinion. Consider also people who are just users of Emacs as a text editor, without knowledge of Emacs Lisp (if there are some), which version is more accessible to them? Yasnippet is almost self-explanatory. Placeholders, and the "metadata" looks pretty much like a shell script, and is not difficult to pick up even if one is not familiar with it, at least in my opinion (I have never looked at Yasnippet manual or docs). I am not an expert on Elisp, but I am quite familiar with it at this time, and I personally don't see any disadvantage of not using Emacs Lisp directly in Yasnippets, despite my "familiarity" with Elisp, on the contrary. And we can still call Elisp in Yasnippet snippets if needed: # -*- mode: snippet; require-final-newline: nil -*- # name: constructor # key: c # -- public ${1:`(file-name-base (or (buffer-file-name) (buffer-name)))`}($2) { $0 } so nothing is lost.
thetemp_ · 2022-11-15 14:36
> I am not sure that we are talking about the same thing with "domain languages". Domain language = target language. This is funny, because I think first you assumed I was talking about something else. But in response, I assumed you were talking about something else. And finally now, we know what each other is talking about. You raise some good points that I don't disagree with. I still think it's mainly a matter of preference, and that there are ways in which Tempel is more powerful. For example, is it possible to have a YASnippet snippet call another YASnippet snippet? There's an example demonstrating how to do this with Tempel on its GitHub page. But maybe that isn't the primary use-case for YASnippet. After all, the term "snippet" refers more to a bit of text one inserts, whereas "template" implies something more complex. So maybe we're comparing apples and oranges. OP's question was *can* Tempel replace YASnippet, not *should* it. (And I take that question as referring to one's personal configuration, not in the more general--existential--sense.) I do think it can if that's what you want to do, but I think I've already said several times that it's a matter of preference. And to be clear, I don't mean to denigrate YASnippet. Both packages are written by people who amaze me with the amount of quality Emacs code they've put out.
linwaytin · 2022-12-11 17:02
Did you try tempel already? I'm curious about your experience.
FreeAd7233 · 2022-12-12 04:27
There’s already capf port for yasnippet. So I am satisfied with yasnippet now.