From: p
Subject: Indentation
Date: 
Message-ID: <1183302233.661141.260520@k79g2000hse.googlegroups.com>
Hi,
I am working my way through Practical Common Lisp and I have a
questiong about indentation (yeah, I know the most important
subject:P):
In chapter 3 there is code indented like this:
(defun save-db (filename)
  (with-open-file (out filename
                   :direction :output
                   :if-exists :supersede)
    (with-standard-io-syntax
      (print *db* out))))

When I type it into emacs I get this:
(defun save-db (filename)
  (with-open-file (out filename
		     :direction :output
		:if-exists :supersede)
		  (with-standard-io-syntax
		   (print *db* out))))

Based on my knowledge of other languages I would expect something like
this:
(defun save-db (filename)
  (with-open-file (out filename
		       :direction :output
	                   :if-exists :supersede)
		  (with-standard-io-syntax
		   (print *db* out))))

From: p
Subject: Re: Indentation
Date: 
Message-ID: <1183302381.662629.294140@q75g2000hsh.googlegroups.com>
Uh, misstype, let's try again:
Book says:
(defun save-db (filename)
  (with-open-file (out filename
                   :direction :output
                   :if-exists :supersede)
    (with-standard-io-syntax
      (print *db* out))))
Emacs says:
(defun save-db (filename)
  (with-open-file (out filename
		       :direction :output
				  :if-exists :supersede)
		  (with-standard-io-syntax
		   (print *db* out))))


Which one is correct?
From: Pascal Bourguignon
Subject: Re: Indentation
Date: 
Message-ID: <87tzsom8b5.fsf@informatimago.com>
p <······@gmail.com> writes:

> Uh, misstype, let's try again:
> Book says:
> (defun save-db (filename)
>   (with-open-file (out filename
>                    :direction :output
>                    :if-exists :supersede)
>     (with-standard-io-syntax
>       (print *db* out))))
> Emacs says:
> (defun save-db (filename)
>   (with-open-file (out filename
> 		       :direction :output
> 				  :if-exists :supersede)
> 		  (with-standard-io-syntax
> 		   (print *db* out))))
>
>
> Which one is correct?

My emacs says:

(defun save-db (filename)
  (with-open-file (out filename
                       :direction :output
                       :if-exists :supersede)
    (with-standard-io-syntax
      (print *db* out))))

Are you in lisp-mode?


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.
From: p
Subject: Re: Indentation
Date: 
Message-ID: <1183310596.144508.242480@c77g2000hse.googlegroups.com>
On 1 Juli, 17:51, Pascal Bourguignon <····@informatimago.com> wrote:
> My emacs says:
>
> (defun save-db (filename)
>   (with-open-file (out filename
>                        :direction :output
>                        :if-exists :supersede)
>     (with-standard-io-syntax
>       (print *db* out))))
>
> Are you in lisp-mode?
>

Yes, lisp-mode and using slime.
/pavel
From: Kent M Pitman
Subject: Re: Indentation
Date: 
Message-ID: <uabugau11.fsf@nhplace.com>
p <······@gmail.com> writes:

> On 1 Juli, 17:51, Pascal Bourguignon <····@informatimago.com> wrote:
> > My emacs says:
> >
> > (defun save-db (filename)
> >   (with-open-file (out filename

This line is indented correctly because it is inside the body
of the defun.  Bodies get "slight" indentation, two spaces is
conventional.

> >                        :direction :output

This line is indented right because "arguments" if they do not fit all
on one line should be arranged vertically above one another.  
with-open-file's options are not quite a function call with arguments,
but the first argument is treated specially in a manner analogous to 
a function call, so applying the indentation rules of function calling
here seems rationale.

[Some people and programs back-indent this line to be under the "o" or "u"
of "out", but I don't happen to like that unless horizontal space is at
a serious premium (i.e., if the parent form occurs much farther right).]

Having two items on a line seems appropriate because keyword arguments
are often arranged in pairs like this since they act together.  Ordinarily
I would not say to put more than one to a line when you go vertical unless
the sheer length of the list is so long that there is no alternative.
Rules of style are not absolutes--they weigh values.

> >                        :if-exists :supersede)

This line is right because it mirrors the previous line.  If the previous
line had been back-indented to be under "out", this line should match.

> >     (with-standard-io-syntax

This line is right because it is is the body of the with-open-file,
and again is "slight" (2 space) indentation of a body.

> >       (print *db* out))))

