TemplatesMode

You can find the Template package at http://emacs-template.sourceforge.net/


The template.el system is a way of automating initial file-creation from templates with place-holder tags that are filled-in when the template is used. To use template.el, you place a series of template files in your ~/.templates directory, for example TEMPLATE.xml.tpl, README.tpl, Makefile.tpl. When you create a new file, some file extension mappings are used to find the appropriate template, and the template.el tags are filled-in automatically (sometimes based on user prompts). For example, there are standard embedded tags like (>>>FILE<<<) or (>>>DATE<<<) that expand to the filename and the date, respectively. There are other features such as (>>>POINT<<<), which marks the place to leave the cursor after file creation.

These tag-substitution templates use boiler-plate that look very much like the finished product, so it is likely that set-up will be easier than with using TempoMode or SkeletonMode, though template.el is geared towards creating initial file content: unlike TempoMode or SkeletonMode, template.el is not intended for inserting code snippets, such as a new class or a new function body.

See HtmlTemplateExample for an illustration.

template.el also contains commands for comment decoration and for file header automatic update.


File creation features:


It’s easy to define custom handlers for tags (ie expansion rules that do things more complexe that just expand the expansion key to some user input). The default is to prompt the user for input if the tag is not found, but you can also define your own custom handler. Here’s an example I wrote to call out to a program that generates GUIIDs when a (>>>GUIID<<<) tag is encounterd in the template. You could also easily make custom prompts.

(add-to-list 
 'template-expansion-alist
 '("GUIID" (insert (substring (shell-command-to-string "uuidgen") 0 -1))))

One thing I’m missing, however, is a way to get access to these variable settings like you can with TempoMode ‘s (s ...) tags. In Tempo you can ask the user for a replacement once with a (p ...) tag and then reuse it later in the template with the (s ...)

⇒ Answer: you can set a policy flag telling whether user is asked for input immediately (before expansion step, this is useful for per-template expansion rule), first time expansion key is met, or evey time expansion key is met.

One limitation is that when you create a file, it picks the best match template based on the filename and only gives you the option of using that one template. It would be nice if it presented you with a list of candidates based on all plausible matches. It’s unrealistic to think the filename will tell you everything you need to know about what type of c++ file you’re trying to create. The list could be something like that used by find-file.el, or just a standard completing-read.

My solution with PerlNow was to provide alternate commands to “find-file” to serve special purposes, like open using the object-oriented perl template, or open using the basic perl module template, and so on. (In general, I think this file-extension orientation is a problem with emacs that hasn’t been really faced yet).

It also seems to lack a reasonable mechanism for handling prompts with defaults. When making your template you might guess that the class in a c++ file should be named after the file, i.e. (>>>FILE_SANS<<<) in Template nomenclature, but still you want to prompt the user just in case using FILE_SANS as the default response is wrong.


How does template.el cooperate with ido.el? simply adding ido-find-file to template-find-file-commands doesn’t work, you need add this to your dot emacs file:

(add-to-list 'template-find-file-commands
	                 'ido-exit-minibuffer)

CategoryTemplates