(gmm-image-load-path): Added arguments LIBRARY, IMAGE and PATH. Don't modify
[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 (define-widget 'gmm-tool-bar-item (if (gmm-widget-p 'lazy) 'lazy 'gmm-lazy)
116   "Tool bar list item."
117   :tag "Tool bar item"
118   :type '(choice
119           (list :tag "Command and Icon"
120                 (function :tag "Command")
121                 (string :tag "Icon file")
122                 (choice
123                  (const :tag "Default map" nil)
124                  ;; Note: Usually we need non-nil attributes if map is t.
125                  (const :tag "No menu" t)
126                  (sexp :tag "Other map"))
127                 (plist :inline t :tag "Properties"))
128           (list :tag "Separator"
129                 (const :tag "No command" gmm-ignore)
130                 (string :tag "Icon file")
131                 (const :tag "No map")
132                 (plist :inline t :tag "Properties"))))
133
134 (define-widget 'gmm-tool-bar-zap-list (if (gmm-widget-p 'lazy) 'lazy 'gmm-lazy)
135   "Tool bar zap list."
136   :tag "Tool bar zap list"
137   :type '(choice (const :tag "Zap all" t)
138                  (const :tag "Keep all" nil)
139                  (list
140                   ;; :value
141                   ;; Work around (bug in customize?), see
142                   ;; <news:v9is48jrj1.fsf@marauder.physik.uni-ulm.de>
143                   ;; (new-file open-file dired kill-buffer write-file
144                   ;;        print-buffer customize help)
145                   (set :inline t
146                        (const new-file)
147                        (const open-file)
148                        (const dired)
149                        (const kill-buffer)
150                        (const save-buffer)
151                        (const write-file)
152                        (const undo)
153                        (const cut)
154                        (const copy)
155                        (const paste)
156                        (const search-forward)
157                        (const print-buffer)
158                        (const customize)
159                        (const help))
160                   (repeat :inline t
161                           :tag "Other"
162                           (symbol :tag "Icon item")))))
163
164 (defvar tool-bar-map)
165
166 ;;;###autoload
167 (defun gmm-tool-bar-from-list (icon-list zap-list default-map)
168   "Make a tool bar from ICON-LIST.
169
170 Within each entry of ICON-LIST, the first element is a menu
171 command, the second element is an icon file name and the third
172 element is a test function.  You can use \\[describe-key]
173 <menu-entry> to find out the name of a menu command.  The fourth
174 and all following elements are passed a the PROPS argument to the
175 function `tool-bar-local-item'.
176
177 If ZAP-LIST is a list, remove those item from the default
178 `tool-bar-map'.  If it is t, start with a new sparse map.  You
179 can use \\[describe-key] <icon> to find out the name of an icon
180 item.  When \\[describe-key] <icon> shows \"<tool-bar> <new-file>
181 runs the command find-file\", then use `new-file' in ZAP-LIST.
182
183 DEFAULT-MAP specifies the default key map for ICON-LIST."
184   (let (;; For Emacs 21, we must let-bind `tool-bar-map'.  In Emacs 22, we
185         ;; could use some other local variable.
186         (tool-bar-map (if (eq zap-list t)
187                           (make-sparse-keymap)
188                         (copy-keymap tool-bar-map))))
189     (when (listp zap-list)
190       ;; Zap some items which aren't relevant for this mode and take up space.
191       (dolist (key zap-list)
192         (define-key tool-bar-map (vector key) nil)))
193     (mapc (lambda (el)
194             (let ((command (car el))
195                   (icon (nth 1 el))
196                   (fmap (or (nth 2 el) default-map))
197                   (props  (cdr (cdr (cdr el)))) )
198               ;; command may stem from different from-maps:
199               (cond ((eq command 'gmm-ignore)
200                      ;; The dummy `gmm-ignore', see `gmm-tool-bar-item'
201                      ;; widget.  Suppress tooltip by adding `:enable nil'.
202                      (if (fboundp 'tool-bar-local-item)
203                          (apply 'tool-bar-local-item icon nil nil
204                                 tool-bar-map :enable nil props)
205                        ;; (tool-bar-local-item ICON DEF KEY MAP &rest PROPS)
206                        ;; (tool-bar-add-item ICON DEF KEY &rest PROPS)
207                        (apply 'tool-bar-add-item icon nil nil :enable nil props)))
208                     ((equal fmap t) ;; Not a menu command
209                      (if (fboundp 'tool-bar-local-item)
210                          (apply 'tool-bar-local-item
211                                 icon command
212                                 (intern icon) ;; reuse icon or fmap here?
213                                 tool-bar-map props)
214                        ;; Emacs 21 compatibility:
215                        (apply 'tool-bar-add-item
216                               icon command
217                               (intern icon)
218                               props)))
219                     (t ;; A menu command
220                      (if (fboundp 'tool-bar-local-item-from-menu)
221                          (apply 'tool-bar-local-item-from-menu
222                                 ;; (apply 'tool-bar-local-item icon def key
223                                 ;; tool-bar-map props)
224                                 command icon tool-bar-map (symbol-value fmap)
225                                 props)
226                        ;; Emacs 21 compatibility:
227                        (apply 'tool-bar-add-item-from-menu
228                               command icon (symbol-value fmap)
229                               props))))
230               t))
231           (if (symbolp icon-list)
232               (eval icon-list)
233             icon-list))
234     tool-bar-map))
235
236 ;; WARNING: The following is subject to change.  Don't rely on it yet.
237
238 ;; From MH-E without modifications:
239
240 (defmacro gmm-defun-compat (name function arg-list &rest body)
241   "Create function NAME.
242 If FUNCTION exists, then NAME becomes an alias for FUNCTION.
243 Otherwise, create function NAME with ARG-LIST and BODY."
244   (let ((defined-p (fboundp function)))
245     (if defined-p
246         `(defalias ',name ',function)
247       `(defun ,name ,arg-list ,@body))))
248
249 (gmm-defun-compat gmm-image-search-load-path
250   image-search-load-path (file &optional path)
251   "Emacs 21 and XEmacs don't have `image-search-load-path'.
252 This function returns nil on those systems."
253   nil)
254
255 ;; From MH-E with modifications:
256
257 (defvar gmm-image-load-path nil
258   "Directory where images are found.
259 See the function `gmm-image-load-path'.")
260
261 (defun gmm-image-load-path (library image &optional path)
262   "Return a suitable search path for images of LIBRARY.
263
264 Images for LIBRARY are found in \"../../etc/images\" relative to
265 the files in \"lisp/LIBRARY\", in `image-load-path', or in
266 `load-path'.
267
268 This function returns value of `load-path' augmented with the
269 path to IMAGE.  If PATH is given, it is used instead of
270 `load-path'."
271   (unless library (error "No library specified."))
272   (unless image   (error "No image specified."))
273   (cond (gmm-image-load-path) ;; User setting exists.
274         ((let (gmm-library-name) ;; Try relative setting
275            ;; First, find library in the load-path.
276            (setq gmm-library-name (locate-library library))
277            (if (not gmm-library-name)
278                (error "Cannot find library `%s' in load-path" library))
279            ;; And then set gmm-image-load-path relative to that.
280            (setq gmm-image-load-path
281                  (expand-file-name (concat
282                                     (file-name-directory gmm-library-name)
283                                     "../../etc/images")))
284            (file-exists-p (expand-file-name image gmm-image-load-path))))
285         ((gmm-image-search-load-path image)
286          ;; Images in image-load-path.
287          (setq gmm-image-load-path
288                (file-name-directory (gmm-image-search-load-path image))))
289         ((locate-library image)
290          ;; Images in load-path.
291          (setq gmm-image-load-path
292                (file-name-directory (locate-library image)))))
293   ;;
294   (unless (file-exists-p gmm-image-load-path)
295     (error "Directory `%s' in gmm-image-load-path does not exist"
296              gmm-image-load-path))
297   (unless (file-exists-p (expand-file-name image gmm-image-load-path))
298     (error "Directory `%s' in gmm-image-load-path does not contain image `%s'."
299            gmm-image-load-path image))
300   ;; Return augmented `image-load-path' or `load-path'.
301   (cond ((and path (symbolp path))
302          (nconc (list gmm-image-load-path)
303                 (delete gmm-image-load-path (if (boundp path)
304                                                 (symbol-value path)
305                                               nil))))
306         (t
307          (nconc (list gmm-image-load-path)
308                 (delete gmm-image-load-path load-path)))))
309
310 (provide 'gmm-utils)
311
312 ;; arch-tag: e0b60920-2ce6-40c1-bfc0-cadbbe26b602
313 ;;; gmm-utils.el ends here