/*
Move_Lines_Up.bsh - Beanshell macro to move a selection of lines down by one
line.  This should handle multiple selections, but doesn't work quite right.

Copyright (c) Dale Anson, 2004
 :tabSize=4:indentSize=4:noTabs=false:
 :folding=explicit:collapseFolds=1:

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
   3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

//Localization
final static String NotEditableMessage = jEdit.getProperty("macro.rs.general.ErrorNotEditableDialog.message", "Buffer is not editable");
final static String NoMultipleSelectionError = jEdit.getProperty("macro.rs.MoveLines.NoMultipleSelection.error", "Line move does not work with multiple selection.");

// get the current selection or the current line if no selection
Selection[] selections = textArea.getSelection();

// this doesn't work right with multiple selection, so don't do anything
if (selections.length > 1)
{
	Macros.error( view, NoMultipleSelectionError );
	return;
}

// the approach is to remove the line immediately preceding the selection
// and insert it immediately following the selection
int startLine;
int endLine;
int prevLine;
int insertionPoint;
int insertionLine;

if (selections.length == 0) {
	// nothing is selected, so use the line containing the caret
	startLine = textArea.getCaretLine();
	endLine = startLine;
}
else {
	startLine = textArea.getLineOfOffset(selections[0].getStart());
	endLine = textArea.getLineOfOffset(selections[0].getEnd());
	// limit to lines that are actually selected
	if (textArea.getLineStartOffset(endLine) == selections[0].getEnd()) {
		-- endLine;	
	}
}
if (startLine == 0) {
	// already at top, can't move further up
	return;	
}

// get the text of the line immediately preceding the selection, this is the 
// text to be moved
prevLine = startLine - 1;
String prevLineText = buffer.getLineText(prevLine);

// remove the previous line from the buffer
int removalStart = buffer.getLineStartOffset(prevLine);
int removalLength = buffer.getLineStartOffset(startLine) - buffer.getLineStartOffset(prevLine);
buffer.remove(removalStart, removalLength);

// then insert the previous line immediately after the selection
insertionLine = endLine;
boolean endLineIsLast = endLine == buffer.getLineCount() - 1;
String lineSeparator = buffer.getStringProperty("lineSeparator");
if (endLineIsLast) {
	insertionPoint = buffer.getLength();
	buffer.insertIndented(insertionPoint, lineSeparator + prevLineText);
}
else {
	insertionPoint = textArea.getLineStartOffset(insertionLine);
	buffer.insertIndented(insertionPoint, prevLineText + lineSeparator);
}

// indent the selection	
Mode mode = buffer.getMode();
boolean shouldIndent = false;
String[] indentProps = new String[]{"indentOpenBrackets", "indentOpenBrackets", "unalignedOpenBrackets", "unalignedCloseBrackets", "indentNextLine", "unindentThisLine", "electricKeys", "doubleBracketIndent", "lineUpClosingBracket"} ;
for (String name : indentProps) {
    if (mode.getProperty(name) != null) {
        shouldIndent = true;
        break;
    }
}
if (shouldIndent) {
	buffer.indentLines(startLine - 1, endLine - 1);	
}

// reselect
textArea.selectNone();
selection = new Selection.Range(textArea.getLineStartOffset(prevLine), insertionPoint);
textArea.addToSelection(selection);

