]> code.delx.au - gnu-emacs-elpa/blobdiff - yasnippet.el
Fixed Issue 136.
[gnu-emacs-elpa] / yasnippet.el
index 5334a3085865cf2552c52537b04c42b14649eb1a..1dbf8c67283a412ea847f3d44323689656e20a15 100644 (file)
@@ -35,8 +35,8 @@
 ;;        (require 'yasnippet)
 ;;   2. Place the `snippets' directory somewhere.  E.g: ~/.emacs.d/snippets
 ;;   3. In your .emacs file
-;;        (setq yas/root-directory "~/.emacs/snippets")
-;;        (yas/load-directory yas/root-directory)
+;;        (setq yas/snippet-dirs "~/.emacs/snippets")
+;;        (yas/load-directory yas/snippet-dirs)
 ;;   4. To enable the YASnippet menu and tab-trigger expansion
 ;;        M-x yas/minor-mode
 ;;   5. To globally enable the minor mode in *all* buffers
 ;;
 ;;   Interesting variables are:
 ;;
-;;       `yas/root-directory'
+;;       `yas/snippet-dirs'
 ;;
 ;;           The directory where user-created snippets are to be
-;;           stored. Can also be a list of directories that
-;;           `yas/reload-all' will use for bulk-reloading snippets. In
-;;           that case the first directory the default for storing new
-;;           snippets.
+;;           stored. Can also be a list of directories. In that case,
+;;           when used for bulk (re)loading of snippets (at startup or
+;;           via `yas/reload-all'), directories appearing earlier in
+;;           the list shadow other dir's snippets. Also, the first
+;;           directory is taken as the default for storing the user's
+;;           new snippets.
 ;;
-;;       `yas/mode-symbol'
+;;           The deprecated `yas/root-directory' aliases this variable
+;;           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 a symbol (or
 ;;           list of symbols) that correspond to subdirectories of
-;;           `yas/root-directory' and is used for deciding which
+;;           `yas/snippet-dirs' and is used for deciding which
 ;;           snippets to consider for the active buffer.
 ;;
+;;           Deprecated `yas/mode-symbol' aliases this variable for
+;;           backward-compatibility.
+;;
 ;;   Major commands are:
 ;;
 ;;       M-x yas/expand
@@ -84,7 +92,7 @@
 ;;       M-x yas/find-snippets
 ;;
 ;;           Lets you find the snippet files in the correct
-;;           subdirectory of `yas/root-directory', according to the
+;;           subdirectory of `yas/snippet-dirs', according to the
 ;;           active major mode (if it exists) like
 ;;           `find-file-other-window'.
 ;;
 ;;       M-x yas/new-snippet
 ;;
 ;;           Lets you create a new snippet file in the correct
-;;           subdirectory of `yas/root-directory', according to the
+;;           subdirectory of `yas/snippet-dirs', according to the
 ;;           active major mode.
 ;;
 ;;       M-x yas/load-snippet-buffer
 ;;           snippet there, so you can see what it looks like.  This is
 ;;           bound to "C-c C-t" while in `snippet-mode'.
 ;;
+;;       M-x yas/describe-tables
+;;
+;;           Lists known snippets in a separate buffer. User is
+;;           prompted as to whether only the currently active tables
+;;           are to be displayed, or all the tables for all major
+;;           modes.
+;;
 ;;   The `dropdown-list.el' extension is bundled with YASnippet, you
 ;;   can optionally use it the preferred "prompting method", puting in
 ;;   your .emacs file, for example:
 ;;        M-x customize-group RET yasnippet RET
 ;;
 ;;   If you use the customization group to set variables
-;;   `yas/root-directory' or `yas/global-mode', make sure the path to
+;;   `yas/snippet-dirs' or `yas/global-mode', make sure the path to
 ;;   "yasnippet.el" is present in the `load-path' *before* the
 ;;   `custom-set-variables' is executed in your .emacs file.
 ;;
 (require 'cl)
 (require 'assoc)
 (require 'easymenu)
+(require 'help-mode)
 
 \f
 ;;; User customizable variables
 
-
 (defgroup yasnippet nil
   "Yet Another Snippet extension"
   :group 'editing)
 
 ;;;###autoload
-(defcustom yas/root-directory nil
-  "Root directory that stores the snippets for each major mode.
-
-If you set this from your .emacs, can also be a list of strings,
-for multiple root directories. If you make this a list, the first
-element is always the user-created snippets directory. Other
-directories are used for bulk reloading of all snippets using
-`yas/reload-all'"
+(defcustom yas/snippet-dirs nil
+  "Directory or list of snippet dirs for each major mode.
+
+The directory where user-created snippets are to be stored. Can
+also be a list of directories. In that case, when used for
+bulk (re)loading of snippets (at startup or via
+`yas/reload-all'), directories appearing earlier in the list
+shadow other dir's snippets. Also, the first directory is taken
+as the default for storing the user's new snippets."
   :type '(choice (string :tag "Single directory (string)")
                  (repeat :args (string) :tag "List of directories (strings)"))
   :group 'yasnippet
@@ -169,6 +185,9 @@ directories are used for bulk reloading of all snippets using
              (unless (or (not (fboundp 'yas/reload-all))
                          (equal old new))
                (yas/reload-all)))))
+(defun yas/snippet-dirs ()
+  (if (listp yas/snippet-dirs) yas/snippet-dirs (list yas/snippet-dirs)))
+(defvaralias 'yas/root-directory 'yas/snippet-dirs)
 
 (defcustom yas/prompt-functions '(yas/x-prompt
                                   yas/dropdown-prompt
@@ -245,7 +264,7 @@ representation using `read-kbd-macro'."
              ;; `yas/trigger-key' is *not* loaded.
              (if (fboundp 'yas/trigger-key-reload)
                  (yas/trigger-key-reload old)))))
-  
+
 (defcustom yas/next-field-key '("TAB" "<tab>")
   "The key to navigate to next field when a snippet is active.
 
@@ -260,7 +279,7 @@ Can also be a list of strings."
            (set-default symbol val)
            (if (fboundp 'yas/init-yas-in-snippet-keymap)
                (yas/init-yas-in-snippet-keymap))))
-           
+
 
 (defcustom yas/prev-field-key '("<backtab>" "<S-tab>")
   "The key to navigate to previous field when a snippet is active.
@@ -315,7 +334,6 @@ field"
   :type '(choice (const :tag "Call previous command"  call-other-command)
                  (const :tag "Do nothing"             return-nil))
   :group 'yasnippet)
-(make-variable-buffer-local 'yas/fallback-behavior)
 
 (defcustom yas/choose-keys-first nil
   "If non-nil, prompt for snippet key first, then for template.
@@ -346,7 +364,7 @@ under the menu \"Yasnippet\".
 corresponds to a major mode are listed.
 
 - If set to `abbreviate', only the current major-mode
-menu and the modes set in `yas/mode-symbol' are listed.
+menu and the modes set in `yas/extra-modes' are listed.
 
 Any other non-nil value, every submenu is listed."
   :type '(choice (const :tag "Full"  t)
@@ -390,8 +408,7 @@ This cafn only work when snippets are loaded from files."
   :group 'yasnippet)
 
 (defface yas/field-highlight-face
-  '((((class color) (background light)) (:background "DarkSeaGreen1"))
-    (t (:background "DimGrey")))
+  '((t (:inherit 'region)))
   "The face used to highlight the currently active field of a snippet"
   :group 'yasnippet)
 
@@ -420,7 +437,7 @@ This cafn only work when snippets are loaded from files."
     map)
   "The keymap active while a snippet expansion is in progress.")
 
-(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
 to try to find a key. For example, if the list is '(\"w\" \"w_\").
 And in emacs-lisp-mode, where \"-\" has the syntax of \"_\":
@@ -447,13 +464,8 @@ Attention: These hooks are not run when exiting nested/stackd snippet expansion!
   "Hooks to run just before expanding a snippet.")
 
 (defvar yas/buffer-local-condition
-  '(if (and (not (bobp))
-            (or (equal 'font-lock-comment-face
-                       (get-char-property (1- (point))
-                                          'face))
-                (equal 'font-lock-string-face
-                       (get-char-property (1- (point))
-                                          'face))))
+  '(if (or (fourth (syntax-ppss))
+           (fifth (syntax-ppss)))
        '(require-snippet-condition . force-in-comment)
      t)
   "Snippet expanding condition.
@@ -487,7 +499,7 @@ This variable is a lisp form:
     * 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. 
+         evaluate to non-nil, are considered for expansion.
 
        * Otherwise, the snippet is not considered.
 
@@ -507,7 +519,6 @@ 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)
 
 \f
 ;;; Internal variables
@@ -582,7 +593,7 @@ snippet itself contains a condition that returns the symbol
         ["Visit snippet file..." yas/visit-snippet-file
          :help "Prompt for an expandable snippet and find its file"]
         ["Find snippets..." yas/find-snippets
-         :help "Invoke `find-file' in the appropriate snippet directory"] 
+         :help "Invoke `find-file' in the appropriate snippet directory"]
         "----"
         ("Snippet menu behaviour"
          ["Visit snippets" (setq yas/visit-from-menu t)
@@ -649,27 +660,27 @@ snippet itself contains a condition that returns the symbol
                                                  'yas/completing-prompt-prompt)]
          )
         ("Misc"
-         ["Wrap region in exit marker" 
+         ["Wrap region in exit marker"
           (setq yas/wrap-around-region
                 (not yas/wrap-around-region))
           :help "If non-nil automatically wrap the selected text in the $0 snippet exit"
           :style toggle :selected yas/wrap-around-region]
-         ["Allow stacked expansions " 
+         ["Allow stacked expansions "
           (setq yas/triggers-in-field
                 (not yas/triggers-in-field))
           :help "If non-nil allow snippets to be triggered inside other snippet fields"
           :style toggle :selected yas/triggers-in-field]
-         ["Revive snippets on undo " 
+         ["Revive snippets on undo "
           (setq yas/snippet-revival
                 (not yas/snippet-revival))
           :help "If non-nil allow snippets to become active again after undo"
           :style toggle :selected yas/snippet-revival]
-         ["Good grace " 
+         ["Good grace "
           (setq yas/good-grace
                 (not yas/good-grace))
           :help "If non-nil don't raise errors in bad embedded eslip in snippets"
           :style toggle :selected yas/good-grace]
-         ["Ignore filenames as triggers" 
+         ["Ignore filenames as triggers"
           (setq yas/ignore-filenames-as-triggers
                 (not yas/ignore-filenames-as-triggers))
           :help "If non-nil don't derive tab triggers from filenames"
@@ -701,16 +712,16 @@ With optional UNBIND-KEY, try to unbind that key from
   (when (and unbind-key
              (stringp unbind-key)
              (not (string= unbind-key "")))
-    (define-key yas/minor-mode-map (read-kbd-macro unbind-key) nil)) 
+    (define-key yas/minor-mode-map (read-kbd-macro unbind-key) nil))
   (when  (and yas/trigger-key
               (stringp yas/trigger-key)
               (not (string= yas/trigger-key "")))
     (define-key yas/minor-mode-map (read-kbd-macro yas/trigger-key) 'yas/expand)))
 
-(defvar yas/snippet-tables (make-hash-table)
-  "A hash table of MAJOR-MODE symbols to `yas/snippet-table' objects.")
+(defvar yas/tables (make-hash-table)
+  "A hash table of MAJOR-MODE symbols to `yas/table' objects.")
 
-(defvar yas/snippet-keymaps nil
+(defvar yas/direct-keymaps (list)
   "Keymap alist supporting direct snippet keybindings.
 
 This variable is is placed `emulation-mode-map-alists'.
@@ -721,16 +732,32 @@ set buffer-locally when entering `yas/minor-mode'. KEYMAP binds
 all defined direct keybindings to the command
 `yas/expand-from-keymap', which acts similarly to `yas/expand'")
 
-(defun yas/snippet-keymaps-reload ()
+(defun yas/direct-keymaps-reload ()
+  "Force reload the direct keybinding for active snippet tables."
   (interactive)
-  (setq yas/snippet-keymaps nil) 
+  (setq yas/direct-keymaps nil)
   (maphash #'(lambda (name table)
                (mapc #'(lambda (table)
-                         (push (cons name
-                                     (yas/snippet-table-keymap table))
-                               yas/snippet-keymaps))
-                     (cons table (yas/snippet-table-get-all-parents table))))
-           yas/snippet-tables))
+                         (push (cons (intern (format "yas//direct-%s" name))
+                                     (yas/table-direct-keymap table))
+                               yas/direct-keymaps))
+                     (cons table (yas/table-get-all-parents table))))
+           yas/tables))
+
+(defun yas/direct-keymaps-set-vars ()
+  (let ((modes-to-activate (list major-mode))
+        (mode major-mode))
+    (while (setq mode (get mode 'derived-mode-parent))
+      (push mode modes-to-activate))
+    (dolist (mode (yas/extra-modes))
+      (push mode modes-to-activate))
+    (dolist (mode modes-to-activate)
+      (let ((name (intern (format "yas//direct-%s" mode))))
+        (set-default name nil)
+        (set (make-local-variable name) t)))))
+
+(defvar yas/minor-mode-hook nil
+  "Hook run when yas/minor-mode is turned on")
 
 ;;;###autoload
 (define-minor-mode yas/minor-mode
@@ -757,36 +784,28 @@ Key bindings:
          (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)
-                     (> (hash-table-count yas/snippet-tables) 0))
+         ;;
+         (unless (or (null yas/snippet-dirs)
+                     (> (hash-table-count yas/tables) 0))
            (yas/reload-all))
          ;; Install the direct keymaps in `emulation-mode-map-alists'
          ;; (we use `add-hook' even though it's not technically a hook,
          ;; but it works). Then define variables named after modes to
-         ;; index `yas/snippet-keymaps'.
+         ;; index `yas/direct-keymaps'.
          ;;
-         ;; FIXME: this is quite wrong and breaks cua-mode for
-         ;; example. It is either `yas/snippet-keymaps' that needs to
-         ;; have a buffer-local value, or those little indicator vars
-         ;; need to be set and unset buffer-locally (preferred).
-         ;; 
-         (add-hook 'emulation-mode-map-alists 'yas/snippet-keymaps nil 'local)
-         (let ((modes-to-activate (list major-mode))
-               (mode major-mode))
-           (while (setq mode (get mode 'derived-mode-parent))
-             (push mode modes-to-activate))
-           (dolist (mode modes-to-activate)
-             (unless (and (boundp mode)
-                          (symbol-value mode))
-               (set (make-local-variable mode) t)))))
+         (add-hook 'emulation-mode-map-alists 'yas/direct-keymaps)
+         (add-hook 'yas/minor-mode-hook 'yas/direct-keymaps-set-vars-runonce 'append))
         (t
          ;; Uninstall the direct keymaps.
-         ;; 
-         (remove-hook 'emulation-mode-map-alists 'yas/snippet-keymaps 'local))))
+         ;;
+         (remove-hook 'emulation-mode-map-alists 'yas/direct-keymaps))))
+
+(defun yas/direct-keymaps-set-vars-runonce ()
+  (yas/direct-keymaps-set-vars)
+  (remove-hook 'yas/minor-mode-hook 'yas/direct-keymaps-set-vars-runonce))
 
 (defvar yas/dont-activate #'(lambda ()
-                              (and yas/root-directory
+                              (and yas/snippet-dirs
                                    (null (yas/get-snippet-tables))))
   "If non-nil don't let `yas/minor-mode-on' active yas for this buffer.
 
@@ -867,131 +886,191 @@ Do this unless `yas/dont-activate' is t or the function
 \f
 ;;; Internal structs for template management
 
-(defstruct (yas/template (:constructor yas/make-template
-                                       (content name condition expand-env file keybinding)))
+(defstruct (yas/template (:constructor yas/make-blank-template))
   "A template for a snippet."
+  table
+  key
   content
   name
   condition
   expand-env
   file
-  keybinding)
-
-(defstruct (yas/snippet-table (:constructor yas/make-snippet-table (name)))
+  keybinding
+  uuid
+  menu-binding-pair
+  group      ;; as dictated by the #group: directive or .yas-make-groups 
+  perm-group ;; as dictated by `yas/define-menu'
+  )
+
+(defun yas/populate-template (template &rest args)
+  "Helper function to populate a template with properties"
+  (let (p v)
+    (while args
+      (aset template
+            (position (intern (substring (symbol-name (car args)) 1))
+                      (mapcar #'car (get 'yas/template 'cl-struct-slots)))
+            (second args))
+      (setq args (cddr args)))
+    template))
+
+(defstruct (yas/table (:constructor yas/make-snippet-table (name)))
   "A table to store snippets for a particular mode.
 
 Has the following fields:
 
-`yas/snippet-table-name'
+`yas/table-name'
 
-  A symbol normally corresponding to a major mode, but can also be
-  a pseudo major-mode to be referenced in `yas/mode-symbol', for
-  example.
+  A symbol name normally corresponding to a major mode, but can
+  also be a pseudo major-mode to be referenced in
+  `yas/extra-modes', for example.
 
-`yas/snippet-table-hash'
+`yas/table-hash'
 
-  A hash table, known as the \"keyhash\" where key is a string or
-  a vector. In case of a string its the snippet trigger key,
-  whereas a vector means its a direct keybinding. The value is
-  yet another hash of (NAME . TEMPLATE), known as the
-  \"namehash\", where NAME is the snippet name and TEMPLATE is a
-  `yas/template' object.
+  A hash table (KEY . NAMEHASH), known as the \"keyhash\". KEY is
+  a string or a vector, where the former is the snippet's trigger
+  and the latter means it's a direct keybinding. NAMEHASH is yet
+  another hash of (NAME . TEMPLATE) where NAME is the snippet's
+  name and TEMPLATE is a `yas/template' object.
 
-`yas/snippet-table-parents'
+`yas/table-parents'
 
   A list of tables considered parents of this table: i.e. when
   searching for expansions they are searched as well.
 
-`yas/snippet-keymap'
+`yas/table-direct-keymap'
 
   A keymap for the snippets in this table that have direct
   keybindings. This is kept in sync with the keyhash, i.e., all
   the elements of the keyhash that are vectors appear here as
   bindings to `yas/expand-from-keymap'.
+
+`yas/table-uuidhash'
+
+  A hash table mapping snippets uuid's to the same `yas/template'
+  objects. A snippet uuid defaults to the snippet's name.
 "
   name
   (hash (make-hash-table :test 'equal))
+  (uuidhash (make-hash-table :test 'equal))
   (parents nil)
-  (keymap (make-sparse-keymap)))
+  (direct-keymap (make-sparse-keymap)))
 
-;; Apropos storing/updating, this is works with two steps:
-;;
-;; 1. `yas/remove-snippet' to remove any existing mappings, with two
-;;    searches:
-;;
-;;    a) Try to get the existing namehash from TABLE using key.
-;;    
-;;    b) When the user changed KEY, the previous key indexing the
-;;      namehash is lost, so try to get the existing namehash by
-;;      searching the *whole* snippet table for NAME *and* checking
-;;      that the key for that previous namehash is of the same type
-;;      as KEY. This latter detail enables independent changes in
-;;      the trigger key and direct keybinding for a snippet.
+(defun yas/get-template-by-uuid (mode uuid)
+  "Find the snippet template in MODE by its UUID."
+  (let* ((table (gethash mode yas/tables mode)))
+    (when table
+      (gethash uuid (yas/table-uuidhash table)))))
+
+;; Apropos storing/updating, this works with two steps:
 ;;
-;;    Search b) is only performed if
-;;    `yas/better-guess-for-replacements' is non-nil, which happens
-;;    when the user is interactively loading the snippet buffer.
-;;    
-;;    If any existing namesomething is found it is deleted, and is
-;;    maybe added later on:
+;; 1. `yas/remove-template-by-uuid' to remove any existing mappings by
+;;    snippet uuid
 ;;
-;; 2. `yas/add-snippet' to add the mappings again:
+;; 2. `yas/add-template' to add the mappings again:
 ;;
-;;    Create or index the entry in TABLES's `yas/snippet-table-hash'
+;;    Create or index the entry in TABLES's `yas/table-hash'
 ;;    linking KEY to a namehash. That namehash links NAME to
 ;;    TEMPLATE, and is also created a new namehash inside that
 ;;    entry.
 ;;
-;; TODO: This is still not ideal. A well designed system (like
-;; TextMate's) indexes the snippets by UUID or filename or something
-;; that uniquely identify a snippet. I.e. this replacement strategy
-;; fails if both the key and the name have changed. In that case,
-;; it's as if a brand new snippet had been created.
-;;
-(defvar yas/better-guess-for-replacements nil
-  "If non-nil `yas/store' guesses snippet replacements \"better\".")
-
-(defun yas/remove-snippet (table name key template type-fn)
-  (let ((key-and-namehash-alist '())
-        (namehash-for-key (gethash key (yas/snippet-table-hash table))))
-    (when namehash-for-key 
-      (push (cons key namehash-for-key) key-and-namehash-alist))
-    (when yas/better-guess-for-replacements
-      ;; "cand" means "candidate for removal"
-      (maphash #'(lambda (cand namehash)
-                   (when (and (gethash name namehash)
-                              (funcall type-fn cand))
-                     (push (cons cand namehash) key-and-namehash-alist)))
-               (yas/snippet-table-hash table)))
-    (dolist (elem key-and-namehash-alist)
-      (remhash name (cdr elem))
-      (when (= 0 (hash-table-count (cdr elem)))
-        (remhash (car elem) (yas/snippet-table-hash table))
-        (when (vectorp (car elem))
-          (define-key (yas/snippet-table-keymap table) (car elem) nil))))))
-
-(defun yas/add-snippet (table name key template)
-  "Store in TABLE the snippet NAME indexed by KEY and expanding TEMPLATE.
+(defun yas/remove-template-by-uuid (table uuid)
+  "Remove from TABLE a template identified by UUID."
+  (let ((template (gethash uuid (yas/table-uuidhash table))))
+    (when template
+      (let* ((name                (yas/template-name template))
+             (empty-keys          nil))
+        ;; Remove the name from each of the targeted namehashes
+        ;;
+        (maphash #'(lambda (k v)
+                     (let ((template (gethash name v)))
+                       (when (and template
+                                  (eq uuid (yas/template-uuid template)))
+                         (remhash name v)
+                         (when (zerop (hash-table-count v))
+                           (push k empty-keys)))))
+                 (yas/table-hash table))
+        ;; Remove the namehashed themselves if they've become empty
+        ;;
+        (dolist (key empty-keys)
+          (remhash key (yas/table-hash table)))
+
+        ;; Finally, remove the uuid from the uuidhash
+        ;;
+        (remhash uuid (yas/table-uuidhash table))))))
+
+
+(defun yas/add-template (table template)
+  "Store in TABLE the snippet template TEMPLATE.
 
 KEY can be a string (trigger key) of a vector (direct
 keybinding)."
-  
-  ;; Now store the new template independent of the previous steps.
+  (let ((name (yas/template-name template))
+        (key (yas/template-key template))
+        (keybinding (yas/template-keybinding template))
+        (menu-binding (car (yas/template-menu-binding-pair template))))
+    (dolist (k (remove nil (list key keybinding)))
+      (puthash name
+               template
+               (or (gethash k
+                            (yas/table-hash table))
+                   (puthash k
+                            (make-hash-table :test 'equal)
+                            (yas/table-hash table))))
+      (when (vectorp k)
+        (define-key (yas/table-direct-keymap table) k 'yas/expand-from-keymap)))
+
+    (when menu-binding
+      (setf (getf (cdr menu-binding) :keys)
+            (or (and keybinding (key-description keybinding))
+                (and key (concat key yas/trigger-symbol))))
+      (setcar (cdr menu-binding)
+              name))
+
+    (puthash (yas/template-uuid template) template (yas/table-uuidhash table))))
+
+(defun yas/update-template (snippet-table template)
+  "Add or update TEMPLATE in SNIPPET-TABLE.
+
+Also takes care of adding and updaring to the associated menu."
+  ;; Remove from table by uuid
+  ;;
+  (yas/remove-template-by-uuid snippet-table (yas/template-uuid template))
+  ;; Add to table again
+  ;;
+  (yas/add-template snippet-table template)
+  ;; Take care of the menu
   ;;
-  (when key
-    (puthash name
-             template
-             (or (gethash key
-                          (yas/snippet-table-hash table))
-                 (puthash key
-                          (make-hash-table :test 'equal)
-                          (yas/snippet-table-hash table))))
-    (when (vectorp key)
-      (define-key (yas/snippet-table-keymap table) key 'yas/expand-from-keymap)))) 
+  (let ((keymap (yas/menu-keymap-get-create snippet-table))
+        (group (yas/template-group template)))
+    (when (and yas/use-menu
+               keymap
+               (not (cdr (yas/template-menu-binding-pair template))))
+      ;; Remove from menu keymap
+      ;;
+      (yas/delete-from-keymap keymap (yas/template-uuid template))
+
+      ;; Add necessary subgroups as necessary.
+      ;; 
+      (dolist (subgroup group)
+        (let ((subgroup-keymap (lookup-key keymap (vector (make-symbol subgroup)))))
+          (unless (and subgroup-keymap
+                       (keymapp subgroup-keymap))
+            (setq subgroup-keymap (make-sparse-keymap))
+            (define-key keymap (vector (make-symbol subgroup))
+              `(menu-item ,subgroup ,subgroup-keymap)))
+            (setq keymap subgroup-keymap)))
+      
+      ;; Add this entry to the keymap
+      ;; 
+      (let ((menu-binding-pair (yas/snippet-menu-binding-pair-get-create template)))
+        (define-key keymap (vector (make-symbol (yas/template-uuid template))) (car menu-binding-pair))))))
 
 (defun yas/fetch (table key)
-  "Fetch snippets in TABLE by KEY. "
-  (let* ((keyhash (yas/snippet-table-hash table))
+  "Fetch templates in TABLE by KEY.
+
+Return a list of cons (NAME . TEMPLATE) where NAME is a
+string and TEMPLATE is a `yas/template' structure."
+  (let* ((keyhash (yas/table-hash table))
          (namehash (and keyhash (gethash key keyhash))))
     (when namehash
       (yas/filter-templates-by-condition
@@ -1001,7 +1080,6 @@ keybinding)."
                   namehash)
          alist)))))
 
-
 \f
 ;;; Filtering/condition logic
 
@@ -1029,7 +1107,8 @@ This function implements the rules described in
     (if (eq requirement 'always)
         templates
       (remove-if-not #'(lambda (pair)
-                         (yas/template-can-expand-p (yas/template-condition (cdr pair)) requirement))
+                         (yas/template-can-expand-p
+                          (yas/template-condition (cdr pair)) requirement))
                      templates))))
 
 (defun yas/require-template-specific-condition-p ()
@@ -1048,38 +1127,30 @@ conditions to filter out potential expansions."
                (symbolp (cdr local-condition))
                (cdr local-condition)))))))
 
-(defun yas/template-can-expand-p (condition &optional requirement)
+(defun yas/template-can-expand-p (condition requirement)
   "Evaluates CONDITION and REQUIREMENT and returns a boolean"
-  (let* ((requirement (or requirement
-                          (yas/require-template-specific-condition-p)))
-         (result (or (null condition)
-                     (yas/eval-condition
-                      (condition-case err
-                          (read condition)
-                        (error (progn
-                                 (message (format "[yas] error reading condition: %s"
-                                                  (error-message-string err))))
-                               nil))))))
+  (let* ((result (or (null condition)
+                     (yas/eval-condition condition))))
     (cond ((eq requirement t)
            result)
           (t
            (eq requirement result)))))
 
-(defun yas/snippet-table-get-all-parents (table)
-  "Returns a list of all parent tables of TABLE" 
-  (let ((parents (yas/snippet-table-parents table)))
+(defun yas/table-get-all-parents (table)
+  "Returns a list of all parent tables of TABLE"
+  (let ((parents (yas/table-parents table)))
     (when parents
       (append (copy-list parents)
-              (mapcan #'yas/snippet-table-get-all-parents parents)))))
+              (mapcan #'yas/table-get-all-parents parents)))))
 
-(defun yas/snippet-table-templates (table)
+(defun yas/table-templates (table)
   (when table
     (let ((acc (list)))
       (maphash #'(lambda (key namehash)
                    (maphash #'(lambda (name template)
                                 (push (cons name template) acc))
                             namehash))
-               (yas/snippet-table-hash table))
+               (yas/table-hash table))
       (yas/filter-templates-by-condition acc))))
 
 (defun yas/current-key ()
@@ -1109,13 +1180,13 @@ the template of a snippet in the current snippet-table."
           end)))
 
 
-(defun yas/snippet-table-all-keys (table)
+(defun yas/table-all-keys (table)
   (when table
     (let ((acc))
       (maphash #'(lambda (key templates)
                    (when (yas/filter-templates-by-condition templates)
                      (push key acc)))
-               (yas/snippet-table-hash table))
+               (yas/table-hash table))
       acc)))
 
 \f
@@ -1168,56 +1239,87 @@ return an expression that when evaluated will issue an error."
     (error (and (not nil-on-error)
                 `(error (error-message-string err))))))
 
-(defvar yas/mode-symbol nil
-  "If non-nil, lookup snippets using this instead of `major-mode'.")
-(make-variable-buffer-local 'yas/mode-symbol)
-
-(defun yas/snippet-table-get-create (mode)
+(defun yas/read-keybinding (keybinding)
+  "Read KEYBINDING as a snippet keybinding, return a vector."
+  (when (and keybinding
+             (not (string-match "keybinding" keybinding)))
+    (condition-case err
+        (let ((keybinding-string (or (and (string-match "\".*\"" keybinding)
+                                          (read keybinding))
+                                     ;; "KEY-DESC" with quotes is deprecated..., but supported
+                                     keybinding)))
+          (read-kbd-macro keybinding-string 'need-vector))
+      (error
+       (message "[yas] warning: keybinding \"%s\" invalid for snippet \"%s\" since %s."
+                keybinding name (error-message-string err))
+       nil))))
+
+(defvar yas/extra-modes nil
+  "If non-nil, also lookup snippets for this/these modes.
+
+Can be a symbol or a list of symbols.
+
+This variable probably makes more sense as buffer-local, so
+ensure your use `make-local-variable' when you set it.")
+(defun yas/extra-modes ()
+  (if (listp yas/extra-modes) yas/extra-modes (list yas/extra-modes)))
+(defvaralias 'yas/mode-symbol 'yas/extra-modes)
+
+(defun yas/table-get-create (mode)
   "Get the snippet table corresponding to MODE.
 
 Optional DIRECTORY gets recorded as the default directory to
 search for snippet files if the retrieved/created table didn't
 already have such a property."
   (let ((table (gethash mode
-                        yas/snippet-tables)))
+                        yas/tables)))
     (unless table
       (setq table (yas/make-snippet-table (symbol-name mode)))
-      (puthash mode table yas/snippet-tables))
+      (puthash mode table yas/tables)
+      (aput 'yas/direct-keymaps (intern (format "yas//direct-%s" mode))
+            (yas/table-direct-keymap table)))
     table))
 
 (defun yas/get-snippet-tables (&optional mode-symbol dont-search-parents)
   "Get snippet tables for current buffer.
 
-Return a list of 'yas/snippet-table' objects indexed by mode.
+Return a list of 'yas/table' objects indexed by mode.
 
 The modes are tried in this order: optional MODE-SYMBOL, then
-`yas/mode-symbol', then `major-mode' then, unless
+`yas/extra-modes', then `major-mode' then, unless
 DONT-SEARCH-PARENTS is non-nil, the guessed parent mode of either
 MODE-SYMBOL or `major-mode'.
 
 Guessing is done by looking up the MODE-SYMBOL's
 `derived-mode-parent' property, see also `derived-mode-p'."
   (let ((mode-tables
-         (mapcar #'(lambda (mode)
-                     (gethash mode yas/snippet-tables))
-                 (append (list mode-symbol)
-                         (if (listp yas/mode-symbol)
-                             yas/mode-symbol
-                           (list yas/mode-symbol))
-                         (list major-mode
-                               (and (not dont-search-parents)
-                                    (get (or mode-symbol major-mode)
-                                         'derived-mode-parent))))))
-        (all-tables))
-    (dolist (table (remove nil mode-tables))
-      (push table all-tables)
-      (nconc all-tables (yas/snippet-table-get-all-parents table)))
-    (remove-duplicates all-tables)))
-
-(defun yas/menu-keymap-get-create (mode)
-  "Get the menu keymap correspondong to MODE."
-  (or (gethash mode yas/menu-table)
-      (puthash mode (make-sparse-keymap) yas/menu-table)))
+         (remove nil
+                 (mapcar #'(lambda (mode)
+                             (gethash mode yas/tables))
+                 (remove nil (append (list mode-symbol)
+                                     (yas/extra-modes)
+                                     (list major-mode
+                                           (and (not dont-search-parents)
+                                                (get major-mode
+                                                     'derived-mode-parent)))))))))
+    (remove-duplicates 
+     (append mode-tables
+             (mapcan #'yas/table-get-all-parents mode-tables)))))
+
+(defun yas/menu-keymap-get-create (table)
+  "Get or create the main menu keymap correspondong to MODE.
+
+This may very well create a plethora of menu keymaps and arrange
+them in all `yas/menu-table'"
+  (let* ((mode (intern (yas/table-name table)))
+         (menu-keymap (or (gethash mode yas/menu-table)
+                          (puthash mode (make-sparse-keymap) yas/menu-table)))
+        (parents (yas/table-parents table)))
+    (mapc #'yas/menu-keymap-get-create parents)
+    (define-key yas/minor-mode-menu (vector mode)
+        `(menu-item ,(symbol-name mode) ,menu-keymap
+                    :visible (yas/show-menu-p ',mode)))
+    menu-keymap))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;; Template-related and snippet loading functions
@@ -1228,32 +1330,29 @@ Guessing is done by looking up the MODE-SYMBOL's
 Optional FILE is the absolute file name of the file being
 parsed.
 
+Optional GROUP is the group where the template is to go,
+otherwise we attempt to calculate it from FILE.
+
 Return a snippet-definition, i.e. a list
 
- (KEY TEMPLATE NAME CONDITION GROUP VARS FILE KEYBINDING)
+ (KEY TEMPLATE NAME CONDITION GROUP VARS FILE KEYBINDING UUID)
 
-If the buffer contains a line of \"# --\" then the contents
-above this line are ignored. Variables can be set above this
-line through the syntax:
+If the buffer contains a line of \"# --\" then the contents above
+this line are ignored. Directives can set most of these with the syntax:
 
-#name : value
+# directive-name : directive-value
 
-Here's a list of currently recognized variables:
+Here's a list of currently recognized directives:
 
  * type
  * name
  * contributor
  * condition
- * key
  * group
+ * key
  * expand-env
  * binding
-
-#name: #include \"...\"
-# --
-#include \"$1\""
-  ;;
-  ;;
+ * uuid"
   (goto-char (point-min))
   (let* ((type 'snippet)
          (name (and file
@@ -1267,7 +1366,8 @@ Here's a list of currently recognized variables:
          (group (and file
                      (yas/calculate-group file)))
          expand-env
-         binding)
+         binding
+         uuid)
     (if (re-search-forward "^# --\n" nil t)
         (progn (setq template
                      (buffer-substring-no-properties (point)
@@ -1275,6 +1375,8 @@ Here's a list of currently recognized variables:
                (setq bound (point))
                (goto-char (point-min))
                (while (re-search-forward "^# *\\([^ ]+?\\) *: *\\(.*\\)$" bound t)
+                 (when (string= "uuid" (match-string-no-properties 1))
+                   (setq uuid (match-string-no-properties 2)))
                  (when (string= "type" (match-string-no-properties 1))
                    (setq type (if (string= "command" (match-string-no-properties 2))
                                   'command
@@ -1284,7 +1386,7 @@ Here's a list of currently recognized variables:
                  (when (string= "name" (match-string-no-properties 1))
                    (setq name (match-string-no-properties 2)))
                  (when (string= "condition" (match-string-no-properties 1))
-                   (setq condition (match-string-no-properties 2)))
+                   (setq condition (yas/read-lisp (match-string-no-properties 2))))
                  (when (string= "group" (match-string-no-properties 1))
                    (setq group (match-string-no-properties 2)))
                  (when (string= "expand-env" (match-string-no-properties 1))
@@ -1296,7 +1398,9 @@ Here's a list of currently recognized variables:
             (buffer-substring-no-properties (point-min) (point-max))))
     (when (eq type 'command)
       (setq template (yas/read-lisp (concat "(progn" template ")"))))
-    (list key template name condition group expand-env file binding)))
+    (when group
+      (setq group (split-string group "\\.")))
+    (list key template name condition group expand-env file binding uuid)))
 
 (defun yas/calculate-group (file)
   "Calculate the group for snippet file path FILE."
@@ -1315,39 +1419,13 @@ Here's a list of currently recognized variables:
                                                (directory-file-name extra-dir)))))
     group))
 
-;; (defun yas/glob-files (directory &optional recurse-p append)
-;;   "Returns files under DIRECTORY ignoring dirs and hidden files.
-
-;; If RECURSE in non-nil, do that recursively."
-;;   (let (ret
-;;         (default-directory directory))
-;;     (dolist (entry (directory-files "."))
-;;       (cond ((or (string-match "^\\."
-;;                                (file-name-nondirectory entry))
-;;                  (string-match "~$"
-;;                                (file-name-nondirectory entry)))
-;;              nil)
-;;             ((and recurse-p
-;;                   (file-directory-p entry))
-;;              (setq ret (nconc ret
-;;                               (yas/glob-files (expand-file-name entry)
-;;                                               recurse-p
-;;                                               (if append
-;;                                                   (concat append "/" entry)
-;;                                                 entry)))))
-;;             ((file-directory-p entry)
-;;              nil)
-;;             (t
-;;              (push (if append
-;;                        (concat append "/" entry)
-;;                      entry) ret))))
-;;     ret))
-
 (defun yas/subdirs (directory &optional file?)
   "Return subdirs or files of DIRECTORY according to FILE?."
   (remove-if (lambda (file)
                (or (string-match "^\\."
                                  (file-name-nondirectory file))
+                   (string-match "^#.*#$"
+                                 (file-name-nondirectory file))
                    (string-match "~$"
                                  (file-name-nondirectory file))
                    (if file?
@@ -1356,17 +1434,22 @@ Here's a list of currently recognized variables:
              (directory-files directory t)))
 
 (defun yas/make-menu-binding (template)
-  `(lambda () (interactive) (yas/expand-or-visit-from-menu ,template)))
-
-(defun yas/expand-or-visit-from-menu (template)
-  (if yas/visit-from-menu
-      (yas/visit-snippet-file-1 template)
-    (let ((where (if mark-active
-                     (cons (region-beginning) (region-end))
-                   (cons (point) (point)))))
-      (yas/expand-snippet (yas/template-content template)
-                          (car where)
-                          (cdr where)))))
+  (let ((mode (intern (yas/table-name (yas/template-table template)))))
+    `(lambda () (interactive) (yas/expand-or-visit-from-menu ',mode ,(yas/template-uuid template)))))
+
+(defun yas/expand-or-visit-from-menu (mode uuid)
+  (let* ((table (yas/table-get-create mode))
+         (yas/current-template (and table
+                                    (gethash uuid (yas/table-uuidhash table)))))
+    (when yas/current-template
+      (if yas/visit-from-menu
+          (yas/visit-snippet-file-1 yas/current-template)
+        (let ((where (if mark-active
+                         (cons (region-beginning) (region-end))
+                       (cons (point) (point)))))
+          (yas/expand-snippet (yas/template-content yas/current-template)
+                              (car where)
+                              (cdr where)))))))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Popping up for keys and templates
@@ -1376,11 +1459,17 @@ Here's a list of currently recognized variables:
 
 TEMPLATES is a list of `yas/template'."
   (when templates
-    (some #'(lambda (fn)
-              (funcall fn (or prompt "Choose a snippet: ")
-                       templates
-                       #'yas/template-name))
-          yas/prompt-functions)))
+    (setq templates
+          (sort templates #'(lambda (t1 t2)
+                              (< (length (yas/template-name t1))
+                                 (length (yas/template-name t2))))))
+    (if yas/x-pretty-prompt-templates
+        (yas/x-pretty-prompt-templates "Choose a snippet" templates)
+      (some #'(lambda (fn)
+                (funcall fn (or prompt "Choose a snippet: ")
+                         templates
+                         #'yas/template-name))
+            yas/prompt-functions))))
 
 (defun yas/prompt-for-keys (keys &optional prompt)
   "Interactively choose a template key from the list KEYS."
@@ -1394,71 +1483,114 @@ TEMPLATES is a list of `yas/template'."
     (some #'(lambda (fn)
               (funcall fn (or prompt "Choose a snippet table: ")
                        tables
-                       #'yas/snippet-table-name))
+                       #'yas/table-name))
           yas/prompt-functions)))
 
 (defun yas/x-prompt (prompt choices &optional display-fn)
+  "Display choices in a x-window prompt."
+  ;; FIXME: HACK: if we notice that one of the objects in choices is
+  ;; actually a `yas/template', defer to `yas/x-prompt-pretty-templates'
+  ;;
+  ;; This would be better implemented by passing CHOICES as a
+  ;; strucutred tree rather than a list. Modifications would go as far
+  ;; up as `yas/all-templates' I think.
+  ;;
   (when (and window-system choices)
-    (let ((keymap (cons 'keymap
-                        (cons
-                         prompt
-                         (mapcar (lambda (choice)
-                                   (list choice
-                                         'menu-item
-                                         (if display-fn
-                                             (funcall display-fn choice)
-                                           choice)
-                                         t))
-                                 choices)))))
-      (when (cdr keymap)
-        (car (x-popup-menu (if (fboundp 'posn-at-point)
+    (let ((chosen
+           (let (menu d) ;; d for display
+             (dolist (c choices)
+               (setq d (or (and display-fn (funcall display-fn c))
+                           c))
+               (cond ((stringp d)
+                      (push (cons (concat "   " d) c) menu))
+                     ((listp d)
+                      (push (car d) menu))))
+             (setq menu (list prompt (push "title" menu)))
+             (x-popup-menu (if (fboundp 'posn-at-point)
                                (let ((x-y (posn-x-y (posn-at-point (point)))))
                                  (list (list (+ (car x-y) 10)
                                              (+ (cdr x-y) 20))
                                        (selected-window)))
                              t)
-                           keymap))))))
+                           menu))))
+      (or chosen
+          (keyboard-quit)))))
+
+(defvar yas/x-pretty-prompt-templates nil
+  "If non-nil, attempt to prompt for templates like TextMate.")
+(defun yas/x-pretty-prompt-templates (prompt templates)
+  "Display TEMPLATES, grouping neatly by table name."
+  (let ((props (list))
+        menu
+        more-than-one-table
+        prefix)
+    (dolist (tl templates)
+      (push tl (getf props (intern (yas/table-name (yas/template-table tl))))))
+    (setq more-than-one-table (> (length props) 2))
+    (setq prefix (if more-than-one-table
+                     "   " ""))
+    (dolist (thing props)
+      (cond ((listp thing)
+             (setq menu (nconc (mapcar #'(lambda (tl)
+                                           (cons (concat prefix (yas/template-name tl))
+                                                 tl))
+                                       thing)
+                               menu)))
+            (more-than-one-table
+             (push (symbol-name thing) menu))))
+    (setq menu (nreverse menu))
+    (x-popup-menu (if (fboundp 'posn-at-point)
+                      (let ((x-y (posn-x-y (posn-at-point (point)))))
+                        (list (list (+ (car x-y) 10)
+                                    (+ (cdr x-y) 20))
+                              (selected-window)))
+                    t)
+                  (list prompt (push "title" menu)))))
 
 (defun yas/ido-prompt (prompt choices &optional display-fn)
   (when (and (featurep 'ido)
              ido-mode)
-    (let* ((formatted-choices (or (and display-fn
-                                       (mapcar display-fn choices))
-                                  choices))
-           (chosen (and formatted-choices
-                        (ido-completing-read prompt
-                                             formatted-choices
-                                             nil
-                                             'require-match
-                                             nil
-                                             nil))))
-      (when chosen
-        (nth (position chosen formatted-choices :test #'string=) choices)))))
+    (yas/completing-prompt prompt choices display-fn #'ido-completing-read)))
 
 (eval-when-compile (require 'dropdown-list nil t))
 (defun yas/dropdown-prompt (prompt choices &optional display-fn)
   (when (featurep 'dropdown-list)
-    (let* ((formatted-choices (or (and display-fn
-                                       (mapcar display-fn choices))
-                                  choices))
-           (chosen (and formatted-choices
+    (let (formatted-choices
+          filtered-choices
+          chosen
+          d)
+      (dolist (choice choices)
+        (setq d (or (and display-fn (funcall display-fn choice))
+                      choice))
+        (when (stringp d)
+          (push d formatted-choices)
+          (push choice filtered-choices)))
+      (setq chosen (and formatted-choices
                         (nth (dropdown-list formatted-choices)
-                             choices))))
-      chosen)))
-
-(defun yas/completing-prompt (prompt choices &optional display-fn)
-  (let* ((formatted-choices (or (and display-fn
-                                     (mapcar display-fn choices))
-                                choices))
-         (chosen (and formatted-choices
-                      (completing-read prompt
-                                       formatted-choices
-                                       nil
-                                       'require-match
-                                       nil
-                                       nil))))
+                             filtered-choices))))))
+
+(defun yas/completing-prompt (prompt choices &optional display-fn completion-fn)
+  (let (formatted-choices
+        filtered-choices
+        chosen
+        d
+        (completion-fn (or completion-fn
+                           #'completing-read)))
+    (dolist (choice choices)
+      (setq d (or (and display-fn (funcall display-fn choice))
+                    choice))
+      (when (stringp d)
+        (push d formatted-choices)
+        (push choice filtered-choices)))
+    (setq chosen (and formatted-choices
+                      (funcall completion-fn prompt
+                               formatted-choices
+                               nil
+                               'require-match
+                               nil
+                               nil)))
     (when chosen
-      (nth (position chosen formatted-choices :test #'string=) choices))))
+      (nth (position chosen formatted-choices :test #'string=) filtered-choices))))
 
 (defun yas/no-prompt (prompt choices &optional display-fn)
   (first choices))
@@ -1466,44 +1598,46 @@ TEMPLATES is a list of `yas/template'."
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Loading snippets from files
 ;;
-(defun yas/load-directory-1 (directory &optional parents no-hierarchy-parents making-groups-sym)
+(defun yas/load-directory-1 (directory &optional mode-sym parents)
   "Recursively load snippet templates from DIRECTORY."
-  ;; TODO: Rewrite this horrible, horrible monster I created
-  ;; TODO: Temp hack, load a .yas-setup.el file if its exists
-  (let ((file (concat directory "/" ".yas-setup.el")))
-    (when (file-readable-p file)
+
+  ;; Load .yas-setup.el files wherever we find them
+  ;;
+  (let ((file (concat directory "/" ".yas-setup")))
+    (when (or (file-readable-p (concat file ".el"))
+              (file-readable-p (concat file ".elc")))
       (load file)))
-  
+
+  ;;
+  ;;
   (unless (file-exists-p (concat directory "/" ".yas-skip"))
-    (let* ((major-mode-and-parents (unless making-groups-sym
-                                     (yas/compute-major-mode-and-parents (concat directory "/dummy")
-                                                                         nil
-                                                                         no-hierarchy-parents)))
-           (yas/ignore-filenames-as-triggers (or yas/ignore-filenames-as-triggers
-                                                 (file-exists-p (concat directory "/" ".yas-ignore-filenames-as-triggers"))))
-           (mode-sym (and major-mode-and-parents
-                          (car major-mode-and-parents)))
-           (parents (if making-groups-sym
-                        parents
-                      (rest major-mode-and-parents)))
-           (snippet-defs nil)
-           (make-groups-p (or making-groups-sym
-                              (file-exists-p (concat directory "/" ".yas-make-groups")))))
+    (let* ((major-mode-and-parents (if mode-sym
+                                       (cons mode-sym parents)
+                                     (yas/compute-major-mode-and-parents (concat directory
+                                                                                 "/dummy"))))
+           (yas/ignore-filenames-as-triggers
+            (or yas/ignore-filenames-as-triggers
+                (file-exists-p (concat directory "/"
+                                       ".yas-ignore-filenames-as-triggers"))))
+           (snippet-defs nil))
+      ;; load the snippet files
+      ;;
       (with-temp-buffer
         (dolist (file (yas/subdirs directory 'no-subdirs-just-files))
           (when (file-readable-p file)
             (insert-file-contents file nil nil nil t)
             (push (yas/parse-template file)
                   snippet-defs))))
-      (yas/define-snippets (or mode-sym
-                               making-groups-sym)
-                           snippet-defs
-                           parents)
+      (when snippet-defs
+        (yas/define-snippets (car major-mode-and-parents)
+                             snippet-defs
+                             (cdr major-mode-and-parents)))
+      ;; now recurse to a lower level
+      ;;
       (dolist (subdir (yas/subdirs directory))
-        (if make-groups-p
-            (yas/load-directory-1 subdir parents 't (or mode-sym
-                                                        making-groups-sym))
-          (yas/load-directory-1 subdir (list mode-sym)))))))
+        (yas/load-directory-1 subdir
+                              (car major-mode-and-parents)
+                              (cdr major-mode-and-parents))))))
 
 (defun yas/load-directory (directory)
   "Load snippet definition from a directory hierarchy.
@@ -1515,13 +1649,21 @@ content of the file is the template."
   (interactive "DSelect the root directory: ")
   (unless (file-directory-p directory)
     (error "Error %s not a directory" directory))
-  (unless yas/root-directory
-    (setq yas/root-directory directory))
+  (unless yas/snippet-dirs
+    (setq yas/snippet-dirs directory))
   (dolist (dir (yas/subdirs directory))
-    (yas/load-directory-1 dir nil 'no-hierarchy-parents))
+    (yas/load-directory-1 dir))
   (when (interactive-p)
     (message "[yas] Loaded snippets from %s." directory)))
 
+(defun yas/load-snippet-dirs ()
+  "Reload the directories listed in `yas/snippet-dirs' or
+   prompt the user to select one."
+  (if yas/snippet-dirs
+      (dolist (directory (reverse (yas/snippet-dirs)))
+        (yas/load-directory directory))
+    (call-interactively 'yas/load-directory)))
+
 (defun yas/reload-all (&optional reset-root-directory)
   "Reload all snippets and rebuild the YASnippet menu. "
   (interactive "P")
@@ -1533,7 +1675,7 @@ content of the file is the template."
                               (yas/minor-mode -1))))
     ;; Empty all snippet tables and all menu tables
     ;;
-    (setq yas/snippet-tables (make-hash-table))
+    (setq yas/tables (make-hash-table))
     (setq yas/menu-table (make-hash-table))
 
     ;; Init the `yas/minor-mode-map', taking care not to break the
@@ -1543,20 +1685,15 @@ content of the file is the template."
           (cdr (yas/init-minor-keymap)))
 
     (when reset-root-directory
-      (setq yas/root-directory nil))
+      (setq yas/snippet-dirs nil))
 
-    ;; Reload the directories listed in `yas/root-directory' or prompt
+    ;; Reload the directories listed in `yas/snippet-dirs' or prompt
     ;; the user to select one.
     ;;
-    (if yas/root-directory
-        (if (listp yas/root-directory)
-            (dolist (directory yas/root-directory)
-              (yas/load-directory directory))
-          (yas/load-directory yas/root-directory))
-      (call-interactively 'yas/load-directory))
+    (yas/load-snippet-dirs)
     ;; Reload the direct keybindings
     ;;
-    (yas/snippet-keymaps-reload)
+    (yas/direct-keymaps-reload)
     ;; Restore the mode configuration
     ;;
     (when restore-minor-mode
@@ -1609,8 +1746,9 @@ Here's the default value for all the parameters:
                         (require 'yasnippet-bundle)`\"
                       \"dropdown-list.el\")
 "
-  (interactive "ffind the yasnippet.el file: \nFTarget bundle file: \nDSnippet directory to bundle: \nMExtra code? \nfdropdown-library: ")
-  
+  (interactive (concat "ffind the yasnippet.el file: \nFTarget bundle file: "
+                       "\nDSnippet directory to bundle: \nMExtra code? \nfdropdown-library: "))
+
   (let* ((yasnippet (or yasnippet
                         "yasnippet.el"))
          (yasnippet-bundle (or yasnippet-bundle
@@ -1653,9 +1791,9 @@ Here's the default value for all the parameters:
                         (condition              (fourth  snippet))
                         (group                  (fifth   snippet))
                         (expand-env             (sixth   snippet))
-                        ;; Omit the file on purpose
-                        (file                   nil) ;; (seventh snippet)) 
-                        (binding                (eighth  snippet)))
+                        (file                   nil) ;; (seventh snippet)) ;; omit on purpose
+                        (binding                (eighth  snippet))
+                        (uuid                    (ninth   snippet)))
                     (push `(,key
                             ,template-content
                             ,name
@@ -1663,13 +1801,19 @@ Here's the default value for all the parameters:
                             ,group
                             ,expand-env
                             ,file
-                            ,binding)
+                            ,binding
+                            ,uuid)
                           literal-snippets)))
                 (insert (pp-to-string `(yas/define-snippets ',mode ',literal-snippets ',parent-or-parents)))
                 (insert "\n\n"))))
         (dolist (dir dirs)
           (dolist (subdir (yas/subdirs dir))
-            (yas/load-directory-1 subdir nil 'no-hierarchy-parents))))
+            (let ((file (concat subdir "/.yas-setup.el")))
+              (when (file-readable-p file)
+                (insert ";; Supporting elisp for subdir " (file-name-nondirectory subdir) "\n\n")
+                (goto-char (+ (point)
+                              (second (insert-file-contents file))))))
+            (yas/load-directory-1 subdir nil))))
 
       (insert (pp-to-string `(yas/global-mode 1)))
       (insert ")\n\n" code "\n")
@@ -1677,14 +1821,14 @@ Here's the default value for all the parameters:
       ;; bundle-specific provide and value for yas/dont-activate
       (let ((bundle-feature-name (file-name-nondirectory
                                   (file-name-sans-extension
-                                   yasnippet-bundle)))) 
+                                   yasnippet-bundle))))
         (insert (pp-to-string `(set-default 'yas/dont-activate
                                             #'(lambda ()
-                                                (and (or yas/root-directory
+                                                (and (or yas/snippet-dirs
                                                          (featurep ',(make-symbol bundle-feature-name)))
                                                      (null (yas/get-snippet-tables)))))))
         (insert (pp-to-string `(provide ',(make-symbol bundle-feature-name)))))
-      
+
       (insert ";;; "
               (file-name-nondirectory yasnippet-bundle)
               " ends here\n"))))
@@ -1713,182 +1857,209 @@ Here's the default value for all the parameters:
   "Define SNIPPETS for MODE.
 
 SNIPPETS is a list of snippet definitions, each taking the
-following form:
+following form
+
+ (KEY TEMPLATE NAME CONDITION GROUP EXPAND-ENV FILE KEYBINDING UUID)
+
+Within these, only KEY and TEMPLATE are actually mandatory.
 
- (KEY TEMPLATE NAME CONDITION GROUP EXPAND-ENV FILE KEYBINDING)
+TEMPLATE might be a lisp form or a string, depending on whether
+this is a snippet or a snippet-command.
 
-Within these, only TEMPLATE is actually mandatory.
+CONDITION, EXPAND-ENV and KEYBINDING are lisp forms, they have
+been `yas/read-lisp'-ed and will eventually be
+`yas/eval-lisp'-ed.
 
-All the elelements are strings, including CONDITION, EXPAND-ENV
-and KEYBINDING which will be `read' and eventually `eval'-ed.
+The remaining elements are strings.
 
 FILE is probably of very little use if you're programatically
 defining snippets.
 
+UUID is the snippets \"unique-id\". Loading a second snippet file
+with the same uuid replaced the previous snippet.
+
 You can use `yas/parse-template' to return such lists based on
 the current buffers contents.
 
 Optional PARENT-MODE can be used to specify the parent tables of
 MODE. It can be a mode symbol of a list of mode symbols. It does
 not need to be a real mode."
-  (let ((snippet-table (yas/snippet-table-get-create mode))
-        (parent-tables (mapcar #'yas/snippet-table-get-create
+  ;; X) `snippet-table' is created or retrieved for MODE, same goes
+  ;;    for the list of snippet tables `parent-tables'.
+  ;;
+  (let ((snippet-table (yas/table-get-create mode))
+        (parent-tables (mapcar #'yas/table-get-create
                                (if (listp parent-mode)
                                    parent-mode
                                  (list parent-mode))))
-        (keymap (if yas/use-menu
-                    (yas/menu-keymap-get-create mode)
-                  nil)))
-    ;; Setup the menu
+        (template nil))
+    ;; X) Connect `snippet-table' with `parent-tables'.
+    ;;
+    ;; TODO: this should be a remove-duplicates of the concatenation
+    ;; of `snippet-table's existings parents with the new parents...
     ;;
-    (when parent-tables
-      (setf (yas/snippet-table-parents snippet-table)
-            parent-tables)
-      (when yas/use-menu
-        (let ((parent-menu-syms-and-names
-               (if (listp parent-mode)
-                   (mapcar #'(lambda (sym)
-                               (cons sym (concat "parent mode - " (symbol-name sym))))
-                           parent-mode)
-                 '((parent-mode . "parent mode")))))
-          (mapc #'(lambda (sym-and-name)
-                    (define-key keymap
-                      (vector (intern (replace-regexp-in-string " " "_" (cdr sym-and-name))))
-                      (list 'menu-item (cdr sym-and-name)
-                            (yas/menu-keymap-get-create (car sym-and-name)))))
-                (reverse parent-menu-syms-and-names)))))
-    (when yas/use-menu
-      (define-key yas/minor-mode-menu (vector mode)
-        `(menu-item ,(symbol-name mode) ,keymap
-                    :visible (yas/show-menu-p ',mode))))
-    ;; Iterate the recently parsed snippets definition
+    (dolist (parent parent-tables)
+      (unless (find parent (yas/table-parents snippet-table))
+        (push parent
+              (yas/table-parents snippet-table))))
+
+    ;; X) Now, iterate for evey snippet def list
     ;;
     (dolist (snippet snippets)
-      (let* ((file (seventh snippet))
-             (key (or (car snippet)
-                      (unless yas/ignore-filenames-as-triggers
-                        (and file 
-                             (file-name-sans-extension (file-name-nondirectory file))))))
-             (name (or (third snippet)
-                       (and file
-                            (file-name-directory file))))
-             (condition (fourth snippet))
-             (group (fifth snippet))
-             (keybinding (eighth snippet))
-             (template nil))
-        ;; Read the snippet's "binding :" expression and turn it into
-        ;; a keysequence vector if all is OK. 
-        ;;
-        (when keybinding
-          (condition-case err
-              (let ((keybinding-string (or (and (string-match "\".*\"" (eighth snippet))
-                                                 (read (eighth snippet)))
-                                            ;; "KEY-DESC" with quotes is deprecated..., but supported
-                                            (eighth snippet))))
-                (setq keybinding (read-kbd-macro keybinding-string 'need-vector)))
-            (error
-             (message "[yas] warning: keybinding \"%s\" invalid for snippet \"%s\" since %s."
-                      keybinding name (error-message-string err))
-             (setf keybinding nil))))
-
-        ;; Create the `yas/template' object and store in the
-        ;; appropriate snippet table. This only done if we have found
-        ;; a key and a name for the snippet, because that is what
-        ;; indexes the snippet tables
-        ;;
-        (setq template (yas/make-template (second snippet)
-                                          (or name key)
-                                          condition
-                                          (sixth snippet)
-                                          (seventh snippet)
-                                          keybinding))
-        (when name
-          ;; The direct keybinding
-          (yas/remove-snippet snippet-table name keybinding template #'vectorp)
-          (when keybinding
-            (yas/add-snippet snippet-table name keybinding template))
-          ;; The trigger key (key can be null if we removed the key) 
-          (yas/remove-snippet snippet-table name key template #'stringp)
-          (when key
-            (yas/add-snippet snippet-table name key template)))
-
-        ;; Setup the menu groups, reorganizing from group to group if
-        ;; necessary
-        ;;
-        (when yas/use-menu
-          (let ((group-keymap keymap))
-            ;; Delete this entry from another group if already exists
-            ;; in some other group. An entry is considered as existing
-            ;; in another group if its name string-matches.
-            ;;
-            (yas/delete-from-keymap group-keymap name)
-
-            ;; ... then add this entry to the correct group
-            (when (and (not (null group))
-                       (not (string= "" group)))
-              (dolist (subgroup (mapcar #'make-symbol
-                                        (split-string group "\\.")))
-                (let ((subgroup-keymap (lookup-key group-keymap
-                                                   (vector subgroup))))
-                  (when (null subgroup-keymap)
-                    (setq subgroup-keymap (make-sparse-keymap))
-                    (define-key group-keymap (vector subgroup)
-                      `(menu-item ,(symbol-name subgroup)
-                                  ,subgroup-keymap)))
-                  (setq group-keymap subgroup-keymap))))
-            (define-key group-keymap (vector (gensym))
-              `(menu-item ,(yas/template-name template)
-                          ,(yas/make-menu-binding template)
-                          :help ,name
-                          :keys ,(or (and key name
-                                          (concat key yas/trigger-symbol))
-                                     (and keybinding (key-description keybinding)))))))))))
+      (setq template (yas/define-snippets-1 snippet
+                                            snippet-table)))
+    template))
+
+(defun yas/define-snippets-1 (snippet snippet-table)
+  "Helper for `yas/define-snippets'."
+  ;; X) Calculate some more defaults on the values returned by
+  ;; `yas/parse-template'.
+  ;;
+  (let* ((file (seventh snippet))
+         (key (or (car snippet)
+                  (unless yas/ignore-filenames-as-triggers
+                    (and file
+                         (file-name-sans-extension (file-name-nondirectory file))))))
+         (name (or (third snippet)
+                   (and file
+                        (file-name-directory file))))
+         (condition (fourth snippet))
+         (group (fifth snippet))
+         (keybinding (yas/read-keybinding (eighth snippet)))
+         (uuid (or (ninth snippet)
+                  name))
+         (template (or (gethash uuid (yas/table-uuidhash snippet-table))
+                       (yas/make-blank-template))))
+    ;; X) populate the template object
+    ;;
+    (yas/populate-template template
+                           :table       snippet-table
+                           :key         key
+                           :content     (second snippet)
+                           :name        (or name key)
+                           :group       group
+                           :condition   condition
+                           :expand-env  (sixth snippet)
+                           :file        (seventh snippet)
+                           :keybinding  keybinding
+                           :uuid         uuid)
+    ;; X) Update this template in the appropriate table. This step
+    ;;    also will take care of adding the key indicators in the
+    ;;    templates menu entry, if any
+    ;;
+    (yas/update-template snippet-table template)
+    ;; X) Return the template
+    ;;
+    ;;
+    template))
+
+(defun yas/snippet-menu-binding-pair-get-create (template &optional type)
+  "Get TEMPLATE's menu binding or assign it a new one."
+  (or (yas/template-menu-binding-pair template)
+      (let ((key (yas/template-key template))
+            (keybinding (yas/template-keybinding template)))
+        (setf (yas/template-menu-binding-pair template)
+              (cons `(menu-item ,(or (yas/template-name template)
+                                     (yas/template-uuid template))
+                                ,(yas/make-menu-binding template)
+                                :keys ,nil)
+                    type)))))
 
 (defun yas/show-menu-p (mode)
   (cond ((eq yas/use-menu 'abbreviate)
          (find mode
                (mapcar #'(lambda (table)
-                           (intern (yas/snippet-table-name table)))
+                           (intern (yas/table-name table)))
                        (yas/get-snippet-tables))))
         ((eq yas/use-menu 'real-modes)
          (yas/real-mode? mode))
         (t
-         t))) 
+         t)))
 
-(defun yas/delete-from-keymap (keymap name)
-  "Recursively delete items name NAME from KEYMAP and its submenus.
+(defun yas/delete-from-keymap (keymap uuid)
+  "Recursively delete items with UUID from KEYMAP and its submenus."
 
-Skip any submenus named \"parent mode\""
+  ;; XXX: This used to skip any submenus named \"parent mode\"
+  ;; 
   ;; First of all, recursively enter submenus, i.e. the tree is
   ;; searched depth first so that stale submenus can be found in the
   ;; higher passes.
   ;;
   (mapc #'(lambda (item)
-            (when (and (keymapp (fourth item))
-                       (stringp (third item))
-                       (not (string-match "parent mode" (third item))))
-              (yas/delete-from-keymap (fourth item) name)))
+            (when (and (listp (cdr item))
+                       (keymapp (third (cdr item))))
+              (yas/delete-from-keymap (third (cdr item)) uuid)))
         (rest keymap))
-  ;;
-  (when (keymapp keymap)
-    (let ((pos-in-keymap))
-      (while (setq pos-in-keymap
-                   (position-if #'(lambda (item)
-                                    (and (listp item)
-                                         (or
-                                          ;; the menu item we want to delete
-                                          (and (eq 'menu-item (second item))
-                                               (third item)
-                                               (and (string= (third item) name)))
-                                          ;; a stale subgroup
-                                          (and (keymapp (fourth item))
-                                               (not (and (stringp (third item))
-                                                         (string-match "parent mode"
-                                                                       (third item))))
-                                               (null (rest (fourth item)))))))
-                                keymap))
-        (setf (nthcdr pos-in-keymap keymap)
-              (nthcdr (+ 1 pos-in-keymap) keymap))))))
+  ;; Set the uuid entry to nil
+  ;; 
+  (define-key keymap (vector (make-symbol uuid)) nil)
+  ;; Destructively modify keymap
+  ;; 
+  (setcdr keymap (delete-if #'(lambda (item)
+                                (or (null (cdr item))
+                                    (and (keymapp (third (cdr item)))
+                                         (null (cdr (third (cdr item)))))))
+                            (rest keymap))))
+
+(defun yas/define-menu (mode menu omit-items)
+  "Define a snippet menu for MODE according to MENU, ommitting OMIT-ITEMS.
+
+MENU is a list, its elements can be:
+
+- (yas/item UUID) : Creates an entry the snippet identified with
+  UUID. The menu entry for a snippet thus identified is
+  permanent, i.e. it will never move in the menu.
+
+- (yas/separator) : Creates a separator
+
+- (yas/submenu NAME SUBMENU) : Creates a submenu with NAME,
+  SUBMENU has the same form as MENU. NAME is also added to the
+  list of groups of the snippets defined thereafter.
+
+OMIT-ITEMS is a list of snippet uuid's that will always be
+ommited from MODE's menu, even if they're manually loaded.
+"
+  (let* ((table (yas/table-get-create mode))
+         (hash (yas/table-uuidhash table)))
+    (yas/define-menu-1 table
+                       (yas/menu-keymap-get-create table)
+                       menu
+                       hash)
+    (dolist (uuid omit-items)
+      (let ((template (or (gethash uuid hash)
+                          (yas/populate-template (puthash uuid
+                                                          (yas/make-blank-template)
+                                                          hash)
+                                                 :table table
+                                                 :uuid uuid))))
+        (setf (yas/template-menu-binding-pair template) (cons nil :none))))))
+
+(defun yas/define-menu-1 (table keymap menu uuidhash &optional group-list)
+  (dolist (e (reverse menu))
+    (cond ((eq (first e) 'yas/item)
+           (let ((template (or (gethash (second e) uuidhash)
+                               (yas/populate-template (puthash (second e)
+                                                               (yas/make-blank-template)
+                                                               uuidhash)
+                                                      :table table
+                                                      :perm-group group-list
+                                                      :uuid (second e)))))
+             (define-key keymap (vector (make-symbol (second e)))
+               (car (yas/snippet-menu-binding-pair-get-create template :stay)))))
+          ((eq (first e) 'yas/submenu)
+           (let ((subkeymap (make-sparse-keymap)))
+             (define-key keymap (vector (make-symbol(second e)))
+               `(menu-item ,(second e) ,subkeymap))
+             (yas/define-menu-1 table
+                                subkeymap
+                                (third e)
+                                uuidhash
+                                (append group-list (list (second e))))))
+          ((eq (first e) 'yas/separator)
+           (define-key keymap (vector (gensym))
+             '(menu-item "----")))
+          (t
+           (message "[yas] don't know anything about menu entry %s" (first e))))))
 
 (defun yas/define (mode key template &optional name condition group)
   "Define a snippet.  Expanding KEY into TEMPLATE.
@@ -1909,8 +2080,37 @@ will only be expanded when the condition evaluated to non-nil."
     (undo 1)
     nil))
 
-(defalias 'yas/expand 'yas/expand-from-trigger-key)
 
+;;; Apropos condition-cache:
+;;;
+;;;
+;;;
+;;;
+(defvar yas/condition-cache-timestamp nil)
+(defmacro yas/define-condition-cache (func doc &rest body)
+  "Define a function FUNC with doc DOC and body BODY, BODY is
+executed at most once every snippet expansion attempt, to check
+expansion conditions.
+
+It doesn't make any sense to call FUNC programatically."
+  `(defun ,func () ,(if (and doc
+                             (stringp doc))
+                        (concat doc
+"\n\nFor use in snippets' conditions. Within each
+snippet-expansion routine like `yas/expand', computes actual
+value for the first time then always returns a cached value.")
+                      (setq body (cons doc body))
+                      nil)
+     (let ((timestamp-and-value (get ',func 'yas/condition-cache)))
+       (if (equal (car timestamp-and-value) yas/condition-cache-timestamp)
+           (cdr timestamp-and-value)
+         (let ((new-value (progn
+                            ,@body
+                            )))
+           (put ',func 'yas/condition-cache (cons yas/condition-cache-timestamp new-value))
+           new-value)))))
+
+(defalias 'yas/expand 'yas/expand-from-trigger-key)
 (defun yas/expand-from-trigger-key (&optional field)
   "Expand a snippet before point.
 
@@ -1920,9 +2120,11 @@ defined in `yas/fallback-behavior'.
 Optional argument FIELD is for non-interactive use and is an
 object satisfying `yas/field-p' to restrict the expansion to."
   (interactive)
+  (setq yas/condition-cache-timestamp (current-time))
   (multiple-value-bind (templates start end) (if field
                                                  (save-restriction
-                                                   (narrow-to-region (yas/field-start field) (yas/field-end field))
+                                                   (narrow-to-region (yas/field-start field)
+                                                                     (yas/field-end field))
                                                    (yas/current-key))
                                                (yas/current-key))
     (if templates
@@ -1930,10 +2132,11 @@ object satisfying `yas/field-p' to restrict the expansion to."
       (yas/fallback 'trigger-key))))
 
 (defun yas/expand-from-keymap ()
-  "Directly expand some snippets, searching `yas/snippet-keymaps'.
+  "Directly expand some snippets, searching `yas/direct-keymaps'.
 
 If expansion fails, execute the previous binding for this key"
   (interactive)
+  (setq yas/condition-cache-timestamp (current-time))
   (let* ((vec (this-command-keys-vector))
          (templates (mapcan #'(lambda (table)
                                 (yas/fetch table vec))
@@ -1942,21 +2145,21 @@ If expansion fails, execute the previous binding for this key"
         (yas/expand-or-prompt-for-template templates)
       (let ((yas/fallback-behavior 'call-other-command))
         (yas/fallback)))))
-    
+
 (defun yas/expand-or-prompt-for-template (templates &optional start end)
   "Expand one of TEMPLATES from START to END.
 
 Prompt the user if TEMPLATES has more than one element, else
 expand immediately. Common gateway for
 `yas/expand-from-trigger-key' and `yas/expand-from-keymap'."
-  (let ((template (or (and (rest templates) ;; more than one
-                           (yas/prompt-for-template (mapcar #'cdr templates)))
-                      (cdar templates))))
-    (when template
-      (yas/expand-snippet (yas/template-content template)
+  (let ((yas/current-template (or (and (rest templates) ;; more than one
+                                       (yas/prompt-for-template (mapcar #'cdr templates)))
+                                  (cdar templates))))
+    (when yas/current-template
+      (yas/expand-snippet (yas/template-content yas/current-template)
                           start
                           end
-                          (yas/template-expand-env template)))))
+                          (yas/template-expand-env yas/current-template)))))
 
 (defun yas/fallback (&optional from-trigger-key-p)
   "Fallback after expansion has failed.
@@ -1968,19 +2171,21 @@ Common gateway for `yas/expand-from-trigger-key' and
          nil)
         ((eq yas/fallback-behavior 'call-other-command)
          (let* ((yas/minor-mode nil)
-                (yas/snippet-keymaps nil)
+                (yas/direct-keymaps nil)
                 (keys-1 (this-command-keys-vector))
                 (keys-2 (and yas/trigger-key
                              from-trigger-key-p
                              (stringp yas/trigger-key)
-                             (read-kbd-macro yas/trigger-key))) 
+                             (read-kbd-macro yas/trigger-key)))
                 (command-1 (and keys-1 (key-binding keys-1)))
                 (command-2 (and keys-2 (key-binding keys-2)))
                 ;; An (ugly) safety: prevents infinite recursion of
                 ;; yas/expand* calls.
-                (command (or (and (not (string-match "yas/expand" (symbol-name command-1)))
+                (command (or (and (symbolp command-1)
+                                  (not (string-match "yas/expand" (symbol-name command-1)))
                                   command-1)
-                             command-2)))
+                             (and (symbolp command-2)
+                                  command-2))))
            (when (and (commandp command)
                       (not (string-match "yas/expand" (symbol-name command))))
              (setq this-command command)
@@ -1997,7 +2202,7 @@ Common gateway for `yas/expand-from-trigger-key' and
         (t
          ;; also return nil if all the other fallbacks have failed
          nil)))
-    
+
 
 \f
 ;;; Snippet development
@@ -2012,12 +2217,12 @@ Honours `yas/choose-tables-first', `yas/choose-keys-first' and
   (mapcar #'cdr
           (if yas/choose-keys-first
               (let ((key (yas/prompt-for-keys
-                          (mapcan #'yas/snippet-table-all-keys tables))))
+                          (mapcan #'yas/table-all-keys tables))))
                 (when key
                   (mapcan #'(lambda (table)
                               (yas/fetch table key))
                           tables)))
-            (remove-duplicates (mapcan #'yas/snippet-table-templates tables)
+            (remove-duplicates (mapcan #'yas/table-templates tables)
                                :test #'equal))))
 
 (defun yas/insert-snippet (&optional no-condition)
@@ -2027,22 +2232,23 @@ to `yas/prompt-function'.
 With prefix argument NO-CONDITION, bypass filtering of snippets
 by condition."
   (interactive "P")
+  (setq yas/condition-cache-timestamp (current-time))
   (let* ((yas/buffer-local-condition (or (and no-condition
                                               'always)
                                          yas/buffer-local-condition))
          (templates (yas/all-templates (yas/get-snippet-tables)))
-         (template (and templates
-                        (or (and (rest templates) ;; more than one template for same key
-                                 (yas/prompt-for-template templates))
-                            (car templates))))
+         (yas/current-template (and templates
+                                    (or (and (rest templates) ;; more than one template for same key
+                                             (yas/prompt-for-template templates))
+                                        (car templates))))
          (where (if mark-active
                     (cons (region-beginning) (region-end))
                   (cons (point) (point)))))
-    (if template
-        (yas/expand-snippet (yas/template-content template)
+    (if yas/current-template
+        (yas/expand-snippet (yas/template-content yas/current-template)
                             (car where)
                             (cdr where)
-                            (yas/template-expand-env template))
+                            (yas/template-expand-env yas/current-template))
       (message "[yas] No snippets can be inserted here!"))))
 
 (defun yas/visit-snippet-file ()
@@ -2064,47 +2270,61 @@ visited file in `snippet-mode'."
 
 (defun yas/visit-snippet-file-1 (template)
   (let ((file (yas/template-file template)))
-    (cond ((and file (file-exists-p file))
+    (cond ((and file (file-readable-p file))
            (find-file-other-window file)
-           (snippet-mode))
+           (snippet-mode)
+           (setq yas/editing-template template))
           (file
            (message "Original file %s no longer exists!" file))
           (t
-           (message "This snippet was not loaded from a file!")))))
-
-(defun yas/guess-snippet-directories-1 (table &optional suffix)
-  "Guesses possible snippet subdirsdirectories for TABLE."
-  (unless suffix
-    (setq suffix (yas/snippet-table-name table))) 
-  (cons suffix
+           (switch-to-buffer (format "*%s*"(yas/template-name template)))
+           (let ((type 'snippet))
+             (when (listp (yas/template-content template))
+               (insert (format "# type: command\n"))
+               (setq type 'command))
+             (insert (format "# key: %s\n" (yas/template-key template)))
+             (insert (format "# name: %s\n" (yas/template-name template)))
+             (when (yas/template-keybinding template)
+               (insert (format "# binding: %s\n" (yas/template-keybinding template))))
+             (when (yas/template-expand-env template)
+               (insert (format "# expand-env: %s\n" (yas/template-expand-env template))))
+             (when (yas/template-condition template)
+               (insert (format "# condition: %s\n" (yas/template-condition template))))
+             (insert "# --\n")
+             (insert (if (eq type 'command)
+                         (pp-to-string (yas/template-content template))
+                       (yas/template-content template))))
+           (snippet-mode)
+           (setq yas/editing-template template)))))
+
+(defun yas/guess-snippet-directories-1 (table)
+  "Guesses possible snippet subdirectories for TABLE."
+  (cons (yas/table-name table)
         (mapcan #'(lambda (parent)
                     (yas/guess-snippet-directories-1
-                     parent
-                     (concat (yas/snippet-table-name parent) "/" suffix)))
-                (yas/snippet-table-parents table))))
+                     parent))
+                (yas/table-parents table))))
 
-(defun yas/guess-snippet-directories ()
+(defun yas/guess-snippet-directories (&optional table)
   "Try to guess suitable directories based on the current active
-tables.
-
-Returns a a list of options alist TABLE -> DIRS where DIRS are
-all the possibly directories where snippets of table might be
-lurking."
-  (let ((main-dir (or (and (listp yas/root-directory)
-                           (first yas/root-directory))
-                      yas/root-directory
-                      (setq yas/root-directory "~/.emacs.d/snippets")))
-        (tables (yas/get-snippet-tables)))
-    ;; HACK! the snippet table created here is a dummy table that
-    ;; holds the correct name so that `yas/make-directory-maybe' can
-    ;; work. The real table, if it does not exist in
-    ;; yas/snippet-tables will be created when the first snippet for
-    ;; that mode is loaded.
-    ;; 
-    (unless (gethash major-mode yas/snippet-tables)
-      (setq tables (cons (yas/make-snippet-table (symbol-name major-mode))
-                         tables)))
-    
+tables (or optional TABLE).
+
+Returns a list of elemts (TABLE . DIRS) where TABLE is a
+`yas/table' object and DIRS is a list of all possible directories
+where snippets of table might exist."
+  (let ((main-dir (replace-regexp-in-string
+                   "/+$" ""
+                   (or (first (or (yas/snippet-dirs)
+                                  (setq yas/snippet-dirs '("~/.emacs.d/snippets")))))))
+        (tables (or (and table
+                         (list table))
+                    (yas/get-snippet-tables))))
+    ;; HACK! the snippet table created here is actually registered!
+    ;;
+    (unless (or table (gethash major-mode yas/tables))
+      (push (yas/table-get-create major-mode)
+            tables))
+
     (mapcar #'(lambda (table)
                 (cons table
                       (mapcar #'(lambda (subdir)
@@ -2116,15 +2336,17 @@ lurking."
   "Returns a dir inside  TABLE-AND-DIRS, prompts for creation if none exists."
   (or (some #'(lambda (dir) (when (file-directory-p dir) dir)) (cdr table-and-dirs))
       (let ((candidate (first (cdr table-and-dirs))))
+        (unless (file-writable-p (file-name-directory candidate))
+          (error "[yas] %s is not writable." candidate))
         (if (y-or-n-p (format "Guessed directory (%s) for%s%s table \"%s\" does not exist! Create? "
                               candidate
-                              (if (gethash (intern (yas/snippet-table-name (car table-and-dirs)))
-                                           yas/snippet-tables)
+                              (if (gethash (intern (yas/table-name (car table-and-dirs)))
+                                           yas/tables)
                                   ""
                                 " brand new")
                               (or main-table-string
                                   "")
-                              (yas/snippet-table-name (car table-and-dirs))))
+                              (yas/table-name (car table-and-dirs))))
             (progn
               (make-directory candidate 'also-make-parents)
               ;; create the .yas-parents file here...
@@ -2133,53 +2355,29 @@ lurking."
 (defun yas/new-snippet (&optional choose-instead-of-guess)
   ""
   (interactive "P")
-  (let* ((guessed-directories (yas/guess-snippet-directories))
-         (option (or (and choose-instead-of-guess
-                          (some #'(lambda (fn)
-                                    (funcall fn "Choose a snippet table: "
-                                             guessed-directories
-                                             #'(lambda (option)
-                                                 (yas/snippet-table-name (car option)))))
-                                yas/prompt-functions))
-                     (first guessed-directories)))
-         (chosen))
-    (setq chosen (yas/make-directory-maybe option (unless choose-instead-of-guess
-                                                    " main")))
-    (unless (or chosen
-                choose-instead-of-guess)
-      (if (y-or-n-p (format "Continue guessing for other active tables %s? "
-                            (mapcar #'(lambda (table-and-dirs)
-                                        (yas/snippet-table-name (car table-and-dirs)))
-                                    (rest guessed-directories))))
-          (setq chosen (some #'yas/make-directory-maybe
-                             (rest guessed-directories)))))
-    (unless (or chosen
-                choose-instead-of-guess)
-      (when (y-or-n-p "Having trouble... use snippet root dir? ")
-        (setq chosen (if (listp yas/root-directory)
-                         (first yas/root-directory)
-                       yas/root-directory))))
-    (if chosen
-        (let ((default-directory chosen)
-              (name (read-from-minibuffer "Enter a snippet name: ")))
-          (find-file-other-window (concat name
-                                          ".yasnippet"))
-          (snippet-mode)
-          (unless (and choose-instead-of-guess
-                       (not (y-or-n-p "Insert a snippet with useful headers? ")))
-            (yas/expand-snippet (format 
-                                 "\
+  (let ((guessed-directories (yas/guess-snippet-directories)))
+
+    (switch-to-buffer "*new snippet*")
+    (erase-buffer)
+    (set (make-local-variable 'yas/editing-template) nil)
+    (snippet-mode)
+    (set (make-local-variable 'yas/guessed-modes) (mapcar #'(lambda (d)
+                                                              (intern (yas/table-name (car d))))
+                                                          guessed-directories))
+    (unless (and choose-instead-of-guess
+                 (not (y-or-n-p "Insert a snippet with useful headers? ")))
+      (yas/expand-snippet "\
 # -*- mode: snippet -*-
-# name: %s
-# key: $1${2:
-# binding: \"${3:keybinding}\"}${4:
-# expand-env: ((${5:some-var} ${6:some-value}))}
+# name: $1
+# key: $2${3:
+# binding: ${4:direct-keybinding}}${5:
+# expand-env: ((${6:some-var} ${7:some-value}))}${8:
+# type: command}
 # --
-$0" name))))
-      (message "[yas] aborted snippet creation."))))
+$0"))))
 
 (defun yas/find-snippets (&optional same-window )
-  "Look for user snippets in guessed current mode's directory.
+  "Find snippet file in guessed current mode's directory.
 
 Calls `find-file' interactively in the guessed directory.
 
@@ -2188,7 +2386,7 @@ With prefix arg SAME-WINDOW opens the buffer in the same window.
 Because snippets can be loaded from many different locations,
 this has to guess the correct directory using
 `yas/guess-snippet-directories', which returns a list of
-options. 
+options.
 
 If any one of these exists, it is taken and `find-file' is called
 there, otherwise, proposes to create the first option returned by
@@ -2201,15 +2399,13 @@ there, otherwise, proposes to create the first option returned by
     (unless chosen
       (if (y-or-n-p (format "Continue guessing for other active tables %s? "
                             (mapcar #'(lambda (table-and-dirs)
-                                        (yas/snippet-table-name (car table-and-dirs)))
+                                        (yas/table-name (car table-and-dirs)))
                                     (rest guessed-directories))))
           (setq chosen (some #'yas/make-directory-maybe
                              (rest guessed-directories)))))
     (unless chosen
       (when (y-or-n-p "Having trouble... go to snippet root dir? ")
-        (setq chosen (if (listp yas/root-directory)
-                         (first yas/root-directory)
-                       yas/root-directory))))
+        (setq chosen (first (yas/snippet-dirs)))))
     (if chosen
         (let ((default-directory chosen))
           (setq buffer (call-interactively (if same-window
@@ -2222,66 +2418,118 @@ there, otherwise, proposes to create the first option returned by
                 (snippet-mode)))))
       (message "Could not guess snippet dir!"))))
 
-(defun yas/compute-major-mode-and-parents (file &optional prompt-if-failed no-hierarchy-parents)
+(defun yas/compute-major-mode-and-parents (file &optional prompt-if-failed)
   (let* ((file-dir (and file
-                        (directory-file-name (or (locate-dominating-file file ".yas-make-groups")
+                        (directory-file-name (or (some #'(lambda (special)
+                                                           (locate-dominating-file file special))
+                                                       '(".yas-setup.el"
+                                                         ".yas-make-groups"
+                                                         ".yas-parents"))
                                                  (directory-file-name (file-name-directory file))))))
-         (extra-parents-file-name (concat file-dir "/.yas-parents"))
-         (no-hierarchy-parents (or no-hierarchy-parents
-                                   (file-readable-p extra-parents-file-name)))
+         (parents-file-name (concat file-dir "/.yas-parents"))
          (major-mode-name (and file-dir
                                (file-name-nondirectory file-dir)))
-         (parent-file-dir (and file-dir
-                               (directory-file-name (file-name-directory file-dir))))
-         (parent-mode-name (and parent-file-dir
-                                (not no-hierarchy-parents)
-                                (file-name-nondirectory parent-file-dir)))
          (major-mode-sym (or (and major-mode-name
                                   (intern major-mode-name))
                              (when prompt-if-failed
                                (read-from-minibuffer
                                 "[yas] Cannot auto-detect major mode! Enter a major mode: "))))
-         (parent-mode-sym (and parent-mode-name
-                               (intern parent-mode-name)))
-         (more-parents (when (file-readable-p extra-parents-file-name)
+         (parents (when (file-readable-p parents-file-name)
                          (mapcar #'intern
                                  (split-string
                                   (with-temp-buffer
-                                    (insert-file-contents extra-parents-file-name)
+                                    (insert-file-contents parents-file-name)
                                     (buffer-substring-no-properties (point-min)
                                                                     (point-max))))))))
     (when major-mode-sym
-      (remove nil (append (list major-mode-sym parent-mode-sym)
-                          more-parents)))))
+      (cons major-mode-sym parents))))
+
+(defvar yas/editing-template nil
+  "Supporting variable for `yas/load-snippet-buffer' and `yas/visit-snippet'")
+
+(defvar yas/current-template nil
+  "Holds the current template being expanded into a snippet.")
+
+(defvar yas/guessed-modes nil
+  "List of guessed modes supporting `yas/load-snippet-buffer'.")
 
 (defun yas/load-snippet-buffer (&optional kill)
   "Parse and load current buffer's snippet definition.
 
 With optional prefix argument KILL quit the window and buffer."
   (interactive "P")
-  (if buffer-file-name
-      (let ((major-mode-and-parent (yas/compute-major-mode-and-parents buffer-file-name)))
-        (if major-mode-and-parent
-            (let* ((yas/ignore-filenames-as-triggers (or yas/ignore-filenames-as-triggers
-                                                         (locate-dominating-file buffer-file-name ".yas-ignore-filenames-as-triggers")))
-                   (parsed (yas/parse-template buffer-file-name))
-                   (name (and parsed
-                              (third parsed))))
-              (when name
-                (let ((yas/better-guess-for-replacements t))
-                  (yas/define-snippets (car major-mode-and-parent)
-                                       (list parsed)
-                                       (cdr major-mode-and-parent)))
-                (when (and (buffer-modified-p)
-                           (y-or-n-p "Also save snippet buffer? "))
-                  (save-buffer))
-                (if kill
-                    (quit-window kill)
-                  (message "[yas] Snippet \"%s\" loaded for %s."
-                           name
-                           (car major-mode-and-parent)))))
-          (message "[yas] Cannot load snippet for unknown major mode")))
-    (message "Save the buffer as a file first!")))
+  (let ((yas/ignore-filenames-as-triggers
+         (or yas/ignore-filenames-as-triggers
+             (and buffer-file-name
+                  (locate-dominating-file
+                   buffer-file-name
+                   ".yas-ignore-filenames-as-triggers")))))
+    (cond
+     ;;  We have `yas/editing-template', this buffer's
+     ;;  content comes from a template which is already loaded and
+     ;;  neatly positioned,...
+     ;;
+     (yas/editing-template
+      (yas/define-snippets-1 (yas/parse-template (yas/template-file yas/editing-template)) 
+                             (yas/template-table yas/editing-template)))
+     ;; Try to use `yas/guessed-modes'. If we don't have that use the
+     ;; value from `yas/compute-major-mode-and-parents'
+     ;;
+     (t
+      (unless yas/guessed-modes
+        (set (make-local-variable 'yas/guessed-modes) (or (yas/compute-major-mode-and-parents buffer-file-name))))
+      (let* ((prompt (if (and (featurep 'ido)
+                              ido-mode)
+                         'ido-completing-read 'completing-read))
+             (table (yas/table-get-create
+                     (intern
+                      (funcall prompt (format "Choose or enter a table (yas guesses %s): "
+                                              (if yas/guessed-modes
+                                                  (first yas/guessed-modes)
+                                                "nothing"))
+                               (mapcar #'symbol-name yas/guessed-modes)
+                               nil
+                               nil
+                               nil
+                               nil
+                               (if (first yas/guessed-modes)
+                                   (symbol-name (first yas/guessed-modes))))))))
+        (set (make-local-variable 'yas/editing-template) 
+             (yas/define-snippets-1 (yas/parse-template buffer-file-name)
+                                    table))))))
+  ;; Now, offer to save this shit
+  ;;
+  ;; 1) if `yas/snippet-dirs' is a list and its first element does not
+  ;; match this template's file (i.e. this is a library snippet, not
+  ;; a user snippet).
+  ;;
+  ;; 2) yas/editing-template comes from a file that we cannot write to...
+  ;;
+  (when (or (not (yas/template-file yas/editing-template))
+            (not (file-writable-p (yas/template-file yas/editing-template)))
+            (and (listp yas/snippet-dirs)
+                 (second yas/snippet-dirs)
+                 (not (string-match (expand-file-name (first yas/snippet-dirs))
+                                    (yas/template-file yas/editing-template)))))
+    
+    (when (y-or-n-p "[yas] Looks like a library or new snippet. Save to new file? ")
+      (let* ((option (first (yas/guess-snippet-directories (yas/template-table yas/editing-template))))
+             (chosen (and option
+                          (yas/make-directory-maybe option))))
+        (when chosen
+          (let ((default-file-name (or (and (yas/template-file yas/editing-template)
+                                            (file-name-nondirectory (yas/template-file yas/editing-template)))
+                                       (yas/template-name yas/editing-template))))
+            (write-file (concat chosen "/"
+                                (read-from-minibuffer (format "File name to create in %s? " chosen)
+                                                      default-file-name)))
+            (setf (yas/template-file yas/editing-template) buffer-file-name))))))
+  (when kill
+    (quit-window kill))
+  (message "[yas] Snippet \"%s\" loaded for %s."
+           (yas/template-name yas/editing-template)
+           (yas/table-name (yas/template-table yas/editing-template))))
+
 
 (defun yas/tryout-snippet (&optional debug)
   "Test current buffers's snippet template in other buffer."
@@ -2291,30 +2539,142 @@ With optional prefix argument KILL quit the window and buffer."
          (test-mode (or (and (car major-mode-and-parent)
                              (fboundp (car major-mode-and-parent))
                              (car major-mode-and-parent))
+                        (first yas/guessed-modes)
                         (intern (read-from-minibuffer "[yas] please input a mode: "))))
-         (template (and parsed
-                        (fboundp test-mode)
-                        (yas/make-template (second parsed)
-                                           (third parsed)
-                                           nil
-                                           (sixth parsed)
-                                           nil
-                                           nil))))
-    (cond (template
-           (let ((buffer-name (format "*YAS TEST: %s*" (yas/template-name template))))
+         (yas/current-template
+          (and parsed
+               (fboundp test-mode)
+               (yas/populate-template (yas/make-blank-template)
+                                      :table       nil ;; no tables for ephemeral snippets
+                                      :key         (first parsed)
+                                      :content     (second parsed)
+                                      :name        (third parsed)
+                                      :expand-env  (sixth parsed)))))
+    (cond (yas/current-template
+           (let ((buffer-name (format "*testing snippet: %s*" (yas/template-name yas/current-template))))
              (set-buffer (switch-to-buffer buffer-name))
              (erase-buffer)
              (setq buffer-undo-list nil)
-             (funcall test-mode)
-             (yas/expand-snippet (yas/template-content template)
+             (condition-case nil (funcall test-mode) (error nil))
+             (yas/expand-snippet (yas/template-content yas/current-template)
                                  (point-min)
                                  (point-max)
-                                 (yas/template-expand-env template))
-             (when debug
+                                 (yas/template-expand-env yas/current-template))
+             (when (and debug
+                        (require 'yasnippet-debug nil t))
                (add-hook 'post-command-hook 'yas/debug-snippet-vars 't 'local))))
           (t
            (message "[yas] Cannot test snippet for unknown major mode")))))
 
+(defun yas/template-fine-group (template)
+  (car (last (or (yas/template-group template)
+                 (yas/template-perm-group template)))))
+
+(defun yas/describe-tables (&optional choose)
+  "Display snippets for each table."
+  (interactive "P")
+  (let* ((by-name-hash (and choose
+                            (y-or-n-p "Show by namehash? ")))
+         (buffer (get-buffer-create "*YASnippet tables*"))
+         (active-tables (yas/get-snippet-tables))
+         (remain-tables (let ((all))
+                          (maphash #'(lambda (k v)
+                                       (unless (find v active-tables)
+                                         (push v all)))
+                                   yas/tables)
+                          all))
+         (table-lists (list active-tables remain-tables))
+         (original-buffer (current-buffer))
+         (continue t)
+         (yas/condition-cache-timestamp (current-time)))
+    (with-current-buffer buffer
+      (setq buffer-read-only nil)
+      (erase-buffer)
+      (cond ((not by-name-hash)
+             (insert "YASnippet tables: \n")
+             (while (and table-lists
+                         continue)
+               (dolist (table (car table-lists))
+                 (yas/describe-pretty-table table))
+               (setq table-lists (cdr table-lists))
+               (when table-lists
+                 (yas/create-snippet-xrefs)
+                 (display-buffer buffer)
+                 (setq continue (and choose (y-or-n-p "Show also non-active tables? ")))))
+             (yas/create-snippet-xrefs)
+             (help-mode)
+             (goto-char 1))
+            (t
+             (insert "\n\nYASnippet tables by NAMEHASH: \n")
+             (dolist (table (append active-tables remain-tables))
+               (insert (format "\nSnippet table `%s':\n\n" (yas/table-name table)))
+               (let ((keys))
+                 (maphash #'(lambda (k v)
+                              (push k keys))
+                          (yas/table-hash table))
+                 (dolist (key keys)
+                   (insert (format "   key %s maps snippets: %s\n" key
+                                   (let ((names))
+                                     (maphash #'(lambda (k v)
+                                                  (push k names))
+                                              (gethash key (yas/table-hash table)))
+                                     names))))))))
+      (goto-char 1)
+      (setq buffer-read-only t))
+    (display-buffer buffer)))
+
+(defun yas/describe-pretty-table (table)
+  (insert (format "\nSnippet table `%s'"
+                  (yas/table-name table)))
+  (if (yas/table-parents table)
+      (insert (format " parents: %s\n"
+                      (mapcar #'yas/table-name
+                              (yas/table-parents table))))
+    (insert "\n"))
+  (insert (make-string 100 ?-) "\n")
+  (insert "group                   state name                                    key             binding\n")
+  (let ((groups-alist (list))
+        group)
+    (maphash #'(lambda (k v)
+                 (setq group (or (yas/template-fine-group v)
+                                 "(top level)"))
+                 (when (yas/template-name v)
+                   
+                   (aput 'groups-alist group (cons v (aget groups-alist group)))))
+             (yas/table-uuidhash table))
+    (dolist (group-and-templates groups-alist)
+      (when (rest groups-alist)
+        (setq group (truncate-string-to-width (car group-and-templates) 25 0 ?  "..."))
+        (insert (make-string 100 ?-) "\n")
+        (dolist (p (cdr group-and-templates))
+          (let ((name (truncate-string-to-width (propertize (format "\\\\snippet `%s'" (yas/template-name p))
+                                                            'yasnippet p)
+                                                50 0 ? "..."))
+                (group (prog1 group 
+                         (setq group (make-string (length group) ? ))))
+                (condition-string (let ((condition (yas/template-condition p)))
+                                    (if condition
+                                        (with-current-buffer original-buffer
+                                          (if (yas/eval-condition condition)
+                                              "(y)"
+                                            "(s)"))
+                                      "(a)"))))
+            (insert group " ")
+            (insert condition-string " ")
+            (insert name 
+                    (if (string-match "\\.\\.\\.$" name)
+                        "'"
+                      " ")
+                    " ")
+            (insert (truncate-string-to-width (or (yas/template-key p) "")
+                                              15 0 ?  "...") " ")
+            (insert (truncate-string-to-width (key-description (yas/template-keybinding p))
+                                              15 0 ?  "...") " ")
+            (insert "\n")))))))
+
+
+
+
 \f
 ;;; User convenience functions, for using in snippet definitions
 
@@ -2367,12 +2727,31 @@ Otherwise throw exception."
     (yas/throw (format "[yas] field only allows %s" possibilities))))
 
 (defun yas/field-value (number)
+  "A primary field transformation..."
   (let* ((snippet (car (yas/snippets-at-point)))
          (field (and snippet
                      (yas/snippet-find-field snippet number))))
     (when field
       (yas/field-text-for-display field))))
 
+(defun yas/text ()
+  "Return `yas/text' if that exists and is non-empty, else nil."
+  (if (and yas/text
+           (not (string= "" yas/text)))
+      yas/text))
+
+;; (defun yas/selected-text ()
+;;   "Return `yas/selected-text' if that exists and is non-empty, else nil."
+;;   (if (and yas/selected-text
+;;            (not (string= "" yas/selected-text)))
+;;       yas/selected-text))
+
+(defun yas/get-field-once (number &optional transform-fn)
+  (unless yas/modified-p
+    (if transform-fn
+        (funcall transform-fn (yas/field-value number))
+      (yas/field-value number))))
+
 (defun yas/default-from-field (number)
   (unless yas/modified-p
     (yas/field-value number)))
@@ -2380,6 +2759,12 @@ Otherwise throw exception."
 (defun yas/inside-string ()
   (equal 'font-lock-string-face (get-char-property (1- (point)) 'face)))
 
+(defun yas/unimplemented ()
+  (if yas/current-template
+      (if (y-or-n-p "This snippet is unimplemented. Visit the snippet definition? ")
+          (yas/visit-snippet-file-1 yas/current-template))
+    (message "No implementation.")))
+
 \f
 ;;; Snippet expansion and field management
 
@@ -2433,6 +2818,7 @@ Otherwise throw exception."
   "A mirror."
   start end
   (transform nil)
+  parent-field
   next)
 
 (defstruct (yas/exit (:constructor yas/make-exit (marker)))
@@ -2455,7 +2841,8 @@ for this field, apply it. Otherwise, returned nil."
          (transformed (and transform
                            (save-excursion
                              (goto-char start-point)
-                             (yas/eval-lisp transform)))))
+                             (let ((ret (yas/eval-lisp transform)))
+                               (or ret ""))))))
     transformed))
 
 (defsubst yas/replace-all (from to &optional text)
@@ -2477,8 +2864,7 @@ With optional string TEXT do it in that string."
   "Sort the fields of SNIPPET in navigation order."
   (setf (yas/snippet-fields snippet)
         (sort (yas/snippet-fields snippet)
-              '(lambda (field1 field2)
-                 (yas/snippet-field-compare field1 field2)))))
+              #'yas/snippet-field-compare)))
 
 (defun yas/snippet-field-compare (field1 field2)
   "Compare two fields. The field with a number is sorted first.
@@ -2488,10 +2874,11 @@ have, compare through the field's start point"
         (n2 (yas/field-number field2)))
     (if n1
         (if n2
-            (< n1 n2)
-          t)
+            (or (zerop n2) (and (not (zerop n1))
+                                (< n1 n2)))
+          (not (zerop n1)))
       (if n2
-          nil
+          (zerop n2)
         (< (yas/field-start field1)
            (yas/field-start field2))))))
 
@@ -2571,12 +2958,17 @@ 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
-  (unless (yas/field-modified-p field)
-    (if (yas/field-update-display field snippet)
-        (let ((inhibit-modification-hooks t))
-          (yas/update-mirrors snippet))
-      (setf (yas/field-modified-p field) nil))))
+  (let ((number (yas/field-number field)))
+    (if (and number (zerop number))
+        (progn
+          (set-mark (yas/field-end field))
+          (setf (yas/snippet-force-exit snippet) t))
+      ;; 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))
+              (yas/update-mirrors snippet))
+          (setf (yas/field-modified-p field) nil))))))
 
 (defun yas/prev-field ()
   "Navigate to prev field.  If there's none, exit the snippet."
@@ -2798,9 +3190,24 @@ Otherwise deletes a character normally by calling `delete-char'."
 
 (defun yas/skip-and-clear (field)
   "Deletes the region of FIELD and sets it modified state to t"
-  (setf (yas/field-modified-p field) t)
+  ;; Just before skipping-and-clearing the field, mark its children
+  ;; fields as modified, too. If the childen have mirrors-in-fields
+  ;; this prevents them from updating erroneously (we're skipping and
+  ;; deleting!).
+  ;;
+  (yas/mark-this-and-children-modified field)
   (delete-region (yas/field-start field) (yas/field-end field)))
 
+(defun yas/mark-this-and-children-modified (field)
+  (setf (yas/field-modified-p field) t)
+  (let ((fom (yas/field-next field)))
+    (while (and fom
+                (yas/fom-parent-field fom))
+      (when (and (eq (yas/fom-parent-field fom) field)
+                 (yas/field-p fom))
+        (yas/mark-this-and-children-modified fom))
+      (setq fom (yas/fom-next fom)))))
+
 (defun yas/make-move-active-field-overlay (snippet field)
   "Place the active field overlay in SNIPPET's FIELD.
 
@@ -2830,10 +3237,11 @@ Only clears the field if it hasn't been modified and it point it
 at field start. This hook doesn't do anything if an undo is in
 progress."
   (unless (yas/undo-in-progress)
-    (let ((field (overlay-get yas/active-field-overlay 'yas/field)))
+    (let* ((field (overlay-get yas/active-field-overlay 'yas/field))
+           (number (and field (yas/field-number field)))
+           (snippet (overlay-get yas/active-field-overlay 'yas/snippet)))
       (cond (after?
              (yas/advance-end-maybe field (overlay-end overlay))
-;;; primary field transform: normal calls to expression
              (let ((saved-point (point)))
                (yas/field-update-display field (car (yas/snippets-at-point)))
                (goto-char saved-point))
@@ -2940,9 +3348,12 @@ The error should be ignored in `debug-ignored-errors'"
 ;; they should account for all situations...
 ;;
 
-(defun yas/expand-snippet (template &optional start end expand-env)
-  "Expand snippet at current point. Text between START and END
-will be deleted before inserting template."
+(defun yas/expand-snippet (content &optional start end expand-env)
+  "Expand snippet CONTENT at current point.
+
+Text between START and END will be deleted before inserting
+template. EXPAND-ENV is are let-style variable to value bindings
+considered when expanding the snippet."
   (run-hooks 'yas/before-expand-snippet-hook)
 
   ;; If a region is active, set `yas/selected-text'
@@ -2969,14 +3380,14 @@ will be deleted before inserting template."
       (delete-region start end)
       (setq yas/deleted-text to-delete))
 
-    (cond ((listp template)
+    (cond ((listp content)
            ;; x) This is a snippet-command
-           ;; 
-           (yas/eval-lisp-no-saves template))
+           ;;
+           (yas/eval-lisp-no-saves content))
           (t
            ;; x) This is a snippet-snippet :-)
            ;;
-           ;;    Narrow the region down to the template, shoosh the
+           ;;    Narrow the region down to the content, shoosh the
            ;;    `buffer-undo-list', and create the snippet, the new
            ;;    snippet updates its mirrors once, so we are left with
            ;;    some plain text.  The undo action for deleting this
@@ -2992,12 +3403,13 @@ will be deleted before inserting template."
                ;; them mostly to make the undo information
                ;;
                (setq yas/start-column (save-restriction (widen) (current-column)))
-               (insert template)
 
                (setq snippet
                      (if expand-env
                          (eval `(let ,expand-env
+                                  (insert content)
                                   (yas/snippet-create (point-min) (point-max))))
+                       (insert content)
                        (yas/snippet-create (point-min) (point-max))))))
 
            ;; stacked-expansion: This checks for stacked expansion, save the
@@ -3019,17 +3431,27 @@ will be deleted before inserting template."
            ;; the new snippet (without the "key") followed by an apply of
            ;; `yas/take-care-of-redo' on the newly inserted snippet boundaries
            ;;
-           (let ((start (overlay-start (yas/snippet-control-overlay snippet)))
+           ;; A small exception, if `yas/also-auto-indent-first-line'
+           ;; is t and `yas/indent' decides to indent the line to a
+           ;; point before the actual expansion point, undo would be
+           ;; messed up. We call the early point "newstart"".  case,
+           ;; and attempt to fix undo.
+           ;;
+           (let ((newstart (overlay-start (yas/snippet-control-overlay snippet)))
                  (end (overlay-end (yas/snippet-control-overlay snippet))))
-             (push (cons start end) buffer-undo-list)
+             (when (< newstart start)
+               (push (cons (make-string (- start newstart) ? ) newstart) buffer-undo-list))
+             (push (cons newstart end) buffer-undo-list)
              (push `(apply yas/take-care-of-redo ,start ,end ,snippet)
                    buffer-undo-list))
-           ;; Now, move to the first field
+           ;; Now, schedule a move to the first field
            ;;
            (let ((first-field (car (yas/snippet-fields snippet))))
              (when first-field
-               (yas/move-to-field snippet first-field)))))
-    (message "[yas] snippet expanded.")))
+               (sit-for 0) ;; fix issue 125
+               (yas/move-to-field snippet first-field)))
+           (message "[yas] snippet expanded.")
+           t))))
 
 (defun yas/take-care-of-redo (beg end snippet)
   "Commits SNIPPET, which in turn pushes an undo action for
@@ -3138,6 +3560,14 @@ Returns the newly created snippet."
         (t
          (yas/exit-next fom))))
 
+(defun yas/fom-parent-field (fom)
+  (cond ((yas/field-p fom)
+         (yas/field-parent-field fom))
+        ((yas/mirror-p fom)
+         (yas/mirror-parent-field fom))
+        (t
+         nil)))
+
 (defun yas/calculate-adjacencies (snippet)
   "Calculate adjacencies for fields or mirrors of SNIPPET.
 
@@ -3151,7 +3581,9 @@ has to be called before the $-constructs are deleted."
                                      (t
                                       (setf (yas/exit-next fom) nextfom))))
          (yas/compare-fom-begs (fom1 fom2)
-                               (> (yas/fom-start fom2) (yas/fom-start fom1)))
+                               (if (= (yas/fom-start fom2) (yas/fom-start fom1))
+                                   (yas/mirror-p fom2)
+                                 (>= (yas/fom-start fom2) (yas/fom-start fom1))))
          (yas/link-foms (fom1 fom2)
                         (yas/fom-set-next-fom fom1 fom2)))
     ;; make some yas/field, yas/mirror and yas/exit soup
@@ -3168,6 +3600,23 @@ has to be called before the $-constructs are deleted."
       (when soup
         (reduce #'yas/link-foms soup)))))
 
+(defun yas/calculate-mirrors-in-fields (snippet mirror)
+  "Attempt to assign a parent field of SNIPPET to the mirror MIRROR.
+
+Use the tighest containing field if more than one field contains
+the mirror. Intended to be called *before* the dollar-regions are
+deleted."
+  (let ((min (point-min))
+        (max (point-max)))
+    (dolist (field (yas/snippet-fields snippet))
+      (when (and (<= (yas/field-start field) (yas/mirror-start mirror))
+                 (<= (yas/mirror-end mirror) (yas/field-end field))
+               (< min (yas/field-start field))
+               (< (yas/field-end field) max))
+          (setq min (yas/field-start field)
+                max (yas/field-end field))
+          (setf (yas/mirror-parent-field mirror) field)))))
+
 (defun yas/advance-end-maybe (fom newend)
   "Maybe advance FOM's end to NEWEND if it needs it.
 
@@ -3176,13 +3625,21 @@ If it does, also:
 * call `yas/advance-start-maybe' on FOM's next fom.
 
 * in case FOM is field call `yas/advance-end-maybe' on its parent
-  field"
-  (when (and fom (< (yas/fom-end fom) newend))
-    (set-marker (yas/fom-end fom) newend)
-    (yas/advance-start-maybe (yas/fom-next fom) newend)
-    (if (and (yas/field-p fom)
-             (yas/field-parent-field fom))
-        (yas/advance-end-maybe (yas/field-parent-field fom) newend))))
+  field
+
+Also, if FOM is an exit-marker, always call
+`yas/advance-start-maybe' on its next fom. This is beacuse
+exit-marker have identical start and end markers.
+
+"
+  (cond ((and fom (< (yas/fom-end fom) newend))
+         (set-marker (yas/fom-end fom) newend)
+         (yas/advance-start-maybe (yas/fom-next fom) newend)
+         (let ((parent (yas/fom-parent-field fom)))
+           (when parent
+             (yas/advance-end-maybe parent newend))))
+        ((yas/exit-p fom)
+         (yas/advance-start-maybe (yas/fom-next fom) newend))))
 
 (defun yas/advance-start-maybe (fom newstart)
   "Maybe advance FOM's start to NEWSTART if it needs it.
@@ -3192,6 +3649,13 @@ If it does, also call `yas/advance-end-maybe' on FOM."
     (set-marker (yas/fom-start fom) newstart)
     (yas/advance-end-maybe fom newstart)))
 
+(defun yas/advance-end-of-parents-maybe (field newend)
+  "Like `yas/advance-end-maybe' but for parents."
+  (when (and field
+             (< (yas/field-end field) newend))
+    (set-marker (yas/field-end field) newend)
+    (yas/advance-end-of-parents-maybe (yas/field-parent-field field) newend)))
+
 (defvar yas/dollar-regions nil
   "When expanding the snippet the \"parse-create\" functions add
   cons cells to this var")
@@ -3250,18 +3714,16 @@ Meant to be called in a narrowed buffer, does various passes"
 (defun yas/indent-according-to-mode (snippet-markers)
   "Indent current line according to mode, preserving
 SNIPPET-MARKERS."
-  ;; XXX: Here seems to be the indent problem:
+  ;;; Apropos indenting problems....
   ;;
-  ;; `indent-according-to-mode' uses whatever
-  ;; `indent-line-function' is available. Some
-  ;; implementations of these functions delete text
-  ;; before they insert. If there happens to be a marker
-  ;; just after the text being deleted, the insertion
-  ;; actually happens  after the marker, which misplaces
-  ;; it.
+  ;; `indent-according-to-mode' uses whatever `indent-line-function'
+  ;; is available. Some implementations of these functions delete text
+  ;; before they insert. If there happens to be a marker just after
+  ;; the text being deleted, the insertion actually happens after the
+  ;; marker, which misplaces it.
   ;;
-  ;; This would also happen if we had used overlays with
-  ;; the `front-advance' property set to nil.
+  ;; This would also happen if we had used overlays with the
+  ;; `front-advance' property set to nil.
   ;;
   ;; This is why I have these `trouble-markers', they are the ones at
   ;; they are the ones at the first non-whitespace char at the line
@@ -3416,7 +3878,8 @@ When multiple expressions are found, only the last one counts."
                                    (not (save-match-data
                                           (eq (string-match "$[ \t\n]*("
                                                             (match-string-no-properties 2)) 0)))
-                                   (not (and number (zerop number)))
+                                   ;; allow ${0: some exit text}
+                                   ;; (not (and number (zerop number)))
                                    (yas/make-field number
                                                    (yas/make-marker (match-beginning 2))
                                                    (yas/make-marker (1- real-match-end-0))
@@ -3436,22 +3899,30 @@ When multiple expressions are found, only the last one counts."
   ;; if we entered from a parent field, now search for the
   ;; `yas/multi-dollar-lisp-expression-regexp'. THis is used for
   ;; primary field transformations
-  ;; 
+  ;;
   (when parent-field
     (save-excursion
       (while (re-search-forward yas/multi-dollar-lisp-expression-regexp nil t)
         (let* ((real-match-end-1 (yas/scan-sexps (match-beginning 1) 1)))
-          ;; commit the primary field transformation if we don't find
-          ;; it in yas/dollar-regions (a subnested field) might have
-          ;; already caught it.
+          ;; commit the primary field transformation if:
+          ;;
+          ;; 1. we don't find it in yas/dollar-regions (a subnested
+          ;; field) might have already caught it.
+          ;;
+          ;; 2. we really make sure we have either two '$' or some
+          ;; text and a '$' after the colon ':'. This is a FIXME: work
+          ;; my regular expressions and end these ugly hacks.
+          ;;
           (when (and real-match-end-1
                      (not (member (cons (match-beginning 0)
                                         real-match-end-1)
-                                  yas/dollar-regions)))
+                                  yas/dollar-regions))
+                     (not (eq ?:
+                              (char-before (1- (match-beginning 1))))))
             (let ((lisp-expression-string (buffer-substring-no-properties (match-beginning 1)
                                                                           real-match-end-1)))
               (setf (yas/field-transform parent-field)
-                    (yas/restore-escapes (yas/read-lisp lisp-expression-string))))
+                    (yas/read-lisp (yas/restore-escapes lisp-expression-string))))
             (push (cons (match-beginning 0) real-match-end-1)
                   yas/dollar-regions)))))))
 
@@ -3462,19 +3933,24 @@ When multiple expressions are found, only the last one counts."
            (number (string-to-number (match-string-no-properties 1)))
            (field (and number
                        (not (zerop number))
-                       (yas/snippet-find-field snippet number))))
-      (when (and real-match-end-0
-                 field)
-        (push (yas/make-mirror (yas/make-marker (match-beginning 0))
-                               (yas/make-marker (match-beginning 0))
-                               (yas/read-lisp (yas/restore-escapes
-                                               (buffer-substring-no-properties (match-beginning 2)
-                                                                               (1- real-match-end-0)))))
+                       (yas/snippet-find-field snippet number)))
+           (brand-new-mirror
+            (and real-match-end-0
+                 field
+                 (yas/make-mirror (yas/make-marker (match-beginning 0))
+                                  (yas/make-marker (match-beginning 0))
+                                  (yas/read-lisp
+                                   (yas/restore-escapes
+                                    (buffer-substring-no-properties (match-beginning 2)
+                                                                    (1- real-match-end-0))))))))
+      (when brand-new-mirror
+        (push brand-new-mirror
               (yas/field-mirrors field))
+        (yas/calculate-mirrors-in-fields snippet brand-new-mirror)
         (push (cons (match-beginning 0) real-match-end-0) yas/dollar-regions)))))
 
 (defun yas/simple-mirror-parse-create (snippet)
-  "Parse the simple \"$n\" mirrors and the exit-marker."
+  "Parse the simple \"$n\" fields/mirrors/exitmarkers."
   (while (re-search-forward yas/simple-mirror-regexp nil t)
     (let ((number (string-to-number (match-string-no-properties 1))))
       (cond ((zerop number)
@@ -3496,10 +3972,13 @@ When multiple expressions are found, only the last one counts."
             (t
              (let ((field (yas/snippet-find-field snippet number)))
                (if field
-                   (push (yas/make-mirror (yas/make-marker (match-beginning 0))
-                                          (yas/make-marker (match-beginning 0))
-                                          nil)
-                         (yas/field-mirrors field))
+                   (let ((brand-new-mirror (yas/make-mirror
+                                            (yas/make-marker (match-beginning 0))
+                                            (yas/make-marker (match-beginning 0))
+                                            nil)))
+                     (push brand-new-mirror
+                           (yas/field-mirrors field))
+                     (yas/calculate-mirrors-in-fields snippet brand-new-mirror))
                  (push (yas/make-field number
                                        (yas/make-marker (match-beginning 0))
                                        (yas/make-marker (match-beginning 0))
@@ -3519,25 +3998,41 @@ When multiple expressions are found, only the last one counts."
 (defun yas/update-mirrors (snippet)
   "Updates all the mirrors of SNIPPET."
   (save-excursion
-    (dolist (field (yas/snippet-fields snippet))
-      (dolist (mirror (yas/field-mirrors field))
-        ;; stacked expansion: I added an `inhibit-modification-hooks'
-        ;; here, for safety, may need to remove if we the mechanism is
-        ;; altered.
-        ;;
-        (let ((inhibit-modification-hooks t))
-          (yas/mirror-update-display mirror field)
-          ;; `yas/place-overlays' is needed if the active field and
-          ;; protected overlays have been changed because of insertions
-          ;; in `yas/mirror-update-display'
+    (let* ((fields (copy-list (yas/snippet-fields snippet)))
+           (field (car fields)))
+      (while field
+        (dolist (mirror (yas/field-mirrors field))
+          ;; stacked expansion: I added an `inhibit-modification-hooks'
+          ;; here, for safety, may need to remove if we the mechanism is
+          ;; altered.
           ;;
-          (when (eq field (yas/snippet-active-field snippet))
-            (yas/place-overlays snippet field)))))))
+          (let ((inhibit-modification-hooks t)
+                (mirror-parent-field (yas/mirror-parent-field mirror)))
+            ;; updatte this mirror
+            ;;
+            (yas/mirror-update-display mirror field)
+            ;; for mirrors-in-fields: schedule a possible
+            ;; parent field for reupdting later on
+            ;;
+            (when mirror-parent-field
+              (add-to-list 'fields mirror-parent-field 'append #'eq))
+            ;; `yas/place-overlays' is needed if the active field and
+            ;; protected overlays have been changed because of insertions
+            ;; in `yas/mirror-update-display'
+            ;;
+            (when (eq field (yas/snippet-active-field snippet))
+              (yas/place-overlays snippet field))))
+        (setq fields (cdr fields))
+        (setq field (car fields))))))
 
 (defun yas/mirror-update-display (mirror field)
   "Update MIRROR according to FIELD (and mirror transform)."
-  (let ((reflection (or (yas/apply-transform mirror field)
-                        (yas/field-text-for-display field))))
+
+  (let* ((mirror-parent-field (yas/mirror-parent-field mirror))
+         (reflection (and (not (and mirror-parent-field
+                                    (yas/field-modified-p mirror-parent-field)))
+                          (or (yas/apply-transform mirror field)
+                              (yas/field-text-for-display field)))))
     (when (and reflection
                (not (string= reflection (buffer-substring-no-properties (yas/mirror-start mirror)
                                                                         (yas/mirror-end mirror)))))
@@ -3546,7 +4041,9 @@ When multiple expressions are found, only the last one counts."
       (if (> (yas/mirror-end mirror) (point))
           (delete-region (point) (yas/mirror-end mirror))
         (set-marker (yas/mirror-end mirror) (point))
-        (yas/advance-start-maybe (yas/mirror-next mirror) (point))))))
+        (yas/advance-start-maybe (yas/mirror-next mirror) (point))
+        ;; super-special advance
+        (yas/advance-end-of-parents-maybe mirror-parent-field (point))))))
 
 (defun yas/field-update-display (field snippet)
   "Much like `yas/mirror-update-display', but for fields"
@@ -3568,10 +4065,29 @@ When multiple expressions are found, only the last one counts."
 
 \f
 ;;; Pre- and post-command hooks:
+
+(defvar yas/post-command-runonce-actions nil
+  "List of actions to run once  `post-command-hook'.
+
+Each element of this list looks like (FN . ARGS) where FN is
+called with ARGS as its arguments after the currently executing
+snippet command.
+
+After all actions have been run, this list is emptied, and after
+that the rest of `yas/post-command-handler' runs.")
+
 (defun yas/pre-command-handler () )
 
 (defun yas/post-command-handler ()
   "Handles various yasnippet conditions after each command."
+  (when yas/post-command-runonce-actions
+    (condition-case err
+        (mapc #'(lambda (fn-and-args)
+                  (apply (car fn-and-args)
+                         (cdr fn-and-args)))
+              yas/post-command-runonce-actions)
+      (error (message "[yas] problem running `yas/post-command-runonce-actions'!")))
+    (setq yas/post-command-runonce-actions nil))
   (cond (yas/protection-violation
          (goto-char yas/protection-violation)
          (setq yas/protection-violation nil))
@@ -3590,7 +4106,8 @@ When multiple expressions are found, only the last one counts."
            (when target-field
              (yas/move-to-field snippet target-field))))
         ((not (yas/undo-in-progress))
-         ;; When not in an undo, check if we must commit the snippet (use exited it).
+         ;; When not in an undo, check if we must commit the snippet
+         ;; (user exited it).
          (yas/check-commit-snippet))))
 \f
 ;;; Fancy docs:
@@ -3598,16 +4115,17 @@ When multiple expressions are found, only the last one counts."
 (put 'yas/expand  'function-documentation '(yas/expand-from-trigger-key-doc))
 (defun yas/expand-from-trigger-key-doc ()
   "A doc synthethizer for `yas/expand-from-trigger-key-doc'."
-  (let ((fallback-description (cond ((eq yas/fallback-behavior 'call-other-command)
-                                     (let* ((yas/minor-mode nil)
-                                            (fallback (key-binding (read-kbd-macro yas/trigger-key))))
-                                       (or (and fallback
-                                                (format " call command `%s'." (pp-to-string fallback)))
-                                           " do nothing.")))
-                           ((eq yas/fallback-behavior 'return-nil)
-                            ", do nothing.")
-                           (t
-                            ", defer to `yas/fallback-behaviour' :-)"))))
+  (let ((fallback-description
+         (cond ((eq yas/fallback-behavior 'call-other-command)
+                (let* ((yas/minor-mode nil)
+                       (fallback (key-binding (read-kbd-macro yas/trigger-key))))
+                  (or (and fallback
+                           (format " call command `%s'." (pp-to-string fallback)))
+                      " do nothing.")))
+               ((eq yas/fallback-behavior 'return-nil)
+                ", do nothing.")
+               (t
+                ", defer to `yas/fallback-behaviour' :-)"))))
     (concat "Expand a snippet before point. If no snippet
 expansion is possible,"
             fallback-description
@@ -3617,154 +4135,63 @@ object satisfying `yas/field-p' to restrict the expansion to.")))
 (put 'yas/expand-from-keymap  'function-documentation '(yas/expand-from-keymap-doc))
 (defun yas/expand-from-keymap-doc ()
   "A doc synthethizer for `yas/expand-from-keymap-doc'."
-  (concat "Expand some snippets from keymaps.\n\nMay fall back to original binding."
+  (add-hook 'temp-buffer-show-hook 'yas/snippet-description-finish-runonce)
+  (concat "Expand/run snippets from keymaps, possibly falling back to original binding.\n"
           (when (eq this-command 'describe-key)
             (let* ((vec (this-single-command-keys))
                    (templates (mapcan #'(lambda (table)
                                           (yas/fetch table vec))
                                       (yas/get-snippet-tables)))
-                   (yas/snippet-keymaps nil)
+                   (yas/direct-keymaps nil)
                    (fallback (key-binding vec)))
-              (concat "In this particular case\nmy guess is it would "
+              (concat "In this case, "
                       (when templates
-                        (concat "expand the snippets:\n"
+                        (concat "these snippets are bound to this key:\n"
                                 (yas/template-pretty-list templates)
-                                "\n\nIf no expansion possible, "))
+                                "\n\nIf none of these expands, "))
                       (or (and fallback
-                               (format "call command `%s'." (pp-to-string fallback)))
-                          "do nothing."))))))
+                               (format "fallback `%s' will be called." (pp-to-string fallback)))
+                          "no fallback keybinding is called."))))))
 
 (defun yas/template-pretty-list (templates)
-  (let ((acc))
+  (let ((acc)
+        (yas/buffer-local-condition 'always))
     (dolist (plate templates)
-      (setq acc (concat acc "\n*) " (car plate))))
+      (setq acc (concat acc "\n*) "
+                        (propertize (concat "\\\\snippet `" (car plate) "'")
+                                    'yasnippet (cdr plate)))))
     acc))
 
-\f
-;;; Debug functions.  Use (or change) at will whenever needed.
-;;
-;; some useful debug code for looking up snippet tables
-;;
-;; (insert (pp
-;; (let ((shit))
-;;   (maphash #'(lambda (k v)
-;;             (push k shit))
-;;         (yas/snippet-table-hash (gethash 'ruby-mode yas/snippet-tables)))
-;;   shit)))
-;;
-(defun yas/debug-tables ()
-  (interactive)
-  (with-output-to-temp-buffer "*YASnippet tables*"
-    (dolist (table (yas/get-snippet-tables))
-      (princ (format "Table hash keys for %s:\n\n" (yas/snippet-table-name table))
-      (let ((keys))
-        (maphash #'(lambda (k v)
-                     (push k keys))
-                 (yas/snippet-table-hash table))
-        (dolist (key keys)
-          (princ (format "   key %s maps snippets: %s\n" key
-                         (let ((names))
-                           (maphash #'(lambda (k v)
-                                        (push k names))
-                                    (gethash key (yas/snippet-table-hash table)))
-                           names)))))))))
-
-(defun yas/debug-snippet-vars ()
-  "Debug snippets, fields, mirrors and the `buffer-undo-list'."
-  (interactive)
-  (with-output-to-temp-buffer "*YASnippet trace*"
-    (princ "Interesting YASnippet vars: \n\n")
-
-    (princ (format "\nPost command hook: %s\n" post-command-hook))
-    (princ (format "\nPre  command hook: %s\n" pre-command-hook))
-
-    (princ (format "%s live snippets in total\n" (length (yas/snippets-at-point (quote all-snippets)))))
-    (princ (format "%s overlays in buffer:\n\n" (length (overlays-in (point-min) (point-max)))))
-    (princ (format "%s live snippets at point:\n\n" (length (yas/snippets-at-point))))
-
-
-    (dolist (snippet (yas/snippets-at-point))
-      (princ (format "\tsid: %d control overlay from %d to %d\n"
-                     (yas/snippet-id snippet)
-                     (overlay-start (yas/snippet-control-overlay snippet))
-                     (overlay-end (yas/snippet-control-overlay snippet))))
-      (princ (format "\tactive field: %d from %s to %s covering \"%s\"\n"
-                     (yas/field-number (yas/snippet-active-field snippet))
-                     (marker-position (yas/field-start (yas/snippet-active-field snippet)))
-                     (marker-position (yas/field-end (yas/snippet-active-field snippet)))
-                     (buffer-substring-no-properties (yas/field-start (yas/snippet-active-field snippet)) (yas/field-end (yas/snippet-active-field snippet)))))
-      (when (yas/snippet-exit snippet)
-        (princ (format "\tsnippet-exit: at %s next: %s\n"
-                       (yas/exit-marker (yas/snippet-exit snippet))
-                       (yas/exit-next (yas/snippet-exit snippet)))))
-      (dolist (field (yas/snippet-fields snippet))
-        (princ (format "\tfield: %d from %s to %s covering \"%s\" next: %s\n"
-                       (yas/field-number field)
-                       (marker-position (yas/field-start field))
-                       (marker-position (yas/field-end field))
-                       (buffer-substring-no-properties (yas/field-start field) (yas/field-end field))
-                       (yas/debug-format-fom-concise (yas/field-next field))))
-        (dolist (mirror (yas/field-mirrors field))
-          (princ (format "\t\tmirror: from %s to %s covering \"%s\" next: %s\n"
-                         (marker-position (yas/mirror-start mirror))
-                         (marker-position (yas/mirror-end mirror))
-                         (buffer-substring-no-properties (yas/mirror-start mirror) (yas/mirror-end mirror))
-                         (yas/debug-format-fom-concise (yas/mirror-next mirror)))))))
-
-    (princ (format "\nUndo is %s and point-max is %s.\n"
-                   (if (eq buffer-undo-list t)
-                       "DISABLED"
-                     "ENABLED")
-                   (point-max)))
-    (unless (eq buffer-undo-list t)
-      (princ (format "Undpolist has %s elements. First 10 elements follow:\n" (length buffer-undo-list)))
-      (let ((first-ten (subseq buffer-undo-list 0 19)))
-        (dolist (undo-elem first-ten)
-          (princ (format "%2s:  %s\n" (position undo-elem first-ten) (truncate-string-to-width (format "%s" undo-elem) 70))))))))
-
-(defun yas/debug-format-fom-concise (fom)
-  (when fom
-    (cond ((yas/field-p fom)
-           (format "field %d from %d to %d"
-                   (yas/field-number fom)
-                   (marker-position (yas/field-start fom))
-                   (marker-position (yas/field-end fom))))
-          ((yas/mirror-p fom)
-           (format "mirror from %d to %d"
-                   (marker-position (yas/mirror-start fom))
-                   (marker-position (yas/mirror-end fom))))
-          (t
-           (format "snippet exit at %d"
-                   (marker-position (yas/fom-start fom)))))))
-
+(define-button-type 'help-snippet-def
+  :supertype 'help-xref
+  'help-function (lambda (template) (yas/visit-snippet-file-1 template))
+  'help-echo (purecopy "mouse-2, RET: find snippets's definition"))
 
-(defun yas/exterminate-package ()
-  (interactive)
-  (yas/global-mode -1)
-  (yas/minor-mode -1)
-  (mapatoms #'(lambda (atom)
-                (when (string-match "yas/" (symbol-name atom))
-                  (unintern atom)))))
-
-(defun yas/debug-test (&optional quiet)
-  (interactive "P")
-  (yas/load-directory (or (and (listp yas/root-directory)
-                               (first yas/root-directory))
-                          yas/root-directory
-                          "~/Source/yasnippet/snippets/"))
-  (set-buffer (switch-to-buffer "*YAS TEST*"))
-  (mapc #'yas/commit-snippet (yas/snippets-at-point 'all-snippets))
-  (erase-buffer)
-  (setq buffer-undo-list nil)
-  (setq undo-in-progress nil)
-  (snippet-mode)
-  (yas/minor-mode 1)
-  (let ((abbrev))
-    (setq abbrev "$f")
-    (insert abbrev))
-  (unless quiet
-    (add-hook 'post-command-hook 'yas/debug-snippet-vars 't 'local)))
+(defun yas/snippet-description-finish-runonce ()
+  "Final adjustments for the help buffer when snippets are concerned."
+  (yas/create-snippet-xrefs)
+  (remove-hook 'temp-buffer-show-hook 'yas/snippet-description-finish-runonce))
 
+(defun yas/create-snippet-xrefs ()
+  (save-excursion
+    (goto-char (point-min))
+    (while (search-forward-regexp "\\\\\\\\snippet[ \s\t]+`\\([^']+\\)'" nil t)
+      (let ((template (get-text-property (match-beginning 1)
+                                         'yasnippet)))
+        (when template
+          (help-xref-button 1 'help-snippet-def template)
+          (kill-region (match-end 1) (match-end 0))
+          (kill-region (match-beginning 0) (match-beginning 1)))))))
+
+(defun yas/expand-uuid (mode-symbol uuid &optional start end expand-env)
+  "Expand a snippet registered in MODE-SYMBOL's table with UUID.
+
+Remaining args as in `yas/expand-snippet'."
+  (let* ((table (gethash mode-symbol yas/tables))
+         (yas/current-template (and table
+                                    (gethash uuid (yas/table-uuidhash table)))))
+    (when yas/current-template
+      (yas/expand-snippet (yas/template-content yas/current-template)))))
 
 \f
 ;;; Some hacks:
@@ -3796,7 +4223,7 @@ and return the directory.  Return nil if not found."
           (prev-file file)
           ;; `user' is not initialized outside the loop because
           ;; `file' may not exist, so we may have to walk up part of the
-          ;; hierarchy before we find the "initial UID".
+          ;; hierarchy before we find the "initial UUID".
           (user nil)
           try)
       (while (not (or root
@@ -3851,6 +4278,3 @@ handle the end-of-buffer error fired in it by calling
 (provide 'yasnippet)
 
 ;;; yasnippet.el ends here
-
-
-