* Fix issue 72 with snippet conditions

* Implemented abbreviated menu in `yas/use-menu'
This commit is contained in:
capitaomorte 2009-08-08 22:48:21 +00:00
parent d70529d121
commit a877bf5cc8

View File

@ -98,7 +98,7 @@
;; ;;
;; Lets you create a new snippet file in the correct ;; Lets you create a new snippet file in the correct
;; subdirectory of `yas/root-directory', according to the ;; subdirectory of `yas/root-directory', according to the
;; active major mode ;; active major mode.
;; ;;
;; M-x yas/load-snippet-buffer ;; M-x yas/load-snippet-buffer
;; ;;
@ -273,9 +273,11 @@ This affects `yas/insert-snippet', `yas/visit-snippet-file'"
(defcustom yas/use-menu t (defcustom yas/use-menu t
"Display a YASnippet menu in the menu bar. "Display a YASnippet menu in the menu bar.
If this is set to t, all snippet template of the current When non-nil, snippet templates will be listed under the menu
mode will be listed under the menu \"yasnippet\"." \"Yasnippet\". If set to `abbreviate', only the current major-mode
:type 'boolean menu and the modes set in `yas/mode-symbol' are listed."
:type '(choice (const :tag "Full" t)
(const :tag "Abbreviate" abbreviate))
:group 'yasnippet) :group 'yasnippet)
(defcustom yas/trigger-symbol " =>" (defcustom yas/trigger-symbol " =>"
@ -484,8 +486,6 @@ Here's an example:
(list "Reload-all-snippets" 'yas/reload-all) (list "Reload-all-snippets" 'yas/reload-all)
(list "Load snippets..." 'yas/load-directory)))))) (list "Load snippets..." 'yas/load-directory))))))
(yas/init-keymap-and-menu)
(define-minor-mode yas/minor-mode (define-minor-mode yas/minor-mode
"Toggle YASnippet mode. "Toggle YASnippet mode.
@ -503,7 +503,15 @@ Key bindings:
nil nil
;; The indicator for the mode line. ;; The indicator for the mode line.
" yas" " yas"
:group 'yasnippet) :group 'yasnippet
(when yas/minor-mode
;; when turning on theminor mode, re-read the `yas/trigger-key'
;; if a `yas/minor-mode-map' is already built. Else, call
;; `yas/init-keymap-and-menu' to build it
(if (and (cdr yas/minor-mode-map)
yas/trigger-key)
(define-key yas/minor-mode-map (read-kbd-macro yas/trigger-key) 'yas/expand)
(yas/init-keymap-and-menu))))
(defun yas/minor-mode-on () (defun yas/minor-mode-on ()
"Turn on YASnippet minor mode." "Turn on YASnippet minor mode."
@ -1175,7 +1183,8 @@ its parent modes."
(when (and yas/use-menu (when (and yas/use-menu
(yas/real-mode? mode)) (yas/real-mode? mode))
(define-key yas/minor-mode-menu (vector mode) (define-key yas/minor-mode-menu (vector mode)
`(menu-item ,(symbol-name mode) ,keymap))) `(menu-item ,(symbol-name mode) ,keymap
:visible (yas/show-menu-p ',mode))))
(dolist (snippet snippets) (dolist (snippet snippets)
(let* ((full-key (car snippet)) (let* ((full-key (car snippet))
(key (file-name-sans-extension full-key)) (key (file-name-sans-extension full-key))
@ -1216,6 +1225,14 @@ its parent modes."
,(yas/make-menu-binding template) ,(yas/make-menu-binding template)
:keys ,(concat key yas/trigger-symbol))))))))) :keys ,(concat key yas/trigger-symbol)))))))))
(defun yas/show-menu-p (mode)
(message "what")
(or (not (eq yas/use-menu 'abbreviate))
(find mode (cons major-mode
(if (listp yas/mode-symbol)
yas/mode-symbol
(list yas/mode-symbol))))))
(defun yas/delete-from-keymap (keymap name) (defun yas/delete-from-keymap (keymap name)
"Recursively delete items name NAME from KEYMAP and its submenus. "Recursively delete items name NAME from KEYMAP and its submenus.
@ -1275,15 +1292,24 @@ conditions to filter out potential expansions."
'always 'always
(let ((local-condition (yas/template-condition-predicate (let ((local-condition (yas/template-condition-predicate
yas/buffer-local-condition))) yas/buffer-local-condition)))
(and local-condition (when local-condition
(consp local-condition) (if (eq local-condition t)
t
(and (consp local-condition)
(eq 'require-snippet-condition (car local-condition)) (eq 'require-snippet-condition (car local-condition))
(symbolp (cdr local-condition)) (symbolp (cdr local-condition))
(cdr local-condition))))) (cdr local-condition)))))))
(defun yas/expand (&optional field) (defun yas/expand ()
"Expand a snippet." "Expand a snippet before point.
If no snippet expansion is possible, fall back to the behaviour
defined in `yas/fallback-behavior'"
(interactive) (interactive)
(yas/expand-1))
(defun yas/expand-1 (&optional field)
"Actually fo the work for `yas/expand'"
(multiple-value-bind (templates start end) (if field (multiple-value-bind (templates start end) (if field
(save-restriction (save-restriction
(narrow-to-region (yas/field-start field) (yas/field-end field)) (narrow-to-region (yas/field-start field) (yas/field-end field))
@ -1497,7 +1523,7 @@ otherwise, proposes to create the first option returned by
With optional prefix argument KILL quit the window and buffer." With optional prefix argument KILL quit the window and buffer."
(interactive "P") (interactive "P")
(if buffer-file-name (if buffer-file-name
(let ((major-mode-and-parent (yas/compute-major-mode-and-parent buffer-file-name))) (let ((major-mode-and-parent (yas/compute-major-mode-and-parents buffer-file-name)))
(if major-mode-and-parent (if major-mode-and-parent
(let* ((parsed (yas/parse-template buffer-file-name)) (let* ((parsed (yas/parse-template buffer-file-name))
(name (and parsed (name (and parsed
@ -1518,7 +1544,7 @@ With optional prefix argument KILL quit the window and buffer."
(defun yas/tryout-snippet (&optional debug) (defun yas/tryout-snippet (&optional debug)
"Test current buffers's snippet template in other buffer." "Test current buffers's snippet template in other buffer."
(interactive "P") (interactive "P")
(let* ((major-mode-and-parent (yas/compute-major-mode-and-parent buffer-file-name)) (let* ((major-mode-and-parent (yas/compute-major-mode-and-parents buffer-file-name))
(parsed (and major-mode-and-parent (parsed (and major-mode-and-parent
(fboundp (car major-mode-and-parent)) (fboundp (car major-mode-and-parent))
(yas/parse-template (symbol-name (car major-mode-and-parent))))) (yas/parse-template (symbol-name (car major-mode-and-parent)))))
@ -1739,7 +1765,7 @@ delegate to `yas/next-field'."
(let ((yas/fallback-behavior 'return-nil) (let ((yas/fallback-behavior 'return-nil)
(active-field (overlay-get yas/active-field-overlay 'yas/field))) (active-field (overlay-get yas/active-field-overlay 'yas/field)))
(when active-field (when active-field
(unless (yas/expand active-field) (unless (yas/expand-1 active-field)
(yas/next-field)))) (yas/next-field))))
(yas/next-field))) (yas/next-field)))