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