summaryrefslogtreecommitdiff
path: root/modules/system-defaults.el
blob: 526071216f1ceea658d0088c80956e7356ceb5ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
;;; 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 'autorevert)
(require 'server)
(require 'bookmark)

;; Loaded earlier in init.el
(eval-when-compile (require 'host-environment))
(eval-when-compile (require 'user-constants))

;; Function in system-utils.el; autoload to avoid requiring it here.
(autoload 'env-bsd-p "host-environment" nil t)

;; -------------------------- Native Comp Preferences --------------------------

(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 -------------------------

(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.
Log to buffer `comp-warnings-log'. Suppress warnings 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 ----------------------------------

(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 ---------------------------------

(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 -------------------------------

(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 ----------------------------

(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 ------------------------------

(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 ------------------------

(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>"))
(keymap-global-set "<remap> <mouse-wheel-text-scale>" #'cj/disabled)

;; ------------------------------- Be Quiet(er)! -------------------------------

(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 -----------------

(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 -----------------------------

;; place bookmark file sync'd org files
(setq bookmark-default-file (concat org-dir "emacs_bookmarks"))

;; save bookmarks each (1) time it's modified.
(setq bookmark-save-flag 1)

;; -------------------------------- 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 --------------------------

(setq  auto-save-default nil)
(setq  create-lockfiles nil)

;; ------------------------------ Backup Settings ------------------------------

;; 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 (env-bsd-p)
  (setq insert-directory-program "/usr/local/bin/gls"))

(provide 'system-defaults)
;;; system-defaults.el ends here