Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

(X)Emacs Skeletons and Auto-Insert

by stefan k (Curate)
on Oct 05, 2001 at 14:09 UTC ( #116951=snippet: print w/ replies, xml ) Need Help??

Description: 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

Comment on (X)Emacs Skeletons and Auto-Insert
Select or Download Code

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://116951]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2015-07-04 07:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (58 votes), past polls