1 ;;; gnus-util.el --- utility functions for Gnus
3 ;; Copyright (C) 1996-2017 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
25 ;; Nothing in this file depends on any other parts of Gnus -- all
26 ;; functions and macros in this file are utility functions that are
27 ;; used by Gnus and may be used by any other package without loading
30 ;; [Unfortunately, it does depend on other parts of Gnus, e.g. the
31 ;; autoloads and defvars below...]
40 (defcustom gnus-completing-read-function
'gnus-emacs-completing-read
41 "Function use to do completing read."
44 :type
`(radio (function-item
45 :doc
"Use Emacs standard `completing-read' function."
46 gnus-emacs-completing-read
)
48 :doc
"Use `ido-completing-read' function."
49 gnus-ido-completing-read
)
51 :doc
"Use iswitchb based completing-read function."
52 gnus-iswitchb-completing-read
)))
54 (defcustom gnus-completion-styles
55 (append (when (and (assq 'substring completion-styles-alist
)
56 (not (memq 'substring completion-styles
)))
59 "Value of `completion-styles' to use when completing."
62 :type
'(repeat symbol
))
64 ;; Fixme: this should be a gnus variable, not nnmail-.
65 (defvar nnmail-pathname-coding-system
)
66 (defvar nnmail-active-file-coding-system
)
68 ;; Inappropriate references to other parts of Gnus.
69 (defvar gnus-emphasize-whitespace-regexp
)
70 (defvar gnus-original-article-buffer
)
71 (defvar gnus-user-agent
)
73 (autoload 'gnus-get-buffer-window
"gnus-win")
74 (autoload 'nnheader-narrow-to-headers
"nnheader")
75 (autoload 'nnheader-replace-chars-in-string
"nnheader")
76 (autoload 'mail-header-remove-comments
"mail-parse")
78 (defun gnus-replace-in-string (string regexp newtext
&optional literal
)
79 "Replace all matches for REGEXP with NEWTEXT in STRING.
80 If LITERAL is non-nil, insert NEWTEXT literally. Return a new
81 string containing the replacements.
83 This is a compatibility function for different Emacsen."
84 (declare (obsolete replace-regexp-in-string
"26.1"))
85 (replace-regexp-in-string regexp newtext string nil literal
))
87 (defmacro gnus-eval-in-buffer-window
(buffer &rest forms
)
88 "Pop to BUFFER, evaluate FORMS, and then return to the original window."
89 (let ((tempvar (make-symbol "GnusStartBufferWindow"))
91 (buf (make-symbol "buf")))
92 `(let* ((,tempvar
(selected-window))
94 (,w
(gnus-get-buffer-window ,buf
'visible
)))
100 (set-buffer (window-buffer ,w
)))
101 (pop-to-buffer ,buf
))
103 (select-window ,tempvar
)))))
105 (put 'gnus-eval-in-buffer-window
'lisp-indent-function
1)
106 (put 'gnus-eval-in-buffer-window
'edebug-form-spec
'(form body
))
108 (defmacro gnus-intern-safe
(string hashtable
)
109 "Get hash value. Arguments are STRING and HASHTABLE."
110 `(let ((symbol (intern ,string
,hashtable
)))
115 (defsubst gnus-goto-char
(point)
116 (and point
(goto-char point
)))
118 (defmacro gnus-buffer-exists-p
(buffer)
119 `(let ((buffer ,buffer
))
121 (funcall (if (stringp buffer
) 'get-buffer
'buffer-name
)
124 (defun gnus-delete-first (elt list
)
125 "Delete by side effect the first occurrence of ELT as a member of LIST."
126 (if (equal (car list
) elt
)
129 (while (and (cdr list
)
130 (not (equal (cadr list
) elt
)))
131 (setq list
(cdr list
)))
133 (setcdr list
(cddr list
)))
136 ;; Delete the current line (and the next N lines).
137 (defmacro gnus-delete-line
(&optional n
)
138 `(delete-region (point-at-bol)
139 (progn (forward-line ,(or n
1)) (point))))
141 (defun gnus-extract-address-components (from)
142 "Extract address components from a From header.
143 Given an RFC-822 address FROM, extract full name and canonical address.
144 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS). Much more simple
145 solution than `mail-extract-address-components', which works much better, but
148 ;; First find the address - the thing with the @ in it. This may
149 ;; not be accurate in mail addresses, but does the trick most of
150 ;; the time in news messages.
151 (cond (;; Check ``<foo@bar>'' first in order to handle the quite common
152 ;; form ``"abc@xyz" <foo@bar>'' (i.e. ``@'' as part of a comment)
154 (string-match "<\\([^@ \t<>]+[!@][^@ \t<>]+\\)>" from
)
155 (setq address
(substring from
(match-beginning 1) (match-end 1))))
156 ((string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from
)
157 (setq address
(substring from
(match-beginning 0) (match-end 0)))))
158 ;; Then we check whether the "name <address>" format is used.
160 ;; Linear white space is not required.
161 (string-match (concat "[ \t]*<" (regexp-quote address
) ">") from
)
162 (and (setq name
(substring from
0 (match-beginning 0)))
163 ;; Strip any quotes from the name.
164 (string-match "^\".*\"$" name
)
165 (setq name
(substring name
1 (1- (match-end 0))))))
166 ;; If not, then "address (name)" is used.
168 (and (string-match "(.+)" from
)
169 (setq name
(substring from
(1+ (match-beginning 0))
170 (1- (match-end 0)))))
171 (and (string-match "()" from
)
173 ;; XOVER might not support folded From headers.
174 (and (string-match "(.*" from
)
175 (setq name
(substring from
(1+ (match-beginning 0))
177 (list (if (string= name
"") nil name
) (or address from
))))
179 (declare-function message-fetch-field
"message" (header &optional not-all
))
181 (defun gnus-fetch-field (field)
182 "Return the value of the header FIELD of current article."
186 (let ((inhibit-point-motion-hooks t
))
187 (nnheader-narrow-to-headers)
188 (message-fetch-field field
)))))
190 (defun gnus-fetch-original-field (field)
191 "Fetch FIELD from the original version of the current article."
192 (with-current-buffer gnus-original-article-buffer
193 (gnus-fetch-field field
)))
196 (defun gnus-goto-colon ()
197 (move-beginning-of-line 1)
198 (let ((eol (point-at-eol)))
199 (goto-char (or (text-property-any (point) eol
'gnus-position t
)
200 (search-forward ":" eol t
)
203 (declare-function gnus-find-method-for-group
"gnus" (group &optional info
))
204 (declare-function gnus-group-name-decode
"gnus-group" (string charset
))
205 (declare-function gnus-group-name-charset
"gnus-group" (method group
))
206 ;; gnus-group requires gnus-int which requires message.
207 (declare-function message-tokenize-header
"message"
208 (header &optional separator
))
210 (defun gnus-decode-newsgroups (newsgroups group
&optional method
)
211 (require 'gnus-group
)
212 (let ((method (or method
(gnus-find-method-for-group group
))))
213 (mapconcat (lambda (group)
214 (gnus-group-name-decode group
(gnus-group-name-charset
216 (message-tokenize-header newsgroups
)
219 (defun gnus-remove-text-with-property (prop)
220 "Delete all text in the current buffer with text property PROP."
221 (let ((start (point-min))
223 (unless (get-text-property start prop
)
224 (setq start
(next-single-property-change start prop
)))
226 (setq end
(text-property-any start
(point-max) prop nil
))
227 (delete-region start
(or end
(point-max)))
228 (setq start
(when end
229 (next-single-property-change start prop
))))))
231 (defun gnus-find-text-property-region (start end prop
)
232 "Return a list of text property regions that has property PROP."
234 (unless (get-text-property start prop
)
235 (setq start
(next-single-property-change start prop
)))
237 (setq value
(get-text-property start prop
)
238 end
(text-property-not-all start
(point-max) prop value
))
242 (push (list (set-marker (make-marker) start
)
243 (set-marker (make-marker) end
)
246 (setq start
(next-single-property-change start prop
))))
249 (defun gnus-newsgroup-directory-form (newsgroup)
250 "Make hierarchical directory name from NEWSGROUP name."
251 (let* ((newsgroup (gnus-newsgroup-savable-name newsgroup
))
252 (idx (string-match ":" newsgroup
)))
254 (if idx
(substring newsgroup
0 idx
))
256 (nnheader-replace-chars-in-string
257 (if idx
(substring newsgroup
(1+ idx
)) newsgroup
)
260 (defun gnus-newsgroup-savable-name (group)
261 ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
263 (nnheader-replace-chars-in-string group ?
/ ?.
))
265 (defun gnus-string> (s1 s2
)
266 (not (or (string< s1 s2
)
269 (defun gnus-string< (s1 s2
)
270 "Return t if first arg string is less than second in lexicographic order.
271 Case is significant if and only if `case-fold-search' is nil.
272 Symbols are also allowed; their print names are used instead."
274 (string-lessp (downcase (if (symbolp s1
) (symbol-name s1
) s1
))
275 (downcase (if (symbolp s2
) (symbol-name s2
) s2
)))
276 (string-lessp s1 s2
)))
280 (defun gnus-file-newer-than (file date
)
281 (let ((fdate (nth 5 (file-attributes file
))))
282 (or (> (car fdate
) (car date
))
283 (and (= (car fdate
) (car date
))
284 (> (nth 1 fdate
) (nth 1 date
))))))
288 (defmacro gnus-local-set-keys
(&rest plist
)
289 "Set the keys in PLIST in the current keymap."
290 `(gnus-define-keys-1 (current-local-map) ',plist
))
292 (defmacro gnus-define-keys
(keymap &rest plist
)
293 "Define all keys in PLIST in KEYMAP."
294 `(gnus-define-keys-1 (quote ,keymap
) (quote ,plist
)))
296 (defmacro gnus-define-keys-safe
(keymap &rest plist
)
297 "Define all keys in PLIST in KEYMAP without overwriting previous definitions."
298 `(gnus-define-keys-1 (quote ,keymap
) (quote ,plist
) t
))
300 (put 'gnus-define-keys
'lisp-indent-function
1)
301 (put 'gnus-define-keys-safe
'lisp-indent-function
1)
302 (put 'gnus-local-set-keys
'lisp-indent-function
1)
304 (defmacro gnus-define-keymap
(keymap &rest plist
)
305 "Define all keys in PLIST in KEYMAP."
306 `(gnus-define-keys-1 ,keymap
(quote ,plist
)))
308 (put 'gnus-define-keymap
'lisp-indent-function
1)
310 (defun gnus-define-keys-1 (keymap plist
&optional safe
)
312 (error "Can't set keys in a null keymap"))
313 (cond ((symbolp keymap
)
314 (setq keymap
(symbol-value keymap
)))
317 (set (car keymap
) nil
)
318 (define-prefix-command (car keymap
))
319 (define-key (symbol-value (caddr keymap
)) (cadr keymap
) (car keymap
))
320 (setq keymap
(symbol-value (car keymap
)))))
323 (when (symbolp (setq key
(pop plist
)))
324 (setq key
(symbol-value key
)))
326 (eq (lookup-key keymap key
) 'undefined
))
327 (define-key keymap key
(pop plist
))
330 (defun gnus-y-or-n-p (prompt)
334 (defun gnus-yes-or-no-p (prompt)
339 ;; By Frank Schmitt <ich@Frank-Schmitt.net>. Allows to have
340 ;; age-depending date representations. (e.g. just the time if it's
341 ;; from today, the day of the week if it's within the last 7 days and
342 ;; the full date if it's older)
344 (defun gnus-seconds-today ()
345 "Return the number of seconds passed today."
346 (let ((now (decode-time)))
347 (+ (car now
) (* (car (cdr now
)) 60) (* (car (nthcdr 2 now
)) 3600))))
349 (defun gnus-seconds-month ()
350 "Return the number of seconds passed this month."
351 (let ((now (decode-time)))
352 (+ (car now
) (* (car (cdr now
)) 60) (* (car (nthcdr 2 now
)) 3600)
353 (* (- (car (nthcdr 3 now
)) 1) 3600 24))))
355 (defun gnus-seconds-year ()
356 "Return the number of seconds passed this year."
357 (let* ((current (current-time))
358 (now (decode-time current
))
359 (days (format-time-string "%j" current
)))
360 (+ (car now
) (* (car (cdr now
)) 60) (* (car (nthcdr 2 now
)) 3600)
361 (* (- (string-to-number days
) 1) 3600 24))))
363 (defmacro gnus-date-get-time
(date)
364 "Convert DATE string to Emacs time.
365 Cache the result as a text property stored in DATE."
366 ;; Either return the cached value...
370 (or (get-text-property 0 'gnus-time d
)
371 ;; or compute the value...
372 (let ((time (safe-date-to-time d
)))
373 ;; and store it back in the string.
374 (put-text-property 0 1 'gnus-time time d
)
377 (defun gnus-dd-mmm (messy-date)
378 "Return a string like DD-MMM from a big messy string."
380 (format-time-string "%d-%b" (gnus-date-get-time messy-date
))
383 (defsubst gnus-time-iso8601
(time)
384 "Return a string of TIME in YYYYMMDDTHHMMSS format."
385 (format-time-string "%Y%m%dT%H%M%S" time
))
387 (defun gnus-date-iso8601 (date)
388 "Convert the DATE to YYYYMMDDTHHMMSS."
390 (gnus-time-iso8601 (gnus-date-get-time date
))
393 (defun gnus-mode-string-quote (string)
394 "Quote all \"%\"'s in STRING."
395 (replace-regexp-in-string "%" "%%" string
))
397 ;; Make a hash table (default and minimum size is 256).
398 ;; Optional argument HASHSIZE specifies the table size.
399 (defun gnus-make-hashtable (&optional hashsize
)
400 (make-vector (if hashsize
(max (gnus-create-hash-size hashsize
) 256) 256) 0))
402 ;; Make a number that is suitable for hashing; bigger than MIN and
403 ;; equal to some 2^x. Many machines (such as sparcs) do not have a
404 ;; hardware modulo operation, so they implement it in software. On
405 ;; many sparcs over 50% of the time to intern is spent in the modulo.
406 ;; Yes, it's slower than actually computing the hash from the string!
407 ;; So we use powers of 2 so people can optimize the modulo to a mask.
408 (defun gnus-create-hash-size (min)
414 (defcustom gnus-verbose
6
415 "Integer that says how verbose Gnus should be.
416 The higher the number, the more messages Gnus will flash to say what
417 it's doing. At zero, Gnus will be totally mute; at five, Gnus will
418 display most important messages; and at ten, Gnus will keep on
419 jabbering all the time."
424 (defcustom gnus-add-timestamp-to-message nil
425 "Non-nil means add timestamps to messages that Gnus issues.
426 If it is `log', add timestamps to only the messages that go into
427 the \"*Messages*\" buffer. If it is neither nil nor `log', add
428 timestamps not only to log messages but also to the ones
429 displayed in the echo area."
430 :version
"23.1" ;; No Gnus
432 :type
'(choice :format
"%{%t%}:\n %[Value Menu%] %v"
433 (const :tag
"Logged messages only" log
)
434 (sexp :tag
"All messages"
435 :match
(lambda (widget value
) value
)
437 (const :tag
"No timestamp" nil
)))
440 (defmacro gnus-message-with-timestamp-1
(format-string args
)
441 (let ((timestamp '(format-time-string "%Y%m%dT%H%M%S.%3N> " time
)))
443 (cond ((eq gnus-add-timestamp-to-message
'log
)
444 (setq str
(let (message-log-max)
445 (apply 'message
,format-string
,args
)))
446 (when (and message-log-max
447 (> message-log-max
0)
449 (setq time
(current-time))
450 (with-current-buffer (if (fboundp 'messages-buffer
)
452 (get-buffer-create "*Messages*"))
453 (goto-char (point-max))
454 (let ((inhibit-read-only t
))
455 (insert ,timestamp str
"\n")
456 (forward-line (- message-log-max
))
457 (delete-region (point-min) (point)))
458 (goto-char (point-max))))
460 (gnus-add-timestamp-to-message
461 (if (or (and (null ,format-string
) (null ,args
))
463 (setq str
(apply 'format
,format-string
,args
))
464 (zerop (length str
))))
466 (and ,format-string str
)
468 (setq time
(current-time))
469 (message "%s" (concat ,timestamp str
))
472 (apply 'message
,format-string
,args
)))))))
474 (defvar gnus-action-message-log nil
)
476 (defun gnus-message-with-timestamp (format-string &rest args
)
477 "Display message with timestamp. Arguments are the same as `message'.
478 The `gnus-add-timestamp-to-message' variable controls how to add
479 timestamp to message."
480 (gnus-message-with-timestamp-1 format-string args
))
482 (defun gnus-message (level &rest args
)
483 "If LEVEL is lower than `gnus-verbose' print ARGS using `message'.
485 Guideline for numbers:
486 1 - error messages, 3 - non-serious error messages, 5 - messages for things
487 that take a long time, 7 - not very important messages on stuff, 9 - messages
489 (if (<= level gnus-verbose
)
491 (if gnus-add-timestamp-to-message
492 (apply 'gnus-message-with-timestamp args
)
493 (apply 'message args
))))
494 (when (and (consp gnus-action-message-log
)
496 (push message gnus-action-message-log
))
498 ;; We have to do this format thingy here even if the result isn't
499 ;; shown - the return value has to be the same as the return value
501 (apply 'format args
)))
503 (defun gnus-final-warning ()
504 (when (and (consp gnus-action-message-log
)
505 (setq gnus-action-message-log
506 (delete nil gnus-action-message-log
)))
507 (message "Warning: %s"
508 (mapconcat #'identity gnus-action-message-log
"; "))))
510 (defun gnus-error (level &rest args
)
511 "Beep an error if LEVEL is equal to or less than `gnus-verbose'.
512 ARGS are passed to `message'."
513 (when (<= (floor level
) gnus-verbose
)
514 (apply 'message args
)
517 (when (and (floatp level
)
518 (not (zerop (setq duration
(* 10 (- level
(floor level
)))))))
519 (sit-for duration
))))
522 (defun gnus-split-references (references)
523 "Return a list of Message-IDs in REFERENCES."
525 (references (mail-header-remove-comments (or references
"")))
527 (while (string-match "<[^<]+[^< \t]" references beg
)
528 (push (substring references
(match-beginning 0) (setq beg
(match-end 0)))
532 (defun gnus-extract-references (references)
533 "Return a list of Message-IDs in REFERENCES (in In-Reply-To
534 format), trimmed to only contain the Message-IDs."
535 (let ((ids (gnus-split-references references
))
538 (when (string-match "<[^<>]+>" id
)
539 (push (match-string 0 id
) refs
)))
542 (defsubst gnus-parent-id
(references &optional n
)
543 "Return the last Message-ID in REFERENCES.
544 If N, return the Nth ancestor instead."
545 (when (and references
546 (not (zerop (length references
))))
548 (let ((ids (inline (gnus-split-references references
))))
549 (while (nthcdr n ids
)
550 (setq ids
(cdr ids
)))
552 (let ((references (mail-header-remove-comments references
)))
553 (when (string-match "\\(<[^<]+>\\)[ \t]*\\'" references
)
554 (match-string 1 references
))))))
556 (defsubst gnus-buffer-live-p
(buffer)
557 "Say whether BUFFER is alive or not."
558 (and buffer
(buffer-live-p (get-buffer buffer
))))
560 (defun gnus-horizontal-recenter ()
561 "Recenter the current buffer horizontally."
562 (if (< (current-column) (/ (window-width) 2))
563 (set-window-hscroll (gnus-get-buffer-window (current-buffer) t
) 0)
564 (let* ((orig (point))
565 (end (window-end (gnus-get-buffer-window (current-buffer) t
)))
568 ;; Find the longest line currently displayed in the window.
569 (goto-char (window-start))
570 (while (and (not (eobp))
573 (setq max
(max max
(current-column)))
576 ;; Scroll horizontally to center (sort of) the point.
577 (if (> max
(window-width))
579 (gnus-get-buffer-window (current-buffer) t
)
580 (min (- (current-column) (/ (window-width) 3))
581 (+ 2 (- max
(window-width)))))
582 (set-window-hscroll (gnus-get-buffer-window (current-buffer) t
) 0))
585 (defun gnus-read-event-char (&optional prompt
)
586 "Get the next event."
587 (let ((event (read-event prompt
)))
588 (cons (and (numberp event
) event
) event
)))
590 (defun gnus-copy-file (file &optional to
)
593 (list (read-file-name "Copy file: " default-directory
)
594 (read-file-name "Copy file to: " default-directory
)))
596 (setq to
(read-file-name "Copy file to: " default-directory
)))
597 (when (file-directory-p to
)
598 (setq to
(concat (file-name-as-directory to
)
599 (file-name-nondirectory file
))))
602 (defvar gnus-work-buffer
" *gnus work*")
604 (declare-function gnus-get-buffer-create
"gnus" (name))
605 ;; gnus.el requires mm-util.
606 (declare-function mm-enable-multibyte
"mm-util")
608 (defun gnus-set-work-buffer ()
609 "Put point in the empty Gnus work buffer."
610 (if (get-buffer gnus-work-buffer
)
612 (set-buffer gnus-work-buffer
)
614 (set-buffer (gnus-get-buffer-create gnus-work-buffer
))
615 (kill-all-local-variables)
616 (mm-enable-multibyte)))
618 (defmacro gnus-group-real-name
(group)
619 "Find the real name of a foreign newsgroup."
620 `(let ((gname ,group
))
621 (if (string-match "^[^:]+:" gname
)
622 (substring gname
(match-end 0))
625 (defmacro gnus-group-server
(group)
626 "Find the server name of a foreign newsgroup.
627 For example, (gnus-group-server \"nnimap+yxa:INBOX.foo\") would
628 yield \"nnimap:yxa\"."
629 `(let ((gname ,group
))
630 (if (string-match "^\\([^:+]+\\)\\(?:\\+\\([^:]*\\)\\)?:" gname
)
631 (format "%s:%s" (match-string 1 gname
) (or
632 (match-string 2 gname
)
634 (format "%s:%s" (car gnus-select-method
) (cadr gnus-select-method
)))))
636 (defun gnus-make-sort-function (funs)
637 "Return a composite sort condition based on the functions in FUNS."
639 ;; Just a simple function.
640 ((functionp funs
) funs
)
641 ;; No functions at all.
643 ;; A list of functions.
648 ,(gnus-make-sort-function-1 (reverse funs
)))))
649 ;; A list containing just one function.
653 (defun gnus-make-sort-function-1 (funs)
654 "Return a composite sort condition based on the functions in FUNS."
655 (let ((function (car funs
))
658 (when (consp function
)
661 ((eq (car function
) 'not
)
662 (setq function
(cadr function
)
665 ((functionp function
)
669 (error "Invalid sort spec: %s" function
))))
671 `(or (,function
,first
,last
)
672 (and (not (,function
,last
,first
))
673 ,(gnus-make-sort-function-1 (cdr funs
))))
674 `(,function
,first
,last
))))
676 (defun gnus-turn-off-edit-menu (type)
677 "Turn off edit menu in `gnus-TYPE-mode-map'."
678 (define-key (symbol-value (intern (format "gnus-%s-mode-map" type
)))
679 [menu-bar edit
] 'undefined
))
681 (defmacro gnus-bind-print-variables
(&rest forms
)
682 "Bind print-* variables and evaluate FORMS.
683 This macro is used with `prin1', `pp', etc. in order to ensure printed
684 Lisp objects are loadable. Bind `print-quoted' and `print-readably'
685 to t, and `print-escape-multibyte', `print-escape-newlines',
686 `print-escape-nonascii', `print-length', `print-level' and
687 `print-string-length' to nil."
688 `(let ((print-quoted t
)
691 ;;print-continuous-numbering
692 print-escape-multibyte
693 print-escape-newlines
694 print-escape-nonascii
701 (defun gnus-prin1 (form)
702 "Use `prin1' on FORM in the current buffer.
703 Bind `print-quoted' and `print-readably' to t, and `print-length' and
704 `print-level' to nil. See also `gnus-bind-print-variables'."
705 (gnus-bind-print-variables (prin1 form
(current-buffer))))
707 (defun gnus-prin1-to-string (form)
708 "The same as `prin1'.
709 Bind `print-quoted' and `print-readably' to t, and `print-length' and
710 `print-level' to nil. See also `gnus-bind-print-variables'."
711 (gnus-bind-print-variables (prin1-to-string form
)))
713 (defun gnus-pp (form &optional stream
)
714 "Use `pp' on FORM in the current buffer.
715 Bind `print-quoted' and `print-readably' to t, and `print-length' and
716 `print-level' to nil. See also `gnus-bind-print-variables'."
717 (gnus-bind-print-variables (pp form
(or stream
(current-buffer)))))
719 (defun gnus-pp-to-string (form)
720 "The same as `pp-to-string'.
721 Bind `print-quoted' and `print-readably' to t, and `print-length' and
722 `print-level' to nil. See also `gnus-bind-print-variables'."
723 (gnus-bind-print-variables (pp-to-string form
)))
725 (defun gnus-make-directory (directory)
726 "Make DIRECTORY (and all its parents) if it doesn't exist."
728 (let ((file-name-coding-system nnmail-pathname-coding-system
))
730 (not (file-exists-p directory
)))
731 (make-directory directory t
)))
734 (defun gnus-write-buffer (file)
735 "Write the current buffer's contents to FILE."
737 (let ((file-name-coding-system nnmail-pathname-coding-system
))
738 ;; Make sure the directory exists.
739 (gnus-make-directory (file-name-directory file
))
741 (write-region (point-min) (point-max) file nil
'quietly
)))
743 (defun gnus-delete-file (file)
744 "Delete FILE if it exists."
745 (when (file-exists-p file
)
748 (defun gnus-delete-duplicates (list)
749 "Remove duplicate entries from LIST."
752 (unless (member (car list
) result
)
753 (push (car list
) result
))
757 (defun gnus-delete-directory (directory)
758 "Delete files in DIRECTORY. Subdirectories remain.
759 If there's no subdirectory, delete DIRECTORY as well."
760 (when (file-directory-p directory
)
761 (let ((files (directory-files
762 directory t
"^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
765 (setq file
(pop files
))
766 (if (eq t
(car (file-attributes file
)))
767 ;; `file' is a subdirectory.
769 ;; `file' is a file or a symlink.
772 (delete-directory directory
)))))
774 (defun gnus-strip-whitespace (string)
775 "Return STRING stripped of all whitespace."
776 (while (string-match "[\r\n\t ]+" string
)
777 (setq string
(replace-match "" t t string
)))
780 (defsubst gnus-put-text-property-excluding-newlines
(beg end prop val
)
781 "The same as `put-text-property', but don't put this prop on any newlines in the region."
786 (while (re-search-forward gnus-emphasize-whitespace-regexp end
'move
)
787 (put-text-property beg
(match-beginning 0) prop val
)
789 (put-text-property beg
(point) prop val
)))))
791 (defsubst gnus-put-overlay-excluding-newlines
(beg end prop val
)
792 "The same as `put-text-property', but don't put this prop on any newlines in the region."
797 (while (re-search-forward gnus-emphasize-whitespace-regexp end
'move
)
798 (overlay-put (make-overlay beg
(match-beginning 0)) prop val
)
800 (overlay-put (make-overlay beg
(point)) prop val
)))))
802 (defun gnus-put-text-property-excluding-characters-with-faces (beg end prop val
)
803 "The same as `put-text-property', except where `gnus-face' is set.
804 If so, and PROP is `face', set the second element of its value to VAL.
805 Otherwise, do nothing."
807 ;; Property values are compared with `eq'.
808 (let ((stop (next-single-property-change beg
'face nil end
)))
809 (if (get-text-property beg
'gnus-face
)
810 (when (eq prop
'face
)
811 (setcar (cdr (get-text-property beg
'face
)) (or val
'default
)))
813 (put-text-property beg stop prop val
)))
816 (defun gnus-get-text-property-excluding-characters-with-faces (pos prop
)
817 "The same as `get-text-property', except where `gnus-face' is set.
818 If so, and PROP is `face', return the second element of its value.
819 Otherwise, return the value."
820 (let ((val (get-text-property pos prop
)))
821 (if (and (get-text-property pos
'gnus-face
)
824 (get-text-property pos prop
))))
826 (defmacro gnus-faces-at
(position)
827 "Return a list of faces at POSITION."
828 `(let ((pos ,position
))
829 (delq nil
(cons (get-text-property pos
'face
)
832 (overlay-get overlay
'face
))
833 (overlays-at pos
))))))
835 ;;; Protected and atomic operations. dmoore@ucsd.edu 21.11.1996
836 ;; The primary idea here is to try to protect internal data structures
837 ;; from becoming corrupted when the user hits C-g, or if a hook or
838 ;; similar blows up. Often in Gnus multiple tables/lists need to be
839 ;; updated at the same time, or information can be lost.
841 (defvar gnus-atomic-be-safe t
842 "If t, certain operations will be protected from interruption by C-g.")
844 (defmacro gnus-atomic-progn
(&rest forms
)
845 "Evaluate FORMS atomically, which means to protect the evaluation
846 from being interrupted by the user. An error from the forms themselves
847 will return without finishing the operation. Since interrupts from
848 the user are disabled, it is recommended that only the most minimal
849 operations are performed by FORMS. If you wish to assign many
850 complicated values atomically, compute the results into temporary
851 variables and then do only the assignment atomically."
852 `(let ((inhibit-quit gnus-atomic-be-safe
))
855 (put 'gnus-atomic-progn
'lisp-indent-function
0)
857 (defmacro gnus-atomic-progn-assign
(protect &rest forms
)
858 "Evaluate FORMS, but ensure that the variables listed in PROTECT
859 are not changed if anything in FORMS signals an error or otherwise
860 non-locally exits. The variables listed in PROTECT are updated atomically.
861 It is safe to use gnus-atomic-progn-assign with long computations.
863 Note that if any of the symbols in PROTECT were unbound, they will be
864 set to nil on a successful assignment. In case of an error or other
865 non-local exit, it will still be unbound."
866 (let* ((temp-sym-map (mapcar (lambda (x) (list (make-symbol
867 (concat (symbol-name x
)
871 (sym-temp-map (mapcar (lambda (x) (list (cadr x
) (car x
)))
873 (temp-sym-let (mapcar (lambda (x) (list (car x
)
874 `(and (boundp ',(cadr x
))
877 (sym-temp-let sym-temp-map
)
878 (temp-sym-assign (apply 'append temp-sym-map
))
879 (sym-temp-assign (apply 'append sym-temp-map
))
880 (result (make-symbol "result-tmp")))
881 `(let (,@temp-sym-let
884 (setq ,result
(progn ,@forms
))
885 (setq ,@temp-sym-assign
))
886 (let ((inhibit-quit gnus-atomic-be-safe
))
887 (setq ,@sym-temp-assign
))
890 (put 'gnus-atomic-progn-assign
'lisp-indent-function
1)
891 ;(put 'gnus-atomic-progn-assign 'edebug-form-spec '(sexp body))
893 (defmacro gnus-atomic-setq
(&rest pairs
)
894 "Similar to setq, except that the real symbols are only assigned when
895 there are no errors. And when the real symbols are assigned, they are
896 done so atomically. If other variables might be changed via side-effect,
897 see gnus-atomic-progn-assign. It is safe to use gnus-atomic-setq
898 with potentially long computations."
902 (push (car tpairs
) syms
)
903 (setq tpairs
(cddr tpairs
)))
904 `(gnus-atomic-progn-assign ,syms
907 ;(put 'gnus-atomic-setq 'edebug-form-spec '(body))
910 ;;; Functions for saving to babyl/mail files.
913 (autoload 'rmail-update-summary
"rmailsum")
915 (defvar mm-text-coding-system
)
917 (declare-function mm-append-to-file
"mm-util"
918 (start end filename
&optional codesys inhibit
))
919 (declare-function rmail-swap-buffers-maybe
"rmail" ())
920 (declare-function rmail-maybe-set-message-counters
"rmail" ())
921 (declare-function rmail-count-new-messages
"rmail" (&optional nomsg
))
922 (declare-function rmail-summary-exists
"rmail" ())
923 (declare-function rmail-show-message
"rmail" (&optional n no-summary
))
924 ;; Macroexpansion of rmail-select-summary:
925 (declare-function rmail-summary-displayed
"rmail" ())
926 (declare-function rmail-pop-to-buffer
"rmail" (&rest args
))
927 (declare-function rmail-maybe-display-summary
"rmail" ())
929 (defun gnus-output-to-rmail (filename &optional ask
)
930 "Append the current article to an Rmail file named FILENAME.
931 In Emacs 22 this writes Babyl format; in Emacs 23 it writes mbox unless
932 FILENAME exists and is Babyl format."
936 ;; Some of this codes is borrowed from rmailout.el.
937 (setq filename
(expand-file-name filename
))
938 ;; FIXME should we really be messing with this defcustom?
939 ;; It is not needed for the operation of this function.
940 (if (boundp 'rmail-default-rmail-file
)
941 (setq rmail-default-rmail-file filename
) ; 22
942 (setq rmail-default-file filename
)) ; 23
943 (let ((artbuf (current-buffer))
944 (tmpbuf (get-buffer-create " *Gnus-output*"))
945 ;; Babyl rmail.el defines this, mbox does not.
946 (babyl (fboundp 'rmail-insert-rmail-file-header
)))
948 ;; Note that we ignore the possibility of visiting a Babyl
949 ;; format buffer in Emacs 23, since Rmail no longer supports that.
950 (or (get-file-buffer filename
)
952 ;; In case someone wants to write to a Babyl file from Emacs 23.
953 (when (file-exists-p filename
)
954 (setq babyl
(mail-file-babyl-p filename
))
958 (concat "\"" filename
"\" does not exist, create it? ")))
959 (let ((file-buffer (create-file-buffer filename
)))
960 (with-current-buffer file-buffer
961 (if (fboundp 'rmail-insert-rmail-file-header
)
962 (rmail-insert-rmail-file-header))
963 (let ((require-final-newline nil
)
964 (coding-system-for-write mm-text-coding-system
))
965 (gnus-write-buffer filename
)))
966 (kill-buffer file-buffer
))
967 (error "Output file does not exist")))
970 (insert-buffer-substring artbuf
)
972 (gnus-convert-article-to-rmail)
973 ;; Non-Babyl case copied from gnus-output-to-mail.
974 (goto-char (point-min))
975 (if (looking-at "From ")
977 (insert "From nobody " (current-time-string) "\n"))
978 (let (case-fold-search)
979 (while (re-search-forward "^From " nil t
)
982 ;; Decide whether to append to a file or to an Emacs buffer.
983 (let ((outbuf (get-file-buffer filename
)))
986 (unless babyl
; from gnus-output-to-mail
987 (let ((buffer-read-only nil
))
988 (goto-char (point-max))
990 (unless (looking-at "\n\n")
991 (goto-char (point-max))
995 (let ((file-name-coding-system nnmail-pathname-coding-system
))
996 (mm-append-to-file (point-min) (point-max) filename
)))
997 ;; File has been visited, in buffer OUTBUF.
999 (let ((buffer-read-only nil
)
1000 (msg (and (boundp 'rmail-current-message
)
1001 (symbol-value 'rmail-current-message
))))
1002 ;; If MSG is non-nil, buffer is in RMAIL mode.
1003 ;; Compare this with rmail-output-to-rmail-buffer in Emacs 23.
1006 (rmail-swap-buffers-maybe)
1007 (rmail-maybe-set-message-counters))
1009 (narrow-to-region (point-max) (point-max)))
1010 (insert-buffer-substring tmpbuf
)
1013 (goto-char (point-min))
1015 (search-backward "\n\^_")
1016 (narrow-to-region (point) (point-max)))
1017 (rmail-count-new-messages t
)
1018 (when (rmail-summary-exists)
1019 (rmail-select-summary
1020 (rmail-update-summary)))
1021 (rmail-show-message msg
))
1023 (kill-buffer tmpbuf
)))
1025 (defun gnus-output-to-mail (filename &optional ask
)
1026 "Append the current article to a mail file named FILENAME."
1028 (setq filename
(expand-file-name filename
))
1029 (let ((artbuf (current-buffer))
1030 (tmpbuf (get-buffer-create " *Gnus-output*")))
1032 ;; Create the file, if it doesn't exist.
1033 (when (and (not (get-file-buffer filename
))
1034 (not (file-exists-p filename
)))
1037 (concat "\"" filename
"\" does not exist, create it? ")))
1038 (let ((file-buffer (create-file-buffer filename
)))
1039 (with-current-buffer file-buffer
1040 (let ((require-final-newline nil
)
1041 (coding-system-for-write mm-text-coding-system
))
1042 (gnus-write-buffer filename
)))
1043 (kill-buffer file-buffer
))
1044 (error "Output file does not exist")))
1047 (insert-buffer-substring artbuf
)
1048 (goto-char (point-min))
1049 (if (looking-at "From ")
1051 (insert "From nobody " (current-time-string) "\n"))
1052 (let (case-fold-search)
1053 (while (re-search-forward "^From " nil t
)
1056 ;; Decide whether to append to a file or to an Emacs buffer.
1057 (let ((outbuf (get-file-buffer filename
)))
1059 (let ((buffer-read-only nil
))
1061 (goto-char (point-max))
1063 (unless (looking-at "\n\n")
1064 (goto-char (point-max))
1068 (goto-char (point-max))
1069 (let ((file-name-coding-system nnmail-pathname-coding-system
))
1070 (mm-append-to-file (point-min) (point-max) filename
))))
1071 ;; File has been visited, in buffer OUTBUF.
1073 (let ((buffer-read-only nil
))
1074 (goto-char (point-max))
1078 (insert-buffer-substring tmpbuf
)))))
1079 (kill-buffer tmpbuf
)))
1081 (defun gnus-convert-article-to-rmail ()
1082 "Convert article in current buffer to Rmail message format."
1083 (let ((buffer-read-only nil
))
1084 ;; Convert article directly into Babyl format.
1085 (goto-char (point-min))
1086 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
1087 (while (search-forward "\n\^_" nil t
) ;single char
1088 (replace-match "\n^_" t t
)) ;2 chars: "^" and "_"
1089 (goto-char (point-max))
1092 (defun gnus-map-function (funs arg
)
1093 "Apply the result of the first function in FUNS to the second, and so on.
1094 ARG is passed to the first function."
1096 (setq arg
(funcall (pop funs
) arg
)))
1099 (defun gnus-run-hooks (&rest funcs
)
1100 "Does the same as `run-hooks', but saves the current buffer."
1101 (save-current-buffer
1102 (apply 'run-hooks funcs
)))
1104 (defun gnus-run-hook-with-args (hook &rest args
)
1105 "Does the same as `run-hook-with-args', but saves the current buffer."
1106 (save-current-buffer
1107 (apply 'run-hook-with-args hook args
)))
1109 (defun gnus-run-mode-hooks (&rest funcs
)
1110 "Run `run-mode-hooks', saving the current buffer."
1111 (save-current-buffer (apply 'run-mode-hooks funcs
)))
1115 (defvar gnus-group-buffer
) ; Compiler directive
1116 (defun gnus-alive-p ()
1117 "Say whether Gnus is running or not."
1118 (and (boundp 'gnus-group-buffer
)
1119 (get-buffer gnus-group-buffer
)
1120 (with-current-buffer gnus-group-buffer
1121 (eq major-mode
'gnus-group-mode
))))
1123 (defun gnus-remove-if (predicate sequence
&optional hash-table-p
)
1124 "Return a copy of SEQUENCE with all items satisfying PREDICATE removed.
1125 SEQUENCE should be a list, a vector, or a string. Returns always a list.
1126 If HASH-TABLE-P is non-nil, regards SEQUENCE as a hash table."
1129 (mapatoms (lambda (symbol)
1130 (unless (funcall predicate symbol
)
1133 (unless (listp sequence
)
1134 (setq sequence
(append sequence nil
)))
1136 (unless (funcall predicate
(car sequence
))
1137 (push (car sequence
) out
))
1138 (setq sequence
(cdr sequence
))))
1141 (defun gnus-remove-if-not (predicate sequence
&optional hash-table-p
)
1142 "Return a copy of SEQUENCE with all items not satisfying PREDICATE removed.
1143 SEQUENCE should be a list, a vector, or a string. Returns always a list.
1144 If HASH-TABLE-P is non-nil, regards SEQUENCE as a hash table."
1147 (mapatoms (lambda (symbol)
1148 (when (funcall predicate symbol
)
1151 (unless (listp sequence
)
1152 (setq sequence
(append sequence nil
)))
1154 (when (funcall predicate
(car sequence
))
1155 (push (car sequence
) out
))
1156 (setq sequence
(cdr sequence
))))
1159 (defun gnus-grep-in-list (word list
)
1160 "Find if a WORD matches any regular expression in the given LIST."
1161 (when (and word list
)
1164 (when (string-match r word
)
1165 (throw 'found r
))))))
1167 (defmacro gnus-alist-pull
(key alist
&optional assoc-p
)
1168 "Modify ALIST to be without KEY."
1169 (unless (symbolp alist
)
1170 (error "Not a symbol: %s" alist
))
1171 (let ((fun (if assoc-p
'assoc
'assq
)))
1172 `(setq ,alist
(delq (,fun
,key
,alist
) ,alist
))))
1174 (defun gnus-globalify-regexp (re)
1175 "Return a regexp that matches a whole line, if RE matches a part of it."
1176 (concat (unless (string-match "^\\^" re
) "^.*")
1178 (unless (string-match "\\$$" re
) ".*$")))
1180 (defun gnus-set-window-start (&optional point
)
1181 "Set the window start to POINT, or (point) if nil."
1182 (let ((win (gnus-get-buffer-window (current-buffer) t
)))
1184 (set-window-start win
(or point
(point))))))
1186 (defun gnus-annotation-in-region-p (b e
)
1188 (eq (cadr (memq 'gnus-undeletable
(text-properties-at b
))) t
)
1189 (text-property-any b e
'gnus-undeletable t
)))
1191 (defun gnus-or (&rest elems
)
1192 "Return non-nil if any of the elements are non-nil."
1196 (throw 'found t
)))))
1198 (defun gnus-and (&rest elems
)
1199 "Return non-nil if all of the elements are non-nil."
1203 (throw 'found nil
)))
1206 ;; gnus.el requires mm-util.
1207 (declare-function mm-disable-multibyte
"mm-util")
1209 (defun gnus-write-active-file (file hashtb
&optional full-names
)
1210 ;; `coding-system-for-write' should be `raw-text' or equivalent.
1211 (let ((coding-system-for-write nnmail-active-file-coding-system
))
1212 (with-temp-file file
1213 ;; The buffer should be in the unibyte mode because group names
1214 ;; are ASCII text or encoded non-ASCII text (i.e., unibyte).
1215 (mm-disable-multibyte)
1221 (insert (format "%S %d %d y\n"
1224 (intern (gnus-group-real-name (symbol-name sym
))))
1225 (or (cdr (symbol-value sym
))
1226 (car (symbol-value sym
)))
1227 (car (symbol-value sym
))))))
1229 (goto-char (point-max))
1230 (while (search-backward "\\." nil t
)
1233 ;; Fixme: Why not use `with-output-to-temp-buffer'?
1234 (defmacro gnus-with-output-to-file
(file &rest body
)
1235 (let ((buffer (make-symbol "output-buffer"))
1236 (size (make-symbol "output-buffer-size"))
1237 (leng (make-symbol "output-buffer-length"))
1238 (append (make-symbol "output-buffer-append")))
1239 `(let* ((,size
131072)
1240 (,buffer
(make-string ,size
0))
1245 (aset ,buffer
,leng c
)
1247 (if (= ,size
(setq ,leng
(1+ ,leng
)))
1248 (progn (write-region ,buffer nil
,file
,append
'no-msg
)
1253 (let ((coding-system-for-write 'no-conversion
))
1254 (write-region (substring ,buffer
0 ,leng
) nil
,file
1255 ,append
'no-msg
))))))
1257 (put 'gnus-with-output-to-file
'lisp-indent-function
1)
1258 (put 'gnus-with-output-to-file
'edebug-form-spec
'(form body
))
1260 (defun gnus-add-text-properties-when
1261 (property value start end properties
&optional object
)
1262 "Like `add-text-properties', only applied on where PROPERTY is VALUE."
1265 (< start end
) ;; XEmacs will loop for every when start=end.
1266 (setq point
(text-property-not-all start end property value
)))
1267 (add-text-properties start point properties object
)
1268 (setq start
(text-property-any point end property value
)))
1270 (add-text-properties start end properties object
))))
1272 (defun gnus-remove-text-properties-when
1273 (property value start end properties
&optional object
)
1274 "Like `remove-text-properties', only applied on where PROPERTY is VALUE."
1278 (setq point
(text-property-not-all start end property value
)))
1279 (remove-text-properties start point properties object
)
1280 (setq start
(text-property-any point end property value
)))
1282 (remove-text-properties start end properties object
))
1285 (defun gnus-string-remove-all-properties (string)
1288 (set-text-properties 0 (length string
) nil string
)
1292 ;; This might use `compare-strings' to reduce consing in the
1293 ;; case-insensitive case, but it has to cope with null args.
1294 ;; (`string-equal' uses symbol print names.)
1295 (defun gnus-string-equal (x y
)
1296 "Like `string-equal', except it compares case-insensitively."
1297 (and (= (length x
) (length y
))
1298 (or (string-equal x y
)
1299 (string-equal (downcase x
) (downcase y
)))))
1301 (defcustom gnus-use-byte-compile t
1302 "If non-nil, byte-compile crucial run-time code.
1303 Setting it to nil has no effect after the first time `gnus-byte-compile'
1307 :group
'gnus-various
)
1309 (defun gnus-byte-compile (form)
1310 "Byte-compile FORM if `gnus-use-byte-compile' is non-nil."
1311 (if gnus-use-byte-compile
1314 (defalias 'gnus-byte-compile
1316 (let ((byte-compile-warnings '(unresolved callargs redefine
)))
1317 (byte-compile form
))))
1318 (gnus-byte-compile form
))
1321 (defun gnus-remassoc (key alist
)
1322 "Delete by side effect any elements of LIST whose car is `equal' to KEY.
1323 The modified LIST is returned. If the first member
1324 of LIST has a car that is `equal' to KEY, there is no way to remove it
1325 by side effect; therefore, write `(setq foo (gnus-remassoc key foo))' to be
1326 sure of changing the value of `foo'."
1328 (if (equal key
(caar alist
))
1330 (setcdr alist
(gnus-remassoc key
(cdr alist
)))
1333 (defun gnus-update-alist-soft (key value alist
)
1335 (cons (cons key value
) (gnus-remassoc key alist
))
1336 (gnus-remassoc key alist
)))
1338 (defun gnus-create-info-command (node)
1339 "Create a command that will go to info NODE."
1342 ,(concat "Enter the info system at node " node
)
1343 (Info-goto-node ,node
)
1344 (setq gnus-info-buffer
(current-buffer))
1345 (gnus-configure-windows 'info
)))
1347 (defun gnus-not-ignore (&rest args
)
1350 (defvar gnus-directory-sep-char-regexp
"/"
1351 "The regexp of directory separator character.
1352 If you find some problem with the directory separator character, try
1353 \"[/\\\\]\" for some systems.")
1355 (defun gnus-url-unhex (x)
1362 ;; Fixme: Do it like QP.
1363 (defun gnus-url-unhex-string (str &optional allow-newlines
)
1364 "Remove %XX, embedded spaces, etc in a url.
1365 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
1366 decoding of carriage returns and line feeds in the string, which is normally
1367 forbidden in URL encoding."
1369 (case-fold-search t
))
1370 (while (string-match "%[0-9a-f][0-9a-f]" str
)
1371 (let* ((start (match-beginning 0))
1372 (ch1 (gnus-url-unhex (elt str
(+ start
1))))
1374 (gnus-url-unhex (elt str
(+ start
2))))))
1376 tmp
(substring str
0 start
)
1379 (char-to-string code
))
1380 ((or (= code ?
\n) (= code ?
\r))
1382 (t (char-to-string code
))))
1383 str
(substring str
(match-end 0)))))
1384 (setq tmp
(concat tmp str
))
1387 (defun gnus-make-predicate (spec)
1388 "Transform SPEC into a function that can be called.
1389 SPEC is a predicate specifier that contains stuff like `or', `and',
1390 `not', lists and functions. The functions all take one parameter."
1391 `(lambda (elem) ,(gnus-make-predicate-1 spec
)))
1393 (defun gnus-make-predicate-1 (spec)
1398 (if (memq (car spec
) '(or and not
))
1399 `(,(car spec
) ,@(mapcar 'gnus-make-predicate-1
(cdr spec
)))
1400 (error "Invalid predicate specifier: %s" spec
)))))
1402 (defun gnus-completing-read (prompt collection
&optional require-match
1403 initial-input history def
)
1404 "Call `gnus-completing-read-function'."
1405 (funcall gnus-completing-read-function
1406 (concat prompt
(when def
1407 (concat " (default " def
")"))
1409 collection require-match initial-input history def
))
1411 (defun gnus-emacs-completing-read (prompt collection
&optional require-match
1412 initial-input history def
)
1413 "Call standard `completing-read-function'."
1414 (let ((completion-styles gnus-completion-styles
))
1415 (completing-read prompt collection
1416 nil require-match initial-input history def
)))
1418 (autoload 'ido-completing-read
"ido")
1419 (defun gnus-ido-completing-read (prompt collection
&optional require-match
1420 initial-input history def
)
1421 "Call `ido-completing-read-function'."
1422 (ido-completing-read prompt collection nil require-match
1423 initial-input history def
))
1426 (declare-function iswitchb-read-buffer
"iswitchb"
1427 (prompt &optional default require-match
1428 _predicate start matches-set
))
1429 (defvar iswitchb-temp-buflist
)
1430 (defvar iswitchb-mode
)
1432 (defun gnus-iswitchb-completing-read (prompt collection
&optional require-match
1433 initial-input history def
)
1434 "`iswitchb' based completing-read function."
1435 ;; Make sure iswitchb is loaded before we let-bind its variables.
1436 ;; If it is loaded inside the let, variables can become unbound afterwards.
1438 (let ((iswitchb-make-buflist-hook
1440 (setq iswitchb-temp-buflist
1441 (let ((choices (append
1442 (when initial-input
(list initial-input
))
1443 (symbol-value history
) collection
))
1446 (setq filtered-choices
(adjoin x filtered-choices
)))
1447 (nreverse filtered-choices
))))))
1451 (add-hook 'minibuffer-setup-hook
'iswitchb-minibuffer-setup
))
1452 (iswitchb-read-buffer prompt def require-match
))
1454 (remove-hook 'minibuffer-setup-hook
'iswitchb-minibuffer-setup
)))))
1456 (put 'gnus-parse-without-error
'lisp-indent-function
0)
1457 (put 'gnus-parse-without-error
'edebug-form-spec
'(body))
1459 (defmacro gnus-parse-without-error
(&rest body
)
1460 "Allow continuing onto the next line even if an error occurs."
1461 `(while (not (eobp))
1465 (goto-char (point-max)))
1467 (gnus-error 4 "Invalid data on line %d"
1468 (count-lines (point-min) (point)))
1469 (forward-line 1)))))
1471 (defun gnus-cache-file-contents (file variable function
)
1472 "Cache the contents of FILE in VARIABLE. The contents come from FUNCTION."
1473 (let ((time (nth 5 (file-attributes file
)))
1475 (if (or (null (setq value
(symbol-value variable
)))
1476 (not (equal (car value
) file
))
1477 (not (equal (nth 1 value
) time
)))
1479 (setq contents
(funcall function file
))
1480 (set variable
(list file time contents
))
1484 (defun gnus-multiple-choice (prompt choice
&optional idx
)
1485 "Ask user a multiple choice question.
1486 CHOICE is a list of the choice char and help message at IDX."
1488 (save-window-excursion
1491 (message "%s (%s): "
1494 (mapconcat (lambda (s) (char-to-string (car s
)))
1495 choice
", ") ", ?"))
1496 (setq tchar
(read-char))
1497 (when (not (assq tchar choice
))
1499 (setq buf
(get-buffer-create "*Gnus Help*"))
1502 (buffer-disable-undo)
1504 (insert prompt
":\n\n")
1511 ;; find the longest string to display
1513 (setq n
(length (nth idx
(car list
))))
1516 (setq list
(cdr list
)))
1517 (setq max
(+ max
4)) ; %c, `:', SPACE, a SPACE at end
1518 (setq n
(/ (1- (window-width)) max
)) ; items per line
1519 (setq width
(/ (1- (window-width)) n
)) ; width of each item
1520 ;; insert `n' items, each in a field of width `width'
1525 (delete-char -
1) ; the `\n' takes a char
1527 (setq pad
(- width
3))
1528 (setq format
(concat "%c: %-" (int-to-string pad
) "s"))
1529 (insert (format format
(caar alist
) (nth idx
(car alist
))))
1530 (setq alist
(cdr alist
))
1531 (setq i
(1+ i
))))))))
1532 (if (buffer-live-p buf
)
1536 (defun gnus-frame-or-window-display-name (object)
1537 "Given a frame or window, return the associated display name.
1538 Return nil otherwise."
1539 (if (or (framep object
)
1540 (and (windowp object
)
1541 (setq object
(window-frame object
))))
1542 (let ((display (frame-parameter object
'display
)))
1543 (if (and (stringp display
)
1544 ;; Exclude invalid display names.
1545 (string-match "\\`[^:]*:[0-9]+\\(\\.[0-9]+\\)?\\'"
1549 (defvar tool-bar-mode
)
1551 (defun gnus-tool-bar-update (&rest ignore
)
1552 "Update the tool bar."
1553 (when (and (boundp 'tool-bar-mode
)
1556 (func (cond ((fboundp 'tool-bar-update
)
1558 ((fboundp 'force-window-update
)
1559 'force-window-update
)
1560 ((fboundp 'redraw-frame
)
1561 (setq args
(list (selected-frame)))
1564 (apply func args
))))
1566 ;; Fixme: This has only one use (in gnus-agent), which isn't worthwhile.
1567 (defmacro gnus-mapcar
(function seq1
&rest seqs2_n
)
1568 "Apply FUNCTION to each element of the sequences, and make a list of the results.
1569 If there are several sequences, FUNCTION is called with that many arguments,
1570 and mapping stops as soon as the shortest sequence runs out. With just one
1571 sequence, this is like `mapcar'. With several, it is like the Common Lisp
1572 `mapcar' function extended to arbitrary sequence types."
1575 (let* ((seqs (cons seq1 seqs2_n
))
1577 (heads (mapcar (lambda (seq)
1578 (make-symbol (concat "head"
1580 (setq cnt
(1+ cnt
))))))
1582 (result (make-symbol "result"))
1583 (result-tail (make-symbol "result-tail")))
1584 `(let* ,(let* ((bindings (cons nil nil
))
1586 (nconc bindings
(list (list result
'(cons nil nil
))))
1587 (nconc bindings
(list (list result-tail result
)))
1589 (nconc bindings
(list (list (pop heads
) (pop seqs
)))))
1591 (while (and ,@heads
)
1592 (setcdr ,result-tail
(cons (funcall ,function
1593 ,@(mapcar (lambda (h) (list 'car h
))
1596 (setq ,result-tail
(cdr ,result-tail
)
1597 ,@(mapcan (lambda (h) (list h
(list 'cdr h
))) heads
)))
1599 `(mapcar ,function
,seq1
)))
1601 (defun gnus-emacs-version ()
1602 "Stringified Emacs version."
1603 (let* ((lst (if (listp gnus-user-agent
)
1605 '(gnus emacs type
)))
1606 (system-v (cond ((memq 'config lst
)
1607 system-configuration
)
1609 (symbol-name system-type
))
1613 ((not (memq 'emacs lst
))
1615 ((string-match "^\\(\\([.0-9]+\\)*\\)\\.[0-9]+$" emacs-version
)
1616 (concat "Emacs/" emacs-version
1618 (concat " (" system-v
")")
1620 (t emacs-version
))))
1622 (defun gnus-rename-file (old-path new-path
&optional trim
)
1623 "Rename OLD-PATH as NEW-PATH. If TRIM, recursively delete
1624 empty directories from OLD-PATH."
1625 (when (file-exists-p old-path
)
1626 (let* ((old-dir (file-name-directory old-path
))
1627 (old-name (file-name-nondirectory old-path
))
1628 (new-dir (file-name-directory new-path
))
1629 (new-name (file-name-nondirectory new-path
))
1631 (gnus-make-directory new-dir
)
1632 (rename-file old-path new-path t
)
1634 (while (progn (setq temp
(directory-files old-dir
))
1635 (while (member (car temp
) '("." ".."))
1636 (setq temp
(cdr temp
)))
1637 (= (length temp
) 0))
1638 (delete-directory old-dir
)
1639 (setq old-dir
(file-name-as-directory
1641 (concat old-dir
"..")))))))))
1643 (defun gnus-set-file-modes (filename mode
)
1644 "Wrapper for set-file-modes."
1646 (set-file-modes filename mode
)))
1648 (declare-function image-size
"image.c" (spec &optional pixels frame
))
1650 (defun gnus-rescale-image (image size
)
1651 "Rescale IMAGE to SIZE if possible.
1652 SIZE is in format (WIDTH . HEIGHT). Return a new image.
1653 Sizes are in pixels."
1654 (if (or (not (fboundp 'imagemagick-types
))
1655 (not (get-buffer-window (current-buffer))))
1657 (let ((new-width (car size
))
1658 (new-height (cdr size
)))
1659 (when (> (cdr (image-size image t
)) new-height
)
1660 (setq image
(or (create-image (plist-get (cdr image
) :data
) 'imagemagick t
1663 (when (> (car (image-size image t
)) new-width
)
1665 (create-image (plist-get (cdr image
) :data
) 'imagemagick t
1670 (defun gnus-recursive-directory-files (dir)
1671 "Return all regular files below DIR.
1672 The first found will be returned if a file has hard or symbolic links."
1673 (let (files attr attrs
)
1676 (dolist (file (directory-files directory t
))
1677 (setq attr
(file-attributes (file-truename file
)))
1678 (when (and (not (member attr attrs
))
1679 (not (member (file-name-nondirectory file
)
1681 (file-readable-p file
))
1683 (cond ((file-regular-p file
)
1685 ((file-directory-p file
)
1690 (defun gnus-list-memq-of-list (elements list
)
1691 "Return non-nil if any of the members of ELEMENTS are in LIST."
1693 (dolist (elem elements
)
1694 (setq found
(or found
1698 (defun gnus-test-list (list predicate
)
1699 "To each element of LIST apply PREDICATE.
1700 Return nil if LIST is no list or is empty or some test returns nil;
1701 otherwise, return t."
1702 (when (and list
(listp list
))
1703 (let ((result (mapcar predicate list
)))
1704 (not (memq nil result
)))))
1706 (defun gnus-subsetp (list1 list2
)
1707 "Return t if LIST1 is a subset of LIST2.
1708 Similar to `subsetp' but use member for element test so that this works for
1710 (when (and (listp list1
) (listp list2
))
1712 (and (member (car list1
) list2
)
1713 (gnus-subsetp (cdr list1
) list2
))
1716 (defun gnus-setdiff (list1 list2
)
1717 "Return member-based set difference of LIST1 and LIST2."
1718 (when (and list1
(listp list1
) (listp list2
))
1719 (if (member (car list1
) list2
)
1720 (gnus-setdiff (cdr list1
) list2
)
1721 (cons (car list1
) (gnus-setdiff (cdr list1
) list2
)))))
1723 ;;; Image functions.
1725 (defun gnus-image-type-available-p (type)
1726 (and (display-images-p)
1727 (image-type-available-p type
)))
1729 (defun gnus-create-image (file &optional type data-p
&rest props
)
1730 (let ((face (plist-get props
:face
)))
1732 (setq props
(plist-put props
:foreground
(face-foreground face
)))
1733 (setq props
(plist-put props
:background
(face-background face
))))
1735 (apply 'create-image file type data-p props
))))
1737 (defun gnus-put-image (glyph &optional string category
)
1738 (let ((point (point)))
1739 (insert-image glyph
(or string
" "))
1740 (put-text-property point
(point) 'gnus-image-category category
)
1742 (put-text-property (1- (point)) (point)
1743 'gnus-image-text-deletable t
))
1746 (defun gnus-remove-image (image &optional category
)
1747 "Remove the image matching IMAGE and CATEGORY found first."
1748 (let ((start (point-min))
1750 (while (and (not end
)
1751 (or (setq val
(get-text-property start
'display
))
1753 (next-single-property-change start
'display
))
1754 (setq val
(get-text-property start
'display
)))))
1755 (setq end
(or (next-single-property-change start
'display
)
1757 (if (and (equal val image
)
1758 (equal (get-text-property start
'gnus-image-category
)
1761 (put-text-property start end
'display nil
)
1762 (when (get-text-property start
'gnus-image-text-deletable
)
1763 (delete-region start end
)))
1764 (unless (= end
(point-max))
1768 (defun gnus-kill-all-overlays ()
1769 "Delete all overlays in the current buffer."
1770 (let* ((overlayss (overlay-lists))
1771 (buffer-read-only nil
)
1772 (overlays (delq nil
(nconc (car overlayss
) (cdr overlayss
)))))
1774 (delete-overlay (pop overlays
)))))
1776 (provide 'gnus-util
)
1778 ;;; gnus-util.el ends here