c303cfc4166178b6f0f6277009a593dc744e3cbb
[dotfiles.git] / emacs.d / 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
68 (defun sd/powerline-center-theme_revised-2 ()
69   "Setup a mode-line with major and minor modes centered."
70   (interactive)
71   (setq-default mode-line-format
72                 '("%e"
73                   (:eval
74                    (let* ((window-type (my-get-window-type))
75                           (active (powerline-selected-window-active))
76                           (my-face1 'sd/powerline-active1 )
77                           ;; (my-face-buffer-modified (if (and (sd/buffer-is-eshel-or-scratch) (buffer-modified-p) (not buffer-read-only)) 
78                           ;;                              'sd/buffer-modified-active1
79                           ;;                            (if buffer-read-only 'sd/buffer-view-active1
80                           ;;                              my-face1)))
81                           (face1 'powerline-active1)
82                           (face2 'powerline-active2)
83                           (separator-left (intern (format "powerline-%s-%s"
84                                                           (powerline-current-separator)
85                                                           (car powerline-default-separator-dir))))
86                           (separator-right (intern (format "powerline-%s-%s"
87                                                            (powerline-current-separator)
88                                                            (cdr powerline-default-separator-dir))))
89                           (lface (if (and (not active)
90                                           (or (= window-type 3) (= window-type 6)))
91                                      face2
92                                    my-face1))
93                           (cface (if active my-face1 face2))
94                           (rface (if (and (not active)
95                                           (or (= window-type 2) (= window-type 5)))
96                                      face2
97                                    my-face1))
98                           (lhs (cond ((or (= window-type 1) (= window-type 2))
99                                       (list (powerline-raw " = ws1 = " lface)
100                                             (funcall separator-left lface face1 )))
101                                      ((or (= window-type 3) (= window-type 6))
102                                       (list (powerline-buffer-id lface 'l)
103                                             (powerline-raw "%* " lface)
104                                             (funcall separator-left lface face1 )))
105                                      (t
106                                       nil)))
107                           (center (if (or (= window-type 1) (= window-type 4))
108                                       (list (powerline-raw " " face1)
109                                             (funcall separator-right face1 cface)
110                                             (powerline-raw "%*" cface)
111                                             (powerline-buffer-id cface 'r)
112                                             (funcall separator-left cface face1))
113                                     nil))
114                           (rhs (cond ((or (= window-type 1) (= window-type 3))
115                                       (list (funcall separator-right face1 rface)
116                                             (powerline-raw (format-time-string " %I:%M %p ") rface 'r)))
117                                      ((or (= window-type 2) (= window-type 5))
118                                       (list (funcall separator-right face1 rface)
119                                             (powerline-raw "%*" rface)
120                                             (powerline-buffer-id rface 'r)
121                                             (powerline-raw " " rface)))
122                                      (t
123                                       nil))))
124                      (concat (powerline-render lhs)
125                              (powerline-fill-center face1 (/ (powerline-width center) 2.0))
126                              (powerline-render center)
127                              (powerline-fill face1 (powerline-width rhs))
128                              (powerline-render rhs)))))))
129
130 (sd/powerline-center-theme_revised-2)
131
132 (provide 'my-mode-line)
133 ;;; my-mode-line.el ends here
134   
135