Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split format string of (format t ...)

Tags:

common-lisp

Sometimes I like to output some text with (format t ..).
To prevent long unreadable format-strings in source code, and get the output easily aligned, I use (format t (concatenate 'string ....).

Example:

(format t (concatenate 'string 
                       "some output~%"
                       "  error-msg: ~a~%"
                       "  uiop-cwd: ~a~%"
                       "  uiop-file-exists: ~a~%")
        "error foo"
        (uiop:getcwd)
        (uiop:file-exists-p "hello_world.bmp"))

Is there a more idiomatic and at-compile-time way to do the same in Common Lisp?

like image 636
jue Avatar asked Nov 16 '25 09:11

jue


1 Answers

Here is an equivalent format string that makes use of the Tilde newline format directive, which ignores the following newline and spaces (until the next visible character). In order to indent with spaces as you did, I wrote the forced newline ~% before the spaces:

(format t 
        "some output~
       ~%   error-msg: ~a~
       ~%   uiop-cwd: ~a~
       ~%   uiop-file-exists: ~a~%"
        "error foo"
        (uiop:getcwd)
        (uiop:file-exists-p "hello_world.bmp"))

(NB. This is a single string so there is no concatenation to be done at compile-time.)

like image 197
coredump Avatar answered Nov 19 '25 09:11

coredump



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!