File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
APP_NAME: str = 'NotepadPy+'
3434
SELECTED_THEME: str = ''
3535
text_to_save: str = ''
36+
selected_text: str = ''
3637
# this is needed to control the displaying of the user prompt while closing.
3738
# If the user closes the document just before closing the document,
3839
# we do not want to display the prompt for save changes.
@@ -211,8 +212,15 @@ def rgb2hex(r, g, b):
211212

212213
def new_file() -> str:
213214
''' Reset body and info bar, and clear FILE_NAME variable '''
215+
216+
global text_last_saved_manually
217+
global text_to_save
218+
214219
WINDOW['-BODY-'].update(value='')
215220
WINDOW['-FILE_INFO-'].update(value='New File:')
221+
WINDOW.set_title('untitled - ' + APP_NAME)
222+
text_last_saved_manually = ''
223+
text_to_save = ''
216224

217225
def open_file() -> str:
218226
''' Open file and update the infobar '''
@@ -409,24 +417,48 @@ def AboutNotepadPyPlus():
409417

410418
# edit menu events.
411419
if EVENT == edit_cut:
412-
selected_text = WINDOW['-BODY-'].Widget.selection_get()
413-
tk.clipboard_clear()
414-
tk.clipboard_append(selected_text)
415-
tk.update()
416-
WINDOW['-BODY-'].Widget.delete("sel.first", "sel.last")
420+
try:
421+
selected_text = WINDOW['-BODY-'].Widget.selection_get()
422+
tk.clipboard_clear()
423+
tk.clipboard_append(selected_text)
424+
tk.update()
425+
WINDOW['-BODY-'].Widget.delete("sel.first", "sel.last")
426+
except: # pylint: disable=bare-except
427+
selected_text = ''
428+
ShowMessageBox(title='Text Selection Error',
429+
message='An active text selection was not found. Please select some text to cut.')
417430

418431
if EVENT == edit_copy:
419-
selected_text = WINDOW['-BODY-'].Widget.selection_get()
420-
tk.clipboard_clear()
421-
tk.clipboard_append(selected_text)
422-
tk.update() # now it stays on the clipboard after the window is closed
432+
try:
433+
selected_text = WINDOW['-BODY-'].Widget.selection_get()
434+
tk.clipboard_clear()
435+
tk.clipboard_append(selected_text)
436+
tk.update() # now it stays on the clipboard after the window is closed
437+
except: # pylint: disable=bare-except
438+
selected_text = ''
439+
ShowMessageBox(title='Text Selection Error',
440+
message='An active text selection was not found. Please select some text to copy.')
423441

424442
if EVENT == edit_paste:
425443
clip_text = tk.clipboard_get()
444+
445+
try:
446+
selected_text = WINDOW['-BODY-'].Widget.selection_get()
447+
except: # pylint: disable=bare-except
448+
selected_text = ''
449+
450+
if selected_text != '':
451+
WINDOW['-BODY-'].Widget.delete("sel.first", "sel.last")
452+
426453
WINDOW['-BODY-'].Widget.insert("insert", clip_text)
427454

428455
if EVENT == edit_delete:
429-
WINDOW['-BODY-'].Widget.delete("sel.first", "sel.last")
456+
try:
457+
WINDOW['-BODY-'].Widget.delete("sel.first", "sel.last")
458+
except:
459+
selected_text = ''
460+
ShowMessageBox(title='Text Selection Error',
461+
message='An active text selection was not found. Please select some text to delete.')
430462

431463
if EVENT in ('Word Count',):
432464
WORDS = get_word_count()
@@ -484,6 +516,10 @@ def AboutNotepadPyPlus():
484516
# record the text after each event to ensure the
485517
# file/text is saved.
486518
try:
487-
text_to_save = VALUES['-BODY-']
519+
# if File -> New menu option is chosen and the new blank editor window is closed, then we do
520+
# not want to display the Save File prompt. Executing this block on the event of a new file
521+
# resets the 'text_to_save' variable to old text in the editor and causes to display the save prompt.
522+
if EVENT != file_new:
523+
text_to_save = VALUES['-BODY-']
488524
except: # pylint: disable=bare-except
489525
pass

0 commit comments

Comments
 (0)