forked from joaotavora/sly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sly.el
7489 lines (6584 loc) · 289 KB
/
sly.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
;;; sly.el --- Sylvester the Cat's Common Lisp IDE -*- lexical-binding: t; -*-
;; Version: 1.0.43
;; URL: https://github.com/joaotavora/sly
;; Package-Requires: ((emacs "24.3"))
;; Keywords: languages, lisp, sly
;; Copyright (C) 2003 Eric Marsden, Luke Gorrie, Helmut Eller
;; Copyright (C) 2004,2005,2006 Luke Gorrie, Helmut Eller
;; Copyright (C) 2007,2008,2009 Helmut Eller, Tobias C. Rittweiler
;; Copyright (C) 2014 João Távora
;; For a detailed list of contributors, see the manual.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; _____ __ __ __
;; / ___/ / / \ \/ / |\ _,,,---,,_
;; \__ \ / / \ / /,`.-'`' -. ;-;;,_
;; ___/ / / /___ / / |,4- ) )-,_..;\ ( `'-'
;; /____/ /_____/ /_/ '---''(_/--' `-'\_)
;;
;;
;; SLY is Sylvester the Cat's Common Lisp IDE.
;;
;; SLY is a direct fork of SLIME, and contains the following
;; improvements over it:
;;
;; * A full-featured REPL based on Emacs's `comint.el`;
;; * Live code annotations via a new `sly-stickers` contrib;
;; * Consistent button interface. Every Lisp object can be copied to the REPL;
;; * flex-style completion out-of-the-box, using Emacs's completion API.
;; Company, Helm, and others supported natively, no plugin required;
;; * Cleanly ASDF-loaded by default, including contribs, enabled out-of-the-box;
;; * Multiple inspectors and multiple REPLs;
;; * An interactive trace dialog with interactive objects. Copies function calls
;; to the REPL;
;; * "Presentations" replaced by interactive backreferences which
;; highlight the object and remain stable throughout the REPL session;
;;
;; SLY is a fork of SLIME. We track its bugfixes, particularly to the
;; implementation backends. All SLIME's familar features (debugger,
;; inspector, xref, etc...) are still available, with improved overall
;; UX.
;;
;; See the NEWS.md file (should be sitting alongside this file) for
;; more information
;;; Code:
(require 'cl-lib)
(eval-and-compile
(if (version< emacs-version "24.3")
(error "Sly requires at least Emacs 24.3")))
(eval-and-compile
(or (require 'hyperspec nil t)
(require 'hyperspec "lib/hyperspec")))
(require 'thingatpt)
(require 'comint)
(require 'pp)
(require 'easymenu)
(require 'arc-mode)
(require 'etags)
(require 'apropos)
(require 'bytecomp) ;; for `byte-compile-current-file' and
;; `sly-byte-compile-hotspots'.
(require 'sly-common "lib/sly-common")
(require 'sly-messages "lib/sly-messages")
(require 'sly-buttons "lib/sly-buttons")
(require 'sly-completion "lib/sly-completion")
(require 'gv) ; for gv--defsetter
(eval-when-compile
(require 'compile)
(require 'gud))
(defvar sly-path nil
"Directory containing the SLY package.
This is used to load the supporting Common Lisp library, Slynk.
The default value is automatically computed from the location of the
Emacs Lisp package.")
;; Determine `sly-path' at load time, regardless of filename (.el or
;; .elc) being loaded.
;;
(setq sly-path
(if load-file-name
(file-name-directory load-file-name)
(error "[sly] fatal: impossible to determine sly-path")))
(defun sly-slynk-path ()
"Path where the bundled Slynk server is located."
(expand-file-name "slynk/" sly-path))
;;;###autoload
(define-obsolete-variable-alias 'sly-setup-contribs
'sly-contribs "2.3.2")
;;;###autoload
(defvar sly-contribs '(sly-fancy)
"A list of contrib packages to load with SLY.")
;;;###autoload
(defun sly-setup (&optional contribs)
"Have SLY load and use extension modules CONTRIBS.
CONTRIBS defaults to `sly-contribs' and is a list (LIB1 LIB2...)
symbols of `provide'd and `require'd Elisp libraries.
If CONTRIBS is nil, `sly-contribs' is *not* affected, otherwise
it is set to CONTRIBS.
However, after `require'ing LIB1, LIB2 ..., this command invokes
additional initialization steps associated with each element
LIB1, LIB2, which can theoretically be reverted by
`sly-disable-contrib.'
Notably, one of the extra initialization steps is affecting the
value of `sly-required-modules' (which see) thus affecting the
libraries loaded in the Slynk servers.
If SLY is currently connected to a Slynk and a contrib in
CONTRIBS has never been loaded, that Slynk is told to load the
associated Slynk extension module.
To ensure that a particular contrib is loaded, use
`sly-enable-contrib' instead."
;; FIXME: The contract should be like some hypothetical
;; `sly-refresh-contribs'
;;
(interactive)
(when contribs
(setq sly-contribs contribs))
(sly--setup-contribs))
(defvaralias 'sly-required-modules 'sly-contrib--required-slynk-modules)
(defvar sly-contrib--required-slynk-modules '()
"Alist of (MODULE . (WHERE CONTRIB)) for slynk-provided features.
MODULE is a symbol naming a specific Slynk feature, WHERE is
the full pathname to the directory where the file(s)
providing the feature are found and CONTRIB is a symbol as found
in `sly-contribs.'")
(cl-defmacro sly--contrib-safe (contrib &body body)
"Run BODY catching and resignalling any errors for CONTRIB"
(declare (indent 1))
`(condition-case-unless-debug e
(progn
,@body)
(error (sly-error "There's an error in %s: %s"
,contrib
e))))
(defun sly--setup-contribs ()
"Load and initialize contribs."
;; active != enabled
;; ^ ^
;; | |
;; v v
;; forgotten != disabled
(add-to-list 'load-path (expand-file-name "contrib" sly-path))
(mapc (lambda (c)
(sly--contrib-safe c (require c)))
sly-contribs)
(let* ((all-active-contribs
;; these are the contribs the user chose to activate
;;
(mapcar #'sly-contrib--find-contrib
(cl-reduce #'append (mapcar #'sly-contrib--all-dependencies
sly-contribs))))
(defined-but-forgotten-contribs
;; "forgotten contribs" are the ones the chose not to
;; activate but whose definitions we have seen
;;
(cl-remove-if #'(lambda (contrib)
(memq contrib all-active-contribs))
(sly-contrib--all-contribs))))
;; Disable any forgotten contribs that are enabled right now.
;;
(cl-loop for to-disable in defined-but-forgotten-contribs
when (sly--contrib-safe to-disable
(sly-contrib--enabled-p to-disable))
do (funcall (sly-contrib--disable to-disable)))
;; Enable any active contrib that is *not* enabled right now.
;;
(cl-loop for to-enable in all-active-contribs
unless (sly--contrib-safe to-enable
(sly-contrib--enabled-p to-enable))
do (funcall (sly-contrib--enable to-enable)))
;; Some contribs add stuff to `sly-mode-hook' or
;; `sly-editing-hook', so make sure we re-run those hooks now.
(when all-active-contribs
(defvar sly-editing-mode) ;FIXME: Forward reference!
(cl-loop for buffer in (buffer-list)
do (with-current-buffer buffer
(when sly-editing-mode (sly-editing-mode 1)))))))
(eval-and-compile
(defun sly-version (&optional interactive file)
"Read SLY's version of its own sly.el file.
If FILE is passed use that instead to discover the version."
(interactive "p")
(let ((version
(with-temp-buffer
(insert-file-contents
(or file
(expand-file-name "sly.el" sly-path))
nil 0 200)
(and (search-forward-regexp
";;[[:space:]]*Version:[[:space:]]*\\(.*\\)$" nil t)
(match-string 1)))))
(if interactive
(sly-message "SLY %s" version)
version))))
(defvar sly-protocol-version nil)
(setq sly-protocol-version
;; Compile the version string into the generated .elc file, but
;; don't actualy affect `sly-protocol-version' until load-time.
;;
(eval-when-compile (sly-version nil (or load-file-name
byte-compile-current-file))))
;;;; Customize groups
;;
;;;;; sly
(defgroup sly nil
"Interaction with the Superior Lisp Environment."
:prefix "sly-"
:group 'applications)
;;;;; sly-ui
(defgroup sly-ui nil
"Interaction with the Superior Lisp Environment."
:prefix "sly-"
:group 'sly)
(defcustom sly-truncate-lines t
"Set `truncate-lines' in popup buffers.
This applies to buffers that present lines as rows of data, such as
debugger backtraces and apropos listings."
:type 'boolean
:group 'sly-ui)
(defcustom sly-kill-without-query-p nil
"If non-nil, kill SLY processes without query when quitting Emacs.
This applies to the *inferior-lisp* buffer and the network connections."
:type 'boolean
:group 'sly-ui)
;;;;; sly-lisp
(defgroup sly-lisp nil
"Lisp server configuration."
:prefix "sly-"
:group 'sly)
(defcustom sly-ignore-protocol-mismatches nil
"If non-nil, ignore protocol mismatches between SLY and Slynk.
Programatically, this variable can be let-bound around calls to
`sly' or `sly-connect'."
:type 'boolean
:group 'sly)
(defcustom sly-init-function 'sly-init-using-asdf
"Function bootstrapping slynk on the remote.
Value is a function of two arguments: SLYNK-PORTFILE and an
ingored argument for backward compatibility. Function should
return a string issuing very first commands issued by Sly to
the remote-connection process. Some time after this there should
be a port number ready in SLYNK-PORTFILE."
:type '(choice (const :tag "Use ASDF"
sly-init-using-asdf)
(const :tag "Use legacy slynk-loader.lisp"
sly-init-using-slynk-loader))
:group 'sly-lisp)
(define-obsolete-variable-alias 'sly-backend
'sly-slynk-loader-backend "3.0")
(defcustom sly-slynk-loader-backend "slynk-loader.lisp"
"The name of the slynk-loader that loads the Slynk server.
Only applicable if `sly-init-function' is set to
`sly-init-using-slynk-loader'. This name is interpreted
relative to the directory containing sly.el, but could also be
set to an absolute filename."
:type 'string
:group 'sly-lisp)
(defcustom sly-connected-hook nil
"List of functions to call when SLY connects to Lisp."
:type 'hook
:group 'sly-lisp)
(defcustom sly-enable-evaluate-in-emacs nil
"*If non-nil, the inferior Lisp can evaluate arbitrary forms in Emacs.
The default is nil, as this feature can be a security risk."
:type '(boolean)
:group 'sly-lisp)
(defcustom sly-lisp-host "localhost"
"The default hostname (or IP address) to connect to."
:type 'string
:group 'sly-lisp)
(defcustom sly-port 4005
"Port to use as the default for `sly-connect'."
:type 'integer
:group 'sly-lisp)
(defvar sly-connect-host-history (list sly-lisp-host))
(defvar sly-connect-port-history (list (prin1-to-string sly-port)))
(defvar sly-net-valid-coding-systems
'((iso-latin-1-unix nil "iso-latin-1-unix")
(iso-8859-1-unix nil "iso-latin-1-unix")
(binary nil "iso-latin-1-unix")
(utf-8-unix t "utf-8-unix")
(emacs-mule-unix t "emacs-mule-unix")
(euc-jp-unix t "euc-jp-unix"))
"A list of valid coding systems.
Each element is of the form: (NAME MULTIBYTEP CL-NAME)")
(defun sly-find-coding-system (name)
"Return the coding system for the symbol NAME.
The result is either an element in `sly-net-valid-coding-systems'
of nil."
(let ((probe (assq name sly-net-valid-coding-systems)))
(when (and probe (if (fboundp 'check-coding-system)
(ignore-errors (check-coding-system (car probe)))
(eq (car probe) 'binary)))
probe)))
(defcustom sly-net-coding-system
(car (cl-find-if 'sly-find-coding-system
sly-net-valid-coding-systems :key 'car))
"Coding system used for network connections.
See also `sly-net-valid-coding-systems'."
:type (cons 'choice
(mapcar (lambda (x)
(list 'const (car x)))
sly-net-valid-coding-systems))
:group 'sly-lisp)
;;;;; sly-mode
(defgroup sly-mode nil
"Settings for sly-mode Lisp source buffers."
:prefix "sly-"
:group 'sly)
;;;;; sly-mode-faces
(defgroup sly-mode-faces nil
"Faces in sly-mode source code buffers."
:prefix "sly-"
:group 'sly-mode)
(defface sly-error-face
`((((class color) (background light))
(:underline "tomato"))
(((class color) (background dark))
(:underline "tomato"))
(t (:underline t)))
"Face for errors from the compiler."
:group 'sly-mode-faces)
(defface sly-warning-face
`((((class color) (background light))
(:underline "orange"))
(((class color) (background dark))
(:underline "coral"))
(t (:underline t)))
"Face for warnings from the compiler."
:group 'sly-mode-faces)
(defface sly-style-warning-face
`((((class color) (background light))
(:underline "olive drab"))
(((class color) (background dark))
(:underline "khaki"))
(t (:underline t)))
"Face for style-warnings from the compiler."
:group 'sly-mode-faces)
(defface sly-note-face
`((((class color) (background light))
(:underline "brown3"))
(((class color) (background dark))
(:underline "light goldenrod"))
(t (:underline t)))
"Face for notes from the compiler."
:group 'sly-mode-faces)
;;;;; sly-db
(defgroup sly-debugger nil
"Backtrace options and fontification."
:prefix "sly-db-"
:group 'sly)
(defmacro define-sly-db-faces (&rest faces)
"Define the set of SLY-DB faces.
Each face specifiation is (NAME DESCRIPTION &optional PROPERTIES).
NAME is a symbol; the face will be called sly-db-NAME-face.
DESCRIPTION is a one-liner for the customization buffer.
PROPERTIES specifies any default face properties."
`(progn ,@(cl-loop for face in faces
collect `(define-sly-db-face ,@face))))
(defmacro define-sly-db-face (name description &optional default)
(let ((facename (intern (format "sly-db-%s-face" (symbol-name name)))))
`(defface ,facename
(list (list t ,default))
,(format "Face for %s." description)
:group 'sly-debugger)))
(define-sly-db-faces
(topline "the top line describing the error")
(condition "the condition class" '(:inherit error))
(section "the labels of major sections in the debugger buffer"
'(:inherit header-line))
(frame-label "backtrace frame numbers"
'(:inherit shadow))
(restart "restart descriptions")
(restart-number "restart numbers (correspond to keystrokes to invoke)"
'(:inherit shadow))
(frame-line "function names and arguments in the backtrace")
(restartable-frame-line
"frames which are surely restartable"
'(:inherit font-lock-constant-face))
(non-restartable-frame-line
"frames which are surely not restartable")
(local-name "local variable names")
(catch-tag "catch tags"))
;;;;; Key bindings
(defvar sly-doc-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-a") 'sly-apropos)
(define-key map (kbd "C-z") 'sly-apropos-all)
(define-key map (kbd "C-p") 'sly-apropos-package)
(define-key map (kbd "C-d") 'sly-describe-symbol)
(define-key map (kbd "C-f") 'sly-describe-function)
(define-key map (kbd "C-h") 'sly-documentation-lookup)
(define-key map (kbd "~") 'common-lisp-hyperspec-format)
(define-key map (kbd "C-g") 'common-lisp-hyperspec-glossary-term)
(define-key map (kbd "#") 'common-lisp-hyperspec-lookup-reader-macro)
map))
(defvar sly-who-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c") 'sly-who-calls)
(define-key map (kbd "C-w") 'sly-calls-who)
(define-key map (kbd "C-r") 'sly-who-references)
(define-key map (kbd "C-b") 'sly-who-binds)
(define-key map (kbd "C-s") 'sly-who-sets)
(define-key map (kbd "C-m") 'sly-who-macroexpands)
(define-key map (kbd "C-a") 'sly-who-specializes)
map))
(defvar sly-selector-map (let ((map (make-sparse-keymap)))
(define-key map "c" 'sly-list-connections)
(define-key map "t" 'sly-list-threads)
(define-key map "d" 'sly-db-pop-to-debugger-maybe)
(define-key map "e" 'sly-pop-to-events-buffer)
(define-key map "i" 'sly-inferior-lisp-buffer)
(define-key map "l" 'sly-switch-to-most-recent)
map)
"A keymap for frequently used SLY shortcuts.
Access to this keymap can be installed in in
`sly-mode-map', using something like
(global-set-key (kbd \"C-z\") sly-selector-map)
This will bind C-z to this prefix map, one keystroke away from
the available shortcuts:
\\{sly-selector-map}
As usual, users or extensions can plug in
any command into it using
(define-key sly-selector-map (kbd \"k\") 'sly-command)
Where \"k\" is the key to bind and \"sly-command\" is any
interactive command.\".")
(defvar sly-prefix-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-r") 'sly-eval-region)
(define-key map (kbd ":") 'sly-interactive-eval)
(define-key map (kbd "C-e") 'sly-interactive-eval)
(define-key map (kbd "E") 'sly-edit-value)
(define-key map (kbd "C-l") 'sly-load-file)
(define-key map (kbd "C-b") 'sly-interrupt)
(define-key map (kbd "M-d") 'sly-disassemble-symbol)
(define-key map (kbd "C-t") 'sly-toggle-trace-fdefinition)
(define-key map (kbd "I") 'sly-inspect)
(define-key map (kbd "C-x t") 'sly-list-threads)
(define-key map (kbd "C-x n") 'sly-next-connection)
(define-key map (kbd "C-x c") 'sly-list-connections)
(define-key map (kbd "C-x p") 'sly-prev-connection)
(define-key map (kbd "<") 'sly-list-callers)
(define-key map (kbd ">") 'sly-list-callees)
;; Include DOC keys...
(define-key map (kbd "C-d") sly-doc-map)
;; Include XREF WHO-FOO keys...
(define-key map (kbd "C-w") sly-who-map)
;; `sly-selector-map' used to be bound to "C-c C-s" by default,
;; but sly-stickers has a better binding for that.
;;
;; (define-key map (kbd "C-s") sly-selector-map)
map))
(defvar sly-mode-map
(let ((map (make-sparse-keymap)))
;; These used to be a `sly-parent-map'
(define-key map (kbd "M-.") 'sly-edit-definition)
(define-key map (kbd "M-,") 'sly-pop-find-definition-stack)
(define-key map (kbd "M-_") 'sly-edit-uses) ; for German layout
(define-key map (kbd "M-?") 'sly-edit-uses) ; for USian layout
(define-key map (kbd "C-x 4 .") 'sly-edit-definition-other-window)
(define-key map (kbd "C-x 5 .") 'sly-edit-definition-other-frame)
(define-key map (kbd "C-x C-e") 'sly-eval-last-expression)
(define-key map (kbd "C-M-x") 'sly-eval-defun)
;; Include PREFIX keys...
(define-key map (kbd "C-c") sly-prefix-map)
;; Completion
(define-key map (kbd "C-c TAB") 'completion-at-point)
;; Evaluating
(define-key map (kbd "C-c C-p") 'sly-pprint-eval-last-expression)
;; Macroexpand
(define-key map (kbd "C-c C-m") 'sly-expand-1)
(define-key map (kbd "C-c M-m") 'sly-macroexpand-all)
;; Misc
(define-key map (kbd "C-c C-u") 'sly-undefine-function)
map))
(defvar sly-editing-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "M-p") 'sly-previous-note)
(define-key map (kbd "M-n") 'sly-next-note)
(define-key map (kbd "C-c M-c") 'sly-remove-notes)
(define-key map (kbd "C-c C-k") 'sly-compile-and-load-file)
(define-key map (kbd "C-c M-k") 'sly-compile-file)
(define-key map (kbd "C-c C-c") 'sly-compile-defun)
map))
(defvar sly-popup-buffer-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "q") 'quit-window)
map))
;;;; Minor modes
;;;;; sly-mode
(defvar sly-buffer-connection)
(defvar sly-dispatching-connection)
(defvar sly-current-thread)
;; exceptional forward decl
(defvar company-tooltip-align-annotations)
;;;###autoload
(define-minor-mode sly-mode
"Minor mode for horizontal SLY functionality."
nil nil nil
;; Company-mode should have this by default
;; See gh#166
(set (make-local-variable 'company-tooltip-align-annotations) t))
(defun sly--lisp-indent-function (&rest args)
(let ((fn (if (fboundp 'sly-common-lisp-indent-function)
#'sly-common-lisp-indent-function
#'lisp-indent-function)))
(apply fn args)))
;;;###autoload
(define-minor-mode sly-editing-mode
"Minor mode for editing `lisp-mode' buffers."
nil nil nil
(sly-mode 1)
(setq-local lisp-indent-function #'sly--lisp-indent-function))
(define-minor-mode sly-popup-buffer-mode
"Minor mode for all read-only SLY buffers"
nil nil nil
(sly-mode 1)
(sly-interactive-buttons-mode 1)
(setq buffer-read-only t))
;;;;;; Mode-Line
(defface sly-mode-line
'((t (:inherit font-lock-constant-face
:weight bold)))
"Face for package-name in SLY's mode line."
:group 'sly)
(defvar sly--mode-line-format `(:eval (sly--mode-line-format)))
(put 'sly--mode-line-format 'risky-local-variable t)
(defvar sly-menu) ;; forward referenced
(defvar sly-extra-mode-line-constructs nil
"A list of mode-line constructs to add to SLY's mode-line.
Each construct is separated by a \"/\" and may be a regular
mode-line construct or a symbol naming a function of no arguments
that returns one such construct.")
(defun sly--mode-line-format ()
(let* ((conn (sly-current-connection))
(conn (and (process-live-p conn) conn))
(name (or (and conn
(sly-connection-name conn))
"*"))
(pkg (sly-current-package))
(format-number (lambda (n) (cond ((and n (not (zerop n)))
(format "%d" n))
(n "-")
(t "*"))))
(package-name (and pkg
(sly--pretty-package-name pkg)))
(pending (and conn
(length (sly-rex-continuations conn))))
(sly-dbs (and conn (length (sly-db-buffers conn)))))
`((:propertize "sly"
face sly-mode-line
keymap ,(let ((map (make-sparse-keymap)))
(define-key map [mode-line down-mouse-1]
sly-menu)
map)
mouse-face mode-line-highlight
help-echo "mouse-1: pop-up SLY menu"
)
" "
(:propertize ,name
face sly-mode-line
keymap ,(let ((map (make-sparse-keymap)))
(define-key map [mode-line mouse-1] 'sly-prev-connection)
(define-key map [mode-line mouse-2] 'sly-list-connections)
(define-key map [mode-line mouse-3] 'sly-next-connection)
map)
mouse-face mode-line-highlight
help-echo ,(concat "mouse-1: previous connection\n"
"mouse-2: list connections\n"
"mouse-3: next connection"))
"/"
,(or package-name "*")
"/"
(:propertize ,(funcall format-number pending)
help-echo ,(if conn (format "%s pending events outgoing\n%s"
pending
(concat "mouse-1: go to *sly-events* buffer"
"mouse-3: forget pending continuations"))
"No current connection")
mouse-face mode-line-highlight
face ,(cond ((and pending (cl-plusp pending))
'warning)
(t
'sly-mode-line))
keymap ,(let ((map (make-sparse-keymap)))
(define-key map [mode-line mouse-1] 'sly-pop-to-events-buffer)
(define-key map [mode-line mouse-3] 'sly-forget-pending-events)
map))
"/"
(:propertize ,(funcall format-number sly-dbs)
help-echo ,(if conn (format "%s SLY-DB buffers waiting\n%s"
pending
"mouse-1: go to first one")
"No current connection")
mouse-face mode-line-highlight
face ,(cond ((and sly-dbs (cl-plusp sly-dbs))
'warning)
(t
'sly-mode-line))
keymap ,(let ((map (make-sparse-keymap)))
(define-key map [mode-line mouse-1] 'sly-db-pop-to-debugger)
map))
,@(cl-loop for construct in sly-extra-mode-line-constructs
collect "/"
collect (if (and (symbolp construct)
(fboundp construct))
(condition-case _oops
(funcall construct)
(error "*sly-invalid*"))
construct)))))
(defun sly--refresh-mode-line ()
(force-mode-line-update t))
(defun sly--pretty-package-name (name)
"Return a pretty version of a package name NAME."
(cond ((string-match "^#?:\\(.*\\)$" name)
(match-string 1 name))
((string-match "^\"\\(.*\\)\"$" name)
(match-string 1 name))
(t name)))
(add-to-list 'mode-line-misc-info
`(sly-mode (" [" sly--mode-line-format "] ")))
;;;; Framework'ey bits
;;;
;;; This section contains some standard SLY idioms: basic macros,
;;; ways of showing messages to the user, etc. All the code in this
;;; file should use these functions when applicable.
;;;
;;;;; Syntactic sugar
(cl-defmacro sly--when-let ((var value) &rest body)
"Evaluate VALUE, if the result is non-nil bind it to VAR and eval BODY.
\(fn (VAR VALUE) &rest BODY)"
(declare (indent 1))
`(let ((,var ,value))
(when ,var ,@body)))
(cl-defmacro sly--when-let* (bindings &rest body)
"Same as `sly--when-let', but for multiple BINDINGS"
(declare (indent 1))
(if bindings
`(sly--when-let ,(car bindings)
(sly--when-let* ,(cdr bindings) ,@body))
`(progn ,@body)))
(defmacro sly-dcase (value &rest patterns)
(declare (indent 1)
(debug (sexp &rest (sexp &rest form))))
"Dispatch VALUE to one of PATTERNS.
A cross between `case' and `destructuring-bind'.
The pattern syntax is:
((HEAD . ARGS) . BODY)
The list of patterns is searched for a HEAD `eq' to the car of
VALUE. If one is found, the BODY is executed with ARGS bound to the
corresponding values in the CDR of VALUE."
(let ((operator (cl-gensym "op-"))
(operands (cl-gensym "rand-"))
(tmp (cl-gensym "tmp-")))
`(let* ((,tmp ,value)
(,operator (car ,tmp))
(,operands (cdr ,tmp)))
(cl-case ,operator
,@(mapcar (lambda (clause)
(if (eq (car clause) t)
`(t ,@(cdr clause))
(cl-destructuring-bind ((op &rest rands) &rest body)
clause
`(,op (cl-destructuring-bind ,rands ,operands
. ,(or body
'((ignore)) ; suppress some warnings
))))))
patterns)
,@(if (eq (caar (last patterns)) t)
'()
`((t (sly-error "Elisp sly-dcase failed: %S" ,tmp))))))))
;;;;; Very-commonly-used functions
;; Interface
(cl-defun sly-buffer-name (type &key connection hidden suffix)
(cl-assert (keywordp type))
(mapconcat #'identity
`(,@(if hidden `(" "))
"*sly-"
,(downcase (substring (symbol-name type) 1))
,@(if connection
`(" for "
,(sly-connection-name
(if (eq connection t)
(sly-current-connection)
connection))))
,@(if suffix
`(" ("
,suffix
")"))
"*")
""))
(defun sly-recenter (target &optional move-point)
"Make the region between point and TARGET visible.
Minimize window motion if possible. If MOVE-POINT allow point to
move to make TARGET visible."
(unless (pos-visible-in-window-p target)
(redisplay)
(let ((screen-line (- (line-number-at-pos)
(line-number-at-pos (window-start))))
(window-end (line-number-at-pos (window-end)))
(window-start (line-number-at-pos (window-start)))
(target-line (line-number-at-pos target))
recenter-arg)
(cond ((> (point) target)
(setq recenter-arg (+ screen-line (- window-start target-line)))
(if (or (not move-point)
(<= recenter-arg (window-height)))
(recenter recenter-arg)
(goto-char target)
(recenter -1)
(move-to-window-line -1)))
((<= (point) target)
(setq recenter-arg (- screen-line (- target-line window-end)))
(if (or (not move-point)
(> recenter-arg 0))
(recenter (max recenter-arg 0))
(goto-char target)
(recenter 0)
(move-to-window-line 0)))))))
;; Interface
(defun sly-set-truncate-lines ()
"Apply `sly-truncate-lines' to the current buffer."
(when sly-truncate-lines
(set (make-local-variable 'truncate-lines) t)))
;; Interface
(defun sly-read-package-name (prompt &optional initial-value allow-blank)
"Read a package name from the minibuffer, prompting with PROMPT.
If ALLOW-BLANK may return nil to signal no particular package
selected."
(let* ((completion-ignore-case t)
(res (completing-read
(concat "[sly] " prompt)
(sly-eval
`(slynk:list-all-package-names t))
nil (not allow-blank) initial-value)))
(unless (zerop (length res))
res)))
;; Interface
(defmacro sly-propertize-region (props &rest body)
"Execute BODY and add PROPS to all the text it inserts.
More precisely, PROPS are added to the region between the point's
positions before and after executing BODY."
(declare (indent 1) (debug (sexp &rest form)))
(let ((start (cl-gensym)))
`(let ((,start (point)))
(prog1 (progn ,@body)
(add-text-properties ,start (point) ,props)))))
(defun sly-add-face (face string)
(declare (indent 1))
(add-text-properties 0 (length string) (list 'face face) string)
string)
;; Interface
(defsubst sly-insert-propertized (props &rest args)
"Insert all ARGS and then add text-PROPS to the inserted text."
(sly-propertize-region props (apply #'insert args)))
(defmacro sly-with-rigid-indentation (level &rest body)
"Execute BODY and then rigidly indent its text insertions.
Assumes all insertions are made at point."
(declare (indent 1))
(let ((start (cl-gensym)) (l (cl-gensym)))
`(let ((,start (point)) (,l ,(or level '(current-column))))
(prog1 (progn ,@body)
(sly-indent-rigidly ,start (point) ,l)))))
(defun sly-indent-rigidly (start end column)
;; Similar to `indent-rigidly' but doesn't inherit text props.
(let ((indent (make-string column ?\ )))
(save-excursion
(goto-char end)
(beginning-of-line)
(while (and (<= start (point))
(progn
(insert-before-markers indent)
(zerop (forward-line -1))))))))
(defun sly-insert-indented (&rest strings)
"Insert all arguments rigidly indented."
(sly-with-rigid-indentation nil
(apply #'insert strings)))
(defun sly-compose (&rest functions)
"Compose unary FUNCTIONS right-associatively, returning a function"
#'(lambda (x)
(cl-reduce #'funcall functions :initial-value x :from-end t)))
(defun sly-curry (fun &rest args)
"Partially apply FUN to ARGS. The result is a new function."
(lambda (&rest more) (apply fun (append args more))))
(defun sly-rcurry (fun &rest args)
"Like `sly-curry' but ARGS on the right are applied."
(lambda (&rest more) (apply fun (append more args))))
;;;;; Temporary popup buffers
;; keep compiler quiet
(defvar sly-buffer-package)
(defvar sly-buffer-connection)
;; Interface
(cl-defmacro sly-with-popup-buffer ((name &key package connection select
same-window-p
mode)
&body body)
"Similar to `with-output-to-temp-buffer'.
Bind standard-output and initialize some buffer-local variables.
Restore window configuration when closed. NAME is the name of
the buffer to be created. PACKAGE is the value
`sly-buffer-package'. CONNECTION is the value for
`sly-buffer-connection', if nil, no explicit connection is
associated with the buffer. If t, the current connection is
taken. MODE is the name of a major mode which will be enabled.
Non-nil SELECT indicates the buffer should be switched to, unless
it is `:hidden' meaning the buffer should not even be
displayed. SELECT can also be `:raise' meaning the buffer should
be switched to and the frame raised. SAME-WINDOW-P is a form
indicating if the popup *can* happen in the same window. The
forms SELECT and SAME-WINDOW-P are evaluated at runtime, not
macroexpansion time.
"
(declare (indent 1)
(debug (sexp &rest form)))
(let* ((package-sym (cl-gensym "package-"))
(connection-sym (cl-gensym "connection-"))
(select-sym (cl-gensym "select"))
(major-mode-sym (cl-gensym "select")))
`(let ((,package-sym ,(if (eq package t)
`(sly-current-package)
package))
(,connection-sym ,(if (eq connection t)
`(sly-current-connection)
connection))
(,major-mode-sym major-mode)
(,select-sym ,select)
(view-read-only nil))
(with-current-buffer (get-buffer-create ,name)
(let ((inhibit-read-only t)
(standard-output (current-buffer)))
(erase-buffer)
,@(cond (mode
`((funcall ,mode)))
(t
`((sly-popup-buffer-mode 1))))
(setq sly-buffer-package ,package-sym
sly-buffer-connection ,connection-sym)
(set-syntax-table lisp-mode-syntax-table)
,@body
(unless (eq ,select-sym :hidden)
(let ((window (display-buffer
(current-buffer)
(if ,(cond (same-window-p same-window-p)
(mode `(eq ,major-mode-sym ,mode)))
nil
t))))
(when ,select-sym
(if window
(select-window window t))))
(if (eq ,select-sym :raise) (raise-frame)))
(current-buffer))))))
;;;;; Filename translation
;;;
;;; Filenames passed between Emacs and Lisp should be translated using
;;; these functions. This way users who run Emacs and Lisp on separate
;;; machines have a chance to integrate file operations somehow.
(defvar sly-to-lisp-filename-function #'convert-standard-filename
"Function to translate Emacs filenames to CL namestrings.")
(defvar sly-from-lisp-filename-function #'identity
"Function to translate CL namestrings to Emacs filenames.")
(defun sly-to-lisp-filename (filename)
"Translate the string FILENAME to a Lisp filename."
(funcall sly-to-lisp-filename-function (substring-no-properties filename)))
(defun sly-from-lisp-filename (filename)
"Translate the Lisp filename FILENAME to an Emacs filename."
(funcall sly-from-lisp-filename-function filename))