aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d/init/ycp-prog.el
blob: 4e2daf536aa98dbcee23ec00471befbfba34415a (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
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
;;; ycp-prog.el -- My config for programming -*- lexical-binding: t -*-

;; Copyright (C) 2023 Free Software Foundation.

;; Author: Yuchen Pei <id@ypei.org>
;; Package-Requires: ((emacs "28.2"))

;; This file is part of dotfiles.

;; dotfiles is free software: you can redistribute it and/or modify it under
;; the terms of the GNU Affero General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; dotfiles is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
;; Public License for more details.

;; You should have received a copy of the GNU Affero General Public
;; License along with dotfiles.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; My config for programming. Covers comint, shell, eshell, compile,
;; xref, eglot, prog-mode etc.

;;; Code:



;;; comint, shell, eshell
(my-package comint
  (setq ansi-color-for-comint-mode t)
  (setq-default comint-scroll-to-bottom-on-input t)
  (setq-default comint-scroll-to-bottom-on-output nil)
  (setq-default comint-input-autoexpand 'input)
  (setq comint-prompt-read-only t)
  (setq comint-buffer-maximum-size 9999)
  (setq comint-completion-autolist t)
  (define-key comint-mode-map (kbd "C-<up>") 'windmove-up)
  (define-key comint-mode-map (kbd "C-<down>") 'windmove-down)
  (setq comint-input-ring-size 5000)
  (setq comint-input-ignoredups t)
  (setq comint-terminfo-terminal "dumb")
  (setq comint-password-prompt-regexp
        (concat comint-password-prompt-regexp
                "\\|^BECOME password:\\s *\\'"
	              "\\|^SSH password:\\s *\\'"))
  )

(my-package shell
  (:delay 5)
  (setq shell-command-prompt-show-cwd t)
  (setq shell-input-autoexpand 'input)

  (my-keybind shell-mode-map
    "<up>" #'comint-previous-input
    "<down>" #'comint-next-input
    "C-c C-k" #'comint-clear-buffer
    "C-c C-w" #'comint-write-output)
  (setq shell-command-prompt-show-cwd t)
  (add-hook 'shell-mode-hook
            (lambda ()
              (setq comint-input-ring-file-name "~/.bash_history")))
  (add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
  )

(my-package sh-script
  (:delay 5)
  (setq sh-basic-offset 2)
  (add-to-list 'auto-mode-alist '("PKGBUILD" . sh-mode))
)

(my-package my-prog
  (:delay 10)
  ;; tab-width 8 for ls etc.
  (add-hook 'shell-mode-hook 'my-set-tab-width-to-8)
  (my-keybind comint-mode-map "C-<return>"
    #'my-comint-send-input-and-return-prompt)
  (add-to-list 'my-buffer-create-functions
               '(shell-mode . my-shell-with-directory))
  (my-keybind shell-mode-map "<f2>" #'my-restart-shell)
  (add-hook 'shell-mode-hook 'my-shell-disable-company-if-remote)
  (my-keybind global-map "<f10>" #'my-switch-or-create-shell-buffer)
  )

(my-package eshell
  (:delay 60)
  (setq eshell-modules-list
        '(eshell-alias eshell-banner eshell-basic eshell-cmpl eshell-dirs eshell-glob eshell-hist eshell-ls eshell-pred eshell-prompt eshell-script eshell-term eshell-tramp eshell-unix))
  )

(my-package bash-completion
  (:install t)
  (:delay 15))

;;; prog modes: c, c++, elisp, js, css, ts,
(my-package prog-mode
  (add-hook 'prog-mode-hook #'display-line-numbers-mode)
  (add-hook 'prog-mode-hook (lambda () (setq-local tab-width 2))))

;; cmake
(my-package cmake-mode
  (:delay 60))

;;; eglot
(my-package eglot
  (:delay 10)
  (add-to-list 'eglot-server-programs
               '((php-mode phps-mode)
                 "phpactor" "language-server" "-vvv"))
  (add-hook 'before-save-hook (lambda () (interactive)
                                (when (eglot-managed-p)
                                  (unless (eq major-mode 'haskell-mode)
                                    (eglot-format-buffer)))))
  (setq-default eglot-workspace-configuration
                '((:pylsp
                   (plugins
                    (pylint
                     (enabled . t)
                     (executable . "/usr/bin/pylint"))))
                  (:haskell-language-server
                   (haskell
                    (formattingProvider . :json-false)))))
  (add-to-list 'eglot-server-programs
               '(haskell-mode
                 "haskell-language-server-wrapper" "--lsp" "--debug"))

  ;; I'm not sure why this is needed, but it throws an error if I remove it
  (cl-defmethod project-root ((project (head eglot-project)))
    (cdr project))

  (defun my-project-try-tsconfig-json (dir)
    (when-let* ((found (locate-dominating-file dir "tsconfig.json")))
      (cons 'eglot-project found)))

  (add-hook 'project-find-functions
            'my-project-try-tsconfig-json nil nil)

  (add-to-list 'eglot-server-programs
               '((typescript-mode) "typescript-language-server" "--stdio")))

(my-package cc-mode
  (:delay 5)
  (define-key c-mode-map (kbd "C-c C-c") 'compile)
  )

(my-package my-prog
  (:delay 10)
  (add-hook 'c-mode-hook 'my-c-set-compile-command)
  (my-keybind global-map "C-c 8" #'my-set-tab-width-to-8)
  )

;;; emacs-lisp mode
(my-package elisp-mode
  (my-keybind emacs-lisp-mode-map "C-c C-c" #'eval-buffer)
  (add-hook 'emacs-lisp-mode-hook (lambda () (auto-fill-mode 1)))
  (setq print-length 1000)
  (my-keybind global-map
    "<f5>" #'my-toggle-debug-on-error-quit
    "C-c e e" (lambda () (interactive)
			          (find-file (locate-user-emacs-file "init.el")))
    "C-c e d" (lambda () (interactive)
			          (find-file (locate-user-emacs-file "init")))
    "C-c e m" (lambda () (interactive)
			          (find-file (locate-user-emacs-file "lisp/my")))
    "C-c e b" (lambda () (interactive)
		            (my-switch-to-buffer-matching-major-mode
                 'emacs-lisp-mode)))
  ;; for deep recursion, e.g. in radix tree
  (setq max-specpdl-size 32000)
  )

;;; paredit
(my-package paredit
  (:install t)
  (add-hook 'emacs-lisp-mode-hook       #'enable-paredit-mode)
  (add-hook 'eval-expression-minibuffer-setup-hook #'enable-paredit-mode)
  (add-hook 'ielm-mode-hook             #'enable-paredit-mode)
  (add-hook 'lisp-mode-hook             #'enable-paredit-mode)
  (add-hook 'lisp-data-mode-hook             #'enable-paredit-mode)
  (add-hook 'lisp-interaction-mode-hook #'enable-paredit-mode)
  (add-hook 'scheme-mode-hook           #'enable-paredit-mode)
  (my-keybind paredit-mode-map
    "M-<left>" #'paredit-forward-barf-sexp
    "M-<right>" #'paredit-forward-slurp-sexp
    "C-<left>" nil
    "C-<right>" nil
    "M-?" nil
    "C-j" nil ;not ideal, we just want to unshadow it in lisp-interaction-mode
    "<RET>" #'paredit-newline
    "M-s" nil
    "M-o" #'paredit-splice-sexp
    "M-h" #'paredit-convolute-sexp)
  )

;;; flymake
(my-package flymake
  (:install t)
  (:delay 15)
  (my-keybind flymake-mode-map
    "M-n" #'flymake-goto-next-error
    "M-p" #'flymake-goto-prev-error))

(my-package my-prog
  (require 'xref)
  (my-override xref-query-replace-in-results))

(my-package js
  (:delay 60)
  (setq js-indent-level 2)
  (add-hook 'js-mode-hook 'subword-mode)
  (my-keybind js-mode-map "M-." #'xref-find-definitions)
  )

(my-package typescript-mode
  (:delay 60)
  (:install t)
  (add-to-list 'auto-mode-alist '("\\.ts\\'" . typescript-mode))
  (setq typescript-indent-level 2)
  )

(my-package tide
  (:delay 60)
  (require 'my-tide)
  ;; aligns annotation to the right hand side
  (setq company-tooltip-align-annotations t)

  ;; formats the buffer before saving
  (add-hook 'before-save-hook 'tide-format-before-save)

  (add-hook 'typescript-mode-hook #'setup-tide-mode)
  (add-hook 'typescript-mode-hook 'subword-mode)
  )

(my-package web-mode
  (:delay 60)
  (:install t)
  (add-to-list 'auto-mode-alist '("\\.tsx\\'" . web-mode))
  (add-to-list 'auto-mode-alist '("\\.jsx\\'" . web-mode))
  (setq web-mode-content-types-alist '(("jsx" . "\\.js[x]?\\'")))
  (setq web-mode-markup-indent-offset 2)
  (setq web-mode-code-indent-offset 2)
  (add-hook 'web-mode-hook
            (lambda ()
              (when (string-equal "tsx" (file-name-extension buffer-file-name))
                (my-setup-tide-mode))))
  )

(my-package flycheck
  (:delay 60)
  (flycheck-add-mode 'typescript-tslint 'web-mode)
  )

(my-package css-mode
  (setq css-indent-offset 2))

(my-package haskell-mode
  (:install t)
  (:delay 60)
  ;; do we need to require haskell-command?
  (setq haskell-compile-command
        "ghc -Wall -ferror-spans -fforce-recomp -dynamic -c %s")
  (my-keybind haskell-mode-map "C-c C-c" #'haskell-compile)
  (setq haskell-mode-stylish-haskell-path "brittany")
  (setq haskell-stylish-on-save t)
  (my-setq-from-local haskell-hoogle-url)
  (add-hook 'haskell-mode-hook 'subword-mode)
  (add-hook 'haskell-mode-hook 'interactive-haskell-mode)
  (add-hook 'haskell-mode-hook
            (lambda ()
              (set (make-local-variable 'company-backends)
                   (append '((company-capf company-dabbrev-code))
                           company-backends))))
  (setq haskell-interactive-popup-errors t)
  (setq haskell-process-suggest-hoogle-imports t)
  (setq haskell-process-log t)
  (cl-pushnew '(haskell-process-use-ghci . t)
              safe-local-variable-values :test #'equal)
  (cl-pushnew '(haskell-indent-spaces . 4)
              safe-local-variable-values :test #'equal)
  (cl-pushnew '(haskell-tags-on-save . t)
              safe-local-variable-values :test #'equal)
  (cl-pushnew '(haskell-indentation-where-post-offset . 2)
              safe-local-variable-values :test #'equal)
  (cl-pushnew '(haskell-indentation-where-pre-offset . 2)
              safe-local-variable-values :test #'equal)
  (cl-pushnew '(haskell-indentation-ifte-offset . 4)
              safe-local-variable-values :test #'equal)
  (cl-pushnew '(haskell-indentation-left-offset . 4)
              safe-local-variable-values :test #'equal)
  (cl-pushnew '(haskell-indentation-starter-offset . 1)
              safe-local-variable-values :test #'equal)
  (cl-pushnew '(haskell-indentation-layout-offset . 4)
              safe-local-variable-values :test #'equal))

(my-package hcel
  ;; fixme: credential
  (:delay 60)
  (:install t)
  ;; The official one is https://haskell-code-explorer.mfix.io
  (my-setq-from-local hcel-host)
  (my-keybind global-map
    "C-c hh" #'hcel
    "C-c hi" #'hcel-global-ids
    "C-c ho" #'hcel-help)
  )

(my-package hcel-haddorg
  (:delay 60)
  ;; fixme: credential
  (my-setq-from-local hcel-haddorg-dir))

(my-package ggtags
  (:install t)
  (:delay 60)
  (my-keybind ggtags-navigation-map
    ggtags-navigation-map "M-<" nil
    ggtags-navigation-map "M->" nil))

(my-package phps-mode
  (:install t)
  (:delay 60)
  (add-to-list 'auto-mode-alist '("\\.\\(?:php[s345]?\\|phtml\\)\\'" . phps-mode))
  (add-to-list 'auto-mode-alist '("\\.\\(?:php\\.inc\\|stub\\)\\'" . phps-mode))
  (add-to-list 'auto-mode-alist '("/\\.php_cs\\(?:\\.dist\\)?\\'" . phps-mode))
  (add-hook 'before-save-hook (lambda () (interactive)
                                (when (eq major-mode 'phps-mode)
                                  (phps-mode-format-buffer)))))

(my-package crystal-mode
  (:delay 60)
  (require 'flycheck-crystal))

(my-package imenu
  (:delay 5)
  (my-keybind global-map "C-c i" #'imenu)
)

;;; proof-general
(my-package proof-general
  (:install t)
  (:delay 60)
  (setq coq-prog-name "/usr/bin/coqtop")
  (setq coq-compiler "~/.opam/default/bin/coqc")
  (setq coq-prog-env '("PATH=/usr/bin/:$HOME/.opam/default/bin/"))
  (setq coq-diffs 'on)
  (setq proof-three-window-enable nil))

;;; tree-sitter
(add-to-list 'load-path (locate-user-emacs-file "lisp/elisp-tree-sitter/core"))
(add-to-list 'load-path (locate-user-emacs-file "lisp/elisp-tree-sitter/lisp"))
(my-package tree-sitter
  (:delay 15)
  (require 'tree-sitter-hl)
  (require 'tree-sitter-langs)
  (require 'tree-sitter-debug)
  (require 'tree-sitter-query)
  (add-to-list 'tree-sitter-major-mode-language-alist
               '(haskell-mode . haskell))
  (add-to-list 'tree-sitter-major-mode-language-alist
               '(phps-mode . php))
  (global-tree-sitter-mode)
  (add-hook 'tree-sitter-after-on-hook #'tree-sitter-hl-mode))

;;; sml
(my-package sml-mode
  (:install t)
  (:delay 60)
  (setq sml-indent-level 2))

(provide 'ycp-prog)
;;; ycp-prog.el ends here