/[slime]/slime/swank.lisp
ViewVC logotype

Contents of /slime/swank.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.567 - (show annotations)
Tue Aug 12 12:56:57 2008 UTC (5 years, 8 months ago) by heller
Branch: MAIN
Changes since 1.566: +29 -24 lines
Let SIGINT create a synthetic event.

* swank.lisp (install-fd-handler,simple-serve-requests): Dispatch
a :emacs-interrupt event in the SIGINT handler.

* slime.el (slime-interrupt): Send nothing over the wire when
SIGINT is used.
1 ;;; -*- outline-regexp:";;;;;*" indent-tabs-mode:nil coding:latin-1-unix -*-
2 ;;;
3 ;;; This code has been placed in the Public Domain. All warranties
4 ;;; are disclaimed.
5 ;;;
6 ;;;; swank.lisp
7 ;;;
8 ;;; This file defines the "Swank" TCP server for Emacs to talk to. The
9 ;;; code in this file is purely portable Common Lisp. We do require a
10 ;;; smattering of non-portable functions in order to write the server,
11 ;;; so we have defined them in `swank-backend.lisp' and implemented
12 ;;; them separately for each Lisp implementation. These extensions are
13 ;;; available to us here via the `SWANK-BACKEND' package.
14
15 (defpackage :swank
16 (:use :cl :swank-backend)
17 (:export #:startup-multiprocessing
18 #:start-server
19 #:create-server
20 #:stop-server
21 #:restart-server
22 #:ed-in-emacs
23 #:inspect-in-emacs
24 #:print-indentation-lossage
25 #:invoke-slime-debugger
26 #:swank-debugger-hook
27 #:emacs-inspect
28 ;;#:inspect-slot-for-emacs
29 ;; These are user-configurable variables:
30 #:*communication-style*
31 #:*dont-close*
32 #:*log-events*
33 #:*log-output*
34 #:*use-dedicated-output-stream*
35 #:*dedicated-output-stream-port*
36 #:*configure-emacs-indentation*
37 #:*readtable-alist*
38 #:*globally-redirect-io*
39 #:*global-debugger*
40 #:*sldb-printer-bindings*
41 #:*swank-pprint-bindings*
42 #:*default-worker-thread-bindings*
43 #:*macroexpand-printer-bindings*
44 #:*record-repl-results*
45 #:*debug-on-swank-error*
46 ;; These are re-exported directly from the backend:
47 #:buffer-first-change
48 #:frame-source-location-for-emacs
49 #:restart-frame
50 #:sldb-step
51 #:sldb-break
52 #:sldb-break-on-return
53 #:profiled-functions
54 #:profile-report
55 #:profile-reset
56 #:unprofile-all
57 #:profile-package
58 #:default-directory
59 #:set-default-directory
60 #:quit-lisp
61 #:with-swank-compilation-unit))
62
63 (in-package :swank)
64
65
66 ;;;; Top-level variables, constants, macros
67
68 (defconstant cl-package (find-package :cl)
69 "The COMMON-LISP package.")
70
71 (defconstant keyword-package (find-package :keyword)
72 "The KEYWORD package.")
73
74 (defvar *canonical-package-nicknames*
75 `((:common-lisp-user . :cl-user))
76 "Canonical package names to use instead of shortest name/nickname.")
77
78 (defvar *auto-abbreviate-dotted-packages* t
79 "Abbreviate dotted package names to their last component if T.")
80
81 (defvar *swank-io-package*
82 (let ((package (make-package :swank-io-package :use '())))
83 (import '(nil t quote) package)
84 package))
85
86 (defconstant default-server-port 4005
87 "The default TCP port for the server (when started manually).")
88
89 (defvar *swank-debug-p* t
90 "When true, print extra debugging information.")
91
92 (defvar *redirect-io* t
93 "When non-nil redirect Lisp standard I/O to Emacs.
94 Redirection is done while Lisp is processing a request for Emacs.")
95
96 (defvar *sldb-printer-bindings*
97 `((*print-pretty* . t)
98 (*print-level* . 4)
99 (*print-length* . 10)
100 (*print-circle* . t)
101 (*print-readably* . nil)
102 (*print-pprint-dispatch* . ,(copy-pprint-dispatch nil))
103 (*print-gensym* . t)
104 (*print-base* . 10)
105 (*print-radix* . nil)
106 (*print-array* . t)
107 (*print-lines* . 10)
108 (*print-escape* . t)
109 (*print-right-margin* . 65))
110 "A set of printer variables used in the debugger.")
111
112 (defvar *backtrace-printer-bindings*
113 `((*print-pretty* . nil)
114 (*print-level* . 4)
115 (*print-length* . 6))
116 "Pretter settings for printing backtraces.")
117
118 (defvar *default-worker-thread-bindings* '()
119 "An alist to initialize dynamic variables in worker threads.
120 The list has the form ((VAR . VALUE) ...). Each variable VAR will be
121 bound to the corresponding VALUE.")
122
123 (defun call-with-bindings (alist fun)
124 "Call FUN with variables bound according to ALIST.
125 ALIST is a list of the form ((VAR . VAL) ...)."
126 (let* ((rlist (reverse alist))
127 (vars (mapcar #'car rlist))
128 (vals (mapcar #'cdr rlist)))
129 (progv vars vals
130 (funcall fun))))
131
132 (defmacro with-bindings (alist &body body)
133 "See `call-with-bindings'."
134 `(call-with-bindings ,alist (lambda () ,@body)))
135
136 ;;; The `DEFSLIMEFUN' macro defines a function that Emacs can call via
137 ;;; RPC.
138
139 (defmacro defslimefun (name arglist &body rest)
140 "A DEFUN for functions that Emacs can call by RPC."
141 `(progn
142 (defun ,name ,arglist ,@rest)
143 ;; see <http://www.franz.com/support/documentation/6.2/doc/pages/variables/compiler/s_cltl1-compile-file-toplevel-compatibility-p_s.htm>
144 (eval-when (:compile-toplevel :load-toplevel :execute)
145 (export ',name :swank))))
146
147 (defun missing-arg ()
148 "A function that the compiler knows will never to return a value.
149 You can use (MISSING-ARG) as the initform for defstruct slots that
150 must always be supplied. This way the :TYPE slot option need not
151 include some arbitrary initial value like NIL."
152 (error "A required &KEY or &OPTIONAL argument was not supplied."))
153
154
155 ;;;; Hooks
156 ;;;
157 ;;; We use Emacs-like `add-hook' and `run-hook' utilities to support
158 ;;; simple indirection. The interface is more CLish than the Emacs
159 ;;; Lisp one.
160
161 (defmacro add-hook (place function)
162 "Add FUNCTION to the list of values on PLACE."
163 `(pushnew ,function ,place))
164
165 (defun run-hook (functions &rest arguments)
166 "Call each of FUNCTIONS with ARGUMENTS."
167 (dolist (function functions)
168 (apply function arguments)))
169
170 (defvar *new-connection-hook* '()
171 "This hook is run each time a connection is established.
172 The connection structure is given as the argument.
173 Backend code should treat the connection structure as opaque.")
174
175 (defvar *connection-closed-hook* '()
176 "This hook is run when a connection is closed.
177 The connection as passed as an argument.
178 Backend code should treat the connection structure as opaque.")
179
180 (defvar *pre-reply-hook* '()
181 "Hook run (without arguments) immediately before replying to an RPC.")
182
183 (defvar *after-init-hook* '()
184 "Hook run after user init files are loaded.")
185
186
187 ;;;; Connections
188 ;;;
189 ;;; Connection structures represent the network connections between
190 ;;; Emacs and Lisp. Each has a socket stream, a set of user I/O
191 ;;; streams that redirect to Emacs, and optionally a second socket
192 ;;; used solely to pipe user-output to Emacs (an optimization).
193 ;;;
194
195 (defstruct (connection
196 (:conc-name connection.)
197 (:print-function print-connection))
198 ;; Raw I/O stream of socket connection.
199 (socket-io (missing-arg) :type stream :read-only t)
200 ;; Optional dedicated output socket (backending `user-output' slot).
201 ;; Has a slot so that it can be closed with the connection.
202 (dedicated-output nil :type (or stream null))
203 ;; Streams that can be used for user interaction, with requests
204 ;; redirected to Emacs.
205 (user-input nil :type (or stream null))
206 (user-output nil :type (or stream null))
207 (user-io nil :type (or stream null))
208 ;; A stream that we use for *trace-output*; if nil, we user user-output.
209 (trace-output nil :type (or stream null))
210 ;; A stream where we send REPL results.
211 (repl-results nil :type (or stream null))
212 ;; In multithreaded systems we delegate certain tasks to specific
213 ;; threads. The `reader-thread' is responsible for reading network
214 ;; requests from Emacs and sending them to the `control-thread'; the
215 ;; `control-thread' is responsible for dispatching requests to the
216 ;; threads that should handle them; the `repl-thread' is the one
217 ;; that evaluates REPL expressions. The control thread dispatches
218 ;; all REPL evaluations to the REPL thread and for other requests it
219 ;; spawns new threads.
220 reader-thread
221 control-thread
222 repl-thread
223 ;; Callback functions:
224 ;; (SERVE-REQUESTS <this-connection>) serves all pending requests
225 ;; from Emacs.
226 (serve-requests (missing-arg) :type function)
227 ;; (CLEANUP <this-connection>) is called when the connection is
228 ;; closed.
229 (cleanup nil :type (or null function))
230 ;; Cache of macro-indentation information that has been sent to Emacs.
231 ;; This is used for preparing deltas to update Emacs's knowledge.
232 ;; Maps: symbol -> indentation-specification
233 (indentation-cache (make-hash-table :test 'eq) :type hash-table)
234 ;; The list of packages represented in the cache:
235 (indentation-cache-packages '())
236 ;; The communication style used.
237 (communication-style nil :type (member nil :spawn :sigio :fd-handler))
238 ;; The coding system for network streams.
239 coding-system
240 ;; The SIGINT handler we should restore when the connection is
241 ;; closed.
242 saved-sigint-handler)
243
244 (defun print-connection (conn stream depth)
245 (declare (ignore depth))
246 (print-unreadable-object (conn stream :type t :identity t)))
247
248 (defvar *connections* '()
249 "List of all active connections, with the most recent at the front.")
250
251 (defvar *emacs-connection* nil
252 "The connection to Emacs currently in use.")
253
254 (defvar *swank-state-stack* '()
255 "A list of symbols describing the current state. Used for debugging
256 and to detect situations where interrupts can be ignored.")
257
258 (defun default-connection ()
259 "Return the 'default' Emacs connection.
260 This connection can be used to talk with Emacs when no specific
261 connection is in use, i.e. *EMACS-CONNECTION* is NIL.
262
263 The default connection is defined (quite arbitrarily) as the most
264 recently established one."
265 (first *connections*))
266
267 (defslimefun state-stack ()
268 "Return the value of *SWANK-STATE-STACK*."
269 *swank-state-stack*)
270
271 ;; A conditions to include backtrace information
272 (define-condition swank-error (error)
273 ((condition :initarg :condition :reader swank-error.condition)
274 (backtrace :initarg :backtrace :reader swank-error.backtrace))
275 (:report (lambda (condition stream)
276 (princ (swank-error.condition condition) stream))))
277
278 (defun make-swank-error (condition)
279 (make-condition 'swank-error :condition condition
280 :backtrace (safe-backtrace)))
281
282 (defun safe-backtrace ()
283 (ignore-errors
284 (call-with-debugging-environment
285 (lambda () (backtrace 0 nil)))))
286
287 (defvar *debug-on-swank-error* nil
288 "When non-nil invoke the system debugger on swank internal errors.
289 Do not set this to T unless you want to debug swank internals.")
290
291 (defmacro with-swank-error-handler ((connection) &body body)
292 (let ((var (gensym)))
293 `(let ((,var ,connection))
294 (handler-case
295 (handler-bind ((swank-error
296 (lambda (condition)
297 (when *debug-on-swank-error*
298 (invoke-default-debugger condition)))))
299 (progn ,@body))
300 (swank-error (condition)
301 (close-connection ,var
302 (swank-error.condition condition)
303 (swank-error.backtrace condition)))))))
304
305 (defmacro with-panic-handler ((connection) &body body)
306 (let ((var (gensym)))
307 `(let ((,var ,connection))
308 (handler-bind ((serious-condition
309 (lambda (condition)
310 (close-connection ,var condition (safe-backtrace)))))
311 . ,body))))
312
313 (add-hook *new-connection-hook* 'notify-backend-of-connection)
314 (defun notify-backend-of-connection (connection)
315 (declare (ignore connection))
316 (emacs-connected))
317
318
319 ;;;; Utilities
320
321 ;;;;; Helper macros
322
323 (defvar *slime-interrupts-enabled*)
324
325 (defmacro with-slime-interrupts (&body body)
326 `(progn
327 (check-slime-interrupts)
328 (let ((*slime-interrupts-enabled* t)
329 (*pending-slime-interrupts* '()))
330 (multiple-value-prog1 (progn ,@body)
331 (check-slime-interrupts)))))
332
333 (defmacro without-slime-interrupts (&body body)
334 `(progn
335 (check-slime-interrupts)
336 (let ((*slime-interrupts-enabled* nil)
337 (*pending-slime-interrupts* '()))
338 (multiple-value-prog1 (progn ,@body)
339 (check-slime-interrupts)))))
340
341 (defun invoke-or-queue-interrupt (function)
342 (cond ((not (boundp '*slime-interrupts-enabled*))
343 (without-slime-interrupts
344 (funcall function)))
345 (*slime-interrupts-enabled*
346 (funcall function))
347 ((cdr *pending-slime-interrupts*)
348 (simple-break "Two many queued interrupts"))
349 (t
350 (push function *pending-slime-interrupts*))))
351
352 (defslimefun simple-break (&optional (datum "Interrupt from Emacs") &rest args)
353 (with-simple-restart (continue "Continue from break.")
354 (invoke-slime-debugger (coerce-to-condition datum args))))
355
356 (defun coerce-to-condition (datum args)
357 (etypecase datum
358 (string (make-condition 'simple-error :format-control datum
359 :format-arguments args))
360 (symbol (apply #'make-condition datum args))))
361
362 (defmacro with-io-redirection ((connection) &body body)
363 "Execute BODY I/O redirection to CONNECTION.
364 If *REDIRECT-IO* is true then all standard I/O streams are redirected."
365 `(maybe-call-with-io-redirection ,connection (lambda () ,@body)))
366
367 (defun maybe-call-with-io-redirection (connection fun)
368 (if *redirect-io*
369 (call-with-redirected-io connection fun)
370 (funcall fun)))
371
372 (defmacro with-connection ((connection) &body body)
373 "Execute BODY in the context of CONNECTION."
374 `(call-with-connection ,connection (lambda () ,@body)))
375
376 (defun call-with-connection (connection function)
377 (let ((*emacs-connection* connection))
378 (without-slime-interrupts
379 (with-swank-error-handler (*emacs-connection*)
380 (with-io-redirection (*emacs-connection*)
381 (call-with-debugger-hook #'swank-debugger-hook function))))))
382
383 (defmacro without-interrupts (&body body)
384 `(call-without-interrupts (lambda () ,@body)))
385
386 (defmacro destructure-case (value &rest patterns)
387 "Dispatch VALUE to one of PATTERNS.
388 A cross between `case' and `destructuring-bind'.
389 The pattern syntax is:
390 ((HEAD . ARGS) . BODY)
391 The list of patterns is searched for a HEAD `eq' to the car of
392 VALUE. If one is found, the BODY is executed with ARGS bound to the
393 corresponding values in the CDR of VALUE."
394 (let ((operator (gensym "op-"))
395 (operands (gensym "rand-"))
396 (tmp (gensym "tmp-")))
397 `(let* ((,tmp ,value)
398 (,operator (car ,tmp))
399 (,operands (cdr ,tmp)))
400 (case ,operator
401 ,@(loop for (pattern . body) in patterns collect
402 (if (eq pattern t)
403 `(t ,@body)
404 (destructuring-bind (op &rest rands) pattern
405 `(,op (destructuring-bind ,rands ,operands
406 ,@body)))))
407 ,@(if (eq (caar (last patterns)) t)
408 '()
409 `((t (error "destructure-case failed: ~S" ,tmp))))))))
410
411 (defmacro with-struct* ((conc-name get obj) &body body)
412 (let ((var (gensym)))
413 `(let ((,var ,obj))
414 (macrolet ((,get (slot)
415 (let ((getter (intern (concatenate 'string
416 ',(string conc-name)
417 (string slot))
418 (symbol-package ',conc-name))))
419 `(,getter ,',var))))
420 ,@body))))
421
422 (defmacro with-temp-package (var &body body)
423 "Execute BODY with VAR bound to a temporary package.
424 The package is deleted before returning."
425 `(let ((,var (make-package (gensym "TEMP-PACKAGE-"))))
426 (unwind-protect (progn ,@body)
427 (delete-package ,var))))
428
429 (defmacro do-symbols* ((var &optional (package '*package*) result-form) &body body)
430 "Just like do-symbols, but makes sure a symbol is visited only once."
431 (let ((seen-ht (gensym "SEEN-HT")))
432 `(let ((,seen-ht (make-hash-table :test #'eq)))
433 (do-symbols (,var ,package ,result-form)
434 (unless (gethash ,var ,seen-ht)
435 (setf (gethash ,var ,seen-ht) t)
436 ,@body)))))
437
438 (defun use-threads-p ()
439 (eq (connection.communication-style *emacs-connection*) :spawn))
440
441 (defun current-thread-id ()
442 (thread-id (current-thread)))
443
444
445 ;;;;; Logging
446
447 (defvar *log-events* nil)
448 (defvar *log-output* nil) ; should be nil for image dumpers
449
450 (defun init-log-output ()
451 (labels ((deref (x)
452 (cond ((typep x 'synonym-stream)
453 (deref (symbol-value (synonym-stream-symbol x))))
454 (t x))))
455 (unless *log-output*
456 (setq *log-output* (deref *error-output*)))))
457
458 (add-hook *after-init-hook* 'init-log-output)
459
460 (defvar *event-history* (make-array 40 :initial-element nil)
461 "A ring buffer to record events for better error messages.")
462 (defvar *event-history-index* 0)
463 (defvar *enable-event-history* t)
464
465 (defun log-event (format-string &rest args)
466 "Write a message to *terminal-io* when *log-events* is non-nil.
467 Useful for low level debugging."
468 (with-standard-io-syntax
469 (let ((*print-readably* nil)
470 (*print-pretty* nil)
471 (*package* *swank-io-package*))
472 (when *enable-event-history*
473 (setf (aref *event-history* *event-history-index*)
474 (format nil "~?" format-string args))
475 (setf *event-history-index*
476 (mod (1+ *event-history-index*) (length *event-history*))))
477 (when *log-events*
478 (write-string (escape-non-ascii (format nil "~?" format-string args))
479 *log-output*)
480 (force-output *log-output*)))))
481
482 (defun event-history-to-list ()
483 "Return the list of events (older events first)."
484 (let ((arr *event-history*)
485 (idx *event-history-index*))
486 (concatenate 'list (subseq arr idx) (subseq arr 0 idx))))
487
488 (defun dump-event-history (stream)
489 (dolist (e (event-history-to-list))
490 (dump-event e stream)))
491
492 (defun dump-event (event stream)
493 (cond ((stringp event)
494 (write-string (escape-non-ascii event) stream))
495 ((null event))
496 (t
497 (write-string
498 (escape-non-ascii (format nil "Unexpected event: ~A~%" event))
499 stream))))
500
501 (defun escape-non-ascii (string)
502 "Return a string like STRING but with non-ascii chars escaped."
503 (cond ((ascii-string-p string) string)
504 (t (with-output-to-string (out)
505 (loop for c across string do
506 (cond ((ascii-char-p c) (write-char c out))
507 (t (format out "\\x~4,'0X" (char-code c)))))))))
508
509 (defun ascii-string-p (o)
510 (and (stringp o)
511 (every #'ascii-char-p o)))
512
513 (defun ascii-char-p (c)
514 (<= (char-code c) 127))
515
516
517 ;;;;; Symbols
518
519 (defun symbol-status (symbol &optional (package (symbol-package symbol)))
520 "Returns one of
521
522 :INTERNAL if the symbol is _present_ in PACKAGE as an _internal_ symbol,
523
524 :EXTERNAL if the symbol is _present_ in PACKAGE as an _external_ symbol,
525
526 :INHERITED if the symbol is _inherited_ by PACKAGE through USE-PACKAGE,
527 but is not _present_ in PACKAGE,
528
529 or NIL if SYMBOL is not _accessible_ in PACKAGE.
530
531
532 Be aware not to get confused with :INTERNAL and how \"internal
533 symbols\" are defined in the spec; there is a slight mismatch of
534 definition with the Spec and what's commonly meant when talking
535 about internal symbols most times. As the spec says:
536
537 In a package P, a symbol S is
538
539 _accessible_ if S is either _present_ in P itself or was
540 inherited from another package Q (which implies
541 that S is _external_ in Q.)
542
543 You can check that with: (AND (SYMBOL-STATUS S P) T)
544
545
546 _present_ if either P is the /home package/ of S or S has been
547 imported into P or exported from P by IMPORT, or
548 EXPORT respectively.
549
550 Or more simply, if S is not _inherited_.
551
552 You can check that with: (LET ((STATUS (SYMBOL-STATUS S P)))
553 (AND STATUS
554 (NOT (EQ STATUS :INHERITED))))
555
556
557 _external_ if S is going to be inherited into any package that
558 /uses/ P by means of USE-PACKAGE, MAKE-PACKAGE, or
559 DEFPACKAGE.
560
561 Note that _external_ implies _present_, since to
562 make a symbol _external_, you'd have to use EXPORT
563 which will automatically make the symbol _present_.
564
565 You can check that with: (EQ (SYMBOL-STATUS S P) :EXTERNAL)
566
567
568 _internal_ if S is _accessible_ but not _external_.
569
570 You can check that with: (LET ((STATUS (SYMBOL-STATUS S P)))
571 (AND STATUS
572 (NOT (EQ STATUS :EXTERNAL))))
573
574
575 Notice that this is *different* to
576 (EQ (SYMBOL-STATUS S P) :INTERNAL)
577 because what the spec considers _internal_ is split up into two
578 explicit pieces: :INTERNAL, and :INHERITED; just as, for instance,
579 CL:FIND-SYMBOL does.
580
581 The rationale is that most times when you speak about \"internal\"
582 symbols, you're actually not including the symbols inherited
583 from other packages, but only about the symbols directly specific
584 to the package in question.
585 "
586 (when package ; may be NIL when symbol is completely uninterned.
587 (check-type symbol symbol) (check-type package package)
588 (multiple-value-bind (present-symbol status)
589 (find-symbol (symbol-name symbol) package)
590 (and (eq symbol present-symbol) status))))
591
592 (defun symbol-external-p (symbol &optional (package (symbol-package symbol)))
593 "True if SYMBOL is external in PACKAGE.
594 If PACKAGE is not specified, the home package of SYMBOL is used."
595 (eq (symbol-status symbol package) :external))
596
597
598 (defun classify-symbol (symbol)
599 "Returns a list of classifiers that classify SYMBOL according to its
600 underneath objects (e.g. :BOUNDP if SYMBOL constitutes a special
601 variable.) The list may contain the following classification
602 keywords: :BOUNDP, :FBOUNDP, :CONSTANT, :GENERIC-FUNCTION,
603 :TYPESPEC, :CLASS, :MACRO, :SPECIAL-OPERATOR, and/or :PACKAGE"
604 (check-type symbol symbol)
605 (flet ((type-specifier-p (s)
606 (or (documentation s 'type)
607 (not (eq (type-specifier-arglist s) :not-available)))))
608 (let (result)
609 (when (boundp symbol) (push (if (constantp symbol)
610 :constant :boundp) result))
611 (when (fboundp symbol) (push :fboundp result))
612 (when (type-specifier-p symbol) (push :typespec result))
613 (when (find-class symbol nil) (push :class result))
614 (when (macro-function symbol) (push :macro result))
615 (when (special-operator-p symbol) (push :special-operator result))
616 (when (find-package symbol) (push :package result))
617 (when (typep (ignore-errors (fdefinition symbol))
618 'generic-function)
619 (push :generic-function result))
620
621 result)))
622
623 (defun symbol-classification->string (flags)
624 (format nil "~A~A~A~A~A~A~A~A"
625 (if (or (member :boundp flags)
626 (member :constant flags)) "b" "-")
627 (if (member :fboundp flags) "f" "-")
628 (if (member :generic-function flags) "g" "-")
629 (if (member :class flags) "c" "-")
630 (if (member :typespec flags) "t" "-")
631 (if (member :macro flags) "m" "-")
632 (if (member :special-operator flags) "s" "-")
633 (if (member :package flags) "p" "-")))
634
635
636 ;;;; TCP Server
637
638 (defvar *use-dedicated-output-stream* nil
639 "When T swank will attempt to create a second connection to
640 Emacs which is used just to send output.")
641
642 (defvar *dedicated-output-stream-port* 0
643 "Which port we should use for the dedicated output stream.")
644
645 (defvar *communication-style* (preferred-communication-style))
646
647 (defvar *dont-close* nil
648 "Default value of :dont-close argument to start-server and
649 create-server.")
650
651 (defvar *dedicated-output-stream-buffering*
652 (if (eq *communication-style* :spawn) :full :none)
653 "The buffering scheme that should be used for the output stream.
654 Valid values are :none, :line, and :full.")
655
656 (defvar *coding-system* "iso-latin-1-unix")
657
658 (defvar *listener-sockets* nil
659 "A property list of lists containing style, socket pairs used
660 by swank server listeners, keyed on socket port number. They
661 are used to close sockets on server shutdown or restart.")
662
663 (defun start-server (port-file &key (style *communication-style*)
664 (dont-close *dont-close*)
665 (coding-system *coding-system*))
666 "Start the server and write the listen port number to PORT-FILE.
667 This is the entry point for Emacs."
668 (setup-server 0 (lambda (port)
669 (announce-server-port port-file port))
670 style dont-close
671 (find-external-format-or-lose coding-system)))
672
673 (defun create-server (&key (port default-server-port)
674 (style *communication-style*)
675 (dont-close *dont-close*)
676 (coding-system *coding-system*))
677 "Start a SWANK server on PORT running in STYLE.
678 If DONT-CLOSE is true then the listen socket will accept multiple
679 connections, otherwise it will be closed after the first."
680 (setup-server port #'simple-announce-function style dont-close
681 (find-external-format-or-lose coding-system)))
682
683 (defun find-external-format-or-lose (coding-system)
684 (or (find-external-format coding-system)
685 (error "Unsupported coding system: ~s" coding-system)))
686
687 (defparameter *loopback-interface* "127.0.0.1")
688
689 (defun setup-server (port announce-fn style dont-close external-format)
690 (declare (type function announce-fn))
691 (init-log-output)
692 (let* ((socket (create-socket *loopback-interface* port))
693 (local-port (local-port socket)))
694 (funcall announce-fn local-port)
695 (flet ((serve ()
696 (serve-connection socket style dont-close external-format)))
697 (ecase style
698 (:spawn
699 (initialize-multiprocessing
700 (lambda ()
701 (spawn (lambda ()
702 (cond ((not dont-close) (serve))
703 (t (loop (ignore-errors (serve))))))
704 :name (cat "Swank " (princ-to-string port))))))
705 ((:fd-handler :sigio)
706 (add-fd-handler socket (lambda () (serve))))
707 ((nil) (loop do (serve) while dont-close)))
708 (setf (getf *listener-sockets* port) (list style socket))
709 local-port)))
710
711 (defun stop-server (port)
712 "Stop server running on PORT."
713 (let* ((socket-description (getf *listener-sockets* port))
714 (style (first socket-description))
715 (socket (second socket-description)))
716 (ecase style
717 (:spawn
718 (let ((thread-position
719 (position-if
720 (lambda (x)
721 (string-equal (first x)
722 (concatenate 'string "Swank "
723 (princ-to-string port))))
724 (list-threads))))
725 (when thread-position
726 (kill-nth-thread thread-position)
727 (close-socket socket)
728 (remf *listener-sockets* port))))
729 ((:fd-handler :sigio)
730 (remove-fd-handlers socket)
731 (close-socket socket)
732 (remf *listener-sockets* port)))))
733
734 (defun restart-server (&key (port default-server-port)
735 (style *communication-style*)
736 (dont-close *dont-close*)
737 (coding-system *coding-system*))
738 "Stop the server listening on PORT, then start a new SWANK server
739 on PORT running in STYLE. If DONT-CLOSE is true then the listen socket
740 will accept multiple connections, otherwise it will be closed after the
741 first."
742 (stop-server port)
743 (sleep 5)
744 (create-server :port port :style style :dont-close dont-close
745 :coding-system coding-system))
746
747
748 (defun serve-connection (socket style dont-close external-format)
749 (let ((closed-socket-p nil))
750 (unwind-protect
751 (let ((client (accept-authenticated-connection
752 socket :external-format external-format)))
753 (unless dont-close
754 (close-socket socket)
755 (setf closed-socket-p t))
756 (let ((connection (create-connection client style)))
757 (run-hook *new-connection-hook* connection)
758 (push connection *connections*)
759 (serve-requests connection)))
760 (unless (or dont-close closed-socket-p)
761 (close-socket socket)))))
762
763 (defun accept-authenticated-connection (&rest args)
764 (let ((new (apply #'accept-connection args))
765 (success nil))
766 (unwind-protect
767 (let ((secret (slime-secret)))
768 (when secret
769 (set-stream-timeout new 20)
770 (let ((first-val (decode-message new)))
771 (unless (and (stringp first-val) (string= first-val secret))
772 (error "Incoming connection doesn't know the password."))))
773 (set-stream-timeout new nil)
774 (setf success t))
775 (unless success
776 (close new :abort t)))
777 new))
778
779 (defun slime-secret ()
780 "Finds the magic secret from the user's home directory. Returns nil
781 if the file doesn't exist; otherwise the first line of the file."
782 (with-open-file (in
783 (merge-pathnames (user-homedir-pathname) #p".slime-secret")
784 :if-does-not-exist nil)
785 (and in (read-line in nil ""))))
786
787 (defun serve-requests (connection)
788 "Read and process all requests on connections."
789 (funcall (connection.serve-requests connection) connection))
790
791 (defun announce-server-port (file port)
792 (with-open-file (s file
793 :direction :output
794 :if-exists :error
795 :if-does-not-exist :create)
796 (format s "~S~%" port))
797 (simple-announce-function port))
798
799 (defun simple-announce-function (port)
800 (when *swank-debug-p*
801 (format *log-output* "~&;; Swank started at port: ~D.~%" port)
802 (force-output *log-output*)))
803
804 (defun open-streams (connection)
805 "Return the 5 streams for IO redirection:
806 DEDICATED-OUTPUT INPUT OUTPUT IO REPL-RESULTS"
807 (let ((output-fn (make-output-function connection))
808 (input-fn
809 (lambda ()
810 (with-connection (connection)
811 (with-simple-restart (abort-read
812 "Abort reading input from Emacs.")
813 (read-user-input-from-emacs))))))
814 (multiple-value-bind (in out) (make-fn-streams input-fn output-fn)
815 (let* ((dedicated-output (if *use-dedicated-output-stream*
816 (open-dedicated-output-stream
817 (connection.socket-io connection))))
818 (out (or dedicated-output out))
819 (io (make-two-way-stream in out))
820 (repl-results (make-output-stream-for-target connection
821 :repl-result)))
822 (when (eq (connection.communication-style connection) :spawn)
823 (spawn (lambda () (auto-flush-loop out))
824 :name "auto-flush-thread"))
825 (values dedicated-output in out io repl-results)))))
826
827 (defvar *maximum-pipelined-output-chunks* 20)
828
829 ;; FIXME: if wait-for-event aborts the event will stay in the queue forever.
830 (defun make-output-function (connection)
831 "Create function to send user output to Emacs."
832 (let ((i 0) (tag 0) (l 0))
833 (lambda (string)
834 (with-connection (connection)
835 (with-simple-restart (abort "Abort sending output to Emacs.")
836 (when (or (= i *maximum-pipelined-output-chunks*)
837 (> l (* 80 20 5)))
838 (setf tag (mod (1+ tag) 1000))
839 (send-to-emacs `(:ping ,(current-thread-id) ,tag))
840 (wait-for-event `(:emacs-pong ,tag))
841 (setf i 0)
842 (setf l 0))
843 (incf i)
844 (incf l (length string))
845 (send-to-emacs `(:write-string ,string)))))))
846
847 (defun make-output-function-for-target (connection target)
848 "Create a function to send user output to a specific TARGET in Emacs."
849 (lambda (string)
850 (with-connection (connection)
851 (with-simple-restart
852 (abort "Abort sending output to Emacs.")
853 (send-to-emacs `(:write-string ,string ,target))))))
854
855 (defun make-output-stream-for-target (connection target)
856 "Create a stream that sends output to a specific TARGET in Emacs."
857 (nth-value 1 (make-fn-streams
858 (lambda ()
859 (error "Should never be called"))
860 (make-output-function-for-target connection target))))
861
862 (defun open-dedicated-output-stream (socket-io)
863 "Open a dedicated output connection to the Emacs on SOCKET-IO.
864 Return an output stream suitable for writing program output.
865
866 This is an optimized way for Lisp to deliver output to Emacs."
867 (let ((socket (create-socket *loopback-interface*
868 *dedicated-output-stream-port*)))
869 (unwind-protect
870 (let ((port (local-port socket)))
871 (encode-message `(:open-dedicated-output-stream ,port) socket-io)
872 (let ((dedicated (accept-authenticated-connection
873 socket
874 :external-format
875 (or (ignore-errors
876 (stream-external-format socket-io))
877 :default)
878 :buffering *dedicated-output-stream-buffering*
879 :timeout 30)))
880 (close-socket socket)
881 (setf socket nil)
882 dedicated))
883 (when socket
884 (close-socket socket)))))
885
886 (defvar *sldb-quit-restart* 'abort
887 "What restart should swank attempt to invoke when the user sldb-quits.")
888
889 (defun handle-requests (connection &optional timeout just-one)
890 "Read and process requests.
891 The processing is done in the extent of the toplevel restart."
892 (assert (null *swank-state-stack*))
893 (let ((*swank-state-stack* '(:handle-request)))
894 (with-connection (connection)
895 (loop
896 (with-simple-restart (abort "Return to SLIME's top level.")
897 (let* ((*sldb-quit-restart* (find-restart 'abort))
898 (timeout? (process-requests timeout just-one)))
899 (when (or just-one timeout?)
900 (return))))))))
901
902 (defun process-requests (timeout just-one)
903 "Read and process requests from Emacs."
904 (loop
905 (multiple-value-bind (event timeout?)
906 (wait-for-event `(:emacs-rex . _) timeout)
907 (when timeout? (return t))
908 (apply #'eval-for-emacs (cdr event))
909 (when just-one (return nil)))))
910
911 (defun current-socket-io ()
912 (connection.socket-io *emacs-connection*))
913
914 (defun close-connection (c condition backtrace)
915 (let ((*debugger-hook* nil))
916 (format *log-output* "~&;; swank:close-connection: ~A~%" condition)
917 (let ((cleanup (connection.cleanup c)))
918 (when cleanup
919 (funcall cleanup c)))
920 (close (connection.socket-io c))
921 (when (connection.dedicated-output c)
922 (close (connection.dedicated-output c)))
923 (setf *connections* (remove c *connections*))
924 (run-hook *connection-closed-hook* c)
925 (when (and condition (not (typep condition 'end-of-file)))
926 (finish-output *log-output*)
927 (format *log-output* "~&;; Event history start:~%")
928 (dump-event-history *log-output*)
929 (format *log-output* ";; Event history end.~%~
930 ;; Backtrace:~%~{~A~%~}~
931 ;; Connection to Emacs lost. [~%~
932 ;; condition: ~A~%~
933 ;; type: ~S~%~
934 ;; encoding: ~A style: ~S dedicated: ~S]~%"
935 backtrace
936 (escape-non-ascii (safe-condition-message condition) )
937 (type-of condition)
938 (ignore-errors (stream-external-format (connection.socket-io c)))
939 (connection.communication-style c)
940 *use-dedicated-output-stream*)
941 (finish-output *log-output*))))
942
943
944 ;;;;;; Thread based communication
945
946 (defvar *active-threads* '())
947
948 (defun read-loop (connection)
949 (let ((input-stream (connection.socket-io connection))
950 (control-thread (connection.control-thread connection)))
951 (with-swank-error-handler (connection)
952 (loop (send control-thread (decode-message input-stream))))))
953
954 (defun dispatch-loop (connection)
955 (let ((*emacs-connection* connection))
956 (with-panic-handler (connection)
957 (loop (dispatch-event (read-event))))))
958
959 (defvar *auto-flush-interval* 0.2)
960
961 (defun auto-flush-loop (stream)
962 (loop
963 (when (not (and (open-stream-p stream)
964 (output-stream-p stream)))
965 (return nil))
966 (finish-output stream)
967 (sleep *auto-flush-interval*)))
968
969 (defun find-repl-thread (connection)
970 (cond ((not (use-threads-p))
971 (current-thread))
972 (t
973 (let ((thread (connection.repl-thread connection)))
974 (assert thread)
975 (cond ((thread-alive-p thread) thread)
976 (t
977 (setf (connection.repl-thread connection)
978 (spawn-repl-thread connection "new-repl-thread"))))))))
979
980 (defun find-worker-thread (id)
981 (etypecase id
982 ((member t)
983 (car *active-threads*))
984 ((member :repl-thread)
985 (find-repl-thread *emacs-connection*))
986 (fixnum
987 (find-thread id))))
988
989 (defun interrupt-worker-thread (id)
990 (let ((thread (or (find-worker-thread id)
991 (find-repl-thread *emacs-connection*))))
992 (signal-interrupt thread
993 (lambda ()
994 (invoke-or-queue-interrupt #'simple-break)))))
995
996 (defun thread-for-evaluation (id)
997 "Find or create a thread to evaluate the next request."
998 (let ((c *emacs-connection*))
999 (etypecase id
1000 ((member t)
1001 (cond ((use-threads-p) (spawn-worker-thread c))
1002 (t (current-thread))))
1003 ((member :repl-thread)
1004 (find-repl-thread c))
1005 (fixnum
1006 (find-thread id)))))
1007
1008 (defun spawn-worker-thread (connection)
1009 (spawn (lambda ()
1010 (with-bindings *default-worker-thread-bindings*
1011 (handle-requests connection nil t)))
1012 :name "worker"))
1013
1014 (defun spawn-repl-thread (connection name)
1015 (spawn (lambda ()
1016 (with-bindings *default-worker-thread-bindings*
1017 (repl-loop connection)))
1018 :name name))
1019
1020 (defun dispatch-event (event)
1021 "Handle an event triggered either by Emacs or within Lisp."
1022 (log-event "dispatch-event: ~s~%" event)
1023 (destructure-case event
1024 ((:emacs-rex form package thread-id id)
1025 (let ((thread (thread-for-evaluation thread-id)))
1026 (push thread *active-threads*)
1027 (send-event thread `(:emacs-rex ,form ,package ,id))))
1028 ((:return thread &rest args)
1029 (let ((tail (member thread *active-threads*)))
1030 (setq *active-threads* (nconc (ldiff *active-threads* tail)
1031 (cdr tail))))
1032 (encode-message `(:return ,@args) (current-socket-io)))
1033 ((:emacs-interrupt thread-id)
1034 (interrupt-worker-thread thread-id))
1035 (((:write-string
1036 :debug :debug-condition :debug-activate :debug-return
1037 :presentation-start :presentation-end
1038 :new-package :new-features :ed :%apply :indentation-update
1039 :eval :eval-no-wait :background-message :inspect :ping
1040 :y-or-n-p :read-string :read-aborted)
1041 &rest _)
1042 (declare (ignore _))
1043 (encode-message event (current-socket-io)))
1044 (((:emacs-pong :emacs-return :emacs-return-string) thread-id &rest args)
1045 (send-event (find-thread thread-id) (cons (car event) args)))
1046 (((:end-of-stream))
1047 (close-connection *emacs-connection* nil (safe-backtrace)))))
1048
1049 (defvar *event-queue* '())
1050
1051 (defun send-event (thread event)
1052 (log-event "send-event: ~s ~s~%" thread event)
1053 (cond ((use-threads-p) (send thread event))
1054 (t (setf *event-queue* (nconc *event-queue* (list event))))))
1055
1056 (defun read-event (&optional timeout)
1057 (cond ((use-threads-p) (receive timeout))
1058 (t (decode-message (current-socket-io) timeout))))
1059
1060 (defun send-to-emacs (event)
1061 "Send EVENT to Emacs."
1062 ;;(log-event "send-to-emacs: ~a" event)
1063 (cond ((use-threads-p)
1064 (send (connection.control-thread *emacs-connection*) event))
1065 (t (dispatch-event event))))
1066
1067 (defun signal-interrupt (thread interrupt)
1068 (log-event "singal-interrupt~%")
1069 (cond ((use-threads-p) (interrupt-thread thread interrupt))
1070 (t (funcall interrupt))))
1071
1072 (defun wait-for-event (pattern &optional timeout)
1073 (log-event "wait-for-event: ~s ~s~%" pattern timeout)
1074 (cond ((use-threads-p)
1075 (without-slime-interrupts
1076 (receive-if (lambda (e) (event-match-p e pattern)) timeout)))
1077 (t
1078 (wait-for-event/event-loop pattern timeout))))
1079
1080 (defun wait-for-event/event-loop (pattern timeout)
1081 (assert (or (not timeout) (eq timeout t)))
1082 (loop
1083 (check-slime-interrupts)
1084 (let ((tail (member-if (lambda (e) (event-match-p e pattern))
1085 *event-queue*)))
1086 (when tail
1087 (setq *event-queue*
1088 (nconc (ldiff *event-queue* tail) (cdr tail)))
1089 (return (car tail))))
1090 (multiple-value-bind (event timeout?) (read-event timeout)
1091 (when timeout? (return (values nil t)))
1092 (dispatch-event event))))
1093
1094 (defun event-match-p (event pattern)
1095 (cond ((or (keywordp pattern) (numberp pattern) (stringp pattern)
1096 (member pattern '(nil t)))
1097 (equal event pattern))
1098 ((symbolp pattern) t)
1099 ((consp pattern)
1100 (and (consp event)
1101 (and (event-match-p (car event) (car pattern))
1102 (event-match-p (cdr event) (cdr pattern)))))
1103 (t (error "Invalid pattern: ~S" pattern))))
1104
1105 (defun spawn-threads-for-connection (connection)
1106 (setf (connection.control-thread connection)
1107 (spawn (lambda () (control-thread connection))
1108 :name "control-thread"))
1109 connection)
1110
1111 (defun control-thread (connection)
1112 (with-struct* (connection. @ connection)
1113 (setf (@ control-thread) (current-thread))
1114 (setf (@ repl-thread) (spawn-repl-thread connection "repl-thread"))
1115 (setf (@ reader-thread) (spawn (lambda () (read-loop connection))
1116 :name "reader-thread"))
1117 (dispatch-loop connection)))
1118
1119 (defun cleanup-connection-threads (connection)
1120 (let ((threads (list (connection.repl-thread connection)
1121 (connection.reader-thread connection)
1122 (connection.control-thread connection))))
1123 (dolist (thread threads)
1124 (when (and thread
1125 (thread-alive-p thread)
1126 (not (equal (current-thread) thread)))
1127 (kill-thread thread)))))
1128
1129 (defun repl-loop (connection)
1130 (handle-requests connection))
1131
1132 ;;;;;; Signal driven IO
1133
1134 (defun install-sigio-handler (connection)
1135 (add-sigio-handler (connection.socket-io connection)
1136 (lambda () (process-io-interrupt connection)))
1137 (handle-or-process-requests connection))
1138
1139 (defun process-io-interrupt (connection)
1140 (log-event "process-io-interrupt~%")
1141 (invoke-or-queue-interrupt
1142 (lambda () (handle-or-process-requests connection))))
1143
1144 (defun handle-or-process-requests (connection)
1145 (log-event "handle-or-process-requests: ~a~%" *swank-state-stack*)
1146 (cond ((null *swank-state-stack*)
1147 (handle-requests connection t))
1148 ((eq (car *swank-state-stack*) :read-next-form))
1149 (t (process-requests t nil))))
1150
1151 (defun deinstall-sigio-handler (connection)
1152 (log-event "deinstall-sigio-handler...~%")
1153 (remove-sigio-handlers (connection.socket-io connection))
1154 (log-event "deinstall-sigio-handler...done~%"))
1155
1156 ;;;;;; SERVE-EVENT based IO
1157
1158 (defun install-fd-handler (connection)
1159 (add-fd-handler (connection.socket-io connection)
1160 (lambda () (handle-or-process-requests connection)))
1161 (setf (connection.saved-sigint-handler connection)
1162 (install-sigint-handler
1163 (lambda ()
1164 (invoke-or-queue-interrupt
1165 (lambda ()
1166 (dispatch-event `(:emacs-interrupt ,(current-thread-id))))))))
1167 (handle-or-process-requests connection))
1168
1169 (defun deinstall-fd-handler (connection)
1170 (remove-fd-handlers (connection.socket-io connection))
1171 (install-sigint-handler (connection.saved-sigint-handler connection)))
1172
1173 ;;;;;; Simple sequential IO
1174
1175 (defun simple-serve-requests (connection)
1176 (unwind-protect
1177 (call-with-user-break-handler
1178 (lambda ()
1179 (invoke-or-queue-interrupt
1180 (lambda ()
1181 (dispatch-event `(:emacs-interrupt ,(current-thread-id))))))
1182 (lambda ()
1183 (with-simple-restart (close-connection "Close SLIME connection")
1184 (handle-requests connection))))
1185 (close-connection connection nil (safe-backtrace))))
1186
1187 (defun initialize-streams-for-connection (connection)
1188 (multiple-value-bind (dedicated in out io repl-results)
1189 (open-streams connection)
1190 (setf (connection.dedicated-output connection) dedicated
1191 (connection.user-io connection) io
1192 (connection.user-output connection) out
1193 (connection.user-input connection) in
1194 (connection.repl-results connection) repl-results)
1195 connection))
1196
1197 (defun create-connection (socket-io style)
1198 (let ((success nil))
1199 (unwind-protect
1200 (let ((c (ecase style
1201 (:spawn
1202 (make-connection :socket-io socket-io
1203 :serve-requests #'spawn-threads-for-connection
1204 :cleanup #'cleanup-connection-threads))
1205 (:sigio
1206 (make-connection :socket-io socket-io
1207 :serve-requests #'install-sigio-handler
1208 :cleanup #'deinstall-sigio-handler))
1209 (:fd-handler
1210 (make-connection :socket-io socket-io
1211 :serve-requests #'install-fd-handler
1212 :cleanup #'deinstall-fd-handler))
1213 ((nil)
1214 (make-connection :socket-io socket-io
1215 :serve-requests #'simple-serve-requests))
1216 )))
1217 (setf (connection.communication-style c) style)
1218 (initialize-streams-for-connection c)
1219 (setf success t)
1220 c)
1221 (unless success
1222 (close socket-io :abort t)))))
1223
1224
1225 ;;;; IO to Emacs
1226 ;;;
1227 ;;; This code handles redirection of the standard I/O streams
1228 ;;; (`*standard-output*', etc) into Emacs. The `connection' structure
1229 ;;; contains the appropriate streams, so all we have to do is make the
1230 ;;; right bindings.
1231
1232 ;;;;; Global I/O redirection framework
1233 ;;;
1234 ;;; Optionally, the top-level global bindings of the standard streams
1235 ;;; can be assigned to be redirected to Emacs. When Emacs connects we
1236 ;;; redirect the streams into the connection, and they keep going into
1237 ;;; that connection even if more are established. If the connection
1238 ;;; handling the streams closes then another is chosen, or if there
1239 ;;; are no connections then we revert to the original (real) streams.
1240 ;;;
1241 ;;; It is slightly tricky to assign the global values of standard
1242 ;;; streams because they are often shadowed by dynamic bindings. We
1243 ;;; solve this problem by introducing an extra indirection via synonym
1244 ;;; streams, so that *STANDARD-INPUT* is a synonym stream to
1245 ;;; *CURRENT-STANDARD-INPUT*, etc. We never shadow the "current"
1246 ;;; variables, so they can always be assigned to affect a global
1247 ;;; change.
1248
1249 (defvar *globally-redirect-io* nil
1250 "When non-nil globally redirect all standard streams to Emacs.")
1251
1252 ;;;;; Global redirection setup
1253
1254 (defvar *saved-global-streams* '()
1255 "A plist to save and restore redirected stream objects.
1256 E.g. the value for '*standard-output* holds the stream object
1257 for *standard-output* before we install our redirection.")
1258
1259 (defun setup-stream-indirection (stream-var &optional stream)
1260 "Setup redirection scaffolding for a global stream variable.
1261 Supposing (for example) STREAM-VAR is *STANDARD-INPUT*, this macro:
1262
1263 1. Saves the value of *STANDARD-INPUT* in `*SAVED-GLOBAL-STREAMS*'.
1264
1265 2. Creates *CURRENT-STANDARD-INPUT*, initially with the same value as
1266 *STANDARD-INPUT*.
1267
1268 3. Assigns *STANDARD-INPUT* to a synonym stream pointing to
1269 *CURRENT-STANDARD-INPUT*.
1270
1271 This has the effect of making *CURRENT-STANDARD-INPUT* contain the
1272 effective global value for *STANDARD-INPUT*. This way we can assign
1273 the effective global value even when *STANDARD-INPUT* is shadowed by a
1274 dynamic binding."
1275 (let ((current-stream-var (prefixed-var '#:current stream-var))
1276 (stream (or stream (symbol-value stream-var))))
1277 ;; Save the real stream value for the future.
1278 (setf (getf *saved-global-streams* stream-var) stream)
1279 ;; Define a new variable for the effective stream.
1280 ;; This can be reassigned.
1281 (proclaim `(special ,current-stream-var))
1282 (set current-stream-var stream)
1283 ;; Assign the real binding as a synonym for the current one.
1284 (set stream-var (make-synonym-stream current-stream-var))))
1285
1286 (defun prefixed-var (prefix variable-symbol)
1287 "(PREFIXED-VAR \"FOO\" '*BAR*) => SWANK::*FOO-BAR*"
1288 (let ((basename (subseq (symbol-name variable-symbol) 1)))
1289 (intern (format nil "*~A-~A" (string prefix) basename) :swank)))
1290
1291 (defvar *standard-output-streams*
1292 '(*standard-output* *error-output* *trace-output*)
1293 "The symbols naming standard output streams.")
1294
1295 (defvar *standard-input-streams*
1296 '(*standard-input*)
1297 "The symbols naming standard input streams.")
1298
1299 (defvar *standard-io-streams*
1300 '(*debug-io* *query-io* *terminal-io*)
1301 "The symbols naming standard io streams.")
1302
1303 (defun init-global-stream-redirection ()
1304 (when *globally-redirect-io*
1305 (assert (not *saved-global-streams*) () "Streams already redirected.")
1306 (mapc #'setup-stream-indirection
1307 (append *standard-output-streams*
1308 *standard-input-streams*
1309 *standard-io-streams*))))
1310
1311 (add-hook *after-init-hook* 'init-global-stream-redirection)
1312
1313 (defun globally-redirect-io-to-connection (connection)
1314 "Set the standard I/O streams to redirect to CONNECTION.
1315 Assigns *CURRENT-<STREAM>* for all standard streams."
1316 (dolist (o *standard-output-streams*)
1317 (set (prefixed-var '#:current o)
1318 (connection.user-output connection)))
1319 ;; FIXME: If we redirect standard input to Emacs then we get the
1320 ;; regular Lisp top-level trying to read from our REPL.
1321 ;;
1322 ;; Perhaps the ideal would be for the real top-level to run in a
1323 ;; thread with local bindings for all the standard streams. Failing
1324 ;; that we probably would like to inhibit it from reading while
1325 ;; Emacs is connected.
1326 ;;
1327 ;; Meanwhile we just leave *standard-input* alone.
1328 #+NIL
1329 (dolist (i *standard-input-streams*)
1330 (set (prefixed-var '#:current i)
1331 (connection.user-input connection)))
1332 (dolist (io *standard-io-streams*)
1333 (set (prefixed-var '#:current io)
1334 (connection.user-io connection))))
1335
1336 (defun revert-global-io-redirection ()
1337 "Set *CURRENT-<STREAM>* to *REAL-<STREAM>* for all standard streams."
1338 (dolist (stream-var (append *standard-output-streams*
1339 *standard-input-streams*
1340 *standard-io-streams*))
1341 (set (prefixed-var '#:current stream-var)
1342 (getf *saved-global-streams* stream-var))))
1343
1344 ;;;;; Global redirection hooks
1345
1346 (defvar *global-stdio-connection* nil
1347 "The connection to which standard I/O streams are globally redirected.
1348 NIL if streams are not globally redirected.")
1349
1350 (defun maybe-redirect-global-io (connection)
1351 "Consider globally redirecting to a newly-established CONNECTION."
1352 (when (and *globally-redirect-io* (null *global-stdio-connection*))
1353 (setq *global-stdio-connection* connection)
1354 (globally-redirect-io-to-connection connection)))
1355
1356 (defun update-redirection-after-close (closed-connection)
1357 "Update redirection after a connection closes."
1358 (check-type closed-connection connection)
1359 (when (eq *global-stdio-connection* closed-connection)
1360 (if (and (default-connection) *globally-redirect-io*)
1361 ;; Redirect to another connection.
1362 (globally-redirect-io-to-connection (default-connection))
1363 ;; No more connections, revert to the real streams.
1364 (progn (revert-global-io-redirection)
1365 (setq *global-stdio-connection* nil)))))
1366
1367 (add-hook *new-connection-hook* 'maybe-redirect-global-io)
1368 (add-hook *connection-closed-hook* 'update-redirection-after-close)
1369
1370 ;;;;; Redirection during requests
1371 ;;;
1372 ;;; We always redirect the standard streams to Emacs while evaluating
1373 ;;; an RPC. This is done with simple dynamic bindings.
1374
1375 (defun call-with-redirected-io (connection function)
1376 "Call FUNCTION with I/O streams redirected via CONNECTION."
1377 (declare (type function function))
1378 (let* ((io (connection.user-io connection))
1379 (in (connection.user-input connection))
1380 (out (connection.user-output connection))
1381 (trace (or (connection.trace-output connection) out))
1382 (*standard-output* out)
1383 (*error-output* out)
1384 (*trace-output* trace)
1385 (*debug-io* io)
1386 (*query-io* io)
1387 (*standard-input* in)
1388 (*terminal-io* io))
1389 (funcall function)))
1390
1391 (defun call-with-thread-description (description thunk)
1392 ;; For `M-x slime-list-threads': Display what threads
1393 ;; created by swank are currently doing.
1394 (flet ((request-to-string (req)
1395 (remove #\Newline
1396 (string-trim '(#\Space #\Tab)
1397 (prin1-to-string req))))
1398 (truncate-string (str n)
1399 (format nil "~A..." (subseq str 0 (min (length str) n)))))
1400 (let* ((thread (current-thread))
1401 (old-description (thread-description thread)))
1402 (set-thread-description thread
1403 (truncate-string (request-to-string description)
1404 55))
1405 (unwind-protect (funcall thunk)
1406 (set-thread-description thread old-description)))))
1407
1408 (defmacro with-thread-description (description &body body)
1409 `(call-with-thread-description ,description #'(lambda () ,@body)))
1410
1411 (defun decode-message (stream &optional timeout)
1412 "Read an S-expression from STREAM using the SLIME protocol."
1413 (assert (or (not timeout) (eq timeout t)))
1414 ;;(log-event "decode-message~%")
1415 (let ((*swank-state-stack* (cons :read-next-form *swank-state-stack*)))
1416 (handler-bind ((error (lambda (c) (error (make-swank-error c)))))
1417 (let ((c (read-char-no-hang stream nil)))
1418 (cond ((and (not c) timeout) (values nil t))
1419 (t
1420 (and c (unread-char c stream))
1421 (values (read-form (read-packet stream)) nil)))))))
1422
1423 (defun read-packet (stream)
1424 (peek-char nil stream) ; wait while queuing interrupts
1425 (check-slime-interrupts)
1426 (let* ((header (read-chunk stream 6))
1427 (length (parse-integer header :radix #x10))
1428 (payload (read-chunk stream length)))
1429 (log-event "READ: ~S~%" payload)
1430 payload))
1431
1432 (defun read-chunk (stream length)
1433 (let* ((buffer (make-string length))
1434 (count (read-sequence buffer stream)))
1435 (assert (= count length) () "Short read: length=~D count=~D" length count)
1436 buffer))
1437
1438 (defun read-form (string)
1439 (with-standard-io-syntax
1440 (let ((*package* *swank-io-package*))
1441 (read-from-string string))))
1442
1443 (defun input-available-p (stream)
1444 ;; return true iff we can read from STREAM without waiting or if we
1445 ;; hit EOF
1446 (let ((c (read-char-no-hang stream nil :eof)))
1447 (cond ((not c) nil)
1448 ((eq c :eof) t)
1449 (t
1450 (unread-char c stream)
1451 t))))
1452
1453 (defvar *slime-features* nil
1454 "The feature list that has been sent to Emacs.")
1455
1456 (defun send-oob-to-emacs (object)
1457 (send-to-emacs object))
1458
1459 (defun encode-message (message stream)
1460 (let* ((string (prin1-to-string-for-emacs message))
1461 (length (length string)))
1462 (log-event "WRITE: ~A~%" string)
1463 (without-interrupts
1464 (let ((*print-pretty* nil))
1465 (format stream "~6,'0x" length))
1466 (write-string string stream))
1467 ;;(terpri stream)
1468 (finish-output stream)))
1469
1470 (defun prin1-to-string-for-emacs (object)
1471 (with-standard-io-syntax
1472 (let ((*print-case* :downcase)
1473 (*print-readably* nil)
1474 (*print-pretty* nil)
1475 (*package* *swank-io-package*))
1476 (prin1-to-string object))))
1477
1478 (defun force-user-output ()
1479 (force-output (connection.user-io *emacs-connection*)))
1480
1481 (defun clear-user-input ()
1482 (clear-input (connection.user-input *emacs-connection*)))
1483
1484 (defvar *tag-counter* 0)
1485
1486 (defun make-tag ()
1487 (setq *tag-counter* (mod (1+ *tag-counter*) (expt 2 22))))
1488
1489 (defun read-user-input-from-emacs ()
1490 (let ((tag (make-tag)))
1491 (force-output)
1492 (send-to-emacs `(:read-string ,(current-thread-id) ,tag))
1493 (let ((ok nil))
1494 (unwind-protect
1495 (prog1 (caddr (wait-for-event `(:emacs-return-string ,tag value)))
1496 (setq ok t))
1497 (unless ok
1498 (send-to-emacs `(:read-aborted ,(current-thread-id) ,tag)))))))
1499
1500 (defun y-or-n-p-in-emacs (format-string &rest arguments)
1501 "Like y-or-n-p, but ask in the Emacs minibuffer."
1502 (let ((tag (make-tag))
1503 (question (apply #'format nil format-string arguments)))
1504 (force-output)
1505 (send-to-emacs `(:y-or-n-p ,(current-thread-id) ,tag ,question))
1506 (caddr (wait-for-event `(:emacs-return ,tag result)))))
1507
1508 (defun process-form-for-emacs (form)
1509 "Returns a string which emacs will read as equivalent to
1510 FORM. FORM can contain lists, strings, characters, symbols and
1511 numbers.
1512
1513 Characters are converted emacs' ?<char> notaion, strings are left
1514 as they are (except for espacing any nested \" chars, numbers are
1515 printed in base 10 and symbols are printed as their symbol-name
1516 converted to lower case."
1517 (etypecase form
1518 (string (format nil "~S" form))
1519 (cons (format nil "(~A . ~A)"
1520 (process-form-for-emacs (car form))
1521 (process-form-for-emacs (cdr form))))
1522 (character (format nil "?~C" form))
1523 (symbol (concatenate 'string (when (eq (symbol-package form)
1524 #.(find-package "KEYWORD"))
1525 ":")
1526 (string-downcase (symbol-name form))))
1527 (number (let ((*print-base* 10))
1528 (princ-to-string form)))))
1529
1530 (defun eval-in-emacs (form &optional nowait)
1531 "Eval FORM in Emacs."
1532 (cond (nowait
1533 (send-to-emacs `(:eval-no-wait ,(process-form-for-emacs form))))
1534 (t
1535 (force-output)
1536 (let ((tag (make-tag)))
1537 (send-to-emacs `(:eval ,(current-thread-id) ,tag
1538 ,(process-form-for-emacs form)))
1539 (let ((value (caddr (wait-for-event `(:emacs-return ,tag result)))))
1540 (destructure-case value
1541 ((:ok value) value)
1542 ((:abort) (abort))))))))
1543
1544 (defvar *swank-wire-protocol-version* nil
1545 "The version of the swank/slime communication protocol.")
1546
1547 (defslimefun connection-info ()
1548 "Return a key-value list of the form:
1549 \(&key PID STYLE LISP-IMPLEMENTATION MACHINE FEATURES PACKAGE VERSION)
1550 PID: is the process-id of Lisp process (or nil, depending on the STYLE)
1551 STYLE: the communication style
1552 LISP-IMPLEMENTATION: a list (&key TYPE NAME VERSION)
1553 FEATURES: a list of keywords
1554 PACKAGE: a list (&key NAME PROMPT)
1555 VERSION: the protocol version"
1556 (setq *slime-features* *features*)
1557 `(:pid ,(getpid) :style ,(connection.communication-style *emacs-connection*)
1558 :lisp-implementation (:type ,(lisp-implementation-type)
1559 :name ,(lisp-implementation-type-name)
1560 :version ,(lisp-implementation-version))
1561 :machine (:instance ,(machine-instance)
1562 :type ,(machine-type)
1563 :version ,(machine-version))
1564 :features ,(features-for-emacs)
1565 :modules ,*modules*
1566 :package (:name ,(package-name *package*)
1567 :prompt ,(package-string-for-prompt *package*))
1568 :version ,*swank-wire-protocol-version*))
1569
1570 (defslimefun io-speed-test (&optional (n 1000) (m 1))
1571 (let* ((s *standard-output*)
1572 (*trace-output* (make-broadcast-stream s *log-output*)))
1573 (time (progn
1574 (dotimes (i n)
1575 (format s "~D abcdefghijklm~%" i)
1576 (when (zerop (mod n m))
1577 (finish-output s)))
1578 (finish-output s)
1579 (when *emacs-connection*
1580 (eval-in-emacs '(message "done.")))))
1581 (terpri *trace-output*)
1582 (finish-output *trace-output*)
1583 nil))
1584
1585
1586 ;;;; Reading and printing
1587
1588 (defmacro define-special (name doc)
1589 "Define a special variable NAME with doc string DOC.
1590 This is like defvar, but NAME will not be initialized."
1591 `(progn
1592 (defvar ,name)
1593 (setf (documentation ',name 'variable) ,doc)))
1594
1595 (define-special *buffer-package*
1596 "Package corresponding to slime-buffer-package.
1597
1598 EVAL-FOR-EMACS binds *buffer-package*. Strings originating from a slime
1599 buffer are best read in this package. See also FROM-STRING and TO-STRING.")
1600
1601 (define-special *buffer-readtable*
1602 "Readtable associated with the current buffer")
1603
1604 (defmacro with-buffer-syntax ((&rest _) &body body)
1605 "Execute BODY with appropriate *package* and *readtable* bindings.
1606
1607 This should be used for code that is conceptionally executed in an
1608 Emacs buffer."
1609 (destructuring-bind () _
1610 `(call-with-buffer-syntax (lambda () ,@body))))
1611
1612 (defun call-with-buffer-syntax (fun)
1613 (let ((*package* *buffer-package*))
1614 ;; Don't shadow *readtable* unnecessarily because that prevents
1615 ;; the user from assigning to it.
1616 (if (eq *readtable* *buffer-readtable*)
1617 (call-with-syntax-hooks fun)
1618 (let ((*readtable* *buffer-readtable*))
1619 (call-with-syntax-hooks fun)))))
1620
1621 (defun to-string (object)
1622 "Write OBJECT in the *BUFFER-PACKAGE*.
1623 The result may not be readable. Handles problems with PRINT-OBJECT methods
1624 gracefully."
1625 (with-buffer-syntax ()
1626 (let ((*print-readably* nil))
1627 (handler-case
1628 (prin1-to-string object)
1629 (error ()
1630 (with-output-to-string (s)
1631 (print-unreadable-object (object s :type t :identity t)
1632 (princ "<<error printing object>>" s))))))))
1633
1634 (defun from-string (string)
1635 "Read string in the *BUFFER-PACKAGE*"
1636 (with-buffer-syntax ()
1637 (let ((*read-suppress* nil))
1638 (read-from-string string))))
1639
1640 ;; FIXME: deal with #\| etc. hard to do portably.
1641 (defun tokenize-symbol (string)
1642 "STRING is interpreted as the string representation of a symbol
1643 and is tokenized accordingly. The result is returned in three
1644 values: The package identifier part, the actual symbol identifier
1645 part, and a flag if the STRING represents a symbol that is
1646 internal to the package identifier part. (Notice that the flag is
1647 also true with an empty package identifier part, as the STRING is
1648 considered to represent a symbol internal to some current package.)"
1649 (let ((package (let ((pos (position #\: string)))
1650 (if pos (subseq string 0 pos) nil)))
1651 (symbol (let ((pos (position #\: string :from-end t)))
1652 (if pos (subseq string (1+ pos)) string)))
1653 (internp (not (= (count #\: string) 1))))
1654 (values symbol package internp)))
1655
1656 (defun tokenize-symbol-thoroughly (string)
1657 "This version of TOKENIZE-SYMBOL handles escape characters."
1658 (let ((package nil)
1659 (token (make-array (length string) :element-type 'character
1660 :fill-pointer 0))
1661 (backslash nil)
1662 (vertical nil)
1663 (internp nil))
1664 (loop for char across string
1665 do (cond
1666 (backslash
1667 (vector-push-extend char token)
1668 (setq backslash nil))
1669 ((char= char #\\) ; Quotes next character, even within |...|
1670 (setq backslash t))
1671 ((char= char #\|)
1672 (setq vertical t))
1673 (vertical
1674 (vector-push-extend char token))
1675 ((char= char #\:)
1676 (if package
1677 (setq internp t)
1678 (setq package token
1679 token (make-array (length string)
1680 :element-type 'character
1681 :fill-pointer 0))))
1682 (t
1683 (vector-push-extend (casify-char char) token))))
1684 (values token package (or (not package) internp))))
1685
1686 (defun untokenize-symbol (package-name internal-p symbol-name)
1687 "The inverse of TOKENIZE-SYMBOL.
1688
1689 (untokenize-symbol \"quux\" nil \"foo\") ==> \"quux:foo\"
1690 (untokenize-symbol \"quux\" t \"foo\") ==> \"quux::foo\"
1691 (untokenize-symbol nil nil \"foo\") ==> \"foo\"
1692 "
1693 (cond ((not package-name) symbol-name)
1694 (internal-p (cat package-name "::" symbol-name))
1695 (t (cat package-name ":" symbol-name))))
1696
1697 (defun casify-char (char)
1698 "Convert CHAR accoring to readtable-case."
1699 (ecase (readtable-case *readtable*)
1700 (:preserve char)
1701 (:upcase (char-upcase char))
1702 (:downcase (char-downcase char))
1703 (:invert (if (upper-case-p char)
1704 (char-downcase char)
1705 (char-upcase char)))))
1706
1707 (defun parse-symbol (string &optional (package *package*))
1708 "Find the symbol named STRING.
1709 Return the symbol and a flag indicating whether the symbols was found."
1710 (multiple-value-bind (sname pname) (tokenize-symbol-thoroughly string)
1711 (let ((package (cond ((string= pname "") keyword-package)
1712 (pname (find-package pname))
1713 (t package))))
1714 (if package
1715 (multiple-value-bind (symbol flag) (find-symbol sname package)
1716 (values symbol flag sname package))
1717 (values nil nil nil nil)))))
1718
1719 (defun parse-symbol-or-lose (string &optional (package *package*))
1720 (multiple-value-bind (symbol status) (parse-symbol string package)
1721 (if status
1722 (values symbol status)
1723 (error "Unknown symbol: ~A [in ~A]" string package))))
1724
1725 (defun parse-package (string)
1726 "Find the package named STRING.
1727 Return the package or nil."
1728 ;; STRING comes usually from a (in-package STRING) form.
1729 (ignore-errors
1730 (find-package (let ((*package* *swank-io-package*))
1731 (read-from-string string)))))
1732
1733 (defun unparse-name (string)
1734 "Print the name STRING according to the current printer settings."
1735 ;; this is intended for package or symbol names
1736 (subseq (prin1-to-string (make-symbol string)) 2))
1737
1738 (defun guess-package (string)
1739 "Guess which package corresponds to STRING.
1740 Return nil if no package matches."
1741 (or (find-package string)
1742 (parse-package string)
1743 (if (find #\! string) ; for SBCL
1744 (guess-package (substitute #\- #\! string)))))
1745
1746 (defvar *readtable-alist* (default-readtable-alist)
1747 "An alist mapping package names to readtables.")
1748
1749 (defun guess-buffer-readtable (package-name)
1750 (let ((package (guess-package package-name)))
1751 (or (and package
1752 (cdr (assoc (package-name package) *readtable-alist*
1753 :test #'string=)))
1754 *readtable*)))
1755
1756
1757 ;;;; Evaluation
1758
1759 (defvar *pending-continuations* '()
1760 "List of continuations for Emacs. (thread local)")
1761
1762 (defun guess-buffer-package (string)
1763 "Return a package for STRING.
1764 Fall back to the the current if no such package exists."
1765 (or (and string (guess-package string))
1766 *package*))
1767
1768 (defun eval-for-emacs (form buffer-package id)
1769 "Bind *BUFFER-PACKAGE* to BUFFER-PACKAGE and evaluate FORM.
1770 Return the result to the continuation ID.
1771 Errors are trapped and invoke our debugger."
1772 (let (ok result)
1773 (unwind-protect
1774 (let ((*buffer-package* (guess-buffer-package buffer-package))
1775 (*buffer-readtable* (guess-buffer-readtable buffer-package))
1776 (*pending-continuations* (cons id *pending-continuations*)))
1777 (check-type *buffer-package* package)
1778 (check-type *buffer-readtable* readtable)
1779 ;; APPLY would be cleaner than EVAL.
1780 ;;(setq result (apply (car form) (cdr form)))
1781 (setq result (with-slime-interrupts (eval form)))
1782 (run-hook *pre-reply-hook*)
1783 (setq ok t))
1784 (send-to-emacs `(:return ,(current-thread)
1785 ,(if ok
1786 `(:ok ,result)
1787 `(:abort))
1788 ,id)))))
1789
1790 (defvar *echo-area-prefix* "=> "
1791 "A prefix that `format-values-for-echo-area' should use.")
1792
1793 (defun format-values-for-echo-area (values)
1794 (with-buffer-syntax ()
1795 (let ((*print-readably* nil))
1796 (cond ((null values) "; No value")
1797 ((and (integerp (car values)) (null (cdr values)))
1798 (let ((i (car values)))
1799 (format nil "~A~D (#x~X, #o~O, #b~B)"
1800 *echo-area-prefix* i i i i)))
1801 (t (format nil "~a~{~S~^, ~}" *echo-area-prefix* values))))))
1802
1803 (defslimefun interactive-eval (string)
1804 (with-buffer-syntax ()
1805 (let ((values (multiple-value-list (eval (from-string string)))))
1806 (fresh-line)
1807 (finish-output)
1808 (format-values-for-echo-area values))))
1809
1810 (defslimefun eval-and-grab-output (string)
1811 (with-buffer-syntax ()
1812 (let* ((s (make-string-output-stream))
1813 (*standard-output* s)
1814 (values (multiple-value-list (eval (from-string string)))))
1815 (list (get-output-stream-string s)
1816 (format nil "~{~S~^~%~}" values)))))
1817
1818 (defun eval-region (string)
1819 "Evaluate STRING.
1820 Return the results of the last form as a list and as secondary value the
1821 last form."
1822 (with-input-from-string (stream string)
1823 (let (- values)
1824 (loop
1825 (let ((form (read stream nil stream)))
1826 (when (eq form stream)
1827 (return (values values -)))
1828 (setq - form)
1829 (setq values (multiple-value-list (eval form)))
1830 (finish-output))))))
1831
1832 (defslimefun interactive-eval-region (string)
1833 (with-buffer-syntax ()
1834 (format-values-for-echo-area (eval-region string))))
1835
1836 (defslimefun re-evaluate-defvar (form)
1837 (with-buffer-syntax ()
1838 (let ((form (read-from-string form)))
1839 (destructuring-bind (dv name &optional value doc) form
1840 (declare (ignore value doc))
1841 (assert (eq dv 'defvar))
1842 (makunbound name)
1843 (prin1-to-string (eval form))))))
1844
1845 (defvar *swank-pprint-bindings*
1846 `((*print-pretty* . t)
1847 (*print-level* . nil)
1848 (*print-length* . nil)
1849 (*print-circle* . t)
1850 (*print-gensym* . t)
1851 (*print-readably* . nil))
1852 "A list of variables bindings during pretty printing.
1853 Used by pprint-eval.")
1854
1855 (defun swank-pprint (list)
1856 "Bind some printer variables and pretty print each object in LIST."
1857 (with-buffer-syntax ()
1858 (with-bindings *swank-pprint-bindings*
1859 (cond ((null list) "; No value")
1860 (t (with-output-to-string (*standard-output*)
1861 (dolist (o list)
1862 (pprint o)
1863 (terpri))))))))
1864
1865 (defslimefun pprint-eval (string)
1866 (with-buffer-syntax ()
1867 (swank-pprint (multiple-value-list (eval (read-from-string string))))))
1868
1869 (defslimefun set-package (name)
1870 "Set *package* to the package named NAME.
1871 Return the full package-name and the string to use in the prompt."
1872 (let ((p (guess-package name)))
1873 (assert (packagep p))
1874 (setq *package* p)
1875 (list (package-name p) (package-string-for-prompt p))))
1876
1877 ;;;;; Listener eval
1878
1879 (defvar *listener-eval-function* 'repl-eval)
1880
1881 (defslimefun listener-eval (string)
1882 (funcall *listener-eval-function* string))
1883
1884 (defvar *send-repl-results-function* 'send-repl-results-to-emacs)
1885
1886 (defun repl-eval (string)
1887 (clear-user-input)
1888 (with-buffer-syntax ()
1889 (track-package
1890 (lambda ()
1891 (multiple-value-bind (values last-form) (eval-region string)
1892 (setq *** ** ** * * (car values)
1893 /// // // / / values
1894 +++ ++ ++ + + last-form)
1895 (funcall *send-repl-results-function* values)))))
1896 nil)
1897
1898 (defun track-package (fun)
1899 (let ((p *package*))
1900 (unwind-protect (funcall fun)
1901 (unless (eq *package* p)
1902 (send-to-emacs (list :new-package (package-name *package*)
1903 (package-string-for-prompt *package*)))))))
1904
1905 (defun send-repl-results-to-emacs (values)
1906 (fresh-line)
1907 (finish-output)
1908 (if (null values)
1909 (send-to-emacs `(:write-string "; No value" :repl-result))
1910 (dolist (v values)
1911 (send-to-emacs `(:write-string ,(cat (prin1-to-string v) #\newline)
1912 :repl-result)))))
1913
1914 (defun cat (&rest strings)
1915 "Concatenate all arguments and make the result a string."
1916 (with-output-to-string (out)
1917 (dolist (s strings)
1918 (etypecase s
1919 (string (write-string s out))
1920 (character (write-char s out))))))
1921
1922 (defun package-string-for-prompt (package)
1923 "Return the shortest nickname (or canonical name) of PACKAGE."
1924 (unparse-name
1925 (or (canonical-package-nickname package)
1926 (auto-abbreviated-package-name package)
1927 (shortest-package-nickname package))))
1928
1929 (defun canonical-package-nickname (package)
1930 "Return the canonical package nickname, if any, of PACKAGE."
1931 (let ((name (cdr (assoc (package-name package) *canonical-package-nicknames*
1932 :test #'string=))))
1933 (and name (string name))))
1934
1935 (defun auto-abbreviated-package-name (package)
1936 "Return an abbreviated 'name' for PACKAGE.
1937
1938 N.B. this is not an actual package name or nickname."
1939 (when *auto-abbreviate-dotted-packages*
1940 (let ((last-dot (position #\. (package-name package) :from-end t)))
1941 (when last-dot (subseq (package-name package) (1+ last-dot))))))
1942
1943 (defun shortest-package-nickname (package)
1944 "Return the shortest nickname (or canonical name) of PACKAGE."
1945 (loop for name in (cons (package-name package) (package-nicknames package))
1946 for shortest = name then (if (< (length name) (length shortest))
1947 name
1948 shortest)
1949 finally (return shortest)))
1950
1951 (defslimefun ed-in-emacs (&optional what)
1952 "Edit WHAT in Emacs.
1953
1954 WHAT can be:
1955 A pathname or a string,
1956 A list (PATHNAME-OR-STRING LINE [COLUMN]),
1957 A function name (symbol or cons),
1958 NIL.
1959
1960 Returns true if it actually called emacs, or NIL if not."
1961 (flet ((pathname-or-string-p (thing)
1962 (or (pathnamep thing) (typep thing 'string)))
1963 (canonicalize-filename (filename)
1964 (namestring (or (probe-file filename) filename))))
1965 (let ((target
1966 (cond ((and (listp what) (pathname-or-string-p (first what)))
1967 (cons (canonicalize-filename (car what)) (cdr what)))
1968 ((pathname-or-string-p what)
1969 (canonicalize-filename what))
1970 ((symbolp what) what)
1971 ((consp what) what)
1972 (t (return-from ed-in-emacs nil)))))
1973 (cond
1974 (*emacs-connection* (send-oob-to-emacs `(:ed ,target)))
1975 ((default-connection)
1976 (with-connection ((default-connection))
1977 (send-oob-to-emacs `(:ed ,target))))
1978 (t nil)))))
1979
1980 (defslimefun inspect-in-emacs (what)
1981 "Inspect WHAT in Emacs."
1982 (flet ((send-it ()
1983 (with-buffer-syntax ()
1984 (reset-inspector)
1985 (send-oob-to-emacs `(:inspect ,(inspect-object what))))))
1986 (cond
1987 (*emacs-connection*
1988 (send-it))
1989 ((default-connection)
1990 (with-connection ((default-connection))
1991 (send-it))))
1992 what))
1993
1994 (defslimefun value-for-editing (form)
1995 "Return a readable value of FORM for editing in Emacs.
1996 FORM is expected, but not required, to be SETF'able."
1997 ;; FIXME: Can we check FORM for setfability? -luke (12/Mar/2005)
1998 (with-buffer-syntax ()
1999 (prin1-to-string (eval (read-from-string form)))))
2000
2001 (defslimefun commit-edited-value (form value)
2002 "Set the value of a setf'able FORM to VALUE.
2003 FORM and VALUE are both strings from Emacs."
2004 (with-buffer-syntax ()
2005 (eval `(setf ,(read-from-string form)
2006 ,(read-from-string (concatenate 'string "`" value))))
2007 t))
2008
2009 (defun background-message (format-string &rest args)
2010 "Display a message in Emacs' echo area.
2011
2012 Use this function for informative messages only. The message may even
2013 be dropped, if we are too busy with other things."
2014 (when *emacs-connection*
2015 (send-to-emacs `(:background-message
2016 ,(apply #'format nil format-string args)))))
2017
2018
2019 ;;;; Debugger
2020
2021 (defun invoke-slime-debugger (condition)
2022 "Sends a message to Emacs declaring that the debugger has been entered,
2023 then waits to handle further requests from Emacs. Eventually returns
2024 after Emacs causes a restart to be invoked."
2025 (without-slime-interrupts
2026 (cond (*emacs-connection*
2027 (debug-in-emacs condition))
2028 ((default-connection)
2029 (with-connection ((default-connection))
2030 (debug-in-emacs condition))))))
2031
2032 (defun swank-debugger-hook (condition hook)
2033 "Debugger function for binding *DEBUGGER-HOOK*."
2034 (restart-case
2035 (call-with-debugger-hook
2036 hook (lambda () (invoke-slime-debugger condition)))
2037 (default-debugger (&optional v)
2038 :report "Use default debugger." (declare (ignore v))
2039 (invoke-default-debugger condition))))
2040
2041 (defun invoke-default-debugger (condition)
2042 (let ((*debugger-hook* nil))
2043 (invoke-debugger condition)))
2044
2045 (defvar *global-debugger* t
2046 "Non-nil means the Swank debugger hook will be installed globally.")
2047
2048 (add-hook *new-connection-hook* 'install-debugger)
2049 (defun install-debugger (connection)
2050 (declare (ignore connection))
2051 (when *global-debugger*
2052 (install-debugger-globally #'swank-debugger-hook)))
2053
2054 ;;;;; Debugger loop
2055 ;;;
2056 ;;; These variables are dynamically bound during debugging.
2057 ;;;
2058 (defvar *swank-debugger-condition* nil
2059 "The condition being debugged.")
2060
2061 (defvar *sldb-level* 0
2062 "The current level of recursive debugging.")
2063
2064 (defvar *sldb-initial-frames* 20
2065 "The initial number of backtrace frames to send to Emacs.")
2066
2067 (defvar *sldb-restarts* nil
2068 "The list of currenlty active restarts.")
2069
2070 (defvar *sldb-stepping-p* nil
2071 "True during execution of a step command.")
2072
2073 (defun debug-in-emacs (condition)
2074 (let ((*swank-debugger-condition* condition)
2075 (*sldb-restarts* (compute-sane-restarts condition))
2076 (*package* (or (and (boundp '*buffer-package*)
2077 (symbol-value '*buffer-package*))
2078 *package*))
2079 (*sldb-level* (1+ *sldb-level*))
2080 (*sldb-stepping-p* nil)
2081 (*swank-state-stack* (cons :swank-debugger-hook *swank-state-stack*)))
2082 (force-user-output)
2083 (call-with-debugging-environment
2084 (lambda ()
2085 (with-bindings *sldb-printer-bindings*
2086 (sldb-loop *sldb-level*))))))
2087
2088 (defun sldb-loop (level)
2089 (unwind-protect
2090 (loop
2091 (with-simple-restart (abort "Return to sldb level ~D." level)
2092 (send-to-emacs
2093 (list* :debug (current-thread-id) level
2094 (debugger-info-for-emacs 0 *sldb-initial-frames*)))
2095 (loop
2096 (send-to-emacs (list :debug-activate (current-thread-id) level nil))
2097 (handler-case (process-requests nil t)
2098 (sldb-condition (c)
2099 (handle-sldb-condition c))))))
2100 (send-to-emacs `(:debug-return
2101 ,(current-thread-id) ,level ,*sldb-stepping-p*))))
2102
2103 (defun handle-sldb-condition (condition)
2104 "Handle an internal debugger condition.
2105 Rather than recursively debug the debugger (a dangerous idea!), these
2106 conditions are simply reported."
2107 (let ((real-condition (original-condition condition)))
2108 (send-to-emacs `(:debug-condition ,(current-thread-id)
2109 ,(princ-to-string real-condition)))))
2110
2111 (defvar *sldb-condition-printer* #'format-sldb-condition
2112 "Function called to print a condition to an SLDB buffer.")
2113
2114 (defun safe-condition-message (condition)
2115 "Safely print condition to a string, handling any errors during
2116 printing."
2117 (let ((*print-pretty* t) (*print-right-margin* 65))
2118 (handler-case
2119 (funcall *sldb-condition-printer* condition)
2120 (error (cond)
2121 ;; Beware of recursive errors in printing, so only use the condition
2122 ;; if it is printable itself:
2123 (format nil "Unable to display error condition~@[: ~A~]"
2124 (ignore-errors (princ-to-string cond)))))))
2125
2126 (defun debugger-condition-for-emacs ()
2127 (list (safe-condition-message *swank-debugger-condition*)
2128 (format nil " [Condition of type ~S]"
2129 (type-of *swank-debugger-condition*))
2130 (condition-extras *swank-debugger-condition*)))
2131
2132 (defun format-restarts-for-emacs ()
2133 "Return a list of restarts for *swank-debugger-condition* in a
2134 format suitable for Emacs."
2135 (let ((*print-right-margin* most-positive-fixnum))
2136 (loop for restart in *sldb-restarts*
2137 collect (list (princ-to-string (restart-name restart))
2138 (princ-to-string restart)))))
2139
2140
2141 ;;;;; SLDB entry points
2142
2143 (defslimefun sldb-break-with-default-debugger ()
2144 "Invoke the default debugger."
2145 (call-with-debugger-hook
2146 nil (lambda () (invoke-debugger *swank-debugger-condition*)))
2147 (send-to-emacs
2148 (list :debug-activate (current-thread-id) *sldb-level* t)))
2149
2150 (defslimefun backtrace (start end)
2151 "Return a list ((I FRAME) ...) of frames from START to END.
2152 I is an integer describing and FRAME a string."
2153 (loop for frame in (compute-backtrace start end)
2154 for i from start
2155 collect (list i (with-output-to-string (stream)
2156 (handler-case
2157 (with-bindings *backtrace-printer-bindings*
2158 (print-frame frame stream))
2159 (t ()
2160 (format stream "[error printing frame]")))))))
2161
2162 (defslimefun debugger-info-for-emacs (start end)
2163 "Return debugger state, with stack frames from START to END.
2164 The result is a list:
2165 (condition ({restart}*) ({stack-frame}*) (cont*))
2166 where
2167 condition ::= (description type [extra])
2168 restart ::= (name description)
2169 stack-frame ::= (number description)
2170 extra ::= (:references and other random things)
2171 cont ::= continutation
2172 condition---a pair of strings: message, and type. If show-source is
2173 not nil it is a frame number for which the source should be displayed.
2174
2175 restart---a pair of strings: restart name, and description.
2176
2177 stack-frame---a number from zero (the top), and a printed
2178 representation of the frame's call.
2179
2180 continutation---the id of a pending Emacs continuation.
2181
2182 Below is an example return value. In this case the condition was a
2183 division by zero (multi-line description), and only one frame is being
2184 fetched (start=0, end=1).
2185
2186 ((\"Arithmetic error DIVISION-BY-ZERO signalled.
2187 Operation was KERNEL::DIVISION, operands (1 0).\"
2188 \"[Condition of type DIVISION-BY-ZERO]\")
2189 ((\"ABORT\" \"Return to Slime toplevel.\")
2190 (\"ABORT\" \"Return to Top-Level.\"))
2191 ((0 \"(KERNEL::INTEGER-/-INTEGER 1 0)\"))
2192 (4))"
2193 (list (debugger-condition-for-emacs)
2194 (format-restarts-for-emacs)
2195 (backtrace start end)
2196 *pending-continuations*))
2197
2198 (defun nth-restart (index)
2199 (nth index *sldb-restarts*))
2200
2201 (defslimefun invoke-nth-restart (index)
2202 (invoke-restart-interactively (nth-restart index)))
2203
2204 (defslimefun sldb-abort ()
2205 (invoke-restart (find 'abort *sldb-restarts* :key #'restart-name)))
2206
2207 (defslimefun sldb-continue ()
2208 (continue))
2209
2210 (defslimefun throw-to-toplevel ()
2211 "Invoke the ABORT-REQUEST restart abort an RPC from Emacs.
2212 If we are not evaluating an RPC then ABORT instead."
2213 (let ((restart (find-restart *sldb-quit-restart*)))
2214 (cond (restart (invoke-restart restart))
2215 (t (format nil
2216 "Restart not found: ~a"
2217 *sldb-quit-restart*)))))
2218
2219 (defslimefun invoke-nth-restart-for-emacs (sldb-level n)
2220 "Invoke the Nth available restart.
2221 SLDB-LEVEL is the debug level when the request was made. If this
2222 has changed, ignore the request."
2223 (when (= sldb-level *sldb-level*)
2224 (invoke-nth-restart n)))
2225
2226 (defun wrap-sldb-vars (form)
2227 `(let ((*sldb-level* ,*sldb-level*))
2228 ,form))
2229
2230 (defslimefun eval-string-in-frame (string index)
2231 (to-string (eval-in-frame (wrap-sldb-vars (from-string string))
2232 index)))
2233
2234 (defslimefun pprint-eval-string-in-frame (string index)
2235 (swank-pprint
2236 (multiple-value-list
2237 (eval-in-frame (wrap-sldb-vars (from-string string)) index))))
2238
2239 (defslimefun frame-locals-for-emacs (index)
2240 "Return a property list ((&key NAME ID VALUE) ...) describing
2241 the local variables in the frame INDEX."
2242 (with-bindings *backtrace-printer-bindings*
2243 (mapcar (lambda (frame-locals)
2244 (destructuring-bind (&key name id value) frame-locals
2245 (list :name (prin1-to-string name) :id id
2246 :value (to-string value))))
2247 (frame-locals index))))
2248
2249 (defslimefun frame-catch-tags-for-emacs (frame-index)
2250 (mapcar #'to-string (frame-catch-tags frame-index)))
2251
2252 (defslimefun sldb-disassemble (index)
2253 (with-output-to-string (*standard-output*)
2254 (disassemble-frame index)))
2255
2256 (defslimefun sldb-return-from-frame (index string)
2257 (let ((form (from-string string)))
2258 (to-string (multiple-value-list (return-from-frame index form)))))
2259
2260 (defslimefun sldb-break (name)
2261 (with-buffer-syntax ()
2262 (sldb-break-at-start (read-from-string name))))
2263
2264 (defmacro define-stepper-function (name backend-function-name)
2265 `(defslimefun ,name (frame)
2266 (cond ((sldb-stepper-condition-p *swank-debugger-condition*)
2267 (setq *sldb-stepping-p* t)
2268 (,backend-function-name))
2269 ((find-restart 'continue)
2270 (activate-stepping frame)
2271 (setq *sldb-stepping-p* t)
2272 (continue))
2273 (t
2274 (error "Not currently single-stepping, and no continue restart available.")))))
2275
2276 (define-stepper-function sldb-step sldb-step-into)
2277 (define-stepper-function sldb-next sldb-step-next)
2278 (define-stepper-function sldb-out sldb-step-out)
2279
2280
2281 ;;;; Compilation Commands.
2282
2283 (defstruct (:swank-compilation-unit
2284 (:type list) :named
2285 (:conc-name swank-compilation-unit.)
2286 (:constructor make-swank-compilation-unit ()))
2287 notes ;
2288 results ; a result is of type (MEMBER T NIL :COMPLAINED)
2289 durations ;
2290 )
2291
2292 (defvar *swank-compilation-unit* nil)
2293
2294 (defun measure-time-interval (fn)
2295 "Call FN and return the first return value and the elapsed time.
2296 The time is measured in microseconds."
2297 (declare (type function fn))
2298 (let ((before (get-internal-real-time)))
2299 (values
2300 (funcall fn)
2301 (* (- (get-internal-real-time) before)
2302 (/ 1000000 internal-time-units-per-second)))))
2303
2304 (defun record-note-for-condition (condition)
2305 "Record a note for a compiler-condition into the currently active
2306 Swank-Compilation-Unit."
2307 (push (make-compiler-note condition)
2308 (swank-compilation-unit.notes *swank-compilation-unit*)))
2309
2310 (defun make-compiler-note (condition)
2311 "Make a compiler note data structure from a compiler-condition."
2312 (declare (type compiler-condition condition))
2313 (list* :message (message condition)
2314 :severity (severity condition)
2315 :location (location condition)
2316 :references (references condition)
2317 (let ((s (short-message condition)))
2318 (if s (list :short-message s)))))
2319
2320 (defmacro with-swank-compilation-unit ((&key override) &body body)
2321 "Similiar to CL:WITH-COMPILATION-UNIT. Within a
2322 Swank-Compilation-Unit all notes, results etc. produced by
2323 COMPILE-FILE-FOR-EMACS and COMPILE-STRING-FOR-EMACS (possibly called
2324 more than once) will be collected into this unit."
2325 (if override
2326 `(let ((*swank-compilation-unit* (make-swank-compilation-unit)))
2327 ,@body)
2328 `(let ((*swank-compilation-unit* (or *swank-compilation-unit*
2329 (make-swank-compilation-unit))))
2330 ,@body)))
2331
2332 (defun swank-compilation-unit-for-emacs (unit)
2333 "Make a Swank-Compilation-Unit suitable for Emacs."
2334 (let ((new (make-swank-compilation-unit)))
2335 (with-struct (swank-compilation-unit. notes results durations) unit
2336 (setf (swank-compilation-unit.notes new) (reverse notes))
2337 (setf (swank-compilation-unit.results new) (reverse results))
2338 (setf (swank-compilation-unit.durations new)
2339 (reverse (mapcar #'(lambda (usecs) (/ usecs 1000000.0)) durations))))
2340 new))
2341
2342 (defun swank-compiler (function)
2343 (let ((notes-p))
2344 (multiple-value-bind (result usecs)
2345 (with-simple-restart (abort "Abort SLIME compilation.")
2346 (handler-bind ((compiler-condition #'(lambda (c)
2347 (setf notes-p t)
2348 (record-note-for-condition c))))
2349 (measure-time-interval function)))
2350 (when result (setf result (if notes-p :complained t)))
2351 (when (eql usecs t) (setf usecs 0)) ; compilation aborted.
2352 (push result (swank-compilation-unit.results *swank-compilation-unit*))
2353 (push usecs (swank-compilation-unit.durations *swank-compilation-unit*))
2354 (swank-compilation-unit-for-emacs *swank-compilation-unit*))))
2355
2356 (defslimefun compile-file-for-emacs (filename load-p)
2357 "Compile FILENAME and, when LOAD-P, load the result.
2358 Record compiler notes signalled as `compiler-condition's."
2359 (with-swank-compilation-unit (:override nil)
2360 (with-buffer-syntax ()
2361 (let ((*compile-print* nil))
2362 (swank-compiler
2363 (lambda ()
2364 (swank-compile-file filename load-p
2365 (or (guess-external-format filename)
2366 :default))))))))
2367
2368 (defslimefun compile-string-for-emacs (string buffer position directory debug)
2369 "Compile STRING (exerpted from BUFFER at POSITION).
2370 Record compiler notes signalled as `compiler-condition's."
2371 (with-swank-compilation-unit (:override nil)
2372 (with-buffer-syntax ()
2373 (swank-compiler
2374 (lambda ()
2375 (let ((*compile-print* nil) (*compile-verbose* t))
2376 (swank-compile-string string :buffer buffer :position position
2377 :directory directory
2378 :debug debug)))))))
2379
2380
2381 (defun file-newer-p (new-file old-file)
2382 "Returns true if NEW-FILE is newer than OLD-FILE."
2383 (> (file-write-date new-file) (file-write-date old-file)))
2384
2385 (defun requires-compile-p (source-file)
2386 (let ((fasl-file (probe-file (compile-file-pathname source-file))))
2387 (or (not fasl-file)
2388 (file-newer-p source-file fasl-file))))
2389
2390 (defslimefun compile-file-if-needed (filename loadp)
2391 (cond ((requires-compile-p filename)
2392 (compile-file-for-emacs filename loadp))
2393 (loadp
2394 (load (compile-file-pathname filename))
2395 nil)))
2396
2397
2398 ;;;; Loading
2399
2400 (defslimefun load-file (filename)
2401 (to-string (load filename)))
2402
2403
2404 ;;;;; swank-require
2405
2406 (defslimefun swank-require (modules &optional filename)
2407 "Load the module MODULE."
2408 (dolist (module (if (listp modules) modules (list modules)))
2409 (unless (member (string module) *modules* :test #'string=)
2410 (require module (or filename (module-filename module)))))
2411 *modules*)
2412
2413 (defvar *find-module* 'find-module
2414 "Pluggable function to locate modules.
2415 The function receives a module name as argument and should return
2416 the filename of the module (or nil if the file doesn't exist).")
2417
2418 (defun module-filename (module)
2419 "Return the filename for the module MODULE."
2420 (or (funcall *find-module* module)
2421 (error "Can't locate module: ~s" module)))
2422
2423 ;;;;;; Simple *find-module* function.
2424
2425 (defun merged-directory (dirname defaults)
2426 (pathname-directory
2427 (merge-pathnames
2428 (make-pathname :directory `(:relative ,dirname) :defaults defaults)
2429 defaults)))
2430
2431 (defvar *load-path* '()
2432 "A list of directories to search for modules.")
2433
2434 (defun module-canditates (name dir)
2435 (list (compile-file-pathname (make-pathname :name name :defaults dir))
2436 (make-pathname :name name :type "lisp" :defaults dir)))
2437
2438 (defun find-module (module)
2439 (let ((name (string-downcase module)))
2440 (some (lambda (dir) (some #'probe-file (module-canditates name dir)))
2441 *load-path*)))
2442
2443
2444 ;;;; Macroexpansion
2445
2446 (defvar *macroexpand-printer-bindings*
2447 '((*print-circle* . nil)
2448 (*print-pretty* . t)
2449 (*print-escape* . t)
2450 (*print-lines* . nil)
2451 (*print-level* . nil)
2452 (*print-length* . nil)))
2453
2454 (defun apply-macro-expander (expander string)
2455 (with-buffer-syntax ()
2456 (with-bindings *macroexpand-printer-bindings*
2457 (prin1-to-string (funcall expander (from-string string))))))
2458
2459 (defslimefun swank-macroexpand-1 (string)
2460 (apply-macro-expander #'macroexpand-1 string))
2461
2462 (defslimefun swank-macroexpand (string)
2463 (apply-macro-expander #'macroexpand string))
2464
2465 (defslimefun swank-macroexpand-all (string)
2466 (apply-macro-expander #'macroexpand-all string))
2467
2468 (defslimefun swank-compiler-macroexpand-1 (string)
2469 (apply-macro-expander #'compiler-macroexpand-1 string))
2470
2471 (defslimefun swank-compiler-macroexpand (string)
2472 (apply-macro-expander #'compiler-macroexpand string))
2473
2474 (defslimefun disassemble-symbol (name)
2475 (with-buffer-syntax ()
2476 (with-output-to-string (*standard-output*)
2477 (let ((*print-readably* nil))
2478 (disassemble (fdefinition (from-string name)))))))
2479
2480
2481 ;;;; Simple completion
2482
2483 (defslimefun simple-completions (string package)
2484 "Return a list of completions for the string STRING."
2485 (let ((strings (all-completions string package #'prefix-match-p)))
2486 (list strings (longest-common-prefix strings))))
2487
2488 (defun all-completions (string package test)
2489 (multiple-value-bind (name pname intern) (tokenize-symbol string)
2490 (let* ((extern (and pname (not intern)))
2491 (pack (cond ((equal pname "") keyword-package)
2492 ((not pname) (guess-buffer-package package))
2493 (t (guess-package pname))))
2494 (test (lambda (sym) (funcall test name (unparse-symbol sym))))
2495 (syms (and pack (matching-symbols pack extern test))))
2496 (format-completion-set (mapcar #'unparse-symbol syms) intern pname))))
2497
2498 (defun matching-symbols (package external test)
2499 (let ((test (if external
2500 (lambda (s)
2501 (and (symbol-external-p s package)
2502 (funcall test s)))
2503 test))
2504 (result '()))
2505 (do-symbols (s package)
2506 (when (funcall test s)
2507 (push s result)))
2508 (remove-duplicates result)))
2509
2510 (defun unparse-symbol (symbol)
2511 (let ((*print-case* (case (readtable-case *readtable*)
2512 (:downcase :upcase)
2513 (t :downcase))))
2514 (unparse-name (symbol-name symbol))))
2515
2516 (defun prefix-match-p (prefix string)
2517 "Return true if PREFIX is a prefix of STRING."
2518 (not (mismatch prefix string :end2 (min (length string) (length prefix)))))
2519
2520 (defun longest-common-prefix (strings)
2521 "Return the longest string that is a common prefix of STRINGS."
2522 (if (null strings)
2523 ""
2524 (flet ((common-prefix (s1 s2)
2525 (let ((diff-pos (mismatch s1 s2)))
2526 (if diff-pos (subseq s1 0 diff-pos) s1))))
2527 (reduce #'common-prefix strings))))
2528
2529 (defun format-completion-set (strings internal-p package-name)
2530 "Format a set of completion strings.
2531 Returns a list of completions with package qualifiers if needed."
2532 (mapcar (lambda (string) (untokenize-symbol package-name internal-p string))
2533 (sort strings #'string<)))
2534
2535
2536 ;;;; Simple arglist display
2537
2538 (defslimefun operator-arglist (name package)
2539 (ignore-errors
2540 (let ((args (arglist (parse-symbol name (guess-buffer-package package))))
2541 (*print-escape* nil))
2542 (cond ((eq args :not-available) nil)
2543 (t (format nil "(~a ~/pprint-fill/)" name args))))))
2544
2545
2546 ;;;; Documentation
2547
2548 (defslimefun apropos-list-for-emacs (name &optional external-only
2549 case-sensitive package)
2550 "Make an apropos search for Emacs.
2551 The result is a list of property lists."
2552 (let ((package (if package
2553 (or (parse-package package)
2554 (error "No such package: ~S" package)))))
2555 ;; The MAPCAN will filter all uninteresting symbols, i.e. those
2556 ;; who cannot be meaningfully described.
2557 (mapcan (listify #'briefly-describe-symbol-for-emacs)
2558 (sort (remove-duplicates
2559 (apropos-symbols name external-only case-sensitive package))
2560 #'present-symbol-before-p))))
2561
2562 (defun briefly-describe-symbol-for-emacs (symbol)
2563 "Return a property list describing SYMBOL.
2564 Like `describe-symbol-for-emacs' but with at most one line per item."
2565 (flet ((first-line (string)
2566 (let ((pos (position #\newline string)))
2567 (if (null pos) string (subseq string 0 pos)))))
2568 (let ((desc (map-if #'stringp #'first-line
2569 (describe-symbol-for-emacs symbol))))
2570 (if desc
2571 (list* :designator (to-string symbol) desc)))))
2572
2573 (defun map-if (test fn &rest lists)
2574 "Like (mapcar FN . LISTS) but only call FN on objects satisfying TEST.
2575 Example:
2576 \(map-if #'oddp #'- '(1 2 3 4 5)) => (-1 2 -3 4 -5)"
2577 (apply #'mapcar
2578 (lambda (x) (if (funcall test x) (funcall fn x) x))
2579 lists))
2580
2581 (defun listify (f)
2582 "Return a function like F, but which returns any non-null value
2583 wrapped in a list."
2584 (lambda (x)
2585 (let ((y (funcall f x)))
2586 (and y (list y)))))
2587
2588 (defun present-symbol-before-p (x y)
2589 "Return true if X belongs before Y in a printed summary of symbols.
2590 Sorted alphabetically by package name and then symbol name, except
2591 that symbols accessible in the current package go first."
2592 (declare (type symbol x y))
2593 (flet ((accessible (s)
2594 ;; Test breaks on NIL for package that does not inherit it
2595 (eq (find-symbol (symbol-name s) *buffer-package*) s)))
2596 (let ((ax (accessible x)) (ay (accessible y)))
2597 (cond ((and ax ay) (string< (symbol-name x) (symbol-name y)))
2598 (ax t)
2599 (ay nil)
2600 (t (let ((px (symbol-package x)) (py (symbol-package y)))
2601 (if (eq px py)
2602 (string< (symbol-name x) (symbol-name y))
2603 (string< (package-name px) (package-name py)))))))))
2604
2605 (defun make-apropos-matcher (pattern case-sensitive)
2606 (let ((chr= (if case-sensitive #'char= #'char-equal)))
2607 (lambda (symbol)
2608 (search pattern (string symbol) :test chr=))))
2609
2610 (defun apropos-symbols (string external-only case-sensitive package)
2611 (let ((packages (or package (remove (find-package :keyword)
2612 (list-all-packages))))
2613 (matcher (make-apropos-matcher string case-sensitive))
2614 (result))
2615 (with-package-iterator (next packages :external :internal)
2616 (loop (multiple-value-bind (morep symbol) (next)
2617 (cond ((not morep) (return))
2618 ((and (if external-only (symbol-external-p symbol) t)
2619 (funcall matcher symbol))
2620 (push symbol result))))))
2621 result))
2622
2623 (defun call-with-describe-settings (fn)
2624 (let ((*print-readably* nil))
2625 (funcall fn)))
2626
2627 (defmacro with-describe-settings ((&rest _) &body body)
2628 (declare (ignore _))
2629 `(call-with-describe-settings (lambda () ,@body)))
2630
2631 (defun describe-to-string (object)
2632 (with-describe-settings ()
2633 (with-output-to-string (*standard-output*)
2634 (describe object))))
2635
2636 (defslimefun describe-symbol (symbol-name)
2637 (with-buffer-syntax ()
2638 (describe-to-string (parse-symbol-or-lose symbol-name))))
2639
2640 (defslimefun describe-function (name)
2641 (with-buffer-syntax ()
2642 (let ((symbol (parse-symbol-or-lose name)))
2643 (describe-to-string (or (macro-function symbol)
2644 (symbol-function symbol))))))
2645
2646 (defslimefun describe-definition-for-emacs (name kind)
2647 (with-buffer-syntax ()
2648 (with-describe-settings ()
2649 (with-output-to-string (*standard-output*)
2650 (describe-definition (parse-symbol-or-lose name) kind)))))
2651
2652 (defslimefun documentation-symbol (symbol-name &optional default)
2653 (with-buffer-syntax ()
2654 (multiple-value-bind (sym foundp) (parse-symbol symbol-name)
2655 (if foundp
2656 (let ((vdoc (documentation sym 'variable))
2657 (fdoc (documentation sym 'function)))
2658 (or (and (or vdoc fdoc)
2659 (concatenate 'string
2660 fdoc
2661 (and vdoc fdoc '(#\Newline #\Newline))
2662 vdoc))
2663 default))
2664 default))))
2665
2666
2667 ;;;; Package Commands
2668
2669 (defslimefun list-all-package-names (&optional nicknames)
2670 "Return a list of all package names.
2671 Include the nicknames if NICKNAMES is true."
2672 (mapcar #'unparse-name
2673 (if nicknames
2674 (mapcan #'package-names (list-all-packages))
2675 (mapcar #'package-name (list-all-packages)))))
2676
2677
2678 ;;;; Tracing
2679
2680 ;; Use eval for the sake of portability...
2681 (defun tracedp (fspec)
2682 (member fspec (eval '(trace))))
2683
2684 (defslimefun swank-toggle-trace (spec-string)
2685 (let ((spec (from-string spec-string)))
2686 (cond ((consp spec) ; handle complicated cases in the backend
2687 (toggle-trace spec))
2688 ((tracedp spec)
2689 (eval `(untrace ,spec))
2690 (format nil "~S is now untraced." spec))
2691 (t
2692 (eval `(trace ,spec))
2693 (format nil "~S is now traced." spec)))))
2694
2695 (defslimefun untrace-all ()
2696 (untrace))
2697
2698 (defslimefun redirect-trace-output (target)
2699 (setf (connection.trace-output *emacs-connection*)
2700 (make-output-stream-for-target *emacs-connection* target))
2701 nil)
2702
2703
2704 ;;;; Undefing
2705
2706 (defslimefun undefine-function (fname-string)
2707 (let ((fname (from-string fname-string)))
2708 (format nil "~S" (fmakunbound fname))))
2709
2710
2711 ;;;; Profiling
2712
2713 (defun profiledp (fspec)
2714 (member fspec (profiled-functions)))
2715
2716 (defslimefun toggle-profile-fdefinition (fname-string)
2717 (let ((fname (from-string fname-string)))
2718 (cond ((profiledp fname)
2719 (unprofile fname)
2720 (format nil "~S is now unprofiled." fname))
2721 (t
2722 (profile fname)
2723 (format nil "~S is now profiled." fname)))))
2724
2725
2726 ;;;; Source Locations
2727
2728 (defslimefun find-definition-for-thing (thing)
2729 (find-source-location thing))
2730
2731 (defslimefun find-definitions-for-emacs (name)
2732 "Return a list ((DSPEC LOCATION) ...) of definitions for NAME.
2733 DSPEC is a string and LOCATION a source location. NAME is a string."
2734 (multiple-value-bind (sexp error) (ignore-errors (values (from-string name)))
2735 (unless error
2736 (mapcar #'xref>elisp (find-definitions sexp)))))
2737
2738 (defslimefun xref (type name)
2739 (let ((symbol (parse-symbol-or-lose name *buffer-package*)))
2740 (mapcar #'xref>elisp
2741 (ecase type
2742 (:calls (who-calls symbol))
2743 (:calls-who (calls-who symbol))
2744 (:references (who-references symbol))
2745 (:binds (who-binds symbol))
2746 (:sets (who-sets symbol))
2747 (:macroexpands (who-macroexpands symbol))
2748 (:specializes (who-specializes symbol))
2749 (:callers (list-callers symbol))
2750 (:callees (list-callees symbol))))))
2751
2752 (defun xref>elisp (xref)
2753 (destructuring-bind (name loc) xref
2754 (list (to-string name) loc)))
2755
2756
2757 ;;;; Inspecting
2758