B
    nôb\*  ã               @   s  d Z ddlmZ ddlZddlZddlmZ ddlm	Z	m
Z
 ddlmZmZmZmZmZmZmZ ddlmZ ddlmZ ddlmZ dd	lmZmZ dd
lmZ ddlmZ ddl m!Z!m"Z"m#Z# ddl$m%Z% ddl&m'Z' ddl(m)Z) ddl*m+Z+ G dd„ deƒZ,G dd„ deƒZ-dS )zConsole History Pluginé    N)ÚPYQT5)ÚSignalÚSlot)Ú	QGroupBoxÚQHBoxLayoutÚQInputDialogÚQMenuÚQToolButtonÚQVBoxLayoutÚQWidget)Ú_)ÚSpyderPluginWidget)ÚPluginConfigPage)Úis_text_stringÚto_text_string)Úencoding)Úicon_manager)Úadd_actionsÚcreate_actionÚcreate_toolbutton)Únormalize_eols)ÚTabs)Ú
codeeditor)ÚFindReplacec               @   s   e Zd Zdd„ Zdd„ ZdS )ÚHistoryConfigPagec             C   s
   t  d¡S )NÚhistory)ÚimaÚicon)Úself© r   ú5lib/python3.7/site-packages/spyder/plugins/history.pyÚget_icon%   s    zHistoryConfigPage.get_iconc       	   	   C   sÌ   t tdƒƒ}| jtdƒtdƒddddtdƒd}t td	ƒƒ}|  td
ƒd¡}|  tdƒd¡}tƒ }| |¡ | |¡ tƒ }| |¡ | |¡ | |¡ tƒ }| |¡ | |¡ | d¡ |  |¡ d S )NZSettingszHistory depth: z entriesÚmax_entriesé
   i'  zSet maximum line count)Zmin_Zmax_ÚstepZtipzSource codez
Wrap linesÚwrapz"Scroll automatically to last entryÚ	go_to_eofé   )r   r   Zcreate_spinboxZcreate_checkboxr
   Ú	addWidgetÚ	setLayoutZ
addStretch)	r   Zsettings_groupZ	hist_spinZsourcecode_groupZwrap_mode_boxZgo_to_eof_boxZsettings_layoutZsourcecode_layoutZvlayoutr   r   r    Ú
setup_page(   s*    







zHistoryConfigPage.setup_pageN)Ú__name__Ú
__module__Ú__qualname__r!   r*   r   r   r   r    r   $   s   r   c               @   s®   e Zd ZdZdZeZeƒ Zdd„ Z	dd„ Z
dd„ Zd	d
„ Zd%dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zeƒ d d!„ ƒZeeƒd"d#„ ƒZd$S )&Ú
HistoryLogz
    History log widget
    Z
