Skip to content
Browse files

Merge pull request #115 from misfo/ctrl-u-and-ctrl-d

Scroll by lines commands: `ctrl+u` and `ctrl+d`
  • Loading branch information...
2 parents 0388648 + 211ebad commit fa30a9dd5c8a7247362e747b0a70165a37d6db9b @sublimehq sublimehq committed Apr 5, 2012
Showing with 41 additions and 0 deletions.
  1. +10 −0 Default.sublime-keymap
  2. +31 −0 vintage.py
View
10 Default.sublime-keymap
@@ -685,6 +685,16 @@
"context": [{"key": "setting.command_mode"}, {"key": "setting.vintage_ctrl_keys"}]
},
+ { "keys": ["ctrl+u"], "command": "vi_scroll_lines",
+ "args": {"forward": false},
+ "context": [{"key": "setting.command_mode"}, {"key": "setting.vintage_ctrl_keys"}]
+ },
+
+ { "keys": ["ctrl+d"], "command": "vi_scroll_lines",
+ "args": {"forward": true},
+ "context": [{"key": "setting.command_mode"}, {"key": "setting.vintage_ctrl_keys"}]
+ },
+
{ "keys": ["H"], "command": "set_motion", "args": {
"motion": "move_caret_to_screen_top",
"motion_args": {"repeat": 1},
View
31 vintage.py
@@ -975,6 +975,37 @@ def run(self, edit):
self.view.set_viewport_position((self.view.viewport_position()[0], 0.0))
self.view.show(self.view.sel()[0], False)
+class ViScrollLines(ViPrefixableCommand):
+ def run(self, edit, forward = True, repeat = None):
+ if repeat:
+ line_delta = repeat * (1 if forward else -1)
+ else:
+ viewport_height = self.view.viewport_extent()[1]
+ lines_per_page = viewport_height / self.view.line_height()
+ line_delta = int(round(lines_per_page / (2 if forward else -2)))
+ visual_mode = self.view.has_non_empty_selection_region()
+
+ y_deltas = []
+ def transform(pt):
+ row = self.view.rowcol(pt)[0]
+ new_pt = self.view.text_point(row + line_delta, 0)
+ y_deltas.append(self.view.text_to_layout(new_pt)[1]
+ - self.view.text_to_layout(pt)[1])
+ return new_pt
+
+ transform_selection(self.view, transform, extend = visual_mode)
+
+ self.view.run_command('vi_move_to_first_non_white_space_character',
+ {'extend': visual_mode})
+
+ # Vim scrolls the viewport as far as it moves the cursor. With multiple
+ # selections the cursors could have moved different distances, due to
+ # word wrapping. Move the viewport by the average of those distances.
+ avg_y_delta = sum(y_deltas) / len(y_deltas)
+ vp = self.view.viewport_position()
+ self.view.set_viewport_position((vp[0], vp[1] + avg_y_delta))
+
+
class ViIndent(sublime_plugin.TextCommand):
def run(self, edit):
self.view.run_command('indent')

0 comments on commit fa30a9d

Please sign in to comment.
Something went wrong with that request. Please try again.