(file-remote-p): Docstring fix.
[bpt/emacs.git] / lisp / gnus / gmm-utils.el
CommitLineData
18c06a99
RS
1;;; gmm-utils.el --- Utility functions for Gnus, Message and MML
2
e3fe4da0 3;; Copyright (C) 2006, 2007, 2008 Free Software Foundation, Inc.
18c06a99
RS
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
5a9dffec 12;; the Free Software Foundation; either version 3, or (at your option)
18c06a99
RS
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
8aed9ac5 29;; dependencies on Gnus, Message, or MML.
18c06a99
RS
30
31;;; Code:
32
d7ba2a01 33(require 'wid-edit)
18c06a99
RS
34
35(defgroup gmm nil
36 "Utility functions for Gnus, Message and MML"
37 :prefix "gmm-"
67099291 38 :version "22.1" ;; Gnus 5.10.9
18c06a99
RS
39 :group 'lisp)
40
41;; Helper functions from `gnus-utils.el': gmm-verbose, gmm-message, gmm-error
42
43(defcustom gmm-verbose 7
44 "Integer that says how verbose gmm should be.
45The higher the number, the more messages will flash to say what
46it done. At zero, it will be totally mute; at five, it will
47display most important messages; and at ten, it will keep on
48jabbering all the time."
49 :type 'integer
50 :group 'gmm)
51
01c52d31
MB
52;;;###autoload
53(defun gmm-regexp-concat (regexp)
54 "Potentially concat a list of regexps into a single one.
55The concatenation is done with logical ORs."
56 (cond ((null regexp)
57 nil)
58 ((stringp regexp)
59 regexp)
60 ((listp regexp)
61 (mapconcat (lambda (elt) (concat "\\(" elt "\\)"))
62 regexp
63 "\\|"))))
64
18c06a99
RS
65;;;###autoload
66(defun gmm-message (level &rest args)
67 "If LEVEL is lower than `gmm-verbose' print ARGS using `message'.
68
69Guideline for numbers:
701 - error messages, 3 - non-serious error messages, 5 - messages for things
71that take a long time, 7 - not very important messages on stuff, 9 - messages
72inside loops."
73 (if (<= level gmm-verbose)
74 (apply 'message args)
75 ;; We have to do this format thingy here even if the result isn't
76 ;; shown - the return value has to be the same as the return value
77 ;; from `message'.
78 (apply 'format args)))
79
80;;;###autoload
81(defun gmm-error (level &rest args)
82 "Beep an error if LEVEL is equal to or less than `gmm-verbose'.
83ARGS are passed to `message'."
84 (when (<= (floor level) gmm-verbose)
85 (apply 'message args)
86 (ding)
87 (let (duration)
88 (when (and (floatp level)
89 (not (zerop (setq duration (* 10 (- level (floor level)))))))
90 (sit-for duration))))
91 nil)
92
93;;;###autoload
94(defun gmm-widget-p (symbol)
e7f767c2 95 "Non-nil if SYMBOL is a widget."
18c06a99
RS
96 (get symbol 'widget-type))
97
98;; Copy of the `nnmail-lazy' code from `nnmail.el':
99(define-widget 'gmm-lazy 'default
100 "Base widget for recursive datastructures.
101
102This is copy of the `lazy' widget in Emacs 22.1 provided for compatibility."
103 :format "%{%t%}: %v"
104 :convert-widget 'widget-value-convert-widget
105 :value-create (lambda (widget)
106 (let ((value (widget-get widget :value))
107 (type (widget-get widget :type)))
108 (widget-put widget :children
109 (list (widget-create-child-value
110 widget (widget-convert type) value)))))
111 :value-delete 'widget-children-value-delete
112 :value-get (lambda (widget)
113 (widget-value (car (widget-get widget :children))))
114 :value-inline (lambda (widget)
115 (widget-apply (car (widget-get widget :children))
116 :value-inline))
117 :default-get (lambda (widget)
118 (widget-default-get
119 (widget-convert (widget-get widget :type))))
120 :match (lambda (widget value)
121 (widget-apply (widget-convert (widget-get widget :type))
122 :match value))
123 :validate (lambda (widget)
124 (widget-apply (car (widget-get widget :children)) :validate)))
125
126;; Note: The format of `gmm-tool-bar-item' may change if some future Emacs
127;; version will provide customizable tool bar buttons using a different
128;; interface.
129
130;; TODO: Extend API so that the "Command" entry can be a function or a plist.
131;; In case of a list it should have the format...
132;;
133;; (:none command-without-modifier
134;; :shift command-with-shift-pressed
135;; :control command-with-ctrl-pressed
136;; :control-shift command-with-control-and-shift-pressed
137;; ;; mouse-2 and mouse-3 can't be used in Emacs yet.
138;; :mouse-2 command-on-mouse-2-press
139;; :mouse-3 command-on-mouse-3-press) ;; typically a menu of related commands
140;;
141;; Combinations of mouse-[23] plus shift and/or controll might be overkill.
142;;
143;; Then use (plist-get rs-command :none), (plist-get rs-command :shift)
144
145(define-widget 'gmm-tool-bar-item (if (gmm-widget-p 'lazy) 'lazy 'gmm-lazy)
146 "Tool bar list item."
147 :tag "Tool bar item"
148 :type '(choice
149 (list :tag "Command and Icon"
150 (function :tag "Command")
151 (string :tag "Icon file")
152 (choice
153 (const :tag "Default map" nil)
154 ;; Note: Usually we need non-nil attributes if map is t.
155 (const :tag "No menu" t)
156 (sexp :tag "Other map"))
157 (plist :inline t :tag "Properties"))
158 (list :tag "Separator"
159 (const :tag "No command" gmm-ignore)
160 (string :tag "Icon file")
161 (const :tag "No map")
162 (plist :inline t :tag "Properties"))))
163
164(define-widget 'gmm-tool-bar-zap-list (if (gmm-widget-p 'lazy) 'lazy 'gmm-lazy)
165 "Tool bar zap list."
166 :tag "Tool bar zap list"
167 :type '(choice (const :tag "Zap all" t)
168 (const :tag "Keep all" nil)
169 (list
170 ;; :value
171 ;; Work around (bug in customize?), see
172 ;; <news:v9is48jrj1.fsf@marauder.physik.uni-ulm.de>
173 ;; (new-file open-file dired kill-buffer write-file
174 ;; print-buffer customize help)
175 (set :inline t
176 (const new-file)
177 (const open-file)
178 (const dired)
179 (const kill-buffer)
180 (const save-buffer)
181 (const write-file)
182 (const undo)
183 (const cut)
184 (const copy)
185 (const paste)
186 (const search-forward)
187 (const print-buffer)
188 (const customize)
189 (const help))
190 (repeat :inline t
191 :tag "Other"
192 (symbol :tag "Icon item")))))
193
194;; (defun gmm-color-cells (&optional display)
195;; "Return the number of color cells supported by DISPLAY.
196;; Compatibility function."
197;; ;; `display-color-cells' doesn't return more than 256 even if color depth is
198;; ;; > 8 in Emacs 21.
199;; ;;
200;; ;; Feel free to add proper XEmacs support.
201;; (let* ((cells (and (fboundp 'display-color-cells)
202;; (display-color-cells display)))
203;; (plane (and (fboundp 'x-display-planes)
204;; (ash 1 (x-display-planes))))
205;; (none -1))
206;; (max (if (integerp cells) cells none)
207;; (if (integerp plane) plane none))))
208
209(defcustom gmm-tool-bar-style
210 (if (and (boundp 'tool-bar-mode)
211 tool-bar-mode
212 (and (fboundp 'display-visual-class)
213 (not (memq (display-visual-class)
214 (list 'static-gray 'gray-scale
215 'static-color 'pseudo-color)))))
216 'gnome
217 'retro)
218 "Prefered tool bar style."
ba361211
MB
219 :type '(choice (const :tag "GNOME style" gnome)
220 (const :tag "Retro look" retro))
18c06a99
RS
221 :group 'gmm)
222
223(defvar tool-bar-map)
224
225;;;###autoload
226(defun gmm-tool-bar-from-list (icon-list zap-list default-map)
227 "Make a tool bar from ICON-LIST.
228
229Within each entry of ICON-LIST, the first element is a menu
230command, the second element is an icon file name and the third
231element is a test function. You can use \\[describe-key]
232<menu-entry> to find out the name of a menu command. The fourth
8aed9ac5 233and all following elements are passed as the PROPS argument to the
18c06a99
RS
234function `tool-bar-local-item'.
235
236If ZAP-LIST is a list, remove those item from the default
237`tool-bar-map'. If it is t, start with a new sparse map. You
238can use \\[describe-key] <icon> to find out the name of an icon
239item. When \\[describe-key] <icon> shows \"<tool-bar> <new-file>
240runs the command find-file\", then use `new-file' in ZAP-LIST.
241
242DEFAULT-MAP specifies the default key map for ICON-LIST."
243 (let (;; For Emacs 21, we must let-bind `tool-bar-map'. In Emacs 22, we
244 ;; could use some other local variable.
245 (tool-bar-map (if (eq zap-list t)
246 (make-sparse-keymap)
247 (copy-keymap tool-bar-map))))
248 (when (listp zap-list)
249 ;; Zap some items which aren't relevant for this mode and take up space.
250 (dolist (key zap-list)
251 (define-key tool-bar-map (vector key) nil)))
252 (mapc (lambda (el)
253 (let ((command (car el))
254 (icon (nth 1 el))
255 (fmap (or (nth 2 el) default-map))
256 (props (cdr (cdr (cdr el)))) )
257 ;; command may stem from different from-maps:
258 (cond ((eq command 'gmm-ignore)
259 ;; The dummy `gmm-ignore', see `gmm-tool-bar-item'
260 ;; widget. Suppress tooltip by adding `:enable nil'.
261 (if (fboundp 'tool-bar-local-item)
262 (apply 'tool-bar-local-item icon nil nil
263 tool-bar-map :enable nil props)
264 ;; (tool-bar-local-item ICON DEF KEY MAP &rest PROPS)
265 ;; (tool-bar-add-item ICON DEF KEY &rest PROPS)
266 (apply 'tool-bar-add-item icon nil nil :enable nil props)))
267 ((equal fmap t) ;; Not a menu command
268 (if (fboundp 'tool-bar-local-item)
269 (apply 'tool-bar-local-item
270 icon command
271 (intern icon) ;; reuse icon or fmap here?
272 tool-bar-map props)
273 ;; Emacs 21 compatibility:
274 (apply 'tool-bar-add-item
275 icon command
276 (intern icon)
277 props)))
278 (t ;; A menu command
279 (if (fboundp 'tool-bar-local-item-from-menu)
280 (apply 'tool-bar-local-item-from-menu
281 ;; (apply 'tool-bar-local-item icon def key
282 ;; tool-bar-map props)
283 command icon tool-bar-map (symbol-value fmap)
284 props)
285 ;; Emacs 21 compatibility:
286 (apply 'tool-bar-add-item-from-menu
287 command icon (symbol-value fmap)
288 props))))
289 t))
290 (if (symbolp icon-list)
291 (eval icon-list)
292 icon-list))
293 tool-bar-map))
294
d7ba2a01 295(defmacro defun-gmm (name function arg-list &rest body)
18c06a99
RS
296 "Create function NAME.
297If FUNCTION exists, then NAME becomes an alias for FUNCTION.
298Otherwise, create function NAME with ARG-LIST and BODY."
299 (let ((defined-p (fboundp function)))
300 (if defined-p
301 `(defalias ',name ',function)
302 `(defun ,name ,arg-list ,@body))))
303
d7ba2a01 304(defun-gmm gmm-image-search-load-path
18c06a99
RS
305 image-search-load-path (file &optional path)
306 "Emacs 21 and XEmacs don't have `image-search-load-path'.
307This function returns nil on those systems."
308 nil)
309
d7ba2a01 310;; Cf. `mh-image-load-path-for-library' in `mh-compat.el'.
18c06a99 311
d7ba2a01
RS
312(defun-gmm gmm-image-load-path-for-library
313 image-load-path-for-library (library image &optional path no-error)
314 "Return a suitable search path for images used by LIBRARY.
18c06a99 315
d7ba2a01 316It searches for IMAGE in `image-load-path' (excluding
18c06a99
RS
317\"`data-directory'/images\") and `load-path', followed by a path
318suitable for LIBRARY, which includes \"../../etc/images\" and
319\"../etc/images\" relative to the library file itself, and then
320in \"`data-directory'/images\".
321
322Then this function returns a list of directories which contains
323first the directory in which IMAGE was found, followed by the
324value of `load-path'. If PATH is given, it is used instead of
325`load-path'.
326
327If NO-ERROR is non-nil and a suitable path can't be found, don't
328signal an error. Instead, return a list of directories as before,
329except that nil appears in place of the image directory.
330
331Here is an example that uses a common idiom to provide
332compatibility with versions of Emacs that lack the variable
333`image-load-path':
334
335 ;; Shush compiler.
336 (defvar image-load-path)
337
338 (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
339 (image-load-path (cons (car load-path)
340 (when (boundp 'image-load-path)
341 image-load-path))))
342 (mh-tool-bar-folder-buttons-init))"
343 (unless library (error "No library specified"))
344 (unless image (error "No image specified"))
345 (let (image-directory image-directory-load-path)
346 ;; Check for images in image-load-path or load-path.
347 (let ((img image)
348 (dir (or
349 ;; Images in image-load-path.
350 (gmm-image-search-load-path image) ;; "gmm-" prefix!
351 ;; Images in load-path.
352 (locate-library image)))
353 parent)
354 ;; Since the image might be in a nested directory (for
355 ;; example, mail/attach.pbm), adjust `image-directory'
356 ;; accordingly.
357 (when dir
358 (setq dir (file-name-directory dir))
359 (while (setq parent (file-name-directory img))
360 (setq img (directory-file-name parent)
361 dir (expand-file-name "../" dir))))
362 (setq image-directory-load-path dir))
363
364 ;; If `image-directory-load-path' isn't Emacs' image directory,
365 ;; it's probably a user preference, so use it. Then use a
366 ;; relative setting if possible; otherwise, use
367 ;; `image-directory-load-path'.
368 (cond
369 ;; User-modified image-load-path?
370 ((and image-directory-load-path
371 (not (equal image-directory-load-path
372 (file-name-as-directory
373 (expand-file-name "images" data-directory)))))
374 (setq image-directory image-directory-load-path))
375 ;; Try relative setting.
376 ((let (library-name d1ei d2ei)
377 ;; First, find library in the load-path.
378 (setq library-name (locate-library library))
379 (if (not library-name)
380 (error "Cannot find library %s in load-path" library))
381 ;; And then set image-directory relative to that.
382 (setq
383 ;; Go down 2 levels.
384 d2ei (file-name-as-directory
385 (expand-file-name
386 (concat (file-name-directory library-name) "../../etc/images")))
387 ;; Go down 1 level.
388 d1ei (file-name-as-directory
389 (expand-file-name
390 (concat (file-name-directory library-name) "../etc/images"))))
391 (setq image-directory
392 ;; Set it to nil if image is not found.
393 (cond ((file-exists-p (expand-file-name image d2ei)) d2ei)
394 ((file-exists-p (expand-file-name image d1ei)) d1ei)))))
395 ;; Use Emacs' image directory.
396 (image-directory-load-path
397 (setq image-directory image-directory-load-path))
398 (no-error
399 (message "Could not find image %s for library %s" image library))
400 (t
401 (error "Could not find image %s for library %s" image library)))
402
403 ;; Return an augmented `path' or `load-path'.
404 (nconc (list image-directory)
405 (delete image-directory (copy-sequence (or path load-path))))))
406
407(defun gmm-customize-mode (&optional mode)
408 "Customize customization group for MODE.
409If mode is nil, use `major-mode' of the curent buffer."
410 (interactive)
411 (customize-group
412 (or mode
413 (intern (let ((mode (symbol-name major-mode)))
414 (string-match "^\\(.+\\)-mode$" mode)
415 (match-string 1 mode))))))
416
92edaeed
MB
417(defun gmm-write-region (start end filename &optional append visit
418 lockname mustbenew)
419 "Compatibility function for `write-region'.
420
421In XEmacs, the seventh argument of `write-region' specifies the
422coding-system."
423 (if (and mustbenew
424 (or (featurep 'xemacs)
425 (= emacs-major-version 20)))
426 (if (file-exists-p filename)
427 (signal 'file-already-exists
428 (list "File exists" filename))
429 (write-region start end filename append visit lockname))
430 (write-region start end filename append visit lockname mustbenew)))
431
18c06a99
RS
432(provide 'gmm-utils)
433
434;; arch-tag: e0b60920-2ce6-40c1-bfc0-cadbbe26b602
435;;; gmm-utils.el ends here