<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <HTML> <HEAD> <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9"> <TITLE>LinuxDoc+Emacs+Ispell-HOWTO: Dirty Tricks</TITLE> <LINK HREF="LinuxDoc+Emacs+Ispell-HOWTO-7.html" REL=next> <LINK HREF="LinuxDoc+Emacs+Ispell-HOWTO-5.html" REL=previous> <LINK HREF="LinuxDoc+Emacs+Ispell-HOWTO.html#toc6" REL=contents> </HEAD> <BODY> <A HREF="LinuxDoc+Emacs+Ispell-HOWTO-7.html">Next</A> <A HREF="LinuxDoc+Emacs+Ispell-HOWTO-5.html">Previous</A> <A HREF="LinuxDoc+Emacs+Ispell-HOWTO.html#toc6">Contents</A> <HR> <H2><A NAME="s6">6. Dirty Tricks</A> </H2> <H2><A NAME="ss6.1">6.1 Inserting a header automatically</A> </H2> <P>Emacs allows you to <EM>hook</EM> some actions to any event (opening of a file, saving, running a new mode, etc). <P> <P>The <B>autoinsert</B> library uses this feature: when you open a new file under Emacs, this library inserts, according to the type of the file, a <EM>standard</EM> header. <P> <P>In our case, this <EM>standard</EM> header could well be the part declaring the document type (LinuxDoc), the title, the author, and the date. <P> <P>I will describe here two ways to insert such a header. You could insert a template file containing the information to insert, or you could run an <B>elisp</B> routine. <P> <H3>by inserting a file </H3> <P>You must first tell Emacs to run the <CODE>auto-insert</CODE> when opening a file, then to read the <B>autoinsert</B> library which declares the <CODE>auto-insert-alist</CODE> list which we need to change. This list defines the header to insert for each file type. By default, the file to insert must be in the <CODE>~/insert/</CODE> directory, but it is possible to redefine the <CODE>auto-insert-directory</CODE> variable if you want to put it somewhere else. <P> <P>Add the following lines to your <CODE>.emacs</CODE> file to insert the <CODE>~/emacs/sgml-insert.sgml</CODE> file each time you open a new SGML file: <P> <PRE> (add-hook 'find-file-hooks 'auto-insert) (load-library "autoinsert") (setq auto-insert-directory "~/emacs/") (setq auto-insert-alist (append '((sgml-mode . "sgml-insert.sgml")) auto-insert-alist)) </PRE> <P>You can then write in the <CODE>~/emacs/sgml-insert.sgml</CODE> file your customised header, then re-run Emacs and open some <CODE>foobar.sgml</CODE> file. Emacs should ask you to confirm the automatic insertion, and if you answer yes, insert your header. <P> <H3>by running a routine </H3> <P>This works like before, but instead of setting the <CODE>auto-insert-alist</CODE> to a file to insert, you need to set it to a function to execute. This is how to proceed, taking for granted you want to write this function in a file named <CODE>~/emacs/sgml-header.el</CODE>. (there's no need to burden your <CODE>.emacs</CODE> file with such a function, as it may turn out to be quite long): <P> <PRE> (add-hook 'find-file-hooks 'auto-insert) (load-library "autoinsert") (add-to-list 'load-path "~/emacs") (load-library "sgml-header") (setq auto-insert-alist (append '(((sgml-mode . "SGML Mode") . insert-sgml-header)) auto-insert-alist)) </PRE> <P>You will find in <A HREF="LinuxDoc+Emacs+Ispell-HOWTO-7.html#insert-sgml-header">appendix</A> an example of <CODE>insert-sgml-header</CODE> function. <P> <P> <HR> <A HREF="LinuxDoc+Emacs+Ispell-HOWTO-7.html">Next</A> <A HREF="LinuxDoc+Emacs+Ispell-HOWTO-5.html">Previous</A> <A HREF="LinuxDoc+Emacs+Ispell-HOWTO.html#toc6">Contents</A> </BODY> </HTML>