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