Next: , Previous: pde-util, Up: Top


9 New file with template

template.el is exists, why template-simple? First of all, it is simple, the there is only one core function template-compile.

— Function: template-compile

Parse current buffer to parsed template.

The parsed template contains a list of strings, symbols or lisp forms. The strings is inserted directly. The symbol can be a predefined value in template-default-alist, or can be translate by template-expand-function. The lisp forms is handled by template-expand-function.

— User Option: template-expand-function

Functions to expand parsed template. There are two candicate functions available in template-simple: template-skeleton-expand and template-tempo-expand. Default function is template-tempo-expand, because I think tempo provide more fetures and flexibility than skeleton.

— Function: define-template-expander name alist &rest body

Define a new type of template-expand-function. NAME is used to create a function template-<NAME>-expand. ALIST can be a symbol or a form to return a list of symbol table add to template-default-alist. BODY is the code to expand and insert the template. the value of variable TEMPLATE is the translated template. The element of parsed template is translated by template-expansion

— Function: template-simple-expand-template file

Expand template in file. Parse the template to parsed templates with template-compile. Use template-expand-function to expand the parsed template.

— Function: template-simple-expand template

Expand string template. template can be a string or a parsed template. If it is string, parse the template to parsed templates with template-compile. Use template-expand-function to expand the parsed template.

template-simple is designed to compatible to template.el, so the default open and close parentheses is “(>>>” and “<<<)”. Note that global value of template-expand-function is saved in template-simple-expand. The same case happened to template-parens in template-compile. So If you need change them temporary for certain template, you can set template-parens and template-expand-function like file variable, for example:

   (template-simple-expand
    ";; -*- template-parens: (\"{\" . \"}\"); template-expand-function: template-tempo-expand -*-
     (defun {p} ({p})
       \"{p}\"
       {p}
       )")

This will expand by template-tempo-expand but not default expand function. Luckily in most case, template of tempo and skeleton can be the same, so you don't have to set template-expand-function. If your template uses element that only exists in tempo or skeleton, you'd better set it in template to make sure the template-simple-expand select the correct one.

The major purpose of template-simple is used for fill empty file using predefined template.

— User Option: template-directory-list

A list of directories for lookup template files. Default is “~/.templates/” and auto-insert-directory.

— Function: template-derive-template

Find proper template for current file. The template file is a file with name “TEMPLATE” and the same extension of current file under template-directory. You can add a suffix “.tpl” to the template file name too, just for compatible with templates provide by template.el. If the file didn't have extension, the template file's extension is current file name, for example, “TEMPLATE.Makefile” is template file for “Makefile”.

— User Option: template-default-alist

Predefined symbol for template file. Symbols to lookup in this table is case insensitive.

Here is a list of predefined symbol:

A neat function provided by template.el is the file name in the header can automatic update when the file is renamed. template-simple also provide this feature.

— Function: template-simple-update-header

update file header when needed. The file header is a line match template-header-regexp at first 3 lines in current file.

A typical file header is like:

   ;;; file-name --- short description

or

   /* @(#)file-name   short description
    */