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