move modeline into new folder
[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 (defun my-get-ws-name-list ()
72   "Return the name list of workspaces gotten from `perspeen-modestring' without the properties."
73   (split-string (substring-no-properties (cadr perspeen-modestring)) "|"))
74
75 (defun my-build-left-below-mode-line (separator lface face1)
76   (let ((l))
77     (setq l (list (powerline-raw " workspace " lface)))
78     (mapc (lambda (i)
79             (setq l (append l (list (powerline-raw i lface))))
80             (setq l (append l (list (funcall separator lface face1)))))
81           (my-get-ws-name-list))
82     l))
83
84
85 (defun sd/powerline-center-theme_revised-2 ()
86   "Setup a mode-line with major and minor modes centered."
87   (interactive)
88   (setq-default mode-line-format
89                 '("%e"
90                   (:eval
91                    (let* ((window-type (my-get-window-type))
92                           (active (powerline-selected-window-active))
93                           (my-face1 'sd/powerline-active1 )
94                           ;; (my-face-buffer-modified (if (and (sd/buffer-is-eshel-or-scratch) (buffer-modified-p) (not buffer-read-only)) 
95                           ;;                              'sd/buffer-modified-active1
96                           ;;                            (if buffer-read-only 'sd/buffer-view-active1
97                           ;;                              my-face1)))
98                           (face1 'powerline-active1)
99                           (face2 'powerline-active2)
100                           (separator-left (intern (format "powerline-%s-%s"
101                                                           (powerline-current-separator)
102                                                           (car powerline-default-separator-dir))))
103                           (separator-right (intern (format "powerline-%s-%s"
104                                                            (powerline-current-separator)
105                                                            (cdr powerline-default-separator-dir))))
106                           (lface (if (and (not active) (or (= window-type 3) (= window-type 6)))
107                                      face2
108                                    my-face1))
109                           (cface (if active my-face1 face2))
110                           (rface (if (and (not active) (or (= window-type 2) (= window-type 5)))
111                                      face2
112                                    my-face1))
113                           (lhs (cond ((or (= window-type 1) (= window-type 2))
114                                       (my-build-left-below-mode-line separator-left lface face1)
115                                       ;; (list (powerline-raw "==wwww===" 'powerline-active1))
116                                       ;; (list (powerline-raw " workspace " lface)
117
118                                       ;;            ;; workspaces
119                                 
120                                             
121                                       ;;            (funcall separator-left lface 'my-powerline-hl-ws)
122                                       ;;            (powerline-raw " ws1 " 'my-powerline-hl-ws)
123                                       ;;            (funcall separator-left 'my-powerline-hl-ws lface)
124                                       ;;            (powerline-raw " ws2 " lface)
125                                       ;;            (funcall separator-left lface face1)
126                                       ;;            )
127                                       )
128                                      ((or (= window-type 3) (= window-type 6))
129                                       (list (powerline-buffer-id lface 'l)
130                                             (powerline-raw "%* " lface)
131                                             (funcall separator-left lface face1 )))
132                                      (t
133                                       nil)))
134                           (center (if (or (= window-type 1) (= window-type 4))
135                                       (list (powerline-raw " " face1)
136                                             (funcall separator-right face1 cface)
137                                             (powerline-raw "%*" cface)
138                                             (powerline-buffer-id cface 'r)
139                                             (funcall separator-left cface face1))
140                                     nil)
141                            )
142                           (rhs (cond ((or (= window-type 1) (= window-type 3))
143                                       (list (funcall separator-right face1 rface)
144                                             (powerline-raw (format-time-string " %I:%M %p ") rface 'r)))
145                                      ((or (= window-type 2) (= window-type 5))
146                                       (list (funcall separator-right face1 rface)
147                                             (powerline-raw "%*" rface)
148                                             (powerline-buffer-id rface 'r)
149                                             (powerline-raw " " rface)))
150                                      (t
151                                       nil))
152                                ))
153                      (concat (powerline-render lhs)
154                              (powerline-fill-center face1 (/ (powerline-width center) 2.0))
155                              (powerline-render center)
156                              (powerline-fill face1 (powerline-width rhs))
157                              (powerline-render rhs)))))))
158
159 (sd/powerline-center-theme_revised-2)
160
161 (provide 'my-mode-line)
162 ;;; my-mode-line.el ends here
163   
164