emacs tabs

This commit is contained in:
Ezri 2024-04-16 14:41:19 -06:00
parent 7c350c7dd1
commit f669162de2
Signed by: ezri
GPG Key ID: 058A78E5680C6F24
3 changed files with 165 additions and 12 deletions

View File

@ -1,5 +1,9 @@
{
"default_context": "personal",
"display_ordering": ["builtin"],
"display_layout": {
"builtin": "eDP-1"
},
"contexts": {
"personal": {
"builtin": [
@ -12,8 +16,9 @@
{
"index": 2,
"name": "code",
"exec": "console",
"program_name": "console"
"exec": "emacsclient",
"args": ["-nc"],
"program_name": "emacsclient"
},
{
"index": 3,

View File

@ -48,9 +48,12 @@ precmd_vcs_info() {
# whether this file is sourced multiple times, we don't end up with duplicate
# entries which will slow down the prompt.
precmd_functions=()
if ! [[ ${TERM} == "dumb" ]]; then
if [[ ${TERM} == "alacritty" ]]; then
precmd_functions+=(precmd_vcs_info precmd_pyenv_info _reset_window_name)
setopt prompt_subst
elif ! [[ ${TERM} == "dumb" ]]; then
precmd_functions+=(precmd_vcs_info precmd_pyenv_info)
setopt prompt_subst
fi
eval $(awk '{print "OS_" $0}' /etc/os-release)

View File

@ -13,15 +13,6 @@ Namely to indent style, theme, and line numbers. Also added Github Copilot.
'("melpa" . "https://melpa.org/packages/") t)
#+END_SRC
* General emacs
** Tab bar mode
#+BEGIN_SRC emacs-lisp
(defun my-tabbar-buffer-groups () ;; customize to show all normal files in one group
(list (cond ((string-equal "*" (substring (buffer-name) 0 1)) "emacs")
((eq major-mode 'dired-mode) "emacs")
(t "user"))))
(setq tabbar-buffer-groups-function 'my-tabbar-buffer-groups)
(tab-bar-mode)
#+END_SRC
** Indentation
Indent using tabs, render with tab-width of 2.
#+BEGIN_SRC emacs-lisp
@ -171,6 +162,160 @@ Indent using tabs, render with tab-width of 2.
(use-package all-the-icons
:ensure t)
#+END_SRC
** Tabs
#+BEGIN_SRC emacs-lisp
(defun my/set-tab-theme ()
(let ((bg (face-attribute 'mode-line :background))
(fg (face-attribute 'default :foreground))
(hg (face-attribute 'default :background))
(base (face-attribute 'mode-line :background))
(box-width (/ (line-pixel-height) 4)))
(set-face-attribute 'tab-line nil
:background base
:foreground fg
:height 0.8
:inherit nil
:box (list :line-width -1 :color base)
)
(set-face-attribute 'tab-line-tab nil
:foreground fg
:background bg
:weight 'normal
:inherit nil
:box (list :line-width box-width :color bg))
(set-face-attribute 'tab-line-tab-inactive nil
:foreground fg
:background base
:weight 'normal
:inherit nil
:box (list :line-width box-width :color base))
(set-face-attribute 'tab-line-highlight nil
:foreground fg
:background hg
:weight 'normal
:inherit nil
:box (list :line-width box-width :color hg))
(set-face-attribute 'tab-line-tab-current nil
:foreground fg
:background hg
:weight 'normal
:inherit nil
:box (list :line-width box-width :color hg))))
(defun my/tab-line-name-buffer (buffer &rest _buffers)
"Create name for tab with padding and truncation.
If buffer name is shorter than `tab-line-tab-max-width' it gets
centered with spaces, otherwise it is truncated, to preserve
equal width for all tabs. This function also tries to fit as
many tabs in window as possible, so if there are no room for tabs
with maximum width, it calculates new width for each tab and
truncates text if needed. Minimal width can be set with
`tab-line-tab-min-width' variable."
(with-current-buffer buffer
(let* ((window-width (window-width (get-buffer-window)))
(tab-amount (length (tab-line-tabs-window-buffers)))
(window-max-tab-width (if (>= (* (+ tab-line-tab-max-width 3) tab-amount) window-width)
(/ window-width tab-amount)
tab-line-tab-max-width))
(tab-width (- (cond ((> window-max-tab-width tab-line-tab-max-width)
tab-line-tab-max-width)
((< window-max-tab-width tab-line-tab-min-width)
tab-line-tab-min-width)
(t window-max-tab-width))
3)) ;; compensation for ' x ' button
(buffer-name (string-trim (buffer-name)))
(name-width (length buffer-name)))
(if (>= name-width tab-width)
(concat " " (truncate-string-to-width buffer-name (- tab-width 2)) "…")
(let* ((padding (make-string (+ (/ (- tab-width name-width) 2) 1) ?\s))
(buffer-name (concat padding buffer-name)))
(concat buffer-name (make-string (- tab-width (length buffer-name)) ?\s)))))))
(defun tab-line-close-tab (&optional e)
"Close the selected tab.
If tab is presented in another window, close the tab by using
`bury-buffer` function. If tab is unique to all existing
windows, kill the buffer with `kill-buffer` function. Lastly, if
no tabs left in the window, it is deleted with `delete-window`
function."
(interactive "e")
(let* ((posnp (event-start e))
(window (posn-window posnp))
(buffer (get-pos-property 1 'tab (car (posn-string posnp)))))
(with-selected-window window
(let ((tab-list (tab-line-tabs-window-buffers))
(buffer-list (flatten-list
(seq-reduce (lambda (list window)
(select-window window t)
(cons (tab-line-tabs-window-buffers) list))
(window-list) nil))))
(select-window window)
(if (> (seq-count (lambda (b) (eq b buffer)) buffer-list) 1)
(progn
(if (eq buffer (current-buffer))
(bury-buffer)
(set-window-prev-buffers window (assq-delete-all buffer (window-prev-buffers)))
(set-window-next-buffers window (delq buffer (window-next-buffers))))
(unless (cdr tab-list)
(ignore-errors (delete-window window))))
(and (kill-buffer buffer)
(unless (cdr tab-list)
(ignore-errors (delete-window window)))))))))
(unless (version< emacs-version "27")
(use-package tab-line
:ensure nil
:hook (after-init . global-tab-line-mode)
:config
(defcustom tab-line-tab-min-width 10
"Minimum width of a tab in characters."
:type 'integer
:group 'tab-line)
(defcustom tab-line-tab-max-width 30
"Maximum width of a tab in characters."
:type 'integer
:group 'tab-line)
(setq tab-line-close-button-show t
tab-line-new-button-show nil
tab-line-separator ""
tab-line-tab-name-function #'my/tab-line-name-buffer
tab-line-right-button (propertize (if (char-displayable-p ?▶) " ▶ " " > ")
'keymap tab-line-right-map
'mouse-face 'tab-line-highlight
'help-echo "Click to scroll right")
tab-line-left-button (propertize (if (char-displayable-p ?◀) " ◀ " " < ")
'keymap tab-line-left-map
'mouse-face 'tab-line-highlight
'help-echo "Click to scroll left")
tab-line-close-button (propertize (if (char-displayable-p ?×) " × " " x ")
'keymap tab-line-tab-close-map
'mouse-face 'tab-line-close-highlight
'help-echo "Click to close tab"))
(my/set-tab-theme)
;;(dolist (mode '(ediff-mode process-menu-mode term-mode vterm-mode))
;;(add-to-list 'tab-line-exclude-modes mode))
(dolist (mode '(ediff-mode process-menu-mode))
(add-to-list 'tab-line-exclude-modes mode))
))
(defun my-tab-config (&optional frame)
"if frame is a terminal, disable tab-line-mode"
(when (not (display-graphic-p frame))
(tab-line-mode nil)))
(global-tab-line-mode t)
(add-hook 'after-make-frame-functions 'my-tab-config)
#+END_SRC
* Projectile
#+BEGIN_SRC emacs-lisp