summaryrefslogtreecommitdiff
path: root/feebleline.el
blob: af4fff995948d82ada798c39144fc321501ca5cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
;;; feebleline.el --- Replace modeline with a slimmer proxy

;; Copyright 2018 Benjamin Lindqvist

;; Author: Benjamin Lindqvist <benjamin.lindqvist@gmail.com>
;; Maintainer: Benjamin Lindqvist <benjamin.lindqvist@gmail.com>
;; URL: https://github.com/tautologyclub/feebleline
;; Package-Version: 1.1
;; Version: 1.1

;; This file is not part of GNU Emacs.

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; For hardline Luddite editing!

;; Feebleline removes the modeline and replaces it with a slimmer proxy
;; version, which displays some basic information in the echo area
;; instead.  This information is only displayed if the echo area is not used
;; for anything else (but if you switch frame/window, it will replace whatever
;; message is currently displayed).

;; To customize feebleline's format, modify `feebleline-mode-line-text'.

;; NOTE:
;; feebleline.el will look considerably better with the following
;; settings:

;;   (window-divider-mode t)
;;   (setq window-divider-default-bottom-width 1)
;;   (setq window-divider-default-places (quote bottom-only))

;; But this mode does not work for all EMACS versions and may not work with
;; terminal EMACS (but I haven't checked).  If you're on GUI EMACS and your
;; version supports it, just place the following in your init file:

;;   (feebleline-default-settings)

;; Otherwise, do (feebleline-mode t) instead, but be warned that I'm not sure
;; if it will look good.

;;; Code:

(require 'advice)

(defvar feebleline-use-legacy-settings nil)
(when (< emacs-major-version 25)
  (setq feebleline-use-legacy-settings t))

(setq feebleline-use-legacy-settings nil)

(defface feebleline-time-face '((t :inherit 'font-lock-comment-face))
  "Feebleline timestamp face."
  :group 'feebleline)
(defface feebleline-linum-face '((t :inherit 'default))
  "Feebleline linum face."
  :group 'feebleline)
(defface feebleline-bufname-face '((t :inherit 'font-lock-function-name-face))
  "Feebleline filename face."
  :group 'feebleline)
(defface feebleline-asterisk-face '((t :foreground "salmon"))
  "Feebleline file modified asterisk face."
  :group 'feebleline)
(defface feebleline-previous-buffer-face '((t :foreground "#7e7e7e"))
  "Feebleline filename face."
  :group 'feebleline)
(defface feebleline-dir-face '((t :inherit 'font-lock-variable-name-face))
  "Feebleline filename face."
  :group 'feebleline)
(defface feebleline-git-branch-face '((t :inherit 'font-lock-comment-face :bold nil :italic t))
  "Feebleline filename face."
  :group 'feebleline)

