[IPython-dev] editor synchronization

Vivian De Smedt vivian at vdesmedt.com
Sun Sep 9 12:16:49 EDT 2007


Dear all,

This mail to propose a new feature for IPython together with a 
proposition of implementation.
I'm a windows user and I love IPython to run Python command, debug 
Python scripts and inspect Python script failures.

However I prefer to edit my scripts in an non console text editor. 
That's why each time IPython tell me my script has a problem at a given 
line I jump in my editor to inspect the code and fix it.

Since IPython has all the information needed I was wondering if it could 
do it himself and that is the purpose of my patch.

I the attached patch I have introduced a new hook 
'synchronize_with_editor' when defined IPython synchronize my editor 
with the code it is considering in a bunch of situations:
 - When it notice that it couldn't import a code that is syntactically 
incorrect (and let me fix it swiftly)
 - When it print a traceback because a script failed (and let me at what 
portion of the code the script failed)
 - When the debugger print a bunch of line of the current line (and let 
me follow the progression of the code pointer in my favorite editor 
which give a much bigger code context)

With that hook set in place debugging script with IPython is nearly as 
easy (and for the reasons you know even more easy) that debugging with a 
Gui debugger as Komodo or PythonWin.

Here is the version of the hook I have put in my "ipy_user_conf.py" 
configuration file:

    # vds >>
    import win32api
    import win32ui
    import win32console
    import dde
    import os

    def synchronize_with_editor(ip, filename, lineno, columnno):
        if not os.path.isabs(filename):
            filename = os.path.join(os.getcwd(), filename)
        if not os.path.isfile(filename):
            print "couldn't find file:", file
            return
        h = win32console.GetConsoleWindow()
        w = win32ui.CreateWindowFromHandle(h)
        w.SetWindowText("%s %d" % (filename, lineno))
       
        server = dde.CreateServer()
        server.Create("myddeserver")
        conversation = dde.CreateConversation(server)
        conversation.ConnectTo("uedit32", "System")
        conversation.Exec(r'[open("%s/%d"])' % (filename, lineno))
       
        win32api.Sleep(10)
        w.SetForegroundWindow()
        server.Shutdown()       
    # vds <<

    import IPython.ipapi
    ip = IPython.ipapi.get()
    ip.set_hook("synchronize_with_editor", synchronize_with_editor)

For the one that would be interested at testing it.
In that version I use UltraEdit and its DDE functionality but something 
simpler works also for other editors that dont have DDE capabilities:

    # vds >>
    import win32api
    import win32console
    import os

    def synchronize_with_editor(ip, filename, lineno, columnno):
        if not os.path.isabs(filename):
            filename = os.path.join(os.getcwd(), filename)
        if not os.path.isfile(filename):
            print "couldn't find file:", file
            return
        h = win32console.GetConsoleWindow()
        w = win32ui.CreateWindowFromHandle(h)
        w.SetWindowText("%s %d" % (filename, lineno))
       
        os.system("uedit32/%d" % lineno)
      
        win32api.Sleep(10)
        w.SetForegroundWindow()
        server.Shutdown()       
    # vds <<

    import IPython.ipapi
    ip = IPython.ipapi.get()
    ip.set_hook("synchronize_with_editor", synchronize_with_editor)

I put in attachment a patch for the 8.1 version. I don't have svn access 
here but if some one send me the code of the latest version I'll be glad 
to send a patch for that version too.


Kindest regards,
Vivian.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20070909/5a824508/attachment.html>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: diff.txt
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20070909/5a824508/attachment.txt>


More information about the IPython-dev mailing list