Linux, Unix, /etc

Danger Will Robinson! You are now entering a condescending Unix user zone!
Sponsored links (requires javascript):

The Nvi Tcl Interpreter

I decided the other day that I would recompile nvi with the tcl interpreter enabled. Why? Take a simple example. If I want to count the words in the file I'm editing, I can do:

!wc -w %

Where % is expanded to "current file name". wc is of course an integral part of any Linux or Unix system. In tcl, this could be written thusly (this example is shamelessly robbed from the nvi source distribution):

#       @(#)wc.tcl      8.2 (Berkeley) 11/18/95
#
proc wc {} {
global viScreenId
global viStartLine
global viStopLine

set lines [viLastLine $viScreenId]
set output ""
set words 0
for {set i $viStartLine} {$i <= $viStopLine} {incr i} {
set outLine [split [string trim [viGetLine $viScreenId $i]]]
set words [expr $words + [llength $outLine]]
}
viMsg $viScreenId "$words words"
}

To invoke this in the nvi tcl interpreter, we would simply type:

:tc wc

Of course, to have this and other scripts visible to the built-in tcl interpreter, nvi has to load them when it starts. A simple addition to .exrc takes care of this:

""" load scripts from nvi  tcl interpreter
tc source /home/paul/.nvi_tcl

Having a built-in interpreter doesn't seem offer much advantage over the traditional way of invoking external filters on the ex command line, but I thought it would be interesting to explore.

Unfortunately, development on nvi seems to have come to a standstill. Not a bad thing in itself — one of the drawbacks of so much free software is how it is constantly changing "under your feet", so to speak — but in the case, it seems work came to a halt while there were still things to do. Not that the software isn't stable and complete in itself, but the tcl interpreter is left undocumented.

More about Tcl here: Softpanorama Tcl Links.

By the way, amid all my reading about Tcl on the weekend, one snippet caught my attention:

About the extra newline thing... under Unix, (possibly POSIX) all text files are supposed to end in a blank line, hence the "extra" newline. This is the proper behavior, even if it isnt technically required for most things these days. Unix text editors and tools still enforce it
—http://mini.net/tcl/367

Did you know that? I didn't know that.


[back to Linux, Unix, /etc]



Copyright © 1995-2007 Paul Dunne,

Sponsored links (requires javascript):