.emacs.d/init.d/basics/buffers.el
2020-11-26 12:47:47 +01:00

128 lines
5.5 KiB
EmacsLisp

;;; buffers.el --- Default settings for buffers. -*- lexical-binding: t; -*-
;; Time-stamp: <2020-11-26T12:47:14+0100>
;;; Commentary:
;; * Setup scratch buffer.
;; * Set fill-column.
;; * Define which buffers should be ignored.
;; * Enable auto-fill-mode.
;;; Code:
(require 'basics/package-management)
(require 'misc/libs)
(use-package emacs
:straight (:type built-in)
:demand t
:custom ((initial-scratch-message nil) ; Make scratch buffer empty,
(initial-major-mode 'gfm-mode)) ; and select mode.
:config (progn
(defvar my/skippable-buffers
'("^\\*" "^magit[:-]" "^Pfuture-Callback")
"Buffer names ignored by `next-buffer' and `previous-buffer'.")
(defvar my/never-skippable-buffers
'("^\\*scratch\\*$" "^\\*Easy-hugo\\*$")
"Buffer names never ignored by `next-buffer' and `previous-buffer'.")
(defun my/buffer-predicate (buffer)
"Return nil if buffer-name matches expression in `my/skippable-buffers'."
(catch 'return
;; Return t if buffer-name is on never-skippable list.
(dolist (expression my/never-skippable-buffers)
(if (string-match expression (buffer-name buffer))
(throw 'return t)))
;; Return nil if buffer-name is on skippable list.
(dolist (expression my/skippable-buffers)
(if (string-match expression (buffer-name buffer))
(throw 'return nil)))
t))
(set-frame-parameter nil 'buffer-predicate 'my/buffer-predicate)
; Doesn't work with use-package?
(add-hook 'after-make-frame-functions
(lambda (frame)
(set-frame-parameter
frame 'buffer-predicate 'my/buffer-predicate)))
(defun my/truncate-lines ()
"Truncate lines in local buffer instead of wrapping them."
(setq-local truncate-lines t)))
:bind (("M-<left>" . previous-buffer)
("M-<right>" . next-buffer))
:hook ((prog-mode . my/truncate-lines)
(conf-mode . my/truncate-lines)
(emacs-startup . (lambda () (with-current-buffer "*scratch*"
(auto-fill-mode -1))))))
;; Show and select buffers. Minimal interface for recovery situations.
(use-package bs
:bind ("C-x C-b" . bs-show))
;; Delete old buffers.
;; https://www.emacswiki.org/emacs/CleanBufferList
(use-package midnight
:defer 4
:init (setq midnight-delay 30 ; 30 seconds after “midnight”.
midnight-period (* 2 60 60)) ; Clean every 2 hours.
:custom ((clean-buffer-list-delay-general 1) ; Clean normal bufs after 1d,
(clean-buffer-list-delay-special (* 30 60))) ; special after 30m.
:config (progn
(setq clean-buffer-list-kill-regexps ; Add these to special buffers.
(nconc clean-buffer-list-kill-regexps
'("^magit-?.*:"
"\\.log$"
"^\\*rdm\\*$"
"^\\*Backtrace\\*$"
"^Pfuture-Callback"
)))
(midnight-mode t)))
;; Dedicate windows to “purposes”.
(use-package window-purpose
:demand t ; projectile depends on window-purpose.
:diminish purpose-mode
:custom (purpose-x-popwin-position 'right)
:config (progn
(purpose-mode)
(add-to-list 'purpose-user-mode-purposes '(c++-mode . cpp))
(add-to-list 'purpose-user-mode-purposes '(qml-mode . cpp))
(add-to-list 'purpose-user-mode-purposes '(emacs-lisp-mode . elisp))
(add-to-list 'purpose-user-mode-purposes '(ebuild-mode . ebuild))
(add-to-list 'purpose-user-mode-purposes '(adoc-mode . adoc))
(add-to-list 'purpose-user-mode-purposes '(markdown-mode . md))
(add-to-list 'purpose-user-mode-purposes '(gfm-mode . md))
(add-to-list 'purpose-user-mode-purposes '(Man-mode . help-buf))
(add-to-list 'purpose-user-mode-purposes '(help-mode . help-buf))
(add-to-list 'purpose-user-mode-purposes '(info-mode . help-buf))
(add-to-list 'purpose-user-mode-purposes
'(compilation-mode . compile))
(add-to-list 'purpose-special-action-sequences
'(help-buf purpose-display-reuse-window-buffer))
(purpose-compile-user-configuration)
(require 'window-purpose-x)
;; Assign all Magit buffers the same purpose.
(purpose-x-magit-single-on)
(purpose-x-kill-setup) ; Replace killed buffers with same purpose.
(purpose-x-popwin-setup) ; FIXME: Doesn't seem to work.
(push 'Man-mode purpose-x-popwin-major-modes)
(purpose-x-popwin-update-conf))
:bind (("<f2>" . purpose-toggle-window-purpose-dedicated)
("C-<f2>" . purpose-toggle-window-buffer-dedicated)
("M-<f2>" . purpose-load-window-layout)))
;; Visual feedback on some operations by highlighting region.
(use-package volatile-highlights
:diminish volatile-highlights-mode
:after (doom-themes display-line-numbers)
:config (progn
(set-face-background 'vhl/default-face
(hexrgb-increment-equal-rgb
(face-background 'default) 2 8)))
:hook (window-setup . volatile-highlights-mode))
(provide 'basics/buffers)
;;; buffers.el ends here