开发者

GNU Emacs: skeleton-mode, is it still used?

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
  &am开发者_StackOverflowp; \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


For the record: even another 7 years later I am still very happy with skeletons. Lots of them available over there in my init files: https://github.com/ska2342/ska-init-files/blob/master/dot.emacs.d/init.el


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