-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvim-tab-bar.el
314 lines (285 loc) · 12.4 KB
/
vim-tab-bar.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
;;; vim-tab-bar.el --- Vim-like tab bar -*- lexical-binding: t; -*-
;; Copyright (C) 2024-2025 James Cherti | https://www.jamescherti.com/contact/
;; Author: James Cherti
;; Version: 1.0.7
;; URL: https://github.com/jamescherti/vim-tab-bar.el
;; Keywords: frames
;; Package-Requires: ((emacs "28.1"))
;; SPDX-License-Identifier: GPL-3.0-or-later
;; This file 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 2, or (at your option)
;; any later version.
;; This file 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; The `vim-tab-bar.el` package enhances Emacs' built-in tab-bar, giving it a
;; style similar to Vim's tabbed browsing interface. It also ensures that the
;; tab-bar's appearance aligns with the current theme's overall color scheme.
;;
;; Features:
;; - Vim-like tab bar: Makes the Emacs `tab-bar` look in a manner reminiscent
;; of Vim's tabbed browsing interface.
;; - Theme Compatibility: Automatically applies Vim-like color themes to tab
;; bars, responding dynamically to theme changes in Emacs.
;; - Group Formatting: Optionally display and format tab groups.
;;
;; Installation:
;; -------------
;; (use-package vim-tab-bar
;; :ensure t
;; :commands vim-tab-bar-mode
;; :hook
;; (after-init . vim-tab-bar-mode))
;;
;; Links:
;; ------
;; - vim-tab-bar.el @GitHub (Screenshots, usage, etc.):
;; https://github.com/jamescherti/vim-tab-bar.el
;;; Code:
(require 'tab-bar)
(defgroup vim-tab-bar nil
"Non-nil if vim-tab-bar mode mode is enabled."
:group 'vim-tab-bar
:prefix "vim-tab-bar-"
:link '(url-link "https://github.com/jamescherti/vim-tab-bar.el"))
(defcustom vim-tab-bar-show-groups nil
"Show groups in the tab-bar."
:type 'boolean
:group 'vim-tab-bar)
(defun vim-tab-bar--default-format-tab-name (name)
"Return NAME surrounded by spaces."
(concat " " name " "))
(defun vim-tab-bar--default-format-group-name (name)
"Return NAME surrounded by spaces."
(concat " " name " "))
(defcustom vim-tab-bar-update-tab-name-function
#'vim-tab-bar--default-format-tab-name
"Function to format the name of a tab.
The function should take a string as its sole argument and return a string."
:type 'function
:group 'vim-tab-bar)
(defcustom vim-tab-bar-update-group-name-function
#'vim-tab-bar--default-format-group-name
"Function to format the name of a tab group.
The function should take a string as its sole argument and return a string."
:type 'function
:group 'vim-tab-bar)
(defvar vim-tab-bar--after-load-theme-hook nil
"Hook run after `load-theme' to update the tab-bar faces.")
(defun vim-tab-bar--name-format-function (tab i)
"Format a TAB name of the tab index I like Vim."
(let ((current-p (eq (car tab) 'current-tab)))
(propertize
(funcall vim-tab-bar-update-tab-name-function
(concat
(if tab-bar-tab-hints (format "%d " i) "")
(alist-get 'name tab)
(or (and tab-bar-close-button-show
(not (eq tab-bar-close-button-show
(if current-p 'non-selected 'selected)))
tab-bar-close-button)
"")))
'face (funcall tab-bar-tab-face-function tab))))
(defun vim-tab-bar--group-format-function (tab i &optional current-p)
"Format a TAB group like Vim.
This function spaces around the group name. Index I is included when
`tab-bar-tab-hints' is enabled and CURRENT-P is nil, indicating the tab is not
the current group."
(propertize
(funcall vim-tab-bar-update-group-name-function
(concat
(if (and tab-bar-tab-hints (not current-p))
(format "%d " i)
"")
(funcall tab-bar-tab-group-function tab)))
'face (if current-p 'tab-bar-tab-group-current
'tab-bar-tab-group-inactive)))
(defun vim-tab-bar--apply (&optional frame)
"Apply Vim-like color themes to Emacs tab bars.
FRAME is the frame."
(unless frame
(setq frame (selected-frame)))
(let* ((color-fallback-light (face-attribute 'default :foreground))
(fallback-color-dark (face-attribute 'default :background))
(bg-default (or (face-attribute 'default :background)
color-fallback-light))
(fg-default (or (face-attribute 'default :foreground)
fallback-color-dark))
(bg-modeline-inactive (or (face-attribute
'mode-line-inactive :background)
fallback-color-dark))
(fg-modeline-inactive (or (face-attribute
'mode-line-inactive :foreground)
color-fallback-light))
(bg-tab-inactive bg-modeline-inactive)
(fg-tab-inactive fg-modeline-inactive)
(fg-tab-active fg-default)
(bg-tab-active bg-default))
(setq tab-bar-tab-name-format-function
#'vim-tab-bar--name-format-function)
(setq tab-bar-tab-group-format-function
#'vim-tab-bar--group-format-function)
(if vim-tab-bar-show-groups
(setq tab-bar-format '(tab-bar-format-tabs-groups tab-bar-separator))
(setq tab-bar-format '(tab-bar-format-tabs tab-bar-separator)))
(with-suppressed-warnings ((obsolete tab-bar-new-button-show))
(setq tab-bar-new-button-show nil)) ; Obsolete as of Emacs 28.1
(setq tab-bar-separator "\u200B") ; Zero width space to fix color bleeding
(setq tab-bar-tab-hints nil) ; Tab numbers of the left of the label
(setq tab-bar-close-button-show nil)
(setq tab-bar-auto-width nil)
;; The tab bar's appearance
(set-face-attribute 'tab-bar frame
:background bg-tab-inactive
:foreground fg-tab-inactive
:inverse-video 'unspecified
:inherit 'unspecified
:family 'unspecified
:foundry 'unspecified
:width 'unspecified
:height 'unspecified
:weight 'unspecified
:slant 'unspecified
:underline 'unspecified
:overline 'unspecified
:extend 'unspecified
:strike-through 'unspecified
:stipple 'unspecified)
;; Inactive tabs
(set-face-attribute 'tab-bar-tab-inactive frame
:background bg-tab-inactive
:foreground fg-tab-inactive
:inverse-video 'unspecified
:inherit 'unspecified
:family 'unspecified
:foundry 'unspecified
:width 'unspecified
:height 'unspecified
:weight 'unspecified
:slant 'unspecified
:underline 'unspecified
:overline 'unspecified
:extend 'unspecified
:strike-through 'unspecified
:stipple 'unspecified)
;; Active tab
(set-face-attribute 'tab-bar-tab frame
:background bg-tab-active
:foreground fg-tab-active
:inverse-video 'unspecified
:inherit 'unspecified
:family 'unspecified
:foundry 'unspecified
:width 'unspecified
:height 'unspecified
:weight 'unspecified
:slant 'unspecified
:underline 'unspecified
:overline 'unspecified
:extend 'unspecified
:strike-through 'unspecified
:stipple 'unspecified)
;; The tab bar's ungrouped appearance
(set-face-attribute 'tab-bar-tab-ungrouped frame
:background bg-tab-inactive
:foreground fg-tab-inactive
:inverse-video 'unspecified
:inherit 'unspecified
:family 'unspecified
:foundry 'unspecified
:width 'unspecified
:height 'unspecified
:weight 'unspecified
:slant 'unspecified
:underline 'unspecified
:overline 'unspecified
:extend 'unspecified
:strike-through 'unspecified
:stipple 'unspecified)
;; Inactive tab groups
(set-face-attribute 'tab-bar-tab-group-inactive frame
:background bg-tab-inactive
:foreground fg-tab-inactive
:inverse-video 'unspecified
:inherit 'unspecified
:family 'unspecified
:foundry 'unspecified
:width 'unspecified
:height 'unspecified
:weight 'unspecified
:slant 'unspecified
:underline 'unspecified
:overline 'unspecified
:extend 'unspecified
:strike-through 'unspecified
:stipple 'unspecified)
;; Current tab group
(set-face-attribute 'tab-bar-tab-group-current frame
:background bg-tab-inactive
:foreground fg-tab-active
:inverse-video 'unspecified
:inherit 'unspecified
:family 'unspecified
:foundry 'unspecified
:width 'unspecified
:height 'unspecified
:weight 'unspecified
:slant 'unspecified
:underline 'unspecified
:overline 'unspecified
:extend 'unspecified
:strike-through 'unspecified
:stipple 'unspecified)
(when (and (display-graphic-p)
(not (daemonp)))
(set-face-attribute
'tab-bar frame
:box `(:line-width 3 :color ,bg-tab-inactive :style nil))
(set-face-attribute
'tab-bar-tab-inactive frame
:box `(:line-width 3 :color ,bg-tab-inactive :style nil))
(set-face-attribute
'tab-bar-tab frame
:box `(:line-width 3 :color ,bg-tab-active :style nil))
(set-face-attribute
'tab-bar-tab-ungrouped frame
:box `(:line-width 3 :color ,bg-tab-inactive :style nil))
(set-face-attribute
'tab-bar-tab-group-inactive frame
:box `(:line-width 3 :color ,bg-tab-inactive :style nil))
(set-face-attribute
'tab-bar-tab-group-current frame
:box `(:line-width 3 :color ,bg-tab-inactive :style nil)))))
(defun vim-tab-bar--run-after-load-theme-hook (&rest _args)
"Run the hooks that are in `vim-tab-bar--after-load-theme-hook'."
(run-hooks 'vim-tab-bar--after-load-theme-hook))
;;;###autoload
(define-minor-mode vim-tab-bar-mode
"Toggle `vim-tab-bar-mode'."
:global t
:lighter " VimTB"
:group 'vim-tab-bar
(if vim-tab-bar-mode
(progn
(vim-tab-bar--apply)
(advice-add 'load-theme :after
#'vim-tab-bar--run-after-load-theme-hook)
;; Hooks
(add-hook 'after-make-frame-functions #'vim-tab-bar--apply)
(add-hook 'vim-tab-bar--after-load-theme-hook #'vim-tab-bar--apply)
(tab-bar-mode 1))
(remove-hook 'after-make-frame-functions #'vim-tab-bar--apply)
(advice-remove 'load-theme #'vim-tab-bar--run-after-load-theme-hook)
(tab-bar-mode -1)
;; (let ((current-theme (car custom-enabled-themes)))
;; (when current-theme
;; (disable-theme current-theme)
;; (load-theme current-theme t)))
))
(provide 'vim-tab-bar)
;;; vim-tab-bar.el ends here