(redirected from Main.AutoInsert)
On this page... (hide)
autoinsert is a standard package included in the Emacs distribution. It provides a mechanism to automatically insert text into a newly created file.
The autoinsert mechanism is based on matching a filename spec with a template, and possibly some code to tweak the contents of the template to the local situation.
Autoinsert has some preexisting content, but is not enabled by
default. To enable it, add (auto-insert-mode t) to you .emacs
file.
Extending auto insertion functions
Autoinsert has a nifty feature that lets you specify an elisp function to execute on the buffer after the template has been inserted into the new file. This allows you to customize the inserted template to the file being created, such as adding comments including the file name, author, email, copyright, and such information.
I've created a configuration file that i load in my .emacs file:
- ;; auto-insert stuff
- ;; From http://www.emacswiki.org/emacs/AutoInsertMode
- ;;; Commentary:
- ;; Provides for inserting standard file templates with variable
- ;; substitution based on the file's type (given by it's extention).
- ;;
- ;; New templates should go into the directory specified by
- ;; `auto-insert-directory' and added to the `auto-insert-alist'
- ;; variable.
- ;;; Code:
- (require 'autoinsert)
- (auto-insert-mode t)
- (setq auto-insert-query nil)
- (setq auto-insert-directory (expand-file-name "~/.emacs.d/file-templates/"))
- ;; Set the alist of file type and actions to take when encountered
- ;; Node format:
- ;; ((file-type-regex . description) [template-name customizing-function])
- ;;
- ;; file-type-regex (regex) -- how to determine which template to use
- ;; description (string) -- file type description
- ;; template-name (string) -- file name (within `auto-insert-directory')
- ;; customizing-function (function) -- name of function that will run
- ;; when the auto insert occurs. This is where you set up the variable
- ;; substititutions.
- (add-to-list 'auto-insert-alist
- '(("\\.[Cc][Pp][Pp]$" . "C++ Files") .
- ["insert.cpp" auto-update-c-source-file]))
- (add-to-list 'auto-insert-alist
- '(("\\.[Hh]$" . "C/C++ Header Files") .
- ["insert.h" auto-update-header-file]))
- (add-to-list 'auto-insert-alist
- '(("\\.[Cc]$" . "C files") .
- ["insert.c" auto-update-c-source-file]))
- (add-to-list 'auto-insert-alist
- '(("\\.[Ss][Hh]$" . "Shell Script") .
- ["insert.sh" auto-update-other-source-file]))
- (add-to-list 'auto-insert-alist
- '(("\\.[Pp][Hh][Pp][345]?$" . "PHP Script") .
- ["insert.php" auto-update-other-source-file]))
- (add-to-list 'auto-insert-alist
- '(("\\.[Pp][Ll]$" . "Perl Script") .
- ["insert.pl" auto-update-other-source-file]))
- (add-to-list 'auto-insert-alist
- '(("\\.[Cc][Ss][Ss]$" . "Cascading Style Sheet") .
- ["insert.css" auto-update-other-source-file]))
- (add-to-list 'auto-insert-alist
- '(("\\.[Ss]?[Hh][Tt][Mm][Ll]?$" . "HTML Page") .
- ["insert.html" auto-update-other-source-file]))
- ;(setq auto-insert 'other)
- ;; Handle substitutions for .h files (used in C/C++)
- ;; function replaces the string '@@@' by the current file
- ;; name. You could use a similar approach to insert name and date into
- ;; your file.
- (defun auto-update-header-file ()
- (save-excursion
- (while (search-forward "@@@" nil t)
- (save-restriction
- (narrow-to-region (match-beginning 0) (match-end 0))
- (replace-match (upcase (file-name-nondirectory buffer-file-name)))
- (subst-char-in-region (point-min) (point-max) ?. ?_)
- ))
- )
- )
- ;; Handle substitutions for .c/.cpp files
- (defun auto-update-c-source-file ()
- (save-excursion
- ;; Replace HHHH with file name sans suffix
- (while (search-forward "HHHH" nil t)
- (save-restriction
- (narrow-to-region (match-beginning 0) (match-end 0))
- (replace-match (concat (file-name-sans-extension (file-name-nondirectory buffer-file-name)) ".h") t
- )
- ))
- )
- (save-excursion
- ;; Replace @@@ with file name
- (while (search-forward "@@@" nil t)
- (save-restriction
- (narrow-to-region (match-beginning 0) (match-end 0))
- (replace-match (file-name-nondirectory buffer-file-name))
- ))
- )
- (save-excursion
- ;; replace DDDD with today's date
- (while (search-forward "DDDD" nil t)
- (save-restriction
- (narrow-to-region (match-beginning 0) (match-end 0))
- (replace-match "")
- (insert-today)
- ))
- )
- )
- ;; Handle substitutions for other types of files
- ;; Standard update to templates
- ;; Replaces the following strings:
- ;; @@AUTHOR@@ -- user's full name
- ;; @@AUTHOREMAIL@@ -- users email address
- ;; @@FILENAMENS@@ -- file name without extension
- ;; @@FILENAME@@ -- file name
- ;; @@TODAY@@ -- today's date
- ;; @@YEAR@@ -- current year
- ;; @@ORGANIZATION@@ -- value of envar ORGANIZATION
- (defun auto-update-other-source-file ()
- (save-excursion
- ;; Replace @@AUTHOR@@ with user's full name
- (while (search-forward "@@AUTHOR@@" nil t)
- (save-restriction
- (narrow-to-region (match-beginning 0) (match-end 0))
- (replace-match user-full-name t)
- ))
- )
- (save-excursion
- ;; Replace @@AUTHOREMAIL@@ with user's email address
- (while (search-forward "@@AUTHOREMAIL@@" nil t)
- (save-restriction
- (narrow-to-region (match-beginning 0) (match-end 0))
- (replace-match user-mail-address t)
- ))
- )
- (save-excursion
- ;; Replace @@FILENAMENS@@ with file name sans suffix
- (while (search-forward "@@FILENAMENS@@" nil t)
- (save-restriction
- (narrow-to-region (match-beginning 0) (match-end 0))
- (replace-match (file-name-sans-extension (file-name-nondirectory buffer-file-name)) t)
- ))
- )
- (save-excursion
- ;; Replace @@FILENAME@@ with file name
- (while (search-forward "@@FILENAME@@" nil t)
- (save-restriction
- (narrow-to-region (match-beginning 0) (match-end 0))
- (replace-match (file-name-nondirectory buffer-file-name))
- ))
- )
- (save-excursion
- ;; replace @@TODAY@@ with today's date
- (while (search-forward "@@TODAY@@" nil t)
- (save-restriction
- (narrow-to-region (match-beginning 0) (match-end 0))
- (replace-match "")
- (insert-today)
- ))
- )
- (save-excursion
- ;; replace @@YEAR@@ with today's date
- (while (search-forward "@@YEAR@@" nil t)
- (save-restriction
- (narrow-to-region (match-beginning 0) (match-end 0))
- (replace-match "")
- (insert-year)
- ))
- )
- (save-excursion
- ;; replace @@ORGANIZATION@@ with today's date
- (while (search-forward "@@ORGANIZATION@@" nil t)
- (save-restriction
- (narrow-to-region (match-beginning 0) (match-end 0))
- (replace-match "")
- (insert (getenv "ORGANIZATION"))
- ))
- )
- )
A sample template looks like this:
- <?php
- /**
- *
- * @@FILENAMENS@@
- *
- * Author: @@AUTHOR@@ <@@AUTHOREMAIL@@>
- * Created: @@TODAY@@
- * Copyright (c) @@YEAR@@ @@ORGANIZATION@@
- *
- */
- /**
- * DOC BLOCK
- */
The auto-update-other-source-file function takes care of
substituting the appropriate values in the template, resulting in
something like the following:
- <?php
- /**
- *
- * sample
- *
- * Author: Tamara Temple <tamara@tamaratemple.com>
- * Created: 2011/10/07
- * Copyright (c) 2011 Tamara Temple Web Development
- *
- */
- /**
- * DOC BLOCK
- */
Combined with Ya Snippets, autoinsert is quite the gem!
| Tags: | Categories: Articles |
Recent Changes |
Printable View |
Page History |
Edit Page
Page last modified on April 09, 2012, at 09:11 AM by tamara