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

Contents of /slime/swank.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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