;;; init.el --- bandali's emacs configuration -*- lexical-binding: t -*- ;; Copyright (c) 2018-2025 Amin Bandali ;; This program is free software: you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; This program is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with this program. If not, see . ;;; Commentary: ;; bandali's opinionated GNU Emacs configs. I tend to use the latest ;; development trunk of emacs.git, but I try to maintain backward ;; compatibility with a few of the recent older GNU Emacs releases ;; so I could easily reuse it on machines stuck with older Emacsen. ;;; Code: (setq use-package-verbose init-file-debug use-package-expand-minimally (not init-file-debug) use-package-compute-statistics init-file-debug debug-on-error init-file-debug debug-on-quit init-file-debug) (require 'package) (when (< emacs-major-version 29) (unless (package-installed-p 'use-package) (unless package-archive-contents (package-refresh-contents)) (package-install 'use-package))) ;; whoami (setq user-full-name "Amin Bandali" user-mail-address "bandali@kelar.org") ;;; Initial setup (eval-and-compile (defsubst b/emacs.d (path) "Expand path PATH relative to `user-emacs-directory'." (expand-file-name (convert-standard-filename path) user-emacs-directory)) ;; Wrappers around the new keybinding functions, with fallback to ;; the corresponding older lower level function on older Emacsen. (defsubst b/keymap-set (keymap key definition) (if (version< emacs-version "29") (define-key keymap (kbd key) definition) (keymap-set keymap key definition))) (defsubst b/keymap-global-set (key command) (if (version< emacs-version "29") (global-set-key (kbd key) command) (keymap-global-set key command))) (defsubst b/keymap-local-set (key command) (if (version< emacs-version "29") (local-set-key (kbd key) command) (keymap-local-set key command))) (defsubst b/keymap-global-unset (key) (if (version< emacs-version "29") (global-unset-key (kbd key)) (keymap-global-unset key 'remove))) (defsubst b/keymap-local-unset (key) (if (version< emacs-version "29") (local-unset-key (kbd key)) (keymap-local-unset key 'remove))) (when (version< emacs-version "29") ;; Emacs 29 introduced the handy `setopt' macro for setting user ;; options (defined with `defcustom') with a syntax similar to ;; `setq'. So, we define it on older Emacsen that don't have it. (defmacro setopt (&rest pairs) "Set VARIABLE/VALUE pairs, and return the final VALUE. This is like `setq', but is meant for user options instead of plain variables. This means that `setopt' will execute any `custom-set' form associated with VARIABLE. \(fn [VARIABLE VALUE]...)" (declare (debug setq)) (unless (zerop (mod (length pairs) 2)) (error "PAIRS must have an even number of variable/value members")) (let ((expr nil)) (while pairs (unless (symbolp (car pairs)) (error "Attempting to set a non-symbol: %s" (car pairs))) (push `(setopt--set ',(car pairs) ,(cadr pairs)) expr) (setq pairs (cddr pairs))) (macroexp-progn (nreverse expr)))) (defun setopt--set (variable value) (custom-load-symbol variable) ;; Check that the type is correct. (when-let ((type (get variable 'custom-type))) (unless (widget-apply (widget-convert type) :match value) (warn "Value `%S' does not match type %s" value type))) (put variable 'custom-check-value (list value)) (funcall (or (get variable 'custom-set) #'set-default) variable value)))) ;; Separate custom file (don't want it mixing with init.el). (setopt custom-file (b/emacs.d "custom.el")) (with-eval-after-load 'custom (load custom-file 'noerror)) ;; Start Emacs server ;; (https://www.gnu.org/software/emacs/manual/html_node/emacs/Emacs-Server.html) (run-with-idle-timer 0.5 nil #'require 'server) (with-eval-after-load 'server (declare-function server-edit "server") (b/keymap-global-set "C-c F D" #'server-edit) (declare-function server-running-p "server") (or (server-running-p) (server-mode))) ;;; Defaults ;;;; C source code (setq-default ;; Case-sensitive search (and `dabbrev-expand'). ;; case-fold-search nil indent-tabs-mode nil ; always use space for indentation ;; tab-width 4 indicate-buffer-boundaries 'left) (setq ;; line-spacing 3 completion-ignore-case t read-buffer-completion-ignore-case t enable-recursive-minibuffers t resize-mini-windows t message-log-max 20000 mode-line-compact t ;; mouse-autoselect-window t scroll-conservatively 15 scroll-preserve-screen-position 1 ;; I don't feel like randomly jumping out of my chair. ring-bell-function 'ignore) ;;;; elisp source code (with-eval-after-load 'minibuffer (setopt read-file-name-completion-ignore-case t)) (with-eval-after-load 'files (setopt make-backup-files nil ;; Insert newline at the end of files. ;; require-final-newline t ;; Open read-only file buffers in view-mode, to get `q' for quit. view-read-only t) (add-to-list 'auto-mode-alist '("\\README.*" . text-mode)) (add-to-list 'auto-mode-alist '("\\.*rc$" . conf-mode)) (add-to-list 'auto-mode-alist '("\\.bashrc$" . sh-mode)) (add-to-list 'auto-mode-alist '("\\COMMIT_EDITMSG$" . text-mode))) (setq disabled-command-function nil) (run-with-idle-timer 0.1 nil #'require 'autorevert) (with-eval-after-load 'autorevert (setopt ;; auto-revert-verbose nil global-auto-revert-non-file-buffers nil) (global-auto-revert-mode 1)) (run-with-idle-timer 0.1 nil #'require 'time) (with-eval-after-load 'time (setopt display-time-default-load-average nil display-time-format " %a %Y-%m-%d %-l:%M%P" display-time-mail-icon '(image :type xpm :file "gnus/gnus-pointer.xpm" :ascent center) display-time-use-mail-icon t zoneinfo-style-world-list `(,@zoneinfo-style-world-list ("Etc/UTC" "UTC") ("Asia/Tehran" "Tehran") ("Australia/Melbourne" "Melbourne"))) (unless (display-graphic-p) (display-time-mode))) (defvar b/battery-format "%p%b %t") (run-with-idle-timer 0.1 nil #'require 'battery) (with-eval-after-load 'battery (setopt battery-mode-line-format (format " [%s]" b/battery-format)) ;; (display-battery-mode -1) ) (run-with-idle-timer 0.5 nil #'require 'winner) (with-eval-after-load 'winner (winner-mode 1) (when (featurep 'exwm) ;; prevent a bad interaction between EXWM and winner-mode, where ;; sometimes closing a window (like closing a terminal after ;; entering a GPG password via pinentry-gnome3's floating window) ;; results in a dead frame somewhere and effectively freezes EXWM. (advice-add 'winner-insert-if-new :around (lambda (orig-fun &rest args) ;; only add the frame if it's live (when (frame-live-p (car args)) (apply orig-fun args)))))) (run-with-idle-timer 0.5 nil #'require 'windmove) (with-eval-after-load 'windmove (setopt windmove-wrap-around t) (b/keymap-global-set "M-H" #'windmove-left) (b/keymap-global-set "M-L" #'windmove-right) (b/keymap-global-set "M-K" #'windmove-up) (b/keymap-global-set "M-J" #'windmove-down)) (with-eval-after-load 'isearch (setopt isearch-allow-scroll t isearch-lazy-count t ;; Match non-ASCII variants during search search-default-mode #'char-fold-to-regexp)) (b/keymap-global-set "C-x v C-=" #'vc-ediff) (with-eval-after-load 'vc-git (setopt ;; vc-git-show-stash 0 vc-git-print-log-follow t)) (with-eval-after-load 'ediff (setopt ediff-window-setup-function #'ediff-setup-windows-plain ediff-split-window-function #'split-window-horizontally)) ;; (with-eval-after-load 'face-remap ;; (setopt ;; ;; Gentler font resizing. ;; text-scale-mode-step 1.05)) (run-with-idle-timer 0.4 nil #'require 'mwheel) (with-eval-after-load 'mwheel (setopt mouse-wheel-scroll-amount '(1 ((shift) . 1)) ; one line at a time mouse-wheel-progressive-speed nil ; don't accelerate scrolling mouse-wheel-follow-mouse t)) ; scroll window under mouse (run-with-idle-timer 0.4 nil #'require 'pixel-scroll) (with-eval-after-load 'pixel-scroll (pixel-scroll-mode 1)) (with-eval-after-load 'epg-config (setopt epg-gpg-program (executable-find "gpg") ;; Ask for GPG passphrase in minibuffer. ;; Will fail if gpg >= 2.1 is not available. epg-pinentry-mode 'loopback)) ;; (with-eval-after-load 'auth-source ;; (setopt ;; auth-sources '("~/.authinfo.gpg") ;; authinfo-hidden ;; (regexp-opt '("password" "client-secret" "token")))) (with-eval-after-load 'info (setq Info-directory-list `(,@Info-directory-list ,(expand-file-name (convert-standard-filename "info/") source-directory) "/usr/share/info/"))) (when (display-graphic-p) (set-fontset-font t 'arabic "Sahel WOL") (let ((emoji-font "Apple Color Emoji")) (when (member emoji-font (font-family-list)) (set-fontset-font t 'emoji `(,emoji-font . "iso10646-1") nil 'prepend))) (with-eval-after-load 'faces (let ((grey "#e7e7e7")) ;; (set-face-attribute 'default nil ;; :font "Source Code Pro" ;; :height 113 ; 130 ; 105 ;; :weight 'medium) ;; (set-face-attribute 'fixed-pitch nil ;; :font "Source Code Pro" ;; :weight 'medium) ;; (set-face-attribute 'default nil ;; :font "Inconsolata Medium-12:hinting=true:autohint=true") ;; (set-face-attribute 'fixed-pitch nil ;; :font "Inconsolata Medium-12:hinting=true:autohint=true") (set-face-attribute 'default nil :font "Source Code Pro Medium-10.5") (set-face-attribute 'fixed-pitch nil :font "Source Code Pro Medium-10.5") (set-face-attribute 'mode-line nil :box '(:line-width 2 :style released-button) :background grey :inherit 'fixed-pitch)))) (when (and (version< emacs-version "28") mode-line-compact) ;; Manually make some `mode-line' spaces smaller. ;; Emacs 28 and above do a terrific job at this out of the box ;; when `mode-line-compact' is set to t (see above)." (setq-default mode-line-format (mapcar (lambda (x) (if (and (stringp x) (or (string= x " ") (string= x " "))) " " x)) mode-line-format) mode-line-buffer-identification (propertized-buffer-identification "%10b"))) ;;; Useful utilities (defun b/insert-asterism () "Insert a centred asterism." (interactive) (let ((asterism "* * *")) (insert (concat "\n" (make-string (floor (/ (- fill-column (length asterism)) 2)) ?\s) asterism "\n")))) (defun b/join-line-top () "Like `join-line', but join next line to the current line." (interactive) (join-line 1)) (defun b/*scratch* () "Switch to `*scratch*' buffer, creating it if it does not exist." (interactive) (let ((fun (if (functionp #'get-scratch-buffer-create) #'get-scratch-buffer-create ; (version<= "29" emacs-version) #'startup--get-buffer-create-scratch))) ; (version< emacs-version "29") (switch-to-buffer (funcall fun)))) (defun b/duplicate-line-or-region (&optional n) "Duplicate the current line, or region (if active). Make N (default: 1) copies of the current line or region." (interactive "*p") (let ((u-r-p (use-region-p)) ; if region is active (n1 (or n 1))) (save-excursion (let ((text (if u-r-p (buffer-substring (region-beginning) (region-end)) (prog1 (thing-at-point 'line) (end-of-line) (if (eobp) (newline) (forward-line 1)))))) (dotimes (_ (abs n1)) (insert text)))))) (defun b/invert-default-face (arg) "Invert the `default' and `mode-line' faces for the current frame. Swap the background and foreground for the two `default' and `mode-line' faces, effectively acting like a simple light/dark theme toggle. If prefix argument ARG is given, invert the faces for all frames." (interactive "P") (let ((frame (unless arg (selected-frame)))) (invert-face 'default frame) (invert-face 'mode-line frame) (when (fboundp #'exwm-systemtray--refresh-background-color) (exwm-systemtray--refresh-background-color 'remap)))) (defun b/unfill-paragraph-or-region (&optional beg end) "Unfill paragraph, or region (if active)." (interactive "r") (let ((fill-column most-positive-fixnum)) (if (use-region-p) (fill-region beg end) (fill-paragraph)))) ;;; General key bindings (let ((kfs '(("C-c i" . ielm) ("C-c d" . b/duplicate-line-or-region) ("C-c j" . b/join-line-top) ("C-S-j" . b/join-line-top) ("C-c s c" . b/*scratch*) ("C-c v" . b/invert-default-face) ("C-c q" . b/unfill-paragraph-or-region) ;; evaling and macro-expanding ("C-c e b" . eval-buffer) ("C-c e e" . eval-last-sexp) ("C-c e m" . pp-macroexpand-last-sexp) ("C-c e r" . eval-region) ;; emacs things ("C-c e i" . emacs-init-time) ("C-c e u" . emacs-uptime) ("C-c e v" . emacs-version) ;; finding ("C-c f ." . find-file) ("C-c f l" . find-library) ("C-c f p" . find-file-at-point) ;; frames ("C-c F m" . make-frame-command) ("C-c F d" . delete-frame) ;; help/describe ("C-c h F" . describe-face)))) (dolist (kf kfs) (let ((key (car kf)) (fun (cdr kf))) (b/keymap-global-set key fun)))) (when (display-graphic-p) ;; Too easy to accidentally suspend (freeze) Emacs GUI. (b/keymap-global-unset "C-z")) ;;; Essential packages (add-to-list 'load-path (b/emacs.d "lisp")) ;; (require 'bandali-exwm) ;; recently opened files (run-with-idle-timer 0.2 nil #'require 'recentf) (with-eval-after-load 'recentf (setopt recentf-max-saved-items 2000) (recentf-mode) (defun b/recentf-open () "Use `completing-read' to \\[find-file] a recent file." (interactive) (find-file (completing-read "Find recent file: " recentf-list))) (b/keymap-global-set "C-c f r" #'b/recentf-open)) (with-eval-after-load 'help (temp-buffer-resize-mode) (setopt help-window-select t)) (with-eval-after-load 'help-mode (let ((m help-mode-map)) (b/keymap-set m "p" #'backward-button) (b/keymap-set m "n" #'forward-button) (b/keymap-set m "b" #'help-go-back) (b/keymap-set m "f" #'help-go-forward))) (with-eval-after-load 'doc-view (b/keymap-set doc-view-mode-map "M-RET" #'image-previous-line)) (with-eval-after-load 'shr (setopt shr-max-width 80)) (with-eval-after-load 'mule-cmds (setopt default-input-method "farsi-isiri-9147")) (with-eval-after-load 'tramp (tramp-set-completion-function "ssh" (append (tramp-get-completion-function "ssh") (mapcar (lambda (file) `(tramp-parse-sconfig ,file)) (directory-files "~/.ssh/config.d/" 'full directory-files-no-dot-files-regexp))))) (require 'bandali-eshell) (require 'bandali-ibuffer) (require 'bandali-dired) ;;; Email with Gnus and message (require 'bandali-gnus) (require 'bandali-message) ;; (with-eval-after-load 'sendmail ;; (setopt mail-header-separator "")) ;; (with-eval-after-load 'smtpmail ;; (setopt smtpmail-queue-mail t ;; smtpmail-queue-dir (concat b/maildir "queue/"))) ;;; IRC with ERC (require 'bandali-erc) ;;; Editing ;; Display Lisp objects at point in the echo area. (with-eval-after-load 'eldoc (setopt eldoc-minor-mode-string " eldoc") (global-eldoc-mode 1)) ;; highlight matching parens (run-with-idle-timer 0.2 nil #'require 'paren) (with-eval-after-load 'paren (show-paren-mode 1)) (with-eval-after-load 'simple (setopt ;; Save what I copy into clipboard from other applications into ;; Emacs' kill-ring, which would allow me to still be able to ;; easily access it in case I kill (cut or copy) something else ;; inside Emacs before yanking (pasting) what I'd originally ;; intended to. save-interprogram-paste-before-kill t) (column-number-mode 1) (line-number-mode 1)) (run-with-idle-timer 0.2 nil #'require 'savehist) (with-eval-after-load 'savehist ;; Save minibuffer history. (savehist-mode 1) (add-to-list 'savehist-additional-variables 'kill-ring)) ;; Automatically save place in files. (run-with-idle-timer 0.2 nil #'require 'saveplace nil 'noerror) (with-eval-after-load 'saveplace (save-place-mode 1)) (with-eval-after-load 'flyspell (setopt flyspell-mode-line-string " fly")) (with-eval-after-load 'text-mode (add-hook 'text-mode-hook #'flyspell-mode) (b/keymap-set text-mode-map "M-RET" #'b/insert-asterism)) (with-eval-after-load 'abbrev (add-hook 'text-mode-hook #'abbrev-mode)) ;;; Programming modes (with-eval-after-load 'lisp-mode (add-hook 'lisp-interaction-mode-hook (lambda () (setq indent-tabs-mode nil)))) ;; (add-to-list 'load-path (b/lisp "alloy-mode")) ;; (autoload 'alloy-mode "alloy-mode" nil t) ;; (with-eval-after-load 'alloy-mode ;; (setq alloy-basic-offset 2) ;; ;; (defun b/alloy-simple-indent (start end) ;; ;; (interactive "r") ;; ;; ;; (if (region-active-p) ;; ;; ;; (indent-rigidly start end alloy-basic-offset) ;; ;; ;; (if (bolp) ;; ;; ;; (indent-rigidly (line-beginning-position) ;; ;; ;; (line-end-position) ;; ;; ;; alloy-basic-offset))) ;; ;; (indent-to (+ (current-column) alloy-basic-offset))) ;; (define-key alloy-mode-map (kbd "RET") #'electric-newline-and-maybe-indent) ;; ;; (define-key alloy-mode-map (kbd "TAB") #'b/alloy-simple-indent) ;; (define-key alloy-mode-map (kbd "TAB") #'indent-for-tab-command)) ;; (add-to-list 'auto-mode-alist '("\\.\\(als\\|dsh\\)\\'" . alloy-mode)) ;; (add-hook 'alloy-mode-hook (lambda nil (setq-local indent-tabs-mode nil))) ;; (eval-when-compile (defvar lean-mode-map)) ;; (run-with-idle-timer 0.4 nil #'require 'lean-mode) ;; (with-eval-after-load 'lean-mode ;; (require 'lean-input) ;; (setq default-input-method "Lean" ;; lean-input-tweak-all '(lean-input-compose ;; (lean-input-prepend "/") ;; (lean-input-nonempty)) ;; lean-input-user-translations '(("/" "/"))) ;; (lean-input-setup) ;; ;; local key bindings ;; (define-key lean-mode-map (kbd "S-SPC") #'company-complete)) (with-eval-after-load 'sgml-mode (setopt sgml-basic-offset 0)) (with-eval-after-load 'css-mode (setopt css-indent-offset 2)) (add-hook 'tex-mode-hook #'auto-fill-mode) (add-hook 'tex-mode-hook #'flyspell-mode) (autoload 'cmake-mode "cmake-mode" nil t) (add-to-list 'auto-mode-alist '("CMakeLists\\.txt\\'" . cmake-mode)) (add-to-list 'auto-mode-alist '("\\.cmake\\'" . cmake-mode)) (with-eval-after-load 'cmake-mode (add-to-list 'load-path (b/emacs.d "lisp/cmake-font-lock")) (require 'cmake-font-lock)) ;;; Emacs enhancements & auxiliary packages (with-eval-after-load 'man (setopt Man-width 80)) ;; `debbugs' (b/keymap-global-set "C-c D d" #'debbugs-gnu) (b/keymap-global-set "C-c D b" #'debbugs-gnu-bugs) (b/keymap-global-set "C-c D e" ; bug-gnu-emacs (lambda () (interactive) (setq debbugs-gnu-current-suppress t) (debbugs-gnu debbugs-gnu-default-severities '("emacs")))) (b/keymap-global-set "C-c D g" ; bug-gnuzilla (lambda () (interactive) (setq debbugs-gnu-current-suppress t) (debbugs-gnu debbugs-gnu-default-severities '("gnuzilla")))) (with-eval-after-load 'eww (setopt eww-download-directory (file-name-as-directory (getenv "XDG_DOWNLOAD_DIR")))) (b/keymap-global-set "C-c e w" #'eww) (run-with-idle-timer 0.2 nil #'require 'display-fill-column-indicator nil 'noerror) (with-eval-after-load 'display-fill-column-indicator (global-display-fill-column-indicator-mode 1)) (with-eval-after-load 'window (setopt split-width-threshold 140)) (add-hook 'latex-mode-hook #'reftex-mode) (when (and (featurep 'completion-preview) (functionp #'completion-preview-mode)) (b/keymap-set completion-preview-active-mode-map "M-n" #'completion-preview-next-candidate) (b/keymap-set completion-preview-active-mode-map "M-p" #'completion-preview-prev-candidate) (b/keymap-set completion-preview-active-mode-map "M-i" #'completion-preview-insert) (add-hook 'prog-mode-hook #'completion-preview-mode) (add-hook 'text-mode-hook #'completion-preview-mode) (with-eval-after-load 'comint (add-hook 'comint-mode-hook #'completion-preview-mode))) (run-with-idle-timer 0.5 nil #'require 'delight) (with-eval-after-load 'delight (delight 'auto-fill-function " f" "simple") (delight 'abbrev-mode "" "abbrev") (delight 'mml-mode " mml" "mml")) (require 'bandali-po) (add-to-list 'load-path (b/emacs.d "lisp/ffs")) (run-with-idle-timer 0.5 nil #'require 'ffs) (with-eval-after-load 'ffs (setopt ffs-default-face-height 250) (global-set-key (kbd "C-c f s") #'ffs)) (add-hook 'ffs-start-hook (lambda () (mapc (lambda (mode) (funcall mode 1)) ; enable '(ffs--no-mode-line-minor-mode ffs--no-cursor-minor-mode)) (mapc (lambda (mode) (funcall mode -1)) ; disable '(show-paren-local-mode display-battery-mode display-fill-column-indicator-mode flyspell-mode tool-bar-mode menu-bar-mode scroll-bar-mode)) (fringe-mode 0))) (add-hook 'ffs-quit-hook (lambda () (mapc (lambda (mode) (funcall mode -1)) ; disable '(ffs--no-mode-line-minor-mode ffs--no-cursor-minor-mode)) (mapc (lambda (mode) (funcall mode 1)) ; enable '(show-paren-local-mode display-battery-mode display-fill-column-indicator-mode flyspell-mode tool-bar-mode menu-bar-mode scroll-bar-mode)) (fringe-mode nil))) (add-to-list 'load-path (b/emacs.d "lisp/debian-el")) (run-with-idle-timer 0.5 nil #'require 'debian-el) (with-eval-after-load 'debian-el (require 'apt-sources) (require 'apt-utils) (require 'debian-bug) (require 'deb-view) (require 'gnus-BTS) (require 'preseed)) (add-to-list 'load-path (b/emacs.d "lisp/dpkg-dev-el")) (run-with-idle-timer 0.5 nil #'require 'dpkg-dev-el) (with-eval-after-load 'dpkg-dev-el (require 'debian-changelog-mode) (require 'debian-bts-control) (require 'debian-changelog-mode) (require 'debian-control-mode) (require 'debian-copyright) (require 'readme-debian)) ;;; init.el ends here