Skip to content

Commit

Permalink
Merge branch 'master' into more_quit
Browse files Browse the repository at this point in the history
  • Loading branch information
nakagami committed Jul 7, 2024
2 parents 88398ff + 4b53e8b commit e8c1e15
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
6 changes: 5 additions & 1 deletion pyvim/commands/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,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 Down Expand Up @@ -360,11 +360,15 @@ def write_all(editor):

@cmd('wqa')
def write_and_quit_all(editor):
@location_cmd('wqa', accepts_force=True)
def write_and_quit_all(editor, location, force=False):
"""
Write all changed buffers and quit all.
"""
write_all(editor)
quit_all(editor)
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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
long_description=long_description,
packages=find_packages('.'),
install_requires=[
'prompt_toolkit>=2.0.0,<3.1.0',
'prompt_toolkit',
'pyflakes', # For Python error reporting.
'pygments', # For the syntax highlighting.
'docopt', # For command line arguments.
Expand Down

0 comments on commit e8c1e15

Please sign in to comment.