From: basman
Subject: Indentation with print-object
Date: 
Message-ID: <1132712768.721321.208930@z14g2000cwz.googlegroups.com>
So I have various objects floating around with print-object methods
defined.  Often, if an object foo has some slot bar, then foo's
print-object method will call bar's print object method.  To improve
readability when printing deeply nested objects, I would like to make
it so that the description of an object is indented depending on how
deeply nested it is.  So if foo has a slot bar which has a slot baz,
maybe I want foo's information to be printed with no indentation, bar's
info to be printed with indentation 2, and baz's information to be
printed with indentation 4.  Anyone know a clean (and portable) way to
do this?
- Bhaskara

From: Brian Downing
Subject: Re: Indentation with print-object
Date: 
Message-ID: <BCTgf.593568$xm3.214454@attbi_s21>
In article <························@z14g2000cwz.googlegroups.com>,
basman <········@gmail.com> wrote:
> So I have various objects floating around with print-object methods
> defined.  Often, if an object foo has some slot bar, then foo's
> print-object method will call bar's print object method.  To improve
> readability when printing deeply nested objects, I would like to make
> it so that the description of an object is indented depending on how
> deeply nested it is.  So if foo has a slot bar which has a slot baz,
> maybe I want foo's information to be printed with no indentation, bar's
> info to be printed with indentation 2, and baz's information to be
> printed with indentation 4.  Anyone know a clean (and portable) way to
> do this?

The pretty printer is one obvious way:

(defun test (stream)
  (format stream "indented: ")
  (pprint-logical-block (stream nil :per-line-prefix "")
    (princ "this is a
block of text
that should be indented
" stream)
    (format stream "and... ")
    (pprint-logical-block (stream nil :per-line-prefix "!! ")
      (format stream "here is~%some more!"))))

CL-USER> (test *standard-output*)
indented: this is a
          block of text
          that should be indented
          and... !! here is
                 !! some more!

Note that without :PER-LINE-PREFIX, even if it's "", newlines will be
treated literally (i.e. they will go all the way to the left instead
of to the left of the block) unless you use PPRINT-NEWLINE.

However, I usually wind up having a lot of trouble getting the pretty
printer to do what I want.  Nevertheless it can be handy.

-bcd
-- 
*** Brian Downing <bdowning at lavos dot net> 
From: Adrian Kubala
Subject: Re: Indentation with print-object
Date: 
Message-ID: <slrndo92kp.uhf.adrian-news@sixfingeredman.net>
On 2005-11-23, basman <········@gmail.com> wrote:
> To improve readability when printing deeply nested objects, I would
> like to make it so that the description of an object is indented
> depending on how deeply nested it is.

See section "22.3.5.2 Tilde Less-Than-Sign: Logical Block" in the
Hyperspec. Here's a simple example:

(format stream "#<·@<MY_OBJECT :key1 ~w :key2 ~w~:>>"
        (key1 object) (key2 object))
From: basman
Subject: Re: Indentation with print-object
Date: 
Message-ID: <1132859962.045143.210640@g44g2000cwa.googlegroups.com>
Hmm ok.  A more general question : until now I haven't bothered to try
to understand pretty-printing.  Instead I have just defined
print-object methods for the various classes in my code, which
occasionally has the annoying side effect of dumping large printed
representations to i/o when i'm examining the stack or something.  Is
this what people usually do, or is the convention that print-object is
used to print concise representations, and the pretty printer is used
to dispatch to more readable representations?
- Bhaskara


Adrian Kubala wrote:
> On 2005-11-23, basman <········@gmail.com> wrote:
> > To improve readability when printing deeply nested objects, I would
> > like to make it so that the description of an object is indented
> > depending on how deeply nested it is.
>
> See section "22.3.5.2 Tilde Less-Than-Sign: Logical Block" in the
> Hyperspec. Here's a simple example:
>
> (format stream "#<·@<MY_OBJECT :key1 ~w :key2 ~w~:>>"
>         (key1 object) (key2 object))
From: Stephen Compall
Subject: Re: Indentation with print-object
Date: 
Message-ID: <pan.2005.11.24.21.16.49.876795@nocandysw.com>
On Thu, 24 Nov 2005 11:19:22 -0800, basman wrote:

> Hmm ok.  A more general question : until now I haven't bothered to try
> to understand pretty-printing.  Instead I have just defined
> print-object methods for the various classes in my code, which
> occasionally has the annoying side effect of dumping large printed
> representations to i/o when i'm examining the stack or something.  Is
> this what people usually do, or is the convention that print-object is
> used to print concise representations, and the pretty printer is used
> to dispatch to more readable representations?

From the define-condition CLHS entry, and followup reading of the
*print-escape* entry, I got the distinct impression that this is the
expected semantic:

(defmethod print-object ((self your-class) stream)
  (if *print-escape*
    (call-next-method)         ;does the default #<bla bla> thing
    #|your fancy printing code here|#))

You could also print a `read'able representation of your object, if you
like, in the first case.

-- 
Stephen Compall
Email: My username is s11.  My domain is member..org, but insert the
abbreviation for `Free Software Foundation' between the dots.