(wl-message-overload-functions): Do nothing if `current-local-map' is not
[elisp/wanderlust.git] / wl / wl-address.el
1 ;;; wl-address.el -- Tiny address management for Wanderlust.
2
3 ;; Copyright 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
4
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
7
8 ;; This file is part of Wanderlust (Yet Another Message Interface on Emacsen).
9
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14 ;;
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19 ;;
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24 ;;
25
26 ;;; Commentary:
27 ;; 
28
29 ;;; Code:
30 ;; 
31
32 (require 'wl-util)
33 (require 'wl-vars)
34 (require 'std11)
35
36 (defvar wl-address-complete-header-regexp "^\\(To\\|From\\|Cc\\|Bcc\\|Mail-Followup-To\\|Reply-To\\|Return-Receipt-To\\):")
37 (defvar wl-newsgroups-complete-header-regexp "^\\(Newsgroups\\|Followup-To\\):")
38 (defvar wl-folder-complete-header-regexp "^\\(FCC\\):")
39 (defvar wl-address-list nil)
40 (defvar wl-address-completion-list nil)
41 (defvar wl-address-petname-hash nil)
42
43 (defvar wl-address-ldap-search-hash nil)
44
45 (eval-when-compile (require 'pldap))
46
47 (defvar wl-ldap-alias-dn-level nil
48 "Level of dn data to make alias postfix.
49 Valid value is nit, t, 1 or larget integer.
50
51 If this value nil, minimum alias postfix is made depends on uniqness
52 with other candidates.  In this implementation, it's same to 1.  If t,
53 always append all dn data.  If number, always append spcified level of
54 data but maybe appended more uniqness.  If invalid value, treat as
55 nil.
56
57 For example, following dn data is exsist, alias of each level is shown
58 bellow.
59
60 Match: Goto
61 dn: CN=Shun-ichi GOTO,OU=Mew,OU=Emacs,OU=Lisper,O=Programmers Inc.
62   nil => Goto/Shun-ichi_GOTO
63     1 => Goto/Shun-ichi_GOTO
64     2 => Goto/Shun-ichi_GOTO/Mew
65     3 => Goto/Shun-ichi_GOTO/Mew/Emacs
66     4 => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper
67     5 => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper/Programmers_Inc_
68     6 => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper/Programmers_Inc_
69     t => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper/Programmers_Inc_
70
71 If level 3 is required for uniqness with other candidates,
72   nil => Goto/Shun-ichi_GOTO/Mew/Emacs    ... appended more
73     1 => Goto/Shun-ichi_GOTO/Mew/Emacs    ... appended more
74     2 => Goto/Shun-ichi_GOTO/Mew/Emacs    ... appended more
75     3 => Goto/Shun-ichi_GOTO/Mew/Emacs
76     4 => Goto/Shun-ichi_GOTO/Mew/Emacs/Lisper
77     (so on...)")
78
79 (defconst wl-ldap-alias-sep "/")
80
81 (defconst wl-ldap-search-attribute-type-list
82   '("sn" "cn" "mail"))
83
84 (defun wl-ldap-get-value (type entry)
85   ""
86   (let* ((values (cdr (assoc type entry)))
87          (ret (car values)))
88     (if (and ret (not ldap-ignore-attribute-codings))
89         (while values
90           (if (not (string-match "^[\000-\177]*$" (car values)))
91               (setq ret (car values)
92                     values nil)
93             (setq values (cdr values)))))
94     ret))
95           
96 (defun wl-ldap-get-value-list (type entry)
97   ""
98   (cdr (assoc type entry)))
99
100 (defun wl-ldap-make-filter (pat type-list)
101   "Make RFC1558 quiery filter for PAT from ATTR-LIST.
102 Each are \"OR\" combination, and PAT is beginning-match."
103   (concat "(&(objectclass=person)(|"
104           (mapconcat (lambda (x) (format "(%s=%s*)" x pat)) ; fixed format
105                      type-list
106                      "")
107           "))"))
108
109 (defun wl-ldap-make-matched-value-list (regexp type-list entry)
110   "Correct matching WORD with value of TYPE-LIST in ENTRY.
111 Returns matched uniq string list."
112   (let (type val values result)
113     ;; collect matching value
114     (while entry
115       (setq type (car (car entry))
116             values (mapcar (function wl-ldap-alias-safe-string)
117                            (cdr (car entry)))
118             entry (cdr entry))
119       (if (string-match "::?$" type)
120           (setq type (substring type 0 (match-beginning 0))))
121       (if (member type type-list)
122           (while values
123             (setq val (car values)
124                   values (cdr values))
125             (if (and (string-match regexp val)
126                      (not (member val result)))
127                 (setq result (cons val result))))))
128     result))
129
130 (defun wl-ldap-alias-safe-string (str)
131   "Modify STR for alias.
132 Replace space/tab in STR into '_' char.
133 And remove domain part of mail addr."
134   (while (string-match "[^_a-zA-Z0-9+@%.!\\-/]+" str)
135     (setq str (concat (substring str 0 (match-beginning 0))
136                       "_"
137                       (substring str (match-end 0)))))
138   (if (string-match "@[^/@]+" str)
139       (setq str (concat (substring str 0 (match-beginning 0))
140                         (substring str (match-end 0)))))
141   str)
142
143 (defun wl-ldap-register-dn-string (hash dn &optional str dn-list)
144   ""
145   (let (sym dnsym value level)
146     (setq dnsym (intern (upcase dn) hash))
147     (if (and (null str) (boundp dnsym))
148         ()                                      ; already processed
149       ;; make dn-list in fisrt time
150       (if (null dn-list)
151           (let ((case-fold-search t))
152             (setq dn-list (mapcar (lambda (str)
153                                     (if (string-match "[a-z]+=\\(.*\\)" str)
154                                         (wl-ldap-alias-safe-string
155                                          (wl-match-string 1 str))))
156                                   (split-string dn ",")))))
157       ;; prepare candidate for uniq str
158       (if str
159           (setq str (concat str wl-ldap-alias-sep (car dn-list))
160                 dn-list (cdr dn-list))
161         ;; first entry, pre-build with given level
162         (cond
163          ((null wl-ldap-alias-dn-level) (setq level 1))
164          ((eq t wl-ldap-alias-dn-level) (setq level 1000)) ; xxx, big enough
165          ((numberp wl-ldap-alias-dn-level)
166           (if (< 0 wl-ldap-alias-dn-level)
167               (setq level  wl-ldap-alias-dn-level)
168             (setq level 1)))
169          (t
170           (setq level 1)))
171         (while (and (< 0 level) dn-list)
172           (if (null str)
173               (setq str (car dn-list))
174             (setq str (concat str wl-ldap-alias-sep (car dn-list))))
175           (setq level (1- level)
176                 dn-list (cdr dn-list))))
177       (setq sym (intern (upcase str) hash))
178       (if (not (boundp sym))
179           ;; good
180           (progn (set sym (list dn str dn-list))
181                  (set dnsym str))
182         ;; conflict
183         (if (not (eq (setq value (symbol-value sym)) t))
184             ;; move away deeper
185             (progn (set sym t)
186                    (apply (function wl-ldap-register-dn-string) hash value)))
187         (wl-ldap-register-dn-string hash dn str dn-list)))))
188
189 (defun wl-address-ldap-search (pattern cl)
190   "Make address completion-list matched for PATTERN by LDAP search.
191 Matched address lists are append to CL."
192   (require 'pldap)
193   (unless wl-address-ldap-search-hash
194     (setq wl-address-ldap-search-hash (elmo-make-hash 7)))
195   (let ((pat (if (string-match wl-ldap-alias-sep pattern)
196                  (substring pattern 0 (match-beginning 0))
197                pattern))
198         (ldap-default-host wl-ldap-server)
199         (ldap-default-port (or wl-ldap-port 389))
200         (ldap-default-base wl-ldap-base)
201         (dnhash (elmo-make-hash))
202         cache len sym tmpl regexp entries ent values dn dnstr alias
203         result cn mails)
204     ;; check cache
205     (mapatoms (lambda (atom)
206                 (if (and (string-match
207                           (concat "^" (symbol-name atom) ".*") pat)
208                          (or (null cache)
209                              (< (car cache)
210                                 (setq len (length (symbol-name atom))))))
211                     (setq cache (cons
212                                  (or len (length (symbol-name atom)))
213                                  (symbol-value atom)))))
214               wl-address-ldap-search-hash)
215     ;; get matched entries
216     (if cache
217         (setq entries (cdr cache))
218       (condition-case nil
219           (progn
220             (message "Searching in LDAP...")
221             (setq entries (ldap-search-entries
222                            (wl-ldap-make-filter
223                             (concat pat "*")
224                             wl-ldap-search-attribute-type-list)
225                            nil wl-ldap-search-attribute-type-list nil t))
226             (message "Searching in LDAP...done")
227             (elmo-set-hash-val pattern entries wl-address-ldap-search-hash))
228         (error (message ""))))                  ; ignore error: No such object
229     ;;
230     (setq tmpl entries)
231     (while tmpl
232       (wl-ldap-register-dn-string dnhash (car (car tmpl))) ; car is 'dn'.
233       (setq tmpl (cdr tmpl)))
234     ;;
235     (setq regexp (concat "^" pat))
236     (while entries
237       (setq ent (cdar entries)
238             values (wl-ldap-make-matched-value-list
239                     regexp '("mail" "sn" "cn") ent)
240             mails (wl-ldap-get-value-list "mail" ent)
241             cn (wl-ldap-get-value "cn" ent)
242             dn (car (car entries))
243             dnstr (elmo-get-hash-val (upcase dn) dnhash))
244       ;; make alias list generated from LDAP data.
245       (while (and mails values)
246         ;; make alias like MATCHED/DN-STRING
247         (if (not (string-match (concat "^" (regexp-quote (car values))) dnstr))
248             (setq alias (concat (car values) wl-ldap-alias-sep dnstr))
249           ;; use DN-STRING if DN-STRING begin with MATCHED
250           (setq alias dnstr))
251         ;; check uniqness then add to list
252         (setq sym (intern (downcase alias) dnhash))
253         (when (not (boundp sym))
254           (set sym alias)
255           (setq result (cons (cons alias
256                                    (concat cn " <" (car mails) ">"))
257                              result)))
258         (setq values (cdr values)))
259       ;; make mail addrses list
260       (while mails
261         (if (null (assoc (car mails) cl)); Not already in cl.
262             ;; (string-match regexp (car mails))
263             ;; add mail address itself to completion list
264             (setq result (cons (cons (car mails)
265                                      (concat cn " <" (car mails) ">"))
266                                result)))
267         (setq mails (cdr mails)))
268       (setq entries (cdr entries)))
269     (append result cl)))
270
271 (defun wl-complete-field-to ()
272   (interactive)
273   (let ((cl wl-address-completion-list))
274     (if cl
275         (completing-read "To: " cl)
276       (read-string "To: "))))
277
278 (defun wl-address-quote-specials (word)
279   "Make quoted string of WORD if needed."
280   (if (assq 'specials (std11-lexical-analyze word))
281       (prin1-to-string word)
282     word))
283
284 (defun wl-address-make-completion-list (address-list)
285   (let (addr-tuple cl)
286     (while address-list
287       (setq addr-tuple (car address-list))
288       (setq cl
289              (cons
290               (cons (nth 0 addr-tuple)
291                     (concat
292                      (wl-address-quote-specials
293                       (nth 2 addr-tuple)) " <"(nth 0 addr-tuple)">"))
294               cl))
295       ;; nickname completion.
296       (unless (or (equal (nth 1 addr-tuple) (nth 0 addr-tuple))
297                   ;; already exists
298                   (assoc (nth 1 addr-tuple) cl))
299         (setq cl
300               (cons
301                (cons (nth 1 addr-tuple)
302                      (concat
303                       (wl-address-quote-specials
304                        (nth 2 addr-tuple)) " <"(nth 0 addr-tuple)">"))
305                cl)))
306       (setq address-list (cdr address-list)))
307     cl))
308
309 (defun wl-complete-field-body-or-tab ()
310   (interactive)
311   (let ((case-fold-search t)
312         epand-char skip-chars
313         (use-ldap nil)
314         completion-list)
315     (if (wl-draft-on-field-p)
316         (wl-complete-field)
317       (if (and
318            (< (point)
319               (save-excursion
320                 (goto-char (point-min))
321                 (search-forward (concat "\n" mail-header-separator "\n") nil 0)
322                 (point)))
323            (save-excursion
324              (beginning-of-line)
325              (setq use-ldap nil)
326              (while (and (looking-at "^[ \t]")
327                          (not (= (point) (point-min))))
328                (forward-line -1))
329              (cond ((looking-at wl-address-complete-header-regexp)
330                     (setq completion-list wl-address-completion-list)
331                     (if wl-use-ldap
332                         (setq use-ldap t))
333                     (setq epand-char ?@))
334                    ((looking-at wl-folder-complete-header-regexp)
335                     (setq completion-list wl-folder-entity-hashtb)
336                     (setq skip-chars "^, "))
337                    ((looking-at wl-newsgroups-complete-header-regexp)
338                     (setq completion-list wl-folder-newsgroups-hashtb)))))
339           (wl-complete-field-body completion-list
340                                   epand-char skip-chars use-ldap)
341         (indent-for-tab-command)))))
342
343 (defvar wl-completion-buf-name "*Completions*")
344
345 (defvar wl-complete-candidates nil)
346
347 (defun wl-complete-window-show (all)
348   (if (and (get-buffer-window wl-completion-buf-name)
349            (equal wl-complete-candidates all))
350       (let ((win (get-buffer-window wl-completion-buf-name)))
351         (save-excursion
352           (set-buffer wl-completion-buf-name)
353           (if (pos-visible-in-window-p (point-max) win)
354               (set-window-start win 1)
355             (scroll-other-window))))
356     (message "Making completion list...")
357     (setq wl-complete-candidates all)
358     (with-output-to-temp-buffer
359         wl-completion-buf-name
360       (display-completion-list all))
361     (message "Making completion list... done")))
362
363 (defun wl-complete-window-delete ()
364   (let (comp-buf comp-win)
365     (if (setq comp-buf (get-buffer wl-completion-buf-name))
366         (if (setq comp-win (get-buffer-window comp-buf))
367             (delete-window comp-win)))))
368
369 (defun wl-complete-field ()
370   (interactive)
371   (let* ((end (point))
372          (start (save-excursion
373                   (skip-chars-backward "_a-zA-Z0-9+@%.!\\-")
374                   (point)))
375          (completion)
376          (pattern (buffer-substring start end))
377          (cl wl-draft-field-completion-list))
378     (if (null cl)
379         nil
380       (setq completion
381             (let ((completion-ignore-case t))
382               (try-completion pattern cl)))
383       (cond ((eq completion t)
384              (let ((alias (assoc pattern cl)))
385                (if alias
386                    (progn
387                      (delete-region start end)
388                      (insert (cdr alias))
389 ;;;                  (wl-highlight-message (point-min)(point-max) t)
390                      )))
391              (wl-complete-window-delete))
392             ((null completion)
393              (message "Can't find completion for \"%s\"" pattern)
394              (ding))
395             ((not (string= pattern completion))
396              (delete-region start end)
397              (insert completion)
398              (wl-complete-window-delete)
399              (wl-highlight-message (point-min)(point-max) t))
400             (t
401              (let ((list (all-completions pattern cl)))
402                (wl-complete-window-show list)))))))
403
404 (defun wl-complete-insert (start end pattern completion-list)
405   (let ((alias (and (consp completion-list)
406                     (assoc pattern completion-list)))
407         comp-buf comp-win)
408     (if alias
409         (progn
410           (delete-region start end)
411           (insert (cdr alias))
412           (if (setq comp-buf (get-buffer wl-completion-buf-name))
413               (if (setq comp-win (get-buffer-window comp-buf))
414                   (delete-window comp-win)))))))
415
416 (defun wl-complete-field-body (completion-list
417                                &optional epand-char skip-chars use-ldap)
418   (interactive)
419   (let* ((end (point))
420          (start (save-excursion
421                   (skip-chars-backward (or skip-chars "^:,>\n"))
422                   (skip-chars-forward " \t")
423                   (point)))
424          (completion)
425          (pattern (buffer-substring start end))
426          (len (length pattern))
427          (cl completion-list))
428     (when use-ldap
429       (setq cl (wl-address-ldap-search pattern cl)))
430     (if (null cl)
431         nil
432       (setq completion (try-completion pattern cl))
433       (cond ((eq completion t)
434              (if use-ldap (setq wl-address-ldap-search-hash nil))
435              (wl-complete-insert start end pattern cl)
436              (wl-complete-window-delete)
437              (message "Sole completion"))
438             ((and epand-char
439                   (> len 0)
440                   (char-equal (aref pattern (1- len)) epand-char)
441                   (assoc (substring pattern 0 (1- len)) cl))
442              (wl-complete-insert
443               start end
444               (substring pattern 0 (1- len))
445               cl))
446             ((null completion)
447              (message "Can't find completion for \"%s\"" pattern)
448              (ding))
449             ((not (string= pattern completion))
450              (delete-region start end)
451              (insert completion))
452             (t
453              (let ((list (sort (all-completions pattern cl) 'string<)))
454                (wl-complete-window-show list)))))))
455
456 (defvar wl-address-init-func 'wl-local-address-init)
457
458 (defun wl-address-init ()
459   "Call `wl-address-init-func'."
460   (funcall wl-address-init-func))
461
462 (defun wl-local-address-init ()
463   "Reload `wl-address-file'.
464 Refresh `wl-address-list', `wl-address-completion-list', and
465 `wl-address-petname-hash'."
466   (message "Updating addresses...")
467   (setq wl-address-list
468         (wl-address-make-address-list wl-address-file))
469   (setq wl-address-completion-list
470         (wl-address-make-completion-list wl-address-list))
471   (if (file-readable-p wl-alias-file)
472       (setq wl-address-completion-list
473             (append wl-address-completion-list
474                     (wl-address-make-alist-from-alias-file wl-alias-file))))
475   (setq wl-address-petname-hash (elmo-make-hash))
476   (let ((addresses wl-address-list))
477     (while addresses
478       (elmo-set-hash-val (downcase (car (car addresses)))
479                          (cadr (car addresses))
480                          wl-address-petname-hash)
481       (setq addresses (cdr addresses))))
482   (message "Updating addresses...done"))
483
484
485 (defun wl-address-expand-aliases (alist nest-count)
486   (when (< nest-count 5)
487     (let (expn-str new-expn-str expn new-expn(n 0) (expanded nil))
488       (while (setq expn-str (cdr (nth n alist)))
489         (setq new-expn-str nil)
490         (while (string-match "^[ \t]*\\([^,]+\\)" expn-str)
491           (setq expn (elmo-match-string 1 expn-str))
492           (setq expn-str (wl-string-delete-match expn-str 0))
493           (if (string-match "^[ \t,]+" expn-str)
494               (setq expn-str (wl-string-delete-match expn-str 0)))
495           (if (string-match "[ \t,]+$" expn)
496               (setq expn (wl-string-delete-match expn 0)))
497           (setq new-expn (cdr (assoc expn alist)))
498           (if new-expn
499               (setq expanded t))
500           (setq new-expn-str (concat new-expn-str (and new-expn-str ", ")
501                                      (or new-expn expn))))
502         (when new-expn-str
503           (setcdr (nth n alist) new-expn-str))
504         (setq n (1+ n)))
505       (and expanded
506            (wl-address-expand-aliases alist (1+ nest-count))))))
507
508 (defun wl-address-make-alist-from-alias-file (file)
509   (elmo-set-work-buf
510     (let ((case-fold-search t)
511           alias expn alist)
512       (insert-file-contents file)
513       (while (re-search-forward ",$" nil t)
514         (end-of-line)
515         (forward-char 1)
516         (delete-backward-char 1))
517       (goto-char (point-min))
518       (while (re-search-forward "^\\([^#;\n][^:]+\\):[ \t]*\\(.*\\)$" nil t)
519         (setq alias (wl-match-buffer 1)
520               expn (wl-match-buffer 2))
521         (setq alist (cons (cons alias expn) alist)))
522       (wl-address-expand-aliases alist 0)
523       (nreverse alist) ; return value
524       )))
525         
526 (defun wl-address-make-address-list (path)
527   (if (and path (file-readable-p path))
528       (elmo-set-work-buf
529         (let (ret
530               (coding-system-for-read wl-cs-autoconv))
531           (insert-file-contents path)
532           (goto-char (point-min))
533           (while (not (eobp))
534             (if (looking-at
535  "^\\([^#\n][^ \t\n]+\\)[ \t]+\"\\(.*\\)\"[ \t]+\"\\(.*\\)\"[ \t]*.*$")
536                 (setq ret
537                       (wl-append-element
538                        ret
539                        (list (wl-match-buffer 1)
540                              (wl-match-buffer 2)
541                              (wl-match-buffer 3)))))
542             (forward-line))
543           ret))))
544
545 (defsubst wl-address-get-petname-1 (string)
546   (let ((address (downcase (wl-address-header-extract-address string))))
547     (elmo-get-hash-val address wl-address-petname-hash)))
548
549 (defsubst wl-address-get-petname (string)
550   (or (wl-address-get-petname-1 string)
551       string))
552
553 (defsubst wl-address-user-mail-address-p (address)
554   "Judge whether ADDRESS is user's or not."
555   (member (downcase (wl-address-header-extract-address address))
556           (or (mapcar 'downcase wl-user-mail-address-list)
557               (list (downcase
558                      (wl-address-header-extract-address
559                       wl-from))))))
560
561 (defsubst wl-address-header-extract-address (str)
562   "Extracts a real e-mail address from STR and return it.
563 e.g. \"Mine Sakurai <m-sakura@ccs.mt.nec.co.jp>\"
564   ->  \"m-sakura@ccs.mt.nec.co.jp\".
565 e.g. \"m-sakura@ccs.mt.nec.co.jp (Mine Sakurai)\"
566   ->  \"m-sakura@ccs.mt.nec.co.jp\"."
567   (cond ((string-match ".*<\\([^>]*\\)>" str) ; .* to extract last <>
568          (wl-match-string 1 str))
569         ((string-match "\\([^ \t\n]*@[^ \t\n]*\\)" str)
570          (wl-match-string 1 str))
571         (t str)))
572
573 (defsubst wl-address-header-extract-realname (str)
574   "Extracts a real name from STR and return it.
575 e.g. \"Mr. bar <hoge@foo.com>\"
576   ->  \"Mr. bar\"."
577   (cond ((string-match "\\(.*[^ \t]\\)[ \t]*<[^>]*>" str)
578          (wl-match-string 1 str))
579         (t "")))
580
581 (defmacro wl-address-concat-token (string token)
582   (` (cond
583       ((eq 'quoted-string (car (, token)))
584        (concat (, string) "\"" (cdr (, token)) "\""))
585       ((eq 'comment (car (, token)))
586        (concat (, string) "(" (cdr (, token)) ")"))
587       (t
588        (concat (, string) (cdr (, token)))))))
589
590 (defun wl-address-string-without-group-list-contents (sequence)
591   "Return address string from lexical analyzed list SEQUENCE.
592 Group list contents is not included."
593   (let (address-string route-addr-end token seq group-end)
594   (while sequence
595     (setq token (car sequence))
596     (cond
597      ;;   group       =  phrase ":" [#mailbox] ";"
598      ((and (eq 'specials (car token))
599            (string= (cdr token) ":"))
600       (setq address-string (concat address-string (cdr token))) ; ':'
601       (setq seq (cdr sequence))
602       (setq token (car seq))
603       (setq group-end nil)
604       (while (not group-end)
605         (setq token (car seq))
606         (setq seq (cdr seq))
607         (setq group-end (and (eq 'specials (car token))
608                              (string= (cdr token) ";"))))
609       (setq address-string (concat address-string (cdr token))) ; ';'
610       (setq sequence seq))
611      ;;   route-addr  =  "<" [route] addr-spec ">"
612      ;;   route       =  1#("@" domain) ":"           ; path-relative
613      ((and (eq 'specials (car token))
614            (string= (cdr token) "<"))
615       (setq seq (std11-parse-route-addr sequence))
616       (setq route-addr-end (car (cdr seq)))
617       (while (not (eq (car sequence) route-addr-end))
618         (setq address-string (wl-address-concat-token address-string
619                                                       (car sequence)))
620         (setq sequence (cdr sequence))))
621      (t
622       (setq address-string (wl-address-concat-token address-string token))
623       (setq sequence (cdr sequence)))))
624   address-string))
625
626 (defun wl-address-petname-delete (the-email)
627   "Delete petname in `wl-address-file'."
628   (let* ( (tmp-buf (get-buffer-create " *wl-petname-tmp*"))
629           (output-coding-system
630            (mime-charset-to-coding-system wl-mime-charset)))
631     (set-buffer tmp-buf)
632     (message "Deleting Petname...")
633     (erase-buffer)
634     (insert-file-contents wl-address-file)
635     (delete-matching-lines (concat "^[ \t]*" the-email))
636     (write-region (point-min) (point-max)
637                   wl-address-file nil 'no-msg)
638     (message "Deleting Petname...done")
639     (kill-buffer tmp-buf)))
640
641
642 (defun wl-address-petname-add-or-change (the-email
643                                          default-petname
644                                          default-realname
645                                          &optional change-petname)
646   "Add petname to `wl-address-file', if not registerd.
647 If already registerd, change it."
648   (let (the-realname the-petname)
649
650     ;; setup output "petname"
651     ;; if null petname'd, let default-petname be the petname.
652     (setq the-petname
653           (read-from-minibuffer (format "Petname: ") default-petname))
654     (if (string= the-petname "")
655         (setq the-petname (or default-petname the-email)))
656
657     ;; setup output "realname"
658     (setq the-realname
659         (read-from-minibuffer (format "Real Name: ") default-realname))
660 ;;;     (if (string= the-realname "")
661 ;;;         (setq the-realname default-petname))
662
663     ;; writing to ~/.address
664     (let ( (tmp-buf (get-buffer-create " *wl-petname-tmp*"))
665            (output-coding-system (mime-charset-to-coding-system wl-mime-charset)))
666       (set-buffer tmp-buf)
667       (message "Adding Petname...")
668       (erase-buffer)
669       (if (file-exists-p wl-address-file)
670           (insert-file-contents wl-address-file))
671       (if (not change-petname)
672           ;; if only add
673           (progn
674             (goto-char (point-max))
675             (if (and (> (buffer-size) 0)
676                      (not (eq (char-after (1- (point-max))) ?\n)))
677                 (insert "\n")))
678         ;; if change
679         (if (re-search-forward (concat "^[ \t]*" the-email) nil t)
680             (delete-region (save-excursion (beginning-of-line)
681                                            (point))
682                            (save-excursion (end-of-line)
683                                            (+ 1 (point))))))
684       (insert (format "%s\t\"%s\"\t\"%s\"\n"
685                       the-email the-petname the-realname))
686       (write-region (point-min) (point-max)
687                     wl-address-file nil 'no-msg)
688       (message "Adding Petname...done")
689       (kill-buffer tmp-buf))))
690
691 (require 'product)
692 (product-provide (provide 'wl-address) (require 'wl-version))
693
694 ;;; wl-address.el ends here
695