diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Editor.cxx | 63 | ||||
| -rw-r--r-- | src/Editor.h | 3 | 
2 files changed, 66 insertions, 0 deletions
diff --git a/src/Editor.cxx b/src/Editor.cxx index ae2d670ce..2fa3a4340 100644 --- a/src/Editor.cxx +++ b/src/Editor.cxx @@ -1027,6 +1027,59 @@ void Editor::VerticalCentreCaret() {  	}  } +void Editor::MoveSelectedLines(int lineDelta) { + +	// if selection doesn't start at the beginning of the line, set the new start +	int selectionStart = SelectionStart().Position(); +	int startLine = pdoc->LineFromPosition(selectionStart); +	int beginningOfStartLine = pdoc->LineStart(startLine); +	selectionStart = beginningOfStartLine; + +	// if selection doesn't end at the beginning of a line greater than that of the start, +	// then set it at the beginning of the next one +	int selectionEnd = SelectionEnd().Position(); +	int endLine = pdoc->LineFromPosition(selectionEnd); +	int beginningOfEndLine = pdoc->LineStart(endLine); +	if (selectionEnd > beginningOfEndLine +		|| selectionStart == selectionEnd) { +		selectionEnd = pdoc->LineStart(endLine + 1); +	} + +	// if there's nowhere for the selection to move +	// (i.e. at the beginning going up or at the end going down), +	// stop it right there! +	if ((selectionStart == 0 && lineDelta < 0) +		|| (selectionEnd == pdoc->Length() && lineDelta > 0) +	        || selectionStart == selectionEnd) { +		return; +	} + +	UndoGroup ug(pdoc); + +	SetSelection(selectionStart, selectionEnd); + +	SelectionText selectedText; +	CopySelectionRange(&selectedText); + +	int selectionLength = SelectionRange(selectionStart, selectionEnd).Length(); +	ClearSelection(); + +	Point currentLocation = LocationFromPosition(CurrentPosition()); +	int currentLine = LineFromLocation(currentLocation); +	GoToLine(currentLine + lineDelta); + +	pdoc->InsertCString(CurrentPosition(), selectedText.s); +	SetSelection(CurrentPosition(), CurrentPosition() + selectionLength); +} + +void Editor::MoveSelectedLinesUp() { +	MoveSelectedLines(-1); +} + +void Editor::MoveSelectedLinesDown() { +	MoveSelectedLines(1); +} +  void Editor::MoveCaretInsideView(bool ensureVisible) {  	PRectangle rcClient = GetTextRectangle();  	Point pt = PointMainCaret(); @@ -4688,6 +4741,8 @@ void Editor::NotifyMacroRecord(unsigned int iMessage, uptr_t wParam, sptr_t lPar  	case SCI_SELECTIONDUPLICATE:  	case SCI_COPYALLOWLINE:  	case SCI_VERTICALCENTRECARET: +	case SCI_MOVESELECTEDLINESUP: +	case SCI_MOVESELECTEDLINESDOWN:  		break;  		// Filter out all others like display changes. Also, newlines are redundant @@ -6957,6 +7012,14 @@ sptr_t Editor::WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) {  		VerticalCentreCaret();  		break; +	case SCI_MOVESELECTEDLINESUP: +		MoveSelectedLinesUp(); +		break; + +	case SCI_MOVESELECTEDLINESDOWN: +		MoveSelectedLinesDown(); +		break; +  	case SCI_COPYRANGE:  		CopyRangeToClipboard(wParam, lParam);  		break; diff --git a/src/Editor.h b/src/Editor.h index ef3fb78f0..5c01ee83d 100644 --- a/src/Editor.h +++ b/src/Editor.h @@ -335,6 +335,9 @@ protected:	// ScintillaBase subclass needs access to much of Editor  	virtual void ScrollText(int linesToMove);  	void HorizontalScrollTo(int xPos);  	void VerticalCentreCaret(); +	void MoveSelectedLines(int lineDelta); +	void MoveSelectedLinesUp(); +	void MoveSelectedLinesDown();  	void MoveCaretInsideView(bool ensureVisible=true);  	int DisplayFromPosition(int pos);  | 
