]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-address.el
emacs: address: save hash
[notmuch] / emacs / notmuch-address.el
1 ;;; notmuch-address.el --- address completion with notmuch
2 ;;
3 ;; Copyright © David Edmondson
4 ;;
5 ;; This file is part of Notmuch.
6 ;;
7 ;; Notmuch is free software: you can redistribute it and/or modify it
8 ;; under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
11 ;;
12 ;; Notmuch is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 ;; General Public License for more details.
16 ;;
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with Notmuch.  If not, see <https://www.gnu.org/licenses/>.
19 ;;
20 ;; Authors: David Edmondson <dme@dme.org>
21
22 ;;; Code:
23
24 (require 'message)
25 (require 'notmuch-parser)
26 (require 'notmuch-lib)
27 (require 'notmuch-company)
28 ;;
29 (declare-function company-manual-begin "company")
30
31 (defvar notmuch-address-last-harvest 0
32   "Time of last address harvest")
33
34 (defvar notmuch-address-completions (make-hash-table :test 'equal)
35   "Hash of email addresses for completion during email composition.
36   This variable is set by calling `notmuch-address-harvest'.")
37
38 (defvar notmuch-address-full-harvest-finished nil
39   "t indicates that full completion address harvesting has been
40 finished. Use notmuch-address--harvest-ready to access as that
41 will load a saved hash if necessary (and available).")
42
43 (defun notmuch-address--harvest-ready ()
44   "Return t if there is a full address hash available.
45
46 If the hash is not present it attempts to load a saved hash."
47   (or notmuch-address-full-harvest-finished
48       (notmuch-address--load-address-hash)))
49
50 (defcustom notmuch-address-command 'internal
51   "Determines how address completion candidates are generated.
52
53 If it is a string then that string should be an external program
54 which must take a single argument (searched string) and output a
55 list of completion candidates, one per line.
56
57 Alternatively, it can be the symbol 'internal, in which case
58 internal completion is used; the variable
59 `notmuch-address-internal-completion` can be used to customize
60 this case.
61
62 Finally, if this variable is nil then address completion is
63 disabled."
64   :type '(radio
65           (const :tag "Use internal address completion" internal)
66           (const :tag "Disable address completion" nil)
67           (string :tag "Use external completion command"))
68   :group 'notmuch-send
69   :group 'notmuch-external)
70
71 (defcustom notmuch-address-internal-completion '(sent nil)
72   "Determines how internal address completion generates candidates.
73
74 This should be a list of the form '(DIRECTION FILTER), where
75  DIRECTION is either sent or received and specifies whether the
76  candidates are searched in messages sent by the user or received
77  by the user (note received by is much faster), and FILTER is
78  either nil or a filter-string, such as \"date:1y..\" to append
79  to the query."
80   :type '(list :tag "Use internal address completion"
81                (radio
82                 :tag "Base completion on messages you have"
83                 :value sent
84                 (const :tag "sent (more accurate)" sent)
85                 (const :tag "received (faster)" received))
86                (radio :tag "Filter messages used for completion"
87                       (const :tag "Use all messages" nil)
88                       (string :tag "Filter query")))
89   ;; We override set so that we can clear the cache when this changes
90   :set (lambda (symbol value)
91          (set-default symbol value)
92          (setq notmuch-address-last-harvest 0)
93          (setq notmuch-address-completions (clrhash notmuch-address-completions))
94          (setq notmuch-address-full-harvest-finished nil))
95   :group 'notmuch-send
96   :group 'notmuch-external)
97
98 (defcustom notmuch-address-save-filename nil
99   "Filename to save the cached completion addresses.
100
101 All the addresses notmuch uses for address completion will be
102 cached in this file. This has obvious privacy implications so you
103 should make sure it is not somewhere publicly readable."
104   :type '(choice (const :tag "Off" nil)
105                  (file :tag "Filename"))
106   :group 'notmuch-send
107   :group 'notmuch-external)
108
109 (defcustom notmuch-address-selection-function 'notmuch-address-selection-function
110   "The function to select address from given list. The function is
111 called with PROMPT, COLLECTION, and INITIAL-INPUT as arguments
112 (subset of what `completing-read' can be called with).
113 While executed the value of `completion-ignore-case' is t.
114 See documentation of function `notmuch-address-selection-function'
115 to know how address selection is made by default."
116   :type 'function
117   :group 'notmuch-send
118   :group 'notmuch-external)
119
120 (defcustom notmuch-address-post-completion-functions nil
121   "Functions called after completing address.
122
123 The completed address is passed as an argument to each function.
124 Note that this hook will be invoked for completion in headers
125 matching `notmuch-address-completion-headers-regexp'.
126 "
127   :type 'hook
128   :group 'notmuch-address
129   :group 'notmuch-hooks)
130
131 (defun notmuch-address-selection-function (prompt collection initial-input)
132   "Call (`completing-read'
133       PROMPT COLLECTION nil nil INITIAL-INPUT 'notmuch-address-history)"
134   (completing-read
135    prompt collection nil nil initial-input 'notmuch-address-history))
136
137 (defvar notmuch-address-completion-headers-regexp
138   "^\\(Resent-\\)?\\(To\\|B?Cc\\|Reply-To\\|From\\|Mail-Followup-To\\|Mail-Copies-To\\):")
139
140 (defvar notmuch-address-history nil)
141
142 (defun notmuch-address-message-insinuate ()
143   (message "calling notmuch-address-message-insinuate is no longer needed"))
144
145 (defcustom notmuch-address-use-company t
146   "If available, use company mode for address completion"
147   :type 'boolean
148   :group 'notmuch-send)
149
150 (defun notmuch-address-setup ()
151   (let* ((setup-company (and notmuch-address-use-company
152                            (require 'company nil t)))
153          (pair (cons notmuch-address-completion-headers-regexp
154                        #'notmuch-address-expand-name)))
155       (when setup-company
156         (notmuch-company-setup))
157       (unless (memq pair message-completion-alist)
158         (setq message-completion-alist
159               (push pair message-completion-alist)))))
160
161 (defun notmuch-address-toggle-internal-completion ()
162   "Toggle use of internal completion for current buffer.
163
164 This overrides the global setting for address completion and
165 toggles the setting in this buffer."
166   (interactive)
167   (if (local-variable-p 'notmuch-address-command)
168       (kill-local-variable 'notmuch-address-command)
169     (notmuch-setq-local notmuch-address-command 'internal))
170   (if (boundp 'company-idle-delay)
171       (if (local-variable-p 'company-idle-delay)
172           (kill-local-variable 'company-idle-delay)
173         (notmuch-setq-local company-idle-delay nil))))
174
175 (defun notmuch-address-matching (substring)
176   "Returns a list of completion candidates matching SUBSTRING.
177 The candidates are taken from `notmuch-address-completions'."
178   (let ((candidates)
179         (re (regexp-quote substring)))
180     (maphash (lambda (key val)
181                (when (string-match re key)
182                  (push key candidates)))
183              notmuch-address-completions)
184     candidates))
185
186 (defun notmuch-address-options (original)
187   "Returns a list of completion candidates. Uses either
188 elisp-based implementation or older implementation requiring
189 external commands."
190   (cond
191    ((eq notmuch-address-command 'internal)
192     (unless (notmuch-address--harvest-ready)
193       ;; First, run quick synchronous harvest based on what the user
194       ;; entered so far
195       (notmuch-address-harvest original t))
196     (prog1 (notmuch-address-matching original)
197       ;; Then start the (potentially long-running) full asynchronous harvest if necessary
198       (notmuch-address-harvest-trigger)))
199    (t
200     (process-lines notmuch-address-command original))))
201
202 (defun notmuch-address-expand-name ()
203   (cond
204    ((and (eq notmuch-address-command 'internal)
205          notmuch-address-use-company
206          (bound-and-true-p company-mode))
207     (company-manual-begin))
208    (notmuch-address-command
209     (let* ((end (point))
210            (beg (save-excursion
211                   (re-search-backward "\\(\\`\\|[\n:,]\\)[ \t]*")
212                   (goto-char (match-end 0))
213                   (point)))
214            (orig (buffer-substring-no-properties beg end))
215            (completion-ignore-case t)
216            (options (with-temp-message "Looking for completion candidates..."
217                       (notmuch-address-options orig)))
218            (num-options (length options))
219            (chosen (cond
220                     ((eq num-options 0)
221                      nil)
222                     ((eq num-options 1)
223                      (car options))
224                     (t
225                      (funcall notmuch-address-selection-function
226                               (format "Address (%s matches): " num-options)
227                               ;; We put the first match as the initial
228                               ;; input; we put all the matches as
229                               ;; possible completions, moving the
230                               ;; first match to the end of the list
231                               ;; makes cursor up/down in the list work
232                               ;; better.
233                               (append (cdr options) (list (car options)))
234                               (car options))))))
235       (if chosen
236           (progn
237             (push chosen notmuch-address-history)
238             (delete-region beg end)
239             (insert chosen)
240             (run-hook-with-args 'notmuch-address-post-completion-functions chosen))
241         (message "No matches.")
242         (ding))))
243    (t nil)))
244
245 ;; Copied from `w3m-which-command'.
246 (defun notmuch-address-locate-command (command)
247   "Return non-nil if `command' is an executable either on
248 `exec-path' or an absolute pathname."
249   (when (stringp command)
250     (if (and (file-name-absolute-p command)
251              (file-executable-p command))
252         command
253       (setq command (file-name-nondirectory command))
254       (catch 'found-command
255         (let (bin)
256           (dolist (dir exec-path)
257             (setq bin (expand-file-name command dir))
258             (when (or (and (file-executable-p bin)
259                            (not (file-directory-p bin)))
260                       (and (file-executable-p (setq bin (concat bin ".exe")))
261                            (not (file-directory-p bin))))
262               (throw 'found-command bin))))))))
263
264 (defun notmuch-address-harvest-addr (result)
265   (let ((name-addr (plist-get result :name-addr)))
266     (puthash name-addr t notmuch-address-completions)))
267
268 (defun notmuch-address-harvest-handle-result (obj)
269   (notmuch-address-harvest-addr obj))
270
271 (defun notmuch-address-harvest-filter (proc string)
272   (when (buffer-live-p (process-buffer proc))
273     (with-current-buffer (process-buffer proc)
274       (save-excursion
275         (goto-char (point-max))
276         (insert string))
277       (notmuch-sexp-parse-partial-list
278        'notmuch-address-harvest-handle-result (process-buffer proc)))))
279
280 (defvar notmuch-address-harvest-procs '(nil . nil)
281   "The currently running harvests.
282
283 The car is a partial harvest, and the cdr is a full harvest")
284
285 (defun notmuch-address-harvest (&optional addr-prefix synchronous callback)
286   "Collect addresses completion candidates.
287
288 It queries the notmuch database for messages sent/received (as
289 configured with `notmuch-address-command`) by the user, collects
290 destination/source addresses from those messages and stores them
291 in `notmuch-address-completions'.
292
293 If ADDR-PREFIX is not nil, only messages with to/from addresses
294 matching ADDR-PREFIX*' are queried.
295
296 Address harvesting may take some time so the address collection runs
297 asynchronously unless SYNCHRONOUS is t. In case of asynchronous
298 execution, CALLBACK is called when harvesting finishes."
299
300   (let* ((sent (eq (car notmuch-address-internal-completion) 'sent))
301          (config-query (cadr notmuch-address-internal-completion))
302          (prefix-query (when addr-prefix
303                          (format "%s:%s*" (if sent "to" "from") addr-prefix)))
304          (from-or-to-me-query
305           (mapconcat (lambda (x)
306                        (concat (if sent "from:" "to:") x))
307                      (notmuch-user-emails) " or "))
308          (query (if (or prefix-query config-query)
309                     (concat (format "(%s)" from-or-to-me-query)
310                             (when prefix-query
311                               (format " and (%s)" prefix-query))
312                             (when config-query
313                               (format " and (%s)" config-query)))
314                   from-or-to-me-query))
315          (args `("address" "--format=sexp" "--format-version=2"
316                  ,(if sent "--output=recipients" "--output=sender")
317                  "--deduplicate=address"
318                  ,query)))
319     (if synchronous
320         (mapc #'notmuch-address-harvest-addr
321                                    (apply 'notmuch-call-notmuch-sexp args))
322       ;; Asynchronous
323       (let* ((current-proc (if addr-prefix
324                                (car notmuch-address-harvest-procs)
325                              (cdr notmuch-address-harvest-procs)))
326              (proc-name (format "notmuch-address-%s-harvest"
327                                 (if addr-prefix "partial" "full")))
328              (proc-buf (concat " *" proc-name "*")))
329         ;; Kill any existing process
330         (when current-proc
331           (kill-buffer (process-buffer current-proc))) ; this also kills the process
332
333         (setq current-proc
334               (apply 'notmuch-start-notmuch proc-name proc-buf
335                      callback                           ; process sentinel
336                      args))
337         (set-process-filter current-proc 'notmuch-address-harvest-filter)
338         (set-process-query-on-exit-flag current-proc nil)
339         (if addr-prefix
340             (setcar notmuch-address-harvest-procs current-proc)
341           (setcdr notmuch-address-harvest-procs current-proc)))))
342   ;; return value
343   nil)
344
345 (defvar notmuch-address--save-hash-version 1
346   "Version format of the save hash.")
347
348 (defun notmuch-address--get-address-hash ()
349   "Returns the saved address hash as a plist.
350
351 Returns nil if the save file does not exist, or it does not seem
352 to be a saved address hash."
353   (when notmuch-address-save-filename
354     (condition-case nil
355         (with-temp-buffer
356           (insert-file-contents notmuch-address-save-filename)
357           (let ((name (read (current-buffer)))
358                 (plist (read (current-buffer))))
359             ;; We do two simple sanity checks on the loaded file. We just
360             ;; check a version is specified, not that it is the current
361             ;; version, as we are allowed to over-write and a save-file with
362             ;; an older version.
363             (when (and (string= name "notmuch-address-hash")
364                        (plist-get plist :version))
365               plist)))
366       ;; The error case catches any of the reads failing.
367       (error nil))))
368
369 (defun notmuch-address--load-address-hash ()
370   "Read the saved address hash and set the corresponding variables."
371   (let ((load-plist (notmuch-address--get-address-hash)))
372     (when (and load-plist
373                ;; If the user's setting have changed, or the version
374                ;; has changed, return nil to make sure the new settings
375                ;; take effect.
376                (equal (plist-get load-plist :completion-settings)
377                       notmuch-address-internal-completion)
378                (equal (plist-get load-plist :version)
379                       notmuch-address--save-hash-version))
380       (setq notmuch-address-last-harvest (plist-get load-plist :last-harvest)
381             notmuch-address-completions (plist-get load-plist :completions)
382             notmuch-address-full-harvest-finished t)
383       ;; Return t to say load was successful.
384       t)))
385
386 (defun notmuch-address--save-address-hash ()
387   (when notmuch-address-save-filename
388     (if (or (not (file-exists-p notmuch-address-save-filename))
389               ;; The file exists, check it is a file we saved
390             (notmuch-address--get-address-hash))
391         (with-temp-file notmuch-address-save-filename
392           (let ((save-plist (list :version notmuch-address--save-hash-version
393                                   :completion-settings notmuch-address-internal-completion
394                                   :last-harvest notmuch-address-last-harvest
395                                   :completions notmuch-address-completions)))
396             (print "notmuch-address-hash" (current-buffer))
397             (print save-plist (current-buffer))))
398       (message "\
399 Warning: notmuch-address-save-filename %s exists but doesn't
400 appear to be an address savefile.  Not overwriting."
401                notmuch-address-save-filename))))
402
403 (defun notmuch-address-harvest-trigger ()
404   (let ((now (float-time)))
405     (when (> (- now notmuch-address-last-harvest) 86400)
406       (setq notmuch-address-last-harvest now)
407       (notmuch-address-harvest nil nil
408                                (lambda (proc event)
409                                  ;; If harvest fails, we want to try
410                                  ;; again when the trigger is next
411                                  ;; called
412                                  (if (string= event "finished\n")
413                                      (progn
414                                        (notmuch-address--save-address-hash)
415                                        (setq notmuch-address-full-harvest-finished t))
416                                    (setq notmuch-address-last-harvest 0)))))))
417
418 ;;
419
420 (defun notmuch-address-from-minibuffer (prompt)
421   (if (not notmuch-address-command)
422       (read-string prompt)
423     (let ((rmap (copy-keymap minibuffer-local-map))
424           (omap minibuffer-local-map))
425       ;; Configure TAB to start completion when executing read-string.
426       ;; "Original" minibuffer keymap is restored just before calling
427       ;; notmuch-address-expand-name as it may also use minibuffer-local-map
428       ;; (completing-read probably does not but if something else is used there).
429       (define-key rmap (kbd "TAB") (lambda ()
430                                      (interactive)
431                                      (let ((enable-recursive-minibuffers t)
432                                            (minibuffer-local-map omap))
433                                        (notmuch-address-expand-name))))
434       (let ((minibuffer-local-map rmap))
435         (read-string prompt)))))
436
437 ;;
438
439 (provide 'notmuch-address)
440
441 ;;; notmuch-address.el ends here