Greytree

TamWiki

For a mouse who is a packrat

Technology » Auto Insert
Automatically insert text when creating a new file in Emacs

Summary:this is what goes at the top of the site

(redirected from Main.AutoInsert)

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:

conf-autoinsert.el
  1. ;; auto-insert stuff
  2. ;; From http://www.emacswiki.org/emacs/AutoInsertMode
  3.  
  4. ;;; Commentary:
  5.  
  6. ;; Provides for inserting standard file templates with variable
  7. ;; substitution based on the file's type (given by it's extention).
  8. ;;
  9. ;; New templates should go into the directory specified by
  10. ;; `auto-insert-directory' and added to the `auto-insert-alist'
  11. ;; variable.
  12.  
  13. ;;; Code:
  14.  
  15. (require 'autoinsert)
  16. (auto-insert-mode t)
  17. (setq auto-insert-query nil)
  18. (setq auto-insert-directory (expand-file-name "~/.emacs.d/file-templates/"))
  19.  
  20. ;; Set the alist of file type and actions to take when encountered
  21. ;; Node format:
  22. ;;   ((file-type-regex . description) [template-name customizing-function])
  23. ;;
  24. ;; file-type-regex (regex) -- how to determine which template to use
  25. ;; description (string) -- file type description
  26. ;; template-name (string) -- file name (within `auto-insert-directory')
  27. ;; customizing-function (function) -- name of function that will run
  28. ;;    when the auto insert occurs. This is where you set up the variable
  29. ;;    substititutions.
  30.  
  31. (add-to-list 'auto-insert-alist
  32.          '(("\\.[Cc][Pp][Pp]$" . "C++ Files") .
  33.            ["insert.cpp" auto-update-c-source-file]))
  34.  
  35. (add-to-list 'auto-insert-alist
  36.          '(("\\.[Hh]$" . "C/C++ Header Files") .
  37.            ["insert.h" auto-update-header-file]))
  38.  
  39. (add-to-list 'auto-insert-alist
  40.          '(("\\.[Cc]$" . "C files") .
  41.            ["insert.c" auto-update-c-source-file]))
  42.  
  43. (add-to-list 'auto-insert-alist
  44.          '(("\\.[Ss][Hh]$" . "Shell Script") .
  45.            ["insert.sh" auto-update-other-source-file]))
  46.  
  47. (add-to-list 'auto-insert-alist
  48.          '(("\\.[Pp][Hh][Pp][345]?$" . "PHP Script") .
  49.            ["insert.php" auto-update-other-source-file]))
  50.  
  51. (add-to-list 'auto-insert-alist
  52.          '(("\\.[Pp][Ll]$" . "Perl Script") .
  53.            ["insert.pl" auto-update-other-source-file]))
  54.  
  55. (add-to-list 'auto-insert-alist
  56.          '(("\\.[Cc][Ss][Ss]$" . "Cascading Style Sheet") .
  57.         ["insert.css" auto-update-other-source-file]))
  58.  
  59. (add-to-list 'auto-insert-alist
  60.          '(("\\.[Ss]?[Hh][Tt][Mm][Ll]?$" . "HTML Page") .
  61.         ["insert.html" auto-update-other-source-file]))
  62.  
  63. ;(setq auto-insert 'other)
  64.  
  65.  
  66.  
  67. ;; Handle substitutions for .h files (used in C/C++)
  68. ;; function replaces the string '@@@' by the current file
  69. ;; name. You could use a similar approach to insert name and date into
  70. ;; your file.
  71. (defun auto-update-header-file ()
  72.   (save-excursion
  73.     (while (search-forward "@@@" nil t)
  74.       (save-restriction
  75.     (narrow-to-region (match-beginning 0) (match-end 0))
  76.     (replace-match (upcase (file-name-nondirectory buffer-file-name)))
  77.     (subst-char-in-region (point-min) (point-max) ?. ?_)
  78.     ))
  79.     )
  80.   )
  81.  
  82. ;; Handle substitutions for .c/.cpp files
  83. (defun auto-update-c-source-file ()
  84.   (save-excursion
  85.     ;; Replace HHHH with file name sans suffix
  86.     (while (search-forward "HHHH" nil t)
  87.       (save-restriction
  88.     (narrow-to-region (match-beginning 0) (match-end 0))
  89.     (replace-match (concat (file-name-sans-extension (file-name-nondirectory buffer-file-name)) ".h") t
  90.                )
  91.     ))
  92.     )
  93.   (save-excursion
  94.     ;; Replace @@@ with file name
  95.     (while (search-forward "@@@" nil t)
  96.       (save-restriction
  97.     (narrow-to-region (match-beginning 0) (match-end 0))
  98.     (replace-match (file-name-nondirectory buffer-file-name))
  99.     ))
  100.     )
  101.   (save-excursion
  102.     ;; replace DDDD with today's date
  103.     (while (search-forward "DDDD" nil t)
  104.       (save-restriction
  105.     (narrow-to-region (match-beginning 0) (match-end 0))
  106.     (replace-match "")
  107.     (insert-today)
  108.     ))
  109.     )
  110.   )
  111.  
  112. ;; Handle substitutions for other types of files
  113. ;; Standard update to templates
  114. ;; Replaces the following strings:
  115. ;;  @@AUTHOR@@ -- user's full name
  116. ;;  @@AUTHOREMAIL@@ -- users email address
  117. ;;  @@FILENAMENS@@ -- file name without extension
  118. ;;  @@FILENAME@@ -- file name
  119. ;;  @@TODAY@@ -- today's date
  120. ;;  @@YEAR@@ -- current year
  121. ;;  @@ORGANIZATION@@ -- value of envar ORGANIZATION
  122. (defun auto-update-other-source-file ()
  123.   (save-excursion
  124.     ;; Replace @@AUTHOR@@ with user's full name
  125.     (while (search-forward "@@AUTHOR@@" nil t)
  126.       (save-restriction
  127.     (narrow-to-region (match-beginning 0) (match-end 0))
  128.     (replace-match user-full-name t)
  129.     ))
  130.     )
  131.   (save-excursion
  132.     ;; Replace @@AUTHOREMAIL@@ with user's email address
  133.     (while (search-forward "@@AUTHOREMAIL@@" nil t)
  134.       (save-restriction
  135.     (narrow-to-region (match-beginning 0) (match-end 0))
  136.     (replace-match user-mail-address t)
  137.     ))
  138.     )
  139.   (save-excursion
  140.     ;; Replace @@FILENAMENS@@ with file name sans suffix
  141.     (while (search-forward "@@FILENAMENS@@" nil t)
  142.       (save-restriction
  143.     (narrow-to-region (match-beginning 0) (match-end 0))
  144.     (replace-match (file-name-sans-extension (file-name-nondirectory buffer-file-name)) t)
  145.     ))
  146.     )
  147.   (save-excursion
  148.     ;; Replace @@FILENAME@@ with file name
  149.     (while (search-forward "@@FILENAME@@" nil t)
  150.       (save-restriction
  151.     (narrow-to-region (match-beginning 0) (match-end 0))
  152.     (replace-match (file-name-nondirectory buffer-file-name))
  153.     ))
  154.     )
  155.   (save-excursion
  156.     ;; replace @@TODAY@@ with today's date
  157.     (while (search-forward "@@TODAY@@" nil t)
  158.       (save-restriction
  159.     (narrow-to-region (match-beginning 0) (match-end 0))
  160.     (replace-match "")
  161.     (insert-today)
  162.     ))
  163.     )
  164.   (save-excursion
  165.     ;; replace @@YEAR@@ with today's date
  166.     (while (search-forward "@@YEAR@@" nil t)
  167.       (save-restriction
  168.     (narrow-to-region (match-beginning 0) (match-end 0))
  169.     (replace-match "")
  170.     (insert-year)
  171.     ))
  172.     )
  173.   (save-excursion
  174.     ;; replace @@ORGANIZATION@@ with today's date
  175.     (while (search-forward "@@ORGANIZATION@@" nil t)
  176.       (save-restriction
  177.     (narrow-to-region (match-beginning 0) (match-end 0))
  178.     (replace-match "")
  179.     (insert (getenv "ORGANIZATION"))
  180.     ))
  181.     )
  182.  
  183.  
  184.   )

A sample template looks like this:

insert.php
  1. <?php
  2. /**
  3.  *
  4.  * @@FILENAMENS@@
  5.  *
  6.  * Author: @@AUTHOR@@ <@@AUTHOREMAIL@@>
  7.  * Created: @@TODAY@@
  8.  * Copyright (c) @@YEAR@@ @@ORGANIZATION@@
  9.  *
  10.  */
  11.  
  12.  
  13. /**
  14.  * DOC BLOCK
  15.  */
  16.  

The auto-update-other-source-file function takes care of substituting the appropriate values in the template, resulting in something like the following:

sample.php
  1. <?php
  2. /**
  3.  *
  4.  * sample
  5.  *
  6.  * Author: Tamara Temple <tamara@tamaratemple.com>
  7.  * Created: 2011/10/07
  8.  * Copyright (c) 2011 Tamara Temple Web Development
  9.  *
  10.  */
  11.  
  12.  
  13. /**
  14.  * DOC BLOCK
  15.  */

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