tags:

views:

155

answers:

3

Hi,

given all the possible solutions to have a template system with GNU Emacs, what do people use today ? I am still using skeleton-mode but as I read it here and there, we must be really few to do so.

What are you using and why ? (maybe I could switch to a more popular tool).

For example, given this snippet:

(define-skeleton mwe:cl-defpackage-skeleton
  "Inserts a Common Lisp DEFPACKAGE skeleton."
  (skeleton-read "Package: " (if v1
                                 (file-name-sans-extension
                                  (file-name-nondirectory
                                   (buffer-file-name)))))
  (if (setq v1 (bobp)) ";;; -*- Mode:Lisp; Syntax:ANSI-Common-Lisp;")
  & (if buffer-file-coding-system
        (concat " Coding:"
                (symbol-name 
                 (coding-system-get buffer-file-coding-system 
                                    'mime-charset))))
  & " -*-"
  & \n
  & \n "(defpackage #:" str
  \n "(:nicknames" ("Nickname: " " #:" str) & ")" | '(kill-whole-line -1)
  \n "(:use #:CL" ((slime-read-package-name "USEd package: ") " #:" str) ")"
  ")" \n
  \n
  (if v1 "(in-package #:") & str & ")" & \n &
  \n
  _)

(credits: http://www.foldr.org/~michaelw/log/programming/lisp/defpackage-skeleton)

which (modern) template mode could do the same (and how ;)) ?

Cheers

+2  A: 

I haven't used skeleton mode much, but I use YASnippet while coding in Ruby and C. Its pretty useful, but I suspect skeleton mode is far more powerful.

Sid H
Can you elaborate a little bit with yasnippet: what is so powerful about them ? Can it do things as complex as skeleton ?
Xavier Maillard
The Yasnippet website is at http://code.google.com/p/yasnippet/. Its derived from Textmate's snippets and uses similar syntax for templating.
Sid H
I don't know skeleton. I use yasnippet. It's easy to use: just specify the templates in text files. It's extensible: I can code snippets (templats) that consist of plain text, or, elisp code. It's general purpose: I can trigger a snippet interactively, or I can inject snippets from elisp - for instance, from a completion package, I can do a lookip in a symbol table, then depending on the outcome, inject a dynamically-created template that contains the method signature. It just works. Never had a reason to look at skeleton.
Cheeso
I would agree with you if yasnippet had been inventer earlier :D The fact is, skeleton is older and part of the official GNU Emacs distribution. This is why I learned it. The big cons of skeleton is really its lack of litterature making one think it is totally abandonware -i.e. there is almost no reference of it anywhere and it is hard to share/to ask something about it.Now, if yasnippet offers the same features, why not try it ? ;)
Xavier Maillard
@Cheeso: is your setting available publicly somewhere ?
Xavier Maillard
yes, I provided my own answer: http://stackoverflow.com/questions/2866759/gnu-emacs-skeleton-mode-is-it-still-used/2889878#2889878
Cheeso
+1  A: 

The emacs wiki lists Yasnippet as a possible replacement for skeleton. The snippets that come with yasnippet are pretty good, but you should really write your own, as the true power lies there.

Mica
+2  A: 

I use yasnippet.

In my emacs I have this:

(require 'yasnippet-bundle)

In my hook for each mode where I want to use snippets (like my c-mode hook, etc), I have this:

(yas/minor-mode-on)

The "static" snippets I use are available, in the directory structure I use, here:

http://cheeso.members.winisp.net/srcview.aspx?dir=emacs&file=snippets.zip

You need to create the bundle .el file mentioned above, once, when any of the snippets change. do it this way:

(require 'yasnippet)
(yas/compile-bundle 
 ; the starting point
 "c:/your/path/yasnippet.el"

 ; the bundle file to generate
 "c:/your/path/yasnippet-bundle.el" 

 ; the snippet dir from which to generate the bundle
 "c:/your/path/snippets")

That's it!

Then, when I'm in a C# file and type for<TAB>, I get a template with a for loop. And so on.


I also use yasnippet with dynamic snippet templates. A C# code-completion module I wrote calls yas/expand-snippet with a dynamically constructed string that defines the template to expand.

So, you can type

  MyType.Method(<COMPLETE>

...where <COMPLETE> is the code-completion key, and the code-completion module does the lookup on the MyType.Method(, then builds a menu of choices, and pops it up. When the user selects a choice from the menu, the code-completion module builds the template, containing fields for each of the arguments for the selected method. Then it calls yas/expand-snippet and that template is injected into the buffer, just as if it had been a static template. In the dynamically-generated template, each argument to the method gets a "typeover" field, and I just fill it in, tabbing through the fields. Pretty nice.

This "dynamic snippet" idea would work with any code-completion engine. You just need a way to map from a method or function signature, like this:

  function(int arg1, string arg2, char arg3)

to a yasnippet template definition string, which looks like this:

  function(${1:int arg1}, ${2:string arg2}, ${3:char arg3}) 

And that's a pretty trivial piece of elisp.

Cheeso
Pretty neat. I will probably give Yasnippet a try and, you should definetely do a screencast :)
Xavier Maillard