##// END OF EJS Templates
fix %rep docs
vivainio -
Show More
@@ -1,239 +1,241 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 """ History related magics and functionality """
4 4
5 5 import fnmatch
6 6
7 7 def magic_history(self, parameter_s = ''):
8 8 """Print input history (_i<n> variables), with most recent last.
9 9
10 10 %history -> print at most 40 inputs (some may be multi-line)\\
11 11 %history n -> print at most n inputs\\
12 12 %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\
13 13
14 14 Each input's number <n> is shown, and is accessible as the
15 15 automatically generated variable _i<n>. Multi-line statements are
16 16 printed starting at a new line for easy copy/paste.
17 17
18 18
19 19 Options:
20 20
21 21 -n: do NOT print line numbers. This is useful if you want to get a
22 22 printout of many lines which can be directly pasted into a text
23 23 editor.
24 24
25 25 This feature is only available if numbered prompts are in use.
26 26
27 27 -t: (default) print the 'translated' history, as IPython understands it.
28 28 IPython filters your input and converts it all into valid Python source
29 29 before executing it (things like magics or aliases are turned into
30 30 function calls, for example). With this option, you'll see the native
31 31 history instead of the user-entered version: '%cd /' will be seen as
32 32 '_ip.magic("%cd /")' instead of '%cd /'.
33 33
34 34 -r: print the 'raw' history, i.e. the actual commands you typed.
35 35
36 36 -g: treat the arg as a pattern to grep for in (full) history.
37 37 This includes the "shadow history" (almost all commands ever written).
38 38 Use '%hist -g' to show full shadow history (may be very long).
39 39 In shadow history, every index nuwber starts with 0.
40 40
41 41
42 42 """
43 43
44 44 ip = self.api
45 45 shell = self.shell
46 46 if not shell.outputcache.do_full_cache:
47 47 print 'This feature is only available if numbered prompts are in use.'
48 48 return
49 49 opts,args = self.parse_options(parameter_s,'gntsr',mode='list')
50 50
51 51 if opts.has_key('t'):
52 52 input_hist = shell.input_hist
53 53 elif opts.has_key('r'):
54 54 input_hist = shell.input_hist_raw
55 55 else:
56 56 input_hist = shell.input_hist
57 57
58 58
59 59 default_length = 40
60 60 pattern = None
61 61 if opts.has_key('g'):
62 62 init = 1
63 63 final = len(input_hist)
64 64 parts = parameter_s.split(None,1)
65 65 if len(parts) == 1:
66 66 parts += '*'
67 67 head, pattern = parts
68 68 pattern = "*" + pattern + "*"
69 69 elif len(args) == 0:
70 70 final = len(input_hist)
71 71 init = max(1,final-default_length)
72 72 elif len(args) == 1:
73 73 final = len(input_hist)
74 74 init = max(1,final-int(args[0]))
75 75 elif len(args) == 2:
76 76 init,final = map(int,args)
77 77 else:
78 78 warn('%hist takes 0, 1 or 2 arguments separated by spaces.')
79 79 print self.magic_hist.__doc__
80 80 return
81 81 width = len(str(final))
82 82 line_sep = ['','\n']
83 83 print_nums = not opts.has_key('n')
84 84
85 85 found = False
86 86 if pattern is not None:
87 87 sh = ip.IP.shadowhist.all()
88 88 for idx, s in sh:
89 89 if fnmatch.fnmatch(s, pattern):
90 90 print "0%d: %s" %(idx, s)
91 91 found = True
92 92
93 93 if found:
94 94 print "==="
95 95 print "^shadow history ends, fetch by %rep <number> (must start with 0)"
96 96 print "=== start of normal history ==="
97 97
98 98 for in_num in range(init,final):
99 99 inline = input_hist[in_num]
100 100 if pattern is not None and not fnmatch.fnmatch(inline, pattern):
101 101 continue
102 102
103 103 multiline = int(inline.count('\n') > 1)
104 104 if print_nums:
105 105 print '%s:%s' % (str(in_num).ljust(width),line_sep[multiline]),
106 106 print inline,
107 107
108 108
109 109
110 110 def magic_hist(self, parameter_s=''):
111 111 """Alternate name for %history."""
112 112 return self.magic_history(parameter_s)
113 113
114 114
115 115
116 116 def rep_f(self, arg):
117 117 r""" Repeat a command, or get command to input line for editing
118 118
119 119 - %rep (no arguments):
120 120
121 Place a string version of last input to the next input prompt. Allows you
122 to create elaborate command lines without using copy-paste::
121 Place a string version of last computation result (stored in the special '_'
122 variable) to the next input prompt. Allows you to create elaborate command
123 lines without using copy-paste::
123 124
124 125 $ l = ["hei", "vaan"]
125 126 $ "".join(l)
126 127 ==> heivaan
127 128 $ %rep
128 129 $ heivaan_ <== cursor blinking
129 130
130 131 %rep 45
131 132
132 Place history line 45 to next input prompt. Use %hist to find out the number.
133 Place history line 45 to next input prompt. Use %hist to find out the
134 number.
133 135
134 136 %rep 1-4 6-7 3
135 137
136 138 Repeat the specified lines immediately. Input slice syntax is the same as
137 139 in %macro and %save.
138 140
139 141 %rep foo
140 142
141 143 Place the most recent line that has the substring "foo" to next input.
142 144 (e.g. 'svn ci -m foobar').
143 145
144 146 """
145 147
146 148
147 149 opts,args = self.parse_options(arg,'',mode='list')
148 150 ip = self.api
149 151 if not args:
150 152 ip.set_next_input(str(ip.user_ns["_"]))
151 153 return
152 154
153 155 if len(args) == 1 and not '-' in args[0]:
154 156 arg = args[0]
155 157 if len(arg) > 1 and arg.startswith('0'):
156 158 # get from shadow hist
157 159 num = int(arg[1:])
158 160 line = self.shadowhist.get(num)
159 161 ip.set_next_input(str(line))
160 162 return
161 163 try:
162 164 num = int(args[0])
163 165 ip.set_next_input(str(ip.IP.input_hist_raw[num]).rstrip())
164 166 return
165 167 except ValueError:
166 168 pass
167 169
168 170 for h in reversed(self.shell.input_hist_raw):
169 171 if 'rep' in h:
170 172 continue
171 173 if fnmatch.fnmatch(h,'*' + arg + '*'):
172 174 ip.set_next_input(str(h).rstrip())
173 175 return
174 176
175 177
176 178 try:
177 179 lines = self.extract_input_slices(args, True)
178 180 print "lines",lines
179 181 ip.runlines(lines)
180 182 except ValueError:
181 183 print "Not found in recent history:", args
182 184
183 185
184 186
185 187 _sentinel = object()
186 188
187 189 class ShadowHist:
188 190 def __init__(self,db):
189 191 # cmd => idx mapping
190 192 self.curidx = 0
191 193 self.db = db
192 194
193 195 def inc_idx(self):
194 196 idx = self.db.get('shadowhist_idx', 1)
195 197 self.db['shadowhist_idx'] = idx + 1
196 198 return idx
197 199
198 200 def add(self, ent):
199 201 old = self.db.hget('shadowhist', ent, _sentinel)
200 202 if old is not _sentinel:
201 203 return
202 204 newidx = self.inc_idx()
203 205 #print "new",newidx # dbg
204 206 self.db.hset('shadowhist',ent, newidx)
205 207
206 208 def all(self):
207 209 d = self.db.hdict('shadowhist')
208 210 items = [(i,s) for (s,i) in d.items()]
209 211 items.sort()
210 212 return items
211 213
212 214 def get(self, idx):
213 215 all = self.all()
214 216
215 217 for k, v in all:
216 218 #print k,v
217 219 if k == idx:
218 220 return v
219 221
220 222 def test_shist():
221 223 from IPython.Extensions import pickleshare
222 224 db = pickleshare.PickleShareDB('~/shist')
223 225 s = ShadowHist(db)
224 226 s.add('hello')
225 227 s.add('world')
226 228 s.add('hello')
227 229 s.add('hello')
228 230 s.add('karhu')
229 231 print "all",s.all()
230 232 print s.get(2)
231 233
232 234 def init_ipython(ip):
233 235 ip.expose_magic("rep",rep_f)
234 236 ip.expose_magic("hist",magic_hist)
235 237 ip.expose_magic("history",magic_history)
236 238
237 239 import ipy_completers
238 240 ipy_completers.quick_completer('%hist' ,'-g -t -r -n')
239 241 #test_shist()
General Comments 0
You need to be logged in to leave comments. Login now