New version number: 0.3
[bbdb-vcard.git] / vcard.el
blob27eb3df643d398b4d31dcfa69621f4af2ab096a2
1 ;;; vcard.el --- vcard parsing and display routines
3 ;; Copyright (C) 1997, 1999, 2000 Noah S. Friedman
5 ;; Author: Noah Friedman <friedman@splode.com>
6 ;; Maintainer: friedman@splode.com
7 ;; Keywords: vcard, mail, news
8 ;; Created: 1997-09-27
10 ;; $Id: vcard.el,v 1.11 2000/06/29 17:07:55 friedman Exp $
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with this program; if not, you can either send email to this
24 ;; program's maintainer or write to: The Free Software Foundation,
25 ;; Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; Unformatted vcards are just plain ugly. But if you live in the MIME
30 ;; world, they are a better way of exchanging contact information than
31 ;; freeform signatures since the former can be automatically parsed and
32 ;; stored in a searchable index.
34 ;; This library of routines provides the back end necessary for parsing
35 ;; vcards so that they can eventually go into an address book like BBDB
36 ;; (although this library does not implement that itself). Also included
37 ;; is a sample pretty-printer which MUAs can use which do not provide their
38 ;; own vcard formatters.
40 ;; This library does not interface directly with any mail user agents. For
41 ;; an example of bindings for the VM MUA, see vm-vcard.el available from
43 ;; http://www.splode.com/~friedman/software/emacs-lisp/index.html#mail
45 ;; Updates to vcard.el should be available there too.
47 ;; The main entry point to this package is `vcard-pretty-print' although
48 ;; any documented variable or function is considered part of the API for
49 ;; operating on vcard data.
51 ;; The vcard 2.1 format is defined by the versit consortium.
52 ;; See http://www.imc.org/pdi/vcard-21.ps
54 ;; RFC 2426 defines the vcard 3.0 format.
55 ;; See ftp://ftp.rfc-editor.org/in-notes/rfc2426.txt
57 ;; A parsed vcard is a list of attributes of the form
59 ;; (proplist value1 value2 ...)
61 ;; Where proplist is a list of property names and parameters, e.g.
63 ;; (property1 (property2 . parameter2) ...)
65 ;; Each property has an associated implicit or explicit parameter value
66 ;; (not to be confused with attribute values; in general this API uses
67 ;; `parameter' to refer to property values and `value' to refer to attribute
68 ;; values to avoid confusion). If a property has no explicit parameter value,
69 ;; the parameter value is considered to be `t'. Any property which does not
70 ;; exist for an attribute is considered to have a nil parameter.
72 ;; TODO:
73 ;; * Finish supporting the 3.0 extensions.
74 ;; Currently, only the 2.1 standard is supported.
75 ;; * Handle nested vcards and grouped attributes?
76 ;; (I've never actually seen one of these in use.)
77 ;; * Handle multibyte charsets.
78 ;; * Inverse of vcard-parse-string: write .VCF files from alist
79 ;; * Implement a vcard address book? Or is using BBDB preferable?
80 ;; * Improve the sample formatter.
82 ;;; Code:
84 (defgroup vcard nil
85 "Support for the vCard electronic business card format."
86 :group 'vcard
87 :group 'mail
88 :group 'news)
90 ;;;###autoload
91 (defcustom vcard-pretty-print-function 'vcard-format-sample-box
92 "*Formatting function used by `vcard-pretty-print'."
93 :type 'function
94 :group 'vcard)
96 ;;;###autoload
97 (defcustom vcard-standard-filters
98 '(vcard-filter-html
99 vcard-filter-adr-newlines
100 vcard-filter-tel-normalize
101 vcard-filter-textprop-cr)
102 "*Standard list of filters to apply to parsed vcard data.
103 These filters are applied sequentially to vcard attributes when
104 the function `vcard-standard-filter' is supplied as the second argument to
105 `vcard-parse'."
106 :type 'hook
107 :group 'vcard)
110 ;;; No user-settable options below.
112 ;; XEmacs 21 ints and chars are disjoint types.
113 ;; For all else, treat them as the same.
114 (defalias 'vcard-char-to-int
115 (if (fboundp 'char-to-int) 'char-to-int 'identity))
117 ;; This is just the version number for this package; it does not refer to
118 ;; the vcard format specification. Currently, this package does not yet
119 ;; support the full vcard 3.0 specification.
121 ;; Whenever any part of the API defined in this package change in a way
122 ;; that is not backward-compatible, the major version number here should be
123 ;; incremented. Backward-compatible additions to the API should be
124 ;; indicated by increasing the minor version number.
125 (defconst vcard-api-version "2.0")
127 ;; The vcard standards allow specifying the encoding for an attribute using
128 ;; these values as immediate property names, rather than parameters of the
129 ;; `encoding' property. If these are encountered while parsing, associate
130 ;; them as parameters of the `encoding' property in the returned structure.
131 (defvar vcard-encoding-tags
132 '("quoted-printable" "base64" "8bit" "7bit"))
134 ;; The vcard parser will auto-decode these encodings when they are
135 ;; encountered. These methods are invoked via vcard-parse-region-value.
136 (defvar vcard-region-decoder-methods
137 '(("quoted-printable" . vcard-region-decode-quoted-printable)
138 ("base64" . vcard-region-decode-base64)))
140 ;; This is used by vcard-region-decode-base64
141 (defvar vcard-region-decode-base64-table
142 (let* ((a "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
143 (len (length a))
144 (tbl (make-vector 123 nil))
145 (i 0))
146 (while (< i len)
147 (aset tbl (vcard-char-to-int (aref a i)) i)
148 (setq i (1+ i)))
149 tbl))
152 ;;; This function can be used generically by applications to obtain
153 ;;; a printable representation of a vcard.
155 ;;;###autoload
156 (defun vcard-pretty-print (vcard)
157 "Format VCARD into a string suitable for display to user.
158 VCARD can be an unparsed string containing raw VCF vcard data
159 or a parsed vcard alist as returned by `vcard-parse-string'.
161 The result is a string with formatted vcard information suitable for
162 insertion into a mime presentation buffer.
164 The function specified by the variable `vcard-pretty-print-function'
165 actually performs the formatting. That function will always receive a
166 parsed vcard alist."
167 (and (stringp vcard)
168 (setq vcard (vcard-parse-string vcard)))
169 (funcall vcard-pretty-print-function vcard))
172 ;;; Parsing routines
174 ;;;###autoload
175 (defun vcard-parse-string (raw &optional filter)
176 "Parse RAW vcard data as a string, and return an alist representing data.
178 If the optional function FILTER is specified, apply that filter to each
179 attribute. If no filter is specified, `vcard-standard-filter' is used.
181 Filters should accept two arguments: the property list and the value list.
182 Modifying in place the property or value list will affect the resulting
183 attribute in the vcard alist.
185 Vcard data is normally in the form
187 begin: vcard
188 prop1a: value1a
189 prop2a;prop2b;prop2c=param2c: value2a
190 prop3a;prop3b: value3a;value3b;value3c
191 end: vcard
193 \(Whitespace around the `:' separating properties and values is optional.\)
194 If supplied to this function an alist of the form
196 \(\(\(\"prop1a\"\) \"value1a\"\)
197 \(\(\"prop2a\" \"prop2b\" \(\"prop2c\" . \"param2c\"\)\) \"value2a\"\)
198 \(\(\"prop3a\" \"prop3b\"\) \"value3a\" \"value3b\" \"value3c\"\)\)
200 would be returned."
201 (let ((vcard nil)
202 (buf (generate-new-buffer " *vcard parser work*")))
203 (unwind-protect
204 (save-excursion
205 (set-buffer buf)
206 ;; Make sure last line is newline-terminated.
207 ;; An extra trailing newline is harmless.
208 (insert raw "\n")
209 (setq vcard (vcard-parse-region (point-min) (point-max) filter)))
210 (kill-buffer buf))
211 vcard))
213 ;;;###autoload
214 (defun vcard-parse-region (beg end &optional filter)
215 "Parse the raw vcard data in region, and return an alist representing data.
216 This function is just like `vcard-parse-string' except that it operates on
217 a region of the current buffer rather than taking a string as an argument.
219 Note: this function modifies the buffer!"
220 (or filter
221 (setq filter 'vcard-standard-filter))
222 (let ((case-fold-search t)
223 (vcard-data nil)
224 (pos (make-marker))
225 (newpos (make-marker))
226 properties value)
227 (save-restriction
228 (narrow-to-region beg end)
229 (save-match-data
230 ;; Unfold folded lines and delete naked carriage returns
231 (goto-char (point-min))
232 (while (re-search-forward "\r$\\|\n[ \t]" nil t)
233 (goto-char (match-beginning 0))
234 (delete-char 1))
236 (goto-char (point-min))
237 (re-search-forward "^begin:[ \t]*vcard[ \t]*\n")
238 (set-marker pos (point))
239 (while (and (not (looking-at "^end[ \t]*:[ \t]*vcard[ \t]*$"))
240 (re-search-forward ":[ \t]*" nil t))
241 (set-marker newpos (match-end 0))
242 (setq properties
243 (vcard-parse-region-properties pos (match-beginning 0)))
244 (set-marker pos (marker-position newpos))
245 (re-search-forward "[ \t]*\n")
246 (set-marker newpos (match-end 0))
247 (setq value
248 (vcard-parse-region-value properties pos (match-beginning 0)))
249 (set-marker pos (marker-position newpos))
250 (goto-char pos)
251 (funcall filter properties value)
252 (setq vcard-data (cons (cons properties value) vcard-data)))))
253 (nreverse vcard-data)))
255 (defun vcard-parse-region-properties (beg end)
256 (downcase-region beg end)
257 (let* ((proplist (vcard-split-string (buffer-substring beg end) ";"))
258 (props proplist)
259 split)
260 (save-match-data
261 (while props
262 (cond ((string-match "=" (car props))
263 (setq split (vcard-split-string (car props) "=" 2))
264 (setcar props (cons (car split) (car (cdr split)))))
265 ((member (car props) vcard-encoding-tags)
266 (setcar props (cons "encoding" (car props)))))
267 (setq props (cdr props))))
268 proplist))
270 (defun vcard-parse-region-value (proplist beg end)
271 (let* ((encoding (vcard-get-property proplist "encoding"))
272 (decoder (cdr (assoc encoding vcard-region-decoder-methods)))
273 result pos match-beg match-end)
274 (save-restriction
275 (narrow-to-region beg end)
276 (cond (decoder
277 ;; Each `;'-separated field needs to be decoded and saved
278 ;; separately; if the entire region were decoded at once, we
279 ;; would not be able to distinguish between the original `;'
280 ;; chars and those which were encoded in order to quote them
281 ;; against being treated as field separators.
282 (goto-char beg)
283 (setq pos (set-marker (make-marker) (point)))
284 (setq match-beg (make-marker))
285 (setq match-end (make-marker))
286 (save-match-data
287 (while (< pos (point-max))
288 (cond ((search-forward ";" nil t)
289 (set-marker match-beg (match-beginning 0))
290 (set-marker match-end (match-end 0)))
292 (set-marker match-beg (point-max))
293 (set-marker match-end (point-max))))
294 (funcall decoder pos match-beg)
295 (setq result (cons (buffer-substring pos match-beg) result))
296 (set-marker pos (marker-position match-end))))
297 (setq result (nreverse result))
298 (vcard-set-property proplist "encoding" nil))
300 (setq result (vcard-split-string (buffer-string) ";")))))
301 (goto-char (point-max))
302 result))
305 ;;; Functions for retrieving property or value information from parsed
306 ;;; vcard attributes.
308 (defun vcard-values (vcard have-props &optional non-props limit)
309 "Return the values in VCARD.
310 This function is like `vcard-ref' and takes the same arguments, but return
311 only the values, not the associated property lists."
312 (mapcar 'cdr (vcard-ref vcard have-props non-props limit)))
314 (defun vcard-ref (vcard have-props &optional non-props limit)
315 "Return the attributes in VCARD with HAVE-PROPS properties.
316 Optional arg NON-PROPS is a list of properties which candidate attributes
317 must not have.
318 Optional arg LIMIT means return no more than that many attributes.
320 The attributes in VCARD which have all properties specified by HAVE-PROPS
321 but not having any specified by NON-PROPS are returned. The first element
322 of each attribute is the actual property list; the remaining elements are
323 the values.
325 If a specific property has an associated parameter \(e.g. an encoding\),
326 use the syntax \(\"property\" . \"parameter\"\) to specify it. If property
327 parameter is not important or it has no specific parameter, just specify
328 the property name as a string."
329 (let ((attrs vcard)
330 (result nil)
331 (count 0))
332 (while (and attrs (or (null limit) (< count limit)))
333 (and (vcard-proplist-all-properties (car (car attrs)) have-props)
334 (not (vcard-proplist-any-properties (car (car attrs)) non-props))
335 (setq result (cons (car attrs) result)
336 count (1+ count)))
337 (setq attrs (cdr attrs)))
338 (nreverse result)))
340 (defun vcard-proplist-all-properties (proplist props)
341 "Returns nil unless PROPLIST contains all properties specified in PROPS."
342 (let ((result t))
343 (while (and result props)
344 (or (vcard-get-property proplist (car props))
345 (setq result nil))
346 (setq props (cdr props)))
347 result))
349 (defun vcard-proplist-any-properties (proplist props)
350 "Returns `t' if PROPLIST contains any of the properties specified in PROPS."
351 (let ((result nil))
352 (while (and (not result) props)
353 (and (vcard-get-property proplist (car props))
354 (setq result t))
355 (setq props (cdr props)))
356 result))
358 (defun vcard-get-property (proplist property)
359 "Return the value from PROPLIST of PROPERTY.
360 PROPLIST is a vcard attribute property list, which is normally the first
361 element of each attribute entry in a vcard."
362 (or (and (member property proplist) t)
363 (cdr (assoc property proplist))))
365 (defun vcard-set-property (proplist property value)
366 "In PROPLIST, set PROPERTY to VALUE.
367 PROPLIST is a vcard attribute property list.
368 If VALUE is nil, PROPERTY is deleted."
369 (let (elt)
370 (cond ((null value)
371 (vcard-delete-property proplist property))
372 ((setq elt (member property proplist))
373 (and value (not (eq value t))
374 (setcar elt (cons property value))))
375 ((setq elt (assoc property proplist))
376 (cond ((eq value t)
377 (setq elt (memq elt proplist))
378 (setcar elt property))
380 (setcdr elt value))))
381 ((eq value t)
382 (nconc proplist (cons property nil)))
384 (nconc proplist (cons (cons property value) nil))))))
386 (defun vcard-delete-property (proplist property)
387 "Delete from PROPLIST the specified property PROPERTY.
388 This will not succeed in deleting the first member of the proplist, but
389 that element should never be deleted since it is the primary key."
390 (let (elt)
391 (cond ((setq elt (member property proplist))
392 (delq (car elt) proplist))
393 ((setq elt (assoc property proplist))
394 (delq (car (memq elt proplist)) proplist)))))
397 ;;; Vcard data filters.
399 ;;; Filters receive both the property list and value list and may modify
400 ;;; either in-place. The return value from the filters are ignored.
402 ;;; These filters can be used for purposes such as removing HTML tags or
403 ;;; normalizing phone numbers into a standard form.
405 (defun vcard-standard-filter (proplist values)
406 "Apply filters in `vcard-standard-filters' to attributes."
407 (vcard-filter-apply-filter-list vcard-standard-filters proplist values))
409 ;; This function could be used to dispatch other filter lists.
410 (defun vcard-filter-apply-filter-list (filter-list proplist values)
411 (while filter-list
412 (funcall (car filter-list) proplist values)
413 (setq filter-list (cdr filter-list))))
415 ;; Some lusers put HTML (or even javascript!) in their vcards under the
416 ;; misguided notion that it's a standard feature of vcards just because
417 ;; Netscape supports this feature. That is wrong; the vcard specification
418 ;; does not define any html content semantics and most MUAs cannot do
419 ;; anything with html text except display them unparsed, which is ugly.
421 ;; Thank Netscape for abusing the standard and damned near rendering it
422 ;; useless for interoperability between MUAs.
424 ;; This filter does a very rudimentary job.
425 (defun vcard-filter-html (proplist values)
426 "Remove HTML tags from attribute values."
427 (save-match-data
428 (while values
429 (while (string-match "<[^<>\n]+>" (car values))
430 (setcar values (replace-match "" t t (car values))))
431 (setq values (cdr values)))))
433 (defun vcard-filter-adr-newlines (proplist values)
434 "Replace newlines with \"; \" in `adr' values."
435 (and (vcard-get-property proplist "adr")
436 (save-match-data
437 (while values
438 (while (string-match "[\r\n]+" (car values))
439 (setcar values (replace-match "; " t t (car values))))
440 (setq values (cdr values))))))
442 (defun vcard-filter-tel-normalize (proplist values)
443 "Normalize telephone numbers in `tel' values.
444 Spaces and hyphens are replaced with `.'.
445 US domestic telephone numbers are replaced with international format."
446 (and (vcard-get-property proplist "tel")
447 (save-match-data
448 (while values
449 (while (string-match "[\t._-]+" (car values))
450 (setcar values (replace-match " " t t (car values))))
451 (and (string-match "^(?\\(\\S-\\S-\\S-\\))? ?\
452 \\(\\S-\\S-\\S- \\S-\\S-\\S-\\S-\\)"
453 (car values))
454 (setcar values
455 (replace-match "+1 \\1 \\2" t nil (car values))))
456 (setq values (cdr values))))))
458 (defun vcard-filter-textprop-cr (proplist values)
459 "Strip carriage returns from text values."
460 (and (vcard-proplist-any-properties
461 proplist '("adr" "email" "fn" "label" "n" "org" "tel" "title" "url"))
462 (save-match-data
463 (while values
464 (while (string-match "\r+" (car values))
465 (setcar values (replace-match "" t t (car values))))
466 (setq values (cdr values))))))
469 ;;; Decoding methods.
471 (defmacro vcard-hexstring-to-ascii (s)
472 (if (string-lessp emacs-version "20")
473 `(format "%c" (car (read-from-string (format "?\\x%s" ,s))))
474 `(format "%c" (string-to-number ,s 16))))
476 (defun vcard-region-decode-quoted-printable (&optional beg end)
477 (save-excursion
478 (save-restriction
479 (save-match-data
480 (narrow-to-region (or beg (point-min)) (or end (point-max)))
481 (goto-char (point-min))
482 (while (re-search-forward "=\n" nil t)
483 (delete-region (match-beginning 0) (match-end 0)))
484 (goto-char (point-min))
485 (while (re-search-forward "=[0-9A-Za-z][0-9A-Za-z]" nil t)
486 (let ((s (buffer-substring (1+ (match-beginning 0)) (match-end 0))))
487 (replace-match (vcard-hexstring-to-ascii s) t t)))))))
489 (defun vcard-region-decode-base64 (&optional beg end)
490 (save-restriction
491 (narrow-to-region (or beg (point-min)) (or end (point-max)))
492 (save-match-data
493 (goto-char (point-min))
494 (while (re-search-forward "[ \t\r\n]+" nil t)
495 (delete-region (match-beginning 0) (match-end 0))))
496 (goto-char (point-min))
497 (let ((count 0)
498 (n 0)
499 (c nil))
500 (while (not (eobp))
501 (setq c (char-after (point)))
502 (delete-char 1)
503 (cond ((char-equal c ?=)
504 (if (= count 2)
505 (insert (lsh n -10))
506 ;; count must be 3
507 (insert (lsh n -16) (logand 255 (lsh n -8))))
508 (delete-region (point) (point-max)))
510 (setq n (+ n (aref vcard-region-decode-base64-table
511 (vcard-char-to-int c))))
512 (setq count (1+ count))
513 (cond ((= count 4)
514 (insert (logand 255 (lsh n -16))
515 (logand 255 (lsh n -8))
516 (logand 255 n))
517 (setq n 0 count 0))
519 (setq n (lsh n 6))))))))))
522 (defun vcard-split-string (string &optional separator limit)
523 "Split STRING at occurences of SEPARATOR. Return a list of substrings.
524 Optional argument SEPARATOR can be any regexp, but anything matching the
525 separator will never appear in any of the returned substrings.
526 If not specified, SEPARATOR defaults to \"[ \\f\\t\\n\\r\\v]+\".
527 If optional arg LIMIT is specified, split into no more than that many
528 fields \(though it may split into fewer\)."
529 (or separator (setq separator "[ \f\t\n\r\v]+"))
530 (let ((string-list nil)
531 (len (length string))
532 (pos 0)
533 (splits 0)
534 str)
535 (save-match-data
536 (while (<= pos len)
537 (setq splits (1+ splits))
538 (cond ((and limit
539 (>= splits limit))
540 (setq str (substring string pos))
541 (setq pos (1+ len)))
542 ((string-match separator string pos)
543 (setq str (substring string pos (match-beginning 0)))
544 (setq pos (match-end 0)))
546 (setq str (substring string pos))
547 (setq pos (1+ len))))
548 (setq string-list (cons str string-list))))
549 (nreverse string-list)))
551 (defun vcard-copy-tree (tree)
552 "Make a deep copy of nested conses."
553 (cond
554 ((consp tree)
555 (cons (vcard-copy-tree (car tree))
556 (vcard-copy-tree (cdr tree))))
557 (t tree)))
559 (defun vcard-flatten (l)
560 (if (consp l)
561 (apply 'nconc (mapcar 'vcard-flatten l))
562 (list l)))
565 ;;; Sample formatting routines.
567 (defun vcard-format-sample-box (vcard)
568 "Like `vcard-format-sample-string', but put an ascii box around text."
569 (let* ((lines (vcard-format-sample-lines vcard))
570 (len (vcard-format-sample-max-length lines))
571 (edge (concat "\n+" (make-string (+ len 2) ?-) "+\n"))
572 (line-fmt (format "| %%-%ds |" len))
573 (formatted-lines
574 (mapconcat (function (lambda (s) (format line-fmt s))) lines "\n")))
575 (if (string= formatted-lines "")
576 formatted-lines
577 (concat edge formatted-lines edge))))
579 (defun vcard-format-sample-string (vcard)
580 "Format VCARD into a string suitable for display to user.
581 VCARD should be a parsed vcard alist. The result is a string
582 with formatted vcard information which can be inserted into a mime
583 presentation buffer."
584 (mapconcat 'identity (vcard-format-sample-lines vcard) "\n"))
586 (defun vcard-format-sample-lines (vcard)
587 (let* ((name (vcard-format-sample-get-name vcard))
588 (title (vcard-format-sample-values-concat vcard '("title") 1 "; "))
589 (org (vcard-format-sample-values-concat vcard '("org") 1 "; "))
590 (addr (vcard-format-sample-get-address vcard))
591 (tel (vcard-format-sample-get-telephone vcard))
592 (lines (delete nil (vcard-flatten (list name title org addr))))
593 (col-template (format "%%-%ds%%s"
594 (vcard-format-sample-offset lines tel)))
595 (l lines))
596 (while tel
597 (setcar l (format col-template (car l) (car tel)))
598 ;; If we stripped away too many nil slots from l, add empty strings
599 ;; back in so setcar above will work on next iteration.
600 (and (cdr tel)
601 (null (cdr l))
602 (setcdr l (cons "" nil)))
603 (setq l (cdr l))
604 (setq tel (cdr tel)))
605 lines))
607 (defun vcard-format-sample-get-name (vcard)
608 (let ((name (car (car (vcard-values vcard '("fn") nil 1))))
609 (email (car (vcard-format-sample-values
610 vcard '((("email" "pref"))
611 (("email" "internet"))
612 (("email"))) 1))))
613 (cond ((and name email)
614 (format "%s <%s>" name email))
615 (email)
616 (name)
617 (""))))
619 (defun vcard-format-sample-get-telephone (vcard)
620 (let ((fields '(("Work: "
621 (("tel" "work" "pref") . ("fax" "pager" "cell"))
622 (("tel" "work" "voice") . ("fax" "pager" "cell"))
623 (("tel" "work") . ("fax" "pager" "cell")))
624 ("Home: "
625 (("tel" "home" "pref") . ("fax" "pager" "cell"))
626 (("tel" "home" "voice") . ("fax" "pager" "cell"))
627 (("tel" "home") . ("fax" "pager" "cell"))
628 (("tel") . ("fax" "pager" "cell" "work")))
629 ("Cell: "
630 (("tel" "cell" "pref"))
631 (("tel" "cell")))
632 ("Fax: "
633 (("tel" "pref" "fax"))
634 (("tel" "work" "fax"))
635 (("tel" "home" "fax"))
636 (("tel" "fax")))))
637 (phones nil)
638 result)
639 (while fields
640 (setq result (vcard-format-sample-values vcard (cdr (car fields))))
641 (while result
642 (setq phones
643 (cons (concat (car (car fields)) (car (car result))) phones))
644 (setq result (cdr result)))
645 (setq fields (cdr fields)))
646 (nreverse phones)))
648 (defun vcard-format-sample-get-address (vcard)
649 (let* ((addr (vcard-format-sample-values vcard '((("adr" "pref" "work"))
650 (("adr" "pref"))
651 (("adr" "work"))
652 (("adr"))) 1))
653 (street (delete "" (list (nth 0 addr) (nth 1 addr) (nth 2 addr))))
654 (city-list (delete "" (nthcdr 3 addr)))
655 (city (cond ((null (car city-list)) nil)
656 ((cdr city-list)
657 (format "%s, %s"
658 (car city-list)
659 (mapconcat 'identity (cdr city-list) " ")))
660 (t (car city-list)))))
661 (delete nil (if city
662 (append street (list city))
663 street))))
665 (defun vcard-format-sample-values-concat (vcard have-props limit sep)
666 (let ((l (car (vcard-values vcard have-props nil limit))))
667 (and l (mapconcat 'identity (delete "" (vcard-copy-tree l)) sep))))
669 (defun vcard-format-sample-values (vcard proplists &optional limit)
670 (let ((result (vcard-format-sample-ref vcard proplists limit)))
671 (if (equal limit 1)
672 (cdr result)
673 (mapcar 'cdr result))))
675 (defun vcard-format-sample-ref (vcard proplists &optional limit)
676 (let ((result nil))
677 (while (and (null result) proplists)
678 (setq result (vcard-ref vcard
679 (car (car proplists))
680 (cdr (car proplists))
681 limit))
682 (setq proplists (cdr proplists)))
683 (if (equal limit 1)
684 (vcard-copy-tree (car result))
685 (vcard-copy-tree result))))
687 (defun vcard-format-sample-offset (row1 row2 &optional maxwidth)
688 (or maxwidth (setq maxwidth (frame-width)))
689 (let ((max1 (vcard-format-sample-max-length row1))
690 (max2 (vcard-format-sample-max-length row2)))
691 (if (zerop max1)
693 (+ max1 (min 5 (max 1 (- maxwidth (+ max1 max2))))))))
695 (defun vcard-format-sample-max-length (strings)
696 (let ((maxlen 0))
697 (while strings
698 (setq maxlen (max maxlen (length (car strings))))
699 (setq strings (cdr strings)))
700 maxlen))
702 (provide 'vcard)
704 ;;; vcard.el ends here.