"Move Line Up", "Move Line Down" macros

Posted by mircho on 2008-01-14 07:29

Two simple macros for moving the line at the cursor up or down the file. This is something available in Notepad++ that I missed in Komodo Edit.

'Move Line Up'

//Move Line Up
komodo.assertMacroVersion(2);
if (komodo.view) { komodo.view.setFocus() };

var ke = komodo.editor;

var startPos = ke.currentPos;

var currentLine = ke.lineFromPosition( startPos );

//we are at the last line
if( currentLine == 0 )
{
    return;
}

ke.lineTranspose();
komodo.doCommand( 'cmd_linePrevious' );

'Move Line Down'

//Move Line Down
komodo.assertMacroVersion(2);
if (komodo.view) { komodo.view.setFocus() };

var ke = komodo.editor;

var startPos = ke.currentPos;

var currentLine = ke.lineFromPosition( startPos );

//we are at the last line
if( currentLine == ( ke.lineCount - 1 ) )
{
    return;
}

komodo.doCommand( 'cmd_lineNext' );
ke.lineTranspose();

pacemkr | Fri, 2010-02-12 12:58

Move Line or Move Selection Up:

// Move Line or Move Selection Up
komodo.assertMacroVersion(2);
if (komodo.view) { komodo.view.setFocus() };

var ke = komodo.editor;

if( ke.lineFromPosition( ke.currentPos ) == 0 )
    return;

if (ke.selText){
   
    // Extend selection to beg of line at front and end of line at back
    var selStartLine     = ke.lineFromPosition(ke.selectionStart);
    var selEndLine       = ke.lineFromPosition(ke.selectionEnd);
    var numLinesSelected = selEndLine - selStartLine;
   
    var selStart = ke.positionFromLine(selStartLine);
    var selEnd   = ke.getLineEndPosition(selEndLine);
   
    ke.setSel(selStart, selEnd);
   
   
    // Copy the selected text and remove it
    var text =  komodo.interpolate('%s');
    komodo.doCommand('cmd_delete'); // This leaves a blank line in place of selection
   
    // Move our selection to a new place
    // First move our blank line up
    ke.lineTranspose();
    komodo.doCommand('cmd_linePrevious')
   
    // Insert our text
    ke.insertText(ke.currentPos, text);
   
   
    // Restore selection        
    var newSelStartLine = ke.lineFromPosition( ke.currentPos );
    var newSelEndLine   = newSelStartLine + numLinesSelected;
   
    var newSelStart = ke.currentPos;
    var newSelEnd   = ke.getLineEndPosition(newSelEndLine);
   
    ke.setSel(newSelStart, newSelEnd);      
   
} else {
   
    ke.lineTranspose();
    komodo.doCommand( 'cmd_linePrevious' );    
}

Move Line or Move Selection Down:

// Move Line or Move Selection Down
komodo.assertMacroVersion(2);
if (komodo.view) { komodo.view.setFocus() };

var ke = komodo.editor;

if( ke.lineFromPosition( ke.currentPos ) == (ke.lineCount - 1) )
    return;

if (ke.selText){
   
    // Extend selection to beg of line at front and end of line at back
    var selStartLine     = ke.lineFromPosition(ke.selectionStart);
    var selEndLine       = ke.lineFromPosition(ke.selectionEnd);
    var numLinesSelected = selEndLine - selStartLine;
   
    var selStart = ke.positionFromLine(selStartLine);
    var selEnd   = ke.getLineEndPosition(selEndLine);
   
    ke.setSel(selStart, selEnd);
   
   
    // Copy the selected text and remove it
    var text =  komodo.interpolate('%s');
    komodo.doCommand('cmd_delete'); // This leaves a blank line in place of selection
   
    // Move our selection to a new place
    // First move our blank line up
    komodo.doCommand('cmd_lineNext')
    ke.lineTranspose();
   
    // Insert our text
    ke.insertText(ke.currentPos, text);
   
   
    // Restore selection        
    var newSelStartLine = ke.lineFromPosition( ke.currentPos );
    var newSelEndLine   = newSelStartLine + numLinesSelected;
   
    var newSelStart = ke.currentPos;
    var newSelEnd   = ke.getLineEndPosition(newSelEndLine);
   
    ke.setSel(newSelStart, newSelEnd);      
   
} else {
   
    komodo.doCommand( 'cmd_lineNext' );
    ke.lineTranspose();
}

jesteree | Tue, 2011-03-22 14:31

Just what I was looking for! Saved me a lot of time getting Komodo to do one of the best functions in Notepad++.

Two things:
1) This is working in Komodo 6. You can change your

komodo.assertMacroVersion(2);

to

