Emacs - suppress the errors at statup
[dotfiles.git] / emacs.d / elisp / my-mode-line.el
1 ;;; my-mode-line.el --- my mode line based on powerline  -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2017  Peng Li
4
5 ;; Author: Peng Li <seudut@gmail.com>
6 ;; Keywords: 
7
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 ;;; Commentary:
22
23 ;; test
24
25 ;;; Code:
26
27 ;; A windows may have six kinds different mode-line
28 ;; type = 1.
29 ;;   Left - workspace list
30 ;;   Right - Showing Time
31 ;;   Center - buffer , as no windows in its left or right.
32 ;;   -----------------------------
33 ;;   |>>>         O           <<<| 
34 ;;   -----------------------------
35 ;; type = 2.
36 ;;   -----------------------------
37 ;;   |>>>                       O| 
38 ;;   -----------------------------
39 ;; type = 3.
40 ;;   -----------------------------
41 ;;   |O                       <<<| 
42 ;;   -----------------------------
43 ;; type = 4.
44 ;;   -----------------------------
45 ;;   |            O              | 
46 ;;   -----------------------------
47 ;; type = 5.
48 ;;   -----------------------------
49 ;;   |                          O| 
50 ;;   -----------------------------
51 ;; type = 6.
52 ;;   -----------------------------
53 ;;   |O                          | 
54 ;;   -----------------------------
55
56 (defun my-below-winow-has-effect-window ()
57   "Return true when its below has no active window or
58 its below window is minibuffer window. "
59   (let ((window (window-in-direction 'below nil nil 1)))
60     (and window
61          (not (window-minibuffer-p window)))))
62
63 (defun my-get-window-type ()
64   "According to the window layout, separate the windows as six types.
65 Each type will have different mode line. This function return the defined
66 window type."
67   (cond ((and (not (window-in-direction 'right nil nil -1))
68               (not (window-in-direction 'left nil nil -1))
69               (not (my-below-winow-has-effect-window)))
70          1)
71         ((and (window-in-direction 'right nil t -1)
72               (not (window-in-direction 'left nil nil -1))
73               (not (my-below-winow-has-effect-window)))
74          2)
75         ((and (not (window-in-direction 'right nil nil -1))
76               (window-in-direction 'left nil nil -1)
77               (not (my-below-winow-has-effect-window)))
78          3)
79         ((and (window-in-direction 'right nil nil -1)
80               (not (window-in-direction 'left nil nil -1))
81               (my-below-winow-has-effect-window))
82          5)
83         ((and (not (window-in-direction 'right nil nil -1))
84               (window-in-direction 'left nil nil -1)
85               (my-below-winow-has-effect-window))
86          6)
87         (t
88          4)))
89
90 (defface my-powerline-hl-ws '((t (:background "blue" :foreground "black" :inherit mode-line)))
91   "My Powerline face 1 based on powerline-active1."
92   :group 'powerline)
93
94 (defface my-powerline-ws-bg '((t (:background "blue" :foreground "black" :inherit mode-line)))
95   "My Powerline face 1 based on powerline-active1."
96   :group 'powerline)
97
98
99 (defface my-evil-mode-tag '((t (:foreground "black" :inherit mode-line)))
100   "My evil mode line"
101   :group 'powerline)
102
103 (defface my-powerline-evil-normal '((t (:background "green" :foreground "black" :inherit mode-line)))
104   "My Powerline face 1 based on powerline-active1."
105   :group 'powerline)
106
107 (defface my-powerline-evil-emacs '((t (:background "red" :foreground "black" :inherit mode-line)))
108   "My Powerline face 1 based on powerline-active1."
109   :group 'powerline)
110
111 (defface my-powerline-evil-insert '((t (:background "orange" :foreground "black" :inherit mode-line)))
112   "My Powerline face 1 based on powerline-active1."
113   :group 'powerline)
114
115 (defface my-powerline-evil-visual '((t (:background "orange" :foreground "black" :inherit mode-line)))
116   "My Powerline face 1 based on powerline-active1."
117   :group 'powerline)
118
119 (defun my-get-ws-name-list ()
120   "Return the name list of workspaces gotten from `perspeen-modestring' without the properties."
121   (split-string (substring-no-properties (cadr perspeen-modestring)) "|"))
122
123 (defun my-get-selected-ws-name ()
124   (let ((ret))
125     (dolist (i (split-string (cadr perspeen-modestring) "|") ret)
126       (if (equal (car (text-properties-at 1 i))
127                  'face)
128           (setq ret (substring-no-properties i))))))
129
130
131 (defun my-build-left-below-mode-line (separator lface face1)
132   (let ((l)
133         (selected-name (my-get-selected-ws-name))
134         (name-list (my-get-ws-name-list)))
135     (if (string= selected-name (car name-list))
136         (progn
137           (setq l (list (funcall separator lface 'my-powerline-hl-ws)
138                         (powerline-raw (car name-list) 'my-powerline-hl-ws)
139                         (funcall separator 'my-powerline-hl-ws face1)))
140           (dolist (i (cdr name-list))
141             (nconc l (list (powerline-raw i face1)
142                            (funcall separator face1 face1)))))
143       (setq l (list (funcall separator lface face1)))
144       (while name-list
145         (let ((current-selected (string= selected-name (car name-list)))
146               (next-selected (string= selected-name (cadr name-list))))
147           (nconc l (list (powerline-raw (car name-list) (if current-selected 'my-powerline-hl-ws face1))
148                          (funcall separator (if current-selected 'my-powerline-hl-ws face1) (if next-selected 'my-powerline-hl-ws face1)))))
149         (pop name-list)))
150     l))
151
152 (defun my-is-special-buffer ()
153   ;; suppose all buffer name started with a star is a special buffer.
154   (string-match "\*" (buffer-name)))
155
156 (defun my-get-buffer-name-face (orignal-face)
157   (cond
158    ((not (powerline-selected-window-active)) orignal-face)
159    (buffer-read-only 
160     'sd/buffer-view-active1)
161    ((and (buffer-modified-p) (not (my-is-special-buffer)))
162     'sd/buffer-modified-active1)
163    (t orignal-face)))
164
165 (defun get-active-window-evil-tag ()
166   (dolist (i (window-list) ret)
167     (with-selected-window i
168       (when (powerline-selected-window-active)
169         (setq ret (with-current-buffer (window-buffer)
170                     evil-mode-line-tag))))))
171
172 (defun sd/powerline-center-theme_revised-2 ()
173   "Setup a mode-line with major and minor modes centered."
174   (interactive)
175   (setq-default mode-line-format
176                 '("%e"
177                   (:eval
178                    (let* ((window-type (my-get-window-type))
179                           (active (powerline-selected-window-active))
180                           (my-face1 'sd/powerline-active1 )
181                           (face1 'powerline-active1)
182                           ;; (face1 'mode-line-inactive)
183                           (face2 'powerline-active2)
184                           (separator-left (intern (format "powerline-%s-%s" (powerline-current-separator) (car powerline-default-separator-dir))))
185                           (separator-right (intern (format "powerline-%s-%s" (powerline-current-separator) (cdr powerline-default-separator-dir))))
186                           (lface (if (and (not active) (or (= window-type 3) (= window-type 6))) face2 my-face1))
187                           (cface (if active my-face1 face2))
188                           (rface (if (and (not active) (or (= window-type 2) (= window-type 5))) face2 my-face1))
189                           (lhs (cond ((or (= window-type 1) (= window-type 2))
190                                       (append (list (powerline-raw (when evil-mode (get-active-window-evil-tag)) 'my-powerline-evil-normal)
191                                                     (powerline-raw " WS " lface))
192                                               (append
193                                                (my-build-left-below-mode-line separator-left lface face1))))
194                                      ((or (= window-type 3) (= window-type 6))
195                                       (list (powerline-buffer-id (my-get-buffer-name-face lface) 'l)
196                                             (unless (my-is-special-buffer)
197                                               (powerline-raw "%* " (my-get-buffer-name-face lface)))
198                                             (funcall separator-left (my-get-buffer-name-face lface) face1 )))
199                                      (t
200                                       nil)))
201                           (center (cond ((or (= window-type 1) (= window-type 4))
202                                          (list (powerline-raw " " face1)
203                                                (funcall separator-right face1 (my-get-buffer-name-face cface))
204                                                (unless (my-is-special-buffer)
205                                                  (powerline-raw "%*" (my-get-buffer-name-face cface)))
206                                                (powerline-buffer-id (my-get-buffer-name-face cface) 'r)
207                                                (funcall separator-left (my-get-buffer-name-face cface) face1)))
208                                         (t
209                                          nil)))
210                           (rhs (cond ((or (= window-type 1) (= window-type 3))
211                                       (list (powerline-raw (format-time-string "%Y-%m-%d %a") face1 'r)
212                                             (funcall separator-right face1 rface)
213                                             (powerline-raw (format-time-string " %I:%M %p ") rface 'r)))
214                                      ((or (= window-type 2) (= window-type 5))
215                                       (list (funcall separator-right face1 (my-get-buffer-name-face rface))
216                                             (unless (my-is-special-buffer)
217                                               (powerline-raw "%*" (my-get-buffer-name-face rface)))
218                                             (powerline-buffer-id (my-get-buffer-name-face rface) 'r)
219                                             (powerline-raw " " (my-get-buffer-name-face rface))))
220                                      (t
221                                       nil))))
222                      (concat (powerline-render lhs)
223                              (powerline-fill-center face1 (/ (powerline-width center) 2.0))
224                              (powerline-render center)
225                              (powerline-fill face1 (powerline-width rhs))
226                              (powerline-render rhs)))))))
227
228 (sd/powerline-center-theme_revised-2)
229
230
231
232
233 (provide 'my-mode-line)
234 ;;; my-mode-line.el ends here
235   
236