
    "e?                     .   d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ dj                  d eD              Zd	Zej"                  d
k(  rdndZ G d d      Zd Zedk(  r d dlmZ  eddd       d dlmZ  ee       yy)    N)
filedialog)
messagebox)	askstring)idleConf)py_extensions c              #   &   K   | ]	  }d |z     yw)*N ).0exts      lib/python3.12/idlelib/iomenu.py	<genexpr>r      s     :SS:s   utf-8win32surrogatepasssurrogateescapec                       e Zd Zd Zd Zd Zd Zd ZdZd Z	dZ
dZd Zdd	Zej                  Zd
 Zd Zd Zd Zd Zd Zd Zd Zd ZdZdZdedfddfZej>                  dk(  rdndZ d Z!ddZ"d Z#d Z$y) 	IOBindingc                    || _         |j                  | _        | j                  j                  d| j                        | _        | j                  j                  d| j
                        | _        | j                  j                  d| j                        | _        | j                  j                  d| j                        | _
        d| _        | j                  j                  d| j                        | _        y )N<<open-window-from-file>><<save-window>><<save-window-as-file>><<save-copy-of-window-as-file>>r   <<print-window>>)editwintextbindopen_IOBinding__id_opensave_IOBinding__id_savesave_as_IOBinding__id_saveassave_a_copy_IOBinding__id_savecopyfileencodingprint_window_IOBinding__id_print)selfr   s     r   __init__zIOBinding.__init__   s    LL	(CTYYO(9499E99>>*C*.,,8!YY^^,M,0,<,<>#))..);T=N=NO    c                    | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j
                         | j                   j                  d| j                         d | _        d | _         d | _        y )Nr   r   r   r   r   )	r   unbindr    r"   r$   r&   r)   r   filename_change_hookr*   s    r   closezIOBinding.close%   s    		4dnnE		*DNN;		243C3CD		:D<N<NO		+T__=	$(!r,   c                 6    | j                   j                         S N)r   	get_savedr0   s    r   r4   zIOBinding.get_saved1   s    ||%%''r,   c                 :    | j                   j                  |       y r3   )r   	set_savedr*   flags     r   r6   zIOBinding.set_saved4   s    t$r,   c                 8    | j                   j                          y r3   )r   
