-
Notifications
You must be signed in to change notification settings - Fork 0
/
helm-addressbook.el
201 lines (181 loc) · 8.43 KB
/
helm-addressbook.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
;;; helm-addressbook.el --- Helm for addressbook bookmarks. -*- lexical-binding: t -*-
;; Copyright (C) 2012 ~ 2017 Thierry Volpiatto <[email protected]>
;; Version: 1.0
;; Package-Requires: ((helm "2.8.2") (addressbook-bookmark "1.0") (cl-lib "0.5") (emacs "24.4"))
;; URL: https://github.com/emacs-helm/helm-addressbook
;; 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:
;; Needs https://github.com/thierryvolpiatto/addressbook-bookmark as dependency.
;;; Code:
(require 'cl-lib)
(require 'bookmark)
(require 'helm-bookmark)
(declare-function addressbook-bookmark-edit "ext:addressbook-bookmark.el" (bookmark))
(declare-function message-buffers "message.el")
(declare-function addressbook-set-mail-buffer-1 "ext:addressbook-bookmark.el"
(&optional bookmark-name append cc))
(declare-function addressbook-bookmark-set-1 "ext:addressbook-bookmark.el" (&optional contact))
(defcustom helm-addressbook-actions
'(("Show Contact(s)"
. (lambda (candidate)
(let* ((contacts (helm-marked-candidates))
(current-prefix-arg helm-current-prefix-arg))
(bookmark-jump
(helm-bookmark-get-bookmark-from-name (car contacts)))
(helm-aif (cdr contacts)
(let ((current-prefix-arg '(4)))
(cl-loop for bmk in it do
(bookmark-jump
(helm-bookmark-get-bookmark-from-name bmk))))))))
("Mail To" . helm-addressbook-send-mail-1)
("Mail Cc" . (lambda (_candidate)
(helm-addressbook-send-mail-1 nil 'cc)))
("Mail Bcc" . (lambda (_candidate)
(helm-addressbook-send-mail-1 nil 'bcc)))
("Edit Bookmark"
. (lambda (candidate)
(let ((bmk (helm-bookmark-get-bookmark-from-name
candidate)))
(addressbook-bookmark-edit
(assoc bmk bookmark-alist)))))
("Delete bookmark(s)" . helm-delete-marked-bookmarks)
("Insert Email at point"
. (lambda (candidate)
(let* ((bmk (helm-bookmark-get-bookmark-from-name
candidate))
(mlist (split-string
(assoc-default
'email (assoc bmk bookmark-alist))
", ")))
(insert
(if (> (length mlist) 1)
(helm-comp-read
"Insert Mail Address: " mlist :must-match t)
(car mlist))))))
("Show annotation"
. (lambda (candidate)
(let ((bmk (helm-bookmark-get-bookmark-from-name
candidate)))
(bookmark-show-annotation bmk))))
("Edit annotation"
. (lambda (candidate)
(let ((bmk (helm-bookmark-get-bookmark-from-name
candidate)))
(bookmark-edit-annotation bmk))))
("Show Google map"
. (lambda (candidate)
(let* ((bmk (helm-bookmark-get-bookmark-from-name
candidate))
(full-bmk (assoc bmk bookmark-alist)))
(addressbook-google-map full-bmk)))))
"Actions for addressbook bookmarks."
:group 'helm-bookmark
:type '(alist :key-type string :value-type function))
;;; Addressbook.
;;
;;
(defun helm-addressbook--search-mail (pattern)
"Search function to search PATTERN for helm-addressbook."
(helm-awhile (next-single-property-change (point) 'email)
(goto-char it)
(end-of-line)
(when (string-match pattern
(get-text-property
0 'email (buffer-substring
(pos-bol) (pos-eol))))
(cl-return
(+ (point) (match-end 0))))))
(defun helm-addressbook--search-group (pattern)
"Search function to search PATTERN for helm-addressbook."
(helm-awhile (next-single-property-change (point) 'group)
(goto-char it)
(end-of-line)
(when (string-match pattern
(get-text-property
0 'group (buffer-substring
(pos-bol) (pos-eol))))
(cl-return
(+ (point) (match-end 0))))))
(defclass helm-addressbook-class (helm-source-in-buffer)
((init :initform (lambda ()
(require 'addressbook-bookmark)
(bookmark-maybe-load-default-file)
(helm-init-candidates-in-buffer
'global
(cl-loop for b in (helm-addressbook-setup-alist)
collect (propertize b
'email (bookmark-prop-get b 'email)
'group (bookmark-prop-get b 'group))))))
(search :initform '(helm-addressbook--search-group
helm-addressbook--search-mail))
(persistent-action :initform
(lambda (candidate)
(let ((bmk (helm-bookmark-get-bookmark-from-name
candidate)))
(if (and (get-buffer-window addressbook-buffer-name 'visible)
(string= bmk (with-current-buffer addressbook-buffer-name
(save-excursion
(search-forward "^Name: " nil t)
(car (addressbook-get-contact-data))))))
(kill-buffer addressbook-buffer-name)
(when (buffer-live-p (get-buffer addressbook-buffer-name))
(kill-buffer addressbook-buffer-name))
(bookmark--jump-via bmk 'switch-to-buffer)))))
(persistent-help :initform "Show contact - Prefix with C-u to append")
(mode-line :initform (list "Contact(s)" helm-mode-line-string))
(filtered-candidate-transformer :initform
'(helm-adaptive-sort
helm-highlight-bookmark))
(action :initform 'helm-addressbook-actions))
"Helm class to build helm-addressbook source.")
(defun helm-addressbook-send-mail-1 (_candidate &optional cc)
"Generic action to send mail from helm-addressbook.
Argument _CANDIDATE is unused and argument CC can be one of `cc' or
`bcc'."
(let* ((contacts (helm-marked-candidates))
(bookmark (helm-bookmark-get-bookmark-from-name
(car contacts)))
(append (message-buffers)))
(addressbook-set-mail-buffer-1 bookmark append cc)
(helm-aif (cdr contacts)
(cl-loop for bmk in it do
(addressbook-set-mail-buffer-1
(helm-bookmark-get-bookmark-from-name bmk) 'append cc)))))
(defun helm-addressbook-setup-alist ()
"Specialized filter function for addressbook bookmarks."
(helm-bookmark-filter-setup-alist 'helm-bookmark-addressbook-p))
(defvar helm-source-addressbook
(helm-make-source "Bookmark Addressbook" 'helm-addressbook-class)
"Main source for helm-addressbook.")
(defvar helm-source-addressbook-set
(helm-build-dummy-source "Addressbook add contact"
:filtered-candidate-transformer
(lambda (_candidates _source)
(list (or (and (not (string= helm-pattern ""))
helm-pattern)
"Enter a contact name to record")))
:action (lambda (candidate)
(addressbook-bookmark-set-1 candidate)))
"Source to add contacts from helm-addressbook.")
;;;###autoload
(defun helm-addressbook-bookmarks ()
"Preconfigured helm for addressbook bookmarks.
Need addressbook-bookmark package as dependencie."
(interactive)
(helm :sources '(helm-source-addressbook
helm-source-addressbook-set)
:prompt "Search Contact: "
:buffer "*helm addressbook*"
:default (list (thing-at-point 'symbol)
(buffer-name helm-current-buffer))))
(provide 'helm-addressbook)
;;; helm-addressbook.el ends here