Kate
katecmd.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) 2001-2010 Christoph Cullmann <cullmann@kde.org> 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 "katecmd.h" 00022 #include "kateglobal.h" 00023 00024 #include <kdebug.h> 00025 00026 //BEGIN KateCmd 00027 #define CMD_HIST_LENGTH 256 00028 00029 KateCmd::KateCmd () 00030 { 00031 m_cmdCompletion.addItem("help"); 00032 } 00033 00034 KateCmd::~KateCmd () 00035 { 00036 } 00037 00038 bool KateCmd::registerCommand (KTextEditor::Command *cmd) 00039 { 00040 QStringList l = cmd->cmds (); 00041 00042 for (int z=0; z<l.count(); z++) 00043 if (m_dict.contains(l[z])) { 00044 kDebug(13050)<<"Command already registered: "<<l[z]<<". Aborting."; 00045 return false; 00046 } 00047 00048 for (int z=0; z<l.count(); z++) { 00049 m_dict.insert (l[z], cmd); 00050 //kDebug(13050)<<"Inserted command:"<<l[z]; 00051 } 00052 00053 m_cmds += l; 00054 m_cmdCompletion.insertItems(l); 00055 00056 return true; 00057 } 00058 00059 bool KateCmd::unregisterCommand (KTextEditor::Command *cmd) 00060 { 00061 QStringList l; 00062 00063 QHash<QString, KTextEditor::Command*>::const_iterator i = m_dict.constBegin(); 00064 while (i != m_dict.constEnd()) { 00065 if (i.value()==cmd) l << i.key(); 00066 ++i; 00067 } 00068 00069 for ( QStringList::Iterator it1 = l.begin(); it1 != l.end(); ++it1 ) { 00070 m_dict.remove(*it1); 00071 m_cmdCompletion.removeItem(*it1); 00072 //kDebug(13050)<<"Removed command:"<<*it1; 00073 } 00074 00075 return true; 00076 } 00077 00078 KTextEditor::Command *KateCmd::queryCommand (const QString &cmd) const 00079 { 00080 // a command can be named ".*[\w\-]+" with the constrain that it must 00081 // contain at least one letter. 00082 int f = 0; 00083 bool b = false; 00084 for ( ; f < cmd.length(); f++ ) 00085 { 00086 if ( cmd[f].isLetter() ) 00087 b = true; 00088 if ( b && ( ! cmd[f].isLetterOrNumber() && cmd[f] != '-' && cmd[f] != '_' ) ) 00089 break; 00090 } 00091 return m_dict.value(cmd.left(f)); 00092 } 00093 00094 QList<KTextEditor::Command*> KateCmd::commands() const 00095 { 00096 return m_dict.values(); 00097 } 00098 00099 QStringList KateCmd::commandList () const 00100 { 00101 return m_cmds; 00102 } 00103 00104 KateCmd *KateCmd::self () 00105 { 00106 return KateGlobal::self()->cmdManager (); 00107 } 00108 00109 void KateCmd::appendHistory( const QString &cmd ) 00110 { 00111 if (!m_history.isEmpty()) //this line should be backported to 3.x 00112 if ( m_history.last() == cmd ) 00113 return; 00114 00115 if ( m_history.count() == CMD_HIST_LENGTH ) 00116 m_history.removeFirst(); 00117 00118 m_history.append( cmd ); 00119 } 00120 00121 const QString KateCmd::fromHistory( int index ) const 00122 { 00123 if ( index < 0 || index > m_history.count() - 1 ) 00124 return QString(); 00125 return m_history[ index ]; 00126 } 00127 00128 KCompletion* KateCmd::commandCompletionObject() 00129 { 00130 return &m_cmdCompletion; 00131 } 00132 //END KateCmd 00133 00134 //BEGIN KateCmdShellCompletion 00135 /* 00136 A lot of the code in the below class is copied from 00137 kdelibs/kio/kio/kshellcompletion.cpp 00138 Copyright (C) 2000 David Smith <dsmith@algonet.se> 00139 Copyright (C) 2004 Anders Lund <anders@alweb.dk> 00140 */ 00141 KateCmdShellCompletion::KateCmdShellCompletion() 00142 : KCompletion() 00143 { 00144 m_word_break_char = ' '; 00145 m_quote_char1 = '\"'; 00146 m_quote_char2 = '\''; 00147 m_escape_char = '\\'; 00148 } 00149 00150 QString KateCmdShellCompletion::makeCompletion( const QString &text ) 00151 { 00152 // Split text at the last unquoted space 00153 // 00154 splitText(text, m_text_start, m_text_compl); 00155 00156 // Make completion on the last part of text 00157 // 00158 return KCompletion::makeCompletion( m_text_compl ); 00159 } 00160 00161 void KateCmdShellCompletion::postProcessMatch( QString *match ) const 00162 { 00163 if ( match->isNull() ) 00164 return; 00165 00166 match->prepend( m_text_start ); 00167 } 00168 00169 void KateCmdShellCompletion::postProcessMatches( QStringList *matches ) const 00170 { 00171 for ( QStringList::Iterator it = matches->begin(); 00172 it != matches->end(); it++ ) 00173 if ( !(*it).isNull() ) 00174 (*it).prepend( m_text_start ); 00175 } 00176 00177 void KateCmdShellCompletion::postProcessMatches( KCompletionMatches *matches ) const 00178 { 00179 for ( KCompletionMatches::Iterator it = matches->begin(); 00180 it != matches->end(); it++ ) 00181 if ( !(*it).value().isNull() ) 00182 (*it).value().prepend( m_text_start ); 00183 } 00184 00185 void KateCmdShellCompletion::splitText(const QString &text, QString &text_start, 00186 QString &text_compl) const 00187 { 00188 bool in_quote = false; 00189 bool escaped = false; 00190 QChar p_last_quote_char; 00191 int last_unquoted_space = -1; 00192 int end_space_len = 0; 00193 00194 for (int pos = 0; pos < text.length(); pos++) { 00195 00196 end_space_len = 0; 00197 00198 if ( escaped ) { 00199 escaped = false; 00200 } 00201 else if ( in_quote && text[pos] == p_last_quote_char ) { 00202 in_quote = false; 00203 } 00204 else if ( !in_quote && text[pos] == m_quote_char1 ) { 00205 p_last_quote_char = m_quote_char1; 00206 in_quote = true; 00207 } 00208 else if ( !in_quote && text[pos] == m_quote_char2 ) { 00209 p_last_quote_char = m_quote_char2; 00210 in_quote = true; 00211 } 00212 else if ( text[pos] == m_escape_char ) { 00213 escaped = true; 00214 } 00215 else if ( !in_quote && text[pos] == m_word_break_char ) { 00216 00217 end_space_len = 1; 00218 00219 while ( pos+1 < text.length() && text[pos+1] == m_word_break_char ) { 00220 end_space_len++; 00221 pos++; 00222 } 00223 00224 if ( pos+1 == text.length() ) 00225 break; 00226 00227 last_unquoted_space = pos; 00228 } 00229 } 00230 00231 text_start = text.left( last_unquoted_space + 1 ); 00232 00233 // the last part without trailing blanks 00234 text_compl = text.mid( last_unquoted_space + 1 ); 00235 } 00236 00237 //END KateCmdShellCompletion 00238 00239 // kate: space-indent on; indent-width 2; replace-tabs on;
KDE 4.6 API Reference