-
Notifications
You must be signed in to change notification settings - Fork 11
/
ebdb-com.el
3373 lines (3055 loc) · 125 KB
/
ebdb-com.el
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
;;; ebdb-com.el --- User-level commands of EBDB -*- lexical-binding: t; -*-
;; Copyright (C) 2016-2024 Free Software Foundation, Inc.
;; Author: Eric Abrahamsen <[email protected]>
;; Keywords: convenience, mail
;; 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This file contains most of the user-level interactive commands for
;; EBDB, including the *EBDB* buffer and ebdb-mode.
;;; Code:
(require 'ebdb)
(require 'ebdb-format)
(require 'mailabbrev)
(eval-and-compile
(autoload 'build-mail-aliases "mailalias")
(autoload 'browse-url-url-at-point "browse-url")
(autoload 'eieio-customize-object "eieio-custom")
(autoload 'message-goto-body "message"))
(require 'crm)
(defvar ebdb-crm-local-completion-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map crm-local-completion-map)
(define-key map " " #'self-insert-command)
map)
"Keymap used for EBDB crm completions.")
(defvar-local ebdb-customization-record nil
"Variable holding the record whose field is being customized.")
(defvar-local ebdb-customization-field nil
"Variable holding the field being customized.")
(defvar-local ebdb-customization-name-p nil
"Variable indicating if the record's main name is being customized.")
;; Customizations for display routines
(defgroup ebdb-record-display nil
"Variables that affect the display of EBDB records"
:group 'ebdb)
(defcustom ebdb-dedicated-window nil
"Make *EBDB* window a dedicated window.
Allowed values include nil (not dedicated) \\='ebdb (weakly dedicated)
and t (strongly dedicated)."
:type '(choice (const :tag "EBDB window not dedicated" nil)
(const :tag "EBDB window weakly dedicated" ebdb)
(const :tag "EBDB window strongly dedicated" t)))
(defcustom ebdb-join-atomic-windows t
"When non-nil, have EBDB buffers join atomic windows.
Atomic windows are window groups that are treated as single
windows by other splitting/display code."
:type 'boolean)
(defcustom ebdb-default-window-size 0.4
"Default size of EBDB popup windows.
Specified as a float between 0 and 1, which is interpreted as a
fractional size of the window that is being split to make way for
the *EBDB* buffer."
:type 'float)
(defcustom ebdb-fill-field-values t
"When non-nil, fill long field values."
:type '(choice (const :tag "Always fill" nil)
(const :tag "Never fill" t)))
(defcustom ebdb-image-additional-plist '(:max-width (6 . em))
"Property list added to image data before display.
The value should be a property list containing keys meaningful
for image display; see the info node `(elisp)Image Descriptors'
for details.
Most often this option will be used to control the sizing of
displayed images, using one or more of the :width, :height,
:max-width, :max-height or :scale keys. The first four keys take
either an integer value, representing number of pixels, or a pair
\"(VALUE . em)\", meaning a number of units relative to the
current font size. The :scale value should be a number (greater
than or less than 1) representing a multiple of the original
image size."
:type 'plist
:options '(:width :height :max-width :max-height :scale
:ascent :margin :relief :rotation
:transform-smoothing :mask))
(defcustom ebdb-user-menu-commands nil
"User defined menu entries which should be appended to the EBDB menu.
This should be a list of menu entries.
When set to a function, it is called with two arguments RECORD and FIELD
and it should either return nil or a list of menu entries.
Used by `ebdb-mouse-menu'."
:type 'sexp)
(defcustom ebdb-display-hook nil
"Hook run after the *EBDB* is filled in."
:type 'hook)
(defcustom ebdb-create-update-roles t
"When non-nil, offer to create and/or update role fields for records.
For instance, note when the domain field of a newly-created
organization matches existing records' mail fields, and offer to
create roles for those records, etc."
:group 'ebdb-record-edit
:type 'boolean
:version "0.7")
;; Faces for font-lock
(defgroup ebdb-faces nil
"Faces used by EBDB."
:group 'ebdb
:group 'faces)
(defcustom ebdb-name-face-alist '((ebdb-record-person . ebdb-person-name)
(ebdb-record-organization . ebdb-organization-name)
(ebdb-record-mailing-list . ebdb-mailing-list-name))
"Alist of record class types to the face names.
Faces are used to font-lock their names in the *EBDB* buffer."
:type '(repeat (cons (ebdb-record :tag "Record type") (face :tag "Face"))))
(defface ebdb-person-name
'((t (:inherit font-lock-function-name-face)))
"Face used for EBDB person names."
:group 'ebdb-faces)
(defface ebdb-organization-name
'((t (:inherit font-lock-type-face)))
"Face used for EBDB organization names."
:group 'ebdb-faces)
(defface ebdb-mailing-list-name
'((t (:inherit font-lock-constant-face)))
"Face used for EBDB mailing list names."
:group 'ebdb-faces)
(defface ebdb-marked
'((t (:background "LightBlue")))
"Face used for currently-marked records."
:group 'ebdb-faces)
(defface ebdb-label
'((t (:inherit font-lock-variable-name-face)))
"Face used for EBDB field labels."
:group 'ebdb-faces)
(defface ebdb-field-url
'((t (:inherit link)))
"Face used for clickable links/URLs in field values."
:group 'ebdb-faces)
(defface ebdb-field-hidden
'((t (:inherit font-lock-constant-face)))
"Face used for placeholder text for fields that aren't actually displayed.")
(defface ebdb-phone-default '((t :inherit default))
"Base face used for all phone fields."
:group 'ebdb-faces)
(defface ebdb-address-default '((t :inherit default))
"Base face used for all address fields."
:group 'ebdb-faces)
(defface ebdb-mail-default '((t :inherit default))
"Base face used for all mail fields."
:group 'ebdb-faces)
(defface ebdb-defunct
'((t :foreground "gray80"))
"Face used to display defunct things."
:group 'ebdb-faces)
(defface ebdb-role-defunct
'((t :inherit ebdb-defunct))
"Face used to display defunct roles."
:group 'ebdb-faces)
(defface ebdb-mail-defunct
'((t :inherit ebdb-defunct ebdb-mail-default))
"Face used to display a defunct mail address."
:group 'ebdb-faces)
(defface ebdb-mail-primary
'((t (:inherit font-lock-builtin-face ebdb-mail-default)))
"Face used to display a record's primary mail address."
:group 'ebdb-faces)
(defface ebdb-db-char
'((t (:inherit shadow)))
"Face used to display a databases's identifying character string."
:group 'ebdb-faces)
(defvar ebdb-buffer-name "EBDB"
"Default name of the EBDB buffer, without surrounding asterisks.")
;;; Buffer-local variables for the database.
(defvar-local ebdb-records nil
"EBDB records list.
In the *EBDB* buffers it includes the records that are actually displayed
and its elements are (RECORD DISPLAY-FORMAT MARKER-POS MARK).")
(defvar-local ebdb-search-history nil
"A list of lists of previously-displayed EBDB records in this buffer.
For each search in a user-initiated EBDB buffer, the
previously-displayed EBDB records are pushed here, as a list of
UUIDs. ebdb-mode keybindings make it possible to pop back to
previous records.")
(defvar ebdb-modeline-info (make-vector 2 nil)
"Precalculated mode line info for EBDB commands.
This is a vector [INVERT-M INVERT].
INVERT-M is the mode line info if variable `ebdb-search-invert' is non-nil.")
;; Note about the arg RECORDS of various EBDB commands:
;; - Usually, RECORDS is a list of records. (Interactively,
;; this list of records is set up by `ebdb-do-records'.)
;; - If these commands are used, e.g., in `ebdb-create-hook' or
;; `ebdb-change-hook', they will be called with one arg, a single record.
;; So depending on context the value of RECORDS will be a single record
;; or a list of records, and we want to handle both cases.
;; So we pass RECORDS to `ebdb-record-list' to handle both cases.
(defun ebdb-record-list (records &optional full)
"Ensure that RECORDS is a list of records.
If RECORDS is a single record turn it into a list.
If FULL is non-nil, assume that RECORDS include display information."
(if records
(if full
(if (eieio-object-p (car records)) (list records) records)
(if (eieio-object-p records) (list records) records))))
;; Note about EBDB prefix commands: `ebdb-search-invert' is a fake
;; prefix commands. They need not precede the main commands.
(defun ebdb-search-invert-p ()
"Return variable `ebdb-search-invert' and set it to nil.
To set it again, use command `ebdb-search-invert'."
(let ((result ebdb-search-invert))
(setq ebdb-search-invert nil)
(aset ebdb-modeline-info 0 nil)
(aset ebdb-modeline-info 1 nil)
result))
;;;###autoload
(defun ebdb-search-invert (&optional arg)
"Toggle inversion of the next search command.
With prefix ARG a positive number, invert next search.
With prefix ARG a negative number, do not invert next search."
(interactive "P")
(setq ebdb-search-invert
(or (and (numberp arg) (< 0 arg))
(and (not (numberp arg)) (not ebdb-search-invert))))
(aset ebdb-modeline-info 0 (if ebdb-search-invert "inv"))
(aset ebdb-modeline-info 1 (if ebdb-search-invert
(substitute-command-keys
"\\<ebdb-mode-map>\\[ebdb-search-invert]")))
(ebdb-prefix-message))
(defun ebdb-prefix-message ()
"Display a message about selected EBDB prefix commands."
(let ((msg (ebdb-concat " " (elt ebdb-modeline-info 0)
(elt ebdb-modeline-info 1))))
(unless (string= "" msg) (message "%s" msg))))
;;;###autoload
(defun ebdb-do-records (&optional full)
"Return list of records to operate on.
Normally this list includes only the current record, but if any
records in the current buffer are marked, they are returned
instead. If FULL is non-nil, the list of records includes
display information."
(let* ((marked (seq-filter (lambda (r) (nth 3 r)) ebdb-records))
(recs (or marked (list (ebdb-current-record t)))))
(setq recs (seq-filter (lambda (r) (eieio-object-p (car r))) recs))
(if full recs (mapcar #'car recs))))
;;; Keymap
(defvar ebdb-mode-map
(let ((km (make-sparse-keymap)))
(define-key km (kbd "!") #'ebdb-search-invert)
(define-key km (kbd "RET") #'ebdb-record-action)
(define-key km (kbd "A") #'ebdb-mail-aliases)
(define-key km (kbd "c") #'ebdb-create-record)
(define-key km (kbd "C") #'ebdb-create-record-extended)
(define-key km (kbd "R") #'ebdb-create-record-and-role)
(define-key km (kbd "e") #'ebdb-edit-field)
(define-key km (kbd "E") #'ebdb-edit-field-customize)
(define-key km (kbd ";") #'ebdb-edit-foo)
(define-key km (kbd "n") #'ebdb-next-record)
(define-key km (kbd "p") #'ebdb-prev-record)
(define-key km (kbd "N") #'ebdb-next-field)
(define-key km (kbd "TAB") #'ebdb-next-field)
(define-key km (kbd "P") #'ebdb-prev-field)
(define-key km (kbd "<backtab>") #'ebdb-prev-field)
;; Database-related commands
(define-key km (kbd "d c") #'ebdb-copy-records)
(define-key km (kbd "d m") #'ebdb-move-records)
(define-key km (kbd "d e") #'ebdb-customize-database)
(define-key km (kbd "d r") #'ebdb-reload-database)
(define-key km (kbd "d d") #'ebdb-disable-database)
(define-key km (kbd "r") #'ebdb-reformat-records)
(define-key km (kbd "f") #'ebdb-format-to-tmp-buffer)
(define-key km (kbd "F") #'ebdb-format-these-records)
(define-key km (kbd "I") #'ebdb-cite-records-ebdb)
(define-key km (kbd "C-k") #'ebdb-delete-field-or-record)
(define-key km (kbd "i") #'ebdb-insert-field)
(define-key km (kbd "s") #'ebdb-save-ebdb)
(define-key km (kbd "C-x C-s") #'ebdb-save-ebdb)
(define-key km (kbd "t") #'ebdb-toggle-records-format)
(define-key km (kbd "T") #'ebdb-toggle-all-records-format)
;; Marking
(define-key km (kbd "#") #'ebdb-toggle-record-mark)
(define-key km (kbd "M-#") #'ebdb-toggle-all-record-marks)
(define-key km (kbd "C-#") #'ebdb-unmark-all-records)
(define-key km (kbd "o") #'ebdb-omit-records)
(define-key km (kbd "m") #'ebdb-mail)
(define-key km (kbd "M") #'ebdb-mail-each)
(define-key km (kbd "M-d") #'ebdb-dial)
(define-key km (kbd "h") #'ebdb-info)
(define-key km (kbd "?") #'ebdb-help)
;; Copying data
(define-key km (kbd "w r") #'ebdb-copy-records-as-kill)
(define-key km (kbd "w f") #'ebdb-copy-fields-as-kill)
(define-key km (kbd "w m") #'ebdb-copy-mail-as-kill)
(define-key km (kbd "=") #'delete-other-windows)
;; Buffer manipulation
(define-key km (kbd "b c") #'ebdb-clone-buffer)
(define-key km (kbd "b r") #'ebdb-rename-buffer)
;; Search keys
(define-key km (kbd "/ /") #'ebdb)
(define-key km (kbd "/ 1") #'ebdb-search-single-record)
(define-key km (kbd "/ n") #'ebdb-search-name)
(define-key km (kbd "| n") #'ebdb-search-name)
(define-key km (kbd "+ n") #'ebdb-search-name)
(define-key km (kbd "/ o") #'ebdb-search-organization)
(define-key km (kbd "| o") #'ebdb-search-organization)
(define-key km (kbd "+ o") #'ebdb-search-organization)
(define-key km (kbd "/ p") #'ebdb-search-phone)
(define-key km (kbd "| p") #'ebdb-search-phone)
(define-key km (kbd "+ p") #'ebdb-search-phone)
(define-key km (kbd "/ a") #'ebdb-search-address)
(define-key km (kbd "| a") #'ebdb-search-address)
(define-key km (kbd "+ a") #'ebdb-search-address)
(define-key km (kbd "/ m") #'ebdb-search-mail)
(define-key km (kbd "| m") #'ebdb-search-mail)
(define-key km (kbd "+ m") #'ebdb-search-mail)
(define-key km (kbd "/ x") #'ebdb-search-user-fields)
(define-key km (kbd "| x") #'ebdb-search-user-fields)
(define-key km (kbd "+ x") #'ebdb-search-user-fields)
(define-key km (kbd "/ c") #'ebdb-search-modified)
(define-key km (kbd "| c") #'ebdb-search-modified)
(define-key km (kbd "+ c") #'ebdb-search-modified)
(define-key km (kbd "/ t") #'ebdb-search-tags)
(define-key km (kbd "| t") #'ebdb-search-tags)
(define-key km (kbd "+ t") #'ebdb-search-tags)
(define-key km (kbd "/ C") #'ebdb-search-record-class)
(define-key km (kbd "| C") #'ebdb-search-record-class)
(define-key km (kbd "+ C") #'ebdb-search-record-class)
(define-key km (kbd "+ d") #'ebdb-search-duplicates)
(define-key km (kbd "/ D") #'ebdb-search-database)
(define-key km (kbd "| D") #'ebdb-search-database)
(define-key km (kbd "+ D") #'ebdb-search-database)
(define-key km (kbd "C-x n w") #'ebdb-display-all-records)
(define-key km (kbd "C-x n d") #'ebdb-display-current-record)
(define-key km (kbd "^") #'ebdb-search-pop)
(define-key km [mouse-3] #'ebdb-mouse-menu)
(define-key km [mouse-2] (lambda (event)
;; Toggle record format
(interactive "e")
(save-excursion
(posn-set-point (event-end event))
(ebdb-toggle-records-format
(ebdb-do-records t) current-prefix-arg))))
km)
"Keymap for EBDB.
This is a child of `special-mode-map'.")
(defun ebdb-current-record (&optional full)
"Return the record under point.
If FULL is non-nil, return a list of (record formatter
position-marker mark)."
(unless (eq major-mode 'ebdb-mode)
(error "This only works while in EBDB buffers"))
(let ((num (get-text-property (if (and (not (bobp)) (eobp))
(1- (point)) (point))
'ebdb-record-number))
record)
(unless num (error "Not a EBDB record"))
(setq record (nth num ebdb-records))
(if full record (car record))))
(defun ebdb-current-field ()
"Return record field under point."
(unless (ebdb-current-record) (error "Not a EBDB record"))
(or (get-text-property (point) 'ebdb-field)
(get-text-property
(if (eolp)
(previous-single-property-change (point)
'ebdb-field nil
(line-beginning-position))
(next-single-property-change (point)
'ebdb-field nil
(line-end-position)))
'ebdb-field)
(error "No field at point")))
;;; *EBDB* formatting
(defclass ebdb-formatter-ebdb (ebdb-formatter-freeform)
;; This post-format-function only comes into play when the user
;; chooses the EBDB format in `ebdb-format-to-tmp-buffer'.
((post-format-function
:initform #'text-mode))
:documentation
"Abstract formatter base class for *EBDB* buffer(s)."
:abstract t)
(defclass ebdb-formatter-ebdb-oneline (ebdb-formatter-ebdb)
nil
:documentation
"Single line formatter for *EBDB* buffers.")
(defclass ebdb-formatter-ebdb-multiline (ebdb-formatter-ebdb)
nil
:documentation
"Multi-line formatter for *EBDB* buffers.")
(defcustom ebdb-default-multiline-include nil
"A list of field types to include in multiline display.
Valid list values include all field class names (ebdb-field-*),
as well as the shortcuts \\='mail, \\='phone, \\='address, \\='notes, \\='tags,
and \\='role, and the special shortcuts \\='mail-primary,
\\='mail-defunct, \\='mail-not-defunct, \\='role-defunct, and
\\='role-not-defunct.
If this option is set, *only* fields listed here will be
displayed. Also see `ebdb-default-multiline-exclude'."
:type '(repeat symbol)
:group 'ebdb-record-display)
(defcustom ebdb-default-multiline-exclude
'(ebdb-field-uuid
ebdb-field-timestamp ebdb-field-creation-date
mail-defunct role-defunct)
"A list of field types to exclude in multiline display.
Valid list values include all field class names (ebdb-field-*),
as well as the shortcuts \\='mail, \\='phone, \\='address, \\='notes, \\='tags,
and \\='role, and the special shortcuts \\='mail-primary,
\\='mail-defunct, \\='mail-not-defunct, \\='role-defunct, and
\\='role-not-defunct.
If `ebdb-default-multiline-include' is set, this option will be
ignored."
:type '(repeat symbol)
:group 'ebdb-record-display)
(defcustom ebdb-default-multiline-combine
'(ebdb-field-mail ebdb-field-phone)
"A list of field types to combine in the multiline display.
\"Combine\" means that instances of this field class will all be
displayed on one line.
Valid list values include all field class names (ebdb-field-*),
as well as the shortcuts \\='mail, \\='phone, \\='address, \\='notes, \\='tags,
and \\='role, and the special shortcuts \\='mail-primary,
\\='mail-defunct, \\='mail-not-defunct, \\='role-defunct, and
\\='role-not-defunct."
:type '(repeat symbol)
:group 'ebdb-record-display)
(defcustom ebdb-default-multiline-collapse
'(ebdb-field-address)
"A list of field types to collapse in the multiline display.
\"Collapse\" means that only the first line of instances of this
field class will be displayed.
Valid list values include all field class names (ebdb-field-*),
as well as the shortcuts \\='mail, \\='phone, \\='address, \\='notes, \\='tags,
and \\='role, and the special shortcuts \\='mail-primary,
\\='mail-defunct, \\='mail-not-defunct, \\='role-defunct, and
\\='role-not-defunct."
:type '(repeat symbol)
:group 'ebdb-record-display)
(defcustom ebdb-default-multiline-formatter
(make-instance 'ebdb-formatter-ebdb-multiline
:label "multiline formatter"
:include ebdb-default-multiline-include
:exclude ebdb-default-multiline-exclude
:combine ebdb-default-multiline-combine
:collapse ebdb-default-multiline-collapse)
"The default multiline formatter for *EBDB* buffers."
:type 'ebdb-formatter-ebdb-multiline
:group 'ebdb-record-display)
(defcustom ebdb-default-oneline-include '(mail-primary)
"Fields to include in the default oneline view."
:type '(repeat symbol)
:group 'ebdb-record-display)
(defcustom ebdb-default-oneline-formatter
(make-instance 'ebdb-formatter-ebdb-oneline
:label "oneline formatter"
:include ebdb-default-oneline-include)
"The default oneline formatter of *EBDB* buffers."
:type 'ebdb-formatter-ebdb-oneline
:group 'ebdb-record-display)
(defconst ebdb-full-formatter
(make-instance 'ebdb-formatter-ebdb-multiline
:include nil :exclude nil
:combine nil :collapse nil
:label "full formatter")
"Formatter used for displaying all values of a record.
This formatter should not be changed.")
(defun ebdb-available-ebdb-formatters (&optional full-okay)
"A list of formatters available in the *EBDB* buffer.
This list is also used for toggling layouts. Unless FULL-OKAY is
non-nil, do not offer the value of `ebdb-full-formatter' as a
choice: that formatter should be selected explicitly."
(seq-filter
(lambda (f) (and (object-of-class-p f 'ebdb-formatter-ebdb)
(or full-okay
(null (equal f ebdb-full-formatter)))))
ebdb-formatter-tracker))
(defsubst ebdb-formatter-prefix ()
"Select a formatter interactively using the prefix arg."
(cond (current-prefix-arg ebdb-default-oneline-formatter)
(t ebdb-default-multiline-formatter)))
;; *EBDB* buffer formatting.
(cl-defmethod ebdb-record-db-char-string ((record ebdb-record))
"Return a char string indicating RECORDs databases."
(let* ((dbs (ebdb-record-databases record))
(char-string
(concat
(delq nil
(mapcar
(lambda (d)
(when (slot-value d 'buffer-char)
(slot-value d 'buffer-char)))
dbs)))))
(propertize char-string 'face 'ebdb-db-char)))
(cl-defmethod ebdb-fmt-field-label :around ((_fmt ebdb-formatter-ebdb)
_field
_style
&optional _record)
(propertize (cl-call-next-method) 'face 'ebdb-label))
(cl-defmethod ebdb-fmt-field-label ((_fmt ebdb-formatter-ebdb)
(field ebdb-field-phone)
(_style (eql oneline))
&optional _record)
(let ((label (slot-value field 'label)))
;; Don't use `ebdb-field-label' because if the field has no label,
;; we end up outputting something stupid like "phone (phone)".
(concat
"phone"
(when label
(format " (%s)" label)))))
(cl-defmethod ebdb-fmt-field-label ((_fmt ebdb-formatter-ebdb)
(field ebdb-field-address)
(_style (eql oneline))
&optional _record)
(let ((label (slot-value field 'label)))
;; As above.
(concat
"address"
(when label
(format " (%s)" label)))))
(cl-defmethod ebdb-fmt-field-label ((_fmt ebdb-formatter-ebdb)
(field ebdb-field-relation)
_style
&optional record)
;; If FIELD doesn't belong to RECORD, we're showing a reverse
;; relationship.
(let ((rel-id (slot-value field 'rel-uuid)))
(if (and record (equal (ebdb-record-uuid record) rel-id))
(slot-value field 'rel-label)
(ebdb-field-label field))))
(cl-defmethod ebdb-fmt-field :around ((_fmt ebdb-formatter-ebdb)
(field ebdb-field)
_style
(_record ebdb-record))
"Put the \\='ebdb-field text property on FIELD. The value of the
property is the field instance itself."
(let ((str (cl-call-next-method)))
;; If the field fails to produce a string, or produces an empty
;; string, `propertize' will fail to add the 'ebdb-field property.
;; Put in a dummy string to be propertized, otherwise the user
;; will have no way of interacting with the field.
(propertize (if (or (null str)
(string-empty-p str))
"<empty>"
str)
'ebdb-field field)))
(cl-defmethod ebdb-fmt-field ((_fmt ebdb-formatter-ebdb)
(field ebdb-field)
(_style (eql oneline))
(_record ebdb-record))
"Handle the `oneline' style in EBDB buffers.
Print the first line, add an ellipsis, and add a tooltip."
(pcase-let* ((full (ebdb-string field))
(`(,head . ,tail) (split-string full "\n")))
(concat (propertize head 'help-echo full)
(when tail
(propertize "…" 'cursor-intangible t)))))
(cl-defmethod ebdb-fmt-field ((_fmt ebdb-formatter-ebdb)
(field ebdb-field-url)
_style
(_record ebdb-record))
"Add an appropriate face to url fields."
(propertize (ebdb-string field) 'face 'ebdb-field-url))
(cl-defmethod ebdb-fmt-field ((_fmt ebdb-formatter-ebdb)
(_field ebdb-field-obfuscated)
_style
(_record ebdb-record))
(propertize "HIDDEN" 'face 'ebdb-field-hidden))
(cl-defmethod ebdb-fmt-field ((_fmt ebdb-formatter-ebdb)
(field ebdb-field-bank-account)
_style
(_record ebdb-record))
(with-slots (bank-name account-name) field
(format "%s: %s" bank-name account-name)))
(cl-defmethod ebdb-fmt-field ((_fmt ebdb-formatter-ebdb)
(field ebdb-field-relation)
_style
(record ebdb-record))
"Format relation-field FIELD for RECORD.
If FIELD really belongs to RECORD, display the \"other end\" of
the relation. If this RECORD is the \"other end\", display the
record that actually owns the field."
(condition-case nil
(let ((rec (ebdb-record-related record field)))
(ebdb-string rec))
(ebdb-related-unfound "record not loaded")))
(cl-defmethod ebdb-fmt-field ((_fmt ebdb-formatter-ebdb)
(field ebdb-field-passport)
_style
(_record ebdb-record))
(with-slots (country id-number) field
(format "(%s) %s" country id-number)))
(cl-defmethod ebdb-fmt-field ((_fmt ebdb-formatter-ebdb)
(field ebdb-field-mail)
_style
(_record ebdb-record))
"Add an appropriate face to primary and defunct mails."
(let* ((priority (slot-value field 'priority))
(value (ebdb-string field))
(face (cond
((eq priority 'primary) 'ebdb-mail-primary)
((eq priority 'defunct) 'ebdb-mail-defunct)
(t 'ebdb-mail-default))))
(if face
(propertize value 'face face)
value)))
(cl-defmethod ebdb-fmt-field ((_fmt ebdb-formatter-ebdb)
(_field ebdb-field-phone)
_style
(_record ebdb-record))
"Add an appropriate face to phones."
(propertize (cl-call-next-method) 'face 'ebdb-phone-default))
(cl-defmethod ebdb-fmt-field ((_fmt ebdb-formatter-ebdb)
(_field ebdb-field-address)
_style
(_record ebdb-record))
"Add an appropriate face to addresses."
(propertize (cl-call-next-method) 'face 'ebdb-address-default))
(cl-defmethod ebdb-fmt-field ((fmt ebdb-formatter-ebdb)
(field ebdb-field-role)
_style
(record ebdb-record))
(with-slots (mail defunct) field
(let* ((rec-string
(condition-case nil
(ebdb-record-name-string
(ebdb-record-related record field))
(ebdb-related-unfound "record not loaded")))
(value (if mail
(format "%s (%s)"
rec-string
(ebdb-fmt-field fmt mail 'oneline record))
rec-string)))
(if defunct
(propertize value 'face 'ebdb-role-defunct)
value))))
(cl-defmethod ebdb-fmt-field ((_fmt ebdb-formatter-ebdb)
(field ebdb-field-image)
_style
(record ebdb-record))
(if (display-images-p)
(progn
(require 'image)
(apply
#'propertize
" "
;; Cribbed from `insert-image'.
(list 'display (append (ebdb-field-image-get field record)
ebdb-image-additional-plist)
'rear-nonsticky '(display)
'keymap image-map)))
"<img>"))
;;; Record display
(cl-defmethod ebdb-fmt-record ((_fmt ebdb-formatter-ebdb)
(_record ebdb-record))
(concat (cl-call-next-method) "\n"))
(cl-defmethod ebdb-fmt-record-header ((fmt ebdb-formatter-ebdb)
(record ebdb-record)
&optional header-fields)
(let ((record-class (eieio-object-class-name record)))
(concat
(propertize
(concat
(ebdb-record-db-char-string record)
" "
(propertize (ebdb-record-name-string record)
'face (cdr (assoc record-class
ebdb-name-face-alist))))
;; We don't actually ask the name field to format itself, just use
;; the cached canonical name string. We do add the field to the
;; string as a text property, however.
'ebdb-record record-class
'ebdb-field (slot-value record 'name))
(when header-fields
(concat
" - "
(mapconcat (pcase-lambda ((map style inst))
(mapconcat (lambda (f)
(ebdb-fmt-field fmt f style record))
inst " "))
header-fields " "))))))
(cl-defmethod ebdb-fmt-record-header :around ((_fmt ebdb-formatter-ebdb-multiline)
(_record ebdb-record)
&optional _header-fields)
(concat (cl-call-next-method) "\n"))
(cl-defmethod ebdb-fmt-compose-fields ((fmt ebdb-formatter-ebdb-multiline)
(record ebdb-record)
&optional
field-alist _depth)
"Turn FIELD-ALIST into a string.
The FIELD-ALIST structure is that returned by
`ebdb-fmt-collect-fields'. It is an alist with three keys:
\\='class, \\='style, and \\='inst.
This function passes the class and field instances to FMT, which
formats them appropriately, and concatenates them into a
string."
(when field-alist
(let* ((field-pairs
(mapcar
(pcase-lambda ((map style inst class))
;; Field labels,
(cons (ebdb-fmt-field-label
fmt
(if (= 1 (length inst))
(car inst)
class)
style
record)
;; and fields.
(mapconcat
#'identity
(mapcar (lambda (f)
(ebdb-fmt-field fmt f style record))
inst)
", ")))
field-alist))
(max-label-width (apply #'max
(mapcar
(lambda (s)
(string-width (car s)))
field-pairs)))
(label-fmt (format " %%%ds"
max-label-width))
(paragraph-start "[ \t]* [[:alpha:] ]+: ")
(fill-prefix (make-string (+ 3 max-label-width) ? ))
(fill-column (window-body-width)))
(with-temp-buffer
(mapc
(pcase-lambda (`(,label . ,fields))
(let ((start (point)))
(insert
(concat
(format label-fmt label)
": "
fields
"\n"))
(if (string-match-p "\n" fields)
;; If a field value contains newlines, don't try to
;; fill it, just indent. I still think there should
;; be a way to achieve this purely using
;; `fill-region', but I'm not going to worry about it
;; for now.
(indent-region (save-excursion
(goto-char start)
(forward-line)
(point))
(point))
(when ebdb-fill-field-values
(fill-region start (point))))))
field-pairs)
(buffer-string)))))
(cl-defmethod ebdb-fmt-compose-fields ((fmt ebdb-formatter-ebdb-oneline)
(record ebdb-record)
&optional field-list _depth)
(concat
(when field-list
(concat
" - "
(mapconcat (pcase-lambda ((map inst style))
(mapconcat (lambda (f) (ebdb-fmt-field fmt f style record))
inst " "))
field-list ", ")))))
;;; This actually takes a &context on major-mode.
(cl-defgeneric ebdb-make-buffer-name ()
"Return the buffer to be used by EBDB.
This examines the current major mode, and makes a decision from
there. The result is passed to `with-current-buffer', so a
buffer object or a buffer name are both acceptable.")
(cl-defmethod ebdb-make-buffer-name (&context (major-mode ebdb-mode))
"If we're already in a ebdb-mode buffer, continue using that
buffer."
(current-buffer))
;; Apparently this is the only way to make a catch-all method with
;; &context.
(cl-defmethod ebdb-make-buffer-name ()
"If we're in a totally unrelated buffer, use the value of
`ebdb-buffer-name'."
(format "*%s*" ebdb-buffer-name))
(cl-defgeneric ebdb-popup-window (major-mode)
"Return a spec for how to pop up a window on an *EBDB* buffer.
This generic function dispatches on the current value of
major-mode. The return value should be a three-element list
of (window split direction), in which WINDOW is the window to
split, SPLIT is either an integer, specifying number of
rows/columns, or a float specifying what percentage of window
real estate the pop-up should occupy, and DIRECTION is one of the
symbols `left', `right', `above' or `below'. SPLIT can be nil,
in which case the value of `ebdb-default-window-size' will be
used. DIRECTION can also be nil, in which case the direction
will either be `right' or `below', depending on the height and
width of the window to be split.
Alternately, the entire return value can be nil, which means
continue using the current window.")
(cl-defmethod ebdb-popup-window (&context (major-mode ebdb-mode))
"When popping up from an existing *EBDB* buffer, just reuse the window.
Ie, don't pop up at all."
nil)
(cl-defmethod ebdb-popup-window ()
"Return a default pop-up spec for an unspecified mode.
If there's no specialization for the current mode, default to
splitting the current window, using `ebdb-default-window-size'."
(list (get-buffer-window) ebdb-default-window-size))
(defun ebdb-display-records (records &optional fmt append
select pop buf)
"Display RECORDS using FMT.
If APPEND is non-nil append RECORDS to the already displayed
records. Otherwise RECORDS overwrite the displayed records.
SELECT and POP are passed directly to `ebdb-pop-up-window'. BUF
indicates which *EBDB* buffer to use, or nil to generate a buffer
name based on the current major mode."
;; All functions that call `ebdb-display-records' set the "fmt"
;; argument, but that's not guaranteed.
(unless fmt
(setq fmt ebdb-default-multiline-formatter))
;; `ebdb-make-buffer-name' is a generic function that
;; dispatches on the current major mode.
(let ((target-buffer (or buf (ebdb-make-buffer-name)))
;; When appending, we want point to end up on the first of the
;; appended records. Save the uuid, and later point a marker
;; at it. Mostly useful for `follow-related'.
(target-record-uuid (when records
(ebdb-record-uuid (car records))))
target-record-marker)
(with-current-buffer (get-buffer-create target-buffer)
;; If we are appending RECORDS to the ones already displayed,
;; then first remove any duplicates, and then sort them.
(if append
(let ((existing (mapcar #'car ebdb-records)))
(setq records
(sort (delete-dups (append records existing))
(lambda (x y) (ebdb-record-lessp x y))))))
(unless (derived-mode-p 'ebdb-mode)
(ebdb-mode))
(setq ebdb-records
(mapcar (lambda (r)
(let ((m (make-marker)))
(when (string= target-record-uuid
(ebdb-record-uuid r))
(setq target-record-marker m))
(list r fmt m nil)))
records))
(ebdb-pop-up-window target-buffer select pop)
(unless (or ebdb-silent-internal ebdb-silent)
(message "Formatting EBDB..."))
(let ((record-number 0)
(inhibit-read-only t)
start)
(erase-buffer)
(insert (ebdb-fmt-header fmt records))
(dolist (record ebdb-records)
(setq start (set-marker (nth 2 record) (point)))
(insert (ebdb-fmt-record fmt (car record)))
(put-text-property start (point) 'ebdb-record-number record-number)
(cl-incf record-number))
(insert (ebdb-fmt-footer fmt records))
(run-hooks 'ebdb-display-hook))
(unless (or ebdb-silent-internal ebdb-silent)
(message "Formatting EBDB...done."))
(set-buffer-modified-p nil)
(goto-char (or target-record-marker (point-min)))
(when (window-live-p (get-buffer-window))
(with-selected-window (get-buffer-window)
(recenter))))))
(defun ebdb-undisplay-records (&optional buffer)
"Undisplay records in *EBDB* BUFFER, leaving the buffer empty.
If BUFFER is nil, use the *EBDB* buffer associated with the
current buffer."
(let ((buf (get-buffer (or buffer (ebdb-make-buffer-name)))))
(when buf
(with-current-buffer buf
(when (eq major-mode 'ebdb-mode)
(let ((inhibit-read-only t))
(erase-buffer))
(setq ebdb-records nil)
(set-buffer-modified-p nil))))))
(defun ebdb-redisplay-all-records (_ignore-auto _noconfirm)
"Used as the value of `revert-buffer-function' in *EBDB* buffers."
(let ((recs (mapcar #'car ebdb-records)))
(ebdb-undisplay-records)
(ebdb-display-records recs)))
(cl-defgeneric ebdb-redisplay-record (record action full-record)
"Redisplay RECORD in current buffer, as specified by ACTION.