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))
43 (autoload 'message-fetch-field "message")
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 (defalias 'gnus-point-at-bol 'point-at-bol))
122 ((fboundp 'line-beginning-position)
123 (defalias '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 (defalias 'gnus-point-at-eol 'point-at-eol))
136 ((fboundp 'line-end-position)
137 (defalias '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))))))
226 (defun gnus-newsgroup-directory-form (newsgroup)
227 "Make hierarchical directory name from NEWSGROUP name."
228 (let* ((newsgroup (gnus-newsgroup-savable-name newsgroup))
229 (idx (string-match ":" newsgroup)))
231 (if idx (substring newsgroup 0 idx))
233 (nnheader-replace-chars-in-string
234 (if idx (substring newsgroup (1+ idx)) newsgroup)
237 (defun gnus-newsgroup-savable-name (group)
238 ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
240 (nnheader-replace-chars-in-string group ?/ ?.))
242 (defun gnus-string> (s1 s2)
243 (not (or (string< s1 s2)
248 (defun gnus-file-newer-than (file date)
249 (let ((fdate (nth 5 (file-attributes file))))
250 (or (> (car fdate) (car date))
251 (and (= (car fdate) (car date))
252 (> (nth 1 fdate) (nth 1 date))))))
256 (defmacro gnus-local-set-keys (&rest plist)
257 "Set the keys in PLIST in the current keymap."
258 `(gnus-define-keys-1 (current-local-map) ',plist))
260 (defmacro gnus-define-keys (keymap &rest plist)
261 "Define all keys in PLIST in KEYMAP."
262 `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
264 (defmacro gnus-define-keys-safe (keymap &rest plist)
265 "Define all keys in PLIST in KEYMAP without overwriting previous definitions."
266 `(gnus-define-keys-1 (quote ,keymap) (quote ,plist) t))
268 (put 'gnus-define-keys 'lisp-indent-function 1)
269 (put 'gnus-define-keys-safe 'lisp-indent-function 1)
270 (put 'gnus-local-set-keys 'lisp-indent-function 1)
272 (defmacro gnus-define-keymap (keymap &rest plist)
273 "Define all keys in PLIST in KEYMAP."
274 `(gnus-define-keys-1 ,keymap (quote ,plist)))
276 (put 'gnus-define-keymap 'lisp-indent-function 1)
278 (defun gnus-define-keys-1 (keymap plist &optional safe)
280 (error "Can't set keys in a null keymap"))
281 (cond ((symbolp keymap)
282 (setq keymap (symbol-value keymap)))
285 (set (car keymap) nil)
286 (define-prefix-command (car keymap))
287 (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
288 (setq keymap (symbol-value (car keymap)))))
291 (when (symbolp (setq key (pop plist)))
292 (setq key (symbol-value key)))
294 (eq (lookup-key keymap key) 'undefined))
295 (define-key keymap key (pop plist))
298 (defun gnus-completing-read (default prompt &rest args)
299 ;; Like `completing-read', except that DEFAULT is the default argument.
300 (let* ((prompt (if default
301 (concat prompt " (default " default ") ")
302 (concat prompt " ")))
303 (answer (apply 'completing-read prompt args)))
304 (if (or (null answer) (zerop (length answer)))
308 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
310 (defun gnus-y-or-n-p (prompt)
315 (defun gnus-yes-or-no-p (prompt)
320 (defun gnus-dd-mmm (messy-date)
321 "Return a string like DD-MMM from a big messy string."
323 (format-time-string "%d-%b" (safe-date-to-time messy-date))
326 (defmacro gnus-date-get-time (date)
327 "Convert DATE string to Emacs time.
328 Cache the result as a text property stored in DATE."
329 ;; Either return the cached value...
333 (or (get-text-property 0 'gnus-time d)
334 ;; or compute the value...
335 (let ((time (safe-date-to-time d)))
336 ;; and store it back in the string.
337 (put-text-property 0 1 'gnus-time time d)
340 (defsubst gnus-time-iso8601 (time)
341 "Return a string of TIME in YYYYMMDDTHHMMSS format."
342 (format-time-string "%Y%m%dT%H%M%S" time))
344 (defun gnus-date-iso8601 (date)
345 "Convert the DATE to YYYYMMDDTHHMMSS."
347 (gnus-time-iso8601 (gnus-date-get-time date))
350 (defun gnus-mode-string-quote (string)
351 "Quote all \"%\"'s in STRING."
353 (gnus-set-work-buffer)
355 (goto-char (point-min))
356 (while (search-forward "%" nil t)
360 ;; Make a hash table (default and minimum size is 256).
361 ;; Optional argument HASHSIZE specifies the table size.
362 (defun gnus-make-hashtable (&optional hashsize)
363 (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 256) 256) 0))
365 ;; Make a number that is suitable for hashing; bigger than MIN and
366 ;; equal to some 2^x. Many machines (such as sparcs) do not have a
367 ;; hardware modulo operation, so they implement it in software. On
368 ;; many sparcs over 50% of the time to intern is spent in the modulo.
369 ;; Yes, it's slower than actually computing the hash from the string!
370 ;; So we use powers of 2 so people can optimize the modulo to a mask.
371 (defun gnus-create-hash-size (min)
377 (defcustom gnus-verbose 7
378 "*Integer that says how verbose Gnus should be.
379 The higher the number, the more messages Gnus will flash to say what
380 it's doing. At zero, Gnus will be totally mute; at five, Gnus will
381 display most important messages; and at ten, Gnus will keep on
382 jabbering all the time."
386 ;; Show message if message has a lower level than `gnus-verbose'.
387 ;; Guideline for numbers:
388 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
389 ;; for things that take a long time, 7 - not very important messages
390 ;; on stuff, 9 - messages inside loops.
391 (defun gnus-message (level &rest args)
392 (if (<= level gnus-verbose)
393 (apply 'message args)
394 ;; We have to do this format thingy here even if the result isn't
395 ;; shown - the return value has to be the same as the return value
397 (apply 'format args)))
399 (defun gnus-error (level &rest args)
400 "Beep an error if LEVEL is equal to or less than `gnus-verbose'."
401 (when (<= (floor level) gnus-verbose)
402 (apply 'message args)
405 (when (and (floatp level)
406 (not (zerop (setq duration (* 10 (- level (floor level)))))))
407 (sit-for duration))))
410 (defun gnus-split-references (references)
411 "Return a list of Message-IDs in REFERENCES."
414 (while (string-match "<[^>]+>" references beg)
415 (push (substring references (match-beginning 0) (setq beg (match-end 0)))
419 (defsubst gnus-parent-id (references &optional n)
420 "Return the last Message-ID in REFERENCES.
421 If N, return the Nth ancestor instead."
423 (let ((ids (inline (gnus-split-references references))))
424 (while (nthcdr (or n 1) ids)
425 (setq ids (cdr ids)))
428 (defsubst gnus-buffer-live-p (buffer)
429 "Say whether BUFFER is alive or not."
432 (buffer-name (get-buffer buffer))))
434 (defun gnus-horizontal-recenter ()
435 "Recenter the current buffer horizontally."
436 (if (< (current-column) (/ (window-width) 2))
437 (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
438 (let* ((orig (point))
439 (end (window-end (get-buffer-window (current-buffer) t)))
442 ;; Find the longest line currently displayed in the window.
443 (goto-char (window-start))
444 (while (and (not (eobp))
447 (setq max (max max (current-column)))
450 ;; Scroll horizontally to center (sort of) the point.
451 (if (> max (window-width))
453 (get-buffer-window (current-buffer) t)
454 (min (- (current-column) (/ (window-width) 3))
455 (+ 2 (- max (window-width)))))
456 (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
459 (defun gnus-read-event-char ()
460 "Get the next event."
461 (let ((event (read-event)))
462 ;; should be gnus-characterp, but this can't be called in XEmacs anyway
463 (cons (and (numberp event) event) event)))
465 (defun gnus-sortable-date (date)
466 "Make string suitable for sorting from DATE."
467 (gnus-time-iso8601 (date-to-time date)))
469 (defun gnus-copy-file (file &optional to)
472 (list (read-file-name "Copy file: " default-directory)
473 (read-file-name "Copy file to: " default-directory)))
475 (setq to (read-file-name "Copy file to: " default-directory)))
476 (when (file-directory-p to)
477 (setq to (concat (file-name-as-directory to)
478 (file-name-nondirectory file))))
481 (defvar gnus-work-buffer " *gnus work*")
483 (defun gnus-set-work-buffer ()
484 "Put point in the empty Gnus work buffer."
485 (if (get-buffer gnus-work-buffer)
487 (set-buffer gnus-work-buffer)
489 (set-buffer (gnus-get-buffer-create gnus-work-buffer))
490 (kill-all-local-variables)))
492 (defmacro gnus-group-real-name (group)
493 "Find the real name of a foreign newsgroup."
494 `(let ((gname ,group))
495 (if (string-match "^[^:]+:" gname)
496 (substring gname (match-end 0))
499 (defun gnus-make-sort-function (funs)
500 "Return a composite sort condition based on the functions in FUNC."
502 ;; Just a simple function.
503 ((gnus-functionp funs) funs)
504 ;; No functions at all.
506 ;; A list of functions.
510 ,(gnus-make-sort-function-1 (reverse funs))))
511 ;; A list containing just one function.
515 (defun gnus-make-sort-function-1 (funs)
516 "Return a composite sort condition based on the functions in FUNC."
517 (let ((function (car funs))
520 (when (consp function)
523 ((eq (car function) 'not)
524 (setq function (cadr function)
527 ((gnus-functionp function)
531 (error "Invalid sort spec: %s" function))))
533 `(or (,function ,first ,last)
534 (and (not (,function ,last ,first))
535 ,(gnus-make-sort-function-1 (cdr funs))))
536 `(,function ,first ,last))))
538 (defun gnus-turn-off-edit-menu (type)
539 "Turn off edit menu in `gnus-TYPE-mode-map'."
540 (define-key (symbol-value (intern (format "gnus-%s-mode-map" type)))
541 [menu-bar edit] 'undefined))
543 (defun gnus-prin1 (form)
544 "Use `prin1' on FORM in the current buffer.
545 Bind `print-quoted' and `print-readably' to t while printing."
546 (let ((print-quoted t)
548 (print-escape-multibyte nil)
549 print-level print-length)
550 (prin1 form (current-buffer))))
552 (defun gnus-prin1-to-string (form)
553 "The same as `prin1', but bind `print-quoted' and `print-readably' to t."
554 (let ((print-quoted t)
556 (prin1-to-string form)))
558 (defun gnus-make-directory (directory)
559 "Make DIRECTORY (and all its parents) if it doesn't exist."
560 (let ((file-name-coding-system nnmail-pathname-coding-system)
561 (pathname-coding-system nnmail-pathname-coding-system))
563 (not (file-exists-p directory)))
564 (make-directory directory t)))
567 (defun gnus-write-buffer (file)
568 "Write the current buffer's contents to FILE."
569 ;; Make sure the directory exists.
570 (gnus-make-directory (file-name-directory file))
571 (let ((file-name-coding-system nnmail-pathname-coding-system)
572 (pathname-coding-system nnmail-pathname-coding-system))
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 gnus-emphasize-whitespace-regexp 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-as-coding-system
723 nnheader-text-coding-system filename)))
724 (kill-buffer file-buffer))
725 (error "Output file does not exist")))
728 (insert-buffer-substring artbuf)
729 (gnus-convert-article-to-rmail)
730 ;; Decide whether to append to a file or to an Emacs buffer.
731 (let ((outbuf (get-file-buffer filename)))
733 (write-region-as-binary (point-min) (point-max) filename 'append)
734 ;; File has been visited, in buffer OUTBUF.
736 (let ((buffer-read-only nil)
737 (msg (and (boundp 'rmail-current-message)
738 (symbol-value 'rmail-current-message))))
739 ;; If MSG is non-nil, buffer is in RMAIL mode.
742 (narrow-to-region (point-max) (point-max)))
743 (insert-buffer-substring tmpbuf)
745 (goto-char (point-min))
747 (search-backward "\n\^_")
748 (narrow-to-region (point) (point-max))
749 (rmail-count-new-messages t)
750 (when (rmail-summary-exists)
751 (rmail-select-summary
752 (rmail-update-summary)))
753 (rmail-count-new-messages t)
754 (rmail-show-message msg))
756 (kill-buffer tmpbuf)))
758 (defun gnus-output-to-mail (filename &optional ask)
759 "Append the current article to a mail file named FILENAME."
760 (setq filename (expand-file-name filename))
761 (let ((artbuf (current-buffer))
762 (tmpbuf (get-buffer-create " *Gnus-output*")))
764 ;; Create the file, if it doesn't exist.
765 (when (and (not (get-file-buffer filename))
766 (not (file-exists-p filename)))
769 (concat "\"" filename "\" does not exist, create it? ")))
770 (let ((file-buffer (create-file-buffer filename)))
772 (set-buffer file-buffer)
773 (let ((require-final-newline nil))
774 (gnus-write-buffer-as-coding-system
775 nnheader-text-coding-system filename)))
776 (kill-buffer file-buffer))
777 (error "Output file does not exist")))
780 (insert-buffer-substring artbuf)
781 (goto-char (point-min))
782 (if (looking-at "From ")
784 (insert "From nobody " (current-time-string) "\n"))
785 (let (case-fold-search)
786 (while (re-search-forward "^From " nil t)
789 ;; Decide whether to append to a file or to an Emacs buffer.
790 (let ((outbuf (get-file-buffer filename)))
792 (let ((buffer-read-only nil))
794 (goto-char (point-max))
796 (unless (looking-at "\n\n")
797 (goto-char (point-max))
801 (goto-char (point-max))
802 (write-region-as-binary (point-min) (point-max)
804 ;; File has been visited, in buffer OUTBUF.
806 (let ((buffer-read-only nil))
807 (goto-char (point-max))
811 (insert-buffer-substring tmpbuf)))))
812 (kill-buffer tmpbuf)))
814 (defun gnus-convert-article-to-rmail ()
815 "Convert article in current buffer to Rmail message format."
816 (let ((buffer-read-only nil))
817 ;; Convert article directly into Babyl format.
818 (goto-char (point-min))
819 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
820 (while (search-forward "\n\^_" nil t) ;single char
821 (replace-match "\n^_" t t)) ;2 chars: "^" and "_"
822 (goto-char (point-max))
825 (defun gnus-map-function (funs arg)
826 "Applies the result of the first function in FUNS to the second, and so on.
827 ARG is passed to the first function."
830 (setq arg (funcall (pop myfuns) arg)))
833 (defun gnus-run-hooks (&rest funcs)
834 "Does the same as `run-hooks', but saves excursion."
835 (let ((buf (current-buffer)))
837 (apply 'run-hooks funcs)
841 ;;; .netrc and .authinforc parsing
844 (defun gnus-parse-netrc (file)
845 "Parse FILE and return an list of all entries in the file."
846 (when (file-exists-p file)
848 (let ((tokens '("machine" "default" "login"
849 "password" "account" "macdef" "force"
851 alist elem result pair)
852 (insert-file-contents file)
853 (goto-char (point-min))
854 ;; Go through the file, line by line.
856 (narrow-to-region (point) (gnus-point-at-eol))
857 ;; For each line, get the tokens and values.
859 (skip-chars-forward "\t ")
860 ;; Skip lines that begin with a "#".
861 (if (eq (char-after) ?#)
862 (goto-char (point-max))
865 (if (= (following-char) ?\")
866 (read (current-buffer))
868 (point) (progn (skip-chars-forward "^\t ")
871 ((equal elem "macdef")
872 ;; We skip past the macro definition.
874 (while (and (zerop (forward-line 1))
876 (narrow-to-region (point) (point)))
877 ((member elem tokens)
878 ;; Tokens that don't have a following value are ignored,
880 (when (and pair (or (cdr pair)
881 (equal (car pair) "default")))
883 (setq pair (list elem)))
885 ;; Values that haven't got a preceding token are ignored.
889 (setq pair nil)))))))
891 (push (nreverse alist) result))
896 (nreverse result)))))
898 (defun gnus-netrc-machine (list machine &optional port defaultport)
899 "Return the netrc values from LIST for MACHINE or for the default entry.
900 If PORT specified, only return entries with matching port tokens.
901 Entries without port tokens default to DEFAULTPORT."
905 (when (equal (cdr (assoc "machine" (car list))) machine)
906 (push (car list) result))
909 ;; No machine name matches, so we look for default entries.
911 (when (assoc "default" (car rest))
912 (push (car rest) result))
915 (setq result (nreverse result))
917 (not (equal (or port defaultport "nntp")
918 (or (gnus-netrc-get (car result) "port")
919 defaultport "nntp"))))
923 (defun gnus-netrc-get (alist type)
924 "Return the value of token TYPE from ALIST."
925 (cdr (assoc type alist)))
929 (defvar gnus-group-buffer) ; Compiler directive
930 (defun gnus-alive-p ()
931 "Say whether Gnus is running or not."
932 (and (boundp 'gnus-group-buffer)
933 (get-buffer gnus-group-buffer)
935 (set-buffer gnus-group-buffer)
936 (eq major-mode 'gnus-group-mode))))
938 (defun gnus-remove-duplicates (list)
939 (let (new (tail list))
941 (or (member (car tail) new)
942 (setq new (cons (car tail) new)))
943 (setq tail (cdr tail)))
946 (defun gnus-delete-if (predicate list)
947 "Delete elements from LIST that satisfy PREDICATE."
950 (unless (funcall predicate (car list))
951 (push (car list) out))
955 (defun gnus-delete-alist (key alist)
956 "Delete all entries in ALIST that have a key eq to KEY."
958 (while (setq entry (assq key alist))
959 (setq alist (delq entry alist)))
962 (defmacro gnus-pull (key alist &optional assoc-p)
963 "Modify ALIST to be without KEY."
964 (unless (symbolp alist)
965 (error "Not a symbol: %s" alist))
966 (let ((fun (if assoc-p 'assoc 'assq)))
967 `(setq ,alist (delq (,fun ,key ,alist) ,alist))))
969 (defun gnus-globalify-regexp (re)
970 "Returns a regexp that matches a whole line, iff RE matches a part of it."
971 (concat (unless (string-match "^\\^" re) "^.*")
973 (unless (string-match "\\$$" re) ".*$")))
975 (defun gnus-set-window-start (&optional point)
976 "Set the window start to POINT, or (point) if nil."
977 (let ((win (get-buffer-window (current-buffer) t)))
979 (set-window-start win (or point (point))))))
981 (defun gnus-annotation-in-region-p (b e)
983 (eq (cadr (memq 'gnus-undeletable (text-properties-at b))) t)
984 (text-property-any b e 'gnus-undeletable t)))
986 (defun gnus-or (&rest elems)
987 "Return non-nil if any of the elements are non-nil."
993 (defun gnus-and (&rest elems)
994 "Return non-nil if all of the elements are non-nil."
1001 (defun gnus-write-active-file (file hashtb &optional full-names)
1002 (let ((output-coding-system nnmail-active-file-coding-system)
1003 (coding-system-for-write nnmail-active-file-coding-system))
1004 (with-temp-file file
1010 (insert (format "%S %d %d y\n"
1013 (intern (gnus-group-real-name (symbol-name sym))))
1014 (or (cdr (symbol-value sym))
1015 (car (symbol-value sym)))
1016 (car (symbol-value sym))))))
1018 (goto-char (point-max))
1019 (while (search-backward "\\." nil t)
1022 (if (fboundp 'union)
1023 (defalias 'gnus-union 'union)
1024 (defun gnus-union (l1 l2)
1025 "Set union of lists L1 and L2."
1026 (cond ((null l1) l2)
1030 (or (>= (length l1) (length l2))
1031 (setq l1 (prog1 l2 (setq l2 l1))))
1033 (or (member (car l2) l1)
1038 (defun gnus-add-text-properties-when
1039 (property value start end properties &optional object)
1040 "Like `gnus-add-text-properties', only applied on where PROPERTY is VALUE."
1043 (< start end) ;; XEmacs will loop for every when start=end.
1044 (setq point (text-property-not-all start end property value)))
1045 (gnus-add-text-properties start point properties object)
1046 (setq start (text-property-any point end property value)))
1048 (gnus-add-text-properties start end properties object))))
1050 (defun gnus-remove-text-properties-when
1051 (property value start end properties &optional object)
1052 "Like `remove-text-properties', only applied on where PROPERTY is VALUE."
1056 (setq point (text-property-not-all start end property value)))
1057 (remove-text-properties start point properties object)
1058 (setq start (text-property-any point end property value)))
1060 (remove-text-properties start end properties object))
1063 (provide 'gnus-util)
1065 ;;; gnus-util.el ends here