-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemud-prompt.el
89 lines (73 loc) · 3.08 KB
/
emud-prompt.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
(require 'emud)
(defvar mud-local-prompt-components
'( (BASE . "") ))
(defvar mud-local-prompt-suffix
"> ")
(make-variable-buffer-local 'mud-local-prompt-components)
(make-variable-buffer-local 'mud-local-prompt-suffix)
(defun emud-prompt-register-component (key-symbol)
(if (assq key-symbol mud-local-prompt-components)
(message "%s is already a registered prompt component" key-symbol)
(setq mud-local-prompt-components
(cons (cons key-symbol "")
mud-local-prompt-components))))
(defun emud-prompt-unregister-component (key-symbol)
(unless (assq key-symbol mud-local-prompt-components)
(error "%s is not a registered prompt component" key-symbol))
(when (eq key-symbol 'BASE)
(error "You cannot unregister the BASE path component"))
(setq mud-local-prompt-components
(assq-delete-all key-symbol mud-local-prompt-components))
(emud-prompt-refresh))
(defun emud-prompt-set-component (key text)
(let (( found-pair (assq key mud-local-prompt-components) ))
(if found-pair
(setcdr found-pair text)
(error "There is no registered prompt component named %s" key)))
(emud-prompt-refresh))
(defadvice mud-set-prompt (before emud-fancy-prompt-set)
(emud-prompt-set-component 'BASE (ad-get-arg 0))
(ad-set-arg 0 (emud-prompt-generate)))
(ad-activate 'mud-set-prompt)
(defun emud-prompt-generate ()
(concat (mapconcat 'identity
(delete "" (mapcar (lambda (comp) (cdr comp))
(reverse mud-local-prompt-components)))
" ")
mud-local-prompt-suffix))
(defun emud-prompt-refresh ()
(save-match-data
(ad-disable-advice 'mud-set-prompt 'before 'emud-fancy-prompt-set)
(ad-activate 'mud-set-prompt)
(mud-set-prompt (emud-prompt-generate))
(ad-enable-advice 'mud-set-prompt 'before 'emud-fancy-prompt-set)
(ad-activate 'mud-set-prompt)))
(defmacro %PROMPT-REGISTER (&rest components)
(let ( result )
(dolist (component components)
(push `(emud-prompt-register-component ',component) result))
`(progn
,@result)))
(defmacro %PROMPT-SET (component text)
`(emud-prompt-set-component ',component ,text))
(defun emud-prompt-color-gauge (number-value gauge-meters)
(let ( range-max color-name )
(while gauge-meters
(setq range-max (cadr gauge-meters)
color-name (car gauge-meters))
(if (and range-max (<= number-value range-max))
(setq gauge-meters nil)
(setq gauge-meters (cddr gauge-meters))))
(emud-color color-name (number-to-string number-value))))
(defmacro emud-prompt-color-number (number-value number-max &optional how)
;; TODO: logarithmic gauges
(unless how (setq how 'linear))
(let ( one-third range-list )
(setq one-third (/ number-max 3)
range-list (list "red" one-third
"yellow" (* 2 one-third)
"green"))
`(emud-prompt-color-gauge ,number-value ',range-list)))
(defmacro %COLOR-GAUGE (number-value number-max)
`(emud-prompt-color-number ,number-value ,number-max))
(provide 'emud-prompt)