When your cursor is at the end of an indented blank line, and you press the enter key, this macro will catch that keypress and insert an empty new line above the current line. This pushes your indented line down, and you can keep coding as normal. It is quite seamless; the only difference is that you have not left any whitespace in your wake.
This makes it easier to follow the Python PEP-8 style guide, but it's not limited to Python code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | /*
Blank Newlines Macro for Komodo
This macro automatically removes blank new lines when you press enter,
while keeping the indentation level the same.
Copied original structure from http://code.activestate.com/recipes/577790/
Installation:
* Find the toolbox
* Right-click and choose "Add" > "New Macro"
* Give it a name, keep Javascript selected
* Paste this entire file into the text box
* Go to Triggers and set it to trigger on startup
* Execute the macro so you can use it without restarting Komodo
*/
if (typeof(window.extensions) == 'undefined') {
window.extensions = {};
}
if (extensions.blanknewlines && extensions.blanknewlines.onkeypress_handler) {
// Remove the existing trigger handler, we'll re-instate it.
var editor_pane = ko.views.manager.topView;
editor_pane.removeEventListener('keypress', extensions.blanknewlines.onkeypress_handler, true);
}
extensions.blanknewlines = {};
(function() {
var log = ko.logging.getLogger("blanknewlines");
log.setLevel(ko.logging.LOG_DEBUG);
var charCodeEnter = 13;
this.onkeypress_handler = function(e) {
try {
// Abort if it wasn't the Enter key.
if (e.which != charCodeEnter && e.keyCode != charCodeEnter) {
return;
}
// Only trap keypresses that originated in the editor.
if (e.getPreventDefault()) {
// Somebody else already tried to cancel this event.
return;
}
// Create shorthands for 'currentView'
var view = ko.views.manager.currentView;
if (e.originalTarget.localName != "scintilla" &&
e.originalTarget != view.scintilla._embed) {
// The event belongs to something else.
return;
}
if (view.scintilla.key_handler) {
// There is a specialized key handler registered, don't do
// anything, as Komodo's likely in interactive search mode.
return;
}
/**
* @type {Components.interfaces.ISciMoz}
*/
var editor = view.scimoz;
// Abort if there is a selection.
if (editor.selText.length) {
return;
}
// Get the current line number.
var lineNumber = editor.lineFromPosition(editor.currentPos);
// Abort if cursor is not at the end of the line.
if (editor.currentPos != editor.getLineEndPosition(lineNumber)) {
return;
}
// Get the current line's text.
var lineBuffer = new Object();
editor.getLine(lineNumber, lineBuffer);
var lineText = String(lineBuffer.value);
// Abort if the line is not just whitespace.
if (!lineText.match(/^\s+\r?\n?$/)) {
return;
}
if (lineText.length <= 1) {
// The user has pressed the enter key on a blank line with
// no whitespace at all.
// I don't like how Komodo will auto-indent in this case.
// If you do like it, then uncomment the next line.
// return;
// Insert a simple new line, avoiding the auto-indent.
editor.newLine();
} else {
// The user has pressed the enter key on a line that
// contains only whitespace.
// Insert an empty new line before the current line. This will
// make a new blank line appear, and push the the existing
// indented line downwards.
editor.home();
editor.newLine();
editor.lineEnd();
}
e.preventDefault();
e.stopPropagation();
} catch(ex) {
log.exception(ex);
}
}
// Hook up the keypress event listener.
var editor_pane = ko.views.manager.topView;
editor_pane.addEventListener('keypress', this.onkeypress_handler, true);
}).apply(extensions.blanknewlines);
|