Flymake fix to handle more C++ filenames (Bug#5488).
[emacs.git] / lisp / gnus / gmm-utils.el
blobb44d3c08c4aa55fb0dc7b1a2b8960ff9788eef1a
1 ;;; gmm-utils.el --- Utility functions for Gnus, Message and MML
3 ;; Copyright (C) 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Reiner Steib <reiner.steib@gmx.de>
6 ;; Keywords: news
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/>.
23 ;;; Commentary:
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.
29 ;;; Code:
31 (require 'wid-edit)
33 (defgroup gmm nil
34 "Utility functions for Gnus, Message and MML."
35 :prefix "gmm-"
36 :version "22.1" ;; Gnus 5.10.9
37 :group 'lisp)
39 ;; Helper functions from `gnus-utils.el': gmm-verbose, gmm-message, gmm-error
41 (defcustom gmm-verbose 7
42 "Integer that says how verbose gmm should be.
43 The higher the number, the more messages will flash to say what
44 it did. At zero, it will be totally mute; at five, it will
45 display most important messages; and at ten, it will keep on
46 jabbering all the time."
47 :type 'integer
48 :group 'gmm)
50 ;;;###autoload
51 (defun gmm-regexp-concat (regexp)
52 "Potentially concat a list of regexps into a single one.
53 The concatenation is done with logical ORs."
54 (cond ((null regexp)
55 nil)
56 ((stringp regexp)
57 regexp)
58 ((listp regexp)
59 (mapconcat (lambda (elt) (concat "\\(" elt "\\)"))
60 regexp
61 "\\|"))))
63 ;;;###autoload
64 (defun gmm-message (level &rest args)
65 "If LEVEL is lower than `gmm-verbose' print ARGS using `message'.
67 Guideline for numbers:
68 1 - error messages
69 3 - non-serious error messages
70 5 - messages for things that take a long time
71 7 - not very important messages on stuff
72 9 - messages inside 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)))
80 ;;;###autoload
81 (defun gmm-error (level &rest args)
82 "Beep an error if LEVEL is equal to or less than `gmm-verbose'.
83 ARGS 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)
93 ;;;###autoload
94 (defun gmm-widget-p (symbol)
95 "Non-nil if SYMBOL is a widget."
96 (get symbol 'widget-type))
98 ;; Copy of the `nnmail-lazy' code from `nnmail.el':
99 (define-widget 'gmm-lazy 'default
100 "Base widget for recursive datastructures.
102 This is a 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)))
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.
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...
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
141 ;; Combinations of mouse-[23] plus shift and/or controll might be overkill.
143 ;; Then use (plist-get rs-command :none), (plist-get rs-command :shift)
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"))))
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")))))
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))))
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."
219 :type '(choice (const :tag "GNOME style" gnome)
220 (const :tag "Retro look" retro))
221 :group 'gmm)
223 (defvar tool-bar-map)
225 ;;;###autoload
226 (defun gmm-tool-bar-from-list (icon-list zap-list default-map)
227 "Make a tool bar from ICON-LIST.
229 Within each entry of ICON-LIST, the first element is a menu
230 command, the second element is an icon file name and the third
231 element is a test function. You can use \\[describe-key]
232 <menu-entry> to find out the name of a menu command. The fourth
233 and all following elements are passed as the PROPS argument to the
234 function `tool-bar-local-item'.
236 If 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
238 can use \\[describe-key] <icon> to find out the name of an icon
239 item. When \\[describe-key] <icon> shows \"<tool-bar> <new-file>
240 runs the command find-file\", then use `new-file' in ZAP-LIST.
242 DEFAULT-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))))
290 (if (symbolp icon-list)
291 (eval icon-list)
292 icon-list))
293 tool-bar-map))
295 (defmacro defun-gmm (name function arg-list &rest body)
296 "Create function NAME.
297 If FUNCTION exists, then NAME becomes an alias for FUNCTION.
298 Otherwise, 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))))
304 (defun-gmm gmm-image-search-load-path
305 image-search-load-path (file &optional path)
306 "Emacs 21 and XEmacs don't have `image-search-load-path'.
307 This function returns nil on those systems."
308 nil)
310 ;; Cf. `mh-image-load-path-for-library' in `mh-compat.el'.
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.
316 It searches for IMAGE in `image-load-path' (excluding
317 \"`data-directory'/images\") and `load-path', followed by a path
318 suitable for LIBRARY, which includes \"../../etc/images\" and
319 \"../etc/images\" relative to the library file itself, and then
320 in \"`data-directory'/images\".
322 Then this function returns a list of directories which contains
323 first the directory in which IMAGE was found, followed by the
324 value of `load-path'. If PATH is given, it is used instead of
325 `load-path'.
327 If NO-ERROR is non-nil and a suitable path can't be found, don't
328 signal an error. Instead, return a list of directories as before,
329 except that nil appears in place of the image directory.
331 Here is an example that uses a common idiom to provide
332 compatibility with versions of Emacs that lack the variable
333 `image-load-path':
335 ;; Shush compiler.
336 (defvar image-load-path)
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))
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))
401 (error "Could not find image %s for library %s" image library)))
403 ;; Return an augmented `path' or `load-path'.
404 (nconc (list image-directory)
405 (delete image-directory (copy-sequence (or path load-path))))))
407 (defun gmm-customize-mode (&optional mode)
408 "Customize customization group for MODE.
409 If mode is nil, use `major-mode' of the current 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))))))
417 (defun gmm-write-region (start end filename &optional append visit
418 lockname mustbenew)
419 "Compatibility function for `write-region'.
421 In XEmacs, the seventh argument of `write-region' specifies the
422 coding-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)))
432 (provide 'gmm-utils)
434 ;; arch-tag: e0b60920-2ce6-40c1-bfc0-cadbbe26b602
435 ;;; gmm-utils.el ends here