From 2b079345cb5cbb2e7cde7cef8e86a9070d3683a4 Mon Sep 17 00:00:00 2001 From: "Bradley M. Froehle" Date: Thu, 9 Aug 2012 16:48:01 -0700 Subject: [PATCH] notebook: up/down arrow keys move to begin/end of line at top/bottom of cell The behavior is the following: * If you press UP while on the first row, you go to the beginning of the line. * If you press UP again (or were already at the beginning of the line), you go to the previous cell. * If you press DOWN while on the last row, you go to the end of the line. * If you press DOWN again (or were already at the end of the line), you go to the next cell. This applies to `CodeCell`s and `RawCell`s. --- .../html/notebook/static/js/codecell.js | 4 +-- .../html/notebook/static/js/textcell.js | 35 +++++++++++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/IPython/frontend/html/notebook/static/js/codecell.js b/IPython/frontend/html/notebook/static/js/codecell.js index 1347fe12f..d2ed5a417 100644 --- a/IPython/frontend/html/notebook/static/js/codecell.js +++ b/IPython/frontend/html/notebook/static/js/codecell.js @@ -234,7 +234,7 @@ var IPython = (function (IPython) { CodeCell.prototype.at_top = function () { var cursor = this.code_mirror.getCursor(); - if (cursor.line === 0) { + if (cursor.line === 0 && cursor.ch === 0) { return true; } else { return false; @@ -244,7 +244,7 @@ var IPython = (function (IPython) { CodeCell.prototype.at_bottom = function () { var cursor = this.code_mirror.getCursor(); - if (cursor.line === (this.code_mirror.lineCount()-1)) { + if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) { return true; } else { return false; diff --git a/IPython/frontend/html/notebook/static/js/textcell.js b/IPython/frontend/html/notebook/static/js/textcell.js index 234ff47d6..8980461bf 100644 --- a/IPython/frontend/html/notebook/static/js/textcell.js +++ b/IPython/frontend/html/notebook/static/js/textcell.js @@ -12,6 +12,7 @@ var IPython = (function (IPython) { // TextCell base class + var key = IPython.utils.keycodes; var TextCell = function () { this.code_mirror_mode = this.code_mirror_mode || 'htmlmixed'; @@ -269,6 +270,36 @@ var IPython = (function (IPython) { }; + RawCell.prototype.handle_codemirror_keyevent = function (editor, event) { + // This method gets called in CodeMirror's onKeyDown/onKeyPress + // handlers and is used to provide custom key handling. Its return + // value is used to determine if CodeMirror should ignore the event: + // true = ignore, false = don't ignore. + + var that = this; + if (event.which === key.UPARROW && event.type === 'keydown') { + // If we are not at the top, let CM handle the up arrow and + // prevent the global keydown handler from handling it. + if (!that.at_top()) { + event.stop(); + return false; + } else { + return true; + }; + } else if (event.which === key.DOWNARROW && event.type === 'keydown') { + // If we are not at the bottom, let CM handle the down arrow and + // prevent the global keydown handler from handling it. + if (!that.at_bottom()) { + event.stop(); + return false; + } else { + return true; + }; + }; + return false; + }; + + RawCell.prototype.select = function () { IPython.Cell.prototype.select.apply(this); this.code_mirror.refresh(); @@ -278,7 +309,7 @@ var IPython = (function (IPython) { RawCell.prototype.at_top = function () { var cursor = this.code_mirror.getCursor(); - if (cursor.line === 0) { + if (cursor.line === 0 && cursor.ch === 0) { return true; } else { return false; @@ -288,7 +319,7 @@ var IPython = (function (IPython) { RawCell.prototype.at_bottom = function () { var cursor = this.code_mirror.getCursor(); - if (cursor.line === (this.code_mirror.lineCount()-1)) { + if (cursor.line === (this.code_mirror.lineCount()-1) && cursor.ch === this.code_mirror.getLine(cursor.line).length) { return true; } else { return false;