Skip to content

Instantly share code, notes, and snippets.

@hjmr
Last active November 21, 2016 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hjmr/8a76f123bf242efaa834062385a5242f to your computer and use it in GitHub Desktop.
Save hjmr/8a76f123bf242efaa834062385a5242f to your computer and use it in GitHub Desktop.

[Emacs] Tabbar group handling

Requires 'tabbar.el'.

'Ctrl-C Ctrl-Tab' works as pressing the home button which shows groups in tabbar. The following key sequences 'Ctrl-Tab' and 'Ctrl-Shift-Tab' enable to move through groups forward and backward respectively. After a predefined period (3 seconds by default) the tabbar recovers to the normal mode.

(defvar my-tabbar-show-group-timer nil)

(defun my-tabbar-buffer-hide-groups ()
  "Hide groups"
  (global-set-key [(control tab)]        'tabbar-forward-tab)
  (global-set-key [(control shift tab)]  'tabbar-backward-tab)
  (setq my-tabbar-show-group-timer nil)
  (tabbar-buffer-show-groups nil)
  (tabbar-display-update))
;;
(defun my-tabbar-change-group (backward)
  "Change tab in the next/previous available group."
  (global-set-key [(control tab)]        'my-tabbar-forward-group)
  (global-set-key [(control shift tab)]  'my-tabbar-backward-group)
  (let ((tabbar-cycle-scope 'groups))
    (tabbar-cycle backward))
  (tabbar-buffer-show-groups t)
  (when my-tabbar-show-group-timer
    (cancel-timer my-tabbar-show-group-timer))
  (setq my-tabbar-show-group-timer
        (run-with-timer 3 nil 'my-tabbar-buffer-hide-groups)))
;;
(defun my-tabbar-backward-group ()
  "Go to selected tab in the previous available group."
  (interactive)
  (my-tabbar-change-group t))
;;
(defun my-tabbar-forward-group ()
  "Go to selected tab in the next available group."
  (interactive)
  (my-tabbar-change-group nil))
;;
(defun my-tabbar-press-home ()
  "Show groups on the tab"
  (interactive)
  (if tabbar--buffer-show-groups
      (my-tabbar-buffer-hide-groups)
    (progn
      (tabbar-buffer-show-groups t)
      (tabbar-display-update)
      (global-set-key [(control tab)]       'my-tabbar-forward-group)
      (global-set-key [(control shift tab)] 'my-tabbar-backward-group))))

(setq tabbar-cycle-scope 'tabs)
(setq tabbar-auto-scroll-flag t)
(global-set-key [(control c) (control tab)]        'my-tabbar-press-home)
(global-set-key [(control tab)]                    'tabbar-forward-tab)
(global-set-key [(control shift tab)]              'tabbar-backward-tab)

This greatly works with the buffer groups function like the followng:

(setq tabbar-buffer-groups-function
      (lambda ()
        (let ((dir (expand-file-name default-directory)))
          (cond ((member (buffer-name) '("*Completions*"
                                         "*scratch*"
                                         "*Messages*"
                                         "*Packages*"
                                         "*Buffer List*"
                                         "*eww*"))
                 (list "*misc*"))
                ((string-match "^\*epc con" (buffer-name))
                 (list "*misc*"))
                ((string-match-p "/.emacs.d/" dir)
                 (list ".emacs.d"))
                (t
                 (if (projectile-project-p)
                     (list (projectile-project-name))
                   (list
                    (tabbar-shorten (if (string-match (getenv "HOME") dir)
                                        (replace-match "~" nil t dir)
                                      dir) 40))))
                ))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment