From 0564b35647715880c7c9f02b3f643f7d2e4f0fd0 Mon Sep 17 00:00:00 2001 From: capitaomorte Date: Sat, 5 Sep 2009 14:44:18 +0000 Subject: [PATCH] * Fixed issue 98, issue 93 and issues related to the customization group * Added workaround for issue 97 in the FAQ * Minor updates in the documentation --- doc/faq.html | 52 +++++++++++++++++++++----------- doc/faq.rst | 58 +++++++++++++++++++++++++----------- doc/snippet-development.html | 42 +++++++++++++++----------- doc/snippet-development.rst | 36 ++++++++++++---------- yasnippet.el | 24 ++++++++++----- 5 files changed, 136 insertions(+), 76 deletions(-) diff --git a/doc/faq.html b/doc/faq.html index 620457e..45b0737 100644 --- a/doc/faq.html +++ b/doc/faq.html @@ -3,7 +3,7 @@ - + Frequently Asked Questions @@ -54,8 +54,8 @@ for other versions can be found here.

-
-

Why there's an extra newline?

+
+

Why is there an extra newline?

If you have a newline at the end of the snippet definition file, then YASnippet will add a newline when you expanding a snippet. Please don't add a newline at the end if you don't want it when you saving @@ -64,8 +64,8 @@ the snippet file.

if you set require-final-newline to t, it will add the final newline for you automatically.

-
-

Why TAB key doesn't expand a snippet?

+
+

Why doesn't TAB expand a snippet?

First check the mode line to see if there's yas. If not, then try M-x yas/minor-mode to manually turn on the minor mode and try to expand the snippet again. If it works, then, you can add the following @@ -88,11 +88,14 @@ than yas/expand, (e.g code to work around:

