]> code.delx.au - gnu-emacs-elpa/blobdiff - yasnippet.el
Attempt to fix a bug with `yas/snippet-dirs` initialization.
[gnu-emacs-elpa] / yasnippet.el
index 5656708afa9ece16be26209b0daacfcc809656ef..7602dbe4d7da2f2462be8c60537dc8de351334b1 100644 (file)
@@ -44,7 +44,7 @@
 ;;
 ;;   Steps 4. and 5. are optional, you don't have to use the minor
 ;;   mode to use YASnippet.
-;; 
+;;
 ;;   Interesting variables are:
 ;;
 ;;       `yas/snippet-dirs'
 ;;           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.
+;;           directory is taken as the default for storing the user's
+;;           new snippets.
+;;
+;;           The deprecated `yas/root-directory' aliases this variable
+;;           for backward-compatibility.
 ;;
-;;       `yas/mode-symbol'
+;;       `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
@@ -64,6 +68,9 @@
 ;;           `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
 ;;           sets it to the appropriate major mode and inserts the
 ;;           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
   "Yet Another Snippet extension"
   :group 'editing)
 
-;;;###autoload
-(defcustom yas/snippet-dirs nil
+(defvar yas/load-file-name load-file-name
+  "Store the filename that yasnippet.el was originally loaded from.")
+(defcustom yas/snippet-dirs (remove nil
+                                    (list "~/.emacs.d/snippets"
+                                          (when yas/load-file-name
+                                            (concat (file-name-directory yas/load-file-name) "snippets"))))
   "Directory or list of snippet dirs for each major mode.
 
 The directory where user-created snippets are to be stored. Can
@@ -178,6 +189,8 @@ as the default for storing the user's new snippets."
              (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
@@ -255,7 +268,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.
 
@@ -270,7 +283,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.
@@ -287,7 +300,7 @@ Can also be a list of strings."
            (if (fboundp 'yas/init-yas-in-snippet-keymap)
                (yas/init-yas-in-snippet-keymap))))
 
-(defcustom yas/skip-and-clear-key "C-d"
+(defcustom yas/skip-and-clear-key '("C-d" "<delete>" "<deletechar>")
   "The key to clear the currently active field.
 
 Value is a string that is converted to the internal Emacs key
@@ -325,7 +338,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 +358,7 @@ This affects `yas/insert-snippet', `yas/visit-snippet-file'"
   :type 'boolean
   :group 'yasnippet)
 
