/*
Move_Lines_Down.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.");

// check buffer read-only status
if (buffer.isReadOnly())
{
	Macros.error( view, NotEditableMessage );
	return;
}

// 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;
}

int oldCaretPosition = textArea.getCaretPosition();
int lineStart; 
int lineEnd;
if (selections.length == 0)
{
	lineStart = textArea.getCaretLine();
	lineEnd = lineStart;
}
else
{
	lineStart = selections[0].getStartLine();

	lineEnd = selections[0].getEndLine();
}

if (lineStart == 0)
	return;

int end_offset = textArea.getLineEndOffset(lineEnd);

int prevLine = lineStart - 1;
int prevLineStartOffset = buffer.getLineStartOffset(prevLine);
String prevLineText = buffer.getLineText(prevLine);
Log.log(Log.DEBUG, this , "prev=" + prevLineText + " endOFfset=" + end_offset);
int prevLineLength = prevLineText.length();

boolean lastLine = lineEnd == buffer.getLineCount() - 1;

String lineSeparator = buffer.getStringProperty( "lineSeparator" );

if (lastLine)
	buffer.insert(end_offset - 1, lineSeparator + prevLineText);
else

	buffer.insert(end_offset, prevLineText + lineSeparator);

buffer.remove(prevLineStartOffset, prevLineLength + 1);

if (selections.length == 0)
{
	textArea.setCaretPosition(oldCaretPosition - prevLineLength - 1);
}

Mode mode = buffer.getMode();
String[] indentProps = new String[]{"indentOpenBrackets", "indentOpenBrackets",
	"unalignedOpenBrackets", "unalignedCloseBrackets", "indentNextLine", 
	"unindentThisLine", "electricKeys", "doubleBracketIndent", 
	"lineUpClosingBracket"};
for (String name : indentProps) 
{
	if (mode.getProperty(name) != null)
	{
		buffer.indentLines(lineStart - 1, lineEnd - 1);
		break;
	}
}



