Commit 97f24412 authored by Robert Cochran's avatar Robert Cochran Committed by Juri Linkov

Add hooks for after tab open, before close, and to prevent closing

* lisp/tab-bar.el (tab-bar-tab-post-open-functions,
tab-bar-tab-prevent-close-functions, tab-bar-tab-pre-close-functions):
New defcustoms
(tab-bar-new-tab-to, tab-bar-close-tab): Use new defcustoms
parent 8e1c5532
Pipeline #4267 failed with stage
in 64 minutes and 48 seconds
......@@ -692,6 +692,15 @@ If `rightmost', create as the last tab."
:group 'tab-bar
:version "27.1")
(defcustom tab-bar-tab-post-open-functions nil
"List of functions to call after creating a new tab.
The current tab is supplied as an argument. Any modifications
made to the tab argument will be applied after all functions are
called."
:type '(repeat function)
:group 'tab-bar
:version "27.1")
(defun tab-bar-new-tab-to (&optional to-index)
"Add a new tab at the absolute position TO-INDEX.
TO-INDEX counts from 1. If no TO-INDEX is specified, then add
......@@ -726,9 +735,13 @@ a new tab at the position specified by `tab-bar-new-tab-to'."
('right (1+ (or from-index 0)))))))
(setq to-index (max 0 (min (or to-index 0) (length tabs))))
(cl-pushnew to-tab (nthcdr to-index tabs))
(when (eq to-index 0)
;; pushnew handles the head of tabs but not frame-parameter
(set-frame-parameter nil 'tabs tabs)))
(set-frame-parameter nil 'tabs tabs))
(run-hook-with-args 'tab-bar-tab-post-open-functions
(nth to-index tabs)))
(when (and (not tab-bar-mode)
(or (eq tab-bar-show t)
......@@ -780,6 +793,24 @@ If the value is a function, call that function with the tab to be closed as an a
:group 'tab-bar
:version "27.1")
(defcustom tab-bar-tab-prevent-close-functions nil
"List of functions to call to determine whether to close a tab.
The tab to be closed and a boolean indicating whether or not it
is the only tab in the frame are supplied as arguments. If any
function returns a non-nil value, the tab will not be closed."
:type '(repeat function)
:group 'tab-bar
:version "27.1")
(defcustom tab-bar-tab-pre-close-functions nil
"List of functions to call before closing a tab.
The tab to be closed and a boolean indicating whether or not it
is the only tab in the frame are supplied as arguments,
respectively."
:type '(repeat function)
:group 'tab-bar
:version "27.1")
(defun tab-bar-close-tab (&optional arg to-index)
"Close the tab specified by its absolute position ARG.
If no ARG is specified, then close the current tab and switch
......@@ -792,11 +823,22 @@ TO-INDEX counts from 1."
(interactive "P")
(let* ((tabs (funcall tab-bar-tabs-function))
(current-index (tab-bar--current-tab-index tabs))
(close-index (if (integerp arg) (1- arg) current-index)))
(if (= 1 (length tabs))
(close-index (if (integerp arg) (1- arg) current-index))
(last-tab-p (= 1 (length tabs)))
(prevent-close (run-hook-with-args-until-success
'tab-bar-tab-prevent-close-functions
(nth close-index tabs)
last-tab-p)))
(unless prevent-close
(run-hook-with-args 'tab-bar-tab-pre-close-functions
(nth close-index tabs)
last-tab-p)
(if last-tab-p
(pcase tab-bar-close-last-tab-choice
('nil
(signal 'user-error '("Attempt to delete the sole tab in a frame")))
(user-error "Attempt to delete the sole tab in a frame"))
('delete-frame
(delete-frame))
('tab-bar-mode-disable
......@@ -837,7 +879,7 @@ TO-INDEX counts from 1."
(force-mode-line-update)
(unless tab-bar-mode
(message "Deleted tab and switched to %s" tab-bar-close-tab-select)))))
(message "Deleted tab and switched to %s" tab-bar-close-tab-select))))))
(defun tab-bar-close-tab-by-name (name)
"Close the tab by NAME."
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment