1 ;;; gnus-util.el --- utility functions for Gnus
3 ;; Copyright (C) 1996-2015 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
)
47 ;; iswitchb.el is very old and ido.el is unavailable
48 ;; in XEmacs, so we exclude those function items.
49 ,@(unless (featurep 'xemacs
)
51 :doc
"Use `ido-completing-read' function."
52 gnus-ido-completing-read
)
54 :doc
"Use iswitchb based completing-read function."
55 gnus-iswitchb-completing-read
)))))
57 (defcustom gnus-completion-styles
58 (if (and (boundp 'completion-styles-alist
)
59 (boundp 'completion-styles
))
60 (append (when (and (assq 'substring completion-styles-alist
)
61 (not (memq 'substring completion-styles
)))
65 "Value of `completion-styles' to use when completing."
68 :type
'(repeat symbol
))
70 ;; Fixme: this should be a gnus variable, not nnmail-.
71 (defvar nnmail-pathname-coding-system
)
72 (defvar nnmail-active-file-coding-system
)
74 ;; Inappropriate references to other parts of Gnus.
75 (defvar gnus-emphasize-whitespace-regexp
)
76 (defvar gnus-original-article-buffer
)
77 (defvar gnus-user-agent
)
79 (autoload 'gnus-get-buffer-window
"gnus-win")
80 (autoload 'nnheader-narrow-to-headers
"nnheader")
81 (autoload 'nnheader-replace-chars-in-string
"nnheader")
82 (autoload 'mail-header-remove-comments
"mail-parse")
86 ;; Prefer `replace-regexp-in-string' (present in Emacs, XEmacs 21.5,
87 ;; SXEmacs 22.1.4) over `replace-in-string'. The latter leads to inf-loops
89 ;; (replace-in-string "foo" "/*$" "/")
90 ;; (replace-in-string "xe" "\\(x\\)?" "")
91 ((fboundp 'replace-regexp-in-string
)
92 (defun gnus-replace-in-string (string regexp newtext
&optional literal
)
93 "Replace all matches for REGEXP with NEWTEXT in STRING.
94 If LITERAL is non-nil, insert NEWTEXT literally. Return a new
95 string containing the replacements.
97 This is a compatibility function for different Emacsen."
98 (replace-regexp-in-string regexp newtext string nil literal
)))
99 ((fboundp 'replace-in-string
)
100 (defalias 'gnus-replace-in-string
'replace-in-string
))))
102 (defun gnus-boundp (variable)
103 "Return non-nil if VARIABLE is bound and non-nil."
104 (and (boundp variable
)
105 (symbol-value variable
)))
107 (defmacro gnus-eval-in-buffer-window
(buffer &rest forms
)
108 "Pop to BUFFER, evaluate FORMS, and then return to the original window."
109 (let ((tempvar (make-symbol "GnusStartBufferWindow"))
110 (w (make-symbol "w"))
111 (buf (make-symbol "buf")))
112 `(let* ((,tempvar
(selected-window))
114 (,w
(gnus-get-buffer-window ,buf
'visible
)))
120 (set-buffer (window-buffer ,w
)))
121 (pop-to-buffer ,buf
))
123 (select-window ,tempvar
)))))
125 (put 'gnus-eval-in-buffer-window
'lisp-indent-function
1)
126 (put 'gnus-eval-in-buffer-window
'edebug-form-spec
'(form body
))
128 (defmacro gnus-intern-safe
(string hashtable
)
129 "Get hash value. Arguments are STRING and HASHTABLE."
130 `(let ((symbol (intern ,string
,hashtable
)))
135 (defsubst gnus-goto-char
(point)
136 (and point
(goto-char point
)))
138 (defmacro gnus-buffer-exists-p
(buffer)
139 `(let ((buffer ,buffer
))
141 (funcall (if (stringp buffer
) 'get-buffer
'buffer-name
)
144 ;; The LOCAL arg to `add-hook' is interpreted differently in Emacs and
145 ;; XEmacs. In Emacs we don't need to call `make-local-hook' first.
146 ;; It's harmless, though, so the main purpose of this alias is to shut
147 ;; up the byte compiler.
148 (defalias 'gnus-make-local-hook
(if (featurep 'xemacs
)
152 (defun gnus-delete-first (elt list
)
153 "Delete by side effect the first occurrence of ELT as a member of LIST."
154 (if (equal (car list
) elt
)
157 (while (and (cdr list
)
158 (not (equal (cadr list
) elt
)))
159 (setq list
(cdr list
)))
161 (setcdr list
(cddr list
)))
164 ;; Delete the current line (and the next N lines).
165 (defmacro gnus-delete-line
(&optional n
)
166 `(delete-region (point-at-bol)
167 (progn (forward-line ,(or n
1)) (point))))
169 (defun gnus-extract-address-components (from)
170 "Extract address components from a From header.
171 Given an RFC-822 address FROM, extract full name and canonical address.
172 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS). Much more simple
173 solution than `mail-extract-address-components', which works much better, but
176 ;; First find the address - the thing with the @ in it. This may
177 ;; not be accurate in mail addresses, but does the trick most of
178 ;; the time in news messages.
179 (cond (;; Check ``<foo@bar>'' first in order to handle the quite common
180 ;; form ``"abc@xyz" <foo@bar>'' (i.e. ``@'' as part of a comment)
182 (string-match "<\\([^@ \t<>]+[!@][^@ \t<>]+\\)>" from
)
183 (setq address
(substring from
(match-beginning 1) (match-end 1))))
184 ((string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from
)
185 (setq address
(substring from
(match-beginning 0) (match-end 0)))))
186 ;; Then we check whether the "name <address>" format is used.
188 ;; Linear white space is not required.
189 (string-match (concat "[ \t]*<" (regexp-quote address
) ">") from
)
190 (and (setq name
(substring from
0 (match-beginning 0)))
191 ;; Strip any quotes from the name.
192 (string-match "^\".*\"$" name
)
193 (setq name
(substring name
1 (1- (match-end 0))))))
194 ;; If not, then "address (name)" is used.
196 (and (string-match "(.+)" from
)
197 (setq name
(substring from
(1+ (match-beginning 0))
198 (1- (match-end 0)))))
199 (and (string-match "()" from
)
201 ;; XOVER might not support folded From headers.
202 (and (string-match "(.*" from
)
203 (setq name
(substring from
(1+ (match-beginning 0))
205 (list (if (string= name
"") nil name
) (or address from
))))
207 (declare-function message-fetch-field
"message" (header &optional not-all
))
209 (defun gnus-fetch-field (field)
210 "Return the value of the header FIELD of current article."
214 (let ((inhibit-point-motion-hooks t
))
215 (nnheader-narrow-to-headers)
216 (message-fetch-field field
)))))
218 (defun gnus-fetch-original-field (field)
219 "Fetch FIELD from the original version of the current article."
220 (with-current-buffer gnus-original-article-buffer
221 (gnus-fetch-field field
)))
224 (defun gnus-goto-colon ()
225 (move-beginning-of-line 1)
226 (let ((eol (point-at-eol)))
227 (goto-char (or (text-property-any (point) eol
'gnus-position t
)
228 (search-forward ":" eol t
)
231 (declare-function gnus-find-method-for-group
"gnus" (group &optional info
))
232 (declare-function gnus-group-name-decode
"gnus-group" (string charset
))
233 (declare-function gnus-group-name-charset
"gnus-group" (method group
))
234 ;; gnus-group requires gnus-int which requires message.
235 (declare-function message-tokenize-header
"message"
236 (header &optional separator
))
238 (defun gnus-decode-newsgroups (newsgroups group
&optional method
)
239 (require 'gnus-group
)
240 (let ((method (or method
(gnus-find-method-for-group group
))))
241 (mapconcat (lambda (group)
242 (gnus-group-name-decode group
(gnus-group-name-charset
244 (message-tokenize-header newsgroups
)
247 (defun gnus-remove-text-with-property (prop)
248 "Delete all text in the current buffer with text property PROP."
249 (let ((start (point-min))
251 (unless (get-text-property start prop
)
252 (setq start
(next-single-property-change start prop
)))
254 (setq end
(text-property-any start
(point-max) prop nil
))
255 (delete-region start
(or end
(point-max)))
256 (setq start
(when end
257 (next-single-property-change start prop
))))))
259 (defun gnus-find-text-property-region (start end prop
)
260 "Return a list of text property regions that has property PROP."
262 (unless (get-text-property start prop
)
263 (setq start
(next-single-property-change start prop
)))
265 (setq value
(get-text-property start prop
)
266 end
(text-property-not-all start
(point-max) prop value
))
270 (push (list (set-marker (make-marker) start
)
271 (set-marker (make-marker) end
)
274 (setq start
(next-single-property-change start prop
))))
277 (defun gnus-newsgroup-directory-form (newsgroup)
278 "Make hierarchical directory name from NEWSGROUP name."
279 (let* ((newsgroup (gnus-newsgroup-savable-name newsgroup
))
280 (idx (string-match ":" newsgroup
)))
282 (if idx
(substring newsgroup
0 idx
))
284 (nnheader-replace-chars-in-string
285 (if idx
(substring newsgroup
(1+ idx
)) newsgroup
)
288 (defun gnus-newsgroup-savable-name (group)
289 ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
291 (nnheader-replace-chars-in-string group ?
/ ?.
))
293 (defun gnus-string> (s1 s2
)
294 (not (or (string< s1 s2
)
297 (defun gnus-string< (s1 s2
)
298 "Return t if first arg string is less than second in lexicographic order.
299 Case is significant if and only if `case-fold-search' is nil.
300 Symbols are also allowed; their print names are used instead."
302 (string-lessp (downcase (if (symbolp s1
) (symbol-name s1
) s1
))
303 (downcase (if (symbolp s2
) (symbol-name s2
) s2
)))
304 (string-lessp s1 s2
)))
308 (defun gnus-file-newer-than (file date
)
309 (let ((fdate (nth 5 (file-attributes file
))))
310 (or (> (car fdate
) (car date
))
311 (and (= (car fdate
) (car date
))
312 (> (nth 1 fdate
) (nth 1 date
))))))
314 ;; Every version of Emacs Gnus supports has built-in float-time.
315 ;; The featurep test silences an irritating compiler warning.
316 (defalias 'gnus-float-time
317 (if (or (featurep 'emacs
)
318 (fboundp 'float-time
))
319 'float-time
'time-to-seconds
))
323 (defmacro gnus-local-set-keys
(&rest plist
)
324 "Set the keys in PLIST in the current keymap."
325 `(gnus-define-keys-1 (current-local-map) ',plist
))
327 (defmacro gnus-define-keys
(keymap &rest plist
)
328 "Define all keys in PLIST in KEYMAP."
329 ;; Convert the key [?\S-\ ] to [(shift space)] for XEmacs.
330 (when (featurep 'xemacs
)
331 (let ((bindings plist
))
333 (when (equal (car bindings
) [?\S-\
])
334 (setcar bindings
[(shift space
)]))
335 (setq bindings
(cddr bindings
)))))
336 `(gnus-define-keys-1 (quote ,keymap
) (quote ,plist
)))
338 (defmacro gnus-define-keys-safe
(keymap &rest plist
)
339 "Define all keys in PLIST in KEYMAP without overwriting previous definitions."
340 `(gnus-define-keys-1 (quote ,keymap
) (quote ,plist
) t
))
342 (put 'gnus-define-keys
'lisp-indent-function
1)
343 (put 'gnus-define-keys-safe
'lisp-indent-function
1)
344 (put 'gnus-local-set-keys
'lisp-indent-function
1)
346 (defmacro gnus-define-keymap
(keymap &rest plist
)
347 "Define all keys in PLIST in KEYMAP."
348 `(gnus-define-keys-1 ,keymap
(quote ,plist
)))
350 (put 'gnus-define-keymap
'lisp-indent-function
1)
352 (defun gnus-define-keys-1 (keymap plist
&optional safe
)
354 (error "Can't set keys in a null keymap"))
355 (cond ((symbolp keymap
)
356 (setq keymap
(symbol-value keymap
)))
359 (set (car keymap
) nil
)
360 (define-prefix-command (car keymap
))
361 (define-key (symbol-value (caddr keymap
)) (cadr keymap
) (car keymap
))
362 (setq keymap
(symbol-value (car keymap
)))))
365 (when (symbolp (setq key
(pop plist
)))
366 (setq key
(symbol-value key
)))
368 (eq (lookup-key keymap key
) 'undefined
))
369 (define-key keymap key
(pop plist
))
372 (defun gnus-y-or-n-p (prompt)
376 (defun gnus-yes-or-no-p (prompt)
381 ;; By Frank Schmitt <ich@Frank-Schmitt.net>. Allows to have
382 ;; age-depending date representations. (e.g. just the time if it's
383 ;; from today, the day of the week if it's within the last 7 days and
384 ;; the full date if it's older)
386 (defun gnus-seconds-today ()
387 "Return the number of seconds passed today."
388 (let ((now (decode-time)))
389 (+ (car now
) (* (car (cdr now
)) 60) (* (car (nthcdr 2 now
)) 3600))))
391 (defun gnus-seconds-month ()
392 "Return the number of seconds passed this month."
393 (let ((now (decode-time)))
394 (+ (car now
) (* (car (cdr now
)) 60) (* (car (nthcdr 2 now
)) 3600)
395 (* (- (car (nthcdr 3 now
)) 1) 3600 24))))
397 (defun gnus-seconds-year ()
398 "Return the number of seconds passed this year."
399 (let* ((current (current-time))
400 (now (decode-time current
))
401 (days (format-time-string "%j" current
)))
402 (+ (car now
) (* (car (cdr now
)) 60) (* (car (nthcdr 2 now
)) 3600)
403 (* (- (string-to-number days
) 1) 3600 24))))
405 (defmacro gnus-date-get-time
(date)
406 "Convert DATE string to Emacs time.
407 Cache the result as a text property stored in DATE."
408 ;; Either return the cached value...
412 (or (get-text-property 0 'gnus-time d
)
413 ;; or compute the value...
414 (let ((time (safe-date-to-time d
)))
415 ;; and store it back in the string.
416 (put-text-property 0 1 'gnus-time time d
)
419 (defun gnus-dd-mmm (messy-date)
420 "Return a string like DD-MMM from a big messy string."
422 (format-time-string "%d-%b" (gnus-date-get-time messy-date
))
425 (defsubst gnus-time-iso8601
(time)
426 "Return a string of TIME in YYYYMMDDTHHMMSS format."
427 (format-time-string "%Y%m%dT%H%M%S" time
))
429 (defun gnus-date-iso8601 (date)
430 "Convert the DATE to YYYYMMDDTHHMMSS."
432 (gnus-time-iso8601 (gnus-date-get-time date
))
435 (defun gnus-mode-string-quote (string)
436 "Quote all \"%\"'s in STRING."
437 (gnus-replace-in-string string
"%" "%%"))
439 ;; Make a hash table (default and minimum size is 256).
440 ;; Optional argument HASHSIZE specifies the table size.
441 (defun gnus-make-hashtable (&optional hashsize
)
442 (make-vector (if hashsize
(max (gnus-create-hash-size hashsize
) 256) 256) 0))
444 ;; Make a number that is suitable for hashing; bigger than MIN and
445 ;; equal to some 2^x. Many machines (such as sparcs) do not have a
446 ;; hardware modulo operation, so they implement it in software. On
447 ;; many sparcs over 50% of the time to intern is spent in the modulo.
448 ;; Yes, it's slower than actually computing the hash from the string!
449 ;; So we use powers of 2 so people can optimize the modulo to a mask.
450 (defun gnus-create-hash-size (min)
456 (defcustom gnus-verbose
6
457 "*Integer that says how verbose Gnus should be.
458 The higher the number, the more messages Gnus will flash to say what
459 it's doing. At zero, Gnus will be totally mute; at five, Gnus will
460 display most important messages; and at ten, Gnus will keep on
461 jabbering all the time."
466 (defcustom gnus-add-timestamp-to-message nil
467 "Non-nil means add timestamps to messages that Gnus issues.
468 If it is `log', add timestamps to only the messages that go into the
469 \"*Messages*\" buffer (in XEmacs, it is the \" *Message-Log*\" buffer).
470 If it is neither nil nor `log', add timestamps not only to log messages
471 but also to the ones displayed in the echo area."
472 :version
"23.1" ;; No Gnus
474 :type
'(choice :format
"%{%t%}:\n %[Value Menu%] %v"
475 (const :tag
"Logged messages only" log
)
476 (sexp :tag
"All messages"
477 :match
(lambda (widget value
) value
)
479 (const :tag
"No timestamp" nil
)))
482 (defmacro gnus-message-with-timestamp-1
(format-string args
)
483 (let ((timestamp '(format-time-string "%Y%m%dT%H%M%S.%3N> " time
)))
484 (if (featurep 'xemacs
)
486 (if (or (and (null ,format-string
) (null ,args
))
488 (setq str
(apply 'format
,format-string
,args
))
489 (zerop (length str
))))
491 (and ,format-string str
)
493 (cond ((eq gnus-add-timestamp-to-message
'log
)
494 (setq time
(current-time))
495 (display-message 'no-log str
)
496 (log-message 'message
(concat ,timestamp str
)))
497 (gnus-add-timestamp-to-message
498 (setq time
(current-time))
499 (display-message 'message
(concat ,timestamp str
)))
501 (display-message 'message str
))))
504 (cond ((eq gnus-add-timestamp-to-message
'log
)
505 (setq str
(let (message-log-max)
506 (apply 'message
,format-string
,args
)))
507 (when (and message-log-max
508 (> message-log-max
0)
510 (setq time
(current-time))
511 (with-current-buffer (if (fboundp 'messages-buffer
)
513 (get-buffer-create "*Messages*"))
514 (goto-char (point-max))
515 (let ((inhibit-read-only t
))
516 (insert ,timestamp str
"\n")
517 (forward-line (- message-log-max
))
518 (delete-region (point-min) (point)))
519 (goto-char (point-max))))
521 (gnus-add-timestamp-to-message
522 (if (or (and (null ,format-string
) (null ,args
))
524 (setq str
(apply 'format
,format-string
,args
))
525 (zerop (length str
))))
527 (and ,format-string str
)
529 (setq time
(current-time))
530 (message "%s" (concat ,timestamp str
))
533 (apply 'message
,format-string
,args
))))))))
535 (defvar gnus-action-message-log nil
)
537 (defun gnus-message-with-timestamp (format-string &rest args
)
538 "Display message with timestamp. Arguments are the same as `message'.
539 The `gnus-add-timestamp-to-message' variable controls how to add
540 timestamp to message."
541 (gnus-message-with-timestamp-1 format-string args
))
543 (defun gnus-message (level &rest args
)
544 "If LEVEL is lower than `gnus-verbose' print ARGS using `message'.
546 Guideline for numbers:
547 1 - error messages, 3 - non-serious error messages, 5 - messages for things
548 that take a long time, 7 - not very important messages on stuff, 9 - messages
550 (if (<= level gnus-verbose
)
552 (if gnus-add-timestamp-to-message
553 (apply 'gnus-message-with-timestamp args
)
554 (apply 'message args
))))
555 (when (and (consp gnus-action-message-log
)
557 (push message gnus-action-message-log
))
559 ;; We have to do this format thingy here even if the result isn't
560 ;; shown - the return value has to be the same as the return value
562 (apply 'format args
)))
564 (defun gnus-final-warning ()
565 (when (and (consp gnus-action-message-log
)
566 (setq gnus-action-message-log
567 (delete nil gnus-action-message-log
)))
568 (message "Warning: %s"
569 (mapconcat #'identity gnus-action-message-log
"; "))))
571 (defun gnus-error (level &rest args
)
572 "Beep an error if LEVEL is equal to or less than `gnus-verbose'.
573 ARGS are passed to `message'."
574 (when (<= (floor level
) gnus-verbose
)
575 (apply 'message args
)
578 (when (and (floatp level
)
579 (not (zerop (setq duration
(* 10 (- level
(floor level
)))))))
580 (sit-for duration
))))
583 (defun gnus-split-references (references)
584 "Return a list of Message-IDs in REFERENCES."
586 (references (mail-header-remove-comments (or references
"")))
588 (while (string-match "<[^<]+[^< \t]" references beg
)
589 (push (substring references
(match-beginning 0) (setq beg
(match-end 0)))
593 (defun gnus-extract-references (references)
594 "Return a list of Message-IDs in REFERENCES (in In-Reply-To
595 format), trimmed to only contain the Message-IDs."
596 (let ((ids (gnus-split-references references
))
599 (when (string-match "<[^<>]+>" id
)
600 (push (match-string 0 id
) refs
)))
603 (defsubst gnus-parent-id
(references &optional n
)
604 "Return the last Message-ID in REFERENCES.
605 If N, return the Nth ancestor instead."
606 (when (and references
607 (not (zerop (length references
))))
609 (let ((ids (inline (gnus-split-references references
))))
610 (while (nthcdr n ids
)
611 (setq ids
(cdr ids
)))
613 (let ((references (mail-header-remove-comments references
)))
614 (when (string-match "\\(<[^<]+>\\)[ \t]*\\'" references
)
615 (match-string 1 references
))))))
617 (defsubst gnus-buffer-live-p
(buffer)
618 "Say whether BUFFER is alive or not."
619 (and buffer
(buffer-live-p (get-buffer buffer
))))
621 (defun gnus-horizontal-recenter ()
622 "Recenter the current buffer horizontally."
623 (if (< (current-column) (/ (window-width) 2))
624 (set-window-hscroll (gnus-get-buffer-window (current-buffer) t
) 0)
625 (let* ((orig (point))
626 (end (window-end (gnus-get-buffer-window (current-buffer) t
)))
629 ;; Find the longest line currently displayed in the window.
630 (goto-char (window-start))
631 (while (and (not (eobp))
634 (setq max
(max max
(current-column)))
637 ;; Scroll horizontally to center (sort of) the point.
638 (if (> max
(window-width))
640 (gnus-get-buffer-window (current-buffer) t
)
641 (min (- (current-column) (/ (window-width) 3))
642 (+ 2 (- max
(window-width)))))
643 (set-window-hscroll (gnus-get-buffer-window (current-buffer) t
) 0))
646 (defun gnus-read-event-char (&optional prompt
)
647 "Get the next event."
648 (let ((event (read-event prompt
)))
649 ;; should be gnus-characterp, but this can't be called in XEmacs anyway
650 (cons (and (numberp event
) event
) event
)))
652 (defun gnus-copy-file (file &optional to
)
655 (list (read-file-name "Copy file: " default-directory
)
656 (read-file-name "Copy file to: " default-directory
)))
658 (setq to
(read-file-name "Copy file to: " default-directory
)))
659 (when (file-directory-p to
)
660 (setq to
(concat (file-name-as-directory to
)
661 (file-name-nondirectory file
))))
664 (defvar gnus-work-buffer
" *gnus work*")
666 (declare-function gnus-get-buffer-create
"gnus" (name))
667 ;; gnus.el requires mm-util.
668 (declare-function mm-enable-multibyte
"mm-util")
670 (defun gnus-set-work-buffer ()
671 "Put point in the empty Gnus work buffer."
672 (if (get-buffer gnus-work-buffer
)
674 (set-buffer gnus-work-buffer
)
676 (set-buffer (gnus-get-buffer-create gnus-work-buffer
))
677 (kill-all-local-variables)
678 (mm-enable-multibyte)))
680 (defmacro gnus-group-real-name
(group)
681 "Find the real name of a foreign newsgroup."
682 `(let ((gname ,group
))
683 (if (string-match "^[^:]+:" gname
)
684 (substring gname
(match-end 0))
687 (defmacro gnus-group-server
(group)
688 "Find the server name of a foreign newsgroup.
689 For example, (gnus-group-server \"nnimap+yxa:INBOX.foo\") would
690 yield \"nnimap:yxa\"."
691 `(let ((gname ,group
))
692 (if (string-match "^\\([^:+]+\\)\\(?:\\+\\([^:]*\\)\\)?:" gname
)
693 (format "%s:%s" (match-string 1 gname
) (or
694 (match-string 2 gname
)
696 (format "%s:%s" (car gnus-select-method
) (cadr gnus-select-method
)))))
698 (defun gnus-make-sort-function (funs)
699 "Return a composite sort condition based on the functions in FUNS."
701 ;; Just a simple function.
702 ((functionp funs
) funs
)
703 ;; No functions at all.
705 ;; A list of functions.
710 ,(gnus-make-sort-function-1 (reverse funs
)))))
711 ;; A list containing just one function.
715 (defun gnus-make-sort-function-1 (funs)
716 "Return a composite sort condition based on the functions in FUNS."
717 (let ((function (car funs
))
720 (when (consp function
)
723 ((eq (car function
) 'not
)
724 (setq function
(cadr function
)
727 ((functionp function
)
731 (error "Invalid sort spec: %s" function
))))
733 `(or (,function
,first
,last
)
734 (and (not (,function
,last
,first
))
735 ,(gnus-make-sort-function-1 (cdr funs
))))
736 `(,function
,first
,last
))))
738 (defun gnus-turn-off-edit-menu (type)
739 "Turn off edit menu in `gnus-TYPE-mode-map'."
740 (define-key (symbol-value (intern (format "gnus-%s-mode-map" type
)))
741 [menu-bar edit
] 'undefined
))
743 (defmacro gnus-bind-print-variables
(&rest forms
)
744 "Bind print-* variables and evaluate FORMS.
745 This macro is used with `prin1', `pp', etc. in order to ensure printed
746 Lisp objects are loadable. Bind `print-quoted' and `print-readably'
747 to t, and `print-escape-multibyte', `print-escape-newlines',
748 `print-escape-nonascii', `print-length', `print-level' and
749 `print-string-length' to nil."
750 `(let ((print-quoted t
)
753 ;;print-continuous-numbering
754 print-escape-multibyte
755 print-escape-newlines
756 print-escape-nonascii
763 (defun gnus-prin1 (form)
764 "Use `prin1' on FORM in the current buffer.
765 Bind `print-quoted' and `print-readably' to t, and `print-length' and
766 `print-level' to nil. See also `gnus-bind-print-variables'."
767 (gnus-bind-print-variables (prin1 form
(current-buffer))))
769 (defun gnus-prin1-to-string (form)
770 "The same as `prin1'.
771 Bind `print-quoted' and `print-readably' to t, and `print-length' and
772 `print-level' to nil. See also `gnus-bind-print-variables'."
773 (gnus-bind-print-variables (prin1-to-string form
)))
775 (defun gnus-pp (form &optional stream
)
776 "Use `pp' on FORM in the current buffer.
777 Bind `print-quoted' and `print-readably' to t, and `print-length' and
778 `print-level' to nil. See also `gnus-bind-print-variables'."
779 (gnus-bind-print-variables (pp form
(or stream
(current-buffer)))))
781 (defun gnus-pp-to-string (form)
782 "The same as `pp-to-string'.
783 Bind `print-quoted' and `print-readably' to t, and `print-length' and
784 `print-level' to nil. See also `gnus-bind-print-variables'."
785 (gnus-bind-print-variables (pp-to-string form
)))
787 (defun gnus-make-directory (directory)
788 "Make DIRECTORY (and all its parents) if it doesn't exist."
790 (let ((file-name-coding-system nnmail-pathname-coding-system
))
792 (not (file-exists-p directory
)))
793 (make-directory directory t
)))
796 (defun gnus-write-buffer (file)
797 "Write the current buffer's contents to FILE."
799 (let ((file-name-coding-system nnmail-pathname-coding-system
))
800 ;; Make sure the directory exists.
801 (gnus-make-directory (file-name-directory file
))
803 (write-region (point-min) (point-max) file nil
'quietly
)))
805 (defun gnus-delete-file (file)
806 "Delete FILE if it exists."
807 (when (file-exists-p file
)
810 (defun gnus-delete-duplicates (list)
811 "Remove duplicate entries from LIST."
814 (unless (member (car list
) result
)
815 (push (car list
) result
))
819 (defun gnus-delete-directory (directory)
820 "Delete files in DIRECTORY. Subdirectories remain.
821 If there's no subdirectory, delete DIRECTORY as well."
822 (when (file-directory-p directory
)
823 (let ((files (directory-files
824 directory t
"^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
827 (setq file
(pop files
))
828 (if (eq t
(car (file-attributes file
)))
829 ;; `file' is a subdirectory.
831 ;; `file' is a file or a symlink.
834 (delete-directory directory
)))))
836 (defun gnus-strip-whitespace (string)
837 "Return STRING stripped of all whitespace."
838 (while (string-match "[\r\n\t ]+" string
)
839 (setq string
(replace-match "" t t string
)))
842 (declare-function gnus-put-text-property
"gnus"
843 (start end property value
&optional object
))
845 (defsubst gnus-put-text-property-excluding-newlines
(beg end prop val
)
846 "The same as `put-text-property', but don't put this prop on any newlines in the region."
851 (while (re-search-forward gnus-emphasize-whitespace-regexp end
'move
)
852 (gnus-put-text-property beg
(match-beginning 0) prop val
)
854 (gnus-put-text-property beg
(point) prop val
)))))
856 (declare-function gnus-overlay-put
"gnus" (overlay prop value
))
857 (declare-function gnus-make-overlay
"gnus"
858 (beg end
&optional buffer front-advance rear-advance
))
860 (defsubst gnus-put-overlay-excluding-newlines
(beg end prop val
)
861 "The same as `put-text-property', but don't put this prop on any newlines in the region."
866 (while (re-search-forward gnus-emphasize-whitespace-regexp end
'move
)
868 (gnus-make-overlay beg
(match-beginning 0))
871 (gnus-overlay-put (gnus-make-overlay beg
(point)) prop val
)))))
873 (defun gnus-put-text-property-excluding-characters-with-faces (beg end prop val
)
874 "The same as `put-text-property', except where `gnus-face' is set.
875 If so, and PROP is `face', set the second element of its value to VAL.
876 Otherwise, do nothing."
878 ;; Property values are compared with `eq'.
879 (let ((stop (next-single-property-change beg
'face nil end
)))
880 (if (get-text-property beg
'gnus-face
)
881 (when (eq prop
'face
)
882 (setcar (cdr (get-text-property beg
'face
)) (or val
'default
)))
884 (gnus-put-text-property beg stop prop val
)))
887 (defun gnus-get-text-property-excluding-characters-with-faces (pos prop
)
888 "The same as `get-text-property', except where `gnus-face' is set.
889 If so, and PROP is `face', return the second element of its value.
890 Otherwise, return the value."
891 (let ((val (get-text-property pos prop
)))
892 (if (and (get-text-property pos
'gnus-face
)
895 (get-text-property pos prop
))))
897 (defmacro gnus-faces-at
(position)
898 "Return a list of faces at POSITION."
899 (if (featurep 'xemacs
)
900 `(let ((pos ,position
))
901 (mapcar-extents 'extent-face
902 nil
(current-buffer) pos pos nil
'face
))
903 `(let ((pos ,position
))
904 (delq nil
(cons (get-text-property pos
'face
)
907 (overlay-get overlay
'face
))
908 (overlays-at pos
)))))))
910 (if (fboundp 'invisible-p
)
911 (defalias 'gnus-invisible-p
'invisible-p
)
912 ;; for Emacs < 22.2, and XEmacs.
913 (defun gnus-invisible-p (pos)
914 "Return non-nil if the character after POS is currently invisible."
915 (let ((prop (get-char-property pos
'invisible
)))
916 (if (eq buffer-invisibility-spec t
)
918 (or (memq prop buffer-invisibility-spec
)
919 (assq prop buffer-invisibility-spec
))))))
921 ;; Note: the optional 2nd argument has a different meaning between
923 ;; (next-char-property-change POSITION &optional LIMIT)
924 ;; (next-extent-change POS &optional OBJECT)
925 (defalias 'gnus-next-char-property-change
926 (if (fboundp 'next-extent-change
)
927 'next-extent-change
'next-char-property-change
))
929 (defalias 'gnus-previous-char-property-change
930 (if (fboundp 'previous-extent-change
)
931 'previous-extent-change
'previous-char-property-change
))
933 ;;; Protected and atomic operations. dmoore@ucsd.edu 21.11.1996
934 ;; The primary idea here is to try to protect internal data structures
935 ;; from becoming corrupted when the user hits C-g, or if a hook or
936 ;; similar blows up. Often in Gnus multiple tables/lists need to be
937 ;; updated at the same time, or information can be lost.
939 (defvar gnus-atomic-be-safe t
940 "If t, certain operations will be protected from interruption by C-g.")
942 (defmacro gnus-atomic-progn
(&rest forms
)
943 "Evaluate FORMS atomically, which means to protect the evaluation
944 from being interrupted by the user. An error from the forms themselves
945 will return without finishing the operation. Since interrupts from
946 the user are disabled, it is recommended that only the most minimal
947 operations are performed by FORMS. If you wish to assign many
948 complicated values atomically, compute the results into temporary
949 variables and then do only the assignment atomically."
950 `(let ((inhibit-quit gnus-atomic-be-safe
))
953 (put 'gnus-atomic-progn
'lisp-indent-function
0)
955 (defmacro gnus-atomic-progn-assign
(protect &rest forms
)
956 "Evaluate FORMS, but ensure that the variables listed in PROTECT
957 are not changed if anything in FORMS signals an error or otherwise
958 non-locally exits. The variables listed in PROTECT are updated atomically.
959 It is safe to use gnus-atomic-progn-assign with long computations.
961 Note that if any of the symbols in PROTECT were unbound, they will be
962 set to nil on a successful assignment. In case of an error or other
963 non-local exit, it will still be unbound."
964 (let* ((temp-sym-map (mapcar (lambda (x) (list (make-symbol
965 (concat (symbol-name x
)
969 (sym-temp-map (mapcar (lambda (x) (list (cadr x
) (car x
)))
971 (temp-sym-let (mapcar (lambda (x) (list (car x
)
972 `(and (boundp ',(cadr x
))
975 (sym-temp-let sym-temp-map
)
976 (temp-sym-assign (apply 'append temp-sym-map
))
977 (sym-temp-assign (apply 'append sym-temp-map
))
978 (result (make-symbol "result-tmp")))
979 `(let (,@temp-sym-let
982 (setq ,result
(progn ,@forms
))
983 (setq ,@temp-sym-assign
))
984 (let ((inhibit-quit gnus-atomic-be-safe
))
985 (setq ,@sym-temp-assign
))
988 (put 'gnus-atomic-progn-assign
'lisp-indent-function
1)
989 ;(put 'gnus-atomic-progn-assign 'edebug-form-spec '(sexp body))
991 (defmacro gnus-atomic-setq
(&rest pairs
)
992 "Similar to setq, except that the real symbols are only assigned when
993 there are no errors. And when the real symbols are assigned, they are
994 done so atomically. If other variables might be changed via side-effect,
995 see gnus-atomic-progn-assign. It is safe to use gnus-atomic-setq
996 with potentially long computations."
1000 (push (car tpairs
) syms
)
1001 (setq tpairs
(cddr tpairs
)))
1002 `(gnus-atomic-progn-assign ,syms
1005 ;(put 'gnus-atomic-setq 'edebug-form-spec '(body))
1008 ;;; Functions for saving to babyl/mail files.
1011 (if (featurep 'xemacs
)
1012 ;; Don't load tm and apel XEmacs packages that provide some
1013 ;; Emacs emulating functions and variables.
1014 (let ((features features
))
1016 (unless (fboundp 'set-alist
) (defalias 'set-alist
'ignore
))
1017 (require 'rmail
)) ;; It requires tm-view that loads apel.
1019 (autoload 'rmail-update-summary
"rmailsum"))
1021 (defvar mm-text-coding-system
)
1023 (declare-function mm-append-to-file
"mm-util"
1024 (start end filename
&optional codesys inhibit
))
1025 (declare-function rmail-swap-buffers-maybe
"rmail" ())
1026 (declare-function rmail-maybe-set-message-counters
"rmail" ())
1027 (declare-function rmail-count-new-messages
"rmail" (&optional nomsg
))
1028 (declare-function rmail-summary-exists
"rmail" ())
1029 (declare-function rmail-show-message
"rmail" (&optional n no-summary
))
1030 ;; Macroexpansion of rmail-select-summary:
1031 (declare-function rmail-summary-displayed
"rmail" ())
1032 (declare-function rmail-pop-to-buffer
"rmail" (&rest args
))
1033 (declare-function rmail-maybe-display-summary
"rmail" ())
1035 (defun gnus-output-to-rmail (filename &optional ask
)
1036 "Append the current article to an Rmail file named FILENAME.
1037 In Emacs 22 this writes Babyl format; in Emacs 23 it writes mbox unless
1038 FILENAME exists and is Babyl format."
1042 ;; Some of this codes is borrowed from rmailout.el.
1043 (setq filename
(expand-file-name filename
))
1044 ;; FIXME should we really be messing with this defcustom?
1045 ;; It is not needed for the operation of this function.
1046 (if (boundp 'rmail-default-rmail-file
)
1047 (setq rmail-default-rmail-file filename
) ; 22
1048 (setq rmail-default-file filename
)) ; 23
1049 (let ((artbuf (current-buffer))
1050 (tmpbuf (get-buffer-create " *Gnus-output*"))
1051 ;; Babyl rmail.el defines this, mbox does not.
1052 (babyl (fboundp 'rmail-insert-rmail-file-header
)))
1054 ;; Note that we ignore the possibility of visiting a Babyl
1055 ;; format buffer in Emacs 23, since Rmail no longer supports that.
1056 (or (get-file-buffer filename
)
1058 ;; In case someone wants to write to a Babyl file from Emacs 23.
1059 (when (file-exists-p filename
)
1060 (setq babyl
(mail-file-babyl-p filename
))
1064 (concat "\"" filename
"\" does not exist, create it? ")))
1065 (let ((file-buffer (create-file-buffer filename
)))
1066 (with-current-buffer file-buffer
1067 (if (fboundp 'rmail-insert-rmail-file-header
)
1068 (rmail-insert-rmail-file-header))
1069 (let ((require-final-newline nil
)
1070 (coding-system-for-write mm-text-coding-system
))
1071 (gnus-write-buffer filename
)))
1072 (kill-buffer file-buffer
))
1073 (error "Output file does not exist")))
1076 (insert-buffer-substring artbuf
)
1078 (gnus-convert-article-to-rmail)
1079 ;; Non-Babyl case copied from gnus-output-to-mail.
1080 (goto-char (point-min))
1081 (if (looking-at "From ")
1083 (insert "From nobody " (current-time-string) "\n"))
1084 (let (case-fold-search)
1085 (while (re-search-forward "^From " nil t
)
1088 ;; Decide whether to append to a file or to an Emacs buffer.
1089 (let ((outbuf (get-file-buffer filename
)))
1092 (unless babyl
; from gnus-output-to-mail
1093 (let ((buffer-read-only nil
))
1094 (goto-char (point-max))
1096 (unless (looking-at "\n\n")
1097 (goto-char (point-max))
1101 (let ((file-name-coding-system nnmail-pathname-coding-system
))
1102 (mm-append-to-file (point-min) (point-max) filename
)))
1103 ;; File has been visited, in buffer OUTBUF.
1105 (let ((buffer-read-only nil
)
1106 (msg (and (boundp 'rmail-current-message
)
1107 (symbol-value 'rmail-current-message
))))
1108 ;; If MSG is non-nil, buffer is in RMAIL mode.
1109 ;; Compare this with rmail-output-to-rmail-buffer in Emacs 23.
1112 (rmail-swap-buffers-maybe)
1113 (rmail-maybe-set-message-counters))
1115 (narrow-to-region (point-max) (point-max)))
1116 (insert-buffer-substring tmpbuf
)
1119 (goto-char (point-min))
1121 (search-backward "\n\^_")
1122 (narrow-to-region (point) (point-max)))
1123 (rmail-count-new-messages t
)
1124 (when (rmail-summary-exists)
1125 (rmail-select-summary
1126 (rmail-update-summary)))
1127 (rmail-show-message msg
))
1129 (kill-buffer tmpbuf
)))
1131 (defun gnus-output-to-mail (filename &optional ask
)
1132 "Append the current article to a mail file named FILENAME."
1134 (setq filename
(expand-file-name filename
))
1135 (let ((artbuf (current-buffer))
1136 (tmpbuf (get-buffer-create " *Gnus-output*")))
1138 ;; Create the file, if it doesn't exist.
1139 (when (and (not (get-file-buffer filename
))
1140 (not (file-exists-p filename
)))
1143 (concat "\"" filename
"\" does not exist, create it? ")))
1144 (let ((file-buffer (create-file-buffer filename
)))
1145 (with-current-buffer file-buffer
1146 (let ((require-final-newline nil
)
1147 (coding-system-for-write mm-text-coding-system
))
1148 (gnus-write-buffer filename
)))
1149 (kill-buffer file-buffer
))
1150 (error "Output file does not exist")))
1153 (insert-buffer-substring artbuf
)
1154 (goto-char (point-min))
1155 (if (looking-at "From ")
1157 (insert "From nobody " (current-time-string) "\n"))
1158 (let (case-fold-search)
1159 (while (re-search-forward "^From " nil t
)
1162 ;; Decide whether to append to a file or to an Emacs buffer.
1163 (let ((outbuf (get-file-buffer filename
)))
1165 (let ((buffer-read-only nil
))
1167 (goto-char (point-max))
1169 (unless (looking-at "\n\n")
1170 (goto-char (point-max))
1174 (goto-char (point-max))
1175 (let ((file-name-coding-system nnmail-pathname-coding-system
))
1176 (mm-append-to-file (point-min) (point-max) filename
))))
1177 ;; File has been visited, in buffer OUTBUF.
1179 (let ((buffer-read-only nil
))
1180 (goto-char (point-max))
1184 (insert-buffer-substring tmpbuf
)))))
1185 (kill-buffer tmpbuf
)))
1187 (defun gnus-convert-article-to-rmail ()
1188 "Convert article in current buffer to Rmail message format."
1189 (let ((buffer-read-only nil
))
1190 ;; Convert article directly into Babyl format.
1191 (goto-char (point-min))
1192 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
1193 (while (search-forward "\n\^_" nil t
) ;single char
1194 (replace-match "\n^_" t t
)) ;2 chars: "^" and "_"
1195 (goto-char (point-max))
1198 (defun gnus-map-function (funs arg
)
1199 "Apply the result of the first function in FUNS to the second, and so on.
1200 ARG is passed to the first function."
1202 (setq arg
(funcall (pop funs
) arg
)))
1205 (defun gnus-run-hooks (&rest funcs
)
1206 "Does the same as `run-hooks', but saves the current buffer."
1207 (save-current-buffer
1208 (apply 'run-hooks funcs
)))
1210 (defun gnus-run-hook-with-args (hook &rest args
)
1211 "Does the same as `run-hook-with-args', but saves the current buffer."
1212 (save-current-buffer
1213 (apply 'run-hook-with-args hook args
)))
1215 (defun gnus-run-mode-hooks (&rest funcs
)
1216 "Run `run-mode-hooks' if it is available, otherwise `run-hooks'.
1217 This function saves the current buffer."
1218 (if (fboundp 'run-mode-hooks
)
1219 (save-current-buffer (apply 'run-mode-hooks funcs
))
1220 (save-current-buffer (apply 'run-hooks funcs
))))
1224 (defvar gnus-group-buffer
) ; Compiler directive
1225 (defun gnus-alive-p ()
1226 "Say whether Gnus is running or not."
1227 (and (boundp 'gnus-group-buffer
)
1228 (get-buffer gnus-group-buffer
)
1229 (with-current-buffer gnus-group-buffer
1230 (eq major-mode
'gnus-group-mode
))))
1232 (defun gnus-remove-if (predicate sequence
&optional hash-table-p
)
1233 "Return a copy of SEQUENCE with all items satisfying PREDICATE removed.
1234 SEQUENCE should be a list, a vector, or a string. Returns always a list.
1235 If HASH-TABLE-P is non-nil, regards SEQUENCE as a hash table."
1238 (mapatoms (lambda (symbol)
1239 (unless (funcall predicate symbol
)
1242 (unless (listp sequence
)
1243 (setq sequence
(append sequence nil
)))
1245 (unless (funcall predicate
(car sequence
))
1246 (push (car sequence
) out
))
1247 (setq sequence
(cdr sequence
))))
1250 (defun gnus-remove-if-not (predicate sequence
&optional hash-table-p
)
1251 "Return a copy of SEQUENCE with all items not satisfying PREDICATE removed.
1252 SEQUENCE should be a list, a vector, or a string. Returns always a list.
1253 If HASH-TABLE-P is non-nil, regards SEQUENCE as a hash table."
1256 (mapatoms (lambda (symbol)
1257 (when (funcall predicate symbol
)
1260 (unless (listp sequence
)
1261 (setq sequence
(append sequence nil
)))
1263 (when (funcall predicate
(car sequence
))
1264 (push (car sequence
) out
))
1265 (setq sequence
(cdr sequence
))))
1268 (if (fboundp 'assq-delete-all
)
1269 (defalias 'gnus-delete-alist
'assq-delete-all
)
1270 (defun gnus-delete-alist (key alist
)
1271 "Delete from ALIST all elements whose car is KEY.
1272 Return the modified alist."
1274 (while (setq entry
(assq key alist
))
1275 (setq alist
(delq entry alist
)))
1278 (defun gnus-grep-in-list (word list
)
1279 "Find if a WORD matches any regular expression in the given LIST."
1280 (when (and word list
)
1283 (when (string-match r word
)
1284 (throw 'found r
))))))
1286 (defmacro gnus-alist-pull
(key alist
&optional assoc-p
)
1287 "Modify ALIST to be without KEY."
1288 (unless (symbolp alist
)
1289 (error "Not a symbol: %s" alist
))
1290 (let ((fun (if assoc-p
'assoc
'assq
)))
1291 `(setq ,alist
(delq (,fun
,key
,alist
) ,alist
))))
1293 (defun gnus-globalify-regexp (re)
1294 "Return a regexp that matches a whole line, if RE matches a part of it."
1295 (concat (unless (string-match "^\\^" re
) "^.*")
1297 (unless (string-match "\\$$" re
) ".*$")))
1299 (defun gnus-set-window-start (&optional point
)
1300 "Set the window start to POINT, or (point) if nil."
1301 (let ((win (gnus-get-buffer-window (current-buffer) t
)))
1303 (set-window-start win
(or point
(point))))))
1305 (defun gnus-annotation-in-region-p (b e
)
1307 (eq (cadr (memq 'gnus-undeletable
(text-properties-at b
))) t
)
1308 (text-property-any b e
'gnus-undeletable t
)))
1310 (defun gnus-or (&rest elems
)
1311 "Return non-nil if any of the elements are non-nil."
1315 (throw 'found t
)))))
1317 (defun gnus-and (&rest elems
)
1318 "Return non-nil if all of the elements are non-nil."
1322 (throw 'found nil
)))
1325 ;; gnus.el requires mm-util.
1326 (declare-function mm-disable-multibyte
"mm-util")
1328 (defun gnus-write-active-file (file hashtb
&optional full-names
)
1329 ;; `coding-system-for-write' should be `raw-text' or equivalent.
1330 (let ((coding-system-for-write nnmail-active-file-coding-system
))
1331 (with-temp-file file
1332 ;; The buffer should be in the unibyte mode because group names
1333 ;; are ASCII text or encoded non-ASCII text (i.e., unibyte).
1334 (mm-disable-multibyte)
1340 (insert (format "%S %d %d y\n"
1343 (intern (gnus-group-real-name (symbol-name sym
))))
1344 (or (cdr (symbol-value sym
))
1345 (car (symbol-value sym
)))
1346 (car (symbol-value sym
))))))
1348 (goto-char (point-max))
1349 (while (search-backward "\\." nil t
)
1352 ;; Fixme: Why not use `with-output-to-temp-buffer'?
1353 (defmacro gnus-with-output-to-file
(file &rest body
)
1354 (let ((buffer (make-symbol "output-buffer"))
1355 (size (make-symbol "output-buffer-size"))
1356 (leng (make-symbol "output-buffer-length"))
1357 (append (make-symbol "output-buffer-append")))
1358 `(let* ((,size
131072)
1359 (,buffer
(make-string ,size
0))
1364 (aset ,buffer
,leng c
)
1366 (if (= ,size
(setq ,leng
(1+ ,leng
)))
1367 (progn (write-region ,buffer nil
,file
,append
'no-msg
)
1372 (let ((coding-system-for-write 'no-conversion
))
1373 (write-region (substring ,buffer
0 ,leng
) nil
,file
1374 ,append
'no-msg
))))))
1376 (put 'gnus-with-output-to-file
'lisp-indent-function
1)
1377 (put 'gnus-with-output-to-file
'edebug-form-spec
'(form body
))
1379 (if (fboundp 'union
)
1380 (defalias 'gnus-union
'union
)
1381 (defun gnus-union (l1 l2
)
1382 "Set union of lists L1 and L2."
1383 (cond ((null l1
) l2
)
1387 (or (>= (length l1
) (length l2
))
1388 (setq l1
(prog1 l2
(setq l2 l1
))))
1390 (or (member (car l2
) l1
)
1395 (declare-function gnus-add-text-properties
"gnus"
1396 (start end properties
&optional object
))
1398 (defun gnus-add-text-properties-when
1399 (property value start end properties
&optional object
)
1400 "Like `gnus-add-text-properties', only applied on where PROPERTY is VALUE."
1403 (< start end
) ;; XEmacs will loop for every when start=end.
1404 (setq point
(text-property-not-all start end property value
)))
1405 (gnus-add-text-properties start point properties object
)
1406 (setq start
(text-property-any point end property value
)))
1408 (gnus-add-text-properties start end properties object
))))
1410 (defun gnus-remove-text-properties-when
1411 (property value start end properties
&optional object
)
1412 "Like `remove-text-properties', only applied on where PROPERTY is VALUE."
1416 (setq point
(text-property-not-all start end property value
)))
1417 (remove-text-properties start point properties object
)
1418 (setq start
(text-property-any point end property value
)))
1420 (remove-text-properties start end properties object
))
1423 (defun gnus-string-remove-all-properties (string)
1426 (set-text-properties 0 (length string
) nil string
)
1430 ;; This might use `compare-strings' to reduce consing in the
1431 ;; case-insensitive case, but it has to cope with null args.
1432 ;; (`string-equal' uses symbol print names.)
1433 (defun gnus-string-equal (x y
)
1434 "Like `string-equal', except it compares case-insensitively."
1435 (and (= (length x
) (length y
))
1436 (or (string-equal x y
)
1437 (string-equal (downcase x
) (downcase y
)))))
1439 (defcustom gnus-use-byte-compile t
1440 "If non-nil, byte-compile crucial run-time code.
1441 Setting it to nil has no effect after the first time `gnus-byte-compile'
1445 :group
'gnus-various
)
1447 (defun gnus-byte-compile (form)
1448 "Byte-compile FORM if `gnus-use-byte-compile' is non-nil."
1449 (if gnus-use-byte-compile
1452 ;; Work around a bug in XEmacs 21.4
1453 (require 'byte-optimize
)
1456 (defalias 'gnus-byte-compile
1458 (let ((byte-compile-warnings '(unresolved callargs redefine
)))
1459 (byte-compile form
))))
1460 (gnus-byte-compile form
))
1463 (defun gnus-remassoc (key alist
)
1464 "Delete by side effect any elements of LIST whose car is `equal' to KEY.
1465 The modified LIST is returned. If the first member
1466 of LIST has a car that is `equal' to KEY, there is no way to remove it
1467 by side effect; therefore, write `(setq foo (gnus-remassoc key foo))' to be
1468 sure of changing the value of `foo'."
1470 (if (equal key
(caar alist
))
1472 (setcdr alist
(gnus-remassoc key
(cdr alist
)))
1475 (defun gnus-update-alist-soft (key value alist
)
1477 (cons (cons key value
) (gnus-remassoc key alist
))
1478 (gnus-remassoc key alist
)))
1480 (defun gnus-create-info-command (node)
1481 "Create a command that will go to info NODE."
1484 ,(concat "Enter the info system at node " node
)
1485 (Info-goto-node ,node
)
1486 (setq gnus-info-buffer
(current-buffer))
1487 (gnus-configure-windows 'info
)))
1489 (defun gnus-not-ignore (&rest args
)
1492 (defvar gnus-directory-sep-char-regexp
"/"
1493 "The regexp of directory separator character.
1494 If you find some problem with the directory separator character, try
1495 \"[/\\\\\]\" for some systems.")
1497 (defun gnus-url-unhex (x)
1504 ;; Fixme: Do it like QP.
1505 (defun gnus-url-unhex-string (str &optional allow-newlines
)
1506 "Remove %XX, embedded spaces, etc in a url.
1507 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
1508 decoding of carriage returns and line feeds in the string, which is normally
1509 forbidden in URL encoding."
1511 (case-fold-search t
))
1512 (while (string-match "%[0-9a-f][0-9a-f]" str
)
1513 (let* ((start (match-beginning 0))
1514 (ch1 (gnus-url-unhex (elt str
(+ start
1))))
1516 (gnus-url-unhex (elt str
(+ start
2))))))
1518 tmp
(substring str
0 start
)
1521 (char-to-string code
))
1522 ((or (= code ?
\n) (= code ?
\r))
1524 (t (char-to-string code
))))
1525 str
(substring str
(match-end 0)))))
1526 (setq tmp
(concat tmp str
))
1529 (defun gnus-make-predicate (spec)
1530 "Transform SPEC into a function that can be called.
1531 SPEC is a predicate specifier that contains stuff like `or', `and',
1532 `not', lists and functions. The functions all take one parameter."
1533 `(lambda (elem) ,(gnus-make-predicate-1 spec
)))
1535 (defun gnus-make-predicate-1 (spec)
1540 (if (memq (car spec
) '(or and not
))
1541 `(,(car spec
) ,@(mapcar 'gnus-make-predicate-1
(cdr spec
)))
1542 (error "Invalid predicate specifier: %s" spec
)))))
1544 (defun gnus-completing-read (prompt collection
&optional require-match
1545 initial-input history def
)
1546 "Call `gnus-completing-read-function'."
1547 (funcall gnus-completing-read-function
1548 (concat prompt
(when def
1549 (concat " (default " def
")"))
1551 collection require-match initial-input history def
))
1553 (defun gnus-emacs-completing-read (prompt collection
&optional require-match
1554 initial-input history def
)
1555 "Call standard `completing-read-function'."
1556 (let ((completion-styles gnus-completion-styles
))
1557 (completing-read prompt
1558 (if (featurep 'xemacs
)
1559 ;; Old XEmacs (at least 21.4) expect an alist,
1560 ;; in which the car of each element is a string,
1564 (list (format "%s" (or (car-safe elem
) elem
))))
1567 nil require-match initial-input history def
)))
1569 (autoload 'ido-completing-read
"ido")
1570 (defun gnus-ido-completing-read (prompt collection
&optional require-match
1571 initial-input history def
)
1572 "Call `ido-completing-read-function'."
1573 (ido-completing-read prompt collection nil require-match
1574 initial-input history def
))
1577 (declare-function iswitchb-read-buffer
"iswitchb"
1578 (prompt &optional default require-match start matches-set
))
1579 (defvar iswitchb-temp-buflist
)
1581 (defun gnus-iswitchb-completing-read (prompt collection
&optional require-match
1582 initial-input history def
)
1583 "`iswitchb' based completing-read function."
1584 ;; Make sure iswitchb is loaded before we let-bind its variables.
1585 ;; If it is loaded inside the let, variables can become unbound afterwards.
1587 (let ((iswitchb-make-buflist-hook
1589 (setq iswitchb-temp-buflist
1590 (let ((choices (append
1591 (when initial-input
(list initial-input
))
1592 (symbol-value history
) collection
))
1595 (setq filtered-choices
(adjoin x filtered-choices
)))
1596 (nreverse filtered-choices
))))))
1600 (add-hook 'minibuffer-setup-hook
'iswitchb-minibuffer-setup
))
1601 (iswitchb-read-buffer prompt def require-match
))
1603 (remove-hook 'minibuffer-setup-hook
'iswitchb-minibuffer-setup
)))))
1605 (defun gnus-graphic-display-p ()
1606 (if (featurep 'xemacs
)
1607 (device-on-window-system-p)
1608 (display-graphic-p)))
1610 (put 'gnus-parse-without-error
'lisp-indent-function
0)
1611 (put 'gnus-parse-without-error
'edebug-form-spec
'(body))
1613 (defmacro gnus-parse-without-error
(&rest body
)
1614 "Allow continuing onto the next line even if an error occurs."
1615 `(while (not (eobp))
1619 (goto-char (point-max)))
1621 (gnus-error 4 "Invalid data on line %d"
1622 (count-lines (point-min) (point)))
1623 (forward-line 1)))))
1625 (defun gnus-cache-file-contents (file variable function
)
1626 "Cache the contents of FILE in VARIABLE. The contents come from FUNCTION."
1627 (let ((time (nth 5 (file-attributes file
)))
1629 (if (or (null (setq value
(symbol-value variable
)))
1630 (not (equal (car value
) file
))
1631 (not (equal (nth 1 value
) time
)))
1633 (setq contents
(funcall function file
))
1634 (set variable
(list file time contents
))
1638 (defun gnus-multiple-choice (prompt choice
&optional idx
)
1639 "Ask user a multiple choice question.
1640 CHOICE is a list of the choice char and help message at IDX."
1642 (save-window-excursion
1645 (message "%s (%s): "
1648 (mapconcat (lambda (s) (char-to-string (car s
)))
1649 choice
", ") ", ?"))
1650 (setq tchar
(read-char))
1651 (when (not (assq tchar choice
))
1653 (setq buf
(get-buffer-create "*Gnus Help*"))
1655 (fundamental-mode) ; for Emacs 20.4+
1656 (buffer-disable-undo)
1658 (insert prompt
":\n\n")
1665 ;; find the longest string to display
1667 (setq n
(length (nth idx
(car list
))))
1670 (setq list
(cdr list
)))
1671 (setq max
(+ max
4)) ; %c, `:', SPACE, a SPACE at end
1672 (setq n
(/ (1- (window-width)) max
)) ; items per line
1673 (setq width
(/ (1- (window-width)) n
)) ; width of each item
1674 ;; insert `n' items, each in a field of width `width'
1679 (delete-char -
1) ; the `\n' takes a char
1681 (setq pad
(- width
3))
1682 (setq format
(concat "%c: %-" (int-to-string pad
) "s"))
1683 (insert (format format
(caar alist
) (nth idx
(car alist
))))
1684 (setq alist
(cdr alist
))
1685 (setq i
(1+ i
))))))))
1686 (if (buffer-live-p buf
)
1690 (if (featurep 'emacs
)
1691 (defalias 'gnus-select-frame-set-input-focus
'select-frame-set-input-focus
)
1692 (if (fboundp 'select-frame-set-input-focus
)
1693 (defalias 'gnus-select-frame-set-input-focus
'select-frame-set-input-focus
)
1694 ;; XEmacs 21.4, SXEmacs
1695 (defun gnus-select-frame-set-input-focus (frame)
1696 "Select FRAME, raise it, and set input focus, if possible."
1698 (select-frame frame
)
1699 (focus-frame frame
))))
1701 (defun gnus-frame-or-window-display-name (object)
1702 "Given a frame or window, return the associated display name.
1703 Return nil otherwise."
1704 (if (featurep 'xemacs
)
1705 (device-connection (dfw-device object
))
1706 (if (or (framep object
)
1707 (and (windowp object
)
1708 (setq object
(window-frame object
))))
1709 (let ((display (frame-parameter object
'display
)))
1710 (if (and (stringp display
)
1711 ;; Exclude invalid display names.
1712 (string-match "\\`[^:]*:[0-9]+\\(\\.[0-9]+\\)?\\'"
1716 (defvar tool-bar-mode
)
1718 (defun gnus-tool-bar-update (&rest ignore
)
1719 "Update the tool bar."
1720 (when (and (boundp 'tool-bar-mode
)
1723 (func (cond ((featurep 'xemacs
)
1725 ((fboundp 'tool-bar-update
)
1727 ((fboundp 'force-window-update
)
1728 'force-window-update
)
1729 ((fboundp 'redraw-frame
)
1730 (setq args
(list (selected-frame)))
1733 (apply func args
))))
1735 ;; Fixme: This has only one use (in gnus-agent), which isn't worthwhile.
1736 (defmacro gnus-mapcar
(function seq1
&rest seqs2_n
)
1737 "Apply FUNCTION to each element of the sequences, and make a list of the results.
1738 If there are several sequences, FUNCTION is called with that many arguments,
1739 and mapping stops as soon as the shortest sequence runs out. With just one
1740 sequence, this is like `mapcar'. With several, it is like the Common Lisp
1741 `mapcar' function extended to arbitrary sequence types."
1744 (let* ((seqs (cons seq1 seqs2_n
))
1746 (heads (mapcar (lambda (seq)
1747 (make-symbol (concat "head"
1749 (setq cnt
(1+ cnt
))))))
1751 (result (make-symbol "result"))
1752 (result-tail (make-symbol "result-tail")))
1753 `(let* ,(let* ((bindings (cons nil nil
))
1755 (nconc bindings
(list (list result
'(cons nil nil
))))
1756 (nconc bindings
(list (list result-tail result
)))
1758 (nconc bindings
(list (list (pop heads
) (pop seqs
)))))
1760 (while (and ,@heads
)
1761 (setcdr ,result-tail
(cons (funcall ,function
1762 ,@(mapcar (lambda (h) (list 'car h
))
1765 (setq ,result-tail
(cdr ,result-tail
)
1766 ,@(apply 'nconc
(mapcar (lambda (h) (list h
(list 'cdr h
))) heads
))))
1768 `(mapcar ,function
,seq1
)))
1770 (if (fboundp 'merge
)
1771 (defalias 'gnus-merge
'merge
)
1772 ;; Adapted from cl-seq.el
1773 (defun gnus-merge (type list1 list2 pred
)
1774 "Destructively merge lists LIST1 and LIST2 to produce a new list.
1775 Argument TYPE is for compatibility and ignored.
1776 Ordering of the elements is preserved according to PRED, a `less-than'
1777 predicate on the elements."
1779 (while (and list1 list2
)
1780 (if (funcall pred
(car list2
) (car list1
))
1781 (push (pop list2
) res
)
1782 (push (pop list1
) res
)))
1783 (nconc (nreverse res
) list1 list2
))))
1785 (defvar xemacs-codename
)
1786 (defvar sxemacs-codename
)
1787 (defvar emacs-program-version
)
1789 (defun gnus-emacs-version ()
1790 "Stringified Emacs version."
1791 (let* ((lst (if (listp gnus-user-agent
)
1793 '(gnus emacs type
)))
1794 (system-v (cond ((memq 'config lst
)
1795 system-configuration
)
1797 (symbol-name system-type
))
1800 (cond ((featurep 'sxemacs
)
1801 (setq emacsname
"SXEmacs"
1802 codename sxemacs-codename
))
1804 (setq emacsname
"XEmacs"
1805 codename xemacs-codename
))
1807 (setq emacsname
"Emacs")))
1809 ((not (memq 'emacs lst
))
1811 ((string-match "^\\(\\([.0-9]+\\)*\\)\\.[0-9]+$" emacs-version
)
1813 (concat "Emacs/" (match-string 1 emacs-version
)
1815 (concat " (" system-v
")")
1817 ((or (featurep 'sxemacs
) (featurep 'xemacs
))
1818 ;; XEmacs or SXEmacs:
1819 (concat emacsname
"/" emacs-program-version
1821 (when (memq 'codename lst
)
1822 (push codename plst
))
1824 (push system-v plst
))
1825 (unless (featurep 'mule
)
1826 (push "no MULE" plst
))
1827 (when (> (length plst
) 0)
1829 " (" (mapconcat 'identity
(reverse plst
) ", ") ")")))))
1830 (t emacs-version
))))
1832 (defun gnus-rename-file (old-path new-path
&optional trim
)
1833 "Rename OLD-PATH as NEW-PATH. If TRIM, recursively delete
1834 empty directories from OLD-PATH."
1835 (when (file-exists-p old-path
)
1836 (let* ((old-dir (file-name-directory old-path
))
1837 (old-name (file-name-nondirectory old-path
))
1838 (new-dir (file-name-directory new-path
))
1839 (new-name (file-name-nondirectory new-path
))
1841 (gnus-make-directory new-dir
)
1842 (rename-file old-path new-path t
)
1844 (while (progn (setq temp
(directory-files old-dir
))
1845 (while (member (car temp
) '("." ".."))
1846 (setq temp
(cdr temp
)))
1847 (= (length temp
) 0))
1848 (delete-directory old-dir
)
1849 (setq old-dir
(file-name-as-directory
1851 (concat old-dir
"..")))))))))
1853 (defun gnus-set-file-modes (filename mode
)
1854 "Wrapper for set-file-modes."
1856 (set-file-modes filename mode
)))
1858 (if (fboundp 'set-process-query-on-exit-flag
)
1859 (defalias 'gnus-set-process-query-on-exit-flag
1860 'set-process-query-on-exit-flag
)
1861 (defalias 'gnus-set-process-query-on-exit-flag
1862 'process-kill-without-query
))
1864 (defalias 'gnus-read-shell-command
1865 (if (fboundp 'read-shell-command
) 'read-shell-command
'read-string
))
1867 (defmacro gnus-put-display-table
(range value display-table
)
1868 "Set the value for char RANGE to VALUE in DISPLAY-TABLE. "
1869 (if (featurep 'xemacs
)
1871 `(if (fboundp 'put-display-table
)
1872 (put-display-table ,range
,value
,display-table
)
1873 (if (sequencep ,display-table
)
1874 (aset ,display-table
,range
,value
)
1875 (put-char-table ,range
,value
,display-table
))))
1876 `(aset ,display-table
,range
,value
)))
1878 (defmacro gnus-get-display-table
(character display-table
)
1879 "Find value for CHARACTER in DISPLAY-TABLE. "
1880 (if (featurep 'xemacs
)
1881 `(if (fboundp 'get-display-table
)
1882 (get-display-table ,character
,display-table
)
1883 (if (sequencep ,display-table
)
1884 (aref ,display-table
,character
)
1885 (get-char-table ,character
,display-table
)))
1886 `(aref ,display-table
,character
)))
1888 (declare-function image-size
"image.c" (spec &optional pixels frame
))
1890 (defun gnus-rescale-image (image size
)
1891 "Rescale IMAGE to SIZE if possible.
1892 SIZE is in format (WIDTH . HEIGHT). Return a new image.
1893 Sizes are in pixels."
1894 (if (or (not (fboundp 'imagemagick-types
))
1895 (not (get-buffer-window (current-buffer))))
1897 (let ((new-width (car size
))
1898 (new-height (cdr size
)))
1899 (when (> (cdr (image-size image t
)) new-height
)
1900 (setq image
(or (create-image (plist-get (cdr image
) :data
) 'imagemagick t
1903 (when (> (car (image-size image t
)) new-width
)
1905 (create-image (plist-get (cdr image
) :data
) 'imagemagick t
1910 (eval-when-compile (require 'gmm-utils
))
1911 (defun gnus-recursive-directory-files (dir)
1912 "Return all regular files below DIR.
1913 The first found will be returned if a file has hard or symbolic links."
1914 (let (files attr attrs
)
1917 (dolist (file (directory-files directory t
))
1918 (setq attr
(file-attributes (file-truename file
)))
1919 (when (and (not (member attr attrs
))
1920 (not (member (file-name-nondirectory file
)
1922 (file-readable-p file
))
1924 (cond ((file-regular-p file
)
1926 ((file-directory-p file
)
1931 (defun gnus-list-memq-of-list (elements list
)
1932 "Return non-nil if any of the members of ELEMENTS are in LIST."
1934 (dolist (elem elements
)
1935 (setq found
(or found
1941 ((fboundp 'match-substitute-replacement
)
1942 (defalias 'gnus-match-substitute-replacement
'match-substitute-replacement
))
1944 (defun gnus-match-substitute-replacement (replacement &optional fixedcase literal string subexp
)
1945 "Return REPLACEMENT as it will be inserted by `replace-match'.
1946 In other words, all back-references in the form `\\&' and `\\N'
1947 are substituted with actual strings matched by the last search.
1948 Optional FIXEDCASE, LITERAL, STRING and SUBEXP have the same
1949 meaning as for `replace-match'.
1951 This is the definition of match-substitute-replacement in subr.el from GNU Emacs."
1952 (let ((match (match-string 0 string
)))
1954 (set-match-data (mapcar (lambda (x)
1956 (- x
(match-beginning 0))
1959 (replace-match replacement fixedcase literal match subexp
)))))))
1961 (if (fboundp 'string-match-p
)
1962 (defalias 'gnus-string-match-p
'string-match-p
)
1963 (defsubst gnus-string-match-p
(regexp string
&optional start
)
1965 Same as `string-match' except this function does not change the match data."
1967 (string-match regexp string start
))))
1969 (if (fboundp 'string-prefix-p
)
1970 (defalias 'gnus-string-prefix-p
'string-prefix-p
)
1971 (defun gnus-string-prefix-p (str1 str2
&optional ignore-case
)
1972 "Return non-nil if STR1 is a prefix of STR2.
1973 If IGNORE-CASE is non-nil, the comparison is done without paying attention
1974 to case differences."
1975 (and (<= (length str1
) (length str2
))
1976 (let ((prefix (substring str2
0 (length str1
))))
1978 (string-equal (downcase str1
) (downcase prefix
))
1979 (string-equal str1 prefix
))))))
1981 ;; Simple check: can be a macro but this way, although slow, it's really clear.
1982 ;; We don't use `bound-and-true-p' because it's not in XEmacs.
1983 (defun gnus-bound-and-true-p (sym)
1984 (and (boundp sym
) (symbol-value sym
)))
1986 (if (fboundp 'timer--function
)
1987 (defalias 'gnus-timer--function
'timer--function
)
1988 (defun gnus-timer--function (timer)
1991 (provide 'gnus-util
)
1993 ;;; gnus-util.el ends here