Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-19 18:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found