]> code.delx.au - gnu-emacs-elpa/blob - packages/excorporate/excorporate-org.el
a06057a13f9fa14f72b4174bde21ca8b3a81224a
[gnu-emacs-elpa] / packages / excorporate / excorporate-org.el
1 ;;; excorporate-org.el --- Exchange Org Mode view -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2016 Free Software Foundation, Inc.
4
5 ;; Author: Thomas Fitzsimmons <fitzsim@fitzsim.org>
6 ;; Keywords: calendar
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 ;; Use the Org Mode to display daily meetings.
24
25 ;;; Code:
26
27 (require 'org)
28 (require 'excorporate)
29
30 (defvar excorporate-org-buffer-name "*Excorporate*"
31 "The buffer into which Org Mode output is inserted.")
32
33 (defun exco-org-initialize-buffer ()
34 "Add initial text to the destination buffer."
35 (with-current-buffer (get-buffer-create excorporate-org-buffer-name)
36 (setq buffer-read-only t)
37 (org-mode)
38 (use-local-map (copy-keymap org-mode-map))
39 (local-set-key "q" 'quit-window)
40 (display-buffer (current-buffer))
41 (let ((inhibit-read-only t))
42 (delete-region (point-min) (point-max))
43 (goto-char 1)
44 (insert "# Updated...\n"))))
45
46 (defun exco-org-format-headline (identifier)
47 "Format an Org headline using IDENTIFIER."
48 (format "* Calendar (%s)\n" identifier))
49
50 (defun exco-org-insert-meeting-headline (subject start-time end-time)
51 "Insert and schedule a meeting.
52 SUBJECT is the meeting's subject, START-TIME and END-TIME are the
53 meeting's start and end times in the same format as is returned
54 by `current-time'."
55 (let* ((now (current-time))
56 (keyword (if (time-less-p now end-time)
57 "TODO"
58 "DONE")))
59 (insert (format "** %s %s\n" keyword subject))
60 (org-schedule nil (format-time-string "<%Y-%m-%d %a %H:%M>"
61 start-time))
62 (forward-line -1)
63 (end-of-line)
64 (insert "--" (format-time-string "<%Y-%m-%d %a %H:%M>" end-time))
65 (forward-line)
66 (org-insert-time-stamp (current-time) t t "+ Retrieved " "\n")))
67
68 (defun exco-org-insert-invitees (invitees)
69 "Parse and insert a list of invitees, INVITEES."
70 (dolist (invitee invitees)
71 (insert (format " + %s\n" invitee))))
72
73 (defun exco-org-insert-headline (identifier month day year)
74 "Insert Org headline for IDENTIFIER on date MONTH DAY YEAR."
75 (with-current-buffer (get-buffer-create excorporate-org-buffer-name)
76 (let ((inhibit-read-only t))
77 (insert (exco-org-format-headline identifier))
78 (org-insert-time-stamp (encode-time 0 0 0 day month year)
79 nil t " + Date " "\n"))))
80
81 (defun exco-org-insert-meeting (subject start end location
82 main-invitees optional-invitees)
83 "Insert a scheduled meeting.
84 SUBJECT is a string, the subject of the meeting. START is the
85 meeting start time in Emacs internal date time format, and END is
86 the end of the meeting in the same format. LOCATION is a string
87 representing the location. MAIN-INVITEES and OPTIONAL-INVITEES
88 are the requested participants."
89 (exco-org-insert-meeting-headline subject start end)
90 (insert (format "+ Duration: %d minutes\n"
91 (round (/ (float-time (time-subtract end start)) 60.0))))
92 (insert (format "+ Location: %s\n" location))
93 (when main-invitees
94 (insert "+ Invitees:\n")
95 (exco-org-insert-invitees main-invitees))
96 (when optional-invitees
97 (insert "+ Optional invitees:\n")
98 (exco-org-insert-invitees optional-invitees)))
99
100 (defun exco-org-insert-meetings (identifier response)
101 "Insert the connection IDENTIFIER's meetings from RESPONSE."
102 (with-current-buffer (get-buffer-create excorporate-org-buffer-name)
103 (let ((inhibit-read-only t)
104 (name-regexp (concat "\\" (exco-org-format-headline identifier))))
105 (goto-char 1)
106 (end-of-line)
107 (insert (format "%s..." identifier))
108 (goto-char (point-max))
109 (re-search-backward name-regexp nil)
110 (forward-line 2)
111 (org-insert-time-stamp (current-time) t t " + Last checked " "\n")
112 (exco-calendar-item-iterate response #'exco-org-insert-meeting)
113 (re-search-backward name-regexp nil)
114 (if (save-excursion (org-goto-first-child))
115 (org-sort-entries t ?s)
116 (forward-line 3)
117 (insert "`♘")))))
118
119 (defun exco-org-finalize-buffer ()
120 "Finalize text in buffer after all connections have responded."
121 (with-current-buffer (get-buffer-create excorporate-org-buffer-name)
122 ;; Sort top-level entries alphabetically.
123 (let ((inhibit-read-only t))
124 (goto-char (point-min))
125 (end-of-line)
126 (insert "done.")
127 (org-sort-entries t ?a))))
128
129 ;;;###autoload
130 (defun exco-org-show-day (month day year)
131 "Show meetings for the date specified by MONTH DAY YEAR."
132 (exco-connection-iterate #'exco-org-initialize-buffer
133 (lambda (identifier callback)
134 (exco-org-insert-headline identifier
135 month day year)
136 (exco-get-meetings-for-day identifier
137 month day year
138 callback))
139 #'exco-org-insert-meetings
140 #'exco-org-finalize-buffer))
141
142 (provide 'excorporate-org)
143
144 ;;; excorporate-org.el ends here