718fe728603000ee5dfa5f901d0644432f2140b7
[elisp/gnus.git-] / lisp / gnus-util.el
1 ;;; gnus-util.el --- utility functions for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;      Tatsuya Ichikawa <t-ichi@po.shiojiri.ne.jp>
7 ;; Keywords: mail, news, MIME
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; Nothing in this file depends on any other parts of Gnus -- all
29 ;; functions and macros in this file are utility functions that are
30 ;; used by Gnus and may be used by any other package without loading
31 ;; Gnus first.
32
33 ;;; Code:
34
35 (eval-when-compile
36   (require 'cl)
37   ;; Fixme: this should be a gnus variable, not nnmail-.
38   (defvar nnmail-pathname-coding-system))
39 (eval-when-compile (require 'static))
40
41 (require 'custom)
42 (require 'nnheader)
43 (require 'time-date)
44
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
52 (eval-and-compile
53   (cond
54    ((fboundp 'replace-in-string)
55     (defalias 'gnus-replace-in-string 'replace-in-string))
56    ((fboundp 'replace-regexp-in-string)
57     (defun gnus-replace-in-string  (string regexp newtext &optional literal)
58       (replace-regexp-in-string regexp newtext string nil literal)))
59    (t
60     (defun gnus-replace-in-string (string regexp newtext &optional literal)
61       (let ((start 0) tail)
62         (while (string-match regexp string start)
63           (setq tail (- (length string) (match-end 0)))
64           (setq string (replace-match newtext nil literal string))
65           (setq start (- (length string) tail))))
66       string))))
67
68 (defun gnus-boundp (variable)
69   "Return non-nil if VARIABLE is bound and non-nil."
70   (and (boundp variable)
71        (symbol-value variable)))
72
73 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
74   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
75   (let ((tempvar (make-symbol "GnusStartBufferWindow"))
76         (w (make-symbol "w"))
77         (buf (make-symbol "buf"))
78         (frame (make-symbol "frame")))
79     `(let* ((,tempvar (selected-window))
80             (,buf ,buffer)
81             (,w (gnus-get-buffer-window ,buf 'visible))
82             ,frame)
83        (unwind-protect
84            (progn
85              (if ,w
86                  (progn
87                    (select-window ,w)
88                    (set-buffer (window-buffer ,w)))
89                (pop-to-buffer ,buf))
90              ,@forms)
91          (setq ,frame (selected-frame))
92          (select-window ,tempvar)
93          (select-frame ,frame)))))
94
95 (put 'gnus-eval-in-buffer-window 'lisp-indent-function 1)
96 (put 'gnus-eval-in-buffer-window 'edebug-form-spec '(form body))
97
98 (defmacro gnus-intern-safe (string hashtable)
99   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
100   `(let ((symbol (intern ,string ,hashtable)))
101      (or (boundp symbol)
102          (set symbol nil))
103      symbol))
104
105 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
106 ;; to limit the length of a string.  This function is necessary since
107 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
108 (defsubst gnus-limit-string (str width)
109   (if (> (length str) width)
110       (substring str 0 width)
111     str))
112
113 (defsubst gnus-functionp (form)
114   "Return non-nil if FORM is funcallable."
115   (or (and (symbolp form) (fboundp form))
116       (and (listp form) (eq (car form) 'lambda))
117       (byte-code-function-p form)))
118
119 (defsubst gnus-goto-char (point)
120   (and point (goto-char point)))
121
122 (defmacro gnus-buffer-exists-p (buffer)
123   `(let ((buffer ,buffer))
124      (when buffer
125        (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
126                 buffer))))
127
128 (defmacro gnus-kill-buffer (buffer)
129   `(let ((buf ,buffer))
130      (when (gnus-buffer-exists-p buf)
131        (kill-buffer buf))))
132
133 (static-cond
134  ((fboundp 'point-at-bol)
135   (defalias 'gnus-point-at-bol 'point-at-bol))
136  ((fboundp 'line-beginning-position)
137   (defalias 'gnus-point-at-bol 'line-beginning-position))
138  (t
139   (defun gnus-point-at-bol ()
140     "Return point at the beginning of the line."
141     (let ((p (point)))
142       (beginning-of-line)
143       (prog1
144           (point)
145         (goto-char p))))
146   ))
147 (static-cond
148  ((fboundp 'point-at-eol)
149   (defalias 'gnus-point-at-eol 'point-at-eol))
150  ((fboundp 'line-end-position)
151   (defalias 'gnus-point-at-eol 'line-end-position))
152  (t
153   (defun gnus-point-at-eol ()
154     "Return point at the end of the line."
155     (let ((p (point)))
156       (end-of-line)
157       (prog1
158           (point)
159         (goto-char p))))
160   ))
161
162 (defun gnus-delete-first (elt list)
163   "Delete by side effect the first occurrence of ELT as a member of LIST."
164   (if (equal (car list) elt)
165       (cdr list)
166     (let ((total list))
167       (while (and (cdr list)
168                   (not (equal (cadr list) elt)))
169         (setq list (cdr list)))
170       (when (cdr list)
171         (setcdr list (cddr list)))
172       total)))
173
174 ;; Delete the current line (and the next N lines).
175 (defmacro gnus-delete-line (&optional n)
176   `(delete-region (progn (beginning-of-line) (point))
177                   (progn (forward-line ,(or n 1)) (point))))
178
179 (defun gnus-byte-code (func)
180   "Return a form that can be `eval'ed based on FUNC."
181   (let ((fval (indirect-function func)))
182     (if (byte-code-function-p fval)
183         (let ((flist (append fval nil)))
184           (setcar flist 'byte-code)
185           flist)
186       (cons 'progn (cddr fval)))))
187
188 (defun gnus-extract-address-components (from)
189   (let (name address)
190     ;; First find the address - the thing with the @ in it.  This may
191     ;; not be accurate in mail addresses, but does the trick most of
192     ;; the time in news messages.
193     (when (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
194       (setq address (substring from (match-beginning 0) (match-end 0))))
195     ;; Then we check whether the "name <address>" format is used.
196     (and address
197          ;; Linear white space is not required.
198          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
199          (and (setq name (substring from 0 (match-beginning 0)))
200               ;; Strip any quotes from the name.
201               (string-match "\".*\"" name)
202               (setq name (substring name 1 (1- (match-end 0))))))
203     ;; If not, then "address (name)" is used.
204     (or name
205         (and (string-match "(.+)" from)
206              (setq name (substring from (1+ (match-beginning 0))
207                                    (1- (match-end 0)))))
208         (and (string-match "()" from)
209              (setq name address))
210         ;; XOVER might not support folded From headers.
211         (and (string-match "(.*" from)
212              (setq name (substring from (1+ (match-beginning 0))
213                                    (match-end 0)))))
214     (list (if (string= name "") nil name) (or address from))))
215
216
217 (defun gnus-fetch-field (field)
218   "Return the value of the header FIELD of current article."
219   (save-excursion
220     (save-restriction
221       (let ((case-fold-search t)
222             (inhibit-point-motion-hooks t))
223         (nnheader-narrow-to-headers)
224         (message-fetch-field field)))))
225
226 (defun gnus-goto-colon ()
227   (beginning-of-line)
228   (let ((eol (gnus-point-at-eol)))
229     (goto-char (or (text-property-any (point) eol 'gnus-position t)
230                    (search-forward ":" eol t)
231                    (point)))))
232
233 (defun gnus-decode-newsgroups (newsgroups group &optional method)
234   (let ((method (or method (gnus-find-method-for-group group))))
235     (mapconcat (lambda (group)
236                  (gnus-group-name-decode group (gnus-group-name-charset
237                                                 method group)))
238                (message-tokenize-header newsgroups)
239                ",")))
240
241 (defun gnus-remove-text-with-property (prop)
242   "Delete all text in the current buffer with text property PROP."
243   (save-excursion
244     (goto-char (point-min))
245     (while (not (eobp))
246       (while (get-text-property (point) prop)
247         (delete-char 1))
248       (goto-char (next-single-property-change (point) prop nil (point-max))))))
249
250 (require 'nnheader)
251 (defun gnus-newsgroup-directory-form (newsgroup)
252   "Make hierarchical directory name from NEWSGROUP name."
253   (let* ((newsgroup (gnus-newsgroup-savable-name newsgroup))
254          (idx (string-match ":" newsgroup)))
255     (concat
256      (if idx (substring newsgroup 0 idx))
257      (if idx "/")
258      (nnheader-replace-chars-in-string
259       (if idx (substring newsgroup (1+ idx)) newsgroup)
260       ?. ?/))))
261
262 (defun gnus-newsgroup-savable-name (group)
263   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
264   ;; with dots.
265   (nnheader-replace-chars-in-string group ?/ ?.))
266
267 (defun gnus-string> (s1 s2)
268   (not (or (string< s1 s2)
269            (string= s1 s2))))
270
271 ;;; Time functions.
272
273 (defun gnus-file-newer-than (file date)
274   (let ((fdate (nth 5 (file-attributes file))))
275     (or (> (car fdate) (car date))
276         (and (= (car fdate) (car date))
277              (> (nth 1 fdate) (nth 1 date))))))
278
279 ;;; Keymap macros.
280
281 (defmacro gnus-local-set-keys (&rest plist)
282   "Set the keys in PLIST in the current keymap."
283   `(gnus-define-keys-1 (current-local-map) ',plist))
284
285 (defmacro gnus-define-keys (keymap &rest plist)
286   "Define all keys in PLIST in KEYMAP."
287   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
288
289 (defmacro gnus-define-keys-safe (keymap &rest plist)
290   "Define all keys in PLIST in KEYMAP without overwriting previous definitions."
291   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist) t))
292
293 (put 'gnus-define-keys 'lisp-indent-function 1)
294 (put 'gnus-define-keys-safe 'lisp-indent-function 1)
295 (put 'gnus-local-set-keys 'lisp-indent-function 1)
296
297 (defmacro gnus-define-keymap (keymap &rest plist)
298   "Define all keys in PLIST in KEYMAP."
299   `(gnus-define-keys-1 ,keymap (quote ,plist)))
300
301 (put 'gnus-define-keymap 'lisp-indent-function 1)
302
303 (defun gnus-define-keys-1 (keymap plist &optional safe)
304   (when (null keymap)
305     (error "Can't set keys in a null keymap"))
306   (cond ((symbolp keymap)
307          (setq keymap (symbol-value keymap)))
308         ((keymapp keymap))
309         ((listp keymap)
310          (set (car keymap) nil)
311          (define-prefix-command (car keymap))
312          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
313          (setq keymap (symbol-value (car keymap)))))
314   (let (key)
315     (while plist
316       (when (symbolp (setq key (pop plist)))
317         (setq key (symbol-value key)))
318       (if (or (not safe)
319               (eq (lookup-key keymap key) 'undefined))
320           (define-key keymap key (pop plist))
321         (pop plist)))))
322
323 (defun gnus-completing-read (default prompt &rest args)
324   ;; Like `completing-read', except that DEFAULT is the default argument.
325   (let* ((prompt (if default
326                      (concat prompt " (default " default ") ")
327                    (concat prompt " ")))
328          (answer (apply 'completing-read prompt args)))
329     (if (or (null answer) (zerop (length answer)))
330         default
331       answer)))
332
333 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
334 ;; the echo area.
335 (defun gnus-y-or-n-p (prompt)
336   (prog1
337       (y-or-n-p prompt)
338     (message "")))
339
340 (defun gnus-yes-or-no-p (prompt)
341   (prog1
342       (yes-or-no-p prompt)
343     (message "")))
344
345 ;; By Frank Schmitt <ich@Frank-Schmitt.net>. Allows to have
346 ;; age-depending date representations. (e.g. just the time if it's
347 ;; from today, the day of the week if it's within the last 7 days and
348 ;; the full date if it's older)
349 (defun gnus-seconds-today ()
350   "Returns the number of seconds passed today"
351   (let ((now (decode-time (current-time))))
352     (+ (car now) (* (car (cdr now)) 60) (* (car (nthcdr 2 now)) 3600))))
353
354 (defun gnus-seconds-month ()
355   "Returns the number of seconds passed this month"
356   (let ((now (decode-time (current-time))))
357     (+ (car now) (* (car (cdr now)) 60) (* (car (nthcdr 2 now)) 3600)
358        (* (- (car (nthcdr 3 now)) 1) 3600 24))))
359
360 (defun gnus-seconds-year ()
361   "Returns the number of seconds passed this year"
362   (let ((now (decode-time (current-time)))
363         (days (format-time-string "%j" (current-time))))
364     (+ (car now) (* (car (cdr now)) 60) (* (car (nthcdr 2 now)) 3600)
365        (* (- (string-to-number days) 1) 3600 24))))
366
367 (defvar gnus-user-date-format-alist
368   '(((gnus-seconds-today) . "%k:%M")
369     (604800 . "%a %k:%M")                   ;;that's one week
370     ((gnus-seconds-month) . "%a %d")
371     ((gnus-seconds-year) . "%b %d")
372     (t . "%b %m '%y"))                      ;;this one is used when no other does match
373   "Alist of time in seconds and format specification used to display dates not older.
374 The first element must be a number or a function returning a
375 number. The second element is a format-specification as described in
376 the documentation for format-time-string.  The list must be ordered
377 smallest number up. When there is an element, which is not a number,
378 the corresponding format-specification will be used, disregarding any
379 following elements.  You can use the functions gnus-seconds-today,
380 gnus-seconds-month, gnus-seconds-year which will return the number of
381 seconds which passed today/this month/this year.")
382
383 (defun gnus-user-date (messy-date)
384   "Format the messy-date acording to gnus-user-date-format-alist.
385 Returns \"  ?  \" if there's bad input or if an other error occurs.
386 Input should look like this: \"Sun, 14 Oct 2001 13:34:39 +0200\"."
387   (condition-case ()
388       (let* ((messy-date (safe-date-to-time messy-date))
389              (now (current-time))
390              ;;If we don't find something suitable we'll use this one
391              (my-format "%b %m '%y")
392              (high (lsh (- (car now) (car messy-date)) 16)))
393         (if (and (> high -1) (= (logand high 65535) 0))
394             ;;overflow and bad input
395             (let* ((difference (+ high (- (car (cdr now))
396                                           (car (cdr messy-date)))))
397                    (templist gnus-user-date-format-alist)
398                    (top (eval (caar templist))))
399               (while (if (numberp top) (< top difference) (not top))
400                 (progn
401                   (setq templist (cdr templist))
402                   (setq top (eval (caar templist)))))
403               (if (stringp (cdr (car templist)))
404                   (setq my-format (cdr (car templist))))))
405         (format-time-string (eval my-format) messy-date))
406     (error "  ?   ")))
407 ;;end of Frank's code
408
409 (defun gnus-dd-mmm (messy-date)
410   "Return a string like DD-MMM from a big messy string."
411   (condition-case ()
412       (format-time-string "%d-%b" (safe-date-to-time messy-date))
413     (error "  -   ")))
414
415 (defmacro gnus-date-get-time (date)
416   "Convert DATE string to Emacs time.
417 Cache the result as a text property stored in DATE."
418   ;; Either return the cached value...
419   `(let ((d ,date))
420      (if (equal "" d)
421          '(0 0)
422        (or (get-text-property 0 'gnus-time d)
423            ;; or compute the value...
424            (let ((time (safe-date-to-time d)))
425              ;; and store it back in the string.
426              (put-text-property 0 1 'gnus-time time d)
427              time)))))
428
429 (defsubst gnus-time-iso8601 (time)
430   "Return a string of TIME in YYYYMMDDTHHMMSS format."
431   (format-time-string "%Y%m%dT%H%M%S" time))
432
433 (defun gnus-date-iso8601 (date)
434   "Convert the DATE to YYYYMMDDTHHMMSS."
435   (condition-case ()
436       (gnus-time-iso8601 (gnus-date-get-time date))
437     (error "")))
438
439 (defun gnus-mode-string-quote (string)
440   "Quote all \"%\"'s in STRING."
441   (gnus-replace-in-string string "%" "%%"))
442
443 ;; Make a hash table (default and minimum size is 256).
444 ;; Optional argument HASHSIZE specifies the table size.
445 (defun gnus-make-hashtable (&optional hashsize)
446   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 256) 256) 0))
447
448 ;; Make a number that is suitable for hashing; bigger than MIN and
449 ;; equal to some 2^x.  Many machines (such as sparcs) do not have a
450 ;; hardware modulo operation, so they implement it in software.  On
451 ;; many sparcs over 50% of the time to intern is spent in the modulo.
452 ;; Yes, it's slower than actually computing the hash from the string!
453 ;; So we use powers of 2 so people can optimize the modulo to a mask.
454 (defun gnus-create-hash-size (min)
455   (let ((i 1))
456     (while (< i min)
457       (setq i (* 2 i)))
458     i))
459
460 (defcustom gnus-verbose 7
461   "*Integer that says how verbose Gnus should be.
462 The higher the number, the more messages Gnus will flash to say what
463 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
464 display most important messages; and at ten, Gnus will keep on
465 jabbering all the time."
466   :group 'gnus-start
467   :type 'integer)
468
469 ;; Show message if message has a lower level than `gnus-verbose'.
470 ;; Guideline for numbers:
471 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
472 ;; for things that take a long time, 7 - not very important messages
473 ;; on stuff, 9 - messages inside loops.
474 (defun gnus-message (level &rest args)
475   (if (<= level gnus-verbose)
476       (apply 'message args)
477     ;; We have to do this format thingy here even if the result isn't
478     ;; shown - the return value has to be the same as the return value
479     ;; from `message'.
480     (apply 'format args)))
481
482 (defun gnus-error (level &rest args)
483   "Beep an error if LEVEL is equal to or less than `gnus-verbose'."
484   (when (<= (floor level) gnus-verbose)
485     (apply 'message args)
486     (ding)
487     (let (duration)
488       (when (and (floatp level)
489                  (not (zerop (setq duration (* 10 (- level (floor level)))))))
490         (sit-for duration))))
491   nil)
492
493 (defun gnus-split-references (references)
494   "Return a list of Message-IDs in REFERENCES."
495   (let ((beg 0)
496         ids)
497     (while (string-match "<[^> \t]+>" references beg)
498       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
499             ids))
500     (nreverse ids)))
501
502 (defsubst gnus-parent-id (references &optional n)
503   "Return the last Message-ID in REFERENCES.
504 If N, return the Nth ancestor instead."
505   (when references
506     (let ((ids (inline (gnus-split-references references))))
507       (while (nthcdr (or n 1) ids)
508         (setq ids (cdr ids)))
509       (car ids))))
510
511 (defun gnus-buffer-live-p (buffer)
512   "Say whether BUFFER is alive or not."
513   (and buffer
514        (get-buffer buffer)
515        (buffer-name (get-buffer buffer))))
516
517 (defun gnus-horizontal-recenter ()
518   "Recenter the current buffer horizontally."
519   (if (< (current-column) (/ (window-width) 2))
520       (set-window-hscroll (gnus-get-buffer-window (current-buffer) t) 0)
521     (let* ((orig (point))
522            (end (window-end (gnus-get-buffer-window (current-buffer) t)))
523            (max 0))
524       (when end
525         ;; Find the longest line currently displayed in the window.
526         (goto-char (window-start))
527         (while (and (not (eobp))
528                     (< (point) end))
529           (end-of-line)
530           (setq max (max max (current-column)))
531           (forward-line 1))
532         (goto-char orig)
533         ;; Scroll horizontally to center (sort of) the point.
534         (if (> max (window-width))
535             (set-window-hscroll
536              (gnus-get-buffer-window (current-buffer) t)
537              (min (- (current-column) (/ (window-width) 3))
538                   (+ 2 (- max (window-width)))))
539           (set-window-hscroll (gnus-get-buffer-window (current-buffer) t) 0))
540         max))))
541
542 (defun gnus-read-event-char ()
543   "Get the next event."
544   (let ((event (read-event)))
545     ;; should be gnus-characterp, but this can't be called in XEmacs anyway
546     (cons (and (numberp event) event) event)))
547
548 (defun gnus-sortable-date (date)
549   "Make string suitable for sorting from DATE."
550   (gnus-time-iso8601 (date-to-time date)))
551
552 (defun gnus-copy-file (file &optional to)
553   "Copy FILE to TO."
554   (interactive
555    (list (read-file-name "Copy file: " default-directory)
556          (read-file-name "Copy file to: " default-directory)))
557   (unless to
558     (setq to (read-file-name "Copy file to: " default-directory)))
559   (when (file-directory-p to)
560     (setq to (concat (file-name-as-directory to)
561                      (file-name-nondirectory file))))
562   (copy-file file to))
563
564 (defvar gnus-work-buffer " *gnus work*")
565
566 (defun gnus-set-work-buffer ()
567   "Put point in the empty Gnus work buffer."
568   (if (get-buffer gnus-work-buffer)
569       (progn
570         (set-buffer gnus-work-buffer)
571         (erase-buffer))
572     (set-buffer (gnus-get-buffer-create gnus-work-buffer))
573     (kill-all-local-variables)))
574
575 (defmacro gnus-group-real-name (group)
576   "Find the real name of a foreign newsgroup."
577   `(let ((gname ,group))
578      (if (string-match "^[^:]+:" gname)
579          (substring gname (match-end 0))
580        gname)))
581
582 (defun gnus-make-sort-function (funs)
583   "Return a composite sort condition based on the functions in FUNC."
584   (cond
585    ;; Just a simple function.
586    ((gnus-functionp funs) funs)
587    ;; No functions at all.
588    ((null funs) funs)
589    ;; A list of functions.
590    ((or (cdr funs)
591         (listp (car funs)))
592     (gnus-byte-compile
593      `(lambda (t1 t2)
594         ,(gnus-make-sort-function-1 (reverse funs)))))
595    ;; A list containing just one function.
596    (t
597     (car funs))))
598
599 (defun gnus-make-sort-function-1 (funs)
600   "Return a composite sort condition based on the functions in FUNC."
601   (let ((function (car funs))
602         (first 't1)
603         (last 't2))
604     (when (consp function)
605       (cond
606        ;; Reversed spec.
607        ((eq (car function) 'not)
608         (setq function (cadr function)
609               first 't2
610               last 't1))
611        ((gnus-functionp function)
612         ;; Do nothing.
613         )
614        (t
615         (error "Invalid sort spec: %s" function))))
616     (if (cdr funs)
617         `(or (,function ,first ,last)
618              (and (not (,function ,last ,first))
619                   ,(gnus-make-sort-function-1 (cdr funs))))
620       `(,function ,first ,last))))
621
622 (defun gnus-turn-off-edit-menu (type)
623   "Turn off edit menu in `gnus-TYPE-mode-map'."
624   (define-key (symbol-value (intern (format "gnus-%s-mode-map" type)))
625     [menu-bar edit] 'undefined))
626
627 (defun gnus-prin1 (form)
628   "Use `prin1' on FORM in the current buffer.
629 Bind `print-quoted' and `print-readably' to t while printing."
630   (let ((print-quoted t)
631         (print-readably t)
632         (print-escape-multibyte nil)
633         print-level print-length)
634     (prin1 form (current-buffer))))
635
636 (defun gnus-prin1-to-string (form)
637   "The same as `prin1', but bind `print-quoted' and `print-readably' to t."
638   (let ((print-quoted t)
639         (print-readably t))
640     (prin1-to-string form)))
641
642 (defun gnus-make-directory (directory)
643   "Make DIRECTORY (and all its parents) if it doesn't exist."
644   (require 'nnmail)
645   (let ((file-name-coding-system nnmail-pathname-coding-system)
646         (pathname-coding-system nnmail-pathname-coding-system))
647     (when (and directory
648                (not (file-exists-p directory)))
649       (make-directory directory t)))
650   t)
651
652 (defun gnus-write-buffer (file)
653   "Write the current buffer's contents to FILE."
654   ;; Make sure the directory exists.
655   (gnus-make-directory (file-name-directory file))
656   (let ((file-name-coding-system nnmail-pathname-coding-system)
657         (pathname-coding-system nnmail-pathname-coding-system))
658     ;; Write the buffer.
659     (write-region (point-min) (point-max) file nil 'quietly)))
660
661 (defun gnus-write-buffer-as-binary (file)
662   "Write the current buffer's contents to FILE without code conversion."
663   ;; Make sure the directory exists.
664   (gnus-make-directory (file-name-directory file))
665   ;; Write the buffer.
666   (write-region-as-binary (point-min) (point-max) file nil 'quietly))
667
668 (defun gnus-write-buffer-as-coding-system (coding-system file)
669   "Write the current buffer's contents to FILE with code conversion."
670   ;; Make sure the directory exists.
671   (gnus-make-directory (file-name-directory file))
672   ;; Write the buffer.
673   (write-region-as-coding-system
674    coding-system (point-min) (point-max) file nil 'quietly))
675
676 (defun gnus-delete-file (file)
677   "Delete FILE if it exists."
678   (when (file-exists-p file)
679     (delete-file file)))
680
681 (defun gnus-strip-whitespace (string)
682   "Return STRING stripped of all whitespace."
683   (while (string-match "[\r\n\t ]+" string)
684     (setq string (replace-match "" t t string)))
685   string)
686
687 (defsubst gnus-put-text-property-excluding-newlines (beg end prop val)
688   "The same as `put-text-property', but don't put this prop on any newlines in the region."
689   (save-match-data
690     (save-excursion
691       (save-restriction
692         (goto-char beg)
693         (while (re-search-forward gnus-emphasize-whitespace-regexp end 'move)
694           (gnus-put-text-property beg (match-beginning 0) prop val)
695           (setq beg (point)))
696         (gnus-put-text-property beg (point) prop val)))))
697
698 (defun gnus-put-text-property-excluding-characters-with-faces (beg end
699                                                                    prop val)
700   "The same as `put-text-property', but don't put props on characters with the `gnus-face' property."
701   (let ((b beg))
702     (while (/= b end)
703       (when (get-text-property b 'gnus-face)
704         (setq b (next-single-property-change b 'gnus-face nil end)))
705       (when (/= b end)
706         (gnus-put-text-property
707          b (setq b (next-single-property-change b 'gnus-face nil end))
708          prop val)))))
709
710 ;;; Protected and atomic operations.  dmoore@ucsd.edu 21.11.1996
711 ;;; The primary idea here is to try to protect internal datastructures
712 ;;; from becoming corrupted when the user hits C-g, or if a hook or
713 ;;; similar blows up.  Often in Gnus multiple tables/lists need to be
714 ;;; updated at the same time, or information can be lost.
715
716 (defvar gnus-atomic-be-safe t
717   "If t, certain operations will be protected from interruption by C-g.")
718
719 (defmacro gnus-atomic-progn (&rest forms)
720   "Evaluate FORMS atomically, which means to protect the evaluation
721 from being interrupted by the user.  An error from the forms themselves
722 will return without finishing the operation.  Since interrupts from
723 the user are disabled, it is recommended that only the most minimal
724 operations are performed by FORMS.  If you wish to assign many
725 complicated values atomically, compute the results into temporary
726 variables and then do only the assignment atomically."
727   `(let ((inhibit-quit gnus-atomic-be-safe))
728      ,@forms))
729
730 (put 'gnus-atomic-progn 'lisp-indent-function 0)
731
732 (defmacro gnus-atomic-progn-assign (protect &rest forms)
733   "Evaluate FORMS, but insure that the variables listed in PROTECT
734 are not changed if anything in FORMS signals an error or otherwise
735 non-locally exits.  The variables listed in PROTECT are updated atomically.
736 It is safe to use gnus-atomic-progn-assign with long computations.
737
738 Note that if any of the symbols in PROTECT were unbound, they will be
739 set to nil on a sucessful assignment.  In case of an error or other
740 non-local exit, it will still be unbound."
741   (let* ((temp-sym-map (mapcar (lambda (x) (list (make-symbol
742                                                   (concat (symbol-name x)
743                                                           "-tmp"))
744                                                  x))
745                                protect))
746          (sym-temp-map (mapcar (lambda (x) (list (cadr x) (car x)))
747                                temp-sym-map))
748          (temp-sym-let (mapcar (lambda (x) (list (car x)
749                                                  `(and (boundp ',(cadr x))
750                                                        ,(cadr x))))
751                                temp-sym-map))
752          (sym-temp-let sym-temp-map)
753          (temp-sym-assign (apply 'append temp-sym-map))
754          (sym-temp-assign (apply 'append sym-temp-map))
755          (result (make-symbol "result-tmp")))
756     `(let (,@temp-sym-let
757            ,result)
758        (let ,sym-temp-let
759          (setq ,result (progn ,@forms))
760          (setq ,@temp-sym-assign))
761        (let ((inhibit-quit gnus-atomic-be-safe))
762          (setq ,@sym-temp-assign))
763        ,result)))
764
765 (put 'gnus-atomic-progn-assign 'lisp-indent-function 1)
766 ;(put 'gnus-atomic-progn-assign 'edebug-form-spec '(sexp body))
767
768 (defmacro gnus-atomic-setq (&rest pairs)
769   "Similar to setq, except that the real symbols are only assigned when
770 there are no errors.  And when the real symbols are assigned, they are
771 done so atomically.  If other variables might be changed via side-effect,
772 see gnus-atomic-progn-assign.  It is safe to use gnus-atomic-setq
773 with potentially long computations."
774   (let ((tpairs pairs)
775         syms)
776     (while tpairs
777       (push (car tpairs) syms)
778       (setq tpairs (cddr tpairs)))
779     `(gnus-atomic-progn-assign ,syms
780        (setq ,@pairs))))
781
782 ;(put 'gnus-atomic-setq 'edebug-form-spec '(body))
783
784
785 ;;; Functions for saving to babyl/mail files.
786
787 (defvar rmail-default-rmail-file)
788 (defun gnus-output-to-rmail (filename &optional ask)
789   "Append the current article to an Rmail file named FILENAME."
790   (require 'rmail)
791   ;; Most of these codes are borrowed from rmailout.el.
792   (setq filename (expand-file-name filename))
793   (setq rmail-default-rmail-file filename)
794   (let ((artbuf (current-buffer))
795         (tmpbuf (get-buffer-create " *Gnus-output*")))
796     (save-excursion
797       (or (get-file-buffer filename)
798           (file-exists-p filename)
799           (if (or (not ask)
800                   (gnus-yes-or-no-p
801                    (concat "\"" filename "\" does not exist, create it? ")))
802               (let ((file-buffer (create-file-buffer filename)))
803                 (save-excursion
804                   (set-buffer file-buffer)
805                   (rmail-insert-rmail-file-header)
806                   (let ((require-final-newline nil))
807                     (gnus-write-buffer-as-coding-system
808                      nnheader-text-coding-system filename)))
809                 (kill-buffer file-buffer))
810             (error "Output file does not exist")))
811       (set-buffer tmpbuf)
812       (erase-buffer)
813       (insert-buffer-substring artbuf)
814       (gnus-convert-article-to-rmail)
815       ;; Decide whether to append to a file or to an Emacs buffer.
816       (let ((outbuf (get-file-buffer filename)))
817         (if (not outbuf)
818             (let ((file-name-coding-system nnmail-pathname-coding-system)
819                   (pathname-coding-system nnmail-pathname-coding-system))
820               (write-region-as-binary (point-min) (point-max)
821                                       filename 'append))
822           ;; File has been visited, in buffer OUTBUF.
823           (set-buffer outbuf)
824           (let ((buffer-read-only nil)
825                 (msg (and (boundp 'rmail-current-message)
826                           (symbol-value 'rmail-current-message))))
827             ;; If MSG is non-nil, buffer is in RMAIL mode.
828             (when msg
829               (widen)
830               (narrow-to-region (point-max) (point-max)))
831             (insert-buffer-substring tmpbuf)
832             (when msg
833               (goto-char (point-min))
834               (widen)
835               (search-backward "\n\^_")
836               (narrow-to-region (point) (point-max))
837               (rmail-count-new-messages t)
838               (when (rmail-summary-exists)
839                 (rmail-select-summary
840                  (rmail-update-summary)))
841               (rmail-count-new-messages t)
842               (rmail-show-message msg))
843             (save-buffer)))))
844     (kill-buffer tmpbuf)))
845
846 (defun gnus-output-to-mail (filename &optional ask)
847   "Append the current article to a mail file named FILENAME."
848   (setq filename (expand-file-name filename))
849   (let ((artbuf (current-buffer))
850         (tmpbuf (get-buffer-create " *Gnus-output*")))
851     (save-excursion
852       ;; Create the file, if it doesn't exist.
853       (when (and (not (get-file-buffer filename))
854                  (not (file-exists-p filename)))
855         (if (or (not ask)
856                 (gnus-y-or-n-p
857                  (concat "\"" filename "\" does not exist, create it? ")))
858             (let ((file-buffer (create-file-buffer filename)))
859               (save-excursion
860                 (set-buffer file-buffer)
861                 (let ((require-final-newline nil))
862                   (gnus-write-buffer-as-coding-system
863                    nnheader-text-coding-system filename)))
864               (kill-buffer file-buffer))
865           (error "Output file does not exist")))
866       (set-buffer tmpbuf)
867       (erase-buffer)
868       (insert-buffer-substring artbuf)
869       (goto-char (point-min))
870       (if (looking-at "From ")
871           (forward-line 1)
872         (insert "From nobody " (current-time-string) "\n"))
873       (let (case-fold-search)
874         (while (re-search-forward "^From " nil t)
875           (beginning-of-line)
876           (insert ">")))
877       ;; Decide whether to append to a file or to an Emacs buffer.
878       (let ((outbuf (get-file-buffer filename)))
879         (if (not outbuf)
880             (let ((buffer-read-only nil))
881               (save-excursion
882                 (goto-char (point-max))
883                 (forward-char -2)
884                 (unless (looking-at "\n\n")
885                   (goto-char (point-max))
886                   (unless (bolp)
887                     (insert "\n"))
888                   (insert "\n"))
889                 (goto-char (point-max))
890                 (let ((file-name-coding-system nnmail-pathname-coding-system)
891                       (pathname-coding-system nnmail-pathname-coding-system))
892                   (write-region-as-binary (point-min) (point-max)
893                                           filename 'append))))
894           ;; File has been visited, in buffer OUTBUF.
895           (set-buffer outbuf)
896           (let ((buffer-read-only nil))
897             (goto-char (point-max))
898             (unless (eobp)
899               (insert "\n"))
900             (insert "\n")
901             (insert-buffer-substring tmpbuf)))))
902     (kill-buffer tmpbuf)))
903
904 (defun gnus-convert-article-to-rmail ()
905   "Convert article in current buffer to Rmail message format."
906   (let ((buffer-read-only nil))
907     ;; Convert article directly into Babyl format.
908     (goto-char (point-min))
909     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
910     (while (search-forward "\n\^_" nil t) ;single char
911       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
912     (goto-char (point-max))
913     (insert "\^_")))
914
915 (defun gnus-map-function (funs arg)
916   "Applies the result of the first function in FUNS to the second, and so on.
917 ARG is passed to the first function."
918   (let ((myfuns funs))
919     (while myfuns
920       (setq arg (funcall (pop myfuns) arg)))
921     arg))
922
923 (defun gnus-run-hooks (&rest funcs)
924   "Does the same as `run-hooks', but saves excursion."
925   (let ((buf (current-buffer)))
926     (unwind-protect
927         (apply 'run-hooks funcs)
928       (set-buffer buf))))
929
930 ;;;
931 ;;; .netrc and .authinforc parsing
932 ;;;
933
934 (defun gnus-parse-netrc (file)
935   "Parse FILE and return an list of all entries in the file."
936   (when (file-exists-p file)
937     (with-temp-buffer
938       (let ((tokens '("machine" "default" "login"
939                       "password" "account" "macdef" "force"
940                       "port"))
941             alist elem result pair)
942         (insert-file-contents file)
943         (goto-char (point-min))
944         ;; Go through the file, line by line.
945         (while (not (eobp))
946           (narrow-to-region (point) (gnus-point-at-eol))
947           ;; For each line, get the tokens and values.
948           (while (not (eobp))
949             (skip-chars-forward "\t ")
950             ;; Skip lines that begin with a "#".
951             (if (eq (char-after) ?#)
952                 (goto-char (point-max))
953               (unless (eobp)
954                 (setq elem
955                       (if (= (following-char) ?\")
956                           (read (current-buffer))
957                         (buffer-substring
958                          (point) (progn (skip-chars-forward "^\t ")
959                                         (point)))))
960                 (cond
961                  ((equal elem "macdef")
962                   ;; We skip past the macro definition.
963                   (widen)
964                   (while (and (zerop (forward-line 1))
965                               (looking-at "$")))
966                   (narrow-to-region (point) (point)))
967                  ((member elem tokens)
968                   ;; Tokens that don't have a following value are ignored,
969                   ;; except "default".
970                   (when (and pair (or (cdr pair)
971                                       (equal (car pair) "default")))
972                     (push pair alist))
973                   (setq pair (list elem)))
974                  (t
975                   ;; Values that haven't got a preceding token are ignored.
976                   (when pair
977                     (setcdr pair elem)
978                     (push pair alist)
979                     (setq pair nil)))))))
980           (when alist
981             (push (nreverse alist) result))
982           (setq alist nil
983                 pair nil)
984           (widen)
985           (forward-line 1))
986         (nreverse result)))))
987
988 (defun gnus-netrc-machine (list machine &optional port defaultport)
989   "Return the netrc values from LIST for MACHINE or for the default entry.
990 If PORT specified, only return entries with matching port tokens.
991 Entries without port tokens default to DEFAULTPORT."
992   (let ((rest list)
993         result)
994     (while list
995       (when (equal (cdr (assoc "machine" (car list))) machine)
996         (push (car list) result))
997       (pop list))
998     (unless result
999       ;; No machine name matches, so we look for default entries.
1000       (while rest
1001         (when (assoc "default" (car rest))
1002           (push (car rest) result))
1003         (pop rest)))
1004     (when result
1005       (setq result (nreverse result))
1006       (while (and result
1007                   (not (equal (or port defaultport "nntp")
1008                               (or (gnus-netrc-get (car result) "port")
1009                                   defaultport "nntp"))))
1010         (pop result))
1011       (car result))))
1012
1013 (defun gnus-netrc-get (alist type)
1014   "Return the value of token TYPE from ALIST."
1015   (cdr (assoc type alist)))
1016
1017 ;;; Various
1018
1019 (defvar gnus-group-buffer)              ; Compiler directive
1020 (defun gnus-alive-p ()
1021   "Say whether Gnus is running or not."
1022   (and (boundp 'gnus-group-buffer)
1023        (get-buffer gnus-group-buffer)
1024        (save-excursion
1025          (set-buffer gnus-group-buffer)
1026          (eq major-mode 'gnus-group-mode))))
1027
1028 (defun gnus-remove-duplicates (list)
1029   (let (new (tail list))
1030     (while tail
1031       (or (member (car tail) new)
1032           (setq new (cons (car tail) new)))
1033       (setq tail (cdr tail)))
1034     (nreverse new)))
1035
1036 (defun gnus-delete-if (predicate list)
1037   "Delete elements from LIST that satisfy PREDICATE."
1038   (let (out)
1039     (while list
1040       (unless (funcall predicate (car list))
1041         (push (car list) out))
1042       (pop list))
1043     (nreverse out)))
1044
1045 (if (fboundp 'assq-delete-all)
1046     (defalias 'gnus-delete-alist 'assq-delete-all)
1047   (defun gnus-delete-alist (key alist)
1048     "Delete from ALIST all elements whose car is KEY.
1049 Return the modified alist."
1050     (let (entry)
1051       (while (setq entry (assq key alist))
1052         (setq alist (delq entry alist)))
1053       alist)))
1054
1055 (defmacro gnus-pull (key alist &optional assoc-p)
1056   "Modify ALIST to be without KEY."
1057   (unless (symbolp alist)
1058     (error "Not a symbol: %s" alist))
1059   (let ((fun (if assoc-p 'assoc 'assq)))
1060     `(setq ,alist (delq (,fun ,key ,alist) ,alist))))
1061
1062 (defun gnus-globalify-regexp (re)
1063   "Returns a regexp that matches a whole line, iff RE matches a part of it."
1064   (concat (unless (string-match "^\\^" re) "^.*")
1065           re
1066           (unless (string-match "\\$$" re) ".*$")))
1067
1068 (defun gnus-set-window-start (&optional point)
1069   "Set the window start to POINT, or (point) if nil."
1070   (let ((win (gnus-get-buffer-window (current-buffer) t)))
1071     (when win
1072       (set-window-start win (or point (point))))))
1073
1074 (defun gnus-annotation-in-region-p (b e)
1075   (if (= b e)
1076       (eq (cadr (memq 'gnus-undeletable (text-properties-at b))) t)
1077     (text-property-any b e 'gnus-undeletable t)))
1078
1079 (defun gnus-or (&rest elems)
1080   "Return non-nil if any of the elements are non-nil."
1081   (catch 'found
1082     (while elems
1083       (when (pop elems)
1084         (throw 'found t)))))
1085
1086 (defun gnus-and (&rest elems)
1087   "Return non-nil if all of the elements are non-nil."
1088   (catch 'found
1089     (while elems
1090       (unless (pop elems)
1091         (throw 'found nil)))
1092     t))
1093
1094 (defun gnus-write-active-file (file hashtb &optional full-names)
1095   (let ((output-coding-system nnmail-active-file-coding-system)
1096         (coding-system-for-write nnmail-active-file-coding-system))
1097     (with-temp-file file
1098       (mapatoms
1099        (lambda (sym)
1100          (when (and sym
1101                     (boundp sym)
1102                     (symbol-value sym))
1103            (insert (format "%S %d %d y\n"
1104                            (if full-names
1105                                sym
1106                              (intern (gnus-group-real-name (symbol-name sym))))
1107                            (or (cdr (symbol-value sym))
1108                                (car (symbol-value sym)))
1109                            (car (symbol-value sym))))))
1110        hashtb)
1111       (goto-char (point-max))
1112       (while (search-backward "\\." nil t)
1113         (delete-char 1)))))
1114
1115 (if (fboundp 'union)
1116     (defalias 'gnus-union 'union)
1117   (defun gnus-union (l1 l2)
1118     "Set union of lists L1 and L2."
1119     (cond ((null l1) l2)
1120           ((null l2) l1)
1121           ((equal l1 l2) l1)
1122           (t
1123            (or (>= (length l1) (length l2))
1124                (setq l1 (prog1 l2 (setq l2 l1))))
1125            (while l2
1126              (or (member (car l2) l1)
1127                  (push (car l2) l1))
1128              (pop l2))
1129            l1))))
1130
1131 (defun gnus-add-text-properties-when
1132   (property value start end properties &optional object)
1133   "Like `gnus-add-text-properties', only applied on where PROPERTY is VALUE."
1134   (let (point)
1135     (while (and start
1136                 (< start end) ;; XEmacs will loop for every when start=end.
1137                 (setq point (text-property-not-all start end property value)))
1138       (gnus-add-text-properties start point properties object)
1139       (setq start (text-property-any point end property value)))
1140     (if start
1141         (gnus-add-text-properties start end properties object))))
1142
1143 (defun gnus-remove-text-properties-when
1144   (property value start end properties &optional object)
1145   "Like `remove-text-properties', only applied on where PROPERTY is VALUE."
1146   (let (point)
1147     (while (and start
1148                 (< start end)
1149                 (setq point (text-property-not-all start end property value)))
1150       (remove-text-properties start point properties object)
1151       (setq start (text-property-any point end property value)))
1152     (if start
1153         (remove-text-properties start end properties object))
1154     t))
1155
1156 (defun gnus-string-equal (x y)
1157   "Like `string-equal', except it compares case-insensitively."
1158   (and (= (length x) (length y))
1159        (or (string-equal x y)
1160            (string-equal (downcase x) (downcase y)))))
1161
1162 (defcustom gnus-use-byte-compile t
1163   "If non-nil, byte-compile crucial run-time codes."
1164   :type 'boolean
1165   :version "21.1"
1166   :group 'gnus-various)
1167
1168 (defun gnus-byte-compile (form)
1169   "Byte-compile FORM if `gnus-use-byte-compile' is non-nil."
1170   (if gnus-use-byte-compile
1171       (progn
1172         (require 'bytecomp)
1173         (defalias 'gnus-byte-compile 'byte-compile)
1174         (byte-compile form))
1175     form))
1176
1177 (defun gnus-remassoc (key alist)
1178   "Delete by side effect any elements of LIST whose car is `equal' to KEY.
1179 The modified LIST is returned.  If the first member
1180 of LIST has a car that is `equal' to KEY, there is no way to remove it
1181 by side effect; therefore, write `(setq foo (remassoc key foo))' to be
1182 sure of changing the value of `foo'."
1183   (when alist
1184     (if (equal key (caar alist))
1185         (cdr alist)
1186       (setcdr alist (gnus-remassoc key (cdr alist)))
1187       alist)))
1188
1189 (defun gnus-update-alist-soft (key value alist)
1190   (if value
1191       (cons (cons key value) (gnus-remassoc key alist))
1192     (gnus-remassoc key alist)))
1193
1194 (defun gnus-create-info-command (node)
1195   "Create a command that will go to info NODE."
1196   `(lambda ()
1197      (interactive)
1198      ,(concat "Enter the info system at node " node)
1199      (Info-goto-node ,node)
1200      (setq gnus-info-buffer (current-buffer))
1201      (gnus-configure-windows 'info)))
1202
1203 (defun gnus-not-ignore (&rest args)
1204   t)
1205
1206 (defvar gnus-directory-sep-char-regexp "/"
1207   "The regexp of directory separator character.
1208 If you find some problem with the directory separator character, try
1209 \"[/\\\\\]\" for some systems.")
1210
1211 (defun gnus-url-unhex (x)
1212   (if (> x ?9)
1213       (if (>= x ?a)
1214           (+ 10 (- x ?a))
1215         (+ 10 (- x ?A)))
1216     (- x ?0)))
1217
1218 (defun gnus-url-unhex-string (str &optional allow-newlines)
1219   "Remove %XXX embedded spaces, etc in a url.
1220 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
1221 decoding of carriage returns and line feeds in the string, which is normally
1222 forbidden in URL encoding."
1223   (setq str (or (mm-subst-char-in-string ?+ ?  str) ""))
1224   (let ((tmp "")
1225         (case-fold-search t))
1226     (while (string-match "%[0-9a-f][0-9a-f]" str)
1227       (let* ((start (match-beginning 0))
1228              (ch1 (gnus-url-unhex (elt str (+ start 1))))
1229              (code (+ (* 16 ch1)
1230                       (gnus-url-unhex (elt str (+ start 2))))))
1231         (setq tmp (concat
1232                    tmp (substring str 0 start)
1233                    (cond
1234                     (allow-newlines
1235                      (char-to-string code))
1236                     ((or (= code ?\n) (= code ?\r))
1237                      " ")
1238                     (t (char-to-string code))))
1239               str (substring str (match-end 0)))))
1240     (setq tmp (concat tmp str))
1241     tmp))
1242
1243 (provide 'gnus-util)
1244
1245 ;;; gnus-util.el ends here