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 ;; Don't use `gmm-defun-compat' until API changes in
302 ;; `image-load-path-for-library' in Emacs CVS are completed.
304 (defun gmm-image-load-path-for-library (library image &optional path no-error)
305 "Return a suitable search path for images relative to LIBRARY.
307 First it searches for IMAGE in `image-load-path' (excluding
308 \"`data-directory'/images\") and `load-path', followed by a path
309 suitable for LIBRARY, which includes \"../../etc/images\" and
310 \"../etc/images\" relative to the library file itself, and then
311 in \"`data-directory'/images\".
313 Then this function returns a list of directories which contains
314 first the directory in which IMAGE was found, followed by the
315 value of `load-path'. If PATH is given, it is used instead of
318 If NO-ERROR is non-nil and a suitable path can't be found, don't
319 signal an error. Instead, return a list of directories as before,
320 except that nil appears in place of the image directory.
322 Here is an example that uses a common idiom to provide
323 compatibility with versions of Emacs that lack the variable
327 (defvar image-load-path)
329 (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
330 (image-load-path (cons (car load-path)
331 (when (boundp 'image-load-path)
333 (mh-tool-bar-folder-buttons-init))"
334 (unless library (error "No library specified"))
335 (unless image (error "No image specified"))
336 (let (image-directory image-directory-load-path)
337 ;; Check for images in image-load-path or load-path.
340 ;; Images in image-load-path.
341 (gmm-image-search-load-path image) ;; "gmm-" prefix!
342 ;; Images in load-path.
343 (locate-library image)))
345 ;; Since the image might be in a nested directory (for
346 ;; example, mail/attach.pbm), adjust `image-directory'
349 (setq dir (file-name-directory dir))
350 (while (setq parent (file-name-directory img))
351 (setq img (directory-file-name parent)
352 dir (expand-file-name "../" dir))))
353 (setq image-directory-load-path dir))
355 ;; If `image-directory-load-path' isn't Emacs' image directory,
356 ;; it's probably a user preference, so use it. Then use a
357 ;; relative setting if possible; otherwise, use
358 ;; `image-directory-load-path'.
360 ;; User-modified image-load-path?
361 ((and image-directory-load-path
362 (not (equal image-directory-load-path
363 (file-name-as-directory
364 (expand-file-name "images" data-directory)))))
365 (setq image-directory image-directory-load-path))
366 ;; Try relative setting.
367 ((let (library-name d1ei d2ei)
368 ;; First, find library in the load-path.
369 (setq library-name (locate-library library))
370 (if (not library-name)
371 (error "Cannot find library %s in load-path" library))
372 ;; And then set image-directory relative to that.
375 d2ei (file-name-as-directory
377 (concat (file-name-directory library-name) "../../etc/images")))
379 d1ei (file-name-as-directory
381 (concat (file-name-directory library-name) "../etc/images"))))
382 (setq image-directory
383 ;; Set it to nil if image is not found.
384 (cond ((file-exists-p (expand-file-name image d2ei)) d2ei)
385 ((file-exists-p (expand-file-name image d1ei)) d1ei)))))
386 ;; Use Emacs' image directory.
387 (image-directory-load-path
388 (setq image-directory image-directory-load-path))
390 (message "Could not find image %s for library %s" image library))
392 (error "Could not find image %s for library %s" image library)))
394 ;; Return an augmented `path' or `load-path'.
395 (nconc (list image-directory)
396 (delete image-directory (copy-sequence (or path load-path))))))
398 (defun gmm-customize-mode (&optional mode)
399 "Customize customization group for MODE.
400 If mode is nil, use `major-mode' of the curent buffer."
404 (intern (let ((mode (symbol-name major-mode)))
405 (string-match "^\\(.+\\)-mode$" mode)
406 (match-string 1 mode))))))
410 ;; arch-tag: e0b60920-2ce6-40c1-bfc0-cadbbe26b602
411 ;;; gmm-utils.el ends here