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

Contents of /slime/swank-gray.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13 - (hide annotations)
Tue Aug 5 17:38:40 2008 UTC (5 years, 8 months ago) by heller
Branch: MAIN
Changes since 1.12: +23 -0 lines
* swank-gray.lisp (stream-write-string): New method.
* swank-backend.lisp (*gray-stream-symbols*): Include write-string.
1 heller 1.1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
2     ;;;
3     ;;; swank-gray.lisp --- Gray stream based IO redirection.
4     ;;;
5 heller 1.6 ;;; Created 2003
6 heller 1.1 ;;;
7     ;;; This code has been placed in the Public Domain. All warranties
8     ;;; are disclaimed.
9     ;;;
10    
11 heller 1.4 (in-package :swank-backend)
12 heller 1.1
13     (defclass slime-output-stream (fundamental-character-output-stream)
14 lgorrie 1.2 ((output-fn :initarg :output-fn)
15 heller 1.8 (buffer :initform (make-string 8000))
16 heller 1.1 (fill-pointer :initform 0)
17 heller 1.8 (column :initform 0)
18 nsiivola 1.10 (lock :initform (make-recursive-lock :name "buffer write lock"))))
19 heller 1.1
20 heller 1.12 (defmacro with-slime-output-stream (stream &body body)
21     `(with-slots (lock output-fn buffer fill-pointer column) ,stream
22     (call-with-recursive-lock-held lock (lambda () ,@body))))
23    
24 heller 1.1 (defmethod stream-write-char ((stream slime-output-stream) char)
25 heller 1.12 (with-slime-output-stream stream
26     (setf (schar buffer fill-pointer) char)
27     (incf fill-pointer)
28     (incf column)
29     (when (char= #\newline char)
30     (setf column 0))
31     (when (= fill-pointer (length buffer))
32     (finish-output stream)))
33 heller 1.1 char)
34    
35 heller 1.13 (defmethod stream-write-string ((stream slime-output-stream) string
36     &optional start end)
37     (with-slime-output-stream stream
38     (let* ((start (or start 0))
39     (end (or end (length string)))
40     (len (length buffer))
41     (count (- end start))
42     (free (- len fill-pointer)))
43     (when (>= count free)
44     (stream-finish-output stream))
45     (cond ((< count len)
46     (replace buffer string :start1 fill-pointer
47     :start2 start :end2 end)
48     (incf fill-pointer count))
49     (t
50     (funcall output-fn (subseq string start end))))
51     (let ((last-newline (position #\newline string :from-end t
52     :start start :end end)))
53     (setf column (if last-newline
54     (- end last-newline 1)
55     (+ column count))))))
56     string)
57    
58 heller 1.1 (defmethod stream-line-column ((stream slime-output-stream))
59 heller 1.12 (with-slime-output-stream stream column))
60 heller 1.1
61     (defmethod stream-line-length ((stream slime-output-stream))
62     75)
63    
64 heller 1.8 (defmethod stream-finish-output ((stream slime-output-stream))
65 heller 1.12 (with-slime-output-stream stream
66     (unless (zerop fill-pointer)
67     (funcall output-fn (subseq buffer 0 fill-pointer))
68     (setf fill-pointer 0)))
69 heller 1.8 nil)
70    
71     (defmethod stream-force-output ((stream slime-output-stream))
72 heller 1.12 (stream-finish-output stream))
73 heller 1.9
74     (defmethod stream-fresh-line ((stream slime-output-stream))
75 heller 1.12 (with-slime-output-stream stream
76     (cond ((zerop column) nil)
77     (t (terpri stream) t))))
78 heller 1.1
79     (defclass slime-input-stream (fundamental-character-input-stream)
80 lgorrie 1.2 ((output-stream :initarg :output-stream)
81     (input-fn :initarg :input-fn)
82 nsiivola 1.10 (buffer :initform "") (index :initform 0)
83     (lock :initform (make-lock :name "buffer read lock"))))
84 heller 1.1
85     (defmethod stream-read-char ((s slime-input-stream))
86 nsiivola 1.10 (call-with-lock-held
87     (slot-value s 'lock)
88     (lambda ()
89     (with-slots (buffer index output-stream input-fn) s
90     (when (= index (length buffer))
91     (when output-stream
92     (finish-output output-stream))
93     (let ((string (funcall input-fn)))
94     (cond ((zerop (length string))
95     (return-from stream-read-char :eof))
96     (t
97     (setf buffer string)
98     (setf index 0)))))
99     (assert (plusp (length buffer)))
100     (prog1 (aref buffer index) (incf index))))))
101 heller 1.1
102     (defmethod stream-listen ((s slime-input-stream))
103 nsiivola 1.10 (call-with-lock-held
104     (slot-value s 'lock)
105     (lambda ()
106     (with-slots (buffer index) s
107     (< index (length buffer))))))
108 heller 1.1
109     (defmethod stream-unread-char ((s slime-input-stream) char)
110 nsiivola 1.10 (call-with-lock-held
111     (slot-value s 'lock)
112     (lambda ()
113     (with-slots (buffer index) s
114     (decf index)
115     (cond ((eql (aref buffer index) char)
116     (setf (aref buffer index) char))
117     (t
118     (warn "stream-unread-char: ignoring ~S (expected ~S)"
119     char (aref buffer index)))))))
120 heller 1.1 nil)
121    
122     (defmethod stream-clear-input ((s slime-input-stream))
123 nsiivola 1.10 (call-with-lock-held
124     (slot-value s 'lock)
125     (lambda ()
126     (with-slots (buffer index) s
127     (setf buffer ""
128     index 0))))
129 heller 1.1 nil)
130    
131     (defmethod stream-line-column ((s slime-input-stream))
132     nil)
133    
134     (defmethod stream-line-length ((s slime-input-stream))
135     75)
136    
137 heller 1.3
138     ;;; CLISP extensions
139    
140     ;; We have to define an additional method for the sake of the C
141     ;; function listen_char (see src/stream.d), on which SYS::READ-FORM
142     ;; depends.
143    
144     ;; We could make do with either of the two methods below.
145    
146     (defmethod stream-read-char-no-hang ((s slime-input-stream))
147 nsiivola 1.10 (call-with-lock-held
148     (slot-value s 'lock)
149     (lambda ()
150     (with-slots (buffer index) s
151     (when (< index (length buffer))
152     (prog1 (aref buffer index) (incf index)))))))
153 heller 1.3
154     ;; This CLISP extension is what listen_char actually calls. The
155     ;; default method would call STREAM-READ-CHAR-NO-HANG, so it is a bit
156     ;; more efficient to define it directly.
157    
158     (defmethod stream-read-char-will-hang-p ((s slime-input-stream))
159     (with-slots (buffer index) s
160     (= index (length buffer))))
161    
162    
163     ;;;
164 heller 1.4 (defimplementation make-fn-streams (input-fn output-fn)
165 heller 1.3 (let* ((output (make-instance 'slime-output-stream
166     :output-fn output-fn))
167     (input (make-instance 'slime-input-stream
168     :input-fn input-fn
169     :output-stream output)))
170     (values input output)))

  ViewVC Help
Powered by ViewVC 1.1.5