-
Notifications
You must be signed in to change notification settings - Fork 0
/
tk-editing.el
609 lines (468 loc) · 16.6 KB
/
tk-editing.el
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
;; -*- lexical-binding: t; -*-
;; Prefer UTF-8 encoding for input and output
(set-language-environment "UTF-8")
(prefer-coding-system 'utf-8)
;; Hard wrapping at column number
(setq-default fill-column 72)
;; Do not insert tabs in place of multiple spaces when formatting a
;; region
(setq-default indent-tabs-mode nil)
;; Default indentation
(setq-default standard-indent 2)
;; Disable double space indicating the end of a sentence. Affects
;; commands such as `fill-paragraph' and `forward-sentence'.
(setq-default sentence-end-double-space nil)
;; Add missing newline to file automatically when saving
(setq-default require-final-newline t)
;; Use text-mode for *scratch* buffer
(setq-default initial-major-mode 'text-mode)
;; Default major-mode
(setq-default major-mode 'text-mode)
;; Allow downcase-region (C-x C-l), upcase-region (C-x C-u)
(put 'downcase-region 'disabled nil)
(put 'upcase-region 'disabled nil)
;; macOS: allow entering special chars via Option key
(setq-default mac-option-modifier nil)
;; macOS: use Cmd key as Meta modifier
(setq-default mac-command-modifier 'meta)
;; macOS: use fn key as Super modifier
(setq-default mac-function-modifier 'super)
;; Typing text replaces active selection
(delete-selection-mode 1)
;; Revert file buffer if changed externally
(global-auto-revert-mode 1)
;; Save typing chars when answering yes-or-no-p questions
(defalias 'yes-or-no-p 'y-or-n-p)
(use-package locate
:config
;; macOS: Use `mdfind' for locate
(when (eq system-type 'darwin)
(setq-default locate-command "mdfind")))
(use-package apropos
:custom
(apropos-do-all t "Apropos commands perform more extensive searches than default"))
;; Enable narrowing to a region (hiding warning text).
(put 'narrow-to-region 'disabled nil)
(defun tk-editing/back-to-indentation-or-move-beginning-of-line (arg)
"Move point back to indentation of beginning of line.
Move point to the first non-whitespace character on this line.
If point is already there, move to the beginning of the line.
Effectively toggle between the first non-whitespace character and
the beginning of the line.
If ARG is not nil or 1, move forward ARG - 1 lines first. If
point reaches the beginning or end of the buffer, stop there.
Adapted from
`https://emacsredux.com/blog/2013/05/22/smarter-navigation-to-the-beginning-of-a-line/'"
(interactive "^p")
(setq arg (or arg 1))
(when (/= arg 1)
(let ((line-move-visual nil))
(forward-line (1- arg))))
(let ((orig-point (point)))
(back-to-indentation)
(when (= orig-point (point))
(move-beginning-of-line 1))))
(bind-key [remap move-beginning-of-line]
#'tk-editing/back-to-indentation-or-move-beginning-of-line)
;;; Navigation by moving in steps of 5
(defun tk-editing/next-line-5 ()
(interactive)
(ignore-errors (forward-line 5)))
(defun tk-editing/previous-line-5 ()
(interactive)
(ignore-errors (forward-line -5)))
(defun tk-editing/forward-char-5 ()
(interactive)
(ignore-errors (forward-char 5)))
(defun tk-editing/backward-char-5 ()
(interactive)
(ignore-errors (backward-char 5)))
(bind-keys ("C-n" . tk-editing/next-line-5)
("C-p" . tk-editing/previous-line-5)
("C-f" . tk-editing/forward-char-5)
("C-b" . tk-editing/backward-char-5))
;;; Editing
(defun tk-editing/join-line ()
(interactive)
(join-line -1))
(bind-key "M-J" #'tk-editing/join-line)
(defun tk-editing/eol-newline-and-indent ()
(interactive)
(end-of-line)
(newline-and-indent))
(bind-keys ("S-<return>" . tk-editing/eol-newline-and-indent)
("s-<backspace>" . delete-char))
;;; Commenting
(defun tk-editing/comment-or-uncomment-region-or-line ()
"Comments or uncomments either the current line (if no region
active) or region, moving position if point is at the beginning
of region."
(interactive)
(require 'subr-x)
(when-let ((region (tk-support/active-region-or-line)))
(let* ((region-begin-pos (car region))
(region-end-pos (cadr region))
(current-pos (point))
(current-line-begin-pos (line-beginning-position))
(next-line-begin-pos (line-beginning-position 2)))
(comment-or-uncomment-region region-begin-pos region-end-pos)
(when (and (= current-line-begin-pos region-begin-pos)
(= current-pos region-begin-pos)
(>= next-line-begin-pos region-end-pos))
(beginning-of-line 2)))))
(bind-keys ("C-c C" . comment-dwim)
("M-/" . tk-editing/comment-or-uncomment-region-or-line))
;;; Global navigation and window management
(bind-keys ("S-<down>" . windmove-down)
("S-<left>" . windmove-left)
("S-<right>" . windmove-right)
("S-<up>" . windmove-up)
("s-0" . delete-frame)
("s-1" . delete-other-frames)
("s-2" . make-frame-command)
("s-<down>" . scroll-up)
("s-<left>" . beginning-of-buffer)
("s-<right>" . end-of-buffer)
("s-<up>" . scroll-down))
;; Force your learning to avoid using M-<left|right> for movement
;; between words
(bind-keys ("M-<left>" . nil)
("M-<right>" . nil))
;;; Kill ring
(defun tk-editing/kill-ring-save (beg end)
"Like `kill-ring-save', but when called interactively with no
active region, copy the current line instead."
(interactive (tk-support/active-region-or-line))
(kill-ring-save beg end))
(defun tk-editing/kill-region (beg end)
"Like `kill-region', but when called interactively with no
active region, kill the current line instead."
(interactive (tk-support/active-region-or-line))
(kill-region beg end))
(defun tk-editing/file-path-to-clipboard ()
"Copy the current file name to the clipboard."
(interactive)
(let ((path (expand-file-name (or (buffer-file-name) default-directory))))
(when path
(let ((select-enable-clipboard t)) (gui-select-text path))
(kill-new path)
(message path))))
(bind-keys ([remap kill-ring-save] . tk-editing/kill-ring-save)
("M-c" . tk-editing/kill-ring-save)
([remap kill-region] . tk-editing/kill-region)
("C-c P" . tk-editing/file-path-to-clipboard)
("M-<kp-delete>" . kill-word))
;; Save clipboard strings into kill ring before replacing them
(setq-default save-interprogram-paste-before-kill t)
;; Mouse yanking inserts at the point instead of the location of the click
(setq-default mouse-yank-at-point t)
;; When killing, stop at subwords inside a CamelCase word
(add-hook 'prog-mode-hook #'subword-mode)
;;; CUA: enhanced rectangle support
(cua-selection-mode 1)
;;; Expand-reqion
(use-package expand-region
:ensure t
:bind
(("M-[" . er/contract-region)
("M-]" . er/expand-region)))
;;; Smartparens
;;;
;;; See https://github.com/Fuco1/smartparens
(use-package smartparens
:ensure t
:demand
:custom
(blink-matching-paren nil)
:config
(require 'smartparens-config)
(smartparens-global-mode 1)
(show-smartparens-global-mode 1)
(add-to-list 'tk-looks/minor-mode-alist
'(smartparens-mode (" SP" (:eval (if smartparens-strict-mode "/s" "")))))
:bind
(:map smartparens-mode-map
;; Navigation
("C-M-f" . sp-forward-sexp)
("C-M-b" . sp-backward-sexp)
("C-M-u" . sp-backward-up-sexp)
("C-M-n" . sp-up-sexp)
("C-M-<up>" . sp-up-sexp)
("C-M-d" . sp-down-sexp)
("C-M-<down>" . sp-down-sexp)
("C-M-p" . sp-backward-down-sexp)
("C-M-a" . sp-beginning-of-sexp)
("C-M-e" . sp-end-of-sexp)
("C-M-k" . sp-kill-sexp)
("C-M-n" . sp-next-sexp)
("C-M-p" . sp-backward-sexp)
("C-M-t" . sp-transpose-sexp)
;; Editing
("C-M-j" . sp-join-sexp)
("C-M-s" . sp-splice-sexp)
("C-M-S-s" . sp-split-sexp)
("C-M-<left>" . sp-forward-barf-sexp)
("C-M-<right>" . sp-forward-slurp-sexp)
;; Misc
("C-c )" . smartparens-strict-mode)))
;;; Other key bindings
(bind-keys ("C-c C-c M-x" . execute-extended-command)
("C-x C-b" . ibuffer)
("C-c U" . browse-url-at-point))
;;; Minibuffer
(setq-default max-mini-window-height 0.2)
;; Disable recursive minibuffers, because resuming them is often
;; confusing
(setq-default enable-recursive-minibuffers nil)
;; Remove duplicate elements from history lists
(setq-default history-delete-duplicates t)
;;; Tramp
(use-package tramp
:custom
(tramp-default-method "ssh")
;; Don't prompt confirmation on writing backup, auto-save, or lock
;; file for a `root'-owned remote file
(tramp-allow-unsafe-temporary-files t))
;;; ffap: find file (or url) at point
(use-package ffap
:custom
(ffap-machine-p-known 'reject "Disallow pinging a host for a symbol that looks like a host"))
;;; Dired
(use-package dired
:custom
(dired-listing-switches "-alh")
:config
;; Allow opening file, replacing current buffer
(put 'dired-find-alternate-file 'disabled nil))
;;; Uniquify: append dir name to buffers with similar filenames
(use-package uniquify
:demand
:custom
(uniquify-buffer-name-style 'forward))
;;; Saveplace: save point location in the buffer when revisiting the buffer
(use-package saveplace
:demand
:custom
(save-place-file (tk-init/user-emacs-path "saveplace"))
:config
(save-place-mode 1))
;;; Savehist: save minibuffer history
(use-package savehist
:demand
:custom
(savehist-file (tk-init/user-emacs-path "savehist"))
:config
(savehist-mode 1))
;;; Recentf: shows list of recently opened files
(use-package recentf
:demand
:init
(defun tk-editing/recentf-save-list-silent ()
"Save the list of recent files periodically. Normally, recentf saves
the list when Emacs exits cleanly. If Emacs crashes, that save is
probably not done."
(let ((inhibit-message t))
(recentf-save-list)))
:custom
(recentf-save-file (tk-init/user-emacs-path "recentf"))
(recentf-exclude
(list
(concat "\\`" (tk-init/user-emacs-path "recentf") "\\'")
(concat "\\`" (tk-init/user-emacs-path "elpa") "/.*-autoloads.elc?\\'"))
"Exclude recentf save file and Emacs ELPA autoloads")
:config
(run-at-time (* 5 60) (* 5 60) #'tk-editing/recentf-save-list-silent)
(recentf-mode 1))
;;; Hippie-expand
(use-package hippie-exp
:bind
(("s-SPC" . hippie-expand)))
;;; Vundo
;;;
;;; See https://github.com/casouri/vundo
(use-package vundo
:ensure t
:custom
(vundo-glyph-alist vundo-unicode-symbols)
:bind
(("C-x u" . vundo)))
;;; Projectile
(use-package projectile
:ensure t
:demand
:custom
(projectile-completion-system 'default)
:config
(dolist (l '(("js" "scss" "less" "css" "html")
("jsx" "scss" "less" "css" "html")
("scss" "jsx" "js" "html")
("less" "jsx" "js" "html")
("css" "jsx" "js" "html")))
(add-to-list 'projectile-other-file-alist l))
(projectile-register-project-type 'npm
'("package.json")
:compile "npm install"
:test "npm test"
:test-suffix ".test")
(projectile-mode 1)
:bind-keymap
("C-c p" . projectile-command-map)
:bind
(("C-c D" . projectile-dired)
("C-c O" . projectile-find-other-file)
("C-c d" . projectile-find-dir)
("C-c f" . projectile-find-file-dwim)
("C-c o" . projectile-toggle-between-implementation-and-test)))
;;; Orderless: completion style that enables space-separated input
;;; components
;;;
;;; See https://github.com/oantolin/orderless
(use-package orderless
:ensure t
:demand
:custom
;; Set the `basic' completion style as fallback in order to ensure
;; that completion commands which rely on dynamic completion tables,
;; such as `completion-table-dynamic' or `completion-table-in-turn',
;; work correctly
(completion-styles '(orderless basic))
;; Try the `basic' completion style first in order to make completion
;; work with Tramp
(completion-category-overrides '((file (styles basic partial-completion)))))
;;; Marginalia: add annotations to minibuffer completions
;;;
;;; See https://github.com/minad/marginalia
(use-package marginalia
:ensure t
:demand
:bind
(:map minibuffer-local-map
("M-A" . marginalia-cycle))
:config
(marginalia-mode 1))
;;; Vertico: minibuffer completion
;;;
;;; See https://github.com/minad/vertico
(use-package vertico
:ensure t
:demand
:config
(vertico-mode 1)
:after
(orderless))
;;; Consult: search and navigation commands
;;;
;;; See https://github.com/minad/consult
(use-package consult
:ensure t
:bind
(
;; Editing
("M-y" . consult-yank-pop)
;; Global navigation
("C-x b" . consult-buffer)
("C-x 4 b" . consult-buffer-other-window)
("C-x 5 b" . consult-buffer-other-frame)
("C-x p b" . consult-project-buffer)
("C-x r b" . consult-bookmark)
("C-x r l" . consult-register-load)
("C-x r s" . consult-register-store)
("C-M-r" . consult-register)
("C-c h" . consult-history)
("C-c m" . consult-man)
("C-c i" . consult-info)
([remap Info-search] . consult-info)
;; Buffer local navigation
("M-g e" . consult-compile-error)
("M-g M-g" . consult-goto-line)
("M-g o" . consult-outline)
("M-g m" . consult-mark)
("M-g k" . consult-global-mark)
("s-." . consult-imenu)
("C-s-." . consult-imenu-multi)
;; Searching
("M-s d" . consult-find)
("M-s c" . consult-locate)
("M-s g" . consult-grep)
("M-s G" . consult-git-grep)
("C-c s" . consult-ripgrep)
("C-S-s" . consult-line)
("M-s L" . consult-line-multi)
;; Misc
("C-c M-x" . consult-mode-command)
;; Minibuffer key map for ordinary input (no completion). For
;; example, used in `M-:' (`eval-expression'). See:
;; `https://www.gnu.org/software/emacs/manual/html_node/emacs/Minibuffer-Maps.html'
:map minibuffer-local-map
("M-s" . consult-history)
("M-r" . consult-history)
)
:custom
(consult-locate-args locate-command)
(xref-show-xrefs-function #'consult-xref)
(xref-show-definitions-function #'consult-xref)
(consult-project-function (lambda (_) (projectile-project-root)))
:after
(projectile)
)
;;; Deadgrep interface for ripgrep
(use-package deadgrep
:ensure t
:init
(defun tk-editing/deadgrep-show-result-other-window ()
"Show the result in another window at point, keeping the
current search result window."
(interactive)
(let ((buf (car-safe (deadgrep--buffers))))
(deadgrep-visit-result-other-window)
(when buf
(pop-to-buffer buf))))
:custom
;; Projectile determines project root
(deadgrep-project-root-function #'projectile-project-root)
:bind
(("C-c a" . deadgrep)
:map deadgrep-mode-map
("C-c C-f" . next-error-follow-minor-mode)
("C-o" . tk-editing/deadgrep-show-result-other-window) ; Same as `compilation-display-error'
))
;;; Symbol-overlay
(use-package symbol-overlay
:ensure t
:config
(let ((symbol-overlay-faces
(cl-loop for (face . color) in '((symbol-overlay-face-1 . "orange3")
(symbol-overlay-face-2 . "DeepPink3")
(symbol-overlay-face-3 . "cyan4")
(symbol-overlay-face-4 . "MediumPurple3")
(symbol-overlay-face-5 . "SpringGreen4")
(symbol-overlay-face-6 . "DarkOrange3")
(symbol-overlay-face-7 . "HotPink3")
(symbol-overlay-face-8 . "RoyalBlue1"))
collect `(,face ((t (:background ,color)))))))
(apply 'custom-set-faces symbol-overlay-faces))
(let ((map (make-sparse-keymap)))
(bind-keys :map map
("M-N" . symbol-overlay-switch-forward)
("M-P" . symbol-overlay-switch-backward)
("M-e" . symbol-overlay-echo-mark)
("M-n" . symbol-overlay-jump-next)
("M-p" . symbol-overlay-jump-prev)
("M-q" . symbol-overlay-query-replace)
("M-r" . symbol-overlay-rename)
("M-s" . symbol-overlay-isearch-literally)
("M-t" . symbol-overlay-toggle-in-scope)
("M-w" . symbol-overlay-save-symbol))
(setq symbol-overlay-map map))
:bind
(("s-O" . symbol-overlay-remove-all)
("s-o" . symbol-overlay-put)))
(use-package olivetti
:ensure t
:custom
(olivetti-style nil "Use margins to balance text")
:config
(add-to-list 'tk-looks/minor-mode-alist
'(olivetti-mode " Olv"))
:bind
(("C-c V" . olivetti-mode)))