summaryrefslogtreecommitdiff
path: root/elchemy-dashboard.el
blob: 0768483412b843f6bed977045769bf1cf133bd7a (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
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
(defun elchemy/create-dashboard ()
  "Create the user dashboard"
  (interactive)
  (let ((buffer (get-buffer-create "*Dashboard*")))
    (switch-to-buffer buffer)
    (unless buffer-read-only
      (when (file-exists-p (concat elchemy/elchemy-root elchemy/dashboard-splash))
	(insert-image (create-image (concat elchemy/elchemy-root elchemy/dashboard-splash) nil nil :scale 0.25))
	(insert "\n"))
      (let ((start (point)))
	(insert "Elchemy Dashboard")
	(add-text-properties start (point)
			     '(face (:height 4.0))))
      (insert "\n\n")
      (dotimes (i (length elchemy/dashboard/heading-buttons))
	(let* ((button (nth i elchemy/dashboard/heading-buttons))
	       (title (car button))
	       (callback (cdr button)))
	  (insert (buttonize title callback))
	  (if (eq (% (+ i 1) elchemy/dashboard/heading-columns) 0)
	      (insert "\n")
	    (insert (format (concat "%-" (format "%d" (+ (- elchemy/dashboard/heading-max-length (length title)) elchemy/dashboard/heading-padding)) "s") " ")))))
      (unless (eq (% (length elchemy/dashboard/heading-buttons) elchemy/dashboard/heading-columns) 0)
	(insert "\n"))
      (insert "\n\n")
      (when (file-exists-p (concat elchemy/elchemy-root elchemy/elchemy-projects-file))
	(let ((start (point)))
	  (insert "Projects")
	  (add-text-properties start (point)
			       '(face (:height 1.5))))
	(insert "\n")
	(mapcar #'(lambda (x) (let ((name (car x))
			       (path (cdr x)))
			   (insert (buttonize name (lambda (y) (elchemy/find-file y)) path) "\n")))
		(elchemy/read-alist-file (concat elchemy/elchemy-root elchemy/elchemy-projects-file)))
        (insert "\n"))
      (ignore-errors
	(setq agenda-items (mapcar #'(lambda (x) (elchemy/get-agenda-items x 3)) elchemy/dashboard-agenda-titles)
	      agenda-max-count (apply 'max (mapcar #'(lambda (x) (length x)) agenda-items))
	      agenda-max-length (apply 'max (mapcar #'(lambda (x) (apply 'max (mapcar #'(lambda (y) (length y)) x))) agenda-items)))
	(let ((start (point)))
	  (insert "Agenda")
	  (add-text-properties start (point)
			       '(face (:height 1.5))))
	(insert "\n")
	(let ((start (point)))
	  (insert (apply 'format (concat "%-" (format "%d" (+ agenda-max-length elchemy/dashboard-agenda-padding)) "s" "%-" (format "%d" (+ agenda-max-length elchemy/dashboard-agenda-padding)) "s" "%-" (format "%d" (+ agenda-max-length elchemy/dashboard-agenda-padding)) "s") elchemy/dashboard-agenda-titles))
	  (add-text-properties start (point)
			       '(face (:weight bold :slant italic :foreground "red"))))
	(insert "\n")
	(dotimes (i agenda-max-count)
	  (insert (format
		   (concat "%-" (format "%d" (+ agenda-max-length elchemy/dashboard-agenda-padding)) "s")
		   (elchemy/replace-nil (nth i (nth 0 agenda-items)))))
	  (insert (format
		   (concat "%-" (format "%d" (+ agenda-max-length elchemy/dashboard-agenda-padding)) "s")
		   (elchemy/replace-nil (nth i (nth 1 agenda-items)))))
	  (insert (format
		   (concat "%-" (format "%d" (+ agenda-max-length elchemy/dashboard-agenda-padding)) "s")
		   (elchemy/replace-nil (nth i (nth 2 agenda-items)))))
	  (insert "\n")
	  ))
      (insert "\n")
      (let ((start (point)))
	(insert "Command Reference")
	(add-text-properties start (point)
			     '(face (:height 1.5))))
      (insert "\n")
      (insert "C-/       ~ Undo\n")
      (insert "C-x n n   ~ Narrow\n")
      (insert "C-x n w   ~ Widen\n")
      (insert "M-%       ~ Query Replace\n")
      (insert "C-M-s     ~ Regex Search\n")
      (insert "F3        ~ Record Macro\n")
      (insert "F4        ~ Play Macro\n")
      (insert "M-0 F4    ~ Play Macro until failure\n")
      (insert "M-l       ~ Lowercase following word\n")
      (insert "M-u       ~ Uppercase following word\n")
      (insert "M-c       ~ Capitalize following word\n")
      (insert "M-g w     ~ Jump to word\n")
      (insert "M-g l     ~ Jump to line\n")
      (insert "C-x C-l   ~ Lowercase Region\n")
      (insert "C-x C-u   ~ Uppercase Region\n")
      (insert "C-M-n     ~ Move forward one balanced expression\n")
      (insert "C-M-p     ~ Move forward one balanced expression\n")
      (insert "\n")
      (insert (buttonize "Refresh" '(lambda (x) (elchemy/recreate-dashboard))))
      (insert "\n")
      (button-mode +1)
      (read-only-mode +1))))

(defun elchemy/recreate-dashboard ()
  "Create the user dashboard"
  (interactive)
  (let ((buffer (get-buffer-create "*Dashboard*")))
    (switch-to-buffer buffer)
    (read-only-mode -1)
    (erase-buffer)
    (when (file-exists-p (concat elchemy/elchemy-root elchemy/dashboard-splash))
      (insert-image (create-image (concat elchemy/elchemy-root elchemy/dashboard-splash) nil nil :scale 0.25))
      (insert "\n"))
    (let ((start (point)))
      (insert "Elchemy Dashboard")
      (add-text-properties start (point)
			   '(face (:height 4.0))))
    (insert "\n\n")
    (dotimes (i (length elchemy/dashboard/heading-buttons))
      (let* ((button (nth i elchemy/dashboard/heading-buttons))
	     (title (car button))
	     (callback (cdr button)))
	(insert (buttonize title callback))
	(if (eq (% (+ i 1) elchemy/dashboard/heading-columns) 0)
	    (insert "\n")
	  (insert (format (concat "%-" (format "%d" (+ (- elchemy/dashboard/heading-max-length (length title)) elchemy/dashboard/heading-padding)) "s") " ")))))
    (unless (eq (% (length elchemy/dashboard/heading-buttons) elchemy/dashboard/heading-columns) 0)
      (insert "\n"))
    (insert "\n\n")
    (when (file-exists-p (concat elchemy/elchemy-root elchemy/elchemy-projects-file))
      (let ((start (point)))
	(insert "Projects")
	(add-text-properties start (point)
			     '(face (:height 1.5))))
      (insert "\n")
      (mapcar #'(lambda (x) (let ((name (car x))
			     (path (cdr x)))
			 (insert (buttonize name (lambda (y) (elchemy/find-file y)) path) "\n")))
	      (elchemy/read-alist-file (concat elchemy/elchemy-root elchemy/elchemy-projects-file)))
      (insert "\n"))
    (let ((start (point)))
      (insert "Agenda")
      (add-text-properties start (point)
			   '(face (:height 1.5))))
    (insert "\n")
    (setq agenda-items (mapcar #'(lambda (x) (elchemy/get-agenda-items x 3)) elchemy/dashboard-agenda-titles)
	  agenda-max-count (apply 'max (mapcar #'(lambda (x) (length x)) agenda-items))
	  agenda-max-length (apply 'max (mapcar #'(lambda (x) (apply 'max (mapcar #'(lambda (y) (length y)) x))) agenda-items)))
    (insert (apply 'format (concat "%-" (format "%d" (+ agenda-max-length elchemy/dashboard-agenda-padding)) "s" "%-" (format "%d" (+ agenda-max-length elchemy/dashboard-agenda-padding)) "s" "%s\n") elchemy/dashboard-agenda-titles))
    (dotimes (i agenda-max-count)
      (insert (format
	       (concat "%-" (format "%d" (+ agenda-max-length elchemy/dashboard-agenda-padding)) "s")
	       (elchemy/replace-nil (nth i (nth 0 agenda-items)))))
      (insert (format
	       (concat "%-" (format "%d" (+ agenda-max-length elchemy/dashboard-agenda-padding)) "s")
	       (elchemy/replace-nil (nth i (nth 1 agenda-items)))))
      (insert (format
	       "%s"
	       (elchemy/replace-nil (nth i (nth 2 agenda-items)))))
      (insert "\n")
      )
    (insert "\n")
    (let ((start (point)))
      (insert "Overdue")
      (add-text-properties start (point)
			   '(face (:height 1.5))))
    (insert "\n")
    (insert (elchemy/format-processed-agenda (mapcar 'elchemy/process-agenda-heading (org-ql-select (org-agenda-files) `(and (todo "STRT" "WAIT" "TODO" "CYCL") (ts :from ,(- elchemy/schedule-lookahead) :to today)) :sort '(todo priority date)))))
    (insert "\n")
    (let ((start (point)))
      (insert "Upcoming")
      (add-text-properties start (point)
			   '(face (:height 1.5))))
    (insert "\n")
    (insert (elchemy/format-processed-agenda (mapcar 'elchemy/process-agenda-heading (org-ql-select (org-agenda-files) `(and (todo "STRT" "WAIT" "TODO" "CYCL") (ts :from today :to ,elchemy/schedule-lookahead)) :sort '(todo priority date)))))
    (insert "\n")
    (let ((start (point)))
      (insert "Command Reference")
      (add-text-properties start (point)
			   '(face (:height 1.5))))
    (insert "\n")
    (insert "C-/       ~ Undo\n")
    (insert "C-x n n   ~ Narrow\n")
    (insert "C-x n w   ~ Widen\n")
    (insert "M-%       ~ Query Replace\n")
    (insert "C-M-s     ~ Regex Search\n")
    (insert "F3        ~ Record Macro\n")
    (insert "F4        ~ Play Macro\n")
    (insert "M-0 F4    ~ Play Macro until failure\n")
    (insert "M-l       ~ Lowercase following word\n")
    (insert "M-u       ~ Uppercase following word\n")
    (insert "M-c       ~ Capitalize following word\n")
    (insert "M-g w     ~ Jump to word\n")
    (insert "M-g l     ~ Jump to line\n")
    (insert "C-x C-l   ~ Lowercase Region\n")
    (insert "C-x C-u   ~ Uppercase Region\n")
    (insert "C-M-n     ~ Move forward one balanced expression\n")
    (insert "C-M-p     ~ Move forward one balanced expression\n")
    (insert "\n")
    (insert (buttonize "Refresh" '(lambda (x) (elchemy/recreate-dashboard))))
    (insert "\n")
    (button-mode +1)
    (read-only-mode +1)))

(provide 'elchemy-dashboard)