From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Floyd Davidson Newsgroups: gmane.emacs.help Subject: Re: regexp / replacement for variable Date: Tue, 24 Feb 2004 11:09:43 -0900 Organization: __________ Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: <87znb8p7co.fld@barrow.com> References: NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: sea.gmane.org 1077655776 14291 80.91.224.253 (24 Feb 2004 20:49:36 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Tue, 24 Feb 2004 20:49:36 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Feb 24 21:49:25 2004 Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1AvjUa-00053O-00 for ; Tue, 24 Feb 2004 21:49:24 +0100 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.30) id 1AvjSP-0004q3-Rx for geh-help-gnu-emacs@m.gmane.org; Tue, 24 Feb 2004 15:47:09 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!logbridge.uoregon.edu!pln-w!spln!dex!extra.newsguy.com!newsp.newsguy.com!enews3 Original-Newsgroups: gnu.emacs.help Original-Lines: 145 Original-NNTP-Posting-Host: p-510.newsdawg.com User-Agent: gnus 5.10.6/XEmacs 21.4.15/Linux 2.6.0 Cancel-Lock: sha1:Qu8fKetyRJwwIq8u3ekUIJVWHKc= Original-Xref: shelby.stanford.edu gnu.emacs.help:121217 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.2 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.help:17169 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:17169 Kevin Dziulko 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 " Contact by email: " (user-full-name) "")))) -- Floyd L. Davidson Ukpeagvik (Barrow, Alaska) floyd@barrow.com