123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- (define-minor-mode tool-bar-mode
- "Toggle the tool bar in all graphical frames (Tool Bar mode).
- With a prefix argument ARG, enable Tool Bar mode if ARG is
- positive, and disable it otherwise. If called from Lisp, enable
- Tool Bar mode if ARG is omitted or nil.
- See `tool-bar-add-item' and `tool-bar-add-item-from-menu' for
- conveniently adding tool bar items."
- :init-value t
- :global t
-
- :variable tool-bar-mode
- (let ((val (if tool-bar-mode 1 0)))
- (dolist (frame (frame-list))
- (set-frame-parameter frame 'tool-bar-lines val))
-
-
- (if (assq 'tool-bar-lines default-frame-alist)
- (setq default-frame-alist
- (cons (cons 'tool-bar-lines val)
- (assq-delete-all 'tool-bar-lines
- default-frame-alist)))))
- (and tool-bar-mode
- (= 1 (length (default-value 'tool-bar-map)))
- (tool-bar-setup)))
- (defun toggle-tool-bar-mode-from-frame (&optional arg)
- "Toggle tool bar on or off, based on the status of the current frame.
- See `tool-bar-mode' for more information."
- (interactive (list (or current-prefix-arg 'toggle)))
- (if (eq arg 'toggle)
- (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.
- Define this locally to override the global tool bar.")
- (global-set-key [tool-bar]
- `(menu-item ,(purecopy "tool bar") ignore
- :filter tool-bar-make-keymap))
- (declare-function image-mask-p "image.c" (spec &optional frame))
- (defconst tool-bar-keymap-cache (make-hash-table :weakness t :test 'equal))
- (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))))
- (defun tool-bar-make-keymap-1 ()
- "Generate an actual keymap from `tool-bar-map', without caching."
- (mapcar (lambda (bind)
- (let (image-exp plist)
- (when (and (eq (car-safe (cdr-safe bind)) 'menu-item)
-
-
- (setq plist (nthcdr (if (consp (nth 4 bind)) 5 4)
- bind))
- (setq image-exp (plist-get plist :image))
- (consp image-exp)
- (not (eq (car image-exp) 'image))
- (fboundp (car image-exp)))
- (if (not (display-images-p))
- (setq bind nil)
- (let ((image (eval image-exp)))
- (unless (and image (image-mask-p image))
- (setq image (append image '(:mask heuristic))))
- (setq bind (copy-sequence bind)
- plist (nthcdr (if (consp (nth 4 bind)) 5 4)
- bind))
- (plist-put plist :image image))))
- bind))
- tool-bar-map))
- (defun tool-bar-add-item (icon def key &rest props)
- "Add an item to the tool bar.
- ICON names the image, DEF is the key definition and KEY is a symbol
- for the fake function key in the menu keymap. Remaining arguments
- PROPS are additional items to add to the menu item specification. See
- Info node `(elisp)Tool Bar'. Items are added from left to right.
- ICON is the base name of a file containing the image to use. The
- function will first try to use low-color/ICON.xpm if `display-color-cells'
- is less or equal to 256, then ICON.xpm, then ICON.pbm, and finally
- 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))
- (defun tool-bar--image-expression (icon)
- "Return an expression that evaluates to an image spec for ICON."
- (let* ((fg (face-attribute 'tool-bar :foreground))
- (bg (face-attribute 'tool-bar :background))
- (colors (nconc (if (eq fg 'unspecified) nil (list :foreground fg))
- (if (eq bg 'unspecified) nil (list :background bg))))
- (xpm-spec (list :type 'xpm :file (concat icon ".xpm")))
- (xpm-lo-spec (list :type 'xpm :file
- (concat "low-color/" icon ".xpm")))
- (pbm-spec (append (list :type 'pbm :file
- (concat icon ".pbm")) colors))
- (xbm-spec (append (list :type 'xbm :file
- (concat icon ".xbm")) colors)))
- `(find-image (cond ((not (display-color-p))
- ',(list pbm-spec xbm-spec xpm-lo-spec xpm-spec))
- ((< (display-color-cells) 256)
- ',(list xpm-lo-spec xpm-spec pbm-spec xbm-spec))
- (t
- ',(list xpm-spec pbm-spec xbm-spec))))))
- (defun tool-bar-local-item (icon def key map &rest props)
- "Add an item to the tool bar in map MAP.
- ICON names the image, DEF is the key definition and KEY is a symbol
- for the fake function key in the menu keymap. Remaining arguments
- PROPS are additional items to add to the menu item specification. See
- Info node `(elisp)Tool Bar'. Items are added from left to right.
- ICON is the base name of a file containing the image to use. The
- function will first try to use low-color/ICON.xpm if `display-color-cells'
- is less or equal to 256, then ICON.xpm, then ICON.pbm, and finally
- 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))))
- (defun tool-bar-add-item-from-menu (command icon &optional map &rest props)
- "Define tool bar binding for COMMAND in keymap MAP using the given ICON.
- This makes a binding for COMMAND in `tool-bar-map', copying its
- binding from the menu bar in MAP (which defaults to `global-map'), but
- modifies the binding by adding an image specification for ICON. It
- finds ICON just like `tool-bar-add-item'. PROPS are additional
- properties to add to the binding.
- MAP must contain appropriate binding for `[menu-bar]' which holds a keymap.
- 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-from-menu'."
- (apply 'tool-bar-local-item-from-menu command icon
- (default-value 'tool-bar-map) map props))
- (defun tool-bar-local-item-from-menu (command icon in-map &optional from-map &rest props)
- "Define local tool bar binding for COMMAND using the given ICON.
- This makes a binding for COMMAND in IN-MAP, copying its binding from
- the menu bar in FROM-MAP (which defaults to `global-map'), but
- modifies the binding by adding an image specification for ICON. It
- finds ICON just like `tool-bar-add-item'. PROPS are additional
- properties to add to the binding.
- FROM-MAP must contain appropriate binding for `[menu-bar]' which
- holds a keymap."
- (unless from-map
- (setq from-map global-map))
- (let* ((menu-bar-map (lookup-key from-map [menu-bar]))
- (keys (where-is-internal command menu-bar-map))
- (image-exp (tool-bar--image-expression icon))
- submap key)
-
-
-
- (dolist (k keys)
-
-
-
- (if (and (vectorp k)
- (> (length k) 1))
- (let ((m (lookup-key menu-bar-map (substring k 0 -1)))
-
- (kk (aref k (1- (length k)))))
- (if (and (keymapp m)
- (symbolp kk))
- (setq submap m
- key kk)))))
- (when (and (symbolp submap) (boundp submap))
- (setq submap (eval submap)))
- (let ((defn (assq key (cdr submap))))
- (if (eq (cadr defn) 'menu-item)
- (define-key-after in-map (vector key)
- (append (cdr defn) (list :image image-exp) props))
- (setq defn (cdr defn))
- (define-key-after in-map (vector key)
- (let ((rest (cdr defn)))
-
-
- (if (and (consp rest) (consp (car rest)))
- (setq rest (cdr rest)))
- (append `(menu-item ,(car defn) ,rest)
- (list :image image-exp) props)))))))
- (defun tool-bar-setup ()
- (setq tool-bar-separator-image-expression
- (tool-bar--image-expression "separator"))
- (tool-bar-add-item-from-menu 'find-file "new" nil :label "New File"
- :vert-only t)
- (tool-bar-add-item-from-menu 'menu-find-file-existing "open" nil
- :label "Open" :vert-only t)
- (tool-bar-add-item-from-menu 'dired "diropen" nil :vert-only t)
- (tool-bar-add-item-from-menu 'kill-this-buffer "close" nil :vert-only t)
- (tool-bar-add-item-from-menu 'save-buffer "save" nil
- :label "Save")
- (define-key-after (default-value 'tool-bar-map) [separator-1] menu-bar-separator)
- (tool-bar-add-item-from-menu 'undo "undo" nil)
- (define-key-after (default-value 'tool-bar-map) [separator-2] menu-bar-separator)
- (tool-bar-add-item-from-menu (lookup-key menu-bar-edit-menu [cut])
- "cut" nil :vert-only t)
- (tool-bar-add-item-from-menu (lookup-key menu-bar-edit-menu [copy])
- "copy" nil :vert-only t)
- (tool-bar-add-item-from-menu (lookup-key menu-bar-edit-menu [paste])
- "paste" nil :vert-only t)
- (define-key-after (default-value 'tool-bar-map) [separator-3] menu-bar-separator)
- (tool-bar-add-item-from-menu 'isearch-forward "search"
- nil :label "Search" :vert-only t)
-
-
-
-
-
-
-
-
-
-
-
- )
- (if (featurep 'move-toolbar)
- (defcustom tool-bar-position 'top
- "Specify on which side the tool bar shall be.
- Possible values are `top' (tool bar on top), `bottom' (tool bar at bottom),
- `left' (tool bar on left) and `right' (tool bar on right).
- Customize `tool-bar-mode' if you want to show or hide the tool bar."
- :version "24.1"
- :type '(choice (const top)
- (const bottom)
- (const left)
- (const right))
- :group 'frames
- :initialize 'custom-initialize-default
- :set (lambda (sym val)
- (set-default sym val)
- (modify-all-frames-parameters
- (list (cons 'tool-bar-position val))))))
- (provide 'tool-bar)
|