Kate
kateviglobal.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries and the Kate part. 00002 * 00003 * Copyright (C) 2008-2009 Erlend Hamberg <ehamberg@gmail.com> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Library General Public License 00016 * along with this library; see the file COPYING.LIB. If not, write to 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "kateviglobal.h" 00022 #include "katevikeyparser.h" 00023 00024 #include "kconfiggroup.h" 00025 #include "kdebug.h" 00026 #include <QApplication> 00027 #include <QClipboard> 00028 00029 KateViGlobal::KateViGlobal() 00030 { 00031 m_numberedRegisters = new QList<QString>; 00032 m_registers = new QMap<QChar, QString>; 00033 } 00034 00035 KateViGlobal::~KateViGlobal() 00036 { 00037 delete m_numberedRegisters; 00038 delete m_registers; 00039 } 00040 00041 void KateViGlobal::writeConfig( KConfigGroup &config ) const 00042 { 00043 config.writeEntry( "Normal Mode Mapping Keys", getMappings( NormalMode, true ) ); 00044 QStringList l; 00045 foreach( const QString &s, getMappings( NormalMode ) ) { 00046 l << KateViKeyParser::getInstance()->decodeKeySequence( getMapping( NormalMode, s ) ); 00047 } 00048 config.writeEntry( "Normal Mode Mappings", l ); 00049 } 00050 00051 void KateViGlobal::readConfig( const KConfigGroup &config ) 00052 { 00053 QStringList keys = config.readEntry( "Normal Mode Mapping Keys", QStringList() ); 00054 QStringList mappings = config.readEntry( "Normal Mode Mappings", QStringList() ); 00055 00056 // sanity check 00057 if ( keys.length() == mappings.length() ) { 00058 for ( int i = 0; i < keys.length(); i++ ) { 00059 addMapping( NormalMode, keys.at( i ), mappings.at( i ) ); 00060 kDebug( 13070 ) << "Mapping " << keys.at( i ) << " -> " << mappings.at( i ); 00061 } 00062 } else { 00063 kDebug( 13070 ) << "Error when reading mappings from config: number of keys != number of values"; 00064 } 00065 } 00066 00067 QString KateViGlobal::getRegisterContent( const QChar ® ) const 00068 { 00069 QString regContent; 00070 QChar _reg = ( reg != '"' ? reg : m_defaultRegister ); 00071 00072 if ( _reg >= '1' && _reg <= '9' ) { // numbered register 00073 int index = QString( _reg ).toInt()-1; 00074 if ( m_numberedRegisters->size() > index) { 00075 regContent = m_numberedRegisters->at( index ); 00076 } 00077 } else if ( _reg == '+' ) { // system clipboard register 00078 regContent = QApplication::clipboard()->text( QClipboard::Clipboard ); 00079 } else if ( _reg == '*' ) { // system selection register 00080 regContent = QApplication::clipboard()->text( QClipboard::Selection ); 00081 } else { // regular, named register 00082 if ( m_registers->contains( _reg ) ) { 00083 regContent = m_registers->value( _reg ); 00084 } 00085 } 00086 00087 return regContent; 00088 } 00089 00090 void KateViGlobal::addToNumberedRegister( const QString &text ) 00091 { 00092 if ( m_numberedRegisters->size() == 9 ) { 00093 m_numberedRegisters->removeLast(); 00094 } 00095 00096 // register 0 is used for the last yank command, so insert at position 1 00097 m_numberedRegisters->prepend( text ); 00098 00099 kDebug( 13070 ) << "Register 1-9:"; 00100 for ( int i = 0; i < m_numberedRegisters->size(); i++ ) { 00101 kDebug( 13070 ) << "\t Register " << i+1 << ": " << m_numberedRegisters->at( i ); 00102 } 00103 } 00104 00105 void KateViGlobal::fillRegister( const QChar ®, const QString &text ) 00106 { 00107 // the specified register is the "black hole register", don't do anything 00108 if ( reg == '_' ) { 00109 return; 00110 } 00111 00112 if ( reg >= '1' && reg <= '9' ) { // "kill ring" registers 00113 addToNumberedRegister( text ); 00114 } else if ( reg == '+' ) { // system clipboard register 00115 QApplication::clipboard()->setText( text, QClipboard::Clipboard ); 00116 } else if ( reg == '*' ) { // system selection register 00117 QApplication::clipboard()->setText( text, QClipboard::Selection ); 00118 } else { 00119 m_registers->insert( reg, text ); 00120 } 00121 00122 kDebug( 13070 ) << "Register " << reg << " set to " << getRegisterContent( reg ); 00123 00124 if ( reg == '0' || reg == '1' || reg == '-' ) { 00125 m_defaultRegister = reg; 00126 kDebug( 13070 ) << "Register " << '"' << " set to point to \"" << reg; 00127 } 00128 } 00129 00130 void KateViGlobal::addMapping( ViMode mode, const QString &from, const QString &to ) 00131 { 00132 if ( !from.isEmpty() ) { 00133 switch ( mode ) { 00134 case NormalMode: 00135 m_normalModeMappings[ KateViKeyParser::getInstance()->encodeKeySequence( from ) ] 00136 = KateViKeyParser::getInstance()->encodeKeySequence( to ); 00137 break; 00138 default: 00139 kDebug( 13070 ) << "Mapping not supported for given mode"; 00140 } 00141 } 00142 } 00143 00144 const QString KateViGlobal::getMapping( ViMode mode, const QString &from, bool decode ) const 00145 { 00146 QString ret; 00147 switch ( mode ) { 00148 case NormalMode: 00149 ret = m_normalModeMappings.value( from ); 00150 break; 00151 default: 00152 kDebug( 13070 ) << "Mapping not supported for given mode"; 00153 } 00154 00155 if ( decode ) { 00156 return KateViKeyParser::getInstance()->decodeKeySequence( ret ); 00157 } 00158 return ret; 00159 } 00160 00161 const QStringList KateViGlobal::getMappings( ViMode mode, bool decode ) const 00162 { 00163 QStringList l; 00164 QHash<QString, QString>::const_iterator i; 00165 switch (mode ) { 00166 case NormalMode: 00167 for (i = m_normalModeMappings.constBegin(); i != m_normalModeMappings.constEnd(); ++i) { 00168 if ( decode ) { 00169 l << KateViKeyParser::getInstance()->decodeKeySequence( i.key() ); 00170 } else { 00171 l << i.key(); 00172 } 00173 } 00174 break; 00175 default: 00176 kDebug( 13070 ) << "Mapping not supported for given mode"; 00177 } 00178 00179 return l; 00180 } 00181 00182 void KateViGlobal::clearMappings( ViMode mode ) 00183 { 00184 switch (mode ) { 00185 case NormalMode: 00186 m_normalModeMappings.clear(); 00187 break; 00188 default: 00189 kDebug( 13070 ) << "Mapping not supported for given mode"; 00190 } 00191 }
KDE 4.6 API Reference