ab4a7458c22c

Fix lispindent to indent flet/labels right so I don't lose my goddamn mind

This code is an incomprehensible hellscape so this probably breaks something
somewhere but if I have to spend another moment without correctly-indented flets
and labelses I'm gonna jump out a fucking window.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Fri, 25 Mar 2016 21:34:05 +0000
parents 97e7e6bce784
children d4ff64dea170
branches/tags (none)
files bin/lispindent bin/lispindent.lisp bin/lispindentclean

Changes

--- a/bin/lispindent	Fri Mar 25 21:32:06 2016 +0000
+++ b/bin/lispindent	Fri Mar 25 21:34:05 2016 +0000
@@ -98,22 +98,44 @@
 ;(trace lisp-indent-number literal-token-p read-from-string past-next-token)
 
 (defstruct lparen
+  word
   spaces-before
   num-aligned-subforms
-  (num-finished-subforms 0))
+  (num-finished-subforms 0)
+  (num-processed-subforms 0))
+
+(defun current-word (s i n)
+  (let ((j (past-next-token s i n)))
+    (when (not (= i j))
+      (subseq s i j))))
+
+(defvar *labels-keywords* '(labels flet))
 
-(defun calc-subindent (s i n)
+(defun in-labels-p (stack w)
+  ; (format t "~%")
+  ; (format t "WORD: ~S~%" w)
+  ; (format t "STACK: ~S~%" stack)
+  (let ((target (cadr stack)))
+    ; (format t "TARGET: ~S~%" target)
+    (and target
+         (member (lparen-word target) *labels-keywords*
+                 :key #'symbol-name :test #'string-equal)
+         (= (lparen-num-processed-subforms target) 0))))
+
+(defun calc-subindent (stack s i n)
   (let* ((j (past-next-token s i n))
          (num-aligned-subforms 0)
          (left-indent
           (if (= j i) 1
             (let ((w (subseq s i j)))
               (if (and (>= i 2) (member (char s (- i 2)) '(#\' #\`))) 1
-                (let ((nas (lisp-indent-number w)))
+                (let ((nas (if (in-labels-p stack w)
+                             1
+                             (lisp-indent-number w))))
                   (cond ((>= nas 0) (setq num-aligned-subforms nas)
                          2)
                         ((literal-token-p w) 1)
-                        ((= j n) 1)
+                        ((= j n) 2)
                         (t (+ (- j i) 2)))))))))
     (values left-indent num-aligned-subforms (1- j))))
 
@@ -130,6 +152,7 @@
 (defun string-trim-blanks (s)
   (string-trim '(#\space #\tab #\newline #\return) s))
 
+
 (defun indent-lines ()
   (let ((left-i 0) (paren-stack '()) (stringp nil))
     (loop
@@ -151,6 +174,7 @@
                             extra-w))))))
         (setq curr-line (string-trim-blanks curr-line))
         (dotimes (k curr-left-i) (write-char #\space))
+        ; (princ paren-stack)
         (princ curr-line) (terpri)
         ;
         (let ((i 0) (n (length curr-line)) (escapep nil)
@@ -172,16 +196,20 @@
                     ((member c '(#\( #\[) :test #'char=)
                      (setq inter-word-space-p nil)
                      (multiple-value-bind (left-indent num-aligned-subforms j)
-                         (calc-subindent curr-line (1+ i) n)
+                         (calc-subindent paren-stack curr-line (1+ i) n)
                        (push
-                        (make-lparen :spaces-before (+ i curr-left-i left-indent)
+                        (make-lparen :word (current-word curr-line (1+ i) n)
+                                     :spaces-before (+ i curr-left-i left-indent)
                                      :num-aligned-subforms num-aligned-subforms)
                         paren-stack)
                        (setq i j)))
                     ((member c '(#\) #\]) :test #'char=)
                      (setq inter-word-space-p nil)
                      (cond (paren-stack (pop paren-stack))
-                           (t (setq left-i 0))))
+                           (t (setq left-i 0)))
+                     (let ((lp (car paren-stack)))
+                       (when lp
+                         (incf (lparen-num-processed-subforms lp)))))
                     (t (setq inter-word-space-p nil)))
               (incf i))))))))
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/lispindent.lisp	Fri Mar 25 21:34:05 2016 +0000
@@ -0,0 +1,1 @@
+./lispindent
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/lispindentclean	Fri Mar 25 21:34:05 2016 +0000
@@ -0,0 +1,3 @@
+#!/usr/bin/env bash
+
+LISP=sbcl lispindent | sed -e 's/ *$//'