summaryrefslogtreecommitdiff
path: root/modules/system-defaults.el
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2025-10-12 11:47:26 -0500
committerCraig Jennings <c@cjennings.net>2025-10-12 11:47:26 -0500
commit092304d9e0ccc37cc0ddaa9b136457e56a1cac20 (patch)
treeea81999b8442246c978b364dd90e8c752af50db5 /modules/system-defaults.el
changing repositories
Diffstat (limited to 'modules/system-defaults.el')
-rw-r--r--modules/system-defaults.el243
1 files changed, 243 insertions, 0 deletions
diff --git a/modules/system-defaults.el b/modules/system-defaults.el
new file mode 100644
index 00000000..c0879d51
--- /dev/null
+++ b/modules/system-defaults.el
@@ -0,0 +1,243 @@
+;;; system-defaults --- Non-UI Preferences -*- lexical-binding: t; coding: utf-8-unix; -*-
+;; author: Craig Jennings <c@cjennings.net>
+;;
+;;; Commentary:
+;; Loads during init to set sane defaults: UTF-8 everywhere, quiet prompts, synced clipboards,
+;; and hands-off async shell buffers. Nothing to call—just launch Emacs and the environment is ready.
+;; Native compilation is tuned for performance and its warnings get logged to comp-warnings.log.
+
+;;; Implementation Notes:
+;; `cj/log-comp-warning` advices `display-warning` so native-comp notices land in the log instead of popping Messages.
+;; Remove the advice if you need stock warning buffers for debugging.
+
+;;; Code:
+
+(require 'host-environment)
+
+;; -------------------------- Native Comp Preferences --------------------------
+;; after async compiler starts, set preferences and warning level
+
+(with-eval-after-load 'comp-run
+ (setopt native-comp-async-jobs-number 8) ; parallel compile workers
+ (setopt native-comp-speed 3) ; highest optimization level
+ (setopt native-comp-always-compile t)) ; always native-compile
+
+;; -------------------------- Log Native Comp Warnings -------------------------
+;; log native comp warnings rather than cluttering the buffer
+
+(defvar comp-warnings-log
+ (expand-file-name "comp-warnings.log" user-emacs-directory)
+ "File where native-comp warnings will be appended.")
+
+(defun cj/log-comp-warning (type message &rest args)
+ "Log native-comp warnings of TYPE with MESSAGE & ARGS to 'comp-warnings-log'.
+Suppress them from appearing in the *Warnings* buffer. If TYPE contains 'comp',
+log the warning with a timestamp to the file specified by 'comp-warnings-log'.
+Return non-nil to indicate the warning was handled."
+ (when (memq 'comp (if (listp type) type (list type)))
+ (with-temp-buffer
+ (insert (format-time-string "[%Y-%m-%d %H:%M:%S] "))
+ (insert (if (stringp message)
+ (apply #'format message args)
+ (format "%S %S" message args)))
+ (insert "\n")
+ (append-to-file (point-min) (point-max) comp-warnings-log))
+ ;; Return non-nil to tell `display-warning' “we handled it.”
+ t))
+
+(advice-add 'display-warning :before-until #'cj/log-comp-warning)
+
+;; ---------------------------------- Unicode ----------------------------------
+;; unicode everywhere
+
+(set-locale-environment "en_US.UTF-8")
+(prefer-coding-system 'utf-8)
+(set-default-coding-systems 'utf-8)
+(set-terminal-coding-system 'utf-8)
+(set-keyboard-coding-system 'utf-8)
+(set-selection-coding-system 'utf-8)
+(setq locale-coding-system 'utf-8)
+(set-charset-priority 'unicode)
+(setq x-select-request-type
+ '(UTF8_STRING COMPOUND_TEXT TEXT STRING))
+
+;; -------------------------- Disabling Functionality --------------------------
+
+(defun cj/disabled ()
+ "Do absolutely nothing and do it quickly.
+Used to disable functionality with defalias 'somefunc 'cj/disabled)."
+ (interactive))
+
+;; VIEW EMACS NEWS
+;; no news is good news
+(defalias 'view-emacs-news 'cj/disabled)
+(global-unset-key (kbd "C-h n"))
+
+;; DESCRIBE GNU PROJECT
+;; no gnus is good gnus
+(defalias 'describe-gnu-project 'cj/disabled)
+(global-unset-key (kbd "C-h g"))
+
+;; CUSTOMIZATIONS
+;; All customizations should be declared in Emacs init files.
+;; Add accidental customizations via the customization interface to a temp file that's never read.
+(setq custom-file (make-temp-file
+ "emacs-customizations-trashbin-"))
+
+;; ------------------------- Re-Enabling Functionality -------------------------
+
+(put 'narrow-to-region 'disabled nil) ;; narrow-to-region is extremely useful!
+(put 'upcase-region 'disabled nil) ;; upcase region is useful
+(put 'erase-buffer 'disabled nil) ;; and so is erase-buffer
+
+;; ------------------------------ Non UI Settings ------------------------------
+
+(setq ring-bell-function 'ignore) ;; disable the bell ring.
+(setq default-directory user-home-dir) ;; consider user home the default directory
+
+(global-auto-revert-mode) ;; update the buffer when the associated file has changed
+(setq global-auto-revert-non-file-buffers t) ;; do so for all buffer types (e.g., ibuffer)
+(setq bidi-display-reordering nil) ;; don't reorder bidirectional text for display
+(setq bidi-paragraph-direction t) ;; forces directionality of text for performance.
+
+(setq system-time-locale "C") ;; use en_US locale to format time.
+
+;; --------------------------------- Clipboard ---------------------------------
+;; keep the clipboard and kill ring in sync
+
+(setq select-enable-clipboard t) ;; cut and paste using clipboard
+(setq yank-pop-change-selection t) ;; update system clipboard when yanking in emacs
+(setq save-interprogram-paste-before-kill t) ;; save existing clipboard to kill ring before replacing
+
+;; Additional settings for better clipboard integration
+(setq select-enable-primary nil) ;; don't use X11 primary selection (no middle-click paste)
+(setq mouse-drag-copy-region nil) ;; don't copy region to clipboard by selecting with mouse
+
+;; -------------------------------- Tab Settings -------------------------------
+;; spaces, not tabs
+
+(setq-default tab-width 4) ;; if tab, make them 4 spaces default
+(setq-default indent-tabs-mode nil) ;; but turn off tabs by default
+
+;; ------------------------------ Scroll Settings ------------------------------
+
+(setq mouse-wheel-scroll-amount '(1 ((shift) . 1))) ;; one line at a time
+(setq mouse-wheel-progressive-speed nil) ;; don't accelerate scrolling
+
+;; ----------------------------- Case Insensitivity ----------------------------
+;; make user interfaces case insensitive
+
+(setq case-fold-search t) ;; case-insensitive searches
+(setq completion-ignore-case t) ;; case-insensitive completion
+(setq read-file-name-completion-ignore-case t) ;; case-insensitive file completion
+
+;; ------------------------------- Async Commands ------------------------------
+;; always create new async command buffers silently
+
+(setq async-shell-command-buffer 'new-buffer)
+
+;; never automatically display async command output buffers
+;; but keep them in the buffer list for later inspection
+(add-to-list 'display-buffer-alist
+ '("*Async Shell Command*" display-buffer-no-window (nil)))
+
+;; ------------------------ Mouse And Trackpad Settings ------------------------
+;; provide smoothest scrolling and avoid accidental gestures
+
+(setq mouse-wheel-follow-mouse 't) ;; scroll window under mouse
+(setq scroll-margin 3) ;; start scrolling at 3 lines from top/bottom
+(setq scroll-step 1) ;; keyboard scroll one line at a time
+
+;; disable pasting with mouse-wheel click
+(global-unset-key (kbd "<mouse-2>"))
+
+;; disable pinching gesture or mouse-wheel changing font size
+(global-unset-key (kbd "<pinch>"))
+(global-set-key [remap mouse-wheel-text-scale] 'cj/disabled)
+
+;; ------------------------------- Be Quiet(er)! -------------------------------
+;; reduces "helpful" instructions that distract Emacs power users.
+
+(setq-default vc-follow-symlinks) ;; don't ask to follow symlinks if target is version controlled
+(setq kill-buffer-query-functions ;; don't ask about killing buffers with processes, just kill them
+ (remq 'process-kill-buffer-query-function
+ kill-buffer-query-functions))
+(setq confirm-kill-processes nil) ;; automatically kill running processes on exit
+(setq confirm-nonexistent-file-or-buffer nil) ;; don't ask if a file I visit with C-x C-f or C-x b doesn't exist
+(setq ad-redefinition-action 'accept) ;; silence warnings about advised functions getting redefined.
+(setq large-file-warning-threshold nil) ;; open files regardless of size
+(fset 'yes-or-no-p 'y-or-n-p) ;; require a single letter for binary answers
+(setq use-short-answers t) ;; same as above with Emacs 28+
+(setq auto-revert-verbose nil) ;; turn off auto revert messages
+(setq custom-safe-themes t) ;; treat all themes as safe (stop asking)
+(setq server-client-instructions nil) ;; I already know what to do when done with the frame
+
+;; ------------------ Reduce Garbage Collections In Minibuffer -----------------
+;; triggers garbage collection when it won't impact user minibuffer entries
+
+(defun cj/minibuffer-setup-hook ()
+ "Hook to prevent garbage collection while user's in minibuffer."
+ (setq gc-cons-threshold most-positive-fixnum))
+
+(defun cj/minibuffer-exit-hook ()
+ "Hook to trigger garbage collection when exiting minibuffer."
+ (setq gc-cons-threshold 800000))
+
+(add-hook 'minibuffer-setup-hook #'cj/minibuffer-setup-hook)
+(add-hook 'minibuffer-exit-hook #'cj/minibuffer-exit-hook)
+
+;; ----------------------------- Bookmark Settings -----------------------------
+;; keep bookmarks in sync location, and save the file whenever a mark is added
+
+;; place bookmark file sync'd org files
+(setq bookmark-default-file (concat sync-dir "emacs_bookmarks"))
+
+;; save bookmarks each (1) time it's modified.
+(setq bookmark-save-flag 1)
+
+;; -------------------------------- Recent Files -------------------------------
+;; don't suggest bookmarks, packages, indexes, or recentf in recent files.
+
+(use-package recentf
+ :init
+ (recentf-mode 1)
+ :ensure nil ;;built-in
+ :config
+ (setq recentf-max-saved-items 1000)
+ (setq recentf-max-menu-items 50)
+ (add-to-list 'recentf-exclude "emacs_bookmarks")
+ (add-to-list 'recentf-exclude "\\.emacs\\.d/elpa")
+ (add-to-list 'recentf-exclude "\\.emacs\\.d/recentf")
+ (add-to-list 'recentf-exclude "\\ElfeedDB/index"))
+
+;; -------------------------- Autosave And Lock Files --------------------------
+;; don't create lockfiles or autosave (i.e., filename~) files.
+
+(setq auto-save-default nil)
+(setq create-lockfiles nil)
+
+;; ------------------------------ Backup Settings ------------------------------
+;; per-save backups can be invaluable, so create them in ~/.emacs.d/backups
+
+;; BACKUP DIRECTORY CREATION
+(defvar cj/backup-directory (concat user-emacs-directory "backups"))
+(if (not (file-exists-p cj/backup-directory))
+ (make-directory cj/backup-directory t))
+
+;; BACKUP SETTINGS
+(setq make-backup-files t) ;; do make backup files
+(setq backup-directory-alist `(("." . ,cj/backup-directory))) ;; put all originals in backup directory
+(setq backup-by-copying t) ;; don't clobber symlinks
+(setq version-control t) ;; make numeric backup versions
+(setq delete-old-versions t) ;; delete excess backup files w/o asking
+(setq kept-new-versions 25) ;; keep 25 of the newest backups made (default: 2)
+(setq vc-make-backup-files t) ;; also backup any files in version control
+
+;; ------------------------------- GNU 'ls' On BSD -------------------------------
+;; when on BSD use the ls from FSF sysutils/coreutils: pkg install coreutils
+
+(when (env-bsd-p)
+ (setq insert-directory-program "/usr/local/bin/gls"))
+
+(provide 'system-defaults)
+;;; system-defaults.el ends here