-(defcustom yas/use-menu 'real-modes
+(defcustom yas/use-menu 'abbreviate
   "Display a YASnippet menu in the menu bar.
 
 When non-nil, submenus for each snippet table will be listed
@@ -356,7 +368,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)
@@ -387,7 +399,7 @@ An error string \"[yas] error\" is returned instead."
 (defcustom yas/ignore-filenames-as-triggers nil
   "If non-nil, don't derive tab triggers from filenames.
 
-This means a snippet without a \"# key:'\ directive wont have a
+This means a snippet without a \"# key:'\ directive won't have a
 tab trigger."
   :type 'boolean
   :group 'yasnippet)
@@ -399,9 +411,24 @@ This cafn only work when snippets are loaded from files."
   :type 'boolean
   :group 'yasnippet)
 
+(defcustom yas/expand-only-for-last-commands nil
+  "List of `last-command' values to restrict tab-triggering to, or nil.
+
+Leave this set at nil (the default) to be able to trigger an
+expansion simply by placing the cursor after a valid tab trigger,
+using whichever commands.
+
+Optionallly, set this to something like '(self-insert-command) if
+you to wish restrict expansion to only happen when the last
+letter of the snippet tab trigger was typed immediately before
+the trigger key itself."
+  :type '(repeat function)
+  :group 'yasnippet)
+
+;; Only two faces, and one of them shouldn't even be used...
+;;
 (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)
 
@@ -430,14 +457,20 @@ 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_.()" "^ ")
-  "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 \"_\":
+(defvar yas/key-syntaxes (list "w" "w_" "w_." "w_.()" "^ ")
+  "List of character syntaxes used to find a trigger key before point.
+The list is tried in the order while scanning characters
+backwards from point. For example, if the list is '(\"w\" \"w_\")
+first look for trigger keys which are composed exclusively of
+\"word\"-syntax characters, and then, if that fails, look for
+keys which are either of \"word\" or \"symbol\"
+syntax. Triggering after
 
 foo-bar
 
-will first try \"bar\", if not found, then \"foo-bar\" is tried.")
+will, according to the \"w\" element first try \"bar\". If that
+isn't a trigger key, \"foo-bar\" is tried, respecting a second
+\"w_\" element.")
 
 (defvar yas/after-exit-snippet-hook
   '()
@@ -457,8 +490,9 @@ 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 (or (fourth (syntax-ppss))
-           (fifth (syntax-ppss)))
+  '(if (and (or (fourth (syntax-ppss))
+                (fifth (syntax-ppss)))
+            (eq (symbol-function this-command) 'yas/expand-from-trigger-key))
        '(require-snippet-condition . force-in-comment)
      t)
   "Snippet expanding condition.
@@ -492,7 +526,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.
 
@@ -512,7 +546,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
@@ -522,16 +555,12 @@ snippet itself contains a condition that returns the symbol
 (defvar yas/menu-table (make-hash-table)
   "A hash table of MAJOR-MODE symbols to menu keymaps.")
 
-(defun teste ()
-  (interactive)
-  (message "AHAHA!"))
-
 (defvar yas/known-modes
   '(ruby-mode rst-mode markdown-mode)
   "A list of mode which is well known but not part of emacs.")
 
 (defvar yas/escaped-characters
-  '(?\\ ?` ?' ?$ ?} )
+  '(?\\ ?` ?' ?$ ?} ?{ ?\( ?\))
   "List of characters which *might* need to be escaped.")
 
 (defconst yas/field-regexp
@@ -587,7 +616,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)
@@ -654,27 +683,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"
@@ -706,7 +735,7 @@ 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 "")))
@@ -729,7 +758,7 @@ all defined direct keybindings to the command
 (defun yas/direct-keymaps-reload ()
   "Force reload the direct keybinding for active snippet tables."
   (interactive)
-  (setq yas/direct-keymaps nil) 
+  (setq yas/direct-keymaps nil)
   (maphash #'(lambda (name table)
                (mapc #'(lambda (table)
                          (push (cons (intern (format "yas//direct-%s" name))
@@ -743,8 +772,8 @@ all defined direct keybindings to the command
         (mode major-mode))
     (while (setq mode (get mode 'derived-mode-parent))
       (push mode modes-to-activate))
-    (when yas/mode-symbol
-      (push yas/mode-symbol 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)
@@ -776,48 +805,49 @@ Key bindings:
          ;; Reload the trigger key
          ;;
          (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/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/direct-keymaps'.
          ;;
+         ;; Also install the post-command-hook.
+         ;;
          (add-hook 'emulation-mode-map-alists 'yas/direct-keymaps)
+         (add-hook 'post-command-hook 'yas/post-command-handler nil t)
          (add-hook 'yas/minor-mode-hook 'yas/direct-keymaps-set-vars-runonce 'append))
         (t
-         ;; Uninstall the direct keymaps.
-         ;; 
+         ;; Uninstall the direct keymaps and the post-command hook
+         ;;
+         (remove-hook 'post-command-hook 'yas/post-command-handler t)
          (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/snippet-dirs
-                                   (null (yas/get-snippet-tables))))
+(defvar yas/dont-activate nil
   "If non-nil don't let `yas/minor-mode-on' active yas for this buffer.
 
 `yas/minor-mode-on' is usually called by `yas/global-mode' so
 this effectively lets you define exceptions to the \"global\"
-behaviour.")
+behaviour. Can also be a function of zero arguments.")
 (make-variable-buffer-local 'yas/dont-activate)
 
 (defun yas/minor-mode-on ()
   "Turn on YASnippet minor mode.
 
-Do this unless `yas/dont-activate' is t or the function
-`yas/get-snippet-tables' (which see), returns an empty list."
+Do this unless `yas/dont-activate' is truish "
   (interactive)
-  (unless (or (and (functionp yas/dont-activate)
-                   (funcall yas/dont-activate))
-              (and (not (functionp yas/dont-activate))
-                   yas/dont-activate))
+  (unless (or (minibufferp)
+              (if (functionp yas/dont-activate)
+                  (funcall yas/dont-activate)
+                yas/dont-activate))
+    ;; Load all snippets definitions unless we still don't have a
+    ;; root-directory or some snippets have already been loaded.
+    ;;
+    (unless (or (null yas/snippet-dirs)
+                (> (hash-table-count yas/tables) 0))
+      (yas/reload-all))
     (yas/minor-mode 1)))
 
 (defun yas/minor-mode-off ()
@@ -825,6 +855,7 @@ Do this unless `yas/dont-activate' is t or the function
   (interactive)
   (yas/minor-mode -1))
 
+;;;###autoload
 (define-globalized-minor-mode yas/global-mode yas/minor-mode yas/minor-mode-on
   :group 'yasnippet
   :require 'yasnippet)
@@ -890,9 +921,11 @@ Do this unless `yas/dont-activate' is t or the function
   expand-env
   file
   keybinding
-  uid
-  menu-binding-pair)
-
+  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"
@@ -914,16 +947,15 @@ Has the following fields:
 
   A symbol name normally corresponding to a major mode, but can
   also be a pseudo major-mode to be referenced in
-  `yas/mode-symbol', for example.
+  `yas/extra-modes', for example.
 
 `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 it's 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/table-parents'
 
@@ -937,27 +969,27 @@ Has the following fields:
   the elements of the keyhash that are vectors appear here as
   bindings to `yas/expand-from-keymap'.
 
-`yas/table-uidhash'
+`yas/table-uuidhash'
 
-  A hash table mapping snippets uid's to the same `yas/template'
-  objects. A snippet uid defaults to the snippet's name.
+  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))
-  (uidhash (make-hash-table :test 'equal))
+  (uuidhash (make-hash-table :test 'equal))
   (parents nil)
   (direct-keymap (make-sparse-keymap)))
 
-(defun yas/get-template-by-uid (mode uid)
-  "Find the snippet template in MODE by its UID."
+(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 uid (yas/table-uidhash table)))))
+      (gethash uuid (yas/table-uuidhash table)))))
 
 ;; Apropos storing/updating, this works with two steps:
 ;;
-;; 1. `yas/remove-template-by-uid' to remove any existing mappings by
-;;    snippet uid
+;; 1. `yas/remove-template-by-uuid' to remove any existing mappings by
+;;    snippet uuid
 ;;
 ;; 2. `yas/add-template' to add the mappings again:
 ;;
@@ -966,9 +998,9 @@ Has the following fields:
 ;;    TEMPLATE, and is also created a new namehash inside that
 ;;    entry.
 ;;
-(defun yas/remove-template-by-uid (table uid)
-  "Remove from TABLE a template identified by UID."
-  (let ((template (gethash uid (yas/table-uidhash table))))
+(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))
@@ -977,7 +1009,7 @@ Has the following fields:
         (maphash #'(lambda (k v)
                      (let ((template (gethash name v)))
                        (when (and template
-                                  (eq uid (yas/template-uid template)))
+                                  (eq uuid (yas/template-uuid template)))
                          (remhash name v)
                          (when (zerop (hash-table-count v))
                            (push k empty-keys)))))
@@ -987,9 +1019,9 @@ Has the following fields:
         (dolist (key empty-keys)
           (remhash key (yas/table-hash table)))
 
-        ;; Finally, remove the uid from the uidhash
+        ;; Finally, remove the uuid from the uuidhash
         ;;
-        (remhash uid (yas/table-uidhash table))))))
+        (remhash uuid (yas/table-uuidhash table))))))
 
 
 (defun yas/add-template (table template)
@@ -1018,26 +1050,62 @@ keybinding)."
                 (and key (concat key yas/trigger-symbol))))
       (setcar (cdr menu-binding)
               name))
-    
-    (puthash (yas/template-uid template) template (yas/table-uidhash table))))
+
+    (puthash (yas/template-uuid template) template (yas/table-uuidhash table))))
 
 (defun yas/update-template (snippet-table template)
-  "Add or update TEMPLATE in SNIPPET-TABLE"
+  "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
+  ;;
+  (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))
 
-  (yas/remove-template-by-uid snippet-table (yas/template-uid template))
-  (yas/add-template snippet-table 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/namehash-templates-alist (namehash)
+  (let (alist)
+    (maphash #'(lambda (k v)
+                 (push (cons k v) alist))
+             namehash)
+    alist))
 
 (defun yas/fetch (table key)
-  "Fetch snippets in TABLE by KEY. "
+  "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
-       (let (alist)
-         (maphash #'(lambda (k v)
-                      (push (cons k v) alist))
-                  namehash)
-         alist)))))
+      (yas/filter-templates-by-condition (yas/namehash-templates-alist namehash)))))
 
 \f
 ;;; Filtering/condition logic
@@ -1066,7 +1134,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 ()
@@ -1095,7 +1164,7 @@ conditions to filter out potential expansions."
            (eq requirement result)))))
 
 (defun yas/table-get-all-parents (table)
-  "Returns a list of all parent tables of TABLE" 
+  "Returns a list of all parent tables of TABLE"
   (let ((parents (yas/table-parents table)))
     (when parents
       (append (copy-list parents)
@@ -1141,8 +1210,8 @@ the template of a snippet in the current snippet-table."
 (defun yas/table-all-keys (table)
   (when table
     (let ((acc))
-      (maphash #'(lambda (key templates)
-                   (when (yas/filter-templates-by-condition templates)
+      (maphash #'(lambda (key namehash)
+                   (when (yas/filter-templates-by-condition (yas/namehash-templates-alist namehash))
                      (push key acc)))
                (yas/table-hash table))
       acc)))
@@ -1200,21 +1269,27 @@ return an expression that when evaluated will issue an error."
 (defun yas/read-keybinding (keybinding)
   "Read KEYBINDING as a snippet keybinding, return a vector."
   (when (and keybinding
-             (not (string-match "keybinding" 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))
+        (let ((res (or (and (string-match "^\\[.*\\]$" keybinding)
+                            (read keybinding))
+                       (read-kbd-macro keybinding 'need-vector))))
+          res)
       (error
-       (message "[yas] warning: keybinding \"%s\" invalid for snippet \"%s\" since %s."
-                keybinding name (error-message-string err))
+       (message "[yas] warning: keybinding \"%s\" invalid since %s."
+                keybinding (error-message-string err))
        nil))))
 
-(defvar yas/mode-symbol nil
-  "If non-nil, lookup snippets using this instead of `major-mode'.")
-(make-variable-buffer-local 'yas/mode-symbol)
+(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.
@@ -1237,28 +1312,25 @@ already have such a property."
 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/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/table-get-all-parents table)))
-    (remove-duplicates all-tables)))
+         (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.
@@ -1269,20 +1341,16 @@ them in all `yas/menu-table'"
          (menu-keymap (or (gethash mode yas/menu-table)
                           (puthash mode (make-sparse-keymap) yas/menu-table)))
         (parents (yas/table-parents table)))
-    (mapc #'(lambda (parent)
-              (define-key menu-keymap
-                (vector (intern (concat "parent_shit_" (yas/table-name parent))))
-                (list 'menu-item
-                      (concat "parent-table: "
-                              (yas/table-name parent))
-                      (yas/menu-keymap-get-create parent))))
-          parents)
+    (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
 
-(defun yas/parse-template (&optional file group)
+(defun yas/parse-template (&optional file)
   "Parse the template in the current buffer.
 
 Optional FILE is the absolute file name of the file being
@@ -1293,30 +1361,24 @@ otherwise we attempt to calculate it from FILE.
 
 Return a snippet-definition, i.e. a list
 
- (KEY TEMPLATE NAME CONDITION GROUP VARS FILE KEYBINDING UID)
+ (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
+ * group
  * key
  * expand-env
  * binding
- * uid
-
-#name: #include \"...\"
-# --
-#include \"$1\""
-  ;;
-  ;;
+ * uuid"
   (goto-char (point-min))
   (let* ((type 'snippet)
          (name (and file
@@ -1327,12 +1389,11 @@ Here's a list of currently recognized variables:
          template
          bound
          condition
-         (group (or group
-                    (and file
-                         (yas/calculate-group file))))
+         (group (and file
+                     (yas/calculate-group file)))
          expand-env
          binding
-         uid)
+         uuid)
     (if (re-search-forward "^# --\n" nil t)
         (progn (setq template
                      (buffer-substring-no-properties (point)
@@ -1340,8 +1401,8 @@ Here's a list of currently recognized variables:
                (setq bound (point))
                (goto-char (point-min))
                (while (re-search-forward "^# *\\([^ ]+?\\) *: *\\(.*\\)$" bound t)
-                 (when (string= "uid" (match-string-no-properties 1))
-                   (setq uid (match-string-no-properties 2)))
+                 (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
@@ -1353,7 +1414,7 @@ Here's a list of currently recognized variables:
                  (when (string= "condition" (match-string-no-properties 1))
                    (setq condition (yas/read-lisp (match-string-no-properties 2))))
                  (when (string= "group" (match-string-no-properties 1))
-                   (message "[yas] Warning: the \"# group:\" is no longer supported!"))
+                   (setq group (match-string-no-properties 2)))
                  (when (string= "expand-env" (match-string-no-properties 1))
                    (setq expand-env (yas/read-lisp (match-string-no-properties 2)
                                                    'nil-on-error)))
@@ -1363,7 +1424,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 uid)))
+    (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."
@@ -1387,7 +1450,7 @@ Here's a list of currently recognized variables:
   (remove-if (lambda (file)
                (or (string-match "^\\."
                                  (file-name-nondirectory file))
-                   (string-match "^#"
+                   (string-match "^#.*#$"
                                  (file-name-nondirectory file))
                    (string-match "~$"
                                  (file-name-nondirectory file))
@@ -1397,22 +1460,23 @@ Here's a list of currently recognized variables:
              (directory-files directory t)))
 
 (defun yas/make-menu-binding (template)
-  (let ((mode (intern (yas/table-name (yas/template-table template))))) 
-    `(lambda () (interactive) (yas/expand-or-visit-from-menu ',mode ,(yas/template-uid template)))))
+  (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 uid)
+(defun yas/expand-or-visit-from-menu (mode uuid)
   (let* ((table (yas/table-get-create mode))
-         (template (and table
-                        (gethash uid (yas/table-uidhash table)))))
-    (when template
+         (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 template)
-        (let ((where (if mark-active
+          (yas/visit-snippet-file-1 yas/current-template)
+        (let ((where (if (region-active-p)
                          (cons (region-beginning) (region-end))
                        (cons (point) (point)))))
-          (yas/expand-snippet (yas/template-content template)
+          (yas/expand-snippet (yas/template-content yas/current-template)
                               (car where)
-                              (cdr where)))))))
+                              (cdr where)
+                              (yas/template-expand-env yas/current-template)))))))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Popping up for keys and templates
@@ -1422,11 +1486,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."
@@ -1444,67 +1514,110 @@ TEMPLATES is a list of `yas/template'."
           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 ((pretty-alist (list))
+        menu
+        more-than-one-table
+        prefix)
+    (dolist (tl templates)
+      (aput 'pretty-alist (yas/template-table tl) (cons tl (aget pretty-alist (yas/template-table tl)))))
+    (setq more-than-one-table (> (length pretty-alist) 1))
+    (setq prefix (if more-than-one-table
+                     "   " ""))
+    (dolist (table-and-templates pretty-alist)
+      (when (cdr table-and-templates)
+        (if more-than-one-table
+            (push (yas/table-name (car table-and-templates)) menu))
+        (dolist (template (cdr table-and-templates))
+          (push (cons (concat prefix (yas/template-name template))
+                      template) menu))))
+    (setq menu (nreverse menu))
+    (or (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)))
+        (keyboard-quit))))
 
 (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)))))
+  (when (featurep 'ido)
+    (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
-                        (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))))
+    (let (formatted-choices
+          filtered-choices
+          d
+          n)
+      (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 n (and formatted-choices (dropdown-list formatted-choices)))
+      (if n
+          (nth n filtered-choices)
+        (keyboard-quit)))))
+
+(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))
@@ -1527,24 +1640,29 @@ TEMPLATES is a list of `yas/template'."
   (unless (file-exists-p (concat directory "/" ".yas-skip"))
     (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"))))
+                                     (yas/compute-major-mode-and-parents (concat directory
+                                                                                 "/dummy"))))
+           (default-directory directory)
+           (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))))
-      (when snippet-defs
+      (when (or snippet-defs
+                (cdr major-mode-and-parents))
         (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))
         (yas/load-directory-1 subdir
                               (car major-mode-and-parents)
@@ -1559,7 +1677,7 @@ of a snippet.  The file name is the trigger key and the
 content of the file is the template."
   (interactive "DSelect the root directory: ")
   (unless (file-directory-p directory)
-    (error "Error %s not a directory" directory))
+    (error "%s is not a directory" directory))
   (unless yas/snippet-dirs
     (setq yas/snippet-dirs directory))
   (dolist (dir (yas/subdirs directory))
@@ -1567,25 +1685,20 @@ content of the file is the template."
   (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." 
+(defun yas/load-snippet-dirs ()
+  "Reload the directories listed in `yas/snippet-dirs' or
+   prompt the user to select one."
   (if yas/snippet-dirs
-      (if (listp yas/snippet-dirs) 
-          (dolist (directory (reverse yas/snippet-dirs)) 
-            (yas/load-directory directory)) 
-        (yas/load-directory yas/snippet-dirs)) 
-    (call-interactively 'yas/load-directory))) 
-(defun yas/reload-all (&optional reset-root-directory)
+      (dolist (directory (reverse (yas/snippet-dirs)))
+        (yas/load-directory directory))
+    (call-interactively 'yas/load-directory)))
+
+(defun yas/reload-all (&optional interactive)
   "Reload all snippets and rebuild the YASnippet menu. "
-  (interactive "P")
+  (interactive "p")
   ;; Turn off global modes and minor modes, save their state though
   ;;
-  (let ((restore-global-mode (prog1 yas/global-mode
-                               (yas/global-mode -1)))
-        (restore-minor-mode (prog1 yas/minor-mode
-                              (yas/minor-mode -1))))
+  (let ((errors))
     ;; Empty all snippet tables and all menu tables
     ;;
     (setq yas/tables (make-hash-table))
@@ -1597,29 +1710,17 @@ content of the file is the template."
     (setf (cdr yas/minor-mode-map)
           (cdr (yas/init-minor-keymap)))
 
-    (when reset-root-directory
-      (setq yas/snippet-dirs nil))
-
     ;; Reload the directories listed in `yas/snippet-dirs' or prompt
     ;; the user to select one.
     ;;
-    (if yas/snippet-dirs
-        (if (listp yas/snippet-dirs)
-            (dolist (directory (reverse yas/snippet-dirs))
-              (yas/load-directory directory))
-          (yas/load-directory yas/snippet-dirs))
-      (call-interactively 'yas/load-directory))
+    (condition-case oops
+        (yas/load-snippet-dirs)
+      (error (push oops errors)
+             (message "[yas] Check your `yas/snippet-dirs': %s" (second oops))))
     ;; Reload the direct keybindings
     ;;
     (yas/direct-keymaps-reload)
-    ;; Restore the mode configuration
-    ;;
-    (when restore-minor-mode
-      (yas/minor-mode 1))
-    (when restore-global-mode
-      (yas/global-mode 1))
-
-    (message "[yas] Reloading everything... Done.")))
+    (message "[yas] Reloaded everything...%s." (if errors " (some errors, check *Messages*)" ""))))
 
 (defun yas/quote-string (string)
   "Escape and quote STRING.
@@ -1664,8 +1765,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
@@ -1699,7 +1801,7 @@ Here's the default value for all the parameters:
               "  \"Initialize YASnippet and load snippets in the bundle.\"")
       (flet ((yas/define-snippets
               (mode snippets &optional parent-or-parents)
-              (insert ";;; snippets for " (symbol-name mode) "\n")
+              (insert ";;; snippets for " (symbol-name mode) ", subdir " (file-name-nondirectory (replace-regexp-in-string "/$" "" default-directory)) "\n")
               (let ((literal-snippets (list)))
                 (dolist (snippet snippets)
                   (let ((key                    (first   snippet))
@@ -1708,9 +1810,9 @@ Here's the default value for all the parameters:
                         (condition              (fourth  snippet))
                         (group                  (fifth   snippet))
                         (expand-env             (sixth   snippet))
-                        (file                   nil) ;; (seventh snippet)) ;; omit on purpose 
+                        (file                   nil) ;; (seventh snippet)) ;; omit on purpose
                         (binding                (eighth  snippet))
-                        (uid                    (ninth   snippet)))
+                        (uuid                    (ninth   snippet)))
                     (push `(,key
                             ,template-content
                             ,name
@@ -1719,7 +1821,7 @@ Here's the default value for all the parameters:
                             ,expand-env
                             ,file
                             ,binding
-                            ,uid)
+                            ,uuid)
                           literal-snippets)))
                 (insert (pp-to-string `(yas/define-snippets ',mode ',literal-snippets ',parent-or-parents)))
                 (insert "\n\n"))))
@@ -1727,9 +1829,13 @@ Here's the default value for all the parameters:
           (dolist (subdir (yas/subdirs dir))
             (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))))))
+                (insert "\n;; Supporting elisp for subdir " (file-name-nondirectory subdir) "\n\n")
+                (with-temp-buffer
+                  (insert-file-contents file)
+                  (replace-regexp "^;;.*$" "" nil (point-min) (point-max))
+                  (replace-regexp "^[\s\t]*\n\\([\s\t]*\n\\)+" "\n" nil (point-min) (point-max))
+                  (kill-region (point-min) (point-max)))
+                (yank)))
             (yas/load-directory-1 subdir nil))))
 
       (insert (pp-to-string `(yas/global-mode 1)))
@@ -1738,14 +1844,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/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"))))
@@ -1776,7 +1882,7 @@ Here's the default value for all the parameters:
 SNIPPETS is a list of snippet definitions, each taking the
 following form
 
- (KEY TEMPLATE NAME CONDITION GROUP EXPAND-ENV FILE KEYBINDING UID)
+ (KEY TEMPLATE NAME CONDITION GROUP EXPAND-ENV FILE KEYBINDING UUID)
 
 Within these, only KEY and TEMPLATE are actually mandatory.
 
@@ -1792,8 +1898,8 @@ The remaining elements are strings.
 FILE is probably of very little use if you're programatically
 defining snippets.
 
-UID is the snippets \"unique-id\". Loading a second snippet file
-with the same uid replaced the previous snippet.
+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.
@@ -1809,7 +1915,7 @@ not need to be a real mode."
                                (if (listp parent-mode)
                                    parent-mode
                                  (list parent-mode))))
-        (menu-keymap nil))
+        (template nil))
     ;; X) Connect `snippet-table' with `parent-tables'.
     ;;
     ;; TODO: this should be a remove-duplicates of the concatenation
@@ -1817,24 +1923,17 @@ not need to be a real mode."
     ;;
     (dolist (parent parent-tables)
       (unless (find parent (yas/table-parents snippet-table))
-        (push (yas/table-parents snippet-table)
-              parent)))
-    
-    ;; X) The keymap created here here is the menu keymap, it is also
-    ;;    gotten/created according to MODE.  Make a menu entry for
-    ;;    mode
-    ;; 
-    (when yas/use-menu
-      (setq menu-keymap (yas/menu-keymap-get-create snippet-table))
-      (define-key yas/minor-mode-menu (vector mode)
-        `(menu-item ,(symbol-name mode) ,menu-keymap
-                    :visible (yas/show-menu-p ',mode))))
+        (push parent
+              (yas/table-parents snippet-table))))
+
     ;; X) Now, iterate for evey snippet def list
     ;;
     (dolist (snippet snippets)
-      (yas/define-snippets-1 mode snippet snippet-table menu-keymap))))
+      (setq template (yas/define-snippets-1 snippet
+                                            snippet-table)))
+    template))
 
-(defun yas/define-snippets-1 (mode snippet snippet-table menu-keymap)
+(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'.
@@ -1842,7 +1941,7 @@ not need to be a real mode."
   (let* ((file (seventh snippet))
          (key (or (car snippet)
                   (unless yas/ignore-filenames-as-triggers
-                    (and file 
+                    (and file
                          (file-name-sans-extension (file-name-nondirectory file))))))
          (name (or (third snippet)
                    (and file
@@ -1850,11 +1949,10 @@ not need to be a real mode."
          (condition (fourth snippet))
          (group (fifth snippet))
          (keybinding (yas/read-keybinding (eighth snippet)))
-         (uid (or (ninth snippet)
+         (uuid (or (ninth snippet)
                   name))
-         (template (or (gethash uid (yas/table-uidhash snippet-table))
+         (template (or (gethash uuid (yas/table-uuidhash snippet-table))
                        (yas/make-blank-template))))
-
     ;; X) populate the template object
     ;;
     (yas/populate-template template
@@ -1862,43 +1960,21 @@ not need to be a real mode."
                            :key         key
                            :content     (second snippet)
                            :name        (or name key)
+                           :group       group
                            :condition   condition
                            :expand-env  (sixth snippet)
                            :file        (seventh snippet)
                            :keybinding  keybinding
-                           :uid         uid)
-    ;; X) setup the menu groups, reorganizing from group to group if
-    ;;    necessary
-    ;;
-    (when (and yas/use-menu
-               (not (cdr (yas/template-menu-binding-pair template))))
-      (let ((group-keymap menu-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))))
-        (let ((menu-binding-pair (yas/snippet-menu-binding-pair-get-create template)))
-          (define-key group-keymap (vector (gensym)) (car menu-binding-pair)))))
+                           :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)))
+    (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."
@@ -1907,11 +1983,11 @@ not need to be a real mode."
             (keybinding (yas/template-keybinding template)))
         (setf (yas/template-menu-binding-pair template)
               (cons `(menu-item ,(or (yas/template-name template)
-                                     (yas/template-uid 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
@@ -1921,76 +1997,87 @@ not need to be a real mode."
         ((eq yas/use-menu 'real-modes)
          (yas/real-mode? mode))
         (t
-         t))) 
+         t)))
 
-(defun yas/delete-from-keymap (keymap name)
-  "Recursively delete items named 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-uidhash table)))
+         (hash (yas/table-uuidhash table)))
     (yas/define-menu-1 table
                        (yas/menu-keymap-get-create table)
                        menu
                        hash)
-    (dolist (uid omit-items)
-      (let ((template (or (gethash uid hash)
-                          (yas/populate-template (puthash uid
+    (dolist (uuid omit-items)
+      (let ((template (or (gethash uuid hash)
+                          (yas/populate-template (puthash uuid
                                                           (yas/make-blank-template)
                                                           hash)
                                                  :table table
-                                                 :uid uid))))
+                                                 :uuid uuid))))
         (setf (yas/template-menu-binding-pair template) (cons nil :none))))))
 
-(defun yas/define-menu-1 (table keymap menu uidhash)
+(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) uidhash)
+           (let ((template (or (gethash (second e) uuidhash)
                                (yas/populate-template (puthash (second e)
                                                                (yas/make-blank-template)
-                                                               uidhash)
+                                                               uuidhash)
                                                       :table table
-                                                      :uid (second e)))))
+                                                      :perm-group group-list
+                                                      :uuid (second e)))))
              (define-key keymap (vector (gensym))
-               ;; '(menu-item "shit" 'ding)
                (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)))
