help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: regexp / replacement for variable


From: Floyd Davidson
Subject: Re: regexp / replacement for variable
Date: Tue, 24 Feb 2004 11:09:43 -0900
User-agent: gnus 5.10.6/XEmacs 21.4.15/Linux 2.6.0

Kevin Dziulko <address@hidden> wrote:
>I find file templates easier.  I have this in my .emacs:
>
> 
>
>;; automaticlly start new files with specific templates
>
>(autoload 'auto-insert "autoinsert")
>     (add-hook 'find-file-not-found-hooks 'auto-insert)
>     (setq auto-insert-query nil)
>     (setq auto-insert-directory "myhomedir/.templates/")
>
>     (setq auto-insert-alist
>        '(("\\.sh$"       . "template.sh")
>          ("\\.pl$"       . "template.pl")))
>

There is certainly some advantage to using setq to define your
own auto-insert-alist, but it also deletes all of the default
entries too.  To simply add to the auto-insert-alist, or to
replace an existing entry, define-auto-insert could be used to
add the two above entries like this,

   (define-auto-insert "\\.sh$" "template.sh")
   (define-auto-insert "\\.pl$" "template.pl")

You might find some of the default entries useful as is (in
particular the one for HTML is fairly good), or they can also be
used as good examples of what can be done when coding up your
own customized templates.

The defaults are (for GNU Emacs and XEmacs respectively) in files,

   .../lisp/autoinsert.el.
   .../xemacs-packages/lisp/text-modes/autoinsert.el

The builtin defaults for C/C++ headers and programs, eLisp,
HTML, shells, and LaTex don't use a template file, and instead
use code to generate text to be inserted.  For Plain TeX,
BibTeX, Makefiles, and ada it just specifies a template file.

Unfortunately none of the defaults provide an example of using a
template file that is then manipulated by code from the alist
entry.  What I've done is delete all of the defaults by using
setq too, and than also using template files, but all of them
are 1) very similar in form, and 2) all of them are manipulated
by code after being inserted.  Plus (and this idea came from the
HTML default template) a date/time stamp for the file creation
time, and another bit of lisp code that is added to
local-write-file-hooks is used to add and/or update a "Last
update: " time stamp every time the file is modified.

Below is one of the least complex examples.  It is for lisp
programs.  A suitable template file might look like this,

  ;;;;
  ;;;;  **title**
  ;;;;
  ;;;;  **timestamp**
  ;;;;
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;; add the working directory...
  (add-to-list 'load-path "**working_directory**")

though of course it might be (and in reality mine is)
significantly more complex too!  The actual text inserted into
the new file would look like this,

  ;;;;
  ;;;;  foo.el             --
  ;;;;
  ;;;;  Copyright 2004 by Floyd L. Davidson, address@hidden
  ;;;;  File created:  Tue Feb 24 10:32:57 2004
  ;;;;
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;; add the working directory...
  (add-to-list 'load-path "/home/floyd/lispsrc/")

The cursor is left two spaces past the "--", where in my
case I would then write a short description of what this
file is.  In my case, though the code to make this happen
is not part of the example below, when this file is modified,
if it is written to disk another line will be added below
the one that gives the "File created" date/time, which is
formated the same but says "Last updated:".  That line is
then automatically changed any time that file is edited.

Here is the alist entry to manipulate a template file.

  (("\\.el\\'" "Emacs Lisp Program Template") nil
    ;;
    ;; It is necessary to wrap the following code inside a
    ;; (let ...) function to prevent each command's return
    ;; value from being inserted into the file.  Inside the
    ;; (let ...) function insertions are made the usual way,
    ;; otherwise all strings are inserted by the auto-insert
    ;; package when this executes.
    ;;
    (let ((dummy0 "xxxxx"))
      (goto-char (point-min))
      (insert-file (concat "" auto-insert-directory "lisp-program.inc"))
      (goto-char (point-min))
      (replace-string "**title**"
          (concat (file-name-nondirectory buffer-file-name)
              (when (> 16 (string-width (file-name-nondirectory 
buffer-file-name)))
                (make-string
                 (- 17 (string-width (file-name-nondirectory 
buffer-file-name))) ?\ ))
              "  --"))
      (goto-char (point-min))
      (replace-string "**timestamp**" (concat "File created:  " 
(current-time-string)))
      (beginning-of-line)
      (insert ";;;;  Copyright ")
      (insert (substring (current-time-string) -4 nil) " by ")
      (insert (user-full-name) ", ")
      (insert user-mail-address "\n")
      (goto-char (point-min))
      (replace-string "**working_directory**" (file-name-directory 
buffer-file-name))
      (goto-char (point-min))
      (replace-regexp "--$" "--  ")
      (message "")))

Note that this /requires/ user-mail-address to return a string.
If it is not defined by set-custom-variables, something should
be put in ~/.emacs to cause it to be defined.  Here is an
example, which does other useful things too,

  ;;
  ;; Can't do this in init.el because custom.el is loaded later.
  ;;
  (add-hook 'select-frame-hook    (lambda ()
  "Set email address correctly before auto-insertion."
  (if (or (not user-mail-address) (> 3 (string-width user-mail-address)))
      (setq user-mail-address
            (concat (user-login-name) "@"
                    (or mail-host-address (system-name)))))
  (when (or (not user-mail-address) (> 3 (string-width user-mail-address)))
    (setq user-mail-address "address@hidden"))

  (setq html-helper-address-string
        (concat "<a href=\"mailto:";  user-mail-address
                "\"> Contact by email:  " (user-full-name) "</a>"))))

--
Floyd L. Davidson           <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska)                         address@hidden


reply via email to

[Prev in Thread] Current Thread [Next in Thread]