/* TextEditor::findNext * Finds the next occurrence of the [find] after the caret position, * selects it and scrolls to it if needed. Returns false if the * [find] was invalid or no match was found, true otherwise *******************************************************************/ bool TextEditor::findNext(string find) { // Check search string if (find.IsEmpty()) return false; // Setup target range SetTargetEnd(GetTextLength()); SetTargetStart(GetSelectionEnd()); // Search within current target range if (SearchInTarget(find) < 0) { // None found, search again from start SetTargetStart(0); SetTargetEnd(GetTextLength()); if (SearchInTarget(find) < 0) { // No matches found in entire text return false; } } // Select matched text SetSelection(GetTargetStart(), GetTargetEnd()); // Scroll to selection EnsureCaretVisible(); return true; }
/* TextEditor::replaceAll * Replaces all occurrences of [find] in the text with [replace]. * Returns the number of occurrences replaced *******************************************************************/ int TextEditor::replaceAll(string find, string replace) { // Check search string if (find.IsEmpty()) return false; // Init search target to entire text SetTargetStart(0); SetTargetEnd(GetTextLength()); // Loop of death int replaced = 0; while (1) { if (SearchInTarget(find) < 0) break; // No matches, finished else { // Replace text & increment counter ReplaceTarget(replace); replaced++; // Continue search from end of replaced text to end of text SetTargetStart(GetTargetEnd()); SetTargetEnd(GetTextLength()); } } // Return number of instances replaced return replaced; }
/* TextEditor::onUpdateUI * Called when anything is modified in the text editor (cursor * position, styling, text, etc) *******************************************************************/ void TextEditor::onUpdateUI(wxStyledTextEvent& e) { // Check for brace match if (txed_brace_match) checkBraceMatch(); // If a calltip is open, update it if (call_tip->IsShown()) updateCalltip(); // Do word matching if appropriate if (txed_match_cursor_word && language) { int word_start = WordStartPosition(GetCurrentPos(), true); int word_end = WordEndPosition(GetCurrentPos(), true); string current_word = GetTextRange(word_start, word_end); if (!current_word.IsEmpty() && HasFocus()) { if (current_word != prev_word_match) { prev_word_match = current_word; SetIndicatorCurrent(8); IndicatorClearRange(0, GetTextLength()); SetTargetStart(0); SetTargetEnd(GetTextLength()); SetSearchFlags(0); while (SearchInTarget(current_word) != -1) { IndicatorFillRange(GetTargetStart(), GetTargetEnd() - GetTargetStart()); SetTargetStart(GetTargetEnd()); SetTargetEnd(GetTextLength()); } } } else { SetIndicatorCurrent(8); IndicatorClearRange(0, GetTextLength()); prev_word_match = ""; } } // Hilight current line MarkerDeleteAll(1); MarkerDeleteAll(2); if (txed_hilight_current_line > 0 && HasFocus()) { int line = LineFromPosition(GetCurrentPos()); MarkerAdd(line, 1); if (txed_hilight_current_line > 1) MarkerAdd(line, 2); } e.Skip(); }
void Edit::OnFindReplaceDialog(wxFindDialogEvent& event) { const wxEventType type = event.GetEventType(); const wxString find_string = m_FindData.GetFindString(); int found_start, found_end; if (type == wxEVT_FIND || type == wxEVT_FIND_NEXT) { // search if (FindText(found_start, found_end, type == wxEVT_FIND_NEXT)) { SetSelection(found_start, found_end); } else { wxMessageDialog dialog(this, wxT("Cannot find the text \"" + find_string + "\" from current position"), wxT("Find")); dialog.ShowModal(); } } else if (type == wxEVT_FIND_REPLACE) { ReplaceText(find_string); } else if (type == wxEVT_FIND_REPLACE_ALL) { const wxString replace_string = m_FindData.GetReplaceString(); int start_index = 0; bool found = true; int found_count = 0; do { // set search area SetTargetStart(start_index); SetTargetEnd(GetLastPosition()); // search and replace found_start = SearchInTarget(find_string); if (found_start > -1) { found_end = found_start + find_string.size(); SetTargetStart(found_start); SetTargetEnd(found_end); ReplaceTarget(replace_string); start_index = found_start + replace_string.size(); found_count++; } else { found = false; } } while (found); const wxString message = wxString::Format(wxT("%d occurrence(s) of \"%s\" were replaced with \"%s\"."), found_count, find_string, replace_string); wxMessageDialog replace_dialog(this, message, wxT("Replaced Text")); replace_dialog.ShowModal(); } }
/* TextEditor::replaceCurrent * Replaces the currently selected occurrence of [find] with * [replace], then selects and scrolls to the next occurrence of * [find] in the text. Returns false if [find] is invalid or the * current selection does not match it, true otherwise *******************************************************************/ bool TextEditor::replaceCurrent(string find, string replace) { // Check search string if (find.IsEmpty()) return false; // Check that we've done a find previously // (by searching for the find string within the current selection) if (GetSelectedText().Length() != find.Length()) return false; SetTargetStart(GetSelectionStart()); SetTargetEnd(GetSelectionEnd()); if (SearchInTarget(find) < 0) return false; // Do the replace ReplaceTarget(replace); // Update selection SetSelection(GetTargetStart(), GetTargetEnd()); // Do find next findNext(find); return true; }
bool wxSTEditorShell::SetMaxLines(int max_lines) { m_max_lines = max_lines; if (m_max_lines < 0) return false; int total_lines = GetLineCount(); total_lines = wxMax(0, total_lines-1); // delete lines when more than m_max_lines, you'll eventually crash otherwise if (total_lines > m_max_lines) { BeginWriteable(); int marker = MarkerGet(total_lines - m_max_lines); SetTargetStart(0); SetTargetEnd(PositionFromLine(total_lines - m_max_lines)); ReplaceTarget(wxEmptyString); // wipe marker that has moved up if there shouldn't be a marker if ((marker & (1<<markerPrompt)) == 0) MarkerDelete(0, markerPrompt); EndWriteable(); return true; } return false; }
void CodeEdit::CommentSelection(const wxString& comment) { if (GetSelectionStart() < GetSelectionEnd()) { int startLine = LineFromPosition(GetSelectionStart()); int endLine = LineFromPosition(GetSelectionEnd()); // Find the minimum indentation level for all of the selected lines. int minIndentation = INT_MAX; for (int i = startLine; i <= endLine; ++i) { wxString lineText = GetLine(i); int firstNonWhitespace = GetFirstNonWhitespace(lineText); if (firstNonWhitespace != -1) { minIndentation = wxMin(firstNonWhitespace, minIndentation); } } // Insert the comment string on each non-blank line. wxString result; for (int i = startLine; i <= endLine; ++i) { wxString lineText = GetLine(i); if (GetFirstNonWhitespace(lineText) != -1) { lineText.insert(minIndentation, comment); } result += lineText; } SetTargetStart(PositionFromLine(startLine)); SetTargetEnd(PositionFromLine(endLine + 1)); ReplaceTarget(result); // Select the replaced text. SetSelectionStart(GetTargetStart()); SetSelectionEnd(GetTargetEnd() - 1); } }
void FB_STC::ReplaceText(int from, int to, const wxString& text) { if (from == to) { InsertText(to, text); return; } SetTargetStart(from); SetTargetEnd(to); ReplaceTarget(text); }
void wxSTEditorShell::SetPromptText(const wxString& text) { BeginWriteable(); int length = GetLength(); wxString promptText = GetPromptText(); SetTargetStart(length - promptText.Length()); SetTargetEnd(length); ReplaceTarget(text); GotoPos(GetLength()); EndWriteable(); }
void wxSTEditorShell::SetPromptText(const wxString& text) { BeginWriteable(); int length = GetLength(); int prompt_line = GetPromptLine(); int start_pos = PositionFromLine(prompt_line); SetTargetStart(start_pos); SetTargetEnd(length); ReplaceTarget(text); GotoPos(GetLength()); EndWriteable(); }
void ScintillaWrapper::replace(boost::python::object searchStr, boost::python::object replaceStr, boost::python::object flags) { int start = 0; int end = GetLength(); int iFlags = 0; if (!flags.is_none()) { iFlags |= boost::python::extract<int>(flags); } const char *replaceChars = boost::python::extract<const char*>(replaceStr.attr("__str__")()); size_t replaceLength = strlen(replaceChars); Sci_TextToFind src; src.lpstrText = const_cast<char*>((const char *)boost::python::extract<const char *>(searchStr.attr("__str__")())); BeginUndoAction(); int result = 0; while(result != -1) { src.chrg.cpMin = start; src.chrg.cpMax = end; result = callScintilla(SCI_FINDTEXT, iFlags, reinterpret_cast<LPARAM>(&src)); // If nothing found, then just finish if (-1 == result) { return; } else { // Replace the location found with the replacement text SetTargetStart(src.chrgText.cpMin); SetTargetEnd(src.chrgText.cpMax); callScintilla(SCI_REPLACETARGET, replaceLength, reinterpret_cast<LPARAM>(replaceChars)); start = src.chrgText.cpMin + (int)replaceLength; end = end + ((int)replaceLength - (src.chrgText.cpMax - src.chrgText.cpMin)); } } EndUndoAction(); }
void CodeEdit::UncommentSelection(const wxString& commentString) { if (GetSelectionStart() < GetSelectionEnd()) { int startLine = LineFromPosition(GetSelectionStart()); int endLine = LineFromPosition(GetSelectionEnd()); wxString result; for (int i = startLine; i <= endLine; ++i) { wxString lineText = GetLine(i); unsigned int c = GetFirstNonWhitespace(lineText); if (c != -1 && lineText.compare(c, commentString.Length(), commentString) == 0) { lineText.erase(c, commentString.Length()); } result += lineText; } SetTargetStart(PositionFromLine(startLine)); SetTargetEnd(PositionFromLine(endLine + 1)); ReplaceTarget(result); // Select the replaced text. SetSelectionStart(GetTargetStart()); SetSelectionEnd(GetTargetEnd() - 1); } }
bool ctlSQLBox::BlockComment(bool uncomment) { wxString lineEnd; switch (GetEOLMode()) { case wxSTC_EOL_LF: lineEnd = wxT("\n"); break; case wxSTC_EOL_CRLF: lineEnd = wxT("\r\n"); break; case wxSTC_EOL_CR: lineEnd = wxT("\r"); break; } // Save the start position const wxString comment = wxT("-- "); int start = GetSelectionStart(); if (!GetSelectedText().IsEmpty()) { wxString selection = GetSelectedText(); if (!uncomment) { selection.Replace(lineEnd, lineEnd + comment); selection.Prepend(comment); if (selection.EndsWith(comment)) selection = selection.Left(selection.Length() - comment.Length()); } else { selection.Replace(lineEnd + comment, lineEnd); if (selection.StartsWith(comment)) selection = selection.Right(selection.Length() - comment.Length()); } ReplaceSelection(selection); SetSelection(start, start + selection.Length()); } else { // No text selection - (un)comment the current line int column = GetColumn(start); int curLineNum = GetCurrentLine(); int pos = PositionFromLine(curLineNum); if (!uncomment) { InsertText(pos, comment); } else { wxString t = GetTextRange(pos, pos + comment.Length()); if (t == comment) { // The line starts with a comment, so we remove it SetTargetStart(pos); SetTargetEnd(pos + comment.Length()); ReplaceTarget(wxT("")); } else { // The line doesn't start with a comment, do nothing return false; } } if (GetLineCount() > curLineNum) { wxString nextLine = GetLine(curLineNum + 1); if (nextLine.EndsWith(lineEnd)) nextLine = nextLine.Left(nextLine.Length() - lineEnd.Length()); int nextColumn = (nextLine.Length() < (unsigned int)column ? nextLine.Length() : column); GotoPos(PositionFromLine(curLineNum + 1) + nextColumn); } } return true; }
void ScintillaWrapper::setTarget(int start, int end) { SetTargetStart(start); SetTargetEnd(end); }
void OutputCtrl::AppendText( const wxString& text ) { SetReadOnly( false ); bool scrollEnd = false; int pos = GetLength(); if ( GetCurrentPos() == pos && GetAnchor() == pos ) scrollEnd = true; // For each line... const wxChar* ptr = text.c_str(); const wxChar* const end = ptr + text.Length(); const wxChar* next; wxString line; bool isError = false; bool isWarn = false; //wxTextAttr errorStyle( *wxRED, *wxWHITE ); //wxTextAttr warnStyle( *wxRED, *wxWHITE ); //wxColour( 255, 128, 0 ), *wxWHITE ); long lnumb; while( ptr != end ) { wxASSERT( ptr < end ); next = std::find( ptr, end, '\n' ); if ( next != end ) ++next; line.assign( ptr, next ); ptr = next; // Look for error lines and highlight them... // // TODO: I need to optimize the regex here... maybe i // shouldn't use a regex, but use my own logic to spot // errors... could be much faster. // if ( m_ErrorExpr.Matches( line ) && m_ErrorExpr.GetMatchCount() > 3 ) isError = true; else if ( m_WarnExpr.Matches( line ) && m_ErrorExpr.GetMatchCount() > 3 ) isWarn = true; pos = GetLength(); SetTargetStart( pos ); SetTargetEnd( pos ); ReplaceTarget( line ); if ( isError ) { StartStyling( pos, 0xFF ); SetStyling( line.Len(), 2 ); // TODO: SetStyle will screw with the current scroll position. The // trick is to disable ECO_AUTOVSCROLL and ECO_AUTOHSCROLL before // changing the selection to change the style. We need to submit this // fix back to wxWindows. //::SendMessage( GetHwnd(), EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL | ECO_AUTOHSCROLL ); //SetStyle( start, last, errorStyle ); //::SendMessage( GetHwnd(), EM_SETOPTIONS, ECOOP_OR, ECO_AUTOVSCROLL | ECO_AUTOHSCROLL ); // Add the error to the debugger state. m_ErrorExpr.GetMatch( line, 2 ).ToLong( &lnumb ); ScriptError* error = new ScriptError; error->file = m_ErrorExpr.GetMatch( line, 1 ); error->line = lnumb; error->start = pos; error->end = pos + line.Len(); error->row = LineFromPosition( pos ); error->error = m_ErrorExpr.GetMatch( line, 3 ); error->error.Trim(); error->warning = false; AddError( error ); isError = false; } else if ( isWarn ) { StartStyling( pos, 0xFF ); SetStyling( line.Len(), 2 ); // TODO: SetStyle will screw with the current scroll position. The // trick is to disable ECO_AUTOVSCROLL and ECO_AUTOHSCROLL before // changing the selection to change the style. We need to submit this // fix back to wxWindows. //::SendMessage( GetHwnd(), EM_SETOPTIONS, ECOOP_XOR, ECO_AUTOVSCROLL | ECO_AUTOHSCROLL ); //SetStyle( start, last, warnStyle ); //::SendMessage( GetHwnd(), EM_SETOPTIONS, ECOOP_OR, ECO_AUTOVSCROLL | ECO_AUTOHSCROLL ); // Add the error to the debugger state. m_WarnExpr.GetMatch( line, 2 ).ToLong( &lnumb ); ScriptError* error = new ScriptError; error->file = m_WarnExpr.GetMatch( line, 1 ); error->line = lnumb; error->start = pos; error->end = pos + line.Len(); error->row = LineFromPosition( pos ); error->error = m_WarnExpr.GetMatch( line, 3 ); error->error.Trim(); error->warning = true; AddError( error ); isWarn = false; } } if ( scrollEnd ) { const int endPos = GetLength(); SetAnchor( endPos ); SetCurrentPos( endPos ); ShowLine( LineFromPosition( endPos ) ); } EmptyUndoBuffer(); SetReadOnly( true ); }