-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
init-dont-panic.el
6522 lines (5748 loc) · 224 KB
/
init-dont-panic.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 --- Nasy's emacs.d init file. -*- lexical-binding: t; -*-
;; Copyright (C) 2018 Nasy
;; Author: Nasy <[email protected]>
;;; Commentary:
;; Nasy's emacs.d init file. For macOS and Emacs 26, Emacs 27.
(setq-default lexical-binding t)
(setq-default debug-on-error t
message-log-max t
load-prefer-newer t
ad-redefinition-action 'accept
gc-cons-threshold most-positive-fixnum)
(defconst *is-a-mac* (eq system-type 'darwin))
;;; Borrow from doom emacs.
(define-error 'n-error "Error in Nasy Emacs core")
(define-error 'n-hook-error "Error in a Nasy startup hook" 'Nasy-error)
(defun nasy-unquote (exp)
"Return EXP unquoted."
(declare (pure t) (side-effect-free t))
(while (memq (car-safe exp) '(quote function))
(setq exp (cadr exp)))
exp)
(defun nasy-enlist (exp)
"Return EXP wrapped in a list, or as-is if already a list."
(declare (pure t) (side-effect-free t))
(if (listp exp) exp (list exp)))
(defun nasy/try-run-hook (hook)
"Run HOOK (a hook function), but handle errors better, to make debugging
issues easier.
Meant to be used with `run-hook-wrapped'."
(message "Running hook: %s" hook)
(condition-case e
(funcall hook)
((debug error)
(signal 'n-hook-error (list hook e))))
;; return nil so `run-hook-wrapped' won't short circuit
nil)
;; File+dir local variables are initialized after the major mode and its hooks
;; have run. If you want hook functions to be aware of these customizations, add
;; them to MODE-local-vars-hook instead.
(defun nasy/run-local-var-hooks-h ()
"Run MODE-local-vars-hook after local variables are initialized."
(run-hook-wrapped (intern-soft (format "%s-local-vars-hook" major-mode))
#'nasy/try-run-hook))
(add-hook 'hack-local-variables-hook #'nasy/run-local-var-hooks-h)
;; If the user has disabled `enable-local-variables', then
;; `hack-local-variables-hook' is never triggered, so we trigger it at the end
;; of `after-change-major-mode-hook':
(defun nasy/run-local-var-hooks-if-necessary-h ()
"Run `nasy/run-local-var-hooks-h' if `enable-local-variables' is disabled."
(unless enable-local-variables
(nasy/run-local-var-hooks-h)))
(add-hook 'after-change-major-mode-hook
#'nasy/run-local-var-hooks-if-necessary-h
'append)
(defun nasy--resolve-hook-forms (hooks)
"Converts a list of modes into a list of hook symbols.
If a mode is quoted, it is left as is. If the entire HOOKS list is quoted, the
list is returned as-is."
(declare (pure t) (side-effect-free t))
(let ((hook-list (nasy-enlist (nasy-unquote hooks))))
(if (eq (car-safe hooks) 'quote)
hook-list
(cl-loop for hook in hook-list
if (eq (car-safe hook) 'quote)
collect (cadr hook)
else collect (intern (format "%s-hook" (symbol-name hook)))))))
(defun radian-reload-init ()
"Reload init.el."
(interactive)
(straight-transaction
(straight-mark-transaction-as-init)
(message "Reloading init.el...")
(load user-init-file nil 'nomessage)
(message "Reloading init.el... done.")))
(defun radian-eval-buffer ()
"Evaluate the current buffer as Elisp code."
(interactive)
(message "Evaluating %s..." (buffer-name))
(straight-transaction
(if (null buffer-file-name)
(eval-buffer)
(when (string= buffer-file-name user-init-file)
(straight-mark-transaction-as-init))
(load-file buffer-file-name)))
(message "Evaluating %s... done." (buffer-name)))
(defun nasy:insert-current-date ()
"Insert current date."
(interactive)
(insert (shell-command-to-string "echo -n $(date +'%b %d, %Y')")))
(defun nasy:insert-current-filename ()
"Insert current buffer filename."
(interactive)
(insert (file-relative-name buffer-file-name)))
(defun posframe-poshandler-frame-top-center (info)
(cons (/ (- (plist-get info :parent-frame-width)
(plist-get info :posframe-width))
2)
(round (* 0.02 (x-display-pixel-height)))))
;; Borrow from https://github.com/raxod502/radian/blob/bf23a07418b3d72a300b21dcdf9cb521423d9681/emacs/radian.el#L30-L49
(defmacro nasy/protect-macros (&rest body)
"Eval BODY, protecting macros from incorrect expansion.
This macro should be used in the following situation:
Some form is being evaluated, and this form contains as a
sub-form some code that will not be evaluated immediately, but
will be evaluated later. The code uses a macro that is not
defined at the time the top-level form is evaluated, but will be
defined by time the sub-form's code is evaluated. This macro
handles its arguments in some way other than evaluating them
directly. And finally, one of the arguments of this macro could
be interpreted itself as a macro invocation, and expanding the
invocation would break the evaluation of the outer macro.
You might think this situation is such an edge case that it would
never happen, but you'd be wrong, unfortunately. In such a
situation, you must wrap at least the outer macro in this form,
but can wrap at any higher level up to the top-level form."
(declare (indent 0))
`(eval '(progn ,@body)))
(if (fboundp 'with-eval-after-load)
(defalias 'after-x 'with-eval-after-load)
(defmacro after-x (feature &rest body)
"Eval BODY afetr FEATURE have loaded."
(declare (indent defun))
`(eval-after-load ,feature
'(progn ,@body))))
(defmacro λ! (&rest body)
"A shortcut for inline interactive lambdas."
(declare (doc-string 1))
`(lambda () (interactive) ,@body))
(defalias 'lambda! 'λ!)
(defmacro after! (package &rest body)
"Evaluate BODY after PACKAGE have loaded.
PACKAGE is a symbol or list of them. These are package names, not modes,
functions or variables. It can be:
- An unquoted package symbol (the name of a package)
(after! helm BODY...)
- An unquoted list of package symbols (i.e. BODY is evaluated once both magit
and git-gutter have loaded)
(after! (magit git-gutter) BODY...)
- An unquoted, nested list of compound package lists, using any combination of
:or/:any and :and/:all
(after! (:or package-a package-b ...) BODY...)
(after! (:and package-a package-b ...) BODY...)
(after! (:and package-a (:or package-b package-c) ...) BODY...)
Without :or/:any/:and/:all, :and/:all are implied.
This is a wrapper around `eval-after-load' that:
1. Suppresses warnings for disabled packages at compile-time
2. No-ops for package that are disabled by the user (via `package!')
3. Supports compound package statements (see below)
4. Prevents eager expansion pulling in autoloaded macros all at once"
(declare (indent defun) (debug t))
(if (symbolp package)
(list (if (or (not (bound-and-true-p byte-compile-current-file))
(require package nil 'noerror))
#'progn
#'with-no-warnings)
(let ((body (macroexp-progn body)))
`(if (featurep ',package)
,body
;; We intentionally avoid `with-eval-after-load' to prevent
;; eager macro expansion from pulling (or failing to pull) in
;; autoloaded macros/packages.
(eval-after-load ',package ',body))))
(let ((p (car package)))
(cond ((not (keywordp p))
`(after! (:and ,@package) ,@body))
((memq p '(:or :any))
(macroexp-progn
(cl-loop for next in (cdr package)
collect `(after! ,next ,@body))))
((memq p '(:and :all))
(dolist (next (cdr package))
(setq body `((after! ,next ,@body))))
(car body))))))
(defmacro prependq! (sym &rest lists)
"Prepend LISTS to SYM in place."
`(setq ,sym (append ,@lists ,sym)))
(defmacro appendq! (sym &rest lists)
"Append LISTS to SYM in place."
`(setq ,sym (append ,sym ,@lists)))
(defmacro delq! (elt list &optional fetcher)
"`delq' ELT from LIST in-place.
If FETCHER is a function, ELT is used as the key in LIST (an alist)."
`(setq ,list
(delq ,(if fetcher
`(funcall ,fetcher ,elt ,list)
elt)
,list)))
(defmacro defadvice! (symbol arglist &optional docstring &rest body)
"Define an advice called SYMBOL and add it to PLACES.
ARGLIST is as in `defun'. WHERE is a keyword as passed to `advice-add', and
PLACE is the function to which to add the advice, like in `advice-add'.
DOCSTRING and BODY are as in `defun'.
\(fn SYMBOL ARGLIST &optional DOCSTRING &rest [WHERE PLACES...] BODY\)"
(declare (doc-string 3) (indent defun))
(unless (stringp docstring)
(push docstring body)
(setq docstring nil))
(let (where-alist)
(while (keywordp (car body))
(push `(cons ,(pop body) (nasy-enlist ,(pop body)))
where-alist))
`(progn
(defun ,symbol ,arglist ,docstring ,@body)
(dolist (targets (list ,@(nreverse where-alist)))
(dolist (target (cdr targets))
(advice-add target (car targets) #',symbol))))))
(defmacro undefadvice! (symbol _arglist &optional docstring &rest body)
"Undefine an advice called SYMBOL.
This has the same signature as `defadvice!' an exists as an easy undefiner when
testing advice (when combined with `rotate-text').
\(fn SYMBOL ARGLIST &optional DOCSTRING &rest [WHERE PLACES...] BODY\)"
(declare (doc-string 3) (indent defun))
(let (where-alist)
(unless (stringp docstring)
(push docstring body))
(while (keywordp (car body))
(push `(cons ,(pop body) (nasy-enlist ,(pop body)))
where-alist))
`(dolist (targets (list ,@(nreverse where-alist)))
(dolist (target (cdr targets))
(advice-remove target #',symbol)))))
(defmacro add-hook! (hooks &rest rest)
"A convenience macro for adding N functions to M hooks.
If N and M = 1, there's no benefit to using this macro over `add-hook'.
This macro accepts, in order:
1. The mode(s) or hook(s) to add to. This is either an unquoted mode, an
unquoted list of modes, a quoted hook variable or a quoted list of hook
variables.
2. Optional properties :local and/or :append, which will make the hook
buffer-local or append to the list of hooks (respectively),
3. The function(s) to be added: this can be one function, a quoted list
thereof, a list of `defun's, or body forms (implicitly wrapped in a
lambda).
\(fn HOOKS [:append :local] FUNCTIONS)"
(declare (indent (lambda (indent-point state)
(goto-char indent-point)
(when (looking-at-p "\\s-*(")
(lisp-indent-defform state indent-point))))
(debug t))
(let* ((hook-forms (nasy--resolve-hook-forms hooks))
(func-forms ())
(defn-forms ())
append-p
local-p
remove-p
forms)
(while (keywordp (car rest))
(pcase (pop rest)
(:append (setq append-p t))
(:local (setq local-p t))
(:remove (setq remove-p t))))
(let ((first (car-safe (car rest))))
(cond ((null first)
(setq func-forms rest))
((eq first 'defun)
(setq func-forms (mapcar #'cadr rest)
defn-forms rest))
((memq first '(quote function))
(setq func-forms
(if (cdr rest)
(mapcar #'nasy-unquote rest)
(nasy-enlist (nasy-unquote (car rest))))))
((setq func-forms (list `(lambda (&rest _) ,@rest)))))
(dolist (hook hook-forms)
(dolist (func func-forms)
(push (if remove-p
`(remove-hook ',hook #',func ,local-p)
`(add-hook ',hook #',func ,append-p ,local-p))
forms)))
(macroexp-progn
(append defn-forms
(if append-p
(nreverse forms)
forms))))))
(defmacro remove-hook! (hooks &rest rest)
"A convenience macro for removing N functions from M hooks.
Takes the same arguments as `add-hook!'.
If N and M = 1, there's no benefit to using this macro over `remove-hook'.
\(fn HOOKS [:append :local] FUNCTIONS)"
(declare (indent defun) (debug t))
`(add-hook! ,hooks :remove ,@rest))
;;;###autoload
(defun nasy/lsp-format-region-or-buffer ()
"Format the buffer (or selection) with LSP."
(interactive)
(unless (bound-and-true-p lsp-mode)
(user-error "Not in an LSP buffer"))
(call-interactively
(if (nasy/region-active-p)
#'lsp-format-region
#'lsp-format-buffer)))
;;;###autoload
(defvar nasy/real-buffer-functions
'(nasy/dired-buffer-p)
"A list of predicate functions run to determine if a buffer is real, unlike
`nasy/unreal-buffer-functions'. They are passed one argument: the buffer to be
tested.
Should any of its function returns non-nil, the rest of the functions are
ignored and the buffer is considered real.
See `nasy/real-buffer-p' for more information.")
;;;###autoload
(defvar nasy/unreal-buffer-functions
'(minibufferp nasy/special-buffer-p nasy/non-file-visiting-buffer-p)
"A list of predicate functions run to determine if a buffer is *not* real,
unlike `nasy/real-buffer-functions'. They are passed one argument: the buffer to
be tested.
Should any of these functions return non-nil, the rest of the functions are
ignored and the buffer is considered unreal.
See `nasy/real-buffer-p' for more information.")
;;;###autoload
(defvar-local nasy/real-buffer-p nil
"If non-nil, this buffer should be considered real no matter what. See
`nasy/real-buffer-p' for more information.")
;;;###autoload
(defvar nasy/fallback-buffer-name "*scratch*"
"The name of the buffer to fall back to if no other buffers exist (will create
it if it doesn't exist).")
;;
;; Functions
;;;###autoload
(defun nasy/buffer-frame-predicate (buf)
"To be used as the default frame buffer-predicate parameter. Returns nil if
BUF should be skipped over by functions like `next-buffer' and `other-buffer'."
(or (nasy/real-buffer-p buf)
(eq buf (nasy/fallback-buffer))))
;;;###autoload
(defun nasy/fallback-buffer ()
"Returns the fallback buffer, creating it if necessary. By default this is the
scratch buffer. See `nasy/fallback-buffer-name' to change this."
(let (buffer-list-update-hook)
(get-buffer-create nasy/fallback-buffer-name)))
;;;###autoload
(defalias 'nasy/buffer-list #'buffer-list)
;;;###autoload
(defun nasy/project-buffer-list (&optional project)
"Return a list of buffers belonging to the specified PROJECT.
If PROJECT is nil, default to the current project.
If no project is active, return all buffers."
(let ((buffers (nasy/buffer-list)))
(if-let* ((project-root
(if project (expand-file-name project)
(nasy/project-root))))
(cl-loop for buf in buffers
if (projectile-project-buffer-p buf project-root)
collect buf)
buffers)))
;;;###autoload
(defun nasy/open-projects ()
"Return a list of projects with open buffers."
(cl-loop with projects = (make-hash-table :test 'equal :size 8)
for buffer in (nasy/buffer-list)
if (buffer-live-p buffer)
if (nasy/real-buffer-p buffer)
if (with-current-buffer buffer (nasy/project-root))
do (puthash (abbreviate-file-name it) t projects)
finally return (hash-table-keys projects)))
;;;###autoload
(defun nasy/dired-buffer-p (buf)
"Returns non-nil if BUF is a dired buffer."
(with-current-buffer buf (derived-mode-p 'dired-mode)))
;;;###autoload
(defun nasy/special-buffer-p (buf)
"Returns non-nil if BUF's name starts and ends with an *."
(equal (substring (buffer-name buf) 0 1) "*"))
;;;###autoload
(defun nasy/temp-buffer-p (buf)
"Returns non-nil if BUF is temporary."
(equal (substring (buffer-name buf) 0 1) " "))
;;;###autoload
(defun nasy/visible-buffer-p (buf)
"Return non-nil if BUF is visible."
(get-buffer-window buf))
;;;###autoload
(defun nasy/buried-buffer-p (buf)
"Return non-nil if BUF is not visible."
(not (nasy/visible-buffer-p buf)))
;;;###autoload
(defun nasy/non-file-visiting-buffer-p (buf)
"Returns non-nil if BUF does not have a value for `buffer-file-name'."
(not (buffer-file-name buf)))
;;;###autoload
(defun nasy/real-buffer-list (&optional buffer-list)
"Return a list of buffers that satify `nasy/real-buffer-p'."
(cl-remove-if-not #'nasy/real-buffer-p (or buffer-list (nasy/buffer-list))))
;;;###autoload
(defun nasy/real-buffer-p (buffer-or-name)
"Returns t if BUFFER-OR-NAME is a 'real' buffer.
A real buffer is a useful buffer; a first class citizen in Doom. Real ones
should get special treatment, because we will be spending most of our time in
them. Unreal ones should be low-profile and easy to cast aside, so we can focus
on real ones.
The exact criteria for a real buffer is:
1. A non-nil value for the buffer-local value of the `nasy/real-buffer-p'
variable OR
2. Any function in `nasy/real-buffer-functions' returns non-nil OR
3. None of the functions in `nasy/unreal-buffer-functions' must return
non-nil.
If BUFFER-OR-NAME is omitted or nil, the current buffer is tested."
(or (bufferp buffer-or-name)
(stringp buffer-or-name)
(signal 'wrong-type-argument (list '(bufferp stringp) buffer-or-name)))
(when-let (buf (get-buffer buffer-or-name))
(and (buffer-live-p buf)
(not (nasy/temp-buffer-p buf))
(or (buffer-local-value 'nasy/real-buffer-p buf)
(run-hook-with-args-until-success 'nasy/real-buffer-functions buf)
(not (run-hook-with-args-until-success 'nasy/unreal-buffer-functions buf))))))
;;;###autoload
(defun nasy/unreal-buffer-p (buffer-or-name)
"Return t if BUFFER-OR-NAME is an 'unreal' buffer.
See `nasy/real-buffer-p' for details on what that means."
(not (nasy/real-buffer-p buffer-or-name)))
;;;###autoload
(defun nasy/buffers-in-mode (modes &optional buffer-list derived-p)
"Return a list of buffers whose `major-mode' is `eq' to MODE(S).
If DERIVED-P, test with `derived-mode-p', otherwise use `eq'."
(let ((modes (nasy/enlist modes)))
(cl-remove-if-not (if derived-p
(lambda (buf)
(with-current-buffer buf
(apply #'derived-mode-p modes)))
(lambda (buf)
(memq (buffer-local-value 'major-mode buf) modes)))
(or buffer-list (nasy/buffer-list)))))
;;;###autoload
(defun nasy/visible-windows (&optional window-list)
"Return a list of the visible, non-popup (dedicated) windows."
(cl-loop for window in (or window-list (window-list))
when (or (window-parameter window 'visible)
(not (window-dedicated-p window)))
collect window))
;;;###autoload
(defun nasy/visible-buffers (&optional buffer-list)
"Return a list of visible buffers (i.e. not buried)."
(if buffer-list
(cl-remove-if-not #'get-buffer-window buffer-list)
(delete-dups (mapcar #'window-buffer (window-list)))))
;;;###autoload
(defun nasy/buried-buffers (&optional buffer-list)
"Get a list of buffers that are buried."
(cl-remove-if #'get-buffer-window (or buffer-list (nasy/buffer-list))))
;;;###autoload
(defun nasy/matching-buffers (pattern &optional buffer-list)
"Get a list of all buffers that match the regex PATTERN."
(cl-loop for buf in (or buffer-list (nasy/buffer-list))
when (string-match-p pattern (buffer-name buf))
collect buf))
;;;###autoload
(defun nasy/set-buffer-real (buffer flag)
"Forcibly mark BUFFER as FLAG (non-nil = real)."
(with-current-buffer buffer
(setq nasy/real-buffer-p flag)))
;;;###autoload
(defun nasy/kill-buffer-and-windows (buffer)
"Kill the buffer and delete all the windows it's displayed in."
(dolist (window (get-buffer-window-list buffer))
(unless (one-window-p t)
(delete-window window)))
(kill-buffer buffer))
;;;###autoload
(defun nasy/fixup-windows (windows)
"Ensure that each of WINDOWS is showing a real buffer or the fallback buffer."
(dolist (window windows)
(with-selected-window window
(when (nasy/unreal-buffer-p (window-buffer))
(previous-buffer)
(when (nasy/unreal-buffer-p (window-buffer))
(switch-to-buffer (nasy/fallback-buffer)))))))
;;;###autoload
(defun nasy/kill-buffer-fixup-windows (buffer)
"Kill the BUFFER and ensure all the windows it was displayed in have switched
to a real buffer or the fallback buffer."
(let ((windows (get-buffer-window-list buffer)))
(kill-buffer buffer)
(nasy/fixup-windows (cl-remove-if-not #'window-live-p windows))))
;;;###autoload
(defun nasy/kill-buffers-fixup-windows (buffers)
"Kill the BUFFERS and ensure all the windows they were displayed in have
switched to a real buffer or the fallback buffer."
(let ((seen-windows (make-hash-table :test 'eq :size 8)))
(dolist (buffer buffers)
(let ((windows (get-buffer-window-list buffer)))
(kill-buffer buffer)
(dolist (window (cl-remove-if-not #'window-live-p windows))
(puthash window t seen-windows))))
(nasy/fixup-windows (hash-table-keys seen-windows))))
;;;###autoload
(defun nasy/kill-matching-buffers (pattern &optional buffer-list)
"Kill all buffers (in current workspace OR in BUFFER-LIST) that match the
regex PATTERN. Returns the number of killed buffers."
(let ((buffers (nasy/matching-buffers pattern buffer-list)))
(dolist (buf buffers (length buffers))
(kill-buffer buf))))
;;
;; Hooks
;;;###autoload
(defun nasy/mark-buffer-as-real-h ()
"Hook function that marks the current buffer as real."
(nasy/set-buffer-real (current-buffer) t))
;;
;; Interactive commands
;;;###autoload
(defun nasy/kill-this-buffer-in-all-windows (buffer &optional dont-save)
"Kill BUFFER globally and ensure all windows previously showing this buffer
have switched to a real buffer or the fallback buffer.
If DONT-SAVE, don't prompt to save modified buffers (discarding their changes)."
(interactive
(list (current-buffer) current-prefix-arg))
(cl-assert (bufferp buffer) t)
(when (and (buffer-modified-p buffer) dont-save)
(with-current-buffer buffer
(set-buffer-modified-p nil)))
(nasy/kill-buffer-fixup-windows buffer))
(defun nasy/message-or-count (interactive message count)
(if interactive
(message message count)
count))
;;;###autoload
(defun nasy/kill-all-buffers (&optional buffer-list interactive)
"Kill all buffers and closes their windows.
If the prefix arg is passed, doesn't close windows and only kill buffers that
belong to the current project."
(interactive
(list (if current-prefix-arg
(nasy/project-buffer-list)
(nasy/buffer-list))
t))
(if (null buffer-list)
(message "No buffers to kill")
(save-some-buffers)
(delete-other-windows)
(when (memq (current-buffer) buffer-list)
(switch-to-buffer (nasy/fallback-buffer)))
(mapc #'kill-buffer buffer-list)
(nasy/message-or-count
interactive "Killed %d buffers"
(- (length buffer-list)
(length (cl-remove-if-not #'buffer-live-p buffer-list))))))
;;;###autoload
(defun nasy/kill-other-buffers (&optional buffer-list interactive)
"Kill all other buffers (besides the current one).
If the prefix arg is passed, kill only buffers that belong to the current
project."
(interactive
(list (delq (current-buffer)
(if current-prefix-arg
(nasy/project-buffer-list)
(nasy/buffer-list)))
t))
(mapc #'nasy/kill-buffer-and-windows buffer-list)
(nasy/message-or-count
interactive "Killed %d other buffers"
(- (length buffer-list)
(length (cl-remove-if-not #'buffer-live-p buffer-list)))))
;;;###autoload
(defun nasy/kill-matching-buffers (pattern &optional buffer-list interactive)
"Kill buffers that match PATTERN in BUFFER-LIST.
If the prefix arg is passed, only kill matching buffers in the current project."
(interactive
(list (read-regexp "Buffer pattern: ")
(if current-prefix-arg
(nasy/project-buffer-list)
(nasy/buffer-list))
t))
(nasy/kill-matching-buffers pattern buffer-list)
(when interactive
(message "Killed %d buffer(s)"
(- (length buffer-list)
(length (cl-remove-if-not #'buffer-live-p buffer-list))))))
;;;###autoload
(defun nasy/kill-buried-buffers (&optional buffer-list interactive)
"Kill buffers that are buried.
If PROJECT-P (universal argument), only kill buried buffers belonging to the
current project."
(interactive
(list (nasy/buried-buffers
(if current-prefix-arg (nasy/project-buffer-list)))
t))
(mapc #'kill-buffer buffer-list)
(nasy/message-or-count
interactive "Killed %d buried buffers"
(- (length buffer-list)
(length (cl-remove-if-not #'buffer-live-p buffer-list)))))
;;;###autoload
(defun nasy/kill-project-buffers (project &optional interactive)
"Kill buffers for the specified PROJECT."
(interactive
(list (if-let (open-projects (nasy/open-projects))
(completing-read
"Kill buffers for project: " open-projects
nil t nil nil
(if-let* ((project-root (nasy/project-root))
(project-root (abbreviate-file-name project-root))
((member project-root open-projects)))
project-root))
(message "No projects are open!")
nil)
t))
(when project
(let ((buffer-list (nasy/project-buffer-list project)))
(nasy/kill-buffers-fixup-windows buffer-list)
(nasy/message-or-count
interactive "Killed %d project buffers"
(- (length buffer-list)
(length (cl-remove-if-not #'buffer-live-p buffer-list)))))))
;;
;;; Library
;;;###autoload
(defun nasy/project-p (&optional dir)
"Return t if DIR (defaults to `default-directory') is a valid project."
(and (nasy/project-root dir)
t))
;;;###autoload
(defun nasy/project-root (&optional dir)
"Return the project root of DIR (defaults to `default-directory').
Returns nil if not in a project."
(let ((projectile-project-root (unless dir projectile-project-root))
projectile-require-project-root)
(projectile-project-root dir)))
;;;###autoload
(defun nasy/delete-backward-word (arg)
"Like `backward-kill-word', but doesn't affect the kill-ring."
(interactive "p")
(let (kill-ring)
(backward-kill-word arg)))
;;;###autoload
(defun nasy/region-active-p ()
"Return non-nil if selection is active."
(declare (side-effect-free t))
(use-region-p))
;;;###autoload
(defun nasy/region-beginning ()
"Return beginning position of selection."
(declare (side-effect-free t))
(region-beginning))
;;;###autoload
(defun nasy/region-end ()
"Return end position of selection."
(declare (side-effect-free t))
(region-end))
;;;###autoload
(defun nasy/thing-at-point-or-region (&optional thing prompt)
"Grab the current selection, THING at point, or xref identifier at point.
Returns THING if it is a string. Otherwise, if nothing is found at point and
PROMPT is non-nil, prompt for a string (if PROMPT is a string it'll be used as
the prompting string). Returns nil if all else fails.
NOTE: Don't use THING for grabbing symbol-at-point. The xref fallback is smarter
in some cases."
(declare (side-effect-free t))
(cond ((stringp thing)
thing)
((nasy/region-active-p)
(buffer-substring-no-properties
(nasy/region-beginning)
(nasy/region-end)))
(thing
(thing-at-point thing t))
((require 'xref nil t)
;; A little smarter than using `symbol-at-point', though in most cases,
;; xref ends up using `symbol-at-point' anyway.
(xref-backend-identifier-at-point (xref-find-backend)))
(prompt
(read-string (if (stringp prompt) prompt "")))))
;;;###autoload
(defalias 'default/newline #'newline)
;;;###autoload
(defun default/newline-above ()
"Insert an indented new line before the current one."
(interactive)
(beginning-of-line)
(save-excursion (newline))
(indent-according-to-mode))
;;;###autoload
(defun default/newline-below ()
"Insert an indented new line after the current one."
(interactive)
(end-of-line)
(newline-and-indent))
;;;###autoload
(defun default/yank-pop ()
"Interactively select what text to insert from the kill ring."
(interactive)
(call-interactively
(cond ((fboundp 'counsel-yank-pop) #'counsel-yank-pop)
((fboundp 'helm-show-kill-ring) #'helm-show-kill-ring)
((error "No kill-ring search backend available. Enable ivy or helm!")))))
;;;###autoload
(defun default/yank-buffer-filename ()
"Copy the current buffer's path to the kill ring."
(interactive)
(if-let* ((filename (or buffer-file-name (bound-and-true-p list-buffers-directory))))
(message (kill-new (abbreviate-file-name filename)))
(error "Couldn't find filename in current buffer")))
;;;###autoload
(defun default/insert-file-path (arg)
"Insert the file name (absolute path if prefix ARG).
If `buffer-file-name' isn't set, uses `default-directory'."
(interactive "P")
(let ((path (or buffer-file-name default-directory)))
(insert
(if arg
(abbreviate-file-name path)
(file-name-nondirectory path)))))
;;;###autoload
(defun default/newline-indent-and-continue-comments-a ()
"A replacement for `newline-and-indent'.
Continues comments if executed from a commented line, with special support for
languages with weak native comment continuation support (like C-family
languages)."
(interactive)
(if (and (sp-point-in-comment)
comment-line-break-function)
(funcall comment-line-break-function nil)
(delete-horizontal-space t)
(newline nil t)
(indent-according-to-mode)))
(setq straight-recipes-gnu-elpa-use-mirror t
straight-repository-branch "develop"
straight-vc-git-default-clone-depth 1
straight-enable-use-package-integration nil
straight-check-for-modifications '(find-when-checking))
(defvar bootstrap-version)
(let ((bootstrap-file
(expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
(bootstrap-version 5))
(unless (file-exists-p bootstrap-file)
(with-current-buffer
(url-retrieve-synchronously
"https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
'silent 'inhibit-cookies)
(goto-char (point-max))
(eval-print-last-sexp)))
(load bootstrap-file nil 'nomessage))
(straight-use-package 'use-package)
(defmacro use-feature (name &rest args)
"Like `use-package', but with `straight-use-package-by-default' disabled.
NAME and ARGS are as in `use-package'."
(declare (indent defun))
`(use-package ,name
,@args))
;; Feature `straight-x' from package `straight' provides
;; experimental/unstable extensions to straight.el which are not yet
;; ready for official inclusion.
(use-feature straight-x
;; Add an autoload for this extremely useful command.
:commands (straight-x-fetch-all))
(defmacro nasy/s-u-p (&rest packages)
"Straight use multiple PACKAGES."
(unless (null packages)
(let* ((package (car packages))
(rest (cdr packages))
(q-p (boundp package)))
`(progn
(if ,q-p
(straight-use-package ,package)
(straight-use-package ',package))
(nasy/s-u-p ,@rest)))))
(straight-use-package 'gcmh)
(use-package gcmh
:demand t
:init
(setq gcmh-verbose t
gcmh-lows-cons-threshold #x800000
gcmh-high-cons-threshold most-positive-fixnum
gcmh-idle-delay 3600)
:config
(gcmh-mode))
(straight-use-package 'benchmark-init)
(use-package benchmark-init
:demand t
:hook ((after-init . benchmark-init/deactivate)))
(straight-use-package 'no-littering)
(require 'no-littering)
(defvar nasy/config-before-hook nil
"Hooks to run config functions before load custom.el.")
(defvar nasy/config-after-hook nil
"Hooks to run config functions after.")
(add-hook 'nasy/config-after-hook
#'(lambda () (message "Hi~ Hope you have fun with this config.")))
(defgroup nasy nil
"Nasy Emacs Custom Configurations."
:group 'emacs)
(defcustom lisp-modes-hooks '(common-lisp-mode-hook
emacs-lisp-mode-hook
lisp-mode-hook
racket-mode-hook
scheme-mode-hook)
"List of lisp-related modes hooks."
:type '(repeat symbol)
:group 'nasy)
(defcustom *clangd* (executable-find "clangd")
"Clangd path. If nil, will not use clangd."
:group 'nasy
:type 'string)
(defcustom *ccls* (executable-find "ccls") ;; macOS
"Ccls path. If nil, will not use ccls."
:group 'nasy
:type 'string)
(defvar *eldoc-use* 'eldoc-overlay
"Use eldoc-box, eldoc-overlay or not.
nil means use default.
box means use eldoc-box.
overlay means use eldoc-overlay.")
(defvar *flycheck-inline* t
"Use flycheck-inline or not.")
(defcustom *nix* nil
"Nix path. If nil, will not use nix."
:group 'nasy
:type 'string)
(defcustom *rust* (or (executable-find "rustc")
(executable-find "cargo")
(executable-find "rustup"))
"The Rust path. If nil, will not use Rust."
:group 'nasy
:type 'string)
(defcustom *rls* (executable-find "rls")
"The rls path. If nil, will not use rls."
:group 'nasy
:type 'string)
(defcustom *highlight-indent-guides* t
"Whether to use highlight-indent-guides or not."
:group 'nasy
:type 'boolean)
(defcustom *debug* t
"Whether to use debug or not."
:group 'nasy
:type 'boolean)
(defcustom *server* t
"Whether to use server or not."
:group 'nasy
:type 'boolean)
(defcustom *intero* t
"Whether to use intero or not."
:group 'nasy
:type 'boolean)
(defcustom *struct-hs* (executable-find "structured-haskell-mode")
"The structured-haskell-mode path. If nil, will not use structured-haskell-mode."
:group 'nasy
:type 'string)
(defcustom *pyblack* nil
"Whether to use black to reformat python or not."
:group 'nasy
:type 'boolean)
(defcustom *py-module* 'elpy
"Select py module."
:group 'nasy
:type '(choice (const :tag "Use elpy" elpy)
(const :tag "Use pyls" pyls)
(const :tag "Use mspyls" mspyls)))
(defcustom *c-box* t
"Whether to use company box or not."
:group 'nasy
:type 'boolean)
(defcustom *ivy/helm/selectrum* 'selectrum
"Use ivy, helm or selectrum?"
:group 'nasy
:type '(choice (const :tag "Use ivy" ivy)
(const :tag "Use helm" helm)
(const :tag "Use selectrum" selectrum)))
(defcustom *ivy-or-helm* 'ivy
"Use ivy or helm?"
:group 'nasy
:type '(choice (const :tag "Use ivy" ivy)
(const :tag "Use helm" helm)))
(defcustom *ivy-posframe* nil