Re: format indentation



On 06/07/2012 12:32 PM, Bigos wrote:
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.

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
.



Relevant Pages

  • Re: PEP 8 and extraneous whitespace
    ... string literals needing run-time adjustments (like stripping ... indentation from triple-quoted strings - it usually depends on the ... whitespice to get it to line up or wrap in your editor, ... This does involve inserting extraneous whitespace, ...
    (comp.lang.python)
  • Re: PEP 8 and extraneous whitespace
    ... INDENTATION, which is a very specific part of formatting involving ... whitespace at the beginning of a line. ... What I mean is that these string literals are broken across multiple ...
    (comp.lang.python)
  • Re: Creating an XML document ?
    ... Is there any way that this can be done using a macro. ... runtime code that "prints" the tabs one way or another. ... where make_tabs returns a string of tabs. ... can set a maximum on your indentation level. ...
    (comp.lang.c)
  • Re: new commands written in perl
    ... > use strict; ... interpolate a variable in your string. ... if you decided to exit afterwards, and put an 'exit 1' at the end of ... Also, I just noticed-- fix your indentation, or lack thereof. ...
    (comp.lang.perl.misc)
  • Re: Creating an XML document ?
    ... Is there any way that this can be done using a macro. ... runtime code that "prints" the tabs one way or another. ... where make_tabs returns a string of tabs. ... can set a maximum on your indentation level. ...
    (comp.lang.c)