def insert_mode_handling(self):
        """Get and handle events for insert mode."""
        for event in SERVICES_NS.get_events():

            if event.type == QUIT:

                ### disable text editing events
                stop_text_input()

                ### raise app quitting exception
                raise QuitAppException

            ### if text is input, add such text

            elif event.type == TEXTINPUT:
                self.add_text(event.text)

            elif event.type == KEYDOWN:

                ### show help text for text editor
                ### according to text editor behavior
                ### set

                if event.key == K_F1:

                    ### disable text editing events
                    stop_text_input()

                    ### show help text

                    if USER_PREFS["TEXT_EDITOR_BEHAVIOR"] == "default":

                        open_htsl_link(
                            "nodezator://help.nodezator.pysite/"
                            "text-editor-default-behavior.htsl"
                        )

                    elif USER_PREFS["TEXT_EDITOR_BEHAVIOR"] == "vim-like":

                        open_htsl_link(
                            "nodezator://help.nodezator.pysite/"
                            "text-editor-vim-like-behavior.htsl"
                        )

                    ### restore text editing events and set the text input
                    ### rect, but only if app is not in play mode

                    if GENERAL_NS.mode_name != 'play':

                        start_text_input()

                        text_input_rect = EDITING_AREA_RECT.copy()
                        text_input_rect.height = 20
                        text_input_rect.top = EDITING_AREA_RECT.bottom
                        set_text_input_rect(text_input_rect)

                ### if escape key is pressed...

                elif event.key == K_ESCAPE:

                    ## if the text editor behavior is set
                    ## to be vim-like, exit insert mode
                    ## and update cursor position;

                    if USER_PREFS["TEXT_EDITOR_BEHAVIOR"] == "vim-like":
                        self.enable_normal_mode()

                ### move cursor

                elif event.key == K_UP:
                    self.go_up_ins()

                elif event.key == K_DOWN:
                    self.go_down_ins()

                elif event.key == K_LEFT:
                    self.go_left_ins()

                elif event.key == K_RIGHT:
                    self.go_right_ins()

                ### delete methods

                elif event.key == K_BACKSPACE:
                    self.delete_previous_or_merge_lines()

                elif event.key == K_DELETE:
                    self.delete_under_or_merge_lines()

                ### slice current line
                elif event.key in (K_RETURN, K_KP_ENTER):
                    self.break_line()

                ### TAB key adds 4 spaces

                elif event.key == K_TAB:

                    ### XXX as of now, we do have a tab
                    ### representation which is the tab
                    ### character itself ('\t'), but which
                    ### is rendered as 04 spaces (check
                    ### the textman.cache module, within
                    ### the SurfaceMap class to know more)
                    ###
                    ### for now, it can't be insert here,
                    ### though it will appear as an empty
                    ### space with the width of 04 spaces
                    ### if the tab character is already
                    ### in the text when loaded here in
                    ### this editor;
                    ###
                    ### in the future make it so the user
                    ### can configure whether pressing
                    ### the tab key inserts 04 spaces
                    ### as we do here or whether it should
                    ### insert a tab character, since we
                    ### do have a representation for it;
                    ###
                    ### furthermore, allow the user
                    ### to decide the number of spaces
                    ### the tab character representation
                    ### uses
                    self.add_text(" " * 4)

                ### ignore event if ctrl key is pressed
                ### since the combination of ctrl key and
                ### other keys doesn't produce characters
                ### of our interest (only strings like
                ### '\x08', etc.)
                elif event.mod & KMOD_CTRL:
                    pass

                ### snap cursor to different edges of
                ### text

                elif event.key == K_HOME:

                    if KMOD_SHIFT & event.mod:
                        self.go_to_top_ins()

                    else:
                        self.go_to_line_start_ins()

                elif event.key == K_END:

                    if KMOD_SHIFT & event.mod:
                        self.go_to_bottom_ins()

                    else:
                        self.go_to_line_end_ins()

                ### jump a page up or down (a page is the
                ### number of lines which fits the editing
                ### area) or snap to top or bottom of text

                elif event.key == K_PAGEUP:

                    if KMOD_SHIFT & event.mod:
                        self.go_to_top_ins()

                    else:
                        self.jump_page_up_ins()

                elif event.key == K_PAGEDOWN:

                    if KMOD_SHIFT & event.mod:
                        self.go_to_bottom_ins()

                    else:
                        self.jump_page_down_ins()

            elif event.type == MOUSEBUTTONUP:

                if event.button == 1:
                    self.te.mouse_release_action(event.pos)

                ### jump many lines up or down by using
                ### the mousewheel

                elif event.button == 4:
                    self.go_many_up_ins()

                elif event.button == 5:
                    self.go_many_down_ins()
Get and handle events for insert mode.
nodezator/textman/editor/cursor/modes/insert.py