-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathebdb-complete.el
224 lines (198 loc) · 8.54 KB
/
ebdb-complete.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
;;; ebdb-complete.el --- Completion functionality for EBDB -*- lexical-binding: t; -*-
;; Copyright (C) 2017-2024 Free Software Foundation, Inc.
;; Author: Feng Shu <[email protected]>
;; Maintainer: Eric Abrahamsen <[email protected]>
;; Keywords: mail, convenience
;; 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 the function `ebdb-complete' which pops
;; up a full EBDB window as email-chooser, typically in mail
;; composition buffers.
;; The simplest installation method is to call:
;; (ebdb-complete-enable)
;; This will bind TAB in message-mode and mail-mode to
;; `ebdb-complete', which will pop up a full EBDB buffer as a contact
;; chooser.
;;
;; This can also be done manually, e.g.:
;;
;; (define-key ebdb-mode-map "q" 'ebdb-complete-bury-ebdb-buffer)
;; (define-key ebdb-mode-map "\C-c\C-c" 'ebdb-complete-push-mail)
;; (define-key ebdb-mode-map (kbd "RET") 'ebdb-complete-push-mail-and-bury-ebdb-buffer)
;; (define-key message-mode-map "\t" 'ebdb-complete-message-tab)
;; (define-key mail-mode-map "\t" 'ebdb-complete-message-tab)
;;; Code:
(require 'ebdb-com)
(require 'message)
(require 'sendmail)
;;;###autoload
(defvar ebdb-complete-info (make-hash-table)
"A hashtable recording buffer, buffer-window and window-point")
(defun ebdb-complete-push-mail (records &optional _ arg)
"Push email-address(es) of `records' to buffer in `ebdb-complete-info'."
(interactive (list (ebdb-do-records) nil
(or (consp current-prefix-arg)
current-prefix-arg)))
(setq records (ebdb-record-list records))
(if (not records)
(message "No records")
(let ((to (mapconcat
(lambda (r)
(ebdb-dwim-mail r (ebdb-record-one-mail r arg)))
records ", "))
(buffer (gethash :buffer ebdb-complete-info)))
(when buffer
(with-current-buffer buffer
(when (not (string= "" to))
(when (save-excursion
(let* ((end (point))
(begin (line-beginning-position))
(string (buffer-substring-no-properties
begin end)))
(and (string-match-p "@" string)
(not (string-match-p ", *$" string)))))
(insert ", "))
(insert to)
(message "%s, will be push to buffer: \"%s\"" to buffer))
(puthash :window-point (point) ebdb-complete-info))))))
(define-obsolete-function-alias
'ebdb-complete-quit-window
#'ebdb-complete-bury-ebdb-buffer "1.0.0")
(defun ebdb-complete-bury-ebdb-buffer ()
"Hide EBDB buffer and switch to message buffer.
Before switch, this command will do some clean jobs."
(interactive)
;; Hide header line in EBDB window.
(with-current-buffer (ebdb-make-buffer-name)
(setq header-line-format nil))
;; Hide ebdb buffer and update window point in Message window.
(let ((buffer (gethash :buffer ebdb-complete-info))
(window (gethash :window ebdb-complete-info))
(window-point (gethash :window-point ebdb-complete-info)))
(when (and (buffer-live-p buffer)
(numberp window-point))
(switch-to-buffer buffer)
(set-window-point
(if (window-live-p window)
window
(get-buffer-window))
window-point)))
;; Clean hashtable `ebdb-complete-info'
(setq ebdb-complete-info (clrhash ebdb-complete-info)))
(define-obsolete-function-alias
'ebdb-complete-push-mail-and-quit-window
#'ebdb-complete-push-mail-and-bury-ebdb-buffer "1.0.0")
(defun ebdb-complete-push-mail-and-bury-ebdb-buffer ()
"Push email-address to Message window and hide EBDB buffer."
(interactive)
(if (gethash :buffer ebdb-complete-info)
(progn (call-interactively 'ebdb-complete-push-mail)
(ebdb-complete-bury-ebdb-buffer))
(message "Invalid push buffer, Do nothing!!")))
(defun ebdb-complete-grab-word ()
"Grab word at point, which used to build search string."
(buffer-substring
(point)
(save-excursion
(skip-syntax-backward "w_")
(point))))
;;;###autoload
(defun ebdb-complete ()
"Open EBDB window as an email-address selector,
if Word at point is found, EBDB will search this word
and show search results in EBDB window. This command
only useful in Message buffer."
(interactive)
(let ((buffer (current-buffer))
prefix-string)
;; Update `ebdb-complete-info'
(if (or (derived-mode-p 'message-mode)
(derived-mode-p 'mail-mode))
(progn
(setq prefix-string (ebdb-complete-grab-word))
(puthash :buffer buffer ebdb-complete-info)
(puthash :window (get-buffer-window) ebdb-complete-info)
(puthash :window-point (point) ebdb-complete-info))
(setq ebdb-complete-info (clrhash ebdb-complete-info)
prefix-string nil))
;; Call ebdb
(if (and prefix-string (> (length prefix-string) 0))
(progn
(delete-char (- 0 (length prefix-string)))
(puthash :window-point (point) ebdb-complete-info)
(ebdb (ebdb-search-style) prefix-string))
(if (save-excursion
(let* ((end (point))
(begin (line-beginning-position))
(string (buffer-substring-no-properties
begin end)))
(string-match-p "@.*>$" string)))
;; When point at "[email protected]><I>",
;; launch `ebdb-complete-mail'.
(let ((ebdb-complete-mail-allow-cycling t))
(message "Cycling current user's email address!")
(ebdb-complete-mail)
;; Close ebdb-buffer's window when complete with
;; `ebdb-complete-mail'
(let ((window (get-buffer-window (ebdb-make-buffer-name))))
(if (window-live-p window)
(quit-window nil window))))
(ebdb (ebdb-search-style) "")))
;; Update `header-line-format'
(when (or (derived-mode-p 'message-mode)
(derived-mode-p 'mail-mode))
(with-current-buffer (ebdb-make-buffer-name)
(setq header-line-format
(format
(substitute-command-keys
(concat
"## Type `\\[ebdb-complete-push-mail]' or `\\[ebdb-complete-push-mail-and-bury-ebdb-buffer]' "
"to push email to buffer \"%s\". ##"))
(buffer-name buffer)))))))
(defun ebdb-complete-message-tab ()
"A command which will be bound to TAB key in message-mode,
when in message headers, this command will launch `ebdb-complete',
when in message body, this command will indent regular text."
(interactive)
(cond
;; Type TAB launch ebdb-complete when in header.
((and (save-excursion
(let ((point (point)))
(message-goto-body)
(> (point) point)))
(not (looking-back "^\\(Subject\\|From\\): *.*"
(line-beginning-position)))
(not (looking-back "^" (line-beginning-position))))
(ebdb-complete))
(message-tab-body-function (funcall message-tab-body-function))
(t (funcall (or (lookup-key text-mode-map "\t")
(lookup-key global-map "\t")
'indent-relative)))))
(defun ebdb-complete-keybinding-setup ()
"Setup ebdb-complete Keybindings."
(define-key ebdb-mode-map "q" #'ebdb-complete-bury-ebdb-buffer)
(define-key ebdb-mode-map "\C-c\C-c" #'ebdb-complete-push-mail)
(define-key ebdb-mode-map (kbd "RET") #'ebdb-complete-push-mail-and-bury-ebdb-buffer))
;;;###autoload
(defun ebdb-complete-enable ()
"Enable ebdb-complete, it will rebind TAB key in `message-mode-map'."
(interactive)
(require 'message)
(add-hook 'ebdb-mode-hook #'ebdb-complete-keybinding-setup)
(define-key message-mode-map "\t" #'ebdb-complete-message-tab)
(define-key mail-mode-map "\t" #'ebdb-complete-message-tab)
(message "ebdb-complete: Override EBDB keybindings: `q', `C-c C-c' and `RET'"))
(provide 'ebdb-complete)
;; Local Variables:
;; coding: utf-8-unix
;; End:
;;; ebdb-complete.el ends here