* gmm-utils.el (gmm-image-load-path): Add alias.
[gnus] / lisp / gmm-utils.el
1 ;;; gmm-utils.el --- Utility functions for Gnus, Message and MML
2
3 ;; Copyright (C) 2006 Free Software Foundation, Inc.
4
5 ;; Author: Reiner Steib <reiner.steib@gmx.de>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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.
24
25 ;;; Commentary:
26
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.
30
31 ;;; Code:
32
33 (defgroup gmm nil
34   "Utility functions for Gnus, Message and MML"
35   :prefix "gmm-"
36   :version "23.0" ;; No Gnus
37   :group 'lisp)
38
39 ;; Helper functions from `gnus-utils.el': gmm-verbose, gmm-message, gmm-error
40
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."
47   :type 'integer
48   :group 'gmm)
49
50 ;;;###autoload
51 (defun gmm-message (level &rest args)
52   "If LEVEL is lower than `gmm-verbose' print ARGS using `message'.
53
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
57 inside loops."
58   (if (<= level gmm-verbose)
59       (apply 'message args)
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
62     ;; from `message'.
63     (apply 'format args)))
64
65 ;;;###autoload
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)
70     (apply 'message args)
71     (ding)
72     (let (duration)
73       (when (and (floatp level)
74                  (not (zerop (setq duration (* 10 (- level (floor level)))))))
75         (sit-for duration))))
76   nil)
77
78 ;;;###autoload
79 (defun gmm-widget-p (symbol)
80   "Non-nil iff SYMBOL is a widget."
81   (get symbol 'widget-type))
82
83 ;; Copy of the `nnmail-lazy' code from `nnmail.el':
84 (define-widget 'gmm-lazy 'default
85   "Base widget for recursive datastructures.
86
87 This is copy of the `lazy' widget in Emacs 22.1 provided for compatibility."
88   :format "%{%t%}: %v"
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))
101                                 :value-inline))
102   :default-get (lambda (widget)
103                  (widget-default-get
104                   (widget-convert (widget-get widget :type))))
105   :match (lambda (widget value)
106            (widget-apply (widget-convert (widget-get widget :type))
107                          :match value))
108   :validate (lambda (widget)
109               (widget-apply (car (widget-get widget :children)) :validate)))
110
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
113 ;; interface.
114
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...
117 ;;
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
125 ;;
126 ;; Combinations of mouse-[23] plus shift and/or controll might be overkill.
127 ;;
128 ;; Then use (plist-get rs-command :none), (plist-get rs-command :shift)
129
130 (define-widget 'gmm-tool-bar-item (if (gmm-widget-p 'lazy) 'lazy 'gmm-lazy)
131   "Tool bar list item."
132   :tag "Tool bar item"
133   :type '(choice
134           (list :tag "Command and Icon"
135                 (function :tag "Command")
136                 (string :tag "Icon file")
137                 (choice
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"))))
148
149 (define-widget 'gmm-tool-bar-zap-list (if (gmm-widget-p 'lazy) 'lazy 'gmm-lazy)
150   "Tool bar zap list."
151   :tag "Tool bar zap list"
152   :type '(choice (const :tag "Zap all" t)
153                  (const :tag "Keep all" nil)
154                  (list
155                   ;; :value
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)
160                   (set :inline t
161                        (const new-file)
162                        (const open-file)
163                        (const dired)
164                        (const kill-buffer)
165                        (const save-buffer)
166                        (const write-file)
167                        (const undo)
168                        (const cut)
169                        (const copy)
170                        (const paste)
171                        (const search-forward)
172                        (const print-buffer)
173                        (const customize)
174                        (const help))
175                   (repeat :inline t
176                           :tag "Other"
177                           (symbol :tag "Icon item")))))
178
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.
184 ;;   ;;
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))))
190 ;;       (none -1))
191 ;;     (max (if (integerp cells) cells none)
192 ;;       (if (integerp plane) plane none))))
193
194 (defcustom gmm-tool-bar-style
195   (if (and (boundp 'tool-bar-mode)
196            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)))))
201       'gnome
202     'retro)
203   "Prefered tool bar style."
204   :type '(choice (const :tag "GNOME style" 'gnome)
205                  (const :tag "Retro look"  'retro))
206   :group 'gmm)
207
208 (defvar tool-bar-map)
209
210 ;;;###autoload
211 (defun gmm-tool-bar-from-list (icon-list zap-list default-map)
212   "Make a tool bar from ICON-LIST.
213
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'.
220
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.
226
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)
231                           (make-sparse-keymap)
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)))
237     (mapc (lambda (el)
238             (let ((command (car el))
239                   (icon (nth 1 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
255                                 icon command
256                                 (intern icon) ;; reuse icon or fmap here?
257                                 tool-bar-map props)
258                        ;; Emacs 21 compatibility:
259                        (apply 'tool-bar-add-item
260                               icon command
261                               (intern icon)
262                               props)))
263                     (t ;; A menu command
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)
269                                 props)
270                        ;; Emacs 21 compatibility:
271                        (apply 'tool-bar-add-item-from-menu
272                               command icon (symbol-value fmap)
273                               props))))
274               t))
275           (if (symbolp icon-list)
276               (eval icon-list)
277             icon-list))
278     tool-bar-map))
279
280 ;; WARNING: The following is subject to change.  Don't rely on it yet.
281
282 ;; From MH-E without modifications:
283
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)))
289     (if defined-p
290         `(defalias ',name ',function)
291       `(defun ,name ,arg-list ,@body))))
292
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."
297   nil)
298
299 ;; From MH-E with modifications:
300
301 (gmm-defun-compat gmm-image-load-path-for-library
302   image-load-path-for-library (library image &optional path no-error)
303   "Return a suitable search path for images relative to LIBRARY.
304
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'.
308
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
312 instead of a path.
313
314 If NO-ERROR is non-nil, don't signal an error if no suitable path
315 for can be found.
316
317 Here is an example that uses a common idiom to provide
318 compatibility with versions of Emacs that lack the variable
319 `image-load-path':
320
321   (let ((load-path
322          (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
323         (image-load-path
324          (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\" 'image-load-path)))
325     (mh-tool-bar-folder-buttons-init))
326
327 This function is used by Emacs versions that don't have
328 `image-load-path-for-library'."
329   (unless library (error "No library specified"))
330   (unless image   (error "No image specified"))
331   (let ((image-directory))
332     (cond
333      ;; Try relative setting.
334      ((let (library-name d1ei d2ei)
335         ;; First, find library in the load-path.
336         (setq library-name (locate-library library))
337         (if (not library-name)
338             (error "Cannot find library %s in load-path" library))
339         ;; And then set image-directory relative to that.
340         (setq
341          ;; Go down 2 levels.
342          d2ei (expand-file-name
343                (concat (file-name-directory library-name) "../../etc/images"))
344          ;; Go down 1 level.
345          d1ei (expand-file-name
346                (concat (file-name-directory library-name) "../etc/images")))
347         (setq image-directory
348               ;; Set it to nil if image is not found.
349               (cond ((file-exists-p (expand-file-name image d2ei)) d2ei)
350                     ((file-exists-p (expand-file-name image d1ei)) d1ei)))))
351      ;; Check for images in image-load-path or load-path.
352      ((let ((img image)
353             (dir (or
354                   ;; Images in image-load-path.
355                   (gmm-image-search-load-path image)
356                   ;; Images in load-path.
357                   (locate-library image)))
358             parent)
359         ;; Since the image might be in a nested directory (for
360         ;; example, mail/attach.pbm), adjust `image-directory'
361         ;; accordingly.
362         (and dir
363              (setq dir (file-name-directory dir))
364              (progn
365                (while (setq parent (file-name-directory img))
366                  (setq img (directory-file-name parent)
367                        dir (expand-file-name "../" dir)))
368                (setq image-directory dir)))))
369      (no-error
370       ;; In this case we will return a nil element
371       (gmm-message 1 "Could not find image %s for library %s" image library))
372      (t
373       (error "Could not find image %s for library %s" image library)))
374
375     ;; Return augmented `image-load-path' or `load-path'.
376     (cond ((eq path t)
377            image-directory)
378           ((and path (symbolp path))
379            (nconc (list image-directory)
380                   (delete image-directory
381                           (if (boundp path)
382                               (copy-sequence (symbol-value path))
383                             nil))))
384           (t
385            (nconc (list image-directory)
386                   (delete image-directory (copy-sequence load-path)))))))
387
388 ;; No time to do it right ATM, sorry...
389 (defalias 'gmm-image-load-path 'gmm-image-load-path-for-library)
390
391 (defun gmm-customize-mode (&optional mode)
392   "Customize customization group for MODE.
393 If mode is nil, use `major-mode' of the curent buffer."
394   (interactive)
395   (customize-group
396    (or mode
397        (intern (let ((mode (symbol-name major-mode)))
398                  (string-match "^\\(.+\\)-mode$" mode)
399                  (match-string 1 mode))))))
400
401 (provide 'gmm-utils)
402
403 ;; arch-tag: e0b60920-2ce6-40c1-bfc0-cadbbe26b602
404 ;;; gmm-utils.el ends here