;; Customizations
(defcustom feebleline-show-time nil
  "Set this if you want to show the time in the modeline proxy."
  :group 'feebleline)
(defcustom feebleline-show-git-branch nil
  "Set this if you want to show the git branch in the modeline proxy."
  :group 'feebleline)
(defcustom feebleline-show-previous-buffer nil
  "Set this if you want to show the previous 'buffer-name' in the modeline proxy."
  :group 'feebleline)
(defcustom feebleline-show-directory t
  "Set this if you want to show the direcory path as well as the file-name in the modeline proxy."
  :group 'feebleline)
(defcustom feebleline-show-linenum t
  "Set this if you want to show line number and column number in the modeline proxy."
  :group 'feebleline)

(defun feebleline-previous-buffer-name ()
  "Get name of previous buffer."
  (buffer-name (other-buffer (current-buffer) 1)))

(defvar feebleline-mode-line-text nil
  "Each element is a list with the following format:

    (FORMAT-STRING FORMAT-ARGS PROPS)

FORMAT-STRING will be used as the first argument to `format', and
FORMAT-ARGS (a list) will be expanded as the rest of `format'
arguments.  If PROPS is given, it should be a list which will be
sent to `add-text-properties'.")

(require 'magit nil t)
(if (fboundp 'magit-get-current-branch)
    (defun feebleline--git-branch-string ()
      "Return current git branch as a string, or the empty string if pwd is not in a git repo (or the git command is not found)."
      (interactive)
      (require 'esh-ext)
      (let ((git-output (magit-get-current-branch)))
        (if (> (length git-output) 0)
            git-output
          "(no branch)")))

  (message "Warning: Feebleline couldn't find magit! Using hacky version to obtain git branch.")
  (require 'esh-ext)
  (defun feebleline--git-branch-string ()
    "Return current git branch as a string, or the empty string if pwd is not in a git repo (or the git command is not found)."
    (interactive)
    (when (and (eshell-search-path "git")
               (locate-dominating-file default-directory ".git"))
      (let ((git-output (shell-command-to-string (concat "cd " default-directory " && git branch | grep '\\*' | sed -e 's/^\\* //'"))))
        (if (> (length git-output) 0)
            (substring git-output 0 -1)
          "(no branch)")))))

(defvar feebleline--home-dir nil)

(setq
 feebleline-mode-line-text
 '(
   ("%s" ((if feebleline-show-time (format-time-string "[%H:%M:%S] ") ""))
    (face feebleline-time-face))
   ("%s"
    ((if feebleline-show-linenum
         (format "%5s:%-2s" (format-mode-line "%l") (current-column))
       ""))
    (face feebleline-linum-face))
   (" %s" ((if (and feebleline-show-directory (buffer-file-name))
               (replace-regexp-in-string
                feebleline--home-dir "~"
                (file-name-directory (buffer-file-name)))
             ""))
    (face feebleline-dir-face))
   ("%s" ((if (buffer-file-name) (file-name-nondirectory (buffer-file-name))
            (buffer-name)))
    (face feebleline-bufname-face))
   ("%s" ((if (and (buffer-file-name) (buffer-modified-p)) "*"
            "" ))
    (face feebleline-asterisk-face))
   ("%s" ((if feebleline-show-git-branch (concat " : " (feebleline--git-branch-string))
            ""))
    (face feebleline-git-branch-face))
   ("%s" ((if feebleline-show-previous-buffer (concat " | " (feebleline-previous-buffer-name))
            ""))
   (face feebleline-previous-buffer-face)))
 )


(defun feebleline-default-settings-on ()
  "Some default settings that works well with feebleline."
  (setq window-divider-default-bottom-width 1
        window-divider-default-places (quote bottom-only))
  (window-divider-mode t)
  (setq-default mode-line-format nil)
  (setq mode-line-format nil))

(defun feebleline-legacy-settings-on ()
  "Some default settings for EMACS < 25."
  (set-face-attribute 'mode-line nil :height 0.1))

(defvar feebleline/timer)
(defvar feebleline/mode-line-format-previous)

;;;###autoload
(define-minor-mode feebleline-mode
  "Replace modeline with a slimmer proxy."
  :require 'feebleline
  :global t
  (if feebleline-mode
      ;; Activation:
      (progn
        (setq feebleline--home-dir (expand-file-name "~"))
        (setq feebleline/mode-line-format-previous mode-line-format)
        (setq feebleline/timer
              (run-with-timer 0 0.5 'feebleline-mode-line-proxy-fn))
        (if feebleline-use-legacy-settings (feebleline-legacy-settings-on)
          (feebleline-default-settings-on))
        ;; (ad-activate 'handle-switch-frame)
        (add-hook 'focus-in-hook 'feebleline-mode-line-proxy-fn))

    ;; Deactivation:
    (set-face-attribute 'mode-line nil :height 1.0)
    (setq-default mode-line-format feebleline/mode-line-format-previous)
    (setq mode-line-format feebleline/mode-line-format-previous)
    (cancel-timer feebleline/timer)
    ;; (ad-deactivate 'handle-switch-frame)
    (remove-hook 'focus-in-hook 'feebleline-mode-line-proxy-fn)
    (force-mode-line-update)
    (redraw-display)
    (with-current-buffer " *Minibuf-0*"
      (erase-buffer))))

(defun feebleline--mode-line-part (part)
  "Return a PART (an element) of `feebleline-mode-line-text` as a propertized string."
  (let ((text (apply #'format (append (list (car part))
                                      (mapcar #'eval (cadr part)))))
        (props (elt part 2)))
    (when props
      (add-text-properties 0 (length text) props text))
    text))

(defvar feebleline-placeholder)
(defun feebleline-write-buffer-name-maybe ()
  "Replace echo-area message with mode-line proxy."
  (progn (setq feebleline-placeholder (mapconcat #'feebleline--mode-line-part
                                                 feebleline-mode-line-text ""))
         (with-current-buffer " *Minibuf-0*"
           (erase-buffer)
           (insert feebleline-placeholder))))

(defun feebleline-mode-line-proxy-fn ()
  "Put a mode-line proxy in the echo area *if* echo area is empty."
  (unless (current-message)
    (feebleline-write-buffer-name-maybe)))

;; (defadvice handle-switch-frame (after switch-frame-message-name)
;;   "Get the modeline proxy to work with i3 switch focus."
;;   (feebleline-write-buffer-name-maybe)
;;   ad-do-it
;;   (feebleline-write-buffer-name-maybe))

(provide 'feebleline)
;;; feebleline.el ends here