@@ -30,7 +30,33 @@ def getTrueFilename(filename):
3030debug = False
3131# UPDATE MODE HERE
3232update = False # UPDATE MODE HERE
33- version = "1.7.1"
33+ version = "1.7.2"
34+
35+ """
36+ Custom Classes
37+ """
38+ # From: https://stackoverflow.com/a/40618152/11106801
39+ class CustomText (tk .Text ):
40+ def __init__ (self , * args , ** kwargs ):
41+ """A text widget that report on internal widget commands"""
42+ tk .Text .__init__ (self , * args , ** kwargs )
43+
44+ # create a proxy for the underlying widget
45+ self ._orig = self ._w + "_orig"
46+ self .tk .call ("rename" , self ._w , self ._orig )
47+ self .tk .createcommand (self ._w , self ._proxy )
48+
49+ def _proxy (self , command , * args ):
50+ cmd = (self ._orig , command ) + args
51+ try :
52+ result = self .tk .call (cmd )
53+ except :
54+ result = ""
55+
56+ if command in ("insert" , "delete" , "replace" ):
57+ self .event_generate ("<<TextModified>>" )
58+
59+ return result
3460
3561"""
3662Window Settings
@@ -346,6 +372,22 @@ def openFile(Event=None, current=False):
346372 text = textboxes [current_tab ].get ("1.0" , tk .END )
347373 textboxes [current_tab ].delete ("1.0" , tk .END )
348374 textboxes [current_tab ].insert (tk .END , text [:- 2 ])
375+ for i in range (len (file_format_tags [current_tab ])):
376+ format = file_format_tags [current_tab ][i ]
377+ textboxes [current_tab ].tag_add (format [0 ], format [1 ], format [2 ])
378+
379+ if "colour" in format [0 ]:
380+ textboxes [current_tab ].tag_config (format [0 ], foreground = format [3 ])
381+ elif "size" in format [0 ]:
382+ textboxes [current_tab ].tag_config (format [0 ], font = (format [3 ], int (format [4 ])))
383+ else :
384+ # Get format type
385+ if "bold" in format [0 ]:
386+ textboxes [current_tab ].tag_config (format [0 ], font = (format [3 ], int (format [4 ]), "bold" ))
387+ elif "italic" in format [0 ]:
388+ textboxes [current_tab ].tag_config (format [0 ], font = (format [3 ], int (format [4 ]), "italic" ))
389+ elif "normal" in format [0 ]:
390+ textboxes [current_tab ].tag_config (format [0 ], font = (format [3 ], int (format [4 ]), "normal" ))
349391
350392def newFile (Event = None ):
351393 global file_save_locations , file_histories , current_versions
@@ -409,6 +451,8 @@ def saveFile(Event=None):
409451 # Set save status to True
410452 setSaveStatus (True , current_tab )
411453
454+ trackChanges (override = True )
455+
412456def saveFileAs (Event = None ):
413457 global file_save_locations
414458
@@ -512,36 +556,39 @@ def redo(Event=None):
512556
513557 setSaveStatus (False , current_tab )
514558
515- def updatePreview (Event = None ):
559+ def updatePreview (Event = None , override = False ):
516560 current_tab = getCurrentTab ()
517561 if current_tab == - 1 :
518- return None
519-
520- try :
521- # Update the preview
522- frame .load_html (markdown (textboxes [current_tab ].get ("1.0" , tk .END )))
523- except :
524- # Only update it if it's markdown or none
525- if file_extensions [current_tab ] == "md" :
526- # If the preview window was opened manually
527- previewWindowCreation ()
562+ try :
563+ frame .load_html (markdown ("" ))
564+ except :
565+ return None
566+ else :
567+ try :
568+ # Update the preview
569+ frame .load_html (markdown (textboxes [current_tab ].get ("1.0" , tk .END )))
570+ except :
571+ # Only update it if it's markdown or none
572+ if file_extensions [current_tab ] == "md" and not override :
573+ # If the preview window was opened manually
574+ previewWindowCreation ()
528575
529- def trackChanges (Event = None ):
576+ def trackChanges (Event = None , override = False ):
530577 global prev_key
531578
532579 current_tab = getCurrentTab ()
533580 if current_tab == - 1 :
534581 return None
535582
536583 # Set save status to False if it's been changed
537- key_ignore = ["Control_L" , "Control_R" , "Alt_L" , "Alt_R" , "Caps_Lock" , "Shift_L" , "Shift_R" , "Escape" , "Left" , "Right" , "Up" , "Down" , "App" , "Win_L" , "Win_R" , "F1" , "F2" , "F3" , "F4" , "F5" , "F6" , "F7" , "F8" , "F9" , "F10" , "F11" , "F12" ]
584+ key_ignore = ["Control_L" , "Control_R" , "Alt_L" , "Alt_R" ]
538585 cur_key = Event .keysym
539- if not (((prev_key in key_ignore [ 0 : 4 ] ) and (cur_key in "abcdefghijklmnopqrstuvwxyz" )) or (cur_key in key_ignore )):
586+ if not (((prev_key in key_ignore ) and (cur_key in "abcdefghijklmnopqrstuvwxyz" )) or (cur_key in key_ignore )):
540587 setSaveStatus (False , current_tab )
541588
542589 prev_key = Event .keysym
543590
544- if Event .keysym in ["space" , "Return" , "quoteleft" , "asciitilde" , "exclam" , "at" , "numbersign" , "dollar" , "percent" , "asciicircum" , "ampersand" , "asterisk" , "parenleft" , "parenright" , "underscore" , "plus" , "braceleft" , "braceright" , "bar" , "colon" , "less" , "greater" , "question" , "minus" , "equal" , "bracketleft" , "bracketright" , "backslash" , "semicolon" , "quoteright" , "comma" , "period" , "slash" , "Tab" ]:
591+ if ( Event .keysym in ["space" , "Return" , "quoteleft" , "asciitilde" , "exclam" , "at" , "numbersign" , "dollar" , "percent" , "asciicircum" , "ampersand" , "asterisk" , "parenleft" , "parenright" , "underscore" , "plus" , "braceleft" , "braceright" , "bar" , "colon" , "less" , "greater" , "question" , "minus" , "equal" , "bracketleft" , "bracketright" , "backslash" , "semicolon" , "quoteright" , "comma" , "period" , "slash" , "Tab" ]) or ( override ) :
545592 global file_histories , current_versions
546593
547594 # Check if the first version is empty
@@ -562,7 +609,7 @@ def trackChanges(Event=None):
562609 file_histories [current_tab ][current_versions [current_tab ]] = file_histories [current_tab ][current_versions [current_tab ]][:- 1 ]
563610
564611 # Update the preview
565- updatePreview ()
612+ updatePreview (override = True )
566613
567614def cut (Event = None ):
568615 current_tab = getCurrentTab ()
@@ -704,6 +751,8 @@ def changeToBold(Event=None):
704751
705752 setSaveStatus (False , current_tab )
706753
754+ return "break"
755+
707756def changeToItalic (Event = None ):
708757 global file_format_tag_nums
709758
@@ -724,6 +773,8 @@ def changeToItalic(Event=None):
724773
725774 setSaveStatus (False , current_tab )
726775
776+ return "break"
777+
727778def changeToNormal (Event = None ):
728779 global file_format_tag_nums
729780
@@ -744,6 +795,8 @@ def changeToNormal(Event=None):
744795
745796 setSaveStatus (False , current_tab )
746797
798+ return "break"
799+
747800def changeTextColour (Event = None ):
748801 global file_format_tag_nums
749802
@@ -768,6 +821,8 @@ def changeTextColour(Event=None):
768821
769822 setSaveStatus (False , current_tab )
770823
824+ return "break"
825+
771826def increaseFont (Event = None ):
772827 global file_format_tag_nums , font_sizes
773828
@@ -791,8 +846,9 @@ def increaseFont(Event=None):
791846 file_format_tag_nums [current_tab ] += 1
792847
793848 setSaveStatus (False , current_tab )
794- except :
795- pass
849+ except : pass
850+
851+ return "break"
796852
797853def decreaseFont (Event = None ):
798854 global file_format_tag_nums , font_sizes
@@ -817,8 +873,9 @@ def decreaseFont(Event=None):
817873 file_format_tag_nums [current_tab ] += 1
818874
819875 setSaveStatus (False , current_tab )
820- except :
821- pass
876+ except : pass
877+
878+ return "break"
822879
823880def showQuickMenu (Event = None ):
824881 try :
@@ -828,7 +885,7 @@ def showQuickMenu(Event=None):
828885
829886def addNewTab (Event = None ):
830887 # Create new textbox
831- textboxes .append (tk . Text (tab_panes , state = tk .NORMAL , font = (default_font_type , default_font_size , "normal" ), cursor = "xterm" , wrap = "word" ))
888+ textboxes .append (CustomText (tab_panes , state = tk .NORMAL , font = (default_font_type , default_font_size , "normal" ), cursor = "xterm" , wrap = "word" ))
832889
833890 # Create new tab info slot in arrays
834891 file_save_locations .append ("" )
@@ -860,21 +917,25 @@ def addNewTab(Event=None):
860917 bindtags = textboxes [- 1 ].bindtags ()
861918 textboxes [- 1 ].bindtags ((bindtags [2 ], bindtags [0 ], bindtags [1 ], bindtags [3 ]))
862919
920+ # Track document changes and update markdown preview
921+ textboxes [- 1 ].bind ('<<TextModified>>' , trackChanges )
922+
863923 # Sets the tab focus to the newly created tab
864924 tab_panes .select (tab_panes .tabs ()[- 1 ])
865925 textboxes [- 1 ].focus ()
866926
927+ updatePreview ()
928+
867929 return "break"
868930
869931def closeCurrentTab (Event = None ):
870932 current_tab = getCurrentTab ()
871933 if current_tab == - 1 :
872- return None
934+ quitApp ()
873935
874936 close_tab_confirm = True
875- for save_status in saved :
876- if save_status == False :
877- close_tab_confirm = False
937+ if saved [current_tab ] == False :
938+ close_tab_confirm = False
878939
879940 if not close_tab_confirm :
880941 close_tab_confirm = messagebox .askyesno ("Close Tab" , "Close current tab?\n \n Any unsaved changes will be lost." )
@@ -891,6 +952,8 @@ def closeCurrentTab(Event=None):
891952 file_format_tag_nums .pop (current_tab )
892953 saved .pop (current_tab )
893954
955+ updatePreview ()
956+
894957def getCurrentTab () -> int :
895958 try :
896959 return tab_panes .index ("current" )
@@ -914,6 +977,7 @@ Window Items
914977tab_panes = ttk .Notebook (root , cursor = "hand2" , padding = 5 )
915978tab_panes .pack (side = tk .TOP , fill = tk .BOTH , expand = 1 )
916979tab_panes .enable_traversal ()
980+ tab_panes .bind ("<<NotebookTabChanged>>" , updatePreview ) # https://stackoverflow.com/a/44092163
917981
918982# Create the first tab
919983addNewTab ()
@@ -963,8 +1027,7 @@ filemenu.add_separator()
9631027filemenu .add_command (label = "Save" , accelerator = "Ctrl+S" , command = saveFile )
9641028root .bind_all ("<Control-s>" , saveFile )
9651029
966- filemenu .add_command (label = "Save As" , accelerator = "Ctrl+Shift+S" , command = saveFileAs )
967- root .bind_all ("<Control-S>" , saveFileAs )
1030+ filemenu .add_command (label = "Save As" , command = saveFileAs )
9681031
9691032filemenu .add_separator ()
9701033
@@ -979,13 +1042,12 @@ filemenu.add_separator()
9791042filemenu .add_command (label = "New Tab" , accelerator = "Ctrl+T" , command = addNewTab )
9801043root .bind ("<Control-t>" , addNewTab )
9811044
982- filemenu .add_command (label = "Close Tab" , accelerator = "Alt +W" , command = closeCurrentTab )
983- root .bind_all ("<Alt -w>" , closeCurrentTab )
1045+ filemenu .add_command (label = "Close Tab" , accelerator = "Ctrl +W" , command = closeCurrentTab )
1046+ root .bind_all ("<Control -w>" , closeCurrentTab )
9841047
9851048filemenu .add_separator ()
9861049
987- filemenu .add_command (label = "Exit" , accelerator = "Ctrl+W" , command = quitApp )
988- root .bind_all ("<Control-w>" , quitApp )
1050+ filemenu .add_command (label = "Exit" , command = quitApp )
9891051md_preview_window .bind_all ("<Control-w>" , closePreview )
9901052
9911053# Edit menu items
@@ -1019,9 +1081,7 @@ editmenu.add_command(label="Close Markdown Preview", accelerator="Ctrl+Shift+P",
10191081root .bind_all ("<Control-P>" , closePreview )
10201082md_preview_window .bind_all ("<Control-P>" , closePreview )
10211083
1022- editmenu .add_command (label = "Update Markdown Preview" , accelerator = "Ctrl+E" , command = updatePreview )
1023- root .bind_all ("<Control-e>" , updatePreview )
1024- md_preview_window .bind_all ("<Control-e>" , updatePreview )
1084+ editmenu .add_command (label = "Update Markdown Preview" , command = updatePreview )
10251085
10261086editmenu .add_separator ()
10271087
@@ -1031,22 +1091,21 @@ editmenu.add_command(label="Edit Preferences", command=openPreferences)
10311091textfontmenu .add_command (label = "Arial" )
10321092
10331093textsizemenu .add_command (label = "Increase Font Size" , accelerator = "Ctrl+Shift++" , command = increaseFont )
1034- root .bind_all ("<Control-+>" , increaseFont )
1094+ tab_panes .bind_all ("<Control-+>" , increaseFont )
10351095
10361096textsizemenu .add_command (label = "Decrease Font Size" , accelerator = "Ctrl+Shift+-" , command = decreaseFont )
1037- root .bind_all ("<Control-_>" , decreaseFont )
1097+ tab_panes .bind_all ("<Control-_>" , decreaseFont )
10381098
1039- formatmenu .add_command (label = "Text Colour" , accelerator = "Alt+C" , command = changeTextColour )
1040- root .bind_all ("<Alt-c>" , changeTextColour )
1099+ formatmenu .add_command (label = "Text Colour" , command = changeTextColour )
10411100
10421101textstylemenu .add_command (label = "Normal" , accelerator = "Alt+N" , command = changeToNormal )
1043- root .bind_all ("<Alt-n>" , changeToNormal )
1102+ tab_panes .bind_all ("<Alt-n>" , changeToNormal )
10441103
1045- textstylemenu .add_command (label = "Bold" , accelerator = "Alt +B" , command = changeToBold )
1046- root .bind_all ("<Alt -b>" , changeToBold )
1104+ textstylemenu .add_command (label = "Bold" , accelerator = "Ctrl +B" , command = changeToBold )
1105+ tab_panes .bind_all ("<Control -b>" , changeToBold )
10471106
1048- textstylemenu .add_command (label = "Italic" , accelerator = "Alt +I" , command = changeToItalic )
1049- root . bind_all ("<Alt -i>" , changeToItalic )
1107+ textstylemenu .add_command (label = "Italic" , accelerator = "Ctrl +I" , command = changeToItalic )
1108+ tab_panes . bind ("<Control -i>" , changeToItalic )
10501109
10511110if update :
10521111 helpmenu .add_command (label = "Update Encryptext" , command = updateMenu )
@@ -1069,9 +1128,6 @@ menubar.add_cascade(label="Help", menu=helpmenu)
10691128# Display the menu bar
10701129root .config (menu = menubar )
10711130
1072- # Track document changes and update markdown preview
1073- root .bind ('<Key>' , trackChanges )
1074-
10751131"""
10761132Window Display
10771133"""
0 commit comments