aboutsummaryrefslogtreecommitdiff
path: root/nix/users/fcuny/configs/emacs/init.el
blob: d0a95091af5bc4c4a267355c30c013ebdc5ed4c5 (plain) (blame)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
;;; init.el --- This is where all emacs start. -*- lexical-binding: t -*-

;;; Commentary:

;;; Code:

;; use utf-8 everywhere
(set-default-coding-systems 'utf-8)

(require 'ibuffer)
(require 'imenu)
(require 'midnight)

(setq auto-save-default nil)                            ;; no auto save
(setq backup-inhibited t)                               ;; no backups
(setq confirm-kill-emacs #'yes-or-no-p)                 ;; ask before killing emacs
(setq create-lockfiles nil)                             ;; don't use a lock file
(setq cursor-in-non-selected-windows nil)               ;; keep cursors and highlights in current window only
(setq delete-by-moving-to-trash t)                      ;; delete files by moving them to the trash
(setq highlight-nonselected-windows nil)                ;; don't highlight inactive windows
(setq history-delete-duplicates t)                      ;; delete duplicate from history
(setq ibuffer-expert t)                                 ;; set expert mode
(setq ibuffer-jump-offer-only-visible-buffers t)        ;; only show visible buffers
(setq ibuffer-use-other-window t)                       ;; open ibuffer in other window
(setq imenu-auto-rescan t)                              ;; rescan automatically
(setq initial-major-mode 'fundamental-mode)             ;; default mode for the scratch buffer
(setq initial-scratch-message "")                       ;; makes the scratch buffer empty
(setq midnight-period (* 3600 6))                       ;; clear buffer every 6 hours
(setq mode-line-default-help-echo nil)                  ;; don't say anything on mode-line mouseover
(setq require-final-newline t)                          ;; ensure a new line is present at the bottom of files
(setq ring-bell-function 'ignore)                       ;; really no bell
(setq sentence-end-double-space nil)                    ;; it matters for filling
(setq use-short-answers t)                              ;; use y-or-n
(setq visible-bell nil)                                 ;; no bell
(setq bidi-display-reordering nil)                      ;; disable bidirectional text support for slight performance bonus
(setq column-number-mode t)                             ;; show column number in the mode line

;; global minor modes
(fringe-mode '(8 . 0))
(global-auto-revert-mode t)
(save-place-mode t)
(which-key-mode t)

(require 'savehist)
(savehist-mode t)
(setq history-length 100
      history-delete-duplicates t
      savehist-save-minibuffer-history t)

(require 'recentf)
(recentf-mode t)
(setq recentf-max-saved-items 2000
      recentf-max-menu-items  200)

;; some key bindings
(global-set-key (kbd "M-j") 'join-line)
(global-set-key (kbd "C-x C-b") 'ibuffer)
(global-set-key (kbd "C-c y") 'git-link)
(global-set-key (kbd "C-c C-r") 'eval-region)
(global-set-key (kbd "C-c C-d") 'eval-defun)
(global-set-key (kbd "C-c C-b") 'eval-buffer)
(global-set-key (kbd "C-c ! b") 'flymake-show-project-diagnostics)
(global-set-key (kbd "C-c ! n") 'flymake-goto-next-error)
(global-set-key (kbd "C-c ! p") 'flymake-goto-prev-error)

;; load the PATH properly on macOS
(require 'exec-path-from-shell)
(exec-path-from-shell-initialize)

;; ensure we highlights whitespaces
(require 'whitespace)
(dolist (mode-hook '(prog-mode-hook text-mode-hook conf-mode-hook))
  (add-hook mode-hook 'whitespace-mode))

(setq whitespace-style '(face trailing))
(setq-default show-trailing-whitespace t)

;;; dired
(require 'dired)
(setq dired-use-ls-dired t
      dired-clean-up-buffers-too nil
      dired-dwim-target t
      dired-hide-details-hide-information-lines nil
      dired-hide-details-hide-symlink-targets nil
      dired-recursive-copies 'always
      dired-recursive-deletes 'always
      dired-no-confirm '(byte-compile chgrp chmod chown copy hardlink symlink touch))

(defun my/rename-this-buffer-and-file ()
  "Renames current buffer and file it is visiting."
  (interactive)
  (let ((name (buffer-name))
        (filename (buffer-file-name))
        (read-file-name-function 'read-file-name-default))
    (if (not (and filename (file-exists-p filename)))
        (error "Buffer '%s' is not visiting a file!" name)
      (let ((new-name (read-file-name "New name: " filename)))
        (cond ((get-buffer new-name)
               (error "A buffer named '%s' already exists!" new-name))
              (t
               (rename-file filename new-name 1)
               (rename-buffer new-name)
               (set-visited-file-name new-name)
               (set-buffer-modified-p nil)
               (message "File '%s' successfully renamed to '%s'" name (file-name-nondirectory new-name))))))))

;;; time related configurations
(require 'time)
(setq display-time-24hr-format t
      display-time-interval 60
      display-time-mode t
      display-time-format "%H:%M %d.%m"
      display-time-day-and-date t
      display-time-default-load-average nil)

(setq world-clock-list t
      world-clock-timer-enable t
      world-clock-timer-second 60
      world-clock-time-format "%R %z  %A %d %B")

;; UTC      => 02:42 +0000  Wednesday 20 April
;; Berkeley => 19:42 -0700  Tuesday 19 April
(setq zoneinfo-style-world-list '(("UTC" "UTC")
				  ("America/Los_Angeles" "Berkeley")
				  ("America/Denver" "Mountain Time")
				  ("America/Chicago" "Central Time")
				  ("America/New_York" "New York")
				  ("Europe/London" "London")
				  ("Europe/Paris" "Paris")))

;;; magit
(require 'magit)
(setq magit-diff-refine-hunk t
      magit-repository-directories '(("~/workspace" . 1))
      magit-clone-default-directory "~/workspace/"
      magit-repolist-columns '(("Name" 25 magit-repolist-column-ident nil)
			       ("" 3 magit-repolist-column-flag)
			       ("Version" 25 magit-repolist-column-version
				((:sort magit-repolist-version<)))
			       ("B<U" 3 magit-repolist-column-unpulled-from-upstream
				((:right-align t)
				 (:sort <)))
			       ("B>U" 3 magit-repolist-column-unpushed-to-upstream
				((:right-align t)
				 (:sort <)))
			       ("Path" 99 magit-repolist-column-path nil))
      magit-repolist-column-flag-alist '((magit-untracked-files . "N")
					 (magit-unstaged-files . "U")
					 (magit-staged-files . "S")))

;; show ANSI colors in the process buffer, so it's easier to read what's going on
;; for some reasons if it's in the `:custom' section it does not get set
(setq magit-process-apply-ansi-colors t)

;;; git-link
(require 'git-link)
(setq git-link-open-in-browser t)

;; FIXME sets up roblox git enterprise as a git-link handler
(with-eval-after-load 'git-link
  (add-to-list 'git-link-remote-alist '("github\\.rblx\\.com" git-link-github))
  (add-to-list 'git-link-commit-remote-alist '("github\\.rblx\\.com" git-link-commit-github)))

;;; rg
(require 'rg)
(setq rg-group-result t
      rg-show-columns t
      rg-align-line-number-field-length 3
      rg-align-column-number-field-length 3
      rg-align-line-column-separator "#"
      rg-align-position-content-separator "|"
      rg-hide-command nil
      rg-align-position-numbers t
      rg-command-line-flags '("--follow"))

;;; project
(require 'project)
(setq project-mode-line t
      project-switch-commands '((project-find-file "Find file" f)
				(project-dired "Dired" d)
				(project-eshell "Eshell" e)
				(magit-project-status "Magit" ?m)))

;;; prog mode related
(add-hook 'prog-mode-hook 'electric-pair-mode)
(add-hook 'prog-mode-hook 'flymake-mode)

;; compile mode
(add-hook 'compilation-filter-hook 'ansi-color-compilation-filter)
(setq compilation-always-kill t
      compilation-context-lines 10
      compilation-disable-input t
      compilation-scroll-output 'first-error
      compilation-scroll-output t
      compilation-skip-threshold 2
      compilation-ask-about-save nil)

;; elisp
(add-hook 'emacs-lisp-mode-hook 'eldoc-mode)
(add-hook 'emacs-lisp-mode-hook 'flymake-mode)

(setq eldoc-idle-delay 1
      eldoc-documentation-strategy #'eldoc-documentation-default
      eldoc-echo-area-use-multiline-p nil)

;; direnv
(require 'direnv)
(direnv-mode t)
(setq direnv-always-show-summary nil)

;; eglot
(require 'eglot)

(add-hook 'python-mode-hook 'eglot-ensure)
(add-hook 'go-mode-hook 'eglot-ensure)
(add-hook 'nix-mode-hook 'eglot-ensure)

(setq eglot-send-changes-idle-time 0.1
      eglot-autoshutdown t)

(setq-default eglot-workspace-configuration
              '(:pylsp (:plugins (:ruff (:enabled t)))
                       :nil (:formatting (:command ["nixfmt"]))
                       :gopls (:usePlaceholders t
						:staticcheck t
						:completeUnimported t
						:matcher "CaseSensitive")))

;; uses https://github.com/nix-community/nixd for the LSP server instead of rnix
(with-eval-after-load 'eglot
  (add-to-list 'eglot-server-programs '(nix-mode . ("nil"))))

;; flymake
(require 'flymake)

(setq flymake-start-on-save-buffer t
      flymake-fringe-indicator-position 'left-fringe
      flymake-suppress-zero-counters t
      flymake-no-changes-timeout 9999
      elisp-flymake-byte-compile-load-path load-path)

;;; completion
(require 'consult)
(require 'consult-imenu)
(require 'consult-compile)
(require 'corfu)
(require 'corfu-popupinfo)
(require 'cape)
(require 'marginalia)
(require 'orderless)
(require 'vertico)

(global-corfu-mode t)
(corfu-popupinfo-mode t)
(marginalia-mode t)
(vertico-mode t)

(setq corfu-auto t)
(setq completion-styles '(orderless basic)
      completion-category-defaults nil)

(bind-key "C-x b" #'consult-buffer)
(bind-key "C-x r b" #'consult-bookmark)
(bind-key "C-x p b" #'consult-project-buffer)
(bind-key "C-c i" #'consult-imenu)
(bind-key "M-g e" #'consult-compile-error)
(bind-key "M-g M-g" #'consult-goto-line)
(bind-key "M-g m" #'consult-mark)
(bind-key "M-g k" #'consult-global-mark)

(add-hook 'after-init-mode 'global-corfu-mode)
(add-hook 'after-init-mode 'vertico-mode)

;;; theming
(require-theme 'modus-themes)
(setq modus-themes-italic-constructs t
      modus-themes-bold-constructs t
      modus-themes-mixed-fonts t
      modus-themes-variable-pitch-ui nil
      modus-themes-prompts '(italic bold)
      modus-themes-completions
      '((matches . (extrabold))
        (selection . (semibold italic text-also))))

(load-theme 'modus-operandi-tinted :no-confirm)

;;; 1Password Integration
(defvar fcuny/op-item-cache nil)

(defun fcuny/read-op-item (op-item-path)
  "Read and cache OP-ITEM-PATH item."
  (or (cdr (assoc op-item-path fcuny/op-item-cache))
      (let ((key (string-trim-right
                  (shell-command-to-string (format "op read '%s'" op-item-path)))))
        (unless (string-match-p "\\[ERROR\\]" key)
          (push (cons op-item-path key) fcuny/op-item-cache)
          key))))

;;; gptel
(require 'gptel)

(with-eval-after-load 'gptel
  (setq gptel-default-mode 'org-mode)
  (gptel-make-anthropic "Claude"
  :stream t
  :key (lambda () (fcuny/read-op-item "op://Private/anthropic llm/credential"))))

;;; aider
(require 'aidermacs)

(global-set-key (kbd "C-c a") 'aidermacs-transient-menu)

(with-eval-after-load 'aidermacs
  (setq aider-args '("--no-check-update" "--no-show-model-warnings"))
  (setq aidermacs-default-model "claude-3-7-sonnet-latest")
  (setenv "ANTHROPIC_API_KEY" (fcuny/read-op-item "op://Private/anthropic llm/credential")))

;;; server
(require 'server)
(unless (server-running-p)
  (server-start))

;;; early-init.el ends here
;; byte-compile-warnings: (not docstrings lexical noruntime)
;; End: