unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: tumashu  <tumashu@163.com>
To: "Juri Linkov" <juri@linkov.net>
Cc: "emacs-devel@gnu.org" <emacs-devel@gnu.org>
Subject: Re:Re: [patch] two patchs about tab-bar.el
Date: Sat, 11 Sep 2021 02:33:51 +0800 (CST)	[thread overview]
Message-ID: <79cc8889.2b.17bd0fcc54e.Coremail.tumashu@163.com> (raw)
In-Reply-To: <87o8916jqb.fsf@mail.linkov.net>

This is my tab-bar config

---------------------------------------------------------------------

;; ** buffer 显示方式
(setq switch-to-buffer-obey-display-actions t)
(setq display-buffer-alist
      '((eh-display-buffer-in-tab-p
         display-buffer-in-tab
         (reusable-frames . visible))))

(defun eh-display-buffer-in-tab-p (buffer-name alist)
  (with-current-buffer (get-buffer buffer-name)
    (and
     ;; Minibuffer.
     (not (minibufferp buffer-name))
     ;; Python, Term 等 buffer.
     (not (derived-mode-p 'comint-mode))
     (not (derived-mode-p 'term-mode))
     ;; magit 相关 buffer
     (not (string-match-p "magit" buffer-name))
     (not (string-match-p "COMMIT_EDITMSG" buffer-name))
     ;; gnus 相关 buffer
     (not (derived-mode-p 'gnus-article-mode))
     ;; 隐藏的 buffer
     (not (string-match-p "^ +" buffer-name)))))

;; ** 开启 tab-bar
(tab-bar-mode 1)
(tab-bar-history-mode 1)
(global-set-key (kbd "C-t") 'tab-bar-switch-to-recent-tab)
(global-set-key (kbd "C-c t") 'tab-switcher)
(setq tab-bar-format
      '(tab-bar-format-tabs
        tab-bar-separator))
(setq tab-bar-separator " ")
(setq tab-bar-tab-hints t)
(setq tab-bar-close-button-show 'selected)
(setq tab-bar-tab-name-function
      #'eh-tab-bar-tab-name)

(defun eh-tab-bar-tab-name ()
  (mapconcat (lambda (buf)
               (let ((tab-name (buffer-name buf))
                     (length 20))
                 (if (< (string-width tab-name) length)
                     tab-name
                   (propertize (truncate-string-to-width
                                tab-name length nil nil
                                tab-bar-tab-name-ellipsis)
                               'help-echo tab-name))))
             (delete-dups (mapcar #'window-buffer
                                  (window-list-1 (frame-first-window)
                                                 'nomini)))
             ", "))

(defun eh-tab-bar-rename-tab ()
  (tab-bar-rename-tab ""))

(add-hook 'window-configuration-change-hook #'eh-tab-bar-rename-tab)
(add-hook 'buffer-list-update-hook #'eh-tab-bar-rename-tab)

(setq tab-bar-tab-name-format-function
      #'eh-tab-bar-tab-name-format-default)

(defun eh-tab-bar-tab-name-format-default (tab i)
  (let ((current-p (eq (car tab) 'current-tab)))
    (propertize
     (concat (if tab-bar-tab-hints (format "[%d] " i) "")
             (alist-get 'name tab)
             (or (and tab-bar-close-button-show
                      (not (eq tab-bar-close-button-show
                               (if current-p 'non-selected 'selected)))
                      tab-bar-close-button)
                 ""))
     'face (funcall tab-bar-tab-face-function tab))))

(defun eh-buffer-used-by-org-agenda-p (buffer)
  (and (get-buffer "*Org Agenda*")
       (with-current-buffer buffer
         (and (derived-mode-p 'org-mode)
              (member (buffer-file-name)
                      (org-agenda-files))))))

(defun eh-kill-buffer (buffer)
  (interactive)
  (let ((buffer-name (buffer-name buffer)))
    (cond
     ((equal buffer-name "*scratch*")
      (message "NOTE: do not kill *scratch* buffer."))
     ((eh-buffer-used-by-org-agenda-p buffer)
      (message "NOTE: buffer %S is used by org-agenda, do not kill it." buffer-name))
     ((tab-bar-get-buffer-tab buffer t t)
      (message "Warn: buffer %S has been managed by other tab, do not kill it." buffer-name))
     (t (message "Kill buffer %S." buffer-name)
        (kill-buffer buffer)))))

(defun eh-tab-bar-close-tab (orig-func &rest args)
  (interactive)
  (let* ((use-dialog-box t)
         (n (length (window-list)))
         (buffers (mapcar #'window-buffer (window-list)))
         (tabs (funcall tab-bar-tabs-function))
         (current-index (tab-bar--current-tab-index tabs))
         (close-index (when (integerp (car args)) (1- (car args)))))
    (if (or (not close-index)
            (eq close-index current-index))
        ;; 如果要关闭的 tab 是 current tab, 就将 buffer 也删除。
        (progn
          (cond ((memq this-command
                       '(tab-bar-close-tab
                         tab-close))
                 (eh-kill-buffer (current-buffer)))
                ((memq this-command
                       '(tab-bar-mouse-close-tab
                         tab-bar-mouse-select-tab))
                 (dolist (buffer buffers)
                   (eh-kill-buffer buffer)))
                (t (message "Note: do not kill any buffer.")))
          (let ((tab-bar-close-last-tab-choice
                 `(@,tab-bar-close-last-tab-choice eh-tab-bar-rename-tab)))
            (if (and (equal this-command 'tab-bar-close-tab)
                     (> n 1))
                (funcall-interactively #'delete-window)
              (apply orig-func args))))
      ;; 如果删除的 tab 不是 current tab, 就不删除任何 buffer.
      (apply orig-func args))))

(advice-add 'tab-bar-close-tab :around #'eh-tab-bar-close-tab)

(global-set-key (kbd "C-x k") 'tab-bar-close-tab)

















At 2021-09-10 14:33:48, "Juri Linkov" <juri@linkov.net> wrote:
>> * lisp/tab-bar.el (tab-bar-get-buffer-tab): Fix issue when
>> 'current-tab is not at the beginning of tabs.
>>
>> --- a/lisp/tab-bar.el
>> +++ b/lisp/tab-bar.el
>> @@ -1940,7 +1940,9 @@ tab-bar-get-buffer-tab
>> -          (funcall tab-bar-tabs-function frame)))
>> +          (let ((tabs (funcall tab-bar-tabs-function frame)))
>> +            ;; Make sure current-tab is alway at the beginning of tabs.
>> +            (push (assq 'current-tab tabs) tabs))))
>
>Please explain what problem this patch is intended to fix.
>
>> --- a/lisp/tab-bar.el
>> +++ b/lisp/tab-bar.el
>> @@ -1962,11 +1962,12 @@ display-buffer-in-tab
>>
>>  If ALIST contains a `reusable-frames' entry, its value determines
>>  which frames to search for a reusable tab:
>> -  nil -- the selected frame (actually the last non-minibuffer frame)
>> +  nil -- do not reuse any frames.
>>    A frame   -- just that frame
>>    `visible' -- all visible frames
>>    0   -- all frames on the current terminal
>>    t   -- all frames.
>> +  others -- selected frame.
>
>I wonder where did you get the value 'others' that means the selected frame?

  parent reply	other threads:[~2021-09-10 18:33 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-10  4:39 [patch] two patchs about tab-bar.el tumashu
2021-09-10  6:33 ` Juri Linkov
2021-09-10 13:18   ` tumashu
2021-09-10 13:24   ` tumashu
2021-09-10 13:36   ` tumashu
2021-09-10 16:17     ` Juri Linkov
2021-09-10 18:33   ` tumashu [this message]
2021-09-12  7:03     ` Juri Linkov
2021-09-12  9:34       ` tumashu
2021-09-12 16:10         ` Juri Linkov
2021-09-10 23:48 ` Feng Shu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=79cc8889.2b.17bd0fcc54e.Coremail.tumashu@163.com \
    --to=tumashu@163.com \
    --cc=emacs-devel@gnu.org \
    --cc=juri@linkov.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).