forked from pinoaffe/org-vcard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorg-vcard.el
More file actions
1560 lines (1396 loc) · 54.4 KB
/
Copy pathorg-vcard.el
File metadata and controls
1560 lines (1396 loc) · 54.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; org-vcard.el --- org-mode support for vCard export and import.
;; Copyright (C) 2014-2019,2021-2022 Alexis <flexibeast@gmail.com>, Will Dey <will123dey@gmail.com>
;; Author: Alexis <flexibeast@gmail.com>, Will Dey <will123dey@gmail.com>
;; Maintainer: Alexis <flexibeast@gmail.com>
;; Created: 2014-07-31
;; URL: https://github.com/flexibeast/org-vcard
;; Keywords: outlines, org, vcard
;;
;; This file is NOT part of GNU Emacs.
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;; *This project is currently unmaintained, and needs a new
;; maintainer. If you wish to take maintainership, please email
;; Alexis.*
;; `org-vcard' is a package for exporting and importing
;; [vCards](https://en.wikipedia.org/wiki/Vcard) from within [GNU
;; Emacs](https://www.gnu.org/software/emacs/)' [Org
;; mode](http://orgmode.org/).
;; ## Table of Contents
;; - [Features](#features)
;; - [Installation](#installation)
;; - [Usage](#usage)
;; - [TODO](#todo)
;; - [Issues](#issues)
;; - [Testing](#testing)
;; - [License](#license)
;; ## Features
;; * _Backwards-compatible with org-contacts.el._
;; `org-vcard' comes with a built-in contacts style called `flat',
;; which adheres to org-contacts' method of structuring contacts and
;; contact information. It not only supports the properties specified
;; in org-contacts.el, but many other properties as well.
;; * _Basic support for vCard 4.0, 3.0 and 2.1._
;; `org-vcard' is working towards full compliance with the vCard 4.0
;; ([RFC 6350](https://tools.ietf.org/html/rfc6350)), 3.0 ([RFC
;; 2426](https://tools.ietf.org/html/rfc2426) and [RFC
;; 4770](https://tools.ietf.org/html/rfc4770)) and
;; [2.1](https://web.archive.org/web/20120501162958/http://www.imc.org/pdi/vcard-21.doc)
;; specifications.
;; * _New contacts style: `tree'._
;; `org-vcard' introduces a new style for Org contacts, called
;; `tree'. More details [below](#tree).
;; * _Highly customisable, via Emacs' `customize' interface._
;; Modify existing contact styles; change the labels used to map
;; contact details in `org-mode' to various vCard properties/types, or
;; add new ones. Create completely new contact styles by plugging in
;; your own code to handle export and import.
;; ## Installation
;; Install `org-vcard' from [MELPA](http://melpa.org/#/org-vcard),
;; [MELPA Stable](http://stable.melpa.org/#/org-vcard), or put the
;; `org-vcard' folder in your load-path and do a `(require
;; 'org-vcard)'.
;; ## Usage
;; The main user commands are `org-vcard-export' and
;; `org-vcard-import', which are intended to be called interactively;
;; you can press TAB at many of the minibuffer prompts to get a list
;; of the available options for a prompt.
;; Both `org-vcard-export' and `org-vcard-import' are wrappers around
;; the `org-vcard-transfer-helper'
;; function. `org-vcard-transfer-helper' can be used to export and
;; import programmatically (i.e. via Emacs Lisp).
;; Enabling `org-vcard-mode' will add an "Org-vCard" menu to the menu
;; bar, from which one can access the various export, import and
;; customisation options.
;; **Note!** When exporting to vCard using the source `buffer',
;; narrowing is respected. If you wish to export the entire buffer
;; without restriction, remove any narrowing in effect.
;; For a list of the properties available by default for each contacts
;; style and related vCard versions, visit the "Org Vcard Styles
;; Languages Mappings" setting in the Org Vcard customize group, or
;; examine the value of the `org-vcard-styles-languages-mappings'
;; variable.
;; **Note!** The default mappings might need to be tweaked for
;; particular use-cases. For example, some systems create vCards
;; with a bare `TEL' property, whereas others use `TEL;TYPE=voice';
;; but both are mapped to the Org `PHONE' property (for `flat'
;; style) or `phone' FIELDTYPE (for `tree' style). In this case, the
;; `customize' interface could be used to delete whichever of the
;; two mappings is unwanted.
;; The value of the `org-vcard-include-import-unknowns' (boolean)
;; variable specifies whether the import process should include vCard
;; properties not listed in the mapping being used.
;; The value of the `org-vcard-append-to-existing-import-buffer' and
;; `org-vcard-append-to-existing-export-buffer' (boolean) variables
;; specify whether the import/export process should append to any
;; existing import/export buffer. If not, a new import/export buffer
;; is created for each import/export.
;; <a name="tree"></a>
;; ### The `tree' contacts style
;; The structure of the `tree' contacts style is:
;; * [Contact name]
;; :PROPERTIES:
;; :KIND: individual
;; :FIELDTYPE: name
;; :END:
;; ** [Information type]
;; *** [Information value]
;; :PROPERTIES:
;; :FIELDTYPE: [Field type]
;; [:PREFERRED:]
;; :END:
;; Here's an example:
;; * Joan Smith
;; :PROPERTIES:
;; :KIND: individual
;; :FIELDTYPE: name
;; :END:
;; ** Mobile
;; *** 0000 999 999
;; :PROPERTIES:
;; :FIELDTYPE: cell
;; :END:
;; ** Email
;; *** Work
;; **** address1@hidden
;; :PROPERTIES:
;; :FIELDTYPE: email-work
;; :PREFERRED:
;; :END:
;; *** Home
;; **** address2@hidden
;; :PROPERTIES:
;; :FIELDTYPE: email-home
;; :END:
;; As the `tree' style uses a heading's FIELDTYPE property to
;; associate fields with their data, the above hierarchy is only one
;; way to structure contacts; equivalently, one could do:
;; * People
;; ** Joan Smith
;; :PROPERTIES:
;; :KIND: individual
;; :FIELDTYPE: name
;; :END:
;; *** Cell
;; **** 0000 999 999
;; :PROPERTIES:
;; :FIELDTYPE: cell
;; :END:
;; *** Email
;; **** address1@hidden
;; :PROPERTIES:
;; :FIELDTYPE: email-work
;; :PREFERRED:
;; :END:
;; **** address2@hidden
;; :PROPERTIES:
;; :FIELDTYPE: email-home
;; :END:
;; ## TODO
;; * Add support for one-vCard-per-file export.
;; * Add support for line folding when exporting.
;; * Add support for multiple instances of EMAIL property.
;; * Add support for vCard PREF for style `flat'.
;; * Add support for vCard KINDs `group' and `org'.
;; * Add support for per-card version handling on import.
;; * Improve compliance with each version of the vCard specification.
;; * Extend test coverage.
;; <a name="issues"></a>
;; ## Issues / bugs
;; If you discover an issue or bug in `org-vcard' not already noted:
;; * as a TODO item, or
;; * in [the project's "Issues" section on
;; GitHub](https://github.com/flexibeast/org-vcard/issues),
;; please create a new issue with as much detail as possible, including:
;; * which version of Emacs you're running on which operating system, and
;; * how you installed `org-vcard'.
;; ## Testing
;; A basic test suite is located in the repository `tests' directory,
;; in `org-vcard-tests.el`. To run the suite:
;; 1. Ensure `org-vcard.el' has been loaded, e.g. `(load "~/org-vcard/org-vcard.el")'.
;; 2. Load the test suite: e.g. `(load "~/org-vcard/tests/org-vcard-tests.el")'.
;; 3. Run the tests by evaluating `(ert '(tag org-vcard))'.
;; ## License
;; [GNU General Public License version
;; 3](http://www.gnu.org/licenses/gpl.html), or (at your option) any
;; later version.
;;; Code:
;;
;; This package is working towards full compliance with the
;; vCard specifications:
;; vCard 4.0: https://tools.ietf.org/html/rfc6350
;; vCard 3.0: https://tools.ietf.org/html/rfc2426
;; vCard 2.1: http://www.imc.org/pdi/vcard-21.txt
;; If you find any apparent instances of non-compliance that aren't
;; already noted in the TODO section of the org-vcard README.md
;; document, please let the maintainers know.
;; Differences between 4.0 and 3.0 can be found in Appendix A of
;; RFC6350: https://tools.ietf.org/html/rfc6350#page-73
;; Note that vCard 3.0 'types' became vCard 4.0 'properties'.
;; Differences between 3.0 and 2.1 can be found in Section 5 of
;; RFC2426: https://tools.ietf.org/html/rfc2426#page-37
;; Point of amusement:
;; In section 7 of RFC2426, the authors of the standard don't
;; include the 'N' type in their supposed-version-3.0 vCards.
;; Please refer to the TODO section of the org-vcard README.md
;; document for known limitations and/or issues.
;;
(require 'org)
(require 'org-element)
(require 'subr-x)
(defconst org-vcard-mode-keymap (make-sparse-keymap))
;;
;; Directory configuration.
;;
(defconst org-vcard--elisp-dir (file-name-directory load-file-name)
"Absolute path of the directory of org-vcard.el.")
(defcustom org-vcard-custom-styles-dir nil
"Directory containing custom styles."
:type 'directory
:group 'org-vcard)
(defun org-vcard--styles-dirs ()
"List of directories containing org-vcard styles."
(let ((default (file-name-as-directory
(concat org-vcard--elisp-dir "styles"))))
(if org-vcard-custom-styles-dir
(list default org-vcard-custom-styles-dir)
(list default))))
;;
;; Internal functions and variables.
;;
(defun org-vcard--canonicalise-adr-property (property-name)
"Canonicalise a vCard ADR property.
Intended for use by `org-vcard--canonicalise-property-name'.
PROPERTY-NAME must be a string containing a vCard property name."
(let ((property-canonicalised "ADR")
(property-type-data '())
(case-fold-search t))
(if (string-match "HOME" property-name)
(cond
((string= "4.0" org-vcard-active-version)
(setq property-type-data
(append property-type-data '("home"))))
((string= "3.0" org-vcard-active-version)
(setq property-type-data
(append property-type-data '("home"))))
((string= "2.1" org-vcard-active-version)
(setq property-type-data
(append property-type-data '(";HOME"))))))
(if (string-match "WORK" property-name)
(cond
((string= "4.0" org-vcard-active-version)
(setq property-type-data
(append property-type-data '("work"))))
((string= "3.0" org-vcard-active-version)
(setq property-type-data
(append property-type-data '("work"))))
((string= "2.1" org-vcard-active-version)
(setq property-type-data
(append property-type-data '(";WORK"))))))
`(,property-canonicalised ,property-type-data)))
(defun org-vcard--canonicalise-email-property (property-name)
"Canonicalise a vCard EMAIL property.
Intended for use by `org-vcard--canonicalise-property-name'.
PROPERTY-NAME must be a string containing a vCard property name."
(let ((property-canonicalised "EMAIL")
(property-type-data '())
(case-fold-search t))
(if (string-match "HOME" property-name)
(cond
((string= "4.0" org-vcard-active-version)
(setq property-type-data
(append property-type-data '("home"))))
((string= "3.0" org-vcard-active-version)
(setq property-type-data
(append property-type-data '("home"))))
((string= "2.1" org-vcard-active-version)
(setq property-type-data
(append property-type-data '(";HOME"))))))
(if (string-match "WORK" property-name)
(cond
((string= "4.0" org-vcard-active-version)
(setq property-type-data
(append property-type-data '("work"))))
((string= "3.0" org-vcard-active-version)
(setq property-type-data
(append property-type-data '("work"))))
((string= "2.1" org-vcard-active-version)
(setq property-type-data
(append property-type-data '(";WORK"))))))
`(,property-canonicalised ,property-type-data)))
(defun org-vcard--canonicalise-property-name (property-name)
"Canonicalise a vCard property name.
Canonicalisation of a property enable its lookup in an org-vcard mapping.
PROPERTY-NAME must be a string containing the vCard property name."
(if (not (string-match ";" property-name))
;; No need to do anything, return property-name unchanged.
property-name
;; Property has qualifiers.
(if (or
(and
(not (string-match "^ADR" property-name))
(not (string-match "^EMAIL" property-name))
(not (string-match "^TEL" property-name)))
(and
(string-match "^TEL" property-name)
(string-match "PAGER" property-name)))
;; We currently only canonicalise the ADR, EMAIL and TEL
;; properties, and don't handle the PAGER type within the
;; latter, so return property-name unchanged when not dealing
;; with ADR, EMAIL or TEL, or when dealing with PAGER.
property-name
;; Canonicalise.
(let* ((property-canonicalised "")
(property-type-data '())
(retval '())
(case-fold-search t)
(preferred
(if (string-match "PREF" property-name)
t
nil)))
(cond
((string-match "^ADR" property-name)
(progn
(setq retval (org-vcard--canonicalise-adr-property property-name))
(setq property-canonicalised (car retval))
(setq property-type-data (cadr retval))))
((string-match "^EMAIL" property-name)
(progn
(setq retval (org-vcard--canonicalise-email-property property-name))
(setq property-canonicalised (car retval))
(setq property-type-data (cadr retval))))
((string-match "^TEL" property-name)
(progn
(setq retval (org-vcard--canonicalise-tel-property property-name))
(setq property-canonicalised (car retval))
(setq property-type-data (cadr retval)))))
(cond
((string= "4.0" org-vcard-active-version)
(progn
(if property-type-data
(progn
(setq property-canonicalised
(concat property-canonicalised ";TYPE=\""))
(let ((processed-one nil))
(dolist (type property-type-data)
(if processed-one
(setq property-canonicalised
(concat property-canonicalised "," type))
(progn
(setq property-canonicalised
(concat property-canonicalised type))
(setq processed-one t)))))
(setq property-canonicalised
(concat property-canonicalised "\""))))
(if preferred
(setq property-canonicalised
(concat property-canonicalised ";PREF=1")))))
((string= "3.0" org-vcard-active-version)
(progn
(if property-type-data
(progn
(setq property-canonicalised
(concat property-canonicalised ";TYPE="))
(let ((processed-one nil))
(dolist (type property-type-data)
(if processed-one
(setq property-canonicalised
(concat property-canonicalised "," type))
(progn
(setq property-canonicalised
(concat property-canonicalised type))
(setq processed-one t)))))))
(if preferred
(if property-type-data
(setq property-canonicalised
(concat property-canonicalised ",pref"))
(setq property-canonicalised
(concat property-canonicalised ";TYPE=pref"))))))
((string= "2.1" org-vcard-active-version)
(progn
(dolist (type property-type-data)
(setq property-canonicalised
(concat property-canonicalised type)))
(if preferred
(setq property-canonicalised
(concat property-canonicalised ";PREF"))))))
property-canonicalised))))
(defun org-vcard--canonicalise-tel-property (property-name)
"Canonicalise a vCard TEL property.
Intended for use by `org-vcard--canonicalise-property-name'.
PROPERTY-NAME must be a string containing a vCard property name."
(let ((property-canonicalised "TEL")
(property-type-data '())
(case-fold-search t))
(if (string-match "CELL" property-name)
(cond
((string= "4.0" org-vcard-active-version)
(setq property-type-data
(append property-type-data '("cell"))))
((string= "3.0" org-vcard-active-version)
(setq property-type-data
(append property-type-data '("cell"))))
((string= "2.1" org-vcard-active-version)
(setq property-type-data
(append property-type-data '(";CELL"))))))
(if (string-match "FAX" property-name)
(cond
((string= "4.0" org-vcard-active-version)
(setq property-type-data
(append property-type-data '("fax"))))
((string= "3.0" org-vcard-active-version)
(setq property-type-data
(append property-type-data '("fax"))))
((string= "2.1" org-vcard-active-version)
(setq property-type-data
(append property-type-data '(";FAX"))))))
;; Assume the TEL is for VOICE if other qualifiers
;; don't specify otherwise.
(if (and (not (string-match "CELL" property-name))
(not (string-match "FAX" property-name))
(not (string-match "MSG" property-name)))
(cond
((string= "4.0" org-vcard-active-version)
(setq property-type-data
(append property-type-data '("voice"))))
((string= "3.0" org-vcard-active-version)
(setq property-type-data
(append property-type-data '("voice"))))
((string= "2.1" org-vcard-active-version)
(setq property-type-data
(append property-type-data '(";VOICE"))))))
(if (string-match "HOME" property-name)
(cond
((string= "4.0" org-vcard-active-version)
(setq property-type-data
(append property-type-data '("home"))))
((string= "3.0" org-vcard-active-version)
(setq property-type-data
(append property-type-data '("home"))))
((string= "2.1" org-vcard-active-version)
(setq property-type-data
(append property-type-data '(";HOME"))))))
(if (string-match "WORK" property-name)
(cond
((string= "4.0" org-vcard-active-version)
(setq property-type-data
(append property-type-data '("work"))))
((string= "3.0" org-vcard-active-version)
(setq property-type-data
(append property-type-data '("work"))))
((string= "2.1" org-vcard-active-version)
(setq property-type-data
(append property-type-data '(";WORK"))))))
`(,property-canonicalised ,property-type-data)))
(defun org-vcard--check-contacts-styles ()
"Check integrity of `org-vcard-contacts-styles'."
(let ((styles '()))
(dolist (style org-vcard--styles-functions)
(if (not (member (car style) styles))
(setq styles (append styles `(,(car style))))
(error
(concat
"Style '"
(cadr style)
"' appears more than once in org-vcards-contacts-styles")))
(if (not (functionp (nth 0 (cadr style))))
(error
(concat
"Style '"
(car style)
"' has an invalid export function")))
(if (not (functionp (nth 1 (cadr style))))
(error
(concat
"Style '"
(car style)
"' has an invalid import function"))))))
(defun org-vcard--create-styles-functions ()
"Create a data structure for use by `org-vcard-styles-functions'."
(let ((the-list) '())
(dolist (style-dir (org-vcard--styles-dirs))
(dolist (style (directory-files style-dir))
(if (and
(not (string= "." (file-name-nondirectory style)))
(not (string= ".." (file-name-nondirectory style))))
(progn
(load
(concat
(file-name-as-directory (concat style-dir style))
"functions.el"))
(add-to-list
'the-list
`(,(file-name-nondirectory style)
,(list
(intern
(concat
"org-vcard-export-from-"
(file-name-nondirectory style)))
(intern
(concat
"org-vcard-import-to-"
(file-name-nondirectory style))))))))))
(sort the-list
#'(lambda (a b)
(if (string< (car a) (car b))
t
nil)))))
(defun org-vcard--create-styles-languages-mappings ()
"Create a data structure for use by `org-vcard-styles-languages-mappings'."
(let ((style-mappings '()))
(dolist (style-dir (org-vcard--styles-dirs))
(dolist (style
;; Reverse the list so the repeated calls to
;; add-to-list will produce a lexicographically-sorted
;; list.
(sort (directory-files style-dir)
#'(lambda (a b)
(if (not (string< a b))
t
nil))))
(if (and
(not (string= "." style))
(not (string= ".." style)))
(progn
(let ((language-mapping '()))
(dolist
(mapping
;; Reverse the list so the repeated calls to add-to-list
;; will produce a lexicographically-sorted list.
(sort (directory-files
(file-name-as-directory
(concat
(file-name-as-directory
(concat style-dir style))
"mappings"))
t)
#'(lambda (a b)
(if (not (string< a b))
t
nil))))
(if (and
(not
(string=
"."
(file-name-nondirectory mapping)))
(not
(string=
".."
(file-name-nondirectory mapping))))
(progn
(add-to-list
'language-mapping
`(,(file-name-nondirectory mapping)
,@(list
(car
(read-from-string
(with-temp-buffer
(insert-file-contents-literally mapping)
(buffer-string))))))))))
(setq language-mapping (list language-mapping))
(add-to-list
'style-mappings
`(,style
,@language-mapping)))))))
style-mappings))
(defun org-vcard--escape-value-string (characters value)
"Escape each instance of each character in CHARACTERS.
CHARACTERS must be a list of strings. VALUE is the string to be
escaped."
(if (member "\134" characters)
;; Process backslashes first.
(setq value
(replace-regexp-in-string
"\134\134"
"\134\134"
value nil t)))
(dolist (char characters)
(if (not (string= "\134" char))
;; We're escaping a non-backslash character.
(setq value
(replace-regexp-in-string
char
(concat "\134" char)
value nil t))))
value)
(defun org-vcard--export-line (property value &optional noseparator)
"Export a line as appropriate for each vCard version.
PROPERTY is the vCard property/type to output, VALUE its value.
If NOSEPARATOR is non-nil, don't output colon to separate PROPERTY
from VALUE."
(let ((separator ":")
(property-name
(progn
(string-match "^[^;:]+" property)
(match-string 0 property))))
(if noseparator
(setq separator ""))
(cond
((string= org-vcard-active-version "4.0")
;; In values, escape commas, semicolons and backslashes.
;; End line with U+000D U+000A.
;; Output must be UTF-8.
(encode-coding-string
(concat
property
separator
(cond
((member property-name org-vcard-comma-separated-properties)
(org-vcard--escape-value-string '(";" "\134") value))
((member property-name org-vcard-compound-properties)
(org-vcard--escape-value-string '("," "\134") value))
(t
(org-vcard--escape-value-string '("," ";" "\134") value)))
"\u000D\u000A")
'utf-8))
((string= org-vcard-active-version "3.0")
;; In values, escape commas and semicolons.
;; End line with CRLF.
;; RFC2426 doesn't seem to mandate an encoding, so output UTF-8.
(encode-coding-string
(concat
property
separator
(cond
((member property-name org-vcard-comma-separated-properties)
(org-vcard--escape-value-string '(";" "\134") value))
((member property-name org-vcard-compound-properties)
(org-vcard--escape-value-string '("," "\134") value))
(t
(org-vcard--escape-value-string '("," ";" "\134") value)))
"\015\012")
'utf-8))
((string= org-vcard-active-version "2.1")
;; In values, escape semicolons.
;; End line with CRLF.
;; Output ASCII.
(concat
(encode-coding-string property 'us-ascii)
(unless (or (string= "BEGIN" property)
(string= "VERSION" property)
(string= "END" property))
(encode-coding-string
(concat
";CHARSET="
(car
(rassoc
org-vcard-default-vcard-21-character-set
org-vcard-character-set-mapping)))
'us-ascii))
(encode-coding-string separator 'us-ascii)
(if (not (member property-name org-vcard-compound-properties))
(encode-coding-string
(org-vcard--escape-value-string '(";") value)
org-vcard-default-vcard-21-character-set)
(encode-coding-string
value
org-vcard-default-vcard-21-character-set))
(encode-coding-string "\015\012" 'us-ascii))))))
(defun org-vcard--set-active-settings ()
"Set active settings to values of last in-buffer settings.
Fall back to value of custom variables."
(save-excursion
(goto-char (point-min))
(let* ((valid-styles (mapcar 'car org-vcard--styles-functions))
(valid-languages '("en" "en_AU" "en_US"))
(valid-versions '("4.0" "3.0" "2.1"))
(found-keywords '()))
(while (not (eobp))
(if (looking-at "^#+")
(let ((this-line (org-element-keyword-parser nil nil)))
(when (eq 'keyword (car this-line))
(cond
((string=
"CONTACTS_STYLE"
(plist-get (cadr this-line) :key))
(if (member (plist-get (cadr this-line) :value) valid-styles)
(progn
(setq org-vcard-active-style
(plist-get (cadr this-line) :value))
(setq found-keywords
(append found-keywords '("CONTACTS_STYLE"))))
(error "Invalid in-buffer setting for CONTACTS_STYLE")))
((string=
"CONTACTS_LANGUAGE"
(plist-get (cadr this-line) :key))
(if (member (plist-get (cadr this-line) :value) valid-languages)
(progn
(setq org-vcard-active-language
(plist-get (cadr this-line) :value))
(setq found-keywords
(append found-keywords '("CONTACTS_LANGUAGE"))))
(error "Invalid in-buffer setting for CONTACTS_LANGUAGE")))
((string=
"VCARD_VERSION"
(plist-get (cadr this-line) :key))
(if (member (plist-get (cadr this-line) :value) valid-versions)
(progn
(setq org-vcard-active-version
(plist-get (cadr this-line) :value))
(setq found-keywords
(append found-keywords '("VCARD_VERSION"))))
(error "Invalid in-buffer setting for VCARD_VERSION")))))))
(forward-line))
(cond
((not (member "CONTACTS_STYLE" found-keywords))
(setq org-vcard-active-style org-vcard-default-style))
((not (member "CONTACTS_LANGUAGE" found-keywords))
(setq org-vcard-active-language org-vcard-default-language))
((not (member "VCARD_VERSION" found-keywords))
(setq org-vcard-active-version org-vcard-default-version))))))
(defun org-vcard--transfer-write (direction content destination &optional filename)
"During import, write CONTENT to DESTINATION.
DIRECTION must be either 'import or 'export. CONTENT must be a string.
DESTINATION must be either \"buffer\" or \"file\"."
(if (not
(or
(eq 'import direction)
(eq 'export direction)))
(error "DIRECTION must be either 'import or 'export"))
(if (not (stringp content))
(error "Received non-string as CONTENT"))
(let ((the-buffer nil)
(direction-string
(cond
((eq 'import direction)
"Imported")
((eq 'export direction)
"Exported"))))
(cond
((string= "buffer" destination)
(progn
(cond
((eq 'import direction)
(if org-vcard-append-to-existing-import-buffer
(setq the-buffer
(get-buffer-create "*org-vcard-import*"))
(setq the-buffer
(generate-new-buffer "*org-vcard-import*"))))
((eq 'export direction)
(if org-vcard-append-to-existing-export-buffer
(setq the-buffer
(get-buffer-create "*org-vcard-export*"))
(setq the-buffer
(generate-new-buffer "*org-vcard-export*")))))
(set-buffer the-buffer)
(insert (string-as-multibyte content))
(message
(concat
direction-string
" contacts data to buffer '"
(buffer-name the-buffer)
"'."))))
((string= "file" destination)
(let ((filename
(if filename
filename
(read-file-name
"Destination filename? "
default-directory
(cond
((eq 'import direction)
org-vcard-default-import-file)
((eq 'export direction)
org-vcard-default-export-file))
nil))))
(with-temp-buffer
(insert (string-as-multibyte content))
(when (file-writable-p filename)
(write-region
(point-min)
(point-max)
filename)))
(message
(concat
direction-string
" contacts data to file '"
filename
"'."))))
(t
(error "Invalid DESTINATION type")))))
;;
;; org-vcard-mode menu setup.
;;
(defvar org-vcard--styles-functions (org-vcard--create-styles-functions)
"Available styles and associated import/export functions.")
(defcustom org-vcard-styles-languages-mappings
(org-vcard--create-styles-languages-mappings)
"Details of the available styles and their associated mappings."
:type '(repeat
(list string
(repeat
(list string
(repeat
(list string
(repeat (cons string string))))))))
:group 'org-vcard)
(defun org-vcard--create-org-vcard-mode-menu ()
"Create or recreate the `org-vcard-mode' menu."
(easy-menu-define org-vcard-menu org-vcard-mode-keymap
"Menu bar entry for org-vcard"
`("Org-vCard"
,(let ((export '("Export")))
(let ((style-list '()))
(dolist (style
(sort (mapcar
'car
org-vcard-styles-languages-mappings)
'string<))
(setq style-list
(list (concat "from " style)))
(let ((language-list '()))
(dolist (language
(sort (mapcar
'car
(cadr
(assoc
style
org-vcard-styles-languages-mappings)))
'string<))
(setq language-list (list language))
(let ((version-list '()))
(dolist (version
(sort (mapcar
'car
(cadr
(assoc
language
(cadr
(assoc
style
org-vcard-styles-languages-mappings)))))
'string<))
(setq version-list
(append
version-list
(list
(vector
(concat "to vCard " version)
`(org-vcard-export-via-menu
,style
,language
,version)
t)))))
(setq language-list
(append language-list version-list)))
(setq style-list
(append style-list `(,language-list)))))
(setq export
(append export `(,style-list)))))
export)
,(let ((import '("Import")))
(let ((style-list '()))
(dolist (style
(sort (mapcar
'car
org-vcard-styles-languages-mappings)
'string<))
(setq style-list
(list (concat "to " style)))
(let ((language-list '()))
(dolist (language
(sort (mapcar
'car
(cadr
(assoc
style
org-vcard-styles-languages-mappings)))
'string<))
(setq language-list (list language))
(let ((version-list '()))
(dolist (version
(sort (mapcar
'car
(cadr
(assoc
language
(cadr
(assoc
style
org-vcard-styles-languages-mappings)))))
'string<))
(setq version-list
(append version-list
(list
(vector
(concat "from vCard " version)
`(org-vcard-import-via-menu
,style
,language
,version)
t)))))