reddit / emacs

updating tab titles in tab-bar-mode

E3D3redit · 2022-06-01 14:35
How can I automatically update the titles (labels) of tabs in tab-bar-mode ? If I have the same buffer in 2 tabs, and kill this buffer in one of the tabs, then the other tab still has the title of the now closed buffer, until I visit that tab. How can I take care that the tab titles are always up-to-date ? (I use Emacs 27.1)
link0ff · 2022-06-01 18:30
You could add a hook to `kill-buffer-hook` that will use `tab-bar-get-buffer-tab` to get the tab with the same buffer, then visit it (that also updates its title), and switch back to the original tab.
E3D3redit · 2022-06-01 21:01
With only the most basic elisp skills I don't know what to do with your answer. If I knew enough elisp, reading the source code of `tab-bar-mode` would maybe provide an simple solution. `tab-bar-get-buffer-tab` returns a tab, but how do I switch to that tab without making it visible ? And how do I write a hook that does things before and after killing ? I expect that before the killing I need to store the name of the current buffer to search for tabs with simular name, and store the current tab index for jumping back after the killing and visiting other tabs. I guess I need to check ALL tab names because sometimes I have more than 2 tabs with the same name. I have no idea where to start solving these questions ? I just started learning Common Lisp with the hope that this will decrease my aversion for parenthesis and elisp but I guess I need much more to find my way in the 33699 functions and 13369 variables. Using `apropos` searches only in names, that are not always descriptive, e.g. `bolp`, while `apropos-documentation` finds more than often too much to read.
paretoOptimalDev · 2022-06-01 21:37
> `tab-bar-get-buffer-tab` returns a tab, but how do I switch to that tab without making it visible ? M-x describe-function <enter> save-excursion <enter>
link0ff · 2022-06-02 08:22
Do you think it would be simpler just to visit all tabs after killing a buffer to update all tabs data? They need to be visited ordered by recently to keep the original order with something like (defun update-tabs () (let ((current (tab-bar--current-tab-index))) (dolist (tab (nreverse (tab-bar--tabs-recent))) (tab-bar-select-tab (1+ (tab-bar--tab-index tab)))) (tab-bar-select-tab (1+ current)))) (add-hook 'kill-buffer-hook (lambda () (run-with-timer 0 nil 'update-tabs)))
E3D3redit · 2022-06-02 11:04
Yes, this seems simpler to me. I understand this immediatly. It seems to work as expected. The added timer to act when Emacs is idle, is also cool. I can't understand why I didn't find the solution in a Emacs wiki or with a search machine c.q. how it seems that other Emacs user can live with wrongly named tabs. I, my Emacs and tab-bar-mode thank you much for this adequate help.
E3D3redit · 2022-06-02 11:22
My docstring doesn't tell that `save-excursion` keeps the visited buffer or tab invisible, only that it restores point & buffer.
paretoOptimalDev · 2022-06-02 14:39
Right, so in emacs I don't know if there is a way to do something in the background as you describe. But if you wrap the code to do it in the foreground in save-excursion it should act like you want.
E3D3redit · 2022-06-02 18:34
I know that switching buffers with `set-buffer` doesn't display the buffer, but I don't know if this also update the corresponding tab(s).