2e6f88c4c0ecb736962cdd4c1ca81d7b3a3ed293
[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 ;;   -----------------------------
30 ;;   |>>>         O           <<<| 
31 ;;   -----------------------------
32
33 (defun my-below-winow-has-effect-window ()
34   "Return true when its below has no active window or
35 its below window is minibuffer window. "
36   (let ((window (window-in-direction 'below nil nil 1)))
37     (and window
38          (not (window-minibuffer-p window)))))
39
40 (defun my-get-window-type ()
41   "According to the window layout, separate the windows as six types.
42 Each type will have different mode line. This function return the defined
43 window type."
44   (cond ((and (not (window-in-direction 'right nil nil -1))
45               (not (window-in-direction 'left nil nil -1))
46               (not (my-below-winow-has-effect-window)))
47          1)
48         ((and (window-in-direction 'right nil t -1)
49               (not (window-in-direction 'left nil nil -1))
50               (not (my-below-winow-has-effect-window)))
51          2)
52         ((and (not (window-in-direction 'right nil nil -1))
53               (window-in-direction 'left nil nil -1)
54               (not (my-below-winow-has-effect-window)))
55          3)
56         ((and (window-in-direction 'right nil nil -1)
57               (not (window-in-direction 'left nil nil -1))
58               (my-below-winow-has-effect-window))
59          5)
60         ((and (not (window-in-direction 'right nil nil -1))
61               (window-in-direction 'left nil nil -1)
62               (my-below-winow-has-effect-window))
63          6)
64         (t
65          4)))
66
67 (defface my-powerline-hl-ws '((t (:background "red" :foreground "black" :inherit mode-line)))
68   "My Powerline face 1 based on powerline-active1."
69   :group 'powerline)
70
71 (defface my-powerline-ws-bg '((t (:background "blue" :foreground "black" :inherit mode-line)))
72   "My Powerline face 1 based on powerline-active1."
73   :group 'powerline)
74
75 (defun my-get-ws-name-list ()
76   "Return the name list of workspaces gotten from `perspeen-modestring' without the properties."
77   (split-string (substring-no-properties (cadr perspeen-modestring)) "|"))
78
79 (defun my-get-selected-ws-name ()
80   (let ((ret))
81     (dolist (i (split-string (cadr perspeen-modestring) "|") ret)
82       (if (equal (car (text-properties-at 1 i))
83                  'face)
84           (setq ret (substring-no-properties i))))))
85
86 (defun my-build-left-below-mode-line (separator lface face1)
87   (let ((l)
88         (selected-name (my-get-selected-ws-name))
89         (name-list (my-get-ws-name-list)))
90     (setq l (list (powerline-raw " workspace " lface)))
91     (if (string= selected-name (car name-list))
92         (progn
93           (nconc l (list (funcall separator lface 'my-powerline-hl-ws)
94                          (powerline-raw (car name-list) 'my-powerline-hl-ws)
95                          (funcall separator 'my-powerline-hl-ws face1)))
96           (dolist (i (cdr name-list))
97             (nconc l (list (powerline-raw i face1)
98                            (funcall separator face1 face1)))))
99       (nconc l (list (funcall separator lface face1)))
100       (while name-list
101         (let ((current-selected (string= selected-name (car name-list)))
102               (next-selected (string= selected-name (cadr name-list))))
103           (nconc l (list (powerline-raw (car name-list) (if current-selected 'my-powerline-hl-ws face1))
104                          (funcall separator (if current-selected 'my-powerline-hl-ws face1) (if next-selected 'my-powerline-hl-ws face1)))))
105         (pop name-list)))
106     l))
107
108 (defun my-is-special-buffer ()
109   ;; suppose all buffer name started with a star is a special buffer.
110   (string-match "\*" (buffer-name))
111   ;; (or (string-match "*scratch*" (buffer-name))
112   ;;     (string-match "*Message*" (buffer-name))
113   ;;     (equal major-mode 'eshell-mode))
114   )
115
116 (defun my-get-buffer-name-face (orignal-face &optional active)
117   (cond
118    ((not active) orignal-face)
119    (buffer-read-only 
120     'sd/buffer-view-active1)
121    ((and (buffer-modified-p) (not (my-is-special-buffer)))
122     'sd/buffer-modified-active1)
123    (t orignal-face)))
124
125 (defun sd/powerline-center-theme_revised-2 ()
126   "Setup a mode-line with major and minor modes centered."
127   (interactive)
128   (setq-default mode-line-format
129                 '("%e"
130                   (:eval
131                    (let* ((window-type (my-get-window-type))
132                           (active (powerline-selected-window-active))
133                           (my-face1 'sd/powerline-active1 )
134                           (face1 'powerline-active1)
135                           (face2 'powerline-active2)
136                           (separator-left (intern (format "powerline-%s-%s" (powerline-current-separator) (car powerline-default-separator-dir))))
137                           (separator-right (intern (format "powerline-%s-%s" (powerline-current-separator) (cdr powerline-default-separator-dir))))
138                           (lface (if (and (not active) (or (= window-type 3) (= window-type 6))) face2 my-face1))
139                           (cface (if active my-face1 face2))
140                           (rface (if (and (not active) (or (= window-type 2) (= window-type 5))) face2 my-face1))
141                           (lhs (cond ((or (= window-type 1) (= window-type 2))
142                                       (my-build-left-below-mode-line separator-left lface face1))
143                                      ((or (= window-type 3) (= window-type 6))
144                                       (list (powerline-buffer-id (my-get-buffer-name-face lface active) 'l)
145                                             (powerline-raw "%* " (my-get-buffer-name-face lface active))
146                                             (funcall separator-left (my-get-buffer-name-face lface active) face1 )))
147                                      (t
148                                       nil)))
149                           (center (cond ((or (= window-type 1) (= window-type 4))
150                                          (list (powerline-raw " " face1)
151                                                (funcall separator-right face1 (my-get-buffer-name-face cface active))
152                                                (powerline-raw "%*" (my-get-buffer-name-face cface active))
153                                                (powerline-buffer-id (my-get-buffer-name-face cface active) 'r)
154                                                (funcall separator-left (my-get-buffer-name-face cface active) face1)))
155                                         (t
156                                          nil)))
157                           (rhs (cond ((or (= window-type 1) (= window-type 3))
158                                       (list (funcall separator-right face1 rface)
159                                             (powerline-raw (format-time-string " %I:%M %p ") rface 'r)))
160                                      ((or (= window-type 2) (= window-type 5))
161                                       (list (funcall separator-right face1 (my-get-buffer-name-face rface active))
162                                             (powerline-raw "%*" (my-get-buffer-name-face rface active))
163                                             (powerline-buffer-id (my-get-buffer-name-face rface active) 'r)
164                                             (powerline-raw " " (my-get-buffer-name-face rface active))))
165                                      (t
166                                       nil))))
167                      (concat (powerline-render lhs)
168                              (powerline-fill-center face1 (/ (powerline-width center) 2.0))
169                              (powerline-render center)
170                              (powerline-fill face1 (powerline-width rhs))
171                              (powerline-render rhs)))))))
172
173 (sd/powerline-center-theme_revised-2)
174
175
176
177
178 (provide 'my-mode-line)
179 ;;; my-mode-line.el ends here
180   
181