-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.el
executable file
·1674 lines (1402 loc) · 61.9 KB
/
init.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
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
;;; init.el --- Yong Jie's (very messy) Emacs init file
;;; Commentary:
;; To-do's:
;; - Learn about difference between use-package and plain require.
;; - Decide on how to modularize the init file(s):
;; - Whether to use separate files, or one single file (leaning towards
;; latter).
;; - How to split into different sections (e.g., some packages like
;; company-mode naturally affects different programming modes, so
;; should I have a section for company-mode, or should those
;; settings be subsumed under the settings for the programming
;; languages modes).
;; - Standardize config files across devices.
;; - Add proper comments.
;; - Standardize use of # symbol.
;; - https://www.gnu.org/software/emacs/manual/html_node/elisp/Index.html
;; - http://endlessparentheses.com/get-in-the-habit-of-using-sharp-quote.html
;; - Learn how folding works in Emacs, develop a workflow.
;;
;; - Make electric-pair work with backticks:
;; - https://emacs.stackexchange.com/questions/2538/how-to-define-additional-mode-specific-pairs-for-electric-pair-mode
;;;-----------------------------------------------------------------------------
;;; Per-Machine Settings
;;;-----------------------------------------------------------------------------
;; Set yj-org-directory to the base directory for org-mode. E.g.,
;; "/home/yongjie/syncthing/org".
(cond
((eq system-type 'darwin)
(defvar yj-org-directory "~/syncthing/org"))
((eq system-type 'windows-nt)
(defvar yj-org-directory "c:/syncthing/org"))
((or (eq system-type 'gnu) (eq system-type 'gun/linux) (eq system-type 'cygwin))
(defvar yj-org-directory "/home/yongjie/syncthing/org"))
(t (defvar yj-org-directory "/home/yongjie/syncthing/org")))
(make-directory yj-org-directory t)
(setq default-directory yj-org-directory)
;;;-----------------------------------------------------------------------------
;;; My Custom Functions
;;;-----------------------------------------------------------------------------
(defun yj/file-location-at-point ()
"Return full file path with line number at point"
(interactive)
(let ((line-number (substring (what-line) 5 nil)))
(kill-new (concat (buffer-file-name) "::" line-number))))
;;; For shrinking infomation-displaying (as opposed to text-editing) windows
;;; containing buffers like *Help* and *xref*, which shouldn't always take up
;;; half the frame.
(defvar yj/info-window-buffer-name '("*Help*" "*xref*" "*company-documentation*" "*Occur*")
"List of buffer names (string) representing informational buffers.
Such informational buffers might contain help content, documentation,
references, and may contain only a few lines of text.")
(defun yj/shrink-info-window (&optional window)
"Shrinks WINDOW to fit buffer size if it is a info-window.
Info-window is defined in the list `yj/info-window-buffer-name'."
(if window (let* ((b (window-buffer window))
(bname (buffer-name b)))
(if (member bname yj/info-window-buffer-name)
(shrink-window-if-larger-than-buffer window)))
(shrink-window-if-larger-than-buffer (selected-window))))
(defun yj/shrink-info-windows (&rest _)
(mapc 'yj/shrink-info-window (window-list)))
(defvar yj/info-window-creating-fns
(list 'describe-variable 'company-show-doc-buffer 'lsp-show-xrefs 'occur)
"List of symbols represent functions that creates infomational windows.")
(mapc (lambda (fn) (advice-add fn :after 'yj/shrink-info-windows))
yj/info-window-creating-fns)
;; yj/find-file-at-point improves on projectile-find-file-dwim by handling
;; filenames with row and column indication like "main.go:69:420". To use this
;; defun, we'll also need to edit projectile-select-files in projectile.el to
;; look something like the following:
;;
;;(defun projectile-select-files (project-files &optional invalidate-cache)
;; "Select a list of files based on filename at point.
;;
;;With a prefix arg INVALIDATE-CACHE invalidates the cache first."
;; (projectile-maybe-invalidate-cache invalidate-cache)
;; (let* ((file (if (region-active-p)
;; (buffer-substring (region-beginning) (region-end))
;; (or (thing-at-point 'filename) "")))
;; (file (if (string-match "\\.?\\./" file)
;; (file-relative-name (file-truename file) (projectile-project-root))
;; file))
;; (file (if (string-match "\\([^:]+\\)\\(:[0-9]\\)+" file) ; Added to remove things like the trailing ":420:69" in "main.go:420:69"
;; (match-string 1 file) ;
;; file)) ;
;; (file (if (string-match "\\./\\(.*\\)" file)
;; (match-string 1 file)
;; file))
;; (files (if file
;; (cl-remove-if-not
;; (lambda (project-file)
;; (string-match file project-file))
;; project-files)
;; nil)))
;; files)
(defun yj/find-file-at-point ()
(interactive)
(let* ((filename-at-point
(string-remove-prefix "file://" (or (thing-at-point 'filename) "")))
(is-absolute (string-match "^/" filename-at-point))
(without-line-col-suffix
(substring filename-at-point 0 (string-match ":" filename-at-point)))
(line-no (if (string-match "\\w:\\([0-9]+\\)" filename-at-point)
(match-string 1 filename-at-point)))
(col-no (if (string-match "\\w:[0-9]+:\\([0-9]+\\)" filename-at-point)
(match-string 1 filename-at-point))))
(if is-absolute
(find-file without-line-col-suffix)
(projectile-find-file-dwim))
(if line-no
(forward-line (- (string-to-number line-no) (line-number-at-pos))))
(if col-no
(move-to-column (string-to-number col-no)))))
;;;-----------------------------------------------------------------------------
;;; Emacs GUI-related
;;;-----------------------------------------------------------------------------
(setq inhibit-startup-message t)
(if (display-graphic-p)
(tool-bar-mode -1))
(menu-bar-mode -1)
(add-to-list 'default-frame-alist
'(vertical-scroll-bars . nil)
'(undecorated . t)) ;; Hide title bar
(pixel-scroll-precision-mode t)
;;;-----------------------------------------------------------------------------
;;; Package Management
;;;-----------------------------------------------------------------------------
;; Initialize MELPA
(require 'package)
(setq package-enable-at-startup nil)
(add-to-list 'package-archives
'("melpa" . "https://melpa.org/packages/"))
(package-initialize)
(unless (package-installed-p 'use-package)
(package-refresh-contents)
(package-install 'use-package))
;; delight allows us to hide major and minor modes from the mode-line. For
;; built-in modes, the configuration will be located here; whereas for installed
;; modes, they will be configured in the respective use-package section.
(use-package delight
:ensure t
:config
(delight 'whitespace-mode nil "whitespace")
(delight 'subword-mode nil "subword"))
;; exec-path-from-shell makes Emacs use the $PATH environment variable as set up
;; by the user's shell, so that it can find the necessary binaries (e.g.,
;; language servers) more easily.
(use-package exec-path-from-shell
:ensure t
:init
(setq exec-path-from-shell-variables
'("PATH" "MANPATH"
"GONOPROXY" "GONOSUMDB" "GOPRIVATE" "GOPATH"))
:config
(when (memq window-system '(mac ns x))
(exec-path-from-shell-initialize)))
;;;-----------------------------------------------------------------------------
;;; Vanilla Emacs Settings
;;;-----------------------------------------------------------------------------
;;; Some sensible defaults
;; Show the same line wrapping indicators when using visual-line-mode. Note:
;; visual-line-mode does word-level (as opposed to character-level) wrapping at
;; window boundaries.
(setq visual-line-fringe-indicators '(left-curly-arrow right-curly-arrow))
;; Enable repeating popping of mark using "C-SPC".
(setq set-mark-command-repeat-pop t)
;; Call push-mark before moving large distances so we return to the postion by
;; popping the mark.
(advice-add 'beginning-of-defun :before 'push-mark)
(advice-add 'end-of-defun :before 'push-mark)
;; Show total number of occurence when using isearch
(setq isearch-lazy-count t)
;; Wrap at 80 charactors by default when using "M-q"
(setq-default fill-column 80)
;; Recent files
(setq recentf-max-menu-items 25)
(setq recentf-max-saved-items 25)
(recentf-mode 1)
;; Automatically save to the recent file list (in addition to the default
;; behavior of saving on closing Emacs). Also using a lambda to save silently
;; without printing a message to the minibuffer, based on stackechange answer:
;; https://emacs.stackexchange.com/a/45700/23895
(run-at-time nil (* 5 60) (lambda ()
(let ((save-silently t)
(inhibit-message t))
(recentf-save-list))))
;; don't print message when auto-saving
(setq-default auto-save-no-message t)
;; remember cursor position, for emacs 25.1 or later
(save-place-mode 1)
;; scroll line by line, instead of jumping half page at a time
;; from https://www.emacswiki.org/emacs/SmoothScrolling
(setq mouse-wheel-scroll-amount '(1 ((shift) . 1))) ;; one line at a time
(setq mouse-wheel-progressive-speed nil) ;; don't accelerate scrolling
(setq mouse-wheel-follow-mouse 't) ;; scroll window under mouse
(setq scroll-step 1) ;; keyboard scroll one line at a time
(setq scroll-step 1
scroll-conservatively 10000)
;; Increase font size by default, so there is no need to increase
;; everytime opening something; Also this is needed for company-mode's
;; overlay to behave nicely. Also needed to decrease the default frame size
;; because the font is now bigger.
;;
;; See GitHub issue:
;; https://github.com/company-mode/company-mode/issues/299
;(add-to-list 'default-frame-alist
; '(font . "-outline-Courier New-normal-normal-normal-mono-23-*-*-*-c-*-iso8859-1"))
(add-to-list 'default-frame-alist '(font . "Iosevka-21:weight=light"))
(add-to-list 'default-frame-alist '(height . 17))
(add-to-list 'default-frame-alist '(width . 60))
;; (set-frame-font "Iosevka-21:weight=light" nil t)
(defun yj/change-font-size (p)
"Change font size for all buffers, windows and frames, based on prefix P."
(interactive "p")
(let* ((current-font-size 20)
(new-font-size (+ current-font-size p)))
(set-frame-font (concat "Iosevka-" (number-to-string new-font-size) ":weight=light") nil t)))
(setq initial-scratch-message nil)
(setq tab-stop-list (number-sequence 4 120 4))
(setq-default tab-width 4)
(setq-default indent-tabs-mode nil)
(setq visible-bell 1)
(setq save-interprogram-paste-before-kill t)
(setq completion-ignore-case t)
(setq-default dired-listing-switches "-alhF")
(add-hook 'dired-mode-hook 'dired-hide-details-mode) ;; Hide details by default, redisplay using "("
;; Back-up files
(defvar --backup-directory (concat user-emacs-directory "backups"))
(if (not (file-exists-p --backup-directory))
(make-directory --backup-directory t))
(setq backup-directory-alist `(("." . ,--backup-directory)))
(setq make-backup-files t ; backup of a file the first time it is saved
backup-by-copying t ; don't clobber symlinks
version-control t ; version numbers of backup files
delete-old-versions t ; delete excess backup files silently
delete-by-moving-to-trash t
kept-old-versions 6 ; oldest versions to keep when a
; new numbered backup is made
; (default: 2)
kept-new-versions 9 ; newest versions to keep when a
; new numbered backup is made
; (default: 2)
auto-save-default t ; auto-save every buffer that visits a file
auto-save-timeout 20 ; number of seconds idle time before auto-save (default: 30)
auto-save-interval 200 ; number of keystrokes between auto-saves (default 300)
)
;; Auto-save files
(defvar --auto-save-directory
(expand-file-name (concat user-emacs-directory "auto-saves/")))
(if (not (file-exists-p --auto-save-directory))
(make-directory --auto-save-directory t))
;;https://stackoverflow.com/a/15303598/5821101
(add-to-list
'auto-save-file-name-transforms
;; Note: We cannot use 'expand-file-name below to construct the file because on
;; windows, the double backslashes will be converted to the root of the
;; drive. But for this, using 'expand-file-name might have been the "more
;; correct" way.
(list "\\(.+/\\)*\\(.*?\\)" (concat --auto-save-directory "\\2"))
t)
;; Display column number in mode line.
(column-number-mode 1)
;; Make sure certain buffers are always created with a bottom split, and shrank
;; as necessary.
(customize-set-variable
'display-buffer-alist
'(("\\*Gofmt Errors\\*\\|\\*xref\\*"
(display-buffer-below-selected display-buffer-at-bottom)
(inhibit-same-window . t)
(window-height . shrink-window-if-larger-than-buffer))))
;;; Keybindings
;; Text editing.
(global-set-key "\M-Z" 'zap-up-to-char)
(global-set-key (kbd "C-S-s") 'isearch-forward-symbol-at-point)
;; Easier windows navigation and manipulation.
(defun yj/num-side-windows ()
"Return the number of side windows in the selected frame."
(seq-count (lambda (w) (window-parameter w 'window-side)) (window-list)))
(defun yj/side-window-p (w)
"Return non-nil if W is a side window."
(window-parameter w 'window-side))
(defun yj/delete-other-windows-dwim ()
"Delete other windows based on what windows are open and which is focused.
Case 0 (only 1 window) - calls 'writeroom--enable' if the function is bound.
Case 1 (only 1 window or no side windows) - passthrough to
'delete-other-windows'
Case 2 (only 1 non-side window) - passthrough
to'window-toggle-side-windows'
Default case (has at least 1 side window and 2 non-side window) -
close other non-side window such that only one is left; next call
to this function will result in Case 2"
(interactive)
(let* ((num-windows (length (window-list)))
(num-side-windows (yj/num-side-windows))
(num-main-windows (- num-windows num-side-windows)))
(cond
((= num-windows 1)
(when (fboundp 'writeroom-mode)
(writeroom-mode 'toggle)))
((zerop num-side-windows)
(delete-other-windows))
((= num-main-windows 1)
(window-toggle-side-windows))
(t
(progn
(when (yj/side-window-p (selected-window)) (select-window (get-lru-window)))
(window-toggle-side-windows)
(delete-other-windows)
(window-toggle-side-windows)))))
(yj/window-divider-mode))
(defun yj/window-buffer-names ()
"Return a list of strings of names of buffers in the displayed windows."
(let ((bufs (mapcar 'window-buffer (window-list))))
(mapcar 'buffer-name bufs)))
(defun yj/delete-window-or-kill-buffer ()
"Delete the selected window or kill the buffer, depending on window state.
If there is only one non-side window, calls 'kill-buffer',
otherwise calls 'delete-window'."
(interactive)
(let ((num-non-side-window (- (length (window-list)) (yj/num-side-windows))))
(if (= num-non-side-window 1)
(kill-buffer)
(delete-window)))
(yj/window-divider-mode))
(defvar yj/non-editing-buffer-regexp '("magit" "*Minibuf-" "*eshell*" "*vterm*" "*Treemacs" " *Org todo*" "*Ibuffer*"))
(defun yj/is-editing-buffer (buffer)
"Return non-nil if BUFFER is an editing buffer."
(not (seq-some (lambda (regexp) (string-match regexp (buffer-name buffer)))
yj/non-editing-buffer-regexp)))
(defun yj/other-editing-buffer ()
"Return the most recently used \"editing\" buffer.
This means that buffers like magit will be excluded."
(let ((order-buffer-list (cdr (buffer-list (selected-frame)))))
(seq-find 'yj/is-editing-buffer order-buffer-list (other-buffer))))
(defun yj/other-org-buffer ()
"Return the most recently used `org-mode' buffer.
This means that buffers like magit will be excluded."
(let ((order-buffer-list (cdr (buffer-list (selected-frame)))))
(seq-find
(lambda (buffer) (string-match "\.org$" (buffer-name buffer)))
order-buffer-list
(other-buffer))))
(defun yj/switch-to-previous-editing-buffer (prefix)
(interactive "P")
(if prefix
(switch-to-buffer (yj/other-org-buffer))
(switch-to-buffer (yj/other-editing-buffer))))
;; We need to unbound "C-[" from ESC first, before we can bind it to anything
;; else. From https://emacs.stackexchange.com/a/10273/23895
(define-key input-decode-map [?\C-\[] (kbd "<C-[>"))
(global-set-key (kbd "<C-[>") 'magit)
;; Bind same key in magit to toggle back to an "editing" buffer
;; TODO: Bind C-` C-1 C-2 ... C-0 C-[ C-]
(defun yj/toggle-buffer ()
(interactive)
(switch-to-buffer (other-buffer)))
(defun yj/other-window-dwim ()
(interactive)
(if (= (length (window-list)) 1)
(switch-to-buffer-other-window nil t)
(other-window 1))
(yj/window-divider-mode))
(defun yj/other-window-reversed ()
(interactive)
(if (= (length (window-list)) 1)
(progn
(split-window-sensibly)
(switch-to-buffer (other-buffer)))
(other-window -1))
(yj/window-divider-mode))
;; Adapted from reddit answer:
;; https://www.reddit.com/r/emacs/comments/gtfxg4/zoommonocle_a_buffer/fsbe7da?utm_source=share&utm_medium=web2x&context=3
(defun yj/toggle-maximize-buffer ()
"Maximize current buffer"
(interactive)
(let ((cb (current-buffer)))
;; Keep a reference to current buffer in case the previous minimize was
;; trigger while in another buffer
(if (one-window-p)
(let ((ws (window-start))
(wp (window-point)))
;; Keep a reference to the position of the window in the buffer, and
;; the point in the window, so we can restore the position (and
;; avoiding going back to the state when the minimize was called).
(jump-to-register '_)
(switch-to-buffer cb)
(set-window-start (selected-window) ws)
(set-window-point (selected-window) wp))
(window-configuration-to-register '_)
(delete-other-windows)))
(yj/window-divider-mode))
(defun yj/window-divider-mode ()
"Enable and disable window-divider-mode sensibly"
(interactive)
(let ((num-windows (length (window-list))))
(if (> num-windows 1)
(window-divider-mode 1)
(window-divider-mode -1))))
(defun yj/highlight (prefix)
(interactive "P")
(let* ((highlighted-regexps (if (boundp 'hi-lock-interactive-lighters)
(mapcar 'car hi-lock-interactive-lighters)
nil))
(selected-text (if (use-region-p)
(buffer-substring (region-beginning) (region-end))))
(to-highlight-or-unhighlight
(or selected-text (symbol-name (symbol-at-point))))
(already-highlighted (seq-find
(lambda (pat) (string-match to-highlight-or-unhighlight pat))
highlighted-regexps)))
(cond
(prefix (unhighlight-regexp t)) ;; unhighlight all if prefix argument passed in
(already-highlighted (unhighlight-regexp already-highlighted)) ;; if symbol at point is already highlighted, remove it
((use-region-p)
(let ((selected-text (buffer-substring (region-beginning) (region-end))))
(hi-lock-face-buffer selected-text)))
(t (hi-lock-face-symbol-at-point))))) ;; else, highlight the symbol
(defun yj/counsel-rg (prefix)
(interactive "P")
(let ((symbol-name-at-point (symbol-name (symbol-at-point))))
(if (and prefix symbol-name-at-point)
(counsel-rg symbol-name-at-point (counsel--git-root) nil nil)
(counsel-rg))))
(defun yj/imenu (prefix)
(interactive "p")
(cond ((= prefix 1) ;; single prefix
(counsel-imenu))
((= prefix 16) ;; double prefix
(lsp-ui-imenu))
(t
(imenu (imenu-choose-buffer-index)))))
(global-set-key (kbd "C-0") 'yj/delete-window-or-kill-buffer)
(global-set-key (kbd "C-1") 'yj/delete-other-windows-dwim)
(global-set-key (kbd "C-2") 'yj/counsel-rg)
(global-set-key (kbd "C-3") 'yj/switch-to-previous-editing-buffer)
(global-set-key (kbd "C-5") 'yj/highlight)
(global-set-key (kbd "C-7") 'swiper)
(global-set-key (kbd "C-8") 'yj/imenu)
;; (global-set-key (kbd "C-9") 'projectile-find-file-dwim)
(global-set-key (kbd "C-9") 'yj/find-file-at-point)
(global-set-key (kbd "C-x C-j") 'dired-jump)
(global-set-key (kbd "M-o") 'yj/other-window-dwim)
(add-hook 'ibuffer-mode-hook
(lambda () (define-key ibuffer-mode-map (kbd "M-o") nil)))
(global-set-key (kbd "M-O") 'yj/other-window-reversed)
(global-set-key (kbd "C-M-`") 'tab-next)
;; Bound to "C-z" because tmux uses prefix-z for similar functionality.
(global-set-key (kbd "C-z") 'yj/toggle-maximize-buffer)
;; Use "C-`" to toggle back-and-forth between eshell.
(global-set-key (kbd "C-`") 'yj/eshell-or-vterm)
(global-set-key (kbd "C-S-w") 'global-subword-mode)
(global-set-key (kbd "C-S-l") 'global-hide-mode-line-mode)
;; Use ibuffer as the default buffer switching mode.
(global-set-key (kbd "C-x C-b") 'ibuffer)
(autoload 'ibuffer "ibuffer" "List buffers." t)
;; Adapted from https://www.emacswiki.org/emacs/IbufferMode
(setq-default ibuffer-saved-filter-groups
`(("default"
("dired" (mode . dired-mode))
("emacs" (or
(name . "^\\*scratch\\*$")
(name . "^\\*Messages\\*$"))))))
(defun yj/ibuffer-switch-to-default ()
(interactive)
(ibuffer-switch-to-saved-filter-groups "default"))
(add-hook 'ibuffer-mode-hook 'yj/ibuffer-switch-to-default)
;; Copied From https://gist.github.com/fspeech/6004949
(defun hs-cycle (&optional arg)
"Visibility cycling for hs-minor-mode, inspired by outline-cycle from
outline-magic.el (https://github.com/tj64/outline-magic).
When repeatedly called we cycle through three states:
1. HIDE: Show only headlines in the current context.
2. OVERVIEW: Expand one level below HIDE to show more details.
3. SHOW: Show everything in the current context."
(interactive "P")
(setq deactivate-mark t)
(cond
((eq last-command 'hs-cycle-hide)
;; current state is HIDE, next OVERVIEW
(hs-hide-level 2)
(message "OVERVIEW...")
(setq this-command 'hs-cycle-overview))
((eq last-command 'hs-cycle-overview)
;; current state is OVERVIEW, next SHOW
(hs-life-goes-on
(message "SHOWING...")
;; we could (hs-hide-level A_LARGE_NUMBER) but that would be inefficient
(let ((minp (point-min))
(maxp (point-max)))
(save-excursion
(when (hs-find-block-beginning)
(setq minp (1+ (point)))
(funcall hs-forward-sexp-func 1)
(setq maxp (1- (point))))
(hs-discard-overlays minp maxp)))
(setq this-command 'hs-cycle-show)))
(t
;; Default: HIDE
(hs-hide-level 1)
(message "HIDING...")
(setq this-command 'hs-cycle-hide))))
(add-hook 'hs-minor-mode-hook
(lambda () (define-key hs-minor-mode-map (kbd "C-<tab>") 'hs-cycle)))
(setq hs-isearch-open t) ;; automatically unfolds when searching
;;; Experiment keybindngs
;; These are keybindngs that I am trying out find now
;; TODO: Bind to "C-9" (9 == nine == mnemonic for notes) to toggle open the last
;; org buffer / or a tagged buffer. OR add a feature to set "control group" to
;; buffer.
(global-set-key (kbd "C-4") 'treemacs)
(global-set-key (kbd "C-S-t") 'toggle-truncate-lines)
;;;-----------------------------------------------------------------------------
;;; Terminal-related
;;;-----------------------------------------------------------------------------
(use-package eshell
:init
(setq eshell-prompt-function 'yj/eshell-prompt)
(setq eshell-prompt-regexp "^[^#$<]*[$#>] ")
(setq eshell-highlight-prompt nil)
(setq eshell-cmpl-ignore-case t)
(setq eshell-cmpl-dir-ignore "\\`\\(\\.\\.?\\|CVS\\|\\.svn\\|\\.git\\)/\\'")
(setq eshell-save-history-on-exit t)
(setq eshell-hist-ignoredups t)
(setq eshell-cmpl-cycle-completions t)
(setq eshell-hist-match-partial nil)
(setq eshell-history-size 65536) ;; default is 128
:config
(add-hook 'eshell-mode-hook 'yj/eshell-custom-keymap)
(add-hook 'eshell-mode-hook 'yj/eshell-max-proess-read)
(add-hook 'eshell-mode-hook 'hl-line-mode)
(add-hook 'eshell-mode-hook 'company-mode)
(add-hook 'eshell-mode-hook (lambda () (setenv "PAGER" "cat"))) ;; avoid paging with less
;; (add-to-list 'company-backends 'company-eshell-history)
)
(defun yj/eshell-max-proess-read ()
(make-local-variable 'read-process-output-max)
(setq read-process-output-max 4096)) ;; Keep this value to default so we don't buffer too much
(defun yj/next-go-source-file-name ()
(interactive)
(forward-word) ;; to avoid searching current word
(search-forward-regexp "\\w+\\.go" nil t 1)
(backward-word 2))
(defun yj/prev-go-source-file-name ()
(interactive)
(search-backward-regexp "\\W\\w+\\.go" nil t nil)
(forward-char))
(defun yj/eshell-custom-keymap ()
;; (local-set-key (kbd "C-`") 'previous-buffer)
(local-set-key (kbd "C-`") 'yj/switch-to-previous-editing-buffer)
(local-set-key (kbd "C-<tab>") 'yj/next-or-new-eshell)
(local-set-key (kbd "M-N") 'yj/next-go-source-file-name)
(local-set-key (kbd "M-P") 'yj/prev-go-source-file-name)
(local-set-key (kbd "M-R") 'eshell-insert-history)
(local-set-key (kbd "C-c o") 'org-open-at-point))
(defun eshell-insert-history ()
"Displays the eshell history to select and insert back into your eshell."
(interactive)
(insert (completing-read "Eshell history: "
(delete-dups
(ring-elements eshell-history-ring)))))
(defun with-face (str &rest face-plist)
(propertize str 'face face-plist))
(defun yj/eshell-prompt ()
(concat
"\n"
(with-face (format-time-string "[%Y-%m-%d %H:%M] " (current-time)) :foreground "#888")
(with-face (concat (eshell/pwd) " "))
(with-face "\n")
"> "))
(defun company-eshell-history (command &optional arg &rest ignored)
"Prompt user with a list of eshell history."
(interactive (list 'interactive))
(cl-case command
(interactive (company-begin-backend 'company-eshell-history))
(prefix (and (eq major-mode 'eshell-mode)
(let ((word (company-grab-word)))
(save-excursion
(eshell-bol)
(and (looking-at-p (s-concat word "$")) word)))))
(candidates (delete-dups
(->> (ring-elements eshell-history-ring)
(seq-filter (lambda (item) (s-prefix-p arg item)))
(mapcar 's-trim))
))
(sorted t)))
(use-package vterm
:ensure t
:config
(defun yj/vterm-custom-keymap ()
(local-set-key (kbd "C-`") 'yj/switch-to-previous-editing-buffer)
(local-set-key (kbd "C-<tab>") 'yj/next-or-new-eshell))
(add-hook 'vterm-mode-hook 'yj/vterm-custom-keymap))
(defun yj/eshell-or-vterm (prefix)
"Switch the last recently used eshell, or create one if none; vterm if PREFIX."
(interactive "P")
(if prefix
(vterm)
(yj/mru-shell)))
(defun yj/mru-shell ()
"Switch to most recently used shell, or create one if none."
(interactive)
(let* ((buffers (buffer-list))
(is-shell-p (lambda (b) (string-match "\*eshell.*\\|\*vterm\*" (buffer-name b))))
(shell-buffers (seq-filter is-shell-p buffers))
(recent-shell-buffer (car shell-buffers)))
(if recent-shell-buffer
(switch-to-buffer recent-shell-buffer)
(eshell))))
(defun yj/next-or-new-eshell (prefix)
"Switch to the next eshell or vterm; with PREFIX, create a new eshell."
(interactive "P")
(let* ((other-buffers (reverse (cdr (buffer-list))))
(is-eshell-p (lambda (b) (string-match "\*eshell.*\\|\*vterm\*" (buffer-name b))))
(other-eshell-buffers (seq-filter is-eshell-p other-buffers)))
(cond
(prefix (eshell t))
((zerop (length other-eshell-buffers)) (eshell t))
(t (switch-to-buffer (car other-eshell-buffers))))))
;;;-----------------------------------------------------------------------------
;;; Look-and-Feel
;;;-----------------------------------------------------------------------------
(setq window-divider-default-bottom-width 1)
(setq window-divider-default-right-width 1)
(setq window-divider-default-places t)
;; split-width-threshold affects how Emacs decide whether to create a split to
;; the right (horizontally) or to the bottom (vertically).
;;(setq split-width-threshold 150)
;; writeroom-mode provides an experience much like VSCode's zen mode, centering
;; the single window, with blank areas on both side. A very clean and enjoyable
;; view.
(use-package writeroom-mode
:ensure t
:config
(setq writeroom-width 100)
(setq writeroom-header-line t))
;; solaire mode slightly changes the background colors of "editable" buffers as
;; compare to "non-editable" buffers like the minibuffer, treemacs side window
;; etc.
;; (use-package solaire-mode
;; :ensure t
;; :config
;; (add-to-list 'solaire-mode-themes-to-face-swap 'zenburn)
;; (solaire-global-mode +1))
(defun yj/update-font-sizes ()
;; Make the mode-line and header-line fonts smaller. Note: This code may need
;; to be called after the theme is loaded to prevent the theme from
;; overwritting the font size.
(set-face-attribute 'mode-line nil :font "Iosevka-17")
(set-face-attribute 'header-line nil :font "Iosevka-17"))
;; Use the zenburn theme, which is the default for the Prelude
;; distribution (https://github.com/bbatsov/prelude), which is the
;; distribution that first made Emacs a true delight for me.
(use-package zenburn-theme
:disabled
:ensure t
:config
(load-theme 'zenburn t)
(yj/update-font-sizes))
;; Use the Dark+ theme from Visual Code. Note the this theme current lacks good
;; support for org-mode (e.g., different colors for different heading levels,
;; different colors for inline verbatim vs code).
(use-package vscode-dark-plus-theme
;; :disabled
:ensure t
:config
(load-theme 'vscode-dark-plus t)
(yj/update-font-sizes))
;; Use the Darcula theme from JetBrains.
(use-package jetbrains-darcula-theme
:disabled
:ensure t
:config
(load-theme 'jetbrains-darcula t)
(yj/update-font-sizes))
;; Use the dracula theme, which provides visually- distinct colors for the
;; following:
;; - various heading levels in org-mode
;; - text selection
;; - highlight of symbol at point
(use-package dracula-theme
:disabled
:ensure t
:config
(load-theme 'dracula t)
(yj/update-font-sizes))
;; Display characters beyond column 80 in a different color
(setq-default
whitespace-line-column 100
whitespace-style '(face lines-tail))
;; Display vertical line at column specified by fill-column variable
(when (boundp 'display-fill-column-indicator-mode)
(display-fill-column-indicator-mode))
;; smart-mode-line provides a less ugly mode-line:
;; (https://github.com/Malabarba/smart-mode-line)
;; TODO: Continue evaluating this mode-line and make changes as necessary
(use-package smart-mode-line
:ensure t
:defer 0.2
:config
(sml/setup)
(setq sml/mode-width 'full
sml/name-width 30))
;; hide-mode-line-mode is a minor mode that hides the mode-line. We
;; can use it to toggle the visibility of mode-line by
;; enabling/disabling the minor mode.
;; (https://github.com/hlissner/emacs-hide-mode-line)
(use-package hide-mode-line
:ensure t
:defer 0.3
:config
(global-hide-mode-line-mode))
;;;-----------------------------------------------------------------------------
;;; Miscellaneus / Minor Packages
;;;-----------------------------------------------------------------------------
;; git-link is used to copy the URL to GitHub, GitLab, Bitbucket, etc.
;; (https://github.com/sshaw/git-link)
(use-package git-link
:ensure t
:commands git-link)
;; rainbow-mode is used to display common color formats (e.g., #RRGGBB) with the
;; actual colors being represented. This is useful when editing CSS / themes.
;; (http://git.savannah.gnu.org/cgit/emacs/elpa.git/?h=externals/rainbow-mode)
(use-package rainbow-mode
:ensure t
:commands rainbow-mode)
;; deadgrep is used for dwim grepping. Grepping is static (as opposed to live),
;; and the results are presented in a user-friendly buffer. Depending on use
;; case, I might choose between using this or counsel-rg.
;; (https://github.com/Wilfred/deadgrep)
(use-package deadgrep
:ensure t
:commands deadgrep)
;; git-gutter mode is used to disable in the left gutter indicators showing
;; whether the current line is added / modified.
;; (https://github.com/emacsorphanage/git-gutter)
(use-package git-gutter
:ensure t
:delight git-gutter-mode
:config
(global-git-gutter-mode)
:bind (("M-n" . git-gutter:next-hunk)
("M-p" . git-gutter:previous-hunk)))
;; expand-region.el (https://github.com/magnars/expand-region.el)
(use-package expand-region
:ensure t
:bind ("C-=" . 'er/expand-region))
;; golden-ration-scroll-screen
;; (https://github.com/jixiuf/golden-ratio-scroll-screen)
(use-package golden-ratio-scroll-screen
:ensure t)
(global-set-key [remap scroll-down-command] 'golden-ratio-scroll-screen-down)
(global-set-key [remap scroll-up-command] 'golden-ratio-scroll-screen-up)
;; eldoc (http://elpa.gnu.org/packages/eldoc.html)
;; Show function arglist or variable docstring in echo area.
(global-eldoc-mode -1)
;; ido
(setq ido-enable-flex-matching t)
(setq ido-everywhere t)
(ido-mode t)
;; smex
(use-package smex
:ensure t
:init (smex-initialize))
(global-set-key (kbd "M-x") 'smex)
(global-set-key (kbd "M-X") 'smex-major-mode-commands) ; This is your old M-x.
(global-set-key (kbd "C-c C-c M-x") 'execute-extended-command)
;; undo-tree
(use-package undo-tree
:disabled
:ensure t
:delight undo-tree-mode
:config
(global-undo-tree-mode))
;; elpy
(use-package elpy
:ensure t
:init
(elpy-enable))
;; flycheck
(use-package flycheck
:ensure t
:delight flycheck-mode
:init (global-flycheck-mode)
:bind (("M-N" . flycheck-next-error)
("M-P" . flycheck-previous-error)))
(setq flycheck-check-syntax-automatically '(mode-enabled save))
;; Always save bookmark files (and not just on exit)
(setq bookmark-save-flag 1)
;; which-key
(use-package which-key
:ensure t
:delight which-key-mode
:config (which-key-mode))
(use-package protobuf-mode
:ensure t)
;;;-----------------------------------------------------------------------------
;;; Major Packages
;;;-----------------------------------------------------------------------------
(defun yj/company-echo-doc-as-metadata-frontend (command)
"Adapted from company-echo-metadata-frontend; COMMAND must be `post-command'."
(pcase command
(`post-command (company-echo-show-soon 'yj/company-fetch-doc))
(`unhide (company-echo-show))
(`hide (company-echo-hide))))
(defun yj/company-fetch-doc ()
"Adapted from company-fetch-metadata and company-show-doc-buffer."
(let ((selected (nth (or company-selection 0) company-candidates)))
(unless (eq selected (car company-last-metadata))
(setq company-last-metadata
(cons selected (company-call-backend 'doc-buffer selected))))
(with-current-buffer (cdr company-last-metadata)
(buffer-string))))
(use-package company
:ensure t
:commands company-mode
:config
;; Immediately show completions after two characters are typed
(setq company-idle-delay 0)
(setq company-minimum-prefix-length 2)
(setq company-show-numbers t)
(setq company-selection-wrap-around t)
;; Use "C-n" and "C-p" to navigate lines in the file (as opposed to
;; company-mode's suggestions) as I already use tab to navigate the
;; suggestions.
(define-key company-active-map (kbd "C-n") nil)
(define-key company-active-map (kbd "C-p") nil)
(define-key company-active-map (kbd "C-v") 'company-next-page)
(define-key company-active-map (kbd "M-v") 'company-previous-page)
;; Unbind "C-d" to re-enable its default behavior instead of calling
;; `company-show-doc-buffer', which is already bound to "C-h".
(define-key company-active-map (kbd "C-d") nil)
;; Enable company-tng-mode, which uses tab key for navigating through
;; suggestions, and inserting into the buffer the currently suggestion as we
;; are tabbing through. For more, details, see the comany-tng.el file.
(add-hook 'after-init-hook 'company-tng-mode))
;; TODO: Find a way to get company-mode to also show documentation in a
;; user-friendly manner. Currently, when selecting a completion candidate, I can
;; press "C-d" to show the documentation in is buffer that'll take up half the
;; screen; this is horrible. I have also tried various other packages:
;;
;; - company-posframe: seems to be a little too heavy. Current complaints:
;; 1. still need to find a way to reconfigure bindings to use tab to navigate
;; through completion candidates,
;; 2. the documentation takes a while to show; and
;; 3. there is screen flicker when it shows the documentation for the first
;; time.
;; (use-package company-posframe
;; :ensure t
;; :after company
;; :config