diff --git a/lib/js/terminal.js b/lib/js/terminal.js index 00407cb83..d336a43bd 100755 --- a/lib/js/terminal.js +++ b/lib/js/terminal.js @@ -175,6 +175,56 @@ var terminal = (function($) { mobileIngoreAutoSpace: [',', '.', ')'], completionEscape: false, wordAutocomplete: false, + keydown: function() { + if (position) { + term.set_position(position); + position = false; + } + }, + keypress: function(e) { + var term = this; + function is_open(token) { + return ['(', '['].indexOf(token) !== -1; + } + function is_close(token) { + return [')', ']'].indexOf(token) !== -1; + } + if (is_close(e.key)) { + setTimeout(function() { + position = term.get_position(); + var command = term.get_command().substring(0, position); + var len = command.split(/\n/)[0].length; + var tokens = lips.tokenize(command, true); + var count = 1; + var token; + var i = tokens.length - 1; + while (count > 0) { + token = tokens[--i]; + if (!token) { + return; + } + if (is_open(token.token)) { + count--; + } else if (is_close(token.token)) { + count++; + } + } + if (is_open(token.token) && count === 0) { + clearTimeout(timer); + setTimeout(function() { + var offset = token.offset; + term.set_position(offset); + timer = setTimeout(function() { + term.set_position(position); + position = false; + }, 200); + }, 0); + } + }, 0); + } else { + position = false; + } + }, doubleTab: function(string, matches, echo_command) { echo_command(); this.echo(matches.map(command => {