Emacs: Complete restructure of the config

Changed
- Org-mode babel config to separate .el modules
- Built-in package manager package.el to Elpaca
- Configuration macro use-package to setup.el
- Completion framework selectrum to vertico
This commit is contained in:
Riyyi
2023-01-01 22:46:55 +01:00
parent 2b5aabebfb
commit 89607db905
29 changed files with 2880 additions and 2705 deletions
@@ -0,0 +1,25 @@
;;; dot-core-advice.el --- -*- lexical-binding: t; -*-
;;; Commentary:
;; Advice and Aliases.
;;; Code:
;; -----------------------------------------
;; Advice
;; Define default terminal option.
(defun dot/ansi-term (program &optional new-buffer-name)
(interactive (list dot/shell)))
(advice-add 'ansi-term :before #'dot/ansi-term)
;; Aliases
;; Make confirm easier, by just pressing y/n.
(defalias 'yes-or-no-p 'y-or-n-p)
(provide 'dot-core-advice)
;;; dot-core-advice.el ends here
@@ -0,0 +1,244 @@
;;; dot-core-config.el --- -*- lexical-binding: t; -*-
;;; Commentary:
;; ??
;;; Code:
;; -----------------------------------------
;; General
;; Columns start at 1
(setq column-number-indicator-zero-based nil)
;; TODO: Make variable below compatible with telephone-line
;; (setq mode-line-position-column-format " C%C")
;; Dont confirm on quitting Emacs
(setq confirm-kill-processes nil)
;; Custom thems, do not ask if safe
(setq custom-safe-themes t)
;; Dired move to trash
(setq delete-by-moving-to-trash t)
;; Column indicator character
(setq display-fill-column-indicator-character ?\N{U+2503})
;; Scrolling
(setq scroll-conservatively 1)
(setq mouse-wheel-scroll-amount '(5))
(setq mouse-wheel-progressive-speed nil)
;; Parenthesis, set behavior
(setq show-paren-delay 0)
(setq show-paren-style 'mixed)
;; Tramp default protocol
(setq tramp-default-method "ssh")
;; Set undo limit, measured in bytes
(setq-default undo-limit 400000)
(setq-default undo-strong-limit 3000000)
(setq-default undo-outer-limit 12000000)
;; Enable line numbers
(global-display-line-numbers-mode)
;; C++ syntax highlighting for .h files
(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))
;; Set the frame title
(setq frame-title-format
`("%b"
(:eval
(if (buffer-file-name)
(concat
(if (buffer-modified-p) "" nil)
" ("
(abbreviate-file-name
(directory-file-name
(file-name-directory (buffer-file-name))))
")")
nil))
,(format " - GNU Emacs %s" emacs-version)
))
(setq icon-title-format frame-title-format)
;; -----------------------------------------
;; Buffers
(setq confirm-nonexistent-file-or-buffer nil)
(setq ibuffer-expert t)
;; -----------------------------------------
;; Dired
(setq wdired-allow-to-change-permissions t)
;; -----------------------------------------
;; Electric
;; Make return key also do indent of previous line
(electric-indent-mode 1)
(setq electric-pair-pairs '(
(?\( . ?\))
(?\[ . ?\])
))
(electric-pair-mode 1)
;; -----------------------------------------
;; File Paths
;; Set file paths for built-in features like: auto-saves, backups, etc.
;; Set Directory locations
(setq auto-save-list-file-prefix (expand-file-name "auto-save/" dot-cache-dir))
(setq auto-save-file-name-transforms `((".*" ,auto-save-list-file-prefix t)))
(setq backup-directory-alist `((".*" . ,(expand-file-name "backup/" dot-cache-dir))))
(setq custom-theme-directory (expand-file-name "themes/" dot-emacs-dir))
(setq eshell-directory-name (expand-file-name "eshell/" dot-cache-dir))
(setq tramp-auto-save-directory (expand-file-name "tramp-auto-save/" dot-cache-dir))
(setq tramp-backup-directory-alist backup-directory-alist)
(setq url-configuration-directory (expand-file-name "url/" dot-cache-dir))
;; Set file locations
(setq bookmark-default-file (expand-file-name "bookmarks" dot-etc-dir))
(setq nsm-settings-file (expand-file-name "network-security.data" dot-cache-dir))
(setq org-id-locations-file (expand-file-name "org-id-locations" dot-cache-dir))
(setq tramp-persistency-file-name (expand-file-name "tramp" dot-cache-dir ))
;; -----------------------------------------
;; File Backups Versioning
;; Setup file backups versioning.
(setq backup-by-copying t) ; Don't cobbler symlinks
(setq create-lockfiles nil) ; Disable lockfiles (.#)
(setq delete-old-versions t) ; Cleanup backups
(setq kept-new-versions 5) ; Newest backups to keep
(setq kept-old-versions 2) ; Oldest backups to keep
(setq version-control t) ; Use version numbers on backups
;; -----------------------------------------
;; Formatting
;; Columnn after line-wrapping happens
(setq-default fill-column 80)
;; Automatically add newline on save at the end of the file
(setq require-final-newline t)
;; End sentences with a single space
(setq sentence-end-double-space nil)
;; `tabify' and `untabify' should only affect indentation
(setq tabify-regexp "^\t* [ \t]+")
;; Do not wrap lines
(setq-default truncate-lines t)
;; Wrap lines in the middle of words, gives a \ indicator
(setq-default word-wrap nil)
;; -----------------------------------------
;; Hide Elements
(menu-bar-mode 0)
(scroll-bar-mode 0)
(tool-bar-mode 0)
(tooltip-mode 0)
(fringe-mode 0)
(blink-cursor-mode 0)
(setq inhibit-startup-message t)
(setq initial-scratch-message nil)
(setq ring-bell-function 'ignore)
;; -----------------------------------------
;; Native Compilation
(setq native-comp-async-report-warnings-errors nil)
;; -----------------------------------------
;; Recentf
(elpaca nil (setup recentf ; built-in
(:require recentf)
(:when-loaded
(setq recentf-auto-cleanup 'never)
(setq recentf-exclude '("~$" "/ssh:" "/sudo:"))
(setq recentf-filename-handlers '(abbreviate-file-name))
(setq recentf-max-menu-items 0)
(setq recentf-max-saved-items 200)
(setq recentf-save-file (expand-file-name "recentf" dot-cache-dir))
(recentf-mode))))
;; -----------------------------------------
;; Tabs
;; Tabs
(setq-default tab-width 4
indent-tabs-mode t
c-basic-offset 4
sgml-basic-offset 4
sh-basic-offset 4)
;; C/C++-like languages formatting style
;; https://www.emacswiki.org/emacs/IndentingC
;; https://www.gnu.org/software/emacs/manual/html_mono/ccmode.html#Getting-Started
;; https://www.gnu.org/software/emacs/manual/html_mono/ccmode.html#Adding-Styles
;; https://www.gnu.org/software/emacs/manual/html_mono/ccmode.html#Sample-Init-File
(c-add-style "user" `("linux"
(c-basic-offset . ,(default-value 'tab-width))
(c-offsets-alist
(innamespace . -)
)))
(setq-default c-default-style "user")
;; -----------------------------------------
;; UTF-8
;; Set UTF-8 encoding as default.
(prefer-coding-system 'utf-8-unix)
(setq locale-coding-system 'utf-8-unix)
;; Default also sets file-name, keyboard and terminal coding system
(set-default-coding-systems 'utf-8-unix)
(set-buffer-file-coding-system 'utf-8-unix)
(set-selection-coding-system 'utf-8-unix)
;; -----------------------------------------
;; Window
;; Set `switch-to-buffer' to respect the window rules
(setq switch-to-buffer-obey-display-actions t)
;; Window rules
(setq display-buffer-alist
'(
;; ^\*(e?shell|(ansi-|v)?term).*
("^\\*\\(e?shell\\|\\(ansi-\\|v\\)?term\\).*"
(display-buffer-in-side-window)
(window-height . 0.25)
(side . bottom)
(slot . -1))
("\\*Faces\\*"
(display-buffer-in-side-window)
(window-height . 0.25)
(side . bottom)
(slot . 1))
("\\*Help.*"
(display-buffer-in-side-window)
(window-height . 0.25)
(side . bottom)
(slot . 0))
))
;; Allow 'undo' and 'redo' changes in Window Configuration.
(winner-mode)
(provide 'dot-core-config)
;;; dot-core-config.el ends here
@@ -0,0 +1,19 @@
;;; dot-core-customize.el --- -*- lexical-binding: t; -*-
;;; Commentary:
;; ??
;;; Code:
;; -----------------------------------------
;; Customizations
;; Store customize file separately, don't freak out when it's not found.
(setq custom-file (expand-file-name "custom.el" dot-etc-dir))
(load custom-file 'noerror)
(provide 'dot-core-customize)
;;; dot-core-customize.el ends here
@@ -0,0 +1,17 @@
;;; dot-core-fonts.el --- -*- lexical-binding: t; -*-
;;; Commentary:
;; ??
;;; Code:
;; -----------------------------------------
;; Set fonts
(set-face-attribute 'default nil :height 90 :family "DejaVu Sans Mono")
(set-face-attribute 'fixed-pitch-serif nil :height 100)
(provide 'dot-core-fonts)
;;; dot-core-fonts.el ends here
@@ -0,0 +1,195 @@
;;; dot-core-functions.el --- -*- lexical-binding: t; -*-
;;; Commentary:
;; Custom elisp functions and macros.
;;; Code:
;; -----------------------------------------
;; General Functions
;; Functions that only use built-in Emacs functionality.
(defun display-startup-echo-area-message ()
"Hide default startup message."
(message nil))
(defun dot/config-visit ()
"Edit config file."
(interactive)
(find-file (expand-file-name "init.el" dot-emacs-dir)))
(defun dot/config-reload ()
"Reload config file."
(interactive)
(load (expand-file-name "init.el" dot-emacs-dir)))
(defun dot/copy-cpp-function-implementation ()
"Copy C++ function implementation to clipboard."
(interactive)
(save-excursion
(let ((func (save-excursion
(re-search-backward "\\b")
(re-search-forward "\\([^;]+\\);")
(match-string 1)))
(type (progn
(re-search-backward "\\b")
(push-mark)
(back-to-indentation)
(buffer-substring (mark) (point))))
(class (progn
(backward-up-list)
(backward-sexp)
(back-to-indentation)
(forward-to-word 1)
(current-word))))
(kill-new (concat type class "::" func "\n{\n}"))))
(message "Copied function implementation"))
;; Reference: http://turingmachine.org/bl/2013-05-29-recursively-listing-directories-in-elisp.html
(defun dot/directory-files-recursively-depth (dir regexp include-directories maxdepth)
"Depth limited variant of the built-in `directory-files-recursively'."
(let ((result '())
(current-directory-list (directory-files dir t)))
(dolist (path current-directory-list)
(cond
((and (file-regular-p path)
(file-readable-p path)
(string-match regexp path))
(setq result (cons path result)))
((and (file-directory-p path)
(file-readable-p path)
(not (string-equal "/.." (substring path -3)))
(not (string-equal "/." (substring path -2))))
(when (and include-directories
(string-match regexp path))
(setq result (cons path result)))
(when (> maxdepth 1)
(setq result (append (nreverse (dot/directory-files-recursively-depth
path regexp include-directories (- maxdepth 1)))
result))))
(t)))
(reverse result)))
(defun dot/dired-find-file ()
"In Dired, visit the file or directory named on this line."
(interactive)
(if (file-directory-p (dired-file-name-at-point))
(dired-find-alternate-file)
(dired-find-file)))
(defun dot/dired-up-directory ()
"Run Dired on parent directory of current directory."
(interactive)
(find-alternate-file ".."))
(defun dot/find-file-emacsd ()
"Find file under `dot-emacs-dir', recursively."
(interactive)
(let ((files (mapcar 'abbreviate-file-name
(directory-files-recursively dot-emacs-dir ""))))
(find-file (completing-read "Find file (emacs): " files nil t))))
(defun dot/indent-buffer ()
"Indent each nonblank line in the buffer."
(interactive)
(save-excursion
(indent-region (point-min) (point-max) nil)))
(defun dot/insert-spaces-until-column (until-column)
"Insert spaces from point to UNTIL-COLUMN."
(interactive "nInsert spaces until column: ")
(let ((current-column (current-column)))
;; Increment column if the index is 1 based
(when (not column-number-indicator-zero-based)
(setq current-column (+ current-column 1)))
;; Insert spaces
(let ((diff (- until-column current-column)))
(if (> diff 0)
(save-excursion (insert (make-string diff ?\ )))
(user-error "Column should be higher than point")))))
(defun dot/reload-theme ()
"Reload custom theme."
(interactive)
(mapc 'load (file-expand-wildcards
(concat (car custom-theme-load-path) "*.el")))
(load-theme (car custom-enabled-themes) t))
(defun dot/sudo-find-file (filename)
"Edit file FILENAME as root."
(interactive "FOpen file (as root): ")
(find-file (concat "/sudo:root@localhost:" filename)))
(defun dot/sudo-this-file ()
"Edit the current file as root."
(interactive)
(if buffer-file-name
(find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))
(princ "Current buffer isn't a file")))
(defun dot/toggle-fringe (&optional arg)
"Toggle left-only fringe, or set state with ARG."
(interactive)
(if (or (and (eq fringe-mode 0) (eq arg nil))
(eq arg 1))
(set-fringe-mode '(nil . 0))
(set-fringe-mode 0)))
(defun dot/M-x (command)
"Prompt and execute COMMAND."
(interactive "CCommand: ")
(command-execute command))
(defun split-follow-horizontally ()
"Split and follow window."
(interactive)
(split-window-below)
(other-window 1))
(defun split-follow-vertically ()
"Split and follow window."
(interactive)
(split-window-right)
(other-window 1))
;; https://emacsredux.com/blog/2013/05/04/rename-file-and-buffer/
(defun rename-file-and-buffer ()
"Rename the current buffer and file it is visiting."
(interactive)
(let ((filename (buffer-file-name)))
(if (not (and filename (file-exists-p filename)))
(message "Buffer is not visiting a file!")
(let ((new-name (read-file-name "New name: " filename)))
(cond
((vc-backend filename) (vc-rename-file filename new-name))
(t
(rename-file filename new-name t)
(set-visited-file-name new-name t t)))))))
;; -----------------------------------------
;; Hook call functions
(defun dot/hook-disable-line-numbers ()
"Disable the line numbers."
(display-line-numbers-mode 0))
(defun dot/hook-disable-mode-line ()
"Disable the mode line."
(setq-local mode-line-format nil))
(provide 'dot-core-functions)
;; -----------------------------------------
;; Macros
;; Reference: https://github.com/arcticicestudio/nord-emacs/issues/59#issuecomment-414882071
(defmacro dot/run-after-new-frame (func)
"Run FUNC once or after every frame creation.
This is needed for UI initialization when running with the daemon."
`(if (daemonp)
(add-hook 'after-make-frame-functions
(lambda (frame) (with-selected-frame frame ,func)))
,func))
;;; dot-core-functions.el ends here
@@ -0,0 +1,32 @@
;;; dot-hooks.el --- -*- lexical-binding: t; -*-
;;; Commentary:
;; Add hooks.
;;; Code:
;; -----------------------------------------
;; Delete trailing whitespace
(add-hook 'before-save-hook #'delete-trailing-whitespace)
;; Display fill column indicator
(add-hook 'prog-mode-hook #'display-fill-column-indicator-mode)
(add-hook 'text-mode-hook #'display-fill-column-indicator-mode)
;; Highlight parenthesis
(add-hook 'prog-mode-hook #'show-paren-mode)
;; Disable line numbers
(add-hook 'Custom-mode-hook #'dot/hook-disable-line-numbers)
(add-hook 'dired-mode-hook #'dot/hook-disable-line-numbers)
(add-hook 'Info-mode-hook #'dot/hook-disable-line-numbers)
(add-hook 'term-mode-hook #'dot/hook-disable-line-numbers)
;; Wrap lines in the middle of words, gives a \ indicator
(add-hook 'visual-line-mode-hook (lambda () (setq word-wrap nil)))
(provide 'dot-core-hooks)
;;; dot-hooks.el ends here
@@ -0,0 +1,90 @@
;;; dot-core-packages.el --- -*- lexical-binding: t; -*-
;;; Commentary:
;; Install core packages.
;;; Code:
;;; Compile
;; Automatically compile all packages.
;; https://github.com/emacscollective/auto-compile
(elpaca-setup auto-compile
(:require auto-compile)
(:when-loaded
(auto-compile-on-load-mode)
(auto-compile-on-save-mode)))
;;; General packages
(elpaca-setup general
(:load-after evil)
(:when-loaded
;; Fix for issue: general #493 and evil #130, #301
;; https://github.com/noctuid/evil-guide#why-dont-keys-defined-with-evil-define-key-work-immediately
(defun dot/general-fix-leader-key ()
"Fix leader key in *Messages* buffer."
(when-let ((messages-buffer (get-buffer "*Messages*")))
(with-current-buffer messages-buffer
(evil-normalize-keymaps))))
(add-hook 'emacs-startup-hook #'dot/general-fix-leader-key)))
(elpaca-setup avy)
(elpaca-setup hungry-delete
(:require hungry-delete)
(:when-loaded (global-hungry-delete-mode)))
(elpaca-setup smart-tabs-mode
;; TODO: how does this get auto-loaded?
(:when-loaded
(smart-tabs-add-language-support latex latex-mode-hook
((latex-indent-line . 4)
(latex-indent-region . 4)))
;; FIXME: breaks for Python files
(smart-tabs-insinuate 'c 'c++ 'java 'python 'latex)))
(elpaca-setup super-save
(:require super-save)
(:when-loaded
(setq super-save-auto-save-when-idle t)
;; Fix for issues: super-save #38 and lsp-mode #1322
(defun dot/super-save-disable-advice (orig-fun &rest args)
"Dont auto-save under these conditions."
(unless (equal (car args) " *LV*")
(apply orig-fun args)))
(advice-add 'super-save-command-advice :around #'dot/super-save-disable-advice)
(super-save-mode)))
(elpaca nil (setup desktop ; built-in
(:require desktop)
(:when-loaded
(setq desktop-base-file-name "state")
(setq desktop-base-lock-name "state.lock")
(setq desktop-dirname (expand-file-name "desktop/" dot-cache-dir))
(setq desktop-path (list desktop-dirname))
(setq desktop-globals-to-save '()) ;; Only need frames and buffers
;; Create directory to store desktop file in
(unless (file-directory-p desktop-dirname)
(make-directory desktop-dirname t))
(defun dot/desktop-save ()
"Save frame state and buffers."
(interactive)
(dot/centaur-tabs-buffer-cleanup)
(desktop-save desktop-dirname t))
(defun dot/desktop-save-on-exit ()
"Save state of buffers before closing Emacs."
(dot/desktop-save)
(desktop-release-lock desktop-dirname))
(add-hook 'kill-emacs-hook #'dot/desktop-save-on-exit))))
(provide 'dot-core-packages)
;;; dot-core-packages.el ends here
@@ -0,0 +1,48 @@
;;; dot-core-variables.el --- -*- lexical-binding: t; -*-
;;; Commentary:
;; Global variables.
;;; Code:
;; -----------------------------------------
;; Global Variables
;; Variables for directories, leader keys, etc.
(defvar dot-emacs-dir (directory-file-name (file-truename user-emacs-directory))
"Directory base.") ; ~/.config/emacs
(defvar dot-etc-dir (expand-file-name "etc" dot-emacs-dir)
"Directory for non-volatile storage.") ; ~/.config/emacs/etc
;; TODO: remove "-modules" when switching over
(defvar dot-cache-dir (expand-file-name "emacs" (getenv "XDG_CACHE_HOME"))
"Directory for cache data.") ; ~/.cache/emacs
(defvar dot/leader-key "SPC"
"Leader prefix key.")
(defvar dot/leader-alt-key "M-SPC"
"Alternative leader prefix key, used for Insert and Emacs states.")
(defvar dot/localleader-key "SPC m"
"Local leader prefix key, for 'major-mode' specific commands.")
(defvar dot/localleader-alt-key "M-SPC m"
"Alternative local leader prefix key, used for Insert and Emacs states.")
(defvar dot/shell "/bin/zsh"
"Command interpreter binary path.")
(defvar dot/hidpi (getenv "HIDPI")
"Whether the primary screen is HiDPI.")
;; Create cache directory
(unless (file-directory-p dot-cache-dir)
(make-directory dot-cache-dir t))
(provide 'dot-core-variables)
;;; dot-core-variables.el ends here