
    =VQ`&                     >    d dl mZ d dlmZ ddlmZ  G d de      Zy)    )QtGui)Bool   )ConsoleWidgetc                   |     e Zd ZdZ edd      Z fdZ fdZd Zd Z	dd	Z
dd
ZddZd Zd Zd Zd Z xZS )HistoryConsoleWidgetz A ConsoleWidget that keeps a history of the commands that have been
        executed and provides a readline-esque interface to this history.
    FT)configc                 \    t        |   |i | g | _        i | _        d| _        d| _        y )Nr    )super__init___history_history_edits_history_index_history_prefix)selfargskw	__class__s      B/usr/lib/python3/dist-packages/qtconsole/history_console_widget.pyr   zHistoryConsoleWidget.__init__   s6    $%"%  !    c                 &   || j                   n|}t        | 	  |||       |rm|j                         }|r9| j                  r| j                  d   |k7  r| j                  j                  |       i | _        t        | j                        | _        yy)z% Reimplemented to the store history. N)	input_bufferr   
do_executerstripr   appendr   lenr   )r   sourcecompleteindenthistoryr   s        r   r   zHistoryConsoleWidget.do_execute&   s    '-~$##668V4 nn&Gr1Bg1M$$W- #%D #&dmm"4D r   c                 `   | j                         }| j                         j                         |j                         k(  r| j                         r|sy| j	                         }| j
                  }t        |t        | j                              }| j                  d| |d| k7  rt        | j                        | _
        | j                         }|j                         }|j                  t        j                  j                         |j                         |k(  }| j                  t        | j                        k(  s5| j                  dk(  r&|r$| j!                  | j                        d| |d| k(  s
|d| | _        | j#                  | j                  |        | j                         }	| j                  r?|	j                  t        j                  j$                  t        | j                               n)|	j                  t        j                  j                         | j'                  |	       yy)zj Called when the up key is pressed. Returns whether to continue
            processing the event.
        FNr   	as_prefixnT)_get_prompt_cursor_get_cursorblockNumber_history_locked_get_input_buffer_cursor_posr   minr   r   r   r   positionmovePositionr   QTextCursor
EndOfBlock_get_edited_historyhistory_previousRight_set_cursor)
r   shift_modifierprompt_cursorposr   r'   ccurrent_posat_eolcursors
             r   _up_pressedz HistoryConsoleWidget._up_pressed=   s    //1))+}/H/H/JJ##%n 335C,,L CT1123A $$Ra(L!,<<&)$--&8#   "A**,KNN5,,778jjlk1F""c$--&88))R/F--d.A.AB4CHLY]Z]L^^'3DS'9$ !!$"6"62@.@ " B ,,.F####E$5$5$;$;&)$*>*>&? $ A ##E$5$5$@$@AV$r   c                    | j                         }| j                         j                         |j                         k(  r| j                         r|sy| j	                  | j
                  |       }| j
                  ra|r_| j                         }|j                  t        j                  j                  t        | j
                               | j                  |       yy)zl Called when the down key is pressed. Returns whether to continue
            processing the event.
        Fr$   r&   T)_get_end_cursorr)   r*   r+   history_nextr   r(   r/   r   r0   r4   r   r5   )r   r6   
end_cursorreplacedr<   s        r   _down_pressedz"HistoryConsoleWidget._down_pressedu   s     ))+
))+z/E/E/GG##%n (()=)=:H6H ) JH ##002##E$5$5$;$;&)$*>*>&? $ A  (r   c                     | j                   }d}|dkD  rH|dz  }| j                  |      }|| j                  k(  r+|r|j                  |      s|s||v rd}n|dkD  rH|r| j	                          || _         | _        |S )a   If possible, set the input buffer to a previous history item.

        Parameters
        ----------
        substring : str, optional
            If specified, search for an item with this substring.
        as_prefix : bool, optional
            If True, the substring must match at the beginning (default).

        Returns
        -------
        Whether the input buffer was changed.
        Fr   r   T)r   r2   r   
startswith_store_editsr   	substringr%   indexreplacer"   s         r   r3   z%HistoryConsoleWidget.history_previous   s     ##aiQJE..u5G$+++g00;!i7&: ai "'D 'Dr   c                 H   | j                   }d}|t        | j                        k  r[|dz  }| j                  |      }|| j                  k(  r>|r|j                  |      s|s||v rd}n|t        | j                        k  r[|r| j                          || _         | _        |S )a   If possible, set the input buffer to a subsequent history item.

        Parameters
        ----------
        substring : str, optional
            If specified, search for an item with this substring.
        as_prefix : bool, optional
            If True, the substring must match at the beginning (default).

        Returns
        -------
        Whether the input buffer was changed.
        Fr   T)r   r   r   r2   r   rE   rF   rG   s         r   r@   z!HistoryConsoleWidget.history_next   s     ##c$--((QJE..u5G$+++g00;!i7&: c$--(( "'D 'Dr   c                 "    | j                   | d S )z Get the local history list.

        Parameters
        ----------
        n : int
            The (maximum) number of history items to get.
        N)r   )r   r'   s     r   history_tailz!HistoryConsoleWidget.history_tail   s     }}aRS!!r   c                     | j                   xrg | j                  | j                        | j                  k7  xr= | j	                         j                         | j                         j                         k7  S )z5 Returns whether history movement is locked.
        )history_lockr2   r   r   r(   r*   r?   )r   s    r   r+   z$HistoryConsoleWidget._history_locked   so     !! 7))$*=*=>""#7 ((*668%%'3356	8r   c                     || j                   v r| j                   |   S |t        | j                        k(  r
t               S | j                  |   S )zB Retrieves a history item, possibly with temporary edits.
        )r   r   r   str)r   rI   s     r   r2   z(HistoryConsoleWidget._get_edited_history   sJ     D'''&&u--c$--((5L}}U##r   c                 f    t        |      | _        i | _        t        | j                        | _        y)zG Replace the current history with a sequence of history items.
        N)listr   r   r   r   )r   r"   s     r   _set_historyz!HistoryConsoleWidget._set_history   s(     W !$--0r   c                     | j                   }| j                  t        | j                        k(  s| j                  | j                     |k7  r|| j                  | j                  <   yy)zE If there are edits to the current input buffer, store them.
        N)r   r   r   r   r   )r   currents     r   rF   z!HistoryConsoleWidget._store_edits   sX     ###dmm"44d112g=7>D 3 34 >r   )r   T)
   )__name__
__module____qualname____doc__r   rO   r   r   r=   rC   r3   r@   rM   r+   r2   rT   rF   __classcell__)r   s   @r   r   r   
   sQ     d+L"5.6p@BB"8$1?r   r   N)qtpyr   	traitletsr   console_widgetr   r    r   r   <module>ra      s      )z?= z?r   