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