/
configure-lisp.el
1181 lines (1028 loc) · 49 KB
/
configure-lisp.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
;;; -*- lexical-binding: t -*-
(require 'ace-link)
(require 'slime)
(require 'slime-autoloads)
(setq inferior-lisp-program (getenv "LISP_BINARY"))
(add-to-list 'auto-mode-alist '("\\.cl\\'" . common-lisp-mode))
;;* slime-contribs
(setq slime-contribs '(slime-repl
slime-autodoc
slime-asdf
slime-editing-commands
slime-fancy-inspector
slime-fancy-trace
;; slime-fuzzy
;; slime-mdot-fu
slime-macrostep
slime-presentations
slime-package-fu
;; slime-scratch
slime-references
;; slime-fontifying-fu
slime-trace-dialog
slime-indentation
;; slime-cl-indent
;; slime-uncompiled-fringe
slime-tramp
slime-xref-browser
slime-c-p-c
inferior-slime
;; slime-mrepl
))
(slime-setup slime-contribs)
;;* uiop support for slime-package-fu
(setq slime-defpackage-regexp
(rx line-start "("
(or (and (? (or "cl:" "common-lisp:")) "defpackage")
(and (? (or "uiop:" "uiop/package:" "package:")) "define-package"))
symbol-end
(* space)))
;;* slime-search-buffer-package fix
(defun slime-search-buffer-package+ ()
"Same as `slime-search-buffer-package', but handle the case
when cursor is directly inside the in-package form."
(save-excursion (beginning-of-line)
(slime-search-buffer-package)))
(setq slime-find-buffer-package-function #'slime-search-buffer-package+)
;;* Paredit
(autoload 'enable-paredit-mode "paredit" "Turn on pseudo-structural editing of Lisp code." t)
(add-hook 'emacs-lisp-mode-hook #'enable-paredit-mode)
(add-hook 'eval-expression-minibuffer-setup-hook #'enable-paredit-mode)
(add-hook 'ielm-mode-hook #'enable-paredit-mode)
(add-hook 'lisp-mode-hook #'enable-paredit-mode)
(add-hook 'slime-repl-mode-hook #'enable-paredit-mode)
(add-hook 'lisp-interaction-mode-hook #'enable-paredit-mode)
(add-hook 'scheme-mode-hook #'enable-paredit-mode)
(define-key paredit-mode-map (kbd "C-}") nil)
(define-key paredit-mode-map (kbd "C-{") nil)
;;* skip parens when reading symbol-at-point
(defun symbol-at-point--skip-parens (orig-fn &rest args)
"Handle cases like |(((symbol or symbol)))| where | is the cursor position."
(save-excursion
(skip-chars-forward "(")
(skip-chars-backward ")")
(apply orig-fn args)))
(advice-add 'slime-symbol-at-point :around #'symbol-at-point--skip-parens)
(advice-add 'elisp-slime-nav--read-symbol-at-point :around #'symbol-at-point--skip-parens)
;;* Elisp
;;** indentation
;; It seems easier to use `common-lisp-indent-function' for stuff such as
;; labels & loop, and manually fix indent of elisp-specific forms(if, when-let, etc)
;; TODO: refactor this
;; This was a bad idea, better to just copy indentation for stuff like
;; loop from `common-lisp-indent-function'.
(setq lisp-indent-function 'common-lisp-indent-function)
(with-eval-after-load 'cl-indent
(labels ((%copy-indent (new old)
(put new 'common-lisp-indent-function
(get old 'common-lisp-indent-function))))
(%copy-indent 'cl-flet 'flet)
(%copy-indent 'flet 'flet)
(%copy-indent 'cl-labels 'labels)
(%copy-indent 'cl-defun 'defun)
(%copy-indent 'cl-defmacro 'defmacro)
(%copy-indent 'when-let 'when)
(%copy-indent 'when-let* 'when)
(%copy-indent 'eval-after-load 'when)
(%copy-indent 'letf 'let)
(%copy-indent 'letf* 'let)
(%copy-indent 'cl-letf 'let)
(%copy-indent 'cl-letf* 'let)
(%copy-indent 'while 'when)
(%copy-indent 'evil-define-key 'defun)
(%copy-indent 'avy-with 'when)
(%copy-indent 'with-ivy-window 'save-excursion)
(%copy-indent 'evil-define-command 'defun)
;; (put 'if 'common-lisp-indent-function 2)
(put 'if-let 'common-lisp-indent-function 2)
(put 'if-let* 'common-lisp-indent-function 2)))
;;** documentation
(defvar *elisp-documentation-last-symbol* nil
"Last symbol for which documentation was queried.")
(defun elisp-documentation (prompt &optional symbol)
"Show documentation for SYMBOL in the minibuffer.
If SYMBOL is not provided, use symbol-at-point.
With prefix arg prompt for symbol first.
When called second time consecutively, call `helpful-symbol' for SYMBOL."
(interactive "P")
(if (and (eq this-command last-command)
*elisp-documentation-last-symbol*
(fboundp 'helpful-symbol))
(helpful-symbol *elisp-documentation-last-symbol*)
(when-let* ((symbol (or symbol
(if prompt
(intern (completing-read "Show documentation for: "
obarray nil t nil nil
(when-let ((s (symbol-at-point)))
(symbol-name s))))
(symbol-at-point))))
(doc (if (or (functionp symbol)
(macrop symbol))
(documentation symbol)
(documentation-property symbol 'variable-documentation))))
(setq *elisp-documentation-last-symbol* symbol)
;; TODO: truncate docs that are more that a page long
(display-truncated-message (format "%s" doc)))))
(define-key emacs-lisp-mode-map (kbd "C-c C-d") 'elisp-documentation)
(define-key read-expression-map (kbd "C-c C-d") 'elisp-documentation)
(with-eval-after-load 'ielm
(define-key inferior-emacs-lisp-mode-map (kbd "C-c C-d") 'elisp-documentation))
;;** evaluation
(require 'eros)
(defun eros-eval-last-sexp-dwim ()
"Eval region if active, eval symbol-at-point if any,
else call eros-eval-last-sexp."
(interactive)
(if (region-active-p)
(let ((lines (count-lines (region-beginning) (region-end))))
(call-interactively #'eval-region)
(message "Evaluated %d line(s)." lines))
(if-let ((symbol-end (cdr (bounds-of-thing-at-point 'symbol))))
(eros--eval-overlay
(save-excursion
(goto-char symbol-end)
(call-interactively #'eval-last-sexp))
symbol-end)
(call-interactively #'eros-eval-last-sexp))))
(global-set-key [remap eval-last-sexp] #'eros-eval-last-sexp-dwim)
(global-set-key [remap eval-defun] #'eros-eval-defun)
;;** pp-eval
(defun pp-eval-dwim (&optional expression)
"Evaluate EXPRESSION and pretty-print its value. If region is active, copy it
as initial value when reading expression.
Also add the value to the front of the list in the variable `values'."
(interactive)
(let ((expression
(or expression
(read--expression
"Eval: "
(when (region-active-p)
(buffer-substring-no-properties
(region-beginning) (region-end)))))))
(message "Evaluating...")
(push (eval expression lexical-binding) values)
(pp-display-expression (car values) "*Pp Eval Output*")))
(global-set-key (kbd "C-x M-e") 'pp-eval-dwim)
(define-key emacs-lisp-mode-map (kbd "C-c M-e") 'pp-eval-dwim)
;;** elisp-slime-nav
(require 'elisp-slime-nav)
(define-key elisp-slime-nav-mode-map (kbd "C-c C-d") nil)
(dolist (hook '(emacs-lisp-mode-hook ielm-mode-hook eval-expression-minibuffer-setup-hook))
(add-hook hook 'elisp-slime-nav-mode))
;;** ielm toggle
(defun elisp-switch-to-ielm ()
"Switch to ielm buffer if exists or call `ielm'.
If there was an active region, insert it into repl."
(interactive)
(let ((selection (and (region-active-p)
(buffer-substring-no-properties (region-beginning) (region-end)))))
(if-let ((buf (get-buffer "*ielm*")))
(pop-to-buffer buf)
(ielm))
(when selection (insert selection))))
(define-key emacs-lisp-mode-map (kbd "C-c C-z") 'elisp-switch-to-ielm)
(with-eval-after-load 'ielm
(define-key inferior-emacs-lisp-mode-map (kbd "C-c C-z") 'quit-window))
;;* TODO: edebug-mode: make compatible with evil-mode, add hydra
;;** debugger-mode
(with-eval-after-load 'debug
(define-key debugger-mode-map "j" 'next-line)
(define-key debugger-mode-map "k" 'previous-line))
;;** hydras in imenu
(add-hook 'emacs-lisp-mode-hook #'hydra-add-imenu)
;;* slime hacks
;;** documentation
(defun slime-documentation-symbol (symbol-name)
(slime-eval `(swank:documentation-symbol ,symbol-name)))
(defun slime-documentation-minibuffer ()
"Display `swank:documentation-symbol' in the minibuffer"
(interactive)
(when-let* ((symbol-name (slime-read-symbol-name "Describe symbol: "))
(doc (slime-documentation-symbol symbol-name)))
(display-truncated-message doc)))
;; Slime documentation is opened in fundamental-mode by default. Force it to open
;; in help-mode instead. MAYBE: define slime-description-mode.
(defun slime-show-description-help-mode (string package)
"Like `slime-show-description', but set description buffer to Help-mode."
(let ((bufname (slime-buffer-name :description)))
(slime-with-popup-buffer (bufname :package package
:connection t
:select slime-description-autofocus
:mode 'help-mode)
(princ string)
(goto-char (point-min)))))
(advice-add 'slime-show-description :override #'slime-show-description-help-mode)
(map-put
display-buffer-alist
"*slime-description*"
'((display-buffer-in-side-window)
(window-width . fit-window-to-buffer)
(side . left)
(slot . 0))
#'string=)
;;*** hyperspec
;; local hyperspec
(load "/home/il/quicklisp/clhs-use-local.el" t)
;; browse hyperspec in eww
(defun hyperspec-lookup-advice (func &rest args)
(let ((browse-url-browser-function #'eww-browse-url))
(apply func args)))
(advice-add 'hyperspec-lookup :around #'hyperspec-lookup-advice)
(advice-add 'hyperspec-lookup-reader-macro :around #'hyperspec-lookup-advice)
(advice-add 'hyperspec-lookup-format :around #'hyperspec-lookup-advice)
;;** edit definition(M-.)
(defun slime--edit-definition-ivy (&optional symbol-name where)
"Adapted from `slime-edit-definition-cont'. Use `ivy' to select a candidate if multiple."
(unless symbol-name
(setq symbol-name (slime-read-symbol-name "Edit definition of: ")))
(or (run-hook-with-args-until-success 'slime-edit-definition-hooks symbol-name where)
(let ((xrefs (slime-find-definitions symbol-name)))
(cl-destructuring-bind (same-loc file-alist) (slime-analyze-xrefs xrefs)
(cond ((null xrefs)
(error "No known definition for: %s (in %s)" symbol-name (slime-current-package)))
(same-loc
(slime-push-definition-stack)
(slime-pop-to-location (slime-xref.location (car xrefs)) where))
;; ((:error "..."))
((slime-length= xrefs 1)
(error "%s" (second (slime-xref.location (car xrefs)))))
(t
(let* ((items (mapcar (lambda (xref)
(let* ((spec (downcase
(replace-regexp-in-string "[\n ]+" " " (slime-xref.dspec xref))))
(location (slime-xref.location xref))
(file (second (assoc :file (cdr location))))
(line (line-number-at-pos (second (assoc :position (cdr location))))))
(and spec file line location (list spec file line location))))
xrefs))
(sorted-items (sort (remove nil items)
(lambda (i1 i2)
(if (string= (second i1) (second i2))
(< (third i1) (third i2))
(string< (second i1) (second i2)))))))
(ivy-read "Edit definition of: "
sorted-items
:action (lambda (item)
(slime-push-definition-stack)
(slime-pop-to-location (fourth item) where))))))))))
(defun slime-edit-definition-ivy (arg)
"`slime-edit-definition' but use `ivy' to select a candidate.
With negative prefix arg call original `slime-edit-definition'."
(interactive "p")
(if (minusp arg)
(call-interactively #'slime-edit-definition)
(slime--edit-definition-ivy nil nil)))
(defun slime-edit-definition-other-window-ivy (arg)
"`slime-edit-definition-ivy' but open result in other window."
(interactive "p")
(if (minusp arg)
(call-interactively #'slime-edit-definition-other-window)
(slime--edit-definition-ivy nil 'window)))
(defun slime-edit-definition-other-frame-ivy (arg)
"`slime-edit-definition-ivy' but open result in other frame."
(interactive "p")
(if (minusp arg)
(call-interactively #'slime-edit-definition-other-frame)
(slime--edit-definition-ivy nil 'frame)))
(defun slime-kill-package-name ()
(interactive)
(let ((package (slime-pretty-package-name (slime-current-package))))
(message package)
(kill-new package)))
(defun slime-repl-set-package--push-package (&rest args)
;; TODO: check if this works properly for multiple swank connections
(let ((package (slime-lisp-package)))
(pushnew package slime-repl-package-stack)
(pushnew package slime-read-package-name-history)))
(advice-add 'slime-repl-set-package :after #'slime-repl-set-package--push-package)
(ivy-enable-calling-for-func #'slime-edit-definition-ivy)
;;** completion
;; (defvar *slime-internal-symbols* nil)
;; (defvar *slime-external-symbols* nil)
;;*** WIP completion for package names
;; TODO: use swank:shortest-package-nickname for nicknames
(defvar *slime-all-packages* nil)
(defun slime-all-packages (&optional update)
(if (or (null *slime-all-packages*) update)
(setq *slime-all-packages*
(slime-eval
`(cl:loop
for p in (cl:list-all-packages)
with k = (cl:find-package :keyword)
unless (cl:eq p k)
collect (cl:cons (cl:package-name p)
(cl:package-nicknames p)))))
*slime-all-packages*))
(defun slime-complete-package-name-exit-func (str status)
(insert ":"))
(defun slime-package-name (&optional prefix)
(when-let ((packages (slime-all-packages t)))
(if prefix
(loop for p in packages
when (string-prefix-p prefix (car p))
collect p)
packages)))
(defun slime-complete-package-name ()
(let ((end (point))
(beg (slime-symbol-start-pos)))
(list beg end (completion-table-dynamic #'slime-package-name)
:exclusive 'no
:exit-function #'slime-complete-package-name-exit-func)))
;;*** keyword args
(defun slime-operator-keyword-args (&optional prefix)
"Return list of keyword args for operator before point."
(when-let* ((op (slime-operator-before-point))
(args (slime-eval
`(swank:operator-arglist ,op
,(slime-current-package)))))
(when prefix
(setq prefix (string-trim-left prefix ":")))
(loop for arg in (cdr (member '&key (read args)))
until (= ?& (aref (symbol-name arg) 0))
for name = (symbol-name arg)
when (or (null prefix)
(string-prefix-p prefix name))
collect (concat ":" (symbol-name arg)))))
(defun slime-complete-keyword-arg ()
(when-let ((end (point))
(beg (slime-symbol-start-pos))
(prefix (unless (= beg end)
(string-trim-left (buffer-substring-no-properties beg end) ":")))
(args (slime-operator-keyword-args prefix)))
(list beg end args)))
;;*** completion-at-point
(defvar slime-completion-table-stage-1
(completion-table-merge (completion-table-dynamic #'slime-operator-keyword-args)
(completion-table-dynamic #'slime-simple-completions)
;; (slime-visible-symbols)
(completion-table-dynamic
(lambda (_) (slime-all-packages t)))
;; #'slime-filename-completion
(completion-table-dynamic
(lambda (_) *slime-all-symbols*))
))
(defun slime-completion-stage-1 ()
(list (slime-symbol-start-pos) (point) slime-completion-table-stage-1 :exclusive 'no))
(setq slime-completion-at-point-functions-old slime-completion-at-point-functions)
(setq slime-completion-at-point-functions (list #'slime-completion-stage-1))
;;** slime-visible-symbols
(defun slime-visible-symbols ()
(slime-eval
`(cl:let ((symbols))
(cl:do-symbols (s "cl-user" symbols)
(cl:push s symbols)))
(slime-current-package)))
;;** slime-find-all-symbols
(defvar *slime-all-symbols* nil)
(defun slime-refresh-all-symbols ()
(interactive)
(when (slime-connected-p)
(message "slime-refresh-all-symbols: refreshing...")
(slime-eval-async
`(cl:let ((symbols (cl:make-hash-table))
(names nil))
(cl:do-all-symbols (symbol symbols)
(cl:unless (cl:or (cl:keywordp symbol)
(cl:gethash symbol symbols))
(cl:setf (cl:gethash symbol symbols) t)))
(cl:with-standard-io-syntax
(cl:maphash (cl:lambda (symbol _)
(cl:push (cl:string-downcase (cl:prin1-to-string symbol)) names))
symbols))
names)
(lambda (symbols)
(setq *slime-all-symbols* symbols)
(unless (minibuffer-window-active-p (selected-window))
(message "slime-refresh-all-symbols: %s symbols." (length *slime-all-symbols*))))
"CL-USER"))
*slime-all-symbols*)
(add-hook 'slime-connected-hook #'slime-refresh-all-symbols)
(defvar slime-refresh-all-symbols-timer
(run-with-idle-timer 30 t #'slime-refresh-all-symbols))
(define-key slime-mode-map (kbd "C-c <f5>") 'slime-refresh-all-symbols)
(define-key slime-repl-mode-map (kbd "C-c <f5>") 'slime-refresh-all-symbols)
(defun slime-find-all-symbols (&optional internal)
"Return symbol-names of symbols from all registered packages."
;; TODO: Extract cl code as a slime contrib or something.
;; TODO: (CL) Add caching. Update cache via a compilation hook.
(let ((symbols
(if internal
*slime-all-symbols*
;; (slime-eval
;; `(cl:let ((symbols))
;; (cl:do-all-symbols (symbol symbols)
;; (cl:unless (cl:keywordp symbol)
;; (cl:push (cl:string-downcase (cl:prin1-to-string symbol)) symbols)))
;; (cl:nreverse symbols))
;; "CL-USER")
(slime-eval
`(cl:let ((symbols))
(cl:dolist (p (cl:list-all-packages))
(cl:do-external-symbols (symbol p)
(cl:unless (cl:keywordp symbol)
(cl:push (cl:string-downcase
(cl:prin1-to-string symbol))
symbols))))
(cl:nreverse symbols))
"CL-USER"))
))
symbols))
;;*** slime-completing-read
(defvar slime-completing-read-func #'completing-read)
(defun slime-completing-read (prompt collection &optional predicate require-match
initial-input hist def inherit-input-method)
(funcall slime-completing-read-func prompt collection predicate require-match
initial-input hist def inherit-input-method))
(defvar slime-ivy-read-map
(let ((m (make-sparse-keymap)))
(set-keymap-parent m ivy-minibuffer-map)
m))
(defun slime-ivy-read (prompt &optional collection predicate require-match
initial-input hist def inherit-input-method)
(ivy-read prompt collection
:predicate predicate
:require-match require-match
:initial-input initial-input
:history (or hist 'slime-minibuffer-history)
:def def
:keymap slime-ivy-read-map))
(setq slime-completing-read-func #'slime-ivy-read)
(define-key slime-ivy-read-map (kbd "<f5>") 'slime-refresh-all-symbols)
(defun slime-ivy-read--edit-definition ()
(interactive)
;; HACK: Make it seem like completing-read didn't return anything
(let ((symbol (ivy-state-current ivy-last)))
(setf (ivy-state-current ivy-last) nil)
(ivy-exit-with-action (lambda (_) (slime-edit-definition symbol)))))
(define-key slime-ivy-read-map (kbd "M-.") 'slime-ivy-read--edit-definition)
(defun slime-read-symbol-name-global (prompt &optional query internal)
"Either read a symbol or choose one at point. Choose from
all external symbols if QUERY is non-nil, there is no symbol
at point or a single prefix arg is supplied. With double
prefix arg or if INTERNAL is non-nil include internal symbols."
(let ((symbol (slime-symbol-at-point)))
(when (string-prefix-p "#:" symbol)
(setq symbol (substring symbol 2)))
(cond ((or current-prefix-arg query (not symbol))
(let ((internal (or internal
(= 16 (prefix-numeric-value current-prefix-arg)))))
(slime-completing-read
prompt
(slime-find-all-symbols internal)
nil nil
symbol)))
(t (slime-symbol-at-point)))))
(advice-add 'slime-read-symbol-name :override #'slime-read-symbol-name-global)
(defun slime--bounds-of-region-or-symbol ()
(if (region-active-p)
(cons (region-beginning) (region-end))
(bounds-of-thing-at-point 'symbol)))
(defun slime-complete-symbol-global (external)
"Complete a symbol searching symbols from all visible packages.
If INTERNAL is T, also search internal symbols."
;; TODO: xref and documentation lookups in completion buffer
(interactive "P")
(let* ((bounds (slime--bounds-of-region-or-symbol))
(start (and bounds (car bounds)))
(end (and bounds (cdr bounds)))
(initial-input (and bounds (buffer-substring-no-properties start end)))
(symbol (slime-completing-read "Find symbol: " (slime-find-all-symbols (not external)) nil nil initial-input)))
(when symbol
(when bounds
(delete-region start end))
(insert symbol))))
;;** `slime-saved-presentations'
(defvar slime-saved-presentations nil
"Saved slime-presentations. List of pairs (name . presentation).
Doesn't work properly with multiple lisp connections.")
(defvar slime-saved-presentations-history nil)
(defun slime-saved-presentations-reset ()
(setq slime-saved-presentations nil))
(add-hook 'slime-inferior-process-start-hook #'slime-saved-presentations-reset)
(defun slime-save-presentation-at-point (point)
"Save a presentation at point under prompted name. If presentation with the same name
already exists, override it."
(interactive "d")
(multiple-value-bind (presentation start end)
(slime-presentation-around-or-before-point-or-error point)
(let* ((str (buffer-substring start end))
(name (ivy-read "Save presentation as: " (mapcar #'car slime-saved-presentations)
:initial-input (substring-no-properties str)
:require-match nil
:caller 'slime-save-presentation-at-point
:history 'slime-saved-presentations-history)))
(slime-save-presentation name str presentation))))
(defun slime-save-presentation (name str presentation)
(let ((id (slime-presentation-id presentation))
(member (cl-member name slime-saved-presentations :key #'car :test #'string=)))
(unless (numberp id)
(setq id (slime-eval `(swank:lookup-and-save-presented-object-or-lose ',id))))
(let ((item (cons name (list str id))))
(if member
(setf (car member) item)
(push item slime-saved-presentations)))))
(defun slime-insert-saved-presentation-action (item)
;; TODO: update presentation str?
(destructuring-bind (str id) (cdr item)
(slime-insert-presentation str id)))
(defun slime-insert-saved-presentation ()
"Insert presentation saved under prompted name."
(interactive)
(ivy-read "Insert presentation: " slime-saved-presentations
:history 'slime-saved-presentations-history
:caller 'slime-insert-saved-presentation
:action #'slime-insert-saved-presentation-action))
(defun slime-saved-presentation-remove-action (presentation)
(setq slime-saved-presentations
(remove presentation slime-saved-presentations)))
(ivy-add-actions 'slime-save-presentation-at-point '(("d" slime-saved-presentation-remove-action "remove")))
(ivy-add-actions 'slime-insert-saved-presentation '(("d" slime-saved-presentation-remove-action "remove")))
(defun slime-saved-presentation-dwim (point)
"Save presentation if there is one around or before point,
otherwise insert a saved presentation."
(interactive "d")
(call-interactively
(if (slime-presentation-around-or-before-point-p)
#'slime-save-presentation-at-point
#'slime-insert-saved-presentation)))
(cl-defun slime-collect-presentations (&optional (buffer (current-buffer)))
"Return a list of (start-pos . end-pos) pairs for each slime presentation in BUFFER."
(let ((positions))
(slime-for-each-presentation-in-region (window-start) (window-end)
(lambda (presentation start end whole-p)
(push (cons start end) positions))
buffer)
(reverse positions)))
(defun slime-avy-copy-presentation-to-point ()
(interactive)
(let ((candidates (mapcan (lambda (window)
(with-selected-window window
(mapcar (lambda (bounds)
(cons (car bounds) window))
(slime-collect-presentations))))
(avy-window-list))))
(avy-with slime-avy-copy-presentation-to-point
(avy--process
candidates
(avy--style-fn 'de-bruijn)))
(slime-copy-presentation-at-point-to-kill-ring (point))
(avy-pop-mark)
(with-current-buffer (slime-repl-buffer)
(call-interactively #'yank))))
(defun slime-repl-set-default-package ()
(interactive)
(slime-repl-set-package "cl-user"))
(defun sldb-sync-frame-package ()
"Set Lisp package to that of the current frame."
(interactive)
(let* ((frame (sldb-frame-number-at-point))
(pkg (slime-eval `(swank:frame-package-name ,frame))))
(slime-repl-set-package pkg)))
(defun slime-repl-bury-buffer ()
(interactive)
(bury-buffer)
(other-window 1))
(defun slime-repl-complete-ivy ()
"Complete input from repl history."
(interactive)
(let ((initial-input (slime-repl-current-input)))
(ivy-read "REPL search: " slime-repl-input-history
:initial-input initial-input
:action (lambda (text)
(slime-repl-delete-current-input)
(insert text)))))
;;** slime-read-package-name
(defvar slime-read-package-name-history nil)
(defun slime-read-package-name* (prompt &optional initial-value)
"Read a package name from the minibuffer, prompting with PROMPT."
(let ((completion-ignore-case t))
(completing-read prompt (slime-bogus-completion-alist
(slime-eval
`(swank:list-all-package-names t)))
nil t initial-value 'slime-read-package-name-history)))
(advice-add 'slime-read-package-name :override #'slime-read-package-name*)
;;** set `slime-buffer-package' in `edit-indirect'
(with-eval-after-load 'edit-indirect
(defun edit-indirect--set-slime-package ()
(when slime-mode
(when-let* ((ov edit-indirect--overlay)
(buf (overlay-buffer ov))
(package (with-current-buffer buf
(slime-current-package))))
(setq-local slime-buffer-package package))))
(add-hook 'edit-indirect-after-creation-hook #'edit-indirect--set-slime-package))
;;** slime-switch-to-sldb-buffer
(defun slime-switch-to-sldb-buffer ()
(interactive)
(when-let* ((bufs (sldb-buffers))
(buf (if (eq major-mode 'sldb-mode)
(cadr (member (current-buffer) bufs))
(car bufs))))
(pop-to-buffer buf)))
(define-key slime-parent-map (kbd "C-c C-a") 'slime-switch-to-sldb-buffer)
(define-key slime-prefix-map (kbd "C-a") 'slime-switch-to-sldb-buffer)
;;** if* keyword form indentation
(setq common-lisp-indent-if*-keyword
(rx (? ":") (or "else" "elseif" "then" "thenret")))
;;* Code refactoring utils
;;** `lisp-toggle-*-form'
(defvar lisp-keywords-with-*-variant nil
"Keywords that have a *-variant. Assoc list ((major-mode . keywords)).")
(defmacro define-*-keywords (mode &rest keywords)
(let ((regexps (mapcar (lambda (keyword)
`(and "(" symbol-start ,keyword (? "*") symbol-end))
keywords)))
`(setf (alist-get ',mode lisp-keywords-with-*-variant) (rx (or ,@regexps)))))
(define-*-keywords lisp-mode "let" "do" "list" "prog")
(define-*-keywords emacs-lisp-mode "let" "if-let" "when-let" "do" "list" "prog" "letf" "cl-letf")
(defun lisp-toggle-*-form (arg)
"Toggle * of ARGth nearest enclosing form that has a *-variant."
(interactive "p")
(when-let ((regexp (alist-get major-mode lisp-keywords-with-*-variant)))
(save-excursion
(condition-case err
(cl-loop do (up-list -1 t t)
counting (looking-at regexp) into i
when (= i arg)
do (progn (forward-symbol 1)
(if (looking-back "\\*")
(backward-delete-char 1)
(insert "*"))
(up-list -1 t t)
(indent-sexp)
(return)))
(scan-error nil)))))
(defun toggle-char (char pos &optional after)
"Toggle CHAR before POS. If AFTER is T, toggle after POS."
(save-excursion
(goto-char pos)
(if (eq char (if after (char-after) (char-before)))
(if after (delete-char 1) (backward-delete-char 1))
(insert char))))
;;** `lisp-toggle-tick'
(defun lisp-toggle-tick ()
"Toggle ' at the start of current region(if active) or symbol."
(interactive)
(when-let ((pos (if (region-active-p)
(region-beginning)
(car (bounds-of-thing-at-point 'symbol)))))
(toggle-char ?\' pos)))
(dolist (map (list lisp-mode-map emacs-lisp-mode-map slime-mode-map))
(define-key map (kbd "C-c C-8") 'lisp-toggle-*-form)
(define-key map (kbd "C-c C-'") 'lisp-toggle-tick)
(define-key map (kbd "C-c C-'") 'lisp-toggle-tick))
;;* slime repl copying/killing
(defun slime--repl-insert-string (string &optional at-point)
(when string
(slime-switch-to-output-buffer)
(unless at-point
(goto-char slime-repl-input-start-mark))
(insert string)))
(defun slime-expression-at-point ()
(or (and (looking-back (rx (or symbol-end ")")))
(slime-last-expression))
(slime-symbol-at-point)))
(defun slime-call-toplevel ()
"Like `slime-call-defun', but treat unknown forms as function definitions."
(interactive)
;; Reuse frame when popping to repl.
(let ((display-buffer-alist
(cons '("\\*slime-repl" nil (reusable-frames . t))
display-buffer-alist)))
(condition-case nil
(progn (call-interactively #'slime-call-defun) t)
(error
(when-let ((toplevel (slime-parse-toplevel-form))
(qualified-name (and (symbolp toplevel)
(slime-qualify-cl-symbol-name toplevel))))
(slime--repl-insert-string (format "(%s )" qualified-name))
(backward-char 1)
t)))))
(defun slime-copy-to-repl (toplevel)
"Copy region to repl if active, else copy last sexp.
With prefix arg, copy toplevel form."
(interactive "P")
(cond ((region-active-p)
(slime--repl-insert-string
(buffer-substring-no-properties (region-beginning) (region-end)) t))
(toplevel
(or (call-interactively #'slime-call-toplevel)
(destructuring-bind (beg end) (slime-region-for-defun-at-point)
(slime--repl-insert-string
(buffer-substring-no-properties beg end)))))
(t (slime--repl-insert-string (slime-expression-at-point)))))
(define-key slime-mode-map (kbd "C-c C-y") 'slime-copy-to-repl)
(define-key slime-repl-mode-map (kbd "C-c C-y") 'slime-copy-to-repl)
(defun slime-repl-kill-input-dwim (&optional all?)
"Like `slime-repl-kill-input', but kill all input with prefix arg.
Also always use `kill-region' instead of `delete-region'."
(interactive "P")
(cond ((or all? (= (point) (marker-position slime-repl-input-start-mark)))
(kill-region (slime-repl-history-yank-start) (point-max)))
((< (marker-position slime-repl-input-start-mark) (point))
(kill-region slime-repl-input-start-mark (point)))))
(define-key slime-repl-mode-map [remap slime-repl-kill-input] 'slime-repl-kill-input-dwim)
;;* slime-c-p-c.el completion
;; Use ivy instead of a completion buffer
(pushnew 'slime-c-p-c-completion-at-point slime-completion-at-point-functions)
(defun slime-display-completions-ivy (completions beg end)
(ivy-completion-in-region beg end completions))
(advice-add 'slime-display-or-scroll-completions :override #'slime-display-completions-ivy)
;;* sldb-fancy-break
;; TODO: track/toggle breaks, highlight functions with breaks/traces etc
;; TODO: slime-read-function-name
(defun sldb-fancy-break (name)
"Set a breakpoint at the start of the function NAME."
(interactive (list (slime-read-symbol-name "Break on enter: ")))
(slime-eval-async `(swank:sldb-break ,name)
(lambda (msg) (message "%s" msg))))
(define-key slime-parent-map (kbd "C-c B") 'sldb-fancy-break)
;;* evaluation(eros)
;; Display overlays with evaluation results
(with-eval-after-load 'eros
(defun slime-eval-last-expression-eros ()
(interactive)
(destructuring-bind (output value)
(slime-eval `(swank:eval-and-grab-output ,(slime-expression-at-point)))
(eros--make-result-overlay (concat output value)
:where (point)
:duration eros-eval-result-duration)))
(define-key slime-mode-map (kbd "C-x C-e") 'slime-eval-last-expression-eros)
(define-key slime-repl-mode-map (kbd "C-x C-e") 'slime-eval-last-expression-eros))
;;* asdf
(with-eval-after-load 'slime-asdf
(defun slime-load-system-dwim (reload)
"Compile and load an ASDF system. With prefix arg reload it instead."
(interactive "P")
(when-let* ((prompt (if reload "Reload system" "Load system"))
(system (slime-read-system-name prompt nil t)))
(if reload
(slime-reload-system system)
(slime-load-system system))))
(define-key slime-mode-map (kbd "C-c L") 'slime-load-system-dwim)
(define-key slime-repl-mode-map (kbd "C-c L") 'slime-load-system-dwim))
;;** slime-edit-definition support for asdf components
(defun slime-edit-asdf-component (name &optional where)
;; (or current-prefix-arg (not (equal (slime-symbol-at-point) name)))
(if current-prefix-arg
nil
(when-let* ((pathname
(slime-eval
`(cl:when (cl:find-package :asdf) ; TODO: a better way to check; featurep ?
(cl:let* ((symbol
(swank::find-definitions-find-symbol-or-package ,name))
(component
(asdf/find-component:find-component nil symbol)))
(cl:and component
(cl:namestring
(cl:or (cl:slot-value component
'asdf/component::source-file)
(asdf/component:component-pathname component)))))))))
;; MAYBE: use `slime-edit-definition-cont'
(slime-push-definition-stack)
(case where
(window (find-file-other-window pathname))
(frame (find-file-other-frame pathname))
(t (find-file pathname)))
t)))
(add-hook 'slime-edit-definition-hooks 'slime-edit-asdf-component)
;;* `slime-import-symbol'
(with-eval-after-load 'slime-package-fu
(require 'slime-import-symbol)
(define-key slime-mode-map (kbd "C-c C-x <C-i>") 'slime-import-symbol))
;;* trace
(defun sldb-fetch-traces ()
(interactive)
(with-current-buffer (slime-trace-dialog--ensure-buffer)
(slime-trace-dialog-fetch-traces)))
(defun sldb-continue-and-fetch-traces ()
(interactive)
(call-interactively #'sldb-continue)
(call-interactively #'sldb-fetch-traces))
;;* fancier fancy-trace
(defun slime-toggle-trace-dwim (&optional using-context-p)
"Toggle trace. Tries to guess what to trace depending on
major-mode. E.g. `slime-xref-mode'."
(interactive "P")
(let* ((spec (cond
((eq major-mode 'slime-xref-mode)
(when-let ((dspec (slime-xref-dspec-at-point)))
(slime-dspec-operator-name dspec)))
(using-context-p
(slime-extract-context))
(t (slime-symbol-at-point))))
(spec (slime-trace-query spec)))
(message "%s" (slime-eval `(swank-trace-dialog:dialog-toggle-trace
(swank::from-string ,spec))))
(run-hooks 'slime-trace-dialog-after-toggle-hook)))
(define-key slime-prefix-map [remap slime-toggle-fancy-trace] 'slime-toggle-trace-dwim)
(define-key slime-prefix-map (kbd "C-c t") 'slime-trace-dialog)
(define-key slime-prefix-map (kbd "C-c T") nil)
(define-key slime-trace-dialog-minor-mode-map (kbd "C-c t") 'slime-trace-dialog)
(define-key slime-trace-dialog-minor-mode-map (kbd "C-c T") nil)
;;** slime-dspec-operator-name
(defun slime-dspec-operator-name (dspec)
"Try to extract operator name from slime dspec."
(let ((parsed-spec (car (read-from-string dspec))))
(pcase parsed-spec
(`(:operator (method (setf ,(and (pred symbolp) name) . ,_))) (symbol-name name))
(`(:operator (method ,(and (pred symbolp) name) . ,_)) (symbol-name name))
(`(:operator (setf ,(and (pred symbolp) name) . ,_)) (symbol-name name))
(`(:operator ,(and (pred symbolp) name)) (symbol-name name))
(_ nil))))
;;** slime-xref-toggle-trace-all
(defun slime-xref-toggle-trace-all (&optional untrace)
"Toggle tracing for ALL operators in a `slime-xref-mode' buffer.
TODO: With prefix arg untrace all."
(interactive "P")
(when-let* ((xrefs (slime-all-xrefs))
(queries
(loop for (dspec) in xrefs
when (slime-dspec-operator-name dspec)
collect `(swank-trace-dialog:dialog-toggle-trace
(swank::from-string ,it)))))
(slime-eval `(cl:progn ,@queries))
(run-hooks 'slime-trace-dialog-after-toggle-hook)))
;; (define-key slime-xref-mode-map (kbd "C-c A") 'slime-xref-toggle-trace-all)
(define-key slime-xref-mode-map (kbd "C-c T") 'slime-xref-toggle-trace-all)
;;* avy-actions
(defun avy-action-copy-to-repl (pt)
(when (number-or-marker-p pt)
(case major-mode
(slime-inspector-mode
(goto-char pt)
(call-interactively #'slime-inspector-copy-down-to-repl))
((or slime-mode slime-popup-buffer-mode slime-trace-dialog-mode sldb-mode slime-repl-mode)
(slime-copy-presentation-at-point-to-repl pt)))))
(defun avy-action-inspect (pt)
(when (number-or-marker-p pt)
(case major-mode
(slime-inspector-mode
(goto-char pt)
(call-interactively #'slime-inspector-operate-on-point))
((or slime-mode slime-popup-buffer-mode slime-trace-dialog-mode sldb-mode slime-repl-mode)
(slime-inspect-presentation-at-point pt)))))
(add-to-list 'avy-dispatch-alist (cons ?C #'avy-action-copy-to-repl))
(add-to-list 'avy-dispatch-alist (cons ?I #'avy-action-inspect))
;;* macrostep
(defun ace-link-macrostep ()
"Jump to and expand a macro or collapse an expaded one."
(interactive)
(let ((pt (avy-with ace-link-macrostep