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

Contents of /src/hemlock/lispmode.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5