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