7a29fb3f231aab92e25a5d41106fd21728f5eb85
[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 (defvar tool-bar-map)
180
181 ;;;###autoload
182 (defun gmm-tool-bar-from-list (icon-list zap-list default-map)
183   "Make a tool bar from ICON-LIST.
184
185 Within each entry of ICON-LIST, the first element is a menu
186 command, the second element is an icon file name and the third
187 element is a test function.  You can use \\[describe-key]
188 <menu-entry> to find out the name of a menu command.  The fourth
189 and all following elements are passed a the PROPS argument to the
190 function `tool-bar-local-item'.
191
192 If ZAP-LIST is a list, remove those item from the default
193 `tool-bar-map'.  If it is t, start with a new sparse map.  You
194 can use \\[describe-key] <icon> to find out the name of an icon
195 item.  When \\[describe-key] <icon> shows \"<tool-bar> <new-file>
196 runs the command find-file\", then use `new-file' in ZAP-LIST.
197
198 DEFAULT-MAP specifies the default key map for ICON-LIST."
199   (let (;; For Emacs 21, we must let-bind `tool-bar-map'.  In Emacs 22, we
200         ;; could use some other local variable.
201         (tool-bar-map (if (eq zap-list t)
202                           (make-sparse-keymap)
203                         (copy-keymap tool-bar-map))))
204     (when (listp zap-list)
205       ;; Zap some items which aren't relevant for this mode and take up space.
206       (dolist (key zap-list)
207         (define-key tool-bar-map (vector key) nil)))
208     (mapc (lambda (el)
209             (let ((command (car el))
210                   (icon (nth 1 el))
211                   (fmap (or (nth 2 el) default-map))
212                   (props  (cdr (cdr (cdr el)))) )
213               ;; command may stem from different from-maps:
214               (cond ((eq command 'gmm-ignore)
215                      ;; The dummy `gmm-ignore', see `gmm-tool-bar-item'
216                      ;; widget.  Suppress tooltip by adding `:enable nil'.
217                      (if (fboundp 'tool-bar-local-item)
218                          (apply 'tool-bar-local-item icon nil nil
219                                 tool-bar-map :enable nil props)
220                        ;; (tool-bar-local-item ICON DEF KEY MAP &rest PROPS)
221                        ;; (tool-bar-add-item ICON DEF KEY &rest PROPS)
222                        (apply 'tool-bar-add-item icon nil nil :enable nil props)))
223                     ((equal fmap t) ;; Not a menu command
224                      (if (fboundp 'tool-bar-local-item)
225                          (apply 'tool-bar-local-item
226                                 icon command
227                                 (intern icon) ;; reuse icon or fmap here?
228                                 tool-bar-map props)
229                        ;; Emacs 21 compatibility:
230                        (apply 'tool-bar-add-item
231                               icon command
232                               (intern icon)
233                               props)))
234                     (t ;; A menu command
235                      (if (fboundp 'tool-bar-local-item-from-menu)
236                          (apply 'tool-bar-local-item-from-menu
237                                 ;; (apply 'tool-bar-local-item icon def key
238                                 ;; tool-bar-map props)
239                                 command icon tool-bar-map (symbol-value fmap)
240                                 props)
241                        ;; Emacs 21 compatibility:
242                        (apply 'tool-bar-add-item-from-menu
243                               command icon (symbol-value fmap)
244                               props))))
245               t))
246           (if (symbolp icon-list)
247               (eval icon-list)
248             icon-list))
249     tool-bar-map))
250
251 ;; WARNING: The following is subject to change.  Don't rely on it yet.
252
253 ;; From MH-E without modifications:
254
255 (defmacro gmm-defun-compat (name function arg-list &rest body)
256   "Create function NAME.
257 If FUNCTION exists, then NAME becomes an alias for FUNCTION.
258 Otherwise, create function NAME with ARG-LIST and BODY."
259   (let ((defined-p (fboundp function)))
260     (if defined-p
261         `(defalias ',name ',function)
262       `(defun ,name ,arg-list ,@body))))
263
264 (gmm-defun-compat gmm-image-search-load-path
265   image-search-load-path (file &optional path)
266   "Emacs 21 and XEmacs don't have `image-search-load-path'.
267 This function returns nil on those systems."
268   nil)
269
270 ;; From MH-E with modifications:
271
272 (defvar gmm-image-load-path nil
273   "Directory where images are found.
274 See the function `gmm-image-load-path'.")
275
276 (defun gmm-image-load-path (library image &optional path)
277   "Return a suitable search path for images of LIBRARY.
278
279 Images for LIBRARY are found in \"../../etc/images\" relative to
280 the files in \"lisp/LIBRARY\", in `image-load-path', or in
281 `load-path'.
282
283 This function returns value of `load-path' augmented with the
284 path to IMAGE.  If PATH is given, it is used instead of
285 `load-path'."
286   (unless library (error "No library specified."))
287   (unless image   (error "No image specified."))
288   (cond (gmm-image-load-path) ;; User setting exists.
289         ((let (gmm-library-name d1ei d2ei)
290            ;; Try relative setting
291            ;; First, find library in the load-path.
292            (setq gmm-library-name (locate-library library))
293            (if (not gmm-library-name)
294                (error "Cannot find library `%s' in load-path" library))
295            ;; And then set gmm-image-load-path relative to that.
296            (setq
297             ;; Go down 2 levels...
298             d2ei (expand-file-name
299                   (concat (file-name-directory gmm-library-name)
300                           "../../etc/images"))
301             ;; Go down 1 level...
302             d1ei (expand-file-name
303                   (concat (file-name-directory gmm-library-name)
304                           "../etc/images")))
305            (setq gmm-image-load-path
306                  ;; Set it to nil if image is not found...
307                  (cond ((file-exists-p (expand-file-name image d2ei)) d2ei)
308                        ((file-exists-p (expand-file-name image d1ei)) d1ei)))))
309         ((let ((img image)
310                (dir (or
311                      ;; Images in image-load-path.
312                      (gmm-image-search-load-path image)
313                      ;; Images in load-path.
314                      (locate-library image)))
315                parent)
316            (and dir
317                 (setq dir (file-name-directory dir))
318                 (progn
319                   ;; Remove subdirectories.
320                   (while (setq parent (file-name-directory img))
321                     (setq img (directory-file-name parent)
322                           dir (expand-file-name "../" dir)))
323                   (setq gmm-image-load-path dir))))))
324   ;;
325   (unless (file-exists-p gmm-image-load-path)
326     (error "Directory `%s' in gmm-image-load-path does not exist"
327              gmm-image-load-path))
328   (unless (file-exists-p (expand-file-name image gmm-image-load-path))
329     (error "Directory `%s' in gmm-image-load-path does not contain image `%s'."
330            gmm-image-load-path image))
331   ;; Return augmented `image-load-path' or `load-path'.
332   (cond ((and path (symbolp path))
333          (nconc (list gmm-image-load-path)
334                 (delete gmm-image-load-path
335                         (if (boundp path)
336                             (copy-sequence (symbol-value path))
337                           nil))))
338         (t
339          (nconc (list gmm-image-load-path)
340                 (delete gmm-image-load-path
341                         (copy-sequence load-path))))))
342
343 (provide 'gmm-utils)
344
345 ;; arch-tag: e0b60920-2ce6-40c1-bfc0-cadbbe26b602
346 ;;; gmm-utils.el ends here