* 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
This commit is contained in:
capitaomorte 2009-11-02 18:08:20 +00:00
parent d6bf2427ea
commit 3d4d06df1d
7 changed files with 115 additions and 77 deletions

View File

@ -0,0 +1 @@
text-mode

View File

@ -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</${1:$(yas/html-first-word yas/text)}>%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>%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
"</" mirror ">")))
(setq template (replace-regexp-in-string "\n" (concat "</" mirror ">\n<" mirror ">") template))
(yas/expand-snippet template)))
(defun yas/html-between-tag-pair-p ()
(save-excursion
(backward-word)
(looking-at "\\\w+></\\\w+>")))

View File

@ -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: &lt;word&gt;&lt;/word&gt;
# It will recognize HTML 4.0 tags that need no close tag.
#
# With no current word, it will insert: &lt;p&gt;&lt;/p&gt; 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 &lt; tag.length &amp;&amp; !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 "&lt;#{tag}#{xhtml}&gt;"
when single then "&lt;#{tag} $1#{xhtml}&gt;"
when '' then "&lt;${1:#{ENV['TM_DEFAULT_TAG'] || 'p'}}&gt;$2&lt;/${1/\\s.*//}&gt;"
else "&lt;#{tag}&gt;$1&lt;/#{tag.strip[/^\S+/]}&gt;"
end
print suffix
(yas/html-insert-tag-pair-snippet)

View File

@ -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

View File

@ -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`</${1/\s.*//}>
<${1:p}>`yas/selected-text`</${1:$(yas/html-first-word yas/text)}>

View File

@ -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"
# --
<?= `yas/selected-text` ?>
<?= $0 ?>

View File

@ -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 ((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 "the command `%s'." (pp-to-string fallback)))
"nothing, in this case."))))
(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
", 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))))
(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 "the command `%s'." (pp-to-string fallback)))
"nothing, in this case."))))
(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.