##// END OF EJS Templates
%rep fix
vivainio -
Show More
@@ -1,139 +1,139 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 """ History related magics and functionality """
4 4
5 5 import IPython.ipapi
6 6 ip = IPython.ipapi.get()
7 7
8 8 import fnmatch
9 9
10 10 def magic_history(self, parameter_s = ''):
11 11 """Print input history (_i<n> variables), with most recent last.
12 12
13 13 %history -> print at most 40 inputs (some may be multi-line)\\
14 14 %history n -> print at most n inputs\\
15 15 %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\
16 16
17 17 Each input's number <n> is shown, and is accessible as the
18 18 automatically generated variable _i<n>. Multi-line statements are
19 19 printed starting at a new line for easy copy/paste.
20 20
21 21
22 22 Options:
23 23
24 24 -n: do NOT print line numbers. This is useful if you want to get a
25 25 printout of many lines which can be directly pasted into a text
26 26 editor.
27 27
28 28 This feature is only available if numbered prompts are in use.
29 29
30 30 -t: print the 'translated' history, as IPython understands it. IPython
31 31 filters your input and converts it all into valid Python source before
32 32 executing it (things like magics or aliases are turned into function
33 33 calls, for example). With this option, you'll see the native history
34 34 instead of the user-entered version: '%cd /' will be seen as
35 35 '_ip.magic("%cd /")' instead of '%cd /'.
36 36
37 37 -g: treat the arg as a pattern to grep for in (full) history
38 38
39 39 """
40 40
41 41 shell = self.shell
42 42 if not shell.outputcache.do_full_cache:
43 43 print 'This feature is only available if numbered prompts are in use.'
44 44 return
45 45 opts,args = self.parse_options(parameter_s,'gnt',mode='list')
46 46
47 47 if not opts.has_key('t'):
48 48 input_hist = shell.input_hist_raw
49 49 else:
50 50 input_hist = shell.input_hist
51 51
52 52 default_length = 40
53 53 pattern = None
54 54 if opts.has_key('g'):
55 55 init = 1
56 56 final = len(input_hist)
57 57 head, pattern = parameter_s.split(None,1)
58 58 pattern = "*" + pattern + "*"
59 59 elif len(args) == 0:
60 60 final = len(input_hist)
61 61 init = max(1,final-default_length)
62 62 elif len(args) == 1:
63 63 final = len(input_hist)
64 64 init = max(1,final-int(args[0]))
65 65 elif len(args) == 2:
66 66 init,final = map(int,args)
67 67 else:
68 68 warn('%hist takes 0, 1 or 2 arguments separated by spaces.')
69 69 print self.magic_hist.__doc__
70 70 return
71 71 width = len(str(final))
72 72 line_sep = ['','\n']
73 73 print_nums = not opts.has_key('n')
74 74 for in_num in range(init,final):
75 75 inline = input_hist[in_num]
76 76 if pattern is not None and not fnmatch.fnmatch(inline, pattern):
77 77 continue
78 78
79 79 multiline = int(inline.count('\n') > 1)
80 80 if print_nums:
81 81 print '%s:%s' % (str(in_num).ljust(width),line_sep[multiline]),
82 82 print inline,
83 83
84 84 ip.expose_magic("history",magic_history)
85 85
86 86 def magic_hist(self, parameter_s=''):
87 87 """Alternate name for %history."""
88 88 return self.magic_history(parameter_s)
89 89
90 90 ip.expose_magic("hist",magic_hist)
91 91
92 92 def rep_f(self, arg):
93 93 r""" Repeat a command, or get command to input line for editing
94 94
95 95 - %rep (no arguments):
96 96
97 97 Place a string version of last input to the next input prompt. Allows you
98 98 to create elaborate command lines without using copy-paste::
99 99
100 100 $ l = ["hei", "vaan"]
101 101 $ "".join(l)
102 102 ==> heivaan
103 103 $ %rep
104 104 $ heivaan_ <== cursor blinking
105 105
106 106 %rep 45
107 107
108 108 Place history line 45 to next input prompt. Use %hist to find out the number.
109 109
110 110 %rep 1-4 6-7 3
111 111
112 112 Repeat the specified lines immediately. Input slice syntax is the same as
113 113 in %macro and %save.
114 114
115 115 """
116 116
117 117
118 118 opts,args = self.parse_options(arg,'',mode='list')
119 print args
119
120 120 if not args:
121 121 ip.set_next_input(str(ip.user_ns["_"]))
122 122 return
123 123
124 if len(arg) == 1:
124 if len(args) == 1:
125 125 try:
126 126 num = int(args[0])
127 127 ip.set_next_input(str(ip.IP.input_hist_raw[num]).rstrip())
128 128 return
129 129 except ValueError:
130 130 pass
131 131
132 132
133 133 lines = self.extract_input_slices(args, True)
134 134 print "lines",lines
135 135 ip.runlines(lines)
136 136
137 137 ip.expose_magic("rep",rep_f)
138 138
139 139
General Comments 0
You need to be logged in to leave comments. Login now