[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Texmacs-dev] Scheme to Prolog
From: |
Henri Lesourd |
Subject: |
Re: [Texmacs-dev] Scheme to Prolog |
Date: |
Wed, 14 Feb 2007 13:29:50 +0100 |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030821 |
Michael Klein wrote:
Hello List,
I'am trying to write an extension which uses Prolog. At the moment I
want to understand how I can access the active Document in Scheme. I
found the-buffer in a discussion. I tried to use it but got an unbound
variable error. Is there a module I need to load? Can anybody give me
a hint how to use tree_to_texmacs? What I need is to transform the
tree to a Prolog representation without losing the information on the
origanl tree, so that I can send back editing commands to texmacs.
The basic tree primitives you can use for accessing trees in TeXmacs are :
:: (path->tree PATH) -> TREE ;; (tree->path TREE) -> PATH
:: (tree-label TREE) -> SYMBOL
:: (tree-arity TREE) -> INTEGER
:: (tree-ref TREE INTEGER) -> TREE
:: (tree-set! TREE INTEGER TREE) -> VOID
:: (cursor-path)
Explanation : trees in the document are characterized by their *path*,
which is a list of integers that tells exactly how to descend from the
root of all the trees inside TeXmacs to the tree you want.
For example, given the following tree :
<<
(+ a (* b c))
>>
path("(+ a ...)") == ()
path("a") == (0)
path("(* b c)") == (1)
path("b") == (1 0)
path("c") == (1 1)
The tags of the tree nodes (i.e., the first symbol,
like "+", "*" in the example above) have no paths,
one can access them by means of (tree-label ...)
One can fetch the tree corresponding to a given
path by means of (path->tree ...), and get the
path of a tree by means of (tree->path ...).
It is important to know that all the documents
inside TeXmacs are one tree as a whole, and
that the paths (0), (1), (2), ... are the paths of
each one of the different buffers in TeXmacs.
Currently, "(path->tree '())" gives you a tuple
with contains all the buffers (i.e., the "root
of all the trees"), therefore, you don't need
to use redundant functions like (the-buffer)
or anything like that, just get the root tree
and access it the way you want by means
of (tree-ref ...).
To know the current buffer, just use "(car (cursor-path))".
The routines (tree-label ...), (tree-arity ...), (tree-set ...)
and (tree-ref ...) are the classical set of routines one
can use to traverse recursive vector-built datastructures.
Attached to this very email, you will find a simple Scheme
program to translate a TeXmacs tree to XML notation.
It should be very easy to transform this code to fit your
purpose of translating a TeXmacs tree to a set of Prolog
tuples.
From inside a Scheme session, load this file by means
of :
<<
(load "xmlize-tree.scm")
>>
, and then try :
<<
(xmlize-tree (path->tree '(0)) 0)
>>
, for dumping the TeXmacs buffer #0 to XML on
the console.
For a more detailed tutorial intro to TeXmacs (especially,
how to write TeXmacs plugins), have a look at :
http://www.ags.uni-sb.de/~henri/texmacs/aTeXmacsTutorial.pdf
This tutorial is not complete, but hopefully, it covers what
you need to know in a sufficiently intuitive and practical
way.
In any case, any other questions are welcomed (all the
more because I'm currently improving the documentation
for Scheme developers : thus any clue is of interest ;-).
Best, Henri
; Some utilities
(define-macro (foreach i . b)
`(for-each (lambda
(,(car i))
,(cons 'begin b))
,(cadr i)))
(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)))
; Try this using (xmlize-tree (path->tree '(0)) 0)
; from a Scheme session inside TeXmacs
(tm-define (xmlize-tree t ind)
(define (indent ind)
(foreach-number (i 0 < ind)
(display " "))
)
(indent ind)
(if (tree? t)
(if (tree-atomic? t)
(if (== (tree-arity t) 0)
(display* "<emptyline/>\n")
(display* (tree->stree t) "\n"))
(let* ((n (tree-arity t))
(l (tree-label t))
)
(cond
((== l 'point)
(display* "<point x1='" (tree->stree (tree-ref t 0))
"' x2='" (tree->stree (tree-ref t 1))
"'/>\n"))
((== l 'with)
(display* "<with>")
(foreach-number (i 0 < (/ (- n 1) 2))
(display "\n")(indent (+ ind 2))
(display* "<attribute name='" (tree->stree (tree-ref t (*
2 i))) "'>")
(display "\n")
(xmlize-tree (tree-ref t (+ (* 2 i) 1)) (+ ind 4))
(indent (+ ind 2))
(display* "</attribute>")
)
(display* "\n")
(xmlize-tree (tree-ref t (- n 1)) (+ ind 2))
(indent ind)
(display* "</with>\n"))
(else
(display* "<" (tree-label t) ">\n")
(foreach-number (i 0 < (tree-arity t))
(xmlize-tree (tree-ref t i) (+ ind 2))
;(display* " ")
)
(indent ind)
(display* "</" (tree-label t) ">\n")
))))))
- [Texmacs-dev] Scheme to Prolog, Michael Klein, 2007/02/14
- Re: [Texmacs-dev] Scheme to Prolog, Joris van der Hoeven, 2007/02/14
- Re: [Texmacs-dev] Scheme to Prolog,
Henri Lesourd <=
- Re: [Texmacs-dev] Scheme to Prolog, Joris van der Hoeven, 2007/02/14
- Re: [Texmacs-dev] Scheme to Prolog, Henri Lesourd, 2007/02/14
- Re: [Texmacs-dev] Scheme to Prolog, Joris van der Hoeven, 2007/02/14
- Re: [Texmacs-dev] Scheme to Prolog, Henri Lesourd, 2007/02/14
- Re: [Texmacs-dev] Scheme to Prolog, Joris van der Hoeven, 2007/02/14
- Re: [Texmacs-dev] Scheme to Prolog, Henri Lesourd, 2007/02/14
- Re: [Texmacs-dev] Scheme to Prolog, Joris van der Hoeven, 2007/02/14
- Re: [Texmacs-dev] Scheme to Prolog, Henri Lesourd, 2007/02/14
- Re: [Texmacs-dev] Scheme to Prolog, Henri Lesourd, 2007/02/15