0
|
1 ;;; Miscellaneous functions for VM
|
70
|
2 ;;; Copyright (C) 1989, 1990, 1991, 1993, 1994 Kyle E. Jones
|
0
|
3 ;;;
|
|
4 ;;; This program is free software; you can redistribute it and/or modify
|
|
5 ;;; it under the terms of the GNU General Public License as published by
|
|
6 ;;; the Free Software Foundation; either version 1, or (at your option)
|
|
7 ;;; any later version.
|
|
8 ;;;
|
|
9 ;;; This program is distributed in the hope that it will be useful,
|
|
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 ;;; GNU General Public License for more details.
|
|
13 ;;;
|
|
14 ;;; You should have received a copy of the GNU General Public License
|
|
15 ;;; along with this program; if not, write to the Free Software
|
|
16 ;;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
17
|
|
18 (provide 'vm-misc)
|
|
19
|
|
20 (defun vm-delete-non-matching-strings (regexp list &optional destructively)
|
|
21 "Delete strings matching REGEXP from LIST.
|
|
22 Optional third arg non-nil means to destructively alter LIST, instead of
|
|
23 working on a copy.
|
|
24
|
|
25 The new version of the list, minus the deleted strings, is returned."
|
|
26 (or destructively (setq list (copy-sequence list)))
|
|
27 (let ((curr list) (prev nil))
|
|
28 (while curr
|
|
29 (if (string-match regexp (car curr))
|
|
30 (setq prev curr
|
|
31 curr (cdr curr))
|
|
32 (if (null prev)
|
|
33 (setq list (cdr list)
|
|
34 curr list)
|
|
35 (setcdr prev (cdr curr))
|
|
36 (setq curr (cdr curr)))))
|
|
37 list ))
|
|
38
|
|
39 (defun vm-parse (string regexp &optional matchn)
|
|
40 (or matchn (setq matchn 1))
|
|
41 (let (list)
|
|
42 (store-match-data nil)
|
|
43 (while (string-match regexp string (match-end 0))
|
|
44 (setq list (cons (substring string (match-beginning matchn)
|
|
45 (match-end matchn)) list)))
|
|
46 (nreverse list)))
|
|
47
|
|
48 (defun vm-parse-addresses (string)
|
|
49 (if (null string)
|
|
50 ()
|
|
51 (let (work-buffer)
|
|
52 (save-excursion
|
|
53 (unwind-protect
|
|
54 (let (list start s char)
|
|
55 (setq work-buffer (generate-new-buffer "*vm-work*"))
|
|
56 (set-buffer work-buffer)
|
|
57 (insert string)
|
|
58 (goto-char (point-min))
|
|
59 (skip-chars-forward "\t\f\n\r ")
|
|
60 (setq start (point))
|
|
61 (while (not (eobp))
|
|
62 (skip-chars-forward "^\"\\,(")
|
|
63 (setq char (following-char))
|
|
64 (cond ((= char ?\\)
|
|
65 (forward-char 1)
|
|
66 (if (not (eobp))
|
|
67 (forward-char 1)))
|
|
68 ((= char ?,)
|
|
69 (setq s (buffer-substring start (point)))
|
|
70 (if (or (null (string-match "^[\t\f\n\r ]+$" s))
|
|
71 (not (string= s "")))
|
|
72 (setq list (cons s list)))
|
76
|
73 (skip-chars-forward ",\t\f\n\r ")
|
0
|
74 (setq start (point)))
|
|
75 ((= char ?\")
|
|
76 (re-search-forward "[^\\]\"" nil 0))
|
|
77 ((= char ?\()
|
|
78 (let ((parens 1))
|
|
79 (forward-char 1)
|
|
80 (while (and (not (eobp)) (not (zerop parens)))
|
76
|
81 (re-search-forward "[()]" nil 0)
|
|
82 (cond ((or (eobp)
|
|
83 (= (char-after (- (point) 2)) ?\\)))
|
0
|
84 ((= (preceding-char) ?\()
|
|
85 (setq parens (1+ parens)))
|
76
|
86 (t
|
0
|
87 (setq parens (1- parens)))))))))
|
|
88 (setq s (buffer-substring start (point)))
|
|
89 (if (and (null (string-match "^[\t\f\n\r ]+$" s))
|
|
90 (not (string= s "")))
|
|
91 (setq list (cons s list)))
|
|
92 (nreverse list)) ; jwz: fixed order
|
|
93 (and work-buffer (kill-buffer work-buffer)))))))
|
|
94
|
|
95 (defun vm-write-string (where string)
|
|
96 (if (bufferp where)
|
|
97 (vm-save-buffer-excursion
|
|
98 (set-buffer where)
|
|
99 (goto-char (point-max))
|
|
100 (insert string))
|
|
101 (let ((temp-buffer nil))
|
|
102 (unwind-protect
|
|
103 (save-excursion
|
|
104 (setq temp-buffer (generate-new-buffer "*vm-work*"))
|
|
105 (set-buffer temp-buffer)
|
|
106 (insert string)
|
|
107 (write-region (point-min) (point-max) where t 'quiet))
|
|
108 (and temp-buffer (kill-buffer temp-buffer))))))
|
|
109
|
|
110 (defmacro vm-marker (pos &optional buffer)
|
|
111 (list 'set-marker '(make-marker) pos buffer))
|
|
112
|
|
113 (defmacro vm-increment (variable)
|
|
114 (list 'setq variable (list '1+ variable)))
|
|
115
|
|
116 (defmacro vm-decrement (variable)
|
|
117 (list 'setq variable (list '1- variable)))
|
|
118
|
|
119 (defmacro vm-select-folder-buffer ()
|
|
120 '(and vm-mail-buffer
|
|
121 (or (buffer-name vm-mail-buffer)
|
|
122 (error "Folder buffer has been killed."))
|
|
123 (set-buffer vm-mail-buffer)))
|
|
124
|
|
125 (defun vm-check-for-killed-summary ()
|
|
126 (and (bufferp vm-summary-buffer) (null (buffer-name vm-summary-buffer))
|
|
127 (let ((mp vm-message-list))
|
|
128 (setq vm-summary-buffer nil)
|
|
129 (while mp
|
|
130 (vm-set-su-start-of (car mp) nil)
|
|
131 (vm-set-su-end-of (car mp) nil)
|
|
132 (setq mp (cdr mp))))))
|
|
133
|
|
134 (defun vm-check-for-killed-folder ()
|
|
135 (and (bufferp vm-mail-buffer) (null (buffer-name vm-mail-buffer))
|
|
136 (setq vm-mail-buffer nil)))
|
|
137
|
|
138 (defmacro vm-error-if-folder-read-only ()
|
|
139 '(while vm-folder-read-only
|
|
140 (signal 'folder-read-only (list (current-buffer)))))
|
|
141
|
76
|
142 (put 'folder-read-only 'error-conditions '(folder-read-only error))
|
|
143 (put 'folder-read-only 'error-message "Folder is read-only")
|
0
|
144
|
|
145 (defmacro vm-error-if-virtual-folder ()
|
|
146 '(and (eq major-mode 'vm-virtual-mode)
|
|
147 (error "%s cannot be applied to virtual folders." this-command)))
|
|
148
|
|
149 (defmacro vm-build-threads-if-unbuilt ()
|
|
150 '(if (null vm-thread-obarray)
|
|
151 (vm-build-threads nil)))
|
|
152
|
|
153 (defun vm-abs (n) (if (< n 0) (- n) n))
|
|
154
|
|
155 ;; save-restriction flubs restoring the clipping region if you
|
|
156 ;; (widen) and modify text outside the old region.
|
|
157 ;; This should do it right.
|
|
158 (defmacro vm-save-restriction (&rest forms)
|
|
159 (let ((vm-sr-clip (make-symbol "vm-sr-clip"))
|
|
160 (vm-sr-min (make-symbol "vm-sr-min"))
|
|
161 (vm-sr-max (make-symbol "vm-sr-max")))
|
|
162 (list 'let (list (list vm-sr-clip '(> (buffer-size)
|
|
163 (- (point-max) (point-min))))
|
|
164 ;; this shouldn't be necessary but the
|
|
165 ;; byte-compiler turns these into interned symbols
|
|
166 ;; which utterly defeats the purpose of the
|
|
167 ;; make-symbol calls above. Soooo, until the compiler
|
|
168 ;; is fixed, these must be made into (let ...)
|
|
169 ;; temporaries so that nested calls to this macros
|
|
170 ;; won't misbehave.
|
|
171 vm-sr-min vm-sr-max)
|
|
172 (list 'and vm-sr-clip
|
|
173 (list 'setq vm-sr-min '(set-marker (make-marker) (point-min)))
|
|
174 (list 'setq vm-sr-max '(set-marker (make-marker) (point-max))))
|
|
175 (list 'unwind-protect (cons 'progn forms)
|
|
176 '(widen)
|
|
177 (list 'and vm-sr-clip
|
|
178 (list 'progn
|
|
179 (list 'narrow-to-region vm-sr-min vm-sr-max)
|
|
180 (list 'set-marker vm-sr-min nil)
|
|
181 (list 'set-marker vm-sr-max nil)))))))
|
|
182
|
|
183 (defmacro vm-save-buffer-excursion (&rest forms)
|
|
184 (list 'let '((vm-sbe-buffer (current-buffer)))
|
|
185 (list 'unwind-protect
|
|
186 (cons 'progn forms)
|
|
187 '(and (not (eq vm-sbe-buffer (current-buffer)))
|
|
188 (buffer-name vm-sbe-buffer)
|
|
189 (set-buffer vm-sbe-buffer)))))
|
|
190
|
|
191 (defun vm-last (list) (while (cdr-safe list) (setq list (cdr list))) list)
|
|
192
|
|
193 (defun vm-vector-to-list (vector)
|
|
194 (let ((i (1- (length vector)))
|
|
195 list)
|
|
196 (while (>= i 0)
|
|
197 (setq list (cons (aref vector i) list))
|
|
198 (vm-decrement i))
|
|
199 list ))
|
|
200
|
|
201 (defun vm-extend-vector (vector length &optional fill)
|
|
202 (let ((vlength (length vector)))
|
|
203 (if (< vlength length)
|
|
204 (apply 'vector (nconc (vm-vector-to-list vector)
|
|
205 (make-list (- length vlength) fill)))
|
|
206 vector )))
|
|
207
|
70
|
208 (defun vm-obarray-to-string-list (obarray)
|
0
|
209 (let ((list nil))
|
|
210 (mapatoms (function (lambda (s) (setq list (cons (symbol-name s) list))))
|
70
|
211 obarray)
|
0
|
212 list ))
|
|
213
|
|
214 (defun vm-mapcar (function &rest lists)
|
|
215 (let (arglist result)
|
|
216 (while (car lists)
|
|
217 (setq arglist (mapcar 'car lists))
|
|
218 (setq result (cons (apply function arglist) result))
|
|
219 (setq lists (mapcar 'cdr lists)))
|
|
220 (nreverse result)))
|
|
221
|
|
222 (defun vm-mapc (function &rest lists)
|
|
223 (let (arglist)
|
|
224 (while (car lists)
|
|
225 (setq arglist (mapcar 'car lists))
|
|
226 (apply function arglist)
|
|
227 (setq lists (mapcar 'cdr lists)))))
|
|
228
|
|
229 (defun vm-delete (predicate list &optional reverse)
|
|
230 (let ((p list) (reverse (if reverse 'not 'identity)) prev)
|
|
231 (while p
|
|
232 (if (funcall reverse (funcall predicate (car p)))
|
|
233 (if (null prev)
|
|
234 (setq list (cdr list) p list)
|
|
235 (setcdr prev (cdr p))
|
|
236 (setq p (cdr p)))
|
|
237 (setq prev p p (cdr p))))
|
|
238 list ))
|
|
239
|
|
240 (defun vm-delete-duplicates (list &optional all hack-addresses)
|
|
241 "Delete duplicate equivalent strings from the list.
|
|
242 If ALL is t, then if there is more than one occurrence of a string in the list,
|
|
243 then all occurrences of it are removed instead of just the subsequent ones.
|
|
244 If HACK-ADDRESSES is t, then the strings are considered to be mail addresses,
|
|
245 and only the address part is compared (so that \"Name <foo>\" and \"foo\"
|
|
246 would be considered to be equivalent.)"
|
|
247 (let ((hashtable vm-delete-duplicates-obarray)
|
|
248 (new-list nil)
|
|
249 sym-string sym)
|
|
250 (fillarray hashtable 0)
|
|
251 (while list
|
|
252 (setq sym-string
|
|
253 (if hack-addresses
|
|
254 (nth 1 (funcall vm-chop-full-name-function (car list)))
|
|
255 (car list))
|
|
256 sym (intern sym-string hashtable))
|
|
257 (if (boundp sym)
|
|
258 (and all (setcar (symbol-value sym) nil))
|
|
259 (setq new-list (cons (car list) new-list))
|
|
260 (set sym new-list))
|
|
261 (setq list (cdr list)))
|
|
262 (delq nil (nreverse new-list))))
|
|
263
|
|
264 (defun vm-member-0 (thing list)
|
|
265 (catch 'done
|
|
266 (while list
|
|
267 (and (equal (car list) thing)
|
|
268 (throw 'done list))
|
|
269 (setq list (cdr list)))
|
|
270 nil ))
|
|
271
|
|
272 (fset 'vm-member (symbol-function (if (fboundp 'member) 'member 'vm-member-0)))
|
|
273
|
|
274 (defun vm-delqual (ob list)
|
|
275 (let ((prev nil)
|
|
276 (curr list))
|
|
277 (while curr
|
|
278 (if (not (equal ob (car curr)))
|
|
279 (setq prev curr
|
|
280 curr (cdr curr))
|
|
281 (if (null prev)
|
|
282 (setq list (cdr list)
|
|
283 curr list)
|
|
284 (setq curr (cdr curr))
|
|
285 (setcdr prev curr))))
|
|
286 list ))
|
|
287
|
|
288 (defun vm-copy-local-variables (buffer &rest variables)
|
|
289 (let ((values (mapcar 'symbol-value variables)))
|
|
290 (save-excursion
|
|
291 (set-buffer buffer)
|
|
292 (vm-mapc 'set variables values))))
|
|
293
|
76
|
294 (put 'folder-empty 'error-conditions '(folder-empty error))
|
|
295 (put 'folder-empty 'error-message "Folder is empty")
|
|
296 (put 'unrecognized-folder-type 'error-conditions
|
|
297 '(unrecognized-folder-type error))
|
|
298 (put 'unrecognized-folder-type 'error-message "Unrecognized folder type")
|
0
|
299
|
|
300 (defun vm-error-if-folder-empty ()
|
|
301 (while (null vm-message-list)
|
|
302 (if vm-folder-type
|
|
303 (signal 'unrecognized-folder-type nil)
|
|
304 (signal 'folder-empty nil))))
|
|
305
|
|
306 (defun vm-copy (object)
|
|
307 (cond ((consp object)
|
|
308 (let (return-value cons)
|
|
309 (setq return-value (cons (vm-copy (car object)) nil)
|
|
310 cons return-value
|
|
311 object (cdr object))
|
|
312 (while (consp object)
|
|
313 (setcdr cons (cons (vm-copy (car object)) nil))
|
|
314 (setq cons (cdr cons)
|
|
315 object (cdr object)))
|
|
316 (setcdr cons object)
|
|
317 return-value ))
|
|
318 ((vectorp object) (apply 'vector (mapcar 'vm-copy object)))
|
|
319 ((stringp object) (copy-sequence object))
|
|
320 (t object)))
|
|
321
|
70
|
322 (defun vm-xemacs-p ()
|
|
323 (let ((case-fold-search nil))
|
|
324 (string-match "XEmacs" emacs-version)))
|
|
325
|
|
326 (defun vm-fsfemacs-19-p ()
|
|
327 (and (string-match "^19" emacs-version)
|
|
328 (not (string-match "XEmacs\\|Lucid" emacs-version))))
|
|
329
|
|
330 ;; make-frame might be defined and still not work. This would
|
|
331 ;; be true since the user could be running on a tty and using
|
|
332 ;; XEmacs 19.12, or using FSF Emacs 19.28 (or prior FSF Emacs versions).
|
|
333 ;;
|
|
334 ;; make-frame works on ttys in FSF Emacs 19.29, but other than
|
|
335 ;; looking at the version number I don't know a sane way to
|
|
336 ;; test for it without just running make-frame. I'll just
|
|
337 ;; let it not work for now... someone will complain eventually
|
|
338 ;; and I'll think of something.
|
|
339 (defun vm-multiple-frames-possible-p ()
|
|
340 (or (and (boundp 'window-system) (not (eq window-system nil)))
|
|
341 (and (fboundp 'device-type) (eq (device-type) 'x))))
|
|
342
|
|
343 (defun vm-mouse-support-possible-p ()
|
|
344 (vm-multiple-frames-possible-p))
|
0
|
345
|
|
346 (defun vm-menu-support-possible-p ()
|
76
|
347 (or (and (boundp 'window-system)
|
|
348 (or (eq window-system 'x)
|
|
349 (eq window-system 'win32)))
|
70
|
350 (and (fboundp 'device-type) (eq (device-type) 'x))))
|
|
351
|
0
|
352 (defun vm-toolbar-support-possible-p ()
|
70
|
353 (and (vm-xemacs-p)
|
|
354 (vm-multiple-frames-possible-p)
|
|
355 (featurep 'toolbar)))
|
24
|
356
|
0
|
357 (defun vm-run-message-hook (message &optional hook-variable)
|
|
358 (save-excursion
|
|
359 (set-buffer (vm-buffer-of message))
|
|
360 (vm-save-restriction
|
|
361 (widen)
|
|
362 (save-excursion
|
|
363 (narrow-to-region (vm-headers-of message) (vm-text-end-of message))
|
|
364 (run-hooks hook-variable)))))
|
|
365
|
|
366 (defun vm-error-free-call (function &rest args)
|
|
367 (condition-case nil
|
|
368 (apply function args)
|
|
369 (error nil)))
|
|
370
|
76
|
371 (put 'beginning-of-folder 'error-conditions '(beginning-of-folder error))
|
|
372 (put 'beginning-of-folder 'error-message "Beginning of folder")
|
|
373 (put 'end-of-folder 'error-conditions '(end-of-folder error))
|
|
374 (put 'end-of-folder 'error-message "End of folder")
|
0
|
375
|
|
376 (defun vm-trace (&rest args)
|
|
377 (save-excursion
|
|
378 (set-buffer (get-buffer-create "*vm-trace*"))
|
|
379 (apply 'insert args)))
|
|
380
|
|
381 (defun vm-timezone-make-date-sortable (string)
|
|
382 (or (cdr (assq string vm-sortable-date-alist))
|
|
383 (let ((vect (vm-parse-date string))
|
|
384 (date (vm-parse (current-time-string) " *\\([^ ]+\\)")))
|
|
385 ;; if specified date is incomplete fill in the holes
|
|
386 ;; with useful information, defaulting to the current
|
|
387 ;; date and timezone for everything except hh:mm:ss which
|
|
388 ;; defaults to midnight.
|
|
389 (if (equal (aref vect 1) "")
|
|
390 (aset vect 1 (nth 2 date)))
|
|
391 (if (equal (aref vect 2) "")
|
|
392 (aset vect 2 (nth 1 date)))
|
|
393 (if (equal (aref vect 3) "")
|
|
394 (aset vect 3 (nth 4 date)))
|
|
395 (if (equal (aref vect 4) "")
|
|
396 (aset vect 4 "00:00:00"))
|
|
397 (if (equal (aref vect 5) "")
|
|
398 (aset vect 5 (vm-current-time-zone)))
|
|
399 ;; save this work so we won't have to do it again
|
|
400 (setq vm-sortable-date-alist
|
|
401 (cons (cons string
|
70
|
402 (timezone-make-date-sortable
|
|
403 (format "%s %s %s %s %s"
|
|
404 (aref vect 1)
|
|
405 (aref vect 2)
|
|
406 (aref vect 3)
|
|
407 (aref vect 4)
|
|
408 (aref vect 5))))
|
0
|
409 vm-sortable-date-alist))
|
|
410 ;; return result
|
|
411 (cdr (car vm-sortable-date-alist)))))
|
|
412
|
|
413 (defun vm-current-time-zone ()
|
|
414 (or (condition-case nil
|
|
415 (let* ((zone (car (current-time-zone)))
|
|
416 (absmin (/ (vm-abs zone) 60)))
|
|
417 (format "%c%02d%02d" (if (< zone 0) ?- ?+)
|
|
418 (/ absmin 60) (% absmin 60)))
|
|
419 (error nil))
|
|
420 (let ((temp-buffer nil))
|
|
421 (condition-case nil
|
|
422 (unwind-protect
|
|
423 (save-excursion
|
|
424 (setq temp-buffer (generate-new-buffer "*vm-work*"))
|
|
425 (set-buffer temp-buffer)
|
|
426 (call-process "date" nil temp-buffer nil)
|
|
427 (nth 4 (vm-parse (vm-buffer-string-no-properties)
|
|
428 " *\\([^ ]+\\)")))
|
|
429 (and temp-buffer (kill-buffer temp-buffer)))
|
|
430 (error nil)))
|
|
431 ""))
|
|
432
|
|
433 (defun vm-should-generate-summary ()
|
|
434 (cond ((eq vm-startup-with-summary t) t)
|
|
435 ((integerp vm-startup-with-summary)
|
|
436 (let ((n vm-startup-with-summary))
|
|
437 (cond ((< n 0) (null (nth (vm-abs n) vm-message-list)))
|
|
438 (t (nth (1- n) vm-message-list)))))
|
|
439 (vm-startup-with-summary t)
|
|
440 (t nil)))
|
|
441
|
|
442 (defun vm-find-composition-buffer (&optional not-picky)
|
|
443 (let ((b-list (buffer-list)) choice alternate)
|
|
444 (save-excursion
|
|
445 (while b-list
|
|
446 (set-buffer (car b-list))
|
|
447 (if (eq major-mode 'mail-mode)
|
|
448 (if (buffer-modified-p)
|
|
449 (setq choice (current-buffer)
|
|
450 b-list nil)
|
|
451 (and not-picky (null alternate)
|
|
452 (setq alternate (current-buffer)))
|
|
453 (setq b-list (cdr b-list)))
|
|
454 (setq b-list (cdr b-list))))
|
|
455 (or choice alternate))))
|
|
456
|
|
457 (defun vm-get-file-buffer (file)
|
|
458 "Like get-file-buffer, but also checks buffers against FILE's truename"
|
|
459 (or (get-file-buffer file)
|
|
460 (and (fboundp 'file-truename)
|
|
461 (get-file-buffer (file-truename file)))))
|
|
462
|
|
463 (defun vm-set-region-face (start end face)
|
70
|
464 (cond ((fboundp 'make-overlay)
|
|
465 (let ((o (make-overlay start end)))
|
|
466 (overlay-put o 'face face)))
|
|
467 ((fboundp 'make-extent)
|
|
468 (let ((o (make-extent start end)))
|
|
469 (set-extent-property o 'face face)))))
|
|
470
|
|
471 (defun vm-unsaved-message (&rest args)
|
|
472 (let ((message-log-max nil))
|
|
473 (apply (function message) args)))
|
0
|
474
|
|
475 (defun vm-default-buffer-substring-no-properties (beg end &optional buffer)
|
|
476 (let ((s (if buffer
|
|
477 (save-excursion
|
|
478 (set-buffer buffer)
|
|
479 (buffer-substring beg end))
|
|
480 (buffer-substring beg end))))
|
|
481 (set-text-properties 0 (length s) nil s)
|
|
482 (copy-sequence s)))
|
|
483
|
|
484 (fset 'vm-buffer-substring-no-properties
|
|
485 (cond ((fboundp 'buffer-substring-no-properties)
|
|
486 (function buffer-substring-no-properties))
|
70
|
487 ((vm-xemacs-p)
|
0
|
488 (function buffer-substring))
|
|
489 (t (function vm-default-buffer-substring-no-properties))))
|
|
490
|
|
491 (defun vm-buffer-string-no-properties ()
|
|
492 (vm-buffer-substring-no-properties (point-min) (point-max)))
|