Emacs - enable evil mode
[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 (defun my-get-ws-name-list ()
99   "Return the name list of workspaces gotten from `perspeen-modestring' without the properties."
100   (split-string (substring-no-properties (cadr perspeen-modestring)) "|"))
101
102 (defun my-get-selected-ws-name ()
103   (let ((ret))
104     (dolist (i (split-string (cadr perspeen-modestring) "|") ret)
105       (if (equal (car (text-properties-at 1 i))
106                  'face)
107           (setq ret (substring-no-properties i))))))
108
109 (defun my-build-left-below-mode-line (separator lface face1)
110   (let ((l)
111         (selected-name (my-get-selected-ws-name))
112         (name-list (my-get-ws-name-list)))
113     (setq l (list (powerline-raw " WS " lface)))
114     (if (string= selected-name (car name-list))
115         (progn
116           (nconc l (list (funcall separator lface 'my-powerline-hl-ws)
117                          (powerline-raw (car name-list) 'my-powerline-hl-ws)
118                          (funcall separator 'my-powerline-hl-ws face1)))
119           (dolist (i (cdr name-list))
120             (nconc l (list (powerline-raw i face1)
121                            (funcall separator face1 face1)))))
122       (nconc l (list (funcall separator lface face1)))
123       (while name-list
124         (let ((current-selected (string= selected-name (car name-list)))
125               (next-selected (string= selected-name (cadr name-list))))
126           (nconc l (list (powerline-raw (car name-list) (if current-selected 'my-powerline-hl-ws face1))
127                          (funcall separator (if current-selected 'my-powerline-hl-ws face1) (if next-selected 'my-powerline-hl-ws face1)))))
128         (pop name-list)))
129     l))
130
131 (defun my-is-special-buffer ()
132   ;; suppose all buffer name started with a star is a special buffer.
133   (string-match "\*" (buffer-name)))
134
135 (defun my-get-buffer-name-face (orignal-face)
136   (cond
137    ((not (powerline-selected-window-active)) orignal-face)
138    (buffer-read-only 
139     'sd/buffer-view-active1)
140    ((and (buffer-modified-p) (not (my-is-special-buffer)))
141     'sd/buffer-modified-active1)
142    (t orignal-face)))
143
144 (defun sd/powerline-center-theme_revised-2 ()
145   "Setup a mode-line with major and minor modes centered."
146   (interactive)
147   (setq-default mode-line-format
148                 '("%e"
149                   (:eval
150                    (let* ((window-type (my-get-window-type))
151                           (active (powerline-selected-window-active))
152                           (my-face1 'sd/powerline-active1 )
153                           (face1 'powerline-active1)
154                           ;; (face1 'mode-line-inactive)
155                           (face2 'powerline-active2)
156                           (separator-left (intern (format "powerline-%s-%s" (powerline-current-separator) (car powerline-default-separator-dir))))
157                           (separator-right (intern (format "powerline-%s-%s" (powerline-current-separator) (cdr powerline-default-separator-dir))))
158                           (lface (if (and (not active) (or (= window-type 3) (= window-type 6))) face2 my-face1))
159                           (cface (if active my-face1 face2))
160                           (rface (if (and (not active) (or (= window-type 2) (= window-type 5))) face2 my-face1))
161                           (lhs (cond ((or (= window-type 1) (= window-type 2))
162                                       (my-build-left-below-mode-line separator-left lface face1))
163                                      ((or (= window-type 3) (= window-type 6))
164                                       (list (powerline-buffer-id (my-get-buffer-name-face lface) 'l)
165                                             (unless (my-is-special-buffer)
166                                               (powerline-raw "%* " (my-get-buffer-name-face lface)))
167                                             (funcall separator-left (my-get-buffer-name-face lface) face1 )))
168                                      (t
169                                       nil)))
170                           (center (cond ((or (= window-type 1) (= window-type 4))
171                                          (list (powerline-raw " " face1)
172                                                (funcall separator-right face1 (my-get-buffer-name-face cface))
173                                                (unless (my-is-special-buffer)
174                                                  (powerline-raw "%*" (my-get-buffer-name-face cface)))
175                                                (powerline-buffer-id (my-get-buffer-name-face cface) 'r)
176                                                (funcall separator-left (my-get-buffer-name-face cface) face1)))
177                                         (t
178                                          nil)))
179                           (rhs (cond ((or (= window-type 1) (= window-type 3))
180                                       (list (powerline-raw (format-time-string "%Y-%m-%d %a") face1 'r)
181                                             (funcall separator-right face1 rface)
182                                             (powerline-raw (format-time-string " %I:%M %p ") rface 'r)))
183                                      ((or (= window-type 2) (= window-type 5))
184                                       (list (funcall separator-right face1 (my-get-buffer-name-face rface))
185                                             (unless (my-is-special-buffer)
186                                               (powerline-raw "%*" (my-get-buffer-name-face rface)))
187                                             (powerline-buffer-id (my-get-buffer-name-face rface) 'r)
188                                             (powerline-raw " " (my-get-buffer-name-face rface))))
189                                      (t
190                                       nil))))
191                      (concat (powerline-render lhs)
192                              (powerline-fill-center face1 (/ (powerline-width center) 2.0))
193                              (powerline-render center)
194                              (powerline-fill face1 (powerline-width rhs))
195                              (powerline-render rhs)))))))
196
197 (sd/powerline-center-theme_revised-2)
198
199
200
201
202 (provide 'my-mode-line)
203 ;;; my-mode-line.el ends here
204   
205