]> code.delx.au - gnu-emacs-elpa/blob - packages/gnorb/gnorb-utils.el
Merge commit '37c46180280f10fa5120a017acd04f7022d124e4'
[gnu-emacs-elpa] / packages / gnorb / gnorb-utils.el
1 ;;; gnorb-utils.el --- Common utilities for all gnorb stuff.
2
3 ;; Copyright (C) 2014 Free Software Foundation, Inc.
4
5 ;; Author: Eric Abrahamsen <eric@ericabrahamsen.net>
6 ;; Keywords:
7
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 ;;; Commentary:
22
23 ;;
24
25 ;;; Code:
26
27 (require 'cl-lib)
28
29 (require 'mailcap)
30 (mailcap-parse-mimetypes)
31
32 (defgroup gnorb nil
33 "Glue code between Gnus, Org, and BBDB."
34 :tag "Gnorb")
35
36 (make-obsolete-variable
37 'gnorb-trigger-todo-default
38 "This variable has been superseded by
39 `gnorb-org-trigger-actions'"
40 "September 8, 2014" 'set)
41
42 (defun gnorb-prompt-for-bbdb-record ()
43 "Prompt the user for a BBDB record."
44 (let ((recs (bbdb-records))
45 name)
46 (while (> (length recs) 1)
47 (setq name
48 (completing-read
49 (format "Filter records by regexp (%d remaining): "
50 (length recs))
51 (mapcar 'bbdb-record-name recs)))
52 (setq recs (bbdb-search recs name name name nil nil)))
53 (if recs
54 (car recs)
55 (error "No matching records"))))
56
57 (defvar gnorb-tmp-dir (make-temp-file "emacs-gnorb" t)
58 "Temporary directory where attachments etc are saved.")
59
60 (defvar gnorb-message-org-ids nil
61 "List of Org heading IDs from the outgoing Gnus message, used
62 to mark mail TODOs as done once the message is sent."
63 ;; The send hook either populates this, or sets it to nil, depending
64 ;; on whether the message in question has an Org id header. Then
65 ;; `gnorb-org-restore-after-send' checks for it and acts
66 ;; appropriately, then sets it to nil.
67 )
68
69 (defvar gnorb-window-conf nil
70 "Save window configurations here, for restoration after mails
71 are sent, or Org headings triggered.")
72
73 (defvar gnorb-return-marker (make-marker)
74 "Return point here after various actions, to be used together
75 with `gnorb-window-conf'.")
76
77 (defvar gnorb-trigger-capture-location nil
78 "Marker pointing at the location where we want to place capture
79 templates, for the capture-to-child and capture-to-sibling
80 trigger actions.")
81
82 (defcustom gnorb-mail-header "X-Org-ID"
83 "Name of the mail header used to store the ID of a related Org
84 heading. Only used locally: always stripped when the mail is
85 sent."
86 :group 'gnorb
87 :type 'string)
88
89 ;;; this is just ghastly, but the value of this var is single regexp
90 ;;; group containing various header names, and we want our value
91 ;;; inside that group.
92 (eval-after-load 'message
93 `(let ((ign-headers-list
94 (split-string message-ignored-mail-headers
95 "|"))
96 (our-val (concat gnorb-mail-header "\\")))
97 (unless (member our-val ign-headers-list)
98 (setq ign-headers-list
99 `(,@(butlast ign-headers-list 1) ,our-val
100 ,@(last ign-headers-list 1)))
101 (setq message-ignored-mail-headers
102 (mapconcat
103 'identity ign-headers-list "|")))))
104
105 ;;;###autoload
106 (defun gnorb-restore-layout ()
107 "Restore window layout and value of point after a Gnorb command.
108
109 Some Gnorb commands change the window layout (ie `gnorb-org-view'
110 or incoming email triggering). This command restores the layout
111 to what it was. Bind it to a global key, or to local keys in Org
112 and Gnus and BBDB maps."
113 (interactive)
114 (when (window-configuration-p gnorb-window-conf)
115 (select-frame-set-input-focus
116 (window-configuration-frame gnorb-window-conf))
117 (set-window-configuration gnorb-window-conf)
118 (when (buffer-live-p (marker-buffer gnorb-return-marker))
119 (goto-char gnorb-return-marker))))
120
121 (defun gnorb-bracket-message-id (id)
122 "Ensure message-id ID is bound by angle brackets."
123 ;; Always use a message-id with angle brackets around it.
124 ;; `gnus-summary-goto-article' can handle either, but
125 ;; `gnus-request-head' will fail without brackets IF you're
126 ;; requesting from an nntp group. Mysterious.
127 (unless (string-match "\\`<" id)
128 (setq id (concat "<" id)))
129 (unless (string-match ">\\'" id)
130 (setq id (concat id ">")))
131 id)
132
133 (defun gnorb-unbracket-message-id (id)
134 "Ensure message-id ID is NOT bound by angle brackets."
135 ;; This shit is annoying, but Org wants an id with no brackets, and
136 ;; Gnus is safest with an id that has brackets. So here we are.
137 (replace-regexp-in-string "\\(\\`<\\|>\\'\\)" "" id))
138
139 (defun gnorb-reply-to-gnus-link (link)
140 "Start a reply to the linked message."
141 (let* ((link (org-link-unescape link))
142 (group (car (org-split-string link "#")))
143 (id (gnorb-bracket-message-id
144 (second (org-split-string link "#"))))
145 (backend
146 (car (gnus-find-method-for-group group))))
147 (gnorb-follow-gnus-link group id)
148 (call-interactively
149 (if (eq backend 'nntp)
150 'gnus-summary-followup-with-original
151 'gnus-summary-wide-reply-with-original))))
152
153 (defun gnorb-follow-gnus-link (group id)
154 "Be a little clever about following gnus links.
155
156 The goal here is reuse frames and windows as much as possible, so
157 we're not opening multiple windows on the *Group* buffer, for
158 instance, and messing up people's layouts. There also seems to be
159 an issue when opening a link to a message whose *Summary* buffer
160 is already visible: somehow, after following the link, point ends
161 up on the message _after_ the one we want, and things go haywire.
162
163 So we try to be a little clever. The logical progression here is
164 this:
165
166 1. If the link's target group is already open in a *Summary*
167 buffer, just switch to that buffer (if it's visible in any frame
168 then raise it and switch focus, otherwise pull it into the
169 current window) and go to the message with
170 `gnus-summary-goto-article'.
171
172 2. If the Gnus *Group* buffer is visible in any window or frame,
173 raise that frame/window and give it focus before following the
174 link.
175
176 3. Otherwise just follow the link as usual, in the current
177 window."
178 (let* ((sum-buffer (gnus-summary-buffer-name group))
179 (target-buffer
180 (cond
181 ((gnus-buffer-exists-p sum-buffer)
182 sum-buffer)
183 ((gnus-buffer-exists-p gnus-group-buffer)
184 gnus-group-buffer)
185 (t nil)))
186 (target-window (when target-buffer
187 (get-buffer-window target-buffer t))))
188 (if target-window
189 ;; Our target buffer is displayed somewhere: just go there.
190 (progn
191 (select-frame-set-input-focus
192 (window-frame target-window))
193 (switch-to-buffer target-buffer))
194 ;; Our target buffer exists, but isn't displayed: pull it up.
195 (if target-buffer
196 (switch-to-buffer target-buffer)))
197 (message "Following link...")
198 (if (gnus-buffer-exists-p sum-buffer)
199 (gnus-summary-goto-article id nil t)
200 (gnorb-open-gnus-link group id))))
201
202 (defun gnorb-open-gnus-link (group id)
203 "Gnorb version of `org-gnus-follow-link'."
204 ;; We've probably already bracketed the id, but just in case this is
205 ;; called from elsewhere...
206 (let* ((id (gnorb-bracket-message-id id))
207 (art-no (cdr (gnus-request-head id group)))
208 (arts (gnus-group-unread group))
209 success)
210 (gnus-activate-group group)
211 (setq success (gnus-group-read-group arts t group))
212 (if success
213 (gnus-summary-goto-article (or art-no id) nil t)
214 (signal 'error "Group could not be opened."))))
215
216 (defun gnorb-trigger-todo-action (arg &optional id)
217 "Do the actual restore action. Two main things here. First: if
218 we were in the agenda when this was called, then keep us in the
219 agenda. Then let the user choose an action from the value of
220 `gnorb-org-trigger-actions'."
221 (let* ((agenda-p (eq major-mode 'org-agenda-mode))
222 (root-marker
223 (cond (agenda-p
224 (copy-marker
225 (org-get-at-bol 'org-hd-marker)))
226 ((derived-mode-p 'org-mode)
227 (save-excursion
228 (org-back-to-heading)
229 (point-marker)))
230 (id
231 (save-excursion
232 (org-id-goto id)
233 (org-back-to-heading)
234 (point-marker)))))
235 (id (or id
236 (org-with-point-at root-marker
237 (org-id-get-create))))
238 (action (cdr (assoc
239 (org-completing-read
240 (format
241 "Trigger action on %s: "
242 (gnorb-pretty-outline id))
243 gnorb-org-trigger-actions nil t)
244 gnorb-org-trigger-actions))))
245 (unless agenda-p
246 (org-reveal))
247 (cl-labels
248 ((make-entry
249 (id)
250 (gnorb-registry-make-entry
251 (plist-get gnorb-gnus-message-info :msg-id)
252 (plist-get gnorb-gnus-message-info :from)
253 (plist-get gnorb-gnus-message-info :subject)
254 id
255 (plist-get gnorb-gnus-message-info :group))))
256 ;; Handle our action.
257 (if (fboundp action)
258 (org-with-point-at root-marker
259 (make-entry (org-id-get-create))
260 (funcall action gnorb-gnus-message-info))
261 (cl-case action
262 (note
263 (org-with-point-at root-marker
264 (make-entry (org-id-get-create))
265 (call-interactively 'org-add-note)))
266 (todo
267 (if agenda-p
268 (progn
269 (org-with-point-at root-marker
270 (make-entry (org-id-get-create)))
271 (call-interactively 'org-agenda-todo))
272 (org-with-point-at root-marker
273 (make-entry (org-id-get-create))
274 (call-interactively 'org-todo))))
275 (no-associate
276 nil)
277 (associate
278 (org-with-point-at root-marker
279 (make-entry (org-id-get-create))))
280 ;; We're going to capture a new heading
281 ((cap-child cap-sib)
282 (org-with-point-at root-marker
283 (setq gnorb-trigger-capture-location (point-marker)))
284 (let ((entry
285 ;; Pick a template.
286 (copy-sequence (org-capture-select-template))))
287 ;; Do surgery on that template so that it finds its
288 ;; location using our function.
289 (setf (nth 3 entry)
290 `(function
291 ,(if (eq action 'cap-child)
292 #'gnorb-trigger-capture-child
293 #'gnorb-trigger-capture-sibling)))
294 ;; This will likely fail horribly for capture templates
295 ;; that aren't entries or list items.
296 (let ((org-capture-entry entry))
297 ;; When org-capture-entry is let-bound, the capture
298 ;; process will use that template instead of
299 ;; prompting the user. Also, `gnorb-registry-capture'
300 ;; will take care of making the registry entry for us.
301 (call-interactively 'org-capture)))))))
302 ;; Lastly, query about attaching email attachments. No matter what
303 ;; happens, clear `gnorb-gnus-capture-attachments'.
304 (unwind-protect
305 (org-with-point-at
306 (if (memq action '(cap-child cap-sib))
307 (point)
308 root-marker)
309 (map-y-or-n-p
310 (lambda (a)
311 (format "Attach %s to heading? "
312 (file-name-nondirectory a)))
313 (lambda (a)
314 (with-demoted-errors
315 (org-attach-attach a nil 'mv)))
316 gnorb-gnus-capture-attachments
317 '("file" "files" "attach")))
318 (setq gnorb-gnus-capture-attachments nil))))
319
320 (defun gnorb-trigger-capture-child ()
321 ;; The capture process creates a child by default
322 (org-goto-marker-or-bmk gnorb-trigger-capture-location)
323 (org-back-to-heading))
324
325 (defun gnorb-trigger-capture-sibling ()
326 ;; This only works if we're not trying to create a sibling for a
327 ;; top-level heading, there appears to be no way to do that. But in
328 ;; that case this trigger action isn't really necessary, just
329 ;; handle it with a regular capture.
330 (org-goto-marker-or-bmk gnorb-trigger-capture-location)
331 (org-up-heading-safe))
332
333 (defun gnorb-pretty-outline (id &optional kw)
334 "Return pretty outline path of the Org heading indicated by ID.
335
336 If the KW argument is true, add the TODO keyword into the path."
337 (let ((pt (org-id-find id t)))
338 (if pt
339 (org-with-point-at pt
340 (let ((el (org-element-at-point)))
341 (concat
342 (if kw
343 (format "(%s): "
344 (org-element-property :todo-keyword el))
345 "")
346 (org-format-outline-path
347 (append
348 (list
349 (file-name-nondirectory
350 (buffer-file-name
351 (org-base-buffer (current-buffer)))))
352 (org-get-outline-path)
353 (list
354 (replace-regexp-in-string
355 org-bracket-link-regexp
356 "\\3" (org-element-property :raw-value el))))))))
357 "[none]")))
358
359 (defun gnorb-scan-links (bound &rest types)
360 "Scan from point to BOUND looking for links of type in TYPES.
361
362 TYPES is a list of symbols, possible values include 'bbdb, 'mail,
363 and 'gnus."
364 ;; this function could be refactored somewhat -- lots of code
365 ;; repetition. It also should be a little faster for when we're
366 ;; scanning for gnus links only, that's a little slow. We should
367 ;; probably use a different regexp based on the value of TYPES.
368 ;;
369 ;; This function should also *not* be responsible for unescaping
370 ;; links -- we don't know what they're going to be used for, and
371 ;; unescaped is safer.
372 (unless (= (point) bound)
373 (let (addr gnus mail bbdb)
374 (while (re-search-forward org-any-link-re bound t)
375 (setq addr (or (match-string-no-properties 2)
376 (match-string-no-properties 0)))
377 (cond
378 ((and (memq 'gnus types)
379 (string-match "^<?gnus:" addr))
380 (push (substring addr (match-end 0)) gnus))
381 ((and (memq 'mail types)
382 (string-match "^<?mailto:" addr))
383 (push (substring addr (match-end 0)) mail))
384 ((and (memq 'bbdb types)
385 (string-match "^<?bbdb:" addr))
386 (push (substring addr (match-end 0)) bbdb))))
387 `(:gnus ,(reverse gnus) :mail ,(reverse mail) :bbdb ,(reverse bbdb)))))
388
389 (defun gnorb-msg-id-to-link (msg-id)
390 "Given a message id, try to create a full org link to the
391 message."
392 (let ((server-group (gnorb-msg-id-to-group msg-id)))
393 (when server-group
394 (org-link-escape
395 (concat server-group "#"
396 (gnorb-unbracket-message-id msg-id))))))
397
398 (defun gnorb-msg-id-to-group (msg-id)
399 "Given a message id, try to find the group it's in.
400
401 So far we're checking the registry, then the groups in
402 `gnorb-gnus-sent-groups'. Use search engines? Other clever
403 methods?"
404 (let (candidates server-group)
405 (setq msg-id (gnorb-bracket-message-id msg-id))
406 (catch 'found
407 (when gnorb-tracking-enabled
408 ;; Make a big list of all the groups where this message might
409 ;; conceivably be.
410 (setq candidates
411 (append (gnus-registry-get-id-key msg-id 'group)
412 gnorb-gnus-sent-groups))
413 (while (setq server-group (pop candidates))
414 (when (and (stringp server-group)
415 (not
416 (string-match-p
417 "\\(nnir\\|nnvirtual\\|UNKNOWN\\)"
418 server-group))
419 (ignore-errors
420 (gnus-request-head msg-id server-group)))
421 (throw 'found server-group))))
422 nil)))
423
424 (defun gnorb-collect-ids (&optional id)
425 "Collect all Org IDs for a subtree.
426
427 Starting with the heading under point (or the heading indicated
428 by the ID argument), collect its ID property, and the IDs of all
429 child headings."
430 (save-excursion
431 (save-restriction
432 (when id
433 (org-id-goto id))
434 (org-narrow-to-subtree)
435 (org-element-map (org-element-parse-buffer)
436 'headline
437 (lambda (hl)
438 (org-element-property :ID hl))))))
439
440 ;; Common functions for extracting references and relevant headings
441 ;; from the message under point. For use in gnorb-gnus.el functions.
442
443 (defun gnorb-get-real-group-name (group art-no)
444 "Find the original group name of a message in a virtual or nnir
445 group."
446 (cl-case (car (gnus-find-method-for-group group))
447 (nnvirtual
448 (setq group (car (nnvirtual-map-article art-no))))
449 (nnir
450 (setq group (nnir-article-group art-no))))
451 group)
452
453 (defun gnorb-find-tracked-headings (headers &optional include-zombies)
454 "Check HEADERS for message references and return relevant heading IDs.
455
456 HEADERs is a message's data header, as produced by
457 \(gnus-interactive \"H\"\), or, equivalently:
458
459 \(gnus-data-header \(gnus-data-find \(gnus-summary-article-number\)\)\)"
460 (let ((references (mail-header-references headers))
461 (msg-id (mail-header-message-id headers)))
462 (when gnorb-tracking-enabled
463 (gnorb-find-visit-candidates
464 (concat msg-id " " references) include-zombies))))
465
466 (defun gnorb-choose-trigger-heading (&optional id)
467 "Given an Org heading ID, ask the user if they want to trigger it.
468
469 If not, prompt for another target heading. Either way, return the
470 target heading id."
471 (let ((id (if (stringp id)
472 id
473 (car-safe id)))
474 refile-result)
475 (if (and id
476 (y-or-n-p (message
477 "Attach part to %s"
478 (gnorb-pretty-outline id))))
479 id
480 (setq refile-result
481 (org-refile-get-location "Attach part to" nil t))
482 (save-window-excursion
483 (find-file (nth 1 refile-result))
484 (goto-char (nth 3 refile-result))
485 (org-id-get-create)))))
486
487 ;; Loading the registry
488
489 (defvar gnorb-tracking-enabled nil
490 "Internal flag indicating whether Gnorb is successfully plugged
491 into the registry or not.")
492
493 ;;;###autoload
494 (defun gnorb-tracking-initialize ()
495 "Start using the Gnus registry to track correspondences between
496 Gnus messages and Org headings. This requires that the Gnus
497 registry be in use, and should be called after the call to
498 `gnus-registry-initialize'."
499 (require 'gnorb-registry)
500 (with-eval-after-load 'gnus-registry
501 (add-to-list 'gnus-registry-extra-entries-precious 'gnorb-ids)
502 (add-to-list 'gnus-registry-track-extra 'gnorb-ids))
503 (add-hook
504 'gnus-started-hook
505 (lambda ()
506 (unless (gnus-registry-install-p)
507 (user-error "Gnorb tracking requires that the Gnus registry be installed."))
508 (add-hook 'org-capture-mode-hook 'gnorb-registry-capture)
509 (add-hook 'org-capture-prepare-finalize-hook 'gnorb-registry-capture-abort-cleanup)
510 (setq gnorb-tracking-enabled t))))
511
512 (provide 'gnorb-utils)
513 ;;; gnorb-utils.el ends here