Writting an editor
Have you ever written an editor? I am going to show you how to write an editor which will work without recompiling on Windows, Linux, AIX, solaris, and Mac. An editor which can safely handle unicode encoded languages and is both fast and small.
Here is your editor.
# In file named editor.tcl
package require Tk ; # Make sure the Tk library is loaded
catch { console show } str ; # Show a console for command input
set rightScrollbar .sb ; # symblic name of scrollbar
set textWin .text ; # symbolc name of text window
scrollbar $rightScrollbar -command "$textWin yview" ; # create a scrollbar
text $textWin -yscrollc "$rightScrollbar set" ; # create a text window
pack $rightScrollbar -side right -fill y ; # put the scrollbar on right
pack $textWin -side right -fill both -expand 1 ; # fill the rest of window with text
proc loadText {w fn} { ; # routine to load text
if {$fn==""} return ; # check for bad input
global fileName ; # make a global named fileName
set fileName $fn ; # save file name for later
wm title . [file tail $fn] ; # set title of window to filename
set fp [open $fn] ; # open the file
$w delete 1.0 end ; # delete any current text
$w insert end [read $fp] ; # insert new text
close $fp ; # close file
}
proc saveText {w fn} { ; # routine to save text
if {$fn==""} return ; # check for bad input
set fp [open $fn w] ; # open file
puts -nonewline $fp [$w get 1.0 end] ; # write to file
close $fp ; # close file
}
if {$argc > 0} { ; # if started with filename
loadText $textWin [lindex $argv 0] ; # load file
} else {
}
focus -force $textWin ; # give text window the focus
Now you can edit a file with the command "wish editor.tcl fileToExit.txt".
The language used is Tcl. Got to ActiveState and follow the directions to download and install for your platform (http://www.activestate.com/activetcl).
No comments:
Post a Comment