def _handle_event(self, view, event, window):
        """Key press event"""
        ### get vars ###
        # constants
        ignore_whitespace = '\t '
        # get document
        doc = window.get_active_document()
        # get cursor
        cursor = doc.get_iter_at_mark(doc.get_insert())
        # get typed string
        typed_string = unicode(event.string, 'UTF-8')
        # get previous char
        prev_char = None
        if not cursor.get_line_offset() == 0:
            prev_char_pos = cursor.copy()
            prev_char_pos.set_line_offset(cursor.get_line_offset() - 1)
            prev_char = doc.get_text(prev_char_pos, cursor, False)
        # get next char
        next_char = None
        if not cursor.ends_line():
            next_char_pos = cursor.copy()
            next_char_pos.set_line_offset(cursor.get_line_offset() + 1)
            next_char = doc.get_text(cursor, next_char_pos, False)
        # get line before cursor
        line_start = cursor.copy()
        line_start.set_line_offset(0)
        preceding_line = doc.get_text(line_start, cursor, False)
        # get line after cursor
        line_end = cursor.copy()
        if not cursor.ends_line():
            line_end.forward_to_line_end()
        line_after = doc.get_text(cursor, line_end, False)
        # get whitespace in front of line
        whitespace_pos = 0
        whitespace = ""
        while len(preceding_line) > whitespace_pos and preceding_line[whitespace_pos] in ignore_whitespace:
            whitespace += preceding_line[whitespace_pos]
            whitespace_pos += 1
        # get options
        options = IntelligentTextCompletionOptions.get_instance()

        # Do not complete text after pasting text.
        if len(typed_string) > 1:
            return False
        typed_char = typed_string

        # GLOBALS
        open_close = {
            '"': '"',
            "'": "'",
            '(': ')',
            '{': '}',
            '[': ']',
        }

        ################### selected text ###################
        bounds = doc.get_selection_bounds()
        if len(bounds) > 0:
            # auto-close brackets and quotes
            if options.closeBracketsAndQuotes:
                for open, close in open_close.items():
                    if typed_char == open:
                        # get bounds data
                        off1 = bounds[0].get_offset()
                        off2 = bounds[1].get_offset()
                        # add open char
                        doc.place_cursor(bounds[0])
                        doc.insert_at_cursor(open)
                        # refresh cursor and move it
                        cursor = doc.get_iter_at_mark(doc.get_insert())
                        cursor.set_offset(cursor.get_offset() + (off2 - off1))
                        doc.place_cursor(cursor)
                        # add close char
                        doc.insert_at_cursor(close)
                        return True
            return False

        ################### auto-close brackets and quotes ###################
        if options.closeBracketsAndQuotes and prev_char != '\\':
            """ detect python comments """
            if typed_char == '"' and re.search('^[^"]*""$', preceding_line) and cursor.ends_line():
                return self._insert_at_cursor(typed_char + ' ', ' """')

            for check_char, add_char in open_close.items():
                # if character user is adding is the same as the one that
                # is auto-generated, remove the auto generated char
                if typed_char == add_char:
                    if not cursor.ends_line():
                        if next_char == add_char:
                            if check_char != add_char:
                                # don't remove ) when it's probably not auto-generated
                                preceding_check_chars = len(re.findall('\%s' % check_char, preceding_line))
                                preceding_add_chars = len(re.findall('\%s' % add_char, preceding_line))
                                following_check_chars = len(re.findall('\%s' % check_char, line_after))
                                following_add_chars = len(re.findall('\%s' % add_char, line_after))
                                if preceding_check_chars - preceding_add_chars > following_add_chars:
                                    continue
                                # don't remove ) when the line becomes complex
                                if following_check_chars > 0:
                                    continue
                            doc.delete(cursor, next_char_pos)
                            return False
                # typed_char equals char we're looking for
                if typed_char == check_char:
                    # check for unlogical adding
                    if check_char == add_char:
                        # uneven number of check_char's in front
                        if len(re.findall(check_char, preceding_line)) % 2 == 1:
                            continue
                        # uneven number of check_char's in back
                        if len(re.findall(check_char, line_after)) % 2 == 1:
                            continue
                    # don't add add_char if it is used around text
                    non_text_left =  ' \t\n\r,=+*:;.?!$&@%~<(){}[]-"\''
                    non_text_right = ' \t\n\r,=+*:;.?&@%~>)}]'
                    if not next_char and not check_char == "'":
                        # if we're just typing with nothing on the right,
                        # adding is OK as long as it isn't a "'".
                        pass
                    elif (not prev_char or prev_char in non_text_left) and (not next_char or next_char in non_text_right):
                        # this char is surrounded by nothing or non-text, therefore, we can add autotext
                        pass
                    elif check_char != add_char and (not next_char or next_char in non_text_right):
                        # this opening char has non-text on the right, therefore, we can add autotext
                        pass
                    else:
                        continue
                    # insert add_char
                    return self._insert_at_cursor(typed_char, add_char)
                # check backspace
                if event.keyval == 65288: # backspace
                    if prev_char == check_char and next_char == add_char:
                        doc.delete(cursor, next_char_pos)

        ################### auto-complete XML tags ###################
        if options.completeXML:
            if prev_char == "<" and typed_char == "/":
                start = doc.get_start_iter()
                preceding_document = doc.get_text(start, cursor, False)
                # analyse previous XML code
                closing_tag = get_closing_xml_tag(preceding_document)
                # insert code
                if closing_tag:
                    return self._insert_at_cursor(typed_char + closing_tag + ">")
                else:
                    return False # do nothing

        ################### detect lists ###################
        if options.detectLists:
            if event.keyval == 65293: # return
                # constants
                list_bullets = ['* ', '- ', '$ ', '> ', '+ ', '~ ']
                # cycle through all bullets
                for bullet in list_bullets:
                    if len(preceding_line) >= whitespace_pos + len(bullet):
                        if preceding_line[whitespace_pos:whitespace_pos + len(bullet)] == bullet:
                            # endlist function by double enter
                            if preceding_line == whitespace + bullet and bullet != '* ':
                                start = cursor.copy()
                                start.set_line_offset(len(whitespace))
                                doc.delete(start, cursor)
                                return True
                            return self._insert_at_cursor(typed_char + whitespace + bullet)

        ################### detect java-like comment ###################
        if event.keyval == 65293: # return
            # constants
            comments = {
                '/**' : (' * ', ' */'),
                '/*'  : (' * ', ' */'),
            }
            # cycle through all types of comment
            for comment_start, (comment_middle, comment_end) in comments.items():
                if preceding_line[whitespace_pos:] == comment_start:
                    add_middle = typed_char + whitespace + comment_middle
                    add_end = typed_char + whitespace + comment_end
                    return self._insert_at_cursor(add_middle, add_end)

        ################### auto-indent after function/list ###################
        if options.autoindentAfterFunctionOrList:
            if event.keyval == 65293: # return
                indent_triggers = {
                    '(': ')',
                    '{': '}',
                    '[': ']',
                    ':': '',
                }
                for indent_trigger, ending_char in indent_triggers.items():
                    if prev_char == indent_trigger:
                        if line_after:
                            # text between begin and ending brackets should come
                            # in the middle row
                            if ending_char != '' and ending_char in line_after:
                                ending_pos = line_after.find(ending_char)
                            else:
                                ending_pos = len(line_after)
                            end = cursor.copy()
                            end.set_line_offset(end.get_line_offset() + ending_pos)
                            ending_text = doc.get_text(cursor, end, False).strip()
                            doc.delete(cursor, end)

                            add_middle = typed_char + whitespace + get_tab_string(view)
                            add_end = ending_text + typed_char + whitespace
                        else:
                            add_middle = typed_char + whitespace + get_tab_string(view)
                            add_end = ""
                        return self._insert_at_cursor(add_middle, add_end)
dataset/ETHPy150Open nymanjens/gedit-intelligent-text-completion/gedit3-0--3-7/intelligent_text_completion.py/IntelligentTextCompletionPlugin._handle_event/original