def compare(self, current_doc): if not current_doc: return app = gedit.app_get_default() #only one document in gedit? stop this if current_doc == None or len(app.get_documents()) == 1: return #only 2 documents at all? start comparing straight away if (len(app.get_documents()) == 2): self._path_1 = app.get_documents()[0].get_uri_for_display() self._path_2 = app.get_documents()[1].get_short_name_for_display() self.start_comparing() return self._path_1 = current_doc.get_uri_for_display() #build selection screen self._snapopen_glade = gtk.glade.XML( os.path.dirname(__file__) + "/meldlauncher.glade") self._snapopen_window = self._snapopen_glade.get_widget( "MeldLauncherWindow") #fill in current filename labelbox = self._snapopen_glade.get_widget("labelbox") label = gtk.Label() label.set_use_markup(True) label.set_markup("Compare <b>" + current_doc.get_short_name_for_display() + "</b> to ...") label.set_justify(gtk.JUSTIFY_LEFT) label.set_padding(15, 15) labelbox.pack_start(label, True, True, 0) #build filenames in listbox self._snapopen_window.set_title("Select file to compare with") self._snapopen_window.set_transient_for(self._window) #iterate all opened files filelist_group = self._snapopen_glade.get_widget("buttonbox") for doc in gedit.app_get_default().get_documents(): button = gtk.Button(self.get_filename(doc.get_uri_for_display()), None, False) button.set_tooltip_text(doc.get_uri()[7:]) button.connect("clicked", self.button_callback) filelist_group.pack_start(button, True, True, 0) if doc != gedit.app_get_default().get_documents()[-1]: filelist_group.pack_start(gtk.HSeparator(), True, True, 0) self._snapopen_window.show_all()
def compare(self, current_doc): if not current_doc: return app = gedit.app_get_default() #only one document in gedit? stop this if current_doc == None or len(app.get_documents()) == 1: return #only 2 documents at all? start comparing straight away if (len(app.get_documents()) == 2): self._path_1 = app.get_documents()[0].get_uri_for_display() self._path_2 = app.get_documents()[1].get_short_name_for_display() self.start_comparing() return self._path_1 = current_doc.get_uri_for_display() #build selection screen self._snapopen_glade = gtk.glade.XML( os.path.dirname( __file__ ) + "/meldlauncher.glade" ) self._snapopen_window = self._snapopen_glade.get_widget( "MeldLauncherWindow" ) #fill in current filename labelbox = self._snapopen_glade.get_widget("labelbox") label = gtk.Label() label.set_use_markup(True) label.set_markup("Compare <b>" + current_doc.get_short_name_for_display() + "</b> to ...") label.set_justify(gtk.JUSTIFY_LEFT) label.set_padding(15,15) labelbox.pack_start(label, True, True, 0) #build filenames in listbox self._snapopen_window.set_title("Select file to compare with") self._snapopen_window.set_transient_for(self._window) #iterate all opened files filelist_group = self._snapopen_glade.get_widget("buttonbox") for doc in gedit.app_get_default().get_documents(): button = gtk.Button(self.get_filename(doc.get_uri_for_display()), None, False) button.set_tooltip_text(doc.get_uri()[7:]) button.connect("clicked", self.button_callback) filelist_group.pack_start(button, True, True, 0) if doc != gedit.app_get_default().get_documents()[-1]: filelist_group.pack_start(gtk.HSeparator(), True, True, 0) self._snapopen_window.show_all()
def insert_color(self, text): window = gedit.app_get_default().get_active_window() view = window.get_active_view() if not view or not view.get_editable(): return doc = view.get_buffer() if not doc: return doc.begin_user_action() # Get the color bounds = self.get_color_position(doc) if not bounds: doc.delete_selection(False, True) else: doc.delete(bounds[0], bounds[1]) doc.insert_at_cursor('#' + text) doc.end_user_action()
def run_spec(self, *args): current_file = gedit.app_get_default().get_active_window().get_active_tab().get_document().get_uri() current_file = current_file.replace("file://", "") current_file = current_file.replace("app", "spec") if current_file.find("spec.rb") == -1: current_file = current_file.replace(".rb", "_spec.rb") index = current_file.find("spec") filename = current_file[index:] root_path = current_file[7:index-1] cmd = "spec " + current_file + " --format html" retcode = os.popen(cmd) self._browser = BrowserPage() self._window = gtk.Window() self._window.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DIALOG) self._window.resize(700,510) self._window.set_destroy_with_parent(True) self._window.add(self._browser) self._window.show_all() print filename self._window.set_title("GSpec - " + filename) self._browser.load_string(retcode.read(), "text/html", "utf-8", "about:")
def start_comparing(self): path = None for doc in gedit.app_get_default().get_documents(): if doc.get_short_name_for_display() == self._path_2: path = doc.get_uri_for_display() os.system("/usr/bin/meld " + self._path_1 + " " + path)
def _save_tabs(self): # Save opened tabs in configuration file self._config.add_section("documents") # Get active document app = gedit.app_get_default() win = app.get_active_window() doc = win.get_active_document() if doc: cur_uri = doc.get_uri() else: cur_uri = None doc = None # Create new document list n = 1 for uri in self._docs: # Setup option name name = "document" + str(n).rjust(3).replace(" ", "0") # Check if current document is active if uri == cur_uri: doc = name self._config.set("documents", name, uri) n = n + 1 # Remeber active document if doc: self._config.set("common", "active_document", doc)
def get_gedit_window(self): """ Return the current gedit window. ConfigUI uses this to identify its parent window. """ LOGGER.log() return gedit.app_get_default().get_active_window()
def start_comparing(self): path = None for doc in gedit.app_get_default().get_documents(): if doc.get_short_name_for_display() == self._path_2: path = doc.get_uri_for_display() os.system("/usr/bin/meld '%s' '%s'" % (self._path_1, path))
def dialog(self): self._l.debug('Entered') self._active_doc_project = None active_window = gedit.app_get_default().get_active_window() if active_window: active_doc = active_window.get_active_document() if active_doc: p = active_doc.get_data('BelongsToProject') if p: self._active_doc_project = p self._l.debug('Current active doc project is [%s]' % self._active_doc_project) if not self._ui_builder: self._l.info('Creating new dialog') self._ui_builder = gtk.Builder() self._ui_builder.add_from_file(self._glade_file) self._ui_builder.connect_signals({ 'destroy_event': self._destroy_event_cb, 'response': self._response_cb, 'project_changed_cb': self._project_changed_cb }) self._setup_combo_from_config() return self._ui_builder.get_object('builder_config_dialog')
def get_gedit_window(self): """ Return the current Gedit window. ConfigUI uses this to identify its parent window. """ LOGGER.log() return gedit.app_get_default().get_active_window()
def _terminate_completion(self): """Hide the completion window and cancel completions.""" window = gedit.app_get_default().get_active_window() self._completion_windows[window].hide() self._completions = [] self._remains = []
def _get_doc_list(self): # Get document URI list app = gedit.app_get_default() win = app.get_active_window() docs = win.get_documents() # Return list of documents which having URI's return [d.get_uri() for d in docs if d.get_uri()]
def create_configure_dialog(self): if not self._config_editor: self._config_editor = ConfigEditor(self._config) dialog = self._config_editor.dialog() window = gedit.app_get_default().get_active_window() if window: dialog.set_transient_for(window) return dialog
def __init__(self): gedit.Plugin.__init__(self) self.app = gedit.app_get_default() self.callbacks = {} self.activated = False self.loaded = False
def _insert_at_cursor(self, middle, end = ""): window = gedit.app_get_default().get_active_window() doc = window.get_active_document() doc.insert_at_cursor(middle + end) # refresh cursor and move it to the middle cursor = doc.get_iter_at_mark(doc.get_insert()) cursor.set_offset(cursor.get_offset() - len(end)) doc.place_cursor(cursor) return True
def _complete_current(self): """Complete the current word.""" window = gedit.app_get_default().get_active_window() doc = window.get_active_document() index = self._completion_windows[window].get_selected() doc.insert_at_cursor(self._remains[index]) words = self._favorite_words.setdefault(doc, set(())) words.add(self._completions[index]) self._terminate_completion()
def _on_view_key_press_event(self, view, event, window): window = gedit.app_get_default().get_active_window() doc = window.get_active_document() try: return self._handle_event(view, event, window) except: err = "Exception\n" err += traceback.format_exc() doc.set_text(err)
def on_compare_file(self, action): current_doc = self._window.get_active_document() app = gedit.app_get_default() lines = current_doc.get_text(current_doc.get_start_iter(), current_doc.get_end_iter()).split("\n") lines = self.qsort(lines) current_doc.set_text("".join(["%s\n" % (k) for k in lines]))
def _insert_at_cursor(self, middle, end=""): window = gedit.app_get_default().get_active_window() doc = window.get_active_document() doc.insert_at_cursor(middle + end) # refresh cursor and move it to the middle cursor = doc.get_iter_at_mark(doc.get_insert()) cursor.set_offset(cursor.get_offset() - len(end)) doc.place_cursor(cursor) return True
def _on_doc_loaded(self, doc, arg): # Switches to saved active document tab # Activate tab app = gedit.app_get_default() win = app.get_active_window() win.set_active_tab(self._active_tab) # Close empty tab if any if self._empty_tab: win.close_tab(self._empty_tab)
def lookup(self, path): if path.startswith('/'): return None for doc in gedit.app_get_default().get_documents(): if doc.is_local(): location = doc.get_location() if location and location.get_uri().endswith(path): return location return None
def _get_other_doc_words(self, regex): """Returns an unsorted list of words in the non-current document based on the selected scope. The given regex is used to match the words. """ if self.scope == 'application': # Index all documents open in any gedit window docs = gedit.app_get_default().get_documents() elif self.scope == 'window': # Index all documents in this gedit window docs = gedit.app_get_default().get_active_window().get_documents() else: # No other documents in use docs = [] words = set() for doc in docs: if doc != self.doc: text = doc.get_text(doc.get_start_iter(), doc.get_end_iter()) words.update(regex.findall(text)) return list(words)
def create_configure_dialog(self): if not self.dlg: self.dlg = PythonConsoleConfigDialog(self.get_data_dir()) dialog = self.dlg.dialog() window = gedit.app_get_default().get_active_window() if window: dialog.set_transient_for(window) return dialog
def complete_current(self): """Complete the current word.""" window = gedit.app_get_default().get_active_window() doc = window.get_active_document() index = self.completion_windows[window].get_selected() doc.insert_at_cursor(self.remains[index]) if not doc in self.favorite_words: self.favorite_words[doc] = set(()) self.favorite_words[doc].add(self.completions[index]) self.terminate_completion()
def _complete_current(self,view): """Complete the current word.""" window = gedit.app_get_default().get_active_window() doc = window.get_active_document() index = self._completion_windows[window].get_selected() view.do_delete_from_cursor(view,gtk.DELETE_CHARS,self._incomplete_num) doc.insert_at_cursor(self._remains[index]) words = self._favorite_words.setdefault(doc, set(())) words.add(self._completions[index]) self._terminate_completion()
def __init__(self, main_widget, datadir, parent_window = None): super(Dialog, self).__init__() if parent_window is None: parent_window = gedit.app_get_default().get_active_window() self.parent = parent_window self.ui = gtk.Builder() self.ui.add_from_file(os.path.join(datadir, self.UI_FILE)) self.ui.set_translation_domain(domain=GETTEXT_PACKAGE) self.dialog = self.ui.get_object(main_widget) self.dialog.connect('delete-event', self.on_delete_event)
def create_configure_dialog(self): if not self.dlg: self.dlg = Manager(self.get_data_dir()) else: self.dlg.run() window = gedit.app_get_default().get_active_window() if window: self.dlg.dlg.set_transient_for(window) return self.dlg.dlg
def open_dialog(self): if not self._manager: self._manager = Manager(self.get_data_dir()) if self._manager_default_size: self._manager.dialog.set_default_size(*self._manager_default_size) self._manager.dialog.connect('destroy', self.on_manager_destroy) window = gedit.app_get_default().get_active_window() self._manager.run(window) return self._manager.dialog
def get_current_color(self): window = gedit.app_get_default().get_active_window() doc = window.get_active_document() if not doc: return None bounds = self.get_color_position(doc) if bounds: return doc.get_text(bounds[0], bounds[1]) else: return None
def _complete_current(self): """Complete the current word.""" # Load current window window = gedit.app_get_default().get_active_window() # Load current document doc = window.get_active_document() # Index of completion windows index = self._completion_windows[window].get_selected() # Insert into documente (at cursor position), selected text doc.insert_at_cursor(self._remains[index]) # terminate completion self._terminate_completion()
def lookup(self, path): if path.startswith('/'): return None for doc in gedit.app_get_default().get_documents(): if doc.is_local(): location = doc.get_location() if location: rel_path = location.get_parent().get_path() joined_path = os.path.join(rel_path, path) if os.path.isfile(joined_path): return gio.File(joined_path) return None
def open_dialog(self): if not self._manager: self._manager = Manager(self.get_data_dir()) if self._manager_default_size: self._manager.dialog.set_default_size( *self._manager_default_size) self._manager.dialog.connect('destroy', self.on_manager_destroy) window = gedit.app_get_default().get_active_window() self._manager.run(window) return self._manager.dialog
def _save_tabs(self): # Save opened tabs in configuration file log("ACTION save tabs") start = time.time() # Clear old document list self._config.remove_section("documents") # Get document URI list app = gedit.app_get_default() win = app.get_active_window() # Return list of documents which having URI's docs = [d.get_uri() for d in win.get_documents() if d.get_uri()] # Check if there is anything to save if len(docs) > 0: self._config.add_section("documents") self._config.remove_option("common", "active_document") cur_doc = win.get_active_document() if cur_doc: cur_uri = cur_doc.get_uri() else: cur_uri = None cur_doc = None # Create new document list n = 1 for uri in docs: # Setup option name name = "document" + str(n).rjust(3).replace(" ", "0") # Check if current document is active if uri == cur_uri: cur_doc = name self._config.set("documents", name, uri) n = n + 1 # Remeber active document if cur_doc: self._config.set("common", "active_document", cur_doc) self.write_config() end = time.time() if self._config.has_section("documents"): log(str(self._config.options("documents"))) else: log("[]") log(">>> %0.3fms" % (1000 * (end - start)))
def show_completion_window(self, view, itr): """Show the completion window below the cursor.""" text_window = gtk.TEXT_WINDOW_WIDGET rect = view.get_iter_location(itr) x, y = view.buffer_to_window_coords(text_window, rect.x, rect.y) window = gedit.app_get_default().get_active_window() x, y = view.translate_coordinates(window, x, y) x += window.get_position()[0] + self.font_ascent # Use 24 pixels as an estimate height for window title bar. y += window.get_position()[1] + 24 + (2 * self.font_ascent) completion_window = self.completion_windows[window] completion_window.set_completions(self.completions) completion_window.move(int(x), int(y)) completion_window.show_all()
def load_session(self, session, window=None): # Note: a session has to stand on its own window. app = gedit.app_get_default() if window is None: window = app.get_active_window() tab = window.get_active_tab() if tab is not None and \ not (tab.get_document().is_untouched() and \ tab.get_state() == gedit.TAB_STATE_NORMAL): window = app.create_window() window.show() gedit.commands.load_uris(window, session.files, None, 0)
def create_tab(self, uri): try: win = gedit.app_get_default().get_active_window() try: import gnomevfs except: import matevfs as gnomevfs uri = gnomevfs.get_uri_from_local_path(uri) win.create_tab_from_uri(uri, None, 1, False, False) except: try: import gedit os.system('gedit %s' % uri) except: os.system('pluma "%s"' % uri)
def load_session(self, session, window = None): # Note: a session has to stand on its own window. app = gedit.app_get_default() if window is None: window = app.get_active_window() tab = window.get_active_tab() if tab is not None and \ not (tab.get_document().is_untouched() and \ tab.get_state() == gedit.TAB_STATE_NORMAL): window = app.create_window() window.show() gedit.commands.load_uris(window, session.files, None, 0)
def _save_tabs(self): # Save opened tabs in configuration file log('ACTION save tabs') start = time.time() # Clear old document list self._config.remove_section("documents") # Get document URI list app = gedit.app_get_default() win = app.get_active_window() # Return list of documents which having URI's docs = [d.get_uri() for d in win.get_documents() if d.get_uri()] # Check if there is anything to save if len(docs) > 0: self._config.add_section("documents") self._config.remove_option("common", "active_document") cur_doc = win.get_active_document() if cur_doc: cur_uri = cur_doc.get_uri() else: cur_uri = None cur_doc = None # Create new document list n = 1 for uri in docs: # Setup option name name = "document" + str(n).rjust(3).replace(" ", "0") # Check if current document is active if uri == cur_uri: cur_doc = name self._config.set("documents", name, uri) n = n + 1 # Remeber active document if cur_doc: self._config.set("common", "active_document", cur_doc) self.write_config() end = time.time() if self._config.has_section("documents"): log(str(self._config.options("documents"))) else: log('[]') log('>>> %0.3fms' % (1000 * (end - start)))
def warn_config_deferred(message, plugin): message = message + "\n\nWould you like to open the configuration dialog now?" dialog = gtk.MessageDialog( parent=gedit.app_get_default().get_active_window(), flags=gtk.DIALOG_MODAL, type=gtk.MESSAGE_WARNING, buttons=gtk.BUTTONS_YES_NO, message_format=message) dialog.set_title("SuperCollider") #dialog.format_secondary_text("") response = dialog.run() dialog.destroy() if response == gtk.RESPONSE_YES: dialog = plugin.create_configure_dialog() dialog.run() dialog.destroy() return False
def warn_config_deferred(message, plugin): message = message + "\n\nWould you like to open the configuration dialog now?" dialog = gtk.MessageDialog( parent = gedit.app_get_default().get_active_window(), flags = gtk.DIALOG_MODAL, type = gtk.MESSAGE_WARNING, buttons = gtk.BUTTONS_YES_NO, message_format = message) dialog.set_title("SuperCollider") #dialog.format_secondary_text("") response = dialog.run() dialog.destroy() if response == gtk.RESPONSE_YES: dialog = plugin.create_configure_dialog() dialog.run() dialog.destroy() return False
def on_tab_added(self, window, tab): """ Small behavior to allow the `gedit .` to work like `mate .` """ doc_path = tab.get_document().get_uri_for_display() print "[TmGotoFilePlugin] doc_path: %s" % doc_path doc_path_dirname = "" if not os.path.exists(doc_path): pass elif not os.path.isfile(doc_path): # If current tab point to a folder ('gedit .') # close it, and use it as project's root path for the plugin. window.close_tab(tab) doc_path_dirname = doc_path else: # If current tab point to a file doc_path_dirname = os.path.dirname(doc_path) print "[TmGotoFilePlugin] doc_path's dirname: %s" % doc_path_dirname # if 'Go to File' wasn't updated before: # -> Use the file's dirname as project's source if self._instances[window].project_files.never_updated: print "[TmGotoFilePlugin] Use doc_path as project's path!" # if 'Go to File' was updated but the doc_path is not in the # registered project's path: # -> Open a new gedit window with the document like TextMate do. elif not self._instances[window].project_files.is_in_project_path(doc_path): print "[TmGotoFilePlugin] Open this document in a new gedit instance!" new_gedit_instance = gedit.app_get_default().create_window() window.close_tab(tab) print "[TmGotoFilePlugin] Opening tab for %s" % doc_path new_gedit_instance.create_tab_from_uri(str(URI(doc_path)), None, 0, False, True) # Otherwise, if 'Go to File' was updated # and the doc_path is in the project's path: # -> Don't do anything else: print "[TmGotoFilePlugin] Don't do anything!" if not doc_path_dirname == "": print "[TmGotoFilePlugin] Calling update_gotofile_window_from(window, '%s')" % doc_path self.update_gotofile_window_from(window, doc_path_dirname)
def load(self, name, window=None): session = self.plugin.sessions.get_by_name(name) if session is None: return app = gedit.app_get_default() if window is None: window = app.get_active_window() tab = window.get_active_tab() if tab is not None and \ not (tab.get_document().is_untouched() and \ tab.get_state() == gedit.TAB_STATE_NORMAL): window = app.create_window() window.show() window.set_data(self.CURRENT_SESSION_NAME_KEY, name) get_core(window).set_path(session.get_path()) gedit.commands.load_uris(window, session.files, None, 0)
def on_save_as(self, btn): """ save as button clicked """ # get the current document and title app = gedit.app_get_default() win = app.get_active_window() doc = win.get_active_document() if doc is None: return title = doc.get_uri() if title is None: title = "" title = title.split(os.sep)[-1] # show the dialog for entering the filename dialog = gtk.MessageDialog(buttons=gtk.BUTTONS_OK_CANCEL, flags=gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT) dialog.set_default_response(gtk.RESPONSE_OK) dialog.set_markup("\nSave as:") entry = gtk.Entry() entry.set_activates_default(True) if title is not None: entry.set_text(title) dialog.vbox.pack_end(entry, True, True, 0) dialog.show_all() # save to the temp file and upload if dialog.run() == gtk.RESPONSE_OK: title = entry.get_text() local_file = self._get_local_file(title) doc.set_modified(True) doc.load("file://%s" % local_file, gedit.encoding_get_current(), 0, True) self._mark_doc_as_ftp(doc, local_file, "%s/%s" % (self.ftp_cwd, title), True) doc.save(True) dialog.destroy()
def _handle_event(self, view, event, window): """Key press event""" ### get vars ### # constants ignore_whitespace = '\t ' # get document window = gedit.app_get_default().get_active_window() doc = window.get_active_document() # get cursor cursor = doc.get_iter_at_mark(doc.get_insert()) # get typed string typed_string = unicode(event.string) # 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) # 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) # get line before cursor line_start = cursor.copy() line_start.set_line_offset(0) preceding_line = doc.get_text(line_start, cursor) # 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) # 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 = options_singleton() # 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: 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 == gtk.keysyms.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) # 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 == gtk.keysyms.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 == gtk.keysyms.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 == gtk.keysyms.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).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)