Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement w! #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions pyvim/commands/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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')
Expand All @@ -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')
Expand Down
46 changes: 36 additions & 10 deletions pyvim/editor_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from six import string_types

import stat
import os
import weakref

Expand Down Expand Up @@ -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.
"""
Expand All @@ -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):
"""
Expand Down