diff --git a/twander.py b/twander.py index b663bd0..3863aaf 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.51 2003/01/11 07:41:07 tundra Exp $" +RCSID = "$Id: twander.py,v 2.52 2003/01/11 22:52:02 tundra Exp $" VERSION = RCSID.split()[2] @@ -244,6 +244,7 @@ COMMANDMENU = 'Commands' # Title for Command Menu button DIRMENU = 'Directories' # Title for Directory Menu button +HISTMENU = 'History' # Title for History Menu button PSEP = os.sep # Character separating path components SHOWDRIVES = '\\\\' # Logical directory name for Win32 Drive Lists @@ -916,6 +917,12 @@ self.DirBtn.menu = Menu(self.DirBtn, font=("courier", FSZ, "bold")) self.DirBtn.pack(side=LEFT, padx=MENUPADX) + # Setup the History Menu + + self.HistBtn = Menubutton(self.mBar, text=HISTMENU, underline=0, font=(FNAME, FSZ, "bold")) + self.HistBtn.menu = Menu(self.HistBtn, font=("courier", FSZ, "bold")) + self.HistBtn.pack(side=LEFT, padx=MENUPADX) + # Setup the Directory Listing and Scrollbars self.hSB = Scrollbar(root, orient=HORIZONTAL) @@ -1251,11 +1258,12 @@ if event.state == Button3Mask: # Button-3 / No Modifier x, y = UI.DirList.winfo_pointerxy() # Position near mouse - CommandMenu(event, x, y) # Display Context Menu + PopupMenu(UI.CmdBtn.menu, x, y) # Display Command Menu elif event.state == (Button3Mask | ShiftMask): # ShiftButton-3 x, y = UI.DirList.winfo_pointerxy() # Position near mouse - DirMenu(event, x, y) # Display Directory Menu + PopupMenu(UI.DirBtn.menu, x, y) # Display Directory Menu + # End Of 'MouseClick() @@ -1288,33 +1296,18 @@ ##### -# Event Handler: Context Menu +# Event Handler: Popup Menus ##### -def CommandMenu(event, x, y): +def PopupMenu(menu, x, y): - # Popup a menu of available commands at requested coordinates - # but only if there are commands defined. + # Popup requested menu at specified coordinates + # but only if the menu has at least one entry. - if UI.CmdBtn.menu.index(END): - UI.CmdBtn.menu.tk_popup(x, y) + if menu.index(END): + menu.tk_popup(x, y) -# End of 'CommandMenu()' - - -##### -# Event Handler: Directory Menu -##### - -def DirMenu(event, x, y): - - # Popup a menu of available directories at requested coordinates - # but only if there are previously visited directories. - - if UI.DirBtn.menu.index(END): - UI.DirBtn.menu.tk_popup(x, y) - -# End of 'DirMenu()' +# End of 'PopupMenu()' ##### @@ -1332,40 +1325,42 @@ # Check for, and handle accelerator keys if event.state == AltMask: + # Set menu button associated with accelerator + + # Command Menu if event.char == 'c': + button = UI.CmdBtn - # Get current location/size of Command Menu - - parts = UI.CmdBtn.winfo_geometry().split('+') # Geometry returned as "WidthxHeight+X+Y" - dims = parts[0].split('x') - x, y = int(parts[1]), int(parts[2]) - w, h = int(dims[0]), int(dims[1]) - - x += UIroot.winfo_rootx() # This is relative to root window position - y += UIroot.winfo_rooty() # So adjust accordingly - - # Display the Command Menu - CommandMenu(event, x+MENUOFFSET, y+h) - - # Inhibit event from getting picked up by local accelerator key handlers - return "break" - + # Directory Menu elif event.char == 'd': - # Get current location/size of Directory Menu + button = UI.DirBtn - parts = UI.DirBtn.winfo_geometry().split('+') # Geometry returned as "WidthxHeight+X+Y" - dims = parts[0].split('x') - x, y = int(parts[1]), int(parts[2]) - w, h = int(dims[0]), int(dims[1]) + # History Menu + elif event.char == 'h': + button = UI.HistBtn - x += UIroot.winfo_rootx() # This is relative to root window position - y += UIroot.winfo_rooty() # So adjust accordingly + # Unrecognized - Ignore + else: + return - # Display the Command Menu - DirMenu(event, x+MENUOFFSET, y+h) + parts = button.winfo_geometry().split('+') # Geometry returned as "WidthxHeight+X+Y" + dims = parts[0].split('x') - # Inhibit event from getting picked up by local accelerator key handlers - return "break" + x, y = int(parts[1]), int(parts[2]) + w, h = int(dims[0]), int(dims[1]) + + x += UIroot.winfo_rootx() # This is relative to root window position + y += UIroot.winfo_rooty() # So adjust accordingly + + # Display the requested menu + PopupMenu(button.menu, x+MENUOFFSET, y+h) + + # Inhibit event from getting picked up by local accelerator key handlers + return "break" + + ##### + # Otherwise, process single character command invocations. + ##### # We *only* want to handle simple single-character # keystrokes. This means that there must be a character @@ -1426,14 +1421,8 @@ cmd = cmd.replace(SELECTION, QUOTECHAR + selection + QUOTECHAR) cmd = cmd.replace(SELECTIONS, selections) - # Just dump command if we're debugging - - if int(DEBUGLEVEL) & DEBUGCMDS: - PrintDebug(dCMD, [cmd,]) - - # Otherwise,actually execute the command - else: - ExecuteCommand(cmd) + # Run the command + ExecuteCommand(cmd) # end of 'KeystrokeHandler()' @@ -1728,17 +1717,26 @@ # Event Handler: Run Command #### -def KeyRunCommand(event): +def KeyRunCommand(event, initial=""): global UI + # Prompt with passed initial edit string + if initial: + cmd = askstring(pRUNCMD, pENCMD, initialvalue=initial) - if UI.CmdHist: - cmd = askstring(pRUNCMD, pENCMD, initialvalue=UI.CmdHist[-1] ) + # Prompt with last manually entered command + elif UI.LastCmd: + cmd = askstring(pRUNCMD, pENCMD, initialvalue=UI.LastCmd ) + + # Prompt with no initial string else: cmd = askstring(pRUNCMD, pENCMD) + # Save command (if any) as last manually entered and execute it if cmd: + UI.LastCmd = cmd ExecuteCommand(cmd) + UI.DirList.focus() # End of 'KeyRunCommand()' @@ -1852,20 +1850,34 @@ def ExecuteCommand(cmd, UseStartDir=FALSE): global UI - # Run the command on Win32 using filename associations - if UseStartDir: - os.startfile(cmd) + # Just dump command if we're debugging - # Normal command execution for both Unix and Win32 + if int(DEBUGLEVEL) & DEBUGCMDS: + PrintDebug(dCMD, [cmd,]) + + # Otherwise,actually execute the command else: - thread.start_new_thread(os.system, (cmd,)) + + # Run the command on Win32 using filename associations + if UseStartDir: + os.startfile(cmd) - # Save command on history stack maintaining max stack depth + # Normal command execution for both Unix and Win32 + else: + thread.start_new_thread(os.system, (cmd,)) + + # In all cases, save command history, maintaining max stack depth 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 # Dump Command History stack if requested @@ -2267,7 +2279,7 @@ 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 + UI.DirBtn['menu'] = UI.DirBtn.menu # End of 'UpdateDirMenu()' @@ -2384,6 +2396,9 @@ # Initialize command history UI.CmdHist = [] +# Initialize last command buffer +UI.LastCmd = "" + # Parse the and store configuration file, if any ParseConfFile(None)