Skip to content

Commit

Permalink
Merge pull request #10 from nakagami/force_write
Browse files Browse the repository at this point in the history
Implement w! prompt-toolkit#88
  • Loading branch information
nakagami authored Jul 7, 2024
2 parents f6e501e + 08679db commit 4b53e8b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
14 changes: 5 additions & 9 deletions pyvim/commands/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,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)
Expand All @@ -343,17 +343,13 @@ def quit_nonzero(editor):
editor.application.exit()


@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')
Expand Down
46 changes: 36 additions & 10 deletions pyvim/editor_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pyvim.completion import DocumentCompleter
from pyvim.reporting import report

import stat
import os
import weakref

Expand Down Expand Up @@ -127,7 +128,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.
"""
Expand All @@ -144,15 +145,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):
"""
Expand Down

0 comments on commit 4b53e8b

Please sign in to comment.