1 ;;; gnus-util.el --- utility functions for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
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
35 (eval-when-compile (require 'cl))
36 (eval-when-compile (require 'static))
44 (autoload 'rmail-insert-rmail-file-header "rmail")
45 (autoload 'rmail-count-new-messages "rmail")
46 (autoload 'rmail-show-message "rmail"))
48 (defun gnus-boundp (variable)
49 "Return non-nil if VARIABLE is bound and non-nil."
50 (and (boundp variable)
51 (symbol-value variable)))
53 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
54 "Pop to BUFFER, evaluate FORMS, and then return to the original window."
55 (let ((tempvar (make-symbol "GnusStartBufferWindow"))
57 (buf (make-symbol "buf"))
58 (frame (make-symbol "frame")))
59 `(let* ((,tempvar (selected-window))
61 (,w (get-buffer-window ,buf 'visible))
68 (set-buffer (window-buffer ,w)))
71 (setq ,frame (selected-frame))
72 (select-window ,tempvar)
73 (select-frame ,frame)))))
75 (put 'gnus-eval-in-buffer-window 'lisp-indent-function 1)
76 (put 'gnus-eval-in-buffer-window 'edebug-form-spec '(form body))
78 (defmacro gnus-intern-safe (string hashtable)
79 "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
80 `(let ((symbol (intern ,string ,hashtable)))
85 ;; Avoid byte-compile warning.
86 ;; In Mule, this function will be redefined to `truncate-string',
87 ;; which takes 3 or 4 args.
88 (defun gnus-truncate-string (str width &rest ignore)
89 (substring str 0 width))
91 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
92 ;; to limit the length of a string. This function is necessary since
93 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
94 (defsubst gnus-limit-string (str width)
95 (if (> (length str) width)
96 (substring str 0 width)
99 (defsubst gnus-functionp (form)
100 "Return non-nil if FORM is funcallable."
101 (or (and (symbolp form) (fboundp form))
102 (and (listp form) (eq (car form) 'lambda))
103 (byte-code-function-p form)))
105 (defsubst gnus-goto-char (point)
106 (and point (goto-char point)))
108 (defmacro gnus-buffer-exists-p (buffer)
109 `(let ((buffer ,buffer))
111 (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
114 (defmacro gnus-kill-buffer (buffer)
115 `(let ((buf ,buffer))
116 (when (gnus-buffer-exists-p buf)
120 ((fboundp 'point-at-bol)
121 (fset 'gnus-point-at-bol 'point-at-bol))
122 ((fboundp 'line-beginning-position)
123 (fset 'gnus-point-at-bol 'line-beginning-position))
125 (defun gnus-point-at-bol ()
126 "Return point at the beginning of the line."
134 ((fboundp 'point-at-eol)
135 (fset 'gnus-point-at-eol 'point-at-eol))
136 ((fboundp 'line-end-position)
137 (fset 'gnus-point-at-eol 'line-end-position))
139 (defun gnus-point-at-eol ()
140 "Return point at the end of the line."
148 (defun gnus-delete-first (elt list)
149 "Delete by side effect the first occurrence of ELT as a member of LIST."
150 (if (equal (car list) elt)
153 (while (and (cdr list)
154 (not (equal (cadr list) elt)))
155 (setq list (cdr list)))
157 (setcdr list (cddr list)))
160 ;; Delete the current line (and the next N lines).
161 (defmacro gnus-delete-line (&optional n)
162 `(delete-region (progn (beginning-of-line) (point))
163 (progn (forward-line ,(or n 1)) (point))))
165 (defun gnus-byte-code (func)
166 "Return a form that can be `eval'ed based on FUNC."
167 (let ((fval (indirect-function func)))
168 (if (byte-code-function-p fval)
169 (let ((flist (append fval nil)))
170 (setcar flist 'byte-code)
172 (cons 'progn (cddr fval)))))
174 (defun gnus-extract-address-components (from)
176 ;; First find the address - the thing with the @ in it. This may
177 ;; not be accurate in mail addresses, but does the trick most of
178 ;; the time in news messages.
179 (when (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
180 (setq address (substring from (match-beginning 0) (match-end 0))))
181 ;; Then we check whether the "name <address>" format is used.
183 ;; Linear white space is not required.
184 (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
185 (and (setq name (substring from 0 (match-beginning 0)))
186 ;; Strip any quotes from the name.
187 (string-match "\".*\"" name)
188 (setq name (substring name 1 (1- (match-end 0))))))
189 ;; If not, then "address (name)" is used.
191 (and (string-match "(.+)" from)
192 (setq name (substring from (1+ (match-beginning 0))
193 (1- (match-end 0)))))
194 (and (string-match "()" from)
196 ;; XOVER might not support folded From headers.
197 (and (string-match "(.*" from)
198 (setq name (substring from (1+ (match-beginning 0))
200 (list (if (string= name "") nil name) (or address from))))
203 (defun gnus-fetch-field (field)
204 "Return the value of the header FIELD of current article."
207 (let ((case-fold-search t)
208 (inhibit-point-motion-hooks t))
209 (nnheader-narrow-to-headers)
210 (message-fetch-field field)))))
212 (defun gnus-goto-colon ()
214 (search-forward ":" (gnus-point-at-eol) t))
216 (defun gnus-remove-text-with-property (prop)
217 "Delete all text in the current buffer with text property PROP."
219 (goto-char (point-min))
221 (while (get-text-property (point) prop)
223 (goto-char (next-single-property-change (point) prop nil (point-max))))))
225 (defun gnus-newsgroup-directory-form (newsgroup)
226 "Make hierarchical directory name from NEWSGROUP name."
227 (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
228 (len (length newsgroup))
230 ;; If this is a foreign group, we don't want to translate the
232 (if (setq idx (string-match ":" newsgroup))
233 (aset newsgroup idx ?/)
235 ;; Replace all occurrences of `.' with `/'.
237 (when (= (aref newsgroup idx) ?.)
238 (aset newsgroup idx ?/))
242 (defun gnus-newsgroup-savable-name (group)
243 ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
245 (nnheader-replace-chars-in-string group ?/ ?.))
247 (defun gnus-string> (s1 s2)
248 (not (or (string< s1 s2)
253 (defun gnus-file-newer-than (file date)
254 (let ((fdate (nth 5 (file-attributes file))))
255 (or (> (car fdate) (car date))
256 (and (= (car fdate) (car date))
257 (> (nth 1 fdate) (nth 1 date))))))
261 (defmacro gnus-local-set-keys (&rest plist)
262 "Set the keys in PLIST in the current keymap."
263 `(gnus-define-keys-1 (current-local-map) ',plist))
265 (defmacro gnus-define-keys (keymap &rest plist)
266 "Define all keys in PLIST in KEYMAP."
267 `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
269 (defmacro gnus-define-keys-safe (keymap &rest plist)
270 "Define all keys in PLIST in KEYMAP without overwriting previous definitions."
271 `(gnus-define-keys-1 (quote ,keymap) (quote ,plist) t))
273 (put 'gnus-define-keys 'lisp-indent-function 1)
274 (put 'gnus-define-keys-safe 'lisp-indent-function 1)
275 (put 'gnus-local-set-keys 'lisp-indent-function 1)
277 (defmacro gnus-define-keymap (keymap &rest plist)
278 "Define all keys in PLIST in KEYMAP."
279 `(gnus-define-keys-1 ,keymap (quote ,plist)))
281 (put 'gnus-define-keymap 'lisp-indent-function 1)
283 (defun gnus-define-keys-1 (keymap plist &optional safe)
285 (error "Can't set keys in a null keymap"))
286 (cond ((symbolp keymap)
287 (setq keymap (symbol-value keymap)))
290 (set (car keymap) nil)
291 (define-prefix-command (car keymap))
292 (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
293 (setq keymap (symbol-value (car keymap)))))
296 (when (symbolp (setq key (pop plist)))
297 (setq key (symbol-value key)))
299 (eq (lookup-key keymap key) 'undefined))
300 (define-key keymap key (pop plist))
303 (defun gnus-completing-read (default prompt &rest args)
304 ;; Like `completing-read', except that DEFAULT is the default argument.
305 (let* ((prompt (if default
306 (concat prompt " (default " default ") ")
307 (concat prompt " ")))
308 (answer (apply 'completing-read prompt args)))
309 (if (or (null answer) (zerop (length answer)))
313 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
315 (defun gnus-y-or-n-p (prompt)
320 (defun gnus-yes-or-no-p (prompt)
325 (defun gnus-dd-mmm (messy-date)
326 "Return a string like DD-MMM from a big messy string."
328 (format-time-string "%d-%b" (safe-date-to-time messy-date))
331 (defmacro gnus-date-get-time (date)
332 "Convert DATE string to Emacs time.
333 Cache the result as a text property stored in DATE."
334 ;; Either return the cached value...
338 (or (get-text-property 0 'gnus-time d)
339 ;; or compute the value...
340 (let ((time (safe-date-to-time d)))
341 ;; and store it back in the string.
342 (put-text-property 0 1 'gnus-time time d)
345 (defsubst gnus-time-iso8601 (time)
346 "Return a string of TIME in YYMMDDTHHMMSS format."
347 (format-time-string "%Y%m%dT%H%M%S" time))
349 (defun gnus-date-iso8601 (date)
350 "Convert the DATE to YYMMDDTHHMMSS."
352 (gnus-time-iso8601 (gnus-date-get-time date))
355 (defun gnus-mode-string-quote (string)
356 "Quote all \"%\"'s in STRING."
358 (gnus-set-work-buffer)
360 (goto-char (point-min))
361 (while (search-forward "%" nil t)
365 ;; Make a hash table (default and minimum size is 256).
366 ;; Optional argument HASHSIZE specifies the table size.
367 (defun gnus-make-hashtable (&optional hashsize)
368 (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 256) 256) 0))
370 ;; Make a number that is suitable for hashing; bigger than MIN and
371 ;; equal to some 2^x. Many machines (such as sparcs) do not have a
372 ;; hardware modulo operation, so they implement it in software. On
373 ;; many sparcs over 50% of the time to intern is spent in the modulo.
374 ;; Yes, it's slower than actually computing the hash from the string!
375 ;; So we use powers of 2 so people can optimize the modulo to a mask.
376 (defun gnus-create-hash-size (min)
382 (defcustom gnus-verbose 7
383 "*Integer that says how verbose Gnus should be.
384 The higher the number, the more messages Gnus will flash to say what
385 it's doing. At zero, Gnus will be totally mute; at five, Gnus will
386 display most important messages; and at ten, Gnus will keep on
387 jabbering all the time."
391 ;; Show message if message has a lower level than `gnus-verbose'.
392 ;; Guideline for numbers:
393 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
394 ;; for things that take a long time, 7 - not very important messages
395 ;; on stuff, 9 - messages inside loops.
396 (defun gnus-message (level &rest args)
397 (if (<= level gnus-verbose)
398 (apply 'message args)
399 ;; We have to do this format thingy here even if the result isn't
400 ;; shown - the return value has to be the same as the return value
402 (apply 'format args)))
404 (defun gnus-error (level &rest args)
405 "Beep an error if LEVEL is equal to or less than `gnus-verbose'."
406 (when (<= (floor level) gnus-verbose)
407 (apply 'message args)
410 (when (and (floatp level)
411 (not (zerop (setq duration (* 10 (- level (floor level)))))))
412 (sit-for duration))))
415 (defun gnus-split-references (references)
416 "Return a list of Message-IDs in REFERENCES."
419 (while (string-match "<[^>]+>" references beg)
420 (push (substring references (match-beginning 0) (setq beg (match-end 0)))
424 (defsubst gnus-parent-id (references &optional n)
425 "Return the last Message-ID in REFERENCES.
426 If N, return the Nth ancestor instead."
428 (let ((ids (inline (gnus-split-references references))))
429 (while (nthcdr (or n 1) ids)
430 (setq ids (cdr ids)))
433 (defsubst gnus-buffer-live-p (buffer)
434 "Say whether BUFFER is alive or not."
437 (buffer-name (get-buffer buffer))))
439 (defun gnus-horizontal-recenter ()
440 "Recenter the current buffer horizontally."
441 (if (< (current-column) (/ (window-width) 2))
442 (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
443 (let* ((orig (point))
444 (end (window-end (get-buffer-window (current-buffer) t)))
447 ;; Find the longest line currently displayed in the window.
448 (goto-char (window-start))
449 (while (and (not (eobp))
452 (setq max (max max (current-column)))
455 ;; Scroll horizontally to center (sort of) the point.
456 (if (> max (window-width))
458 (get-buffer-window (current-buffer) t)
459 (min (- (current-column) (/ (window-width) 3))
460 (+ 2 (- max (window-width)))))
461 (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
464 (defun gnus-read-event-char ()
465 "Get the next event."
466 (let ((event (read-event)))
467 ;; should be gnus-characterp, but this can't be called in XEmacs anyway
468 (cons (and (numberp event) event) event)))
470 (defun gnus-sortable-date (date)
471 "Make string suitable for sorting from DATE."
472 (gnus-time-iso8601 (date-to-time date)))
474 (defun gnus-copy-file (file &optional to)
477 (list (read-file-name "Copy file: " default-directory)
478 (read-file-name "Copy file to: " default-directory)))
480 (setq to (read-file-name "Copy file to: " default-directory)))
481 (when (file-directory-p to)
482 (setq to (concat (file-name-as-directory to)
483 (file-name-nondirectory file))))
486 (defun gnus-kill-all-overlays ()
487 "Delete all overlays in the current buffer."
488 (let* ((overlayss (overlay-lists))
489 (buffer-read-only nil)
490 (overlays (delq nil (nconc (car overlayss) (cdr overlayss)))))
492 (delete-overlay (pop overlays)))))
494 (defvar gnus-work-buffer " *gnus work*")
496 (defun gnus-set-work-buffer ()
497 "Put point in the empty Gnus work buffer."
498 (if (get-buffer gnus-work-buffer)
500 (set-buffer gnus-work-buffer)
502 (set-buffer (gnus-get-buffer-create gnus-work-buffer))
503 (kill-all-local-variables)))
505 (defmacro gnus-group-real-name (group)
506 "Find the real name of a foreign newsgroup."
507 `(let ((gname ,group))
508 (if (string-match "^[^:]+:" gname)
509 (substring gname (match-end 0))
512 (defun gnus-make-sort-function (funs)
513 "Return a composite sort condition based on the functions in FUNC."
515 ;; Just a simple function.
516 ((gnus-functionp funs) funs)
517 ;; No functions at all.
519 ;; A list of functions.
523 ,(gnus-make-sort-function-1 (reverse funs))))
524 ;; A list containing just one function.
528 (defun gnus-make-sort-function-1 (funs)
529 "Return a composite sort condition based on the functions in FUNC."
530 (let ((function (car funs))
533 (when (consp function)
536 ((eq (car function) 'not)
537 (setq function (cadr function)
540 ((gnus-functionp function)
544 (error "Invalid sort spec: %s" function))))
546 `(or (,function ,first ,last)
547 (and (not (,function ,last ,first))
548 ,(gnus-make-sort-function-1 (cdr funs))))
549 `(,function ,first ,last))))
551 (defun gnus-turn-off-edit-menu (type)
552 "Turn off edit menu in `gnus-TYPE-mode-map'."
553 (define-key (symbol-value (intern (format "gnus-%s-mode-map" type)))
554 [menu-bar edit] 'undefined))
556 (defun gnus-prin1 (form)
557 "Use `prin1' on FORM in the current buffer.
558 Bind `print-quoted' and `print-readably' to t while printing."
559 (let ((print-quoted t)
561 (print-escape-multibyte nil)
562 print-level print-length)
563 (prin1 form (current-buffer))))
565 (defun gnus-prin1-to-string (form)
566 "The same as `prin1', but bind `print-quoted' and `print-readably' to t."
567 (let ((print-quoted t)
569 (prin1-to-string form)))
571 (defun gnus-make-directory (directory)
572 "Make DIRECTORY (and all its parents) if it doesn't exist."
574 (not (file-exists-p directory)))
575 (make-directory directory t))
578 (defun gnus-write-buffer (file)
579 "Write the current buffer's contents to FILE."
580 ;; Make sure the directory exists.
581 (gnus-make-directory (file-name-directory file))
583 (write-region (point-min) (point-max) file nil 'quietly))
585 (defun gnus-write-buffer-as-binary (file)
586 "Write the current buffer's contents to FILE without code conversion."
587 ;; Make sure the directory exists.
588 (gnus-make-directory (file-name-directory file))
590 (write-region-as-binary (point-min) (point-max) file nil 'quietly))
592 (defun gnus-write-buffer-as-coding-system (coding-system file)
593 "Write the current buffer's contents to FILE with code conversion."
594 ;; Make sure the directory exists.
595 (gnus-make-directory (file-name-directory file))
597 (write-region-as-coding-system
598 coding-system (point-min) (point-max) file nil 'quietly))
600 (defun gnus-delete-file (file)
601 "Delete FILE if it exists."
602 (when (file-exists-p file)
605 (defun gnus-strip-whitespace (string)
606 "Return STRING stripped of all whitespace."
607 (while (string-match "[\r\n\t ]+" string)
608 (setq string (replace-match "" t t string)))
611 (defsubst gnus-put-text-property-excluding-newlines (beg end prop val)
612 "The same as `put-text-property', but don't put this prop on any newlines in the region."
617 (while (re-search-forward "[ \t]+\\|[ \t]*\n" end 'move)
618 (gnus-put-text-property beg (match-beginning 0) prop val)
620 (gnus-put-text-property beg (point) prop val)))))
622 (defun gnus-put-text-property-excluding-characters-with-faces (beg end
624 "The same as `put-text-property', but don't put props on characters with the `gnus-face' property."
627 (when (get-text-property b 'gnus-face)
628 (setq b (next-single-property-change b 'gnus-face nil end)))
630 (gnus-put-text-property
631 b (setq b (next-single-property-change b 'gnus-face nil end))
634 ;;; Protected and atomic operations. dmoore@ucsd.edu 21.11.1996
635 ;;; The primary idea here is to try to protect internal datastructures
636 ;;; from becoming corrupted when the user hits C-g, or if a hook or
637 ;;; similar blows up. Often in Gnus multiple tables/lists need to be
638 ;;; updated at the same time, or information can be lost.
640 (defvar gnus-atomic-be-safe t
641 "If t, certain operations will be protected from interruption by C-g.")
643 (defmacro gnus-atomic-progn (&rest forms)
644 "Evaluate FORMS atomically, which means to protect the evaluation
645 from being interrupted by the user. An error from the forms themselves
646 will return without finishing the operation. Since interrupts from
647 the user are disabled, it is recommended that only the most minimal
648 operations are performed by FORMS. If you wish to assign many
649 complicated values atomically, compute the results into temporary
650 variables and then do only the assignment atomically."
651 `(let ((inhibit-quit gnus-atomic-be-safe))
654 (put 'gnus-atomic-progn 'lisp-indent-function 0)
656 (defmacro gnus-atomic-progn-assign (protect &rest forms)
657 "Evaluate FORMS, but insure that the variables listed in PROTECT
658 are not changed if anything in FORMS signals an error or otherwise
659 non-locally exits. The variables listed in PROTECT are updated atomically.
660 It is safe to use gnus-atomic-progn-assign with long computations.
662 Note that if any of the symbols in PROTECT were unbound, they will be
663 set to nil on a sucessful assignment. In case of an error or other
664 non-local exit, it will still be unbound."
665 (let* ((temp-sym-map (mapcar (lambda (x) (list (make-symbol
666 (concat (symbol-name x)
670 (sym-temp-map (mapcar (lambda (x) (list (cadr x) (car x)))
672 (temp-sym-let (mapcar (lambda (x) (list (car x)
673 `(and (boundp ',(cadr x))
676 (sym-temp-let sym-temp-map)
677 (temp-sym-assign (apply 'append temp-sym-map))
678 (sym-temp-assign (apply 'append sym-temp-map))
679 (result (make-symbol "result-tmp")))
680 `(let (,@temp-sym-let
683 (setq ,result (progn ,@forms))
684 (setq ,@temp-sym-assign))
685 (let ((inhibit-quit gnus-atomic-be-safe))
686 (setq ,@sym-temp-assign))
689 (put 'gnus-atomic-progn-assign 'lisp-indent-function 1)
690 ;(put 'gnus-atomic-progn-assign 'edebug-form-spec '(sexp body))
692 (defmacro gnus-atomic-setq (&rest pairs)
693 "Similar to setq, except that the real symbols are only assigned when
694 there are no errors. And when the real symbols are assigned, they are
695 done so atomically. If other variables might be changed via side-effect,
696 see gnus-atomic-progn-assign. It is safe to use gnus-atomic-setq
697 with potentially long computations."
701 (push (car tpairs) syms)
702 (setq tpairs (cddr tpairs)))
703 `(gnus-atomic-progn-assign ,syms
706 ;(put 'gnus-atomic-setq 'edebug-form-spec '(body))
709 ;;; Functions for saving to babyl/mail files.
711 (defvar rmail-default-rmail-file)
712 (defun gnus-output-to-rmail (filename &optional ask)
713 "Append the current article to an Rmail file named FILENAME."
715 ;; Most of these codes are borrowed from rmailout.el.
716 (setq filename (expand-file-name filename))
717 (setq rmail-default-rmail-file filename)
718 (let ((artbuf (current-buffer))
719 (tmpbuf (get-buffer-create " *Gnus-output*")))
721 (or (get-file-buffer filename)
722 (file-exists-p filename)
725 (concat "\"" filename "\" does not exist, create it? ")))
726 (let ((file-buffer (create-file-buffer filename)))
728 (set-buffer file-buffer)
729 (rmail-insert-rmail-file-header)
730 (let ((require-final-newline nil))
731 (gnus-write-buffer filename)))
732 (kill-buffer file-buffer))
733 (error "Output file does not exist")))
736 (insert-buffer-substring artbuf)
737 (gnus-convert-article-to-rmail)
738 ;; Decide whether to append to a file or to an Emacs buffer.
739 (let ((outbuf (get-file-buffer filename)))
741 (write-region-as-binary (point-min) (point-max) filename 'append)
742 ;; File has been visited, in buffer OUTBUF.
744 (let ((buffer-read-only nil)
745 (msg (and (boundp 'rmail-current-message)
746 (symbol-value 'rmail-current-message))))
747 ;; If MSG is non-nil, buffer is in RMAIL mode.
750 (narrow-to-region (point-max) (point-max)))
751 (insert-buffer-substring tmpbuf)
753 (goto-char (point-min))
755 (search-backward "\n\^_")
756 (narrow-to-region (point) (point-max))
757 (rmail-count-new-messages t)
758 (when (rmail-summary-exists)
759 (rmail-select-summary
760 (rmail-update-summary)))
761 (rmail-count-new-messages t)
762 (rmail-show-message msg))
764 (kill-buffer tmpbuf)))
766 (defun gnus-output-to-mail (filename &optional ask)
767 "Append the current article to a mail file named FILENAME."
768 (setq filename (expand-file-name filename))
769 (let ((artbuf (current-buffer))
770 (tmpbuf (get-buffer-create " *Gnus-output*")))
772 ;; Create the file, if it doesn't exist.
773 (when (and (not (get-file-buffer filename))
774 (not (file-exists-p filename)))
777 (concat "\"" filename "\" does not exist, create it? ")))
778 (let ((file-buffer (create-file-buffer filename)))
780 (set-buffer file-buffer)
781 (let ((require-final-newline nil))
782 (gnus-write-buffer-as-binary filename)))
783 (kill-buffer file-buffer))
784 (error "Output file does not exist")))
787 (insert-buffer-substring artbuf)
788 (goto-char (point-min))
789 (if (looking-at "From ")
791 (insert "From nobody " (current-time-string) "\n"))
792 (let (case-fold-search)
793 (while (re-search-forward "^From " nil t)
796 ;; Decide whether to append to a file or to an Emacs buffer.
797 (let ((outbuf (get-file-buffer filename)))
799 (let ((buffer-read-only nil))
801 (goto-char (point-max))
803 (unless (looking-at "\n\n")
804 (goto-char (point-max))
808 (goto-char (point-max))
809 (write-region-as-binary (point-min) (point-max)
811 ;; File has been visited, in buffer OUTBUF.
813 (let ((buffer-read-only nil))
814 (goto-char (point-max))
818 (insert-buffer-substring tmpbuf)))))
819 (kill-buffer tmpbuf)))
821 (defun gnus-convert-article-to-rmail ()
822 "Convert article in current buffer to Rmail message format."
823 (let ((buffer-read-only nil))
824 ;; Convert article directly into Babyl format.
825 (goto-char (point-min))
826 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
827 (while (search-forward "\n\^_" nil t) ;single char
828 (replace-match "\n^_" t t)) ;2 chars: "^" and "_"
829 (goto-char (point-max))
832 (defun gnus-map-function (funs arg)
833 "Applies the result of the first function in FUNS to the second, and so on.
834 ARG is passed to the first function."
837 (setq arg (funcall (pop myfuns) arg)))
840 (defun gnus-run-hooks (&rest funcs)
841 "Does the same as `run-hooks', but saves excursion."
842 (let ((buf (current-buffer)))
844 (apply 'run-hooks funcs)
848 ;;; .netrc and .authinforc parsing
851 (defun gnus-parse-netrc (file)
852 "Parse FILE and return an list of all entries in the file."
853 (when (file-exists-p file)
855 (let ((tokens '("machine" "default" "login"
856 "password" "account" "macdef" "force"
858 alist elem result pair)
859 (insert-file-contents file)
860 (goto-char (point-min))
861 ;; Go through the file, line by line.
863 (narrow-to-region (point) (gnus-point-at-eol))
864 ;; For each line, get the tokens and values.
866 (skip-chars-forward "\t ")
867 ;; Skip lines that begin with a "#".
868 (if (eq (char-after) ?#)
869 (goto-char (point-max))
872 (if (= (following-char) ?\")
873 (read (current-buffer))
875 (point) (progn (skip-chars-forward "^\t ")
878 ((equal elem "macdef")
879 ;; We skip past the macro definition.
881 (while (and (zerop (forward-line 1))
883 (narrow-to-region (point) (point)))
884 ((member elem tokens)
885 ;; Tokens that don't have a following value are ignored,
887 (when (and pair (or (cdr pair)
888 (equal (car pair) "default")))
890 (setq pair (list elem)))
892 ;; Values that haven't got a preceding token are ignored.
896 (setq pair nil)))))))
898 (push (nreverse alist) result))
903 (nreverse result)))))
905 (defun gnus-netrc-machine (list machine &optional port)
906 "Return the netrc values from LIST for MACHINE or for the default entry."
910 (when (equal (cdr (assoc "machine" (car list))) machine)
911 (push (car list) result))
914 ;; No machine name matches, so we look for default entries.
916 (when (assoc "default" (car rest))
917 (push (car rest) result))
920 (setq result (nreverse result))
922 (not (equal (or port "nntp")
923 (or (gnus-netrc-get (car result) "port")
928 (defun gnus-netrc-get (alist type)
929 "Return the value of token TYPE from ALIST."
930 (cdr (assoc type alist)))
934 (defvar gnus-group-buffer) ; Compiler directive
935 (defun gnus-alive-p ()
936 "Say whether Gnus is running or not."
937 (and (boundp 'gnus-group-buffer)
938 (get-buffer gnus-group-buffer)
940 (set-buffer gnus-group-buffer)
941 (eq major-mode 'gnus-group-mode))))
943 (defun gnus-remove-duplicates (list)
944 (let (new (tail list))
946 (or (member (car tail) new)
947 (setq new (cons (car tail) new)))
948 (setq tail (cdr tail)))
951 (defun gnus-delete-if (predicate list)
952 "Delete elements from LIST that satisfy PREDICATE."
955 (unless (funcall predicate (car list))
956 (push (car list) out))
960 (defun gnus-delete-alist (key alist)
961 "Delete all entries in ALIST that have a key eq to KEY."
963 (while (setq entry (assq key alist))
964 (setq alist (delq entry alist)))
967 (defmacro gnus-pull (key alist &optional assoc-p)
968 "Modify ALIST to be without KEY."
969 (unless (symbolp alist)
970 (error "Not a symbol: %s" alist))
971 (let ((fun (if assoc-p 'assoc 'assq)))
972 `(setq ,alist (delq (,fun ,key ,alist) ,alist))))
974 (defun gnus-globalify-regexp (re)
975 "Returns a regexp that matches a whole line, iff RE matches a part of it."
976 (concat (unless (string-match "^\\^" re) "^.*")
978 (unless (string-match "\\$$" re) ".*$")))
980 (defun gnus-set-window-start (&optional point)
981 "Set the window start to POINT, or (point) if nil."
982 (let ((win (get-buffer-window (current-buffer) t)))
984 (set-window-start win (or point (point))))))
986 (defun gnus-annotation-in-region-p (b e)
988 (eq (cadr (memq 'gnus-undeletable (text-properties-at b))) t)
989 (text-property-any b e 'gnus-undeletable t)))
991 (defun gnus-or (&rest elems)
992 "Return non-nil if any of the elements are non-nil."
998 (defun gnus-and (&rest elems)
999 "Return non-nil if all of the elements are non-nil."
1003 (throw 'found nil)))
1006 (defun gnus-write-active-file-as-coding-system (coding-system file hashtb
1009 (let ((output-coding-system coding-system)
1010 (coding-system-for-write coding-system))
1011 (with-temp-file file
1017 (insert (format "%S %d %d y\n"
1020 (intern (gnus-group-real-name (symbol-name sym))))
1021 (or (cdr (symbol-value sym))
1022 (car (symbol-value sym)))
1023 (car (symbol-value sym))))))
1025 (goto-char (point-max))
1026 (while (search-backward "\\." nil t)
1029 (provide 'gnus-util)
1031 ;;; gnus-util.el ends here