-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.el
1683 lines (1474 loc) · 55.4 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
;;* dependencies
;; provides string-trim
(eval-when-compile (require 'subr-x))
;;* file and path utilities
(defvar nh/icloud
(expand-file-name "~/Library/Mobile Documents/com~apple~CloudDocs/Documents/sync")
"Base directory for files stored in icloud")
(defvar nh/onedrive
(expand-file-name "~/Library/CloudStorage/OneDrive-UW/emacs-sync")
"Base directory for files stored in OneDrive")
(defun nh/path-join (&rest x)
"Join elements of x with a path separator and apply `expand-file-name'"
(expand-file-name
(concat
(mapconcat 'file-name-as-directory (seq-take x (- (length x) 1)) "")
(elt x (- (length x) 1)))))
(defun nh/emacs-dir-path (name)
"Return absolute path to a file in the same directory as `user-init-file'"
(expand-file-name name user-emacs-directory))
(defun nh/safename (str)
"Remove non-alphanum characters and downcase"
(let ((exprs '(("^\\W+" "") ("\\W+$" "") ("\\W+" "-"))))
(dolist (e exprs)
(setq str (replace-regexp-in-string (nth 0 e) (nth 1 e) str)))
(downcase str)))
(defun nh/iterm2-open-project-dir ()
"Open the current project root in a new tab in iTerm2"
(interactive)
(let* ((interpreter (getenv "IT2PY"))
(home (getenv "HOME"))
(script
(nh/path-join home "dotfiles" "mac" "bin" "iterm2_create_tab.py"))
(thisdir (or (projectile-project-root) home)))
(shell-command (format "\"%s\" \"%s\" \"%s\"" interpreter script thisdir))))
;;* Package management
(require 'package)
(setq package-archives
'(("ELPA" . "https://tromey.com/elpa/")
("gnu" . "https://elpa.gnu.org/packages/")
("melpa" . "https://melpa.org/packages/")
("melpa-stable" . "https://stable.melpa.org/packages/")))
(setq package-archive-priorities
'(("melpa-stable" . 20)
("gnu" . 10)
("melpa" . 5)))
(setq package-native-compile t)
(setq package-menu-hide-low-priority t)
(setq package-check-signature nil) ;; TODO: fix this properly
(package-initialize)
;; bootstrap use-package use-package is built in as of emacs 29; at
;; some point this can be removed
(unless (package-installed-p 'use-package)
(if (yes-or-no-p "use-package is not installed yet - install it? ")
(progn
(message "** installing use-package")
(package-refresh-contents)
(package-install 'use-package))
(message "** defining fake use-package macro")
(defmacro use-package (pkg &rest args)
(warn
"use-package is not installed - could not activate %s"
(symbol-name pkg)))))
;; bootstrap straight
;; from https://github.com/radian-software/straight.el
(defvar bootstrap-version)
(let ((bootstrap-file
(expand-file-name
"straight/repos/straight.el/bootstrap.el" user-emacs-directory))
(bootstrap-version 6))
(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))
;; save customizations here instead of init.el
(setq custom-file (nh/emacs-dir-path "custom.el"))
(unless (file-exists-p custom-file)
(write-region "" nil custom-file))
(load custom-file)
;;* startup and shutdown
(defun nh/set-default-font-verbosely (font-name)
(interactive)
(message (format "** setting default font to %s" font-name))
(condition-case nil
;; (set-default-font font-name)
(set-face-attribute 'default nil :font font-name)
(error (message (format "** Error: could not set to font %s" font-name)))))
(defun nh/fix-frame (&optional frame)
"Apply platform-specific settings."
(interactive)
(cond ((string= "ns" window-system) ;; cocoa
(progn
(message (format "** running %s windowing system" window-system))
;; key bindings for mac - see
;; http://stuff-things.net/2009/01/06/emacs-on-the-mac/
;; http://osx.iusethis.com/app/carbonemacspackage
(set-keyboard-coding-system 'mac-roman)
(setq mac-option-modifier 'meta)
(setq mac-command-key-is-meta nil)
(nh/set-default-font-verbosely "Menlo-15")))
((string= "x" window-system)
(progn
(message (format "** running %s windowing system" window-system))
(set-default-font-verbosely "Liberation Mono-10")
;; M-w or C-w copies to system clipboard
;; see http://www.gnu.org/software/emacs/elisp/html_node/Window-System-Selections.html
(setq x-select-enable-clipboard t)))
(t
(message "** running in terminal mode"))))
(nh/fix-frame)
(setq inhibit-splash-screen t)
(setq initial-scratch-message nil)
(setq require-final-newline t)
(setq delete-trailing-lines nil)
(add-hook 'before-save-hook 'delete-trailing-whitespace)
;; buffers opened from command line don't create new frame
(setq ns-pop-up-frames nil)
;; require prompt before exit on C-x C-c
(defun nh/ask-before-exit ()
(interactive)
(cond ((y-or-n-p "Quit? (save-buffers-kill-terminal) ")
(save-buffers-kill-terminal))))
(global-set-key (kbd "C-x C-c") 'nh/ask-before-exit)
;; kill buffer without asking which one by default
(global-set-key (kbd "C-x k") 'kill-this-buffer)
;;* desktop
(defun nh/desktop-save-no-p ()
"Save desktop without prompting (replaces `desktop-save-in-desktop-dir')"
(interactive)
(desktop-save desktop-dirname))
(if (member "--no-desktop" command-line-args)
(message "** desktop auto-save is disabled")
(progn
(message "** desktop auto-save is enabled")
(require 'desktop)
(desktop-save-mode 1)
(add-hook 'auto-save-hook 'nh/desktop-save-no-p)))
;;* bookmarks
(defun nh/set-bookmark-for-function ()
(interactive)
(let* ((tag (read-string "project tag: "))
(funcname (which-function))
(name (format "%s %s" tag funcname)))
(if (y-or-n-p (format "set bookmark '%s'? " name))
(bookmark-set name))))
;;* appearance and GUI
(blink-cursor-mode 1)
(set-cursor-color "red")
(menu-bar-mode -1) ;; hide menu bar
(scroll-bar-mode -1) ;; hide scroll bar
(tool-bar-mode -1) ;; hide tool bar
(setq column-number-mode t)
(setq ediff-split-window-function 'split-window-horizontally)
;; prevent windows from being split vertically
(setq split-height-threshold nil)
;; File path in title bar.
(setq frame-title-format
(list (format "%s %%S: %%j " (system-name))
'(buffer-file-name "%f" (dired-directory dired-directory "%b"))))
;; show matching parens
(show-paren-mode 1)
(use-package rainbow-delimiters
:ensure t)
;; use list-faces-display to show preview of all faces for the current theme
;; use M-x customize-group to modify theme elements in specific modes
(defvar nh/theme-dark 'spacemacs-dark)
(defvar nh/theme-light 'spacemacs-light)
(defun nh/hex-to-rgb (hexcolor)
"Return a list of decimal RGB values from a hex color name"
(mapcar (lambda (start)
(string-to-number
(substring-no-properties hexcolor start (+ start 2)) 16))
'(1 3 5)))
(defun nh/toggle-theme ()
"Toggle theme between preferred light and dark themes"
(interactive)
;; sum the RGB values of the current theme's background color and guess that
;; the current theme is dark if < 300
(if (< (apply '+ (nh/hex-to-rgb (face-attribute 'default :background))) 300)
(load-theme nh/theme-light t)
(load-theme nh/theme-dark t)))
(use-package spacemacs-theme
:ensure t
:defer t
:init (load-theme nh/theme-dark t))
;;* execution environment
(defun nh/ssh-refresh ()
"Reset the environment variable SSH_AUTH_SOCK"
(interactive)
(let
((ssh-auth-sock-old (getenv "SSH_AUTH_SOCK"))
(mac-cmd "ls -t $(find /tmp/* -user $USER -name Listeners 2> /dev/null)")
(linux-cmd "ls -t $(find /tmp/ssh-* -user $USER -name 'agent.*' 2> /dev/null)"))
(setenv "SSH_AUTH_SOCK"
(car (split-string
(shell-command-to-string
(if (eq system-type 'darwin) mac-cmd linux-cmd)))))
(message
(format "SSH_AUTH_SOCK %s --> %s"
ssh-auth-sock-old (getenv "SSH_AUTH_SOCK")))))
(defun nh/prepend-path (path)
"Add `path' to the beginning of $PATH unless already present."
(interactive)
(unless (string-match path (getenv "PATH"))
(setenv "PATH" (concat path ":" (getenv "PATH")))))
(nh/prepend-path (nh/emacs-dir-path "bin"))
(add-to-list 'exec-path (nh/emacs-dir-path "bin"))
;;* other utility functions
(defun nh/advice-unadvice (sym)
"Remove all advices from symbol SYM."
(interactive "aFunction symbol: ")
(advice-mapc (lambda (advice _props) (advice-remove sym advice)) sym))
;; fix errors with connection to package repositories
;; see https://github.com/melpa/melpa/issues/7238
;; suppress on Ubuntu 18.04 to prevent errors
(unless
(equal (string-trim (shell-command-to-string "lsb_release -rs")) "18.04")
(setq gnutls-algorithm-priority "NORMAL:-VERS-TLS1.3"))
;;* dired
;; use 'ls --dired' if available
(setq dired-use-ls-dired
(if (eq (call-process-shell-command "ls --dired" nil nil nil) 0)
t nil))
;; dired performs file renaming using underlying version control system
(setq dired-vc-rename-file t)
;;* other settings
(put 'downcase-region 'disabled nil)
(put 'upcase-region 'disabled nil)
(put 'narrow-to-region 'disabled nil)
;; Default 'untabify converts a tab to equivalent number of spaces
;; before deleting a single character.
(setq backward-delete-char-untabify-method "all")
(setq-default indent-tabs-mode nil)
;; Let a period followed by a single space be treated as end of sentence
(setq sentence-end-double-space nil)
;; (setq-default fill-column 80)
(setq suggest-key-bindings 4)
(setq mouse-wheel-scroll-amount '(3 ((shift) . 3))) ;; number of lines at a time
(setq mouse-wheel-progressive-speed nil) ;; don't accelerate scrolling
(setq mouse-wheel-follow-mosue 't) ;; scroll window under mouse
(setq scroll-step 1) ;; keyboard scroll one line at a time
(setq scroll-conservatively 1) ;; scroll by one line to follow cursor off screen
(setq scroll-margin 2) ;; Start scrolling when 2 lines from top/bottom
(global-set-key (kbd "<f5>") 'call-last-kbd-macro)
;;* general utilities
(defun nh/back-window ()
"switch windows with C- and arrow keys"
(interactive)
(other-window -1))
(global-set-key (kbd "C-<right>") 'other-window)
(global-set-key (kbd "C-<left>") 'nh/back-window)
(defun nh/insert-date ()
"insert today's timestamp in format '<%Y-%m-%d %a>'"
(interactive)
(insert (format-time-string "<%Y-%m-%d %a>")))
(defun nh/copy-buffer-file-name ()
"Add `buffer-file-name' to `kill-ring' and echo the value to
the minibuffer"
(interactive)
(if buffer-file-name
(progn
(kill-new buffer-file-name t)
(message buffer-file-name))
(message "no file associated with this buffer")))
(defun nh/move-line-up ()
(interactive)
(transpose-lines 1)
(previous-line 2))
(global-set-key (kbd "M-<up>") 'nh/move-line-up)
(defun nh/move-line-down ()
(interactive)
(next-line 1)
(transpose-lines 1)
(previous-line 1))
(global-set-key (kbd "M-<down>") 'nh/move-line-down)
(defun nh/transpose-buffers (arg)
"Transpose the buffers shown in two windows."
(interactive "p")
(let ((selector (if (>= arg 0) 'next-window 'previous-window)))
(while (/= arg 0)
(let ((this-win (window-buffer))
(next-win (window-buffer (funcall selector))))
(set-window-buffer (selected-window) next-win)
(set-window-buffer (funcall selector) this-win)
(select-window (funcall selector)))
;; (setq arg (if (plusp arg) (1- arg) (1+ arg)))
(setq arg (if (>= arg 0) (1- arg) (1+ arg)))
)))
(global-set-key (kbd "C-x 4") 'nh/transpose-buffers)
(defun nh/switch-buffers-between-frames ()
"switch-buffers-between-frames switches the buffers between the two last frames"
(interactive)
(let ((this-frame-buffer nil)
(other-frame-buffer nil))
(setq this-frame-buffer (car (frame-parameter nil 'buffer-list)))
(other-frame 1)
(setq other-frame-buffer (car (frame-parameter nil 'buffer-list)))
(switch-to-buffer this-frame-buffer)
(other-frame 1)
(switch-to-buffer other-frame-buffer)))
(global-set-key (kbd "C-x 5") 'nh/switch-buffers-between-frames)
(defun nh/toggle-frame-split ()
"If the frame is split vertically, split it horizontally or vice versa.
Assumes that the frame is only split into two."
(interactive)
(unless (= (length (window-list)) 2) (error "Can only toggle a frame split in two"))
(let ((split-vertically-p (window-combined-p)))
(delete-window) ; closes current window
(if split-vertically-p
(split-window-horizontally)
(split-window-vertically)) ; gives us a split with the other window twice
(switch-to-buffer nil))) ; restore the original window in this part of the frame
(global-set-key (kbd "C-x 6") 'nh/toggle-frame-split)
(defun nh/unfill-paragraph ()
(interactive)
(let ((fill-column (point-max)))
(fill-paragraph nil)))
(global-set-key (kbd "M-C-q") 'nh/unfill-paragraph)
(defun nh/copy-region-or-line-other-window ()
"Copy selected text or current line to other window"
(interactive)
(progn (save-excursion
(if (region-active-p)
(copy-region-as-kill
(region-beginning) (region-end))
(copy-region-as-kill
(line-beginning-position) (+ (line-end-position) 1)))
(other-window 1)
(yank))
(other-window -1)))
;; copied from https://emacs.stackexchange.com/questions/54659/how-to-delete-surrounding-brackets/54679#54679
(defun nh/delete-surround-at-point--find-brackets (pos)
"Return a pair of buffer positions for the opening & closing bracket positions.
Or nil when nothing is found."
(save-excursion
(goto-char pos)
(when
(or
;; Check if we're on the opening brace.
(when
;; Note that the following check for opening brace
;; can be skipped, however it can cause the entire buffer
;; to be scanned for an opening brace causing noticeable lag.
(and
;; Opening brace.
(eq (syntax-class (syntax-after pos)) 4)
;; Not escaped.
(= (logand (skip-syntax-backward "/\\") 1) 0))
(forward-char 1)
(if (and (ignore-errors (backward-up-list 1) t) (eq (point) pos))
t
;; Restore location and fall through to the next check.
(goto-char pos)
nil))
;; Check if we're on the closing or final brace.
(ignore-errors (backward-up-list 1) t))
;; Upon success, return the pair as a list.
(list (point)
(progn
(forward-list)
(1- (point)))))))
(defun nh/delete-surround-at-point ()
(interactive)
(let ((range (nh/delete-surround-at-point--find-brackets (point))))
(unless range
(user-error "No surrounding brackets"))
(pcase-let ((`(,beg ,end) range))
;; For user message.
(let ((lines (count-lines beg end))
(beg-char (char-after beg))
(end-char (char-after end)))
(save-excursion
(goto-char end)
(delete-char 1)
(goto-char beg)
(delete-char 1))
(message
"Delete surrounding \"%c%c\"%s" beg-char end-char
(if (> lines 1)
(format " across %d lines" lines)
""))))))
(defun nh/get-finder-directory ()
"Return the absolute path of the directory of the active Finder window."
(let ((script-file (nh/path-join (nh/emacs-dir-path "bin") "get-finder-dir.scpt")))
(string-trim-right
(shell-command-to-string
(format "/usr/bin/osascript %s" script-file)))))
;;* spelling
(defvar nh/enable-flyspell-p "enable flyspell in various modes")
;; use aspell if installed
(if (cond
((executable-find "aspell")
(setq ispell-dictionary "en")
(setq ispell-program-name "aspell")))
(progn
(message "** using %s for flyspell" ispell-program-name)
(autoload 'flyspell-mode "flyspell" "On-the-fly spelling checker." t)
(setq flyspell-issue-welcome-flag nil)
(setq nh/enable-flyspell-p t))
(setq nh/enable-flyspell-p nil)
(message "** could not find hunspell or aspell"))
;;* init file utilities
;; TODO: refer to
(defun nh/init-file-edit ()
"Edit `user-init-file'"
(interactive)
(find-file user-init-file))
(defun nh/init-file-header-occur ()
(interactive)
(find-file user-init-file)
(occur "^;;\\* "))
(defun nh/init-file-use-package-occur ()
(interactive)
(find-file user-init-file)
(occur "^(use-package"))
(defun nh/init-file-header-insert ()
"insert ';;*' header"
(interactive)
(insert ";;*"))
(defun nh/init-file-load ()
"Reload init file"
(interactive)
(load user-init-file))
;;* saving
(setq make-backup-files nil)
(global-auto-revert-mode t)
(unless (< emacs-major-version 27)
(setq auto-revert-avoid-polling t))
;; save buffers automatically
(use-package super-save
:ensure t
:config
(super-save-mode +1))
(setq undo-limit 800000)
(setq undo-strong-limit 12000000)
(setq undo-outer-limit 120000000)
;;* search and navigation (vertico, consult)
(use-package vertico
:ensure t
:init
(vertico-mode)
;; Different scroll margin
;; (setq vertico-scroll-margin 0)
;; Show more candidates
(setq vertico-count 20)
;; Grow and shrink the Vertico minibuffer
(setq vertico-resize t)
;; Optionally enable cycling for `vertico-next' and `vertico-previous'.
;; (setq vertico-cycle t)
)
(use-package orderless
:ensure t
:init
;; Configure a custom style dispatcher (see the Consult wiki)
;; (setq orderless-style-dispatchers '(+orderless-dispatch)
;; orderless-component-separator #'orderless-escapable-split-on-space)
(setq completion-styles '(orderless basic)
completion-category-defaults nil
completion-category-overrides '((file (styles partial-completion))))
)
(use-package consult
:ensure t
;; Replace bindings. Lazily loaded due by `use-package'.
:bind (("C-x b" . consult-buffer)
;; ("C-s" . consult-line) ;; swiper is better
("M-g M-g" . consult-goto-line)
("M-g g" . consult-goto-line)
("M-y" . consult-yank-pop))
;; Enable automatic preview at point in the *Completions* buffer. This is
;; relevant when you use the default completion UI.
;; :hook (completion-list-mode . consult-preview-at-point-mode)
;; The :init configuration is always executed (Not lazy)
:init
;; Optionally configure the register formatting. This improves the register
;; preview for `consult-register', `consult-register-load',
;; `consult-register-store' and the Emacs built-ins.
;; (setq register-preview-delay 0.5
;; register-preview-function #'consult-register-format)
;; Optionally tweak the register preview window.
;; This adds thin lines, sorting and hides the mode line of the window.
;; (advice-add #'register-preview :override #'consult-register-window)
;; Use Consult to select xref locations with preview
(setq xref-show-xrefs-function #'consult-xref
xref-show-definitions-function #'consult-xref)
:config
(consult-customize
consult-theme :preview-key '(:debounce 0.2 any)
consult-ripgrep consult-git-grep consult-grep
consult-bookmark consult-recent-file consult-xref
consult--source-bookmark consult--source-file-register
consult--source-recent-file consult--source-project-recent-file
;; :preview-key (kbd "M-.")
:preview-key '(:debounce 0.4 any))
;; Optionally configure the narrowing key.
;; Both < and C-+ work reasonably well.
(setq consult-narrow-key "<") ;; (kbd "C-+")
)
(use-package marginalia
:ensure t
;; Either bind `marginalia-cycle' globally or only in the minibuffer
:bind (("M-A" . marginalia-cycle)
:map minibuffer-local-map
("M-A" . marginalia-cycle))
:init (marginalia-mode))
;;* search and navigation (ivy, counsel, and friends)
;; (use-package ivy
;; :ensure t
;; :pin melpa
;; :config
;; (ivy-mode 1)
;; (setq enable-recursive-minibuffers t)
;; (setq ivy-count-format "%d/%d ")
;; (setq ivy-height 30)
;; (global-set-key (kbd "C-c C-r") 'ivy-resume))
;; (use-package counsel
;; :ensure t
;; :pin melpa
;; :bind (("M-x" . counsel-M-x)
;; ("C-x C-f" . counsel-find-file)
;; ("C-c g" . counsel-git)
;; ("C-c j" . counsel-git-grep)
;; ("C-c a" . counsel-ag)
;; ("M-y" . counsel-yank-pop))
;; :config
;; (define-key minibuffer-local-map (kbd "C-r") 'counsel-minibuffer-history))
(use-package swiper
:ensure t
:config
(global-set-key (kbd "C-s") 'swiper))
(use-package projectile
:ensure t
:init
;; (setq projectile-completion-system 'ivy)
:config
(define-key projectile-mode-map (kbd "C-c p") 'projectile-command-map)
(add-to-list 'projectile-globally-ignored-modes "fundamental-mode")
(projectile-mode +1))
(use-package avy
:ensure t
:bind (("M-'" . avy-goto-word-1)
("C-M-SPC" . avy-goto-char-timer)))
;; see https://github.com/ericdanan/counsel-projectile
;; (use-package counsel-projectile
;; :ensure t
;; :config
;; (counsel-projectile-mode))
(use-package rg
:ensure t
:config
(rg-enable-menu))
(when (boundp 'grep-find-ignored-directories)
(add-to-list 'grep-find-ignored-directories ".eggs")
(add-to-list 'grep-find-ignored-directories "src"))
;; (defun nh/grep-ignore-venv-current-project (&rest args)
;; (interactive)
;; (let ((venv (find-venv-current-project)))
;; (if venv
;; (progn
;; (setq venv (file-name-nondirectory
;; (replace-regexp-in-string "/$" "" venv)))
;; (message "adding '%s' to grep-find-ignored-directories" venv)
;; (add-to-list 'grep-find-ignored-directories venv))
;; (message "no virtualenv at this location")
;; )))
;; (advice-add 'rgrep :before #'nh/grep-ignore-venv-current-project)
;; (advice-add 'projectile-grep :before #'nh/grep-ignore-venv-current-project)
;; (advice-add 'counsel-projectile-grep :before #'nh/grep-ignore-venv-current-project)
;;* auto-complete using company-mode
;; (use-package company
;; :ensure t
;; :defer t
;; :config
;; (setq company-minimum-prefix-length 1
;; company-idle-delay 0
;; company-tooltip-limit 10
;; company-transformers nil
;; company-show-numbers t)
;; (global-company-mode)
;; :hook (python-mode . company-mode))
;; * elgot
(use-package eglot
:ensure t
:defer t
:config
(setq eldoc-echo-area-use-multiline-p nil))
;;* elisp
(use-package paredit
:ensure t
:bind
(:map paredit-mode-map
("C-<left>" . nh/back-window)
("C-<right>" . other-window)))
;;* debugging emacs
;; (use-package explain-pause-mode
;; :straight (explain-pause-mode
;; :type git
;; :host github
;; :repo "lastquestion/explain-pause-mode")
;; :config
;; (explain-pause-mode))
;;* python
(defcustom nh/py3-venv
(nh/emacs-dir-path "py3-env") "virtualenv for flycheck, etc")
(defcustom nh/venv-setup-packages
'("pip" "wheel" "'python-lsp-server[all]'" "autoflake" "mypy")
"packages to install using `nh/venv-setup'")
(defun nh/py3-venv-bin (name)
"Return the path to an executable installed in `nh/py3-venv'"
(nh/path-join nh/py3-venv "bin" name))
(use-package python-mode
:preface
(defun nh/python-shell-make-comint (orig-fun &rest args)
"Fix issue where python code block evaluation freezes on a mac in
org-mode using :session. This as a bug in prompt detection
in python.el: apparently the startup message for the python
interpreter is not being recognized. Launching the
interpreter with python -q suppresses the prompt, but the
variable python-shell-interpreter-args does not appear to be
respected. So the brute force solution is to advise the
function that sets up inferior-python-mode to add -q:"
(setq args (append '("python3 -q") (cdr args)))
(apply orig-fun args))
(if (eq system-type 'darwin)
(progn
(advice-add 'python-shell-make-comint :around #'nh/python-shell-make-comint)
(setq python-shell-completion-native-enable nil)))
:mode
("\\.py$'" . python-mode)
("\\.wsgi$" . python-mode)
("\\.cgi$" . python-mode)
("SConstruct" . python-mode)
("SConscript" . python-mode)
:config
(setq python-shell-interpreter "python3")
(setq tab-width 4)
(setq python-indent-guess-indent-offset t)
(setq python-indent-guess-indent-offset-verbose nil)
(setq python-indent-offset tab-width)
:hook
(python-mode . (lambda ()
(setq display-fill-column-indicator-column 80))))
(use-package pyvenv
:ensure t)
(defun nh/venv-list (basedir)
"Return a list of paths to virtualenvs in 'basedir' or nil if
none can be found"
(interactive)
(let ((fstr "find %s -path '*bin/activate' -maxdepth 5")
(pth (replace-regexp-in-string "/$" "" basedir)))
(mapcar (lambda (string)
(replace-regexp-in-string "/bin/activate$" "" string))
(cl-remove-if
(lambda (string) (= (length string) 0))
(split-string (shell-command-to-string (format fstr pth)) "\n")))
))
(defun nh/pylsp-installed-p ()
(= 0 (shell-command "python -c 'import pylsp' 2>/dev/null")))
(defun nh/venv-activate-eglot ()
"Activate eglot in the selected virtualenv, installing
dependencies if necessary."
(interactive)
(nh/venv-activate)
(unless (nh/pylsp-installed-p)
(save-excursion (nh/venv-setup)))
(eglot-ensure))
(defun nh/venv-activate ()
"Activate the virtualenv in the current project, or in
`default-directory' if not in a project. Prompts for a selection
if there is more than one option."
(interactive)
(let* ((thisdir (or (projectile-project-root) default-directory))
(venvs (append
(nh/venv-list thisdir)
(nh/venv-list (expand-file-name "~/.pyenv/versions"))
`(,nh/py3-venv)))
;; maybe use completing-read
(venv (completing-read "choose a virtualenv: " venvs)))
(pyvenv-activate venv)
(message "Activated virtualenv %s (%s)"
venv (string-trim (shell-command-to-string "python3 --version")))))
(defun nh/venv-setup ()
"Install or update packages specified in `nh/venv-setup-packages'
to the active virtualenv. Prompts for a selection if no
virtualenv is active."
(interactive)
(unless pyvenv-virtual-env (nh/venv-activate))
(let ((bufname nil)
(packages (mapconcat 'identity nh/venv-setup-packages " ")))
(if (nh/pylsp-installed-p)
(message "dependencies already installed")
(if (y-or-n-p (format "Install dependencies to %s?" pyvenv-virtual-env))
(progn (setq bufname (generate-new-buffer
(format "*%s*" pyvenv-virtual-env)))
(if (= 0 (call-process-shell-command
(format "uv pip install -U --python %sbin/python %s"
pyvenv-virtual-env packages)
nil bufname t))
(message "installation complete, see output in %s" bufname)
(switch-to-buffer bufname)))))))
(defun nh/pip-install (package)
"Pip install a python package in a virtualenv. Prompts for a
selection if no virtualenv is active."
(interactive "sPackage name: ")
(unless pyvenv-virtual-env (nh/venv-activate))
(let ((bufname (generate-new-buffer (format "*%s*" pyvenv-virtual-env)))
(command (format "uv pip install -U --python %sbin/python %s"
pyvenv-virtual-env package)))
(if (= 0 (call-process-shell-command command nil bufname t))
(message "installation complete, see output in %s" bufname)
(switch-to-buffer bufname))))
(use-package flycheck
:ensure t
:pin melpa
:config
(setq flycheck-flake8rc (nh/emacs-dir-path "flake8.conf"))
(setq flycheck-pylintrc (nh/emacs-dir-path "python-pylint.conf"))
(setq flycheck-temp-prefix ".flycheck")
:hook
(python-mode . flycheck-mode))
(defun nh/python-flycheck-select-checker (checker)
(interactive)
(flycheck-reset-enabled-checker checker)
(flycheck-disable-checker checker t)
(flycheck-select-checker checker))
(defun nh/python-flycheck-select-checkers ()
(interactive)
(nh/venv-setup)
(flycheck-mode t)
;; checkers are run in reverse order of activation in lines below
(nh/python-flycheck-select-checker 'python-mypy)
(nh/python-flycheck-select-checker 'python-pylint)
(nh/python-flycheck-select-checker 'python-flake8)
;; (flycheck-verify-setup)
)
;; function to reformat using yapf
(defun nh/yapf-region-or-buffer ()
"Apply yapf to the current region or buffer"
(interactive)
(let* ((yapf (nh/py3-venv-bin "yapf"))
(yapf-config (nh/emacs-dir-path "yapf.cfg"))
;; use config file if exists
(yapf-cmd (if (file-exists-p yapf-config)
(concat yapf " --style " yapf-config)
yapf)))
(unless (region-active-p)
(mark-whole-buffer))
(shell-command-on-region
(region-beginning) (region-end) ;; beginning and end of region or buffer
yapf-cmd ;; command and parameters
(current-buffer) ;; output buffer
t ;; replace?
"*yapf errors*" ;; name of the error buffer
t) ;; show error buffer?
))
;; (defun nh/autopep8-and-ediff ()
;; "Compare the current buffer to the output of autopep8 using ediff"
;; (interactive)
;; (let ((p8-output
;; (get-buffer-create (format "* %s autopep8 *" (buffer-name)))))
;; (shell-command-on-region
;; (point-min) (point-max) ;; beginning and end of buffer
;; "autopep8 -" ;; command and parameters
;; p8-output ;; output buffer
;; nil ;; replace?
;; "*autopep8 errors*" ;; name of the error buffer
;; t) ;; show error buffer?
;; (ediff-buffers (current-buffer) p8-output)))
;;* javascript/json
(use-package json-mode
:ensure t)
(add-hook 'js-mode-hook
(lambda ()
(make-local-variable 'js-indent-level)
(setq js-indent-level 2)))
(add-hook 'json-mode-hook
(lambda ()
(make-local-variable 'js-indent-level)
(setq js-indent-level 2)))
;; (straight-use-package
;; '(tsx-mode
;; :type git
;; :host github
;; :repo "orzechowskid/tsx-mode.el"))
;;* ESS (R language support)
(defun nh/set-inferior-ess-r-program-name ()
"Set `inferior-ess-r-program-name' as the absolute path to the R
interpreter. On systems using 'modules'
(http://modules.sourceforge.net/), load the R module before defining
the path."
(interactive)
(setq inferior-ess-r-program-name
(string-trim
(shell-command-to-string
"which ml > /dev/null && (ml R; which R) || which R"))))
(use-package ess
:ensure t
:pin melpa
:defer t
:config
(setq ess-S-assign "_")
(add-hook 'ess-mode-hook
(lambda()
(message "** Loading ess-mode hooks")
(ess-set-style 'GNU 'quiet)
(nh/set-inferior-ess-r-program-name))))
;; (use-package poly-R
;; :ensure t)
(use-package markdown-mode
:commands (markdown-mode gfm-mode)
:mode (("README\\.md" . gfm-mode)
("\\.md" . markdown-mode)
("\\.markdown" . markdown-mode))
:bind (:map markdown-mode-map
;; don't redefine =M-<left>= and =M-<right>= in this mode
("M-<right>" . nil)
("M-<left>" . nil))
:init (setq markdown-command "multimarkdown")
:config
;; (set-face-background 'markdown-pre-face "grey20")
;; (set-face-background 'markdown-markup-face "grey20")
;; (set-face-background 'markdown-code-face "grey20")
;; (set-face-background 'markdown-inline-code-face "grey20")
;; (set-face-foreground 'markdown-markup-face "lavender")
)
;; https://plantarum.ca/2021/10/03/emacs-tutorial-rmarkdown/
(use-package poly-markdown
:ensure t
:init (setq markdown-code-block-braces t)
:mode (("\\.Rmd" . poly-gfm+r-mode)))
;;* org-mode
(use-package org
:preface
(defun nh/org-mode-hooks ()
(visual-line-mode)
(yas-minor-mode t))
(defadvice org-todo-list-bottom (after nh/org-todo-list ())
"Move to bottom of page after entering org-todo-list"
(progn (end-of-buffer) (recenter-top-bottom)))
(defadvice org-download-screenshot (before nh/org-download-screenshot-advice ())
"Remove extra lines before inserted screenshot and check for pngpaste"
(if (executable-find "pngpaste")
(progn (delete-blank-lines) (org-delete-backward-char 1))
(error "pngpaste is not installed")))
(defun nh/org-mode-add-tag ()
"Add a tag to the current headline"
(interactive)
(end-of-line)
(delete-horizontal-space)
(insert
(format " :%s:" (completing-read "Tag: " (nh/org-mode-list-tags)))))
(defun nh/org-mode-list-tags ()
(let ((all-tags '()))
(org-map-entries
(lambda ()
(let ((tag-string (car (last (org-heading-components)))))
(when tag-string
(setq all-tags
(append all-tags (split-string tag-string ":" t))))))
t nil)
(delq nil (delete-dups all-tags))))
:mode
("\\.org\\'" . org-mode)
("\\.org\\.txt\\'" . org-mode)
:bind
(:map org-mode-map
("M-<right>" . forward-word)
("M-<left>" . backward-word)
("M-S-<right>" . org-do-demote)
("M-S-<left>" . org-do-promote)
("C-c C-v" . verb-command-map))