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

Contents of /slime/swank-lispworks.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.117 - (hide annotations)
Wed Sep 17 06:19:49 2008 UTC (5 years, 7 months ago) by heller
Branch: MAIN
Changes since 1.116: +14 -7 lines
Adjust positions in files with CRLF-style end-on-line markers.

* slime.el (slime-eol-conversion-fixup): New function.
(slime-goto-location-position): Use it.  Also add a new position
type :offset, so that we don't adjust offsets in strings that were
sent over the wire (which uses LF eol-convention).

* swank-abcl.lisp
* swank-allegro.lisp
* swank-clisp.lisp
* swank-cmucl.lisp
* swank-corman.lisp
* swank-ecl.lisp
* swank-lispworks.lisp
* swank-openmcl.lisp
* swank-sbcl.lisp
* swank-scl.lisp: Create :offset style positions where needed.

* swank-lispworks.lisp (skip-comments): New function.
(dspec-stream-position): Use it.
1 heller 1.74 ;;; -*- indent-tabs-mode: nil -*-
2 heller 1.1 ;;;
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 heller 1.30 (in-package :swank-backend)
12 heller 1.1
13     (eval-when (:compile-toplevel :load-toplevel :execute)
14 heller 1.79 (require "comm")
15     (import-from :stream *gray-stream-symbols* :swank-backend))
16 heller 1.1
17 heller 1.62 (import-swank-mop-symbols :clos '(:slot-definition-documentation
18     :eql-specializer
19 heller 1.65 :eql-specializer-object
20     :compute-applicable-methods-using-classes))
21 msimmons 1.57
22     (defun swank-mop:slot-definition-documentation (slot)
23     (documentation slot t))
24 mbaringer 1.59
25 heller 1.65 (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 mbaringer 1.59 (deftype swank-mop:eql-specializer () 'cons)
31    
32     (defun swank-mop:eql-specializer-object (eql-spec)
33     (second eql-spec))
34 msimmons 1.57
35 heller 1.111 (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 heller 1.49
45 heller 1.13 ;;; TCP server
46 lgorrie 1.12
47 heller 1.30 (defimplementation preferred-communication-style ()
48     :spawn)
49 heller 1.18
50 heller 1.13 (defun socket-fd (socket)
51     (etypecase socket
52     (fixnum socket)
53     (comm:socket-stream (comm:socket-stream-socket socket))))
54    
55 heller 1.22 (defimplementation create-socket (host port)
56 heller 1.13 (multiple-value-bind (socket where errno)
57 msimmons 1.64 #-(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 heller 1.13 (cond (socket socket)
62 heller 1.14 (t (error 'network-error
63 heller 1.13 :format-control "~A failed: ~A (~D)"
64     :format-arguments (list where
65     (list #+unix (lw:get-unix-error errno))
66 heller 1.14 errno))))))
67 heller 1.13
68 lgorrie 1.17 (defimplementation local-port (socket)
69 heller 1.13 (nth-value 1 (comm:get-socket-address (socket-fd socket))))
70    
71 lgorrie 1.17 (defimplementation close-socket (socket)
72 heller 1.13 (comm::close-socket (socket-fd socket)))
73    
74 heller 1.63 (defimplementation accept-connection (socket
75 heller 1.85 &key external-format buffering timeout)
76 heller 1.88 (declare (ignore buffering timeout external-format))
77 heller 1.61 (let* ((fd (comm::get-fd-from-socket socket)))
78 heller 1.13 (assert (/= fd -1))
79     (make-instance 'comm:socket-stream :socket fd :direction :io
80     :element-type 'base-char)))
81    
82 heller 1.37 (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 heller 1.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 heller 1.15 ;;; Unix signals
107    
108 heller 1.18 (defun sigint-handler ()
109 heller 1.16 (with-simple-restart (continue "Continue from SIGINT handler.")
110     (invoke-debugger "SIGINT")))
111 heller 1.1
112 heller 1.18 (defun make-sigint-handler (process)
113     (lambda (&rest args)
114     (declare (ignore args))
115     (mp:process-interrupt process #'sigint-handler)))
116    
117 heller 1.38 (defimplementation call-without-interrupts (fn)
118 heller 1.37 (lw:without-interrupts (funcall fn)))
119 heller 1.48
120 heller 1.29 (defimplementation getpid ()
121     #+win32 (win32:get-current-process-id)
122     #-win32 (system::getpid))
123 heller 1.1
124 heller 1.23 (defimplementation lisp-implementation-type-name ()
125     "lispworks")
126    
127 heller 1.36 (defimplementation set-default-directory (directory)
128     (namestring (hcl:change-directory directory)))
129 heller 1.40
130     ;;;; Documentation
131 heller 1.36
132 msimmons 1.57 (defimplementation arglist (symbol-or-function)
133     (let ((arglist (lw:function-lambda-list symbol-or-function)))
134 heller 1.30 (etypecase arglist
135 heller 1.39 ((member :dont-know)
136     :not-available)
137     (list
138     arglist))))
139 msimmons 1.57
140     (defimplementation function-name (function)
141     (nth-value 2 (function-lambda-expression function)))
142 heller 1.1
143 lgorrie 1.17 (defimplementation macroexpand-all (form)
144 heller 1.1 (walker:walk-form form))
145    
146 heller 1.37 (defun generic-function-p (object)
147 heller 1.36 (typep object 'generic-function))
148    
149 lgorrie 1.17 (defimplementation describe-symbol-for-emacs (symbol)
150 heller 1.1 "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 heller 1.116 (let ((string (or (documentation sym kind))))
158 heller 1.1 (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 heller 1.36 :generic-function (if (and (fboundp symbol)
169 heller 1.37 (generic-function-p (fdefinition symbol)))
170 heller 1.36 (doc 'function)))
171     (maybe-push
172     :function (if (and (fboundp symbol)
173 heller 1.37 (not (generic-function-p (fdefinition symbol))))
174 heller 1.1 (doc 'function)))
175     (maybe-push
176 heller 1.49 :setf (let ((setf-name (sys:underlying-setf-name `(setf ,symbol))))
177     (if (fboundp setf-name)
178     (doc 'setf))))
179     (maybe-push
180 heller 1.1 :class (if (find-class symbol nil)
181     (doc 'class)))
182 heller 1.30 result)))
183 heller 1.1
184 heller 1.30 (defimplementation describe-definition (symbol type)
185     (ecase type
186     (:variable (describe-symbol symbol))
187     (:class (describe (find-class symbol)))
188 heller 1.49 ((:function :generic-function) (describe-function symbol))
189     (:setf (describe-function (sys:underlying-setf-name `(setf ,symbol))))))
190 heller 1.30
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 heller 1.38 (describe (fdefinition symbol)))
199 heller 1.30 (t (format t "~S is not fbound" symbol))))
200 heller 1.4
201 heller 1.30 (defun describe-symbol (sym)
202 heller 1.1 (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 heller 1.30 (describe-function sym)))
210 heller 1.1
211     ;;; Debugging
212    
213 heller 1.66 (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 heller 1.111 (defmethod env-internals:environment-display-notifier
222 heller 1.66 ((env slime-env) &key restarts condition)
223 heller 1.111 (declare (ignore restarts condition))
224 heller 1.113 (funcall (swank-sym :swank-debugger-hook) condition *debugger-hook*)
225     ;; nil
226 heller 1.111 )
227 heller 1.66
228     (defmethod env-internals:environment-display-debugger ((env slime-env))
229     *debug-io*)
230    
231 heller 1.111 (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 heller 1.110 (defimplementation call-with-debugger-hook (hook fun)
235     (let ((*debugger-hook* hook))
236     (env:with-environment ((slime-env hook '()))
237     (funcall fun))))
238 heller 1.66
239 msimmons 1.100 (defimplementation install-debugger-globally (function)
240     (setq *debugger-hook* function)
241     (setf (env:environment) (slime-env function '())))
242    
243 heller 1.26 (defvar *sldb-top-frame*)
244 heller 1.1
245     (defun interesting-frame-p (frame)
246 heller 1.36 (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 heller 1.1
258 heller 1.41 (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 heller 1.48 ((or (not frame)
263     (and (interesting-frame-p frame) (zerop i)))
264     frame)))
265 heller 1.41
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 mbaringer 1.69 (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 heller 1.41
281     (defimplementation call-with-debugging-environment (fn)
282     (dbg::with-debugger-stack ()
283     (let ((*sldb-top-frame* (find-top-frame)))
284     (funcall fn))))
285 heller 1.1
286 heller 1.30 (defimplementation compute-backtrace (start end)
287 heller 1.1 (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 trittweiler 1.114 (push (make-swank-frame :%frame frame :restartable :unknown)
295     backtrace)))))
296 heller 1.1
297 heller 1.36 (defun frame-actual-args (frame)
298 heller 1.50 (let ((*break-on-signals* nil))
299     (mapcar (lambda (arg)
300     (case arg
301     ((&rest &optional &key) arg)
302     (t
303     (handler-case (dbg::dbg-eval arg frame)
304 msimmons 1.53 (error (e) (format nil "<~A>" arg))))))
305 heller 1.50 (dbg::call-frame-arglist frame))))
306 heller 1.36
307 trittweiler 1.114 (defimplementation print-swank-frame (swank-frame stream)
308     (let ((frame (swank-frame.%frame swank-frame)))
309     (cond ((dbg::call-frame-p frame)
310     (format stream "~S ~S"
311     (dbg::call-frame-function-name frame)
312     (frame-actual-args frame)))
313     (t (princ frame stream)))))
314 heller 1.1
315 heller 1.47 (defun frame-vars (frame)
316     (first (dbg::frame-locals-format-list frame #'list 75 0)))
317    
318 lgorrie 1.17 (defimplementation frame-locals (n)
319 heller 1.29 (let ((frame (nth-frame n)))
320 heller 1.1 (if (dbg::call-frame-p frame)
321 heller 1.47 (mapcar (lambda (var)
322     (destructuring-bind (name value symbol location) var
323     (declare (ignore name location))
324     (list :name symbol :id 0
325     :value value)))
326     (frame-vars frame)))))
327    
328     (defimplementation frame-var-value (frame var)
329     (let ((frame (nth-frame frame)))
330     (destructuring-bind (_n value _s _l) (nth var (frame-vars frame))
331     (declare (ignore _n _s _l))
332     value)))
333 heller 1.1
334 lgorrie 1.17 (defimplementation frame-catch-tags (index)
335 heller 1.1 (declare (ignore index))
336     nil)
337    
338 lgorrie 1.17 (defimplementation frame-source-location-for-emacs (frame)
339 heller 1.60 (let ((frame (nth-frame frame))
340     (callee (if (plusp frame) (nth-frame (1- frame)))))
341 heller 1.1 (if (dbg::call-frame-p frame)
342 heller 1.60 (let ((dspec (dbg::call-frame-function-name frame))
343     (cname (and (dbg::call-frame-p callee)
344     (dbg::call-frame-function-name callee))))
345     (if dspec
346     (frame-location dspec cname))))))
347 heller 1.26
348     (defimplementation eval-in-frame (form frame-number)
349     (let ((frame (nth-frame frame-number)))
350     (dbg::dbg-eval form frame)))
351    
352     (defimplementation return-from-frame (frame-number form)
353     (let* ((frame (nth-frame frame-number))
354 heller 1.30 (return-frame (dbg::find-frame-for-return frame)))
355 heller 1.26 (dbg::dbg-return-from-call-frame frame form return-frame
356     dbg::*debugger-stack*)))
357    
358     (defimplementation restart-frame (frame-number)
359     (let ((frame (nth-frame frame-number)))
360     (dbg::restart-frame frame :same-args t)))
361 heller 1.1
362 heller 1.109 (defimplementation disassemble-frame (frame-number)
363     (let* ((frame (nth-frame frame-number)))
364     (when (dbg::call-frame-p frame)
365     (let ((function (dbg::get-call-frame-function frame)))
366     (disassemble function)))))
367    
368 heller 1.18 ;;; Definition finding
369    
370 heller 1.60 (defun frame-location (dspec callee-name)
371     (let ((infos (dspec:find-dspec-locations dspec)))
372     (cond (infos
373     (destructuring-bind ((rdspec location) &rest _) infos
374     (declare (ignore _))
375     (let ((name (and callee-name (symbolp callee-name)
376     (string callee-name))))
377     (make-dspec-location rdspec location
378     `(:call-site ,name)))))
379     (t
380     (list :error (format nil "Source location not available for: ~S"
381     dspec))))))
382 heller 1.4
383 heller 1.33 (defimplementation find-definitions (name)
384 heller 1.26 (let ((locations (dspec:find-name-locations dspec:*dspec-classes* name)))
385 heller 1.33 (loop for (dspec location) in locations
386     collect (list dspec (make-dspec-location dspec location)))))
387 heller 1.6
388 msimmons 1.56
389 heller 1.18 ;;; Compilation
390    
391 msimmons 1.42 (defmacro with-swank-compilation-unit ((location &rest options) &body body)
392     (lw:rebinding (location)
393     `(let ((compiler::*error-database* '()))
394     (with-compilation-unit ,options
395     ,@body
396     (signal-error-data-base compiler::*error-database* ,location)
397     (signal-undefined-functions compiler::*unknown-functions* ,location)))))
398    
399 heller 1.88 (defimplementation swank-compile-file (filename load-p external-format)
400 msimmons 1.42 (with-swank-compilation-unit (filename)
401 heller 1.88 (compile-file filename :load load-p :external-format external-format)))
402 msimmons 1.56
403 eweitz 1.70 (defvar *within-call-with-compilation-hooks* nil
404     "Whether COMPILE-FILE was called from within CALL-WITH-COMPILATION-HOOKS.")
405    
406     (defvar *undefined-functions-hash* nil
407     "Hash table to map info about undefined functions to pathnames.")
408    
409     (lw:defadvice (compile-file compile-file-and-collect-notes :around)
410     (pathname &rest rest)
411 msimmons 1.75 (multiple-value-prog1 (apply #'lw:call-next-advice pathname rest)
412 eweitz 1.70 (when *within-call-with-compilation-hooks*
413     (maphash (lambda (unfun dspecs)
414     (dolist (dspec dspecs)
415     (let ((unfun-info (list unfun dspec)))
416     (unless (gethash unfun-info *undefined-functions-hash*)
417     (setf (gethash unfun-info *undefined-functions-hash*)
418     pathname)))))
419     compiler::*unknown-functions*))))
420    
421 msimmons 1.56 (defimplementation call-with-compilation-hooks (function)
422 eweitz 1.70 (let ((compiler::*error-database* '())
423     (*undefined-functions-hash* (make-hash-table :test 'equal))
424     (*within-call-with-compilation-hooks* t))
425     (with-compilation-unit ()
426     (prog1 (funcall function)
427     (signal-error-data-base compiler::*error-database*)
428     (signal-undefined-functions compiler::*unknown-functions*)))))
429 heller 1.1
430     (defun map-error-database (database fn)
431     (loop for (filename . defs) in database do
432     (loop for (dspec . conditions) in defs do
433     (dolist (c conditions)
434 msimmons 1.98 (funcall fn filename dspec (if (consp c) (car c) c))))))
435 heller 1.1
436     (defun lispworks-severity (condition)
437     (cond ((not condition) :warning)
438     (t (etypecase condition
439 heller 1.6 (error :error)
440 heller 1.1 (style-warning :warning)
441     (warning :warning)))))
442    
443     (defun signal-compiler-condition (message location condition)
444     (check-type message string)
445     (signal
446     (make-instance 'compiler-condition :message message
447     :severity (lispworks-severity condition)
448     :location location
449     :original-condition condition)))
450    
451     (defun compile-from-temp-file (string filename)
452     (unwind-protect
453     (progn
454     (with-open-file (s filename :direction :output :if-exists :supersede)
455     (write-string string s)
456     (finish-output s))
457     (let ((binary-filename (compile-file filename :load t)))
458 heller 1.6 (when binary-filename
459     (delete-file binary-filename))))
460 heller 1.1 (delete-file filename)))
461    
462 heller 1.117 (defun dspec-function-name-position (dspec fallback)
463 heller 1.34 (etypecase dspec
464     (cons (let ((name (dspec:dspec-primary-name dspec)))
465 heller 1.37 (typecase name
466 heller 1.34 ((or symbol string)
467     (list :function-name (string name)))
468 heller 1.117 (t fallback))))
469     (null fallback)
470 heller 1.34 (symbol (list :function-name (string dspec)))))
471 heller 1.18
472 lgorrie 1.45 (defmacro with-fairly-standard-io-syntax (&body body)
473     "Like WITH-STANDARD-IO-SYNTAX but preserve *PACKAGE* and *READTABLE*."
474     (let ((package (gensym))
475     (readtable (gensym)))
476     `(let ((,package *package*)
477     (,readtable *readtable*))
478     (with-standard-io-syntax
479     (let ((*package* ,package)
480     (*readtable* ,readtable))
481     ,@body)))))
482    
483 heller 1.117 (defun skip-comments (stream)
484     (let ((pos0 (file-position stream)))
485     (cond ((equal (ignore-errors (list (read-delimited-list #\( stream)))
486     '(()))
487     (file-position stream (1- (file-position stream))))
488     (t (file-position stream pos0)))))
489    
490 heller 1.52 #-(or lispworks4.1 lispworks4.2) ; no dspec:parse-form-dspec prior to 4.3
491 msimmons 1.42 (defun dspec-stream-position (stream dspec)
492 lgorrie 1.45 (with-fairly-standard-io-syntax
493 heller 1.117 (loop (let* ((pos (progn (skip-comments stream) (file-position stream)))
494 lgorrie 1.44 (form (read stream nil '#1=#:eof)))
495     (when (eq form '#1#)
496     (return nil))
497     (labels ((check-dspec (form)
498     (when (consp form)
499     (let ((operator (car form)))
500     (case operator
501     ((progn)
502     (mapcar #'check-dspec
503     (cdr form)))
504     ((eval-when locally macrolet symbol-macrolet)
505     (mapcar #'check-dspec
506     (cddr form)))
507     ((in-package)
508     (let ((package (find-package (second form))))
509     (when package
510     (setq *package* package))))
511     (otherwise
512     (let ((form-dspec (dspec:parse-form-dspec form)))
513     (when (dspec:dspec-equal dspec form-dspec)
514     (return pos)))))))))
515     (check-dspec form))))))
516 msimmons 1.42
517 heller 1.52 (defun dspec-file-position (file dspec)
518 mbaringer 1.90 (let* ((*compile-file-pathname* (pathname file))
519     (*compile-file-truename* (truename *compile-file-pathname*))
520     (*load-pathname* *compile-file-pathname*)
521     (*load-truename* *compile-file-truename*))
522     (with-open-file (stream file)
523     (let ((pos
524     #-(or lispworks4.1 lispworks4.2)
525     (dspec-stream-position stream dspec)))
526     (if pos
527 heller 1.117 (list :position (1+ pos))
528     (dspec-function-name-position dspec `(:position 1)))))))
529 heller 1.52
530 heller 1.25 (defun emacs-buffer-location-p (location)
531     (and (consp location)
532     (eq (car location) :emacs-buffer)))
533    
534 heller 1.60 (defun make-dspec-location (dspec location &optional hints)
535 heller 1.52 (etypecase location
536     ((or pathname string)
537     (multiple-value-bind (file err)
538     (ignore-errors (namestring (truename location)))
539     (if err
540     (list :error (princ-to-string err))
541     (make-location `(:file ,file)
542 heller 1.60 (dspec-file-position file dspec)
543     hints))))
544 heller 1.52 (symbol
545     `(:error ,(format nil "Cannot resolve location: ~S" location)))
546     ((satisfies emacs-buffer-location-p)
547     (destructuring-bind (_ buffer offset string) location
548     (declare (ignore _ string))
549     (make-location `(:buffer ,buffer)
550 heller 1.117 (dspec-function-name-position dspec `(:offset ,offset 0))
551 heller 1.60 hints)))))
552 heller 1.1
553 msimmons 1.42 (defun make-dspec-progenitor-location (dspec location)
554     (let ((canon-dspec (dspec:canonicalize-dspec dspec)))
555     (make-dspec-location
556     (if canon-dspec
557     (if (dspec:local-dspec-p canon-dspec)
558     (dspec:dspec-progenitor canon-dspec)
559     canon-dspec)
560     nil)
561     location)))
562    
563 eweitz 1.70 (defun signal-error-data-base (database &optional location)
564 heller 1.1 (map-error-database
565     database
566     (lambda (filename dspec condition)
567     (signal-compiler-condition
568     (format nil "~A" condition)
569 eweitz 1.70 (make-dspec-progenitor-location dspec (or location filename))
570 heller 1.1 condition))))
571    
572 eweitz 1.71 (defun unmangle-unfun (symbol)
573     "Converts symbols like 'SETF::|\"CL-USER\" \"GET\"| to
574     function names like \(SETF GET)."
575 heller 1.93 (cond ((sys::setf-symbol-p symbol)
576     (sys::setf-pair-from-underlying-name symbol))
577     (t symbol)))
578    
579 eweitz 1.70 (defun signal-undefined-functions (htab &optional filename)
580 heller 1.1 (maphash (lambda (unfun dspecs)
581     (dolist (dspec dspecs)
582     (signal-compiler-condition
583 eweitz 1.71 (format nil "Undefined function ~A" (unmangle-unfun unfun))
584 eweitz 1.70 (make-dspec-progenitor-location dspec
585     (or filename
586     (gethash (list unfun dspec)
587     *undefined-functions-hash*)))
588 heller 1.1 nil)))
589     htab))
590 heller 1.2
591 heller 1.99 (defimplementation swank-compile-string (string &key buffer position directory
592     debug)
593     (declare (ignore directory debug))
594 heller 1.1 (assert buffer)
595     (assert position)
596 heller 1.30 (let* ((location (list :emacs-buffer buffer position string))
597 heller 1.26 (tmpname (hcl:make-temp-file nil "lisp")))
598 msimmons 1.42 (with-swank-compilation-unit (location)
599 heller 1.26 (compile-from-temp-file
600 heller 1.68 (with-output-to-string (s)
601     (let ((*print-radix* t))
602     (print `(eval-when (:compile-toplevel)
603     (setq dspec::*location* (list ,@location)))
604     s))
605     (write-string string s))
606 msimmons 1.42 tmpname))))
607 heller 1.1
608 heller 1.3 ;;; xref
609    
610 heller 1.31 (defmacro defxref (name function)
611     `(defimplementation ,name (name)
612     (xref-results (,function name))))
613    
614     (defxref who-calls hcl:who-calls)
615 msimmons 1.42 (defxref who-macroexpands hcl:who-calls) ; macros are in the calls table too
616 heller 1.67 (defxref calls-who hcl:calls-who)
617 msimmons 1.42 (defxref list-callers list-callers-internal)
618 heller 1.68 ;; (defxref list-callees list-callees-internal)
619 msimmons 1.42
620     (defun list-callers-internal (name)
621     (let ((callers (make-array 100
622     :fill-pointer 0
623     :adjustable t)))
624     (hcl:sweep-all-objects
625     #'(lambda (object)
626     (when (and #+Harlequin-PC-Lisp (low:compiled-code-p object)
627     #-Harlequin-PC-Lisp (sys::callablep object)
628     (system::find-constant$funcallable name object))
629     (vector-push-extend object callers))))
630     ;; Delay dspec:object-dspec until after sweep-all-objects
631     ;; to reduce allocation problems.
632     (loop for object across callers
633     collect (if (symbolp object)
634     (list 'function object)
635 heller 1.63 (or (dspec:object-dspec object) object)))))
636 heller 1.36
637 heller 1.37 ;; only for lispworks 4.2 and above
638     #-lispworks4.1
639     (progn
640     (defxref who-references hcl:who-references)
641     (defxref who-binds hcl:who-binds)
642     (defxref who-sets hcl:who-sets))
643    
644 heller 1.36 (defimplementation who-specializes (classname)
645     (let ((methods (clos:class-direct-methods (find-class classname))))
646     (xref-results (mapcar #'dspec:object-dspec methods))))
647 heller 1.31
648     (defun xref-results (dspecs)
649 heller 1.63 (flet ((frob-locs (dspec locs)
650     (cond (locs
651     (loop for (name loc) in locs
652     collect (list name (make-dspec-location name loc))))
653     (t `((,dspec (:error "Source location not available")))))))
654     (loop for dspec in dspecs
655     append (frob-locs dspec (dspec:dspec-definition-locations dspec)))))
656 heller 1.66
657 heller 1.25 ;;; Inspector
658 mbaringer 1.58
659 heller 1.95 (defmethod emacs-inspect ((o t))
660 heller 1.60 (lispworks-inspect o))
661    
662 heller 1.95 (defmethod emacs-inspect ((o function))
663 heller 1.60 (lispworks-inspect o))
664    
665 heller 1.82 ;; FIXME: slot-boundp-using-class in LW works with names so we can't
666     ;; use our method in swank.lisp.
667 heller 1.95 (defmethod emacs-inspect ((o standard-object))
668 heller 1.82 (lispworks-inspect o))
669    
670 heller 1.60 (defun lispworks-inspect (o)
671 heller 1.25 (multiple-value-bind (names values _getter _setter type)
672     (lw:get-inspector-values o nil)
673     (declare (ignore _getter _setter))
674 heller 1.60 (append
675     (label-value-line "Type" type)
676 msimmons 1.91 (loop for name in names
677     for value in values
678 heller 1.96 append (label-value-line name value)))))
679 heller 1.25
680 lgorrie 1.46 ;;; Miscellaneous
681    
682     (defimplementation quit-lisp ()
683     (lispworks:quit))
684    
685 heller 1.66 ;;; Tracing
686    
687     (defun parse-fspec (fspec)
688     "Return a dspec for FSPEC."
689     (ecase (car fspec)
690 heller 1.67 ((:defmethod) `(method ,(cdr fspec)))))
691 heller 1.66
692     (defun tracedp (dspec)
693     (member dspec (eval '(trace)) :test #'equal))
694    
695 heller 1.67 (defun toggle-trace-aux (dspec)
696 heller 1.66 (cond ((tracedp dspec)
697     (eval `(untrace ,dspec))
698     (format nil "~S is now untraced." dspec))
699     (t
700     (eval `(trace (,dspec)))
701     (format nil "~S is now traced." dspec))))
702    
703 heller 1.67 (defimplementation toggle-trace (fspec)
704     (toggle-trace-aux (parse-fspec fspec)))
705 heller 1.66
706 heller 1.16 ;;; Multithreading
707    
708 mbaringer 1.86 (defimplementation initialize-multiprocessing (continuation)
709 heller 1.87 (cond ((not mp::*multiprocessing*)
710     (push (list "Initialize SLIME" '() continuation)
711     mp:*initial-processes*)
712     (mp:initialize-multiprocessing))
713     (t (funcall continuation))))
714 heller 1.16
715 heller 1.18 (defimplementation spawn (fn &key name)
716 heller 1.38 (let ((mp:*process-initial-bindings*
717     (remove (find-package :cl)
718     mp:*process-initial-bindings*
719     :key (lambda (x) (symbol-package (car x))))))
720     (mp:process-run-function name () fn)))
721 heller 1.48
722     (defvar *id-lock* (mp:make-lock))
723     (defvar *thread-id-counter* 0)
724    
725     (defimplementation thread-id (thread)
726     (mp:with-lock (*id-lock*)
727     (or (getf (mp:process-plist thread) 'id)
728     (setf (getf (mp:process-plist thread) 'id)
729     (incf *thread-id-counter*)))))
730    
731     (defimplementation find-thread (id)
732     (find id (mp:list-all-processes)
733     :key (lambda (p) (getf (mp:process-plist p) 'id))))
734 heller 1.16
735 heller 1.21 (defimplementation thread-name (thread)
736     (mp:process-name thread))
737 heller 1.16
738 heller 1.21 (defimplementation thread-status (thread)
739     (format nil "~A ~D"
740     (mp:process-whostate thread)
741     (mp:process-priority thread)))
742 heller 1.16
743 heller 1.18 (defimplementation make-lock (&key name)
744 heller 1.16 (mp:make-lock :name name))
745    
746 heller 1.18 (defimplementation call-with-lock-held (lock function)
747 heller 1.16 (mp:with-lock (lock) (funcall function)))
748 heller 1.20
749     (defimplementation current-thread ()
750     mp:*current-process*)
751 heller 1.21
752     (defimplementation all-threads ()
753     (mp:list-all-processes))
754 heller 1.20
755     (defimplementation interrupt-thread (thread fn)
756     (mp:process-interrupt thread fn))
757 heller 1.25
758     (defimplementation kill-thread (thread)
759     (mp:process-kill thread))
760 heller 1.27
761     (defimplementation thread-alive-p (thread)
762     (mp:process-alive-p thread))
763 heller 1.20
764 heller 1.102 (defstruct (mailbox (:conc-name mailbox.))
765     (mutex (mp:make-lock :name "thread mailbox"))
766     (queue '() :type list))
767    
768 heller 1.20 (defvar *mailbox-lock* (mp:make-lock))
769    
770     (defun mailbox (thread)
771     (mp:with-lock (*mailbox-lock*)
772     (or (getf (mp:process-plist thread) 'mailbox)
773     (setf (getf (mp:process-plist thread) 'mailbox)
774 heller 1.102 (make-mailbox)))))
775 heller 1.20
776 heller 1.112 (defimplementation receive-if (test &optional timeout)
777 heller 1.102 (let* ((mbox (mailbox mp:*current-process*))
778     (lock (mailbox.mutex mbox)))
779 heller 1.112 (assert (or (not timeout) (eq timeout t)))
780 heller 1.102 (loop
781 heller 1.106 (check-slime-interrupts)
782     (mp:with-lock (lock "receive-if/try")
783     (let* ((q (mailbox.queue mbox))
784     (tail (member-if test q)))
785     (when tail
786     (setf (mailbox.queue mbox) (nconc (ldiff q tail) (cdr tail)))
787     (return (car tail)))))
788 heller 1.112 (when (eq timeout t) (return (values nil t)))
789 heller 1.106 (mp:process-wait-with-timeout
790     "receive-if" 0.2 (lambda () (some test (mailbox.queue mbox)))))))
791 heller 1.102
792     (defimplementation send (thread message)
793     (let ((mbox (mailbox thread)))
794 heller 1.106 (mp:with-lock ((mailbox.mutex mbox))
795     (setf (mailbox.queue mbox)
796     (nconc (mailbox.queue mbox) (list message))))))
797 heller 1.3
798 heller 1.66 ;;; Some intergration with the lispworks environment
799    
800 heller 1.74 (defun swank-sym (name) (find-symbol (string name) :swank))
801 heller 1.66
802     (defimplementation emacs-connected ()
803     (when (eq (eval (swank-sym :*communication-style*))
804     nil)
805 heller 1.110 (set-sigint-handler)))
806 mkoeppe 1.77
807 eweitz 1.89
808     ;;;; Weak hashtables
809    
810     (defimplementation make-weak-key-hash-table (&rest args)
811     (apply #'make-hash-table :weak-kind :key args))
812    
813     (defimplementation make-weak-value-hash-table (&rest args)
814     (apply #'make-hash-table :weak-kind :value args))

  ViewVC Help
Powered by ViewVC 1.1.5