Update x-face section.
[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 (defalias 'wl-parse 'elmo-parse)
61 (make-obsolete 'wl-parse 'elmo-parse)
62
63 (defun wl-delete-duplicates (list &optional all hack-addresses)
64   "Delete duplicate equivalent strings from the LIST.
65 If ALL is t, then if there is more than one occurrence of a string in the LIST,
66  then all occurrences of it are removed instead of just the subsequent ones.
67 If HACK-ADDRESSES is t, then the strings are considered to be mail addresses,
68  and only the address part is compared (so that \"Name <foo>\" and \"foo\"
69  would be considered to be equivalent.)"
70   (let ((hashtable (make-vector 29 0))
71         (new-list nil)
72         sym-string sym)
73     (fillarray hashtable 0)
74     (while list
75       (setq sym-string
76             (if hack-addresses
77                 (wl-address-header-extract-address (car list))
78               (car list))
79             sym-string (or sym-string "-unparseable-garbage-")
80             sym (intern sym-string hashtable))
81       (if (boundp sym)
82           (and all (setcar (symbol-value sym) nil))
83         (setq new-list (cons (car list) new-list))
84         (set sym new-list))
85       (setq list (cdr list)))
86     (delq nil (nreverse new-list))))
87
88 ;; string utils.
89 (defalias 'wl-string-member 'elmo-string-member)
90 (defalias 'wl-string-match-member 'elmo-string-match-member)
91 (defalias 'wl-string-delete-match 'elmo-string-delete-match)
92 (defalias 'wl-string-match-assoc 'elmo-string-match-assoc)
93 (defalias 'wl-string-assoc 'elmo-string-assoc)
94 (defalias 'wl-string-rassoc 'elmo-string-rassoc)
95
96 (defun wl-parse-addresses (string)
97   (if (null string)
98       ()
99     (elmo-set-work-buf
100      ;;(unwind-protect
101      (let (list start s char)
102        (insert string)
103        (goto-char (point-min))
104        (skip-chars-forward "\t\f\n\r ")
105        (setq start (point))
106        (while (not (eobp))
107          (skip-chars-forward "^\"\\,(")
108          (setq char (following-char))
109          (cond ((= char ?\\)
110                 (forward-char 1)
111                 (if (not (eobp))
112                     (forward-char 1)))
113                ((= char ?,)
114                 (setq s (buffer-substring start (point)))
115                 (if (or (null (string-match "^[\t\f\n\r ]+$" s))
116                         (not (string= s "")))
117                     (setq list (cons s list)))
118                 (skip-chars-forward ",\t\f\n\r ")
119                 (setq start (point)))
120                ((= char ?\")
121                 (re-search-forward "[^\\]\"" nil 0))
122                ((= char ?\()
123                 (let ((parens 1))
124                   (forward-char 1)
125                   (while (and (not (eobp)) (not (zerop parens)))
126                     (re-search-forward "[()]" nil 0)
127                     (cond ((or (eobp)
128                                (= (char-after (- (point) 2)) ?\\)))
129                           ((= (preceding-char) ?\()
130                            (setq parens (1+ parens)))
131                           (t
132                            (setq parens (1- parens)))))))))
133        (setq s (buffer-substring start (point)))
134        (if (and (null (string-match "^[\t\f\n\r ]+$" s))
135                 (not (string= s "")))
136            (setq list (cons s list)))
137        (nreverse list)) ; jwz: fixed order
138      )))
139
140 (defun wl-append-element (list element)
141   (if element
142       (append list (list element))
143     list))
144
145 (defmacro wl-push (v l)
146   "Insert V at the head of the list stored in L."
147   (list 'setq l (list 'cons v l)))
148
149 (defmacro wl-pop (l)
150   "Remove the head of the list stored in L."
151   (list 'car (list 'prog1 l (list 'setq l (list 'cdr l)))))
152
153 (defun wl-ask-folder (func mes-string)
154   (let* (key keve
155              (cmd (if (featurep 'xemacs)
156                       (event-to-character last-command-event)
157                     (string-to-char (format "%s" (this-command-keys))))))
158     (message mes-string)
159     (setq key (car (setq keve (wl-read-event-char))))
160     (if (or (equal key ?\ )
161             (and cmd
162                  (equal key cmd)))
163         (progn
164           (message "")
165           (funcall func))
166       (wl-push (cdr keve) unread-command-events))))
167
168 ;(defalias 'wl-make-hash 'elmo-make-hash)
169 ;;(make-obsolete 'wl-make-hash 'elmo-make-hash)
170
171 ;;(defalias 'wl-get-hash-val 'elmo-get-hash-val)
172 ;;(make-obsolete 'wl-get-hash-val 'elmo-get-hash-val)
173
174 ;;(defalias 'wl-set-hash-val 'elmo-set-hash-val)
175 ;;(make-obsolete 'wl-set-hash-val 'elmo-set-hash-val)
176
177 (defsubst wl-set-string-width (width string)
178   (elmo-set-work-buf
179    (elmo-set-buffer-multibyte default-enable-multibyte-characters)
180    (insert string)
181    (if (> (current-column) width)
182        (if (> (move-to-column width) width)
183            (progn
184              (condition-case nil ; ignore error
185                  (backward-char 1)
186                (error))
187              (concat (buffer-substring (point-min) (point)) " "))
188          (buffer-substring (point-min) (point)))
189      (if (= (current-column) width)
190          string
191        (concat string
192                (format (format "%%%ds"
193                                (- width (current-column)))
194                        " "))))))
195
196 (defun wl-display-bytes (num)
197   (let (result remain)
198     (cond
199      ((> (setq result (/ num 1000000)) 0)
200       (setq remain (% num 1000000))
201       (if (> remain 400000)
202           (setq result (+ 1 result)))
203       (format "%dM" result))
204      ((> (setq result (/ num 1000)) 0)
205       (setq remain (% num 1000))
206       (if (> remain 400)
207           (setq result (+ 1 result)))
208       (format "%dK" result))
209      (t (format "%dB" result)))))
210
211 (defun wl-mode-line-buffer-identification (&optional id)
212   (let ((priorities '(biff plug title)))
213     (let ((items (reverse wl-mode-line-display-priority-list))
214           item)
215       (while items
216         (setq item (car items)
217               items (cdr items))
218         (unless (memq item '(biff plug))
219           (setq item 'title))
220         (setq priorities (cons item (delq item priorities)))))
221     (let (priority result)
222       (while priorities
223         (setq priority (car priorities)
224               priorities (cdr priorities))
225         (cond
226          ((eq 'biff priority)
227           (when wl-biff-check-folder-list
228             (setq result (append result '((wl-modeline-biff-status
229                                            wl-modeline-biff-state-on
230                                            wl-modeline-biff-state-off))))))
231          ((eq 'plug priority)
232           (when wl-show-plug-status-on-modeline
233             (setq result (append result '((wl-modeline-plug-status
234                                            wl-modeline-plug-state-on
235                                            wl-modeline-plug-state-off))))))
236          (t
237           (setq result (append result (or id '("Wanderlust: %12b")))))))
238       (prog1
239           (setq mode-line-buffer-identification (if (stringp (car result))
240                                                     result
241                                                   (cons "" result)))
242         (force-mode-line-update t)))))
243
244 (defalias 'wl-display-error 'elmo-display-error)
245 (make-obsolete 'wl-display-error 'elmo-display-error)
246
247 (defun wl-get-assoc-list-value (assoc-list folder &optional match)
248   (catch 'found
249     (let ((alist assoc-list)
250           value pair)
251       (while alist
252         (setq pair (car alist))
253         (if (string-match (car pair) folder)
254             (cond ((eq match 'all)
255                    (setq value (append value (list (cdr pair)))))
256                   ((eq match 'all-list)
257                    (setq value (append value (cdr pair))))
258                   ((not match)
259                    (throw 'found (cdr pair)))))
260         (setq alist (cdr alist)))
261       value)))
262
263 (defmacro wl-match-string (pos string)
264   "Substring POSth matched STRING."
265   (` (substring (, string) (match-beginning (, pos)) (match-end (, pos)))))
266
267 (defmacro wl-match-buffer (pos)
268   "Substring POSth matched from the current buffer."
269   (` (buffer-substring-no-properties
270       (match-beginning (, pos)) (match-end (, pos)))))
271
272 (put 'wl-as-coding-system 'lisp-indent-function 1)
273 (put 'wl-as-mime-charset 'lisp-indent-function 1)
274
275 (eval-and-compile
276   (if wl-on-mule3
277       (defmacro wl-as-coding-system (coding-system &rest body)
278         (` (let ((coding-system-for-read (, coding-system))
279                  (coding-system-for-write (, coding-system)))
280              (,@ body))))
281     (if wl-on-mule
282         (defmacro wl-as-coding-system (coding-system &rest body)
283           (` (let ((file-coding-system-for-read (, coding-system))
284                    (file-coding-system (, coding-system)))
285                (,@ body)))))))
286
287 (defmacro wl-as-mime-charset (mime-charset &rest body)
288   (` (wl-as-coding-system (mime-charset-to-coding-system (, mime-charset))
289        (,@ body))))
290
291 (defalias 'wl-string 'elmo-string)
292 (make-obsolete 'wl-string 'elmo-string)
293
294 ;; Check if active region exists or not.
295 (if (boundp 'mark-active)
296     (defmacro wl-region-exists-p ()
297       'mark-active)
298   (if (fboundp 'region-exists-p)
299       (defmacro wl-region-exists-p ()
300         (list 'region-exists-p))))
301
302 (if (not (fboundp 'overlays-in))
303     (defun overlays-in (beg end)
304       "Return a list of the overlays that overlap the region BEG ... END.
305 Overlap means that at least one character is contained within the overlay
306 and also contained within the specified region.
307 Empty overlays are included in the result if they are located at BEG
308 or between BEG and END."
309       (let ((ovls (overlay-lists))
310             tmp retval)
311         (if (< end beg)
312             (setq tmp end
313                   end beg
314                   beg tmp))
315         (setq ovls (nconc (car ovls) (cdr ovls)))
316         (while ovls
317           (setq tmp (car ovls)
318                 ovls (cdr ovls))
319           (if (or (and (<= (overlay-start tmp) end)
320                        (>= (overlay-start tmp) beg))
321                   (and (<= (overlay-end tmp) end)
322                        (>= (overlay-end tmp) beg)))
323               (setq retval (cons tmp retval))))
324         retval)))
325
326 (defsubst wl-repeat-string (str times)
327   (let ((loop times)
328         ret-val)
329     (while (> loop 0)
330       (setq ret-val (concat ret-val str))
331       (setq loop (- loop 1)))
332     ret-val))
333
334 (defun wl-list-diff (list1 list2)
335   "Return a list of elements of LIST1 that do not appear in LIST2."
336   (let ((list1 (copy-sequence list1)))
337     (while list2
338       (setq list1 (delq (car list2) list1))
339       (setq list2 (cdr list2)))
340     list1))
341
342 (defun wl-append-assoc-list (item value alist)
343   "make assoc list '((item1 value1-1 value1-2 ...)) (item2 value2-1 ...)))"
344   (let ((entry (assoc item alist)))
345     (if entry
346         (progn
347           (when (not (member value (cdr entry)))
348             (nconc entry (list value)))
349           alist)
350       (append alist
351               (list (list item value))))))
352
353 (defun wl-delete-alist (key alist)
354   "Delete by side effect any entries specified with KEY from ALIST.
355 Return the modified ALIST.  Key comparison is done with `assq'.
356 Write `(setq foo (wl-delete-alist key foo))' to be sure of changing
357 the value of `foo'."
358   (let (entry)
359     (while (setq entry (assq key alist))
360       (setq alist (delq entry alist)))
361     alist))
362
363 (defun wl-delete-associations (keys alist)
364   "Delete by side effect any entries specified with KEYS from ALIST.
365 Return the modified ALIST.  KEYS must be a list of keys for ALIST.
366 Deletion is done with `wl-delete-alist'.
367 Write `(setq foo (wl-delete-associations keys foo))' to be sure of
368 changing the value of `foo'."
369   (while keys
370     (setq alist (wl-delete-alist (car keys) alist))
371     (setq keys (cdr keys)))
372   alist)
373
374 (defun wl-inverse-alist (keys alist)
375   "Inverse ALIST, copying.
376 Return an association list represents the inverse mapping of ALIST,
377 from objects to KEYS.
378 The objects mapped (cdrs of elements of the ALIST) are shared."
379   (let (x y tmp result)
380     (while keys
381       (setq x (car keys))
382       (setq y (cdr (assq x alist)))
383       (if y
384           (if (setq tmp (assoc y result))
385               (setq result (cons (append tmp (list x))
386                                  (delete tmp result)))
387             (setq result (cons (list y x) result))))
388       (setq keys (cdr keys)))
389     result))
390
391 (eval-when-compile
392   (require 'static))
393 (static-unless (fboundp 'pp)
394   (defvar pp-escape-newlines t)
395   (defun pp (object &optional stream)
396     "Output the pretty-printed representation of OBJECT, any Lisp object.
397 Quoting characters are printed when needed to make output that `read'
398 can handle, whenever this is possible.
399 Output stream is STREAM, or value of `standard-output' (which see)."
400     (princ (pp-to-string object) (or stream standard-output)))
401
402   (defun pp-to-string (object)
403     "Return a string containing the pretty-printed representation of OBJECT,
404 any Lisp object.  Quoting characters are used when needed to make output
405 that `read' can handle, whenever this is possible."
406     (save-excursion
407       (set-buffer (generate-new-buffer " pp-to-string"))
408       (unwind-protect
409           (progn
410             (lisp-mode-variables t)
411             (let ((print-escape-newlines pp-escape-newlines))
412               (prin1 object (current-buffer)))
413             (goto-char (point-min))
414             (while (not (eobp))
415               (cond
416                ((looking-at "\\s(\\|#\\s(")
417                 (while (looking-at "\\s(\\|#\\s(")
418                   (forward-char 1)))
419                ((and (looking-at "\\(quote[ \t]+\\)\\([^.)]\\)")
420                      (> (match-beginning 1) 1)
421                      (= ?\( (char-after (1- (match-beginning 1))))
422                      ;; Make sure this is a two-element list.
423                      (save-excursion
424                        (goto-char (match-beginning 2))
425                        (forward-sexp)
426                        ;; Avoid mucking with match-data; does this test work?
427                        (char-equal ?\) (char-after (point)))))
428                 ;; -1 gets the paren preceding the quote as well.
429                 (delete-region (1- (match-beginning 1)) (match-end 1))
430                 (insert "'")
431                 (forward-sexp 1)
432                 (if (looking-at "[ \t]*\)")
433                     (delete-region (match-beginning 0) (match-end 0))
434                   (error "Malformed quote"))
435                 (backward-sexp 1))
436                ((condition-case err-var
437                     (prog1 t (down-list 1))
438                   (error nil))
439                 (backward-char 1)
440                 (skip-chars-backward " \t")
441                 (delete-region
442                  (point)
443                  (progn (skip-chars-forward " \t") (point)))
444                 (if (not (char-equal ?' (char-after (1- (point)))))
445                     (insert ?\n)))
446                ((condition-case err-var
447                     (prog1 t (up-list 1))
448                   (error nil))
449                 (while (looking-at "\\s)")
450                   (forward-char 1))
451                 (skip-chars-backward " \t")
452                 (delete-region
453                  (point)
454                  (progn (skip-chars-forward " \t") (point)))
455                 (if (not (char-equal ?' (char-after (1- (point)))))
456                     (insert ?\n)))
457                (t (goto-char (point-max)))))
458             (goto-char (point-min))
459             (indent-sexp)
460             (buffer-string))
461         (kill-buffer (current-buffer))))))
462
463 (defsubst wl-get-date-iso8601 (date)
464   (or (get-text-property 0 'wl-date date)
465       (let* ((d1 (timezone-fix-time date nil nil))
466              (time (format "%04d%02d%02dT%02d%02d%02d"
467                            (aref d1 0) (aref d1 1) (aref d1 2)
468                            (aref d1 3) (aref d1 4) (aref d1 5))))
469         (put-text-property 0 1 'wl-date time date)
470         time)))
471
472 (defun wl-make-date-string ()
473   (let ((s (current-time-string)))
474     (string-match "\\`\\([A-Z][a-z][a-z]\\) +[A-Z][a-z][a-z] +[0-9][0-9]? *[0-9][0-9]?:[0-9][0-9]:[0-9][0-9] *[0-9]?[0-9]?[0-9][0-9]"
475                   s)
476     (concat (wl-match-string 1 s) ", "
477             (timezone-make-date-arpa-standard s (current-time-zone)))))
478
479 (defun wl-date-iso8601 (date)
480   "Convert the DATE to YYMMDDTHHMMSS."
481   (condition-case ()
482       (wl-get-date-iso8601 date)
483     (error "")))
484
485 (defun wl-day-number (date)
486   (let ((dat (mapcar '(lambda (s) (and s (string-to-int s)) )
487                      (timezone-parse-date date))))
488     (timezone-absolute-from-gregorian
489      (nth 1 dat) (nth 2 dat) (car dat))))
490
491 (defun wl-url-news (url &rest args)
492   (interactive "sURL: ")
493   (if (string-match "^news:\\(.*\\)$" url)
494       (wl-summary-goto-folder-subr
495        (concat "-" (elmo-match-string 1 url)) nil nil nil t)
496     (message "Not a news: url.")))
497
498 (defun wl-url-nntp (url &rest args)
499   (interactive "sURL: ")
500   (let (folder fld-name server port msg)
501     (if (string-match
502          "^nntp://\\([^:/]*\\):?\\([0-9]*\\)/\\([^/]*\\)/\\([0-9]*\\).*$" url)
503         (progn
504           (if (eq (length (setq fld-name
505                                 (elmo-match-string 3 url))) 0)
506               (setq fld-name nil))
507           (if (eq (length (setq port
508                                 (elmo-match-string 2 url))) 0)
509               (setq port (int-to-string elmo-nntp-default-port)))
510           (if (eq (length (setq server
511                                 (elmo-match-string 1 url))) 0)
512               (setq server elmo-nntp-default-server))
513           (setq folder (concat "-" fld-name "@" server ":" port))
514           (if (eq (length (setq msg
515                                 (elmo-match-string 4 url))) 0)
516               (wl-summary-goto-folder-subr
517                folder nil nil nil t)
518             (wl-summary-goto-folder-subr
519              folder 'update nil nil t)
520             (goto-char (point-min))
521             (re-search-forward (concat "^ *" msg) nil t)
522             (wl-summary-redisplay)))
523       (message "Not a nntp: url."))))
524
525 (defmacro wl-concat-list (list separator)
526   (` (mapconcat 'identity (delete "" (delq nil (, list))) (, separator))))
527
528 (defmacro wl-current-message-buffer ()
529   (` (save-excursion
530        (if (buffer-live-p wl-current-summary-buffer)
531            (set-buffer wl-current-summary-buffer))
532        wl-message-buffer)))
533
534 (defmacro wl-kill-buffers (regexp)
535   (` (mapcar (function
536               (lambda (x)
537                 (if (and (buffer-name x)
538                          (string-match (, regexp) (buffer-name x)))
539                     (and (get-buffer x)
540                          (kill-buffer x)))))
541              (buffer-list))))
542
543 (defun wl-sendlog-time ()
544   (static-if (fboundp 'format-time-string)
545       (format-time-string "%Y/%m/%d %T")
546     (let ((date (current-time-string)))
547       (format "%s/%02d/%02d %s"
548               (substring date -4)
549               (cdr (assoc (upcase (substring date 4 7))
550                           timezone-months-assoc))
551               (string-to-int (substring date 8 10))
552               (substring date 11 19)))))
553
554 (defun wl-collect-summary ()
555   (let (result)
556     (mapcar
557      (function (lambda (x)
558                  (if (and (string-match "^Summary"
559                                         (buffer-name x))
560                           (save-excursion
561                             (set-buffer x)
562                             (equal major-mode 'wl-summary-mode)))
563                      (setq result (nconc result (list x))))))
564      (buffer-list))
565     result))
566
567 (static-if (fboundp 'read-directory-name)
568     (defalias 'wl-read-directory-name 'read-directory-name)
569   (defun wl-read-directory-name (prompt dir)
570     (let ((dir (read-file-name prompt dir)))
571       (unless (file-directory-p dir)
572         (error "%s is not directory" dir))
573       dir)))
574
575 ;; local variable check.
576 (static-if (fboundp 'local-variable-p)
577     (defalias 'wl-local-variable-p 'local-variable-p)
578   (defmacro wl-local-variable-p (symbol &optional buffer)
579     (` (if (assq (, symbol) (buffer-local-variables (, buffer)))
580            t))))
581
582 (defun wl-number-base36 (num len)
583   (if (if (< len 0)
584           (<= num 0)
585         (= len 0))
586       ""
587     (concat (wl-number-base36 (/ num 36) (1- len))
588             (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
589                                   (% num 36))))))
590
591 (defvar wl-unique-id-char nil)
592
593 (defun wl-unique-id ()
594   ;; Don't use microseconds from (current-time), they may be unsupported.
595   ;; Instead we use this randomly inited counter.
596   (setq wl-unique-id-char
597         (% (1+ (or wl-unique-id-char (logand (random t) (1- (lsh 1 20)))))
598            ;; (current-time) returns 16-bit ints,
599            ;; and 2^16*25 just fits into 4 digits i base 36.
600            (* 25 25)))
601   (let ((tm (static-if (fboundp 'current-time)
602                 (current-time)
603               (let* ((cts (split-string (current-time-string) "[ :]"))
604                      (m (cdr (assoc (nth 1 cts)
605                                     '(("Jan" . "01") ("Feb" . "02")
606                                       ("Mar" . "03") ("Apr" . "04")
607                                       ("May" . "05") ("Jun" . "06")
608                                       ("Jul" . "07") ("Aug" . "08")
609                                       ("Sep" . "09") ("Oct" . "10")
610                                       ("Nov" . "11") ("Dec" . "12"))))))
611                 (list (string-to-int (concat (nth 6 cts) m
612                                              (substring (nth 2 cts) 0 1)))
613                       (string-to-int (concat (substring (nth 2 cts) 1)
614                                              (nth 4 cts) (nth 5 cts)
615                                              (nth 6 cts))))))))
616     (concat
617      (if (memq system-type '(ms-dos emx vax-vms))
618          (let ((user (downcase (user-login-name))))
619            (while (string-match "[^a-z0-9_]" user)
620              (aset user (match-beginning 0) ?_))
621            user)
622        (wl-number-base36 (user-uid) -1))
623      (wl-number-base36 (+ (car   tm)
624                           (lsh (% wl-unique-id-char 25) 16)) 4)
625      (wl-number-base36 (+ (nth 1 tm)
626                           (lsh (/ wl-unique-id-char 25) 16)) 4)
627      ;; Append the name of the message interface, because while the
628      ;; generated ID is unique to this newsreader, other newsreaders
629      ;; might otherwise generate the same ID via another algorithm.
630      ".wl")))
631
632 (defun wl-draft-make-message-id-string ()
633   "Return Message-ID field value."
634   (concat "<" (wl-unique-id) "@"
635           (or wl-message-id-domain
636               (if wl-local-domain
637                   (concat (system-name) "." wl-local-domain)
638                 (system-name)))
639           ">"))
640
641 ;;; Profile loading.
642 (defvar wl-load-profile-function 'wl-local-load-profile)
643 (defun wl-local-load-profile ()
644   "Load `wl-init-file'."
645   (message "Initializing ...")
646   (load wl-init-file 'noerror 'nomessage))
647
648 (defun wl-load-profile ()
649   "Call `wl-load-profile-function' function."
650   (funcall wl-load-profile-function))
651
652 ;;;
653
654 (defmacro wl-count-lines ()
655   (` (save-excursion
656        (beginning-of-line)
657        (count-lines 1 (point)))))
658
659 (defun wl-horizontal-recenter ()
660   "Recenter the current buffer horizontally."
661   (beginning-of-line)
662   (re-search-forward "[[<]" (point-at-eol) t)
663   (if (< (current-column) (/ (window-width) 2))
664       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
665     (let* ((orig (point))
666            (end (window-end (get-buffer-window (current-buffer) t)))
667            (max 0))
668       (when end
669         ;; Find the longest line currently displayed in the window.
670         (goto-char (window-start))
671         (while (and (not (eobp))
672                     (< (point) end))
673           (end-of-line)
674           (setq max (max max (current-column)))
675           (forward-line 1))
676         (goto-char orig)
677         ;; Scroll horizontally to center (sort of) the point.
678         (if (> max (window-width))
679             (set-window-hscroll
680              (get-buffer-window (current-buffer) t)
681              (min (- (current-column) (/ (window-width) 3))
682                   (+ 2 (- max (window-width)))))
683           (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
684         max))))
685
686 ;; Biff
687 (static-cond
688  (wl-on-xemacs
689   (defvar wl-biff-timer-name "wl-biff")
690
691   (defun wl-biff-stop ()
692     (when (get-itimer wl-biff-timer-name)
693       (delete-itimer wl-biff-timer-name)))
694
695   (defun wl-biff-start ()
696     (wl-biff-stop)
697     (when wl-biff-check-folder-list
698       (wl-biff-check-folders)
699       (start-itimer wl-biff-timer-name 'wl-biff-check-folders
700                     wl-biff-check-interval wl-biff-check-interval))))
701
702  ((and (condition-case nil (require 'timer) (error nil));; FSFmacs 19+
703        (fboundp 'timer-activate))
704
705   (defun wl-biff-stop ()
706     (when (get 'wl-biff 'timer)
707       (cancel-timer (get 'wl-biff 'timer))))
708
709   (defun wl-biff-start ()
710     (require 'timer)
711     (when wl-biff-check-folder-list
712       (wl-biff-check-folders)
713       (if (get 'wl-biff 'timer)
714           (timer-activate (get 'wl-biff 'timer))
715         (put 'wl-biff 'timer (run-at-time
716                               (timer-next-integral-multiple-of-time
717                                (current-time) wl-biff-check-interval)
718                               wl-biff-check-interval
719                               'wl-biff-event-handler)))))
720
721   (defun-maybe timer-next-integral-multiple-of-time (time secs)
722     "Yield the next value after TIME that is an integral multiple of SECS.
723 More precisely, the next value, after TIME, that is an integral multiple
724 of SECS seconds since the epoch.  SECS may be a fraction.
725 This function is imported from Emacs 20.7."
726     (let ((time-base (ash 1 16)))
727       (if (fboundp 'atan)
728           ;; Use floating point, taking care to not lose precision.
729           (let* ((float-time-base (float time-base))
730                  (million 1000000.0)
731                  (time-usec (+ (* million
732                                   (+ (* float-time-base (nth 0 time))
733                                      (nth 1 time)))
734                                (nth 2 time)))
735                  (secs-usec (* million secs))
736                  (mod-usec (mod time-usec secs-usec))
737                  (next-usec (+ (- time-usec mod-usec) secs-usec))
738                  (time-base-million (* float-time-base million)))
739             (list (floor next-usec time-base-million)
740                   (floor (mod next-usec time-base-million) million)
741                   (floor (mod next-usec million))))
742         ;; Floating point is not supported.
743         ;; Use integer arithmetic, avoiding overflow if possible.
744         (let* ((mod-sec (mod (+ (* (mod time-base secs)
745                                    (mod (nth 0 time) secs))
746                                 (nth 1 time))
747                              secs))
748                (next-1-sec (+ (- (nth 1 time) mod-sec) secs)))
749           (list (+ (nth 0 time) (floor next-1-sec time-base))
750                 (mod next-1-sec time-base)
751                 0)))))
752
753   (defun wl-biff-event-handler ()
754     ;; PAKURing from FSF:time.el
755     (wl-biff-check-folders)
756     ;; Do redisplay right now, if no input pending.
757     (sit-for 0)
758     (let* ((current (current-time))
759            (timer (get 'wl-biff 'timer))
760            ;; Compute the time when this timer will run again, next.
761            (next-time (timer-relative-time
762                        (list (aref timer 1) (aref timer 2) (aref timer 3))
763                        (* 5 (aref timer 4)) 0)))
764       ;; If the activation time is far in the past,
765       ;; skip executions until we reach a time in the future.
766       ;; This avoids a long pause if Emacs has been suspended for hours.
767       (or (> (nth 0 next-time) (nth 0 current))
768           (and (= (nth 0 next-time) (nth 0 current))
769                (> (nth 1 next-time) (nth 1 current)))
770           (and (= (nth 0 next-time) (nth 0 current))
771                (= (nth 1 next-time) (nth 1 current))
772                (> (nth 2 next-time) (nth 2 current)))
773           (progn
774             (timer-set-time timer (timer-next-integral-multiple-of-time
775                                    current wl-biff-check-interval)
776                             wl-biff-check-interval)
777             (timer-activate timer))))))
778  (t
779   (fset 'wl-biff-stop 'ignore)
780   (fset 'wl-biff-start 'ignore)))
781
782 (defsubst wl-biff-notify (new-mails notify-minibuf)
783   (when (and (not wl-modeline-biff-status) (> new-mails 0))
784     (run-hooks 'wl-biff-notify-hook))
785   (when (and wl-modeline-biff-status (eq new-mails 0))
786     (run-hooks 'wl-biff-unnotify-hook))
787   (setq wl-modeline-biff-status (> new-mails 0))
788   (force-mode-line-update t)
789   (when notify-minibuf
790     (cond ((zerop new-mails) (message "No mail."))
791           ((= 1 new-mails) (message "You have a new mail."))
792           (t (message "You have %d new mails." new-mails)))))
793
794 ;; Internal variable.
795 (defvar wl-biff-check-folders-running nil)
796
797 (defun wl-biff-check-folders ()
798   (interactive)
799   (if wl-biff-check-folders-running
800       (when (interactive-p)
801         (message "Biff process is running."))
802     (setq wl-biff-check-folders-running t)
803     (when (interactive-p)
804       (message "Checking new mails..."))
805     (let ((new-mails 0)
806           (flist (or wl-biff-check-folder-list (list wl-default-folder)))
807           folder)
808       (if (eq (length flist) 1)
809           (wl-biff-check-folder-async (wl-folder-get-elmo-folder
810                                        (car flist)) (interactive-p))
811         (unwind-protect
812             (while flist
813               (setq folder (wl-folder-get-elmo-folder (car flist))
814                     flist (cdr flist))
815               (when (elmo-folder-plugged-p folder)
816                 (setq new-mails
817                       (+ new-mails
818                          (nth 0 (wl-biff-check-folder folder))))))
819           (setq wl-biff-check-folders-running nil)
820           (wl-biff-notify new-mails (interactive-p)))))))
821
822 (defun wl-biff-check-folder (folder)
823   (if (eq (elmo-folder-type-internal folder) 'pop3)
824       (unless (elmo-pop3-get-session folder 'if-exists)
825         ;; Currently no main pop3 process.
826         (let ((elmo-network-session-name-prefix "BIFF-"))
827           (wl-folder-check-one-entity
828            (elmo-folder-name-internal folder))))
829     (let ((elmo-network-session-name-prefix "BIFF-"))
830       (wl-folder-check-one-entity (elmo-folder-name-internal folder)))))
831
832 (defun wl-biff-check-folder-async-callback (diff data)
833   (if (nth 1 data)
834       (with-current-buffer (nth 1 data)
835         (wl-folder-entity-hashtb-set wl-folder-entity-hashtb
836                                      (nth 0 data)
837                                      (list (car diff) 0 (cdr diff))
838                                      (current-buffer))))
839   (setq wl-folder-info-alist-modified t)
840   (setq wl-biff-check-folders-running nil)
841   (sit-for 0)
842   (wl-biff-notify (car diff) (nth 2 data)))
843
844 (defun wl-biff-check-folder-async (folder notify-minibuf)
845   (when (elmo-folder-plugged-p folder)
846     (if (and (eq (elmo-folder-type-internal folder) 'imap4)
847              (elmo-folder-use-flag-p folder))
848         ;; Check asynchronously only when IMAP4 and use server diff.
849         (progn
850           (setq elmo-folder-diff-async-callback
851                 'wl-biff-check-folder-async-callback)
852           (setq elmo-folder-diff-async-callback-data
853                 (list (elmo-folder-name-internal folder)
854                       (get-buffer wl-folder-buffer-name)
855                       notify-minibuf))
856           (let ((elmo-network-session-name-prefix "BIFF-"))
857             (elmo-folder-diff-async folder)))
858       (unwind-protect
859           (wl-biff-notify (car (wl-biff-check-folder folder))
860                           notify-minibuf)
861         (setq wl-biff-check-folders-running nil)))))
862
863 (if (and (fboundp 'regexp-opt)
864          (not (featurep 'xemacs)))
865     (defalias 'wl-regexp-opt 'regexp-opt)
866   (defun wl-regexp-opt (strings &optional paren)
867     "Return a regexp to match a string in STRINGS.
868 Each string should be unique in STRINGS and should not contain any regexps,
869 quoted or not.  If optional PAREN is non-nil, ensure that the returned regexp
870 is enclosed by at least one regexp grouping construct."
871     (let ((open-paren (if paren "\\(" "")) (close-paren (if paren "\\)" "")))
872       (concat open-paren (mapconcat 'regexp-quote strings "\\|")
873               close-paren))))
874
875 (defun wl-expand-newtext (newtext original)
876   (let ((len (length newtext))
877         (pos 0)
878         c expanded beg N did-expand)
879     (while (< pos len)
880       (setq beg pos)
881       (while (and (< pos len)
882                   (not (= (aref newtext pos) ?\\)))
883         (setq pos (1+ pos)))
884       (unless (= beg pos)
885         (push (substring newtext beg pos) expanded))
886       (when (< pos len)
887         ;; We hit a \; expand it.
888         (setq did-expand t
889               pos (1+ pos)
890               c (aref newtext pos))
891         (if (not (or (= c ?\&)
892                      (and (>= c ?1)
893                           (<= c ?9))))
894             ;; \ followed by some character we don't expand.
895             (push (char-to-string c) expanded)
896           ;; \& or \N
897           (if (= c ?\&)
898               (setq N 0)
899             (setq N (- c ?0)))
900           (when (match-beginning N)
901             (push (substring original (match-beginning N) (match-end N))
902                   expanded))))
903       (setq pos (1+ pos)))
904     (if did-expand
905         (apply (function concat) (nreverse expanded))
906       newtext)))
907
908 (require 'product)
909 (product-provide (provide 'wl-util) (require 'wl-version))
910
911 ;;; wl-util.el ends here