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?
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 fonction mark-whole-buffer")