##// END OF EJS Templates
Initial fix for magic %rep function (was broken by new history system). Needs a bit of further thought.
Thomas Kluyver -
Show More
@@ -372,6 +372,8 b' def extract_hist_ranges(ranges_str):'
372 """
372 """
373 for range_str in ranges_str.split():
373 for range_str in ranges_str.split():
374 rmatch = range_re.match(range_str)
374 rmatch = range_re.match(range_str)
375 if not rmatch:
376 continue
375 start = int(rmatch.group("start"))
377 start = int(rmatch.group("start"))
376 end = rmatch.group("end")
378 end = rmatch.group("end")
377 end = int(end) if end else start+1 # If no end specified, get (a, a+1)
379 end = int(end) if end else start+1 # If no end specified, get (a, a+1)
@@ -537,11 +539,8 b" def magic_history(self, parameter_s = ''):"
537 if close_at_end:
539 if close_at_end:
538 outfile.close()
540 outfile.close()
539
541
540 # %hist is an alternative name
541 magic_hist = magic_history
542
543
542
544 def rep_f(self, arg):
543 def magic_rep(self, arg):
545 r""" Repeat a command, or get command to input line for editing
544 r""" Repeat a command, or get command to input line for editing
546
545
547 - %rep (no arguments):
546 - %rep (no arguments):
@@ -573,43 +572,37 b' def rep_f(self, arg):'
573 """
572 """
574
573
575 opts,args = self.parse_options(arg,'',mode='list')
574 opts,args = self.parse_options(arg,'',mode='list')
576 if not args:
575 if not args: # Last output
577 self.set_next_input(str(self.shell.user_ns["_"]))
576 self.set_next_input(str(self.shell.user_ns["_"]))
578 return
577 return
579
578
580 if len(args) == 1 and not '-' in args[0]:
579 arg = " ".join(args)
581 arg = args[0]
580 histlines = self.history_manager.get_hist_from_rangestr(arg, raw=False)
582 if len(arg) > 1 and arg.startswith('0'):
581 histlines = [x[2] for x in histlines]
583 # get from shadow hist
582
584 num = int(arg[1:])
583 if len(histlines) > 1: # Execute immediately
585 line = self.shell.shadowhist.get(num)
584 histlines = "\n".join(histlines)
586 self.set_next_input(str(line))
585 print("=== Executing: ===")
587 return
586 print(histlines)
588 try:
587 print("=== Output: ===")
589 num = int(args[0])
588 self.run_source(histlines, symbol="exec")
590 self.set_next_input(str(self.shell.input_hist_raw[num]).rstrip())
589
591 return
590 elif len(histlines) == 1: # Editable input
592 except ValueError:
591 self.set_next_input(histlines[0].rstrip())
593 pass
592
594
593 else: # Search for term - editable input
595 for h in reversed(self.shell.input_hist_raw):
594 histlines = self.history_manager.get_hist_search("*"+arg+"*")
595 for h in reversed([x[2] for x in histlines]):
596 if 'rep' in h:
596 if 'rep' in h:
597 continue
597 continue
598 if fnmatch.fnmatch(h,'*' + arg + '*'):
598 self.set_next_input(h.rstrip())
599 self.set_next_input(str(h).rstrip())
599 return
600 return
600 print("Not found in history:", arg)
601
602 try:
603 lines = self.extract_input_slices(args, True)
604 print("lines", lines)
605 self.run_cell(lines)
606 except ValueError:
607 print("Not found in recent history:", args)
608
601
609
602
610 def init_ipython(ip):
603 def init_ipython(ip):
611 ip.define_magic("rep",rep_f)
604 ip.define_magic("rep", magic_rep)
612 ip.define_magic("hist",magic_hist)
605 ip.define_magic("hist",magic_history) # Alternative name
613 ip.define_magic("history",magic_history)
606 ip.define_magic("history",magic_history)
614
607
615 # XXX - ipy_completers are in quarantine, need to be updated to new apis
608 # XXX - ipy_completers are in quarantine, need to be updated to new apis
General Comments 0
You need to be logged in to leave comments. Login now