diff options
| author | Juri Linkov | 2021-02-27 22:09:33 +0200 |
|---|---|---|
| committer | Juri Linkov | 2021-02-27 22:09:33 +0200 |
| commit | 3b3b16ea17a6ce3169e32acf4aa4c020f4db71d7 (patch) | |
| tree | 072f094c5a5673a2cb01f0e0f0d4c4ea16827479 | |
| parent | 53f6218cdc677cda38d23b9049c80ab84e7b4f31 (diff) | |
| download | emacs-3b3b16ea17a6ce3169e32acf4aa4c020f4db71d7.tar.gz emacs-3b3b16ea17a6ce3169e32acf4aa4c020f4db71d7.zip | |
* lisp/tab-bar.el: Support displaying global-mode-string in the tab bar.
* lisp/tab-bar.el (tab-bar--define-keys): Update global-mode-string
in mode-line-misc-info with condition to disable global-mode-string
in the mode line.
(tab-bar-format): New variable.
(tab-bar-format-history, tab-bar-format-add-tab)
(tab-bar-format-tabs): New functions with body from
'tab-bar-make-keymap-1'.
(tab-bar-format-align-right, tab-bar-format-global): New functions for
'tab-bar-format' list.
(tab-bar-format-list): New utility function.
(tab-bar-make-keymap-1): Just call 'tab-bar-format-list'.
https://lists.gnu.org/archive/html/emacs-devel/2020-11/msg01210.html
| -rw-r--r-- | etc/NEWS | 7 | ||||
| -rw-r--r-- | lisp/tab-bar.el | 165 |
2 files changed, 124 insertions, 48 deletions
| @@ -494,6 +494,13 @@ It can be used to enable/disable the tab bar individually on each frame | |||
| 494 | independently from the value of 'tab-bar-mode' and 'tab-bar-show'. | 494 | independently from the value of 'tab-bar-mode' and 'tab-bar-show'. |
| 495 | 495 | ||
| 496 | --- | 496 | --- |
| 497 | *** New variable 'tab-bar-format' defines a list of tab bar items. | ||
| 498 | When it contains 'tab-bar-format-global' (possibly appended after | ||
| 499 | 'tab-bar-format-align-right'), then after enabling 'display-time-mode' | ||
| 500 | (or any other mode that uses 'global-mode-string') it displays time | ||
| 501 | aligned to the right on the tab bar instead of the mode line. | ||
| 502 | |||
| 503 | --- | ||
| 497 | *** 'Mod-9' bound to 'tab-last' now switches to the last tab. | 504 | *** 'Mod-9' bound to 'tab-last' now switches to the last tab. |
| 498 | It also supports a negative argument. | 505 | It also supports a negative argument. |
| 499 | 506 | ||
diff --git a/lisp/tab-bar.el b/lisp/tab-bar.el index c95559a1b7d..c3955913e2d 100644 --- a/lisp/tab-bar.el +++ b/lisp/tab-bar.el | |||
| @@ -113,7 +113,21 @@ Possible modifier keys are `control', `meta', `shift', `hyper', `super' and | |||
| 113 | (unless (global-key-binding [(control shift tab)]) | 113 | (unless (global-key-binding [(control shift tab)]) |
| 114 | (global-set-key [(control shift tab)] 'tab-previous)) | 114 | (global-set-key [(control shift tab)] 'tab-previous)) |
| 115 | (unless (global-key-binding [(control shift iso-lefttab)]) | 115 | (unless (global-key-binding [(control shift iso-lefttab)]) |
| 116 | (global-set-key [(control shift iso-lefttab)] 'tab-previous))) | 116 | (global-set-key [(control shift iso-lefttab)] 'tab-previous)) |
| 117 | |||
| 118 | ;; Replace default value with a condition that supports displaying | ||
| 119 | ;; global-mode-string in the tab bar instead of the mode line. | ||
| 120 | (when (member '(global-mode-string ("" global-mode-string " ")) | ||
| 121 | mode-line-misc-info) | ||
| 122 | (setq mode-line-misc-info | ||
| 123 | (append '(global-mode-string | ||
| 124 | ("" (:eval (if (and tab-bar-mode | ||
| 125 | (memq 'tab-bar-format-global | ||
| 126 | tab-bar-format)) | ||
| 127 | "" global-mode-string)) | ||
| 128 | " ")) | ||
| 129 | (remove '(global-mode-string ("" global-mode-string " ")) | ||
| 130 | mode-line-misc-info))))) | ||
| 117 | 131 | ||
| 118 | (defun tab-bar--undefine-keys () | 132 | (defun tab-bar--undefine-keys () |
| 119 | "Uninstall key bindings previously bound by `tab-bar--define-keys'." | 133 | "Uninstall key bindings previously bound by `tab-bar--define-keys'." |
| @@ -503,56 +517,111 @@ the formatted tab name to display in the tab bar." | |||
| 503 | "")) | 517 | "")) |
| 504 | 'face (if current-p 'tab-bar-tab 'tab-bar-tab-inactive)))) | 518 | 'face (if current-p 'tab-bar-tab 'tab-bar-tab-inactive)))) |
| 505 | 519 | ||
| 506 | (defun tab-bar-make-keymap-1 () | 520 | (defvar tab-bar-format '(tab-bar-format-history |
| 507 | "Generate an actual keymap from `tab-bar-map', without caching." | 521 | tab-bar-format-tabs |
| 522 | tab-bar-separator | ||
| 523 | tab-bar-format-add-tab) | ||
| 524 | "Template for displaying tab bar items. | ||
| 525 | Every item in the list is a function that returns | ||
| 526 | a string, or a list of menu-item elements, or nil. | ||
| 527 | When you add more items `tab-bar-format-align-right' and | ||
| 528 | `tab-bar-format-global' to the end, then after enabling | ||
| 529 | `display-time-mode' (or any other mode that uses `global-mode-string') | ||
| 530 | it will display time aligned to the right on the tab bar instead of | ||
| 531 | the mode line.") | ||
| 532 | |||
| 533 | (defun tab-bar-format-history () | ||
| 534 | (when (and tab-bar-history-mode tab-bar-history-buttons-show) | ||
| 535 | `((sep-history-back menu-item ,(tab-bar-separator) ignore) | ||
| 536 | (history-back | ||
| 537 | menu-item ,tab-bar-back-button tab-bar-history-back | ||
| 538 | :help "Click to go back in tab history") | ||
| 539 | (sep-history-forward menu-item ,(tab-bar-separator) ignore) | ||
| 540 | (history-forward | ||
| 541 | menu-item ,tab-bar-forward-button tab-bar-history-forward | ||
| 542 | :help "Click to go forward in tab history")))) | ||
| 543 | |||
| 544 | (defun tab-bar-format-tabs () | ||
| 508 | (let ((separator (tab-bar-separator)) | 545 | (let ((separator (tab-bar-separator)) |
| 509 | (tabs (funcall tab-bar-tabs-function)) | 546 | (tabs (funcall tab-bar-tabs-function)) |
| 510 | (i 0)) | 547 | (i 0)) |
| 511 | (append | 548 | (mapcan |
| 512 | '(keymap (mouse-1 . tab-bar-handle-mouse)) | 549 | (lambda (tab) |
| 513 | (when (and tab-bar-history-mode tab-bar-history-buttons-show) | 550 | (setq i (1+ i)) |
| 514 | `((sep-history-back menu-item ,separator ignore) | 551 | (append |
| 515 | (history-back | 552 | `((,(intern (format "sep-%i" i)) menu-item ,separator ignore)) |
| 516 | menu-item ,tab-bar-back-button tab-bar-history-back | 553 | (cond |
| 517 | :help "Click to go back in tab history") | 554 | ((eq (car tab) 'current-tab) |
| 518 | (sep-history-forward menu-item ,separator ignore) | 555 | `((current-tab |
| 519 | (history-forward | 556 | menu-item |
| 520 | menu-item ,tab-bar-forward-button tab-bar-history-forward | 557 | ,(funcall tab-bar-tab-name-format-function tab i) |
| 521 | :help "Click to go forward in tab history"))) | 558 | ignore |
| 522 | (mapcan | 559 | :help "Current tab"))) |
| 523 | (lambda (tab) | 560 | (t |
| 524 | (setq i (1+ i)) | 561 | `((,(intern (format "tab-%i" i)) |
| 525 | (append | 562 | menu-item |
| 526 | `((,(intern (format "sep-%i" i)) menu-item ,separator ignore)) | 563 | ,(funcall tab-bar-tab-name-format-function tab i) |
| 527 | (cond | 564 | ,(or |
| 528 | ((eq (car tab) 'current-tab) | 565 | (alist-get 'binding tab) |
| 529 | `((current-tab | 566 | `(lambda () |
| 530 | menu-item | 567 | (interactive) |
| 531 | ,(funcall tab-bar-tab-name-format-function tab i) | 568 | (tab-bar-select-tab ,i))) |
| 532 | ignore | 569 | :help "Click to visit tab")))) |
| 533 | :help "Current tab"))) | 570 | `((,(if (eq (car tab) 'current-tab) 'C-current-tab (intern (format "C-tab-%i" i))) |
| 534 | (t | 571 | menu-item "" |
| 535 | `((,(intern (format "tab-%i" i)) | 572 | ,(or |
| 536 | menu-item | 573 | (alist-get 'close-binding tab) |
| 537 | ,(funcall tab-bar-tab-name-format-function tab i) | 574 | `(lambda () |
| 538 | ,(or | 575 | (interactive) |
| 539 | (alist-get 'binding tab) | 576 | (tab-bar-close-tab ,i))))))) |
| 540 | `(lambda () | 577 | tabs))) |
| 541 | (interactive) | 578 | |
| 542 | (tab-bar-select-tab ,i))) | 579 | (defun tab-bar-format-add-tab () |
| 543 | :help "Click to visit tab")))) | 580 | (when (and tab-bar-new-button-show tab-bar-new-button) |
| 544 | `((,(if (eq (car tab) 'current-tab) 'C-current-tab (intern (format "C-tab-%i" i))) | 581 | `((add-tab menu-item ,tab-bar-new-button tab-bar-new-tab |
| 545 | menu-item "" | 582 | :help "New tab")))) |
| 546 | ,(or | 583 | |
| 547 | (alist-get 'close-binding tab) | 584 | (defun tab-bar-format-align-right () |
| 548 | `(lambda () | 585 | "Align the rest of tab bar items to the right." |
| 549 | (interactive) | 586 | (let* ((rest (cdr (memq 'tab-bar-format-align-right tab-bar-format))) |
| 550 | (tab-bar-close-tab ,i))))))) | 587 | (rest (tab-bar-format-list rest)) |
| 551 | tabs) | 588 | (rest (mapconcat (lambda (item) (nth 2 item)) rest "")) |
| 552 | `((sep-add-tab menu-item ,separator ignore)) | 589 | (hpos (length rest)) |
| 553 | (when (and tab-bar-new-button-show tab-bar-new-button) | 590 | (str (propertize " " 'display `(space :align-to (- right ,hpos))))) |
| 554 | `((add-tab menu-item ,tab-bar-new-button tab-bar-new-tab | 591 | `((tab-bar-format-align-right menu-item ,str ignore)))) |
| 555 | :help "New tab")))))) | 592 | |
| 593 | (defun tab-bar-format-global () | ||
| 594 | "Format `global-mode-string' to display it in the tab bar. | ||
| 595 | When `tab-bar-format-global' is added to `tab-bar-format' | ||
| 596 | (possibly appended after `tab-bar-format-align-right'), | ||
| 597 | then modes that display information on the mode line | ||
| 598 | using `global-mode-string' will display the same text | ||
| 599 | on the tab bar instead." | ||
| 600 | `((tab-bar-format-global | ||
| 601 | menu-item | ||
| 602 | ,(format-mode-line global-mode-string) | ||
| 603 | ignore))) | ||
| 604 | |||
| 605 | (defun tab-bar-format-list (format-list) | ||
| 606 | (let ((i 0)) | ||
| 607 | (apply #'append | ||
| 608 | (mapcar | ||
| 609 | (lambda (format) | ||
| 610 | (setq i (1+ i)) | ||
| 611 | (cond | ||
| 612 | ((functionp format) | ||
| 613 | (let ((ret (funcall format))) | ||
| 614 | (when (stringp ret) | ||
| 615 | (setq ret `((,(intern (format "str-%i" i)) | ||
| 616 | menu-item ,ret ignore)))) | ||
| 617 | ret)))) | ||
| 618 | format-list)))) | ||
| 619 | |||
| 620 | (defun tab-bar-make-keymap-1 () | ||
| 621 | "Generate an actual keymap from `tab-bar-map', without caching." | ||
| 622 | (append | ||
| 623 | '(keymap (mouse-1 . tab-bar-handle-mouse)) | ||
| 624 | (tab-bar-format-list tab-bar-format))) | ||
| 556 | 625 | ||
| 557 | 626 | ||
| 558 | ;; Some window-configuration parameters don't need to be persistent. | 627 | ;; Some window-configuration parameters don't need to be persistent. |