How to add recently edited files to IDLE?

Eric Hardwick e_hardwick at yahoo.com
Tue Oct 1 13:44:36 EDT 2002


Lance,

I wrote a very simple IDLE extension to do just that about 6 months
ago. I never released it since it was just a quick hack to help myself
in using IDLE.

Feel free to use this if it suits your needs.

Copy the following code into a file named "RecentFiles.py" and place
that file in the IDLE directory. Then edit the config.txt file (also
in the IDLE directory). Add the following line of text to the end of
the config.txt file to enable the extension.

[RecentFiles]

That's all you should need to do. Please note this extension has only
been tested and used by me. No guarantees are given. Make sure you
format the code correctly if the act of posting it here changes it
somehow.

Eric Hardwick

# RecentFiles.py CODE BEGINS #

##############################################################################
# File: RecentFiles.py
# Author: Eric Hardwick
# Purpose: Modify the File menu so that every time the File menu item
is opened,
#	a list of the recently opened files will be created and placed in
#	the Recent Files menu item that gets created under the File menu.
##############################################################################

import pickle
import os
from Tkinter import *


# When module is imported, load the recent files list from file.
history_file = os.path.join(os.path.dirname(__file__),
"recentfiles.dat")
history_list = []

try:
    f = open(history_file, "rb")
    history_list = pickle.load(f)
    f.close()
    
except IOError, EOFError:
    pass


class RecentFiles:
    
    menudefs = [
    ]

    windows_keydefs = {
    }
    
    unix_keydefs = {
    }

    def __init__(self, editwin):
        global history_file
        global history_list
            
        self.editwin = editwin
        
        # Get the filemenu obj from the edit win
        file_menu = get_sub_menu_obj(editwin.menubar, 'File')
        if file_menu == None:
            return
        
        # Create the Recent files menu item
        file_menu.add_separator()
        self.recent_menu = Menu(file_menu, tearoff=0)
        file_menu.add_cascade(label='Recent Files',
menu=self.recent_menu, underline=0)
        file_menu.config(postcommand=self.updateRecentMenu)
        
        
        # Check if there are any new files in the flist
        file = editwin.flist.inversedict[editwin]
        if file == None:
            return


        # Remove it from the list
        try:
            history_list.remove(file)
        except ValueError:
            pass
        history_list.append(file)

        if len(history_list) >= 10:
            history_list = history_list[-10:]
        
        # Dump it out to file
        try:
            f = open(history_file, "wb")
            pickle.dump(history_list, f)
            f.close()
        except IOError:
            pass

        
    def updateRecentMenu(self):
        global history_list
        
        # Delete any existing entries in the Recent Files menu
        # Get the filemenu obj from the edit win
        file_menu = get_sub_menu_obj(self.editwin.menubar, 'File')
        if file_menu == None:
            return

        file_menu.delete(file_menu.index('Recent Files'))
        
        # Create the Recent files menu item

        self.recent_menu = Menu(file_menu, tearoff=0)
        file_menu.add_cascade(label='Recent Files',
menu=self.recent_menu, underline=0)
        file_menu.config(postcommand=self.updateRecentMenu)
        

        entries = history_list[:]
        entries.reverse()

        index = 0
        for file in entries:
            def openrecent(f=file, editwin=self.editwin):
                editwin.flist.open(f)
            self.recent_menu.add_command(label=str(index) + ' ' +
file, command=openrecent, underline=0)
            index = index + 1
            

def get_sub_menu_obj(parent, label):
    index_count = parent.index("end")

    for index in range(1, index_count + 1):

        try:    
            menu_label = parent.entrycget(index, 'label')
        except TclError:
            menu_label = ''
        
        if menu_label == label:
            try:
                menu_id = parent.entrycget(index, 'menu')
                menu_id = menu_id.split('.')[-1:][0]

                menu_obj = parent.children[menu_id]
                return menu_obj
            except TclError:
                return None
    return None

# RecentFiles.py CODE ENDS #


"Lance" <lbrannma at cablespeed.com> wrote in message news:<upbpl7n39co835 at corp.supernews.com>...
> Hi All again,
> 
> It would also be nice to add a list of recently edited files to the File
> menu in IDLE. Any suggestions would be appreciated.
> 
> Thanks,
> Lance



More information about the Python-list mailing list