diff --git a/chrome/content/vimperator/commands.js b/chrome/content/vimperator/commands.js index 871133ea..7b291efb 100644 --- a/chrome/content/vimperator/commands.js +++ b/chrome/content/vimperator/commands.js @@ -1298,6 +1298,77 @@ function Commands() //{{{ short_help: "Switch to the first tab" } )); + addDefaultCommand(new Command(["time"], + function(args, special, count) + { + if (!count || count < 1) + count = 1; + + try + { + if (count > 1) + { + var i = count; + var before_time = Date.now(); + + if (args && args[0] == ":") + while (i--) + vimperator.execute(args); + else + while (i--) + eval(args); + + var after_time = Date.now(); + + if ((after_time - before_time) / count >= 100) + var each = " Each time: " + + ((after_time - before_time) / 1000.0 / count) + + " sec"; + else + var each = " Each time: " + + ((after_time - before_time) / count) + + " msec"; + + if (after_time - before_time >= 100) + var total = " Total time: " + + ((after_time - before_time) / 1000.0) + + " sec"; + else + var total = " Total time: " + + (after_time - before_time) + " msec"; + + + vimperator.echo("
Code execution summary:
" + + " Executed: " + count + " times
" + each + total); + } + else + { + var before_time = Date.now(); + if (args && args[0] == ":") + vimperator.execute(args); + else + eval(args); + var after_time = Date.now(); + + if (after_time - before_time >= 100) + vimperator.echo("Total time: " + ((after_time - before_time) / 1000.0) + " sec"); + else + vimperator.echo("Total time: " + (after_time - before_time) + " msec"); + } + } + catch (e) + { + vimperator.echoerr(e); + } + }, + { + usage: ["{count}time {code|:command}"], + short_help: "Profile a piece of code or a command", + help: "Runs {code} {count} times (default 1) and returns the elapsed time. " + + "{code} is always passed to JavaScript's eval(), which might be slow, so take the results with a grain of salt. " + + "If {code} starts with a :, it is executed as a vimperator command" + } + )); addDefaultCommand(new Command(["u[ndo]"], function(args, special, count) { if (count < 1) count = 1; undoCloseTab(count-1); }, { diff --git a/chrome/content/vimperator/events.js b/chrome/content/vimperator/events.js index a2c72a08..ab533a36 100644 --- a/chrome/content/vimperator/events.js +++ b/chrome/content/vimperator/events.js @@ -509,8 +509,9 @@ function Events() //{{{ default: // clear any selection made var selection = window.content.getSelection(); - if (selection) + try { // a simple if (selection) does not work selection.collapseToStart(); + } catch (e) { } vimperator.commandline.clear(); vimperator.modes.reset(); diff --git a/chrome/content/vimperator/modes.js b/chrome/content/vimperator/modes.js index 544bfe07..10df9b91 100644 --- a/chrome/content/vimperator/modes.js +++ b/chrome/content/vimperator/modes.js @@ -87,8 +87,9 @@ vimperator.modes = (function() { // clear any selection made var selection = window.content.getSelection(); - if (selection) + try { // a simple if (selection) does not work selection.collapseToStart(); + } catch (e) { } } else vimperator.editor.unselectText();