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

Contents of /src/hemlock/lispmode.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.9 - (show annotations)
Sat Sep 5 23:21:17 1998 UTC (15 years, 7 months ago) by dtc
Branch: MAIN
Changes since 1.8: +5 -1 lines
Add indent definitions for with-package-iterator and with-alien; from
the 18b release.
1 ;;; -*- Log: hemlock.log; Package: Hemlock -*-
2 ;;;
3 ;;; **********************************************************************
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 ;;;
7 (ext:file-comment
8 "$Header: /tiger/var/lib/cvsroots/cmucl/src/hemlock/lispmode.lisp,v 1.9 1998/09/05 23:21:17 dtc Exp $")
9 ;;;
10 ;;; **********************************************************************
11 ;;;
12 ;;; Hemlock LISP Mode commands
13 ;;;
14 ;;; Written by Ivan Vazquez and Bill Maddox.
15 ;;;
16
17 (in-package "HEMLOCK")
18
19 (declaim (optimize (speed 2))); turn off byte compilation.
20
21
22 ;;;; Variables and lisp-info structure.
23
24 ;;; 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 ;;; LISP-INFO is the structure used to store the data about the line in its
41 ;;; Plist.
42 ;;;
43 ;;; -> 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 ;;;; Macros.
67
68 ;;; 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 ;;;; Parsing functions.
159
160 ;;; PRE-COMMAND-PARSE-CHECK -- Public.
161 ;;;
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 ;;;
172 (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 ;;;; Parse block finders.
230
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 ;;;; PARSE-LISP-LINE-INFO.
291
292 ;;; PARSE-LISP-LINE-INFO -- Internal.
293 ;;;
294 ;;; This parses through the line doing the following things:
295 ;;;
296 ;;; Counting/Setting the NET-OPEN-PARENS & NET-CLOSE-PARENS.
297 ;;;
298 ;;; 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 ;;;
304 (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 RANGES-TO-INGORE, and ENDING-QUOTED."
307 (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
368
369
370 ;;;; String quote utilities.
371
372 ;;; VALID-STRING-QUOTE-P
373 ;;;
374 (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 ,(unless forwardp
379 ;; TEST-MARK should always be right before the String-quote to be
380 ;; checked.
381 `(mark-before ,test-mark))
382 (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 ;;;; DEAL-WITH-STRING-QUOTE.
421
422 ;;; DEAL-WITH-STRING-QUOTE
423 ;;;
424 ;;; Called when a string is begun (i.e. parse hits a #\"). It checks for a
425 ;;; 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 (defun deal-with-string-quote (mark info-struct)
431 "Alter the current line's info struct as necessary as due to encountering a
432 string quote character."
433 (with-mark ((e-mark mark))
434 (cond ((find-valid-string-quote e-mark :forwardp t :cease-at-eol t)
435 ;; 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 (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 ;; If the EOL has been hit before the matching quote then mark the
444 ;; area from MARK to the EOL as invalid.
445 (t
446 (push-range (cons (mark-charpos mark)
447 (1+ (line-length (mark-line mark))))
448 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
454
455
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 ;;;; List offseting.
535
536 ;;; %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 according to the FORWARDP flag."
543 (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 ,(when forwardp
557 ;; When going forward, an unmatching close-paren means the
558 ;; end of list.
559 `(when (<= paren-count 0)
560 (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 ;; 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 (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 ;;;; Form offseting.
742
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 ;;;; Table of special forms with special indenting requirements.
799
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 "destructuring-bind" 2)
852 (defindent "defmacro" 2)
853 (defindent "defpackage" 1)
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 "locally" 0)
875 (defindent "loop" 0)
876 (defindent "macrolet" 1)
877 (defindent "multiple-value-bind" 2)
878 (defindent "multiple-value-call" 1)
879 (defindent "multiple-value-prog1" 1)
880 (defindent "multiple-value-setq" 1)
881 (defindent "prog1" 1)
882 (defindent "progv" 2)
883 (defindent "progn" 0)
884 (defindent "typecase" 1)
885 (defindent "unless" 1)
886 (defindent "unwind-protect" 1)
887 (defindent "when" 1)
888 (defindent "with-input-from-string" 1)
889 (defindent "with-open-file" 1)
890 (defindent "with-open-stream" 1)
891 (defindent "with-output-to-string" 1)
892 (defindent "with-package-iterator" 1)
893
894 ;;; Error/condition system forms.
895 ;;;
896 (defindent "define-condition" 2)
897 (defindent "handler-bind" 1)
898 (defindent "handler-case" 1)
899 (defindent "restart-bind" 1)
900 (defindent "restart-case" 1)
901 (defindent "with-simple-restart" 1)
902 ;;; These are for RESTART-CASE branch formatting.
903 (defindent "store-value" 1)
904 (defindent "use-value" 1)
905 (defindent "muffle-warning" 1)
906 (defindent "abort" 1)
907 (defindent "continue" 1)
908
909 ;;; Debug-internals forms.
910 ;;;
911 (defindent "do-debug-function-blocks" 1)
912 (defindent "di:do-debug-function-blocks" 1)
913 (defindent "do-debug-function-variables" 1)
914 (defindent "di:do-debug-function-variables" 1)
915 (defindent "do-debug-block-locations" 1)
916 (defindent "di:do-debug-block-locations" 1)
917 ;;;
918 ;;; Debug-internals conditions
919 ;;; (define these to make uses of HANDLER-CASE indent branches correctly.)
920 ;;;
921 (defindent "debug-condition" 1)
922 (defindent "di:debug-condition" 1)
923 (defindent "no-debug-info" 1)
924 (defindent "di:no-debug-info" 1)
925 (defindent "no-debug-function-returns" 1)
926 (defindent "di:no-debug-function-returns" 1)
927 (defindent "no-debug-blocks" 1)
928 (defindent "di:no-debug-blocks" 1)
929 (defindent "lambda-list-unavailable" 1)
930 (defindent "di:lambda-list-unavailable" 1)
931 (defindent "no-debug-variables" 1)
932 (defindent "di:no-debug-variables" 1)
933 (defindent "invalid-value" 1)
934 (defindent "di:invalid-value" 1)
935 (defindent "ambiguous-variable-name" 1)
936 (defindent "di:ambiguous-variable-name" 1)
937 (defindent "debug-error" 1)
938 (defindent "di:debug-error" 1)
939 (defindent "unhandled-condition" 1)
940 (defindent "di:unhandled-condition" 1)
941 (defindent "unknown-code-location" 1)
942 (defindent "di:unknown-code-location" 1)
943 (defindent "unknown-debug-variable" 1)
944 (defindent "di:unknown-debug-variable" 1)
945 (defindent "invalid-control-stack-pointer" 1)
946 (defindent "di:invalid-control-stack-pointer" 1)
947 (defindent "frame-function-mismatch" 1)
948 (defindent "di:frame-function-mismatch" 1)
949
950 ;;; Xlib forms.
951 ;;;
952 (defindent "with-gcontext" 1)
953 (defindent "xlib:with-gcontext" 1)
954 (defindent "with-state" 1)
955 (defindent "xlib:with-state" 1)
956 (defindent "with-display" 1)
957 (defindent "xlib:with-display" 1)
958 (defindent "with-event-queue" 1)
959 (defindent "xlib:with-event-queue" 1)
960 (defindent "with-server-grabbed" 1)
961 (defindent "xlib:with-server-grabbed" 1)
962 (defindent "event-case" 1)
963 (defindent "xlib:event-case" 1)
964
965 ;;; CLOS forms.
966 ;;;
967 (defindent "with-slots" 1)
968 (defindent "with-slots*" 2) ; obsolete
969 (defindent "with-accessors" 2)
970 (defindent "with-accessors*" 2) ; obsolete
971 (defindent "defclass" 2)
972 (defindent "print-unreadable-object" 1)
973
974 ;;; System forms.
975 ;;;
976 (defindent "alien-bind" 1)
977 (defindent "def-c-record" 1)
978 (defindent "defrecord" 1)
979 (defindent "add-fd-handler" 2)
980 (defindent "with-fd-handler" 1)
981
982 ;;; Wire forms.
983 (defindent "remote" 1)
984 (defindent "wire:remote" 1)
985 (defindent "remote-value" 1)
986 (defindent "wire:remote-value" 1)
987 (defindent "remote-value-bind" 3)
988 (defindent "wire:remote-value-bind" 3)
989
990 ;;; Multiprocessing forms.
991 (defindent "with-lock-held" 1)
992 (defindent "process-wait" 1)
993
994 ;;; Alien forms.
995 (defindent "with-alien" 1)
996
997
998 ;;;; Indentation.
999
1000 ;;; LISP-INDENTATION -- Internal Interface.
1001 ;;;
1002 (defun lisp-indentation (mark)
1003 "Compute number of spaces which mark should be indented according to
1004 local context and lisp grinding conventions. This assumes mark is at the
1005 beginning of the line to be indented."
1006 (with-mark ((m mark)
1007 (temp mark))
1008 ;; See if we are in a quoted context.
1009 (unless (valid-spot m nil)
1010 (return-from lisp-indentation (lisp-generic-indentation m)))
1011 ;; Look for the paren that opens the containing form.
1012 (unless (backward-up-list m)
1013 (return-from lisp-indentation 0))
1014 ;; Move after the paren, save the start, and find the form name.
1015 (mark-after m)
1016 (with-mark ((start m))
1017 (unless (and (scan-char m :lisp-syntax
1018 (not (or :space :prefix :char-quote)))
1019 (test-char (next-character m) :lisp-syntax :constituent))
1020 (return-from lisp-indentation (mark-column start)))
1021 (with-mark ((fstart m))
1022 (scan-char m :lisp-syntax (not :constituent))
1023 (let* ((fname (nstring-upcase (region-to-string (region fstart m))))
1024 (special-args (or (gethash fname *special-forms*)
1025 (and (> (length fname) 2)
1026 (string= fname "DEF" :end1 3)
1027 (value indent-defanything)))))
1028 (declare (simple-string fname))
1029 ;; Now that we have the form name, did it have special syntax?
1030 (cond (special-args
1031 (with-mark ((spec m))
1032 (cond ((and (form-offset spec special-args)
1033 (mark<= spec mark))
1034 (1+ (mark-column start)))
1035 ((skip-valid-space m)
1036 (mark-column m))
1037 (t
1038 (+ (mark-column start) 3)))))
1039 ;; See if the user seems to have altered the editor's
1040 ;; indentation, and if so, try to adhere to it. This usually
1041 ;; happens when you type in a quoted list constant that line
1042 ;; wraps. You want all the items on successive lines to fall
1043 ;; under the first character after the opening paren, not as if
1044 ;; you are calling a function.
1045 ((and (form-offset temp -1)
1046 (or (blank-before-p temp) (not (same-line-p temp fstart)))
1047 (not (same-line-p temp mark)))
1048 (unless (blank-before-p temp)
1049 (line-start temp)
1050 (find-attribute temp :space #'zerop))
1051 (mark-column temp))
1052 ;; Appears to be a normal form. Is the first arg on the same
1053 ;; line as the form name?
1054 ((skip-valid-space m)
1055 (or (lisp-indentation-check-for-local-def
1056 mark temp fstart start t)
1057 (mark-column m)))
1058 ;; Okay, fall under the first character after the opening paren.
1059 (t
1060 (or (lisp-indentation-check-for-local-def
1061 mark temp fstart start nil)
1062 (mark-column start)))))))))
1063
1064 (defhvar "Lisp Indentation Local Definers"
1065 "Forms with syntax like LABELS, MACROLET, etc."
1066 :value '("LABELS" "MACROLET" "FLET"))
1067
1068 ;;; LISP-INDENTATION-CHECK-FOR-LOCAL-DEF -- Internal.
1069 ;;;
1070 ;;; This is a temporary hack to see how it performs. When we are indenting
1071 ;;; what appears to be a function call, let's look for FLET or MACROLET to see
1072 ;;; if we really are indenting a local definition. If we are, return the
1073 ;;; indentation for a DEFUN; otherwise, nil
1074 ;;;
1075 ;;; Mark is the argument to LISP-INDENTATION. Start is just inside the paren
1076 ;;; of what looks like a function call. If we are in an FLET, arg-list
1077 ;;; indicates whether the local function's arg-list has been entered, that is,
1078 ;;; whether we need to normally indent for a DEFUN body or indent specially for
1079 ;;; the arg-list.
1080 ;;;
1081 (defun lisp-indentation-check-for-local-def (mark temp1 temp2 start arg-list)
1082 ;; We know this succeeds from LISP-INDENTATION.
1083 (backward-up-list (move-mark temp1 mark)) ;Paren for local definition.
1084 (cond ((and (backward-up-list temp1) ;Paren opening the list of defs
1085 (form-offset (move-mark temp2 temp1) -1)
1086 (mark-before temp2)
1087 (backward-up-list temp1) ;Paren for FLET or MACROLET.
1088 (mark= temp1 temp2)) ;Must be in first arg form.
1089 ;; See if the containing form is named FLET or MACROLET.
1090 (mark-after temp1)
1091 (unless (and (scan-char temp1 :lisp-syntax
1092 (not (or :space :prefix :char-quote)))
1093 (test-char (next-character temp1) :lisp-syntax
1094 :constituent))
1095 (return-from lisp-indentation-check-for-local-def nil))
1096 (move-mark temp2 temp1)
1097 (scan-char temp2 :lisp-syntax (not :constituent))
1098 (let ((fname (nstring-upcase (region-to-string (region temp1 temp2)))))
1099 (cond ((not (member fname (value lisp-indentation-local-definers)
1100 :test #'string=))
1101 nil)
1102 (arg-list
1103 (1+ (mark-column start)))
1104 (t
1105 (+ (mark-column start) 3)))))))
1106
1107 ;;; LISP-GENERIC-INDENTATION -- Internal.
1108 ;;;
1109 ;;; LISP-INDENTATION calls this when mark is in a invalid spot, or quoted
1110 ;;; context. If we are inside a string, we return the column one greater
1111 ;;; than the opening double quote. Otherwise, we just use the indentation
1112 ;;; of the first preceding non-blank line.
1113 ;;;
1114 (defun lisp-generic-indentation (mark)
1115 (with-mark ((m mark))
1116 (form-offset m -1)
1117 (cond ((eq (character-attribute :lisp-syntax (next-character m))
1118 :string-quote)
1119 (1+ (mark-column m)))
1120 (t
1121 (let* ((line (mark-line mark))
1122 (prev (do ((line (line-previous line) (line-previous line)))
1123 ((not (and line (blank-line-p line))) line))))
1124 (cond (prev
1125 (line-start mark prev)
1126 (find-attribute mark :space #'zerop)
1127 (mark-column mark))
1128 (t 0)))))))
1129
1130 ;;; Skip-Valid-Space -- Internal
1131 ;;;
1132 ;;; Skip over any space on the line Mark is on, stopping at the first valid
1133 ;;; non-space character. If there is none on the line, return nil.
1134 ;;;
1135 (defun skip-valid-space (mark)
1136 (loop
1137 (scan-char mark :lisp-syntax (not :space))
1138 (let ((val (character-attribute :lisp-syntax
1139 (next-character mark))))
1140 (cond ((eq val :newline) (return nil))
1141 ((valid-spot mark t) (return mark))))
1142 (mark-after mark)))
1143
1144 (declaim (optimize (speed 0))); byte compile again
1145
1146
1147 ;;;; Indentation commands and hook functions.
1148
1149 (defcommand "Defindent" (p)
1150 "Define the Lisp indentation for the current function.
1151 The indentation is a non-negative integer which is the number
1152 of special arguments for the form. Examples: 2 for Do, 1 for Dolist.
1153 If a prefix argument is supplied, then delete the indentation information."
1154 "Do a defindent, man!"
1155 (with-mark ((m (current-point)))
1156 (pre-command-parse-check m)
1157 (unless (backward-up-list m) (editor-error))
1158 (mark-after m)
1159 (with-mark ((n m))
1160 (scan-char n :lisp-syntax (not :constituent))
1161 (let ((s (region-to-string (region m n))))
1162 (declare (simple-string s))
1163 (when (zerop (length s)) (editor-error))
1164 (if p
1165 (defindent s nil)
1166 (let ((i (prompt-for-integer
1167 :prompt (format nil "Indentation for ~A: " s)
1168 :help "Number of special arguments.")))
1169 (when (minusp i)
1170 (editor-error "Indentation must be non-negative."))
1171 (defindent s i))))))
1172 (indent-command nil))
1173
1174 (defcommand "Indent Form" (p)
1175 "Indent Lisp code in the next form."
1176 "Indent Lisp code in the next form."
1177 (declare (ignore p))
1178 (let ((point (current-point)))
1179 (pre-command-parse-check point)
1180 (with-mark ((m point))
1181 (unless (form-offset m 1) (editor-error))
1182 (lisp-indent-region (region point m) "Indent Form"))))
1183
1184 ;;; LISP-INDENT-REGION -- Internal.
1185 ;;;
1186 ;;; This indents a region of Lisp code without doing excessive redundant
1187 ;;; computation. We parse the entire region once, then scan through doing
1188 ;;; indentation on each line. We forcibly reparse each line that we indent so
1189 ;;; that the list operations done to determine indentation of subsequent lines
1190 ;;; will work. This is done undoably with save1, save2, buf-region, and
1191 ;;; undo-region.
1192 ;;;
1193 (defun lisp-indent-region (region &optional (undo-text "Lisp region indenting"))
1194 (check-region-query-size region)
1195 (let ((start (region-start region))
1196 (end (region-end region)))
1197 (with-mark ((m1 start)
1198 (m2 end))
1199 (funcall (value parse-start-function) m1)
1200 (funcall (value parse-end-function) m2)
1201 (parse-over-block (mark-line m1) (mark-line m2)))
1202 (let* ((first-line (mark-line start))
1203 (last-line (mark-line end))
1204 (prev (line-previous first-line))
1205 (prev-line-info
1206 (and prev (getf (line-plist prev) 'lisp-info)))
1207 (save1 (line-start (copy-mark start :right-inserting)))
1208 (save2 (line-end (copy-mark end :left-inserting)))
1209 (buf-region (region save1 save2))
1210 (undo-region (copy-region buf-region)))
1211 (with-mark ((bol start :left-inserting))
1212 (do ((line first-line (line-next line)))
1213 (nil)
1214 (line-start bol line)
1215 (insert-lisp-indentation bol)
1216 (let ((line-info (getf (line-plist line) 'lisp-info)))
1217 (parse-lisp-line-info bol line-info prev-line-info)
1218 (setq prev-line-info line-info))
1219 (when (eq line last-line) (return nil))))
1220 (make-region-undo :twiddle undo-text buf-region undo-region))))
1221
1222 ;;; INDENT-FOR-LISP -- Internal.
1223 ;;;
1224 ;;; This is the value of "Indent Function" for "Lisp" mode.
1225 ;;;
1226 (defun indent-for-lisp (mark)
1227 (line-start mark)
1228 (pre-command-parse-check mark)
1229 (insert-lisp-indentation mark))
1230
1231 (defun insert-lisp-indentation (m)
1232 (delete-horizontal-space m)
1233 (funcall (value indent-with-tabs) m (lisp-indentation m)))
1234
1235
1236
1237 ;;;; Most "Lisp" mode commands.
1238
1239 (defcommand "Beginning of Defun" (p)
1240 "Move the point to the beginning of a top-level form.
1241 with an argument, skips the previous p top-level forms."
1242 "Move the point to the beginning of a top-level form."
1243 (let ((point (current-point))
1244 (count (or p 1)))
1245 (pre-command-parse-check point)
1246 (if (minusp count)
1247 (end-of-defun-command (- count))
1248 (unless (top-level-offset point (- count))
1249 (editor-error)))))
1250
1251 ;;; "End of Defun", with a positive p (the normal case), does something weird.
1252 ;;; Get a mark at the beginning of the defun, and then offset it forward one
1253 ;;; less top level form than we want. This sets us up to use FORM-OFFSET which
1254 ;;; allows us to leave the point immediately after the defun. If we used
1255 ;;; TOP-LEVEL-OFFSET one less than p on the mark at the end of the current
1256 ;;; defun, point would be left at the beginning of the p+1'st form instead of
1257 ;;; at the end of the p'th form.
1258 ;;;
1259 (defcommand "End of Defun" (p)
1260 "Move the point to the end of a top-level form.
1261 With an argument, skips the next p top-level forms."
1262 "Move the point to the end of a top-level form."
1263 (let ((point (current-point))
1264 (count (or p 1)))
1265 (pre-command-parse-check point)
1266 (if (minusp count)
1267 (beginning-of-defun-command (- count))
1268 (with-mark ((m point)
1269 (dummy point))
1270 (cond ((not (mark-top-level-form m dummy))
1271 (editor-error "No current or next top level form."))
1272 (t
1273 (unless (top-level-offset m (1- count))
1274 (editor-error "Not enough top level forms."))
1275 ;; We might be one unparsed for away.
1276 (pre-command-parse-check m)
1277 (unless (form-offset m 1)
1278 (editor-error "Not enough top level forms."))
1279 (when (blank-after-p m) (line-offset m 1 0))
1280 (move-mark point m)))))))
1281
1282 (defcommand "Forward List" (p)
1283 "Skip over the next Lisp list.
1284 With argument, skips the next p lists."
1285 "Skip over the next Lisp list."
1286 (let ((point (current-point))
1287 (count (or p 1)))
1288 (pre-command-parse-check point)
1289 (unless (list-offset point count) (editor-error))))
1290
1291 (defcommand "Backward List" (p)
1292 "Skip over the previous Lisp list.
1293 With argument, skips the previous p lists."
1294 "Skip over the previous Lisp list."
1295 (let ((point (current-point))
1296 (count (- (or p 1))))
1297 (pre-command-parse-check point)
1298 (unless (list-offset point count) (editor-error))))
1299
1300 (defcommand "Forward Form" (p)
1301 "Skip over the next Form.
1302 With argument, skips the next p Forms."
1303 "Skip over the next Form."
1304 (let ((point (current-point))
1305 (count (or p 1)))
1306 (pre-command-parse-check point)
1307 (unless (form-offset point count) (editor-error))))
1308
1309 (defcommand "Backward Form" (p)
1310 "Skip over the previous Form.
1311 With argument, skips the previous p Forms."
1312 "Skip over the previous Form."
1313 (let ((point (current-point))
1314 (count (- (or p 1))))
1315 (pre-command-parse-check point)
1316 (unless (form-offset point count) (editor-error))))
1317
1318 (defcommand "Mark Form" (p)
1319 "Set the mark at the end of the next Form.
1320 With a positive argument, set the mark after the following p
1321 Forms. With a negative argument, set the mark before
1322 the preceding -p Forms."
1323 "Set the mark at the end of the next Form."
1324 (with-mark ((m (current-point)))
1325 (pre-command-parse-check m)
1326 (let ((count (or p 1))
1327 (mark (push-buffer-mark (copy-mark m) t)))
1328 (if (form-offset m count)
1329 (move-mark mark m)
1330 (editor-error)))))
1331
1332 (defcommand "Mark Defun" (p)
1333 "Puts the region around the next or containing top-level form.
1334 The point is left before the form and the mark is placed immediately
1335 after it."
1336 "Puts the region around the next or containing top-level form."
1337 (declare (ignore p))
1338 (let ((point (current-point)))
1339 (pre-command-parse-check point)
1340 (with-mark ((start point)
1341 (end point))
1342 (cond ((not (mark-top-level-form start end))
1343 (editor-error "No current or next top level form."))
1344 (t
1345 (move-mark point start)
1346 (move-mark (push-buffer-mark (copy-mark point) t) end))))))
1347
1348 (defcommand "Forward Kill Form" (p)
1349 "Kill the next Form.
1350 With a positive argument, kills the next p Forms.
1351 Kills backward with a negative argument."
1352 "Kill the next Form."
1353 (with-mark ((m1 (current-point))
1354 (m2 (current-point)))
1355 (pre-command-parse-check m1)
1356 (let ((count (or p 1)))
1357 (unless (form-offset m1 count) (editor-error))
1358 (if (minusp count)
1359 (kill-region (region m1 m2) :kill-backward)
1360 (kill-region (region m2 m1) :kill-forward)))))
1361
1362 (defcommand "Backward Kill Form" (p)
1363 "Kill the previous Form.
1364 With a positive argument, kills the previous p Forms.
1365 Kills forward with a negative argument."
1366 "Kill the previous Form."
1367 (forward-kill-form-command (- (or p 1))))
1368
1369 (defcommand "Extract Form" (p)
1370 "Replace the current containing list with the next form. The entire affected
1371 area is pushed onto the kill ring. If an argument is supplied, that many
1372 upward levels of list nesting is replaced by the next form."
1373 "Replace the current containing list with the next form. The entire affected
1374 area is pushed onto the kill ring. If an argument is supplied, that many
1375 upward levels of list nesting is replaced by the next form."
1376 (let ((point (current-point)))
1377 (pre-command-parse-check point)
1378 (with-mark ((form-start point :right-inserting)
1379 (form-end point))
1380 (unless (form-offset form-end 1) (editor-error))
1381 (form-offset (move-mark form-start form-end) -1)
1382 (with-mark ((containing-start form-start :left-inserting)
1383 (containing-end form-end :left-inserting))
1384 (dotimes (i (or p 1))
1385 (unless (and (forward-up-list containing-end)
1386 (backward-up-list containing-start))
1387 (editor-error)))
1388 (let ((r (copy-region (region form-start form-end))))
1389 (ring-push (delete-and-save-region
1390 (region containing-start containing-end))
1391 *kill-ring*)
1392 (ninsert-region point r)
1393 (move-mark point form-start))))))
1394
1395 (defcommand "Extract List" (p)
1396 "Extract the current list.
1397 The current list replaces the surrounding list. The entire affected
1398 area is pushed on the kill-ring. With prefix argument, remove that
1399 many surrounding lists."
1400 "Replace the P containing lists with the current one."
1401 (let ((point (current-point)))
1402 (pre-command-parse-check point)
1403 (with-mark ((lstart point :right-inserting)
1404 (lend point))
1405 (if (eq (character-attribute :lisp-syntax (next-character lstart))
1406 :open-paren)
1407 (mark-after lend)
1408 (unless (backward-up-list lstart) (editor-error)))
1409 (unless (forward-up-list lend) (editor-error))
1410 (with-mark ((rstart lstart)
1411 (rend lend))
1412 (dotimes (i (or p 1))
1413 (unless (and (forward-up-list rend) (backward-up-list rstart))
1414 (editor-error)))
1415 (let ((r (copy-region (region lstart lend))))
1416 (ring-push (delete-and-save-region (region rstart rend))
1417 *kill-ring*)
1418 (ninsert-region point r)
1419 (move-mark point lstart))))))
1420
1421 (defcommand "Transpose Forms" (p)
1422 "Transpose Forms immediately preceding and following the point.
1423 With a zero argument, tranposes the Forms at the point and the mark.
1424 With a positive argument, transposes the Form preceding the point
1425 with the p-th one following it. With a negative argument, transposes the
1426 Form following the point with the p-th one preceding it."
1427 "Transpose Forms immediately preceding and following the point."
1428 (let ((point (current-point))
1429 (count (or p 1)))
1430 (pre-command-parse-check point)
1431 (if (zerop count)
1432 (let ((mark (current-mark)))
1433 (with-mark ((s1 mark :left-inserting)
1434 (s2 point :left-inserting))
1435 (scan-char s1 :whitespace nil)
1436 (scan-char s2 :whitespace nil)
1437 (with-mark ((e1 s1 :right-inserting)
1438 (e2 s2 :right-inserting))
1439 (unless (form-offset e1 1) (editor-error))
1440 (unless (form-offset e2 1) (editor-error))
1441 (ninsert-region s1 (delete-and-save-region (region s2 e2)))
1442 (ninsert-region s2 (delete-and-save-region (region s1 e1))))))
1443 (let ((fcount (if (plusp count) count 1))
1444 (bcount (if (plusp count) 1 count)))
1445 (with-mark ((s1 point :left-inserting)
1446 (e2 point :right-inserting))
1447 (dotimes (i bcount)
1448 (unless (form-offset s1 -1) (editor-error)))
1449 (dotimes (i fcount)
1450 (unless (form-offset e2 1) (editor-error)))
1451 (with-mark ((e1 s1 :right-inserting)
1452 (s2 e2 :left-inserting))
1453 (unless (form-offset e1 1) (editor-error))
1454 (unless (form-offset s2 -1) (editor-error))
1455 (ninsert-region s1 (delete-and-save-region (region s2 e2)))
1456 (ninsert-region s2 (delete-and-save-region (region s1 e1)))
1457 (move-mark point s2)))))))
1458
1459
1460 (defcommand "Insert ()" (p)
1461 "Insert a pair of parentheses ().
1462 With positive argument, puts parentheses around the next p
1463 Forms. The point is positioned after the open parenthesis."
1464 "Insert a pair of parentheses ()."
1465 (let ((point (current-point))
1466 (count (or p 0)))
1467 (pre-command-parse-check point)
1468 (cond ((not (minusp count))
1469 (insert-character point #\()
1470 (with-mark ((tmark point))
1471 (unless (form-offset tmark count) (editor-error))
1472 (cond ((mark= tmark point)
1473 (insert-character point #\))
1474 (mark-before point))
1475 (t (insert-character tmark #\))))))
1476 (t (editor-error)))))
1477
1478
1479 (defcommand "Move Over )" (p)
1480 "Move past the next close parenthesis, and start a new line.
1481 Any indentation preceding the preceding the parenthesis is deleted,
1482 and the new line is indented."
1483 "Move past the next close parenthesis, and start a new line."
1484 (declare (ignore p))
1485 (let ((point (current-point)))
1486 (pre-command-parse-check point)
1487 (with-mark ((m point :left-inserting))
1488 (cond ((scan-char m :lisp-syntax :close-paren)
1489 (delete-horizontal-space m)
1490 (mark-after m)
1491 (move-mark point m)
1492 (delete-mark m)
1493 (indent-new-line-command 1))
1494 (t
1495 (delete-mark m)
1496 (editor-error))))))
1497
1498
1499 (defcommand "Forward Up List" (p)
1500 "Move forward past a one containing )."
1501 "Move forward past a one containing )."
1502 (let ((point (current-point))
1503 (count (or p 1)))
1504 (pre-command-parse-check point)
1505 (if (minusp count)
1506 (backward-up-list-command (- count))
1507 (with-mark ((m point))
1508 (dotimes (i count (move-mark point m))
1509 (unless (forward-up-list m) (editor-error)))))))
1510
1511
1512 (defcommand "Backward Up List" (p)
1513 "Move backward past a one containing (."
1514 "Move backward past a one containing (."
1515 (let ((point (current-point))
1516 (count (or p 1)))
1517 (pre-command-parse-check point)
1518 (if (minusp count)
1519 (forward-up-list-command (- count))
1520 (with-mark ((m point))
1521 (dotimes (i count (move-mark point m))
1522 (unless (backward-up-list m) (editor-error)))))))
1523
1524
1525 (defcommand "Down List" (p)
1526 "Move down a level in list structure.
1527 With argument, moves down p levels."
1528 "Move down a level in list structure."
1529 (let ((point (current-point))
1530 (count (or p 1)))
1531 (pre-command-parse-check point)
1532 (with-mark ((m point))
1533 (dotimes (i count (move-mark point m))
1534 (unless (and (scan-char m :lisp-syntax :open-paren)
1535 (mark-after m))
1536 (editor-error))))))
1537
1538
1539
1540 ;;;; Filling Lisp comments, strings, and indented text.
1541
1542 (defhvar "Fill Lisp Comment Paragraph Confirm"
1543 "This determines whether \"Fill Lisp Comment Paragraph\" will prompt for
1544 confirmation to fill contiguous lines with the same initial whitespace when
1545 it is invoked outside of a comment or string."
1546 :value t)
1547
1548 (defcommand "Fill Lisp Comment Paragraph" (p)
1549 "This fills a flushleft or indented Lisp comment.
1550 This also fills Lisp string literals using the proper indentation as a
1551 filling prefix. When invoked outside of a comment or string, this tries
1552 to fill all contiguous lines beginning with the same initial, non-empty
1553 blankspace. When filling a comment, the current line is used to determine a
1554 fill prefix by taking all the initial whitespace on the line, the semicolons,
1555 and any whitespace following the semicolons."
1556 "Fills a flushleft or indented Lisp comment."
1557 (declare (ignore p))
1558 (let ((point (current-point)))
1559 (pre-command-parse-check point)
1560 (with-mark ((start point)
1561 (end point)
1562 (m point))
1563 (let ((commentp (fill-lisp-comment-paragraph-prefix start end)))
1564 (cond (commentp
1565 (fill-lisp-comment-or-indented-text start end))
1566 ((and (not (valid-spot m nil))
1567 (form-offset m -1)
1568 (eq (character-attribute :lisp-syntax (next-character m))
1569 :string-quote))
1570 (fill-lisp-string m))
1571 ((or (not (value fill-lisp-comment-paragraph-confirm))
1572 (prompt-for-y-or-n
1573 :prompt '("Not in a comment or string. Fill contiguous ~
1574 lines with the same initial whitespace? ")))
1575 (fill-lisp-comment-or-indented-text start end)))))))
1576
1577 ;;; FILL-LISP-STRING -- Internal.
1578 ;;;
1579 ;;; This fills the Lisp string containing mark as if it had been entered using
1580 ;;; Hemlock's Lisp string indentation, "Indent Function" for "Lisp" mode. This
1581 ;;; assumes the area around mark has already been PRE-COMMAND-PARSE-CHECK'ed,
1582 ;;; and it ensures the string ends before doing any filling. This function
1583 ;;; is undo'able.
1584 ;;;
1585 (defun fill-lisp-string (mark)
1586 (with-mark ((end mark))
1587 (unless (form-offset end 1)
1588 (editor-error "Attempted to fill Lisp string, but it doesn't end?"))
1589 (let* ((mark (copy-mark mark :left-inserting))
1590 (end (copy-mark end :left-inserting))
1591 (string-region (region mark end))
1592 (undo-region (copy-region string-region))
1593 (hack (make-empty-region)))
1594 ;; Generate prefix.
1595 (funcall (value indent-with-tabs)
1596 (region-end hack) (1+ (mark-column mark)))
1597 ;; Skip opening double quote and fill string starting on its own line.
1598 (mark-after mark)
1599 (insert-character mark #\newline)
1600 (line-start mark)
1601 (setf (mark-kind mark) :right-inserting)
1602 (fill-region string-region (region-to-string hack))
1603 ;; Clean up inserted prefix on first line, delete inserted newline, and
1604 ;; move before the double quote for undo.
1605 (with-mark ((text mark :left-inserting))
1606 (find-attribute text :whitespace #'zerop)
1607 (delete-region (region mark text)))
1608 (delete-characters mark -1)
1609 (mark-before mark)
1610 ;; Save undo.
1611 (make-region-undo :twiddle "Fill Lisp Comment Paragraph"
1612 string-region undo-region))))
1613
1614 ;;; FILL-LISP-COMMENT-OR-INDENTED-TEXT -- Internal.
1615 ;;;
1616 ;;; This fills all contiguous lines around start and end containing fill prefix
1617 ;;; designated by the region between start and end. These marks can only be
1618 ;;; equal when there is no comment and no initial whitespace. This is a bad
1619 ;;; situation since this function in that situation would fill the entire
1620 ;;; buffer into one paragraph. This function is undo'able.
1621 ;;;
1622 (defun fill-lisp-comment-or-indented-text (start end)
1623 (when (mark= start end)
1624 (editor-error "This command only fills Lisp comments, strings, or ~
1625 indented text, but this line is flushleft."))
1626 ;;
1627 ;; Find comment block.
1628 (let* ((prefix (region-to-string (region start end)))
1629 (length (length prefix)))
1630 (declare (simple-string prefix))
1631 (flet ((frob (mark direction)
1632 (loop
1633 (let* ((line (line-string (mark-line mark)))
1634 (line-len (length line)))
1635 (declare (simple-string line))
1636 (unless (string= line prefix :end1 (min line-len length))
1637 (when (= direction -1)
1638 (unless (same-line-p mark end) (line-offset mark 1 0)))
1639 (return)))
1640 (unless (line-offset mark direction 0)
1641 (when (= direction 1) (line-end mark))
1642 (return)))))
1643 (frob start -1)
1644 (frob end 1))
1645 ;;
1646 ;; Do it undoable.
1647 (let* ((start1 (copy-mark start :right-inserting))
1648 (end2 (copy-mark end :left-inserting))
1649 (region (region start1 end2))
1650 (undo-region (copy-region region)))
1651 (fill-region region prefix)
1652 (make-region-undo :twiddle "Fill Lisp Comment Paragraph"
1653 region undo-region))))
1654
1655 ;;; FILL-LISP-COMMENT-PARAGRAPH-PREFIX -- Internal.
1656 ;;;
1657 ;;; This sets start and end around the prefix to be used for filling. We
1658 ;;; assume we are dealing with a comment. If there is no ";", then we try to
1659 ;;; find some initial whitespace. If there is a ";", we make sure the line is
1660 ;;; blank before it to eliminate ";"'s in the middle of a line of text.
1661 ;;; Finally, if we really have a comment instead of some indented text, we skip
1662 ;;; the ";"'s and any immediately following whitespace. We allow initial
1663 ;;; whitespace, so we can fill strings with the same command.
1664 ;;;
1665 (defun fill-lisp-comment-paragraph-prefix (start end)
1666 (line-start start)
1667 (let ((commentp t)) ; Assumes there's a comment.
1668 (unless (to-line-comment (line-start end) ";")
1669 (find-attribute end :whitespace #'zerop)
1670 #|(when (start-line-p end)
1671 (editor-error "No comment on line, and no initial whitespace."))|#
1672 (setf commentp nil))
1673 (when commentp
1674 (unless (blank-before-p end)
1675 (find-attribute (line-start end) :whitespace #'zerop)
1676 #|(when (start-line-p end)
1677 (editor-error "Semicolon preceded by unindented text."))|#
1678 (setf commentp nil)))
1679 (when commentp
1680 (find-attribute end :lisp-syntax #'(lambda (x) (not (eq x :comment))))
1681 (find-attribute end :whitespace #'zerop))
1682 commentp))
1683
1684
1685
1686 ;;;; "Lisp" mode.
1687
1688 (defcommand "LISP Mode" (p)
1689 "Put current buffer in LISP mode."
1690 "Put current buffer in LISP mode."
1691 (declare (ignore p))
1692 (setf (buffer-major-mode (current-buffer)) "LISP"))
1693
1694
1695 (defmode "Lisp" :major-p t :setup-function 'setup-lisp-mode)
1696
1697 (defun setup-lisp-mode (buffer)
1698 (unless (hemlock-bound-p 'current-package :buffer buffer)
1699 (defhvar "Current Package"
1700 "The package used for evaluation of Lisp in this buffer."
1701 :buffer buffer
1702 :value "USER")))
1703
1704
1705
1706 ;;;; Matching parenthesis display.
1707
1708 (defhvar "Paren Pause Period"
1709 "This is how long commands that deal with \"brackets\" shows the cursor at
1710 the matching \"bracket\" for this number of seconds."
1711 :value 0.5)
1712
1713 (defcommand "Lisp Insert )" (p)
1714 "Inserts a \")\" and briefly positions the cursor at the matching \"(\"."
1715 "Inserts a \")\" and briefly positions the cursor at the matching \"(\"."
1716 (declare (ignore p))
1717 (let ((point (current-point)))
1718 (insert-character point #\))
1719 (pre-command-parse-check point)
1720 (when (valid-spot point nil)
1721 (with-mark ((m point))
1722 (if (list-offset m -1)
1723 (let ((pause (value paren-pause-period))
1724 (win (current-window)))
1725 (if pause
1726 (unless (show-mark m win pause)
1727 (clear-echo-area)
1728 (message "~A" (line-string (mark-line m))))
1729 (unless (displayed-p m (current-window))
1730 (clear-echo-area)
1731 (message "~A" (line-string (mark-line m))))))
1732 (editor-error))))))
1733
1734 ;;; Since we use paren highlighting in Lisp mode, we do not want paren
1735 ;;; flashing too.
1736 ;;;
1737 (defhvar "Paren Pause Period"
1738 "This is how long commands that deal with \"brackets\" shows the cursor at
1739 the matching \"bracket\" for this number of seconds."
1740 :value nil
1741 :mode "Lisp")
1742 ;;;
1743 (defhvar "Highlight Open Parens"
1744 "When non-nil, causes open parens to be displayed in a different font when
1745 the cursor is directly to the right of the corresponding close paren."
1746 :value t
1747 :mode "Lisp")
1748
1749
1750 (defhvar "Open Paren Finder Function"
1751 "Should be a function that takes a mark for input and returns either NIL
1752 if the mark is not after a close paren, or two (temporary) marks
1753 surrounding the corresponding open paren."
1754 :mode "Lisp"
1755 :value 'lisp-open-paren-finder-function)
1756
1757 (defun lisp-open-paren-finder-function (mark)
1758 (when (eq (character-attribute :lisp-syntax (previous-character mark))
1759 :close-paren)
1760 (with-mark ((mark mark))
1761 (pre-command-parse-check mark)
1762 (if (not (and (valid-spot mark nil) (list-offset mark -1)))
1763 (values nil nil)
1764 (values mark (mark-after (copy-mark mark)))))))
1765
1766
1767
1768 ;;;; Some mode variables to coordinate with other stuff.
1769
1770 (defhvar "Auto Fill Space Indent"
1771 "When non-nil, uses \"Indent New Comment Line\" to break lines instead of
1772 \"New Line\"."
1773 :mode "Lisp" :value t)
1774
1775 (defhvar "Comment Start"
1776 "String that indicates the start of a comment."
1777 :mode "Lisp" :value ";")
1778
1779 (defhvar "Comment Begin"
1780 "String that is inserted to begin a comment."
1781 :mode "Lisp" :value "; ")
1782
1783 (defhvar "Indent Function"
1784 "Indentation function which is invoked by \"Indent\" command.
1785 It must take one argument that is the prefix argument."
1786 :value 'indent-for-lisp
1787 :mode "Lisp")

  ViewVC Help
Powered by ViewVC 1.1.5