From 41b55bd0f63637b2ead54db306e1186b446dfb6d Mon Sep 17 00:00:00 2001 From: moculus Date: Sun, 19 Apr 2009 20:12:18 +0000 Subject: [PATCH] actually nice support for reloading files --HG-- branch : pmacs2 --- method/__init__.py | 4 ++-- method/buffers.py | 30 +++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/method/__init__.py b/method/__init__.py index dfef0d0..85a32d7 100644 --- a/method/__init__.py +++ b/method/__init__.py @@ -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"') diff --git a/method/buffers.py b/method/buffers.py index 8217750..1c63e48 100644 --- a/method/buffers.py +++ b/method/buffers.py @@ -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):