+             (define-key keymap (vector (gensym))
                `(menu-item ,(second e) ,subkeymap))
-             (yas/define-menu-1 table subkeymap (third e) uidhash)))
+             (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 "----")))
@@ -2057,13 +2144,20 @@ 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))
-                                                   (yas/current-key))
-                                               (yas/current-key))
-    (if templates
-        (yas/expand-or-prompt-for-template templates start end)
+  (let (templates-and-pos)
+    (unless (and yas/expand-only-for-last-commands
+                 (not (member last-command yas/expand-only-for-last-commands)))
+      (setq templates-and-pos (if field
+                                  (save-restriction
+                                    (narrow-to-region (yas/field-start field)
+                                                      (yas/field-end field))
+                                    (yas/current-key))
+                                (yas/current-key))))
+    (if (and templates-and-pos
+             (first templates-and-pos))
+        (yas/expand-or-prompt-for-template (first templates-and-pos)
+                                           (second templates-and-pos)
+                                           (third templates-and-pos))
       (yas/fallback 'trigger-key))))
 
 (defun yas/expand-from-keymap ()
@@ -2080,21 +2174,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.
@@ -2111,14 +2205,16 @@ Common gateway for `yas/expand-from-trigger-key' and
                 (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)
@@ -2135,7 +2231,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
@@ -2170,18 +2266,18 @@ by 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))))
-         (where (if mark-active
+         (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 (region-active-p)
                     (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 ()
@@ -2192,21 +2288,22 @@ visited file in `snippet-mode'."
   (interactive)
   (let* ((yas/buffer-local-condition 'always)
          (templates (yas/all-templates (yas/get-snippet-tables)))
+         (yas/prompt-functions '(yas/ido-prompt yas/completing-prompt))
          (template (and templates
-                        (or (and (rest templates) ;; more than one template for same key
-                                 (yas/prompt-for-template templates
-                                                          "Choose a snippet template to edit: "))
+                        (or (yas/prompt-for-template templates
+                                                     "Choose a snippet template to edit: ")
                             (car templates)))))
 
-    (when template
-      (yas/visit-snippet-file-1 template))))
+    (if template
+        (yas/visit-snippet-file-1 template)
+      (message "No snippets tables active!"))))
 
 (defun yas/visit-snippet-file-1 (template)
   (let ((file (yas/template-file template)))
     (cond ((and file (file-readable-p file))
            (find-file-other-window file)
            (snippet-mode)
-           (setq yas/current-template template))
+           (set (make-local-variable 'yas/editing-template) template))
           (file
            (message "Original file %s no longer exists!" file))
           (t
@@ -2228,7 +2325,7 @@ visited file in `snippet-mode'."
                          (pp-to-string (yas/template-content template))
                        (yas/template-content template))))
            (snippet-mode)
-           (setq yas/current-template template)))))
+           (set (make-local-variable 'yas/editing-template) template)))))
 
 (defun yas/guess-snippet-directories-1 (table)
   "Guesses possible snippet subdirectories for TABLE."
@@ -2242,28 +2339,22 @@ visited file in `snippet-mode'."
   "Try to guess suitable directories based on the current active
 tables (or optional TABLE).
 
-Returns a a list of options alist TABLE -> DIRS where DIRS are
-all the possibly directories where snippets of table might be
-lurking."
+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 (and (listp yas/snippet-dirs)
-                            (first yas/snippet-dirs))
-                       yas/snippet-dirs
-                       (setq yas/snippet-dirs "~/.emacs.d/snippets"))))
+                   (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 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/tables will be created when the first snippet for
-    ;; that mode is loaded.
-    ;; 
+    ;; HACK! the snippet table created here is actually registered!
+    ;;
     (unless (or table (gethash major-mode yas/tables))
-      (setq tables (cons (yas/make-snippet-table (symbol-name major-mode))
-                         tables)))
-    
+      (push (yas/table-get-create major-mode)
+            tables))
+
     (mapcar #'(lambda (table)
                 (cons table
                       (mapcar #'(lambda (subdir)
@@ -2295,13 +2386,14 @@ lurking."
   ""
   (interactive "P")
   (let ((guessed-directories (yas/guess-snippet-directories)))
-    
-    (switch-to-buffer (format "*new snippet for %s*"
-                              (if guessed-directories
-                                  (yas/table-name (car (first guessed-directories)))
-                                "unknown mode")))
+
+    (switch-to-buffer "*new snippet*")
+    (erase-buffer)
+    (kill-all-local-variables)
     (snippet-mode)
-    (setq yas/guessed-directories guessed-directories)
+    (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 "\
@@ -2315,7 +2407,7 @@ lurking."
 $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.
 
@@ -2324,7 +2416,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
@@ -2343,9 +2435,7 @@ there, otherwise, proposes to create the first option returned by
                              (rest guessed-directories)))))
     (unless chosen
       (when (y-or-n-p "Having trouble... go to snippet root dir? ")
-        (setq chosen (if (listp yas/snippet-dirs)
-                         (first yas/snippet-dirs)
-                       yas/snippet-dirs))))
+        (setq chosen (first (yas/snippet-dirs)))))
     (if chosen
         (let ((default-directory chosen))
           (setq buffer (call-interactively (if same-window
@@ -2360,7 +2450,11 @@ there, otherwise, proposes to create the first option returned by
 
 (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))))))
          (parents-file-name (concat file-dir "/.yas-parents"))
          (major-mode-name (and file-dir
@@ -2380,143 +2474,91 @@ there, otherwise, proposes to create the first option returned by
     (when major-mode-sym
       (cons major-mode-sym parents))))
 
-(defvar yas/current-template nil
+(defvar yas/editing-template nil
   "Supporting variable for `yas/load-snippet-buffer' and `yas/visit-snippet'")
-(make-variable-buffer-local 'yas/current-template)
 
-(defvar yas/guessed-directories nil
-  "Supporting variable for `yas/load-snippet-buffer' and `yas/new-snippet'")
-(make-variable-buffer-local 'yas/guessed-directories)
+(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")
-  (cond
-   ;; X) Option 1: We have `yas/current-template', this buffer's
-   ;;  content comes from a template which is already loaded and
-   ;;  neatly positioned,...
-   ;;
-   ((and (boundp 'yas/current-template)
-         yas/current-template
-         (yas/template-p yas/current-template))
-         
-    (let ((parsed (yas/parse-template (yas/template-file yas/current-template))))
-      ;; ... just change its template, expand-env, condition, key,
-      ;; keybinding and name. The group cannot be changed.
-      (yas/populate-template yas/current-template
-                             :content    (second parsed)
-                             :key        (first parsed)
-                             :name       (third parsed)
-                             :condition  (fourth parsed)
-                             :expand-env (sixth parsed)
-                             :keybinding (yas/read-keybinding (eighth parsed)))
-      (yas/update-template (yas/template-table yas/current-template)
-                          yas/current-template))
-    ;; Now, prompt for new file creation much like
-    ;; `yas/new-snippet' if one of the following is true:
-    ;;
-    ;; 1) `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/current-template comes from a file that we cannot write to...
-    ;;
-    (when (or (and (listp yas/snippet-dirs)
-                   (second yas/snippet-dirs)
-                   (not (string-match (expand-file-name (first yas/snippet-dirs))
-                                      (yas/template-file yas/current-template))))
-              (and (yas/template-file yas/current-template)
-                   (not (file-writable-p (yas/template-file yas/current-template))))
-              (not (yas/template-file yas/current-template)))
-      (when (y-or-n-p "[yas] Also save snippet buffer to new file? ")
-        (let* ((option (first (yas/guess-snippet-directories (yas/template-table yas/current-template))))
-               (chosen (and option
-                            (yas/make-directory-maybe option))))
-          (when chosen
-            (let ((default-file-name (or (and (yas/template-file yas/current-template)
-                                              (file-name-nondirectory (yas/template-file yas/current-template)))
-                                         (yas/template-name yas/current-template))))
-              (write-file (concat chosen "/"
-                                  (read-from-minibuffer (format "File name to create in %s? " chosen)
-                                                        default-file-name)))
-              (setf (yas/template-file yas/current-template) buffer-file-name))))))
-    (when kill
-      (quit-window kill))
-    (message "[yas] Snippet \"%s\" loaded for %s."
-             (yas/template-name yas/current-template)
-             (yas/table-name (yas/template-table yas/current-template))))
-   (;; X) Option 2: We have a file name, consider this a brand new
-    ;; snippet and calculate name, groups, etc from the current
-    ;; file-name and buffer content
-    ;;
-    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
-              (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))
-            (when kill
-              (quit-window kill))
-            (message "[yas] Snippet \"%s\" loaded for %s."
-                     name
-                     (car major-mode-and-parent)))
-        (message (format "[yas] Unknown major mode for snippet at %s" buffer-file-name)))))
-
-   ;; X) Option 3: We have `yas/guessed-directories', this
-   ;;  buffer's content comes from `yas/new-snippet' call. Prompt
-   ;;  user for dir and name in guessed dirs, then call
-   ;;  `yas/load-snippet-buffer' (ourselves) again to load the
-   ;;  snippet based on the file-name.
-   ;;
-   ((and (boundp 'yas/guessed-directories)
-         yas/guessed-directories)
-    (let* ((yas/prompt-functions '(yas/ido-prompt yas/completing-prompt))
-           (guessed-directories yas/guessed-directories)
-           (option (or (and (not (y-or-n-p "Let yasnippet guess tables? "))
-                            (first
-                             (yas/guess-snippet-directories 
-                              (some #'(lambda (fn)
-                                        (funcall fn "Choose any snippet table: "
-                                                 (let (res)
-                                                   (maphash #'(lambda (k v)
-                                                                (push v res))
-                                                            yas/tables)
-                                                   res)
-                                                 #'yas/table-name))
-                                    yas/prompt-functions))))
-                       (and (second guessed-directories)
-                            (some #'(lambda (fn)
-                                      (funcall fn "Choose from guessed list of tables: "
-                                               guessed-directories
-                                               #'(lambda (option)
-                                                   (yas/table-name (car option)))))
-                                  yas/prompt-functions))
-                       (first guessed-directories)))
-           (chosen))
-      (setq chosen (yas/make-directory-maybe option))
-
-      (unless chosen
-        (when (y-or-n-p "Having trouble... use snippet root dir? ")
-          (setq chosen (if (listp yas/snippet-dirs)
-                           (first yas/snippet-dirs)
-                         yas/snippet-dirs))))
-      (when chosen
-        (let ((default-directory chosen))
-          (call-interactively 'write-file))
-        (setq yas/guessed-directories nil)
-        (setq yas/current-template nil)
-        (yas/load-snippet-buffer))))))
+  (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)
@@ -2527,31 +2569,37 @@ 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/populate-template (yas/make-blank-template)
-                                               :table       nil ;; an ephemeral snippet has no table...
-                                               :key         (first parsed)
-                                               :content     (second parsed)
-                                               :name        (third parsed)
-                                               :expand-env  (sixth parsed)))))
-    (cond (template
-           (let ((buffer-name (format "*YAS TEST: %s*" (yas/template-name template))))
-             (set-buffer (switch-to-buffer buffer-name))
-             (erase-buffer)
+         (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))))
+             (kill-buffer (get-buffer-create buffer-name))
+             (switch-to-buffer (get-buffer-create buffer-name))
              (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))
+                                 (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))))
+               (add-hook 'post-command-hook 'yas/debug-snippet-vars nil t))))
           (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")
@@ -2569,69 +2617,95 @@ With optional prefix argument KILL quit the window and buffer."
          (original-buffer (current-buffer))
          (continue t)
          (yas/condition-cache-timestamp (current-time)))
-    (with-current-buffer buffer 
-      (let ((buffer-read-only nil))
-        (erase-buffer)
-        (cond ((not by-name-hash)
-               (insert "YASnippet tables by UUID: \n")
-               (while (and table-lists
-                           continue)
-                 (dolist (table (car table-lists))
-                   (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"))
-                   (let ((always (cons "(a)" (list)))
-                         (active (cons "(y)" (list)))
-                         (sleeping (cons "(n)" (list))))
-                     (maphash #'(lambda (k v)
-                                  (let ((condition (yas/template-condition v)))
-                                    (if condition
-                                      (with-current-buffer original-buffer
-                                        (if (yas/eval-condition condition)
-                                            (push v (cdr active))
-                                          (push v (cdr sleeping))))
-                                      (push v (cdr always)))))
-                              (yas/table-uidhash table))
-                     (dolist (type-and-templates (list always active sleeping))
-                       (dolist (p (cdr type-and-templates))
-                         (let ((name (yas/template-name p)))
-                           (insert (propertize (format "%s \\\\snippet `%s'" (car type-and-templates) name) 'yasnippet p))
-                           (insert (make-string (max (- 50 (length name))
-                                                     1) ? ))
-                           (when (yas/template-key p)
-                             (insert (format "key \"%s\" " (yas/template-key p))))
-                           (when (yas/template-keybinding p)
-                             (insert (format "bound to %s " (key-description (yas/template-keybinding p)))))
-                           (insert "\n"))))))
-                 (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)
-               (goto-char (point-min))
-               (help-mode))
-              (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))))))))))
+    (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 original-buffer))
+               (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 &optional original-buffer)
+  (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 group-and-templates)
+        (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 (and condition
+                                             original-buffer)
+                                        (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
 
@@ -2683,20 +2757,28 @@ Otherwise throw exception."
   (when (and yas/moving-away-p (notany #'(lambda (pos) (string= pos yas/text)) possibilities))
     (yas/throw (format "[yas] field only allows %s" possibilities))))
 
-(defun yas/ephemeral-field (number)
-  "Automatically exit snippet when something is type in field NUMBER.
-
-To be used as a primary field transformation."
-  (when yas/modified-p (yas/exit-snippet (first (yas/snippets-at-point))) (yas/field-value number)))
-
 (defun yas/field-value (number)
-  "A primary field transformation..."
+  "Get the string for field with NUMBER.
+
+Use this in primary and mirror transformations to tget."
   (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
@@ -2710,6 +2792,14 @@ To be used as a primary field transformation."
 (defun yas/inside-string ()
   (equal 'font-lock-string-face (get-char-property (1- (point)) 'face)))
 
+(defun yas/unimplemented (&optional missing-feature)
+  (if yas/current-template
+      (if (y-or-n-p (format "This snippet is unimplemented (missing %s) Visit the snippet definition? "
+                            (or missing-feature
+                                "something")))
+          (yas/visit-snippet-file-1 yas/current-template))
+    (message "No implementation. Missing %s" (or missing-feature "something"))))
+
 \f
 ;;; Snippet expansion and field management
 
@@ -2722,9 +2812,6 @@ To be used as a primary field transformation."
 (defconst yas/prefix nil
   "A prefix argument for expansion direct from keybindings")
 
-(defvar yas/deleted-text nil
-  "The text deleted in the last snippet expansion.")
-
 (defvar yas/selected-text nil
   "The selected region deleted on the last snippet expansion.")
 
@@ -2733,7 +2820,8 @@ To be used as a primary field transformation."
 
 (make-variable-buffer-local 'yas/active-field-overlay)
 (make-variable-buffer-local 'yas/field-protection-overlays)
-(make-variable-buffer-local 'yas/deleted-text)
+(put 'yas/active-field-overlay 'permanent-local t)
+(put 'yas/field-protection-overlays 'permanent-local t)
 
 (defstruct (yas/snippet (:constructor yas/make-snippet ()))
   "A snippet.
@@ -2770,9 +2858,13 @@ To be used as a primary field transformation."
   marker
   next)
 
-(defun yas/apply-transform (field-or-mirror field)
-  "Calculate the value of the field/mirror. If there's a transform
-for this field, apply it. Otherwise, returned nil."
+(defun yas/apply-transform (field-or-mirror field &optional empty-on-nil-p)
+  "Calculate transformed string for FIELD-OR-MIRROR from FIELD.
+
+If there is no transform for ht field, return nil.
+
+If there is a transform but it returns nil, return the empty
+string iff EMPTY-ON-NIL-P is true."
   (let* ((yas/text (yas/field-text-for-display field))
          (text yas/text)
          (yas/modified-p (yas/field-modified-p field))
@@ -2786,7 +2878,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 (and empty-on-nil-p "")))))))
     transformed))
 
 (defsubst yas/replace-all (from to &optional text)
@@ -2808,8 +2901,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.
@@ -2819,10 +2911,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))))))
 
