[REBOL] Mold and VID objects
From: sunandadh::aol::com at: 22-Dec-2001 7:32
Here's a well-known problem, and an attempt at a solution.
We all know it's bad luck to mistakenly print a VID object:
Layout [myButton: Button]
print mold Mybutton
will print scads of useless stuff, and may at times even hang your session.
The simple answer is don't do it.
But that doesn't work for me any more as I increasingly have data structures
that had Vid objects embedded within them, and I want to print those data
structures.
for example, my tree viewer uses the object:
TREE-OBJECT: make object! [
TYPE: copy "?" ; node type
SORTID: copy "" ; Sort order
ID: copy "?" ; external id for this item (may be duplicates)
NodeID: copy "" ; Internal use only. To give each node a unique
id
PARENTID: none ; Internal user only: internal id of parent
TITLE: copy "" ; Title to display
OPEN?: true ; Are child nodes displayed?
HIDDEN?: false ; Is this node displayed?
DATS: copy "" ; VID display attributes (Bold, Green, font-size
etc)
LEFTCLICK: copy "" ; VID Left click action
RIGHTCLICK: copy "" ; VID Right click action
VID-FACE: copy [] ; VID face
Inner: copy [] ; Child tree objects
]
The key point being that it contains a VID face, and I need to print it (and
all its child inners--also with VID faces) for debugging.
If you have similar problems, you might find the function below useful. I
call it uprint.r (for useful print). It recurses down objects and blocks,
skipping over VID faces. A VID face is empirically identified as being an
object with both OFFSET and PANE defined within it.
Examples:
uprint "mybutton"
uprint "tree-object"
VID-ARRAY: copy [] loop 50 [append VID-ARRAY mybutton] uprint "VID-ARRAY"
(Note the variable to print is in a string)
Problems:
-- It's not fully error trapped (try uprint "system")
-- you can't start with a path (uprint "tree-object/open?" doesn't work)
-- it's not so good for printing functions (uprint "uprint")
But it solves a problem for me. And, if you find it useful, maybe you can
extend it.
Sunanda.
rebol []
uPrint: func [Item [String!] /Inner Val Indent /local Item-Value Last-bit nn]
[
if none? indent [Indent: 0]
loop Indent [prin " "]
Last-bit: copy Item
error? try [Last-bit: last parse Item "/"]
either Inner
[Item-Value: Val]
[if error? try [Item-Value: get to-word Item]
[Print [Item " is not defined"]
Return false
]
]
switch/default form type? item-Value
["object"
[either error? try [Item-Value/offset Item-Value/Pane]
[Print [Last-bit ": make object!"]
foreach var next first Item-Value
[uPrint/Inner join Item ["/" Var]
get in Item-Value var Indent + 3]
]
[print [Last-bit ": VID Object (not printed)"]
]
]
"Block"
[prin Last-bit prin [": ["]
for nn 1 length? Item-Value 1
[either any [object? Item-value/:nn Block? Item-value/:nn]
[uprint/Inner join Item ["/" nn]
Item-value/:nn
indent + 3
]
[prin [mold Item-value/:nn " "]]
]
print "]"
]
]
[; default
prin Last-bit print [":" mold Item-Value]
]
Return True
]