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

Contents of /slime/swank.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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