1 ;;; gnus-util.el --- utility functions for Semi-gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Tatsuya Ichikawa <t-ichi@po.shiojiri.ne.jp>
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))
39 (eval-when-compile (require 'static))
42 (autoload 'rmail-insert-rmail-file-header "rmail")
43 (autoload 'rmail-count-new-messages "rmail")
44 (autoload 'rmail-show-message "rmail"))
46 (defun gnus-boundp (variable)
47 "Return non-nil if VARIABLE is bound and non-nil."
48 (and (boundp variable)
49 (symbol-value variable)))
51 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
52 "Pop to BUFFER, evaluate FORMS, and then return to the original window."
53 (let ((tempvar (make-symbol "GnusStartBufferWindow"))
55 (buf (make-symbol "buf")))
56 `(let* ((,tempvar (selected-window))
58 (,w (get-buffer-window ,buf 'visible)))
64 (set-buffer (window-buffer ,w)))
67 (select-window ,tempvar)))))
69 (put 'gnus-eval-in-buffer-window 'lisp-indent-function 1)
70 (put 'gnus-eval-in-buffer-window 'edebug-form-spec '(form body))
72 (defmacro gnus-intern-safe (string hashtable)
73 "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
74 `(let ((symbol (intern ,string ,hashtable)))
79 ;; Avoid byte-compile warning.
80 ;; In Mule, this function will be redefined to `truncate-string',
81 ;; which takes 3 or 4 args.
82 (defun gnus-truncate-string (str width &rest ignore)
83 (substring str 0 width))
85 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
86 ;; to limit the length of a string. This function is necessary since
87 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
88 (defsubst gnus-limit-string (str width)
89 (if (> (length str) width)
90 (substring str 0 width)
93 (defsubst gnus-functionp (form)
94 "Return non-nil if FORM is funcallable."
95 (or (and (symbolp form) (fboundp form))
96 (and (listp form) (eq (car form) 'lambda))
97 (byte-code-function-p form)))
99 (defsubst gnus-goto-char (point)
100 (and point (goto-char point)))
102 (defmacro gnus-buffer-exists-p (buffer)
103 `(let ((buffer ,buffer))
105 (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
108 (defmacro gnus-kill-buffer (buffer)
109 `(let ((buf ,buffer))
110 (when (gnus-buffer-exists-p buf)
114 ((fboundp 'point-at-bol)
115 (fset 'gnus-point-at-bol 'point-at-bol))
116 ((fboundp 'line-beginning-position)
117 (fset 'gnus-point-at-bol 'line-beginning-position))
119 (defun gnus-point-at-bol ()
120 "Return point at the beginning of the line."
128 ((fboundp 'point-at-eol)
129 (fset 'gnus-point-at-eol 'point-at-eol))
130 ((fboundp 'line-end-position)
131 (fset 'gnus-point-at-eol 'line-end-position))
133 (defun gnus-point-at-eol ()
134 "Return point at the end of the line."
142 (defun gnus-delete-first (elt list)
143 "Delete by side effect the first occurrence of ELT as a member of LIST."
144 (if (equal (car list) elt)
147 (while (and (cdr list)
148 (not (equal (cadr list) elt)))
149 (setq list (cdr list)))
151 (setcdr list (cddr list)))
154 ;; Delete the current line (and the next N lines).
155 (defmacro gnus-delete-line (&optional n)
156 `(delete-region (progn (beginning-of-line) (point))
157 (progn (forward-line ,(or n 1)) (point))))
159 (defun gnus-byte-code (func)
160 "Return a form that can be `eval'ed based on FUNC."
161 (let ((fval (indirect-function func)))
162 (if (byte-code-function-p fval)
163 (let ((flist (append fval nil)))
164 (setcar flist 'byte-code)
166 (cons 'progn (cddr fval)))))
168 (defun gnus-extract-address-components (from)
170 ;; First find the address - the thing with the @ in it. This may
171 ;; not be accurate in mail addresses, but does the trick most of
172 ;; the time in news messages.
173 (when (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
174 (setq address (substring from (match-beginning 0) (match-end 0))))
175 ;; Then we check whether the "name <address>" format is used.
177 ;; Linear white space is not required.
178 (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
179 (and (setq name (substring from 0 (match-beginning 0)))
180 ;; Strip any quotes from the name.
181 (string-match "\".*\"" name)
182 (setq name (substring name 1 (1- (match-end 0))))))
183 ;; If not, then "address (name)" is used.
185 (and (string-match "(.+)" from)
186 (setq name (substring from (1+ (match-beginning 0))
187 (1- (match-end 0)))))
188 (and (string-match "()" from)
190 ;; XOVER might not support folded From headers.
191 (and (string-match "(.*" from)
192 (setq name (substring from (1+ (match-beginning 0))
194 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
195 (list (or name from) (or address from))))
197 (defun gnus-fetch-field (field)
198 "Return the value of the header FIELD of current article."
201 (let ((case-fold-search t)
202 (inhibit-point-motion-hooks t))
203 (nnheader-narrow-to-headers)
204 (message-fetch-field field)))))
206 (defun gnus-goto-colon ()
208 (search-forward ":" (gnus-point-at-eol) t))
210 (defun gnus-remove-text-with-property (prop)
211 "Delete all text in the current buffer with text property PROP."
213 (goto-char (point-min))
215 (while (get-text-property (point) prop)
217 (goto-char (next-single-property-change (point) prop nil (point-max))))))
219 (defun gnus-newsgroup-directory-form (newsgroup)
220 "Make hierarchical directory name from NEWSGROUP name."
221 (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
222 (len (length newsgroup))
224 ;; If this is a foreign group, we don't want to translate the
226 (if (setq idx (string-match ":" newsgroup))
227 (aset newsgroup idx ?/)
229 ;; Replace all occurrences of `.' with `/'.
231 (when (= (aref newsgroup idx) ?.)
232 (aset newsgroup idx ?/))
236 (defun gnus-newsgroup-savable-name (group)
237 ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
239 (nnheader-replace-chars-in-string group ?/ ?.))
241 (defun gnus-string> (s1 s2)
242 (not (or (string< s1 s2)
247 (defun gnus-file-newer-than (file date)
248 (let ((fdate (nth 5 (file-attributes file))))
249 (or (> (car fdate) (car date))
250 (and (= (car fdate) (car date))
251 (> (nth 1 fdate) (nth 1 date))))))
255 (defmacro gnus-local-set-keys (&rest plist)
256 "Set the keys in PLIST in the current keymap."
257 `(gnus-define-keys-1 (current-local-map) ',plist))
259 (defmacro gnus-define-keys (keymap &rest plist)
260 "Define all keys in PLIST in KEYMAP."
261 `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
263 (defmacro gnus-define-keys-safe (keymap &rest plist)
264 "Define all keys in PLIST in KEYMAP without overwriting previous definitions."
265 `(gnus-define-keys-1 (quote ,keymap) (quote ,plist) t))
267 (put 'gnus-define-keys 'lisp-indent-function 1)
268 (put 'gnus-define-keys-safe 'lisp-indent-function 1)
269 (put 'gnus-local-set-keys 'lisp-indent-function 1)
271 (defmacro gnus-define-keymap (keymap &rest plist)
272 "Define all keys in PLIST in KEYMAP."
273 `(gnus-define-keys-1 ,keymap (quote ,plist)))
275 (put 'gnus-define-keymap 'lisp-indent-function 1)
277 (defun gnus-define-keys-1 (keymap plist &optional safe)
279 (error "Can't set keys in a null keymap"))
280 (cond ((symbolp keymap)
281 (setq keymap (symbol-value keymap)))
284 (set (car keymap) nil)
285 (define-prefix-command (car keymap))
286 (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
287 (setq keymap (symbol-value (car keymap)))))
290 (when (symbolp (setq key (pop plist)))
291 (setq key (symbol-value key)))
293 (eq (lookup-key keymap key) 'undefined))
294 (define-key keymap key (pop plist))
297 (defun gnus-completing-read (default prompt &rest args)
298 ;; Like `completing-read', except that DEFAULT is the default argument.
299 (let* ((prompt (if default
300 (concat prompt " (default " default ") ")
301 (concat prompt " ")))
302 (answer (apply 'completing-read prompt args)))
303 (if (or (null answer) (zerop (length answer)))
307 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
309 (defun gnus-y-or-n-p (prompt)
314 (defun gnus-yes-or-no-p (prompt)
319 (defun gnus-dd-mmm (messy-date)
320 "Return a string like DD-MMM from a big messy string."
321 (format-time-string "%d-%b" (safe-date-to-time messy-date)))
323 (defmacro gnus-date-get-time (date)
324 "Convert DATE string to Emacs time.
325 Cache the result as a text property stored in DATE."
326 ;; Either return the cached value...
330 (or (get-text-property 0 'gnus-time d)
331 ;; or compute the value...
332 (let ((time (safe-date-to-time d)))
333 ;; and store it back in the string.
334 (put-text-property 0 1 'gnus-time time d)
337 (defsubst gnus-time-iso8601 (time)
338 "Return a string of TIME in YYMMDDTHHMMSS format."
339 (format-time-string "%Y%m%dT%H%M%S" time))
341 (defun gnus-date-iso8601 (date)
342 "Convert the DATE to YYMMDDTHHMMSS."
344 (gnus-time-iso8601 (gnus-date-get-time date))
347 (defun gnus-mode-string-quote (string)
348 "Quote all \"%\"'s in STRING."
350 (gnus-set-work-buffer)
352 (goto-char (point-min))
353 (while (search-forward "%" nil t)
357 ;; Make a hash table (default and minimum size is 256).
358 ;; Optional argument HASHSIZE specifies the table size.
359 (defun gnus-make-hashtable (&optional hashsize)
360 (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 256) 256) 0))
362 ;; Make a number that is suitable for hashing; bigger than MIN and
363 ;; equal to some 2^x. Many machines (such as sparcs) do not have a
364 ;; hardware modulo operation, so they implement it in software. On
365 ;; many sparcs over 50% of the time to intern is spent in the modulo.
366 ;; Yes, it's slower than actually computing the hash from the string!
367 ;; So we use powers of 2 so people can optimize the modulo to a mask.
368 (defun gnus-create-hash-size (min)
374 (defcustom gnus-verbose 7
375 "*Integer that says how verbose Gnus should be.
376 The higher the number, the more messages Gnus will flash to say what
377 it's doing. At zero, Gnus will be totally mute; at five, Gnus will
378 display most important messages; and at ten, Gnus will keep on
379 jabbering all the time."
383 ;; Show message if message has a lower level than `gnus-verbose'.
384 ;; Guideline for numbers:
385 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
386 ;; for things that take a long time, 7 - not very important messages
387 ;; on stuff, 9 - messages inside loops.
388 (defun gnus-message (level &rest args)
389 (if (<= level gnus-verbose)
390 (apply 'message args)
391 ;; We have to do this format thingy here even if the result isn't
392 ;; shown - the return value has to be the same as the return value
394 (apply 'format args)))
396 (defun gnus-error (level &rest args)
397 "Beep an error if LEVEL is equal to or less than `gnus-verbose'."
398 (when (<= (floor level) gnus-verbose)
399 (apply 'message args)
402 (when (and (floatp level)
403 (not (zerop (setq duration (* 10 (- level (floor level)))))))
404 (sit-for duration))))
407 (defun gnus-split-references (references)
408 "Return a list of Message-IDs in REFERENCES."
411 (while (string-match "<[^>]+>" references beg)
412 (push (substring references (match-beginning 0) (setq beg (match-end 0)))
416 (defsubst gnus-parent-id (references &optional n)
417 "Return the last Message-ID in REFERENCES.
418 If N, return the Nth ancestor instead."
420 (let ((ids (inline (gnus-split-references references))))
421 (while (nthcdr (or n 1) ids)
422 (setq ids (cdr ids)))
425 (defsubst gnus-buffer-live-p (buffer)
426 "Say whether BUFFER is alive or not."
429 (buffer-name (get-buffer buffer))))
431 (defun gnus-horizontal-recenter ()
432 "Recenter the current buffer horizontally."
433 (if (< (current-column) (/ (window-width) 2))
434 (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
435 (let* ((orig (point))
436 (end (window-end (get-buffer-window (current-buffer) t)))
439 ;; Find the longest line currently displayed in the window.
440 (goto-char (window-start))
441 (while (and (not (eobp))
444 (setq max (max max (current-column)))
447 ;; Scroll horizontally to center (sort of) the point.
448 (if (> max (window-width))
450 (get-buffer-window (current-buffer) t)
451 (min (- (current-column) (/ (window-width) 3))
452 (+ 2 (- max (window-width)))))
453 (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
456 (defun gnus-read-event-char ()
457 "Get the next event."
458 (let ((event (read-event)))
459 ;; should be gnus-characterp, but this can't be called in XEmacs anyway
460 (cons (and (numberp event) event) event)))
462 (defun gnus-sortable-date (date)
463 "Make string suitable for sorting from DATE."
464 (gnus-time-iso8601 (date-to-time date)))
466 (defun gnus-copy-file (file &optional to)
469 (list (read-file-name "Copy file: " default-directory)
470 (read-file-name "Copy file to: " default-directory)))
472 (setq to (read-file-name "Copy file to: " default-directory)))
473 (when (file-directory-p to)
474 (setq to (concat (file-name-as-directory to)
475 (file-name-nondirectory file))))
478 (defun gnus-kill-all-overlays ()
479 "Delete all overlays in the current buffer."
480 (let* ((overlayss (overlay-lists))
481 (buffer-read-only nil)
482 (overlays (delq nil (nconc (car overlayss) (cdr overlayss)))))
484 (delete-overlay (pop overlays)))))
486 (defvar gnus-work-buffer " *gnus work*")
488 (defun gnus-set-work-buffer ()
489 "Put point in the empty Gnus work buffer."
490 (if (get-buffer gnus-work-buffer)
492 (set-buffer gnus-work-buffer)
494 (set-buffer (gnus-get-buffer-create gnus-work-buffer))
495 (kill-all-local-variables)))
497 (defmacro gnus-group-real-name (group)
498 "Find the real name of a foreign newsgroup."
499 `(let ((gname ,group))
500 (if (string-match "^[^:]+:" gname)
501 (substring gname (match-end 0))
504 (defun gnus-make-sort-function (funs)
505 "Return a composite sort condition based on the functions in FUNC."
507 ;; Just a simple function.
508 ((gnus-functionp funs) funs)
509 ;; No functions at all.
511 ;; A list of functions.
515 ,(gnus-make-sort-function-1 (reverse funs))))
516 ;; A list containing just one function.
520 (defun gnus-make-sort-function-1 (funs)
521 "Return a composite sort condition based on the functions in FUNC."
522 (let ((function (car funs))
525 (when (consp function)
528 ((eq (car function) 'not)
529 (setq function (cadr function)
532 ((gnus-functionp function)
535 (error "Invalid sort spec: %s" function))))
537 `(or (,function ,first ,last)
538 (and (not (,function ,last ,first))
539 ,(gnus-make-sort-function-1 (cdr funs))))
540 `(,function ,first ,last))))
542 (defun gnus-turn-off-edit-menu (type)
543 "Turn off edit menu in `gnus-TYPE-mode-map'."
544 (define-key (symbol-value (intern (format "gnus-%s-mode-map" type)))
545 [menu-bar edit] 'undefined))
547 (defun gnus-prin1 (form)
548 "Use `prin1' on FORM in the current buffer.
549 Bind `print-quoted' and `print-readably' to t while printing."
550 (let ((print-quoted t)
552 (print-escape-multibyte nil)
553 print-level print-length)
554 (prin1 form (current-buffer))))
556 (defun gnus-prin1-to-string (form)
557 "The same as `prin1', but bind `print-quoted' and `print-readably' to t."
558 (let ((print-quoted t)
560 (prin1-to-string form)))
562 (defun gnus-make-directory (directory)
563 "Make DIRECTORY (and all its parents) if it doesn't exist."
565 (not (file-exists-p directory)))
566 (make-directory directory t))
569 (defun gnus-write-buffer (file)
570 "Write the current buffer's contents to FILE."
571 ;; Make sure the directory exists.
572 (gnus-make-directory (file-name-directory file))
574 (write-region (point-min) (point-max) file nil 'quietly))
576 (defun gnus-write-buffer-as-binary (file)
577 "Write the current buffer's contents to FILE without code conversion."
578 ;; Make sure the directory exists.
579 (gnus-make-directory (file-name-directory file))
581 (write-region-as-binary (point-min) (point-max) file nil 'quietly))
583 (defun gnus-write-buffer-as-coding-system (coding-system file)
584 "Write the current buffer's contents to FILE with code conversion."
585 ;; Make sure the directory exists.
586 (gnus-make-directory (file-name-directory file))
588 (write-region-as-coding-system
589 coding-system (point-min) (point-max) file nil 'quietly))
591 (defun gnus-delete-file (file)
592 "Delete FILE if it exists."
593 (when (file-exists-p file)
596 (defun gnus-strip-whitespace (string)
597 "Return STRING stripped of all whitespace."
598 (while (string-match "[\r\n\t ]+" string)
599 (setq string (replace-match "" t t string)))
602 (defsubst gnus-put-text-property-excluding-newlines (beg end prop val)
603 "The same as `put-text-property', but don't put this prop on any newlines in the region."
608 (while (re-search-forward "[ \t]*\n" end 'move)
609 (gnus-put-text-property beg (match-beginning 0) prop val)
611 (gnus-put-text-property beg (point) prop val)))))
613 (defun gnus-put-text-property-excluding-characters-with-faces (beg end
615 "The same as `put-text-property', but don't put props on characters with the `gnus-face' property."
618 (when (get-text-property b 'gnus-face)
619 (setq b (next-single-property-change b 'gnus-face nil end)))
621 (gnus-put-text-property
622 b (setq b (next-single-property-change b 'gnus-face nil end))
625 ;;; Protected and atomic operations. dmoore@ucsd.edu 21.11.1996
626 ;;; The primary idea here is to try to protect internal datastructures
627 ;;; from becoming corrupted when the user hits C-g, or if a hook or
628 ;;; similar blows up. Often in Gnus multiple tables/lists need to be
629 ;;; updated at the same time, or information can be lost.
631 (defvar gnus-atomic-be-safe t
632 "If t, certain operations will be protected from interruption by C-g.")
634 (defmacro gnus-atomic-progn (&rest forms)
635 "Evaluate FORMS atomically, which means to protect the evaluation
636 from being interrupted by the user. An error from the forms themselves
637 will return without finishing the operation. Since interrupts from
638 the user are disabled, it is recommended that only the most minimal
639 operations are performed by FORMS. If you wish to assign many
640 complicated values atomically, compute the results into temporary
641 variables and then do only the assignment atomically."
642 `(let ((inhibit-quit gnus-atomic-be-safe))
645 (put 'gnus-atomic-progn 'lisp-indent-function 0)
647 (defmacro gnus-atomic-progn-assign (protect &rest forms)
648 "Evaluate FORMS, but insure that the variables listed in PROTECT
649 are not changed if anything in FORMS signals an error or otherwise
650 non-locally exits. The variables listed in PROTECT are updated atomically.
651 It is safe to use gnus-atomic-progn-assign with long computations.
653 Note that if any of the symbols in PROTECT were unbound, they will be
654 set to nil on a sucessful assignment. In case of an error or other
655 non-local exit, it will still be unbound."
656 (let* ((temp-sym-map (mapcar (lambda (x) (list (make-symbol
657 (concat (symbol-name x)
661 (sym-temp-map (mapcar (lambda (x) (list (cadr x) (car x)))
663 (temp-sym-let (mapcar (lambda (x) (list (car x)
664 `(and (boundp ',(cadr x))
667 (sym-temp-let sym-temp-map)
668 (temp-sym-assign (apply 'append temp-sym-map))
669 (sym-temp-assign (apply 'append sym-temp-map))
670 (result (make-symbol "result-tmp")))
671 `(let (,@temp-sym-let
674 (setq ,result (progn ,@forms))
675 (setq ,@temp-sym-assign))
676 (let ((inhibit-quit gnus-atomic-be-safe))
677 (setq ,@sym-temp-assign))
680 (put 'gnus-atomic-progn-assign 'lisp-indent-function 1)
681 ;(put 'gnus-atomic-progn-assign 'edebug-form-spec '(sexp body))
683 (defmacro gnus-atomic-setq (&rest pairs)
684 "Similar to setq, except that the real symbols are only assigned when
685 there are no errors. And when the real symbols are assigned, they are
686 done so atomically. If other variables might be changed via side-effect,
687 see gnus-atomic-progn-assign. It is safe to use gnus-atomic-setq
688 with potentially long computations."
692 (push (car tpairs) syms)
693 (setq tpairs (cddr tpairs)))
694 `(gnus-atomic-progn-assign ,syms
697 ;(put 'gnus-atomic-setq 'edebug-form-spec '(body))
700 ;;; Functions for saving to babyl/mail files.
702 (defvar rmail-default-rmail-file)
703 (defun gnus-output-to-rmail (filename &optional ask)
704 "Append the current article to an Rmail file named FILENAME."
706 ;; Most of these codes are borrowed from rmailout.el.
707 (setq filename (expand-file-name filename))
708 (setq rmail-default-rmail-file filename)
709 (let ((artbuf (current-buffer))
710 (tmpbuf (get-buffer-create " *Gnus-output*")))
712 (or (get-file-buffer filename)
713 (file-exists-p filename)
716 (concat "\"" filename "\" does not exist, create it? ")))
717 (let ((file-buffer (create-file-buffer filename)))
719 (set-buffer file-buffer)
720 (rmail-insert-rmail-file-header)
721 (let ((require-final-newline nil))
722 (gnus-write-buffer filename)))
723 (kill-buffer file-buffer))
724 (error "Output file does not exist")))
727 (insert-buffer-substring artbuf)
728 (gnus-convert-article-to-rmail)
729 ;; Decide whether to append to a file or to an Emacs buffer.
730 (let ((outbuf (get-file-buffer filename)))
732 (append-to-file (point-min) (point-max) filename)
733 ;; File has been visited, in buffer OUTBUF.
735 (let ((buffer-read-only nil)
736 (msg (and (boundp 'rmail-current-message)
737 (symbol-value 'rmail-current-message))))
738 ;; If MSG is non-nil, buffer is in RMAIL mode.
741 (narrow-to-region (point-max) (point-max)))
742 (insert-buffer-substring tmpbuf)
744 (goto-char (point-min))
746 (search-backward "\n\^_")
747 (narrow-to-region (point) (point-max))
748 (rmail-count-new-messages t)
749 (when (rmail-summary-exists)
750 (rmail-select-summary
751 (rmail-update-summary)))
752 (rmail-count-new-messages t)
753 (rmail-show-message msg))
755 (kill-buffer tmpbuf)))
757 (defun gnus-output-to-mail (filename &optional ask)
758 "Append the current article to a mail file named FILENAME."
759 (setq filename (expand-file-name filename))
760 (let ((artbuf (current-buffer))
761 (tmpbuf (get-buffer-create " *Gnus-output*")))
763 ;; Create the file, if it doesn't exist.
764 (when (and (not (get-file-buffer filename))
765 (not (file-exists-p filename)))
768 (concat "\"" filename "\" does not exist, create it? ")))
769 (let ((file-buffer (create-file-buffer filename)))
771 (set-buffer file-buffer)
772 (let ((require-final-newline nil))
773 (gnus-write-buffer-as-binary filename)))
774 (kill-buffer file-buffer))
775 (error "Output file does not exist")))
778 (insert-buffer-substring artbuf)
779 (goto-char (point-min))
780 (if (looking-at "From ")
782 (insert "From nobody " (current-time-string) "\n"))
783 (let (case-fold-search)
784 (while (re-search-forward "^From " nil t)
787 ;; Decide whether to append to a file or to an Emacs buffer.
788 (let ((outbuf (get-file-buffer filename)))
790 (let ((buffer-read-only nil))
792 (goto-char (point-max))
794 (unless (looking-at "\n\n")
795 (goto-char (point-max))
799 (goto-char (point-max))
800 (write-region-as-binary (point-min) (point-max)
802 ;; File has been visited, in buffer OUTBUF.
804 (let ((buffer-read-only nil))
805 (goto-char (point-max))
809 (insert-buffer-substring tmpbuf)))))
810 (kill-buffer tmpbuf)))
812 (defun gnus-convert-article-to-rmail ()
813 "Convert article in current buffer to Rmail message format."
814 (let ((buffer-read-only nil))
815 ;; Convert article directly into Babyl format.
816 (goto-char (point-min))
817 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
818 (while (search-forward "\n\^_" nil t) ;single char
819 (replace-match "\n^_" t t)) ;2 chars: "^" and "_"
820 (goto-char (point-max))
823 (defun gnus-map-function (funs arg)
824 "Applies the result of the first function in FUNS to the second, and so on.
825 ARG is passed to the first function."
828 (setq arg (funcall (pop myfuns) arg)))
831 (defun gnus-run-hooks (&rest funcs)
832 "Does the same as `run-hooks', but saves excursion."
833 (let ((buf (current-buffer)))
835 (apply 'run-hooks funcs)
839 ;;; .netrc and .authinforc parsing
842 (defun gnus-parse-netrc (file)
843 "Parse FILE and return an list of all entries in the file."
844 (when (file-exists-p file)
846 (let ((tokens '("machine" "default" "login"
847 "password" "account" "macdef" "force"))
848 alist elem result pair)
849 (insert-file-contents file)
850 (goto-char (point-min))
851 ;; Go through the file, line by line.
853 (narrow-to-region (point) (gnus-point-at-eol))
854 ;; For each line, get the tokens and values.
856 (skip-chars-forward "\t ")
857 ;; Skip lines that begin with a "#".
858 (if (eq (char-after) ?#)
859 (goto-char (point-max))
861 (setq elem (buffer-substring
862 (point) (progn (skip-chars-forward "^\t ")
865 ((equal elem "macdef")
866 ;; We skip past the macro definition.
868 (while (and (zerop (forward-line 1))
870 (narrow-to-region (point) (point)))
871 ((member elem tokens)
872 ;; Tokens that don't have a following value are ignored,
874 (when (and pair (or (cdr pair)
875 (equal (car pair) "default")))
877 (setq pair (list elem)))
879 ;; Values that haven't got a preceding token are ignored.
883 (setq pair nil)))))))
885 (push (nreverse alist) result))
890 (nreverse result)))))
892 (defun gnus-netrc-machine (list machine)
893 "Return the netrc values from LIST for MACHINE or for the default entry."
896 (not (equal (cdr (assoc "machine" (car list))) machine)))
899 (progn (while (and rest (not (assoc "default" (car rest))))
903 (defun gnus-netrc-get (alist type)
904 "Return the value of token TYPE from ALIST."
905 (cdr (assoc type alist)))
909 (defvar gnus-group-buffer) ; Compiler directive
910 (defun gnus-alive-p ()
911 "Say whether Gnus is running or not."
912 (and (boundp 'gnus-group-buffer)
913 (get-buffer gnus-group-buffer)
915 (set-buffer gnus-group-buffer)
916 (eq major-mode 'gnus-group-mode))))
918 (defun gnus-remove-duplicates (list)
919 (let (new (tail list))
921 (or (member (car tail) new)
922 (setq new (cons (car tail) new)))
923 (setq tail (cdr tail)))
926 (defun gnus-delete-if (predicate list)
927 "Delete elements from LIST that satisfy PREDICATE."
930 (unless (funcall predicate (car list))
931 (push (car list) out))
935 (defun gnus-delete-alist (key alist)
936 "Delete all entries in ALIST that have a key eq to KEY."
938 (while (setq entry (assq key alist))
939 (setq alist (delq entry alist)))
942 (defmacro gnus-pull (key alist &optional assoc-p)
943 "Modify ALIST to be without KEY."
944 (unless (symbolp alist)
945 (error "Not a symbol: %s" alist))
946 (let ((fun (if assoc-p 'assoc 'assq)))
947 `(setq ,alist (delq (,fun ,key ,alist) ,alist))))
949 (defun gnus-globalify-regexp (re)
950 "Returns a regexp that matches a whole line, iff RE matches a part of it."
951 (concat (unless (string-match "^\\^" re) "^.*")
953 (unless (string-match "\\$$" re) ".*$")))
955 (defun gnus-set-window-start (&optional point)
956 "Set the window start to POINT, or (point) if nil."
957 (let ((win (get-buffer-window (current-buffer) t)))
959 (set-window-start win (or point (point))))))
961 (defun gnus-annotation-in-region-p (b e)
963 (eq (cadr (memq 'gnus-undeletable (text-properties-at b))) t)
964 (text-property-any b e 'gnus-undeletable t)))
966 (defun gnus-or (&rest elems)
967 "Return non-nil if any of the elements are non-nil."
973 (defun gnus-and (&rest elems)
974 "Return non-nil if all of the elements are non-nil."
981 (static-if (boundp 'MULE)
982 (defun gnus-write-active-file-as-coding-system (coding-system file hashtb)
983 (let ((output-coding-system coding-system))
990 (insert (format "%s %d %d y\n"
991 (symbol-name sym) (cdr (symbol-value sym))
992 (car (symbol-value sym))))))
994 (defun gnus-write-active-file-as-coding-system (coding-system file hashtb)
995 (let ((coding-system-for-write coding-system))
1002 (insert (format "%s %d %d y\n"
1003 (symbol-name sym) (cdr (symbol-value sym))
1004 (car (symbol-value sym))))))
1008 (provide 'gnus-util)
1010 ;;; gnus-util.el ends here