Ditto, for the body of with-standard-io-syntax.

Hence, I concur with Pascal's suggestion and reject the other two
suggested earlier.  (Even though it's an issue of personal taste and
there is no canonical answer--I'm telling you that in my mind there is
only one way I'd do it.)
From: Tamas Papp
Subject: Re: Indentation
Date: 
Message-ID: <87wsxkowvn.fsf@pu100877.student.princeton.edu>
p <······@gmail.com> writes:

> On 1 Juli, 17:51, Pascal Bourguignon <····@informatimago.com> wrote:
>> My emacs says:
>>
>> (defun save-db (filename)
>>   (with-open-file (out filename
>>                        :direction :output
>>                        :if-exists :supersede)
>>     (with-standard-io-syntax
>>       (print *db* out))))
>>
>> Are you in lisp-mode?
>>
>
> Yes, lisp-mode and using slime.

Which version?  Slime from deb package 1:20070409-3 indents it as

(defun save-db (filename)
  (with-open-file (out filename
  		   :direction :output
		   :if-exists :supersede)
    (with-standard-io-syntax
      (print *db* out))))

Are you using M-C-q?  Note that if you just keep pressing TAB,
indentation could be incorrect.

Tamas
From: Kaz Kylheku
Subject: Re: Indentation
Date: 
Message-ID: <1183492980.893076.169990@g4g2000hsf.googlegroups.com>
On Jul 1, 8:06 am, p <······@gmail.com> wrote:
> Uh, misstype, let's try again:
> Book says:
> (defun save-db (filename)
>   (with-open-file (out filename
>                    :direction :output
>                    :if-exists :supersede)
>     (with-standard-io-syntax
>       (print *db* out))))
> Emacs says:
> (defun save-db (filename)
>   (with-open-file (out filename
>                        :direction :output
>                                   :if-exists :supersede)
>                   (with-standard-io-syntax
>                    (print *db* out))))
>
> Which one is correct?

The first example is correct. All of the arguments are parallel
elements. There is no subordination among them and so they are placed
at the same indentation level.

Keyword argument values are subordinate to their keywords, so if they
must be split among multiple lines, then indentation can be used:

  (out
   filename
   :direction
       :output
   :if-exists
       :supersede)


The hanging indentation keyed to OUT is wrong:

  (out filename
       :direction ...)

That's because OUT isn't an operator but simply another argument, just
like FILENAME.

There are three basic patterns to remember:

1. Simple function-call-like expression:

   ;; alignment with first argument
   (operator argument argument ...
             argument argument ...)


2. Special syntax with arguments followed by expression body:

  ;; indentation of body
  (operator arguments
    body
    body)

3. List representing data:

  ;; alignment with first item
  (item item item ...
   item item item ...
   item item ...)

Software which indents Lisp has to do some analysis of the syntax to
analyze, or at least guess which of these cases to apply.
From: Alex Mizrahi
Subject: Re: Indentation
Date: 
Message-ID: <4687e9d0$0$90265$14726298@news.sunsite.dk>
(message (Hello 'p)
(you :wrote  :on '(Sun, 01 Jul 2007 15:03:53 -0000))
(

 p> When I type it into emacs I get this:
 p> (defun save-db (filename)
 p>   (with-open-file (out filename
 p>        :direction :output
 p>   :if-exists :supersede)
 p>     (with-standard-io-syntax
 p>      (print *db* out))))

honestly, i do not know how you managed to skrew Emacs indentation :)
but in general, indentation is much better when you add this to you init 
file:

(setq lisp-indent-function 'common-lisp-indent-function)

;(you can also try it in scratch buffer to see the difference

)
(With-best-regards '(Alex Mizrahi) :aka 'killer_storm)
"scorn") 
From: p
Subject: Re: Indentation
Date: 
Message-ID: <1183312862.912280.144670@o61g2000hsh.googlegroups.com>
> honestly, i do not know how you managed to skrew Emacs indentation :)
> but in general, indentation is much better when you add this to you init
> file:
>
> (setq lisp-indent-function 'common-lisp-indent-function)
>
> ;(you can also try it in scratch buffer to see the difference
>
> )
> (With-best-regards '(Alex Mizrahi) :aka 'killer_storm)
> "scorn")

Thanks, that line did the trick.
/pavel