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