summary refs log tree commit diff stats
path: root/lisp/ical2org.el
blob: 2716787b066544b559323652ea1dc47e460052c4 (plain)
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
;;; ical2org.el --- Run ical2org in Emacs  -*- lexical-binding: t; -*-

;;; Commentary:

;; based on code from this reddit thread:
;; https://www.reddit.com/r/emacs/comments/8s1ion/ical2org_integrations/
;;
;; see also: icalendar.org (converts to diary format, might be all I need)
;;
;; XXX: This code currently imports into gnus, which isn't what I want.

;;; Code:

(defun ical2org (&optional replace output-buffer)
  "Run ical2org on contents of this buffer.
If REPLACE (interactive prefix argument), replace contents of the
buffer.  If no REPLACE nor OUTPUT-BUFFER, output goes to
minibuffer."
  (interactive "P")
  (shell-command-on-region (point-min) (point-max)
                           "ical2org"
                           output-buffer
                           replace
                           "*ical2org errors*"
                           'display-errors))

(defun ical2org-capture ()
  "Run `ical2org' on this buffer, then `org-capture' the result.
Leaves current buffer as-was afterwards."
  (interactive)
  (let ((buf (current-buffer))
        (ics (buffer-string)))
    (ical2org 'replace)
    (mark-whole-buffer)
    (call-interactively #'org-capture)
    (with-current-buffer buf
      (delete-region (point-min) (point-max))
      (insert ics))))

(defun my-gnus-org-capture-icalendar ()
  "Capture any text/calendar invites with org."
  (interactive)
  (with-current-buffer gnus-article-buffer ;;; XXX
    (save-excursion
      (dolist (part gnus-article-mime-handle-alist)
        (when  (and (>= (length part) 3)
                    (listp (caddr part))
                    (or (equal "application/ics" (caaddr part))
                        (equal "text/calendar" (caaddr part))))
          (save-window-excursion
            (gnus-mime-copy-part (cdr part))
            (ical2org-capture)))))))
(add-hook 'gnus-article-prepare-hook #'my-gnus-org-capture-icalendar)

(provide 'ical2org)
;;; ical2org.el ends here