reset_undor0   s    r   r:   zIOBinding.reset_undo7   s    !r,   Nc                     || _         y r3   )r/   )r*   hooks     r   set_filename_change_hookz"IOBinding.set_filename_change_hook<   s
    $(!r,   c                     |r.t         j                  j                  |      rd | _        || _        y || _        d | _        | j                  d       | j                  r| j                          y y )N   )ospathisdirfilenamedirnamer6   r/   r*   rC   s     r   set_filenamezIOBinding.set_filenameB   sV    h/ DM#DL$DMDLNN1(())+ )r,   c                    | j                   j                  }|r|s| j                         }n|}|rn| j                   rPt        | j                   dd       s9| j                  s-| j                         r|j                  || j                         y|j                  |       y| j                  r| j                  j                          y| j                         r0| j                         }|dk(  r| j                  j                          y|s| j                         }n|}|r| j                  |       y| j                  j                          y)Ninterpbreakcancel)r   flistaskopenfilegetattrrC   r4   r   loadfiler   	focus_set	maybesave)r*   eventeditFilerK   rC   replys         r   r   zIOBinding.openM   s   ""++-! LL#DLL(DA MM(JJx7 	 JJx(  99II'') >>NN$E 		##%'')HHMM(#  II!r,   c                    	 	 t        j                  |      5 }|j                         }|j                  }|j                  }d}d d d        t        t              s6|$t        j                   dd| j                  	       d}t"        j$                  }| j                  j'                  dd       | j)                  d        | _        || _        | j                  j/                  d       | j1                          | j)                  |       r| j3                  d       | j                  j5                  dd       | j                  j7                  d       | j9                  |       y# 1 sw Y   $xY w# t
        t        f$ r | j                  j                  j                          t        ddd| j                  j                        }t        ||      5 }|j                         }|j                  }|j                  }d}d d d        n# 1 sw Y   nxY wY w xY w# t        $ r5}t        j                  dt        |      | j                  	       Y d }~yd }~wt
        $ r( t        j                  d
d|z  | j                  	       Y yw xY w)NFzSpecify file encodingzwThe file's encoding is invalid for Python 3.x.
IDLE will convert it to UTF-8.
What is the current encoding of the file?r   )initialvalueparent)encodingT	I/O ErrorrV   zDecoding ErrorzFile %s
Failed to DecodezMixed Newlinesz:Mixed newlines detected.
The file will be changed on save.1.0endinsert)tokenizer   readrW   newlinesUnicodeDecodeErrorSyntaxErrorr   r   updater   OSErrorr   	showerrorstr
isinstanceshowwarningr@   linesepdeleterF   r'   eol_conventionr\   r:   r6   mark_setyviewupdaterecentfileslist)	r*   rC   fcharsr'   rj   	convertedencerrs	            r   rN   zIOBinding.loadfile{   s(   	%]]8, &FFHE#$::L%&ZZN %I	&: .#. )&&'7*M04		; !	ZZN		&$(,		&(# NN5!		8U+		!""8,m& &
 '4 %!!((*+@ ")<<,,. (S1 %QFFHE#$::L%&ZZN $I	% % %%  	  c#htyyI! 	  !1#>#I*.))5 		se   E2 +E%E2 %E/*E2 2A#H+H	 	H	H	HH HH 	J	%+I1J	J	c                 B   | j                         ryd| j                  xs d d}t        j                  d|t        j                  | j
                        }|r&| j                  d       | j                         rdnd}n|dnd	}| j
                  j                          |S )
zReturn 'yes', 'no', 'cancel' as appropriate.

        Tkinter messagebox.askyesnocancel converts these tk responses
        to True, False, None.  Convert back, as now expected elsewhere.
        yeszDo you want to save zthis untitled documentz before closing?zSave On ClosetitlemessagedefaultrV   NrJ   no)r4   rC   r   askyesnocancelYESr   r!   rO   )r*   rw   confirmrS   s       r   rP   zIOBinding.maybesave   s     >>)mm?'?@%& ++'!$..	$
 IIdO!^^-E8E#*?xu		r,   c                     | j                   s| j                  |       nG| j                  | j                         r,| j                  d       	 | j                  j                          | j                  j                          y# t        $ r Y &w xY w)NTrI   )	rC   r#   	writefiler6   r   store_file_breaksAttributeErrorr   rO   r*   rQ   s     r   r!   zIOBinding.save   so    }}LL~~dmm,t$LL224 			 & s   B 	BBc                 8   | j                         }|rN| j                  |      r=| j                  |       | j                  d       	 | j                  j                          | j                  j                          | j                  |       y# t        $ r Y 7w xY w)Nr?   rI   )
