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