def revert_file(self, revision): """Revert file to a local history revision""" if not self.rcs_dir: return Logger("LocalHist").log("revert " + self.file + " to " + revision) local = self.local_checkout(revision) if local: shutil.copymode(self.file, local) shutil.move(local, self.file) EditorBuffer.get(File(self.file), force=True)
def on_file_edited(hook, file): name = file.path l = len(name) # If the file that has been opened is not a changelog, return. if (name[l - 4:l] != '$log'): return # Get the basename of the log file to find the actual source. basename = name[max(name.rfind('\\'), name.rfind('/')) + 1:l - 4] # Eliminate potential $x trailing trail = basename.rfind('$') if trail != -1: basename = basename[0:trail] source = File(basename) buffer = EditorBuffer.get(source, False, False) log = EditorBuffer.get(file, False, False) # If the log is not empty, exit. if log.get_chars() != '': return # Query the Locations View for a list of visual differences for the source locations = Locations.list_locations("Visual differences", basename) prev = "" # Find out which is the "enclosing" program/package. loc = buffer.at(buffer.lines_count() - 1, 1) global_proc = loc.subprogram_name() i = 0 while i < len(locations) / 2: loc = buffer.at(locations[2 * i].line(), locations[2 * i].column()) prog = loc.subprogram_name() # If there are multiple changes within the same subprogram, display # the subprogram only once if (prog != prev) and (prog != global_proc): log.insert(log.end_of_buffer(), "(" + prog + "): \n") prev = prog i = i + 1 # Jump to the end of the first line log.current_view().goto(log.at(1, 1).end_of_line())
def indent_all(menu): for f in Project.root().sources(recursive=True): ed = EditorBuffer.get(f) ed.indent() ed.save() ed.close() Console().write("Done indenting")
def __init__(self, case_sensitive=0, backward=0, regexp=0): try: self.editor = EditorBuffer.get() self.loc = self.editor.current_view().cursor() self.end_loc = self.loc self.regexp = regexp self.case_sensitive = case_sensitive self.explicit_case_sensitive = False # # Automatic or from alt-c ? self.backward = backward self.stack = [(self.loc, self.end_loc, '', 0)] self.locked = False self.overlay = self.editor.create_overlay('isearch') self.overlay.set_property( 'background', bg_next_match_pref.get()) self.insert_overlays_id = 0 self.remove_overlays() CommandWindow.__init__( self, prompt=self.prompt(), on_changed=self.on_changed, on_cancel=self.on_cancel, on_key=self.on_key, on_activate=self.on_activate) except: pass
def on_file_closed(file): buffer = EditorBuffer.get(file, open=False) if buffer: line = buffer.current_view().cursor().line() column = buffer.current_view().cursor().column() file.set_property("lastloc_line", repr(line), persistent=True) file.set_property("lastloc_column", repr(column), persistent=True)
def sel_pipe(command, buffer=None): """Process the current selection in BUFFER through COMMAND, and replace that selection with the output of the command""" if not buffer: buffer = EditorBuffer.get() start = buffer.selection_start() end = buffer.selection_end() # Ignore white spaces and newlines at end, to preserve the rest # of the text if start != end: while end.get_char() == ' ' or end.get_char() == '\n': end = end - 1 text = buffer.get_chars(start, end) else: text = "" proc = Process(command) proc.send(text) proc.send(chr(4)) # Close input output = proc.get_result() with buffer.new_undo_group(): if start != end: buffer.delete(start, end) buffer.insert(start, output.rstrip())
def view_as_tree(): try: buffer = EditorBuffer.get() view = XMLViewer(buffer.file().path) view.parse_string(buffer.get_chars()) except: pass
def in_c_file(context): """Returns True if the focus is currently inside an c/cpp editor""" if not hasattr(context, "in_c_file"): buffer = EditorBuffer.get(open=False) context.in_c_file = (context.module_name == "Source_Editor" and buffer and buffer.file().language().lower() in ["c", "c++", "cpp"]) return context.in_c_file
def in_ada_file(context): """Returns True if the focus is currently inside an Ada editor""" if not hasattr(context, "in_ada_file"): buffer = EditorBuffer.get(open=False) context.in_ada_file = (context.module_name == "Source_Editor" and buffer and buffer.file().language().lower() == "ada") return context.in_ada_file
def in_xml_file(context): """Returns True if the focus is in an XML editor""" if not hasattr(context, "in_xml_file"): buffer = EditorBuffer.get(open=False) context.in_xml_file = (context.module_name == "Source_Editor" and buffer and buffer.file().language().lower() in ["xml", "html"]) return context.in_xml_file
def toggle_field(editor=None, backward=False, first=False): if not editor or not is_in_alias_expansion(editor): editor = EditorBuffer.get() try: if editor: reset_overlay(editor) editor.apply_overlay( editor.aliases_background_overlay, editor.alias_begin_mark.location().beginning_of_line(), editor.alias_end_mark.location() ) if editor.current_alias_mark_index is None: return if first: editor.current_alias_mark_index = 0 elif backward: editor.current_alias_mark_index -= 1 else: editor.current_alias_mark_index += 1 index = editor.current_alias_mark_index if index < 0: exit_alias_expand(editor) return if index > len(editor.alias_marks) - 1: if editor.last_alias_mark: editor.alias_move_expected = True editor.current_view().goto( editor.last_alias_mark.location()) editor.alias_move_expected = False exit_alias_expand(editor) return False editor.alias_move_expected = True try: editor.remove_all_slave_cursors() marks = editor.alias_marks[index] editor.current_view().goto(marks[0][0].location()) # Add multi cursors for every other mark if len(marks) > 1: for mark_begin, mark_end in marks[1:]: editor.add_cursor(mark_begin.location()) # Select the placeholder text for j, cursor in enumerate(editor.cursors()): cursor.move(marks[j][1].location(), True) finally: editor.alias_move_expected = False except AttributeError: return
def save_excursion(f, args, kwargs, undo_group=True): """ Save current buffer, cursor position and selection and execute f. (args and kwargs) are passed as arguments to f. They indicate that any number of parameters (named or unamed) can be passed in the usual way to save_excursion, and they will be transparently passed on to f. If undo_group is True, then all actions performed by f will be grouped so that the user needs perform only one single undo to restore previous start. Then restore the context as it was before, even in the case of abnormal exit. Example of use:: def my_subprogram(): def do_work(): pass # do actual work here save_excursion(do_work) See also the with_save_excursion decorator below for cases when you need to apply save_excursion to a whole function. """ mdi = MDI.current() buffer = EditorBuffer.get() view = buffer.current_view() cursor = view.cursor() start = buffer.selection_start().create_mark() end = buffer.selection_end().create_mark(left_gravity=False) had_selection = start.location() != end.location() try: if undo_group: with buffer.new_undo_group(): return f(*args, **kwargs) else: return f(*args, **kwargs) finally: try: # View might have been destroyed mdi.raise_window() view.goto(cursor) except Exception: # In this case use the next view available if any view = buffer.current_view() if not view: return if had_selection: buffer.select(start.location(), end.location()) else: buffer.current_view().goto(start.location()) start.delete() end.delete()
def expand_lsp_snippet(snippet): """ Expand the given LSP snippet in the current editor. """ editor = EditorBuffer.get(open=False, force=False) if not editor: return expand_alias(editor=editor, alias=snippet, from_lsp=True)
def onclick(self, text): matched = re.match(file_line_re, text) buffer = EditorBuffer.get(File(matched.group(2))) MDI.get_by_child(buffer.current_view()).raise_window() line = int(matched.group(3)) column = matched.group(5) if column is not None: buffer.current_view().goto(buffer.at(line, int(column))) else: buffer.current_view().goto(buffer.at(line, 1))
def describe_char(char=None): """Describe the unicode character under the cursor (name, value,...)""" if not char: char = EditorBuffer.get().current_view().cursor().get_char() uni = char.decode("utf-8") Console().write("Character: " + char + "\n") Console().write(" Name: " + unicodedata.name(uni) + "\n") Console().write(" Unicode: " + repr(ord(uni)) + " (U+" + hex(ord(uni))[2:] + ")\n") Console().write(" Category: " + unicodedata.category(uni) + "\n")
def on_activate(self, input): if input != "": buffer = EditorBuffer.get() input = input.strip() if input[0] in "0123456789": chr = unichr(int(input)) elif input[0] == 'x' and input[1] in "0123456789": chr = unichr(int(input[1:], 16)) else: chr = unicodedata.lookup(input) buffer.insert(buffer.current_view().cursor(), chr.encode('utf-8'))
def repeat_command(loc=None): """Repeat the last command that was executed, in Editor""" if not loc: loc = EditorBuffer.get().current_view().cursor() buffer = loc.buffer() (cmd, loc, maxloc) = CmdLine.get_scope(CmdLine.history[0], loc, buffer) with buffer.new_undo_group(): if cmd[0] == "s": CmdLine.do_replace(cmd[1:], loc, maxloc) elif cmd[0] == "d": CmdLine.do_delete_line(cmd[1:], loc, maxloc)
def next_close_tag(): """Move to the next closing tag""" buffer = EditorBuffer.get() loc = buffer.current_view().cursor() + 1 try: while loc.get_char() != "<" \ or (loc + 1).get_char() != "/": loc += 1 buffer.current_view().goto(loc) except: # End of file pass
def make_readonly(): """Make every other line readonly in the current file""" buffer = EditorBuffer.get() loc = buffer.at(1, 1) overlay = buffer.create_overlay("readonly") overlay.set_property("editable", False) while loc < buffer.end_of_buffer(): eol = loc.end_of_line() buffer.apply_overlay(overlay, loc, eol - 1) loc = loc.forward_line(2)
def display_local_vars(menu): """ Show in the Data Window the value for each all local variables (one box per variable). """ buffer = EditorBuffer.get() subp = text_utils.goto_subprogram_start(buffer.current_view().cursor()) if subp: entity = Entity(subp.block_name(), buffer.file(), subp.line()) vars = text_utils.get_local_vars(entity) debug = Debugger.get() for v in vars: debug.send("graph display " + v.name(), False)
def __init__(self): try: self.loc = EditorBuffer.get().current_view().cursor() CommandWindow.__init__(self, prompt=self.prompt(), on_cancel=self.on_cancel, on_key=self.on_key, on_activate=self.on_activate) self.set_background(Preference("Plugins/vi/bgcolor").get()) self.current_in_history = -1 self.current_cmd_line = "" # Before moving in the history except: pass
def quote_selection(): """ Replace XML special characters in the current selection (or current selection) by their equivalent XML entities. """ buffer = EditorBuffer.get() (min, max) = (buffer.selection_start(), buffer.selection_end()) if min == max: (min, max) = (buffer.current_view().cursor().beginning_of_line(), buffer.current_view().cursor().end_of_line()) with buffer.new_undo_group(): text = buffer.get_chars(min, max) text = xml.sax.saxutils.escape(text) buffer.delete(min, max) buffer.insert(min, text)
def goto_matching_tag(): """Go to the matching closing tag""" buffer = EditorBuffer.get() loc = buffer.current_view().cursor() try: while loc.get_char() != '<': loc -= 1 start = loc + 1 end = start.forward_word() loc = start.search('</' + buffer.get_chars(start, end - 1), dialog_on_failure=False, case_sensitive=True) if loc: buffer.current_view().goto(loc[0]) except: pass
def fmt_selection(): """Process the current selection through the "fmt" command to reformat paragraphs """ width = Preference("Src-Editor-Highlight-Column").get() buffer = EditorBuffer.get() prefix = None if buffer.file().language() == "ada": prefix = "--" loc = buffer.selection_start().beginning_of_line() while loc.get_char() == ' ': loc = loc + 1 prefix = '-p """' + (' ' * (loc.column() - 1)) + prefix + '"""' sel_pipe("fmt " + prefix + " -w " + repr(width), buffer)
def on_file_edited(file): try: # If the file was opened inside an editor (as opposed to a # QGen browser for instance) buffer = EditorBuffer.get(file, open=False) if not buffer: return # Do not change the line if the editor was already scrolled for # any reason cursor = buffer.current_view().cursor() if cursor.line() == 1 and cursor.column() == 1: line = file.get_property("lastloc_line") column = file.get_property("lastloc_column") buffer.current_view().goto(buffer.at(int(line), int(column))) except: pass
def on_edit(hook_name, file_name): """ Event handler on insert/delete. Mainly ensures that the current field in alias expansion is highlighted (via the aliases overlay) """ editor = EditorBuffer.get(file_name) # This hook is global: it could happen that we are calling it on another # editor than the one where the alias expansion is occurring: simply # return in this case. if not is_in_alias_expansion(editor): return if editor.current_alias_mark_index > 0: marks_list = editor.alias_marks[editor.current_alias_mark_index - 1] reset_overlay(editor) for mark_start, mark_end in marks_list: apply_overlay(editor, mark_start, mark_end, editor.aliases_overlay)
def expand_alias_action(): """ Action to expand the alias under cursor the editor """ editor = EditorBuffer.get(open=False, force=False) if not editor: return if is_in_alias_expansion(editor): return with editor.new_undo_group(): cursor_loc = editor.current_view().cursor().forward_char(-1) start_loc = goto_word_start(cursor_loc) alias_name = editor.get_chars(start_loc, cursor_loc) editor.delete(start_loc, cursor_loc) alias = Alias.get(alias_name) if alias: expand_alias(editor, alias)
def check_wf(): """Check whether the current XML document is well-formed""" try: file = EditorBuffer.get().file() handler = xml.sax.handler.ContentHandler() errors = GPSErrorHandler() xml.sax.parse(file.path, handler, errors) Locations.remove_category('XML well-formedness') if not errors.output: Console().write('Document is well-formed\n') else: Console().write(errors.output) Locations.parse(errors.output, 'XML well-formedness') except StopProcessing: Locations.parse(errors.output, 'XML well-formedness') except xml.sax.SAXParseException: Console().write('Unexpected error while parsing the XML document') except: Console().write('Unexpected error %s' % (traceback.format_exc(), ))
def on_move(hook_name, file_name, line, column): """ Event handler on cursor move. Gets out of alias expansion mode when the cursor gets out of the zone. """ editor = EditorBuffer.get(file_name) # This hook is global: it could happen that we are calling it on another # editor than the one where the alias expansion is occurring: simply # return in this case. # Similarly, if we are expecting a cursor move at this point, do not # exit the alias expansion mode. if not is_in_alias_expansion(editor) \ or move_expected_while_in_alias(editor): return index = editor.current_alias_mark_index start_mark, end_mark = editor.alias_marks[index][0] start_loc = start_mark.location() end_loc = end_mark.location() cursor_loc = editor.current_view().cursor() if not (start_loc <= cursor_loc <= end_loc): exit_alias_expand(editor)
def on_node_clicked(self, node_name, attrs, value): attr = self.parse_attrs(attrs) if node_name == "project" or node_name == "dependency": EditorBuffer.get(File(attr["name"] + ".gpr")) elif node_name == "file": EditorBuffer.get(File(attr["src"]))