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 (defun wl-parse-addresses (string)
100 (let (list start s char)
102 (goto-char (point-min))
103 (skip-chars-forward "\t\f\n\r ")
106 (skip-chars-forward "^\"\\,(")
107 (setq char (following-char))
113 (setq s (buffer-substring start (point)))
114 (if (or (null (string-match "^[\t\f\n\r ]+$" s))
115 (not (string= s "")))
116 (setq list (cons s list)))
117 (skip-chars-forward ",\t\f\n\r ")
118 (setq start (point)))
120 (re-search-forward "[^\\]\"" nil 0))
124 (while (and (not (eobp)) (not (zerop parens)))
125 (re-search-forward "[()]" nil 0)
127 (= (char-after (- (point) 2)) ?\\)))
128 ((= (preceding-char) ?\()
129 (setq parens (1+ parens)))
131 (setq parens (1- parens)))))))))
132 (setq s (buffer-substring start (point)))
133 (if (and (null (string-match "^[\t\f\n\r ]+$" s))
134 (not (string= s "")))
135 (setq list (cons s list)))
136 (nreverse list)) ; jwz: fixed order
139 (defun wl-append-element (list element)
141 (append list (list element))
144 (defmacro wl-push (v l)
145 "Insert V at the head of the list stored in L."
146 (list 'setq l (list 'cons v l)))
149 "Remove the head of the list stored in L."
150 (list 'car (list 'prog1 l (list 'setq l (list 'cdr l)))))
152 (defun wl-ask-folder (func mes-string)
154 (cmd (if (featurep 'xemacs)
155 (event-to-character last-command-event)
156 (string-to-char (format "%s" (this-command-keys))))))
158 (setq key (car (setq keve (wl-read-event-char))))
159 (if (or (equal key ?\ )
165 (wl-push (cdr keve) unread-command-events))))
167 ;(defalias 'wl-make-hash 'elmo-make-hash)
168 ;;(make-obsolete 'wl-make-hash 'elmo-make-hash)
170 ;;(defalias 'wl-get-hash-val 'elmo-get-hash-val)
171 ;;(make-obsolete 'wl-get-hash-val 'elmo-get-hash-val)
173 ;;(defalias 'wl-set-hash-val 'elmo-set-hash-val)
174 ;;(make-obsolete 'wl-set-hash-val 'elmo-set-hash-val)
176 (defsubst wl-set-string-width (width string)
178 (elmo-set-buffer-multibyte default-enable-multibyte-characters)
180 (if (> (current-column) width)
181 (if (> (move-to-column width) width)
183 (condition-case nil ; ignore error
186 (concat (buffer-substring (point-min) (point)) " "))
187 (buffer-substring (point-min) (point)))
188 (if (= (current-column) width)
191 (format (format "%%%ds"
192 (- width (current-column)))
195 (defun wl-display-bytes (num)
198 ((> (setq result (/ num 1000000)) 0)
199 (setq remain (% num 1000000))
200 (if (> remain 400000)
201 (setq result (+ 1 result)))
202 (format "%dM" result))
203 ((> (setq result (/ num 1000)) 0)
204 (setq remain (% num 1000))
206 (setq result (+ 1 result)))
207 (format "%dK" result))
208 (t (format "%dB" result)))))
210 (defun wl-mode-line-buffer-identification (&optional id)
211 (let ((priorities '(biff plug title)))
212 (let ((items (reverse wl-mode-line-display-priority-list))
215 (setq item (car items)
217 (unless (memq item '(biff plug))
219 (setq priorities (cons item (delq item priorities)))))
220 (let (priority result)
222 (setq priority (car priorities)
223 priorities (cdr priorities))
226 (when wl-biff-check-folder-list
227 (setq result (append result '((wl-modeline-biff-status
228 wl-modeline-biff-state-on
229 wl-modeline-biff-state-off))))))
231 (when wl-show-plug-status-on-modeline
232 (setq result (append result '((wl-modeline-plug-status
233 wl-modeline-plug-state-on
234 wl-modeline-plug-state-off))))))
236 (setq result (append result (or id '("Wanderlust: %12b")))))))
238 (setq mode-line-buffer-identification (if (stringp (car result))
241 (force-mode-line-update t)))))
243 (defalias 'wl-display-error 'elmo-display-error)
244 (make-obsolete 'wl-display-error 'elmo-display-error)
246 (defun wl-get-assoc-list-value (assoc-list folder &optional match)
248 (let ((alist assoc-list)
251 (setq pair (car alist))
252 (if (string-match (car pair) folder)
253 (cond ((eq match 'all)
254 (setq value (append value (list (cdr pair)))))
255 ((eq match 'all-list)
256 (setq value (append value (cdr pair))))
258 (throw 'found (cdr pair)))))
259 (setq alist (cdr alist)))
262 (defmacro wl-match-string (pos string)
263 "Substring POSth matched STRING."
264 (` (substring (, string) (match-beginning (, pos)) (match-end (, pos)))))
266 (defmacro wl-match-buffer (pos)
267 "Substring POSth matched from the current buffer."
268 (` (buffer-substring-no-properties
269 (match-beginning (, pos)) (match-end (, pos)))))
271 (put 'wl-as-coding-system 'lisp-indent-function 1)
272 (put 'wl-as-mime-charset 'lisp-indent-function 1)
276 (defmacro wl-as-coding-system (coding-system &rest body)
277 (` (let ((coding-system-for-read (, coding-system))
278 (coding-system-for-write (, coding-system)))
281 (defmacro wl-as-coding-system (coding-system &rest body)
282 (` (let ((file-coding-system-for-read (, coding-system))
283 (file-coding-system (, coding-system)))
286 (defmacro wl-as-mime-charset (mime-charset &rest body)
287 (` (wl-as-coding-system (mime-charset-to-coding-system (, mime-charset))
290 (defalias 'wl-string 'elmo-string)
291 (make-obsolete 'wl-string 'elmo-string)
293 ;; Check if active region exists or not.
294 (if (boundp 'mark-active)
295 (defmacro wl-region-exists-p ()
297 (if (fboundp 'region-exists-p)
298 (defmacro wl-region-exists-p ()
299 (list 'region-exists-p))))
301 (if (not (fboundp 'overlays-in))
302 (defun overlays-in (beg end)
303 "Return a list of the overlays that overlap the region BEG ... END.
304 Overlap means that at least one character is contained within the overlay
305 and also contained within the specified region.
306 Empty overlays are included in the result if they are located at BEG
307 or between BEG and END."
308 (let ((ovls (overlay-lists))
314 (setq ovls (nconc (car ovls) (cdr ovls)))
318 (if (or (and (<= (overlay-start tmp) end)
319 (>= (overlay-start tmp) beg))
320 (and (<= (overlay-end tmp) end)
321 (>= (overlay-end tmp) beg)))
322 (setq retval (cons tmp retval))))
325 (defsubst wl-repeat-string (str times)
329 (setq ret-val (concat ret-val str))
330 (setq loop (- loop 1)))
333 (defun wl-list-diff (list1 list2)
334 "Return a list of elements of LIST1 that do not appear in LIST2."
335 (let ((list1 (copy-sequence list1)))
337 (setq list1 (delq (car list2) list1))
338 (setq list2 (cdr list2)))
341 (defun wl-append-assoc-list (item value alist)
342 "make assoc list '((item1 value1-1 value1-2 ...)) (item2 value2-1 ...)))"
343 (let ((entry (assoc item alist)))
346 (when (not (member value (cdr entry)))
347 (nconc entry (list value)))
350 (list (list item value))))))
352 (defun wl-delete-alist (key alist)
353 "Delete by side effect any entries specified with KEY from ALIST.
354 Return the modified ALIST. Key comparison is done with `assq'.
355 Write `(setq foo (wl-delete-alist key foo))' to be sure of changing
358 (while (setq entry (assq key alist))
359 (setq alist (delq entry alist)))
362 (defun wl-delete-associations (keys alist)
363 "Delete by side effect any entries specified with KEYS from ALIST.
364 Return the modified ALIST. KEYS must be a list of keys for ALIST.
365 Deletion is done with `wl-delete-alist'.
366 Write `(setq foo (wl-delete-associations keys foo))' to be sure of
367 changing the value of `foo'."
369 (setq alist (wl-delete-alist (car keys) alist))
370 (setq keys (cdr keys)))
373 (defun wl-inverse-alist (keys alist)
374 "Inverse ALIST, copying.
375 Return an association list represents the inverse mapping of ALIST,
376 from objects to KEYS.
377 The objects mapped (cdrs of elements of the ALIST) are shared."
378 (let (x y tmp result)
381 (setq y (cdr (assq x alist)))
383 (if (setq tmp (assoc y result))
384 (setq result (cons (append tmp (list x))
385 (delete tmp result)))
386 (setq result (cons (list y x) result))))
387 (setq keys (cdr keys)))
392 (static-unless (fboundp 'pp)
393 (defvar pp-escape-newlines t)
394 (defun pp (object &optional stream)
395 "Output the pretty-printed representation of OBJECT, any Lisp object.
396 Quoting characters are printed when needed to make output that `read'
397 can handle, whenever this is possible.
398 Output stream is STREAM, or value of `standard-output' (which see)."
399 (princ (pp-to-string object) (or stream standard-output)))
401 (defun pp-to-string (object)
402 "Return a string containing the pretty-printed representation of OBJECT,
403 any Lisp object. Quoting characters are used when needed to make output
404 that `read' can handle, whenever this is possible."
406 (set-buffer (generate-new-buffer " pp-to-string"))
409 (lisp-mode-variables t)
410 (let ((print-escape-newlines pp-escape-newlines))
411 (prin1 object (current-buffer)))
412 (goto-char (point-min))
415 ((looking-at "\\s(\\|#\\s(")
416 (while (looking-at "\\s(\\|#\\s(")
418 ((and (looking-at "\\(quote[ \t]+\\)\\([^.)]\\)")
419 (> (match-beginning 1) 1)
420 (= ?\( (char-after (1- (match-beginning 1))))
421 ;; Make sure this is a two-element list.
423 (goto-char (match-beginning 2))
425 ;; Avoid mucking with match-data; does this test work?
426 (char-equal ?\) (char-after (point)))))
427 ;; -1 gets the paren preceding the quote as well.
428 (delete-region (1- (match-beginning 1)) (match-end 1))
431 (if (looking-at "[ \t]*\)")
432 (delete-region (match-beginning 0) (match-end 0))
433 (error "Malformed quote"))
435 ((condition-case err-var
436 (prog1 t (down-list 1))
439 (skip-chars-backward " \t")
442 (progn (skip-chars-forward " \t") (point)))
443 (if (not (char-equal ?' (char-after (1- (point)))))
445 ((condition-case err-var
446 (prog1 t (up-list 1))
448 (while (looking-at "\\s)")
450 (skip-chars-backward " \t")
453 (progn (skip-chars-forward " \t") (point)))
454 (if (not (char-equal ?' (char-after (1- (point)))))
456 (t (goto-char (point-max)))))
457 (goto-char (point-min))
460 (kill-buffer (current-buffer))))))
462 (defsubst wl-get-date-iso8601 (date)
463 (or (get-text-property 0 'wl-date date)
464 (let* ((d1 (timezone-fix-time date nil nil))
465 (time (format "%04d%02d%02dT%02d%02d%02d"
466 (aref d1 0) (aref d1 1) (aref d1 2)
467 (aref d1 3) (aref d1 4) (aref d1 5))))
468 (put-text-property 0 1 'wl-date time date)
471 (defun wl-make-date-string ()
472 (let ((s (current-time-string)))
473 (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]"
475 (concat (wl-match-string 1 s) ", "
476 (timezone-make-date-arpa-standard s (current-time-zone)))))
478 (defun wl-date-iso8601 (date)
479 "Convert the DATE to YYMMDDTHHMMSS."
481 (wl-get-date-iso8601 date)
484 (defun wl-day-number (date)
485 (let ((dat (mapcar '(lambda (s) (and s (string-to-int s)) )
486 (timezone-parse-date date))))
487 (timezone-absolute-from-gregorian
488 (nth 1 dat) (nth 2 dat) (car dat))))
490 (defun wl-url-news (url &rest args)
491 (interactive "sURL: ")
492 (if (string-match "^news:\\(.*\\)$" url)
493 (wl-summary-goto-folder-subr
494 (concat "-" (elmo-match-string 1 url)) nil nil nil t)
495 (message "Not a news: url.")))
497 (defun wl-url-nntp (url &rest args)
498 (interactive "sURL: ")
499 (let (folder fld-name server port msg)
501 "^nntp://\\([^:/]*\\):?\\([0-9]*\\)/\\([^/]*\\)/\\([0-9]*\\).*$" url)
503 (if (eq (length (setq fld-name
504 (elmo-match-string 3 url))) 0)
506 (if (eq (length (setq port
507 (elmo-match-string 2 url))) 0)
508 (setq port (int-to-string elmo-nntp-default-port)))
509 (if (eq (length (setq server
510 (elmo-match-string 1 url))) 0)
511 (setq server elmo-nntp-default-server))
512 (setq folder (concat "-" fld-name "@" server ":" port))
513 (if (eq (length (setq msg
514 (elmo-match-string 4 url))) 0)
515 (wl-summary-goto-folder-subr
516 folder nil nil nil t)
517 (wl-summary-goto-folder-subr
518 folder 'update nil nil t)
519 (goto-char (point-min))
520 (re-search-forward (concat "^ *" msg) nil t)
521 (wl-summary-redisplay)))
522 (message "Not a nntp: url."))))
524 (defmacro wl-concat-list (list separator)
525 (` (mapconcat 'identity (delete "" (delq nil (, list))) (, separator))))
527 (defmacro wl-current-message-buffer ()
529 (if (buffer-live-p wl-current-summary-buffer)
530 (set-buffer wl-current-summary-buffer))
533 (defmacro wl-kill-buffers (regexp)
536 (if (and (buffer-name x)
537 (string-match (, regexp) (buffer-name x)))
542 (defun wl-sendlog-time ()
543 (static-if (fboundp 'format-time-string)
544 (format-time-string "%Y/%m/%d %T")
545 (let ((date (current-time-string)))
546 (format "%s/%02d/%02d %s"
548 (cdr (assoc (upcase (substring date 4 7))
549 timezone-months-assoc))
550 (string-to-int (substring date 8 10))
551 (substring date 11 19)))))
553 (defun wl-collect-summary ()
556 (function (lambda (x)
557 (if (and (string-match "^Summary"
561 (equal major-mode 'wl-summary-mode)))
562 (setq result (nconc result (list x))))))
566 (static-if (fboundp 'read-directory-name)
567 (defalias 'wl-read-directory-name 'read-directory-name)
568 (defun wl-read-directory-name (prompt dir)
569 (let ((dir (read-file-name prompt dir)))
570 (unless (file-directory-p dir)
571 (error "%s is not directory" dir))
574 ;; local variable check.
575 (static-if (fboundp 'local-variable-p)
576 (defalias 'wl-local-variable-p 'local-variable-p)
577 (defmacro wl-local-variable-p (symbol &optional buffer)
578 (` (if (assq (, symbol) (buffer-local-variables (, buffer)))
581 (defun wl-number-base36 (num len)
586 (concat (wl-number-base36 (/ num 36) (1- len))
587 (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
590 (defvar wl-unique-id-char nil)
592 (defun wl-unique-id ()
593 ;; Don't use microseconds from (current-time), they may be unsupported.
594 ;; Instead we use this randomly inited counter.
595 (setq wl-unique-id-char
596 (% (1+ (or wl-unique-id-char (logand (random t) (1- (lsh 1 20)))))
597 ;; (current-time) returns 16-bit ints,
598 ;; and 2^16*25 just fits into 4 digits i base 36.
600 (let ((tm (static-if (fboundp 'current-time)
602 (let* ((cts (split-string (current-time-string) "[ :]"))
603 (m (cdr (assoc (nth 1 cts)
604 '(("Jan" . "01") ("Feb" . "02")
605 ("Mar" . "03") ("Apr" . "04")
606 ("May" . "05") ("Jun" . "06")
607 ("Jul" . "07") ("Aug" . "08")
608 ("Sep" . "09") ("Oct" . "10")
609 ("Nov" . "11") ("Dec" . "12"))))))
610 (list (string-to-int (concat (nth 6 cts) m
611 (substring (nth 2 cts) 0 1)))
612 (string-to-int (concat (substring (nth 2 cts) 1)
613 (nth 4 cts) (nth 5 cts)
616 (if (memq system-type '(ms-dos emx vax-vms))
617 (let ((user (downcase (user-login-name))))
618 (while (string-match "[^a-z0-9_]" user)
619 (aset user (match-beginning 0) ?_))
621 (wl-number-base36 (user-uid) -1))
622 (wl-number-base36 (+ (car tm)
623 (lsh (% wl-unique-id-char 25) 16)) 4)
624 (wl-number-base36 (+ (nth 1 tm)
625 (lsh (/ wl-unique-id-char 25) 16)) 4)
626 ;; Append the name of the message interface, because while the
627 ;; generated ID is unique to this newsreader, other newsreaders
628 ;; might otherwise generate the same ID via another algorithm.
631 (defun wl-draft-make-message-id-string ()
632 "Return Message-ID field value."
633 (concat "<" (wl-unique-id) "@"
634 (or wl-message-id-domain
636 (concat (system-name) "." wl-local-domain)
641 (defvar wl-load-profile-function 'wl-local-load-profile)
642 (defun wl-local-load-profile ()
643 "Load `wl-init-file'."
644 (message "Initializing ...")
645 (load wl-init-file 'noerror 'nomessage))
647 (defun wl-load-profile ()
648 "Call `wl-load-profile-function' function."
649 (funcall wl-load-profile-function))
653 (defmacro wl-count-lines ()
656 (count-lines 1 (point)))))
658 (defun wl-horizontal-recenter ()
659 "Recenter the current buffer horizontally."
661 (re-search-forward "[[<]" (point-at-eol) t)
662 (if (< (current-column) (/ (window-width) 2))
663 (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
664 (let* ((orig (point))
665 (end (window-end (get-buffer-window (current-buffer) t)))
668 ;; Find the longest line currently displayed in the window.
669 (goto-char (window-start))
670 (while (and (not (eobp))
673 (setq max (max max (current-column)))
676 ;; Scroll horizontally to center (sort of) the point.
677 (if (> max (window-width))
679 (get-buffer-window (current-buffer) t)
680 (min (- (current-column) (/ (window-width) 3))
681 (+ 2 (- max (window-width)))))
682 (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
688 (defvar wl-biff-timer-name "wl-biff")
690 (defun wl-biff-stop ()
691 (when (get-itimer wl-biff-timer-name)
692 (delete-itimer wl-biff-timer-name)))
694 (defun wl-biff-start ()
696 (when wl-biff-check-folder-list
697 (wl-biff-check-folders)
698 (start-itimer wl-biff-timer-name 'wl-biff-check-folders
699 wl-biff-check-interval wl-biff-check-interval))))
701 ((and (condition-case nil (require 'timer) (error nil));; FSFmacs 19+
702 (fboundp 'timer-activate))
704 (defun wl-biff-stop ()
705 (when (get 'wl-biff 'timer)
706 (cancel-timer (get 'wl-biff 'timer))))
708 (defun wl-biff-start ()
710 (when wl-biff-check-folder-list
711 (wl-biff-check-folders)
712 (if (get 'wl-biff 'timer)
713 (timer-activate (get 'wl-biff 'timer))
714 (put 'wl-biff 'timer (run-at-time
715 (timer-next-integral-multiple-of-time
716 (current-time) wl-biff-check-interval)
717 wl-biff-check-interval
718 'wl-biff-event-handler)))))
720 (defun-maybe timer-next-integral-multiple-of-time (time secs)
721 "Yield the next value after TIME that is an integral multiple of SECS.
722 More precisely, the next value, after TIME, that is an integral multiple
723 of SECS seconds since the epoch. SECS may be a fraction.
724 This function is imported from Emacs 20.7."
725 (let ((time-base (ash 1 16)))
727 ;; Use floating point, taking care to not lose precision.
728 (let* ((float-time-base (float time-base))
730 (time-usec (+ (* million
731 (+ (* float-time-base (nth 0 time))
734 (secs-usec (* million secs))
735 (mod-usec (mod time-usec secs-usec))
736 (next-usec (+ (- time-usec mod-usec) secs-usec))
737 (time-base-million (* float-time-base million)))
738 (list (floor next-usec time-base-million)
739 (floor (mod next-usec time-base-million) million)
740 (floor (mod next-usec million))))
741 ;; Floating point is not supported.
742 ;; Use integer arithmetic, avoiding overflow if possible.
743 (let* ((mod-sec (mod (+ (* (mod time-base secs)
744 (mod (nth 0 time) secs))
747 (next-1-sec (+ (- (nth 1 time) mod-sec) secs)))
748 (list (+ (nth 0 time) (floor next-1-sec time-base))
749 (mod next-1-sec time-base)
752 (defun wl-biff-event-handler ()
753 ;; PAKURing from FSF:time.el
754 (wl-biff-check-folders)
755 ;; Do redisplay right now, if no input pending.
757 (let* ((current (current-time))
758 (timer (get 'wl-biff 'timer))
759 ;; Compute the time when this timer will run again, next.
760 (next-time (timer-relative-time
761 (list (aref timer 1) (aref timer 2) (aref timer 3))
762 (* 5 (aref timer 4)) 0)))
763 ;; If the activation time is far in the past,
764 ;; skip executions until we reach a time in the future.
765 ;; This avoids a long pause if Emacs has been suspended for hours.
766 (or (> (nth 0 next-time) (nth 0 current))
767 (and (= (nth 0 next-time) (nth 0 current))
768 (> (nth 1 next-time) (nth 1 current)))
769 (and (= (nth 0 next-time) (nth 0 current))
770 (= (nth 1 next-time) (nth 1 current))
771 (> (nth 2 next-time) (nth 2 current)))
773 (timer-set-time timer (timer-next-integral-multiple-of-time
774 current wl-biff-check-interval)
775 wl-biff-check-interval)
776 (timer-activate timer))))))
778 (fset 'wl-biff-stop 'ignore)
779 (fset 'wl-biff-start 'ignore)))
781 (defsubst wl-biff-notify (new-mails notify-minibuf)
782 (when (and (not wl-modeline-biff-status) (> new-mails 0))
783 (run-hooks 'wl-biff-notify-hook))
784 (when (and wl-modeline-biff-status (eq new-mails 0))
785 (run-hooks 'wl-biff-unnotify-hook))
786 (setq wl-modeline-biff-status (> new-mails 0))
787 (force-mode-line-update t)
789 (cond ((zerop new-mails) (message "No mail."))
790 ((= 1 new-mails) (message "You have a new mail."))
791 (t (message "You have %d new mails." new-mails)))))
793 ;; Internal variable.
794 (defvar wl-biff-check-folders-running nil)
796 (defun wl-biff-check-folders ()
798 (if wl-biff-check-folders-running
799 (when (interactive-p)
800 (message "Biff process is running."))
801 (setq wl-biff-check-folders-running t)
802 (when (interactive-p)
803 (message "Checking new mails..."))
805 (flist (or wl-biff-check-folder-list (list wl-default-folder)))
807 (if (eq (length flist) 1)
808 (wl-biff-check-folder-async (wl-folder-get-elmo-folder
809 (car flist)) (interactive-p))
812 (setq folder (wl-folder-get-elmo-folder (car flist))
814 (when (elmo-folder-plugged-p folder)
817 (nth 0 (wl-biff-check-folder folder))))))
818 (setq wl-biff-check-folders-running nil)
819 (wl-biff-notify new-mails (interactive-p)))))))
821 (defun wl-biff-check-folder (folder)
822 (if (eq (elmo-folder-type-internal folder) 'pop3)
823 (unless (elmo-pop3-get-session folder 'if-exists)
824 ;; Currently no main pop3 process.
825 (let ((elmo-network-session-name-prefix "BIFF-"))
826 (wl-folder-check-one-entity
827 (elmo-folder-name-internal folder))))
828 (let ((elmo-network-session-name-prefix "BIFF-"))
829 (wl-folder-check-one-entity (elmo-folder-name-internal folder)))))
831 (defun wl-biff-check-folder-async-callback (diff data)
833 (with-current-buffer (nth 1 data)
834 (wl-folder-entity-hashtb-set wl-folder-entity-hashtb
836 (list (car diff) 0 (cdr diff))
838 (setq wl-folder-info-alist-modified t)
839 (setq wl-biff-check-folders-running nil)
841 (wl-biff-notify (car diff) (nth 2 data)))
843 (defun wl-biff-check-folder-async (folder notify-minibuf)
844 (when (elmo-folder-plugged-p folder)
845 (if (and (eq (elmo-folder-type-internal folder) 'imap4)
846 (elmo-folder-use-flag-p folder))
847 ;; Check asynchronously only when IMAP4 and use server diff.
849 (setq elmo-folder-diff-async-callback
850 'wl-biff-check-folder-async-callback)
851 (setq elmo-folder-diff-async-callback-data
852 (list (elmo-folder-name-internal folder)
853 (get-buffer wl-folder-buffer-name)
855 (let ((elmo-network-session-name-prefix "BIFF-"))
856 (elmo-folder-diff-async folder)))
858 (wl-biff-notify (car (wl-biff-check-folder folder))
860 (setq wl-biff-check-folders-running nil)))))
862 (if (and (fboundp 'regexp-opt)
863 (not (featurep 'xemacs)))
864 (defalias 'wl-regexp-opt 'regexp-opt)
865 (defun wl-regexp-opt (strings &optional paren)
866 "Return a regexp to match a string in STRINGS.
867 Each string should be unique in STRINGS and should not contain any regexps,
868 quoted or not. If optional PAREN is non-nil, ensure that the returned regexp
869 is enclosed by at least one regexp grouping construct."
870 (let ((open-paren (if paren "\\(" "")) (close-paren (if paren "\\)" "")))
871 (concat open-paren (mapconcat 'regexp-quote strings "\\|")
874 (defun wl-expand-newtext (newtext original)
875 (let ((len (length newtext))
877 c expanded beg N did-expand)
880 (while (and (< pos len)
881 (not (= (aref newtext pos) ?\\)))
884 (push (substring newtext beg pos) expanded))
886 ;; We hit a \; expand it.
889 c (aref newtext pos))
890 (if (not (or (= c ?\&)
893 ;; \ followed by some character we don't expand.
894 (push (char-to-string c) expanded)
899 (when (match-beginning N)
900 (push (substring original (match-beginning N) (match-end N))
904 (apply (function concat) (nreverse expanded))
908 (product-provide (provide 'wl-util) (require 'wl-version))
910 ;;; wl-util.el ends here