1 ;;; gnus-util.el --- utility functions for Gnus
3 ;; Copyright (C) 1996-2012 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...]
35 ;; For Emacs <22.2 and XEmacs.
37 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
43 (defcustom gnus-completing-read-function
'gnus-emacs-completing-read
44 "Function use to do completing read."
47 :type
`(radio (function-item
48 :doc
"Use Emacs standard `completing-read' function."
49 gnus-emacs-completing-read
)
50 ;; iswitchb.el is very old and ido.el is unavailable
51 ;; in XEmacs, so we exclude those function items.
52 ,@(unless (featurep 'xemacs
)
54 :doc
"Use `ido-completing-read' function."
55 gnus-ido-completing-read
)
57 :doc
"Use iswitchb based completing-read function."
58 gnus-iswitchb-completing-read
)))))
60 (defcustom gnus-completion-styles
61 (if (and (boundp 'completion-styles-alist
)
62 (boundp 'completion-styles
))
63 (append (when (and (assq 'substring completion-styles-alist
)
64 (not (memq 'substring completion-styles
)))
68 "Value of `completion-styles' to use when completing."
73 ;; Fixme: this should be a gnus variable, not nnmail-.
74 (defvar nnmail-pathname-coding-system
)
75 (defvar nnmail-active-file-coding-system
)
77 ;; Inappropriate references to other parts of Gnus.
78 (defvar gnus-emphasize-whitespace-regexp
)
79 (defvar gnus-original-article-buffer
)
80 (defvar gnus-user-agent
)
82 (autoload 'gnus-get-buffer-window
"gnus-win")
83 (autoload 'nnheader-narrow-to-headers
"nnheader")
84 (autoload 'nnheader-replace-chars-in-string
"nnheader")
85 (autoload 'mail-header-remove-comments
"mail-parse")
89 ;; Prefer `replace-regexp-in-string' (present in Emacs, XEmacs 21.5,
90 ;; SXEmacs 22.1.4) over `replace-in-string'. The latter leads to inf-loops
92 ;; (replace-in-string "foo" "/*$" "/")
93 ;; (replace-in-string "xe" "\\(x\\)?" "")
94 ((fboundp 'replace-regexp-in-string
)
95 (defun gnus-replace-in-string (string regexp newtext
&optional literal
)
96 "Replace all matches for REGEXP with NEWTEXT in STRING.
97 If LITERAL is non-nil, insert NEWTEXT literally. Return a new
98 string containing the replacements.
100 This is a compatibility function for different Emacsen."
101 (replace-regexp-in-string regexp newtext string nil literal
)))
102 ((fboundp 'replace-in-string
)
103 (defalias 'gnus-replace-in-string
'replace-in-string
))))
105 (defun gnus-boundp (variable)
106 "Return non-nil if VARIABLE is bound and non-nil."
107 (and (boundp variable
)
108 (symbol-value variable
)))
110 (defmacro gnus-eval-in-buffer-window
(buffer &rest forms
)
111 "Pop to BUFFER, evaluate FORMS, and then return to the original window."
112 (let ((tempvar (make-symbol "GnusStartBufferWindow"))
113 (w (make-symbol "w"))
114 (buf (make-symbol "buf")))
115 `(let* ((,tempvar
(selected-window))
117 (,w
(gnus-get-buffer-window ,buf
'visible
)))
123 (set-buffer (window-buffer ,w
)))
124 (pop-to-buffer ,buf
))
126 (select-window ,tempvar
)))))
128 (put 'gnus-eval-in-buffer-window
'lisp-indent-function
1)
129 (put 'gnus-eval-in-buffer-window
'edebug-form-spec
'(form body
))
131 (defmacro gnus-intern-safe
(string hashtable
)
132 "Get hash value. Arguments are STRING and HASHTABLE."
133 `(let ((symbol (intern ,string
,hashtable
)))
138 (defsubst gnus-goto-char
(point)
139 (and point
(goto-char point
)))
141 (defmacro gnus-buffer-exists-p
(buffer)
142 `(let ((buffer ,buffer
))
144 (funcall (if (stringp buffer
) 'get-buffer
'buffer-name
)
147 ;; The LOCAL arg to `add-hook' is interpreted differently in Emacs and
148 ;; XEmacs. In Emacs we don't need to call `make-local-hook' first.
149 ;; It's harmless, though, so the main purpose of this alias is to shut
150 ;; up the byte compiler.
151 (defalias 'gnus-make-local-hook
(if (featurep 'xemacs
)
155 (defun gnus-delete-first (elt list
)
156 "Delete by side effect the first occurrence of ELT as a member of LIST."
157 (if (equal (car list
) elt
)
160 (while (and (cdr list
)
161 (not (equal (cadr list
) elt
)))
162 (setq list
(cdr list
)))
164 (setcdr list
(cddr list
)))
167 ;; Delete the current line (and the next N lines).
168 (defmacro gnus-delete-line
(&optional n
)
169 `(delete-region (point-at-bol)
170 (progn (forward-line ,(or n
1)) (point))))
172 (defun gnus-extract-address-components (from)
173 "Extract address components from a From header.
174 Given an RFC-822 address FROM, extract full name and canonical address.
175 Returns a list of the form (FULL-NAME CANONICAL-ADDRESS). Much more simple
176 solution than `mail-extract-address-components', which works much better, but
179 ;; First find the address - the thing with the @ in it. This may
180 ;; not be accurate in mail addresses, but does the trick most of
181 ;; the time in news messages.
182 (cond (;; Check ``<foo@bar>'' first in order to handle the quite common
183 ;; form ``"abc@xyz" <foo@bar>'' (i.e. ``@'' as part of a comment)
185 (string-match "<\\([^@ \t<>]+[!@][^@ \t<>]+\\)>" from
)
186 (setq address
(substring from
(match-beginning 1) (match-end 1))))
187 ((string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from
)
188 (setq address
(substring from
(match-beginning 0) (match-end 0)))))
189 ;; Then we check whether the "name <address>" format is used.
191 ;; Linear white space is not required.
192 (string-match (concat "[ \t]*<" (regexp-quote address
) ">") from
)
193 (and (setq name
(substring from
0 (match-beginning 0)))
194 ;; Strip any quotes from the name.
195 (string-match "^\".*\"$" name
)
196 (setq name
(substring name
1 (1- (match-end 0))))))
197 ;; If not, then "address (name)" is used.
199 (and (string-match "(.+)" from
)
200 (setq name
(substring from
(1+ (match-beginning 0))
201 (1- (match-end 0)))))
202 (and (string-match "()" from
)
204 ;; XOVER might not support folded From headers.
205 (and (string-match "(.*" from
)
206 (setq name
(substring from
(1+ (match-beginning 0))
208 (list (if (string= name
"") nil name
) (or address from
))))
210 (declare-function message-fetch-field
"message" (header &optional not-all
))
212 (defun gnus-fetch-field (field)
213 "Return the value of the header FIELD of current article."
217 (let ((inhibit-point-motion-hooks t
))
218 (nnheader-narrow-to-headers)
219 (message-fetch-field field
)))))
221 (defun gnus-fetch-original-field (field)
222 "Fetch FIELD from the original version of the current article."
223 (with-current-buffer gnus-original-article-buffer
224 (gnus-fetch-field field
)))
227 (defun gnus-goto-colon ()
229 (let ((eol (point-at-eol)))
230 (goto-char (or (text-property-any (point) eol
'gnus-position t
)
231 (search-forward ":" eol t
)
234 (declare-function gnus-find-method-for-group
"gnus" (group &optional info
))
235 (declare-function gnus-group-name-decode
"gnus-group" (string charset
))
236 (declare-function gnus-group-name-charset
"gnus-group" (method group
))
237 ;; gnus-group requires gnus-int which requires message.
238 (declare-function message-tokenize-header
"message"
239 (header &optional separator
))
241 (defun gnus-decode-newsgroups (newsgroups group
&optional method
)
242 (require 'gnus-group
)
243 (let ((method (or method
(gnus-find-method-for-group group
))))
244 (mapconcat (lambda (group)
245 (gnus-group-name-decode group
(gnus-group-name-charset
247 (message-tokenize-header newsgroups
)
250 (defun gnus-remove-text-with-property (prop)
251 "Delete all text in the current buffer with text property PROP."
252 (let ((start (point-min))
254 (unless (get-text-property start prop
)
255 (setq start
(next-single-property-change start prop
)))
257 (setq end
(text-property-any start
(point-max) prop nil
))
258 (delete-region start
(or end
(point-max)))
259 (setq start
(when end
260 (next-single-property-change start prop
))))))
262 (defun gnus-find-text-property-region (start end prop
)
263 "Return a list of text property regions that has property PROP."
265 (unless (get-text-property start prop
)
266 (setq start
(next-single-property-change start prop
)))
268 (setq value
(get-text-property start prop
)
269 end
(text-property-not-all start
(point-max) prop value
))
273 (push (list (set-marker (make-marker) start
)
274 (set-marker (make-marker) end
)
277 (setq start
(next-single-property-change start prop
))))
280 (defun gnus-newsgroup-directory-form (newsgroup)
281 "Make hierarchical directory name from NEWSGROUP name."
282 (let* ((newsgroup (gnus-newsgroup-savable-name newsgroup
))
283 (idx (string-match ":" newsgroup
)))
285 (if idx
(substring newsgroup
0 idx
))
287 (nnheader-replace-chars-in-string
288 (if idx
(substring newsgroup
(1+ idx
)) newsgroup
)
291 (defun gnus-newsgroup-savable-name (group)
292 ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
294 (nnheader-replace-chars-in-string group ?
/ ?.
))
296 (defun gnus-string> (s1 s2
)
297 (not (or (string< s1 s2
)
300 (defun gnus-string< (s1 s2
)
301 "Return t if first arg string is less than second in lexicographic order.
302 Case is significant if and only if `case-fold-search' is nil.
303 Symbols are also allowed; their print names are used instead."
305 (string-lessp (downcase (if (symbolp s1
) (symbol-name s1
) s1
))
306 (downcase (if (symbolp s2
) (symbol-name s2
) s2
)))
307 (string-lessp s1 s2
)))
311 (defun gnus-file-newer-than (file date
)
312 (let ((fdate (nth 5 (file-attributes file
))))
313 (or (> (car fdate
) (car date
))
314 (and (= (car fdate
) (car date
))
315 (> (nth 1 fdate
) (nth 1 date
))))))
317 ;; Every version of Emacs Gnus supports has built-in float-time.
318 ;; The featurep test silences an irritating compiler warning.
320 (if (or (featurep 'emacs
)
321 (fboundp 'float-time
))
322 (defalias 'gnus-float-time
'float-time
)
323 (defun gnus-float-time (&optional time
)
324 "Convert time value TIME to a floating point number.
325 TIME defaults to the current time."
326 (time-to-seconds (or time
(current-time))))))
330 (defmacro gnus-local-set-keys
(&rest plist
)
331 "Set the keys in PLIST in the current keymap."
332 `(gnus-define-keys-1 (current-local-map) ',plist
))
334 (defmacro gnus-define-keys
(keymap &rest plist
)
335 "Define all keys in PLIST in KEYMAP."
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 (current-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 (current-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 ((now (decode-time (current-time)))
400 (days (format-time-string "%j" (current-time))))
401 (+ (car now
) (* (car (cdr now
)) 60) (* (car (nthcdr 2 now
)) 3600)
402 (* (- (string-to-number days
) 1) 3600 24))))
404 (defmacro gnus-date-get-time
(date)
405 "Convert DATE string to Emacs time.
406 Cache the result as a text property stored in DATE."
407 ;; Either return the cached value...
411 (or (get-text-property 0 'gnus-time d
)
412 ;; or compute the value...
413 (let ((time (safe-date-to-time d
)))
414 ;; and store it back in the string.
415 (put-text-property 0 1 'gnus-time time d
)
418 (defun gnus-dd-mmm (messy-date)
419 "Return a string like DD-MMM from a big messy string."
421 (format-time-string "%d-%b" (gnus-date-get-time messy-date
))
424 (defsubst gnus-time-iso8601
(time)
425 "Return a string of TIME in YYYYMMDDTHHMMSS format."
426 (format-time-string "%Y%m%dT%H%M%S" time
))
428 (defun gnus-date-iso8601 (date)
429 "Convert the DATE to YYYYMMDDTHHMMSS."
431 (gnus-time-iso8601 (gnus-date-get-time date
))
434 (defun gnus-mode-string-quote (string)
435 "Quote all \"%\"'s in STRING."
436 (gnus-replace-in-string string
"%" "%%"))
438 ;; Make a hash table (default and minimum size is 256).
439 ;; Optional argument HASHSIZE specifies the table size.
440 (defun gnus-make-hashtable (&optional hashsize
)
441 (make-vector (if hashsize
(max (gnus-create-hash-size hashsize
) 256) 256) 0))
443 ;; Make a number that is suitable for hashing; bigger than MIN and
444 ;; equal to some 2^x. Many machines (such as sparcs) do not have a
445 ;; hardware modulo operation, so they implement it in software. On
446 ;; many sparcs over 50% of the time to intern is spent in the modulo.
447 ;; Yes, it's slower than actually computing the hash from the string!
448 ;; So we use powers of 2 so people can optimize the modulo to a mask.
449 (defun gnus-create-hash-size (min)
455 (defcustom gnus-verbose
6
456 "*Integer that says how verbose Gnus should be.
457 The higher the number, the more messages Gnus will flash to say what
458 it's doing. At zero, Gnus will be totally mute; at five, Gnus will
459 display most important messages; and at ten, Gnus will keep on
460 jabbering all the time."
465 (defcustom gnus-add-timestamp-to-message nil
466 "Non-nil means add timestamps to messages that Gnus issues.
467 If it is `log', add timestamps to only the messages that go into the
468 \"*Messages*\" buffer (in XEmacs, it is the \" *Message-Log*\" buffer).
469 If it is neither nil nor `log', add timestamps not only to log messages
470 but also to the ones displayed in the echo area."
471 :version
"23.1" ;; No Gnus
473 :type
'(choice :format
"%{%t%}:\n %[Value Menu%] %v"
474 (const :tag
"Logged messages only" log
)
475 (sexp :tag
"All messages"
476 :match
(lambda (widget value
) value
)
478 (const :tag
"No timestamp" nil
)))
481 (defmacro gnus-message-with-timestamp-1
(format-string args
)
482 (let ((timestamp '(format-time-string "%Y%m%dT%H%M%S.%3N> " time
)))
483 (if (featurep 'xemacs
)
485 (if (or (and (null ,format-string
) (null ,args
))
487 (setq str
(apply 'format
,format-string
,args
))
488 (zerop (length str
))))
490 (and ,format-string str
)
492 (cond ((eq gnus-add-timestamp-to-message
'log
)
493 (setq time
(current-time))
494 (display-message 'no-log str
)
495 (log-message 'message
(concat ,timestamp str
)))
496 (gnus-add-timestamp-to-message
497 (setq time
(current-time))
498 (display-message 'message
(concat ,timestamp str
)))
500 (display-message 'message str
))))
503 (cond ((eq gnus-add-timestamp-to-message
'log
)
504 (setq str
(let (message-log-max)
505 (apply 'message
,format-string
,args
)))
506 (when (and message-log-max
507 (> message-log-max
0)
509 (setq time
(current-time))
510 (with-current-buffer (get-buffer-create "*Messages*")
511 (goto-char (point-max))
512 (insert ,timestamp str
"\n")
513 (forward-line (- message-log-max
))
514 (delete-region (point-min) (point))
515 (goto-char (point-max))))
517 (gnus-add-timestamp-to-message
518 (if (or (and (null ,format-string
) (null ,args
))
520 (setq str
(apply 'format
,format-string
,args
))
521 (zerop (length str
))))
523 (and ,format-string str
)
525 (setq time
(current-time))
526 (message "%s" (concat ,timestamp str
))
529 (apply 'message
,format-string
,args
))))))))
531 (defvar gnus-action-message-log nil
)
533 (defun gnus-message-with-timestamp (format-string &rest args
)
534 "Display message with timestamp. Arguments are the same as `message'.
535 The `gnus-add-timestamp-to-message' variable controls how to add
536 timestamp to message."
537 (gnus-message-with-timestamp-1 format-string args
))
539 (defun gnus-message (level &rest args
)
540 "If LEVEL is lower than `gnus-verbose' print ARGS using `message'.
542 Guideline for numbers:
543 1 - error messages, 3 - non-serious error messages, 5 - messages for things
544 that take a long time, 7 - not very important messages on stuff, 9 - messages
546 (if (<= level gnus-verbose
)
548 (if gnus-add-timestamp-to-message
549 (apply 'gnus-message-with-timestamp args
)
550 (apply 'message args
))))
551 (when (and (consp gnus-action-message-log
)
553 (push message gnus-action-message-log
))
555 ;; We have to do this format thingy here even if the result isn't
556 ;; shown - the return value has to be the same as the return value
558 (apply 'format args
)))
560 (defun gnus-final-warning ()
561 (when (and (consp gnus-action-message-log
)
562 (setq gnus-action-message-log
563 (delete nil gnus-action-message-log
)))
564 (message "Warning: %s"
565 (mapconcat #'identity gnus-action-message-log
"; "))))
567 (defun gnus-error (level &rest args
)
568 "Beep an error if LEVEL is equal to or less than `gnus-verbose'.
569 ARGS are passed to `message'."
570 (when (<= (floor level
) gnus-verbose
)
571 (apply 'message args
)
574 (when (and (floatp level
)
575 (not (zerop (setq duration
(* 10 (- level
(floor level
)))))))
576 (sit-for duration
))))
579 (defun gnus-split-references (references)
580 "Return a list of Message-IDs in REFERENCES."
582 (references (mail-header-remove-comments (or references
"")))
584 (while (string-match "<[^<]+[^< \t]" references beg
)
585 (push (substring references
(match-beginning 0) (setq beg
(match-end 0)))
589 (defun gnus-extract-references (references)
590 "Return a list of Message-IDs in REFERENCES (in In-Reply-To
591 format), trimmed to only contain the Message-IDs."
592 (let ((ids (gnus-split-references references
))
595 (when (string-match "<[^<>]+>" id
)
596 (push (match-string 0 id
) refs
)))
599 (defsubst gnus-parent-id
(references &optional n
)
600 "Return the last Message-ID in REFERENCES.
601 If N, return the Nth ancestor instead."
602 (when (and references
603 (not (zerop (length references
))))
605 (let ((ids (inline (gnus-split-references references
))))
606 (while (nthcdr n ids
)
607 (setq ids
(cdr ids
)))
609 (let ((references (mail-header-remove-comments references
)))
610 (when (string-match "\\(<[^<]+>\\)[ \t]*\\'" references
)
611 (match-string 1 references
))))))
613 (defsubst gnus-buffer-live-p
(buffer)
614 "Say whether BUFFER is alive or not."
615 (and buffer
(buffer-live-p (get-buffer buffer
))))
617 (defun gnus-horizontal-recenter ()
618 "Recenter the current buffer horizontally."
619 (if (< (current-column) (/ (window-width) 2))
620 (set-window-hscroll (gnus-get-buffer-window (current-buffer) t
) 0)
621 (let* ((orig (point))
622 (end (window-end (gnus-get-buffer-window (current-buffer) t
)))
625 ;; Find the longest line currently displayed in the window.
626 (goto-char (window-start))
627 (while (and (not (eobp))
630 (setq max
(max max
(current-column)))
633 ;; Scroll horizontally to center (sort of) the point.
634 (if (> max
(window-width))
636 (gnus-get-buffer-window (current-buffer) t
)
637 (min (- (current-column) (/ (window-width) 3))
638 (+ 2 (- max
(window-width)))))
639 (set-window-hscroll (gnus-get-buffer-window (current-buffer) t
) 0))
642 (defun gnus-read-event-char (&optional prompt
)
643 "Get the next event."
644 (let ((event (read-event prompt
)))
645 ;; should be gnus-characterp, but this can't be called in XEmacs anyway
646 (cons (and (numberp event
) event
) event
)))
648 (defun gnus-copy-file (file &optional to
)
651 (list (read-file-name "Copy file: " default-directory
)
652 (read-file-name "Copy file to: " default-directory
)))
654 (setq to
(read-file-name "Copy file to: " default-directory
)))
655 (when (file-directory-p to
)
656 (setq to
(concat (file-name-as-directory to
)
657 (file-name-nondirectory file
))))
660 (defvar gnus-work-buffer
" *gnus work*")
662 (declare-function gnus-get-buffer-create
"gnus" (name))
663 ;; gnus.el requires mm-util.
664 (declare-function mm-enable-multibyte
"mm-util")
666 (defun gnus-set-work-buffer ()
667 "Put point in the empty Gnus work buffer."
668 (if (get-buffer gnus-work-buffer
)
670 (set-buffer gnus-work-buffer
)
672 (set-buffer (gnus-get-buffer-create gnus-work-buffer
))
673 (kill-all-local-variables)
674 (mm-enable-multibyte)))
676 (defmacro gnus-group-real-name
(group)
677 "Find the real name of a foreign newsgroup."
678 `(let ((gname ,group
))
679 (if (string-match "^[^:]+:" gname
)
680 (substring gname
(match-end 0))
683 (defmacro gnus-group-server
(group)
684 "Find the server name of a foreign newsgroup.
685 For example, (gnus-group-server \"nnimap+yxa:INBOX.foo\") would
686 yield \"nnimap:yxa\"."
687 `(let ((gname ,group
))
688 (if (string-match "^\\([^:+]+\\)\\(?:\\+\\([^:]*\\)\\)?:" gname
)
689 (format "%s:%s" (match-string 1 gname
) (or
690 (match-string 2 gname
)
692 (format "%s:%s" (car gnus-select-method
) (cadr gnus-select-method
)))))
694 (defun gnus-make-sort-function (funs)
695 "Return a composite sort condition based on the functions in FUNS."
697 ;; Just a simple function.
698 ((functionp funs
) funs
)
699 ;; No functions at all.
701 ;; A list of functions.
706 ,(gnus-make-sort-function-1 (reverse funs
)))))
707 ;; A list containing just one function.
711 (defun gnus-make-sort-function-1 (funs)
712 "Return a composite sort condition based on the functions in FUNS."
713 (let ((function (car funs
))
716 (when (consp function
)
719 ((eq (car function
) 'not
)
720 (setq function
(cadr function
)
723 ((functionp function
)
727 (error "Invalid sort spec: %s" function
))))
729 `(or (,function
,first
,last
)
730 (and (not (,function
,last
,first
))
731 ,(gnus-make-sort-function-1 (cdr funs
))))
732 `(,function
,first
,last
))))
734 (defun gnus-turn-off-edit-menu (type)
735 "Turn off edit menu in `gnus-TYPE-mode-map'."
736 (define-key (symbol-value (intern (format "gnus-%s-mode-map" type
)))
737 [menu-bar edit
] 'undefined
))
739 (defmacro gnus-bind-print-variables
(&rest forms
)
740 "Bind print-* variables and evaluate FORMS.
741 This macro is used with `prin1', `pp', etc. in order to ensure printed
742 Lisp objects are loadable. Bind `print-quoted' and `print-readably'
743 to t, and `print-escape-multibyte', `print-escape-newlines',
744 `print-escape-nonascii', `print-length', `print-level' and
745 `print-string-length' to nil."
746 `(let ((print-quoted t
)
749 ;;print-continuous-numbering
750 print-escape-multibyte
751 print-escape-newlines
752 print-escape-nonascii
759 (defun gnus-prin1 (form)
760 "Use `prin1' on FORM in the current buffer.
761 Bind `print-quoted' and `print-readably' to t, and `print-length' and
762 `print-level' to nil. See also `gnus-bind-print-variables'."
763 (gnus-bind-print-variables (prin1 form
(current-buffer))))
765 (defun gnus-prin1-to-string (form)
766 "The same as `prin1'.
767 Bind `print-quoted' and `print-readably' to t, and `print-length' and
768 `print-level' to nil. See also `gnus-bind-print-variables'."
769 (gnus-bind-print-variables (prin1-to-string form
)))
771 (defun gnus-pp (form &optional stream
)
772 "Use `pp' on FORM in the current buffer.
773 Bind `print-quoted' and `print-readably' to t, and `print-length' and
774 `print-level' to nil. See also `gnus-bind-print-variables'."
775 (gnus-bind-print-variables (pp form
(or stream
(current-buffer)))))
777 (defun gnus-pp-to-string (form)
778 "The same as `pp-to-string'.
779 Bind `print-quoted' and `print-readably' to t, and `print-length' and
780 `print-level' to nil. See also `gnus-bind-print-variables'."
781 (gnus-bind-print-variables (pp-to-string form
)))
783 (defun gnus-make-directory (directory)
784 "Make DIRECTORY (and all its parents) if it doesn't exist."
786 (let ((file-name-coding-system nnmail-pathname-coding-system
))
788 (not (file-exists-p directory
)))
789 (make-directory directory t
)))
792 (defun gnus-write-buffer (file)
793 "Write the current buffer's contents to FILE."
795 (let ((file-name-coding-system nnmail-pathname-coding-system
))
796 ;; Make sure the directory exists.
797 (gnus-make-directory (file-name-directory file
))
799 (write-region (point-min) (point-max) file nil
'quietly
)))
801 (defun gnus-delete-file (file)
802 "Delete FILE if it exists."
803 (when (file-exists-p file
)
806 (defun gnus-delete-duplicates (list)
807 "Remove duplicate entries from LIST."
810 (unless (member (car list
) result
)
811 (push (car list
) result
))
815 (defun gnus-delete-directory (directory)
816 "Delete files in DIRECTORY. Subdirectories remain.
817 If there's no subdirectory, delete DIRECTORY as well."
818 (when (file-directory-p directory
)
819 (let ((files (directory-files
820 directory t
"^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
823 (setq file
(pop files
))
824 (if (eq t
(car (file-attributes file
)))
825 ;; `file' is a subdirectory.
827 ;; `file' is a file or a symlink.
830 (delete-directory directory
)))))
832 (defun gnus-strip-whitespace (string)
833 "Return STRING stripped of all whitespace."
834 (while (string-match "[\r\n\t ]+" string
)
835 (setq string
(replace-match "" t t string
)))
838 (declare-function gnus-put-text-property
"gnus"
839 (start end property value
&optional object
))
841 (defsubst gnus-put-text-property-excluding-newlines
(beg end prop val
)
842 "The same as `put-text-property', but don't put this prop on any newlines in the region."
847 (while (re-search-forward gnus-emphasize-whitespace-regexp end
'move
)
848 (gnus-put-text-property beg
(match-beginning 0) prop val
)
850 (gnus-put-text-property beg
(point) prop val
)))))
852 (declare-function gnus-overlay-put
"gnus" (overlay prop value
))
853 (declare-function gnus-make-overlay
"gnus"
854 (beg end
&optional buffer front-advance rear-advance
))
856 (defsubst gnus-put-overlay-excluding-newlines
(beg end prop val
)
857 "The same as `put-text-property', but don't put this prop on any newlines in the region."
862 (while (re-search-forward gnus-emphasize-whitespace-regexp end
'move
)
864 (gnus-make-overlay beg
(match-beginning 0))
867 (gnus-overlay-put (gnus-make-overlay beg
(point)) prop val
)))))
869 (defun gnus-put-text-property-excluding-characters-with-faces (beg end
871 "The same as `put-text-property', but don't put props on characters with the `gnus-face' property."
874 (when (get-text-property b
'gnus-face
)
875 (setq b
(next-single-property-change b
'gnus-face nil end
)))
878 (gnus-put-text-property
879 b
(setq b
(next-single-property-change b
'gnus-face nil end
))
882 (defmacro gnus-faces-at
(position)
883 "Return a list of faces at POSITION."
884 (if (featurep 'xemacs
)
885 `(let ((pos ,position
))
886 (mapcar-extents 'extent-face
887 nil
(current-buffer) pos pos nil
'face
))
888 `(let ((pos ,position
))
889 (delq nil
(cons (get-text-property pos
'face
)
892 (overlay-get overlay
'face
))
893 (overlays-at pos
)))))))
895 (if (fboundp 'invisible-p
)
896 (defalias 'gnus-invisible-p
'invisible-p
)
897 ;; for Emacs < 22.2, and XEmacs.
898 (defun gnus-invisible-p (pos)
899 "Return non-nil if the character after POS is currently invisible."
900 (let ((prop (get-char-property pos
'invisible
)))
901 (if (eq buffer-invisibility-spec t
)
903 (or (memq prop buffer-invisibility-spec
)
904 (assq prop buffer-invisibility-spec
))))))
906 ;; Note: the optional 2nd argument has a different meaning between
908 ;; (next-char-property-change POSITION &optional LIMIT)
909 ;; (next-extent-change POS &optional OBJECT)
910 (defalias 'gnus-next-char-property-change
911 (if (fboundp 'next-extent-change
)
912 'next-extent-change
'next-char-property-change
))
914 (defalias 'gnus-previous-char-property-change
915 (if (fboundp 'previous-extent-change
)
916 'previous-extent-change
'previous-char-property-change
))
918 ;;; Protected and atomic operations. dmoore@ucsd.edu 21.11.1996
919 ;; The primary idea here is to try to protect internal datastructures
920 ;; from becoming corrupted when the user hits C-g, or if a hook or
921 ;; similar blows up. Often in Gnus multiple tables/lists need to be
922 ;; updated at the same time, or information can be lost.
924 (defvar gnus-atomic-be-safe t
925 "If t, certain operations will be protected from interruption by C-g.")
927 (defmacro gnus-atomic-progn
(&rest forms
)
928 "Evaluate FORMS atomically, which means to protect the evaluation
929 from being interrupted by the user. An error from the forms themselves
930 will return without finishing the operation. Since interrupts from
931 the user are disabled, it is recommended that only the most minimal
932 operations are performed by FORMS. If you wish to assign many
933 complicated values atomically, compute the results into temporary
934 variables and then do only the assignment atomically."
935 `(let ((inhibit-quit gnus-atomic-be-safe
))
938 (put 'gnus-atomic-progn
'lisp-indent-function
0)
940 (defmacro gnus-atomic-progn-assign
(protect &rest forms
)
941 "Evaluate FORMS, but ensure that the variables listed in PROTECT
942 are not changed if anything in FORMS signals an error or otherwise
943 non-locally exits. The variables listed in PROTECT are updated atomically.
944 It is safe to use gnus-atomic-progn-assign with long computations.
946 Note that if any of the symbols in PROTECT were unbound, they will be
947 set to nil on a successful assignment. In case of an error or other
948 non-local exit, it will still be unbound."
949 (let* ((temp-sym-map (mapcar (lambda (x) (list (make-symbol
950 (concat (symbol-name x
)
954 (sym-temp-map (mapcar (lambda (x) (list (cadr x
) (car x
)))
956 (temp-sym-let (mapcar (lambda (x) (list (car x
)
957 `(and (boundp ',(cadr x
))
960 (sym-temp-let sym-temp-map
)
961 (temp-sym-assign (apply 'append temp-sym-map
))
962 (sym-temp-assign (apply 'append sym-temp-map
))
963 (result (make-symbol "result-tmp")))
964 `(let (,@temp-sym-let
967 (setq ,result
(progn ,@forms
))
968 (setq ,@temp-sym-assign
))
969 (let ((inhibit-quit gnus-atomic-be-safe
))
970 (setq ,@sym-temp-assign
))
973 (put 'gnus-atomic-progn-assign
'lisp-indent-function
1)
974 ;(put 'gnus-atomic-progn-assign 'edebug-form-spec '(sexp body))
976 (defmacro gnus-atomic-setq
(&rest pairs
)
977 "Similar to setq, except that the real symbols are only assigned when
978 there are no errors. And when the real symbols are assigned, they are
979 done so atomically. If other variables might be changed via side-effect,
980 see gnus-atomic-progn-assign. It is safe to use gnus-atomic-setq
981 with potentially long computations."
985 (push (car tpairs
) syms
)
986 (setq tpairs
(cddr tpairs
)))
987 `(gnus-atomic-progn-assign ,syms
990 ;(put 'gnus-atomic-setq 'edebug-form-spec '(body))
993 ;;; Functions for saving to babyl/mail files.
996 (if (featurep 'xemacs
)
997 ;; Don't load tm and apel XEmacs packages that provide some
998 ;; Emacs emulating functions and variables.
999 (let ((features features
))
1001 (unless (fboundp 'set-alist
) (defalias 'set-alist
'ignore
))
1002 (require 'rmail
)) ;; It requires tm-view that loads apel.
1004 (autoload 'rmail-update-summary
"rmailsum"))
1006 (defvar mm-text-coding-system
)
1008 (declare-function mm-append-to-file
"mm-util"
1009 (start end filename
&optional codesys inhibit
))
1011 (defun gnus-output-to-rmail (filename &optional ask
)
1012 "Append the current article to an Rmail file named FILENAME.
1013 In Emacs 22 this writes Babyl format; in Emacs 23 it writes mbox unless
1014 FILENAME exists and is Babyl format."
1018 ;; Some of this codes is borrowed from rmailout.el.
1019 (setq filename
(expand-file-name filename
))
1020 ;; FIXME should we really be messing with this defcustom?
1021 ;; It is not needed for the operation of this function.
1022 (if (boundp 'rmail-default-rmail-file
)
1023 (setq rmail-default-rmail-file filename
) ; 22
1024 (setq rmail-default-file filename
)) ; 23
1025 (let ((artbuf (current-buffer))
1026 (tmpbuf (get-buffer-create " *Gnus-output*"))
1027 ;; Babyl rmail.el defines this, mbox does not.
1028 (babyl (fboundp 'rmail-insert-rmail-file-header
)))
1030 ;; Note that we ignore the possibility of visiting a Babyl
1031 ;; format buffer in Emacs 23, since Rmail no longer supports that.
1032 (or (get-file-buffer filename
)
1034 ;; In case someone wants to write to a Babyl file from Emacs 23.
1035 (when (file-exists-p filename
)
1036 (setq babyl
(mail-file-babyl-p filename
))
1040 (concat "\"" filename
"\" does not exist, create it? ")))
1041 (let ((file-buffer (create-file-buffer filename
)))
1042 (with-current-buffer file-buffer
1043 (if (fboundp 'rmail-insert-rmail-file-header
)
1044 (rmail-insert-rmail-file-header))
1045 (let ((require-final-newline nil
)
1046 (coding-system-for-write mm-text-coding-system
))
1047 (gnus-write-buffer filename
)))
1048 (kill-buffer file-buffer
))
1049 (error "Output file does not exist")))
1052 (insert-buffer-substring artbuf
)
1054 (gnus-convert-article-to-rmail)
1055 ;; Non-Babyl case copied from gnus-output-to-mail.
1056 (goto-char (point-min))
1057 (if (looking-at "From ")
1059 (insert "From nobody " (current-time-string) "\n"))
1060 (let (case-fold-search)
1061 (while (re-search-forward "^From " nil t
)
1064 ;; Decide whether to append to a file or to an Emacs buffer.
1065 (let ((outbuf (get-file-buffer filename
)))
1068 (unless babyl
; from gnus-output-to-mail
1069 (let ((buffer-read-only nil
))
1070 (goto-char (point-max))
1072 (unless (looking-at "\n\n")
1073 (goto-char (point-max))
1077 (let ((file-name-coding-system nnmail-pathname-coding-system
))
1078 (mm-append-to-file (point-min) (point-max) filename
)))
1079 ;; File has been visited, in buffer OUTBUF.
1081 (let ((buffer-read-only nil
)
1082 (msg (and (boundp 'rmail-current-message
)
1083 (symbol-value 'rmail-current-message
))))
1084 ;; If MSG is non-nil, buffer is in RMAIL mode.
1085 ;; Compare this with rmail-output-to-rmail-buffer in Emacs 23.
1088 (rmail-swap-buffers-maybe)
1089 (rmail-maybe-set-message-counters))
1091 (narrow-to-region (point-max) (point-max)))
1092 (insert-buffer-substring tmpbuf
)
1095 (goto-char (point-min))
1097 (search-backward "\n\^_")
1098 (narrow-to-region (point) (point-max)))
1099 (rmail-count-new-messages t
)
1100 (when (rmail-summary-exists)
1101 (rmail-select-summary
1102 (rmail-update-summary)))
1103 (rmail-show-message msg
))
1105 (kill-buffer tmpbuf
)))
1107 (defun gnus-output-to-mail (filename &optional ask
)
1108 "Append the current article to a mail file named FILENAME."
1110 (setq filename
(expand-file-name filename
))
1111 (let ((artbuf (current-buffer))
1112 (tmpbuf (get-buffer-create " *Gnus-output*")))
1114 ;; Create the file, if it doesn't exist.
1115 (when (and (not (get-file-buffer filename
))
1116 (not (file-exists-p filename
)))
1119 (concat "\"" filename
"\" does not exist, create it? ")))
1120 (let ((file-buffer (create-file-buffer filename
)))
1121 (with-current-buffer file-buffer
1122 (let ((require-final-newline nil
)
1123 (coding-system-for-write mm-text-coding-system
))
1124 (gnus-write-buffer filename
)))
1125 (kill-buffer file-buffer
))
1126 (error "Output file does not exist")))
1129 (insert-buffer-substring artbuf
)
1130 (goto-char (point-min))
1131 (if (looking-at "From ")
1133 (insert "From nobody " (current-time-string) "\n"))
1134 (let (case-fold-search)
1135 (while (re-search-forward "^From " nil t
)
1138 ;; Decide whether to append to a file or to an Emacs buffer.
1139 (let ((outbuf (get-file-buffer filename
)))
1141 (let ((buffer-read-only nil
))
1143 (goto-char (point-max))
1145 (unless (looking-at "\n\n")
1146 (goto-char (point-max))
1150 (goto-char (point-max))
1151 (let ((file-name-coding-system nnmail-pathname-coding-system
))
1152 (mm-append-to-file (point-min) (point-max) filename
))))
1153 ;; File has been visited, in buffer OUTBUF.
1155 (let ((buffer-read-only nil
))
1156 (goto-char (point-max))
1160 (insert-buffer-substring tmpbuf
)))))
1161 (kill-buffer tmpbuf
)))
1163 (defun gnus-convert-article-to-rmail ()
1164 "Convert article in current buffer to Rmail message format."
1165 (let ((buffer-read-only nil
))
1166 ;; Convert article directly into Babyl format.
1167 (goto-char (point-min))
1168 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
1169 (while (search-forward "\n\^_" nil t
) ;single char
1170 (replace-match "\n^_" t t
)) ;2 chars: "^" and "_"
1171 (goto-char (point-max))
1174 (defun gnus-map-function (funs arg
)
1175 "Apply the result of the first function in FUNS to the second, and so on.
1176 ARG is passed to the first function."
1178 (setq arg
(funcall (pop funs
) arg
)))
1181 (defun gnus-run-hooks (&rest funcs
)
1182 "Does the same as `run-hooks', but saves the current buffer."
1183 (save-current-buffer
1184 (apply 'run-hooks funcs
)))
1186 (defun gnus-run-hook-with-args (hook &rest args
)
1187 "Does the same as `run-hook-with-args', but saves the current buffer."
1188 (save-current-buffer
1189 (apply 'run-hook-with-args hook args
)))
1191 (defun gnus-run-mode-hooks (&rest funcs
)
1192 "Run `run-mode-hooks' if it is available, otherwise `run-hooks'.
1193 This function saves the current buffer."
1194 (if (fboundp 'run-mode-hooks
)
1195 (save-current-buffer (apply 'run-mode-hooks funcs
))
1196 (save-current-buffer (apply 'run-hooks funcs
))))
1200 (defvar gnus-group-buffer
) ; Compiler directive
1201 (defun gnus-alive-p ()
1202 "Say whether Gnus is running or not."
1203 (and (boundp 'gnus-group-buffer
)
1204 (get-buffer gnus-group-buffer
)
1205 (with-current-buffer gnus-group-buffer
1206 (eq major-mode
'gnus-group-mode
))))
1208 (defun gnus-remove-if (predicate sequence
&optional hash-table-p
)
1209 "Return a copy of SEQUENCE with all items satisfying PREDICATE removed.
1210 SEQUENCE should be a list, a vector, or a string. Returns always a list.
1211 If HASH-TABLE-P is non-nil, regards SEQUENCE as a hash table."
1214 (mapatoms (lambda (symbol)
1215 (unless (funcall predicate symbol
)
1218 (unless (listp sequence
)
1219 (setq sequence
(append sequence nil
)))
1221 (unless (funcall predicate
(car sequence
))
1222 (push (car sequence
) out
))
1223 (setq sequence
(cdr sequence
))))
1226 (defun gnus-remove-if-not (predicate sequence
&optional hash-table-p
)
1227 "Return a copy of SEQUENCE with all items not satisfying PREDICATE removed.
1228 SEQUENCE should be a list, a vector, or a string. Returns always a list.
1229 If HASH-TABLE-P is non-nil, regards SEQUENCE as a hash table."
1232 (mapatoms (lambda (symbol)
1233 (when (funcall predicate symbol
)
1236 (unless (listp sequence
)
1237 (setq sequence
(append sequence nil
)))
1239 (when (funcall predicate
(car sequence
))
1240 (push (car sequence
) out
))
1241 (setq sequence
(cdr sequence
))))
1244 (if (fboundp 'assq-delete-all
)
1245 (defalias 'gnus-delete-alist
'assq-delete-all
)
1246 (defun gnus-delete-alist (key alist
)
1247 "Delete from ALIST all elements whose car is KEY.
1248 Return the modified alist."
1250 (while (setq entry
(assq key alist
))
1251 (setq alist
(delq entry alist
)))
1254 (defun gnus-grep-in-list (word list
)
1255 "Find if a WORD matches any regular expression in the given LIST."
1256 (when (and word list
)
1259 (when (string-match r word
)
1260 (throw 'found r
))))))
1262 (defmacro gnus-alist-pull
(key alist
&optional assoc-p
)
1263 "Modify ALIST to be without KEY."
1264 (unless (symbolp alist
)
1265 (error "Not a symbol: %s" alist
))
1266 (let ((fun (if assoc-p
'assoc
'assq
)))
1267 `(setq ,alist
(delq (,fun
,key
,alist
) ,alist
))))
1269 (defun gnus-globalify-regexp (re)
1270 "Return a regexp that matches a whole line, if RE matches a part of it."
1271 (concat (unless (string-match "^\\^" re
) "^.*")
1273 (unless (string-match "\\$$" re
) ".*$")))
1275 (defun gnus-set-window-start (&optional point
)
1276 "Set the window start to POINT, or (point) if nil."
1277 (let ((win (gnus-get-buffer-window (current-buffer) t
)))
1279 (set-window-start win
(or point
(point))))))
1281 (defun gnus-annotation-in-region-p (b e
)
1283 (eq (cadr (memq 'gnus-undeletable
(text-properties-at b
))) t
)
1284 (text-property-any b e
'gnus-undeletable t
)))
1286 (defun gnus-or (&rest elems
)
1287 "Return non-nil if any of the elements are non-nil."
1291 (throw 'found t
)))))
1293 (defun gnus-and (&rest elems
)
1294 "Return non-nil if all of the elements are non-nil."
1298 (throw 'found nil
)))
1301 ;; gnus.el requires mm-util.
1302 (declare-function mm-disable-multibyte
"mm-util")
1304 (defun gnus-write-active-file (file hashtb
&optional full-names
)
1305 ;; `coding-system-for-write' should be `raw-text' or equivalent.
1306 (let ((coding-system-for-write nnmail-active-file-coding-system
))
1307 (with-temp-file file
1308 ;; The buffer should be in the unibyte mode because group names
1309 ;; are ASCII text or encoded non-ASCII text (i.e., unibyte).
1310 (mm-disable-multibyte)
1316 (insert (format "%S %d %d y\n"
1319 (intern (gnus-group-real-name (symbol-name sym
))))
1320 (or (cdr (symbol-value sym
))
1321 (car (symbol-value sym
)))
1322 (car (symbol-value sym
))))))
1324 (goto-char (point-max))
1325 (while (search-backward "\\." nil t
)
1328 ;; Fixme: Why not use `with-output-to-temp-buffer'?
1329 (defmacro gnus-with-output-to-file
(file &rest body
)
1330 (let ((buffer (make-symbol "output-buffer"))
1331 (size (make-symbol "output-buffer-size"))
1332 (leng (make-symbol "output-buffer-length"))
1333 (append (make-symbol "output-buffer-append")))
1334 `(let* ((,size
131072)
1335 (,buffer
(make-string ,size
0))
1340 (aset ,buffer
,leng c
)
1342 (if (= ,size
(setq ,leng
(1+ ,leng
)))
1343 (progn (write-region ,buffer nil
,file
,append
'no-msg
)
1348 (let ((coding-system-for-write 'no-conversion
))
1349 (write-region (substring ,buffer
0 ,leng
) nil
,file
1350 ,append
'no-msg
))))))
1352 (put 'gnus-with-output-to-file
'lisp-indent-function
1)
1353 (put 'gnus-with-output-to-file
'edebug-form-spec
'(form body
))
1355 (if (fboundp 'union
)
1356 (defalias 'gnus-union
'union
)
1357 (defun gnus-union (l1 l2
)
1358 "Set union of lists L1 and L2."
1359 (cond ((null l1
) l2
)
1363 (or (>= (length l1
) (length l2
))
1364 (setq l1
(prog1 l2
(setq l2 l1
))))
1366 (or (member (car l2
) l1
)
1371 (declare-function gnus-add-text-properties
"gnus"
1372 (start end properties
&optional object
))
1374 (defun gnus-add-text-properties-when
1375 (property value start end properties
&optional object
)
1376 "Like `gnus-add-text-properties', only applied on where PROPERTY is VALUE."
1379 (< start end
) ;; XEmacs will loop for every when start=end.
1380 (setq point
(text-property-not-all start end property value
)))
1381 (gnus-add-text-properties start point properties object
)
1382 (setq start
(text-property-any point end property value
)))
1384 (gnus-add-text-properties start end properties object
))))
1386 (defun gnus-remove-text-properties-when
1387 (property value start end properties
&optional object
)
1388 "Like `remove-text-properties', only applied on where PROPERTY is VALUE."
1392 (setq point
(text-property-not-all start end property value
)))
1393 (remove-text-properties start point properties object
)
1394 (setq start
(text-property-any point end property value
)))
1396 (remove-text-properties start end properties object
))
1399 (defun gnus-string-remove-all-properties (string)
1402 (set-text-properties 0 (length string
) nil string
)
1406 ;; This might use `compare-strings' to reduce consing in the
1407 ;; case-insensitive case, but it has to cope with null args.
1408 ;; (`string-equal' uses symbol print names.)
1409 (defun gnus-string-equal (x y
)
1410 "Like `string-equal', except it compares case-insensitively."
1411 (and (= (length x
) (length y
))
1412 (or (string-equal x y
)
1413 (string-equal (downcase x
) (downcase y
)))))
1415 (defcustom gnus-use-byte-compile t
1416 "If non-nil, byte-compile crucial run-time code.
1417 Setting it to nil has no effect after the first time `gnus-byte-compile'
1421 :group
'gnus-various
)
1423 (defun gnus-byte-compile (form)
1424 "Byte-compile FORM if `gnus-use-byte-compile' is non-nil."
1425 (if gnus-use-byte-compile
1428 ;; Work around a bug in XEmacs 21.4
1429 (require 'byte-optimize
)
1432 (defalias 'gnus-byte-compile
1434 (let ((byte-compile-warnings '(unresolved callargs redefine
)))
1435 (byte-compile form
))))
1436 (gnus-byte-compile form
))
1439 (defun gnus-remassoc (key alist
)
1440 "Delete by side effect any elements of LIST whose car is `equal' to KEY.
1441 The modified LIST is returned. If the first member
1442 of LIST has a car that is `equal' to KEY, there is no way to remove it
1443 by side effect; therefore, write `(setq foo (gnus-remassoc key foo))' to be
1444 sure of changing the value of `foo'."
1446 (if (equal key
(caar alist
))
1448 (setcdr alist
(gnus-remassoc key
(cdr alist
)))
1451 (defun gnus-update-alist-soft (key value alist
)
1453 (cons (cons key value
) (gnus-remassoc key alist
))
1454 (gnus-remassoc key alist
)))
1456 (defun gnus-create-info-command (node)
1457 "Create a command that will go to info NODE."
1460 ,(concat "Enter the info system at node " node
)
1461 (Info-goto-node ,node
)
1462 (setq gnus-info-buffer
(current-buffer))
1463 (gnus-configure-windows 'info
)))
1465 (defun gnus-not-ignore (&rest args
)
1468 (defvar gnus-directory-sep-char-regexp
"/"
1469 "The regexp of directory separator character.
1470 If you find some problem with the directory separator character, try
1471 \"[/\\\\\]\" for some systems.")
1473 (defun gnus-url-unhex (x)
1480 ;; Fixme: Do it like QP.
1481 (defun gnus-url-unhex-string (str &optional allow-newlines
)
1482 "Remove %XX, embedded spaces, etc in a url.
1483 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
1484 decoding of carriage returns and line feeds in the string, which is normally
1485 forbidden in URL encoding."
1487 (case-fold-search t
))
1488 (while (string-match "%[0-9a-f][0-9a-f]" str
)
1489 (let* ((start (match-beginning 0))
1490 (ch1 (gnus-url-unhex (elt str
(+ start
1))))
1492 (gnus-url-unhex (elt str
(+ start
2))))))
1494 tmp
(substring str
0 start
)
1497 (char-to-string code
))
1498 ((or (= code ?
\n) (= code ?
\r))
1500 (t (char-to-string code
))))
1501 str
(substring str
(match-end 0)))))
1502 (setq tmp
(concat tmp str
))
1505 (defun gnus-make-predicate (spec)
1506 "Transform SPEC into a function that can be called.
1507 SPEC is a predicate specifier that contains stuff like `or', `and',
1508 `not', lists and functions. The functions all take one parameter."
1509 `(lambda (elem) ,(gnus-make-predicate-1 spec
)))
1511 (defun gnus-make-predicate-1 (spec)
1516 (if (memq (car spec
) '(or and not
))
1517 `(,(car spec
) ,@(mapcar 'gnus-make-predicate-1
(cdr spec
)))
1518 (error "Invalid predicate specifier: %s" spec
)))))
1520 (defun gnus-completing-read (prompt collection
&optional require-match
1521 initial-input history def
)
1522 "Call `gnus-completing-read-function'."
1523 (funcall gnus-completing-read-function
1524 (concat prompt
(when def
1525 (concat " (default " def
")"))
1527 collection require-match initial-input history def
))
1529 (defun gnus-emacs-completing-read (prompt collection
&optional require-match
1530 initial-input history def
)
1531 "Call standard `completing-read-function'."
1532 (let ((completion-styles gnus-completion-styles
))
1533 (completing-read prompt
1534 ;; Old XEmacs (at least 21.4) expect an alist for
1536 (mapcar 'list collection
)
1537 nil require-match initial-input history def
)))
1539 (autoload 'ido-completing-read
"ido")
1540 (defun gnus-ido-completing-read (prompt collection
&optional require-match
1541 initial-input history def
)
1542 "Call `ido-completing-read-function'."
1543 (ido-completing-read prompt collection nil require-match
1544 initial-input history def
))
1547 (declare-function iswitchb-read-buffer
"iswitchb"
1548 (prompt &optional default require-match start matches-set
))
1549 (defvar iswitchb-temp-buflist
)
1551 (defun gnus-iswitchb-completing-read (prompt collection
&optional require-match
1552 initial-input history def
)
1553 "`iswitchb' based completing-read function."
1554 ;; Make sure iswitchb is loaded before we let-bind its variables.
1555 ;; If it is loaded inside the let, variables can become unbound afterwards.
1557 (let ((iswitchb-make-buflist-hook
1559 (setq iswitchb-temp-buflist
1560 (let ((choices (append
1561 (when initial-input
(list initial-input
))
1562 (symbol-value history
) collection
))
1565 (setq filtered-choices
(adjoin x filtered-choices
)))
1566 (nreverse filtered-choices
))))))
1570 (add-hook 'minibuffer-setup-hook
'iswitchb-minibuffer-setup
))
1571 (iswitchb-read-buffer prompt def require-match
))
1573 (remove-hook 'minibuffer-setup-hook
'iswitchb-minibuffer-setup
)))))
1575 (defun gnus-graphic-display-p ()
1576 (if (featurep 'xemacs
)
1577 (device-on-window-system-p)
1578 (display-graphic-p)))
1580 (put 'gnus-parse-without-error
'lisp-indent-function
0)
1581 (put 'gnus-parse-without-error
'edebug-form-spec
'(body))
1583 (defmacro gnus-parse-without-error
(&rest body
)
1584 "Allow continuing onto the next line even if an error occurs."
1585 `(while (not (eobp))
1589 (goto-char (point-max)))
1591 (gnus-error 4 "Invalid data on line %d"
1592 (count-lines (point-min) (point)))
1593 (forward-line 1)))))
1595 (defun gnus-cache-file-contents (file variable function
)
1596 "Cache the contents of FILE in VARIABLE. The contents come from FUNCTION."
1597 (let ((time (nth 5 (file-attributes file
)))
1599 (if (or (null (setq value
(symbol-value variable
)))
1600 (not (equal (car value
) file
))
1601 (not (equal (nth 1 value
) time
)))
1603 (setq contents
(funcall function file
))
1604 (set variable
(list file time contents
))
1608 (defun gnus-multiple-choice (prompt choice
&optional idx
)
1609 "Ask user a multiple choice question.
1610 CHOICE is a list of the choice char and help message at IDX."
1612 (save-window-excursion
1615 (message "%s (%s): "
1618 (mapconcat (lambda (s) (char-to-string (car s
)))
1619 choice
", ") ", ?"))
1620 (setq tchar
(read-char))
1621 (when (not (assq tchar choice
))
1623 (setq buf
(get-buffer-create "*Gnus Help*"))
1625 (fundamental-mode) ; for Emacs 20.4+
1626 (buffer-disable-undo)
1628 (insert prompt
":\n\n")
1635 ;; find the longest string to display
1637 (setq n
(length (nth idx
(car list
))))
1640 (setq list
(cdr list
)))
1641 (setq max
(+ max
4)) ; %c, `:', SPACE, a SPACE at end
1642 (setq n
(/ (1- (window-width)) max
)) ; items per line
1643 (setq width
(/ (1- (window-width)) n
)) ; width of each item
1644 ;; insert `n' items, each in a field of width `width'
1649 (delete-char -
1) ; the `\n' takes a char
1651 (setq pad
(- width
3))
1652 (setq format
(concat "%c: %-" (int-to-string pad
) "s"))
1653 (insert (format format
(caar alist
) (nth idx
(car alist
))))
1654 (setq alist
(cdr alist
))
1655 (setq i
(1+ i
))))))))
1656 (if (buffer-live-p buf
)
1660 (if (featurep 'emacs
)
1661 (defalias 'gnus-select-frame-set-input-focus
'select-frame-set-input-focus
)
1662 (if (fboundp 'select-frame-set-input-focus
)
1663 (defalias 'gnus-select-frame-set-input-focus
'select-frame-set-input-focus
)
1664 ;; XEmacs 21.4, SXEmacs
1665 (defun gnus-select-frame-set-input-focus (frame)
1666 "Select FRAME, raise it, and set input focus, if possible."
1668 (select-frame frame
)
1669 (focus-frame frame
))))
1671 (defun gnus-frame-or-window-display-name (object)
1672 "Given a frame or window, return the associated display name.
1673 Return nil otherwise."
1674 (if (featurep 'xemacs
)
1675 (device-connection (dfw-device object
))
1676 (if (or (framep object
)
1677 (and (windowp object
)
1678 (setq object
(window-frame object
))))
1679 (let ((display (frame-parameter object
'display
)))
1680 (if (and (stringp display
)
1681 ;; Exclude invalid display names.
1682 (string-match "\\`[^:]*:[0-9]+\\(\\.[0-9]+\\)?\\'"
1686 (defvar tool-bar-mode
)
1688 (defun gnus-tool-bar-update (&rest ignore
)
1689 "Update the tool bar."
1690 (when (and (boundp 'tool-bar-mode
)
1693 (func (cond ((featurep 'xemacs
)
1695 ((fboundp 'tool-bar-update
)
1697 ((fboundp 'force-window-update
)
1698 'force-window-update
)
1699 ((fboundp 'redraw-frame
)
1700 (setq args
(list (selected-frame)))
1703 (apply func args
))))
1705 ;; Fixme: This has only one use (in gnus-agent), which isn't worthwhile.
1706 (defmacro gnus-mapcar
(function seq1
&rest seqs2_n
)
1707 "Apply FUNCTION to each element of the sequences, and make a list of the results.
1708 If there are several sequences, FUNCTION is called with that many arguments,
1709 and mapping stops as soon as the shortest sequence runs out. With just one
1710 sequence, this is like `mapcar'. With several, it is like the Common Lisp
1711 `mapcar' function extended to arbitrary sequence types."
1714 (let* ((seqs (cons seq1 seqs2_n
))
1716 (heads (mapcar (lambda (seq)
1717 (make-symbol (concat "head"
1719 (setq cnt
(1+ cnt
))))))
1721 (result (make-symbol "result"))
1722 (result-tail (make-symbol "result-tail")))
1723 `(let* ,(let* ((bindings (cons nil nil
))
1725 (nconc bindings
(list (list result
'(cons nil nil
))))
1726 (nconc bindings
(list (list result-tail result
)))
1728 (nconc bindings
(list (list (pop heads
) (pop seqs
)))))
1730 (while (and ,@heads
)
1731 (setcdr ,result-tail
(cons (funcall ,function
1732 ,@(mapcar (lambda (h) (list 'car h
))
1735 (setq ,result-tail
(cdr ,result-tail
)
1736 ,@(apply 'nconc
(mapcar (lambda (h) (list h
(list 'cdr h
))) heads
))))
1738 `(mapcar ,function
,seq1
)))
1740 (if (fboundp 'merge
)
1741 (defalias 'gnus-merge
'merge
)
1742 ;; Adapted from cl-seq.el
1743 (defun gnus-merge (type list1 list2 pred
)
1744 "Destructively merge lists LIST1 and LIST2 to produce a new list.
1745 Argument TYPE is for compatibility and ignored.
1746 Ordering of the elements is preserved according to PRED, a `less-than'
1747 predicate on the elements."
1749 (while (and list1 list2
)
1750 (if (funcall pred
(car list2
) (car list1
))
1751 (push (pop list2
) res
)
1752 (push (pop list1
) res
)))
1753 (nconc (nreverse res
) list1 list2
))))
1755 (defvar xemacs-codename
)
1756 (defvar sxemacs-codename
)
1757 (defvar emacs-program-version
)
1759 (defun gnus-emacs-version ()
1760 "Stringified Emacs version."
1761 (let* ((lst (if (listp gnus-user-agent
)
1763 '(gnus emacs type
)))
1764 (system-v (cond ((memq 'config lst
)
1765 system-configuration
)
1767 (symbol-name system-type
))
1770 (cond ((featurep 'sxemacs
)
1771 (setq emacsname
"SXEmacs"
1772 codename sxemacs-codename
))
1774 (setq emacsname
"XEmacs"
1775 codename xemacs-codename
))
1777 (setq emacsname
"Emacs")))
1779 ((not (memq 'emacs lst
))
1781 ((string-match "^\\(\\([.0-9]+\\)*\\)\\.[0-9]+$" emacs-version
)
1783 (concat "Emacs/" (match-string 1 emacs-version
)
1785 (concat " (" system-v
")")
1787 ((or (featurep 'sxemacs
) (featurep 'xemacs
))
1788 ;; XEmacs or SXEmacs:
1789 (concat emacsname
"/" emacs-program-version
1791 (when (memq 'codename lst
)
1792 (push codename plst
))
1794 (push system-v plst
))
1795 (unless (featurep 'mule
)
1796 (push "no MULE" plst
))
1797 (when (> (length plst
) 0)
1799 " (" (mapconcat 'identity
(reverse plst
) ", ") ")")))))
1800 (t emacs-version
))))
1802 (defun gnus-rename-file (old-path new-path
&optional trim
)
1803 "Rename OLD-PATH as NEW-PATH. If TRIM, recursively delete
1804 empty directories from OLD-PATH."
1805 (when (file-exists-p old-path
)
1806 (let* ((old-dir (file-name-directory old-path
))
1807 (old-name (file-name-nondirectory old-path
))
1808 (new-dir (file-name-directory new-path
))
1809 (new-name (file-name-nondirectory new-path
))
1811 (gnus-make-directory new-dir
)
1812 (rename-file old-path new-path t
)
1814 (while (progn (setq temp
(directory-files old-dir
))
1815 (while (member (car temp
) '("." ".."))
1816 (setq temp
(cdr temp
)))
1817 (= (length temp
) 0))
1818 (delete-directory old-dir
)
1819 (setq old-dir
(file-name-as-directory
1821 (concat old-dir
"..")))))))))
1823 (defun gnus-set-file-modes (filename mode
)
1824 "Wrapper for set-file-modes."
1826 (set-file-modes filename mode
)))
1828 (if (fboundp 'set-process-query-on-exit-flag
)
1829 (defalias 'gnus-set-process-query-on-exit-flag
1830 'set-process-query-on-exit-flag
)
1831 (defalias 'gnus-set-process-query-on-exit-flag
1832 'process-kill-without-query
))
1834 (defalias 'gnus-read-shell-command
1835 (if (fboundp 'read-shell-command
) 'read-shell-command
'read-string
))
1837 (defmacro gnus-put-display-table
(range value display-table
)
1838 "Set the value for char RANGE to VALUE in DISPLAY-TABLE. "
1839 (if (featurep 'xemacs
)
1841 `(if (fboundp 'put-display-table
)
1842 (put-display-table ,range
,value
,display-table
)
1843 (if (sequencep ,display-table
)
1844 (aset ,display-table
,range
,value
)
1845 (put-char-table ,range
,value
,display-table
))))
1846 `(aset ,display-table
,range
,value
)))
1848 (defmacro gnus-get-display-table
(character display-table
)
1849 "Find value for CHARACTER in DISPLAY-TABLE. "
1850 (if (featurep 'xemacs
)
1851 `(if (fboundp 'get-display-table
)
1852 (get-display-table ,character
,display-table
)
1853 (if (sequencep ,display-table
)
1854 (aref ,display-table
,character
)
1855 (get-char-table ,character
,display-table
)))
1856 `(aref ,display-table
,character
)))
1858 (defun gnus-rescale-image (image size
)
1859 "Rescale IMAGE to SIZE if possible.
1860 SIZE is in format (WIDTH . HEIGHT). Return a new image.
1861 Sizes are in pixels."
1862 (if (or (not (fboundp 'imagemagick-types
))
1863 (not (get-buffer-window (current-buffer))))
1865 (let ((new-width (car size
))
1866 (new-height (cdr size
)))
1867 (when (> (cdr (image-size image t
)) new-height
)
1868 (setq image
(or (create-image (plist-get (cdr image
) :data
) 'imagemagick t
1871 (when (> (car (image-size image t
)) new-width
)
1873 (create-image (plist-get (cdr image
) :data
) 'imagemagick t
1878 (defun gnus-recursive-directory-files (dir)
1879 "Return all regular files below DIR."
1881 (dolist (file (directory-files dir t
))
1882 (when (and (not (member (file-name-nondirectory file
) '("." "..")))
1883 (file-readable-p file
))
1885 ((file-regular-p file
)
1887 ((file-directory-p file
)
1888 (setq files
(append (gnus-recursive-directory-files file
) files
))))))
1891 (defun gnus-list-memq-of-list (elements list
)
1892 "Return non-nil if any of the members of ELEMENTS are in LIST."
1894 (dolist (elem elements
)
1895 (setq found
(or found
1901 ((fboundp 'match-substitute-replacement
)
1902 (defalias 'gnus-match-substitute-replacement
'match-substitute-replacement
))
1904 (defun gnus-match-substitute-replacement (replacement &optional fixedcase literal string subexp
)
1905 "Return REPLACEMENT as it will be inserted by `replace-match'.
1906 In other words, all back-references in the form `\\&' and `\\N'
1907 are substituted with actual strings matched by the last search.
1908 Optional FIXEDCASE, LITERAL, STRING and SUBEXP have the same
1909 meaning as for `replace-match'.
1911 This is the definition of match-substitute-replacement in subr.el from GNU Emacs."
1912 (let ((match (match-string 0 string
)))
1914 (set-match-data (mapcar (lambda (x)
1916 (- x
(match-beginning 0))
1919 (replace-match replacement fixedcase literal match subexp
)))))))
1921 (if (fboundp 'string-match-p
)
1922 (defalias 'gnus-string-match-p
'string-match-p
)
1923 (defsubst gnus-string-match-p
(regexp string
&optional start
)
1925 Same as `string-match' except this function does not change the match data."
1927 (string-match regexp string start
))))
1929 (if (fboundp 'string-prefix-p
)
1930 (defalias 'gnus-string-prefix-p
'string-prefix-p
)
1931 (defun gnus-string-prefix-p (str1 str2
&optional ignore-case
)
1932 "Return non-nil if STR1 is a prefix of STR2.
1933 If IGNORE-CASE is non-nil, the comparison is done without paying attention
1934 to case differences."
1935 (and (<= (length str1
) (length str2
))
1936 (let ((prefix (substring str2
0 (length str1
))))
1938 (string-equal (downcase str1
) (downcase prefix
))
1939 (string-equal str1 prefix
))))))
1942 (if (fboundp 'macroexpand-all
)
1943 (defalias 'gnus-macroexpand-all
'macroexpand-all
)
1944 (defun gnus-macroexpand-all (form &optional environment
)
1945 "Return result of expanding macros at all levels in FORM.
1946 If no macros are expanded, FORM is returned unchanged.
1947 The second optional arg ENVIRONMENT specifies an environment of macro
1948 definitions to shadow the loaded ones for use in file byte-compilation."
1951 (len (length (setq form
(copy-sequence form
))))
1954 (setcar (nthcdr idx form
) (gnus-macroexpand-all (nth idx form
)
1956 (setq idx
(1+ idx
)))
1957 (if (eq (setq expanded
(macroexpand form environment
)) form
)
1959 (gnus-macroexpand-all expanded environment
)))
1962 ;; Simple check: can be a macro but this way, although slow, it's really clear.
1963 ;; We don't use `bound-and-true-p' because it's not in XEmacs.
1964 (defun gnus-bound-and-true-p (sym)
1965 (and (boundp sym
) (symbol-value sym
)))
1967 (if (fboundp 'timer--function
)
1968 (defalias 'gnus-timer--function
'timer--function
)
1969 (defun gnus-timer--function (timer)
1972 (provide 'gnus-util
)
1974 ;;; gnus-util.el ends here