-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemacs.org
More file actions
2582 lines (2398 loc) · 92.2 KB
/
emacs.org
File metadata and controls
2582 lines (2398 loc) · 92.2 KB
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#+TITLE: Emacs Config
#+SETUPFILE: .setup.org
#+PROPERTY: header-args:elisp+ :tangle ~/.emacs.d/haris/default.el
#+begin_src elisp :exports none
;; -*- mode: emacs-lisp; -*- vim: ft=lisp sw=2
#+end_src
Emacs is a very versatile tool. I use it as:
- a text editor (of course)
- a [[*Git][git GUI]] (magit)
- a multilingual [[*Programming languages][IDE for quick and simple work]]
- a [[*Vterm][special-purpose terminal emulator]] (vterm)
- for [[*Org mode][note-taking and organization]] (org-mode)
- as a [[*Comint][superior interface for many interpreters]] (comint)
What's more, I'm using a distribution of Emacs called Spacemacs, which comes
with many sensible defaults and simplifies the installation and setup of
packages. Because it is so feature-rich, Emacs can take a long time to start up.
That's why I have set up a systemd service that runs an emacs daemon on system
startup. When I want to launch emacs to perform a task, I run [[file:~/.haris/scripts.org::*=myemacs=][myemacs]] (a custom
command that wraps [[man:emacsclient][emacsclient]]), which attaches to the daemon, without the need
to load the cruft each time.
* General
** Configuration loading
The main generated user configuration file is [[~/.emacs.d/haris/root.el]]. This
file is loaded by spacemacs and in turn, it loads the other files based on
the purpose of the current emacs daemon (by reading the =server-name= variable).
Currently, only the default daemon =emacs= is used, but this might change in the future.
#+begin_src elisp :tangle ~/.emacs.d/haris/root.el
(load-file "~/.emacs.d/haris/default.el")
#+end_src
** Setup
:PROPERTIES:
:header-args:bash+: :tangle ~/.haris-setup/setup-emacs.sh
:END:
Run this script in order to perform a first-time setup of Emacs: [[~/.haris-setup/setup-emacs.sh]].
#+begin_src bash
set -e
#+end_src
*** Initialize spacemacs repo
#+begin_src bash
mkdir -p ~/.emacs.d
pushd ~/.emacs.d >/dev/null
if [ ! -d .git ]; then
git init
git remote add origin 'https://github.com/veracioux/spacemacs'
git remote set-url --push origin 'git@github.com:veracioux/spacemacs'
git remote add upstream 'https://github.com/syl20bnr/spacemacs'
git checkout --recurse-submodules haris/main
fi
popd >/dev/null
#+end_src
*** Interactively install spacemacs
#+begin_src bash
ln -s ~/.haris/.spacemacs ~/.spacemacs || true
echo "Emacs will be launched now. Follow the prompts to setup spacemacs"
echo "When done, you can close the emacs window"
read -n1 -p 'Press any key to continue: '
# NOTE: In same cases I observed that the GUI won't run on initial spacemacs
# startup, so I used --no-window-system
COLORTERM=truecolor LSP_USE_PLISTS=true emacs --debug-init --no-window-system
#+end_src
** Theme
I use a slightly customized version of the beautiful Dracula theme. Make sure
you perfomed the [[*Setup][setup]] in order to have the theme available.
*** Configure custom theme directory
#+begin_src elisp
(setq custom-theme-directory "~/.emacs.d/private/themes")
#+end_src
*** Theme: Dracula
#+begin_src elisp
(load-theme 'dracula t)
(add-hook 'after-make-frame-functions
(defun haris/load-theme-delayed (frame)
;; Without this the theme only loads after a second frame is created
(run-with-timer 0 nil
(lambda ()
(load-theme 'dracula t)))
(remove-hook 'after-make-frame-functions #'haris/load-theme-delayed)))
#+end_src
** API keys
#+begin_src elisp
(defun haris/api-keys ()
(interactive)
(setq openai-key (password-store-get "openai/api-key"))
(setq anthropic-key (password-store-get "anthropic/api-key"))
(provide 'haris/api-keys))
#+end_src
** Package bootstrapping
I normally use =use-package= for installing packages (with =melpa= as the default source).
I use =straight= to clone some packages that are not available on =melpa=.
#+NAME: package-init
#+begin_src elisp
(defvar bootstrap-version)
(let ((bootstrap-file
(expand-file-name
"straight/repos/straight.el/bootstrap.el"
(or (bound-and-true-p straight-base-dir)
user-emacs-directory)))
(bootstrap-version 7))
(unless (file-exists-p bootstrap-file)
(with-current-buffer
(url-retrieve-synchronously
"https://raw.githubusercontent.com/radian-software/straight.el/develop/install.el"
'silent 'inhibit-cookies)
(goto-char (point-max))
(eval-print-last-sexp)))
(load bootstrap-file nil 'nomessage))
(setq straight-vc-git-default-clone-depth 1)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
(require 'use-package)
;; (setq use-package-defaults (assq-delete-all ':straight use-package-defaults))
(setq use-package-always-ensure t)
#+end_src
#+begin_src elisp :tangle ~/.emacs.d/haris/irc.el :exports none
<<package-init>>
#+end_src
** Workarounds
In some version, calls to seq-empty-p started raising errors. This seems to
solve it.
#+begin_src elisp
(cl-defgeneric seq-empty-p (sequence)
"Return non-nil if the SEQUENCE is empty, nil otherwise."
(= 0 (seq-length sequence)))
#+end_src
Centaur tabs doesn't work properly with daemon mode. This fixes it:
#+begin_src elisp
(add-hook
'window-selection-change-functions
(defun haris//fix-centaur-tabs (&rest arg)
(setq centaur-tabs-active-bar
(or centaur-tabs-active-bar
(centaur-tabs--make-xpm 'centaur-tabs-active-bar-face
2
centaur-tabs-bar-height)))))
#+end_src
** Global packages
#+begin_src elisp
(use-package focus-autosave-mode :defer t)
(use-package multi-vterm :defer t)
(use-package command-log-mode)
(straight-use-package
'(explain-pause-mode
:type git
:host github
:repo "lastquestion/explain-pause-mode"))
(use-package fontawesome :defer t)
#+end_src
*** Language modes
#+begin_src elisp
(use-package json-mode :defer t)
(use-package counsel-jq :defer t)
(use-package fish-mode :defer t)
(use-package vimrc-mode :defer t)
(use-package sxhkdrc-mode :defer t)
(use-package i3wm-config-mode :defer t)
(use-package git-modes :defer t)
(use-package systemd :defer t)
(use-package ssh-config-mode :defer t)
(use-package crontab-mode :defer t)
(use-package lark-mode :defer t)
#+end_src
** Global settings
#+NAME: global-settings
#+begin_src elisp
;; Performance improvements
(setq native-comp-async-report-warnings-errors nil)
;; 500 MB
(setq gc-cons-threshold (* 500 1024 1024))
;; Increase savehist autosave interval to reduce lag (10 minutes)
(setq spacemacs-savehist-autosave-idle-interval 600)
(setq paradox-github-token t)
(auth-source-pass-enable)
(setq image-auto-resize 'fit-width)
(setq image-auto-resize-on-window-resize t)
(setq evil-want-keybinding nil)
(setq uniquify-buffer-name-style 'forward)
(setq frame-title-format "%b"
dotspacemacs-frame-title-format "%b")
(eval-after-load "recentf"
(defun haris//after-load/recentf ()
(add-hook 'dired-after-readin-hook
(defun haris//add-file-to-recentf ()
(recentf-add-file default-directory)))
(add-to-list 'recentf-exclude "^/tmp/haris-pipe-")
(cl-delete-if (lambda (el) (string-match "~/\\.emacs\\.d/elpa/[^/]+/develop" el))
recentf-exclude)))
(setq winum-scope 'frame-local)
(add-to-list 'image-types 'svg)
(setq warning-minimum-level :emergency)
(add-to-list 'exec-path (expand-file-name "~/.local/bin"))
(if (not (boundp 'haris/prepended-path))
(progn
(setq haris/prepended-path "yes")
(setenv "PATH" (concat (expand-file-name "~/.local/bin") ":" (getenv "PATH")))))
(add-hook 'text-mode-hook #'auto-fill-mode)
(add-hook 'prog-mode-hook #'auto-fill-mode)
;; Some packages prefer helm over ivy and don't provide a customization option.
(eval-after-load 'helm
(lambda () (fmakunbound 'helm)))
#+end_src
#+begin_src elisp :tangle ~/.emacs.d/haris/irc.el :exports none
<<global-settings>>
#+end_src
*** Editor settings
#+begin_src elisp
(setq fill-column 80)
(setq-default tab-width 4)
(spacemacs/toggle-visual-line-navigation-globally-on)
(set-language-environment "UTF-8")
(modify-syntax-entry ?_ "w")
(setq indent-guide-delay 0.1)
(setq tab-width 4
go-tab-width 4)
(setq emacs-lisp-format-on-save nil)
#+end_src
*** External tool integrations
#+begin_src elisp
(setq browse-url-generic-program (executable-find "firefox"))
(setq dotspacemacs-search-tools '("rg" "grep"))
;; Ways to spawn shells from within Emacs
(setq shell-default-shell 'shell)
(setq terminal-here-linux-terminal-command '("alacritty")
terminal-here-mac-terminal-command '("alacritty")
terminal-here-command-flag "-e")
#+end_src
*** Scrolling
#+begin_src elisp
(setq scroll-bar-mode-explicit nil)
(setq mouse-wheel-scroll-amount '(6))
;; Fix scrolling performance
(use-package fast-scroll)
(fast-scroll-config)
(fast-scroll-mode 1)
#+end_src
*** Custom miscellaneous backup file patterns
I change the following in order to prevent Emacs from complaining about
filenames being too long. The main culprit is Java, with its horrendously deep
directory structures.
#+begin_src elisp
(make-directory "~/.emacs.d/.cache/auto-save/dist" t)
(make-directory "~/.emacs.d/.cache/auto-save/site" t)
(dolist (item auto-save-file-name-transforms)
(let ((uniquify (nthcdr 2 item)))
(if uniquify (setcar uniquify 'md5))))
(defalias 'haris//undo-tree-make-history-save-file-name/default
(symbol-function
(if (fboundp 'haris//undo-tree-make-history-save-file-name/default)
'haris//undo-tree-make-history-save-file-name/default
'undo-tree-make-history-save-file-name))
"The original implementation of the undo-tree-make-history-save-file-name
function, so I can call it in my custom override.")
(defun undo-tree-make-history-save-file-name (file)
"Create the undo history file name for FILE. This overrides the default
implementation, by making the basename of the file a hashed version of the
original path. The benefit of this is that it prevents a 'filename too long
error'."
(let ((original (haris//undo-tree-make-history-save-file-name/default file)))
(concat (file-name-directory original)
(md5 (file-name-nondirectory file))
".~undo-history~")))
#+end_src
Call these to test the patterns:
#+begin_src elisp :tangle no
(make-auto-save-file-name)
#+end_src
#+begin_src elisp :tangle no
(undo-tree-make-history-save-file-name (buffer-file-name))
#+end_src
I change this in order to prevent non-emacs tools from picking up the files, and
misbehaving due to their existence.
#+begin_src elisp
(make-directory "~/.emacs.d/.cache/lock-file/dist" t)
(make-directory "~/.emacs.d/.cache/lock-file/site" t)
;; Set lock-file-name-transforms to auto-save-file-name-transforms,
;; but replace "auto-save" with "lock-file" in each replacement pattern.
(setq lock-file-name-transforms
(remove nil
(mapcar
(lambda (item)
(let ((item-copy (copy-tree item)))
(if (string-match "auto-save" "lock-file")
(setcar (cdr item-copy)
(string-replace "auto-save" "lock-file" (cadr item-copy))))
item-copy))
auto-save-file-name-transforms)))
(dolist (item lock-file-name-transforms)
(let ((uniquify (nthcdr 2 item)))
(if uniquify (setcar uniquify 'sha512))))
#+end_src
You can test the transforms by calling this:
#+begin_src elisp :tangle no
(make-lock-file-name (buffer-file-name))
#+end_src
*** Global modes
**** xclip
#+begin_src elisp
;; Enable clipboard in the terminal
(use-package xclip)
;; Using xclip instead makes emacsclient hang
(setq xclip-method 'xsel)
(setq xclip-program "xsel")
(xclip-mode)
#+end_src
**** Auto-revert mode
#+begin_src elisp
(global-auto-revert-mode t)
(setq auto-revert-verbose t
auto-revert-use-notify t
auto-revert-avoid-polling t
;; When reverting buffers whose files have been changed by emacs, it
;; seems that the interval from this variable is used, even though
;; polling was disabled above.
;; An example where this can be observed is when an org-mode code block
;; is tangled and the target file is open in another buffer.
auto-revert-interval 2.5)
#+end_src
**** Miscellaneous
#+begin_src elisp
;; Handle URLs as files
(url-handler-mode)
(centaur-tabs-mode)
#+end_src
*** Buffer cleanup
#+begin_src elisp
(setq haris/custom-temp-buffer-name "*haris-temp*")
(setq clean-buffer-list-delay-general 1
clean-buffer-list-delay-special 1800
clean-buffer-list-kill-regexps
`(
"^haris-pipe-"
"^\\*Help\\*"
"^\\*helpful "
"^\\*ivy-occur"
"^\\*lsp-help\\*"
"^\\*lsp session\\*"
"^magit: "
"^magit-\\(log\\|diff\\|stash\\|revision\\)"
"^\\*Man "
"^\\*straight-process\\*"
"\\*which-key\\*"
"^\\*xref\\*"
"\\*Customize Group:"
;; Docker command output buffers
"^\\* docker .*\\*"
;; Docker compose command output buffers
"^\\* .* docker-compose .*\\*"
,(concat "^" (regexp-quote haris/custom-temp-buffer-name))))
#+end_src
This enables periodic cleanup:
#+begin_src elisp
(when (boundp 'haris/timer/clean-buffer-list)
(cancel-timer haris/timer/clean-buffer-list)
(makunbound 'haris/timer/clean-buffer-list))
(setq haris/timer/clean-buffer-list (run-with-timer 1800 1800 #'clean-buffer-list))
#+end_src
**** Temporary pipe files
This makes sure that temporary pipe files are deleted when their buffers are killed.
#+begin_src elisp
(add-hook
'kill-buffer-hook
(defun haris//kill-buffer-delete-pipe-file ()
"Delete pipe file if buffer matches /tmp/haris-pipe-* pattern"
(when (and buffer-file-name
(string-match "^/tmp/haris-pipe-" buffer-file-name))
(delete-file buffer-file-name))))
#+end_src
*** Global bindings
#+begin_src elisp
(global-set-key (kbd "C-+") 'text-scale-adjust)
(global-set-key (kbd "C--") 'text-scale-adjust)
(global-set-key (kbd "C-0") (defun haris//text-scale-reset ()
(interactive)
(text-scale-set 0)))
#+end_src
** Functions
#+begin_src elisp
(defun haris/stage ()
"Go to a user-specific temporary staging directory, useful as a playground."
(interactive)
(let ((dir (format "/tmp/stage-%s"
(user-login-name))))
(mkdir dir t)
(dired dir)))
(defun haris/force-kill-window (&optional window)
"Kill a window, and the frame as well if it's the last one."
(interactive)
(let ((frame (window-frame window)))
(if (eq (length (window-list frame)) 1)
(delete-frame frame)
(quit-window window))))
(defun haris/yas-minor-mode-on ()
"Like yas-minor-mode-on, but do not honor yas-dont-activate-functions"
(yas-minor-mode 1))
(defun haris/fifo (text)
"Send TEXT to the fifo I use for testing"
(interactive "sInput: ")
(with-temp-buffer
(insert text "\n")
(shell-command-on-region
(point-min)
(point-max)
"fifo")))
#+end_src
** Hooks
#+begin_src elisp
(add-hook
'find-file-hook
(defun haris//auto-sudo-edit ()
(let ((original-buf (current-buffer)))
(run-with-timer
1 nil
(lambda ()
;; open as root
(when (and (not (file-writable-p buffer-file-name))
(y-or-n-p-with-timeout "File not writable. Open as root?" nil))
(spacemacs/sudo-edit)
(unless (equal (current-buffer) original-buf)
(let (kill-buffer-query-functions kill-buffer-hook)
(kill-buffer original-buf)))))))))
#+end_src
** Avy
*** Try to make evil-easymotion work bidirectionally
#+begin_src elisp
(define-key evil-normal-state-map (kbd "M-w") 'avy-goto-word-0)
(define-key evil-normal-state-map (kbd "M-f") 'avy-goto-char)
#+end_src
** Dired
#+begin_src elisp
(setq dired-listing-switches "-al \"--time-style=+%Y-%m-%d %H:%M\"")
(setq dired-kill-when-opening-new-dired-buffer t)
#+end_src
*** Additional packages
#+begin_src elisp
(use-package dired-rsync :defer t)
(use-package dired-rsync-transient :defer t)
(straight-use-package '(dired-hacks :type git :host github :repo "Fuco1/dired-hacks"))
(use-package dired-toggle-sudo :defer t)
#+end_src
** Interactive commands
These are commands that I primarily intend to use interactively and directly,
without binding them to any keys.
*** General purpose commands
#+begin_src elisp
(defun haris/vscode (file &optional new-window)
"Open a file or directory in VSCode."
(interactive (list (buffer-file-name) current-prefix-arg))
(if new-window
(start-process
"vscode" nil
"fish" "-c"
(format "o code --wait --new-window %s &"
(shell-quote-argument (expand-file-name file))))
(start-process
"vscode" nil
"code"
(expand-file-name file))))
#+end_src
*** Commands from local shell scripts
All commands defined in [[file:scripts.org][./scripts.org]] are taken and loaded as equivalent Elisp
interactive commands. Each command is mapped to a function named
=haris/script/<script-name-from-scripts.org>=. When this interactive command is
run, it opens a vterm buffer named based on the command name, and runs the
command there (without any arguments).
#+begin_src elisp
(funcall
(defun haris/load-commands-from-local-shell-scripts ()
"Load all local shell script commands as interactive Elisp commands."
(interactive)
(with-temp-buffer
(org-mode)
(setq-local org-use-tag-inheritance nil)
(insert-file-contents "~/.haris/scripts.org")
;; Extract all applicable script commands
(setq-local
_commands
(org-map-entries
(lambda () (let ((title (nth 4 (org-heading-components))))
(string-replace "=" "" title)))
"script" nil))
;; Create an interactive function definition for each command
(mapcar
(lambda (command)
(eval
`(defun ,(intern (format "haris/script/%s" command))
;; Arglist
(prefix-arg)
;; Docstring
,(format
"Interactive command corresponding to the custom local shell script '%s'"
command)
(interactive "P")
(let ((default-directory "~")
(command ,command)
(_vterm nil)
(run-command nil))
;; Run multi-vterm
(setq _vterm (multi-vterm))
(with-current-buffer _vterm
;; Rename the buffer based on the command name
(rename-buffer (format "*haris/script/%s*" command) t)
(setq
run-command
(eval `(lambda (&optional argstring)
(interactive ,(format "sCLI arguments: %s " ,command))
"Run the command inside the open vterm buffer"
(comint-send-string
(get-buffer-process ,_vterm)
(format "%s %s\n" ,command (or argstring ""))))))
;; Run the command
(if prefix-arg
;; With prefix arg - prompt for CLI arguments before running
(call-interactively run-command)
;; No prefix arg - run without CLI arguments
(run-with-timer 0.6 nil (eval `(lambda () (funcall ,run-command))))))))))
_commands))))
#+end_src
** Bindings
#+begin_src elisp
(defun haris/insert-tab ()
(interactive)
(insert-tab))
(defun haris/describe-symbol-at-point ()
(interactive)
(let ((was-in-minibuffer (minibufferp))
(original-buffer (current-buffer)))
(helpful-symbol (helpful--symbol-at-point))
(when was-in-minibuffer (switch-to-buffer original-buffer))))
;; M-TAB in insert mode inserts a tab emulated by spaces
(define-key evil-insert-state-map (kbd "M-TAB") #'insert-tab)
;; "SPC +" will pop up eshell
(spacemacs/set-leader-keys "+" 'spacemacs/shell-pop-eshell)
;; Don't use it, plus it interferes with bindings such as forward-button
(eval-after-load "helpful"
(lambda ()
(define-key evil-normal-state-map (kbd "TAB") nil)))
;; Help bindings
(spacemacs/set-leader-keys "hdo" 'helpful-symbol)
(evil-define-key 'normal org-mode-map (kbd "C-q")
'haris/describe-symbol-at-point)
(evil-define-key 'normal emacs-lisp-mode-map (kbd "C-q")
'haris/describe-symbol-at-point)
(evil-define-key 'normal ielm-map (kbd "C-q")
'haris/describe-symbol-at-point)
(evil-define-key 'normal read--expression-map (kbd "C-q")
'haris/describe-symbol-at-point)
(evil-define-key 'normal helpful-mode-map (kbd "TAB") #'forward-button)
#+end_src
#+begin_src elisp
(spacemacs/declare-prefix "o" "custom")
#+end_src
*** Launching other programs at current context
Note: there is also spacemacs' builtin =SPC "= that opens a terminal in-place.
#+begin_src elisp
(global-set-key (kbd "M-e")
(defun haris/open-buffer-in-new-frame ()
(interactive)
(let ((buf (current-buffer)))
(select-frame (make-frame '((window-system . x))))
(switch-to-buffer buf))))
(global-set-key
(kbd "M-v")
(lambda () (interactive)
(start-process "" nil "gvim" (buffer-file-name (window-buffer)))))
#+end_src
*** Spacemacs-like bindings
#+NAME: spacemacs-like-bindings
#+begin_src elisp
(defun haris/open-emacs.org ()
(interactive)
(find-file "~/.haris/emacs.org"))
(defun haris/load-user-config ()
(interactive)
(load-file "~/.emacs.d/haris/root.el"))
(defun haris/open-dotfiles-git ()
(interactive)
(magit-status "~/.haris"))
(define-key evil-normal-state-map (kbd "SPC f e h") #'haris/open-emacs.org)
(define-key evil-normal-state-map (kbd "SPC f e H") #'haris/open-dotfiles-git)
(define-key evil-normal-state-map (kbd "SPC f e r") #'haris/load-user-config)
(defalias 'spacemacs/default-pop-shell 'spacemacs/shell-pop-multivterm)
#+end_src
#+begin_src elisp :tangle ~/.emacs.d/haris/irc.el :exports none
<<spacemacs-like-bindings>>
#+end_src
*** Consistent vim-like bindings
There are some inconsistencies in the vim key bindings (vim is guilty of this as
well). For example =D= deletes until end of line, but =V= visually selects the whole
line. This section remaps =V= to =v$= and does the same for other similar cases.
Some custom keybindings are defined here as well.
#+NAME: consistent-vim-bindings
#+begin_src elisp
(define-key evil-normal-state-map (kbd "Q") 'delete-window)
(define-key evil-motion-state-map (kbd "Q") 'delete-window)
(define-key evil-visual-state-map (kbd "v") 'evil-visual-line)
(define-key evil-normal-state-map (kbd "V") (kbd "v$"))
(setq evil-want-Y-yank-to-eol t)
(define-key evil-normal-state-map (kbd "C-a") 'evil-numbers/inc-at-pt)
(define-key evil-visual-state-map (kbd "C-a") 'evil-numbers/inc-at-pt)
(define-key evil-normal-state-map (kbd "C-x") 'evil-numbers/dec-at-pt)
(define-key evil-visual-state-map (kbd "C-x") 'evil-numbers/dec-at-pt)
(defun haris/nohighlight () (interactive) (evil-ex-call-command "" "noh" ""))
(define-key evil-normal-state-map (kbd "M-/") 'haris/nohighlight)
(define-key evil-motion-state-map (kbd "M-/") 'haris/nohighlight)
(setq dotspacemacs-distinguish-gui-tab t)
#+end_src
#+begin_src elisp :tangle ~/.emacs.d/haris/irc.el :exports none
<<consistent-vim-bindings>>
#+end_src
*** Ielm
#+NAME: ielm
#+begin_src elisp
(setq ielm-dynamic-return nil)
;; Use RET to execute command even in normal mode
(evil-define-key 'normal ielm-map (kbd "RET") #'ielm-send-input)
;; Make RET in insert mode insert newline at point, unless the
;; point is at the end of the line, in which case send input.
(defun haris/ielm-insert-mode-return ()
"Insert newline at point"
(interactive)
(if (= (point)
(save-excursion
(end-of-visual-line)
(point)))
(ielm-send-input)
(ielm-return)))
(evil-define-key 'insert ielm-map (kbd "RET") #'haris/ielm-insert-mode-return)
#+end_src
#+begin_src elisp :tangle ~/.emacs.d/haris/irc.el :exports none
<<ielm>>
#+end_src
*** Custom global map
#+NAME: custom-global-map
#+begin_src elisp
;; Buffer map
(setq haris/buffer-prefix-map (make-sparse-keymap))
(spacemacs/set-leader-keys "ob" haris/buffer-prefix-map)
(define-key haris/buffer-prefix-map (kbd "r") #'rename-buffer)
(define-key haris/buffer-prefix-map (kbd "R") #'revert-buffer)
(define-key haris/buffer-prefix-map (kbd "c") #'clone-buffer)
(define-key haris/buffer-prefix-map (kbd "i") #'ibuffer)
;; Command log mode
(setq haris/command-log-prefix-map (make-sparse-keymap))
(spacemacs/set-leader-keys "oc" haris/command-log-prefix-map)
(define-key haris/command-log-prefix-map (kbd "l") #'haris/command-log)
;; Misc
(eval-after-load 'npm-mode
(lambda () (spacemacs/set-leader-keys "on" npm-mode-command-keymap)))
;; Git
(setq haris/git-prefix-map (make-sparse-keymap))
(eval-after-load 'magit
(lambda () (spacemacs/set-leader-keys "og" haris/git-prefix-map)))
(define-key haris/git-prefix-map (kbd "c") 'magit-find-git-config-file)
;; Misc
(spacemacs/set-leader-keys "*" 'gptel-menu)
;; Friendly descriptions
(which-key-add-key-based-replacements
"SPC o b" "Buffer manipulation"
"SPC o c" "Command log"
"SPC o c l" "Local command log"
"SPC o t" "Translate"
"SPC o n" "NPM"
"SPC o g" "Git")
#+end_src
#+begin_src elisp :tangle ~/.emacs.d/haris/irc.el
<<custom-global-map>>
#+end_src
*** Tabs
#+begin_src elisp
(define-key evil-motion-state-map (kbd "g M-l") 'centaur-tabs-move-current-tab-to-right)
(define-key evil-motion-state-map (kbd "g M-h") 'centaur-tabs-move-current-tab-to-left)
#+end_src
*** Miscellaneous
#+NAME: bindings-miscellaneous
#+begin_src elisp
;; Use M-y or M-n to answer a minibuffer prompt
(defun haris/insert-into-minibuffer-and-exit (text)
(interactive)
(with-current-buffer (window-buffer (active-minibuffer-window))
(insert text)
(exit-minibuffer)))
(global-set-key (kbd "M-y")
(lambda ()
(interactive)
(haris/insert-into-minibuffer-and-exit "y")))
(global-set-key (kbd "M-n")
(lambda ()
(interactive)
(haris/insert-into-minibuffer-and-exit "n")))
(define-key comint-mode-map (kbd "M-h") (lambda ()
"Search through current history"
(interactive)
(counsel-shell-history)))
#+end_src
#+begin_src elisp :tangle ~/.emacs.d/haris/irc.el :exports none
<<bindings-miscellaneous>>
#+end_src
** Evil
#+begin_src elisp
(use-package evil-quickscope)
(global-evil-quickscope-mode)
(setq evil-lookup-func (lambda () (call-interactively #'man)))
(setq evil-want-C-i-jump t)
(add-hook 'evil-insert-state-exit-hook #'company-cancel)
#+end_src
*** evil-collection
#+begin_src elisp
(setq evil-collection-setup-minibuffer t)
;; Please keep this sorted
(evil-collection-init 'bluetooth)
(evil-collection-init 'bookmark)
(evil-collection-init 'calendar)
(evil-collection-init 'comint)
(evil-collection-init 'daemons)
(evil-collection-init 'docker)
(evil-collection-init 'doc-view)
(evil-collection-init 'edbi)
(evil-collection-init 'edebug)
(evil-collection-init 'explain-pause-mode)
(evil-collection-init 'git-timemachine)
(evil-collection-init 'ibuffer)
(evil-collection-init 'info)
(evil-collection-init 'ivy)
(evil-collection-init 'man)
(evil-collection-init 'minibuffer)
(evil-collection-init 'proced)
(evil-collection-init 'tablist)
(evil-collection-init 'tabulated-list)
(evil-collection-init 'yaml-mode)
(evil-collection-init 'compilation)
(evil-collection-init 'evil-mc)
#+end_src
*** evil-surround
#+begin_src elisp
(eval-after-load 'evil-surround
(lambda () (add-to-list 'evil-surround-pairs-alist '(?$ . ("\"$(" . ")\"")))))
#+end_src
*** Functions
#+begin_src elisp
(defun haris/evil-define-key-both (keymap key def &rest bindings)
"evil-define-key in both normal and insert state"
;; Forward to evil-define-key, handling &rest args properly
(apply 'evil-define-key* (append `(normal ,keymap ,key ,def) bindings))
(apply 'evil-define-key* (append `(insert ,keymap ,key ,def) bindings)))
#+end_src
** Projectile
#+begin_src elisp
(setq projectile-require-project-root nil)
(setq projectile-auto-discover nil)
(setq projectile-track-known-projects-automatically nil)
(setq projectile-git-ignored-command "git ls-files -zcoi -X=.gitignore")
(setq projectile-ignored-projects '("~/"))
(setq projectile-indexing-method 'hybrid
projectile-enable-caching t)
(setq projectile-ignored-project-function
(defun haris//projectile-ignored-project-function (project-root)
(string-match-p "^/usr/" project-root)
(string-match-p "\.pyenv/" project-root)
(string-match-p "\.local/" project-root)))
(defun haris/projectile-project-root-function (dir)
"Find project root by checking for projectile-known-projects or .projectile
file first, then fall back to .git directory."
(or
;; First: dominating directory in projectile-known-projects or with .projectile
(locate-dominating-file
dir
(lambda (d)
(or (member d projectile-known-projects)
(file-exists-p (expand-file-name ".projectile" d)))))
;; Second: dominating directory with .git
(locate-dominating-file dir ".git")))
(setq projectile-project-root-functions
'(haris/projectile-project-root-function))
#+end_src
*** Default known projects
#+begin_src elisp
(with-eval-after-load 'magit
(with-eval-after-load 'projectile
(projectile-load-known-projects)
(let*
((projects (-filter
'file-directory-p
(append
(remove
"~/proj/contrib/"
(file-expand-wildcards "~/proj/*/"))
(file-expand-wildcards "~/proj/contrib/*/")
'("~/.haris/"
"~/.emacs.d/"
"~/.emacs.d/private/snippets/"
"~/.emacs.d/private/themes/dracula/"
"~/wiki/"))))
(worktrees
(flatten-list
(mapcar
(lambda (proj)
(append
;; Add all worktrees of the project's repo as projects
(let ((default-directory proj))
(mapcar 'car (magit-list-worktrees))
;; In case it is not a git repo, worktrees would be nil, so add the
;; project itself
(list proj))))
projects))))
;; Add each project (+ its worktrees) to projectile-known-projects
(dolist (worktree worktrees)
(add-to-list 'projectile-known-projects worktree nil 'file-equal-p)
;; Also create a .projectile file in case the directory exists and it is not
;; a git repo
(when (and (file-exists-p worktree) (not (magit-git-repo-p worktree)))
(f-touch (f-join worktree ".projectile")))))))
#+end_src
*** Functions
#+begin_src elisp
(defun haris/projectile-initialize-project (directory)
(interactive (list default-directory))
(f-touch (f-join directory ".projectile")))
(defun haris/projectile-open-in-vscode ()
(interactive)
(haris/vscode (projectile-project-root) t))
(defun haris/projectile-open-opencode ()
(interactive)
(let ((default-directory (projectile-project-root)))
(terminal-here '("fish" "-C" "opencode"))))
#+end_src
*** Keybindings
#+begin_src elisp
;; Provides better namespacing than the default spacemacs/projectile-shell-pop
(define-key spacemacs-cmds (kbd "p '") 'multi-vterm-project)
(define-key spacemacs-cmds (kbd "p .") 'haris/projectile-initialize-project)
(define-key spacemacs-cmds (kbd "p s") 'projectile-save-project-buffers)
(define-key spacemacs-cmds (kbd "p V") 'haris/projectile-open-in-vscode)
(define-key spacemacs-cmds (kbd "p O") 'haris/projectile-open-opencode)
#+end_src
* Programming languages
#+begin_src elisp
(add-hook 'prog-mode-hook 'spacemacs/toggle-display-fill-column-indicator-on)
#+end_src
** YAML
#+begin_src elisp
(with-eval-after-load 'yaml
(use-package yaml-pro :ensure t))
;; Use yaml-ts-mode instead of yaml-mode (it's faster), unless the mode is docker-compose-mode.
(add-hook 'yaml-mode-hook
(defun haris/replace-yaml-with-yaml-ts ()
(when (not (eq major-mode 'docker-compose-mode))
(yaml-ts-mode))))
(let ((hooks '(yaml-mode-hook yaml-ts-mode-hook)))
(dolist (hook hooks)
(add-hook hook 'spacemacs/toggle-indent-guide)
(add-hook hook #'eldoc-mode 90)
(add-hook hook 'spacemacs/toggle-display-fill-column-indicator-on)
(add-hook hook
(lambda ()
(setq-local counsel-jq-command "yq")) 92)
(add-hook hook #'lsp 94)))
(with-eval-after-load 'yaml-pro
(add-hook 'yaml-mode-hook 'yaml-pro-ts-mode 91)
(add-hook 'yaml-ts-mode-hook 'yaml-pro-ts-mode 91))
(with-eval-after-load 'yaml-pro
(add-hook 'yaml-pro-ts-mode-hook
(lambda ()
(setq-local lsp-enable-imenu nil))))
#+end_src
*** Keybindings
#+begin_src elisp
(eval-after-load 'yaml-pro
(lambda ()
(evil-define-key 'normal yaml-mode-map (kbd "SPC j =") 'lsp-format-buffer)
(evil-define-key 'normal yaml-pro-ts-mode-map (kbd "M-n") 'yaml-pro-ts-next-subtree)
(evil-define-key 'normal yaml-pro-ts-mode-map (kbd "g j") 'yaml-pro-ts-next-subtree)
(define-key yaml-pro-ts-mode-map (kbd "C-c C-n") nil)
(evil-define-key 'normal yaml-pro-ts-mode-map (kbd "M-p") 'yaml-pro-ts-prev-subtree)
(evil-define-key 'normal yaml-pro-ts-mode-map (kbd "g k") 'yaml-pro-ts-prev-subtree)
(define-key yaml-pro-ts-mode-map (kbd "C-c C-p") nil)
(evil-define-key 'normal yaml-pro-ts-mode-map (kbd "g h") 'yaml-pro-ts-up-level)
(define-key yaml-pro-ts-mode-map (kbd "C-c C-u") nil)
(evil-define-key 'normal yaml-pro-ts-mode-map (kbd ", '") 'yaml-pro-edit-ts-scalar)
(define-key yaml-pro-ts-mode-map (kbd "C-c '") nil)
(evil-define-key 'normal yaml-pro-ts-mode-map (kbd ", <") 'yaml-pro-ts-unindent-subtree)
(define-key yaml-pro-ts-mode-map (kbd "C-c <") nil)
(evil-define-key 'normal yaml-pro-ts-mode-map (kbd ", >") 'yaml-pro-ts-indent-subtree)
(define-key yaml-pro-ts-mode-map (kbd "C-c >") nil)
(evil-define-key 'normal yaml-pro-ts-mode-map (kbd ", v") 'yaml-pro-ts-mark-subtree)
(define-key yaml-pro-ts-mode-map (kbd "C-c @") nil)
(evil-define-key 'normal yaml-pro-ts-mode-map (kbd "M-j") 'yaml-pro-ts-move-subtree-down)
(define-key yaml-pro-ts-mode-map (kbd "s-<down>") nil)
(evil-define-key 'normal yaml-pro-ts-mode-map (kbd "M-k") 'yaml-pro-ts-move-subtree-up)
(define-key yaml-pro-ts-mode-map (kbd "s-<up>") nil)
(evil-define-key 'normal yaml-pro-ts-mode-map (kbd ", d") 'yaml-pro-kill-subtree)
(define-key yaml-pro-ts-mode-map (kbd "C-c C-x C-w") nil)
(evil-define-key 'normal yaml-pro-ts-mode-map (kbd ", p") 'yaml-pro-ts-paste-subtree)
(define-key yaml-pro-ts-mode-map (kbd "C-c C-x C-y") nil)))
#+end_src
*** Dependencies :deps:
#+begin_src text :tangle (haris/tangle-deps "emacs/yaml.pacman")
yaml-language-server yamllint
#+end_src
** JSON
#+begin_src elisp
(defun haris/json/set-indent-level () (setq-local js-indent-level 2))
(add-hook 'json-mode-hook 'haris/json/set-indent-level)
(add-hook 'json-mode-hook 'spacemacs/toggle-indent-guide)
(add-hook 'json-mode-hook 'json-ts-mode 90)
(defalias 'jq 'counsel-jq)
(setq counsel-jq-json-buffer-mode 'json-ts-mode)
#+end_src
** LSP
Some performance improvement settings depend on the power of your hardware. You
can find them [[https://emacs-lsp.github.io/lsp-mode/page/performance/][here]]. You can place them in your [[*Private config][private configuration]].
#+begin_src elisp
(with-eval-after-load 'lsp-mode
(setq lsp-idle-delay 0.1)
(setq lsp-keep-workspace-alive nil)
;; Diagnostic mode doesn't work well with flycheck
(setq lsp-diagnostics-disabled-modes '(python-mode sh-mode))
(setq lsp-enable-on-type-formatting nil)