From f8c80945f88c9d00f6fbbf44f08561a1285f0f35 Mon Sep 17 00:00:00 2001 From: Zhang Chiyuan Date: Sun, 16 Mar 2008 02:26:08 +0000 Subject: [PATCH] Use minor-mode instead of global-set-key. Thanks Trey Jackson. --- yasnippet.el | 56 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/yasnippet.el b/yasnippet.el index 67ed7fe..83aa448 100644 --- a/yasnippet.el +++ b/yasnippet.el @@ -57,15 +57,11 @@ will first try \"bar\", if not found, then \"foo-bar\" is tried.") current column if this variable is non-`nil'.") (make-variable-buffer-local 'yas/indent-line) -(defvar yas/trigger-key (kbd "") +(defvar yas/trigger-key (kbd "TAB") "The key to bind as a trigger of snippet.") -(defvar yas/trigger-fallback 'yas/default-trigger-fallback - "The fallback command to call when there's no snippet to expand.") -(make-variable-buffer-local 'yas/trigger-fallback) (defvar yas/keymap (make-sparse-keymap) "The keymap of snippet.") -(define-key yas/keymap (kbd "") 'yas/next-field-group) (define-key yas/keymap (kbd "TAB") 'yas/next-field-group) (define-key yas/keymap (kbd "S-TAB") 'yas/prev-field-group) (define-key yas/keymap (kbd "") 'yas/prev-field-group) @@ -154,6 +150,35 @@ a window system.") (list 'yas/overlay-maybe-insert-behind-hook) "The list of hooks of the big keymap overlay modification event.") +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; YASnippet minor mode +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +(define-minor-mode yas/minor-mode + "Toggle YASnippet mode. +With no argument, this command toggles the mode. +positive prefix argument turns on the mode. +Negative prefix argument turns off the mode. + +When YASnippet mode is enabled, the TAB key +expands snippets of code depending on the mode. + +You can customize the key through `yas/trigger-key'." + ;; The initial value. + nil + ;; The indicator for the mode line. + " yas" + ;; The minor mode bindings. + `((,yas/trigger-key . yas/expand)) + :group 'editing) +(defun yas/minor-mode-on () + "Turn on YASnippet minor mode." + (interactive) + (yas/minor-mode 1)) +(defun yas/minor-mode-off () + "Turn off YASnippet minor mode." + (interactive) + (yas/minor-mode -1)) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Internal Structs @@ -823,18 +848,6 @@ is the output file of the compile result. Here's an example: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; User level functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -(defun yas/default-trigger-fallback () - "Default fallback when a snippet expansion failed. -It looks key binding for TAB. If found, execute it. If not found. -Run `indent-for-tab-command'." - (interactive) - (let ((command (key-binding (kbd "TAB")))) - (if (and command - (not (eq command 'yas/expand)) - (not (eq command 'yas/next-field-group))) - (call-interactively command) - (call-interactively 'indent-for-tab-command)))) - (defun yas/about () (interactive) (message (concat "yasnippet (version " @@ -864,7 +877,8 @@ content of the file is the template." (defun yas/initialize () "Do necessary initialization." - (global-set-key yas/trigger-key 'yas/expand) + (add-hook 'after-change-major-mode-hook + 'yas/minor-mode-on) (when yas/use-menu (define-key-after (lookup-key global-map [menu-bar]) @@ -945,8 +959,10 @@ the menu if `yas/use-menu' is `t'." (yas/popup-for-template templates)))) (when template (yas/expand-snippet start end template))) - (when yas/trigger-fallback - (call-interactively yas/trigger-fallback)))))) + (let* ((yas/minor-mode nil) + (command (key-binding yas/trigger-key))) + (when (commandp command) + (call-interactively command))))))) (defun yas/next-field-group () "Navigate to next field group. If there's none, exit the snippet."