all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Lars Magne Ingebrigtsen <larsi@gnus.org>
To: emacs-devel@gnu.org
Subject: Common Lisp indentation bug fix/new feature
Date: Mon, 21 Nov 2011 23:26:56 +0100	[thread overview]
Message-ID: <m3sjlhw1qn.fsf@stories.gnus.org> (raw)

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/




             reply	other threads:[~2011-11-21 22:26 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-21 22:26 Lars Magne Ingebrigtsen [this message]
2011-11-22 14:57 ` Common Lisp indentation bug fix/new feature 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m3sjlhw1qn.fsf@stories.gnus.org \
    --to=larsi@gnus.org \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.