/
powerline.el
550 lines (514 loc) · 19.9 KB
/
powerline.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
;;; powerline.el --- fancy statusline
;; Name: Emacs Powerline
;; Author: Unknown
;; Version: 1.0
;; Keywords: statusline
;;; Commentary:
;; This package simply provides a minor mode for fancifying the status line.
;; Modified by: Jonathan Chu
;;; Code:
(defvar powerline-color1)
(defvar powerline-color2)
(setq powerline-color1 "grey22")
(setq powerline-color2 "grey40")
(set-face-attribute 'mode-line nil
:background "OliveDrab3"
:box nil)
(set-face-attribute 'mode-line-inactive nil
:box nil)
(if (functionp 'scroll-bar-mode)
(scroll-bar-mode -1))
(defun arrow-left-xpm
(color1 color2)
"Return an XPM left arrow string representing."
(create-image
(format "/* XPM */
static char * arrow_left[] = {
\"12 18 2 1\",
\". c %s\",
\" c %s\",
\". \",
\".. \",
\"... \",
\".... \",
\"..... \",
\"...... \",
\"....... \",
\"........ \",
\"......... \",
\"......... \",
\"........ \",
\"....... \",
\"...... \",
\"..... \",
\".... \",
\"... \",
\".. \",
\". \"};"
(if color1 color1 "None")
(if color2 color2 "None"))
'xpm t :ascent 'center))
(defun arrow-right-xpm
(color1 color2)
"Return an XPM right arrow string representing."
(create-image
(format "/* XPM */
static char * arrow_right[] = {
\"12 18 2 1\",
\". c %s\",
\" c %s\",
\" .\",
\" ..\",
\" ...\",
\" ....\",
\" .....\",
\" ......\",
\" .......\",
\" ........\",
\" .........\",
\" .........\",
\" ........\",
\" .......\",
\" ......\",
\" .....\",
\" ....\",
\" ...\",
\" ..\",
\" .\"};"
(if color2 color2 "None")
(if color1 color1 "None"))
'xpm t :ascent 'center))
(defun arrow14-left-xpm
(color1 color2)
"Return an XPM left arrow string representing."
(create-image
(format "/* XPM */
static char * arrow_left[] = {
\"12 14 2 1\",
\". c %s\",
\" c %s\",
\". \",
\".. \",
\"... \",
\".... \",
\"..... \",
\"...... \",
\"....... \",
\"....... \",
\"...... \",
\"..... \",
\".... \",
\"... \",
\".. \",
\". \"};"
(if color1 color1 "None")
(if color2 color2 "None"))
'xpm t :ascent 'center))
(defun arrow14-right-xpm
(color1 color2)
"Return an XPM right arrow string representing."
(create-image
(format "/* XPM */
static char * arrow_right[] = {
\"12 14 2 1\",
\". c %s\",
\" c %s\",
\" .\",
\" ..\",
\" ...\",
\" ....\",
\" .....\",
\" ......\",
\" .......\",
\" .......\",
\" ......\",
\" .....\",
\" ....\",
\" ...\",
\" ..\",
\" .\"};"
(if color2 color2 "None")
(if color1 color1 "None"))
'xpm t :ascent 'center))
(defun curve-right-xpm
(color1 color2)
"Return an XPM right curve string representing."
(create-image
(format "/* XPM */
static char * curve_right[] = {
\"12 18 2 1\",
\". c %s\",
\" c %s\",
\" .\",
\" ...\",
\" ...\",
\" .....\",
\" .....\",
\" .....\",
\" ......\",
\" ......\",
\" ......\",
\" ......\",
\" ......\",
\" ......\",
\" .....\",
\" .....\",
\" .....\",
\" ...\",
\" ...\",
\" .\"};"
(if color2 color2 "None")
(if color1 color1 "None"))
'xpm t :ascent 'center))
(defun curve-left-xpm
(color1 color2)
"Return an XPM left curve string representing."
(create-image
(format "/* XPM */
static char * curve_left[] = {
\"12 18 2 1\",
\". c %s\",
\" c %s\",
\". \",
\"... \",
\"... \",
\"..... \",
\"..... \",
\"..... \",
\"...... \",
\"...... \",
\"...... \",
\"...... \",
\"...... \",
\"...... \",
\"..... \",
\"..... \",
\"..... \",
\"... \",
\"... \",
\". \"};"
(if color1 color1 "None")
(if color2 color2 "None"))
'xpm t :ascent 'center))
(defun make-xpm
(name color1 color2 data)
"Return an XPM image for lol data"
(create-image
(concat
(format "/* XPM */
static char * %s[] = {
\"%i %i 2 1\",
\". c %s\",
\" c %s\",
"
(downcase (replace-regexp-in-string " " "_" name))
(length (car data))
(length data)
(if color1 color1 "None")
(if color2 color2 "None"))
(let ((len (length data))
(idx 0))
(apply 'concat
(mapcar '(lambda (dl)
(setq idx (+ idx 1))
(concat
"\""
(concat
(mapcar '(lambda (d)
(if (eq d 0)
(string-to-char " ")
(string-to-char ".")))
dl))
(if (eq idx len)
"\"};"
"\",\n")))
data))))
'xpm t :ascent 'center))
(defun half-xpm
(color1 color2)
(make-xpm "half" color1 color2
(make-list 18
(append (make-list 6 0)
(make-list 6 1)))))
(defun percent-xpm
(pmax pmin we ws width color1 color2)
(let* ((fs (if (eq pmin ws)
0
(round (* 17 (/ (float ws) (float pmax))))))
(fe (if (eq pmax we)
17
(round (* 17 (/ (float we) (float pmax))))))
(o nil)
(i 0))
(while (< i 18)
(setq o (cons
(if (and (<= fs i)
(<= i fe))
(append (list 0) (make-list width 1) (list 0))
(append (list 0) (make-list width 0) (list 0)))
o))
(setq i (+ i 1)))
(make-xpm "percent" color1 color2 (reverse o))))
;; from memoize.el @ http://nullprogram.com/blog/2010/07/26/
(defun memoize (func)
"Memoize the given function. If argument is a symbol then
install the memoized function over the original function."
(typecase func
(symbol (fset func (memoize-wrap (symbol-function func))) func)
(function (memoize-wrap func))))
(defun memoize-wrap (func)
"Return the memoized version of the given function."
(let ((table-sym (gensym))
(val-sym (gensym))
(args-sym (gensym)))
(set table-sym (make-hash-table :test 'equal))
`(lambda (&rest ,args-sym)
,(concat (documentation func) "\n(memoized function)")
(let ((,val-sym (gethash ,args-sym ,table-sym)))
(if ,val-sym
,val-sym
(puthash ,args-sym (apply ,func ,args-sym) ,table-sym))))))
(memoize 'arrow-left-xpm)
(memoize 'arrow-right-xpm)
(memoize 'arrow14-left-xpm)
(memoize 'arrow14-right-xpm)
(memoize 'curve-left-xpm)
(memoize 'curve-right-xpm)
(memoize 'half-xpm)
(memoize 'percent-xpm)
(defvar powerline-minor-modes nil)
(defvar powerline-arrow-shape 'arrow)
(defun powerline-make-face
(bg &optional fg)
(if bg
(let ((cface (intern (concat "powerline-"
bg
"-"
(if fg
(format "%s" fg)
"white")))))
(make-face cface)2
(if fg
(if (eq fg 0)
(set-face-attribute cface nil
:background bg
:box nil)
(set-face-attribute cface nil
:foreground fg
:background bg
:box nil))
(set-face-attribute cface nil
:foreground "white"
:background bg
:box nil))
cface)
nil))
(defun powerline-make-left
(string color1 &optional color2 localmap)
(let ((plface (powerline-make-face color1))
(arrow (and color2 (not (string= color1 color2)))))
(concat
(if (or (not string) (string= string ""))
""
(propertize " " 'face plface))
(if string
(if localmap
(propertize string 'face plface 'mouse-face plface 'local-map localmap)
(propertize string 'face plface))
"")
(if arrow
(propertize " " 'face plface)
"")
(if arrow
(propertize " " 'display
(cond ((eq powerline-arrow-shape 'arrow)
(arrow-left-xpm color1 color2))
((eq powerline-arrow-shape 'arrow14)
(arrow14-left-xpm color1 color2))
((eq powerline-arrow-shape 'curve)
(curve-left-xpm color1 color2))
((eq powerline-arrow-shape 'half)
(half-xpm color2 color1))
(t
(arrow-left-xpm color1 color2)))
'local-map (make-mode-line-mouse-map
'mouse-1 (lambda () (interactive)
(setq powerline-arrow-shape
(cond ((eq powerline-arrow-shape 'arrow) 'arrow14)
((eq powerline-arrow-shape 'arrow14) 'curve)
((eq powerline-arrow-shape 'curve) 'half)
((eq powerline-arrow-shape 'half) 'arrow)
(t 'arrow)))
(redraw-modeline))))
""))))
(defun powerline-make-right
(string color2 &optional color1 localmap)
(let ((plface (powerline-make-face color2))
(arrow (and color1 (not (string= color1 color2)))))
(concat
(if arrow
(propertize " " 'display
(cond ((eq powerline-arrow-shape 'arrow)
(arrow-right-xpm color1 color2))
((eq powerline-arrow-shape 'arrow14)
(arrow14-right-xpm color1 color2))
((eq powerline-arrow-shape 'curve)
(curve-right-xpm color1 color2))
((eq powerline-arrow-shape 'half)
(half-xpm color2 color1))
(t
(arrow-right-xpm color1 color2)))
'local-map (make-mode-line-mouse-map
'mouse-1 (lambda () (interactive)
(setq powerline-arrow-shape
(cond ((eq powerline-arrow-shape 'arrow) 'arrow14)
((eq powerline-arrow-shape 'arrow14) 'curve)
((eq powerline-arrow-shape 'curve) 'half)
((eq powerline-arrow-shape 'half) 'arrow)
(t 'arrow)))
(redraw-modeline))))
"")
(if arrow
(propertize " " 'face plface)
"")
(if string
(if localmap
(propertize string 'face plface 'mouse-face plface 'local-map localmap)
(propertize string 'face plface))
"")
(if (or (not string) (string= string ""))
""
(propertize " " 'face plface)))))
;; get-scroll-bar-mode is not available in emacs 23.2
(if (not (functionp 'get-scroll-bar-mode))
(defun get-scroll-bar-mode () scroll-bar-mode))
(defun powerline-make-fill
(color)
;; justify right by filling with spaces to right fringe, 20 should be calculated
(let ((plface (powerline-make-face color)))
(if (eq 'right (get-scroll-bar-mode))
(propertize " " 'display '((space :align-to (- right-fringe 21)))
'face plface)
(propertize " " 'display '((space :align-to (- right-fringe 24)))
'face plface))))
(defun powerline-make-text
(string color &optional fg localmap)
(let ((plface (powerline-make-face color)))
(if string
(if localmap
(propertize string 'face plface 'mouse-face plface 'local-map localmap)
(propertize string 'face plface))
"")))
(defun powerline-make (side string color1 &optional color2 localmap)
(cond ((and (eq side 'right) color2) (powerline-make-right string color1 color2 localmap))
((and (eq side 'left) color2) (powerline-make-left string color1 color2 localmap))
((eq side 'left) (powerline-make-left string color1 color1 localmap))
((eq side 'right) (powerline-make-right string color1 color1 localmap))
(t (powerline-make-text string color1 localmap))))
(defmacro defpowerline (name string)
`(defun ,(intern (concat "powerline-" (symbol-name name)))
(side color1 &optional color2)
(powerline-make side
,string
color1 color2)))
(defun powerline-mouse (click-group click-type string)
(cond ((eq click-group 'minor)
(cond ((eq click-type 'menu)
`(lambda (event)
(interactive "@e")
(minor-mode-menu-from-indicator ,string)))
((eq click-type 'help)
`(lambda (event)
(interactive "@e")
(describe-minor-mode-from-indicator ,string)))
(t
`(lambda (event)
(interactive "@e")
nil))))
(t
`(lambda (event)
(interactive "@e")
nil))))
(defpowerline arrow "")
(defpowerline buffer-id (propertize (car (propertized-buffer-identification "%12b"))
'face (powerline-make-face color1)))
(defvar powerline-buffer-size-suffix t)
(defpowerline buffer-size (propertize
(if powerline-buffer-size-suffix
"%I"
"%i")
'local-map (make-mode-line-mouse-map
'mouse-1 (lambda () (interactive)
(setq powerline-buffer-size-suffix
(not powerline-buffer-size-suffix))
(redraw-modeline)))))
(defpowerline rmw "%*")
(defpowerline major-mode (propertize (format-mode-line mode-name)
'help-echo "Major mode\n\ mouse-1: Display major mode menu\n\ mouse-2: Show help for major mode\n\ mouse-3: Toggle minor modes"
'local-map (let ((map (make-sparse-keymap)))
(define-key map [mode-line down-mouse-1]
`(menu-item ,(purecopy "Menu Bar") ignore
:filter (lambda (_) (mouse-menu-major-mode-map))))
(define-key map [mode-line mouse-2] 'describe-mode)
(define-key map [mode-line down-mouse-3] mode-line-mode-menu)
map)))
(defpowerline minor-modes (let ((mms (split-string (format-mode-line minor-mode-alist))))
(apply 'concat
(mapcar '(lambda (mm)
(propertize (if (string= (car mms)
mm)
mm
(concat " " mm))
'help-echo "Minor mode\n mouse-1: Display minor mode menu\n mouse-2: Show help for minor mode\n mouse-3: Toggle minor modes"
'local-map (let ((map (make-sparse-keymap)))
(define-key map [mode-line down-mouse-1] (powerline-mouse 'minor 'menu mm))
(define-key map [mode-line mouse-2] (powerline-mouse 'minor 'help mm))
(define-key map [mode-line down-mouse-3] (powerline-mouse 'minor 'menu mm))
(define-key map [header-line down-mouse-3] (powerline-mouse 'minor 'menu mm))
map)))
mms))))
(defpowerline row "%4l")
(defpowerline column "%3c")
(defpowerline percent "%6p")
(defpowerline narrow (let (real-point-min real-point-max)
(save-excursion
(save-restriction
(widen)
(setq real-point-min (point-min) real-point-max (point-max))))
(when (or (/= real-point-min (point-min))
(/= real-point-max (point-max)))
(propertize "Narrow"
'help-echo "mouse-1: Remove narrowing from the current buffer"
'local-map (make-mode-line-mouse-map
'mouse-1 'mode-line-widen)))))
(defpowerline status "%s")
(defpowerline emacsclient mode-line-client)
(defpowerline vc vc-mode)
(defpowerline percent-xpm (propertize " "
'display
(let (pmax
pmin
(ws (window-start))
(we (window-end)))
(save-restriction
(widen)
(setq pmax (point-max))
(setq pmin (point-min)))
(percent-xpm pmax pmin we ws 15 color1 color2))))
(setq-default mode-line-format
(list "%e"
'(:eval (concat
(powerline-rmw 'left nil )
(powerline-buffer-id 'left nil powerline-color1 )
(powerline-major-mode 'left powerline-color1 )
(powerline-minor-modes 'left powerline-color1 )
(powerline-narrow 'left powerline-color1 powerline-color2 )
(powerline-vc 'center powerline-color2 )
(powerline-make-fill powerline-color2 )
(powerline-row 'right powerline-color1 powerline-color2 )
(powerline-make-text ":" powerline-color1 )
(powerline-column 'right powerline-color1 )
(powerline-percent 'right nil powerline-color1 )
(powerline-make-text " " nil )))))
(provide 'powerline)
;;; powerline.el ends here