diff --git a/twander.py b/twander.py index f6beaac..98d477f 100755 --- a/twander.py +++ b/twander.py @@ -4,7 +4,7 @@ PROGNAME = "twander" -RCSID = "$Id: twander.py,v 1.39 2002/11/10 19:21:46 tundra Exp $" +RCSID = "$Id: twander.py,v 1.40 2002/11/10 21:47:59 tundra Exp $" VERSION = RCSID.split()[2] @@ -97,7 +97,8 @@ CMDKEY = r'&' # Command key delimiter COMMENT = r"#" # Comment character -NAME = r'[NAME]' # Substitution field in config files +DIRNAME = r'[DIRECTORY]' # Substitution field in config files +FILENAME = r'[FILE]' # Substitution field in config files ENVIRO = r'$' # Introduces environment variables @@ -161,6 +162,8 @@ #----------------------------------------------------------# +FILESELECTED = "" + #---------------------------Code Begins Here----------------------------------# @@ -226,6 +229,10 @@ # Canonicalize the current directory name ROOTDIR = os.path.abspath(ROOTDIR) + # And make sure it ends with a path separator character + if ROOTDIR[-1] != PSEP: + ROOTDIR = ROOTDIR + PSEP + # Update the window title UIroot.title(PROGNAME + " " + VERSION + " " + ROOTDIR) @@ -290,9 +297,11 @@ WrnMsg(wCMDKEY % fields[0]) key = fields[0] # Use whole word as index - # Found legit delimiter + # Found legit delimiter, so use it as + # dictionary index - mapped to lower case so + # command keys are case insensitive. else: - key = fields[0][keypos+1] # Use selected key as index + key = fields[0][keypos+1].lower() if key in rcfile: # This is a Python 2.2 or later idiom ErrMsg(eDUPKEY % fields[0]) # Duplicate key found @@ -348,6 +357,7 @@ def __init__(self, root): # Setup the visual elements + self.hSB = Scrollbar(root, orient=HORIZONTAL) self.vSB = Scrollbar(root, orient=VERTICAL) self.DirList = Listbox(root, @@ -362,22 +372,34 @@ width = WIDTH, ) + # Make them visible by packing + self.hSB.config(command=self.DirList.xview) self.hSB.pack(side=BOTTOM, fill=X) self.vSB.config(command=self.DirList.yview) self.vSB.pack(side=RIGHT, fill=Y) self.DirList.pack(side=LEFT, fill=BOTH, expand=1) + # Bind the relevant widget event handlers + self.DirList.bind('', DirListHandler) + + # Bind the relevant root window handlers + + # Set up keystroke handler for application + root.bind('', KeystrokeHandler) + + # Make sure the top level window has input focus + root.focus() # End of class definition, 'twanderUI' ##### -# Double-Click Event Handler For Directory Listing ListBox +# Event Handler For Directory Listbox ##### def DirListHandler(event): - global ROOTDIR + global FILESELECTED, ROOTDIR selected = UI.DirList.get(UI.DirList.nearest(event.y)) @@ -386,6 +408,9 @@ # delimiter characters previously inserted in BuildDirList() if selected[0] == DIR_LDELIM and selected[-1] == DIR_RDELIM: + + FILESELECTED = "" # Didnt' select a file + # Strip off delimiters to get real name selected = selected[1:-1] @@ -410,12 +435,37 @@ ROOTDIR = selected LoadDirList() + # File was selected. Save its full name for later use. else: - # If its not a dir, we ignore double-clicks - pass + FILESELECTED = os.path.join(ROOTDIR, selected) # End of 'DirListHandler()' + +##### +# Event Handler For Individual Keystrokes +##### + +def KeystrokeHandler(event): + + # If the key pressed is a command key, + # get its associated string and + # execute the command. + + cmd = rcfile.get(event.char.lower(), ["",""])[1] + + # cmd == null means no matching command key - do nothing + # Otherwise, replace config tokens with actual file/dir names + + if cmd: + cmd = cmd.replace(FILENAME, FILESELECTED) + cmd = cmd.replace(DIRNAME, ROOTDIR) + + # Actually execute the command + + os.system(cmd) + +# end of 'KeystrokeHandler() #----------------------------------------------------------# @@ -450,7 +500,7 @@ if opt == "-n": FNAME = val if opt == "-q": - WARN = FALSE + WARN = FALSE if opt == "-s": FSZ = val if opt == "-v": @@ -497,9 +547,6 @@ # Initialize the UI directory listing LoadDirList() -# Set up handler for double-clicks in list box -UI.DirList.bind('', DirListHandler) - # Run the program interface UIroot.mainloop()