0797e3cc5020

Oh hell yes, Python 2.5.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Mon, 13 Jun 2011 21:17:54 -0400
parents 83d8eb7f58e8
children 338639048930
branches/tags (none)
files plugin/threesomelib/modes.py plugin/threesomelib/util/windows.py

Changes

--- a/plugin/threesomelib/modes.py	Mon Jun 13 20:58:58 2011 -0400
+++ b/plugin/threesomelib/modes.py	Mon Jun 13 21:17:54 2011 -0400
@@ -1,3 +1,5 @@
+from __future__ import with_statement
+
 import vim
 from util import buffers, windows
 from settings import boolsetting, setting
@@ -11,9 +13,8 @@
 
 
     def diff(self, diffmode):
-        curwindow = windows.currentnr()
-        getattr(self, '_diff_%d' % diffmode)()
-        windows.focus(curwindow)
+        with windows.remain():
+            getattr(self, '_diff_%d' % diffmode)()
 
         # Reset the scrollbind to whatever it was before we diffed.
         if not diffmode:
@@ -27,21 +28,19 @@
 
 
     def diffoff(self):
-        curwindow = windows.currentnr()
-
-        for winnr in range(2, 2 + self._number_of_windows):
-            windows.focus(winnr)
-            curbuffer = buffers.current
+        with windows.remain():
+            for winnr in range(2, 2 + self._number_of_windows):
+                windows.focus(winnr)
+                curbuffer = buffers.current
 
-            for buffer in buffers.all:
-                buffer.open()
-                vim.command('diffoff')
-                if setting('wrap'):
-                    vim.command('setlocal ' + setting('wrap'))
+                for buffer in buffers.all:
+                    buffer.open()
+                    vim.command('diffoff')
+                    if setting('wrap'):
+                        vim.command('setlocal ' + setting('wrap'))
 
-            curbuffer.open()
+                curbuffer.open()
 
-        windows.focus(curwindow)
 
     def key_diffoff(self):
         self.diff(0)
@@ -51,23 +50,19 @@
         if self._current_diff_mode:
             return
 
-        curwindow = windows.currentnr()
-        pos = vim.current.window.cursor
-        self._current_scrollbind = enabled
+        with windows.remain():
+            self._current_scrollbind = enabled
 
-        for winnr in range(2, 2 + self._number_of_windows):
-            windows.focus(winnr)
+            for winnr in range(2, 2 + self._number_of_windows):
+                windows.focus(winnr)
+
+                if enabled:
+                    vim.command('set scrollbind')
+                else:
+                    vim.command('set noscrollbind')
 
             if enabled:
-                vim.command('set scrollbind')
-            else:
-                vim.command('set noscrollbind')
-
-        if enabled:
-            vim.command('syncbind')
-
-        windows.focus(curwindow)
-        vim.current.window.cursor = pos
+                vim.command('syncbind')
 
     def key_scrollbind(self):
         self.scrollbind(not self._current_scrollbind)
@@ -159,19 +154,16 @@
         return lines
 
     def redraw_hud(self):
-        curwindow = windows.currentnr()
-
-        windows.focus(1)
+        with windows.remain():
+            windows.focus(1)
 
-        vim.command('setlocal modifiable')
-        buffers.hud.set_lines(self.hud_lines())
-        vim.command('setlocal nomodifiable')
+            vim.command('setlocal modifiable')
+            buffers.hud.set_lines(self.hud_lines())
+            vim.command('setlocal nomodifiable')
 
-        vim.command('set winfixheight')
-        vim.command('resize ' + setting('hud_size', '3'))
-        vim.command('wincmd =')
-
-        windows.focus(curwindow)
+            vim.command('set winfixheight')
+            vim.command('resize ' + setting('hud_size', '3'))
+            vim.command('wincmd =')
 
 
 class GridMode(Mode):
--- a/plugin/threesomelib/util/windows.py	Mon Jun 13 20:58:58 2011 -0400
+++ b/plugin/threesomelib/util/windows.py	Mon Jun 13 21:17:54 2011 -0400
@@ -21,3 +21,16 @@
 def currentnr():
     return int(vim.eval('winnr()'))
 
+def pos():
+    return vim.current.window.cursor
+
+
+class remain:
+    def __enter__(self):
+        self.curwindow = currentnr()
+        self.pos = pos()
+
+    def __exit__(self, type, value, traceback):
+        focus(self.curwindow)
+        vim.current.window.cursor = self.pos
+