unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Common Lisp indentation bug fix/new feature
@ 2011-11-21 22:26 Lars Magne Ingebrigtsen
  2011-11-22 14:57 ` Stefan Monnier
  0 siblings, 1 reply; 11+ messages in thread
From: Lars Magne Ingebrigtsen @ 2011-11-21 22:26 UTC (permalink / raw)
  To: emacs-devel

This is how much (most? some? I haven't actually counted) Common Lisp
code found out in the world looks like:

(defun foo ()
  (loop for z from 1 upto 5
        do (princ z)
           (terpri)
           (terpri)))

That is, the statements after DO/DOING/FINALLY/etc line up so that you
can see that they are part of the same block of code.

Unfortunately, the CL indentation code does this instead:

  (loop for z from 1 upto 5
        do (princ z)
          (terpri)
          (terpri)

Now, one can adjust the indentation with `lisp-loop-forms-indentation',
so I could just increase it by 1 and things would be OK?  Nope, since 
then FINALLY would look like:

  (loop for z from 1 upto 5
        finally (princ z)
           (terpri)
           (terpri)

With the following patch (and a defcustom adjustment), we would allow
getting the desired behaviour.

The defaults don't change, though, so this wouldn't be a user-visible
change, unless the set `lisp-loop-forms-indentation' to nil.

So would it be OK to apply this, or is it too new-featurish?  I kinda
think it fixes an indentation bug...

=== modified file 'lisp/emacs-lisp/cl-indent.el'
--- lisp/emacs-lisp/cl-indent.el	2011-11-21 21:58:38 +0000
+++ lisp/emacs-lisp/cl-indent.el	2011-11-21 22:15:28 +0000
@@ -152,11 +152,26 @@
     (error t)))
 
 
+(defun common-lisp-loop-keyword-length (loop-start)
+  "Return the length of the preceding loop keyword.
+Stop looking before LOOP-START."
+  (save-excursion
+    (let ((length 0))
+      (while (and (zerop length)
+		  (> (point) loop-start))
+	(beginning-of-line)
+	(when (looking-at "^\\s-*\\(loop\\s-*\\)?\\(:?\\sw+\\|;\\)")
+	  (setq length (length (match-string 2))))
+	(forward-line -1))
+      length)))
+
+
 (defun common-lisp-loop-part-indentation (indent-point state)
   "Compute the indentation of loop form constituents."
-  (let* ((loop-indentation (save-excursion
-			     (goto-char (elt state 1))
-			     (current-column))))
+  (let ((loop-indentation (save-excursion
+			    (goto-char (elt state 1))
+			    (current-column)))
+	(case-fold-search t))
     (goto-char indent-point)
     (beginning-of-line)
     (list
@@ -165,7 +180,13 @@
 	   ((looking-at "^\\s-*\\(:?\\sw+\\|;\\)")
 	    (+ loop-indentation lisp-loop-keyword-indentation))
 	   (t
-	    (+ loop-indentation lisp-loop-forms-indentation)))
+	    (+ loop-indentation
+	       lisp-loop-keyword-indentation
+	       (or lisp-loop-forms-indentation
+		   (1+ (common-lisp-loop-keyword-length
+			(or (save-excursion
+			      (re-search-backward "(\\s-*loop" nil t))
+			    indent-point)))))))
      ;; Tell the caller that the next line needs recomputation, even
      ;; though it doesn't start a sexp.
      loop-indentation)))



-- 
(domestic pets only, the antidote for overdose, milk.)
  bloggy blog http://lars.ingebrigtsen.no/




^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2012-04-03 14:53 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-11-21 22:26 Common Lisp indentation bug fix/new feature Lars Magne Ingebrigtsen
2011-11-22 14:57 ` Stefan Monnier
2011-11-22 15:35   ` Lars Magne Ingebrigtsen
2011-11-22 15:52     ` Lawrence Mitchell
2011-11-22 16:43     ` Stefan Monnier
2011-11-22 18:09       ` Lars Magne Ingebrigtsen
2011-11-22 22:28         ` Tim Cross
2012-03-31 10:21         ` Nikodemus Siivola
2012-04-03  3:47           ` Lars Magne Ingebrigtsen
2012-04-03 10:13             ` Nikodemus Siivola
2012-04-03 14:53               ` Stefan Monnier

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).