/**
 * Transpose line at caret with previous line, and move caret to next line.
 *  Emulates Emacs "transpose-lines" command (without prefix argument support).
 */

source (MiscUtilities.constructPath(dirname(scriptPath), "EmacsUtil.bsh"));

void emacsTransposeLines()
{
    caret = textArea.getCaretPosition();
    if ((caret == 0) || atEndOfBuffer())
    {
        beep();
        return;
    }

    caretLine = textArea.getCaretLine();
    lineStart = textArea.getLineStartOffset (caretLine);
    lineEnd = textArea.getLineEndOffset (caretLine);
    lineIndex = textArea.getLineOfOffset (caretLine);

    selection = new Selection.Range (lineStart, lineEnd);
    line = textArea.getSelectedText(selection);

    // Get the location of the previous line

    textArea.goToPrevLine (false);
    prevLineCaret = textArea.getCaretLine();
    prevLineStart = textArea.getLineStartOffset (prevLineCaret);

    // Go back to the original location.
    
    textArea.setCaretPosition (caret);
    
    // Delete the line
    
    textArea.deleteLine();
    
    // Insert it in the new location.

    buffer.insert (prevLineStart, line);
}

emacsTransposeLines();

