1 ;;; gmm-utils.el --- Utility functions for Gnus, Message and MML
3 ;; Copyright (C) 2006 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 2, or (at your option)
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; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; This library provides self-contained utility functions. The functions are
28 ;; used in Gnus, Message and MML, but within this library there are no
29 ;; dependencies on Gnus, Message, or MML or Gnus.
34 "Utility functions for Gnus, Message and MML"
36 :version "23.0" ;; No Gnus
39 ;; Helper functions from `gnus-utils.el': gmm-verbose, gmm-message, gmm-error
41 (defcustom gmm-verbose 7
42 "Integer that says how verbose gmm should be.
43 The higher the number, the more messages will flash to say what
44 it done. At zero, it will be totally mute; at five, it will
45 display most important messages; and at ten, it will keep on
46 jabbering all the time."
51 (defun gmm-message (level &rest args)
52 "If LEVEL is lower than `gmm-verbose' print ARGS using `message'.
54 Guideline for numbers:
55 1 - error messages, 3 - non-serious error messages, 5 - messages for things
56 that take a long time, 7 - not very important messages on stuff, 9 - messages
58 (if (<= level gmm-verbose)
60 ;; We have to do this format thingy here even if the result isn't
61 ;; shown - the return value has to be the same as the return value
63 (apply 'format args)))
66 (defun gmm-error (level &rest args)
67 "Beep an error if LEVEL is equal to or less than `gmm-verbose'.
68 ARGS are passed to `message'."
69 (when (<= (floor level) gmm-verbose)
73 (when (and (floatp level)
74 (not (zerop (setq duration (* 10 (- level (floor level)))))))
79 (defun gmm-widget-p (symbol)
80 "Non-nil iff SYMBOL is a widget."
81 (get symbol 'widget-type))
83 ;; Copy of the `nnmail-lazy' code from `nnmail.el':
84 (define-widget 'gmm-lazy 'default
85 "Base widget for recursive datastructures.
87 This is copy of the `lazy' widget in Emacs 22.1 provided for compatibility."
89 :convert-widget 'widget-value-convert-widget
90 :value-create (lambda (widget)
91 (let ((value (widget-get widget :value))
92 (type (widget-get widget :type)))
93 (widget-put widget :children
94 (list (widget-create-child-value
95 widget (widget-convert type) value)))))
96 :value-delete 'widget-children-value-delete
97 :value-get (lambda (widget)
98 (widget-value (car (widget-get widget :children))))
99 :value-inline (lambda (widget)
100 (widget-apply (car (widget-get widget :children))
102 :default-get (lambda (widget)
104 (widget-convert (widget-get widget :type))))
105 :match (lambda (widget value)
106 (widget-apply (widget-convert (widget-get widget :type))
108 :validate (lambda (widget)
109 (widget-apply (car (widget-get widget :children)) :validate)))
111 ;; Note: The format of `gmm-tool-bar-item' may change if some future Emacs
112 ;; version will provide customizable tool bar buttons using a different
115 ;; TODO: Extend API so that the "Command" entry can be a function or a plist.
116 ;; In case of a list it should have the format...
118 ;; (:none command-without-modifier
119 ;; :shift command-with-shift-pressed
120 ;; :control command-with-ctrl-pressed
121 ;; :control-shift command-with-control-and-shift-pressed
122 ;; ;; mouse-2 and mouse-3 can't be used in Emacs yet.
123 ;; :mouse-2 command-on-mouse-2-press
124 ;; :mouse-3 command-on-mouse-3-press) ;; typically a menu of related commands
126 ;; Combinations of mouse-[23] plus shift and/or controll might be overkill.
128 ;; Then use (plist-get rs-command :none), (plist-get rs-command :shift)
130 (define-widget 'gmm-tool-bar-item (if (gmm-widget-p 'lazy) 'lazy 'gmm-lazy)
131 "Tool bar list item."
134 (list :tag "Command and Icon"
135 (function :tag "Command")
136 (string :tag "Icon file")
138 (const :tag "Default map" nil)
139 ;; Note: Usually we need non-nil attributes if map is t.
140 (const :tag "No menu" t)
141 (sexp :tag "Other map"))
142 (plist :inline t :tag "Properties"))
143 (list :tag "Separator"
144 (const :tag "No command" gmm-ignore)
145 (string :tag "Icon file")
146 (const :tag "No map")
147 (plist :inline t :tag "Properties"))))
149 (define-widget 'gmm-tool-bar-zap-list (if (gmm-widget-p 'lazy) 'lazy 'gmm-lazy)
151 :tag "Tool bar zap list"
152 :type '(choice (const :tag "Zap all" t)
153 (const :tag "Keep all" nil)
156 ;; Work around (bug in customize?), see
157 ;; <news:v9is48jrj1.fsf@marauder.physik.uni-ulm.de>
158 ;; (new-file open-file dired kill-buffer write-file
159 ;; print-buffer customize help)
171 (const search-forward)
177 (symbol :tag "Icon item")))))
179 ;; (defun gmm-color-cells (&optional display)
180 ;; "Return the number of color cells supported by DISPLAY.
181 ;; Compatibility function."
182 ;; ;; `display-color-cells' doesn't return more than 256 even if color depth is
183 ;; ;; > 8 in Emacs 21.
185 ;; ;; Feel free to add proper XEmacs support.
186 ;; (let* ((cells (and (fboundp 'display-color-cells)
187 ;; (display-color-cells display)))
188 ;; (plane (and (fboundp 'x-display-planes)
189 ;; (ash 1 (x-display-planes))))
191 ;; (max (if (integerp cells) cells none)
192 ;; (if (integerp plane) plane none))))
194 (defcustom gmm-tool-bar-style
195 (if (and (boundp 'tool-bar-mode)
197 (and (fboundp 'display-visual-class)
198 (not (memq (display-visual-class)
199 (list 'static-gray 'gray-scale
200 'static-color 'pseudo-color)))))
203 "Prefered tool bar style."
204 :type '(choice (const :tag "GNOME style" 'gnome)
205 (const :tag "Retro look" 'retro))
208 (defvar tool-bar-map)
211 (defun gmm-tool-bar-from-list (icon-list zap-list default-map)
212 "Make a tool bar from ICON-LIST.
214 Within each entry of ICON-LIST, the first element is a menu
215 command, the second element is an icon file name and the third
216 element is a test function. You can use \\[describe-key]
217 <menu-entry> to find out the name of a menu command. The fourth
218 and all following elements are passed a the PROPS argument to the
219 function `tool-bar-local-item'.
221 If ZAP-LIST is a list, remove those item from the default
222 `tool-bar-map'. If it is t, start with a new sparse map. You
223 can use \\[describe-key] <icon> to find out the name of an icon
224 item. When \\[describe-key] <icon> shows \"<tool-bar> <new-file>
225 runs the command find-file\", then use `new-file' in ZAP-LIST.
227 DEFAULT-MAP specifies the default key map for ICON-LIST."
228 (let (;; For Emacs 21, we must let-bind `tool-bar-map'. In Emacs 22, we
229 ;; could use some other local variable.
230 (tool-bar-map (if (eq zap-list t)
232 (copy-keymap tool-bar-map))))
233 (when (listp zap-list)
234 ;; Zap some items which aren't relevant for this mode and take up space.
235 (dolist (key zap-list)
236 (define-key tool-bar-map (vector key) nil)))
238 (let ((command (car el))
240 (fmap (or (nth 2 el) default-map))
241 (props (cdr (cdr (cdr el)))) )
242 ;; command may stem from different from-maps:
243 (cond ((eq command 'gmm-ignore)
244 ;; The dummy `gmm-ignore', see `gmm-tool-bar-item'
245 ;; widget. Suppress tooltip by adding `:enable nil'.
246 (if (fboundp 'tool-bar-local-item)
247 (apply 'tool-bar-local-item icon nil nil
248 tool-bar-map :enable nil props)
249 ;; (tool-bar-local-item ICON DEF KEY MAP &rest PROPS)
250 ;; (tool-bar-add-item ICON DEF KEY &rest PROPS)
251 (apply 'tool-bar-add-item icon nil nil :enable nil props)))
252 ((equal fmap t) ;; Not a menu command
253 (if (fboundp 'tool-bar-local-item)
254 (apply 'tool-bar-local-item
256 (intern icon) ;; reuse icon or fmap here?
258 ;; Emacs 21 compatibility:
259 (apply 'tool-bar-add-item
264 (if (fboundp 'tool-bar-local-item-from-menu)
265 (apply 'tool-bar-local-item-from-menu
266 ;; (apply 'tool-bar-local-item icon def key
267 ;; tool-bar-map props)
268 command icon tool-bar-map (symbol-value fmap)
270 ;; Emacs 21 compatibility:
271 (apply 'tool-bar-add-item-from-menu
272 command icon (symbol-value fmap)
275 (if (symbolp icon-list)
280 ;; WARNING: The following is subject to change. Don't rely on it yet.
282 ;; From MH-E without modifications:
284 (defmacro gmm-defun-compat (name function arg-list &rest body)
285 "Create function NAME.
286 If FUNCTION exists, then NAME becomes an alias for FUNCTION.
287 Otherwise, create function NAME with ARG-LIST and BODY."
288 (let ((defined-p (fboundp function)))
290 `(defalias ',name ',function)
291 `(defun ,name ,arg-list ,@body))))
293 (gmm-defun-compat gmm-image-search-load-path
294 image-search-load-path (file &optional path)
295 "Emacs 21 and XEmacs don't have `image-search-load-path'.
296 This function returns nil on those systems."
299 ;; From MH-E with modifications:
301 (gmm-defun-compat gmm-image-load-path-for-library
302 image-load-path-for-library (library image &optional path)
303 "Return a suitable search path for images relative to LIBRARY.
305 Images for LIBRARY are searched for in \"../../etc/images\" and
306 \"../etc/images\" relative to the files in \"lisp/LIBRARY\" as
307 well as in `image-load-path' and `load-path'.
309 This function returns the value of `load-path' augmented with the
310 path to IMAGE. If PATH is given, it is used instead of
311 `load-path'. If PATH is t, return a single image directory
314 Here is an example that uses a common idiom to provide
315 compatibility with versions of Emacs that lack the variable
319 (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
321 (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\" 'image-load-path)))
322 (mh-tool-bar-folder-buttons-init))
324 This function is used by Emacs versions that don't have
325 `image-load-path-for-library'."
326 (unless library (error "No library specified"))
327 (unless image (error "No image specified"))
328 (let ((image-directory))
330 ;; Try relative setting.
331 ((let (library-name d1ei d2ei)
332 ;; First, find library in the load-path.
333 (setq library-name (locate-library library))
334 (if (not library-name)
335 (error "Cannot find library %s in load-path" library))
336 ;; And then set image-directory relative to that.
339 d2ei (expand-file-name
340 (concat (file-name-directory library-name) "../../etc/images"))
342 d1ei (expand-file-name
343 (concat (file-name-directory library-name) "../etc/images")))
344 (setq image-directory
345 ;; Set it to nil if image is not found.
346 (cond ((file-exists-p (expand-file-name image d2ei)) d2ei)
347 ((file-exists-p (expand-file-name image d1ei)) d1ei)))))
348 ;; Check for images in image-load-path or load-path.
351 ;; Images in image-load-path.
352 (gmm-image-search-load-path image)
353 ;; Images in load-path.
354 (locate-library image)))
356 ;; Since the image might be in a nested directory (for
357 ;; example, mail/attach.pbm), adjust `image-directory'
360 (setq dir (file-name-directory dir))
362 (while (setq parent (file-name-directory img))
363 (setq img (directory-file-name parent)
364 dir (expand-file-name "../" dir)))
365 (setq image-directory dir)))))
367 (error "Could not find image %s for library %s" image library)))
369 ;; Return augmented `image-load-path' or `load-path'.
372 ((and path (symbolp path))
373 (nconc (list image-directory)
374 (delete image-directory
376 (copy-sequence (symbol-value path))
379 (nconc (list image-directory)
380 (delete image-directory (copy-sequence load-path)))))))
382 (defun gmm-customize-mode (&optional mode)
383 "Customize customization group for MODE.
384 If mode is nil, use `major-mode' of the curent buffer."
388 (intern (let ((mode (symbol-name major-mode)))
389 (string-match "^\\(.+\\)-mode$" mode)
390 (match-string 1 mode))))))
394 ;; arch-tag: e0b60920-2ce6-40c1-bfc0-cadbbe26b602
395 ;;; gmm-utils.el ends here