* fix big bug in `yas/remove-snippet-by-uid', much cleaner also.

* other minor adjustments
This commit is contained in:
capitaomorte 2010-03-19 14:01:47 +00:00
parent cd8cbd943a
commit fccfb6e22f

View File

@ -148,6 +148,7 @@
(require 'cl) (require 'cl)
(require 'assoc) (require 'assoc)
(require 'easymenu) (require 'easymenu)
(require 'help-mode)
;;; User customizable variables ;;; User customizable variables
@ -945,34 +946,28 @@ Has the following fields:
;; entry. ;; entry.
;; ;;
(defun yas/remove-snippet-by-uid (table uid) (defun yas/remove-snippet-by-uid (table uid)
"Attempt to remove from TABLE a template with NAME and KEY. "Remove from TABLE a template identified by UID."
TYPE-FN indicates if KEY is a trigger key (string) or a
keybinding (vector)."
(let ((template (gethash uid (yas/snippet-table-uidhash table)))) (let ((template (gethash uid (yas/snippet-table-uidhash table))))
(when template (when template
(let* ((name (yas/template-name template)) (let* ((name (yas/template-name template))
(key (yas/template-key template)) (empty-keys nil))
(keybinding (yas/template-keybinding template))
(key-namehash (and key (gethash key (yas/snippet-table-hash table))))
(keybinding-namehash (and keybinding (gethash keybinding (yas/snippet-table-hash table)))))
;; Remove the name from each of the targeted namehashes ;; Remove the name from each of the targeted namehashes
;; ;;
(dolist (namehash (remove nil (list key-namehash (maphash #'(lambda (k v)
keybinding-namehash))) (when (gethash name v)
(remhash name namehash)) (remhash name v)
;; Cleanup if any of the namehashes in now empty. The (when (zerop (hash-table-count v))
;; keybinding namehash, if empty, leads to the actual (push k empty-keys))))
;; keybinding being removed as well. (yas/snippet-table-hash table))
;; Remove the namehashed themselves if they've become empty
;; ;;
(when (and key-namehash (zerop (hash-table-count key-namehash))) (dolist (key empty-keys)
(remhash key (yas/snippet-table-hash table)) (remhash key (yas/snippet-table-hash table)))
(when (and keybinding-namehash (zerop (hash-table-count keybinding-namehash)))
(define-key (yas/snippet-table-direct-keymap table) keybinding nil)
(remhash keybinding (yas/snippet-table-hash table)))
;; Finally, remove the uid from the uidhash ;; Finally, remove the uid from the uidhash
;; ;;
(remhash uid (yas/snippet-table-uidhash table))))))) (remhash uid (yas/snippet-table-uidhash table))))))
(defun yas/add-snippet (table template) (defun yas/add-snippet (table template)
"Store in TABLE the snippet template TEMPLATE. "Store in TABLE the snippet template TEMPLATE.
@ -2358,7 +2353,8 @@ With optional prefix argument KILL quit the window and buffer."
(not (string-match (expand-file-name (first yas/snippet-dirs)) (not (string-match (expand-file-name (first yas/snippet-dirs))
(yas/template-file yas/current-template)))) (yas/template-file yas/current-template))))
(and (yas/template-file yas/current-template) (and (yas/template-file yas/current-template)
(not (file-writable-p (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? ") (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)))) (let* ((option (first (yas/guess-snippet-directories (yas/template-table yas/current-template))))
(chosen (and option (chosen (and option
@ -2485,45 +2481,63 @@ With optional prefix argument KILL quit the window and buffer."
(t (t
(message "[yas] Cannot test snippet for unknown major mode"))))) (message "[yas] Cannot test snippet for unknown major mode")))))
(defun yas/describe-tables (all-tables &optional by-name-hash) (defun yas/describe-tables (&optional choose)
"Display snippets for each table." "Display snippets for each table."
(interactive (list (y-or-n-p "Show also non-active tables? ") (interactive "P")
nil)) (let* ((by-name-hash (and choose
(let ((buffer (get-buffer-create "*YASnippet tables*")) (y-or-n-p "Show by namehash? ")))
(tables (or (and all-tables (buffer (get-buffer-create "*YASnippet tables*"))
(let ((all)) (active-tables (yas/get-snippet-tables))
(maphash #'(lambda (k v) (remain-tables (let ((all))
(push v all)) (maphash #'(lambda (k v)
yas/snippet-tables) (unless (find v active-tables)
all)) (push v all)))
(yas/get-snippet-tables)))) yas/snippet-tables)
all))
(table-lists (list active-tables remain-tables))
(continue t))
(with-current-buffer buffer (with-current-buffer buffer
(let ((buffer-read-only nil)) (let ((buffer-read-only nil))
(erase-buffer) (erase-buffer)
(cond ((not by-name-hash) (cond ((not by-name-hash)
(insert "YASnippet tables by UUID: \n") (insert "YASnippet tables by UUID: \n")
(dolist (table tables) (while (and table-lists
(insert (format "\nSnippet table `%s':\n\n" (yas/snippet-table-name table))) continue)
(let ((templates)) (dolist (table (car table-lists))
(maphash #'(lambda (k v) (insert (format "\nSnippet table `%s'"
(push v templates)) (yas/snippet-table-name table)))
(yas/snippet-table-uidhash table)) (if (yas/snippet-table-parents table)
(dolist (p templates) (insert (format " parents: %s\n\n"
(let ((name (yas/template-name p))) (combine-and-quote-strings
(insert (propertize (format "\\\\snippet `%s'" name) 'yasnippet p)) (mapcar #'yas/snippet-table-name
(insert (make-string (max (- 50 (length name)) (yas/snippet-table-parents table))
1) ? )) ", ")))
(when (yas/template-key p) (insert "\n\n"))
(insert (format "key \"%s\" " (yas/template-key p)))) (let ((templates))
(when (yas/template-keybinding p) (maphash #'(lambda (k v)
(insert (format "bound to %s " (key-description (yas/template-keybinding p))))) (push v templates))
(insert "\n"))))) (yas/snippet-table-uidhash table))
(dolist (p templates)
(let ((name (yas/template-name p)))
(insert (propertize (format "\\\\snippet `%s'" 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) (yas/create-snippet-xrefs)
(beginning-of-buffer) (beginning-of-buffer)
(help-mode)) (help-mode))
(t (t
(insert "\n\nYASnippet tables by NAMEHASH: \n") (insert "\n\nYASnippet tables by NAMEHASH: \n")
(dolist (table tables) (dolist (table (append active-tables remain-tables))
(insert (format "\nSnippet table `%s':\n\n" (yas/snippet-table-name table))) (insert (format "\nSnippet table `%s':\n\n" (yas/snippet-table-name table)))
(let ((keys)) (let ((keys))
(maphash #'(lambda (k v) (maphash #'(lambda (k v)