A minimally extended minimal editor

Summary

CB 2004/01/12: This was a little piece of fun I had playing with code for my new editor project in progress viTHE. I have backported it to Richard Suchenwirth's A minimal editor.

It is a little fragile in this form (You probably don't want to click the right mouse button if the first line of the file you are editing starts with "<DIR> ..").

Adding a tcl command line allows an increase in the potential abilities of this editor out of all proportion to its size. (Although one would have to come up with some utility procs for it to be really useful.)

[Why add a tcl command line - just call the tkcon widget and get all of its power without having to debug...]


Code

# Minimally extended Minimal editor
#
set about "minEd - a minimally extended minimal editor
Richard Suchenwirth 2003 -- Modified 2004 Connor Berry
F1: help
F2: load
F3: save
F4: navigate 
       filesystem
"
pack [entry .cline] -side bottom -fill x              ;# ADDED
pack [scrollbar .y -command ".t yview"] -side right -fill y
pack [text .t -wrap word -yscrollc ".y set" -undo 1] -side right -fill both -expand 1
 
bind . <F1> {tk_messageBox -message $about}
bind . <F2> {loadText .t [tk_getOpenFile]}
bind . <F3> {saveText .t [tk_getSaveFile]}
bind .cline <Return> {process_cline}                  ;# ADDED
bind . <F4> {showDir}                                 ;# ADDED
bind .t <Button-3> {view}                             ;# ADDED
 
proc loadText {w fn} {
    if {$fn==""} return
    wm title . [file tail $fn]
    set fp [open $fn]
    $w delete 1.0 end
    $w insert end [read $fp]
    close $fp
}
 
proc saveText {w fn} {
    if {$fn==""} return
    set fp [open $fn w]
    puts -nonewline $fp [$w get 1.0 "end - 1 c"]
    close $fp
}
 
# ADDED
proc process_cline {} {
    set my_cmd [.cline get]
    uplevel #0 $my_cmd
    .cline delete 0 end
}
 
# ADDED
proc showDir {} {
    .t delete 1.0 end
    .t insert 1.0 [list_dir]
    .t mark set insert 1.end
}
 
# ADDED
proc list_dir {} {
    append output "<DIR>    ..\n"
    foreach item [glob -nocomplain *] {
        if {[file isdirectory $item]} {
            append output "<DIR>    $item\n"
        } else {
            append output "         $item\n"
        }
    }
    return $output
}
 
# ADDED
proc view {} {
    if {[string match "<DIR>    .." [.t get 1.0 1.end]]} {
        set cursor_pos [.t index insert]
        set line [expr int($cursor_pos)]
        set line_text [.t get ${line}.0 ${line}.end]
        if {[string first "<DIR>" $line_text] == -1} {
            set filename [string trimleft $line_text]
            .t delete 1.0 end
            loadText .t $filename
        } else {
            set dirname [string trimleft [string replace $line_text 0 5]]
            puts $dirname
            cd $dirname
            showDir
        }
    }
}
 
# Modified
if {$argv != ""} {
    loadText .t [lindex $argv 0]
} else {
    append instructions "F1 for minimal help\n"
    append instructions "F2 for load dialog\n"
    append instructions "F3 for save dialog\n"
    append instructions "F4 for interactive filesystem navigation --\n"
    append instructions "    Click left button to select and right button to view\n\n"
    append instructions "Tcl commands can be run from command line at bottom\n"
    .t insert end $instructions
}
focus -force .t

Comments

The ZipGuy 01/2004 if you want a sorted list of files and directories when navigating change the glob in the list_dir proc from

 [[glob -nocomplain *]] 

to

 [[lsort -dictionary [[glob -nocomplain *]] ]]. 

Also I had to move the procs up near the top to avoid errors when the binds referred to commands that had not yet been defined.

HJG Enabled "undo" for the text-widget.