From 4ff70466bf0fea1fa91c130e3864868459bfbe9e Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Wed, 18 Nov 2015 18:36:58 +0100 Subject: Editor: decrease indent with shift-tab / backspace Backspace is "smart" - behaves normally except on the first non-whitespace character in the current line. --- js/codeq/editor.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/js/codeq/editor.js b/js/codeq/editor.js index d0b365e..27296d5 100644 --- a/js/codeq/editor.js +++ b/js/codeq/editor.js @@ -9,10 +9,26 @@ codeq.makeEditor = function (elt, options) { matchBrackets: true, extraKeys: { // replace tabs with spaces - Tab: function (cm) { + 'Tab': function (cm) { var spaces = Array(cm.getOption("indentUnit") + 1).join(" "); cm.replaceSelection(spaces); - } + }, + // decrease indent if on first non-whitespace character in line + 'Backspace': function (cm) { + var doc = cm.getDoc(), + cursor = doc.getCursor(), + line = doc.getLine(cursor.line), + col = cursor.ch; + if (col > 0 && !/\S/.test(line.substring(0, col)) && + (col === line.length || /\S/.test(line[col]))) { + // cursor on the first non-whitespace character in line + cm.execCommand('indentLess'); + } + else { + cm.execCommand('delCharBefore'); + } + }, + 'Shift-Tab': 'indentLess', } }, options), editor = CodeMirror(elt, allOptions); -- cgit v1.2.1