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

Contents of /slime/swank-corman.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations)
Tue Jun 7 10:08:03 2005 UTC (8 years, 10 months ago) by ewiborg
Branch: MAIN
Changes since 1.1: +479 -477 lines
Convert to Unix line-endings.
(create-socket): Pass through the port argument unmodified,
gettting a random port if 0.  Requires supporting change in
<ccl>/modules/sockets.lisp.
(inspect-for-emacs): defimplementation instead of defmethod.
1 ewiborg 1.2 ;;;
2     ;;; swank-corman.lisp --- Corman Lisp specific code for SLIME.
3     ;;;
4     ;;; Copyright (C) 2004, 2005 Espen Wiborg (espenhw@grumblesmurf.org)
5     ;;;
6     ;;; License
7     ;;; =======
8     ;;; This software is provided 'as-is', without any express or implied
9     ;;; warranty. In no event will the author be held liable for any damages
10     ;;; arising from the use of this software.
11     ;;;
12     ;;; Permission is granted to anyone to use this software for any purpose,
13     ;;; including commercial applications, and to alter it and redistribute
14     ;;; it freely, subject to the following restrictions:
15     ;;;
16     ;;; 1. The origin of this software must not be misrepresented; you must
17     ;;; not claim that you wrote the original software. If you use this
18     ;;; software in a product, an acknowledgment in the product documentation
19     ;;; would be appreciated but is not required.
20     ;;;
21     ;;; 2. Altered source versions must be plainly marked as such, and must
22     ;;; not be misrepresented as being the original software.
23     ;;;
24     ;;; 3. This notice may not be removed or altered from any source
25     ;;; distribution.
26     ;;;
27     ;;; Notes
28     ;;; =====
29     ;;; You will need CCL 2.51, and you will *definitely* need to patch
30     ;;; CCL with the patches at
31     ;;; http://www.grumblesmurf.org/lisp/corman-patches, otherwise SLIME
32     ;;; will blow up in your face. You should also follow the
33     ;;; instructions on http://www.grumblesmurf.org/lisp/corman-slime.
34     ;;;
35     ;;; The only communication style currently supported is NIL.
36     ;;;
37     ;;; Starting CCL inside emacs (with M-x slime) seems to work for me
38     ;;; with Corman Lisp 2.51, but I have seen random failures with 2.5
39     ;;; (sometimes it works, other times it hangs on start or hangs when
40     ;;; initializing WinSock) - starting CCL externally and using M-x
41     ;;; slime-connect always works fine.
42     ;;;
43     ;;; Sometimes CCL gets confused and starts giving you random memory
44     ;;; access violation errors on startup; if this happens, try redumping
45     ;;; your image.
46     ;;;
47     ;;; What works
48     ;;; ==========
49     ;;; * Basic editing and evaluation
50     ;;; * Arglist display
51     ;;; * Compilation
52     ;;; * Loading files
53     ;;; * apropos/describe
54     ;;; * Debugger
55     ;;; * Inspector
56     ;;;
57     ;;; TODO
58     ;;; ====
59     ;;; * More debugger functionality (missing bits: restart-frame,
60     ;;; return-from-frame, disassemble-frame, activate-stepping,
61     ;;; toggle-trace)
62     ;;; * XREF
63     ;;; * Profiling
64     ;;; * More sophisticated communication styles than NIL
65     ;;;
66    
67     (in-package :swank-backend)
68    
69     ;;; Pull in various needed bits
70     (require :composite-streams)
71     (require :sockets)
72     (require :winbase)
73     (require :lp)
74    
75     (use-package :gs)
76    
77     ;; MOP stuff
78    
79     (defclass swank-mop:standard-slot-definition ()
80     ()
81     (:documentation "Dummy class created so that swank.lisp will compile and load."))
82    
83     (defun named-by-gensym-p (c)
84     (null (symbol-package (class-name c))))
85    
86     (deftype swank-mop:eql-specializer ()
87     '(satisfies named-by-gensym-p))
88    
89     (defun swank-mop:eql-specializer-object (specializer)
90     (with-hash-table-iterator (next-entry cl::*clos-singleton-specializers*)
91     (loop (multiple-value-bind (more key value)
92     (next-entry)
93     (unless more (return nil))
94     (when (eq specializer value)
95     (return key))))))
96    
97     (defun swank-mop:class-finalized-p (class)
98     (declare (ignore class))
99     t)
100    
101     (defun swank-mop:class-prototype (class)
102     (make-instance class))
103    
104     (defun swank-mop:specializer-direct-methods (obj)
105     (declare (ignore obj))
106     nil)
107    
108     (defun swank-mop:generic-function-argument-precedence-order (gf)
109     (generic-function-lambda-list gf))
110    
111     (defun swank-mop:generic-function-method-combination (gf)
112     (declare (ignore gf))
113     :standard)
114    
115     (defun swank-mop:generic-function-declarations (gf)
116     (declare (ignore gf))
117     nil)
118    
119     (defun swank-mop:slot-definition-documentation (slot)
120     (declare (ignore slot))
121     (getf slot :documentation nil))
122    
123     (defun swank-mop:slot-definition-type (slot)
124     (declare (ignore slot))
125     t)
126    
127     (import-swank-mop-symbols :cl '(;; classes
128     :standard-slot-definition
129     :eql-specializer
130     :eql-specializer-object
131     ;; standard class readers
132     :class-default-initargs
133     :class-direct-default-initargs
134     :class-finalized-p
135     :class-prototype
136     :specializer-direct-methods
137     ;; gf readers
138     :generic-function-argument-precedence-order
139     :generic-function-declarations
140     :generic-function-method-combination
141     ;; method readers
142     ;; slot readers
143     :slot-definition-documentation
144     :slot-definition-type))
145    
146     ;;;; swank implementations
147    
148     ;;; Debugger
149    
150     (defvar *stack-trace* nil)
151     (defvar *frame-trace* nil)
152    
153     (defstruct frame
154     name function address debug-info variables)
155    
156     (defimplementation call-with-debugging-environment (fn)
157     (let* ((real-stack-trace (cl::stack-trace))
158     (*stack-trace* (cdr (member 'cl:invoke-debugger real-stack-trace
159     :key #'car)))
160     (*frame-trace*
161     (let* ((db::*debug-level* 1)
162     (db::*debug-frame-pointer* (db::stash-ebp
163     (ct:create-foreign-ptr)))
164     (db::*debug-max-level* (length real-stack-trace))
165     (db::*debug-min-level* 1))
166     (cdr (member #'cl:invoke-debugger
167     (cons
168     (make-frame :function nil)
169     (loop for i from db::*debug-min-level*
170     upto db::*debug-max-level*
171     until (eq (db::get-frame-function i) cl::*top-level*)
172     collect
173     (make-frame :function (db::get-frame-function i)
174     :address (db::get-frame-address i))))
175     :key #'frame-function)))))
176     (funcall fn)))
177    
178     (defimplementation compute-backtrace (start end)
179     (subseq *stack-trace* start (min end (length *stack-trace*))))
180    
181     (defimplementation print-frame (frame stream)
182     (format stream "~S" frame))
183    
184     (defun get-frame-debug-info (frame)
185     (let ((info (frame-debug-info frame)))
186     (if info
187     info
188     (setf (frame-debug-info frame)
189     (db::prepare-frame-debug-info (frame-function frame)
190     (frame-address frame))))))
191    
192     (defimplementation frame-locals (frame-number)
193     (let* ((frame (elt *frame-trace* frame-number))
194     (info (get-frame-debug-info frame)))
195     (let ((var-list
196     (loop for i from 4 below (length info) by 2
197     collect `(list :name ',(svref info i) :id 0
198     :value (db::debug-filter ,(svref info i))))))
199     (let ((vars (eval-in-frame `(list ,@var-list) frame-number)))
200     (setf (frame-variables frame) vars)))))
201    
202     (defimplementation eval-in-frame (form frame-number)
203     (let ((frame (elt *frame-trace* frame-number)))
204     (let ((cl::*compiler-environment* (get-frame-debug-info frame)))
205     (eval form))))
206    
207     (defimplementation frame-catch-tags (index)
208     (declare (ignore index))
209     nil)
210    
211     (defimplementation frame-var-value (frame-number var)
212     (let ((vars (frame-variables (elt *frame-trace* frame-number))))
213     (when vars
214     (second (elt vars var)))))
215    
216     (defimplementation frame-source-location-for-emacs (frame-number)
217     (fspec-location (frame-function (elt *frame-trace* frame-number))))
218    
219     ;;; Socket communication
220    
221     (defimplementation create-socket (host port)
222     (sockets:start-sockets)
223     (sockets:make-server-socket :host host :port port))
224    
225     (defimplementation local-port (socket)
226     (sockets:socket-port socket))
227    
228     (defimplementation close-socket (socket)
229     (close socket))
230    
231     (defimplementation accept-connection (socket
232     &key (external-format :iso-latin-1-unix))
233     (ecase external-format
234     (:iso-latin-1-unix
235     (sockets:make-socket-stream (sockets:accept-socket socket)))))
236    
237     ;;; Misc
238    
239     (defimplementation preferred-communication-style ()
240     nil)
241    
242     (defimplementation getpid ()
243     ccl:*current-process-id*)
244    
245     (defimplementation lisp-implementation-type-name ()
246     "cormanlisp")
247    
248     (defimplementation quit-lisp ()
249     (sockets:stop-sockets)
250     (win32:exitprocess 0))
251    
252     (defimplementation set-default-directory (directory)
253     (setf (ccl:current-directory) directory)
254     (directory-namestring (setf *default-pathname-defaults*
255     (truename (merge-pathnames directory)))))
256    
257     (defimplementation default-directory ()
258     (ccl:current-directory))
259    
260     (defimplementation macroexpand-all (form)
261     (ccl:macroexpand-all form))
262    
263     ;;; Documentation
264    
265     (defun fspec-location (fspec)
266     (when (symbolp fspec)
267     (setq fspec (symbol-function fspec)))
268     (let ((file (ccl::function-source-file fspec)))
269     (if file
270     (handler-case
271     (let ((truename (truename
272     (merge-pathnames file
273     ccl:*cormanlisp-directory*))))
274     (make-location (list :file (namestring truename))
275     (if (ccl::function-source-line fspec)
276     (list :line (ccl::function-source-line fspec))
277     (list :function-name (princ-to-string
278     (function-name fspec))))))
279     (error (c) (list :error (princ-to-string c))))
280     (list :error (format nil "No source information available for ~S"
281     fspec)))))
282    
283     (defimplementation find-definitions (name)
284     (list (list name (fspec-location name))))
285    
286     (defimplementation arglist (name)
287     (handler-case
288     (cond ((and (symbolp name)
289     (macro-function name))
290     (ccl::macro-lambda-list (symbol-function name)))
291     (t
292     (when (symbolp name)
293     (setq name (symbol-function name)))
294     (if (eq (class-of name) cl::the-class-standard-gf)
295     (generic-function-lambda-list name)
296     (ccl:function-lambda-list name))))
297     (error () :not-available)))
298    
299     (defimplementation function-name (fn)
300     (handler-case (getf (cl::function-info-list fn) 'cl::function-name)
301     (error () nil)))
302    
303     (defimplementation describe-symbol-for-emacs (symbol)
304     (let ((result '()))
305     (flet ((doc (kind &optional (sym symbol))
306     (or (documentation sym kind) :not-documented))
307     (maybe-push (property value)
308     (when value
309     (setf result (list* property value result)))))
310     (maybe-push
311     :variable (when (boundp symbol)
312     (doc 'variable)))
313     (maybe-push
314     :function (if (fboundp symbol)
315     (doc 'function)))
316     (maybe-push
317     :class (if (find-class symbol nil)
318     (doc 'class)))
319     result)))
320    
321     (defimplementation describe-definition (symbol namespace)
322     (ecase namespace
323     (:variable
324     (describe symbol))
325     ((:function :generic-function)
326     (describe (symbol-function symbol)))
327     (:class
328     (describe (find-class symbol)))))
329    
330     ;;; Compiler
331    
332     (defvar *buffer-name* nil)
333     (defvar *buffer-position*)
334     (defvar *buffer-string*)
335     (defvar *compile-filename* nil)
336    
337     ;; FIXME
338     (defimplementation call-with-compilation-hooks (FN)
339     (handler-bind ((error (lambda (c)
340     (signal (make-condition
341     'compiler-condition
342     :original-condition c
343     :severity :warning
344     :message (format nil "~A" c)
345     :location
346     (cond (*buffer-name*
347     (make-location
348     (list :buffer *buffer-name*)
349     (list :position *buffer-position*)))
350     (*compile-filename*
351     (make-location
352     (list :file *compile-filename*)
353     (list :position 1)))
354     (t
355     (list :error "No location"))))))))
356     (funcall fn)))
357    
358     (defimplementation swank-compile-file (*compile-filename* load-p)
359     (with-compilation-hooks ()
360     (let ((*buffer-name* nil))
361     (compile-file *compile-filename*)
362     (when load-p
363     (load (compile-file-pathname *compile-filename*))))))
364    
365     (defimplementation swank-compile-string (string &key buffer position directory)
366     (declare (ignore directory))
367     (with-compilation-hooks ()
368     (let ((*buffer-name* buffer)
369     (*buffer-position* position)
370     (*buffer-string* string))
371     (funcall (compile nil (read-from-string
372     (format nil "(~S () ~A)" 'lambda string)))))))
373    
374     ;;;; Inspecting
375    
376     (defclass corman-inspector (inspector)
377     ())
378    
379     (defimplementation make-default-inspector ()
380     (make-instance 'corman-inspector))
381    
382     (defun comma-separated (list &optional (callback (lambda (v)
383     `(:value ,v))))
384     (butlast (loop for e in list
385     collect (funcall callback e)
386     collect ", ")))
387    
388     (defimplementation inspect-for-emacs ((class standard-class)
389     (inspector corman-inspector))
390     (declare (ignore inspector))
391     (values "A class."
392     `("Name: " (:value ,(class-name class))
393     (:newline)
394     "Super classes: "
395     ,@(comma-separated (swank-mop:class-direct-superclasses class))
396     (:newline)
397     "Direct Slots: "
398     ,@(comma-separated
399     (swank-mop:class-direct-slots class)
400     (lambda (slot)
401     `(:value ,slot ,(princ-to-string (swank-mop:slot-definition-name slot)))))
402     (:newline)
403     "Effective Slots: "
404     ,@(if (swank-mop:class-finalized-p class)
405     (comma-separated
406     (swank-mop:class-slots class)
407     (lambda (slot)
408     `(:value ,slot ,(princ-to-string
409     (swank-mop:slot-definition-name slot)))))
410     '("#<N/A (class not finalized)>"))
411     (:newline)
412     ,@(when (documentation class t)
413     `("Documentation:" (:newline) ,(documentation class t) (:newline)))
414     "Sub classes: "
415     ,@(comma-separated (swank-mop:class-direct-subclasses class)
416     (lambda (sub)
417     `(:value ,sub ,(princ-to-string (class-name sub)))))
418     (:newline)
419     "Precedence List: "
420     ,@(if (swank-mop:class-finalized-p class)
421     (comma-separated (swank-mop:class-precedence-list class)
422     (lambda (class)
423     `(:value ,class ,(princ-to-string (class-name class)))))
424     '("#<N/A (class not finalized)>"))
425     (:newline))))
426    
427     (defimplementation inspect-for-emacs ((slot cons) (inspector corman-inspector))
428     ;; Inspects slot definitions
429     (declare (ignore corman-inspector))
430     (if (eq (car slot) :name)
431     (values "A slot."
432     `("Name: " (:value ,(swank-mop:slot-definition-name slot))
433     (:newline)
434     ,@(when (swank-mop:slot-definition-documentation slot)
435     `("Documentation:" (:newline)
436     (:value ,(swank-mop:slot-definition-documentation slot))
437     (:newline)))
438     "Init args: " (:value ,(swank-mop:slot-definition-initargs slot)) (:newline)
439     "Init form: " ,(if (swank-mop:slot-definition-initfunction slot)
440     `(:value ,(swank-mop:slot-definition-initform slot))
441     "#<unspecified>") (:newline)
442     "Init function: " (:value ,(swank-mop:slot-definition-initfunction slot))
443     (:newline)))
444     (call-next-method)))
445    
446     (defimplementation inspect-for-emacs ((pathname pathnames::pathname-internal)
447     inspector)
448     (declare (ignore inspector))
449     (values (if (wild-pathname-p pathname)
450     "A wild pathname."
451     "A pathname.")
452     (append (label-value-line*
453     ("Namestring" (namestring pathname))
454     ("Host" (pathname-host pathname))
455     ("Device" (pathname-device pathname))
456     ("Directory" (pathname-directory pathname))
457     ("Name" (pathname-name pathname))
458     ("Type" (pathname-type pathname))
459     ("Version" (pathname-version pathname)))
460     (unless (or (wild-pathname-p pathname)
461     (not (probe-file pathname)))
462     (label-value-line "Truename" (truename pathname))))))
463    
464     ;;; This is probably not good, but it WFM
465     (in-package :common-lisp)
466    
467     (defvar *old-documentation* #'documentation)
468     (defun documentation (thing &optional (type 'function))
469     (if (symbolp thing)
470     (funcall *old-documentation* thing type)
471     (values)))
472    
473     (defmethod print-object ((restart restart) stream)
474     (if (or *print-escape*
475     *print-readably*)
476     (print-unreadable-object (restart stream :type t :identity t)
477     (princ (restart-name restart) stream))
478     (when (functionp (restart-report-function restart))
479     (funcall (restart-report-function restart) stream))))

  ViewVC Help
Powered by ViewVC 1.1.5