close Warning: Can't use blame annotator:
No changeset 2064 in the repository

source: release/1.7/source/cocoa-ide/hemlock/src/lispmode.lisp

Last change on this file was 14907, checked in by R. Matthew Emerson, 13 years ago

Merge selected changes from trunk.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 86.3 KB
RevLine 
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#+CMU (ext:file-comment
8 "$Header$")
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
23;;;; Variables and lisp-info structure.
24
25;;; 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;;; LISP-INFO is the structure used to store the data about the line in its
42;;; Plist.
43;;;
44;;; -> BEGINS-QUOTED, ENDING-QUOTED are both slots that tell whether or not
45;;; a line's begining and/or ending are quoted, and if so, how.
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 where :lisp-syntax attributes are ignored. 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) ; nil or quote char attribute or comment nesting depth
59 (ending-quoted nil) ; nil or quote char attribute or comment nesting depth
60 (ranges-to-ignore nil)
61 (net-open-parens 0 :type fixnum)
62 (net-close-parens 0 :type fixnum)
63 (signature-slot))
64
65
66
67
68;;;; Macros.
69
70;;; The following Macros exist to make it easy to acces the Syntax primitives
71;;; without uglifying the code. They were originally written by Maddox.
72;;;
73
74(defmacro scan-char (mark attribute values)
75 `(find-attribute ,mark ',attribute ,(attr-predicate values)))
76
77(defmacro rev-scan-char (mark attribute values)
78 `(reverse-find-attribute ,mark ',attribute ,(attr-predicate values)))
79
80(defmacro test-char (char attribute values)
81 `(let ((x (character-attribute ',attribute ,char)))
82 ,(attr-predicate-aux values)))
83
84(eval-when (:compile-toplevel :execute :load-toplevel)
85(defun attr-predicate (values)
86 (cond ((eq values 't)
87 '#'plusp)
88 ((eq values 'nil)
89 '#'zerop)
90 (t `#'(lambda (x) ,(attr-predicate-aux values)))))
91
92(defun attr-predicate-aux (values)
93 (cond ((eq values t)
94 '(plusp x))
95 ((eq values nil)
96 '(zerop x))
97 ((symbolp values)
98 `(eq x ',values))
99 ((and (listp values) (member (car values) '(and or not)))
100 (cons (car values) (mapcar #'attr-predicate-aux (cdr values))))
101 (t (error "Illegal form in attribute pattern - ~S" values))))
102
103); Eval-When
104
105;;;
106;;; FIND-LISP-CHAR
107
108(defmacro find-lisp-char (mark)
109 "Move MARK to next :LISP-SYNTAX character, if one isn't found, return NIL."
110 `(find-attribute ,mark :lisp-syntax
111 #'(lambda (x)
112 (member x '(:open-paren :close-paren :newline :comment :prefix-dispatch
113 :char-quote :symbol-quote :string-quote)))))
114;;;
115;;; PUSH-RANGE
116
117(defmacro push-range (new-range info-struct)
118 "Insert NEW-RANGE into the LISP-INFO-RANGES-TO-IGNORE slot of the INFO-STRUCT."
119 `(when ,new-range
120 (setf (lisp-info-ranges-to-ignore ,info-struct)
121 (cons ,new-range (lisp-info-ranges-to-ignore ,info-struct)))))
122;;;
123;;; SCAN-DIRECTION
124
125(defmacro scan-direction (mark forwardp &rest forms)
126 "Expand to a form that scans either backward or forward according to Forwardp."
127 (if forwardp
128 `(scan-char ,mark ,@forms)
129 `(rev-scan-char ,mark ,@forms)))
130;;;
131;;; DIRECTION-CHAR
132
133(defmacro direction-char (mark forwardp)
134 "Expand to a form that returns either the previous or next character according
135 to Forwardp."
136 (if forwardp
137 `(next-character ,mark)
138 `(previous-character ,mark)))
139
140;;;
141;;; NEIGHBOR-MARK
142
143(defmacro neighbor-mark (mark forwardp)
144 "Expand to a form that moves MARK either backward or forward one character,
145 depending on FORWARDP."
146 (if forwardp
147 `(mark-after ,mark)
148 `(mark-before ,mark)))
149
150;;;
151;;; NEIGHBOR-LINE
152
153(defmacro neighbor-line (line forwardp)
154 "Expand to return the next or previous line, according to Forwardp."
155 (if forwardp
156 `(line-next ,line)
157 `(line-previous ,line)))
158
159
160
161;;;; Parsing functions.
162
163;;; PRE-COMMAND-PARSE-CHECK -- Public.
164;;;
165(defun pre-command-parse-check (mark &optional (fer-sure-parse nil))
166 "Parse the area before the command is actually executed."
167 (with-mark ((top mark)
168 (bottom mark))
169 (funcall (value parse-start-function) top)
170 (funcall (value parse-end-function) bottom)
171 (parse-over-block (mark-line top) (mark-line bottom) fer-sure-parse)))
172
173;;; PARSE-OVER-BLOCK
174;;;
175(defun parse-over-block (start-line end-line &optional (fer-sure-parse nil))
176 "Parse over an area indicated from END-LINE to START-LINE."
177 (let ((test-line start-line)
178 prev-line-info)
179
180 (with-mark ((mark (mark test-line 0)))
181
182 ; Set the pre-begining and post-ending lines to delimit the range
183 ; of action any command will take. This means set the lisp-info of the
184 ; lines immediately before and after the block to Nil.
185
186 (when (line-previous start-line)
187 (setf (getf (line-plist (line-previous start-line)) 'lisp-info) nil))
188 (when (line-next end-line)
189 (setf (getf (line-plist (line-next end-line)) 'lisp-info) nil))
190
191 (loop
192 (let ((line-info (getf (line-plist test-line) 'lisp-info)))
193
194 ;; Reparse the line when any of the following are true:
195 ;;
196 ;; FER-SURE-PARSE is T
197 ;;
198 ;; LINE-INFO or PREV-LINE-INFO are Nil.
199 ;;
200 ;; If the line begins quoted and the previous one wasn't
201 ;; ended quoted.
202 ;;
203 ;; The Line's signature slot is invalid (the line has changed).
204 ;;
205
206 (when (or fer-sure-parse
207 (not line-info)
208 (not (eq (lisp-info-begins-quoted line-info)
209 (let ((prev (and prev-line-info (lisp-info-ending-quoted prev-line-info))))
210 (and (not (eq prev :char-quote)) prev))))
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
231;;;; Parse block finders.
232
233
234(defun start-of-parse-block (mark)
235 (buffer-start mark))
236
237(defun end-of-parse-block (mark)
238 (buffer-end mark))
239
240;;;
241;;; START-OF-SEARCH-LINE
242
243(defun start-of-search-line (line)
244 "Set LINE to the begining line of the block of text to parse."
245 (with-mark ((mark (mark line 0)))
246 (funcall (value 'Parse-Start-Function) mark)
247 (setq line (mark-line mark))))
248
249;;;
250;;; END-OF-SEACH-LINE
251
252(defun end-of-search-line (line)
253 "Set LINE to the ending line of the block of text to parse."
254 (with-mark ((mark (mark line 0)))
255 (funcall (value 'Parse-End-Function) mark)
256 (setq line (mark-line mark))))
257
258
259
260;;;; PARSE-LISP-LINE-INFO.
261
262;;; PARSE-LISP-LINE-INFO -- Internal.
263;;;
264;;; This parses through the line doing the following things:
265;;;
266;;; Counting/Setting the NET-OPEN-PARENS & NET-CLOSE-PARENS.
267;;;
268;;; Making all areas of the line that should be invalid (comments,
269;;; char-quotes, and the inside of strings) and such be in
270;;; RANGES-TO-IGNORE.
271;;;
272;;; Set BEGINS-QUOTED and ENDING-QUOTED
273;;;
274(defun parse-lisp-line-info (mark line-info prev-line-info)
275 "Parse line and set line information like NET-OPEN-PARENS, NET-CLOSE-PARENS,
276 RANGES-TO-INGORE, and ENDING-QUOTED."
277 (let ((net-open-parens 0)
278 (net-close-parens 0))
279 (declare (fixnum net-open-parens net-close-parens))
280
281 ;; Re-set the slots necessary
282
283 (setf (lisp-info-ranges-to-ignore line-info) nil)
284
285 (setf (lisp-info-ending-quoted line-info) nil)
286
287 ;; The only way the current line begins quoted is when there
288 ;; is a previous line and it's ending was quoted.
289
290 (setf (lisp-info-begins-quoted line-info)
291 (and prev-line-info
292 (let ((prev (lisp-info-ending-quoted prev-line-info)))
293 (and (not (eq prev :char-quote)) prev))))
294
295 (assert (eq (hi::mark-buffer mark) (current-buffer)))
296
297 (when (lisp-info-begins-quoted line-info)
298 (deal-with-quote (lisp-info-begins-quoted line-info) mark line-info))
299
300 (unless (lisp-info-ending-quoted line-info)
301 (loop
302
303 (unless (find-lisp-char mark)
304 (error "Expected at least a newline!"))
305 (case (character-attribute :lisp-syntax (next-character mark))
306
307 (:open-paren
308 (setq net-open-parens (1+ net-open-parens))
309 (mark-after mark))
310
311 (:close-paren
312 (if (zerop net-open-parens)
313 (setq net-close-parens (1+ net-close-parens))
314 (setq net-open-parens (1- net-open-parens)))
315 (mark-after mark))
316
317 (:newline
318 (setf (lisp-info-ending-quoted line-info) nil)
319 (return t))
320
321 (:comment
322 (push-range (cons (mark-charpos mark) (line-length (mark-line mark)))
323 line-info)
324 (setf (lisp-info-ending-quoted line-info) nil)
325 (return t))
326
327 (:char-quote
328 (mark-after mark)
329 (let* ((charpos (mark-charpos mark))
330 (nextpos (1+ charpos))
331 (linelen (line-length (mark-line mark))))
332 (when (< linelen nextpos)
333 (setf (lisp-info-ending-quoted line-info) :char-quote)
334 (return t))
335 (push-range (cons charpos nextpos) line-info)
336 (mark-after mark)))
337
338 (:prefix-dispatch
339 (mark-after mark)
340 (when (test-char (next-character mark) :lisp-syntax :symbol-quote)
341 (mark-after mark)
342 (unless (deal-with-quote 1 mark line-info (- (mark-charpos mark) 2))
343 (return t))))
344
345 (:symbol-quote
346 (mark-after mark)
347 (unless (deal-with-quote :symbol-quote mark line-info)
348 (return t)))
349
350 (:string-quote
351 (mark-after mark)
352 (unless (deal-with-quote :string-quote mark line-info)
353 (return t)))
354
355 (t (ERROR "character attribute of: ~s is ~s, at ~s"
356 (next-character mark)
357 (character-attribute :lisp-syntax (next-character mark))
358 mark)))))
359 (setf (lisp-info-net-open-parens line-info) net-open-parens)
360 (setf (lisp-info-net-close-parens line-info) net-close-parens)
361 (setf (lisp-info-signature-slot line-info)
362 (line-signature (mark-line mark)))))
363
364
365
366
367;;;; String/symbol quote utilities.
368
369;;; VALID-QUOTE-P
370;;;
371(defmacro valid-quote-p (quote mark forwardp)
372 "Return T if the string-quote indicated by MARK is valid."
373 `(and (eq (character-attribute :lisp-syntax (direction-char ,mark ,forwardp)) ,quote)
374 (not (char-quoted-at-mark-p ,mark ,forwardp))))
375
376(defun char-quoted-at-mark-p (mark forwardp)
377 (unless forwardp
378 (unless (mark-before mark)
379 (return-from char-quoted-at-mark-p nil)))
380 (loop for count upfrom 0
381 do (unless (test-char (previous-character mark) :lisp-syntax :char-quote)
382 (character-offset mark count) ;; go back to where started
383 (unless forwardp
384 (mark-after mark))
385 (return (oddp count)))
386 do (mark-before mark)))
387
388;;;
389;;; FIND-VALID-QUOTE
390
391(defmacro find-valid-quote (quote mark &key forwardp (cease-at-eol nil))
392 "Expand to a form that will leave MARK before a valid string-quote character,
393 in either a forward or backward direction, according to FORWARDP. If
394 CEASE-AT-EOL is T then it will return nil if encountering the EOL before a
395 valid string-quote."
396 (let ((e-mark (gensym))
397 (pred (gensym)))
398 `(with-mark ((,e-mark ,mark))
399 (let ((,pred ,(if cease-at-eol
400 `#'(lambda (x) (or (eq x :newline) (eq x ,quote)))
401 `#'(lambda (x) (eq x ,quote)))))
402
403 (loop
404 (unless (,(if forwardp 'find-attribute 'reverse-find-attribute)
405 ,e-mark :lisp-syntax ,pred)
406 (return nil))
407
408 ,@(if cease-at-eol
409 `((when (test-char (direction-char ,e-mark ,forwardp) :lisp-syntax
410 :newline)
411 (return nil))))
412
413 (when (valid-quote-p ,quote ,e-mark ,forwardp)
414 (move-mark ,mark ,e-mark)
415 (return t))
416
417 (neighbor-mark ,e-mark ,forwardp))))))
418
419
420;;; DEAL-WITH-QUOTE
421;;;
422;;; Called when a quoted area is begun (i.e. parse hits a #\" or #\|). It checks for a
423;;; matching quote on the line that MARK points to, and puts the appropriate
424;;; area in the RANGES-TO-IGNORE slot and leaves MARK pointing after this area.
425;;; The "appropriate area" is from MARK to the end of the line or the matching
426;;; string-quote, whichever comes first.
427;;;
428
429(defun deal-with-quote (quote mark info-struct &optional (start (mark-charpos mark)))
430 "Alter the current line's info struct as necessary as due to encountering a
431 string or symbol quote character."
432 (if (fixnump quote) ;; nesting multi-line comments
433 (loop
434 (unless (and (scan-char mark :lisp-syntax (or :newline :symbol-quote))
435 (test-char (next-character mark) :lisp-syntax :symbol-quote))
436 (line-end mark)
437 (push-range (cons start (mark-charpos mark)) info-struct)
438 (setf (lisp-info-ending-quoted info-struct) quote)
439 (return nil))
440 (if (prog1 (test-char (previous-character mark) :lisp-syntax :prefix-dispatch) (mark-after mark))
441 (incf quote)
442 (when (test-char (next-character mark) :lisp-syntax :prefix-dispatch)
443 (mark-after mark)
444 (decf quote)
445 (when (<= quote 0)
446 (push-range (cons start (mark-charpos mark)) info-struct)
447 (setf (lisp-info-ending-quoted info-struct) nil)
448 (return mark)))))
449 (cond ((find-valid-quote quote mark :forwardp t :cease-at-eol t)
450 ;; If matching quote is on this line then mark the area between the
451 ;; first quote (MARK) and the matching quote as invalid by pushing
452 ;; its begining and ending into the IGNORE-RANGE.
453 (push-range (cons start (mark-charpos mark)) info-struct)
454 (mark-after mark))
455 ;; If the EOL has been hit before the matching quote then mark the
456 ;; area from MARK to the EOL as invalid.
457 (t
458 (line-end mark)
459 (push-range (cons start (mark-charpos mark)) info-struct)
460 ;; The Ending is marked as still being quoted.
461 (setf (lisp-info-ending-quoted info-struct) quote)
462 nil))))
463
464;;;; Character validity checking:
465
466;;; Find-Ignore-Region -- Internal
467;;;
468;;; If the character in the specified direction from Mark is in an ignore
469;;; region, then return the region and the line that the region is in as
470;;; values. If there is no ignore region, then return NIL and the Mark-Line.
471;;; If the line is not parsed, or there is no character (because of being at
472;;; the buffer beginning or end), then return both values NIL.
473;;;
474(defun find-ignore-region (mark forwardp)
475 (flet ((scan (line pos)
476 (declare (fixnum pos))
477 (let ((info (getf (line-plist line) 'lisp-info)))
478 (if info
479 (dolist (range (lisp-info-ranges-to-ignore info)
480 (values nil line))
481 (let ((start (car range))
482 (end (cdr range)))
483 (declare (fixnum start end))
484 (when (and (>= pos start) (< pos end))
485 (return (values range line)))))
486 (values nil nil)))))
487 (let ((pos (mark-charpos mark))
488 (line (mark-line mark)))
489 (declare (fixnum pos))
490 (cond (forwardp (scan line pos))
491 ((> pos 0) (scan line (1- pos)))
492 (t
493 (let ((prev (line-previous line)))
494 (if prev
495 (scan prev (line-length prev))
496 (values nil nil))))))))
497
498
499;;; Valid-Spot -- Public
500;;;
501(defun valid-spot (mark forwardp)
502 "Return true if the character pointed to by Mark is not in a quoted context,
503 false otherwise. If Forwardp is true, we use the next character, otherwise
504 we use the previous."
505 (if (and (not forwardp)
506 (null (previous-character mark)))
507 t ;beginning of buffer always a valid spot
508 (multiple-value-bind (region line)
509 (find-ignore-region mark forwardp)
510 (and line (not region)))))
511
512;;; Scan-Direction-Valid -- Internal
513;;;
514;;; Like scan-direction, but only stop on valid characters.
515;;;
516(defmacro scan-direction-valid (mark forwardp &rest forms)
517 (let ((n-mark (gensym))
518 (n-line (gensym))
519 (n-region (gensym))
520 (n-won (gensym)))
521 `(let ((,n-mark ,mark) (,n-won nil))
522 (loop
523 (multiple-value-bind (,n-region ,n-line)
524 (find-ignore-region ,n-mark ,forwardp)
525 (unless ,n-line (return nil))
526 (if ,n-region
527 (move-to-position ,n-mark
528 ,(if forwardp
529 `(cdr ,n-region)
530 `(car ,n-region))
531 ,n-line)
532 (when ,n-won (return t)))
533 ;;
534 ;; Peculiar condition when a quoting character terminates a line.
535 ;; The ignore region is off the end of the line causing %FORM-OFFSET
536 ;; to infinitely loop.
537 (when (> (mark-charpos ,n-mark) (line-length ,n-line))
538 #+gz (break "This shouldn't happen any more")
539 (line-offset ,n-mark 1 0))
540 (unless (scan-direction ,n-mark ,forwardp ,@forms)
541 (return nil))
542 (setq ,n-won t))))))
543
544
545
546;;;; List offseting.
547
548;;; %LIST-OFFSET allows for BACKWARD-LIST and FORWARD-LIST to be built
549;;; with the same existing structure, with the altering of one variable.
550;;; This one variable being FORWARDP.
551;;;
552(defmacro %list-offset (actual-mark forwardp &key (extra-parens 0) )
553 "Expand to code that will go forward one list either backward or forward,
554 according to the FORWARDP flag."
555 (let ((mark (gensym)))
556 `(with-mark ((,mark ,actual-mark))
557 (if (valid-spot ,mark ,forwardp)
558 (let ((paren-count ,extra-parens))
559 (declare (fixnum paren-count))
560 (loop
561 (unless (scan-direction-valid ,mark ,forwardp :lisp-syntax
562 (or :close-paren :open-paren :newline))
563 (return nil))
564 (let ((ch (direction-char ,mark ,forwardp)))
565 (case (character-attribute :lisp-syntax ch)
566 (:close-paren
567 (decf paren-count)
568 ,(when forwardp
569 ;; When going forward, an unmatching close-paren means the
570 ;; end of list.
571 `(when (<= paren-count 0)
572 (neighbor-mark ,mark ,forwardp)
573 (move-mark ,actual-mark ,mark)
574 (return t))))
575 (:open-paren
576 (incf paren-count)
577 ,(unless forwardp ; Same as above only end of list
578 `(when (>= paren-count 0) ; is opening parens.
579 (neighbor-mark ,mark ,forwardp)
580 (move-mark ,actual-mark ,mark)
581 (return t))))
582
583 (:newline
584 ;; When a #\Newline is hit, then the matching paren must lie
585 ;; on some other line so drop down into the multiple line
586 ;; balancing function: QUEST-FOR-BALANCING-PAREN If no paren
587 ;; seen yet, keep going.
588 (cond ((zerop paren-count))
589 ((quest-for-balancing-paren ,mark paren-count ,forwardp)
590 (move-mark ,actual-mark ,mark)
591 (return t))
592 (t
593 (return nil))))))
594 (neighbor-mark ,mark ,forwardp)))
595 ;; We're inside a comment or a string. Try anyway.
596 (when ,(if forwardp
597 `(%forward-list-at-mark ,mark ,extra-parens t)
598 `(%backward-list-at-mark ,mark ,extra-parens t))
599 (move-mark ,actual-mark ,mark))))))
600
601;;;
602;;; QUEST-FOR-BALANCING-PAREN
603
604(defmacro quest-for-balancing-paren (mark paren-count forwardp)
605 "Expand to a form that finds the the balancing paren for however many opens or
606 closes are registered by Paren-Count."
607 `(let* ((line (mark-line ,mark)))
608 (loop
609 (setq line (neighbor-line line ,forwardp))
610 (unless line (return nil))
611 (let ((line-info (getf (line-plist line) 'lisp-info))
612 (unbal-paren ,paren-count))
613 (unless line-info (return nil))
614
615 ,(if forwardp
616 `(decf ,paren-count (lisp-info-net-close-parens line-info))
617 `(incf ,paren-count (lisp-info-net-open-parens line-info)))
618
619 (when ,(if forwardp
620 `(<= ,paren-count 0)
621 `(>= ,paren-count 0))
622 ,(if forwardp
623 `(line-start ,mark line)
624 `(line-end ,mark line))
625 (return (goto-correct-paren-char ,mark unbal-paren ,forwardp)))
626
627 ,(if forwardp
628 `(incf ,paren-count (lisp-info-net-open-parens line-info))
629 `(decf ,paren-count (lisp-info-net-close-parens line-info)))))))
630
631
632;;;
633;;; GOTO-CORRECT-PAREN-CHAR
634
635(defmacro goto-correct-paren-char (mark paren-count forwardp)
636 "Expand to a form that will leave MARK on the correct balancing paren matching
637 however many are indicated by COUNT."
638 `(with-mark ((m ,mark))
639 (let ((count ,paren-count))
640 (loop
641 (scan-direction m ,forwardp :lisp-syntax
642 (or :close-paren :open-paren :newline))
643 (when (valid-spot m ,forwardp)
644 (ecase (character-attribute :lisp-syntax (direction-char m ,forwardp))
645 (:close-paren
646 (decf count)
647 ,(when forwardp
648 `(when (zerop count)
649 (neighbor-mark m ,forwardp)
650 (move-mark ,mark m)
651 (return t))))
652
653 (:open-paren
654 (incf count)
655 ,(unless forwardp
656 `(when (zerop count)
657 (neighbor-mark m ,forwardp)
658 (move-mark ,mark m)
659 (return t))))))
660 (neighbor-mark m ,forwardp)))))
661
662
663(defun list-offset (mark offset)
664 (if (plusp offset)
665 (dotimes (i offset t)
666 (unless (%list-offset mark t) (return nil)))
667 (dotimes (i (- offset) t)
668 (unless (%list-offset mark nil) (return nil)))))
669
670(defun forward-up-list (mark)
671 "Moves mark just past the closing paren of the immediately containing list."
672 (%list-offset mark t :extra-parens 1))
673
674(defun backward-up-list (mark)
675 "Moves mark just before the opening paren of the immediately containing list."
676 (%list-offset mark nil :extra-parens -1))
677
678
679
680
681;;;; Top level form location hacks (open parens beginning lines).
682
683;;; NEIGHBOR-TOP-LEVEL is used only in TOP-LEVEL-OFFSET.
684;;;
685(eval-when (:compile-toplevel :execute)
686(defmacro neighbor-top-level (line forwardp)
687 `(loop
688 (when (test-char (line-character ,line 0) :lisp-syntax :open-paren)
689 (return t))
690 (setf ,line ,(if forwardp `(line-next ,line) `(line-previous ,line)))
691 (unless ,line (return nil))))
692) ;eval-when
693
694(defun top-level-offset (mark offset)
695 "Go forward or backward offset number of top level forms. Mark is
696 returned if offset forms exists, otherwise nil."
697 (declare (fixnum offset))
698 (let* ((line (mark-line mark))
699 (at-start (test-char (line-character line 0) :lisp-syntax :open-paren)))
700 (cond ((zerop offset) mark)
701 ((plusp offset)
702 (do ((offset (if at-start offset (1- offset))
703 (1- offset)))
704 (nil)
705 (declare (fixnum offset))
706 (unless (neighbor-top-level line t) (return nil))
707 (when (zerop offset) (return (line-start mark line)))
708 (unless (setf line (line-next line)) (return nil))))
709 (t
710 (do ((offset (if (and at-start (start-line-p mark))
711 offset
712 (1+ offset))
713 (1+ offset)))
714 (nil)
715 (declare (fixnum offset))
716 (unless (neighbor-top-level line nil) (return nil))
717 (when (zerop offset) (return (line-start mark line)))
718 (unless (setf line (line-previous line)) (return nil)))))))
719
720
721(defun mark-top-level-form (mark1 mark2)
722 "Moves mark1 and mark2 to the beginning and end of the current or next defun.
723 Mark1 one is used as a reference. The marks may be altered even if
724 unsuccessful. if successful, return mark2, else nil."
725 (let ((winp (cond ((inside-defun-p mark1)
726 (cond ((not (top-level-offset mark1 -1)) nil)
727 ((not (form-offset (move-mark mark2 mark1) 1)) nil)
728 (t mark2)))
729 ((start-defun-p mark1)
730 (form-offset (move-mark mark2 mark1) 1))
731 ((and (top-level-offset (move-mark mark2 mark1) -1)
732 (start-defun-p mark2)
733 (form-offset mark2 1)
734 (same-line-p mark1 mark2))
735 (form-offset (move-mark mark1 mark2) -1)
736 mark2)
737 ((top-level-offset mark1 1)
738 (form-offset (move-mark mark2 mark1) 1)))))
739 (when winp
740 (when (blank-after-p mark2) (line-offset mark2 1 0))
741 mark2)))
742
743(defun inside-defun-p (mark)
744 "T if the current point is (supposedly) in a top level form."
745 (with-mark ((m mark))
746 (when (top-level-offset m -1)
747 (form-offset m 1)
748 (mark> m mark))))
749
750(defun start-defun-p (mark)
751 "Returns t if mark is sitting before an :open-paren at the beginning of a
752 line."
753 (and (start-line-p mark)
754 (test-char (next-character mark) :lisp-syntax :open-paren)))
755
756;;;; Form offseting.
757
758;; Heuristic versions, for navigating inside comments, doesn't make use of line info
759
760(defun unparsed-form-offset (mark forwardp)
761 ;; TODO: if called in "invalid" spot, arrange to stay within bounds of current invalid region.
762 ;; For now, just stop at #||# boundaries, as first approximation.
763 (if forwardp
764 (forward-form mark t)
765 (backward-form mark t)))
766
767(defun forward-form (mark &optional in-comment-p)
768 ;; If in-comment-p is true, tries not to go past a |#.
769 (with-mark ((m mark))
770 (when (and (scan-char m :lisp-syntax (or :open-paren :close-paren :prefix-dispatch
771 :symbol-quote :string-quote :char-quote
772 :comment :constituent))
773 (%forward-form-at-mark m in-comment-p))
774 (move-mark mark m))))
775
776(defun backward-form (mark &optional in-comment-p)
777 ;; If in-comment-p is true, tries not to go past a #|.
778 (with-mark ((m mark))
779 (when (%backward-form-at-mark m in-comment-p)
780 (loop while (test-char (previous-character m) :lisp-syntax (or :prefix :prefix-dispatch)) do (mark-before m))
781 (move-mark mark m))))
782
783(defun %forward-form-at-mark (mark in-comment-p)
784 ;; Warning: moves mark even if returns nil (hence the % in name).
785 (case (character-attribute :lisp-syntax (next-character mark))
786 (:open-paren
787 (mark-after mark)
788 (%forward-list-at-mark mark 1))
789 (:close-paren
790 nil)
791 (:char-quote
792 (%forward-symbol-at-mark mark in-comment-p))
793 (:symbol-quote
794 (mark-after mark)
795 (unless (and in-comment-p (test-char (next-character mark) :lisp-syntax :prefix-dispatch))
796 (mark-before mark)
797 (%forward-symbol-at-mark mark in-comment-p)))
798 (:prefix-dispatch
799 (mark-after mark)
800 (case (character-attribute :lisp-syntax (next-character mark))
801 (:symbol-quote
802 (mark-after mark)
803 (%forward-nesting-comment-at-mark mark 1))
804 (:prefix
805 (mark-after mark)
806 (%forward-form-at-mark mark in-comment-p))
807 (t
808 (mark-before mark)
809 (%forward-symbol-at-mark mark in-comment-p))))
810 (:string-quote
811 (%forward-string-at-mark mark))
812 (:constituent
813 (%forward-symbol-at-mark mark in-comment-p))
814 (:comment
815 (%forward-comments-at-mark mark))
816 (t
817 (mark-after mark)
818 (%forward-form-at-mark mark in-comment-p))))
819
820(defun %backward-form-at-mark (mark in-comment-p)
821 ;; Warning: moves mark even if returns nil (hence the % in name).
822 (let* ((char (previous-character mark))
823 (attrib (character-attribute :lisp-syntax char)))
824 (when char
825 (mark-before mark)
826 (when (char-quoted-at-mark-p mark t)
827 (setq attrib :constituent))
828 (case attrib
829 (:open-paren
830 nil)
831 (:close-paren
832 (%backward-list-at-mark mark 1))
833 (:char-quote ;;; can only happen if starting right after an unquoted char-quote
834 (%backward-symbol-at-mark mark in-comment-p))
835 (:symbol-quote
836 (unless (and in-comment-p (test-char (previous-character mark) :lisp-syntax :prefix-dispatch))
837 (mark-after mark)
838 (%backward-symbol-at-mark mark in-comment-p)))
839 (:prefix-dispatch
840 (if (test-char (previous-character mark) :lisp-syntax :symbol-quote)
841 (progn
842 (mark-before mark)
843 (%backward-nesting-comment-at-mark mark 1))
844 (progn
845 (mark-after mark)
846 (%backward-symbol-at-mark mark in-comment-p))))
847 (:string-quote
848 (mark-after mark)
849 (%backward-string-at-mark mark))
850 (:constituent
851 (mark-after mark)
852 (%backward-symbol-at-mark mark in-comment-p))
853 (:prefix
854 (loop while (test-char (previous-character mark) :lisp-syntax :prefix) do (mark-before mark))
855 mark)
856 (:comment
857 (loop while (test-char (previous-character mark) :lisp-syntax :comment) do (mark-before mark))
858 mark)
859 ;; TODO: it would be nice to skip over ;; comments if starting outside one, i.e. if encounter a newline
860 ;; before a form starts.
861 (t (%backward-form-at-mark mark in-comment-p))))))
862
863(defun %forward-symbol-at-mark (mark in-comment-p)
864 ;; Warning: moves mark even if returns nil (hence the % in name).
865 (loop
866 (unless (scan-char mark :lisp-syntax (not (or :constituent :prefix-dispatch)))
867 (return (buffer-end mark)))
868 (case (character-attribute :lisp-syntax (next-character mark))
869 (:symbol-quote
870 (mark-after mark)
871 (when (and in-comment-p (test-char (next-character mark) :lisp-syntax :prefix-dispatch))
872 (return (mark-before mark)))
873 (unless (loop
874 (unless (scan-char mark :lisp-syntax (or :char-quote :symbol-quote))
875 (return nil))
876 (when (test-char (next-character mark) :lisp-syntax :symbol-quote)
877 (return t))
878 (character-offset mark 2))
879 (return nil))
880 (mark-after mark))
881 (:char-quote
882 (character-offset mark 2))
883 (t (return mark)))))
884
885(defun %backward-symbol-at-mark (mark in-comment-p)
886 (loop
887 (unless (rev-scan-char mark :lisp-syntax (not (or :constituent :prefix-dispatch :char-quote)))
888 (buffer-start mark)
889 (return mark))
890 (mark-before mark)
891 (if (char-quoted-at-mark-p mark t)
892 (mark-before mark)
893 (let* ((char (next-character mark)))
894 (case (character-attribute :lisp-syntax char)
895 (:symbol-quote
896 (when (and in-comment-p (test-char (previous-character mark) :lisp-syntax :prefix-dispatch))
897 (return (mark-after mark)))
898 (unless (loop
899 (unless (rev-scan-char mark :lisp-syntax :symbol-quote)
900 (return nil))
901 (mark-before mark)
902 (unless (char-quoted-at-mark-p mark t)
903 (return t))
904 (mark-before mark))
905 (return nil)))
906 (t (mark-after mark)
907 (return mark)))))))
908
909(defun %forward-nesting-comment-at-mark (mark nesting)
910 ;; Warning: moves mark even if returns nil (hence the % in name).
911 (loop
912 (unless (scan-char mark :lisp-syntax :symbol-quote)
913 (return nil))
914 (let ((prev (previous-character mark)))
915 (mark-after mark)
916 (cond ((test-char prev :lisp-syntax :prefix-dispatch)
917 (incf nesting))
918 ((test-char (next-character mark) :lisp-syntax :prefix-dispatch)
919 (mark-after mark)
920 (when (<= (decf nesting) 0)
921 (return mark)))))))
922
923(defun %backward-nesting-comment-at-mark (mark nesting)
924 ;; Warning: moves mark even if returns nil (hence the % in name).
925 (loop
926 (unless (rev-scan-char mark :lisp-syntax :symbol-quote)
927 (return nil))
928 (let ((next (next-character mark)))
929 (mark-before mark)
930 (cond ((test-char next :lisp-syntax :prefix-dispatch)
931 (incf nesting))
932 ((test-char (previous-character mark) :lisp-syntax :prefix-dispatch)
933 (mark-before mark)
934 (when (<= (decf nesting) 0)
935 (return mark)))))))
936
937
938(defun %scan-to-form (m forwardp)
939 (if forwardp
940 ;; Stop at :prefix-dispatch if it is not followed by :prefix. If it's followed by :prefix,
941 ;; assume it has the semantics of :prefix and skip it.
942 (loop while (scan-direction-valid m t :lisp-syntax
943 (or :open-paren :close-paren
944 :char-quote :string-quote :symbol-quote
945 :prefix-dispatch :constituent))
946 do (unless (and (test-char (next-character m) :lisp-syntax :prefix-dispatch)
947 (mark-after m))
948 (return t))
949 do (unless (test-char (next-character m) :lisp-syntax :prefix)
950 (mark-before m)
951 (return t)))
952 (scan-direction-valid m nil :lisp-syntax
953 (or :open-paren :close-paren
954 :char-quote :string-quote :symbol-quote
955 :prefix-dispatch :constituent))))
956
957;; %FORM-OFFSET
958
959(defmacro %form-offset (mark forwardp)
960 `(if (valid-spot ,mark ,forwardp)
961 (with-mark ((m ,mark))
962 (when (%scan-to-form m ,forwardp)
963 (ecase (character-attribute :lisp-syntax (direction-char m ,forwardp))
964 (:open-paren
965 (when ,(if forwardp `(list-offset m 1) `(mark-before m))
966 ,(unless forwardp
967 '(scan-direction m nil :lisp-syntax (not (or :prefix-dispatch :prefix))))
968 (move-mark ,mark m)
969 t))
970 (:close-paren
971 (when ,(if forwardp `(mark-after m) `(list-offset m -1))
972 ,(unless forwardp
973 '(scan-direction m nil :lisp-syntax (not (or :prefix-dispatch :prefix))))
974 (move-mark ,mark m)
975 t))
976 ((:constituent :char-quote :symbol-quote :prefix-dispatch)
977 ,(if forwardp
978 `(scan-direction-valid m t :lisp-syntax
979 (not (or :constituent :char-quote :symbol-quote :prefix-dispatch)))
980 `(scan-direction-valid m nil :lisp-syntax
981 (not (or :constituent :char-quote :symbol-quote :prefix-dispatch
982 :prefix))))
983 (move-mark ,mark m)
984 t)
985 (:string-quote
986 (neighbor-mark m ,forwardp)
987 (when (scan-direction-valid m ,forwardp :lisp-syntax
988 :string-quote)
989 (neighbor-mark m ,forwardp)
990 (move-mark ,mark m)
991 t)))))
992 ;; Inside a comment or a string. Switch to heuristic method.
993 (unparsed-form-offset ,mark ,forwardp)))
994
995(defun %forward-list-at-mark (mark nesting &optional in-comment-p)
996 ;; Warning: moves mark even if returns nil (hence the % in name).
997 (loop
998 (unless (scan-char mark :lisp-syntax (or :open-paren :close-paren :prefix-dispatch
999 :symbol-quote :string-quote :char-quote :comment))
1000 (return nil))
1001 (case (character-attribute :lisp-syntax (next-character mark))
1002 (:open-paren
1003 (mark-after mark)
1004 (incf nesting))
1005 (:close-paren
1006 (mark-after mark)
1007 (when (<= (decf nesting) 0)
1008 (return (and (eql nesting 0) mark))))
1009 (t
1010 (unless (%forward-form-at-mark mark in-comment-p)
1011 (return nil))))))
1012
1013(defun %backward-list-at-mark (mark nesting &optional in-comment-p)
1014 ;; Warning: moves mark even if returns nil (hence the % in name).
1015 (loop
1016 (unless (rev-scan-char mark :lisp-syntax (or :open-paren :close-paren :prefix-dispatch
1017 :symbol-quote :string-quote :comment))
1018 (return nil))
1019 (mark-before mark)
1020 (if (char-quoted-at-mark-p mark t)
1021 (mark-before mark)
1022 (case (character-attribute :lisp-syntax (next-character mark))
1023 (:close-paren
1024 (incf nesting))
1025 (:open-paren
1026 (when (<= (decf nesting) 0)
1027 (return mark)))
1028 (t
1029 (mark-after mark)
1030 (unless (%backward-form-at-mark mark in-comment-p)
1031 (return nil)))))))
1032
1033(defun %forward-string-at-mark (mark)
1034 ;; Warning: moves mark even if returns nil (hence the % in name).
1035 (mark-after mark)
1036 (loop
1037 (unless (scan-char mark :lisp-syntax (or :char-quote :string-quote))
1038 (return nil))
1039 (unless (test-char (next-character mark) :lisp-syntax :char-quote)
1040 (return (mark-after mark)))
1041 (character-offset mark 2)))
1042
1043
1044(defun %backward-string-at-mark (mark)
1045 ;; Warning: moves mark even if returns nil (hence the % in name).
1046 (mark-before mark)
1047 (loop
1048 (unless (rev-scan-char mark :lisp-syntax :string-quote)
1049 (return nil))
1050 (mark-before mark)
1051 (unless (char-quoted-at-mark-p mark t)
1052 (return mark))
1053 (mark-before mark)))
1054
1055(defun %forward-comments-at-mark (mark)
1056 ;; Warning: moves mark even if returns nil (hence the % in name).
1057 (with-mark ((m mark))
1058 (loop
1059 (line-end m)
1060 (mark-after m)
1061 (move-mark mark m)
1062 (unless (and (scan-char m :lisp-syntax (not :space))
1063 (test-char (next-character m) :lisp-syntax :comment))
1064 (return mark)))))
1065
1066(defun form-offset (mark offset)
1067 "Move mark offset number of forms, after if positive, before if negative.
1068 Mark is always moved. If there weren't enough forms, returns nil instead of
1069 mark."
1070 (if (plusp offset)
1071 (dotimes (i offset t)
1072 (unless (%form-offset mark t) (return nil)))
1073 (dotimes (i (- offset) t)
1074 (unless (%form-offset mark nil) (return nil)))))
1075
1076;; Return region for the "current form" at mark.
1077;; TODO: See also mark-nearest-form, should merge them
1078(defun form-region-at-mark (mark)
1079 (with-mark ((bwd-start mark)
1080 (bwd-end mark)
1081 (fwd-start mark)
1082 (fwd-end mark))
1083 (let* ((fwd (and (or (and (char-quoted-at-mark-p mark t) ;; back-up so get whole character
1084 (mark-before fwd-end))
1085 (test-char (next-character mark) :lisp-syntax
1086 (or :open-paren :string-quote
1087 :char-quote :symbol-quote :constituent :prefix-dispatch
1088 :prefix)))
1089 (form-offset fwd-end 1)
1090 (form-offset (move-mark fwd-start fwd-end) -1)
1091 (mark<= fwd-start mark)))
1092 (bwd (and (or (char-quoted-at-mark-p mark nil)
1093 (test-char (previous-character mark) :lisp-syntax
1094 (or :close-paren :string-quote
1095 :char-quote :symbol-quote :constituent :prefix-dispatch)))
1096 ;; Special case - if at an open paren, always select forward because that's
1097 ;; the matching paren that's highlighted.
1098 (not (and fwd (test-char (next-character mark) :lisp-syntax :open-paren)))
1099 ;; Also prefer string over anything but close paren.
1100 (not (and fwd (test-char (next-character mark) :lisp-syntax :string-quote)
1101 (not (test-char (previous-character mark) :lisp-syntax :close-paren))))
1102 (form-offset bwd-start -1)
1103 (form-offset (move-mark bwd-end bwd-start) 1)
1104 (mark<= mark bwd-end))))
1105 (if bwd
1106 (when (or (not fwd) ;; back is only option
1107 (and (mark= bwd-start fwd-start) (mark= bwd-end fwd-end)) ;; or they're the same
1108 (and (mark= bwd-start fwd-end) ;; or had to skip prefix chars to get to forward
1109 (test-char (next-character fwd-start) :lisp-syntax (or :prefix :prefix-dispatch))))
1110 (region bwd-start bwd-end))
1111 (if fwd
1112 (region fwd-start fwd-end))))))
1113
1114;; Return region for the current word at mark, or nil if there isn't one.
1115(defun word-region-at-mark (mark)
1116 (with-mark ((fwd mark)
1117 (bwd mark))
1118 (or (find-attribute fwd :word-delimiter)
1119 (buffer-end fwd))
1120 (or (reverse-find-attribute bwd :word-delimiter)
1121 (buffer-start bwd))
1122 (unless (mark= bwd fwd)
1123 ;; Special-case for keywords (and gensyms)
1124 (when (eq (previous-character bwd) #\:)
1125 (mark-before bwd)
1126 (when (test-char (previous-character bwd) :lisp-syntax :constituent)
1127 (mark-after bwd))) ;; oops, never mind
1128 ;; Special-case for stuff like #_foo.
1129 (when (test-char (previous-character bwd) :lisp-syntax :prefix-dispatch)
1130 ;; let :prefix-dispatch take on the attribute of the following char, which is a word constituent
1131 (mark-before bwd))
1132 (region bwd fwd))))
1133
1134;;;; Table of special forms with special indenting requirements.
1135
1136(defhvar "Indent Defanything"
1137 "This is the number of special arguments implicitly assumed to be supplied
1138 in calls to functions whose names begin with \"DEF\". If set to NIL, this
1139 feature is disabled."
1140 :value 2)
1141
1142(defhvar "Indent With-anything"
1143 "This is the number of special arguments implicitly assumed to be supplied
1144 in calls to functions whose names begin with \"WITH-\". If set to NIL, this
1145 feature is disabled."
1146 :value 1)
1147
1148(defvar *special-forms* (make-hash-table :test #'equal))
1149
1150(defun defindent (fname args)
1151 "Define Fname to have Args special arguments. If args is null then remove
1152 any special arguments information."
1153 (check-type fname string)
1154 (let ((fname (string-upcase fname)))
1155 (cond ((null args) (remhash fname *special-forms*))
1156 (t
1157 (check-type args integer)
1158 (setf (gethash fname *special-forms*) args)))))
1159
1160
1161;;; Hemlock forms.
1162;;;
1163(defindent "defhvar" 1)
1164(defindent "hlet" 1)
1165(defindent "defcommand" 2)
1166(defindent "defattribute" 1)
1167(defindent "command-case" 1)
1168(defindent "do-strings" 1)
1169(defindent "save-for-undo" 1)
1170(defindent "do-alpha-chars" 1)
1171(defindent "do-headers-buffers" 1)
1172(defindent "do-headers-lines" 1)
1173(defindent "frob" 1) ;cover silly FLET and MACROLET names for Rob and Bill.
1174(defindent "modifying-buffer" 1)
1175
1176;;; Common Lisp forms.
1177;;;
1178(defindent "block" 1)
1179(defindent "return-from" 1)
1180(defindent "case" 1)
1181(defindent "catch" 1)
1182(defindent "ccase" 1)
1183(defindent "compiler-let" 1)
1184(defindent "ctypecase" 1)
1185(defindent "defconstant" 1)
1186(defindent "define-compiler-macro" 2)
1187(defindent "define-setf-method" 2)
1188(defindent "destructuring-bind" 2)
1189(defindent "defmacro" 2)
1190(defindent "defpackage" 1)
1191(defindent "defparameter" 1)
1192(defindent "defstruct" 1)
1193(defindent "deftype" 2)
1194(defindent "defun" 2)
1195(defindent "defvar" 1)
1196(defindent "do" 2)
1197(defindent "do*" 2)
1198(defindent "do-all-symbols" 1)
1199(defindent "do-external-symbols" 1)
1200(defindent "do-symbols" 1)
1201(defindent "dolist" 1)
1202(defindent "dotimes" 1)
1203(defindent "ecase" 1)
1204(defindent "etypecase" 1)
1205(defindent "eval-when" 1)
1206(defindent "flet" 1)
1207(defindent "if" 1)
1208(defindent "labels" 1)
1209(defindent "lambda" 1)
1210(defindent "let" 1)
1211(defindent "let*" 1)
1212(defindent "locally" 0)
1213(defindent "loop" 0)
1214(defindent "macrolet" 1)
1215(defindent "multiple-value-bind" 2)
1216(defindent "multiple-value-call" 1)
1217(defindent "multiple-value-prog1" 1)
1218(defindent "multiple-value-setq" 1)
1219(defindent "prog" 1)
1220(defindent "prog*" 1)
1221(defindent "prog1" 1)
1222(defindent "progv" 2)
1223(defindent "progn" 0)
1224(defindent "typecase" 1)
1225(defindent "unless" 1)
1226(defindent "unwind-protect" 1)
1227(defindent "when" 1)
1228
1229;; CCL extensions
1230(defindent "iterate" 2)
1231(defindent "rlet" 1)
1232(defindent "rletz" 1)
1233
1234;;; Error/condition system forms.
1235;;;
1236(defindent "define-condition" 2)
1237(defindent "handler-bind" 1)
1238(defindent "handler-case" 1)
1239(defindent "restart-bind" 1)
1240(defindent "restart-case" 1)
1241;;; These are for RESTART-CASE branch formatting.
1242(defindent "store-value" 1)
1243(defindent "use-value" 1)
1244(defindent "muffle-warning" 1)
1245(defindent "abort" 1)
1246(defindent "continue" 1)
1247;;; This is for DEFGENERIC method formatting
1248(defindent "method" 1)
1249
1250;;; Debug-internals forms.
1251;;;
1252(defindent "do-debug-function-blocks" 1)
1253(defindent "di:do-debug-function-blocks" 1)
1254(defindent "do-debug-function-variables" 1)
1255(defindent "di:do-debug-function-variables" 1)
1256(defindent "do-debug-block-locations" 1)
1257(defindent "di:do-debug-block-locations" 1)
1258;;;
1259;;; Debug-internals conditions
1260;;; (define these to make uses of HANDLER-CASE indent branches correctly.)
1261;;;
1262(defindent "debug-condition" 1)
1263(defindent "di:debug-condition" 1)
1264(defindent "no-debug-info" 1)
1265(defindent "di:no-debug-info" 1)
1266(defindent "no-debug-function-returns" 1)
1267(defindent "di:no-debug-function-returns" 1)
1268(defindent "no-debug-blocks" 1)
1269(defindent "di:no-debug-blocks" 1)
1270(defindent "lambda-list-unavailable" 1)
1271(defindent "di:lambda-list-unavailable" 1)
1272(defindent "no-debug-variables" 1)
1273(defindent "di:no-debug-variables" 1)
1274(defindent "invalid-value" 1)
1275(defindent "di:invalid-value" 1)
1276(defindent "ambiguous-variable-name" 1)
1277(defindent "di:ambiguous-variable-name" 1)
1278(defindent "debug-error" 1)
1279(defindent "di:debug-error" 1)
1280(defindent "unhandled-condition" 1)
1281(defindent "di:unhandled-condition" 1)
1282(defindent "unknown-code-location" 1)
1283(defindent "di:unknown-code-location" 1)
1284(defindent "unknown-debug-variable" 1)
1285(defindent "di:unknown-debug-variable" 1)
1286(defindent "invalid-control-stack-pointer" 1)
1287(defindent "di:invalid-control-stack-pointer" 1)
1288(defindent "frame-function-mismatch" 1)
1289(defindent "di:frame-function-mismatch" 1)
1290
1291
1292;;; CLOS forms.
1293;;;
1294(defindent "with-accessors" 2)
1295(defindent "defclass" 2)
1296(defindent "print-unreadable-object" 1)
1297(defindent "defmethod" 2)
1298(defindent "make-instance" 1)
1299
1300;;; System forms.
1301;;;
1302
1303;;; Multiprocessing forms.
1304(defindent "process-wait" 1)
1305
1306
1307
1308
1309;;;; Indentation.
1310
1311;;; LISP-INDENTATION -- Internal Interface.
1312
1313(defun strip-package-prefix (string)
1314 (let* ((p (position #\: string :from-end t)))
1315 (if p
1316 (subseq string (1+ p))
1317 string)))
1318;;;
1319(defun lisp-indentation (mark)
1320 "Compute number of spaces which mark should be indented according to
1321 local context and lisp grinding conventions. This assumes mark is at the
1322 beginning of the line to be indented."
1323 (with-mark ((m mark)
1324 (temp mark))
1325 ;; See if we are in a quoted context.
1326 (unless (valid-spot m nil)
1327 (return-from lisp-indentation (lisp-generic-indentation m)))
1328 ;; Look for the paren that opens the containing form.
1329 (unless (backward-up-list m)
1330 (return-from lisp-indentation 0))
1331 ;; Move after the paren, save the start, and find the form name.
1332 (mark-after m)
1333 (with-mark ((start m))
1334 (unless (and (scan-char m :lisp-syntax
1335 (not (or :space :prefix :prefix-dispatch :char-quote)))
1336 (test-char (next-character m) :lisp-syntax :constituent))
1337 (return-from lisp-indentation (mark-column start)))
1338 (with-mark ((fstart m))
1339 (scan-char m :lisp-syntax (not :constituent))
1340 (let* ((fname (nstring-upcase
1341 (strip-package-prefix (region-to-string (region fstart m)))))
1342 (special-args (or (gethash fname *special-forms*)
1343 (and (> (length fname) 2)
1344 (string= fname "DEF" :end1 3)
1345 (value indent-defanything))
1346 (and (> (length fname) 4)
1347 (string= fname "WITH-" :end1 5)
1348 (value indent-with-anything)))))
1349 (declare (simple-string fname))
1350 ;; Now that we have the form name, did it have special syntax?
1351 (cond (special-args
1352 (with-mark ((spec m))
1353 (cond ((and (form-offset spec special-args)
1354 (mark<= spec mark))
1355 (1+ (mark-column start)))
1356 ((skip-valid-space m)
1357 (mark-column m))
1358 (t
1359 (+ (mark-column start) 3)))))
1360 ;; See if the user seems to have altered the editor's
1361 ;; indentation, and if so, try to adhere to it. This usually
1362 ;; happens when you type in a quoted list constant that line
1363 ;; wraps. You want all the items on successive lines to fall
1364 ;; under the first character after the opening paren, not as if
1365 ;; you are calling a function.
1366 ((and (form-offset temp -1)
1367 (or (blank-before-p temp) (not (same-line-p temp fstart)))
1368 (not (same-line-p temp mark)))
1369 (unless (blank-before-p temp)
1370 (line-start temp)
1371 (find-attribute temp :space #'zerop))
1372 (mark-column temp))
1373 ;; Appears to be a normal form. Is the first arg on the same
1374 ;; line as the form name?
1375 ((skip-valid-space m)
1376 (or (lisp-indentation-check-for-local-def
1377 mark temp fstart start t)
1378 (mark-column m)))
1379 ;; Okay, fall under the first character after the opening paren.
1380 (t
1381 (or (lisp-indentation-check-for-local-def
1382 mark temp fstart start nil)
1383 (mark-column start)))))))))
1384
1385(defhvar "Lisp Indentation Local Definers"
1386 "Forms with syntax like LABELS, MACROLET, etc."
1387 :value '("LABELS" "MACROLET" "FLET"))
1388
1389;;; LISP-INDENTATION-CHECK-FOR-LOCAL-DEF -- Internal.
1390;;;
1391;;; This is a temporary hack to see how it performs. When we are indenting
1392;;; what appears to be a function call, let's look for FLET or MACROLET to see
1393;;; if we really are indenting a local definition. If we are, return the
1394;;; indentation for a DEFUN; otherwise, nil
1395;;;
1396;;; Mark is the argument to LISP-INDENTATION. Start is just inside the paren
1397;;; of what looks like a function call. If we are in an FLET, arg-list
1398;;; indicates whether the local function's arg-list has been entered, that is,
1399;;; whether we need to normally indent for a DEFUN body or indent specially for
1400;;; the arg-list.
1401;;;
1402(defun lisp-indentation-check-for-local-def (mark temp1 temp2 start arg-list)
1403 ;; We know this succeeds from LISP-INDENTATION.
1404 (backward-up-list (move-mark temp1 mark)) ;Paren for local definition.
1405 (cond ((and (backward-up-list temp1) ;Paren opening the list of defs
1406 (form-offset (move-mark temp2 temp1) -1)
1407 (mark-before temp2)
1408 (backward-up-list temp1) ;Paren for FLET or MACROLET.
1409 (mark= temp1 temp2)) ;Must be in first arg form.
1410 ;; See if the containing form is named FLET or MACROLET.
1411 (mark-after temp1)
1412 (unless (and (scan-char temp1 :lisp-syntax
1413 (not (or :space :prefix :prefix-dispatch :char-quote)))
1414 (test-char (next-character temp1) :lisp-syntax
1415 :constituent))
1416 (return-from lisp-indentation-check-for-local-def nil))
1417 (move-mark temp2 temp1)
1418 (scan-char temp2 :lisp-syntax (not :constituent))
1419 (let ((fname (nstring-upcase (region-to-string (region temp1 temp2)))))
1420 (cond ((not (member fname (value lisp-indentation-local-definers)
1421 :test #'string=))
1422 nil)
1423 (arg-list
1424 (1+ (mark-column start)))
1425 (t
1426 (+ (mark-column start) 3)))))))
1427
1428;;; LISP-GENERIC-INDENTATION -- Internal.
1429;;;
1430;;; LISP-INDENTATION calls this when mark is in a invalid spot, or quoted
1431;;; context. If we are inside a string, we return the column one greater
1432;;; than the opening double quote. Otherwise, we just use the indentation
1433;;; of the first preceding non-blank line.
1434;;;
1435(defun lisp-generic-indentation (mark)
1436 (with-mark ((m mark))
1437 (form-offset m -1)
1438 (cond ((eq (character-attribute :lisp-syntax (next-character m))
1439 :string-quote)
1440 (1+ (mark-column m)))
1441 (t
1442 (let* ((line (mark-line mark))
1443 (prev (do ((line (line-previous line) (line-previous line)))
1444 ((not (and line (blank-line-p line))) line))))
1445 (cond (prev
1446 (line-start mark prev)
1447 (find-attribute mark :space #'zerop)
1448 (mark-column mark))
1449 (t 0)))))))
1450
1451;;; Skip-Valid-Space -- Internal
1452;;;
1453;;; Skip over any space on the line Mark is on, stopping at the first valid
1454;;; non-space character. If there is none on the line, return nil.
1455;;;
1456(defun skip-valid-space (mark)
1457 (loop
1458 (scan-char mark :lisp-syntax (not :space))
1459 (let ((val (character-attribute :lisp-syntax
1460 (next-character mark))))
1461 (cond ((eq val :newline) (return nil))
1462 ((valid-spot mark t) (return mark))))
1463 (mark-after mark)))
1464
1465;; (declaim (optimize (speed 0))); byte compile again
1466
1467
1468
1469;;;; Indentation commands and hook functions.
1470
1471(defcommand "Defindent" (p)
1472 "Define the Lisp indentation for the current function.
1473 The indentation is a non-negative integer which is the number
1474 of special arguments for the form. Examples: 2 for Do, 1 for Dolist.
1475 If a prefix argument is supplied, then delete the indentation information."
1476 "Do a defindent, man!"
1477 (with-mark ((m (current-point)))
1478 (pre-command-parse-check m)
1479 (unless (backward-up-list m) (editor-error))
1480 (mark-after m)
1481 (with-mark ((n m))
1482 (scan-char n :lisp-syntax (not :constituent))
1483 (let ((s (region-to-string (region m n))))
1484 (declare (simple-string s))
1485 (when (zerop (length s)) (editor-error))
1486 (if p
1487 (defindent s nil)
1488 (let ((i (prompt-for-integer
1489 :prompt (format nil "Indentation for ~A: " s)
1490 :help "Number of special arguments.")))
1491 (when (minusp i)
1492 (editor-error "Indentation must be non-negative."))
1493 (defindent s i))))))
1494 (indent-command nil))
1495
1496(defcommand "Indent Form" (p)
1497 "Indent Lisp code in the next form, unless point is to the right of
1498 a closing parenthesis, in which case the previous form will be
1499 indented."
1500 "Indent Lisp code in the next form."
1501 (declare (ignore p))
1502 (let ((point (current-point))
1503 (offset 1))
1504 (pre-command-parse-check point)
1505 (when (eql (previous-character point) #\))
1506 (setq offset -1))
1507 (with-mark ((m point))
1508 (unless (form-offset m offset) (editor-error))
1509 (when (minusp offset)
1510 (rotatef point m))
1511 (lisp-indent-region (region point m) "Indent Form"))))
1512
1513;;; LISP-INDENT-REGION -- Internal.
1514;;;
1515;;; This indents a region of Lisp code without doing excessive redundant
1516;;; computation. We parse the entire region once, then scan through doing
1517;;; indentation on each line. We forcibly reparse each line that we indent so
1518;;; that the list operations done to determine indentation of subsequent lines
1519;;; will work. This is done undoably with save1, save2, buf-region, and
1520;;; undo-region.
1521;;;
1522(defun lisp-indent-region (region &optional (undo-text "Lisp region indenting"))
1523 (let* ((start (region-start region))
1524 (end (region-end region))
1525 (buffer (hi::line-%buffer (mark-line start))))
1526 (with-mark ((m1 start)
1527 (m2 end))
1528 (funcall (value parse-start-function) m1)
1529 (funcall (value parse-end-function) m2)
1530 (parse-over-block (mark-line m1) (mark-line m2)))
1531 (hi::check-buffer-modification buffer start)
1532 (hi::check-buffer-modification buffer end)
1533 (let* ((first-line (mark-line start))
1534 (last-line (mark-line end))
1535 (prev (line-previous first-line))
1536 (prev-line-info
1537 (and prev (getf (line-plist prev) 'lisp-info)))
1538 (save1 (line-start (copy-mark start :right-inserting)))
1539 (save2 (line-end (copy-mark end :left-inserting)))
1540 (buf-region (region save1 save2))
1541 (undo-region (copy-region buf-region)))
1542 (with-mark ((bol start :left-inserting))
1543 (do ((line first-line (line-next line)))
1544 (nil)
1545 (line-start bol line)
1546 (ensure-lisp-indentation bol)
1547 (let ((line-info (getf (line-plist line) 'lisp-info)))
1548 (parse-lisp-line-info bol line-info prev-line-info)
1549 (setq prev-line-info line-info))
1550 (when (eq line last-line) (return nil))))
1551 (make-region-undo :twiddle undo-text buf-region undo-region))))
1552
1553;;; INDENT-FOR-LISP -- Internal.
1554;;;
1555;;; This is the value of "Indent Function" for "Lisp" mode.
1556;;;
1557(defun indent-for-lisp (mark)
1558 (line-start mark)
1559 (pre-command-parse-check mark)
1560 (ensure-lisp-indentation mark))
1561
1562(defun count-leading-whitespace (mark)
1563 (with-mark ((m mark))
1564 (line-start m)
1565 (do* ((p 0)
1566 (q 0 (1+ q))
1567 (tab-width (value spaces-per-tab)))
1568 ()
1569 (case (next-character m)
1570 (#\space (incf p))
1571 (#\tab (setq p (* tab-width (ceiling (1+ p) tab-width))))
1572 (t (return (values p q))))
1573 (character-offset m 1))))
1574
1575;;; Don't do anything if M's line is already correctly indented.
1576(defun ensure-lisp-indentation (m)
1577 (let* ((col (lisp-indentation m)))
1578 (multiple-value-bind (curcol curpos) (count-leading-whitespace m)
1579 (cond ((= curcol col) (setf (mark-charpos m) curpos))
1580 (t
1581 (delete-horizontal-space m)
1582 (indent-to-column m col))))))
1583
1584
1585
1586
1587
1588;;;; Most "Lisp" mode commands.
1589
1590(defcommand "Beginning of Defun" (p)
1591 "Move the point to the beginning of a top-level form, collapsing the selection.
1592 with an argument, skips the previous p top-level forms."
1593 "Move the point to the beginning of a top-level form, collapsing the selection."
1594 (let ((point (current-point-collapsing-selection))
1595 (count (or p 1)))
1596 (pre-command-parse-check point)
1597 (if (minusp count)
1598 (end-of-defun-command (- count))
1599 (unless (top-level-offset point (- count))
1600 (editor-error)))))
1601
1602(defcommand "Select to Beginning of Defun" (p)
1603 "Move the point to the beginning of a top-level form, extending the selection.
1604 with an argument, skips the previous p top-level forms."
1605 "Move the point to the beginning of a top-level form, extending the selection."
1606 (let ((point (current-point-for-selection-start))
1607 (count (or p 1)))
1608 (pre-command-parse-check point)
1609 (if (minusp count)
1610 (end-of-defun-command (- count))
1611 (unless (top-level-offset point (- count))
1612 (editor-error)))))
1613
1614;;; "End of Defun", with a positive p (the normal case), does something weird.
1615;;; Get a mark at the beginning of the defun, and then offset it forward one
1616;;; less top level form than we want. This sets us up to use FORM-OFFSET which
1617;;; allows us to leave the point immediately after the defun. If we used
1618;;; TOP-LEVEL-OFFSET one less than p on the mark at the end of the current
1619;;; defun, point would be left at the beginning of the p+1'st form instead of
1620;;; at the end of the p'th form.
1621;;;
1622(defcommand "End of Defun" (p)
1623 "Move the point to the end of a top-level form, collapsing the selection.
1624 With an argument, skips the next p top-level forms."
1625 "Move the point to the end of a top-level form, collapsing the selection."
1626 (let ((point (current-point-collapsing-selection))
1627 (count (or p 1)))
1628 (pre-command-parse-check point)
1629 (if (minusp count)
1630 (beginning-of-defun-command (- count))
1631 (with-mark ((m point)
1632 (dummy point))
1633 (cond ((not (mark-top-level-form m dummy))
1634 (editor-error "No current or next top level form."))
1635 (t
1636 (unless (top-level-offset m (1- count))
1637 (editor-error "Not enough top level forms."))
1638 ;; We might be one unparsed for away.
1639 (pre-command-parse-check m)
1640 (unless (form-offset m 1)
1641 (editor-error "Not enough top level forms."))
1642 (when (blank-after-p m) (line-offset m 1 0))
1643 (move-mark point m)))))))
1644
1645(defcommand "Select to End of Defun" (p)
1646 "Move the point to the end of a top-level form, extending the selection.
1647 With an argument, skips the next p top-level forms."
1648 "Move the point to the end of a top-level form, extending the selection."
1649 (let ((point (current-point-for-selection-end))
1650 (count (or p 1)))
1651 (pre-command-parse-check point)
1652 (if (minusp count)
1653 (beginning-of-defun-command (- count))
1654 (with-mark ((m point)
1655 (dummy point))
1656 (cond ((not (mark-top-level-form m dummy))
1657 (editor-error "No current or next top level form."))
1658 (t
1659 (unless (top-level-offset m (1- count))
1660 (editor-error "Not enough top level forms."))
1661 ;; We might be one unparsed for away.
1662 (pre-command-parse-check m)
1663 (unless (form-offset m 1)
1664 (editor-error "Not enough top level forms."))
1665 (when (blank-after-p m) (line-offset m 1 0))
1666 (move-mark point m)))))))
1667
1668(defcommand "Forward List" (p)
1669 "Skip over the next Lisp list, collapsing the selection.
1670 With argument, skips the next p lists."
1671 "Skip over the next Lisp list, collapsing the selection."
1672 (or (collapse-if-selection :direction :forward)
1673 (let ((point (current-point-collapsing-selection))
1674 (count (or p 1)))
1675 (pre-command-parse-check point)
1676 (unless (list-offset point count) (editor-error)))))
1677
1678(defcommand "Select Forward List" (p)
1679 "Skip over the next Lisp list, extending the selection.
1680 With argument, skips the next p lists."
1681 "Skip over the next Lisp list, extending the selection."
1682 (let ((point (current-point-for-selection-end))
1683 (count (or p 1)))
1684 (pre-command-parse-check point)
1685 (unless (list-offset point count) (editor-error))))
1686
1687(defcommand "Backward List" (p)
1688 "Skip over the previous Lisp list, collapsing the selection.
1689 With argument, skips the previous p lists."
1690 "Skip over the previous Lisp list, collapsing the selection."
1691 (or (collapse-if-selection :direction :backward)
1692 (let ((point (current-point-collapsing-selection))
1693 (count (- (or p 1))))
1694 (pre-command-parse-check point)
1695 (unless (list-offset point count) (editor-error)))))
1696
1697(defcommand "Select Backward List" (p)
1698 "Skip over the previous Lisp list, extending the selection.
1699 With argument, skips the previous p lists."
1700 "Skip over the previous Lisp list, extending the selection."
1701 (let ((point (current-point-for-selection-start))
1702 (count (- (or p 1))))
1703 (pre-command-parse-check point)
1704 (unless (list-offset point count) (editor-error))))
1705
1706(defcommand "Forward Form" (p)
1707 "Skip over the next Form, collapsing the selection.
1708 With argument, skips the next p Forms."
1709 "Skip over the next Form, collapsing the selection."
1710 (or (collapse-if-selection :direction :forward)
1711 (let ((point (current-point-collapsing-selection))
1712 (count (or p 1)))
1713 (pre-command-parse-check point)
1714 (unless (form-offset point count) (editor-error)))))
1715
1716(defcommand "Select Forward Form" (p)
1717 "Skip over the next Form, extending the selection.
1718 With argument, skips the next p Forms."
1719 "Skip over the next Form, extending the selection."
1720 (let ((point (current-point-for-selection-end))
1721 (count (or p 1)))
1722 (pre-command-parse-check point)
1723 (unless (form-offset point count) (editor-error))))
1724
1725(defcommand "Backward Form" (p)
1726 "Skip over the previous Form, collapsing the selection.
1727 With argument, skips the previous p Forms."
1728 "Skip over the previous Form, collaspsing the selection."
1729 (or (collapse-if-selection :direction :backward)
1730 (let ((point (current-point-collapsing-selection))
1731 (count (- (or p 1))))
1732 (pre-command-parse-check point)
1733 (unless (form-offset point count) (editor-error)))))
1734
1735(defcommand "Select Backward Form" (p)
1736 "Skip over the previous Form, extending the selection.
1737 With argument, skips the previous p Forms."
1738 "Skip over the previous Form, extending the selection."
1739 (let ((point (current-point-for-selection-start))
1740 (count (- (or p 1))))
1741 (pre-command-parse-check point)
1742 (unless (form-offset point count) (editor-error))))
1743
1744(defcommand "Mark Form" (p)
1745 "Set the mark at the end of the next Form.
1746 With a positive argument, set the mark after the following p
1747 Forms. With a negative argument, set the mark before
1748 the preceding -p Forms."
1749 "Set the mark at the end of the next Form."
1750 (with-mark ((m (current-point)))
1751 (pre-command-parse-check m)
1752 (let ((count (or p 1))
1753 (mark (push-new-buffer-mark m t)))
1754 (if (form-offset m count)
1755 (move-mark mark m)
1756 (editor-error)))))
1757
1758(defcommand "Mark Defun" (p)
1759 "Puts the region around the next or containing top-level form.
1760 The point is left before the form and the mark is placed immediately
1761 after it."
1762 "Puts the region around the next or containing top-level form."
1763 (declare (ignore p))
1764 (let ((point (current-point)))
1765 (pre-command-parse-check point)
1766 (with-mark ((start point)
1767 (end point))
1768 (cond ((not (mark-top-level-form start end))
1769 (editor-error "No current or next top level form."))
1770 (t
1771 (move-mark point start)
1772 (move-mark (push-new-buffer-mark point t) end))))))
1773
1774(defcommand "Forward Kill Form" (p)
1775 "Kill the next Form.
1776 With a positive argument, kills the next p Forms.
1777 Kills backward with a negative argument."
1778 "Kill the next Form."
1779 (with-mark ((m1 (current-point))
1780 (m2 (current-point)))
1781 (pre-command-parse-check m1)
1782 (let ((count (or p 1)))
1783 (unless (form-offset m1 count) (editor-error))
1784 (if (minusp count)
1785 (kill-region (region m1 m2) :kill-backward)
1786 (kill-region (region m2 m1) :kill-forward)))))
1787
1788(defcommand "Backward Kill Form" (p)
1789 "Kill the previous Form.
1790 With a positive argument, kills the previous p Forms.
1791 Kills forward with a negative argument."
1792 "Kill the previous Form."
1793 (forward-kill-form-command (- (or p 1))))
1794
1795(defcommand "Extract Form" (p)
1796 "Replace the current containing list with the next form. The entire affected
1797 area is pushed onto the kill ring. If an argument is supplied, that many
1798 upward levels of list nesting is replaced by the next form."
1799 "Replace the current containing list with the next form. The entire affected
1800 area is pushed onto the kill ring. If an argument is supplied, that many
1801 upward levels of list nesting is replaced by the next form."
1802 (let ((point (current-point)))
1803 (pre-command-parse-check point)
1804 (with-mark ((form-start point :right-inserting)
1805 (form-end point))
1806 (unless (form-offset form-end 1) (editor-error))
1807 (form-offset (move-mark form-start form-end) -1)
1808 (with-mark ((containing-start form-start :left-inserting)
1809 (containing-end form-end :left-inserting))
1810 (dotimes (i (or p 1))
1811 (unless (and (forward-up-list containing-end)
1812 (backward-up-list containing-start))
1813 (editor-error)))
1814 (let ((r (copy-region (region form-start form-end))))
1815 (ring-push (delete-and-save-region
1816 (region containing-start containing-end))
1817 *kill-ring*)
1818 (ninsert-region point r)
1819 (move-mark point form-start))))))
1820
1821(defcommand "Extract List" (p)
1822 "Extract the current list.
1823 The current list replaces the surrounding list. The entire affected
1824 area is pushed on the kill-ring. With prefix argument, remove that
1825 many surrounding lists."
1826 "Replace the P containing lists with the current one."
1827 (let ((point (current-point)))
1828 (pre-command-parse-check point)
1829 (with-mark ((lstart point :right-inserting)
1830 (lend point))
1831 (if (eq (character-attribute :lisp-syntax (next-character lstart))
1832 :open-paren)
1833 (mark-after lend)
1834 (unless (backward-up-list lstart) (editor-error)))
1835 (unless (forward-up-list lend) (editor-error))
1836 (with-mark ((rstart lstart)
1837 (rend lend))
1838 (dotimes (i (or p 1))
1839 (unless (and (forward-up-list rend) (backward-up-list rstart))
1840 (editor-error)))
1841 (let ((r (copy-region (region lstart lend))))
1842 (ring-push (delete-and-save-region (region rstart rend))
1843 *kill-ring*)
1844 (ninsert-region point r)
1845 (move-mark point lstart))))))
1846
1847(defcommand "Transpose Forms" (p)
1848 "Transpose Forms immediately preceding and following the point.
1849 With a zero argument, tranposes the Forms at the point and the mark.
1850 With a positive argument, transposes the Form preceding the point
1851 with the p-th one following it. With a negative argument, transposes the
1852 Form following the point with the p-th one preceding it."
1853 "Transpose Forms immediately preceding and following the point."
1854 (let ((point (current-point))
1855 (count (or p 1)))
1856 (pre-command-parse-check point)
1857 (if (zerop count)
1858 (let ((mark (current-mark)))
1859 (with-mark ((s1 mark :left-inserting)
1860 (s2 point :left-inserting))
1861 (scan-char s1 :whitespace nil)
1862 (scan-char s2 :whitespace nil)
1863 (with-mark ((e1 s1 :right-inserting)
1864 (e2 s2 :right-inserting))
1865 (unless (form-offset e1 1) (editor-error))
1866 (unless (form-offset e2 1) (editor-error))
1867 (ninsert-region s1 (delete-and-save-region (region s2 e2)))
1868 (ninsert-region s2 (delete-and-save-region (region s1 e1))))))
1869 (let ((fcount (if (plusp count) count 1))
1870 (bcount (if (plusp count) 1 count)))
1871 (with-mark ((s1 point :left-inserting)
1872 (e2 point :right-inserting))
1873 (dotimes (i bcount)
1874 (unless (form-offset s1 -1) (editor-error)))
1875 (dotimes (i fcount)
1876 (unless (form-offset e2 1) (editor-error)))
1877 (with-mark ((e1 s1 :right-inserting)
1878 (s2 e2 :left-inserting))
1879 (unless (form-offset e1 1) (editor-error))
1880 (unless (form-offset s2 -1) (editor-error))
1881 (ninsert-region s1 (delete-and-save-region (region s2 e2)))
1882 (ninsert-region s2 (delete-and-save-region (region s1 e1)))
1883 (move-mark point s2)))))))
1884
1885
1886(defcommand "Insert ()" (count)
1887 "Insert a pair of parentheses (). With positive argument, puts
1888 parentheses around the next COUNT Forms, or previous COUNT forms, if
1889 COUNT is negative. The point is positioned after the open parenthesis."
1890 "Insert a pair of parentheses ()."
1891 ;; TODO Form navigation is broken, so this is broken too -- it is
1892 ;; possible to put parens around more forms than there are in current
1893 ;; expression. It works by moving past as many forms as there is, and
1894 ;; then each delimiting paren also counts as a form.
1895 (let ((point (current-point)))
1896 (pre-command-parse-check point)
1897 (cond (count
1898 (when (minusp count)
1899 (form-offset point count)
1900 (setq count (- count)))
1901 (insert-character point #\()
1902 (with-mark ((m point))
1903 (unless (form-offset m count)
1904 (editor-error "Could not find that many forms."))
1905 (insert-character m #\))))
1906 ;; The simple case with no prefix argument
1907 (t
1908 (insert-character point #\()
1909 (insert-character point #\))
1910 (mark-before point)))))
1911
1912
1913(defcommand "Move Over )" (p)
1914 "Move past the next close parenthesis, and start a new line. Any
1915 indentation preceding the preceding the parenthesis is deleted, and the
1916 new line is indented. If there is only whitespace preceding the close
1917 paren, the paren is moved to the end of the previous line. With prefix
1918 argument, this command moves past next closing paren and inserts space."
1919 "Move past the next close parenthesis, and start a new line."
1920 ;; TODO This is still not complete, because SCAN-CHAR finds the next
1921 ;; close-paren, but we need to find the next paren that closes current
1922 ;; expression. This will have to be updated when form navigation is
1923 ;; fixed.
1924 (let ((point (current-point)))
1925 (pre-command-parse-check point)
1926 (with-mark ((m point :right-inserting))
1927 (cond ((scan-char m :lisp-syntax :close-paren)
1928 (cond ((same-line-p point m)
1929 (delete-horizontal-space m))
1930 (t
1931 (move-mark point m)
1932 (reverse-find-attribute point :whitespace #'zerop)
1933 (delete-region (region point m))))
1934 (cond ((not p)
1935 ;; Move to the previous line if current is empty
1936 (when (zerop (mark-charpos m))
1937 (delete-characters m -1))
1938 (mark-after m)
1939 (move-mark point m)
1940 (indent-new-line-command 1))
1941 (t
1942 (mark-after m)
1943 (move-mark point m)
1944 (insert-character m #\space))))
1945 (t
1946 (editor-error "Could not find closing paren."))))))
1947
1948
1949(defcommand "Forward Up List" (p)
1950 "Move forward past a one containing )."
1951 "Move forward past a one containing )."
1952 (or (collapse-if-selection :direction :forward)
1953 (let ((point (current-point-collapsing-selection))
1954 (count (or p 1)))
1955 (pre-command-parse-check point)
1956 (if (minusp count)
1957 (backward-up-list-command (- count))
1958 (with-mark ((m point))
1959 (dotimes (i count (move-mark point m))
1960 (unless (forward-up-list m) (editor-error))))))))
1961
1962(defcommand "Backward Up List" (p)
1963 "Move backward past a one containing (."
1964 "Move backward past a one containing (."
1965 (or (collapse-if-selection :direction :backward)
1966 (let ((point (current-point-collapsing-selection))
1967 (count (or p 1)))
1968 (pre-command-parse-check point)
1969 (if (minusp count)
1970 (forward-up-list-command (- count))
1971 (with-mark ((m point))
1972 (dotimes (i count (move-mark point m))
1973 (unless (backward-up-list m) (editor-error))))))))
1974
1975
1976(defcommand "Down List" (p)
1977 "Move down a level in list structure. With positive argument, moves down
1978 p levels. With negative argument, moves down backward, but only one
1979 level."
1980 "Move down a level in list structure."
1981 (let ((point (current-point-collapsing-selection))
1982 (count (or p 1)))
1983 (pre-command-parse-check point)
1984 (with-mark ((m point))
1985 (cond ((plusp count)
1986 (loop repeat count
1987 do (unless (and (scan-char m :lisp-syntax :open-paren)
1988 (mark-after m))
1989 (editor-error))))
1990 (t
1991 (unless (and (rev-scan-char m :lisp-syntax :close-paren)
1992 (mark-before m))
1993 (editor-error))))
1994 (move-mark point m))))
1995
1996
1997
1998
1999;;;; Filling Lisp comments, strings, and indented text.
2000
2001(defhvar "Fill Lisp Comment Paragraph Confirm"
2002 "This determines whether \"Fill Lisp Comment Paragraph\" will prompt for
2003 confirmation to fill contiguous lines with the same initial whitespace when
2004 it is invoked outside of a comment or string."
2005 :value t)
2006
2007(defcommand "Fill Lisp Comment Paragraph" (p)
2008 "This fills a flushleft or indented Lisp comment.
2009 This also fills Lisp string literals using the proper indentation as a
2010 filling prefix. When invoked outside of a comment or string, this tries
2011 to fill all contiguous lines beginning with the same initial, non-empty
2012 blankspace. When filling a comment, the current line is used to determine a
2013 fill prefix by taking all the initial whitespace on the line, the semicolons,
2014 and any whitespace following the semicolons."
2015 "Fills a flushleft or indented Lisp comment."
2016 (declare (ignore p))
2017 (let ((point (current-point)))
2018 (pre-command-parse-check point)
2019 (with-mark ((start point)
2020 (end point)
2021 (m point))
2022 (let ((commentp (fill-lisp-comment-paragraph-prefix start end)))
2023 (cond (commentp
2024 (fill-lisp-comment-or-indented-text start end))
2025 ((and (not (valid-spot m nil))
2026 (form-offset m -1)
2027 (eq (character-attribute :lisp-syntax (next-character m))
2028 :string-quote))
2029 (fill-lisp-string m))
2030 ((or (not (value fill-lisp-comment-paragraph-confirm))
2031 (prompt-for-y-or-n
2032 :prompt '("Not in a comment or string. Fill contiguous ~
2033 lines with the same initial whitespace? ")))
2034 (fill-lisp-comment-or-indented-text start end)))))))
2035
2036;;; FILL-LISP-STRING -- Internal.
2037;;;
2038;;; This fills the Lisp string containing mark as if it had been entered using
2039;;; Hemlock's Lisp string indentation, "Indent Function" for "Lisp" mode. This
2040;;; assumes the area around mark has already been PRE-COMMAND-PARSE-CHECK'ed,
2041;;; and it ensures the string ends before doing any filling. This function
2042;;; is undo'able.
2043;;;
2044(defun fill-lisp-string (mark)
2045 (with-mark ((end mark))
2046 (unless (form-offset end 1)
2047 (editor-error "Attempted to fill Lisp string, but it doesn't end?"))
2048 (let* ((mark (copy-mark mark :left-inserting))
2049 (end (copy-mark end :left-inserting))
2050 (string-region (region mark end))
2051 (undo-region (copy-region string-region))
2052 (hack (make-empty-region)))
2053 ;; Generate prefix.
2054 (indent-to-column (region-end hack) (1+ (mark-column mark)))
2055 ;; Skip opening double quote and fill string starting on its own line.
2056 (mark-after mark)
2057 (insert-character mark #\newline)
2058 (line-start mark)
2059 (setf (mark-kind mark) :right-inserting)
2060 (fill-region string-region (region-to-string hack))
2061 ;; Clean up inserted prefix on first line, delete inserted newline, and
2062 ;; move before the double quote for undo.
2063 (with-mark ((text mark :left-inserting))
2064 (find-attribute text :whitespace #'zerop)
2065 (delete-region (region mark text)))
2066 (delete-characters mark -1)
2067 (mark-before mark)
2068 ;; Save undo.
2069 (make-region-undo :twiddle "Fill Lisp Comment Paragraph"
2070 string-region undo-region))))
2071
2072;;; FILL-LISP-COMMENT-OR-INDENTED-TEXT -- Internal.
2073;;;
2074;;; This fills all contiguous lines around start and end containing fill prefix
2075;;; designated by the region between start and end. These marks can only be
2076;;; equal when there is no comment and no initial whitespace. This is a bad
2077;;; situation since this function in that situation would fill the entire
2078;;; buffer into one paragraph. This function is undo'able.
2079;;;
2080(defun fill-lisp-comment-or-indented-text (start end)
2081 (when (mark= start end)
2082 (editor-error "This command only fills Lisp comments, strings, or ~
2083 indented text, but this line is flushleft."))
2084 ;;
2085 ;; Find comment block.
2086 (let* ((prefix (region-to-string (region start end)))
2087 (length (length prefix)))
2088 (declare (simple-string prefix))
2089 (flet ((frob (mark direction)
2090 (loop
2091 (let* ((line (line-string (mark-line mark)))
2092 (line-len (length line)))
2093 (declare (simple-string line))
2094 (unless (string= line prefix :end1 (min line-len length))
2095 (when (= direction -1)
2096 (unless (same-line-p mark end) (line-offset mark 1 0)))
2097 (return)))
2098 (unless (line-offset mark direction 0)
2099 (when (= direction 1) (line-end mark))
2100 (return)))))
2101 (frob start -1)
2102 (frob end 1))
2103 ;;
2104 ;; Do it undoable.
2105 (let* ((start1 (copy-mark start :right-inserting))
2106 (end2 (copy-mark end :left-inserting))
2107 (region (region start1 end2))
2108 (undo-region (copy-region region)))
2109 (fill-region region prefix)
2110 (make-region-undo :twiddle "Fill Lisp Comment Paragraph"
2111 region undo-region))))
2112
2113;;; FILL-LISP-COMMENT-PARAGRAPH-PREFIX -- Internal.
2114;;;
2115;;; This sets start and end around the prefix to be used for filling. We
2116;;; assume we are dealing with a comment. If there is no ";", then we try to
2117;;; find some initial whitespace. If there is a ";", we make sure the line is
2118;;; blank before it to eliminate ";"'s in the middle of a line of text.
2119;;; Finally, if we really have a comment instead of some indented text, we skip
2120;;; the ";"'s and any immediately following whitespace. We allow initial
2121;;; whitespace, so we can fill strings with the same command.
2122;;;
2123(defun fill-lisp-comment-paragraph-prefix (start end)
2124 (line-start start)
2125 (let ((commentp t)) ; Assumes there's a comment.
2126 (unless (to-line-comment (line-start end) ";")
2127 (find-attribute end :whitespace #'zerop)
2128 #|(when (start-line-p end)
2129 (editor-error "No comment on line, and no initial whitespace."))|#
2130 (setf commentp nil))
2131 (when commentp
2132 (unless (blank-before-p end)
2133 (find-attribute (line-start end) :whitespace #'zerop)
2134 #|(when (start-line-p end)
2135 (editor-error "Semicolon preceded by unindented text."))|#
2136 (setf commentp nil)))
2137 (when commentp
2138 (find-attribute end :lisp-syntax #'(lambda (x) (not (eq x :comment))))
2139 (find-attribute end :whitespace #'zerop))
2140 commentp))
2141
2142
2143
2144
2145;;;; "Lisp" mode.
2146
2147(defcommand "LISP Mode" (p)
2148 "Put current buffer in LISP mode."
2149 "Put current buffer in LISP mode."
2150 (declare (ignore p))
2151 (setf (buffer-major-mode (current-buffer)) "LISP"))
2152
2153
2154(defmode "Lisp" :major-p t :setup-function 'setup-lisp-mode)
2155
2156
2157(defun buffer-first-in-package-form (buffer)
2158 "Returns the package name referenced in the first apparent IN-PACKAGE
2159 form in buffer, or NIL if it can't find an IN-PACKAGE."
2160 (let* ((pattern (new-search-pattern :string-insensitive :forward "in-package" nil))
2161 (mark (copy-mark (buffer-start-mark buffer))))
2162 (with-mark ((start mark)
2163 (end mark))
2164 (loop
2165 (unless (find-pattern mark pattern)
2166 (return))
2167 (pre-command-parse-check mark)
2168 (when (valid-spot mark t)
2169 (move-mark end mark)
2170 (when (form-offset end 1)
2171 (move-mark start end)
2172 (when (backward-up-list start)
2173 (when (scan-char start :lisp-syntax :constituent)
2174 (let* ((s (nstring-upcase (region-to-string (region start end))))
2175 (*package* (find-package "CL-USER")))
2176 (unless (eq (ignore-errors (values (read-from-string s)))
2177 'in-package)
2178 (return)))
2179 (unless (form-offset end 1) (return))
2180 (move-mark start end)
2181 (form-offset start -1)
2182 (let* ((pkgname (ignore-errors (values (read-from-string (region-to-string (region start end)))))))
2183 (return
2184 (if pkgname
2185 (values (ignore-errors (string pkgname))))))))))))))
2186
2187(defparameter *previous-in-package-search-pattern*
2188 (new-search-pattern :string-insensitive :backward "in-package" nil))
2189
2190(defun package-at-mark (start-mark)
2191 (let* ((pattern *previous-in-package-search-pattern*)
2192 (mark (copy-mark start-mark :temporary)))
2193 (with-mark ((start mark)
2194 (end mark)
2195 (list-end mark))
2196 (loop
2197 (unless (find-pattern mark pattern)
2198 (return))
2199 (pre-command-parse-check mark)
2200 (when (valid-spot mark t)
2201 (move-mark end mark)
2202 (when (form-offset end 1)
2203 (move-mark start end)
2204 (when (backward-up-list start)
2205 (move-mark list-end start)
2206 (unless (and (list-offset list-end 1)
2207 (mark<= list-end start-mark))
2208 (return))
2209 (when (scan-char start :lisp-syntax :constituent)
2210 (unless (or (mark= mark start)
2211 (let* ((s (nstring-upcase (region-to-string (region start end))))
2212 (*package* (find-package "CL-USER")))
2213 (eq (ignore-errors (values (read-from-string s)))
2214 'in-package)))
2215 (return))
2216 (unless (form-offset end 1) (format t "~& worse") (return 4))
2217 (move-mark start end)
2218 (form-offset start -1)
2219 (return
2220 (if (eql (next-character start) #\")
2221 (progn
2222 (character-offset start 1)
2223 (character-offset end -1)
2224 (region-to-string (region start end)))
2225 (let* ((pkgname (ignore-errors (values (read-from-string (region-to-string (region start end)))))))
2226 (if pkgname
2227 (values (ignore-errors (string pkgname)))))))))))))))
2228
2229(defun ensure-buffer-package (buffer)
2230 (or (variable-value 'current-package :buffer buffer)
2231 (setf (variable-value 'current-package :buffer buffer)
2232 (buffer-first-in-package-form buffer))))
2233
2234(defun buffer-package (buffer)
2235 (when (hemlock-bound-p 'current-package :buffer buffer)
2236 (let ((package-name (variable-value 'current-package :buffer buffer)))
2237 (find-package package-name))))
2238
2239(defun setup-lisp-mode (buffer)
2240 (unless (hemlock-bound-p 'current-package :buffer buffer)
2241 (defhvar "Current Package"
2242 "The package used for evaluation of Lisp in this buffer."
2243 :buffer buffer
2244 :value nil
2245 :hooks (list 'package-name-change-hook)))
2246 (unless (hemlock-bound-p 'default-package :buffer buffer)
2247 (defhvar "Default Package"
2248 "The package to use if the current package doesn't exist or isn't set."
2249 :buffer buffer
2250 :value (package-name *package*))))
2251
2252
2253
2254
2255
2256
2257;;;; Some mode variables to coordinate with other stuff.
2258
2259(defhvar "Auto Fill Space Indent"
2260 "When non-nil, uses \"Indent New Comment Line\" to break lines instead of
2261 \"New Line\"."
2262 :mode "Lisp" :value t)
2263
2264(defhvar "Comment Start"
2265 "String that indicates the start of a comment."
2266 :mode "Lisp" :value ";")
2267
2268(defhvar "Comment Begin"
2269 "String that is inserted to begin a comment."
2270 :mode "Lisp" :value "; ")
2271
2272(defhvar "Indent Function"
2273 "Indentation function which is invoked by \"Indent\" command.
2274 It must take one argument that is the prefix argument."
2275 :value 'indent-for-lisp
2276 :mode "Lisp")
2277
2278(defun string-to-arglist (string buffer &optional quiet-if-unknown)
2279 (multiple-value-bind (name error)
2280 (let* ((*package* (or
2281 (find-package
2282 (variable-value 'current-package :buffer buffer))
2283 *package*)))
2284 (ignore-errors (values (read-from-string string))))
2285 (unless error
2286 (when (typep name 'symbol)
2287 (multiple-value-bind (arglist win)
2288 (ccl::arglist-string name)
2289 (if (or win (not quiet-if-unknown))
2290 (format nil "~S : ~A" name (if win (or arglist "()") "(unknown)"))))))))
2291
2292(defcommand "Current Function Arglist" (p)
2293 "Show arglist of function whose name precedes point."
2294 "Show arglist of function whose name precedes point."
2295 (declare (ignore p))
2296 (let ((point (current-point)))
2297 (pre-command-parse-check point)
2298 (with-mark ((mark1 point)
2299 (mark2 point))
2300 (when (backward-up-list mark1)
2301 (when (form-offset (move-mark mark2 (mark-after mark1)) 1)
2302 (let* ((fun-name (region-to-string (region mark1 mark2)))
2303 (arglist-string (string-to-arglist fun-name (current-buffer))))
2304 (when arglist-string
2305 (message "~a" arglist-string))))))))
2306
2307(defcommand "Arglist On Space" (p)
2308 "Insert a space, then show the current function's arglist."
2309 "Insert a space, then show the current function's arglist."
2310 (declare (ignore p))
2311 (let ((point (current-point)))
2312 (insert-character point #\Space)
2313 (pre-command-parse-check point)
2314 (with-mark ((m point))
2315 (when (backward-up-list m)
2316 (when (and (scan-char m :lisp-syntax :open-paren)
2317 (mark-after m))
2318 (with-mark ((n m))
2319 (forward-form n)
2320 (let* ((fun-name (region-to-string (region m n)))
2321 (arglist-string (string-to-arglist fun-name (current-buffer) t)))
2322 (when arglist-string
2323 (message "~a" arglist-string)))))))))
2324
2325(hi:defcommand "Show Callers" (p)
2326 "Display a scrolling list of the callers of the symbol at point.
2327 Double-click a row to go to the caller's definition."
2328 (declare (ignore p))
2329 (with-mark ((mark1 (current-point))
2330 (mark2 (current-point)))
2331 (mark-symbol mark1 mark2)
2332 (with-input-from-region (s (region mark1 mark2))
2333 (let* ((symbol (read s)))
2334 (hemlock-ext:open-sequence-dialog
2335 :title (format nil "Callers of ~a" symbol)
2336 :sequence (ccl::callers symbol)
2337 :action #'edit-definition)))))
2338
2339#||
2340(defcommand "Set Package Name" (p)
2341 (variable-value 'current-package :buffer buffer)
2342||#
2343
2344(defcommand "Insert Sharp Comment" (p)
2345 "Inserts #| |# around the selection and puts point between them."
2346 (declare (ignore p))
2347 (multiple-value-bind (start end) (buffer-selection-range (current-buffer))
2348 (let ((point (current-point)))
2349 (cond ((= start end)
2350 (insert-string point "#|")
2351 (insert-character point #\newline)
2352 (insert-character point #\newline)
2353 (insert-string point "|#")
2354 (character-offset point -3))
2355 (t
2356 (with-mark ((start-mark point :left-inserting)
2357 (end-mark point :left-inserting))
2358 (move-to-absolute-position start-mark start)
2359 (move-to-absolute-position end-mark end)
2360 (insert-string start-mark "#|
2361")
2362 (insert-string end-mark "
2363|#")))))))
Note: See TracBrowser for help on using the repository browser.