wm title . "Funky Calculator"

proc compute { } {
        set err [catch {set ::results " = [expr $::expr]"} errmsg]
        if { $err } {
                bell
                set ::results "Expression error!"
        } else {
                .follow_win insert 1.0 "\n"
                .follow_win insert 1.0 $::results
                .follow_win insert 1.0 $::expr
                incr ::num_entries_in_follow_win
                set ::expr ""
        }
}

proc get_next_compute { } {
        set str [.follow_win get $::cur_indexed_entry_in_follow_win.0 $::cur_indexed_entry_in_follow_win.end]
        set ::expr [string trim [lindex [split $str "="] 0]]
        set ::results "= [string trim [lindex [split $str "="] 1]]"
        if { $::cur_indexed_entry_in_follow_win < $::num_entries_in_follow_win } {
                incr ::cur_indexed_entry_in_follow_win
        }
}

proc get_prev_compute { } {
        if { $::cur_indexed_entry_in_follow_win > 1 } {
                incr ::cur_indexed_entry_in_follow_win -1
        }
        set str [.follow_win get $::cur_indexed_entry_in_follow_win.0 $::cur_indexed_entry_in_follow_win.end]
        set ::expr [string trim [lindex [split $str "="] 0]]
        set ::results "= [string trim [lindex [split $str "="] 1]]"
}

set results 0
entry .expr -textvar expr -width 40
label .results -textvar results

set num_entries_in_follow_win 0
set cur_indexed_entry_in_follow_win 1
text .follow_win
bind .expr <Return> compute
bind .expr <Down> get_next_compute
bind .expr <Up> get_prev_compute
pack .expr -expand true -fill x
pack .results
pack .follow_win -expand true -fill both

wm deiconify .
focus .
update idletasks
