1 ;;; gnus-util.el --- utility functions for Gnus
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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 3 of the License, or
14 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
26 ;; Nothing in this file depends on any other parts of Gnus -- all
27 ;; functions and macros in this file are utility functions that are
28 ;; used by Gnus and may be used by any other package without loading
31 ;; [Unfortunately, it does depend on other parts of Gnus, e.g. the
32 ;; autoloads and defvars below...]
36 ;; For Emacs <22.2 and XEmacs.
38 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
43 (unless (fboundp 'with-no-warnings
)
44 (defmacro with-no-warnings
(&rest body
)
47 (defcustom gnus-completing-read-function
'gnus-emacs-completing-read
48 "Function use to do completing read."
51 :type
`(radio (function-item
52 :doc
"Use Emacs standard `completing-read' function."
53 gnus-emacs-completing-read
)
54 ;; iswitchb.el is very old and ido.el is unavailable
55 ;; in XEmacs, so we exclude those function items.
56 ,@(unless (featurep 'xemacs
)
58 :doc
"Use `ido-completing-read' function."
59 gnus-ido-completing-read
)
61 :doc
"Use iswitchb based completing-read function."
62 gnus-iswitchb-completing-read
)))))
64 (defcustom gnus-completion-styles
65 (if (and (boundp 'completion-styles-alist
)
66 (boundp 'completion-styles
))
67 (append (when (and (assq 'substring completion-styles-alist
)
68 (not (memq 'substring completion-styles
)))
72 "Value of `completion-styles' to use when completing."
77 ;; Fixme: this should be a gnus variable, not nnmail-.
78 (defvar nnmail-pathname-coding-system
)
79 (defvar nnmail-active-file-coding-system
)
81 ;; Inappropriate references to other parts of Gnus.
82 (defvar gnus-emphasize-whitespace-regexp
)
83 (defvar gnus-original-article-buffer
)
84 (defvar gnus-user-agent
)
86 (autoload 'gnus-get-buffer-window
"gnus-win")
87 (autoload 'nnheader-narrow-to-headers
"nnheader")
88 (autoload 'nnheader-replace-chars-in-string
"nnheader")
89 (autoload 'mail-header-remove-comments
"mail-parse")
93 ;; Prefer `replace-regexp-in-string' (present in Emacs, XEmacs 21.5,
94 ;; SXEmacs 22.1.4) over `replace-in-string'. The latter leads to inf-loops
96 ;; (replace-in-string "foo" "/*$" "/")
97 ;; (replace-in-string "xe" "\\(x\\)?" "")
98 ((fboundp 'replace-regexp-in-string
)
99 (defun gnus-replace-in-string (string regexp newtext
&optional literal
)
100 "Replace all matches for REGEXP with NEWTEXT in STRING.
101 If LITERAL is non-nil, insert NEWTEXT literally. Return a new
102 string containing the replacements.
104 This is a compatibility function for different Emacsen."
105 (replace-regexp-in-string regexp newtext string nil literal
)))
106 ((fboundp 'replace-in-string
)
107 (defalias 'gnus-replace-in-string
'replace-in-string
))))
109 (defun gnus-boundp (variable)
110 "Return non-nil if VARIABLE is bound and non-nil."
111 (and (boundp variable
)
112 (symbol-value variable
)))
114 (defmacro gnus-eval-in-buffer-window
(buffer &rest forms
)
115 "Pop to BUFFER, evaluate FORMS, and then return to the original window."
116 (let ((tempvar (make-symbol "GnusStartBufferWindow"))
117 (w (make-symbol "w"))
118 (buf (make-symbol "buf")))
119 `(let* ((,tempvar
(selected-window))
121 (,w
(gnus-get-buffer-window ,buf
'visible
)))
127 (set-buffer (window-buffer ,w
)))
128 (pop-to-buffer ,buf
))
130 (select-window ,tempvar
)))))
132 (put 'gnus-eval-in-buffer-window
'lisp-indent-function
1)
133 (put 'gnus-eval-in-buffer-window
'edebug-form-spec
'(form body
))
135 (defmacro gnus-intern-safe
(string hashtable
)
136 "Get hash value. Arguments are STRING and HASHTABLE."
137 `(let ((symbol (intern ,string
,hashtable
)))
142 (defsubst gnus-goto-char
(point)
143 (and point
(goto-char point
)))
145 (defmacro gnus-buffer-exists-p
(buffer)
146 `(let ((buffer ,buffer
))
148 (funcall (if (stringp buffer
) 'get-buffer
'buffer-name
)
151 ;; The LOCAL arg to `add-hook' is interpreted differently in Emacs and
152 ;; XEmacs. In Emacs we don't need to call `make-local-hook' first.
153 ;; It's harmless, though, so the main purpose of this alias is to shut
154 ;; up the byte compiler.
155 (defalias 'gnus-make-local-hook
(if (featurep 'xemacs
)
159 (defun gnus-delete-first (elt list
)
160 "Delete by side effect the first occurrence of ELT as a member of LIST."
161 (if (equal (car list
) elt
)
164 (while (and (cdr list
)
165 (not (equal (cadr list
) elt
)))
166 (setq list
(cdr list
)))
168 (setcdr list
(cddr list
)))
171 ;; Delete the current line (and the next N lines).
172 (defmacro gnus-delete-line
(&optional n
)
173 `(delete-region (point-at-bol)
174 (progn (forward-line ,(or n
1)) (point))))
176 (defun gnus-byte-code (func)
177 "Return a form that can be `eval'ed based on FUNC."
178 (let ((fval (indirect-function func
)))
179 (if (byte-code-function-p fval
)
180 (let ((flist (append fval nil
)))
181 (setcar flist
'byte-code
)
183 (cons 'progn
(cddr fval
)))))
185 (defun gnus-extract-address-components (from)
186 "Extract address components from a From header.
187 Given an RFC-822 address FROM, extract full name and canonical address.
188 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS). Much more simple
189 solution than `mail-extract-address-components', which works much better, but
192 ;; First find the address - the thing with the @ in it. This may
193 ;; not be accurate in mail addresses, but does the trick most of
194 ;; the time in news messages.
195 (cond (;; Check ``<foo@bar>'' first in order to handle the quite common
196 ;; form ``"abc@xyz" <foo@bar>'' (i.e. ``@'' as part of a comment)
198 (string-match "<\\([^@ \t<>]+[!@][^@ \t<>]+\\)>" from
)
199 (setq address
(substring from
(match-beginning 1) (match-end 1))))
200 ((string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from
)
201 (setq address
(substring from
(match-beginning 0) (match-end 0)))))
202 ;; Then we check whether the "name <address>" format is used.
204 ;; Linear white space is not required.
205 (string-match (concat "[ \t]*<" (regexp-quote address
) ">") from
)
206 (and (setq name
(substring from
0 (match-beginning 0)))
207 ;; Strip any quotes from the name.
208 (string-match "^\".*\"$" name
)
209 (setq name
(substring name
1 (1- (match-end 0))))))
210 ;; If not, then "address (name)" is used.
212 (and (string-match "(.+)" from
)
213 (setq name
(substring from
(1+ (match-beginning 0))
214 (1- (match-end 0)))))
215 (and (string-match "()" from
)
217 ;; XOVER might not support folded From headers.
218 (and (string-match "(.*" from
)
219 (setq name
(substring from
(1+ (match-beginning 0))
221 (list (if (string= name
"") nil name
) (or address from
))))
223 (defun gnus-extract-address-component-name (from)
224 "Extract name from a From header.
225 Uses `gnus-extract-address-components'."
226 (nth 0 (gnus-extract-address-components from
)))
228 (defun gnus-extract-address-component-email (from)
229 "Extract e-mail address from a From header.
230 Uses `gnus-extract-address-components'."
231 (nth 1 (gnus-extract-address-components from
)))
233 (declare-function message-fetch-field
"message" (header &optional not-all
))
235 (defun gnus-fetch-field (field)
236 "Return the value of the header FIELD of current article."
240 (let ((inhibit-point-motion-hooks t
))
241 (nnheader-narrow-to-headers)
242 (message-fetch-field field
)))))
244 (defun gnus-fetch-original-field (field)
245 "Fetch FIELD from the original version of the current article."
246 (with-current-buffer gnus-original-article-buffer
247 (gnus-fetch-field field
)))
250 (defun gnus-goto-colon ()
252 (let ((eol (point-at-eol)))
253 (goto-char (or (text-property-any (point) eol
'gnus-position t
)
254 (search-forward ":" eol t
)
257 (declare-function gnus-find-method-for-group
"gnus" (group &optional info
))
258 (declare-function gnus-group-name-decode
"gnus-group" (string charset
))
259 (declare-function gnus-group-name-charset
"gnus-group" (method group
))
260 ;; gnus-group requires gnus-int which requires message.
261 (declare-function message-tokenize-header
"message"
262 (header &optional separator
))
264 (defun gnus-decode-newsgroups (newsgroups group
&optional method
)
265 (require 'gnus-group
)
266 (let ((method (or method
(gnus-find-method-for-group group
))))
267 (mapconcat (lambda (group)
268 (gnus-group-name-decode group
(gnus-group-name-charset
270 (message-tokenize-header newsgroups
)
273 (defun gnus-remove-text-with-property (prop)
274 "Delete all text in the current buffer with text property PROP."
275 (let ((start (point-min))
277 (unless (get-text-property start prop
)
278 (setq start
(next-single-property-change start prop
)))
280 (setq end
(text-property-any start
(point-max) prop nil
))
281 (delete-region start
(or end
(point-max)))
282 (setq start
(when end
283 (next-single-property-change start prop
))))))
285 (defun gnus-newsgroup-directory-form (newsgroup)
286 "Make hierarchical directory name from NEWSGROUP name."
287 (let* ((newsgroup (gnus-newsgroup-savable-name newsgroup
))
288 (idx (string-match ":" newsgroup
)))
290 (if idx
(substring newsgroup
0 idx
))
292 (nnheader-replace-chars-in-string
293 (if idx
(substring newsgroup
(1+ idx
)) newsgroup
)
296 (defun gnus-newsgroup-savable-name (group)
297 ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
299 (nnheader-replace-chars-in-string group ?
/ ?.
))
301 (defun gnus-string> (s1 s2
)
302 (not (or (string< s1 s2
)
305 (defun gnus-string< (s1 s2
)
306 "Return t if first arg string is less than second in lexicographic order.
307 Case is significant if and only if `case-fold-search' is nil.
308 Symbols are also allowed; their print names are used instead."
310 (string-lessp (downcase (if (symbolp s1
) (symbol-name s1
) s1
))
311 (downcase (if (symbolp s2
) (symbol-name s2
) s2
)))
312 (string-lessp s1 s2
)))
316 (defun gnus-file-newer-than (file date
)
317 (let ((fdate (nth 5 (file-attributes file
))))
318 (or (> (car fdate
) (car date
))
319 (and (= (car fdate
) (car date
))
320 (> (nth 1 fdate
) (nth 1 date
))))))
323 (if (and (fboundp 'float-time
)
324 (subrp (symbol-function 'float-time
)))
325 (defalias 'gnus-float-time
'float-time
)
326 (defun gnus-float-time (&optional time
)
327 "Convert time value TIME to a floating point number.
328 TIME defaults to the current time."
329 (with-no-warnings (time-to-seconds (or time
(current-time)))))))
333 (defmacro gnus-local-set-keys
(&rest plist
)
334 "Set the keys in PLIST in the current keymap."
335 `(gnus-define-keys-1 (current-local-map) ',plist
))
337 (defmacro gnus-define-keys
(keymap &rest plist
)
338 "Define all keys in PLIST in KEYMAP."
339 `(gnus-define-keys-1 (quote ,keymap
) (quote ,plist
)))
341 (defmacro gnus-define-keys-safe
(keymap &rest plist
)
342 "Define all keys in PLIST in KEYMAP without overwriting previous definitions."
343 `(gnus-define-keys-1 (quote ,keymap
) (quote ,plist
) t
))
345 (put 'gnus-define-keys
'lisp-indent-function
1)
346 (put 'gnus-define-keys-safe
'lisp-indent-function
1)
347 (put 'gnus-local-set-keys
'lisp-indent-function
1)
349 (defmacro gnus-define-keymap
(keymap &rest plist
)
350 "Define all keys in PLIST in KEYMAP."
351 `(gnus-define-keys-1 ,keymap
(quote ,plist
)))
353 (put 'gnus-define-keymap
'lisp-indent-function
1)
355 (defun gnus-define-keys-1 (keymap plist
&optional safe
)
357 (error "Can't set keys in a null keymap"))
358 (cond ((symbolp keymap
)
359 (setq keymap
(symbol-value keymap
)))
362 (set (car keymap
) nil
)
363 (define-prefix-command (car keymap
))
364 (define-key (symbol-value (caddr keymap
)) (cadr keymap
) (car keymap
))
365 (setq keymap
(symbol-value (car keymap
)))))
368 (when (symbolp (setq key
(pop plist
)))
369 (setq key
(symbol-value key
)))
371 (eq (lookup-key keymap key
) 'undefined
))
372 (define-key keymap key
(pop plist
))
375 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
378 ;; Do we really need these functions? Workarounds for bugs in the corresponding
379 ;; Emacs functions? Maybe these bugs are no longer present in any supported
380 ;; (X)Emacs version? Alias them to the original functions and see if anyone
381 ;; reports a problem. If not, replace with original functions. --rsteib,
384 ;; All supported Emacsen clear the echo area after `yes-or-no-p', so we can
385 ;; remove `yes-or-no-p'. RMS says that not clearing after `y-or-n-p' is
386 ;; intentional (see below), so we could remove `gnus-y-or-n-p' too.
387 ;; Objections? --rsteib, 2008-02-16
389 ;; ,----[ http://thread.gmane.org/gmane.emacs.gnus.general/65099/focus=66070 ]
390 ;; | From: Richard Stallman
391 ;; | Subject: Re: Do we need gnus-yes-or-no-p and gnus-y-or-n-p?
392 ;; | To: Katsumi Yamaoka [...]
393 ;; | Cc: emacs-devel@[...], xemacs-beta@[...], ding@[...]
394 ;; | Date: Mon, 07 Jan 2008 12:16:05 -0500
395 ;; | Message-ID: <E1JBva1-000528-VY@fencepost.gnu.org>
397 ;; | The behavior of `y-or-n-p' that it doesn't clear the question
398 ;; | and the answer is not serious of course, but I feel it is not
401 ;; | It is intentional.
403 ;; | Currently, it is commented out in the trunk by Reiner Steib. He
404 ;; | also wrote the benefit of leaving the question and the answer in
405 ;; | the echo area as follows:
407 ;; | (http://article.gmane.org/gmane.emacs.gnus.general/66061)
408 ;; | > In contrast to yes-or-no-p it is much easier to type y, n,
409 ;; | > SPC, DEL, etc accidentally, so it might be useful for the user
410 ;; | > to see what he has typed.
412 ;; | Yes, that is the reason.
415 ;; (defun gnus-y-or-n-p (prompt)
419 ;; (defun gnus-yes-or-no-p (prompt)
421 ;; (yes-or-no-p prompt)
424 (defalias 'gnus-y-or-n-p
'y-or-n-p
)
425 (defalias 'gnus-yes-or-no-p
'yes-or-no-p
)
427 ;; By Frank Schmitt <ich@Frank-Schmitt.net>. Allows to have
428 ;; age-depending date representations. (e.g. just the time if it's
429 ;; from today, the day of the week if it's within the last 7 days and
430 ;; the full date if it's older)
432 (defun gnus-seconds-today ()
433 "Return the number of seconds passed today."
434 (let ((now (decode-time (current-time))))
435 (+ (car now
) (* (car (cdr now
)) 60) (* (car (nthcdr 2 now
)) 3600))))
437 (defun gnus-seconds-month ()
438 "Return the number of seconds passed this month."
439 (let ((now (decode-time (current-time))))
440 (+ (car now
) (* (car (cdr now
)) 60) (* (car (nthcdr 2 now
)) 3600)
441 (* (- (car (nthcdr 3 now
)) 1) 3600 24))))
443 (defun gnus-seconds-year ()
444 "Return the number of seconds passed this year."
445 (let ((now (decode-time (current-time)))
446 (days (format-time-string "%j" (current-time))))
447 (+ (car now
) (* (car (cdr now
)) 60) (* (car (nthcdr 2 now
)) 3600)
448 (* (- (string-to-number days
) 1) 3600 24))))
450 (defmacro gnus-date-get-time
(date)
451 "Convert DATE string to Emacs time.
452 Cache the result as a text property stored in DATE."
453 ;; Either return the cached value...
457 (or (get-text-property 0 'gnus-time d
)
458 ;; or compute the value...
459 (let ((time (safe-date-to-time d
)))
460 ;; and store it back in the string.
461 (put-text-property 0 1 'gnus-time time d
)
464 (defvar gnus-user-date-format-alist
465 '(((gnus-seconds-today) .
"%k:%M")
466 (604800 .
"%a %k:%M") ;;that's one week
467 ((gnus-seconds-month) .
"%a %d")
468 ((gnus-seconds-year) .
"%b %d")
469 (t .
"%b %d '%y")) ;;this one is used when no
471 "Specifies date format depending on age of article.
472 This is an alist of items (AGE . FORMAT). AGE can be a number (of
473 seconds) or a Lisp expression evaluating to a number. When the age of
474 the article is less than this number, then use `format-time-string'
475 with the corresponding FORMAT for displaying the date of the article.
476 If AGE is not a number or a Lisp expression evaluating to a
477 non-number, then the corresponding FORMAT is used as a default value.
479 Note that the list is processed from the beginning, so it should be
480 sorted by ascending AGE. Also note that items following the first
481 non-number AGE will be ignored.
483 You can use the functions `gnus-seconds-today', `gnus-seconds-month'
484 and `gnus-seconds-year' in the AGE spec. They return the number of
485 seconds passed since the start of today, of this month, of this year,
488 (defun gnus-user-date (messy-date)
489 "Format the messy-date according to gnus-user-date-format-alist.
490 Returns \" ? \" if there's bad input or if another error occurs.
491 Input should look like this: \"Sun, 14 Oct 2001 13:34:39 +0200\"."
493 (let* ((messy-date (gnus-float-time (gnus-date-get-time messy-date
)))
494 (now (gnus-float-time))
495 ;;If we don't find something suitable we'll use this one
496 (my-format "%b %d '%y"))
497 (let* ((difference (- now messy-date
))
498 (templist gnus-user-date-format-alist
)
499 (top (eval (caar templist
))))
500 (while (if (numberp top
) (< top difference
) (not top
))
502 (setq templist
(cdr templist
))
503 (setq top
(eval (caar templist
)))))
504 (if (stringp (cdr (car templist
)))
505 (setq my-format
(cdr (car templist
)))))
506 (format-time-string (eval my-format
) (seconds-to-time messy-date
)))
509 (defun gnus-dd-mmm (messy-date)
510 "Return a string like DD-MMM from a big messy string."
512 (format-time-string "%d-%b" (gnus-date-get-time messy-date
))
515 (defsubst gnus-time-iso8601
(time)
516 "Return a string of TIME in YYYYMMDDTHHMMSS format."
517 (format-time-string "%Y%m%dT%H%M%S" time
))
519 (defun gnus-date-iso8601 (date)
520 "Convert the DATE to YYYYMMDDTHHMMSS."
522 (gnus-time-iso8601 (gnus-date-get-time date
))
525 (defun gnus-mode-string-quote (string)
526 "Quote all \"%\"'s in STRING."
527 (gnus-replace-in-string string
"%" "%%"))
529 ;; Make a hash table (default and minimum size is 256).
530 ;; Optional argument HASHSIZE specifies the table size.
531 (defun gnus-make-hashtable (&optional hashsize
)
532 (make-vector (if hashsize
(max (gnus-create-hash-size hashsize
) 256) 256) 0))
534 ;; Make a number that is suitable for hashing; bigger than MIN and
535 ;; equal to some 2^x. Many machines (such as sparcs) do not have a
536 ;; hardware modulo operation, so they implement it in software. On
537 ;; many sparcs over 50% of the time to intern is spent in the modulo.
538 ;; Yes, it's slower than actually computing the hash from the string!
539 ;; So we use powers of 2 so people can optimize the modulo to a mask.
540 (defun gnus-create-hash-size (min)
546 (defcustom gnus-verbose
7
547 "*Integer that says how verbose Gnus should be.
548 The higher the number, the more messages Gnus will flash to say what
549 it's doing. At zero, Gnus will be totally mute; at five, Gnus will
550 display most important messages; and at ten, Gnus will keep on
551 jabbering all the time."
555 (defcustom gnus-add-timestamp-to-message nil
556 "Non-nil means add timestamps to messages that Gnus issues.
557 If it is `log', add timestamps to only the messages that go into the
558 \"*Messages*\" buffer (in XEmacs, it is the \" *Message-Log*\" buffer).
559 If it is neither nil nor `log', add timestamps not only to log messages
560 but also to the ones displayed in the echo area."
561 :version
"23.1" ;; No Gnus
563 :type
'(choice :format
"%{%t%}:\n %[Value Menu%] %v"
564 (const :tag
"Logged messages only" log
)
565 (sexp :tag
"All messages"
566 :match
(lambda (widget value
) value
)
568 (const :tag
"No timestamp" nil
)))
571 (defmacro gnus-message-with-timestamp-1
(format-string args
)
572 (let ((timestamp '((format-time-string "%Y%m%dT%H%M%S" time
)
573 "." (format "%03d" (/ (nth 2 time
) 1000)) "> ")))
574 (if (featurep 'xemacs
)
576 (if (or (and (null ,format-string
) (null ,args
))
578 (setq str
(apply 'format
,format-string
,args
))
579 (zerop (length str
))))
581 (and ,format-string str
)
583 (cond ((eq gnus-add-timestamp-to-message
'log
)
584 (setq time
(current-time))
585 (display-message 'no-log str
)
586 (log-message 'message
(concat ,@timestamp str
)))
587 (gnus-add-timestamp-to-message
588 (setq time
(current-time))
589 (display-message 'message
(concat ,@timestamp str
)))
591 (display-message 'message str
))))
594 (cond ((eq gnus-add-timestamp-to-message
'log
)
595 (setq str
(let (message-log-max)
596 (apply 'message
,format-string
,args
)))
597 (when (and message-log-max
598 (> message-log-max
0)
600 (setq time
(current-time))
601 (with-current-buffer (get-buffer-create "*Messages*")
602 (goto-char (point-max))
603 (insert ,@timestamp str
"\n")
604 (forward-line (- message-log-max
))
605 (delete-region (point-min) (point))
606 (goto-char (point-max))))
608 (gnus-add-timestamp-to-message
609 (if (or (and (null ,format-string
) (null ,args
))
611 (setq str
(apply 'format
,format-string
,args
))
612 (zerop (length str
))))
614 (and ,format-string str
)
616 (setq time
(current-time))
617 (message "%s" (concat ,@timestamp str
))
620 (apply 'message
,format-string
,args
))))))))
622 (defvar gnus-action-message-log nil
)
624 (defun gnus-message-with-timestamp (format-string &rest args
)
625 "Display message with timestamp. Arguments are the same as `message'.
626 The `gnus-add-timestamp-to-message' variable controls how to add
627 timestamp to message."
628 (gnus-message-with-timestamp-1 format-string args
))
630 (defun gnus-message (level &rest args
)
631 "If LEVEL is lower than `gnus-verbose' print ARGS using `message'.
633 Guideline for numbers:
634 1 - error messages, 3 - non-serious error messages, 5 - messages for things
635 that take a long time, 7 - not very important messages on stuff, 9 - messages
637 (if (<= level gnus-verbose
)
639 (if gnus-add-timestamp-to-message
640 (apply 'gnus-message-with-timestamp args
)
641 (apply 'message args
))))
642 (when (and (consp gnus-action-message-log
)
644 (push message gnus-action-message-log
))
646 ;; We have to do this format thingy here even if the result isn't
647 ;; shown - the return value has to be the same as the return value
649 (apply 'format args
)))
651 (defun gnus-final-warning ()
652 (when (and (consp gnus-action-message-log
)
653 (setq gnus-action-message-log
654 (delete nil gnus-action-message-log
)))
655 (message "Warning: %s"
656 (mapconcat #'identity gnus-action-message-log
"; "))))
658 (defun gnus-error (level &rest args
)
659 "Beep an error if LEVEL is equal to or less than `gnus-verbose'.
660 ARGS are passed to `message'."
661 (when (<= (floor level
) gnus-verbose
)
662 (apply 'message args
)
665 (when (and (floatp level
)
666 (not (zerop (setq duration
(* 10 (- level
(floor level
)))))))
667 (sit-for duration
))))
670 (defun gnus-split-references (references)
671 "Return a list of Message-IDs in REFERENCES."
673 (references (mail-header-remove-comments (or references
"")))
675 (while (string-match "<[^<]+[^< \t]" references beg
)
676 (push (substring references
(match-beginning 0) (setq beg
(match-end 0)))
680 (defun gnus-extract-references (references)
681 "Return a list of Message-IDs in REFERENCES (in In-Reply-To
682 format), trimmed to only contain the Message-IDs."
683 (let ((ids (gnus-split-references references
))
686 (when (string-match "<[^<>]+>" id
)
687 (push (match-string 0 id
) refs
)))
690 (defsubst gnus-parent-id
(references &optional n
)
691 "Return the last Message-ID in REFERENCES.
692 If N, return the Nth ancestor instead."
693 (when (and references
694 (not (zerop (length references
))))
696 (let ((ids (inline (gnus-split-references references
))))
697 (while (nthcdr n ids
)
698 (setq ids
(cdr ids
)))
700 (let ((references (mail-header-remove-comments references
)))
701 (when (string-match "\\(<[^<]+>\\)[ \t]*\\'" references
)
702 (match-string 1 references
))))))
704 (defun gnus-buffer-live-p (buffer)
705 "Say whether BUFFER is alive or not."
708 (buffer-name (get-buffer buffer
))))
710 (defun gnus-horizontal-recenter ()
711 "Recenter the current buffer horizontally."
712 (if (< (current-column) (/ (window-width) 2))
713 (set-window-hscroll (gnus-get-buffer-window (current-buffer) t
) 0)
714 (let* ((orig (point))
715 (end (window-end (gnus-get-buffer-window (current-buffer) t
)))
718 ;; Find the longest line currently displayed in the window.
719 (goto-char (window-start))
720 (while (and (not (eobp))
723 (setq max
(max max
(current-column)))
726 ;; Scroll horizontally to center (sort of) the point.
727 (if (> max
(window-width))
729 (gnus-get-buffer-window (current-buffer) t
)
730 (min (- (current-column) (/ (window-width) 3))
731 (+ 2 (- max
(window-width)))))
732 (set-window-hscroll (gnus-get-buffer-window (current-buffer) t
) 0))
735 (defun gnus-read-event-char (&optional prompt
)
736 "Get the next event."
737 (let ((event (read-event prompt
)))
738 ;; should be gnus-characterp, but this can't be called in XEmacs anyway
739 (cons (and (numberp event
) event
) event
)))
741 (defun gnus-sortable-date (date)
742 "Make string suitable for sorting from DATE."
743 (gnus-time-iso8601 (date-to-time date
)))
745 (defun gnus-copy-file (file &optional to
)
748 (list (read-file-name "Copy file: " default-directory
)
749 (read-file-name "Copy file to: " default-directory
)))
751 (setq to
(read-file-name "Copy file to: " default-directory
)))
752 (when (file-directory-p to
)
753 (setq to
(concat (file-name-as-directory to
)
754 (file-name-nondirectory file
))))
757 (defvar gnus-work-buffer
" *gnus work*")
759 (declare-function gnus-get-buffer-create
"gnus" (name))
760 ;; gnus.el requires mm-util.
761 (declare-function mm-enable-multibyte
"mm-util")
763 (defun gnus-set-work-buffer ()
764 "Put point in the empty Gnus work buffer."
765 (if (get-buffer gnus-work-buffer
)
767 (set-buffer gnus-work-buffer
)
769 (set-buffer (gnus-get-buffer-create gnus-work-buffer
))
770 (kill-all-local-variables)
771 (mm-enable-multibyte)))
773 (defmacro gnus-group-real-name
(group)
774 "Find the real name of a foreign newsgroup."
775 `(let ((gname ,group
))
776 (if (string-match "^[^:]+:" gname
)
777 (substring gname
(match-end 0))
780 (defmacro gnus-group-server
(group)
781 "Find the server name of a foreign newsgroup.
782 For example, (gnus-group-server \"nnimap+yxa:INBOX.foo\") would
783 yield \"nnimap:yxa\"."
784 `(let ((gname ,group
))
785 (if (string-match "^\\([^:+]+\\)\\(?:\\+\\([^:]*\\)\\)?:" gname
)
786 (format "%s:%s" (match-string 1 gname
) (or
787 (match-string 2 gname
)
789 (format "%s:%s" (car gnus-select-method
) (cadr gnus-select-method
)))))
791 (defun gnus-make-sort-function (funs)
792 "Return a composite sort condition based on the functions in FUNS."
794 ;; Just a simple function.
795 ((functionp funs
) funs
)
796 ;; No functions at all.
798 ;; A list of functions.
803 ,(gnus-make-sort-function-1 (reverse funs
)))))
804 ;; A list containing just one function.
808 (defun gnus-make-sort-function-1 (funs)
809 "Return a composite sort condition based on the functions in FUNS."
810 (let ((function (car funs
))
813 (when (consp function
)
816 ((eq (car function
) 'not
)
817 (setq function
(cadr function
)
820 ((functionp function
)
824 (error "Invalid sort spec: %s" function
))))
826 `(or (,function
,first
,last
)
827 (and (not (,function
,last
,first
))
828 ,(gnus-make-sort-function-1 (cdr funs
))))
829 `(,function
,first
,last
))))
831 (defun gnus-turn-off-edit-menu (type)
832 "Turn off edit menu in `gnus-TYPE-mode-map'."
833 (define-key (symbol-value (intern (format "gnus-%s-mode-map" type
)))
834 [menu-bar edit
] 'undefined
))
836 (defmacro gnus-bind-print-variables
(&rest forms
)
837 "Bind print-* variables and evaluate FORMS.
838 This macro is used with `prin1', `pp', etc. in order to ensure printed
839 Lisp objects are loadable. Bind `print-quoted' and `print-readably'
840 to t, and `print-escape-multibyte', `print-escape-newlines',
841 `print-escape-nonascii', `print-length', `print-level' and
842 `print-string-length' to nil."
843 `(let ((print-quoted t
)
846 ;;print-continuous-numbering
847 print-escape-multibyte
848 print-escape-newlines
849 print-escape-nonascii
856 (defun gnus-prin1 (form)
857 "Use `prin1' on FORM in the current buffer.
858 Bind `print-quoted' and `print-readably' to t, and `print-length' and
859 `print-level' to nil. See also `gnus-bind-print-variables'."
860 (gnus-bind-print-variables (prin1 form
(current-buffer))))
862 (defun gnus-prin1-to-string (form)
863 "The same as `prin1'.
864 Bind `print-quoted' and `print-readably' to t, and `print-length' and
865 `print-level' to nil. See also `gnus-bind-print-variables'."
866 (gnus-bind-print-variables (prin1-to-string form
)))
868 (defun gnus-pp (form &optional stream
)
869 "Use `pp' on FORM in the current buffer.
870 Bind `print-quoted' and `print-readably' to t, and `print-length' and
871 `print-level' to nil. See also `gnus-bind-print-variables'."
872 (gnus-bind-print-variables (pp form
(or stream
(current-buffer)))))
874 (defun gnus-pp-to-string (form)
875 "The same as `pp-to-string'.
876 Bind `print-quoted' and `print-readably' to t, and `print-length' and
877 `print-level' to nil. See also `gnus-bind-print-variables'."
878 (gnus-bind-print-variables (pp-to-string form
)))
880 (defun gnus-make-directory (directory)
881 "Make DIRECTORY (and all its parents) if it doesn't exist."
883 (let ((file-name-coding-system nnmail-pathname-coding-system
))
885 (not (file-exists-p directory
)))
886 (make-directory directory t
)))
889 (defun gnus-write-buffer (file)
890 "Write the current buffer's contents to FILE."
891 (let ((file-name-coding-system nnmail-pathname-coding-system
))
892 ;; Make sure the directory exists.
893 (gnus-make-directory (file-name-directory file
))
895 (write-region (point-min) (point-max) file nil
'quietly
)))
897 (defun gnus-delete-file (file)
898 "Delete FILE if it exists."
899 (when (file-exists-p file
)
902 (defun gnus-delete-directory (directory)
903 "Delete files in DIRECTORY. Subdirectories remain.
904 If there's no subdirectory, delete DIRECTORY as well."
905 (when (file-directory-p directory
)
906 (let ((files (directory-files
907 directory t
"^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
910 (setq file
(pop files
))
911 (if (eq t
(car (file-attributes file
)))
912 ;; `file' is a subdirectory.
914 ;; `file' is a file or a symlink.
917 (delete-directory directory
)))))
919 ;; The following two functions are used in gnus-registry.
920 ;; They were contributed by Andreas Fuchs <asf@void.at>.
921 (defun gnus-alist-to-hashtable (alist)
922 "Build a hashtable from the values in ALIST."
923 (let ((ht (make-hash-table
928 (puthash (car kv-pair
) (cdr kv-pair
) ht
))
932 (defun gnus-hashtable-to-alist (hash)
933 "Build an alist from the values in HASH."
937 (setq list
(cons (cons key value
) list
)))
941 (defun gnus-strip-whitespace (string)
942 "Return STRING stripped of all whitespace."
943 (while (string-match "[\r\n\t ]+" string
)
944 (setq string
(replace-match "" t t string
)))
947 (declare-function gnus-put-text-property
"gnus"
948 (start end property value
&optional object
))
950 (defsubst gnus-put-text-property-excluding-newlines
(beg end prop val
)
951 "The same as `put-text-property', but don't put this prop on any newlines in the region."
956 (while (re-search-forward gnus-emphasize-whitespace-regexp end
'move
)
957 (gnus-put-text-property beg
(match-beginning 0) prop val
)
959 (gnus-put-text-property beg
(point) prop val
)))))
961 (declare-function gnus-overlay-put
"gnus" (overlay prop value
))
962 (declare-function gnus-make-overlay
"gnus"
963 (beg end
&optional buffer front-advance rear-advance
))
965 (defsubst gnus-put-overlay-excluding-newlines
(beg end prop val
)
966 "The same as `put-text-property', but don't put this prop on any newlines in the region."
971 (while (re-search-forward gnus-emphasize-whitespace-regexp end
'move
)
973 (gnus-make-overlay beg
(match-beginning 0))
976 (gnus-overlay-put (gnus-make-overlay beg
(point)) prop val
)))))
978 (defun gnus-put-text-property-excluding-characters-with-faces (beg end
980 "The same as `put-text-property', but don't put props on characters with the `gnus-face' property."
983 (when (get-text-property b
'gnus-face
)
984 (setq b
(next-single-property-change b
'gnus-face nil end
)))
987 (gnus-put-text-property
988 b
(setq b
(next-single-property-change b
'gnus-face nil end
))
991 (defmacro gnus-faces-at
(position)
992 "Return a list of faces at POSITION."
993 (if (featurep 'xemacs
)
994 `(let ((pos ,position
))
995 (mapcar-extents 'extent-face
996 nil
(current-buffer) pos pos nil
'face
))
997 `(let ((pos ,position
))
998 (delq nil
(cons (get-text-property pos
'face
)
1001 (overlay-get overlay
'face
))
1002 (overlays-at pos
)))))))
1004 (if (fboundp 'invisible-p
)
1005 (defalias 'gnus-invisible-p
'invisible-p
)
1006 ;; for Emacs < 22.2, and XEmacs.
1007 (defun gnus-invisible-p (pos)
1008 "Return non-nil if the character after POS is currently invisible."
1009 (let ((prop (get-char-property pos
'invisible
)))
1010 (if (eq buffer-invisibility-spec t
)
1012 (or (memq prop buffer-invisibility-spec
)
1013 (assq prop buffer-invisibility-spec
))))))
1015 ;; Note: the optional 2nd argument has a different meaning between
1016 ;; Emacs and XEmacs.
1017 ;; (next-char-property-change POSITION &optional LIMIT)
1018 ;; (next-extent-change POS &optional OBJECT)
1019 (defalias 'gnus-next-char-property-change
1020 (if (fboundp 'next-extent-change
)
1021 'next-extent-change
'next-char-property-change
))
1023 (defalias 'gnus-previous-char-property-change
1024 (if (fboundp 'previous-extent-change
)
1025 'previous-extent-change
'previous-char-property-change
))
1027 ;;; Protected and atomic operations. dmoore@ucsd.edu 21.11.1996
1028 ;; The primary idea here is to try to protect internal datastructures
1029 ;; from becoming corrupted when the user hits C-g, or if a hook or
1030 ;; similar blows up. Often in Gnus multiple tables/lists need to be
1031 ;; updated at the same time, or information can be lost.
1033 (defvar gnus-atomic-be-safe t
1034 "If t, certain operations will be protected from interruption by C-g.")
1036 (defmacro gnus-atomic-progn
(&rest forms
)
1037 "Evaluate FORMS atomically, which means to protect the evaluation
1038 from being interrupted by the user. An error from the forms themselves
1039 will return without finishing the operation. Since interrupts from
1040 the user are disabled, it is recommended that only the most minimal
1041 operations are performed by FORMS. If you wish to assign many
1042 complicated values atomically, compute the results into temporary
1043 variables and then do only the assignment atomically."
1044 `(let ((inhibit-quit gnus-atomic-be-safe
))
1047 (put 'gnus-atomic-progn
'lisp-indent-function
0)
1049 (defmacro gnus-atomic-progn-assign
(protect &rest forms
)
1050 "Evaluate FORMS, but ensure that the variables listed in PROTECT
1051 are not changed if anything in FORMS signals an error or otherwise
1052 non-locally exits. The variables listed in PROTECT are updated atomically.
1053 It is safe to use gnus-atomic-progn-assign with long computations.
1055 Note that if any of the symbols in PROTECT were unbound, they will be
1056 set to nil on a successful assignment. In case of an error or other
1057 non-local exit, it will still be unbound."
1058 (let* ((temp-sym-map (mapcar (lambda (x) (list (make-symbol
1059 (concat (symbol-name x
)
1063 (sym-temp-map (mapcar (lambda (x) (list (cadr x
) (car x
)))
1065 (temp-sym-let (mapcar (lambda (x) (list (car x
)
1066 `(and (boundp ',(cadr x
))
1069 (sym-temp-let sym-temp-map
)
1070 (temp-sym-assign (apply 'append temp-sym-map
))
1071 (sym-temp-assign (apply 'append sym-temp-map
))
1072 (result (make-symbol "result-tmp")))
1073 `(let (,@temp-sym-let
1076 (setq ,result
(progn ,@forms
))
1077 (setq ,@temp-sym-assign
))
1078 (let ((inhibit-quit gnus-atomic-be-safe
))
1079 (setq ,@sym-temp-assign
))
1082 (put 'gnus-atomic-progn-assign
'lisp-indent-function
1)
1083 ;(put 'gnus-atomic-progn-assign 'edebug-form-spec '(sexp body))
1085 (defmacro gnus-atomic-setq
(&rest pairs
)
1086 "Similar to setq, except that the real symbols are only assigned when
1087 there are no errors. And when the real symbols are assigned, they are
1088 done so atomically. If other variables might be changed via side-effect,
1089 see gnus-atomic-progn-assign. It is safe to use gnus-atomic-setq
1090 with potentially long computations."
1091 (let ((tpairs pairs
)
1094 (push (car tpairs
) syms
)
1095 (setq tpairs
(cddr tpairs
)))
1096 `(gnus-atomic-progn-assign ,syms
1099 ;(put 'gnus-atomic-setq 'edebug-form-spec '(body))
1102 ;;; Functions for saving to babyl/mail files.
1105 (if (featurep 'xemacs
)
1106 ;; Don't load tm and apel XEmacs packages that provide some
1107 ;; Emacs emulating functions and variables.
1108 (let ((features features
))
1110 (unless (fboundp 'set-alist
) (defalias 'set-alist
'ignore
))
1111 (require 'rmail
)) ;; It requires tm-view that loads apel.
1113 (autoload 'rmail-update-summary
"rmailsum"))
1115 (defvar mm-text-coding-system
)
1117 (declare-function mm-append-to-file
"mm-util"
1118 (start end filename
&optional codesys inhibit
))
1120 (defun gnus-output-to-rmail (filename &optional ask
)
1121 "Append the current article to an Rmail file named FILENAME.
1122 In Emacs 22 this writes Babyl format; in Emacs 23 it writes mbox unless
1123 FILENAME exists and is Babyl format."
1126 ;; Some of this codes is borrowed from rmailout.el.
1127 (setq filename
(expand-file-name filename
))
1128 ;; FIXME should we really be messing with this defcustom?
1129 ;; It is not needed for the operation of this function.
1130 (if (boundp 'rmail-default-rmail-file
)
1131 (setq rmail-default-rmail-file filename
) ; 22
1132 (setq rmail-default-file filename
)) ; 23
1133 (let ((artbuf (current-buffer))
1134 (tmpbuf (get-buffer-create " *Gnus-output*"))
1135 ;; Babyl rmail.el defines this, mbox does not.
1136 (babyl (fboundp 'rmail-insert-rmail-file-header
)))
1138 ;; Note that we ignore the possibility of visiting a Babyl
1139 ;; format buffer in Emacs 23, since Rmail no longer supports that.
1140 (or (get-file-buffer filename
)
1142 ;; In case someone wants to write to a Babyl file from Emacs 23.
1143 (when (file-exists-p filename
)
1144 (setq babyl
(mail-file-babyl-p filename
))
1148 (concat "\"" filename
"\" does not exist, create it? ")))
1149 (let ((file-buffer (create-file-buffer filename
)))
1150 (with-current-buffer file-buffer
1151 (if (fboundp 'rmail-insert-rmail-file-header
)
1152 (rmail-insert-rmail-file-header))
1153 (let ((require-final-newline nil
)
1154 (coding-system-for-write mm-text-coding-system
))
1155 (gnus-write-buffer filename
)))
1156 (kill-buffer file-buffer
))
1157 (error "Output file does not exist")))
1160 (insert-buffer-substring artbuf
)
1162 (gnus-convert-article-to-rmail)
1163 ;; Non-Babyl case copied from gnus-output-to-mail.
1164 (goto-char (point-min))
1165 (if (looking-at "From ")
1167 (insert "From nobody " (current-time-string) "\n"))
1168 (let (case-fold-search)
1169 (while (re-search-forward "^From " nil t
)
1172 ;; Decide whether to append to a file or to an Emacs buffer.
1173 (let ((outbuf (get-file-buffer filename
)))
1176 (unless babyl
; from gnus-output-to-mail
1177 (let ((buffer-read-only nil
))
1178 (goto-char (point-max))
1180 (unless (looking-at "\n\n")
1181 (goto-char (point-max))
1185 (let ((file-name-coding-system nnmail-pathname-coding-system
))
1186 (mm-append-to-file (point-min) (point-max) filename
)))
1187 ;; File has been visited, in buffer OUTBUF.
1189 (let ((buffer-read-only nil
)
1190 (msg (and (boundp 'rmail-current-message
)
1191 (symbol-value 'rmail-current-message
))))
1192 ;; If MSG is non-nil, buffer is in RMAIL mode.
1193 ;; Compare this with rmail-output-to-rmail-buffer in Emacs 23.
1196 (rmail-swap-buffers-maybe)
1197 (rmail-maybe-set-message-counters))
1199 (narrow-to-region (point-max) (point-max)))
1200 (insert-buffer-substring tmpbuf
)
1203 (goto-char (point-min))
1205 (search-backward "\n\^_")
1206 (narrow-to-region (point) (point-max)))
1207 (rmail-count-new-messages t
)
1208 (when (rmail-summary-exists)
1209 (rmail-select-summary
1210 (rmail-update-summary)))
1211 (rmail-show-message msg
))
1213 (kill-buffer tmpbuf
)))
1215 (defun gnus-output-to-mail (filename &optional ask
)
1216 "Append the current article to a mail file named FILENAME."
1217 (setq filename
(expand-file-name filename
))
1218 (let ((artbuf (current-buffer))
1219 (tmpbuf (get-buffer-create " *Gnus-output*")))
1221 ;; Create the file, if it doesn't exist.
1222 (when (and (not (get-file-buffer filename
))
1223 (not (file-exists-p filename
)))
1226 (concat "\"" filename
"\" does not exist, create it? ")))
1227 (let ((file-buffer (create-file-buffer filename
)))
1228 (with-current-buffer file-buffer
1229 (let ((require-final-newline nil
)
1230 (coding-system-for-write mm-text-coding-system
))
1231 (gnus-write-buffer filename
)))
1232 (kill-buffer file-buffer
))
1233 (error "Output file does not exist")))
1236 (insert-buffer-substring artbuf
)
1237 (goto-char (point-min))
1238 (if (looking-at "From ")
1240 (insert "From nobody " (current-time-string) "\n"))
1241 (let (case-fold-search)
1242 (while (re-search-forward "^From " nil t
)
1245 ;; Decide whether to append to a file or to an Emacs buffer.
1246 (let ((outbuf (get-file-buffer filename
)))
1248 (let ((buffer-read-only nil
))
1250 (goto-char (point-max))
1252 (unless (looking-at "\n\n")
1253 (goto-char (point-max))
1257 (goto-char (point-max))
1258 (let ((file-name-coding-system nnmail-pathname-coding-system
))
1259 (mm-append-to-file (point-min) (point-max) filename
))))
1260 ;; File has been visited, in buffer OUTBUF.
1262 (let ((buffer-read-only nil
))
1263 (goto-char (point-max))
1267 (insert-buffer-substring tmpbuf
)))))
1268 (kill-buffer tmpbuf
)))
1270 (defun gnus-convert-article-to-rmail ()
1271 "Convert article in current buffer to Rmail message format."
1272 (let ((buffer-read-only nil
))
1273 ;; Convert article directly into Babyl format.
1274 (goto-char (point-min))
1275 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
1276 (while (search-forward "\n\^_" nil t
) ;single char
1277 (replace-match "\n^_" t t
)) ;2 chars: "^" and "_"
1278 (goto-char (point-max))
1281 (defun gnus-map-function (funs arg
)
1282 "Apply the result of the first function in FUNS to the second, and so on.
1283 ARG is passed to the first function."
1285 (setq arg
(funcall (pop funs
) arg
)))
1288 (defun gnus-run-hooks (&rest funcs
)
1289 "Does the same as `run-hooks', but saves the current buffer."
1290 (save-current-buffer
1291 (apply 'run-hooks funcs
)))
1293 (defun gnus-run-hook-with-args (hook &rest args
)
1294 "Does the same as `run-hook-with-args', but saves the current buffer."
1295 (save-current-buffer
1296 (apply 'run-hook-with-args hook args
)))
1298 (defun gnus-run-mode-hooks (&rest funcs
)
1299 "Run `run-mode-hooks' if it is available, otherwise `run-hooks'.
1300 This function saves the current buffer."
1301 (if (fboundp 'run-mode-hooks
)
1302 (save-current-buffer (apply 'run-mode-hooks funcs
))
1303 (save-current-buffer (apply 'run-hooks funcs
))))
1307 (defvar gnus-group-buffer
) ; Compiler directive
1308 (defun gnus-alive-p ()
1309 "Say whether Gnus is running or not."
1310 (and (boundp 'gnus-group-buffer
)
1311 (get-buffer gnus-group-buffer
)
1312 (with-current-buffer gnus-group-buffer
1313 (eq major-mode
'gnus-group-mode
))))
1315 (defun gnus-remove-if (predicate sequence
&optional hash-table-p
)
1316 "Return a copy of SEQUENCE with all items satisfying PREDICATE removed.
1317 SEQUENCE should be a list, a vector, or a string. Returns always a list.
1318 If HASH-TABLE-P is non-nil, regards SEQUENCE as a hash table."
1321 (mapatoms (lambda (symbol)
1322 (unless (funcall predicate symbol
)
1325 (unless (listp sequence
)
1326 (setq sequence
(append sequence nil
)))
1328 (unless (funcall predicate
(car sequence
))
1329 (push (car sequence
) out
))
1330 (setq sequence
(cdr sequence
))))
1333 (defun gnus-remove-if-not (predicate sequence
&optional hash-table-p
)
1334 "Return a copy of SEQUENCE with all items not satisfying PREDICATE removed.
1335 SEQUENCE should be a list, a vector, or a string. Returns always a list.
1336 If HASH-TABLE-P is non-nil, regards SEQUENCE as a hash table."
1339 (mapatoms (lambda (symbol)
1340 (when (funcall predicate symbol
)
1343 (unless (listp sequence
)
1344 (setq sequence
(append sequence nil
)))
1346 (when (funcall predicate
(car sequence
))
1347 (push (car sequence
) out
))
1348 (setq sequence
(cdr sequence
))))
1351 (if (fboundp 'assq-delete-all
)
1352 (defalias 'gnus-delete-alist
'assq-delete-all
)
1353 (defun gnus-delete-alist (key alist
)
1354 "Delete from ALIST all elements whose car is KEY.
1355 Return the modified alist."
1357 (while (setq entry
(assq key alist
))
1358 (setq alist
(delq entry alist
)))
1361 (defun gnus-grep-in-list (word list
)
1362 "Find if a WORD matches any regular expression in the given LIST."
1363 (when (and word list
)
1366 (when (string-match r word
)
1367 (throw 'found r
))))))
1369 (defmacro gnus-alist-pull
(key alist
&optional assoc-p
)
1370 "Modify ALIST to be without KEY."
1371 (unless (symbolp alist
)
1372 (error "Not a symbol: %s" alist
))
1373 (let ((fun (if assoc-p
'assoc
'assq
)))
1374 `(setq ,alist
(delq (,fun
,key
,alist
) ,alist
))))
1376 (defun gnus-globalify-regexp (re)
1377 "Return a regexp that matches a whole line, if RE matches a part of it."
1378 (concat (unless (string-match "^\\^" re
) "^.*")
1380 (unless (string-match "\\$$" re
) ".*$")))
1382 (defun gnus-set-window-start (&optional point
)
1383 "Set the window start to POINT, or (point) if nil."
1384 (let ((win (gnus-get-buffer-window (current-buffer) t
)))
1386 (set-window-start win
(or point
(point))))))
1388 (defun gnus-annotation-in-region-p (b e
)
1390 (eq (cadr (memq 'gnus-undeletable
(text-properties-at b
))) t
)
1391 (text-property-any b e
'gnus-undeletable t
)))
1393 (defun gnus-or (&rest elems
)
1394 "Return non-nil if any of the elements are non-nil."
1398 (throw 'found t
)))))
1400 (defun gnus-and (&rest elems
)
1401 "Return non-nil if all of the elements are non-nil."
1405 (throw 'found nil
)))
1408 ;; gnus.el requires mm-util.
1409 (declare-function mm-disable-multibyte
"mm-util")
1411 (defun gnus-write-active-file (file hashtb
&optional full-names
)
1412 ;; `coding-system-for-write' should be `raw-text' or equivalent.
1413 (let ((coding-system-for-write nnmail-active-file-coding-system
))
1414 (with-temp-file file
1415 ;; The buffer should be in the unibyte mode because group names
1416 ;; are ASCII text or encoded non-ASCII text (i.e., unibyte).
1417 (mm-disable-multibyte)
1423 (insert (format "%S %d %d y\n"
1426 (intern (gnus-group-real-name (symbol-name sym
))))
1427 (or (cdr (symbol-value sym
))
1428 (car (symbol-value sym
)))
1429 (car (symbol-value sym
))))))
1431 (goto-char (point-max))
1432 (while (search-backward "\\." nil t
)
1435 ;; Fixme: Why not use `with-output-to-temp-buffer'?
1436 (defmacro gnus-with-output-to-file
(file &rest body
)
1437 (let ((buffer (make-symbol "output-buffer"))
1438 (size (make-symbol "output-buffer-size"))
1439 (leng (make-symbol "output-buffer-length"))
1440 (append (make-symbol "output-buffer-append")))
1441 `(let* ((,size
131072)
1442 (,buffer
(make-string ,size
0))
1447 (aset ,buffer
,leng c
)
1449 (if (= ,size
(setq ,leng
(1+ ,leng
)))
1450 (progn (write-region ,buffer nil
,file
,append
'no-msg
)
1455 (let ((coding-system-for-write 'no-conversion
))
1456 (write-region (substring ,buffer
0 ,leng
) nil
,file
1457 ,append
'no-msg
))))))
1459 (put 'gnus-with-output-to-file
'lisp-indent-function
1)
1460 (put 'gnus-with-output-to-file
'edebug-form-spec
'(form body
))
1462 (if (fboundp 'union
)
1463 (defalias 'gnus-union
'union
)
1464 (defun gnus-union (l1 l2
)
1465 "Set union of lists L1 and L2."
1466 (cond ((null l1
) l2
)
1470 (or (>= (length l1
) (length l2
))
1471 (setq l1
(prog1 l2
(setq l2 l1
))))
1473 (or (member (car l2
) l1
)
1478 (declare-function gnus-add-text-properties
"gnus"
1479 (start end properties
&optional object
))
1481 (defun gnus-add-text-properties-when
1482 (property value start end properties
&optional object
)
1483 "Like `gnus-add-text-properties', only applied on where PROPERTY is VALUE."
1486 (< start end
) ;; XEmacs will loop for every when start=end.
1487 (setq point
(text-property-not-all start end property value
)))
1488 (gnus-add-text-properties start point properties object
)
1489 (setq start
(text-property-any point end property value
)))
1491 (gnus-add-text-properties start end properties object
))))
1493 (defun gnus-remove-text-properties-when
1494 (property value start end properties
&optional object
)
1495 "Like `remove-text-properties', only applied on where PROPERTY is VALUE."
1499 (setq point
(text-property-not-all start end property value
)))
1500 (remove-text-properties start point properties object
)
1501 (setq start
(text-property-any point end property value
)))
1503 (remove-text-properties start end properties object
))
1506 (defun gnus-string-remove-all-properties (string)
1509 (set-text-properties 0 (length string
) nil string
)
1513 ;; This might use `compare-strings' to reduce consing in the
1514 ;; case-insensitive case, but it has to cope with null args.
1515 ;; (`string-equal' uses symbol print names.)
1516 (defun gnus-string-equal (x y
)
1517 "Like `string-equal', except it compares case-insensitively."
1518 (and (= (length x
) (length y
))
1519 (or (string-equal x y
)
1520 (string-equal (downcase x
) (downcase y
)))))
1522 (defcustom gnus-use-byte-compile t
1523 "If non-nil, byte-compile crucial run-time code.
1524 Setting it to nil has no effect after the first time `gnus-byte-compile'
1528 :group
'gnus-various
)
1530 (defun gnus-byte-compile (form)
1531 "Byte-compile FORM if `gnus-use-byte-compile' is non-nil."
1532 (if gnus-use-byte-compile
1535 ;; Work around a bug in XEmacs 21.4
1536 (require 'byte-optimize
)
1539 (defalias 'gnus-byte-compile
1541 (let ((byte-compile-warnings '(unresolved callargs redefine
)))
1542 (byte-compile form
))))
1543 (gnus-byte-compile form
))
1546 (defun gnus-remassoc (key alist
)
1547 "Delete by side effect any elements of LIST whose car is `equal' to KEY.
1548 The modified LIST is returned. If the first member
1549 of LIST has a car that is `equal' to KEY, there is no way to remove it
1550 by side effect; therefore, write `(setq foo (gnus-remassoc key foo))' to be
1551 sure of changing the value of `foo'."
1553 (if (equal key
(caar alist
))
1555 (setcdr alist
(gnus-remassoc key
(cdr alist
)))
1558 (defun gnus-update-alist-soft (key value alist
)
1560 (cons (cons key value
) (gnus-remassoc key alist
))
1561 (gnus-remassoc key alist
)))
1563 (defun gnus-create-info-command (node)
1564 "Create a command that will go to info NODE."
1567 ,(concat "Enter the info system at node " node
)
1568 (Info-goto-node ,node
)
1569 (setq gnus-info-buffer
(current-buffer))
1570 (gnus-configure-windows 'info
)))
1572 (defun gnus-not-ignore (&rest args
)
1575 (defvar gnus-directory-sep-char-regexp
"/"
1576 "The regexp of directory separator character.
1577 If you find some problem with the directory separator character, try
1578 \"[/\\\\\]\" for some systems.")
1580 (defun gnus-url-unhex (x)
1587 ;; Fixme: Do it like QP.
1588 (defun gnus-url-unhex-string (str &optional allow-newlines
)
1589 "Remove %XX, embedded spaces, etc in a url.
1590 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
1591 decoding of carriage returns and line feeds in the string, which is normally
1592 forbidden in URL encoding."
1594 (case-fold-search t
))
1595 (while (string-match "%[0-9a-f][0-9a-f]" str
)
1596 (let* ((start (match-beginning 0))
1597 (ch1 (gnus-url-unhex (elt str
(+ start
1))))
1599 (gnus-url-unhex (elt str
(+ start
2))))))
1601 tmp
(substring str
0 start
)
1604 (char-to-string code
))
1605 ((or (= code ?
\n) (= code ?
\r))
1607 (t (char-to-string code
))))
1608 str
(substring str
(match-end 0)))))
1609 (setq tmp
(concat tmp str
))
1612 (defun gnus-make-predicate (spec)
1613 "Transform SPEC into a function that can be called.
1614 SPEC is a predicate specifier that contains stuff like `or', `and',
1615 `not', lists and functions. The functions all take one parameter."
1616 `(lambda (elem) ,(gnus-make-predicate-1 spec
)))
1618 (defun gnus-make-predicate-1 (spec)
1623 (if (memq (car spec
) '(or and not
))
1624 `(,(car spec
) ,@(mapcar 'gnus-make-predicate-1
(cdr spec
)))
1625 (error "Invalid predicate specifier: %s" spec
)))))
1627 (defun gnus-completing-read (prompt collection
&optional require-match
1628 initial-input history def
)
1629 "Call `gnus-completing-read-function'."
1630 (funcall gnus-completing-read-function
1631 (concat prompt
(when def
1632 (concat " (default " def
")"))
1634 collection require-match initial-input history def
))
1636 (defun gnus-emacs-completing-read (prompt collection
&optional require-match
1637 initial-input history def
)
1638 "Call standard `completing-read-function'."
1639 (let ((completion-styles gnus-completion-styles
))
1640 (completing-read prompt
1641 ;; Old XEmacs (at least 21.4) expect an alist for
1643 (mapcar 'list collection
)
1644 nil require-match initial-input history def
)))
1646 (autoload 'ido-completing-read
"ido")
1647 (defun gnus-ido-completing-read (prompt collection
&optional require-match
1648 initial-input history def
)
1649 "Call `ido-completing-read-function'."
1650 (ido-completing-read prompt collection nil require-match
1651 initial-input history def
))
1654 (autoload 'iswitchb-read-buffer
"iswitchb")
1655 (defun gnus-iswitchb-completing-read (prompt collection
&optional require-match
1656 initial-input history def
)
1657 "`iswitchb' based completing-read function."
1658 (let ((iswitchb-make-buflist-hook
1660 (setq iswitchb-temp-buflist
1661 (let ((choices (append
1662 (when initial-input
(list initial-input
))
1663 (symbol-value history
) collection
))
1666 (setq filtered-choices
(adjoin x filtered-choices
)))
1667 (nreverse filtered-choices
))))))
1670 (when (not iswitchb-mode
)
1671 (add-hook 'minibuffer-setup-hook
'iswitchb-minibuffer-setup
))
1672 (iswitchb-read-buffer prompt def require-match
))
1673 (when (not iswitchb-mode
)
1674 (remove-hook 'minibuffer-setup-hook
'iswitchb-minibuffer-setup
)))))
1676 (defun gnus-graphic-display-p ()
1677 (if (featurep 'xemacs
)
1678 (device-on-window-system-p)
1679 (display-graphic-p)))
1681 (put 'gnus-parse-without-error
'lisp-indent-function
0)
1682 (put 'gnus-parse-without-error
'edebug-form-spec
'(body))
1684 (defmacro gnus-parse-without-error
(&rest body
)
1685 "Allow continuing onto the next line even if an error occurs."
1686 `(while (not (eobp))
1690 (goto-char (point-max)))
1692 (gnus-error 4 "Invalid data on line %d"
1693 (count-lines (point-min) (point)))
1694 (forward-line 1)))))
1696 (defun gnus-cache-file-contents (file variable function
)
1697 "Cache the contents of FILE in VARIABLE. The contents come from FUNCTION."
1698 (let ((time (nth 5 (file-attributes file
)))
1700 (if (or (null (setq value
(symbol-value variable
)))
1701 (not (equal (car value
) file
))
1702 (not (equal (nth 1 value
) time
)))
1704 (setq contents
(funcall function file
))
1705 (set variable
(list file time contents
))
1709 (defun gnus-multiple-choice (prompt choice
&optional idx
)
1710 "Ask user a multiple choice question.
1711 CHOICE is a list of the choice char and help message at IDX."
1713 (save-window-excursion
1716 (message "%s (%s): "
1719 (mapconcat (lambda (s) (char-to-string (car s
)))
1720 choice
", ") ", ?"))
1721 (setq tchar
(read-char))
1722 (when (not (assq tchar choice
))
1724 (setq buf
(get-buffer-create "*Gnus Help*"))
1726 (fundamental-mode) ; for Emacs 20.4+
1727 (buffer-disable-undo)
1729 (insert prompt
":\n\n")
1736 ;; find the longest string to display
1738 (setq n
(length (nth idx
(car list
))))
1741 (setq list
(cdr list
)))
1742 (setq max
(+ max
4)) ; %c, `:', SPACE, a SPACE at end
1743 (setq n
(/ (1- (window-width)) max
)) ; items per line
1744 (setq width
(/ (1- (window-width)) n
)) ; width of each item
1745 ;; insert `n' items, each in a field of width `width'
1750 (delete-char -
1) ; the `\n' takes a char
1752 (setq pad
(- width
3))
1753 (setq format
(concat "%c: %-" (int-to-string pad
) "s"))
1754 (insert (format format
(caar alist
) (nth idx
(car alist
))))
1755 (setq alist
(cdr alist
))
1756 (setq i
(1+ i
))))))))
1757 (if (buffer-live-p buf
)
1761 (if (fboundp 'select-frame-set-input-focus
)
1762 (defalias 'gnus-select-frame-set-input-focus
'select-frame-set-input-focus
)
1763 ;; XEmacs 21.4, SXEmacs
1764 (defun gnus-select-frame-set-input-focus (frame)
1765 "Select FRAME, raise it, and set input focus, if possible."
1767 (select-frame frame
)
1768 (focus-frame frame
)))
1770 (defun gnus-frame-or-window-display-name (object)
1771 "Given a frame or window, return the associated display name.
1772 Return nil otherwise."
1773 (if (featurep 'xemacs
)
1774 (device-connection (dfw-device object
))
1775 (if (or (framep object
)
1776 (and (windowp object
)
1777 (setq object
(window-frame object
))))
1778 (let ((display (frame-parameter object
'display
)))
1779 (if (and (stringp display
)
1780 ;; Exclude invalid display names.
1781 (string-match "\\`[^:]*:[0-9]+\\(\\.[0-9]+\\)?\\'"
1785 (defvar tool-bar-mode
)
1787 (defun gnus-tool-bar-update (&rest ignore
)
1788 "Update the tool bar."
1789 (when (and (boundp 'tool-bar-mode
)
1792 (func (cond ((featurep 'xemacs
)
1794 ((fboundp 'tool-bar-update
)
1796 ((fboundp 'force-window-update
)
1797 'force-window-update
)
1798 ((fboundp 'redraw-frame
)
1799 (setq args
(list (selected-frame)))
1802 (apply func args
))))
1804 ;; Fixme: This has only one use (in gnus-agent), which isn't worthwhile.
1805 (defmacro gnus-mapcar
(function seq1
&rest seqs2_n
)
1806 "Apply FUNCTION to each element of the sequences, and make a list of the results.
1807 If there are several sequences, FUNCTION is called with that many arguments,
1808 and mapping stops as soon as the shortest sequence runs out. With just one
1809 sequence, this is like `mapcar'. With several, it is like the Common Lisp
1810 `mapcar' function extended to arbitrary sequence types."
1813 (let* ((seqs (cons seq1 seqs2_n
))
1815 (heads (mapcar (lambda (seq)
1816 (make-symbol (concat "head"
1818 (setq cnt
(1+ cnt
))))))
1820 (result (make-symbol "result"))
1821 (result-tail (make-symbol "result-tail")))
1822 `(let* ,(let* ((bindings (cons nil nil
))
1824 (nconc bindings
(list (list result
'(cons nil nil
))))
1825 (nconc bindings
(list (list result-tail result
)))
1827 (nconc bindings
(list (list (pop heads
) (pop seqs
)))))
1829 (while (and ,@heads
)
1830 (setcdr ,result-tail
(cons (funcall ,function
1831 ,@(mapcar (lambda (h) (list 'car h
))
1834 (setq ,result-tail
(cdr ,result-tail
)
1835 ,@(apply 'nconc
(mapcar (lambda (h) (list h
(list 'cdr h
))) heads
))))
1837 `(mapcar ,function
,seq1
)))
1839 (if (fboundp 'merge
)
1840 (defalias 'gnus-merge
'merge
)
1841 ;; Adapted from cl-seq.el
1842 (defun gnus-merge (type list1 list2 pred
)
1843 "Destructively merge lists LIST1 and LIST2 to produce a new list.
1844 Argument TYPE is for compatibility and ignored.
1845 Ordering of the elements is preserved according to PRED, a `less-than'
1846 predicate on the elements."
1848 (while (and list1 list2
)
1849 (if (funcall pred
(car list2
) (car list1
))
1850 (push (pop list2
) res
)
1851 (push (pop list1
) res
)))
1852 (nconc (nreverse res
) list1 list2
))))
1854 (defvar xemacs-codename
)
1855 (defvar sxemacs-codename
)
1856 (defvar emacs-program-version
)
1858 (defun gnus-emacs-version ()
1859 "Stringified Emacs version."
1860 (let* ((lst (if (listp gnus-user-agent
)
1862 '(gnus emacs type
)))
1863 (system-v (cond ((memq 'config lst
)
1864 system-configuration
)
1866 (symbol-name system-type
))
1869 (cond ((featurep 'sxemacs
)
1870 (setq emacsname
"SXEmacs"
1871 codename sxemacs-codename
))
1873 (setq emacsname
"XEmacs"
1874 codename xemacs-codename
))
1876 (setq emacsname
"Emacs")))
1878 ((not (memq 'emacs lst
))
1880 ((string-match "^\\(\\([.0-9]+\\)*\\)\\.[0-9]+$" emacs-version
)
1882 (concat "Emacs/" (match-string 1 emacs-version
)
1884 (concat " (" system-v
")")
1886 ((or (featurep 'sxemacs
) (featurep 'xemacs
))
1887 ;; XEmacs or SXEmacs:
1888 (concat emacsname
"/" emacs-program-version
1890 (when (memq 'codename lst
)
1891 (push codename plst
))
1893 (push system-v plst
))
1894 (unless (featurep 'mule
)
1895 (push "no MULE" plst
))
1896 (when (> (length plst
) 0)
1898 " (" (mapconcat 'identity
(reverse plst
) ", ") ")")))))
1899 (t emacs-version
))))
1901 (defun gnus-rename-file (old-path new-path
&optional trim
)
1902 "Rename OLD-PATH as NEW-PATH. If TRIM, recursively delete
1903 empty directories from OLD-PATH."
1904 (when (file-exists-p old-path
)
1905 (let* ((old-dir (file-name-directory old-path
))
1906 (old-name (file-name-nondirectory old-path
))
1907 (new-dir (file-name-directory new-path
))
1908 (new-name (file-name-nondirectory new-path
))
1910 (gnus-make-directory new-dir
)
1911 (rename-file old-path new-path t
)
1913 (while (progn (setq temp
(directory-files old-dir
))
1914 (while (member (car temp
) '("." ".."))
1915 (setq temp
(cdr temp
)))
1916 (= (length temp
) 0))
1917 (delete-directory old-dir
)
1918 (setq old-dir
(file-name-as-directory
1920 (concat old-dir
"..")))))))))
1922 (defun gnus-set-file-modes (filename mode
)
1923 "Wrapper for set-file-modes."
1925 (set-file-modes filename mode
)))
1927 (if (fboundp 'set-process-query-on-exit-flag
)
1928 (defalias 'gnus-set-process-query-on-exit-flag
1929 'set-process-query-on-exit-flag
)
1930 (defalias 'gnus-set-process-query-on-exit-flag
1931 'process-kill-without-query
))
1933 (if (fboundp 'with-local-quit
)
1934 (defalias 'gnus-with-local-quit
'with-local-quit
)
1935 (defmacro gnus-with-local-quit
(&rest body
)
1936 "Execute BODY, allowing quits to terminate BODY but not escape further.
1937 When a quit terminates BODY, `gnus-with-local-quit' returns nil but
1938 requests another quit. That quit will be processed as soon as quitting
1939 is allowed once again. (Immediately, if `inhibit-quit' is nil.)"
1940 ;;(declare (debug t) (indent 0))
1941 `(condition-case nil
1942 (let ((inhibit-quit nil
))
1944 (quit (setq quit-flag t
)
1945 ;; This call is to give a chance to handle quit-flag
1946 ;; in case inhibit-quit is nil.
1947 ;; Without this, it will not be handled until the next function
1948 ;; call, and that might allow it to exit thru a condition-case
1949 ;; that intends to handle the quit signal next time.
1950 (eval '(ignore nil
))))))
1952 (defalias 'gnus-read-shell-command
1953 (if (fboundp 'read-shell-command
) 'read-shell-command
'read-string
))
1955 (defmacro gnus-put-display-table
(range value display-table
)
1956 "Set the value for char RANGE to VALUE in DISPLAY-TABLE. "
1957 (if (featurep 'xemacs
)
1959 `(if (fboundp 'put-display-table
)
1960 (put-display-table ,range
,value
,display-table
)
1961 (if (sequencep ,display-table
)
1962 (aset ,display-table
,range
,value
)
1963 (put-char-table ,range
,value
,display-table
))))
1964 `(aset ,display-table
,range
,value
)))
1966 (defmacro gnus-get-display-table
(character display-table
)
1967 "Find value for CHARACTER in DISPLAY-TABLE. "
1968 (if (featurep 'xemacs
)
1969 `(if (fboundp 'get-display-table
)
1970 (get-display-table ,character
,display-table
)
1971 (if (sequencep ,display-table
)
1972 (aref ,display-table
,character
)
1973 (get-char-table ,character
,display-table
)))
1974 `(aref ,display-table
,character
)))
1976 (defun gnus-rescale-image (image size
)
1977 "Rescale IMAGE to SIZE if possible.
1978 SIZE is in format (WIDTH . HEIGHT). Return a new image.
1979 Sizes are in pixels."
1980 (if (or (not (fboundp 'imagemagick-types
))
1981 (not (get-buffer-window (current-buffer))))
1983 (let ((new-width (car size
))
1984 (new-height (cdr size
)))
1985 (when (> (cdr (image-size image t
)) new-height
)
1986 (setq image
(or (create-image (plist-get (cdr image
) :data
) 'imagemagick t
1989 (when (> (car (image-size image t
)) new-width
)
1991 (create-image (plist-get (cdr image
) :data
) 'imagemagick t
1996 (provide 'gnus-util
)
1998 ;;; gnus-util.el ends here