-
Notifications
You must be signed in to change notification settings - Fork 0
/
tk-dev.el
606 lines (455 loc) · 15.7 KB
/
tk-dev.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
;; -*- lexical-binding: t; -*-
;;; Ediff
(use-package ediff-wind
:custom
;; Use current frame for control panel
(ediff-window-setup-function 'ediff-setup-windows-plain)
;; I find it easier to read a diff by having one buffer on the left and
;; another on the right
(ediff-split-window-function 'split-window-horizontally))
;;; Interactive regexp builder
(use-package re-builder
:custom
(reb-re-syntax 'string)
:bind
("C-c R" . re-builder)
(:map reb-mode-map
("C-c C-k" . reb-quit)
("M-n" . reb-next-match)
("M-p" . reb-prev-match)))
;;; Compilation
(use-package compile
:bind
(:map compilation-mode-map
("M-N" . compilation-next-file)
("M-P" . compilation-previous-file)))
;;; ElDoc
(use-package eldoc
:custom
(eldoc-echo-area-prefer-doc-buffer nil)
(eldoc-echo-area-use-multiline-p
(- max-mini-window-height 0.05)
"Subtract a bit from the max height in order to avoid vertical \
content clipping (maybe the truncation algorithm has problems \
with bitmaps)")
(eldoc-documentation-strategy 'eldoc-documentation-compose-eagerly))
;;; Company
(use-package company
:ensure t
:demand
:custom
(company-idle-delay 0.5)
(company-minimum-prefix-length 2 "The minimum prefix length before showing idle completion")
(company-tooltip-align-annotations t "Align annotations to the right tooltip border")
(company-dabbrev-downcase nil "Don't lowercase completion candidates (dabbrev backend)")
(company-dabbrev-ignore-case t "Ignore case when collecting completion candidates and copy candidate verbatim")
(company-dabbrev-code-ignore-case t "Ignore case when collecting completion candidates and copy candidate verbatim")
(company-backends '(company-semantic
company-clang
(company-capf company-dabbrev-code company-gtags)
company-files
company-keywords
company-dabbrev)
"Use relevant completion engines only. Especially, put \
`company-capf' and `company-dabbrev-code' into same group so that \
the latter adds candidates the former misses.")
:config
(global-company-mode 1)
:bind
(("C-<tab>" . company-complete)))
;;; Snippets
(use-package yasnippet
:ensure t
:hook
(lsp-mode . yas-minor-mode-on)
:commands
(yas-expand-snippet))
;;; Flycheck
(use-package flycheck
:ensure t
:demand
:custom
(flycheck-disabled-checkers '(emacs-lisp-checkdoc json-python-json))
(flycheck-temp-prefix ".~flycheck")
:config
(global-flycheck-mode 1))
;;; Tree-sitter
(use-package treesit
:if (>= emacs-major-version 29)
:init
(defvar tk-dev/treesit-language-source-alist
'((bash "https://github.com/tree-sitter/tree-sitter-bash" "v0.23.1")
(css "https://github.com/tree-sitter/tree-sitter-css" "v0.23.0")
(dockerfile "https://github.com/camdencheek/tree-sitter-dockerfile" "v0.2.0")
(javascript "https://github.com/tree-sitter/tree-sitter-javascript" "v0.20.1")
(json "https://github.com/tree-sitter/tree-sitter-json" "v0.24.1")
(ruby "https://github.com/tree-sitter/tree-sitter-ruby" "v0.23.0")
(tsx . ("https://github.com/tree-sitter/tree-sitter-typescript" "v0.20.3" "tsx/src"))
(typescript . ("https://github.com/tree-sitter/tree-sitter-typescript" "v0.20.3" "typescript/src"))
(yaml "https://github.com/ikatyang/tree-sitter-yaml" "v0.5.0"))
"Tree-sitter language grammar configuration")
(defun tk-dev/treesit-install-language-grammars (force-install-all)
"Install Tree-sitter grammars for configured languages if grammars
are missing. If FORCE-INSTALL-ALL is t, then install grammars for
all configured languages regardless whether they are already
installed. Use FORCE-INSTALL-ALL to update grammars."
(interactive
(list (y-or-n-p "Force install all configured Tree-sitter language grammars?")))
(pcase-dolist (`(,grammar . ,_rest) tk-dev/treesit-language-source-alist)
;; Install `grammar' if forced or if we don't have it installed already
(when (or force-install-all
(not (treesit-language-available-p grammar)))
(message "Installing Tree-sitter language grammar %s…" grammar)
(treesit-install-language-grammar grammar))))
:custom
(treesit-max-buffer-size (let ((mb (* 1024 1024))) (* 100 mb)))
:config
(dolist (lang tk-dev/treesit-language-source-alist)
(add-to-list 'treesit-language-source-alist lang))
(tk-dev/treesit-install-language-grammars nil)
(dolist (mapping '((sh-mode . bash-ts-mode)
(css-mode . css-ts-mode)
(js-mode . js-ts-mode)
(json-mode . json-ts-mode)
(ruby-mode . ruby-ts-mode)
(typescript-mode . tsx-ts-mode)
(yaml-mode . yaml-ts-mode)))
(add-to-list 'major-mode-remap-alist mapping)))
;;; Magit
(use-package magit
:ensure t
:custom
(magit-auto-revert-mode nil "Disable `magit-auto-revert-mode', because we're using global-auto-revert-mode")
:bind
(("C-x g" . magit-status)))
;; Disable Emacs' Version Control interface
;; (setq-default vc-handled-backends '(RCS CVS SVN SCCS SRC Bzr Git Hg Mtn))
(setq-default vc-handled-backends nil)
;;; Xref
;; Add additional keybinding, as macOS interprets M-? to show menu bar
(bind-key "C-M-/" #'xref-find-references)
;;; ggtags frontend for GNU global
(use-package ggtags
:ensure t
:init
(defun tk-dev/make-gtags (rootdir)
"Make gtags files to the current project.
If called with a prefix, specify the directory to make gtags files for."
(interactive (cl-flet ((read-dir ()
(read-directory-name "Make GTAGS to: " nil nil t)))
(let ((dir (if current-prefix-arg
(read-dir)
(if-let ((proj-dir (projectile-project-root)))
proj-dir
(read-dir)))))
(list dir))))
(let ((current-prefix-arg nil)) ; reset as it might affect future commands
(ggtags-create-tags rootdir)))
(defun tk-dev/ggtags-adjust-tag-bounds-for-scss-mode (org-bounds)
"Adjusts tag bounds so that `$var' gets converted to `var'. The
dollar sign does not belong to SCSS variable symbol in our
configuration for GNU Global."
(pcase org-bounds
(`(,org-beg . ,org-end)
(let* ((tag-str (buffer-substring org-beg org-end))
(dollar-prefix-length (tk-support/string-prefix-length-with-char ?$ tag-str))
(new-beg (+ org-beg dollar-prefix-length))
(new-bounds (if (< new-beg org-end)
(cons new-beg org-end)
org-bounds)))
new-bounds))))
(defun tk-dev/ggtags-bounds-of-tag ()
(let ((bounds (bounds-of-thing-at-point 'symbol)))
(pcase major-mode
('scss-mode (tk-dev/ggtags-adjust-tag-bounds-for-scss-mode bounds))
(- bounds))))
:custom
(ggtags-bounds-of-tag-function #'tk-dev/ggtags-bounds-of-tag)
:config
;; Don't change `mode-line-buffer-identification', because we
;; show project root dir in the mode line with projectile
(setq ggtags-mode-line-project-name nil)
(add-to-list 'tk-looks/minor-mode-alist
'(ggtags-mode (:eval (if ggtags-navigation-mode " GG[nav]" " GG"))))
(unbind-key "M-]" ggtags-mode-map)
:bind
(("C-c t" . ggtags-mode)
("C-c T" . tk-dev/make-gtags)
:map ggtags-mode-map
("C-M-/" . ggtags-find-reference)))
;;; LSP
(use-package lsp-mode
:ensure t
:init
;; See `(setenv "LSP_USE_PLISTS" "true")' in `early-init.el'
(setq lsp-use-plists t)
:custom
(lsp-eldoc-render-all t)
(lsp-progress-prefix " … " "Less obtrusive progress status")
:config
(add-to-list 'tk-looks/minor-mode-alist
(assq 'lsp-mode minor-mode-alist))
:hook
((lsp-mode . lsp-enable-which-key-integration))
:commands
(lsp lsp-deferred)
:bind
(("C-c l" . lsp)
:map lsp-mode-map
("C-c H" . lsp-describe-thing-at-point)
("C-M-/" . lsp-find-references)
("C-M->" . lsp-find-type-definition)))
(use-package lsp-modeline
:custom
(lsp-modeline-code-action-fallback-icon "?" "Less obtrusive code action icon"))
(use-package lsp-ui
:ensure t
:custom
(lsp-ui-doc-delay 0.5 "Number of seconds before showing documentation popup")
(lsp-ui-doc-position 'top)
(lsp-ui-doc-max-width 180)
(lsp-ui-doc-max-height 40)
(lsp-ui-doc-include-signature t)
:commands
(lsp-ui-mode)
:bind
(:map lsp-ui-mode-map
("C-c h" . lsp-ui-doc-toggle)
("s->" . lsp-ui-imenu))
:after
(lsp-mode))
;;; Prettier: format buffer with `prettier' upon save automatically
(use-package prettier
:ensure t
:init
(defun tk-dev/prettier-mode-advice-not-on-file-remote-p (&rest _r)
(not (ignore-errors (file-remote-p (buffer-file-name (current-buffer))
'method))))
:config
(advice-add #'prettier-mode :before-while #'tk-dev/prettier-mode-advice-not-on-file-remote-p)
(add-to-list 'tk-looks/minor-mode-alist '(prettier-mode (" Prettier")) t)
:commands
(prettier-mode))
;;; CSS and SCSS
(use-package css-mode
:custom
(css-indent-offset 2)
:hook
((scss-mode . ggtags-mode)))
;;; C family
(setq-default c-basic-offset 4)
(setq-default c-default-style '((awk-mode . "awk")
(java-mode . "java")
(other . "linux")))
;;; js-mode for `.js' and `.jsx' sources
(use-package js
:init
(defun tk-dev/js-lsp-mode-hook ()
;; Don't enable LSP in `json-mode' or `jsonc-mode' (which derive
;; from `js-mode' via the `javascript-mode' alias)
(when (not (member major-mode '(json-mode jsonc-mode)))
(lsp-deferred)))
:custom
(js-indent-level 2)
:config
;; `js-base-mode' is the parent mode of both `js-mode' and
;; `js-ts-mode'
(add-hook 'js-base-mode-hook #'tk-dev/js-lsp-mode-hook)
(unbind-key "M-." js-mode-map)
(unbind-key "M-." js-ts-mode-map)
:hook
((js-base-mode . prettier-mode))
:mode
(("\\.[cm]?jsx?\\'" . js-mode)
("\\.javascript\\'" . js-mode))
:interpreter
(("node\\(?:js\\)?" . js-mode)))
;;; TypeScript for `.ts' and `.tsx' sources
(use-package typescript-mode
:ensure t
:custom
(typescript-indent-level 2)
:hook
((typescript-mode . lsp-deferred)
(typescript-mode . prettier-mode))
:mode
("\\.tsx?\\'"))
(use-package typescript-ts-mode
:hook
;; `typescript-ts-base-mode' is the parent mode for both
;; `typescript-ts-mode' and `typescript-ts-base-mode'
((typescript-ts-base-mode . lsp-deferred)
(typescript-ts-base-mode . prettier-mode)))
;; HTML
(use-package mhtml-mode
:hook
((mhtml-mode . prettier-mode))
:mode
("/\\.x?html\\'"))
;;; JSON
(use-package json-mode
:ensure t
;; Don't add prettier-mode hook, because json-mode derives from js-mode
:mode
("\\.json\\'"))
(use-package json-ts-mode
:hook
((json-ts-mode . prettier-mode)))
;;; YAML
(use-package yaml-mode
:ensure t
:hook
((yaml-mode . prettier-mode)
(yaml-mode . ggtags-mode))
:mode
("/\\.ya?ml\\'"
"/\\.gemrc\\'"))
(use-package yaml-ts-mode
:hook
((yaml-ts-mode . prettier-mode)
(yaml-ts-mode . ggtags-mode)))
;;; ELisp
(use-package elisp-mode
:config
(add-hook 'emacs-lisp-mode-hook #'smartparens-strict-mode)
:after
(smartparens))
(use-package macrostep
:ensure t
:bind
(:map emacs-lisp-mode-map
("C-c e" . macrostep-expand)))
;;; Clojure
(use-package clojure-mode
:config
(define-clojure-indent
(ANY 2)
(DELETE 2)
(GET 2)
(HEAD 2)
(OPTIONS 2)
(PATCH 2)
(POST 2)
(PUT 2)
(api 'defun)
(context 1)
(defroutes 'defun)
(describe 1)
(it 1))
(add-hook 'clojure-mode-hook #'smartparens-strict-mode)
:mode
("/\\.clj\\'"
"/\\.edn\\'")
:after
(smartparens))
;;; CIDER
(use-package cider
:pin melpa-stable
:init
(shell-command (mapconcat #'identity
`("touch ~/.cider_history"
"chmod 600 ~/.cider_history")
" && ")
t)
:init
(defun tk-dev/cider-mode-hook ()
(local-set-key (kbd "C-c B") #'cider-connection-browser)
(local-set-key (kbd "C-c M-l") #'cider-inspect-last-result)
(local-set-key (kbd "C-c M-R") #'cider-restart))
:custom
(cider-eval-result-prefix ";; => ")
(cider-repl-result-prefix ";; => ")
(cider-repl-history-file "~/.cider_history")
(cider-prompt-for-symbol nil "Attempt to use the symbol at point as input for `cider-find-var', and only prompt if that throws an error")
(cider-inject-dependencies-at-jack-in nil "I want to inject dependencies manually via `~/.lein/profiles.clj'. Otherwise Leiningen's `:pedantic? :abort' setting causes `lein repl' to abort due to overriding version of `org.clojure/tools.nrepl'.")
(cider-mode-line '(" " (:eval (cider--modeline-info))) "Shorten mode line info")
:config
(add-to-list 'tk-looks/minor-mode-alist '(cider-popup-buffer-mode (" cider-tmp")))
(add-to-list 'tk-looks/minor-mode-alist '(cider-auto-test-mode (cider-mode " Test")))
(add-to-list 'tk-looks/minor-mode-alist '(cider--debug-mode " DEBUG"))
(add-to-list 'tk-looks/minor-mode-alist '(cider-mode cider-mode-line))
(add-hook 'cider-mode-hook #'tk-dev/cider-mode-hook)
(add-hook 'cider-repl-mode-hook #'tk-dev/cider-mode-hook)
:custom-face
(cider-result-overlay-face ((t (:background "grey30"))))
:after
(clojure-mode))
;;; Haskell
(use-package haskell-mode
:init
(defun tk-dev/haskell-mode-hook ()
(haskell-indentation-mode)
(haskell-decl-scan-mode)
(interactive-haskell-mode))
:custom
(haskell-process-suggest-remove-import-lines t)
(haskell-process-auto-import-loaded-modules t)
(haskell-process-log t)
(haskell-process-type 'cabal-repl)
:config
(add-hook 'haskell-mode-hook #'tk-dev/haskell-mode-hook)
:bind
(:map haskell-mode-map
("<f8>" . haskell-navigate-imports)
("C-`" . haskell-interactive-bring)
("C-c c" . haskell-process-cabal)
("C-c o" . haskell-hoogle))
:mode
("/\\.hs\\'"))
;; Shell scripts
(use-package sh-script
:hook
((sh-mode . ggtags-mode)
(bash-ts-mode . ggtags-mode)))
;;; Python
(use-package python
:hook
((python-mode . ggtags-mode)))
;;; Ruby
(use-package ruby-mode
:hook
((ruby-mode . ggtags-mode)
(ruby-ts-mode . ggtags-mode))
:mode
("/.Brewfile\\'"
"/Gemfile-[[:alnum:]]+\\'"))
;;; Rust
(use-package rust-mode
:custom
(rust-format-on-save t)
(rust-rustfmt-switches '())
(lsp-rust-analyzer-cargo-watch-command "clippy")
:bind
(:map rust-mode-map
("C-c e" . lsp-rust-analyzer-expand-macro)
("C-x C-e" . lsp-rust-analyzer-run))
:hook
((rust-mode . lsp-deferred))
:mode
("/\\.rs\\'"))
;;; Markdown
(use-package markdown-mode
:ensure t
:config
;; Don't include XHTML DTD for generated HTML, as it affects CSS
;; styles
(advice-add #'markdown-output-standalone-p
:override
(lambda () t))
:custom
(markdown-command "marked --gfm")
(markdown-hide-urls nil)
(markdown-asymmetric-header t)
(markdown-live-preview-delete-export 'delete-on-export)
:mode
(("\\.markdown\\'" . gfm-mode)
("\\.md\\'" . gfm-mode)))
;;; Dockerfile
(use-package dockerfile-ts-mode
:mode
("/Dockerfile\\(?:\\.[^/]+\\)?\\'"))
;;; Configuration files
(add-to-list 'auto-mode-alist '("/\\.aws/config\\'" . conf-unix-mode))
(add-to-list 'auto-mode-alist '("/\\.aws/credentials\\'" . conf-unix-mode))
(add-to-list 'auto-mode-alist '("/\\.s3cfg\\'" . conf-unix-mode))
(add-to-list 'auto-mode-alist '("/Cargo\\.lock\\'" . conf-toml-mode))