diff --git a/pyvim/commands/commands.py b/pyvim/commands/commands.py index 4e29802..f03459b 100644 --- a/pyvim/commands/commands.py +++ b/pyvim/commands/commands.py @@ -318,7 +318,7 @@ def write(editor, location, force=False): if location is None and eb.location is None: editor.show_message(_NO_FILE_NAME) else: - eb.write(location) + eb.write(location, force) @location_cmd('wq', accepts_force=True) @@ -327,7 +327,7 @@ def write_and_quit(editor, location, force=False): Write file and quit. """ write(editor, location, force=force) - editor.cli.set_return_value('') + quit(editor, all_=False, force=force) @cmd('cq') @@ -341,17 +341,13 @@ def quit_nonzero(editor): sys.exit(1) -@cmd('wqa') -def write_and_quit_all(editor): +@location_cmd('wqa', accepts_force=True) +def write_and_quit_all(editor, location, force=False): """ Write current buffer and quit all. """ - eb = editor.window_arrangement.active_editor_buffer - if eb.location is None: - editor.show_message(_NO_FILE_NAME) - else: - eb.write() - quit(editor, all_=True, force=False) + write(editor, location, force=force) + quit(editor, all_=True, force=force) @cmd('h') diff --git a/pyvim/editor_buffer.py b/pyvim/editor_buffer.py index 47e1952..d987b5c 100644 --- a/pyvim/editor_buffer.py +++ b/pyvim/editor_buffer.py @@ -7,6 +7,7 @@ from six import string_types +import stat import os import weakref @@ -110,7 +111,7 @@ def reload(self): self.buffer.document = Document(text, cursor_position) self._file_content = text - def write(self, location=None): + def write(self, location=None, force=False): """ Write file to I/O backend. """ @@ -127,15 +128,40 @@ def write(self, location=None): self.editor.show_message('Unknown location: %r' % location) # Write it. - try: - io.write(self.location, self.buffer.text + '\n', self.encoding) - self.is_new = False - except Exception as e: - # E.g. "No such file or directory." - self.editor.show_message('%s' % e) - else: - # When the save succeeds: update: _file_content. - self._file_content = self.buffer.text + toggle_write_permission = False + done = False + while (not done): + try: + io.write(self.location, self.buffer.text + '\n', self.encoding) + self.is_new = False + if toggle_write_permission: + try: + os.chmod(self.location, os.stat(self.location).st_mode & ~stat.S_IWRITE) + done = True + except Exception as e: + self.editor.show_message('%s' % e) + done = True + except Exception as e: + if os.path.isfile(self.location) and (not os.access(self.location, os.W_OK)): # File is not writable + if force: + if not toggle_write_permission: + try: + os.chmod(self.location, os.stat(self.location).st_mode | stat.S_IWRITE) + toggle_write_permission = True + except Exception as e: + self.editor.show_message('%s' % e) + done = True + else: + # E.g. "No such file or directory." + self.editor.show_message('%s' % e) + done = True + else: + self.editor.show_message("'readonly' option is set (add ! to override)") + done = True + else: + # When the save succeeds: update: _file_content. + self._file_content = self.buffer.text + done = True def get_display_name(self, short=False): """