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