-
Notifications
You must be signed in to change notification settings - Fork 11
/
lispindent.lisp
executable file
·196 lines (179 loc) · 7.71 KB
/
lispindent.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
":";if test -z "$LISP"; then LISP=ecl; fi
":";if test "$LISP" = clasp; then exec clasp --script $0
":";elif test "$LISP" = clisp; then exec clisp -q $0
":";elif test "$LISP" = clozure; then exec ccl -b -Q -l $0
":";elif test "$LISP" = ecl; then exec ecl -shell $0
":";elif test "$LISP" = sbcl; then exec sbcl --script $0
":";fi
;Dorai Sitaram
;Oct 8, 1999
;last change 2022-07-05
;this script takes lines of Lisp or Scheme code from its
;stdin and produces an indented version thereof on its
;stdout
(defvar *lisp-keywords* '())
(defun set-lisp-indent-number (sym num-of-subforms-to-be-indented-wide)
(declare (symbol sym) (integer num-of-subforms-to-be-indented-wide))
(let* ((x (symbol-name sym))
(c (assoc x *lisp-keywords* :test #'string-equal)))
(unless c
(push (setq c (cons x nil)) *lisp-keywords*))
(setf (cdr c) num-of-subforms-to-be-indented-wide)))
(defun retrieve-env (s)
(declare (string s))
#+(or abcl clasp clisp ecl) (ext:getenv s)
#+allegro (sys:getenv s)
#+clozure (ccl:getenv s)
#+cmucl (cdr (assoc (intern s :keyword)
ext:*environment-list* :test #'string=))
#+mkcl (mkcl:getenv s)
#+sbcl (sb-ext:posix-getenv s))
(defun read-home-lispwords ()
(with-open-file (i (or (retrieve-env "LISPWORDS")
(merge-pathnames ".lispwords" (user-homedir-pathname)))
:if-does-not-exist nil)
(when i
(loop
(let* ((w (or (read i nil) (return)))
(a w))
(unless (atom w)
(setq a (car w)))
(cond ((atom w)
(set-lisp-indent-number a 0))
((numberp a)
(mapc (lambda (x) (set-lisp-indent-number x a)) (cdr w)))
((consp a)
(let ((n (cadr w)))
(mapc (lambda (x) (set-lisp-indent-number x n)) a)))
(t (set-lisp-indent-number a (cadr w)))))))))
(defun past-next-atom (s i n)
(declare (string s) (integer i) (integer n))
(loop
(when (>= i n) (return n))
(let ((c (char s i)))
(cond ((char= c #\\) (incf i))
((member c '(#\space #\tab #\( #\) #\[ #\] #\" #\' #\` #\, #\;))
(return i))))
(incf i)))
(defun get-lisp-indent-number (s)
(declare (symbol s) (symbol raw-symbol-p))
(labels ((get-lisp-indent-number
(s &key (raw-symbol-p nil))
(or (cdr (assoc s *lisp-keywords* :test #'string-equal))
(cond ((eql (search "def" s :test #'char-equal) 0) 0)
(raw-symbol-p -1)
(t (let ((p (position #\: s :from-end t)))
(if p
(get-lisp-indent-number (subseq s (1+ p)) :raw-symbol-p t)
-1)))))))
(get-lisp-indent-number s)))
(defun literal-token-p (s)
(declare (string s))
(let ((colon-pos (position #\: s)))
(if colon-pos (= colon-pos 0)
(let ((s (read-from-string s)))
(or (characterp s) (numberp s) (stringp s))))))
(defstruct lparen
spaces-before
lisp-indent-num
num-finished-subforms)
(defun calc-subindent (s i n)
(declare (string s) (integer i) (integer n))
(let* ((j (past-next-atom s i n))
(lisp-indent-num 0)
(delta-indent
(if (= j i) 0
(let ((w (subseq s i j)))
(if (or (and (>= i 2) (member (char s (- i 2)) '(#\' #\`)))
(literal-token-p w)) 0
(progn (setq lisp-indent-num (get-lisp-indent-number w))
(case lisp-indent-num
(-2 0)
(-1 (if (< j n) (1+ (- j i)) 1))
(t 1))))))))
(values delta-indent lisp-indent-num j)))
(defun num-leading-spaces (s)
(declare (string s))
(let ((n (length s))
(i 0) (j 0))
(loop
(when (>= i n) (return 0))
(case (char s i)
(#\space (incf i) (incf j))
(#\tab (incf i) (incf j 8))
(t (return j))))))
(defun string-trim-blanks (s)
(declare (string s))
(string-trim '(#\space #\tab #\newline #\return) s))
(defun indent-lines ()
(let ((default-left-i -1) (left-i 0) (paren-stack '()) (inside-stringp nil))
(loop
(let* ((curr-line (or (read-line nil nil) (return)))
(leading-spaces (num-leading-spaces curr-line))
(curr-left-i
(cond (inside-stringp leading-spaces)
((null paren-stack)
(when (= left-i 0)
(when (= default-left-i -1)
(setq default-left-i leading-spaces))
(setq left-i default-left-i))
left-i)
(t (let* ((lp (car paren-stack))
(nas (lparen-lisp-indent-num lp))
(nfs (lparen-num-finished-subforms lp))
(extra-w 0))
(when (< nfs nas) ;(and (>= nas 0) (< nfs nas))
(incf (lparen-num-finished-subforms lp))
(setq extra-w 2))
(+ (lparen-spaces-before lp)
extra-w))))))
(setq curr-line (string-trim-blanks curr-line))
(unless (string= curr-line "")
(dotimes (k curr-left-i) (write-char #\space))
(princ curr-line))
(terpri)
;
(let ((i 0) (n (length curr-line)) (escapep nil)
(token-interstice-p nil))
(flet ((incr-finished-subforms ()
(unless token-interstice-p
(when paren-stack
(incf (lparen-num-finished-subforms
(car paren-stack))))
(setq token-interstice-p t))))
;
(loop
(when (>= i n) (return))
(let ((c (char curr-line i)))
(cond (escapep (setq escapep nil))
((char= c #\\) (setq token-interstice-p nil escapep t))
(inside-stringp (when (char= c #\")
(setq inside-stringp nil)
(incr-finished-subforms)))
((char= c #\;) (incr-finished-subforms) (return))
((char= c #\") (incr-finished-subforms) (setq inside-stringp t))
((member c '(#\space #\tab) :test #'char=)
(incr-finished-subforms))
((member c '(#\( #\[) :test #'char=)
(incr-finished-subforms)
(multiple-value-bind (delta-indent lisp-indent-num j)
(calc-subindent curr-line (1+ i) n)
(push (make-lparen :spaces-before (+ 1 i curr-left-i delta-indent)
:lisp-indent-num lisp-indent-num
:num-finished-subforms -1)
paren-stack)
(setq token-interstice-p t)
(let ((inext (1+ i)))
(when (> j inext)
(setq inext j token-interstice-p nil))
(setq i (1- inext)))))
((member c '(#\) #\]) :test #'char=)
(setq token-interstice-p nil)
(cond (paren-stack (pop paren-stack))
(t (setq left-i 0))))
(t (setq token-interstice-p nil))))
(incf i))
(incr-finished-subforms)))))))
(read-home-lispwords)
(indent-lines)
;eof