forked from TeMPOraL/nyan-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorg-roam-nyan-mode.el
392 lines (328 loc) · 14.3 KB
/
org-roam-nyan-mode.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
;;; org-roam-nyan-mode.el --- Nyan Cat shows how many ideas you are trying to create each day.
;; Nyanyanyanyanyanyanya!
;; Author: Ran Wang
;; URL:
;; Version:
;; Keywords: nyan, cat, progress bar, org-roam
;; This file is not part of GNU Emacs.
;; 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, 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;; Commentary:
;; Contributions and feature requests welcome!
;; Inspired by Nyan-mode written by Jacek "TeMPOraL" Zlydach.
;; See: https://github.com/TeMPOraL/nyan-mode/
;;; History:
;;; Code:
;;;; Requirements
(require 'org-real)
(require 'ts)
(defcustom zk-reminder-string "Wk:"
"Information need to keep in mind."
:type 'string
:group 'nyan)
(defcustom zk-reminder-date-string "2021-01-01"
"By setting date, the modeline will show how many weeks past
from a date."
:type 'string
:group 'nyan)
(defcustom zk-active-proj-path "~/org.org"
""
:type 'string
:group 'nyan)
(defun zk-how-many-weeks-from-a-date ()
(/ (round (/ (- (ts-unix (ts-now))
(ts-unix (ts-parse zk-reminder-date-string)))
86400))
7))
(defun get-string-from-file (filePath)
"Return filePath's file content."
(with-temp-buffer
(insert-file-contents filePath)
(buffer-string)))
(defun today-effort-str ()
"Return a string of how many ideas generated today"
(substring (get-string-from-file "~/.emacs.d/nyan-mode/log.today_effort") 0 -1))
(defun today-effort ()
"Check how many ideas generated today"
(float (string-to-number (today-effort-str))))
(defcustom zk-daily-goal 10
"Number of seconds between animation frames."
:type 'float
:group 'nyan)
(defun read-lines (filePath)
"Return a list of lines of a file at filePath."
(with-temp-buffer
(insert-file-contents filePath)
(split-string (buffer-string) "\n" t)))
(defun open-proj-button-function ()
(interactive)
(call-interactively 'dummy/org-real-headlines))
(setq open-pad-button
(propertize " [Pad] "
'help-echo "Click to Open Active Projects"
'mouse-face 'mode-line-highlight
'local-map (let ((map (make-sparse-keymap)))
(define-key map [mode-line mouse-1] 'open-proj-button-function)
map)))
(defun dummy/org-real-headlines ()
"View all org headlines as an org real diagram.
MAX-LEVEL is the maximum level to show headlines for."
(interactive)
(delete-other-windows)
(find-file zk-active-proj-path)
(org-real--pp
(org-real--parse-headlines)
nil
'display-buffer-same-window
t 2 3))
(defun org-roam-mode-line-need-more-effort ()
(when (string-match-p (expand-file-name org-roam-directory) (format "%s" buffer-file-name))
(progn (setq mode-line-format
(list
'(:eval
(window-numbering-get-number-string))
" "
'mode-line-mule-info
'mode-line-modified
'mode-line-frame-identification
" "
'(:eval (list (nyan-create)))
"|"
"%n"
" Today: "
'(:eval (today-effort-str))
" Goal: "
'(:eval (format "%s" zk-daily-goal))
" "
" | "
zk-reminder-string
(format "%s" (zk-how-many-weeks-from-a-date))
'global-mode-string
" "
open-pad-button
))
(force-mode-line-update)
)))
;; (setq mode-line-end-spaces
;; (list open-pad-button))
(add-hook 'before-save-hook #'org-roam-mode-line-need-more-effort)
(defconst +nyan-directory+ (file-name-directory (or load-file-name buffer-file-name)))
(defconst +nyan-cat-size+ 3)
(defconst +nyan-cat-image+ (concat +nyan-directory+ "img/nyan.xpm"))
(defconst +nyan-rainbow-image+ (concat +nyan-directory+ "img/rainbow.xpm"))
(defconst +nyan-outerspace-image+ (concat +nyan-directory+ "img/outerspace.xpm"))
(defconst +nyan-modeline-help-string+ "Nyanyanya!\nmouse-1: Scroll buffer position")
(defvar nyan-old-car-mode-line-position nil)
(defgroup nyan nil
"Customization group for `nyan-mode'."
:group 'frames)
(defun nyan-refresh ()
"Refresh nyan mode.
Intended to be called when customizations were changed, to reapply them immediately."
(when (featurep 'nyan-mode)
(when (and (boundp 'nyan-mode)
nyan-mode)
(nyan-mode -1)
(nyan-mode 1))))
(defcustom nyan-animation-frame-interval 0.2
"Number of seconds between animation frames."
:type 'float
:set (lambda (sym val)
(set-default sym val)
(nyan-refresh))
:group 'nyan)
(defvar nyan-animation-timer nil)
(defun nyan--is-animating-p ()
"T if animating, NIL otherwise."
(timerp nyan-animation-timer))
(defun nyan-start-animation ()
(interactive)
(unless (nyan--is-animating-p)
(setq nyan-animation-timer (run-at-time "1 sec"
nyan-animation-frame-interval
'nyan-swich-anim-frame))))
(defun nyan-stop-animation ()
(interactive)
(when (nyan--is-animating-p)
(cancel-timer nyan-animation-timer)
(setq nyan-animation-timer nil)))
(defcustom nyan-minimum-window-width 64
"Minimum width of the window, below which nyan-mode will not be displayed.
This is important because nyan-mode will push out all informations from small windows."
:type 'integer
:set (lambda (sym val)
(set-default sym val)
(nyan-refresh))
:group 'nyan)
;;; FIXME bug, doesn't work for antoszka.
(defcustom nyan-wavy-trail nil
"If enabled, Nyan Cat's rainbow trail will be wavy."
:type '(choice (const :tag "Enabled" t)
(const :tag "Disabled" nil))
:set (lambda (sym val)
(set-default sym val)
(nyan-refresh))
:group 'nyan)
(defcustom nyan-bar-length 32
"Length of Nyan Cat bar in units.
Each unit is equal to an 8px image.
Minimum of 3 units are required for Nyan Cat."
:type 'integer
:set (lambda (sym val)
(set-default sym val)
(nyan-refresh))
:group 'nyan)
(defcustom nyan-animate-nyancat nil
"Enable animation for Nyan Cat.
This can be t or nil."
:type '(choice (const :tag "Enabled" t)
(const :tag "Disabled" nil))
;; FIXME: Starting an animation timer on defcustom isn't a good idea; this needs to, at best, maybe start/stop a timer iff the mode is on,
;; otherwise just set a flag. -- Jacek Złydach, 2020-05-26
:set (lambda (sym val)
(set-default sym val)
(if val
(nyan-start-animation)
(nyan-stop-animation))
(nyan-refresh))
:group 'nyan)
(defcustom nyan-cat-face-number 1
"Select cat face number for console."
:type 'integer
:group 'nyan
)
;;; Load images of Nyan Cat an it's rainbow.
(defvar nyan-cat-image (if (image-type-available-p 'xpm)
(create-image +nyan-cat-image+ 'xpm nil :ascent 'center)))
(defvar nyan-animation-frames (if (image-type-available-p 'xpm)
(mapcar (lambda (id)
(create-image (concat +nyan-directory+ (format "img/nyan-frame-%d.xpm" id))
'xpm nil :ascent 95))
'(1 2 3 4 5 6))))
(defvar nyan-current-frame 0)
(defconst +nyan-catface+ [
["[]*" "[]#"]
["(*^ー゚)" "( ^ー^)" "(^ー^ )" "(゚ー^*)"]
["(´ω`三 )" "( ´ω三` )" "( ´三ω` )" "( 三´ω`)"
"( 三´ω`)" "( ´三ω` )" "( ´ω三` )" "(´ω`三 )"]
["(´д`;)" "( ´д`;)" "( ;´д`)" "(;´д` )"]
["(」・ω・)」" "(/・ω・)/" "(」・ω・)」" "(/・ω・)/"
"(」・ω・)」" "(/・ω・)/" "(」・ω・)」" "\(・ω・)/"]
["(>ワ<三 )" "( >ワ三< )"
"( >三ワ< )" "( 三>ワ<)"
"( >三ワ< )" "( >ワ三< )"]])
(defun nyan-toggle-wavy-trail ()
"Toggle the trail to look more like the original Nyan Cat animation."
(interactive)
(setq nyan-wavy-trail (not nyan-wavy-trail)))
(defun nyan-swich-anim-frame ()
(setq nyan-current-frame (% (+ 1 nyan-current-frame) 6))
(redraw-modeline))
(defun nyan-get-anim-frame ()
(if (nyan--is-animating-p)
(nth nyan-current-frame nyan-animation-frames)
nyan-cat-image))
(defun nyan-wavy-rainbow-ascent (number)
(if (nyan--is-animating-p)
(min 100 (+ 90
(* 3 (abs (- (/ 6 2)
(% (+ number nyan-current-frame)
6))))))
(if (zerop (% number 2)) 80 'center)))
(defun nyan-number-of-rainbows ()
(round (/ (* (round (* 100
(if (> (/ (today-effort) zk-daily-goal) 1)
(float 1)
(/ (today-effort) zk-daily-goal))
))
(- nyan-bar-length +nyan-cat-size+))
100)))
(defun nyan-catface ()
(aref +nyan-catface+ nyan-cat-face-number))
(defun nyan-catface-index ()
(min (round (/ (* (round (* 100
(/ (- (float (point))
(float (point-min)))
(float (point-max)))))
(length (nyan-catface)))
100)) (- (length (nyan-catface)) 1)))
(defun nyan-scroll-buffer (percentage buffer)
"Move point `BUFFER' to `PERCENTAGE' percent in the buffer."
(interactive)
(with-current-buffer buffer
(goto-char (floor (* percentage (point-max))))))
(defun nyan-add-scroll-handler (string percentage buffer)
"Propertize `STRING' to scroll `BUFFER' to `PERCENTAGE' on click."
(lexical-let ((percentage percentage)
(buffer buffer))
(propertize string 'keymap `(keymap (mode-line keymap (down-mouse-1 . ,(lambda () (interactive) (nyan-scroll-buffer percentage buffer))))))))
(defun nyan-create ()
"Return the Nyan Cat indicator to be inserted into mode line."
(if (< (window-width) nyan-minimum-window-width)
"" ; disabled for too small windows
(let* ((rainbows (nyan-number-of-rainbows))
(outerspaces (- nyan-bar-length rainbows +nyan-cat-size+))
(rainbow-string "")
(xpm-support (image-type-available-p 'xpm))
(nyancat-string (propertize
(aref (nyan-catface) (nyan-catface-index))
'display (nyan-get-anim-frame)))
(outerspace-string "")
(buffer (current-buffer)))
(dotimes (number rainbows)
(setq rainbow-string (concat rainbow-string
(nyan-add-scroll-handler
(if xpm-support
(propertize "|"
'display (create-image +nyan-rainbow-image+ 'xpm nil :ascent (or (and nyan-wavy-trail
(nyan-wavy-rainbow-ascent number))
(if (nyan--is-animating-p) 95 'center))))
"|")
(/ (float number) nyan-bar-length) buffer))))
(dotimes (number outerspaces)
(setq outerspace-string (concat outerspace-string
(nyan-add-scroll-handler
(if xpm-support
(propertize "-"
'display (create-image +nyan-outerspace-image+ 'xpm nil :ascent (if (nyan--is-animating-p) 95 'center)))
"-")
(/ (float (+ rainbows +nyan-cat-size+ number)) nyan-bar-length) buffer))))
;; Compute Nyan Cat string.
(propertize (concat rainbow-string
nyancat-string
outerspace-string)
'help-echo +nyan-modeline-help-string+))))
;;;###autoload
(define-minor-mode nyan-mode
"Use NyanCat to show buffer size and position in mode-line.
You can customize this minor mode, see option `nyan-mode'.
Note: If you turn this mode on then you probably want to turn off
option `scroll-bar-mode'."
:global t
:group 'nyan
;; FIXME: That doesn't smell right; might still get duplicate nyan cats and other mode-line disruptions. -- Jacek Złydach, 2020-05-26
(cond (nyan-mode
(unless nyan-old-car-mode-line-position
(setq nyan-old-car-mode-line-position (car mode-line-position)))
(setcar mode-line-position '(:eval (list (nyan-create))))
;; NOTE Redundant, but intended to, in the future, prevent the custom variable from starting the animation timer even if nyan mode isn't active. -- Jacek Złydach, 2020-05-26
(when nyan-animate-nyancat
(nyan-start-animation)))
((not nyan-mode)
(nyan-stop-animation) ; In case there was an animation going on.
(setcar mode-line-position nyan-old-car-mode-line-position)
(setq nyan-old-car-mode-line-position nil))))
(provide 'org-roam-nyan-mode)
;;; org-roam-nyan-mode.el ends here