1 ;;; gnus-util.el --- utility functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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 (autoload 'message-fetch-field
"message")
41 (autoload 'rmail-insert-rmail-file-header
"rmail")
42 (autoload 'rmail-count-new-messages
"rmail")
43 (autoload 'rmail-show-message
"rmail"))
45 (defun gnus-boundp (variable)
46 "Return non-nil if VARIABLE is bound and non-nil."
47 (and (boundp variable
)
48 (symbol-value variable
)))
50 (defmacro gnus-eval-in-buffer-window
(buffer &rest forms
)
51 "Pop to BUFFER, evaluate FORMS, and then return to the original window."
52 (let ((tempvar (make-symbol "GnusStartBufferWindow"))
54 (buf (make-symbol "buf")))
55 `(let* ((,tempvar
(selected-window))
57 (,w
(get-buffer-window ,buf
'visible
)))
63 (set-buffer (window-buffer ,w
)))
66 (select-window ,tempvar
)))))
68 (put 'gnus-eval-in-buffer-window
'lisp-indent-function
1)
69 (put 'gnus-eval-in-buffer-window
'edebug-form-spec
'(form body
))
71 (defmacro gnus-intern-safe
(string hashtable
)
72 "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
73 `(let ((symbol (intern ,string
,hashtable
)))
78 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
79 ;; to limit the length of a string. This function is necessary since
80 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
81 (defsubst gnus-limit-string
(str width
)
82 (if (> (length str
) width
)
83 (substring str
0 width
)
86 (defsubst gnus-functionp
(form)
87 "Return non-nil if FORM is funcallable."
88 (or (and (symbolp form
) (fboundp form
))
89 (and (listp form
) (eq (car form
) 'lambda
))
90 (byte-code-function-p form
)))
92 (defsubst gnus-goto-char
(point)
93 (and point
(goto-char point
)))
95 (defmacro gnus-buffer-exists-p
(buffer)
96 `(let ((buffer ,buffer
))
98 (funcall (if (stringp buffer
) 'get-buffer
'buffer-name
)
101 (defmacro gnus-kill-buffer
(buffer)
102 `(let ((buf ,buffer
))
103 (when (gnus-buffer-exists-p buf
)
106 (defalias 'gnus-point-at-bol
107 (if (fboundp 'point-at-bol
)
109 'line-beginning-position
))
111 (defalias 'gnus-point-at-eol
112 (if (fboundp 'point-at-eol
)
116 (defun gnus-delete-first (elt list
)
117 "Delete by side effect the first occurrence of ELT as a member of LIST."
118 (if (equal (car list
) elt
)
121 (while (and (cdr list
)
122 (not (equal (cadr list
) elt
)))
123 (setq list
(cdr list
)))
125 (setcdr list
(cddr list
)))
128 ;; Delete the current line (and the next N lines).
129 (defmacro gnus-delete-line
(&optional n
)
130 `(delete-region (progn (beginning-of-line) (point))
131 (progn (forward-line ,(or n
1)) (point))))
133 (defun gnus-byte-code (func)
134 "Return a form that can be `eval'ed based on FUNC."
135 (let ((fval (indirect-function func
)))
136 (if (byte-code-function-p fval
)
137 (let ((flist (append fval nil
)))
138 (setcar flist
'byte-code
)
140 (cons 'progn
(cddr fval
)))))
142 (defun gnus-extract-address-components (from)
144 ;; First find the address - the thing with the @ in it. This may
145 ;; not be accurate in mail addresses, but does the trick most of
146 ;; the time in news messages.
147 (when (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from
)
148 (setq address
(substring from
(match-beginning 0) (match-end 0))))
149 ;; Then we check whether the "name <address>" format is used.
151 ;; Linear white space is not required.
152 (string-match (concat "[ \t]*<" (regexp-quote address
) ">") from
)
153 (and (setq name
(substring from
0 (match-beginning 0)))
154 ;; Strip any quotes from the name.
155 (string-match "\".*\"" name
)
156 (setq name
(substring name
1 (1- (match-end 0))))))
157 ;; If not, then "address (name)" is used.
159 (and (string-match "(.+)" from
)
160 (setq name
(substring from
(1+ (match-beginning 0))
161 (1- (match-end 0)))))
162 (and (string-match "()" from
)
164 ;; XOVER might not support folded From headers.
165 (and (string-match "(.*" from
)
166 (setq name
(substring from
(1+ (match-beginning 0))
168 (list (if (string= name
"") nil name
) (or address from
))))
171 (defun gnus-fetch-field (field)
172 "Return the value of the header FIELD of current article."
175 (let ((case-fold-search t
)
176 (inhibit-point-motion-hooks t
))
177 (nnheader-narrow-to-headers)
178 (message-fetch-field field
)))))
180 (defun gnus-goto-colon ()
182 (search-forward ":" (gnus-point-at-eol) t
))
184 (defun gnus-remove-text-with-property (prop)
185 "Delete all text in the current buffer with text property PROP."
187 (goto-char (point-min))
189 (while (get-text-property (point) prop
)
191 (goto-char (next-single-property-change (point) prop nil
(point-max))))))
193 (defun gnus-newsgroup-directory-form (newsgroup)
194 "Make hierarchical directory name from NEWSGROUP name."
195 (let ((newsgroup (gnus-newsgroup-savable-name newsgroup
))
196 (len (length newsgroup
))
198 ;; If this is a foreign group, we don't want to translate the
200 (if (setq idx
(string-match ":" newsgroup
))
201 (aset newsgroup idx ?
/)
203 ;; Replace all occurrences of `.' with `/'.
205 (when (= (aref newsgroup idx
) ?.
)
206 (aset newsgroup idx ?
/))
210 (defun gnus-newsgroup-savable-name (group)
211 ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
213 (nnheader-replace-chars-in-string group ?
/ ?.
))
215 (defun gnus-string> (s1 s2
)
216 (not (or (string< s1 s2
)
221 (defun gnus-file-newer-than (file date
)
222 (let ((fdate (nth 5 (file-attributes file
))))
223 (or (> (car fdate
) (car date
))
224 (and (= (car fdate
) (car date
))
225 (> (nth 1 fdate
) (nth 1 date
))))))
229 (defmacro gnus-local-set-keys
(&rest plist
)
230 "Set the keys in PLIST in the current keymap."
231 `(gnus-define-keys-1 (current-local-map) ',plist
))
233 (defmacro gnus-define-keys
(keymap &rest plist
)
234 "Define all keys in PLIST in KEYMAP."
235 `(gnus-define-keys-1 (quote ,keymap
) (quote ,plist
)))
237 (defmacro gnus-define-keys-safe
(keymap &rest plist
)
238 "Define all keys in PLIST in KEYMAP without overwriting previous definitions."
239 `(gnus-define-keys-1 (quote ,keymap
) (quote ,plist
) t
))
241 (put 'gnus-define-keys
'lisp-indent-function
1)
242 (put 'gnus-define-keys-safe
'lisp-indent-function
1)
243 (put 'gnus-local-set-keys
'lisp-indent-function
1)
245 (defmacro gnus-define-keymap
(keymap &rest plist
)
246 "Define all keys in PLIST in KEYMAP."
247 `(gnus-define-keys-1 ,keymap
(quote ,plist
)))
249 (put 'gnus-define-keymap
'lisp-indent-function
1)
251 (defun gnus-define-keys-1 (keymap plist
&optional safe
)
253 (error "Can't set keys in a null keymap"))
254 (cond ((symbolp keymap
)
255 (setq keymap
(symbol-value keymap
)))
258 (set (car keymap
) nil
)
259 (define-prefix-command (car keymap
))
260 (define-key (symbol-value (caddr keymap
)) (cadr keymap
) (car keymap
))
261 (setq keymap
(symbol-value (car keymap
)))))
264 (when (symbolp (setq key
(pop plist
)))
265 (setq key
(symbol-value key
)))
267 (eq (lookup-key keymap key
) 'undefined
))
268 (define-key keymap key
(pop plist
))
271 (defun gnus-completing-read (default prompt
&rest args
)
272 ;; Like `completing-read', except that DEFAULT is the default argument.
273 (let* ((prompt (if default
274 (concat prompt
" (default " default
") ")
275 (concat prompt
" ")))
276 (answer (apply 'completing-read prompt args
)))
277 (if (or (null answer
) (zerop (length answer
)))
281 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
283 (defun gnus-y-or-n-p (prompt)
288 (defun gnus-yes-or-no-p (prompt)
293 (defun gnus-dd-mmm (messy-date)
294 "Return a string like DD-MMM from a big messy string."
296 (format-time-string "%d-%b" (safe-date-to-time messy-date
))
299 (defmacro gnus-date-get-time
(date)
300 "Convert DATE string to Emacs time.
301 Cache the result as a text property stored in DATE."
302 ;; Either return the cached value...
306 (or (get-text-property 0 'gnus-time d
)
307 ;; or compute the value...
308 (let ((time (safe-date-to-time d
)))
309 ;; and store it back in the string.
310 (put-text-property 0 1 'gnus-time time d
)
313 (defsubst gnus-time-iso8601
(time)
314 "Return a string of TIME in YYYYMMDDTHHMMSS format."
315 (format-time-string "%Y%m%dT%H%M%S" time
))
317 (defun gnus-date-iso8601 (date)
318 "Convert the DATE to YYYYMMDDTHHMMSS."
320 (gnus-time-iso8601 (gnus-date-get-time date
))
323 (defun gnus-mode-string-quote (string)
324 "Quote all \"%\"'s in STRING."
326 (gnus-set-work-buffer)
328 (goto-char (point-min))
329 (while (search-forward "%" nil t
)
333 ;; Make a hash table (default and minimum size is 256).
334 ;; Optional argument HASHSIZE specifies the table size.
335 (defun gnus-make-hashtable (&optional hashsize
)
336 (make-vector (if hashsize
(max (gnus-create-hash-size hashsize
) 256) 256) 0))
338 ;; Make a number that is suitable for hashing; bigger than MIN and
339 ;; equal to some 2^x. Many machines (such as sparcs) do not have a
340 ;; hardware modulo operation, so they implement it in software. On
341 ;; many sparcs over 50% of the time to intern is spent in the modulo.
342 ;; Yes, it's slower than actually computing the hash from the string!
343 ;; So we use powers of 2 so people can optimize the modulo to a mask.
344 (defun gnus-create-hash-size (min)
350 (defcustom gnus-verbose
7
351 "*Integer that says how verbose Gnus should be.
352 The higher the number, the more messages Gnus will flash to say what
353 it's doing. At zero, Gnus will be totally mute; at five, Gnus will
354 display most important messages; and at ten, Gnus will keep on
355 jabbering all the time."
359 ;; Show message if message has a lower level than `gnus-verbose'.
360 ;; Guideline for numbers:
361 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
362 ;; for things that take a long time, 7 - not very important messages
363 ;; on stuff, 9 - messages inside loops.
364 (defun gnus-message (level &rest args
)
365 (if (<= level gnus-verbose
)
366 (apply 'message args
)
367 ;; We have to do this format thingy here even if the result isn't
368 ;; shown - the return value has to be the same as the return value
370 (apply 'format args
)))
372 (defun gnus-error (level &rest args
)
373 "Beep an error if LEVEL is equal to or less than `gnus-verbose'."
374 (when (<= (floor level
) gnus-verbose
)
375 (apply 'message args
)
378 (when (and (floatp level
)
379 (not (zerop (setq duration
(* 10 (- level
(floor level
)))))))
380 (sit-for duration
))))
383 (defun gnus-split-references (references)
384 "Return a list of Message-IDs in REFERENCES."
387 (while (string-match "<[^>]+>" references beg
)
388 (push (substring references
(match-beginning 0) (setq beg
(match-end 0)))
392 (defsubst gnus-parent-id
(references &optional n
)
393 "Return the last Message-ID in REFERENCES.
394 If N, return the Nth ancestor instead."
396 (let ((ids (inline (gnus-split-references references
))))
397 (while (nthcdr (or n
1) ids
)
398 (setq ids
(cdr ids
)))
401 (defsubst gnus-buffer-live-p
(buffer)
402 "Say whether BUFFER is alive or not."
405 (buffer-name (get-buffer buffer
))))
407 (defun gnus-horizontal-recenter ()
408 "Recenter the current buffer horizontally."
409 (if (< (current-column) (/ (window-width) 2))
410 (set-window-hscroll (get-buffer-window (current-buffer) t
) 0)
411 (let* ((orig (point))
412 (end (window-end (get-buffer-window (current-buffer) t
)))
415 ;; Find the longest line currently displayed in the window.
416 (goto-char (window-start))
417 (while (and (not (eobp))
420 (setq max
(max max
(current-column)))
423 ;; Scroll horizontally to center (sort of) the point.
424 (if (> max
(window-width))
426 (get-buffer-window (current-buffer) t
)
427 (min (- (current-column) (/ (window-width) 3))
428 (+ 2 (- max
(window-width)))))
429 (set-window-hscroll (get-buffer-window (current-buffer) t
) 0))
432 (defun gnus-read-event-char ()
433 "Get the next event."
434 (let ((event (read-event)))
435 ;; should be gnus-characterp, but this can't be called in XEmacs anyway
436 (cons (and (numberp event
) event
) event
)))
438 (defun gnus-sortable-date (date)
439 "Make string suitable for sorting from DATE."
440 (gnus-time-iso8601 (date-to-time date
)))
442 (defun gnus-copy-file (file &optional to
)
445 (list (read-file-name "Copy file: " default-directory
)
446 (read-file-name "Copy file to: " default-directory
)))
448 (setq to
(read-file-name "Copy file to: " default-directory
)))
449 (when (file-directory-p to
)
450 (setq to
(concat (file-name-as-directory to
)
451 (file-name-nondirectory file
))))
454 (defvar gnus-work-buffer
" *gnus work*")
456 (defun gnus-set-work-buffer ()
457 "Put point in the empty Gnus work buffer."
458 (if (get-buffer gnus-work-buffer
)
460 (set-buffer gnus-work-buffer
)
462 (set-buffer (gnus-get-buffer-create gnus-work-buffer
))
463 (kill-all-local-variables)
464 (mm-enable-multibyte)))
466 (defmacro gnus-group-real-name
(group)
467 "Find the real name of a foreign newsgroup."
468 `(let ((gname ,group
))
469 (if (string-match "^[^:]+:" gname
)
470 (substring gname
(match-end 0))
473 (defun gnus-make-sort-function (funs)
474 "Return a composite sort condition based on the functions in FUNC."
476 ;; Just a simple function.
477 ((gnus-functionp funs
) funs
)
478 ;; No functions at all.
480 ;; A list of functions.
484 ,(gnus-make-sort-function-1 (reverse funs
))))
485 ;; A list containing just one function.
489 (defun gnus-make-sort-function-1 (funs)
490 "Return a composite sort condition based on the functions in FUNC."
491 (let ((function (car funs
))
494 (when (consp function
)
497 ((eq (car function
) 'not
)
498 (setq function
(cadr function
)
501 ((gnus-functionp function
)
505 (error "Invalid sort spec: %s" function
))))
507 `(or (,function
,first
,last
)
508 (and (not (,function
,last
,first
))
509 ,(gnus-make-sort-function-1 (cdr funs
))))
510 `(,function
,first
,last
))))
512 (defun gnus-turn-off-edit-menu (type)
513 "Turn off edit menu in `gnus-TYPE-mode-map'."
514 (define-key (symbol-value (intern (format "gnus-%s-mode-map" type
)))
515 [menu-bar edit
] 'undefined
))
517 (defun gnus-prin1 (form)
518 "Use `prin1' on FORM in the current buffer.
519 Bind `print-quoted' and `print-readably' to t while printing."
520 (let ((print-quoted t
)
522 (print-escape-multibyte nil
)
523 print-level print-length
)
524 (prin1 form
(current-buffer))))
526 (defun gnus-prin1-to-string (form)
527 "The same as `prin1', but bind `print-quoted' and `print-readably' to t."
528 (let ((print-quoted t
)
530 (prin1-to-string form
)))
532 (defun gnus-make-directory (directory)
533 "Make DIRECTORY (and all its parents) if it doesn't exist."
534 (let ((file-name-coding-system nnmail-pathname-coding-system
))
536 (not (file-exists-p directory
)))
537 (make-directory directory t
)))
540 (defun gnus-write-buffer (file)
541 "Write the current buffer's contents to FILE."
542 ;; Make sure the directory exists.
543 (gnus-make-directory (file-name-directory file
))
544 (let ((file-name-coding-system nnmail-pathname-coding-system
))
546 (write-region (point-min) (point-max) file nil
'quietly
)))
548 (defun gnus-delete-file (file)
549 "Delete FILE if it exists."
550 (when (file-exists-p file
)
553 (defun gnus-strip-whitespace (string)
554 "Return STRING stripped of all whitespace."
555 (while (string-match "[\r\n\t ]+" string
)
556 (setq string
(replace-match "" t t string
)))
559 (defsubst gnus-put-text-property-excluding-newlines
(beg end prop val
)
560 "The same as `put-text-property', but don't put this prop on any newlines in the region."
565 (while (re-search-forward gnus-emphasize-whitespace-regexp end
'move
)
566 (gnus-put-text-property beg
(match-beginning 0) prop val
)
568 (gnus-put-text-property beg
(point) prop val
)))))
570 (defun gnus-put-text-property-excluding-characters-with-faces (beg end
572 "The same as `put-text-property', but don't put props on characters with the `gnus-face' property."
575 (when (get-text-property b
'gnus-face
)
576 (setq b
(next-single-property-change b
'gnus-face nil end
)))
578 (gnus-put-text-property
579 b
(setq b
(next-single-property-change b
'gnus-face nil end
))
582 ;;; Protected and atomic operations. dmoore@ucsd.edu 21.11.1996
583 ;;; The primary idea here is to try to protect internal datastructures
584 ;;; from becoming corrupted when the user hits C-g, or if a hook or
585 ;;; similar blows up. Often in Gnus multiple tables/lists need to be
586 ;;; updated at the same time, or information can be lost.
588 (defvar gnus-atomic-be-safe t
589 "If t, certain operations will be protected from interruption by C-g.")
591 (defmacro gnus-atomic-progn
(&rest forms
)
592 "Evaluate FORMS atomically, which means to protect the evaluation
593 from being interrupted by the user. An error from the forms themselves
594 will return without finishing the operation. Since interrupts from
595 the user are disabled, it is recommended that only the most minimal
596 operations are performed by FORMS. If you wish to assign many
597 complicated values atomically, compute the results into temporary
598 variables and then do only the assignment atomically."
599 `(let ((inhibit-quit gnus-atomic-be-safe
))
602 (put 'gnus-atomic-progn
'lisp-indent-function
0)
604 (defmacro gnus-atomic-progn-assign
(protect &rest forms
)
605 "Evaluate FORMS, but insure that the variables listed in PROTECT
606 are not changed if anything in FORMS signals an error or otherwise
607 non-locally exits. The variables listed in PROTECT are updated atomically.
608 It is safe to use gnus-atomic-progn-assign with long computations.
610 Note that if any of the symbols in PROTECT were unbound, they will be
611 set to nil on a sucessful assignment. In case of an error or other
612 non-local exit, it will still be unbound."
613 (let* ((temp-sym-map (mapcar (lambda (x) (list (make-symbol
614 (concat (symbol-name x
)
618 (sym-temp-map (mapcar (lambda (x) (list (cadr x
) (car x
)))
620 (temp-sym-let (mapcar (lambda (x) (list (car x
)
621 `(and (boundp ',(cadr x
))
624 (sym-temp-let sym-temp-map
)
625 (temp-sym-assign (apply 'append temp-sym-map
))
626 (sym-temp-assign (apply 'append sym-temp-map
))
627 (result (make-symbol "result-tmp")))
628 `(let (,@temp-sym-let
631 (setq ,result
(progn ,@forms
))
632 (setq ,@temp-sym-assign
))
633 (let ((inhibit-quit gnus-atomic-be-safe
))
634 (setq ,@sym-temp-assign
))
637 (put 'gnus-atomic-progn-assign
'lisp-indent-function
1)
638 ;(put 'gnus-atomic-progn-assign 'edebug-form-spec '(sexp body))
640 (defmacro gnus-atomic-setq
(&rest pairs
)
641 "Similar to setq, except that the real symbols are only assigned when
642 there are no errors. And when the real symbols are assigned, they are
643 done so atomically. If other variables might be changed via side-effect,
644 see gnus-atomic-progn-assign. It is safe to use gnus-atomic-setq
645 with potentially long computations."
649 (push (car tpairs
) syms
)
650 (setq tpairs
(cddr tpairs
)))
651 `(gnus-atomic-progn-assign ,syms
654 ;(put 'gnus-atomic-setq 'edebug-form-spec '(body))
657 ;;; Functions for saving to babyl/mail files.
659 (defvar rmail-default-rmail-file
)
660 (defun gnus-output-to-rmail (filename &optional ask
)
661 "Append the current article to an Rmail file named FILENAME."
663 ;; Most of these codes are borrowed from rmailout.el.
664 (setq filename
(expand-file-name filename
))
665 (setq rmail-default-rmail-file filename
)
666 (let ((artbuf (current-buffer))
667 (tmpbuf (get-buffer-create " *Gnus-output*")))
669 (or (get-file-buffer filename
)
670 (file-exists-p filename
)
673 (concat "\"" filename
"\" does not exist, create it? ")))
674 (let ((file-buffer (create-file-buffer filename
)))
676 (set-buffer file-buffer
)
677 (rmail-insert-rmail-file-header)
678 (let ((require-final-newline nil
)
679 (coding-system-for-write mm-text-coding-system
))
680 (gnus-write-buffer filename
)))
681 (kill-buffer file-buffer
))
682 (error "Output file does not exist")))
685 (insert-buffer-substring artbuf
)
686 (gnus-convert-article-to-rmail)
687 ;; Decide whether to append to a file or to an Emacs buffer.
688 (let ((outbuf (get-file-buffer filename
)))
690 (mm-append-to-file (point-min) (point-max) filename
)
691 ;; File has been visited, in buffer OUTBUF.
693 (let ((buffer-read-only nil
)
694 (msg (and (boundp 'rmail-current-message
)
695 (symbol-value 'rmail-current-message
))))
696 ;; If MSG is non-nil, buffer is in RMAIL mode.
699 (narrow-to-region (point-max) (point-max)))
700 (insert-buffer-substring tmpbuf
)
702 (goto-char (point-min))
704 (search-backward "\n\^_")
705 (narrow-to-region (point) (point-max))
706 (rmail-count-new-messages t
)
707 (when (rmail-summary-exists)
708 (rmail-select-summary
709 (rmail-update-summary)))
710 (rmail-count-new-messages t
)
711 (rmail-show-message msg
))
713 (kill-buffer tmpbuf
)))
715 (defun gnus-output-to-mail (filename &optional ask
)
716 "Append the current article to a mail file named FILENAME."
717 (setq filename
(expand-file-name filename
))
718 (let ((artbuf (current-buffer))
719 (tmpbuf (get-buffer-create " *Gnus-output*")))
721 ;; Create the file, if it doesn't exist.
722 (when (and (not (get-file-buffer filename
))
723 (not (file-exists-p filename
)))
726 (concat "\"" filename
"\" does not exist, create it? ")))
727 (let ((file-buffer (create-file-buffer filename
)))
729 (set-buffer file-buffer
)
730 (let ((require-final-newline nil
)
731 (coding-system-for-write mm-text-coding-system
))
732 (gnus-write-buffer filename
)))
733 (kill-buffer file-buffer
))
734 (error "Output file does not exist")))
737 (insert-buffer-substring artbuf
)
738 (goto-char (point-min))
739 (if (looking-at "From ")
741 (insert "From nobody " (current-time-string) "\n"))
742 (let (case-fold-search)
743 (while (re-search-forward "^From " nil t
)
746 ;; Decide whether to append to a file or to an Emacs buffer.
747 (let ((outbuf (get-file-buffer filename
)))
749 (let ((buffer-read-only nil
))
751 (goto-char (point-max))
753 (unless (looking-at "\n\n")
754 (goto-char (point-max))
758 (goto-char (point-max))
759 (mm-append-to-file (point-min) (point-max) filename
)))
760 ;; File has been visited, in buffer OUTBUF.
762 (let ((buffer-read-only nil
))
763 (goto-char (point-max))
767 (insert-buffer-substring tmpbuf
)))))
768 (kill-buffer tmpbuf
)))
770 (defun gnus-convert-article-to-rmail ()
771 "Convert article in current buffer to Rmail message format."
772 (let ((buffer-read-only nil
))
773 ;; Convert article directly into Babyl format.
774 (goto-char (point-min))
775 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
776 (while (search-forward "\n\^_" nil t
) ;single char
777 (replace-match "\n^_" t t
)) ;2 chars: "^" and "_"
778 (goto-char (point-max))
781 (defun gnus-map-function (funs arg
)
782 "Applies the result of the first function in FUNS to the second, and so on.
783 ARG is passed to the first function."
786 (setq arg
(funcall (pop myfuns
) arg
)))
789 (defun gnus-run-hooks (&rest funcs
)
790 "Does the same as `run-hooks', but saves excursion."
791 (let ((buf (current-buffer)))
793 (apply 'run-hooks funcs
)
797 ;;; .netrc and .authinforc parsing
800 (defun gnus-parse-netrc (file)
801 "Parse FILE and return an list of all entries in the file."
802 (when (file-exists-p file
)
804 (let ((tokens '("machine" "default" "login"
805 "password" "account" "macdef" "force"
807 alist elem result pair
)
808 (insert-file-contents file
)
809 (goto-char (point-min))
810 ;; Go through the file, line by line.
812 (narrow-to-region (point) (gnus-point-at-eol))
813 ;; For each line, get the tokens and values.
815 (skip-chars-forward "\t ")
816 ;; Skip lines that begin with a "#".
817 (if (eq (char-after) ?
#)
818 (goto-char (point-max))
821 (if (= (following-char) ?
\")
822 (read (current-buffer))
824 (point) (progn (skip-chars-forward "^\t ")
827 ((equal elem
"macdef")
828 ;; We skip past the macro definition.
830 (while (and (zerop (forward-line 1))
832 (narrow-to-region (point) (point)))
833 ((member elem tokens
)
834 ;; Tokens that don't have a following value are ignored,
836 (when (and pair
(or (cdr pair
)
837 (equal (car pair
) "default")))
839 (setq pair
(list elem
)))
841 ;; Values that haven't got a preceding token are ignored.
845 (setq pair nil
)))))))
847 (push (nreverse alist
) result
))
852 (nreverse result
)))))
854 (defun gnus-netrc-machine (list machine
&optional port defaultport
)
855 "Return the netrc values from LIST for MACHINE or for the default entry.
856 If PORT specified, only return entries with matching port tokens.
857 Entries without port tokens default to DEFAULTPORT."
861 (when (equal (cdr (assoc "machine" (car list
))) machine
)
862 (push (car list
) result
))
865 ;; No machine name matches, so we look for default entries.
867 (when (assoc "default" (car rest
))
868 (push (car rest
) result
))
871 (setq result
(nreverse result
))
873 (not (equal (or port defaultport
"nntp")
874 (or (gnus-netrc-get (car result
) "port")
875 defaultport
"nntp"))))
879 (defun gnus-netrc-get (alist type
)
880 "Return the value of token TYPE from ALIST."
881 (cdr (assoc type alist
)))
885 (defvar gnus-group-buffer
) ; Compiler directive
886 (defun gnus-alive-p ()
887 "Say whether Gnus is running or not."
888 (and (boundp 'gnus-group-buffer
)
889 (get-buffer gnus-group-buffer
)
891 (set-buffer gnus-group-buffer
)
892 (eq major-mode
'gnus-group-mode
))))
894 (defun gnus-remove-duplicates (list)
895 (let (new (tail list
))
897 (or (member (car tail
) new
)
898 (setq new
(cons (car tail
) new
)))
899 (setq tail
(cdr tail
)))
902 (defun gnus-delete-if (predicate list
)
903 "Delete elements from LIST that satisfy PREDICATE."
906 (unless (funcall predicate
(car list
))
907 (push (car list
) out
))
911 (defun gnus-delete-alist (key alist
)
912 "Delete all entries in ALIST that have a key eq to KEY."
914 (while (setq entry
(assq key alist
))
915 (setq alist
(delq entry alist
)))
918 (defmacro gnus-pull
(key alist
&optional assoc-p
)
919 "Modify ALIST to be without KEY."
920 (unless (symbolp alist
)
921 (error "Not a symbol: %s" alist
))
922 (let ((fun (if assoc-p
'assoc
'assq
)))
923 `(setq ,alist
(delq (,fun
,key
,alist
) ,alist
))))
925 (defun gnus-globalify-regexp (re)
926 "Returns a regexp that matches a whole line, iff RE matches a part of it."
927 (concat (unless (string-match "^\\^" re
) "^.*")
929 (unless (string-match "\\$$" re
) ".*$")))
931 (defun gnus-set-window-start (&optional point
)
932 "Set the window start to POINT, or (point) if nil."
933 (let ((win (get-buffer-window (current-buffer) t
)))
935 (set-window-start win
(or point
(point))))))
937 (defun gnus-annotation-in-region-p (b e
)
939 (eq (cadr (memq 'gnus-undeletable
(text-properties-at b
))) t
)
940 (text-property-any b e
'gnus-undeletable t
)))
942 (defun gnus-or (&rest elems
)
943 "Return non-nil if any of the elements are non-nil."
949 (defun gnus-and (&rest elems
)
950 "Return non-nil if all of the elements are non-nil."
957 (defun gnus-write-active-file (file hashtb
&optional full-names
)
958 (let ((coding-system-for-write nnmail-active-file-coding-system
))
965 (insert (format "%S %d %d y\n"
968 (intern (gnus-group-real-name (symbol-name sym
))))
969 (or (cdr (symbol-value sym
))
970 (car (symbol-value sym
)))
971 (car (symbol-value sym
))))))
973 (goto-char (point-max))
974 (while (search-backward "\\." nil t
)
979 ;;; gnus-util.el ends here