/[climacs]/climacs/climacs-lisp-syntax.lisp
ViewVC logotype

Contents of /climacs/climacs-lisp-syntax.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations)
Tue Nov 20 12:59:53 2007 UTC (6 years, 4 months ago) by thenriksen
Branch: MAIN
Changes since 1.4: +4 -4 lines
Fixed Climacs to adapt to changes in Drei.
1 thenriksen 1.1 ;;; -*- Mode: Lisp; Package: DREI-LISP-SYNTAX -*-
2    
3     ;;; (c) copyright 2005 by
4     ;;; Robert Strandh (strandh@labri.fr)
5     ;;; (c) copyright 2006 by
6     ;;; Troels Henriksen (athas@sigkill.dk)
7     ;;;
8     ;;; This library is free software; you can redistribute it and/or
9     ;;; modify it under the terms of the GNU Library General Public
10     ;;; License as published by the Free Software Foundation; either
11     ;;; version 2 of the License, or (at your option) any later version.
12     ;;;
13     ;;; This library is distributed in the hope that it will be useful,
14     ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15     ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16     ;;; Library General Public License for more details.
17     ;;;
18     ;;; You should have received a copy of the GNU Library General Public
19     ;;; License along with this library; if not, write to the
20     ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21     ;;; Boston, MA 02111-1307 USA.
22    
23     ;;; Add Climacs-dependent functionality to the stock Lisp syntax.
24    
25     (in-package :drei-lisp-syntax)
26    
27     (defmethod frame-clear-completions ((frame climacs-gui:climacs))
28     (let ((completions-pane
29     (when (typep *application-frame* 'esa-frame-mixin)
30     (find "Completions" (windows *application-frame*)
31     :key #'pane-name
32     :test #'string=))))
33     (unless (null completions-pane)
34     (climacs-gui:delete-window completions-pane)
35     (setf completions-pane nil))))
36    
37     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
38     ;;;
39     ;;; Compiler note hyperlinking
40    
41     (defclass location ()()
42     (:documentation "The base for all locations."))
43    
44     (defclass error-location (location)
45     ((error-message :initarg :error-message :accessor error-message)))
46    
47     (defclass actual-location (location)
48     ((source-position :initarg :position :accessor source-position)
49     (snippet :initarg :snippet :accessor snippet :initform nil))
50     (:documentation "The base for all non-error locations."))
51    
52     (defclass buffer-location (actual-location)
53     ((buffer-name :initarg :buffer :accessor buffer-name)))
54    
55     (defclass file-location (actual-location)
56     ((file-name :initarg :file :accessor file-name)))
57    
58     (defclass source-location (actual-location)
59     ((source-form :initarg :source-form :accessor source-form)))
60    
61     (defclass basic-position () ()
62     (:documentation "The base for all positions."))
63    
64     (defclass char-position (basic-position)
65     ((char-position :initarg :position :accessor char-position)
66     (align-p :initarg :align-p :initform nil :accessor align-p)))
67    
68     (defun make-char-position (position-list)
69     (make-instance 'char-position :position (second position-list)
70     :align-p (third position-list)))
71    
72     (defclass line-position (basic-position)
73     ((start-line :initarg :line :accessor start-line)
74     (end-line :initarg :end-line :initform nil :accessor end-line)))
75    
76     (defun make-line-position (position-list)
77     (make-instance 'line-position :line (second position-list)
78     :end-line (third position-list)))
79    
80     (defclass function-name-position (basic-position)
81     ((function-name :initarg :function-name)))
82    
83     (defun make-function-name-position (position-list)
84     (make-instance 'function-name-position :function-name (second position-list)))
85    
86     (defclass source-path-position (basic-position)
87     ((path :initarg :source-path :accessor path)
88     (start-position :initarg :start-position :accessor start-position)))
89    
90     (defun make-source-path-position (position-list)
91     (make-instance 'source-path-position :source-path (second position-list)
92     :start-position (third position-list)))
93    
94     (defclass text-anchored-position (basic-position)
95     ((start :initarg :text-anchored :accessor start)
96     (text :initarg :text :accessor text)
97     (delta :initarg :delta :accessor delta)))
98    
99     (defun make-text-anchored-position (position-list)
100     (make-instance 'text-anchored-position :text-anchored (second position-list)
101     :text (third position-list)
102     :delta (fourth position-list)))
103    
104     (defclass method-position (basic-position)
105     ((name :initarg :method :accessor name)
106     (specializers :initarg :specializers :accessor specializers)
107     (qualifiers :initarg :qualifiers :accessor qualifiers)))
108    
109     (defun make-method-position (position-list)
110     (make-instance 'method-position :method (second position-list)
111     :specializers (third position-list)
112     :qualifiers (last position-list)))
113    
114     (defun make-location (location-list)
115     (ecase (first location-list)
116     (:error (make-instance 'error-location :error-message (second location-list)))
117     (:location
118     (destructuring-bind (l buf pos hints) location-list
119     (declare (ignore l))
120     (let ((location
121     (apply #'make-instance
122     (ecase (first buf)
123     (:file 'file-location)
124     (:buffer 'buffer-location)
125     (:source-form 'source-location))
126     buf))
127     (position
128     (funcall
129     (ecase (first pos)
130     (:position #'make-char-position)
131     (:line #'make-line-position)
132     (:function-name #'make-function-name-position)
133     (:source-path #'make-source-path-position)
134     (:text-anchored #'make-text-anchored-position)
135     (:method #'make-method-position))
136     pos)))
137     (setf (source-position location) position)
138     (when hints
139     (setf (snippet location) (rest hints)))
140     location)))))
141    
142     (defmethod initialize-instance :after ((note compiler-note) &rest args)
143     (declare (ignore args))
144     (setf (location note) (make-location (location note))))
145    
146     (defun show-note-counts (notes &optional seconds)
147     (loop with nerrors = 0
148     with nwarnings = 0
149     with nstyle-warnings = 0
150     with nnotes = 0
151     for note in notes
152     do (etypecase note
153     (error-compiler-note (incf nerrors))
154     (read-error-compiler-note (incf nerrors))
155     (warning-compiler-note (incf nwarnings))
156     (style-warning-compiler-note (incf nstyle-warnings))
157     (note-compiler-note (incf nnotes)))
158     finally
159     (esa:display-message "Compilation finished: ~D error~:P ~
160     ~D warning~:P ~D style-warning~:P ~D note~:P ~
161     ~@[[~D secs]~]"
162     nerrors nwarnings nstyle-warnings nnotes seconds)))
163    
164     (defun one-line-ify (string)
165     "Return a single-line version of STRING.
166     Each newline and following whitespace is replaced by a single space."
167     (loop with count = 0
168     while (< count (length string))
169     with new-string = (make-array 0 :element-type 'character :adjustable t
170     :fill-pointer 0)
171     when (char= (char string count) #\Newline)
172     do (loop while (and (< count (length string))
173 thenriksen 1.5 (whitespacep (current-syntax) (char string count)))
174 thenriksen 1.1 do (incf count)
175     ;; Just ignore whitespace if it is last in the
176     ;; string.
177     finally (when (< count (length string))
178     (vector-push-extend #\Space new-string)))
179     else
180     do (vector-push-extend (char string count) new-string)
181     (incf count)
182     finally (return new-string)))
183    
184     (defgeneric print-for-menu (object stream))
185    
186     (defun print-note-for-menu (note stream severity ink)
187     (with-accessors ((message message) (short-message short-message)) note
188     (with-drawing-options (stream :ink ink
189     :text-style (make-text-style :sans-serif :italic nil))
190     (princ severity stream)
191     (princ " " stream))
192     (princ (if short-message
193     (one-line-ify short-message)
194     (one-line-ify message))
195     stream)))
196    
197     (defmacro def-print-for-menu (class name colour)
198     `(defmethod print-for-menu ((object ,class) stream)
199     (print-note-for-menu object stream ,name ,colour)))
200    
201     (def-print-for-menu error-compiler-note "Error" +red+)
202     (def-print-for-menu read-error-compiler-note "Read Error" +red+)
203     (def-print-for-menu warning-compiler-note "Warning" +dark-red+)
204     (def-print-for-menu style-warning-compiler-note "Style Warning" +brown+)
205     (def-print-for-menu note-compiler-note "Note" +brown+)
206    
207     (defun show-notes (notes buffer-name definition)
208     (let ((stream (climacs-gui:typeout-window
209     (format nil "~10TCompiler Notes: ~A ~A" buffer-name definition))))
210     (loop for note in notes
211     do (with-output-as-presentation (stream note 'compiler-note)
212     (print-for-menu note stream))
213     (terpri stream)
214     count note into length
215     finally (change-space-requirements stream
216     :height (* length (stream-line-height stream)))
217     (scroll-extent stream 0 0))))
218    
219     (defgeneric goto-location (location))
220    
221     (defmethod goto-location ((location error-location))
222     (esa:display-message (error-message location)))
223    
224     (defmethod goto-location ((location buffer-location))
225     (let ((buffer (find (buffer-name location)
226     (buffers *application-frame*)
227     :test #'string= :key #'name)))
228     (unless buffer
229     (esa:display-message "No buffer ~A" (buffer-name location))
230     (beep)
231     (return-from goto-location))
232     (climacs-core:switch-to-buffer (current-window) buffer)
233     (goto-position (point (current-window))
234     (char-position (source-position location)))))
235    
236     (defmethod goto-location ((location file-location))
237     (let ((buffer (find (file-name location)
238     (buffers *application-frame*)
239     :test #'string= :key #'(lambda (buffer)
240     (let ((path (filepath buffer)))
241     (when path
242     (namestring path)))))))
243     (if buffer
244 thenriksen 1.5 (climacs-core:switch-to-buffer (current-window) buffer)
245 thenriksen 1.1 (find-file (file-name location)))
246     (goto-position (point (current-window))
247     (char-position (source-position location)))))
248    
249     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
250     ;;;
251     ;;; Macroexpansion and evaluation
252    
253     (defun macroexpand-token (syntax token &optional (all nil))
254     (with-syntax-package (syntax (start-offset token))
255 thenriksen 1.3 (let* ((string (form-string syntax token))
256 thenriksen 1.1 (expression (read-from-string string))
257     (expansion (macroexpand-for-drei (get-usable-image syntax)
258     expression
259     all))
260     (expansion-string (with-output-to-string (s)
261     (pprint expansion s))))
262 thenriksen 1.5 (let ((buffer (climacs-core:switch-to-buffer (current-window) "*Macroexpansion*")))
263 thenriksen 1.1 (set-syntax buffer "Lisp"))
264     (let ((point (point (current-window)))
265     (header-string (one-line-ify (subseq string 0
266     (min 40 (length string))))))
267     (end-of-buffer point)
268     (unless (beginning-of-buffer-p point)
269     (insert-object point #\Newline))
270     (insert-sequence point
271     (format nil ";;; Macroexpand-~:[1~;all~] ~A...~%"
272     all header-string))
273     (insert-sequence point expansion-string)
274     (insert-object point #\Newline)))))
275    
276 thenriksen 1.2 (defun compile-definition-interactively (mark syntax)
277 thenriksen 1.4 (let* ((token (definition-at-mark syntax mark))
278 thenriksen 1.3 (string (form-string syntax token))
279 thenriksen 1.2 (m (clone-mark mark))
280     (buffer-name (name (buffer syntax)))
281     (*read-base* (base syntax)))
282     (with-syntax-package (syntax mark)
283 thenriksen 1.4 (forward-definition m syntax 1 nil)
284     (if (backward-definition m syntax 1 nil)
285     (multiple-value-bind (result notes)
286     (compile-form-for-drei (get-usable-image syntax)
287     (form-to-object syntax token
288     :read t
289     :package (package-at-mark syntax mark))
290     (buffer syntax)
291     m)
292     (show-note-counts notes (second result))
293     (when (not (null notes))
294     (show-notes notes buffer-name
295     (one-line-ify (subseq string 0 (min (length string) 20))))))
296     (display-message "No definition at point")))))
297 thenriksen 1.2
298 thenriksen 1.1 (defun compile-file-interactively (buffer &optional load-p)
299     (cond ((null (filepath buffer))
300     (esa:display-message "Buffer ~A is not associated with a file" (name buffer)))
301     (t
302     (when (and (needs-saving buffer)
303     (accept 'boolean :prompt (format nil "Save buffer ~A ?" (name buffer))))
304     (climacs-core:save-buffer buffer))
305     (let ((*read-base* (base (syntax buffer))))
306     (multiple-value-bind (result notes)
307     (compile-file-for-drei (get-usable-image (syntax buffer))
308     (filepath buffer)
309     (package-at-mark (syntax buffer) 0) load-p)
310     (show-note-counts notes (second result))
311     (when notes (show-notes notes (name buffer) "")))))))
312    
313     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
314     ;;;
315     ;;; Definition editing
316    
317     (defparameter *find-definition-stack* '())
318    
319     (defun pop-find-definition-stack ()
320     (unless (null *find-definition-stack*)
321     (let* ((offset+buffer (pop *find-definition-stack*))
322     (offset (first offset+buffer))
323     (buffer (second offset+buffer)))
324     (if (find buffer (buffers *application-frame*))
325 thenriksen 1.5 (progn (climacs-core:switch-to-buffer (current-window) buffer)
326 thenriksen 1.1 (goto-position (point (current-window)) offset))
327     (pop-find-definition-stack)))))
328    
329     ;; KLUDGE: We need to put more info in the definition objects to begin
330     ;; with.
331     (defun definition-type (definition)
332     (let ((data (read-from-string (first definition))))
333     (case (first data)
334     ((or cl:defclass)
335     'cl:class)
336     ((or cl:defgeneric
337     cl:defmethod
338     cl:defun
339     cl:defmacro)
340     'cl:function)
341     (t t))))
342    
343     (defun edit-definition (symbol &optional type)
344     (let ((all-definitions (find-definitions-for-drei
345     (get-usable-image (syntax (current-buffer)))
346     symbol)))
347     (let ((definitions (if (not type)
348     all-definitions
349     (remove-if-not #'(lambda (definition)
350     (eq (definition-type definition) type))
351     all-definitions))))
352     (cond ((null definitions)
353     (esa:display-message "No known definitions for: ~A" symbol)
354     (beep))
355     (t
356     (goto-definition symbol definitions))))))
357    
358     (defun goto-definition (name definitions)
359     (let* ((pane (current-window))
360     (buffer (buffer pane))
361     (point (point pane))
362     (offset (offset point)))
363     (push (list offset buffer) *find-definition-stack*))
364     (cond ((null (cdr definitions))
365     (let* ((def (car definitions))
366     (xref (make-xref def)))
367     (goto-location xref)))
368     (t
369     (let ((xref (show-definitions name definitions)))
370     (when xref (goto-location xref))))))
371    
372     (defclass xref ()
373     ((dspec :initarg :dspec :accessor dspec)
374     (location :initarg :location :accessor location)))
375    
376     (defun make-xref (xref-list)
377     (destructuring-bind (dspec location) xref-list
378     (make-instance 'xref
379     :dspec dspec
380     :location (make-location location))))
381    
382     (defmethod goto-location ((xref xref))
383     (goto-location (location xref)))
384    
385     (defun show-definitions (name definitions)
386     (show-xrefs (loop for xref-list in definitions
387     collect (make-xref xref-list))
388     'definition name))
389    
390     (defun show-xrefs (xrefs type symbol)
391     (cond ((null xrefs)
392     (esa:display-message "No references found for ~A." symbol)
393     (beep))
394     (t
395     (flet ((printer (item stream)
396     (with-drawing-options (stream :ink +dark-blue+
397     :text-style (make-text-style :fixed nil nil))
398     (princ (dspec item) stream))))
399     (let ((stream (climacs-gui:typeout-window
400     (format nil "~10T~A ~A" type symbol))))
401     (loop for xref in xrefs
402     do (with-output-as-presentation (stream xref 'xref)
403     (printer xref stream))
404     (terpri stream)
405     count xref into length
406     finally (change-space-requirements stream
407     :height (* length (stream-line-height stream)))
408     (scroll-extent stream 0 0)))))))
409    
410     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
411     ;;;
412     ;;; Some group support
413    
414     ;; WARNING, using this group can be dangerous, as Climacs is not
415     ;; really suited to opening up a large amount of buffers that each
416     ;; require a full syntax reparse. FIXME: Groups are currently
417     ;; disabled.
418     #+nil (climacs-core:define-group "ASDF System Files" (group (system (asdf:find-system (accept 'symbol :prompt "System"))))
419     (declare (ignore group))
420     (when system
421     (mapcar #'asdf:component-pathname
422     (remove-if-not (lambda (c)
423     (typep c 'asdf:cl-source-file))
424     (asdf:module-components system)))))

  ViewVC Help
Powered by ViewVC 1.1.5