(Finteractive_form): Doc fix.
[emacs.git] / lisp / gnus / gnus-util.el
blobca46e52fc305b960cac4cfae763f8f5d8269f35d
1 ;;; gnus-util.el --- utility functions for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
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)
13 ;; any later version.
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.
25 ;;; Commentary:
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
30 ;; Gnus first.
32 ;;; Code:
34 (require 'custom)
35 (eval-when-compile
36 (require 'cl)
37 ;; Fixme: this should be a gnus variable, not nnmail-.
38 (defvar nnmail-pathname-coding-system))
39 (require 'nnheader)
40 (require 'time-date)
42 (eval-and-compile
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"))
56 (w (make-symbol "w"))
57 (buf (make-symbol "buf")))
58 `(let* ((,tempvar (selected-window))
59 (,buf ,buffer)
60 (,w (get-buffer-window ,buf 'visible)))
61 (unwind-protect
62 (progn
63 (if ,w
64 (progn
65 (select-window ,w)
66 (set-buffer (window-buffer ,w)))
67 (pop-to-buffer ,buf))
68 ,@forms)
69 (select-window ,tempvar)))))
71 (put 'gnus-eval-in-buffer-window 'lisp-indent-function 1)
72 (put 'gnus-eval-in-buffer-window 'edebug-form-spec '(form body))
74 (defmacro gnus-intern-safe (string hashtable)
75 "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
76 `(let ((symbol (intern ,string ,hashtable)))
77 (or (boundp symbol)
78 (set symbol nil))
79 symbol))
81 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
82 ;; to limit the length of a string. This function is necessary since
83 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
84 (defsubst gnus-limit-string (str width)
85 (if (> (length str) width)
86 (substring str 0 width)
87 str))
89 (defsubst gnus-functionp (form)
90 "Return non-nil if FORM is funcallable."
91 (or (and (symbolp form) (fboundp form))
92 (and (listp form) (eq (car form) 'lambda))
93 (byte-code-function-p form)))
95 (defsubst gnus-goto-char (point)
96 (and point (goto-char point)))
98 (defmacro gnus-buffer-exists-p (buffer)
99 `(let ((buffer ,buffer))
100 (when buffer
101 (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
102 buffer))))
104 (defmacro gnus-kill-buffer (buffer)
105 `(let ((buf ,buffer))
106 (when (gnus-buffer-exists-p buf)
107 (kill-buffer buf))))
109 (defalias 'gnus-point-at-bol
110 (if (fboundp 'point-at-bol)
111 'point-at-bol
112 'line-beginning-position))
114 (defalias 'gnus-point-at-eol
115 (if (fboundp 'point-at-eol)
116 'point-at-eol
117 'line-end-position))
119 (defun gnus-delete-first (elt list)
120 "Delete by side effect the first occurrence of ELT as a member of LIST."
121 (if (equal (car list) elt)
122 (cdr list)
123 (let ((total list))
124 (while (and (cdr list)
125 (not (equal (cadr list) elt)))
126 (setq list (cdr list)))
127 (when (cdr list)
128 (setcdr list (cddr list)))
129 total)))
131 ;; Delete the current line (and the next N lines).
132 (defmacro gnus-delete-line (&optional n)
133 `(delete-region (progn (beginning-of-line) (point))
134 (progn (forward-line ,(or n 1)) (point))))
136 (defun gnus-byte-code (func)
137 "Return a form that can be `eval'ed based on FUNC."
138 (let ((fval (indirect-function func)))
139 (if (byte-code-function-p fval)
140 (let ((flist (append fval nil)))
141 (setcar flist 'byte-code)
142 flist)
143 (cons 'progn (cddr fval)))))
145 (defun gnus-extract-address-components (from)
146 (let (name address)
147 ;; First find the address - the thing with the @ in it. This may
148 ;; not be accurate in mail addresses, but does the trick most of
149 ;; the time in news messages.
150 (when (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
151 (setq address (substring from (match-beginning 0) (match-end 0))))
152 ;; Then we check whether the "name <address>" format is used.
153 (and address
154 ;; Linear white space is not required.
155 (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
156 (and (setq name (substring from 0 (match-beginning 0)))
157 ;; Strip any quotes from the name.
158 (string-match "\".*\"" name)
159 (setq name (substring name 1 (1- (match-end 0))))))
160 ;; If not, then "address (name)" is used.
161 (or name
162 (and (string-match "(.+)" from)
163 (setq name (substring from (1+ (match-beginning 0))
164 (1- (match-end 0)))))
165 (and (string-match "()" from)
166 (setq name address))
167 ;; XOVER might not support folded From headers.
168 (and (string-match "(.*" from)
169 (setq name (substring from (1+ (match-beginning 0))
170 (match-end 0)))))
171 (list (if (string= name "") nil name) (or address from))))
174 (defun gnus-fetch-field (field)
175 "Return the value of the header FIELD of current article."
176 (save-excursion
177 (save-restriction
178 (let ((case-fold-search t)
179 (inhibit-point-motion-hooks t))
180 (nnheader-narrow-to-headers)
181 (message-fetch-field field)))))
183 (defun gnus-goto-colon ()
184 (beginning-of-line)
185 (search-forward ":" (gnus-point-at-eol) t))
187 (defun gnus-remove-text-with-property (prop)
188 "Delete all text in the current buffer with text property PROP."
189 (save-excursion
190 (goto-char (point-min))
191 (while (not (eobp))
192 (while (get-text-property (point) prop)
193 (delete-char 1))
194 (goto-char (next-single-property-change (point) prop nil (point-max))))))
196 (defun gnus-newsgroup-directory-form (newsgroup)
197 "Make hierarchical directory name from NEWSGROUP name."
198 (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
199 (len (length newsgroup))
200 idx)
201 ;; If this is a foreign group, we don't want to translate the
202 ;; entire name.
203 (if (setq idx (string-match ":" newsgroup))
204 (aset newsgroup idx ?/)
205 (setq idx 0))
206 ;; Replace all occurrences of `.' with `/'.
207 (while (< idx len)
208 (when (= (aref newsgroup idx) ?.)
209 (aset newsgroup idx ?/))
210 (setq idx (1+ idx)))
211 newsgroup))
213 (defun gnus-newsgroup-savable-name (group)
214 ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
215 ;; with dots.
216 (nnheader-replace-chars-in-string group ?/ ?.))
218 (defun gnus-string> (s1 s2)
219 (not (or (string< s1 s2)
220 (string= s1 s2))))
222 ;;; Time functions.
224 (defun gnus-file-newer-than (file date)
225 (let ((fdate (nth 5 (file-attributes file))))
226 (or (> (car fdate) (car date))
227 (and (= (car fdate) (car date))
228 (> (nth 1 fdate) (nth 1 date))))))
230 ;;; Keymap macros.
232 (defmacro gnus-local-set-keys (&rest plist)
233 "Set the keys in PLIST in the current keymap."
234 `(gnus-define-keys-1 (current-local-map) ',plist))
236 (defmacro gnus-define-keys (keymap &rest plist)
237 "Define all keys in PLIST in KEYMAP."
238 `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
240 (defmacro gnus-define-keys-safe (keymap &rest plist)
241 "Define all keys in PLIST in KEYMAP without overwriting previous definitions."
242 `(gnus-define-keys-1 (quote ,keymap) (quote ,plist) t))
244 (put 'gnus-define-keys 'lisp-indent-function 1)
245 (put 'gnus-define-keys-safe 'lisp-indent-function 1)
246 (put 'gnus-local-set-keys 'lisp-indent-function 1)
248 (defmacro gnus-define-keymap (keymap &rest plist)
249 "Define all keys in PLIST in KEYMAP."
250 `(gnus-define-keys-1 ,keymap (quote ,plist)))
252 (put 'gnus-define-keymap 'lisp-indent-function 1)
254 (defun gnus-define-keys-1 (keymap plist &optional safe)
255 (when (null keymap)
256 (error "Can't set keys in a null keymap"))
257 (cond ((symbolp keymap)
258 (setq keymap (symbol-value keymap)))
259 ((keymapp keymap))
260 ((listp keymap)
261 (set (car keymap) nil)
262 (define-prefix-command (car keymap))
263 (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
264 (setq keymap (symbol-value (car keymap)))))
265 (let (key)
266 (while plist
267 (when (symbolp (setq key (pop plist)))
268 (setq key (symbol-value key)))
269 (if (or (not safe)
270 (eq (lookup-key keymap key) 'undefined))
271 (define-key keymap key (pop plist))
272 (pop plist)))))
274 (defun gnus-completing-read (default prompt &rest args)
275 ;; Like `completing-read', except that DEFAULT is the default argument.
276 (let* ((prompt (if default
277 (concat prompt " (default " default ") ")
278 (concat prompt " ")))
279 (answer (apply 'completing-read prompt args)))
280 (if (or (null answer) (zerop (length answer)))
281 default
282 answer)))
284 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
285 ;; the echo area.
286 (defun gnus-y-or-n-p (prompt)
287 (prog1
288 (y-or-n-p prompt)
289 (message "")))
291 (defun gnus-yes-or-no-p (prompt)
292 (prog1
293 (yes-or-no-p prompt)
294 (message "")))
296 (defun gnus-dd-mmm (messy-date)
297 "Return a string like DD-MMM from a big messy string."
298 (condition-case ()
299 (format-time-string "%d-%b" (safe-date-to-time messy-date))
300 (error " - ")))
302 (defmacro gnus-date-get-time (date)
303 "Convert DATE string to Emacs time.
304 Cache the result as a text property stored in DATE."
305 ;; Either return the cached value...
306 `(let ((d ,date))
307 (if (equal "" d)
308 '(0 0)
309 (or (get-text-property 0 'gnus-time d)
310 ;; or compute the value...
311 (let ((time (safe-date-to-time d)))
312 ;; and store it back in the string.
313 (put-text-property 0 1 'gnus-time time d)
314 time)))))
316 (defsubst gnus-time-iso8601 (time)
317 "Return a string of TIME in YYYYMMDDTHHMMSS format."
318 (format-time-string "%Y%m%dT%H%M%S" time))
320 (defun gnus-date-iso8601 (date)
321 "Convert the DATE to YYYYMMDDTHHMMSS."
322 (condition-case ()
323 (gnus-time-iso8601 (gnus-date-get-time date))
324 (error "")))
326 (defun gnus-mode-string-quote (string)
327 "Quote all \"%\"'s in STRING."
328 (save-excursion
329 (gnus-set-work-buffer)
330 (insert string)
331 (goto-char (point-min))
332 (while (search-forward "%" nil t)
333 (insert "%"))
334 (buffer-string)))
336 ;; Make a hash table (default and minimum size is 256).
337 ;; Optional argument HASHSIZE specifies the table size.
338 (defun gnus-make-hashtable (&optional hashsize)
339 (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 256) 256) 0))
341 ;; Make a number that is suitable for hashing; bigger than MIN and
342 ;; equal to some 2^x. Many machines (such as sparcs) do not have a
343 ;; hardware modulo operation, so they implement it in software. On
344 ;; many sparcs over 50% of the time to intern is spent in the modulo.
345 ;; Yes, it's slower than actually computing the hash from the string!
346 ;; So we use powers of 2 so people can optimize the modulo to a mask.
347 (defun gnus-create-hash-size (min)
348 (let ((i 1))
349 (while (< i min)
350 (setq i (* 2 i)))
353 (defcustom gnus-verbose 7
354 "*Integer that says how verbose Gnus should be.
355 The higher the number, the more messages Gnus will flash to say what
356 it's doing. At zero, Gnus will be totally mute; at five, Gnus will
357 display most important messages; and at ten, Gnus will keep on
358 jabbering all the time."
359 :group 'gnus-start
360 :type 'integer)
362 ;; Show message if message has a lower level than `gnus-verbose'.
363 ;; Guideline for numbers:
364 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
365 ;; for things that take a long time, 7 - not very important messages
366 ;; on stuff, 9 - messages inside loops.
367 (defun gnus-message (level &rest args)
368 (if (<= level gnus-verbose)
369 (apply 'message args)
370 ;; We have to do this format thingy here even if the result isn't
371 ;; shown - the return value has to be the same as the return value
372 ;; from `message'.
373 (apply 'format args)))
375 (defun gnus-error (level &rest args)
376 "Beep an error if LEVEL is equal to or less than `gnus-verbose'."
377 (when (<= (floor level) gnus-verbose)
378 (apply 'message args)
379 (ding)
380 (let (duration)
381 (when (and (floatp level)
382 (not (zerop (setq duration (* 10 (- level (floor level)))))))
383 (sit-for duration))))
384 nil)
386 (defun gnus-split-references (references)
387 "Return a list of Message-IDs in REFERENCES."
388 (let ((beg 0)
389 ids)
390 (while (string-match "<[^>]+>" references beg)
391 (push (substring references (match-beginning 0) (setq beg (match-end 0)))
392 ids))
393 (nreverse ids)))
395 (defsubst gnus-parent-id (references &optional n)
396 "Return the last Message-ID in REFERENCES.
397 If N, return the Nth ancestor instead."
398 (when references
399 (let ((ids (inline (gnus-split-references references))))
400 (while (nthcdr (or n 1) ids)
401 (setq ids (cdr ids)))
402 (car ids))))
404 (defsubst gnus-buffer-live-p (buffer)
405 "Say whether BUFFER is alive or not."
406 (and buffer
407 (get-buffer buffer)
408 (buffer-name (get-buffer buffer))))
410 (defun gnus-horizontal-recenter ()
411 "Recenter the current buffer horizontally."
412 (if (< (current-column) (/ (window-width) 2))
413 (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
414 (let* ((orig (point))
415 (end (window-end (get-buffer-window (current-buffer) t)))
416 (max 0))
417 (when end
418 ;; Find the longest line currently displayed in the window.
419 (goto-char (window-start))
420 (while (and (not (eobp))
421 (< (point) end))
422 (end-of-line)
423 (setq max (max max (current-column)))
424 (forward-line 1))
425 (goto-char orig)
426 ;; Scroll horizontally to center (sort of) the point.
427 (if (> max (window-width))
428 (set-window-hscroll
429 (get-buffer-window (current-buffer) t)
430 (min (- (current-column) (/ (window-width) 3))
431 (+ 2 (- max (window-width)))))
432 (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
433 max))))
435 (defun gnus-read-event-char ()
436 "Get the next event."
437 (let ((event (read-event)))
438 ;; should be gnus-characterp, but this can't be called in XEmacs anyway
439 (cons (and (numberp event) event) event)))
441 (defun gnus-sortable-date (date)
442 "Make string suitable for sorting from DATE."
443 (gnus-time-iso8601 (date-to-time date)))
445 (defun gnus-copy-file (file &optional to)
446 "Copy FILE to TO."
447 (interactive
448 (list (read-file-name "Copy file: " default-directory)
449 (read-file-name "Copy file to: " default-directory)))
450 (unless to
451 (setq to (read-file-name "Copy file to: " default-directory)))
452 (when (file-directory-p to)
453 (setq to (concat (file-name-as-directory to)
454 (file-name-nondirectory file))))
455 (copy-file file to))
457 (defvar gnus-work-buffer " *gnus work*")
459 (defun gnus-set-work-buffer ()
460 "Put point in the empty Gnus work buffer."
461 (if (get-buffer gnus-work-buffer)
462 (progn
463 (set-buffer gnus-work-buffer)
464 (erase-buffer))
465 (set-buffer (gnus-get-buffer-create gnus-work-buffer))
466 (kill-all-local-variables)
467 (mm-enable-multibyte)))
469 (defmacro gnus-group-real-name (group)
470 "Find the real name of a foreign newsgroup."
471 `(let ((gname ,group))
472 (if (string-match "^[^:]+:" gname)
473 (substring gname (match-end 0))
474 gname)))
476 (defun gnus-make-sort-function (funs)
477 "Return a composite sort condition based on the functions in FUNC."
478 (cond
479 ;; Just a simple function.
480 ((gnus-functionp funs) funs)
481 ;; No functions at all.
482 ((null funs) funs)
483 ;; A list of functions.
484 ((or (cdr funs)
485 (listp (car funs)))
486 `(lambda (t1 t2)
487 ,(gnus-make-sort-function-1 (reverse funs))))
488 ;; A list containing just one function.
490 (car funs))))
492 (defun gnus-make-sort-function-1 (funs)
493 "Return a composite sort condition based on the functions in FUNC."
494 (let ((function (car funs))
495 (first 't1)
496 (last 't2))
497 (when (consp function)
498 (cond
499 ;; Reversed spec.
500 ((eq (car function) 'not)
501 (setq function (cadr function)
502 first 't2
503 last 't1))
504 ((gnus-functionp function)
505 ;; Do nothing.
508 (error "Invalid sort spec: %s" function))))
509 (if (cdr funs)
510 `(or (,function ,first ,last)
511 (and (not (,function ,last ,first))
512 ,(gnus-make-sort-function-1 (cdr funs))))
513 `(,function ,first ,last))))
515 (defun gnus-turn-off-edit-menu (type)
516 "Turn off edit menu in `gnus-TYPE-mode-map'."
517 (define-key (symbol-value (intern (format "gnus-%s-mode-map" type)))
518 [menu-bar edit] 'undefined))
520 (defun gnus-prin1 (form)
521 "Use `prin1' on FORM in the current buffer.
522 Bind `print-quoted' and `print-readably' to t while printing."
523 (let ((print-quoted t)
524 (print-readably t)
525 (print-escape-multibyte nil)
526 print-level print-length)
527 (prin1 form (current-buffer))))
529 (defun gnus-prin1-to-string (form)
530 "The same as `prin1', but bind `print-quoted' and `print-readably' to t."
531 (let ((print-quoted t)
532 (print-readably t))
533 (prin1-to-string form)))
535 (defun gnus-make-directory (directory)
536 "Make DIRECTORY (and all its parents) if it doesn't exist."
537 (require 'nnmail)
538 (let ((file-name-coding-system nnmail-pathname-coding-system))
539 (when (and directory
540 (not (file-exists-p directory)))
541 (make-directory directory t)))
544 (defun gnus-write-buffer (file)
545 "Write the current buffer's contents to FILE."
546 ;; Make sure the directory exists.
547 (gnus-make-directory (file-name-directory file))
548 (let ((file-name-coding-system nnmail-pathname-coding-system))
549 ;; Write the buffer.
550 (write-region (point-min) (point-max) file nil 'quietly)))
552 (defun gnus-delete-file (file)
553 "Delete FILE if it exists."
554 (when (file-exists-p file)
555 (delete-file file)))
557 (defun gnus-strip-whitespace (string)
558 "Return STRING stripped of all whitespace."
559 (while (string-match "[\r\n\t ]+" string)
560 (setq string (replace-match "" t t string)))
561 string)
563 (defsubst gnus-put-text-property-excluding-newlines (beg end prop val)
564 "The same as `put-text-property', but don't put this prop on any newlines in the region."
565 (save-match-data
566 (save-excursion
567 (save-restriction
568 (goto-char beg)
569 (while (re-search-forward gnus-emphasize-whitespace-regexp end 'move)
570 (gnus-put-text-property beg (match-beginning 0) prop val)
571 (setq beg (point)))
572 (gnus-put-text-property beg (point) prop val)))))
574 (defun gnus-put-text-property-excluding-characters-with-faces (beg end
575 prop val)
576 "The same as `put-text-property', but don't put props on characters with the `gnus-face' property."
577 (let ((b beg))
578 (while (/= b end)
579 (when (get-text-property b 'gnus-face)
580 (setq b (next-single-property-change b 'gnus-face nil end)))
581 (when (/= b end)
582 (gnus-put-text-property
583 b (setq b (next-single-property-change b 'gnus-face nil end))
584 prop val)))))
586 ;;; Protected and atomic operations. dmoore@ucsd.edu 21.11.1996
587 ;;; The primary idea here is to try to protect internal datastructures
588 ;;; from becoming corrupted when the user hits C-g, or if a hook or
589 ;;; similar blows up. Often in Gnus multiple tables/lists need to be
590 ;;; updated at the same time, or information can be lost.
592 (defvar gnus-atomic-be-safe t
593 "If t, certain operations will be protected from interruption by C-g.")
595 (defmacro gnus-atomic-progn (&rest forms)
596 "Evaluate FORMS atomically, which means to protect the evaluation
597 from being interrupted by the user. An error from the forms themselves
598 will return without finishing the operation. Since interrupts from
599 the user are disabled, it is recommended that only the most minimal
600 operations are performed by FORMS. If you wish to assign many
601 complicated values atomically, compute the results into temporary
602 variables and then do only the assignment atomically."
603 `(let ((inhibit-quit gnus-atomic-be-safe))
604 ,@forms))
606 (put 'gnus-atomic-progn 'lisp-indent-function 0)
608 (defmacro gnus-atomic-progn-assign (protect &rest forms)
609 "Evaluate FORMS, but insure that the variables listed in PROTECT
610 are not changed if anything in FORMS signals an error or otherwise
611 non-locally exits. The variables listed in PROTECT are updated atomically.
612 It is safe to use gnus-atomic-progn-assign with long computations.
614 Note that if any of the symbols in PROTECT were unbound, they will be
615 set to nil on a successful assignment. In case of an error or other
616 non-local exit, it will still be unbound."
617 (let* ((temp-sym-map (mapcar (lambda (x) (list (make-symbol
618 (concat (symbol-name x)
619 "-tmp"))
621 protect))
622 (sym-temp-map (mapcar (lambda (x) (list (cadr x) (car x)))
623 temp-sym-map))
624 (temp-sym-let (mapcar (lambda (x) (list (car x)
625 `(and (boundp ',(cadr x))
626 ,(cadr x))))
627 temp-sym-map))
628 (sym-temp-let sym-temp-map)
629 (temp-sym-assign (apply 'append temp-sym-map))
630 (sym-temp-assign (apply 'append sym-temp-map))
631 (result (make-symbol "result-tmp")))
632 `(let (,@temp-sym-let
633 ,result)
634 (let ,sym-temp-let
635 (setq ,result (progn ,@forms))
636 (setq ,@temp-sym-assign))
637 (let ((inhibit-quit gnus-atomic-be-safe))
638 (setq ,@sym-temp-assign))
639 ,result)))
641 (put 'gnus-atomic-progn-assign 'lisp-indent-function 1)
642 ;(put 'gnus-atomic-progn-assign 'edebug-form-spec '(sexp body))
644 (defmacro gnus-atomic-setq (&rest pairs)
645 "Similar to setq, except that the real symbols are only assigned when
646 there are no errors. And when the real symbols are assigned, they are
647 done so atomically. If other variables might be changed via side-effect,
648 see gnus-atomic-progn-assign. It is safe to use gnus-atomic-setq
649 with potentially long computations."
650 (let ((tpairs pairs)
651 syms)
652 (while tpairs
653 (push (car tpairs) syms)
654 (setq tpairs (cddr tpairs)))
655 `(gnus-atomic-progn-assign ,syms
656 (setq ,@pairs))))
658 ;(put 'gnus-atomic-setq 'edebug-form-spec '(body))
661 ;;; Functions for saving to babyl/mail files.
663 (defvar rmail-default-rmail-file)
664 (defun gnus-output-to-rmail (filename &optional ask)
665 "Append the current article to an Rmail file named FILENAME."
666 (require 'rmail)
667 ;; Most of these codes are borrowed from rmailout.el.
668 (setq filename (expand-file-name filename))
669 (setq rmail-default-rmail-file filename)
670 (let ((artbuf (current-buffer))
671 (tmpbuf (get-buffer-create " *Gnus-output*")))
672 (save-excursion
673 (or (get-file-buffer filename)
674 (file-exists-p filename)
675 (if (or (not ask)
676 (gnus-yes-or-no-p
677 (concat "\"" filename "\" does not exist, create it? ")))
678 (let ((file-buffer (create-file-buffer filename)))
679 (save-excursion
680 (set-buffer file-buffer)
681 (rmail-insert-rmail-file-header)
682 (let ((require-final-newline nil)
683 (coding-system-for-write mm-text-coding-system))
684 (gnus-write-buffer filename)))
685 (kill-buffer file-buffer))
686 (error "Output file does not exist")))
687 (set-buffer tmpbuf)
688 (erase-buffer)
689 (insert-buffer-substring artbuf)
690 (gnus-convert-article-to-rmail)
691 ;; Decide whether to append to a file or to an Emacs buffer.
692 (let ((outbuf (get-file-buffer filename)))
693 (if (not outbuf)
694 (let ((file-name-coding-system nnmail-pathname-coding-system))
695 (mm-append-to-file (point-min) (point-max) filename))
696 ;; File has been visited, in buffer OUTBUF.
697 (set-buffer outbuf)
698 (let ((buffer-read-only nil)
699 (msg (and (boundp 'rmail-current-message)
700 (symbol-value 'rmail-current-message))))
701 ;; If MSG is non-nil, buffer is in RMAIL mode.
702 (when msg
703 (widen)
704 (narrow-to-region (point-max) (point-max)))
705 (insert-buffer-substring tmpbuf)
706 (when msg
707 (goto-char (point-min))
708 (widen)
709 (search-backward "\n\^_")
710 (narrow-to-region (point) (point-max))
711 (rmail-count-new-messages t)
712 (when (rmail-summary-exists)
713 (rmail-select-summary
714 (rmail-update-summary)))
715 (rmail-count-new-messages t)
716 (rmail-show-message msg))
717 (save-buffer)))))
718 (kill-buffer tmpbuf)))
720 (defun gnus-output-to-mail (filename &optional ask)
721 "Append the current article to a mail file named FILENAME."
722 (setq filename (expand-file-name filename))
723 (let ((artbuf (current-buffer))
724 (tmpbuf (get-buffer-create " *Gnus-output*")))
725 (save-excursion
726 ;; Create the file, if it doesn't exist.
727 (when (and (not (get-file-buffer filename))
728 (not (file-exists-p filename)))
729 (if (or (not ask)
730 (gnus-y-or-n-p
731 (concat "\"" filename "\" does not exist, create it? ")))
732 (let ((file-buffer (create-file-buffer filename)))
733 (save-excursion
734 (set-buffer file-buffer)
735 (let ((require-final-newline nil)
736 (coding-system-for-write mm-text-coding-system))
737 (gnus-write-buffer filename)))
738 (kill-buffer file-buffer))
739 (error "Output file does not exist")))
740 (set-buffer tmpbuf)
741 (erase-buffer)
742 (insert-buffer-substring artbuf)
743 (goto-char (point-min))
744 (if (looking-at "From ")
745 (forward-line 1)
746 (insert "From nobody " (current-time-string) "\n"))
747 (let (case-fold-search)
748 (while (re-search-forward "^From " nil t)
749 (beginning-of-line)
750 (insert ">")))
751 ;; Decide whether to append to a file or to an Emacs buffer.
752 (let ((outbuf (get-file-buffer filename)))
753 (if (not outbuf)
754 (let ((buffer-read-only nil))
755 (save-excursion
756 (goto-char (point-max))
757 (forward-char -2)
758 (unless (looking-at "\n\n")
759 (goto-char (point-max))
760 (unless (bolp)
761 (insert "\n"))
762 (insert "\n"))
763 (goto-char (point-max))
764 (let ((file-name-coding-system nnmail-pathname-coding-system))
765 (mm-append-to-file (point-min) (point-max) filename))))
766 ;; File has been visited, in buffer OUTBUF.
767 (set-buffer outbuf)
768 (let ((buffer-read-only nil))
769 (goto-char (point-max))
770 (unless (eobp)
771 (insert "\n"))
772 (insert "\n")
773 (insert-buffer-substring tmpbuf)))))
774 (kill-buffer tmpbuf)))
776 (defun gnus-convert-article-to-rmail ()
777 "Convert article in current buffer to Rmail message format."
778 (let ((buffer-read-only nil))
779 ;; Convert article directly into Babyl format.
780 (goto-char (point-min))
781 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
782 (while (search-forward "\n\^_" nil t) ;single char
783 (replace-match "\n^_" t t)) ;2 chars: "^" and "_"
784 (goto-char (point-max))
785 (insert "\^_")))
787 (defun gnus-map-function (funs arg)
788 "Applies the result of the first function in FUNS to the second, and so on.
789 ARG is passed to the first function."
790 (let ((myfuns funs))
791 (while myfuns
792 (setq arg (funcall (pop myfuns) arg)))
793 arg))
795 (defun gnus-run-hooks (&rest funcs)
796 "Does the same as `run-hooks', but saves excursion."
797 (let ((buf (current-buffer)))
798 (unwind-protect
799 (apply 'run-hooks funcs)
800 (set-buffer buf))))
803 ;;; .netrc and .authinforc parsing
806 (defun gnus-parse-netrc (file)
807 "Parse FILE and return a list of all entries in the file."
808 (when (file-exists-p file)
809 (with-temp-buffer
810 (let ((tokens '("machine" "default" "login"
811 "password" "account" "macdef" "force"
812 "port"))
813 alist elem result pair)
814 (insert-file-contents file)
815 (goto-char (point-min))
816 ;; Go through the file, line by line.
817 (while (not (eobp))
818 (narrow-to-region (point) (gnus-point-at-eol))
819 ;; For each line, get the tokens and values.
820 (while (not (eobp))
821 (skip-chars-forward "\t ")
822 ;; Skip lines that begin with a "#".
823 (if (eq (char-after) ?#)
824 (goto-char (point-max))
825 (unless (eobp)
826 (setq elem
827 (if (= (following-char) ?\")
828 (read (current-buffer))
829 (buffer-substring
830 (point) (progn (skip-chars-forward "^\t ")
831 (point)))))
832 (cond
833 ((equal elem "macdef")
834 ;; We skip past the macro definition.
835 (widen)
836 (while (and (zerop (forward-line 1))
837 (looking-at "$")))
838 (narrow-to-region (point) (point)))
839 ((member elem tokens)
840 ;; Tokens that don't have a following value are ignored,
841 ;; except "default".
842 (when (and pair (or (cdr pair)
843 (equal (car pair) "default")))
844 (push pair alist))
845 (setq pair (list elem)))
847 ;; Values that haven't got a preceding token are ignored.
848 (when pair
849 (setcdr pair elem)
850 (push pair alist)
851 (setq pair nil)))))))
852 (when alist
853 (push (nreverse alist) result))
854 (setq alist nil
855 pair nil)
856 (widen)
857 (forward-line 1))
858 (nreverse result)))))
860 (defun gnus-netrc-machine (list machine &optional port defaultport)
861 "Return the netrc values from LIST for MACHINE or for the default entry.
862 If PORT specified, only return entries with matching port tokens.
863 Entries without port tokens default to DEFAULTPORT."
864 (let ((rest list)
865 result)
866 (while list
867 (when (equal (cdr (assoc "machine" (car list))) machine)
868 (push (car list) result))
869 (pop list))
870 (unless result
871 ;; No machine name matches, so we look for default entries.
872 (while rest
873 (when (assoc "default" (car rest))
874 (push (car rest) result))
875 (pop rest)))
876 (when result
877 (setq result (nreverse result))
878 (while (and result
879 (not (equal (or port defaultport "nntp")
880 (or (gnus-netrc-get (car result) "port")
881 defaultport "nntp"))))
882 (pop result))
883 (car result))))
885 (defun gnus-netrc-get (alist type)
886 "Return the value of token TYPE from ALIST."
887 (cdr (assoc type alist)))
889 ;;; Various
891 (defvar gnus-group-buffer) ; Compiler directive
892 (defun gnus-alive-p ()
893 "Say whether Gnus is running or not."
894 (and (boundp 'gnus-group-buffer)
895 (get-buffer gnus-group-buffer)
896 (save-excursion
897 (set-buffer gnus-group-buffer)
898 (eq major-mode 'gnus-group-mode))))
900 (defun gnus-remove-duplicates (list)
901 (let (new (tail list))
902 (while tail
903 (or (member (car tail) new)
904 (setq new (cons (car tail) new)))
905 (setq tail (cdr tail)))
906 (nreverse new)))
908 (defun gnus-delete-if (predicate list)
909 "Delete elements from LIST that satisfy PREDICATE."
910 (let (out)
911 (while list
912 (unless (funcall predicate (car list))
913 (push (car list) out))
914 (pop list))
915 (nreverse out)))
917 (defun gnus-delete-alist (key alist)
918 "Delete all entries in ALIST that have a key eq to KEY."
919 (let (entry)
920 (while (setq entry (assq key alist))
921 (setq alist (delq entry alist)))
922 alist))
924 (defmacro gnus-pull (key alist &optional assoc-p)
925 "Modify ALIST to be without KEY."
926 (unless (symbolp alist)
927 (error "Not a symbol: %s" alist))
928 (let ((fun (if assoc-p 'assoc 'assq)))
929 `(setq ,alist (delq (,fun ,key ,alist) ,alist))))
931 (defun gnus-globalify-regexp (re)
932 "Returns a regexp that matches a whole line, iff RE matches a part of it."
933 (concat (unless (string-match "^\\^" re) "^.*")
935 (unless (string-match "\\$$" re) ".*$")))
937 (defun gnus-set-window-start (&optional point)
938 "Set the window start to POINT, or (point) if nil."
939 (let ((win (get-buffer-window (current-buffer) t)))
940 (when win
941 (set-window-start win (or point (point))))))
943 (defun gnus-annotation-in-region-p (b e)
944 (if (= b e)
945 (eq (cadr (memq 'gnus-undeletable (text-properties-at b))) t)
946 (text-property-any b e 'gnus-undeletable t)))
948 (defun gnus-or (&rest elems)
949 "Return non-nil if any of the elements are non-nil."
950 (catch 'found
951 (while elems
952 (when (pop elems)
953 (throw 'found t)))))
955 (defun gnus-and (&rest elems)
956 "Return non-nil if all of the elements are non-nil."
957 (catch 'found
958 (while elems
959 (unless (pop elems)
960 (throw 'found nil)))
963 (defun gnus-write-active-file (file hashtb &optional full-names)
964 (let ((coding-system-for-write nnmail-active-file-coding-system))
965 (with-temp-file file
966 (mapatoms
967 (lambda (sym)
968 (when (and sym
969 (boundp sym)
970 (symbol-value sym))
971 (insert (format "%S %d %d y\n"
972 (if full-names
974 (intern (gnus-group-real-name (symbol-name sym))))
975 (or (cdr (symbol-value sym))
976 (car (symbol-value sym)))
977 (car (symbol-value sym))))))
978 hashtb)
979 (goto-char (point-max))
980 (while (search-backward "\\." nil t)
981 (delete-char 1)))))
983 (defun gnus-add-text-properties-when
984 (property value start end properties &optional object)
985 "Like `gnus-add-text-properties', only applied on where PROPERTY is VALUE."
986 (let (point)
987 (while (and start
988 (setq point (text-property-not-all start end property value)))
989 (gnus-add-text-properties start point properties object)
990 (setq start (text-property-any point end property value)))
991 (if start
992 (gnus-add-text-properties start end properties object))))
994 (defun gnus-remove-text-properties-when
995 (property value start end properties &optional object)
996 "Like `remove-text-properties', only applied on where PROPERTY is VALUE."
997 (let (point)
998 (while (and start
999 (setq point (text-property-not-all start end property value)))
1000 (remove-text-properties start point properties object)
1001 (setq start (text-property-any point end property value)))
1002 (if start
1003 (remove-text-properties start end properties object))
1006 (defvar gnus-directory-sep-char-regexp "/"
1007 "The regexp of directory separator character.
1008 If you find some problem with the directory separator character, try
1009 \"[/\\\\\]\" for some systems.")
1011 (provide 'gnus-util)
1013 ;;; arch-tag: f94991af-d32b-4c97-8c26-ca12a934de49
1014 ;;; gnus-util.el ends here