Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

format indentation

39 views
Skip to first unread message

Bigos

unread,
Jun 7, 2012, 12:32:23 PM6/7/12
to
Hi,

I need to print several indented lines.

This is my present solution:

(setq r (concatenate 'string
r
(format nil "~2&~A~A~%~A#~%~Aelse~%~A#~%~Aend" (spaces 4) (fourth e)
(spaces 6) (spaces 4) (spaces 6) (spaces 4) )))


It looks appalling and I wonder if there's better way to do it. I think
having 5 (spaces n) looks horrible. I couldn't find anything on
Hyperspec of Google.

Jack

Alberto Riva

unread,
Jun 7, 2012, 12:52:47 PM6/7/12
to
In a case like this you can use the ~T directive:

(format nil "~2&~4T~A~%~6T#~%~4Telse~%~6T#~%~4Tend" (fourth e))

If the amount of indentation is determined at runtime, you can use ~VT:

(format nil "~VT~a" 5 "test")

=> " test"

Note that ~xT means "move to position x from the start of the line", not
"insert x spaces", that's why I said that this works in the example you
provided, ie to indent strings from the left margin.

Also, instead of (concatenate 'string r (format nil "...")) I would add
a ~a at the beginning of the format string, and simply use

(format nil "~a..." r other-arguments...)

Alberto

Bigos

unread,
Jun 7, 2012, 2:30:22 PM6/7/12
to
On 07/06/12 17:52, Alberto Riva wrote:
>
> In a case like this you can use the ~T directive:
>
> (format nil "~2&~4T~A~%~6T#~%~4Telse~%~6T#~%~4Tend" (fourth e))
>
> If the amount of indentation is determined at runtime, you can use ~VT:
>
> (format nil "~VT~a" 5 "test")
>
> => " test"
>
> Note that ~xT means "move to position x from the start of the line", not
> "insert x spaces", that's why I said that this works in the example you
> provided, ie to indent strings from the left margin.
>
> Also, instead of (concatenate 'string r (format nil "...")) I would add
> a ~a at the beginning of the format string, and simply use
>
> (format nil "~a..." r other-arguments...)
>
> Alberto

Thank you, it works.

Jack

Elias Mårtenson

unread,
Jun 7, 2012, 9:32:43 PM6/7/12
to
I'd use the @ modifier for the A directive.

If you want to print "else" prefixed by 4 spaces, you can do this:

(format t "~,,v@a" 4 "else")

This solution is different from Alberto's ~T suggestion, since this one inserts a given number of spaces while his aligns to a specific column.

Elias Mårtenson

unread,
Jun 7, 2012, 9:35:13 PM6/7/12
to
On Friday, 8 June 2012 09:32:43 UTC+8, Elias Mårtenson wrote:

> If you want to print "else" prefixed by 4 spaces, you can do this:
>
> (format t "~,,v@a" 4 "else")

Just to clarify, you can of course use "~,,4@a" if you don't need to pass the 4 as a computed argument.

Bigos

unread,
Jun 8, 2012, 3:39:21 AM6/8/12
to
Thank you so much, yesterday I was going in circles looking for that.


Marco Antoniotti

unread,
Jun 8, 2012, 9:48:57 AM6/8/12
to
On Friday, June 8, 2012 9:39:21 AM UTC+2, Bigos wrote:
> On 08/06/12 02:35, Elias M�rtenson wrote:
> > On Friday, 8 June 2012 09:32:43 UTC+8, Elias M�rtenson wrote:
> >
> >> If you want to print "else" prefixed by 4 spaces, you can do this:
> >>
> >> (format t "~,,v@a" 4 "else")
> >
> > Just to clarify, you can of course use "~,,4@a" if you don't need to pass the 4 as a computed argument.
>
> Thank you so much, yesterday I was going in circles looking for that.

If you want to "print" something that is internally represented as CL data structures, then the Pretty Printer is your friend. Not a friend easy to get along with, but definitively a friend.

MA
0 new messages