forked from rougier/nano-emacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nano-agenda.el
380 lines (320 loc) · 14.4 KB
/
nano-agenda.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
;;; -*- lexical-binding: t -*-
;; ---------------------------------------------------------------------
;; GNU Emacs / N Λ N O - Emacs made simple
;; Copyright (C) 2020 - N Λ N O developers
;;
;; 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/>.
;; ---------------------------------------------------------------------
;; An experimental interactive nano-agenda that displays side by side a
;; mini calendar on the left and timestamped org entries on the right.
;; ---------------------------------------------------------------------
(require 'ts)
(require 'org)
(require 'org-agenda)
(require 'calendar)
(require 'holidays)
;; --- Faces -----------------------------------------------------------
(defgroup nano-agenda-faces nil
"Nano-Agenda faces")
(defface nano-agenda-face-default
'((t :inherit 'default ))
"Default face"
:group 'nano-agenda-faces)
(defface nano-agenda-face-selected
`((t :foreground ,nano-light-background
:background ,nano-light-foreground ))
"Face for the selected day"
:group 'nano-agenda-faces)
(defface nano-agenda-face-today
`((t :foreground ,nano-light-popout
:inherit 'bold ))
"Today face when not selected."
:group 'nano-agenda-faces)
(defface nano-agenda-face-selected-today
`((t :foreground ,nano-light-background
:background ,nano-light-popout ))
"Today face when selected."
:group 'nano-agenda-faces)
(defface nano-agenda-face-weekend
`((t :foreground ,nano-light-faded ))
"Weekend face"
:group 'nano-agenda-faces)
(defface nano-agenda-face-holidays
`((t :foreground ,nano-light-faded ))
"Holidays face"
:group 'nano-agenda-faces)
(defface nano-agenda-face-outday
`((t :foreground ,nano-light-subtle ))
"Out day face"
:group 'nano-agenda-faces)
(defface nano-agenda-face-day-name
`((t :foreground ,nano-light-faded ))
"Day name face (on second line)"
:group 'nano-agenda-faces)
(defface nano-agenda-face-month-name
'((t :inherit 'bold ))
"Month name face (on first line)"
:group 'nano-agenda-faces)
(defface nano-agenda-face-mouse
'((t :inherit 'highlight ))
"Mouse highlight face"
:group 'nano-agenda-faces)
(defface nano-agenda-face-button
`((t :foreground ,nano-light-faded ))
"Header button (left and right)"
:group 'nano-agenda-faces)
;; --- Global variable -------------------------------------------------
(setq nano-agenda-selected (ts-now))
(setq nano-agenda-file "~/Documents/org/agenda.org")
;; --- Useful functions ------------------------------------------------
(defun center-string (string size)
(let* ((padding (/ (- size (length string)) 2))
(lpad (+ (length string) padding))
(lformat (format "%%%ds" lpad))
(rformat (format "%%%ds" (- size))))
(format rformat (format lformat string))))
;; --- Nano-Agenda minor mode ---------------------------------------------
(define-minor-mode nano-agenda-mode
"Minor mode for nano-agenda."
:init nil
:lighter "Calendar"
:keymap (make-sparse-keymap)
(when nano-agenda-mode
(setq buffer-read-only t)
(setq cursor-type nil)
(local-set-key (kbd "<left>") #'nano-agenda-backward-day)
(local-set-key (kbd "<right>") #'nano-agenda-forward-day)
(local-set-key (kbd "<up>") #'nano-agenda-backward-week)
(local-set-key (kbd "<down>") #'nano-agenda-forward-week)
(local-set-key (kbd "<S-left>") #'nano-agenda-backward-month)
(local-set-key (kbd "<S-right>") #'nano-agenda-forward-month)
(local-set-key (kbd "<S-up>") #'nano-agenda-backward-year)
(local-set-key (kbd "<S-down>") #'nano-agenda-forward-year)
(local-set-key (kbd ".") #'nano-agenda-select-today)
(local-set-key (kbd "t") #'nano-agenda-select-today)
(local-set-key (kbd "q") #'nano-agenda-close)
(local-set-key (kbd "<return>") #'nano-agenda-close)
(local-set-key (kbd "<escape>") #'nano-agenda-cancel)))
;; --- Navigation ------------------------------------------------------
(defun nano-agenda-forward-day ()
(interactive)
(setq nano-agenda-selected (ts-inc 'day 1 nano-agenda-selected))
(nano-agenda))
(defun nano-agenda-backward-day ()
(interactive)
(setq nano-agenda-selected (ts-dec 'day 1 nano-agenda-selected))
(nano-agenda))
(defun nano-agenda-forward-week ()
(interactive)
(setq nano-agenda-selected (ts-inc 'day 7 nano-agenda-selected))
(nano-agenda))
(defun nano-agenda-backward-week ()
(interactive)
(setq nano-agenda-selected (ts-dec 'day 7 nano-agenda-selected))
(nano-agenda))
(defun nano-agenda-forward-month ()
(interactive)
(setq nano-agenda-selected (ts-inc 'month 1 nano-agenda-selected))
(nano-agenda))
(defun nano-agenda-backward-month ()
(interactive)
(setq nano-agenda-selected (ts-dec 'month 1 nano-agenda-selected))
(nano-agenda))
(defun nano-agenda-forward-year ()
(interactive)
(setq nano-agenda-selected (ts-inc 'year 1 nano-agenda-selected))
(nano-agenda))
(defun nano-agenda-backward-year ()
(interactive)
(setq nano-agenda-selected (ts-dec 'year 1 nano-agenda-selected))
(nano-agenda))
(defun nano-agenda-select-today ()
(interactive)
(setq nano-agenda-selected (ts-now))
(nano-agenda))
(defun nano-agenda-close ()
(interactive)
(kill-buffer "*nano-agenda*"))
(defun nano-agenda-select ()
(interactive)
(kill-buffer "*nano-agenda*"))
(defun nano-agenda-cancel ()
(interactive)
(kill-buffer "*nano-agenda*"))
;; --- Display ---------------------------------------------------------
(defun nano-agenda-header-month (selected)
(let* ((map-left (make-sparse-keymap))
(map-right (make-sparse-keymap)))
(define-key map-left (kbd "<down-mouse-1>") #'nano-agenda-backward-month)
(define-key map-right (kbd "<down-mouse-1>") #'nano-agenda-forward-month)
(concat
(propertize "<" 'face 'nano-agenda-face-button
'mouse-face 'nano-agenda-face-mouse
'help-echo "Previous month"
'keymap map-left)
(propertize (center-string (format "%s %d" (ts-month-name selected)
(ts-year selected)) 18)
'face 'nano-agenda-face-month-name)
(propertize ">" 'face 'nano-agenda-face-button
'mouse-face 'nano-agenda-face-mouse
'help-echo "Next month"
'keymap map-right)
" ")))
(defun nano-agenda-header-names (selected)
(propertize "Mo Tu We Th Fr Sa Su "
'face 'nano-agenda-face-day-name))
(defun nano-agenda-body-days (selected)
(let* ((today (ts-now))
(day (ts-day selected))
(month (ts-month selected))
(year (ts-year selected))
(start (make-ts :year year :month month :day 1
:hour 0 :minute 0 :second 0))
(dow (mod (+ 6 (ts-dow start)) 7))
(start (ts-dec 'day dow start))
(result ""))
(dotimes (row 6)
(dotimes (col 7)
(let* ((day (+ (* row 7) col))
(date (ts-inc 'day day start))
(map (make-sparse-keymap))
(is-today (and (= (ts-year date) (ts-year today))
(= (ts-doy date) (ts-doy today))))
(is-selected (and (= (ts-year date) (ts-year selected))
(= (ts-doy date) (ts-doy selected))))
(is-selected-today (and is-selected is-today))
(is-outday (not (= (ts-month date) month)))
(is-holidays (calendar-check-holidays (list
(ts-month date)
(ts-day date)
(ts-year date))))
(is-weekend (or (= (ts-dow date) 0) (= (ts-dow date) 6)))
(face (cond (is-selected-today 'nano-agenda-face-selected-today)
(is-selected 'nano-agenda-face-selected)
(is-today 'nano-agenda-face-today)
(is-outday 'nano-agenda-face-outday)
(is-weekend 'nano-agenda-face-weekend)
(is-holidays 'nano-agenda-face-holidays)
(t 'nano-agenda-face-default))))
(define-key map (kbd "<down-mouse-1>")
`(lambda() (interactive) (nano-agenda ,date)))
(setq result (concat result
(propertize (format "%2d" (ts-day date))
'face face
'mouse-face (cond (is-selected-today 'nano-agenda-face-selected-today)
(is-selected 'nano-agenda-face-selected)
(t 'nano-agenda-face-mouse))
'help-echo (format "%s%s" (ts-format "%A %-e %B %Y" date)
(if is-holidays (format " (%s)" (nth 0 is-holidays))
""))
'keymap map)
" "))))
(setq result (concat result "\n")))
result))
;; ---------------------------------------------------------------------
(defun nano-agenda (&optional selected)
(interactive)
(if selected
(setq nano-agenda-selected selected))
;; -- Create frame if necessary ---
(condition-case nil
(select-frame-by-name "*nano-agenda-frame*")
(error
(make-frame `((name . "*nano-agenda-frame*")
(unsplittable . t)
(buffer-predicate . (lambda (x) nil))
(internal-border-width . 0)
(minibuffer . nil)))))
(select-frame-by-name "*nano-agenda-frame*")
(modify-frame-parameters nil '((width . 72)
(height . 10)))
(switch-to-buffer (get-buffer-create "*nano-agenda*"))
(set-window-dedicated-p (get-buffer-window ) t)
;; --- Display agenda ---
(with-current-buffer (get-buffer-create "*nano-agenda*")
(switch-to-buffer "*nano-agenda*")
(let ((inhibit-read-only t))
(erase-buffer)
(set-window-margins nil 2)
(face-remap-add-relative 'header-line
`(:family "Roboto Mono"
:foreground ,nano-light-background
:background ,nano-light-faded
:weight regular
:box (:line-width 2 :color "#ffffff" :style nil)))
(setq mode-line-format nil)
(setq header-line-format nil)
(insert "\n")
(insert (nano-agenda-header-month nano-agenda-selected))
(insert "\n")
(insert (nano-agenda-header-names nano-agenda-selected))
(insert "\n")
(insert (nano-agenda-body-days nano-agenda-selected))
(insert "\n")
;; --- Org agenda entries ---
(goto-char (point-min))
(forward-line 1)
(end-of-line)
(let ((is-holidays (calendar-check-holidays (list
(ts-month nano-agenda-selected)
(ts-day nano-agenda-selected)
(ts-year nano-agenda-selected)))))
;; (setq header-line-format (concat
;; (propertize " " 'display '(raise +0.25))
;; " "
;; (ts-format "%A %-e %B %Y" nano-agenda-selected)
;; (if is-holidays (format " (%s)" (nth 0 is-holidays)) "")
;; (propertize " " 'display '(raise -0.35))))
(insert (concat (propertize (ts-format " %A %-e %B %Y" nano-agenda-selected)
'face 'nano-agenda-face-month-name)
(propertize
(if is-holidays (format " (%s)" (nth 0 is-holidays)) "")
'face 'nano-agenda-face-holidays))))
(forward-line 2)
(end-of-line)
(let* ((org-agenda-hide-tags-regexp ".")
(date nano-agenda-selected)
(file nano-agenda-file)
(date (list (ts-month date) (ts-day date) (ts-year date)))
(entries (cl-sort (org-agenda-get-day-entries file date :timestamp) 'string-lessp)))
(setq num 0)
(while (< num (min 5 (length entries)))
(let* ((entry (nth num entries))
(text (substring-no-properties (format "%s" entry))))
(insert (propertize
(truncate-string-to-width
(if (string-match org-link-bracket-re text)
(replace-match "[-]" nil nil text) text) 46 nil nil "…")
'face 'nano-face-salient)))
(forward-line)
(end-of-line)
(setq num (1+ num))
)
(if (> (length entries) 5)
(insert (propertize (format " + %s non-displayed event(s)" (- (length entries) 5))
'face 'nano-agenda-face-holidays))))
;; --- End org agenda entries ---
(goto-char (point-min)))
(let ((message-log-max)
(is-holidays (calendar-check-holidays (list
(ts-month nano-agenda-selected)
(ts-day nano-agenda-selected)
(ts-year nano-agenda-selected)))))
(message (format "%s%s" (ts-format "%A %-e %B %Y" nano-agenda-selected)
(propertize
(if is-holidays (format " (%s)" (nth 0 is-holidays)) "")
'face 'nano-agenda-face-holidays))))
(nano-agenda-mode t)))
;;
(provide 'nano-agenda)