1 ;;; gnus-util.el --- utility functions for Semi-gnus
2 ;; Copyright (C) 1996,97,98 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Katsumi Yamaoka <yamaoka@jpl.org>
6 ;; Keywords: mail, news, MIME
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)
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.
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
35 (eval-when-compile (require 'cl))
40 (when (locate-library "rmail")
44 (autoload 'nnmail-date-to-time "nnmail")
45 (autoload 'rmail-insert-rmail-file-header "rmail")
46 (autoload 'rmail-count-new-messages "rmail")
47 (autoload 'rmail-show-message "rmail"))
49 (defun gnus-boundp (variable)
50 "Return non-nil if VARIABLE is bound and non-nil."
51 (and (boundp variable)
52 (symbol-value variable)))
54 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
55 "Pop to BUFFER, evaluate FORMS, and then return to the original window."
56 (let ((tempvar (make-symbol "GnusStartBufferWindow"))
58 (buf (make-symbol "buf")))
59 `(let* ((,tempvar (selected-window))
61 (,w (get-buffer-window ,buf 'visible)))
67 (set-buffer (window-buffer ,w)))
70 (select-window ,tempvar)))))
72 (put 'gnus-eval-in-buffer-window 'lisp-indent-function 1)
73 (put 'gnus-eval-in-buffer-window 'edebug-form-spec '(form body))
75 (defmacro gnus-intern-safe (string hashtable)
76 "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
77 `(let ((symbol (intern ,string ,hashtable)))
82 ;; Avoid byte-compile warning.
83 ;; In Mule, this function will be redefined to `truncate-string',
84 ;; which takes 3 or 4 args.
85 (defun gnus-truncate-string (str width &rest ignore)
86 (substring str 0 width))
88 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
89 ;; to limit the length of a string. This function is necessary since
90 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
91 (defsubst gnus-limit-string (str width)
92 (if (> (length str) width)
93 (substring str 0 width)
96 (defsubst gnus-functionp (form)
97 "Return non-nil if FORM is funcallable."
98 (or (and (symbolp form) (fboundp form))
99 (and (listp form) (eq (car form) 'lambda))
100 (byte-code-function-p form)))
102 (defsubst gnus-goto-char (point)
103 (and point (goto-char point)))
105 (defmacro gnus-buffer-exists-p (buffer)
106 `(let ((buffer ,buffer))
108 (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
111 (defmacro gnus-kill-buffer (buffer)
112 `(let ((buf ,buffer))
113 (when (gnus-buffer-exists-p buf)
116 (if (fboundp 'point-at-bol)
117 (fset 'gnus-point-at-bol 'point-at-bol)
118 (defun gnus-point-at-bol ()
119 "Return point at the beginning of the line."
126 (if (fboundp 'point-at-eol)
127 (fset 'gnus-point-at-eol 'point-at-eol)
128 (defun gnus-point-at-eol ()
129 "Return point at the end of the line."
136 (defun gnus-delete-first (elt list)
137 "Delete by side effect the first occurrence of ELT as a member of LIST."
138 (if (equal (car list) elt)
141 (while (and (cdr list)
142 (not (equal (cadr list) elt)))
143 (setq list (cdr list)))
145 (setcdr list (cddr list)))
148 ;; Delete the current line (and the next N lines).
149 (defmacro gnus-delete-line (&optional n)
150 `(delete-region (progn (beginning-of-line) (point))
151 (progn (forward-line ,(or n 1)) (point))))
153 (defun gnus-byte-code (func)
154 "Return a form that can be `eval'ed based on FUNC."
155 (let ((fval (indirect-function func)))
156 (if (byte-code-function-p fval)
157 (let ((flist (append fval nil)))
158 (setcar flist 'byte-code)
160 (cons 'progn (cddr fval)))))
162 (defun gnus-extract-address-components (from)
164 ;; First find the address - the thing with the @ in it. This may
165 ;; not be accurate in mail addresses, but does the trick most of
166 ;; the time in news messages.
167 (when (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
168 (setq address (substring from (match-beginning 0) (match-end 0))))
169 ;; Then we check whether the "name <address>" format is used.
171 ;; Linear white space is not required.
172 (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
173 (and (setq name (substring from 0 (match-beginning 0)))
174 ;; Strip any quotes from the name.
175 (string-match "\".*\"" name)
176 (setq name (substring name 1 (1- (match-end 0))))))
177 ;; If not, then "address (name)" is used.
179 (and (string-match "(.+)" from)
180 (setq name (substring from (1+ (match-beginning 0))
181 (1- (match-end 0)))))
182 (and (string-match "()" from)
184 ;; XOVER might not support folded From headers.
185 (and (string-match "(.*" from)
186 (setq name (substring from (1+ (match-beginning 0))
188 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
189 (list (or name from) (or address from))))
191 (defun gnus-fetch-field (field)
192 "Return the value of the header FIELD of current article."
195 (let ((case-fold-search t)
196 (inhibit-point-motion-hooks t))
197 (nnheader-narrow-to-headers)
198 (message-fetch-field field)))))
200 (defun gnus-goto-colon ()
202 (search-forward ":" (gnus-point-at-eol) t))
204 (defun gnus-remove-text-with-property (prop)
205 "Delete all text in the current buffer with text property PROP."
207 (goto-char (point-min))
209 (while (get-text-property (point) prop)
211 (goto-char (next-single-property-change (point) prop nil (point-max))))))
213 (defun gnus-newsgroup-directory-form (newsgroup)
214 "Make hierarchical directory name from NEWSGROUP name."
215 (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
216 (len (length newsgroup))
218 ;; If this is a foreign group, we don't want to translate the
220 (if (setq idx (string-match ":" newsgroup))
221 (aset newsgroup idx ?/)
223 ;; Replace all occurrences of `.' with `/'.
225 (when (= (aref newsgroup idx) ?.)
226 (aset newsgroup idx ?/))
230 (defun gnus-newsgroup-savable-name (group)
231 ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
233 (nnheader-replace-chars-in-string group ?/ ?.))
235 (defun gnus-string> (s1 s2)
236 (not (or (string< s1 s2)
241 (defun gnus-days-between (date1 date2)
242 ;; Return the number of days between date1 and date2.
243 (- (gnus-day-number date1) (gnus-day-number date2)))
245 (defun gnus-day-number (date)
246 (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
247 (timezone-parse-date date))))
248 (timezone-absolute-from-gregorian
249 (nth 1 dat) (nth 2 dat) (car dat))))
251 (defun gnus-time-to-day (time)
252 "Convert TIME to day number."
253 (let ((tim (decode-time time)))
254 (timezone-absolute-from-gregorian
255 (nth 4 tim) (nth 3 tim) (nth 5 tim))))
257 (defun gnus-encode-date (date)
258 "Convert DATE to internal time."
259 (let* ((parse (timezone-parse-date date))
260 (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
261 (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
262 (encode-time (caddr time) (cadr time) (car time)
263 (caddr date) (cadr date) (car date)
264 (* 60 (timezone-zone-to-minute (nth 4 date))))))
266 (defun gnus-time-minus (t1 t2)
267 "Subtract two internal times."
268 (let ((borrow (< (cadr t1) (cadr t2))))
269 (list (- (car t1) (car t2) (if borrow 1 0))
270 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
272 (defun gnus-time-less (t1 t2)
273 "Say whether time T1 is less than time T2."
274 (or (< (car t1) (car t2))
275 (and (= (car t1) (car t2))
276 (< (nth 1 t1) (nth 1 t2)))))
278 (defun gnus-file-newer-than (file date)
279 (let ((fdate (nth 5 (file-attributes file))))
280 (or (> (car fdate) (car date))
281 (and (= (car fdate) (car date))
282 (> (nth 1 fdate) (nth 1 date))))))
286 (defmacro gnus-local-set-keys (&rest plist)
287 "Set the keys in PLIST in the current keymap."
288 `(gnus-define-keys-1 (current-local-map) ',plist))
290 (defmacro gnus-define-keys (keymap &rest plist)
291 "Define all keys in PLIST in KEYMAP."
292 `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
294 (defmacro gnus-define-keys-safe (keymap &rest plist)
295 "Define all keys in PLIST in KEYMAP without overwriting previous definitions."
296 `(gnus-define-keys-1 (quote ,keymap) (quote ,plist) t))
298 (put 'gnus-define-keys 'lisp-indent-function 1)
299 (put 'gnus-define-keys-safe 'lisp-indent-function 1)
300 (put 'gnus-local-set-keys 'lisp-indent-function 1)
302 (defmacro gnus-define-keymap (keymap &rest plist)
303 "Define all keys in PLIST in KEYMAP."
304 `(gnus-define-keys-1 ,keymap (quote ,plist)))
306 (put 'gnus-define-keymap 'lisp-indent-function 1)
308 (defun gnus-define-keys-1 (keymap plist &optional safe)
310 (error "Can't set keys in a null keymap"))
311 (cond ((symbolp keymap)
312 (setq keymap (symbol-value keymap)))
315 (set (car keymap) nil)
316 (define-prefix-command (car keymap))
317 (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
318 (setq keymap (symbol-value (car keymap)))))
321 (when (symbolp (setq key (pop plist)))
322 (setq key (symbol-value key)))
324 (eq (lookup-key keymap key) 'undefined))
325 (define-key keymap key (pop plist))
328 (defun gnus-completing-read (default prompt &rest args)
329 ;; Like `completing-read', except that DEFAULT is the default argument.
330 (let* ((prompt (if default
331 (concat prompt " (default " default ") ")
332 (concat prompt " ")))
333 (answer (apply 'completing-read prompt args)))
334 (if (or (null answer) (zerop (length answer)))
338 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
340 (defun gnus-y-or-n-p (prompt)
345 (defun gnus-yes-or-no-p (prompt)
350 (defun gnus-dd-mmm (messy-date)
351 "Return a string like DD-MMM from a big messy string."
352 (let ((datevec (ignore-errors (timezone-parse-date messy-date))))
353 (if (or (not datevec)
354 (string-equal "0" (aref datevec 1)))
358 ;; Make sure leading zeroes are stripped.
359 (number-to-string (string-to-number (aref datevec 2)))
363 (nth (1- (string-to-number (aref datevec 1)))
364 timezone-months-assoc))
367 (defmacro gnus-date-get-time (date)
368 "Convert DATE string to Emacs time.
369 Cache the result as a text property stored in DATE."
370 ;; Either return the cached value...
374 (or (get-text-property 0 'gnus-time d)
375 ;; or compute the value...
376 (let ((time (nnmail-date-to-time d)))
377 ;; and store it back in the string.
378 (put-text-property 0 1 'gnus-time time d)
381 (defsubst gnus-time-iso8601 (time)
382 "Return a string of TIME in YYMMDDTHHMMSS format."
383 (format-time-string "%Y%m%dT%H%M%S" time))
385 (defun gnus-date-iso8601 (date)
386 "Convert the DATE to YYMMDDTHHMMSS."
388 (gnus-time-iso8601 (gnus-date-get-time date))
391 (defun gnus-mode-string-quote (string)
392 "Quote all \"%\"'s in STRING."
394 (gnus-set-work-buffer)
396 (goto-char (point-min))
397 (while (search-forward "%" nil t)
401 ;; Make a hash table (default and minimum size is 256).
402 ;; Optional argument HASHSIZE specifies the table size.
403 (defun gnus-make-hashtable (&optional hashsize)
404 (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 256) 256) 0))
406 ;; Make a number that is suitable for hashing; bigger than MIN and
407 ;; equal to some 2^x. Many machines (such as sparcs) do not have a
408 ;; hardware modulo operation, so they implement it in software. On
409 ;; many sparcs over 50% of the time to intern is spent in the modulo.
410 ;; Yes, it's slower than actually computing the hash from the string!
411 ;; So we use powers of 2 so people can optimize the modulo to a mask.
412 (defun gnus-create-hash-size (min)
418 (defcustom gnus-verbose 7
419 "*Integer that says how verbose Gnus should be.
420 The higher the number, the more messages Gnus will flash to say what
421 it's doing. At zero, Gnus will be totally mute; at five, Gnus will
422 display most important messages; and at ten, Gnus will keep on
423 jabbering all the time."
427 ;; Show message if message has a lower level than `gnus-verbose'.
428 ;; Guideline for numbers:
429 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
430 ;; for things that take a long time, 7 - not very important messages
431 ;; on stuff, 9 - messages inside loops.
432 (defun gnus-message (level &rest args)
433 (if (<= level gnus-verbose)
434 (apply 'message args)
435 ;; We have to do this format thingy here even if the result isn't
436 ;; shown - the return value has to be the same as the return value
438 (apply 'format args)))
440 (defun gnus-error (level &rest args)
441 "Beep an error if LEVEL is equal to or less than `gnus-verbose'."
442 (when (<= (floor level) gnus-verbose)
443 (apply 'message args)
446 (when (and (floatp level)
447 (not (zerop (setq duration (* 10 (- level (floor level)))))))
448 (sit-for duration))))
451 (defun gnus-split-references (references)
452 "Return a list of Message-IDs in REFERENCES."
455 (while (string-match "<[^>]+>" references beg)
456 (push (substring references (match-beginning 0) (setq beg (match-end 0)))
460 (defun gnus-parent-id (references &optional n)
461 "Return the last Message-ID in REFERENCES.
462 If N, return the Nth ancestor instead."
464 (let ((ids (inline (gnus-split-references references))))
465 (while (nthcdr (or n 1) ids)
466 (setq ids (cdr ids)))
469 (defsubst gnus-buffer-live-p (buffer)
470 "Say whether BUFFER is alive or not."
473 (buffer-name (get-buffer buffer))))
475 (defun gnus-horizontal-recenter ()
476 "Recenter the current buffer horizontally."
477 (if (< (current-column) (/ (window-width) 2))
478 (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
479 (let* ((orig (point))
480 (end (window-end (get-buffer-window (current-buffer) t)))
483 ;; Find the longest line currently displayed in the window.
484 (goto-char (window-start))
485 (while (and (not (eobp))
488 (setq max (max max (current-column)))
491 ;; Scroll horizontally to center (sort of) the point.
492 (if (> max (window-width))
494 (get-buffer-window (current-buffer) t)
495 (min (- (current-column) (/ (window-width) 3))
496 (+ 2 (- max (window-width)))))
497 (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
500 (defun gnus-read-event-char ()
501 "Get the next event."
502 (let ((event (read-event)))
503 ;; should be gnus-characterp, but this can't be called in XEmacs anyway
504 (cons (and (numberp event) event) event)))
506 (defun gnus-sortable-date (date)
507 "Make sortable string by string-lessp from DATE.
508 Timezone package is used."
511 (setq date (inline (timezone-fix-time
513 (aref (inline (timezone-parse-date date)) 4))))
515 (timezone-make-sortable-date
516 (aref date 0) (aref date 1) (aref date 2)
518 (timezone-make-time-string
519 (aref date 3) (aref date 4) (aref date 5))))))
522 (defun gnus-copy-file (file &optional to)
525 (list (read-file-name "Copy file: " default-directory)
526 (read-file-name "Copy file to: " default-directory)))
528 (setq to (read-file-name "Copy file to: " default-directory)))
529 (when (file-directory-p to)
530 (setq to (concat (file-name-as-directory to)
531 (file-name-nondirectory file))))
534 (defun gnus-kill-all-overlays ()
535 "Delete all overlays in the current buffer."
536 (let* ((overlayss (overlay-lists))
537 (buffer-read-only nil)
538 (overlays (delq nil (nconc (car overlayss) (cdr overlayss)))))
540 (delete-overlay (pop overlays)))))
542 (defvar gnus-work-buffer " *gnus work*")
544 (defun gnus-set-work-buffer ()
545 "Put point in the empty Gnus work buffer."
546 (if (get-buffer gnus-work-buffer)
548 (set-buffer gnus-work-buffer)
550 (set-buffer (gnus-get-buffer-create gnus-work-buffer))
551 (kill-all-local-variables)
552 (buffer-disable-undo (current-buffer))))
554 (defmacro gnus-group-real-name (group)
555 "Find the real name of a foreign newsgroup."
556 `(let ((gname ,group))
557 (if (string-match "^[^:]+:" gname)
558 (substring gname (match-end 0))
561 (defun gnus-make-sort-function (funs)
562 "Return a composite sort condition based on the functions in FUNC."
564 ((not (listp funs)) funs)
568 ,(gnus-make-sort-function-1 (reverse funs))))
572 (defun gnus-make-sort-function-1 (funs)
573 "Return a composite sort condition based on the functions in FUNC."
575 `(or (,(car funs) t1 t2)
576 (and (not (,(car funs) t2 t1))
577 ,(gnus-make-sort-function-1 (cdr funs))))
578 `(,(car funs) t1 t2)))
580 (defun gnus-turn-off-edit-menu (type)
581 "Turn off edit menu in `gnus-TYPE-mode-map'."
582 (define-key (symbol-value (intern (format "gnus-%s-mode-map" type)))
583 [menu-bar edit] 'undefined))
585 (defun gnus-prin1 (form)
586 "Use `prin1' on FORM in the current buffer.
587 Bind `print-quoted' and `print-readably' to t while printing."
588 (let ((print-quoted t)
590 (print-escape-multibyte nil)
591 print-level print-length)
592 (prin1 form (current-buffer))))
594 (defun gnus-prin1-to-string (form)
595 "The same as `prin1', but bind `print-quoted' and `print-readably' to t."
596 (let ((print-quoted t)
598 (prin1-to-string form)))
600 (defun gnus-make-directory (directory)
601 "Make DIRECTORY (and all its parents) if it doesn't exist."
603 (not (file-exists-p directory)))
604 (make-directory directory t))
607 (defun gnus-write-buffer (file)
608 "Write the current buffer's contents to FILE."
609 ;; Make sure the directory exists.
610 (gnus-make-directory (file-name-directory file))
612 (write-region (point-min) (point-max) file nil 'quietly))
614 (defun gnus-write-buffer-as-binary (file)
615 "Write the current buffer's contents to FILE without code conversion."
616 ;; Make sure the directory exists.
617 (gnus-make-directory (file-name-directory file))
619 (write-region-as-binary (point-min) (point-max) file nil 'quietly))
621 (defun gnus-write-buffer-as-coding-system (coding-system file)
622 "Write the current buffer's contents to FILE with code conversion."
623 ;; Make sure the directory exists.
624 (gnus-make-directory (file-name-directory file))
626 (write-region-as-coding-system
627 coding-system (point-min) (point-max) file nil 'quietly))
629 (defun gnus-delete-file (file)
630 "Delete FILE if it exists."
631 (when (file-exists-p file)
634 (defun gnus-strip-whitespace (string)
635 "Return STRING stripped of all whitespace."
636 (while (string-match "[\r\n\t ]+" string)
637 (setq string (replace-match "" t t string)))
640 (defun gnus-put-text-property-excluding-newlines (beg end prop val)
641 "The same as `put-text-property', but don't put this prop on any newlines in the region."
646 (while (re-search-forward "[ \t]*\n" end 'move)
647 (gnus-put-text-property beg (match-beginning 0) prop val)
649 (gnus-put-text-property beg (point) prop val)))))
651 (defun gnus-put-text-property-excluding-characters-with-faces (beg end
653 "The same as `put-text-property', but don't put props on characters with the `gnus-face' property."
656 (when (get-text-property b 'gnus-face)
657 (setq b (next-single-property-change b 'gnus-face nil end)))
659 (gnus-put-text-property
660 b (setq b (next-single-property-change b 'gnus-face nil end))
663 ;;; Protected and atomic operations. dmoore@ucsd.edu 21.11.1996
664 ;;; The primary idea here is to try to protect internal datastructures
665 ;;; from becoming corrupted when the user hits C-g, or if a hook or
666 ;;; similar blows up. Often in Gnus multiple tables/lists need to be
667 ;;; updated at the same time, or information can be lost.
669 (defvar gnus-atomic-be-safe t
670 "If t, certain operations will be protected from interruption by C-g.")
672 (defmacro gnus-atomic-progn (&rest forms)
673 "Evaluate FORMS atomically, which means to protect the evaluation
674 from being interrupted by the user. An error from the forms themselves
675 will return without finishing the operation. Since interrupts from
676 the user are disabled, it is recommended that only the most minimal
677 operations are performed by FORMS. If you wish to assign many
678 complicated values atomically, compute the results into temporary
679 variables and then do only the assignment atomically."
680 `(let ((inhibit-quit gnus-atomic-be-safe))
683 (put 'gnus-atomic-progn 'lisp-indent-function 0)
685 (defmacro gnus-atomic-progn-assign (protect &rest forms)
686 "Evaluate FORMS, but insure that the variables listed in PROTECT
687 are not changed if anything in FORMS signals an error or otherwise
688 non-locally exits. The variables listed in PROTECT are updated atomically.
689 It is safe to use gnus-atomic-progn-assign with long computations.
691 Note that if any of the symbols in PROTECT were unbound, they will be
692 set to nil on a sucessful assignment. In case of an error or other
693 non-local exit, it will still be unbound."
694 (let* ((temp-sym-map (mapcar (lambda (x) (list (make-symbol
695 (concat (symbol-name x)
699 (sym-temp-map (mapcar (lambda (x) (list (cadr x) (car x)))
701 (temp-sym-let (mapcar (lambda (x) (list (car x)
702 `(and (boundp ',(cadr x))
705 (sym-temp-let sym-temp-map)
706 (temp-sym-assign (apply 'append temp-sym-map))
707 (sym-temp-assign (apply 'append sym-temp-map))
708 (result (make-symbol "result-tmp")))
709 `(let (,@temp-sym-let
712 (setq ,result (progn ,@forms))
713 (setq ,@temp-sym-assign))
714 (let ((inhibit-quit gnus-atomic-be-safe))
715 (setq ,@sym-temp-assign))
718 (put 'gnus-atomic-progn-assign 'lisp-indent-function 1)
719 ;(put 'gnus-atomic-progn-assign 'edebug-form-spec '(sexp body))
721 (defmacro gnus-atomic-setq (&rest pairs)
722 "Similar to setq, except that the real symbols are only assigned when
723 there are no errors. And when the real symbols are assigned, they are
724 done so atomically. If other variables might be changed via side-effect,
725 see gnus-atomic-progn-assign. It is safe to use gnus-atomic-setq
726 with potentially long computations."
730 (push (car tpairs) syms)
731 (setq tpairs (cddr tpairs)))
732 `(gnus-atomic-progn-assign ,syms
735 ;(put 'gnus-atomic-setq 'edebug-form-spec '(body))
738 ;;; Functions for saving to babyl/mail files.
740 (defvar rmail-default-rmail-file)
741 (defun gnus-output-to-rmail (filename &optional ask)
742 "Append the current article to an Rmail file named FILENAME."
744 ;; Most of these codes are borrowed from rmailout.el.
745 (setq filename (expand-file-name filename))
746 (setq rmail-default-rmail-file filename)
747 (let ((artbuf (current-buffer))
748 (tmpbuf (get-buffer-create " *Gnus-output*")))
750 (or (get-file-buffer filename)
751 (file-exists-p filename)
754 (concat "\"" filename "\" does not exist, create it? ")))
755 (let ((file-buffer (create-file-buffer filename)))
757 (set-buffer file-buffer)
758 (rmail-insert-rmail-file-header)
759 (let ((require-final-newline nil))
760 (gnus-write-buffer filename)))
761 (kill-buffer file-buffer))
762 (error "Output file does not exist")))
765 (insert-buffer-substring artbuf)
766 (gnus-convert-article-to-rmail)
767 ;; Decide whether to append to a file or to an Emacs buffer.
768 (let ((outbuf (get-file-buffer filename)))
770 (append-to-file (point-min) (point-max) filename)
771 ;; File has been visited, in buffer OUTBUF.
773 (let ((buffer-read-only nil)
774 (msg (and (boundp 'rmail-current-message)
775 (symbol-value 'rmail-current-message))))
776 ;; If MSG is non-nil, buffer is in RMAIL mode.
779 (narrow-to-region (point-max) (point-max)))
780 (insert-buffer-substring tmpbuf)
782 (goto-char (point-min))
784 (search-backward "\n\^_")
785 (narrow-to-region (point) (point-max))
786 (rmail-count-new-messages t)
787 (when (rmail-summary-exists)
788 (rmail-select-summary
789 (rmail-update-summary)))
790 (rmail-count-new-messages t)
791 (rmail-show-message msg))
793 (kill-buffer tmpbuf)))
795 (defun gnus-output-to-mail (filename &optional ask)
796 "Append the current article to a mail file named FILENAME."
797 (setq filename (expand-file-name filename))
798 (let ((artbuf (current-buffer))
799 (tmpbuf (get-buffer-create " *Gnus-output*")))
801 ;; Create the file, if it doesn't exist.
802 (when (and (not (get-file-buffer filename))
803 (not (file-exists-p filename)))
806 (concat "\"" filename "\" does not exist, create it? ")))
807 (let ((file-buffer (create-file-buffer filename)))
809 (set-buffer file-buffer)
810 (let ((require-final-newline nil))
811 (gnus-write-buffer-as-binary filename)))
812 (kill-buffer file-buffer))
813 (error "Output file does not exist")))
816 (insert-buffer-substring artbuf)
817 (goto-char (point-min))
818 (if (looking-at "From ")
820 (insert "From nobody " (current-time-string) "\n"))
821 (let (case-fold-search)
822 (while (re-search-forward "^From " nil t)
825 ;; Decide whether to append to a file or to an Emacs buffer.
826 (let ((outbuf (get-file-buffer filename)))
828 (let ((buffer-read-only nil))
830 (goto-char (point-max))
832 (unless (looking-at "\n\n")
833 (goto-char (point-max))
837 (goto-char (point-max))
838 (write-region-as-binary (point-min) (point-max)
840 ;; File has been visited, in buffer OUTBUF.
842 (let ((buffer-read-only nil))
843 (goto-char (point-max))
847 (insert-buffer-substring tmpbuf)))))
848 (kill-buffer tmpbuf)))
850 (defun gnus-convert-article-to-rmail ()
851 "Convert article in current buffer to Rmail message format."
852 (let ((buffer-read-only nil))
853 ;; Convert article directly into Babyl format.
854 (goto-char (point-min))
855 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
856 (while (search-forward "\n\^_" nil t) ;single char
857 (replace-match "\n^_" t t)) ;2 chars: "^" and "_"
858 (goto-char (point-max))
861 (defun gnus-map-function (funs arg)
862 "Applies the result of the first function in FUNS to the second, and so on.
863 ARG is passed to the first function."
866 (setq arg (funcall (pop myfuns) arg)))
869 (defun gnus-run-hooks (&rest funcs)
870 "Does the same as `run-hooks', but saves excursion."
871 (let ((buf (current-buffer)))
873 (apply 'run-hooks funcs)
877 ;;; .netrc and .authinforc parsing
880 (defvar gnus-netrc-syntax-table
881 (let ((table (copy-syntax-table text-mode-syntax-table)))
882 (modify-syntax-entry ?@ "w" table)
883 (modify-syntax-entry ?- "w" table)
884 (modify-syntax-entry ?_ "w" table)
885 (modify-syntax-entry ?! "w" table)
886 (modify-syntax-entry ?. "w" table)
887 (modify-syntax-entry ?, "w" table)
888 (modify-syntax-entry ?: "w" table)
889 (modify-syntax-entry ?\; "w" table)
890 (modify-syntax-entry ?% "w" table)
891 (modify-syntax-entry ?) "w" table)
892 (modify-syntax-entry ?( "w" table)
894 "Syntax table when parsing .netrc files.")
896 (defun gnus-parse-netrc (file)
897 "Parse FILE and return an list of all entries in the file."
898 (if (not (file-exists-p file))
901 (let ((tokens '("machine" "default" "login"
902 "password" "account" "macdef" "force"))
903 alist elem result pair)
904 (nnheader-set-temp-buffer " *netrc*")
907 (set-syntax-table gnus-netrc-syntax-table)
908 (insert-file-contents file)
909 (goto-char (point-min))
910 ;; Go through the file, line by line.
912 (narrow-to-region (point) (gnus-point-at-eol))
913 ;; For each line, get the tokens and values.
915 (skip-chars-forward "\t ")
917 (setq elem (buffer-substring
918 (point) (progn (forward-sexp 1) (point))))
920 ((equal elem "macdef")
921 ;; We skip past the macro definition.
923 (while (and (zerop (forward-line 1))
925 (narrow-to-region (point) (point)))
926 ((member elem tokens)
927 ;; Tokens that don't have a following value are ignored,
929 (when (and pair (or (cdr pair)
930 (equal (car pair) "default")))
932 (setq pair (list elem)))
934 ;; Values that haven't got a preceding token are ignored.
940 (push (nreverse alist) result))
946 (kill-buffer " *netrc*"))))))
948 (defun gnus-netrc-machine (list machine)
949 "Return the netrc values from LIST for MACHINE or for the default entry."
952 (not (equal (cdr (assoc "machine" (car list))) machine)))
955 (progn (while (and rest (not (assoc "default" (car rest))))
959 (defun gnus-netrc-get (alist type)
960 "Return the value of token TYPE from ALIST."
961 (cdr (assoc type alist)))
965 (defvar gnus-group-buffer) ; Compiler directive
966 (defun gnus-alive-p ()
967 "Say whether Gnus is running or not."
968 (and (boundp 'gnus-group-buffer)
969 (get-buffer gnus-group-buffer)
971 (set-buffer gnus-group-buffer)
972 (eq major-mode 'gnus-group-mode))))
974 (defun gnus-remove-duplicates (list)
975 (let (new (tail list))
977 (or (member (car tail) new)
978 (setq new (cons (car tail) new)))
979 (setq tail (cdr tail)))
982 (defun gnus-delete-if (predicate list)
983 "Delete elements from LIST that satisfy PREDICATE."
986 (unless (funcall predicate (car list))
987 (push (car list) out))
991 (defun gnus-delete-alist (key alist)
992 "Delete all entries in ALIST that have a key eq to KEY."
994 (while (setq entry (assq key alist))
995 (setq alist (delq entry alist)))
998 (defmacro gnus-pull (key alist)
999 "Modify ALIST to be without KEY."
1000 (unless (symbolp alist)
1001 (error "Not a symbol: %s" alist))
1002 `(setq ,alist (delq (assq ,key ,alist) ,alist)))
1004 (defun gnus-globalify-regexp (re)
1005 "Returns a regexp that matches a whole line, iff RE matches a part of it."
1006 (concat (unless (string-match "^\\^" re) "^.*")
1008 (unless (string-match "\\$$" re) ".*$")))
1010 (provide 'gnus-util)
1012 ;;; gnus-util.el ends here