/[cmucl]/src/hemlock/lispmode.lisp
ViewVC logotype

Contents of /src/hemlock/lispmode.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (hide annotations)
Mon Jul 28 13:44:34 1997 UTC (16 years, 8 months ago) by pw
Branch: MAIN
Changes since 1.6: +2 -1 lines
Add defindent for LOCALLY
1 ram 1.1 ;;; -*- Log: hemlock.log; Package: Hemlock -*-
2     ;;;
3     ;;; **********************************************************************
4 ram 1.3 ;;; This code was written as part of the CMU Common Lisp project at
5     ;;; Carnegie Mellon University, and has been placed in the public domain.
6     ;;;
7     (ext:file-comment
8 pw 1.7 "$Header: /tiger/var/lib/cvsroots/cmucl/src/hemlock/lispmode.lisp,v 1.7 1997/07/28 13:44:34 pw Exp $")
9 ram 1.3 ;;;
10 ram 1.1 ;;; **********************************************************************
11     ;;;
12     ;;; Hemlock LISP Mode commands
13     ;;;
14     ;;; Written by Ivan Vazquez and Bill Maddox.
15     ;;;
16    
17     (in-package "HEMLOCK")
18    
19 ram 1.3 (declaim (optimize (speed 2))); turn off byte compilation.
20 ram 1.1
21    
22 ram 1.3 ;;;; Variables and lisp-info structure.
23    
24 ram 1.1 ;;; These routines are used to define, for standard LISP mode, the start and end
25     ;;; of a block to parse. If these need to be changed for a minor mode that sits
26     ;;; on top of LISP mode, simply do a DEFHVAR with the minor mode and give the
27     ;;; name of the function to use instead of START-OF-PARSE-BLOCK and
28     ;;; END-OF-PARSE-BLOCK.
29     ;;;
30    
31     (defhvar "Parse Start Function"
32     "Take a mark and move it to the top of a block for paren parsing."
33     :value 'start-of-parse-block)
34    
35     (defhvar "Parse End Function"
36     "Take a mark and move it to the bottom of a block for paren parsing."
37     :value 'end-of-parse-block)
38    
39    
40 ram 1.3 ;;; LISP-INFO is the structure used to store the data about the line in its
41     ;;; Plist.
42     ;;;
43 ram 1.1 ;;; -> BEGINS-QUOTED, ENDING-QUOTED are both Boolean slots that tell whether
44     ;;; or not a line's begining and/or ending are quoted.
45     ;;;
46     ;;; -> RANGES-TO-IGNORE is a list of cons cells, each having the form
47     ;;; ( [begining-charpos] [end-charpos] ) each of these cells indicating
48     ;;; a range to ignore. End is exclusive.
49     ;;;
50     ;;; -> NET-OPEN-PARENS, NET-CLOSE-PARENS integers that are the number of
51     ;;; unmatched opening and closing parens that there are on a line.
52     ;;;
53     ;;; -> SIGNATURE-SLOT ...
54     ;;;
55    
56     (defstruct (lisp-info (:constructor make-lisp-info ()))
57     (begins-quoted nil) ; (or t nil)
58     (ending-quoted nil) ; (or t nil)
59     (ranges-to-ignore nil) ; (or t nil)
60     (net-open-parens 0 :type fixnum)
61     (net-close-parens 0 :type fixnum)
62     (signature-slot))
63    
64    
65    
66 ram 1.3 ;;;; Macros.
67    
68 ram 1.1 ;;; The following Macros exist to make it easy to acces the Syntax primitives
69     ;;; without uglifying the code. They were originally written by Maddox.
70     ;;;
71    
72     (defmacro scan-char (mark attribute values)
73     `(find-attribute ,mark ',attribute ,(attr-predicate values)))
74    
75     (defmacro rev-scan-char (mark attribute values)
76     `(reverse-find-attribute ,mark ',attribute ,(attr-predicate values)))
77    
78     (defmacro test-char (char attribute values)
79     `(let ((x (character-attribute ',attribute ,char)))
80     ,(attr-predicate-aux values)))
81    
82     (eval-when (compile load eval)
83     (defun attr-predicate (values)
84     (cond ((eq values 't)
85     '#'plusp)
86     ((eq values 'nil)
87     '#'zerop)
88     (t `#'(lambda (x) ,(attr-predicate-aux values)))))
89    
90     (defun attr-predicate-aux (values)
91     (cond ((eq values t)
92     '(plusp x))
93     ((eq values nil)
94     '(zerop x))
95     ((symbolp values)
96     `(eq x ',values))
97     ((and (listp values) (member (car values) '(and or not)))
98     (cons (car values) (mapcar #'attr-predicate-aux (cdr values))))
99     (t (error "Illegal form in attribute pattern - ~S" values))))
100    
101     ); Eval-When (Compile Load Eval)
102    
103     ;;;
104     ;;; FIND-LISP-CHAR
105    
106     (defmacro find-lisp-char (mark)
107     "Move MARK to next :LISP-SYNTAX character, if one isn't found, return NIL."
108     `(find-attribute ,mark :lisp-syntax
109     #'(lambda (x)
110     (member x '(:open-paren :close-paren :newline :comment
111     :char-quote :string-quote)))))
112     ;;;
113     ;;; PUSH-RANGE
114    
115     (defmacro push-range (new-range info-struct)
116     "Insert NEW-RANGE into the LISP-INFO-RANGES-TO-IGNORE slot of the INFO-STRUCT."
117     `(when ,new-range
118     (setf (lisp-info-ranges-to-ignore ,info-struct)
119     (cons ,new-range (lisp-info-ranges-to-ignore ,info-struct)))))
120     ;;;
121     ;;; SCAN-DIRECTION
122    
123     (defmacro scan-direction (mark forwardp &rest forms)
124     "Expand to a form that scans either backward or forward according to Forwardp."
125     (if forwardp
126     `(scan-char ,mark ,@forms)
127     `(rev-scan-char ,mark ,@forms)))
128     ;;;
129     ;;; DIRECTION-CHAR
130    
131     (defmacro direction-char (mark forwardp)
132     "Expand to a form that returns either the previous or next character according
133     to Forwardp."
134     (if forwardp
135     `(next-character ,mark)
136     `(previous-character ,mark)))
137    
138     ;;;
139     ;;; NEIGHBOR-MARK
140    
141     (defmacro neighbor-mark (mark forwardp)
142     "Expand to a form that moves MARK either backward or forward one character,
143     depending on FORWARDP."
144     (if forwardp
145     `(mark-after ,mark)
146     `(mark-before ,mark)))
147    
148     ;;;
149     ;;; NEIGHBOR-LINE
150    
151     (defmacro neighbor-line (line forwardp)
152     "Expand to return the next or previous line, according to Forwardp."
153     (if forwardp
154     `(line-next ,line)
155     `(line-previous ,line)))
156    
157    
158 ram 1.3 ;;;; Parsing functions.
159    
160     ;;; PRE-COMMAND-PARSE-CHECK -- Public.
161 ram 1.1 ;;;
162     (defun pre-command-parse-check (mark &optional (fer-sure-parse nil))
163     "Parse the area before the command is actually executed."
164     (with-mark ((top mark)
165     (bottom mark))
166     (funcall (value parse-start-function) top)
167     (funcall (value parse-end-function) bottom)
168     (parse-over-block (mark-line top) (mark-line bottom) fer-sure-parse)))
169    
170     ;;; PARSE-OVER-BLOCK
171 ram 1.3 ;;;
172 ram 1.1 (defun parse-over-block (start-line end-line &optional (fer-sure-parse nil))
173     "Parse over an area indicated from END-LINE to START-LINE."
174     (let ((test-line start-line)
175     prev-line-info)
176    
177     (with-mark ((mark (mark test-line 0)))
178    
179     ; Set the pre-begining and post-ending lines to delimit the range
180     ; of action any command will take. This means set the lisp-info of the
181     ; lines immediately before and after the block to Nil.
182    
183     (when (line-previous start-line)
184     (setf (getf (line-plist (line-previous start-line)) 'lisp-info) nil))
185     (when (line-next end-line)
186     (setf (getf (line-plist (line-next end-line)) 'lisp-info) nil))
187    
188     (loop
189     (let ((line-info (getf (line-plist test-line) 'lisp-info)))
190    
191     ;; Reparse the line when any of the following are true:
192     ;;
193     ;; FER-SURE-PARSE is T
194     ;;
195     ;; LINE-INFO or PREV-LINE-INFO are Nil.
196     ;;
197     ;; If the line begins quoted and the previous one wasn't
198     ;; ended quoted.
199     ;;
200     ;; The Line's signature slot is invalid (the line has changed).
201     ;;
202    
203     (when (or fer-sure-parse
204     (not line-info)
205     (not prev-line-info)
206    
207     (not (eq (lisp-info-begins-quoted line-info)
208     (lisp-info-ending-quoted prev-line-info)))
209    
210     (not (eql (line-signature test-line)
211     (lisp-info-signature-slot line-info))))
212    
213     (move-to-position mark 0 test-line)
214    
215     (unless line-info
216     (setf line-info (make-lisp-info))
217     (setf (getf (line-plist test-line) 'lisp-info) line-info))
218    
219     (parse-lisp-line-info mark line-info prev-line-info))
220    
221     (when (eq end-line test-line)
222     (return nil))
223    
224     (setq prev-line-info line-info)
225    
226     (setq test-line (line-next test-line)))))))
227    
228    
229 ram 1.3 ;;;; Parse block finders.
230 ram 1.1
231     (defhvar "Minimum Lines Parsed"
232     "The minimum number of lines before and after the point parsed by Lisp mode."
233     :value 50)
234     (defhvar "Maximum Lines Parsed"
235     "The maximum number of lines before and after the point parsed by Lisp mode."
236     :value 500)
237     (defhvar "Defun Parse Goal"
238     "Lisp mode parses the region obtained by skipping this many defuns forward
239     and backward from the point unless this falls outside of the range specified
240     by \"Minimum Lines Parsed\" and \"Maximum Lines Parsed\"."
241     :value 2)
242    
243    
244     (macrolet ((frob (step end)
245     `(let ((min (value minimum-lines-parsed))
246     (max (value maximum-lines-parsed))
247     (goal (value defun-parse-goal))
248     (last-defun nil))
249     (declare (fixnum min max goal))
250     (do ((line (mark-line mark) (,step line))
251     (count 0 (1+ count)))
252     ((null line)
253     (,end mark))
254     (declare (fixnum count))
255     (when (char= (line-character line 0) #\()
256     (setq last-defun line)
257     (decf goal)
258     (when (and (<= goal 0) (>= count min))
259     (line-start mark line)
260     (return)))
261     (when (> count max)
262     (line-start mark (or last-defun line))
263     (return))))))
264    
265     (defun start-of-parse-block (mark)
266     (frob line-previous buffer-start))
267    
268     (defun end-of-parse-block (mark)
269     (frob line-next buffer-end)))
270    
271     ;;;
272     ;;; START-OF-SEARCH-LINE
273    
274     (defun start-of-search-line (line)
275     "Set LINE to the begining line of the block of text to parse."
276     (with-mark ((mark (mark line 0)))
277     (funcall (value 'Parse-Start-Function) mark)
278     (setq line (mark-line mark))))
279    
280     ;;;
281     ;;; END-OF-SEACH-LINE
282    
283     (defun end-of-search-line (line)
284     "Set LINE to the ending line of the block of text to parse."
285     (with-mark ((mark (mark line 0)))
286     (funcall (value 'Parse-End-Function) mark)
287     (setq line (mark-line mark))))
288    
289    
290 ram 1.3 ;;;; PARSE-LISP-LINE-INFO.
291    
292     ;;; PARSE-LISP-LINE-INFO -- Internal.
293     ;;;
294     ;;; This parses through the line doing the following things:
295     ;;;
296 ram 1.1 ;;; Counting/Setting the NET-OPEN-PARENS & NET-CLOSE-PARENS.
297 ram 1.3 ;;;
298 ram 1.1 ;;; Making all areas of the line that should be invalid (comments,
299     ;;; char-quotes, and the inside of strings) and such be in
300     ;;; RANGES-TO-IGNORE.
301     ;;;
302     ;;; Set BEGINS-QUOTED and ENDING-QUOTED
303 ram 1.3 ;;;
304 ram 1.1 (defun parse-lisp-line-info (mark line-info prev-line-info)
305     "Parse line and set line information like NET-OPEN-PARENS, NET-CLOSE-PARENS,
306 ram 1.3 RANGES-TO-INGORE, and ENDING-QUOTED."
307 ram 1.1 (let ((net-open-parens 0)
308     (net-close-parens 0))
309     (declare (fixnum net-open-parens net-close-parens))
310    
311     ;; Re-set the slots necessary
312    
313     (setf (lisp-info-ranges-to-ignore line-info) nil)
314    
315     ;; The only way the current line begins quoted is when there
316     ;; is a previous line and it's ending was quoted.
317    
318     (setf (lisp-info-begins-quoted line-info)
319     (and prev-line-info
320     (lisp-info-ending-quoted prev-line-info)))
321    
322     (if (lisp-info-begins-quoted line-info)
323     (deal-with-string-quote mark line-info)
324     (setf (lisp-info-ending-quoted line-info) nil))
325    
326     (unless (lisp-info-ending-quoted line-info)
327     (loop
328     (find-lisp-char mark)
329     (ecase (character-attribute :lisp-syntax (next-character mark))
330    
331     (:open-paren
332     (setq net-open-parens (1+ net-open-parens))
333     (mark-after mark))
334    
335     (:close-paren
336     (if (zerop net-open-parens)
337     (setq net-close-parens (1+ net-close-parens))
338     (setq net-open-parens (1- net-open-parens)))
339     (mark-after mark))
340    
341     (:newline
342     (setf (lisp-info-ending-quoted line-info) nil)
343     (return t))
344    
345     (:comment
346     (push-range (cons (mark-charpos mark) (line-length (mark-line mark)))
347     line-info)
348     (setf (lisp-info-ending-quoted line-info) nil)
349     (return t))
350    
351     (:char-quote
352     (mark-after mark)
353     (push-range (cons (mark-charpos mark) (1+ (mark-charpos mark)))
354     line-info)
355     (mark-after mark))
356    
357     (:string-quote
358     (mark-after mark)
359     (unless (deal-with-string-quote mark line-info)
360     (setf (lisp-info-ending-quoted line-info) t)
361     (return t))))))
362    
363     (setf (lisp-info-net-open-parens line-info) net-open-parens)
364     (setf (lisp-info-net-close-parens line-info) net-close-parens)
365     (setf (lisp-info-signature-slot line-info)
366     (line-signature (mark-line mark)))))
367 ram 1.3
368    
369 ram 1.1
370 ram 1.3 ;;;; String quote utilities.
371    
372 ram 1.1 ;;; VALID-STRING-QUOTE-P
373 ram 1.3 ;;;
374 ram 1.1 (defmacro valid-string-quote-p (mark forwardp)
375     "Return T if the string-quote indicated by MARK is valid."
376     (let ((test-mark (gensym)))
377     `(with-mark ((,test-mark ,mark))
378 ram 1.3 ,(unless forwardp
379     ;; TEST-MARK should always be right before the String-quote to be
380     ;; checked.
381     `(mark-before ,test-mark))
382 ram 1.1 (when (test-char (next-character ,test-mark) :lisp-syntax :string-quote)
383     (let ((slash-count 0))
384     (loop
385     (mark-before ,test-mark)
386     (if (test-char (next-character ,test-mark) :lisp-syntax :char-quote)
387     (incf slash-count)
388     (return t)))
389     (not (oddp slash-count)))))))
390    
391     ;;;
392     ;;; FIND-VALID-STRING-QUOTE
393    
394     (defmacro find-valid-string-quote (mark &key forwardp (cease-at-eol nil))
395     "Expand to a form that will leave MARK before a valid string-quote character,
396     in either a forward or backward direction, according to FORWARDP. If
397     CEASE-AT-EOL is T then it will return nil if encountering the EOL before a
398     valid string-quote."
399     (let ((e-mark (gensym)))
400     `(with-mark ((,e-mark ,mark))
401    
402     (loop
403     (unless (scan-direction ,e-mark ,forwardp :lisp-syntax
404     ,(if cease-at-eol
405     `(or :newline :string-quote)
406     `:string-quote))
407     (return nil))
408    
409     ,@(if cease-at-eol
410     `((when (test-char (direction-char ,e-mark ,forwardp) :lisp-syntax
411     :newline)
412     (return nil))))
413    
414     (when (valid-string-quote-p ,e-mark ,forwardp)
415     (move-mark ,mark ,e-mark)
416     (return t))
417    
418     (neighbor-mark ,e-mark ,forwardp)))))
419    
420 ram 1.3 ;;;; DEAL-WITH-STRING-QUOTE.
421    
422     ;;; DEAL-WITH-STRING-QUOTE
423     ;;;
424 ram 1.1 ;;; Called when a string is begun (i.e. parse hits a #\"). It checks for a
425 ram 1.3 ;;; matching quote on the line that MARK points to, and puts the appropriate
426     ;;; area in the RANGES-TO-IGNORE slot and leaves MARK pointing after this area.
427     ;;; The "appropriate area" is from MARK to the end of the line or the matching
428     ;;; string-quote, whichever comes first.
429     ;;;
430 ram 1.1 (defun deal-with-string-quote (mark info-struct)
431     "Alter the current line's info struct as necessary as due to encountering a
432 ram 1.3 string quote character."
433 ram 1.1 (with-mark ((e-mark mark))
434     (cond ((find-valid-string-quote e-mark :forwardp t :cease-at-eol t)
435 ram 1.3 ;; If matching quote is on this line then mark the area between the
436     ;; first quote (MARK) and the matching quote as invalid by pushing
437     ;; its begining and ending into the IGNORE-RANGE.
438 ram 1.1 (push-range (cons (mark-charpos mark) (mark-charpos e-mark))
439     info-struct)
440     (setf (lisp-info-ending-quoted info-struct) nil)
441     (mark-after e-mark)
442     (move-mark mark e-mark))
443 ram 1.3 ;; If the EOL has been hit before the matching quote then mark the
444     ;; area from MARK to the EOL as invalid.
445 ram 1.1 (t
446 ram 1.3 (push-range (cons (mark-charpos mark)
447     (1+ (line-length (mark-line mark))))
448 ram 1.1 info-struct)
449     ;; The Ending is marked as still being quoted.
450     (setf (lisp-info-ending-quoted info-struct) t)
451     (line-end mark)
452     nil))))
453 ram 1.3
454    
455 ram 1.1
456     ;;;; Character validity checking:
457    
458     ;;; Find-Ignore-Region -- Internal
459     ;;;
460     ;;; If the character in the specified direction from Mark is in an ignore
461     ;;; region, then return the region and the line that the region is in as
462     ;;; values. If there is no ignore region, then return NIL and the Mark-Line.
463     ;;; If the line is not parsed, or there is no character (because of being at
464     ;;; the buffer beginning or end), then return both values NIL.
465     ;;;
466     (defun find-ignore-region (mark forwardp)
467     (flet ((scan (line pos)
468     (declare (fixnum pos))
469     (let ((info (getf (line-plist line) 'lisp-info)))
470     (if info
471     (dolist (range (lisp-info-ranges-to-ignore info)
472     (values nil line))
473     (let ((start (car range))
474     (end (cdr range)))
475     (declare (fixnum start end))
476     (when (and (>= pos start) (< pos end))
477     (return (values range line)))))
478     (values nil nil)))))
479     (let ((pos (mark-charpos mark))
480     (line (mark-line mark)))
481     (declare (fixnum pos))
482     (cond (forwardp (scan line pos))
483     ((> pos 0) (scan line (1- pos)))
484     (t
485     (let ((prev (line-previous line)))
486     (if prev
487     (scan prev (line-length prev))
488     (values nil nil))))))))
489    
490    
491     ;;; Valid-Spot -- Public
492     ;;;
493     (defun valid-spot (mark forwardp)
494     "Return true if the character pointed to by Mark is not in a quoted context,
495     false otherwise. If Forwardp is true, we use the next character, otherwise
496     we use the previous."
497     (multiple-value-bind (region line)
498     (find-ignore-region mark forwardp)
499     (and line (not region))))
500    
501    
502     ;;; Scan-Direction-Valid -- Internal
503     ;;;
504     ;;; Like scan-direction, but only stop on valid characters.
505     ;;;
506     (defmacro scan-direction-valid (mark forwardp &rest forms)
507     (let ((n-mark (gensym))
508     (n-line (gensym))
509     (n-region (gensym))
510     (n-won (gensym)))
511     `(let ((,n-mark ,mark) (,n-won nil))
512     (loop
513     (multiple-value-bind (,n-region ,n-line)
514     (find-ignore-region ,n-mark ,forwardp)
515     (unless ,n-line (return nil))
516     (if ,n-region
517     (move-to-position ,n-mark
518     ,(if forwardp
519     `(cdr ,n-region)
520     `(car ,n-region))
521     ,n-line)
522     (when ,n-won (return t)))
523     ;;
524     ;; Peculiar condition when a quoting character terminates a line.
525     ;; The ignore region is off the end of the line causing %FORM-OFFSET
526     ;; to infinitely loop.
527     (when (> (mark-charpos ,n-mark) (line-length ,n-line))
528     (line-offset ,n-mark 1 0))
529     (unless (scan-direction ,n-mark ,forwardp ,@forms)
530     (return nil))
531     (setq ,n-won t))))))
532    
533    
534 ram 1.3 ;;;; List offseting.
535    
536 ram 1.1 ;;; %LIST-OFFSET allows for BACKWARD-LIST and FORWARD-LIST to be built
537     ;;; with the same existing structure, with the altering of one variable.
538     ;;; This one variable being FORWARDP.
539     ;;;
540     (defmacro %list-offset (actual-mark forwardp &key (extra-parens 0) )
541     "Expand to code that will go forward one list either backward or forward,
542 ram 1.3 according to the FORWARDP flag."
543 ram 1.1 (let ((mark (gensym)))
544     `(let ((paren-count ,extra-parens))
545     (declare (fixnum paren-count))
546     (with-mark ((,mark ,actual-mark))
547     (loop
548     (scan-direction ,mark ,forwardp :lisp-syntax
549     (or :close-paren :open-paren :newline))
550     (let ((ch (direction-char ,mark ,forwardp)))
551     (unless ch (return nil))
552     (when (valid-spot ,mark ,forwardp)
553     (case (character-attribute :lisp-syntax ch)
554     (:close-paren
555     (decf paren-count)
556 ram 1.3 ,(when forwardp
557     ;; When going forward, an unmatching close-paren means the
558     ;; end of list.
559     `(when (<= paren-count 0)
560 ram 1.1 (neighbor-mark ,mark ,forwardp)
561     (move-mark ,actual-mark ,mark)
562     (return t))))
563     (:open-paren
564     (incf paren-count)
565     ,(unless forwardp ; Same as above only end of list
566     `(when (>= paren-count 0) ; is opening parens.
567     (neighbor-mark ,mark ,forwardp)
568     (move-mark ,actual-mark ,mark)
569     (return t))))
570    
571     (:newline
572 ram 1.3 ;; When a #\Newline is hit, then the matching paren must lie
573     ;; on some other line so drop down into the multiple line
574     ;; balancing function: QUEST-FOR-BALANCING-PAREN If no paren
575     ;; seen yet, keep going.
576 ram 1.1 (cond ((zerop paren-count))
577     ((quest-for-balancing-paren ,mark paren-count ,forwardp)
578     (move-mark ,actual-mark ,mark)
579     (return t))
580     (t
581     (return nil)))))))
582    
583     (neighbor-mark ,mark ,forwardp))))))
584    
585     ;;;
586     ;;; QUEST-FOR-BALANCING-PAREN
587    
588     (defmacro quest-for-balancing-paren (mark paren-count forwardp)
589     "Expand to a form that finds the the balancing paren for however many opens or
590     closes are registered by Paren-Count."
591     `(let* ((line (mark-line ,mark)))
592     (loop
593     (setq line (neighbor-line line ,forwardp))
594     (unless line (return nil))
595     (let ((line-info (getf (line-plist line) 'lisp-info))
596     (unbal-paren ,paren-count))
597     (unless line-info (return nil))
598    
599     ,(if forwardp
600     `(decf ,paren-count (lisp-info-net-close-parens line-info))
601     `(incf ,paren-count (lisp-info-net-open-parens line-info)))
602    
603     (when ,(if forwardp
604     `(<= ,paren-count 0)
605     `(>= ,paren-count 0))
606     ,(if forwardp
607     `(line-start ,mark line)
608     `(line-end ,mark line))
609     (return (goto-correct-paren-char ,mark unbal-paren ,forwardp)))
610    
611     ,(if forwardp
612     `(incf ,paren-count (lisp-info-net-open-parens line-info))
613     `(decf ,paren-count (lisp-info-net-close-parens line-info)))))))
614    
615    
616     ;;;
617     ;;; GOTO-CORRECT-PAREN-CHAR
618    
619     (defmacro goto-correct-paren-char (mark paren-count forwardp)
620     "Expand to a form that will leave MARK on the correct balancing paren matching
621     however many are indicated by COUNT."
622     `(with-mark ((m ,mark))
623     (let ((count ,paren-count))
624     (loop
625     (scan-direction m ,forwardp :lisp-syntax
626     (or :close-paren :open-paren :newline))
627     (when (valid-spot m ,forwardp)
628     (ecase (character-attribute :lisp-syntax (direction-char m ,forwardp))
629     (:close-paren
630     (decf count)
631     ,(when forwardp
632     `(when (zerop count)
633     (neighbor-mark m ,forwardp)
634     (move-mark ,mark m)
635     (return t))))
636    
637     (:open-paren
638     (incf count)
639     ,(unless forwardp
640     `(when (zerop count)
641     (neighbor-mark m ,forwardp)
642     (move-mark ,mark m)
643     (return t))))))
644     (neighbor-mark m ,forwardp)))))
645    
646    
647     (defun list-offset (mark offset)
648     (if (plusp offset)
649     (dotimes (i offset t)
650     (unless (%list-offset mark t) (return nil)))
651     (dotimes (i (- offset) t)
652     (unless (%list-offset mark nil) (return nil)))))
653    
654     (defun forward-up-list (mark)
655     "Moves mark just past the closing paren of the immediately containing list."
656     (%list-offset mark t :extra-parens 1))
657    
658     (defun backward-up-list (mark)
659     "Moves mark just before the opening paren of the immediately containing list."
660     (%list-offset mark nil :extra-parens -1))
661    
662    
663    
664     ;;;; Top level form location hacks (open parens beginning lines).
665    
666     ;;; NEIGHBOR-TOP-LEVEL is used only in TOP-LEVEL-OFFSET.
667     ;;;
668     (eval-when (compile eval)
669     (defmacro neighbor-top-level (line forwardp)
670     `(loop
671     (when (test-char (line-character ,line 0) :lisp-syntax :open-paren)
672     (return t))
673     (setf ,line ,(if forwardp `(line-next ,line) `(line-previous ,line)))
674     (unless ,line (return nil))))
675     ) ;eval-when
676    
677     (defun top-level-offset (mark offset)
678     "Go forward or backward offset number of top level forms. Mark is
679     returned if offset forms exists, otherwise nil."
680     (declare (fixnum offset))
681     (let* ((line (mark-line mark))
682     (at-start (test-char (line-character line 0) :lisp-syntax :open-paren)))
683     (cond ((zerop offset) mark)
684     ((plusp offset)
685     (do ((offset (if at-start offset (1- offset))
686     (1- offset)))
687     (nil)
688     (declare (fixnum offset))
689     (unless (neighbor-top-level line t) (return nil))
690     (when (zerop offset) (return (line-start mark line)))
691     (unless (setf line (line-next line)) (return nil))))
692     (t
693     (do ((offset (if (and at-start (start-line-p mark))
694     offset
695     (1+ offset))
696     (1+ offset)))
697     (nil)
698     (declare (fixnum offset))
699     (unless (neighbor-top-level line nil) (return nil))
700     (when (zerop offset) (return (line-start mark line)))
701     (unless (setf line (line-previous line)) (return nil)))))))
702    
703    
704     (defun mark-top-level-form (mark1 mark2)
705     "Moves mark1 and mark2 to the beginning and end of the current or next defun.
706     Mark1 one is used as a reference. The marks may be altered even if
707     unsuccessful. if successful, return mark2, else nil."
708     (let ((winp (cond ((inside-defun-p mark1)
709     (cond ((not (top-level-offset mark1 -1)) nil)
710     ((not (form-offset (move-mark mark2 mark1) 1)) nil)
711     (t mark2)))
712     ((start-defun-p mark1)
713     (form-offset (move-mark mark2 mark1) 1))
714     ((and (top-level-offset (move-mark mark2 mark1) -1)
715     (start-defun-p mark2)
716     (form-offset mark2 1)
717     (same-line-p mark1 mark2))
718     (form-offset (move-mark mark1 mark2) -1)
719     mark2)
720     ((top-level-offset mark1 1)
721     (form-offset (move-mark mark2 mark1) 1)))))
722     (when winp
723     (when (blank-after-p mark2) (line-offset mark2 1 0))
724     mark2)))
725    
726     (defun inside-defun-p (mark)
727     "T if the current point is (supposedly) in a top level form."
728     (with-mark ((m mark))
729     (when (top-level-offset m -1)
730     (form-offset m 1)
731     (mark> m mark))))
732    
733     (defun start-defun-p (mark)
734     "Returns t if mark is sitting before an :open-paren at the beginning of a
735     line."
736     (and (start-line-p mark)
737     (test-char (next-character mark) :lisp-syntax :open-paren)))
738    
739    
740    
741 ram 1.3 ;;;; Form offseting.
742 ram 1.1
743     (defmacro %form-offset (mark forwardp)
744     `(with-mark ((m ,mark))
745     (when (scan-direction-valid m ,forwardp :lisp-syntax
746     (or :open-paren :close-paren
747     :char-quote :string-quote
748     :constituent))
749     (ecase (character-attribute :lisp-syntax (direction-char m ,forwardp))
750     (:open-paren
751     (when ,(if forwardp `(list-offset m 1) `(mark-before m))
752     ,(unless forwardp
753     '(scan-direction m nil :lisp-syntax (not :prefix)))
754     (move-mark ,mark m)
755     t))
756     (:close-paren
757     (when ,(if forwardp `(mark-after m) `(list-offset m -1))
758     ,(unless forwardp
759     '(scan-direction m nil :lisp-syntax (not :prefix)))
760     (move-mark ,mark m)
761     t))
762     ((:constituent :char-quote)
763     (scan-direction-valid m ,forwardp :lisp-syntax
764     (not (or :constituent :char-quote)))
765     ,(if forwardp
766     `(scan-direction-valid m t :lisp-syntax
767     (not (or :constituent :char-quote)))
768     `(scan-direction-valid m nil :lisp-syntax
769     (not (or :constituent :char-quote
770     :prefix))))
771     (move-mark ,mark m)
772     t)
773     (:string-quote
774     (cond ((valid-spot m ,(not forwardp))
775     (neighbor-mark m ,forwardp)
776     (when (scan-direction-valid m ,forwardp :lisp-syntax
777     :string-quote)
778     (neighbor-mark m ,forwardp)
779     (move-mark ,mark m)
780     t))
781     (t (neighbor-mark m ,forwardp)
782     (move-mark ,mark m)
783     t)))))))
784    
785    
786     (defun form-offset (mark offset)
787     "Move mark offset number of forms, after if positive, before if negative.
788     Mark is always moved. If there weren't enough forms, returns nil instead of
789     mark."
790     (if (plusp offset)
791     (dotimes (i offset t)
792     (unless (%form-offset mark t) (return nil)))
793     (dotimes (i (- offset) t)
794     (unless (%form-offset mark nil) (return nil)))))
795    
796    
797    
798 ram 1.3 ;;;; Table of special forms with special indenting requirements.
799 ram 1.1
800     (defhvar "Indent Defanything"
801     "This is the number of special arguments implicitly assumed to be supplied
802     in calls to functions whose names begin with \"DEF\". If set to NIL, this
803     feature is disabled."
804     :value 2)
805    
806     (defvar *special-forms* (make-hash-table :test #'equal))
807    
808     (defun defindent (fname args)
809     "Define Fname to have Args special arguments. If args is null then remove
810     any special arguments information."
811     (check-type fname string)
812     (let ((fname (string-upcase fname)))
813     (cond ((null args) (remhash fname *special-forms*))
814     (t
815     (check-type args integer)
816     (setf (gethash fname *special-forms*) args)))))
817    
818    
819     ;;; Hemlock forms.
820     ;;;
821     (defindent "with-mark" 1)
822     (defindent "with-random-typeout" 1)
823     (defindent "with-pop-up-display" 1)
824     (defindent "defhvar" 1)
825     (defindent "hlet" 1)
826     (defindent "defcommand" 2)
827     (defindent "defattribute" 1)
828     (defindent "command-case" 1)
829     (defindent "with-input-from-region" 1)
830     (defindent "with-output-to-mark" 1)
831     (defindent "with-output-to-window" 1)
832     (defindent "do-strings" 1)
833     (defindent "save-for-undo" 1)
834     (defindent "do-alpha-chars" 1)
835     (defindent "do-headers-buffers" 1)
836     (defindent "do-headers-lines" 1)
837     (defindent "with-headers-mark" 1)
838     (defindent "frob" 1) ;cover silly FLET and MACROLET names for Rob and Bill.
839     (defindent "with-writable-buffer" 1)
840    
841     ;;; Common Lisp forms.
842     ;;;
843     (defindent "block" 1)
844     (defindent "case" 1)
845     (defindent "catch" 1)
846     (defindent "ccase" 1)
847     (defindent "compiler-let" 1)
848     (defindent "ctypecase" 1)
849     (defindent "defconstant" 1)
850     (defindent "define-setf-method" 2)
851     (defindent "defmacro" 2)
852 pw 1.5 (defindent "defpackage" 1)
853 ram 1.1 (defindent "defparameter" 1)
854     (defindent "defstruct" 1)
855     (defindent "deftype" 2)
856     (defindent "defun" 2)
857     (defindent "defvar" 1)
858     (defindent "do" 2)
859     (defindent "do*" 2)
860     (defindent "do-all-symbols" 1)
861     (defindent "do-external-symbols" 1)
862     (defindent "do-symbols" 1)
863     (defindent "dolist" 1)
864     (defindent "dotimes" 1)
865     (defindent "ecase" 1)
866     (defindent "etypecase" 1)
867     (defindent "eval-when" 1)
868     (defindent "flet" 1)
869     (defindent "labels" 1)
870     (defindent "lambda" 1)
871     (defindent "let" 1)
872     (defindent "let*" 1)
873 pw 1.7 (defindent "locally" 0)
874 ram 1.1 (defindent "loop" 0)
875     (defindent "macrolet" 1)
876     (defindent "multiple-value-bind" 2)
877     (defindent "multiple-value-call" 1)
878     (defindent "multiple-value-prog1" 1)
879     (defindent "multiple-value-setq" 1)
880     (defindent "prog1" 1)
881     (defindent "progv" 2)
882     (defindent "progn" 0)
883     (defindent "typecase" 1)
884     (defindent "unless" 1)
885     (defindent "unwind-protect" 1)
886     (defindent "when" 1)
887     (defindent "with-input-from-string" 1)
888     (defindent "with-open-file" 1)
889     (defindent "with-open-stream" 1)
890     (defindent "with-output-to-string" 1)
891    
892     ;;; Error/condition system forms.
893     ;;;
894     (defindent "define-condition" 2)
895     (defindent "handler-bind" 1)
896     (defindent "handler-case" 1)
897     (defindent "restart-bind" 1)
898     (defindent "restart-case" 1)
899     (defindent "with-simple-restart" 1)
900     ;;; These are for RESTART-CASE branch formatting.
901     (defindent "store-value" 1)
902     (defindent "use-value" 1)
903     (defindent "muffle-warning" 1)
904     (defindent "abort" 1)
905     (defindent "continue" 1)
906    
907 ram 1.3 ;;; Debug-internals forms.
908     ;;;
909     (defindent "do-debug-function-blocks" 1)
910     (defindent "di:do-debug-function-blocks" 1)
911     (defindent "do-debug-function-variables" 1)
912     (defindent "di:do-debug-function-variables" 1)
913     (defindent "do-debug-block-locations" 1)
914     (defindent "di:do-debug-block-locations" 1)
915     ;;;
916     ;;; Debug-internals conditions
917     ;;; (define these to make uses of HANDLER-CASE indent branches correctly.)
918     ;;;
919     (defindent "debug-condition" 1)
920     (defindent "di:debug-condition" 1)
921     (defindent "no-debug-info" 1)
922     (defindent "di:no-debug-info" 1)
923     (defindent "no-debug-function-returns" 1)
924     (defindent "di:no-debug-function-returns" 1)
925     (defindent "no-debug-blocks" 1)
926     (defindent "di:no-debug-blocks" 1)
927     (defindent "lambda-list-unavailable" 1)
928     (defindent "di:lambda-list-unavailable" 1)
929     (defindent "no-debug-variables" 1)
930     (defindent "di:no-debug-variables" 1)
931     (defindent "invalid-value" 1)
932     (defindent "di:invalid-value" 1)
933     (defindent "ambiguous-variable-name" 1)
934     (defindent "di:ambiguous-variable-name" 1)
935     (defindent "debug-error" 1)
936     (defindent "di:debug-error" 1)
937     (defindent "unhandled-condition" 1)
938     (defindent "di:unhandled-condition" 1)
939     (defindent "unknown-code-location" 1)
940     (defindent "di:unknown-code-location" 1)
941     (defindent "unknown-debug-variable" 1)
942     (defindent "di:unknown-debug-variable" 1)
943     (defindent "invalid-control-stack-pointer" 1)
944     (defindent "di:invalid-control-stack-pointer" 1)
945     (defindent "frame-function-mismatch" 1)
946     (defindent "di:frame-function-mismatch" 1)
947    
948 ram 1.1 ;;; Xlib forms.
949     ;;;
950     (defindent "with-gcontext" 1)
951     (defindent "xlib:with-gcontext" 1)
952     (defindent "with-state" 1)
953     (defindent "xlib:with-state" 1)
954     (defindent "with-display" 1)
955     (defindent "xlib:with-display" 1)
956     (defindent "with-event-queue" 1)
957     (defindent "xlib:with-event-queue" 1)
958     (defindent "with-server-grabbed" 1)
959     (defindent "xlib:with-server-grabbed" 1)
960     (defindent "event-case" 1)
961     (defindent "xlib:event-case" 1)
962    
963     ;;; CLOS forms.
964     ;;;
965     (defindent "with-slots" 1)
966 pw 1.5 (defindent "with-slots*" 2) ; obsolete
967 pw 1.6 (defindent "with-accessors" 2)
968 pw 1.5 (defindent "with-accessors*" 2) ; obsolete
969 ram 1.1 (defindent "defclass" 2)
970 pw 1.5 (defindent "print-unreadable-object" 1)
971 ram 1.1
972     ;;; System forms.
973     ;;;
974     (defindent "alien-bind" 1)
975     (defindent "def-c-record" 1)
976     (defindent "defrecord" 1)
977    
978 ram 1.3 ;;; Wire forms.
979     (defindent "remote" 1)
980     (defindent "wire:remote" 1)
981     (defindent "remote-value" 1)
982     (defindent "wire:remote-value" 1)
983     (defindent "remote-value-bind" 3)
984     (defindent "wire:remote-value-bind" 3)
985 ram 1.1
986 ram 1.3
987 ram 1.1
988 ram 1.3 ;;;; Indentation.
989 ram 1.1
990 ram 1.3 ;;; LISP-INDENTATION -- Internal Interface.
991     ;;;
992 ram 1.1 (defun lisp-indentation (mark)
993 ram 1.3 "Compute number of spaces which mark should be indented according to
994     local context and lisp grinding conventions. This assumes mark is at the
995     beginning of the line to be indented."
996 ram 1.1 (with-mark ((m mark)
997     (temp mark))
998 ram 1.3 ;; See if we are in a quoted context.
999 ram 1.1 (unless (valid-spot m nil)
1000 ram 1.3 (return-from lisp-indentation (lisp-generic-indentation m)))
1001     ;; Look for the paren that opens the containing form.
1002 ram 1.1 (unless (backward-up-list m)
1003     (return-from lisp-indentation 0))
1004 ram 1.3 ;; Move after the paren, save the start, and find the form name.
1005 ram 1.1 (mark-after m)
1006     (with-mark ((start m))
1007 ram 1.3 (unless (and (scan-char m :lisp-syntax
1008     (not (or :space :prefix :char-quote)))
1009 ram 1.1 (test-char (next-character m) :lisp-syntax :constituent))
1010     (return-from lisp-indentation (mark-column start)))
1011     (with-mark ((fstart m))
1012     (scan-char m :lisp-syntax (not :constituent))
1013     (let* ((fname (nstring-upcase (region-to-string (region fstart m))))
1014     (special-args (or (gethash fname *special-forms*)
1015     (and (> (length fname) 2)
1016     (string= fname "DEF" :end1 3)
1017     (value indent-defanything)))))
1018     (declare (simple-string fname))
1019 ram 1.3 ;; Now that we have the form name, did it have special syntax?
1020 ram 1.1 (cond (special-args
1021     (with-mark ((spec m))
1022     (cond ((and (form-offset spec special-args)
1023     (mark<= spec mark))
1024     (1+ (mark-column start)))
1025     ((skip-valid-space m)
1026     (mark-column m))
1027     (t
1028     (+ (mark-column start) 3)))))
1029 ram 1.3 ;; See if the user seems to have altered the editor's
1030     ;; indentation, and if so, try to adhere to it. This usually
1031     ;; happens when you type in a quoted list constant that line
1032     ;; wraps. You want all the items on successive lines to fall
1033     ;; under the first character after the opening paren, not as if
1034     ;; you are calling a function.
1035 ram 1.1 ((and (form-offset temp -1)
1036 ram 1.3 (or (blank-before-p temp) (not (same-line-p temp fstart)))
1037 ram 1.1 (not (same-line-p temp mark)))
1038     (unless (blank-before-p temp)
1039     (line-start temp)
1040     (find-attribute temp :space #'zerop))
1041     (mark-column temp))
1042 ram 1.3 ;; Appears to be a normal form. Is the first arg on the same
1043     ;; line as the form name?
1044 ram 1.1 ((skip-valid-space m)
1045 ram 1.3 (or (lisp-indentation-check-for-local-def
1046     mark temp fstart start t)
1047     (mark-column m)))
1048     ;; Okay, fall under the first character after the opening paren.
1049 ram 1.1 (t
1050 ram 1.3 (or (lisp-indentation-check-for-local-def
1051     mark temp fstart start nil)
1052     (mark-column start)))))))))
1053 ram 1.1
1054 ram 1.3 (defhvar "Lisp Indentation Local Definers"
1055     "Forms with syntax like LABELS, MACROLET, etc."
1056     :value '("LABELS" "MACROLET" "FLET"))
1057    
1058     ;;; LISP-INDENTATION-CHECK-FOR-LOCAL-DEF -- Internal.
1059     ;;;
1060     ;;; This is a temporary hack to see how it performs. When we are indenting
1061     ;;; what appears to be a function call, let's look for FLET or MACROLET to see
1062     ;;; if we really are indenting a local definition. If we are, return the
1063     ;;; indentation for a DEFUN; otherwise, nil
1064     ;;;
1065     ;;; Mark is the argument to LISP-INDENTATION. Start is just inside the paren
1066     ;;; of what looks like a function call. If we are in an FLET, arg-list
1067     ;;; indicates whether the local function's arg-list has been entered, that is,
1068     ;;; whether we need to normally indent for a DEFUN body or indent specially for
1069     ;;; the arg-list.
1070     ;;;
1071     (defun lisp-indentation-check-for-local-def (mark temp1 temp2 start arg-list)
1072     ;; We know this succeeds from LISP-INDENTATION.
1073     (backward-up-list (move-mark temp1 mark)) ;Paren for local definition.
1074     (cond ((and (backward-up-list temp1) ;Paren opening the list of defs
1075     (form-offset (move-mark temp2 temp1) -1)
1076     (mark-before temp2)
1077     (backward-up-list temp1) ;Paren for FLET or MACROLET.
1078     (mark= temp1 temp2)) ;Must be in first arg form.
1079     ;; See if the containing form is named FLET or MACROLET.
1080     (mark-after temp1)
1081     (unless (and (scan-char temp1 :lisp-syntax
1082     (not (or :space :prefix :char-quote)))
1083     (test-char (next-character temp1) :lisp-syntax
1084     :constituent))
1085     (return-from lisp-indentation-check-for-local-def nil))
1086     (move-mark temp2 temp1)
1087     (scan-char temp2 :lisp-syntax (not :constituent))
1088     (let ((fname (nstring-upcase (region-to-string (region temp1 temp2)))))
1089     (cond ((not (member fname (value lisp-indentation-local-definers)
1090     :test #'string=))
1091     nil)
1092     (arg-list
1093     (1+ (mark-column start)))
1094     (t
1095     (+ (mark-column start) 3)))))))
1096    
1097     ;;; LISP-GENERIC-INDENTATION -- Internal.
1098     ;;;
1099     ;;; LISP-INDENTATION calls this when mark is in a invalid spot, or quoted
1100     ;;; context. If we are inside a string, we return the column one greater
1101     ;;; than the opening double quote. Otherwise, we just use the indentation
1102     ;;; of the first preceding non-blank line.
1103     ;;;
1104 ram 1.1 (defun lisp-generic-indentation (mark)
1105 ram 1.3 (with-mark ((m mark))
1106     (form-offset m -1)
1107     (cond ((eq (character-attribute :lisp-syntax (next-character m))
1108     :string-quote)
1109     (1+ (mark-column m)))
1110     (t
1111     (let* ((line (mark-line mark))
1112     (prev (do ((line (line-previous line) (line-previous line)))
1113     ((not (and line (blank-line-p line))) line))))
1114     (cond (prev
1115     (line-start mark prev)
1116     (find-attribute mark :space #'zerop)
1117     (mark-column mark))
1118     (t 0)))))))
1119 ram 1.1
1120     ;;; Skip-Valid-Space -- Internal
1121     ;;;
1122     ;;; Skip over any space on the line Mark is on, stopping at the first valid
1123     ;;; non-space character. If there is none on the line, return nil.
1124     ;;;
1125     (defun skip-valid-space (mark)
1126     (loop
1127     (scan-char mark :lisp-syntax (not :space))
1128     (let ((val (character-attribute :lisp-syntax
1129     (next-character mark))))
1130     (cond ((eq val :newline) (return nil))
1131     ((valid-spot mark t) (return mark))))
1132     (mark-after mark)))
1133    
1134 ram 1.3 (declaim (optimize (speed 0))); byte compile again
1135    
1136 ram 1.1
1137 ram 1.3 ;;;; Indentation commands and hook functions.
1138 ram 1.1
1139     (defcommand "Defindent" (p)
1140     "Define the Lisp indentation for the current function.
1141     The indentation is a non-negative integer which is the number
1142     of special arguments for the form. Examples: 2 for Do, 1 for Dolist.
1143     If a prefix argument is supplied, then delete the indentation information."
1144     "Do a defindent, man!"
1145     (with-mark ((m (current-point)))
1146     (pre-command-parse-check m)
1147     (unless (backward-up-list m) (editor-error))
1148     (mark-after m)
1149     (with-mark ((n m))
1150     (scan-char n :lisp-syntax (not :constituent))
1151     (let ((s (region-to-string (region m n))))
1152     (declare (simple-string s))
1153     (when (zerop (length s)) (editor-error))
1154     (if p
1155     (defindent s nil)
1156     (let ((i (prompt-for-integer
1157     :prompt (format nil "Indentation for ~A: " s)
1158     :help "Number of special arguments.")))
1159     (when (minusp i)
1160     (editor-error "Indentation must be non-negative."))
1161     (defindent s i))))))
1162 ram 1.3 (indent-command nil))
1163 ram 1.1
1164 ram 1.3 (defcommand "Indent Form" (p)
1165     "Indent Lisp code in the next form."
1166     "Indent Lisp code in the next form."
1167     (declare (ignore p))
1168     (let ((point (current-point)))
1169     (pre-command-parse-check point)
1170     (with-mark ((m point))
1171     (unless (form-offset m 1) (editor-error))
1172     (lisp-indent-region (region point m) "Indent Form"))))
1173    
1174     ;;; LISP-INDENT-REGION -- Internal.
1175     ;;;
1176     ;;; This indents a region of Lisp code without doing excessive redundant
1177     ;;; computation. We parse the entire region once, then scan through doing
1178     ;;; indentation on each line. We forcibly reparse each line that we indent so
1179     ;;; that the list operations done to determine indentation of subsequent lines
1180     ;;; will work. This is done undoably with save1, save2, buf-region, and
1181     ;;; undo-region.
1182     ;;;
1183     (defun lisp-indent-region (region &optional (undo-text "Lisp region indenting"))
1184     (check-region-query-size region)
1185     (let ((start (region-start region))
1186     (end (region-end region)))
1187     (with-mark ((m1 start)
1188     (m2 end))
1189     (funcall (value parse-start-function) m1)
1190     (funcall (value parse-end-function) m2)
1191     (parse-over-block (mark-line m1) (mark-line m2)))
1192     (let* ((first-line (mark-line start))
1193     (last-line (mark-line end))
1194     (prev (line-previous first-line))
1195     (prev-line-info
1196     (and prev (getf (line-plist prev) 'lisp-info)))
1197     (save1 (line-start (copy-mark start :right-inserting)))
1198     (save2 (line-end (copy-mark end :left-inserting)))
1199     (buf-region (region save1 save2))
1200     (undo-region (copy-region buf-region)))
1201     (with-mark ((bol start :left-inserting))
1202     (do ((line first-line (line-next line)))
1203     (nil)
1204     (line-start bol line)
1205     (insert-lisp-indentation bol)
1206     (let ((line-info (getf (line-plist line) 'lisp-info)))
1207     (parse-lisp-line-info bol line-info prev-line-info)
1208     (setq prev-line-info line-info))
1209     (when (eq line last-line) (return nil))))
1210     (make-region-undo :twiddle undo-text buf-region undo-region))))
1211    
1212     ;;; INDENT-FOR-LISP -- Internal.
1213     ;;;
1214     ;;; This is the value of "Indent Function" for "Lisp" mode.
1215     ;;;
1216     (defun indent-for-lisp (mark)
1217     (line-start mark)
1218     (pre-command-parse-check mark)
1219     (insert-lisp-indentation mark))
1220    
1221     (defun insert-lisp-indentation (m)
1222     (delete-horizontal-space m)
1223     (funcall (value indent-with-tabs) m (lisp-indentation m)))
1224    
1225    
1226    
1227     ;;;; Most "Lisp" mode commands.
1228    
1229 ram 1.1 (defcommand "Beginning of Defun" (p)
1230     "Move the point to the beginning of a top-level form.
1231     with an argument, skips the previous p top-level forms."
1232     "Move the point to the beginning of a top-level form."
1233     (let ((point (current-point))
1234     (count (or p 1)))
1235     (pre-command-parse-check point)
1236     (if (minusp count)
1237     (end-of-defun-command (- count))
1238     (unless (top-level-offset point (- count))
1239     (editor-error)))))
1240    
1241     ;;; "End of Defun", with a positive p (the normal case), does something weird.
1242     ;;; Get a mark at the beginning of the defun, and then offset it forward one
1243     ;;; less top level form than we want. This sets us up to use FORM-OFFSET which
1244     ;;; allows us to leave the point immediately after the defun. If we used
1245     ;;; TOP-LEVEL-OFFSET one less than p on the mark at the end of the current
1246     ;;; defun, point would be left at the beginning of the p+1'st form instead of
1247     ;;; at the end of the p'th form.
1248     ;;;
1249     (defcommand "End of Defun" (p)
1250     "Move the point to the end of a top-level form.
1251     With an argument, skips the next p top-level forms."
1252     "Move the point to the end of a top-level form."
1253     (let ((point (current-point))
1254     (count (or p 1)))
1255     (pre-command-parse-check point)
1256     (if (minusp count)
1257     (beginning-of-defun-command (- count))
1258     (with-mark ((m point)
1259     (dummy point))
1260     (cond ((not (mark-top-level-form m dummy))
1261     (editor-error "No current or next top level form."))
1262     (t
1263     (unless (top-level-offset m (1- count))
1264     (editor-error "Not enough top level forms."))
1265     ;; We might be one unparsed for away.
1266     (pre-command-parse-check m)
1267     (unless (form-offset m 1)
1268     (editor-error "Not enough top level forms."))
1269     (when (blank-after-p m) (line-offset m 1 0))
1270     (move-mark point m)))))))
1271    
1272     (defcommand "Forward List" (p)
1273     "Skip over the next Lisp list.
1274     With argument, skips the next p lists."
1275     "Skip over the next Lisp list."
1276     (let ((point (current-point))
1277     (count (or p 1)))
1278     (pre-command-parse-check point)
1279     (unless (list-offset point count) (editor-error))))
1280    
1281     (defcommand "Backward List" (p)
1282     "Skip over the previous Lisp list.
1283     With argument, skips the previous p lists."
1284     "Skip over the previous Lisp list."
1285     (let ((point (current-point))
1286     (count (- (or p 1))))
1287     (pre-command-parse-check point)
1288     (unless (list-offset point count) (editor-error))))
1289    
1290     (defcommand "Forward Form" (p)
1291     "Skip over the next Form.
1292     With argument, skips the next p Forms."
1293     "Skip over the next Form."
1294     (let ((point (current-point))
1295     (count (or p 1)))
1296     (pre-command-parse-check point)
1297     (unless (form-offset point count) (editor-error))))
1298    
1299     (defcommand "Backward Form" (p)
1300     "Skip over the previous Form.
1301     With argument, skips the previous p Forms."
1302     "Skip over the previous Form."
1303     (let ((point (current-point))
1304     (count (- (or p 1))))
1305     (pre-command-parse-check point)
1306     (unless (form-offset point count) (editor-error))))
1307    
1308     (defcommand "Mark Form" (p)
1309     "Set the mark at the end of the next Form.
1310     With a positive argument, set the mark after the following p
1311     Forms. With a negative argument, set the mark before
1312     the preceding -p Forms."
1313     "Set the mark at the end of the next Form."
1314     (with-mark ((m (current-point)))
1315     (pre-command-parse-check m)
1316     (let ((count (or p 1))
1317     (mark (push-buffer-mark (copy-mark m) t)))
1318     (if (form-offset m count)
1319     (move-mark mark m)
1320     (editor-error)))))
1321    
1322     (defcommand "Mark Defun" (p)
1323     "Puts the region around the next or containing top-level form.
1324     The point is left before the form and the mark is placed immediately
1325     after it."
1326     "Puts the region around the next or containing top-level form."
1327     (declare (ignore p))
1328     (let ((point (current-point)))
1329     (pre-command-parse-check point)
1330     (with-mark ((start point)
1331     (end point))
1332     (cond ((not (mark-top-level-form start end))
1333     (editor-error "No current or next top level form."))
1334     (t
1335     (move-mark point start)
1336     (move-mark (push-buffer-mark (copy-mark point) t) end))))))
1337    
1338     (defcommand "Forward Kill Form" (p)
1339     "Kill the next Form.
1340     With a positive argument, kills the next p Forms.
1341     Kills backward with a negative argument."
1342     "Kill the next Form."
1343     (with-mark ((m1 (current-point))
1344     (m2 (current-point)))
1345     (pre-command-parse-check m1)
1346     (let ((count (or p 1)))
1347     (unless (form-offset m1 count) (editor-error))
1348     (if (minusp count)
1349     (kill-region (region m1 m2) :kill-backward)
1350     (kill-region (region m2 m1) :kill-forward)))))
1351    
1352     (defcommand "Backward Kill Form" (p)
1353     "Kill the previous Form.
1354     With a positive argument, kills the previous p Forms.
1355     Kills forward with a negative argument."
1356     "Kill the previous Form."
1357     (forward-kill-form-command (- (or p 1))))
1358    
1359 ram 1.2 (defcommand "Extract Form" (p)
1360     "Replace the current containing list with the next form. The entire affected
1361     area is pushed onto the kill ring. If an argument is supplied, that many
1362     upward levels of list nesting is replaced by the next form."
1363     "Replace the current containing list with the next form. The entire affected
1364     area is pushed onto the kill ring. If an argument is supplied, that many
1365     upward levels of list nesting is replaced by the next form."
1366     (let ((point (current-point)))
1367     (pre-command-parse-check point)
1368     (with-mark ((form-start point :right-inserting)
1369     (form-end point))
1370     (unless (form-offset form-end 1) (editor-error))
1371     (form-offset (move-mark form-start form-end) -1)
1372     (with-mark ((containing-start form-start :left-inserting)
1373     (containing-end form-end :left-inserting))
1374     (dotimes (i (or p 1))
1375     (unless (and (forward-up-list containing-end)
1376     (backward-up-list containing-start))
1377     (editor-error)))
1378     (let ((r (copy-region (region form-start form-end))))
1379     (ring-push (delete-and-save-region
1380     (region containing-start containing-end))
1381     *kill-ring*)
1382     (ninsert-region point r)
1383     (move-mark point form-start))))))
1384    
1385 ram 1.1 (defcommand "Extract List" (p)
1386     "Extract the current list.
1387     The current list replaces the surrounding list. The entire affected
1388     area is pushed on the kill-ring. With prefix argument, remove that
1389     many surrounding lists."
1390     "Replace the P containing lists with the current one."
1391     (let ((point (current-point)))
1392     (pre-command-parse-check point)
1393     (with-mark ((lstart point :right-inserting)
1394     (lend point))
1395     (if (eq (character-attribute :lisp-syntax (next-character lstart))
1396     :open-paren)
1397     (mark-after lend)
1398     (unless (backward-up-list lstart) (editor-error)))
1399     (unless (forward-up-list lend) (editor-error))
1400     (with-mark ((rstart lstart)
1401     (rend lend))
1402     (dotimes (i (or p 1))
1403     (unless (and (forward-up-list rend) (backward-up-list rstart))
1404     (editor-error)))
1405     (let ((r (copy-region (region lstart lend))))
1406     (ring-push (delete-and-save-region (region rstart rend))
1407     *kill-ring*)
1408     (ninsert-region point r)
1409     (move-mark point lstart))))))
1410    
1411     (defcommand "Transpose Forms" (p)
1412     "Transpose Forms immediately preceding and following the point.
1413     With a zero argument, tranposes the Forms at the point and the mark.
1414     With a positive argument, transposes the Form preceding the point
1415     with the p-th one following it. With a negative argument, transposes the
1416     Form following the point with the p-th one preceding it."
1417     "Transpose Forms immediately preceding and following the point."
1418     (let ((point (current-point))
1419     (count (or p 1)))
1420     (pre-command-parse-check point)
1421     (if (zerop count)
1422     (let ((mark (current-mark)))
1423     (with-mark ((s1 mark :left-inserting)
1424     (s2 point :left-inserting))
1425     (scan-char s1 :whitespace nil)
1426     (scan-char s2 :whitespace nil)
1427     (with-mark ((e1 s1 :right-inserting)
1428     (e2 s2 :right-inserting))
1429     (unless (form-offset e1 1) (editor-error))
1430     (unless (form-offset e2 1) (editor-error))
1431     (ninsert-region s1 (delete-and-save-region (region s2 e2)))
1432     (ninsert-region s2 (delete-and-save-region (region s1 e1))))))
1433     (let ((fcount (if (plusp count) count 1))
1434     (bcount (if (plusp count) 1 count)))
1435     (with-mark ((s1 point :left-inserting)
1436     (e2 point :right-inserting))
1437     (dotimes (i bcount)
1438     (unless (form-offset s1 -1) (editor-error)))
1439     (dotimes (i fcount)
1440     (unless (form-offset e2 1) (editor-error)))
1441     (with-mark ((e1 s1 :right-inserting)
1442     (s2 e2 :left-inserting))
1443     (unless (form-offset e1 1) (editor-error))
1444     (unless (form-offset s2 -1) (editor-error))
1445     (ninsert-region s1 (delete-and-save-region (region s2 e2)))
1446     (ninsert-region s2 (delete-and-save-region (region s1 e1)))
1447     (move-mark point s2)))))))
1448    
1449    
1450     (defcommand "Insert ()" (p)
1451     "Insert a pair of parentheses ().
1452     With positive argument, puts parentheses around the next p
1453     Forms. The point is positioned after the open parenthesis."
1454     "Insert a pair of parentheses ()."
1455     (let ((point (current-point))
1456     (count (or p 0)))
1457     (pre-command-parse-check point)
1458     (cond ((not (minusp count))
1459     (insert-character point #\()
1460     (with-mark ((tmark point))
1461     (unless (form-offset tmark count) (editor-error))
1462     (cond ((mark= tmark point)
1463     (insert-character point #\))
1464     (mark-before point))
1465     (t (insert-character tmark #\))))))
1466     (t (editor-error)))))
1467    
1468    
1469     (defcommand "Move Over )" (p)
1470     "Move past the next close parenthesis, and start a new line.
1471     Any indentation preceding the preceding the parenthesis is deleted,
1472     and the new line is indented."
1473     "Move past the next close parenthesis, and start a new line."
1474     (declare (ignore p))
1475     (let ((point (current-point)))
1476     (pre-command-parse-check point)
1477 ram 1.3 (with-mark ((m point :left-inserting))
1478 ram 1.1 (cond ((scan-char m :lisp-syntax :close-paren)
1479     (delete-horizontal-space m)
1480     (mark-after m)
1481     (move-mark point m)
1482 ram 1.3 (delete-mark m)
1483 ram 1.1 (indent-new-line-command 1))
1484 ram 1.3 (t
1485     (delete-mark m)
1486     (editor-error))))))
1487 ram 1.1
1488    
1489     (defcommand "Forward Up List" (p)
1490     "Move forward past a one containing )."
1491     "Move forward past a one containing )."
1492     (let ((point (current-point))
1493     (count (or p 1)))
1494     (pre-command-parse-check point)
1495     (if (minusp count)
1496     (backward-up-list-command (- count))
1497     (with-mark ((m point))
1498     (dotimes (i count (move-mark point m))
1499     (unless (forward-up-list m) (editor-error)))))))
1500    
1501    
1502     (defcommand "Backward Up List" (p)
1503     "Move backward past a one containing (."
1504     "Move backward past a one containing (."
1505     (let ((point (current-point))
1506     (count (or p 1)))
1507     (pre-command-parse-check point)
1508     (if (minusp count)
1509     (forward-up-list-command (- count))
1510     (with-mark ((m point))
1511     (dotimes (i count (move-mark point m))
1512     (unless (backward-up-list m) (editor-error)))))))
1513    
1514    
1515     (defcommand "Down List" (p)
1516     "Move down a level in list structure.
1517     With argument, moves down p levels."
1518     "Move down a level in list structure."
1519     (let ((point (current-point))
1520     (count (or p 1)))
1521     (pre-command-parse-check point)
1522     (with-mark ((m point))
1523     (dotimes (i count (move-mark point m))
1524     (unless (and (scan-char m :lisp-syntax :open-paren)
1525     (mark-after m))
1526     (editor-error))))))
1527    
1528    
1529    
1530 ram 1.3 ;;;; Filling Lisp comments, strings, and indented text.
1531 ram 1.1
1532 ram 1.3 (defhvar "Fill Lisp Comment Paragraph Confirm"
1533     "This determines whether \"Fill Lisp Comment Paragraph\" will prompt for
1534     confirmation to fill contiguous lines with the same initial whitespace when
1535     it is invoked outside of a comment or string."
1536     :value t)
1537    
1538     (defcommand "Fill Lisp Comment Paragraph" (p)
1539     "This fills a flushleft or indented Lisp comment.
1540     This also fills Lisp string literals using the proper indentation as a
1541     filling prefix. When invoked outside of a comment or string, this tries
1542     to fill all contiguous lines beginning with the same initial, non-empty
1543     blankspace. When filling a comment, the current line is used to determine a
1544     fill prefix by taking all the initial whitespace on the line, the semicolons,
1545     and any whitespace following the semicolons."
1546     "Fills a flushleft or indented Lisp comment."
1547     (declare (ignore p))
1548     (let ((point (current-point)))
1549     (pre-command-parse-check point)
1550     (with-mark ((start point)
1551     (end point)
1552     (m point))
1553     (let ((commentp (fill-lisp-comment-paragraph-prefix start end)))
1554     (cond (commentp
1555     (fill-lisp-comment-or-indented-text start end))
1556     ((and (not (valid-spot m nil))
1557     (form-offset m -1)
1558     (eq (character-attribute :lisp-syntax (next-character m))
1559     :string-quote))
1560     (fill-lisp-string m))
1561     ((or (not (value fill-lisp-comment-paragraph-confirm))
1562     (prompt-for-y-or-n
1563     :prompt '("Not in a comment or string. Fill contiguous ~
1564     lines with the same initial whitespace? ")))
1565     (fill-lisp-comment-or-indented-text start end)))))))
1566    
1567     ;;; FILL-LISP-STRING -- Internal.
1568     ;;;
1569     ;;; This fills the Lisp string containing mark as if it had been entered using
1570     ;;; Hemlock's Lisp string indentation, "Indent Function" for "Lisp" mode. This
1571     ;;; assumes the area around mark has already been PRE-COMMAND-PARSE-CHECK'ed,
1572     ;;; and it ensures the string ends before doing any filling. This function
1573     ;;; is undo'able.
1574     ;;;
1575     (defun fill-lisp-string (mark)
1576     (with-mark ((end mark))
1577     (unless (form-offset end 1)
1578     (editor-error "Attempted to fill Lisp string, but it doesn't end?"))
1579     (let* ((mark (copy-mark mark :left-inserting))
1580     (end (copy-mark end :left-inserting))
1581     (string-region (region mark end))
1582     (undo-region (copy-region string-region))
1583     (hack (make-empty-region)))
1584     ;; Generate prefix.
1585     (funcall (value indent-with-tabs)
1586     (region-end hack) (1+ (mark-column mark)))
1587     ;; Skip opening double quote and fill string starting on its own line.
1588     (mark-after mark)
1589     (insert-character mark #\newline)
1590     (line-start mark)
1591     (setf (mark-kind mark) :right-inserting)
1592     (fill-region string-region (region-to-string hack))
1593     ;; Clean up inserted prefix on first line, delete inserted newline, and
1594     ;; move before the double quote for undo.
1595     (with-mark ((text mark :left-inserting))
1596     (find-attribute text :whitespace #'zerop)
1597     (delete-region (region mark text)))
1598     (delete-characters mark -1)
1599     (mark-before mark)
1600     ;; Save undo.
1601     (make-region-undo :twiddle "Fill Lisp Comment Paragraph"
1602     string-region undo-region))))
1603    
1604     ;;; FILL-LISP-COMMENT-OR-INDENTED-TEXT -- Internal.
1605     ;;;
1606     ;;; This fills all contiguous lines around start and end containing fill prefix
1607     ;;; designated by the region between start and end. These marks can only be
1608     ;;; equal when there is no comment and no initial whitespace. This is a bad
1609     ;;; situation since this function in that situation would fill the entire
1610     ;;; buffer into one paragraph. This function is undo'able.
1611     ;;;
1612     (defun fill-lisp-comment-or-indented-text (start end)
1613     (when (mark= start end)
1614     (editor-error "This command only fills Lisp comments, strings, or ~
1615     indented text, but this line is flushleft."))
1616     ;;
1617     ;; Find comment block.
1618     (let* ((prefix (region-to-string (region start end)))
1619     (length (length prefix)))
1620     (declare (simple-string prefix))
1621     (flet ((frob (mark direction)
1622     (loop
1623     (let* ((line (line-string (mark-line mark)))
1624     (line-len (length line)))
1625     (declare (simple-string line))
1626     (unless (string= line prefix :end1 (min line-len length))
1627     (when (= direction -1)
1628     (unless (same-line-p mark end) (line-offset mark 1 0)))
1629     (return)))
1630     (unless (line-offset mark direction 0)
1631     (when (= direction 1) (line-end mark))
1632     (return)))))
1633     (frob start -1)
1634     (frob end 1))
1635     ;;
1636     ;; Do it undoable.
1637     (let* ((start1 (copy-mark start :right-inserting))
1638     (end2 (copy-mark end :left-inserting))
1639     (region (region start1 end2))
1640     (undo-region (copy-region region)))
1641     (fill-region region prefix)
1642     (make-region-undo :twiddle "Fill Lisp Comment Paragraph"
1643     region undo-region))))
1644    
1645     ;;; FILL-LISP-COMMENT-PARAGRAPH-PREFIX -- Internal.
1646     ;;;
1647     ;;; This sets start and end around the prefix to be used for filling. We
1648     ;;; assume we are dealing with a comment. If there is no ";", then we try to
1649     ;;; find some initial whitespace. If there is a ";", we make sure the line is
1650     ;;; blank before it to eliminate ";"'s in the middle of a line of text.
1651     ;;; Finally, if we really have a comment instead of some indented text, we skip
1652     ;;; the ";"'s and any immediately following whitespace. We allow initial
1653     ;;; whitespace, so we can fill strings with the same command.
1654     ;;;
1655     (defun fill-lisp-comment-paragraph-prefix (start end)
1656     (line-start start)
1657     (let ((commentp t)) ; Assumes there's a comment.
1658     (unless (to-line-comment (line-start end) ";")
1659     (find-attribute end :whitespace #'zerop)
1660     #|(when (start-line-p end)
1661     (editor-error "No comment on line, and no initial whitespace."))|#
1662     (setf commentp nil))
1663     (when commentp
1664     (unless (blank-before-p end)
1665     (find-attribute (line-start end) :whitespace #'zerop)
1666     #|(when (start-line-p end)
1667     (editor-error "Semicolon preceded by unindented text."))|#
1668     (setf commentp nil)))
1669     (when commentp
1670     (find-attribute end :lisp-syntax #'(lambda (x) (not (eq x :comment))))
1671     (find-attribute end :whitespace #'zerop))
1672     commentp))
1673    
1674    
1675    
1676     ;;;; "Lisp" mode.
1677    
1678 ram 1.1 (defcommand "LISP Mode" (p)
1679     "Put current buffer in LISP mode."
1680     "Put current buffer in LISP mode."
1681     (declare (ignore p))
1682     (setf (buffer-major-mode (current-buffer)) "LISP"))
1683    
1684    
1685     (defmode "Lisp" :major-p t :setup-function 'setup-lisp-mode)
1686    
1687     (defun setup-lisp-mode (buffer)
1688     (unless (hemlock-bound-p 'current-package :buffer buffer)
1689     (defhvar "Current Package"
1690     "The package used for evaluation of Lisp in this buffer."
1691     :buffer buffer
1692     :value "USER")))
1693    
1694    
1695    
1696     ;;;; Matching parenthesis display.
1697    
1698     (defhvar "Paren Pause Period"
1699     "This is how long commands that deal with \"brackets\" shows the cursor at
1700     the matching \"bracket\" for this number of seconds."
1701     :value 0.5)
1702    
1703     (defcommand "Lisp Insert )" (p)
1704     "Inserts a \")\" and briefly positions the cursor at the matching \"(\"."
1705     "Inserts a \")\" and briefly positions the cursor at the matching \"(\"."
1706     (declare (ignore p))
1707     (let ((point (current-point)))
1708     (insert-character point #\))
1709     (pre-command-parse-check point)
1710     (when (valid-spot point nil)
1711     (with-mark ((m point))
1712     (if (list-offset m -1)
1713     (let ((pause (value paren-pause-period))
1714     (win (current-window)))
1715     (if pause
1716     (unless (show-mark m win pause)
1717     (clear-echo-area)
1718     (message "~A" (line-string (mark-line m))))
1719     (unless (displayed-p m (current-window))
1720     (clear-echo-area)
1721     (message "~A" (line-string (mark-line m))))))
1722     (editor-error))))))
1723    
1724     ;;; Since we use paren highlighting in Lisp mode, we do not want paren
1725     ;;; flashing too.
1726     ;;;
1727     (defhvar "Paren Pause Period"
1728     "This is how long commands that deal with \"brackets\" shows the cursor at
1729     the matching \"bracket\" for this number of seconds."
1730     :value nil
1731     :mode "Lisp")
1732     ;;;
1733     (defhvar "Highlight Open Parens"
1734     "When non-nil, causes open parens to be displayed in a different font when
1735     the cursor is directly to the right of the corresponding close paren."
1736     :value t
1737     :mode "Lisp")
1738 ram 1.3
1739    
1740     (defhvar "Open Paren Finder Function"
1741     "Should be a function that takes a mark for input and returns either NIL
1742     if the mark is not after a close paren, or two (temporary) marks
1743     surrounding the corresponding open paren."
1744     :mode "Lisp"
1745     :value 'lisp-open-paren-finder-function)
1746    
1747     (defun lisp-open-paren-finder-function (mark)
1748     (when (eq (character-attribute :lisp-syntax (previous-character mark))
1749     :close-paren)
1750     (with-mark ((mark mark))
1751     (pre-command-parse-check mark)
1752     (if (not (and (valid-spot mark nil) (list-offset mark -1)))
1753     (values nil nil)
1754     (values mark (mark-after (copy-mark mark)))))))
1755 ram 1.1
1756    
1757    
1758     ;;;; Some mode variables to coordinate with other stuff.
1759    
1760     (defhvar "Auto Fill Space Indent"
1761     "When non-nil, uses \"Indent New Comment Line\" to break lines instead of
1762     \"New Line\"."
1763     :mode "Lisp" :value t)
1764    
1765     (defhvar "Comment Start"
1766     "String that indicates the start of a comment."
1767     :mode "Lisp" :value ";")
1768    
1769     (defhvar "Comment Begin"
1770     "String that is inserted to begin a comment."
1771     :mode "Lisp" :value "; ")
1772    
1773     (defhvar "Indent Function"
1774     "Indentation function which is invoked by \"Indent\" command.
1775     It must take one argument that is the prefix argument."
1776     :value 'indent-for-lisp
1777     :mode "Lisp")

  ViewVC Help
Powered by ViewVC 1.1.5