-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathslime.el
10291 lines (8980 loc) · 385 KB
/
slime.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
;;; slime.el --- Superior Lisp Interaction Mode for Emacs
;;
;;;; License
;; Copyright (C) 2003 Eric Marsden, Luke Gorrie, Helmut Eller
;; Copyright (C) 2004,2005,2006 Luke Gorrie, Helmut Eller
;;
;; 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 2 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, write to the Free
;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
;; MA 02111-1307, USA.
;;;; Commentary
;;
;; This file contains extensions for programming in Common Lisp. The
;; main features are:
;;
;; A socket-based communication/RPC interface between Emacs and
;; Lisp.
;;
;; The `slime-mode' minor-mode complementing `lisp-mode'. This new
;; mode includes many commands for interacting with the Common Lisp
;; process.
;;
;; Common Lisp REPL (Read-Eval-Print Loop) written in Emacs Lisp,
;; similar to `ielm'.
;;
;; Common Lisp debugger written in Emacs Lisp. The debugger pops up
;; an Emacs buffer similar to the Emacs/Elisp debugger.
;;
;; Trapping compiler messages and creating annotations in the source
;; file on the appropriate forms.
;;
;; SLIME is compatible with GNU Emacs 20 and 21 and XEmacs 21. In
;; order to run SLIME requires a supporting Lisp server called
;; Swank. Swank is distributed with slime.el and will automatically be
;; started in a normal installation.
;;;; Dependencies and setup
(eval-and-compile
(require 'cl)
(unless (fboundp 'define-minor-mode)
(require 'easy-mmode)
(defalias 'define-minor-mode 'easy-mmode-define-minor-mode))
(when (locate-library "hyperspec")
(require 'hyperspec)))
(require 'comint)
(require 'timer)
(require 'pp)
(require 'hideshow)
(require 'font-lock)
(when (featurep 'xemacs)
(require 'overlay))
(require 'easymenu)
(eval-when (compile)
(require 'arc-mode)
(require 'apropos)
(require 'outline)
(require 'etags))
(eval-and-compile
(defvar slime-path
(let ((path (or (locate-library "slime") load-file-name)))
(and path (file-name-directory path)))
"Directory containing the Slime package.
This is used to load the supporting Common Lisp library, Swank.
The default value is automatically computed from the location of the
Emacs Lisp package."))
(defvar slime-lisp-modes '(lisp-mode))
(defun slime-setup (&optional contribs)
"Setup Emacs so that lisp-mode buffers always use SLIME.
CONTRIBS is a list of contrib packages to load."
(when (member 'lisp-mode slime-lisp-modes)
(add-hook 'lisp-mode-hook 'slime-lisp-mode-hook))
(when contribs
(add-to-list 'load-path (expand-file-name "contrib" slime-path))
(dolist (c contribs)
(require c)
(let ((init (intern (format "%s-init" c))))
(when (fboundp init)
(funcall init))))))
(defun slime-lisp-mode-hook ()
(slime-mode 1)
(set (make-local-variable 'lisp-indent-function)
'common-lisp-indent-function))
(eval-and-compile
(defun slime-changelog-date ()
"Return the datestring of the latest entry in the ChangeLog file.
Return nil if the ChangeLog file cannot be found."
(let ((changelog (concat slime-path "ChangeLog")))
(if (file-exists-p changelog)
(with-temp-buffer
(insert-file-contents-literally changelog nil 0 100)
(goto-char (point-min))
(symbol-name (read (current-buffer))))
nil))))
(defvar slime-protocol-version nil)
(setq slime-protocol-version
(eval-when-compile (slime-changelog-date)))
;;;; Customize groups
;;
;;;;; slime
(defgroup slime nil
"Interaction with the Superior Lisp Environment."
:prefix "slime-"
:group 'applications)
;;;;; slime-ui
(defgroup slime-ui nil
"Interaction with the Superior Lisp Environment."
:prefix "slime-"
:group 'slime)
(defcustom slime-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 'slime-ui)
(defcustom slime-extended-modeline t
"If non-nil, display various information in the mode line of a
Lisp buffer. The information includes the current connection of
that buffer, the buffer package, and some state indication."
:type 'boolean
:group 'slime-ui)
(defcustom slime-kill-without-query-p nil
"If non-nil, kill SLIME processes without query when quitting Emacs.
This applies to the *inferior-lisp* buffer and the network connections."
:type 'boolean
:group 'slime-ui)
;;;;; slime-lisp
(defgroup slime-lisp nil
"Lisp server configuration."
:prefix "slime-"
:group 'slime)
(defcustom slime-backend "swank-loader.lisp"
"The name of the Lisp file that loads the Swank server.
This name is interpreted relative to the directory containing
slime.el, but could also be set to an absolute filename."
:type 'string
:group 'slime-lisp)
(defcustom slime-connected-hook nil
"List of functions to call when SLIME connects to Lisp."
:type 'hook
:group 'slime-lisp)
(defcustom slime-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 'slime-lisp)
(defcustom slime-lisp-host "127.0.0.1"
"The default hostname (or IP address) to connect to."
:type 'string
:group 'slime-lisp)
(defcustom slime-port 4005
"Port to use as the default for `slime-connect'."
:type 'integer
:group 'slime-lisp)
;;;;; slime-mode
(defgroup slime-mode nil
"Settings for slime-mode Lisp source buffers."
:prefix "slime-"
:group 'slime)
(defcustom slime-find-definitions-function 'slime-find-definitions-rpc
"Function to find definitions for a name.
The function is called with the definition name, a string, as its
argument."
:type 'function
:group 'slime-mode
:options '(slime-find-definitions-rpc
slime-etags-definitions
(lambda (name)
(append (slime-find-definitions-rpc name)
(slime-etags-definitions name)))
(lambda (name)
(or (slime-find-definitions-rpc name)
(and tags-table-list
(slime-etags-definitions name))))))
(defcustom slime-complete-symbol-function 'slime-simple-complete-symbol
"*Function to perform symbol completion."
:group 'slime-mode
:type '(choice (const :tag "Simple" slime-simple-complete-symbol)
(const :tag "Compound" slime-complete-symbol*)
(const :tag "Fuzzy" slime-fuzzy-complete-symbol)))
(defcustom slime-when-complete-filename-expand nil
"Use comint-replace-by-expanded-filename instead of
comint-dynamic-complete-as-filename to complete file names"
:group 'slime-mode
:type 'boolean)
(defcustom slime-space-information-p t
"Have the SPC key offer arglist information."
:type 'boolean
:group 'slime-mode)
;;;;; slime-mode-faces
(defgroup slime-mode-faces nil
"Faces in slime-mode source code buffers."
:prefix "slime-"
:group 'slime-mode)
(defun slime-underline-color (color)
"Return a legal value for the :underline face attribute based on COLOR."
;; In XEmacs the :underline attribute can only be a boolean.
;; In GNU it can be the name of a colour.
(if (featurep 'xemacs)
(if color t nil)
color))
(defface slime-error-face
`((((class color) (background light))
(:underline ,(slime-underline-color "red")))
(((class color) (background dark))
(:underline ,(slime-underline-color "red")))
(t (:underline t)))
"Face for errors from the compiler."
:group 'slime-mode-faces)
(defface slime-warning-face
`((((class color) (background light))
(:underline ,(slime-underline-color "orange")))
(((class color) (background dark))
(:underline ,(slime-underline-color "coral")))
(t (:underline t)))
"Face for warnings from the compiler."
:group 'slime-mode-faces)
(defface slime-style-warning-face
`((((class color) (background light))
(:underline ,(slime-underline-color "brown")))
(((class color) (background dark))
(:underline ,(slime-underline-color "gold")))
(t (:underline t)))
"Face for style-warnings from the compiler."
:group 'slime-mode-faces)
(defface slime-note-face
`((((class color) (background light))
(:underline ,(slime-underline-color "brown4")))
(((class color) (background dark))
(:underline ,(slime-underline-color "light goldenrod")))
(t (:underline t)))
"Face for notes from the compiler."
:group 'slime-mode-faces)
(defun slime-face-inheritance-possible-p ()
"Return true if the :inherit face attribute is supported."
(assq :inherit custom-face-attributes))
(defface slime-highlight-face
(if (slime-face-inheritance-possible-p)
'((t (:inherit highlight :underline nil)))
'((((class color) (background light))
(:background "darkseagreen2"))
(((class color) (background dark))
(:background "darkolivegreen"))
(t (:inverse-video t))))
"Face for compiler notes while selected."
:group 'slime-mode-faces)
;;;;; sldb
(defgroup slime-debugger nil
"Backtrace options and fontification."
:prefix "sldb-"
:group 'slime)
(defmacro define-sldb-faces (&rest faces)
"Define the set of SLDB faces.
Each face specifiation is (NAME DESCRIPTION &optional PROPERTIES).
NAME is a symbol; the face will be called sldb-NAME-face.
DESCRIPTION is a one-liner for the customization buffer.
PROPERTIES specifies any default face properties."
`(progn ,@(loop for face in faces
collect `(define-sldb-face ,@face))))
(defmacro define-sldb-face (name description &optional default)
(let ((facename (intern (format "sldb-%s-face" (symbol-name name)))))
`(defface ,facename
(list (list t ,default))
,(format "Face for %s." description)
:group 'slime-debugger)))
(define-sldb-faces
(topline "the top line describing the error")
(condition "the condition class")
(section "the labels of major sections in the debugger buffer")
(frame-label "backtrace frame numbers")
(restart-type "restart names."
(if (slime-face-inheritance-possible-p)
'(:inherit font-lock-keyword-face)))
(restart "restart descriptions")
(restart-number "restart numbers (correspond to keystrokes to invoke)"
'(:bold t))
(frame-line "function names and arguments in the backtrace")
(restartable-frame-line
"frames which are surely restartable")
(non-restartable-frame-line
"frames which are surely not restartable")
(detailed-frame-line
"function names and arguments in a detailed (expanded) frame")
(local-name "local variable names")
(local-value "local variable values")
(catch-tag "catch tags"))
;;;;; slime-repl
(defgroup slime-repl nil
"The Read-Eval-Print Loop (*slime-repl* buffer)."
:prefix "slime-repl-"
:group 'slime)
(defcustom slime-repl-shortcut-dispatch-char ?\,
"Character used to distinguish repl commands from lisp forms."
:type '(character)
:group 'slime-repl)
(defcustom slime-repl-only-save-lisp-buffers t
"When T we only attempt to save lisp-mode file buffers. When
NIL slime will attempt to save all buffers (as per
save-some-buffers). This applies to all ASDF related repl
shortcuts."
:type '(boolean)
:group 'slime-repl)
(defface slime-repl-prompt-face
(if (slime-face-inheritance-possible-p)
'((t (:inherit font-lock-keyword-face)))
'((((class color) (background light)) (:foreground "Purple"))
(((class color) (background dark)) (:foreground "Cyan"))
(t (:weight bold))))
"Face for the prompt in the SLIME REPL."
:group 'slime-repl)
(defface slime-repl-output-face
(if (slime-face-inheritance-possible-p)
'((t (:inherit font-lock-string-face)))
'((((class color) (background light)) (:foreground "RosyBrown"))
(((class color) (background dark)) (:foreground "LightSalmon"))
(t (:slant italic))))
"Face for Lisp output in the SLIME REPL."
:group 'slime-repl)
(defface slime-repl-input-face
'((t (:bold t)))
"Face for previous input in the SLIME REPL."
:group 'slime-repl)
(defface slime-repl-result-face
'((t ()))
"Face for the result of an evaluation in the SLIME REPL."
:group 'slime-repl)
(defcustom slime-repl-history-file "~/.slime-history.eld"
"File to save the persistent REPL history to."
:type 'string
:group 'slime-repl)
(defcustom slime-repl-history-size 200
"*Maximum number of lines for persistent REPL history."
:type 'integer
:group 'slime-repl)
;;;; Minor modes
;;;;; slime-mode
(define-minor-mode slime-mode
"\\<slime-mode-map>\
SLIME: The Superior Lisp Interaction Mode for Emacs (minor-mode).
Commands to compile the current buffer's source file and visually
highlight any resulting compiler notes and warnings:
\\[slime-compile-and-load-file] - Compile and load the current buffer's file.
\\[slime-compile-file] - Compile (but not load) the current buffer's file.
\\[slime-compile-defun] - Compile the top-level form at point.
Commands for visiting compiler notes:
\\[slime-next-note] - Goto the next form with a compiler note.
\\[slime-previous-note] - Goto the previous form with a compiler note.
\\[slime-remove-notes] - Remove compiler-note annotations in buffer.
Finding definitions:
\\[slime-edit-definition] - Edit the definition of the function called at point.
\\[slime-pop-find-definition-stack] - Pop the definition stack to go back from a definition.
Documentation commands:
\\[slime-describe-symbol] - Describe symbol.
\\[slime-apropos] - Apropos search.
\\[slime-disassemble-symbol] - Disassemble a function.
Evaluation commands:
\\[slime-eval-defun] - Evaluate top-level from containing point.
\\[slime-eval-last-expression] - Evaluate sexp before point.
\\[slime-pprint-eval-last-expression] - Evaluate sexp before point, pretty-print result.
Full set of commands:
\\{slime-mode-map}"
nil
nil
;; Fake binding to coax `define-minor-mode' to create the keymap
'((" " 'undefined)))
(make-variable-buffer-local
(defvar slime-modeline-string nil
"The string that should be displayed in the modeline if
`slime-extended-modeline' is true, and which indicates the
current connection, package and state of a Lisp buffer.
The string is periodically updated by an idle timer."))
;;; These are used to keep track of old values, so we can determine
;;; whether the mode line has changed, and should be updated.
(make-variable-buffer-local
(defvar slime-modeline-package nil))
(make-variable-buffer-local
(defvar slime-modeline-connection-name nil))
(make-variable-buffer-local
(defvar slime-modeline-connection-state nil))
(defun slime-compute-modeline-package ()
(when (memq major-mode slime-lisp-modes)
;; WHEN-LET is defined later.
(let ((package (slime-current-package)))
(when package
(slime-pretty-package-name package)))))
(defun slime-pretty-package-name (name)
"Return a pretty version of a package name NAME."
(let ((name (cond ((string-match "^#?:\\(.*\\)$" name)
(match-string 1 name))
((string-match "^\"\\(.*\\)\"$" name)
(match-string 1 name))
(t name))))
(format "%s" (read name))))
(defun slime-compute-modeline-connection ()
(let ((conn (slime-current-connection)))
(if (or (null conn) (slime-stale-connection-p conn))
nil
(slime-connection-name conn))))
(defun slime-compute-modeline-connection-state ()
(let* ((conn (slime-current-connection))
(new-state (slime-compute-connection-state conn)))
(if (eq new-state :connected)
(let ((rex-cs (length (slime-rex-continuations)))
(sldb-cs (length (sldb-debugged-continuations conn)))
;; There can be SLDB buffers which have no continuations
;; attached to it, e.g. the one resulting from
;; `slime-interrupt'.
(sldbs (length (sldb-buffers conn))))
(cond ((and (= sldbs 0) (zerop rex-cs)) nil)
((= sldbs 0) (format "%s" rex-cs))
(t (format "%s/%s"
(if (= rex-cs 0) 0 (- rex-cs sldb-cs))
sldbs))))
(slime-connection-state-as-string new-state))))
(defun slime-compute-modeline-string (conn state pkg)
(concat (when (or conn pkg) "[")
(when pkg (format "%s" pkg))
(when (and (or conn state) pkg) ", ")
(when conn (format "%s" conn))
(when state (format "{%s}" state))
(when (or conn pkg) "]")))
(defun slime-update-modeline-string ()
(let ((old-pkg slime-modeline-package)
(old-conn slime-modeline-connection-name)
(old-state slime-modeline-connection-state)
(new-pkg (slime-compute-modeline-package))
(new-conn (slime-compute-modeline-connection))
(new-state (slime-compute-modeline-connection-state)))
(when (or (not (equal old-pkg new-pkg))
(not (equal old-conn new-conn))
(not (equal old-state new-state)))
(setq slime-modeline-package new-pkg)
(setq slime-modeline-connection-name new-conn)
(setq slime-modeline-connection-state new-state)
(setq slime-modeline-string
(slime-compute-modeline-string new-conn new-state new-pkg)))))
(defun slime-shall-we-update-modeline-p ()
(and slime-extended-modeline
(or slime-mode slime-popup-buffer-mode)))
(defun slime-update-all-modelines ()
(dolist (window (window-list))
(with-current-buffer (window-buffer window)
(when (slime-shall-we-update-modeline-p)
(slime-update-modeline-string)
(force-mode-line-update)))))
(defvar slime-modeline-update-timer nil)
(defun slime-restart-or-init-modeline-update-timer ()
(when slime-modeline-update-timer
(cancel-timer slime-modeline-update-timer))
(setq slime-modeline-update-timer
(run-with-idle-timer 0.5 0.5 'slime-update-all-modelines)))
(slime-restart-or-init-modeline-update-timer)
(defun slime-recompute-modelines (delay)
(cond (delay
;; Minimize flashing of modeline due to short lived
;; requests such as those of autodoc.
(slime-restart-or-init-modeline-update-timer))
(t
;; Must do this ourselves since emacs may have
;; been idling long enough that
;; SLIME-MODELINE-UPDATE-TIMER is not going to
;; trigger by itself.
(slime-update-all-modelines))))
;; Setup the mode-line to say when we're in slime-mode, which
;; connection is active, and which CL package we think the current
;; buffer belongs to.
(add-to-list 'minor-mode-alist
'(slime-mode
(" Slime" slime-modeline-string)))
;;;;; Key bindings
;; See `slime-define-key' below for keyword meanings.
(defvar slime-keys
'(;; Compiler notes
("\M-p" slime-previous-note)
("\M-n" slime-next-note)
("\M-c" slime-remove-notes :prefixed t)
("\C-k" slime-compile-and-load-file :prefixed t)
("\M-k" slime-compile-file :prefixed t)
("\C-c" slime-compile-defun :prefixed t)
("\C-l" slime-load-file :prefixed t)
;; Editing/navigating
("\M-\C-i" slime-complete-symbol :inferior t)
("\C-i" slime-complete-symbol :prefixed t :inferior t)
("\M-." slime-edit-definition :inferior t :sldb t)
("\C-x4." slime-edit-definition-other-window :inferior t :sldb t)
("\C-x5." slime-edit-definition-other-frame :inferior t :sldb t)
("\M-," slime-pop-find-definition-stack :inferior t :sldb t)
;; Evaluating
("\C-x\C-e" slime-eval-last-expression :inferior t)
("\C-x\M-e" slime-eval-last-expression-display-output :inferior t)
("\C-p" slime-pprint-eval-last-expression :prefixed t :inferior t)
("\C-r" slime-eval-region :prefixed t :inferior t)
("\C-\M-x" slime-eval-defun)
(":" slime-interactive-eval :prefixed t :sldb t)
("\C-e" slime-interactive-eval :prefixed t :sldb t :inferior t)
("\C-y" slime-call-defun :prefixed t)
("E" slime-edit-value :prefixed t :sldb t :inferior t)
("\C-z" slime-switch-to-output-buffer :prefixed t :sldb t)
("\C-b" slime-interrupt :prefixed t :inferior t :sldb t)
("\M-g" slime-quit :prefixed t :inferior t :sldb t)
;; Documentation
(" " slime-space :inferior t)
("\C-f" slime-describe-function :prefixed t :inferior t :sldb t)
("\M-d" slime-disassemble-symbol :prefixed t :inferior t :sldb t)
("\C-t" slime-toggle-trace-fdefinition :prefixed t :sldb t)
("\C-u" slime-undefine-function :prefixed t)
("\C-m" slime-macroexpand-1 :prefixed t :inferior t)
("\M-m" slime-macroexpand-all :prefixed t :inferior t)
("\M-0" slime-restore-window-configuration :prefixed t :inferior t)
([(control meta ?\.)] slime-next-location :inferior t)
("~" slime-sync-package-and-default-directory :prefixed t :inferior t)
("\M-p" slime-repl-set-package :prefixed t :inferior t)
;; Cross reference
("<" slime-list-callers :prefixed t :inferior t :sldb t)
(">" slime-list-callees :prefixed t :inferior t :sldb t)
;; "Other"
("\I" slime-inspect :prefixed t :inferior t :sldb t)
("\C-]" slime-close-all-parens-in-sexp :prefixed t :inferior t :sldb t)
("\C-xt" slime-list-threads :prefixed t :inferior t :sldb t)
("\C-xc" slime-list-connections :prefixed t :inferior t :sldb t)
;; ;; Shadow unwanted bindings from inf-lisp
;; ("\C-a" slime-nop :prefixed t :inferior t :sldb t)
;; ("\C-v" slime-nop :prefixed t :inferior t :sldb t)
))
(defun slime-nop ()
"The null command. Used to shadow currently-unused keybindings."
(interactive)
(call-interactively 'undefined))
(defvar slime-doc-map (make-sparse-keymap)
"Keymap for documentation commands. Bound to a prefix key.")
(defvar slime-doc-bindings
'((?a slime-apropos)
(?z slime-apropos-all)
(?p slime-apropos-package)
(?d slime-describe-symbol)
(?f slime-describe-function)
(?h slime-hyperspec-lookup)
(?~ common-lisp-hyperspec-format)))
(defvar slime-who-map (make-sparse-keymap)
"Keymap for who-xref commands. Bound to a prefix key.")
(defvar slime-who-bindings
'((?c slime-who-calls)
(?w slime-calls-who)
(?r slime-who-references)
(?b slime-who-binds)
(?s slime-who-sets)
(?m slime-who-macroexpands)
(?a slime-who-specializes)))
;; Maybe a good idea, maybe not..
(defvar slime-prefix-key "\C-c"
"The prefix key to use in SLIME keybinding sequences.")
(defvar slime-prefix-map (make-sparse-keymap)
"Keymap for commands prefixed with `slime-prefix-key'.")
(defun* slime-define-key (key command &key prefixed)
"Define a keybinding of KEY for COMMAND.
If PREFIXED is non-nil, `slime-prefix-key' is prepended to KEY."
(cond (prefixed (define-key slime-prefix-map key command))
(t (define-key slime-mode-map key command))))
(defun slime-init-keymaps ()
"(Re)initialize the keymaps for `slime-mode'."
(interactive)
(setq slime-prefix-map (make-sparse-keymap))
(define-key slime-mode-map slime-prefix-key slime-prefix-map)
(loop for (key command . keys) in slime-keys
do (apply #'slime-define-key key command :allow-other-keys t keys))
;; Documentation
(setq slime-doc-map (make-sparse-keymap))
(slime-define-both-key-bindings slime-doc-map slime-doc-bindings)
;; C-c C-d is the prefix for the doc map.
(slime-define-key "\C-d" slime-doc-map :prefixed t)
;; Who-xref
(setq slime-who-map (make-sparse-keymap))
(slime-define-both-key-bindings slime-who-map slime-who-bindings)
;; C-c C-w is the prefix for the who-xref map.
(slime-define-key "\C-w" slime-who-map :prefixed t))
(defun slime-define-both-key-bindings (keymap bindings)
(loop for (char command) in bindings do
;; We bind both unmodified and with control.
(define-key keymap `[,char] command)
(unless (equal char ?h) ; But don't bind C-h
(define-key keymap `[(control ,char)] command))))
(slime-init-keymaps)
;;;; Setup initial `slime-mode' hooks
(make-variable-buffer-local
(defvar slime-pre-command-actions nil
"List of functions to execute before the next Emacs command.
This list of flushed between commands."))
(defun slime-pre-command-hook ()
"Execute all functions in `slime-pre-command-actions', then NIL it."
(dolist (undo-fn slime-pre-command-actions)
(ignore-errors (funcall undo-fn)))
(setq slime-pre-command-actions nil))
(defun slime-post-command-hook ()
(when (null pre-command-hook) ; sometimes this is lost
(add-hook 'pre-command-hook 'slime-pre-command-hook)))
(defun slime-setup-command-hooks ()
"Setup a buffer-local `pre-command-hook' to call `slime-pre-command-hook'."
(slime-add-local-hook 'pre-command-hook 'slime-pre-command-hook)
(slime-add-local-hook 'post-command-hook 'slime-post-command-hook))
;;;; Framework'ey bits
;;;
;;; This section contains some standard SLIME 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
(defmacro* when-let ((var value) &rest body)
"Evaluate VALUE, and if the result is non-nil bind it to VAR and
evaluate BODY.
\(fn (VAR VALUE) &rest BODY)"
`(let ((,var ,value))
(when ,var ,@body)))
(put 'when-let 'lisp-indent-function 1)
(defmacro destructure-case (value &rest patterns)
"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 (gensym "op-"))
(operands (gensym "rand-"))
(tmp (gensym "tmp-")))
`(let* ((,tmp ,value)
(,operator (car ,tmp))
(,operands (cdr ,tmp)))
(case ,operator
,@(mapcar (lambda (clause)
(if (eq (car clause) t)
`(t ,@(cdr clause))
(destructuring-bind ((op &rest rands) &rest body) clause
`(,op (destructuring-bind ,rands ,operands
. ,body)))))
patterns)
,@(if (eq (caar (last patterns)) t)
'()
`((t (error "Elisp destructure-case failed: %S" ,tmp))))))))
(put 'destructure-case 'lisp-indent-function 1)
(defmacro slime-define-keys (keymap &rest key-command)
"Define keys in KEYMAP. Each KEY-COMMAND is a list of (KEY COMMAND)."
`(progn . ,(mapcar (lambda (k-c) `(define-key ,keymap . ,k-c))
key-command)))
(put 'slime-define-keys 'lisp-indent-function 1)
(defmacro* with-struct ((conc-name &rest slots) struct &body body)
"Like with-slots but works only for structs.
\(fn (CONC-NAME &rest SLOTS) STRUCT &body BODY)"
(flet ((reader (slot) (intern (concat (symbol-name conc-name)
(symbol-name slot)))))
(let ((struct-var (gensym "struct")))
`(let ((,struct-var ,struct))
(symbol-macrolet
,(mapcar (lambda (slot)
(etypecase slot
(symbol `(,slot (,(reader slot) ,struct-var)))
(cons `(,(first slot) (,(reader (second slot))
,struct-var)))))
slots)
. ,body)))))
(put 'with-struct 'lisp-indent-function 2)
;;;;; Very-commonly-used functions
(defvar slime-message-function 'message)
;; Interface
(defun slime-message (format &rest args)
"Like `message' but with special support for multi-line messages.
Single-line messages use the echo area."
(apply slime-message-function format args))
(when (or (featurep 'xemacs)
(= emacs-major-version 20))
(setq slime-message-function 'slime-format-display-message))
(defun slime-format-display-message (format &rest args)
(slime-display-message (apply #'format format args) "*SLIME Note*"))
(defun slime-display-message (message buffer-name)
"Display MESSAGE in the echo area or in BUFFER-NAME.
Use the echo area if MESSAGE needs only a single line. If the MESSAGE
requires more than one line display it in BUFFER-NAME and add a hook
to `slime-pre-command-actions' to remove the window before the next
command."
(when (get-buffer-window buffer-name) (delete-windows-on buffer-name))
(cond ((or (string-match "\n" message)
(> (length message) (1- (frame-width))))
(lexical-let ((buffer (get-buffer-create buffer-name)))
(with-current-buffer buffer
(erase-buffer)
(insert message)
(goto-char (point-min))
(let ((win (slime-create-message-window)))
(set-window-buffer win (current-buffer))
(shrink-window-if-larger-than-buffer
(display-buffer (current-buffer)))))
(push (lambda () (delete-windows-on buffer) (bury-buffer buffer))
slime-pre-command-actions)))
(t (message "%s" message))))
(defun slime-create-message-window ()
"Create a window at the bottom of the frame, above the minibuffer."
(let ((previous (previous-window (minibuffer-window))))
(when (<= (window-height previous) (* 2 window-min-height))
(save-selected-window
(select-window previous)
(enlarge-window (- (1+ (* 2 window-min-height))
(window-height previous)))))
(split-window previous)))
(defvar slime-background-message-function 'slime-display-oneliner)
;; Interface
(defun slime-background-message (format-string &rest format-args)
"Display a message in passing.
This is like `slime-message', but less distracting because it
will never pop up a buffer or display multi-line messages.
It should be used for \"background\" messages such as argument lists."
(apply slime-background-message-function format-string format-args))
(defun slime-display-oneliner (format-string &rest format-args)
(let* ((msg (apply #'format format-string format-args)))
(unless (minibuffer-window-active-p (minibuffer-window))
(message "%s" (slime-oneliner msg)))))
(defun slime-oneliner (string)
"Return STRING truncated to fit in a single echo-area line."
(substring string 0 (min (length string)
(or (position ?\n string) most-positive-fixnum)
(1- (frame-width)))))
;; Interface
(defun slime-set-truncate-lines ()
"Apply `slime-truncate-lines' to the current buffer."
(when slime-truncate-lines
(set (make-local-variable 'truncate-lines) t)))
;; Interface
(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)))
;; Interface
(defun slime-read-symbol-name (prompt &optional query)
"Either read a symbol name or choose the one at point.
The user is prompted if a prefix argument is in effect, if there is no
symbol at point, or if QUERY is non-nil.
This function avoids mistaking the REPL prompt for a symbol."
(cond ((or current-prefix-arg query (not (slime-symbol-name-at-point)))
(slime-read-from-minibuffer prompt (slime-symbol-name-at-point)))
(t (slime-symbol-name-at-point))))
;; Interface
(defmacro slime-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."
(let ((start (gensym)))
`(let ((,start (point)))
(prog1 (progn ,@body)
(add-text-properties ,start (point) ,props)))))
(put 'slime-propertize-region 'lisp-indent-function 1)
;; Interface
(defsubst slime-insert-propertized (props &rest args)
"Insert all ARGS and then add text-PROPS to the inserted text."
(slime-propertize-region props (apply #'insert args)))
(defmacro slime-with-rigid-indentation (level &rest body)
"Execute BODY and then rigidly indent its text insertions.
Assumes all insertions are made at point."
(let ((start (gensym)) (l (gensym)))
`(let ((,start (point)) (,l ,(or level '(current-column))))
(prog1 (progn ,@body)
(slime-indent-rigidly ,start (point) ,l)))))
(put 'slime-with-rigid-indentation 'lisp-indent-function 1)
(defun slime-indent-rigidly (start end column)
;; Similar to `indent-rigidly' but doesn't inherit text props.
(save-excursion
(goto-char end)
(beginning-of-line)
(while (and (<= start (point))
(progn
(save-excursion (insert-char ?\ column))
(zerop (forward-line -1)))))))
(defun slime-insert-indented (&rest strings)
"Insert all arguments rigidly indented."
(slime-with-rigid-indentation nil
(apply #'insert strings)))
(defun slime-curry (fun &rest args)
`(lambda (&rest more) (apply ',fun (append ',args more))))
(defun slime-rcurry (fun &rest args)
`(lambda (&rest more) (apply ',fun (append more ',args))))
;;;;; Snapshots of current Emacs state
;;; Window configurations do not save (and hence not restore)
;;; any narrowing that could be applied to a buffer.
;;;
;;; For this purpose, we introduce a superset of a window
;;; configuration that does include the necessary information to
;;; properly restore narrowing.
;;;
;;; We call this superset an Emacs Snapshot.
(defstruct (slime-narrowing-configuration
(:conc-name slime-narrowing-configuration.))
narrowedp beg end)
(defstruct (slime-emacs-snapshot (:conc-name slime-emacs-snapshot.))
;; We explicitly store the value of point even though it's implicitly
;; stored in the window-configuration because Emacs provides no
;; way to access the things stored in a window configuration.
window-configuration narrowing-configuration point-marker)
(defun slime-current-narrowing-configuration (&optional buffer)
(with-current-buffer (or buffer (current-buffer))
(make-slime-narrowing-configuration :narrowedp (slime-buffer-narrowed-p)
:beg (point-min-marker)
:end (point-max-marker))))
(defun slime-set-narrowing-configuration (narrowing-cfg)
(when (slime-narrowing-configuration.narrowedp narrowing-cfg)
(narrow-to-region (slime-narrowing-configuration.beg narrowing-cfg)
(slime-narrowing-configuration.end narrowing-cfg))))
(defun slime-current-emacs-snapshot (&optional frame)
"Returns a snapshot of the current state of FRAME, or the
currently active frame if FRAME is not given respectively."
(with-current-buffer
(if frame
(window-buffer (frame-selected-window (selected-frame)))
(current-buffer))
(make-slime-emacs-snapshot
:window-configuration (current-window-configuration frame)
:narrowing-configuration (slime-current-narrowing-configuration)
:point-marker (point-marker))))
(defun slime-set-emacs-snapshot (snapshot)
"Restores the state of Emacs according to the information saved
in SNAPSHOT."
(let ((window-cfg (slime-emacs-snapshot.window-configuration snapshot))
(narrowing-cfg (slime-emacs-snapshot.narrowing-configuration snapshot))
(marker (slime-emacs-snapshot.point-marker snapshot)))
(set-window-configuration window-cfg) ; restores previously current buffer.
(slime-set-narrowing-configuration narrowing-cfg)
(goto-char (marker-position marker))))
(defun slime-current-emacs-snapshot-fingerprint (&optional frame)
"Return a fingerprint of the current emacs snapshot.
Fingerprints are `equalp' if and only if they represent window
configurations that are very similar (same windows and buffers.)
Unlike real window-configuration objects, fingerprints are not
sensitive to the point moving and they can't be restored."
(mapcar (lambda (window) (list window (window-buffer window)))
(slime-frame-windows frame)))
(defun slime-frame-windows (&optional frame)
"Return the list of windows in FRAME."
(loop with last-window = (previous-window (frame-first-window frame))
for window = (frame-first-window frame) then (next-window window)
collect window
until (eq window last-window)))
;;;;; Temporary popup buffers