-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbongocat.el
89 lines (72 loc) · 2.87 KB
/
bongocat.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
;;; Code:
(defconst bongocat-directory (file-name-directory (or load-file-name buffer-file-name)))
(defconst bongocat-default-smash-delay 0.075)
(defvar bongocat-frames nil)
(defvar bongocat-current-state "up")
(defun bongocat-load-frame (state)
"Create the frame image corresponding to the specified STATE (down/up)."
(create-image (concat bongocat-directory
(format "img/default/bongocat-%s.xpm" state)) 'xpm nil :ascent 'center))
(defun bongocat-set-state (state)
"Set the STATE of Bongo cat's paws."
(if (eq state nil)
(setq bongocat-current-state "up")
(setq bongocat-current-state state))
(bongocat-refresh-frame)
)
(defun bongocat-refresh-frame ()
"Re-render the current frame in the modeline."
(when (and (featurep 'bongocat)
(bound-and-true-p bongocat-mode))
(force-mode-line-update)
)
)
(defun bongocat-load-frames ()
"Load images into memory."
(when (image-type-available-p 'xpm)
;; This loads the images of "up" and "down" into the list "bongocat-frames".
;; Thus the image indices in the list are as follows: Down = 0, Up = 1
(setq bongocat-frames (mapcar (lambda (state)
(bongocat-load-frame state))
'("down" "up")))
)
)
(defun bongocat-get-current-frame ()
"Get the frame to be displayed based on current state."
(cond ((string= bongocat-current-state "down") (nth 0 bongocat-frames))
((string= bongocat-current-state "up") (nth 1 bongocat-frames))
(t (nth 1 bongocat-frames)) ;; return the UP image by default
)
)
(defun bongocat-initialize ()
"Create modeline string containing Bongocat!"
(let ((modeline-string ""))
(setq modeline-string (propertize "-" 'display (bongocat-get-current-frame)))
)
)
(defun bongocat-smash (&optional delay)
"Make Bongocat smash the modeline with optional DELAY in seconds. Otherwise default delay will be used."
(if (string= bongocat-current-state "down")
(bongocat-set-state "up"))
(bongocat-set-state "down")
(run-at-time (format "%s seconds" bongocat-default-smash-delay) nil #'bongocat-set-state "up")
)
(defvar bongocat-initial-modeline-cdr nil)
(define-minor-mode bongocat-mode
"Bongocat minor mode"
:global t
(if bongocat-mode
(progn
(setq bongocat-current-state "up")
(bongocat-load-frames)
(unless bongocat-initial-modeline-cdr
(setq bongocat-initial-modeline-cdr (cdr mode-line-position)))
(setcdr mode-line-position (cons '(:eval (list (bongocat-initialize)))
(cdr bongocat-initial-modeline-cdr)))
(add-hook 'pre-command-hook #'bongocat-smash))
(setcdr mode-line-position bongocat-initial-modeline-cdr)
(remove-hook 'pre-command-hook #'bongocat-smash)
)
)
(provide 'bongocat)
;;; bongocat.el ends here