-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjcs-echobar.el
174 lines (141 loc) · 5.24 KB
/
jcs-echobar.el
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
;;; jcs-echobar.el --- An echo-bar for jcs-emacs -*- lexical-binding: t; -*-
;; Copyright (C) 2023-2025 Shen, Jen-Chieh
;; Author: Shen, Jen-Chieh <[email protected]>
;; Maintainer: Shen, Jen-Chieh <[email protected]>
;; URL: https://github.com/jcs-emacs/jcs-echobar
;; Version: 0.1.0
;; Package-Requires: ((emacs "29.1") (echo-bar "1.0.0") (indent-control "0.1.0") (show-eol "0.1.0") (keycast "1.2.0"))
;; Keywords: faces echo-bar
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; An echo-bar for jcs-emacs
;;
;;; Code:
(require 'echo-bar)
(require 'indent-control)
(require 'show-eol)
(require 'keycast)
(defgroup jcs-echobar nil
"An echo-bar for jcs-emacs."
:prefix "jcs-echobar-"
:group 'faces
:link '(url-link :tag "Github" "https://github.com/jcs-emacs/jcs-echobar"))
(defcustom jcs-echobar-render
`((:eval (jcs-echobar--render-keycast))
(:eval (jcs-echobar--render-spaces-tabs-size))
(:eval (jcs-echobar--render-coding-system))
(:eval (jcs-echobar--render-eol))
(:eval (jcs-echobar--render-time)))
"List of item to render in echo-bar."
:type 'list
:group 'jcs-echobar)
(defcustom jcs-echobar-keycast-format "%K%C%R "
"The keycast format spec."
:type 'string
:group 'jcs-echobar)
;;
;; (@* "Entry" )
;;
(defvar jcs-echobar--default-function nil
"Default modeline value to revert back.")
(defun jcs-echobar--enable ()
"Enable function `jcs-echobar-mode'."
(progn ; keycast
(add-hook 'post-command-hook #'keycast--update t)
(add-hook 'minibuffer-exit-hook #'keycast--minibuffer-exit t)
(advice-add 'keycast--update :after #'jcs-echobar--keycast-update))
(add-hook 'window-size-change-functions #'jcs-echobar--window-resize)
(jcs-echobar--window-resize) ; call it manually once
(setq jcs-echobar--default-function echo-bar-function)
(setq echo-bar-function #'jcs-echobar-render)
(echo-bar-mode 1))
(defun jcs-echobar--disable ()
"Disable function `jcs-echobar-mode'."
(progn ; keycast
(remove-hook 'post-command-hook #'keycast--update)
(remove-hook 'minibuffer-exit-hook #'keycast--minibuffer-exit)
(advice-remove 'keycast--update #'jcs-echobar--keycast-update))
(remove-hook 'window-size-change-functions #'jcs-echobar--window-resize)
(setq echo-bar-function jcs-echobar--default-function)
(echo-bar-mode -1))
;;;###autoload
(define-minor-mode jcs-echobar-mode
"Minor mode `jcs-echobar-mode'."
:global t
:require 'jcs-echobar-mode
:group 'jcs-echobar
:lighter nil
(if jcs-echobar-mode (jcs-echobar--enable) (jcs-echobar--disable)))
;;
;; (@* "Util" )
;;
(defun jcs-echobar--str-len (str)
"Calculate STR in pixel width."
(let ((width (frame-char-width))
(len (string-pixel-width str)))
(+ (/ len width)
(if (zerop (% len width)) 0 1)))) ; add one if exceeed
(defun jcs-echobar--buffer-spaces-or-tabs ()
"Check if buffer using spaces or tabs."
(if indent-tabs-mode "TAB" "SPC"))
;;
;; (@* "Core" )
;;
(defvar jcs-echobar--render nil)
(defun jcs-echobar--render-width ()
"Return the render width."
(let ((full-width (window-width (minibuffer-window))))
(* full-width 0.5)))
(defun jcs-echobar--window-resize (&rest _)
"Window resize hook."
(setq jcs-echobar--render nil) ; reset
(let ((current-width 0))
(dolist (item jcs-echobar-render)
(let* ((format (format-mode-line item))
(width (jcs-echobar--str-len format))
(new-width (+ current-width width)))
(when (<= new-width (jcs-echobar--render-width))
(setq current-width new-width)
(push item jcs-echobar--render)))))
(setq jcs-echobar--render (reverse jcs-echobar--render)))
(defun jcs-echobar-render (&rest _)
"Render the echo-bar."
(string-trim (mapconcat #'format-mode-line jcs-echobar--render "")))
(defun jcs-echobar--keycast-update (&rest _)
"Exection after `keycast-update' function."
(when (bound-and-true-p jcs-echobar-mode)
(echo-bar-update)))
;;
;; (@* "Plugins" )
;;
(defun jcs-echobar--render-spaces-tabs-size ()
"Render spaces/tabs size."
(format "%s: %s "
(jcs-echobar--buffer-spaces-or-tabs)
(indent-control-get-indent-level-by-mode)))
(defun jcs-echobar--render-coding-system ()
"Render buffer coding system."
(format "%s " buffer-file-coding-system))
(defun jcs-echobar--render-eol ()
"Render line-endings."
(format "%s " (show-eol-get-eol-mark-by-system)))
(defun jcs-echobar--render-time ()
"Render time."
(format-time-string "%b %d, %Y %H:%M:%S"))
(defun jcs-echobar--render-keycast ()
"Render `keycast'."
(when (featurep 'keycast)
(keycast--format jcs-echobar-keycast-format)))
(provide 'jcs-echobar)
;;; jcs-echobar.el ends here