* 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)
(symbol-value symbol))))
(set-default symbol key)
;; On very first loading of this defcustom,
;; `yas/trigger-key' is *not* loaded.
(if (fboundp 'yas/trigger-key-reload)
(yas/trigger-key-reload old)))))
@ -401,10 +403,7 @@ This cafn only work when snippets are loaded from files."
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; User can also customize these
(defvar yas/keymap nil
"The keymap active while a snippet expansion is in progress.")
;; User can also customize the next defvars
(defun yas/define-some-keys (keys keymap definition)
"Bind KEYS to DEFINITION in KEYMAP, read with `read-kbd-macro'."
(let ((keys (or (and (listp keys) keys)
@ -412,7 +411,7 @@ This cafn only work when snippets are loaded from files."
(dolist (key keys)
(define-key keymap (read-kbd-macro key) definition))))
(defun yas/init-yas-in-snippet-keymap ()
(defvar yas/keymap
(let ((map (make-sparse-keymap)))
(mapc #'(lambda (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)
("C-g" . yas/abort-snippet)
(,yas/skip-and-clear-key . yas/skip-and-clear-or-delete-char)))
(setq yas/keymap map)))
(yas/init-yas-in-snippet-keymap)
map)
"The keymap active while a snippet expansion is in progress.")
(defvar yas/key-syntaxes (list "w" "w_" "w_." "^ ")
"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))))
'(require-snippet-condition . force-in-comment)
t)
"Condition to yasnippet local to each buffer.
"Snippet expanding condition.
The default value helps filtering out potential snippet
expansions inside comments and string literals, unless the
snippet itself contains a condition that returns the symbol
`force-in-comment'.
This variable is a lisp form:
* If yas/buffer-local-condition evaluate to nil, snippet
won't be expanded.
* If it evaluates to nil, no snippets can be expanded.
* If it evaluate to the a cons cell where the car is the
symbol `require-snippet-condition' and the cdr is a
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 the a cons (require-snippet-condition
. REQUIREMENT)
* If it evaluates to `always', snippet is unconditionally
expanded.
* Snippets bearing no \"# condition:\" directive are not
considered
* If it evaluates to other non-nil value:
* If the snippet has no condition, or has a condition that
evaluate to non-nil, it is ready to be expanded.
* Otherwise, it won't be expanded.
* Snippets bearing conditions that evaluate to nil (or
produce an error) won't be onsidered.
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
'(lambda ()
(setq yas/buffer-local-condition
'(if (python-in-string/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)
@ -558,15 +566,13 @@ Here's an example:
;; XXX: `last-buffer-undo-list' is somehow needed in Carbon Emacs for MacOSX
(defvar last-buffer-undo-list nil)
(defvar yas/minor-mode-map (make-sparse-keymap)
"The keymap used when `yas/minor-mode' is active.")
(defvar yas/minor-mode-menu (make-sparse-keymap)
"Holds the YASnippet menu. For use with `easy-menu-define'.")
(defvar yas/minor-mode-menu nil
"Holds the YASnippet menu")
(defun yas/init-minor-keymap ()
(let ((map (make-sparse-keymap)))
(easy-menu-define yas/minor-mode-menu
yas/minor-mode-map
map
"Menu used when YAS/minor-mode is active."
'("YASnippet"
"----"
@ -681,11 +687,14 @@ Here's an example:
:help "Display some information about YASsnippet"]))
;; Now for the stuff that has direct keybindings
;;
(yas/trigger-key-reload)
(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))
(define-key map "\C-c&\C-s" 'yas/insert-snippet)
(define-key map "\C-c&\C-n" 'yas/new-snippet)
(define-key map "\C-c&\C-v" 'yas/visit-snippet-file)
(define-key map "\C-c&\C-f" 'yas/find-snippets)
map))
(defvar yas/minor-mode-map (yas/init-minor-keymap)
"The keymap used when `yas/minor-mode' is active.")
(defun yas/trigger-key-reload (&optional unbind-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 "")))
(define-key yas/minor-mode-map (read-kbd-macro yas/trigger-key) 'yas/expand)))
;;; eval'ed on require/load
(yas/init-minor-keymap)
;;;###autoload
(define-minor-mode yas/minor-mode
"Toggle YASnippet mode.
@ -724,13 +730,7 @@ Key bindings:
" yas"
:group 'yasnippet
(when yas/minor-mode
;; when turning on the minor mode.
;;
;; 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))
(yas/trigger-key-reload)
;; load all snippets definitions unless we still don't have a
;; root-directory or some snippets have already been loaded.
(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)))))
(defvar snippet-mode-map (make-sparse-keymap)
"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
(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 snippet-mode-map (third ent) (second 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"))))))
(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")
(progn
(yas/init-major-keymap))
(define-derived-mode snippet-mode text-mode "Snippet"
"A mode for editing yasnippets"
@ -1429,17 +1427,11 @@ content of the file is the template."
(setq yas/snippet-tables (make-hash-table))
(setq yas/menu-table (make-hash-table))
;; The minor mode and major mode keymap's cdr set to nil (this is
;; the same as `make-sparse-keymap;)
(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
;; Init the `yas/minor-mode-map', taking care not to break the
;; menu....
;;
(yas/init-minor-keymap)
(yas/init-major-keymap)
(setf (cdr yas/minor-mode-map)
(cdr (yas/init-minor-keymap)))
;; Now, clean up the other keymaps we might have cluttered up.
(yas/kill-snippet-keybindings)
@ -2429,7 +2421,7 @@ delegate to `yas/next-field'."
(yas/text (yas/field-text-for-display active-field))
(text yas/text)
(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))))
;; Now actually move...
(cond ((>= target-pos (length live-fields))
@ -2452,7 +2444,7 @@ Also create some protection overlays"
(setf (yas/snippet-active-field snippet) field)
(yas/place-overlays snippet 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)
(if (yas/field-update-display field snippet)
(let ((inhibit-modification-hooks t))
@ -2958,8 +2950,6 @@ Returns the newly created snippet."
;; Sort and link each field
(yas/snippet-sort-fields snippet)
;; (yas/update-mirrors snippet) ;; XXX: WHY was this here for so long...
;; Create keymap overlay for snippet
(setf (yas/snippet-control-overlay snippet)
(yas/make-control-overlay snippet (point-min) (point-max)))