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

Contents of /slime/swank.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.349 - (show annotations)
Sun Oct 30 15:07:07 2005 UTC (8 years, 5 months ago) by heller
Branch: MAIN
Changes since 1.348: +5 -5 lines
(simple-serve-requests): Close the connection at the end.
1 ;;; -*- outline-regexp: ";;;;;*"; indent-tabs-mode: nil -*-
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 :common-lisp :swank-backend)
17 (:export #:startup-multiprocessing
18 #:start-server
19 #:create-swank-server
20 #:create-server
21 #:ed-in-emacs
22 #:print-indentation-lossage
23 #:swank-debugger-hook
24 ;; These are user-configurable variables:
25 #:*communication-style*
26 #:*log-events*
27 #:*log-output*
28 #:*use-dedicated-output-stream*
29 #:*dedicated-output-stream-port*
30 #:*configure-emacs-indentation*
31 #:*readtable-alist*
32 #:*globally-redirect-io*
33 #:*global-debugger*
34 #:*sldb-printer-bindings*
35 #:*swank-pprint-bindings*
36 #:*default-worker-thread-bindings*
37 #:*macroexpand-printer-bindings*
38 #:*record-repl-results*
39 ;; These are re-exported directly from the backend:
40 #:buffer-first-change
41 #:frame-source-location-for-emacs
42 #:restart-frame
43 #:sldb-step
44 #:sldb-break
45 #:sldb-break-on-return
46 #:profiled-functions
47 #:profile-report
48 #:profile-reset
49 #:unprofile-all
50 #:profile-package
51 #:default-directory
52 #:set-default-directory
53 #:quit-lisp))
54
55 (in-package :swank)
56
57
58 ;;;; Top-level variables, constants, macros
59
60 (defconstant cl-package (find-package :cl)
61 "The COMMON-LISP package.")
62
63 (defconstant keyword-package (find-package :keyword)
64 "The KEYWORD package.")
65
66 (defvar *canonical-package-nicknames*
67 `((:common-lisp-user . :cl-user))
68 "Canonical package names to use instead of shortest name/nickname.")
69
70 (defvar *auto-abbreviate-dotted-packages* t
71 "Abbreviate dotted package names to their last component if T.")
72
73 (defvar *swank-io-package*
74 (let ((package (make-package :swank-io-package :use '())))
75 (import '(nil t quote) package)
76 package))
77
78 (defconstant default-server-port 4005
79 "The default TCP port for the server (when started manually).")
80
81 (defvar *swank-debug-p* t
82 "When true, print extra debugging information.")
83
84 (defvar *redirect-io* t
85 "When non-nil redirect Lisp standard I/O to Emacs.
86 Redirection is done while Lisp is processing a request for Emacs.")
87
88 (defvar *sldb-printer-bindings*
89 `((*print-pretty* . nil)
90 (*print-level* . 4)
91 (*print-length* . 10)
92 (*print-circle* . t)
93 (*print-readably* . nil)
94 (*print-pprint-dispatch* . ,(copy-pprint-dispatch nil))
95 (*print-gensym* . t)
96 (*print-base* . 10)
97 (*print-radix* . nil)
98 (*print-array* . t)
99 (*print-lines* . 200)
100 (*print-escape* . t))
101 "A set of printer variables used in the debugger.")
102
103 (defvar *default-worker-thread-bindings* '()
104 "An alist to initialize dynamic variables in worker threads.
105 The list has the form ((VAR . VALUE) ...). Each variable VAR will be
106 bound to the corresponding VALUE.")
107
108 (defun call-with-bindings (alist fun)
109 "Call FUN with variables bound according to ALIST.
110 ALIST is a list of the form ((VAR . VAL) ...)."
111 (let* ((rlist (reverse alist))
112 (vars (mapcar #'car rlist))
113 (vals (mapcar #'cdr rlist)))
114 (progv vars vals
115 (funcall fun))))
116
117 (defmacro with-bindings (alist &body body)
118 "See `call-with-bindings'."
119 `(call-with-bindings ,alist (lambda () ,@body)))
120
121 ;;; The `DEFSLIMEFUN' macro defines a function that Emacs can call via
122 ;;; RPC.
123
124 (defmacro defslimefun (name arglist &body rest)
125 "A DEFUN for functions that Emacs can call by RPC."
126 `(progn
127 (defun ,name ,arglist ,@rest)
128 ;; see <http://www.franz.com/support/documentation/6.2/doc/pages/variables/compiler/s_cltl1-compile-file-toplevel-compatibility-p_s.htm>
129 (eval-when (:compile-toplevel :load-toplevel :execute)
130 (export ',name :swank))))
131
132 (declaim (ftype (function () nil) missing-arg))
133 (defun missing-arg ()
134 "A function that the compiler knows will never to return a value.
135 You can use (MISSING-ARG) as the initform for defstruct slots that
136 must always be supplied. This way the :TYPE slot option need not
137 include some arbitrary initial value like NIL."
138 (error "A required &KEY or &OPTIONAL argument was not supplied."))
139
140
141 ;;;; Hooks
142 ;;;
143 ;;; We use Emacs-like `add-hook' and `run-hook' utilities to support
144 ;;; simple indirection. The interface is more CLish than the Emacs
145 ;;; Lisp one.
146
147 (defmacro add-hook (place function)
148 "Add FUNCTION to the list of values on PLACE."
149 `(pushnew ,function ,place))
150
151 (defun run-hook (functions &rest arguments)
152 "Call each of FUNCTIONS with ARGUMENTS."
153 (dolist (function functions)
154 (apply function arguments)))
155
156 (defvar *new-connection-hook* '()
157 "This hook is run each time a connection is established.
158 The connection structure is given as the argument.
159 Backend code should treat the connection structure as opaque.")
160
161 (defvar *connection-closed-hook* '()
162 "This hook is run when a connection is closed.
163 The connection as passed as an argument.
164 Backend code should treat the connection structure as opaque.")
165
166 (defvar *pre-reply-hook* '()
167 "Hook run (without arguments) immediately before replying to an RPC.")
168
169
170 ;;;; Connections
171 ;;;
172 ;;; Connection structures represent the network connections between
173 ;;; Emacs and Lisp. Each has a socket stream, a set of user I/O
174 ;;; streams that redirect to Emacs, and optionally a second socket
175 ;;; used solely to pipe user-output to Emacs (an optimization).
176 ;;;
177
178 (defvar *coding-system* ':iso-latin-1-unix)
179
180 (defstruct (connection
181 (:conc-name connection.)
182 (:print-function print-connection))
183 ;; Raw I/O stream of socket connection.
184 (socket-io (missing-arg) :type stream :read-only t)
185 ;; Optional dedicated output socket (backending `user-output' slot).
186 ;; Has a slot so that it can be closed with the connection.
187 (dedicated-output nil :type (or stream null))
188 ;; Streams that can be used for user interaction, with requests
189 ;; redirected to Emacs.
190 (user-input nil :type (or stream null))
191 (user-output nil :type (or stream null))
192 (user-io nil :type (or stream null))
193 ;; In multithreaded systems we delegate certain tasks to specific
194 ;; threads. The `reader-thread' is responsible for reading network
195 ;; requests from Emacs and sending them to the `control-thread'; the
196 ;; `control-thread' is responsible for dispatching requests to the
197 ;; threads that should handle them; the `repl-thread' is the one
198 ;; that evaluates REPL expressions. The control thread dispatches
199 ;; all REPL evaluations to the REPL thread and for other requests it
200 ;; spawns new threads.
201 reader-thread
202 control-thread
203 repl-thread
204 ;; Callback functions:
205 ;; (SERVE-REQUESTS <this-connection>) serves all pending requests
206 ;; from Emacs.
207 (serve-requests (missing-arg) :type function)
208 ;; (READ) is called to read and return one message from Emacs.
209 (read (missing-arg) :type function)
210 ;; (SEND OBJECT) is called to send one message to Emacs.
211 (send (missing-arg) :type function)
212 ;; (CLEANUP <this-connection>) is called when the connection is
213 ;; closed.
214 (cleanup nil :type (or null function))
215 ;; Cache of macro-indentation information that has been sent to Emacs.
216 ;; This is used for preparing deltas to update Emacs's knowledge.
217 ;; Maps: symbol -> indentation-specification
218 (indentation-cache (make-hash-table :test 'eq) :type hash-table)
219 ;; The list of packages represented in the cache:
220 (indentation-cache-packages '())
221 ;; The communication style used.
222 (communication-style nil :type (member nil :spawn :sigio :fd-handler))
223 ;; The coding system for network streams.
224 (external-format *coding-system* :type (member :iso-latin-1-unix
225 :emacs-mule-unix
226 :utf-8-unix
227 :euc-jp-unix)))
228
229 (defun print-connection (conn stream depth)
230 (declare (ignore depth))
231 (print-unreadable-object (conn stream :type t :identity t)))
232
233 (defvar *connections* '()
234 "List of all active connections, with the most recent at the front.")
235
236 (defvar *emacs-connection* nil
237 "The connection to Emacs currently in use.")
238
239 (defvar *swank-state-stack* '()
240 "A list of symbols describing the current state. Used for debugging
241 and to detect situations where interrupts can be ignored.")
242
243 (defun default-connection ()
244 "Return the 'default' Emacs connection.
245 This connection can be used to talk with Emacs when no specific
246 connection is in use, i.e. *EMACS-CONNECTION* is NIL.
247
248 The default connection is defined (quite arbitrarily) as the most
249 recently established one."
250 (first *connections*))
251
252 (defslimefun state-stack ()
253 "Return the value of *SWANK-STATE-STACK*."
254 *swank-state-stack*)
255
256 (define-condition slime-protocol-error (error)
257 ((condition :initarg :condition :reader slime-protocol-error.condition))
258 (:report (lambda (condition stream)
259 (format stream "~A" (slime-protocol-error.condition condition)))))
260
261 (add-hook *new-connection-hook* 'notify-backend-of-connection)
262 (defun notify-backend-of-connection (connection)
263 (declare (ignore connection))
264 (emacs-connected))
265
266
267 ;;;; Helper macros
268
269 (defmacro with-io-redirection ((connection) &body body)
270 "Execute BODY I/O redirection to CONNECTION.
271 If *REDIRECT-IO* is true then all standard I/O streams are redirected."
272 `(maybe-call-with-io-redirection ,connection (lambda () ,@body)))
273
274 (defun maybe-call-with-io-redirection (connection fun)
275 (if *redirect-io*
276 (call-with-redirected-io connection fun)
277 (funcall fun)))
278
279 (defmacro with-connection ((connection) &body body)
280 "Execute BODY in the context of CONNECTION."
281 `(call-with-connection ,connection (lambda () ,@body)))
282
283 (defun call-with-connection (connection fun)
284 (let ((*emacs-connection* connection))
285 (with-io-redirection (*emacs-connection*)
286 (let ((*debugger-hook* #'swank-debugger-hook))
287 (funcall fun)))))
288
289 (defmacro without-interrupts (&body body)
290 `(call-without-interrupts (lambda () ,@body)))
291
292 (defmacro destructure-case (value &rest patterns)
293 "Dispatch VALUE to one of PATTERNS.
294 A cross between `case' and `destructuring-bind'.
295 The pattern syntax is:
296 ((HEAD . ARGS) . BODY)
297 The list of patterns is searched for a HEAD `eq' to the car of
298 VALUE. If one is found, the BODY is executed with ARGS bound to the
299 corresponding values in the CDR of VALUE."
300 (let ((operator (gensym "op-"))
301 (operands (gensym "rand-"))
302 (tmp (gensym "tmp-")))
303 `(let* ((,tmp ,value)
304 (,operator (car ,tmp))
305 (,operands (cdr ,tmp)))
306 (case ,operator
307 ,@(loop for (pattern . body) in patterns collect
308 (if (eq pattern t)
309 `(t ,@body)
310 (destructuring-bind (op &rest rands) pattern
311 `(,op (destructuring-bind ,rands ,operands
312 ,@body)))))
313 ,@(if (eq (caar (last patterns)) t)
314 '()
315 `((t (error "destructure-case failed: ~S" ,tmp))))))))
316
317 (defmacro with-temp-package (var &body body)
318 "Execute BODY with VAR bound to a temporary package.
319 The package is deleted before returning."
320 `(let ((,var (make-package (gensym "TEMP-PACKAGE-"))))
321 (unwind-protect (progn ,@body)
322 (delete-package ,var))))
323
324 (defvar *log-events* nil)
325 (defvar *log-output* *error-output*)
326
327 (defun log-event (format-string &rest args)
328 "Write a message to *terminal-io* when *log-events* is non-nil.
329 Useful for low level debugging."
330 (when *log-events*
331 (apply #'format *log-output* format-string args)
332 (force-output *log-output*)))
333
334
335 ;;;; TCP Server
336
337 (defvar *use-dedicated-output-stream* t
338 "When T swank will attempt to create a second connection to
339 Emacs which is used just to send output.")
340 (defvar *dedicated-output-stream-port* 0
341 "Which port we should use for the dedicated output stream.")
342
343 (defvar *communication-style* (preferred-communication-style))
344
345 (defun start-server (port-file &key (style *communication-style*)
346 dont-close (external-format *coding-system*))
347 "Start the server and write the listen port number to PORT-FILE.
348 This is the entry point for Emacs."
349 (when (eq style :spawn)
350 (initialize-multiprocessing))
351 (setup-server 0 (lambda (port) (announce-server-port port-file port))
352 style dont-close external-format)
353 (when (eq style :spawn)
354 (startup-idle-and-top-level-loops)))
355
356 (defun create-server (&key (port default-server-port)
357 (style *communication-style*)
358 dont-close (external-format *coding-system*))
359 "Start a SWANK server on PORT running in STYLE.
360 If DONT-CLOSE is true then the listen socket will accept multiple
361 connections, otherwise it will be closed after the first."
362 (setup-server port #'simple-announce-function style dont-close
363 external-format))
364
365 (defun create-swank-server (&optional (port default-server-port)
366 (style *communication-style*)
367 (announce-fn #'simple-announce-function)
368 dont-close (external-format *coding-system*))
369 (setup-server port announce-fn style dont-close external-format))
370
371 (defparameter *loopback-interface* "127.0.0.1")
372
373 (defun setup-server (port announce-fn style dont-close external-format)
374 (declare (type function announce-fn))
375 (let* ((socket (create-socket *loopback-interface* port))
376 (port (local-port socket)))
377 (funcall announce-fn port)
378 (flet ((serve ()
379 (serve-connection socket style dont-close external-format)))
380 (ecase style
381 (:spawn
382 (spawn (lambda () (loop do (serve) while dont-close))
383 :name "Swank"))
384 ((:fd-handler :sigio)
385 (add-fd-handler socket (lambda () (serve))))
386 ((nil) (loop do (serve) while dont-close)))
387 port)))
388
389 (defun serve-connection (socket style dont-close external-format)
390 (let ((client (accept-authenticated-connection
391 socket :external-format external-format)))
392 (unless dont-close
393 (close-socket socket))
394 (let ((connection (create-connection client style external-format)))
395 (run-hook *new-connection-hook* connection)
396 (push connection *connections*)
397 (serve-requests connection))))
398
399 (defun accept-authenticated-connection (&rest args)
400 (let ((new (apply #'accept-connection args))
401 (secret (slime-secret)))
402 (when secret
403 (let ((first-val (decode-message new)))
404 (unless (and (stringp first-val) (string= first-val secret))
405 (close new)
406 (error "Incoming connection doesn't know the password."))))
407 new))
408
409 (defun slime-secret ()
410 "Finds the magic secret from the user's home directory. Returns nil
411 if the file doesn't exist; otherwise the first line of the file."
412 (with-open-file (in
413 (merge-pathnames (user-homedir-pathname) #p".slime-secret")
414 :if-does-not-exist nil)
415 (and in (read-line in nil ""))))
416
417 (defun serve-requests (connection)
418 "Read and process all requests on connections."
419 (funcall (connection.serve-requests connection) connection))
420
421 (defun announce-server-port (file port)
422 (with-open-file (s file
423 :direction :output
424 :if-exists :error
425 :if-does-not-exist :create)
426 (format s "~S~%" port))
427 (simple-announce-function port))
428
429 (defun simple-announce-function (port)
430 (when *swank-debug-p*
431 (format *debug-io* "~&;; Swank started at port: ~D.~%" port)
432 (force-output *debug-io*)))
433
434 (defun open-streams (connection)
435 "Return the 4 streams for IO redirection:
436 DEDICATED-OUTPUT INPUT OUTPUT IO"
437 (multiple-value-bind (output-fn dedicated-output)
438 (make-output-function connection)
439 (let ((input-fn
440 (lambda ()
441 (with-connection (connection)
442 (with-simple-restart (abort-read
443 "Abort reading input from Emacs.")
444 (read-user-input-from-emacs))))))
445 (multiple-value-bind (in out) (make-fn-streams input-fn output-fn)
446 (let ((out (or dedicated-output out)))
447 (let ((io (make-two-way-stream in out)))
448 (mapc #'make-stream-interactive (list in out io))
449 (values dedicated-output in out io)))))))
450
451 (defun make-output-function (connection)
452 "Create function to send user output to Emacs.
453 This function may open a dedicated socket to send output. It
454 returns two values: the output function, and the dedicated
455 stream (or NIL if none was created)."
456 (if *use-dedicated-output-stream*
457 (let ((stream (open-dedicated-output-stream
458 (connection.socket-io connection)
459 (connection.external-format connection))))
460 (values (lambda (string)
461 (write-string string stream)
462 (force-output stream))
463 stream))
464 (values (lambda (string)
465 (with-connection (connection)
466 (with-simple-restart
467 (abort "Abort sending output to Emacs.")
468 (send-to-emacs `(:write-string ,string)))))
469 nil)))
470
471 (defun open-dedicated-output-stream (socket-io external-format)
472 "Open a dedicated output connection to the Emacs on SOCKET-IO.
473 Return an output stream suitable for writing program output.
474
475 This is an optimized way for Lisp to deliver output to Emacs."
476 (let* ((socket (create-socket *loopback-interface*
477 *dedicated-output-stream-port*))
478 (port (local-port socket)))
479 (encode-message `(:open-dedicated-output-stream ,port) socket-io)
480 (accept-authenticated-connection
481 socket :external-format external-format)))
482
483 (defun handle-request (connection)
484 "Read and process one request. The processing is done in the extend
485 of the toplevel restart."
486 (assert (null *swank-state-stack*))
487 (let ((*swank-state-stack* '(:handle-request))
488 (*debugger-hook* nil))
489 (with-connection (connection)
490 (with-simple-restart (abort-request "Abort handling SLIME request.")
491 (read-from-emacs)))))
492
493 (defun current-socket-io ()
494 (connection.socket-io *emacs-connection*))
495
496 (defun close-connection (c &optional condition)
497 (let ((cleanup (connection.cleanup c)))
498 (when cleanup
499 (funcall cleanup c)))
500 (close (connection.socket-io c))
501 (when (connection.dedicated-output c)
502 (close (connection.dedicated-output c)))
503 (setf *connections* (remove c *connections*))
504 (run-hook *connection-closed-hook* c)
505 (when condition
506 (format *debug-io* "~&;; Connection to Emacs lost.~%;; [~A]~%" condition)
507 (finish-output *debug-io*)))
508
509 (defmacro with-reader-error-handler ((connection) &body body)
510 `(handler-case (progn ,@body)
511 (slime-protocol-error (e)
512 (close-connection ,connection e))))
513
514 (defslimefun simple-break ()
515 (with-simple-restart (continue "Continue from interrupt.")
516 (let ((*debugger-hook* #'swank-debugger-hook))
517 (invoke-debugger
518 (make-condition 'simple-error
519 :format-control "Interrupt from Emacs"))))
520 nil)
521
522 ;;;;;; Thread based communication
523
524 (defvar *active-threads* '())
525
526 (defun read-loop (control-thread input-stream connection)
527 (with-reader-error-handler (connection)
528 (loop (send control-thread (decode-message input-stream)))))
529
530 (defun dispatch-loop (socket-io connection)
531 (let ((*emacs-connection* connection))
532 (handler-case
533 (loop (dispatch-event (receive) socket-io))
534 (error (e)
535 (close-connection connection e)))))
536
537 (defun repl-thread (connection)
538 (let ((thread (connection.repl-thread connection)))
539 (if (thread-alive-p thread)
540 thread
541 (setf (connection.repl-thread connection)
542 (spawn-repl-thread connection "new-repl-thread")))))
543
544
545 (defun find-worker-thread (id)
546 (etypecase id
547 ((member t)
548 (car *active-threads*))
549 ((member :repl-thread)
550 (repl-thread *emacs-connection*))
551 (fixnum
552 (find-thread id))))
553
554 (defun interrupt-worker-thread (id)
555 (let ((thread (or (find-worker-thread id)
556 (repl-thread *emacs-connection*))))
557 (interrupt-thread thread #'simple-break)))
558
559 (defun thread-for-evaluation (id)
560 "Find or create a thread to evaluate the next request."
561 (let ((c *emacs-connection*))
562 (etypecase id
563 ((member t)
564 (spawn-worker-thread c))
565 ((member :repl-thread)
566 (repl-thread c))
567 (fixnum
568 (find-thread id)))))
569
570 (defun spawn-worker-thread (connection)
571 (spawn (lambda ()
572 (with-bindings *default-worker-thread-bindings*
573 (handle-request connection)))
574 :name "worker"))
575
576 (defun spawn-repl-thread (connection name)
577 (spawn (lambda ()
578 (with-bindings *default-worker-thread-bindings*
579 (repl-loop connection)))
580 :name name))
581
582 (defun dispatch-event (event socket-io)
583 "Handle an event triggered either by Emacs or within Lisp."
584 (log-event "DISPATCHING: ~S~%" event)
585 (destructure-case event
586 ((:emacs-rex form package thread-id id)
587 (let ((thread (thread-for-evaluation thread-id)))
588 (push thread *active-threads*)
589 (send thread `(eval-for-emacs ,form ,package ,id))))
590 ((:return thread &rest args)
591 (let ((tail (member thread *active-threads*)))
592 (setq *active-threads* (nconc (ldiff *active-threads* tail)
593 (cdr tail))))
594 (encode-message `(:return ,@args) socket-io))
595 ((:emacs-interrupt thread-id)
596 (interrupt-worker-thread thread-id))
597 (((:debug :debug-condition :debug-activate :debug-return)
598 thread &rest args)
599 (encode-message `(,(car event) ,(thread-id thread) ,@args) socket-io))
600 ((:read-string thread &rest args)
601 (encode-message `(:read-string ,(thread-id thread) ,@args) socket-io))
602 ((:y-or-n-p thread &rest args)
603 (encode-message `(:y-or-n-p ,(thread-id thread) ,@args) socket-io))
604 ((:read-aborted thread &rest args)
605 (encode-message `(:read-aborted ,(thread-id thread) ,@args) socket-io))
606 ((:emacs-return-string thread-id tag string)
607 (send (find-thread thread-id) `(take-input ,tag ,string)))
608 ((:eval thread &rest args)
609 (encode-message `(:eval ,(thread-id thread) ,@args) socket-io))
610 ((:emacs-return thread-id tag value)
611 (send (find-thread thread-id) `(take-input ,tag ,value)))
612 (((:write-string :presentation-start :presentation-end
613 :new-package :new-features :ed :%apply :indentation-update
614 :eval-no-wait :background-message)
615 &rest _)
616 (declare (ignore _))
617 (encode-message event socket-io))))
618
619 (defun spawn-threads-for-connection (connection)
620 (let* ((socket-io (connection.socket-io connection))
621 (control-thread (spawn (lambda ()
622 (let ((*debugger-hook* nil))
623 (dispatch-loop socket-io connection)))
624 :name "control-thread")))
625 (setf (connection.control-thread connection) control-thread)
626 (let ((reader-thread (spawn (lambda ()
627 (let ((*debugger-hook* nil))
628 (read-loop control-thread socket-io
629 connection)))
630 :name "reader-thread"))
631 (repl-thread (spawn-repl-thread connection "repl-thread")))
632 (setf (connection.reader-thread connection) reader-thread)
633 (setf (connection.repl-thread connection) repl-thread)
634 connection)))
635
636 (defun cleanup-connection-threads (connection)
637 (let ((threads (list (connection.repl-thread connection)
638 (connection.reader-thread connection)
639 (connection.control-thread connection))))
640 (dolist (thread threads)
641 (unless (equal (current-thread) thread)
642 (kill-thread thread)))))
643
644 (defun repl-loop (connection)
645 (with-connection (connection)
646 (loop (handle-request connection))))
647
648 (defun process-available-input (stream fn)
649 (loop while (and (open-stream-p stream)
650 (listen stream))
651 do (funcall fn)))
652
653 ;;;;;; Signal driven IO
654
655 (defun install-sigio-handler (connection)
656 (let ((client (connection.socket-io connection)))
657 (flet ((handler ()
658 (cond ((null *swank-state-stack*)
659 (with-reader-error-handler (connection)
660 (process-available-input
661 client (lambda () (handle-request connection)))))
662 ((eq (car *swank-state-stack*) :read-next-form))
663 (t (process-available-input client #'read-from-emacs)))))
664 (add-sigio-handler client #'handler)
665 (handler))))
666
667 (defun deinstall-sigio-handler (connection)
668 (remove-sigio-handlers (connection.socket-io connection)))
669
670 ;;;;;; SERVE-EVENT based IO
671
672 (defun install-fd-handler (connection)
673 (let ((client (connection.socket-io connection)))
674 (flet ((handler ()
675 (cond ((null *swank-state-stack*)
676 (with-reader-error-handler (connection)
677 (process-available-input
678 client (lambda () (handle-request connection)))))
679 ((eq (car *swank-state-stack*) :read-next-form))
680 (t (process-available-input client #'read-from-emacs)))))
681 (setq *debugger-hook*
682 (lambda (c h)
683 (with-reader-error-handler (connection)
684 (block debugger
685 (with-connection (connection)
686 (swank-debugger-hook c h)
687 (return-from debugger))
688 (abort)))))
689 (add-fd-handler client #'handler)
690 (handler))))
691
692 (defun deinstall-fd-handler (connection)
693 (remove-fd-handlers (connection.socket-io connection)))
694
695 ;;;;;; Simple sequential IO
696
697 (defun simple-serve-requests (connection)
698 (with-reader-error-handler (connection)
699 (unwind-protect (loop (handle-request connection))
700 (close-connection connection))))
701
702 (defun read-from-socket-io ()
703 (let ((event (decode-message (current-socket-io))))
704 (log-event "DISPATCHING: ~S~%" event)
705 (destructure-case event
706 ((:emacs-rex form package thread id)
707 (declare (ignore thread))
708 `(eval-for-emacs ,form ,package ,id))
709 ((:emacs-interrupt thread)
710 (declare (ignore thread))
711 '(simple-break))
712 ((:emacs-return-string thread tag string)
713 (declare (ignore thread))
714 `(take-input ,tag ,string))
715 ((:emacs-return thread tag value)
716 (declare (ignore thread))
717 `(take-input ,tag ,value)))))
718
719 (defun send-to-socket-io (event)
720 (log-event "DISPATCHING: ~S~%" event)
721 (flet ((send (o)
722 (without-interrupts
723 (encode-message o (current-socket-io)))))
724 (destructure-case event
725 (((:debug-activate :debug :debug-return :read-string :read-aborted
726 :y-or-n-p :eval)
727 thread &rest args)
728 (declare (ignore thread))
729 (send `(,(car event) 0 ,@args)))
730 ((:return thread &rest args)
731 (declare (ignore thread))
732 (send `(:return ,@args)))
733 (((:write-string :new-package :new-features :debug-condition
734 :presentation-start :presentation-end
735 :indentation-update :ed :%apply :eval-no-wait
736 :background-message)
737 &rest _)
738 (declare (ignore _))
739 (send event)))))
740
741 (defun initialize-streams-for-connection (connection)
742 (multiple-value-bind (dedicated in out io) (open-streams connection)
743 (setf (connection.dedicated-output connection) dedicated
744 (connection.user-io connection) io
745 (connection.user-output connection) out
746 (connection.user-input connection) in)
747 connection))
748
749 (defun create-connection (socket-io style external-format)
750 (let ((c (ecase style
751 (:spawn
752 (make-connection :socket-io socket-io
753 :read #'read-from-control-thread
754 :send #'send-to-control-thread
755 :serve-requests #'spawn-threads-for-connection
756 :cleanup #'cleanup-connection-threads))
757 (:sigio
758 (make-connection :socket-io socket-io
759 :read #'read-from-socket-io
760 :send #'send-to-socket-io
761 :serve-requests #'install-sigio-handler
762 :cleanup #'deinstall-sigio-handler))
763 (:fd-handler
764 (make-connection :socket-io socket-io
765 :read #'read-from-socket-io
766 :send #'send-to-socket-io
767 :serve-requests #'install-fd-handler
768 :cleanup #'deinstall-fd-handler))
769 ((nil)
770 (make-connection :socket-io socket-io
771 :read #'read-from-socket-io
772 :send #'send-to-socket-io
773 :serve-requests #'simple-serve-requests)))))
774 (setf (connection.communication-style c) style)
775 (setf (connection.external-format c) external-format)
776 (initialize-streams-for-connection c)
777 c))
778
779
780 ;;;; IO to Emacs
781 ;;;
782 ;;; This code handles redirection of the standard I/O streams
783 ;;; (`*standard-output*', etc) into Emacs. The `connection' structure
784 ;;; contains the appropriate streams, so all we have to do is make the
785 ;;; right bindings.
786
787 ;;;;; Global I/O redirection framework
788 ;;;
789 ;;; Optionally, the top-level global bindings of the standard streams
790 ;;; can be assigned to be redirected to Emacs. When Emacs connects we
791 ;;; redirect the streams into the connection, and they keep going into
792 ;;; that connection even if more are established. If the connection
793 ;;; handling the streams closes then another is chosen, or if there
794 ;;; are no connections then we revert to the original (real) streams.
795 ;;;
796 ;;; It is slightly tricky to assign the global values of standard
797 ;;; streams because they are often shadowed by dynamic bindings. We
798 ;;; solve this problem by introducing an extra indirection via synonym
799 ;;; streams, so that *STANDARD-INPUT* is a synonym stream to
800 ;;; *CURRENT-STANDARD-INPUT*, etc. We never shadow the "current"
801 ;;; variables, so they can always be assigned to affect a global
802 ;;; change.
803
804 (defvar *globally-redirect-io* nil
805 "When non-nil globally redirect all standard streams to Emacs.")
806
807 (defmacro setup-stream-indirection (stream-var)
808 "Setup redirection scaffolding for a global stream variable.
809 Supposing (for example) STREAM-VAR is *STANDARD-INPUT*, this macro:
810
811 1. Saves the value of *STANDARD-INPUT* in a variable called
812 *REAL-STANDARD-INPUT*.
813
814 2. Creates *CURRENT-STANDARD-INPUT*, initially with the same value as
815 *STANDARD-INPUT*.
816
817 3. Assigns *STANDARD-INPUT* to a synonym stream pointing to
818 *CURRENT-STANDARD-INPUT*.
819
820 This has the effect of making *CURRENT-STANDARD-INPUT* contain the
821 effective global value for *STANDARD-INPUT*. This way we can assign
822 the effective global value even when *STANDARD-INPUT* is shadowed by a
823 dynamic binding."
824 (let ((real-stream-var (prefixed-var "REAL" stream-var))
825 (current-stream-var (prefixed-var "CURRENT" stream-var)))
826 `(progn
827 ;; Save the real stream value for the future.
828 (defvar ,real-stream-var ,stream-var)
829 ;; Define a new variable for the effective stream.
830 ;; This can be reassigned.
831 (defvar ,current-stream-var ,stream-var)
832 ;; Assign the real binding as a synonym for the current one.
833 (setq ,stream-var (make-synonym-stream ',current-stream-var)))))
834
835 (eval-when (:compile-toplevel :load-toplevel :execute)
836 (defun prefixed-var (prefix variable-symbol)
837 "(PREFIXED-VAR \"FOO\" '*BAR*) => SWANK::*FOO-BAR*"
838 (let ((basename (subseq (symbol-name variable-symbol) 1)))
839 (intern (format nil "*~A-~A" prefix basename) :swank))))
840
841 ;;;;; Global redirection setup
842
843 (setup-stream-indirection *standard-output*)
844 (setup-stream-indirection *error-output*)
845 (setup-stream-indirection *trace-output*)
846 (setup-stream-indirection *standard-input*)
847 (setup-stream-indirection *debug-io*)
848 (setup-stream-indirection *query-io*)
849 (setup-stream-indirection *terminal-io*)
850
851 (defparameter *standard-output-streams*
852 '(*standard-output* *error-output* *trace-output*)
853 "The symbols naming standard output streams.")
854
855 (defparameter *standard-input-streams*
856 '(*standard-input*)
857 "The symbols naming standard input streams.")
858
859 (defparameter *standard-io-streams*
860 '(*debug-io* *query-io* *terminal-io*)
861 "The symbols naming standard io streams.")
862
863 (defun globally-redirect-io-to-connection (connection)
864 "Set the standard I/O streams to redirect to CONNECTION.
865 Assigns *CURRENT-<STREAM>* for all standard streams."
866 (dolist (o *standard-output-streams*)
867 (set (prefixed-var "CURRENT" o)
868 (connection.user-output connection)))
869 ;; FIXME: If we redirect standard input to Emacs then we get the
870 ;; regular Lisp top-level trying to read from our REPL.
871 ;;
872 ;; Perhaps the ideal would be for the real top-level to run in a
873 ;; thread with local bindings for all the standard streams. Failing
874 ;; that we probably would like to inhibit it from reading while
875 ;; Emacs is connected.
876 ;;
877 ;; Meanwhile we just leave *standard-input* alone.
878 #+NIL
879 (dolist (i *standard-input-streams*)
880 (set (prefixed-var "CURRENT" i)
881 (connection.user-input connection)))
882 (dolist (io *standard-io-streams*)
883 (set (prefixed-var "CURRENT" io)
884 (connection.user-io connection))))
885
886 (defun revert-global-io-redirection ()
887 "Set *CURRENT-<STREAM>* to *REAL-<STREAM>* for all standard streams."
888 (dolist (stream-var (append *standard-output-streams*
889 *standard-input-streams*
890 *standard-io-streams*))
891 (set (prefixed-var "CURRENT" stream-var)
892 (symbol-value (prefixed-var "REAL" stream-var)))))
893
894 ;;;;; Global redirection hooks
895
896 (defvar *global-stdio-connection* nil
897 "The connection to which standard I/O streams are globally redirected.
898 NIL if streams are not globally redirected.")
899
900 (defun maybe-redirect-global-io (connection)
901 "Consider globally redirecting to a newly-established CONNECTION."
902 (when (and *globally-redirect-io* (null *global-stdio-connection*))
903 (setq *global-stdio-connection* connection)
904 (globally-redirect-io-to-connection connection)))
905
906 (defun update-redirection-after-close (closed-connection)
907 "Update redirection after a connection closes."
908 (when (eq *global-stdio-connection* closed-connection)
909 (if (and (default-connection) *globally-redirect-io*)
910 ;; Redirect to another connection.
911 (globally-redirect-io-to-connection (default-connection))
912 ;; No more connections, revert to the real streams.
913 (progn (revert-global-io-redirection)
914 (setq *global-stdio-connection* nil)))))
915
916 (add-hook *new-connection-hook* 'maybe-redirect-global-io)
917 (add-hook *connection-closed-hook* 'update-redirection-after-close)
918
919 ;;;;; Redirection during requests
920 ;;;
921 ;;; We always redirect the standard streams to Emacs while evaluating
922 ;;; an RPC. This is done with simple dynamic bindings.
923
924 (defun call-with-redirected-io (connection function)
925 "Call FUNCTION with I/O streams redirected via CONNECTION."
926 (declare (type function function))
927 (let* ((io (connection.user-io connection))
928 (in (connection.user-input connection))
929 (out (connection.user-output connection))
930 (*standard-output* out)
931 (*error-output* out)
932 (*trace-output* out)
933 (*debug-io* io)
934 (*query-io* io)
935 (*standard-input* in)
936 (*terminal-io* io))
937 (funcall function)))
938
939 (defun read-from-emacs ()
940 "Read and process a request from Emacs."
941 (apply #'funcall (funcall (connection.read *emacs-connection*))))
942
943 (defun read-from-control-thread ()
944 (receive))
945
946 (defun decode-message (stream)
947 "Read an S-expression from STREAM using the SLIME protocol.
948 If a protocol error occurs then a SLIME-PROTOCOL-ERROR is signalled."
949 (let ((*swank-state-stack* (cons :read-next-form *swank-state-stack*)))
950 (handler-case
951 (let* ((length (decode-message-length stream))
952 (string (make-string length))
953 (pos (read-sequence string stream)))
954 (assert (= pos length) ()
955 "Short read: length=~D pos=~D" length pos)
956 (let ((form (read-form string)))
957 (log-event "READ: ~A~%" string)
958 form))
959 (serious-condition (c)
960 (error (make-condition 'slime-protocol-error :condition c))))))
961
962 (defun decode-message-length (stream)
963 (let ((buffer (make-string 6)))
964 (dotimes (i 6)
965 (setf (aref buffer i) (read-char stream)))
966 (parse-integer buffer :radix #x10)))
967
968 (defun read-form (string)
969 (with-standard-io-syntax
970 (let ((*package* *swank-io-package*))
971 (read-from-string string))))
972
973 (defvar *slime-features* nil
974 "The feature list that has been sent to Emacs.")
975
976 (defun send-to-emacs (object)
977 "Send OBJECT to Emacs."
978 (funcall (connection.send *emacs-connection*) object))
979
980 (defun send-oob-to-emacs (object)
981 (send-to-emacs object))
982
983 (defun send-to-control-thread (object)
984 (send (connection.control-thread *emacs-connection*) object))
985
986 (defun encode-message (message stream)
987 (let* ((string (prin1-to-string-for-emacs message))
988 (length (length string)))
989 (log-event "WRITE: ~A~%" string)
990 (let ((*print-pretty* nil))
991 (format stream "~6,'0x" length))
992 (write-string string stream)
993 ;;(terpri stream)
994 (force-output stream)))
995
996 (defun prin1-to-string-for-emacs (object)
997 (with-standard-io-syntax
998 (let ((*print-case* :downcase)
999 (*print-readably* nil)
1000 (*print-pretty* nil)
1001 (*package* *swank-io-package*))
1002 (prin1-to-string object))))
1003
1004 (defun force-user-output ()
1005 (force-output (connection.user-io *emacs-connection*))
1006 (finish-output (connection.user-output *emacs-connection*)))
1007
1008 (defun clear-user-input ()
1009 (clear-input (connection.user-input *emacs-connection*)))
1010
1011 (defvar *read-input-catch-tag* 0)
1012
1013 (defun intern-catch-tag (tag)
1014 ;; fixnums aren't eq in ABCL, so we use intern to create tags
1015 (intern (format nil "~D" tag) :swank))
1016
1017 (defun read-user-input-from-emacs ()
1018 (let ((tag (incf *read-input-catch-tag*)))
1019 (force-output)
1020 (send-to-emacs `(:read-string ,(current-thread) ,tag))
1021 (let ((ok nil))
1022 (unwind-protect
1023 (prog1 (catch (intern-catch-tag tag)
1024 (loop (read-from-emacs)))
1025 (setq ok t))
1026 (unless ok
1027 (send-to-emacs `(:read-aborted ,(current-thread) ,tag)))))))
1028
1029 (defun y-or-n-p-in-emacs (format-string &rest arguments)
1030 "Like y-or-n-p, but ask in the Emacs minibuffer."
1031 (let ((tag (incf *read-input-catch-tag*))
1032 (question (apply #'format nil format-string arguments)))
1033 (force-output)
1034 (send-to-emacs `(:y-or-n-p ,(current-thread) ,tag ,question))
1035 (catch (intern-catch-tag tag)
1036 (loop (read-from-emacs)))))
1037
1038 (defslimefun take-input (tag input)
1039 "Return the string INPUT to the continuation TAG."
1040 (throw (intern-catch-tag tag) input))
1041
1042 (defun process-form-for-emacs (form)
1043 "Returns a string which emacs will read as equivalent to
1044 FORM. FORM can contain lists, strings, characters, symbols and
1045 numbers.
1046
1047 Characters are converted emacs' ?<char> notaion, strings are left
1048 as they are (except for espacing any nested \" chars, numbers are
1049 printed in base 10 and symbols are printed as their symbol-nome
1050 converted to lower case."
1051 (etypecase form
1052 (string (format nil "~S" form))
1053 (cons (format nil "(~A . ~A)"
1054 (process-form-for-emacs (car form))
1055 (process-form-for-emacs (cdr form))))
1056 (character (format nil "?~C" form))
1057 (symbol (string-downcase (symbol-name form)))
1058 (number (let ((*print-base* 10))
1059 (princ-to-string form)))))
1060
1061 (defun eval-in-emacs (form &optional nowait)
1062 "Eval FORM in Emacs."
1063 (cond (nowait
1064 (send-to-emacs `(:eval-no-wait ,(process-form-for-emacs form))))
1065 (t
1066 (force-output)
1067 (let* ((tag (incf *read-input-catch-tag*))
1068 (value (catch (intern-catch-tag tag)
1069 (send-to-emacs
1070 `(:eval ,(current-thread) ,tag
1071 ,(process-form-for-emacs form)))
1072 (loop (read-from-emacs)))))
1073 (destructure-case value
1074 ((:ok value) value)
1075 ((:abort) (abort)))))))
1076
1077 (defslimefun connection-info ()
1078 "Return a key-value list of the form:
1079 \(&key PID STYLE LISP-IMPLEMENTATION MACHINE FEATURES PACKAGE)
1080 PID: is the process-id of Lisp process (or nil, depending on the STYLE)
1081 STYLE: the communication style
1082 LISP-IMPLEMENTATION: a list (&key TYPE TYPE-NAME VERSION)
1083 FEATURES: a list of keywords
1084 PACKAGE: a list (&key NAME PROMPT)"
1085 (setq *slime-features* *features*)
1086 `(:pid ,(getpid) :style ,(connection.communication-style *emacs-connection*)
1087 :lisp-implementation (:type ,(lisp-implementation-type)
1088 :type-name ,(lisp-implementation-type-name)
1089 :version ,(lisp-implementation-version))
1090 :machine (:instance ,(machine-instance)
1091 :type ,(machine-type)
1092 :version ,(machine-version))
1093 :features ,(features-for-emacs)
1094 :package (:name ,(package-name *package*)
1095 :prompt ,(package-string-for-prompt *package*))))
1096
1097 (defslimefun io-speed-test (&optional (n 5000) (m 1))
1098 (let* ((s *standard-output*)
1099 (*trace-output* (make-broadcast-stream s *log-output*)))
1100 (time (progn
1101 (dotimes (i n)
1102 (format s "~D abcdefghijklm~%" i)
1103 (when (zerop (mod n m))
1104 (force-output s)))
1105 (finish-output s)
1106 (when *emacs-connection*
1107 (eval-in-emacs '(message "done.")))))
1108 (terpri *trace-output*)
1109 (finish-output *trace-output*)
1110 nil))
1111
1112
1113 ;;;; Reading and printing
1114
1115 (defmacro define-special (name doc)
1116 "Define a special variable NAME with doc string DOC.
1117 This is like defvar, but NAME will not be initialized."
1118 `(progn
1119 (defvar ,name)
1120 (setf (documentation ',name 'variable) ,doc)))
1121
1122 (define-special *buffer-package*
1123 "Package corresponding to slime-buffer-package.
1124
1125 EVAL-FOR-EMACS binds *buffer-package*. Strings originating from a slime
1126 buffer are best read in this package. See also FROM-STRING and TO-STRING.")
1127
1128 (define-special *buffer-readtable*
1129 "Readtable associated with the current buffer")
1130
1131 (defmacro with-buffer-syntax ((&rest _) &body body)
1132 "Execute BODY with appropriate *package* and *readtable* bindings.
1133
1134 This should be used for code that is conceptionally executed in an
1135 Emacs buffer."
1136 (destructuring-bind () _
1137 `(call-with-buffer-syntax (lambda () ,@body))))
1138
1139 (defun call-with-buffer-syntax (fun)
1140 (let ((*package* *buffer-package*))
1141 ;; Don't shadow *readtable* unnecessarily because that prevents
1142 ;; the user from assigning to it.
1143 (if (eq *readtable* *buffer-readtable*)
1144 (call-with-syntax-hooks fun)
1145 (let ((*readtable* *buffer-readtable*))
1146 (call-with-syntax-hooks fun)))))
1147
1148 (defun to-string (object)
1149 "Write OBJECT in the *BUFFER-PACKAGE*.
1150 The result may not be readable."
1151 (with-buffer-syntax ()
1152 (let ((*print-readably* nil))
1153 (prin1-to-string object))))
1154
1155 (defun from-string (string)
1156 "Read string in the *BUFFER-PACKAGE*"
1157 (with-buffer-syntax ()
1158 (let ((*read-suppress* nil))
1159 (read-from-string string))))
1160
1161 ;; FIXME: deal with #\| etc. hard to do portably.
1162 (defun tokenize-symbol (string)
1163 (let ((package (let ((pos (position #\: string)))
1164 (if pos (subseq string 0 pos) nil)))
1165 (symbol (let ((pos (position #\: string :from-end t)))
1166 (if pos (subseq string (1+ pos)) string)))
1167 (internp (search "::" string)))
1168 (values symbol package internp)))
1169
1170 ;; FIXME: Escape chars are ignored
1171 (defun casify (string)
1172 "Convert string accoring to readtable-case."
1173 (ecase (readtable-case *readtable*)
1174 (:preserve string)
1175 (:upcase (string-upcase string))
1176 (:downcase (string-downcase string))
1177 (:invert (multiple-value-bind (lower upper) (determine-case string)
1178 (cond ((and lower upper) string)
1179 (lower (string-upcase string))
1180 (upper (string-downcase string))
1181 (t string))))))
1182
1183 (defun parse-symbol (string &optional (package *package*))
1184 "Find the symbol named STRING.
1185 Return the symbol and a flag indicating whether the symbols was found."
1186 (multiple-value-bind (sname pname) (tokenize-symbol string)
1187 (let ((package (cond ((string= pname "") keyword-package)
1188 (pname (find-package (casify pname)))
1189 (t package))))
1190 (if package
1191 (find-symbol (casify sname) package)
1192 (values nil nil)))))
1193
1194 (defun parse-symbol-or-lose (string &optional (package *package*))
1195 (multiple-value-bind (symbol status) (parse-symbol string package)
1196 (if status
1197 (values symbol status)
1198 (error "Unknown symbol: ~A [in ~A]" string package))))
1199
1200 ;; FIXME: interns the name
1201 (defun parse-package (string)
1202 "Find the package named STRING.
1203 Return the package or nil."
1204 (multiple-value-bind (name pos)
1205 (if (zerop (length string))
1206 (values :|| 0)
1207 (let ((*package* keyword-package))
1208 (ignore-errors (read-from-string string))))
1209 (if (and (or (keywordp name) (stringp name))
1210 (= (length string) pos))
1211 (find-package name))))
1212
1213 (defun guess-package-from-string (name &optional (default-package *package*))
1214 (or (and name
1215 (or (parse-package name)
1216 (find-package (string-upcase name))
1217 (parse-package (substitute #\- #\! name))))
1218 default-package))
1219
1220 (defvar *readtable-alist* (default-readtable-alist)
1221 "An alist mapping package names to readtables.")
1222
1223 (defun guess-buffer-readtable (package-name &optional (default *readtable*))
1224 (let ((package (guess-package-from-string package-name)))
1225 (if package
1226 (or (cdr (assoc (package-name package) *readtable-alist*
1227 :test #'string=))
1228 default)
1229 default)))
1230
1231 (defun valid-operator-symbol-p (symbol)
1232 "Test if SYMBOL names a function, macro, or special-operator."
1233 (or (fboundp symbol)
1234 (macro-function symbol)
1235 (special-operator-p symbol)))
1236
1237 (defun valid-operator-name-p (string)
1238 "Test if STRING names a function, macro, or special-operator."
1239 (let ((symbol (parse-symbol string)))
1240 (valid-operator-symbol-p symbol)))
1241
1242
1243 ;;;; Arglists
1244
1245 (defslimefun arglist-for-echo-area (names)
1246 "Return the arglist for the first function, macro, or special-op in NAMES."
1247 (handler-case
1248 (with-buffer-syntax ()
1249 (let ((name (find-if #'valid-operator-name-p names)))
1250 (if name (format-arglist-for-echo-area (parse-symbol name) name))))
1251 (error (cond)
1252 (format nil "ARGLIST: ~A" cond))))
1253
1254 (defun format-arglist-for-echo-area (symbol name)
1255 "Return SYMBOL's arglist as string for display in the echo area.
1256 Use the string NAME as operator name."
1257 (let ((arglist (arglist symbol)))
1258 (etypecase arglist
1259 ((member :not-available)
1260 nil)
1261 (list
1262 (let ((enriched-arglist
1263 (if (extra-keywords symbol)
1264 ;; When there are extra keywords, we decode the
1265 ;; arglist, merge in the keywords and encode it
1266 ;; again.
1267 (let ((decoded-arglist (decode-arglist arglist)))
1268 (enrich-decoded-arglist-with-extra-keywords
1269 decoded-arglist (list symbol))
1270 (encode-arglist decoded-arglist))
1271 ;; Otherwise, just use the original arglist.
1272 ;; This works better for implementation-specific
1273 ;; lambda-list-keywords like CMUCL's &parse-body.
1274 arglist)))
1275 (arglist-to-string (cons name enriched-arglist)
1276 (symbol-package symbol)))))))
1277
1278 (defun clean-arglist (arglist)
1279 "Remove &whole, &enviroment, and &aux elements from ARGLIST."
1280 (cond ((null arglist) '())
1281 ((member (car arglist) '(&whole &environment))
1282 (clean-arglist (cddr arglist)))
1283 ((eq (car arglist) '&aux)
1284 '())
1285 (t (cons (car arglist) (clean-arglist (cdr arglist))))))
1286
1287 (defun arglist-to-string (arglist package)
1288 "Print the list ARGLIST for display in the echo area.
1289 The argument name are printed without package qualifiers and
1290 pretty printing of (function foo) as #'foo is suppressed."
1291 (setq arglist (clean-arglist arglist))
1292 (etypecase arglist
1293 (null "()")
1294 (cons
1295 (with-output-to-string (*standard-output*)
1296 (with-standard-io-syntax
1297 (let ((*package* package) (*print-case* :downcase)
1298 (*print-pretty* t) (*print-circle* nil) (*print-readably* nil)
1299 (*print-level* 10) (*print-length* 20))
1300 (pprint-logical-block (nil nil :prefix "(" :suffix ")")
1301 (loop
1302 (let ((arg (pop arglist)))
1303 (etypecase arg
1304 (symbol (princ arg))
1305 (string (princ arg))
1306 (cons (pprint-logical-block (nil nil :prefix "(" :suffix ")")
1307 (princ (car arg))
1308 (unless (null (cdr arg))
1309 (write-char #\space))
1310 (pprint-fill *standard-output* (cdr arg) nil))))
1311 (when (null arglist) (return))
1312 (write-char #\space)
1313 (pprint-newline :fill))))))))))
1314
1315 (defun test-print-arglist (list string)
1316 (string= (arglist-to-string list (find-package :swank)) string))
1317
1318 ;; Should work:
1319 (progn
1320 (assert (test-print-arglist '(function cons) "(function cons)"))
1321 (assert (test-print-arglist '(quote cons) "(quote cons)"))
1322 (assert (test-print-arglist '(&key (function #'+)) "(&key (function #'+))"))
1323 (assert (test-print-arglist '(&whole x y z) "(y z)"))
1324 (assert (test-print-arglist '(x &aux y z) "(x)"))
1325 (assert (test-print-arglist '(x &environment env y) "(x y)")))
1326 ;; Expected failure:
1327 ;; (assert (test-print-arglist '(&key ((function f))) "(&key ((function f)))"))
1328
1329 (defslimefun variable-desc-for-echo-area (variable-name)
1330 "Return a short description of VARIABLE-NAME, or NIL."
1331 (with-buffer-syntax ()
1332 (let ((sym (parse-symbol variable-name)))
1333 (if (and sym (boundp sym))
1334 (let ((*print-pretty* nil) (*print-level* 4)
1335 (*print-length* 10) (*print-circle* t))
1336 (format nil "~A => ~A" sym (symbol-value sym)))))))
1337
1338 (defstruct (keyword-arg
1339 (:conc-name keyword-arg.)
1340 (:constructor make-keyword-arg (keyword arg-name default-arg)))
1341 keyword
1342 arg-name
1343 default-arg)
1344
1345 (defun decode-keyword-arg (arg)
1346 "Decode a keyword item of formal argument list.
1347 Return three values: keyword, argument name, default arg."
1348 (cond ((symbolp arg)
1349 (make-keyword-arg (intern (symbol-name arg) keyword-package)
1350 arg
1351 nil))
1352 ((and (consp arg)
1353 (consp (car arg)))
1354 (make-keyword-arg (caar arg)
1355 (cadar arg)
1356 (cadr arg)))
1357 ((consp arg)
1358 (make-keyword-arg (intern (symbol-name (car arg)) keyword-package)
1359 (car arg)
1360 (cadr arg)))
1361 (t
1362 (error "Bad keyword item of formal argument list"))))
1363
1364 (defun encode-keyword-arg (arg)
1365 (if (eql (intern (symbol-name (keyword-arg.arg-name arg))
1366 keyword-package)
1367 (keyword-arg.keyword arg))
1368 (if (keyword-arg.default-arg arg)
1369 (list (keyword-arg.arg-name arg)
1370 (keyword-arg.default-arg arg))
1371 (keyword-arg.arg-name arg))
1372 (let ((keyword/name (list (keyword-arg.arg-name arg)
1373 (keyword-arg.keyword arg))))
1374 (if (keyword-arg.default-arg arg)
1375 (list keyword/name
1376 (keyword-arg.default-arg arg))
1377 (list keyword/name)))))
1378
1379 (progn
1380 (assert (equalp (decode-keyword-arg 'x)
1381 (make-keyword-arg :x 'x nil)))
1382 (assert (equalp (decode-keyword-arg '(x t))
1383 (make-keyword-arg :x 'x t)))
1384 (assert (equalp (decode-keyword-arg '((:x y)))
1385 (make-keyword-arg :x 'y nil)))
1386 (assert (equalp (decode-keyword-arg '((:x y) t))
1387 (make-keyword-arg :x 'y t))))
1388
1389 (defstruct (optional-arg
1390 (:conc-name optional-arg.)
1391 (:constructor make-optional-arg (arg-name default-arg)))
1392 arg-name
1393 default-arg)
1394
1395 (defun decode-optional-arg (arg)
1396 "Decode an optional item of a formal argument list.
1397 Return an OPTIONAL-ARG structure."
1398 (etypecase arg
1399 (symbol (make-optional-arg arg nil))
1400 (list (make-optional-arg (car arg) (cadr arg)))))
1401
1402 (defun encode-optional-arg (optional-arg)
1403 (if (optional-arg.default-arg optional-arg)
1404 (list (optional-arg.arg-name optional-arg)
1405 (optional-arg.default-arg optional-arg))
1406 (optional-arg.arg-name optional-arg)))
1407
1408 (progn
1409 (assert (equalp (decode-optional-arg 'x)
1410 (make-optional-arg 'x nil)))
1411 (assert (equalp (decode-optional-arg '(x t))
1412 (make-optional-arg 'x t))))
1413
1414 (defstruct (arglist (:conc-name arglist.))
1415 required-args ; list of the required arguments
1416 optional-args ; list of the optional arguments
1417 key-p ; whether &key appeared
1418 keyword-args ; list of the keywords
1419 rest ; name of the &rest or &body argument (if any)
1420 body-p ; whether the rest argument is a &body
1421 allow-other-keys-p) ; whether &allow-other-keys appeared
1422
1423 (defun decode-arglist (arglist)
1424 "Parse the list ARGLIST and return an ARGLIST structure."
1425 (let ((mode nil)
1426 (result (make-arglist)))
1427 (dolist (arg arglist)
1428 (cond
1429 ((eql arg '&allow-other-keys)
1430 (setf (arglist.allow-other-keys-p result) t))
1431 ((eql arg '&key)
1432 (setf (arglist.key-p result) t
1433 mode arg))
1434 ((member arg lambda-list-keywords)
1435 (setq mode arg))
1436 (t
1437 (case mode
1438 (&key
1439 (push (decode-keyword-arg arg)
1440 (arglist.keyword-args result)))
1441 (&optional
1442 (push (decode-optional-arg arg)
1443 (arglist.optional-args result)))
1444 (&body
1445 (setf (arglist.body-p result) t
1446 (arglist.rest result) arg))
1447 (&rest
1448 (setf (arglist.rest result) arg))
1449 ((nil)
1450 (push arg (arglist.required-args result)))
1451 ((&whole &environment)
1452 (setf mode nil))))))
1453 (setf (arglist.required-args result)
1454 (nreverse (arglist.required-args result)))
1455 (setf (arglist.optional-args result)
1456 (nreverse (arglist.optional-args result)))
1457 (setf (arglist.keyword-args result)
1458 (nreverse (arglist.keyword-args result)))
1459 result))
1460
1461 (defun encode-arglist (decoded-arglist)
1462 (append (arglist.required-args decoded-arglist)
1463 (when (arglist.optional-args decoded-arglist)
1464 '(&optional))
1465 (mapcar #'encode-optional-arg (arglist.optional-args decoded-arglist))
1466 (when (arglist.key-p decoded-arglist)
1467 '(&key))
1468 (mapcar #'encode-keyword-arg (arglist.keyword-args decoded-arglist))
1469 (when (arglist.allow-other-keys-p decoded-arglist)
1470 '(&allow-other-keys))
1471 (cond ((not (arglist.rest decoded-arglist))
1472 '())
1473 ((arglist.body-p decoded-arglist)
1474 `(&body ,(arglist.rest decoded-arglist)))
1475 (t
1476 `(&rest ,(arglist.rest decoded-arglist))))))
1477
1478 (defun arglist-keywords (arglist)
1479 "Return the list of keywords in ARGLIST.
1480 As a secondary value, return whether &allow-other-keys appears."
1481 (let ((decoded-arglist (decode-arglist arglist)))
1482 (values (arglist.keyword-args decoded-arglist)
1483 (arglist.allow-other-keys-p decoded-arglist))))
1484
1485 (defun methods-keywords (methods)
1486 "Collect all keywords in the arglists of METHODS.
1487 As a secondary value, return whether &allow-other-keys appears somewhere."
1488 (let ((keywords '())
1489 (allow-other-keys nil))
1490 (dolist (method methods)
1491 (multiple-value-bind (kw aok)
1492 (arglist-keywords
1493 (swank-mop:method-lambda-list method))
1494 (setq keywords (remove-duplicates (append keywords kw)
1495 :key #'keyword-arg.keyword)
1496 allow-other-keys (or allow-other-keys aok))))
1497 (values keywords allow-other-keys)))
1498
1499 (defun generic-function-keywords (generic-function)
1500 "Collect all keywords in the methods of GENERIC-FUNCTION.
1501 As a secondary value, return whether &allow-other-keys appears somewhere."
1502 (methods-keywords
1503 (swank-mop:generic-function-methods generic-function)))
1504
1505 (defun applicable-methods-keywords (generic-function classes)
1506 "Collect all keywords in the methods of GENERIC-FUNCTION that are
1507 applicable for argument of CLASSES. As a secondary value, return
1508 whether &allow-other-keys appears somewhere."
1509 (methods-keywords
1510 (swank-mop:compute-applicable-methods-using-classes
1511 generic-function classes)))
1512
1513 (defun arglist-to-template-string (arglist package)
1514 "Print the list ARGLIST for insertion as a template for a function call."
1515 (decoded-arglist-to-template-string
1516 (decode-arglist arglist) package))
1517
1518 (defun decoded-arglist-to-template-string (decoded-arglist package &key (prefix "(") (suffix ")"))
1519 (with-output-to-string (*standard-output*)
1520 (with-standard-io-syntax
1521 (let ((*package* package) (*print-case* :downcase)
1522 (*print-pretty* t) (*print-circle* nil) (*print-readably* nil)
1523 (*print-level* 10) (*print-length* 20))
1524 (pprint-logical-block (nil nil :prefix prefix :suffix suffix)
1525 (print-decoded-arglist-as-template decoded-arglist))))))
1526
1527 (defun print-decoded-arglist-as-template (decoded-arglist)
1528 (let ((first-p t))
1529 (flet ((space ()
1530 (unless first-p
1531 (write-char #\space)
1532 (pprint-newline :fill))
1533 (setq first-p nil)))
1534 (dolist (arg (arglist.required-args decoded-arglist))
1535 (space)
1536 (princ arg))
1537 (dolist (arg (arglist.optional-args decoded-arglist))
1538 (space)
1539 (format t "[~A]" (optional-arg.arg-name arg)))
1540 (dolist (keyword-arg (arglist.keyword-args decoded-arglist))
1541 (space)
1542 (let ((arg-name (keyword-arg.arg-name keyword-arg))
1543 (keyword (keyword-arg.keyword keyword-arg)))
1544 (format t "~W ~A"
1545 (if (keywordp keyword) keyword `',keyword)
1546 arg-name)))
1547 (when (and (arglist.rest decoded-arglist)
1548 (or (not (arglist.keyword-args decoded-arglist))
1549 (arglist.allow-other-keys-p decoded-arglist)))
1550 (if (arglist.body-p decoded-arglist)
1551 (pprint-newline :mandatory)
1552 (space))
1553 (format t "~A..." (arglist.rest decoded-arglist)))))
1554 (pprint-newline :fill))
1555
1556 (defgeneric extra-keywords (operator &rest args)
1557 (:documentation "Return a list of extra keywords of OPERATOR (a
1558 symbol) when applied to the (unevaluated) ARGS. As a secondary value,
1559 return whether other keys are allowed."))
1560
1561 (defmethod extra-keywords (operator &rest args)
1562 ;; default method
1563 (declare (ignore args))
1564 (let ((symbol-function (symbol-function operator)))
1565 (if (typep symbol-function 'generic-function)
1566 (generic-function-keywords symbol-function)
1567 nil)))
1568
1569 (defmethod extra-keywords ((operator (eql 'make-instance))
1570 &rest args)
1571 (unless (null args)
1572 (let ((class-name-form (car args)))
1573 (when (and (listp class-name-form)
1574 (= (length class-name-form) 2)
1575 (eq (car class-name-form) 'quote))
1576 (let* ((class-name (cadr class-name-form))
1577 (class (find-class class-name nil)))
1578 (unless (swank-mop:class-finalized-p class)
1579 ;; Try to finalize the class, which can fail if
1580 ;; superclasses are not defined yet
1581 (handler-case (swank-mop:finalize-inheritance class)
1582 (program-error (c)
1583 (declare (ignore c)))))
1584 (when class
1585 ;; We have the case (make-instance 'CLASS ...)
1586 ;; with a known CLASS.
1587 (multiple-value-bind (slots allow-other-keys-p)
1588 (if (swank-mop:class-finalized-p class)
1589 (values (swank-mop:class-slots class) nil)
1590 (values (swank-mop:class-direct-slots class) t))
1591 (let ((slot-init-keywords
1592 (loop for slot in slots append
1593 (mapcar (lambda (initarg)
1594 (make-keyword-arg
1595 initarg
1596 initarg ; FIXME
1597 (swank-mop:slot-definition-initform slot)))
1598 (swank-mop:slot-definition-initargs slot))))
1599 (initialize-instance-keywords
1600 (applicable-methods-keywords #'initialize-instance
1601 (list class))))
1602 (return-from extra-keywords
1603 (values (append slot-init-keywords
1604 initialize-instance-keywords)
1605 allow-other-keys-p)))))))))
1606 (call-next-method))
1607
1608 (defun enrich-decoded-arglist-with-extra-keywords (decoded-arglist form)
1609 (multiple-value-bind (extra-keywords extra-aok)
1610 (apply #'extra-keywords form)
1611 ;; enrich the list of keywords with the extra keywords
1612 (when extra-keywords
1613 (setf (arglist.key-p decoded-arglist) t)
1614 (setf (arglist.keyword-args decoded-arglist)
1615 (remove-duplicates
1616 (append (arglist.keyword-args decoded-arglist)
1617 extra-keywords)
1618 :key #'keyword-arg.keyword)))
1619 (setf (arglist.allow-other-keys-p decoded-arglist)
1620 (or (arglist.allow-other-keys-p decoded-arglist) extra-aok)))
1621 decoded-arglist)
1622
1623 (defslimefun arglist-for-insertion (name)
1624 (with-buffer-syntax ()
1625 (let ((symbol (parse-symbol name)))
1626 (cond
1627 ((and symbol
1628 (valid-operator-name-p name))
1629 (let ((arglist (arglist symbol)))
1630 (etypecase arglist
1631 ((member :not-available)
1632 :not-available)
1633 (list
1634 (let ((decoded-arglist (decode-arglist arglist)))
1635 (enrich-decoded-arglist-with-extra-keywords decoded-arglist
1636 (list symbol))
1637 (decoded-arglist-to-template-string decoded-arglist
1638 *buffer-package*))))))
1639 (t
1640 :not-available)))))
1641
1642 (defvar *remove-keywords-alist*
1643 '((:test :test-not)
1644 (:test-not :test)))
1645
1646 (defun remove-actual-args (decoded-arglist actual-arglist)
1647 "Remove from DECODED-ARGLIST the arguments that have already been
1648 provided in ACTUAL-ARGLIST."
1649 (loop while (and actual-arglist
1650 (arglist.required-args decoded-arglist))
1651 do (progn (pop actual-arglist)
1652 (pop (arglist.required-args decoded-arglist))))
1653 (loop while (and actual-arglist
1654 (arglist.optional-args decoded-arglist))
1655 do (progn (pop actual-arglist)
1656 (pop (arglist.optional-args decoded-arglist))))
1657 (loop for keyword in actual-arglist by #'cddr
1658 for keywords-to-remove = (cdr (assoc keyword *remove-keywords-alist*))
1659 do (setf (arglist.keyword-args decoded-arglist)
1660 (remove-if (lambda (kw)
1661 (or (eql kw keyword)
1662 (member kw keywords-to-remove)))
1663 (arglist.keyword-args decoded-arglist)
1664 :key #'keyword-arg.keyword))))
1665
1666 (defgeneric form-completion (operator-form &rest argument-forms))
1667
1668 (defmethod form-completion (operator-form &rest argument-forms)
1669 (when (and (symbolp operator-form)
1670 (valid-operator-symbol-p operator-form))
1671 (let ((arglist (arglist operator-form)))
1672 (etypecase arglist
1673 ((member :not-available)
1674 :not-available)
1675 (list
1676 (let ((decoded-arglist (decode-arglist arglist)))
1677 (enrich-decoded-arglist-with-extra-keywords decoded-arglist
1678 (cons operator-form
1679 argument-forms))
1680 ;; get rid of formal args already provided
1681 (remove-actual-args decoded-arglist argument-forms)
1682 (return-from form-completion decoded-arglist))))))
1683 :not-available)
1684
1685 (defmethod form-completion ((operator-form (eql 'defmethod))
1686 &rest argument-forms)
1687 (when (and (listp argument-forms)
1688 (not (null argument-forms)) ;have generic function name
1689 (notany #'listp (rest argument-forms))) ;don't have arglist yet
1690 (let* ((gf-name (first argument-forms))
1691 (gf (and (or (symbolp gf-name)
1692 (and (listp gf-name)
1693 (eql (first gf-name) 'setf)))
1694 (fboundp gf-name)
1695 (fdefinition gf-name))))
1696 (when (typep gf 'generic-function)
1697 (let ((arglist (arglist gf)))
1698 (etypecase arglist
1699 ((member :not-available))
1700 (list
1701 (return-from form-completion
1702 (make-arglist :required-args (list arglist)
1703 :rest "body" :body-p t))))))))
1704 (call-next-method))
1705
1706 (defslimefun complete-form (form-string)
1707 "Read FORM-STRING in the current buffer package, then complete it
1708 by adding a template for the missing arguments."
1709 (with-buffer-syntax ()
1710 (handler-case
1711 (let ((form (read-from-string form-string)))
1712 (when (consp form)
1713 (let ((operator-form (first form))
1714 (argument-forms (rest form)))
1715 (let ((form-completion
1716 (apply #'form-completion operator-form argument-forms)))
1717 (unless (eql form-completion :not-available)
1718 (return-from complete-form
1719 (decoded-arglist-to-template-string form-completion
1720 *buffer-package*
1721 :prefix ""))))))
1722 :not-available)
1723 (reader-error (c)
1724 (declare (ignore c))
1725 :not-available))))
1726
1727
1728 ;;;; Recording and accessing results of computations
1729
1730 (defvar *record-repl-results* t
1731 "Non-nil means that REPL results are saved for later lookup.")
1732
1733 (defvar *object-to-presentation-id*
1734 (make-weak-key-hash-table :test 'eq)
1735 "Store the mapping of objects to numeric identifiers")
1736
1737 (defvar *presentation-id-to-object*
1738 (make-weak-value-hash-table :test 'eql)
1739 "Store the mapping of numeric identifiers to objects")
1740
1741 (defun clear-presentation-tables ()
1742 (clrhash *object-to-presentation-id*)
1743 (clrhash *presentation-id-to-object*))
1744
1745 (defvar *presentation-counter* 0 "identifier counter")
1746
1747 ;; XXX thread safety?
1748 (defun save-presented-object (object)
1749 "Save OBJECT and return the assigned id.
1750 If OBJECT was saved previously return the old id."
1751 (or (gethash object *object-to-presentation-id*)
1752 (let ((id (decf *presentation-counter*)))
1753 (setf (gethash id *presentation-id-to-object*) object)
1754 (setf (gethash object *object-to-presentation-id*) id)
1755 id)))
1756
1757 (defun lookup-presented-object (id)
1758 "Retrieve the object corresponding to ID.
1759 The secondary value indicates the absence of an entry."
1760 (gethash id *presentation-id-to-object*))
1761
1762 (defslimefun get-repl-result (id)
1763 "Get the result of the previous REPL evaluation with ID."
1764 (multiple-value-bind (object foundp) (lookup-presented-object id)
1765 (cond (foundp object)
1766 (t (error "Attempt to access unrecorded object (id ~D)." id)))))
1767
1768 (defslimefun clear-repl-results ()
1769 "Forget the results of all previous REPL evaluations."
1770 (clear-presentation-tables)
1771 t)
1772
1773
1774 ;;;; Evaluation
1775
1776 (defvar *pending-continuations* '()
1777 "List of continuations for Emacs. (thread local)")
1778
1779 (defun guess-buffer-package (string)
1780 "Return a package for STRING.
1781 Fall back to the the current if no such package exists."
1782 (or (guess-package-from-string string nil)
1783 *package*))
1784
1785 (defun eval-for-emacs (form buffer-package id)
1786 "Bind *BUFFER-PACKAGE* BUFFER-PACKAGE and evaluate FORM.
1787 Return the result to the continuation ID.
1788 Errors are trapped and invoke our debugger."
1789 (call-with-debugger-hook
1790 #'swank-debugger-hook
1791 (lambda ()
1792 (let (ok result)
1793 (unwind-protect
1794 (let ((*buffer-package* (guess-buffer-package buffer-package))
1795 (*buffer-readtable* (guess-buffer-readtable buffer-package))
1796 (*pending-continuations* (cons id *pending-continuations*)))
1797 (check-type *buffer-package* package)
1798 (check-type *buffer-readtable* readtable)
1799 (setq result (eval form))
1800 (finish-output)
1801 (run-hook *pre-reply-hook*)
1802 (setq ok t))
1803 (force-user-output)
1804 (send-to-emacs `(:return ,(current-thread)
1805 ,(if ok `(:ok ,result) '(:abort))
1806 ,id)))))))
1807
1808 (defvar *echo-area-prefix* "=> "
1809 "A prefix that `format-values-for-echo-area' should use.")
1810
1811 (defun format-values-for-echo-area (values)
1812 (with-buffer-syntax ()
1813 (let ((*print-readably* nil))
1814 (cond ((null values) "; No value")
1815 ((and (null (cdr values)) (integerp (car values)))
1816 (let ((i (car values)))
1817 (format nil "~A~D (#x~X, #o~O, #b~B)"
1818 *echo-area-prefix* i i i i)))
1819 (t (format nil "~A~{~S~^, ~}" *echo-area-prefix* values))))))
1820
1821 (defslimefun interactive-eval (string)
1822 (with-buffer-syntax ()
1823 (let ((values (multiple-value-list (eval (from-string string)))))
1824 (fresh-line)
1825 (finish-output)
1826 (format-values-for-echo-area values))))
1827
1828 (defslimefun eval-and-grab-output (string)
1829 (with-buffer-syntax ()
1830 (let* ((s (make-string-output-stream))
1831 (*standard-output* s)
1832 (values (multiple-value-list (eval (from-string string)))))
1833 (list (get-output-stream-string s)
1834 (format nil "~{~S~^~%~}" values)))))
1835
1836 ;;; XXX do we need this stuff? What is it good for?
1837 (defvar *slime-repl-advance-history* nil
1838 "In the dynamic scope of a single form typed at the repl, is set to nil to
1839 prevent the repl from advancing the history - * ** *** etc.")
1840
1841 (defvar *slime-repl-suppress-output* nil
1842 "In the dynamic scope of a single form typed at the repl, is set to nil to
1843 prevent the repl from printing the result of the evalation.")
1844
1845 (defvar *slime-repl-eval-hook-pass* (gensym "PASS")
1846 "Token to indicate that a repl hook declines to evaluate the form")
1847
1848 (defvar *slime-repl-eval-hooks* nil
1849 "A list of functions. When the repl is about to eval a form, first try running each of
1850 these hooks. The first hook which returns a value which is not *slime-repl-eval-hook-pass*
1851 is considered a replacement for calling eval. If there are no hooks, or all
1852 pass, then eval is used.")
1853
1854 (defslimefun repl-eval-hook-pass ()
1855 "call when repl hook declines to evaluate the form"
1856 (throw *slime-repl-eval-hook-pass* *slime-repl-eval-hook-pass*))
1857
1858 (defslimefun repl-suppress-output ()
1859 "In the dynamic scope of a single form typed at the repl, call to
1860 prevent the repl from printing the result of the evalation."
1861 (setq *slime-repl-suppress-output* t))
1862
1863 (defslimefun repl-suppress-advance-history ()
1864 "In the dynamic scope of a single form typed at the repl, call to
1865 prevent the repl from advancing the history - * ** *** etc."
1866 (setq *slime-repl-advance-history* nil))
1867
1868 (defun eval-region (string &optional package-update-p)
1869 "Evaluate STRING and return the result.
1870 If PACKAGE-UPDATE-P is non-nil, and evaluation causes a package
1871 change, then send Emacs an update."
1872 (unwind-protect
1873 (with-input-from-string (stream string)
1874 (let (- values)
1875 (loop
1876 (let ((form (read stream nil stream)))
1877 (when (eq form stream)
1878 (fresh-line)
1879 (finish-output)
1880 (return (values values -)))
1881 (setq - form)
1882 (if *slime-repl-eval-hooks*
1883 (setq values (run-repl-eval-hooks form))
1884 (setq values (multiple-value-list (eval form))))
1885 (finish-output)))))
1886 (when (and package-update-p (not (eq *package* *buffer-package*)))
1887 (send-to-emacs
1888 (list :new-package (package-name *package*)
1889 (package-string-for-prompt *package*))))))
1890
1891 (defun run-repl-eval-hooks (form)
1892 (loop for hook in *slime-repl-eval-hooks*
1893 for res = (catch *slime-repl-eval-hook-pass*
1894 (multiple-value-list (funcall hook form)))
1895 until (not (eq res *slime-repl-eval-hook-pass*))
1896 finally (return
1897 (if (eq res *slime-repl-eval-hook-pass*)
1898 (multiple-value-list (eval form))
1899 res))))
1900
1901 (defun package-string-for-prompt (package)
1902 "Return the shortest nickname (or canonical name) of PACKAGE."
1903 (princ-to-string
1904 (make-symbol
1905 (or (canonical-package-nickname package)
1906 (auto-abbreviated-package-name package)
1907 (shortest-package-nickname package)))))
1908
1909 (defun canonical-package-nickname (package)
1910 "Return the canonical package nickname, if any, of PACKAGE."
1911 (let ((name (cdr (assoc (package-name package) *canonical-package-nicknames*
1912 :test #'string=))))
1913 (and name (string name))))
1914
1915 (defun auto-abbreviated-package-name (package)
1916 "Return an abbreviated 'name' for PACKAGE.
1917
1918 N.B. this is not an actual package name or nickname."
1919 (when *auto-abbreviate-dotted-packages*
1920 (let ((last-dot (position #\. (package-name package) :from-end t)))
1921 (when last-dot (subseq (package-name package) (1+ last-dot))))))
1922
1923 (defun shortest-package-nickname (package)
1924 "Return the shortest nickname (or canonical name) of PACKAGE."
1925 (loop for name in (cons (package-name package) (package-nicknames package))
1926 for shortest = name then (if (< (length name) (length shortest))
1927 name
1928 shortest)
1929 finally (return shortest)))
1930
1931 (defslimefun interactive-eval-region (string)
1932 (with-buffer-syntax ()
1933 (format-values-for-echo-area (eval-region string))))
1934
1935 (defslimefun re-evaluate-defvar (form)
1936 (with-buffer-syntax ()
1937 (let ((form (read-from-string form)))
1938 (destructuring-bind (dv name &optional value doc) form
1939 (declare (ignore value doc))
1940 (assert (eq dv 'defvar))
1941 (makunbound name)
1942 (prin1-to-string (eval form))))))
1943
1944 (defvar *swank-pprint-bindings*
1945 `((*print-pretty* . t)
1946 (*print-level* . nil)
1947 (*print-length* . nil)
1948 (*print-circle* . t)
1949 (*print-gensym* . t)
1950 (*print-readably* . nil))
1951 "A list of variables bindings during pretty printing.
1952 Used by pprint-eval.")
1953
1954 (defun swank-pprint (list)
1955 "Bind some printer variables and pretty print each object in LIST."
1956 (with-buffer-syntax ()
1957 (with-bindings *swank-pprint-bindings*
1958 (cond ((null list) "; No value")
1959 (t (with-output-to-string (*standard-output*)
1960 (dolist (o list)
1961 (pprint o)
1962 (terpri))))))))
1963
1964 (defslimefun pprint-eval (string)
1965 (with-buffer-syntax ()
1966 (swank-pprint (multiple-value-list (eval (read-from-string string))))))
1967
1968 (defslimefun set-package (package)
1969 "Set *package* to PACKAGE.
1970 Return its name and the string to use in the prompt."
1971 (let ((p (setq *package* (guess-package-from-string package))))
1972 (list (package-name p) (package-string-for-prompt p))))
1973
1974 (defslimefun listener-eval (string)
1975 (clear-user-input)
1976 (with-buffer-syntax ()
1977 (let ((*slime-repl-suppress-output* :unset)
1978 (*slime-repl-advance-history* :unset))
1979 (multiple-value-bind (values last-form) (eval-region string t)
1980 (unless (or (and (eq values nil) (eq last-form nil))
1981 (eq *slime-repl-advance-history* nil))
1982 (setq *** ** ** * * (car values)
1983 /// // // / / values))
1984 (setq +++ ++ ++ + + last-form)
1985 (cond ((eq *slime-repl-suppress-output* t) '(:suppress-output))
1986 (*record-repl-results*
1987 `(:present ,(loop for x in values
1988 collect (cons (prin1-to-string x)
1989 (save-presented-object x)))))
1990 (t
1991 `(:values ,(mapcar #'prin1-to-string values))))))))
1992
1993 (defslimefun ed-in-emacs (&optional what)
1994 "Edit WHAT in Emacs.
1995
1996 WHAT can be:
1997 A pathname or a string,
1998 A list (PATHNAME-OR-STRING LINE [COLUMN]),
1999 A function name (symbol),
2000 NIL.
2001
2002 Returns true if it actually called emacs, or NIL if not."
2003 (flet ((pathname-or-string-p (thing)
2004 (or (pathnamep thing) (typep thing 'string))))
2005 (let ((target
2006 (cond ((and (listp what) (pathname-or-string-p (first what)))
2007 (cons (canonicalize-filename (car what)) (cdr what)))
2008 ((pathname-or-string-p what)
2009 (canonicalize-filename what))
2010 ((symbolp what) what)
2011 (t (return-from ed-in-emacs nil)))))
2012 (send-oob-to-emacs `(:ed ,target))
2013 t)))
2014
2015 (defslimefun value-for-editing (form)
2016 "Return a readable value of FORM for editing in Emacs.
2017 FORM is expected, but not required, to be SETF'able."
2018 ;; FIXME: Can we check FORM for setfability? -luke (12/Mar/2005)
2019 (with-buffer-syntax ()
2020 (prin1-to-string (eval (read-from-string form)))))
2021
2022 (defslimefun commit-edited-value (form value)
2023 "Set the value of a setf'able FORM to VALUE.
2024 FORM and VALUE are both strings from Emacs."
2025 (with-buffer-syntax ()
2026 (eval `(setf ,(read-from-string form)
2027 ,(read-from-string (concatenate 'string "`" value))))
2028 t))
2029
2030 (defun background-message (format-string &rest args)
2031 "Display a message in Emacs' echo area.
2032
2033 Use this function for informative messages only. The message may even
2034 be dropped, if we are too busy with other things."
2035 (when *emacs-connection*
2036 (send-to-emacs `(:background-message
2037 ,(apply #'format nil format-string args)))))
2038
2039
2040 ;;;; Debugger
2041
2042 (defun swank-debugger-hook (condition hook)
2043 "Debugger function for binding *DEBUGGER-HOOK*.
2044 Sends a message to Emacs declaring that the debugger has been entered,
2045 then waits to handle further requests from Emacs. Eventually returns
2046 after Emacs causes a restart to be invoked."
2047 (declare (ignore hook))
2048 (cond (*emacs-connection*
2049 (debug-in-emacs condition))
2050 ((default-connection)
2051 (with-connection ((default-connection))
2052 (debug-in-emacs condition)))))
2053
2054 (defvar *global-debugger* t
2055 "Non-nil means the Swank debugger hook will be installed globally.")
2056
2057 (add-hook *new-connection-hook* 'install-debugger)
2058 (defun install-debugger (connection)
2059 (declare (ignore connection))
2060 (when *global-debugger*
2061 (install-debugger-globally #'swank-debugger-hook)))
2062
2063 ;;;;; Debugger loop
2064 ;;;
2065 ;;; These variables are dynamically bound during debugging.
2066 ;;;
2067 (defvar *swank-debugger-condition* nil
2068 "The condition being debugged.")
2069
2070 (defvar *sldb-level* 0
2071 "The current level of recursive debugging.")
2072
2073 (defvar *sldb-initial-frames* 20
2074 "The initial number of backtrace frames to send to Emacs.")
2075
2076 (defvar *sldb-restarts* nil
2077 "The list of currenlty active restarts.")
2078
2079 (defvar *sldb-stepping-p* nil
2080 "True when during execution of a stepp command.")
2081
2082 (defun debug-in-emacs (condition)
2083 (let ((*swank-debugger-condition* condition)
2084 (*sldb-restarts* (compute-restarts condition))
2085 (*package* (or (and (boundp '*buffer-package*)
2086 (symbol-value '*buffer-package*))
2087 *package*))
2088 (*sldb-level* (1+ *sldb-level*))
2089 (*sldb-stepping-p* nil)
2090 (*swank-state-stack* (cons :swank-debugger-hook *swank-state-stack*)))
2091 (force-user-output)
2092 (with-bindings *sldb-printer-bindings*
2093 (call-with-debugging-environment
2094 (lambda () (sldb-loop *sldb-level*))))))
2095
2096 (defun sldb-loop (level)
2097 (unwind-protect
2098 (catch 'sldb-enter-default-debugger
2099 (send-to-emacs
2100 (list* :debug (current-thread) level
2101 (debugger-info-for-emacs 0 *sldb-initial-frames*)))
2102 (loop (catch 'sldb-loop-catcher
2103 (with-simple-restart (abort "Return to sldb level ~D." level)
2104 (send-to-emacs (list :debug-activate (current-thread)
2105 level))
2106 (handler-bind ((sldb-condition #'handle-sldb-condition))
2107 (read-from-emacs))))))
2108 (send-to-emacs `(:debug-return
2109 ,(current-thread) ,level ,*sldb-stepping-p*))))
2110
2111 (defun handle-sldb-condition (condition)
2112 "Handle an internal debugger condition.
2113 Rather than recursively debug the debugger (a dangerous idea!), these
2114 conditions are simply reported."
2115 (let ((real-condition (original-condition condition)))
2116 (send-to-emacs `(:debug-condition ,(current-thread)
2117 ,(princ-to-string real-condition))))
2118 (throw 'sldb-loop-catcher nil))
2119
2120 (defun safe-condition-message (condition)
2121 "Safely print condition to a string, handling any errors during
2122 printing."
2123 (let ((*print-pretty* t))
2124 (handler-case
2125 (format-sldb-condition condition)
2126 (error (cond)
2127 ;; Beware of recursive errors in printing, so only use the condition
2128 ;; if it is printable itself:
2129 (format nil "Unable to display error condition~@[: ~A~]"
2130 (ignore-errors (princ-to-string cond)))))))
2131
2132 (defun debugger-condition-for-emacs ()
2133 (list (safe-condition-message *swank-debugger-condition*)
2134 (format nil " [Condition of type ~S]"
2135 (type-of *swank-debugger-condition*))
2136 (condition-references *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 (loop for restart in *sldb-restarts*
2143 collect (list (princ-to-string (restart-name restart))
2144 (princ-to-string restart))))
2145
2146 (defun frame-for-emacs (n frame)
2147 (let* ((label (format nil " ~2D: " n))
2148 (string (with-output-to-string (stream)
2149 (princ label stream)
2150 (print-frame frame stream))))
2151 (subseq string (length label))))
2152
2153 ;;;;; SLDB entry points
2154
2155 (defslimefun sldb-break-with-default-debugger ()
2156 "Invoke the default debugger by returning from our debugger-loop."
2157 (throw 'sldb-enter-default-debugger nil))
2158
2159 (defslimefun backtrace (start end)
2160 "Return a list ((I FRAME) ...) of frames from START to END.
2161 I is an integer describing and FRAME a string."
2162 (loop for frame in (compute-backtrace start end)
2163 for i from start
2164 collect (list i (frame-for-emacs i frame))))
2165
2166 (defslimefun debugger-info-for-emacs (start end)
2167 "Return debugger state, with stack frames from START to END.
2168 The result is a list:
2169 (condition ({restart}*) ({stack-frame}*) (cont*))
2170 where
2171 condition ::= (description type [extra])
2172 restart ::= (name description)
2173 stack-frame ::= (number description)
2174 extra ::= (:references and other random things)
2175 cont ::= continutation
2176 condition---a pair of strings: message, and type. If show-source is
2177 not nil it is a frame number for which the source should be displayed.
2178
2179 restart---a pair of strings: restart name, and description.
2180
2181 stack-frame---a number from zero (the top), and a printed
2182 representation of the frame's call.
2183
2184 continutation---the id of a pending Emacs continuation.
2185
2186 Below is an example return value. In this case the condition was a
2187 division by zero (multi-line description), and only one frame is being
2188 fetched (start=0, end=1).
2189
2190 ((\"Arithmetic error DIVISION-BY-ZERO signalled.
2191 Operation was KERNEL::DIVISION, operands (1 0).\"
2192 \"[Condition of type DIVISION-BY-ZERO]\")
2193 ((\"ABORT\" \"Return to Slime toplevel.\")
2194 (\"ABORT\" \"Return to Top-Level.\"))
2195 ((0 \"(KERNEL::INTEGER-/-INTEGER 1 0)\"))
2196 (4))"
2197 (list (debugger-condition-for-emacs)
2198 (format-restarts-for-emacs)
2199 (backtrace start end)
2200 *pending-continuations*))
2201
2202 (defun nth-restart (index)
2203 (nth index *sldb-restarts*))
2204
2205 (defslimefun invoke-nth-restart (index)
2206 (invoke-restart-interactively (nth-restart index)))
2207
2208 (defslimefun sldb-abort ()
2209 (invoke-restart (find 'abort *sldb-restarts* :key #'restart-name)))
2210
2211 (defslimefun sldb-continue ()
2212 (continue))
2213
2214 (defslimefun throw-to-toplevel ()
2215 "Invoke the ABORT-REQUEST restart abort an RPC from Emacs.
2216 If we are not evaluating an RPC then ABORT instead."
2217 (let ((restart (find-restart 'abort-request)))
2218 (cond (restart (invoke-restart restart))
2219 (t
2220 ;; If we get here then there was no catch. Try aborting as
2221 ;; a fallback. That makes the 'q' command in SLDB safer to
2222 ;; use with threads.
2223 (abort)))))
2224
2225 (defslimefun invoke-nth-restart-for-emacs (sldb-level n)
2226 "Invoke the Nth available restart.
2227 SLDB-LEVEL is the debug level when the request was made. If this
2228 has changed, ignore the request."
2229 (when (= sldb-level *sldb-level*)
2230 (invoke-nth-restart n)))
2231
2232 (defun wrap-sldb-vars (form)
2233 `(let ((*sldb-level* ,*sldb-level*))
2234 ,form))
2235
2236 (defslimefun eval-string-in-frame (string index)
2237 (to-string (eval-in-frame (wrap-sldb-vars (from-string string))
2238 index)))
2239
2240 (defslimefun pprint-eval-string-in-frame (string index)
2241 (swank-pprint
2242 (multiple-value-list
2243 (eval-in-frame (wrap-sldb-vars (from-string string)) index))))
2244
2245 (defslimefun frame-locals-for-emacs (index)
2246 "Return a property list ((&key NAME ID VALUE) ...) describing
2247 the local variables in the frame INDEX."
2248 (mapcar (lambda (frame-locals)
2249 (destructuring-bind (&key name id value) frame-locals
2250 (list :name (prin1-to-string name) :id id
2251 :value (to-string value))))
2252 (frame-locals index)))
2253
2254 (defslimefun frame-catch-tags-for-emacs (frame-index)
2255 (mapcar #'to-string (frame-catch-tags frame-index)))
2256
2257 (defslimefun sldb-disassemble (index)
2258 (with-output-to-string (*standard-output*)
2259 (disassemble-frame index)))
2260
2261 (defslimefun sldb-return-from-frame (index string)
2262 (let ((form (from-string string)))
2263 (to-string (multiple-value-list (return-from-frame index form)))))
2264
2265 (defslimefun sldb-break (name)
2266 (with-buffer-syntax ()
2267 (sldb-break-at-start (read-from-string name))))
2268
2269 (defslimefun sldb-step (frame)
2270 (cond ((find-restart 'continue)
2271 (activate-stepping frame)
2272 (setq *sldb-stepping-p* t)
2273 (continue))
2274 (t
2275 (error "No continue restart."))))
2276
2277
2278 ;;;; Compilation Commands.
2279
2280 (defvar *compiler-notes* '()
2281 "List of compiler notes for the last compilation unit.")
2282
2283 (defun clear-compiler-notes ()
2284 (setf *compiler-notes* '()))
2285
2286 (defun canonicalize-filename (filename)
2287 (namestring (truename filename)))
2288
2289 (defslimefun compiler-notes-for-emacs ()
2290 "Return the list of compiler notes for the last compilation unit."
2291 (reverse *compiler-notes*))
2292
2293 (defun measure-time-interval (fn)
2294 "Call FN and return the first return value and the elapsed time.
2295 The time is measured in microseconds."
2296 (declare (type function fn))
2297 (let ((before (get-internal-real-time)))
2298 (values
2299 (funcall fn)
2300 (* (- (get-internal-real-time) before)
2301 (/ 1000000 internal-time-units-per-second)))))
2302
2303 (defun record-note-for-condition (condition)
2304 "Record a note for a compiler-condition."
2305 (push (make-compiler-note condition) *compiler-notes*))
2306
2307 (defun make-compiler-note (condition)
2308 "Make a compiler note data structure from a compiler-condition."
2309 (declare (type compiler-condition condition))
2310 (list* :message (message condition)
2311 :severity (severity condition)
2312 :location (location condition)
2313 :references (references condition)
2314 (let ((s (short-message condition)))
2315 (if s (list :short-message s)))))
2316
2317 (defun swank-compiler (function)
2318 (clear-compiler-notes)
2319 (with-simple-restart (abort "Abort SLIME compilation.")
2320 (multiple-value-bind (result usecs)
2321 (handler-bind ((compiler-condition #'record-note-for-condition))
2322 (measure-time-interval function))
2323 (list (to-string result)
2324 (format nil "~,2F" (/ usecs 1000000.0))))))
2325
2326 (defslimefun compile-file-for-emacs (filename load-p &optional external-format)
2327 "Compile FILENAME and, when LOAD-P, load the result.
2328 Record compiler notes signalled as `compiler-condition's."
2329 (with-buffer-syntax ()
2330 (let ((*compile-print* nil))
2331 (swank-compiler (lambda () (swank-compile-file filename load-p
2332 external-format))))))
2333
2334 (defslimefun compile-string-for-emacs (string buffer position directory)
2335 "Compile STRING (exerpted from BUFFER at POSITION).
2336 Record compiler notes signalled as `compiler-condition's."
2337 (with-buffer-syntax ()
2338 (swank-compiler
2339 (lambda ()
2340 (let ((*compile-print* nil) (*compile-verbose* t))
2341 (swank-compile-string string :buffer buffer :position position
2342 :directory directory))))))
2343
2344 (defslimefun operate-on-system-for-emacs (system-name operation &rest keywords)
2345 "Compile and load SYSTEM using ASDF.
2346 Record compiler notes signalled as `compiler-condition's."
2347 (swank-compiler
2348 (lambda ()
2349 (apply #'operate-on-system system-name operation keywords))))
2350
2351 (defun asdf-central-registry ()
2352 (when (find-package :asdf)
2353 (symbol-value (find-symbol (string :*central-registry*) :asdf))))
2354
2355 (defslimefun list-all-systems-in-central-registry ()
2356 "Returns a list of all systems in ASDF's central registry."
2357 (delete-duplicates
2358 (loop for dir in (asdf-central-registry)
2359 for defaults = (eval dir)
2360 when defaults
2361 nconc (mapcar #'file-namestring
2362 (directory
2363 (make-pathname :defaults defaults
2364 :version :newest
2365 :type "asd"
2366 :name :wild
2367 :case :local))))
2368 :test #'string=))
2369
2370 (defun file-newer-p (new-file old-file)
2371 "Returns true if NEW-FILE is newer than OLD-FILE."
2372 (> (file-write-date new-file) (file-write-date old-file)))
2373
2374 (defun requires-compile-p (source-file)
2375 (let ((fasl-file (probe-file (compile-file-pathname source-file))))
2376 (or (not fasl-file)
2377 (file-newer-p source-file fasl-file))))
2378
2379 (defslimefun compile-file-if-needed (filename loadp)
2380 (cond ((requires-compile-p filename)
2381 (compile-file-for-emacs filename loadp))
2382 (loadp
2383 (load (compile-file-pathname filename))
2384 nil)))
2385
2386
2387 ;;;; Loading
2388
2389 (defslimefun load-file (filename)
2390 (to-string (load filename)))
2391
2392 (defslimefun load-file-set-package (filename &optional package)
2393 (load-file filename)
2394 (if package
2395 (set-package package)))
2396
2397
2398 ;;;; Macroexpansion
2399
2400 (defvar *macroexpand-printer-bindings*
2401 '((*print-circle* . nil)
2402 (*print-pretty* . t)
2403 (*print-escape* . t)
2404 (*print-level* . nil)
2405 (*print-length* . nil)))
2406
2407 (defun apply-macro-expander (expander string)
2408 (declare (type function expander))
2409 (with-buffer-syntax ()
2410 (with-bindings *macroexpand-printer-bindings*
2411 (prin1-to-string (funcall expander (from-string string))))))
2412
2413 (defslimefun swank-macroexpand-1 (string)
2414 (apply-macro-expander #'macroexpand-1 string))
2415
2416 (defslimefun swank-macroexpand (string)
2417 (apply-macro-expander #'macroexpand string))
2418
2419 (defslimefun swank-macroexpand-all (string)
2420 (apply-macro-expander #'macroexpand-all string))
2421
2422 (defslimefun disassemble-symbol (name)
2423 (with-buffer-syntax ()
2424 (with-output-to-string (*standard-output*)
2425 (let ((*print-readably* nil))
2426 (disassemble (fdefinition (from-string name)))))))
2427
2428
2429 ;;;; Basic completion
2430
2431 (defslimefun completions (string default-package-name)
2432 "Return a list of completions for a symbol designator STRING.
2433
2434 The result is the list (COMPLETION-SET
2435 COMPLETED-PREFIX). COMPLETION-SET is the list of all matching
2436 completions, and COMPLETED-PREFIX is the best (partial)
2437 completion of the input string.
2438
2439 If STRING is package qualified the result list will also be
2440 qualified. If string is non-qualified the result strings are
2441 also not qualified and are considered relative to
2442 DEFAULT-PACKAGE-NAME.
2443
2444 The way symbols are matched depends on the symbol designator's
2445 format. The cases are as follows:
2446 FOO - Symbols with matching prefix and accessible in the buffer package.
2447 PKG:FOO - Symbols with matching prefix and external in package PKG.
2448 PKG::FOO - Symbols with matching prefix and accessible in package PKG."
2449 (let ((completion-set (completion-set string default-package-name
2450 #'compound-prefix-match)))
2451 (list completion-set (longest-completion completion-set))))
2452
2453 (defslimefun simple-completions (string default-package-name)
2454 "Return a list of completions for a symbol designator STRING."
2455 (let ((completion-set (completion-set string default-package-name
2456 #'prefix-match-p)))
2457 (list completion-set (longest-common-prefix completion-set))))
2458
2459 ;;;;; Find completion set
2460
2461 (defun completion-set (string default-package-name matchp)
2462 "Return the set of completion-candidates as strings."
2463 (multiple-value-bind (name package-name package internal-p)
2464 (parse-completion-arguments string default-package-name)
2465 (let* ((symbols (and package
2466 (find-matching-symbols name
2467 package
2468 (and (not internal-p)
2469 package-name)
2470 matchp)))
2471 (packs (and (not package-name)
2472 (find-matching-packages name matchp)))
2473 (converter (output-case-converter name))
2474 (strings
2475 (mapcar converter
2476 (nconc (mapcar #'symbol-name symbols) packs))))
2477 (format-completion-set strings internal-p package-name))))
2478
2479 (defun find-matching-symbols (string package external test)
2480 "Return a list of symbols in PACKAGE matching STRING.
2481 TEST is called with two strings. If EXTERNAL is true, only external
2482 symbols are returned."
2483 (let ((completions '())
2484 (converter (output-case-converter string)))
2485 (flet ((symbol-matches-p (symbol)
2486 (and (or (not external)
2487 (symbol-external-p symbol package))
2488 (funcall test string
2489 (funcall converter (symbol-name symbol))))))
2490 (do-symbols (symbol package)
2491 (when (symbol-matches-p symbol)
2492 (push symbol completions))))
2493 (remove-duplicates completions)))
2494
2495 (defun symbol-external-p (symbol &optional (package (symbol-package symbol)))
2496 "True if SYMBOL is external in PACKAGE.
2497 If PACKAGE is not specified, the home package of SYMBOL is used."
2498 (and package
2499 (eq (nth-value 1 (find-symbol (symbol-name symbol) package))
2500 :external)))
2501
2502 (defun find-matching-packages (name matcher)
2503 "Return a list of package names matching NAME with MATCHER.
2504 MATCHER is a two-argument predicate."
2505 (let ((to-match (string-upcase name)))
2506 (remove-if-not (lambda (x) (funcall matcher to-match x))
2507 (mapcar (lambda (pkgname)
2508 (concatenate 'string pkgname ":"))
2509 (loop for package in (list-all-packages)
2510 collect (package-name package)
2511 append (package-nicknames package))))))
2512
2513 (defun parse-completion-arguments (string default-package-name)
2514 "Parse STRING as a symbol designator.
2515 Return these values:
2516 SYMBOL-NAME
2517 PACKAGE-NAME, or nil if the designator does not include an explicit package.
2518 PACKAGE, the package to complete in
2519 INTERNAL-P, if the symbol is qualified with `::'."
2520 (multiple-value-bind (name package-name internal-p)
2521 (tokenize-symbol string)
2522 (let ((package (carefully-find-package package-name default-package-name)))
2523 (values name package-name package internal-p))))
2524
2525 (defun carefully-find-package (name default-package-name)
2526 "Find the package with name NAME, or DEFAULT-PACKAGE-NAME, or the
2527 *buffer-package*. NAME and DEFAULT-PACKAGE-NAME can be nil."
2528 (let ((string (cond ((equal name "") "KEYWORD")
2529 (t (or name default-package-name)))))
2530 (if string
2531 (guess-package-from-string string nil)
2532 *buffer-package*)))
2533
2534 ;;;;; Format completion results
2535 ;;;
2536 ;;; We try to format results in the case as inputs. If you complete
2537 ;;; `FOO' then your result should include `FOOBAR' rather than
2538 ;;; `foobar'.
2539
2540 (defun format-completion-set (strings internal-p package-name)
2541 "Format a set of completion strings.
2542 Returns a list of completions with package qualifiers if needed."
2543 (mapcar (lambda (string)
2544 (format-completion-result string internal-p package-name))
2545 (sort strings #'string<)))
2546
2547 (defun format-completion-result (string internal-p package-name)
2548 (let ((prefix (cond (internal-p (format nil "~A::" package-name))
2549 (package-name (format nil "~A:" package-name))
2550 (t ""))))
2551 (values (concatenate 'string prefix string)
2552 (length prefix))))
2553
2554 (defun output-case-converter (input)
2555 "Return a function to case convert strings for output.
2556 INPUT is used to guess the preferred case."
2557 (ecase (readtable-case *readtable*)
2558 (:upcase (if (some #'lower-case-p input) #'string-downcase #'identity))
2559 (:invert (lambda (output)
2560 (multiple-value-bind (lower upper) (determine-case output)
2561 (cond ((and lower upper) output)
2562 (lower (string-upcase output))
2563 (upper (string-downcase output))
2564 (t output)))))
2565 (:downcase (if (some #'upper-case-p input) #'string-upcase #'identity))
2566 (:preserve #'identity)))
2567
2568 (defun determine-case (string)
2569 "Return two booleans LOWER and UPPER indicating whether STRING
2570 contains lower or upper case characters."
2571 (values (some #'lower-case-p string)
2572 (some #'upper-case-p string)))
2573
2574
2575 ;;;;; Compound-prefix matching
2576
2577 (defun compound-prefix-match (prefix target)
2578 "Return true if PREFIX is a compound-prefix of TARGET.
2579 Viewing each of PREFIX and TARGET as a series of substrings delimited
2580 by hyphens, if each substring of PREFIX is a prefix of the
2581 corresponding substring in TARGET then we call PREFIX a
2582 compound-prefix of TARGET.
2583
2584 Examples:
2585 \(compound-prefix-match \"foo\" \"foobar\") => t
2586 \(compound-prefix-match \"m--b\" \"multiple-value-bind\") => t
2587 \(compound-prefix-match \"m-v-c\" \"multiple-value-bind\") => NIL"
2588 (declare (type simple-string prefix target))
2589 (loop for ch across prefix
2590 with tpos = 0
2591 always (and (< tpos (length target))
2592 (if (char= ch #\-)
2593 (setf tpos (position #\- target :start tpos))
2594 (char= ch (aref target tpos))))
2595 do (incf tpos)))
2596
2597 (defun prefix-match-p (prefix string)
2598 "Return true if PREFIX is a prefix of STRING."
2599 (not (mismatch prefix string :end2 (min (length string) (length prefix)))))
2600
2601
2602 ;;;;; Extending the input string by completion
2603
2604 (defun longest-completion (completions)
2605 "Return the longest prefix for all COMPLETIONS.
2606 COMPLETIONS is a list of strings."
2607 (untokenize-completion
2608 (mapcar #'longest-common-prefix
2609 (transpose-lists (mapcar #'tokenize-completion completions)))))
2610
2611 (defun tokenize-completion (string)
2612 "Return all substrings of STRING delimited by #\-."
2613 (loop with end
2614 for start = 0 then (1+ end)
2615 until (> start (length string))
2616 do (setq end (or (position #\- string :start start) (length string)))
2617 collect (subseq string start end)))
2618
2619 (defun untokenize-completion (tokens)
2620 (format nil "~{~A~^-~}" tokens))
2621
2622 (defun longest-common-prefix (strings)
2623 "Return the longest string that is a common prefix of STRINGS."
2624 (if (null strings)
2625 ""
2626 (flet ((common-prefix (s1 s2)
2627 (let ((diff-pos (mismatch s1 s2)))
2628 (if diff-pos (subseq s1 0 diff-pos) s1))))
2629 (reduce #'common-prefix strings))))
2630
2631 (defun transpose-lists (lists)
2632 "Turn a list-of-lists on its side.
2633 If the rows are of unequal length, truncate uniformly to the shortest.
2634
2635 For example:
2636 \(transpose-lists '((ONE TWO THREE) (1 2)))
2637 => ((ONE 1) (TWO 2))"
2638 (cond ((null lists) '())
2639 ((some #'null lists) '())
2640 (t (cons (mapcar #'car lists)
2641 (transpose-lists (mapcar #'cdr lists))))))
2642
2643
2644 ;;;;; Completion Tests
2645
2646 (defpackage :swank-completion-test
2647 (:use))
2648
2649 (let ((*readtable* (copy-readtable *readtable*))
2650 (p (find-package :swank-completion-test)))
2651 (intern "foo" p)
2652 (intern "Foo" p)
2653 (intern "FOO" p)
2654 (setf (readtable-case *readtable*) :invert)
2655 (flet ((names (prefix)
2656 (sort (mapcar #'symbol-name
2657 (find-matching-symbols prefix p nil #'prefix-match-p))
2658 #'string<)))
2659 (assert (equal '("FOO") (names "f")))
2660 (assert (equal '("Foo" "foo") (names "F")))
2661 (assert (equal '("Foo") (names "Fo")))
2662 (assert (equal '("foo") (names "FO")))))
2663
2664 ;;;; Fuzzy completion
2665
2666 (defslimefun fuzzy-completions (string default-package-name &optional limit)
2667 "Return an (optionally limited to LIMIT best results) list of
2668 fuzzy completions for a symbol designator STRING. The list will
2669 be sorted by score, most likely match first.
2670
2671 The result is a list of completion objects, where a completion
2672 object is:
2673 (COMPLETED-STRING SCORE (&rest CHUNKS) FLAGS)
2674 where a CHUNK is a description of a matched string of characters:
2675 (OFFSET STRING)
2676 and FLAGS is a list of keywords describing properties of the symbol.
2677 For example, the top result for completing \"mvb\" in a package
2678 that uses COMMON-LISP would be something like:
2679 (\"multiple-value-bind\" 42.391666 ((0 \"mul\") (9 \"v\") (15 \"b\"))
2680 (:FBOUNDP :MACRO))
2681
2682 If STRING is package qualified the result list will also be
2683 qualified. If string is non-qualified the result strings are
2684 also not qualified and are considered relative to
2685 DEFAULT-PACKAGE-NAME.
2686
2687 Which symbols are candidates for matching depends on the symbol
2688 designator's format. The cases are as follows:
2689 FOO - Symbols accessible in the buffer package.
2690 PKG:FOO - Symbols external in package PKG.
2691 PKG::FOO - Symbols accessible in package PKG."
2692 (fuzzy-completion-set string default-package-name limit))
2693
2694 (defun convert-fuzzy-completion-result (result converter
2695 internal-p package-name)
2696 "Converts a result from the fuzzy completion core into
2697 something that emacs is expecting. Converts symbols to strings,
2698 fixes case issues, and adds information describing if the symbol
2699 is :bound, :fbound, a :class, a :macro, a :generic-function,
2700 a :special-operator, or a :package."
2701 (destructuring-bind (symbol-or-name score chunks) result
2702 (multiple-value-bind (name added-length)
2703 (format-completion-result
2704 (funcall converter
2705 (if (symbolp symbol-or-name)
2706 (symbol-name symbol-or-name)
2707 symbol-or-name))
2708 internal-p package-name)
2709 (list name score
2710 (mapcar
2711 #'(lambda (chunk)
2712 ;; fix up chunk positions to account for possible
2713 ;; added package identifier
2714 (list (+ added-length (first chunk))
2715 (second chunk)))
2716 chunks)
2717 (loop for flag in '(:boundp :fboundp :generic-function
2718 :class :macro :special-operator
2719 :package)
2720 if (if (symbolp symbol-or-name)
2721 (case flag
2722 (:boundp (boundp symbol-or-name))
2723 (:fboundp (fboundp symbol-or-name))
2724 (:class (find-class symbol-or-name nil))
2725 (:macro (macro-function symbol-or-name))
2726 (:special-operator
2727 (special-operator-p symbol-or-name))
2728 (:generic-function
2729 (typep (ignore-errors (fdefinition symbol-or-name))
2730 'generic-function)))
2731 (case flag
2732 (:package (stringp symbol-or-name)
2733 ;; KLUDGE: depends on internal
2734 ;; knowledge that packages are
2735 ;; brought up from the bowels of
2736 ;; the completion algorithm as
2737 ;; strings!
2738 )))
2739 collect flag)))))
2740
2741 (defun fuzzy-completion-set (string default-package-name &optional limit)
2742 "Prepares list of completion obajects, sorted by SCORE, of fuzzy
2743 completions of STRING in DEFAULT-PACKAGE-NAME. If LIMIT is set,
2744 only the top LIMIT results will be returned."
2745 (multiple-value-bind (name package-name package internal-p)
2746 (parse-completion-arguments string default-package-name)
2747 (let* ((symbols (and package
2748 (fuzzy-find-matching-symbols name
2749 package
2750 (and (not internal-p)
2751 package-name))))
2752 (packs (and (not package-name)
2753 (fuzzy-find-matching-packages name)))
2754 (converter (output-case-converter name))
2755 (results
2756 (sort (mapcar #'(lambda (result)
2757 (convert-fuzzy-completion-result
2758 result converter internal-p package-name))
2759 (nconc symbols packs))
2760 #'> :key #'second)))
2761 (when (and limit
2762 (> limit 0)
2763 (< limit (length results)))
2764 (setf (cdr (nthcdr (1- limit) results)) nil))
2765 results)))
2766
2767 (defun fuzzy-find-matching-symbols (string package external)
2768 "Return a list of symbols in PACKAGE matching STRING using the
2769 fuzzy completion algorithm. If EXTERNAL is true, only external
2770 symbols are returned."
2771 (let ((completions '())
2772 (converter (output-case-converter string)))
2773 (flet ((symbol-match (symbol)
2774 (and (or (not external)
2775 (symbol-external-p symbol package))
2776 (compute-highest-scoring-completion
2777 string (funcall converter (symbol-name symbol)) #'char=))))
2778 (do-symbols (symbol package)
2779 (if (string= "" string)
2780 (when (or (and external (symbol-external-p symbol package))
2781 (not external))
2782 (push (list symbol 0.0 (list (list 0 ""))) completions))
2783 (multiple-value-bind (result score) (symbol-match symbol)
2784 (when result
2785 (push (list symbol score result) completions))))))
2786 (remove-duplicates completions :key #'first)))
2787
2788 (defun fuzzy-find-matching-packages (name)
2789 "Return a list of package names matching NAME using the fuzzy
2790 completion algorithm."
2791 (let ((converter (output-case-converter name)))
2792 (loop for package in (list-all-packages)
2793 for package-name = (concatenate 'string
2794 (funcall converter
2795 (package-name package))
2796 ":")
2797 for (result score) = (multiple-value-list
2798 (compute-highest-scoring-completion
2799 name package-name #'char=))
2800 if result collect (list package-name score result))))
2801
2802 (defslimefun fuzzy-completion-selected (original-string completion)
2803 "This function is called by Slime when a fuzzy completion is
2804 selected by the user. It is for future expansion to make
2805 testing, say, a machine learning algorithm for completion scoring
2806 easier.
2807
2808 ORIGINAL-STRING is the string the user completed from, and
2809 COMPLETION is the completion object (see docstring for
2810 SWANK:FUZZY-COMPLETIONS) corresponding to the completion that the
2811 user selected."
2812 (declare (ignore original-string completion))
2813 nil)
2814
2815 ;;;;; Fuzzy completion core
2816
2817 (defparameter *fuzzy-recursion-soft-limit* 30
2818 "This is a soft limit for recursion in
2819 RECURSIVELY-COMPUTE-MOST-COMPLETIONS. Without this limit,
2820 completing a string such as \"ZZZZZZ\" with a symbol named
2821 \"ZZZZZZZZZZZZZZZZZZZZZZZ\" will result in explosive recursion to
2822 find all the ways it can match.
2823
2824 Most natural language searches and symbols do not have this
2825 problem -- this is only here as a safeguard.")
2826
2827 (defun compute-highest-scoring-completion (short full test)
2828 "Finds the highest scoring way to complete the abbreviation
2829 SHORT onto the string FULL, using TEST as a equality function for
2830 letters. Returns two values: The first being the completion
2831 chunks of the high scorer, and the second being the score."
2832 (let* ((scored-results
2833 (mapcar #'(lambda (result)
2834 (cons (score-completion result short full) result))
2835 (compute-most-completions short full test)))
2836 (winner (first (sort scored-results #'> :key #'first))))
2837 (values (rest winner) (first winner))))
2838
2839 (defun compute-most-completions (short full test)
2840 "Finds most possible ways to complete FULL with the letters in SHORT.
2841 Calls RECURSIVELY-COMPUTE-MOST-COMPLETIONS recursively. Returns
2842 a list of (&rest CHUNKS), where each CHUNKS is a description of
2843 how a completion matches."
2844 (let ((*all-chunks* nil))
2845 (declare (special *all-chunks*))
2846 (recursively-compute-most-completions short full test 0 0 nil nil nil t)
2847 *all-chunks*))
2848
2849 (defun recursively-compute-most-completions
2850 (short full test
2851 short-index initial-full-index
2852 chunks current-chunk current-chunk-pos
2853 recurse-p)
2854 "Recursively (if RECURSE-P is true) find /most/ possible ways
2855 to fuzzily map the letters in SHORT onto FULL, with TEST being a
2856 function to determine if two letters match.
2857
2858 A chunk is a list of elements that have matched consecutively.
2859 When consecutive matches stop, it is coerced into a string,
2860 paired with the starting position of the chunk, and pushed onto
2861 CHUNKS.
2862
2863 Whenever a letter matches, if RECURSE-P is true,
2864 RECURSIVELY-COMPUTE-MOST-COMPLETIONS calls itself with a position
2865 one index ahead, to find other possibly higher scoring
2866 possibilities. If there are less than
2867 *FUZZY-RECURSION-SOFT-LIMIT* results in *ALL-CHUNKS* currently,
2868 this call will also recurse.
2869
2870 Once a word has been completely matched, the chunks are pushed
2871 onto the special variable *ALL-CHUNKS* and the function returns."
2872 (declare (special *all-chunks*))
2873 (flet ((short-cur ()
2874 "Returns the next letter from the abbreviation, or NIL
2875 if all have been used."
2876 (if (= short-index (length short))
2877 nil
2878 (aref short short-index)))
2879 (add-to-chunk (char pos)
2880 "Adds the CHAR at POS in FULL to the current chunk,
2881 marking the start position if it is empty."
2882 (unless current-chunk
2883 (setf current-chunk-pos pos))
2884 (push char current-chunk))
2885 (collect-chunk ()
2886 "Collects the current chunk to CHUNKS and prepares for
2887 a new chunk."
2888 (when current-chunk
2889 (push (list current-chunk-pos
2890 (coerce (reverse current-chunk) 'string)) chunks)
2891 (setf current-chunk nil
2892 current-chunk-pos nil))))
2893 ;; If there's an outstanding chunk coming in collect it. Since
2894 ;; we're recursively called on skipping an input character, the
2895 ;; chunk can't possibly continue on.
2896 (when current-chunk (collect-chunk))
2897 (do ((pos initial-full-index (1+ pos)))
2898 ((= pos (length full)))
2899 (let ((cur-char (aref full pos)))
2900 (if (and (short-cur)
2901 (funcall test cur-char (short-cur)))
2902 (progn
2903 (when recurse-p
2904 ;; Try other possibilities, limiting insanely deep
2905 ;; recursion somewhat.
2906 (recursively-compute-most-completions
2907 short full test short-index (1+ pos)
2908 chunks current-chunk current-chunk-pos
2909 (not (> (length *all-chunks*)
2910 *fuzzy-recursion-soft-limit*))))
2911 (incf short-index)
2912 (add-to-chunk cur-char pos))
2913 (collect-chunk))))
2914 (collect-chunk)
2915 ;; If we've exhausted the short characters we have a match.
2916 (if (short-cur)
2917 nil
2918 (let ((rev-chunks (reverse chunks)))
2919 (push rev-chunks *all-chunks*)
2920 rev-chunks))))
2921
2922 ;;;;; Fuzzy completion scoring
2923
2924 (defparameter *fuzzy-completion-symbol-prefixes* "*+-%&?<"
2925 "Letters that are likely to be at the beginning of a symbol.
2926 Letters found after one of these prefixes will be scored as if
2927 they were at the beginning of ths symbol.")
2928 (defparameter *fuzzy-completion-symbol-suffixes* "*+->"
2929 "Letters that are likely to be at the end of a symbol.
2930 Letters found before one of these suffixes will be scored as if
2931 they were at the end of the symbol.")
2932 (defparameter *fuzzy-completion-word-separators* "-/."
2933 "Letters that separate different words in symbols. Letters
2934 after one of these symbols will be scores more highly than other
2935 letters.")
2936
2937 (defun score-completion (completion short full)
2938 "Scores the completion chunks COMPLETION as a completion from
2939 the abbreviation SHORT to the full string FULL. COMPLETION is a
2940 list like:
2941 ((0 \"mul\") (9 \"v\") (15 \"b\"))
2942 Which, if SHORT were \"mulvb\" and full were \"multiple-value-bind\",
2943 would indicate that it completed as such (completed letters
2944 capitalized):
2945 MULtiple-Value-Bind
2946
2947 Letters are given scores based on their position in the string.
2948 Letters at the beginning of a string or after a prefix letter at
2949 the beginning of a string are scored highest. Letters after a
2950 word separator such as #\- are scored next highest. Letters at
2951 the end of a string or before a suffix letter at the end of a
2952 string are scored medium, and letters anywhere else are scored
2953 low.
2954
2955 If a letter is directly after another matched letter, and its
2956 intrinsic value in that position is less than a percentage of the
2957 previous letter's value, it will use that percentage instead.
2958
2959 Finally, a small scaling factor is applied to favor shorter
2960 matches, all other things being equal."
2961 (labels ((at-beginning-p (pos)
2962 (= pos 0))
2963 (after-prefix-p (pos)
2964 (and (= pos 1)
2965 (find (aref full 0) *fuzzy-completion-symbol-prefixes*)))
2966 (word-separator-p (pos)
2967 (find (aref full pos) *fuzzy-completion-word-separators*))
2968 (after-word-separator-p (pos)
2969 (find (aref full (1- pos)) *fuzzy-completion-word-separators*))
2970 (at-end-p (pos)
2971 (= pos (1- (length full))))
2972 (before-suffix-p (pos)
2973 (and (= pos (- (length full) 2))
2974 (find (aref full (1- (length full)))
2975 *fuzzy-completion-symbol-suffixes*)))
2976 (score-or-percentage-of-previous (base-score pos chunk-pos)
2977 (if (zerop chunk-pos)
2978 base-score
2979 (max base-score
2980 (* (score-char (1- pos) (1- chunk-pos)) 0.85))))
2981 (score-char (pos chunk-pos)
2982 (score-or-percentage-of-previous
2983 (cond ((at-beginning-p pos) 10)
2984 ((after-prefix-p pos) 10)
2985 ((word-separator-p pos) 1)
2986 ((after-word-separator-p pos) 8)
2987 ((at-end-p pos) 6)
2988 ((before-suffix-p pos) 6)
2989 (t 1))
2990 pos chunk-pos))
2991 (score-chunk (chunk)
2992 (loop for chunk-pos below (length (second chunk))
2993 for pos from (first chunk)
2994 summing (score-char pos chunk-pos))))
2995 (let* ((chunk-scores (mapcar #'score-chunk completion))
2996 (length-score (/ 10.0 (1+ (- (length full) (length short))))))
2997 (values
2998 (+ (reduce #'+ chunk-scores) length-score)
2999 (list (mapcar #'list chunk-scores completion) length-score)))))
3000
3001 (defun highlight-completion (completion full)
3002 "Given a chunk definition COMPLETION and the string FULL,
3003 HIGHLIGHT-COMPLETION will create a string that demonstrates where
3004 the completion matched in the string. Matches will be
3005 capitalized, while the rest of the string will be lower-case."
3006 (let ((highlit (nstring-downcase (copy-seq full))))
3007 (dolist (chunk completion)
3008 (setf highlit (nstring-upcase highlit
3009 :start (first chunk)
3010 :end (+ (first chunk)
3011 (length (second chunk))))))
3012 highlit))
3013
3014 (defun format-fuzzy-completions (winners)
3015 "Given a list of completion objects such as on returned by
3016 FUZZY-COMPLETIONS, format the list into user-readable output."
3017 (let ((max-len
3018 (loop for winner in winners maximizing (length (first winner)))))
3019 (loop for (sym score result) in winners do
3020 (format t "~&~VA score ~8,2F ~A"
3021 max-len (highlight-completion result sym) score result))))
3022
3023
3024 ;;;; Documentation
3025
3026 (defslimefun apropos-list-for-emacs (name &optional external-only
3027 case-sensitive package)
3028 "Make an apropos search for Emacs.
3029 The result is a list of property lists."
3030 (let ((package (if package
3031 (or (find-package (string-to-package-designator package))
3032 (error "No such package: ~S" package)))))
3033 (mapcan (listify #'briefly-describe-symbol-for-emacs)
3034 (sort (remove-duplicates
3035 (apropos-symbols name external-only case-sensitive package))
3036 #'present-symbol-before-p))))
3037
3038 (defun string-to-package-designator (string)
3039 "Return a package designator made from STRING.
3040 Uses READ to case-convert STRING."
3041 (let ((*package* *swank-io-package*))
3042 (read-from-string string)))
3043
3044 (defun briefly-describe-symbol-for-emacs (symbol)
3045 "Return a property list describing SYMBOL.
3046 Like `describe-symbol-for-emacs' but with at most one line per item."
3047 (flet ((first-line (string)
3048 (let ((pos (position #\newline string)))
3049 (if (null pos) string (subseq string 0 pos)))))
3050 (let ((desc (map-if #'stringp #'first-line
3051 (describe-symbol-for-emacs symbol))))
3052 (if desc
3053 (list* :designator (to-string symbol) desc)))))
3054
3055 (defun map-if (test fn &rest lists)
3056 "Like (mapcar FN . LISTS) but only call FN on objects satisfying TEST.
3057 Example:
3058 \(map-if #'oddp #'- '(1 2 3 4 5)) => (-1 2 -3 4 -5)"
3059 (declare (type function test fn))
3060 (apply #'mapcar
3061 (lambda (x) (if (funcall test x) (funcall fn x) x))
3062 lists))
3063
3064 (defun listify (f)
3065 "Return a function like F, but which returns any non-null value
3066 wrapped in a list."
3067 (declare (type function f))
3068 (lambda (x)
3069 (let ((y (funcall f x)))
3070 (and y (list y)))))
3071
3072 (defun present-symbol-before-p (x y)
3073 "Return true if X belongs before Y in a printed summary of symbols.
3074 Sorted alphabetically by package name and then symbol name, except
3075 that symbols accessible in the current package go first."
3076 (declare (type symbol x y))
3077 (flet ((accessible (s)
3078 ;; Test breaks on NIL for package that does not inherit it
3079 (eq (find-symbol (symbol-name s) *buffer-package*) s)))
3080 (let ((ax (accessible x)) (ay (accessible y)))
3081 (cond ((and ax ay) (string< (symbol-name x) (symbol-name y)))
3082 (ax t)
3083 (ay nil)
3084 (t (let ((px (symbol-package x)) (py (symbol-package y)))
3085 (if (eq px py)
3086 (string< (symbol-name x) (symbol-name y))
3087 (string< (package-name px) (package-name py)))))))))
3088
3089 (let ((regex-hash (make-hash-table :test #'equal)))
3090 (defun compiled-regex (regex-string)
3091 (or (gethash regex-string regex-hash)
3092 (setf (gethash regex-string regex-hash)
3093 (if (zerop (length regex-string))
3094 (lambda (s) (check-type s string) t)
3095 (compile nil (slime-nregex:regex-compile regex-string)))))))
3096
3097 (defun apropos-matcher (string case-sensitive package external-only)
3098 (let* ((case-modifier (if case-sensitive #'string #'string-upcase))
3099 (regex (compiled-regex (funcall case-modifier string))))
3100 (lambda (symbol)
3101 (and (not (keywordp symbol))
3102 (if package (eq (symbol-package symbol) package) t)
3103 (if external-only (symbol-external-p symbol) t)
3104 (funcall regex (funcall case-modifier symbol))))))
3105
3106 (defun apropos-symbols (string external-only case-sensitive package)
3107 (let ((result '())
3108 (matchp (apropos-matcher string case-sensitive package external-only)))
3109 (with-package-iterator (next (or package (list-all-packages))
3110 :external :internal)
3111 (loop
3112 (multiple-value-bind (morep symbol) (next)
3113 (cond ((not morep)
3114 (return))
3115 ((funcall matchp symbol)
3116 (push symbol result))))))
3117 result))
3118
3119 (defun call-with-describe-settings (fn)
3120 (let ((*print-readably* nil))
3121 (funcall fn)))
3122
3123 (defmacro with-describe-settings ((&rest _) &body body)
3124 (declare (ignore _))
3125 `(call-with-describe-settings (lambda () ,@body)))
3126
3127 (defun describe-to-string (object)
3128 (with-describe-settings ()
3129 (with-output-to-string (*standard-output*)
3130 (describe object))))
3131
3132 (defslimefun describe-symbol (symbol-name)
3133 (with-buffer-syntax ()
3134 (describe-to-string (parse-symbol-or-lose symbol-name))))
3135
3136 (defslimefun describe-function (name)
3137 (with-buffer-syntax ()
3138 (let ((symbol (parse-symbol-or-lose name)))
3139 (describe-to-string (or (macro-function symbol)
3140 (symbol-function symbol))))))
3141
3142 (defslimefun describe-definition-for-emacs (name kind)
3143 (with-buffer-syntax ()
3144 (with-describe-settings ()
3145 (with-output-to-string (*standard-output*)
3146 (describe-definition (parse-symbol-or-lose name) kind)))))
3147
3148 (defslimefun documentation-symbol (symbol-name &optional default)
3149 (with-buffer-syntax ()
3150 (multiple-value-bind (sym foundp) (parse-symbol symbol-name)
3151 (if foundp
3152 (let ((vdoc (documentation sym 'variable))
3153 (fdoc (documentation sym 'function)))
3154 (or (and (or vdoc fdoc)
3155 (concatenate 'string
3156 fdoc
3157 (and vdoc fdoc '(#\Newline #\Newline))
3158 vdoc))
3159 default))
3160 default))))
3161
3162
3163 ;;;; Package Commands
3164
3165 (defslimefun list-all-package-names (&optional include-nicknames)
3166 "Return a list of all package names.
3167 Include the nicknames if INCLUDE-NICKNAMES is true."
3168 (loop for package in (list-all-packages)
3169 collect (package-name package)
3170 when include-nicknames append (package-nicknames package)))
3171
3172
3173 ;;;; Tracing
3174
3175 ;; Use eval for the sake of portability...
3176 (defun tracedp (fspec)
3177 (member fspec (eval '(trace))))
3178
3179 (defslimefun swank-toggle-trace (spec-string)
3180 (let ((spec (from-string spec-string)))
3181 (cond ((consp spec) ; handle complicated cases in the backend
3182 (toggle-trace spec))
3183 ((tracedp spec)
3184 (eval `(untrace ,spec))
3185 (format nil "~S is now untraced." spec))
3186 (t
3187 (eval `(trace ,spec))
3188 (format nil "~S is now traced." spec)))))
3189
3190 (defslimefun untrace-all ()
3191 (untrace))
3192
3193
3194 ;;;; Undefing
3195
3196 (defslimefun undefine-function (fname-string)
3197 (let ((fname (from-string fname-string)))
3198 (format nil "~S" (fmakunbound fname))))
3199
3200
3201 ;;;; Profiling
3202
3203 (defun profiledp (fspec)
3204 (member fspec (profiled-functions)))
3205
3206 (defslimefun toggle-profile-fdefinition (fname-string)
3207 (let ((fname (from-string fname-string)))
3208 (cond ((profiledp fname)
3209 (unprofile fname)
3210 (format nil "~S is now unprofiled." fname))
3211 (t
3212 (profile fname)
3213 (format nil "~S is now profiled." fname)))))
3214
3215
3216 ;;;; Source Locations
3217
3218 (defslimefun find-definitions-for-emacs (name)
3219 "Return a list ((DSPEC LOCATION) ...) of definitions for NAME.
3220 DSPEC is a string and LOCATION a source location. NAME is a string."
3221 (multiple-value-bind (sexp error)
3222 (ignore-errors (values (from-string name)))
3223 (cond (error '())
3224 (t (loop for (dspec loc) in (find-definitions sexp)
3225 collect (list (to-string dspec) loc))))))
3226
3227 (defun alistify (list key test)
3228 "Partition the elements of LIST into an alist. KEY extracts the key
3229 from an element and TEST is used to compare keys."
3230 (declare (type function key))
3231 (let ((alist '()))
3232 (dolist (e list)
3233 (let* ((k (funcall key e))
3234 (probe (assoc k alist :test test)))
3235 (if probe
3236 (push e (cdr probe))
3237 (push (cons k (list e)) alist))))
3238 alist))
3239
3240 (defun location-position< (pos1 pos2)
3241 (cond ((and (position-p pos1) (position-p pos2))
3242 (< (position-pos pos1)
3243 (position-pos pos2)))
3244 (t nil)))
3245
3246 (defun partition (list test key)
3247 (declare (type function test key))
3248 (loop for e in list
3249 if (funcall test (funcall key e)) collect e into yes
3250 else collect e into no
3251 finally (return (values yes no))))
3252
3253 (defstruct (xref (:conc-name xref.)
3254 (:type list))
3255 dspec location)
3256
3257 (defun location-valid-p (location)
3258 (eq (car location) :location))
3259
3260 (defun xref-buffer (xref)
3261 (location-buffer (xref.location xref)))
3262
3263 (defun xref-position (xref)
3264 (location-buffer (xref.location xref)))
3265
3266 (defun group-xrefs (xrefs)
3267 "Group XREFS, a list of the form ((DSPEC LOCATION) ...) by location.
3268 The result is a list of the form ((LOCATION . ((DSPEC . LOCATION) ...)) ...)."
3269 (multiple-value-bind (resolved errors)
3270 (partition xrefs #'location-valid-p #'xref.location)
3271 (let ((alist (alistify resolved #'xref-buffer #'equal)))
3272 (append
3273 (loop for (buffer . list) in alist
3274 collect (cons (second buffer)
3275 (mapcar (lambda (xref)
3276 (cons (to-string (xref.dspec xref))
3277 (xref.location xref)))
3278 (sort list #'location-position<
3279 :key #'xref-position))))
3280 (if errors
3281 (list (cons "Unresolved"
3282 (mapcar (lambda (xref)
3283 (cons (to-string (xref.dspec xref))
3284 (xref.location xref)))
3285 errors))))))))
3286
3287 (defslimefun xref (type symbol-name)
3288 (let ((symbol (parse-symbol-or-lose symbol-name *buffer-package*)))
3289 (group-xrefs
3290 (ecase type
3291 (:calls (who-calls symbol))
3292 (:calls-who (calls-who symbol))
3293 (:references (who-references symbol))
3294 (:binds (who-binds symbol))
3295 (:sets (who-sets symbol))
3296 (:macroexpands (who-macroexpands symbol))
3297 (:specializes (who-specializes symbol))
3298 (:callers (list-callers symbol))
3299 (:callees (list-callees symbol))))))
3300
3301
3302 ;;;; Inspecting
3303
3304 (defun common-seperated-spec (list &optional (callback (lambda (v)
3305 `(:value ,v))))
3306 (butlast
3307 (loop
3308 for i in list
3309 collect (funcall callback i)
3310 collect ", ")))
3311
3312 (defun inspector-princ (list)
3313 "Like princ-to-string, but don't rewrite (function foo) as #'foo.
3314 Do NOT pass circular lists to this function."
3315 (let ((*print-pprint-dispatch* (copy-pprint-dispatch)))
3316 (set-pprint-dispatch '(cons (member function)) nil)
3317 (princ-to-string list)))
3318
3319 (defmethod inspect-for-emacs ((object cons) inspector)
3320 (declare (ignore inspector))
3321 (if (consp (cdr object))
3322 (inspect-for-emacs-list object)
3323 (inspect-for-emacs-simple-cons object)))
3324
3325 (defun inspect-for-emacs-simple-cons (cons)
3326 (values "A cons cell."
3327 (label-value-line*
3328 ('car (car cons))
3329 ('cdr (cdr cons)))))
3330
3331 (defun inspect-for-emacs-list (list)
3332 (let ((maxlen 40))
3333 (multiple-value-bind (length tail) (safe-length list)
3334 (flet ((frob (title list)
3335 (let ((lines
3336 (do ((i 0 (1+ i))
3337 (l list (cdr l))
3338 (a '() (cons (label-value-line i (car l)) a)))
3339 ((not (consp l))
3340 (let ((a (if (null l)
3341 a
3342 (cons (label-value-line :tail l) a))))
3343 (reduce #'append (reverse a) :from-end t))))))
3344 (values title (append '("Elements:" (:newline)) lines)))))
3345
3346 (cond ((not length) ; circular
3347 (frob "A circular list."
3348 (cons (car list)
3349 (ldiff (cdr list) list))))
3350 ((and (<= length maxlen) (not tail))
3351 (frob "A proper list." list))
3352 (tail
3353 (frob "An improper list." list))
3354 (t
3355 (frob "A proper list." list)))))))
3356
3357 ;; (inspect-for-emacs-list '#1=(a #1# . #1# ))
3358
3359 (defun safe-length (list)
3360 "Similar to `list-length', but avoid errors on improper lists.
3361 Return two values: the length of the list and the last cdr.
3362 NIL is returned if the list is circular."
3363 (do ((n 0 (+ n 2)) ;Counter.
3364 (fast list (cddr fast)) ;Fast pointer: leaps by 2.
3365 (slow list (cdr slow))) ;Slow pointer: leaps by 1.
3366 (nil)
3367 (cond ((null fast) (return (values n nil)))
3368 ((not (consp fast)) (return (values n fast)))
3369 ((null (cdr fast)) (return (values (1+ n) (cdr fast))))
3370 ((and (eq fast slow) (> n 0)) (return nil))
3371 ((not (consp (cdr fast))) (return (values (1+ n) (cdr fast)))))))
3372
3373 (defvar *slime-inspect-contents-limit* nil "How many elements of
3374 a hash table or array to show by default. If table has more than
3375 this then offer actions to view more. Set to nil for no limit." )
3376
3377 (defmethod inspect-for-emacs ((ht hash-table) inspector)
3378 (declare (ignore inspector))
3379 (values (prin1-to-string ht)
3380 (append
3381 (label-value-line*
3382 ("Count" (hash-table-count ht))
3383 ("Size" (hash-table-size ht))
3384 ("Test" (hash-table-test ht))
3385 ("Rehash size" (hash-table-rehash-size ht))
3386 ("Rehash threshold" (hash-table-rehash-threshold ht)))
3387 '("Contents: " (:newline))
3388 (if (and *slime-inspect-contents-limit*
3389 (>= (hash-table-count ht) *slime-inspect-contents-limit*))
3390 (inspect-bigger-piece-actions ht (hash-table-count ht))
3391 nil)
3392 (loop for key being the hash-keys of ht
3393 for value being the hash-values of ht
3394 repeat (or *slime-inspect-contents-limit* most-positive-fixnum)
3395 append `((:value ,key) " = " (:value ,value) (:newline))
3396 )
3397
3398 )))
3399
3400 (defmethod inspect-bigger-piece-actions (thing size)
3401 (append
3402 (if (> size *slime-inspect-contents-limit*)
3403 (list (inspect-show-more-action thing)
3404 '(:newline))
3405 nil)
3406 (list (inspect-whole-thing-action thing size)
3407 '(:newline))))
3408
3409 (defmethod inspect-whole-thing-action (thing size)
3410 `(:action ,(format nil "Inspect all ~a elements."
3411 size)
3412 ,(lambda()
3413 (let ((*slime-inspect-contents-limit* nil))
3414 (values
3415 (swank::inspect-object thing)
3416 :replace)
3417 ))))
3418
3419 (defmethod inspect-show-more-action (thing)
3420 `(:action ,(format nil "~a elements shown. Prompt for how many to inspect..."
3421 *slime-inspect-contents-limit* )
3422 ,(lambda()
3423 (let ((*slime-inspect-contents-limit*
3424 (progn (format t "How many elements should be shown? ") (read))))
3425 (values
3426 (swank::inspect-object thing)
3427 :replace)
3428 ))
3429 ))
3430
3431 (defmethod inspect-for-emacs ((array array) inspector)
3432 (declare (ignore inspector))
3433 (values "An array."
3434 (append
3435 (label-value-line*
3436 ("Dimensions" (array-dimensions array))
3437 ("Its element type is" (array-element-type array))
3438 ("Total size" (array-total-size array))
3439 ("Adjustable" (adjustable-array-p array)))
3440 (when (array-has-fill-pointer-p array)
3441 (label-value-line "Fill pointer" (fill-pointer array)))
3442 '("Contents:" (:newline))
3443 (if (and *slime-inspect-contents-limit*
3444 (>= (array-total-size array) *slime-inspect-contents-limit*))
3445 (inspect-bigger-piece-actions array (length array))
3446 nil)
3447 (loop for i below (or *slime-inspect-contents-limit* (array-total-size array))
3448 append (label-value-line i (row-major-aref array i))))))
3449
3450 (defmethod inspect-for-emacs ((char character) inspector)
3451 (declare (ignore inspector))
3452 (values "A character."
3453 (append
3454 (label-value-line*
3455 ("Char code" (char-code char))
3456 ("Lower cased" (char-downcase char))
3457 ("Upper cased" (char-upcase char)))
3458 (if (get-macro-character char)
3459 `("In the current readtable ("
3460 (:value ,*readtable*) ") it is a macro character: "
3461 (:value ,(get-macro-character char)))))))
3462
3463 (defun docstring-ispec (label object kind)
3464 "Return a inspector spec if OBJECT has a docstring of of kind KIND."
3465 (let ((docstring (documentation object kind)))
3466 (cond ((not docstring) nil)
3467 ((< (+ (length label) (length docstring))
3468 75)
3469 (list label ": " docstring '(:newline)))
3470 (t
3471 (list label ": " '(:newline) " " docstring '(:newline))))))
3472
3473 (defmethod inspect-for-emacs ((symbol symbol) inspector)
3474 (declare (ignore inspector))
3475 (let ((package (symbol-package symbol)))
3476 (multiple-value-bind (_symbol status)
3477 (and package (find-symbol (string symbol) package))
3478 (declare (ignore _symbol))
3479 (values
3480 "A symbol."
3481 (append
3482 (label-value-line "Its name is" (symbol-name symbol))
3483 ;;
3484 ;; Value
3485 (cond ((boundp symbol)
3486 (label-value-line (if (constantp symbol)
3487 "It is a constant of value"
3488 "It is a global variable bound to")
3489 (symbol-value symbol)))
3490 (t '("It is unbound." (:newline))))
3491 (docstring-ispec "Documentation" symbol 'variable)
3492 (multiple-value-bind (expansion definedp) (macroexpand symbol)
3493 (if definedp
3494 (label-value-line "It is a symbol macro with expansion"
3495 expansion)))
3496 ;;
3497 ;; Function
3498 (if (fboundp symbol)
3499 (append (if (macro-function symbol)
3500 `("It a macro with macro-function: "
3501 (:value ,(macro-function symbol)))
3502 `("It is a function: "
3503 (:value ,(symbol-function symbol))))
3504 `(" " (:action "[make funbound]"
3505 ,(lambda () (fmakunbound symbol))))
3506 `((:newline)))
3507 `("It has no function value." (:newline)))
3508 (docstring-ispec "Function Documentation" symbol 'function)
3509 (if (compiler-macro-function symbol)
3510 (label-value-line "It also names the compiler macro"
3511 (compiler-macro-function symbol)))
3512 (docstring-ispec "Compiler Macro Documentation"
3513 symbol 'compiler-macro)
3514 ;;
3515 ;; Package
3516 (if package
3517 `("It is " ,(string-downcase (string status))
3518 " to the package: "
3519 (:value ,package ,(package-name package))
3520 ,@(if (eq :internal status)
3521 `(" "
3522 (:action "[export it]"
3523 ,(lambda () (export symbol package)))))
3524 " "
3525 (:action "[unintern it]"
3526 ,(lambda () (unintern symbol package)))
3527 (:newline))
3528 '("It is a non-interned symbol." (:newline)))
3529 ;;
3530 ;; Plist
3531 (label-value-line "Property list" (symbol-plist symbol))
3532 ;;
3533 ;; Class
3534 (if (find-class symbol nil)
3535 `("It names the class "
3536 (:value ,(find-class symbol) ,(string symbol))
3537 " "
3538 (:action "[remove]"
3539 ,(lambda () (setf (find-class symbol) nil)))
3540 (:newline)))
3541 ;;
3542 ;; More package
3543 (if (find-package symbol)
3544 (label-value-line "It names the package" (find-package symbol)))
3545 )))))
3546
3547 (defmethod inspect-for-emacs ((f function) inspector)
3548 (declare (ignore inspector))
3549 (values "A function."
3550 (append
3551 (label-value-line "Name" (function-name f))
3552 `("Its argument list is: "
3553 ,(inspector-princ (arglist f)) (:newline))
3554 (docstring-ispec "Documentation" f t)
3555 (if (function-lambda-expression f)
3556 (label-value-line "Lambda Expression"
3557 (function-lambda-expression f))))))
3558
3559 (defun method-specializers-for-inspect (method)
3560 "Return a \"pretty\" list of the method's specializers. Normal
3561 specializers are replaced by the name of the class, eql
3562 specializers are replaced by `(eql ,object)."
3563 (mapcar (lambda (spec)
3564 (typecase spec
3565 (swank-mop:eql-specializer
3566 `(eql ,(swank-mop:eql-specializer-object spec)))
3567 (t (swank-mop:class-name spec))))
3568 (swank-mop:method-specializers method)))
3569
3570 (defun method-for-inspect-value (method)
3571 "Returns a \"pretty\" list describing METHOD. The first element
3572 of the list is the name of generic-function method is
3573 specialiazed on, the second element is the method qualifiers,
3574 the rest of the list is the method's specialiazers (as per
3575 method-specializers-for-inspect)."
3576 (append (list (swank-mop:generic-function-name
3577 (swank-mop:method-generic-function method)))
3578 (swank-mop:method-qualifiers method)
3579 (method-specializers-for-inspect method)))
3580
3581 (defmethod inspect-for-emacs ((o standard-object) inspector)
3582 (declare (ignore inspector))
3583 (values "An object."
3584 `("Class: " (:value ,(class-of o))
3585 (:newline)
3586 "Slots:" (:newline)
3587 ,@(loop
3588 with direct-slots = (swank-mop:class-direct-slots (class-of o))
3589 for slot in (swank-mop:class-slots (class-of o))
3590 for slot-def = (or (find-if (lambda (a)
3591 ;; find the direct slot
3592 ;; with the same name
3593 ;; as SLOT (an
3594 ;; effective slot).
3595 (eql (swank-mop:slot-definition-name a)
3596 (swank-mop:slot-definition-name slot)))
3597 direct-slots)
3598 slot)
3599 collect `(:value ,slot-def ,(inspector-princ (swank-mop:slot-definition-name slot-def)))
3600 collect " = "
3601 if (slot-boundp o (swank-mop:slot-definition-name slot-def))
3602 collect `(:value ,(slot-value o (swank-mop:slot-definition-name slot-def)))
3603 else
3604 collect "#<unbound>"
3605 collect '(:newline)))))
3606
3607 (defvar *gf-method-getter* 'methods-by-applicability
3608 "This function is called to get the methods of a generic function.
3609 The default returns the method sorted by applicability.
3610 See `methods-by-applicability'.")
3611
3612 (defun specializer< (specializer1 specializer2)
3613 "Return true if SPECIALIZER1 is more specific than SPECIALIZER2."
3614 (let ((s1 specializer1) (s2 specializer2) )
3615 (cond ((typep s1 'swank-mop:eql-specializer)
3616 (not (typep s2 'swank-mop:eql-specializer)))
3617 (t
3618 (flet ((cpl (class)
3619 (and (swank-mop:class-finalized-p class)
3620 (swank-mop:class-precedence-list class))))
3621 (member s2 (cpl s1)))))))
3622
3623 (defun methods-by-applicability (gf)
3624 "Return methods ordered by most specific argument types.
3625
3626 `method-specializer<' is used for sorting."
3627 ;; FIXME: argument-precedence-order and qualifiers are ignored.
3628 (let ((methods (copy-list (swank-mop:generic-function-methods gf))))
3629 (labels ((method< (meth1 meth2)
3630 (loop for s1 in (swank-mop:method-specializers meth1)
3631 for s2 in (swank-mop:method-specializers meth2)
3632 do (cond ((specializer< s2 s1) (return nil))
3633 ((specializer< s1 s2) (return t))))))
3634 (stable-sort methods #'method<))))
3635
3636 (defun abbrev-doc (doc &optional (maxlen 80))
3637 "Return the first sentence of DOC, but not more than MAXLAN characters."
3638 (subseq doc 0 (min (1+ (or (position #\. doc) (1- maxlen)))
3639 maxlen
3640 (length doc))))
3641
3642 (defmethod inspect-for-emacs ((gf standard-generic-function) inspector)
3643 (declare (ignore inspector))
3644 (flet ((lv (label value) (label-value-line label value)))
3645 (values
3646 "A generic function."
3647 (append
3648 (lv "Name" (swank-mop:generic-function-name gf))
3649 (lv "Arguments" (swank-mop:generic-function-lambda-list gf))
3650 (docstring-ispec "Documentation" gf t)
3651 (lv "Method class" (swank-mop:generic-function-method-class gf))
3652 (lv "Method combination"
3653 (swank-mop:generic-function-method-combination gf))
3654 `("Methods: " (:newline))
3655 (loop for method in (funcall *gf-method-getter* gf) append
3656 `((:value ,method ,(inspector-princ
3657 ;; drop the name of the GF
3658 (cdr (method-for-inspect-value method))))
3659 " "
3660 (:action "[remove method]"
3661 ,(let ((m method)) ; LOOP reassigns method
3662 (lambda ()
3663 (remove-method gf m))))
3664 (:newline)))))))
3665
3666 (defmethod inspect-for-emacs ((method standard-method) inspector)
3667 (declare (ignore inspector))
3668 (values "A method."
3669 `("Method defined on the generic function "
3670 (:value ,(swank-mop:method-generic-function method)
3671 ,(inspector-princ
3672 (swank-mop:generic-function-name
3673 (swank-mop:method-generic-function method))))
3674 (:newline)
3675 ,@(docstring-ispec "Documentation" method t)
3676 "Lambda List: " (:value ,(swank-mop:method-lambda-list method))
3677 (:newline)
3678 "Specializers: " (:value ,(swank-mop:method-specializers method)
3679 ,(inspector-princ (method-specializers-for-inspect method)))
3680 (:newline)
3681 "Qualifiers: " (:value ,(swank-mop:method-qualifiers method))
3682 (:newline)
3683 "Method function: " (:value ,(swank-mop:method-function method)))))
3684
3685 (defmethod inspect-for-emacs ((class standard-class) inspector)
3686 (declare (ignore inspector))
3687 (values "A class."
3688 `("Name: " (:value ,(class-name class))
3689 (:newline)
3690 "Super classes: "
3691 ,@(common-seperated-spec (swank-mop:class-direct-superclasses class))
3692 (:newline)
3693 "Direct Slots: "
3694 ,@(common-seperated-spec
3695 (swank-mop:class-direct-slots class)
3696 (lambda (slot)
3697 `(:value ,slot ,(inspector-princ (swank-mop:slot-definition-name slot)))))
3698 (:newline)
3699 "Effective Slots: "
3700 ,@(if (swank-mop:class-finalized-p class)
3701 (common-seperated-spec
3702 (swank-mop:class-slots class)
3703 (lambda (slot)
3704 `(:value ,slot ,(inspector-princ
3705 (swank-mop:slot-definition-name slot)))))
3706 '("#<N/A (class not finalized)>"))
3707 (:newline)
3708 ,@(when (documentation class t)
3709 `("Documentation:" (:newline) ,(documentation class t) (:newline)))
3710 "Sub classes: "
3711 ,@(common-seperated-spec (swank-mop:class-direct-subclasses class)
3712 (lambda (sub)
3713 `(:value ,sub ,(inspector-princ (class-name sub)))))
3714 (:newline)
3715 "Precedence List: "
3716 ,@(if (swank-mop:class-finalized-p class)
3717 (common-seperated-spec (swank-mop:class-precedence-list class)
3718 (lambda (class)
3719 `(:value ,class ,(inspector-princ (class-name class)))))
3720 '("#<N/A (class not finalized)>"))
3721 (:newline)
3722 ,@(when (swank-mop:specializer-direct-methods class)
3723 `("It is used as a direct specializer in the following methods:" (:newline)
3724 ,@(loop
3725 for method in (sort (copy-list (swank-mop:specializer-direct-methods class))
3726 #'string< :key (lambda (x)
3727 (symbol-name
3728 (let ((name (swank-mop::generic-function-name
3729 (swank-mop::method-generic-function x))))
3730 (if (symbolp name) name (second name))))))
3731 collect " "
3732 collect `(:value ,method ,(inspector-princ (method-for-inspect-value method)))
3733 collect '(:newline)
3734 if (documentation method t)
3735 collect " Documentation: " and
3736 collect (abbrev-doc (documentation method t)) and
3737 collect '(:newline))))
3738 "Prototype: " ,(if (swank-mop:class-finalized-p class)
3739 `(:value ,(swank-mop:class-prototype class))
3740 '"#<N/A (class not finalized)>")
3741 (:newline)
3742 "------------------------------" (:newline)
3743 "All Slots:" (:newline)
3744 ,@(loop
3745 with direct-slots = (swank-mop:class-direct-slots (class-of class))
3746 for slot in (swank-mop:class-slots (class-of class))
3747 for slot-def = (or (find-if (lambda (a)
3748 ;; find the direct slot
3749 ;; with the same name
3750 ;; as SLOT (an
3751 ;; effective slot).
3752 (eql (swank-mop:slot-definition-name a)
3753 (swank-mop:slot-definition-name slot)))
3754 direct-slots)
3755 slot)
3756 collect `(:value ,slot-def ,(inspector-princ (swank-mop:slot-definition-name slot-def)))
3757 collect " = "
3758 if (slot-boundp class (swank-mop:slot-definition-name slot-def))
3759 collect `(:value ,(slot-value class (swank-mop:slot-definition-name slot-def)))
3760 else
3761 collect "#<unbound>"
3762 collect '(:newline)))))
3763
3764 (defmethod inspect-for-emacs ((slot swank-mop:standard-slot-definition) inspector)
3765 (declare (ignore inspector))
3766 (values "A slot."
3767 `("Name: " (:value ,(swank-mop:slot-definition-name slot))
3768 (:newline)
3769 ,@(when (swank-mop:slot-definition-documentation slot)
3770 `("Documentation:" (:newline)
3771 (:value ,(swank-mop:slot-definition-documentation slot))
3772 (:newline)))
3773 "Init args: " (:value ,(swank-mop:slot-definition-initargs slot)) (:newline)
3774 "Init form: " ,(if (swank-mop:slot-definition-initfunction slot)
3775 `(:value ,(swank-mop:slot-definition-initform slot))
3776 "#<unspecified>") (:newline)
3777 "Init function: " (:value ,(swank-mop:slot-definition-initfunction slot))
3778 (:newline))))
3779
3780 (defmethod inspect-for-emacs ((package package) inspector)
3781 (declare (ignore inspector))
3782 (let ((internal-symbols '())
3783 (external-symbols '()))
3784 (do-symbols (sym package)
3785 (when (eq package (symbol-package sym))
3786 (push sym internal-symbols)
3787 (multiple-value-bind (symbol status)
3788 (find-symbol (symbol-name sym) package)
3789 (declare (ignore symbol))
3790 (when (eql :external status)
3791 (push sym external-symbols)))))
3792 (setf internal-symbols (sort internal-symbols #'string-lessp)
3793 external-symbols (sort external-symbols #'string-lessp))
3794 (values "A package."
3795 `("Name: " (:value ,(package-name package))
3796 (:newline)
3797 "Nick names: " ,@(common-seperated-spec (sort (package-nicknames package) #'string-lessp))
3798 (:newline)
3799 ,@(when (documentation package t)
3800 `("Documentation:" (:newline)
3801 ,(documentation package t) (:newline)))
3802 "Use list: " ,@(common-seperated-spec (sort (package-use-list package) #'string-lessp :key #'package-name)
3803 (lambda (pack)
3804 `(:value ,pack ,(inspector-princ (package-name pack)))))
3805 (:newline)
3806 "Used by list: " ,@(common-seperated-spec (sort (package-used-by-list package) #'string-lessp :key #'package-name)
3807 (lambda (pack)
3808 `(:value ,pack ,(inspector-princ (package-name pack)))))
3809 (:newline)
3810 ,(if (null external-symbols)
3811 "0 external symbols."
3812 `(:value ,external-symbols ,(format nil "~D external symbol~:P." (length external-symbols))))
3813 (:newline)
3814 ,(if (null internal-symbols)
3815 "0 internal symbols."
3816 `(:value ,internal-symbols ,(format nil "~D internal symbol~:P." (length internal-symbols))))
3817 (:newline)
3818 ,(if (null (package-shadowing-symbols package))
3819 "0 shadowed symbols."
3820 `(:value ,(package-shadowing-symbols package)
3821 ,(format nil "~D shadowed symbol~:P." (length (package-shadowing-symbols package)))))))))
3822
3823 (defmethod inspect-for-emacs ((pathname pathname) inspector)
3824 (declare (ignore inspector))
3825 (values (if (wild-pathname-p pathname)
3826 "A wild pathname."
3827 "A pathname.")
3828 (append (label-value-line*
3829 ("Namestring" (namestring pathname))
3830 ("Host" (pathname-host pathname))
3831 ("Device" (pathname-device pathname))
3832 ("Directory" (pathname-directory pathname))
3833 ("Name" (pathname-name pathname))
3834 ("Type" (pathname-type pathname))
3835 ("Version" (pathname-version pathname)))
3836 (unless (or (wild-pathname-p pathname)
3837 (not (probe-file pathname)))
3838 (label-value-line "Truename" (truename pathname))))))
3839
3840 (defmethod inspect-for-emacs ((pathname logical-pathname) inspector)
3841 (declare (ignore inspector))
3842 (values "A logical pathname."
3843 (append
3844 (label-value-line*
3845 ("Namestring" (namestring pathname))
3846 ("Physical pathname: " (translate-logical-pathname pathname)))
3847 `("Host: " (pathname-host pathname)
3848 " (" (:value ,(logical-pathname-translations
3849 (pathname-host pathname)))
3850 "other translations)"
3851 (:newline))
3852 (label-value-line*
3853 ("Directory" (pathname-directory pathname))
3854 ("Name" (pathname-name pathname))
3855 ("Type" (pathname-type pathname))
3856 ("Version" (pathname-version pathname))
3857 ("Truename" (if (not (wild-pathname-p pathname))
3858 (probe-file pathname)))))))
3859
3860 (defmethod inspect-for-emacs ((n number) inspector)
3861 (declare (ignore inspector))
3862 (values "A number." `("Value: " ,(princ-to-string n))))
3863
3864 (defun format-iso8601-time (time-value &optional include-timezone-p)
3865 "Formats a universal time TIME-VALUE in ISO 8601 format, with
3866 the time zone included if INCLUDE-TIMEZONE-P is non-NIL"
3867 ;; Taken from http://www.pvv.ntnu.no/~nsaa/ISO8601.html
3868 ;; Thanks, Nikolai Sandved and Thomas Russ!
3869 (flet ((format-iso8601-timezone (zone)
3870 (if (zerop zone)
3871 "Z"
3872 (multiple-value-bind (h m) (truncate (abs zone) 1.0)
3873 ;; Tricky. Sign of time zone is reversed in ISO 8601
3874 ;; relative to Common Lisp convention!
3875 (format nil "~:[+~;-~]~2,'0D:~2,'0D"
3876 (> zone 0) h (round m))))))
3877 (multiple-value-bind (second minute hour day month year dow dst zone)
3878 (decode-universal-time time-value)
3879 (declare (ignore dow dst))
3880 (format nil "~4,'0D-~2,'0D-~2,'0DT~2,'0D:~2,'0D:~2,'0D~:[~*~;~A~]"
3881 year month day hour minute second
3882 include-timezone-p (format-iso8601-timezone zone)))))
3883
3884 (defmethod inspect-for-emacs ((i integer) inspector)
3885 (declare (ignore inspector))
3886 (values "A number."
3887 (append
3888 `(,(format nil "Value: ~D = #x~X = #o~O = #b~,,' ,8:B = ~E"
3889 i i i i i)
3890 (:newline))
3891 (if (< -1 i char-code-limit)
3892 (label-value-line "Corresponding character" (code-char i)))
3893 (label-value-line "Length" (integer-length i))
3894 (ignore-errors
3895 (list "As time: "
3896 (format-iso8601-time i t))))))
3897
3898 (defmethod inspect-for-emacs ((c complex) inspector)
3899 (declare (ignore inspector))
3900 (values "A complex number."
3901 (label-value-line*
3902 ("Real part" (realpart c))
3903 ("Imaginary part" (imagpart c)))))
3904
3905 (defmethod inspect-for-emacs ((r ratio) inspector)
3906 (declare (ignore inspector))
3907 (values "A non-integer ratio."
3908 (label-value-line*
3909 ("Numerator" (numerator r))
3910 ("Denominator" (denominator r))
3911 ("As float" (float r)))))
3912
3913 (defmethod inspect-for-emacs ((f float) inspector)
3914 (declare (ignore inspector))
3915 (multiple-value-bind (significand exponent sign) (decode-float f)
3916 (values "A floating point number."
3917 (append
3918 `("Scientific: " ,(format nil "~E" f) (:newline)
3919 "Decoded: "
3920 (:value ,sign) " * "
3921 (:value ,significand) " * "
3922 (:value ,(float-radix f)) "^" (:value ,exponent) (:newline))
3923 (label-value-line "Digits" (float-digits f))
3924 (label-value-line "Precision" (float-precision f))))))
3925
3926 (defvar *inspectee*)
3927 (defvar *inspectee-parts*)
3928 (defvar *inspectee-actions*)
3929 (defvar *inspector-stack* '())
3930 (defvar *inspector-history* (make-array 10 :adjustable t :fill-pointer 0))
3931 (declaim (type vector *inspector-history*))
3932 (defvar *inspect-length* 30)
3933
3934 (defun reset-inspector ()
3935 (setq *inspectee* nil
3936 *inspector-stack* nil
3937 *inspectee-parts* (make-array 10 :adjustable t :fill-pointer 0)
3938 *inspectee-actions* (make-array 10 :adjustable t :fill-pointer 0)
3939 *inspector-history* (make-array 10 :adjustable t :fill-pointer 0)))
3940
3941 (defslimefun init-inspector (string)
3942 (with-buffer-syntax ()
3943 (reset-inspector)
3944 (inspect-object (eval (read-from-string string)))))
3945
3946 (defun print-part-to-string (value)
3947 (let ((string (to-string value))
3948 (pos (position value *inspector-history*)))
3949 (if pos
3950 (format nil "#~D=~A" pos string)
3951 string)))
3952
3953 (defun inspector-content-for-emacs (specs)
3954 (loop for part in specs collect
3955 (etypecase part
3956 (null ; XXX encourages sloppy programming
3957 nil)
3958 (string part)
3959 (cons (destructure-case part
3960 ((:newline)
3961 (string #\newline))
3962 ((:value obj &optional str)
3963 (value-part-for-emacs obj str))
3964 ((:action label lambda)
3965 (action-part-for-emacs label lambda)))))))
3966
3967 (defun assign-index (object vector)
3968 (let ((index (fill-pointer vector)))
3969 (vector-push-extend object vector)
3970 index))
3971
3972 (defun value-part-for-emacs (object string)
3973 (list :value
3974 (or string (print-part-to-string object))
3975 (assign-index object *inspectee-parts*)))
3976
3977 (defun action-part-for-emacs (label lambda)
3978 (list :action label (assign-index lambda *inspectee-actions*)))
3979
3980 (defun inspect-object (object &optional (inspector (make-default-inspector)))
3981 (push (setq *inspectee* object) *inspector-stack*)
3982 (unless (find object *inspector-history*)
3983 (vector-push-extend object *inspector-history*))
3984 (let ((*print-pretty* nil) ; print everything in the same line
3985 (*print-circle* t)
3986 (*print-readably* nil))
3987 (multiple-value-bind (title content)
3988 (inspect-for-emacs object inspector)
3989 (list :title title
3990 :type (to-string (type-of object))
3991 :content (inspector-content-for-emacs content)))))
3992
3993 (defslimefun inspector-nth-part (index)
3994 (aref *inspectee-parts* index))
3995
3996 (defslimefun inspect-nth-part (index)
3997 (with-buffer-syntax ()
3998 (inspect-object (inspector-nth-part index))))
3999
4000 (defslimefun inspector-call-nth-action (index &rest args)
4001 (multiple-value-bind (value replace) (apply (aref *inspectee-actions* index) args)
4002 (if (eq replace :replace)
4003 value
4004 (inspect-object (pop *inspector-stack*)))))
4005
4006 (defslimefun inspector-pop ()
4007 "Drop the inspector stack and inspect the second element. Return
4008 nil if there's no second element."
4009 (with-buffer-syntax ()
4010 (cond ((cdr *inspector-stack*)
4011 (pop *inspector-stack*)
4012 (inspect-object (pop *inspector-stack*)))
4013 (t nil))))
4014
4015 (defslimefun inspector-next ()
4016 "Inspect the next element in the *inspector-history*."
4017 (with-buffer-syntax ()
4018 (let ((position (position *inspectee* *inspector-history*)))
4019 (cond ((= (1+ position) (length *inspector-history*))
4020 nil)
4021 (t (inspect-object (aref *inspector-history* (1+ position))))))))
4022
4023 (defslimefun inspector-reinspect ()
4024 (inspect-object *inspectee*))
4025
4026 (defslimefun quit-inspector ()
4027 (reset-inspector)
4028 nil)
4029
4030 (defslimefun describe-inspectee ()
4031 "Describe the currently inspected object."
4032 (with-buffer-syntax ()
4033 (describe-to-string *inspectee*)))
4034
4035 (defslimefun inspect-in-frame (string index)
4036 (with-buffer-syntax ()
4037 (reset-inspector)
4038 (inspect-object (eval-in-frame (from-string string) index))))
4039
4040 (defslimefun inspect-current-condition ()
4041 (with-buffer-syntax ()
4042 (reset-inspector)
4043 (inspect-object *swank-debugger-condition*)))
4044
4045 (defslimefun inspect-frame-var (frame var)
4046 (with-buffer-syntax ()
4047 (reset-inspector)
4048 (inspect-object (frame-var-value frame var))))
4049
4050
4051 ;;;; Thread listing
4052
4053 (defvar *thread-list* ()
4054 "List of threads displayed in Emacs. We don't care a about
4055 synchronization issues (yet). There can only be one thread listing at
4056 a time.")
4057
4058 (defslimefun list-threads ()
4059 "Return a list ((NAME DESCRIPTION) ...) of all threads."
4060 (setq *thread-list* (all-threads))
4061 (loop for thread in *thread-list*
4062 for name = (thread-name thread)
4063 collect (list (if (symbolp name) (symbol-name name) name)
4064 (thread-status thread)
4065 (thread-id thread))))
4066
4067 (defslimefun quit-thread-browser ()
4068 (setq *thread-list* nil))
4069
4070 (defun nth-thread (index)
4071 (nth index *thread-list*))
4072
4073 (defslimefun debug-nth-thread (index)
4074 (let ((connection *emacs-connection*))
4075 (interrupt-thread (nth-thread index)
4076 (lambda ()
4077 (with-connection (connection)
4078 (simple-break))))))
4079
4080 (defslimefun kill-nth-thread (index)
4081 (kill-thread (nth-thread index)))
4082
4083 (defslimefun start-swank-server-in-thread (index port-file-name)
4084 "Interrupt the INDEXth thread and make it start a swank server.
4085 The server port is written to PORT-FILE-NAME."
4086 (interrupt-thread (nth-thread index)
4087 (lambda ()
4088 (start-server port-file-name :style nil))))
4089
4090 ;;;; Class browser
4091
4092 (defun mop-helper (class-name fn)
4093 (let ((class (find-class class-name nil)))
4094 (if class
4095 (mapcar (lambda (x) (to-string (class-name x)))
4096 (funcall fn class)))))
4097
4098 (defslimefun mop (type symbol-name)
4099 "Return info about classes using mop.
4100
4101 When type is:
4102 :subclasses - return the list of subclasses of class.
4103 :superclasses - return the list of superclasses of class."
4104 (let ((symbol (parse-symbol symbol-name *buffer-package*)))
4105 (ecase type
4106 (:subclasses
4107 (mop-helper symbol #'swank-mop:class-direct-subclasses))
4108 (:superclasses
4109 (mop-helper symbol #'swank-mop:class-direct-superclasses)))))
4110
4111
4112 ;;;; Automatically synchronized state
4113 ;;;
4114 ;;; Here we add hooks to push updates of relevant information to
4115 ;;; Emacs.
4116
4117 ;;;;; *FEATURES*
4118
4119 (defun sync-features-to-emacs ()
4120 "Update Emacs if any relevant Lisp state has changed."
4121 ;; FIXME: *slime-features* should be connection-local
4122 (unless (eq *slime-features* *features*)
4123 (setq *slime-features* *features*)
4124 (send-to-emacs (list :new-features (features-for-emacs)))))
4125
4126 (defun features-for-emacs ()
4127 "Return `*slime-features*' in a format suitable to send it to Emacs."
4128 *slime-features*)
4129
4130 (add-hook *pre-reply-hook* 'sync-features-to-emacs)
4131
4132
4133 ;;;;; Indentation of macros
4134 ;;;
4135 ;;; This code decides how macros should be indented (based on their
4136 ;;; arglists) and tells Emacs. A per-connection cache is used to avoid
4137 ;;; sending redundant information to Emacs -- we just say what's
4138 ;;; changed since last time.
4139 ;;;
4140 ;;; The strategy is to scan all symbols, pick out the macros, and look
4141 ;;; for &body-arguments.
4142
4143 (defvar *configure-emacs-indentation* t
4144 "When true, automatically send indentation information to Emacs
4145 after each command.")
4146
4147 (defslimefun update-indentation-information ()
4148 (perform-indentation-update *emacs-connection* t))
4149
4150 ;; This function is for *PRE-REPLY-HOOK*.
4151 (defun sync-indentation-to-emacs ()
4152 "Send any indentation updates to Emacs via CONNECTION."
4153 (when *configure-emacs-indentation*
4154 (let ((fullp (need-full-indentation-update-p *emacs-connection*)))
4155 (perform-indentation-update *emacs-connection* fullp))))
4156
4157 (defun need-full-indentation-update-p (connection)
4158 "Return true if the whole indentation cache should be updated.
4159 This is a heuristic to avoid scanning all symbols all the time:
4160 instead, we only do a full scan if the set of packages has changed."
4161 (set-difference (list-all-packages)
4162 (connection.indentation-cache-packages connection)))
4163
4164 (defun perform-indentation-update (connection force)
4165 "Update the indentation cache in CONNECTION and update Emacs.
4166 If FORCE is true then start again without considering the old cache."
4167 (let ((cache (connection.indentation-cache connection)))
4168 (when force (clrhash cache))
4169 (let ((delta (update-indentation/delta-for-emacs cache force)))
4170 (setf (connection.indentation-cache-packages connection)
4171 (list-all-packages))
4172 (unless (null delta)
4173 (send-to-emacs (list :indentation-update delta))))))
4174
4175 (defun update-indentation/delta-for-emacs (cache &optional force)
4176 "Update the cache and return the changes in a (SYMBOL . INDENT) list.
4177 If FORCE is true then check all symbols, otherwise only check symbols
4178 belonging to the buffer package."
4179 (let ((alist '()))
4180 (flet ((consider (symbol)
4181 (let ((indent (symbol-indentation symbol)))
4182 (when indent
4183 (unless (equal (gethash symbol cache) indent)
4184 (setf (gethash symbol cache) indent)
4185 (push (cons (string-downcase symbol) indent) alist))))))
4186 (if force
4187 (do-all-symbols (symbol)
4188 (consider symbol))
4189 (do-symbols (symbol *buffer-package*)
4190 (when (eq (symbol-package symbol) *buffer-package*)
4191 (consider symbol)))))
4192 alist))
4193
4194 (defun package-names (package)
4195 "Return the name and all nicknames of PACKAGE in a list."
4196 (cons (package-name package) (package-nicknames package)))
4197
4198 (defun cl-symbol-p (symbol)
4199 "Is SYMBOL a symbol in the COMMON-LISP package?"
4200 (eq (symbol-package symbol) cl-package))
4201
4202 (defun known-to-emacs-p (symbol)
4203 "Return true if Emacs has special rules for indenting SYMBOL."
4204 (cl-symbol-p symbol))
4205
4206 (defun symbol-indentation (symbol)
4207 "Return a form describing the indentation of SYMBOL.
4208 The form is to be used as the `common-lisp-indent-function' property
4209 in Emacs."
4210 (if (and (macro-function symbol)
4211 (not (known-to-emacs-p symbol)))
4212 (let ((arglist (arglist symbol)))
4213 (etypecase arglist
4214 ((member :not-available)
4215 nil)
4216 (list
4217 (macro-indentation arglist))))
4218 nil))
4219
4220 (defun macro-indentation (arglist)
4221 (if (well-formed-list-p arglist)
4222 (position '&body (clean-arglist arglist))
4223 nil))
4224
4225 (defun well-formed-list-p (list)
4226 "Is LIST a proper list terminated by NIL?"
4227 (typecase list
4228 (null t)
4229 (cons (well-formed-list-p (cdr list)))
4230 (t nil)))
4231
4232 (defun print-indentation-lossage (&optional (stream *standard-output*))
4233 "Return the list of symbols whose indentation styles collide incompatibly.
4234 Collisions are caused because package information is ignored."
4235 (let ((table (make-hash-table :test 'equal)))
4236 (flet ((name (s) (string-downcase (symbol-name s))))
4237 (do-all-symbols (s)
4238 (setf (gethash (name s) table)
4239 (cons s (symbol-indentation s))))
4240 (let ((collisions '()))
4241 (do-all-symbols (s)
4242 (let* ((entry (gethash (name s) table))
4243 (owner (car entry))
4244 (ind