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