From 4fb6f617d858e75897c40cab961cfd998d84eac8 2011-08-09 21:09:03 From: Brian E. Granger Date: 2011-08-09 21:09:03 Subject: [PATCH] Implemented smart autoindenting. --- diff --git a/IPython/frontend/html/notebook/static/css/notebook.css b/IPython/frontend/html/notebook/static/css/notebook.css index 25d7c45..1a0fcef 100644 --- a/IPython/frontend/html/notebook/static/css/notebook.css +++ b/IPython/frontend/html/notebook/static/css/notebook.css @@ -83,6 +83,10 @@ span.section_row_buttons > button { float: right; } +#autoindent_span { + float: right; +} + .checkbox_label { font-size: 85%; float: right; diff --git a/IPython/frontend/html/notebook/static/js/cell.js b/IPython/frontend/html/notebook/static/js/cell.js index ba11ab8..d48c071 100644 --- a/IPython/frontend/html/notebook/static/js/cell.js +++ b/IPython/frontend/html/notebook/static/js/cell.js @@ -68,6 +68,17 @@ var IPython = (function (IPython) { }; + Cell.prototype.set_autoindent = function (state) { + if (state) { + this.code_mirror.setOption('tabMode', 'indent'); + this.code_mirror.setOption('enterMode', 'indent'); + } else { + this.code_mirror.setOption('tabMode', 'shift'); + this.code_mirror.setOption('enterMode', 'flat'); + } + }; + + // Subclasses must implement create_element. Cell.prototype.create_element = function () {}; diff --git a/IPython/frontend/html/notebook/static/js/codecell.js b/IPython/frontend/html/notebook/static/js/codecell.js index e0588da..6b2c952 100644 --- a/IPython/frontend/html/notebook/static/js/codecell.js +++ b/IPython/frontend/html/notebook/static/js/codecell.js @@ -48,8 +48,7 @@ var IPython = (function (IPython) { if (event.keyCode === 13 && event.shiftKey) { // Always ignore shift-enter in CodeMirror as we handle it. return true; - // } else if (event.keyCode == 32 && (event.ctrlKey || event.metaKey) && !event.altKey) { - } else if (event.keyCode == 9) { + } else if (event.keyCode === 9) { var cur = editor.getCursor(); var pre_cursor = editor.getRange({line:cur.line,ch:0},cur).trim(); if (pre_cursor === "") { @@ -65,6 +64,21 @@ var IPython = (function (IPython) { IPython.notebook.complete_cell(this, line, cur.ch); return true; } + } else if (event.keyCode === 8) { + // If backspace and the line ends with 4 spaces, remove them. + var cur = editor.getCursor(); + var line = editor.getLine(cur.line); + var ending = line.slice(-4); + if (ending === ' ') { + editor.replaceRange('', + {line: cur.line, ch: cur.ch-4}, + {line: cur.line, ch: cur.ch} + ); + event.stop(); + return true; + } else { + return false; + }; } else { if (this.is_completing && this.completion_cursor !== editor.getCursor()) { this.is_completing = false; diff --git a/IPython/frontend/html/notebook/static/js/notebook.js b/IPython/frontend/html/notebook/static/js/notebook.js index 950f55e..27e821d 100644 --- a/IPython/frontend/html/notebook/static/js/notebook.js +++ b/IPython/frontend/html/notebook/static/js/notebook.js @@ -447,6 +447,14 @@ var IPython = (function (IPython) { }; + Notebook.prototype.set_autoindent = function (state) { + var cells = this.cells(); + len = cells.length; + for (var i=0; i Run +
+ + + + Autoindent: +