typo.
[emacs.git] / lisp / gnus / gnus-util.el
blob22db7ecd6d1e349d2b5b1ecd77e1bd172d3fd157
1 ;;; gnus-util.el --- utility functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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 2, or (at your option)
13 ;; 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; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; Nothing in this file depends on any other parts of Gnus -- all
28 ;; functions and macros in this file are utility functions that are
29 ;; used by Gnus and may be used by any other package without loading
30 ;; Gnus first.
32 ;; [Unfortunately, it does depend on other parts of Gnus, e.g. the
33 ;; autoloads below...]
35 ;;; Code:
37 (require 'custom)
38 (eval-when-compile
39 (require 'cl)
40 ;; Fixme: this should be a gnus variable, not nnmail-.
41 (defvar nnmail-pathname-coding-system))
42 (require 'time-date)
43 (require 'netrc)
45 (eval-and-compile
46 (autoload 'message-fetch-field "message")
47 (autoload 'gnus-get-buffer-window "gnus-win")
48 (autoload 'rmail-insert-rmail-file-header "rmail")
49 (autoload 'rmail-count-new-messages "rmail")
50 (autoload 'rmail-show-message "rmail")
51 (autoload 'nnheader-narrow-to-headers "nnheader")
52 (autoload 'nnheader-replace-chars-in-string "nnheader"))
54 (eval-and-compile
55 (cond
56 ((fboundp 'replace-in-string)
57 (defalias 'gnus-replace-in-string 'replace-in-string))
58 ((fboundp 'replace-regexp-in-string)
59 (defun gnus-replace-in-string (string regexp newtext &optional literal)
60 (replace-regexp-in-string regexp newtext string nil literal)))
62 (defun gnus-replace-in-string (string regexp newtext &optional literal)
63 (let ((start 0) tail)
64 (while (string-match regexp string start)
65 (setq tail (- (length string) (match-end 0)))
66 (setq string (replace-match newtext nil literal string))
67 (setq start (- (length string) tail))))
68 string))))
70 ;;; bring in the netrc functions as aliases
71 (defalias 'gnus-netrc-get 'netrc-get)
72 (defalias 'gnus-netrc-machine 'netrc-machine)
73 (defalias 'gnus-parse-netrc 'netrc-parse)
75 (defun gnus-boundp (variable)
76 "Return non-nil if VARIABLE is bound and non-nil."
77 (and (boundp variable)
78 (symbol-value variable)))
80 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
81 "Pop to BUFFER, evaluate FORMS, and then return to the original window."
82 (let ((tempvar (make-symbol "GnusStartBufferWindow"))
83 (w (make-symbol "w"))
84 (buf (make-symbol "buf")))
85 `(let* ((,tempvar (selected-window))
86 (,buf ,buffer)
87 (,w (gnus-get-buffer-window ,buf 'visible)))
88 (unwind-protect
89 (progn
90 (if ,w
91 (progn
92 (select-window ,w)
93 (set-buffer (window-buffer ,w)))
94 (pop-to-buffer ,buf))
95 ,@forms)
96 (select-window ,tempvar)))))
98 (put 'gnus-eval-in-buffer-window 'lisp-indent-function 1)
99 (put 'gnus-eval-in-buffer-window 'edebug-form-spec '(form body))
101 (defmacro gnus-intern-safe (string hashtable)
102 "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
103 `(let ((symbol (intern ,string ,hashtable)))
104 (or (boundp symbol)
105 (set symbol nil))
106 symbol))
108 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
109 ;; to limit the length of a string. This function is necessary since
110 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
111 ;; Fixme: Why not `truncate-string-to-width'?
112 (defsubst gnus-limit-string (str width)
113 (if (> (length str) width)
114 (substring str 0 width)
115 str))
117 (defsubst gnus-goto-char (point)
118 (and point (goto-char point)))
120 (defmacro gnus-buffer-exists-p (buffer)
121 `(let ((buffer ,buffer))
122 (when buffer
123 (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
124 buffer))))
126 (defalias 'gnus-point-at-bol
127 (if (fboundp 'point-at-bol)
128 'point-at-bol
129 'line-beginning-position))
131 (defalias 'gnus-point-at-eol
132 (if (fboundp 'point-at-eol)
133 'point-at-eol
134 'line-end-position))
136 ;; The LOCAL arg to `add-hook' is interpreted differently in Emacs and
137 ;; XEmacs. In Emacs we don't need to call `make-local-hook' first.
138 ;; It's harmless, though, so the main purpose of this alias is to shut
139 ;; up the byte compiler.
140 (defalias 'gnus-make-local-hook
141 (if (eq (get 'make-local-hook 'byte-compile)
142 'byte-compile-obsolete)
143 'ignore ; Emacs
144 'make-local-hook)) ; XEmacs
146 (defun gnus-delete-first (elt list)
147 "Delete by side effect the first occurrence of ELT as a member of LIST."
148 (if (equal (car list) elt)
149 (cdr list)
150 (let ((total list))
151 (while (and (cdr list)
152 (not (equal (cadr list) elt)))
153 (setq list (cdr list)))
154 (when (cdr list)
155 (setcdr list (cddr list)))
156 total)))
158 ;; Delete the current line (and the next N lines).
159 (defmacro gnus-delete-line (&optional n)
160 `(delete-region (gnus-point-at-bol)
161 (progn (forward-line ,(or n 1)) (point))))
163 (defun gnus-byte-code (func)
164 "Return a form that can be `eval'ed based on FUNC."
165 (let ((fval (indirect-function func)))
166 (if (byte-code-function-p fval)
167 (let ((flist (append fval nil)))
168 (setcar flist 'byte-code)
169 flist)
170 (cons 'progn (cddr fval)))))
172 (defun gnus-extract-address-components (from)
173 "Extract address components from a From header.
174 Given an RFC-822 address FROM, extract full name and canonical address.
175 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS). Much more simple
176 solution than `mail-extract-address-components', which works much better, but
177 is slower."
178 (let (name address)
179 ;; First find the address - the thing with the @ in it. This may
180 ;; not be accurate in mail addresses, but does the trick most of
181 ;; the time in news messages.
182 (when (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
183 (setq address (substring from (match-beginning 0) (match-end 0))))
184 ;; Then we check whether the "name <address>" format is used.
185 (and address
186 ;; Linear white space is not required.
187 (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
188 (and (setq name (substring from 0 (match-beginning 0)))
189 ;; Strip any quotes from the name.
190 (string-match "^\".*\"$" name)
191 (setq name (substring name 1 (1- (match-end 0))))))
192 ;; If not, then "address (name)" is used.
193 (or name
194 (and (string-match "(.+)" from)
195 (setq name (substring from (1+ (match-beginning 0))
196 (1- (match-end 0)))))
197 (and (string-match "()" from)
198 (setq name address))
199 ;; XOVER might not support folded From headers.
200 (and (string-match "(.*" from)
201 (setq name (substring from (1+ (match-beginning 0))
202 (match-end 0)))))
203 (list (if (string= name "") nil name) (or address from))))
206 (defun gnus-fetch-field (field)
207 "Return the value of the header FIELD of current article."
208 (save-excursion
209 (save-restriction
210 (let ((case-fold-search t)
211 (inhibit-point-motion-hooks t))
212 (nnheader-narrow-to-headers)
213 (message-fetch-field field)))))
215 (defun gnus-fetch-original-field (field)
216 "Fetch FIELD from the original version of the current article."
217 (with-current-buffer gnus-original-article-buffer
218 (gnus-fetch-field field)))
221 (defun gnus-goto-colon ()
222 (beginning-of-line)
223 (let ((eol (gnus-point-at-eol)))
224 (goto-char (or (text-property-any (point) eol 'gnus-position t)
225 (search-forward ":" eol t)
226 (point)))))
228 (defun gnus-decode-newsgroups (newsgroups group &optional method)
229 (let ((method (or method (gnus-find-method-for-group group))))
230 (mapconcat (lambda (group)
231 (gnus-group-name-decode group (gnus-group-name-charset
232 method group)))
233 (message-tokenize-header newsgroups)
234 ",")))
236 (defun gnus-remove-text-with-property (prop)
237 "Delete all text in the current buffer with text property PROP."
238 (save-excursion
239 (goto-char (point-min))
240 (while (not (eobp))
241 (while (get-text-property (point) prop)
242 (delete-char 1))
243 (goto-char (next-single-property-change (point) prop nil (point-max))))))
245 (defun gnus-newsgroup-directory-form (newsgroup)
246 "Make hierarchical directory name from NEWSGROUP name."
247 (let* ((newsgroup (gnus-newsgroup-savable-name newsgroup))
248 (idx (string-match ":" newsgroup)))
249 (concat
250 (if idx (substring newsgroup 0 idx))
251 (if idx "/")
252 (nnheader-replace-chars-in-string
253 (if idx (substring newsgroup (1+ idx)) newsgroup)
254 ?. ?/))))
256 (defun gnus-newsgroup-savable-name (group)
257 ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
258 ;; with dots.
259 (nnheader-replace-chars-in-string group ?/ ?.))
261 (defun gnus-string> (s1 s2)
262 (not (or (string< s1 s2)
263 (string= s1 s2))))
265 ;;; Time functions.
267 (defun gnus-file-newer-than (file date)
268 (let ((fdate (nth 5 (file-attributes file))))
269 (or (> (car fdate) (car date))
270 (and (= (car fdate) (car date))
271 (> (nth 1 fdate) (nth 1 date))))))
273 ;;; Keymap macros.
275 (defmacro gnus-local-set-keys (&rest plist)
276 "Set the keys in PLIST in the current keymap."
277 `(gnus-define-keys-1 (current-local-map) ',plist))
279 (defmacro gnus-define-keys (keymap &rest plist)
280 "Define all keys in PLIST in KEYMAP."
281 `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
283 (defmacro gnus-define-keys-safe (keymap &rest plist)
284 "Define all keys in PLIST in KEYMAP without overwriting previous definitions."
285 `(gnus-define-keys-1 (quote ,keymap) (quote ,plist) t))
287 (put 'gnus-define-keys 'lisp-indent-function 1)
288 (put 'gnus-define-keys-safe 'lisp-indent-function 1)
289 (put 'gnus-local-set-keys 'lisp-indent-function 1)
291 (defmacro gnus-define-keymap (keymap &rest plist)
292 "Define all keys in PLIST in KEYMAP."
293 `(gnus-define-keys-1 ,keymap (quote ,plist)))
295 (put 'gnus-define-keymap 'lisp-indent-function 1)
297 (defun gnus-define-keys-1 (keymap plist &optional safe)
298 (when (null keymap)
299 (error "Can't set keys in a null keymap"))
300 (cond ((symbolp keymap)
301 (setq keymap (symbol-value keymap)))
302 ((keymapp keymap))
303 ((listp keymap)
304 (set (car keymap) nil)
305 (define-prefix-command (car keymap))
306 (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
307 (setq keymap (symbol-value (car keymap)))))
308 (let (key)
309 (while plist
310 (when (symbolp (setq key (pop plist)))
311 (setq key (symbol-value key)))
312 (if (or (not safe)
313 (eq (lookup-key keymap key) 'undefined))
314 (define-key keymap key (pop plist))
315 (pop plist)))))
317 (defun gnus-completing-read-with-default (default prompt &rest args)
318 ;; Like `completing-read', except that DEFAULT is the default argument.
319 (let* ((prompt (if default
320 (concat prompt " (default " default ") ")
321 (concat prompt " ")))
322 (answer (apply 'completing-read prompt args)))
323 (if (or (null answer) (zerop (length answer)))
324 default
325 answer)))
327 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
328 ;; the echo area.
329 (defun gnus-y-or-n-p (prompt)
330 (prog1
331 (y-or-n-p prompt)
332 (message "")))
334 (defun gnus-yes-or-no-p (prompt)
335 (prog1
336 (yes-or-no-p prompt)
337 (message "")))
339 ;; By Frank Schmitt <ich@Frank-Schmitt.net>. Allows to have
340 ;; age-depending date representations. (e.g. just the time if it's
341 ;; from today, the day of the week if it's within the last 7 days and
342 ;; the full date if it's older)
344 (defun gnus-seconds-today ()
345 "Return the number of seconds passed today."
346 (let ((now (decode-time (current-time))))
347 (+ (car now) (* (car (cdr now)) 60) (* (car (nthcdr 2 now)) 3600))))
349 (defun gnus-seconds-month ()
350 "Return the number of seconds passed this month."
351 (let ((now (decode-time (current-time))))
352 (+ (car now) (* (car (cdr now)) 60) (* (car (nthcdr 2 now)) 3600)
353 (* (- (car (nthcdr 3 now)) 1) 3600 24))))
355 (defun gnus-seconds-year ()
356 "Return the number of seconds passed this year."
357 (let ((now (decode-time (current-time)))
358 (days (format-time-string "%j" (current-time))))
359 (+ (car now) (* (car (cdr now)) 60) (* (car (nthcdr 2 now)) 3600)
360 (* (- (string-to-number days) 1) 3600 24))))
362 (defvar gnus-user-date-format-alist
363 '(((gnus-seconds-today) . "%k:%M")
364 (604800 . "%a %k:%M") ;;that's one week
365 ((gnus-seconds-month) . "%a %d")
366 ((gnus-seconds-year) . "%b %d")
367 (t . "%b %d '%y")) ;;this one is used when no
368 ;;other does match
369 "Specifies date format depending on age of article.
370 This is an alist of items (AGE . FORMAT). AGE can be a number (of
371 seconds) or a Lisp expression evaluating to a number. When the age of
372 the article is less than this number, then use `format-time-string'
373 with the corresponding FORMAT for displaying the date of the article.
374 If AGE is not a number or a Lisp expression evaluating to a
375 non-number, then the corresponding FORMAT is used as a default value.
377 Note that the list is processed from the beginning, so it should be
378 sorted by ascending AGE. Also note that items following the first
379 non-number AGE will be ignored.
381 You can use the functions `gnus-seconds-today', `gnus-seconds-month'
382 and `gnus-seconds-year' in the AGE spec. They return the number of
383 seconds passed since the start of today, of this month, of this year,
384 respectively.")
386 (defun gnus-user-date (messy-date)
387 "Format the messy-date according to gnus-user-date-format-alist.
388 Returns \" ? \" if there's bad input or if an other error occurs.
389 Input should look like this: \"Sun, 14 Oct 2001 13:34:39 +0200\"."
390 (condition-case ()
391 (let* ((messy-date (time-to-seconds (safe-date-to-time messy-date)))
392 (now (time-to-seconds (current-time)))
393 ;;If we don't find something suitable we'll use this one
394 (my-format "%b %d '%y"))
395 (let* ((difference (- now messy-date))
396 (templist gnus-user-date-format-alist)
397 (top (eval (caar templist))))
398 (while (if (numberp top) (< top difference) (not top))
399 (progn
400 (setq templist (cdr templist))
401 (setq top (eval (caar templist)))))
402 (if (stringp (cdr (car templist)))
403 (setq my-format (cdr (car templist)))))
404 (format-time-string (eval my-format) (seconds-to-time messy-date)))
405 (error " ? ")))
407 (defun gnus-dd-mmm (messy-date)
408 "Return a string like DD-MMM from a big messy string."
409 (condition-case ()
410 (format-time-string "%d-%b" (safe-date-to-time messy-date))
411 (error " - ")))
413 (defmacro gnus-date-get-time (date)
414 "Convert DATE string to Emacs time.
415 Cache the result as a text property stored in DATE."
416 ;; Either return the cached value...
417 `(let ((d ,date))
418 (if (equal "" d)
419 '(0 0)
420 (or (get-text-property 0 'gnus-time d)
421 ;; or compute the value...
422 (let ((time (safe-date-to-time d)))
423 ;; and store it back in the string.
424 (put-text-property 0 1 'gnus-time time d)
425 time)))))
427 (defsubst gnus-time-iso8601 (time)
428 "Return a string of TIME in YYYYMMDDTHHMMSS format."
429 (format-time-string "%Y%m%dT%H%M%S" time))
431 (defun gnus-date-iso8601 (date)
432 "Convert the DATE to YYYYMMDDTHHMMSS."
433 (condition-case ()
434 (gnus-time-iso8601 (gnus-date-get-time date))
435 (error "")))
437 (defun gnus-mode-string-quote (string)
438 "Quote all \"%\"'s in STRING."
439 (gnus-replace-in-string string "%" "%%"))
441 ;; Make a hash table (default and minimum size is 256).
442 ;; Optional argument HASHSIZE specifies the table size.
443 (defun gnus-make-hashtable (&optional hashsize)
444 (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 256) 256) 0))
446 ;; Make a number that is suitable for hashing; bigger than MIN and
447 ;; equal to some 2^x. Many machines (such as sparcs) do not have a
448 ;; hardware modulo operation, so they implement it in software. On
449 ;; many sparcs over 50% of the time to intern is spent in the modulo.
450 ;; Yes, it's slower than actually computing the hash from the string!
451 ;; So we use powers of 2 so people can optimize the modulo to a mask.
452 (defun gnus-create-hash-size (min)
453 (let ((i 1))
454 (while (< i min)
455 (setq i (* 2 i)))
458 (defcustom gnus-verbose 7
459 "*Integer that says how verbose Gnus should be.
460 The higher the number, the more messages Gnus will flash to say what
461 it's doing. At zero, Gnus will be totally mute; at five, Gnus will
462 display most important messages; and at ten, Gnus will keep on
463 jabbering all the time."
464 :group 'gnus-start
465 :type 'integer)
467 (defun gnus-message (level &rest args)
468 "If LEVEL is lower than `gnus-verbose' print ARGS using `message'.
470 Guideline for numbers:
471 1 - error messages, 3 - non-serious error messages, 5 - messages for things
472 that take a long time, 7 - not very important messages on stuff, 9 - messages
473 inside loops."
474 (if (<= level gnus-verbose)
475 (apply 'message args)
476 ;; We have to do this format thingy here even if the result isn't
477 ;; shown - the return value has to be the same as the return value
478 ;; from `message'.
479 (apply 'format args)))
481 (defun gnus-error (level &rest args)
482 "Beep an error if LEVEL is equal to or less than `gnus-verbose'."
483 (when (<= (floor level) gnus-verbose)
484 (apply 'message args)
485 (ding)
486 (let (duration)
487 (when (and (floatp level)
488 (not (zerop (setq duration (* 10 (- level (floor level)))))))
489 (sit-for duration))))
490 nil)
492 (defun gnus-split-references (references)
493 "Return a list of Message-IDs in REFERENCES."
494 (let ((beg 0)
495 ids)
496 (while (string-match "<[^<]+[^< \t]" references beg)
497 (push (substring references (match-beginning 0) (setq beg (match-end 0)))
498 ids))
499 (nreverse ids)))
501 (defsubst gnus-parent-id (references &optional n)
502 "Return the last Message-ID in REFERENCES.
503 If N, return the Nth ancestor instead."
504 (when (and references
505 (not (zerop (length references))))
506 (if n
507 (let ((ids (inline (gnus-split-references references))))
508 (while (nthcdr n ids)
509 (setq ids (cdr ids)))
510 (car ids))
511 (when (string-match "\\(<[^<]+>\\)[ \t]*\\'" references)
512 (match-string 1 references)))))
514 (defun gnus-buffer-live-p (buffer)
515 "Say whether BUFFER is alive or not."
516 (and buffer
517 (get-buffer buffer)
518 (buffer-name (get-buffer buffer))))
520 (defun gnus-horizontal-recenter ()
521 "Recenter the current buffer horizontally."
522 (if (< (current-column) (/ (window-width) 2))
523 (set-window-hscroll (gnus-get-buffer-window (current-buffer) t) 0)
524 (let* ((orig (point))
525 (end (window-end (gnus-get-buffer-window (current-buffer) t)))
526 (max 0))
527 (when end
528 ;; Find the longest line currently displayed in the window.
529 (goto-char (window-start))
530 (while (and (not (eobp))
531 (< (point) end))
532 (end-of-line)
533 (setq max (max max (current-column)))
534 (forward-line 1))
535 (goto-char orig)
536 ;; Scroll horizontally to center (sort of) the point.
537 (if (> max (window-width))
538 (set-window-hscroll
539 (gnus-get-buffer-window (current-buffer) t)
540 (min (- (current-column) (/ (window-width) 3))
541 (+ 2 (- max (window-width)))))
542 (set-window-hscroll (gnus-get-buffer-window (current-buffer) t) 0))
543 max))))
545 (defun gnus-read-event-char (&optional prompt)
546 "Get the next event."
547 (let ((event (read-event prompt)))
548 ;; should be gnus-characterp, but this can't be called in XEmacs anyway
549 (cons (and (numberp event) event) event)))
551 (defun gnus-sortable-date (date)
552 "Make string suitable for sorting from DATE."
553 (gnus-time-iso8601 (date-to-time date)))
555 (defun gnus-copy-file (file &optional to)
556 "Copy FILE to TO."
557 (interactive
558 (list (read-file-name "Copy file: " default-directory)
559 (read-file-name "Copy file to: " default-directory)))
560 (unless to
561 (setq to (read-file-name "Copy file to: " default-directory)))
562 (when (file-directory-p to)
563 (setq to (concat (file-name-as-directory to)
564 (file-name-nondirectory file))))
565 (copy-file file to))
567 (defvar gnus-work-buffer " *gnus work*")
569 (defun gnus-set-work-buffer ()
570 "Put point in the empty Gnus work buffer."
571 (if (get-buffer gnus-work-buffer)
572 (progn
573 (set-buffer gnus-work-buffer)
574 (erase-buffer))
575 (set-buffer (gnus-get-buffer-create gnus-work-buffer))
576 (kill-all-local-variables)
577 (mm-enable-multibyte)))
579 (defmacro gnus-group-real-name (group)
580 "Find the real name of a foreign newsgroup."
581 `(let ((gname ,group))
582 (if (string-match "^[^:]+:" gname)
583 (substring gname (match-end 0))
584 gname)))
586 (defun gnus-make-sort-function (funs)
587 "Return a composite sort condition based on the functions in FUNS."
588 (cond
589 ;; Just a simple function.
590 ((functionp funs) funs)
591 ;; No functions at all.
592 ((null funs) funs)
593 ;; A list of functions.
594 ((or (cdr funs)
595 (listp (car funs)))
596 (gnus-byte-compile
597 `(lambda (t1 t2)
598 ,(gnus-make-sort-function-1 (reverse funs)))))
599 ;; A list containing just one function.
601 (car funs))))
603 (defun gnus-make-sort-function-1 (funs)
604 "Return a composite sort condition based on the functions in FUNS."
605 (let ((function (car funs))
606 (first 't1)
607 (last 't2))
608 (when (consp function)
609 (cond
610 ;; Reversed spec.
611 ((eq (car function) 'not)
612 (setq function (cadr function)
613 first 't2
614 last 't1))
615 ((functionp function)
616 ;; Do nothing.
619 (error "Invalid sort spec: %s" function))))
620 (if (cdr funs)
621 `(or (,function ,first ,last)
622 (and (not (,function ,last ,first))
623 ,(gnus-make-sort-function-1 (cdr funs))))
624 `(,function ,first ,last))))
626 (defun gnus-turn-off-edit-menu (type)
627 "Turn off edit menu in `gnus-TYPE-mode-map'."
628 (define-key (symbol-value (intern (format "gnus-%s-mode-map" type)))
629 [menu-bar edit] 'undefined))
631 (defmacro gnus-bind-print-variables (&rest forms)
632 "Bind print-* variables and evaluate FORMS.
633 This macro is used with `prin1', `pp', etc. in order to ensure printed
634 Lisp objects are loadable. Bind `print-quoted' and `print-readably'
635 to t, and `print-escape-multibyte', `print-escape-newlines',
636 `print-escape-nonascii', `print-length', `print-level' and
637 `print-string-length' to nil."
638 `(let ((print-quoted t)
639 (print-readably t)
640 ;;print-circle
641 ;;print-continuous-numbering
642 print-escape-multibyte
643 print-escape-newlines
644 print-escape-nonascii
645 ;;print-gensym
646 print-length
647 print-level
648 print-string-length)
649 ,@forms))
651 (defun gnus-prin1 (form)
652 "Use `prin1' on FORM in the current buffer.
653 Bind `print-quoted' and `print-readably' to t, and `print-length' and
654 `print-level' to nil. See also `gnus-bind-print-variables'."
655 (gnus-bind-print-variables (prin1 form (current-buffer))))
657 (defun gnus-prin1-to-string (form)
658 "The same as `prin1'.
659 Bind `print-quoted' and `print-readably' to t, and `print-length' and
660 `print-level' to nil. See also `gnus-bind-print-variables'."
661 (gnus-bind-print-variables (prin1-to-string form)))
663 (defun gnus-pp (form)
664 "Use `pp' on FORM in the current buffer.
665 Bind `print-quoted' and `print-readably' to t, and `print-length' and
666 `print-level' to nil. See also `gnus-bind-print-variables'."
667 (gnus-bind-print-variables (pp form (current-buffer))))
669 (defun gnus-pp-to-string (form)
670 "The same as `pp-to-string'.
671 Bind `print-quoted' and `print-readably' to t, and `print-length' and
672 `print-level' to nil. See also `gnus-bind-print-variables'."
673 (gnus-bind-print-variables (pp-to-string form)))
675 (defun gnus-make-directory (directory)
676 "Make DIRECTORY (and all its parents) if it doesn't exist."
677 (require 'nnmail)
678 (let ((file-name-coding-system nnmail-pathname-coding-system))
679 (when (and directory
680 (not (file-exists-p directory)))
681 (make-directory directory t)))
684 (defun gnus-write-buffer (file)
685 "Write the current buffer's contents to FILE."
686 ;; Make sure the directory exists.
687 (gnus-make-directory (file-name-directory file))
688 (let ((file-name-coding-system nnmail-pathname-coding-system))
689 ;; Write the buffer.
690 (write-region (point-min) (point-max) file nil 'quietly)))
692 (defun gnus-delete-file (file)
693 "Delete FILE if it exists."
694 (when (file-exists-p file)
695 (delete-file file)))
697 (defun gnus-strip-whitespace (string)
698 "Return STRING stripped of all whitespace."
699 (while (string-match "[\r\n\t ]+" string)
700 (setq string (replace-match "" t t string)))
701 string)
703 (defsubst gnus-put-text-property-excluding-newlines (beg end prop val)
704 "The same as `put-text-property', but don't put this prop on any newlines in the region."
705 (save-match-data
706 (save-excursion
707 (save-restriction
708 (goto-char beg)
709 (while (re-search-forward gnus-emphasize-whitespace-regexp end 'move)
710 (gnus-put-text-property beg (match-beginning 0) prop val)
711 (setq beg (point)))
712 (gnus-put-text-property beg (point) prop val)))))
714 (defsubst gnus-put-overlay-excluding-newlines (beg end prop val)
715 "The same as `put-text-property', but don't put this prop on any newlines in the region."
716 (save-match-data
717 (save-excursion
718 (save-restriction
719 (goto-char beg)
720 (while (re-search-forward gnus-emphasize-whitespace-regexp end 'move)
721 (gnus-overlay-put
722 (gnus-make-overlay beg (match-beginning 0))
723 prop val)
724 (setq beg (point)))
725 (gnus-overlay-put (gnus-make-overlay beg (point)) prop val)))))
727 (defun gnus-put-text-property-excluding-characters-with-faces (beg end
728 prop val)
729 "The same as `put-text-property', but don't put props on characters with the `gnus-face' property."
730 (let ((b beg))
731 (while (/= b end)
732 (when (get-text-property b 'gnus-face)
733 (setq b (next-single-property-change b 'gnus-face nil end)))
734 (when (/= b end)
735 (inline
736 (gnus-put-text-property
737 b (setq b (next-single-property-change b 'gnus-face nil end))
738 prop val))))))
740 (defmacro gnus-faces-at (position)
741 "Return a list of faces at POSITION."
742 (if (featurep 'xemacs)
743 `(let ((pos ,position))
744 (mapcar-extents 'extent-face
745 nil (current-buffer) pos pos nil 'face))
746 `(let ((pos ,position))
747 (delq nil (cons (get-text-property pos 'face)
748 (mapcar
749 (lambda (overlay)
750 (overlay-get overlay 'face))
751 (overlays-at pos)))))))
753 ;;; Protected and atomic operations. dmoore@ucsd.edu 21.11.1996
754 ;;; The primary idea here is to try to protect internal datastructures
755 ;;; from becoming corrupted when the user hits C-g, or if a hook or
756 ;;; similar blows up. Often in Gnus multiple tables/lists need to be
757 ;;; updated at the same time, or information can be lost.
759 (defvar gnus-atomic-be-safe t
760 "If t, certain operations will be protected from interruption by C-g.")
762 (defmacro gnus-atomic-progn (&rest forms)
763 "Evaluate FORMS atomically, which means to protect the evaluation
764 from being interrupted by the user. An error from the forms themselves
765 will return without finishing the operation. Since interrupts from
766 the user are disabled, it is recommended that only the most minimal
767 operations are performed by FORMS. If you wish to assign many
768 complicated values atomically, compute the results into temporary
769 variables and then do only the assignment atomically."
770 `(let ((inhibit-quit gnus-atomic-be-safe))
771 ,@forms))
773 (put 'gnus-atomic-progn 'lisp-indent-function 0)
775 (defmacro gnus-atomic-progn-assign (protect &rest forms)
776 "Evaluate FORMS, but insure that the variables listed in PROTECT
777 are not changed if anything in FORMS signals an error or otherwise
778 non-locally exits. The variables listed in PROTECT are updated atomically.
779 It is safe to use gnus-atomic-progn-assign with long computations.
781 Note that if any of the symbols in PROTECT were unbound, they will be
782 set to nil on a successful assignment. In case of an error or other
783 non-local exit, it will still be unbound."
784 (let* ((temp-sym-map (mapcar (lambda (x) (list (make-symbol
785 (concat (symbol-name x)
786 "-tmp"))
788 protect))
789 (sym-temp-map (mapcar (lambda (x) (list (cadr x) (car x)))
790 temp-sym-map))
791 (temp-sym-let (mapcar (lambda (x) (list (car x)
792 `(and (boundp ',(cadr x))
793 ,(cadr x))))
794 temp-sym-map))
795 (sym-temp-let sym-temp-map)
796 (temp-sym-assign (apply 'append temp-sym-map))
797 (sym-temp-assign (apply 'append sym-temp-map))
798 (result (make-symbol "result-tmp")))
799 `(let (,@temp-sym-let
800 ,result)
801 (let ,sym-temp-let
802 (setq ,result (progn ,@forms))
803 (setq ,@temp-sym-assign))
804 (let ((inhibit-quit gnus-atomic-be-safe))
805 (setq ,@sym-temp-assign))
806 ,result)))
808 (put 'gnus-atomic-progn-assign 'lisp-indent-function 1)
809 ;(put 'gnus-atomic-progn-assign 'edebug-form-spec '(sexp body))
811 (defmacro gnus-atomic-setq (&rest pairs)
812 "Similar to setq, except that the real symbols are only assigned when
813 there are no errors. And when the real symbols are assigned, they are
814 done so atomically. If other variables might be changed via side-effect,
815 see gnus-atomic-progn-assign. It is safe to use gnus-atomic-setq
816 with potentially long computations."
817 (let ((tpairs pairs)
818 syms)
819 (while tpairs
820 (push (car tpairs) syms)
821 (setq tpairs (cddr tpairs)))
822 `(gnus-atomic-progn-assign ,syms
823 (setq ,@pairs))))
825 ;(put 'gnus-atomic-setq 'edebug-form-spec '(body))
828 ;;; Functions for saving to babyl/mail files.
830 (eval-when-compile
831 (condition-case nil
832 (progn
833 (require 'rmail)
834 (autoload 'rmail-update-summary "rmailsum"))
835 (error
836 (define-compiler-macro rmail-select-summary (&rest body)
837 ;; Rmail of the XEmacs version is supplied by the package, and
838 ;; requires tm and apel packages. However, there may be those
839 ;; who haven't installed those packages. This macro helps such
840 ;; people even if they install those packages later.
841 `(eval '(rmail-select-summary ,@body)))
842 ;; If there's rmail but there's no tm (or there's apel of the
843 ;; mainstream, not the XEmacs version), loading rmail of the XEmacs
844 ;; version fails halfway, however it provides the rmail-select-summary
845 ;; macro which uses the following functions:
846 (autoload 'rmail-summary-displayed "rmail")
847 (autoload 'rmail-maybe-display-summary "rmail")))
848 (defvar rmail-default-rmail-file)
849 (defvar mm-text-coding-system))
851 (defun gnus-output-to-rmail (filename &optional ask)
852 "Append the current article to an Rmail file named FILENAME."
853 (require 'rmail)
854 (require 'mm-util)
855 ;; Most of these codes are borrowed from rmailout.el.
856 (setq filename (expand-file-name filename))
857 (setq rmail-default-rmail-file filename)
858 (let ((artbuf (current-buffer))
859 (tmpbuf (get-buffer-create " *Gnus-output*")))
860 (save-excursion
861 (or (get-file-buffer filename)
862 (file-exists-p filename)
863 (if (or (not ask)
864 (gnus-yes-or-no-p
865 (concat "\"" filename "\" does not exist, create it? ")))
866 (let ((file-buffer (create-file-buffer filename)))
867 (save-excursion
868 (set-buffer file-buffer)
869 (rmail-insert-rmail-file-header)
870 (let ((require-final-newline nil)
871 (coding-system-for-write mm-text-coding-system))
872 (gnus-write-buffer filename)))
873 (kill-buffer file-buffer))
874 (error "Output file does not exist")))
875 (set-buffer tmpbuf)
876 (erase-buffer)
877 (insert-buffer-substring artbuf)
878 (gnus-convert-article-to-rmail)
879 ;; Decide whether to append to a file or to an Emacs buffer.
880 (let ((outbuf (get-file-buffer filename)))
881 (if (not outbuf)
882 (let ((file-name-coding-system nnmail-pathname-coding-system))
883 (mm-append-to-file (point-min) (point-max) filename))
884 ;; File has been visited, in buffer OUTBUF.
885 (set-buffer outbuf)
886 (let ((buffer-read-only nil)
887 (msg (and (boundp 'rmail-current-message)
888 (symbol-value 'rmail-current-message))))
889 ;; If MSG is non-nil, buffer is in RMAIL mode.
890 (when msg
891 (widen)
892 (narrow-to-region (point-max) (point-max)))
893 (insert-buffer-substring tmpbuf)
894 (when msg
895 (goto-char (point-min))
896 (widen)
897 (search-backward "\n\^_")
898 (narrow-to-region (point) (point-max))
899 (rmail-count-new-messages t)
900 (when (rmail-summary-exists)
901 (rmail-select-summary
902 (rmail-update-summary)))
903 (rmail-count-new-messages t)
904 (rmail-show-message msg))
905 (save-buffer)))))
906 (kill-buffer tmpbuf)))
908 (defun gnus-output-to-mail (filename &optional ask)
909 "Append the current article to a mail file named FILENAME."
910 (setq filename (expand-file-name filename))
911 (let ((artbuf (current-buffer))
912 (tmpbuf (get-buffer-create " *Gnus-output*")))
913 (save-excursion
914 ;; Create the file, if it doesn't exist.
915 (when (and (not (get-file-buffer filename))
916 (not (file-exists-p filename)))
917 (if (or (not ask)
918 (gnus-y-or-n-p
919 (concat "\"" filename "\" does not exist, create it? ")))
920 (let ((file-buffer (create-file-buffer filename)))
921 (save-excursion
922 (set-buffer file-buffer)
923 (let ((require-final-newline nil)
924 (coding-system-for-write mm-text-coding-system))
925 (gnus-write-buffer filename)))
926 (kill-buffer file-buffer))
927 (error "Output file does not exist")))
928 (set-buffer tmpbuf)
929 (erase-buffer)
930 (insert-buffer-substring artbuf)
931 (goto-char (point-min))
932 (if (looking-at "From ")
933 (forward-line 1)
934 (insert "From nobody " (current-time-string) "\n"))
935 (let (case-fold-search)
936 (while (re-search-forward "^From " nil t)
937 (beginning-of-line)
938 (insert ">")))
939 ;; Decide whether to append to a file or to an Emacs buffer.
940 (let ((outbuf (get-file-buffer filename)))
941 (if (not outbuf)
942 (let ((buffer-read-only nil))
943 (save-excursion
944 (goto-char (point-max))
945 (forward-char -2)
946 (unless (looking-at "\n\n")
947 (goto-char (point-max))
948 (unless (bolp)
949 (insert "\n"))
950 (insert "\n"))
951 (goto-char (point-max))
952 (let ((file-name-coding-system nnmail-pathname-coding-system))
953 (mm-append-to-file (point-min) (point-max) filename))))
954 ;; File has been visited, in buffer OUTBUF.
955 (set-buffer outbuf)
956 (let ((buffer-read-only nil))
957 (goto-char (point-max))
958 (unless (eobp)
959 (insert "\n"))
960 (insert "\n")
961 (insert-buffer-substring tmpbuf)))))
962 (kill-buffer tmpbuf)))
964 (defun gnus-convert-article-to-rmail ()
965 "Convert article in current buffer to Rmail message format."
966 (let ((buffer-read-only nil))
967 ;; Convert article directly into Babyl format.
968 (goto-char (point-min))
969 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
970 (while (search-forward "\n\^_" nil t) ;single char
971 (replace-match "\n^_" t t)) ;2 chars: "^" and "_"
972 (goto-char (point-max))
973 (insert "\^_")))
975 (defun gnus-map-function (funs arg)
976 "Apply the result of the first function in FUNS to the second, and so on.
977 ARG is passed to the first function."
978 (while funs
979 (setq arg (funcall (pop funs) arg)))
980 arg)
982 (defun gnus-run-hooks (&rest funcs)
983 "Does the same as `run-hooks', but saves the current buffer."
984 (save-current-buffer
985 (apply 'run-hooks funcs)))
987 ;;; Various
989 (defvar gnus-group-buffer) ; Compiler directive
990 (defun gnus-alive-p ()
991 "Say whether Gnus is running or not."
992 (and (boundp 'gnus-group-buffer)
993 (get-buffer gnus-group-buffer)
994 (save-excursion
995 (set-buffer gnus-group-buffer)
996 (eq major-mode 'gnus-group-mode))))
998 (defun gnus-remove-duplicates (list)
999 (let (new)
1000 (while list
1001 (or (member (car list) new)
1002 (setq new (cons (car list) new)))
1003 (setq list (cdr list)))
1004 (nreverse new)))
1006 (defun gnus-remove-if (predicate list)
1007 "Return a copy of LIST with all items satisfying PREDICATE removed."
1008 (let (out)
1009 (while list
1010 (unless (funcall predicate (car list))
1011 (push (car list) out))
1012 (setq list (cdr list)))
1013 (nreverse out)))
1015 (if (fboundp 'assq-delete-all)
1016 (defalias 'gnus-delete-alist 'assq-delete-all)
1017 (defun gnus-delete-alist (key alist)
1018 "Delete from ALIST all elements whose car is KEY.
1019 Return the modified alist."
1020 (let (entry)
1021 (while (setq entry (assq key alist))
1022 (setq alist (delq entry alist)))
1023 alist)))
1025 (defmacro gnus-pull (key alist &optional assoc-p)
1026 "Modify ALIST to be without KEY."
1027 (unless (symbolp alist)
1028 (error "Not a symbol: %s" alist))
1029 (let ((fun (if assoc-p 'assoc 'assq)))
1030 `(setq ,alist (delq (,fun ,key ,alist) ,alist))))
1032 (defun gnus-globalify-regexp (re)
1033 "Return a regexp that matches a whole line, iff RE matches a part of it."
1034 (concat (unless (string-match "^\\^" re) "^.*")
1036 (unless (string-match "\\$$" re) ".*$")))
1038 (defun gnus-set-window-start (&optional point)
1039 "Set the window start to POINT, or (point) if nil."
1040 (let ((win (gnus-get-buffer-window (current-buffer) t)))
1041 (when win
1042 (set-window-start win (or point (point))))))
1044 (defun gnus-annotation-in-region-p (b e)
1045 (if (= b e)
1046 (eq (cadr (memq 'gnus-undeletable (text-properties-at b))) t)
1047 (text-property-any b e 'gnus-undeletable t)))
1049 (defun gnus-or (&rest elems)
1050 "Return non-nil if any of the elements are non-nil."
1051 (catch 'found
1052 (while elems
1053 (when (pop elems)
1054 (throw 'found t)))))
1056 (defun gnus-and (&rest elems)
1057 "Return non-nil if all of the elements are non-nil."
1058 (catch 'found
1059 (while elems
1060 (unless (pop elems)
1061 (throw 'found nil)))
1064 (defun gnus-write-active-file (file hashtb &optional full-names)
1065 (let ((coding-system-for-write nnmail-active-file-coding-system))
1066 (with-temp-file file
1067 (mapatoms
1068 (lambda (sym)
1069 (when (and sym
1070 (boundp sym)
1071 (symbol-value sym))
1072 (insert (format "%S %d %d y\n"
1073 (if full-names
1075 (intern (gnus-group-real-name (symbol-name sym))))
1076 (or (cdr (symbol-value sym))
1077 (car (symbol-value sym)))
1078 (car (symbol-value sym))))))
1079 hashtb)
1080 (goto-char (point-max))
1081 (while (search-backward "\\." nil t)
1082 (delete-char 1)))))
1084 ;; Fixme: Why not use `with-output-to-temp-buffer'?
1085 (defmacro gnus-with-output-to-file (file &rest body)
1086 (let ((buffer (make-symbol "output-buffer"))
1087 (size (make-symbol "output-buffer-size"))
1088 (leng (make-symbol "output-buffer-length"))
1089 (append (make-symbol "output-buffer-append")))
1090 `(let* ((,size 131072)
1091 (,buffer (make-string ,size 0))
1092 (,leng 0)
1093 (,append nil)
1094 (standard-output
1095 (lambda (c)
1096 (aset ,buffer ,leng c)
1098 (if (= ,size (setq ,leng (1+ ,leng)))
1099 (progn (write-region ,buffer nil ,file ,append 'no-msg)
1100 (setq ,leng 0
1101 ,append t))))))
1102 ,@body
1103 (when (> ,leng 0)
1104 (let ((coding-system-for-write 'no-conversion))
1105 (write-region (substring ,buffer 0 ,leng) nil ,file
1106 ,append 'no-msg))))))
1108 (put 'gnus-with-output-to-file 'lisp-indent-function 1)
1109 (put 'gnus-with-output-to-file 'edebug-form-spec '(form body))
1111 (if (fboundp 'union)
1112 (defalias 'gnus-union 'union)
1113 (defun gnus-union (l1 l2)
1114 "Set union of lists L1 and L2."
1115 (cond ((null l1) l2)
1116 ((null l2) l1)
1117 ((equal l1 l2) l1)
1119 (or (>= (length l1) (length l2))
1120 (setq l1 (prog1 l2 (setq l2 l1))))
1121 (while l2
1122 (or (member (car l2) l1)
1123 (push (car l2) l1))
1124 (pop l2))
1125 l1))))
1127 (defun gnus-add-text-properties-when
1128 (property value start end properties &optional object)
1129 "Like `gnus-add-text-properties', only applied on where PROPERTY is VALUE."
1130 (let (point)
1131 (while (and start
1132 (< start end) ;; XEmacs will loop for every when start=end.
1133 (setq point (text-property-not-all start end property value)))
1134 (gnus-add-text-properties start point properties object)
1135 (setq start (text-property-any point end property value)))
1136 (if start
1137 (gnus-add-text-properties start end properties object))))
1139 (defun gnus-remove-text-properties-when
1140 (property value start end properties &optional object)
1141 "Like `remove-text-properties', only applied on where PROPERTY is VALUE."
1142 (let (point)
1143 (while (and start
1144 (< start end)
1145 (setq point (text-property-not-all start end property value)))
1146 (remove-text-properties start point properties object)
1147 (setq start (text-property-any point end property value)))
1148 (if start
1149 (remove-text-properties start end properties object))
1152 ;; This might use `compare-strings' to reduce consing in the
1153 ;; case-insensitive case, but it has to cope with null args.
1154 ;; (`string-equal' uses symbol print names.)
1155 (defun gnus-string-equal (x y)
1156 "Like `string-equal', except it compares case-insensitively."
1157 (and (= (length x) (length y))
1158 (or (string-equal x y)
1159 (string-equal (downcase x) (downcase y)))))
1161 (defcustom gnus-use-byte-compile t
1162 "If non-nil, byte-compile crucial run-time code.
1163 Setting it to nil has no effect after the first time `gnus-byte-compile'
1164 is run."
1165 :type 'boolean
1166 :version "21.4"
1167 :group 'gnus-various)
1169 (defun gnus-byte-compile (form)
1170 "Byte-compile FORM if `gnus-use-byte-compile' is non-nil."
1171 (if gnus-use-byte-compile
1172 (progn
1173 (condition-case nil
1174 ;; Work around a bug in XEmacs 21.4
1175 (require 'byte-optimize)
1176 (error))
1177 (require 'bytecomp)
1178 (defalias 'gnus-byte-compile
1179 (lambda (form)
1180 (let ((byte-compile-warnings '(unresolved callargs redefine)))
1181 (byte-compile form))))
1182 (gnus-byte-compile form))
1183 form))
1185 (defun gnus-remassoc (key alist)
1186 "Delete by side effect any elements of LIST whose car is `equal' to KEY.
1187 The modified LIST is returned. If the first member
1188 of LIST has a car that is `equal' to KEY, there is no way to remove it
1189 by side effect; therefore, write `(setq foo (remassoc key foo))' to be
1190 sure of changing the value of `foo'."
1191 (when alist
1192 (if (equal key (caar alist))
1193 (cdr alist)
1194 (setcdr alist (gnus-remassoc key (cdr alist)))
1195 alist)))
1197 (defun gnus-update-alist-soft (key value alist)
1198 (if value
1199 (cons (cons key value) (gnus-remassoc key alist))
1200 (gnus-remassoc key alist)))
1202 (defun gnus-create-info-command (node)
1203 "Create a command that will go to info NODE."
1204 `(lambda ()
1205 (interactive)
1206 ,(concat "Enter the info system at node " node)
1207 (Info-goto-node ,node)
1208 (setq gnus-info-buffer (current-buffer))
1209 (gnus-configure-windows 'info)))
1211 (defun gnus-not-ignore (&rest args)
1214 (defvar gnus-directory-sep-char-regexp "/"
1215 "The regexp of directory separator character.
1216 If you find some problem with the directory separator character, try
1217 \"[/\\\\\]\" for some systems.")
1219 (defun gnus-url-unhex (x)
1220 (if (> x ?9)
1221 (if (>= x ?a)
1222 (+ 10 (- x ?a))
1223 (+ 10 (- x ?A)))
1224 (- x ?0)))
1226 ;; Fixme: Do it like QP.
1227 (defun gnus-url-unhex-string (str &optional allow-newlines)
1228 "Remove %XX, embedded spaces, etc in a url.
1229 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
1230 decoding of carriage returns and line feeds in the string, which is normally
1231 forbidden in URL encoding."
1232 (let ((tmp "")
1233 (case-fold-search t))
1234 (while (string-match "%[0-9a-f][0-9a-f]" str)
1235 (let* ((start (match-beginning 0))
1236 (ch1 (gnus-url-unhex (elt str (+ start 1))))
1237 (code (+ (* 16 ch1)
1238 (gnus-url-unhex (elt str (+ start 2))))))
1239 (setq tmp (concat
1240 tmp (substring str 0 start)
1241 (cond
1242 (allow-newlines
1243 (char-to-string code))
1244 ((or (= code ?\n) (= code ?\r))
1245 " ")
1246 (t (char-to-string code))))
1247 str (substring str (match-end 0)))))
1248 (setq tmp (concat tmp str))
1249 tmp))
1251 (defun gnus-make-predicate (spec)
1252 "Transform SPEC into a function that can be called.
1253 SPEC is a predicate specifier that contains stuff like `or', `and',
1254 `not', lists and functions. The functions all take one parameter."
1255 `(lambda (elem) ,(gnus-make-predicate-1 spec)))
1257 (defun gnus-make-predicate-1 (spec)
1258 (cond
1259 ((symbolp spec)
1260 `(,spec elem))
1261 ((listp spec)
1262 (if (memq (car spec) '(or and not))
1263 `(,(car spec) ,@(mapcar 'gnus-make-predicate-1 (cdr spec)))
1264 (error "Invalid predicate specifier: %s" spec)))))
1266 (defun gnus-local-map-property (map)
1267 "Return a list suitable for a text property list specifying keymap MAP."
1268 (cond
1269 ((featurep 'xemacs)
1270 (list 'keymap map))
1271 ((>= emacs-major-version 21)
1272 (list 'keymap map))
1274 (list 'local-map map))))
1276 (defmacro gnus-completing-read-maybe-default (prompt table &optional predicate
1277 require-match initial-contents
1278 history default)
1279 "Like `completing-read', allowing for non-existent 7th arg in older XEmacsen."
1280 `(completing-read ,prompt ,table ,predicate ,require-match
1281 ,initial-contents ,history
1282 ,@(if (and (featurep 'xemacs) (< emacs-minor-version 2))
1284 (list default))))
1286 (defun gnus-completing-read (prompt table &optional predicate require-match
1287 history)
1288 (when (and history
1289 (not (boundp history)))
1290 (set history nil))
1291 (gnus-completing-read-maybe-default
1292 (if (symbol-value history)
1293 (concat prompt " (" (car (symbol-value history)) "): ")
1294 (concat prompt ": "))
1295 table
1296 predicate
1297 require-match
1299 history
1300 (car (symbol-value history))))
1302 (defun gnus-graphic-display-p ()
1303 (or (and (fboundp 'display-graphic-p)
1304 (display-graphic-p))
1305 ;;;!!!This is bogus. Fixme!
1306 (and (featurep 'xemacs)
1307 t)))
1309 (put 'gnus-parse-without-error 'lisp-indent-function 0)
1310 (put 'gnus-parse-without-error 'edebug-form-spec '(body))
1312 (defmacro gnus-parse-without-error (&rest body)
1313 "Allow continuing onto the next line even if an error occurs."
1314 `(while (not (eobp))
1315 (condition-case ()
1316 (progn
1317 ,@body
1318 (goto-char (point-max)))
1319 (error
1320 (gnus-error 4 "Invalid data on line %d"
1321 (count-lines (point-min) (point)))
1322 (forward-line 1)))))
1324 (defun gnus-cache-file-contents (file variable function)
1325 "Cache the contents of FILE in VARIABLE. The contents come from FUNCTION."
1326 (let ((time (nth 5 (file-attributes file)))
1327 contents value)
1328 (if (or (null (setq value (symbol-value variable)))
1329 (not (equal (car value) file))
1330 (not (equal (nth 1 value) time)))
1331 (progn
1332 (setq contents (funcall function file))
1333 (set variable (list file time contents))
1334 contents)
1335 (nth 2 value))))
1337 (defun gnus-multiple-choice (prompt choice &optional idx)
1338 "Ask user a multiple choice question.
1339 CHOICE is a list of the choice char and help message at IDX."
1340 (let (tchar buf)
1341 (save-window-excursion
1342 (save-excursion
1343 (while (not tchar)
1344 (message "%s (%s): "
1345 prompt
1346 (concat
1347 (mapconcat (lambda (s) (char-to-string (car s)))
1348 choice ", ") ", ?"))
1349 (setq tchar (read-char))
1350 (when (not (assq tchar choice))
1351 (setq tchar nil)
1352 (setq buf (get-buffer-create "*Gnus Help*"))
1353 (pop-to-buffer buf)
1354 (fundamental-mode) ; for Emacs 20.4+
1355 (buffer-disable-undo)
1356 (erase-buffer)
1357 (insert prompt ":\n\n")
1358 (let ((max -1)
1359 (list choice)
1360 (alist choice)
1361 (idx (or idx 1))
1362 (i 0)
1363 n width pad format)
1364 ;; find the longest string to display
1365 (while list
1366 (setq n (length (nth idx (car list))))
1367 (unless (> max n)
1368 (setq max n))
1369 (setq list (cdr list)))
1370 (setq max (+ max 4)) ; %c, `:', SPACE, a SPACE at end
1371 (setq n (/ (1- (window-width)) max)) ; items per line
1372 (setq width (/ (1- (window-width)) n)) ; width of each item
1373 ;; insert `n' items, each in a field of width `width'
1374 (while alist
1375 (if (< i n)
1377 (setq i 0)
1378 (delete-char -1) ; the `\n' takes a char
1379 (insert "\n"))
1380 (setq pad (- width 3))
1381 (setq format (concat "%c: %-" (int-to-string pad) "s"))
1382 (insert (format format (caar alist) (nth idx (car alist))))
1383 (setq alist (cdr alist))
1384 (setq i (1+ i))))))))
1385 (if (buffer-live-p buf)
1386 (kill-buffer buf))
1387 tchar))
1389 (defun gnus-select-frame-set-input-focus (frame)
1390 "Select FRAME, raise it, and set input focus, if possible."
1391 (cond ((featurep 'xemacs)
1392 (raise-frame frame)
1393 (select-frame frame)
1394 (focus-frame frame))
1395 ;; The function `select-frame-set-input-focus' won't set
1396 ;; the input focus under Emacs 21.2 and X window system.
1397 ;;((fboundp 'select-frame-set-input-focus)
1398 ;; (defalias 'gnus-select-frame-set-input-focus
1399 ;; 'select-frame-set-input-focus)
1400 ;; (select-frame-set-input-focus frame))
1402 (raise-frame frame)
1403 (select-frame frame)
1404 (cond ((and (eq window-system 'x)
1405 (fboundp 'x-focus-frame))
1406 (x-focus-frame frame))
1407 ((eq window-system 'w32)
1408 (w32-focus-frame frame)))
1409 (when focus-follows-mouse
1410 (set-mouse-position frame (1- (frame-width frame)) 0)))))
1412 (defun gnus-frame-or-window-display-name (object)
1413 "Given a frame or window, return the associated display name.
1414 Return nil otherwise."
1415 (if (featurep 'xemacs)
1416 (device-connection (dfw-device object))
1417 (if (or (framep object)
1418 (and (windowp object)
1419 (setq object (window-frame object))))
1420 (let ((display (frame-parameter object 'display)))
1421 (if (and (stringp display)
1422 ;; Exclude invalid display names.
1423 (string-match "\\`[^:]*:[0-9]+\\(\\.[0-9]+\\)?\\'"
1424 display))
1425 display)))))
1427 ;; Fixme: This has only one use (in gnus-agent), which isn't worthwhile.
1428 (defmacro gnus-mapcar (function seq1 &rest seqs2_n)
1429 "Apply FUNCTION to each element of the sequences, and make a list of the results.
1430 If there are several sequences, FUNCTION is called with that many arguments,
1431 and mapping stops as soon as the shortest sequence runs out. With just one
1432 sequence, this is like `mapcar'. With several, it is like the Common Lisp
1433 `mapcar' function extended to arbitrary sequence types."
1435 (if seqs2_n
1436 (let* ((seqs (cons seq1 seqs2_n))
1437 (cnt 0)
1438 (heads (mapcar (lambda (seq)
1439 (make-symbol (concat "head"
1440 (int-to-string
1441 (setq cnt (1+ cnt))))))
1442 seqs))
1443 (result (make-symbol "result"))
1444 (result-tail (make-symbol "result-tail")))
1445 `(let* ,(let* ((bindings (cons nil nil))
1446 (heads heads))
1447 (nconc bindings (list (list result '(cons nil nil))))
1448 (nconc bindings (list (list result-tail result)))
1449 (while heads
1450 (nconc bindings (list (list (pop heads) (pop seqs)))))
1451 (cdr bindings))
1452 (while (and ,@heads)
1453 (setcdr ,result-tail (cons (funcall ,function
1454 ,@(mapcar (lambda (h) (list 'car h))
1455 heads))
1456 nil))
1457 (setq ,result-tail (cdr ,result-tail)
1458 ,@(apply 'nconc (mapcar (lambda (h) (list h (list 'cdr h))) heads))))
1459 (cdr ,result)))
1460 `(mapcar ,function ,seq1)))
1462 (if (fboundp 'merge)
1463 (defalias 'gnus-merge 'merge)
1464 ;; Adapted from cl-seq.el
1465 (defun gnus-merge (type list1 list2 pred)
1466 "Destructively merge lists LIST1 and LIST2 to produce a new list.
1467 Argument TYPE is for compatibility and ignored.
1468 Ordering of the elements is preserved according to PRED, a `less-than'
1469 predicate on the elements."
1470 (let ((res nil))
1471 (while (and list1 list2)
1472 (if (funcall pred (car list2) (car list1))
1473 (push (pop list2) res)
1474 (push (pop list1) res)))
1475 (nconc (nreverse res) list1 list2))))
1477 (eval-when-compile
1478 (defvar xemacs-codename))
1480 (defun gnus-emacs-version ()
1481 "Stringified Emacs version."
1482 (let ((system-v
1483 (cond
1484 ((eq gnus-user-agent 'emacs-gnus-config)
1485 system-configuration)
1486 ((eq gnus-user-agent 'emacs-gnus-type)
1487 (symbol-name system-type))
1488 (t nil))))
1489 (cond
1490 ((eq gnus-user-agent 'gnus)
1491 nil)
1492 ((string-match "^\\(\\([.0-9]+\\)*\\)\\.[0-9]+$" emacs-version)
1493 (concat "Emacs/" (match-string 1 emacs-version)
1494 (if system-v
1495 (concat " (" system-v ")")
1496 "")))
1497 ((string-match
1498 "\\([A-Z]*[Mm][Aa][Cc][Ss]\\)[^(]*\\(\\((beta.*)\\|'\\)\\)?"
1499 emacs-version)
1500 (concat
1501 (match-string 1 emacs-version)
1502 (format "/%d.%d" emacs-major-version emacs-minor-version)
1503 (if (match-beginning 3)
1504 (match-string 3 emacs-version)
1506 (if (boundp 'xemacs-codename)
1507 (concat
1508 " (" xemacs-codename
1509 (if system-v
1510 (concat ", " system-v ")")
1511 ")"))
1512 "")))
1513 (t emacs-version))))
1515 (provide 'gnus-util)
1517 ;;; arch-tag: f94991af-d32b-4c97-8c26-ca12a934de49
1518 ;;; gnus-util.el ends here