From: dltest@castle.ed.ac.uk (Kevin Davidson)
Newsgroups: gnu.emacs.sources,alt.sources
Subject: Emacs Lisp `improved' auto-insert.el
Keywords: emacs auto-insert template dmacro
Date: 26 Aug 92 10:29:03 GMT
Followup-To: comp.emacs
Organization: Edinburgh University

# I mentioned this on comp.emacs a while ago. I finish my current job at the
# end of the week, so I may as well post this. I *do* use it myself, but I'm
# gradually weaning myself onto Wayne Mesard's Dmacro. If you're *not* an
# elisp programmer then this auto-insert code may be for you.
# Similarly if you have user's who would like to use something like this, but
# haven't the time/patience to learn how to use dmacro for simple files.
#
# I include my insert files as examples. If you're a Perl programmer, the
# perl-insert.pl may be of interest esp. if you don't have wrapman(1).
# The perl produced is runnable under Perl and nroffable as a man page.
#
# Please mail me any suggestions and notify me of any bugs.
#	.Kevin.
#Kevin.Davidson@ed.ac.uk | dltest@castle.ed.ac.uk | dltest@festival.ed.ac.uk
#  ``You are what you drink, and I'm a bitter man'' -- The Macc Lads


#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#	README.auto-insert
#	lib/lisp/auto-insert-tkld.el
#	lib/insert/c-insert.c
#	lib/insert/elisp-insert.el
#	lib/insert/h-insert.h
#	lib/insert/makefile.inc
#	lib/insert/perl-insert.pl
#	lib/insert/test-insert
# This archive created: Wed Aug 26 10:43:20 1992
export PATH; PATH=/bin:/usr/bin:$PATH
echo shar: "extracting 'README.auto-insert'" '(2576 characters)'
if test -f 'README.auto-insert'
then
	echo shar: "will not over-write existing file 'README.auto-insert'"
else
sed 's/^	X//' << \SHAR_EOF > 'README.auto-insert'
	XWhat is auto-insert ?
	XAuto insertion allows you to set up skeletons for the files you commonly
	Xcreate (C source files, TeX documents or whatever).
	XThe idea is to give all your files a common layout.
	X
	XThis is a beta version. Please comment on it. Does it work ? Does it do what
	Xyou want ? Is the code really badly written ? :-)
	X
	XDifferences from distributed autoinsert.el:
	X1) Files to insert are searched for in a list of directories, rather than just
	X   a single directory.
	X2) Auto insertion can be run interactively: M-x insert-auto-insert-file will
	X   ask for a file to be inserted into the current buffer.
	X3) During insertion a variety of %-escapes are expanded to common strings:
	X    %u is replaced by user's login name
	X    %U is user's full name
	X    %h is the name of the user's machine
	X    %m is the user's mail address (from the variable mail-self-address)
	X    %f is the file name (without directory part)
	X    %F is the file name capitalised
	X    %b is the base file name, without suffix
	X    %B is the base file name capitalised
	X    %e is the file's suffix (extension)
	X    %E is the file's suffix capitalised
	X    %p is the full path name of the file
	X    %P is the directory part of the file name
	X    %d is the current date
	X    %y is the current year
	X    %% is a %
	X    %@ sets the initial position of `point'. (If you use this, then the whole
	X       buffer is not marked, so ^W will not remove the insertion)
	X  For instance a common C programming idiom in include files to prevent 
	Xmultiple inclusions is:
	X	#ifndef _FILENAME_H
	X	#define _FILENAME_H
	X	/* File contents here */
	X	#endif
	X  This can be created automatically from a .h insert file containing
	X	#ifndef _%B_%E
	X	#define _%B_%E
	X	%@
	X	#endif	
	X  Note that this will work correctly if two similarly named C and C++
	Xinclude files are used in a program  as one will define _FILE_H and the
	Xother _FILE_HPP, although I am not suggesting this is a smart thing to do :-)
	X
	XHow to install:
	XByte compile auto-insert-tkld.el and stick it in your elisp directory.
	XStick the insert files in ~/insert or somewhere.
	XAdd your insert directory to auto-insert-path in ~/.emacs if you need to.
	XPut (require 'auto-insert-tkld) in your ~/.emacs.
	XSet mail-self-address to a fully-qualified domain address.
	XModify auto-insert-alist to recognise the file types you edit.
	X
	XTry creating some new files.
	X
	XIf this seems too simple, you probably want to be using Wayne Mesard's
	XDmacro package.
	X
	X	.Kevin.
	XKevin.Davidson@ed.ac.uk | dltest@castle.ed.ac.uk | dltest@festival.ed.ac.uk
	X  ``You are what you drink, and I'm a bitter man'' -- The Macc Lads
SHAR_EOF
if test 2576 -ne "`wc -c < 'README.auto-insert'`"
then
	echo shar: "error transmitting 'README.auto-insert'" '(should have been 2576 characters)'
fi
fi
echo shar: "extracting 'auto-insert-tkld.el'" '(9670 characters)'
if test -f 'auto-insert-tkld.el'
then
	echo shar: "will not over-write existing file 'auto-insert-tkld.el'"
else
sed 's/^	X//' << \SHAR_EOF > 'auto-insert-tkld.el'
	X;; Automatic mode-dependent insertion of text into new files.
	X;; Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
	X;; Altered by tkld 1992.
	X
	X;; This file was part of GNU Emacs.
	X
	X;; GNU Emacs is distributed in the hope that it will be useful,
	X;; but WITHOUT ANY WARRANTY.  No author or distributor
	X;; accepts responsibility to anyone for the consequences of using it
	X;; or for whether it serves any particular purpose or works at all,
	X;; unless he says so in writing.  Refer to the GNU Emacs General Public
	X;; License for full details.
	X
	X;; Everyone is granted permission to copy, modify and redistribute
	X;; GNU Emacs, but only under the conditions described in the
	X;; GNU Emacs General Public License.   A copy of this license is
	X;; supposed to have been given to you along with GNU Emacs so you
	X;; can know your rights and responsibilities.  It should be in a
	X;; file named COPYING.  Among other things, the copyright notice
	X;; and this notice must be preserved on all copies.
	X
	X;;; auto-insert-tkld.el
	X
	X;;;  Abstract:
	X;;;
	X;;;  The following defines an association list for files to be
	X;;;  automatically inserted when a new file is created, and a function
	X;;;  which automatically inserts these files; the idea is to insert
	X;;;  default files much as the mode is automatically set using
	X;;;  auto-mode-alist.
	X;;;
	X;;;  The auto-insert-alist consists of dotted pairs of
	X;;;  ( REGEXP . FILENAME ) where REGEXP is a regular expression, and
	X;;;  FILENAME is the file name of a file which is to be inserted into
	X;;;  all new files matching the regular expression with which it is
	X;;;  paired.
	X;;;
	X;;;  To use: 
	X;;;     load autoinsert.el, or (require 'auto-insert-tkld)
	X;;;     setq auto-insert-path to a list of appropriate values, which
	X;;;       must end in "/"
	X;;;     e.g. (setq auto-insert-path
	X;;;             (cons "/usr/local/lib/insert/" auto-insert-path))
	X;;;
	X;;;  Original Author:  Charlie Martin
	X;;;           Department of Computer Science and
	X;;;           National Biomedical Simulation Resource
	X;;;           Box 3709
	X;;;           Duke University Medical Center
	X;;;           Durham, NC 27710
	X;;;	      (crm@cs.duke.edu,mcnc!duke!crm) 
	X;;;
	X;;;  Date: Fri Jul  1 16:15:31 EDT 1988
	X;;; Much hacked about by Kevin Davidson <dltest@castle.ed.ac.uk>
	X;;;                                     <Kevin.Davidson@ed.ac.uk>
	X;;; 19th August 1992
	X;;; Whole buffer is marked. If auto-insertion is not wanted, ^W will remove
	X;;; it all if point has not been set by %@.
	X;;; Now replaces % escapes in inserted file:
	X;;; %u is replaced by user's login name
	X;;; %U is user's full name
	X;;; %h is the name of the user's machine
	X;;; %m is the user's mail address (from the variable mail-self-address)
	X;;; %f is the file name (without directory part)
	X;;; %F is the file name capitalised
	X;;; %b is the base file name, without suffix
	X;;; %B is the base file name capitalised
	X;;; %e is the file's suffix (extension)
	X;;; %E is the file's suffix capitalised
	X;;; %p is the full path name of the file
	X;;; %P is the directory part of the file name
	X;;; %d is the current date
	X;;; %y is the current year
	X;;; %% is a %
	X;;; %@ sets the initial position of `point'.                              
	X;;;
	X;;; Things to do:
	X;;; I don't like the look of the %-expansion code :-( How *should* it be done?
	X;;; Are there any other *useful* %-escapes ?
	X;;;   (ie %s for a smiley :-); %c for complete code of program etc.)
	X;;; How about comments ? %{ stuff %} ?
	X
	X(defconst auto-insert-version "1.2beta"
	X  "Report bugs to: Kevin Davidson <Kevin.Davidson@ed.ac.uk>")
	X
	X(defvar mail-self-address
	X  (concat "<" (user-login-name) "@" (system-name) ">" )
	X  "*Mail address of user.")
	X
	X(defvar current-year-string
	X  (substring (current-time-string) -4 nil)
	X  "*The current year.")
	X(defvar current-date-string
	X  (concat (substring (current-time-string) 0 11) current-year-string)
	X  "*Today's date.")
	X
	X(defvar auto-insert-alist '(("\\.tex$" . "tex-insert.tex")
	X			    ("\\.c$" . "c-insert.c")
	X			    ("\\.h[+p]*$" . "h-insert.h")
	X			    ("\\.el$" . "elisp-insert.el")
	X			    ("[Mm]akefile" . "makefile.inc")
	X			    ("\\.bib$" . "tex-insert.tex")
	X			    ("\\.pl$" . "perl-insert.pl"))
	X  "Alist specifying text to insert by default into a new file.
	XElements look like (REGEXP . FILENAME); if the new file's name
	Xmatches REGEXP, then the file FILENAME is inserted into the buffer.
	XOnly the first matching element is effective.")
	X
	X;;; Establish a default value for auto-insert-directory
	X(defvar auto-insert-path '("~/insert/" "~/lib/insert/")
	X  "*Directories from which auto-inserted files are taken.")
	X
	X(defun file-name-sans-suffix (name)
	X  "Return file name NAME without suffix.
	XThis is everything before the last dot."
	X  (substring name 0 (string-match "\\.[^.]*$" (file-name-nondirectory name))))
	X
	X(defun file-name-suffix (name)
	X  "Return the suffix of file NAME, or \"\" if none."
	X  (let ((pos (string-match "\\.[^.]*$" (file-name-nondirectory name))))
	X    (if pos
	X	(substring name (1+ pos) nil)
	X      "")))
	X
	X(defun insert-auto-insert-file (file)
	X  "Auto insert FILE into current buffer, performing %-expansions.
	X%u is replaced by user's login name                                   
	X%U is user's full name                                                
	X%h is the name of the user's machine                                  
	X%m is the user's mail address (from the variable mail-self-address)   
	X%f is the file name (without directory part)                          
	X%F is the file name capitalised                                       
	X%b is the base file name, without suffix                              
	X%B is the base file name capitalised                                  
	X%e is the file's suffix (extension)                                   
	X%E is the file's suffix capitalised                                   
	X%p is the full path name of the file                                  
	X%P is the directory part of the file name                             
	X%d is the current date                                                
	X%y is the current year                                                
	X%% is a %                                                             
	X%@ sets the initial position of `point'."
	X  (interactive "fAuto insert file: ")
	X  (if (file-readable-p file)
	X      (let ((current-year-string (substring (current-time-string) -4 nil))
	X	    (case-fold-search nil)
	X	    (point-set nil)
	X	    (buffer-file-name-nondirectory (file-name-nondirectory buffer-file-name))
	X	    (current-date-string (concat (substring (current-time-string) 0 11) current-year-string)))
	X	(insert-file-contents file)
	X	(while (not (eobp))
	X	  (if (looking-at "%")
	X	      (progn
	X		(delete-char 1)
	X		(cond
	X		 ((looking-at "h")
	X		  (delete-char 1)
	X		  (insert (system-name)))
	X		 ((looking-at "m")
	X		  (delete-char 1)
	X		  (insert mail-self-address))
	X		 ((looking-at "u")
	X		  (delete-char 1)
	X		  (insert (user-login-name)))
	X		 ((looking-at "U")
	X		  (delete-char 1)
	X		  (insert (user-full-name)))
	X		 ((looking-at "f")
	X		  (delete-char 1)
	X		  (insert buffer-file-name-nondirectory))
	X		 ((looking-at "F")
	X		  (delete-char 1)
	X		  (insert (upcase buffer-file-name-nondirectory)))
	X		 ((looking-at "b")
	X		  (delete-char 1)
	X		  (insert (file-name-sans-suffix buffer-file-name-nondirectory)))
	X		 ((looking-at "B")
	X		  (delete-char 1)
	X		  (insert (upcase (file-name-sans-suffix buffer-file-name-nondirectory))))
	X		 ((looking-at "e")
	X		  (delete-char 1)
	X		  (insert (file-name-suffix buffer-file-name)))
	X		 ((looking-at "E")
	X		  (delete-char 1)
	X		  (insert (upcase (file-name-suffix buffer-file-name))))
	X		 ((looking-at "p")
	X		  (delete-char 1)
	X		  (insert buffer-file-name))
	X		 ((looking-at "P")
	X		  (delete-char 1)
	X		  (insert (file-name-directory buffer-file-name)))
	X		 ((looking-at "d")
	X		  (delete-char 1)
	X		  (insert current-date-string))
	X		 ((looking-at "y")
	X		  (delete-char 1)
	X		  (insert current-year-string))
	X		 ((looking-at "@")
	X		  (if (not point-set)	; Save point to go back here later
	X		      (setq point-set (point)))
	X		  (delete-char 1))
	X		 ((looking-at "%") (forward-char 1))
	X		 ((looking-at ".") ;Default
	X		  (message "Auto-insert: unknown `%%' escape: %%%s" (buffer-substring (match-beginning 0) (match-end 0)))
	X		  (sleep-for 1))))
	X	    (forward-char 1)))
	X	(if point-set
	X	    (goto-char point-set)
	X	    (mark-whole-buffer)))	;This is probably the wrong thing to
	X					;do when called interactively...
	X    (message "Auto-insert: file %s not found" file)
	X    (sleep-for 1)))
	X
	X(defun insert-auto-insert-files ()
	X  "Insert default contents into a new file.
	XMatches the visited file name against the elements of `auto-insert-alist'."
	X  (let ((alist auto-insert-alist)
	X	;; remove backup suffixes from file name
	X        (name (file-name-sans-versions buffer-file-name))
	X        (insert-file nil))
	X
	X    ;; find first matching alist entry
	X    (while (and (not insert-file) alist)
	X      (if (string-match (car (car alist)) name)
	X          (setq insert-file (cdr (car alist)))
	X        (setq alist (cdr alist))))
	X
	X    ;; Now, if we found an appropriate insert file, insert it
	X    (if insert-file
	X	(let* ((dirs auto-insert-path) (file (concat (car dirs) insert-file)))
	X	  (while (and (not (file-readable-p file)) dirs)
	X	    (setq dirs (cdr dirs))
	X	    (setq file (concat (car dirs) insert-file)))
	X	  (insert-auto-insert-file file)))))
	X
	X;; Make this feature take effect when a nonexistent file is visited.
	X;; Make this the last in the list, so rcs-sk can check out files that
	X;; really should exist already.
	X(or (memq 'insert-auto-insert-files find-file-not-found-hooks)
	X    (setq find-file-not-found-hooks
	X	  (append find-file-not-found-hooks '(insert-auto-insert-files))))
	X
	X(provide 'auto-insert-tkld)		; We're here
	X
SHAR_EOF
if test 9670 -ne "`wc -c < 'auto-insert-tkld.el'`"
then
	echo shar: "error transmitting 'auto-insert-tkld.el'" '(should have been 9670 characters)'
fi
fi
echo shar: "extracting 'c-insert.c'" '(86 characters)'
if test -f 'c-insert.c'
then
	echo shar: "will not over-write existing file 'c-insert.c'"
else
sed 's/^	X//' << \SHAR_EOF > 'c-insert.c'
	X/* %f	-- %U %d
	X *
	X * $Log$
	X */
	X
	X#ifndef lint
	Xstatic char *rcsid = "$Header$";
	X#endif
	X
SHAR_EOF
if test 86 -ne "`wc -c < 'c-insert.c'`"
then
	echo shar: "error transmitting 'c-insert.c'" '(should have been 86 characters)'
fi
fi
echo shar: "extracting 'elisp-insert.el'" '(862 characters)'
if test -f 'elisp-insert.el'
then
	echo shar: "will not over-write existing file 'elisp-insert.el'"
else
sed 's/^	X//' << \SHAR_EOF > 'elisp-insert.el'
	X;;; %F - Short description here
	X;;; Copyright (C) %y %U
	X;;;
	X;;; This program is free software; you can redistribute it and/or modify
	X;;; it under the terms of the GNU General Public License as published by
	X;;; the Free Software Foundation; either version 1, or (at your option)
	X;;; any later version.
	X;;;
	X;;; This program is distributed in the hope that it will be useful,
	X;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
	X;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	X;;; GNU General Public License for more details.
	X;;;
	X;;; A copy of the GNU General Public License can be obtained from this
	X;;; program's author (send electronic mail to %m) or from
	X;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
	X;;; 02139, USA.
	X;;;
	X
	X(defconst %b-version (substring "$Revision$" 11 -2)
	X  "$Id$
	X
	XReport bugs to: %U %m")
SHAR_EOF
if test 862 -ne "`wc -c < 'elisp-insert.el'`"
then
	echo shar: "error transmitting 'elisp-insert.el'" '(should have been 862 characters)'
fi
fi
echo shar: "extracting 'h-insert.h'" '(79 characters)'
if test -f 'h-insert.h'
then
	echo shar: "will not over-write existing file 'h-insert.h'"
else
sed 's/^	X//' << \SHAR_EOF > 'h-insert.h'
	X/* %f	-- %U
	X *
	X * $Header$
	X * $Log$
	X */
	X
	X#ifndef _%B_%E
	X#define _%B_%E
	X
	X#endif
SHAR_EOF
if test 79 -ne "`wc -c < 'h-insert.h'`"
then
	echo shar: "error transmitting 'h-insert.h'" '(should have been 79 characters)'
fi
fi
echo shar: "extracting 'makefile.inc'" '(113 characters)'
if test -f 'makefile.inc'
then
	echo shar: "will not over-write existing file 'makefile.inc'"
else
sed 's/^	X//' << \SHAR_EOF > 'makefile.inc'
	X# Makefile for %@
	X# Copyright (C) %y %U %m
	X# $Header$
	X# $Log$
	X
	XCC = gcc
	XCFLAGS = -g
	X
	XOBJS =
	XSRCS = $(OBJS:.o=.c)
SHAR_EOF
if test 113 -ne "`wc -c < 'makefile.inc'`"
then
	echo shar: "error transmitting 'makefile.inc'" '(should have been 113 characters)'
fi
fi
echo shar: "extracting 'perl-insert.pl'" '(749 characters)'
if test -f 'perl-insert.pl'
then
	echo shar: "will not over-write existing file 'perl-insert.pl'"
else
sed 's/^	X//' << \SHAR_EOF > 'perl-insert.pl'
	X#!/usr/local/bin/perl --  # -*-Perl-*-
	X'/bin/true' && eval "exec /usr/local/bin/perl -S $0 $*"
	X    if $running_under_some_shell;
	X'di';
	X'ig00';
	X# %f	-- %U
	X#
	X# $Log$
	X$RCSHEADER = "$Header$";
	X
	X########################################################
	X
	X	# These next few lines are legal in both Perl and Nroff.
	X
	X.00;		# finish .ig
	X 
	X'di		\" finish diversion--previous line must be blank
	X.nr nl 0-1	\" fake up transition to first page again
	X.nr %% 0		\" start at page 1
	X'; __END__	#### From here on it's a standard manual page ####
	X.TH %B 1 local "EUCS Datalibrary"
	X.SH NAME
	X%b \- brief description
	X.SH SYNOPSIS
	X.B %b [options] [files]
	X.SH DESCRIPTION
	X.SH OPTIONS
	X.SH ENVIRONMENT
	X.SH EXAMPLES
	X.SH FILES
	X.SH AUTHOR
	X%U
	X.SH SEE ALSO
	X.SH DIAGNOSTICS
	X.SH BUGS
SHAR_EOF
if test 749 -ne "`wc -c < 'perl-insert.pl'`"
then
	echo shar: "error transmitting 'perl-insert.pl'" '(should have been 749 characters)'
fi
fi
echo shar: "extracting 'test-insert'" '(404 characters)'
if test -f 'test-insert'
then
	echo shar: "will not over-write existing file 'test-insert'"
else
sed 's/^	X//' << \SHAR_EOF > 'test-insert'
	X%%f %f (the file name)
	X%%F %F (THE FILE NAME)
	X%%b %b (base of file name)
	X%%B %B (BASE OF FILE NAME)
	X%%e %e (file extension/suffix)
	X%%E %E (FILE EXTENSION)
	X%%p %p (full pathname of file)
	X%%P %P (directory part of file name)
	X
	X%%@ %@ (set point here)
	X
	X%%u %u (user name)
	X%%U %U (full name)
	X%%m %m (mail address)
	X%%h %h (hostname)
	X
	XAnd another %@ which should be ignored
	X
	X%%d %d (date)
	X%%y %y (year)
	X%%%% %%
SHAR_EOF
if test 404 -ne "`wc -c < 'test-insert'`"
then
	echo shar: "error transmitting 'test-insert'" '(should have been 404 characters)'
fi
fi
exit 0
#	End of shell archive
-- 
	.Kevin.
Kevin.Davidson@ed.ac.uk | dltest@castle.ed.ac.uk | dltest@festival.ed.ac.uk
  ``You are what you drink, and I'm a bitter man'' -- The Macc Lads
Newsgroups: gnu.emacs.sources