-
Notifications
You must be signed in to change notification settings - Fork 3
/
ergoemacs-status.el
1734 lines (1570 loc) · 66.4 KB
/
ergoemacs-status.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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; ergoemacs-status.el --- Adaptive Status Bar / Mode Line -*- lexical-binding: t -*-
;;
;; Filename: ergoemacs-status.el
;; Description: Adaptive Status Bar / Mode Line
;; Author: Matthew Fidler
;; Maintainer: Matthew Fidler
;; Created: Fri Mar 4 14:13:50 2016 (-0600)
;; Version: 0.1
;; Package-Requires: ((powerline "2.3") (mode-icons "0.1.0"))
;;
;;; Commentary:
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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 of the License, 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
(require 'powerline nil t)
(require 'mode-icons nil t)
(eval-when-compile
(require 'cl))
(declare-function ergoemacs-next-emacs-buffer "ergoemacs-lib")
(declare-function ergoemacs-next-user-buffer "ergoemacs-lib")
(declare-function ergoemacs-previous-emacs-buffer "ergoemacs-lib")
(declare-function ergoemacs-previous-user-buffer "ergoemacs-lib")
(defvar ergoemacs-menu--get-major-modes)
(declare-function mode-icons-get-mode-icon "mode-icons")
(declare-function mode-icons-mode "mode-icons")
(declare-function mode-icons-propertize-mode "mode-icons")
(declare-function mode-icons--read-only-status "mode-icons")
(declare-function mode-icons--modified-status "mode-icons")
(declare-function flycheck-count-errors "flycheck")
(declare-function powerline-current-separator "powerline")
(declare-function powerline-selected-window-active "powerline")
(defmacro ergoemacs-status-save-buffer-state (&rest body)
"Eval BODY without modifiying the buffer state.
This then restore the buffer state under the assumption that no significant
modification has been made in BODY. A change is considered
significant if it affects the buffer text in any way that isn't
completely restored again. Changes in text properties like `face' or
`syntax-table' are considered insignificant. This macro allows text
properties to be changed, even in a read-only buffer.
This macro should be placed around all calculations which set
\"insignificant\" text properties in a buffer, even when the buffer is
known to be writeable. That way, these text properties remain set
even if the user undoes the command which set them.
This macro should ALWAYS be placed around \"temporary\" internal buffer
changes \(like adding a newline to calculate a text-property then
deleting it again\), so that the user never sees them on his
`buffer-undo-list'.
However, any user-visible changes to the buffer \(like auto-newlines\)
must not be within a `ergoemacs-status-save-buffer-state', since the user then
wouldn't be able to undo them.
The return value is the value of the last form in BODY.
This was stole/modified from `c-save-buffer-state'"
`(let* ((modified (buffer-modified-p)) (buffer-undo-list t)
(inhibit-read-only t) (inhibit-point-motion-hooks t)
before-change-functions after-change-functions
deactivate-mark
buffer-file-name buffer-file-truename ; Prevent primitives checking
; for file modification
)
(unwind-protect
(progn ,@body)
(and (not modified)
(buffer-modified-p)
(set-buffer-modified-p nil)))))
(defvar ergoemacs-status-file (locate-user-emacs-file ".ergoemacs-status.el")
"The `ergoemacs-status-mode' preferences file.")
(defcustom ergoemacs-status-popup-languages t
"Allow Swapping of `major-modes' when clicking the `mode-name'."
:type 'boolean
:group 'ergoemacs-status)
(defcustom ergoemacs-status-change-buffer 'ergoemacs-status-group-function
"Method of changing buffer."
:type '(choice
(function :tag "Function to group buffers.")
(const :tag "Switch to next/previous user/emacs buffer." 'ergoemacs)
(const :tag "Use emacs default method." nil))
:group 'ergoemacs-status)
(defface ergoemacs-status-selected-element
'((t :background "#e6c200" :foreground "#0024e6" :inherit mode-line))
"Face for the modeline in buffers with Flycheck errors."
:group 'ergoemacs-status)
(defvar ergoemacs-status--major-mode-menu-map nil)
(defun ergoemacs-status--major-mode-menu-map (&optional _)
"Popup major modes and information about current mode."
(interactive)
(ergoemacs-status-save-buffer-state
(or ergoemacs-status--major-mode-menu-map
(set (make-local-variable 'ergoemacs-status--major-mode-menu-map)
;; This require `ergoemacs-mode'.
(let ((map (and ergoemacs-status-popup-languages
;; Mode in menu
(boundp 'ergoemacs-menu--get-major-modes)
(memq major-mode ergoemacs-menu--get-major-modes)
(key-binding [menu-bar languages])))
mmap)
(if (not map)
(mouse-menu-major-mode-map)
(setq mmap (mouse-menu-major-mode-map))
(define-key map [major-mode-sep-b] '(menu-item "---"))
(define-key map [major-mode] (cons (nth 1 mmap) mmap))
map))))))
(defun ergoemacs-status-buffer-list ()
"List of buffers shown in popup menu."
(delq nil
(mapcar #'(lambda (b)
(cond
;; Always include the current buffer.
((eq (current-buffer) b) b)
((buffer-file-name b) b)
((char-equal ?\ (aref (buffer-name b) 0)) nil)
((buffer-live-p b) b)))
(buffer-list))))
(defun ergoemacs-status-group-function (&optional buffer)
"What group does the current BUFFER belong to?"
(if (char-equal ?\* (aref (buffer-name buffer) 0))
"Emacs Buffer"
"User Buffer"))
(defun ergoemacs-status-menu (&optional buffer)
"Create the BUFFER name menu."
(let* ((cb (or buffer (current-buffer)))
(group (with-current-buffer cb
(if (functionp ergoemacs-status-change-buffer)
(funcall ergoemacs-status-change-buffer)
'("Common"))))
(groups '())
(buf-list (sort
(mapcar
;; for each buffer, create list: buffer, buffer name, groups-list
;; sort on buffer name; store to bl (buffer list)
(lambda (b)
(let (tmp0 tmp1 tmp2)
(with-current-buffer b
(setq tmp0 b
tmp1 (buffer-name b)
tmp2 (if (functionp ergoemacs-status-change-buffer)
(funcall ergoemacs-status-change-buffer)
"Common"))
(unless (or (string= group tmp2) (assoc tmp2 groups))
(push (cons tmp2 (intern tmp2)) groups))
(list tmp0 tmp1 tmp2 (intern tmp1)))))
(ergoemacs-status-buffer-list))
(lambda (e1 e2)
(or (and (string= (nth 2 e2) (nth 2 e2))
(not (string-lessp (nth 1 e1) (nth 1 e2))))
(not (string-lessp (nth 2 e1) (nth 2 e2)))))))
menu menu2 tmp)
(dolist (item buf-list)
(if (string= (nth 2 item) group)
(unless (eq cb (nth 0 item))
(push `(,(nth 3 item) menu-item ,(nth 1 item) (lambda() (interactive) (funcall menu-bar-select-buffer-function ,(nth 0 item)))) menu))
(if (setq tmp (assoc (nth 2 item) menu2))
(push `(,(nth 3 item) menu-item ,(nth 1 item) (lambda() (interactive) (funcall menu-bar-select-buffer-function ,(nth 0 item))))
(cdr tmp))
(push (list (nth 2 item) `(,(nth 3 item) menu-item ,(nth 1 item) (lambda() (interactive) (funcall menu-bar-select-buffer-function ,(nth 0 item))))) menu2))))
(setq menu `(keymap ,(if (or menu (> (length menu2) 1))
group
(car (car menu2)))
,@(if (or menu (> (length menu2) 1))
(mapcar
(lambda(elt)
`(,(intern (car elt)) menu-item ,(car elt) (keymap ,@(cdr elt))))
menu2)
(cdr (car menu2)))
,(when (and menu (>= (length menu2) 1))
'(sepb menu-item "--"))
,@menu))
menu))
(defun ergoemacs-status-mouse-1-buffer (event)
"Next ergoemacs buffer.
EVENT is where the mouse-click occured and is used to figure out
what window is associated with the mode-line click."
(interactive "e")
(with-selected-window (posn-window (event-start event))
(let ((emacs-buffer-p (string-match-p "^[*]" (buffer-name))))
(cond
((functionp ergoemacs-status-change-buffer)
(popup-menu (ergoemacs-status-menu)))
((and ergoemacs-status-change-buffer emacs-buffer-p (fboundp #'ergoemacs-next-emacs-buffer))
(ergoemacs-next-emacs-buffer))
((and ergoemacs-status-change-buffer (fboundp #'ergoemacs-next-user-buffer))
(ergoemacs-next-user-buffer))
(t
(next-buffer))))))
(defun ergoemacs-status-mouse-3-buffer (event)
"Prevous ergoemacs buffer.
EVENT is where the mouse clicked and is used to figure out which
buffer is selected with mode-line click."
(interactive "e")
(with-selected-window (posn-window (event-start event))
(let ((emacs-buffer-p (string-match-p "^[*]" (buffer-name))))
(cond
((functionp ergoemacs-status-change-buffer)
(popup-menu (ergoemacs-status-menu)))
((and ergoemacs-status-change-buffer emacs-buffer-p (fboundp #'ergoemacs-previous-emacs-buffer))
(ergoemacs-previous-emacs-buffer))
((and ergoemacs-status-change-buffer (fboundp #'ergoemacs-previous-user-buffer))
(ergoemacs-previous-user-buffer))
(t
(previous-buffer))))))
(defcustom ergoemacs-status-use-vc t
"Include vc in mode-line."
:type 'boolean
:group 'ergoemacs-status)
(defun ergoemacs-status--property-substrings (str prop)
"Return a list of substrings of STR when PROP change."
;; Taken from powerline by Donald Ephraim Curtis, Jason Milkins and
;; Nicolas Rougier
(let ((beg 0) (end 0)
(len (length str))
(out))
(while (< end (length str))
(setq end (or (next-single-property-change beg prop str) len))
(setq out (append out (list (substring str beg (setq beg end))))))
out))
(defun ergoemacs-status--ensure-list (item)
"Ensure that ITEM is a list."
(or (and (listp item) item) (list item)))
(defun ergoemacs-status--add-text-property (str prop val)
"For STR add PROP property set to VAL value."
;; Taken from powerline by Donald Ephraim Curtis, Jason Milkins and
;; Nicolas Rougier
;; Changed so that prop is not just 'face
;; Also changed to not force list, or add a nil to the list
(mapconcat
(lambda (mm)
(let ((cur (get-text-property 0 prop mm)))
(if (not cur)
(propertize mm prop val)
(propertize mm prop (append (ergoemacs-status--ensure-list cur) (list val))))))
(ergoemacs-status--property-substrings str prop)
""))
(defun ergoemacs-status--if--1 (lst-or-string)
"Render LST-OR-STRING conditionally.
If this list contains functions, and they return nil, don't
render anything. The last element should be a string, list or
symbol to render in the mode-line."
(cond
((consp lst-or-string)
(catch 'found-it
(dolist (elt lst-or-string)
(cond
((functionp elt)
(let ((tmp (funcall elt)))
(if (or (and tmp (stringp tmp)) (listp tmp))
(throw 'found-it tmp)
;; Otherwise, assume its a boolean.
;; If didn't
(when (and (booleanp tmp)
(not tmp))
(throw 'found-it "")))))
((and elt (stringp elt))
(throw 'found-it elt))
((and elt (consp elt))
(throw 'found-it elt))
((and (symbolp elt) (boundp elt) (or (consp (symbol-value elt)) (stringp (symbol-value elt))))
(throw 'found-it (symbol-value elt)))))
""))
((and lst-or-string (stringp lst-or-string))
lst-or-string)
(t "")))
(defun ergoemacs-status--if (str &optional face)
"Render STR as mode-line data using FACE."
(let* ((rendered-str (format-mode-line (ergoemacs-status--if--1 str))))
(if face
(ergoemacs-status--add-text-property rendered-str 'face face)
rendered-str)))
(defun ergoemacs-status--set-buffer-file-coding-system (event)
"Set buffer file-coding.
EVENT tells what window to set the codings system."
(interactive "e")
(with-selected-window (posn-window (event-start event))
(call-interactively #'set-buffer-file-coding-system)))
(defun ergoemacs-status--encoding ()
"Encoding mode-line."
(propertize (format "%s" (coding-system-type buffer-file-coding-system))
'mouse-face 'mode-line-highlight
'help-echo (format "mouse-1: Change buffer coding system\n%s"
(coding-system-doc-string buffer-file-coding-system))
'local-map '(keymap
(mode-line keymap
(mouse-1 . ergoemacs-status--set-buffer-file-coding-system)))))
(defvar ergoemacs-status-sep-swap
'(alternate arrow arrow-fade bar box brace butt chamfer contour curve rounded roundstub wave zigzag)
"List of separators to swap.")
(defvar powerline-default-separator)
(defun ergoemacs-status-sep-swap ()
"Swap separators used in powerline."
(interactive)
(let ((sep (memq powerline-default-separator ergoemacs-status-sep-swap)))
(when sep
(setq sep (cdr sep))
(if (= (length sep) 0)
(setq powerline-default-separator (car ergoemacs-status-sep-swap))
(setq powerline-default-separator (car sep))))
(force-mode-line-update )))
(defvar ergoemacs-status--sep-map
(let ((map (make-sparse-keymap)))
(define-key map [mode-line mouse-3] #'ergoemacs-status-right-click)
(define-key map [mode-line down-mouse-1] #'ergoemacs-status-sep-swap)
map)
"Keymap for separators.")
(defvar powerline-default-separator-dir)
(defvar ergoemacs-status--sep 'left
"Direction if current separator.
Used with `ergoemacs-status--sep'.")
(defvar ergoemacs-status--swap '(alternate bar box chamfer contour rounded wave zigzag)
"Separators that swap directions with every separator used.")
(defun ergoemacs-status--sep (&rest args)
"Separator between elements.
The additional ARGS are the fonts applied. This uses `powerline' functions."
(let* ((dir ergoemacs-status--sep)
(cs (powerline-current-separator))
(separator (and (fboundp #'powerline-current-separator)
(intern (format "powerline-%s-%s" cs
(or (and (eq dir 'left)
(car powerline-default-separator-dir))
(cdr powerline-default-separator-dir))))))
(args (mapcar
(lambda(face)
(let* ((f (or (and (symbolp face) face)
(and (consp face) (symbolp (car face)) (car face))))
(fa (assoc f face-remapping-alist)))
(if fa
(car (cdr fa))
f)))
args)))
(when (fboundp separator)
(let ((img (apply separator args)))
(when (and (listp img) (eq 'image (car img)))
(when (memq cs ergoemacs-status--swap)
(setq ergoemacs-status--sep
(or (and (eq 'left ergoemacs-status--sep) 'right) 'left)))
(propertize " " 'display img
'face (plist-get (cdr img) :face)
'mouse-face 'mode-line-highlight
'local-map ergoemacs-status--sep-map
'help-echo "Separator\nmouse-1: Swap separator\nmouse-3: mode-line properties."))))))
(defvar ergoemacs-status--lhs nil
"Internal variable to render left handed side of mode-line.")
(defvar ergoemacs-status--center nil
"Internal variable to render center of mode-line.")
(defvar ergoemacs-status--rhs nil
"Internal variable to render right handed side of mode-line.")
(defvar ergoemacs-status--lhs nil)
(defvar ergoemacs-status--center nil)
(defvar ergoemacs-status--rhs nil)
(defvar ergoemacs-status--automatic-hidden-minor-modes nil
"List of minor modes hidden due to either space or adaptive hiding of minor-modes.")
;; Adapted from powerline.
(defvar ergoemacs-status--suppressed-minor-modes '(isearch-mode)
"List of suppressed minor modes.")
(defun ergoemacs-minor-mode-menu-from-indicator (indicator &optional dont-popup)
"Show menu for minor mode specified by INDICATOR.
Interactively, INDICATOR is read using completion.
If there is no menu defined for the minor mode, then create one with
items `Turn Off', `Hide' and `Help'.
When DONT-POPUP is non-nil, return the menu without actually popping up the menu."
(interactive
(list (completing-read
"Minor mode indicator: "
(describe-minor-mode-completion-table-for-indicator))))
(let* ((minor-mode (or (and (stringp indicator) (lookup-minor-mode-from-indicator indicator))
(and (symbolp indicator) indicator)))
(mm-fun (or (get minor-mode :minor-mode-function) minor-mode)))
(unless minor-mode (error "Cannot find minor mode for `%s'" indicator))
(let* ((map (cdr-safe (assq minor-mode minor-mode-map-alist)))
(menu (and (keymapp map) (lookup-key map [menu-bar])))
(hidden (memq minor-mode ergoemacs-status--automatic-hidden-minor-modes)))
(setq menu
(if menu
(if hidden
(mouse-menu-non-singleton menu)
`(,@(mouse-menu-non-singleton menu)
(sep-minor-mode-ind menu-item "--")
(hide menu-item ,(if dont-popup
"Show this minor-mode"
"Hide this minor-mode")
(lambda () (interactive)
(ergoemacs-minor-mode-hide ',mm-fun ,dont-popup)))))
`(keymap
,indicator
(turn-off menu-item "Turn Off minor mode" ,mm-fun)
,(if hidden nil
`(hide menu-item ,(if dont-popup
"Show this minor-mode"
"Hide this minor-mode")
(lambda () (interactive)
(ergoemacs-minor-mode-hide ',mm-fun ,dont-popup))))
(help menu-item "Help for minor mode"
(lambda () (interactive)
(describe-function ',mm-fun))))))
(if dont-popup menu
(popup-menu menu)))))
(defun ergoemacs-status--minor-mode-mouse (click-group click-type string)
"Return mouse handler for CLICK-GROUP given CLICK-TYPE and STRING."
;; Taken from Powerline
(cond ((eq click-group 'minor)
(cond ((eq click-type 'menu)
`(lambda (event)
(interactive "@e")
(ergoemacs-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))))
(defvar ergoemacs-status--transient-hidden-minor-modes '()
"List of temorarily hidden modes that are not saved between sessions.")
(defvar ergoemacs-status--hidden-minor-modes '()
"List of tempoarily hidden modes.")
(defcustom ergoemacs-status-hidden-indicators
'("FlyC-")
"Minor modes that will be hidden based on indicator status."
:type '(repeat
(string :tag "Minor mode indicator"))
:group 'ergoemacs-status)
(defcustom ergoemacs-status-hidden-regexp "\\(Ergo.*\\[.*\\]\\)"
"Regular Expression of adaptive hidden indicators."
:type '(choice
(regexp :tag "Regular Expression")
(const :tag "No additional mapping."))
:group 'ergoemacs-status)
(defvar ergoemacs-status--regexp-hidden nil
"Regular Expression of when minor mode are hidden.")
(defun ergoemacs-status-save-file--sym (sym)
"Print SYM Emacs Lisp value in current buffer."
(let ((print-level nil)
(print-length nil))
(insert "(setq " (symbol-name sym) " '")
(prin1 (symbol-value sym) (current-buffer))
(goto-char (point-max))
(insert ")\n")))
(defvar ergoemacs-status--suppressed-elements nil
"List of suppressed `ergoemacs-status' elements.")
(defvar ergoemacs-status-current nil
"Current layout of mode-line.")
(defvar ergoemacs-status-save-symbols
'(ergoemacs-status--hidden-minor-modes
ergoemacs-status--suppressed-minor-modes
ergoemacs-status-current
ergoemacs-status--suppressed-elements
ergoemacs-status-elements-popup-save
powerline-default-separator
ergoemacs-status--minor-modes-separator)
"List of symbols to save in `ergoemacs-status-file'.")
(defun ergoemacs-status-save-file ()
"Save preferences to `ergoemacs-status-file'."
(ergoemacs-status-save-buffer-state
(with-temp-file ergoemacs-status-file
(insert ";; -*- coding: utf-8-emacs -*-\n"
";; This file is automatically generated by the `ergoemacs-status-mode'.\n")
(dolist (sym ergoemacs-status-save-symbols)
(ergoemacs-status-save-file--sym sym))
(insert "(add-hook 'emacs-startup-hook #'ergoemacs-status-elements-popup-restore)\n(add-hook 'emacs-startup-hook #'ergoemacs-status-current-update)\n;;; end of file\n"))))
(defun ergoemacs-minor-mode-hide (minor-mode &optional show transient)
"Hide a minor-mode based on mode indicator MINOR-MODE.
When SHOW is non-nil, show instead of hide the MINOR-MODE.
When TRANSIENT is non-nil, hide this minor mode
tranisently (doesn't save between sessions).
This function also saves your prefrences to
`ergoemacs-status-file' by calling the function
`ergoemacs-status-save-file'."
(let ((present-p (memq minor-mode (or (and transient ergoemacs-status--transient-hidden-minor-modes)
ergoemacs-status--hidden-minor-modes)))
(refresh-p t))
(cond
((and show present-p transient)
(setq ergoemacs-status--transient-hidden-minor-modes (delq minor-mode ergoemacs-status--transient-hidden-minor-modes)))
((and show present-p)
(setq ergoemacs-status--hidden-minor-modes (delq minor-mode ergoemacs-status--hidden-minor-modes)))
((and transient (not present-p))
(push minor-mode ergoemacs-status--transient-hidden-minor-modes))
((not present-p)
(push minor-mode ergoemacs-status--hidden-minor-modes))
(t
(setq refresh-p nil)))
(when refresh-p
(unless transient
(ergoemacs-status-save-file))
(force-mode-line-update))))
(defun ergoemacs-minor-mode-hidden-menu (&optional _event)
"Display a list of the hidden minor modes.
Currently this ignores the _EVENT data."
(interactive "@e")
(popup-menu
`(keymap
,@(mapcar
(lambda(m)
`(,m menu-item ,(format "%s" m) ,(ergoemacs-minor-mode-menu-from-indicator m t)))
(let (ret)
(dolist (elt (append ergoemacs-status--hidden-minor-modes
ergoemacs-status--transient-hidden-minor-modes
ergoemacs-status--automatic-hidden-minor-modes))
(when (and (boundp elt) (symbol-value elt))
(push elt ret)))
ret))
"Hidden Minor Modes")))
(defun ergoemacs-minor-mode-alist ()
"Get a list of the minor-modes."
(let (ret)
(dolist (a (reverse minor-mode-alist))
(unless (memq (car a) (append ergoemacs-status--hidden-minor-modes
ergoemacs-status--transient-hidden-minor-modes
ergoemacs-status--suppressed-minor-modes))
(push a ret)))
ret))
(defvar ergoemacs-status--minor-modes-p t
"Determine if `ergoemacs-status--minor-modes' generates space.")
(defvar ergoemacs-status--minor-modes-available nil)
(defun ergoemacs-status--minor-modes-available (mode-line face1 &optional reduce)
"Calculate the space available for minor-modes.
MODE-LINE is the mode-line face. FACE1 is the alternative face.
passed to `ergoemacs-status--eval-lhs',
`ergoemacs-status--eval-rhs' and
`ergoemacs-status--eval-center'.
REDUCE is the current reduction level being calculated."
(let (lhs rhs center)
(setq ergoemacs-status--minor-modes-p nil)
(unwind-protect
(setq lhs (ergoemacs-status--eval-lhs mode-line face1 reduce)
rhs (ergoemacs-status--eval-rhs mode-line face1 reduce)
center (ergoemacs-status--eval-center mode-line face1 reduce))
(setq ergoemacs-status--minor-modes-p t
ergoemacs-status--minor-modes-available (- (ergoemacs-status--eval-width)
(+ (ergoemacs-status--eval-width lhs)
(ergoemacs-status--eval-width rhs)
(ergoemacs-status--eval-width center)))))))
(defvar ergoemacs-status--minor-modes-separator " "
"Separator for minor modes.")
(defun ergoemacs-status--minor-modes ()
"Get minor modes."
(let* ((width 0)
(ret ""))
(unless ergoemacs-status--regexp-hidden
(setq ergoemacs-status--regexp-hidden (concat "\\` *" (regexp-opt ergoemacs-status-hidden-indicators 't)
(or (and ergoemacs-status-hidden-regexp "\\|") "")
(or ergoemacs-status-hidden-regexp "")
"\\'")))
(when ergoemacs-status--minor-modes-p
(setq ergoemacs-status--automatic-hidden-minor-modes nil
ret (replace-regexp-in-string
" +$" ""
(mapconcat (lambda (mm)
(if (or (not (numberp ergoemacs-status--minor-modes-available))
(< width ergoemacs-status--minor-modes-available))
(let ((cur (propertize mm
'mouse-face 'mode-line-highlight
'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]
(ergoemacs-status--minor-mode-mouse 'minor 'menu mm))
(define-key map
[mode-line mouse-2]
(ergoemacs-status--minor-mode-mouse 'minor 'help mm))
(define-key map
[mode-line down-mouse-3]
(ergoemacs-status--minor-mode-mouse 'minor 'menu mm))
(define-key map
[header-line down-mouse-3]
(ergoemacs-status--minor-mode-mouse 'minor 'menu mm))
map))))
;; (message "`%s';%s" cur (string-match-p ergoemacs-status--regexp-hidden cur))
(if (string-match-p ergoemacs-status--regexp-hidden cur)
(progn
(push (lookup-minor-mode-from-indicator mm) ergoemacs-status--automatic-hidden-minor-modes)
"")
(if (or (not (numberp ergoemacs-status--minor-modes-available))
(< width ergoemacs-status--minor-modes-available))
cur
(push (lookup-minor-mode-from-indicator mm) ergoemacs-status--automatic-hidden-minor-modes)
"")))
(push (lookup-minor-mode-from-indicator mm) ergoemacs-status--automatic-hidden-minor-modes)
""))
(split-string (format-mode-line (ergoemacs-minor-mode-alist)))
ergoemacs-status--minor-modes-separator))))
(when (or (not ergoemacs-status--minor-modes-p)
ergoemacs-status--automatic-hidden-minor-modes
(catch 'found
(dolist (elt ergoemacs-status--hidden-minor-modes)
(when (and (boundp elt) (symbol-value elt))
(throw 'found t)))
nil))
(setq ret (concat ret ergoemacs-status--minor-modes-separator
(propertize (if (and (fboundp #'mode-icons-propertize-mode))
(mode-icons-propertize-mode "+" (list "+" #xf151 'FontAwesome))
"+")
'mouse-face 'mode-line-highlight
'help-echo "Hidden Minor Modes\nmouse-1: Display hidden minor modes"
'local-map (let ((map (make-sparse-keymap)))
(define-key map [mode-line down-mouse-1] 'ergoemacs-minor-mode-hidden-menu)
(define-key map [mode-line down-mouse-3] 'ergoemacs-minor-mode-hidden-menu)
map)))
ret (replace-regexp-in-string (format "%s%s+"
(regexp-quote ergoemacs-status--minor-modes-separator)
(regexp-quote ergoemacs-status--minor-modes-separator))
ergoemacs-status--minor-modes-separator ret)))
ret))
(defvar ergoemacs-status-position-map
(let ((map (copy-keymap mode-line-column-line-number-mode-map)))
;; (define-key map [mode-line down-mouse-3]
;; (lookup-key map [mode-line down-mouse-1]))
;; (define-key (lookup-key map [mode-line down-mouse-3])
;; [size-indication-mode]
;; '(menu-item "Display Buffer Size"
;; size-indication-mode
;; :help "Toggle displaying file size in the mode-line"
;; :button (:toggle . size-indication-mode)))
(define-key map [mode-line down-mouse-1] 'ignore)
(define-key map [mode-line mouse-1] 'ergoemacs-status-goto-line)
map)
"Position map (includes function `size-indication-mode').")
(defun ergoemacs-status-goto-line (event)
"Goto line.
EVENT is the mouse-click event to determine the window where
`goto-line' is called."
(interactive "e")
(with-selected-window (posn-window (event-start event))
(call-interactively #'goto-line)))
(defun ergoemacs-status-position ()
"`ergoemacs-status-mode' line position."
(let ((col (propertize
(or (and line-number-mode "%3c") "")
'mouse-face 'mode-line-highlight
'local-map ergoemacs-status-position-map)))
(when (>= (current-column) 80)
(setq col (propertize col 'face 'error)))
(concat (propertize
(or (and column-number-mode "%4l") "")
;; 'face 'bold
'mouse-face 'mode-line-highlight
'local-map ergoemacs-status-position-map)
(or (and column-number-mode line-number-mode ":") "")
col)))
(defun ergoemacs-status-size-indication-mode ()
"Gives mode-line information when variable `size-indication-mode' is non-nil."
(when size-indication-mode
(propertize
"%I"
'mouse-face 'mode-line-highlight
'local-map ergoemacs-status-position-map)))
(defun ergoemacs-status--read-only ()
"Gives read-only indicator."
(propertize (mode-icons--read-only-status)
'face 'mode-line-buffer-id))
(defun ergoemacs-status--modified ()
"Gives modified indicator."
(when (buffer-file-name)
(propertize (mode-icons--modified-status)
'face 'mode-line-buffer-id)))
(defvar flycheck-current-errors)
(defun ergoemacs-status--flycheck ()
"Element for flycheck."
(when (and (boundp 'flycheck-mode) flycheck-mode)
(ergoemacs-status-save-buffer-state
(let ((info (flycheck-count-errors flycheck-current-errors))
tmp
ret)
(when (setq tmp (assoc 'error info))
(push (propertize
(format "+%s"(cdr tmp))
'face 'error
'local-map '(keymap
(mode-line keymap
(mouse-1 . flycheck-next-error)
(mouse-3 . flycheck-prev-error))))
ret))
(when (setq tmp (assoc 'warning info))
(push (propertize
(format "+%s" (cdr tmp))
'face 'warning
'local-map '(keymap
(mode-line keymap
(mouse-1 . flycheck-next-error)
(mouse-3 . flycheck-prev-error))))
ret))
(when (setq tmp (assoc 'info info))
(push (propertize
(format "+%s" (cdr tmp))
'face 'font-lock-doc-face
'local-map '(keymap
(mode-line keymap
(mouse-1 . flycheck-next-error)
(mouse-3 . flycheck-prev-error))))
ret))
(when ret
;; (push 'flycheck-mode ergoemacs-status--automatic-hidden-minor-modes)
(setq ret (mapconcat (lambda(x) x) (reverse ret) " ")))
ret))))
(defun ergoemacs-status--nyan ()
"Nyan element for ergoemacs-status."
(when (and (boundp 'nyan-mode) nyan-mode)
(ergoemacs-status-save-buffer-state
(format-mode-line '(:eval (list (nyan-create)))))))
(defun ergoemacs-status--anzu ()
"Anzu element for `ergoemacs-status-mode'."
(when (bound-and-true-p anzu--state)
(anzu--update-mode-line)))
(defvar evil-state)
(defvar evil-visual-selection)
;; Adapted from spaceline.
(defun ergoemacs-status--column-number-at-pos (pos)
"Column number at POS. Analog to `line-number-at-pos'."
(save-excursion (goto-char pos) (current-column)))
(defun ergoemacs-status--selection-info ()
"Information about the size of the current selection, when applicable.
Supports both Emacs and Evil cursor conventions."
(when (or mark-active
(and (bound-and-true-p evil-local-mode)
(eq 'visual evil-state)))
(let* ((lines (count-lines (region-beginning) (min (1+ (region-end)) (point-max))))
(chars (- (1+ (region-end)) (region-beginning)))
(cols (1+ (abs (- (ergoemacs-status--column-number-at-pos (region-end))
(ergoemacs-status--column-number-at-pos (region-beginning))))))
(evil (and (bound-and-true-p evil-state) (eq 'visual evil-state)))
(rect (or (bound-and-true-p rectangle-mark-mode)
(and evil (eq 'block evil-visual-selection))))
(multi-line (or (> lines 1) (and evil (eq 'line evil-visual-selection)))))
(cond
(rect (format "%d×%d block" lines (if evil cols (1- cols))))
(multi-line (format "%d lines" lines))
(t (format "%d chars" (if evil chars (1- chars))))))))
(defun ergoemacs-status--hud ()
"HUD for ergoemacs-status."
"Heads up display"
(powerline-hud 'mode-line 'powerline-active1))
(defcustom ergoemacs-status-elements
'((:anzu (ergoemacs-status--anzu) 4 "Anzu")
(:selection-info (ergoemacs-status--selection-info) 1 "Selection Information")
(:read-only (ergoemacs-status--read-only) 3 "Read Only Indicator")
(:buffer-id (ergoemacs-status-buffer-id) nil "Buffer Name")
(:modified (ergoemacs-status--modified) nil "Modified Indicator")
(:size (ergoemacs-status-size-indication-mode) 2 (("Buffer Size" size-indication-mode)))
(:nyan (ergoemacs-status--nyan) 1 (("Nyan mode" nyan-mode)))
(:flycheck (ergoemacs-status--flycheck) 1 "Flycheck Errors")
(:position (ergoemacs-status-position) 2 ( ("Line" line-number-mode)
("Column" column-number-mode)))
(:vc (ergoemacs-status-use-vc powerline-vc) 1 "Version Control")
(:minor (ergoemacs-status--minor-modes) 4 "Minor Mode List")
(:narrow (mode-icons--generate-narrow) 4 "Narrow Indicator")
(:global (global-mode-string) nil nil (("Time" display-time-mode)
("Battery Charge" display-battery-mode)
("Fancy Battery Charge" fancy-battery-mode)))
(:coding ((lambda() (not (string= "undecided" (ergoemacs-status--encoding)))) ergoemacs-status--encoding) 2 "Coding System")
(:eol ((lambda() (not (string= ":" (mode-line-eol-desc)))) mode-icons--mode-line-eol-desc mode-line-eol-desc) 2 "End Of Line Convention")
(:major (ergoemacs-status-major-mode-item) nil nil "Language/Major mode")
(:which-func (ergoemacs-status-which-function-mode) nil nil "Which function")
(:process (mode-line-process) 1 nil "Process")
(:hud (ergoemacs-status--hud) 1 nil "Heads Up Display"))
"Elements of mode-line recognized by `ergoemacs-status-mode'.
This is a list of element recognized by `ergoemacs-status-mode'."
:type '(repeat
(list
(symbol :tag "Element Name")
(sexp :tag "Element Expression")
(choice :tag "How this element is collapsed"
(const :tag "Always keep this element" nil)
(integer :tag "Reduction Level"))
(choice :tag "Description"
(const :tag "No Description")
(string :tag "Description")
(repeat
(list
(string :tag "Description")
(symbol :tag "Minor mode function"))))))
:group 'ergoemacs-status)
(defun ergoemacs-status-elements-toggle (elt)
"Toggle the display of ELT."
(if (memq elt ergoemacs-status--suppressed-elements)
(setq ergoemacs-status--suppressed-elements (delq elt ergoemacs-status--suppressed-elements))
(push elt ergoemacs-status--suppressed-elements))
(ergoemacs-status-save-file)
(ergoemacs-status-current-update)
(force-mode-line-update))
(defun ergoemacs-status-right-click (event)
"Right click context menu for EVENT."
(interactive "e")
(with-selected-window (posn-window (event-start event))
(ergoemacs-status-elements-popup)))
(defvar ergoemacs-status-elements-popup nil)
(defvar ergoemacs-status-elements-popup-save nil)
(defun ergoemacs-status-elements-popup-save ()
"Save mode status defined in right-click menu."
(setq ergoemacs-status-elements-popup-save nil)
(dolist (elt ergoemacs-status-elements-popup)
(push (list elt (symbol-value elt))
ergoemacs-status-elements-popup-save))
(ergoemacs-status-save-file))
(defun ergoemacs-status-elements-popup-restore ()
"Restore status defined in right-click menu."
(dolist (elt ergoemacs-status-elements-popup-save)
(when (fboundp (nth 0 elt))
(if (nth 1 elt)
(funcall (nth 0 elt) 1)
(funcall (nth 0 elt) -1)))))
(defun ergoemacs-status-elements-popup (&optional dont-popup)
"Popup menu about displayed `ergoemacs-status' elements.
When DONT-POPUP is non-nil, just return the menu"
(let ((map (make-sparse-keymap "Status Bar"))
(i 0))
(define-key map [arrow-type] `(menu-item "Separators"
,(let ((map (make-sparse-keymap "Separator")))
(dolist (elt (reverse ergoemacs-status-sep-swap))
(define-key map (vector elt) `(menu-item ,(format "%s" elt)
(lambda(&rest _) (interactive)
(setq powerline-default-separator ',elt)
(force-mode-line-update)
(ergoemacs-status-save-file))
:button (:toggle . (eq powerline-default-separator ',elt)))))
map)))
(define-key map [sep-toggle] '(menu-item "---"))
(dolist (elt (reverse (append (plist-get ergoemacs-status-current :left)
(list "--")
(plist-get ergoemacs-status-current :center)
(list "--")
(plist-get ergoemacs-status-current :right))))
(cond
((equal elt "--")
(define-key map (vector (intern (format "status-element-popup-sep-%s" i))) '(menu-item "---")))
((consp elt)
(dolist (group-elt elt)
(when (setq elt (assoc group-elt ergoemacs-status-elements))
(if (stringp (nth 3 elt))
(define-key map (vector (car elt))
`(menu-item ,(nth 3 elt) (lambda(&rest _) (interactive) (ergoemacs-status-elements-toggle ,(car elt)))
:button (:toggle . (not (memq ,(car elt) ergoemacs-status--suppressed-elements)))))
(dolist (new-elt (reverse (nth 3 elt)))
(when (fboundp (nth 1 new-elt))
(pushnew (nth 1 new-elt) ergoemacs-status-elements-popup)
(define-key map (vector (nth 1 new-elt))
`(menu-item ,(nth 0 new-elt) (lambda (&rest _) (interactive) (call-interactively ',(nth 1 new-elt)) (ergoemacs-status-elements-popup-save)) :button (:toggle . ,(nth 1 new-elt))))))))))
(t
(when (setq elt (assoc elt ergoemacs-status-elements))
(if (stringp (nth 3 elt))
(define-key map (vector (car elt))
`(menu-item ,(nth 3 elt) (lambda(&rest _) (interactive) (ergoemacs-status-elements-toggle ,(car elt)))
:button (:toggle . (not (memq ,(car elt) ergoemacs-status--suppressed-elements)))))
(dolist (new-elt (reverse (nth 3 elt)))
(when (fboundp (nth 1 new-elt))
(pushnew (nth 1 new-elt) ergoemacs-status-elements-popup)
(define-key map (vector (nth 1 new-elt))
`(menu-item ,(nth 0 new-elt) (lambda (&rest _) (interactive) (call-interactively ',(nth 1 new-elt)) (ergoemacs-status-elements-popup-save)) :button (:toggle . ,(nth 1 new-elt))))))))))
(setq i (1+ i)))
(if dont-popup map
(popup-menu map))))
(defvar ergoemacs-status-buffer-id-map
(let ((map (make-sparse-keymap)))
(define-key map [mode-line mouse-1] #'ergoemacs-status-mouse-1-buffer)
(define-key map [mode-line mouse-3] #'ergoemacs-status-mouse-3-buffer)
map)
"Keymap for clicking on the buffer status.")
(defun ergoemacs-status-buffer-id ()
"Gives the Buffer identification string."
(propertize "%12b"
'mouse-face 'mode-line-highlight
'face 'mode-line-buffer-id
'local-map ergoemacs-status-buffer-id-map
'help-echo "Buffer name\nBuffer menu"))
(defvar ergoemacs-status-major-mode-map
(let ((map (make-sparse-keymap)))
(define-key map [mode-line down -mouse-3] (lookup-key mode-line-major-mode-keymap [mode-line down-mouse-3]))
(define-key map [mode-line mouse-2] #'describe-mode)
(define-key map [mode-line down-mouse-1]
`(menu-item "Menu Bar" ignore :filter ergoemacs-status--major-mode-menu-map))
map)
"Major mode keymap.")
(defun ergoemacs-status-major-mode-item ()
"Gives `major-mode' item for mode-line."
(propertize mode-name
'mouse-face 'mode-line-highlight
'local-map ergoemacs-status-major-mode-map
'help-echo "Major mode\nmouse-1: Display major mode menu\nmouse-2: Show help for major mode\nmouse-3: Toggle minor modes"))
(defvar which-func-format)