Kate
katecmd.cpp
Go to the documentation of this file.00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2001, 2003 Christoph Cullmann <cullmann@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 00017 */ 00018 00019 #include "katecmd.h" 00020 #include "kateglobal.h" 00021 00022 #include <kdebug.h> 00023 00024 //BEGIN KateCmd 00025 #define CMD_HIST_LENGTH 256 00026 00027 KateCmd::KateCmd () 00028 { 00029 } 00030 00031 KateCmd::~KateCmd () 00032 { 00033 } 00034 00035 bool KateCmd::registerCommand (KTextEditor::Command *cmd) 00036 { 00037 QStringList l = cmd->cmds (); 00038 00039 for (int z=0; z<l.count(); z++) 00040 if (m_dict.contains(l[z])) 00041 return false; 00042 00043 for (int z=0; z<l.count(); z++) { 00044 m_dict.insert (l[z], cmd); 00045 //kDebug(13050)<<"Inserted command:"<<l[z]; 00046 } 00047 00048 m_cmds += l; 00049 00050 return true; 00051 } 00052 00053 bool KateCmd::unregisterCommand (KTextEditor::Command *cmd) 00054 { 00055 QStringList l; 00056 00057 QHash<QString, KTextEditor::Command*>::const_iterator i = m_dict.constBegin(); 00058 while (i != m_dict.constEnd()) { 00059 if (i.value()==cmd) l << i.key(); 00060 ++i; 00061 } 00062 00063 for ( QStringList::Iterator it1 = l.begin(); it1 != l.end(); ++it1 ) { 00064 m_dict.remove(*it1); 00065 //kDebug(13050)<<"Removed command:"<<*it1; 00066 } 00067 00068 return true; 00069 } 00070 00071 KTextEditor::Command *KateCmd::queryCommand (const QString &cmd) const 00072 { 00073 // a command can be named ".*[\w\-]+" with the constrain that it must 00074 // contain at least one letter. 00075 int f = 0; 00076 bool b = false; 00077 for ( ; f < cmd.length(); f++ ) 00078 { 00079 if ( cmd[f].isLetter() ) 00080 b = true; 00081 if ( b && ( ! cmd[f].isLetterOrNumber() && cmd[f] != '-' && cmd[f] != '_' ) ) 00082 break; 00083 } 00084 return m_dict.value(cmd.left(f)); 00085 } 00086 00087 QList<KTextEditor::Command*> KateCmd::commands() const 00088 { 00089 return m_dict.values(); 00090 } 00091 00092 QStringList KateCmd::commandList () const 00093 { 00094 return m_cmds; 00095 } 00096 00097 KateCmd *KateCmd::self () 00098 { 00099 return KateGlobal::self()->cmdManager (); 00100 } 00101 00102 void KateCmd::appendHistory( const QString &cmd ) 00103 { 00104 if (!m_history.isEmpty()) //this line should be backported to 3.x 00105 if ( m_history.last() == cmd ) 00106 return; 00107 00108 if ( m_history.count() == CMD_HIST_LENGTH ) 00109 m_history.removeFirst(); 00110 00111 m_history.append( cmd ); 00112 } 00113 00114 const QString KateCmd::fromHistory( int index ) const 00115 { 00116 if ( index < 0 || index > m_history.count() - 1 ) 00117 return QString(); 00118 return m_history[ index ]; 00119 } 00120 //END KateCmd 00121 00122 //BEGIN KateCmdShellCompletion 00123 /* 00124 A lot of the code in the below class is copied from 00125 kdelibs/kio/kio/kshellcompletion.cpp 00126 Copyright (C) 2000 David Smith <dsmith@algonet.se> 00127 Copyright (C) 2004 Anders Lund <anders@alweb.dk> 00128 */ 00129 KateCmdShellCompletion::KateCmdShellCompletion() 00130 : KCompletion() 00131 { 00132 m_word_break_char = ' '; 00133 m_quote_char1 = '\"'; 00134 m_quote_char2 = '\''; 00135 m_escape_char = '\\'; 00136 } 00137 00138 QString KateCmdShellCompletion::makeCompletion( const QString &text ) 00139 { 00140 // Split text at the last unquoted space 00141 // 00142 splitText(text, m_text_start, m_text_compl); 00143 00144 // Make completion on the last part of text 00145 // 00146 return KCompletion::makeCompletion( m_text_compl ); 00147 } 00148 00149 void KateCmdShellCompletion::postProcessMatch( QString *match ) const 00150 { 00151 if ( match->isNull() ) 00152 return; 00153 00154 match->prepend( m_text_start ); 00155 } 00156 00157 void KateCmdShellCompletion::postProcessMatches( QStringList *matches ) const 00158 { 00159 for ( QStringList::Iterator it = matches->begin(); 00160 it != matches->end(); it++ ) 00161 if ( !(*it).isNull() ) 00162 (*it).prepend( m_text_start ); 00163 } 00164 00165 void KateCmdShellCompletion::postProcessMatches( KCompletionMatches *matches ) const 00166 { 00167 for ( KCompletionMatches::Iterator it = matches->begin(); 00168 it != matches->end(); it++ ) 00169 if ( !(*it).value().isNull() ) 00170 (*it).value().prepend( m_text_start ); 00171 } 00172 00173 void KateCmdShellCompletion::splitText(const QString &text, QString &text_start, 00174 QString &text_compl) const 00175 { 00176 bool in_quote = false; 00177 bool escaped = false; 00178 QChar p_last_quote_char; 00179 int last_unquoted_space = -1; 00180 int end_space_len = 0; 00181 00182 for (int pos = 0; pos < text.length(); pos++) { 00183 00184 end_space_len = 0; 00185 00186 if ( escaped ) { 00187 escaped = false; 00188 } 00189 else if ( in_quote && text[pos] == p_last_quote_char ) { 00190 in_quote = false; 00191 } 00192 else if ( !in_quote && text[pos] == m_quote_char1 ) { 00193 p_last_quote_char = m_quote_char1; 00194 in_quote = true; 00195 } 00196 else if ( !in_quote && text[pos] == m_quote_char2 ) { 00197 p_last_quote_char = m_quote_char2; 00198 in_quote = true; 00199 } 00200 else if ( text[pos] == m_escape_char ) { 00201 escaped = true; 00202 } 00203 else if ( !in_quote && text[pos] == m_word_break_char ) { 00204 00205 end_space_len = 1; 00206 00207 while ( pos+1 < text.length() && text[pos+1] == m_word_break_char ) { 00208 end_space_len++; 00209 pos++; 00210 } 00211 00212 if ( pos+1 == text.length() ) 00213 break; 00214 00215 last_unquoted_space = pos; 00216 } 00217 } 00218 00219 text_start = text.left( last_unquoted_space + 1 ); 00220 00221 // the last part without trailing blanks 00222 text_compl = text.mid( last_unquoted_space + 1 ); 00223 } 00224 00225 //END KateCmdShellCompletion 00226 00227 // kate: space-indent on; indent-width 2; replace-tabs on;