6

Let say I want to add a Toolbar icon to emacs which when clicked should "Select all" text in the current window. We know for selecting text we can use M-x mark-whole-buffer. How can I use this command to make emacs toolbar icon?

Eka
  • 173
  • 1
  • 7

1 Answers1

5

I have created an image corresponding to the pepper.xpm in the below example. In emacs25.3 use 22x22 pixel image. I just copied one of the xpm image files in the /usr/share/emacs/25.3/etc/images (in Linux) folder, and resaved it in a different name (pepper.xpm) in the same folder.

Add to your init.el | emacs.el:

(add-hook 'after-init-hook
          (lambda ()
            (define-key global-map [tool-bar pepper-button]
              '(menu-item "Pepper" mark-whole-buffer
                          :image (image :type xpm :file "/usr/share/emacs/25.3/etc/images/pepper.xpm")
                          ))))

    (tool-bar-add-item "pepper" 'mark-whole-buffer
               'mark-whole-buffer
               :help   "Run fonction mark-whole-buffer")

Replace the image with your xpm file and re-eval your init.el

You can actually do without the add-hook code that defines the button globally after init, But it is useful for demonstrating where the image is stored. For me it works ( running a daemon and in emacsclient -c) without like so:

(tool-bar-add-item "pepper" 
               #'mark-whole-buffer
               'mark-whole-buffer
               :help "Run function `mark-whole-buffer'")
sam boosalis
  • 127
  • 1
  • 7
manandearth
  • 2,068
  • 1
  • 11
  • 23
  • How to add this to the right side of the toolbar, now it adds it on the left side? – Eka Mar 23 '18 at 23:42
  • Its not working properly, whenever I load a external file to emacs I lose the icon from the toolbar – Eka Mar 24 '18 at 00:15
  • in my case the icon was added on the right and it stays there whilst opening and closing files and in all modes. Is there anything else you have in the init.el to do with the tool-bar? – manandearth Mar 24 '18 at 07:28
  • 2
    Initially I added the first block of the code with `add-hook` that time the icon was in the left side and also will go away when a file is added. Then I tried your second block of the code alone `tool-bar-add-item` this time it worked perfectly. Now its fine and thank you for your help – Eka Mar 24 '18 at 07:37
  • Same here. The hook seems useless. – Compro Prasad Jul 12 '18 at 06:04