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-inverse-alist (keys alist)
340 "Inverse ALIST, copying.
341 Return an association list represents the inverse mapping of ALIST,
342 from objects to KEYS.
343 The objects mapped (cdrs of elements of the ALIST) are shared."
344 (let (x y tmp result)
347 (setq y (cdr (assq x alist)))
349 (if (setq tmp (assoc y result))
350 (setq result (cons (append tmp (list x))
351 (delete tmp result)))
352 (setq result (cons (list y x) result))))
353 (setq keys (cdr keys)))
358 (static-unless (fboundp 'pp)
359 (defvar pp-escape-newlines t)
360 (defun pp (object &optional stream)
361 "Output the pretty-printed representation of OBJECT, any Lisp object.
362 Quoting characters are printed when needed to make output that `read'
363 can handle, whenever this is possible.
364 Output stream is STREAM, or value of `standard-output' (which see)."
365 (princ (pp-to-string object) (or stream standard-output)))
367 (defun pp-to-string (object)
368 "Return a string containing the pretty-printed representation of OBJECT,
369 any Lisp object. Quoting characters are used when needed to make output
370 that `read' can handle, whenever this is possible."
372 (set-buffer (generate-new-buffer " pp-to-string"))
375 (lisp-mode-variables t)
376 (let ((print-escape-newlines pp-escape-newlines))
377 (prin1 object (current-buffer)))
378 (goto-char (point-min))
381 ((looking-at "\\s(\\|#\\s(")
382 (while (looking-at "\\s(\\|#\\s(")
384 ((and (looking-at "\\(quote[ \t]+\\)\\([^.)]\\)")
385 (> (match-beginning 1) 1)
386 (= ?\( (char-after (1- (match-beginning 1))))
387 ;; Make sure this is a two-element list.
389 (goto-char (match-beginning 2))
391 ;; Avoid mucking with match-data; does this test work?
392 (char-equal ?\) (char-after (point)))))
393 ;; -1 gets the paren preceding the quote as well.
394 (delete-region (1- (match-beginning 1)) (match-end 1))
397 (if (looking-at "[ \t]*\)")
398 (delete-region (match-beginning 0) (match-end 0))
399 (error "Malformed quote"))
401 ((condition-case err-var
402 (prog1 t (down-list 1))
405 (skip-chars-backward " \t")
408 (progn (skip-chars-forward " \t") (point)))
409 (if (not (char-equal ?' (char-after (1- (point)))))
411 ((condition-case err-var
412 (prog1 t (up-list 1))
414 (while (looking-at "\\s)")
416 (skip-chars-backward " \t")
419 (progn (skip-chars-forward " \t") (point)))
420 (if (not (char-equal ?' (char-after (1- (point)))))
422 (t (goto-char (point-max)))))
423 (goto-char (point-min))
426 (kill-buffer (current-buffer))))))
428 (defsubst wl-get-date-iso8601 (date)
429 (or (get-text-property 0 'wl-date date)
430 (let* ((d1 (timezone-fix-time date nil nil))
431 (time (format "%04d%02d%02dT%02d%02d%02d"
432 (aref d1 0) (aref d1 1) (aref d1 2)
433 (aref d1 3) (aref d1 4) (aref d1 5))))
434 (put-text-property 0 1 'wl-date time date)
437 (defun wl-make-date-string ()
438 (let ((s (current-time-string)))
439 (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]"
441 (concat (wl-match-string 1 s) ", "
442 (timezone-make-date-arpa-standard s (current-time-zone)))))
444 (defun wl-date-iso8601 (date)
445 "Convert the DATE to YYMMDDTHHMMSS."
447 (wl-get-date-iso8601 date)
450 (defun wl-url-news (url &rest args)
451 (interactive "sURL: ")
452 (if (string-match "^news:\\(.*\\)$" url)
453 (wl-summary-goto-folder-subr
454 (concat "-" (elmo-match-string 1 url)) nil nil nil t)
455 (message "Not a news: url.")))
457 (defun wl-url-nntp (url &rest args)
458 (interactive "sURL: ")
459 (let (folder fld-name server port msg)
461 "^nntp://\\([^:/]*\\):?\\([0-9]*\\)/\\([^/]*\\)/\\([0-9]*\\).*$" url)
463 (if (eq (length (setq fld-name
464 (elmo-match-string 3 url))) 0)
466 (if (eq (length (setq port
467 (elmo-match-string 2 url))) 0)
468 (setq port (int-to-string elmo-nntp-default-port)))
469 (if (eq (length (setq server
470 (elmo-match-string 1 url))) 0)
471 (setq server elmo-nntp-default-server))
472 (setq folder (concat "-" fld-name "@" server ":" port))
473 (if (eq (length (setq msg
474 (elmo-match-string 4 url))) 0)
475 (wl-summary-goto-folder-subr
476 folder nil nil nil t)
477 (wl-summary-goto-folder-subr
478 folder 'update nil nil t)
479 (wl-summary-jump-to-msg (string-to-number msg))
480 (wl-summary-redisplay)))
481 (message "Not a nntp: url."))))
483 (defmacro wl-concat-list (list separator)
484 (` (mapconcat 'identity (delete "" (delq nil (, list))) (, separator))))
486 (defun wl-current-message-buffer ()
487 (when (buffer-live-p wl-current-summary-buffer)
488 (with-current-buffer wl-current-summary-buffer
489 (or wl-message-buffer
490 (and (wl-summary-message-number)
491 (wl-message-buffer-display
492 wl-summary-buffer-elmo-folder
493 (wl-summary-message-number)
494 wl-summary-buffer-display-mime-mode
497 (defmacro wl-kill-buffers (regexp)
500 (if (and (buffer-name x)
501 (string-match (, regexp) (buffer-name x)))
506 (defun wl-collect-summary ()
509 (function (lambda (x)
510 (if (and (string-match "^Summary"
514 (equal major-mode 'wl-summary-mode)))
515 (setq result (nconc result (list x))))))
519 (defun wl-collect-draft ()
520 (let ((draft-regexp (concat "^" (regexp-quote wl-draft-folder)))
522 (dolist (buffer (buffer-list))
523 (when (with-current-buffer buffer
524 (and (eq major-mode 'wl-draft-mode)
526 (string-match draft-regexp (buffer-name))))
527 (setq result (cons buffer result))))
530 (defvar wl-inhibit-save-drafts nil)
531 (defvar wl-disable-auto-save nil)
532 (make-variable-buffer-local 'wl-disable-auto-save)
534 (defun wl-save-drafts ()
535 "Save all drafts. Return nil if there is no draft buffer."
536 (if wl-inhibit-save-drafts
538 (let ((wl-inhibit-save-drafts t)
539 (msg (current-message))
540 (buffers (wl-collect-draft)))
542 (dolist (buffer buffers)
544 (when (and (not wl-disable-auto-save)
547 (message "%s" (or msg ""))
550 (static-if (fboundp 'read-directory-name)
551 (defun wl-read-directory-name (prompt dir)
552 (read-directory-name prompt dir dir))
553 (defun wl-read-directory-name (prompt dir)
554 (let ((dir (read-file-name prompt dir)))
555 (unless (file-directory-p dir)
556 (error "%s is not directory" dir))
559 ;; local variable check.
560 (static-if (fboundp 'local-variable-p)
561 (defalias 'wl-local-variable-p 'local-variable-p)
562 (defmacro wl-local-variable-p (symbol &optional buffer)
563 (` (if (assq (, symbol) (buffer-local-variables (, buffer)))
566 (defun wl-number-base36 (num len)
571 (concat (wl-number-base36 (/ num 36) (1- len))
572 (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
575 (defvar wl-unique-id-char nil)
577 (defun wl-unique-id ()
578 ;; Don't use microseconds from (current-time), they may be unsupported.
579 ;; Instead we use this randomly inited counter.
580 (setq wl-unique-id-char
581 (% (1+ (or wl-unique-id-char (logand (random t) (1- (lsh 1 20)))))
582 ;; (current-time) returns 16-bit ints,
583 ;; and 2^16*25 just fits into 4 digits i base 36.
585 (let ((tm (static-if (fboundp 'current-time)
587 (let* ((cts (split-string (current-time-string) "[ :]"))
588 (m (cdr (assoc (nth 1 cts)
589 '(("Jan" . "01") ("Feb" . "02")
590 ("Mar" . "03") ("Apr" . "04")
591 ("May" . "05") ("Jun" . "06")
592 ("Jul" . "07") ("Aug" . "08")
593 ("Sep" . "09") ("Oct" . "10")
594 ("Nov" . "11") ("Dec" . "12"))))))
595 (list (string-to-int (concat (nth 6 cts) m
596 (substring (nth 2 cts) 0 1)))
597 (string-to-int (concat (substring (nth 2 cts) 1)
598 (nth 4 cts) (nth 5 cts)
601 (if (memq system-type '(ms-dos emx vax-vms))
602 (let ((user (downcase (user-login-name))))
603 (while (string-match "[^a-z0-9_]" user)
604 (aset user (match-beginning 0) ?_))
606 (wl-number-base36 (user-uid) -1))
607 (wl-number-base36 (+ (car tm)
608 (lsh (% wl-unique-id-char 25) 16)) 4)
609 (wl-number-base36 (+ (nth 1 tm)
610 (lsh (/ wl-unique-id-char 25) 16)) 4)
611 ;; Append the name of the message interface, because while the
612 ;; generated ID is unique to this newsreader, other newsreaders
613 ;; might otherwise generate the same ID via another algorithm.
614 wl-unique-id-suffix)))
616 (defvar wl-message-id-function 'wl-draft-make-message-id-string)
617 (defun wl-draft-make-message-id-string ()
618 "Return Message-ID field value."
619 (concat "<" (wl-unique-id)
620 (let (from user domain)
621 (if (and wl-message-id-use-wl-from
623 (setq from (wl-address-header-extract-address wl-from))
624 (and (string-match "^\\(.*\\)@\\(.*\\)$" from)
625 (setq user (match-string 1 from))
626 (setq domain (match-string 2 from)))))
627 (format "%%%s@%s>" user domain)
629 (or wl-message-id-domain
631 (concat (system-name) "." wl-local-domain)
635 (defvar wl-load-profile-function 'wl-local-load-profile)
636 (defun wl-local-load-profile ()
637 "Load `wl-init-file'."
638 (message "Initializing...")
639 (load wl-init-file 'noerror 'nomessage))
641 (defun wl-load-profile ()
642 "Call `wl-load-profile-function' function."
643 (funcall wl-load-profile-function))
647 (defmacro wl-count-lines ()
650 (count-lines 1 (point)))))
652 (defun wl-horizontal-recenter ()
653 "Recenter the current buffer horizontally."
655 (re-search-forward "[[<]" (point-at-eol) t)
656 (if (< (current-column) (/ (window-width) 2))
657 (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
658 (let* ((orig (point))
659 (end (window-end (get-buffer-window (current-buffer) t)))
662 ;; Find the longest line currently displayed in the window.
663 (goto-char (window-start))
664 (while (and (not (eobp))
667 (setq max (max max (current-column)))
670 ;; Scroll horizontally to center (sort of) the point.
671 (if (> max (window-width))
673 (get-buffer-window (current-buffer) t)
674 (min (- (current-column) (/ (window-width) 3))
675 (+ 2 (- max (window-width)))))
676 (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
680 (defun wl-auto-save-drafts ()
681 (unless (wl-save-drafts)
682 (wl-stop-save-drafts)))
686 (defvar wl-save-drafts-timer-name "wl-save-drafts")
688 (defun wl-start-save-drafts ()
689 (when (numberp wl-auto-save-drafts-interval)
690 (unless (get-itimer wl-save-drafts-timer-name)
691 (start-itimer wl-save-drafts-timer-name
693 wl-auto-save-drafts-interval
694 wl-auto-save-drafts-interval
697 (defun wl-stop-save-drafts ()
698 (when (get-itimer wl-save-drafts-timer-name)
699 (delete-itimer wl-save-drafts-timer-name))))
701 (defun wl-start-save-drafts ()
702 (when (numberp wl-auto-save-drafts-interval)
704 (if (get 'wl-save-drafts 'timer)
706 (timer-set-idle-time (get 'wl-save-drafts 'timer)
707 wl-auto-save-drafts-interval t)
708 (timer-activate-when-idle (get 'wl-save-drafts 'timer)))
709 (put 'wl-save-drafts 'timer
711 wl-auto-save-drafts-interval t 'wl-auto-save-drafts)))))
713 (defun wl-stop-save-drafts ()
714 (when (get 'wl-save-drafts 'timer)
715 (cancel-timer (get 'wl-save-drafts 'timer))))))
717 (defun wl-set-auto-save-draft (&optional arg)
719 (unless (setq wl-disable-auto-save
721 ((null arg) (not wl-disable-auto-save))
722 ((< (prefix-numeric-value arg) 0) t)
724 (wl-start-save-drafts))
725 (when (interactive-p)
726 (message "Auto save is %s (in this buffer)"
727 (if wl-disable-auto-save "disabled" "enabled"))))
732 (defvar wl-biff-timer-name "wl-biff")
734 (defun wl-biff-stop ()
735 (when (get-itimer wl-biff-timer-name)
736 (delete-itimer wl-biff-timer-name)))
738 (defun wl-biff-start ()
740 (when wl-biff-check-folder-list
741 (start-itimer wl-biff-timer-name 'wl-biff-check-folders
742 wl-biff-check-interval wl-biff-check-interval
743 wl-biff-use-idle-timer))))
746 (defun wl-biff-stop ()
747 (when (get 'wl-biff 'timer)
748 (cancel-timer (get 'wl-biff 'timer))))
750 (defun wl-biff-start ()
752 (when wl-biff-check-folder-list
753 (if wl-biff-use-idle-timer
754 (if (get 'wl-biff 'timer)
755 (progn (timer-set-idle-time (get 'wl-biff 'timer)
756 wl-biff-check-interval t)
757 (timer-activate-when-idle (get 'wl-biff 'timer)))
760 wl-biff-check-interval t 'wl-biff-event-handler)))
761 (if (get 'wl-biff 'timer)
763 (timer-set-time (get 'wl-biff 'timer)
764 (timer-next-integral-multiple-of-time
765 (current-time) wl-biff-check-interval)
766 wl-biff-check-interval)
767 (timer-activate (get 'wl-biff 'timer)))
770 (timer-next-integral-multiple-of-time
771 (current-time) wl-biff-check-interval)
772 wl-biff-check-interval
773 'wl-biff-event-handler))))))
775 (defun-maybe timer-next-integral-multiple-of-time (time secs)
776 "Yield the next value after TIME that is an integral multiple of SECS.
777 More precisely, the next value, after TIME, that is an integral multiple
778 of SECS seconds since the epoch. SECS may be a fraction.
779 This function is imported from Emacs 20.7."
780 (let ((time-base (ash 1 16)))
782 ;; Use floating point, taking care to not lose precision.
783 (let* ((float-time-base (float time-base))
785 (time-usec (+ (* million
786 (+ (* float-time-base (nth 0 time))
789 (secs-usec (* million secs))
790 (mod-usec (mod time-usec secs-usec))
791 (next-usec (+ (- time-usec mod-usec) secs-usec))
792 (time-base-million (* float-time-base million)))
793 (list (floor next-usec time-base-million)
794 (floor (mod next-usec time-base-million) million)
795 (floor (mod next-usec million))))
796 ;; Floating point is not supported.
797 ;; Use integer arithmetic, avoiding overflow if possible.
798 (let* ((mod-sec (mod (+ (* (mod time-base secs)
799 (mod (nth 0 time) secs))
802 (next-1-sec (+ (- (nth 1 time) mod-sec) secs)))
803 (list (+ (nth 0 time) (floor next-1-sec time-base))
804 (mod next-1-sec time-base)
807 (defun wl-biff-event-handler ()
808 ;; PAKURing from FSF:time.el
809 (wl-biff-check-folders)
810 ;; Do redisplay right now, if no input pending.
812 (let* ((current (current-time))
813 (timer (get 'wl-biff 'timer))
814 ;; Compute the time when this timer will run again, next.
815 (next-time (timer-relative-time
816 (list (aref timer 1) (aref timer 2) (aref timer 3))
817 (* 5 (aref timer 4)) 0)))
818 ;; If the activation time is far in the past,
819 ;; skip executions until we reach a time in the future.
820 ;; This avoids a long pause if Emacs has been suspended for hours.
821 (or (> (nth 0 next-time) (nth 0 current))
822 (and (= (nth 0 next-time) (nth 0 current))
823 (> (nth 1 next-time) (nth 1 current)))
824 (and (= (nth 0 next-time) (nth 0 current))
825 (= (nth 1 next-time) (nth 1 current))
826 (> (nth 2 next-time) (nth 2 current)))
828 (timer-set-time timer (timer-next-integral-multiple-of-time
829 current wl-biff-check-interval)
830 wl-biff-check-interval)
831 (timer-activate timer)))))))
833 (defsubst wl-biff-notify (new-mails notify-minibuf)
834 (when (and (not wl-modeline-biff-status) (> new-mails 0))
835 (run-hooks 'wl-biff-notify-hook))
836 (when (and wl-modeline-biff-status (eq new-mails 0))
837 (run-hooks 'wl-biff-unnotify-hook))
838 (setq wl-modeline-biff-status (> new-mails 0))
839 (force-mode-line-update t)
841 (cond ((zerop new-mails) (message "No mail."))
842 ((= 1 new-mails) (message "You have a new mail."))
843 (t (message "You have %d new mails." new-mails)))))
845 ;; Internal variable.
846 (defvar wl-biff-check-folders-running nil)
848 (defun wl-biff-check-folders ()
850 (if wl-biff-check-folders-running
851 (when (interactive-p)
852 (message "Biff process is running."))
853 (setq wl-biff-check-folders-running t)
854 (when (interactive-p)
855 (message "Checking new mails..."))
857 (flist (or wl-biff-check-folder-list (list wl-default-folder)))
859 (if (eq (length flist) 1)
860 (wl-biff-check-folder-async (wl-folder-get-elmo-folder
861 (car flist) 'biff) (interactive-p))
864 (setq folder (wl-folder-get-elmo-folder (car flist))
866 (when (and (elmo-folder-plugged-p folder)
867 (elmo-folder-exists-p folder))
870 (nth 0 (wl-biff-check-folder folder))))))
871 (setq wl-biff-check-folders-running nil)
872 (wl-biff-notify new-mails (interactive-p)))))))
874 (defun wl-biff-check-folder (folder)
875 (if (eq (elmo-folder-type-internal folder) 'pop3)
876 (unless (elmo-pop3-get-session folder 'any-exists)
877 (wl-folder-check-one-entity (elmo-folder-name-internal folder)
879 (wl-folder-check-one-entity (elmo-folder-name-internal folder)
882 (defun wl-biff-check-folder-async-callback (diff data)
884 (with-current-buffer (nth 1 data)
885 (wl-folder-entity-hashtb-set wl-folder-entity-hashtb
888 (- (nth 1 diff) (nth 0 diff))
891 (setq wl-folder-info-alist-modified t)
892 (setq wl-biff-check-folders-running nil)
894 (wl-biff-notify (car diff) (nth 2 data)))
896 (defun wl-biff-check-folder-async (folder notify-minibuf)
897 (if (and (elmo-folder-plugged-p folder)
898 (wl-folder-entity-exists-p (elmo-folder-name-internal folder)))
900 (elmo-folder-set-biff-internal folder t)
901 (if (and (eq (elmo-folder-type-internal folder) 'imap4)
902 (elmo-folder-use-flag-p folder))
903 ;; Check asynchronously only when IMAP4 and use server diff.
905 (setq elmo-folder-diff-async-callback
906 'wl-biff-check-folder-async-callback)
907 (setq elmo-folder-diff-async-callback-data
908 (list (elmo-folder-name-internal folder)
909 (get-buffer wl-folder-buffer-name)
911 (elmo-folder-diff-async folder))
913 (wl-biff-notify (car (wl-biff-check-folder folder))
915 (setq wl-biff-check-folders-running nil))))
916 (setq wl-biff-check-folders-running nil)))
918 (if (and (fboundp 'regexp-opt)
919 (not (featurep 'xemacs)))
920 (defalias 'wl-regexp-opt 'regexp-opt)
921 (defun wl-regexp-opt (strings &optional paren)
922 "Return a regexp to match a string in STRINGS.
923 Each string should be unique in STRINGS and should not contain any regexps,
924 quoted or not. If optional PAREN is non-nil, ensure that the returned regexp
925 is enclosed by at least one regexp grouping construct."
926 (let ((open-paren (if paren "\\(" "")) (close-paren (if paren "\\)" "")))
927 (concat open-paren (mapconcat 'regexp-quote strings "\\|")
930 (defalias 'wl-expand-newtext 'elmo-expand-newtext)
931 (defalias 'wl-regexp-opt 'elmo-regexp-opt)
933 (defun wl-region-exists-p ()
934 "Return non-nil if a region exists on current buffer."
935 (static-if (featurep 'xemacs)
937 (and transient-mark-mode mark-active)))
939 (defun wl-deactivate-region ()
940 "Deactivate region on current buffer"
941 (static-if (not (featurep 'xemacs))
942 (setq mark-active nil)))
944 (defvar wl-line-string)
945 (defun wl-line-parse-format (format spec-alist)
946 "Make a formatter from FORMAT and SPEC-ALIST."
947 (let (f spec specs stack)
951 (goto-char (point-min))
952 (while (search-forward "%" nil t)
955 (goto-char (match-end 0)))
956 ((looking-at "\\(-?\\(0?\\)[0-9]*\\)\\([^0-9]\\)")
958 ((string= (match-string 3) "(")
959 (if (zerop (length (match-string 1)))
960 (error "No number specification for %%( line format"))
962 (match-beginning 0) ; start
963 (match-end 0) ; start-content
965 (match-string 1)) ; width
969 ((string= (match-string 3) ")")
970 (let ((entry (pop stack))
974 "No matching %%( parenthesis in summary line format"))
975 (goto-char (car entry)) ; start
976 (setq form (buffer-substring (nth 1 entry) ; start-content
977 (- (match-beginning 0) 1)))
978 (delete-region (car entry) (match-end 0))
983 (list (list 'wl-set-string-width (nth 2 entry)
989 (if (setq spec (assq (string-to-char (match-string 3))
993 (unless (string= "" (match-string 1))
994 (setq spec (list 'wl-set-string-width
995 (string-to-number (match-string 1))
997 (unless (string= "" (match-string 2))
998 (string-to-char (match-string 2))))))
999 (replace-match "s" 'fixed)
1000 (setq specs (append specs
1003 'setq 'wl-line-string
1006 (append (list 'format f) specs)))
1008 (defmacro wl-line-formatter-setup (formatter format alist)
1009 (` (let (byte-compile-warnings)
1013 (wl-line-parse-format (, format) (, alist)))))
1014 (when (get-buffer "*Compile-Log*")
1015 (bury-buffer "*Compile-Log*"))
1016 (when (get-buffer "*Compile-Log-Show*")
1017 (bury-buffer "*Compile-Log-Show*")))))
1019 (defsubst wl-copy-local-variables (src dst local-variables)
1020 "Copy value of LOCAL-VARIABLES from SRC buffer to DST buffer."
1021 (with-current-buffer dst
1022 (dolist (variable local-variables)
1023 (set (make-local-variable variable)
1024 (with-current-buffer src
1025 (symbol-value variable))))))
1027 ;;; Search Condition
1028 (defun wl-read-search-condition (default)
1029 "Read search condition string interactively."
1030 (wl-read-search-condition-internal "Search by" default))
1032 (defun wl-read-search-condition-internal (prompt default &optional paren)
1033 (let* ((completion-ignore-case t)
1034 (denial-fields (nconc (mapcar 'capitalize elmo-msgdb-extra-fields)
1035 '("Flag" "Since" "Before"
1036 "From" "Subject" "To" "Cc" "Body" "ToCc"
1037 "Larger" "Smaller")))
1038 (field (completing-read
1039 (format "%s (%s): " prompt default)
1041 (append '("AND" "OR" "Last" "First")
1043 (mapcar (lambda (f) (concat "!" f))
1046 (setq field (if (string= field "")
1047 (setq field default)
1050 ((or (string= field "AND") (string= field "OR"))
1051 (concat (if paren "(" "")
1052 (wl-read-search-condition-internal
1053 (concat field "(1) Search by") default 'paren)
1054 (if (string= field "AND") "&" "|")
1055 (wl-read-search-condition-internal
1056 (concat field "(2) Search by") default 'paren)
1058 ((string-match "Since\\|Before" field)
1059 (let ((default (format-time-string "%Y-%m-%d")))
1060 (setq value (completing-read
1061 (format "Value for '%s' [%s]: " field default)
1064 (list (format "%s" (car x)))))
1065 elmo-date-descriptions)))
1066 (concat (downcase field) ":"
1067 (if (equal value "") default value))))
1068 ((string-match "!?Flag" field)
1070 (setq value (downcase
1072 (format "Value for '%s': " field)
1073 (mapcar (lambda (f) (list (capitalize (symbol-name f))))
1076 '(unread answered forwarded digest any)
1077 (copy-sequence elmo-global-flags))
1079 (unless (elmo-flag-valid-p value)
1080 (message "Invalid char in `%s'" value)
1083 (unless (string-match (concat "^" elmo-condition-atom-regexp "$")
1085 (setq value (prin1-to-string value)))
1086 (concat (downcase field) ":" value))
1088 (setq value (read-from-minibuffer (format "Value for '%s': " field)))
1089 (unless (string-match (concat "^" elmo-condition-atom-regexp "$")
1091 (setq value (prin1-to-string value)))
1092 (concat (downcase field) ":" value)))))
1095 (product-provide (provide 'wl-util) (require 'wl-version))
1097 ;;; wl-util.el ends here