1 ;;; wl-util.el -- Utility modules for Wanderlust.
3 ;; Copyright 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
8 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
33 (product-provide (provide 'wl-util) (require 'wl-version))
37 (condition-case nil (require 'tm-edit) (error nil))
38 (condition-case nil (require 'pp) (error nil))
42 (defalias-maybe 'read-event 'ignore)
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 (defun wl-parse (string regexp &optional matchn)
60 (or matchn (setq matchn 1))
62 (store-match-data nil)
63 (while (string-match regexp string (match-end 0))
64 (setq list (cons (substring string (match-beginning matchn)
65 (match-end matchn)) list)))
68 (defun wl-delete-duplicates (list &optional all hack-addresses)
69 "Delete duplicate equivalent strings from the LIST.
70 If ALL is t, then if there is more than one occurrence of a string in the LIST,
71 then all occurrences of it are removed instead of just the subsequent ones.
72 If HACK-ADDRESSES is t, then the strings are considered to be mail addresses,
73 and only the address part is compared (so that \"Name <foo>\" and \"foo\"
74 would be considered to be equivalent.)"
75 (let ((hashtable (make-vector 29 0))
78 (fillarray hashtable 0)
82 (wl-address-header-extract-address (car list))
84 sym-string (or sym-string "-unparseable-garbage-")
85 sym (intern sym-string hashtable))
87 (and all (setcar (symbol-value sym) nil))
88 (setq new-list (cons (car list) new-list))
90 (setq list (cdr list)))
91 (delq nil (nreverse new-list))))
94 (defalias 'wl-string-member 'elmo-string-member)
95 (defalias 'wl-string-match-member 'elmo-string-match-member)
96 (defalias 'wl-string-delete-match 'elmo-string-delete-match)
97 (defalias 'wl-string-match-assoc 'elmo-string-match-assoc)
98 (defalias 'wl-string-assoc 'elmo-string-assoc)
99 (defalias 'wl-string-rassoc 'elmo-string-rassoc)
101 (defun wl-parse-addresses (string)
106 (let (list start s char)
108 (goto-char (point-min))
109 (skip-chars-forward "\t\f\n\r ")
112 (skip-chars-forward "^\"\\,(")
113 (setq char (following-char))
119 (setq s (buffer-substring start (point)))
120 (if (or (null (string-match "^[\t\f\n\r ]+$" s))
121 (not (string= s "")))
122 (setq list (cons s list)))
123 (skip-chars-forward ",\t\f\n\r ")
124 (setq start (point)))
126 (re-search-forward "[^\\]\"" nil 0))
130 (while (and (not (eobp)) (not (zerop parens)))
131 (re-search-forward "[()]" nil 0)
133 (= (char-after (- (point) 2)) ?\\)))
134 ((= (preceding-char) ?\()
135 (setq parens (1+ parens)))
137 (setq parens (1- parens)))))))))
138 (setq s (buffer-substring start (point)))
139 (if (and (null (string-match "^[\t\f\n\r ]+$" s))
140 (not (string= s "")))
141 (setq list (cons s list)))
142 (nreverse list)) ; jwz: fixed order
145 (defun wl-append-element (list element)
147 (append list (list element))
150 (defun wl-read-event-char ()
151 "Get the next event."
152 (let ((event (read-event)))
153 ;; should be gnus-characterp, but this can't be called in XEmacs anyway
154 (cons (and (numberp event) event) event)))
156 (defun wl-xmas-read-event-char ()
157 "Get the next event."
158 (let ((event (next-command-event)))
160 ;; We junk all non-key events. Is this naughty?
161 (while (not (or (key-press-event-p event)
162 (button-press-event-p event)))
163 (dispatch-event event)
164 (setq event (next-command-event)))
165 (cons (and (key-press-event-p event)
166 (event-to-character event))
170 (fset 'wl-read-event-char 'wl-xmas-read-event-char))
172 (defmacro wl-push (v l)
173 "Insert V at the head of the list stored in L."
174 (list 'setq l (list 'cons v l)))
177 "Remove the head of the list stored in L."
178 (list 'car (list 'prog1 l (list 'setq l (list 'cdr l)))))
180 (defun wl-ask-folder (func mes-string)
182 (cmd (if (featurep 'xemacs)
183 (event-to-character last-command-event)
184 (string-to-char (format "%s" (this-command-keys))))))
186 (setq key (car (setq keve (wl-read-event-char))))
187 (if (or (equal key ?\ )
193 (wl-push (cdr keve) unread-command-events))))
195 ;(defalias 'wl-make-hash 'elmo-make-hash)
196 ;;(make-obsolete 'wl-make-hash 'elmo-make-hash)
198 ;;(defalias 'wl-get-hash-val 'elmo-get-hash-val)
199 ;;(make-obsolete 'wl-get-hash-val 'elmo-get-hash-val)
201 ;;(defalias 'wl-set-hash-val 'elmo-set-hash-val)
202 ;;(make-obsolete 'wl-set-hash-val 'elmo-set-hash-val)
204 (defsubst wl-set-string-width (width string)
206 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
208 (if (> (current-column) width)
209 (if (> (move-to-column width) width)
211 (condition-case nil ; ignore error
214 (concat (buffer-substring (point-min) (point)) " "))
215 (buffer-substring (point-min) (point)))
216 (if (= (current-column) width)
219 (format (format "%%%ds"
220 (- width (current-column)))
223 (defun wl-display-bytes (num)
226 ((> (setq result (/ num 1000000)) 0)
227 (setq remain (% num 1000000))
228 (if (> remain 400000)
229 (setq result (+ 1 result)))
230 (format "%dM" result))
231 ((> (setq result (/ num 1000)) 0)
232 (setq remain (% num 1000))
234 (setq result (+ 1 result)))
235 (format "%dK" result))
236 (t (format "%dB" result)))))
238 (defun wl-mode-line-buffer-identification (&optional id)
239 (let ((priorities '(biff plug title)))
240 (let ((items (reverse wl-mode-line-display-priority-list))
243 (setq item (car items)
245 (unless (memq item '(biff plug))
247 (setq priorities (cons item (delq item priorities)))))
248 (let (priority result)
250 (setq priority (car priorities)
251 priorities (cdr priorities))
254 (when wl-biff-check-folder-list
255 (setq result (append result '((wl-modeline-biff-status
256 wl-modeline-biff-state-on
257 wl-modeline-biff-state-off))))))
259 (when wl-show-plug-status-on-modeline
260 (setq result (append result '((wl-modeline-plug-status
261 wl-modeline-plug-state-on
262 wl-modeline-plug-state-off))))))
264 (setq result (append result (or id '("Wanderlust: %12b")))))))
266 (setq mode-line-buffer-identification (if (stringp (car result))
269 (force-mode-line-update t)))))
271 (defalias 'wl-display-error 'elmo-display-error)
272 (make-obsolete 'wl-display-error 'elmo-display-error)
274 (defun wl-get-assoc-list-value (assoc-list folder &optional match)
276 (let ((alist assoc-list)
279 (setq pair (car alist))
280 (if (string-match (car pair) folder)
281 (cond ((eq match 'all)
282 (setq value (append value (list (cdr pair)))))
283 ((eq match 'all-list)
284 (setq value (append value (cdr pair))))
286 (throw 'found (cdr pair)))))
287 (setq alist (cdr alist)))
290 (defmacro wl-match-string (pos string)
291 "Substring POSth matched STRING."
292 (` (substring (, string) (match-beginning (, pos)) (match-end (, pos)))))
294 (defmacro wl-match-buffer (pos)
295 "Substring POSth matched from the current buffer."
296 (` (buffer-substring-no-properties
297 (match-beginning (, pos)) (match-end (, pos)))))
299 (put 'wl-as-coding-system 'lisp-indent-function 1)
300 (put 'wl-as-mime-charset 'lisp-indent-function 1)
304 (defmacro wl-as-coding-system (coding-system &rest body)
305 (` (let ((coding-system-for-read (, coding-system))
306 (coding-system-for-write (, coding-system)))
309 (defmacro wl-as-coding-system (coding-system &rest body)
310 (` (let ((file-coding-system-for-read (, coding-system))
311 (file-coding-system (, coding-system)))
314 (defmacro wl-as-coding-system (coding-system &rest body)
315 (` (let ((default-kanji-fileio-code (, coding-system))
316 (kanji-fileio-code (, coding-system))
320 (defmacro wl-as-mime-charset (mime-charset &rest body)
321 (` (wl-as-coding-system (mime-charset-to-coding-system (, mime-charset))
324 (defalias 'wl-string 'elmo-string)
325 (make-obsolete 'wl-string 'elmo-string)
327 (defun wl-parse-newsgroups (string &optional subscribe-only)
328 (let* ((nglist (wl-parse string "[ \t\f\r\n,]*\\([^ \t\f\r\n,]+\\)"))
330 (if (not subscribe-only)
333 (if (intern-soft (car nglist) wl-folder-newsgroups-hashtb)
334 (wl-append ret-val (list (car nglist))))
335 (setq nglist (cdr nglist)))
338 ;; Check if active region exists or not.
339 (if (boundp 'mark-active)
340 (defmacro wl-region-exists-p ()
342 (if (fboundp 'region-exists-p)
343 (defmacro wl-region-exists-p ()
344 (list 'region-exists-p))))
346 (if (not (fboundp 'overlays-in))
347 (defun overlays-in (beg end)
348 "Return a list of the overlays that overlap the region BEG ... END.
349 Overlap means that at least one character is contained within the overlay
350 and also contained within the specified region.
351 Empty overlays are included in the result if they are located at BEG
352 or between BEG and END."
353 (let ((ovls (overlay-lists))
359 (setq ovls (nconc (car ovls) (cdr ovls)))
363 (if (or (and (<= (overlay-start tmp) end)
364 (>= (overlay-start tmp) beg))
365 (and (<= (overlay-end tmp) end)
366 (>= (overlay-end tmp) beg)))
367 (setq retval (cons tmp retval))))
370 (defsubst wl-repeat-string (str times)
374 (setq ret-val (concat ret-val str))
375 (setq loop (- loop 1)))
378 (defun wl-list-diff (list1 list2)
379 "Return a list of elements of LIST1 that do not appear in LIST2."
380 (let ((list1 (copy-sequence list1)))
382 (setq list1 (delq (car list2) list1))
383 (setq list2 (cdr list2)))
386 (defun wl-append-assoc-list (item value alist)
387 "make assoc list '((item1 value1-1 value1-2 ...)) (item2 value2-1 ...)))"
388 (let ((entry (assoc item alist)))
391 (when (not (member value (cdr entry)))
392 (nconc entry (list value)))
395 (list (list item value))))))
397 (defun wl-delete-alist (key alist)
398 "Delete by side effect any entries specified with KEY from ALIST.
399 Return the modified ALIST. Key comparison is done with `assq'.
400 Write `(setq foo (wl-delete-alist key foo))' to be sure of changing
403 (while (setq entry (assq key alist))
404 (setq alist (delq entry alist)))
407 (defun wl-delete-associations (keys alist)
408 "Delete by side effect any entries specified with KEYS from ALIST.
409 Return the modified ALIST. KEYS must be a list of keys for ALIST.
410 Deletion is done with `wl-delete-alist'.
411 Write `(setq foo (wl-delete-associations keys foo))' to be sure of
412 changing the value of `foo'."
414 (setq alist (wl-delete-alist (car keys) alist))
415 (setq keys (cdr keys)))
418 (defun wl-inverse-alist (keys alist)
419 "Inverse ALIST, copying.
420 Return an association list represents the inverse mapping of ALIST,
421 from objects to KEYS.
422 The objects mapped (cdrs of elements of the ALIST) are shared."
423 (let (x y tmp result)
426 (setq y (cdr (assq x alist)))
428 (if (setq tmp (assoc y result))
429 (setq result (cons (append tmp (list x))
430 (delete tmp result)))
431 (setq result (cons (list y x) result))))
432 (setq keys (cdr keys)))
437 (static-unless (fboundp 'pp)
438 (defvar pp-escape-newlines t)
439 (defun pp (object &optional stream)
440 "Output the pretty-printed representation of OBJECT, any Lisp object.
441 Quoting characters are printed when needed to make output that `read'
442 can handle, whenever this is possible.
443 Output stream is STREAM, or value of `standard-output' (which see)."
444 (princ (pp-to-string object) (or stream standard-output)))
446 (defun pp-to-string (object)
447 "Return a string containing the pretty-printed representation of OBJECT,
448 any Lisp object. Quoting characters are used when needed to make output
449 that `read' can handle, whenever this is possible."
451 (set-buffer (generate-new-buffer " pp-to-string"))
454 (lisp-mode-variables t)
455 (let ((print-escape-newlines pp-escape-newlines))
456 (prin1 object (current-buffer)))
457 (goto-char (point-min))
460 ((looking-at "\\s(\\|#\\s(")
461 (while (looking-at "\\s(\\|#\\s(")
463 ((and (looking-at "\\(quote[ \t]+\\)\\([^.)]\\)")
464 (> (match-beginning 1) 1)
465 (= ?\( (char-after (1- (match-beginning 1))))
466 ;; Make sure this is a two-element list.
468 (goto-char (match-beginning 2))
470 ;; Avoid mucking with match-data; does this test work?
471 (char-equal ?\) (char-after (point)))))
472 ;; -1 gets the paren preceding the quote as well.
473 (delete-region (1- (match-beginning 1)) (match-end 1))
476 (if (looking-at "[ \t]*\)")
477 (delete-region (match-beginning 0) (match-end 0))
478 (error "Malformed quote"))
480 ((condition-case err-var
481 (prog1 t (down-list 1))
484 (skip-chars-backward " \t")
487 (progn (skip-chars-forward " \t") (point)))
488 (if (not (char-equal ?' (char-after (1- (point)))))
490 ((condition-case err-var
491 (prog1 t (up-list 1))
493 (while (looking-at "\\s)")
495 (skip-chars-backward " \t")
498 (progn (skip-chars-forward " \t") (point)))
499 (if (not (char-equal ?' (char-after (1- (point)))))
501 (t (goto-char (point-max)))))
502 (goto-char (point-min))
505 (kill-buffer (current-buffer))))))
507 (defsubst wl-get-date-iso8601 (date)
508 (or (get-text-property 0 'wl-date date)
509 (let* ((d1 (timezone-fix-time date nil nil))
510 (time (format "%04d%02d%02dT%02d%02d%02d"
511 (aref d1 0) (aref d1 1) (aref d1 2)
512 (aref d1 3) (aref d1 4) (aref d1 5))))
513 (put-text-property 0 1 'wl-date time date)
516 (defun wl-make-date-string ()
517 (let ((s (current-time-string)))
518 (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]"
520 (concat (wl-match-string 1 s) ", "
521 (timezone-make-date-arpa-standard s (current-time-zone)))))
523 (defun wl-date-iso8601 (date)
524 "Convert the DATE to YYMMDDTHHMMSS."
526 (wl-get-date-iso8601 date)
529 (defun wl-day-number (date)
530 (let ((dat (mapcar '(lambda (s) (and s (string-to-int s)) )
531 (timezone-parse-date date))))
532 (timezone-absolute-from-gregorian
533 (nth 1 dat) (nth 2 dat) (car dat))))
535 (defun wl-url-news (url &rest args)
536 (interactive "sURL: ")
537 (if (string-match "^news:\\(.*\\)$" url)
538 (wl-summary-goto-folder-subr
539 (concat "-" (elmo-match-string 1 url)) nil nil nil t)
540 (message "Not a news: url.")))
542 (defun wl-url-nntp (url &rest args)
543 (interactive "sURL: ")
544 (let (folder fld-name server port msg)
546 "^nntp://\\([^:/]*\\):?\\([0-9]*\\)/\\([^/]*\\)/\\([0-9]*\\).*$" url)
548 (if (eq (length (setq fld-name
549 (elmo-match-string 3 url))) 0)
551 (if (eq (length (setq port
552 (elmo-match-string 2 url))) 0)
553 (setq port (int-to-string elmo-default-nntp-port)))
554 (if (eq (length (setq server
555 (elmo-match-string 1 url))) 0)
556 (setq server elmo-default-nntp-server))
557 (setq folder (concat "-" fld-name "@" server ":" port))
558 (if (eq (length (setq msg
559 (elmo-match-string 4 url))) 0)
560 (wl-summary-goto-folder-subr
561 folder nil nil nil t)
562 (wl-summary-goto-folder-subr
563 folder 'update nil nil t)
564 (goto-char (point-min))
565 (re-search-forward (concat "^ *" msg) nil t)
566 (wl-summary-redisplay)))
567 (message "Not a nntp: url."))))
569 (defmacro wl-concat-list (list separator)
570 (` (mapconcat 'identity (delete "" (delq nil (, list))) (, separator))))
572 (defmacro wl-current-message-buffer ()
574 (if (buffer-live-p wl-current-summary-buffer)
575 (set-buffer wl-current-summary-buffer))
576 wl-message-buf-name)))
578 (defmacro wl-kill-buffers (regexp)
581 (if (and (buffer-name x)
582 (string-match (, regexp) (buffer-name x)))
587 (defun wl-sendlog-time ()
588 (static-if (fboundp 'format-time-string)
589 (format-time-string "%Y/%m/%d %T")
590 (let ((date (current-time-string)))
591 (format "%s/%02d/%02d %s"
593 (cdr (assoc (upcase (substring date 4 7))
594 timezone-months-assoc))
595 (string-to-int (substring date 8 10))
596 (substring date 11 19)))))
598 (defun wl-collect-summary ()
601 (function (lambda (x)
602 (if (and (string-match "^Summary"
606 (equal major-mode 'wl-summary-mode)))
607 (setq result (nconc result (list x))))))
611 (static-if (fboundp 'read-directory-name)
612 (defalias 'wl-read-directory-name 'read-directory-name)
613 (defun wl-read-directory-name (prompt dir)
614 (let ((dir (read-file-name prompt dir)))
615 (unless (file-directory-p dir)
616 (error "%s is not directory" dir))
619 ;; local variable check.
620 (static-if (fboundp 'local-variable-p)
621 (defalias 'wl-local-variable-p 'local-variable-p)
622 (defmacro wl-local-variable-p (symbol &optional buffer)
623 (` (if (assq (, symbol) (buffer-local-variables (, buffer)))
626 (defun wl-number-base36 (num len)
631 (concat (wl-number-base36 (/ num 36) (1- len))
632 (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
635 (defvar wl-unique-id-char nil)
637 (defun wl-unique-id ()
638 ;; Don't use microseconds from (current-time), they may be unsupported.
639 ;; Instead we use this randomly inited counter.
640 (setq wl-unique-id-char
641 (% (1+ (or wl-unique-id-char (logand (random t) (1- (lsh 1 20)))))
642 ;; (current-time) returns 16-bit ints,
643 ;; and 2^16*25 just fits into 4 digits i base 36.
645 (let ((tm (static-if (fboundp 'current-time)
647 (let* ((cts (split-string (current-time-string) "[ :]"))
648 (m (cdr (assoc (nth 1 cts)
649 '(("Jan" . "01") ("Feb" . "02")
650 ("Mar" . "03") ("Apr" . "04")
651 ("May" . "05") ("Jun" . "06")
652 ("Jul" . "07") ("Aug" . "08")
653 ("Sep" . "09") ("Oct" . "10")
654 ("Nov" . "11") ("Dec" . "12"))))))
655 (list (string-to-int (concat (nth 6 cts) m
656 (substring (nth 2 cts) 0 1)))
657 (string-to-int (concat (substring (nth 2 cts) 1)
658 (nth 4 cts) (nth 5 cts)
661 (if (memq system-type '(ms-dos emx vax-vms))
662 (let ((user (downcase (user-login-name))))
663 (while (string-match "[^a-z0-9_]" user)
664 (aset user (match-beginning 0) ?_))
666 (wl-number-base36 (user-uid) -1))
667 (wl-number-base36 (+ (car tm)
668 (lsh (% wl-unique-id-char 25) 16)) 4)
669 (wl-number-base36 (+ (nth 1 tm)
670 (lsh (/ wl-unique-id-char 25) 16)) 4)
671 ;; Append the name of the message interface, because while the
672 ;; generated ID is unique to this newsreader, other newsreaders
673 ;; might otherwise generate the same ID via another algorithm.
676 (defun wl-draft-make-message-id-string ()
677 "Return Message-ID field value."
678 (concat "<" (wl-unique-id) "@"
679 (or wl-message-id-domain
681 (concat (system-name) "." wl-local-domain)
686 (defvar wl-load-profile-func 'wl-local-load-profile)
687 (defun wl-local-load-profile ()
688 "Load `wl-init-file'."
689 (message "Initializing ...")
690 (load wl-init-file 'noerror 'nomessage))
692 (defun wl-load-profile ()
693 "Call `wl-load-profile-func' function."
694 (funcall wl-load-profile-func))
698 (defmacro wl-count-lines ()
701 (count-lines 1 (point)))))
703 (defun wl-horizontal-recenter ()
704 "Recenter the current buffer horizontally."
706 (re-search-forward "[[<]" (point-at-eol) t)
707 (if (< (current-column) (/ (window-width) 2))
708 (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
709 (let* ((orig (point))
710 (end (window-end (get-buffer-window (current-buffer) t)))
713 ;; Find the longest line currently displayed in the window.
714 (goto-char (window-start))
715 (while (and (not (eobp))
718 (setq max (max max (current-column)))
721 ;; Scroll horizontally to center (sort of) the point.
722 (if (> max (window-width))
724 (get-buffer-window (current-buffer) t)
725 (min (- (current-column) (/ (window-width) 3))
726 (+ 2 (- max (window-width)))))
727 (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
733 (defvar wl-biff-timer-name "wl-biff")
735 (defun wl-biff-stop ()
736 (when (get-itimer wl-biff-timer-name)
737 (delete-itimer wl-biff-timer-name)))
739 (defun wl-biff-start ()
741 (when wl-biff-check-folder-list
742 (wl-biff-check-folders)
743 (start-itimer wl-biff-timer-name 'wl-biff-check-folders
744 wl-biff-check-interval wl-biff-check-interval))))
746 ((condition-case nil (require 'timer) (error nil));; FSFmacs 19+
748 (defun wl-biff-stop ()
749 (put 'wl-biff 'timer nil))
751 (defun wl-biff-start ()
753 (when wl-biff-check-folder-list
754 (wl-biff-check-folders)
755 (put 'wl-biff 'timer (run-at-time
756 (timer-next-integral-multiple-of-time
757 (current-time) wl-biff-check-interval)
758 wl-biff-check-interval
759 'wl-biff-event-handler))))
761 (defun-maybe timer-next-integral-multiple-of-time (time secs)
762 "Yield the next value after TIME that is an integral multiple of SECS.
763 More precisely, the next value, after TIME, that is an integral multiple
764 of SECS seconds since the epoch. SECS may be a fraction.
765 This function is imported from Emacs 20.7."
766 (let ((time-base (ash 1 16)))
768 ;; Use floating point, taking care to not lose precision.
769 (let* ((float-time-base (float time-base))
771 (time-usec (+ (* million
772 (+ (* float-time-base (nth 0 time))
775 (secs-usec (* million secs))
776 (mod-usec (mod time-usec secs-usec))
777 (next-usec (+ (- time-usec mod-usec) secs-usec))
778 (time-base-million (* float-time-base million)))
779 (list (floor next-usec time-base-million)
780 (floor (mod next-usec time-base-million) million)
781 (floor (mod next-usec million))))
782 ;; Floating point is not supported.
783 ;; Use integer arithmetic, avoiding overflow if possible.
784 (let* ((mod-sec (mod (+ (* (mod time-base secs)
785 (mod (nth 0 time) secs))
788 (next-1-sec (+ (- (nth 1 time) mod-sec) secs)))
789 (list (+ (nth 0 time) (floor next-1-sec time-base))
790 (mod next-1-sec time-base)
793 (defun wl-biff-event-handler ()
794 ;; PAKURing from FSF:time.el
795 (wl-biff-check-folders)
796 ;; Do redisplay right now, if no input pending.
798 (let* ((current (current-time))
799 (timer (get 'wl-biff 'timer))
800 ;; Compute the time when this timer will run again, next.
801 (next-time (timer-relative-time
802 (list (aref timer 1) (aref timer 2) (aref timer 3))
803 (* 5 (aref timer 4)) 0)))
804 ;; If the activation time is far in the past,
805 ;; skip executions until we reach a time in the future.
806 ;; This avoids a long pause if Emacs has been suspended for hours.
807 (or (> (nth 0 next-time) (nth 0 current))
808 (and (= (nth 0 next-time) (nth 0 current))
809 (> (nth 1 next-time) (nth 1 current)))
810 (and (= (nth 0 next-time) (nth 0 current))
811 (= (nth 1 next-time) (nth 1 current))
812 (> (nth 2 next-time) (nth 2 current)))
814 (timer-set-time timer (timer-next-integral-multiple-of-time
815 current wl-biff-check-interval)
816 wl-biff-check-interval)
817 (timer-activate timer))))))
819 (fset 'wl-biff-stop 'ignore)
820 (fset 'wl-biff-start 'ignore)))
822 (defsubst wl-biff-notify (new-mails notify-minibuf)
823 (setq wl-modeline-biff-status (> new-mails 0))
824 (force-mode-line-update t)
826 (cond ((zerop new-mails) (message "No mail."))
827 ((eq 1 new-mails) (message "You have a new mail."))
828 (t (message "You have %d new mails." new-mails)))))
830 ;; Internal variable.
831 (defvar wl-biff-check-folders-running nil)
833 (defun wl-biff-check-folders ()
835 (if wl-biff-check-folders-running
836 (when (interactive-p)
837 (message "Biff process is running."))
838 (setq wl-biff-check-folders-running t)
839 (when (interactive-p)
840 (message "Checking new mails..."))
842 (flist (or wl-biff-check-folder-list (list wl-default-folder)))
844 (if (eq (length flist) 1)
845 (wl-biff-check-folder-async (car flist) (interactive-p))
848 (setq folder (car flist)
850 (when (elmo-folder-plugged-p folder)
853 (nth 0 (wl-biff-check-folder folder))))))
854 (setq wl-biff-check-folders-running nil)
855 (wl-biff-notify new-mails (interactive-p)))))))
857 (defun wl-biff-check-folder (folder)
858 (if (eq (elmo-folder-get-type folder) 'pop3)
859 ;; pop3 biff should share the session.
861 (elmo-commit folder) ; Close session.
862 (wl-folder-check-one-entity folder)
863 (elmo-commit folder))
864 (let ((elmo-network-session-name-prefix "BIFF-"))
865 (wl-folder-check-one-entity folder))))
867 (defun wl-biff-check-folder-async-callback (diff data)
869 (with-current-buffer (nth 1 data)
870 (wl-folder-entity-hashtb-set wl-folder-entity-hashtb (nth 0 data)
871 (list (car diff) 0 (cdr diff))
873 (setq wl-folder-info-alist-modified t)
874 (setq wl-biff-check-folders-running nil)
876 (wl-biff-notify (car diff) (nth 2 data)))
878 (defun wl-biff-check-folder-async (folder notify-minibuf)
879 (when (elmo-folder-plugged-p folder)
880 (let ((type (elmo-folder-get-type folder)))
881 (if (and (eq type 'imap4)
882 (wl-folder-use-server-diff-p folder))
883 ;; Check asynchronously only when IMAP4 and use server diff.
885 (setq elmo-folder-diff-async-callback
886 'wl-biff-check-folder-async-callback)
887 (setq elmo-folder-diff-async-callback-data
888 (list folder (get-buffer wl-folder-buffer-name)
890 (let ((elmo-network-session-name-prefix "BIFF-"))
891 (elmo-folder-diff-async folder)))
892 (wl-biff-notify (car (wl-biff-check-folder folder))
894 (setq wl-biff-check-folders-running nil))))
896 ;;; wl-util.el ends here