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 'tm-edit) (error nil))
40 (condition-case nil (require 'pp) (error nil))
44 (defalias-maybe 'next-command-event 'ignore)
45 (defalias-maybe 'event-to-character 'ignore)
46 (defalias-maybe 'key-press-event-p 'ignore)
47 (defalias-maybe 'button-press-event-p 'ignore)
48 (defalias-maybe 'set-process-kanji-code 'ignore)
49 (defalias-maybe 'set-process-coding-system 'ignore)
50 (defalias-maybe 'dispatch-event 'ignore))
52 (defalias 'wl-set-work-buf 'elmo-set-work-buf)
53 (make-obsolete 'wl-set-work-buf 'elmo-set-work-buf)
55 (defmacro wl-append (val func)
57 (list 'nconc val func)
58 (list 'setq val func)))
60 (defun wl-parse (string regexp &optional matchn)
61 (or matchn (setq matchn 1))
63 (store-match-data nil)
64 (while (string-match regexp string (match-end 0))
65 (setq list (cons (substring string (match-beginning matchn)
66 (match-end matchn)) list)))
69 (defun wl-delete-duplicates (list &optional all hack-addresses)
70 "Delete duplicate equivalent strings from the LIST.
71 If ALL is t, then if there is more than one occurrence of a string in the LIST,
72 then all occurrences of it are removed instead of just the subsequent ones.
73 If HACK-ADDRESSES is t, then the strings are considered to be mail addresses,
74 and only the address part is compared (so that \"Name <foo>\" and \"foo\"
75 would be considered to be equivalent.)"
76 (let ((hashtable (make-vector 29 0))
79 (fillarray hashtable 0)
83 (wl-address-header-extract-address (car list))
85 sym-string (or sym-string "-unparseable-garbage-")
86 sym (intern sym-string hashtable))
88 (and all (setcar (symbol-value sym) nil))
89 (setq new-list (cons (car list) new-list))
91 (setq list (cdr list)))
92 (delq nil (nreverse new-list))))
95 (defalias 'wl-string-member 'elmo-string-member)
96 (defalias 'wl-string-match-member 'elmo-string-match-member)
97 (defalias 'wl-string-delete-match 'elmo-string-delete-match)
98 (defalias 'wl-string-match-assoc 'elmo-string-match-assoc)
99 (defalias 'wl-string-assoc 'elmo-string-assoc)
100 (defalias 'wl-string-rassoc 'elmo-string-rassoc)
102 (defun wl-parse-addresses (string)
107 (let (list start s char)
109 (goto-char (point-min))
110 (skip-chars-forward "\t\f\n\r ")
113 (skip-chars-forward "^\"\\,(")
114 (setq char (following-char))
120 (setq s (buffer-substring start (point)))
121 (if (or (null (string-match "^[\t\f\n\r ]+$" s))
122 (not (string= s "")))
123 (setq list (cons s list)))
124 (skip-chars-forward ",\t\f\n\r ")
125 (setq start (point)))
127 (re-search-forward "[^\\]\"" nil 0))
131 (while (and (not (eobp)) (not (zerop parens)))
132 (re-search-forward "[()]" nil 0)
134 (= (char-after (- (point) 2)) ?\\)))
135 ((= (preceding-char) ?\()
136 (setq parens (1+ parens)))
138 (setq parens (1- parens)))))))))
139 (setq s (buffer-substring start (point)))
140 (if (and (null (string-match "^[\t\f\n\r ]+$" s))
141 (not (string= s "")))
142 (setq list (cons s list)))
143 (nreverse list)) ; jwz: fixed order
146 (defun wl-append-element (list element)
148 (append list (list element))
151 (defmacro wl-push (v l)
152 "Insert V at the head of the list stored in L."
153 (list 'setq l (list 'cons v l)))
156 "Remove the head of the list stored in L."
157 (list 'car (list 'prog1 l (list 'setq l (list 'cdr l)))))
159 (defun wl-ask-folder (func mes-string)
161 (cmd (if (featurep 'xemacs)
162 (event-to-character last-command-event)
163 (string-to-char (format "%s" (this-command-keys))))))
165 (setq key (car (setq keve (wl-read-event-char))))
166 (if (or (equal key ?\ )
172 (wl-push (cdr keve) unread-command-events))))
174 ;(defalias 'wl-make-hash 'elmo-make-hash)
175 ;;(make-obsolete 'wl-make-hash 'elmo-make-hash)
177 ;;(defalias 'wl-get-hash-val 'elmo-get-hash-val)
178 ;;(make-obsolete 'wl-get-hash-val 'elmo-get-hash-val)
180 ;;(defalias 'wl-set-hash-val 'elmo-set-hash-val)
181 ;;(make-obsolete 'wl-set-hash-val 'elmo-set-hash-val)
183 (defsubst wl-set-string-width (width string)
185 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
187 (if (> (current-column) width)
188 (if (> (move-to-column width) width)
190 (condition-case nil ; ignore error
193 (concat (buffer-substring (point-min) (point)) " "))
194 (buffer-substring (point-min) (point)))
195 (if (= (current-column) width)
198 (format (format "%%%ds"
199 (- width (current-column)))
202 (defun wl-display-bytes (num)
205 ((> (setq result (/ num 1000000)) 0)
206 (setq remain (% num 1000000))
207 (if (> remain 400000)
208 (setq result (+ 1 result)))
209 (format "%dM" result))
210 ((> (setq result (/ num 1000)) 0)
211 (setq remain (% num 1000))
213 (setq result (+ 1 result)))
214 (format "%dK" result))
215 (t (format "%dB" result)))))
217 (defun wl-mode-line-buffer-identification (&optional id)
218 (let ((priorities '(biff plug title)))
219 (let ((items (reverse wl-mode-line-display-priority-list))
222 (setq item (car items)
224 (unless (memq item '(biff plug))
226 (setq priorities (cons item (delq item priorities)))))
227 (let (priority result)
229 (setq priority (car priorities)
230 priorities (cdr priorities))
233 (when wl-biff-check-folder-list
234 (setq result (append result '((wl-modeline-biff-status
235 wl-modeline-biff-state-on
236 wl-modeline-biff-state-off))))))
238 (when wl-show-plug-status-on-modeline
239 (setq result (append result '((wl-modeline-plug-status
240 wl-modeline-plug-state-on
241 wl-modeline-plug-state-off))))))
243 (setq result (append result (or id '("Wanderlust: %12b")))))))
245 (setq mode-line-buffer-identification (if (stringp (car result))
248 (force-mode-line-update t)))))
250 (defalias 'wl-display-error 'elmo-display-error)
251 (make-obsolete 'wl-display-error 'elmo-display-error)
253 (defun wl-get-assoc-list-value (assoc-list folder &optional match)
255 (let ((alist assoc-list)
258 (setq pair (car alist))
259 (if (string-match (car pair) folder)
260 (cond ((eq match 'all)
261 (setq value (append value (list (cdr pair)))))
262 ((eq match 'all-list)
263 (setq value (append value (cdr pair))))
265 (throw 'found (cdr pair)))))
266 (setq alist (cdr alist)))
269 (defmacro wl-match-string (pos string)
270 "Substring POSth matched STRING."
271 (` (substring (, string) (match-beginning (, pos)) (match-end (, pos)))))
273 (defmacro wl-match-buffer (pos)
274 "Substring POSth matched from the current buffer."
275 (` (buffer-substring-no-properties
276 (match-beginning (, pos)) (match-end (, pos)))))
278 (put 'wl-as-coding-system 'lisp-indent-function 1)
279 (put 'wl-as-mime-charset 'lisp-indent-function 1)
283 (defmacro wl-as-coding-system (coding-system &rest body)
284 (` (let ((coding-system-for-read (, coding-system))
285 (coding-system-for-write (, coding-system)))
288 (defmacro wl-as-coding-system (coding-system &rest body)
289 (` (let ((file-coding-system-for-read (, coding-system))
290 (file-coding-system (, coding-system)))
293 (defmacro wl-as-coding-system (coding-system &rest body)
294 (` (let ((default-kanji-fileio-code (, coding-system))
295 (kanji-fileio-code (, coding-system))
299 (defmacro wl-as-mime-charset (mime-charset &rest body)
300 (` (wl-as-coding-system (mime-charset-to-coding-system (, mime-charset))
303 (defalias 'wl-string 'elmo-string)
304 (make-obsolete 'wl-string 'elmo-string)
306 (defun wl-parse-newsgroups (string &optional subscribe-only)
307 (let* ((nglist (wl-parse string "[ \t\f\r\n,]*\\([^ \t\f\r\n,]+\\)"))
309 (if (not subscribe-only)
312 (if (intern-soft (car nglist) wl-folder-newsgroups-hashtb)
313 (wl-append ret-val (list (car nglist))))
314 (setq nglist (cdr nglist)))
317 ;; Check if active region exists or not.
318 (if (boundp 'mark-active)
319 (defmacro wl-region-exists-p ()
321 (if (fboundp 'region-exists-p)
322 (defmacro wl-region-exists-p ()
323 (list 'region-exists-p))))
325 (if (not (fboundp 'overlays-in))
326 (defun overlays-in (beg end)
327 "Return a list of the overlays that overlap the region BEG ... END.
328 Overlap means that at least one character is contained within the overlay
329 and also contained within the specified region.
330 Empty overlays are included in the result if they are located at BEG
331 or between BEG and END."
332 (let ((ovls (overlay-lists))
338 (setq ovls (nconc (car ovls) (cdr ovls)))
342 (if (or (and (<= (overlay-start tmp) end)
343 (>= (overlay-start tmp) beg))
344 (and (<= (overlay-end tmp) end)
345 (>= (overlay-end tmp) beg)))
346 (setq retval (cons tmp retval))))
349 (defsubst wl-repeat-string (str times)
353 (setq ret-val (concat ret-val str))
354 (setq loop (- loop 1)))
357 (defun wl-list-diff (list1 list2)
358 "Return a list of elements of LIST1 that do not appear in LIST2."
359 (let ((list1 (copy-sequence list1)))
361 (setq list1 (delq (car list2) list1))
362 (setq list2 (cdr list2)))
365 (defun wl-append-assoc-list (item value alist)
366 "make assoc list '((item1 value1-1 value1-2 ...)) (item2 value2-1 ...)))"
367 (let ((entry (assoc item alist)))
370 (when (not (member value (cdr entry)))
371 (nconc entry (list value)))
374 (list (list item value))))))
376 (defun wl-delete-alist (key alist)
377 "Delete by side effect any entries specified with KEY from ALIST.
378 Return the modified ALIST. Key comparison is done with `assq'.
379 Write `(setq foo (wl-delete-alist key foo))' to be sure of changing
382 (while (setq entry (assq key alist))
383 (setq alist (delq entry alist)))
386 (defun wl-delete-associations (keys alist)
387 "Delete by side effect any entries specified with KEYS from ALIST.
388 Return the modified ALIST. KEYS must be a list of keys for ALIST.
389 Deletion is done with `wl-delete-alist'.
390 Write `(setq foo (wl-delete-associations keys foo))' to be sure of
391 changing the value of `foo'."
393 (setq alist (wl-delete-alist (car keys) alist))
394 (setq keys (cdr keys)))
397 (defun wl-inverse-alist (keys alist)
398 "Inverse ALIST, copying.
399 Return an association list represents the inverse mapping of ALIST,
400 from objects to KEYS.
401 The objects mapped (cdrs of elements of the ALIST) are shared."
402 (let (x y tmp result)
405 (setq y (cdr (assq x alist)))
407 (if (setq tmp (assoc y result))
408 (setq result (cons (append tmp (list x))
409 (delete tmp result)))
410 (setq result (cons (list y x) result))))
411 (setq keys (cdr keys)))
416 (static-unless (fboundp 'pp)
417 (defvar pp-escape-newlines t)
418 (defun pp (object &optional stream)
419 "Output the pretty-printed representation of OBJECT, any Lisp object.
420 Quoting characters are printed when needed to make output that `read'
421 can handle, whenever this is possible.
422 Output stream is STREAM, or value of `standard-output' (which see)."
423 (princ (pp-to-string object) (or stream standard-output)))
425 (defun pp-to-string (object)
426 "Return a string containing the pretty-printed representation of OBJECT,
427 any Lisp object. Quoting characters are used when needed to make output
428 that `read' can handle, whenever this is possible."
430 (set-buffer (generate-new-buffer " pp-to-string"))
433 (lisp-mode-variables t)
434 (let ((print-escape-newlines pp-escape-newlines))
435 (prin1 object (current-buffer)))
436 (goto-char (point-min))
439 ((looking-at "\\s(\\|#\\s(")
440 (while (looking-at "\\s(\\|#\\s(")
442 ((and (looking-at "\\(quote[ \t]+\\)\\([^.)]\\)")
443 (> (match-beginning 1) 1)
444 (= ?\( (char-after (1- (match-beginning 1))))
445 ;; Make sure this is a two-element list.
447 (goto-char (match-beginning 2))
449 ;; Avoid mucking with match-data; does this test work?
450 (char-equal ?\) (char-after (point)))))
451 ;; -1 gets the paren preceding the quote as well.
452 (delete-region (1- (match-beginning 1)) (match-end 1))
455 (if (looking-at "[ \t]*\)")
456 (delete-region (match-beginning 0) (match-end 0))
457 (error "Malformed quote"))
459 ((condition-case err-var
460 (prog1 t (down-list 1))
463 (skip-chars-backward " \t")
466 (progn (skip-chars-forward " \t") (point)))
467 (if (not (char-equal ?' (char-after (1- (point)))))
469 ((condition-case err-var
470 (prog1 t (up-list 1))
472 (while (looking-at "\\s)")
474 (skip-chars-backward " \t")
477 (progn (skip-chars-forward " \t") (point)))
478 (if (not (char-equal ?' (char-after (1- (point)))))
480 (t (goto-char (point-max)))))
481 (goto-char (point-min))
484 (kill-buffer (current-buffer))))))
486 (defsubst wl-get-date-iso8601 (date)
487 (or (get-text-property 0 'wl-date date)
488 (let* ((d1 (timezone-fix-time date nil nil))
489 (time (format "%04d%02d%02dT%02d%02d%02d"
490 (aref d1 0) (aref d1 1) (aref d1 2)
491 (aref d1 3) (aref d1 4) (aref d1 5))))
492 (put-text-property 0 1 'wl-date time date)
495 (defun wl-make-date-string ()
496 (let ((s (current-time-string)))
497 (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]"
499 (concat (wl-match-string 1 s) ", "
500 (timezone-make-date-arpa-standard s (current-time-zone)))))
502 (defun wl-date-iso8601 (date)
503 "Convert the DATE to YYMMDDTHHMMSS."
505 (wl-get-date-iso8601 date)
508 (defun wl-day-number (date)
509 (let ((dat (mapcar '(lambda (s) (and s (string-to-int s)) )
510 (timezone-parse-date date))))
511 (timezone-absolute-from-gregorian
512 (nth 1 dat) (nth 2 dat) (car dat))))
514 (defun wl-url-news (url &rest args)
515 (interactive "sURL: ")
516 (if (string-match "^news:\\(.*\\)$" url)
517 (wl-summary-goto-folder-subr
518 (concat "-" (elmo-match-string 1 url)) nil nil nil t)
519 (message "Not a news: url.")))
521 (defun wl-url-nntp (url &rest args)
522 (interactive "sURL: ")
523 (let (folder fld-name server port msg)
525 "^nntp://\\([^:/]*\\):?\\([0-9]*\\)/\\([^/]*\\)/\\([0-9]*\\).*$" url)
527 (if (eq (length (setq fld-name
528 (elmo-match-string 3 url))) 0)
530 (if (eq (length (setq port
531 (elmo-match-string 2 url))) 0)
532 (setq port (int-to-string elmo-default-nntp-port)))
533 (if (eq (length (setq server
534 (elmo-match-string 1 url))) 0)
535 (setq server elmo-default-nntp-server))
536 (setq folder (concat "-" fld-name "@" server ":" port))
537 (if (eq (length (setq msg
538 (elmo-match-string 4 url))) 0)
539 (wl-summary-goto-folder-subr
540 folder nil nil nil t)
541 (wl-summary-goto-folder-subr
542 folder 'update nil nil t)
543 (goto-char (point-min))
544 (re-search-forward (concat "^ *" msg) nil t)
545 (wl-summary-redisplay)))
546 (message "Not a nntp: url."))))
548 (defmacro wl-concat-list (list separator)
549 (` (mapconcat 'identity (delete "" (delq nil (, list))) (, separator))))
551 (defmacro wl-current-message-buffer ()
553 (if (buffer-live-p wl-current-summary-buffer)
554 (set-buffer wl-current-summary-buffer))
555 wl-message-buf-name)))
557 (defmacro wl-kill-buffers (regexp)
560 (if (and (buffer-name x)
561 (string-match (, regexp) (buffer-name x)))
566 (defun wl-sendlog-time ()
567 (static-if (fboundp 'format-time-string)
568 (format-time-string "%Y/%m/%d %T")
569 (let ((date (current-time-string)))
570 (format "%s/%02d/%02d %s"
572 (cdr (assoc (upcase (substring date 4 7))
573 timezone-months-assoc))
574 (string-to-int (substring date 8 10))
575 (substring date 11 19)))))
577 (defun wl-collect-summary ()
580 (function (lambda (x)
581 (if (and (string-match "^Summary"
585 (equal major-mode 'wl-summary-mode)))
586 (setq result (nconc result (list x))))))
590 (static-if (fboundp 'read-directory-name)
591 (defalias 'wl-read-directory-name 'read-directory-name)
592 (defun wl-read-directory-name (prompt dir)
593 (let ((dir (read-file-name prompt dir)))
594 (unless (file-directory-p dir)
595 (error "%s is not directory" dir))
598 ;; local variable check.
599 (static-if (fboundp 'local-variable-p)
600 (defalias 'wl-local-variable-p 'local-variable-p)
601 (defmacro wl-local-variable-p (symbol &optional buffer)
602 (` (if (assq (, symbol) (buffer-local-variables (, buffer)))
605 (defun wl-number-base36 (num len)
610 (concat (wl-number-base36 (/ num 36) (1- len))
611 (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
614 (defvar wl-unique-id-char nil)
616 (defun wl-unique-id ()
617 ;; Don't use microseconds from (current-time), they may be unsupported.
618 ;; Instead we use this randomly inited counter.
619 (setq wl-unique-id-char
620 (% (1+ (or wl-unique-id-char (logand (random t) (1- (lsh 1 20)))))
621 ;; (current-time) returns 16-bit ints,
622 ;; and 2^16*25 just fits into 4 digits i base 36.
624 (let ((tm (static-if (fboundp 'current-time)
626 (let* ((cts (split-string (current-time-string) "[ :]"))
627 (m (cdr (assoc (nth 1 cts)
628 '(("Jan" . "01") ("Feb" . "02")
629 ("Mar" . "03") ("Apr" . "04")
630 ("May" . "05") ("Jun" . "06")
631 ("Jul" . "07") ("Aug" . "08")
632 ("Sep" . "09") ("Oct" . "10")
633 ("Nov" . "11") ("Dec" . "12"))))))
634 (list (string-to-int (concat (nth 6 cts) m
635 (substring (nth 2 cts) 0 1)))
636 (string-to-int (concat (substring (nth 2 cts) 1)
637 (nth 4 cts) (nth 5 cts)
640 (if (memq system-type '(ms-dos emx vax-vms))
641 (let ((user (downcase (user-login-name))))
642 (while (string-match "[^a-z0-9_]" user)
643 (aset user (match-beginning 0) ?_))
645 (wl-number-base36 (user-uid) -1))
646 (wl-number-base36 (+ (car tm)
647 (lsh (% wl-unique-id-char 25) 16)) 4)
648 (wl-number-base36 (+ (nth 1 tm)
649 (lsh (/ wl-unique-id-char 25) 16)) 4)
650 ;; Append the name of the message interface, because while the
651 ;; generated ID is unique to this newsreader, other newsreaders
652 ;; might otherwise generate the same ID via another algorithm.
655 (defun wl-draft-make-message-id-string ()
656 "Return Message-ID field value."
657 (concat "<" (wl-unique-id) "@"
658 (or wl-message-id-domain
660 (concat (system-name) "." wl-local-domain)
665 (defvar wl-load-profile-func 'wl-local-load-profile)
666 (defun wl-local-load-profile ()
667 "Load `wl-init-file'."
668 (message "Initializing ...")
669 (load wl-init-file 'noerror 'nomessage))
671 (defun wl-load-profile ()
672 "Call `wl-load-profile-func' function."
673 (funcall wl-load-profile-func))
677 (defmacro wl-count-lines ()
680 (count-lines 1 (point)))))
682 (defun wl-horizontal-recenter ()
683 "Recenter the current buffer horizontally."
685 (re-search-forward "[[<]" (point-at-eol) t)
686 (if (< (current-column) (/ (window-width) 2))
687 (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
688 (let* ((orig (point))
689 (end (window-end (get-buffer-window (current-buffer) t)))
692 ;; Find the longest line currently displayed in the window.
693 (goto-char (window-start))
694 (while (and (not (eobp))
697 (setq max (max max (current-column)))
700 ;; Scroll horizontally to center (sort of) the point.
701 (if (> max (window-width))
703 (get-buffer-window (current-buffer) t)
704 (min (- (current-column) (/ (window-width) 3))
705 (+ 2 (- max (window-width)))))
706 (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
712 (defvar wl-biff-timer-name "wl-biff")
714 (defun wl-biff-stop ()
715 (when (get-itimer wl-biff-timer-name)
716 (delete-itimer wl-biff-timer-name)))
718 (defun wl-biff-start ()
720 (when wl-biff-check-folder-list
721 (wl-biff-check-folders)
722 (start-itimer wl-biff-timer-name 'wl-biff-check-folders
723 wl-biff-check-interval wl-biff-check-interval))))
725 ((and (condition-case nil (require 'timer) (error nil));; FSFmacs 19+
726 (fboundp 'timer-activate))
728 (defun wl-biff-stop ()
729 (when (get 'wl-biff 'timer)
730 (cancel-timer (get 'wl-biff 'timer))))
732 (defun wl-biff-start ()
734 (when wl-biff-check-folder-list
735 (wl-biff-check-folders)
736 (if (get 'wl-biff 'timer)
737 (timer-activate (get 'wl-biff 'timer))
738 (put 'wl-biff 'timer (run-at-time
739 (timer-next-integral-multiple-of-time
740 (current-time) wl-biff-check-interval)
741 wl-biff-check-interval
742 'wl-biff-event-handler)))))
744 (defun-maybe timer-next-integral-multiple-of-time (time secs)
745 "Yield the next value after TIME that is an integral multiple of SECS.
746 More precisely, the next value, after TIME, that is an integral multiple
747 of SECS seconds since the epoch. SECS may be a fraction.
748 This function is imported from Emacs 20.7."
749 (let ((time-base (ash 1 16)))
751 ;; Use floating point, taking care to not lose precision.
752 (let* ((float-time-base (float time-base))
754 (time-usec (+ (* million
755 (+ (* float-time-base (nth 0 time))
758 (secs-usec (* million secs))
759 (mod-usec (mod time-usec secs-usec))
760 (next-usec (+ (- time-usec mod-usec) secs-usec))
761 (time-base-million (* float-time-base million)))
762 (list (floor next-usec time-base-million)
763 (floor (mod next-usec time-base-million) million)
764 (floor (mod next-usec million))))
765 ;; Floating point is not supported.
766 ;; Use integer arithmetic, avoiding overflow if possible.
767 (let* ((mod-sec (mod (+ (* (mod time-base secs)
768 (mod (nth 0 time) secs))
771 (next-1-sec (+ (- (nth 1 time) mod-sec) secs)))
772 (list (+ (nth 0 time) (floor next-1-sec time-base))
773 (mod next-1-sec time-base)
776 (defun wl-biff-event-handler ()
777 ;; PAKURing from FSF:time.el
778 (wl-biff-check-folders)
779 ;; Do redisplay right now, if no input pending.
781 (let* ((current (current-time))
782 (timer (get 'wl-biff 'timer))
783 ;; Compute the time when this timer will run again, next.
784 (next-time (timer-relative-time
785 (list (aref timer 1) (aref timer 2) (aref timer 3))
786 (* 5 (aref timer 4)) 0)))
787 ;; If the activation time is far in the past,
788 ;; skip executions until we reach a time in the future.
789 ;; This avoids a long pause if Emacs has been suspended for hours.
790 (or (> (nth 0 next-time) (nth 0 current))
791 (and (= (nth 0 next-time) (nth 0 current))
792 (> (nth 1 next-time) (nth 1 current)))
793 (and (= (nth 0 next-time) (nth 0 current))
794 (= (nth 1 next-time) (nth 1 current))
795 (> (nth 2 next-time) (nth 2 current)))
797 (timer-set-time timer (timer-next-integral-multiple-of-time
798 current wl-biff-check-interval)
799 wl-biff-check-interval)
800 (timer-activate timer))))))
802 (fset 'wl-biff-stop 'ignore)
803 (fset 'wl-biff-start 'ignore)))
805 (defsubst wl-biff-notify (new-mails notify-minibuf)
806 (when (and (not wl-modeline-biff-status) (> new-mails 0))
807 (run-hooks 'wl-biff-notify-hook))
808 (when (and wl-modeline-biff-status (eq new-mails 0))
809 (run-hooks 'wl-biff-unnotify-hook))
810 (setq wl-modeline-biff-status (> new-mails 0))
811 (force-mode-line-update t)
813 (cond ((zerop new-mails) (message "No mail."))
814 ((= 1 new-mails) (message "You have a new mail."))
815 (t (message "You have %d new mails." new-mails)))))
817 ;; Internal variable.
818 (defvar wl-biff-check-folders-running nil)
820 (defun wl-biff-check-folders ()
822 (if wl-biff-check-folders-running
823 (when (interactive-p)
824 (message "Biff process is running."))
825 (setq wl-biff-check-folders-running t)
826 (when (interactive-p)
827 (message "Checking new mails..."))
829 (flist (or wl-biff-check-folder-list (list wl-default-folder)))
831 (if (eq (length flist) 1)
832 (wl-biff-check-folder-async (car flist) (interactive-p))
835 (setq folder (car flist)
837 (when (elmo-folder-plugged-p folder)
840 (nth 0 (wl-biff-check-folder folder))))))
841 (setq wl-biff-check-folders-running nil)
842 (wl-biff-notify new-mails (interactive-p)))))))
844 (defun wl-biff-check-folder (folder)
845 (if (eq (elmo-folder-get-type folder) 'pop3)
846 ;; pop3 biff should share the session.
848 (elmo-commit folder) ; Close session.
849 (wl-folder-check-one-entity folder)
850 (elmo-commit folder))
851 (let ((elmo-network-session-name-prefix "BIFF-"))
852 (wl-folder-check-one-entity folder))))
854 (defun wl-biff-check-folder-async-callback (diff data)
856 (with-current-buffer (nth 1 data)
857 (wl-folder-entity-hashtb-set wl-folder-entity-hashtb (nth 0 data)
858 (list (car diff) 0 (cdr diff))
860 (setq wl-folder-info-alist-modified t)
861 (setq wl-biff-check-folders-running nil)
863 (wl-biff-notify (car diff) (nth 2 data)))
865 (defun wl-biff-check-folder-async (folder notify-minibuf)
866 (when (elmo-folder-plugged-p folder)
867 (let ((type (elmo-folder-get-type folder)))
868 (if (and (eq type 'imap4)
869 (wl-folder-use-server-diff-p folder))
870 ;; Check asynchronously only when IMAP4 and use server diff.
872 (setq elmo-folder-diff-async-callback
873 'wl-biff-check-folder-async-callback)
874 (setq elmo-folder-diff-async-callback-data
875 (list folder (get-buffer wl-folder-buffer-name)
877 (let ((elmo-network-session-name-prefix "BIFF-"))
878 (elmo-folder-diff-async folder)))
879 (wl-biff-notify (car (wl-biff-check-folder folder))
881 (setq wl-biff-check-folders-running nil)))))
883 (if (and (fboundp 'regexp-opt)
884 (not (featurep 'xemacs)))
885 (defalias 'wl-regexp-opt 'regexp-opt)
886 (defun wl-regexp-opt (strings &optional paren)
887 "Return a regexp to match a string in STRINGS.
888 Each string should be unique in STRINGS and should not contain any regexps,
889 quoted or not. If optional PAREN is non-nil, ensure that the returned regexp
890 is enclosed by at least one regexp grouping construct."
891 (let ((open-paren (if paren "\\(" "")) (close-paren (if paren "\\)" "")))
892 (concat open-paren (mapconcat 'regexp-quote strings "\\|")
896 (product-provide (provide 'wl-util) (require 'wl-version))
898 ;;; wl-util.el ends here