aboutsummaryrefslogtreecommitdiff
path: root/config/emacs/init.el
blob: 83065b6f2bf8a1650c8dcabdde78f84027778ca3 (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
239
240
241
242
243
244
245
246
247
248
249
;;; init.el --- Emacs initialization -*- lexical-binding: t -*-

;;; Commentary:

;; Emacs initialization file. Contains my personal and evolving Emacs
;; configuration - what a rabbit hole!

;;; Code:

;;; -- Customizations --

(defcustom my/journal-file "~/Documents/Journal.org"
  "The path to my journal file."
  :tag "Journal File"
  :type '(string)
  :group 'convenience)

;;; -- Utility Functions --

(defun my/expand-file-name (name)
  "Expands file NAME relative to the `user-emacs-directory'."
  (expand-file-name name user-emacs-directory))

(defun my/show-trailing-whitespace ()
  "Show trailing whitespace for the current buffer."
  (setq-local show-trailing-whitespace t))

(defun my/auto-save-directory ()
  "The auto-save directory where the save file lives."
  (file-name-directory (concat auto-save-list-file-prefix "1-localhost")))

(defun my/open-init ()
  "Opens the Emacs init.el configuration file."
  (interactive)
  (find-file (my/expand-file-name "init.el")))

(defun my/open-local-init ()
  "Opens the system-specific configuration file."
  (interactive)
  (find-file custom-file))

(defun my/open-journal ()
  "Opens my journal."
  (interactive)
  (find-file my/journal-file))

(defun my/vc-spin-off-branch (new-branch)
  "Create and switch to a new branch from the current branch."
  (interactive "sSpin-off new branch: ")
  (let* ((default-directory (vc-root-dir))
         (current-branch (string-trim
                          (vc-git--run-command-string
                           nil "branch" "--show-current")))
         (upstream (or (ignore-errors
                         (string-trim
                          (vc-git--run-command-string
                           nil "rev-parse" "--abbrev-ref" "@{upstream}")))
                       "")))
    (when (string-empty-p new-branch)
      (user-error "Branch name cannot be empty"))
    (when (string-prefix-p "-" new-branch)
      (user-error "Branch name cannot start with '-'"))
    (vc-git-command nil 0 nil "checkout" "-b" new-branch)
    (if (or (string-empty-p current-branch) (string-empty-p upstream))
        (message "Spun off %s from %s" new-branch
                 (if (string-empty-p current-branch) "HEAD" current-branch))
      (condition-case err
          (let ((merge-base (string-trim
                             (vc-git--run-command-string
                              nil "merge-base" current-branch upstream))))
            (when (not (string-empty-p merge-base))
              (vc-git-command nil 0 nil "branch" "--force"
                              current-branch merge-base)
              (message "Spun off %s from %s; reset %s to merge-base with %s"
                       new-branch current-branch current-branch upstream)))
        (error (message "Spun off %s from %s (could not reset %s: %s)"
                        new-branch current-branch current-branch
                        (error-message-string err)))))
    (revert-buffer)))

(defun my/vc-push-set-upstream ()
  "Push the current branch to origin and set it as upstream, asynchronously."
  (interactive)
  (let* ((default-directory (vc-root-dir))
         (branch (string-trim
                  (vc-git--run-command-string
                   nil "branch" "--show-current")))
         (buffer-name "*vc-git: push*")
         (vc-dir-buffer (current-buffer)))
    (when (string-empty-p branch)
      (user-error "Not on a branch"))
    (let ((proc (vc-git-command
                 buffer-name 'async nil "push" "-u" "origin" branch)))
      (when (processp proc)
        (process-put proc 'my-branch branch)
        (process-put proc 'my-vc-dir-buffer vc-dir-buffer)
        (set-process-sentinel
         proc
         (lambda (proc event)
           (when (memq (process-status proc) '(exit signal))
             (let ((status (process-exit-status proc))
                   (branch (process-get proc 'my-branch))
                   (vc-dir-buffer (process-get proc 'my-vc-dir-buffer)))
               (if (eq status 0)
                   (progn
                     (message "Pushed %s to upstream origin/%s" branch branch)
                     (when (buffer-live-p vc-dir-buffer)
                       (with-current-buffer vc-dir-buffer
                         (revert-buffer t t))))
                 (message "Push failed for %s (see %s)"
                          branch (process-buffer proc))))))))
      (pop-to-buffer buffer-name))))

(defun my/setenv-from-shell (variable)
  "Set environment VARIABLE from the user's login shell."
  (let ((shell (getenv "SHELL"))
        (output ""))
    (make-process
     :name (format "setenv-%s" variable)
     :command `(,shell "-i" "-l" "-c"
                       ,(format "printf \"%%s\" \"$%s\"" variable))
     :buffer nil
     :noquery t
     :connection-type 'pipe
     :filter (lambda (_proc chunk)
               (setq output (concat output chunk)))
     :sentinel (lambda (proc _event)
                 (when (eq (process-status proc) 'exit)
                   (if (not (zerop (process-exit-status proc)))
                       (message "Failed to get %s from shell (exit %d)"
                                variable (process-exit-status proc))
                     (setenv variable output))))
     ;; We need to ignore `stderr` because starting interactive shells without
     ;; a proper TTY usually produces some noise.
     :stderr (make-pipe-process
              :name (format "setenv-%s-stderr" variable)
              :noquery t
              :filter #'ignore
              :sentinel #'ignore))))

;;; -- Emacs Configuration --

(use-package emacs
  :hook ((before-save . delete-trailing-whitespace)
         (prog-mode . display-line-numbers-mode)
         (prog-mode . my/show-trailing-whitespace))
  :bind (("C-c ," . my/open-local-init)
         ("C-c C-," . my/open-init)
         ("C-c C-j" . my/open-journal)
         ("C-." . completion-at-point)
         ("C-x k" . kill-current-buffer)
         ("M-/" . hippie-expand))
  :custom
  (use-short-answers t)
  (inhibit-startup-screen t)
  (initial-scratch-message "")
  (initial-major-mode 'fundamental-mode)
  (frame-title-format '("%b"))
  (ring-bell-function 'ignore)
  (sentence-end-double-space nil)
  (cursor-type 'bar)
  (indent-tabs-mode nil)
  (tab-width 2)
  (backup-by-copying t)
  (backup-directory-alist `((".*" . ,(my/expand-file-name "backup"))))
  (auto-save-file-name-transforms `((".*" ,(my/auto-save-directory) t)))
  (create-lockfiles nil)
  (tab-width 4)
  (tab-always-indent 'complete)
  (auto-revert-avoid-polling t)
  (remote-file-name-inhibit-locks t)
  (major-mode-remap-alist
   '((c-mode . c-ts-mode)
     (javascript-mode . js-ts-mode)
     (typescript-mode . typescript-ts-mode)))
  (custom-file (my/expand-file-name "local-init.el"))
  :init
  (menu-bar-mode -1)
  (tool-bar-mode -1)
  (when (fboundp 'scroll-bar-mode)
    (scroll-bar-mode -1))
  (which-key-mode +1)
  (savehist-mode +1)
  (column-number-mode +1)
  (delete-selection-mode +1)
  (global-auto-revert-mode +1)
  (load-theme 'modus-vivendi t)
  (my/setenv-from-shell "PATH")
  (load (my/expand-file-name "local-init.el") t t)
  (setq gc-cons-threshold (or my/gc-cons-threshold 800001)))

;;; -- Packages --

(use-package completion-preview
  :hook ((prog-mode . completion-preview-mode))
  :bind (:map completion-preview-active-mode-map
         ("M-n" . completion-preview-next-candidate)
         ("M-p" . completion-preview-prev-candidate)))

(use-package eglot
  :bind (:map eglot-mode-map
         ("M-q" . eglot-format))
  :config
  (fset #'jsonrpc--log-event #'ignore)
  ;; Some language servers monitor their parent's process ID and will
  ;; automatically kill themselve if the process ID is no longer there. Since
  ;; we run tools in containerized environments, we don't want this behaviour:
  ;; Emacs is running on the host, and its PID is not visible to the container.
  ;; Tell Eglot to **not** send a process ID.
  (setq eglot-withhold-process-id t))

(use-package flymake
  :bind (:map flymake-mode-map
         ("M-n" . flymake-goto-next-error)
         ("M-p" . flymake-goto-prev-error))
  :custom
  (flymake-indicator-type 'margins)
  (flymake-margin-indicators-string
   `((error " " compilation-error)
     (warning " " compilation-warning)
     (note " " compilation-info))))

(use-package markdown-ts-mode
  :mode "\\.md\\'"
  :config
  (require 'markdown-ts-mode-x))

(use-package project
  :custom
  (project-mode-line t))

(use-package rust-ts-mode
  :mode "\\.rs\\'")

(use-package vc
  :custom
  (vc-handled-backends '(Git)))
(use-package vc-dir
  :bind (:map vc-dir-mode-map
              ("C-c s" . my/vc-spin-off-branch)
              ("C-c P" . my/vc-push-set-upstream)))

(use-package xref
  :config
  (when (executable-find "rg")
    (setopt xref-search-program 'ripgrep)))

(provide 'init)
;;; init.el ends here.