From 3d4d06df1da8768482b9abecfd0d516896fdd215 Mon Sep 17 00:00:00 2001 From: capitaomorte Date: Mon, 2 Nov 2009 18:08:20 +0000 Subject: [PATCH] * Working on the html-bundle. Major work done, details missing... * Fixed an aesthetic describe-key bug. * A serious "kill-line from snippet field" bug lurks somewhere --- extras/imported/html-mode/.yas-parents | 1 + extras/imported/html-mode/.yas-setup.el | 71 +++++++++++++++++++ .../html-mode/Insert Tag Pair.yasnippet | 45 +----------- ...Smart returnindent for tag pairs.yasnippet | 4 +- .../html-mode/Wrap Selection In Tag.yasnippet | 8 +-- .../html-mode/Wrap in qmark=.yasnippet | 3 +- yasnippet.el | 60 ++++++++-------- 7 files changed, 115 insertions(+), 77 deletions(-) create mode 100644 extras/imported/html-mode/.yas-parents create mode 100644 extras/imported/html-mode/.yas-setup.el diff --git a/extras/imported/html-mode/.yas-parents b/extras/imported/html-mode/.yas-parents new file mode 100644 index 0000000..d58dacb --- /dev/null +++ b/extras/imported/html-mode/.yas-parents @@ -0,0 +1 @@ +text-mode \ No newline at end of file diff --git a/extras/imported/html-mode/.yas-setup.el b/extras/imported/html-mode/.yas-setup.el new file mode 100644 index 0000000..69d49c9 --- /dev/null +++ b/extras/imported/html-mode/.yas-setup.el @@ -0,0 +1,71 @@ +(defvar yas/html-default-tag "p") + +(defvar yas/html-xhtml-attr "") + +(defvar yas/html-just-like-tm nil + "Html-mode snippets behave as close to TextMate as possible.") + + +(defun yas/html-remove-preceding-word () + (interactive) + (let (word-begin + word-end + (orig-point (point)) + retval) + (save-excursion + (forward-word -1) + (setq word-begin (point)) + (forward-word 1) + (setq word-end (point))) + (when (and (> word-end word-begin) + (>= word-end (point))) + (setq retval + (cons + (buffer-substring-no-properties word-begin orig-point) + (buffer-substring-no-properties word-end orig-point))) + (delete-region word-begin word-end) + retval))) + + +(defun yas/html-first-word (string) + (replace-regexp-in-string "\\\W.*" "" string)) + +(defun yas/html-insert-tag-pair-snippet () + (let* ((tag-and-suffix (or (and yas/selected-text + (cons yas/selected-text nil)) + (yas/html-remove-preceding-word))) + (tag (car tag-and-suffix)) + (suffix (or (cdr tag-and-suffix) "")) + (single-no-arg "\\(br\\|hr\\)") + (single "\\(img\\|meta\\|link\\|input\\|base\\|area\\|col\\|frame\\|param\\)")) + (cond ((null tag) + (yas/expand-snippet (format "<${1:%s}>%s%s" + (or yas/html-default-tag + "p") + (if yas/html-just-like-tm "$2" "$0") + suffix))) + ((string-match single-no-arg tag) + (insert (format "<%s%s/>%s" tag yas/html-xhtml-attr suffix))) + ((string-match single tag) + (yas/expand-snippet (format "<%s $1%s/>%s" tag yas/html-xhtml-attr suffix))) + (t + (yas/expand-snippet (format "<%s>%s%s" + tag + (if yas/html-just-like-tm "$1" "$0") + (replace-regexp-in-string "\\\W.*" "" tag) + suffix)))))) + +(defun yas/html-wrap-each-line-in-openclose-tag () + (let* ((mirror "${1:$(yas/html-first-word yas/text)}") + (template (concat (format "<${1:%s}>" (or yas/html-default-tag "p")) + yas/selected-text + ""))) + (setq template (replace-regexp-in-string "\n" (concat "\n<" mirror ">") template)) + (yas/expand-snippet template))) + +(defun yas/html-between-tag-pair-p () + (save-excursion + (backward-word) + (looking-at "\\\w+>"))) + + diff --git a/extras/imported/html-mode/Insert Tag Pair.yasnippet b/extras/imported/html-mode/Insert Tag Pair.yasnippet index c6c43fa..4d77662 100644 --- a/extras/imported/html-mode/Insert Tag Pair.yasnippet +++ b/extras/imported/html-mode/Insert Tag Pair.yasnippet @@ -1,48 +1,9 @@ # -*- mode: snippet -*- # type: command -# contributor: joaotavora, adapted from TextMate HTML bundle, work in progress +# contributor: joaotavora, adapted from TextMate HTML bundle # name: Insert Open/Close Tag (With Current Word) +# expand-env: ((yas/wrap-around-region t)) # binding: "^<" ## condition: "text.html," # -- -(let* ((tag yas/selected-text) - (single-no-arg "\\(br\\|hr\\)") - (single "\\(img\\|meta\\|link\\|input\\|base\\|area\\|col\\|frame\\|param\\)")) - (cond ((string-match single-no-arg yas/selected-text)))) -# This script will expand the current word into: <word></word> -# It will recognize HTML 4.0 tags that need no close tag. -# -# With no current word, it will insert: <p></p> and allows you -# to overwrite the tag name and add potential arguments. -# -# The result is inserted as a snippet, so it's -# possible to tab through the place holders. - -# single tags -single_no_arg = /^(?:br|hr)$/i -single = /^(?:img|meta|link|input|base|area|col|frame|param)$/i - -# we are not in HTML mode, so let's scrap the above hardcoded tag lists -unless ENV.has_key? 'TM_HTML_EMPTY_TAGS' then - single_no_arg = /(?=not)possible/ - single = /(?=not)possible/ -end - -# handle the case where caret is in the middle of a word, assume only the left part is the tag -index = ENV['TM_LINE_INDEX'].to_i - ENV['TM_INPUT_START_LINE_INDEX'].to_i -tag, suffix = STDIN.read, '' -if index < tag.length && !ENV['TM_SELECTED_TEXT'] - tag, suffix = tag[0...index], tag[index..-1] -end - -xhtml = ENV['TM_XHTML'].to_s - -print case tag - when single_no_arg then "<#{tag}#{xhtml}>" - when single then "<#{tag} $1#{xhtml}>" - when '' then "<${1:#{ENV['TM_DEFAULT_TAG'] || 'p'}}>$2</${1/\\s.*//}>" - else "<#{tag}>$1</#{tag.strip[/^\S+/]}>" -end - -print suffix - +(yas/html-insert-tag-pair-snippet) diff --git a/extras/imported/html-mode/Smart returnindent for tag pairs.yasnippet b/extras/imported/html-mode/Smart returnindent for tag pairs.yasnippet index b68a0c3..b816c2d 100644 --- a/extras/imported/html-mode/Smart returnindent for tag pairs.yasnippet +++ b/extras/imported/html-mode/Smart returnindent for tag pairs.yasnippet @@ -1,8 +1,8 @@ # -*- mode: snippet -*- # contributor: Translated from TextMate Snippet # name: Special: Return Inside Empty Open/Close Tags -## binding: " " -## condition: "meta.scope.between-tag-pair" +# binding: "C-M" +# condition: (yas/html-between-tag-pair-p) # -- $0 diff --git a/extras/imported/html-mode/Wrap Selection In Tag.yasnippet b/extras/imported/html-mode/Wrap Selection In Tag.yasnippet index 9d3ab77..ee855b4 100644 --- a/extras/imported/html-mode/Wrap Selection In Tag.yasnippet +++ b/extras/imported/html-mode/Wrap Selection In Tag.yasnippet @@ -1,7 +1,7 @@ # -*- mode: snippet -*- -# contributor: Translated from TextMate Snippet +# contributor: joaotavora, adapted from TextMate HTML bundle # name: Wrap Selection in Open/Close Tag -## binding: "^W" -## condition: "text.html," +# binding: "C-S-w" +# condition: mark-active # -- -<${1:p}>`yas/selected-text` \ No newline at end of file +<${1:p}>`yas/selected-text` \ No newline at end of file diff --git a/extras/imported/html-mode/Wrap in qmark=.yasnippet b/extras/imported/html-mode/Wrap in qmark=.yasnippet index e021ca6..b8c43c9 100644 --- a/extras/imported/html-mode/Wrap in qmark=.yasnippet +++ b/extras/imported/html-mode/Wrap in qmark=.yasnippet @@ -1,6 +1,7 @@ # -*- mode: snippet -*- # contributor: Translated from TextMate Snippet # name: Wrap in +# expand-env: ((yas/wrap-around-region t)) ## condition: "text.html string" # -- - \ No newline at end of file + \ No newline at end of file diff --git a/yasnippet.el b/yasnippet.el index 87350e6..cd5b60e 100644 --- a/yasnippet.el +++ b/yasnippet.el @@ -3294,7 +3294,8 @@ SNIPPET-MARKERS." (forward-line 1))) (not (eobp)) (<= (point) end)) - (yas/indent-according-to-mode snippet-markers)))) + (yas/indent-according-to-mode snippet-markers)) + (yas/indent-according-to-mode snippet-markers))) (t nil))))) @@ -3584,44 +3585,47 @@ When multiple expressions are found, only the last one counts." (put 'yas/expand 'function-documentation '(yas/expand-from-trigger-key-doc)) (defun yas/expand-from-trigger-key-doc () "A doc synthethizer for `yas/expand-from-trigger-key-doc'." - (let ((which-means (cond ((eq yas/fallback-behavior 'call-other-command) - (concat ", which means that if no snippets are eligible then this\nfalls back to " - (let* ((yas/minor-mode nil) - (fallback (key-binding (read-kbd-macro yas/trigger-key)))) - (or (and fallback - (format "the command `%s'." (pp-to-string fallback))) - "nothing, in this case.")))) + (let ((fallback-description (cond ((eq yas/fallback-behavior 'call-other-command) + (let* ((yas/minor-mode nil) + (fallback (key-binding (read-kbd-macro yas/trigger-key)))) + (or (and fallback + (format " call command `%s'." (pp-to-string fallback))) + " do nothing."))) ((eq yas/fallback-behavior 'return-nil) - ", which means nothing is done if no snippets are eligible.") + ", do nothing.") (t - ", which means I hope you know what you're doing :-)")))) - (concat "Expand a snippet before point. - -If no snippet expansion is possible, fall back to the behaviour -defined in `yas/fallback-behavior' which in this case would be:\n\n" - (pp-to-string yas/fallback-behavior) - "\n\n" - which-means -"\n\nOptional argument FIELD is for non-interactive use and is an + ", defer to `yas/fallback-behaviour' :-)")))) + (concat "Expand a snippet before point. If no snippet +expansion is possible," + fallback-description + "\n\nOptional argument FIELD is for non-interactive use and is an object satisfying `yas/field-p' to restrict the expansion to."))) (put 'yas/expand-from-keymap 'function-documentation '(yas/expand-from-keymap-doc)) (defun yas/expand-from-keymap-doc () "A doc synthethizer for `yas/expand-from-keymap-doc'." - (concat "Expand some snippets from keymaps.\n\n" + (concat "Expand some snippets from keymaps.\n\nMay fall back to original binding." (when (eq this-command 'describe-key) (let* ((vec (this-single-command-keys)) (templates (mapcan #'(lambda (table) (yas/fetch table vec)) - (yas/get-snippet-tables)))) - (concat "In this particular case my guess it is would expand the snippets:\n\n" - (mapconcat #'car templates "\n")))) - "\n\nIf no snippet expansion is possible then this falls back to \n" - (let* ((yas/minor-mode nil) - (fallback (key-binding (read-kbd-macro yas/trigger-key)))) - (or (and fallback - (format "the command `%s'." (pp-to-string fallback))) - "nothing, in this case.")))) + (yas/get-snippet-tables))) + (yas/snippet-keymaps nil) + (fallback (key-binding vec))) + (concat "In this particular case\nmy guess is it would " + (when templates + (concat "expand the snippets:\n" + (yas/template-pretty-list templates) + "\n\nIf no expansion possible, ")) + (or (and fallback + (format "call command `%s'." (pp-to-string fallback))) + "do nothing.")))))) + +(defun yas/template-pretty-list (templates) + (let ((acc)) + (dolist (plate templates) + (setq acc (concat acc "\n*) " (car plate)))) + acc)) ;;; Debug functions. Use (or change) at will whenever needed.