From 3b6e9dda59e5cec1f7843c8c74a68439ca1d0387 Mon Sep 17 00:00:00 2001 From: capitaomorte Date: Sat, 25 Jul 2009 16:26:27 +0000 Subject: [PATCH] * Empty fields that are the last of the list *and* match the snippet's exit are skipped automatically. * Added a printf snippet to show off. * The printf snippet also exhibits the back-adjacent mirror bug --- snippets/text-mode/cc-mode/c-mode/printf | 20 ++++++++++++++++++++ snippets/text-mode/snippet-mode/mirror | 3 +-- yasnippet.el | 13 ++++++++----- 3 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 snippets/text-mode/cc-mode/c-mode/printf diff --git a/snippets/text-mode/cc-mode/c-mode/printf b/snippets/text-mode/cc-mode/c-mode/printf new file mode 100644 index 0000000..5401410 --- /dev/null +++ b/snippets/text-mode/cc-mode/c-mode/printf @@ -0,0 +1,20 @@ +# name : printf +# contributor : joaotavora +# description : a snippet that is very slightly buggy in 0.6.0b. There *must* be a space before the $2! +# +# While a per-snippet exit-hook property does not exist, this can be fixed with +# this in your .emacs +# +# (setq yas/after-exit-snippet-hook '(yas/munch-useless-spaces-at-end)) +# +# (defun yas/munch-useless-spaces-at-end () +# (let ((end (1- yas/snippet-end))) +# (while (= (char-after end) ?\s) +# (setq end (1- end)) +# (backward-char)) +# (delete-region (point) yas/snippet-end))) +# +# but this might break some other snippets... +# +# -- +printf ("${1:%s}\\n"${1:$(if (string-match "%" text) "," "\);")} $2${1:$(if (string-match "%" text) "\);" "")} \ No newline at end of file diff --git a/snippets/text-mode/snippet-mode/mirror b/snippets/text-mode/snippet-mode/mirror index 94c8e56..1c32eeb 100644 --- a/snippets/text-mode/snippet-mode/mirror +++ b/snippets/text-mode/snippet-mode/mirror @@ -2,5 +2,4 @@ # key : $m # contributor : joaotavora # -- -\${${2:n}:${4:\$(${5:reflection-fn})}\}$0 - +\${${2:n}:${4:\$(${5:reflection-fn})}\}$0 \ No newline at end of file diff --git a/yasnippet.el b/yasnippet.el index 084d2c1..7b828d2 100644 --- a/yasnippet.el +++ b/yasnippet.el @@ -1598,10 +1598,12 @@ have, compare through the field's start point" (< (yas/field-start field1) (yas/field-start field2)))))) -(defun yas/field-probably-deleted-p (field) - "Guess if FIELD was deleted because of his parent-field" +(defun yas/field-probably-deleted-p (snippet field) + "Guess if SNIPPET's FIELD should be skipped." (and (zerop (- (yas/field-start field) (yas/field-end field))) - (yas/field-parent-field field))) + (or (yas/field-parent-field field) + (and (eq field (car (last (yas/snippet-fields snippet)))) + (= (yas/field-start field) (overlay-end (yas/snippet-control-overlay snippet))))))) (defun yas/snippets-at-point (&optional all-snippets) "Return a sorted list of snippets at point, most recently @@ -1636,7 +1638,7 @@ delegate to `yas/next-field'." (active-field (overlay-get yas/active-field-overlay 'yas/field)) (live-fields (remove-if #'(lambda (field) (and (not (eq field active-field)) - (yas/field-probably-deleted-p field))) + (yas/field-probably-deleted-p snippet field))) (yas/snippet-fields snippet))) (active-field-pos (position active-field live-fields)) (target-pos (and active-field-pos (+ arg active-field-pos))) @@ -1848,7 +1850,8 @@ snippet, if so cleans up the whole snippet up." ;; (let* ((snippet (car (yas/snippets-at-point))) (target-field (and snippet - (find-if-not #'yas/field-probably-deleted-p + (find-if-not #'(lambda (field) + (yas/field-probably-deleted-p snippet field)) (remove nil (cons (yas/snippet-active-field snippet) (yas/snippet-fields snippet)))))))