tidy up some logic
[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
114   ;;     (equal major-mode 'eshell-mode))
115   )
116
117 (defun my-get-buffer-name-face (active-face, changed-face, readonly-face)
118   (cond ((and buffer-read-only (not (my-is-special-buffer)))
119          readonly-face)
120         ((and (buffer-modified-p) (not (my-is-special-buffer)))
121          changed-face)
122         (t active-face)))
123
124 (defun sd/powerline-center-theme_revised-2 ()
125   "Setup a mode-line with major and minor modes centered."
126   (interactive)
127   (setq-default mode-line-format
128                 '("%e"
129                   (:eval
130                    (let* ((window-type (my-get-window-type))
131                           (active (powerline-selected-window-active))
132                           (my-face1 'sd/powerline-active1 )
133                           (face1 'powerline-active1)
134                           (face2 'powerline-active2)
135                           (separator-left (intern (format "powerline-%s-%s" (powerline-current-separator) (car powerline-default-separator-dir))))
136                           (separator-right (intern (format "powerline-%s-%s" (powerline-current-separator) (cdr powerline-default-separator-dir))))
137                           (lface (if (and (not active) (or (= window-type 3) (= window-type 6))) face2 my-face1))
138                           (cface (if active my-face1 face2))
139                           (rface (if (and (not active) (or (= window-type 2) (= window-type 5))) face2 my-face1))
140                           (lhs (cond ((or (= window-type 1) (= window-type 2))
141                                       (my-build-left-below-mode-line separator-left lface face1))
142                                      ((or (= window-type 3) (= window-type 6))
143                                       (list (powerline-buffer-id lface 'l)
144                                             (powerline-raw "%* " lface)
145                                             (funcall separator-left lface face1 )))
146                                      (t
147                                       nil)))
148                           (center (cond ((or (= window-type 1) (= window-type 4))
149                                          (list (powerline-raw " " face1)
150                                                (funcall separator-right face1 cface)
151                                                (powerline-raw "%*" cface)
152                                                (powerline-buffer-id cface 'r)
153                                                (funcall separator-left cface face1)))
154                                         (t
155                                          nil)))
156                           (rhs (cond ((or (= window-type 1) (= window-type 3))
157                                       (list (funcall separator-right face1 rface)
158                                             (powerline-raw (format-time-string " %I:%M %p ") rface 'r)))
159                                      ((or (= window-type 2) (= window-type 5))
160                                       (list (funcall separator-right face1 rface)
161                                             (powerline-raw "%*" rface)
162                                             (powerline-buffer-id rface 'r)
163                                             (powerline-raw " " rface)))
164                                      (t
165                                       nil))))
166                      (concat (powerline-render lhs)
167                              (powerline-fill-center face1 (/ (powerline-width center) 2.0))
168                              (powerline-render center)
169                              (powerline-fill face1 (powerline-width rhs))
170                              (powerline-render rhs)))))))
171
172 (sd/powerline-center-theme_revised-2)
173
174 (provide 'my-mode-line)
175 ;;; my-mode-line.el ends here
176   
177