1 ;;; gnus-util.el --- utility functions for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Tatsuya Ichikawa <t-ichi@po.shiojiri.ne.jp>
7 ;; Keywords: mail, news, MIME
9 ;; This file is part of GNU Emacs.
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)
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.
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.
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
37 ;; Fixme: this should be a gnus variable, not nnmail-.
38 (defvar nnmail-pathname-coding-system))
39 (eval-when-compile (require 'static))
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"))
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)))
60 (defun gnus-replace-in-string (string regexp newtext &optional literal)
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))))
68 (defun gnus-boundp (variable)
69 "Return non-nil if VARIABLE is bound and non-nil."
70 (and (boundp variable)
71 (symbol-value variable)))
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"))
77 (buf (make-symbol "buf"))
78 (frame (make-symbol "frame")))
79 `(let* ((,tempvar (selected-window))
81 (,w (gnus-get-buffer-window ,buf 'visible))
88 (set-buffer (window-buffer ,w)))
91 (setq ,frame (selected-frame))
92 (select-window ,tempvar)
93 (select-frame ,frame)))))
95 (put 'gnus-eval-in-buffer-window 'lisp-indent-function 1)
96 (put 'gnus-eval-in-buffer-window 'edebug-form-spec '(form body))
98 (defmacro gnus-intern-safe (string hashtable)
99 "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
100 `(let ((symbol (intern ,string ,hashtable)))
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)
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)))
119 (defsubst gnus-goto-char (point)
120 (and point (goto-char point)))
122 (defmacro gnus-buffer-exists-p (buffer)
123 `(let ((buffer ,buffer))
125 (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
128 (defmacro gnus-kill-buffer (buffer)
129 `(let ((buf ,buffer))
130 (when (gnus-buffer-exists-p buf)
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))
139 (defun gnus-point-at-bol ()
140 "Return point at the beginning of the line."
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))
153 (defun gnus-point-at-eol ()
154 "Return point at the end of the line."
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)
167 (while (and (cdr list)
168 (not (equal (cadr list) elt)))
169 (setq list (cdr list)))
171 (setcdr list (cddr list)))
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))))
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)
186 (cons 'progn (cddr fval)))))
188 (defun gnus-extract-address-components (from)
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.
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.
205 (and (string-match "(.+)" from)
206 (setq name (substring from (1+ (match-beginning 0))
207 (1- (match-end 0)))))
208 (and (string-match "()" from)
210 ;; XOVER might not support folded From headers.
211 (and (string-match "(.*" from)
212 (setq name (substring from (1+ (match-beginning 0))
214 (list (if (string= name "") nil name) (or address from))))
217 (defun gnus-fetch-field (field)
218 "Return the value of the header FIELD of current article."
221 (let ((case-fold-search t)
222 (inhibit-point-motion-hooks t))
223 (nnheader-narrow-to-headers)
224 (message-fetch-field field)))))
226 (defun gnus-goto-colon ()
228 (let ((eol (gnus-point-at-eol)))
229 (goto-char (or (text-property-any (point) eol 'gnus-position t)
230 (search-forward ":" eol t)
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
238 (message-tokenize-header newsgroups)
241 (defun gnus-remove-text-with-property (prop)
242 "Delete all text in the current buffer with text property PROP."
244 (goto-char (point-min))
246 (while (get-text-property (point) prop)
248 (goto-char (next-single-property-change (point) prop nil (point-max))))))
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)))
256 (if idx (substring newsgroup 0 idx))
258 (nnheader-replace-chars-in-string
259 (if idx (substring newsgroup (1+ idx)) newsgroup)
262 (defun gnus-newsgroup-savable-name (group)
263 ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
265 (nnheader-replace-chars-in-string group ?/ ?.))
267 (defun gnus-string> (s1 s2)
268 (not (or (string< s1 s2)
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))))))
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))
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)))
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))
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)
297 (defmacro gnus-define-keymap (keymap &rest plist)
298 "Define all keys in PLIST in KEYMAP."
299 `(gnus-define-keys-1 ,keymap (quote ,plist)))
301 (put 'gnus-define-keymap 'lisp-indent-function 1)
303 (defun gnus-define-keys-1 (keymap plist &optional safe)
305 (error "Can't set keys in a null keymap"))
306 (cond ((symbolp keymap)
307 (setq keymap (symbol-value 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)))))
316 (when (symbolp (setq key (pop plist)))
317 (setq key (symbol-value key)))
319 (eq (lookup-key keymap key) 'undefined))
320 (define-key keymap key (pop plist))
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)))
333 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
335 (defun gnus-y-or-n-p (prompt)
340 (defun gnus-yes-or-no-p (prompt)
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))))
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))))
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))))
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.")
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\"."
388 (let* ((messy-date (safe-date-to-time messy-date))
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))
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))
407 ;;end of Frank's code
409 (defun gnus-dd-mmm (messy-date)
410 "Return a string like DD-MMM from a big messy string."
412 (format-time-string "%d-%b" (safe-date-to-time messy-date))
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...
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)
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))
433 (defun gnus-date-iso8601 (date)
434 "Convert the DATE to YYYYMMDDTHHMMSS."
436 (gnus-time-iso8601 (gnus-date-get-time date))
439 (defun gnus-mode-string-quote (string)
440 "Quote all \"%\"'s in STRING."
441 (gnus-replace-in-string string "%" "%%"))
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))
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)
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."
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
480 (apply 'format args)))
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)
488 (when (and (floatp level)
489 (not (zerop (setq duration (* 10 (- level (floor level)))))))
490 (sit-for duration))))
493 (defun gnus-split-references (references)
494 "Return a list of Message-IDs in REFERENCES."
497 (while (string-match "<[^> \t]+>" references beg)
498 (push (substring references (match-beginning 0) (setq beg (match-end 0)))
502 (defsubst gnus-parent-id (references &optional n)
503 "Return the last Message-ID in REFERENCES.
504 If N, return the Nth ancestor instead."
506 (let ((ids (inline (gnus-split-references references))))
507 (while (nthcdr (or n 1) ids)
508 (setq ids (cdr ids)))
511 (defun gnus-buffer-live-p (buffer)
512 "Say whether BUFFER is alive or not."
515 (buffer-name (get-buffer buffer))))
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)))
525 ;; Find the longest line currently displayed in the window.
526 (goto-char (window-start))
527 (while (and (not (eobp))
530 (setq max (max max (current-column)))
533 ;; Scroll horizontally to center (sort of) the point.
534 (if (> max (window-width))
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))
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)))
548 (defun gnus-sortable-date (date)
549 "Make string suitable for sorting from DATE."
550 (gnus-time-iso8601 (date-to-time date)))
552 (defun gnus-copy-file (file &optional to)
555 (list (read-file-name "Copy file: " default-directory)
556 (read-file-name "Copy file to: " default-directory)))
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))))
564 (defvar gnus-work-buffer " *gnus work*")
566 (defun gnus-set-work-buffer ()
567 "Put point in the empty Gnus work buffer."
568 (if (get-buffer gnus-work-buffer)
570 (set-buffer gnus-work-buffer)
572 (set-buffer (gnus-get-buffer-create gnus-work-buffer))
573 (kill-all-local-variables)))
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))
582 (defun gnus-make-sort-function (funs)
583 "Return a composite sort condition based on the functions in FUNC."
585 ;; Just a simple function.
586 ((gnus-functionp funs) funs)
587 ;; No functions at all.
589 ;; A list of functions.
594 ,(gnus-make-sort-function-1 (reverse funs)))))
595 ;; A list containing just one function.
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))
604 (when (consp function)
607 ((eq (car function) 'not)
608 (setq function (cadr function)
611 ((gnus-functionp function)
615 (error "Invalid sort spec: %s" function))))
617 `(or (,function ,first ,last)
618 (and (not (,function ,last ,first))
619 ,(gnus-make-sort-function-1 (cdr funs))))
620 `(,function ,first ,last))))
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))
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)
632 (print-escape-multibyte nil)
633 print-level print-length)
634 (prin1 form (current-buffer))))
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)
640 (prin1-to-string form)))
642 (defun gnus-make-directory (directory)
643 "Make DIRECTORY (and all its parents) if it doesn't exist."
645 (let ((file-name-coding-system nnmail-pathname-coding-system)
646 (pathname-coding-system nnmail-pathname-coding-system))
648 (not (file-exists-p directory)))
649 (make-directory directory t)))
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))
659 (write-region (point-min) (point-max) file nil 'quietly)))
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))
666 (write-region-as-binary (point-min) (point-max) file nil 'quietly))
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))
673 (write-region-as-coding-system
674 coding-system (point-min) (point-max) file nil 'quietly))
676 (defun gnus-delete-file (file)
677 "Delete FILE if it exists."
678 (when (file-exists-p file)
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)))
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."
693 (while (re-search-forward gnus-emphasize-whitespace-regexp end 'move)
694 (gnus-put-text-property beg (match-beginning 0) prop val)
696 (gnus-put-text-property beg (point) prop val)))))
698 (defun gnus-put-text-property-excluding-characters-with-faces (beg end
700 "The same as `put-text-property', but don't put props on characters with the `gnus-face' property."
703 (when (get-text-property b 'gnus-face)
704 (setq b (next-single-property-change b 'gnus-face nil end)))
706 (gnus-put-text-property
707 b (setq b (next-single-property-change b 'gnus-face nil end))
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.
716 (defvar gnus-atomic-be-safe t
717 "If t, certain operations will be protected from interruption by C-g.")
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))
730 (put 'gnus-atomic-progn 'lisp-indent-function 0)
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.
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)
746 (sym-temp-map (mapcar (lambda (x) (list (cadr x) (car x)))
748 (temp-sym-let (mapcar (lambda (x) (list (car x)
749 `(and (boundp ',(cadr x))
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
759 (setq ,result (progn ,@forms))
760 (setq ,@temp-sym-assign))
761 (let ((inhibit-quit gnus-atomic-be-safe))
762 (setq ,@sym-temp-assign))
765 (put 'gnus-atomic-progn-assign 'lisp-indent-function 1)
766 ;(put 'gnus-atomic-progn-assign 'edebug-form-spec '(sexp body))
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."
777 (push (car tpairs) syms)
778 (setq tpairs (cddr tpairs)))
779 `(gnus-atomic-progn-assign ,syms
782 ;(put 'gnus-atomic-setq 'edebug-form-spec '(body))
785 ;;; Functions for saving to babyl/mail files.
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."
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*")))
797 (or (get-file-buffer filename)
798 (file-exists-p filename)
801 (concat "\"" filename "\" does not exist, create it? ")))
802 (let ((file-buffer (create-file-buffer filename)))
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")))
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)))
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)
822 ;; File has been visited, in 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.
830 (narrow-to-region (point-max) (point-max)))
831 (insert-buffer-substring tmpbuf)
833 (goto-char (point-min))
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))
844 (kill-buffer tmpbuf)))
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*")))
852 ;; Create the file, if it doesn't exist.
853 (when (and (not (get-file-buffer filename))
854 (not (file-exists-p filename)))
857 (concat "\"" filename "\" does not exist, create it? ")))
858 (let ((file-buffer (create-file-buffer filename)))
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")))
868 (insert-buffer-substring artbuf)
869 (goto-char (point-min))
870 (if (looking-at "From ")
872 (insert "From nobody " (current-time-string) "\n"))
873 (let (case-fold-search)
874 (while (re-search-forward "^From " nil t)
877 ;; Decide whether to append to a file or to an Emacs buffer.
878 (let ((outbuf (get-file-buffer filename)))
880 (let ((buffer-read-only nil))
882 (goto-char (point-max))
884 (unless (looking-at "\n\n")
885 (goto-char (point-max))
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)
894 ;; File has been visited, in buffer OUTBUF.
896 (let ((buffer-read-only nil))
897 (goto-char (point-max))
901 (insert-buffer-substring tmpbuf)))))
902 (kill-buffer tmpbuf)))
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))
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."
920 (setq arg (funcall (pop myfuns) arg)))
923 (defun gnus-run-hooks (&rest funcs)
924 "Does the same as `run-hooks', but saves excursion."
925 (let ((buf (current-buffer)))
927 (apply 'run-hooks funcs)
931 ;;; .netrc and .authinforc parsing
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)
938 (let ((tokens '("machine" "default" "login"
939 "password" "account" "macdef" "force"
941 alist elem result pair)
942 (insert-file-contents file)
943 (goto-char (point-min))
944 ;; Go through the file, line by line.
946 (narrow-to-region (point) (gnus-point-at-eol))
947 ;; For each line, get the tokens and values.
949 (skip-chars-forward "\t ")
950 ;; Skip lines that begin with a "#".
951 (if (eq (char-after) ?#)
952 (goto-char (point-max))
955 (if (= (following-char) ?\")
956 (read (current-buffer))
958 (point) (progn (skip-chars-forward "^\t ")
961 ((equal elem "macdef")
962 ;; We skip past the macro definition.
964 (while (and (zerop (forward-line 1))
966 (narrow-to-region (point) (point)))
967 ((member elem tokens)
968 ;; Tokens that don't have a following value are ignored,
970 (when (and pair (or (cdr pair)
971 (equal (car pair) "default")))
973 (setq pair (list elem)))
975 ;; Values that haven't got a preceding token are ignored.
979 (setq pair nil)))))))
981 (push (nreverse alist) result))
986 (nreverse result)))))
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."
995 (when (equal (cdr (assoc "machine" (car list))) machine)
996 (push (car list) result))
999 ;; No machine name matches, so we look for default entries.
1001 (when (assoc "default" (car rest))
1002 (push (car rest) result))
1005 (setq result (nreverse result))
1007 (not (equal (or port defaultport "nntp")
1008 (or (gnus-netrc-get (car result) "port")
1009 defaultport "nntp"))))
1013 (defun gnus-netrc-get (alist type)
1014 "Return the value of token TYPE from ALIST."
1015 (cdr (assoc type alist)))
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)
1025 (set-buffer gnus-group-buffer)
1026 (eq major-mode 'gnus-group-mode))))
1028 (defun gnus-remove-duplicates (list)
1029 (let (new (tail list))
1031 (or (member (car tail) new)
1032 (setq new (cons (car tail) new)))
1033 (setq tail (cdr tail)))
1036 (defun gnus-delete-if (predicate list)
1037 "Delete elements from LIST that satisfy PREDICATE."
1040 (unless (funcall predicate (car list))
1041 (push (car list) out))
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."
1051 (while (setq entry (assq key alist))
1052 (setq alist (delq entry alist)))
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))))
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) "^.*")
1066 (unless (string-match "\\$$" re) ".*$")))
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)))
1072 (set-window-start win (or point (point))))))
1074 (defun gnus-annotation-in-region-p (b e)
1076 (eq (cadr (memq 'gnus-undeletable (text-properties-at b))) t)
1077 (text-property-any b e 'gnus-undeletable t)))
1079 (defun gnus-or (&rest elems)
1080 "Return non-nil if any of the elements are non-nil."
1084 (throw 'found t)))))
1086 (defun gnus-and (&rest elems)
1087 "Return non-nil if all of the elements are non-nil."
1091 (throw 'found nil)))
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
1103 (insert (format "%S %d %d y\n"
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))))))
1111 (goto-char (point-max))
1112 (while (search-backward "\\." nil t)
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)
1123 (or (>= (length l1) (length l2))
1124 (setq l1 (prog1 l2 (setq l2 l1))))
1126 (or (member (car l2) l1)
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."
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)))
1141 (gnus-add-text-properties start end properties object))))
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."
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)))
1153 (remove-text-properties start end properties object))
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)))))
1162 (defcustom gnus-use-byte-compile t
1163 "If non-nil, byte-compile crucial run-time codes."
1166 :group 'gnus-various)
1168 (defun gnus-byte-compile (form)
1169 "Byte-compile FORM if `gnus-use-byte-compile' is non-nil."
1170 (if gnus-use-byte-compile
1173 (defalias 'gnus-byte-compile 'byte-compile)
1174 (byte-compile form))
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'."
1184 (if (equal key (caar alist))
1186 (setcdr alist (gnus-remassoc key (cdr alist)))
1189 (defun gnus-update-alist-soft (key value alist)
1191 (cons (cons key value) (gnus-remassoc key alist))
1192 (gnus-remassoc key alist)))
1194 (defun gnus-create-info-command (node)
1195 "Create a command that will go to info NODE."
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)))
1203 (defun gnus-not-ignore (&rest args)
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.")
1211 (defun gnus-url-unhex (x)
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) ""))
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))))
1230 (gnus-url-unhex (elt str (+ start 2))))))
1232 tmp (substring str 0 start)
1235 (char-to-string code))
1236 ((or (= code ?\n) (= code ?\r))
1238 (t (char-to-string code))))
1239 str (substring str (match-end 0)))))
1240 (setq tmp (concat tmp str))
1243 (provide 'gnus-util)
1245 ;;; gnus-util.el ends here