Commit | Line | Data |
---|---|---|
eec82323 | 1 | ;;; gnus-util.el --- utility functions for Gnus |
e84b4b86 | 2 | |
88e6695f | 3 | ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
e3fe4da0 | 4 | ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
eec82323 | 5 | |
6748645f | 6 | ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org> |
eec82323 LMI |
7 | ;; Keywords: news |
8 | ||
9 | ;; This file is part of GNU Emacs. | |
10 | ||
5e809f55 | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
eec82323 | 12 | ;; it under the terms of the GNU General Public License as published by |
5e809f55 GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
eec82323 LMI |
15 | |
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
5e809f55 | 18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
eec82323 LMI |
19 | ;; GNU General Public License for more details. |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
5e809f55 | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
eec82323 LMI |
23 | |
24 | ;;; Commentary: | |
25 | ||
26 | ;; Nothing in this file depends on any other parts of Gnus -- all | |
27 | ;; functions and macros in this file are utility functions that are | |
28 | ;; used by Gnus and may be used by any other package without loading | |
29 | ;; Gnus first. | |
30 | ||
23f87bed | 31 | ;; [Unfortunately, it does depend on other parts of Gnus, e.g. the |
01c52d31 | 32 | ;; autoloads and defvars below...] |
23f87bed | 33 | |
eec82323 LMI |
34 | ;;; Code: |
35 | ||
88bfa2e4 GM |
36 | ;; For Emacs < 22.2. |
37 | (eval-and-compile | |
38 | (unless (fboundp 'declare-function) (defmacro declare-function (&rest r)))) | |
5eee36fa | 39 | (eval-when-compile |
9efa445f DN |
40 | (require 'cl)) |
41 | ;; Fixme: this should be a gnus variable, not nnmail-. | |
42 | (defvar nnmail-pathname-coding-system) | |
43 | (defvar nnmail-active-file-coding-system) | |
44 | ||
45 | ;; Inappropriate references to other parts of Gnus. | |
46 | (defvar gnus-emphasize-whitespace-regexp) | |
47 | (defvar gnus-original-article-buffer) | |
48 | (defvar gnus-user-agent) | |
49 | ||
16409b0b | 50 | (require 'time-date) |
23f87bed | 51 | (require 'netrc) |
eec82323 | 52 | |
dbb6c370 GM |
53 | (autoload 'message-fetch-field "message") |
54 | (autoload 'gnus-get-buffer-window "gnus-win") | |
55 | (autoload 'rmail-insert-rmail-file-header "rmail") | |
56 | (autoload 'rmail-count-new-messages "rmail") | |
57 | (autoload 'rmail-show-message "rmail") | |
58 | (autoload 'nnheader-narrow-to-headers "nnheader") | |
59 | (autoload 'nnheader-replace-chars-in-string "nnheader") | |
09aece0b | 60 | (autoload 'mail-header-remove-comments "mail-parse") |
23f87bed MB |
61 | |
62 | (eval-and-compile | |
63 | (cond | |
f67d6742 | 64 | ;; Prefer `replace-regexp-in-string' (present in Emacs, XEmacs 21.5, |
4b4f6dc8 | 65 | ;; SXEmacs 22.1.4) over `replace-in-string'. The latter leads to inf-loops |
f67d6742 MB |
66 | ;; on empty matches: |
67 | ;; (replace-in-string "foo" "/*$" "/") | |
68 | ;; (replace-in-string "xe" "\\(x\\)?" "") | |
23f87bed | 69 | ((fboundp 'replace-regexp-in-string) |
01c52d31 | 70 | (defun gnus-replace-in-string (string regexp newtext &optional literal) |
ad136a7c MB |
71 | "Replace all matches for REGEXP with NEWTEXT in STRING. |
72 | If LITERAL is non-nil, insert NEWTEXT literally. Return a new | |
73 | string containing the replacements. | |
74 | ||
75 | This is a compatibility function for different Emacsen." | |
23f87bed | 76 | (replace-regexp-in-string regexp newtext string nil literal))) |
f67d6742 | 77 | ((fboundp 'replace-in-string) |
01c52d31 | 78 | (defalias 'gnus-replace-in-string 'replace-in-string)))) |
eec82323 LMI |
79 | |
80 | (defun gnus-boundp (variable) | |
81 | "Return non-nil if VARIABLE is bound and non-nil." | |
82 | (and (boundp variable) | |
83 | (symbol-value variable))) | |
84 | ||
85 | (defmacro gnus-eval-in-buffer-window (buffer &rest forms) | |
86 | "Pop to BUFFER, evaluate FORMS, and then return to the original window." | |
87 | (let ((tempvar (make-symbol "GnusStartBufferWindow")) | |
23f87bed MB |
88 | (w (make-symbol "w")) |
89 | (buf (make-symbol "buf"))) | |
eec82323 | 90 | `(let* ((,tempvar (selected-window)) |
23f87bed MB |
91 | (,buf ,buffer) |
92 | (,w (gnus-get-buffer-window ,buf 'visible))) | |
eec82323 | 93 | (unwind-protect |
23f87bed MB |
94 | (progn |
95 | (if ,w | |
96 | (progn | |
97 | (select-window ,w) | |
98 | (set-buffer (window-buffer ,w))) | |
99 | (pop-to-buffer ,buf)) | |
100 | ,@forms) | |
101 | (select-window ,tempvar))))) | |
eec82323 LMI |
102 | |
103 | (put 'gnus-eval-in-buffer-window 'lisp-indent-function 1) | |
104 | (put 'gnus-eval-in-buffer-window 'edebug-form-spec '(form body)) | |
105 | ||
106 | (defmacro gnus-intern-safe (string hashtable) | |
815b81c8 | 107 | "Get hash value. Arguments are STRING and HASHTABLE." |
eec82323 LMI |
108 | `(let ((symbol (intern ,string ,hashtable))) |
109 | (or (boundp symbol) | |
110 | (set symbol nil)) | |
111 | symbol)) | |
112 | ||
eec82323 LMI |
113 | (defsubst gnus-goto-char (point) |
114 | (and point (goto-char point))) | |
115 | ||
116 | (defmacro gnus-buffer-exists-p (buffer) | |
117 | `(let ((buffer ,buffer)) | |
118 | (when buffer | |
119 | (funcall (if (stringp buffer) 'get-buffer 'buffer-name) | |
120 | buffer)))) | |
121 | ||
23f87bed MB |
122 | ;; The LOCAL arg to `add-hook' is interpreted differently in Emacs and |
123 | ;; XEmacs. In Emacs we don't need to call `make-local-hook' first. | |
124 | ;; It's harmless, though, so the main purpose of this alias is to shut | |
125 | ;; up the byte compiler. | |
126 | (defalias 'gnus-make-local-hook | |
127 | (if (eq (get 'make-local-hook 'byte-compile) | |
128 | 'byte-compile-obsolete) | |
129 | 'ignore ; Emacs | |
130 | 'make-local-hook)) ; XEmacs | |
131 | ||
eec82323 LMI |
132 | (defun gnus-delete-first (elt list) |
133 | "Delete by side effect the first occurrence of ELT as a member of LIST." | |
134 | (if (equal (car list) elt) | |
135 | (cdr list) | |
136 | (let ((total list)) | |
137 | (while (and (cdr list) | |
138 | (not (equal (cadr list) elt))) | |
139 | (setq list (cdr list))) | |
140 | (when (cdr list) | |
141 | (setcdr list (cddr list))) | |
142 | total))) | |
143 | ||
144 | ;; Delete the current line (and the next N lines). | |
145 | (defmacro gnus-delete-line (&optional n) | |
01c52d31 | 146 | `(delete-region (point-at-bol) |
eec82323 LMI |
147 | (progn (forward-line ,(or n 1)) (point)))) |
148 | ||
149 | (defun gnus-byte-code (func) | |
150 | "Return a form that can be `eval'ed based on FUNC." | |
6748645f LMI |
151 | (let ((fval (indirect-function func))) |
152 | (if (byte-code-function-p fval) | |
eec82323 LMI |
153 | (let ((flist (append fval nil))) |
154 | (setcar flist 'byte-code) | |
155 | flist) | |
156 | (cons 'progn (cddr fval))))) | |
157 | ||
158 | (defun gnus-extract-address-components (from) | |
23f87bed MB |
159 | "Extract address components from a From header. |
160 | Given an RFC-822 address FROM, extract full name and canonical address. | |
161 | Returns a list of the form (FULL-NAME CANONICAL-ADDRESS). Much more simple | |
162 | solution than `mail-extract-address-components', which works much better, but | |
163 | is slower." | |
eec82323 LMI |
164 | (let (name address) |
165 | ;; First find the address - the thing with the @ in it. This may | |
166 | ;; not be accurate in mail addresses, but does the trick most of | |
167 | ;; the time in news messages. | |
4573e0df MB |
168 | (cond (;; Check ``<foo@bar>'' first in order to handle the quite common |
169 | ;; form ``"abc@xyz" <foo@bar>'' (i.e. ``@'' as part of a comment) | |
170 | ;; correctly. | |
171 | (string-match "<\\([^@ \t<>]+[!@][^@ \t<>]+\\)>" from) | |
172 | (setq address (substring from (match-beginning 1) (match-end 1)))) | |
173 | ((string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from) | |
174 | (setq address (substring from (match-beginning 0) (match-end 0))))) | |
eec82323 LMI |
175 | ;; Then we check whether the "name <address>" format is used. |
176 | (and address | |
eec82323 LMI |
177 | ;; Linear white space is not required. |
178 | (string-match (concat "[ \t]*<" (regexp-quote address) ">") from) | |
179 | (and (setq name (substring from 0 (match-beginning 0))) | |
180 | ;; Strip any quotes from the name. | |
23f87bed | 181 | (string-match "^\".*\"$" name) |
eec82323 LMI |
182 | (setq name (substring name 1 (1- (match-end 0)))))) |
183 | ;; If not, then "address (name)" is used. | |
184 | (or name | |
185 | (and (string-match "(.+)" from) | |
186 | (setq name (substring from (1+ (match-beginning 0)) | |
187 | (1- (match-end 0))))) | |
188 | (and (string-match "()" from) | |
189 | (setq name address)) | |
eec82323 LMI |
190 | ;; XOVER might not support folded From headers. |
191 | (and (string-match "(.*" from) | |
192 | (setq name (substring from (1+ (match-beginning 0)) | |
193 | (match-end 0))))) | |
16409b0b GM |
194 | (list (if (string= name "") nil name) (or address from)))) |
195 | ||
0ab5c2be MB |
196 | (defun gnus-extract-address-component-name (from) |
197 | "Extract name from a From header. | |
198 | Uses `gnus-extract-address-components'." | |
199 | (nth 0 (gnus-extract-address-components from))) | |
200 | ||
201 | (defun gnus-extract-address-component-email (from) | |
202 | "Extract e-mail address from a From header. | |
203 | Uses `gnus-extract-address-components'." | |
204 | (nth 1 (gnus-extract-address-components from))) | |
eec82323 LMI |
205 | |
206 | (defun gnus-fetch-field (field) | |
207 | "Return the value of the header FIELD of current article." | |
208 | (save-excursion | |
209 | (save-restriction | |
01c52d31 | 210 | (let ((inhibit-point-motion-hooks t)) |
eec82323 LMI |
211 | (nnheader-narrow-to-headers) |
212 | (message-fetch-field field))))) | |
213 | ||
23f87bed MB |
214 | (defun gnus-fetch-original-field (field) |
215 | "Fetch FIELD from the original version of the current article." | |
216 | (with-current-buffer gnus-original-article-buffer | |
217 | (gnus-fetch-field field))) | |
218 | ||
219 | ||
eec82323 LMI |
220 | (defun gnus-goto-colon () |
221 | (beginning-of-line) | |
01c52d31 | 222 | (let ((eol (point-at-eol))) |
23f87bed MB |
223 | (goto-char (or (text-property-any (point) eol 'gnus-position t) |
224 | (search-forward ":" eol t) | |
225 | (point))))) | |
226 | ||
5ec7fe1b GM |
227 | (declare-function gnus-find-method-for-group "gnus" (group &optional info)) |
228 | (autoload 'gnus-group-name-decode "gnus-group") | |
229 | (declare-function gnus-group-name-charset "gnus-group" (method group)) | |
230 | ;; gnus-group requires gnus-int which requires message. | |
231 | (declare-function message-tokenize-header "message" | |
232 | (header &optional separator)) | |
233 | ||
23f87bed MB |
234 | (defun gnus-decode-newsgroups (newsgroups group &optional method) |
235 | (let ((method (or method (gnus-find-method-for-group group)))) | |
236 | (mapconcat (lambda (group) | |
237 | (gnus-group-name-decode group (gnus-group-name-charset | |
238 | method group))) | |
239 | (message-tokenize-header newsgroups) | |
240 | ","))) | |
eec82323 LMI |
241 | |
242 | (defun gnus-remove-text-with-property (prop) | |
243 | "Delete all text in the current buffer with text property PROP." | |
01c52d31 MB |
244 | (let ((start (point-min)) |
245 | end) | |
246 | (unless (get-text-property start prop) | |
247 | (setq start (next-single-property-change start prop))) | |
248 | (while start | |
249 | (setq end (text-property-any start (point-max) prop nil)) | |
250 | (delete-region start (or end (point-max))) | |
251 | (setq start (when end | |
252 | (next-single-property-change start prop)))))) | |
eec82323 LMI |
253 | |
254 | (defun gnus-newsgroup-directory-form (newsgroup) | |
255 | "Make hierarchical directory name from NEWSGROUP name." | |
23f87bed MB |
256 | (let* ((newsgroup (gnus-newsgroup-savable-name newsgroup)) |
257 | (idx (string-match ":" newsgroup))) | |
258 | (concat | |
259 | (if idx (substring newsgroup 0 idx)) | |
260 | (if idx "/") | |
261 | (nnheader-replace-chars-in-string | |
262 | (if idx (substring newsgroup (1+ idx)) newsgroup) | |
263 | ?. ?/)))) | |
eec82323 LMI |
264 | |
265 | (defun gnus-newsgroup-savable-name (group) | |
266 | ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group) | |
267 | ;; with dots. | |
268 | (nnheader-replace-chars-in-string group ?/ ?.)) | |
269 | ||
270 | (defun gnus-string> (s1 s2) | |
271 | (not (or (string< s1 s2) | |
272 | (string= s1 s2)))) | |
273 | ||
b4fde39f MB |
274 | (defun gnus-string< (s1 s2) |
275 | "Return t if first arg string is less than second in lexicographic order. | |
276 | Case is significant if and only if `case-fold-search' is nil. | |
277 | Symbols are also allowed; their print names are used instead." | |
278 | (if case-fold-search | |
279 | (string-lessp (downcase (if (symbolp s1) (symbol-name s1) s1)) | |
280 | (downcase (if (symbolp s2) (symbol-name s2) s2))) | |
281 | (string-lessp s1 s2))) | |
282 | ||
eec82323 LMI |
283 | ;;; Time functions. |
284 | ||
eec82323 LMI |
285 | (defun gnus-file-newer-than (file date) |
286 | (let ((fdate (nth 5 (file-attributes file)))) | |
287 | (or (> (car fdate) (car date)) | |
288 | (and (= (car fdate) (car date)) | |
289 | (> (nth 1 fdate) (nth 1 date)))))) | |
290 | ||
291 | ;;; Keymap macros. | |
292 | ||
293 | (defmacro gnus-local-set-keys (&rest plist) | |
294 | "Set the keys in PLIST in the current keymap." | |
295 | `(gnus-define-keys-1 (current-local-map) ',plist)) | |
296 | ||
297 | (defmacro gnus-define-keys (keymap &rest plist) | |
298 | "Define all keys in PLIST in KEYMAP." | |
299 | `(gnus-define-keys-1 (quote ,keymap) (quote ,plist))) | |
300 | ||
301 | (defmacro gnus-define-keys-safe (keymap &rest plist) | |
302 | "Define all keys in PLIST in KEYMAP without overwriting previous definitions." | |
303 | `(gnus-define-keys-1 (quote ,keymap) (quote ,plist) t)) | |
304 | ||
305 | (put 'gnus-define-keys 'lisp-indent-function 1) | |
306 | (put 'gnus-define-keys-safe 'lisp-indent-function 1) | |
307 | (put 'gnus-local-set-keys 'lisp-indent-function 1) | |
308 | ||
309 | (defmacro gnus-define-keymap (keymap &rest plist) | |
310 | "Define all keys in PLIST in KEYMAP." | |
311 | `(gnus-define-keys-1 ,keymap (quote ,plist))) | |
312 | ||
313 | (put 'gnus-define-keymap 'lisp-indent-function 1) | |
314 | ||
315 | (defun gnus-define-keys-1 (keymap plist &optional safe) | |
316 | (when (null keymap) | |
317 | (error "Can't set keys in a null keymap")) | |
318 | (cond ((symbolp keymap) | |
319 | (setq keymap (symbol-value keymap))) | |
320 | ((keymapp keymap)) | |
321 | ((listp keymap) | |
322 | (set (car keymap) nil) | |
323 | (define-prefix-command (car keymap)) | |
324 | (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap)) | |
325 | (setq keymap (symbol-value (car keymap))))) | |
326 | (let (key) | |
327 | (while plist | |
328 | (when (symbolp (setq key (pop plist))) | |
329 | (setq key (symbol-value key))) | |
330 | (if (or (not safe) | |
331 | (eq (lookup-key keymap key) 'undefined)) | |
332 | (define-key keymap key (pop plist)) | |
333 | (pop plist))))) | |
334 | ||
23f87bed | 335 | (defun gnus-completing-read-with-default (default prompt &rest args) |
eec82323 LMI |
336 | ;; Like `completing-read', except that DEFAULT is the default argument. |
337 | (let* ((prompt (if default | |
81df110a RF |
338 | (concat prompt " (default " default "): ") |
339 | (concat prompt ": "))) | |
eec82323 LMI |
340 | (answer (apply 'completing-read prompt args))) |
341 | (if (or (null answer) (zerop (length answer))) | |
342 | default | |
343 | answer))) | |
344 | ||
345 | ;; Two silly functions to ensure that all `y-or-n-p' questions clear | |
346 | ;; the echo area. | |
bbbe940b | 347 | ;; |
52bec650 MB |
348 | ;; Do we really need these functions? Workarounds for bugs in the corresponding |
349 | ;; Emacs functions? Maybe these bugs are no longer present in any supported | |
bbbe940b | 350 | ;; (X)Emacs version? Alias them to the original functions and see if anyone |
52bec650 MB |
351 | ;; reports a problem. If not, replace with original functions. --rsteib, |
352 | ;; 2007-12-14 | |
bbbe940b | 353 | ;; |
52bec650 MB |
354 | ;; All supported Emacsen clear the echo area after `yes-or-no-p', so we can |
355 | ;; remove `yes-or-no-p'. RMS says that not clearing after `y-or-n-p' is | |
356 | ;; intentional (see below), so we could remove `gnus-y-or-n-p' too. | |
357 | ;; Objections? --rsteib, 2008-02-16 | |
358 | ;; | |
359 | ;; ,----[ http://thread.gmane.org/gmane.emacs.gnus.general/65099/focus=66070 ] | |
360 | ;; | From: Richard Stallman | |
361 | ;; | Subject: Re: Do we need gnus-yes-or-no-p and gnus-y-or-n-p? | |
362 | ;; | To: Katsumi Yamaoka [...] | |
363 | ;; | Cc: emacs-devel@[...], xemacs-beta@[...], ding@[...] | |
364 | ;; | Date: Mon, 07 Jan 2008 12:16:05 -0500 | |
365 | ;; | Message-ID: <E1JBva1-000528-VY@fencepost.gnu.org> | |
366 | ;; | | |
367 | ;; | The behavior of `y-or-n-p' that it doesn't clear the question | |
368 | ;; | and the answer is not serious of course, but I feel it is not | |
369 | ;; | cool. | |
370 | ;; | | |
371 | ;; | It is intentional. | |
372 | ;; | | |
373 | ;; | Currently, it is commented out in the trunk by Reiner Steib. He | |
374 | ;; | also wrote the benefit of leaving the question and the answer in | |
375 | ;; | the echo area as follows: | |
376 | ;; | | |
377 | ;; | (http://article.gmane.org/gmane.emacs.gnus.general/66061) | |
378 | ;; | > In contrast to yes-or-no-p it is much easier to type y, n, | |
379 | ;; | > SPC, DEL, etc accidentally, so it might be useful for the user | |
380 | ;; | > to see what he has typed. | |
381 | ;; | | |
382 | ;; | Yes, that is the reason. | |
383 | ;; `---- | |
384 | ||
bbbe940b MB |
385 | ;; (defun gnus-y-or-n-p (prompt) |
386 | ;; (prog1 | |
387 | ;; (y-or-n-p prompt) | |
388 | ;; (message ""))) | |
389 | ;; (defun gnus-yes-or-no-p (prompt) | |
390 | ;; (prog1 | |
391 | ;; (yes-or-no-p prompt) | |
392 | ;; (message ""))) | |
393 | ||
394 | (defalias 'gnus-y-or-n-p 'y-or-n-p) | |
395 | (defalias 'gnus-yes-or-no-p 'yes-or-no-p) | |
eec82323 | 396 | |
23f87bed MB |
397 | ;; By Frank Schmitt <ich@Frank-Schmitt.net>. Allows to have |
398 | ;; age-depending date representations. (e.g. just the time if it's | |
399 | ;; from today, the day of the week if it's within the last 7 days and | |
400 | ;; the full date if it's older) | |
401 | ||
402 | (defun gnus-seconds-today () | |
403 | "Return the number of seconds passed today." | |
404 | (let ((now (decode-time (current-time)))) | |
405 | (+ (car now) (* (car (cdr now)) 60) (* (car (nthcdr 2 now)) 3600)))) | |
406 | ||
407 | (defun gnus-seconds-month () | |
408 | "Return the number of seconds passed this month." | |
409 | (let ((now (decode-time (current-time)))) | |
410 | (+ (car now) (* (car (cdr now)) 60) (* (car (nthcdr 2 now)) 3600) | |
411 | (* (- (car (nthcdr 3 now)) 1) 3600 24)))) | |
412 | ||
413 | (defun gnus-seconds-year () | |
414 | "Return the number of seconds passed this year." | |
415 | (let ((now (decode-time (current-time))) | |
416 | (days (format-time-string "%j" (current-time)))) | |
417 | (+ (car now) (* (car (cdr now)) 60) (* (car (nthcdr 2 now)) 3600) | |
418 | (* (- (string-to-number days) 1) 3600 24)))) | |
419 | ||
420 | (defvar gnus-user-date-format-alist | |
421 | '(((gnus-seconds-today) . "%k:%M") | |
422 | (604800 . "%a %k:%M") ;;that's one week | |
423 | ((gnus-seconds-month) . "%a %d") | |
424 | ((gnus-seconds-year) . "%b %d") | |
425 | (t . "%b %d '%y")) ;;this one is used when no | |
426 | ;;other does match | |
427 | "Specifies date format depending on age of article. | |
428 | This is an alist of items (AGE . FORMAT). AGE can be a number (of | |
429 | seconds) or a Lisp expression evaluating to a number. When the age of | |
430 | the article is less than this number, then use `format-time-string' | |
431 | with the corresponding FORMAT for displaying the date of the article. | |
432 | If AGE is not a number or a Lisp expression evaluating to a | |
433 | non-number, then the corresponding FORMAT is used as a default value. | |
434 | ||
435 | Note that the list is processed from the beginning, so it should be | |
436 | sorted by ascending AGE. Also note that items following the first | |
437 | non-number AGE will be ignored. | |
438 | ||
439 | You can use the functions `gnus-seconds-today', `gnus-seconds-month' | |
440 | and `gnus-seconds-year' in the AGE spec. They return the number of | |
441 | seconds passed since the start of today, of this month, of this year, | |
442 | respectively.") | |
443 | ||
444 | (defun gnus-user-date (messy-date) | |
445 | "Format the messy-date according to gnus-user-date-format-alist. | |
446 | Returns \" ? \" if there's bad input or if an other error occurs. | |
447 | Input should look like this: \"Sun, 14 Oct 2001 13:34:39 +0200\"." | |
448 | (condition-case () | |
449 | (let* ((messy-date (time-to-seconds (safe-date-to-time messy-date))) | |
450 | (now (time-to-seconds (current-time))) | |
451 | ;;If we don't find something suitable we'll use this one | |
452 | (my-format "%b %d '%y")) | |
453 | (let* ((difference (- now messy-date)) | |
454 | (templist gnus-user-date-format-alist) | |
455 | (top (eval (caar templist)))) | |
456 | (while (if (numberp top) (< top difference) (not top)) | |
457 | (progn | |
458 | (setq templist (cdr templist)) | |
459 | (setq top (eval (caar templist))))) | |
460 | (if (stringp (cdr (car templist))) | |
461 | (setq my-format (cdr (car templist))))) | |
462 | (format-time-string (eval my-format) (seconds-to-time messy-date))) | |
463 | (error " ? "))) | |
464 | ||
eec82323 | 465 | (defun gnus-dd-mmm (messy-date) |
6748645f | 466 | "Return a string like DD-MMM from a big messy string." |
16409b0b GM |
467 | (condition-case () |
468 | (format-time-string "%d-%b" (safe-date-to-time messy-date)) | |
469 | (error " - "))) | |
eec82323 LMI |
470 | |
471 | (defmacro gnus-date-get-time (date) | |
472 | "Convert DATE string to Emacs time. | |
473 | Cache the result as a text property stored in DATE." | |
474 | ;; Either return the cached value... | |
475 | `(let ((d ,date)) | |
476 | (if (equal "" d) | |
477 | '(0 0) | |
478 | (or (get-text-property 0 'gnus-time d) | |
479 | ;; or compute the value... | |
16409b0b | 480 | (let ((time (safe-date-to-time d))) |
eec82323 LMI |
481 | ;; and store it back in the string. |
482 | (put-text-property 0 1 'gnus-time time d) | |
483 | time))))) | |
484 | ||
485 | (defsubst gnus-time-iso8601 (time) | |
8b93df01 | 486 | "Return a string of TIME in YYYYMMDDTHHMMSS format." |
eec82323 LMI |
487 | (format-time-string "%Y%m%dT%H%M%S" time)) |
488 | ||
6748645f | 489 | (defun gnus-date-iso8601 (date) |
8b93df01 | 490 | "Convert the DATE to YYYYMMDDTHHMMSS." |
eec82323 | 491 | (condition-case () |
6748645f | 492 | (gnus-time-iso8601 (gnus-date-get-time date)) |
eec82323 LMI |
493 | (error ""))) |
494 | ||
495 | (defun gnus-mode-string-quote (string) | |
496 | "Quote all \"%\"'s in STRING." | |
23f87bed | 497 | (gnus-replace-in-string string "%" "%%")) |
eec82323 LMI |
498 | |
499 | ;; Make a hash table (default and minimum size is 256). | |
500 | ;; Optional argument HASHSIZE specifies the table size. | |
501 | (defun gnus-make-hashtable (&optional hashsize) | |
502 | (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 256) 256) 0)) | |
503 | ||
504 | ;; Make a number that is suitable for hashing; bigger than MIN and | |
505 | ;; equal to some 2^x. Many machines (such as sparcs) do not have a | |
506 | ;; hardware modulo operation, so they implement it in software. On | |
507 | ;; many sparcs over 50% of the time to intern is spent in the modulo. | |
508 | ;; Yes, it's slower than actually computing the hash from the string! | |
509 | ;; So we use powers of 2 so people can optimize the modulo to a mask. | |
510 | (defun gnus-create-hash-size (min) | |
511 | (let ((i 1)) | |
512 | (while (< i min) | |
513 | (setq i (* 2 i))) | |
514 | i)) | |
515 | ||
516 | (defcustom gnus-verbose 7 | |
517 | "*Integer that says how verbose Gnus should be. | |
518 | The higher the number, the more messages Gnus will flash to say what | |
519 | it's doing. At zero, Gnus will be totally mute; at five, Gnus will | |
520 | display most important messages; and at ten, Gnus will keep on | |
521 | jabbering all the time." | |
522 | :group 'gnus-start | |
523 | :type 'integer) | |
524 | ||
01c52d31 MB |
525 | (defcustom gnus-add-timestamp-to-message nil |
526 | "Non-nil means add timestamps to messages that Gnus issues. | |
527 | If it is `log', add timestamps to only the messages that go into the | |
528 | \"*Messages*\" buffer (in XEmacs, it is the \" *Message-Log*\" buffer). | |
529 | If it is neither nil nor `log', add timestamps not only to log messages | |
530 | but also to the ones displayed in the echo area." | |
330f707b | 531 | :version "23.1" ;; No Gnus |
01c52d31 MB |
532 | :group 'gnus-various |
533 | :type '(choice :format "%{%t%}:\n %[Value Menu%] %v" | |
534 | (const :tag "Logged messages only" log) | |
535 | (sexp :tag "All messages" | |
536 | :match (lambda (widget value) value) | |
537 | :value t) | |
538 | (const :tag "No timestamp" nil))) | |
539 | ||
540 | (eval-when-compile | |
541 | (defmacro gnus-message-with-timestamp-1 (format-string args) | |
542 | (let ((timestamp '((format-time-string "%Y%m%dT%H%M%S" time) | |
543 | "." (format "%03d" (/ (nth 2 time) 1000)) "> "))) | |
544 | (if (featurep 'xemacs) | |
545 | `(let (str time) | |
546 | (if (or (and (null ,format-string) (null ,args)) | |
547 | (progn | |
548 | (setq str (apply 'format ,format-string ,args)) | |
549 | (zerop (length str)))) | |
550 | (prog1 | |
551 | (and ,format-string str) | |
552 | (clear-message nil)) | |
553 | (cond ((eq gnus-add-timestamp-to-message 'log) | |
554 | (setq time (current-time)) | |
555 | (display-message 'no-log str) | |
556 | (log-message 'message (concat ,@timestamp str))) | |
557 | (gnus-add-timestamp-to-message | |
558 | (setq time (current-time)) | |
559 | (display-message 'message (concat ,@timestamp str))) | |
560 | (t | |
561 | (display-message 'message str)))) | |
562 | str) | |
563 | `(let (str time) | |
564 | (cond ((eq gnus-add-timestamp-to-message 'log) | |
565 | (setq str (let (message-log-max) | |
566 | (apply 'message ,format-string ,args))) | |
567 | (when (and message-log-max | |
568 | (> message-log-max 0) | |
569 | (/= (length str) 0)) | |
570 | (setq time (current-time)) | |
571 | (with-current-buffer (get-buffer-create "*Messages*") | |
572 | (goto-char (point-max)) | |
573 | (insert ,@timestamp str "\n") | |
574 | (forward-line (- message-log-max)) | |
575 | (delete-region (point-min) (point)) | |
576 | (goto-char (point-max)))) | |
577 | str) | |
578 | (gnus-add-timestamp-to-message | |
579 | (if (or (and (null ,format-string) (null ,args)) | |
580 | (progn | |
581 | (setq str (apply 'format ,format-string ,args)) | |
582 | (zerop (length str)))) | |
583 | (prog1 | |
584 | (and ,format-string str) | |
585 | (message nil)) | |
586 | (setq time (current-time)) | |
587 | (message "%s" (concat ,@timestamp str)) | |
588 | str)) | |
589 | (t | |
590 | (apply 'message ,format-string ,args)))))))) | |
591 | ||
592 | (defun gnus-message-with-timestamp (format-string &rest args) | |
593 | "Display message with timestamp. Arguments are the same as `message'. | |
594 | The `gnus-add-timestamp-to-message' variable controls how to add | |
595 | timestamp to message." | |
596 | (gnus-message-with-timestamp-1 format-string args)) | |
597 | ||
eec82323 | 598 | (defun gnus-message (level &rest args) |
23f87bed MB |
599 | "If LEVEL is lower than `gnus-verbose' print ARGS using `message'. |
600 | ||
601 | Guideline for numbers: | |
602 | 1 - error messages, 3 - non-serious error messages, 5 - messages for things | |
603 | that take a long time, 7 - not very important messages on stuff, 9 - messages | |
604 | inside loops." | |
eec82323 | 605 | (if (<= level gnus-verbose) |
01c52d31 MB |
606 | (if gnus-add-timestamp-to-message |
607 | (apply 'gnus-message-with-timestamp args) | |
608 | (apply 'message args)) | |
eec82323 LMI |
609 | ;; We have to do this format thingy here even if the result isn't |
610 | ;; shown - the return value has to be the same as the return value | |
611 | ;; from `message'. | |
612 | (apply 'format args))) | |
613 | ||
614 | (defun gnus-error (level &rest args) | |
6203370b MB |
615 | "Beep an error if LEVEL is equal to or less than `gnus-verbose'. |
616 | ARGS are passed to `message'." | |
eec82323 LMI |
617 | (when (<= (floor level) gnus-verbose) |
618 | (apply 'message args) | |
619 | (ding) | |
620 | (let (duration) | |
621 | (when (and (floatp level) | |
622 | (not (zerop (setq duration (* 10 (- level (floor level))))))) | |
623 | (sit-for duration)))) | |
624 | nil) | |
625 | ||
626 | (defun gnus-split-references (references) | |
627 | "Return a list of Message-IDs in REFERENCES." | |
628 | (let ((beg 0) | |
521c4a23 | 629 | (references (mail-header-remove-comments (or references ""))) |
eec82323 | 630 | ids) |
23f87bed | 631 | (while (string-match "<[^<]+[^< \t]" references beg) |
eec82323 LMI |
632 | (push (substring references (match-beginning 0) (setq beg (match-end 0))) |
633 | ids)) | |
634 | (nreverse ids))) | |
635 | ||
01c52d31 MB |
636 | (defun gnus-extract-references (references) |
637 | "Return a list of Message-IDs in REFERENCES (in In-Reply-To | |
638 | format), trimmed to only contain the Message-IDs." | |
639 | (let ((ids (gnus-split-references references)) | |
640 | refs) | |
641 | (dolist (id ids) | |
642 | (when (string-match "<[^<>]+>" id) | |
643 | (push (match-string 0 id) refs))) | |
644 | refs)) | |
645 | ||
16409b0b | 646 | (defsubst gnus-parent-id (references &optional n) |
eec82323 LMI |
647 | "Return the last Message-ID in REFERENCES. |
648 | If N, return the Nth ancestor instead." | |
23f87bed MB |
649 | (when (and references |
650 | (not (zerop (length references)))) | |
651 | (if n | |
652 | (let ((ids (inline (gnus-split-references references)))) | |
653 | (while (nthcdr n ids) | |
654 | (setq ids (cdr ids))) | |
655 | (car ids)) | |
521c4a23 AS |
656 | (let ((references (mail-header-remove-comments references))) |
657 | (when (string-match "\\(<[^<]+>\\)[ \t]*\\'" references) | |
658 | (match-string 1 references)))))) | |
23f87bed MB |
659 | |
660 | (defun gnus-buffer-live-p (buffer) | |
eec82323 LMI |
661 | "Say whether BUFFER is alive or not." |
662 | (and buffer | |
663 | (get-buffer buffer) | |
664 | (buffer-name (get-buffer buffer)))) | |
665 | ||
666 | (defun gnus-horizontal-recenter () | |
667 | "Recenter the current buffer horizontally." | |
668 | (if (< (current-column) (/ (window-width) 2)) | |
23f87bed | 669 | (set-window-hscroll (gnus-get-buffer-window (current-buffer) t) 0) |
eec82323 | 670 | (let* ((orig (point)) |
23f87bed | 671 | (end (window-end (gnus-get-buffer-window (current-buffer) t))) |
eec82323 | 672 | (max 0)) |
6748645f LMI |
673 | (when end |
674 | ;; Find the longest line currently displayed in the window. | |
675 | (goto-char (window-start)) | |
676 | (while (and (not (eobp)) | |
677 | (< (point) end)) | |
678 | (end-of-line) | |
679 | (setq max (max max (current-column))) | |
680 | (forward-line 1)) | |
681 | (goto-char orig) | |
682 | ;; Scroll horizontally to center (sort of) the point. | |
683 | (if (> max (window-width)) | |
684 | (set-window-hscroll | |
23f87bed | 685 | (gnus-get-buffer-window (current-buffer) t) |
6748645f LMI |
686 | (min (- (current-column) (/ (window-width) 3)) |
687 | (+ 2 (- max (window-width))))) | |
23f87bed | 688 | (set-window-hscroll (gnus-get-buffer-window (current-buffer) t) 0)) |
6748645f | 689 | max)))) |
eec82323 | 690 | |
23f87bed | 691 | (defun gnus-read-event-char (&optional prompt) |
eec82323 | 692 | "Get the next event." |
23f87bed | 693 | (let ((event (read-event prompt))) |
eec82323 LMI |
694 | ;; should be gnus-characterp, but this can't be called in XEmacs anyway |
695 | (cons (and (numberp event) event) event))) | |
696 | ||
697 | (defun gnus-sortable-date (date) | |
16409b0b GM |
698 | "Make string suitable for sorting from DATE." |
699 | (gnus-time-iso8601 (date-to-time date))) | |
eec82323 LMI |
700 | |
701 | (defun gnus-copy-file (file &optional to) | |
702 | "Copy FILE to TO." | |
703 | (interactive | |
704 | (list (read-file-name "Copy file: " default-directory) | |
705 | (read-file-name "Copy file to: " default-directory))) | |
706 | (unless to | |
707 | (setq to (read-file-name "Copy file to: " default-directory))) | |
708 | (when (file-directory-p to) | |
709 | (setq to (concat (file-name-as-directory to) | |
710 | (file-name-nondirectory file)))) | |
711 | (copy-file file to)) | |
712 | ||
eec82323 LMI |
713 | (defvar gnus-work-buffer " *gnus work*") |
714 | ||
5ec7fe1b GM |
715 | (declare-function gnus-get-buffer-create "gnus" (name)) |
716 | ;; gnus.el requires mm-util. | |
717 | (declare-function mm-enable-multibyte "mm-util") | |
718 | ||
eec82323 LMI |
719 | (defun gnus-set-work-buffer () |
720 | "Put point in the empty Gnus work buffer." | |
721 | (if (get-buffer gnus-work-buffer) | |
722 | (progn | |
723 | (set-buffer gnus-work-buffer) | |
724 | (erase-buffer)) | |
6748645f | 725 | (set-buffer (gnus-get-buffer-create gnus-work-buffer)) |
eec82323 | 726 | (kill-all-local-variables) |
16409b0b | 727 | (mm-enable-multibyte))) |
eec82323 LMI |
728 | |
729 | (defmacro gnus-group-real-name (group) | |
730 | "Find the real name of a foreign newsgroup." | |
731 | `(let ((gname ,group)) | |
732 | (if (string-match "^[^:]+:" gname) | |
733 | (substring gname (match-end 0)) | |
734 | gname))) | |
735 | ||
6c5d6b6c MB |
736 | (defmacro gnus-group-server (group) |
737 | "Find the server name of a foreign newsgroup. | |
738 | For example, (gnus-group-server \"nnimap+yxa:INBOX.foo\") would | |
739 | yield \"nnimap:yxa\"." | |
740 | `(let ((gname ,group)) | |
741 | (if (string-match "^\\([^:+]+\\)\\(?:\\+\\([^:]*\\)\\)?:" gname) | |
742 | (format "%s:%s" (match-string 1 gname) (or | |
743 | (match-string 2 gname) | |
744 | "")) | |
745 | (format "%s:%s" (car gnus-select-method) (cadr gnus-select-method))))) | |
746 | ||
eec82323 | 747 | (defun gnus-make-sort-function (funs) |
23f87bed | 748 | "Return a composite sort condition based on the functions in FUNS." |
eec82323 | 749 | (cond |
16409b0b | 750 | ;; Just a simple function. |
23f87bed | 751 | ((functionp funs) funs) |
16409b0b | 752 | ;; No functions at all. |
eec82323 | 753 | ((null funs) funs) |
16409b0b GM |
754 | ;; A list of functions. |
755 | ((or (cdr funs) | |
756 | (listp (car funs))) | |
23f87bed MB |
757 | (gnus-byte-compile |
758 | `(lambda (t1 t2) | |
759 | ,(gnus-make-sort-function-1 (reverse funs))))) | |
16409b0b | 760 | ;; A list containing just one function. |
eec82323 LMI |
761 | (t |
762 | (car funs)))) | |
763 | ||
764 | (defun gnus-make-sort-function-1 (funs) | |
23f87bed | 765 | "Return a composite sort condition based on the functions in FUNS." |
16409b0b GM |
766 | (let ((function (car funs)) |
767 | (first 't1) | |
768 | (last 't2)) | |
769 | (when (consp function) | |
770 | (cond | |
771 | ;; Reversed spec. | |
772 | ((eq (car function) 'not) | |
773 | (setq function (cadr function) | |
774 | first 't2 | |
775 | last 't1)) | |
23f87bed | 776 | ((functionp function) |
16409b0b GM |
777 | ;; Do nothing. |
778 | ) | |
779 | (t | |
780 | (error "Invalid sort spec: %s" function)))) | |
781 | (if (cdr funs) | |
782 | `(or (,function ,first ,last) | |
783 | (and (not (,function ,last ,first)) | |
784 | ,(gnus-make-sort-function-1 (cdr funs)))) | |
785 | `(,function ,first ,last)))) | |
eec82323 LMI |
786 | |
787 | (defun gnus-turn-off-edit-menu (type) | |
788 | "Turn off edit menu in `gnus-TYPE-mode-map'." | |
789 | (define-key (symbol-value (intern (format "gnus-%s-mode-map" type))) | |
790 | [menu-bar edit] 'undefined)) | |
791 | ||
23f87bed MB |
792 | (defmacro gnus-bind-print-variables (&rest forms) |
793 | "Bind print-* variables and evaluate FORMS. | |
794 | This macro is used with `prin1', `pp', etc. in order to ensure printed | |
795 | Lisp objects are loadable. Bind `print-quoted' and `print-readably' | |
796 | to t, and `print-escape-multibyte', `print-escape-newlines', | |
797 | `print-escape-nonascii', `print-length', `print-level' and | |
798 | `print-string-length' to nil." | |
799 | `(let ((print-quoted t) | |
800 | (print-readably t) | |
801 | ;;print-circle | |
802 | ;;print-continuous-numbering | |
803 | print-escape-multibyte | |
804 | print-escape-newlines | |
805 | print-escape-nonascii | |
806 | ;;print-gensym | |
807 | print-length | |
808 | print-level | |
809 | print-string-length) | |
810 | ,@forms)) | |
811 | ||
eec82323 LMI |
812 | (defun gnus-prin1 (form) |
813 | "Use `prin1' on FORM in the current buffer. | |
23f87bed MB |
814 | Bind `print-quoted' and `print-readably' to t, and `print-length' and |
815 | `print-level' to nil. See also `gnus-bind-print-variables'." | |
816 | (gnus-bind-print-variables (prin1 form (current-buffer)))) | |
eec82323 LMI |
817 | |
818 | (defun gnus-prin1-to-string (form) | |
23f87bed MB |
819 | "The same as `prin1'. |
820 | Bind `print-quoted' and `print-readably' to t, and `print-length' and | |
821 | `print-level' to nil. See also `gnus-bind-print-variables'." | |
822 | (gnus-bind-print-variables (prin1-to-string form))) | |
823 | ||
01c52d31 | 824 | (defun gnus-pp (form &optional stream) |
23f87bed MB |
825 | "Use `pp' on FORM in the current buffer. |
826 | Bind `print-quoted' and `print-readably' to t, and `print-length' and | |
827 | `print-level' to nil. See also `gnus-bind-print-variables'." | |
01c52d31 | 828 | (gnus-bind-print-variables (pp form (or stream (current-buffer))))) |
23f87bed MB |
829 | |
830 | (defun gnus-pp-to-string (form) | |
831 | "The same as `pp-to-string'. | |
832 | Bind `print-quoted' and `print-readably' to t, and `print-length' and | |
833 | `print-level' to nil. See also `gnus-bind-print-variables'." | |
834 | (gnus-bind-print-variables (pp-to-string form))) | |
eec82323 LMI |
835 | |
836 | (defun gnus-make-directory (directory) | |
837 | "Make DIRECTORY (and all its parents) if it doesn't exist." | |
5eee36fa | 838 | (require 'nnmail) |
16409b0b GM |
839 | (let ((file-name-coding-system nnmail-pathname-coding-system)) |
840 | (when (and directory | |
841 | (not (file-exists-p directory))) | |
842 | (make-directory directory t))) | |
eec82323 LMI |
843 | t) |
844 | ||
845 | (defun gnus-write-buffer (file) | |
846 | "Write the current buffer's contents to FILE." | |
16409b0b | 847 | (let ((file-name-coding-system nnmail-pathname-coding-system)) |
01c52d31 MB |
848 | ;; Make sure the directory exists. |
849 | (gnus-make-directory (file-name-directory file)) | |
16409b0b GM |
850 | ;; Write the buffer. |
851 | (write-region (point-min) (point-max) file nil 'quietly))) | |
eec82323 | 852 | |
eec82323 LMI |
853 | (defun gnus-delete-file (file) |
854 | "Delete FILE if it exists." | |
855 | (when (file-exists-p file) | |
856 | (delete-file file))) | |
857 | ||
aa0a8561 MB |
858 | (defun gnus-delete-directory (directory) |
859 | "Delete files in DIRECTORY. Subdirectories remain. | |
860 | If there's no subdirectory, delete DIRECTORY as well." | |
861 | (when (file-directory-p directory) | |
862 | (let ((files (directory-files | |
863 | directory t "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")) | |
864 | file dir) | |
865 | (while files | |
866 | (setq file (pop files)) | |
867 | (if (eq t (car (file-attributes file))) | |
868 | ;; `file' is a subdirectory. | |
869 | (setq dir t) | |
870 | ;; `file' is a file or a symlink. | |
871 | (delete-file file))) | |
872 | (unless dir | |
873 | (delete-directory directory))))) | |
874 | ||
996aa8c1 MB |
875 | ;; The following two functions are used in gnus-registry. |
876 | ;; They were contributed by Andreas Fuchs <asf@void.at>. | |
877 | (defun gnus-alist-to-hashtable (alist) | |
878 | "Build a hashtable from the values in ALIST." | |
879 | (let ((ht (make-hash-table | |
880 | :size 4096 | |
881 | :test 'equal))) | |
882 | (mapc | |
883 | (lambda (kv-pair) | |
884 | (puthash (car kv-pair) (cdr kv-pair) ht)) | |
885 | alist) | |
886 | ht)) | |
887 | ||
888 | (defun gnus-hashtable-to-alist (hash) | |
889 | "Build an alist from the values in HASH." | |
890 | (let ((list nil)) | |
891 | (maphash | |
892 | (lambda (key value) | |
893 | (setq list (cons (cons key value) list))) | |
894 | hash) | |
895 | list)) | |
896 | ||
eec82323 LMI |
897 | (defun gnus-strip-whitespace (string) |
898 | "Return STRING stripped of all whitespace." | |
899 | (while (string-match "[\r\n\t ]+" string) | |
900 | (setq string (replace-match "" t t string))) | |
901 | string) | |
902 | ||
5ec7fe1b GM |
903 | (declare-function gnus-put-text-property "gnus" |
904 | (start end property value &optional object)) | |
905 | ||
16409b0b | 906 | (defsubst gnus-put-text-property-excluding-newlines (beg end prop val) |
eec82323 LMI |
907 | "The same as `put-text-property', but don't put this prop on any newlines in the region." |
908 | (save-match-data | |
909 | (save-excursion | |
910 | (save-restriction | |
911 | (goto-char beg) | |
16409b0b | 912 | (while (re-search-forward gnus-emphasize-whitespace-regexp end 'move) |
6748645f | 913 | (gnus-put-text-property beg (match-beginning 0) prop val) |
eec82323 | 914 | (setq beg (point))) |
6748645f LMI |
915 | (gnus-put-text-property beg (point) prop val))))) |
916 | ||
5ec7fe1b GM |
917 | (declare-function gnus-overlay-put "gnus" (overlay prop value)) |
918 | (declare-function gnus-make-overlay "gnus" | |
919 | (beg end &optional buffer front-advance rear-advance)) | |
920 | ||
23f87bed MB |
921 | (defsubst gnus-put-overlay-excluding-newlines (beg end prop val) |
922 | "The same as `put-text-property', but don't put this prop on any newlines in the region." | |
923 | (save-match-data | |
924 | (save-excursion | |
925 | (save-restriction | |
926 | (goto-char beg) | |
927 | (while (re-search-forward gnus-emphasize-whitespace-regexp end 'move) | |
928 | (gnus-overlay-put | |
929 | (gnus-make-overlay beg (match-beginning 0)) | |
930 | prop val) | |
931 | (setq beg (point))) | |
932 | (gnus-overlay-put (gnus-make-overlay beg (point)) prop val))))) | |
933 | ||
6748645f LMI |
934 | (defun gnus-put-text-property-excluding-characters-with-faces (beg end |
935 | prop val) | |
936 | "The same as `put-text-property', but don't put props on characters with the `gnus-face' property." | |
937 | (let ((b beg)) | |
938 | (while (/= b end) | |
939 | (when (get-text-property b 'gnus-face) | |
940 | (setq b (next-single-property-change b 'gnus-face nil end))) | |
941 | (when (/= b end) | |
23f87bed MB |
942 | (inline |
943 | (gnus-put-text-property | |
944 | b (setq b (next-single-property-change b 'gnus-face nil end)) | |
945 | prop val)))))) | |
946 | ||
947 | (defmacro gnus-faces-at (position) | |
948 | "Return a list of faces at POSITION." | |
949 | (if (featurep 'xemacs) | |
950 | `(let ((pos ,position)) | |
951 | (mapcar-extents 'extent-face | |
952 | nil (current-buffer) pos pos nil 'face)) | |
953 | `(let ((pos ,position)) | |
954 | (delq nil (cons (get-text-property pos 'face) | |
955 | (mapcar | |
956 | (lambda (overlay) | |
957 | (overlay-get overlay 'face)) | |
958 | (overlays-at pos))))))) | |
eec82323 LMI |
959 | |
960 | ;;; Protected and atomic operations. dmoore@ucsd.edu 21.11.1996 | |
d346bf7e SM |
961 | ;; The primary idea here is to try to protect internal datastructures |
962 | ;; from becoming corrupted when the user hits C-g, or if a hook or | |
963 | ;; similar blows up. Often in Gnus multiple tables/lists need to be | |
964 | ;; updated at the same time, or information can be lost. | |
eec82323 LMI |
965 | |
966 | (defvar gnus-atomic-be-safe t | |
967 | "If t, certain operations will be protected from interruption by C-g.") | |
968 | ||
969 | (defmacro gnus-atomic-progn (&rest forms) | |
970 | "Evaluate FORMS atomically, which means to protect the evaluation | |
971 | from being interrupted by the user. An error from the forms themselves | |
972 | will return without finishing the operation. Since interrupts from | |
973 | the user are disabled, it is recommended that only the most minimal | |
974 | operations are performed by FORMS. If you wish to assign many | |
975 | complicated values atomically, compute the results into temporary | |
976 | variables and then do only the assignment atomically." | |
977 | `(let ((inhibit-quit gnus-atomic-be-safe)) | |
978 | ,@forms)) | |
979 | ||
980 | (put 'gnus-atomic-progn 'lisp-indent-function 0) | |
981 | ||
982 | (defmacro gnus-atomic-progn-assign (protect &rest forms) | |
d346bf7e | 983 | "Evaluate FORMS, but ensure that the variables listed in PROTECT |
eec82323 LMI |
984 | are not changed if anything in FORMS signals an error or otherwise |
985 | non-locally exits. The variables listed in PROTECT are updated atomically. | |
986 | It is safe to use gnus-atomic-progn-assign with long computations. | |
987 | ||
988 | Note that if any of the symbols in PROTECT were unbound, they will be | |
8f688cb0 | 989 | set to nil on a successful assignment. In case of an error or other |
eec82323 LMI |
990 | non-local exit, it will still be unbound." |
991 | (let* ((temp-sym-map (mapcar (lambda (x) (list (make-symbol | |
992 | (concat (symbol-name x) | |
993 | "-tmp")) | |
994 | x)) | |
995 | protect)) | |
996 | (sym-temp-map (mapcar (lambda (x) (list (cadr x) (car x))) | |
997 | temp-sym-map)) | |
998 | (temp-sym-let (mapcar (lambda (x) (list (car x) | |
999 | `(and (boundp ',(cadr x)) | |
1000 | ,(cadr x)))) | |
1001 | temp-sym-map)) | |
1002 | (sym-temp-let sym-temp-map) | |
1003 | (temp-sym-assign (apply 'append temp-sym-map)) | |
1004 | (sym-temp-assign (apply 'append sym-temp-map)) | |
1005 | (result (make-symbol "result-tmp"))) | |
1006 | `(let (,@temp-sym-let | |
1007 | ,result) | |
1008 | (let ,sym-temp-let | |
1009 | (setq ,result (progn ,@forms)) | |
1010 | (setq ,@temp-sym-assign)) | |
1011 | (let ((inhibit-quit gnus-atomic-be-safe)) | |
1012 | (setq ,@sym-temp-assign)) | |
1013 | ,result))) | |
1014 | ||
1015 | (put 'gnus-atomic-progn-assign 'lisp-indent-function 1) | |
1016 | ;(put 'gnus-atomic-progn-assign 'edebug-form-spec '(sexp body)) | |
1017 | ||
1018 | (defmacro gnus-atomic-setq (&rest pairs) | |
1019 | "Similar to setq, except that the real symbols are only assigned when | |
1020 | there are no errors. And when the real symbols are assigned, they are | |
1021 | done so atomically. If other variables might be changed via side-effect, | |
1022 | see gnus-atomic-progn-assign. It is safe to use gnus-atomic-setq | |
1023 | with potentially long computations." | |
1024 | (let ((tpairs pairs) | |
1025 | syms) | |
1026 | (while tpairs | |
1027 | (push (car tpairs) syms) | |
1028 | (setq tpairs (cddr tpairs))) | |
1029 | `(gnus-atomic-progn-assign ,syms | |
1030 | (setq ,@pairs)))) | |
1031 | ||
1032 | ;(put 'gnus-atomic-setq 'edebug-form-spec '(body)) | |
1033 | ||
1034 | ||
1035 | ;;; Functions for saving to babyl/mail files. | |
1036 | ||
23f87bed MB |
1037 | (eval-when-compile |
1038 | (condition-case nil | |
1039 | (progn | |
1040 | (require 'rmail) | |
1041 | (autoload 'rmail-update-summary "rmailsum")) | |
1042 | (error | |
1043 | (define-compiler-macro rmail-select-summary (&rest body) | |
1044 | ;; Rmail of the XEmacs version is supplied by the package, and | |
1045 | ;; requires tm and apel packages. However, there may be those | |
1046 | ;; who haven't installed those packages. This macro helps such | |
1047 | ;; people even if they install those packages later. | |
1048 | `(eval '(rmail-select-summary ,@body))) | |
1049 | ;; If there's rmail but there's no tm (or there's apel of the | |
1050 | ;; mainstream, not the XEmacs version), loading rmail of the XEmacs | |
1051 | ;; version fails halfway, however it provides the rmail-select-summary | |
1052 | ;; macro which uses the following functions: | |
1053 | (autoload 'rmail-summary-displayed "rmail") | |
9efa445f DN |
1054 | (autoload 'rmail-maybe-display-summary "rmail")))) |
1055 | ||
1056 | (defvar rmail-default-rmail-file) | |
1057 | (defvar mm-text-coding-system) | |
23f87bed | 1058 | |
88bfa2e4 GM |
1059 | (declare-function mm-append-to-file "mm-util" |
1060 | (start end filename &optional codesys inhibit)) | |
1061 | ||
eec82323 LMI |
1062 | (defun gnus-output-to-rmail (filename &optional ask) |
1063 | "Append the current article to an Rmail file named FILENAME." | |
1064 | (require 'rmail) | |
23f87bed | 1065 | (require 'mm-util) |
eec82323 LMI |
1066 | ;; Most of these codes are borrowed from rmailout.el. |
1067 | (setq filename (expand-file-name filename)) | |
1068 | (setq rmail-default-rmail-file filename) | |
1069 | (let ((artbuf (current-buffer)) | |
1070 | (tmpbuf (get-buffer-create " *Gnus-output*"))) | |
1071 | (save-excursion | |
1072 | (or (get-file-buffer filename) | |
1073 | (file-exists-p filename) | |
1074 | (if (or (not ask) | |
1075 | (gnus-yes-or-no-p | |
1076 | (concat "\"" filename "\" does not exist, create it? "))) | |
1077 | (let ((file-buffer (create-file-buffer filename))) | |
1078 | (save-excursion | |
1079 | (set-buffer file-buffer) | |
1080 | (rmail-insert-rmail-file-header) | |
16409b0b GM |
1081 | (let ((require-final-newline nil) |
1082 | (coding-system-for-write mm-text-coding-system)) | |
eec82323 LMI |
1083 | (gnus-write-buffer filename))) |
1084 | (kill-buffer file-buffer)) | |
1085 | (error "Output file does not exist"))) | |
1086 | (set-buffer tmpbuf) | |
1087 | (erase-buffer) | |
1088 | (insert-buffer-substring artbuf) | |
1089 | (gnus-convert-article-to-rmail) | |
1090 | ;; Decide whether to append to a file or to an Emacs buffer. | |
1091 | (let ((outbuf (get-file-buffer filename))) | |
1092 | (if (not outbuf) | |
47e77e9f SZ |
1093 | (let ((file-name-coding-system nnmail-pathname-coding-system)) |
1094 | (mm-append-to-file (point-min) (point-max) filename)) | |
eec82323 LMI |
1095 | ;; File has been visited, in buffer OUTBUF. |
1096 | (set-buffer outbuf) | |
1097 | (let ((buffer-read-only nil) | |
1098 | (msg (and (boundp 'rmail-current-message) | |
1099 | (symbol-value 'rmail-current-message)))) | |
1100 | ;; If MSG is non-nil, buffer is in RMAIL mode. | |
1101 | (when msg | |
1102 | (widen) | |
1103 | (narrow-to-region (point-max) (point-max))) | |
1104 | (insert-buffer-substring tmpbuf) | |
1105 | (when msg | |
1106 | (goto-char (point-min)) | |
1107 | (widen) | |
23f87bed MB |
1108 | (search-backward "\n\^_") |
1109 | (narrow-to-region (point) (point-max)) | |
1110 | (rmail-count-new-messages t) | |
1111 | (when (rmail-summary-exists) | |
6748645f LMI |
1112 | (rmail-select-summary |
1113 | (rmail-update-summary))) | |
eec82323 | 1114 | (rmail-count-new-messages t) |
6748645f LMI |
1115 | (rmail-show-message msg)) |
1116 | (save-buffer))))) | |
eec82323 LMI |
1117 | (kill-buffer tmpbuf))) |
1118 | ||
1119 | (defun gnus-output-to-mail (filename &optional ask) | |
1120 | "Append the current article to a mail file named FILENAME." | |
1121 | (setq filename (expand-file-name filename)) | |
1122 | (let ((artbuf (current-buffer)) | |
1123 | (tmpbuf (get-buffer-create " *Gnus-output*"))) | |
1124 | (save-excursion | |
1125 | ;; Create the file, if it doesn't exist. | |
1126 | (when (and (not (get-file-buffer filename)) | |
1127 | (not (file-exists-p filename))) | |
1128 | (if (or (not ask) | |
1129 | (gnus-y-or-n-p | |
1130 | (concat "\"" filename "\" does not exist, create it? "))) | |
1131 | (let ((file-buffer (create-file-buffer filename))) | |
1132 | (save-excursion | |
1133 | (set-buffer file-buffer) | |
16409b0b GM |
1134 | (let ((require-final-newline nil) |
1135 | (coding-system-for-write mm-text-coding-system)) | |
eec82323 LMI |
1136 | (gnus-write-buffer filename))) |
1137 | (kill-buffer file-buffer)) | |
1138 | (error "Output file does not exist"))) | |
1139 | (set-buffer tmpbuf) | |
1140 | (erase-buffer) | |
1141 | (insert-buffer-substring artbuf) | |
1142 | (goto-char (point-min)) | |
1143 | (if (looking-at "From ") | |
1144 | (forward-line 1) | |
1145 | (insert "From nobody " (current-time-string) "\n")) | |
1146 | (let (case-fold-search) | |
1147 | (while (re-search-forward "^From " nil t) | |
1148 | (beginning-of-line) | |
1149 | (insert ">"))) | |
1150 | ;; Decide whether to append to a file or to an Emacs buffer. | |
1151 | (let ((outbuf (get-file-buffer filename))) | |
1152 | (if (not outbuf) | |
1153 | (let ((buffer-read-only nil)) | |
1154 | (save-excursion | |
1155 | (goto-char (point-max)) | |
1156 | (forward-char -2) | |
1157 | (unless (looking-at "\n\n") | |
1158 | (goto-char (point-max)) | |
1159 | (unless (bolp) | |
1160 | (insert "\n")) | |
1161 | (insert "\n")) | |
1162 | (goto-char (point-max)) | |
47e77e9f SZ |
1163 | (let ((file-name-coding-system nnmail-pathname-coding-system)) |
1164 | (mm-append-to-file (point-min) (point-max) filename)))) | |
eec82323 LMI |
1165 | ;; File has been visited, in buffer OUTBUF. |
1166 | (set-buffer outbuf) | |
1167 | (let ((buffer-read-only nil)) | |
1168 | (goto-char (point-max)) | |
1169 | (unless (eobp) | |
1170 | (insert "\n")) | |
1171 | (insert "\n") | |
1172 | (insert-buffer-substring tmpbuf))))) | |
1173 | (kill-buffer tmpbuf))) | |
1174 | ||
1175 | (defun gnus-convert-article-to-rmail () | |
1176 | "Convert article in current buffer to Rmail message format." | |
1177 | (let ((buffer-read-only nil)) | |
1178 | ;; Convert article directly into Babyl format. | |
1179 | (goto-char (point-min)) | |
1180 | (insert "\^L\n0, unseen,,\n*** EOOH ***\n") | |
1181 | (while (search-forward "\n\^_" nil t) ;single char | |
1182 | (replace-match "\n^_" t t)) ;2 chars: "^" and "_" | |
1183 | (goto-char (point-max)) | |
1184 | (insert "\^_"))) | |
1185 | ||
6748645f | 1186 | (defun gnus-map-function (funs arg) |
23f87bed | 1187 | "Apply the result of the first function in FUNS to the second, and so on. |
6748645f | 1188 | ARG is passed to the first function." |
23f87bed MB |
1189 | (while funs |
1190 | (setq arg (funcall (pop funs) arg))) | |
1191 | arg) | |
6748645f LMI |
1192 | |
1193 | (defun gnus-run-hooks (&rest funcs) | |
23f87bed MB |
1194 | "Does the same as `run-hooks', but saves the current buffer." |
1195 | (save-current-buffer | |
1196 | (apply 'run-hooks funcs))) | |
6748645f | 1197 | |
b4e8a25b | 1198 | (defun gnus-run-mode-hooks (&rest funcs) |
cfcd5c91 MB |
1199 | "Run `run-mode-hooks' if it is available, otherwise `run-hooks'. |
1200 | This function saves the current buffer." | |
b4e8a25b | 1201 | (if (fboundp 'run-mode-hooks) |
cfcd5c91 MB |
1202 | (save-current-buffer (apply 'run-mode-hooks funcs)) |
1203 | (save-current-buffer (apply 'run-hooks funcs)))) | |
b4e8a25b | 1204 | |
6748645f LMI |
1205 | ;;; Various |
1206 | ||
16409b0b | 1207 | (defvar gnus-group-buffer) ; Compiler directive |
6748645f LMI |
1208 | (defun gnus-alive-p () |
1209 | "Say whether Gnus is running or not." | |
1210 | (and (boundp 'gnus-group-buffer) | |
1211 | (get-buffer gnus-group-buffer) | |
1212 | (save-excursion | |
1213 | (set-buffer gnus-group-buffer) | |
1214 | (eq major-mode 'gnus-group-mode)))) | |
1215 | ||
23f87bed MB |
1216 | (defun gnus-remove-if (predicate list) |
1217 | "Return a copy of LIST with all items satisfying PREDICATE removed." | |
6748645f LMI |
1218 | (let (out) |
1219 | (while list | |
1220 | (unless (funcall predicate (car list)) | |
1221 | (push (car list) out)) | |
23f87bed | 1222 | (setq list (cdr list))) |
6748645f LMI |
1223 | (nreverse out))) |
1224 | ||
23f87bed MB |
1225 | (if (fboundp 'assq-delete-all) |
1226 | (defalias 'gnus-delete-alist 'assq-delete-all) | |
1227 | (defun gnus-delete-alist (key alist) | |
1228 | "Delete from ALIST all elements whose car is KEY. | |
1229 | Return the modified alist." | |
1230 | (let (entry) | |
1231 | (while (setq entry (assq key alist)) | |
1232 | (setq alist (delq entry alist))) | |
1233 | alist))) | |
6748645f | 1234 | |
16409b0b | 1235 | (defmacro gnus-pull (key alist &optional assoc-p) |
6748645f LMI |
1236 | "Modify ALIST to be without KEY." |
1237 | (unless (symbolp alist) | |
1238 | (error "Not a symbol: %s" alist)) | |
16409b0b GM |
1239 | (let ((fun (if assoc-p 'assoc 'assq))) |
1240 | `(setq ,alist (delq (,fun ,key ,alist) ,alist)))) | |
6748645f LMI |
1241 | |
1242 | (defun gnus-globalify-regexp (re) | |
e7f767c2 | 1243 | "Return a regexp that matches a whole line, if RE matches a part of it." |
6748645f LMI |
1244 | (concat (unless (string-match "^\\^" re) "^.*") |
1245 | re | |
1246 | (unless (string-match "\\$$" re) ".*$"))) | |
1247 | ||
16409b0b GM |
1248 | (defun gnus-set-window-start (&optional point) |
1249 | "Set the window start to POINT, or (point) if nil." | |
23f87bed | 1250 | (let ((win (gnus-get-buffer-window (current-buffer) t))) |
16409b0b GM |
1251 | (when win |
1252 | (set-window-start win (or point (point)))))) | |
1253 | ||
1254 | (defun gnus-annotation-in-region-p (b e) | |
1255 | (if (= b e) | |
1256 | (eq (cadr (memq 'gnus-undeletable (text-properties-at b))) t) | |
1257 | (text-property-any b e 'gnus-undeletable t))) | |
1258 | ||
1259 | (defun gnus-or (&rest elems) | |
1260 | "Return non-nil if any of the elements are non-nil." | |
1261 | (catch 'found | |
1262 | (while elems | |
1263 | (when (pop elems) | |
1264 | (throw 'found t))))) | |
1265 | ||
1266 | (defun gnus-and (&rest elems) | |
1267 | "Return non-nil if all of the elements are non-nil." | |
1268 | (catch 'found | |
1269 | (while elems | |
1270 | (unless (pop elems) | |
1271 | (throw 'found nil))) | |
1272 | t)) | |
1273 | ||
5ec7fe1b GM |
1274 | ;; gnus.el requires mm-util. |
1275 | (declare-function mm-disable-multibyte "mm-util") | |
1276 | ||
16409b0b | 1277 | (defun gnus-write-active-file (file hashtb &optional full-names) |
01c52d31 | 1278 | ;; `coding-system-for-write' should be `raw-text' or equivalent. |
16409b0b GM |
1279 | (let ((coding-system-for-write nnmail-active-file-coding-system)) |
1280 | (with-temp-file file | |
01c52d31 MB |
1281 | ;; The buffer should be in the unibyte mode because group names |
1282 | ;; are ASCII text or encoded non-ASCII text (i.e., unibyte). | |
1283 | (mm-disable-multibyte) | |
16409b0b GM |
1284 | (mapatoms |
1285 | (lambda (sym) | |
1286 | (when (and sym | |
1287 | (boundp sym) | |
1288 | (symbol-value sym)) | |
1289 | (insert (format "%S %d %d y\n" | |
1290 | (if full-names | |
1291 | sym | |
1292 | (intern (gnus-group-real-name (symbol-name sym)))) | |
1293 | (or (cdr (symbol-value sym)) | |
1294 | (car (symbol-value sym))) | |
1295 | (car (symbol-value sym)))))) | |
1296 | hashtb) | |
1297 | (goto-char (point-max)) | |
1298 | (while (search-backward "\\." nil t) | |
1299 | (delete-char 1))))) | |
1300 | ||
23f87bed MB |
1301 | ;; Fixme: Why not use `with-output-to-temp-buffer'? |
1302 | (defmacro gnus-with-output-to-file (file &rest body) | |
1303 | (let ((buffer (make-symbol "output-buffer")) | |
1304 | (size (make-symbol "output-buffer-size")) | |
1305 | (leng (make-symbol "output-buffer-length")) | |
1306 | (append (make-symbol "output-buffer-append"))) | |
1307 | `(let* ((,size 131072) | |
1308 | (,buffer (make-string ,size 0)) | |
1309 | (,leng 0) | |
1310 | (,append nil) | |
1311 | (standard-output | |
1312 | (lambda (c) | |
1313 | (aset ,buffer ,leng c) | |
bf247b6e | 1314 | |
23f87bed MB |
1315 | (if (= ,size (setq ,leng (1+ ,leng))) |
1316 | (progn (write-region ,buffer nil ,file ,append 'no-msg) | |
1317 | (setq ,leng 0 | |
1318 | ,append t)))))) | |
1319 | ,@body | |
1320 | (when (> ,leng 0) | |
1321 | (let ((coding-system-for-write 'no-conversion)) | |
1322 | (write-region (substring ,buffer 0 ,leng) nil ,file | |
1323 | ,append 'no-msg)))))) | |
1324 | ||
1325 | (put 'gnus-with-output-to-file 'lisp-indent-function 1) | |
1326 | (put 'gnus-with-output-to-file 'edebug-form-spec '(form body)) | |
1327 | ||
1328 | (if (fboundp 'union) | |
1329 | (defalias 'gnus-union 'union) | |
1330 | (defun gnus-union (l1 l2) | |
1331 | "Set union of lists L1 and L2." | |
1332 | (cond ((null l1) l2) | |
1333 | ((null l2) l1) | |
1334 | ((equal l1 l2) l1) | |
1335 | (t | |
1336 | (or (>= (length l1) (length l2)) | |
1337 | (setq l1 (prog1 l2 (setq l2 l1)))) | |
1338 | (while l2 | |
1339 | (or (member (car l2) l1) | |
1340 | (push (car l2) l1)) | |
1341 | (pop l2)) | |
1342 | l1)))) | |
1343 | ||
5ec7fe1b GM |
1344 | (declare-function gnus-add-text-properties "gnus" |
1345 | (start end properties &optional object)) | |
1346 | ||
520aa572 SZ |
1347 | (defun gnus-add-text-properties-when |
1348 | (property value start end properties &optional object) | |
1349 | "Like `gnus-add-text-properties', only applied on where PROPERTY is VALUE." | |
1350 | (let (point) | |
3d8c35d3 | 1351 | (while (and start |
23f87bed | 1352 | (< start end) ;; XEmacs will loop for every when start=end. |
520aa572 SZ |
1353 | (setq point (text-property-not-all start end property value))) |
1354 | (gnus-add-text-properties start point properties object) | |
1355 | (setq start (text-property-any point end property value))) | |
1356 | (if start | |
1357 | (gnus-add-text-properties start end properties object)))) | |
1358 | ||
1359 | (defun gnus-remove-text-properties-when | |
1360 | (property value start end properties &optional object) | |
1361 | "Like `remove-text-properties', only applied on where PROPERTY is VALUE." | |
1362 | (let (point) | |
3d8c35d3 | 1363 | (while (and start |
23f87bed | 1364 | (< start end) |
520aa572 SZ |
1365 | (setq point (text-property-not-all start end property value))) |
1366 | (remove-text-properties start point properties object) | |
1367 | (setq start (text-property-any point end property value))) | |
1368 | (if start | |
4481aa98 SZ |
1369 | (remove-text-properties start end properties object)) |
1370 | t)) | |
520aa572 | 1371 | |
01c52d31 MB |
1372 | (defun gnus-string-remove-all-properties (string) |
1373 | (condition-case () | |
1374 | (let ((s string)) | |
1375 | (set-text-properties 0 (length string) nil string) | |
1376 | s) | |
1377 | (error string))) | |
1378 | ||
23f87bed MB |
1379 | ;; This might use `compare-strings' to reduce consing in the |
1380 | ;; case-insensitive case, but it has to cope with null args. | |
1381 | ;; (`string-equal' uses symbol print names.) | |
1382 | (defun gnus-string-equal (x y) | |
1383 | "Like `string-equal', except it compares case-insensitively." | |
1384 | (and (= (length x) (length y)) | |
1385 | (or (string-equal x y) | |
1386 | (string-equal (downcase x) (downcase y))))) | |
1387 | ||
1388 | (defcustom gnus-use-byte-compile t | |
1389 | "If non-nil, byte-compile crucial run-time code. | |
1390 | Setting it to nil has no effect after the first time `gnus-byte-compile' | |
1391 | is run." | |
1392 | :type 'boolean | |
bf247b6e | 1393 | :version "22.1" |
23f87bed MB |
1394 | :group 'gnus-various) |
1395 | ||
1396 | (defun gnus-byte-compile (form) | |
1397 | "Byte-compile FORM if `gnus-use-byte-compile' is non-nil." | |
1398 | (if gnus-use-byte-compile | |
1399 | (progn | |
1400 | (condition-case nil | |
1401 | ;; Work around a bug in XEmacs 21.4 | |
1402 | (require 'byte-optimize) | |
1403 | (error)) | |
1404 | (require 'bytecomp) | |
1405 | (defalias 'gnus-byte-compile | |
1406 | (lambda (form) | |
1407 | (let ((byte-compile-warnings '(unresolved callargs redefine))) | |
1408 | (byte-compile form)))) | |
1409 | (gnus-byte-compile form)) | |
1410 | form)) | |
1411 | ||
1412 | (defun gnus-remassoc (key alist) | |
1413 | "Delete by side effect any elements of LIST whose car is `equal' to KEY. | |
1414 | The modified LIST is returned. If the first member | |
1415 | of LIST has a car that is `equal' to KEY, there is no way to remove it | |
54506618 | 1416 | by side effect; therefore, write `(setq foo (gnus-remassoc key foo))' to be |
23f87bed MB |
1417 | sure of changing the value of `foo'." |
1418 | (when alist | |
1419 | (if (equal key (caar alist)) | |
1420 | (cdr alist) | |
1421 | (setcdr alist (gnus-remassoc key (cdr alist))) | |
1422 | alist))) | |
1423 | ||
1424 | (defun gnus-update-alist-soft (key value alist) | |
1425 | (if value | |
1426 | (cons (cons key value) (gnus-remassoc key alist)) | |
1427 | (gnus-remassoc key alist))) | |
1428 | ||
1429 | (defun gnus-create-info-command (node) | |
1430 | "Create a command that will go to info NODE." | |
1431 | `(lambda () | |
1432 | (interactive) | |
1433 | ,(concat "Enter the info system at node " node) | |
1434 | (Info-goto-node ,node) | |
1435 | (setq gnus-info-buffer (current-buffer)) | |
1436 | (gnus-configure-windows 'info))) | |
1437 | ||
1438 | (defun gnus-not-ignore (&rest args) | |
1439 | t) | |
1440 | ||
47b63dfa SZ |
1441 | (defvar gnus-directory-sep-char-regexp "/" |
1442 | "The regexp of directory separator character. | |
1443 | If you find some problem with the directory separator character, try | |
1444 | \"[/\\\\\]\" for some systems.") | |
1445 | ||
23f87bed MB |
1446 | (defun gnus-url-unhex (x) |
1447 | (if (> x ?9) | |
1448 | (if (>= x ?a) | |
1449 | (+ 10 (- x ?a)) | |
1450 | (+ 10 (- x ?A))) | |
1451 | (- x ?0))) | |
1452 | ||
1453 | ;; Fixme: Do it like QP. | |
1454 | (defun gnus-url-unhex-string (str &optional allow-newlines) | |
1455 | "Remove %XX, embedded spaces, etc in a url. | |
1456 | If optional second argument ALLOW-NEWLINES is non-nil, then allow the | |
1457 | decoding of carriage returns and line feeds in the string, which is normally | |
1458 | forbidden in URL encoding." | |
1459 | (let ((tmp "") | |
1460 | (case-fold-search t)) | |
1461 | (while (string-match "%[0-9a-f][0-9a-f]" str) | |
1462 | (let* ((start (match-beginning 0)) | |
1463 | (ch1 (gnus-url-unhex (elt str (+ start 1)))) | |
1464 | (code (+ (* 16 ch1) | |
1465 | (gnus-url-unhex (elt str (+ start 2)))))) | |
1466 | (setq tmp (concat | |
1467 | tmp (substring str 0 start) | |
1468 | (cond | |
1469 | (allow-newlines | |
1470 | (char-to-string code)) | |
1471 | ((or (= code ?\n) (= code ?\r)) | |
1472 | " ") | |
1473 | (t (char-to-string code)))) | |
1474 | str (substring str (match-end 0))))) | |
1475 | (setq tmp (concat tmp str)) | |
1476 | tmp)) | |
1477 | ||
1478 | (defun gnus-make-predicate (spec) | |
1479 | "Transform SPEC into a function that can be called. | |
1480 | SPEC is a predicate specifier that contains stuff like `or', `and', | |
1481 | `not', lists and functions. The functions all take one parameter." | |
1482 | `(lambda (elem) ,(gnus-make-predicate-1 spec))) | |
1483 | ||
1484 | (defun gnus-make-predicate-1 (spec) | |
1485 | (cond | |
1486 | ((symbolp spec) | |
1487 | `(,spec elem)) | |
1488 | ((listp spec) | |
1489 | (if (memq (car spec) '(or and not)) | |
1490 | `(,(car spec) ,@(mapcar 'gnus-make-predicate-1 (cdr spec))) | |
1491 | (error "Invalid predicate specifier: %s" spec))))) | |
1492 | ||
23f87bed MB |
1493 | (defun gnus-completing-read (prompt table &optional predicate require-match |
1494 | history) | |
1495 | (when (and history | |
1496 | (not (boundp history))) | |
1497 | (set history nil)) | |
01c52d31 | 1498 | (completing-read |
23f87bed MB |
1499 | (if (symbol-value history) |
1500 | (concat prompt " (" (car (symbol-value history)) "): ") | |
1501 | (concat prompt ": ")) | |
1502 | table | |
1503 | predicate | |
1504 | require-match | |
1505 | nil | |
1506 | history | |
1507 | (car (symbol-value history)))) | |
1508 | ||
1509 | (defun gnus-graphic-display-p () | |
1510 | (or (and (fboundp 'display-graphic-p) | |
1511 | (display-graphic-p)) | |
1512 | ;;;!!!This is bogus. Fixme! | |
1513 | (and (featurep 'xemacs) | |
1514 | t))) | |
1515 | ||
1516 | (put 'gnus-parse-without-error 'lisp-indent-function 0) | |
1517 | (put 'gnus-parse-without-error 'edebug-form-spec '(body)) | |
1518 | ||
1519 | (defmacro gnus-parse-without-error (&rest body) | |
1520 | "Allow continuing onto the next line even if an error occurs." | |
1521 | `(while (not (eobp)) | |
1522 | (condition-case () | |
1523 | (progn | |
1524 | ,@body | |
1525 | (goto-char (point-max))) | |
1526 | (error | |
1527 | (gnus-error 4 "Invalid data on line %d" | |
1528 | (count-lines (point-min) (point))) | |
1529 | (forward-line 1))))) | |
1530 | ||
1531 | (defun gnus-cache-file-contents (file variable function) | |
1532 | "Cache the contents of FILE in VARIABLE. The contents come from FUNCTION." | |
1533 | (let ((time (nth 5 (file-attributes file))) | |
1534 | contents value) | |
1535 | (if (or (null (setq value (symbol-value variable))) | |
1536 | (not (equal (car value) file)) | |
1537 | (not (equal (nth 1 value) time))) | |
1538 | (progn | |
1539 | (setq contents (funcall function file)) | |
1540 | (set variable (list file time contents)) | |
1541 | contents) | |
1542 | (nth 2 value)))) | |
1543 | ||
1544 | (defun gnus-multiple-choice (prompt choice &optional idx) | |
1545 | "Ask user a multiple choice question. | |
1546 | CHOICE is a list of the choice char and help message at IDX." | |
1547 | (let (tchar buf) | |
1548 | (save-window-excursion | |
1549 | (save-excursion | |
1550 | (while (not tchar) | |
1551 | (message "%s (%s): " | |
1552 | prompt | |
1553 | (concat | |
1554 | (mapconcat (lambda (s) (char-to-string (car s))) | |
1555 | choice ", ") ", ?")) | |
1556 | (setq tchar (read-char)) | |
1557 | (when (not (assq tchar choice)) | |
1558 | (setq tchar nil) | |
1559 | (setq buf (get-buffer-create "*Gnus Help*")) | |
1560 | (pop-to-buffer buf) | |
1561 | (fundamental-mode) ; for Emacs 20.4+ | |
1562 | (buffer-disable-undo) | |
1563 | (erase-buffer) | |
1564 | (insert prompt ":\n\n") | |
1565 | (let ((max -1) | |
1566 | (list choice) | |
1567 | (alist choice) | |
1568 | (idx (or idx 1)) | |
1569 | (i 0) | |
1570 | n width pad format) | |
1571 | ;; find the longest string to display | |
1572 | (while list | |
1573 | (setq n (length (nth idx (car list)))) | |
1574 | (unless (> max n) | |
1575 | (setq max n)) | |
1576 | (setq list (cdr list))) | |
1577 | (setq max (+ max 4)) ; %c, `:', SPACE, a SPACE at end | |
1578 | (setq n (/ (1- (window-width)) max)) ; items per line | |
1579 | (setq width (/ (1- (window-width)) n)) ; width of each item | |
1580 | ;; insert `n' items, each in a field of width `width' | |
1581 | (while alist | |
1582 | (if (< i n) | |
1583 | () | |
1584 | (setq i 0) | |
1585 | (delete-char -1) ; the `\n' takes a char | |
1586 | (insert "\n")) | |
1587 | (setq pad (- width 3)) | |
1588 | (setq format (concat "%c: %-" (int-to-string pad) "s")) | |
1589 | (insert (format format (caar alist) (nth idx (car alist)))) | |
1590 | (setq alist (cdr alist)) | |
1591 | (setq i (1+ i)))))))) | |
1592 | (if (buffer-live-p buf) | |
1593 | (kill-buffer buf)) | |
1594 | tchar)) | |
1595 | ||
dbb6c370 | 1596 | (declare-function x-focus-frame "xfns.c" (frame)) |
88bfa2e4 GM |
1597 | (declare-function w32-focus-frame "../term/w32-win" (frame)) |
1598 | ||
23f87bed MB |
1599 | (defun gnus-select-frame-set-input-focus (frame) |
1600 | "Select FRAME, raise it, and set input focus, if possible." | |
1601 | (cond ((featurep 'xemacs) | |
f67d6742 MB |
1602 | (if (fboundp 'select-frame-set-input-focus) |
1603 | (select-frame-set-input-focus frame) | |
1604 | (raise-frame frame) | |
1605 | (select-frame frame) | |
1606 | (focus-frame frame))) | |
1607 | ;; `select-frame-set-input-focus' defined in Emacs 21 will not | |
1608 | ;; set the input focus. | |
1609 | ((>= emacs-major-version 22) | |
1610 | (select-frame-set-input-focus frame)) | |
23f87bed MB |
1611 | (t |
1612 | (raise-frame frame) | |
1613 | (select-frame frame) | |
d221466a | 1614 | (cond ((memq window-system '(x ns mac)) |
23f87bed MB |
1615 | (x-focus-frame frame)) |
1616 | ((eq window-system 'w32) | |
d221466a | 1617 | (w32-focus-frame frame))) |
23f87bed MB |
1618 | (when focus-follows-mouse |
1619 | (set-mouse-position frame (1- (frame-width frame)) 0))))) | |
1620 | ||
1621 | (defun gnus-frame-or-window-display-name (object) | |
1622 | "Given a frame or window, return the associated display name. | |
1623 | Return nil otherwise." | |
1624 | (if (featurep 'xemacs) | |
1625 | (device-connection (dfw-device object)) | |
1626 | (if (or (framep object) | |
1627 | (and (windowp object) | |
1628 | (setq object (window-frame object)))) | |
1629 | (let ((display (frame-parameter object 'display))) | |
1630 | (if (and (stringp display) | |
1631 | ;; Exclude invalid display names. | |
1632 | (string-match "\\`[^:]*:[0-9]+\\(\\.[0-9]+\\)?\\'" | |
1633 | display)) | |
1634 | display))))) | |
1635 | ||
9efa445f | 1636 | (defvar tool-bar-mode) |
531bedc3 | 1637 | |
85fd8002 RS |
1638 | (defun gnus-tool-bar-update (&rest ignore) |
1639 | "Update the tool bar." | |
1640 | (when (and (boundp 'tool-bar-mode) | |
1641 | tool-bar-mode) | |
1642 | (let* ((args nil) | |
1643 | (func (cond ((featurep 'xemacs) | |
1644 | 'ignore) | |
1645 | ((fboundp 'tool-bar-update) | |
1646 | 'tool-bar-update) | |
1647 | ((fboundp 'force-window-update) | |
1648 | 'force-window-update) | |
1649 | ((fboundp 'redraw-frame) | |
1650 | (setq args (list (selected-frame))) | |
1651 | 'redraw-frame) | |
1652 | (t 'ignore)))) | |
1653 | (apply func args)))) | |
1654 | ||
23f87bed MB |
1655 | ;; Fixme: This has only one use (in gnus-agent), which isn't worthwhile. |
1656 | (defmacro gnus-mapcar (function seq1 &rest seqs2_n) | |
1657 | "Apply FUNCTION to each element of the sequences, and make a list of the results. | |
1658 | If there are several sequences, FUNCTION is called with that many arguments, | |
1659 | and mapping stops as soon as the shortest sequence runs out. With just one | |
1660 | sequence, this is like `mapcar'. With several, it is like the Common Lisp | |
1661 | `mapcar' function extended to arbitrary sequence types." | |
1662 | ||
1663 | (if seqs2_n | |
1664 | (let* ((seqs (cons seq1 seqs2_n)) | |
1665 | (cnt 0) | |
1666 | (heads (mapcar (lambda (seq) | |
1667 | (make-symbol (concat "head" | |
1668 | (int-to-string | |
1669 | (setq cnt (1+ cnt)))))) | |
1670 | seqs)) | |
1671 | (result (make-symbol "result")) | |
1672 | (result-tail (make-symbol "result-tail"))) | |
1673 | `(let* ,(let* ((bindings (cons nil nil)) | |
1674 | (heads heads)) | |
1675 | (nconc bindings (list (list result '(cons nil nil)))) | |
1676 | (nconc bindings (list (list result-tail result))) | |
1677 | (while heads | |
1678 | (nconc bindings (list (list (pop heads) (pop seqs))))) | |
1679 | (cdr bindings)) | |
1680 | (while (and ,@heads) | |
1681 | (setcdr ,result-tail (cons (funcall ,function | |
1682 | ,@(mapcar (lambda (h) (list 'car h)) | |
1683 | heads)) | |
1684 | nil)) | |
1685 | (setq ,result-tail (cdr ,result-tail) | |
1686 | ,@(apply 'nconc (mapcar (lambda (h) (list h (list 'cdr h))) heads)))) | |
1687 | (cdr ,result))) | |
1688 | `(mapcar ,function ,seq1))) | |
1689 | ||
1690 | (if (fboundp 'merge) | |
1691 | (defalias 'gnus-merge 'merge) | |
1692 | ;; Adapted from cl-seq.el | |
1693 | (defun gnus-merge (type list1 list2 pred) | |
1694 | "Destructively merge lists LIST1 and LIST2 to produce a new list. | |
1695 | Argument TYPE is for compatibility and ignored. | |
1696 | Ordering of the elements is preserved according to PRED, a `less-than' | |
1697 | predicate on the elements." | |
1698 | (let ((res nil)) | |
1699 | (while (and list1 list2) | |
1700 | (if (funcall pred (car list2) (car list1)) | |
1701 | (push (pop list2) res) | |
1702 | (push (pop list1) res))) | |
1703 | (nconc (nreverse res) list1 list2)))) | |
1704 | ||
9efa445f DN |
1705 | (defvar xemacs-codename) |
1706 | (defvar sxemacs-codename) | |
1707 | (defvar emacs-program-version) | |
23f87bed MB |
1708 | |
1709 | (defun gnus-emacs-version () | |
1710 | "Stringified Emacs version." | |
4a2358e9 MB |
1711 | (let* ((lst (if (listp gnus-user-agent) |
1712 | gnus-user-agent | |
1713 | '(gnus emacs type))) | |
1714 | (system-v (cond ((memq 'config lst) | |
1715 | system-configuration) | |
1716 | ((memq 'type lst) | |
1717 | (symbol-name system-type)) | |
1718 | (t nil))) | |
1719 | codename emacsname) | |
1720 | (cond ((featurep 'sxemacs) | |
1721 | (setq emacsname "SXEmacs" | |
1722 | codename sxemacs-codename)) | |
1723 | ((featurep 'xemacs) | |
1724 | (setq emacsname "XEmacs" | |
1725 | codename xemacs-codename)) | |
1726 | (t | |
1727 | (setq emacsname "Emacs"))) | |
23f87bed | 1728 | (cond |
4a2358e9 | 1729 | ((not (memq 'emacs lst)) |
23f87bed MB |
1730 | nil) |
1731 | ((string-match "^\\(\\([.0-9]+\\)*\\)\\.[0-9]+$" emacs-version) | |
4a2358e9 | 1732 | ;; Emacs: |
23f87bed MB |
1733 | (concat "Emacs/" (match-string 1 emacs-version) |
1734 | (if system-v | |
1735 | (concat " (" system-v ")") | |
1736 | ""))) | |
4a2358e9 MB |
1737 | ((or (featurep 'sxemacs) (featurep 'xemacs)) |
1738 | ;; XEmacs or SXEmacs: | |
1739 | (concat emacsname "/" emacs-program-version | |
01c52d31 MB |
1740 | (let (plst) |
1741 | (when (memq 'codename lst) | |
1742 | (push codename plst)) | |
1743 | (when system-v | |
1744 | (push system-v plst)) | |
1745 | (unless (featurep 'mule) | |
1746 | (push "no MULE" plst)) | |
1747 | (when (> (length plst) 0) | |
1748 | (concat | |
1749 | " (" (mapconcat 'identity (reverse plst) ", ") ")"))))) | |
23f87bed MB |
1750 | (t emacs-version)))) |
1751 | ||
54506618 MB |
1752 | (defun gnus-rename-file (old-path new-path &optional trim) |
1753 | "Rename OLD-PATH as NEW-PATH. If TRIM, recursively delete | |
1754 | empty directories from OLD-PATH." | |
1755 | (when (file-exists-p old-path) | |
1756 | (let* ((old-dir (file-name-directory old-path)) | |
1757 | (old-name (file-name-nondirectory old-path)) | |
1758 | (new-dir (file-name-directory new-path)) | |
1759 | (new-name (file-name-nondirectory new-path)) | |
1760 | temp) | |
1761 | (gnus-make-directory new-dir) | |
1762 | (rename-file old-path new-path t) | |
1763 | (when trim | |
1764 | (while (progn (setq temp (directory-files old-dir)) | |
1765 | (while (member (car temp) '("." "..")) | |
1766 | (setq temp (cdr temp))) | |
1767 | (= (length temp) 0)) | |
1768 | (delete-directory old-dir) | |
bf247b6e KS |
1769 | (setq old-dir (file-name-as-directory |
1770 | (file-truename | |
54506618 MB |
1771 | (concat old-dir ".."))))))))) |
1772 | ||
01c52d31 MB |
1773 | (defun gnus-set-file-modes (filename mode) |
1774 | "Wrapper for set-file-modes." | |
1775 | (ignore-errors | |
1776 | (set-file-modes filename mode))) | |
1777 | ||
4a43ee9b MB |
1778 | (if (fboundp 'set-process-query-on-exit-flag) |
1779 | (defalias 'gnus-set-process-query-on-exit-flag | |
1780 | 'set-process-query-on-exit-flag) | |
1781 | (defalias 'gnus-set-process-query-on-exit-flag | |
1782 | 'process-kill-without-query)) | |
54506618 | 1783 | |
996aa8c1 MB |
1784 | (if (fboundp 'with-local-quit) |
1785 | (defalias 'gnus-with-local-quit 'with-local-quit) | |
1786 | (defmacro gnus-with-local-quit (&rest body) | |
1787 | "Execute BODY, allowing quits to terminate BODY but not escape further. | |
1788 | When a quit terminates BODY, `gnus-with-local-quit' returns nil but | |
1789 | requests another quit. That quit will be processed as soon as quitting | |
1790 | is allowed once again. (Immediately, if `inhibit-quit' is nil.)" | |
1791 | ;;(declare (debug t) (indent 0)) | |
1792 | `(condition-case nil | |
1793 | (let ((inhibit-quit nil)) | |
1794 | ,@body) | |
1795 | (quit (setq quit-flag t) | |
1796 | ;; This call is to give a chance to handle quit-flag | |
1797 | ;; in case inhibit-quit is nil. | |
1798 | ;; Without this, it will not be handled until the next function | |
1799 | ;; call, and that might allow it to exit thru a condition-case | |
1800 | ;; that intends to handle the quit signal next time. | |
1801 | (eval '(ignore nil)))))) | |
1802 | ||
d346bf7e SM |
1803 | (defalias 'gnus-read-shell-command |
1804 | (if (fboundp 'read-shell-command) 'read-shell-command 'read-string)) | |
1805 | ||
2b968687 MB |
1806 | (defmacro gnus-put-display-table (range value display-table) |
1807 | "Set the value for char RANGE to VALUE in DISPLAY-TABLE. " | |
1808 | (if (featurep 'xemacs) | |
1809 | (progn | |
1810 | `(if (fboundp 'put-display-table) | |
1811 | (put-display-table ,range ,value ,display-table) | |
1812 | (if (sequencep ,display-table) | |
1813 | (aset ,display-table ,range ,value) | |
1814 | (put-char-table ,range ,value ,display-table)))) | |
1815 | `(aset ,display-table ,range ,value))) | |
1816 | ||
1817 | (defmacro gnus-get-display-table (character display-table) | |
1818 | "Find value for CHARACTER in DISPLAY-TABLE. " | |
1819 | (if (featurep 'xemacs) | |
1820 | `(if (fboundp 'get-display-table) | |
1821 | (get-display-table ,character ,display-table) | |
1822 | (if (sequencep ,display-table) | |
1823 | (aref ,display-table ,character) | |
1824 | (get-char-table ,character ,display-table))) | |
1825 | `(aref ,display-table ,character))) | |
1826 | ||
eec82323 LMI |
1827 | (provide 'gnus-util) |
1828 | ||
cbee283d | 1829 | ;; arch-tag: f94991af-d32b-4c97-8c26-ca12a934de49 |
eec82323 | 1830 | ;;; gnus-util.el ends here |