-
Notifications
You must be signed in to change notification settings - Fork 67
/
nyan-mode.el
328 lines (269 loc) · 12.6 KB
/
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
;;; nyan-mode.el --- Nyan Cat shows position in current buffer in mode-line -*- lexical-binding: t; -*-
;; Nyanyanyanyanyanyanya!
;; Author: Jacek "TeMPOraL" Zlydach <[email protected]>
;; URL: https://github.com/TeMPOraL/nyan-mode/
;; Version: 1.1.4
;; Keywords: convenience, games, mouse, multimedia
;; Nyanwords: nyan, cat, lulz, scrolling, pop tart cat, build something amazing
;; Package-Requires: ((emacs "24.1"))
;; This file is not part of GNU Emacs.
;; ...yet. ;).
;; 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:
;; NEW! You can now click on the rainbow (or the empty space)
;; to scroll your buffer!
;; NEW! You can now customize the minimum window width
;; below which the nyan-mode will be disabled, so that more important
;; information can be shown in the modeline.
;; To activate, just load and put `(nyan-mode 1)' in your init file.
;; Contributions and feature requests welcome!
;; Inspired by (and in few places copied from) sml-modeline.el written by Lennart Borgman.
;; See: http://bazaar.launchpad.net/~nxhtml/nxhtml/main/annotate/head%3A/util/sml-modeline.el
;;; History:
;; 2016-04-26 - introduced click-to-scroll feature.
;; Started as a totally random idea back in August 2011.
;; The homepage at http://nyan-mode.buildsomethingamazing.com died somewhen in 2014/2015 because reasons.
;; I might get the domain back one day.
;;; Code:
(defconst nyan-directory (file-name-directory (or load-file-name buffer-file-name)))
(defconst nyan-cat-size 3)
(defconst nyan-cat-face-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-music (concat nyan-directory "mus/nyanlooped.mp3"))
(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-face-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-cat-face [
["[]*" "[]#"]
["(*^ー゚)" "( ^ー^)" "(^ー^ )" "(゚ー^*)"]
["(´ω`三 )" "( ´ω三` )" "( ´三ω` )" "( 三´ω`)"
"( 三´ω`)" "( ´三ω` )" "( ´ω三` )" "(´ω`三 )"]
["(´д`;)" "( ´д`;)" "( ;´д`)" "(;´д` )"]
["(」・ω・)」" "(/・ω・)/" "(」・ω・)」" "(/・ω・)/"
"(」・ω・)」" "(/・ω・)/" "(」・ω・)」" "\(・ω・)/"]
["(>ワ<三 )" "( >ワ三< )"
"( >三ワ< )" "( 三>ワ<)"
"( >三ワ< )" "( >ワ三< )"]])
(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))
(force-mode-line-update))
(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
(/ (- (float (point))
(float (point-min)))
(float (point-max)))))
(- nyan-bar-length nyan-cat-size))
100)))
(defun nyan-catface ()
(aref nyan-cat-face 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."
(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))))
;;; Music handling.
;; mplayer needs to be installed for that
(defvar nyan-music-process nil)
(defun nyan-start-music ()
(interactive)
(unless nyan-music-process
(setq nyan-music-process (start-process-shell-command "nyan-music"
"nyan-music"
(concat "mplayer " nyan-music " -loop 0")))))
(defun nyan-stop-music ()
(interactive)
(when nyan-music-process
(delete-process nyan-music-process)
(setq nyan-music-process nil)))
;;;###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 'nyan-mode)
;;; nyan-mode.el ends here