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