void AStylePart::setCursorPos( KParts::Part *part, uint line, uint col ) { if (!part || !part->inherits("KTextEditor::Document")) return; KTextEditor::ViewCursorInterface *iface = dynamic_cast<KTextEditor::ViewCursorInterface*>(part->widget()); if (iface) { iface->setCursorPositionReal( line, col ); } }
/** * Moves the cursor to a given position. * @param nLine The cursor's new line number * @param nCol The cursor's new column number * @return true if successful, false otherwise (cursor interface was not * obtained) */ bool EditorPage::setCursorPos(uint nLine, uint nCol) { Kate::View* pKateView; KTextEditor::ViewCursorInterface* pCursorIf; // Cannot accept line 0 if (nLine == 0) return false; // Adjust to 0-based counting nLine--; nCol--; // Acquire the view cursor pCursorIf = dynamic_cast<KTextEditor::ViewCursorInterface*>(m_pView); if (pCursorIf == NULL) return false; // NOTE: The following code is a fix to a bug in Kate, which wrongly // calculates the column number in setCursorPosition. pKateView = dynamic_cast<Kate::View*>(m_pView); if (pKateView != NULL) { KTextEditor::EditInterface* pEditIf; const char* szLine; uint nRealCol; uint nTabAdjust; // Get a pointer to the edit interface pEditIf = dynamic_cast<KTextEditor::EditInterface*>(m_pDoc); if (!pEditIf) return false; nRealCol = 0; // Check for out of bound line numbers if (nLine < pEditIf->numLines()) { // Get the contents of the requested line szLine = pEditIf->textLine(nLine).latin1(); // Check for empty line if (szLine != NULL) { // The number of columns which a tab character adds nTabAdjust = pKateView->tabWidth() - 1; // Calculate the real column, based on the tab width for (; nRealCol < nCol && szLine[nRealCol] != 0; nRealCol++) { if (szLine[nRealCol] == '\t') nCol -= nTabAdjust; } } } else { // Marker set beyond end of file, move to the last line nLine = pEditIf->numLines() - 1; } // Set the cursor position pCursorIf->setCursorPositionReal(nLine, nRealCol); } else { // Non-Kate editors: set the cursor position normally pCursorIf->setCursorPosition(nLine, nCol); } return true; }