Format: application/x-python-code
1 o
2
3 ��hU1 � @ s� d Z ddlZddlZddlmZ ddlZddlmZ ddl m
4 Z
5 mZmZ ddl
6 mZmZmZ ddlmZ ddlmZ e� ZeG d d
7 � d
8 e
9 ��ZdS )z6Implementation of magic functions related to History.
10 � N)�open)�StdinNotImplementedError)�Magics�magics_class�
11 line_magic)�argument�magic_arguments�parse_argstring)�skip_doctest)�ioc @ s� e Zd Ze� edddddd�eddddd d�ed
12 ddddd�ed
13 ddddd�edddd�edddddd�eddededd �ed!d"dd#d$�ed%dd&�eed.d(d)�������������Z ed*d+� �Z
14 ed.d,d-��ZdS )/�
15 HistoryMagicsz-n�
16 print_nums�
17 store_trueFz{
18 print line numbers for each input.
19 This feature is only available if numbered prompts are in use.
20 )�dest�action�default�helpz-o�
21 get_outputz"also print outputs for each input.z-p� pypromptsz�
22 print classic '>>>' python prompts before each input.
23 This is useful for making documentation, and in conjunction
24 with -o, for producing doctest-ready output.
25 z-t�raw�store_falseTa�
26 print the 'translated' history, as IPython understands it.
27 IPython filters your input and converts it all into valid Python
28 source before executing it (things like magics or aliases are turned
29 into function calls, for example). With this option, you'll see the
30 native history instead of the user-entered version: '%%cd /' will be
31 seen as 'get_ipython().run_line_magic("cd", "/")' instead of '%%cd /'.
32 z-f�filenamea�
33 FILENAME: instead of printing the output to the screen, redirect
34 it to the given file. The file is always overwritten, though *when
35 it can*, IPython asks for confirmation first. In particular, running
36 the command 'history -f FILENAME' from the IPython Notebook
37 interface will replace FILENAME even if it already exists *without*
38 confirmation.
39 )r r z-g�pattern�*Nac
40 treat the arg as a glob pattern to search for in (full) history.
41 This includes the saved history (almost all commands ever written).
42 The pattern may contain '?' to match one unknown character and '*'
43 to match any number of unknown characters. Use '%%hist -g' to show
44 full saved history (may be very long).
45 )r �nargsr r z-l�limit�?z�
46 get the last n lines from all sessions. Specify n as a single
47 arg, or the default is the last 10 lines.
48 )r �typer r r z-u�uniquezN
49 when searching history using `-g`, show only unique history.
50 )r r r �range)r � c sb t | j|�}| jj� � fdd�}|j}|stj}d}n6tj� |�}tj�
51 |�rHz t�d| �}W n t
52 y; d}Y nw |sDtd� dS td� t|d d
53 d�}d}|j}|j} |j}
54 |j}d}|jtu rfdn|j}
55 d}|jdur�|js�|jr�dd
56 �|j� d }nd}� j||| |
57 |jd�}d}n0|jtur�|
58 du r�dn|
59 }� j||| d�}n|jr�dd
60 �|j� d }d}� �d
61 �|j�|| �}d}|D ]`\}}}| r�|\}}|r�t�||�s�q�|�d��� }d|v }|r�dnd
62 }|r�td|||�� |�|f |dd� |
63 �rtdd|d� |�rd�|�!� �d }t||d� | �r%|�r%t||d� q�|�r/|�"� dS dS )a� Print input history (_i<n> variables), with most recent last.
64
65 By default, input history is printed without line numbers so it can be
66 directly pasted into an editor. Use -n to show them.
67
68 By default, all input history from the current session is displayed.
69 Ranges of history can be indicated using the syntax:
70
71 ``4``
72 Line 4, current session
73 ``4-6``
74 Lines 4-6, current session
75 ``243/1-5``
76 Lines 1-5, session 243
77 ``~2/7``
78 Line 7, session 2 before current
79 ``~8/1-~6/5``
80 From the first line of 8 sessions ago, to the fifth line of 6
81 sessions ago.
82
83 Multiple ranges can be entered, separated by spaces
84
85 The same syntax is used by %macro, %save, %edit, %rerun
86
87 Examples
88 --------
89 ::
90
91 In [6]: %history -n 4-6
92 4:a = 12
93 5:print(a**2)
94 6:%history -n 4-6
95
96 c s"