MCPcopy Index your code
hub / github.com/ipython/ipython / recall

Method recall

IPython/core/magics/history.py:252–308  ·  view source on GitHub ↗

r"""Repeat a command, or get command to input line for editing. %recall and %rep are equivalent. - %recall (no arguments): Place a string version of last computation result (stored in the special '_' variable) to the next input prompt. Allows you to create

(self, arg)

Source from the content-addressed store, hash-verified

250
251 @line_magic
252 def recall(self, arg):
253 r"""Repeat a command, or get command to input line for editing.
254
255 %recall and %rep are equivalent.
256
257 - %recall (no arguments):
258
259 Place a string version of last computation result (stored in the
260 special '_' variable) to the next input prompt. Allows you to create
261 elaborate command lines without using copy-paste::
262
263 In[1]: l = ["hei", "vaan"]
264 In[2]: "".join(l)
265 Out[2]: heivaan
266 In[3]: %recall
267 In[4]: heivaan_ <== cursor blinking
268
269 %recall 45
270
271 Place history line 45 on the next input prompt. Use %hist to find
272 out the number.
273
274 %recall 1-4
275
276 Combine the specified lines into one cell, and place it on the next
277 input prompt. See %history for the slice syntax.
278
279 %recall foo+bar
280
281 If foo+bar can be evaluated in the user namespace, the result is
282 placed at the next input prompt. Otherwise, the history is searched
283 for lines which contain that substring, and the most recent one is
284 placed at the next input prompt.
285 """
286 if not arg: # Last output
287 self.shell.set_next_input(str(self.shell.user_ns["_"]))
288 return
289 # Get history range
290 histlines = self.shell.history_manager.get_range_by_str(arg)
291 cmd = "\n".join(x[2] for x in histlines)
292 if cmd:
293 self.shell.set_next_input(cmd.rstrip())
294 return
295
296 try: # Variable in user namespace
297 cmd = str(eval(arg, self.shell.user_ns))
298 except Exception: # Search for term in history
299 histlines = self.shell.history_manager.search("*"+arg+"*")
300 for h in reversed([x[2] for x in histlines]):
301 if 'recall' in h or 'rep' in h:
302 continue
303 self.shell.set_next_input(h.rstrip())
304 return
305 else:
306 self.shell.set_next_input(cmd.rstrip())
307 return
308 print("Couldn't evaluate or find in history:", arg)
309

Callers

nothing calls this directly

Calls 3

set_next_inputMethod · 0.80
get_range_by_strMethod · 0.45
searchMethod · 0.45

Tested by

no test coverage detected