#+SETUPFILE: org-setup.inc #+TITLE: Writing snippets * Snippet development ** Quickly finding snippets There are some ways you can quickly find a snippet file or create a new one: - =M-x yas-new-snippet= Creates a new buffer with a template for making a new snippet. The buffer is in =snippet-mode= (see below). When you are done editing the new snippet, use =C-c C-c= to save it. This will prompt for a directory two steps: first, the snippet table (with a default based on the major mode you started in), and then then snippet collection directory (defaults to the first directory in =yas-snippet-dirs=. (See [[file:snippet-organization.org][Organizing Snippets]] for more detail on how snippets are organized.) - =M-x yas-find-snippets= Lets you find the snippet file in the directory the snippet was loaded from (if it exists) like =find-file-other-window=. The directory searching logic is similar to =M-x yas-new-snippet=. - =M-x yas-visit-snippet-file= Prompts you for possible snippet expansions like [[sym:yas-insert-snippet][=yas-insert-snippet=]], but instead of expanding it, takes you directly to the snippet definition's file, if it exists. Once you find this file it will be set to =snippet-mode= (see ahead) and you can start editing your snippet. ** Using the =snippet-mode= major mode There is a major mode =snippet-mode= to edit snippets. You can set the buffer to this mode with =M-x snippet-mode=. It provides reasonably useful syntax highlighting. Two commands are defined in this mode: - =M-x yas-load-snippet-buffer= When editing a snippet, this loads the snippet into the correct mode and menu. Bound to =C-c C-c= by default while in =snippet-mode=. - =M-x yas-tryout-snippet= When editing a snippet, this opens a new empty buffer, sets it to the appropriate major mode and inserts the snippet there, so you can see what it looks like. This is bound to =C-c C-t= while in =snippet-mode=. There are also /snippets for writing snippets/: =vars=, =$f= and =$m= :-). * File content A file defining a snippet generally contains the template to be expanded. Optionally, if the file contains a line of =# --=, the lines above it count as comments, some of which can be /directives/ (or meta data). Snippet directives look like =# property: value= and tweak certain snippets properties described below. If no =# --= is found, the whole file is considered the snippet template. Here's a typical example: #+BEGIN_SRC snippet # contributor: pluskid # name: __...__ # -- __${init}__ #+END_SRC Here's a list of currently supported directives: ** =# key:= snippet abbrev This is the probably the most important directive, it's the abbreviation you type to expand a snippet just before hitting the key that runs [[sym:yas-expand][=yas-expand=]]. If you don't specify this the snippet will not be expandable through the trigger mechanism. ** =# name:= snippet name This is a one-line description of the snippet. It will be displayed in the menu. It's a good idea to select a descriptive name for a snippet -- especially distinguishable among similar snippets. If you omit this name it will default to the file name the snippet was loaded from. ** =# condition:= snippet condition This is a piece of Emacs-lisp code. If a snippet has a condition, then it will only be expanded when the condition code evaluate to some non-nil value. See also [[sym:yas-buffer-local-condition][=yas-buffer-local-condition=]] in [[./snippet-expansion.org][Expanding snippets]] ** =# group:= snippet menu grouping When expanding/visiting snippets from the menu-bar menu, snippets for a given mode can be grouped into sub-menus . This is useful if one has too many snippets for a mode which will make the menu too long. The =# group:= property only affect menu construction (See [[./snippet-menu.org][the YASnippet menu]]) and the same effect can be achieved by grouping snippets into sub-directories and using the =.yas-make-groups= special file (for this see [[./snippet-organization.org][Organizing Snippets]] Refer to the bundled snippets for =ruby-mode= for examples on the =# group:= directive. Group can also be nested, e.g. =control structure.loops= tells that the snippet is under the =loops= group which is under the =control structure= group. ** =# expand-env:= expand environment This is another piece of Emacs-lisp code in the form of a =let= /varlist form/, i.e. a list of lists assigning values to variables. It can be used to override variable values while the snippet is being expanded. Interesting variables to override are [[sym:yas-wrap-around-region][=yas-wrap-around-region=]] and [[sym:yas-indent-line][=yas-indent-line=]] (see [[./snippet-expansion.org][Expanding Snippets]]). As an example, you might normally have [[sym:yas-indent-line][=yas-indent-line=]] set to '=auto= and [[sym:yas-wrap-around-region][=yas-wrap-around-region=]] set to =t=, but for this particularly brilliant piece of ASCII art these values would mess up your hard work. You can then use: #+BEGIN_SRC snippet # name: ASCII home # expand-env: ((yas-indent-line 'fixed) (yas-wrap-around-region 'nil)) # -- welcome to my X humble / \ home, / \ $0 / \ /-------\ | | | +-+ | | | | | +--+-+--+ #+END_SRC ** =# binding:= direct keybinding You can use this directive to expand a snippet directly from a normal Emacs keybinding. The keybinding will be registered in the Emacs keymap named after the major mode the snippet is active for. Additionally a variable [[sym:yas-prefix][=yas-prefix=]] is set to to the prefix argument you normally use for a command. This allows for small variations on the same snippet, for example in this "html-mode" snippet. #+BEGIN_SRC snippet # name:

...

# binding: C-c C-c C-m # --

`(when yas-prefix "\n")`$0`(when yas-prefix "\n")`

#+END_SRC This binding will be recorded in the keymap =html-mode-map=. To expand a paragraph tag newlines, just press =C-u C-c C-c C-m=. Omitting the =C-u= will expand the paragraph tag without newlines. ** =# type:= =snippet= or =command= If the =type= directive is set to =command=, the body of the snippet is interpreted as lisp code to be evaluated when the snippet is triggered. If it's =snippet= (the default when there is no =type= directive), the snippet body will be parsed according to the [[Template Syntax]], described below. ** =# uuid:= unique identifier This provides to a way to identify a snippet, independent of its name. Loading a second snippet file with the same uuid would replace the previous snippet. ** =# contributor:= snippet author This is optional and has no effect whatsoever on snippet functionality, but it looks nice. * <