@@ -2831,7 +2924,10 @@ have, compare through the field's start point"
   (and (zerop (- (yas/field-start field) (yas/field-end field)))
        (or (yas/field-parent-field field)
            (and (eq field (car (last (yas/snippet-fields snippet))))
-                (= (yas/field-start field) (overlay-end (yas/snippet-control-overlay snippet)))))))
+                (= (yas/field-start field) (overlay-end (yas/snippet-control-overlay snippet)))))
+       ;; the field numbered 0, just before the exit marker, should
+       ;; never be skipped
+       (not (zerop (yas/field-number field)))))
 
 (defun yas/snippets-at-point (&optional all-snippets)
   "Return a sorted list of snippets at point, most recently
@@ -2841,7 +2937,7 @@ inserted first."
                                               (overlay-get ov 'yas/snippet))
                                           (if all-snippets
                                               (overlays-in (point-min) (point-max))
-                                            (overlays-at (point))))))
+                                            (nconc (overlays-at (point)) (overlays-at (1- (point))))))))
    #'(lambda (s1 s2)
        (<= (yas/snippet-id s2) (yas/snippet-id s1)))))
 
@@ -2870,7 +2966,7 @@ delegate to `yas/next-field'."
                                  (yas/snippet-fields snippet)))
          (active-field-pos (position active-field live-fields))
          (target-pos (and active-field-pos (+ arg active-field-pos)))
-         (target-field (nth target-pos live-fields)))
+         (target-field (and target-pos (nth target-pos live-fields))))
     ;; First check if we're moving out of a field with a transform
     ;;
     (when (and active-field
@@ -2882,7 +2978,7 @@ delegate to `yas/next-field'."
         ;; primary field transform: exit call to field-transform
         (yas/eval-lisp (yas/field-transform active-field))))
     ;; Now actually move...
