1 ;;; gmm-utils.el --- Utility functions for Gnus, Message and MML
3 ;; Copyright (C) 2006-2011 Free Software Foundation, Inc.
5 ;; Author: Reiner Steib <reiner.steib@gmx.de>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This library provides self-contained utility functions. The functions are
26 ;; used in Gnus, Message and MML, but within this library there are no
27 ;; dependencies on Gnus, Message, or MML.
32 "Utility functions for Gnus, Message and MML."
34 :version "22.1" ;; Gnus 5.10.9
37 ;; Helper functions from `gnus-utils.el': gmm-verbose, gmm-message, gmm-error
39 (defcustom gmm-verbose 7
40 "Integer that says how verbose gmm should be.
41 The higher the number, the more messages will flash to say what
42 it did. At zero, it will be totally mute; at five, it will
43 display most important messages; and at ten, it will keep on
44 jabbering all the time."
49 (defun gmm-regexp-concat (regexp)
50 "Potentially concat a list of regexps into a single one.
51 The concatenation is done with logical ORs."
57 (mapconcat (lambda (elt) (concat "\\(" elt "\\)"))
62 (defun gmm-message (level &rest args)
63 "If LEVEL is lower than `gmm-verbose' print ARGS using `message'.
65 Guideline for numbers:
67 3 - non-serious error messages
68 5 - messages for things that take a long time
69 7 - not very important messages on stuff
70 9 - messages inside loops."
71 (if (<= level gmm-verbose)
73 ;; We have to do this format thingy here even if the result isn't
74 ;; shown - the return value has to be the same as the return value
76 (apply 'format args)))
79 (defun gmm-error (level &rest args)
80 "Beep an error if LEVEL is equal to or less than `gmm-verbose'.
81 ARGS are passed to `message'."
82 (when (<= (floor level) gmm-verbose)
86 (when (and (floatp level)
87 (not (zerop (setq duration (* 10 (- level (floor level)))))))
92 (defun gmm-widget-p (symbol)
93 "Non-nil if SYMBOL is a widget."
94 (get symbol 'widget-type))
96 (autoload 'widget-create-child-value "wid-edit")
97 (autoload 'widget-convert "wid-edit")
98 (autoload 'widget-default-get "wid-edit")
100 ;; Copy of the `nnmail-lazy' code from `nnmail.el':
101 (define-widget 'gmm-lazy 'default
102 "Base widget for recursive datastructures.
104 This is a copy of the `lazy' widget in Emacs 22.1 provided for compatibility."
106 :convert-widget 'widget-value-convert-widget
107 :value-create (lambda (widget)
108 (let ((value (widget-get widget :value))
109 (type (widget-get widget :type)))
110 (widget-put widget :children
111 (list (widget-create-child-value
112 widget (widget-convert type) value)))))
113 :value-delete 'widget-children-value-delete
114 :value-get (lambda (widget)
115 (widget-value (car (widget-get widget :children))))
116 :value-inline (lambda (widget)
117 (widget-apply (car (widget-get widget :children))
119 :default-get (lambda (widget)
121 (widget-convert (widget-get widget :type))))
122 :match (lambda (widget value)
123 (widget-apply (widget-convert (widget-get widget :type))
125 :validate (lambda (widget)
126 (widget-apply (car (widget-get widget :children)) :validate)))
128 ;; Note: The format of `gmm-tool-bar-item' may change if some future Emacs
129 ;; version will provide customizable tool bar buttons using a different
132 ;; TODO: Extend API so that the "Command" entry can be a function or a plist.
133 ;; In case of a list it should have the format...
135 ;; (:none command-without-modifier
136 ;; :shift command-with-shift-pressed
137 ;; :control command-with-ctrl-pressed
138 ;; :control-shift command-with-control-and-shift-pressed
139 ;; ;; mouse-2 and mouse-3 can't be used in Emacs yet.
140 ;; :mouse-2 command-on-mouse-2-press
141 ;; :mouse-3 command-on-mouse-3-press) ;; typically a menu of related commands
143 ;; Combinations of mouse-[23] plus shift and/or controll might be overkill.
145 ;; Then use (plist-get rs-command :none), (plist-get rs-command :shift)
147 (define-widget 'gmm-tool-bar-item (if (gmm-widget-p 'lazy) 'lazy 'gmm-lazy)
148 "Tool bar list item."
151 (list :tag "Command and Icon"
152 (function :tag "Command")
153 (string :tag "Icon file")
155 (const :tag "Default map" nil)
156 ;; Note: Usually we need non-nil attributes if map is t.
157 (const :tag "No menu" t)
158 (sexp :tag "Other map"))
159 (plist :inline t :tag "Properties"))
160 (list :tag "Separator"
161 (const :tag "No command" gmm-ignore)
162 (string :tag "Icon file")
163 (const :tag "No map")
164 (plist :inline t :tag "Properties"))))
166 (define-widget 'gmm-tool-bar-zap-list (if (gmm-widget-p 'lazy) 'lazy 'gmm-lazy)
168 :tag "Tool bar zap list"
169 :type '(choice (const :tag "Zap all" t)
170 (const :tag "Keep all" nil)
173 ;; Work around (bug in customize?), see
174 ;; <news:v9is48jrj1.fsf@marauder.physik.uni-ulm.de>
175 ;; (new-file open-file dired kill-buffer write-file
176 ;; print-buffer customize help)
188 (const search-forward)
194 (symbol :tag "Icon item")))))
196 ;; (defun gmm-color-cells (&optional display)
197 ;; "Return the number of color cells supported by DISPLAY.
198 ;; Compatibility function."
199 ;; ;; `display-color-cells' doesn't return more than 256 even if color depth is
200 ;; ;; > 8 in Emacs 21.
202 ;; ;; Feel free to add proper XEmacs support.
203 ;; (let* ((cells (and (fboundp 'display-color-cells)
204 ;; (display-color-cells display)))
205 ;; (plane (and (fboundp 'x-display-planes)
206 ;; (ash 1 (x-display-planes))))
208 ;; (max (if (integerp cells) cells none)
209 ;; (if (integerp plane) plane none))))
211 (defcustom gmm-tool-bar-style
212 (if (and (boundp 'tool-bar-mode)
214 (and (fboundp 'display-visual-class)
215 (not (memq (display-visual-class)
216 (list 'static-gray 'gray-scale
217 'static-color 'pseudo-color)))))
220 "Prefered tool bar style."
221 :type '(choice (const :tag "GNOME style" gnome)
222 (const :tag "Retro look" retro))
225 (defvar tool-bar-map)
228 (defun gmm-tool-bar-from-list (icon-list zap-list default-map)
229 "Make a tool bar from ICON-LIST.
231 Within each entry of ICON-LIST, the first element is a menu
232 command, the second element is an icon file name and the third
233 element is a test function. You can use \\[describe-key]
234 <menu-entry> to find out the name of a menu command. The fourth
235 and all following elements are passed as the PROPS argument to the
236 function `tool-bar-local-item'.
238 If ZAP-LIST is a list, remove those item from the default
239 `tool-bar-map'. If it is t, start with a new sparse map. You
240 can use \\[describe-key] <icon> to find out the name of an icon
241 item. When \\[describe-key] <icon> shows \"<tool-bar> <new-file>
242 runs the command find-file\", then use `new-file' in ZAP-LIST.
244 DEFAULT-MAP specifies the default key map for ICON-LIST."
245 (let (;; For Emacs 21, we must let-bind `tool-bar-map'. In Emacs 22, we
246 ;; could use some other local variable.
247 (tool-bar-map (if (eq zap-list t)
249 (copy-keymap tool-bar-map))))
250 (when (listp zap-list)
251 ;; Zap some items which aren't relevant for this mode and take up space.
252 (dolist (key zap-list)
253 (define-key tool-bar-map (vector key) nil)))
255 (let ((command (car el))
257 (fmap (or (nth 2 el) default-map))
258 (props (cdr (cdr (cdr el)))) )
259 ;; command may stem from different from-maps:
260 (cond ((eq command 'gmm-ignore)
261 ;; The dummy `gmm-ignore', see `gmm-tool-bar-item'
262 ;; widget. Suppress tooltip by adding `:enable nil'.
263 (if (fboundp 'tool-bar-local-item)
264 (apply 'tool-bar-local-item icon nil nil
265 tool-bar-map :enable nil props)
266 ;; (tool-bar-local-item ICON DEF KEY MAP &rest PROPS)
267 ;; (tool-bar-add-item ICON DEF KEY &rest PROPS)
268 (apply 'tool-bar-add-item icon nil nil :enable nil props)))
269 ((equal fmap t) ;; Not a menu command
270 (apply 'tool-bar-local-item
272 (intern icon) ;; reuse icon or fmap here?
275 (apply 'tool-bar-local-item-from-menu