##// END OF EJS Templates
Add quotes in the editor hook so that editor commands with spaces in them...
fperez -
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -1,184 +1,184 b''
1 """hooks for IPython.
1 """hooks for IPython.
2
2
3 In Python, it is possible to overwrite any method of any object if you really
3 In Python, it is possible to overwrite any method of any object if you really
4 want to. But IPython exposes a few 'hooks', methods which are _designed_ to
4 want to. But IPython exposes a few 'hooks', methods which are _designed_ to
5 be overwritten by users for customization purposes. This module defines the
5 be overwritten by users for customization purposes. This module defines the
6 default versions of all such hooks, which get used by IPython if not
6 default versions of all such hooks, which get used by IPython if not
7 overridden by the user.
7 overridden by the user.
8
8
9 hooks are simple functions, but they should be declared with 'self' as their
9 hooks are simple functions, but they should be declared with 'self' as their
10 first argument, because when activated they are registered into IPython as
10 first argument, because when activated they are registered into IPython as
11 instance methods. The self argument will be the IPython running instance
11 instance methods. The self argument will be the IPython running instance
12 itself, so hooks have full access to the entire IPython object.
12 itself, so hooks have full access to the entire IPython object.
13
13
14 If you wish to define a new hook and activate it, you need to put the
14 If you wish to define a new hook and activate it, you need to put the
15 necessary code into a python file which can be either imported or execfile()'d
15 necessary code into a python file which can be either imported or execfile()'d
16 from within your ipythonrc configuration.
16 from within your ipythonrc configuration.
17
17
18 For example, suppose that you have a module called 'myiphooks' in your
18 For example, suppose that you have a module called 'myiphooks' in your
19 PYTHONPATH, which contains the following definition:
19 PYTHONPATH, which contains the following definition:
20
20
21 import os
21 import os
22 import IPython.ipapi
22 import IPython.ipapi
23 ip = IPython.ipapi.get()
23 ip = IPython.ipapi.get()
24
24
25 def calljed(self,filename, linenum):
25 def calljed(self,filename, linenum):
26 "My editor hook calls the jed editor directly."
26 "My editor hook calls the jed editor directly."
27 print "Calling my own editor, jed ..."
27 print "Calling my own editor, jed ..."
28 os.system('jed +%d %s' % (linenum,filename))
28 os.system('jed +%d %s' % (linenum,filename))
29
29
30 ip.set_hook('editor', calljed)
30 ip.set_hook('editor', calljed)
31
31
32 You can then enable the functionality by doing 'import myiphooks'
32 You can then enable the functionality by doing 'import myiphooks'
33 somewhere in your configuration files or ipython command line.
33 somewhere in your configuration files or ipython command line.
34
34
35 $Id: hooks.py 1107 2006-01-30 19:02:20Z vivainio $"""
35 $Id: hooks.py 1156 2006-02-12 02:30:36Z fperez $"""
36
36
37 #*****************************************************************************
37 #*****************************************************************************
38 # Copyright (C) 2005 Fernando Perez. <fperez@colorado.edu>
38 # Copyright (C) 2005 Fernando Perez. <fperez@colorado.edu>
39 #
39 #
40 # Distributed under the terms of the BSD License. The full license is in
40 # Distributed under the terms of the BSD License. The full license is in
41 # the file COPYING, distributed as part of this software.
41 # the file COPYING, distributed as part of this software.
42 #*****************************************************************************
42 #*****************************************************************************
43
43
44 from IPython import Release
44 from IPython import Release
45 from IPython import ipapi
45 from IPython import ipapi
46 __author__ = '%s <%s>' % Release.authors['Fernando']
46 __author__ = '%s <%s>' % Release.authors['Fernando']
47 __license__ = Release.license
47 __license__ = Release.license
48 __version__ = Release.version
48 __version__ = Release.version
49
49
50 import os,bisect
50 import os,bisect
51 from genutils import Term
51 from genutils import Term
52 from pprint import pformat
52 from pprint import pformat
53
53
54 # List here all the default hooks. For now it's just the editor functions
54 # List here all the default hooks. For now it's just the editor functions
55 # but over time we'll move here all the public API for user-accessible things.
55 # but over time we'll move here all the public API for user-accessible things.
56 __all__ = ['editor', 'fix_error_editor', 'result_display',
56 __all__ = ['editor', 'fix_error_editor', 'result_display',
57 'input_prefilter', 'shutdown_hook', 'late_startup_hook']
57 'input_prefilter', 'shutdown_hook', 'late_startup_hook']
58
58
59 def editor(self,filename, linenum=None):
59 def editor(self,filename, linenum=None):
60 """Open the default editor at the given filename and linenumber.
60 """Open the default editor at the given filename and linenumber.
61
61
62 This is IPython's default editor hook, you can use it as an example to
62 This is IPython's default editor hook, you can use it as an example to
63 write your own modified one. To set your own editor function as the
63 write your own modified one. To set your own editor function as the
64 new editor hook, call ip.set_hook('editor',yourfunc)."""
64 new editor hook, call ip.set_hook('editor',yourfunc)."""
65
65
66 # IPython configures a default editor at startup by reading $EDITOR from
66 # IPython configures a default editor at startup by reading $EDITOR from
67 # the environment, and falling back on vi (unix) or notepad (win32).
67 # the environment, and falling back on vi (unix) or notepad (win32).
68 editor = self.rc.editor
68 editor = self.rc.editor
69
69
70 # marker for at which line to open the file (for existing objects)
70 # marker for at which line to open the file (for existing objects)
71 if linenum is None or editor=='notepad':
71 if linenum is None or editor=='notepad':
72 linemark = ''
72 linemark = ''
73 else:
73 else:
74 linemark = '+%d' % linenum
74 linemark = '+%d' % linenum
75 # Call the actual editor
75 # Call the actual editor
76 os.system('%s %s %s' % (editor,linemark,filename))
76 os.system('"%s" %s %s' % (editor,linemark,filename))
77
77
78 import tempfile
78 import tempfile
79 def fix_error_editor(self,filename,linenum,column,msg):
79 def fix_error_editor(self,filename,linenum,column,msg):
80 """Open the editor at the given filename, linenumber, column and
80 """Open the editor at the given filename, linenumber, column and
81 show an error message. This is used for correcting syntax errors.
81 show an error message. This is used for correcting syntax errors.
82 The current implementation only has special support for the VIM editor,
82 The current implementation only has special support for the VIM editor,
83 and falls back on the 'editor' hook if VIM is not used.
83 and falls back on the 'editor' hook if VIM is not used.
84
84
85 Call ip.set_hook('fix_error_editor',youfunc) to use your own function,
85 Call ip.set_hook('fix_error_editor',youfunc) to use your own function,
86 """
86 """
87 def vim_quickfix_file():
87 def vim_quickfix_file():
88 t = tempfile.NamedTemporaryFile()
88 t = tempfile.NamedTemporaryFile()
89 t.write('%s:%d:%d:%s\n' % (filename,linenum,column,msg))
89 t.write('%s:%d:%d:%s\n' % (filename,linenum,column,msg))
90 t.flush()
90 t.flush()
91 return t
91 return t
92 if os.path.basename(self.rc.editor) != 'vim':
92 if os.path.basename(self.rc.editor) != 'vim':
93 self.hooks.editor(filename,linenum)
93 self.hooks.editor(filename,linenum)
94 return
94 return
95 t = vim_quickfix_file()
95 t = vim_quickfix_file()
96 try:
96 try:
97 os.system('vim --cmd "set errorformat=%f:%l:%c:%m" -q ' + t.name)
97 os.system('vim --cmd "set errorformat=%f:%l:%c:%m" -q ' + t.name)
98 finally:
98 finally:
99 t.close()
99 t.close()
100
100
101
101
102 class CommandChainDispatcher:
102 class CommandChainDispatcher:
103 """ Dispatch calls to a chain of commands until some func can handle it
103 """ Dispatch calls to a chain of commands until some func can handle it
104
104
105 Usage: instantiate, execute "add" to add commands (with optional
105 Usage: instantiate, execute "add" to add commands (with optional
106 priority), execute normally via f() calling mechanism.
106 priority), execute normally via f() calling mechanism.
107
107
108 """
108 """
109 def __init__(self,commands=None):
109 def __init__(self,commands=None):
110 if commands is None:
110 if commands is None:
111 self.chain = []
111 self.chain = []
112 else:
112 else:
113 self.chain = commands
113 self.chain = commands
114
114
115
115
116 def __call__(self,*args, **kw):
116 def __call__(self,*args, **kw):
117 """ Command chain is called just like normal func.
117 """ Command chain is called just like normal func.
118
118
119 This will call all funcs in chain with the same args as were given to this
119 This will call all funcs in chain with the same args as were given to this
120 function, and return the result of first func that didn't raise
120 function, and return the result of first func that didn't raise
121 TryNext """
121 TryNext """
122
122
123 for prio,cmd in self.chain:
123 for prio,cmd in self.chain:
124 #print "prio",prio,"cmd",cmd #dbg
124 #print "prio",prio,"cmd",cmd #dbg
125 try:
125 try:
126 ret = cmd(*args, **kw)
126 ret = cmd(*args, **kw)
127 return ret
127 return ret
128 except ipapi.TryNext:
128 except ipapi.TryNext:
129 pass
129 pass
130
130
131 def __str__(self):
131 def __str__(self):
132 return str(self.chain)
132 return str(self.chain)
133
133
134 def add(self, func, priority=0):
134 def add(self, func, priority=0):
135 """ Add a func to the cmd chain with given priority """
135 """ Add a func to the cmd chain with given priority """
136 bisect.insort(self.chain,(priority,func))
136 bisect.insort(self.chain,(priority,func))
137
137
138 def result_display(self,arg):
138 def result_display(self,arg):
139 """ Default display hook.
139 """ Default display hook.
140
140
141 Called for displaying the result to the user.
141 Called for displaying the result to the user.
142 """
142 """
143
143
144 if self.rc.pprint:
144 if self.rc.pprint:
145 out = pformat(arg)
145 out = pformat(arg)
146 if '\n' in out:
146 if '\n' in out:
147 # So that multi-line strings line up with the left column of
147 # So that multi-line strings line up with the left column of
148 # the screen, instead of having the output prompt mess up
148 # the screen, instead of having the output prompt mess up
149 # their first line.
149 # their first line.
150 Term.cout.write('\n')
150 Term.cout.write('\n')
151 print >>Term.cout, out
151 print >>Term.cout, out
152 else:
152 else:
153 print >>Term.cout, arg
153 print >>Term.cout, arg
154 # the default display hook doesn't manipulate the value to put in history
154 # the default display hook doesn't manipulate the value to put in history
155 return None
155 return None
156
156
157 def input_prefilter(self,line):
157 def input_prefilter(self,line):
158 """ Default input prefilter
158 """ Default input prefilter
159
159
160 This returns the line as unchanged, so that the interpreter
160 This returns the line as unchanged, so that the interpreter
161 knows that nothing was done and proceeds with "classic" prefiltering
161 knows that nothing was done and proceeds with "classic" prefiltering
162 (%magics, !shell commands etc.).
162 (%magics, !shell commands etc.).
163
163
164 Note that leading whitespace is not passed to this hook. Prefilter
164 Note that leading whitespace is not passed to this hook. Prefilter
165 can't alter indentation.
165 can't alter indentation.
166
166
167 """
167 """
168 #print "attempt to rewrite",line #dbg
168 #print "attempt to rewrite",line #dbg
169 return line
169 return line
170
170
171 def shutdown_hook(self):
171 def shutdown_hook(self):
172 """ default shutdown hook
172 """ default shutdown hook
173
173
174 Typically, shotdown hooks should raise TryNext so all shutdown ops are done
174 Typically, shotdown hooks should raise TryNext so all shutdown ops are done
175 """
175 """
176
176
177 #print "default shutdown hook ok" # dbg
177 #print "default shutdown hook ok" # dbg
178 return
178 return
179
179
180 def late_startup_hook(self):
180 def late_startup_hook(self):
181 """ Executed after ipython has been constructed and configured
181 """ Executed after ipython has been constructed and configured
182
182
183 """
183 """
184 #print "default startup hook ok" # dbg No newline at end of file
184 #print "default startup hook ok" # dbg
1 NO CONTENT: modified file
NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now