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