diff options
Diffstat (limited to 'lisp/tool-bar.el')
-rw-r--r-- | lisp/tool-bar.el | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/lisp/tool-bar.el b/lisp/tool-bar.el index 7ec5c0beccc..b1f7a70e33c 100644 --- a/lisp/tool-bar.el +++ b/lisp/tool-bar.el @@ -75,8 +75,8 @@ See `tool-bar-mode' for more information." (tool-bar-mode (if (> (frame-parameter nil 'tool-bar-lines) 0) 0 1)) (tool-bar-mode arg))) -(defvar tool-bar-map (make-sparse-keymap) - "Keymap for the tool bar. +(defvar-keymap tool-bar-map + :doc "Keymap for the tool bar. To override the global tool bar, define this variable buffer-locally and add the items you want to it with @@ -89,15 +89,29 @@ functions.") (declare-function image-mask-p "image.c" (spec &optional frame)) -(defconst tool-bar-keymap-cache (make-hash-table :weakness t :test 'equal)) +(defconst tool-bar-keymap-cache (make-hash-table :test #'equal)) + +(defun tool-bar--cache-key () + (cons (frame-terminal) (sxhash-eq tool-bar-map))) + +(defun tool-bar--flush-cache () + "Remove all cached entries that refer to the current `tool-bar-map'." + (let ((id (sxhash-eq tool-bar-map)) + (entries nil)) + (maphash (lambda (k _) + (when (equal (cdr k) id) + (push k entries))) + tool-bar-keymap-cache) + (dolist (k entries) + (remhash k tool-bar-keymap-cache)))) (defun tool-bar-make-keymap (&optional _ignore) "Generate an actual keymap from `tool-bar-map'. Its main job is to figure out which images to use based on the display's color capability and based on the available image libraries." - (let ((key (cons (frame-terminal) tool-bar-map))) - (or (gethash key tool-bar-keymap-cache) - (puthash key (tool-bar-make-keymap-1) tool-bar-keymap-cache)))) + (or (gethash (tool-bar--cache-key) tool-bar-keymap-cache) + (setf (gethash (tool-bar--cache-key) tool-bar-keymap-cache) + (tool-bar-make-keymap-1)))) (defun tool-bar-make-keymap-1 () "Generate an actual keymap from `tool-bar-map', without caching." @@ -139,7 +153,8 @@ ICON.xbm, using `find-image'. Use this function only to make bindings in the global value of `tool-bar-map'. To define items in any other map, use `tool-bar-local-item'." - (apply #'tool-bar-local-item icon def key tool-bar-map props)) + (apply #'tool-bar-local-item icon def key tool-bar-map props) + (tool-bar--flush-cache)) (defun tool-bar--image-expression (icon) "Return an expression that evaluates to an image spec for ICON." @@ -177,6 +192,7 @@ ICON.xbm, using `find-image'." (let* ((image-exp (tool-bar--image-expression icon))) (define-key-after map (vector key) `(menu-item ,(symbol-name key) ,def :image ,image-exp ,@props)) + (tool-bar--flush-cache) (force-mode-line-update))) ;;;###autoload @@ -243,6 +259,7 @@ holds a keymap." (setq rest (cdr rest))) (append `(menu-item ,(car defn) ,rest) (list :image image-exp) props)))) + (tool-bar--flush-cache) (force-mode-line-update)))) ;;; Set up some global items. Additions/deletions up for grabs. |