* 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,15 +566,13 @@ 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 ()
(let ((map (make-sparse-keymap)))
(easy-menu-define yas/minor-mode-menu (easy-menu-define yas/minor-mode-menu
yas/minor-mode-map map
"Menu used when YAS/minor-mode is active." "Menu used when YAS/minor-mode is active."
'("YASnippet" '("YASnippet"
"----" "----"
@ -681,11 +687,14 @@ Here's an example:
:help "Display some information about YASsnippet"])) :help "Display some information about YASsnippet"]))
;; Now for the stuff that has direct keybindings ;; Now for the stuff that has direct keybindings
;; ;;
(yas/trigger-key-reload) (define-key map "\C-c&\C-s" 'yas/insert-snippet)
(define-key yas/minor-mode-map "\C-c&\C-s" 'yas/insert-snippet) (define-key map "\C-c&\C-n" 'yas/new-snippet)
(define-key yas/minor-mode-map "\C-c&\C-n" 'yas/new-snippet) (define-key map "\C-c&\C-v" 'yas/visit-snippet-file)
(define-key yas/minor-mode-map "\C-c&\C-v" 'yas/visit-snippet-file) (define-key map "\C-c&\C-f" 'yas/find-snippets)
(define-key yas/minor-mode-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) (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)
"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 () (defun yas/init-major-keymap ()
(easy-menu-define yas/major-mode-menu (let ((map (make-sparse-keymap)))
snippet-mode-map (easy-menu-define nil
map
"Menu used when snippet-mode is active." "Menu used when snippet-mode is active."
(cons "Snippet" (cons "Snippet"
(mapcar #'(lambda (ent) (mapcar #'(lambda (ent)
(when (third 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)) (vector (first ent) (second ent) t))
(list (list
(list "Load this snippet" 'yas/load-snippet-buffer "\C-c\C-c") (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" (define-derived-mode snippet-mode text-mode "Snippet"
"A mode for editing yasnippets" "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/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)
@ -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))
@ -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)))