mirrored from git://git.sv.gnu.org/emacs.git
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
tab-bar.el
3011 lines (2690 loc) · 123 KB
/
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; tab-bar.el --- frame-local tabs with named persistent window configurations -*- lexical-binding: t; -*-
;; Copyright (C) 2019-2024 Free Software Foundation, Inc.
;; Author: Juri Linkov <[email protected]>
;; Keywords: frames tabs
;; Maintainer: [email protected]
;; This file is part of GNU Emacs.
;; GNU Emacs 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.
;; GNU Emacs 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:
;; Provides `tab-bar-mode' to control display of the tab bar and
;; bindings for the global tab bar.
;; The normal global binding for [tab-bar] (below) uses the value of
;; `tab-bar-map' as the actual keymap to define the tab bar.
;;; Code:
(eval-when-compile (require 'icons))
(eval-when-compile (require 'cl-lib))
(defgroup tab-bar nil
"Frame-local tabs."
:group 'convenience
:version "27.1")
(defgroup tab-bar-faces '((tab-bar custom-face)) ; tab-bar is defined in faces.el
"Faces used in the tab bar."
:group 'tab-bar
:group 'faces
:version "27.1")
(defface tab-bar-tab
'((default
:inherit tab-bar)
(((class color) (min-colors 88))
:box (:line-width 1 :style released-button))
(t
:inverse-video nil))
"Tab bar face for selected tab."
:version "27.1"
:group 'tab-bar-faces)
(defface tab-bar-tab-inactive
'((default
:inherit tab-bar-tab)
(((class color) (min-colors 88))
:background "grey75")
(t
:inverse-video t))
"Tab bar face for non-selected tab."
:version "27.1"
:group 'tab-bar-faces)
(defface tab-bar-tab-group-current
'((t :inherit tab-bar-tab :box nil :weight bold))
"Tab bar face for current group tab."
:version "28.1"
:group 'tab-bar-faces)
(defface tab-bar-tab-group-inactive
'((t :inherit (shadow tab-bar-tab-inactive)))
"Tab bar face for inactive group tab."
:version "28.1"
:group 'tab-bar-faces)
(defface tab-bar-tab-ungrouped
'((t :inherit (shadow tab-bar-tab-inactive)))
"Tab bar face for ungrouped tab when tab groups are used."
:version "28.1"
:group 'tab-bar-faces)
(defcustom tab-bar-select-tab-modifiers '()
"List of modifier keys for selecting tab-bar tabs by their numbers.
Possible modifier keys are `control', `meta', `shift', `hyper', `super' and
`alt'. Pressing one of the modifiers in the list and a digit selects the
tab whose number equals the digit (see `tab-bar-select-tab').
The digit 9 selects the last (rightmost) tab (see `tab-last').
The digit 0 selects the most recently visited tab (see `tab-recent').
For easier selection of tabs by their numbers, consider customizing
`tab-bar-tab-hints', which will show tab numbers alongside the tab name."
:type '(set :tag "Tab selection modifier keys"
(const control)
(const meta)
(const shift)
(const hyper)
(const super)
(const alt))
:initialize #'custom-initialize-default
:set (lambda (sym val)
(when tab-bar-mode
(tab-bar--undefine-keys))
(set-default sym val)
;; Reenable the tab-bar with new keybindings
(when tab-bar-mode
(tab-bar--define-keys)))
:group 'tab-bar
:version "27.1")
(defun tab-bar--define-keys ()
"Install key bindings to switch between tabs if so configured."
(when tab-bar-select-tab-modifiers
(define-key tab-bar-mode-map
(vector (append tab-bar-select-tab-modifiers (list ?0)))
#'tab-recent)
(dotimes (i 8)
(define-key tab-bar-mode-map
(vector (append tab-bar-select-tab-modifiers
(list (+ i 1 ?0))))
#'tab-bar-select-tab))
(define-key tab-bar-mode-map
(vector (append tab-bar-select-tab-modifiers (list ?9)))
#'tab-last))
;; Replace default value with a condition that supports displaying
;; global-mode-string in the tab bar instead of the mode line.
(when (and (memq 'tab-bar-format-global tab-bar-format)
(member '(global-mode-string ("" global-mode-string))
mode-line-misc-info))
(setf (alist-get 'global-mode-string mode-line-misc-info)
'(("" (:eval (if (and tab-bar-mode
(memq 'tab-bar-format-global
tab-bar-format))
"" global-mode-string)))))))
(defun tab-bar--undefine-keys ()
"Uninstall key bindings previously bound by `tab-bar--define-keys'."
(when tab-bar-select-tab-modifiers
(define-key tab-bar-mode-map
(vector (append tab-bar-select-tab-modifiers (list ?0)))
nil t)
(dotimes (i 8)
(define-key tab-bar-mode-map
(vector (append tab-bar-select-tab-modifiers
(list (+ i 1 ?0))))
nil t))
(define-key tab-bar-mode-map
(vector (append tab-bar-select-tab-modifiers (list ?9)))
nil t)))
(defun tab-bar--load-buttons ()
"Load the icons for the tab buttons."
(require 'icons)
(declare-function icon-string "icons" (name))
(declare-function iconp "icons" (object))
(declare-function icons--register "icons")
(unless (iconp 'tab-bar-new)
(define-icon tab-bar-new nil
`((image "symbols/plus_16.svg" "tabs/new.xpm"
:face shadow
:height (1 . em)
:margin ,tab-bar-button-margin
:ascent center)
;; (emoji "➕")
;; (symbol "+")
(text " + "))
"Icon for creating a new tab."
:version "29.1"
:help-echo "New tab"))
(setq tab-bar-new-button (icon-string 'tab-bar-new))
(unless (iconp 'tab-bar-close)
(define-icon tab-bar-close nil
`((image "symbols/cross_16.svg" "tabs/close.xpm"
:face shadow
:height (1 . em)
:margin ,tab-bar-button-margin
:ascent center)
;; (emoji " ❌")
;; (symbol "✕") ;; "ⓧ"
(text " x"))
"Icon for closing the clicked tab."
:version "29.1"
:help-echo "Click to close tab"))
(setq tab-bar-close-button (propertize (icon-string 'tab-bar-close)
'close-tab t))
(unless (iconp 'tab-bar-menu-bar)
(define-icon tab-bar-menu-bar nil
`((image "symbols/menu_16.svg"
:height (1 . em)
:margin ,tab-bar-button-margin
:ascent center)
;; (emoji "🍔")
(symbol "☰")
(text "Menu" :face tab-bar-tab-inactive))
"Icon for the menu bar."
:version "29.1"
:help-echo "Menu bar"))
(setq tab-bar-menu-bar-button (icon-string 'tab-bar-menu-bar)))
(defun tab-bar--tab-bar-lines-for-frame (frame)
"Determine and return the value of `tab-bar-lines' for FRAME.
Return 0 if `tab-bar-mode' is not enabled. Otherwise return
either 1 or 0 depending on the value of the customizable variable
`tab-bar-show', which see."
(cond
((not tab-bar-mode) 0)
((not tab-bar-show) 0)
((eq tab-bar-show t) 1)
((natnump tab-bar-show)
(if (> (length (funcall tab-bar-tabs-function frame)) tab-bar-show) 1 0))))
(defun tab-bar--update-tab-bar-lines (&optional frames)
"Update the `tab-bar-lines' frame parameter in FRAMES.
If the optional parameter FRAMES is omitted, update only
the currently selected frame. If it is t, update all frames
as well as the default for new frames. Otherwise FRAMES should be
a list of frames to update."
(let ((frame-lst (cond ((null frames)
(list (selected-frame)))
((eq frames t)
(frame-list))
(t frames))))
;; Loop over all frames and update `tab-bar-lines'
(dolist (frame frame-lst)
(unless (or (frame-parameter frame 'tab-bar-lines-keep-state)
(and (eq auto-resize-tab-bars 'grow-only)
(> (frame-parameter frame 'tab-bar-lines) 1)))
(set-frame-parameter frame 'tab-bar-lines
(tab-bar--tab-bar-lines-for-frame frame)))))
;; Update `default-frame-alist'
(when (eq frames t)
(setq default-frame-alist
(cons (cons 'tab-bar-lines
(if (and tab-bar-mode (eq tab-bar-show t)) 1 0))
(assq-delete-all 'tab-bar-lines default-frame-alist)))))
(defun tab-bar-mode--tab-key-bind (map key binding)
;; Don't override user customized global key bindings
(define-key map key
`(menu-item "" ,binding
:filter ,(lambda (cmd) (unless (global-key-binding key) cmd)))))
(defvar tab-bar-mode-map
(let ((map (make-sparse-keymap)))
(tab-bar-mode--tab-key-bind map [(control tab)] #'tab-next)
(tab-bar-mode--tab-key-bind map [(control shift tab)] #'tab-previous)
(tab-bar-mode--tab-key-bind map [(control shift iso-lefttab)] #'tab-previous)
map)
"Tab Bar mode map.")
(define-minor-mode tab-bar-mode
"Toggle the tab bar in all graphical frames (Tab Bar mode).
When this mode is enabled, Emacs displays a tab bar on top of each frame.
The tab bar is a row of tabs -- buttons that you can click
to switch the frame between different window configurations.
See `current-window-configuration' for more about window configurations.
To add a button (which can then record one more window configuration),
click on the \"+\" button. Clicking on the \"x\" icon of a button
deletes the button."
:global t
;; It's defined in C/cus-start, this stops the d-m-m macro defining it again.
:variable tab-bar-mode
;; Recalculate `tab-bar-lines' for all frames
(tab-bar--update-tab-bar-lines t)
(when tab-bar-mode
(tab-bar--load-buttons))
(if tab-bar-mode
(tab-bar--define-keys)
(tab-bar--undefine-keys)))
;;; Key bindings
(defun tab-bar--key-to-number (key)
"Return the tab number represented by KEY.
If KEY is a symbol `tab-N', where N is a tab number, the value is N.
If KEY is \\='current-tab, the value is nil.
For any other value of KEY, the value is t."
(cond
((null key) t)
((eq key 'current-tab) nil)
((let ((key-name (format "%S" key)))
(when (string-prefix-p "tab-" key-name)
(string-to-number (string-replace "tab-" "" key-name)))))
(t t)))
(defvar tab-bar--dragging-in-progress)
(defun tab-bar--event-to-item (posn)
"Extract extra info from the mouse event at position POSN.
It returns a list of the form (KEY KEY-BINDING CLOSE-P), where:
KEY is a symbol representing a tab, such as \\='tab-1 or \\='current-tab;
KEY-BINDING is the binding of KEY;
CLOSE-P is non-nil if the mouse event was a click on the close button \"x\",
nil otherwise."
(setq tab-bar--dragging-in-progress nil)
(if (posn-window posn)
(let* ((caption (car (posn-string posn)))
(menu-item (when caption
(get-text-property 0 'menu-item caption))))
(when (equal menu-item '(global ignore nil))
(setf (nth 1 menu-item)
(key-binding (vector 'tab-bar last-nonmenu-event) t)))
menu-item)
;; Text-mode emulation of switching tabs on the tab bar.
;; This code is used when you click the mouse in the tab bar
;; on a console which has no window system but does have a mouse.
(let* ((x-position (car (posn-x-y posn)))
(keymap (lookup-key (cons 'keymap (nreverse (current-active-maps)))
[tab-bar]))
(column 0))
(when x-position
(catch 'done
(map-keymap
(lambda (key binding)
(when (eq (car-safe binding) 'menu-item)
(when (> (+ column (length (nth 1 binding))) x-position)
(throw 'done (list key (nth 2 binding)
(get-text-property
(- x-position column)
'close-tab (nth 1 binding)))))
(setq column (+ column (length (nth 1 binding))))))
keymap))))))
(defun tab-bar-mouse-down-1 (event)
"Select the tab at mouse click, or add a new tab on the tab bar.
Whether this command adds a new tab or selects an existing tab
depends on whether the click is on the \"+\" button or on an
existing tab."
(interactive "e")
(let* ((item (tab-bar--event-to-item (event-start event)))
(tab-number (tab-bar--key-to-number (nth 0 item))))
(setq tab-bar--dragging-in-progress t)
;; Don't close the tab when clicked on the close button. Also
;; don't add new tab on down-mouse. Let `tab-bar-mouse-1' do this.
(unless (or (memq (car item) '(add-tab history-back history-forward global))
(nth 2 item))
(if (functionp (nth 1 item))
(call-interactively (nth 1 item))
(unless (eq tab-number t)
(tab-bar-select-tab tab-number))))))
(defun tab-bar-mouse-1 (event)
"Close the tab whose \"x\" close button you click.
See also `tab-bar-mouse-close-tab', which closes the tab
regardless of where you click on it. Also add a new tab."
(interactive "e")
(let* ((item (tab-bar--event-to-item (event-start event)))
(tab-number (tab-bar--key-to-number (nth 0 item))))
(cond
((and (memq (car item) '(add-tab history-back history-forward global))
(not (eq (nth 1 item) 'tab-bar-mouse-1))
(functionp (nth 1 item)))
(call-interactively (nth 1 item)))
((and (nth 2 item) (not (eq tab-number t)))
(tab-bar-close-tab tab-number)))))
(defun tab-bar-mouse-close-tab (event)
"Close the tab you click on.
This is in contrast with `tab-bar-mouse-1' that closes a tab
only when you click on its \"x\" close button."
(interactive "e")
(let* ((item (tab-bar--event-to-item (event-start event)))
(tab-number (tab-bar--key-to-number (nth 0 item))))
(unless (eq tab-number t)
(tab-bar-close-tab tab-number))))
(defun tab-bar-mouse-context-menu (event &optional posn)
"Pop up the context menu for the tab on which you click.
EVENT is a mouse or touch screen event. POSN is nil or the
position of EVENT."
(interactive "e")
(let* ((item (tab-bar--event-to-item (or posn (event-start event))))
(tab-number (tab-bar--key-to-number (nth 0 item)))
(menu (make-sparse-keymap (propertize "Context Menu" 'hide t))))
(cond
((eq tab-number t)
(define-key-after menu [new-tab]
'(menu-item "New tab" tab-bar-new-tab
:help "Create a new tab"))
(when tab-bar-closed-tabs
(define-key-after menu [undo-close]
'(menu-item "Reopen closed tab" tab-bar-undo-close-tab
:help "Undo closing the tab"))))
(t
(define-key-after menu [duplicate-tab]
`(menu-item "Duplicate" (lambda () (interactive)
(tab-bar-duplicate-tab
nil ,tab-number))
:help "Clone the tab"))
(define-key-after menu [detach-tab]
`(menu-item "Detach" (lambda () (interactive)
(tab-bar-detach-tab
,tab-number))
:help "Move the tab to new frame"))
(define-key-after menu [close]
`(menu-item "Close" (lambda () (interactive)
(tab-bar-close-tab ,tab-number))
:help "Close the tab"))
(define-key-after menu [close-other]
`(menu-item "Close other tabs"
(lambda () (interactive)
(tab-bar-close-other-tabs ,tab-number))
:help "Close all other tabs"))))
(popup-menu menu event)))
(defun tab-bar-mouse-move-tab (event)
"Move a tab to a different position on the tab bar.
This command should be bound to a drag event. It moves the tab
at the mouse-down event to the position at mouse-up event."
(interactive "e")
(setq tab-bar--dragging-in-progress nil)
(let ((from (tab-bar--key-to-number
(nth 0 (tab-bar--event-to-item
(event-start event)))))
(to (tab-bar--key-to-number
(nth 0 (tab-bar--event-to-item
(event-end event))))))
(unless (or (eq from to) (eq from t) (eq to t))
(tab-bar-move-tab-to
(if (null to) (1+ (tab-bar--current-tab-index)) to) from))))
;;; Tab bar touchscreen support.
(declare-function touch-screen-track-tap "touch-screen.el")
(defun tab-bar-handle-timeout ()
"Handle a touch-screen timeout on the tab bar.
Beep, then throw to `context-menu' and return."
(beep)
(throw 'context-menu 'context-menu))
(defvar touch-screen-delay)
(defun tab-bar-touchscreen-begin (event)
"Handle a touchscreen begin EVENT on the tab bar.
Determine where the touch was made. If it was made on a tab
itself, start a timer set to go off after a certain amount of
time, and wait for the touch point to be released, and either
display a context menu or select a tab as appropriate.
Otherwise, if it was made on a button, close or create a tab as
appropriate."
(interactive "e")
(let* ((posn (cdadr event))
(item (tab-bar--event-to-item posn))
(number (tab-bar--key-to-number (car item)))
timer)
(when (eq (catch 'context-menu
(cond ((integerp number)
;; The touch began on a tab. Start a context
;; menu timer and start tracking the tap.
(unwind-protect
(progn
(setq timer (run-at-time touch-screen-delay nil
#'tab-bar-handle-timeout))
;; Now wait for the tap to complete.
(when (touch-screen-track-tap event)
;; And select the tab, or close it,
;; depending on whether or not the
;; close button was pressed.
(if (caddr item)
(tab-bar-close-tab number)
(tab-bar-select-tab number))))
;; Cancel the timer.
(cancel-timer timer)))
((and (memq (car item) '( add-tab history-back
history-forward global))
(functionp (cadr item)))
;; This is some kind of button. Wait for the
;; tap to complete and press it.
(when (touch-screen-track-tap event)
(call-interactively (cadr item))))
(t
;; The touch began on the tab bar itself.
;; Start a context menu timer and start
;; tracking the tap, but don't do anything
;; afterwards.
(unwind-protect
(progn
(setq timer (run-at-time touch-screen-delay nil
#'tab-bar-handle-timeout))
;; Now wait for the tap to complete.
(touch-screen-track-tap event))
;; Cancel the timer.
(cancel-timer timer)))))
'context-menu)
;; Display the context menu in response to a time out waiting
;; for the tap to complete.
(tab-bar-mouse-context-menu event posn))))
(defvar-keymap tab-bar-map
:doc "Keymap for the commands used on the tab bar."
"<down-mouse-1>" #'tab-bar-mouse-down-1
"<drag-mouse-1>" #'tab-bar-mouse-move-tab
"<mouse-1>" #'tab-bar-mouse-1
"<down-mouse-2>" #'tab-bar-mouse-close-tab
"<mouse-2>" #'ignore
"<down-mouse-3>" #'tab-bar-mouse-context-menu
"<mouse-4>" #'tab-previous
"<mouse-5>" #'tab-next
"<wheel-up>" #'tab-previous
"<wheel-down>" #'tab-next
"<wheel-left>" #'tab-previous
"<wheel-right>" #'tab-next
"S-<mouse-4>" #'tab-bar-move-tab-backward
"S-<mouse-5>" #'tab-bar-move-tab
"S-<wheel-up>" #'tab-bar-move-tab-backward
"S-<wheel-down>" #'tab-bar-move-tab
"S-<wheel-left>" #'tab-bar-move-tab-backward
"S-<wheel-right>" #'tab-bar-move-tab
"<touchscreen-begin>" #'tab-bar-touchscreen-begin)
(global-set-key [tab-bar]
`(menu-item ,(purecopy "tab bar") ,(make-sparse-keymap)
:filter tab-bar-make-keymap))
(defun tab-bar-make-keymap (&optional _ignore)
"Generate an actual keymap from `tab-bar-map'.
Its main job is to show tabs in the tab bar
and to bind mouse events to the commands."
(tab-bar-make-keymap-1))
(defun toggle-tab-bar-mode-from-frame (&optional arg)
"Toggle tab bar on or off, based on the status of the current frame.
Used in the Show/Hide menu, to have the toggle reflect the current frame.
See `tab-bar-mode' for more information."
(interactive (list (or current-prefix-arg 'toggle)))
(if (eq arg 'toggle)
(tab-bar-mode (if (> (frame-parameter nil 'tab-bar-lines) 0) 0 1))
(tab-bar-mode arg)))
(defun toggle-frame-tab-bar (&optional frame)
"Toggle tab bar of the selected frame.
When calling from Lisp, use the optional argument FRAME to toggle
the tab bar on that frame.
This is useful if you want to enable the tab bar individually
on each new frame when the global `tab-bar-mode' is disabled,
or if you want to disable the tab bar individually on each
new frame when the global `tab-bar-mode' is enabled, by using
(add-hook \\='after-make-frame-functions #\\='toggle-frame-tab-bar)"
(interactive)
(set-frame-parameter frame 'tab-bar-lines
(if (> (frame-parameter frame 'tab-bar-lines) 0) 0 1))
(set-frame-parameter frame 'tab-bar-lines-keep-state
(not (frame-parameter frame 'tab-bar-lines-keep-state))))
(defcustom tab-bar-show t
"Defines when to show the tab bar.
If t, the default, enable `tab-bar-mode' automatically upon using
the commands that create new window configurations (e.g., `tab-new').
If a non-negative integer, show the tab bar only if the number of
the tabs exceeds the value of this variable. In particular,
if the value is 1, hide the tab bar when it has only one tab, and
show it again once more tabs are created. A value that is a
non-negative integer also makes the tab bar appearance be different
on different frames: the tab bar can be shown on some frames and
hidden on others, depending on how many tab-bar tabs are on that
frame, and whether that number is greater than the numerical value
of this variable.
If nil, always keep the tab bar hidden. In this case it's still
possible to use persistent named window configurations by relying on
keyboard commands `tab-new', `tab-close', `tab-next', `tab-switcher', etc.
Setting this variable directly does not take effect; please customize
it (see the info node `Easy Customization'), then it will automatically
update the tab bar on all frames according to the new value.
To enable or disable the tab bar individually on each frame,
you can use the command `toggle-frame-tab-bar'."
:type '(choice (const :tag "Always" t)
(const :tag "When more than one tab" 1)
(const :tag "Never" nil))
:initialize #'custom-initialize-default
:set (lambda (sym val)
(set-default sym val)
(if val
(tab-bar-mode 1)
(tab-bar--update-tab-bar-lines t)))
:group 'tab-bar
:version "27.1")
(defcustom tab-bar-new-tab-choice t
"Defines what to show in a new tab.
If t, start a new tab with the current buffer, i.e. the buffer
that was current before calling the command that adds a new tab
(this is the same what `make-frame' does by default).
If the value is the symbol `window', then keep the selected
window as a single window on the new tab, and keep all its
window parameters except `window-atom' and `window-side'.
If the value is a string, use it as a buffer name to switch to
if such buffer exists, or switch to a buffer visiting the file or
directory that the string specifies. If the value is a function,
call it with no arguments and switch to the buffer that it returns.
If `clone', duplicate the contents of the tab that was active
before calling the command that adds a new tab."
:type '(choice (const :tag "Current buffer" t)
(const :tag "Current window" window)
(string :tag "Buffer" "*scratch*")
(directory :tag "Directory" :value "~/")
(file :tag "File" :value "~/.emacs")
(function :tag "Function")
(const :tag "Duplicate tab" clone))
:group 'tab-bar
:version "27.1")
(defcustom tab-bar-new-tab-group t
"Defines what group to assign to a new tab.
If nil, don't set a default group automatically.
If t, inherit the group name from the previous tab.
If the value is a string, use it as the group name of a new tab.
If the value is a function, call it with no arguments
to get the group name."
:type '(choice (const :tag "No automatic group" nil)
(const :tag "Inherit group from previous tab" t)
(string :tag "Fixed group name")
(function :tag "Function that returns group name"))
:group 'tab-bar
:version "28.1")
(defcustom tab-bar-new-button-show t
"If non-nil, show the \"New tab\" button in the tab bar.
When this is nil, you can create new tabs with \\[tab-new]."
:type 'boolean
:initialize #'custom-initialize-default
:set (lambda (sym val)
(set-default sym val)
(force-mode-line-update))
:group 'tab-bar
:version "27.1")
(make-obsolete-variable 'tab-bar-new-button-show 'tab-bar-format "28.1")
(defvar tab-bar-new-button " + "
"Button for creating a new tab.")
(defcustom tab-bar-close-button-show t
"Defines where to show the close tab button.
If t, show the close tab button on all tabs.
If `selected', show it only on the selected tab.
If `non-selected', show it only on non-selected tab.
If nil, don't show it at all."
:type '(choice (const :tag "On all tabs" t)
(const :tag "On selected tab" selected)
(const :tag "On non-selected tabs" non-selected)
(const :tag "None" nil))
:initialize #'custom-initialize-default
:set (lambda (sym val)
(set-default sym val)
(force-mode-line-update))
:group 'tab-bar
:version "27.1")
(defvar tab-bar-close-button
(propertize " x"
'close-tab t
:help "Click to close tab")
"Button for closing the clicked tab.")
(defvar tab-bar-back-button " < "
"Button for going back in tab history.")
(defvar tab-bar-forward-button " > "
"Button for going forward in tab history.")
(defcustom tab-bar-tab-hints nil
"Show absolute numbers on tabs in the tab bar before the tab name.
This helps to select the tab by its number using `tab-bar-select-tab'
and `tab-bar-select-tab-modifiers'."
:type 'boolean
:initialize #'custom-initialize-default
:set (lambda (sym val)
(set-default sym val)
(force-mode-line-update))
:group 'tab-bar
:version "27.1")
(defvar tab-bar-separator nil
"String that delimits tabs.")
(defun tab-bar-separator ()
"Separator between tabs."
(or tab-bar-separator (if (window-system) " " "|")))
(defcustom tab-bar-tab-name-function #'tab-bar-tab-name-current
"Function to get a tab name.
Function gets no arguments.
The choice is between displaying only the name of the current buffer
in the tab name (default), or displaying the names of all buffers
from all windows in the window configuration."
:type '(choice (const :tag "Selected window buffer"
tab-bar-tab-name-current)
(const :tag "Selected window buffer with window count"
tab-bar-tab-name-current-with-count)
(const :tag "Truncated buffer name"
tab-bar-tab-name-truncated)
(const :tag "All window buffers"
tab-bar-tab-name-all)
(function :tag "Function"))
:initialize #'custom-initialize-default
:set (lambda (sym val)
(set-default sym val)
(force-mode-line-update))
:group 'tab-bar
:version "27.1")
(defun tab-bar-tab-name-current ()
"Generate tab name from the buffer of the selected window."
;; `minibuffer-selected-window' loses its original window
;; after switching to another tab while the minibuffer was active,
;; so get the most recently used non-minibuffer window.
(buffer-name (window-buffer (or (minibuffer-selected-window)
(and (window-minibuffer-p)
(get-mru-window))))))
(defun tab-bar-tab-name-current-with-count ()
"Generate tab name from the buffer of the selected window.
Also add the number of windows in the window configuration."
(let ((count (length (window-list-1 nil 'nomini)))
(name (tab-bar-tab-name-current)))
(if (> count 1)
(format "%s (%d)" name count)
(format "%s" name))))
(defun tab-bar-tab-name-all ()
"Generate tab name from buffers of all windows."
(mapconcat #'buffer-name
(delete-dups (mapcar #'window-buffer
(window-list-1 (frame-first-window)
'nomini)))
", "))
(defcustom tab-bar-tab-name-truncated-max 20
"Maximum length of the tab name from the current buffer.
Effective when `tab-bar-tab-name-function' is customized
to `tab-bar-tab-name-truncated'."
:type 'natnum
:group 'tab-bar
:version "27.1")
(defvar tab-bar-tab-name-ellipsis t)
(defun tab-bar-tab-name-truncated ()
"Generate tab name from the buffer of the selected window.
Truncate it to the length specified by `tab-bar-tab-name-truncated-max'.
Append ellipsis `tab-bar-tab-name-ellipsis' in this case."
(let ((tab-name (tab-bar-tab-name-current)))
(if (< (length tab-name) tab-bar-tab-name-truncated-max)
tab-name
(propertize (truncate-string-to-width
tab-name tab-bar-tab-name-truncated-max nil nil
tab-bar-tab-name-ellipsis)
'help-echo tab-name))))
(defvar tab-bar-tabs-function #'tab-bar-tabs
"Function to get a list of tabs to display in the tab bar.
This function should have one optional argument FRAME,
defaulting to the selected frame when nil.
It should return a list of alists with parameters
that include at least the element (name . TAB-NAME).
For example, \\='((tab (name . \"Tab 1\")) (current-tab (name . \"Tab 2\")))
By default, use function `tab-bar-tabs'.")
(defun tab-bar-tabs (&optional frame)
"Return a list of tabs belonging to the FRAME.
Ensure the frame parameter `tabs' is pre-populated.
Update the current tab name when it exists.
Return its existing value or a new value."
(let ((tabs (frame-parameter frame 'tabs)))
(if tabs
(let* ((current-tab (tab-bar--current-tab-find tabs))
(current-tab-name (assq 'name current-tab))
(current-tab-explicit-name (assq 'explicit-name current-tab)))
(when (and current-tab-name
current-tab-explicit-name
(not (cdr current-tab-explicit-name)))
(setf (cdr current-tab-name)
(funcall tab-bar-tab-name-function))))
;; Create default tabs
(setq tabs (list (tab-bar--current-tab-make)))
(tab-bar-tabs-set tabs frame)
(run-hook-with-args 'tab-bar-tab-post-open-functions
(car tabs)))
tabs))
(defun tab-bar-tabs-set (tabs &optional frame)
"Set a list of TABS on the FRAME."
(set-frame-parameter frame 'tabs tabs))
(defun tab-bar-tab-name-format-truncated (name _tab _i)
"Truncate the tab name.
The maximal length is specified by `tab-bar-tab-name-truncated-max'.
Append ellipsis `tab-bar-tab-name-ellipsis' at the end."
(if (< (length name) tab-bar-tab-name-truncated-max)
name
(truncate-string-to-width
name tab-bar-tab-name-truncated-max nil nil
tab-bar-tab-name-ellipsis)))
(defun tab-bar-tab-name-format-hints (name _tab i)
"Show absolute numbers on tabs in the tab bar before the tab name.
It has effect when `tab-bar-tab-hints' is non-nil."
(if tab-bar-tab-hints (concat (format "%d " i) name) name))
(defun tab-bar-tab-name-format-close-button (name tab _i)
"Show the tab close button.
The variable `tab-bar-close-button-show' defines when to show it."
(if (and tab-bar-close-button-show
(not (eq tab-bar-close-button-show
(if (eq (car tab) 'current-tab) 'non-selected 'selected)))
tab-bar-close-button)
(concat name tab-bar-close-button)
name))
(defcustom tab-bar-tab-face-function #'tab-bar-tab-face-default
"Function to define a tab face.
Function gets one argument: a tab."
:type 'function
:group 'tab-bar
:version "28.1")
(defun tab-bar-tab-face-default (tab)
(if (eq (car tab) 'current-tab) 'tab-bar-tab 'tab-bar-tab-inactive))
(defun tab-bar-tab-name-format-face (name tab _i)
"Apply the face to the tab name.
It uses the function `tab-bar-tab-face-function'."
(add-face-text-property
0 (length name) (funcall tab-bar-tab-face-function tab) t name)
name)
(defcustom tab-bar-tab-name-format-functions
'(tab-bar-tab-name-format-hints
tab-bar-tab-name-format-close-button
tab-bar-tab-name-format-face)
"Functions called to modify the tab name.
Each function is called with three arguments: the name returned
by the previously called modifier, the tab and its number.
It should return the formatted tab name to display in the tab bar."
:type '(repeat
(choice (function-item tab-bar-tab-name-format-truncated)
(function-item tab-bar-tab-name-format-hints)
(function-item tab-bar-tab-name-format-close-button)
(function-item tab-bar-tab-name-format-face)
(function :tag "Custom function")))
:group 'tab-bar
:version "30.1")
(defun tab-bar-tab-name-format-default (tab i)
(let ((name (copy-sequence (alist-get 'name tab))))
(run-hook-wrapped 'tab-bar-tab-name-format-functions
(lambda (fun)
(setq name (funcall fun name tab i))
nil))
name))
(defcustom tab-bar-tab-name-format-function #'tab-bar-tab-name-format-default
"Function to format a tab name.
Function gets two arguments, the tab and its number, and should return
the formatted tab name to display in the tab bar."
:type 'function
:initialize #'custom-initialize-default
:set (lambda (sym val)
(set-default sym val)
(force-mode-line-update))
:group 'tab-bar
:version "28.1")
(defcustom tab-bar-format '(tab-bar-format-history
tab-bar-format-tabs
tab-bar-separator
tab-bar-format-add-tab)
"Template for displaying tab bar items.
Every item in the list is a function that returns
a string, or a list of menu-item elements, or nil.
Adding a function to the list causes the tab bar to show
that string, or display a tab button which, when clicked,
will invoke the command that is the binding of the menu item.
The menu-item binding of nil will produce a tab clicking
on which will select that tab. The menu-item's title is
displayed as the label of the tab.
If a function returns nil, it doesn't directly affect the
tab bar appearance, but can do that by some side-effect.
If the list ends with `tab-bar-format-align-right' and
`tab-bar-format-global', then after enabling `display-time-mode'
\(or any other mode that uses `global-mode-string'),
it will display time aligned to the right on the tab bar instead
of the mode line. Replacing `tab-bar-format-tabs' with
`tab-bar-format-tabs-groups' will group tabs on the tab bar."
:type 'hook
:options '(tab-bar-format-menu-bar
tab-bar-format-history
tab-bar-format-tabs
tab-bar-format-tabs-groups
tab-bar-separator
tab-bar-format-add-tab
tab-bar-format-align-right
tab-bar-format-global)
:initialize #'custom-initialize-default
:set (lambda (sym val)
(set-default sym val)
(force-mode-line-update))
:group 'tab-bar
:version "28.1")
(defun tab-bar-menu-bar (event)
"Pop up the same menu as displayed by the menu bar.
Used by `tab-bar-format-menu-bar'."
(interactive "e")
(let ((menu (make-sparse-keymap (propertize "Menu Bar" 'hide t))))
(run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
(map-keymap (lambda (key binding)
(when (consp binding)
(define-key-after menu (vector key)
(copy-sequence binding))))
(menu-bar-keymap))
(popup-menu menu event)))
(defvar tab-bar-menu-bar-button
(propertize "Menu" 'face 'tab-bar-tab-inactive)
"Button for the menu bar.")
(defun tab-bar-format-menu-bar ()
"Produce the Menu button for the tab bar that shows the menu bar."
`((menu-bar menu-item ,tab-bar-menu-bar-button
tab-bar-menu-bar :help "Menu bar")))
(defun tab-bar-format-history ()
"Produce back and forward buttons for the tab bar.
These buttons will be shown when `tab-bar-history-mode' is enabled.
You can hide these buttons by customizing `tab-bar-format' and removing
`tab-bar-format-history' from it."
(when tab-bar-history-mode
`((sep-history-back menu-item ,(tab-bar-separator) ignore)
(history-back
menu-item ,tab-bar-back-button tab-bar-history-back
:help "Click to go back in tab history")
(sep-history-forward menu-item ,(tab-bar-separator) ignore)
(history-forward
menu-item ,tab-bar-forward-button tab-bar-history-forward
:help "Click to go forward in tab history"))))
(defun tab-bar--format-tab (tab i)
"Format TAB using its index I and return the result as a keymap."
(append
`((,(intern (format "sep-%i" i)) menu-item ,(tab-bar-separator) ignore))
(cond
((eq (car tab) 'current-tab)
`((current-tab
menu-item
,(funcall tab-bar-tab-name-format-function tab i)
ignore
:help ,(alist-get 'name tab))))
(t
`((,(intern (format "tab-%i" i))
menu-item
,(funcall tab-bar-tab-name-format-function tab i)
,(alist-get 'binding tab)
:help ,(alist-get 'name tab)))))
(when (alist-get 'close-binding tab)
`((,(if (eq (car tab) 'current-tab) 'C-current-tab
(intern (format "C-tab-%i" i)))
menu-item ""
,(alist-get 'close-binding tab))))))
(defun tab-bar-format-tabs ()
"Produce all the tabs for the tab bar."
(let ((i 0))
(mapcan
(lambda (tab)
(setq i (1+ i))
(tab-bar--format-tab tab i))
(funcall tab-bar-tabs-function))))