* Adjusting the yas/trigger-key mechanism, now can be set after require but before yas/initialize.

* Indented the buffe
* Some cleanup to the yas/minor-mode-keymap initialization mechanism
* Some commenting
This commit is contained in:
capitaomorte 2009-09-13 14:34:12 +00:00
parent 7b8da4357d
commit 97e7491dfa

View File

@ -242,6 +242,8 @@ representation using `read-kbd-macro'."
(let ((old (and (boundp symbol) (let ((old (and (boundp symbol)
(symbol-value symbol)))) (symbol-value symbol))))
(set-default symbol key) (set-default symbol key)
;; On very first loading of this defcustom,
;; `yas/trigger-key' is *not* loaded.
(if (fboundp 'yas/trigger-key-reload) (if (fboundp 'yas/trigger-key-reload)
(yas/trigger-key-reload old))))) (yas/trigger-key-reload old)))))
@ -401,10 +403,7 @@ This cafn only work when snippets are loaded from files."
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; User can also customize these ;; User can also customize the next defvars
(defvar yas/keymap nil
"The keymap active while a snippet expansion is in progress.")
(defun yas/define-some-keys (keys keymap definition) (defun yas/define-some-keys (keys keymap definition)
"Bind KEYS to DEFINITION in KEYMAP, read with `read-kbd-macro'." "Bind KEYS to DEFINITION in KEYMAP, read with `read-kbd-macro'."
(let ((keys (or (and (listp keys) keys) (let ((keys (or (and (listp keys) keys)
@ -412,7 +411,7 @@ This cafn only work when snippets are loaded from files."
(dolist (key keys) (dolist (key keys)
(define-key keymap (read-kbd-macro key) definition)))) (define-key keymap (read-kbd-macro key) definition))))
(defun yas/init-yas-in-snippet-keymap () (defvar yas/keymap
(let ((map (make-sparse-keymap))) (let ((map (make-sparse-keymap)))
(mapc #'(lambda (binding) (mapc #'(lambda (binding)
(yas/define-some-keys (car binding) map (cdr binding))) (yas/define-some-keys (car binding) map (cdr binding)))
@ -420,9 +419,8 @@ This cafn only work when snippets are loaded from files."
(,yas/prev-field-key . yas/prev-field) (,yas/prev-field-key . yas/prev-field)
("C-g" . yas/abort-snippet) ("C-g" . yas/abort-snippet)
(,yas/skip-and-clear-key . yas/skip-and-clear-or-delete-char))) (,yas/skip-and-clear-key . yas/skip-and-clear-or-delete-char)))
(setq yas/keymap map))) map)
"The keymap active while a snippet expansion is in progress.")
(yas/init-yas-in-snippet-keymap)
(defvar yas/key-syntaxes (list "w" "w_" "w_." "^ ") (defvar yas/key-syntaxes (list "w" "w_" "w_." "^ ")
"A list of syntax of a key. This list is tried in the order "A list of syntax of a key. This list is tried in the order
@ -460,47 +458,57 @@ Attention: These hooks are not run when exiting nested/stackd snippet expansion!
'face)))) 'face))))
'(require-snippet-condition . force-in-comment) '(require-snippet-condition . force-in-comment)
t) t)
"Condition to yasnippet local to each buffer. "Snippet expanding condition.
The default value helps filtering out potential snippet This variable is a lisp form:
expansions inside comments and string literals, unless the
snippet itself contains a condition that returns the symbol
`force-in-comment'.
* If yas/buffer-local-condition evaluate to nil, snippet * If it evaluates to nil, no snippets can be expanded.
won't be expanded.
* If it evaluate to the a cons cell where the car is the * If it evaluates to the a cons (require-snippet-condition
symbol `require-snippet-condition' and the cdr is a . REQUIREMENT)
symbol (let's call it \"requirement\"):
* If the snippet has no condition, then it won't be
expanded.
* If the snippet has a condition but it evaluates to nil or
error occured during evaluation, it won't be expanded.
* If the snippet has a condition that evaluate to
non-nil (let's call it \"result\"):
* If \"requirement\" is t, the snippet is ready to be
expanded.
* If \"requirement\" is eq to \"result\", the snippet is ready
to be expanded.
* Otherwise the snippet won't be expanded.
* If it evaluates to `always', snippet is unconditionally * Snippets bearing no \"# condition:\" directive are not
expanded. considered
* If it evaluates to other non-nil value: * Snippets bearing conditions that evaluate to nil (or
* If the snippet has no condition, or has a condition that produce an error) won't be onsidered.
evaluate to non-nil, it is ready to be expanded.
* Otherwise, it won't be expanded.
Here's an example: * If the snippet has a condition that evaluates to non-nil
RESULT:
* If REQUIREMENT is t, the snippet is considered
* If REQUIREMENT is `eq' RESULT, the snippet is
considered
* Otherwise, the snippet is not considered.
* If it evaluates to the symbol 'always, all snippets are
considered for expansion, regardless of any conditions.
* If it evaluates to t or some other non-nil value
* Snippet bearing no conditions, or conditions that
evaluate to non-nil, are considered for expansion.
* Otherwise, the snippet is not considered.
Here's an example preventing snippets from being expanded from
inside comments, in `python-mode' only, with the exception of
snippets returning the symbol 'force-in-comment in their
conditions.
(add-hook 'python-mode-hook (add-hook 'python-mode-hook
'(lambda () '(lambda ()
(setq yas/buffer-local-condition (setq yas/buffer-local-condition
'(if (python-in-string/comment) '(if (python-in-string/comment)
'(require-snippet-condition . force-in-comment) '(require-snippet-condition . force-in-comment)
t))))") t))))
The default value is similar, it filters out potential snippet
expansions inside comments and string literals, unless the
snippet itself contains a condition that returns the symbol
`force-in-comment'.")
(make-variable-buffer-local 'yas/buffer-local-condition) (make-variable-buffer-local 'yas/buffer-local-condition)
@ -558,134 +566,135 @@ Here's an example:
;; XXX: `last-buffer-undo-list' is somehow needed in Carbon Emacs for MacOSX ;; XXX: `last-buffer-undo-list' is somehow needed in Carbon Emacs for MacOSX
(defvar last-buffer-undo-list nil) (defvar last-buffer-undo-list nil)
(defvar yas/minor-mode-map (make-sparse-keymap) (defvar yas/minor-mode-menu nil
"The keymap used when `yas/minor-mode' is active.") "Holds the YASnippet menu")
(defvar yas/minor-mode-menu (make-sparse-keymap)
"Holds the YASnippet menu. For use with `easy-menu-define'.")
(defun yas/init-minor-keymap () (defun yas/init-minor-keymap ()
(easy-menu-define yas/minor-mode-menu (let ((map (make-sparse-keymap)))
yas/minor-mode-map (easy-menu-define yas/minor-mode-menu
"Menu used when YAS/minor-mode is active." map
'("YASnippet" "Menu used when YAS/minor-mode is active."
"----" '("YASnippet"
["Expand trigger" yas/expand "----"
:help "Possibly expand tab trigger before point"] ["Expand trigger" yas/expand
["Insert at point..." yas/insert-snippet :help "Possibly expand tab trigger before point"]
:help "Prompt for an expandable snippet and expand it at point"] ["Insert at point..." yas/insert-snippet
["New snippet..." yas/new-snippet :help "Prompt for an expandable snippet and expand it at point"]
:help "Create a new snippet in an appropriate directory"] ["New snippet..." yas/new-snippet
["Visit snippet file..." yas/visit-snippet-file :help "Create a new snippet in an appropriate directory"]
:help "Prompt for an expandable snippet and find its file"] ["Visit snippet file..." yas/visit-snippet-file
["Find snippets..." yas/find-snippets :help "Prompt for an expandable snippet and find its file"]
:help "Invoke `find-file' in the appropriate snippet directory"] ["Find snippets..." yas/find-snippets
"----" :help "Invoke `find-file' in the appropriate snippet directory"]
("Snippet menu behaviour" "----"
["Visit snippets" (setq yas/visit-from-menu t) ("Snippet menu behaviour"
:help "Visit snippets from the menu" ["Visit snippets" (setq yas/visit-from-menu t)
:active t :style radio :selected yas/visit-from-menu] :help "Visit snippets from the menu"
["Expand snippets" (setq yas/visit-from-menu nil) :active t :style radio :selected yas/visit-from-menu]
:help "Expand snippets from the menu" ["Expand snippets" (setq yas/visit-from-menu nil)
:active t :style radio :selected (not yas/visit-from-menu)] :help "Expand snippets from the menu"
"----" :active t :style radio :selected (not yas/visit-from-menu)]
["Show \"Real\" modes only" (setq yas/use-menu 'real-modes) "----"
:help "Show snippet submenus for modes that appear to be real major modes" ["Show \"Real\" modes only" (setq yas/use-menu 'real-modes)
:active t :style radio :selected (eq yas/use-menu 'real-modes)] :help "Show snippet submenus for modes that appear to be real major modes"
["Show all modes" (setq yas/use-menu 't) :active t :style radio :selected (eq yas/use-menu 'real-modes)]
:help "Show one snippet submenu for each loaded table" ["Show all modes" (setq yas/use-menu 't)
:active t :style radio :selected (eq yas/use-menu 't)] :help "Show one snippet submenu for each loaded table"
["Abbreviate according to current mode" (setq yas/use-menu 'abbreviate) :active t :style radio :selected (eq yas/use-menu 't)]
:help "Show only snippet submenus for the current active modes" ["Abbreviate according to current mode" (setq yas/use-menu 'abbreviate)
:active t :style radio :selected (eq yas/use-menu 'abbreviate)]) :help "Show only snippet submenus for the current active modes"
("Indenting" :active t :style radio :selected (eq yas/use-menu 'abbreviate)])
["Auto" (setq yas/indent-line 'auto) ("Indenting"
:help "Indent each line of the snippet with `indent-according-to-mode'" ["Auto" (setq yas/indent-line 'auto)
:active t :style radio :selected (eq yas/indent-line 'auto)] :help "Indent each line of the snippet with `indent-according-to-mode'"
["Fixed" (setq yas/indent-line 'fixed) :active t :style radio :selected (eq yas/indent-line 'auto)]
:help "Indent the snippet to the current column" ["Fixed" (setq yas/indent-line 'fixed)
:active t :style radio :selected (eq yas/indent-line 'fixed)] :help "Indent the snippet to the current column"
["None" (setq yas/indent-line 'none) :active t :style radio :selected (eq yas/indent-line 'fixed)]
:help "Don't apply any particular snippet indentation after expansion" ["None" (setq yas/indent-line 'none)
:active t :style radio :selected (not (member yas/indent-line '(fixed auto)))] :help "Don't apply any particular snippet indentation after expansion"
"----" :active t :style radio :selected (not (member yas/indent-line '(fixed auto)))]
["Also auto indent first line" (setq yas/also-auto-indent-first-line "----"
(not yas/also-auto-indent-first-line)) ["Also auto indent first line" (setq yas/also-auto-indent-first-line
:help "When auto-indenting also, auto indent the first line menu" (not yas/also-auto-indent-first-line))
:active (eq yas/indent-line 'auto) :help "When auto-indenting also, auto indent the first line menu"
:style toggle :selected yas/also-auto-indent-first-line] :active (eq yas/indent-line 'auto)
) :style toggle :selected yas/also-auto-indent-first-line]
("Prompting method" )
["System X-widget" (setq yas/prompt-functions ("Prompting method"
(cons 'yas/x-prompt ["System X-widget" (setq yas/prompt-functions
(remove 'yas/x-prompt (cons 'yas/x-prompt
(remove 'yas/x-prompt
yas/prompt-functions)))
:help "Use your windowing system's (gtk, mac, windows, etc...) default menu"
:active t :style radio :selected (eq (car yas/prompt-functions)
'yas/x-prompt)]
["Dropdown-list" (setq yas/prompt-functions
(cons 'yas/dropdown-prompt
(remove 'yas/dropdown-prompt
yas/prompt-functions))) yas/prompt-functions)))
:help "Use your windowing system's (gtk, mac, windows, etc...) default menu" :help "Use a special dropdown list"
:active t :style radio :selected (eq (car yas/prompt-functions) :active t :style radio :selected (eq (car yas/prompt-functions)
'yas/x-prompt)] 'yas/dropdown-prompt)]
["Dropdown-list" (setq yas/prompt-functions ["Ido" (setq yas/prompt-functions
(cons 'yas/dropdown-prompt (cons 'yas/ido-prompt
(remove 'yas/dropdown-prompt (remove 'yas/ido-prompt
yas/prompt-functions))) yas/prompt-functions)))
:help "Use a special dropdown list" :help "Use an ido-style minibuffer prompt"
:active t :style radio :selected (eq (car yas/prompt-functions) :active t :style radio :selected (eq (car yas/prompt-functions)
'yas/dropdown-prompt)] 'yas/ido-prompt)]
["Ido" (setq yas/prompt-functions ["Completing read" (setq yas/prompt-functions
(cons 'yas/ido-prompt (cons 'yas/completing-prompt
(remove 'yas/ido-prompt (remove 'yas/completing-prompt-prompt
yas/prompt-functions))) yas/prompt-functions)))
:help "Use an ido-style minibuffer prompt" :help "Use a normal minibuffer prompt"
:active t :style radio :selected (eq (car yas/prompt-functions) :active t :style radio :selected (eq (car yas/prompt-functions)
'yas/ido-prompt)] 'yas/completing-prompt-prompt)]
["Completing read" (setq yas/prompt-functions )
(cons 'yas/completing-prompt ("Misc"
(remove 'yas/completing-prompt-prompt ["Wrap region in exit marker"
yas/prompt-functions))) (setq yas/wrap-around-region
:help "Use a normal minibuffer prompt" (not yas/wrap-around-region))
:active t :style radio :selected (eq (car yas/prompt-functions) :help "If non-nil automatically wrap the selected text in the $0 snippet exit"
'yas/completing-prompt-prompt)] :style toggle :selected yas/wrap-around-region]
) ["Allow stacked expansions "
("Misc" (setq yas/triggers-in-field
["Wrap region in exit marker" (not yas/triggers-in-field))
(setq yas/wrap-around-region :help "If non-nil allow snippets to be triggered inside other snippet fields"
(not yas/wrap-around-region)) :style toggle :selected yas/triggers-in-field]
:help "If non-nil automatically wrap the selected text in the $0 snippet exit" ["Revive snippets on undo "
:style toggle :selected yas/wrap-around-region] (setq yas/snippet-revival
["Allow stacked expansions " (not yas/snippet-revival))
(setq yas/triggers-in-field :help "If non-nil allow snippets to become active again after undo"
(not yas/triggers-in-field)) :style toggle :selected yas/snippet-revival]
:help "If non-nil allow snippets to be triggered inside other snippet fields" ["Good grace "
:style toggle :selected yas/triggers-in-field] (setq yas/good-grace
["Revive snippets on undo " (not yas/good-grace))
(setq yas/snippet-revival :help "If non-nil don't raise errors in bad embedded eslip in snippets"
(not yas/snippet-revival)) :style toggle :selected yas/good-grace]
:help "If non-nil allow snippets to become active again after undo" ["Ignore filenames as triggers"
:style toggle :selected yas/snippet-revival] (setq yas/ignore-filenames-as-triggers
["Good grace " (not yas/ignore-filenames-as-triggers))
(setq yas/good-grace :help "If non-nil don't derive tab triggers from filenames"
(not yas/good-grace)) :style toggle :selected yas/ignore-filenames-as-triggers]
:help "If non-nil don't raise errors in bad embedded eslip in snippets" )
:style toggle :selected yas/good-grace] "----"
["Ignore filenames as triggers" ["Load snippets..." yas/load-directory
(setq yas/ignore-filenames-as-triggers :help "Load snippets from a specific directory"]
(not yas/ignore-filenames-as-triggers)) ["Reload everything" yas/reload-all
:help "If non-nil don't derive tab triggers from filenames" :help "Cleanup stuff, reload snippets, rebuild menus"]
:style toggle :selected yas/ignore-filenames-as-triggers] ["About" yas/about
) :help "Display some information about YASsnippet"]))
"----" ;; Now for the stuff that has direct keybindings
["Load snippets..." yas/load-directory ;;
:help "Load snippets from a specific directory"] (define-key map "\C-c&\C-s" 'yas/insert-snippet)
["Reload everything" yas/reload-all (define-key map "\C-c&\C-n" 'yas/new-snippet)
:help "Cleanup stuff, reload snippets, rebuild menus"] (define-key map "\C-c&\C-v" 'yas/visit-snippet-file)
["About" yas/about (define-key map "\C-c&\C-f" 'yas/find-snippets)
:help "Display some information about YASsnippet"])) map))
;; Now for the stuff that has direct keybindings
;; (defvar yas/minor-mode-map (yas/init-minor-keymap)
(yas/trigger-key-reload) "The keymap used when `yas/minor-mode' is active.")
(define-key yas/minor-mode-map "\C-c&\C-s" 'yas/insert-snippet)
(define-key yas/minor-mode-map "\C-c&\C-n" 'yas/new-snippet)
(define-key yas/minor-mode-map "\C-c&\C-v" 'yas/visit-snippet-file)
(define-key yas/minor-mode-map "\C-c&\C-f" 'yas/find-snippets))
(defun yas/trigger-key-reload (&optional unbind-key) (defun yas/trigger-key-reload (&optional unbind-key)
"Rebind `yas/expand' to the new value of `yas/trigger-key'. "Rebind `yas/expand' to the new value of `yas/trigger-key'.
@ -701,9 +710,6 @@ With optional UNBIND-KEY, try to unbind that key from
(not (string= yas/trigger-key ""))) (not (string= yas/trigger-key "")))
(define-key yas/minor-mode-map (read-kbd-macro yas/trigger-key) 'yas/expand))) (define-key yas/minor-mode-map (read-kbd-macro yas/trigger-key) 'yas/expand)))
;;; eval'ed on require/load
(yas/init-minor-keymap)
;;;###autoload ;;;###autoload
(define-minor-mode yas/minor-mode (define-minor-mode yas/minor-mode
"Toggle YASnippet mode. "Toggle YASnippet mode.
@ -724,13 +730,7 @@ Key bindings:
" yas" " yas"
:group 'yasnippet :group 'yasnippet
(when yas/minor-mode (when yas/minor-mode
;; when turning on the minor mode. (yas/trigger-key-reload)
;;
;; re-read the `yas/trigger-key' if a `yas/minor-mode-map' is
;; already built. Else, call `yas/init-minor-keymap' to build it
(unless (and (cdr yas/minor-mode-map)
(yas/trigger-key-reload))
(yas/init-minor-keymap))
;; load all snippets definitions unless we still don't have a ;; load all snippets definitions unless we still don't have a
;; root-directory or some snippets have already been loaded. ;; root-directory or some snippets have already been loaded.
(unless (or (null yas/root-directory) (unless (or (null yas/root-directory)
@ -789,27 +789,25 @@ Do this unless `yas/dont-activate' is t or the function
("}" ("}"
(0 font-lock-keyword-face))))) (0 font-lock-keyword-face)))))
(defvar snippet-mode-map (make-sparse-keymap) (defun yas/init-major-keymap ()
(let ((map (make-sparse-keymap)))
(easy-menu-define nil
map
"Menu used when snippet-mode is active."
(cons "Snippet"
(mapcar #'(lambda (ent)
(when (third ent)
(define-key map (third ent) (second ent)))
(vector (first ent) (second ent) t))
(list
(list "Load this snippet" 'yas/load-snippet-buffer "\C-c\C-c")
(list "Try out this snippet" 'yas/tryout-snippet "\C-c\C-t")))))
map))
(defvar snippet-mode-map
(yas/init-major-keymap)
"The keymap used when `snippet-mode' is active") "The keymap used when `snippet-mode' is active")
(defvar yas/major-mode-menu (make-sparse-keymap)
"Holds the snippet-mode menu. For use with `easy-menu-define'.")
(defun yas/init-major-keymap ()
(easy-menu-define yas/major-mode-menu
snippet-mode-map
"Menu used when snippet-mode is active."
(cons "Snippet"
(mapcar #'(lambda (ent)
(when (third ent)
(define-key snippet-mode-map (third ent) (second ent)))
(vector (first ent) (second ent) t))
(list
(list "Load this snippet" 'yas/load-snippet-buffer "\C-c\C-c")
(list "Try out this snippet" 'yas/tryout-snippet "\C-c\C-t"))))))
(progn
(yas/init-major-keymap))
(define-derived-mode snippet-mode text-mode "Snippet" (define-derived-mode snippet-mode text-mode "Snippet"
"A mode for editing yasnippets" "A mode for editing yasnippets"
@ -917,7 +915,7 @@ Has the following fields:
(maphash #'(lambda (k v) (maphash #'(lambda (k v)
(push (cons k v) alist)) (push (cons k v) alist))
namehash) namehash)
alist))))) alist)))))
;; Filtering/condition logic ;; Filtering/condition logic
@ -1253,11 +1251,11 @@ Here's a list of currently recognized variables:
(if yas/visit-from-menu (if yas/visit-from-menu
(yas/visit-snippet-file-1 template) (yas/visit-snippet-file-1 template)
(let ((where (if mark-active (let ((where (if mark-active
(cons (region-beginning) (region-end)) (cons (region-beginning) (region-end))
(cons (point) (point))))) (cons (point) (point)))))
(yas/expand-snippet (yas/template-content template) (yas/expand-snippet (yas/template-content template)
(car where) (car where)
(cdr where))))) (cdr where)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Popping up for keys and templates ;; Popping up for keys and templates
@ -1429,17 +1427,11 @@ content of the file is the template."
(setq yas/snippet-tables (make-hash-table)) (setq yas/snippet-tables (make-hash-table))
(setq yas/menu-table (make-hash-table)) (setq yas/menu-table (make-hash-table))
;; The minor mode and major mode keymap's cdr set to nil (this is ;; Init the `yas/minor-mode-map', taking care not to break the
;; the same as `make-sparse-keymap;) ;; menu....
(setf (cdr yas/minor-mode-menu) nil)
(setf (cdr yas/minor-mode-map) nil)
(setf (cdr yas/major-mode-menu) nil)
(setf (cdr snippet-mode-map) nil)
;; Initialize both keymaps
;; ;;
(yas/init-minor-keymap) (setf (cdr yas/minor-mode-map)
(yas/init-major-keymap) (cdr (yas/init-minor-keymap)))
;; Now, clean up the other keymaps we might have cluttered up. ;; Now, clean up the other keymaps we might have cluttered up.
(yas/kill-snippet-keybindings) (yas/kill-snippet-keybindings)
@ -1760,7 +1752,7 @@ not need to be a real mode."
,(yas/make-menu-binding template) ,(yas/make-menu-binding template)
:help ,name :help ,name
:keys ,(when (and key name) :keys ,(when (and key name)
(concat key yas/trigger-symbol)))))))))) (concat key yas/trigger-symbol))))))))))
(defun yas/show-menu-p (mode) (defun yas/show-menu-p (mode)
(cond ((eq yas/use-menu 'abbreviate) (cond ((eq yas/use-menu 'abbreviate)
@ -2429,7 +2421,7 @@ delegate to `yas/next-field'."
(yas/text (yas/field-text-for-display active-field)) (yas/text (yas/field-text-for-display active-field))
(text yas/text) (text yas/text)
(yas/modified-p (yas/field-modified-p active-field))) (yas/modified-p (yas/field-modified-p active-field)))
;;; primary field transform: exit call to field-transform ;; primary field transform: exit call to field-transform
(yas/read-and-eval-string (yas/field-transform active-field)))) (yas/read-and-eval-string (yas/field-transform active-field))))
;; Now actually move... ;; Now actually move...
(cond ((>= target-pos (length live-fields)) (cond ((>= target-pos (length live-fields))
@ -2452,7 +2444,7 @@ Also create some protection overlays"
(setf (yas/snippet-active-field snippet) field) (setf (yas/snippet-active-field snippet) field)
(yas/place-overlays snippet field) (yas/place-overlays snippet field)
(overlay-put yas/active-field-overlay 'yas/field field) (overlay-put yas/active-field-overlay 'yas/field field)
;;; primary field transform: first call to snippet transform ;; primary field transform: first call to snippet transform
(unless (yas/field-modified-p field) (unless (yas/field-modified-p field)
(if (yas/field-update-display field snippet) (if (yas/field-update-display field snippet)
(let ((inhibit-modification-hooks t)) (let ((inhibit-modification-hooks t))
@ -2713,7 +2705,7 @@ progress."
(let ((field (overlay-get yas/active-field-overlay 'yas/field))) (let ((field (overlay-get yas/active-field-overlay 'yas/field)))
(cond (after? (cond (after?
(yas/advance-end-maybe field (overlay-end overlay)) (yas/advance-end-maybe field (overlay-end overlay))
;;; primary field transform: normal calls to expression ;;; primary field transform: normal calls to expression
(let ((saved-point (point))) (let ((saved-point (point)))
(yas/field-update-display field (car (yas/snippets-at-point))) (yas/field-update-display field (car (yas/snippets-at-point)))
(goto-char saved-point)) (goto-char saved-point))
@ -2831,7 +2823,7 @@ will be deleted before inserting template."
(prog1 (buffer-substring-no-properties (region-beginning) (prog1 (buffer-substring-no-properties (region-beginning)
(region-end)) (region-end))
(unless start (setq start (region-beginning)) (unless start (setq start (region-beginning))
(unless end (setq end (region-end))))))) (unless end (setq end (region-end)))))))
(when start (when start
(goto-char start)) (goto-char start))
@ -2873,7 +2865,7 @@ will be deleted before inserting template."
(read expand-env) (read expand-env)
(error nil)))) (error nil))))
(eval `(let ,read-vars (eval `(let ,read-vars
(yas/snippet-create (point-min) (point-max))))) (yas/snippet-create (point-min) (point-max)))))
(yas/snippet-create (point-min) (point-max)))))) (yas/snippet-create (point-min) (point-max))))))
;; stacked-expansion: This checks for stacked expansion, save the ;; stacked-expansion: This checks for stacked expansion, save the
@ -2958,8 +2950,6 @@ Returns the newly created snippet."
;; Sort and link each field ;; Sort and link each field
(yas/snippet-sort-fields snippet) (yas/snippet-sort-fields snippet)
;; (yas/update-mirrors snippet) ;; XXX: WHY was this here for so long...
;; Create keymap overlay for snippet ;; Create keymap overlay for snippet
(setf (yas/snippet-control-overlay snippet) (setf (yas/snippet-control-overlay snippet)
(yas/make-control-overlay snippet (point-min) (point-max))) (yas/make-control-overlay snippet (point-min) (point-max)))
@ -3248,10 +3238,10 @@ With optional string TEXT do it in string instead of the buffer."
"Replace all the \"`(lisp-expression)`\"-style expression "Replace all the \"`(lisp-expression)`\"-style expression
with their evaluated value" with their evaluated value"
(while (re-search-forward yas/backquote-lisp-expression-regexp nil t) (while (re-search-forward yas/backquote-lisp-expression-regexp nil t)
(let ((transformed (yas/read-and-eval-string (yas/restore-escapes (match-string 1))))) (let ((transformed (yas/read-and-eval-string (yas/restore-escapes (match-string 1)))))
(goto-char (match-end 0)) (goto-char (match-end 0))
(when transformed (insert transformed)) (when transformed (insert transformed))
(delete-region (match-beginning 0) (match-end 0))))) (delete-region (match-beginning 0) (match-end 0)))))
(defun yas/scan-sexps (from count) (defun yas/scan-sexps (from count)
(condition-case err (condition-case err
@ -3334,20 +3324,20 @@ When multiple expressions are found, only the last one counts."
(defun yas/transform-mirror-parse-create (snippet) (defun yas/transform-mirror-parse-create (snippet)
"Parse the \"${n:$(lisp-expression)}\" mirror transformations." "Parse the \"${n:$(lisp-expression)}\" mirror transformations."
(while (re-search-forward yas/transform-mirror-regexp nil t) (while (re-search-forward yas/transform-mirror-regexp nil t)
(let* ((real-match-end-0 (yas/scan-sexps (1+ (match-beginning 0)) 1)) (let* ((real-match-end-0 (yas/scan-sexps (1+ (match-beginning 0)) 1))
(number (string-to-number (match-string-no-properties 1))) (number (string-to-number (match-string-no-properties 1)))
(field (and number (field (and number
(not (zerop number)) (not (zerop number))
(yas/snippet-find-field snippet number)))) (yas/snippet-find-field snippet number))))
(when (and real-match-end-0 (when (and real-match-end-0
field) field)
(push (yas/make-mirror (yas/make-marker (match-beginning 0)) (push (yas/make-mirror (yas/make-marker (match-beginning 0))
(yas/make-marker (match-beginning 0)) (yas/make-marker (match-beginning 0))
(yas/restore-escapes (yas/restore-escapes
(buffer-substring-no-properties (match-beginning 2) (buffer-substring-no-properties (match-beginning 2)
(1- real-match-end-0)))) (1- real-match-end-0))))
(yas/field-mirrors field)) (yas/field-mirrors field))
(push (cons (match-beginning 0) real-match-end-0) yas/dollar-regions))))) (push (cons (match-beginning 0) real-match-end-0) yas/dollar-regions)))))
(defun yas/simple-mirror-parse-create (snippet) (defun yas/simple-mirror-parse-create (snippet)
"Parse the simple \"$n\" mirrors and the exit-marker." "Parse the simple \"$n\" mirrors and the exit-marker."
@ -3395,52 +3385,52 @@ When multiple expressions are found, only the last one counts."
(defun yas/update-mirrors (snippet) (defun yas/update-mirrors (snippet)
"Updates all the mirrors of SNIPPET." "Updates all the mirrors of SNIPPET."
(save-excursion (save-excursion
(dolist (field (yas/snippet-fields snippet)) (dolist (field (yas/snippet-fields snippet))
(dolist (mirror (yas/field-mirrors field)) (dolist (mirror (yas/field-mirrors field))
;; stacked expansion: I added an `inhibit-modification-hooks' ;; stacked expansion: I added an `inhibit-modification-hooks'
;; here, for safety, may need to remove if we the mechanism is ;; here, for safety, may need to remove if we the mechanism is
;; altered. ;; altered.
;;
(let ((inhibit-modification-hooks t))
(yas/mirror-update-display mirror field)
;; `yas/place-overlays' is needed if the active field and
;; protected overlays have been changed because of insertions
;; in `yas/mirror-update-display'
;; ;;
(when (eq field (yas/snippet-active-field snippet)) (let ((inhibit-modification-hooks t))
(yas/place-overlays snippet field))))))) (yas/mirror-update-display mirror field)
;; `yas/place-overlays' is needed if the active field and
;; protected overlays have been changed because of insertions
;; in `yas/mirror-update-display'
;;
(when (eq field (yas/snippet-active-field snippet))
(yas/place-overlays snippet field)))))))
(defun yas/mirror-update-display (mirror field) (defun yas/mirror-update-display (mirror field)
"Update MIRROR according to FIELD (and mirror transform)." "Update MIRROR according to FIELD (and mirror transform)."
(let ((reflection (or (yas/apply-transform mirror field) (let ((reflection (or (yas/apply-transform mirror field)
(yas/field-text-for-display field)))) (yas/field-text-for-display field))))
(when (and reflection (when (and reflection
(not (string= reflection (buffer-substring-no-properties (yas/mirror-start mirror) (not (string= reflection (buffer-substring-no-properties (yas/mirror-start mirror)
(yas/mirror-end mirror))))) (yas/mirror-end mirror)))))
(goto-char (yas/mirror-start mirror)) (goto-char (yas/mirror-start mirror))
(insert reflection) (insert reflection)
(if (> (yas/mirror-end mirror) (point)) (if (> (yas/mirror-end mirror) (point))
(delete-region (point) (yas/mirror-end mirror)) (delete-region (point) (yas/mirror-end mirror))
(set-marker (yas/mirror-end mirror) (point)) (set-marker (yas/mirror-end mirror) (point))
(yas/advance-start-maybe (yas/mirror-next mirror) (point)))))) (yas/advance-start-maybe (yas/mirror-next mirror) (point))))))
(defun yas/field-update-display (field snippet) (defun yas/field-update-display (field snippet)
"Much like `yas/mirror-update-display', but for fields" "Much like `yas/mirror-update-display', but for fields"
(when (yas/field-transform field) (when (yas/field-transform field)
(let ((inhibit-modification-hooks t) (let ((inhibit-modification-hooks t)
(transformed (yas/apply-transform field field)) (transformed (yas/apply-transform field field))
(point (point))) (point (point)))
(when (and transformed (when (and transformed
(not (string= transformed (buffer-substring-no-properties (yas/field-start field) (not (string= transformed (buffer-substring-no-properties (yas/field-start field)
(yas/field-end field))))) (yas/field-end field)))))
(setf (yas/field-modified-p field) t) (setf (yas/field-modified-p field) t)
(goto-char (yas/field-start field)) (goto-char (yas/field-start field))
(insert transformed) (insert transformed)
(if (> (yas/field-end field) (point)) (if (> (yas/field-end field) (point))
(delete-region (point) (yas/field-end field)) (delete-region (point) (yas/field-end field))
(set-marker (yas/field-end field) (point)) (set-marker (yas/field-end field) (point))
(yas/advance-start-maybe (yas/field-next field) (point))) (yas/advance-start-maybe (yas/field-next field) (point)))
t)))) t))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -3593,8 +3583,8 @@ When multiple expressions are found, only the last one counts."
(snippet-mode) (snippet-mode)
(yas/minor-mode 1) (yas/minor-mode 1)
(let ((abbrev)) (let ((abbrev))
(setq abbrev "$f") (setq abbrev "$f")
(insert abbrev)) (insert abbrev))
(unless quiet (unless quiet
(add-hook 'post-command-hook 'yas/debug-snippet-vars 't 'local))) (add-hook 'post-command-hook 'yas/debug-snippet-vars 't 'local)))
@ -3666,15 +3656,15 @@ and return the directory. Return nil if not found."
handle the end-of-buffer error fired in it by calling handle the end-of-buffer error fired in it by calling
`forward-char' at the end of buffer." `forward-char' at the end of buffer."
(condition-case err (condition-case err
ad-do-it ad-do-it
(error (message (error-message-string err))))) (error (message (error-message-string err)))))
;; disable c-electric-* serial command in YAS fields ;; disable c-electric-* serial command in YAS fields
(add-hook 'c-mode-common-hook (add-hook 'c-mode-common-hook
'(lambda () '(lambda ()
(dolist (k '(":" ">" ";" "<" "{" "}")) (dolist (k '(":" ">" ";" "<" "{" "}"))
(define-key (symbol-value (make-local-variable 'yas/keymap)) (define-key (symbol-value (make-local-variable 'yas/keymap))
k 'self-insert-command)))) k 'self-insert-command))))
;;; yasnippet.el ends here ;;; yasnippet.el ends here