From da9852a43f2d6011094e12d813dcb7eab6851e69 Mon Sep 17 00:00:00 2001 From: Noam Postavsky Date: Mon, 25 Nov 2013 19:42:22 -0500 Subject: [PATCH] {snippet-development,faq}.org: fixup pandoc output - add source blocks - fix links - don't duplicate docstrings - formatting --- doc/faq.org | 75 +++++++-- doc/snippet-development.org | 310 +++++++++++++++++++----------------- 2 files changed, 227 insertions(+), 158 deletions(-) diff --git a/doc/faq.org b/doc/faq.org index d45b2c371..d81181662 100644 --- a/doc/faq.org +++ b/doc/faq.org @@ -1,3 +1,5 @@ +#+SETUPFILE: org-setup.inc + * Frequently Asked Questions ** Why is there an extra newline? @@ -18,13 +20,17 @@ First check the mode line to see if there's =yas=. If not, then try expand the snippet again. If it works, then, you can add the following code to your =.emacs= /before/ loading YASnippet: -where =the-major-mode= is the major mode in which =yas-minor-mode= isn't +#+BEGIN_SRC emacs-lisp + (add-hook 'the-major-mode-hook 'yas-minor-mode-on) +#+END_SRC + +where =the-major-mode= is the major mode in which [[sym:yas-minor-mode][=yas-minor-mode=]] isn't enabled by default. From YASnippet 0.6 you can also use the command =M-x yas-global-mode= to turn on YASnippet automatically for /all/ major modes. -If =yas-minor-mode= is on but the snippet still not expanded. Then try +If [[sym:yas-minor-mode][=yas-minor-mode=]] is on but the snippet still not expanded. Then try to see what command is bound to the =TAB= key: press =C-h k= and then press =TAB=. Emacs will show you the result. @@ -32,24 +38,44 @@ You'll see a buffer prompted by Emacs saying that =TAB runs the command ...=. Alternatively, you might see = runs the command ...=, note the difference between =TAB= and == where the latter has priority. If you see == bound to a -command other than =yas-expand=, (e.g. in =org-mode=) you can try the +command other than [[sym:yas-expand][=yas-expand=]], (e.g. in =org-mode=) you can try the following code to work around: +#+BEGIN_SRC emacs-lisp + (add-hook 'org-mode-hook + (let ((original-command (lookup-key org-mode-map [tab]))) + `(lambda () + (setq yas-fallback-behavior + '(apply ,original-command)) + (local-set-key [tab] 'yas-expand)))) +#+END_SRC + replace =org-mode-hook= and =org-mode-map= with the major mode hook you are dealing with (Use =C-h m= to see what major mode you are in). As an alternative, you can also try +#+BEGIN_SRC emacs-lisp + (defun yas-advise-indent-function (function-symbol) + (eval `(defadvice ,function-symbol (around yas-try-expand-first activate) + ,(format + "Try to expand a snippet before point, then call `%s' as usual" + function-symbol) + (let ((yas-fallback-behavior nil)) + (unless (and (interactive-p) + (yas-expand)) + ad-do-it))))) + + (yas-advise-indent-function 'ruby-indent-line) +#+END_SRC + To /advise/ the modes indentation function bound to TAB, (in this case -=ruby-indent-line=) to first try to run =yas-expand=. +=ruby-indent-line=) to first try to run [[sym:yas-expand][=yas-expand=]]. If the output of =C-h k RET = tells you that == is indeed -bound to =yas-expand= but YASnippet still doesn't work, check your -configuration and you may also ask for help on the -[[http://groups.google.com/group/smart-snippet][discussion group]]. See -this particular -[[http://code.google.com/p/yasnippet/issues/detail?id=93&can=1][thread]] -for quite some solutions and alternatives. +bound to [[sym:yas-expand][=yas-expand=]] but YASnippet still doesn't work, check your +configuration and you may also ask for help on the [[http://groups.google.com/group/smart-snippet][discussion group]]. +See this particular [[http://code.google.com/p/yasnippet/issues/detail?id=93&can=1][thread]] for quite some solutions and alternatives. Don't forget to attach the information on what command is bound to TAB as well as the mode information (Can be obtained by =C-h m=). @@ -59,6 +85,13 @@ as well as the mode information (Can be obtained by =C-h m=). A workaround is to inhibit flyspell overlays while the snippet is active: +#+BEGIN_SRC emacs-lisp + (add-hook 'flyspell-incorrect-hook + #'(lambda (dummy1 dummy2 dymmy3) + (and yas-active-field-overlay + (overlay-buffer yas-active-field-overlay)))) +#+END_SRC + This is apparently related to overlay priorities. For some reason, the =keymap= property of flyspell's overlays always takes priority over the same property in yasnippet's overlays, even if one sets the latter's @@ -69,7 +102,14 @@ solve this problem, drop a line in the ** How do I turn off the minor mode where in some buffers The best way, since version 0.6.1c, is to set the default value of the -variable =yas-dont-activate= to a lambda function like so: +variable [[sym:yas-dont-activate][=yas-dont-activate=]] to a lambda function like so: + +#+BEGIN_SRC emacs-lisp + (set-default 'yas-dont-activate + #'(lambda () + (and yas-root-directory + (null (yas-get-snippet-tables))))) +#+END_SRC This is also the default value starting for that version. It skips the minor mode in buffers where it is not applicable (no snippet tables), @@ -78,9 +118,9 @@ but only once you have setup your yas-root-directory. ** How do I define an abbrev key containing characters not supported by the filesystem? -- *Note*: This question applies if you're still defining - snippets :: whose key /is/ the filename. This is behavior stil - provided by version 0.6 for backward compatibilty, but is somewhat +- *Note*: This question applies if you're still defining snippets + whose key /is/ the filename. This is behavior still provided by + version 0.6 for backward compatibilty, but is somewhat deprecated... For example, you want to define a snippet by the key =<= which is not a @@ -91,3 +131,10 @@ You should rather use the =# key:= directive to specify the key of the defined snippet explicitly and name your snippet with an arbitrary valid filename, =lt.yasnippet= for example, using =<= for the =# key:= directive: + +#+BEGIN_SRC snippet + # key: < + # name: <...> + # -- + <${1:div}>$0 +#+END_SRC diff --git a/doc/snippet-development.org b/doc/snippet-development.org index 7298ff9f6..f0ab1320c 100644 --- a/doc/snippet-development.org +++ b/doc/snippet-development.org @@ -1,3 +1,5 @@ +#+SETUPFILE: org-setup.inc + * Writing snippets ** Snippet development @@ -22,7 +24,7 @@ There are some ways you can quickly find a snippet file: - =M-x yas-visit-snippet-file= Prompts you for possible snippet expansions like - =yas-insert-snippet=, but instead of expanding it, takes you directly + [[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 @@ -69,12 +71,19 @@ 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 =yas-trigger-key=. If +you type to expand a snippet just before hitting [[sym:yas-trigger-key][=yas-trigger-key=]]. If you don't specify this the snippet will not be expandable through the key mechanism. @@ -93,8 +102,8 @@ 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 =yas-buffer-local-condition= in -[[snippet-expansion.html][Expanding snippets]] +See also [[sym:yas-buffer-local-condition][=yas-buffer-local-condition=]] in +[[./snippet-expansion.org][Expanding snippets]] *** =# group:= snippet menu grouping @@ -103,10 +112,10 @@ 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.html][the YASnippet menu]]) and the same effect can be +[[./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.html][Organizing Snippets]] +[[./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. @@ -119,24 +128,47 @@ 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 =yas-wrap-around-region= and -=yas-indent-line= (see [[snippet-expansion.html][Expanding Snippets]]). +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 =yas-indent-line= set to ='auto= -and =yas-wrap-around-region= set to =t=, but for this particularly +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 =yas-prefix= is set to to the prefix argument +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. @@ -168,13 +200,28 @@ snippet being expanded. Here's an example for c-mode` to calculate the header file guard dynamically: +#+BEGIN_SRC snippet + #ifndef ${1:_`(upcase (file-name-nondirectory (file-name-sans-extension (buffer-file-name))))`_H_} + #define $1 + + $0 + + #endif /* $1 */ +#+END_SRC + From version 0.6, snippets expansions are run with some special -Emacs-lisp variables bound. One of this is =yas-selected-text=. You can +Emacs-lisp variables bound. One of this is [[sym:yas-selected-text][=yas-selected-text=]]. You can therefore define a snippet like: +#+BEGIN_SRC snippet + for ($1;$2;$3) { + `yas-selected-text`$0 + } +#+END_SRC + to "wrap" the selected region inside your recently inserted snippet. Alternatively, you can also customize the variable -=yas-wrap-around-region= to =t= which will do this automatically. +[[sym:yas-wrap-around-region][=yas-wrap-around-region=]] to =t= which will do this automatically. *** Tab stop fields @@ -183,40 +230,68 @@ Tab stops are fields that you can navigate back and forth by =TAB= and special meaning of the /exit point/ of a snippet. That is the last place to go when you've traveled all the fields. Here's a typical example: +#+BEGIN_SRC snippet + + $0 + +#+END_SRC *** Placeholder fields Tab stops can have default values -- a.k.a placeholders. The syntax is like this: -They acts as the default value for a tab stop. But when you firstly type -at a tab stop, the default value will be replaced by your typing. The -number can be omitted if you don't want to create mirrors\_ or -transformations\_ for this field. +#+BEGIN_SRC snippet + ${N:default value} +#+END_SRC -*** Mirrors +They acts as the default value for a tab stop. But when you firstly +type at a tab stop, the default value will be replaced by your typing. +The number can be omitted if you don't want to create [[mirrors]] or +[[transformations]] for this field. + +*** <> We refer the tab stops with placeholders as a /field/. A field can have mirrors. Its mirrors will get updated when you change the text of a field. Here's an example: -When you type ="document"= at =${1:enumerate}=, the word ="document"= -will also be inserted at =\end{$1}=. The best explanation is to see the -screencast([[http://www.youtube.com/watch?v=vOj7btx3ATg][YouTube]] or -[[http://yasnippet.googlecode.com/files/yasnippet.avi][avi video]]). +#+BEGIN_SRC snippet + \begin{${1:enumerate}} + $0 + \end{$1} +#+END_SRC + +When you type "document" at =${1:enumerate}=, the word "document" will +also be inserted at =\end{$1}=. The best explanation is to see the +screencast([[http://www.youtube.com/watch?v=vOj7btx3ATg][YouTube]] or [[http://yasnippet.googlecode.com/files/yasnippet.avi][avi video]]). The tab stops with the same number to the field act as its mirrors. If none of the tab stops has an initial value, the first one is selected as the field and others mirrors. -*** Mirrors with transformations +*** Mirrors with <> If the value of an =${n:=-construct starts with and contains =$(=, then it is interpreted as a mirror for field =n= with a transformation. The mirror's text content is calculated according to this transformation, which is Emacs-lisp code that gets evaluated in an environment where the -variable =text= (or =yas-text=) is bound to the text content (string) +variable =text= (or [[sym:yas-text][=yas-text=]]) is bound to the text content (string) contained in the field =n=.Here's an example for Objective-C: +#+BEGIN_SRC snippet + - (${1:id})${2:foo} + { + return $2; + } + + - (void)set${2:$(capitalize text)}:($1)aValue + { + [$2 autorelease]; + $2 = [aValue retain]; + } + $0 +#+END_SRC + Look at =${2:$(capitalize text)}=, it is a mirror with transformation instead of a field. The actual field is at the first line: =${2:foo}=. When you type text in =${2:foo}=, the transformation will be evaluated @@ -228,10 +303,30 @@ Another example is for =rst-mode=. In reStructuredText, the document title can be some text surrounded by "===" below and above. The "===" should be at least as long as the text. So +#+BEGIN_SRC rst + ===== + Title + ===== +#+END_SRC + is a valid title but +#+BEGIN_SRC rst + === + Title + === +#+END_SRC + is not. Here's an snippet for rst title: +#+BEGIN_SRC snippet + ${1:$(make-string (string-width text) ?\=)} + ${1:Title} + ${1:$(make-string (string-width text) ?\=)} + + $0 +#+END_SRC + *** Fields with transformations From version 0.6 on, you can also have lisp transformation inside @@ -242,6 +337,8 @@ also just before you exit the field. The syntax is also a tiny bit different, so that the parser can distinguish between fields and mirrors. In the following example +: #define "${1:mydefine$(upcase yas-text)}" + =mydefine= gets automatically upcased to =MYDEFINE= once you enter the field. As you type text, it gets filtered through the transformation every time. @@ -251,6 +348,8 @@ transformation, YASnippet needs extra text between the =:= and the transformation's =$=. If you don't want this extra-text, you can use two =$='s instead. +: #define "${1:$$(upcase yas-text)}" + Please note that as soon as a transformation takes place, it changes the value of the field and sets it its internal modification state to =true=. As a consequence, the auto-deletion behaviour of normal fields @@ -260,139 +359,53 @@ does not take place. This is by design. As mentioned, the field transformation is invoked just after you enter the field, and with some useful variables bound, notably -=yas-modified-p= and =yas-moving-away-p=. Because of this feature you +[[sym:yas-modified-p][=yas-modified-p=]] and [[sym:yas-moving-away-p][=yas-moving-away-p=]]. Because of this feature you can place a transformation in the primary field that lets you select default values for it. -The =yas-choose-value= does this work for you. For example: +The [[sym:yas-choose-value][=yas-choose-value=]] does this work for you. For example: + +#+BEGIN_SRC snippet +
+ $0 +
+#+END_SRC -See the definition of =yas-choose-value= to see how it was written using +See the definition of [[sym:yas-choose-value][=yas-choose-value=]] to see how it was written using the two variables. Here's another use, for LaTeX-mode, which calls reftex-label just as you -enter snippet field 2. This one makes use of =yas-modified-p= directly. +enter snippet field 2. This one makes use of [[sym:yas-modified-p][=yas-modified-p=]] directly. -The function =yas-verify-value= has another neat trick, and makes use of -=yas-moving-away-p=. Try it and see! Also, check out this +#+BEGIN_SRC snippet + \section{${1:"Titel der Tour"}}% + \index{$1}% + \label{{2:"waiting for reftex-label call..."$(unless yas-modified-p (reftex-label nil 'dont- + insert))}}% +#+END_SRC + +The function [[sym:yas-verify-value][=yas-verify-value=]] has another neat trick, and makes use of +[[sym:yas-moving-away-p][=yas-moving-away-p=]]. Try it and see! Also, check out this [[http://groups.google.com/group/smart-snippet/browse_thread/thread/282a90a118e1b662][thread]] *** Nested placeholder fields From version 0.6 on, you can also have nested placeholders of the type: +#+BEGIN_SRC snippet + $0 +#+END_SRC + This allows you to choose if you want to give this =div= an =id= attribute. If you tab forward after expanding it will let you change "some\_id" to whatever you like. Alternatively, you can just press =C-d= -(which executes =yas-skip-and-clear-or-delete-char=) and go straight to +(which executes [[sym:yas-skip-and-clear-or-delete-char][=yas-skip-and-clear-or-delete-char=]]) and go straight to the exit marker. By the way, =C-d= will only clear the field if you cursor is at the beginning of the field /and/ it hasn't been changed yet. Otherwise, it performs the normal Emacs =delete-char= command. -** Customizable variables - -*** =yas-trigger-key= - -The key bound to =yas-expand= when function =yas-minor-mode= is active. - -Value is a string that is converted to the internal Emacs key -representation using =read-kbd-macro=. - -Default value is ="TAB"=. - -*** =yas-next-field-key= - -The key to navigate to next field when a snippet is active. - -Value is a string that is converted to the internal Emacs key -representation using =read-kbd-macro=. - -Can also be a list of keys. - -Default value is ="TAB"=. - -*** =yas-prev-field-key= - -The key to navigate to previous field when a snippet is active. - -Value is a string that is converted to the internal Emacs key -representation using =read-kbd-macro=. - -Can also be a list of keys. - -Default value is =("" ")"=. - -*** =yas-skip-and-clear-key= - -The key to clear the currently active field. - -Value is a string that is converted to the internal Emacs key -representation using =read-kbd-macro=. - -Can also be a list of keys. - -Default value is ="C-d"=. - -*** =yas-good-grace= - -If non-nil, don't raise errors in inline Emacs-lisp evaluation inside -snippet definitions. An error string "[yas] error" is returned instead. - -*** =yas-indent-line= - -The variable =yas-indent-line= controls the indenting. It is bound to -='auto= by default, which causes your snippet to be indented according -to the mode of the buffer it was inserted in. - -Another variable =yas-also-auto-indent-first-line=, when non-nil does -exactly that :-). - -To use the hard-coded indentation in your snippet template, set this -variable to =fixed=. - -To control indentation on a per-snippet basis, see also the directive -=# expand-env:= in [[snippet-development.html][Writing Snippets]]. - -For backward compatibility with earlier versions of YASnippet, you can -also place a =$>= in your snippet, an =(indent-according-to-mode)= will -be executed there to indent the line. This only takes effect when -=yas-indent-line= is set to something other than ='auto=. - -*** =yas-wrap-around-region= - -If non-nil, YASnippet will try to expand the snippet's exit marker -around the currently selected region. When this variable is set to t, -this has the same effect has using the =`yas-selected-text=` inline -evaluation. - -Because on most systems starting to type deletes the currently selected -region, this works mostly for snippets with direct keybindings or with -the =yas-insert-snippet= command. - -However, when the value is of this variable is =cua= YASnippet will -additionally look-up any recently selected that you deleted by starting -typing. This allows you select a region, type a snippet key (deleting -the region), then press =yas-trigger-key= to see the deleted region -spring back to life inside your new snippet. - -*** =yas-triggers-in-field= - -If non-nil, =yas-next-field-key= can trigger stacked expansions, that is -a snippet expansion inside another snippet expansion. Otherwise, -=yas-next-field-key= just tries to move on to the next field. - -*** =yas-snippet-revival= - -Non-nil means re-activate snippet fields after undo/redo. - -*** =yas-after-exit-snippet-hook= and =yas-before-expand-snippet-hook= - -These hooks are called, respectively, before the insertion of a snippet -and after exiting the snippet. If you find any strange but functional -use for them, that's probably a design flaw in YASnippet, so let us -know. - ** Importing TextMate snippets There are a couple of tools that take TextMate's ".tmSnippet" xml files @@ -400,14 +413,10 @@ and create YASnippet definitions: #+BEGIN_QUOTE - - [[http://code.nokrev.com/?p=snippet-copier.git;a=blob_plain;f=snippet_copier.py][a - python script by Jeff Wheeler]] + - [[http://code.nokrev.com/?p=snippet-copier.git;a=blob_plain;f=snippet_copier.py][a python script by Jeff Wheeler]] - - a - [[http://yasnippet.googlecode.com/svn/trunk/extras/textmate_import.rb][ruby - tool]] , =textmate_import.rb= adapted from - [[http://www.neutronflux.net/2009/07/28/shoulda-snippets-for-emacs/][Rob - Christie's]], which I have uploaded to the repository. + - a [[http://yasnippet.googlecode.com/svn/trunk/extras/textmate_import.rb][ruby tool]] , =textmate_import.rb= adapted from [[http://www.neutronflux.net/2009/07/28/shoulda-snippets-for-emacs/][Rob Christie's]], + which I have uploaded to the repository. #+END_QUOTE @@ -416,15 +425,28 @@ In this section, i'll shortly cover the *second* option. Download the =textmate_import.rb= tool and the TextMate bundle you're interested in. +#+BEGIN_EXAMPLE + $ curl -O http://yasnippet.googlecode.com/svn/trunk/extras/textmate_import.rb + $ svn export http://svn.textmate.org/trunk/Bundles/HTML.tmbundle/ +#+END_EXAMPLE + Then invoke =textmate_import.rb= like this: +#+BEGIN_EXAMPLE + $ ./textmate_import.rb -d HTML.tmbundle/Snippets/ -o html-mode -g HTML.tmbundle/info.plist +#+END_EXAMPLE + You should end up with a =html-mode= subdir containing snippets exported from textmate. +#+BEGIN_EXAMPLE + $ tree html-mode # to view dir contents, if you have 'tree' installed +#+END_EXAMPLE + The =-g= is optional but helps the tool figure out the grouping. -According to [[snippet-organization.html][Organizing Snippets]], don't -forget to touch =.yas-make-groups= and =.yas-ignore-filename-triggers= -inside the =html-mode= dir. +According to [[./snippet-organization.org][Organizing Snippets]], don't forget to touch +=.yas-make-groups= and =.yas-ignore-filename-triggers= inside the +=html-mode= dir. Also try =textmate_import.rb --help= for a list of options. -- 2.39.2