aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d/lisp/my/my-bbdb.el
blob: 80661cd2e3d52c110dccac5d73ae3c90fbade54b (plain) (blame)
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
;;; my-bbdb.el -- Extensions for bbdb -*- lexical-binding: t -*-

;; Copyright (C) 2023 Free Software Foundation.

;; Author: Yuchen Pei <id@ypei.org>
;; Package-Requires: ((emacs "28.2"))

;; This file is part of dotfiles.

;; dotfiles is free software: you can redistribute it and/or modify it under
;; the terms of the GNU Affero General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; dotfiles 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 Affero General
;; Public License for more details.

;; You should have received a copy of the GNU Affero General Public
;; License along with dotfiles.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Extensions for bbdb.

;;; Code:


;; overrides bbdb-read-record in bbdb-com.el
(defun my-bbdb-read-record (&optional first-and-last)
  "Read and return a new BBDB record.
Does not insert it into the database or update the hashtables,
but does ensure that there will not be name collisions."
  (bbdb-editable)
  (let ((record (bbdb-empty-record)))
    ;; name
    (let (name)
      (bbdb-error-retry
       (setq name (bbdb-read-name first-and-last))
       (bbdb-check-name (car name) (cdr name)))
      (bbdb-record-set-firstname record (car name))
      (bbdb-record-set-lastname record (cdr name)))

    ;; akas
    (bbdb-record-set-aka record
			                   (bbdb-split 'aka (bbdb-read-string "AKAs: ")))

    ;; urls
    (bbdb-record-set-xfield record 'url (bbdb-read-string "urls: "))

    ;; mail
    (bbdb-record-set-mail
     record (bbdb-split 'mail (bbdb-read-string "E-Mail Addresses: ")))

    ;; notes
    (bbdb-record-set-xfield record 'notes (bbdb-read-string "notes: "))

    record))

;; overrides bbdb-create
;;;###autoload
(defun my-bbdb-create (record)
  "Add a new RECORD to BBDB.
When called interactively read all relevant info.
Do not call this from a program; call `bbdb-create-internal' instead."
  (interactive (list (bbdb-read-record current-prefix-arg)))
  (bbdb-change-record record)
  (bbdb-display-records (list record) nil nil t))

(defun my-bbdb-form (name)
  "Insert or update a bbdb record using a form.

If NAME exists in bbdb, update. Otherwise insert."
  (interactive
   (list (completing-read "Name: " (mapcar 'bbdb-record-name (bbdb-records)))))
  (let* ((record (or (car (bbdb-gethash name))
                     (bbdb-empty-record)))
         (new-first-name) (new-last-name))
    (switch-to-buffer-other-window "*Form BBDB*")
    (kill-all-local-variables)
    (let ((inhibit-read-only t)) (erase-buffer))
    (remove-overlays)
    (widget-insert "BBDB form\n\n")
    (setq my-bbdb-widget-first-name
          (widget-create 'editable-field :format "First Name: %v"
                         (or (bbdb-record-firstname record)
                             (car (bbdb-divide-name name)))))
    (setq my-bbdb-widget-last-name
          (widget-create 'editable-field :format "Last Name: %v"
                         (or (bbdb-record-lastname record)
                             (cdr (bbdb-divide-name name)))))
    (setq my-bbdb-widget-aka
          (widget-create 'editable-field
                         :format "Aliases: %v"
                         :value (bbdb-concat
                                 'aka
                                 (bbdb-record-aka record))))
    (setq my-bbdb-widget-org
          (widget-create 'editable-field
                         :format "Organisations: %v"
                         :value (bbdb-concat
                                 'organization
                                 (bbdb-record-organization record))))
    (setq my-bbdb-widget-mail
          (widget-create 'editable-field
                         :format "Email addresses: %v"
                         :value (bbdb-concat 'mail (bbdb-record-mail record))))
    (widget-insert "Phone numbers\n")
    (setq my-bbdb-widget-phone
          (widget-create 'editable-list
                         :entry-format "%i %d %v"
                         :value
                         (mapcar (lambda (phone-info)
                                   (list (aref phone-info 0)
                                         (aref phone-info 1)))
                                 (bbdb-record-phone record))
                         '(group
                           (menu-choice
                            :tag "Type"
                            (choice-item :value "cell")
                            (choice-item :value "work")
                            (choice-item :value "home")
                            (choice-item :value "other"))
                           (editable-field :format "number: %v" ""))))
    (widget-create
     'push-button
     :notify (lambda (&rest _)
               (setq new-first-name (widget-value my-bbdb-widget-first-name))
               (setq new-last-name (widget-value my-bbdb-widget-last-name))
               (unless (equal name
                              (bbdb-concat 'name-first-last new-first-name
                                           new-last-name))
                 (bbdb-check-name new-first-name new-last-name))
               (bbdb-record-set-name record new-first-name new-last-name)
               (bbdb-record-set-organization
                record (bbdb-split 'organization (widget-value my-bbdb-widget-org)))
               (bbdb-record-set-mail
                record (bbdb-split 'mail (widget-value my-bbdb-widget-mail)))
               (bbdb-record-set-aka
                record (bbdb-split 'aka (widget-value my-bbdb-widget-aka)))
               (bbdb-record-set-phone
                record (mapcar
                        (lambda (pair)
                          (vector (car pair) (cadr pair)))
                        (widget-value my-bbdb-widget-phone)))
               (bbdb-change-record record)
               (bbdb-display-records (list record) nil nil t))
     "Submit"))
  (use-local-map widget-keymap)
  (widget-setup)
  (goto-char (point-min)))

(defun my-bbdb-parse-record (record)
  (list
   (cons 'Name (bbdb-record-name record))
   (cons 'First-name (bbdb-record-firstname record))
   (cons 'Last-name (bbdb-record-lastname record))
   (cons 'Organisation (car (bbdb-record-organization record)))
   (cons 'Notes (bbdb-record-xfield record 'notes))
   (cons 'Website (bbdb-record-xfield record 'url))))

(defun my-bbdb-insert-record-to-org (parsed)
  (goto-char (point-min))
  (insert "\n* "(alist-get 'Name parsed))
  (insert "\n" (or
                (and (alist-get 'Notes parsed)
                     (format "- %s" (alist-get 'Notes parsed)))
                ""))
  (dolist (pair parsed)
    (when (and (not (member (car pair) '(Name Notes))) (cdr pair))
      (org-set-property (symbol-name (car pair)) (cdr pair)))))

(defun my--bbdb-to-org ()
  "ONLY do this in a new buffer!"
  (dolist (record (bbdb-records))
    (my-bbdb-insert-record-to-org (my-bbdb-parse-record record))))

(defun my-bbdb-done ()
  "Save and quit bbdb window"
  (interactive)
  (bbdb-save) (quit-window))

(defun my-bbdb-all ()
  "Dispaly all bbdb records"
  (interactive)
  (bbdb ""))

(provide 'my-bbdb)
;;; my-bbdb.el ends here