diff --git a/twander.py b/twander.py index d5e0b27..372ecd7 100755 --- a/twander.py +++ b/twander.py @@ -4,7 +4,7 @@ # For Updates See: http://www.tundraware.com/Software/twander PROGNAME = "twander" -RCSID = "$Id: twander.py,v 2.57 2003/01/14 22:05:09 tundra Exp $" +RCSID = "$Id: twander.py,v 2.58 2003/01/15 21:41:48 tundra Exp $" VERSION = RCSID.split()[2] @@ -569,8 +569,16 @@ foreground=FCOLOR, background=BCOLOR, height=HEIGHT, width=WIDTH) - # Dump requested debug information + # Make sure menus conform to max lengths (which may have changed). + UpdateMenu(UI.DirBtn, UI.AllDirs, MAXDIR, LoadDirList) + UpdateMenu(UI.HistBtn, UI.CmdHist, MAXHIST, KeyRunCommand, fakeevent=TRUE) + + + ##### + # Dump requested debug information + ##### + if int(DEBUGLEVEL) & DEBUGKEYS: debuginfo = [] @@ -618,6 +626,10 @@ PrintDebug(dINTVAR, debuginfo) + # If we just wanted debug output, quit now + if int(DEBUGLEVEL) & DEBUGQUIT: + sys.exit() + # End of 'ParseConfFile()' @@ -754,11 +766,16 @@ WrnMsg(wBADVAL % (num, line)) elif name in UI.OptionsString: - try: - globals()[name] = val - except: + # RHS cannot be null + if not val: WrnMsg(wBADVAL % (num, line)) + else: + try: + globals()[name] = val + except: + WrnMsg(wBADVAL % (num, line)) + # Process other variable types. # Distinguish between internal program variables and # user-defined variables and act accordingly. We inhibit @@ -1784,7 +1801,7 @@ if cmd: ExecuteCommand(cmd) - # Save the command only if Command History is enable (MAXHIST > 0) + # Save the command only if Command History is enabled (MAXHIST > 0) # AND one of two conditions exist: # # 1) No initial string was provided (The user entered a command manually). @@ -1944,24 +1961,8 @@ WrnMsg(wBADEXE % cmd) - # If the user has Command History enabled (MAXHIST > 0), - # save command history, maintaining max stack depth - - if (MAXHIST > 0): - if len(UI.CmdHist) == MAXHIST: - UI.CmdHist = UI.CmdHist[1:] - UI.CmdHist.append(cmd) - - # Now update the History Menu accordingly - - UI.HistBtn.menu.delete(0,END) - for entry in UI.CmdHist: - UI.HistBtn.menu.add_command(label=entry, command=lambda cmd=entry: KeyRunCommand(None, initial=cmd)) - UI.HistBtn['menu'] = UI.HistBtn.menu - - # Enable the History Menu button if there is now content there - if UI.HistBtn.menu.size(): - UI.HistBtn.config(state=NORMAL) + # Update the Command History observing MAXHIST + UpdateMenu(UI.HistBtn, UI.CmdHist, MAXHIST, KeyRunCommand, newentry=cmd, fakeevent=TRUE) # Dump Command History stack if requested @@ -2114,17 +2115,22 @@ else: fList.append(file) - # On Win32, adjust file-name case to make sorting meaningful + # On Win32, do case-insensitive sorting since case + # is irrelevant in file names on these systems if OSNAME == 'nt': - l = fList - for x in range(len(l)): - l[x] = l[x].lower() - # Put each in sorted order + for l in (dList, fList): + lowerlist = [(x.lower(), x) for x in l] + lowerlist.sort() + l = [] + [l.append(x[1]) for x in lowerlist] - dList.sort() - fList.sort() + # Everywhere else, do absolute sorts + + else: + dList.sort() + fList.sort() # Entry to move up one directory is always first, # no matter what the sort. This is necessary because @@ -2385,8 +2391,7 @@ # First make a case-collapsed copy of the existing list dlc = [] - for d in UI.AllDirs: - dlc.append(d.lower()) + [dlc.append(d.lower()) for d in UI.AllDirs] # Now see if our new entry is already there @@ -2399,26 +2404,53 @@ # Now add the entry if we decided it was necessary. observing MAXDIR value. if addentry: - UI.AllDirs.append(newdir) - if len(UI.AllDirs) > MAXDIR: - UI.AllDirs = UI.AllDirs[1:] - - # If there's anything to display and we've actually changed something, update the menu - - if len(UI.AllDirs) and addentry: - UI.AllDirs.sort() - UI.DirBtn.menu.delete(0,END) - for dir in UI.AllDirs: - UI.DirBtn.menu.add_command(label=dir, command=lambda dir=dir: LoadDirList(dir)) - UI.DirBtn['menu'] = UI.DirBtn.menu - - # Directory Menu now has content, enable it - UI.DirBtn.config(state=NORMAL) - + UpdateMenu(UI.DirBtn, UI.AllDirs, MAXDIR, LoadDirList, newentry=newdir) # End of 'UpdateDirMenu()' +##### +# Generic Menu Update Routine +##### + +def UpdateMenu(menubtn, datastore, max, func, newentry="", sort=TRUE, fakeevent=FALSE): + + # First add the new data, if any, to the specified data storage stucture. + + if newentry: + datastore.append(newentry) + + # Now trim it to requested maximum length - this only changes how many + # entries are display on the menu - nothing is actually removed + # from the data storage for this menu. + + data = datastore[:] + if not max: + data = [] + elif len(data) > max: + data = datastore[-max:] + + # Initialize the menu to empty + + menubtn.menu.delete(0,END) + menubtn.config(state=DISABLED) + + if len(data): + if sort: + data.sort() + for entry in data: + if fakeevent: + menubtn.menu.add_command(label=entry, command=lambda item=entry: func(None, item)) + else: + menubtn.menu.add_command(label=entry, command=lambda item=entry: func(item)) + menubtn['menu'] = menubtn.menu + + # Menu now has content, enable it + menubtn.config(state=NORMAL) + +# End of 'UpdateMenu()' + + #----------------------------------------------------------# # Program Entry Point # #----------------------------------------------------------# @@ -2601,10 +2633,6 @@ LoadDirList(STARTDIR) KeySelTop(None) -# If we just wanted debug output, quit now -if int(DEBUGLEVEL) & DEBUGQUIT: - sys.exit() - # And start the periodic polling of the widget UI.poll()