Skip to content

Instantly share code, notes, and snippets.

@bravosierrasierra
Last active October 28, 2017 15:59
Show Gist options
  • Save bravosierrasierra/daf7846632b7b4944811018ed36dd360 to your computer and use it in GitHub Desktop.
Save bravosierrasierra/daf7846632b7b4944811018ed36dd360 to your computer and use it in GitHub Desktop.
*** tab bar
[[https://www.emacswiki.org/emacs/TabBarMode][EmacsWiki: Tab Bar Mode]]
#+begin_src emacs-lisp
(use-package tabbar
:config
; turn on the tabbar
(tabbar-mode -1)
;; (setq tabbar-use-images nil)
(setq tabbar-use-images nil)
; define all tabs to be one of 3 possible groups: “Emacs Buffer”, “Dired”,
;“User Buffer”.
(global-set-key [M-s-left] 'tabbar-backward)
(global-set-key [M-s-right] 'tabbar-forward)
(defun tabbar-buffer-groups ()
"Return the list of group names the current buffer belongs to.
This function is a custom function for tabbar-mode's tabbar-buffer-groups.
This function group all buffers into 3 groups:
Those Dired, those user buffer, and those emacs buffer.
Emacs buffer are those starting with “*”."
(list
(cond
((eq major-mode 'dired-mode)
"Dired"
)
((or (eq major-mode 'web-mode)
(eq major-mode 'js2-mode))
"Web-Mode"
)
((or (eq major-mode 'org-mode)
(eq major-mode 'org-agenda-mode))
"Org-Mode"
)
((string-equal "*" (substring (buffer-name) 0 1))
"Emacs Buffer"
)
(t
;; "User Buffer"
(if (and (stringp mode-name)
;; Take care of preserving the match-data because this
;; function is called when updating the header line.
(save-match-data (string-match "[^ ]" mode-name)))
mode-name
(symbol-name major-mode))
)
)))
(setq tabbar-buffer-groups-function 'tabbar-buffer-group)
(setq tbbr-md "groups")
(defun toggle-tabbar-mode ()
"Toggles tabbar modes - all buffers vs. defined in the `tabbar-buffer-groups'."
(interactive)
(if (string= tbbr-md "groups")
(progn ;; then
(setq tabbar-buffer-groups-function
(lambda ()
(list "All")))
(setq tbbr-md "all"))
(progn ;; else
(setq tabbar-buffer-groups-function 'tabbar-buffer-groups)
(setq tbbr-md "groups"))))
;; Sort tabbar buffers by name
(defun tabbar-add-tab (tabset object &optional append_ignored)
"Add to TABSET a tab with value OBJECT if there isn't one there yet.
If the tab is added, it is added at the beginning of the tab list,
unless the optional argument APPEND is non-nil, in which case it is
added at the end."
(let ((tabs (tabbar-tabs tabset)))
(if (tabbar-get-tab object tabset)
tabs
(let ((tab (tabbar-make-tab object tabset)))
(tabbar-set-template tabset nil)
(set tabset (sort (cons tab tabs)
(lambda (a b) (string< (buffer-name (car a)) (buffer-name (car b))))))))))
;; Add a buffer modification state indicator in the label
;; Add a buffer modification state indicator in the tab label, and place a
;; space around the label to make it looks less crowd.
(defadvice tabbar-buffer-tab-label (after fixup_tab_label_space_and_flag activate)
(setq ad-return-value
(if (and (buffer-modified-p (tabbar-tab-value tab))
(buffer-file-name (tabbar-tab-value tab)))
(concat " * " (concat ad-return-value " "))
(concat " " (concat ad-return-value " ")))))
;; Called each time the modification state of the buffer changed.
(defun ztl-modification-state-change ()
(tabbar-set-template tabbar-current-tabset nil)
(tabbar-display-update))
;; First-change-hook is called BEFORE the change is made.
(defun ztl-on-buffer-modification ()
(set-buffer-modified-p t)
(ztl-modification-state-change))
(add-hook 'after-save-hook 'ztl-modification-state-change)
;; This doesn't work for revert, I don't know.
;;(add-hook 'after-revert-hook 'ztl-modification-state-change)
(add-hook 'first-change-hook 'ztl-on-buffer-modification)
)
#+end_src
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment