From c1daeb4c2826af28311a08100e98948349715c37 Mon Sep 17 00:00:00 2001 From: "James N. V. Cash" Date: Tue, 5 Jan 2021 20:35:35 +0200 Subject: [PATCH] Refactor tab-bar-mode to -define-keys and -load-buttons (bug#42052) * lisp/tab-bar.el (tab-bar--define-keys, tab-bar--load-buttons): Move some code here from 'tab-bar-mode'. (tab-bar-new-tab-to): Call tab-bar--load-buttons and tab-bar--define-keys. Copyright-paperwork-exempt: yes --- lisp/tab-bar.el | 79 +++++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 35 deletions(-) diff --git a/lisp/tab-bar.el b/lisp/tab-bar.el index 935c97e2a41..93f3c550ceb 100644 --- a/lisp/tab-bar.el +++ b/lisp/tab-bar.el @@ -95,23 +95,26 @@ Possible modifier keys are `control', `meta', `shift', `hyper', `super' and :version "27.1") -(define-minor-mode tab-bar-mode - "Toggle the tab bar in all graphical frames (Tab Bar mode)." - :global t - ;; It's defined in C/cus-start, this stops the d-m-m macro defining it again. - :variable tab-bar-mode - (let ((val (if tab-bar-mode 1 0))) - (dolist (frame (frame-list)) - (set-frame-parameter frame 'tab-bar-lines val)) - ;; If the user has given `default-frame-alist' a `tab-bar-lines' - ;; parameter, replace it. - (if (assq 'tab-bar-lines default-frame-alist) - (setq default-frame-alist - (cons (cons 'tab-bar-lines val) - (assq-delete-all 'tab-bar-lines - default-frame-alist))))) - - (when (and tab-bar-mode tab-bar-new-button +(defun tab-bar--define-keys () + "Install key bindings for switching between tabs if the user has configured them." + (when tab-bar-select-tab-modifiers + (global-set-key (vector (append tab-bar-select-tab-modifiers (list ?0))) + 'tab-bar-switch-to-recent-tab) + (dotimes (i 9) + (global-set-key (vector (append tab-bar-select-tab-modifiers + (list (+ i 1 ?0)))) + 'tab-bar-select-tab))) + ;; Don't override user customized key bindings + (unless (global-key-binding [(control tab)]) + (global-set-key [(control tab)] 'tab-next)) + (unless (global-key-binding [(control shift tab)]) + (global-set-key [(control shift tab)] 'tab-previous)) + (unless (global-key-binding [(control shift iso-lefttab)]) + (global-set-key [(control shift iso-lefttab)] 'tab-previous))) + +(defun tab-bar--load-buttons () + "Load the icons for the tab buttons." + (when (and tab-bar-new-button (not (get-text-property 0 'display tab-bar-new-button))) ;; This file is pre-loaded so only here we can use the right data-directory: (add-text-properties 0 (length tab-bar-new-button) @@ -121,7 +124,7 @@ Possible modifier keys are `control', `meta', `shift', `hyper', `super' and :ascent center)) tab-bar-new-button)) - (when (and tab-bar-mode tab-bar-close-button + (when (and tab-bar-close-button (not (get-text-property 0 'display tab-bar-close-button))) ;; This file is pre-loaded so only here we can use the right data-directory: (add-text-properties 0 (length tab-bar-close-button) @@ -129,24 +132,27 @@ Possible modifier keys are `control', `meta', `shift', `hyper', `super' and :file "tabs/close.xpm" :margin (2 . 0) :ascent center)) - tab-bar-close-button)) + tab-bar-close-button))) +(define-minor-mode tab-bar-mode + "Toggle the tab bar in all graphical frames (Tab Bar mode)." + :global t + ;; It's defined in C/cus-start, this stops the d-m-m macro defining it again. + :variable tab-bar-mode + (let ((val (if tab-bar-mode 1 0))) + (dolist (frame (frame-list)) + (set-frame-parameter frame 'tab-bar-lines val)) + ;; If the user has given `default-frame-alist' a `tab-bar-lines' + ;; parameter, replace it. + (if (assq 'tab-bar-lines default-frame-alist) + (setq default-frame-alist + (cons (cons 'tab-bar-lines val) + (assq-delete-all 'tab-bar-lines + default-frame-alist))))) + (when tab-bar-mode + (tab-bar--load-buttons)) (if tab-bar-mode - (progn - (when tab-bar-select-tab-modifiers - (global-set-key (vector (append tab-bar-select-tab-modifiers (list ?0))) - 'tab-bar-switch-to-recent-tab) - (dotimes (i 9) - (global-set-key (vector (append tab-bar-select-tab-modifiers - (list (+ i 1 ?0)))) - 'tab-bar-select-tab))) - ;; Don't override user customized key bindings - (unless (global-key-binding [(control tab)]) - (global-set-key [(control tab)] 'tab-next)) - (unless (global-key-binding [(control shift tab)]) - (global-set-key [(control shift tab)] 'tab-previous)) - (unless (global-key-binding [(control shift iso-lefttab)]) - (global-set-key [(control shift iso-lefttab)] 'tab-previous))) + (tab-bar--define-keys) ;; Unset only keys bound by tab-bar (when (eq (global-key-binding [(control tab)]) 'tab-next) (global-unset-key [(control tab)])) @@ -815,7 +821,10 @@ After the tab is created, the hooks in ((and (natnump tab-bar-show) (> (length (funcall tab-bar-tabs-function)) tab-bar-show) (zerop (frame-parameter nil 'tab-bar-lines))) - (set-frame-parameter nil 'tab-bar-lines 1))) + (progn + (tab-bar--load-buttons) + (tab-bar--define-keys) + (set-frame-parameter nil 'tab-bar-lines 1)))) (force-mode-line-update) (unless tab-bar-mode -- 2.39.5