24 #include <QtCore/QCharRef>
25 #include <QtCore/QMutableStringListIterator>
26 #include <QtCore/QRegExp>
29 class KShellCompletionPrivate
32 KShellCompletionPrivate()
33 : m_word_break_char(
' ')
34 , m_quote_char1(
'\"' )
35 , m_quote_char2(
'\'' )
36 , m_escape_char(
'\\' )
41 bool quoteText(
QString *text,
bool force,
bool skip_last)
const;
47 QChar m_word_break_char;
55 d( new KShellCompletionPrivate )
74 d->splitText(text, d->m_text_start, d->m_text_compl);
78 QString tmp = d->unquote(d->m_text_compl);
79 d->m_text_compl = tmp;
83 bool is_exe_completion =
true;
85 for (
int i = 0; i < d->m_text_start.length(); i++ ) {
86 if ( d->m_text_start[i] != d->m_word_break_char ) {
87 is_exe_completion =
false;
113 if ( match->isNull() )
116 if ( match->endsWith( QLatin1Char(
'/' ) ) )
117 d->quoteText( match,
false,
true );
119 d->quoteText( match,
false,
false );
121 match->prepend( d->m_text_start );
128 for ( QStringList::Iterator it = matches->begin();
129 it != matches->end(); ++it )
131 if ( !(*it).isNull() ) {
132 if ( (*it).endsWith( QLatin1Char(
'/' ) ) )
133 d->quoteText( &(*it),
false,
true );
135 d->quoteText( &(*it),
false,
false );
137 (*it).prepend( d->m_text_start );
146 for ( KCompletionMatches::Iterator it = matches->begin();
147 it != matches->end(); ++it )
149 if ( !(*it).value().isNull() ) {
150 if ( (*it).value().endsWith( QLatin1Char(
'/' ) ) )
151 d->quoteText( &(*it).value(),
false, true );
153 d->quoteText( &(*it).value(),
false, false );
155 (*it).value().prepend( d->m_text_start );
168 void KShellCompletionPrivate::splitText(
const QString &text,
QString &text_start,
171 bool in_quote =
false;
172 bool escaped =
false;
173 QChar p_last_quote_char;
174 int last_unquoted_space = -1;
175 int end_space_len = 0;
177 for (
int pos = 0; pos < text.length(); pos++) {
184 else if ( in_quote && text[pos] == p_last_quote_char ) {
187 else if ( !in_quote && text[pos] == m_quote_char1 ) {
188 p_last_quote_char = m_quote_char1;
191 else if ( !in_quote && text[pos] == m_quote_char2 ) {
192 p_last_quote_char = m_quote_char2;
195 else if ( text[pos] == m_escape_char ) {
198 else if ( !in_quote && text[pos] == m_word_break_char ) {
202 while ( pos+1 < text.length() && text[pos+1] == m_word_break_char ) {
207 if ( pos+1 == text.length() )
210 last_unquoted_space = pos;
214 text_start = text.left( last_unquoted_space + 1 );
217 text_compl = text.mid( last_unquoted_space + 1 );
228 bool KShellCompletionPrivate::quoteText(
QString *text,
bool force,
bool skip_last)
const
233 pos = text->indexOf( m_word_break_char );
234 if ( skip_last && (pos == (
int)(text->length())-1) ) pos = -1;
237 if ( !force && pos == -1 ) {
238 pos = text->indexOf( m_quote_char1 );
239 if ( skip_last && (pos == (
int)(text->length())-1) ) pos = -1;
242 if ( !force && pos == -1 ) {
243 pos = text->indexOf( m_quote_char2 );
244 if ( skip_last && (pos == (
int)(text->length())-1) ) pos = -1;
247 if ( !force && pos == -1 ) {
248 pos = text->indexOf( m_escape_char );
249 if ( skip_last && (pos == (
int)(text->length())-1) ) pos = -1;
252 if ( force || (pos >= 0) ) {
255 text->replace( m_escape_char,
256 QString( m_escape_char ) + m_escape_char );
259 text->replace( m_quote_char1,
260 QString( m_escape_char ) + m_quote_char1 );
263 text->insert( 0, m_quote_char1 );
267 text->insert( text->length()-1, m_quote_char1 );
269 text->insert( text->length(), m_quote_char1 );
283 QString KShellCompletionPrivate::unquote(
const QString &text)
const
285 bool in_quote =
false;
286 bool escaped =
false;
287 QChar p_last_quote_char;
290 for (
int pos = 0; pos < text.length(); pos++) {
294 result.insert( result.length(), text[pos] );
296 else if ( in_quote && text[pos] == p_last_quote_char ) {
299 else if ( !in_quote && text[pos] == m_quote_char1 ) {
300 p_last_quote_char = m_quote_char1;
303 else if ( !in_quote && text[pos] == m_quote_char2 ) {
304 p_last_quote_char = m_quote_char2;
307 else if ( text[pos] == m_escape_char ) {
309 result.insert( result.length(), text[pos] );
312 result.insert( result.length(), text[pos] );
320 #include "kshellcompletion.moc"
void postProcessMatch(QString *match) const
virtual void setMode(Mode mode)
Changes the completion mode: exe or file completion.
Mode
Determines how completion is done.
virtual Mode mode() const
Returns the completion mode: exe or file completion (default FileCompletion).
KShellCompletion()
Constructs a KShellCompletion object.
virtual QString makeCompletion(const QString &text)
Finds completions to the given text.
void postProcessMatch(QString *match) const
void postProcessMatches(QStringList *matches) const
void postProcessMatches(QStringList *matches) const
QString makeCompletion(const QString &text)
Finds completions to the given text.
This class does completion of URLs including user directories (~user) and environment variables...