Yes, this ain't perl code, it's elisp, but it is for perl coders, since it shows how to auto-create some often used fragments.
The auto-insert feature come into action whenever you load an empty file for the according mode. It then uses a skeleton. You can define your own skeletons and bind them to keys for several constructs.
1. Auto-Insert
1.1 you need to enable it:
(add-hook 'find-file-hooks '(lambda () (auto-insert) ))
1.2. Now define the auto-insert template/skeleton (this is only a snippet of my setup which features other languages, too)
(setq auto-insert-copyright (user-full-name)) (setq auto-insert-alist '( ((perl-mode . "Perl Program") nil "#! /usr/bin/perl -w\n\n" "# File: " (file-name-nondirectory buffer-file-name) "\n" "# Time-stamp: <>\n#\n" "# Copyright (C) " (substring (current-time-string) -4) " by " auto-insert-copyright "\n#\n" "# Author: "(user-full-name) "\n#\n" (progn (save-buffer) (shell-command (format "chmod +x %s" (buffer-file-name))) "") "# Description:\n# " _ "\n" ) ))
2. Skeletons
2.1 you need to define some skeletons:
(define-skeleton ska-skel-perl-sub "Insert a perl subroutine with arguments." "Subroutine name: " "sub " str " {" \n "my (" ("Argument name: " "$" str ", ") -2 ") = @_;" "\n" \n _ \n "}" '(progn (indent-according-to-mode) nil) \n) ;; any other way to get a time format string? (require 'time-stamp) (define-skeleton ska-skel-perl-prog-id "Insert perl program identification." (nil) (insert-char ?# 60) \n "#" (insert-char ? 18) "PROGRAMM IDENTIFICATION" (insert-char ? 17) "#"\n (insert-char ?# 60) \n "my $program = \"" (file-name-nondirectory (file-name-sans-extension buffer-file-name)) "\";"\n "my $filedate=\"" (time-stamp-strftime "%y-%m-%d") "\";"\n "my $fileversion=\"0.01\";"\n "my $copyright = \"Copyright (C) " (substring (current-time-string) -4) " by " (user-full-name) "\";"\n "my $title = \"$program $fileversion, $filedate - $copyright\";" ) (define-skeleton ska-skel-perl-options "Insert perl program getopt stuff." (nil) (save-excursion (if (re-search-backward "^use" (beginning-of-buffer) t) (progn (end-of-line) (newline-and-indent)) (progn (beginning-of-buffer) (while (string-match "^#" (char-to-string (char-after) +)) (forward-line)))) (insert "use Getopt::Long;") (indent-according-to-mode) (insert "\n") nil) (insert-char ?# 60) \n "#" (insert-char ? 26) "OPTIONS" (insert-char ? 25) "#"\n (insert-char ?# 60) \n "my $opt_help = 0;"\n "my $opt_version = 0;"\n "my $ret = GetOptions("\n "\"help!\","\n "\"version!\""\n ");"\n )
(this last uses some kind of heuristic to place the use-statement, this may be buggy) 2.2 now bind to some keys, e.g.:
(local-set-key '[(control b) (control s)] 'ska-skel-perl-sub) (local-set-key '[(control b) (control i)] 'ska-skel-perl-prog-id) (local-set-key '[(control b) (control o)] 'ska-skel-perl-options)
(this assumes that you call it from the cperl-mode-hook (so that the local keymap is the correct one) and that you may use Ctrl-b as a prefix.

3. Documentation
For more info and docs see:

C-h f skeleton-insert
and the files skeleton.el, sh-script.el

Shameless Self Ad: the full code of my newly created xemacs config will be available on www.skamphausen.de/xemacs, soon. I'm still in the process of testing things, then it's time to update my pages. Don't despair: though the pages are german the code is english. :) I hope this helps.

Regards... Stefan


In reply to (X)Emacs Skeletons and Auto-Insert by stefan k

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.