[Top] [Contents] [Index] [ ? ]

Top node.

This is the manual of Emacs template package


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1. Overview

When you create a new file with Emacs, package Template supplies an initial buffer content via a template: a file with normal text and expansion forms. There is a menu to easily create such templates. You can also use new commands to decorate comments and update the buffer contents.

The main difference between Template and other similar packages is that you can define very flexible templates without having to learn Lisp or changing your Emacs init file. This package does not help Lisp programmers to define complex macros.

There is a menu to support the creation of template files. Additionally, this package provides the following features:


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.1 Packages Related to Template

There are template packages of two different categories, which are determined by one question:

What do you want to do with templates? Do you want to use templates when creating new files, or do you want to insert some stuff during editing (but you are not satisfied with the standard macro mechanism)?

Well, templates could be seen as both new-file templates and macro-like template, but the main application of templates determines the answers for the following questions:

Here is a little list of related files, collected around 1998, some discarded later (no URLs, no active updating, but you can send me a mail):


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2. Installation


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.1 Installation Instructions

  1. Copy file ‘template/lisp/template.el’ into your load-path and byte-compile it.
  2. Insert the following code into your Emacs init file (‘~/.emacs’):
     
    (require 'template)
    (template-initialize)
    
  3. If you want to customize package Template, use M-x customize-group <RET> template <RET> or the customize entry in menu Options.
  4. You might want to copy the template files from directory ‘template/templates/’ to ‘~/.templates/’ and change them according to your preferences.

Installation instructions with further explanations can be found in file ‘template/INSTALL’ of the distribution.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3. Basics

Emacs, template, file template, comment decoration, auto-updating, time stamp, details


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.1 Menus and Key-Bindings

For Emacs-21.1+ and XEmacs, this package defines the following menu entries and key-bindings:


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.2 Using a Template

Package Template offers various ways how to use template files:

In Emacs, the usual way to create a new file is by typing C-x C-f, using the menu or the toolbar (running command find-file or ffap) and entering a non-existent file name. In this case, you will be asked whether to use a template for the new file. You can turn off this feature.

For more template-new-file and template-auto-insert, See section Customization of keyword expansion rules.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.3 Finding a Template

The algorithm for finding the most appropriate template can be best explained with a little example:

The creation of the template files in the right directory is already enough for packages Template to find the most appropriate template for a new file:

For more on derivation See section Customization of how templates are derived.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.4 Creating a Template

There are various predefined expansions which are probably enough for most templates. A corresponding expansion forms can be easily inserted by selecting the appropriate entry in the new submenu Template Creation :

You can define your own expansions in your Emacs init file and at the end of a template file. A corresponding definition can be easily inserted by selecting the appropriate entry in the new submenu Template Creation:

User Option: template-default-expansion-alist
User Option: template-definition-start

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.5 Decorating a Comment

There are commands to decorate comments with dashes and alike (‘-’, ‘=’ and ‘#’ by default). Both commands are included in the new submenu Comments and bound to keys:

The comment decoration style is highly customizable and can be controlled by

For more on comments See section Customization of comments.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.6 Updating a Buffer

A buffer is automatically updated according to some rules when saving the file. This updating can be invoked explicitly by selecting the menu entry Update Buffer in the new submenu Comments .

User Option: template-update-buffer
User Option: template-auto-update
User Option: template-update-buffer-alist
User Option: template-header-regexp-alist

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4. Templates for advanced users


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.1 Templates customization


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.1.1 Customization of keyword expansion rules

Interactive Command: template-new-file file template with-undo

Open a new file file by using a template. Using a template for creating a new file consists of, steps 1 to 3 are only executed when called interactively:

  1. Prompt for the name of the new file.
  2. Template derivation: suggest a reasonable template file to the user see template-derivation-alist.
  3. File name refinement: e.g., if the given file name is ‘exercise’ and there are two files ‘exercise1.tex’ and ‘exercise2.tex’ in the same directory and if we have a template ‘exercise.tex.tpl’, the file name is refined to ‘exercise3.tex’. This is turned off when C-x t is called with a prefix argument.
  4. Template insertion: insert the template file into the empty buffer.
  5. Read per-template expansion definition section starting at template-definition-start and delete it.
  6. Display: before message in template-message-buffer.
  7. Execute pre-expansion commands defined in the definition section.
  8. Set local variables defined in the definition section.
  9. Expansion: expand the expansion forms (text matched by template-expansion-regexp) They are defined in the definition section, in template-expansion-alist, or provided by default, see template-expansion-regexp and template-register-regexp.
  10. Execute post-expansion commands defined in the definition section.
  11. Run normal-mode and functions in find-file-hooks.
  12. Update header according to template-update-header with argument if-exists.
  13. Display: after message in template-message-buffer.
  14. Report: display a temporary message at point defined in the definition section and an automatically generated message in the minibuffer area, see template-message-timeout.

If optional with-undo is non-nil, store corresponding changes in buffer-undo-list. If file is nil, the buffer for file has already been created and the accessible part will be replaced by the expanded template. If template is nil (empty input when called interactively), do not use a template.

User Option: template-auto-insert

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.1.2 Customization of how templates are derived

User Option: template-derivation-alist

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.1.3 Customization of comments

User Option: template-single-comment
Interactive command: template-block-comment arg

It is bound to C-x C-;, and <menu-bar> <edit> <Comment> <Decorate Comment Block>.

Decorate the current block of comment-only lines with dashes and alike. That is, surround the the contiguous comment-only lines around point with extra lines containing dashes and alike and to put the correct number of newlines around the block.

Barf if the comment syntax at point has a non-empty comment-end or if point is not in a comment-only line.

A block comment consists of all neighboring lines which start with spaces and comment-start. If comment-start is a string of length 1, the number of repetitions of comment-start must be the same or larger than in the line where the command is invoked from, too.

Prefix argument arg and template-comment-specification determines the comment style to use. The length of the separator line is determined by template-max-column.

This command can also be used with point in an empty line after a block comment. A second invocation of this command directly after a successful invocation deletes the remaining empty lines from the current line on.

User Option: template-comment-specification-alist

List of specifications for comment functions. Each specification at level, starting at 1, is a list (separator before-block after-block DELETE-LINES)

separator is the string which is inserted repeatedly by commands C-x C-= and C-x C-; up to template-max-column.

After that, C-x C-; deletes DELETE-LINES after the comment block and inserts string after-block at the end of the block and before-block at the front of the block.

The specification level to use is determined by:

  1. If the prefix argument is non-nil and its numeric value is > 0, this value is the level.
  2. If the prefix argument is nil, and there is an old comment style, use old comment style.
  3. If template-comment-specification-special is a function or the current major mode has a property with this name and its value is a function, this function returns the specification.
  4. If comment-end is empty and comment-start is a string of length 1: level is number of repetitions of comment-start at the beginning of the line. Otherwise, if the correctly indented line starts at the beginning of the line, level=3, else level=2.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5. Indexes for commands, keyword, and variable


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.1 Keyword Index


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.2 Keyword Index

Jump to:   T  
Index Entry Section

T
template-block-comment4.1.3 Customization of comments
template-new-file4.1.1 Customization of keyword expansion rules

Jump to:   T  

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.3 Variable Index

Jump to:   T  
Index Entry Section

T
template-auto-insert4.1.1 Customization of keyword expansion rules
template-auto-update3.6 Updating a Buffer
template-comment-specification-alist4.1.3 Customization of comments
template-default-expansion-alist3.4 Creating a Template
template-definition-start3.4 Creating a Template
template-derivation-alist4.1.2 Customization of how templates are derived
template-header-regexp-alist3.6 Updating a Buffer
template-single-comment4.1.3 Customization of comments
template-update-buffer3.6 Updating a Buffer
template-update-buffer-alist3.6 Updating a Buffer

Jump to:   T  

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6. News for Package Template

Latest Version:

3.1c (21 Aug 2003).

Requirements:

Emacs-20.2, XEmacs-20.2, or higher.

No menu support for Emacs before Version 21.1.

Licence:

GNU General Public License.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6.1 Changes with Release 3.1c


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6.2 Changes with Release 3.1

Christoph Wedler http://sourceforge.net/users/wedler/, 21 Aug 2003


[Top] [Contents] [Index] [ ? ]

Footnotes

(1)

for the menu entry and its key-binding type C-h f template-new-file, @xref{EExpansion}


[Top] [Contents] [Index] [ ? ]

About This Document

This document was generated by a tester on a sunny day using texi2html.

The buttons in the navigation panels have the following meaning:

Button Name Go to From 1.2.3 go to
[ < ] Back Previous section in reading order 1.2.2
[ > ] Forward Next section in reading order 1.2.4
[ << ] FastBack Beginning of this chapter or previous chapter 1
[ Up ] Up Up section 1.2
[ >> ] FastForward Next chapter 2
[Top] Top Cover (top) of document  
[Contents] Contents Table of contents  
[Index] Index Index  
[ ? ] About About (help)  

where the Example assumes that the current position is at Subsubsection One-Two-Three of a document of the following structure:


This document was generated by a tester on a sunny day using texi2html.