all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Floyd Davidson <floyd@barrow.com>
Subject: Re: regexp / replacement for variable
Date: Tue, 24 Feb 2004 11:09:43 -0900	[thread overview]
Message-ID: <87znb8p7co.fld@barrow.com> (raw)
In-Reply-To: mailman.372.1077629684.340.help-gnu-emacs@gnu.org

Kevin Dziulko <weaselboy1976@yahoo.com> 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, floyd@barrow.com
  ;;;;  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 "nobody@home.here"))

  (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)                         floyd@barrow.com

  parent reply	other threads:[~2004-02-24 20:09 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.372.1077629684.340.help-gnu-emacs@gnu.org>
2004-02-24 14:03 ` regexp / replacement for variable Joakim Hove
2004-02-24 20:09 ` Floyd Davidson [this message]
     [not found] <mailman.262.1077404990.340.help-gnu-emacs@gnu.org>
2004-02-22  0:41 ` Kin Cho
2004-02-22  0:51 ` lawrence mitchell
2004-02-22 12:18   ` Jan Misol
2004-02-22 12:14 ` Floyd Davidson
2004-02-21 23:08 Jan Misol
2004-02-24 13:31 ` Kevin Dziulko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87znb8p7co.fld@barrow.com \
    --to=floyd@barrow.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.