Merge pull request #424 from ljos/de/activate-extra-mode

Feature: de/activate-extra-mode
This commit is contained in:
João Távora 2013-10-31 06:28:26 -07:00
commit 0f67c07ddd
2 changed files with 69 additions and 14 deletions

View File

@ -601,6 +601,31 @@ TODO: be meaner"
(should (eq (key-binding [(tab)]) 'yas-expand)) (should (eq (key-binding [(tab)]) 'yas-expand))
(should (eq (key-binding (kbd "TAB")) 'yas-expand)))) (should (eq (key-binding (kbd "TAB")) 'yas-expand))))
(ert-deftest test-yas-activate-extra-modes ()
"Given a symbol, `yas-activate-extra-mode' should be able to
add the snippets associated with the given mode."
(with-temp-buffer
(emacs-lisp-mode)
(yas-minor-mode-on)
(yas-activate-extra-mode 'markdown-mode)
(should (eq 'markdown-mode (car yas--extra-modes)))
(yas-should-expand '(("_" . "_Text_ ")))
(yas-should-expand '(("car" . "(car )")))))
(ert-deftest test-yas-deactivate-extra-modes ()
"Given a symbol, `yas-deactive-extra-mode' should be able to
remove one of the extra modes that is present in the current
buffer."
(with-temp-buffer
(emacs-lisp-mode)
(yas-minor-mode-on)
(yas-activate-extra-mode 'markdown-mode)
(should (eq 'markdown-mode (car yas--extra-modes)))
(yas-deactivate-extra-mode 'markdown-mode)
(should-not (eq 'markdown-mode (car yas--extra-modes)))
(yas-should-not-expand '("_"))
(yas-should-expand '(("car" . "(car )")))))
;;; Helpers ;;; Helpers
;;; ;;;

View File

@ -3,7 +3,7 @@
;; Copyright (C) 2008-2013 Free Software Foundation, Inc. ;; Copyright (C) 2008-2013 Free Software Foundation, Inc.
;; Authors: pluskid <pluskid@gmail.com>, João Távora <joaotavora@gmail.com> ;; Authors: pluskid <pluskid@gmail.com>, João Távora <joaotavora@gmail.com>
;; Maintainer: João Távora <joaotavora@gmail.com> ;; Maintainer: João Távora <joaotavora@gmail.com>
;; Version: 0.8.0 ;; Version: 0.8.1
;; Package-version: 0.8.0 ;; Package-version: 0.8.0
;; X-URL: http://github.com/capitaomorte/yasnippet ;; X-URL: http://github.com/capitaomorte/yasnippet
;; Keywords: convenience, emulation ;; Keywords: convenience, emulation
@ -48,13 +48,6 @@
;; The deprecated `yas/root-directory' aliases this variable ;; The deprecated `yas/root-directory' aliases this variable
;; for backward-compatibility. ;; for backward-compatibility.
;; ;;
;; `yas-extra-modes'
;;
;; A local variable that you can set in a hook to override
;; snippet-lookup based on major mode. It is a list of
;; symbols that correspond to subdirectories of
;; `yas-snippet-dirs' and is used for deciding which
;; snippets to consider for the active buffer.
;; ;;
;; Major commands are: ;; Major commands are:
;; ;;
@ -68,6 +61,11 @@
;; ;;
;; Prompts you for a directory hierarchy of snippets to load. ;; Prompts you for a directory hierarchy of snippets to load.
;; ;;
;; M-x yas-activate-extra-mode
;;
;; Prompts you for an extra mode to add snippets for in the
;; current buffer.
;;
;; M-x yas-insert-snippet ;; M-x yas-insert-snippet
;; ;;
;; Prompts you for possible snippet expansion if that is ;; Prompts you for possible snippet expansion if that is
@ -309,7 +307,7 @@ When non-nil, submenus for each snippet table will be listed
under the menu \"Yasnippet\". under the menu \"Yasnippet\".
- If set to `abbreviate', only the current major-mode - If set to `abbreviate', only the current major-mode
menu and the modes set in `yas-extra-modes' are listed. menu and the modes set in `yas--extra-modes' are listed.
- If set to `full', every submenu is listed - If set to `full', every submenu is listed
@ -657,11 +655,12 @@ snippet itself contains a condition that returns the symbol
(defvar yas-minor-mode-map (yas--init-minor-keymap) (defvar yas-minor-mode-map (yas--init-minor-keymap)
"The keymap used when `yas-minor-mode' is active.") "The keymap used when `yas-minor-mode' is active.")
(defvar yas-extra-modes nil (defvar yas--extra-modes nil
"A list of modes for which to also lookup snippets. "An internal list of modes for which to also lookup snippets.
This variable probably makes more sense as buffer-local, so This variable probably makes more sense as buffer-local, so
ensure your use `make-local-variable' when you set it.") ensure your use `make-local-variable' when you set it.")
(define-obsolete-variable-alias 'yas-extra-modes 'yas--extra-modes "0.8.1")
(defvar yas--tables (make-hash-table) (defvar yas--tables (make-hash-table)
"A hash table of mode symbols to `yas--table' objects.") "A hash table of mode symbols to `yas--table' objects.")
@ -713,7 +712,7 @@ and friends."
unless (memq neighbour explored) unless (memq neighbour explored)
append (funcall dfs neighbour explored))))) append (funcall dfs neighbour explored)))))
(remove-duplicates (append yas-extra-modes (remove-duplicates (append yas--extra-modes
(funcall dfs major-mode))))) (funcall dfs major-mode)))))
(defvar yas-minor-mode-hook nil (defvar yas-minor-mode-hook nil
@ -762,6 +761,37 @@ Key bindings:
(remove-hook 'post-command-hook 'yas--post-command-handler t) (remove-hook 'post-command-hook 'yas--post-command-handler t)
(remove-hook 'emulation-mode-map-alists 'yas--direct-keymaps)))) (remove-hook 'emulation-mode-map-alists 'yas--direct-keymaps))))
(defun yas-activate-extra-mode (mode)
"Activates the snippets for the given `mode' in the buffer.
The function can be called in the hook of a minor mode to
activate snippets associated with that mode."
(interactive
(let (modes
symbol)
(maphash (lambda (k _)
(setq modes (cons (list k) modes)))
yas--parents)
(setq symbol (completing-read
"Activate mode: " modes nil t))
(list
(when (not (string= "" symbol))
(intern symbol)))))
(when mode
(make-variable-buffer-local 'yas--extra-modes)
(add-to-list 'yas--extra-modes mode)
(yas--load-pending-jits)))
(defun yas-deactivate-extra-mode (mode)
"Deactivates the snippets for the given `mode' in the buffer."
(interactive
(list (intern
(completing-read
"Deactivate mode: " (mapcar #'list yas--extra-modes) nil t))))
(setq yas--extra-modes
(remove mode
yas--extra-modes)))
(defvar yas-dont-activate '(minibufferp) (defvar yas-dont-activate '(minibufferp)
"If non-nil don't let `yas-global-mode' affect some buffers. "If non-nil don't let `yas-global-mode' affect some buffers.
@ -889,8 +919,8 @@ Has the following fields:
`yas--table-name' `yas--table-name'
A symbol name normally corresponding to a major mode, but can A symbol name normally corresponding to a major mode, but can
also be a pseudo major-mode to be referenced in also be a pseudo major-mode to be used in
`yas-extra-modes', for example. `yas-activate-extra-mode', for example.
`yas--table-hash' `yas--table-hash'