Newer
Older
;;; -*- Log: hemlock.log; Package: Hemlock -*-
;;;
;;; **********************************************************************
;;; This code was written as part of the CMU Common Lisp project at
;;; Carnegie Mellon University, and has been placed in the public domain.
;;; If you want to use this code or any part of CMU Common Lisp, please contact
;;; Scott Fahlman or slisp-group@cs.cmu.edu.
;;;
(ext:file-comment
"$Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/hemlock/lispmode.lisp,v 1.1.1.16 1992/02/15 01:01:50 wlott Exp $")
;;; **********************************************************************
;;;
;;; Hemlock LISP Mode commands
;;;
;;; Written by Ivan Vazquez and Bill Maddox.
;;;
(in-package "HEMLOCK")
;;; These routines are used to define, for standard LISP mode, the start and end
;;; of a block to parse. If these need to be changed for a minor mode that sits
;;; on top of LISP mode, simply do a DEFHVAR with the minor mode and give the
;;; name of the function to use instead of START-OF-PARSE-BLOCK and
;;; END-OF-PARSE-BLOCK.
;;;
(defhvar "Parse Start Function"
"Take a mark and move it to the top of a block for paren parsing."
:value 'start-of-parse-block)
(defhvar "Parse End Function"
"Take a mark and move it to the bottom of a block for paren parsing."
:value 'end-of-parse-block)
;;; LISP-INFO is the structure used to store the data about the line in its
;;; Plist.
;;;
;;; -> BEGINS-QUOTED, ENDING-QUOTED are both Boolean slots that tell whether
;;; or not a line's begining and/or ending are quoted.
;;;
;;; -> RANGES-TO-IGNORE is a list of cons cells, each having the form
;;; ( [begining-charpos] [end-charpos] ) each of these cells indicating
;;; a range to ignore. End is exclusive.
;;;
;;; -> NET-OPEN-PARENS, NET-CLOSE-PARENS integers that are the number of
;;; unmatched opening and closing parens that there are on a line.
;;;
;;; -> SIGNATURE-SLOT ...
;;;
(defstruct (lisp-info (:constructor make-lisp-info ()))
(begins-quoted nil) ; (or t nil)
(ending-quoted nil) ; (or t nil)
(ranges-to-ignore nil) ; (or t nil)
(net-open-parens 0 :type fixnum)
(net-close-parens 0 :type fixnum)
(signature-slot))
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
;;; The following Macros exist to make it easy to acces the Syntax primitives
;;; without uglifying the code. They were originally written by Maddox.
;;;
(defmacro scan-char (mark attribute values)
`(find-attribute ,mark ',attribute ,(attr-predicate values)))
(defmacro rev-scan-char (mark attribute values)
`(reverse-find-attribute ,mark ',attribute ,(attr-predicate values)))
(defmacro test-char (char attribute values)
`(let ((x (character-attribute ',attribute ,char)))
,(attr-predicate-aux values)))
(eval-when (compile load eval)
(defun attr-predicate (values)
(cond ((eq values 't)
'#'plusp)
((eq values 'nil)
'#'zerop)
(t `#'(lambda (x) ,(attr-predicate-aux values)))))
(defun attr-predicate-aux (values)
(cond ((eq values t)
'(plusp x))
((eq values nil)
'(zerop x))
((symbolp values)
`(eq x ',values))
((and (listp values) (member (car values) '(and or not)))
(cons (car values) (mapcar #'attr-predicate-aux (cdr values))))
(t (error "Illegal form in attribute pattern - ~S" values))))
); Eval-When (Compile Load Eval)
;;;
;;; FIND-LISP-CHAR
(defmacro find-lisp-char (mark)
"Move MARK to next :LISP-SYNTAX character, if one isn't found, return NIL."
`(find-attribute ,mark :lisp-syntax
#'(lambda (x)
(member x '(:open-paren :close-paren :newline :comment
:char-quote :string-quote)))))
;;;
;;; PUSH-RANGE
(defmacro push-range (new-range info-struct)
"Insert NEW-RANGE into the LISP-INFO-RANGES-TO-IGNORE slot of the INFO-STRUCT."
`(when ,new-range
(setf (lisp-info-ranges-to-ignore ,info-struct)
(cons ,new-range (lisp-info-ranges-to-ignore ,info-struct)))))
;;;
;;; SCAN-DIRECTION
(defmacro scan-direction (mark forwardp &rest forms)
"Expand to a form that scans either backward or forward according to Forwardp."
(if forwardp
`(scan-char ,mark ,@forms)
`(rev-scan-char ,mark ,@forms)))
;;;
;;; DIRECTION-CHAR
(defmacro direction-char (mark forwardp)
"Expand to a form that returns either the previous or next character according
to Forwardp."
(if forwardp
`(next-character ,mark)
`(previous-character ,mark)))
;;;
;;; NEIGHBOR-MARK
(defmacro neighbor-mark (mark forwardp)
"Expand to a form that moves MARK either backward or forward one character,
depending on FORWARDP."
(if forwardp
`(mark-after ,mark)
`(mark-before ,mark)))
;;;
;;; NEIGHBOR-LINE
(defmacro neighbor-line (line forwardp)
"Expand to return the next or previous line, according to Forwardp."
(if forwardp
`(line-next ,line)
`(line-previous ,line)))
(defun pre-command-parse-check (mark &optional (fer-sure-parse nil))
"Parse the area before the command is actually executed."
(with-mark ((top mark)
(bottom mark))
(funcall (value parse-start-function) top)
(funcall (value parse-end-function) bottom)
(parse-over-block (mark-line top) (mark-line bottom) fer-sure-parse)))
;;; PARSE-OVER-BLOCK
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
(defun parse-over-block (start-line end-line &optional (fer-sure-parse nil))
"Parse over an area indicated from END-LINE to START-LINE."
(let ((test-line start-line)
prev-line-info)
(with-mark ((mark (mark test-line 0)))
; Set the pre-begining and post-ending lines to delimit the range
; of action any command will take. This means set the lisp-info of the
; lines immediately before and after the block to Nil.
(when (line-previous start-line)
(setf (getf (line-plist (line-previous start-line)) 'lisp-info) nil))
(when (line-next end-line)
(setf (getf (line-plist (line-next end-line)) 'lisp-info) nil))
(loop
(let ((line-info (getf (line-plist test-line) 'lisp-info)))
;; Reparse the line when any of the following are true:
;;
;; FER-SURE-PARSE is T
;;
;; LINE-INFO or PREV-LINE-INFO are Nil.
;;
;; If the line begins quoted and the previous one wasn't
;; ended quoted.
;;
;; The Line's signature slot is invalid (the line has changed).
;;
(when (or fer-sure-parse
(not line-info)
(not prev-line-info)
(not (eq (lisp-info-begins-quoted line-info)
(lisp-info-ending-quoted prev-line-info)))
(not (eql (line-signature test-line)
(lisp-info-signature-slot line-info))))
(move-to-position mark 0 test-line)
(unless line-info
(setf line-info (make-lisp-info))
(setf (getf (line-plist test-line) 'lisp-info) line-info))
(parse-lisp-line-info mark line-info prev-line-info))
(when (eq end-line test-line)
(return nil))
(setq prev-line-info line-info)
(setq test-line (line-next test-line)))))))
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
(defhvar "Minimum Lines Parsed"
"The minimum number of lines before and after the point parsed by Lisp mode."
:value 50)
(defhvar "Maximum Lines Parsed"
"The maximum number of lines before and after the point parsed by Lisp mode."
:value 500)
(defhvar "Defun Parse Goal"
"Lisp mode parses the region obtained by skipping this many defuns forward
and backward from the point unless this falls outside of the range specified
by \"Minimum Lines Parsed\" and \"Maximum Lines Parsed\"."
:value 2)
(macrolet ((frob (step end)
`(let ((min (value minimum-lines-parsed))
(max (value maximum-lines-parsed))
(goal (value defun-parse-goal))
(last-defun nil))
(declare (fixnum min max goal))
(do ((line (mark-line mark) (,step line))
(count 0 (1+ count)))
((null line)
(,end mark))
(declare (fixnum count))
(when (char= (line-character line 0) #\()
(setq last-defun line)
(decf goal)
(when (and (<= goal 0) (>= count min))
(line-start mark line)
(return)))
(when (> count max)
(line-start mark (or last-defun line))
(return))))))
(defun start-of-parse-block (mark)
(frob line-previous buffer-start))
(defun end-of-parse-block (mark)
(frob line-next buffer-end)))
;;;
;;; START-OF-SEARCH-LINE
(defun start-of-search-line (line)
"Set LINE to the begining line of the block of text to parse."
(with-mark ((mark (mark line 0)))
(funcall (value 'Parse-Start-Function) mark)
(setq line (mark-line mark))))
;;;
;;; END-OF-SEACH-LINE
(defun end-of-search-line (line)
"Set LINE to the ending line of the block of text to parse."
(with-mark ((mark (mark line 0)))
(funcall (value 'Parse-End-Function) mark)
(setq line (mark-line mark))))
;;;; PARSE-LISP-LINE-INFO.
;;; PARSE-LISP-LINE-INFO -- Internal.
;;;
;;; This parses through the line doing the following things:
;;;
;;; Making all areas of the line that should be invalid (comments,
;;; char-quotes, and the inside of strings) and such be in
;;; RANGES-TO-IGNORE.
;;;
;;; Set BEGINS-QUOTED and ENDING-QUOTED
(defun parse-lisp-line-info (mark line-info prev-line-info)
"Parse line and set line information like NET-OPEN-PARENS, NET-CLOSE-PARENS,
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
(let ((net-open-parens 0)
(net-close-parens 0))
(declare (fixnum net-open-parens net-close-parens))
;; Re-set the slots necessary
(setf (lisp-info-ranges-to-ignore line-info) nil)
;; The only way the current line begins quoted is when there
;; is a previous line and it's ending was quoted.
(setf (lisp-info-begins-quoted line-info)
(and prev-line-info
(lisp-info-ending-quoted prev-line-info)))
(if (lisp-info-begins-quoted line-info)
(deal-with-string-quote mark line-info)
(setf (lisp-info-ending-quoted line-info) nil))
(unless (lisp-info-ending-quoted line-info)
(loop
(find-lisp-char mark)
(ecase (character-attribute :lisp-syntax (next-character mark))
(:open-paren
(setq net-open-parens (1+ net-open-parens))
(mark-after mark))
(:close-paren
(if (zerop net-open-parens)
(setq net-close-parens (1+ net-close-parens))
(setq net-open-parens (1- net-open-parens)))
(mark-after mark))
(:newline
(setf (lisp-info-ending-quoted line-info) nil)
(return t))
(:comment
(push-range (cons (mark-charpos mark) (line-length (mark-line mark)))
line-info)
(setf (lisp-info-ending-quoted line-info) nil)
(return t))
(:char-quote
(mark-after mark)
(push-range (cons (mark-charpos mark) (1+ (mark-charpos mark)))
line-info)
(mark-after mark))
(:string-quote
(mark-after mark)
(unless (deal-with-string-quote mark line-info)
(setf (lisp-info-ending-quoted line-info) t)
(return t))))))
(setf (lisp-info-net-open-parens line-info) net-open-parens)
(setf (lisp-info-net-close-parens line-info) net-close-parens)
(setf (lisp-info-signature-slot line-info)
(line-signature (mark-line mark)))))
(defmacro valid-string-quote-p (mark forwardp)
"Return T if the string-quote indicated by MARK is valid."
(let ((test-mark (gensym)))
`(with-mark ((,test-mark ,mark))
,(unless forwardp
;; TEST-MARK should always be right before the String-quote to be
;; checked.
`(mark-before ,test-mark))
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
(when (test-char (next-character ,test-mark) :lisp-syntax :string-quote)
(let ((slash-count 0))
(loop
(mark-before ,test-mark)
(if (test-char (next-character ,test-mark) :lisp-syntax :char-quote)
(incf slash-count)
(return t)))
(not (oddp slash-count)))))))
;;;
;;; FIND-VALID-STRING-QUOTE
(defmacro find-valid-string-quote (mark &key forwardp (cease-at-eol nil))
"Expand to a form that will leave MARK before a valid string-quote character,
in either a forward or backward direction, according to FORWARDP. If
CEASE-AT-EOL is T then it will return nil if encountering the EOL before a
valid string-quote."
(let ((e-mark (gensym)))
`(with-mark ((,e-mark ,mark))
(loop
(unless (scan-direction ,e-mark ,forwardp :lisp-syntax
,(if cease-at-eol
`(or :newline :string-quote)
`:string-quote))
(return nil))
,@(if cease-at-eol
`((when (test-char (direction-char ,e-mark ,forwardp) :lisp-syntax
:newline)
(return nil))))
(when (valid-string-quote-p ,e-mark ,forwardp)
(move-mark ,mark ,e-mark)
(return t))
(neighbor-mark ,e-mark ,forwardp)))))
;;; DEAL-WITH-STRING-QUOTE
;;;
;;; Called when a string is begun (i.e. parse hits a #\"). It checks for a
;;; matching quote on the line that MARK points to, and puts the appropriate
;;; area in the RANGES-TO-IGNORE slot and leaves MARK pointing after this area.
;;; The "appropriate area" is from MARK to the end of the line or the matching
;;; string-quote, whichever comes first.
;;;
(defun deal-with-string-quote (mark info-struct)
"Alter the current line's info struct as necessary as due to encountering a
(with-mark ((e-mark mark))
(cond ((find-valid-string-quote e-mark :forwardp t :cease-at-eol t)
;; If matching quote is on this line then mark the area between the
;; first quote (MARK) and the matching quote as invalid by pushing
;; its begining and ending into the IGNORE-RANGE.
(push-range (cons (mark-charpos mark) (mark-charpos e-mark))
info-struct)
(setf (lisp-info-ending-quoted info-struct) nil)
(mark-after e-mark)
(move-mark mark e-mark))
;; If the EOL has been hit before the matching quote then mark the
;; area from MARK to the EOL as invalid.
info-struct)
;; The Ending is marked as still being quoted.
(setf (lisp-info-ending-quoted info-struct) t)
(line-end mark)
nil))))
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
;;;; Character validity checking:
;;; Find-Ignore-Region -- Internal
;;;
;;; If the character in the specified direction from Mark is in an ignore
;;; region, then return the region and the line that the region is in as
;;; values. If there is no ignore region, then return NIL and the Mark-Line.
;;; If the line is not parsed, or there is no character (because of being at
;;; the buffer beginning or end), then return both values NIL.
;;;
(defun find-ignore-region (mark forwardp)
(flet ((scan (line pos)
(declare (fixnum pos))
(let ((info (getf (line-plist line) 'lisp-info)))
(if info
(dolist (range (lisp-info-ranges-to-ignore info)
(values nil line))
(let ((start (car range))
(end (cdr range)))
(declare (fixnum start end))
(when (and (>= pos start) (< pos end))
(return (values range line)))))
(values nil nil)))))
(let ((pos (mark-charpos mark))
(line (mark-line mark)))
(declare (fixnum pos))
(cond (forwardp (scan line pos))
((> pos 0) (scan line (1- pos)))
(t
(let ((prev (line-previous line)))
(if prev
(scan prev (line-length prev))
(values nil nil))))))))
;;; Valid-Spot -- Public
;;;
(defun valid-spot (mark forwardp)
"Return true if the character pointed to by Mark is not in a quoted context,
false otherwise. If Forwardp is true, we use the next character, otherwise
we use the previous."
(multiple-value-bind (region line)
(find-ignore-region mark forwardp)
(and line (not region))))
;;; Scan-Direction-Valid -- Internal
;;;
;;; Like scan-direction, but only stop on valid characters.
;;;
(defmacro scan-direction-valid (mark forwardp &rest forms)
(let ((n-mark (gensym))
(n-line (gensym))
(n-region (gensym))
(n-won (gensym)))
`(let ((,n-mark ,mark) (,n-won nil))
(loop
(multiple-value-bind (,n-region ,n-line)
(find-ignore-region ,n-mark ,forwardp)
(unless ,n-line (return nil))
(if ,n-region
(move-to-position ,n-mark
,(if forwardp
`(cdr ,n-region)
`(car ,n-region))
,n-line)
(when ,n-won (return t)))
;;
;; Peculiar condition when a quoting character terminates a line.
;; The ignore region is off the end of the line causing %FORM-OFFSET
;; to infinitely loop.
(when (> (mark-charpos ,n-mark) (line-length ,n-line))
(line-offset ,n-mark 1 0))
(unless (scan-direction ,n-mark ,forwardp ,@forms)
(return nil))
(setq ,n-won t))))))
;;; %LIST-OFFSET allows for BACKWARD-LIST and FORWARD-LIST to be built
;;; with the same existing structure, with the altering of one variable.
;;; This one variable being FORWARDP.
;;;
(defmacro %list-offset (actual-mark forwardp &key (extra-parens 0) )
"Expand to code that will go forward one list either backward or forward,
(let ((mark (gensym)))
`(let ((paren-count ,extra-parens))
(declare (fixnum paren-count))
(with-mark ((,mark ,actual-mark))
(loop
(scan-direction ,mark ,forwardp :lisp-syntax
(or :close-paren :open-paren :newline))
(let ((ch (direction-char ,mark ,forwardp)))
(unless ch (return nil))
(when (valid-spot ,mark ,forwardp)
(case (character-attribute :lisp-syntax ch)
(:close-paren
(decf paren-count)
,(when forwardp
;; When going forward, an unmatching close-paren means the
;; end of list.
`(when (<= paren-count 0)
(neighbor-mark ,mark ,forwardp)
(move-mark ,actual-mark ,mark)
(return t))))
(:open-paren
(incf paren-count)
,(unless forwardp ; Same as above only end of list
`(when (>= paren-count 0) ; is opening parens.
(neighbor-mark ,mark ,forwardp)
(move-mark ,actual-mark ,mark)
(return t))))
(:newline
;; When a #\Newline is hit, then the matching paren must lie
;; on some other line so drop down into the multiple line
;; balancing function: QUEST-FOR-BALANCING-PAREN If no paren
;; seen yet, keep going.
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
(cond ((zerop paren-count))
((quest-for-balancing-paren ,mark paren-count ,forwardp)
(move-mark ,actual-mark ,mark)
(return t))
(t
(return nil)))))))
(neighbor-mark ,mark ,forwardp))))))
;;;
;;; QUEST-FOR-BALANCING-PAREN
(defmacro quest-for-balancing-paren (mark paren-count forwardp)
"Expand to a form that finds the the balancing paren for however many opens or
closes are registered by Paren-Count."
`(let* ((line (mark-line ,mark)))
(loop
(setq line (neighbor-line line ,forwardp))
(unless line (return nil))
(let ((line-info (getf (line-plist line) 'lisp-info))
(unbal-paren ,paren-count))
(unless line-info (return nil))
,(if forwardp
`(decf ,paren-count (lisp-info-net-close-parens line-info))
`(incf ,paren-count (lisp-info-net-open-parens line-info)))
(when ,(if forwardp
`(<= ,paren-count 0)
`(>= ,paren-count 0))
,(if forwardp
`(line-start ,mark line)
`(line-end ,mark line))
(return (goto-correct-paren-char ,mark unbal-paren ,forwardp)))
,(if forwardp
`(incf ,paren-count (lisp-info-net-open-parens line-info))
`(decf ,paren-count (lisp-info-net-close-parens line-info)))))))
;;;
;;; GOTO-CORRECT-PAREN-CHAR
(defmacro goto-correct-paren-char (mark paren-count forwardp)
"Expand to a form that will leave MARK on the correct balancing paren matching
however many are indicated by COUNT."
`(with-mark ((m ,mark))
(let ((count ,paren-count))
(loop
(scan-direction m ,forwardp :lisp-syntax
(or :close-paren :open-paren :newline))
(when (valid-spot m ,forwardp)
(ecase (character-attribute :lisp-syntax (direction-char m ,forwardp))
(:close-paren
(decf count)
,(when forwardp
`(when (zerop count)
(neighbor-mark m ,forwardp)
(move-mark ,mark m)
(return t))))
(:open-paren
(incf count)
,(unless forwardp
`(when (zerop count)
(neighbor-mark m ,forwardp)
(move-mark ,mark m)
(return t))))))
(neighbor-mark m ,forwardp)))))
(defun list-offset (mark offset)
(if (plusp offset)
(dotimes (i offset t)
(unless (%list-offset mark t) (return nil)))
(dotimes (i (- offset) t)
(unless (%list-offset mark nil) (return nil)))))
(defun forward-up-list (mark)
"Moves mark just past the closing paren of the immediately containing list."
(%list-offset mark t :extra-parens 1))
(defun backward-up-list (mark)
"Moves mark just before the opening paren of the immediately containing list."
(%list-offset mark nil :extra-parens -1))
;;;; Top level form location hacks (open parens beginning lines).
;;; NEIGHBOR-TOP-LEVEL is used only in TOP-LEVEL-OFFSET.
;;;
(eval-when (compile eval)
(defmacro neighbor-top-level (line forwardp)
`(loop
(when (test-char (line-character ,line 0) :lisp-syntax :open-paren)
(return t))
(setf ,line ,(if forwardp `(line-next ,line) `(line-previous ,line)))
(unless ,line (return nil))))
) ;eval-when
(defun top-level-offset (mark offset)
"Go forward or backward offset number of top level forms. Mark is
returned if offset forms exists, otherwise nil."
(declare (fixnum offset))
(let* ((line (mark-line mark))
(at-start (test-char (line-character line 0) :lisp-syntax :open-paren)))
(cond ((zerop offset) mark)
((plusp offset)
(do ((offset (if at-start offset (1- offset))
(1- offset)))
(nil)
(declare (fixnum offset))
(unless (neighbor-top-level line t) (return nil))
(when (zerop offset) (return (line-start mark line)))
(unless (setf line (line-next line)) (return nil))))
(t
(do ((offset (if (and at-start (start-line-p mark))
offset
(1+ offset))
(1+ offset)))
(nil)
(declare (fixnum offset))
(unless (neighbor-top-level line nil) (return nil))
(when (zerop offset) (return (line-start mark line)))
(unless (setf line (line-previous line)) (return nil)))))))
(defun mark-top-level-form (mark1 mark2)
"Moves mark1 and mark2 to the beginning and end of the current or next defun.
Mark1 one is used as a reference. The marks may be altered even if
unsuccessful. if successful, return mark2, else nil."
(let ((winp (cond ((inside-defun-p mark1)
(cond ((not (top-level-offset mark1 -1)) nil)
((not (form-offset (move-mark mark2 mark1) 1)) nil)
(t mark2)))
((start-defun-p mark1)
(form-offset (move-mark mark2 mark1) 1))
((and (top-level-offset (move-mark mark2 mark1) -1)
(start-defun-p mark2)
(form-offset mark2 1)
(same-line-p mark1 mark2))
(form-offset (move-mark mark1 mark2) -1)
mark2)
((top-level-offset mark1 1)
(form-offset (move-mark mark2 mark1) 1)))))
(when winp
(when (blank-after-p mark2) (line-offset mark2 1 0))
mark2)))
(defun inside-defun-p (mark)
"T if the current point is (supposedly) in a top level form."
(with-mark ((m mark))
(when (top-level-offset m -1)
(form-offset m 1)
(mark> m mark))))
(defun start-defun-p (mark)
"Returns t if mark is sitting before an :open-paren at the beginning of a
line."
(and (start-line-p mark)
(test-char (next-character mark) :lisp-syntax :open-paren)))
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
(defmacro %form-offset (mark forwardp)
`(with-mark ((m ,mark))
(when (scan-direction-valid m ,forwardp :lisp-syntax
(or :open-paren :close-paren
:char-quote :string-quote
:constituent))
(ecase (character-attribute :lisp-syntax (direction-char m ,forwardp))
(:open-paren
(when ,(if forwardp `(list-offset m 1) `(mark-before m))
,(unless forwardp
'(scan-direction m nil :lisp-syntax (not :prefix)))
(move-mark ,mark m)
t))
(:close-paren
(when ,(if forwardp `(mark-after m) `(list-offset m -1))
,(unless forwardp
'(scan-direction m nil :lisp-syntax (not :prefix)))
(move-mark ,mark m)
t))
((:constituent :char-quote)
(scan-direction-valid m ,forwardp :lisp-syntax
(not (or :constituent :char-quote)))
,(if forwardp
`(scan-direction-valid m t :lisp-syntax
(not (or :constituent :char-quote)))
`(scan-direction-valid m nil :lisp-syntax
(not (or :constituent :char-quote
:prefix))))
(move-mark ,mark m)
t)
(:string-quote
(cond ((valid-spot m ,(not forwardp))
(neighbor-mark m ,forwardp)
(when (scan-direction-valid m ,forwardp :lisp-syntax
:string-quote)
(neighbor-mark m ,forwardp)
(move-mark ,mark m)
t))
(t (neighbor-mark m ,forwardp)
(move-mark ,mark m)
t)))))))
(defun form-offset (mark offset)
"Move mark offset number of forms, after if positive, before if negative.
Mark is always moved. If there weren't enough forms, returns nil instead of
mark."
(if (plusp offset)
(dotimes (i offset t)
(unless (%form-offset mark t) (return nil)))
(dotimes (i (- offset) t)
(unless (%form-offset mark nil) (return nil)))))
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
(defhvar "Indent Defanything"
"This is the number of special arguments implicitly assumed to be supplied
in calls to functions whose names begin with \"DEF\". If set to NIL, this
feature is disabled."
:value 2)
(defvar *special-forms* (make-hash-table :test #'equal))
(defun defindent (fname args)
"Define Fname to have Args special arguments. If args is null then remove
any special arguments information."
(check-type fname string)
(let ((fname (string-upcase fname)))
(cond ((null args) (remhash fname *special-forms*))
(t
(check-type args integer)
(setf (gethash fname *special-forms*) args)))))
;;; Hemlock forms.
;;;
(defindent "with-mark" 1)
(defindent "with-random-typeout" 1)
(defindent "with-pop-up-display" 1)
(defindent "defhvar" 1)
(defindent "hlet" 1)
(defindent "defcommand" 2)
(defindent "defattribute" 1)
(defindent "command-case" 1)
(defindent "with-input-from-region" 1)
(defindent "with-output-to-mark" 1)
(defindent "with-output-to-window" 1)
(defindent "do-strings" 1)
(defindent "save-for-undo" 1)
(defindent "do-alpha-chars" 1)
(defindent "do-headers-buffers" 1)
(defindent "do-headers-lines" 1)
(defindent "with-headers-mark" 1)
(defindent "frob" 1) ;cover silly FLET and MACROLET names for Rob and Bill.
(defindent "with-writable-buffer" 1)
;;; Common Lisp forms.
;;;
(defindent "block" 1)
(defindent "case" 1)
(defindent "catch" 1)
(defindent "ccase" 1)
(defindent "compiler-let" 1)
(defindent "ctypecase" 1)
(defindent "defconstant" 1)
(defindent "define-setf-method" 2)
(defindent "defmacro" 2)
(defindent "defparameter" 1)
(defindent "defstruct" 1)
(defindent "deftype" 2)
(defindent "defun" 2)
(defindent "defvar" 1)
(defindent "do" 2)
(defindent "do*" 2)
(defindent "do-all-symbols" 1)
(defindent "do-external-symbols" 1)
(defindent "do-symbols" 1)
(defindent "dolist" 1)
(defindent "dotimes" 1)
(defindent "ecase" 1)
(defindent "etypecase" 1)
(defindent "eval-when" 1)
(defindent "flet" 1)
(defindent "labels" 1)
(defindent "lambda" 1)
(defindent "let" 1)
(defindent "let*" 1)
(defindent "loop" 0)
(defindent "macrolet" 1)
(defindent "multiple-value-bind" 2)
(defindent "multiple-value-call" 1)
(defindent "multiple-value-prog1" 1)
(defindent "multiple-value-setq" 1)
(defindent "prog1" 1)
(defindent "progv" 2)
(defindent "progn" 0)
(defindent "typecase" 1)
(defindent "unless" 1)
(defindent "unwind-protect" 1)
(defindent "when" 1)
(defindent "with-input-from-string" 1)
(defindent "with-open-file" 1)
(defindent "with-open-stream" 1)
(defindent "with-output-to-string" 1)
;;; Error/condition system forms.
;;;
(defindent "define-condition" 2)
(defindent "handler-bind" 1)
(defindent "handler-case" 1)
(defindent "restart-bind" 1)
(defindent "restart-case" 1)
(defindent "with-simple-restart" 1)
;;; These are for RESTART-CASE branch formatting.
(defindent "store-value" 1)
(defindent "use-value" 1)
(defindent "muffle-warning" 1)
(defindent "abort" 1)
(defindent "continue" 1)
(defindent "do-debug-function-blocks" 1)
(defindent "di:do-debug-function-blocks" 1)
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
(defindent "do-debug-function-variables" 1)
(defindent "di:do-debug-function-variables" 1)
(defindent "do-debug-block-locations" 1)
(defindent "di:do-debug-block-locations" 1)
;;;
;;; Debug-internals conditions
;;; (define these to make uses of HANDLER-CASE indent branches correctly.)
;;;
(defindent "debug-condition" 1)
(defindent "di:debug-condition" 1)
(defindent "no-debug-info" 1)
(defindent "di:no-debug-info" 1)
(defindent "no-debug-function-returns" 1)
(defindent "di:no-debug-function-returns" 1)
(defindent "no-debug-blocks" 1)
(defindent "di:no-debug-blocks" 1)
(defindent "lambda-list-unavailable" 1)
(defindent "di:lambda-list-unavailable" 1)
(defindent "no-debug-variables" 1)
(defindent "di:no-debug-variables" 1)
(defindent "invalid-value" 1)
(defindent "di:invalid-value" 1)
(defindent "ambiguous-variable-name" 1)
(defindent "di:ambiguous-variable-name" 1)
(defindent "debug-error" 1)
(defindent "di:debug-error" 1)
(defindent "unhandled-condition" 1)
(defindent "di:unhandled-condition" 1)
(defindent "unknown-code-location" 1)
(defindent "di:unknown-code-location" 1)
(defindent "unknown-debug-variable" 1)
(defindent "di:unknown-debug-variable" 1)
(defindent "invalid-control-stack-pointer" 1)
(defindent "di:invalid-control-stack-pointer" 1)
(defindent "frame-function-mismatch" 1)
(defindent "di:frame-function-mismatch" 1)
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
;;; Xlib forms.
;;;
(defindent "with-gcontext" 1)
(defindent "xlib:with-gcontext" 1)
(defindent "with-state" 1)
(defindent "xlib:with-state" 1)
(defindent "with-display" 1)
(defindent "xlib:with-display" 1)
(defindent "with-event-queue" 1)
(defindent "xlib:with-event-queue" 1)
(defindent "with-server-grabbed" 1)
(defindent "xlib:with-server-grabbed" 1)
(defindent "event-case" 1)
(defindent "xlib:event-case" 1)
;;; CLOS forms.
;;;
(defindent "with-slots" 1)
(defindent "with-slots*" 2)
(defindent "with-accessors*" 2)
(defindent "defclass" 2)
;;; System forms.
;;;
(defindent "alien-bind" 1)
(defindent "def-c-record" 1)
(defindent "defrecord" 1)
;;; Wire forms.
(defindent "remote" 1)
(defindent "wire:remote" 1)
(defindent "remote-value" 1)
(defindent "wire:remote-value" 1)
(defindent "remote-value-bind" 3)
(defindent "wire:remote-value-bind" 3)
"Compute number of spaces which mark should be indented according to
local context and lisp grinding conventions. This assumes mark is at the
beginning of the line to be indented."
(unless (backward-up-list m)
(return-from lisp-indentation 0))