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