asksavefiler~   rF   r6   r   r   r   r   rO   rm   r*   rQ   rC   s      r   r#   zIOBinding.save_as   s    ##%~~h'!!(+q!LL224 			""8,	 & s   B 	BBc                     | j                         }|r| j                  |       | j                  j                          | j	                  |       y)NrI   )r   r~   r   rO   rm   r   s      r   r%   zIOBinding.save_a_copy   s@    ##%NN8$		""8,r,   c                    | j                         }| j                  |      }	 t        |d      5 }|j                  |       |j	                          t        j                  |j                                d d d        y# 1 sw Y   yxY w# t        $ r5}t        j                  dt        |      | j                         Y d }~yd }~ww xY w)NwbTrX   rY   F)fixnewlinesencoder   writeflushr@   fsyncfilenorc   r   rd   re   r   )r*   rC   r   ro   rn   msgs         r   r~   zIOBinding.writefile   s    !D!		h% %	$% 	%  	  c#h*.))5	s5   B	 AA=4B	 =BB	 B	 		C+CCc                    t        | j                  d      r6| j                  j                  d| j                  j	                  d            }nV| j                  j                  d      dk7  r| j                  j                  dd       | j                  j                  dd      }| j                  dk7  r|j                  d| j                        }|S )zYReturn text with os eols.

        Add prompts if shell else final 
 if missing.
        rH   rZ   zend-1czend-2c
)	hasattrr   get_prompt_textr   indexgetr\   rj   replacer*   r   s     r   r   zIOBinding.fixnewlines   s     4<<*<<//tyyx7PQDyy}}X&$.		  4099==1D$&<<d&9&9:Dr,   c                 &   t        |t              r|S | j                  dk(  r|j                  d      S 	 |j                  d      S # t        $ r Y nw xY w	 |j                  dd      }t        j                  t        j                  |      j                        \  }}|j                  |      S # t        $ r}t        |      }Y d }~nd }~wt        $ r dz  }Y nw xY wt        j                  dd|z  | j                         |j                  d      S )Nz	utf-8-sigasciir   zInvalid encoding '%s'rX   z%s.
Saving as UTF-8rY   )rf   bytesr'   r   UnicodeEncodeErrorr]   detect_encodingioBytesIOreadlinera   re   r   rd   r   )r*   ro   encodedrq   _rr   faileds          r   r   zIOBinding.encode  s    eU# L+<<,,	<<((! 			3ll7I6G--bjj.A.J.JKFC<<$$ 	XF! 	3,s2F	3"V+99	 ||K((s0   A 	AAAB- -	C6CCCc                    t        j                  ddt         j                  | j                        }|s| j                  j	                          yd }| j                         }|r| j                  }|rWt        j                  d      \  }}|}t        j                  |       | j                  |      st        j                  |       yt        j                  }d}|dk(  rt        j                  d	d
d      }	|	dz   }	n|dk(  rt        j                  d	d
d      }	nd}|r	t!        j"                  |      z  }	t        j$                  |	d      }
|
j'                         j)                         }|
j                         }|rd|z  |z   }|r[dt+        |	      z  |z   }t        j,                  d|| j                         n'd|z  }t        j.                  d|| j                         |rt        j                  |       y)NPrintzPrint to Default Printerru   rI   	IDLE_tmp_)prefixTposixmainGeneralzprint-command-posixz 2>&1ntzprint-command-winFrz#Printing failed (exit status 0x%x)
zPrinting command: %s
zPrint statusrY   z-Printing is not enabled for this platform: %s)r   askokcancelOKr   rO   r4   rC   tempfilemkstempr@   r1   r~   unlinknamer   	GetOptionshlexquotepopenr^   stripreprrd   showinfo)r*   rQ   r|   tempfilenamesavedrC   tfdplatformprintPlatformcommandpipeoutputstatusrw   s                 r   r(   zIOBinding.print_window-  s   ((4$--	$
 II! }}H("*"2"2+"FS,#HHHSM>>,/		,'77w((	)>@G'G((	:MNG!MH 55G88GS)DYY[&&(FZZ\F? "()1DMAFJ$$^VDIINEPG		JIIl#r,   zPython filesTEXT)z
Text filesz*.txtr   )z	All filesr
   darwinz.py c                     | j                  d      \  }}| j                  s0t        j                  | j                  | j
                        | _        | j                  j                  ||      }|S )Nr   )rV   	filetypes
initialdirinitialfile)defaultfilename
opendialogr   Openr   r   showr*   dirbaserC   s       r   rL   zIOBinding.askopenfilej  sY    ((0	T(ooTYY:>..JDO??''3D'Ir,   c                     | j                   r)t        j                  j                  | j                         S | j                  r| j                  dfS 	 t        j
                         }|dfS # t        $ r d}Y |dfS w xY w)Nr   )rC   r@   rA   splitrD   getcwdrc   )r*   modepwds      r   r   zIOBinding.defaultfilenamer  sp    ==77==//\\<<##iik 7N  7Ns   A) )A:9A:c                     | j                  d      \  }}| j                  s;t        j                  | j                  | j
                  | j                        | _        | j                  j                  ||      }|S )Nr!   )rV   r   defaultextensionr   )r   
