]> code.delx.au - gnu-emacs-elpa/blob - doc/snippet-expansion.rst
* Fix the multi-line substitution problem
[gnu-emacs-elpa] / doc / snippet-expansion.rst
1 ==================
2 Expanding snippets
3 ==================
4
5 .. _Organizing Snippets: snippet-organization.html
6 .. _Expanding Snippets: snippet-expansion.html
7 .. _Writing Snippets: snippet-development.html
8 .. _The YASnippet Menu: snippet-menu.html
9
10 .. contents::
11
12
13 Triggering expansion
14 ====================
15
16 You can use YASnippet to expand snippets in different ways:
17
18 * By typing an abbrev, the snippet *trigger key*, and then pressing
19 the key defined in ``yas/trigger-key`` (which defaults to
20 "TAB"). This works in buffers where the minor mode
21 ``yas/minor-mode`` is active;
22
23 * By invoking the command ``yas/insert-snippet`` (either by typing
24 ``M-x yas/insert-snippet`` or its keybinding). This does *not*
25 require ``yas/minor-mode`` to be active.
26
27 * By using the keybinding associated with an active snippet. This also
28 requires ``yas/minor-mode`` to be active;
29
30 * By expanding directly from the "YASnippet" menu in the menu-bar
31
32 * By using hippie-expand
33
34 * Expanding from emacs-lisp code
35
36 Trigger key
37 -----------
38
39 When ``yas/minor-mode`` is enabled, the keybinding taken from
40 ``yas/trigger-key`` will take effect.
41
42 ``yas/trigger-key`` invokes ``yas/expand``, which tries to expand a
43 *snippet abbrev* (also known as *snippet key*) before point.
44
45 The default key is ``"TAB"``, however, you can freely set it to some
46 other key.
47
48 .. image:: images/minor-mode-indicator.png
49 :align: left
50
51 To enable the YASnippet minor mode in all buffers globally use the
52 command ``yas/global-mode``.
53
54 When you use ``yas/global-mode`` you can also selectively disable
55 YASnippet in some buffers by setting the buffer-local variable
56 ``yas/dont-active`` in the buffer's mode hook.
57
58 Trouble when using or understanding the ``yas/trigger-key`` is easily
59 the most controversial issue in YASsnippet. See the `FAQ <faq.html>`_.
60
61 Fallback bahaviour
62 ~~~~~~~~~~~~~~~~~~
63
64 ``yas/fallback-behaviour`` is a customization variable bound to
65 ``'call-other-command`` by default. If ``yas/expand`` failed to find
66 any suitable snippet to expand, it will disable the minor mode
67 temporarily and find if there's any other command bound the
68 ``yas/trigger-key``.
69
70 If found, the command will be called. Usually this works very well --
71 when there's a snippet, expand it, otherwise, call whatever command
72 originally bind to the trigger key.
73
74 However, you can change this behavior by customizing the
75 ``yas/fallback-behavior`` variable. If you set this variable to
76 ``'return-nil``, it will return ``nil`` instead of trying to call the
77 *original* command when no snippet is found.
78
79 Insert at point
80 ---------------
81
82 The command ``M-x yas/insert-snippet`` lets you insert snippets at
83 point *for you current major mode*. It prompts you for the snippet
84 key first, and then for a snippet template if more than one template
85 exists for the same key.
86
87 The list presented contains the snippets that can be inserted at
88 point, according to the condition system. If you want to see all
89 applicable snippets for the major mode, prefix this command with
90 ``C-u``.
91
92 The prompting methods used are again controlled by
93 ``yas/prompt-functions``.
94
95 Snippet keybinding
96 ------------------
97
98 See the section of the ``# binding:`` directive in `Writing
99 Snippets`_.
100
101
102 Expanding from the menu
103 -----------------------
104
105 See `the YASnippet Menu`_.
106
107 Expanding with ``hippie-expand``
108 ----------------------------------
109
110 To integrate with ``hippie-expand``, just put
111 ``yas/hippie-try-expand`` in
112 ``hippie-expand-try-functions-list``. This probably makes more sense
113 when placed at the top of the list, but it can be put anywhere you
114 prefer.
115
116 Expanding from emacs-lisp code
117 ------------------------------
118
119 Sometimes you might want to expand a snippet directly from you own
120 elisp code. You should call ``yas/expand-snippet`` instead of
121 ``yas/expand`` in this case.
122
123 As with expanding from the menubar, the condition system and multiple
124 candidates doesn't affect expansion. In fact, expanding from the
125 YASnippet menu has the same effect of evaluating the follow code:
126
127 .. sourcecode:: common-lisp
128
129 (yas/expand-snippet template)
130
131 See the internal documentation on ``yas/expand-snippet`` for more
132 information.
133
134 Controlling expansion
135 =====================
136
137 Eligible snippets
138 -----------------
139
140 YASnippet does quite a bit of filtering to find out which snippets are
141 eligible for expanding at the current cursor position.
142
143 In particular, the following things matter:
144
145 * Currently loaded snippets tables
146
147 These are loaded from a directory hierarchy in your file system. See
148 `Organizing Snippets`_. They are named after major modes like
149 ``html-mode``, ``ruby-mode``, etc...
150
151 * Major mode of the current buffer
152
153 If the currrent major mode matches one of the loaded snippet tables,
154 then all that table's snippets are considered for expansion. Use
155 ``M-x describe-variable RET major-mode RET`` to find out which major
156 mode you are in currently.
157
158 * Parent tables
159
160 Snippet tables defined as the parent of some other eligible table
161 are also considered. This works recursively, i.e. parents of parents
162 of eligible tables are also considered.
163
164 * Buffer-local ``yas/mode-symbol`` variable
165
166 This can be used to consider snippet tables whose name does not
167 correspond to a major mode. If you set this variable to a name ,
168 like ``rinari-minor-mode``, you can have some snippets expand only
169 in that minor mode. Naturally, you want to set this conditionally,
170 i.e. only when entering that minor mode, so using a hook is a good
171 idea.
172
173 .. sourcecode:: common-lisp
174
175 ;; When entering rinari-minor-mode, consider also the snippets in the
176 ;; snippet table "rails-mode"
177 (add-hook 'rinari-minor-mode-hook
178 #'(lambda ()
179 (setq yas/mode-symbol 'rails-mode)))
180
181 * Buffer-local ``yas/buffer-local-condition`` variable
182
183 This variable provides finer grained control over what snippets can
184 be expanded in the current buffer. The default value won't let you
185 expand snippets inside comments or string literals for example. See
186 `The condition system`_ for more info.
187
188 The condition system
189 --------------------
190
191 Consider this scenario: you are an old Emacs hacker. You like the
192 abbrev-way and set ``yas/trigger-key`` to ``"SPC"``. However,
193 you don't want ``if`` to be expanded as a snippet when you are typing
194 in a comment block or a string (e.g. in ``python-mode``).
195
196 If you use the ``# condition :`` directive (see `Writing Snippets`_)
197 you could just specify the condition for ``if`` to be ``(not
198 (python-in-string/comment))``. But how about ``while``, ``for``,
199 etc. ? Writing the same condition for all the snippets is just
200 boring. So has a buffer local variable
201 ``yas/buffer-local-condition``. You can set this variable to ``(not
202 (python-in-string/comment))`` in ``python-mode-hook``.
203
204 Then, what if you really want some particular snippet to expand even
205 inside a comment? This is also possible! But let's stop telling the
206 story and look at the rules:
207
208 * If ``yas/buffer-local-condition`` evaluate to nil, no snippets will
209 be considered for expansion.
210
211 * If it evaluates to the a *cons cell* where the ``car`` is the symbol
212 ``require-snippet-condition`` and the ``cdr`` is a symbol (let's
213 call it ``requirement``), then:
214
215 * Snippets having no ``# condition:`` directive won't be considered;
216
217 * Snippets with conditions that evaluate to nil (or produce an
218 error) won't be considered;
219
220 * If the snippet has a condition that evaluates to non-nil (let's
221 call it ``result``):
222
223 * If ``requirement`` is ``t``, the snippet is ready to be
224 expanded;
225
226 * If ``requirement`` is ``eq`` to ``result``, the snippet is ready
227 to be expanded;
228
229 * Otherwise the snippet won't be considered.
230
231 * If it evaluates to the symbol ``always``, all snippets are
232 considered for expansion, regardless of any conditions.
233
234 * If it evaluate to ``t`` or some other non-nil value:
235
236 * If the snippet has no condition, or has a condition that evaluate
237 to non-nil, it is ready to be expanded.
238
239 * Otherwise, it won't be considered.
240
241 In the mentioned scenario, set ``yas/buffer-local-condition`` like
242 this
243
244 .. sourcecode:: common-lisp
245
246 (add-hook 'python-mode-hook
247 '(lambda ()
248 (setq yas/buffer-local-condition
249 '(if (python-in-string/comment)
250 '(require-snippet-condition . force-in-comment)
251 t))))
252
253 ... and specify the condition for a snippet that you're going to
254 expand in comment to be evaluated to the symbol
255 ``force-in-comment``. Then it can be expanded as you expected, while
256 other snippets like ``if`` still can't expanded in comment.
257
258 Multiples snippet with the same key
259 -----------------------------------
260
261 The rules outlined `above <Eligible snippets>`_ can return more than
262 one snippet to be expanded at point.
263
264 When there are multiple candidates, YASnippet will let you select
265 one. The UI for selecting multiple candidate can be customized through
266 ``yas/prompt-functions`` , which defines your preferred methods of
267 being prompted for snippets.
268
269 You can customize it with ``M-x customize-variable RET
270 yas/prompt-functions RET``. Alternatively you can put in your
271 emacs-file:
272
273 .. sourcecode:: common-lisp
274
275 (setq yas/prompt-functions '(yas/x-prompt yas/dropdown-prompt))
276
277 Currently there are some alternatives solution with YASnippet.
278
279 .. image:: images/x-menu.png
280 :align: right
281
282 Use the X window system
283 ~~~~~~~~~~~~~~~~~~~~~~~
284
285 The function ``yas/x-prompt`` can be used to show a popup menu for you
286 to select. This menu will be part of you native window system widget,
287 which means:
288
289 * It usually looks beautiful. E.g. when you compile Emacs with gtk
290 support, this menu will be rendered with your gtk theme.
291 * Emacs have little control over it. E.g. you can't use ``C-n``,
292 ``C-p`` to navigate.
293 * This function can't be used when in a terminal.
294
295 .. image:: images/ido-menu.png
296 :align: right
297
298 Minibuffer prompting
299 ~~~~~~~~~~~~~~~~~~~~
300
301 You can use functions ``yas/completing-prompt`` for the classic emacs
302 completion method or ``yas/ido-prompt`` for a much nicer looking
303 method. The best way is to try it. This works in a terminal.
304
305 .. image:: images/dropdown-menu.png
306 :align: right
307
308 Use ``dropdown-menu.el``
309 ~~~~~~~~~~~~~~~~~~~~~~~~
310
311 The function ``yas/dropdown-prompt`` can also be placed in the
312 ``yas/prompt-functions`` list.
313
314 This works in both window system and terminal and is customizable, you
315 can use ``C-n``, ``C-p`` to navigate, ``q`` to quit and even press
316 ``6`` as a shortcut to select the 6th candidate.
317
318 Roll your own
319 ~~~~~~~~~~~~~
320
321 See below for the documentation on variable ``yas/prompt-functions``
322
323 Customizable Variables
324 ======================
325
326 ``yas/prompt-functions``
327 ------------------------
328
329 You can write a function and add it to the ``yas/prompt-functions``
330 list. These functions are called with the following arguments:
331
332 * PROMPT: A string to prompt the user;
333
334 * CHOICES: A list of strings or objects;
335
336 * optional DISPLAY-FN : A function. When applied to each of the
337 objects in CHOICES it will return a string;
338
339 The return value of any function you put here should be one of
340 the objects in CHOICES, properly formatted with DISPLAY-FN (if
341 that is passed).
342
343 * To signal that your particular style of prompting is unavailable at
344 the moment, you can also have the function return nil.
345
346 * To signal that the user quit the prompting process, you can signal
347 ``quit`` with ``(signal 'quit "user quit!")``
348
349 ``yas/fallback-behavior``
350 -------------------------
351
352 How to act when ``yas/expand`` does *not* expand a snippet.
353
354 ``call-other-command`` means try to temporarily disable YASnippet and
355 call the next command bound to ``yas/trigger-key``.
356
357 ``return-nil`` means return nil. (i.e. do nothing)
358
359 An entry (apply COMMAND . ARGS) means interactively call COMMAND, if
360 ARGS is non-nil, call COMMAND non-interactively with ARGS as
361 arguments.
362
363 ``yas/choose-keys-first``
364 -------------------------
365
366 If non-nil, prompt for snippet key first, then for template.
367
368 Otherwise prompts for all possible snippet names.
369
370 This affects ``yas/insert-snippet`` and ``yas/visit-snippet-file``.
371
372 ``yas/choose-tables-first``
373 ---------------------------
374
375 If non-nil, and multiple eligible snippet tables, prompts user for
376 tables first.
377
378 Otherwise, user chooses between the merging together of all
379 eligible tables.
380
381 This affects ``yas/insert-snippet``, ``yas/visit-snippet-file``
382
383 ``yas/key-syntaxes``
384 --------------------
385
386 The default searching strategy is quite powerful. For example, in
387 ``c-mode``, ``bar``, ``foo_bar``, ``"#foo_bar"`` can all be recognized
388 as a snippet key. Furthermore, the searching is in that order. In
389 other words, if ``bar`` is found to be a key to some *valid* snippet,
390 then that snippet is expanded and replaces the ``bar``. Snippets
391 pointed to by ``foo_bar`` and ``"#foobar`` won't be considered.
392
393 However, this strategy can also be customized easily from the
394 ``yas/key-syntaxes`` variable. It is a list of syntax rules, the
395 default value is ``("w" "w_" "w_." "^ ")``. Which means search the
396 following thing until found one:
397
398 * a word.
399 * a symbol. In lisp, ``-`` and ``?`` can all be part of a symbol.
400 * a sequence of characters of either word, symbol or punctuation.
401 * a sequence of characters of non-whitespace characters.
402
403 But you'd better keep the default value unless you want to understand
404 how Emacs's syntax rules work...
405
406