mineo / yatemplate

File templates for Emacs with YASnippet
https://github.com/mineo/yatemplate
GNU General Public License v3.0
70 stars 11 forks source link

Support templates by major-mode #1

Open PythonNut opened 9 years ago

PythonNut commented 9 years ago

auto-insert-mode can define insertions by major mode (in addition to regex matching the filename). It would be great if yatemplate also supported this. Perhaps something-like:

00:major-mode:c++-mode

To define a snippet for c++-mode.

TLATER commented 2 years ago

Hmm, doing this in a backward-compatible way may be difficult. Using two : would definitely cause issues, because currently yatemplate is documented to specifically throw away everything before the first :.

It could be something like 00-major-mode:c++-mode, since that wouldn't break ordering constraints, but that would still be a breaking change (albeit one that is unlikely to collide with anything). Have you ever had any thoughts about this @mineo?

noctuid commented 2 years ago

Right now I just have Doom's template system copied in my config file. I'd like to switch to something that's actually packaged, but the functionality of yatemplate seems limited by comparison. For example, Doom's template system supports this:

(setq doom-+file-templates-alist
      `((,(rx ".org" eol)
         :when noct-blog-dir-p
         :trigger "__post"
         :mode org-mode)
        (emacs-lisp-mode :trigger "__package")))

Going entirely by file naming seems too basic to handle more complex cases.

TLATER commented 2 years ago

I've ultimately decided that doing this in file names is probably a bad idea to begin with. It makes it much more likely that my emacs config will fail on some more exotic filesystems (or, i don't know, an OS that uses \ as a path separator, but who would want to use such a thing?), and it makes things awkward to type if you want your regexes to be a little robust. It also indeed prevents using the really, really nice rx macro.

The code for doing it with just autoinsert isn't a significant amount of boilerplate anyway (or any if you don't want to write regexes in your file names), so I don't think that a package is actually necessary for the basics.

autoinsert supports file name regexes and modes, I don't know by what sorcery the :when thing works for doom (but perhaps arbitrary functions are accepted and undocumented, or doom does regex manipulation?), but I find I don't need that for my config currently.

The only slight annoyance is that it feels like the template names are duplicating information, which is why I used yasnippet in the first place. I think ideally yasnippet itself would be extended to have a template pragma instead, where you can record these regexes/mode names. I looked a little into doing that, but it's a bit much effort for what I want to do. Upstream hasn't seen much maintenance lately either, and I'm not too interested in trying to work on something that will never be merged anyway.

noctuid commented 2 years ago

I don't know by what sorcery the :when thing works for doom

Doom doesn't use autoinsert at all; it uses find-file-hook. It has a couple other useful keywords as well.

I think ideally yasnippet itself would be extended to have a template pragma instead, where you can record these regexes/mode names. I looked a little into doing that, but it's a bit much effort for what I want to do. Upstream hasn't seen much maintenance lately either, and I'm not too interested in trying to work on something that will never be merged anyway.

Yeah, it would be nice if yasnippet just had this functionality built in. I would package an alternative myself, but I don't really want to maintain it. I'll just keep the code in my config for now.

piknik commented 1 year ago

See #32 for a possible solution

TLATER commented 1 year ago

See #32 for a possible solution

Very cool! Hope it lands, last I looked upstream wasn't really accepting PRs because they're working on a more-or-less full rework of the codebase that in turn has stalled. Maybe I'll switch to your branch in the mean time :)