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