actually nice support for reloading files

--HG--
branch : pmacs2
This commit is contained in:
moculus 2009-04-19 20:12:18 +00:00
parent dd276afbbf
commit 41b55bd0f6
2 changed files with 29 additions and 5 deletions

View File

@ -144,10 +144,10 @@ class Exit(Method):
def _callback(self, v):
a = self._old_window.application
if v == 'yes':
if v in ('yes', 'y'):
a.exit()
a.close_mini_buffer()
if v == 'no':
if v in ('no', 'n'):
return
a.open_mini_buffer(self._prompt, self._callback)
a.set_error('Please type "yes" or "no"')

View File

@ -101,16 +101,17 @@ class SaveBufferAs(Method):
args = [arg('path', dt="path", p="Write file: ", ld=True,
dv=default.current_working_dir, h="new path to use")]
def _execute(self, w, **vargs):
a = w.application
curr_buffer = w.buffer
curr_buffer_name = curr_buffer.name()
data = curr_buffer.make_string()
path = os.path.realpath(os.path.expanduser(vargs['path']))
w.set_error("got %r (%d)" % (path, len(data)))
if w.application.has_buffer_name(path):
if a.has_buffer_name(path):
w.set_error("buffer for %r is already open" % path)
return
w.application.new_file_buffer(path, data, switch_to=True)
w.application.methods['kill-buffer'].execute(w, buffername=curr_buffer_name)
a.new_file_buffer(path, data, switch_to=True)
a.methods['kill-buffer'].execute(w, buffername=curr_buffer_name)
w.set_error('Wrote %r' % path)
class SaveBuffer(Method):
'''Save the contents of a buffer'''
@ -126,19 +127,42 @@ class SaveBuffer(Method):
w.buffer.save(force=True)
w.set_error("File had disappeared! Wrote %s" % (w.buffer.path))
except buffer.FileChangedError, e:
self._old_window = w
self._prompt = "File changed on-disk; reload(r), overwrite(o) or diff(d)? "
a.open_mini_buffer(self._prompt, self._callback)
def _callback(self, v):
w = self._old_window
a = w.application
a.close_mini_buffer()
if v in ('reload', 'r'):
a.methods['reload-buffer'].execute(w)
elif v in ('overwrite', 'o'):
a.methods['overwrite-buffer'].execute(w)
elif v in ('diff', 'd'):
path1 = w.buffer.path
tf = tempfile.NamedTemporaryFile(delete=False)
tf.write(w.buffer.make_string())
tf.close()
path2 = tf.name
a.methods['diff'].execute(w, path1=path1, path2=path2)
os.unlink(path2)
w.set_error("File changed on disk; showing differences")
else:
a.open_mini_buffer(self._prompt, self._callback)
a.set_error('Please type reload(r), overwrite(o) or diff(d)')
class ReloadBuffer(Method):
'''Reload the contents of a buffer from the filesystem'''
def _execute(self, w, **vargs):
w.buffer.reload()
w.set_error("Buffer contents reloaded from %r" % w.buffer.path)
class OverwriteBuffer(Method):
'''Unconditionally overwrite path with the contents of the buffer'''
def _execute(self, w, **vargs):
w.buffer.save(force=True)
w.set_error("Wrote %s" % (w.buffer.path))
class ToggleTabs(Method):
'''Toggle whether to write tabs out or not (defaults to false)'''
def _execute(self, w, **vargs):