Passing unconditioned text to a TK Text widget

Started by CaveGuy, October 21, 2002, 01:23:37 PM

Previous topic - Next topic

CaveGuy

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.

Lutz

#1
try:



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



So Tk will see your string in braces.





Lutz

CaveGuy

#2
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.

Lutz

#3
this will work:


Quote
(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

CaveGuy

#4
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)) => "rn(1 2 3)" it makes

creating "(setq a '(1 2 3))" real hard.
Bob the Caveguy aka Lord High Fixer.

Lutz

#5
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 'rn(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 "rn". '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

#6
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

CaveGuy

#7
(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.

Lutz

#8
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

CaveGuy

#9
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.

Lutz

#10
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

CaveGuy

#11
Thanks for the hints.  I'l be back :)
Bob the Caveguy aka Lord High Fixer.