Permalink
Browse files

Implemented smart autoindenting.

  • Loading branch information...
1 parent a85c3e7 commit 4fb6f617d858e75897c40cab961cfd998d84eac8 @ellisonbg ellisonbg committed Aug 9, 2011
@@ -83,6 +83,10 @@ span.section_row_buttons > button {
float: right;
}
+#autoindent_span {
+ float: right;
+}
+
.checkbox_label {
font-size: 85%;
float: right;
@@ -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 () {};
@@ -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;
@@ -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<len; i++) {
+ cells[i].set_autoindent(state)
+ };
+ };
+
// Kernel related things
Notebook.prototype.start_kernel = function () {
@@ -167,6 +167,10 @@ var IPython = (function (IPython) {
this.content.find('#run_all_cells').click(function () {
IPython.notebook.execute_all_cells();
});
+ this.content.find('#autoindent').change(function () {
+ var state = $('#autoindent').prop('checked');
+ IPython.notebook.set_autoindent(state);
+ });
};
@@ -119,6 +119,12 @@ <h3 class="section_header">Cell</h3>
</span>
<span class="button_label">Run</span>
</div>
+ <div class="section_row">
+ <span id="autoindent_span">
+ <input type="checkbox" id="autoindent"></input>
+ </span>
+ <span class="checkbox_label">Autoindent:</span>
+ </div>
</div>
</div>

0 comments on commit 4fb6f61

Please sign in to comment.