"Down arrow" 将光标移动到行尾 - 如何关闭它
"Down arrow" moves cursor to end of line - how to turn it off
在 IPython Notebook / Jupyter 中,单元格内的箭头 up/down 击键由 CodeMirror 处理(据我所知)。我经常使用这些操作(重新绑定到 control-p / control-n)在单元格之间移动;但在每个单元格的末尾,光标 先移动到行尾 ,然后再跳转到下一个单元格。这是违反直觉的,对我来说,相当分散注意力。
有什么方法可以配置 CodeMirror 使这个向下移动成为向下移动?
谢谢!
移动到下一个单元格的行为由 IPython 包装器代码定义,它可能会检查光标是否位于当前单元格的末尾,并在这种情况下覆盖默认的 CodeMirror 行为。您必须找到该处理程序并以某种方式将其替换为检查光标是否位于最后一行的处理程序。 (我对 IPython 了解不多,只了解 CodeMirror,所以我无法为您指出查找和覆盖相关代码的正确方法。他们可能已经绑定了 Down
键,或者他们可能已经覆盖了 goLineDown
command。)
知道我不是唯一一个想要在从代码单元格的最后一行向下跳转时跳过 "going to end of line" 行为的人,我调查了该行为并发现:
- 它是 CodeMirror,当您在代码单元格的最后一行键入时(文件:
codemirror.js
;"methods" : findPosV
和 moveV
)
- IPython 决定在 CodeMirror 处理后如何处理 "down" 事件(文件:
cell.js
; class: Cell
; 方法: handle_codemirror_keyevent
) ;查看代码,我看到 IPython 在不在最后一行的最后一个字符时忽略事件。
这基本上证实了 Marijin 的回答。
主要目标是跳转到下一个单元格,我认为没有必要阻止 CodeMirror 转到该行的末尾。重点是 强制 IPython 处理事件。
我的解决方案是将代码从 Cell.prototype.handle_codemirror_keyevent
更改为:
Cell.prototype.handle_codemirror_keyevent = function (editor, event) {
var shortcuts = this.keyboard_manager.edit_shortcuts;
var cur = editor.getCursor();
if((cur.line !== 0) && event.keyCode === 38){
// going up, but not from the first line
// don't do anything more with the event
event._ipkmIgnore = true;
}
var nLastLine = editor.lastLine();
if ((event.keyCode === 40) &&
((cur.line !== nLastLine))
) {
// going down, but not from the last line
// don't do anything more with the event
event._ipkmIgnore = true;
}
// if this is an edit_shortcuts shortcut, the global keyboard/shortcut
// manager will handle it
if (shortcuts.handles(event)) {
return true;
}
return false;
};
此代码为 "down-arrow" 键提供了所需的行为(几乎:光标仍然移动到行尾,只是我们看不到它,因为我们已经在另一个单元格中在那一点上),并且还类似地处理 "up-arrow" 键。
要修改 handle_codemirror_keyevent
原型,您有两种可能性:
- 你编辑
cell.js
文件,把原型的代码改成我上面给的代码。该文件位于 <python>/Lib/site-packages/IPython/html/static/notebook/js
或类似的文件中,具体取决于您的发行版
好多了,在页面加载后,您可以动态更改原型:
IPython.Cell.prototype.handle_codemirror_keyevent = function (editor, event) {
<same code as above>
};
例如,您可以在 custom.js
中执行此操作,或者 create an extension 执行此操作(我就是这样做的)。
在 IPython Notebook / Jupyter 中,单元格内的箭头 up/down 击键由 CodeMirror 处理(据我所知)。我经常使用这些操作(重新绑定到 control-p / control-n)在单元格之间移动;但在每个单元格的末尾,光标 先移动到行尾 ,然后再跳转到下一个单元格。这是违反直觉的,对我来说,相当分散注意力。
有什么方法可以配置 CodeMirror 使这个向下移动成为向下移动?
谢谢!
移动到下一个单元格的行为由 IPython 包装器代码定义,它可能会检查光标是否位于当前单元格的末尾,并在这种情况下覆盖默认的 CodeMirror 行为。您必须找到该处理程序并以某种方式将其替换为检查光标是否位于最后一行的处理程序。 (我对 IPython 了解不多,只了解 CodeMirror,所以我无法为您指出查找和覆盖相关代码的正确方法。他们可能已经绑定了 Down
键,或者他们可能已经覆盖了 goLineDown
command。)
知道我不是唯一一个想要在从代码单元格的最后一行向下跳转时跳过 "going to end of line" 行为的人,我调查了该行为并发现:
- 它是 CodeMirror,当您在代码单元格的最后一行键入时(文件:
codemirror.js
;"methods" :findPosV
和moveV
) - IPython 决定在 CodeMirror 处理后如何处理 "down" 事件(文件:
cell.js
; class:Cell
; 方法:handle_codemirror_keyevent
) ;查看代码,我看到 IPython 在不在最后一行的最后一个字符时忽略事件。
这基本上证实了 Marijin 的回答。
主要目标是跳转到下一个单元格,我认为没有必要阻止 CodeMirror 转到该行的末尾。重点是 强制 IPython 处理事件。
我的解决方案是将代码从 Cell.prototype.handle_codemirror_keyevent
更改为:
Cell.prototype.handle_codemirror_keyevent = function (editor, event) {
var shortcuts = this.keyboard_manager.edit_shortcuts;
var cur = editor.getCursor();
if((cur.line !== 0) && event.keyCode === 38){
// going up, but not from the first line
// don't do anything more with the event
event._ipkmIgnore = true;
}
var nLastLine = editor.lastLine();
if ((event.keyCode === 40) &&
((cur.line !== nLastLine))
) {
// going down, but not from the last line
// don't do anything more with the event
event._ipkmIgnore = true;
}
// if this is an edit_shortcuts shortcut, the global keyboard/shortcut
// manager will handle it
if (shortcuts.handles(event)) {
return true;
}
return false;
};
此代码为 "down-arrow" 键提供了所需的行为(几乎:光标仍然移动到行尾,只是我们看不到它,因为我们已经在另一个单元格中在那一点上),并且还类似地处理 "up-arrow" 键。
要修改 handle_codemirror_keyevent
原型,您有两种可能性:
- 你编辑
cell.js
文件,把原型的代码改成我上面给的代码。该文件位于<python>/Lib/site-packages/IPython/html/static/notebook/js
或类似的文件中,具体取决于您的发行版 好多了,在页面加载后,您可以动态更改原型:
IPython.Cell.prototype.handle_codemirror_keyevent = function (editor, event) { <same code as above> };
例如,您可以在
custom.js
中执行此操作,或者 create an extension 执行此操作(我就是这样做的)。