##// END OF EJS Templates
Change %history to default to 'raw' history.
Fernando Perez -
Show More
@@ -1,278 +1,279 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """ History related magics and functionality """
2 """ History related magics and functionality """
3
3
4 # Stdlib imports
4 # Stdlib imports
5 import fnmatch
5 import fnmatch
6 import os
6 import os
7
7
8 import IPython.utils.io
8 import IPython.utils.io
9 from IPython.utils.io import ask_yes_no
9 from IPython.utils.io import ask_yes_no
10 from IPython.utils.warn import warn
10 from IPython.utils.warn import warn
11 from IPython.core import ipapi
11 from IPython.core import ipapi
12
12
13 def magic_history(self, parameter_s = ''):
13 def magic_history(self, parameter_s = ''):
14 """Print input history (_i<n> variables), with most recent last.
14 """Print input history (_i<n> variables), with most recent last.
15
15
16 %history -> print at most 40 inputs (some may be multi-line)\\
16 %history -> print at most 40 inputs (some may be multi-line)\\
17 %history n -> print at most n inputs\\
17 %history n -> print at most n inputs\\
18 %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\
18 %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\
19
19
20 By default, input history is printed without line numbers so it can be
20 By default, input history is printed without line numbers so it can be
21 directly pasted into an editor.
21 directly pasted into an editor.
22
22
23 With -n, each input's number <n> is shown, and is accessible as the
23 With -n, each input's number <n> is shown, and is accessible as the
24 automatically generated variable _i<n> as well as In[<n>]. Multi-line
24 automatically generated variable _i<n> as well as In[<n>]. Multi-line
25 statements are printed starting at a new line for easy copy/paste.
25 statements are printed starting at a new line for easy copy/paste.
26
26
27 Options:
27 Options:
28
28
29 -n: print line numbers for each input.
29 -n: print line numbers for each input.
30 This feature is only available if numbered prompts are in use.
30 This feature is only available if numbered prompts are in use.
31
31
32 -o: also print outputs for each input.
32 -o: also print outputs for each input.
33
33
34 -p: print classic '>>>' python prompts before each input. This is useful
34 -p: print classic '>>>' python prompts before each input. This is useful
35 for making documentation, and in conjunction with -o, for producing
35 for making documentation, and in conjunction with -o, for producing
36 doctest-ready output.
36 doctest-ready output.
37
37
38 -t: (default) print the 'translated' history, as IPython understands it.
38 -r: (default) print the 'raw' history, i.e. the actual commands you typed.
39 IPython filters your input and converts it all into valid Python source
40 before executing it (things like magics or aliases are turned into
41 function calls, for example). With this option, you'll see the native
42 history instead of the user-entered version: '%cd /' will be seen as
43 '_ip.magic("%cd /")' instead of '%cd /'.
44
39
45 -r: print the 'raw' history, i.e. the actual commands you typed.
40 -t: print the 'translated' history, as IPython understands it. IPython
41 filters your input and converts it all into valid Python source before
42 executing it (things like magics or aliases are turned into function
43 calls, for example). With this option, you'll see the native history
44 instead of the user-entered version: '%cd /' will be seen as
45 'get_ipython().magic("%cd /")' instead of '%cd /'.
46
46
47 -g: treat the arg as a pattern to grep for in (full) history.
47 -g: treat the arg as a pattern to grep for in (full) history.
48 This includes the "shadow history" (almost all commands ever written).
48 This includes the "shadow history" (almost all commands ever written).
49 Use '%hist -g' to show full shadow history (may be very long).
49 Use '%hist -g' to show full shadow history (may be very long).
50 In shadow history, every index nuwber starts with 0.
50 In shadow history, every index nuwber starts with 0.
51
51
52 -f FILENAME: instead of printing the output to the screen, redirect it to
52 -f FILENAME: instead of printing the output to the screen, redirect it to
53 the given file. The file is always overwritten, though IPython asks for
53 the given file. The file is always overwritten, though IPython asks for
54 confirmation first if it already exists.
54 confirmation first if it already exists.
55 """
55 """
56
56
57 if not self.displayhook.do_full_cache:
57 if not self.displayhook.do_full_cache:
58 print 'This feature is only available if numbered prompts are in use.'
58 print 'This feature is only available if numbered prompts are in use.'
59 return
59 return
60 opts,args = self.parse_options(parameter_s,'gnoptsrf:',mode='list')
60 opts,args = self.parse_options(parameter_s,'gnoptsrf:',mode='list')
61
61
62 # Check if output to specific file was requested.
62 # Check if output to specific file was requested.
63 try:
63 try:
64 outfname = opts['f']
64 outfname = opts['f']
65 except KeyError:
65 except KeyError:
66 outfile = IPython.utils.io.Term.cout # default
66 outfile = IPython.utils.io.Term.cout # default
67 # We don't want to close stdout at the end!
67 # We don't want to close stdout at the end!
68 close_at_end = False
68 close_at_end = False
69 else:
69 else:
70 if os.path.exists(outfname):
70 if os.path.exists(outfname):
71 if not ask_yes_no("File %r exists. Overwrite?" % outfname):
71 if not ask_yes_no("File %r exists. Overwrite?" % outfname):
72 print 'Aborting.'
72 print 'Aborting.'
73 return
73 return
74
74
75 outfile = open(outfname,'w')
75 outfile = open(outfname,'w')
76 close_at_end = True
76 close_at_end = True
77
77
78 if 't' in opts:
78 if 't' in opts:
79 input_hist = self.input_hist
79 input_hist = self.input_hist
80 elif 'r' in opts:
80 elif 'r' in opts:
81 input_hist = self.input_hist_raw
81 input_hist = self.input_hist_raw
82 else:
82 else:
83 input_hist = self.input_hist
83 # Raw history is the default
84 input_hist = self.input_hist_raw
84
85
85 default_length = 40
86 default_length = 40
86 pattern = None
87 pattern = None
87 if 'g' in opts:
88 if 'g' in opts:
88 init = 1
89 init = 1
89 final = len(input_hist)
90 final = len(input_hist)
90 parts = parameter_s.split(None, 1)
91 parts = parameter_s.split(None, 1)
91 if len(parts) == 1:
92 if len(parts) == 1:
92 parts += '*'
93 parts += '*'
93 head, pattern = parts
94 head, pattern = parts
94 pattern = "*" + pattern + "*"
95 pattern = "*" + pattern + "*"
95 elif len(args) == 0:
96 elif len(args) == 0:
96 final = len(input_hist)-1
97 final = len(input_hist)-1
97 init = max(1,final-default_length)
98 init = max(1,final-default_length)
98 elif len(args) == 1:
99 elif len(args) == 1:
99 final = len(input_hist)
100 final = len(input_hist)
100 init = max(1, final-int(args[0]))
101 init = max(1, final-int(args[0]))
101 elif len(args) == 2:
102 elif len(args) == 2:
102 init, final = map(int, args)
103 init, final = map(int, args)
103 else:
104 else:
104 warn('%hist takes 0, 1 or 2 arguments separated by spaces.')
105 warn('%hist takes 0, 1 or 2 arguments separated by spaces.')
105 print >> IPython.utils.io.Term.cout, self.magic_hist.__doc__
106 print >> IPython.utils.io.Term.cout, self.magic_hist.__doc__
106 return
107 return
107
108
108 width = len(str(final))
109 width = len(str(final))
109 line_sep = ['','\n']
110 line_sep = ['','\n']
110 print_nums = 'n' in opts
111 print_nums = 'n' in opts
111 print_outputs = 'o' in opts
112 print_outputs = 'o' in opts
112 pyprompts = 'p' in opts
113 pyprompts = 'p' in opts
113
114
114 found = False
115 found = False
115 if pattern is not None:
116 if pattern is not None:
116 sh = self.shadowhist.all()
117 sh = self.shadowhist.all()
117 for idx, s in sh:
118 for idx, s in sh:
118 if fnmatch.fnmatch(s, pattern):
119 if fnmatch.fnmatch(s, pattern):
119 print >> outfile, "0%d: %s" %(idx, s)
120 print >> outfile, "0%d: %s" %(idx, s)
120 found = True
121 found = True
121
122
122 if found:
123 if found:
123 print >> outfile, "==="
124 print >> outfile, "==="
124 print >> outfile, \
125 print >> outfile, \
125 "shadow history ends, fetch by %rep <number> (must start with 0)"
126 "shadow history ends, fetch by %rep <number> (must start with 0)"
126 print >> outfile, "=== start of normal history ==="
127 print >> outfile, "=== start of normal history ==="
127
128
128 for in_num in range(init,final):
129 for in_num in range(init,final):
129 inline = input_hist[in_num]
130 inline = input_hist[in_num]
130 if pattern is not None and not fnmatch.fnmatch(inline, pattern):
131 if pattern is not None and not fnmatch.fnmatch(inline, pattern):
131 continue
132 continue
132
133
133 multiline = int(inline.count('\n') > 1)
134 multiline = int(inline.count('\n') > 1)
134 if print_nums:
135 if print_nums:
135 print >> outfile, \
136 print >> outfile, \
136 '%s:%s' % (str(in_num).ljust(width), line_sep[multiline]),
137 '%s:%s' % (str(in_num).ljust(width), line_sep[multiline]),
137 if pyprompts:
138 if pyprompts:
138 print >> outfile, '>>>',
139 print >> outfile, '>>>',
139 if multiline:
140 if multiline:
140 lines = inline.splitlines()
141 lines = inline.splitlines()
141 print >> outfile, '\n... '.join(lines)
142 print >> outfile, '\n... '.join(lines)
142 print >> outfile, '... '
143 print >> outfile, '... '
143 else:
144 else:
144 print >> outfile, inline,
145 print >> outfile, inline,
145 else:
146 else:
146 print >> outfile, inline,
147 print >> outfile, inline,
147 if print_outputs:
148 if print_outputs:
148 output = self.shell.user_ns['Out'].get(in_num)
149 output = self.shell.user_ns['Out'].get(in_num)
149 if output is not None:
150 if output is not None:
150 print >> outfile, repr(output)
151 print >> outfile, repr(output)
151
152
152 if close_at_end:
153 if close_at_end:
153 outfile.close()
154 outfile.close()
154
155
155
156
156 def magic_hist(self, parameter_s=''):
157 def magic_hist(self, parameter_s=''):
157 """Alternate name for %history."""
158 """Alternate name for %history."""
158 return self.magic_history(parameter_s)
159 return self.magic_history(parameter_s)
159
160
160
161
161 def rep_f(self, arg):
162 def rep_f(self, arg):
162 r""" Repeat a command, or get command to input line for editing
163 r""" Repeat a command, or get command to input line for editing
163
164
164 - %rep (no arguments):
165 - %rep (no arguments):
165
166
166 Place a string version of last computation result (stored in the special '_'
167 Place a string version of last computation result (stored in the special '_'
167 variable) to the next input prompt. Allows you to create elaborate command
168 variable) to the next input prompt. Allows you to create elaborate command
168 lines without using copy-paste::
169 lines without using copy-paste::
169
170
170 $ l = ["hei", "vaan"]
171 $ l = ["hei", "vaan"]
171 $ "".join(l)
172 $ "".join(l)
172 ==> heivaan
173 ==> heivaan
173 $ %rep
174 $ %rep
174 $ heivaan_ <== cursor blinking
175 $ heivaan_ <== cursor blinking
175
176
176 %rep 45
177 %rep 45
177
178
178 Place history line 45 to next input prompt. Use %hist to find out the
179 Place history line 45 to next input prompt. Use %hist to find out the
179 number.
180 number.
180
181
181 %rep 1-4 6-7 3
182 %rep 1-4 6-7 3
182
183
183 Repeat the specified lines immediately. Input slice syntax is the same as
184 Repeat the specified lines immediately. Input slice syntax is the same as
184 in %macro and %save.
185 in %macro and %save.
185
186
186 %rep foo
187 %rep foo
187
188
188 Place the most recent line that has the substring "foo" to next input.
189 Place the most recent line that has the substring "foo" to next input.
189 (e.g. 'svn ci -m foobar').
190 (e.g. 'svn ci -m foobar').
190 """
191 """
191
192
192 opts,args = self.parse_options(arg,'',mode='list')
193 opts,args = self.parse_options(arg,'',mode='list')
193 if not args:
194 if not args:
194 self.set_next_input(str(self.user_ns["_"]))
195 self.set_next_input(str(self.user_ns["_"]))
195 return
196 return
196
197
197 if len(args) == 1 and not '-' in args[0]:
198 if len(args) == 1 and not '-' in args[0]:
198 arg = args[0]
199 arg = args[0]
199 if len(arg) > 1 and arg.startswith('0'):
200 if len(arg) > 1 and arg.startswith('0'):
200 # get from shadow hist
201 # get from shadow hist
201 num = int(arg[1:])
202 num = int(arg[1:])
202 line = self.shadowhist.get(num)
203 line = self.shadowhist.get(num)
203 self.set_next_input(str(line))
204 self.set_next_input(str(line))
204 return
205 return
205 try:
206 try:
206 num = int(args[0])
207 num = int(args[0])
207 self.set_next_input(str(self.input_hist_raw[num]).rstrip())
208 self.set_next_input(str(self.input_hist_raw[num]).rstrip())
208 return
209 return
209 except ValueError:
210 except ValueError:
210 pass
211 pass
211
212
212 for h in reversed(self.input_hist_raw):
213 for h in reversed(self.input_hist_raw):
213 if 'rep' in h:
214 if 'rep' in h:
214 continue
215 continue
215 if fnmatch.fnmatch(h,'*' + arg + '*'):
216 if fnmatch.fnmatch(h,'*' + arg + '*'):
216 self.set_next_input(str(h).rstrip())
217 self.set_next_input(str(h).rstrip())
217 return
218 return
218
219
219 try:
220 try:
220 lines = self.extract_input_slices(args, True)
221 lines = self.extract_input_slices(args, True)
221 print "lines",lines
222 print "lines",lines
222 self.runlines(lines)
223 self.runlines(lines)
223 except ValueError:
224 except ValueError:
224 print "Not found in recent history:", args
225 print "Not found in recent history:", args
225
226
226
227
227 _sentinel = object()
228 _sentinel = object()
228
229
229 class ShadowHist(object):
230 class ShadowHist(object):
230 def __init__(self,db):
231 def __init__(self,db):
231 # cmd => idx mapping
232 # cmd => idx mapping
232 self.curidx = 0
233 self.curidx = 0
233 self.db = db
234 self.db = db
234 self.disabled = False
235 self.disabled = False
235
236
236 def inc_idx(self):
237 def inc_idx(self):
237 idx = self.db.get('shadowhist_idx', 1)
238 idx = self.db.get('shadowhist_idx', 1)
238 self.db['shadowhist_idx'] = idx + 1
239 self.db['shadowhist_idx'] = idx + 1
239 return idx
240 return idx
240
241
241 def add(self, ent):
242 def add(self, ent):
242 if self.disabled:
243 if self.disabled:
243 return
244 return
244 try:
245 try:
245 old = self.db.hget('shadowhist', ent, _sentinel)
246 old = self.db.hget('shadowhist', ent, _sentinel)
246 if old is not _sentinel:
247 if old is not _sentinel:
247 return
248 return
248 newidx = self.inc_idx()
249 newidx = self.inc_idx()
249 #print "new",newidx # dbg
250 #print "new",newidx # dbg
250 self.db.hset('shadowhist',ent, newidx)
251 self.db.hset('shadowhist',ent, newidx)
251 except:
252 except:
252 ipapi.get().showtraceback()
253 ipapi.get().showtraceback()
253 print "WARNING: disabling shadow history"
254 print "WARNING: disabling shadow history"
254 self.disabled = True
255 self.disabled = True
255
256
256 def all(self):
257 def all(self):
257 d = self.db.hdict('shadowhist')
258 d = self.db.hdict('shadowhist')
258 items = [(i,s) for (s,i) in d.items()]
259 items = [(i,s) for (s,i) in d.items()]
259 items.sort()
260 items.sort()
260 return items
261 return items
261
262
262 def get(self, idx):
263 def get(self, idx):
263 all = self.all()
264 all = self.all()
264
265
265 for k, v in all:
266 for k, v in all:
266 #print k,v
267 #print k,v
267 if k == idx:
268 if k == idx:
268 return v
269 return v
269
270
270
271
271 def init_ipython(ip):
272 def init_ipython(ip):
272 ip.define_magic("rep",rep_f)
273 ip.define_magic("rep",rep_f)
273 ip.define_magic("hist",magic_hist)
274 ip.define_magic("hist",magic_hist)
274 ip.define_magic("history",magic_history)
275 ip.define_magic("history",magic_history)
275
276
276 # XXX - ipy_completers are in quarantine, need to be updated to new apis
277 # XXX - ipy_completers are in quarantine, need to be updated to new apis
277 #import ipy_completers
278 #import ipy_completers
278 #ipy_completers.quick_completer('%hist' ,'-g -t -r -n')
279 #ipy_completers.quick_completer('%hist' ,'-g -t -r -n')
General Comments 0
You need to be logged in to leave comments. Login now