gnus-art.el (gnus-article-goto-part): Fix last change
[gnus] / lisp / gmm-utils.el
1 ;;; gmm-utils.el --- Utility functions for Gnus, Message and MML
2
3 ;; Copyright (C) 2006, 2007, 2008, 2009, 2010
4 ;;   Free Software Foundation, Inc.
5
6 ;; Author: Reiner Steib <reiner.steib@gmx.de>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; This library provides self-contained utility functions.  The functions are
27 ;; used in Gnus, Message and MML, but within this library there are no
28 ;; dependencies on Gnus, Message, or MML.
29
30 ;;; Code:
31
32 (defgroup gmm nil
33   "Utility functions for Gnus, Message and MML."
34   :prefix "gmm-"
35   :version "22.1" ;; Gnus 5.10.9
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 did.  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-regexp-concat (regexp)
51   "Potentially concat a list of regexps into a single one.
52 The concatenation is done with logical ORs."
53   (cond ((null regexp)
54          nil)
55         ((stringp regexp)
56          regexp)
57         ((listp regexp)
58          (mapconcat (lambda (elt) (concat "\\(" elt "\\)"))
59                     regexp
60                     "\\|"))))
61
62 ;;;###autoload
63 (defun gmm-message (level &rest args)
64   "If LEVEL is lower than `gmm-verbose' print ARGS using `message'.
65
66 Guideline for numbers:
67 1 - error messages
68 3 - non-serious error messages
69 5 - messages for things that take a long time
70 7 - not very important messages on stuff
71 9 - messages inside loops."
72   (if (<= level gmm-verbose)
73       (apply 'message args)
74     ;; We have to do this format thingy here even if the result isn't
75     ;; shown - the return value has to be the same as the return value
76     ;; from `message'.
77     (apply 'format args)))
78
79 ;;;###autoload
80 (defun gmm-error (level &rest args)
81   "Beep an error if LEVEL is equal to or less than `gmm-verbose'.
82 ARGS are passed to `message'."
83   (when (<= (floor level) gmm-verbose)
84     (apply 'message args)
85     (ding)
86     (let (duration)
87       (when (and (floatp level)
88                  (not (zerop (setq duration (* 10 (- level (floor level)))))))
89         (sit-for duration))))
90   nil)
91
92 ;;;###autoload
93 (defun gmm-widget-p (symbol)
94   "Non-nil if SYMBOL is a widget."
95   (get symbol 'widget-type))
96
97 (autoload 'widget-create-child-value "wid-edit")
98 (autoload 'widget-convert "wid-edit")
99 (autoload 'widget-default-get "wid-edit")
100
101 ;; Copy of the `nnmail-lazy' code from `nnmail.el':
102 (define-widget 'gmm-lazy 'default
103   "Base widget for recursive datastructures.
104
105 This is a copy of the `lazy' widget in Emacs 22.1 provided for compatibility."
106   :format "%{%t%}: %v"
107   :convert-widget 'widget-value-convert-widget
108   :value-create (lambda (widget)
109                   (let ((value (widget-get widget :value))
110                         (type (widget-get widget :type)))
111                     (widget-put widget :children
112                                 (list (widget-create-child-value
113                                        widget (widget-convert type) value)))))
114   :value-delete 'widget-children-value-delete
115   :value-get (lambda (widget)
116                (widget-value (car (widget-get widget :children))))
117   :value-inline (lambda (widget)
118                   (widget-apply (car (widget-get widget :children))
119                                 :value-inline))
120   :default-get (lambda (widget)
121                  (widget-default-get
122                   (widget-convert (widget-get widget :type))))
123   :match (lambda (widget value)
124            (widget-apply (widget-convert (widget-get widget :type))
125                          :match value))
126   :validate (lambda (widget)
127               (widget-apply (car (widget-get widget :children)) :validate)))
128
129 ;; Note: The format of `gmm-tool-bar-item' may change if some future Emacs
130 ;; version will provide customizable tool bar buttons using a different
131 ;; interface.
132
133 ;; TODO: Extend API so that the "Command" entry can be a function or a plist.
134 ;; In case of a list it should have the format...
135 ;;
136 ;;  (:none command-without-modifier
137 ;;   :shift command-with-shift-pressed
138 ;;   :control command-with-ctrl-pressed
139 ;;   :control-shift command-with-control-and-shift-pressed
140 ;;   ;; mouse-2 and mouse-3 can't be used in Emacs yet.
141 ;;   :mouse-2 command-on-mouse-2-press
142 ;;   :mouse-3 command-on-mouse-3-press) ;; typically a menu of related commands
143 ;;
144 ;; Combinations of mouse-[23] plus shift and/or controll might be overkill.
145 ;;
146 ;; Then use (plist-get rs-command :none), (plist-get rs-command :shift)
147
148 (define-widget 'gmm-tool-bar-item (if (gmm-widget-p 'lazy) 'lazy 'gmm-lazy)
149   "Tool bar list item."
150   :tag "Tool bar item"
151   :type '(choice
152           (list :tag "Command and Icon"
153                 (function :tag "Command")
154                 (string :tag "Icon file")
155                 (choice
156                  (const :tag "Default map" nil)
157                  ;; Note: Usually we need non-nil attributes if map is t.
158                  (const :tag "No menu" t)
159                  (sexp :tag "Other map"))
160                 (plist :inline t :tag "Properties"))
161           (list :tag "Separator"
162                 (const :tag "No command" gmm-ignore)
163                 (string :tag "Icon file")
164                 (const :tag "No map")
165                 (plist :inline t :tag "Properties"))))
166
167 (define-widget 'gmm-tool-bar-zap-list (if (gmm-widget-p 'lazy) 'lazy 'gmm-lazy)
168   "Tool bar zap list."
169   :tag "Tool bar zap list"
170   :type '(choice (const :tag "Zap all" t)
171                  (const :tag "Keep all" nil)
172                  (list
173                   ;; :value
174                   ;; Work around (bug in customize?), see
175                   ;; <news:v9is48jrj1.fsf@marauder.physik.uni-ulm.de>
176                   ;; (new-file open-file dired kill-buffer write-file
177                   ;;        print-buffer customize help)
178                   (set :inline t
179                        (const new-file)
180                        (const open-file)
181                        (const dired)
182                        (const kill-buffer)
183                        (const save-buffer)
184                        (const write-file)
185                        (const undo)
186                        (const cut)
187                        (const copy)
188                        (const paste)
189                        (const search-forward)
190                        (const print-buffer)
191                        (const customize)
192                        (const help))
193                   (repeat :inline t
194                           :tag "Other"
195                           (symbol :tag "Icon item")))))
196
197 ;; (defun gmm-color-cells (&optional display)
198 ;;   "Return the number of color cells supported by DISPLAY.
199 ;; Compatibility function."
200 ;;   ;; `display-color-cells' doesn't return more than 256 even if color depth is
201 ;;   ;; > 8 in Emacs 21.
202 ;;   ;;
203 ;;   ;; Feel free to add proper XEmacs support.
204 ;;   (let* ((cells (and (fboundp 'display-color-cells)
205 ;;                   (display-color-cells display)))
206 ;;       (plane (and (fboundp 'x-display-planes)
207 ;;                   (ash 1 (x-display-planes))))
208 ;;       (none -1))
209 ;;     (max (if (integerp cells) cells none)
210 ;;       (if (integerp plane) plane none))))
211
212 (defcustom gmm-tool-bar-style
213   (if (and (boundp 'tool-bar-mode)
214            tool-bar-mode
215            (and (fboundp 'display-visual-class)
216                 (not (memq (display-visual-class)
217                            (list 'static-gray 'gray-scale
218                                  'static-color 'pseudo-color)))))
219       'gnome
220     'retro)
221   "Prefered tool bar style."
222   :type '(choice (const :tag "GNOME style" gnome)
223                  (const :tag "Retro look"  retro))
224   :group 'gmm)
225