komodo.assertMacroVersion(3);

2) I know this isn't truly a "fork", but since this has such great functionality, you may want to create this as an official recipe and fork it from Recipes 577100 & 577101. I'm sure people using those macro's would really enjoy your modifications.

matedon | Thu, 2013-03-14 18:15

OMG! I have spent a whole day to figure out how Komodo macros work. I have started to develop the same functionality with macros. Unfortunately I haven't found a handy guide which teach me how to and why. Nevermind. I have rewrited these macros and extended the functionality.

1.) The previous version loose the cursor position after moving one or more lines up/down
2.) Undo and redo (Ctrl+Z, Ctrl+Y) will step back the whole procedure not every macro commands

Here is the Move Down macro

(i set it to Ctrl + Shift + Down keys like in notepad++):

// Move Line or Move Selection Down
komodo.assertMacroVersion(3);
if (komodo.view) { komodo.view.setFocus() };

var ke = komodo.editor;

if( ke.lineFromPosition( ke.currentPos ) == ( ke.lineCount - 1 ) )
        return;

// Prevent Undo remember macro steps
ke.beginUndoAction();
   
var sel_start_a = ke.selectionStart;
var sel_end_a = ke.selectionEnd;

// Extend selection to beg of line at front and end of line at back
var selStartLine = ke.lineFromPosition(ke.selectionStart);
var selEndLine = ke.lineFromPosition(ke.selectionEnd);
var numLinesSelected = selEndLine - selStartLine;

var selStart = ke.positionFromLine( selStartLine );
var selEnd   = ke.getLineEndPosition( selEndLine );

ke.setSel( selStart, selEnd );

// Determine original selection position offset related to extended
var sel_start_b = ke.selectionStart;
var sel_end_b = ke.selectionEnd;
var offset_start = sel_start_a - sel_start_b;
var offset_end = sel_end_b - sel_end_a;  

// Copy the selected text and remove it
var text =  komodo.interpolate('%s');
komodo.doCommand('cmd_delete'); // This leaves a blank line in place of selection

// Move our selection to a new place
// First move our blank line up
komodo.doCommand('cmd_lineNext')
ke.lineTranspose();

// Insert our text
ke.insertText(ke.currentPos, text);

// Restore selection           
var newSelStartLine = ke.lineFromPosition( ke.currentPos );
var newSelEndLine   = newSelStartLine + numLinesSelected;

var newSelStart = ke.currentPos + offset_start;
var newSelEnd   = ke.getLineEndPosition(newSelEndLine) - offset_end;

ke.setSel(newSelStart, newSelEnd);

// End of prevent Undo
ke.endUndoAction();

Here is the Move Up macro

(i set it to Ctrl + Shift + Up keys like in notepad++):

// Move Line or Move Selection Up
komodo.assertMacroVersion(3);
if (komodo.view) { komodo.view.setFocus() };

var ke = komodo.editor;

if( ke.lineFromPosition( ke.currentPos ) == 0 )
        return;

// Prevent Undo remember macro steps
ke.beginUndoAction();
   
var sel_start_a = ke.selectionStart;
var sel_end_a = ke.selectionEnd;

// Extend selection to beg of line at front and end of line at back
var selStartLine = ke.lineFromPosition(ke.selectionStart);
var selEndLine = ke.lineFromPosition(ke.selectionEnd);
var numLinesSelected = selEndLine - selStartLine;

var selStart = ke.positionFromLine(selStartLine);
var selEnd   = ke.getLineEndPosition(selEndLine);

ke.setSel(selStart, selEnd);

// Determine original selection position offset related to extended
var sel_start_b = ke.selectionStart;
var sel_end_b = ke.selectionEnd;
var offset_start = sel_start_a - sel_start_b;
var offset_end = sel_end_b - sel_end_a;  

// Copy the selected text and remove it
var text =  komodo.interpolate('%s');
komodo.doCommand('cmd_delete'); // This leaves a blank line in place of selection

// Move our selection to a new place
// First move our blank line up
ke.lineTranspose();
komodo.doCommand('cmd_linePrevious')

// Insert our text
ke.insertText(ke.currentPos, text);

// Restore selection
var newSelStartLine = ke.lineFromPosition( ke.currentPos );
var newSelEndLine   = newSelStartLine + numLinesSelected;

var newSelStart = ke.currentPos + offset_start;
var newSelEnd   = ke.getLineEndPosition(newSelEndLine) - offset_end;

ke.setSel(newSelStart, newSelEnd);

// End of prevent Undo
ke.endUndoAction();

These way I have got the best alternative for Notepad++ on linux (Ubuntu).
I only miss the "double click to highlight all the same word" function but I figure out something.

;) Cheers!