Merge from trunk:
[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 (require 'bytecomp)
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 &optional padding ignore-invalid)
177   "Make a new string which have specified WIDTH and content of STRING.
178 `wl-invalid-character-message' is used when invalid character is contained.
179 If WIDTH is negative number, padding chars are added to the head and
180 otherwise, padding chars are added to the tail of the string.
181 The optional 3rd arg PADDING, if non-nil, specifies a padding character
182 to add the result instead of white space.
183 If optional 4th argument is non-nil, don't use `wl-invalid-character-message'
184 even when invalid character is contained."
185   (static-cond
186    ((and (fboundp 'string-width) (fboundp 'truncate-string-to-width)
187          (not (featurep 'xemacs)))
188     (if (> (string-width string) (abs width))
189         (setq string (truncate-string-to-width string (abs width))))
190     (if (= (string-width string) (abs width))
191         string
192       (when (and (not ignore-invalid)
193                  (< (abs width) (string-width string)))
194         (setq string
195               (truncate-string-to-width wl-invalid-character-message
196                                         (abs width))))
197       (let ((paddings (make-string
198                        (max 0 (- (abs width) (string-width string)))
199                        (or padding ?\ ))))
200         (if (< width 0)
201             (concat paddings string)
202           (concat string paddings)))))
203    (t
204     (elmo-set-work-buf
205      (elmo-set-buffer-multibyte default-enable-multibyte-characters)
206      (insert string)
207      (when (> (current-column) (abs width))
208        (when (> (move-to-column (abs width)) (abs width))
209          (condition-case nil ; ignore error
210              (backward-char 1)
211            (error)))
212        (setq string (buffer-substring (point-min) (point))))
213      (if (= (current-column) (abs width))
214          string
215        (let ((paddings (make-string (- (abs width) (current-column))
216                                     (or padding ?\ ))))
217          (if (< width 0)
218              (concat paddings string)
219            (concat string paddings))))))))
220
221 (defun wl-mode-line-buffer-identification (&optional id)
222   (let ((priorities '(biff plug title)))
223     (let ((items (reverse wl-mode-line-display-priority-list))
224           item)
225       (while items
226         (setq item (car items)
227               items (cdr items))
228         (unless (memq item '(biff plug))
229           (setq item 'title))
230         (setq priorities (cons item (delq item priorities)))))
231     (let (priority result)
232       (while priorities
233         (setq priority (car priorities)
234               priorities (cdr priorities))
235         (cond
236          ((eq 'biff priority)
237           (when wl-biff-check-folder-list
238             (setq result (append result '((wl-modeline-biff-status
239                                            wl-modeline-biff-state-on
240                                            wl-modeline-biff-state-off))))))
241          ((eq 'plug priority)
242           (when wl-show-plug-status-on-modeline
243             (setq result (append result '((wl-modeline-plug-status
244                                            wl-modeline-plug-state-on
245                                            wl-modeline-plug-state-off))))))
246          (t
247           (setq result (append result (or id '("Wanderlust: %12b")))))))
248       (prog1
249           (setq mode-line-buffer-identification (if (stringp (car result))
250                                                     result
251                                                   (cons "" result)))
252         (force-mode-line-update t)))))
253
254 (defalias 'wl-display-error 'elmo-display-error)
255 (make-obsolete 'wl-display-error 'elmo-display-error)
256
257 (defun wl-get-assoc-list-value (assoc-list folder &optional match)
258   (catch 'found
259     (let ((alist assoc-list)
260           value pair)
261       (while alist
262         (setq pair (car alist))
263         (if (string-match (car pair) folder)
264             (cond ((eq match 'all)
265                    (setq value (append value (list (cdr pair)))))
266                   ((eq match 'all-list)
267                    (setq value (append value (cdr pair))))
268                   ((not match)
269                    (throw 'found (cdr pair)))))
270         (setq alist (cdr alist)))
271       value)))
272
273 (defmacro wl-match-string (pos string)
274   "Substring POSth matched STRING."
275   (` (substring (, string) (match-beginning (, pos)) (match-end (, pos)))))
276
277 (defmacro wl-match-buffer (pos)
278   "Substring POSth matched from the current buffer."
279   (` (buffer-substring-no-properties
280       (match-beginning (, pos)) (match-end (, pos)))))
281
282 (put 'wl-as-coding-system 'lisp-indent-function 1)
283 (put 'wl-as-mime-charset 'lisp-indent-function 1)
284
285 (eval-and-compile
286   (if wl-on-mule3
287       (defmacro wl-as-coding-system (coding-system &rest body)
288         (` (let ((coding-system-for-read (, coding-system))
289                  (coding-system-for-write (, coding-system)))
290              (,@ body))))
291     (if wl-on-mule
292         (defmacro wl-as-coding-system (coding-system &rest body)
293           (` (let ((file-coding-system-for-read (, coding-system))
294                    (file-coding-system (, coding-system)))
295                (,@ body)))))))
296
297 (defmacro wl-as-mime-charset (mime-charset &rest body)
298   (` (wl-as-coding-system (mime-charset-to-coding-system (, mime-charset))
299        (,@ body))))
300
301 (defalias 'wl-string 'elmo-string)
302 (make-obsolete 'wl-string 'elmo-string)
303
304 ;; Check if active region exists or not.
305 (if (boundp 'mark-active)
306     (defmacro wl-region-exists-p ()
307       'mark-active)
308   (if (fboundp 'region-exists-p)
309       (defmacro wl-region-exists-p ()
310         (list 'region-exists-p))))
311
312 (if (not (fboundp 'overlays-in))
313     (defun overlays-in (beg end)
314       "Return a list of the overlays that overlap the region BEG ... END.
315 Overlap means that at least one character is contained within the overlay
316 and also contained within the specified region.
317 Empty overlays are included in the result if they are located at BEG
318 or between BEG and END."
319       (let ((ovls (overlay-lists))
320             tmp retval)
321         (if (< end beg)
322             (setq tmp end
323                   end beg
324                   beg tmp))
325         (setq ovls (nconc (car ovls) (cdr ovls)))
326         (while ovls
327           (setq tmp (car ovls)
328                 ovls (cdr ovls))
329           (if (or (and (<= (overlay-start tmp) end)
330                        (>= (overlay-start tmp) beg))
331                   (and (<= (overlay-end tmp) end)
332                        (>= (overlay-end tmp) beg)))
333               (setq retval (cons tmp retval))))
334         retval)))
335
336 (defsubst wl-repeat-string (str times)
337   (let ((loop times)
338         ret-val)
339     (while (> loop 0)
340       (setq ret-val (concat ret-val str))
341       (setq loop (- loop 1)))
342     ret-val))
343
344 (defun wl-list-diff (list1 list2)
345   "Return a list of elements of LIST1 that do not appear in LIST2."
346   (let ((list1 (copy-sequence list1)))
347     (while list2
348       (setq list1 (delq (car list2) list1))
349       (setq list2 (cdr list2)))
350     list1))
351
352 (defun wl-append-assoc-list (item value alist)
353   "make assoc list '((item1 value1-1 value1-2 ...)) (item2 value2-1 ...)))"
354   (let ((entry (assoc item alist)))
355     (if entry
356         (progn
357           (when (not (member value (cdr entry)))
358             (nconc entry (list value)))
359           alist)
360       (append alist
361               (list (list item value))))))
362
363 (defun wl-delete-alist (key alist)
364   "Delete by side effect any entries specified with KEY from ALIST.
365 Return the modified ALIST.  Key comparison is done with `assq'.
366 Write `(setq foo (wl-delete-alist key foo))' to be sure of changing
367 the value of `foo'."
368   (let (entry)
369     (while (setq entry (assq key alist))
370       (setq alist (delq entry alist)))
371     alist))
372
373 (defun wl-delete-associations (keys alist)
374   "Delete by side effect any entries specified with KEYS from ALIST.
375 Return the modified ALIST.  KEYS must be a list of keys for ALIST.
376 Deletion is done with `wl-delete-alist'.
377 Write `(setq foo (wl-delete-associations keys foo))' to be sure of
378 changing the value of `foo'."
379   (while keys
380     (setq alist (wl-delete-alist (car keys) alist))
381     (setq keys (cdr keys)))
382   alist)
383
384 (defun wl-inverse-alist (keys alist)
385   "Inverse ALIST, copying.
386 Return an association list represents the inverse mapping of ALIST,
387 from objects to KEYS.
388 The objects mapped (cdrs of elements of the ALIST) are shared."
389   (let (x y tmp result)
390     (while keys
391       (setq x (car keys))
392       (setq y (cdr (assq x alist)))
393       (if y
394           (if (setq tmp (assoc y result))
395               (setq result (cons (append tmp (list x))
396                                  (delete tmp result)))
397             (setq result (cons (list y x) result))))
398       (setq keys (cdr keys)))
399     result))
400
401 (eval-when-compile
402   (require 'static))
403 (static-unless (fboundp 'pp)
404   (defvar pp-escape-newlines t)
405   (defun pp (object &optional stream)
406     "Output the pretty-printed representation of OBJECT, any Lisp object.
407 Quoting characters are printed when needed to make output that `read'
408 can handle, whenever this is possible.
409 Output stream is STREAM, or value of `standard-output' (which see)."
410     (princ (pp-to-string object) (or stream standard-output)))
411
412   (defun pp-to-string (object)
413     "Return a string containing the pretty-printed representation of OBJECT,
414 any Lisp object.  Quoting characters are used when needed to make output
415 that `read' can handle, whenever this is possible."
416     (save-excursion
417       (set-buffer (generate-new-buffer " pp-to-string"))
418       (unwind-protect
419           (progn
420             (lisp-mode-variables t)
421             (let ((print-escape-newlines pp-escape-newlines))
422               (prin1 object (current-buffer)))
423             (goto-char (point-min))
424             (while (not (eobp))
425               (cond
426                ((looking-at "\\s(\\|#\\s(")
427                 (while (looking-at "\\s(\\|#\\s(")
428                   (forward-char 1)))
429                ((and (looking-at "\\(quote[ \t]+\\)\\([^.)]\\)")
430                      (> (match-beginning 1) 1)
431                      (= ?\( (char-after (1- (match-beginning 1))))
432                      ;; Make sure this is a two-element list.
433                      (save-excursion
434                        (goto-char (match-beginning 2))
435                        (forward-sexp)
436                        ;; Avoid mucking with match-data; does this test work?
437                        (char-equal ?\) (char-after (point)))))
438                 ;; -1 gets the paren preceding the quote as well.
439                 (delete-region (1- (match-beginning 1)) (match-end 1))
440                 (insert "'")
441                 (forward-sexp 1)
442                 (if (looking-at "[ \t]*\)")
443                     (delete-region (match-beginning 0) (match-end 0))
444                   (error "Malformed quote"))
445                 (backward-sexp 1))
446                ((condition-case err-var
447                     (prog1 t (down-list 1))
448                   (error nil))
449                 (backward-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                ((condition-case err-var
457                     (prog1 t (up-list 1))
458                   (error nil))
459                 (while (looking-at "\\s)")
460                   (forward-char 1))
461                 (skip-chars-backward " \t")
462                 (delete-region
463                  (point)
464                  (progn (skip-chars-forward " \t") (point)))
465                 (if (not (char-equal ?' (char-after (1- (point)))))
466                     (insert ?\n)))
467                (t (goto-char (point-max)))))
468             (goto-char (point-min))
469             (indent-sexp)
470             (buffer-string))
471         (kill-buffer (current-buffer))))))
472
473 (defsubst wl-get-date-iso8601 (date)
474   (or (get-text-property 0 'wl-date date)
475       (let* ((d1 (timezone-fix-time date nil nil))
476              (time (format "%04d%02d%02dT%02d%02d%02d"
477                            (aref d1 0) (aref d1 1) (aref d1 2)
478                            (aref d1 3) (aref d1 4) (aref d1 5))))
479         (put-text-property 0 1 'wl-date time date)
480         time)))
481
482 (defun wl-make-date-string ()
483   (let ((s (current-time-string)))
484     (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]"
485                   s)
486     (concat (wl-match-string 1 s) ", "
487             (timezone-make-date-arpa-standard s (current-time-zone)))))
488
489 (defun wl-date-iso8601 (date)
490   "Convert the DATE to YYMMDDTHHMMSS."
491   (condition-case ()
492       (wl-get-date-iso8601 date)
493     (error "")))
494
495 (defun wl-day-number (date)
496   (let ((dat (mapcar '(lambda (s) (and s (string-to-int s)) )
497                      (timezone-parse-date date))))
498     (timezone-absolute-from-gregorian
499      (nth 1 dat) (nth 2 dat) (car dat))))
500
501 (defun wl-url-news (url &rest args)
502   (interactive "sURL: ")
503   (if (string-match "^news:\\(.*\\)$" url)
504       (wl-summary-goto-folder-subr
505        (concat "-" (elmo-match-string 1 url)) nil nil nil t)
506     (message "Not a news: url.")))
507
508 (defun wl-url-nntp (url &rest args)
509   (interactive "sURL: ")
510   (let (folder fld-name server port msg)
511     (if (string-match
512          "^nntp://\\([^:/]*\\):?\\([0-9]*\\)/\\([^/]*\\)/\\([0-9]*\\).*$" url)
513         (progn
514           (if (eq (length (setq fld-name
515                                 (elmo-match-string 3 url))) 0)
516               (setq fld-name nil))
517           (if (eq (length (setq port
518                                 (elmo-match-string 2 url))) 0)
519               (setq port (int-to-string elmo-nntp-default-port)))
520           (if (eq (length (setq server
521                                 (elmo-match-string 1 url))) 0)
522               (setq server elmo-nntp-default-server))
523           (setq folder (concat "-" fld-name "@" server ":" port))
524           (if (eq (length (setq msg
525                                 (elmo-match-string 4 url))) 0)
526               (wl-summary-goto-folder-subr
527                folder nil nil nil t)
528             (wl-summary-goto-folder-subr
529              folder 'update nil nil t)
530             (goto-char (point-min))
531             (re-search-forward (concat "^ *" msg) nil t)
532             (wl-summary-redisplay)))
533       (message "Not a nntp: url."))))
534
535 (defmacro wl-concat-list (list separator)
536   (` (mapconcat 'identity (delete "" (delq nil (, list))) (, separator))))
537
538 (defmacro wl-current-message-buffer ()
539   (` (save-excursion
540        (if (buffer-live-p wl-current-summary-buffer)
541            (set-buffer wl-current-summary-buffer))
542        wl-message-buffer)))
543
544 (defmacro wl-kill-buffers (regexp)
545   (` (mapcar (function
546               (lambda (x)
547                 (if (and (buffer-name x)
548                          (string-match (, regexp) (buffer-name x)))
549                     (and (get-buffer x)
550                          (kill-buffer x)))))
551              (buffer-list))))
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 (defun wl-collect-draft ()
567   (let ((draft-regexp (concat
568                        "^" (regexp-quote
569                             (elmo-localdir-folder-directory-internal
570                              (wl-folder-get-elmo-folder wl-draft-folder)))))
571         result buf)
572     (mapcar
573      (function (lambda (x)
574                  (if (and
575                       (setq buf (with-current-buffer x
576                                   wl-draft-buffer-file-name))
577                       (string-match draft-regexp buf))
578                      (setq result (nconc result (list x))))))
579      (buffer-list))
580     result))
581
582 (static-if (fboundp 'read-directory-name)
583     (defun wl-read-directory-name (prompt dir)
584       (read-directory-name prompt dir dir))
585   (defun wl-read-directory-name (prompt dir)
586     (let ((dir (read-file-name prompt dir)))
587       (unless (file-directory-p dir)
588         (error "%s is not directory" dir))
589       dir)))
590
591 ;; local variable check.
592 (static-if (fboundp 'local-variable-p)
593     (defalias 'wl-local-variable-p 'local-variable-p)
594   (defmacro wl-local-variable-p (symbol &optional buffer)
595     (` (if (assq (, symbol) (buffer-local-variables (, buffer)))
596            t))))
597
598 (defun wl-number-base36 (num len)
599   (if (if (< len 0)
600           (<= num 0)
601         (= len 0))
602       ""
603     (concat (wl-number-base36 (/ num 36) (1- len))
604             (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
605                                   (% num 36))))))
606
607 (defvar wl-unique-id-char nil)
608
609 (defun wl-unique-id ()
610   ;; Don't use microseconds from (current-time), they may be unsupported.
611   ;; Instead we use this randomly inited counter.
612   (setq wl-unique-id-char
613         (% (1+ (or wl-unique-id-char (logand (random t) (1- (lsh 1 20)))))
614            ;; (current-time) returns 16-bit ints,
615            ;; and 2^16*25 just fits into 4 digits i base 36.
616            (* 25 25)))
617   (let ((tm (static-if (fboundp 'current-time)
618                 (current-time)
619               (let* ((cts (split-string (current-time-string) "[ :]"))
620                      (m (cdr (assoc (nth 1 cts)
621                                     '(("Jan" . "01") ("Feb" . "02")
622                                       ("Mar" . "03") ("Apr" . "04")
623                                       ("May" . "05") ("Jun" . "06")
624                                       ("Jul" . "07") ("Aug" . "08")
625                                       ("Sep" . "09") ("Oct" . "10")
626                                       ("Nov" . "11") ("Dec" . "12"))))))
627                 (list (string-to-int (concat (nth 6 cts) m
628                                              (substring (nth 2 cts) 0 1)))
629                       (string-to-int (concat (substring (nth 2 cts) 1)
630                                              (nth 4 cts) (nth 5 cts)
631                                              (nth 6 cts))))))))
632     (concat
633      (if (memq system-type '(ms-dos emx vax-vms))
634          (let ((user (downcase (user-login-name))))
635            (while (string-match "[^a-z0-9_]" user)
636              (aset user (match-beginning 0) ?_))
637            user)
638        (wl-number-base36 (user-uid) -1))
639      (wl-number-base36 (+ (car   tm)
640                           (lsh (% wl-unique-id-char 25) 16)) 4)
641      (wl-number-base36 (+ (nth 1 tm)
642                           (lsh (/ wl-unique-id-char 25) 16)) 4)
643      ;; Append the name of the message interface, because while the
644      ;; generated ID is unique to this newsreader, other newsreaders
645      ;; might otherwise generate the same ID via another algorithm.
646      wl-unique-id-suffix)))
647
648 (defvar wl-message-id-function 'wl-draft-make-message-id-string)
649 (defun wl-draft-make-message-id-string ()
650   "Return Message-ID field value."
651   (concat "<" (wl-unique-id)
652           (let (from user domain)
653             (if (and wl-message-id-use-wl-from
654                      (progn
655                        (setq from (wl-address-header-extract-address wl-from))
656                        (and (string-match "^\\(.*\\)@\\(.*\\)$" from)
657                             (setq user   (match-string 1 from))
658                             (setq domain (match-string 2 from)))))
659                 (format "%%%s@%s>" user domain)
660               (format "@%s>"
661                       (or wl-message-id-domain
662                           (if wl-local-domain
663                               (concat (system-name) "." wl-local-domain)
664                             (system-name))))))))
665
666 ;;; Profile loading.
667 (defvar wl-load-profile-function 'wl-local-load-profile)
668 (defun wl-local-load-profile ()
669   "Load `wl-init-file'."
670   (message "Initializing...")
671   (load wl-init-file 'noerror 'nomessage))
672
673 (defun wl-load-profile ()
674   "Call `wl-load-profile-function' function."
675   (funcall wl-load-profile-function))
676
677 ;;;
678
679 (defmacro wl-count-lines ()
680   (` (save-excursion
681        (beginning-of-line)
682        (count-lines 1 (point)))))
683
684 (defun wl-horizontal-recenter ()
685   "Recenter the current buffer horizontally."
686   (beginning-of-line)
687   (re-search-forward "[[<]" (point-at-eol) t)
688   (if (< (current-column) (/ (window-width) 2))
689       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
690     (let* ((orig (point))
691            (end (window-end (get-buffer-window (current-buffer) t)))
692            (max 0))
693       (when end
694         ;; Find the longest line currently displayed in the window.
695         (goto-char (window-start))
696         (while (and (not (eobp))
697                     (< (point) end))
698           (end-of-line)
699           (setq max (max max (current-column)))
700           (forward-line 1))
701         (goto-char orig)
702         ;; Scroll horizontally to center (sort of) the point.
703         (if (> max (window-width))
704             (set-window-hscroll
705              (get-buffer-window (current-buffer) t)
706              (min (- (current-column) (/ (window-width) 3))
707                   (+ 2 (- max (window-width)))))
708           (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
709         max))))
710
711 ;; Biff
712 (static-cond
713  (wl-on-xemacs
714   (defvar wl-biff-timer-name "wl-biff")
715
716   (defun wl-biff-stop ()
717     (when (get-itimer wl-biff-timer-name)
718       (delete-itimer wl-biff-timer-name)))
719
720   (defun wl-biff-start ()
721     (wl-biff-stop)
722     (when wl-biff-check-folder-list
723       (wl-biff-check-folders)
724       (start-itimer wl-biff-timer-name 'wl-biff-check-folders
725                     wl-biff-check-interval wl-biff-check-interval))))
726
727  ((and (condition-case nil (require 'timer) (error nil));; FSFmacs 19+
728        (fboundp 'timer-activate))
729
730   (defun wl-biff-stop ()
731     (when (get 'wl-biff 'timer)
732       (cancel-timer (get 'wl-biff 'timer))))
733
734   (defun wl-biff-start ()
735     (require 'timer)
736     (when wl-biff-check-folder-list
737       (wl-biff-check-folders)
738       (if (get 'wl-biff 'timer)
739           (timer-activate (get 'wl-biff 'timer))
740         (put 'wl-biff 'timer (run-at-time
741                               (timer-next-integral-multiple-of-time
742                                (current-time) wl-biff-check-interval)
743                               wl-biff-check-interval
744                               'wl-biff-event-handler)))))
745
746   (defun-maybe timer-next-integral-multiple-of-time (time secs)
747     "Yield the next value after TIME that is an integral multiple of SECS.
748 More precisely, the next value, after TIME, that is an integral multiple
749 of SECS seconds since the epoch.  SECS may be a fraction.
750 This function is imported from Emacs 20.7."
751     (let ((time-base (ash 1 16)))
752       (if (fboundp 'atan)
753           ;; Use floating point, taking care to not lose precision.
754           (let* ((float-time-base (float time-base))
755                  (million 1000000.0)
756                  (time-usec (+ (* million
757                                   (+ (* float-time-base (nth 0 time))
758                                      (nth 1 time)))
759                                (nth 2 time)))
760                  (secs-usec (* million secs))
761                  (mod-usec (mod time-usec secs-usec))
762                  (next-usec (+ (- time-usec mod-usec) secs-usec))
763                  (time-base-million (* float-time-base million)))
764             (list (floor next-usec time-base-million)
765                   (floor (mod next-usec time-base-million) million)
766                   (floor (mod next-usec million))))
767         ;; Floating point is not supported.
768         ;; Use integer arithmetic, avoiding overflow if possible.
769         (let* ((mod-sec (mod (+ (* (mod time-base secs)
770                                    (mod (nth 0 time) secs))
771                                 (nth 1 time))
772                              secs))
773                (next-1-sec (+ (- (nth 1 time) mod-sec) secs)))
774           (list (+ (nth 0 time) (floor next-1-sec time-base))
775                 (mod next-1-sec time-base)
776                 0)))))
777
778   (defun wl-biff-event-handler ()
779     ;; PAKURing from FSF:time.el
780     (wl-biff-check-folders)
781     ;; Do redisplay right now, if no input pending.
782     (sit-for 0)
783     (let* ((current (current-time))
784            (timer (get 'wl-biff 'timer))
785            ;; Compute the time when this timer will run again, next.
786            (next-time (timer-relative-time
787                        (list (aref timer 1) (aref timer 2) (aref timer 3))
788                        (* 5 (aref timer 4)) 0)))
789       ;; If the activation time is far in the past,
790       ;; skip executions until we reach a time in the future.
791       ;; This avoids a long pause if Emacs has been suspended for hours.
792       (or (> (nth 0 next-time) (nth 0 current))
793           (and (= (nth 0 next-time) (nth 0 current))
794                (> (nth 1 next-time) (nth 1 current)))
795           (and (= (nth 0 next-time) (nth 0 current))
796                (= (nth 1 next-time) (nth 1 current))
797                (> (nth 2 next-time) (nth 2 current)))
798           (progn
799             (timer-set-time timer (timer-next-integral-multiple-of-time
800                                    current wl-biff-check-interval)
801                             wl-biff-check-interval)
802             (timer-activate timer))))))
803  (t
804   (fset 'wl-biff-stop 'ignore)
805   (fset 'wl-biff-start 'ignore)))
806
807 (defsubst wl-biff-notify (new-mails notify-minibuf)
808   (when (and (not wl-modeline-biff-status) (> new-mails 0))
809     (run-hooks 'wl-biff-notify-hook))
810   (when (and wl-modeline-biff-status (eq new-mails 0))
811     (run-hooks 'wl-biff-unnotify-hook))
812   (setq wl-modeline-biff-status (> new-mails 0))
813   (force-mode-line-update t)
814   (when notify-minibuf
815     (cond ((zerop new-mails) (message "No mail."))
816           ((= 1 new-mails) (message "You have a new mail."))
817           (t (message "You have %d new mails." new-mails)))))
818
819 ;; Internal variable.
820 (defvar wl-biff-check-folders-running nil)
821
822 (defun wl-biff-check-folders ()
823   (interactive)
824   (if wl-biff-check-folders-running
825       (when (interactive-p)
826         (message "Biff process is running."))
827     (setq wl-biff-check-folders-running t)
828     (when (interactive-p)
829       (message "Checking new mails..."))
830     (let ((new-mails 0)
831           (flist (or wl-biff-check-folder-list (list wl-default-folder)))
832           folder)
833       (if (eq (length flist) 1)
834           (wl-biff-check-folder-async (wl-folder-get-elmo-folder
835                                        (car flist) 'biff) (interactive-p))
836         (unwind-protect
837             (while flist
838               (setq folder (wl-folder-get-elmo-folder (car flist))
839                     flist (cdr flist))
840               (when (elmo-folder-plugged-p folder)
841                 (setq new-mails
842                       (+ new-mails
843                          (nth 0 (wl-biff-check-folder folder))))))
844           (setq wl-biff-check-folders-running nil)
845           (wl-biff-notify new-mails (interactive-p)))))))
846
847 (defun wl-biff-check-folder (folder)
848   (if (eq (elmo-folder-type-internal folder) 'pop3)
849       (unless (elmo-pop3-get-session folder 'any-exists)
850         (wl-folder-check-one-entity (elmo-folder-name-internal folder)
851                                     'biff))
852     (wl-folder-check-one-entity (elmo-folder-name-internal folder)
853                                 'biff)))
854
855 (defun wl-biff-check-folder-async-callback (diff data)
856   (if (nth 1 data)
857       (with-current-buffer (nth 1 data)
858         (wl-folder-entity-hashtb-set wl-folder-entity-hashtb
859                                      (nth 0 data)
860                                      (list (nth 0 diff)
861                                            (- (nth 1 diff) (nth 0 diff))
862                                            (nth 2 diff))
863                                      (current-buffer))))
864   (setq wl-folder-info-alist-modified t)
865   (setq wl-biff-check-folders-running nil)
866   (sit-for 0)
867   (wl-biff-notify (car diff) (nth 2 data)))
868
869 (defun wl-biff-check-folder-async (folder notify-minibuf)
870   (when (elmo-folder-plugged-p folder)
871     (elmo-folder-set-biff-internal folder t)
872     (if (and (eq (elmo-folder-type-internal folder) 'imap4)
873              (elmo-folder-use-flag-p folder))
874         ;; Check asynchronously only when IMAP4 and use server diff.
875         (progn
876           (setq elmo-folder-diff-async-callback
877                 'wl-biff-check-folder-async-callback)
878           (setq elmo-folder-diff-async-callback-data
879                 (list (elmo-folder-name-internal folder)
880                       (get-buffer wl-folder-buffer-name)
881                       notify-minibuf))
882           (elmo-folder-diff-async folder))
883       (unwind-protect
884           (wl-biff-notify (car (wl-biff-check-folder folder))
885                           notify-minibuf)
886         (setq wl-biff-check-folders-running nil)))))
887
888 (if (and (fboundp 'regexp-opt)
889          (not (featurep 'xemacs)))
890     (defalias 'wl-regexp-opt 'regexp-opt)
891   (defun wl-regexp-opt (strings &optional paren)
892     "Return a regexp to match a string in STRINGS.
893 Each string should be unique in STRINGS and should not contain any regexps,
894 quoted or not.  If optional PAREN is non-nil, ensure that the returned regexp
895 is enclosed by at least one regexp grouping construct."
896     (let ((open-paren (if paren "\\(" "")) (close-paren (if paren "\\)" "")))
897       (concat open-paren (mapconcat 'regexp-quote strings "\\|")
898               close-paren))))
899
900 (defalias 'wl-expand-newtext 'elmo-expand-newtext)
901
902 (defun wl-region-exists-p ()
903   "Return non-nil if a region exists on current buffer."
904   (static-if (featurep 'xemacs)
905       (and zmacs-regions zmacs-region-active-p)
906     (and transient-mark-mode mark-active)))
907
908 (defvar wl-line-string)
909 (defun wl-line-parse-format (format spec-alist)
910   "Make a formatter from FORMAT and SPEC-ALIST."
911   (let (f spec specs stack)
912     (setq f
913           (with-temp-buffer
914             (insert format)
915             (goto-char (point-min))
916             (while (search-forward "%" nil t)
917               (cond
918                ((looking-at "%")
919                 (goto-char (match-end 0)))
920                ((looking-at "\\(-?\\(0?\\)[0-9]*\\)\\([^0-9]\\)")
921                 (cond
922                  ((string= (match-string 3) "(")
923                   (if (zerop (length (match-string 1)))
924                       (error "No number specification for %%( line format"))
925                   (push (list
926                          (match-beginning 0) ; start
927                          (match-end 0)       ; start-content
928                          (string-to-number
929                           (match-string 1))  ; width
930                          specs) ; specs
931                         stack)
932                   (setq specs nil))
933                  ((string= (match-string 3) ")")
934                   (let ((entry (pop stack))
935                         form)
936                     (unless entry
937                       (error
938                        "No matching %%( parenthesis in summary line format"))
939                     (goto-char (car entry)) ; start
940                     (setq form (buffer-substring (nth 1 entry) ; start-content
941                                                  (- (match-beginning 0) 1)))
942                     (delete-region (car entry) (match-end 0))
943                     (insert "s")
944                     (setq specs
945                           (append
946                            (nth 3 entry)
947                            (list (list 'wl-set-string-width (nth 2 entry)
948                                        (append
949                                         (list 'format form)
950                                         specs)))))))
951                  (t
952                   (setq spec
953                         (if (setq spec (assq (string-to-char (match-string 3))
954                                              spec-alist))
955                             (nth 1 spec)
956                           (match-string 3)))
957                   (unless (string= "" (match-string 1))
958                     (setq spec (list 'wl-set-string-width
959                                      (string-to-number (match-string 1))
960                                      spec
961                                      (unless (string= "" (match-string 2))
962                                        (string-to-char (match-string 2))))))
963                   (replace-match "s" 'fixed)
964                   (setq specs (append specs
965                                       (list
966                                        (list
967                                         'setq 'wl-line-string
968                                         spec)))))))))
969             (buffer-string)))
970     (append (list 'format f) specs)))
971
972 (defmacro wl-line-formatter-setup (formatter format alist)
973   (` (let (byte-compile-warnings)
974        (setq (, formatter)
975              (byte-compile
976               (list 'lambda ()
977                     (wl-line-parse-format (, format) (, alist)))))
978        (when (get-buffer "*Compile-Log*")
979          (bury-buffer "*Compile-Log*"))
980        (when (get-buffer "*Compile-Log-Show*")
981          (bury-buffer "*Compile-Log-Show*")))))
982
983 (require 'product)
984 (product-provide (provide 'wl-util) (require 'wl-version))
985
986 ;;; wl-util.el ends here