From: Andreas Gerler Date: Mon, 21 Dec 2015 10:19:39 +0000 (+0100) Subject: use yasnippets as file templates. X-Git-Url: http://git.bundesbrandschatzamt.de/?a=commitdiff_plain;h=1f1c70a372e709003c1e8f274b0d378ba9e7eda0;p=emacsconfig.git use yasnippets as file templates. borrowed from Howard Abrams. --- diff --git a/bba.org b/bba.org index 9ec51f65..ecc265e7 100644 --- a/bba.org +++ b/bba.org @@ -385,16 +385,25 @@ My first thought: Nice. But do I have a use case? After half a year I cannot live without it. I write most of my [[http://www.cfengine.com][CFEngine]] code with snippets. Some help for Perl and even for org-mode. Thought about using snippets for shell-mode and eshell-mode? - +=ha-autoinsert-yas-expand= copied from Howard Abrams. #+BEGIN_SRC emacs-lisp (add-to-list 'load-path "~/.emacs.d/elpa/yasnippet-20150415.244") (require 'yasnippet) (setq yas-snippet-dirs '("~/.emacs.d/snippets")) (yas-global-mode 1) + (auto-insert-mode 1) + (setq auto-insert-directory "~/.emacs.d/templates") (define-key global-map "\C-cy" 'yas-insert-snippet) (if (>= emacs-major-version 24) (add-to-list 'yas-prompt-functions 'yas-shk-helm-prompt)) + (defun ha-autoinsert-yas-expand() + "Replace text in yasnippet template." + (yas-expand-snippet (buffer-string) (point-min) (point-max))) + (define-auto-insert "\\.sh$" ["default-sh.sh" ha-autoinsert-yas-expand]) + (define-auto-insert "\\.pl$" ["default-pl.pl" ha-autoinsert-yas-expand]) + + #+END_SRC From http://emacswiki.org/emacs/Yasnippet @@ -490,16 +499,6 @@ Probably the best help to keep track of the not every day used keybindings. #+END_SRC -* template support - -Some default templates for new files are stored in [[templates/]]. -No need to start .pl .sh from scratch. - -#+BEGIN_SRC emacs-lisp - (require 'template) - (template-initialize) -#+END_SRC - * thesaurus #+BEGIN_SRC emacs-lisp diff --git a/templates/default-pl.pl b/templates/default-pl.pl new file mode 100755 index 00000000..de51218b --- /dev/null +++ b/templates/default-pl.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl +use strict; +use warnings; +use Getopt::Long; +use Pod::Usage; + +my %params; + +=head1 NAME + +`( file-name-nondirectory (buffer-file-name))` - + +=head1 DESCRIPTION + +$0 + +=head1 SYNOPSIS + +=head1 EXAMPLES + +=head1 SUBS + +=head2 main + +=cut + +sub main { + + my $result = GetOptions ( + "help|h" => \$params{help}, + "verbose|v" => \$params{verbose}, + "man|m" => \$params{man} + ); + + if ( $params{man} ) { pod2usage( -verbose => 2 ); } + + if ( ! ( $server ) || $params{help} ) + { + pod2usage(1); + } + + +} + +main(); + +=head1 AUTHOR + + +`(user-full-name)` <`user-mail-address`> + +=head1 LICENSE + +This program is free software, you can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut + +1; diff --git a/templates/default-sh.sh b/templates/default-sh.sh new file mode 100644 index 00000000..9971fa5b --- /dev/null +++ b/templates/default-sh.sh @@ -0,0 +1,19 @@ +#!/bin/${1:sh} +# ---------------------------------------------------------------------- +# `(upcase (file-name-nondirectory (file-name-sans-extension (buffer-file-name))))`: $0 +# ---------------------------------------------------------------------- + + OPT1="default value" # Setup a default value + OPT2=0 + + while getopts "d:s" o + do case "$o" in + d) OPT1="$OPTARG";; + s) OPT2=1;; + [?]) print >&2 "Usage: \$0 [-s] [-d seplist] file ..." + exit 1;; + esac + done + shift $(expr $OPTIND - 1) + +# ----------------------------------------------------------------------