lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: output some string


From: Gianmaria Lari
Subject: Re: output some string
Date: Sat, 16 Jun 2018 16:56:52 +0200



On Fri, 15 Jun 2018 at 22:47, Thomas Morley <address@hidden> wrote:
2018-06-15 20:11 GMT+02:00 Aaron Hill <address@hidden>:
> On 2018-06-15 09:40, Gianmaria Lari wrote:
>>
>> I'm trying to write some code in scheme. At the moment I do it using
>> frescobaldi and I need some help for the output.
>> The following code
>>
>> \version "2.19.81"
>> #(define (f w) w)
>> $(f "Hello")
>>
>>
>> generates a pdf file containing "Hello".
>>
>> If my function f is called passing something that is not a string like in
>> the following code
>>
>> \version "2.19.81"
>> #(define (f w) w)
>> $(f '(a b c))
>>
>>
>> it doesn't work. How can I convert my data to a string?
>>
>> Thank you, g.
>> P.S. I have been able to write in the Lilypond log using  #(define (f w)
>> (write w))...
>
>
> You might want to look at the `format` function:
>
> https://www.gnu.org/software/guile/manual/html_node/Formatted-Output.html

`format` is surely pretty flexible.
The link points to the guile-2.2.3-manual, though regarding `format` I
don't think there are many differences to guile-1.8.
Other things differ substantially.

>
> This gives you quite a few options for formatting data and getting a string.
>
> `pretty-print` can also be useful if you want to dump an arbitrary
> _expression_ to the output.  You have to jump through a few hoops, though, to
> get its output as a string:
>
> %%%%
>   \version "2.19.81"
>   #(define (pretty-print-to-string arg)
>     (let ((o (open-output-string)) (s ""))
>       (pretty-print arg o)
>       (set! s (get-output-string o))
>       (close-output-port o)
>       s))
>   $(pretty-print-to-string '(a b #(c d e) f . g))
> %%%%

No need for this.
Afaik, default `object->string' does this already. It also takes an
optional argument for the printing-procedure.

With 2.19.81 in scheme-sandbox:

(object->string #{ cis1 #} display-scheme-music)
=>
"(make-music
  'NoteEvent
  'duration
  (ly:make-duration 0)
  'pitch
  (ly:make-pitch -1 0 1/2))

"

(object->string #{ cis1 #} display-lily-music)
=>
"cis1
"

(object->string (ly:make-pitch 0 0 0))
=>
"#<Pitch c' >"


If a list contains only strings it's also possible to do:

(string-concatenate '("foo" "bar" "buzz"))
=>
"foobarbuzz"


Cheers,
  Harm

Thank Harm!
My problem was to find a way to display, in the music view of frescobaldi, the result of simple function returning "simple" types: numbers, string, list. 
Your suggestion,  "object->string" looks the simplest way.

I write here my test and my personal note, maybe other can take advantage of it. If you find some error please correct them. Thanks a lot, g.

Example code displaying simple type in the music view of frescobaldi

\version "2.19.81"
$(object->string 3)
$(object->string 3.48)
$(object->string #f)
$(object->string "Test")
$(object->string '(ciccio lippo))

Result:

image.png

Should be noted that, 

$(object->string 3)

is equivalent to

$(object->string 3 display) 

* * *

Some examples displaying a lilypond type, cis1. Please note the code is always the same except the optional argument.

\version "2.19.81"
$(object->string #{ cis1 #}) %no optional argument
image.png
(the output in the previous example is cut on the right!!)

\version "2.19.81"
$(object->string #{ cis1 #} display-scheme-music)
image.png

\version "2.19.81"
$(object->string #{ cis1 #} display-lily-music)
image.png

reply via email to

[Prev in Thread] Current Thread [Next in Thread]