-    (cond ((>= target-pos (length live-fields))
+    (cond ((and target-pos (>= target-pos (length live-fields)))
            (yas/exit-snippet snippet))
           (target-field
            (yas/move-to-field snippet target-field))
@@ -2899,15 +2995,23 @@ delegate to `yas/next-field'."
 
 Also create some protection overlays"
   (goto-char (yas/field-start field))
-  (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)))
+    ;; check for the special ${0: ...} field 
+    (if (and number (zerop number))
+        (progn
+          (set-mark (yas/field-end field))
+          (setf (yas/snippet-force-exit snippet)
+                (or (yas/field-transform field)
+                    t)))
+      ;; make this field active
+      (setf (yas/snippet-active-field snippet) field)
+      ;; primary field transform: first call to snippet transform
+      (unless (yas/field-modified-p field)
+        (if (yas/field-update-display field snippet)
+            (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."
@@ -2940,14 +3044,18 @@ Also create some protection overlays"
 \f
 ;;; Some low level snippet-routines
 
-(defun yas/commit-snippet (snippet &optional no-hooks)
+(defmacro yas/inhibit-overlay-hooks (&rest body)
+  "Run BODY with `yas/inhibit-overlay-hooks' set to t."
+  (declare (indent 0))
+  `(let ((yas/inhibit-overlay-hooks t))
+     (progn ,@body)))
+
+(defun yas/commit-snippet (snippet)
   "Commit SNIPPET, but leave point as it is.  This renders the
 snippet as ordinary text.
 
 Return a buffer position where the point should be placed if
-exiting the snippet.
-
-NO-HOOKS means don't run the `yas/after-exit-snippet-hook' hooks."
+exiting the snippet."
 
   (let ((control-overlay (yas/snippet-control-overlay snippet))
         yas/snippet-beg
@@ -2962,7 +3070,7 @@ NO-HOOKS means don't run the `yas/after-exit-snippet-hook' hooks."
       (setq yas/snippet-end (overlay-end control-overlay))
       (delete-overlay control-overlay))
 
-    (let ((inhibit-modification-hooks t))
+    (yas/inhibit-overlay-hooks
       (when yas/active-field-overlay
         (delete-overlay yas/active-field-overlay))
       (when yas/field-protection-overlays
@@ -2987,30 +3095,31 @@ NO-HOOKS means don't run the `yas/after-exit-snippet-hook' hooks."
               buffer-undo-list)
       ;; Dismember the snippet... this is useful if we get called
       ;; again from `yas/take-care-of-redo'....
-      (setf (yas/snippet-fields snippet) nil))
+      (setf (yas/snippet-fields snippet) nil)))
 
-    ;; XXX: `yas/after-exit-snippet-hook' should be run with
-    ;; `yas/snippet-beg' and `yas/snippet-end' bound. That might not
-    ;; be the case if the main overlay had somehow already
-    ;; disappeared, which sometimes happens when the snippet's messed
-    ;; up...
-    ;;
-    (unless no-hooks (run-hooks 'yas/after-exit-snippet-hook)))
+  (message "[yas] snippet %s exited." (yas/snippet-id snippet)))
+
+(defun yas/safely-run-hooks (hook-var)
+  (condition-case error
+      (run-hooks hook-var)
+    (error
+     (message "[yas] %s error: %s" hook-var (error-message-string error)))))
 
-  (message "[yas] snippet exited."))
 
 (defun yas/check-commit-snippet ()
   "Checks if point exited the currently active field of the
 snippet, if so cleans up the whole snippet up."
   (let* ((snippets (yas/snippets-at-point 'all-snippets))
-         (snippets-left snippets))
+         (snippets-left snippets)
+         (snippet-exit-transform))
     (dolist (snippet snippets)
       (let ((active-field (yas/snippet-active-field snippet)))
-        (cond ((or (prog1 (yas/snippet-force-exit snippet)
-                     (setf (yas/snippet-force-exit snippet) nil))
+        (setq snippet-exit-transform (yas/snippet-force-exit snippet)) 
+        (cond ((or snippet-exit-transform
                    (not (and active-field (yas/field-contains-point-p active-field))))
                (setq snippets-left (delete snippet snippets-left))
-               (yas/commit-snippet snippet snippets-left))
+               (setf (yas/snippet-force-exit snippet) nil)
+               (yas/commit-snippet snippet))
               ((and active-field
                     (or (not yas/active-field-overlay)
                         (not (overlay-buffer yas/active-field-overlay))))
@@ -3024,9 +3133,10 @@ snippet, if so cleans up the whole snippet up."
                  (yas/update-mirrors snippet)))
               (t
                nil))))
-    (unless snippets-left
-      (remove-hook 'post-command-hook 'yas/post-command-handler 'local)
-      (remove-hook 'pre-command-hook 'yas/pre-command-handler 'local))))
+    (unless (or (null snippets) snippets-left)
+      (if snippet-exit-transform
+          (yas/eval-lisp-no-saves snippet-exit-transform))
+      (yas/safely-run-hooks 'yas/after-exit-snippet-hook))))
 
 ;; Apropos markers-to-points:
 ;;
@@ -3107,6 +3217,7 @@ holds the keymap."
                                nil
                                t)))
     (overlay-put overlay 'keymap yas/keymap)
+    (overlay-put overlay 'priority 100)
     (overlay-put overlay 'yas/snippet snippet)
     overlay))
 
@@ -3133,7 +3244,7 @@ Otherwise deletes a character normally by calling `delete-char'."
   ;; 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)))
 
@@ -3169,21 +3280,25 @@ Move the overlay, or create it if it does not exit."
     (overlay-put yas/active-field-overlay 'insert-behind-hooks
                  '(yas/on-field-overlay-modification))))
 
+(defvar yas/inhibit-overlay-hooks nil
+  "Bind this temporarity to non-nil to prevent running `yas/on-*-modification'.")
+
 (defun yas/on-field-overlay-modification (overlay after? beg end &optional length)
   "Clears the field and updates mirrors, conditionally.
 
 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)))
+  (unless (or yas/inhibit-overlay-hooks
+              (yas/undo-in-progress))
+    (let* ((field (overlay-get 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))
-             (yas/update-mirrors (car (yas/snippets-at-point))))
+             (save-excursion
+               (yas/field-update-display field snippet))
+             (yas/update-mirrors snippet))
             (field
              (when (and (not after?)
                         (not (yas/field-modified-p field))
@@ -3224,7 +3339,7 @@ Move the overlays, or create them if they do not exit."
     ;;
     (when (< (buffer-size) end)
       (save-excursion
-        (let ((inhibit-modification-hooks t))
+        (yas/inhibit-overlay-hooks
           (goto-char (point-max))
           (newline))))
     ;; go on to normal overlay creation/moving
@@ -3255,10 +3370,11 @@ originated")
   "Signals a snippet violation, then issues error.
 
 The error should be ignored in `debug-ignored-errors'"
-  (cond ((not (or after?
-                  (yas/undo-in-progress)))
-         (setq yas/protection-violation (point))
-         (error "Exit the snippet first!"))))
+  (unless yas/inhibit-overlay-hooks
+    (cond ((not (or after?
+                    (yas/undo-in-progress)))
+           (setq yas/protection-violation (point))
+           (error "Exit the snippet first!")))))
 
 (add-to-list 'debug-ignored-errors "^Exit the snippet first!$")
 
@@ -3296,7 +3412,7 @@ considered when expanding the snippet."
 
   ;; If a region is active, set `yas/selected-text'
   (setq yas/selected-text
-        (when mark-active
+        (when (region-active-p)
           (prog1 (buffer-substring-no-properties (region-beginning)
                                                  (region-end))
             (unless start (setq start (region-beginning))
@@ -3309,18 +3425,17 @@ considered when expanding the snippet."
   (let ((to-delete (and start end (buffer-substring-no-properties start end)))
         (start (or start (point)))
         (end (or end (point)))
-        (column (current-column))
         snippet)
+    (setq yas/indent-original-column (current-column))
     ;; Delete the region to delete, this *does* get undo-recorded.
     ;;
     (when (and to-delete
                (> end start))
-      (delete-region start end)
-      (setq yas/deleted-text to-delete))
+      (delete-region start end))
 
     (cond ((listp content)
            ;; x) This is a snippet-command
-           ;; 
+           ;;
            (yas/eval-lisp-no-saves content))
           (t
            ;; x) This is a snippet-snippet :-)
@@ -3334,21 +3449,20 @@ considered when expanding the snippet."
            ;;    stacked expansion: also shoosh the overlay modification hooks
            (save-restriction
              (narrow-to-region start start)
-             (let ((inhibit-modification-hooks t)
-                   (buffer-undo-list t))
+             (let ((buffer-undo-list t))
                ;; snippet creation might evaluate users elisp, which
                ;; might generate errors, so we have to be ready to catch
                ;; them mostly to make the undo information
                ;;
                (setq yas/start-column (save-restriction (widen) (current-column)))
-
-               (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))))))
+               (yas/inhibit-overlay-hooks
+                 (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
            ;; `yas/previous-active-field' and advance its boudary.
@@ -3387,8 +3501,9 @@ considered when expanding the snippet."
            (let ((first-field (car (yas/snippet-fields snippet))))
              (when first-field
                (sit-for 0) ;; fix issue 125
-               (yas/move-to-field snippet first-field))))
-          (message "[yas] snippet expanded."))))
+               (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
@@ -3398,7 +3513,7 @@ Meant to exit in the `buffer-undo-list'."
   ;; slightly optimize: this action is only needed for snippets with
   ;; at least one field
   (when (yas/snippet-fields snippet)
-    (yas/commit-snippet snippet 'no-hooks)))
+    (yas/commit-snippet snippet)))
 
 (defun yas/snippet-revive (beg end snippet)
   "Revives the SNIPPET and creates a control overlay from BEG to
@@ -3424,9 +3539,6 @@ After revival, push the `yas/take-care-of-redo' in the
 
       (yas/move-to-field snippet target-field)
 
-      (add-hook 'post-command-hook 'yas/post-command-handler nil t)
-      (add-hook 'pre-command-hook 'yas/pre-command-handler t t)
-
       (push `(apply yas/take-care-of-redo ,beg ,end ,snippet)
             buffer-undo-list))))
 
@@ -3448,10 +3560,6 @@ Returns the newly created snippet."
     ;; Move to end
     (goto-char (point-max))
 
-    ;; Setup hooks
-    (add-hook 'post-command-hook 'yas/post-command-handler nil t)
-    (add-hook 'pre-command-hook 'yas/pre-command-handler t t)
-
     snippet))
 
 \f
@@ -3572,9 +3680,7 @@ 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/advance-end-of-parents-maybe (yas/fom-parent-field fom) newend))
         ((yas/exit-p fom)
          (yas/advance-start-maybe (yas/fom-next fom) newend))))
 
@@ -3587,7 +3693,10 @@ If it does, also call `yas/advance-end-maybe' on FOM."
     (yas/advance-end-maybe fom newstart)))
 
 (defun yas/advance-end-of-parents-maybe (field newend)
-  "Like `yas/advance-end-maybe' but for parents."
+  "Like `yas/advance-end-maybe' but for parent fields.
+
+Only works for fields and doesn't care about the start of the
+next FOM. Works its way up recursively for parents of parents."
   (when (and field
              (< (yas/field-end field) newend))
     (set-marker (yas/field-end field) newend)
@@ -3651,18 +3760,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
@@ -3685,6 +3792,7 @@ SNIPPET-MARKERS."
               (set-marker marker (point)))
           trouble-markers)))
 
+(defvar yas/indent-original-column nil)
 (defun yas/indent (snippet)
   (let ((snippet-markers (yas/collect-snippet-markers snippet)))
     ;; Look for those $>
@@ -3698,7 +3806,7 @@ SNIPPET-MARKERS."
       (cond ((eq yas/indent-line 'fixed)
              (while (and (zerop (forward-line))
                          (zerop (current-column)))
-               (indent-to-column column)))
+               (indent-to-column yas/indent-original-column)))
             ((eq yas/indent-line 'auto)
              (let ((end (set-marker (make-marker) (point-max)))
                    (indent-first-line-p yas/also-auto-indent-first-line))
@@ -3770,12 +3878,13 @@ With optional string TEXT do it in string instead of the buffer."
 
 (defun yas/replace-backquotes ()
   "Replace all the \"`(lisp-expression)`\"-style expression
-  with their evaluated value"
+       with their evaluated value"
   (while (re-search-forward yas/backquote-lisp-expression-regexp nil t)
-    (let ((transformed (yas/eval-lisp (yas/read-lisp (yas/restore-escapes (match-string 1))))))
-      (goto-char (match-end 0))
-      (when transformed (insert transformed))
-      (delete-region (match-beginning 0) (match-end 0)))))
+    (let ((current-string (match-string 1)) transformed)
+      (delete-region (match-beginning 0) (match-end 0))
+      (setq transformed (yas/eval-lisp (yas/read-lisp (yas/restore-escapes current-string))))
+      (goto-char (match-beginning 0))
+      (when transformed (insert transformed)))))
 
 (defun yas/scan-sexps (from count)
   (condition-case err
@@ -3817,7 +3926,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))
@@ -3837,7 +3947,7 @@ 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)
@@ -3850,7 +3960,7 @@ When multiple expressions are found, only the last one counts."
           ;; 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)
@@ -3882,13 +3992,13 @@ When multiple expressions are found, only the last one counts."
                                     (buffer-substring-no-properties (match-beginning 2)
                                                                     (1- real-match-end-0))))))))
       (when brand-new-mirror
-        (push 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)
@@ -3914,7 +4024,7 @@ When multiple expressions are found, only the last one counts."
                                             (yas/make-marker (match-beginning 0))
                                             (yas/make-marker (match-beginning 0))
                                             nil)))
-                     (push brand-new-mirror 
+                     (push brand-new-mirror
                            (yas/field-mirrors field))
                      (yas/calculate-mirrors-in-fields snippet brand-new-mirror))
                  (push (yas/make-field number
@@ -3940,18 +4050,13 @@ When multiple expressions are found, only the last one counts."
            (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.
-          ;;
-          (let ((inhibit-modification-hooks t)
-                (mirror-parent-field (yas/mirror-parent-field mirror)))
+          (let ((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
@@ -3969,13 +4074,14 @@ When multiple expressions are found, only the last one counts."
   (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)
+                          (or (yas/apply-transform mirror field 'empty-on-nil)
                               (yas/field-text-for-display field)))))
     (when (and reflection
                (not (string= reflection (buffer-substring-no-properties (yas/mirror-start mirror)
                                                                         (yas/mirror-end mirror)))))
       (goto-char (yas/mirror-start mirror))
-      (insert reflection)
+      (yas/inhibit-overlay-hooks
+        (insert reflection))
       (if (> (yas/mirror-end mirror) (point))
           (delete-region (point) (yas/mirror-end mirror))
         (set-marker (yas/mirror-end mirror) (point))
@@ -3986,26 +4092,27 @@ When multiple expressions are found, only the last one counts."
 (defun yas/field-update-display (field snippet)
   "Much like `yas/mirror-update-display', but for fields"
   (when (yas/field-transform field)
-    (let ((inhibit-modification-hooks t)
-          (transformed (yas/apply-transform field field))
+    (let ((transformed (and (not (eq (yas/field-number field) 0))
+                            (yas/apply-transform field field)))
           (point (point)))
       (when (and transformed
                  (not (string= transformed (buffer-substring-no-properties (yas/field-start field)
                                                                            (yas/field-end field)))))
         (setf (yas/field-modified-p field) t)
         (goto-char (yas/field-start field))
-        (insert transformed)
-        (if (> (yas/field-end field) (point))
-            (delete-region (point) (yas/field-end field))
-          (set-marker (yas/field-end field) (point))
-          (yas/advance-start-maybe (yas/field-next field) (point)))
-        t))))
+        (yas/inhibit-overlay-hooks
+          (insert transformed)
+          (if (> (yas/field-end field) (point))
+              (delete-region (point) (yas/field-end field))
+            (set-marker (yas/field-end field) (point))
+            (yas/advance-start-maybe (yas/field-next field) (point)))
+          t)))))
 
 \f
-;;; Pre- and post-command hooks:
+;;; Post-command hooks:
 
 (defvar yas/post-command-runonce-actions nil
-  "List of actions to run once  `post-command-hook'.
+  "List of actions to run once in `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
@@ -4014,8 +4121,6 @@ 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
@@ -4050,19 +4155,21 @@ that the rest of `yas/post-command-handler' runs.")
 \f
 ;;; Fancy docs:
 
-(put 'yas/expand  'function-documentation '(yas/expand-from-trigger-key-doc))
+(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
@@ -4112,21 +4219,39 @@ object satisfying `yas/field-p' to restrict the expansion to.")))
 (defun yas/create-snippet-xrefs ()
   (save-excursion
     (goto-char (point-min))
-    (while (search-forward-regexp "\\\\\\\\snippet[ \s\t]+\\(`[^']+'\\)" nil t)
+    (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:
-;; `locate-dominating-file' is added for compatibility in emacs < 23
-(unless (or (eq emacs-major-version 23)
-            (fboundp 'locate-dominating-file))
-  (defvar locate-dominating-stop-dir-regexp
-    "\\`\\(?:[\\/][\\/][^\\/]+[\\/]\\|/\\(?:net\\|afs\\|\\.\\.\\.\\)/\\)\\'"
-    "Regexp of directory names which stop the search in `locate-dominating-file'.
+;;; 
+;; `locate-dominating-file' 
+;; `region-active-p'
+;; 
+;; added for compatibility in emacs < 23
+(unless (>= emacs-major-version 23)
+  (unless (fboundp 'region-active-p)
+    (defun region-active-p ()  (and transient-mark-mode mark-active)))
+
+  (unless (fboundp 'locate-dominating-file)
+    (defvar locate-dominating-stop-dir-regexp
+      "\\`\\(?:[\\/][\\/][^\\/]+[\\/]\\|/\\(?:net\\|afs\\|\\.\\.\\.\\)/\\)\\'"
+      "Regexp of directory names which stop the search in `locate-dominating-file'.
 Any directory whose name matches this regexp will be treated like
 a kind of root directory by `locate-dominating-file' which will stop its search
 when it bumps into it.
@@ -4134,44 +4259,44 @@ The default regexp prevents fruitless and time-consuming attempts to find
 special files in directories in which filenames are interpreted as hostnames,
 or mount points potentially requiring authentication as a different user.")
 
-  (defun locate-dominating-file (file name)
-    "Look up the directory hierarchy from FILE for a file named NAME.
+    (defun locate-dominating-file (file name)
+      "Look up the directory hierarchy from FILE for a file named NAME.
 Stop at the first parent directory containing a file NAME,
 and return the directory.  Return nil if not found."
-    ;; We used to use the above locate-dominating-files code, but the
-    ;; directory-files call is very costly, so we're much better off doing
-    ;; multiple calls using the code in here.
-    ;;
-    ;; Represent /home/luser/foo as ~/foo so that we don't try to look for
-    ;; `name' in /home or in /.
-    (setq file (abbreviate-file-name file))
-    (let ((root nil)
-          (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".
-          (user nil)
-          try)
-      (while (not (or root
-                      (null file)
-                      ;; FIXME: Disabled this heuristic because it is sometimes
-                      ;; inappropriate.
-                      ;; As a heuristic, we stop looking up the hierarchy of
-                      ;; directories as soon as we find a directory belonging
-                      ;; to another user.  This should save us from looking in
-                      ;; things like /net and /afs.  This assumes that all the
-                      ;; files inside a project belong to the same user.
-                      ;; (let ((prev-user user))
-                      ;;   (setq user (nth 2 (file-attributes file)))
-                      ;;   (and prev-user (not (equal user prev-user))))
-                      (string-match locate-dominating-stop-dir-regexp file)))
-        (setq try (file-exists-p (expand-file-name name file)))
-        (cond (try (setq root file))
-              ((equal file (setq prev-file file
-                                 file (file-name-directory
-                                       (directory-file-name file))))
-               (setq file nil))))
-      root)))
+      ;; We used to use the above locate-dominating-files code, but the
+      ;; directory-files call is very costly, so we're much better off doing
+      ;; multiple calls using the code in here.
+      ;;
+      ;; Represent /home/luser/foo as ~/foo so that we don't try to look for
+      ;; `name' in /home or in /.
+      (setq file (abbreviate-file-name file))
+      (let ((root nil)
+            (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 UUID".
+            (user nil)
+            try)
+        (while (not (or root
+                        (null file)
+                        ;; FIXME: Disabled this heuristic because it is sometimes
+                        ;; inappropriate.
+                        ;; As a heuristic, we stop looking up the hierarchy of
+                        ;; directories as soon as we find a directory belonging
+                        ;; to another user.  This should save us from looking in
+                        ;; things like /net and /afs.  This assumes that all the
+                        ;; files inside a project belong to the same user.
+                        ;; (let ((prev-user user))
+                        ;;   (setq user (nth 2 (file-attributes file)))
+                        ;;   (and prev-user (not (equal user prev-user))))
+                        (string-match locate-dominating-stop-dir-regexp file)))
+          (setq try (file-exists-p (expand-file-name name file)))
+          (cond (try (setq root file))
+                ((equal file (setq prev-file file
+                                   file (file-name-directory
+                                         (directory-file-name file))))
+                 (setq file nil))))
+        root))))
 
 ;; `c-neutralize-syntax-in-CPP` sometimes fires "End of Buffer" error
 ;; (when it execute forward-char) and interrupt the after change