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

Contents of /slime/swank-lispworks.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.112 - (show annotations)
Mon Aug 11 07:37:05 2008 UTC (5 years, 8 months ago) by heller
Branch: MAIN
Changes since 1.111: +3 -4 lines
* swank.lisp (wait-for-event): Add timeout argument.  This is used
for :fd-handler and :sigio style where we only process events as
long we don't block.
(wait-for-event/event-loop, read-event)
(decode-message, receive-if): Ditto.
(process-events): Renamed from read-from-emacs.
(handle-requests): Renamed from handle-request. Take timeout
argument.  Update callers.
(process-available-input): Deleted.
1 ;;; -*- indent-tabs-mode: nil -*-
2 ;;;
3 ;;; swank-lispworks.lisp --- LispWorks specific code for SLIME.
4 ;;;
5 ;;; Created 2003, Helmut Eller
6 ;;;
7 ;;; This code has been placed in the Public Domain. All warranties
8 ;;; are disclaimed.
9 ;;;
10
11 (in-package :swank-backend)
12
13 (eval-when (:compile-toplevel :load-toplevel :execute)
14 (require "comm")
15 (import-from :stream *gray-stream-symbols* :swank-backend))
16
17 (import-swank-mop-symbols :clos '(:slot-definition-documentation
18 :eql-specializer
19 :eql-specializer-object
20 :compute-applicable-methods-using-classes))
21
22 (defun swank-mop:slot-definition-documentation (slot)
23 (documentation slot t))
24
25 (defun swank-mop:compute-applicable-methods-using-classes (gf classes)
26 (clos::compute-applicable-methods-from-classes gf classes))
27
28 ;; lispworks doesn't have the eql-specializer class, it represents
29 ;; them as a list of `(EQL ,OBJECT)
30 (deftype swank-mop:eql-specializer () 'cons)
31
32 (defun swank-mop:eql-specializer-object (eql-spec)
33 (second eql-spec))
34
35 (eval-when (:compile-toplevel :execute :load-toplevel)
36 (defvar *original-defimplementation* (macro-function 'defimplementation))
37 (defmacro defimplementation (&whole whole name args &body body
38 &environment env)
39 (declare (ignore args body))
40 `(progn
41 (dspec:record-definition '(defun ,name) (dspec:location)
42 :check-redefinition-p nil)
43 ,(funcall *original-defimplementation* whole env))))
44
45 ;;; TCP server
46
47 (defimplementation preferred-communication-style ()
48 :spawn)
49
50 (defun socket-fd (socket)
51 (etypecase socket
52 (fixnum socket)
53 (comm:socket-stream (comm:socket-stream-socket socket))))
54
55 (defimplementation create-socket (host port)
56 (multiple-value-bind (socket where errno)
57 #-(or lispworks4.1 (and macosx lispworks4.3))
58 (comm::create-tcp-socket-for-service port :address host)
59 #+(or lispworks4.1 (and macosx lispworks4.3))
60 (comm::create-tcp-socket-for-service port)
61 (cond (socket socket)
62 (t (error 'network-error
63 :format-control "~A failed: ~A (~D)"
64 :format-arguments (list where
65 (list #+unix (lw:get-unix-error errno))
66 errno))))))
67
68 (defimplementation local-port (socket)
69 (nth-value 1 (comm:get-socket-address (socket-fd socket))))
70
71 (defimplementation close-socket (socket)
72 (comm::close-socket (socket-fd socket)))
73
74 (defimplementation accept-connection (socket
75 &key external-format buffering timeout)
76 (declare (ignore buffering timeout external-format))
77 (let* ((fd (comm::get-fd-from-socket socket)))
78 (assert (/= fd -1))
79 (make-instance 'comm:socket-stream :socket fd :direction :io
80 :element-type 'base-char)))
81
82 (defun set-sigint-handler ()
83 ;; Set SIGINT handler on Swank request handler thread.
84 #-win32
85 (sys::set-signal-handler +sigint+
86 (make-sigint-handler mp:*current-process*)))
87
88 ;;; Coding Systems
89
90 (defvar *external-format-to-coding-system*
91 '(((:latin-1 :eol-style :lf)
92 "latin-1-unix" "iso-latin-1-unix" "iso-8859-1-unix")
93 ((:latin-1)
94 "latin-1" "iso-latin-1" "iso-8859-1")
95 ((:utf-8) "utf-8")
96 ((:utf-8 :eol-style :lf) "utf-8-unix")
97 ((:euc-jp) "euc-jp")
98 ((:euc-jp :eol-style :lf) "euc-jp-unix")
99 ((:ascii) "us-ascii")
100 ((:ascii :eol-style :lf) "us-ascii-unix")))
101
102 (defimplementation find-external-format (coding-system)
103 (car (rassoc-if (lambda (x) (member coding-system x :test #'equal))
104 *external-format-to-coding-system*)))
105
106 ;;; Unix signals
107
108 (defun sigint-handler ()
109 (with-simple-restart (continue "Continue from SIGINT handler.")
110 (invoke-debugger "SIGINT")))
111
112 (defun make-sigint-handler (process)
113 (lambda (&rest args)
114 (declare (ignore args))
115 (mp:process-interrupt process #'sigint-handler)))
116
117 (defimplementation call-without-interrupts (fn)
118 (lw:without-interrupts (funcall fn)))
119
120 (defimplementation getpid ()
121 #+win32 (win32:get-current-process-id)
122 #-win32 (system::getpid))
123
124 (defimplementation lisp-implementation-type-name ()
125 "lispworks")
126
127 (defimplementation set-default-directory (directory)
128 (namestring (hcl:change-directory directory)))
129
130 ;;;; Documentation
131
132 (defimplementation arglist (symbol-or-function)
133 (let ((arglist (lw:function-lambda-list symbol-or-function)))
134 (etypecase arglist
135 ((member :dont-know)
136 :not-available)
137 (list
138 arglist))))
139
140 (defimplementation function-name (function)
141 (nth-value 2 (function-lambda-expression function)))
142
143 (defimplementation macroexpand-all (form)
144 (walker:walk-form form))
145
146 (defun generic-function-p (object)
147 (typep object 'generic-function))
148
149 (defimplementation describe-symbol-for-emacs (symbol)
150 "Return a plist describing SYMBOL.
151 Return NIL if the symbol is unbound."
152 (let ((result '()))
153 (labels ((first-line (string)
154 (let ((pos (position #\newline string)))
155 (if (null pos) string (subseq string 0 pos))))
156 (doc (kind &optional (sym symbol))
157 (let ((string (documentation sym kind)))
158 (if string
159 (first-line string)
160 :not-documented)))
161 (maybe-push (property value)
162 (when value
163 (setf result (list* property value result)))))
164 (maybe-push
165 :variable (when (boundp symbol)
166 (doc 'variable)))
167 (maybe-push
168 :generic-function (if (and (fboundp symbol)
169 (generic-function-p (fdefinition symbol)))
170 (doc 'function)))
171 (maybe-push
172 :function (if (and (fboundp symbol)
173 (not (generic-function-p (fdefinition symbol))))
174 (doc 'function)))
175 (maybe-push
176 :setf (let ((setf-name (sys:underlying-setf-name `(setf ,symbol))))
177 (if (fboundp setf-name)
178 (doc 'setf))))
179 (maybe-push
180 :class (if (find-class symbol nil)
181 (doc 'class)))
182 result)))
183
184 (defimplementation describe-definition (symbol type)
185 (ecase type
186 (:variable (describe-symbol symbol))
187 (:class (describe (find-class symbol)))
188 ((:function :generic-function) (describe-function symbol))
189 (:setf (describe-function (sys:underlying-setf-name `(setf ,symbol))))))
190
191 (defun describe-function (symbol)
192 (cond ((fboundp symbol)
193 (format t "~%(~A~{ ~A~})~%~%~:[(not documented)~;~:*~A~]~%"
194 (string-downcase symbol)
195 (mapcar #'string-upcase
196 (lispworks:function-lambda-list symbol))
197 (documentation symbol 'function))
198 (describe (fdefinition symbol)))
199 (t (format t "~S is not fbound" symbol))))
200
201 (defun describe-symbol (sym)
202 (format t "~A is a symbol in package ~A." sym (symbol-package sym))
203 (when (boundp sym)
204 (format t "~%~%Value: ~A" (symbol-value sym)))
205 (let ((doc (documentation sym 'variable)))
206 (when doc
207 (format t "~%~%Variable documentation:~%~A" doc)))
208 (when (fboundp sym)
209 (describe-function sym)))
210
211 ;;; Debugging
212
213 (defclass slime-env (env:environment)
214 ((debugger-hook :initarg :debugger-hoook)))
215
216 (defun slime-env (hook io-bindings)
217 (make-instance 'slime-env :name "SLIME Environment"
218 :io-bindings io-bindings
219 :debugger-hoook hook))
220
221 (defmethod env-internals:environment-display-notifier
222 ((env slime-env) &key restarts condition)
223 (declare (ignore restarts condition))
224 ;;(funcall (swank-sym :swank-debugger-hook) condition *debugger-hook*)
225 (values t nil)
226 )
227
228 (defmethod env-internals:environment-display-debugger ((env slime-env))
229 *debug-io*)
230
231 (defmethod env-internals:confirm-p ((e slime-env) &optional msg &rest args)
232 (apply (swank-sym :y-or-n-p-in-emacs) msg args))
233
234 (defimplementation call-with-debugger-hook (hook fun)
235 (let ((*debugger-hook* hook))
236 (env:with-environment ((slime-env hook '()))
237 (funcall fun))))
238
239 (defimplementation install-debugger-globally (function)
240 (setq *debugger-hook* function)
241 (setf (env:environment) (slime-env function '())))
242
243 (defvar *sldb-top-frame*)
244
245 (defun interesting-frame-p (frame)
246 (cond ((or (dbg::call-frame-p frame)
247 (dbg::derived-call-frame-p frame)
248 (dbg::foreign-frame-p frame)
249 (dbg::interpreted-call-frame-p frame))
250 t)
251 ((dbg::catch-frame-p frame) dbg:*print-catch-frames*)
252 ((dbg::binding-frame-p frame) dbg:*print-binding-frames*)
253 ((dbg::handler-frame-p frame) dbg:*print-handler-frames*)
254 ((dbg::restart-frame-p frame) dbg:*print-restart-frames*)
255 ((dbg::open-frame-p frame) dbg:*print-open-frames*)
256 (t nil)))
257
258 (defun nth-next-frame (frame n)
259 "Unwind FRAME N times."
260 (do ((frame frame (dbg::frame-next frame))
261 (i n (if (interesting-frame-p frame) (1- i) i)))
262 ((or (not frame)
263 (and (interesting-frame-p frame) (zerop i)))
264 frame)))
265
266 (defun nth-frame (index)
267 (nth-next-frame *sldb-top-frame* index))
268
269 (defun find-top-frame ()
270 "Return the most suitable top-frame for the debugger."
271 (or (do ((frame (dbg::debugger-stack-current-frame dbg::*debugger-stack*)
272 (nth-next-frame frame 1)))
273 ((or (null frame) ; no frame found!
274 (and (dbg::call-frame-p frame)
275 (eq (dbg::call-frame-function-name frame)
276 'invoke-debugger)))
277 (nth-next-frame frame 1)))
278 ;; if we can't find a invoke-debugger frame, take any old frame at the top
279 (dbg::debugger-stack-current-frame dbg::*debugger-stack*)))
280
281 (defimplementation call-with-debugging-environment (fn)
282 (dbg::with-debugger-stack ()
283 (let ((*sldb-top-frame* (find-top-frame)))
284 (funcall fn))))
285
286 (defimplementation compute-backtrace (start end)
287 (let ((end (or end most-positive-fixnum))
288 (backtrace '()))
289 (do ((frame (nth-frame start) (dbg::frame-next frame))
290 (i start))
291 ((or (not frame) (= i end)) (nreverse backtrace))
292 (when (interesting-frame-p frame)
293 (incf i)
294 (push frame backtrace)))))
295
296 (defun frame-actual-args (frame)
297 (let ((*break-on-signals* nil))
298 (mapcar (lambda (arg)
299 (case arg
300 ((&rest &optional &key) arg)
301 (t
302 (handler-case (dbg::dbg-eval arg frame)
303 (error (e) (format nil "<~A>" arg))))))
304 (dbg::call-frame-arglist frame))))
305
306 (defimplementation print-frame (frame stream)
307 (cond ((dbg::call-frame-p frame)
308 (format stream "~S ~S"
309 (dbg::call-frame-function-name frame)
310 (frame-actual-args frame)))
311 (t (princ frame stream))))
312
313 (defun frame-vars (frame)
314 (first (dbg::frame-locals-format-list frame #'list 75 0)))
315
316 (defimplementation frame-locals (n)
317 (let ((frame (nth-frame n)))
318 (if (dbg::call-frame-p frame)
319 (mapcar (lambda (var)
320 (destructuring-bind (name value symbol location) var
321 (declare (ignore name location))
322 (list :name symbol :id 0
323 :value value)))
324 (frame-vars frame)))))
325
326 (defimplementation frame-var-value (frame var)
327 (let ((frame (nth-frame frame)))
328 (destructuring-bind (_n value _s _l) (nth var (frame-vars frame))
329 (declare (ignore _n _s _l))
330 value)))
331
332 (defimplementation frame-catch-tags (index)
333 (declare (ignore index))
334 nil)
335
336 (defimplementation frame-source-location-for-emacs (frame)
337 (let ((frame (nth-frame frame))
338 (callee (if (plusp frame) (nth-frame (1- frame)))))
339 (if (dbg::call-frame-p frame)
340 (let ((dspec (dbg::call-frame-function-name frame))
341 (cname (and (dbg::call-frame-p callee)
342 (dbg::call-frame-function-name callee))))
343 (if dspec
344 (frame-location dspec cname))))))
345
346 (defimplementation eval-in-frame (form frame-number)
347 (let ((frame (nth-frame frame-number)))
348 (dbg::dbg-eval form frame)))
349
350 (defimplementation return-from-frame (frame-number form)
351 (let* ((frame (nth-frame frame-number))
352 (return-frame (dbg::find-frame-for-return frame)))
353 (dbg::dbg-return-from-call-frame frame form return-frame
354 dbg::*debugger-stack*)))
355
356 (defimplementation restart-frame (frame-number)
357 (let ((frame (nth-frame frame-number)))
358 (dbg::restart-frame frame :same-args t)))
359
360 (defimplementation disassemble-frame (frame-number)
361 (let* ((frame (nth-frame frame-number)))
362 (when (dbg::call-frame-p frame)
363 (let ((function (dbg::get-call-frame-function frame)))
364 (disassemble function)))))
365
366 ;;; Definition finding
367
368 (defun frame-location (dspec callee-name)
369 (let ((infos (dspec:find-dspec-locations dspec)))
370 (cond (infos
371 (destructuring-bind ((rdspec location) &rest _) infos
372 (declare (ignore _))
373 (let ((name (and callee-name (symbolp callee-name)
374 (string callee-name))))
375 (make-dspec-location rdspec location
376 `(:call-site ,name)))))
377 (t
378 (list :error (format nil "Source location not available for: ~S"
379 dspec))))))
380
381 (defimplementation find-definitions (name)
382 (let ((locations (dspec:find-name-locations dspec:*dspec-classes* name)))
383 (loop for (dspec location) in locations
384 collect (list dspec (make-dspec-location dspec location)))))
385
386
387 ;;; Compilation
388
389 (defmacro with-swank-compilation-unit ((location &rest options) &body body)
390 (lw:rebinding (location)
391 `(let ((compiler::*error-database* '()))
392 (with-compilation-unit ,options
393 ,@body
394 (signal-error-data-base compiler::*error-database* ,location)
395 (signal-undefined-functions compiler::*unknown-functions* ,location)))))
396
397 (defimplementation swank-compile-file (filename load-p external-format)
398 (with-swank-compilation-unit (filename)
399 (compile-file filename :load load-p :external-format external-format)))
400
401 (defvar *within-call-with-compilation-hooks* nil
402 "Whether COMPILE-FILE was called from within CALL-WITH-COMPILATION-HOOKS.")
403
404 (defvar *undefined-functions-hash* nil
405 "Hash table to map info about undefined functions to pathnames.")
406
407 (lw:defadvice (compile-file compile-file-and-collect-notes :around)
408 (pathname &rest rest)
409 (multiple-value-prog1 (apply #'lw:call-next-advice pathname rest)
410 (when *within-call-with-compilation-hooks*
411 (maphash (lambda (unfun dspecs)
412 (dolist (dspec dspecs)
413 (let ((unfun-info (list unfun dspec)))
414 (unless (gethash unfun-info *undefined-functions-hash*)
415 (setf (gethash unfun-info *undefined-functions-hash*)
416 pathname)))))
417 compiler::*unknown-functions*))))
418
419 (defimplementation call-with-compilation-hooks (function)
420 (let ((compiler::*error-database* '())
421 (*undefined-functions-hash* (make-hash-table :test 'equal))
422 (*within-call-with-compilation-hooks* t))
423 (with-compilation-unit ()
424 (prog1 (funcall function)
425 (signal-error-data-base compiler::*error-database*)
426 (signal-undefined-functions compiler::*unknown-functions*)))))
427
428 (defun map-error-database (database fn)
429 (loop for (filename . defs) in database do
430 (loop for (dspec . conditions) in defs do
431 (dolist (c conditions)
432 (funcall fn filename dspec (if (consp c) (car c) c))))))
433
434 (defun lispworks-severity (condition)
435 (cond ((not condition) :warning)
436 (t (etypecase condition
437 (error :error)
438 (style-warning :warning)
439 (warning :warning)))))
440
441 (defun signal-compiler-condition (message location condition)
442 (check-type message string)
443 (signal
444 (make-instance 'compiler-condition :message message
445 :severity (lispworks-severity condition)
446 :location location
447 :original-condition condition)))
448
449 (defun compile-from-temp-file (string filename)
450 (unwind-protect
451 (progn
452 (with-open-file (s filename :direction :output :if-exists :supersede)
453 (write-string string s)
454 (finish-output s))
455 (let ((binary-filename (compile-file filename :load t)))
456 (when binary-filename
457 (delete-file binary-filename))))
458 (delete-file filename)))
459
460 (defun dspec-buffer-position (dspec offset)
461 (etypecase dspec
462 (cons (let ((name (dspec:dspec-primary-name dspec)))
463 (typecase name
464 ((or symbol string)
465 (list :function-name (string name)))
466 (t (list :position offset)))))
467 (null (list :position offset))
468 (symbol (list :function-name (string dspec)))))
469
470 (defmacro with-fairly-standard-io-syntax (&body body)
471 "Like WITH-STANDARD-IO-SYNTAX but preserve *PACKAGE* and *READTABLE*."
472 (let ((package (gensym))
473 (readtable (gensym)))
474 `(let ((,package *package*)
475 (,readtable *readtable*))
476 (with-standard-io-syntax
477 (let ((*package* ,package)
478 (*readtable* ,readtable))
479 ,@body)))))
480
481 #-(or lispworks4.1 lispworks4.2) ; no dspec:parse-form-dspec prior to 4.3
482 (defun dspec-stream-position (stream dspec)
483 (with-fairly-standard-io-syntax
484 (loop (let* ((pos (file-position stream))
485 (form (read stream nil '#1=#:eof)))
486 (when (eq form '#1#)
487 (return nil))
488 (labels ((check-dspec (form)
489 (when (consp form)
490 (let ((operator (car form)))
491 (case operator
492 ((progn)
493 (mapcar #'check-dspec
494 (cdr form)))
495 ((eval-when locally macrolet symbol-macrolet)
496 (mapcar #'check-dspec
497 (cddr form)))
498 ((in-package)
499 (let ((package (find-package (second form))))
500 (when package
501 (setq *package* package))))
502 (otherwise
503 (let ((form-dspec (dspec:parse-form-dspec form)))
504 (when (dspec:dspec-equal dspec form-dspec)
505 (return pos)))))))))
506 (check-dspec form))))))
507
508 (defun dspec-file-position (file dspec)
509 (let* ((*compile-file-pathname* (pathname file))
510 (*compile-file-truename* (truename *compile-file-pathname*))
511 (*load-pathname* *compile-file-pathname*)
512 (*load-truename* *compile-file-truename*))
513 (with-open-file (stream file)
514 (let ((pos
515 #-(or lispworks4.1 lispworks4.2)
516 (dspec-stream-position stream dspec)))
517 (if pos
518 (list :position (1+ pos) t)
519 (dspec-buffer-position dspec 1))))))
520
521 (defun emacs-buffer-location-p (location)
522 (and (consp location)
523 (eq (car location) :emacs-buffer)))
524
525 (defun make-dspec-location (dspec location &optional hints)
526 (etypecase location
527 ((or pathname string)
528 (multiple-value-bind (file err)
529 (ignore-errors (namestring (truename location)))
530 (if err
531 (list :error (princ-to-string err))
532 (make-location `(:file ,file)
533 (dspec-file-position file dspec)
534 hints))))
535 (symbol
536 `(:error ,(format nil "Cannot resolve location: ~S" location)))
537 ((satisfies emacs-buffer-location-p)
538 (destructuring-bind (_ buffer offset string) location
539 (declare (ignore _ string))
540 (make-location `(:buffer ,buffer)
541 (dspec-buffer-position dspec offset)
542 hints)))))
543
544 (defun make-dspec-progenitor-location (dspec location)
545 (let ((canon-dspec (dspec:canonicalize-dspec dspec)))
546 (make-dspec-location
547 (if canon-dspec
548 (if (dspec:local-dspec-p canon-dspec)
549 (dspec:dspec-progenitor canon-dspec)
550 canon-dspec)
551 nil)
552 location)))
553
554 (defun signal-error-data-base (database &optional location)
555 (map-error-database
556 database
557 (lambda (filename dspec condition)
558 (signal-compiler-condition
559 (format nil "~A" condition)
560 (make-dspec-progenitor-location dspec (or location filename))
561 condition))))
562
563 (defun unmangle-unfun (symbol)
564 "Converts symbols like 'SETF::|\"CL-USER\" \"GET\"| to
565 function names like \(SETF GET)."
566 (cond ((sys::setf-symbol-p symbol)
567 (sys::setf-pair-from-underlying-name symbol))
568 (t symbol)))
569
570 (defun signal-undefined-functions (htab &optional filename)
571 (maphash (lambda (unfun dspecs)
572 (dolist (dspec dspecs)
573 (signal-compiler-condition
574 (format nil "Undefined function ~A" (unmangle-unfun unfun))
575 (make-dspec-progenitor-location dspec
576 (or filename
577 (gethash (list unfun dspec)
578 *undefined-functions-hash*)))
579 nil)))
580 htab))
581
582 (defimplementation swank-compile-string (string &key buffer position directory
583 debug)
584 (declare (ignore directory debug))
585 (assert buffer)
586 (assert position)
587 (let* ((location (list :emacs-buffer buffer position string))
588 (tmpname (hcl:make-temp-file nil "lisp")))
589 (with-swank-compilation-unit (location)
590 (compile-from-temp-file
591 (with-output-to-string (s)
592 (let ((*print-radix* t))
593 (print `(eval-when (:compile-toplevel)
594 (setq dspec::*location* (list ,@location)))
595 s))
596 (write-string string s))
597 tmpname))))
598
599 ;;; xref
600
601 (defmacro defxref (name function)
602 `(defimplementation ,name (name)
603 (xref-results (,function name))))
604
605 (defxref who-calls hcl:who-calls)
606 (defxref who-macroexpands hcl:who-calls) ; macros are in the calls table too
607 (defxref calls-who hcl:calls-who)
608 (defxref list-callers list-callers-internal)
609 ;; (defxref list-callees list-callees-internal)
610
611 (defun list-callers-internal (name)
612 (let ((callers (make-array 100
613 :fill-pointer 0
614 :adjustable t)))
615 (hcl:sweep-all-objects
616 #'(lambda (object)
617 (when (and #+Harlequin-PC-Lisp (low:compiled-code-p object)
618 #-Harlequin-PC-Lisp (sys::callablep object)
619 (system::find-constant$funcallable name object))
620 (vector-push-extend object callers))))
621 ;; Delay dspec:object-dspec until after sweep-all-objects
622 ;; to reduce allocation problems.
623 (loop for object across callers
624 collect (if (symbolp object)
625 (list 'function object)
626 (or (dspec:object-dspec object) object)))))
627
628 ;; only for lispworks 4.2 and above
629 #-lispworks4.1
630 (progn
631 (defxref who-references hcl:who-references)
632 (defxref who-binds hcl:who-binds)
633 (defxref who-sets hcl:who-sets))
634
635 (defimplementation who-specializes (classname)
636 (let ((methods (clos:class-direct-methods (find-class classname))))
637 (xref-results (mapcar #'dspec:object-dspec methods))))
638
639 (defun xref-results (dspecs)
640 (flet ((frob-locs (dspec locs)
641 (cond (locs
642 (loop for (name loc) in locs
643 collect (list name (make-dspec-location name loc))))
644 (t `((,dspec (:error "Source location not available")))))))
645 (loop for dspec in dspecs
646 append (frob-locs dspec (dspec:dspec-definition-locations dspec)))))
647
648 ;;; Inspector
649
650 (defmethod emacs-inspect ((o t))
651 (lispworks-inspect o))
652
653 (defmethod emacs-inspect ((o function))
654 (lispworks-inspect o))
655
656 ;; FIXME: slot-boundp-using-class in LW works with names so we can't
657 ;; use our method in swank.lisp.
658 (defmethod emacs-inspect ((o standard-object))
659 (lispworks-inspect o))
660
661 (defun lispworks-inspect (o)
662 (multiple-value-bind (names values _getter _setter type)
663 (lw:get-inspector-values o nil)
664 (declare (ignore _getter _setter))
665 (append
666 (label-value-line "Type" type)
667 (loop for name in names
668 for value in values
669 append (label-value-line name value)))))
670
671 ;;; Miscellaneous
672
673 (defimplementation quit-lisp ()
674 (lispworks:quit))
675
676 ;;; Tracing
677
678 (defun parse-fspec (fspec)
679 "Return a dspec for FSPEC."
680 (ecase (car fspec)
681 ((:defmethod) `(method ,(cdr fspec)))))
682
683 (defun tracedp (dspec)
684 (member dspec (eval '(trace)) :test #'equal))
685
686 (defun toggle-trace-aux (dspec)
687 (cond ((tracedp dspec)
688 (eval `(untrace ,dspec))
689 (format nil "~S is now untraced." dspec))
690 (t
691 (eval `(trace (,dspec)))
692 (format nil "~S is now traced." dspec))))
693
694 (defimplementation toggle-trace (fspec)
695 (toggle-trace-aux (parse-fspec fspec)))
696
697 ;;; Multithreading
698
699 (defimplementation initialize-multiprocessing (continuation)
700 (cond ((not mp::*multiprocessing*)
701 (push (list "Initialize SLIME" '() continuation)
702 mp:*initial-processes*)
703 (mp:initialize-multiprocessing))
704 (t (funcall continuation))))
705
706 (defimplementation spawn (fn &key name)
707 (let ((mp:*process-initial-bindings*
708 (remove (find-package :cl)
709 mp:*process-initial-bindings*
710 :key (lambda (x) (symbol-package (car x))))))
711 (mp:process-run-function name () fn)))
712
713 (defvar *id-lock* (mp:make-lock))
714 (defvar *thread-id-counter* 0)
715
716 (defimplementation thread-id (thread)
717 (mp:with-lock (*id-lock*)
718 (or (getf (mp:process-plist thread) 'id)
719 (setf (getf (mp:process-plist thread) 'id)
720 (incf *thread-id-counter*)))))
721
722 (defimplementation find-thread (id)
723 (find id (mp:list-all-processes)
724 :key (lambda (p) (getf (mp:process-plist p) 'id))))
725
726 (defimplementation thread-name (thread)
727 (mp:process-name thread))
728
729 (defimplementation thread-status (thread)
730 (format nil "~A ~D"
731 (mp:process-whostate thread)
732 (mp:process-priority thread)))
733
734 (defimplementation make-lock (&key name)
735 (mp:make-lock :name name))
736
737 (defimplementation call-with-lock-held (lock function)
738 (mp:with-lock (lock) (funcall function)))
739
740 (defimplementation current-thread ()
741 mp:*current-process*)
742
743 (defimplementation all-threads ()
744 (mp:list-all-processes))
745
746 (defimplementation interrupt-thread (thread fn)
747 (mp:process-interrupt thread fn))
748
749 (defimplementation kill-thread (thread)
750 (mp:process-kill thread))
751
752 (defimplementation thread-alive-p (thread)
753 (mp:process-alive-p thread))
754
755 (defstruct (mailbox (:conc-name mailbox.))
756 (mutex (mp:make-lock :name "thread mailbox"))
757 (queue '() :type list))
758
759 (defvar *mailbox-lock* (mp:make-lock))
760
761 (defun mailbox (thread)
762 (mp:with-lock (*mailbox-lock*)
763 (or (getf (mp:process-plist thread) 'mailbox)
764 (setf (getf (mp:process-plist thread) 'mailbox)
765 (make-mailbox)))))
766
767 (defimplementation receive-if (test &optional timeout)
768 (let* ((mbox (mailbox mp:*current-process*))
769 (lock (mailbox.mutex mbox)))
770 (assert (or (not timeout) (eq timeout t)))
771 (loop
772 (check-slime-interrupts)
773 (mp:with-lock (lock "receive-if/try")
774 (let* ((q (mailbox.queue mbox))
775 (tail (member-if test q)))
776 (when tail
777 (setf (mailbox.queue mbox) (nconc (ldiff q tail) (cdr tail)))
778 (return (car tail)))))
779 (when (eq timeout t) (return (values nil t)))
780 (mp:process-wait-with-timeout
781 "receive-if" 0.2 (lambda () (some test (mailbox.queue mbox)))))))
782
783 (defimplementation send (thread message)
784 (let ((mbox (mailbox thread)))
785 (mp:with-lock ((mailbox.mutex mbox))
786 (setf (mailbox.queue mbox)
787 (nconc (mailbox.queue mbox) (list message))))))
788
789 ;;; Some intergration with the lispworks environment
790
791 (defun swank-sym (name) (find-symbol (string name) :swank))
792
793 (defimplementation emacs-connected ()
794 (when (eq (eval (swank-sym :*communication-style*))
795 nil)
796 (set-sigint-handler)))
797
798
799 ;;;; Weak hashtables
800
801 (defimplementation make-weak-key-hash-table (&rest args)
802 (apply #'make-hash-table :weak-kind :key args))
803
804 (defimplementation make-weak-value-hash-table (&rest args)
805 (apply #'make-hash-table :weak-kind :value args))

  ViewVC Help
Powered by ViewVC 1.1.5