(add-hook 'org-mode-hook
           #'(lambda ()
+              (setq yas/fallback-behavior
+                    `(apply ,(lookup-key org-mode-map [tab])))
               (local-set-key [tab] 'yas/expand)))
 
-

replace org-mode-hook with the major mode hook you are dealing -with (C-h m to see what major mode you are in).

-

If this doesn't work, you can also try

+

replace org-mode-hook and org-mode-map with the major mode +hook you are dealing with (C-h m to see what major mode you are +in).

+

As an alternative, you can also try

(defun yas/advise-indent-function (function-symbol)
   (eval `(defadvice ,function-symbol (around yas/try-expand-first activate)
            ,(format
@@ -114,15 +117,30 @@ group.

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).

-
-

How to define snippets with named by characters not supported by the filesystem?

-

For example, you want to define a snippet by the key < which is not a -valid character for filename on Windows. In this case, you may use -yas/define to define the snippet. If you want to enjoy defining -snippets in a file, you can use the key property to specify the key of -the defined snippet explicitly.

-

Just name your snippet with an arbitrary valid filename, lt for -example. and specify < for the key property:

+
+

Why doesn't TAB navigation work with flyspell

+

A workaround is to inhibit flyspell overlays while the snippet is active:

+
(add-hook 'flyspell-incorrect-hook
+        #'(lambda (dummy1 dummy2 dymmy3)
+            (and yas/active-field-overlay
+                 (overlay-buffer yas/active-field-overlay))))
+
+

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 priority property to something big. If you know +emacs-lisp and can solve this problem, drop a line in the discussion +group.

+
+
+

How do I define an abbrev key containing characters not supported by the filesystem?

+

For example, you want to define a snippet by the key < which is +not a valid character for filename on Windows. This means you can't +use the filename as a trigger key in this case.

+

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:

#key: <
 #name: <...></...>
 # --
diff --git a/doc/faq.rst b/doc/faq.rst
index 25ef09d..65e9a78 100644
--- a/doc/faq.rst
+++ b/doc/faq.rst
@@ -2,8 +2,8 @@
 Frequently Asked Questions
 ==========================
 
-Why there's an extra newline?
-=============================
+Why is there an extra newline?
+==============================
 
 If you have a newline at the end of the snippet definition file, then
 YASnippet will add a newline when you expanding a snippet. Please
@@ -14,8 +14,8 @@ Note some editors will automatically add a newline for you. In Emacs,
 if you set ``require-final-newline`` to ``t``, it will add the final
 newline for you automatically.
 
-Why TAB key doesn't expand a snippet?
-=====================================
+Why doesn't TAB expand a snippet?
+=================================
 
 First check the mode line to see if there's ``yas``. If not, then try
 ``M-x yas/minor-mode`` to manually turn on the minor mode and try to
@@ -48,12 +48,15 @@ code to work around:
 
   (add-hook 'org-mode-hook
             #'(lambda ()
-                (local-set-key [tab] 'yas/expand))) 
+                (setq yas/fallback-behavior
+                      `(apply ,(lookup-key org-mode-map [tab]))) 
+                (local-set-key [tab] 'yas/expand)))
 
-replace ``org-mode-hook`` with the major mode hook you are dealing
-with (``C-h m`` to see what major mode you are in).
+replace ``org-mode-hook`` and ``org-mode-map`` with the major mode
+hook you are dealing with (``C-h m`` to see what major mode you are
+in).
 
-If this doesn't work, you can also try
+As an alternative, you can also try
 
 .. sourcecode:: lisp
 
@@ -80,16 +83,36 @@ group `_.
 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``).
 
-How to define snippets with named by characters not supported by the filesystem?
-================================================================================
-For example, you want to define a snippet by the key ``<`` which is not a
-valid character for filename on Windows. In this case, you may use
-``yas/define`` to define the snippet. If you want to enjoy defining
-snippets in a file, you can use the ``key`` property to specify the key of
-the defined snippet explicitly.
+Why doesn't TAB navigation work with flyspell
+=============================================
 
-Just name your snippet with an arbitrary valid filename, ``lt`` for
-example. and specify ``<`` for the ``key`` property:
+A workaround is to inhibit flyspell overlays while the snippet is active:
+
+.. sourcecode:: lisp
+  
+  (add-hook 'flyspell-incorrect-hook
+          #'(lambda (dummy1 dummy2 dymmy3)
+              (and yas/active-field-overlay
+                   (overlay-buffer yas/active-field-overlay))))
+
+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 ``priority`` property to something big. If you know
+emacs-lisp and can solve this problem, drop a line in the `discussion
+group`_.
+
+How do I define an abbrev key containing characters not supported by the filesystem?
+====================================================================================
+
+For example, you want to define a snippet by the key ``<`` which is
+not a valid character for filename on Windows. This means you can't
+use the filename as a trigger key in this case.
+
+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:
 
 .. sourcecode:: text
 
@@ -98,3 +121,4 @@ example. and specify ``<`` for the ``key`` property:
   # --
   <${1:div}>$0
 
+.. _discussion group: http://groups.google.com/group/smart-snippet
diff --git a/doc/snippet-development.html b/doc/snippet-development.html
index 2e18833..bb68132 100644
--- a/doc/snippet-development.html
+++ b/doc/snippet-development.html
@@ -3,7 +3,7 @@
 
 
 
-
+
 Writing snippets
 
 
@@ -262,13 +262,17 @@ newlines.

To override the keymap choice based on the major mode name. Use a cons cell where the first element specifies the name of the keymap where you want to record the keybinding.

-
+
#name : <p>...</p>
+#binding: (rinari-minor-mode-map . "C-c C-c C-m")
+# --
+<p>`(when yas/prefix "\n")`$0`(when yas/prefix "\n")`</p>
 
-

Note that this feature is still experimental and should be used with -caution: It is easy to override important keybindings for many basic -modes and it is hard to undefine them. In particular, the variable +

Note that this feature is still experimental, it might go away, +be changed in future release, and should be used with caution: It is +easy to override important keybindings for many basic modes and it is +hard to undefine them. For the moment, the variable yas/active-keybinding can tell you what snippet keybindings are -active and the function yas/kill-snippet-keybindings will try to +active and the function yas/kill-snippet-keybindings will attempt to undefine all the keybindings.

@@ -354,11 +358,13 @@ as the field and others mirrors.

Mirrors with transformations

-

If the default value of a field starts with $, then it is interpreted -as the transformation code instead of default value. A transformation -is some arbitrary Emacs-lisp code that will get evaluated in an environment -when the variable text is bind to the inputted text of the -field. Here's an example for Objective-C:

+

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) contained in the field n.Here's an +example for Objective-C:

- (${1:id})${2:foo}
 {
     return $2;
@@ -371,13 +377,13 @@ field. Here's an example for Objective-C:

} $0
-

Look at ${2:$(capitalize text)}, it is a transformation instead of -a placeholder. The actual placeholder is at the first line: -${2:foo}. When you type text in ${2:foo}, the transformation -will be evaluated and the result will be placed there as the -transformed text. So in this example, if you type "baz" in the field, -the transformed text will be "Baz". This example is also available in -the screencast.

+

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 and the result will be placed there +as the transformed text. So in this example, if you type "baz" in the +field, the transformed text will be "Baz". This example is also +available in the screencast.

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

diff --git a/doc/snippet-development.rst b/doc/snippet-development.rst index 554cd94..abecd78 100644 --- a/doc/snippet-development.rst +++ b/doc/snippet-development.rst @@ -213,16 +213,18 @@ cell where the first element specifies the name of the keymap where you want to record the keybinding. .. sourcecode:: text + #name :

...

#binding: (rinari-minor-mode-map . "C-c C-c C-m") # --

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

-Note that this feature is still experimental and should be used with -caution: It is easy to override important keybindings for many basic -modes and it is hard to undefine them. In particular, the variable +*Note* that this feature is still **experimental**, it might go away, +be changed in future release, and should be used with caution: It is +easy to override important keybindings for many basic modes and it is +hard to undefine them. For the moment, the variable ``yas/active-keybinding`` can tell you what snippet keybindings are -active and the function ``yas/kill-snippet-keybindings`` will try to +active and the function ``yas/kill-snippet-keybindings`` will attempt to undefine all the keybindings. ``# contributor:`` snippet author @@ -339,11 +341,13 @@ as the field and others mirrors. Mirrors with transformations ---------------------------- -If the default value of a field starts with ``$``, then it is interpreted -as the transformation code instead of default value. A transformation -is some arbitrary Emacs-lisp code that will get evaluated in an environment -when the variable text is bind to the inputted text of the -field. Here's an example for Objective-C: +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) contained in the field ``n``.Here's an +example for Objective-C: .. sourcecode:: text @@ -359,13 +363,13 @@ field. Here's an example for Objective-C: } $0 -Look at ``${2:$(capitalize text)}``, it is a transformation instead of -a placeholder. The actual placeholder is at the first line: -``${2:foo}``. When you type text in ``${2:foo}``, the transformation -will be evaluated and the result will be placed there as the -transformed text. So in this example, if you type "baz" in the field, -the transformed text will be "Baz". This example is also available in -the screencast. +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 and the result will be placed there +as the transformed text. So in this example, if you type "baz" in the +field, the transformed text will be "Baz". This example is also +available in the screencast. Another example is for ``rst-mode``. In reStructuredText, the document title can be some text surrounded by "===" below and above. The "===" diff --git a/yasnippet.el b/yasnippet.el index e8c2b4e..a9c212f 100644 --- a/yasnippet.el +++ b/yasnippet.el @@ -314,6 +314,7 @@ field" :type '(choice (const :tag "Call previous command" call-other-command) (const :tag "Do nothing" return-nil)) :group 'yasnippet) +(make-variable-buffer-local 'yas/fallback-behavior) (defcustom yas/choose-keys-first nil "If non-nil, prompt for snippet key first, then for template. @@ -700,7 +701,8 @@ With optional UNBIND-KEY, try to unbind that key from (stringp yas/trigger-key) (not (string= yas/trigger-key ""))) (define-key yas/minor-mode-map (read-kbd-macro yas/trigger-key) 'yas/expand))) - + +;;;###autoload (define-minor-mode yas/minor-mode "Toggle YASnippet mode. @@ -741,6 +743,7 @@ this effectively lets you define exceptions to the \"global\" behaviour.") (make-variable-buffer-local 'yas/dont-activate) + (defun yas/minor-mode-on () "Turn on YASnippet minor mode. @@ -1846,11 +1849,15 @@ defined in `yas/fallback-behavior'" nil) ((eq yas/fallback-behavior 'call-other-command) (let* ((yas/minor-mode nil) - (keys (or (and yas/trigger-key - (stringp yas/trigger-key) - (read-kbd-macro yas/trigger-key)) - (this-command-keys-vector))) - (command (key-binding keys))) + (keys-1 (this-command-keys-vector)) + (keys-2 (and yas/trigger-key + (stringp yas/trigger-key) + (read-kbd-macro yas/trigger-key))) + (command-1 (and keys-1 (key-binding keys-1))) + (command-2 (and keys-2(key-binding keys-2))) + (command (or (and (not (eq' command-1 'yas/expand)) + command-1) + command2))) (when (and (commandp command) (not (eq 'yas/expand command))) (setq this-command command) @@ -2680,6 +2687,7 @@ Move the overlay, or create it if it does not exit." (make-overlay (yas/field-start field) (yas/field-end field) nil nil t)) + (overlay-put yas/active-field-overlay 'priority 100) (overlay-put yas/active-field-overlay 'face 'yas/field-highlight-face) (overlay-put yas/active-field-overlay 'yas/snippet snippet) (overlay-put yas/active-field-overlay 'modification-hooks '(yas/on-field-overlay-modification)) @@ -3067,9 +3075,9 @@ Meant to be called in a narrowed buffer, does various passes" ;; Reset the yas/dollar-regions ;; (setq yas/dollar-regions nil) - ;; protect quote and backquote escapes + ;; protect escaped quote, backquotes and backslashes ;; - (yas/protect-escapes nil '(?` ?')) + (yas/protect-escapes nil '(?\\ ?` ?')) ;; replace all backquoted expressions ;; (goto-char parse-start)