Show More
@@ -202,7 +202,8 b' class HistoryManager(Configurable):' | |||
|
202 | 202 | (n,), raw=raw, output=output) |
|
203 | 203 | return reversed(list(cur)) |
|
204 | 204 | |
|
205 |
def get_hist_search(self, pattern="*", raw=True, |
|
|
205 | def get_hist_search(self, pattern="*", raw=True, search_raw=True, | |
|
206 | output=False): | |
|
206 | 207 | """Search the database using unix glob-style matching (wildcards * and |
|
207 | 208 | ?, escape using \). |
|
208 | 209 | |
@@ -210,7 +211,7 b' class HistoryManager(Configurable):' | |||
|
210 | 211 | ------- |
|
211 | 212 | An iterator over tuples: (session, line_number, command) |
|
212 | 213 | """ |
|
213 | tosearch = "source_raw" if raw else "source" | |
|
214 | tosearch = "source_raw" if search_raw else "source" | |
|
214 | 215 | if output: |
|
215 | 216 | tosearch = "history." + tosearch |
|
216 | 217 | self.writeout_cache() |
@@ -307,13 +308,15 b' class HistoryManager(Configurable):' | |||
|
307 | 308 | """ |
|
308 | 309 | if source_raw is None: |
|
309 | 310 | source_raw = source |
|
311 | source = source.rstrip() | |
|
312 | source_raw = source_raw.rstrip() | |
|
310 | 313 | |
|
311 | 314 | # do not store exit/quit commands |
|
312 | 315 | if source_raw.strip() in self._exit_commands: |
|
313 | 316 | return |
|
314 | 317 | |
|
315 |
self.input_hist_parsed.append(source |
|
|
316 |
self.input_hist_raw.append(source_raw |
|
|
318 | self.input_hist_parsed.append(source) | |
|
319 | self.input_hist_raw.append(source_raw) | |
|
317 | 320 | |
|
318 | 321 | self.db_input_cache.append((self.session_number, line_num, |
|
319 | 322 | source, source_raw)) |
@@ -556,59 +559,95 b' def magic_rep(self, arg):' | |||
|
556 | 559 | variable) to the next input prompt. Allows you to create elaborate command |
|
557 | 560 | lines without using copy-paste:: |
|
558 | 561 | |
|
559 |
|
|
|
560 |
|
|
|
561 |
|
|
|
562 |
|
|
|
563 |
|
|
|
562 | In[1]: l = ["hei", "vaan"] | |
|
563 | In[2]: "".join(l) | |
|
564 | Out[2]: heivaan | |
|
565 | In[3]: %rep | |
|
566 | In[4]: heivaan_ <== cursor blinking | |
|
564 | 567 | |
|
565 | 568 | %rep 45 |
|
566 | 569 | |
|
567 |
Place history line 45 |
|
|
568 | number. | |
|
570 | Place history line 45 on the next input prompt. Use %hist to find | |
|
571 | out the number. | |
|
569 | 572 | |
|
570 | 573 | %rep 1-4 6-7 3 |
|
571 | 574 | |
|
572 | Repeat the specified lines immediately. Input slice syntax is the same as | |
|
573 | in %macro and %save. | |
|
575 | Combine the specified lines into one cell, and place it on the next | |
|
576 | input prompt. History slice syntax is the same as in %macro and %save. | |
|
574 | 577 | |
|
575 | %rep foo | |
|
578 | %rep foo+bar | |
|
576 | 579 | |
|
577 | Place the most recent line that has the substring "foo" to next input. | |
|
578 | (e.g. 'svn ci -m foobar'). | |
|
580 | If foo+bar can be evaluated in the user namespace, the result is | |
|
581 | placed at the next input prompt. Otherwise, the history is searched | |
|
582 | for lines which contain that substring, and the most recent one is | |
|
583 | placed at the next input prompt. | |
|
579 | 584 | """ |
|
580 | ||
|
581 | opts,args = self.parse_options(arg,'',mode='list') | |
|
582 | if not args: # Last output | |
|
585 | if not arg: # Last output | |
|
583 | 586 | self.set_next_input(str(self.shell.user_ns["_"])) |
|
584 | 587 | return |
|
588 | # Get history range | |
|
589 | histlines = self.history_manager.get_hist_from_rangestr(arg) | |
|
590 | cmd = "\n".join(x[2] for x in histlines) | |
|
591 | if cmd: | |
|
592 | self.set_next_input(cmd.rstrip()) | |
|
593 | return | |
|
585 | 594 | |
|
586 | arg = " ".join(args) | |
|
587 | histlines = self.history_manager.get_hist_from_rangestr(arg, raw=False) | |
|
588 | histlines = [x[2] for x in histlines] | |
|
589 | ||
|
590 | if len(histlines) > 1: # Execute immediately | |
|
591 | histlines = "\n".join(histlines) | |
|
592 | print("=== Executing: ===") | |
|
593 | print(histlines) | |
|
594 | print("=== Output: ===") | |
|
595 | self.run_source(histlines, symbol="exec") | |
|
596 | ||
|
597 | elif len(histlines) == 1: # Editable input | |
|
598 | self.set_next_input(histlines[0].rstrip()) | |
|
599 | ||
|
600 | else: # Search for term - editable input | |
|
595 | try: # Variable in user namespace | |
|
596 | cmd = str(eval(arg, self.shell.user_ns)) | |
|
597 | except Exception: # Search for term in history | |
|
601 | 598 | histlines = self.history_manager.get_hist_search("*"+arg+"*") |
|
602 | 599 | for h in reversed([x[2] for x in histlines]): |
|
603 | 600 | if 'rep' in h: |
|
604 | 601 | continue |
|
605 | 602 | self.set_next_input(h.rstrip()) |
|
606 | 603 | return |
|
607 | print("Not found in history:", arg) | |
|
604 | else: | |
|
605 | self.set_next_input(cmd.rstrip()) | |
|
606 | print("Couldn't evaluate or find in history:", arg) | |
|
607 | ||
|
608 | def magic_rerun(self, parameter_s=''): | |
|
609 | """Re-run previous input | |
|
610 | ||
|
611 | By default, you can specify ranges of input history to be repeated | |
|
612 | (as with %hist). With no arguments, it will repeat the last line. | |
|
613 | ||
|
614 | Options: | |
|
615 | ||
|
616 | -l <n> : Repeat the last n lines of input, not including the | |
|
617 | current command. | |
|
618 | ||
|
619 | -g foo : Repeat the most recent line which contains foo | |
|
620 | """ | |
|
621 | opts, args = self.parse_options(parameter_s, 'l:g:', mode='string') | |
|
622 | if "l" in opts: # Last n lines | |
|
623 | n = int(opts['l']) + 1 | |
|
624 | hist = self.history_manager.get_hist_tail(n, raw=False) | |
|
625 | elif "g" in opts: # Search | |
|
626 | p = "*"+opts['g']+"*" | |
|
627 | hist = self.history_manager.get_hist_search(p, raw=False) | |
|
628 | hist = list(hist)[-2:] | |
|
629 | elif args: # Specify history ranges | |
|
630 | hist = self.history_manager.get_hist_from_rangestr(args) | |
|
631 | else: # Last line | |
|
632 | hist = self.history_manager.get_hist_tail(2, raw=False) | |
|
633 | hist = [x[2] for x in hist] | |
|
634 | if hist and parameter_s in hist[-1]: | |
|
635 | hist = hist[:-1] | |
|
636 | if not hist: | |
|
637 | print("No lines in history match specification") | |
|
638 | return | |
|
639 | histlines = "\n".join(hist) | |
|
640 | print("=== Executing: ===") | |
|
641 | print(histlines) | |
|
642 | print("=== Output: ===") | |
|
643 | self.run_source("\n".join(hist), symbol="exec") | |
|
608 | 644 | |
|
609 | 645 | |
|
610 | 646 | def init_ipython(ip): |
|
611 | 647 | ip.define_magic("rep", magic_rep) |
|
648 | ip.define_magic("recall", magic_rep) | |
|
649 | ip.define_magic("rerun", magic_rerun) | |
|
650 | ip.define_magic("r", magic_rerun) | |
|
612 | 651 | ip.define_magic("hist",magic_history) # Alternative name |
|
613 | 652 | ip.define_magic("history",magic_history) |
|
614 | 653 |
@@ -3029,38 +3029,6 b' Defaulting color scheme to \'NoColor\'"""' | |||
|
3029 | 3029 | if parameter_s: |
|
3030 | 3030 | return self.shell.getoutput(parameter_s) |
|
3031 | 3031 | |
|
3032 | def magic_r(self, parameter_s=''): | |
|
3033 | """Repeat previous input. | |
|
3034 | ||
|
3035 | Note: Consider using the more powerfull %rep instead! | |
|
3036 | ||
|
3037 | If given an argument, repeats the previous command which starts with | |
|
3038 | the same string, otherwise it just repeats the previous input. | |
|
3039 | ||
|
3040 | Shell escaped commands (with ! as first character) are not recognized | |
|
3041 | by this system, only pure python code and magic commands. | |
|
3042 | """ | |
|
3043 | ||
|
3044 | start = parameter_s.strip() | |
|
3045 | esc_magic = ESC_MAGIC | |
|
3046 | # Identify magic commands even if automagic is on (which means | |
|
3047 | # the in-memory version is different from that typed by the user). | |
|
3048 | if self.shell.automagic: | |
|
3049 | start_magic = esc_magic+start | |
|
3050 | else: | |
|
3051 | start_magic = start | |
|
3052 | # Look through the input history in reverse | |
|
3053 | for n in range(len(self.shell.history_manager.input_hist_parsed)-2,0,-1): | |
|
3054 | input = self.shell.history_manager.input_hist_parsed[n] | |
|
3055 | # skip plain 'r' lines so we don't recurse to infinity | |
|
3056 | if input != '_ip.magic("r")\n' and \ | |
|
3057 | (input.startswith(start) or input.startswith(start_magic)): | |
|
3058 | #print 'match',`input` # dbg | |
|
3059 | print 'Executing:',input, | |
|
3060 | self.shell.run_cell(input) | |
|
3061 | return | |
|
3062 | print 'No previous input matching `%s` found.' % start | |
|
3063 | ||
|
3064 | 3032 | |
|
3065 | 3033 | def magic_bookmark(self, parameter_s=''): |
|
3066 | 3034 | """Manage IPython's bookmark system. |
General Comments 0
You need to be logged in to leave comments.
Login now