* wl-draft.el (wl-message-mail-p): Test resent-to: field.
[elisp/wanderlust.git] / wl / wl-util.el
1 ;;; wl-util.el -- Utility modules for Wanderlust.
2
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>
6
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
11
12 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
13
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)
17 ;; any later version.
18 ;;
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.
23 ;;
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.
28 ;;
29
30 ;;; Commentary:
31 ;;
32
33 ;;; Code:
34 ;;
35
36 (eval-when-compile
37   (require 'elmo-util))
38
39 (condition-case nil (require 'tm-edit) (error nil))
40 (condition-case nil (require 'pp) (error nil))
41
42 (eval-when-compile
43   (require 'time-stamp)
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))
51
52 (defalias 'wl-set-work-buf 'elmo-set-work-buf)
53 (make-obsolete 'wl-set-work-buf 'elmo-set-work-buf)
54
55 (defmacro wl-append (val func)
56   (list 'if val
57       (list 'nconc val func)
58     (list 'setq val func)))
59
60 (defun wl-parse (string regexp &optional matchn)
61   (or matchn (setq matchn 1))
62   (let (list)
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)))
67     (nreverse list)))
68
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))
77         (new-list nil)
78         sym-string sym)
79     (fillarray hashtable 0)
80     (while list
81       (setq sym-string
82             (if hack-addresses
83                 (wl-address-header-extract-address (car list))
84               (car list))
85             sym-string (or sym-string "-unparseable-garbage-")
86             sym (intern sym-string hashtable))
87       (if (boundp sym)
88           (and all (setcar (symbol-value sym) nil))
89         (setq new-list (cons (car list) new-list))
90         (set sym new-list))
91       (setq list (cdr list)))
92     (delq nil (nreverse new-list))))
93
94 ;; string utils.
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)
101
102 (defun wl-parse-addresses (string)
103   (if (null string)
104       ()
105     (elmo-set-work-buf
106      ;;(unwind-protect
107      (let (list start s char)
108        (insert string)
109        (goto-char (point-min))
110        (skip-chars-forward "\t\f\n\r ")
111        (setq start (point))
112        (while (not (eobp))
113          (skip-chars-forward "^\"\\,(")
114          (setq char (following-char))
115          (cond ((= char ?\\)
116                 (forward-char 1)
117                 (if (not (eobp))
118                     (forward-char 1)))
119                ((= 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)))
126                ((= char ?\")
127                 (re-search-forward "[^\\]\"" nil 0))
128                ((= char ?\()
129                 (let ((parens 1))
130                   (forward-char 1)
131                   (while (and (not (eobp)) (not (zerop parens)))
132                     (re-search-forward "[()]" nil 0)
133                     (cond ((or (eobp)
134                                (= (char-after (- (point) 2)) ?\\)))
135                           ((= (preceding-char) ?\()
136                            (setq parens (1+ parens)))
137                           (t
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
144      )))
145
146 (defun wl-append-element (list element)
147   (if element
148       (append list (list element))
149     list))
150
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)))
154
155 (defmacro wl-pop (l)
156   "Remove the head of the list stored in L."
157   (list 'car (list 'prog1 l (list 'setq l (list 'cdr l)))))
158
159 (defun wl-ask-folder (func mes-string)
160   (let* (key keve
161              (cmd (if (featurep 'xemacs)
162                       (event-to-character last-command-event)
163                     (string-to-char (format "%s" (this-command-keys))))))
164     (message mes-string)
165     (setq key (car (setq keve (wl-read-event-char))))
166     (if (or (equal key ?\ )
167             (and cmd
168                  (equal key cmd)))
169         (progn
170           (message "")
171           (funcall func))
172       (wl-push (cdr keve) unread-command-events))))
173
174 ;(defalias 'wl-make-hash 'elmo-make-hash)
175 ;;(make-obsolete 'wl-make-hash 'elmo-make-hash)
176
177 ;;(defalias 'wl-get-hash-val 'elmo-get-hash-val)
178 ;;(make-obsolete 'wl-get-hash-val 'elmo-get-hash-val)
179
180 ;;(defalias 'wl-set-hash-val 'elmo-set-hash-val)
181 ;;(make-obsolete 'wl-set-hash-val 'elmo-set-hash-val)
182
183 (defsubst wl-set-string-width (width string)
184   (elmo-set-work-buf
185    (elmo-set-buffer-multibyte default-enable-multibyte-characters)
186    (insert string)
187    (if (> (current-column) width)
188        (if (> (move-to-column width) width)
189            (progn
190              (condition-case nil ; ignore error
191                  (backward-char 1)
192                (error))
193              (concat (buffer-substring (point-min) (point)) " "))
194          (buffer-substring (point-min) (point)))
195      (if (= (current-column) width)
196          string
197        (concat string
198                (format (format "%%%ds"
199                                (- width (current-column)))
200                        " "))))))
201
202 (defun wl-display-bytes (num)
203   (let (result remain)
204     (cond
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))
212       (if (> remain 400)
213           (setq result (+ 1 result)))
214       (format "%dK" result))
215      (t (format "%dB" result)))))
216
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))
220           item)
221       (while items
222         (setq item (car items)
223               items (cdr items))
224         (unless (memq item '(biff plug))
225           (setq item 'title))
226         (setq priorities (cons item (delq item priorities)))))
227     (let (priority result)
228       (while priorities
229         (setq priority (car priorities)
230               priorities (cdr priorities))
231         (cond
232          ((eq 'biff priority)
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))))))
237          ((eq 'plug priority)
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))))))
242          (t
243           (setq result (append result (or id '("Wanderlust: %12b")))))))
244       (prog1
245           (setq mode-line-buffer-identification (if (stringp (car result))
246                                                     result
247                                                   (cons "" result)))
248         (force-mode-line-update t)))))
249
250 (defalias 'wl-display-error 'elmo-display-error)
251 (make-obsolete 'wl-display-error 'elmo-display-error)
252
253 (defun wl-get-assoc-list-value (assoc-list folder &optional match)
254   (catch 'found
255     (let ((alist assoc-list)
256           value pair)
257       (while alist
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))))
264                   ((not match)
265                    (throw 'found (cdr pair)))))
266         (setq alist (cdr alist)))
267       value)))
268
269 (defmacro wl-match-string (pos string)
270   "Substring POSth matched STRING."
271   (` (substring (, string) (match-beginning (, pos)) (match-end (, pos)))))
272
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)))))
277
278 (put 'wl-as-coding-system 'lisp-indent-function 1)
279 (put 'wl-as-mime-charset 'lisp-indent-function 1)
280
281 (eval-and-compile
282   (if wl-on-mule3
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)))
286              (,@ body))))
287     (if wl-on-mule
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)))
291                (,@ body))))
292       (if wl-on-nemacs
293           (defmacro wl-as-coding-system (coding-system &rest body)
294             (` (let ((default-kanji-fileio-code (, coding-system))
295                      (kanji-fileio-code (, coding-system))
296                      kanji-expected-code)
297                  (,@ body))))))))
298
299 (defmacro wl-as-mime-charset (mime-charset &rest body)
300   (` (wl-as-coding-system (mime-charset-to-coding-system (, mime-charset))
301        (,@ body))))
302
303 (defalias 'wl-string 'elmo-string)
304 (make-obsolete 'wl-string 'elmo-string)
305
306 (defun wl-parse-newsgroups (string &optional subscribe-only)
307   (let* ((nglist (wl-parse string "[ \t\f\r\n,]*\\([^ \t\f\r\n,]+\\)"))
308          ret-val)
309     (if (not subscribe-only)
310         nglist
311       (while nglist
312         (if (intern-soft (car nglist) wl-folder-newsgroups-hashtb)
313             (wl-append ret-val (list (car nglist))))
314         (setq nglist (cdr nglist)))
315       ret-val)))
316
317 ;; Check if active region exists or not.
318 (if (boundp 'mark-active)
319     (defmacro wl-region-exists-p ()
320       'mark-active)
321   (if (fboundp 'region-exists-p)
322       (defmacro wl-region-exists-p ()
323         (list 'region-exists-p))))
324
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))
333             tmp retval)
334         (if (< end beg)
335             (setq tmp end
336                   end beg
337                   beg tmp))
338         (setq ovls (nconc (car ovls) (cdr ovls)))
339         (while ovls
340           (setq tmp (car ovls)
341                 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))))
347         retval)))
348
349 (defsubst wl-repeat-string (str times)
350   (let ((loop times)
351         ret-val)
352     (while (> loop 0)
353       (setq ret-val (concat ret-val str))
354       (setq loop (- loop 1)))
355     ret-val))
356
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)))
360     (while list2
361       (setq list1 (delq (car list2) list1))
362       (setq list2 (cdr list2)))
363     list1))
364
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)))
368     (if entry
369         (progn
370           (when (not (member value (cdr entry)))
371             (nconc entry (list value)))
372           alist)
373       (append alist
374               (list (list item value))))))
375
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
380 the value of `foo'."
381   (let (entry)
382     (while (setq entry (assq key alist))
383       (setq alist (delq entry alist)))
384     alist))
385
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'."
392   (while keys
393     (setq alist (wl-delete-alist (car keys) alist))
394     (setq keys (cdr keys)))
395   alist)
396
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)
403     (while keys
404       (setq x (car keys))
405       (setq y (cdr (assq x alist)))
406       (if y
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)))
412     result))
413
414 (eval-when-compile
415   (require 'static))
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)))
424
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."
429     (save-excursion
430       (set-buffer (generate-new-buffer " pp-to-string"))
431       (unwind-protect
432           (progn
433             (lisp-mode-variables t)
434             (let ((print-escape-newlines pp-escape-newlines))
435               (prin1 object (current-buffer)))
436             (goto-char (point-min))
437             (while (not (eobp))
438               (cond
439                ((looking-at "\\s(\\|#\\s(")
440                 (while (looking-at "\\s(\\|#\\s(")
441                   (forward-char 1)))
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.
446                      (save-excursion
447                        (goto-char (match-beginning 2))
448                        (forward-sexp)
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))
453                 (insert "'")
454                 (forward-sexp 1)
455                 (if (looking-at "[ \t]*\)")
456                     (delete-region (match-beginning 0) (match-end 0))
457                   (error "Malformed quote"))
458                 (backward-sexp 1))
459                ((condition-case err-var
460                     (prog1 t (down-list 1))
461                   (error nil))
462                 (backward-char 1)
463                 (skip-chars-backward " \t")
464                 (delete-region
465                  (point)
466                  (progn (skip-chars-forward " \t") (point)))
467                 (if (not (char-equal ?' (char-after (1- (point)))))
468                     (insert ?\n)))
469                ((condition-case err-var
470                     (prog1 t (up-list 1))
471                   (error nil))
472                 (while (looking-at "\\s)")
473                   (forward-char 1))
474                 (skip-chars-backward " \t")
475                 (delete-region
476                  (point)
477                  (progn (skip-chars-forward " \t") (point)))
478                 (if (not (char-equal ?' (char-after (1- (point)))))
479                     (insert ?\n)))
480                (t (goto-char (point-max)))))
481             (goto-char (point-min))
482             (indent-sexp)
483             (buffer-string))
484         (kill-buffer (current-buffer))))))
485
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)
493         time)))
494
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]"
498                   s)
499     (concat (wl-match-string 1 s) ", "
500             (timezone-make-date-arpa-standard s (current-time-zone)))))
501
502 (defun wl-date-iso8601 (date)
503   "Convert the DATE to YYMMDDTHHMMSS."
504   (condition-case ()
505       (wl-get-date-iso8601 date)
506     (error "")))
507
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))))
513
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.")))
520
521 (defun wl-url-nntp (url &rest args)
522   (interactive "sURL: ")
523   (let (folder fld-name server port msg)
524     (if (string-match
525          "^nntp://\\([^:/]*\\):?\\([0-9]*\\)/\\([^/]*\\)/\\([0-9]*\\).*$" url)
526         (progn
527           (if (eq (length (setq fld-name
528                                 (elmo-match-string 3 url))) 0)
529               (setq fld-name nil))
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."))))
547
548 (defmacro wl-concat-list (list separator)
549   (` (mapconcat 'identity (delete "" (delq nil (, list))) (, separator))))
550
551 (defmacro wl-current-message-buffer ()
552   (` (save-excursion
553        (if (buffer-live-p wl-current-summary-buffer)
554            (set-buffer wl-current-summary-buffer))
555        wl-message-buf-name)))
556
557 (defmacro wl-kill-buffers (regexp)
558   (` (mapcar (function
559               (lambda (x)
560                 (if (and (buffer-name x)
561                          (string-match (, regexp) (buffer-name x)))
562                     (and (get-buffer x)
563                          (kill-buffer x)))))
564              (buffer-list))))
565
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"
571               (substring date -4)
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)))))
576
577 (defun wl-collect-summary ()
578   (let (result)
579     (mapcar
580      (function (lambda (x)
581                  (if (and (string-match "^Summary"
582                                         (buffer-name x))
583                           (save-excursion
584                             (set-buffer x)
585                             (equal major-mode 'wl-summary-mode)))
586                      (setq result (nconc result (list x))))))
587      (buffer-list))
588     result))
589
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))
596       dir)))
597
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)))
603            t))))
604
605 (defun wl-number-base36 (num len)
606   (if (if (< len 0)
607           (<= num 0)
608         (= len 0))
609       ""
610     (concat (wl-number-base36 (/ num 36) (1- len))
611             (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
612                                   (% num 36))))))
613
614 (defvar wl-unique-id-char nil)
615
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.
623            (* 25 25)))
624   (let ((tm (static-if (fboundp 'current-time)
625                 (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)
638                                              (nth 6 cts))))))))
639     (concat
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) ?_))
644            user)
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.
653      ".wl")))
654
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
659               (if wl-local-domain
660                   (concat (system-name) "." wl-local-domain)
661                 (system-name)))
662           ">"))
663
664 ;;; Profile loading.
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))
670
671 (defun wl-load-profile ()
672   "Call `wl-load-profile-func' function."
673   (funcall wl-load-profile-func))
674
675 ;;;
676
677 (defmacro wl-count-lines ()
678   (` (save-excursion
679        (beginning-of-line)
680        (count-lines 1 (point)))))
681
682 (defun wl-horizontal-recenter ()
683   "Recenter the current buffer horizontally."
684   (beginning-of-line)
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)))
690            (max 0))
691       (when end
692         ;; Find the longest line currently displayed in the window.
693         (goto-char (window-start))
694         (while (and (not (eobp))
695                     (< (point) end))
696           (end-of-line)
697           (setq max (max max (current-column)))
698           (forward-line 1))
699         (goto-char orig)
700         ;; Scroll horizontally to center (sort of) the point.
701         (if (> max (window-width))
702             (set-window-hscroll
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))
707         max))))
708
709 ;; Biff
710 (static-cond
711  (wl-on-xemacs
712   (defvar wl-biff-timer-name "wl-biff")
713
714   (defun wl-biff-stop ()
715     (when (get-itimer wl-biff-timer-name)
716       (delete-itimer wl-biff-timer-name)))
717
718   (defun wl-biff-start ()
719     (wl-biff-stop)
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))))
724
725  ((and (condition-case nil (require 'timer) (error nil));; FSFmacs 19+
726        (fboundp 'timer-activate))
727
728   (defun wl-biff-stop ()
729     (when (get 'wl-biff 'timer)
730       (cancel-timer (get 'wl-biff 'timer))))
731
732   (defun wl-biff-start ()
733     (require 'timer)
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)))))
743
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)))
750       (if (fboundp 'atan)
751           ;; Use floating point, taking care to not lose precision.
752           (let* ((float-time-base (float time-base))
753                  (million 1000000.0)
754                  (time-usec (+ (* million
755                                   (+ (* float-time-base (nth 0 time))
756                                      (nth 1 time)))
757                                (nth 2 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))
769                                 (nth 1 time))
770                              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)
774                 0)))))
775
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.
780     (sit-for 0)
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)))
796           (progn
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))))))
801  (t
802   (fset 'wl-biff-stop 'ignore)
803   (fset 'wl-biff-start 'ignore)))
804
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)
812   (when notify-minibuf
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)))))
816
817 ;; Internal variable.
818 (defvar wl-biff-check-folders-running nil)
819
820 (defun wl-biff-check-folders ()
821   (interactive)
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..."))
828     (let ((new-mails 0)
829           (flist (or wl-biff-check-folder-list (list wl-default-folder)))
830           folder)
831       (if (eq (length flist) 1)
832           (wl-biff-check-folder-async (car flist) (interactive-p))
833         (unwind-protect
834             (while flist
835               (setq folder (car flist)
836                     flist (cdr flist))
837               (when (elmo-folder-plugged-p folder)
838                 (setq new-mails
839                       (+ new-mails
840                          (nth 0 (wl-biff-check-folder folder))))))
841           (setq wl-biff-check-folders-running nil)
842           (wl-biff-notify new-mails (interactive-p)))))))
843
844 (defun wl-biff-check-folder (folder)
845   (if (eq (elmo-folder-get-type folder) 'pop3)
846       (unless (elmo-pop3-get-session (elmo-folder-get-spec folder) 'if-exists)
847         ;; Currently no main pop3 process.
848         (let ((elmo-network-session-name-prefix "BIFF-"))
849           (wl-folder-check-one-entity folder)))
850     (let ((elmo-network-session-name-prefix "BIFF-"))
851       (wl-folder-check-one-entity folder))))
852
853 (defun wl-biff-check-folder-async-callback (diff data)
854   (if (nth 1 data)
855       (with-current-buffer (nth 1 data)
856         (wl-folder-entity-hashtb-set wl-folder-entity-hashtb (nth 0 data)
857                                      (list (car diff) 0 (cdr diff))
858                                      (current-buffer))))
859   (setq wl-folder-info-alist-modified t)
860   (setq wl-biff-check-folders-running nil)
861   (sit-for 0)
862   (wl-biff-notify (car diff) (nth 2 data)))
863
864 (defun wl-biff-check-folder-async (folder notify-minibuf)
865   (when (elmo-folder-plugged-p folder)
866     (let ((type (elmo-folder-get-type folder)))
867       (if (and (eq type 'imap4)
868                (wl-folder-use-server-diff-p folder))
869           ;; Check asynchronously only when IMAP4 and use server diff.
870           (progn
871             (setq elmo-folder-diff-async-callback
872                   'wl-biff-check-folder-async-callback)
873             (setq elmo-folder-diff-async-callback-data
874                   (list folder (get-buffer wl-folder-buffer-name)
875                         notify-minibuf))
876             (let ((elmo-network-session-name-prefix "BIFF-"))
877               (elmo-folder-diff-async folder)))
878         (unwind-protect
879             (wl-biff-notify (car (wl-biff-check-folder folder))
880                             notify-minibuf)
881           (setq wl-biff-check-folders-running nil))))))
882
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 "\\|")
893               close-paren))))
894
895 (require 'product)
896 (product-provide (provide 'wl-util) (require 'wl-version))
897
898 ;;; wl-util.el ends here