forked from TeMPOraL/nyan-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnyan-mode.el
184 lines (158 loc) · 7.02 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
;;; nyan-mode.el --- Nyan Cat shows position in current buffer in mode-line.
;;;
;;; Nyanyanyanyanyanyanya!
;;;
;;; Author: Jacek "TeMPOraL" Zlydach <temporal.pl@gmail.com>
;;; URL: http://nyan-mode.buildsomethingamazing.com
;;; Version: 0.1
;;; Keywords: nyan, cat, lulz, pop tart cat, build something amazing
;;;
;;; 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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; LICENSE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Some TODOs
;;; * Investigate why wavy rainbow didn't work on Antoszka's computer.
;;; * Refactor-out :set lambdas in customs if possible.
;;; * MAYBE add something to protect users from going to 0 with nyanbar width?
;;; * Add credits for used images.
(defgroup nyan nil
"Customization group for `nyan-mode'."
:group 'frames)
(defun nyan-refresh ()
"Refresh after option changes if loaded."
(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."
:set (lambda (sym val)
(set-default sym val)
(nyan-refresh))
:group 'nyan)
(defvar nyan-animation-timer nil)
(defun nyan-start-animation ()
(interactive)
(when (not nyan-animate-nyancat)
(setq nyan-animation-timer (run-at-time "1 sec"
nyan-animation-frame-interval
'nyan-swich-anim-frame))
(setq nyan-animate-nyancat t)))
(defun nyan-stop-animation ()
(interactive)
(when nyan-animate-nyancat
(cancel-timer nyan-animation-timer)
(setq nyan-animation-timer nil)
(setq nyan-animate-nyancat nil)))
;;; 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."
: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))
:set (lambda (sym val)
(set-default sym val)
(if val
(nyan-start-animation)
(nyan-stop-animation))
(nyan-refresh))
:group 'nyan)
(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"))
;;; Load images of Nyan Cat an it's rainbow.
(defvar nyan-cat-image (create-image +nyan-cat-image+ 'xpm nil :ascent 'center))
(defvar nyan-animation-frames (mapcar (lambda (id)
(create-image (concat +nyan-directory+ (format "img/nyan-frame-%d.xpm" id))
'xpm nil :ascent 'center))
'(1 2 3 4 5 6)))
(defvar nyan-current-frame 0)
(defun nyan-swich-anim-frame ()
(setq nyan-current-frame (% (+ 1 nyan-current-frame) 6))
(redraw-modeline))
(defun nyan-get-anim-frame ()
(if nyan-animate-nyancat
(nth nyan-current-frame nyan-animation-frames)
nyan-cat-image))
(defun nyan-create ()
(let* ((percentage (round (* 100
(/ (- (float (point))
(float (point-min)))
(float (point-max))))))
(rainbows (round (/ (* percentage (- nyan-bar-length +nyan-cat-size+))
100)))
(outerspaces (- nyan-bar-length rainbows +nyan-cat-size+))
(rainbow-string "")
(nyancat-string (propertize "[]*"
'display (nyan-get-anim-frame)))
(outerspace-string ""))
(dotimes (number rainbows)
(setq rainbow-string (concat rainbow-string
(propertize "|"
'display (create-image +nyan-rainbow-image+ 'xpm nil :ascent (if (and nyan-wavy-trail
(zerop (% number 2)))
80
'center))))))
(dotimes (number outerspaces)
(setq outerspace-string (concat outerspace-string
(propertize "-"
'display (create-image +nyan-outerspace-image+ 'xpm nil :ascent 'center)))))
;; Compute Nyan Cat string.
(concat rainbow-string
nyancat-string
outerspace-string)))
(defvar nyan-old-car-mode-line-position 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
(if nyan-mode
(progn
(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)))))
(setcar mode-line-position nyan-old-car-mode-line-position)))
(provide 'nyan-mode)
;;; nyan-mode.el ends here