1
0
mirror of https://github.com/wting/autojump synced 2024-10-27 20:34:07 +00:00
wting_autojump/tools/autojump_ipython.py

37 lines
1.2 KiB
Python
Raw Normal View History

2010-11-25 14:19:33 +00:00
# This module was contributed by Mario Pastorelli <pastorelli.mario@gmail.com>
# It is released in the public domain
2010-11-25 14:33:53 +00:00
# This tool provides "j" for ipython
# To use it, copy it in your ~/.ipython directory
# and add the following line to ipy_user_conf.py:
# import autojump_ipython
2010-11-25 14:19:33 +00:00
import os
import subprocess as sub
from IPython.ipapi import get
from IPython.iplib import InteractiveShell
ip = get()
def magic_j(self,parameter_s=''):
cmd = ['autojump']+parameter_s.split()
2010-11-25 14:33:53 +00:00
# print 'executing autojump with args %s' % str(cmd)
2010-11-25 14:19:33 +00:00
newpath=sub.Popen(cmd,stdout=sub.PIPE,shell=False).communicate()[0][:-1] # delete last '\n'
2010-11-25 14:33:53 +00:00
# print 'Autojump answer: \'%s\'' % newpath
if newpath:
ip.magic('cd \'%s\'' % newpath)
2010-11-25 14:19:33 +00:00
def cd_decorator(f):
def autojump_cd_monitor(self,parameter_s=''):
f(self,parameter_s)
2010-11-25 14:33:53 +00:00
sub.call(['autojump','-a',os.getcwd()])
2010-11-25 14:19:33 +00:00
return autojump_cd_monitor
# Add the new magic function to the class dict and decorate magic_cd:
InteractiveShell.magic_j = magic_j
InteractiveShell.magic_cd = cd_decorator(InteractiveShell.magic_cd)
# And remove the global name to keep global namespace clean.
del magic_j
del cd_decorator