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