yatemplate icon indicating copy to clipboard operation
yatemplate copied to clipboard

Support templates by major-mode

Open PythonNut opened this issue 8 years ago • 6 comments

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.

PythonNut avatar Oct 08 '15 17:10 PythonNut

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?

TLATER avatar Apr 20 '22 18:04 TLATER

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.

noctuid avatar May 05 '22 13:05 noctuid

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.

TLATER avatar May 05 '22 14:05 TLATER

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.

noctuid avatar May 05 '22 15:05 noctuid

See #32 for a possible solution

piknik avatar Feb 12 '23 13:02 piknik

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 :)

TLATER avatar Feb 19 '23 08:02 TLATER