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?

2
  • Look at the manual under: tool-bar Commented Mar 23, 2018 at 17:01
  • just did @Drew . thanks for advising. Commented Mar 23, 2018 at 23:23

1 Answer 1

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'")
6
  • How to add this to the right side of the toolbar, now it adds it on the left side?
    – Eka
    Commented Mar 23, 2018 at 23:42
  • Its not working properly, whenever I load a external file to emacs I lose the icon from the toolbar
    – Eka
    Commented Mar 24, 2018 at 0: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? Commented Mar 24, 2018 at 7: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
    Commented Mar 24, 2018 at 7:37
  • Same here. The hook seems useless. Commented Jul 12, 2018 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.