00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "katevivisualmode.h"
00021 #include "katesmartrange.h"
00022 #include "katevirange.h"
00023
00024 KateViVisualMode::KateViVisualMode( KateViInputModeManager* viInputModeManager, KateView *view, KateViewInternal *viewInternal )
00025 : KateViNormalMode( viInputModeManager, view, viewInternal )
00026 {
00027 m_start.setPosition( -1, -1 );
00028 m_topRange = doc()->newSmartRange(doc()->documentRange());
00029 static_cast<KateSmartRange*>(m_topRange)->setInternal();
00030 m_topRange->setInsertBehavior(KTextEditor::SmartRange::ExpandLeft | KTextEditor::SmartRange::ExpandRight);
00031
00032 m_view->addInternalHighlight(m_topRange);
00033
00034 m_visualLine = false;
00035
00036 KTextEditor::Range r;
00037 highlightRange = doc()->newSmartRange( r, m_topRange );
00038 attribute = KTextEditor::Attribute::Ptr(new KTextEditor::Attribute());
00039 attribute->setBackground( m_viewInternal->palette().highlight() );
00040 attribute->setForeground( m_viewInternal->palette().highlightedText() );
00041 highlightRange->setInsertBehavior(KTextEditor::SmartRange::DoNotExpand);
00042
00043 initializeCommands();
00044 }
00045
00046 KateViVisualMode::~KateViVisualMode()
00047 {
00048 }
00049
00050 void KateViVisualMode::highlight() const
00051 {
00052
00053 highlightRange->setAttribute(KTextEditor::Attribute::Ptr());
00054 highlightRange->setAttribute(attribute);
00055
00056 KTextEditor::Cursor c1 = m_start;
00057 KTextEditor::Cursor c2 = m_view->cursorPosition();
00058
00059 if ( m_visualLine ) {
00060 c1.setColumn( ( c1 < c2 ) ? 0 : getLine( m_start.line() ).length() );
00061 c2.setColumn( ( c1 < c2 ? getLine().length() : 0 ) );
00062 } else if ( c1 > c2 && c1.column() != 0 ) {
00063 c1.setColumn( c1.column()+1 );
00064 }
00065
00066 highlightRange->setRange( KTextEditor::Range( c1, c2 ) );
00067 }
00068
00069 void KateViVisualMode::goToPos( const KateViRange &r )
00070 {
00071 KTextEditor::Cursor c = m_view->cursorPosition();
00072
00073 if ( r.startLine != -1 && r.startColumn != -1 && c == m_start ) {
00074 m_start.setLine( r.startLine );
00075 m_start.setColumn( r.startColumn );
00076 c.setLine( r.endLine );
00077 c.setColumn( r.endColumn );
00078 } else if ( r.startLine != -1 && r.startColumn != -1 && c < m_start ) {
00079 c.setLine( r.startLine );
00080 c.setColumn( r.startColumn );
00081 } else {
00082 c.setLine( r.endLine );
00083 c.setColumn( r.endColumn );
00084 }
00085
00086 if ( c.line() >= doc()->lines() ) {
00087 c.setLine( doc()->lines()-1 );
00088 }
00089
00090 updateCursor( c );
00091
00092 m_commandRange.startLine = m_start.line();
00093 m_commandRange.startColumn = m_start.column();
00094 m_commandRange.endLine = r.endLine;
00095 m_commandRange.endColumn = r.endColumn;
00096
00097 highlight();
00098 }
00099
00100 void KateViVisualMode::reset()
00101 {
00102
00103 highlightRange->setAttribute(KTextEditor::Attribute::Ptr());
00104
00105 m_awaitingMotionOrTextObject.push_back( 0 );
00106
00107 m_visualLine = false;
00108
00109
00110
00111 if ( m_viInputModeManager->getCurrentViMode() == VisualMode
00112 || m_viInputModeManager->getCurrentViMode() == VisualLineMode ) {
00113 m_viInputModeManager->viEnterNormalMode();
00114 }
00115 }
00116
00117 void KateViVisualMode::init()
00118 {
00119 m_start = m_view->cursorPosition();
00120 highlightRange->setRange( KTextEditor::Range( m_start, m_view->cursorPosition() ) );
00121 highlightRange->setAttribute(attribute);
00122 highlight();
00123
00124 m_awaitingMotionOrTextObject.push_back( 0 );
00125
00126 m_commandRange.startLine = m_commandRange.endLine = m_start.line();
00127 m_commandRange.startColumn = m_commandRange.endColumn = m_start.column();
00128 }
00129
00130
00131 void KateViVisualMode::setVisualLine( bool l )
00132 {
00133 m_visualLine = l;
00134 highlight();
00135 }
00136
00137 void KateViVisualMode::switchStartEnd()
00138 {
00139 KTextEditor::Cursor c = m_start;
00140 m_start = m_view->cursorPosition();
00141
00142 updateCursor( c );
00143
00144 highlight();
00145 }
00146
00147 void KateViVisualMode::initializeCommands()
00148 {
00149 m_commands.clear();
00150 m_motions.clear();
00151 m_commands.push_back( new KateViCommand( this, "J", &KateViNormalMode::commandJoinLines, IS_CHANGE ) );
00152 m_commands.push_back( new KateViCommand( this, "c", &KateViNormalMode::commandChange, IS_CHANGE ) );
00153 m_commands.push_back( new KateViCommand( this, "s", &KateViNormalMode::commandChange, IS_CHANGE ) );
00154 m_commands.push_back( new KateViCommand( this, "C", &KateViNormalMode::commandChangeToEOL, IS_CHANGE ) );
00155 m_commands.push_back( new KateViCommand( this, "d", &KateViNormalMode::commandDelete, IS_CHANGE ) );
00156 m_commands.push_back( new KateViCommand( this, "D", &KateViNormalMode::commandDeleteToEOL, IS_CHANGE ) );
00157 m_commands.push_back( new KateViCommand( this, "x", &KateViNormalMode::commandDeleteChar, IS_CHANGE ) );
00158 m_commands.push_back( new KateViCommand( this, "X", &KateViNormalMode::commandDeleteCharBackward, IS_CHANGE ) );
00159 m_commands.push_back( new KateViCommand( this, "gu", &KateViNormalMode::commandMakeLowercase, IS_CHANGE ) );
00160 m_commands.push_back( new KateViCommand( this, "gU", &KateViNormalMode::commandMakeUppercase, IS_CHANGE ) );
00161 m_commands.push_back( new KateViCommand( this, "y", &KateViNormalMode::commandYank ) );
00162 m_commands.push_back( new KateViCommand( this, "Y", &KateViNormalMode::commandYankToEOL ) );
00163 m_commands.push_back( new KateViCommand( this, "p", &KateViNormalMode::commandPaste, IS_CHANGE ) );
00164 m_commands.push_back( new KateViCommand( this, "P", &KateViNormalMode::commandPasteBefore, IS_CHANGE ) );
00165 m_commands.push_back( new KateViCommand( this, "r.", &KateViNormalMode::commandReplaceCharacter, IS_CHANGE | REGEX_PATTERN ) );
00166 m_commands.push_back( new KateViCommand( this, ":", &KateViNormalMode::commandSwitchToCmdLine ) );
00167 m_commands.push_back( new KateViCommand( this, "/", &KateViNormalMode::commandSearch ) );
00168 m_commands.push_back( new KateViCommand( this, "u", &KateViNormalMode::commandUndo ) );
00169 m_commands.push_back( new KateViCommand( this, "U", &KateViNormalMode::commandRedo ) );
00170 m_commands.push_back( new KateViCommand( this, "m.", &KateViNormalMode::commandSetMark, REGEX_PATTERN ) );
00171 m_commands.push_back( new KateViCommand( this, "n", &KateViNormalMode::commandFindNext ) );
00172 m_commands.push_back( new KateViCommand( this, "N", &KateViNormalMode::commandFindPrev ) );
00173 m_commands.push_back( new KateViCommand( this, ">", &KateViNormalMode::commandIndentLines ) );
00174 m_commands.push_back( new KateViCommand( this, "<", &KateViNormalMode::commandUnindentLines ) );
00175 m_commands.push_back( new KateViCommand( this, "<c-c>", &KateViNormalMode::commandAbort ) );
00176 m_commands.push_back( new KateViCommand( this, "<c-[>", &KateViNormalMode::commandAbort ) );
00177 m_commands.push_back( new KateViCommand( this, "ga", &KateViNormalMode::commandPrintCharacterCode, SHOULD_NOT_RESET ) );
00178 m_commands.push_back( new KateViCommand( this, "v", &KateViNormalMode::commandEnterVisualMode, SHOULD_NOT_RESET ) );
00179 m_commands.push_back( new KateViCommand( this, "V", &KateViNormalMode::commandEnterVisualLineMode, SHOULD_NOT_RESET ) );
00180 m_commands.push_back( new KateViCommand( this, "o", &KateViNormalMode::commandToOtherEnd, SHOULD_NOT_RESET ) );
00181 m_commands.push_back( new KateViCommand( this, "=", &KateViNormalMode::commandAlignLines, SHOULD_NOT_RESET ) );
00182 m_commands.push_back( new KateViCommand( this, "~", &KateViNormalMode::commandChangeCase, IS_CHANGE ) );
00183
00184
00185 m_motions.push_back( new KateViMotion( this, "h", &KateViNormalMode::motionLeft ) );
00186 m_motions.push_back( new KateViMotion( this, "<left>", &KateViNormalMode::motionLeft ) );
00187 m_motions.push_back( new KateViMotion( this, "<backspace>", &KateViNormalMode::motionLeft ) );
00188 m_motions.push_back( new KateViMotion( this, "j", &KateViNormalMode::motionDown ) );
00189 m_motions.push_back( new KateViMotion( this, "<down>", &KateViNormalMode::motionDown ) );
00190 m_motions.push_back( new KateViMotion( this, "k", &KateViNormalMode::motionUp ) );
00191 m_motions.push_back( new KateViMotion( this, "<up>", &KateViNormalMode::motionUp ) );
00192 m_motions.push_back( new KateViMotion( this, "l", &KateViNormalMode::motionRight ) );
00193 m_motions.push_back( new KateViMotion( this, "<right>", &KateViNormalMode::motionRight ) );
00194 m_motions.push_back( new KateViMotion( this, " ", &KateViNormalMode::motionRight ) );
00195 m_motions.push_back( new KateViMotion( this, "$", &KateViNormalMode::motionToEOL ) );
00196 m_motions.push_back( new KateViMotion( this, "<end>", &KateViNormalMode::motionToEOL ) );
00197 m_motions.push_back( new KateViMotion( this, "0", &KateViNormalMode::motionToColumn0 ) );
00198 m_motions.push_back( new KateViMotion( this, "<home>", &KateViNormalMode::motionToColumn0 ) );
00199 m_motions.push_back( new KateViMotion( this, "^", &KateViNormalMode::motionToFirstCharacterOfLine ) );
00200 m_motions.push_back( new KateViMotion( this, "f.", &KateViNormalMode::motionFindChar, REGEX_PATTERN ) );
00201 m_motions.push_back( new KateViMotion( this, "F.", &KateViNormalMode::motionFindCharBackward, REGEX_PATTERN ) );
00202 m_motions.push_back( new KateViMotion( this, "t.", &KateViNormalMode::motionToChar, REGEX_PATTERN ) );
00203 m_motions.push_back( new KateViMotion( this, "T.", &KateViNormalMode::motionToCharBackward, REGEX_PATTERN ) );
00204 m_motions.push_back( new KateViMotion( this, "gg", &KateViNormalMode::motionToLineFirst ) );
00205 m_motions.push_back( new KateViMotion( this, "G", &KateViNormalMode::motionToLineLast ) );
00206 m_motions.push_back( new KateViMotion( this, "w", &KateViNormalMode::motionWordForward ) );
00207 m_motions.push_back( new KateViMotion( this, "W", &KateViNormalMode::motionWORDForward ) );
00208 m_motions.push_back( new KateViMotion( this, "b", &KateViNormalMode::motionWordBackward ) );
00209 m_motions.push_back( new KateViMotion( this, "B", &KateViNormalMode::motionWORDBackward ) );
00210 m_motions.push_back( new KateViMotion( this, "e", &KateViNormalMode::motionToEndOfWord ) );
00211 m_motions.push_back( new KateViMotion( this, "E", &KateViNormalMode::motionToEndOfWORD ) );
00212 m_motions.push_back( new KateViMotion( this, "ge", &KateViNormalMode::motionToEndOfPrevWord ) );
00213 m_motions.push_back( new KateViMotion( this, "gE", &KateViNormalMode::motionToEndOfPrevWORD ) );
00214 m_motions.push_back( new KateViMotion( this, "|", &KateViNormalMode::motionToScreenColumn ) );
00215 m_motions.push_back( new KateViMotion( this, "%", &KateViNormalMode::motionToMatchingItem ) );
00216 m_motions.push_back( new KateViMotion( this, "`.", &KateViNormalMode::motionToMark, REGEX_PATTERN ) );
00217 m_motions.push_back( new KateViMotion( this, "'.", &KateViNormalMode::motionToMarkLine, REGEX_PATTERN ) );
00218 m_motions.push_back( new KateViMotion( this, "[[", &KateViNormalMode::motionToPreviousBraceBlockStart ) );
00219 m_motions.push_back( new KateViMotion( this, "]]", &KateViNormalMode::motionToNextBraceBlockStart ) );
00220 m_motions.push_back( new KateViMotion( this, "[]", &KateViNormalMode::motionToPreviousBraceBlockEnd ) );
00221 m_motions.push_back( new KateViMotion( this, "][", &KateViNormalMode::motionToNextBraceBlockEnd ) );
00222
00223
00224 m_motions.push_back( new KateViMotion( this, "iw", &KateViNormalMode::textObjectInnerWord ) );
00225 m_motions.push_back( new KateViMotion( this, "aw", &KateViNormalMode::textObjectAWord ) );
00226 m_motions.push_back( new KateViMotion( this, "i\"", &KateViNormalMode::textObjectInnerQuoteDouble ) );
00227 m_motions.push_back( new KateViMotion( this, "a\"", &KateViNormalMode::textObjectAQuoteDouble ) );
00228 m_motions.push_back( new KateViMotion( this, "i'", &KateViNormalMode::textObjectInnerQuoteSingle ) );
00229 m_motions.push_back( new KateViMotion( this, "a'", &KateViNormalMode::textObjectAQuoteSingle ) );
00230 m_motions.push_back( new KateViMotion( this, "i[()]", &KateViNormalMode::textObjectInnerParen, REGEX_PATTERN ) );
00231 m_motions.push_back( new KateViMotion( this, "a[()]", &KateViNormalMode::textObjectAParen, REGEX_PATTERN ) );
00232 m_motions.push_back( new KateViMotion( this, "i[\\[\\]]", &KateViNormalMode::textObjectInnerBracket, REGEX_PATTERN ) );
00233 m_motions.push_back( new KateViMotion( this, "a[\\[\\]]", &KateViNormalMode::textObjectABracket, REGEX_PATTERN ) );
00234 m_motions.push_back( new KateViMotion( this, "i,", &KateViNormalMode::textObjectInnerComma ) );
00235 m_motions.push_back( new KateViMotion( this, "a,", &KateViNormalMode::textObjectAComma ) );
00236 }