Passing unconditioned text to a TK Text widget

Q&A's, tips, howto's

Passing unconditioned text to a TK Text widget

Postby CaveGuy » Mon Oct 21, 2002 8:23 pm

I am caught in a string problem with TK.


Here is the snipit I am having problems with.

(set 'fh (open ns "read"))
(while (read-line fh)
(set 'tl (current-line))
(tk ".meow1.fr2.text insert end " tl)
)
(tk "pack .meow1.fr2.text")
(close fh)

I want to put the contents of an unconditioned text file 'ns into a previously opened text widget .meow1.fr2.text. The file is unconditioned and has
random punctuation. I want the string contained in 'tl to be presented as-is without evaluation by TK or NewLISP.

I am sure I have overlooked something simple :)
Bob the Caveguy aka Lord High Fixer.
CaveGuy
 
Posts: 112
Joined: Sun Oct 13, 2002 3:00 pm
Location: Columbus Ohio

Postby Lutz » Tue Oct 22, 2002 7:07 pm

try:

(tk ".meow1.fr2.text insert end {" tl "}")

So Tk will see your string in braces.


Lutz
Lutz
 
Posts: 5279
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

Postby CaveGuy » Tue Oct 22, 2002 7:49 pm

ok, that kinda works :) Now how do I force line seperation.
\n does not seem to work as I think it should.

(tk ".meow1.fr2.text insert end {" tl "\n}") ???
(tk ".meow1.fr2.text insert end {" tl "\\n}") ???
Bob the Caveguy aka Lord High Fixer.
CaveGuy
 
Posts: 112
Joined: Sun Oct 13, 2002 3:00 pm
Location: Columbus Ohio

Postby Lutz » Tue Oct 22, 2002 9:48 pm

this will work:

(set 'line "hello There")
(tk "toplevel .txtdemo; pack [text .txtdemo.txt]")
(tk ".txtdemo.txt insert end [subst {" line "\\n}]")
(tk ".txtdemo.txt insert end [subst {" line "\\n}]")


The second line is just a cute trick, how you can roll 3 statements into one. The critical thing is the 'subst', which gets a textual '\n' and makes a linefeed out of it. The [,] brackets force evaluation in Tcl/Tk.

Lutz
Lutz
 
Posts: 5279
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

Postby CaveGuy » Tue Oct 22, 2002 10:25 pm

Thanks for the clues, they help ...
I think it may be a mental block, but I do still have a problem.

(set 'line "My address is [66.35.72.10]")
in the previous example results in
an invalid command 66.35.72.10 error :(

also what is with (string '(1 2 3)) => "\r\n(1 2 3)" it makes
creating "(setq a '(1 2 3))" real hard.
Bob the Caveguy aka Lord High Fixer.
CaveGuy
 
Posts: 112
Joined: Sun Oct 13, 2002 3:00 pm
Location: Columbus Ohio

Postby Lutz » Tue Oct 22, 2002 10:54 pm

That is a bug, which will be fixed in the next release. But creating statements still works:

(set 's (string "(setq a '" '(1 2 3) ")")) => "(setq a '\r\n(1 2 3))"

(eval-string s) => (1 2 3)

Note, that 'string' after the bug fix will still embed line breaks, but trim the leading "\r\n". 'string' is doing pretty-print on the expressions passed to it.

>> (set 'line "My address is [66.35.72.10]")

let me think about it

Lutz
Lutz
 
Posts: 5279
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

Postby Lutz » Tue Oct 22, 2002 11:06 pm

the following will work:

(set 'line "My address is \\[66.35.72.10\\]")
(tk "toplevel .txtdemo; pack [text .txtdemo.txt]")
(tk ".txtdemo.txt insert end [subst {" line "\\n}]")

again 'subts' is doing the trick. What you would have todo with your lines before passing them is a general replace of offending stuff:

(replace "[" line "\\[")
(replace "]" line "\\]")
(replace "$" line "\\$")

The same you would have to do for other characters confusing the issue. {,} braces go through fine but $ doesn't:


Lutz
Lutz
 
Posts: 5279
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

Postby CaveGuy » Tue Oct 22, 2002 11:19 pm

(setq a '(1 2 3))
(setq fh (open "test.file" "w"))
(write-line (append "(set '" (string 'a) " '" (string a)))
(close fh)

an open readline of test.file retuens => "(setq 'a '"
a second read-line returns => "(1 2 3))"

myload has to doo a double read-line for each symbol.

Thanks for the fix, it about drove me nuts till I figured out what it was doing.
Bob the Caveguy aka Lord High Fixer.
CaveGuy
 
Posts: 112
Joined: Sun Oct 13, 2002 3:00 pm
Location: Columbus Ohio

Postby Lutz » Wed Oct 23, 2002 12:51 am

instead of:

(append (string x) (string y) (string z))

you can do:

(string x y z)

'string' not only converts but also appends, if given more than one argument. Use 'append' if all arguments are already strings, in which instance it is faster than using string.

Also, just finished the fix for 'string' no pretty-printing at all will be done in 'string'. This will avoid any problem of unwanted/unforseen line-feeds.

Lutz
Lutz
 
Posts: 5279
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

Postby CaveGuy » Wed Oct 23, 2002 1:39 am

Got two fresh stumbling block this time :)

(directory 'mydir) subber reaily needs to accept a file mask. Unless of course you already have a directory to list box widget example :)


Now that I can build and fill text widgets nicely, how do I "clear" one ?

My next task is to work out the callbacks, I am sure I will have questions in that area soon.
Bob the Caveguy aka Lord High Fixer.
CaveGuy
 
Posts: 112
Joined: Sun Oct 13, 2002 3:00 pm
Location: Columbus Ohio

Postby Lutz » Wed Oct 23, 2002 5:01 pm

Try this for the directoty mask:

(filter (lambda (x) (ends-with x ".bat" nil)) (directory "c:/"))


This would print a list ofl files ending with ".bat" or ".BAT" in the root of C (note the legal forward slash, although on Windows). The 'nil' in the 'ends-with' function tells it to be case insensitive.

I also have good news, there are file dialogboxes standard in the normal Tcl/Tk, so it is builtin and accessible from newLISP-tk, they are called 'tk_getOpenFile' or 'tk_getSaveFile'. Just try the following in newLISP:

(tk "tk_getOpenFile")

select a file and hit return. The return val;ue of the function will be the path-file-string of the file selected. There are zillions of options for filetype masks, dialogbox title, etc. Look it up in some Tcl/Tk book. BTW I am pretty happy with the "'Reilly Tcl/Tk Pocket Reference", where I have it from.

There is also a 'tk_chooseColor', and a 'tk_dialog', which can be configured with buttons and a 'tk_messageBox'.

To delete a text window:

(tk ".mywin.mytxt delete 1.0 end")

Lutz
Lutz
 
Posts: 5279
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

Postby CaveGuy » Wed Oct 23, 2002 8:21 pm

Thanks for the hints. I'l be back :)
Bob the Caveguy aka Lord High Fixer.
CaveGuy
 
Posts: 112
Joined: Sun Oct 13, 2002 3:00 pm
Location: Columbus Ohio


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 1 guest

cron