mailing-list for TeXmacs Users

Text archives Help


Re: [TeXmacs] Scheme string to tm tree conversion


Chronological Thread 
  • From: Henri Lesourd <address@hidden>
  • To: Lionel Elie Mamane <address@hidden>
  • Cc: address@hidden
  • Subject: Re: [TeXmacs] Scheme string to tm tree conversion
  • Date: Tue, 28 Feb 2006 21:35:25 +0100

Lionel Elie Mamane wrote:

Hi,

What is the scheme function foo such that (foo s) is the TeXmacs tree
(document snippet) whose rendering is s? I tried string->tree, but
this _interprets_ the string. verbatim->tree doesn't do what I want
either. I grepped the source for "string->" and "->tree" without
success.

In particular:

- (foo "<gtr>") is the tree whose .tm serialisation is:
\<less\>gtr\<gtr\> .


Uh, that is not clear. As for me, I get :
[[
(string->tree "<gtr>") or
(string->tree ">")
=>
'>'
]]

isn't it what you want ? Or rather would you like to get :
[[
(string->tree ">")
=>
'\<gtr\>'
]]

In my own case, I got fed up with these escaping problems
in the strings, thus I finally decided to do my escapings
myself. For this, you just need convenient Scheme routines
to rewrite strings.

The following ones work well for me :
[[
(define-macro (foreach-number i . b)
`(do ((,(car i) ,(cadr i)
(,(if (memq (caddr i) '(> >=)) '- '+) ,(car i) 1))
)
((,(if (eq? (caddr i) '>)
'<=
(if (eq? (caddr i) '<)
'>=
(if (eq? (caddr i) '>=) '< '>)))
,(car i) ,(cadddr i)
)
,(car i)
)
,(cons 'begin b))
)
(define (substring? s s0)
(define b #f)
(let ((l (string-length s))
(l0 (string-length s0))
)
(foreach-number (i 0 < l0)
(if (equal? s (substring s0 i (min (+ i l) l0)))
(set! b i)))
)
b
)
(define (substring! s rs s0)
(define r s0)
(let ((l (string-length s))
(l0 (string-length s0))
)
(foreach-number (i 0 < l0)
(if (equal? s (substring s0 i (min (+ i l) l0)))
(let ((s2 (string-append
(substring s0 0 i)
rs
(substring! s rs (substring s0 (min (+ i l) l0) l0))
)))
(begin
(set! i l0)
(set! r s2)))))
)
r
)
;;;;;;;;;;;;;;;;;;;; TEST ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;(display* (substring? "<->" "abc<->defg") "\n")
;(display* (substring? "<->" "<-") "\n")
;(display* (substring! "<->" "(=)" "abc<->defg") "\n")
]]

You also need (display*), so if you don't try the code above
under TeXmacs, the following Scheme chunk will solve the problem :
[[
(define-macro (mapfunc f l) `(map ,f ,l))
(define-macro (display* . l)
"Display all objects in @l."
`(begin .
,(mapfunc (lambda (x) (list 'display x)) l)))

]]


Using the routines above, if you want to escape a string, write :
[[
(set! s (substring! "<" "\<less\" s)) ; starting escaping for '<'
(set! s (substring! ">" "\<gtr\>" s)) ; escaping '>'
(set! s (substring! "\<less\" "\<less\>" s)) ; finishing escaping for '<'
]]

Now if you want to unescape a string, write :
[[
(set! s (substring! "\<less\>" "<" s)) ; unescaping '<'
(set! s (substring! "\<gtr\>" ">" s)) ; unescaping '>'
]]

As soon as you don't need too much speed, using this
technique works pretty well, the especially nice point
being that you know *exactly* how the processing is done.


Best, Henri




Archive powered by MHonArc 2.6.19.

Top of page