Rose Kunkel
Rose Kunkel

Reputation: 3238

Changing the toolbar in Emacs Lisp

I'm trying to customize the Emacs toolbar with my own images and commands. I have two images for each button, a "disabled" and an "enabled" image. Unfortunately, elisp only provides tool-bar-add-item, which allows you to specify a single image. However, the extended menu item syntax for the toolbar has an :image property, which can be set to either a single image or a vector of four images, for all combinations of enabled/disabled and selected/deselected. I'm storing my two images in "filename.xpm" and "filename-disabled.xpm", and I've defined a helper function to allow me to easily define new toolbar items in this format:

(defun wk-add-toolbar-button (display-name icon definition &rest properties)
  "Add an item to the toolbar
Automatically look for ICON.xpm and ICON-disabled.xpm"
  (let ((images
         `((,(concat icon ".xpm") xpm nil)
           (,(concat icon ".xpm") xpm nil)
           (,(concat icon "-disabled.xpm") xpm nil)
           (,(concat icon "-disabled.xpm") xpm nil))))
    (define-key
      global-map
      (vector 'tool-bar (make-symbol display-name))
      `(menu-item
        ,display-name
        ,(make-symbol display-name)
        ,definition
        :image ,(vconcat (mapcar (lambda (xs) (apply 'create-image xs)) images))
        ,@properties))))

But when I call this function, like:

(setq tool-bar-map (make-sparse-keymap))
(wk-add-toolbar-button "Build" "c:/Users/William/Desktop/emacs-icons/build" 'smart-compile)

my toolbar remains empty.

Adding an image manually, like this:

(define-key global-map [tool-bar build]
  `(menu-item ,(symbol-name 'build) ,'smart-compile :image [
    ,(create-image "c:/Users/William/Desktop/emacs-icons/build.xpm" 'xpm nil)
    ,(create-image "c:/Users/William/Desktop/emacs-icons/build.xpm" 'xpm nil)
    ,(create-image "c:/Users/William/Desktop/emacs-icons/build-disabled.xpm" 'xpm nil)
    ,(create-image "c:/Users/William/Desktop/emacs-icons/build-disabled.xpm" 'xpm nil)]))

causes the menu item to appear, but then it quickly disappears, with no obvious trigger.

How can I correct this behavior?

Upvotes: 2

Views: 1350

Answers (1)

Drew
Drew

Reputation: 30701

This part is faulty:

`(menu-item
    ,display-name
    ,(make-symbol display-name) ; <=========== GET RID OF THIS
    ,definition

Get rid of the make-symbol part and you get what you used manually. And an icon appears in the tool bar.

You can see what's wrong if you print the result of calling wk-add-toolbar-button:

(menu-item "Build" Build smart-compile :image
           [(image :type xpm :file "c:/Users/William/Desktop/emacs-icons/build.xpm")
            (image :type xpm :file "c:/Users/William/Desktop/emacs-icons/build.xpm")
            (image :type xpm :file "c:/Users/William/Desktop/emacs-icons/build-disabled.xpm")
            (image :type xpm :file "c:/Users/William/Desktop/emacs-icons/build-disabled.xpm")])

That Build symbol is extaneous. You want only the "Build" string and the smart-compile symbol.

Upvotes: 1

Related Questions