17493
|
1 ;;; gnus-util.el --- utility functions for Gnus
|
|
2 ;; Copyright (C) 1996,97 Free Software Foundation, Inc.
|
|
3
|
|
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
|
|
5 ;; Keywords: news
|
|
6
|
|
7 ;; This file is part of GNU Emacs.
|
|
8
|
|
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
10 ;; it under the terms of the GNU General Public License as published by
|
|
11 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
12 ;; any later version.
|
|
13
|
|
14 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 ;; GNU General Public License for more details.
|
|
18
|
|
19 ;; You should have received a copy of the GNU General Public License
|
|
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
22 ;; Boston, MA 02111-1307, USA.
|
|
23
|
|
24 ;;; Commentary:
|
|
25
|
|
26 ;; Nothing in this file depends on any other parts of Gnus -- all
|
|
27 ;; functions and macros in this file are utility functions that are
|
|
28 ;; used by Gnus and may be used by any other package without loading
|
|
29 ;; Gnus first.
|
|
30
|
|
31 ;;; Code:
|
|
32
|
|
33 (require 'custom)
|
|
34 (require 'cl)
|
|
35 (require 'nnheader)
|
|
36 (require 'timezone)
|
|
37 (require 'message)
|
|
38
|
|
39 (eval-and-compile
|
|
40 (autoload 'nnmail-date-to-time "nnmail"))
|
|
41
|
|
42 (defun gnus-boundp (variable)
|
|
43 "Return non-nil if VARIABLE is bound and non-nil."
|
|
44 (and (boundp variable)
|
|
45 (symbol-value variable)))
|
|
46
|
|
47 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
|
|
48 "Pop to BUFFER, evaluate FORMS, and then return to the original window."
|
|
49 (let ((tempvar (make-symbol "GnusStartBufferWindow"))
|
|
50 (w (make-symbol "w"))
|
|
51 (buf (make-symbol "buf")))
|
|
52 `(let* ((,tempvar (selected-window))
|
|
53 (,buf ,buffer)
|
|
54 (,w (get-buffer-window ,buf 'visible)))
|
|
55 (unwind-protect
|
|
56 (progn
|
|
57 (if ,w
|
|
58 (progn
|
|
59 (select-window ,w)
|
|
60 (set-buffer (window-buffer ,w)))
|
|
61 (pop-to-buffer ,buf))
|
|
62 ,@forms)
|
|
63 (select-window ,tempvar)))))
|
|
64
|
|
65 (put 'gnus-eval-in-buffer-window 'lisp-indent-function 1)
|
|
66 (put 'gnus-eval-in-buffer-window 'edebug-form-spec '(form body))
|
|
67
|
|
68 (defmacro gnus-intern-safe (string hashtable)
|
|
69 "Set hash value. Arguments are STRING, VALUE, and HASHTABLE."
|
|
70 `(let ((symbol (intern ,string ,hashtable)))
|
|
71 (or (boundp symbol)
|
|
72 (set symbol nil))
|
|
73 symbol))
|
|
74
|
|
75 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
|
|
76 ;; function `substring' might cut on a middle of multi-octet
|
|
77 ;; character.
|
|
78 (defun gnus-truncate-string (str width)
|
|
79 (substring str 0 width))
|
|
80
|
|
81 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>. A safe way
|
|
82 ;; to limit the length of a string. This function is necessary since
|
|
83 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
|
|
84 (defsubst gnus-limit-string (str width)
|
|
85 (if (> (length str) width)
|
|
86 (substring str 0 width)
|
|
87 str))
|
|
88
|
|
89 (defsubst gnus-functionp (form)
|
|
90 "Return non-nil if FORM is funcallable."
|
|
91 (or (and (symbolp form) (fboundp form))
|
|
92 (and (listp form) (eq (car form) 'lambda))
|
|
93 (compiled-function-p form)))
|
|
94
|
|
95 (defsubst gnus-goto-char (point)
|
|
96 (and point (goto-char point)))
|
|
97
|
|
98 (defmacro gnus-buffer-exists-p (buffer)
|
|
99 `(let ((buffer ,buffer))
|
|
100 (when buffer
|
|
101 (funcall (if (stringp buffer) 'get-buffer 'buffer-name)
|
|
102 buffer))))
|
|
103
|
|
104 (defmacro gnus-kill-buffer (buffer)
|
|
105 `(let ((buf ,buffer))
|
|
106 (when (gnus-buffer-exists-p buf)
|
|
107 (kill-buffer buf))))
|
|
108
|
|
109 (if (fboundp 'point-at-bol)
|
|
110 (fset 'gnus-point-at-bol 'point-at-bol)
|
|
111 (defun gnus-point-at-bol ()
|
|
112 "Return point at the beginning of the line."
|
|
113 (let ((p (point)))
|
|
114 (beginning-of-line)
|
|
115 (prog1
|
|
116 (point)
|
|
117 (goto-char p)))))
|
|
118
|
|
119 (if (fboundp 'point-at-eol)
|
|
120 (fset 'gnus-point-at-eol 'point-at-eol)
|
|
121 (defun gnus-point-at-eol ()
|
|
122 "Return point at the end of the line."
|
|
123 (let ((p (point)))
|
|
124 (end-of-line)
|
|
125 (prog1
|
|
126 (point)
|
|
127 (goto-char p)))))
|
|
128
|
|
129 (defun gnus-delete-first (elt list)
|
|
130 "Delete by side effect the first occurrence of ELT as a member of LIST."
|
|
131 (if (equal (car list) elt)
|
|
132 (cdr list)
|
|
133 (let ((total list))
|
|
134 (while (and (cdr list)
|
|
135 (not (equal (cadr list) elt)))
|
|
136 (setq list (cdr list)))
|
|
137 (when (cdr list)
|
|
138 (setcdr list (cddr list)))
|
|
139 total)))
|
|
140
|
|
141 ;; Delete the current line (and the next N lines).
|
|
142 (defmacro gnus-delete-line (&optional n)
|
|
143 `(delete-region (progn (beginning-of-line) (point))
|
|
144 (progn (forward-line ,(or n 1)) (point))))
|
|
145
|
|
146 (defun gnus-byte-code (func)
|
|
147 "Return a form that can be `eval'ed based on FUNC."
|
|
148 (let ((fval (symbol-function func)))
|
|
149 (if (compiled-function-p fval)
|
|
150 (let ((flist (append fval nil)))
|
|
151 (setcar flist 'byte-code)
|
|
152 flist)
|
|
153 (cons 'progn (cddr fval)))))
|
|
154
|
|
155 (defun gnus-extract-address-components (from)
|
|
156 (let (name address)
|
|
157 ;; First find the address - the thing with the @ in it. This may
|
|
158 ;; not be accurate in mail addresses, but does the trick most of
|
|
159 ;; the time in news messages.
|
|
160 (when (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
|
|
161 (setq address (substring from (match-beginning 0) (match-end 0))))
|
|
162 ;; Then we check whether the "name <address>" format is used.
|
|
163 (and address
|
|
164 ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
|
|
165 ;; Linear white space is not required.
|
|
166 (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
|
|
167 (and (setq name (substring from 0 (match-beginning 0)))
|
|
168 ;; Strip any quotes from the name.
|
|
169 (string-match "\".*\"" name)
|
|
170 (setq name (substring name 1 (1- (match-end 0))))))
|
|
171 ;; If not, then "address (name)" is used.
|
|
172 (or name
|
|
173 (and (string-match "(.+)" from)
|
|
174 (setq name (substring from (1+ (match-beginning 0))
|
|
175 (1- (match-end 0)))))
|
|
176 (and (string-match "()" from)
|
|
177 (setq name address))
|
|
178 ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
|
|
179 ;; XOVER might not support folded From headers.
|
|
180 (and (string-match "(.*" from)
|
|
181 (setq name (substring from (1+ (match-beginning 0))
|
|
182 (match-end 0)))))
|
|
183 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
|
|
184 (list (or name from) (or address from))))
|
|
185
|
|
186 (defun gnus-fetch-field (field)
|
|
187 "Return the value of the header FIELD of current article."
|
|
188 (save-excursion
|
|
189 (save-restriction
|
|
190 (let ((case-fold-search t)
|
|
191 (inhibit-point-motion-hooks t))
|
|
192 (nnheader-narrow-to-headers)
|
|
193 (message-fetch-field field)))))
|
|
194
|
|
195 (defun gnus-goto-colon ()
|
|
196 (beginning-of-line)
|
|
197 (search-forward ":" (gnus-point-at-eol) t))
|
|
198
|
|
199 (defun gnus-remove-text-with-property (prop)
|
|
200 "Delete all text in the current buffer with text property PROP."
|
|
201 (save-excursion
|
|
202 (goto-char (point-min))
|
|
203 (while (not (eobp))
|
|
204 (while (get-text-property (point) prop)
|
|
205 (delete-char 1))
|
|
206 (goto-char (next-single-property-change (point) prop nil (point-max))))))
|
|
207
|
|
208 (defun gnus-newsgroup-directory-form (newsgroup)
|
|
209 "Make hierarchical directory name from NEWSGROUP name."
|
|
210 (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
|
|
211 (len (length newsgroup))
|
|
212 idx)
|
|
213 ;; If this is a foreign group, we don't want to translate the
|
|
214 ;; entire name.
|
|
215 (if (setq idx (string-match ":" newsgroup))
|
|
216 (aset newsgroup idx ?/)
|
|
217 (setq idx 0))
|
|
218 ;; Replace all occurrences of `.' with `/'.
|
|
219 (while (< idx len)
|
|
220 (when (= (aref newsgroup idx) ?.)
|
|
221 (aset newsgroup idx ?/))
|
|
222 (setq idx (1+ idx)))
|
|
223 newsgroup))
|
|
224
|
|
225 (defun gnus-newsgroup-savable-name (group)
|
|
226 ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
|
|
227 ;; with dots.
|
|
228 (nnheader-replace-chars-in-string group ?/ ?.))
|
|
229
|
|
230 (defun gnus-string> (s1 s2)
|
|
231 (not (or (string< s1 s2)
|
|
232 (string= s1 s2))))
|
|
233
|
|
234 ;;; Time functions.
|
|
235
|
|
236 (defun gnus-days-between (date1 date2)
|
|
237 ;; Return the number of days between date1 and date2.
|
|
238 (- (gnus-day-number date1) (gnus-day-number date2)))
|
|
239
|
|
240 (defun gnus-day-number (date)
|
|
241 (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
|
|
242 (timezone-parse-date date))))
|
|
243 (timezone-absolute-from-gregorian
|
|
244 (nth 1 dat) (nth 2 dat) (car dat))))
|
|
245
|
|
246 (defun gnus-time-to-day (time)
|
|
247 "Convert TIME to day number."
|
|
248 (let ((tim (decode-time time)))
|
|
249 (timezone-absolute-from-gregorian
|
|
250 (nth 4 tim) (nth 3 tim) (nth 5 tim))))
|
|
251
|
|
252 (defun gnus-encode-date (date)
|
|
253 "Convert DATE to internal time."
|
|
254 (let* ((parse (timezone-parse-date date))
|
|
255 (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
|
|
256 (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
|
|
257 (encode-time (caddr time) (cadr time) (car time)
|
|
258 (caddr date) (cadr date) (car date) (nth 4 date))))
|
|
259
|
|
260 (defun gnus-time-minus (t1 t2)
|
|
261 "Subtract two internal times."
|
|
262 (let ((borrow (< (cadr t1) (cadr t2))))
|
|
263 (list (- (car t1) (car t2) (if borrow 1 0))
|
|
264 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
|
|
265
|
|
266 (defun gnus-time-less (t1 t2)
|
|
267 "Say whether time T1 is less than time T2."
|
|
268 (or (< (car t1) (car t2))
|
|
269 (and (= (car t1) (car t2))
|
|
270 (< (nth 1 t1) (nth 1 t2)))))
|
|
271
|
|
272 (defun gnus-file-newer-than (file date)
|
|
273 (let ((fdate (nth 5 (file-attributes file))))
|
|
274 (or (> (car fdate) (car date))
|
|
275 (and (= (car fdate) (car date))
|
|
276 (> (nth 1 fdate) (nth 1 date))))))
|
|
277
|
|
278 ;;; Keymap macros.
|
|
279
|
|
280 (defmacro gnus-local-set-keys (&rest plist)
|
|
281 "Set the keys in PLIST in the current keymap."
|
|
282 `(gnus-define-keys-1 (current-local-map) ',plist))
|
|
283
|
|
284 (defmacro gnus-define-keys (keymap &rest plist)
|
|
285 "Define all keys in PLIST in KEYMAP."
|
|
286 `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
|
|
287
|
|
288 (defmacro gnus-define-keys-safe (keymap &rest plist)
|
|
289 "Define all keys in PLIST in KEYMAP without overwriting previous definitions."
|
|
290 `(gnus-define-keys-1 (quote ,keymap) (quote ,plist) t))
|
|
291
|
|
292 (put 'gnus-define-keys 'lisp-indent-function 1)
|
|
293 (put 'gnus-define-keys-safe 'lisp-indent-function 1)
|
|
294 (put 'gnus-local-set-keys 'lisp-indent-function 1)
|
|
295
|
|
296 (defmacro gnus-define-keymap (keymap &rest plist)
|
|
297 "Define all keys in PLIST in KEYMAP."
|
|
298 `(gnus-define-keys-1 ,keymap (quote ,plist)))
|
|
299
|
|
300 (put 'gnus-define-keymap 'lisp-indent-function 1)
|
|
301
|
|
302 (defun gnus-define-keys-1 (keymap plist &optional safe)
|
|
303 (when (null keymap)
|
|
304 (error "Can't set keys in a null keymap"))
|
|
305 (cond ((symbolp keymap)
|
|
306 (setq keymap (symbol-value keymap)))
|
|
307 ((keymapp keymap))
|
|
308 ((listp keymap)
|
|
309 (set (car keymap) nil)
|
|
310 (define-prefix-command (car keymap))
|
|
311 (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
|
|
312 (setq keymap (symbol-value (car keymap)))))
|
|
313 (let (key)
|
|
314 (while plist
|
|
315 (when (symbolp (setq key (pop plist)))
|
|
316 (setq key (symbol-value key)))
|
|
317 (if (or (not safe)
|
|
318 (eq (lookup-key keymap key) 'undefined))
|
|
319 (define-key keymap key (pop plist))
|
|
320 (pop plist)))))
|
|
321
|
|
322 (defun gnus-completing-read (default prompt &rest args)
|
|
323 ;; Like `completing-read', except that DEFAULT is the default argument.
|
|
324 (let* ((prompt (if default
|
|
325 (concat prompt " (default " default ") ")
|
|
326 (concat prompt " ")))
|
|
327 (answer (apply 'completing-read prompt args)))
|
|
328 (if (or (null answer) (zerop (length answer)))
|
|
329 default
|
|
330 answer)))
|
|
331
|
|
332 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
|
|
333 ;; the echo area.
|
|
334 (defun gnus-y-or-n-p (prompt)
|
|
335 (prog1
|
|
336 (y-or-n-p prompt)
|
|
337 (message "")))
|
|
338
|
|
339 (defun gnus-yes-or-no-p (prompt)
|
|
340 (prog1
|
|
341 (yes-or-no-p prompt)
|
|
342 (message "")))
|
|
343
|
|
344 ;; I suspect there's a better way, but I haven't taken the time to do
|
|
345 ;; it yet. -erik selberg@cs.washington.edu
|
|
346 (defun gnus-dd-mmm (messy-date)
|
|
347 "Return a string like DD-MMM from a big messy string"
|
|
348 (let ((datevec (ignore-errors (timezone-parse-date messy-date))))
|
|
349 (if (not datevec)
|
|
350 "??-???"
|
|
351 (format "%2s-%s"
|
|
352 (condition-case ()
|
|
353 ;; Make sure leading zeroes are stripped.
|
|
354 (number-to-string (string-to-number (aref datevec 2)))
|
|
355 (error "??"))
|
|
356 (capitalize
|
|
357 (or (car
|
|
358 (nth (1- (string-to-number (aref datevec 1)))
|
|
359 timezone-months-assoc))
|
|
360 "???"))))))
|
|
361
|
|
362 (defmacro gnus-date-get-time (date)
|
|
363 "Convert DATE string to Emacs time.
|
|
364 Cache the result as a text property stored in DATE."
|
|
365 ;; Either return the cached value...
|
|
366 `(let ((d ,date))
|
|
367 (if (equal "" d)
|
|
368 '(0 0)
|
|
369 (or (get-text-property 0 'gnus-time d)
|
|
370 ;; or compute the value...
|
|
371 (let ((time (nnmail-date-to-time d)))
|
|
372 ;; and store it back in the string.
|
|
373 (put-text-property 0 1 'gnus-time time d)
|
|
374 time)))))
|
|
375
|
|
376 (defsubst gnus-time-iso8601 (time)
|
|
377 "Return a string of TIME in YYMMDDTHHMMSS format."
|
|
378 (format-time-string "%Y%m%dT%H%M%S" time))
|
|
379
|
|
380 (defun gnus-date-iso8601 (header)
|
|
381 "Convert the date field in HEADER to YYMMDDTHHMMSS"
|
|
382 (condition-case ()
|
|
383 (gnus-time-iso8601 (gnus-date-get-time (mail-header-date header)))
|
|
384 (error "")))
|
|
385
|
|
386 (defun gnus-mode-string-quote (string)
|
|
387 "Quote all \"%\"'s in STRING."
|
|
388 (save-excursion
|
|
389 (gnus-set-work-buffer)
|
|
390 (insert string)
|
|
391 (goto-char (point-min))
|
|
392 (while (search-forward "%" nil t)
|
|
393 (insert "%"))
|
|
394 (buffer-string)))
|
|
395
|
|
396 ;; Make a hash table (default and minimum size is 256).
|
|
397 ;; Optional argument HASHSIZE specifies the table size.
|
|
398 (defun gnus-make-hashtable (&optional hashsize)
|
|
399 (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 256) 256) 0))
|
|
400
|
|
401 ;; Make a number that is suitable for hashing; bigger than MIN and
|
|
402 ;; equal to some 2^x. Many machines (such as sparcs) do not have a
|
|
403 ;; hardware modulo operation, so they implement it in software. On
|
|
404 ;; many sparcs over 50% of the time to intern is spent in the modulo.
|
|
405 ;; Yes, it's slower than actually computing the hash from the string!
|
|
406 ;; So we use powers of 2 so people can optimize the modulo to a mask.
|
|
407 (defun gnus-create-hash-size (min)
|
|
408 (let ((i 1))
|
|
409 (while (< i min)
|
|
410 (setq i (* 2 i)))
|
|
411 i))
|
|
412
|
|
413 (defcustom gnus-verbose 7
|
|
414 "*Integer that says how verbose Gnus should be.
|
|
415 The higher the number, the more messages Gnus will flash to say what
|
|
416 it's doing. At zero, Gnus will be totally mute; at five, Gnus will
|
|
417 display most important messages; and at ten, Gnus will keep on
|
|
418 jabbering all the time."
|
|
419 :group 'gnus-start
|
|
420 :type 'integer)
|
|
421
|
|
422 ;; Show message if message has a lower level than `gnus-verbose'.
|
|
423 ;; Guideline for numbers:
|
|
424 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
|
|
425 ;; for things that take a long time, 7 - not very important messages
|
|
426 ;; on stuff, 9 - messages inside loops.
|
|
427 (defun gnus-message (level &rest args)
|
|
428 (if (<= level gnus-verbose)
|
|
429 (apply 'message args)
|
|
430 ;; We have to do this format thingy here even if the result isn't
|
|
431 ;; shown - the return value has to be the same as the return value
|
|
432 ;; from `message'.
|
|
433 (apply 'format args)))
|
|
434
|
|
435 (defun gnus-error (level &rest args)
|
|
436 "Beep an error if LEVEL is equal to or less than `gnus-verbose'."
|
|
437 (when (<= (floor level) gnus-verbose)
|
|
438 (apply 'message args)
|
|
439 (ding)
|
|
440 (let (duration)
|
|
441 (when (and (floatp level)
|
|
442 (not (zerop (setq duration (* 10 (- level (floor level)))))))
|
|
443 (sit-for duration))))
|
|
444 nil)
|
|
445
|
|
446 (defun gnus-split-references (references)
|
|
447 "Return a list of Message-IDs in REFERENCES."
|
|
448 (let ((beg 0)
|
|
449 ids)
|
|
450 (while (string-match "<[^>]+>" references beg)
|
|
451 (push (substring references (match-beginning 0) (setq beg (match-end 0)))
|
|
452 ids))
|
|
453 (nreverse ids)))
|
|
454
|
|
455 (defun gnus-parent-id (references &optional n)
|
|
456 "Return the last Message-ID in REFERENCES.
|
|
457 If N, return the Nth ancestor instead."
|
|
458 (when references
|
|
459 (let ((ids (inline (gnus-split-references references))))
|
|
460 (car (last ids (or n 1))))))
|
|
461
|
|
462 (defsubst gnus-buffer-live-p (buffer)
|
|
463 "Say whether BUFFER is alive or not."
|
|
464 (and buffer
|
|
465 (get-buffer buffer)
|
|
466 (buffer-name (get-buffer buffer))))
|
|
467
|
|
468 (defun gnus-horizontal-recenter ()
|
|
469 "Recenter the current buffer horizontally."
|
|
470 (if (< (current-column) (/ (window-width) 2))
|
|
471 (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
|
|
472 (let* ((orig (point))
|
|
473 (end (window-end (get-buffer-window (current-buffer) t)))
|
|
474 (max 0))
|
|
475 ;; Find the longest line currently displayed in the window.
|
|
476 (goto-char (window-start))
|
|
477 (while (and (not (eobp))
|
|
478 (< (point) end))
|
|
479 (end-of-line)
|
|
480 (setq max (max max (current-column)))
|
|
481 (forward-line 1))
|
|
482 (goto-char orig)
|
|
483 ;; Scroll horizontally to center (sort of) the point.
|
|
484 (if (> max (window-width))
|
|
485 (set-window-hscroll
|
|
486 (get-buffer-window (current-buffer) t)
|
|
487 (min (- (current-column) (/ (window-width) 3))
|
|
488 (+ 2 (- max (window-width)))))
|
|
489 (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
|
|
490 max)))
|
|
491
|
|
492 (defun gnus-read-event-char ()
|
|
493 "Get the next event."
|
|
494 (let ((event (read-event)))
|
|
495 ;; should be gnus-characterp, but this can't be called in XEmacs anyway
|
|
496 (cons (and (numberp event) event) event)))
|
|
497
|
|
498 (defun gnus-sortable-date (date)
|
|
499 "Make sortable string by string-lessp from DATE.
|
|
500 Timezone package is used."
|
|
501 (condition-case ()
|
|
502 (progn
|
|
503 (setq date (inline (timezone-fix-time
|
|
504 date nil
|
|
505 (aref (inline (timezone-parse-date date)) 4))))
|
|
506 (inline
|
|
507 (timezone-make-sortable-date
|
|
508 (aref date 0) (aref date 1) (aref date 2)
|
|
509 (inline
|
|
510 (timezone-make-time-string
|
|
511 (aref date 3) (aref date 4) (aref date 5))))))
|
|
512 (error "")))
|
|
513
|
|
514 (defun gnus-copy-file (file &optional to)
|
|
515 "Copy FILE to TO."
|
|
516 (interactive
|
|
517 (list (read-file-name "Copy file: " default-directory)
|
|
518 (read-file-name "Copy file to: " default-directory)))
|
|
519 (unless to
|
|
520 (setq to (read-file-name "Copy file to: " default-directory)))
|
|
521 (when (file-directory-p to)
|
|
522 (setq to (concat (file-name-as-directory to)
|
|
523 (file-name-nondirectory file))))
|
|
524 (copy-file file to))
|
|
525
|
|
526 (defun gnus-kill-all-overlays ()
|
|
527 "Delete all overlays in the current buffer."
|
|
528 (unless gnus-xemacs
|
|
529 (let* ((overlayss (overlay-lists))
|
|
530 (buffer-read-only nil)
|
|
531 (overlays (nconc (car overlayss) (cdr overlayss))))
|
|
532 (while overlays
|
|
533 (delete-overlay (pop overlays))))))
|
|
534
|
|
535 (defvar gnus-work-buffer " *gnus work*")
|
|
536
|
|
537 (defun gnus-set-work-buffer ()
|
|
538 "Put point in the empty Gnus work buffer."
|
|
539 (if (get-buffer gnus-work-buffer)
|
|
540 (progn
|
|
541 (set-buffer gnus-work-buffer)
|
|
542 (erase-buffer))
|
|
543 (set-buffer (get-buffer-create gnus-work-buffer))
|
|
544 (kill-all-local-variables)
|
|
545 (buffer-disable-undo (current-buffer))))
|
|
546
|
|
547 (defmacro gnus-group-real-name (group)
|
|
548 "Find the real name of a foreign newsgroup."
|
|
549 `(let ((gname ,group))
|
|
550 (if (string-match "^[^:]+:" gname)
|
|
551 (substring gname (match-end 0))
|
|
552 gname)))
|
|
553
|
|
554 (defun gnus-make-sort-function (funs)
|
|
555 "Return a composite sort condition based on the functions in FUNC."
|
|
556 (cond
|
|
557 ((not (listp funs)) funs)
|
|
558 ((null funs) funs)
|
|
559 ((cdr funs)
|
|
560 `(lambda (t1 t2)
|
|
561 ,(gnus-make-sort-function-1 (reverse funs))))
|
|
562 (t
|
|
563 (car funs))))
|
|
564
|
|
565 (defun gnus-make-sort-function-1 (funs)
|
|
566 "Return a composite sort condition based on the functions in FUNC."
|
|
567 (if (cdr funs)
|
|
568 `(or (,(car funs) t1 t2)
|
|
569 (and (not (,(car funs) t2 t1))
|
|
570 ,(gnus-make-sort-function-1 (cdr funs))))
|
|
571 `(,(car funs) t1 t2)))
|
|
572
|
|
573 (defun gnus-turn-off-edit-menu (type)
|
|
574 "Turn off edit menu in `gnus-TYPE-mode-map'."
|
|
575 (define-key (symbol-value (intern (format "gnus-%s-mode-map" type)))
|
|
576 [menu-bar edit] 'undefined))
|
|
577
|
|
578 (defun gnus-prin1 (form)
|
|
579 "Use `prin1' on FORM in the current buffer.
|
|
580 Bind `print-quoted' to t while printing."
|
|
581 (let ((print-quoted t)
|
|
582 print-level print-length)
|
|
583 (prin1 form (current-buffer))))
|
|
584
|
|
585 (defun gnus-prin1-to-string (form)
|
|
586 "The same as `prin1', but but `print-quoted' to t."
|
|
587 (let ((print-quoted t))
|
|
588 (prin1-to-string form)))
|
|
589
|
|
590 (defun gnus-make-directory (directory)
|
|
591 "Make DIRECTORY (and all its parents) if it doesn't exist."
|
|
592 (when (and directory
|
|
593 (not (file-exists-p directory)))
|
|
594 (make-directory directory t))
|
|
595 t)
|
|
596
|
|
597 (defun gnus-write-buffer (file)
|
|
598 "Write the current buffer's contents to FILE."
|
|
599 ;; Make sure the directory exists.
|
|
600 (gnus-make-directory (file-name-directory file))
|
|
601 ;; Write the buffer.
|
|
602 (write-region (point-min) (point-max) file nil 'quietly))
|
|
603
|
|
604 (defmacro gnus-delete-assq (key list)
|
|
605 `(let ((listval (eval ,list)))
|
|
606 (setq ,list (delq (assq ,key listval) listval))))
|
|
607
|
|
608 (defmacro gnus-delete-assoc (key list)
|
|
609 `(let ((listval ,list))
|
|
610 (setq ,list (delq (assoc ,key listval) listval))))
|
|
611
|
|
612 (defun gnus-delete-file (file)
|
|
613 "Delete FILE if it exists."
|
|
614 (when (file-exists-p file)
|
|
615 (delete-file file)))
|
|
616
|
|
617 (defun gnus-strip-whitespace (string)
|
|
618 "Return STRING stripped of all whitespace."
|
|
619 (while (string-match "[\r\n\t ]+" string)
|
|
620 (setq string (replace-match "" t t string)))
|
|
621 string)
|
|
622
|
|
623 (defun gnus-put-text-property-excluding-newlines (beg end prop val)
|
|
624 "The same as `put-text-property', but don't put this prop on any newlines in the region."
|
|
625 (save-match-data
|
|
626 (save-excursion
|
|
627 (save-restriction
|
|
628 (goto-char beg)
|
|
629 (while (re-search-forward "[ \t]*\n" end 'move)
|
|
630 (put-text-property beg (match-beginning 0) prop val)
|
|
631 (setq beg (point)))
|
|
632 (put-text-property beg (point) prop val)))))
|
|
633
|
|
634 ;;; Protected and atomic operations. dmoore@ucsd.edu 21.11.1996
|
|
635 ;;; The primary idea here is to try to protect internal datastructures
|
|
636 ;;; from becoming corrupted when the user hits C-g, or if a hook or
|
|
637 ;;; similar blows up. Often in Gnus multiple tables/lists need to be
|
|
638 ;;; updated at the same time, or information can be lost.
|
|
639
|
|
640 (defvar gnus-atomic-be-safe t
|
|
641 "If t, certain operations will be protected from interruption by C-g.")
|
|
642
|
|
643 (defmacro gnus-atomic-progn (&rest forms)
|
|
644 "Evaluate FORMS atomically, which means to protect the evaluation
|
|
645 from being interrupted by the user. An error from the forms themselves
|
|
646 will return without finishing the operation. Since interrupts from
|
|
647 the user are disabled, it is recommended that only the most minimal
|
|
648 operations are performed by FORMS. If you wish to assign many
|
|
649 complicated values atomically, compute the results into temporary
|
|
650 variables and then do only the assignment atomically."
|
|
651 `(let ((inhibit-quit gnus-atomic-be-safe))
|
|
652 ,@forms))
|
|
653
|
|
654 (put 'gnus-atomic-progn 'lisp-indent-function 0)
|
|
655
|
|
656 (defmacro gnus-atomic-progn-assign (protect &rest forms)
|
|
657 "Evaluate FORMS, but insure that the variables listed in PROTECT
|
|
658 are not changed if anything in FORMS signals an error or otherwise
|
|
659 non-locally exits. The variables listed in PROTECT are updated atomically.
|
|
660 It is safe to use gnus-atomic-progn-assign with long computations.
|
|
661
|
|
662 Note that if any of the symbols in PROTECT were unbound, they will be
|
|
663 set to nil on a sucessful assignment. In case of an error or other
|
|
664 non-local exit, it will still be unbound."
|
|
665 (let* ((temp-sym-map (mapcar (lambda (x) (list (make-symbol
|
|
666 (concat (symbol-name x)
|
|
667 "-tmp"))
|
|
668 x))
|
|
669 protect))
|
|
670 (sym-temp-map (mapcar (lambda (x) (list (cadr x) (car x)))
|
|
671 temp-sym-map))
|
|
672 (temp-sym-let (mapcar (lambda (x) (list (car x)
|
|
673 `(and (boundp ',(cadr x))
|
|
674 ,(cadr x))))
|
|
675 temp-sym-map))
|
|
676 (sym-temp-let sym-temp-map)
|
|
677 (temp-sym-assign (apply 'append temp-sym-map))
|
|
678 (sym-temp-assign (apply 'append sym-temp-map))
|
|
679 (result (make-symbol "result-tmp")))
|
|
680 `(let (,@temp-sym-let
|
|
681 ,result)
|
|
682 (let ,sym-temp-let
|
|
683 (setq ,result (progn ,@forms))
|
|
684 (setq ,@temp-sym-assign))
|
|
685 (let ((inhibit-quit gnus-atomic-be-safe))
|
|
686 (setq ,@sym-temp-assign))
|
|
687 ,result)))
|
|
688
|
|
689 (put 'gnus-atomic-progn-assign 'lisp-indent-function 1)
|
|
690 ;(put 'gnus-atomic-progn-assign 'edebug-form-spec '(sexp body))
|
|
691
|
|
692 (defmacro gnus-atomic-setq (&rest pairs)
|
|
693 "Similar to setq, except that the real symbols are only assigned when
|
|
694 there are no errors. And when the real symbols are assigned, they are
|
|
695 done so atomically. If other variables might be changed via side-effect,
|
|
696 see gnus-atomic-progn-assign. It is safe to use gnus-atomic-setq
|
|
697 with potentially long computations."
|
|
698 (let ((tpairs pairs)
|
|
699 syms)
|
|
700 (while tpairs
|
|
701 (push (car tpairs) syms)
|
|
702 (setq tpairs (cddr tpairs)))
|
|
703 `(gnus-atomic-progn-assign ,syms
|
|
704 (setq ,@pairs))))
|
|
705
|
|
706 ;(put 'gnus-atomic-setq 'edebug-form-spec '(body))
|
|
707
|
|
708
|
|
709 ;;; Functions for saving to babyl/mail files.
|
|
710
|
|
711 (defvar rmail-default-rmail-file)
|
|
712 (defun gnus-output-to-rmail (filename &optional ask)
|
|
713 "Append the current article to an Rmail file named FILENAME."
|
|
714 (require 'rmail)
|
|
715 ;; Most of these codes are borrowed from rmailout.el.
|
|
716 (setq filename (expand-file-name filename))
|
|
717 (setq rmail-default-rmail-file filename)
|
|
718 (let ((artbuf (current-buffer))
|
|
719 (tmpbuf (get-buffer-create " *Gnus-output*")))
|
|
720 (save-excursion
|
|
721 (or (get-file-buffer filename)
|
|
722 (file-exists-p filename)
|
|
723 (if (or (not ask)
|
|
724 (gnus-yes-or-no-p
|
|
725 (concat "\"" filename "\" does not exist, create it? ")))
|
|
726 (let ((file-buffer (create-file-buffer filename)))
|
|
727 (save-excursion
|
|
728 (set-buffer file-buffer)
|
|
729 (rmail-insert-rmail-file-header)
|
|
730 (let ((require-final-newline nil))
|
|
731 (gnus-write-buffer filename)))
|
|
732 (kill-buffer file-buffer))
|
|
733 (error "Output file does not exist")))
|
|
734 (set-buffer tmpbuf)
|
|
735 (erase-buffer)
|
|
736 (insert-buffer-substring artbuf)
|
|
737 (gnus-convert-article-to-rmail)
|
|
738 ;; Decide whether to append to a file or to an Emacs buffer.
|
|
739 (let ((outbuf (get-file-buffer filename)))
|
|
740 (if (not outbuf)
|
|
741 (append-to-file (point-min) (point-max) filename)
|
|
742 ;; File has been visited, in buffer OUTBUF.
|
|
743 (set-buffer outbuf)
|
|
744 (let ((buffer-read-only nil)
|
|
745 (msg (and (boundp 'rmail-current-message)
|
|
746 (symbol-value 'rmail-current-message))))
|
|
747 ;; If MSG is non-nil, buffer is in RMAIL mode.
|
|
748 (when msg
|
|
749 (widen)
|
|
750 (narrow-to-region (point-max) (point-max)))
|
|
751 (insert-buffer-substring tmpbuf)
|
|
752 (when msg
|
|
753 (goto-char (point-min))
|
|
754 (widen)
|
|
755 (search-backward "\^_")
|
|
756 (narrow-to-region (point) (point-max))
|
|
757 (goto-char (1+ (point-min)))
|
|
758 (rmail-count-new-messages t)
|
|
759 (rmail-show-message msg))))))
|
|
760 (kill-buffer tmpbuf)))
|
|
761
|
|
762 (defun gnus-output-to-mail (filename &optional ask)
|
|
763 "Append the current article to a mail file named FILENAME."
|
|
764 (setq filename (expand-file-name filename))
|
|
765 (let ((artbuf (current-buffer))
|
|
766 (tmpbuf (get-buffer-create " *Gnus-output*")))
|
|
767 (save-excursion
|
|
768 ;; Create the file, if it doesn't exist.
|
|
769 (when (and (not (get-file-buffer filename))
|
|
770 (not (file-exists-p filename)))
|
|
771 (if (or (not ask)
|
|
772 (gnus-y-or-n-p
|
|
773 (concat "\"" filename "\" does not exist, create it? ")))
|
|
774 (let ((file-buffer (create-file-buffer filename)))
|
|
775 (save-excursion
|
|
776 (set-buffer file-buffer)
|
|
777 (let ((require-final-newline nil))
|
|
778 (gnus-write-buffer filename)))
|
|
779 (kill-buffer file-buffer))
|
|
780 (error "Output file does not exist")))
|
|
781 (set-buffer tmpbuf)
|
|
782 (erase-buffer)
|
|
783 (insert-buffer-substring artbuf)
|
|
784 (goto-char (point-min))
|
|
785 (if (looking-at "From ")
|
|
786 (forward-line 1)
|
|
787 (insert "From nobody " (current-time-string) "\n"))
|
|
788 (let (case-fold-search)
|
|
789 (while (re-search-forward "^From " nil t)
|
|
790 (beginning-of-line)
|
|
791 (insert ">")))
|
|
792 ;; Decide whether to append to a file or to an Emacs buffer.
|
|
793 (let ((outbuf (get-file-buffer filename)))
|
|
794 (if (not outbuf)
|
|
795 (let ((buffer-read-only nil))
|
|
796 (save-excursion
|
|
797 (goto-char (point-max))
|
|
798 (forward-char -2)
|
|
799 (unless (looking-at "\n\n")
|
|
800 (goto-char (point-max))
|
|
801 (unless (bolp)
|
|
802 (insert "\n"))
|
|
803 (insert "\n"))
|
|
804 (goto-char (point-max))
|
|
805 (append-to-file (point-min) (point-max) filename)))
|
|
806 ;; File has been visited, in buffer OUTBUF.
|
|
807 (set-buffer outbuf)
|
|
808 (let ((buffer-read-only nil))
|
|
809 (goto-char (point-max))
|
|
810 (unless (eobp)
|
|
811 (insert "\n"))
|
|
812 (insert "\n")
|
|
813 (insert-buffer-substring tmpbuf)))))
|
|
814 (kill-buffer tmpbuf)))
|
|
815
|
|
816 (defun gnus-convert-article-to-rmail ()
|
|
817 "Convert article in current buffer to Rmail message format."
|
|
818 (let ((buffer-read-only nil))
|
|
819 ;; Convert article directly into Babyl format.
|
|
820 (goto-char (point-min))
|
|
821 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
|
|
822 (while (search-forward "\n\^_" nil t) ;single char
|
|
823 (replace-match "\n^_" t t)) ;2 chars: "^" and "_"
|
|
824 (goto-char (point-max))
|
|
825 (insert "\^_")))
|
|
826
|
|
827 (provide 'gnus-util)
|
|
828
|
|
829 ;;; gnus-util.el ends here
|