* wl-folder.el (wl-folder-check-one-entity): Use `new' value of diff.
[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 'pp) (error nil))
40
41 (eval-when-compile
42   (require 'time-stamp)
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))
50
51 (defalias 'wl-set-work-buf 'elmo-set-work-buf)
52 (make-obsolete 'wl-set-work-buf 'elmo-set-work-buf)
53
54 (defmacro wl-append (val func)
55   (list 'if val
56       (list 'nconc val func)
57     (list 'setq val func)))
58
59 (defalias 'wl-parse 'elmo-parse)
60 (make-obsolete 'wl-parse 'elmo-parse)
61
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))
70         (new-list nil)
71         sym-string sym)
72     (fillarray hashtable 0)
73     (while list
74       (setq sym-string
75             (if hack-addresses
76                 (wl-address-header-extract-address (car list))
77               (car list))
78             sym-string (or sym-string "-unparseable-garbage-")
79             sym (intern sym-string hashtable))
80       (if (boundp sym)
81           (and all (setcar (symbol-value sym) nil))
82         (setq new-list (cons (car list) new-list))
83         (set sym new-list))
84       (setq list (cdr list)))
85     (delq nil (nreverse new-list))))
86
87 ;; string utils.
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)
94
95 (defun wl-parse-addresses (string)
96   (if (null string)
97       ()
98     (elmo-set-work-buf
99      ;;(unwind-protect
100      (let (list start s char)
101        (insert string)
102        (goto-char (point-min))
103        (skip-chars-forward "\t\f\n\r ")
104        (setq start (point))
105        (while (not (eobp))
106          (skip-chars-forward "^\"\\,(")
107          (setq char (following-char))
108          (cond ((= char ?\\)
109                 (forward-char 1)
110                 (if (not (eobp))
111                     (forward-char 1)))
112                ((= 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)))
119                ((= char ?\")
120                 (re-search-forward "[^\\]\"" nil 0))
121                ((= char ?\()
122                 (let ((parens 1))
123                   (forward-char 1)
124                   (while (and (not (eobp)) (not (zerop parens)))
125                     (re-search-forward "[()]" nil 0)
126                     (cond ((or (eobp)
127                                (= (char-after (- (point) 2)) ?\\)))
128                           ((= (preceding-char) ?\()
129                            (setq parens (1+ parens)))
130                           (t
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
137      )))
138
139 (defun wl-append-element (list element)
140   (if element
141       (append list (list element))
142     list))
143
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)))
147
148 (defmacro wl-pop (l)
149   "Remove the head of the list stored in L."
150   (list 'car (list 'prog1 l (list 'setq l (list 'cdr l)))))
151
152 (defun wl-ask-folder (func mes-string)
153   (let* (key keve
154              (cmd (if (featurep 'xemacs)
155                       (event-to-character last-command-event)
156                     (string-to-char (format "%s" (this-command-keys))))))
157     (message mes-string)
158     (setq key (car (setq keve (wl-read-event-char))))
159     (if (or (equal key ?\ )
160             (and cmd
161                  (equal key cmd)))
162         (progn
163           (message "")
164           (funcall func))
165       (wl-push (cdr keve) unread-command-events))))
166
167 ;(defalias 'wl-make-hash 'elmo-make-hash)
168 ;;(make-obsolete 'wl-make-hash 'elmo-make-hash)
169
170 ;;(defalias 'wl-get-hash-val 'elmo-get-hash-val)
171 ;;(make-obsolete 'wl-get-hash-val 'elmo-get-hash-val)
172
173 ;;(defalias 'wl-set-hash-val 'elmo-set-hash-val)
174 ;;(make-obsolete 'wl-set-hash-val 'elmo-set-hash-val)
175
176 (defsubst wl-set-string-width (width string)
177   (elmo-set-work-buf
178    (elmo-set-buffer-multibyte default-enable-multibyte-characters)
179    (insert string)
180    (if (> (current-column) width)
181        (if (> (move-to-column width) width)
182            (progn
183              (condition-case nil ; ignore error
184                  (backward-char 1)
185                (error))
186              (concat (buffer-substring (point-min) (point)) " "))
187          (buffer-substring (point-min) (point)))
188      (if (= (current-column) width)
189          string
190        (concat string
191                (format (format "%%%ds"
192                                (- width (current-column)))
193                        " "))))))
194
195 (defun wl-display-bytes (num)
196   (let (result remain)
197     (cond
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))
205       (if (> remain 400)
206           (setq result (+ 1 result)))
207       (format "%dK" result))
208      (t (format "%dB" result)))))
209
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))
213           item)
214       (while items
215         (setq item (car items)
216               items (cdr items))
217         (unless (memq item '(biff plug))
218           (setq item 'title))
219         (setq priorities (cons item (delq item priorities)))))
220     (let (priority result)
221       (while priorities
222         (setq priority (car priorities)
223               priorities (cdr priorities))
224         (cond
225          ((eq 'biff priority)
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))))))
230          ((eq 'plug priority)
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))))))
235          (t
236           (setq result (append result (or id '("Wanderlust: %12b")))))))
237       (prog1
238           (setq mode-line-buffer-identification (if (stringp (car result))
239                                                     result
240                                                   (cons "" result)))
241         (force-mode-line-update t)))))
242
243 (defalias 'wl-display-error 'elmo-display-error)
244 (make-obsolete 'wl-display-error 'elmo-display-error)
245
246 (defun wl-get-assoc-list-value (assoc-list folder &optional match)
247   (catch 'found
248     (let ((alist assoc-list)
249           value pair)
250       (while alist
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))))
257                   ((not match)
258                    (throw 'found (cdr pair)))))
259         (setq alist (cdr alist)))
260       value)))
261
262 (defmacro wl-match-string (pos string)
263   "Substring POSth matched STRING."
264   (` (substring (, string) (match-beginning (, pos)) (match-end (, pos)))))
265
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)))))
270
271 (put 'wl-as-coding-system 'lisp-indent-function 1)
272 (put 'wl-as-mime-charset 'lisp-indent-function 1)
273
274 (eval-and-compile
275   (if wl-on-mule3
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)))
279              (,@ body))))
280     (if wl-on-mule
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)))
284                (,@ body)))))))
285
286 (defmacro wl-as-mime-charset (mime-charset &rest body)
287   (` (wl-as-coding-system (mime-charset-to-coding-system (, mime-charset))
288        (,@ body))))
289
290 (defalias 'wl-string 'elmo-string)
291 (make-obsolete 'wl-string 'elmo-string)
292
293 ;; Check if active region exists or not.
294 (if (boundp 'mark-active)
295     (defmacro wl-region-exists-p ()
296       'mark-active)
297   (if (fboundp 'region-exists-p)
298       (defmacro wl-region-exists-p ()
299         (list 'region-exists-p))))
300
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))
309             tmp retval)
310         (if (< end beg)
311             (setq tmp end
312                   end beg
313                   beg tmp))
314         (setq ovls (nconc (car ovls) (cdr ovls)))
315         (while ovls
316           (setq tmp (car ovls)
317                 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))))
323         retval)))
324
325 (defsubst wl-repeat-string (str times)
326   (let ((loop times)
327         ret-val)
328     (while (> loop 0)
329       (setq ret-val (concat ret-val str))
330       (setq loop (- loop 1)))
331     ret-val))
332
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)))
336     (while list2
337       (setq list1 (delq (car list2) list1))
338       (setq list2 (cdr list2)))
339     list1))
340
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)))
344     (if entry
345         (progn
346           (when (not (member value (cdr entry)))
347             (nconc entry (list value)))
348           alist)
349       (append alist
350               (list (list item value))))))
351
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
356 the value of `foo'."
357   (let (entry)
358     (while (setq entry (assq key alist))
359       (setq alist (delq entry alist)))
360     alist))
361
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'."
368   (while keys
369     (setq alist (wl-delete-alist (car keys) alist))
370     (setq keys (cdr keys)))
371   alist)
372
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)
379     (while keys
380       (setq x (car keys))
381       (setq y (cdr (assq x alist)))
382       (if y
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)))
388     result))
389
390 (eval-when-compile
391   (require 'static))
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)))
400
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."
405     (save-excursion
406       (set-buffer (generate-new-buffer " pp-to-string"))
407       (unwind-protect
408           (progn
409             (lisp-mode-variables t)
410             (let ((print-escape-newlines pp-escape-newlines))
411               (prin1 object (current-buffer)))
412             (goto-char (point-min))
413             (while (not (eobp))
414               (cond
415                ((looking-at "\\s(\\|#\\s(")
416                 (while (looking-at "\\s(\\|#\\s(")
417                   (forward-char 1)))
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.
422                      (save-excursion
423                        (goto-char (match-beginning 2))
424                        (forward-sexp)
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))
429                 (insert "'")
430                 (forward-sexp 1)
431                 (if (looking-at "[ \t]*\)")
432                     (delete-region (match-beginning 0) (match-end 0))
433                   (error "Malformed quote"))
434                 (backward-sexp 1))
435                ((condition-case err-var
436                     (prog1 t (down-list 1))
437                   (error nil))
438                 (backward-char 1)
439                 (skip-chars-backward " \t")
440                 (delete-region
441                  (point)
442                  (progn (skip-chars-forward " \t") (point)))
443                 (if (not (char-equal ?' (char-after (1- (point)))))
444                     (insert ?\n)))
445                ((condition-case err-var
446                     (prog1 t (up-list 1))
447                   (error nil))
448                 (while (looking-at "\\s)")
449                   (forward-char 1))
450                 (skip-chars-backward " \t")
451                 (delete-region
452                  (point)
453                  (progn (skip-chars-forward " \t") (point)))
454                 (if (not (char-equal ?' (char-after (1- (point)))))
455                     (insert ?\n)))
456                (t (goto-char (point-max)))))
457             (goto-char (point-min))
458             (indent-sexp)
459             (buffer-string))
460         (kill-buffer (current-buffer))))))
461
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)
469         time)))
470
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]"
474                   s)
475     (concat (wl-match-string 1 s) ", "
476             (timezone-make-date-arpa-standard s (current-time-zone)))))
477
478 (defun wl-date-iso8601 (date)
479   "Convert the DATE to YYMMDDTHHMMSS."
480   (condition-case ()
481       (wl-get-date-iso8601 date)
482     (error "")))
483
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))))
489
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.")))
496
497 (defun wl-url-nntp (url &rest args)
498   (interactive "sURL: ")
499   (let (folder fld-name server port msg)
500     (if (string-match
501          "^nntp://\\([^:/]*\\):?\\([0-9]*\\)/\\([^/]*\\)/\\([0-9]*\\).*$" url)
502         (progn
503           (if (eq (length (setq fld-name
504                                 (elmo-match-string 3 url))) 0)
505               (setq fld-name nil))
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."))))
523
524 (defmacro wl-concat-list (list separator)
525   (` (mapconcat 'identity (delete "" (delq nil (, list))) (, separator))))
526
527 (defmacro wl-current-message-buffer ()
528   (` (save-excursion
529        (if (buffer-live-p wl-current-summary-buffer)
530            (set-buffer wl-current-summary-buffer))
531        wl-message-buffer)))
532
533 (defmacro wl-kill-buffers (regexp)
534   (` (mapcar (function
535               (lambda (x)
536                 (if (and (buffer-name x)
537                          (string-match (, regexp) (buffer-name x)))
538                     (and (get-buffer x)
539                          (kill-buffer x)))))
540              (buffer-list))))
541
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"
547               (substring date -4)
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)))))
552
553 (defun wl-collect-summary ()
554   (let (result)
555     (mapcar
556      (function (lambda (x)
557                  (if (and (string-match "^Summary"
558                                         (buffer-name x))
559                           (save-excursion
560                             (set-buffer x)
561                             (equal major-mode 'wl-summary-mode)))
562                      (setq result (nconc result (list x))))))
563      (buffer-list))
564     result))
565
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))
572       dir)))
573
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)))
579            t))))
580
581 (defun wl-number-base36 (num len)
582   (if (if (< len 0)
583           (<= num 0)
584         (= len 0))
585       ""
586     (concat (wl-number-base36 (/ num 36) (1- len))
587             (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
588                                   (% num 36))))))
589
590 (defvar wl-unique-id-char nil)
591
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.
599            (* 25 25)))
600   (let ((tm (static-if (fboundp 'current-time)
601                 (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)
614                                              (nth 6 cts))))))))
615     (concat
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) ?_))
620            user)
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.
629      ".wl")))
630
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
635               (if wl-local-domain
636                   (concat (system-name) "." wl-local-domain)
637                 (system-name)))
638           ">"))
639
640 ;;; Profile loading.
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))
646
647 (defun wl-load-profile ()
648   "Call `wl-load-profile-function' function."
649   (funcall wl-load-profile-function))
650
651 ;;;
652
653 (defmacro wl-count-lines ()
654   (` (save-excursion
655        (beginning-of-line)
656        (count-lines 1 (point)))))
657
658 (defun wl-horizontal-recenter ()
659   "Recenter the current buffer horizontally."
660   (beginning-of-line)
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)))
666            (max 0))
667       (when end
668         ;; Find the longest line currently displayed in the window.
669         (goto-char (window-start))
670         (while (and (not (eobp))
671                     (< (point) end))
672           (end-of-line)
673           (setq max (max max (current-column)))
674           (forward-line 1))
675         (goto-char orig)
676         ;; Scroll horizontally to center (sort of) the point.
677         (if (> max (window-width))
678             (set-window-hscroll
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))
683         max))))
684
685 ;; Biff
686 (static-cond
687  (wl-on-xemacs
688   (defvar wl-biff-timer-name "wl-biff")
689
690   (defun wl-biff-stop ()
691     (when (get-itimer wl-biff-timer-name)
692       (delete-itimer wl-biff-timer-name)))
693
694   (defun wl-biff-start ()
695     (wl-biff-stop)
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))))
700
701  ((and (condition-case nil (require 'timer) (error nil));; FSFmacs 19+
702        (fboundp 'timer-activate))
703
704   (defun wl-biff-stop ()
705     (when (get 'wl-biff 'timer)
706       (cancel-timer (get 'wl-biff 'timer))))
707
708   (defun wl-biff-start ()
709     (require 'timer)
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)))))
719
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)))
726       (if (fboundp 'atan)
727           ;; Use floating point, taking care to not lose precision.
728           (let* ((float-time-base (float time-base))
729                  (million 1000000.0)
730                  (time-usec (+ (* million
731                                   (+ (* float-time-base (nth 0 time))
732                                      (nth 1 time)))
733                                (nth 2 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))
745                                 (nth 1 time))
746                              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)
750                 0)))))
751
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.
756     (sit-for 0)
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)))
772           (progn
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))))))
777  (t
778   (fset 'wl-biff-stop 'ignore)
779   (fset 'wl-biff-start 'ignore)))
780
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)
788   (when notify-minibuf
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)))))
792
793 ;; Internal variable.
794 (defvar wl-biff-check-folders-running nil)
795
796 (defun wl-biff-check-folders ()
797   (interactive)
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..."))
804     (let ((new-mails 0)
805           (flist (or wl-biff-check-folder-list (list wl-default-folder)))
806           folder)
807       (if (eq (length flist) 1)
808           (wl-biff-check-folder-async (wl-folder-get-elmo-folder
809                                        (car flist)) (interactive-p))
810         (unwind-protect
811             (while flist
812               (setq folder (wl-folder-get-elmo-folder (car flist))
813                     flist (cdr flist))
814               (when (elmo-folder-plugged-p folder)
815                 (setq new-mails
816                       (+ new-mails
817                          (nth 0 (wl-biff-check-folder folder))))))
818           (setq wl-biff-check-folders-running nil)
819           (wl-biff-notify new-mails (interactive-p)))))))
820
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)))))
830
831 (defun wl-biff-check-folder-async-callback (diff data)
832   (if (nth 1 data)
833       (with-current-buffer (nth 1 data)
834         (wl-folder-entity-hashtb-set wl-folder-entity-hashtb
835                                      (nth 0 data)
836                                      (list (nth 0 diff)
837                                            (nth 1 diff)
838                                            (nth 2 diff))
839                                      (current-buffer))))
840   (setq wl-folder-info-alist-modified t)
841   (setq wl-biff-check-folders-running nil)
842   (sit-for 0)
843   (wl-biff-notify (car diff) (nth 2 data)))
844
845 (defun wl-biff-check-folder-async (folder notify-minibuf)
846   (when (elmo-folder-plugged-p folder)
847     (if (and (eq (elmo-folder-type-internal folder) 'imap4)
848              (elmo-folder-use-flag-p folder))
849         ;; Check asynchronously only when IMAP4 and use server diff.
850         (progn
851           (setq elmo-folder-diff-async-callback
852                 'wl-biff-check-folder-async-callback)
853           (setq elmo-folder-diff-async-callback-data
854                 (list (elmo-folder-name-internal folder)
855                       (get-buffer wl-folder-buffer-name)
856                       notify-minibuf))
857           (let ((elmo-network-session-name-prefix "BIFF-"))
858             (elmo-folder-diff-async folder)))
859       (unwind-protect
860           (wl-biff-notify (car (wl-biff-check-folder folder))
861                           notify-minibuf)
862         (setq wl-biff-check-folders-running nil)))))
863
864 (if (and (fboundp 'regexp-opt)
865          (not (featurep 'xemacs)))
866     (defalias 'wl-regexp-opt 'regexp-opt)
867   (defun wl-regexp-opt (strings &optional paren)
868     "Return a regexp to match a string in STRINGS.
869 Each string should be unique in STRINGS and should not contain any regexps,
870 quoted or not.  If optional PAREN is non-nil, ensure that the returned regexp
871 is enclosed by at least one regexp grouping construct."
872     (let ((open-paren (if paren "\\(" "")) (close-paren (if paren "\\)" "")))
873       (concat open-paren (mapconcat 'regexp-quote strings "\\|")
874               close-paren))))
875
876 (defun wl-expand-newtext (newtext original)
877   (let ((len (length newtext))
878         (pos 0)
879         c expanded beg N did-expand)
880     (while (< pos len)
881       (setq beg pos)
882       (while (and (< pos len)
883                   (not (= (aref newtext pos) ?\\)))
884         (setq pos (1+ pos)))
885       (unless (= beg pos)
886         (push (substring newtext beg pos) expanded))
887       (when (< pos len)
888         ;; We hit a \; expand it.
889         (setq did-expand t
890               pos (1+ pos)
891               c (aref newtext pos))
892         (if (not (or (= c ?\&)
893                      (and (>= c ?1)
894                           (<= c ?9))))
895             ;; \ followed by some character we don't expand.
896             (push (char-to-string c) expanded)
897           ;; \& or \N
898           (if (= c ?\&)
899               (setq N 0)
900             (setq N (- c ?0)))
901           (when (match-beginning N)
902             (push (substring original (match-beginning N) (match-end N))
903                   expanded))))
904       (setq pos (1+ pos)))
905     (if did-expand
906         (apply (function concat) (nreverse expanded))
907       newtext)))
908
909 (require 'product)
910 (product-provide (provide 'wl-util) (require 'wl-version))
911
912 ;;; wl-util.el ends here