forked from kiwanami/emacs-calfw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calfw-org.el
500 lines (448 loc) · 18.4 KB
/
calfw-org.el
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
;;; calfw-org.el --- calendar view for org-agenda -*- coding: utf-8 -*-
;; Copyright (C) 2011 SAKURAI Masashi
;; Author: SAKURAI Masashi <m.sakurai at kiwanami.net>
;; Keywords: calendar, org
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Display org-agenda items in the calfw buffer.
;; (Because I don't use the org-agenda mainly,
;; I hope someone continue integration with the org.)
;; (require 'calfw-org)
;;
;; ;; use org agenda buffer style keybinding.
;; ;; (setq cfw:org-overwrite-default-keybinding t)
;;
;; M-x cfw:open-org-calendar
;;; Code:
(require 'calfw)
(require 'org)
(require 'org-agenda)
(require 'org-element)
(require 'org-capture)
(require 'google-maps nil t)
(defgroup cfw-org nil
"Options about calfw-org."
:tag "Calfw Org"
:group 'org
:group 'cfw)
(defcustom cfw:org-capture-template nil
"org-capture template. If you use `org-capture' with `calfw', you shold set like
'(\"c\" \"calfw2org\" entry (file nil) \"* %?\n %(cfw:org-capture-day)\")"
:group 'cfw-org
:version "24.1"
:type
'(list string string symbol (list symbol (choice file (const nil))) string))
(defsubst cfw:org-tp (text prop)
"[internal] Return text property at position 0."
(get-text-property 0 prop text))
(defvar cfw:org-agenda-schedule-args nil
"Default arguments for collecting agenda entries.
If value is nil, `org-agenda-entry-types' is used.")
(defvar cfw:org-icalendars nil
"Org buffers for exporting icalendars.
Setting a list of the custom agenda files, one can use the
different agenda files from the default agenda ones.")
(defvar cfw:org-overwrite-default-keybinding nil
"Overwrites default keybinding. It needs restarting of Emacs(if not work)
For emample,
g cfw:refresh-calendar-buffer
j cfw:org-goto-date
k org-capture
x cfw:org-clean-exit
")
(defun cfw:org-collect-schedules-period (begin end)
"[internal] Return org schedule items between BEGIN and END."
(let ((org-agenda-prefix-format " ")
(span 'day))
(setq org-agenda-buffer
(when (buffer-live-p org-agenda-buffer)
org-agenda-buffer))
(org-compile-prefix-format nil)
(loop for date in (cfw:enumerate-days begin end) append
(loop for file in (or cfw:org-icalendars (org-agenda-files nil 'ifmode))
append
(progn
(org-check-agenda-file file)
(apply 'org-agenda-get-day-entries
file date
cfw:org-agenda-schedule-args))))))
(defun cfw:org-onclick ()
"Jump to the clicked org item."
(interactive)
(let (
(marker (get-text-property (point) 'org-marker))
(link (get-text-property (point) 'org-link))
(file (get-text-property (point) 'cfw:org-file))
(beg (get-text-property (point) 'cfw:org-h-beg))
(loc (get-text-property (point) 'cfw:org-loc)))
(when link
(org-open-link-from-string link))
(when (and marker (marker-buffer marker))
(org-mark-ring-push)
(switch-to-buffer (marker-buffer marker))
(widen)
(goto-char (marker-position marker))
(when (eq major-mode 'org-mode)
(org-reveal)))
(when beg
(find-file file)
(goto-char beg)
(org-cycle))))
(defun cfw:org-jump-map ()
"Jump to the clicked org item."
(interactive)
(let ((loc (get-text-property (point) 'cfw:org-loc)))
(when loc
(google-maps loc))))
(defun cfw:org-clean-exit ()
"Close buffers opened by calfw-org before closing Calendar Framework."
(interactive)
(org-release-buffers org-agenda-new-buffers)
(setq org-agenda-new-buffers nil)
(bury-buffer))
(defvar cfw:org-text-keymap
(let ((map (make-sparse-keymap)))
(define-key map [mouse-1] 'cfw:org-onclick)
(define-key map (kbd "RET") 'cfw:org-onclick)
(define-key map (kbd "C-c C-o") 'cfw:org-onclick)
(define-key map (kbd "m") 'cfw:org-jump-map)
map)
"key map on the calendar item text.")
(defun cfw:org-extract-summary (org-item)
"[internal] Remove some strings."
(let ((item org-item))
;; (when (string-match cfw:org-todo-keywords-regexp item) ; dynamic bind
;; (setq item (replace-match "" nil nil item)))
(when (string-match "^ +" item)
(setq item (replace-match "" nil nil item)))
(when (= 0 (length item))
(setq item (cfw:org-tp org-item 'org-category)))
item))
(defun cfw:org-summary-format (item)
"Format an item. (How should be displayed?)"
(let* ((time (cfw:org-tp item 'time))
(time-of-day (cfw:org-tp item 'time-of-day))
(time-str (and time-of-day
(format "%02i:%02i " (/ time-of-day 100) (% time-of-day 100))))
(category (cfw:org-tp item 'org-category))
(tags (cfw:org-tp item 'tags))
(marker (cfw:org-tp item 'org-marker))
(buffer (and marker (marker-buffer marker)))
(text (cfw:org-extract-summary item))
(props (cfw:extract-text-props item 'face 'keymap))
(extra (cfw:org-tp item 'extra))
(i 0))
;;; ------------------------------------------------------------------------
;;; set title color as default agenda color
;;; ------------------------------------------------------------------------
(setq text (if (string-match "^Deadline:.*" extra)
text
(apply 'propertize (substring-no-properties text) props)))
;;; ------------------------------------------------------------------------
;;; skip time related string
;;; ------------------------------------------------------------------------
(setq text (replace-regexp-in-string "[0-9]\\{2\\}:[0-9]\\{2\\}\\(-[0-9]\\{2\\}:[0-9]\\{2\\}\\)?[\t ]+" "" text))
;;; ------------------------------------------------------------------------
;;; skip tags
;;; ------------------------------------------------------------------------
(if tags
(progn
(while (< i (length tags))
(setq text (replace-regexp-in-string (concat ":" (elt tags i) ":") "" text))
(setq i (+ i 1)))
(setq text (replace-regexp-in-string "[\t ]*:?$" "" text))))
;;; ------------------------------------------------------------------------
;;; act for org link
;;; ------------------------------------------------------------------------
(setq text (replace-regexp-in-string "%[0-9A-F]\\{2\\}" " " text))
(if (string-match org-bracket-link-regexp text)
(let* ((desc (if (match-end 3) (org-match-string-no-properties 3 text)))
(link (org-link-unescape (org-match-string-no-properties 1 text)))
(help (concat "LINK: " link))
(link-props (list
'face 'org-link
'mouse-face 'highlight
'help-echo help
'org-link link)))
(if desc
(progn
(setq desc (apply 'propertize desc link-props))
(setq text (replace-match desc nil nil text)))
(setq link (apply 'propertize link link-props))
(setq text (replace-match link nil nil text)))))
(propertize
(concat
(if time-str
(apply 'propertize time-str props))
text
;; include org filename
;; (and buffer (concat " " (buffer-name buffer)))
)
'keymap cfw:org-text-keymap
;; Delete the display property, since displaying images will break our
;; table layout.
'display nil)))
(defvar cfw:org-schedule-summary-transformer 'cfw:org-summary-format
"Transformation function which transforms the org item string to calendar title.
If this function splits into a list of string, the calfw displays those string in multi-lines.")
(defun cfw:org-normalize-date (date)
"Return a normalized date. (MM DD YYYY)."
(cond
((numberp date)
(calendar-gregorian-from-absolute date))
(t date)))
(defun cfw:org-get-timerange (text)
"Return a range object (begin end text).
If TEXT does not have a range, return nil."
(let* ((dotime (cfw:org-tp text 'dotime)))
(and (stringp dotime) (string-match org-ts-regexp dotime)
(let ((date-string (match-string 1 dotime))
(extra (cfw:org-tp text 'extra)))
(if (string-match "(\\([0-9]+\\)/\\([0-9]+\\)): " extra)
(let* ((cur-day (string-to-number
(match-string 1 extra)))
(total-days (string-to-number
(match-string 2 extra)))
(start-date (time-subtract
(org-read-date nil t date-string)
(seconds-to-time (* 3600 24 (- cur-day 1)))))
(end-date (time-add
(org-read-date nil t date-string)
(seconds-to-time (* 3600 24 (- total-days cur-day))))))
(list (calendar-gregorian-from-absolute (time-to-days start-date))
(calendar-gregorian-from-absolute (time-to-days end-date)) text))
)))))
(defun cfw:org-schedule-period-to-calendar (begin end)
"[internal] Return calfw calendar items between BEGIN and END
from the org schedule data."
(loop
with cfw:org-todo-keywords-regexp = (regexp-opt org-todo-keywords-for-agenda) ; dynamic bind
with contents = nil with periods = nil
for i in (cfw:org-collect-schedules-period begin end)
for date = (cfw:org-tp i 'date)
for line = (funcall cfw:org-schedule-summary-transformer i)
for range = (cfw:org-get-timerange line)
if range do
(unless (member range periods)
(push range periods))
else do
; dotime is not present if this event was already added as a timerange
(if (cfw:org-tp i 'dotime)
(setq contents (cfw:contents-add
(cfw:org-normalize-date date)
line contents)))
finally return (nconc contents (list (cons 'periods periods)))))
(defun cfw:org-schedule-sorter (text1 text2)
"[internal] Sorting algorithm for org schedule items.
TEXT1 < TEXT2."
(condition-case err
(let ((time1 (cfw:org-tp text1 'time-of-day))
(time2 (cfw:org-tp text2 'time-of-day)))
(cond
((and time1 time2) (< time1 time2))
(time1 t) ; time object is moved to upper
(time2 nil) ;
(t (string-lessp text1 text2))))
(error (string-lessp text1 text2))))
(defun cfw:org-schedule-sorter2 (text1 text2)
"[internal] Sorting algorithm for org schedule items.
TEXT1 < TEXT2. This function makes no-time items in front of timed-items."
(condition-case err
(let ((time1 (cfw:org-tp text1 'time-of-day))
(time2 (cfw:org-tp text2 'time-of-day)))
(cond
((and time1 time2) (< time1 time2))
(time1 nil) ; time object is moved to upper
(time2 t) ;
(t (string-lessp text1 text2))))
(error (string-lessp text1 text2))))
(defun cfw:org-format-title (file h-obj t-obj h-beg loc)
(propertize
(concat
(when (org-element-property :hour-start t-obj)
(format "%02i:%02i "
(org-element-property :hour-start t-obj)
(org-element-property :minute-start t-obj)))
(org-element-property :title h-obj))
'keymap cfw:org-text-keymap
'display nil
'cfw:org-file file
'cfw:org-h-beg h-beg
'cfw:org-loc loc))
(defun cfw:org-format-date (t-obj lst)
(mapcar
(lambda (v)
(org-element-property v t-obj)) lst))
(defun cfw:org-filter-datetime (t-obj lst)
(if (car (cfw:org-format-date t-obj lst))
(cfw:org-format-date t-obj lst)
nil))
(defun cfw:org-convert-event (file h-obj t-obj h-beg)
(let ((sdate '(:month-start :day-start :year-start))
(stime '(:hour-start :minute-start))
(edate '(:month-end :day-end :year-end))
(etime '(:hour-end :minute-end))
(loc (org-element-property :LOCATION h-obj)))
(make-cfw:event
:start-date (cfw:org-format-date t-obj sdate)
:start-time (cfw:org-filter-datetime t-obj stime)
:end-date (cfw:org-filter-datetime t-obj edate)
:end-time (cfw:org-filter-datetime t-obj etime)
:title (cfw:org-format-title file h-obj t-obj h-beg loc)
:location loc
:description (if (org-element-property :contents-begin h-obj)
(replace-regexp-in-string
" *:PROPERTIES:\n \\(.*\\(?:\n.*\\)*?\\) :END:\n" ""
(buffer-substring (org-element-property :contents-begin h-obj)
(org-element-property :contents-end h-obj)))
nil))))
(defun cfw:org-convert-org-to-calfw (file)
(save-excursion
(with-current-buffer
(find-file-noselect file)
(let*
((elem-obj (org-element-parse-buffer))
(pos-lst `( ,@(org-element-map elem-obj 'timestamp
(lambda (hl) (org-element-property :begin hl) ))
,@(org-element-map (org-element-map elem-obj 'headline
(lambda (hl)
(org-element-property :deadline hl) ) ) 'timestamp
(lambda (hl) (org-element-property :begin hl) ))
,@(org-element-map (org-element-map elem-obj 'headline
(lambda (hl)
(org-element-property :scheduled hl) ) ) 'timestamp
(lambda (hl) (org-element-property :begin hl) )))))
(loop for pos in pos-lst
do (goto-char pos)
for t-obj = (org-element-timestamp-parser)
for h-obj = (progn
(org-back-to-heading t)
(org-element-headline-parser (point-max) t))
for h-beg = (point)
for event = (cfw:org-convert-event file h-obj t-obj h-beg)
for ts-type = (org-element-property :type t-obj)
if (eq 'active-range ts-type)
collect event into periods
else if (eq 'active ts-type)
collect event into contents
;; else do
;; (message "calfw-org: Cannot handle event")
finally
(kill-buffer (get-file-buffer file))
(return `((periods ,periods) ,@contents)))))))
(defun cfw:org-to-calendar (file begin end)
(loop for event in (cfw:org-convert-org-to-calfw file)
if (and (listp event)
(equal 'periods (car event)))
collect
(cons
'periods
(loop for evt in (cadr event)
if (and
(cfw:date-less-equal-p begin (cfw:event-end-date evt))
(cfw:date-less-equal-p (cfw:event-start-date evt) end))
collect evt))
else if (cfw:date-between begin end (cfw:event-start-date event))
collect event))
(defun cfw:org-create-file-source (name file color)
"Create org-element based source. "
(lexical-let ((file file))
(make-cfw:source
:name (concat "Org:" name)
:color color
:data (lambda (begin end)
(cfw:org-to-calendar file begin end)))))
(defun cfw:org-capture-day ()
(with-current-buffer (get-buffer-create cfw:calendar-buffer-name)
(let ((pos (cfw:cursor-to-nearest-date)))
(concat "<"
(format-time-string "%Y-%m-%d %a"
(encode-time 0 0 0
(calendar-extract-day pos)
(calendar-extract-month pos)
(calendar-extract-year pos)))
">"))))
(when cfw:org-capture-template
(setq org-capture-templates
(append org-capture-templates (list cfw:org-capture-template))))
(defun cfw:org-capture ()
"Open org-agenda buffer on the selected date."
(interactive)
(if cfw:org-capture-template
(org-capture nil (car cfw:org-capture-template))
(message "cfw:org-capture-template is not set yet.")))
(defun cfw:org-open-agenda-day ()
"Open org-agenda buffer on the selected date."
(interactive)
(let ((date (cfw:cursor-to-nearest-date)))
(when date
(org-agenda-list nil (calendar-absolute-from-gregorian date) 'day))))
(define-key
cfw:calendar-mode-map "c" 'cfw:org-capture)
(defvar cfw:org-schedule-map
(cfw:define-keymap
'(
("q" . bury-buffer)
("SPC" . cfw:org-open-agenda-day)
))
"Key map for the calendar buffer.")
(defvar cfw:org-custom-map
(cfw:define-keymap
'(
("g" . cfw:refresh-calendar-buffer)
("j" . cfw:org-goto-date)
("k" . org-capture)
("q" . bury-buffer)
("x" . cfw:org-clean-exit)
("SPC" . cfw:org-open-agenda-day)
))
"Key map for the calendar buffer.")
(defun cfw:org-create-source (&optional color)
"Create org-agenda source."
(make-cfw:source
:name "org-agenda"
:color (or color "Seagreen4")
:data 'cfw:org-schedule-period-to-calendar))
(defun cfw:open-org-calendar ()
"Open an org schedule calendar in the new buffer."
(interactive)
(save-excursion
(let* ((source1 (cfw:org-create-source))
(curr-keymap (if cfw:org-overwrite-default-keybinding cfw:org-custom-map cfw:org-schedule-map))
(cp (cfw:create-calendar-component-buffer
:view 'month
:contents-sources (list source1)
:custom-map curr-keymap
:sorter 'cfw:org-schedule-sorter)))
(switch-to-buffer (cfw:cp-get-buffer cp)))))
(defun cfw:org-from-calendar ()
"Do something. This command should be executed on the calfw calendar."
(interactive)
(let* ((mdy (cfw:cursor-to-nearest-date))
(m (calendar-extract-month mdy))
(d (calendar-extract-day mdy))
(y (calendar-extract-year mdy)))
;; exec org-remember here?
))
(defun cfw:org-read-date-command ()
"Move the cursor to the specified date."
(interactive)
(cfw:emacs-to-calendar (org-read-date nil 'to-time)))
(defun cfw:org-goto-date ()
"Move the cursor to the specified date."
(interactive)
(cfw:navi-goto-date
(cfw:org-read-date-command)))
;; (progn (eval-current-buffer) (cfw:open-org-calendar))
;; (setq org-agenda-files '("./org-samples/complex.org"))
(provide 'calfw-org)
;;; calfw-org.el ends here