1 ;;; gmm-utils.el --- Utility functions for Gnus, Message and MML
3 ;; Copyright (C) 2006-2013 Free Software Foundation, Inc.
5 ;; Author: Reiner Steib <reiner.steib@gmx.de>
8 ;; This file is part of GNU Emacs.
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 3 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This library provides self-contained utility functions. The functions are
26 ;; used in Gnus, Message and MML, but within this library there are no
27 ;; dependencies on Gnus, Message, or MML.
32 "Utility functions for Gnus, Message and MML."
34 :version
"22.1" ;; Gnus 5.10.9
37 ;; Helper functions from `gnus-utils.el': gmm-verbose, gmm-message, gmm-error
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 did. 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."
49 (defun gmm-regexp-concat (regexp)
50 "Potentially concat a list of regexps into a single one.
51 The concatenation is done with logical ORs."
57 (mapconcat (lambda (elt) (concat "\\(" elt
"\\)"))
62 (defun gmm-message (level &rest args
)
63 "If LEVEL is lower than `gmm-verbose' print ARGS using `message'.
65 Guideline for numbers:
67 3 - non-serious error messages
68 5 - messages for things that take a long time
69 7 - not very important messages on stuff
70 9 - messages inside loops."
71 (if (<= level gmm-verbose
)
73 ;; We have to do this format thingy here even if the result isn't
74 ;; shown - the return value has to be the same as the return value
76 (apply 'format args
)))
79 (defun gmm-error (level &rest args
)
80 "Beep an error if LEVEL is equal to or less than `gmm-verbose'.
81 ARGS are passed to `message'."
82 (when (<= (floor level
) gmm-verbose
)
86 (when (and (floatp level
)
87 (not (zerop (setq duration
(* 10 (- level
(floor level
)))))))
92 (defun gmm-widget-p (symbol)
93 "Non-nil if SYMBOL is a widget."
94 (get symbol
'widget-type
))
96 (autoload 'widget-create-child-value
"wid-edit")
97 (autoload 'widget-convert
"wid-edit")
98 (autoload 'widget-default-get
"wid-edit")
100 ;; Copy of the `nnmail-lazy' code from `nnmail.el':
101 (define-widget 'gmm-lazy
'default
102 "Base widget for recursive datastructures.
104 This is a copy of the `lazy' widget in Emacs 22.1 provided for compatibility."
106 :convert-widget
'widget-value-convert-widget
107 :value-create
(lambda (widget)
108 (let ((value (widget-get widget
:value
))
109 (type (widget-get widget
:type
)))
110 (widget-put widget
:children
111 (list (widget-create-child-value
112 widget
(widget-convert type
) value
)))))
113 :value-delete
'widget-children-value-delete
114 :value-get
(lambda (widget)
115 (widget-value (car (widget-get widget
:children
))))
116 :value-inline
(lambda (widget)
117 (widget-apply (car (widget-get widget
:children
))
119 :default-get
(lambda (widget)
121 (widget-convert (widget-get widget
:type
))))
122 :match
(lambda (widget value
)
123 (widget-apply (widget-convert (widget-get widget
:type
))
125 :validate
(lambda (widget)
126 (widget-apply (car (widget-get widget
:children
)) :validate
)))
128 ;; Note: The format of `gmm-tool-bar-item' may change if some future Emacs
129 ;; version will provide customizable tool bar buttons using a different
132 ;; TODO: Extend API so that the "Command" entry can be a function or a plist.
133 ;; In case of a list it should have the format...
135 ;; (:none command-without-modifier
136 ;; :shift command-with-shift-pressed
137 ;; :control command-with-ctrl-pressed
138 ;; :control-shift command-with-control-and-shift-pressed
139 ;; ;; mouse-2 and mouse-3 can't be used in Emacs yet.
140 ;; :mouse-2 command-on-mouse-2-press
141 ;; :mouse-3 command-on-mouse-3-press) ;; typically a menu of related commands
143 ;; Combinations of mouse-[23] plus shift and/or control might be overkill.
145 ;; Then use (plist-get rs-command :none), (plist-get rs-command :shift)
147 (define-widget 'gmm-tool-bar-item
(if (gmm-widget-p 'lazy
) 'lazy
'gmm-lazy
)
148 "Tool bar list item."
151 (list :tag
"Command and Icon"
152 (function :tag
"Command")
153 (string :tag
"Icon file")
155 (const :tag
"Default map" nil
)
156 ;; Note: Usually we need non-nil attributes if map is t.
157 (const :tag
"No menu" t
)
158 (sexp :tag
"Other map"))
159 (plist :inline t
:tag
"Properties"))
160 (list :tag
"Separator"
161 (const :tag
"No command" gmm-ignore
)
162 (string :tag
"Icon file")
163 (const :tag
"No map")
164 (plist :inline t
:tag
"Properties"))))
166 (define-widget 'gmm-tool-bar-zap-list
(if (gmm-widget-p 'lazy
) 'lazy
'gmm-lazy
)
168 :tag
"Tool bar zap list"
169 :type
'(choice (const :tag
"Zap all" t
)
170 (const :tag
"Keep all" nil
)
173 ;; Work around (bug in customize?), see
174 ;; <news:v9is48jrj1.fsf@marauder.physik.uni-ulm.de>
175 ;; (new-file open-file dired kill-buffer write-file
176 ;; print-buffer customize help)
188 (const search-forward
)
194 (symbol :tag
"Icon item")))))
196 ;; (defun gmm-color-cells (&optional display)
197 ;; "Return the number of color cells supported by DISPLAY.
198 ;; Compatibility function."
199 ;; ;; `display-color-cells' doesn't return more than 256 even if color depth is
200 ;; ;; > 8 in Emacs 21.
202 ;; ;; Feel free to add proper XEmacs support.
203 ;; (let* ((cells (and (fboundp 'display-color-cells)
204 ;; (display-color-cells display)))
205 ;; (plane (and (fboundp 'x-display-planes)
206 ;; (ash 1 (x-display-planes))))
208 ;; (max (if (integerp cells) cells none)
209 ;; (if (integerp plane) plane none))))
211 (defcustom gmm-tool-bar-style
212 (if (and (boundp 'tool-bar-mode
)
214 (and (fboundp 'display-visual-class
)
215 (not (memq (display-visual-class)
216 (list 'static-gray
'gray-scale
217 'static-color
'pseudo-color
)))))
220 "Preferred tool bar style."
221 :type
'(choice (const :tag
"GNOME style" gnome
)
222 (const :tag
"Retro look" retro
))
225 (defvar tool-bar-map
)
228 (defun gmm-tool-bar-from-list (icon-list zap-list default-map
)
229 "Make a tool bar from ICON-LIST.
231 Within each entry of ICON-LIST, the first element is a menu
232 command, the second element is an icon file name and the third
233 element is a test function. You can use \\[describe-key]
234 <menu-entry> to find out the name of a menu command. The fourth
235 and all following elements are passed as the PROPS argument to the
236 function `tool-bar-local-item'.
238 If ZAP-LIST is a list, remove those item from the default
239 `tool-bar-map'. If it is t, start with a new sparse map. You
240 can use \\[describe-key] <icon> to find out the name of an icon
241 item. When \\[describe-key] <icon> shows \"<tool-bar> <new-file>
242 runs the command find-file\", then use `new-file' in ZAP-LIST.
244 DEFAULT-MAP specifies the default key map for ICON-LIST."
245 (let (;; For Emacs 21, we must let-bind `tool-bar-map'. In Emacs 22, we
246 ;; could use some other local variable.
247 (tool-bar-map (if (eq zap-list t
)
249 (copy-keymap tool-bar-map
))))
250 (when (listp zap-list
)
251 ;; Zap some items which aren't relevant for this mode and take up space.
252 (dolist (key zap-list
)
253 (define-key tool-bar-map
(vector key
) nil
)))
255 (let ((command (car el
))
257 (fmap (or (nth 2 el
) default-map
))
258 (props (cdr (cdr (cdr el
)))) )
259 ;; command may stem from different from-maps:
260 (cond ((eq command
'gmm-ignore
)
261 ;; The dummy `gmm-ignore', see `gmm-tool-bar-item'
262 ;; widget. Suppress tooltip by adding `:enable nil'.
263 (if (fboundp 'tool-bar-local-item
)
264 (apply 'tool-bar-local-item icon nil nil
265 tool-bar-map
:enable nil props
)
266 ;; (tool-bar-local-item ICON DEF KEY MAP &rest PROPS)
267 ;; (tool-bar-add-item ICON DEF KEY &rest PROPS)
268 (apply 'tool-bar-add-item icon nil nil
:enable nil props
)))
269 ((equal fmap t
) ;; Not a menu command
270 (apply 'tool-bar-local-item
272 (intern icon
) ;; reuse icon or fmap here?
275 (apply 'tool-bar-local-item-from-menu
276 ;; (apply 'tool-bar-local-item icon def key
277 ;; tool-bar-map props)
278 command icon tool-bar-map
(symbol-value fmap
)
281 (if (symbolp icon-list
)
286 (defmacro defun-gmm
(name function arg-list
&rest body
)
287 "Create function NAME.
288 If FUNCTION exists, then NAME becomes an alias for FUNCTION.
289 Otherwise, create function NAME with ARG-LIST and BODY."
290 (let ((defined-p (fboundp function
)))
292 `(defalias ',name
',function
)
293 `(defun ,name
,arg-list
,@body
))))
295 (defun-gmm gmm-image-search-load-path
296 image-search-load-path
(file &optional path
)
297 "Emacs 21 and XEmacs don't have `image-search-load-path'.
298 This function returns nil on those systems."
301 ;; Cf. `mh-image-load-path-for-library' in `mh-compat.el'.
303 (defun-gmm gmm-image-load-path-for-library
304 image-load-path-for-library
(library image
&optional path no-error
)
305 "Return a suitable search path for images used by LIBRARY.
307 It searches for IMAGE in `image-load-path' (excluding
308 \"`data-directory'/images\") and `load-path', followed by a path
309 suitable for LIBRARY, which includes \"../../etc/images\" and
310 \"../etc/images\" relative to the library file itself, and then
311 in \"`data-directory'/images\".
313 Then this function returns a list of directories which contains
314 first the directory in which IMAGE was found, followed by the
315 value of `load-path'. If PATH is given, it is used instead of
318 If NO-ERROR is non-nil and a suitable path can't be found, don't
319 signal an error. Instead, return a list of directories as before,
320 except that nil appears in place of the image directory.
322 Here is an example that uses a common idiom to provide
323 compatibility with versions of Emacs that lack the variable
327 (defvar image-load-path)
329 (let* ((load-path (image-load-path-for-library \"mh-e\" \"mh-logo.xpm\"))
330 (image-load-path (cons (car load-path)
331 (when (boundp 'image-load-path)
333 (mh-tool-bar-folder-buttons-init))"
334 (unless library
(error "No library specified"))
335 (unless image
(error "No image specified"))
336 (let (image-directory image-directory-load-path
)
337 ;; Check for images in image-load-path or load-path.
340 ;; Images in image-load-path.
341 (gmm-image-search-load-path image
) ;; "gmm-" prefix!
342 ;; Images in load-path.
343 (locate-library image
)))
345 ;; Since the image might be in a nested directory (for
346 ;; example, mail/attach.pbm), adjust `image-directory'
349 (setq dir
(file-name-directory dir
))
350 (while (setq parent
(file-name-directory img
))
351 (setq img
(directory-file-name parent
)
352 dir
(expand-file-name "../" dir
))))
353 (setq image-directory-load-path dir
))
355 ;; If `image-directory-load-path' isn't Emacs's image directory,
356 ;; it's probably a user preference, so use it. Then use a
357 ;; relative setting if possible; otherwise, use
358 ;; `image-directory-load-path'.
360 ;; User-modified image-load-path?
361 ((and image-directory-load-path
362 (not (equal image-directory-load-path
363 (file-name-as-directory
364 (expand-file-name "images" data-directory
)))))
365 (setq image-directory image-directory-load-path
))
366 ;; Try relative setting.
367 ((let (library-name d1ei d2ei
)
368 ;; First, find library in the load-path.
369 (setq library-name
(locate-library library
))
370 (if (not library-name
)
371 (error "Cannot find library %s in load-path" library
))
372 ;; And then set image-directory relative to that.
375 d2ei
(file-name-as-directory
377 (concat (file-name-directory library-name
) "../../etc/images")))
379 d1ei
(file-name-as-directory
381 (concat (file-name-directory library-name
) "../etc/images"))))
382 (setq image-directory
383 ;; Set it to nil if image is not found.
384 (cond ((file-exists-p (expand-file-name image d2ei
)) d2ei
)
385 ((file-exists-p (expand-file-name image d1ei
)) d1ei
)))))
386 ;; Use Emacs's image directory.
387 (image-directory-load-path
388 (setq image-directory image-directory-load-path
))
390 (message "Could not find image %s for library %s" image library
))
392 (error "Could not find image %s for library %s" image library
)))
394 ;; Return an augmented `path' or `load-path'.
395 (nconc (list image-directory
)
396 (delete image-directory
(copy-sequence (or path load-path
))))))
398 (defun gmm-customize-mode (&optional mode
)
399 "Customize customization group for MODE.
400 If mode is nil, use `major-mode' of the current buffer."
404 (intern (let ((mode (symbol-name major-mode
)))
405 (string-match "^\\(.+\\)-mode$" mode
)
406 (match-string 1 mode
))))))
408 (defun gmm-write-region (start end filename
&optional append visit
410 "Compatibility function for `write-region'.
412 In XEmacs, the seventh argument of `write-region' specifies the
414 (if (and mustbenew
(featurep 'xemacs
))
415 (if (file-exists-p filename
)
416 (signal 'file-already-exists
(list "File exists" filename
))
417 (write-region start end filename append visit lockname
))
418 (write-region start end filename append visit lockname mustbenew
)))
420 ;; `interactive-p' is obsolete since Emacs 23.2.
421 (defmacro gmm-called-interactively-p
(kind)
424 (eval '(called-interactively-p 'any
))
426 `(called-interactively-p ,kind
))
428 (wrong-number-of-arguments '(called-interactively-p))
430 (void-function '(interactive-p))))
432 ;; `labels' is obsolete since Emacs 24.3.
433 (defmacro gmm-labels
(bindings &rest body
)
434 "Make temporary function bindings.
435 The bindings can be recursive and the scoping is lexical, but capturing
436 them in closures will only work if `lexical-binding' is in use. But in
437 Emacs 24.2 and older, the lexical scoping is handled via `lexical-let'
438 rather than relying on `lexical-binding'.
440 \(fn ((FUNC ARGLIST BODY...) ...) FORM...)"
441 `(,(progn (require 'cl
) (if (fboundp 'cl-labels
) 'cl-labels
'labels
))
443 (put 'gmm-labels
'lisp-indent-function
1)
447 ;;; gmm-utils.el ends here