savedialogr   SaveAsr   r   r   r   r   s       r   r   zIOBinding.asksavefile~  sf    ((0	T(//99"nn%)%:%:<DO ??''3D'Ir,   c                 h    | j                   j                  r| j                   j                  |       yy)z-Update recent file list on all editor windowsN)r   rK   update_recent_files_listrE   s     r   rm   zIOBinding.updaterecentfileslist  s&    <<LL11(; r,   )NN)r   )%__name__
__module____qualname__r+   r1   r4   r6   r:   r/   r=   rC   rD   rF   r   r@   rh   rj   rN   rP   r!   r#   r%   r~   r   r   r(   r   r   r   r   sysr   r   rL   r   r   rm   r   r,   r   r   r      s    

P
)(%"  ) HG	,*X ZZN9v. )>0d JJ 
/'
I !$ 8ub
<r,   r   c                 h   ddl m}m}  ||       }|j                  d       t	        t
        | j                         j                  d      dd        \  }}|j                  d||dz   fz          G d d	      } ||      }|j                          |j                           ||      }t        |       y )
Nr   )ToplevelTextzTest IOBinding+r?   z+%d+%d   c                   B    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)_io_binding.<locals>.MyEditWinc                    || _         d | _        | j                   j                  d| j                         | j                   j                  d| j                         | j                   j                  d| j
                         | j                   j                  d| j                         | j                   j                  d| j                         y )Nz<Control-o>z<Control-p>z<Control-s>z<Alt-s>z<Control-c>)r   rK   r   r   printr!   saveassavecopyr   s     r   r+   z'_io_binding.<locals>.MyEditWin.__init__  s~    DIDJIINN=$))4IINN=$**5IINN=$))4IINN9dkk2IINN=$--8r,   c                      y)Nr   r   r0   s    r   r4   z(_io_binding.<locals>.MyEditWin.get_saved  s    Ar,   c                      y r3   r   r7   s     r   r6   z(_io_binding.<locals>.MyEditWin.set_saved      r,   c                      y r3   r   r0   s    r   r:   z)_io_binding.<locals>.MyEditWin.reset_undo  r   r,   c                 :    | j                   j                  d       y )Nr   r   event_generater   s     r   r   z#_io_binding.<locals>.MyEditWin.open  s    II$$%@Ar,   c                 :    | j                   j                  d       y )Nr   r   r   s     r   r   z$_io_binding.<locals>.MyEditWin.print  s    II$$%78r,   c                 :    | j                   j                  d       y )Nr   r   r   s     r   r!   z#_io_binding.<locals>.MyEditWin.save  s    II$$%67r,   c                 :    | j                   j                  d       y )Nr   r   r   s     r   r   z%_io_binding.<locals>.MyEditWin.saveas  s    II$$%>?r,   c                 :    | j                   j                  d       y )Nr   r   r   s     r   r   z'_io_binding.<locals>.MyEditWin.savecopy  s    II$$%FGr,   N)r   r   r   r+   r4   r6   r:   r   r   r!   r   r   r   r,   r   	MyEditWinr     s.    	9 	&'"	B	9	8	@	Hr,   r   )tkinterr   r   rv   mapintgeometryr   packrO   r   )	rV   r   r   topxyr   r   r   s	            r   _io_bindingr     s    &
6
CIIsFOO%++C045DAqLLQCL()H H. 9DIIKNNoGgr,   __main__)r   zidlelib.idle_test.test_iomenu   F)	verbosityexit)run)r   r@   r   r   r   r]   r   r   r   tkinter.simpledialogr   idlelib.configr   idlelib.utilr   joinrW   r   errorsr   r   r   unittestr   idlelib.idle_test.htestr  r   r,   r   <module>r     s    	 	  
     * # &:M::LLG39Jw< w<t#L z	(AEB+ r,   