historylogc             C   s`  d | _ d | _d | _d | _g | _g | _tr:tj| ||d nt | |¡ |  	¡  t
ƒ }t| | jƒ| _ | j j | j¡ | j j | j¡ tjdkrÌtƒ }| d¡ t|ƒ}| dddd¡ | | j ¡ | |¡ n| | j ¡ t| tdƒt d¡d}| tj¡ t| ƒ}t || jƒ | !|¡ | j  "|¡ t#| ƒ| _$| j$ %¡  |  &| j$¡ | | j$¡ |  '|¡ d S )N)ÚmainÚdarwinztab-containerr   ZOptionsZtooloptions)Útextr   )(Ú	tabwidgetÚmenu_actionsZ
dockviewerÚwrap_actionÚeditorsÚ	filenamesr   r   Ú__init__Zinitialize_pluginr
   r   ZcurrentChangedÚconnectÚrefresh_pluginZ	move_dataÚmove_tabÚsysÚplatformr   ZsetObjectNamer   ZsetContentsMarginsr(   r   r   r   r   ZsetPopupModer	   ZInstantPopupr   r   ZsetMenuZsetCornerWidgetr   Úfind_widgetZhideZregister_widget_shortcutsr)   )r   ÚparentZlayoutZtab_containerZ
tab_layoutZoptions_buttonZmenur   r   r    r7   L   sD    





zHistoryLog.__init__c             C   s   t dƒS )zReturn widget titlezHistory log)r   )r   r   r   r    Úget_plugin_title~   s    zHistoryLog.get_plugin_titlec             C   s
   t  d¡S )zReturn widget iconr   )r   r   )r   r   r   r    Úget_plugin_icon‚   s    zHistoryLog.get_plugin_iconc             C   s
   | j  ¡ S )zq
        Return the widget to give focus to when
        this plugin's dockwidget is raised on top-level
        )r2   ÚcurrentWidget)r   r   r   r    Úget_focus_widget†   s    zHistoryLog.get_focus_widgetFc             C   s   dS )z3Perform actions before parent main window is closedTr   )r   Z
cancelabler   r   r    Úclosing_plugin   s    zHistoryLog.closing_pluginc             C   s*   | j  ¡ r| j  ¡ }nd}| j |¡ dS )zRefresh tabwidgetN)r2   ÚcountrA   r=   Ú
set_editor)r   Úeditorr   r   r    r9   ‘   s    
zHistoryLog.refresh_pluginc             C   s^   t | tdƒdt d¡tdƒ| jd}t | tdƒ| jd| _| j |  d¡¡ || jg| _	| j	S )	z*Return a list of actions related to pluginz
History...Nr   zSet history maximum entries)Z	triggeredz
Wrap lines)Ztoggledr%   )
r   r   r   r   Úchange_history_depthÚtoggle_wrap_moder4   Ú
setCheckedÚ
get_optionr3   )r   Zhistory_actionr   r   r    Úget_plugin_actions™   s    



zHistoryLog.get_plugin_actionsc             C   s   | j  | j j| ¡ dS )z3Action to be performed on first plugin registrationN)r/   Ztabify_pluginsZ
ipyconsole)r   r   r   r    Úon_first_registration¥   s    z HistoryLog.on_first_registrationc             C   s4   | j  | jj¡ | j | ¡ | jjjj | j¡ dS )z'Register plugin in Spyder's main windowN)	Úfocus_changedr8   r/   Zplugin_focus_changedZadd_dockwidgetZconsoleÚshellZrefreshr9   )r   r   r   r    Úregister_plugin©   s    zHistoryLog.register_pluginc             C   s0   |   ¡ }|  ¡ }x| jD ]}| ||¡ qW dS )zUpdate font from PreferencesN)Úget_color_schemeÚget_plugin_fontr5   Úset_font)r   Úcolor_schemeZfontrF   r   r   r    Úupdate_font°   s    zHistoryLog.update_fontc       
      C   s   d}|   ¡ }d}|  ¡ }d}|  |¡}| j |¡ xX| jD ]N}||krd||krR|nd}	| ||	¡ n||krv| |¡ ||kr:| |¡ q:W dS )z*Apply configuration file's plugin settingsZcolor_scheme_nameZplugin_fontr%   N)	rP   rQ   rJ   r4   rI   r5   rR   Zset_color_schemerH   )
r   ZoptionsZcolor_scheme_nZcolor_scheme_oZfont_nZfont_oZwrap_nZwrap_orF   Zscsr   r   r    Úapply_plugin_settings·   s    

z HistoryLog.apply_plugin_settingsc             C   s8   | j  |¡}| j |¡}| j  ||¡ | j ||¡ dS )zU
        Move tab (tabs themselves have already been moved by the tabwidget)
        N)r6   Úpopr5   Úinsert)r   Z
index_fromZindex_toÚfilenamerF   r   r   r    r:   Ê   s    zHistoryLog.move_tabc       
         sf  t  |¡}|ˆ jkrdS t ˆ ¡}t |¡d dkr:d}nd}|jd|dd |j 	‡ fdd	„¡ | 
d
¡ ˆ  ¡ }| ˆ  ¡ |¡ | ˆ  d¡¡ t  |¡\}}t|ƒ}dd„ t d|¡D ƒ}ˆ  d¡}t|ƒ|krü||| d  d d… }t  ||¡ | |¡ | d¡ ˆ j |¡ ˆ j |¡ ˆ j |t |¡¡}	ˆ j |¡ ˆ j |	|¡ ˆ j  |	¡ dS )zc
        Add new history tab
        Slot for add_history signal emitted by shell instance
        Nr'   z.pyÚpyZbatF)ZlinenumbersÚlanguageZscrollflagareac                  s
   ˆ j  ¡ S )N)rM   Úemitr   )r   r   r    Ú<lambda>ä   s    z(HistoryLog.add_history.<locals>.<lambda>Tr%   c             S   s   g | ]}|  ¡ ‘qS r   )Ústart)Ú.0Úmr   r   r    ú
<listcomp>ì   s    z*HistoryLog.add_history.<locals>.<listcomp>Ú
r"   Úeof)!r   Zto_unicode_from_fsr6   r   Z
CodeEditorÚospÚsplitextZsetup_editorrM   r8   ZsetReadOnlyrP   rR   rQ   rH   rJ   Úreadr   ÚreÚfinditerÚlenÚwriteZset_textÚset_cursor_positionr5   Úappendr2   ZaddTabÚbasenamer=   rE   ZsetTabToolTipÚsetCurrentIndex)
r   rX   rF   rZ   rS   r1   r   Z
linebreaksZmaxNlineÚindexr   )r   r    Úadd_historyÕ   s:    






zHistoryLog.add_historyc             C   sd   t |ƒst| ¡ dƒ}t|ƒ}| j |¡}| j|  |¡ |  d¡rT| j|  d¡ | j	 
|¡ dS )zy
        Append an entry to history filename
        Slot for append_to_history signal emitted by shell instance
        zutf-8r&   rb   N)r   r   ZtoUtf8r6   rn   r5   rk   rJ   rj   r2   rm   )r   rX   Zcommandrn   r   r   r    Úappend_to_historyû   s    
zHistoryLog.append_to_historyc             C   s:   t  | tdƒtdƒ|  d¡dd¡\}}|r6|  d|¡ dS )zChange history max entriesZHistoryzMaximum entriesr"   r#   i'  N)r   ZgetIntr   rJ   Ú
set_option)r   ZdepthZvalidr   r   r    rG   	  s    zHistoryLog.change_history_depthc             C   s8   | j dkrdS x| jD ]}| |¡ qW |  d|¡ dS )zToggle wrap modeNr%   )r2   r5   rH   rq   )r   ÚcheckedrF   r   r   r    rH     s
    
zHistoryLog.toggle_wrap_modeN)F)r+   r,   r-   Ú__doc__ZCONF_SECTIONr   ZCONFIGWIDGET_CLASSr   rM   r7   r?   r@   rB   rC   r9   rK   rL   rO   rT   rU   r:   ro   rp   r   rG   ÚboolrH   r   r   r   r    r.   D   s(   2
&
r.   ).rs   Úos.pathÚpathrc   r;   rf   Zqtpyr   Zqtpy.QtCorer   r   Zqtpy.QtWidgetsr   r   r   r   r	   r
   r   Zspyder.config.baser   Zspyder.pluginsr   Zspyder.plugins.configdialogr   Zspyder.py3compatr   r   Zspyder.utilsr   r   r   Zspyder.utils.qthelpersr   r   r   Zspyder.utils.sourcecoder   Zspyder.widgets.tabsr   Zspyder.widgets.sourcecoder   Zspyder.widgets.findreplacer   r   r.   r   r   r   r    Ú<module>   s&   $ 