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