(gmm-tool-bar-item, gmm-tool-bar-zap-list): Don't autoload. From Steve Youngs.
[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   :group 'lisp)
37
38 ;; Helper functions from `gnus-utils.el': gmm-verbose, gmm-message, gmm-error
39
40 (defcustom gmm-verbose 7
41   "Integer that says how verbose gmm should be.
42 The higher the number, the more messages will flash to say what
43 it done.  At zero, it will be totally mute; at five, it will
44 display most important messages; and at ten, it will keep on
45 jabbering all the time."
46   :type 'integer
47   :group 'gmm)
48
49 ;;;###autoload
50 (defun gmm-message (level &rest args)
51   "If LEVEL is lower than `gmm-verbose' print ARGS using `message'.
52
53 Guideline for numbers:
54 1 - error messages, 3 - non-serious error messages, 5 - messages for things
55 that take a long time, 7 - not very important messages on stuff, 9 - messages
56 inside loops."
57   (if (<= level gmm-verbose)
58       (apply 'message args)
59     ;; We have to do this format thingy here even if the result isn't
60     ;; shown - the return value has to be the same as the return value
61     ;; from `message'.
62     (apply 'format args)))
63
64 ;;;###autoload
65 (defun gmm-error (level &rest args)
66   "Beep an error if LEVEL is equal to or less than `gmm-verbose'.
67 ARGS are passed to `message'."
68   (when (<= (floor level) gmm-verbose)
69     (apply 'message args)
70     (ding)
71     (let (duration)
72       (when (and (floatp level)
73                  (not (zerop (setq duration (* 10 (- level (floor level)))))))
74         (sit-for duration))))
75   nil)
76
77 ;;;###autoload
78 (defun gmm-widget-p (symbol)
79   "Non-nil iff SYMBOL is a widget."
80   (get symbol 'widget-type))
81
82 ;; Copy of the `nnmail-lazy' code from `nnmail.el':
83 (define-widget 'gmm-lazy 'default
84   "Base widget for recursive datastructures.
85
86 This is copy of the `lazy' widget in Emacs 22.1 provided for compatibility."
87   :format "%{%t%}: %v"
88   :convert-widget 'widget-value-convert-widget
89   :value-create (lambda (widget)
90                   (let ((value (widget-get widget :value))
91                         (type (widget-get widget :type)))
92                     (widget-put widget :children
93                                 (list (widget-create-child-value
94                                        widget (widget-convert type) value)))))
95   :value-delete 'widget-children-value-delete
96   :value-get (lambda (widget)
97                (widget-value (car (widget-get widget :children))))
98   :value-inline (lambda (widget)
99                   (widget-apply (car (widget-get widget :children))
100                                 :value-inline))
101   :default-get (lambda (widget)
102                  (widget-default-get
103                   (widget-convert (widget-get widget :type))))
104   :match (lambda (widget value)
105            (widget-apply (widget-convert (widget-get widget :type))
106                          :match value))
107   :validate (lambda (widget)
108               (widget-apply (car (widget-get widget :children)) :validate)))
109
110 ;; Note: The format of `gmm-tool-bar-item' may change if some future Emacs
111 ;; version will provide customizable tool bar buttons using a different
112 ;; interface.
113
114 (define-widget 'gmm-tool-bar-item (if (gmm-widget-p 'lazy) 'lazy 'gmm-lazy)
115   "Tool bar list item."
116   :tag "Tool bar item"
117   :type '(choice
118           (list :tag "Command and Icon"
119                 (function :tag "Command")
120                 (string :tag "Icon file")
121                 (choice
122                  (const :tag "Default map" nil)
123                  ;; Note: Usually we need non-nil attributes if map is t.
124                  (const :tag "No menu" t)
125                  (sexp :tag "Other map"))
126                 (plist :inline t :tag "Properties"))
127           (list :tag "Separator"
128                 (const :tag "No command" gmm-ignore)
129                 (string :tag "Icon file")
130                 (const :tag "No map")
131                 (plist :inline t :tag "Properties"))))
132
133 (define-widget 'gmm-tool-bar-zap-list (if (gmm-widget-p 'lazy) 'lazy 'gmm-lazy)
134   "Tool bar zap list."
135   :tag "Tool bar zap list"
136   :type '(choice (const :tag "Zap all" t)
137                  (const :tag "Keep all" nil)
138                  (list
139                   ;; :value
140                   ;; Work around (bug in customize?), see
141                   ;; <news:v9is48jrj1.fsf@marauder.physik.uni-ulm.de>
142                   ;; (new-file open-file dired kill-buffer write-file
143                   ;;        print-buffer customize help)
144                   (set :inline t
145                        (const new-file)
146                        (const open-file)
147                        (const dired)
148                        (const kill-buffer)
149                        (const save-buffer)
150                        (const write-file)
151                        (const undo)
152                        (const cut)
153                        (const copy)
154                        (const paste)
155                        (const search-forward)
156                        (const print-buffer)
157                        (const customize)
158                        (const help))
159                   (repeat :inline t
160                           :tag "Other"
161                           (symbol :tag "Icon item")))))
162
163 (defvar tool-bar-map)
164
165 ;;;###autoload
166 (defun gmm-tool-bar-from-list (icon-list zap-list default-map)
167   "Make a tool bar from ICON-LIST.
168
169 Within each entry of ICON-LIST, the first element is a menu
170 command, the second element is an icon file name and the third
171 element is a test function.  You can use \\[describe-key]
172 <menu-entry> to find out the name of a menu command.  The fourth
173 and all following elements are passed a the PROPS argument to the
174 function `tool-bar-local-item'.
175
176 If ZAP-LIST is a list, remove those item from the default
177 `tool-bar-map'.  If it is t, start with a new sparse map.  You
178 can use \\[describe-key] <icon> to find out the name of an icon
179 item.  When \\[describe-key] <icon> shows \"<tool-bar> <new-file>
180 runs the command find-file\", then use `new-file' in ZAP-LIST.
181
182 DEFAULT-MAP specifies the default key map for ICON-LIST."
183   (let (;; For Emacs 21, we must let-bind `tool-bar-map'.  In Emacs 22, we
184         ;; could use some other local variable.
185         (tool-bar-map (if (eq zap-list t)
186                           (make-sparse-keymap)
187                         (copy-keymap tool-bar-map))))
188     (when (listp zap-list)
189       ;; Zap some items which aren't relevant for this mode and take up space.
190       (dolist (key zap-list)
191         (define-key tool-bar-map (vector key) nil)))
192     (mapc (lambda (el)
193             (let ((command (car el))
194                   (icon (nth 1 el))
195                   (fmap (or (nth 2 el) default-map))
196                   (props  (cdr (cdr (cdr el)))) )
197               ;; command may stem from different from-maps:
198               (cond ((eq command 'gmm-ignore)
199                      ;; The dummy `gmm-ignore', see `gmm-tool-bar-item'
200                      ;; widget.  Suppress tooltip by adding `:enable nil'.
201                      (if (fboundp 'tool-bar-local-item)
202                          (apply 'tool-bar-local-item icon nil nil
203                                 tool-bar-map :enable nil props)
204                        ;; (tool-bar-local-item ICON DEF KEY MAP &rest PROPS)
205                        ;; (tool-bar-add-item ICON DEF KEY &rest PROPS)
206                        (apply 'tool-bar-add-item icon nil nil :enable nil props)))
207                     ((equal fmap t) ;; Not a menu command
208                      (if (fboundp 'tool-bar-local-item)
209                          (apply 'tool-bar-local-item
210                                 icon command
211                                 (intern icon) ;; reuse icon or fmap here?
212                                 tool-bar-map props)
213                        ;; Emacs 21 compatibility:
214                        (apply 'tool-bar-add-item
215                               icon command
216                               (intern icon)
217                               props)))
218                     (t ;; A menu command
219                      (if (fboundp 'tool-bar-local-item-from-menu)
220                          (apply 'tool-bar-local-item-from-menu
221                                 ;; (apply 'tool-bar-local-item icon def key
222                                 ;; tool-bar-map props)
223                                 command icon tool-bar-map (symbol-value fmap)
224                                 props)
225                        ;; Emacs 21 compatibility:
226                        (apply 'tool-bar-add-item-from-menu
227                               command icon (symbol-value fmap)
228                               props))))
229               t))
230           (if (symbolp icon-list)
231               (eval icon-list)
232             icon-list))
233     tool-bar-map))
234
235 (provide 'gmm-utils)
236
237 ;; arch-tag: e0b60920-2ce6-40c1-bfc0-cadbbe26b602
238 ;;; gmm-utils.el ends here