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