-
Notifications
You must be signed in to change notification settings - Fork 0
/
emacs
250 lines (217 loc) · 9.95 KB
/
emacs
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
;; -*- mode: lisp; -*-
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Some usage notes — beyond obvious known navigation keyboard shortcuts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Reloading config file:
;; - `M-x eval-buffer` while in this file reloads config without restart
;;
;; Navigation:
;; - `M-r` moves cursor to top, middle, & bottom of current window.
;; - (https://emacs.stackexchange.com/a/36849)
;; - `M-g g X` is equivalent to `M-x goto-line X`
;;
;; Multiple cursors:
;; - `C-c e` to edit all lines in region
;; - `C-c n` to select next occurrence of region
;; - `C-c p` to select previous occurrence of region
;; - `C-c a` to select all occurrences of region
;;
;; Other:
;; - `C-h` is the help prefix key. `C-h k` is used to describe a keybinding.
;;
;; TODO should probs check out https://www.masteringemacs.org/ for more.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Set up MELPA (https://www.emacswiki.org/emacs/MELPA)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Package initialization
;;
;; Grabbed this section from https://stackoverflow.com/a/55058934/5802691
;;
;; This section should install desired packages when initializing emacs if they
;; haven't been installed. Add new packages to `my-packages` below. Note that
;; sometimes you may need to run `M-x package-refresh-contents` to update the
;; package list, as indicated in the comments below.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Init the package facility
(require 'package)
(package-initialize)
;; (package-refresh-contents)
;; ^this line is commented since refreshing packages is time-consuming and
;; should be done on demand, i.e. M-x package-refresh-contents
;; Declare packages
;; [1] https://github.com/zerolfx/copilot.el
;; [2] https://github.com/magnars/multiple-cursors.el
(setq my-packages
'(dash ;; added per [1]
diff-hl
editorconfig ;; added per [1]
f ;; added per [1]
multiple-cursors ;; [2]
s ;; added per [1]
solarized-theme
undo-tree
xclip))
;; Iterate on packages and install missing ones
(dolist (pkg my-packages)
(unless (package-installed-p pkg)
(package-install pkg)))
;; Install copilot.el. Following instructions from:
;; - https://github.com/zerolfx/copilot.el
;; - https://robert.kra.hn/posts/2023-02-22-copilot-emacs-setup/
;;
;; Note: this following line assumes that `~/.emacs` is a symlink to this file,
;; & that `copilot.el/` is a sibling directory to this file.
(add-to-list 'load-path (expand-file-name "copilot.el" (file-name-directory (file-truename "~/.emacs"))))
(require 'copilot)
(require 'multiple-cursors)
(require 'undo-tree)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; User configuration
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Package configuration ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Use diff-hl-mode in all buffers; this shows VC (e.g. Git) uncommitted changes
;; on left-side of emacs windows
;; https://github.com/dgutov/diff-hl
(global-diff-hl-mode)
(diff-hl-margin-mode 1)
(diff-hl-flydiff-mode 1)
;; With ChatGPT help (!):
;; - buffer-list-update-hook runs every time the buffer list is updated (e.g.
;; when switching buffers), so diff-hl-update [1] will be run now too.
;; - I also defined my own timer fxn (runs every 2 sec) to continuously update
;; the diff-hl state based on potential changes to file's git status per
;; commands done outside of emacs (e.g. if I `git add` & `git commit` a
;; file, before this timer the diff on left side of file wouldn't update
;; until buffer was refreshed). Technically this is does what `(add-hook *)`
;; line does below, so `buffer-list-update-hook` is superfluous, but keeping
;; around just in case I need to comment out timer fxn for performance
;; reasons. Whole thing is wrapped in "start timer" fxn to ensure only one
;; of these timers is running at once (e.g. while debugging &
;; M-x eval-buffer'ing this file a bunch of times in a row, can spawn many
;; timers, which can be trickier to debug).
;; [1] https://github.com/dgutov/diff-hl#integration
(add-hook 'buffer-list-update-hook 'diff-hl-update)
(defun continuous-diff-hl ()
(funcall 'diff-hl-update))
(defun start-running-timer ()
(when (boundp 'running-timer)
(cancel-timer running-timer))
(setq running-timer
(run-at-time nil 2 'continuous-diff-hl))) ;; run every 2 seconds
(start-running-timer)
;; Set solarized dark color theme
;; https://github.com/bbatsov/solarized-emacs
(load-theme 'solarized-dark t)
;; Enable xclip-mode by default
;; https://elpa.gnu.org/packages/xclip.html
(xclip-mode 1)
;;;; GH Copilot
;; Use copilot-mode in any programming mode (i.e. derived from prog-mode)
(add-hook 'prog-mode-hook 'copilot-mode)
;; tab key
(define-key copilot-completion-map (kbd "<tab>") 'copilot-accept-completion)
(define-key copilot-completion-map (kbd "TAB") 'copilot-accept-completion)
;; disable copilot warning about indent offset:
;; https://github.com/copilot-emacs/copilot.el/blob/733bff26450255e092c10873580e9abfed8a81b8/copilot.el#L111C12-L111C49
(setq copilot-indent-offset-warning-disable t)
;; TODO consider going through [1] & setting up more copilot stuff
;; [1] https://robert.kra.hn/posts/2023-02-22-copilot-emacs-setup/
;;;; Multiple cursors
;; mostly setting default configuration in repo README
;; https://github.com/magnars/multiple-cursors.el
(global-set-key (kbd "C-c e") 'mc/edit-lines)
(global-set-key (kbd "C-c n") 'mc/mark-next-like-this)
(global-set-key (kbd "C-c p") 'mc/mark-previous-like-this)
(global-set-key (kbd "C-c a") 'mc/mark-all-like-this)
;;;; Undo tree
;; https://www.emacswiki.org/emacs/UndoTree
(global-undo-tree-mode)
(setq undo-tree-visualizer-timestamps t)
;; Save undo tree history files in a ~/.emacs.d instead of current directory.
(setq undo-tree-history-directory-alist
'(("." . "~/.emacs.d/undo-tree-histories/")))
;; Custom code from ChatGPT. This makes it so that when I'm
;; in undo-tree-visualizer & I hit RET, it quits undo-tree-visualizer, kills the
;; undo-tree buffer, & restores the previous window configuration.
(defun my/undo-tree-visualizer-quit ()
"Quit undo-tree-visualizer, kill undo-tree buffer, and restore the previous window configuration."
(interactive)
(let ((buffer-to-restore (other-buffer (current-buffer))))
;; Quit undo-tree-visualizer
(undo-tree-visualizer-quit)
;; Kill the undo-tree buffer
(kill-buffer "*undo-tree*")
;; Restore the other buffer in the current window
(set-window-buffer (selected-window) buffer-to-restore)))
(with-eval-after-load 'undo-tree
(define-key undo-tree-visualizer-mode-map (kbd "RET") 'my/undo-tree-visualizer-quit))
;;;;;;;;;;;;;;;;;
;; Other stuff ;;
;;;;;;;;;;;;;;;;;
;; Display vertical line at column 80
;; https://www.gnu.org/software/emacs/manual/html_node/emacs/Displaying-Boundaries.html
(global-display-fill-column-indicator-mode)
(setq-default display-fill-column-indicator-column 80)
;; note: this has to be HTML code of unicode character, see https://unicode-table.com/
(setq-default display-fill-column-indicator-character 9474)
;; Display current line & column number
;; https://www.gnu.org/software/emacs/manual/html_node/efaq/Displaying-the-current-line-or-column.html
(global-display-line-numbers-mode 1)
(setq column-number-mode t)
;; Have C-v & M-v move cursor to top & bottom of buffer
;; https://www.gnu.org/software/emacs/manual/html_node/emacs/Scrolling.html
(setq scroll-error-top-bottom 'true)
;; Have Emacs create no backup files.
;; https://stackoverflow.com/questions/151945/how-do-i-control-how-emacs-makes-backup-files
;;
;; TODO: should probably try to actually save stuff under something like
;; ~/.emacs/backups or something, but I haven't been able to get this all
;; working correctly. See above answer & https://www.gnu.org/software/emacs/manual/html_node/elisp/Backup-Files.html
;; for more.
(setq make-backup-files nil)
;; Auto-refresh all buffers when files have changed on disk
;; https://www.gnu.org/software/emacs/manual/html_node/emacs/Auto-Revert.html
;; https://stackoverflow.com/a/1481706
(setq auto-revert-interval 2) ;; 2 seconds
(global-auto-revert-mode t)
;; Splitting window horizontal & vertically (similar bindings as in `.tmux`)
(global-set-key (kbd "C-x -") 'split-window-below)
(global-set-key (kbd "C-x |") 'split-window-right)
;; Turn off menu-bar-mode
(menu-bar-mode -1)
;; Set default python indent to 4 spaces
(setq python-indent-offset 4)
;; from https://stackoverflow.com/a/51966682, don't want this emacs warning
;; to show up.
(setq python-indent-guess-indent-offset-verbose nil)
;; Turn off startup message
;; from: https://emacs.stackexchange.com/a/437
(defun display-startup-echo-area-message () (message nil))
;; Keep search strings highlighted
;; https://stackoverflow.com/a/3780053
(setq lazy-highlight-cleanup nil)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Stuff that was automatically added
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(package-selected-packages '(diff-hl f undo-tree s dash editorconfig solarized-theme)))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
;; Added after first time I ran M-x list-timers
(put 'list-timers 'disabled nil)