-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathcentaur-tabs-functions.el
1452 lines (1277 loc) · 58.8 KB
/
centaur-tabs-functions.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
;;; centaur-tabs-functions.el --- centaur-tabs logic components -*- lexical-binding: t; -*-
;; Copyright (C) 2019-2024 Emmanuel Bustos
;; Copyright (C) 2024 Jen-Chieh Shen
;; 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 2, 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;; This file contains functions that control the logic of centaur-tabs
;;
;;; Code:
(require 'cl-lib)
(require 'seq)
(require 'centaur-tabs-elements)
;; Compiler pacifier
(declare-function vterm "ext:vterm.el")
(declare-function centaur-tabs-move-current-tab-to-right "nerd-icons.el")
(declare-function centaur-tabs-move-current-tab-to-left "nerd-icons.el")
(defvar centaur-tabs--buffer-show-groups)
(defvar centaur-tabs-ace-jump-active)
(declare-function centaur-tabs-mode "centaur-tabs.el")
;;
;;; Customs
(defcustom centaur-tabs-cycle-scope nil
"*Specify the scope of cyclic navigation through tabs.
The following scopes are possible:
- `tabs'
Navigate through visible tabs only.
- `groups'
Navigate through tab groups only.
- default
Navigate through visible tabs, then through tab groups."
:group 'centaur-tabs
:type '(choice :tag "Cycle through..."
(const :tag "Visible Tabs Only" tabs)
(const :tag "Tab Groups Only" groups)
(const :tag "Visible Tabs then Tab Groups" nil)))
(defcustom centaur-tabs-auto-scroll-flag t
"*Non-nil means to automatically scroll the tab bar.
That is, when a tab is selected outside of the tab bar visible area,
the tab bar is scrolled horizontally so the selected tab becomes
visible."
:group 'centaur-tabs
:type 'boolean)
(defcustom centaur-tabs-common-group-name "Common"
"If the current buffer does not belong to any project the group name uses the
name of this variable."
:group 'centaur-tabs
:type 'string)
(defcustom centaur-tabs-label-fixed-length 0
"Fixed length of label. Set to 0 if dynamic."
:group 'centaur-tabs
:type 'integer)
(defcustom centaur-tabs-hide-tabs-hooks
'(magit-status-mode-hook
magit-popup-mode-hook
reb-mode-hook
completion-list-mode-hook)
"Set hooks for buffers in which it isn't desired to have tabs."
:type '(repeat symbol)
:group 'centaur-tabs)
(defcustom centaur-tabs-excluded-prefixes
'("*epc"
"*helm"
"*Helm"
" *which"
"*Compile-Log*"
"*Choices"
"*Process"
"*Calc"
"*lsp"
"*LSP"
"*company"
"*Flycheck"
"*Ediff"
"*ediff"
"*tramp"
" *Mini"
"*straight"
" *temp")
"List of prefixes that indicates which buffers should not be included as tabs.
Buffers that have names that start with any of these strings will be ignored."
:type '(repeat string)
:group 'centaur-tabs)
(defvar centaur-tabs-hide-predicate #'ignore
"Predicate function to hide the entire tab line.
Ths tab line will hide if this function returns t.")
(defvar centaur-tabs-hide-tab-function 'centaur-tabs-hide-tab
"Function to hide tabs.
This function filters tabs. The tab will hide if this function returns t.")
(defvar centaur-tabs-current-tabset-function nil
"Function called with no argument to obtain the current tab set.
This is the tab set displayed on the tab bar.")
(defvar centaur-tabs-tab-label-function nil
"Function that obtains a tab label displayed on the tab bar.
The function is passed a tab and should return a string.")
(defvar centaur-tabs-select-tab-function nil
"Function that selects a tab.
The function is passed a tab, and makes it the
selected tab.")
(defvar centaur-tabs-buffer-list-function 'centaur-tabs-buffer-list
"Function that returns the list of buffers to show in the tab line.
That function is called with no arguments and must return a list of
buffers.")
(defvar centaur-tabs-buffer-groups-function 'centaur-tabs-buffer-groups
"Function that gives the group names the current buffer belongs to.
It must return a list of group names, or nil if the buffer has no
group. Notice that it is better that a buffer belongs to one group.")
(defvar centaur-tabs-custom-buffer-groups nil
"Function that runs before any grouping logic inside the function
`centaur-tabs-buffer-groups-function'.")
(defvar centaur-tabs-adjust-buffer-order-function 'centaur-tabs-adjust-buffer-order
"Function to adjust buffer order after switch tab.
Default is `centaur-tabs-adjust-buffer-order', you can write your own rule.")
(defcustom centaur-tabs-adjust-buffer-order nil
"Set automatic buffer ordering for buffer changing commands.
The ordering is appliet for non click or tab motion commands.
There are four options:
1 - nil: No ordering applied
2 - t: Move the currently selected tab to the side (right or left) of the last
visited tab.
3 - left: Move the currently selected tab to left of the last visited tab.
4 - right: Move the currently selected tab to right of the last visited tab."
:group 'centaur-tabs
:type '(choice :tag "Automatic buffer reordering..."
(const :tag "Do not adjust buffer order." nil)
(const :tag "When the currently selected tab(A) is at the right of the last visited
tab(B), move A to the right of B. When the currently selected tab(A) is at the left of the last visited
tab(B), move A to the left of B" t)
(const :tag "Move the currently selected tab to the left of the the last visited tab." left)
(const :tag "Move the currently selected tab to the right of the the last visited tab." right)))
(defcustom centaur-tabs-enable-key-bindings nil
"Enable a selection of default key bindings for centaur-tabs."
:group 'centaur-tabs
:type 'boolean)
(defun centaur-tabs-headline-match ()
"Make headline use centaur-tabs-default-face."
(set-face-attribute
centaur-tabs-display-line nil
:background (face-background 'centaur-tabs-unselected nil 'default)
:box nil :overline nil :underline nil))
;; Change the font and height for all tab faces
(defun centaur-tabs-change-fonts (family height)
"Change the fonts of all the tabs.
FAMILY is the font family and HEIGHT is the font height."
(dolist (centaur-face '(centaur-tabs-selected
centaur-tabs-selected-modified
centaur-tabs-unselected
centaur-tabs-unselected-modified))
(set-face-attribute centaur-face nil :family family :height height)))
;;; Tabs Redisplay function
;;
(eval-and-compile
(defalias 'centaur-tabs-display-update
(if (fboundp 'force-window-update)
#'(lambda () (force-window-update (selected-window)))
'force-mode-line-update)))
(defun centaur-tabs-2str (obj)
"Convert OBJ to string."
(format "%s" obj))
;;; Name truncation
;;
;; Copied from s.el
(defun centaur-tabs-truncate-string (len s &optional ellipsis)
"If S is longer than LEN, cut it down and add ELLIPSIS to the end.
The resulting string, including ellipsis, will be LEN characters
long.
When not specified, ELLIPSIS defaults to ‘...’."
(declare (pure t) (side-effect-free t))
(unless ellipsis
(setq ellipsis (if (char-displayable-p ?…) "…" "...")))
(if (> (length s) len)
(format "%s%s" (substring s 0 (- len (length ellipsis))) ellipsis)
(concat s (make-string (- len (length s)) ? ))))
;;
;;; Keymaps
(defvar centaur-tabs-prefix-key ["C-c t"]
"The common prefix key used in Centaur-Tabs mode.")
(defvar centaur-tabs-prefix-map
(let ((km (make-sparse-keymap)))
(define-key km (kbd "<left>") 'centaur-tabs-backward)
(define-key km (kbd "<right>") 'centaur-tabs-forward)
(define-key km (kbd "<up>") 'centaur-tabs-backward-group)
(define-key km (kbd "<down>") 'centaur-tabs-forward-group)
(define-key km (kbd "f10") 'centaur-tabs-local-mode)
(define-key km (kbd "C-5") 'centaur-tabs-extract-window-to-new-frame)
(define-key km (kbd "a") 'centaur-tabs-ace-jump)
(define-key km (kbd "s") 'centaur-tabs-counsel-switch-group)
(define-key km (kbd "p") 'centaur-tabs-group-by-projectile-project)
(define-key km (kbd "g") 'centaur-tabs-group-buffer-groups)
(define-key km (kbd "k") 'centaur-tabs-kill-all-buffers-in-current-group)
(define-key km (kbd "o") 'centaur-tabs-kill-other-buffers-in-current-group)
(define-key km (kbd "d") 'centaur-tabs-open-directory-in-external-application)
km)
"The key bindings provided in Centaur-Tabs mode.")
(defvar centaur-tabs-mode-map
(let ((map (make-sparse-keymap)))
;; Optional keybord bindings
(when centaur-tabs-enable-key-bindings
(define-key map centaur-tabs-prefix-key centaur-tabs-prefix-map))
;; Use mouse wheel to switch between buffers of same group
(define-key map (vector centaur-tabs-display-line 'mouse-5 ) 'centaur-tabs-forward )
(define-key map (vector centaur-tabs-display-line 'mouse-4 ) 'centaur-tabs-backward)
(define-key map (vector centaur-tabs-display-line 'wheel-down) 'centaur-tabs-forward )
(define-key map (vector centaur-tabs-display-line 'wheel-up ) 'centaur-tabs-backward)
;; Use right click to show the rest of groups
(define-key map (vector centaur-tabs-display-line 'mouse-3) 'centaur-tabs--tab-menu )
map)
"Keymap to use in Centaur-Tabs mode.")
(defvar centaur-tabs-close-map
(let ((map (make-sparse-keymap)))
(define-key map (vector centaur-tabs-display-line 'mouse-1) 'centaur-tabs-do-close)
(define-key map (vector centaur-tabs-display-line 'mouse-2) 'centaur-tabs-do-close)
map)
"Keymap used for setting mouse events for close button.")
(defvar centaur-tabs-backward-tab-map
(let ((map (make-sparse-keymap)))
(define-key map (vector centaur-tabs-display-line 'mouse-1) 'centaur-tabs-backward--button)
(define-key map (vector centaur-tabs-display-line 'mouse-3) 'centaur-tabs--groups-menu)
(define-key map (vector centaur-tabs-display-line 'C-mouse-1) 'centaur-tabs-move-current-tab-to-left--button)
map)
"Keymap used for setting mouse events for backward tab button.")
(defvar centaur-tabs-forward-tab-map
(let ((map (make-sparse-keymap)))
(define-key map (vector centaur-tabs-display-line 'mouse-1) 'centaur-tabs-forward--button)
(define-key map (vector centaur-tabs-display-line 'mouse-3) 'centaur-tabs--groups-menu)
(define-key map (vector centaur-tabs-display-line 'C-mouse-1) 'centaur-tabs-move-current-tab-to-right--button)
map)
"Keymap used for setting mouse events for forward tab button.")
(defvar centaur-tabs-down-tab-map
(let ((map (make-sparse-keymap)))
(define-key map (vector centaur-tabs-display-line 'mouse-1) 'centaur-tabs--groups-menu)
(define-key map (vector centaur-tabs-display-line 'mouse-3) 'centaur-tabs--groups-menu)
map)
"Keymap used for setting mouse events for down tab button.")
(defvar centaur-tabs-default-map
(let ((map (make-sparse-keymap)))
(define-key map (vector centaur-tabs-display-line 'mouse-1) 'centaur-tabs-do-select)
(define-key map (vector centaur-tabs-display-line 'mouse-2) 'centaur-tabs-do-close)
map)
"Keymap used for setting mouse events for a tab.")
(defvar centaur-tabs-new-tab-map
(let ((map (make-sparse-keymap)))
(define-key map (vector centaur-tabs-display-line 'mouse-1) 'centaur-tabs-new-tab--button)
map)
"Keymap used for setting mouse events for new tab button.")
;;
;;; Tab and tab sets
(defsubst centaur-tabs-make-tab (object tabset)
"Return a new tab with value OBJECT.
TABSET is the tab set the tab belongs to."
(cons object tabset))
(defsubst centaur-tabs-tab-value (tab)
"Return the value of tab TAB."
(car tab))
(defsubst centaur-tabs-tab-tabset (tab)
"Return the tab set TAB belongs to."
(cdr tab))
(defvar centaur-tabs-tabsets nil
"The tab sets store.")
(defvar centaur-tabs-tabsets-tabset nil
"The special tab set of existing tab sets.")
(defvar centaur-tabs-current-tabset nil
"The tab set currently displayed on the tab bar.")
(make-variable-buffer-local 'centaur-tabs-current-tabset)
(defvar centaur-tabs-init-hook nil
"Hook run after tab bar data has been initialized.
You should use this hook to initialize dependent data.")
(defvar centaur-tabs-display-hash (make-hash-table :test 'equal)
"Display format cache.")
(defsubst centaur-tabs-init-tabsets-store ()
"Initialize the tab set store."
(setq centaur-tabs-tabsets (make-vector 31 0)
centaur-tabs-tabsets-tabset (make-symbol "centaur-tabs-tabsets-tabset"))
(put centaur-tabs-tabsets-tabset 'start 0)
(run-hooks 'centaur-tabs-init-hook))
(defvar centaur-tabs-quit-hook nil
"Hook run after tab bar data has been freed.
You should use this hook to reset dependent data.")
(defsubst centaur-tabs-free-tabsets-store ()
"Free the tab set store."
(setq centaur-tabs-tabsets nil
centaur-tabs-tabsets-tabset nil)
(run-hooks 'centaur-tabs-quit-hook))
;; Define an "hygienic" function free of side effect between its local
;; variables and those of the callee.
(eval-and-compile
(defalias 'centaur-tabs-map-tabsets
(let ((function (make-symbol "function"))
(result (make-symbol "result"))
(tabset (make-symbol "tabset")))
`(lambda (,function)
"Apply FUNCTION to each tab set, and make a list of the results.
The result is a list just as long as the number of existing tab sets."
(let (,result)
(mapatoms
#'(lambda (,tabset)
(push (funcall ,function ,tabset) ,result))
centaur-tabs-tabsets)
,result)))))
(defun centaur-tabs-make-tabset (name &rest objects)
"Make a new tab set whose name is the string NAME.
It is initialized with tabs build from the list of OBJECTS."
(when name ; some buffers don't have a tabset (e.g. org-agenda)
(let* ((tabset (intern name centaur-tabs-tabsets))
(tabs (mapcar #'(lambda (object)
(centaur-tabs-make-tab object tabset))
objects)))
(set tabset tabs)
(centaur-tabs-put-cache tabset 'select (car tabs))
(put tabset 'start 0)
tabset)))
(defsubst centaur-tabs-get-tabset (name)
"Return the tab set whose name is the string NAME.
Return nil if not found."
(intern-soft name centaur-tabs-tabsets))
(defsubst centaur-tabs-delete-tabset (tabset)
"Delete the tab set TABSET.
That is, remove it from the tab sets store."
(unintern tabset centaur-tabs-tabsets))
(defsubst centaur-tabs-tabs (tabset)
"Return the list of tabs in TABSET."
(symbol-value tabset))
(defsubst centaur-tabs-tab-values (tabset)
"Return the list of tab values in TABSET."
(mapcar 'centaur-tabs-tab-value (centaur-tabs-tabs tabset)))
(defun centaur-tabs-get-cache (cache key)
"Return the cached value of KEY in CACHE."
(when-let* ((cache (format "%s" cache))
(cached-hash (gethash cache centaur-tabs-display-hash))
((hash-table-p cached-hash)))
(gethash key cached-hash nil)))
(defun centaur-tabs-put-cache (cache key value)
"Set the cached value of KEY in CACHE to VALUE."
(let* ((cache (format "%s" cache))
(cached-hash (gethash cache centaur-tabs-display-hash))
(hash (if (hash-table-p cached-hash) cached-hash (make-hash-table))))
(puthash key value hash)
(puthash cache hash centaur-tabs-display-hash))
value)
(defsubst centaur-tabs-get-tab (object tabset)
"Search for a tab with value OBJECT in TABSET.
Return the tab found, or nil if not found."
(assoc object (centaur-tabs-tabs tabset)))
(defsubst centaur-tabs-member (tab tabset)
"Return non-nil if TAB is in TABSET."
(or (eq (centaur-tabs-tab-tabset tab) tabset)
(memq tab (centaur-tabs-tabs tabset))))
(defsubst centaur-tabs-template (tabset)
"Return the cached visual representation of TABSET.
That is, a `centaur-tabs-display-line-format' template, or nil if
the cache is empty."
(centaur-tabs-get-cache tabset 'template))
(defsubst centaur-tabs-set-template (tabset template)
"Set the cached visual representation of TABSET to TEMPLATE.
TEMPLATE must be a valid `centaur-tabs-display-line-format' template,
or nil to cleanup the cache."
(centaur-tabs-put-cache tabset 'template template))
(defsubst centaur-tabs-selected-tab (tabset)
"Return the tab selected in TABSET."
(centaur-tabs-get-cache tabset 'select))
(defsubst centaur-tabs-selected-value (tabset)
"Return the value of the tab selected in TABSET."
(centaur-tabs-tab-value (centaur-tabs-selected-tab tabset)))
(defsubst centaur-tabs-selected-p (tab tabset)
"Return non-nil if TAB is the selected tab in TABSET."
(eq tab (centaur-tabs-selected-tab tabset)))
(defvar centaur-tabs--track-selected nil)
(defsubst centaur-tabs-select-tab (tab tabset)
"Make TAB the selected tab in TABSET.
Does nothing if TAB is not found in TABSET.
Return TAB if selected, nil if not."
(when (centaur-tabs-member tab tabset)
(unless (centaur-tabs-selected-p tab tabset)
(centaur-tabs-set-template tabset nil)
(setq centaur-tabs--track-selected centaur-tabs-auto-scroll-flag))
(centaur-tabs-put-cache tabset 'select tab)))
(defsubst centaur-tabs-select-tab-value (object tabset)
"Make the tab with value OBJECT, the selected tab in TABSET.
Does nothing if a tab with value OBJECT is not found in TABSET.
Return the tab selected, or nil if nothing was selected."
(centaur-tabs-select-tab (centaur-tabs-get-tab object tabset) tabset))
(defsubst centaur-tabs-start (tabset)
"Return the index of the first visible tab in TABSET."
(get tabset 'start))
(defsubst centaur-tabs-view (tabset)
"Return the list of visible tabs in TABSET.
That is, the sub-list of tabs starting at the first visible one."
(nthcdr (centaur-tabs-start tabset) (centaur-tabs-tabs tabset)))
(defun centaur-tabs-add-tab (tabset object)
"Check if OBJECT tab is already open in TABSET.
Otherwise insert it."
(let ((tabs (centaur-tabs-tabs tabset)))
(if (centaur-tabs-get-tab object tabset)
tabs
(let* ((tab (centaur-tabs-make-tab object tabset))
(selected (centaur-tabs-selected-tab tabset))
(selected-index (cl-position (car selected) (mapcar 'car tabs))))
(centaur-tabs-set-template tabset nil)
(set tabset (centaur-tabs-insert-at tabs selected-index tab))))))
(defun centaur-tabs-insert-at (list index insert-element)
"Insert INSERT-ELEMENT in LIST at index INDEX."
(let ((counter 0)
result)
(dolist (element list)
(if (equal counter index)
(setq result (append result (list element insert-element)))
(setq result (append result (list element))))
(setq counter (+ 1 counter)))
result))
(defun centaur-tabs-delete-tab (tab)
"Remove TAB from its tab set."
(let* ((tabset (centaur-tabs-tab-tabset tab))
(tabs (centaur-tabs-tabs tabset))
(sel (eq tab (centaur-tabs-selected-tab tabset)))
(next (and sel (cdr (memq tab tabs)))))
(centaur-tabs-set-template tabset nil)
(setq tabs (delq tab tabs))
;; When the selected tab is deleted, select the next one, if
;; available, or the last one otherwise.
(and sel (centaur-tabs-select-tab (car (or next (last tabs))) tabset))
(set tabset tabs)))
(defun centaur-tabs-scroll (tabset count)
"Scroll the visible tabs in TABSET of COUNT units.
If COUNT is positive move the view on right. If COUNT is negative,
move the view on left."
(let ((start (min (max 0 (+ (centaur-tabs-start tabset) count))
(1- (length (centaur-tabs-tabs tabset))))))
(when (/= start (centaur-tabs-start tabset))
(centaur-tabs-set-template tabset nil)
(put tabset 'start start))))
(defun centaur-tabs-tab-next (tabset tab &optional before)
"Search in TABSET for the tab after TAB.
If optional argument BEFORE is non-nil, search for the tab before
TAB. Return the tab found, or nil otherwise."
(let* (last (tabs (centaur-tabs-tabs tabset)))
(while (and tabs (not (eq tab (car tabs))))
(setq last (car tabs)
tabs (cdr tabs)))
(and tabs (if before last (nth 1 tabs)))))
(defun centaur-tabs-current-tabset (&optional update)
"Return the tab set currently displayed on the tab bar.
If optional argument UPDATE is non-nil, call the user defined function
`centaur-tabs-current-tabset-function' to obtain it. Otherwise return the
current cached copy."
(and update centaur-tabs-current-tabset-function
(setq centaur-tabs-current-tabset
(funcall centaur-tabs-current-tabset-function)))
centaur-tabs-current-tabset)
(defun centaur-tabs-get-tabsets-tabset ()
"Return the tab set of selected tabs in existing tabsets."
(set centaur-tabs-tabsets-tabset (centaur-tabs-map-tabsets 'centaur-tabs-selected-tab))
(centaur-tabs-scroll centaur-tabs-tabsets-tabset 0)
(centaur-tabs-set-template centaur-tabs-tabsets-tabset nil)
centaur-tabs-tabsets-tabset)
(defun centaur-tabs-after-focus (&rest _)
"Focus hook."
(when (frame-focus-state)
(ignore-errors (centaur-tabs-buffer-update-groups))
(ignore-errors (centaur-tabs-display-update))))
(defun centaur-tabs-on-window-buffer-change (frame &rest _)
"Function to be run after window buffer is changed in FRAME."
(unless (frame-parent frame)
(ignore-errors (centaur-tabs-buffer-update-groups))))
;; Functions for modification hooks and advices
(defun centaur-tabs-on-saving-buffer ()
"Function to be run after the buffer is saved."
(centaur-tabs-set-template centaur-tabs-current-tabset nil)
(centaur-tabs-display-update))
(defun centaur-tabs-on-modifying-buffer (&rest _)
"Function to be run after the buffer is first changed."
(set-buffer-modified-p (buffer-modified-p))
(centaur-tabs-set-template centaur-tabs-current-tabset nil)
(centaur-tabs-display-update))
(defun centaur-tabs--after-load-theme (&rest _)
"Function to be run after the theme changed."
(dolist (buffer (and centaur-tabs-buffer-list-function
(funcall centaur-tabs-buffer-list-function)))
(with-current-buffer buffer
(centaur-tabs-set-template centaur-tabs-current-tabset nil)
(setq centaur-tabs-style-right nil
centaur-tabs-style-left nil)))
(centaur-tabs-display-update))
;;
;;; Events and event functions
(defun centaur-tabs-buffer-close-tab (tab)
"Function for closing TAB."
(let ((buffer (centaur-tabs-tab-value tab)))
(kill-buffer buffer)
(centaur-tabs-buffer-update-groups)
(centaur-tabs-display-update)))
(defun centaur-tabs-get-tab-from-event (event)
"Given a mouse EVENT, extract the tab at the mouse point."
(let ((pos (posn-string (event-start event))))
(get-text-property (cdr pos) 'centaur-tabs-tab (car pos))))
(defun centaur-tabs-do-select (event)
"Given a mouse EVENT, select the tab at the mouse point."
(interactive "e")
(select-window (posn-window (event-start event)))
(centaur-tabs-buffer-select-tab `,(centaur-tabs-get-tab-from-event event)))
(defun centaur-tabs-do-close (event)
"Given a mouse EVENT, close the tab at the mouse point."
(interactive "e")
(let ((window (posn-window (event-start event))))
(with-selected-window window
(select-window window)
(let ((foreground-buffer-name (buffer-name)))
(centaur-tabs-buffer-select-tab `,(centaur-tabs-get-tab-from-event event))
(let* ((buffer (window-buffer window))
(target-buffer-name (buffer-name))
(same-target-check (string-equal foreground-buffer-name target-buffer-name))
(window-num (- (length (get-buffer-window-list buffer))
(if same-target-check 0 1))))
(if (> window-num 1)
(delete-window window)
(centaur-tabs-buffer-close-tab `,(centaur-tabs-get-tab-from-event event))))))))
(defun centaur-tabs-backward--button (event)
"Same as centaur-tabs-backward, but changing window to EVENT source."
(interactive "e")
(select-window (posn-window (event-start event)))
(centaur-tabs-backward))
(defun centaur-tabs-forward--button (event)
"Same as centaur-tabs-forward, but changing window to EVENT source."
(interactive "e")
(select-window (posn-window (event-start event)))
(centaur-tabs-forward))
(defun centaur-tabs-new-tab--button (event)
"Same as centaur-tabs--create-new-tab, but changing window to EVENT source."
(interactive "e")
(select-window (posn-window (event-start event)))
(centaur-tabs--create-new-tab))
(defun centaur-tabs-move-current-tab-to-left--button (evt)
"Same as centaur-tabs-move-current-tab-to-left, but ensuring the tab will
remain visible. The active window will the the EVT source."
(interactive "e")
(centaur-tabs-move-current-tab-to-left)
(centaur-tabs--button-ensure-selected-tab-is-visible evt))
(defun centaur-tabs-move-current-tab-to-right--button (evt)
"Same as centaur-tabs-move-current-tab-to-right, but ensuring the tab will
remain visible. The active window will the the EVT source."
(interactive "e")
(centaur-tabs-move-current-tab-to-right)
(centaur-tabs--button-ensure-selected-tab-is-visible evt))
(defun centaur-tabs--button-ensure-selected-tab-is-visible (evt)
"This is a nasty trick to make the current tab visible, since
`centaur-tabs--track-selected' or `centaur-tabs-auto-scroll-flag' seems not
to work. EVT is used to change the active window."
;; This works if the tab has not reached the last position
(centaur-tabs-forward--button evt)
(centaur-tabs-backward--button evt)
;; Just in case the tab has the tab reached the last position
(centaur-tabs-backward--button evt)
(centaur-tabs-forward--button evt))
(defun centaur-tabs-refill-tabs ()
"Refill current tab line."
(centaur-tabs-buffer-update-groups)
(force-window-update (selected-window))
(centaur-tabs--button-ensure-selected-tab-is-visible nil))
;;
;;; Tabs display
(defsubst centaur-tabs-line-tab (tab)
"Return the display representation of tab TAB.
That is, a propertized string used as an `centaur-tabs-display-line-format'
template element.
Call `centaur-tabs-tab-label-function' to obtain a label for TAB."
(let* ((buf (centaur-tabs-tab-value tab))
(buf-file-name (buffer-file-name buf))
(selected-p (centaur-tabs-selected-p tab (centaur-tabs-current-tabset)))
(not-read-only-p (with-current-buffer buf (not buffer-read-only)))
(modified-p (and not-read-only-p (buffer-modified-p buf)))
(use-mod-mark-p (and centaur-tabs-set-modified-marker modified-p))
(mod-mark-face (if selected-p
'centaur-tabs-modified-marker-selected
'centaur-tabs-modified-marker-unselected))
(face (if selected-p
(if modified-p
'centaur-tabs-selected-modified
'centaur-tabs-selected)
(if modified-p
'centaur-tabs-unselected-modified
'centaur-tabs-unselected)))
(bar (if (and selected-p (eq (if (display-graphic-p) centaur-tabs-set-bar) 'left))
(propertize
centaur-tabs-active-bar
'centaur-tabs-tab tab
'pointer centaur-tabs-mouse-pointer
'local-map centaur-tabs-default-map)
""))
(icon (if (and centaur-tabs-set-icons
(not centaur-tabs--buffer-show-groups))
(propertize
(centaur-tabs-icon tab face selected-p)
'centaur-tabs-tab tab
'pointer centaur-tabs-mouse-pointer
'help-echo (with-current-buffer buf (format-mode-line mode-name))
'local-map centaur-tabs-default-map)
"")))
(when (or (not centaur-tabs-style-left)
(not centaur-tabs-style-right))
(centaur-tabs-select-separator-style centaur-tabs-style))
(concat (centaur-tabs-separator-render centaur-tabs-style-left face)
bar
;; left margin
(when centaur-tabs-left-edge-margin
(propertize centaur-tabs-left-edge-margin
'face face
'centaur-tabs-tab tab
'pointer centaur-tabs-mouse-pointer
'local-map centaur-tabs-default-map))
;; left close button
(when centaur-tabs-set-left-close-button
(propertize centaur-tabs-close-button
'face (if selected-p
'centaur-tabs-close-selected
'centaur-tabs-close-unselected)
'pointer centaur-tabs-mouse-pointer
'help-echo "Close buffer"
'centaur-tabs-tab tab
'mouse-face 'centaur-tabs-close-mouse-face
'local-map centaur-tabs-close-map))
;; icon
(if (= (length icon) 0) ""
(concat (propertize centaur-tabs-icons-prefix
'face face
'centaur-tabs-tab tab
'pointer centaur-tabs-mouse-pointer
'local-map centaur-tabs-default-map)
icon))
;; tab name
(propertize (concat
(if centaur-tabs-tab-label-function
(funcall centaur-tabs-tab-label-function tab)
(buffer-name buf))
" ")
'centaur-tabs-tab tab
'face face
'mouse-face 'centaur-tabs-name-mouse-face
'pointer centaur-tabs-mouse-pointer
'help-echo buf-file-name
'local-map centaur-tabs-default-map)
;; tab identifier
(when centaur-tabs-show-jump-identifier
(when (or (eq centaur-tabs-show-jump-identifier 'always)
centaur-tabs-ace-jump-active)
(when-let* ((position (nth (cl-position tab (centaur-tabs-view (centaur-tabs-current-tabset t)))
centaur-tabs-ace-jump-keys)))
(propertize
(format "%c" position)
'centaur-tabs-tab tab
'face (if selected-p
'centaur-tabs-jump-identifier-selected
'centaur-tabs-jump-identifier-unselected)
'pointer centaur-tabs-mouse-pointer
'help-echo buf-file-name
'local-map centaur-tabs-default-map))))
;; close button and/or modified marker
(unless centaur-tabs-ace-jump-active
(if centaur-tabs-set-close-button
(propertize (if use-mod-mark-p
centaur-tabs-modified-marker
centaur-tabs-close-button)
'face (if use-mod-mark-p
mod-mark-face
(if selected-p
'centaur-tabs-close-selected
'centaur-tabs-close-unselected))
'pointer centaur-tabs-mouse-pointer
'help-echo "Close buffer"
'centaur-tabs-tab tab
'mouse-face 'centaur-tabs-close-mouse-face
'local-map centaur-tabs-close-map)
(if (and centaur-tabs-set-modified-marker modified-p)
(propertize centaur-tabs-modified-marker
'face mod-mark-face
'pointer centaur-tabs-mouse-pointer
'centaur-tabs-tab tab
'help-echo buf-file-name
'local-map centaur-tabs-default-map)
"" )))
;; right margin
(when centaur-tabs-right-edge-margin
(propertize centaur-tabs-right-edge-margin
'face face
'centaur-tabs-tab tab
'pointer centaur-tabs-mouse-pointer
'local-map centaur-tabs-default-map))
(centaur-tabs-separator-render centaur-tabs-style-right face))))
(defsubst centaur-tabs-button-tab (button)
"Return the display representation of button BUTTON.
That is, a propertized string used as an `centaur-tabs-display-line-format'
template element."
(let* ((face 'centaur-tabs-unselected))
(concat (propertize button
'face face
'mouse-face 'highlight))))
(defun centaur-tabs-line-format (tabset)
"Return the `centaur-tabs-display-line-format' value to display TABSET."
(let* ((sel (centaur-tabs-selected-tab tabset))
(tabs (centaur-tabs-view tabset))
(padcolor centaur-tabs-background-color)
(all-tabs (centaur-tabs-tabs tabset))
(total-tabs (length all-tabs))
(sel-index (+ (cl-position (car sel) (mapcar 'car all-tabs)) 1))
atsel elts)
;; Track the selected tab to ensure it is always visible.
(when centaur-tabs--track-selected
(while (not (memq sel tabs))
(centaur-tabs-scroll tabset -1)
(setq tabs (centaur-tabs-view tabset)))
(while (and tabs (not atsel))
(setq elts (cons (centaur-tabs-line-tab (car tabs)) elts)
atsel (eq (car tabs) sel)
tabs (cdr tabs)))
(setq elts (nreverse elts))
;; At this point the selected tab is the last elt in ELTS.
;; Scroll TABSET and ELTS until the selected tab becomes
;; visible.
(let (buffer-list-update-hook)
(with-temp-buffer
(let ((truncate-partial-width-windows nil)
(inhibit-modification-hooks t)
deactivate-mark ;; Prevent deactivation of the mark!
start)
(setq truncate-lines nil
buffer-undo-list t)
(setq start (point))
(while (and (cdr elts) ;; Always show the selected tab!
(progn
(delete-region start (point-max))
(goto-char (point-max))
(apply #'insert elts)
(goto-char (point-min))
(> (vertical-motion 1) 0)))
(centaur-tabs-scroll tabset -1)
(setq elts (cdr elts))))))
(setq elts (nreverse elts))
(setq centaur-tabs--track-selected nil))
;; Format remaining tabs.
(while tabs
(setq elts (cons (centaur-tabs-line-tab (car tabs)) elts)
tabs (cdr tabs)))
;; Cache and return the new tab bar.
(centaur-tabs-set-template
tabset
(list
(centaur-tabs-count sel-index total-tabs)
(centaur-tabs-line-format--buttons)
(nreverse elts)
(propertize "% "
'face (list :background padcolor)
'pointer 'arrow)
(centaur-tabs-line-format--new-button)))))
(defun centaur-tabs-count (index count)
"Return a centaur-tabs-button-tab with the current tab INDEX and the total
tabs COUNT."
(if centaur-tabs-show-count
(propertize (centaur-tabs-button-tab (format centaur-tabs-count-format
index count))
'help-echo "Tabs count")
""))
(defun centaur-tabs-line-format--buttons ()
"Return the buttons fragment of the header line."
(if (and centaur-tabs-show-navigation-buttons (display-graphic-p))
(list
(propertize (centaur-tabs-button-tab centaur-tabs-down-tab-text)
'local-map centaur-tabs-down-tab-map
'help-echo "Change tab group")
(propertize (centaur-tabs-button-tab centaur-tabs-backward-tab-text)
'local-map centaur-tabs-backward-tab-map
'help-echo "Previous tab")
(propertize (centaur-tabs-button-tab centaur-tabs-forward-tab-text)
'local-map centaur-tabs-forward-tab-map
'help-echo "Next tab"))
""))
(defun centaur-tabs-line-format--new-button ()
"Return the new-tab button fragment at the right end of the header line."
(if centaur-tabs-show-new-tab-button
(concat
(propertize (centaur-tabs-button-tab centaur-tabs-new-tab-text)
'local-map centaur-tabs-new-tab-map
'help-echo "Create new tab")
"")))
(defun centaur-tabs-line ()
"Return the header line templates that represent the tab bar.
Inhibit display of the tab bar in current window where
`centaur-tabs-hide-tab-function' return t."
(cond ((or (centaur-tabs-hide-tab-cached (current-buffer))
(and centaur-tabs-hide-predicate
(funcall centaur-tabs-hide-predicate)))
;; Don't show the tab bar.
(set centaur-tabs-display-line-format nil))
((centaur-tabs-current-tabset t)
;; When available, use a cached tab bar value, else recompute it.
(or (centaur-tabs-template centaur-tabs-current-tabset)
(centaur-tabs-line-format centaur-tabs-current-tabset)))))
(defconst centaur-tabs-header-line-format '(:eval (centaur-tabs-line))
"The tab bar header line format.")
;;
;;; Cyclic navigation through tabs
(defun centaur-tabs-cycle (&optional backward)
"Cycle to the next available tab.
The scope of the cyclic navigation through tabs is specified by the
option `centaur-tabs-cycle-scope'.
If optional argument BACKWARD is non-nil, cycle to the previous tab
instead."
(let* ((tabset (centaur-tabs-current-tabset t))
(ttabset (centaur-tabs-get-tabsets-tabset))
;; If navigation through groups is requested, and there is
;; only one group, navigate through visible tabs.
(cycle (if (and (eq centaur-tabs-cycle-scope 'groups)
(not (cdr (centaur-tabs-tabs ttabset))))
'tabs
centaur-tabs-cycle-scope))
selected tab)
(when tabset
(setq selected (centaur-tabs-selected-tab tabset))
(cond
;; Cycle through visible tabs only.
((eq cycle 'tabs)
(setq tab (centaur-tabs-tab-next tabset selected backward))
;; When there is no tab after/before the selected one, cycle
;; to the first/last visible tab.
(unless tab
(setq tabset (centaur-tabs-tabs tabset)
tab (car (if backward (last tabset) tabset)))))
;; Cycle through tab groups only.
((eq cycle 'groups)
(setq tab (centaur-tabs-tab-next ttabset selected backward))
;; When there is no group after/before the selected one, cycle
;; to the first/last available group.
(unless tab
(setq tabset (centaur-tabs-tabs ttabset)
tab (car (if backward (last tabset) tabset)))))
(t
;; Cycle through visible tabs then tab groups.
(setq tab (centaur-tabs-tab-next tabset selected backward))
;; When there is no visible tab after/before the selected one,
;; cycle to the next/previous available group.
(unless tab
(setq tab (centaur-tabs-tab-next ttabset selected backward))
;; When there is no next/previous group, cycle to the
;; first/last available group.
(unless tab
(setq tabset (centaur-tabs-tabs ttabset)
tab (car (if backward (last tabset) tabset))))
;; Select the first/last visible tab of the new group.
(setq tabset (centaur-tabs-tabs (centaur-tabs-tab-tabset tab))
tab (car (if backward (last tabset) tabset))))))
(centaur-tabs-buffer-select-tab tab))))
;;;###autoload
(defun centaur-tabs-backward ()
"Select the previous available tab.
Depend on the setting of the option `centaur-tabs-cycle-scope'."
(interactive)
(if (centaur-tabs-current-tabset t)
(centaur-tabs-cycle t)
(previous-buffer)))
;;;###autoload
(defun centaur-tabs-forward ()
"Select the next available tab.
Depend on the setting of the option `centaur-tabs-cycle-scope'."
(interactive)
(if (centaur-tabs-current-tabset t)
(centaur-tabs-cycle)
(next-buffer)))