1 ;;; wl-util.el --- Utility modules for Wanderlust.
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
4 ;; Copyright (C) 2000 A. SAGATA <sagata@nttvdt.hil.ntt.co.jp>
5 ;; Copyright (C) 2000 Katsumi Yamaoka <yamaoka@jpl.org>
7 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
8 ;; A. SAGATA <sagata@nttvdt.hil.ntt.co.jp>
9 ;; Katsumi Yamaoka <yamaoka@jpl.org>
10 ;; Keywords: mail, net news
12 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
14 ;; This program is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; This program is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
39 (condition-case nil (require 'pp) (error nil))
43 (defalias-maybe 'next-command-event 'ignore)
44 (defalias-maybe 'event-to-character 'ignore)
45 (defalias-maybe 'key-press-event-p 'ignore)
46 (defalias-maybe 'button-press-event-p 'ignore)
47 (defalias-maybe 'set-process-kanji-code 'ignore)
48 (defalias-maybe 'set-process-coding-system 'ignore)
49 (defalias-maybe 'dispatch-event 'ignore))
51 (defalias 'wl-set-work-buf 'elmo-set-work-buf)
52 (make-obsolete 'wl-set-work-buf 'elmo-set-work-buf)
54 (defmacro wl-append (val func)
56 (list 'nconc val func)
57 (list 'setq val func)))
59 (defalias 'wl-parse 'elmo-parse)
60 (make-obsolete 'wl-parse 'elmo-parse)
62 (defun wl-delete-duplicates (list &optional all hack-addresses)
63 "Delete duplicate equivalent strings from the LIST.
64 If ALL is t, then if there is more than one occurrence of a string in the LIST,
65 then all occurrences of it are removed instead of just the subsequent ones.
66 If HACK-ADDRESSES is t, then the strings are considered to be mail addresses,
67 and only the address part is compared (so that \"Name <foo>\" and \"foo\"
68 would be considered to be equivalent.)"
69 (let ((hashtable (make-vector 29 0))
72 (fillarray hashtable 0)
76 (wl-address-header-extract-address (car list))
78 sym-string (or sym-string "-unparseable-garbage-")
79 sym (intern sym-string hashtable))
81 (and all (setcar (symbol-value sym) nil))
82 (setq new-list (cons (car list) new-list))
84 (setq list (cdr list)))
85 (delq nil (nreverse new-list))))
88 (defalias 'wl-string-member 'elmo-string-member)
89 (defalias 'wl-string-match-member 'elmo-string-match-member)
90 (defalias 'wl-string-delete-match 'elmo-string-delete-match)
91 (defalias 'wl-string-match-assoc 'elmo-string-match-assoc)
92 (defalias 'wl-string-assoc 'elmo-string-assoc)
93 (defalias 'wl-string-rassoc 'elmo-string-rassoc)
95 (defalias 'wl-parse-addresses 'elmo-parse-addresses)
97 (defun wl-append-element (list element)
99 (append list (list element))
102 (defmacro wl-push (v l)
103 "Insert V at the head of the list stored in L."
104 (list 'setq l (list 'cons v l)))
107 "Remove the head of the list stored in L."
108 (list 'car (list 'prog1 l (list 'setq l (list 'cdr l)))))
110 (defun wl-ask-folder (func mes-string)
112 (cmd (if (featurep 'xemacs)
113 (event-to-character last-command-event)
114 (string-to-char (format "%s" (this-command-keys))))))
115 (message "%s" mes-string)
116 (setq key (car (setq keve (wl-read-event-char))))
117 (if (or (equal key ?\ )
123 (wl-push (cdr keve) unread-command-events))))
125 (defun wl-require-update-all-folder-p (name)
126 "Return non-nil if NAME is draft or queue folder."
127 (or (string= name wl-draft-folder)
128 (string= name wl-queue-folder)))
130 ;(defalias 'wl-make-hash 'elmo-make-hash)
131 ;;(make-obsolete 'wl-make-hash 'elmo-make-hash)
133 ;;(defalias 'wl-get-hash-val 'elmo-get-hash-val)
134 ;;(make-obsolete 'wl-get-hash-val 'elmo-get-hash-val)
136 ;;(defalias 'wl-set-hash-val 'elmo-set-hash-val)
137 ;;(make-obsolete 'wl-set-hash-val 'elmo-set-hash-val)
139 (defsubst wl-set-string-width (width string &optional padding ignore-invalid)
140 "Make a new string which have specified WIDTH and content of STRING.
141 `wl-invalid-character-message' is used when invalid character is contained.
142 If WIDTH is negative number, padding chars are added to the head and
143 otherwise, padding chars are added to the tail of the string.
144 The optional 3rd arg PADDING, if non-nil, specifies a padding character
145 to add the result instead of white space.
146 If optional 4th argument is non-nil, don't use `wl-invalid-character-message'
147 even when invalid character is contained."
149 ((and (fboundp 'string-width) (fboundp 'truncate-string-to-width)
150 (not (featurep 'xemacs)))
151 (if (> (string-width string) (abs width))
152 (setq string (truncate-string-to-width string (abs width))))
153 (if (= (string-width string) (abs width))
155 (when (and (not ignore-invalid)
156 (< (abs width) (string-width string)))
158 (truncate-string-to-width wl-invalid-character-message
160 (let ((paddings (make-string
161 (max 0 (- (abs width) (string-width string)))
164 (concat paddings string)
165 (concat string paddings)))))
168 (set-buffer-multibyte default-enable-multibyte-characters)
170 (when (> (current-column) (abs width))
171 (when (> (move-to-column (abs width)) (abs width))
172 (condition-case nil ; ignore error
175 (setq string (buffer-substring (point-min) (point))))
176 (if (= (current-column) (abs width))
178 (let ((paddings (make-string (- (abs width) (current-column))
181 (concat paddings string)
182 (concat string paddings))))))))
184 (defun wl-mode-line-buffer-identification (&optional id)
185 (let ((priorities '(biff plug title)))
186 (let ((items (reverse wl-mode-line-display-priority-list))
189 (setq item (car items)
191 (unless (memq item '(biff plug))
193 (setq priorities (cons item (delq item priorities)))))
194 (let (priority result)
196 (setq priority (car priorities)
197 priorities (cdr priorities))
200 (when wl-biff-check-folder-list
201 (setq result (append result '((wl-modeline-biff-status
202 wl-modeline-biff-state-on
203 wl-modeline-biff-state-off))))))
205 (when wl-show-plug-status-on-modeline
206 (setq result (append result '((wl-modeline-plug-status
207 wl-modeline-plug-state-on
208 wl-modeline-plug-state-off))))))
210 (setq result (append result (or id '("Wanderlust: %12b")))))))
212 (setq mode-line-buffer-identification (if (stringp (car result))
215 (force-mode-line-update t)))))
217 (defalias 'wl-display-error 'elmo-display-error)
218 (make-obsolete 'wl-display-error 'elmo-display-error)
220 (defun wl-get-assoc-list-value (assoc-list folder &optional match)
222 (let ((alist assoc-list)
225 (setq pair (car alist))
226 (if (and (eq match 'function)
227 (functionp (car pair)))
228 (when (funcall (car pair) folder)
229 (throw 'found (cdr pair)))
230 (if (string-match (car pair) folder)
231 (cond ((eq match 'all)
232 (setq value (append value (list (cdr pair)))))
233 ((eq match 'all-list)
234 (setq value (append value (cdr pair))))
235 ((or (not match) (eq match 'function))
236 (throw 'found (cdr pair))))))
237 (setq alist (cdr alist)))
240 (defmacro wl-match-string (pos string)
241 "Substring POSth matched STRING."
242 (` (substring (, string) (match-beginning (, pos)) (match-end (, pos)))))
244 (defmacro wl-match-buffer (pos)
245 "Substring POSth matched from the current buffer."
246 (` (buffer-substring-no-properties
247 (match-beginning (, pos)) (match-end (, pos)))))
249 (put 'wl-as-coding-system 'lisp-indent-function 1)
250 (put 'wl-as-mime-charset 'lisp-indent-function 1)
255 (defmacro wl-as-coding-system (coding-system &rest body)
256 (` (let ((coding-system-for-read (, coding-system))
257 (coding-system-for-write (, coding-system)))
260 (defmacro wl-as-coding-system (coding-system &rest body)
261 (` (let ((file-coding-system-for-read (, coding-system))
262 (file-coding-system (, coding-system)))
265 (defmacro wl-as-coding-system (coding-system &rest body)
266 (` (progn (,@ body)))))))
268 (defmacro wl-as-mime-charset (mime-charset &rest body)
269 (` (wl-as-coding-system (mime-charset-to-coding-system (, mime-charset))
272 (defalias 'wl-string 'elmo-string)
273 (make-obsolete 'wl-string 'elmo-string)
275 (if (not (fboundp 'overlays-in))
276 (defun overlays-in (beg end)
277 "Return a list of the overlays that overlap the region BEG ... END.
278 Overlap means that at least one character is contained within the overlay
279 and also contained within the specified region.
280 Empty overlays are included in the result if they are located at BEG
281 or between BEG and END."
282 (let ((ovls (overlay-lists))
288 (setq ovls (nconc (car ovls) (cdr ovls)))
292 (if (or (and (<= (overlay-start tmp) end)
293 (>= (overlay-start tmp) beg))
294 (and (<= (overlay-end tmp) end)
295 (>= (overlay-end tmp) beg)))
296 (setq retval (cons tmp retval))))
299 (defsubst wl-repeat-string (str times)
303 (setq ret-val (concat ret-val str))
304 (setq loop (- loop 1)))
307 (defun wl-append-assoc-list (item value alist)
308 "make assoc list '((item1 value1-1 value1-2 ...)) (item2 value2-1 ...)))"
309 (let ((entry (assoc item alist)))
312 (when (not (member value (cdr entry)))
313 (nconc entry (list value)))
316 (list (list item value))))))
318 (defun wl-delete-alist (key alist)
319 "Delete by side effect any entries specified with KEY from ALIST.
320 Return the modified ALIST. Key comparison is done with `assq'.
321 Write `(setq foo (wl-delete-alist key foo))' to be sure of changing
324 (while (setq entry (assq key alist))
325 (setq alist (delq entry alist)))
328 (defun wl-delete-associations (keys alist)
329 "Delete by side effect any entries specified with KEYS from ALIST.
330 Return the modified ALIST. KEYS must be a list of keys for ALIST.
331 Deletion is done with `wl-delete-alist'.
332 Write `(setq foo (wl-delete-associations keys foo))' to be sure of
333 changing the value of `foo'."
335 (setq alist (wl-delete-alist (car keys) alist))
336 (setq keys (cdr keys)))
339 (defun wl-filter-associations (keys alist)
342 (when (setq entry (assq (car keys) alist))
343 (setq result (cons entry result)))
344 (setq keys (cdr keys)))
347 (defun wl-inverse-alist (keys alist)
348 "Inverse ALIST, copying.
349 Return an association list represents the inverse mapping of ALIST,
350 from objects to KEYS.
351 The objects mapped (cdrs of elements of the ALIST) are shared."
352 (let (x y tmp result)
355 (setq y (cdr (assq x alist)))
357 (if (setq tmp (assoc y result))
358 (setq result (cons (append tmp (list x))
359 (delete tmp result)))
360 (setq result (cons (list y x) result))))
361 (setq keys (cdr keys)))
366 (static-unless (fboundp 'pp)
367 (defvar pp-escape-newlines t)
368 (defun pp (object &optional stream)
369 "Output the pretty-printed representation of OBJECT, any Lisp object.
370 Quoting characters are printed when needed to make output that `read'
371 can handle, whenever this is possible.
372 Output stream is STREAM, or value of `standard-output' (which see)."
373 (princ (pp-to-string object) (or stream standard-output)))
375 (defun pp-to-string (object)
376 "Return a string containing the pretty-printed representation of OBJECT,
377 any Lisp object. Quoting characters are used when needed to make output
378 that `read' can handle, whenever this is possible."
380 (set-buffer (generate-new-buffer " pp-to-string"))
383 (lisp-mode-variables t)
384 (let ((print-escape-newlines pp-escape-newlines))
385 (prin1 object (current-buffer)))
386 (goto-char (point-min))
389 ((looking-at "\\s(\\|#\\s(")
390 (while (looking-at "\\s(\\|#\\s(")
392 ((and (looking-at "\\(quote[ \t]+\\)\\([^.)]\\)")
393 (> (match-beginning 1) 1)
394 (= ?\( (char-after (1- (match-beginning 1))))
395 ;; Make sure this is a two-element list.
397 (goto-char (match-beginning 2))
399 ;; Avoid mucking with match-data; does this test work?
400 (char-equal ?\) (char-after (point)))))
401 ;; -1 gets the paren preceding the quote as well.
402 (delete-region (1- (match-beginning 1)) (match-end 1))
405 (if (looking-at "[ \t]*\)")
406 (delete-region (match-beginning 0) (match-end 0))
407 (error "Malformed quote"))
409 ((condition-case err-var
410 (prog1 t (down-list 1))
413 (skip-chars-backward " \t")
416 (progn (skip-chars-forward " \t") (point)))
417 (if (not (char-equal ?' (char-after (1- (point)))))
419 ((condition-case err-var
420 (prog1 t (up-list 1))
422 (while (looking-at "\\s)")
424 (skip-chars-backward " \t")
427 (progn (skip-chars-forward " \t") (point)))
428 (if (not (char-equal ?' (char-after (1- (point)))))
430 (t (goto-char (point-max)))))
431 (goto-char (point-min))
434 (kill-buffer (current-buffer))))))
436 (defsubst wl-get-date-iso8601 (date)
437 (or (get-text-property 0 'wl-date date)
438 (let* ((d1 (timezone-fix-time date nil nil))
439 (time (format "%04d%02d%02dT%02d%02d%02d"
440 (aref d1 0) (aref d1 1) (aref d1 2)
441 (aref d1 3) (aref d1 4) (aref d1 5))))
442 (put-text-property 0 1 'wl-date time date)
445 (defun wl-make-date-string ()
446 (let ((s (current-time-string)))
447 (string-match "\\`\\([A-Z][a-z][a-z]\\) +[A-Z][a-z][a-z] +[0-9][0-9]? *[0-9][0-9]?:[0-9][0-9]:[0-9][0-9] *[0-9]?[0-9]?[0-9][0-9]"
449 (concat (wl-match-string 1 s) ", "
450 (timezone-make-date-arpa-standard s (current-time-zone)))))
452 (defun wl-date-iso8601 (date)
453 "Convert the DATE to YYMMDDTHHMMSS."
455 (wl-get-date-iso8601 date)
458 (defun wl-url-news (url &rest args)
459 (interactive "sURL: ")
460 (if (string-match "^news:\\(.*\\)$" url)
461 (wl-summary-goto-folder-subr
462 (concat "-" (elmo-match-string 1 url)) nil nil nil t)
463 (message "Not a news: url.")))
465 (defun wl-url-nntp (url &rest args)
466 (interactive "sURL: ")
467 (let (folder fld-name server port msg)
469 "^nntp://\\([^:/]*\\):?\\([0-9]*\\)/\\([^/]*\\)/\\([0-9]*\\).*$" url)
471 (if (eq (length (setq fld-name
472 (elmo-match-string 3 url))) 0)
474 (if (eq (length (setq port
475 (elmo-match-string 2 url))) 0)
476 (setq port (int-to-string elmo-nntp-default-port)))
477 (if (eq (length (setq server
478 (elmo-match-string 1 url))) 0)
479 (setq server elmo-nntp-default-server))
480 (setq folder (concat "-" fld-name "@" server ":" port))
481 (if (eq (length (setq msg
482 (elmo-match-string 4 url))) 0)
483 (wl-summary-goto-folder-subr
484 folder nil nil nil t)
485 (wl-summary-goto-folder-subr
486 folder 'update nil nil t)
487 (wl-summary-jump-to-msg (string-to-number msg))
488 (wl-summary-redisplay)))
489 (message "Not a nntp: url."))))
491 (defmacro wl-concat-list (list separator)
492 (` (mapconcat 'identity (delete "" (delq nil (, list))) (, separator))))
494 (defun wl-current-message-buffer ()
495 (when (buffer-live-p wl-current-summary-buffer)
496 (with-current-buffer wl-current-summary-buffer
497 (or wl-message-buffer
498 (and (wl-summary-message-number)
499 (wl-message-buffer-display
500 wl-summary-buffer-elmo-folder
501 (wl-summary-message-number)
502 wl-summary-buffer-display-mime-mode
505 (defmacro wl-kill-buffers (regexp)
508 (if (and (buffer-name x)
509 (string-match (, regexp) (buffer-name x)))
514 (defun wl-collect-summary ()
517 (function (lambda (x)
518 (if (and (string-match "^Summary"
522 (equal major-mode 'wl-summary-mode)))
523 (setq result (nconc result (list x))))))
527 (defun wl-collect-draft ()
528 (let ((draft-regexp (concat "^" (regexp-quote wl-draft-folder)))
530 (dolist (buffer (buffer-list))
531 (when (with-current-buffer buffer
532 (and (eq major-mode 'wl-draft-mode)
534 (string-match draft-regexp (buffer-name))))
535 (setq result (cons buffer result))))
538 (defvar wl-inhibit-save-drafts nil)
539 (defvar wl-disable-auto-save nil)
540 (make-variable-buffer-local 'wl-disable-auto-save)
542 (defun wl-save-drafts ()
543 "Save all drafts. Return nil if there is no draft buffer."
544 (if wl-inhibit-save-drafts
546 (let ((wl-inhibit-save-drafts t)
547 (msg (current-message))
548 (buffers (wl-collect-draft)))
550 (dolist (buffer buffers)
552 (when (and (not wl-disable-auto-save)
555 (message "%s" (or msg ""))
558 (static-if (fboundp 'read-directory-name)
559 (defun wl-read-directory-name (prompt dir)
560 (read-directory-name prompt dir dir))
561 (defun wl-read-directory-name (prompt dir)
562 (let ((dir (read-file-name prompt dir)))
563 (unless (file-directory-p dir)
564 (error "%s is not directory" dir))
567 ;; local variable check.
568 (static-if (fboundp 'local-variable-p)
569 (defalias 'wl-local-variable-p 'local-variable-p)
570 (defmacro wl-local-variable-p (symbol &optional buffer)
571 (` (if (assq (, symbol) (buffer-local-variables (, buffer)))
574 (defun wl-number-base36 (num len)
579 (concat (wl-number-base36 (/ num 36) (1- len))
580 (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
583 (defvar wl-unique-id-char nil)
585 (defun wl-unique-id ()
586 ;; Don't use microseconds from (current-time), they may be unsupported.
587 ;; Instead we use this randomly inited counter.
588 (setq wl-unique-id-char
589 (% (1+ (or wl-unique-id-char (logand (random t) (1- (lsh 1 20)))))
590 ;; (current-time) returns 16-bit ints,
591 ;; and 2^16*25 just fits into 4 digits i base 36.
593 (let ((tm (static-if (fboundp 'current-time)
595 (let* ((cts (split-string (current-time-string) "[ :]"))
596 (m (cdr (assoc (nth 1 cts)
597 '(("Jan" . "01") ("Feb" . "02")
598 ("Mar" . "03") ("Apr" . "04")
599 ("May" . "05") ("Jun" . "06")
600 ("Jul" . "07") ("Aug" . "08")
601 ("Sep" . "09") ("Oct" . "10")
602 ("Nov" . "11") ("Dec" . "12"))))))
603 (list (string-to-int (concat (nth 6 cts) m
604 (substring (nth 2 cts) 0 1)))
605 (string-to-int (concat (substring (nth 2 cts) 1)
606 (nth 4 cts) (nth 5 cts)
609 (if (memq system-type '(ms-dos emx vax-vms))
610 (let ((user (downcase (user-login-name))))
611 (while (string-match "[^a-z0-9_]" user)
612 (aset user (match-beginning 0) ?_))
614 (wl-number-base36 (user-uid) -1))
615 (wl-number-base36 (+ (car tm)
616 (lsh (% wl-unique-id-char 25) 16)) 4)
617 (wl-number-base36 (+ (nth 1 tm)
618 (lsh (/ wl-unique-id-char 25) 16)) 4)
619 ;; Append the name of the message interface, because while the
620 ;; generated ID is unique to this newsreader, other newsreaders
621 ;; might otherwise generate the same ID via another algorithm.
622 wl-unique-id-suffix)))
624 (defvar wl-message-id-function 'wl-draft-make-message-id-string)
625 (defun wl-draft-make-message-id-string ()
626 "Return Message-ID field value."
627 (concat "<" (wl-unique-id)
628 (let (from user domain)
629 (if (and wl-message-id-use-wl-from
631 (setq from (wl-address-header-extract-address wl-from))
632 (and (string-match "^\\(.*\\)@\\(.*\\)$" from)
633 (setq user (match-string 1 from))
634 (setq domain (match-string 2 from)))))
635 (format "%%%s@%s>" user domain)
637 (or wl-message-id-domain
639 (concat (system-name) "." wl-local-domain)
643 (defvar wl-load-profile-function 'wl-local-load-profile)
644 (defun wl-local-load-profile ()
645 "Load `wl-init-file'."
646 (message "Initializing...")
647 (load wl-init-file 'noerror 'nomessage))
649 (defun wl-load-profile ()
650 "Call `wl-load-profile-function' function."
651 (funcall wl-load-profile-function))
655 (defmacro wl-count-lines ()
658 (count-lines 1 (point)))))
660 (defun wl-horizontal-recenter ()
661 "Recenter the current buffer horizontally."
663 (re-search-forward "[[<]" (point-at-eol) t)
664 (if (< (current-column) (/ (window-width) 2))
665 (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
666 (let* ((orig (point))
667 (end (window-end (get-buffer-window (current-buffer) t)))
670 ;; Find the longest line currently displayed in the window.
671 (goto-char (window-start))
672 (while (and (not (eobp))
675 (setq max (max max (current-column)))
678 ;; Scroll horizontally to center (sort of) the point.
679 (if (> max (window-width))
681 (get-buffer-window (current-buffer) t)
682 (min (- (current-column) (/ (window-width) 3))
683 (+ 2 (- max (window-width)))))
684 (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
688 (defun wl-auto-save-drafts ()
689 (unless (wl-save-drafts)
690 (wl-stop-save-drafts)))
694 (defvar wl-save-drafts-timer-name "wl-save-drafts")
696 (defun wl-start-save-drafts ()
697 (when (numberp wl-auto-save-drafts-interval)
698 (unless (get-itimer wl-save-drafts-timer-name)
699 (start-itimer wl-save-drafts-timer-name
701 wl-auto-save-drafts-interval
702 wl-auto-save-drafts-interval
705 (defun wl-stop-save-drafts ()
706 (when (get-itimer wl-save-drafts-timer-name)
707 (delete-itimer wl-save-drafts-timer-name))))
709 (defun wl-start-save-drafts ()
710 (when (numberp wl-auto-save-drafts-interval)
712 (if (get 'wl-save-drafts 'timer)
714 (timer-set-idle-time (get 'wl-save-drafts 'timer)
715 wl-auto-save-drafts-interval t)
716 (timer-activate-when-idle (get 'wl-save-drafts 'timer)))
717 (put 'wl-save-drafts 'timer
719 wl-auto-save-drafts-interval t 'wl-auto-save-drafts)))))
721 (defun wl-stop-save-drafts ()
722 (when (get 'wl-save-drafts 'timer)
723 (cancel-timer (get 'wl-save-drafts 'timer))))))
725 (defun wl-set-auto-save-draft (&optional arg)
727 (unless (setq wl-disable-auto-save
729 ((null arg) (not wl-disable-auto-save))
730 ((< (prefix-numeric-value arg) 0) t)
732 (wl-start-save-drafts))
733 (when (interactive-p)
734 (message "Auto save is %s (in this buffer)"
735 (if wl-disable-auto-save "disabled" "enabled"))))
740 (defvar wl-biff-timer-name "wl-biff")
742 (defun wl-biff-stop ()
743 (when (get-itimer wl-biff-timer-name)
744 (delete-itimer wl-biff-timer-name)))
746 (defun wl-biff-start ()
748 (when wl-biff-check-folder-list
749 (start-itimer wl-biff-timer-name 'wl-biff-check-folders
750 wl-biff-check-interval wl-biff-check-interval
751 wl-biff-use-idle-timer))))
754 (defun wl-biff-stop ()
755 (when (get 'wl-biff 'timer)
756 (cancel-timer (get 'wl-biff 'timer))))
758 (defun wl-biff-start ()
760 (when wl-biff-check-folder-list
761 (if wl-biff-use-idle-timer
762 (if (get 'wl-biff 'timer)
763 (progn (timer-set-idle-time (get 'wl-biff 'timer)
764 wl-biff-check-interval t)
765 (timer-activate-when-idle (get 'wl-biff 'timer)))
768 wl-biff-check-interval t 'wl-biff-event-handler)))
769 (if (get 'wl-biff 'timer)
771 (timer-set-time (get 'wl-biff 'timer)
772 (timer-next-integral-multiple-of-time
773 (current-time) wl-biff-check-interval)
774 wl-biff-check-interval)
775 (timer-activate (get 'wl-biff 'timer)))
778 (timer-next-integral-multiple-of-time
779 (current-time) wl-biff-check-interval)
780 wl-biff-check-interval
781 'wl-biff-event-handler))))))
783 (defun-maybe timer-next-integral-multiple-of-time (time secs)
784 "Yield the next value after TIME that is an integral multiple of SECS.
785 More precisely, the next value, after TIME, that is an integral multiple
786 of SECS seconds since the epoch. SECS may be a fraction.
787 This function is imported from Emacs 20.7."
788 (let ((time-base (ash 1 16)))
790 ;; Use floating point, taking care to not lose precision.
791 (let* ((float-time-base (float time-base))
793 (time-usec (+ (* million
794 (+ (* float-time-base (nth 0 time))
797 (secs-usec (* million secs))
798 (mod-usec (mod time-usec secs-usec))
799 (next-usec (+ (- time-usec mod-usec) secs-usec))
800 (time-base-million (* float-time-base million)))
801 (list (floor next-usec time-base-million)
802 (floor (mod next-usec time-base-million) million)
803 (floor (mod next-usec million))))
804 ;; Floating point is not supported.
805 ;; Use integer arithmetic, avoiding overflow if possible.
806 (let* ((mod-sec (mod (+ (* (mod time-base secs)
807 (mod (nth 0 time) secs))
810 (next-1-sec (+ (- (nth 1 time) mod-sec) secs)))
811 (list (+ (nth 0 time) (floor next-1-sec time-base))
812 (mod next-1-sec time-base)
815 (defun wl-biff-event-handler ()
816 ;; PAKURing from FSF:time.el
817 (wl-biff-check-folders)
818 ;; Do redisplay right now, if no input pending.
820 (let* ((current (current-time))
821 (timer (get 'wl-biff 'timer))
822 ;; Compute the time when this timer will run again, next.
823 (next-time (timer-relative-time
824 (list (aref timer 1) (aref timer 2) (aref timer 3))
825 (* 5 (aref timer 4)) 0)))
826 ;; If the activation time is far in the past,
827 ;; skip executions until we reach a time in the future.
828 ;; This avoids a long pause if Emacs has been suspended for hours.
829 (or (> (nth 0 next-time) (nth 0 current))
830 (and (= (nth 0 next-time) (nth 0 current))
831 (> (nth 1 next-time) (nth 1 current)))
832 (and (= (nth 0 next-time) (nth 0 current))
833 (= (nth 1 next-time) (nth 1 current))
834 (> (nth 2 next-time) (nth 2 current)))
836 (timer-set-time timer (timer-next-integral-multiple-of-time
837 current wl-biff-check-interval)
838 wl-biff-check-interval)
839 (timer-activate timer)))))))
841 (defsubst wl-biff-notify (new-mails notify-minibuf)
842 (when (and (not wl-modeline-biff-status) (> new-mails 0))
843 (run-hooks 'wl-biff-notify-hook))
844 (when (and wl-modeline-biff-status (eq new-mails 0))
845 (run-hooks 'wl-biff-unnotify-hook))
846 (setq wl-modeline-biff-status (> new-mails 0))
847 (force-mode-line-update t)
849 (cond ((zerop new-mails) (message "No mail."))
850 ((= 1 new-mails) (message "You have a new mail."))
851 (t (message "You have %d new mails." new-mails)))))
853 ;; Internal variable.
854 (defvar wl-biff-check-folders-running nil)
856 (defun wl-biff-check-folders ()
858 (if wl-biff-check-folders-running
859 (when (interactive-p)
860 (message "Biff process is running."))
861 (setq wl-biff-check-folders-running t)
862 (when (interactive-p)
863 (message "Checking new mails..."))
865 (flist (or wl-biff-check-folder-list (list wl-default-folder)))
867 (if (eq (length flist) 1)
868 (wl-biff-check-folder-async (wl-folder-get-elmo-folder
869 (car flist) 'biff) (interactive-p))
872 (setq folder (wl-folder-get-elmo-folder (car flist))
874 (elmo-folder-set-biff-internal folder t)
875 (when (and (elmo-folder-plugged-p folder)
876 (elmo-folder-exists-p folder))
879 (nth 0 (wl-biff-check-folder folder))))))
880 (setq wl-biff-check-folders-running nil)
881 (wl-biff-notify new-mails (interactive-p)))))))
883 (defun wl-biff-check-folder (folder)
884 (if (eq (elmo-folder-type-internal folder) 'pop3)
885 (unless (elmo-pop3-get-session folder 'any-exists)
886 (wl-folder-check-one-entity (elmo-folder-name-internal folder)
888 (wl-folder-check-one-entity (elmo-folder-name-internal folder)
891 (defun wl-biff-check-folder-async-callback (diff data)
893 (with-current-buffer (nth 1 data)
894 (wl-folder-entity-hashtb-set wl-folder-entity-hashtb
897 (- (nth 1 diff) (nth 0 diff))
900 (setq wl-folder-info-alist-modified t)
901 (setq wl-biff-check-folders-running nil)
903 (wl-biff-notify (car diff) (nth 2 data)))
905 (defun wl-biff-check-folder-async (folder notify-minibuf)
906 (if (and (elmo-folder-plugged-p folder)
907 (wl-folder-entity-exists-p (elmo-folder-name-internal folder)))
909 (elmo-folder-set-biff-internal folder t)
910 (if (and (eq (elmo-folder-type-internal folder) 'imap4)
911 (elmo-folder-use-flag-p folder))
912 ;; Check asynchronously only when IMAP4 and use server diff.
914 (setq elmo-folder-diff-async-callback
915 'wl-biff-check-folder-async-callback)
916 (setq elmo-folder-diff-async-callback-data
917 (list (elmo-folder-name-internal folder)
918 (get-buffer wl-folder-buffer-name)
920 (elmo-folder-diff-async folder))
922 (wl-biff-notify (car (wl-biff-check-folder folder))
924 (setq wl-biff-check-folders-running nil))))
925 (setq wl-biff-check-folders-running nil)))
927 (if (and (fboundp 'regexp-opt)
928 (not (featurep 'xemacs)))
929 (defalias 'wl-regexp-opt 'regexp-opt)
930 (defun wl-regexp-opt (strings &optional paren)
931 "Return a regexp to match a string in STRINGS.
932 Each string should be unique in STRINGS and should not contain any regexps,
933 quoted or not. If optional PAREN is non-nil, ensure that the returned regexp
934 is enclosed by at least one regexp grouping construct."
935 (let ((open-paren (if paren "\\(" "")) (close-paren (if paren "\\)" "")))
936 (concat open-paren (mapconcat 'regexp-quote strings "\\|")
939 (defalias 'wl-expand-newtext 'elmo-expand-newtext)
940 (defalias 'wl-regexp-opt 'elmo-regexp-opt)
942 (defun wl-region-exists-p ()
943 "Return non-nil if a region exists on current buffer."
944 (static-if (featurep 'xemacs)
946 (and transient-mark-mode mark-active)))
948 (defun wl-deactivate-region ()
949 "Deactivate region on current buffer"
950 (static-if (not (featurep 'xemacs))
951 (setq mark-active nil)))
953 (defvar wl-line-string)
954 (defun wl-line-parse-format (format spec-alist)
955 "Make a formatter from FORMAT and SPEC-ALIST."
956 (let (f spec specs stack)
960 (goto-char (point-min))
961 (while (search-forward "%" nil t)
964 (goto-char (match-end 0)))
965 ((looking-at "\\(-?\\(0?\\)[0-9]*\\)\\([^0-9]\\)")
967 ((string= (match-string 3) "(")
968 (if (zerop (length (match-string 1)))
969 (error "No number specification for %%( line format"))
971 (match-beginning 0) ; start
972 (match-end 0) ; start-content
974 (match-string 1)) ; width
978 ((string= (match-string 3) ")")
979 (let ((entry (pop stack))
983 "No matching %%( parenthesis in summary line format"))
984 (goto-char (car entry)) ; start
985 (setq form (buffer-substring (nth 1 entry) ; start-content
986 (- (match-beginning 0) 1)))
987 (delete-region (car entry) (match-end 0))
992 (list (list 'wl-set-string-width (nth 2 entry)
998 (if (setq spec (assq (string-to-char (match-string 3))
1002 (unless (string= "" (match-string 1))
1003 (setq spec (list 'wl-set-string-width
1004 (string-to-number (match-string 1))
1006 (unless (string= "" (match-string 2))
1007 (string-to-char (match-string 2))))))
1008 (replace-match "s" 'fixed)
1009 (setq specs (append specs
1012 'setq 'wl-line-string
1015 (append (list 'format f) specs)))
1017 (defmacro wl-line-formatter-setup (formatter format alist)
1018 (` (let (byte-compile-warnings)
1022 (wl-line-parse-format (, format) (, alist)))))
1023 (when (get-buffer "*Compile-Log*")
1024 (bury-buffer "*Compile-Log*"))
1025 (when (get-buffer "*Compile-Log-Show*")
1026 (bury-buffer "*Compile-Log-Show*")))))
1028 (defsubst wl-copy-local-variables (src dst local-variables)
1029 "Copy value of LOCAL-VARIABLES from SRC buffer to DST buffer."
1030 (with-current-buffer dst
1031 (dolist (variable local-variables)
1032 (set (make-local-variable variable)
1033 (with-current-buffer src
1034 (symbol-value variable))))))
1036 ;;; Search Condition
1037 (defun wl-search-condition-fields ()
1038 (let ((denial-fields
1039 (nconc (mapcar 'capitalize elmo-msgdb-extra-fields)
1040 (mapcar 'capitalize wl-additional-search-condition-fields)
1041 '("Flag" "Since" "Before"
1042 "From" "Subject" "To" "Cc" "Body" "ToCc"
1043 "Larger" "Smaller"))))
1044 (append '("Last" "First")
1046 (mapcar (lambda (f) (concat "!" f))
1049 (defun wl-read-search-condition (default)
1050 "Read search condition string interactively."
1051 (wl-read-search-condition-internal "Search by" default))
1053 (defun wl-read-search-condition-internal (prompt default &optional paren)
1054 (let* ((completion-ignore-case t)
1055 (field (completing-read
1056 (format "%s (%s): " prompt default)
1058 (append '("AND" "OR") (wl-search-condition-fields)))))
1060 (setq field (if (string= field "")
1061 (setq field default)
1064 ((or (string= field "AND") (string= field "OR"))
1065 (concat (if paren "(" "")
1066 (wl-read-search-condition-internal
1067 (concat field "(1) Search by") default 'paren)
1068 (if (string= field "AND") "&" "|")
1069 (wl-read-search-condition-internal
1070 (concat field "(2) Search by") default 'paren)
1072 ((string-match "Since\\|Before" field)
1073 (let ((default (format-time-string "%Y-%m-%d")))
1074 (setq value (completing-read
1075 (format "Value for '%s' [%s]: " field default)
1078 (list (format "%s" (car x)))))
1079 elmo-date-descriptions)))
1080 (concat (downcase field) ":"
1081 (if (equal value "") default value))))
1082 ((string-match "!?Flag" field)
1084 (setq value (downcase
1086 (format "Value for '%s': " field)
1087 (mapcar (lambda (f) (list (capitalize (symbol-name f))))
1090 '(unread answered forwarded digest any)
1091 (copy-sequence elmo-global-flags))
1093 (unless (elmo-flag-valid-p value)
1094 (message "Invalid char in `%s'" value)
1097 (unless (string-match (concat "^" elmo-condition-atom-regexp "$")
1099 (setq value (prin1-to-string value)))
1100 (concat (downcase field) ":" value))
1102 (setq value (read-from-minibuffer (format "Value for '%s': " field)))
1103 (unless (string-match (concat "^" elmo-condition-atom-regexp "$")
1105 (setq value (prin1-to-string value)))
1106 (concat (downcase field) ":" value)))))
1108 (defun wl-y-or-n-p-with-scroll (prompt &optional scroll-by-SPC)
1109 (let ((prompt (concat prompt (if scroll-by-SPC
1110 "<y/n/SPC(down)/BS(up)> "
1111 "<y/n/j(down)/k(up)> "))))
1115 (case (let ((cursor-in-echo-area t))
1116 (cdr (wl-read-event-char prompt)))
1121 (ignore-errors (scroll-up))
1124 (ignore-errors (scroll-up)))
1125 ((?^ ?k ?K prior backspace)
1126 (ignore-errors (scroll-down)))
1128 (throw 'done nil)))))))
1130 (defun wl-find-region (beg-regexp end-regexp)
1131 (if (or (re-search-forward end-regexp nil t)
1132 (re-search-backward end-regexp nil t))
1133 (let ((end (match-end 0))
1134 (beg (re-search-backward beg-regexp nil t)))
1138 (defun wl-simple-display-progress (label action current total)
1139 (message "%s... %d%%"
1141 (if (> total 0) (floor (* (/ current (float total)) 100)) 0)))
1143 (when (fboundp 'progress-feedback-with-label)
1144 (defun wl-display-progress-with-gauge (label action current total)
1145 (progress-feedback-with-label
1148 (if (> total 0) (floor (* (/ current (float total)) 100)) 0)
1151 (defun wl-progress-callback-function (label action current total)
1154 (let ((threshold (if (consp wl-display-progress-threshold)
1155 (cdr (or (assq label wl-display-progress-threshold)
1156 (assq t wl-display-progress-threshold)))
1157 wl-display-progress-threshold)))
1159 (>= total threshold))))
1161 (message "%s..." action))
1163 (message "%s...done" action))
1165 (when wl-display-progress-function
1166 (funcall wl-display-progress-function label action current total)))))
1168 ;; read multiple strings with completion
1169 (defun wl-completing-read-multiple-1 (prompt
1172 require-match initial-input
1173 hist def inherit-input-method)
1174 "Read multiple strings in the minibuffer"
1176 (completing-read prompt table predicate nil
1177 initial-input hist def inherit-input-method)
1180 (static-when (fboundp 'completing-read-multiple)
1183 (defun wl-completing-read-multiple-2 (prompt
1186 require-match initial-input
1187 hist def inherit-input-method)
1188 "Read multiple strings in the minibuffer"
1189 (let ((ret (completing-read-multiple prompt table predicate
1190 require-match initial-input
1191 hist def inherit-input-method)))
1192 (if (and def (equal ret '("")))
1193 (split-string def crm-separator)
1197 ((not (fboundp 'completing-read-multiple))
1198 (defalias 'wl-completing-read-multiple 'wl-completing-read-multiple-1))
1199 ((< emacs-major-version 22)
1200 (defun wl-completing-read-multiple (prompt
1203 require-match initial-input
1204 hist def inherit-input-method)
1205 "Read multiple strings in the minibuffer"
1207 (wl-completing-read-multiple-1 prompt table predicate
1209 hist def inherit-input-method)
1210 (wl-completing-read-multiple-2 prompt table predicate
1212 hist def inherit-input-method))))
1214 (defalias 'wl-completing-read-multiple 'completing-read-multiple)))
1218 ((fboundp 'shell-command-read-minibuffer)
1219 (defun wl-read-shell-command (prompt &optional
1220 initial-contents keymap read hist)
1221 (shell-command-read-minibuffer prompt default-directory
1222 initial-contents keymap read hist)))
1224 (defalias 'wl-read-shell-command 'read-from-minibuffer)))
1227 (product-provide (provide 'wl-util) (require 'wl-version))
1229 ;;; wl-util.el ends here