23#include <tqstringlist.h>
25#include <kcompletion.h>
27#include "kshellcompletion.h"
29class KShellCompletionPrivate
35 m_word_break_char =
' ';
50 splitText(text, m_text_start, m_text_compl);
54 TQString tmp = unquote(m_text_compl);
59 bool is_exe_completion =
true;
61 for ( uint i = 0; i < m_text_start.length(); i++ ) {
62 if ( m_text_start[i] != m_word_break_char ) {
63 is_exe_completion =
false;
68 Mode mode = (is_exe_completion ? ExeCompletion : FileCompletion );
85void KShellCompletion::postProcessMatch( TQString *match )
const
90 KURLCompletion::postProcessMatch( match );
92 if ( match->isNull() )
95 if ( match->right(1) == TQChar(
'/') )
96 quoteText( match,
false,
true );
98 quoteText( match,
false,
false );
100 match->prepend( m_text_start );
106void KShellCompletion::postProcessMatches( TQStringList *matches )
const
108 KURLCompletion::postProcessMatches( matches );
110 for ( TQStringList::Iterator it = matches->begin();
111 it != matches->end(); it++ )
113 if ( !(*it).isNull() ) {
114 if ( (*it).right(1) == TQChar(
'/') )
115 quoteText( &(*it),
false,
true );
117 quoteText( &(*it),
false,
false );
119 (*it).prepend( m_text_start );
124void KShellCompletion::postProcessMatches( TDECompletionMatches *matches )
const
126 KURLCompletion::postProcessMatches( matches );
128 for ( TDECompletionMatches::Iterator it = matches->begin();
129 it != matches->end(); it++ )
131 if ( !(*it).value().isNull() ) {
132 if ( (*it).value().right(1) == TQChar(
'/') )
133 quoteText( &(*it).value(),
false,
true );
135 quoteText( &(*it).value(),
false,
false );
137 (*it).value().prepend( m_text_start );
150void KShellCompletion::splitText(
const TQString &text, TQString &text_start,
151 TQString &text_compl)
const
153 bool in_quote =
false;
154 bool escaped =
false;
155 TQChar p_last_quote_char;
156 int last_unquoted_space = -1;
157 int end_space_len = 0;
159 for (uint pos = 0; pos < text.length(); pos++) {
166 else if ( in_quote && text[pos] == p_last_quote_char ) {
169 else if ( !in_quote && text[pos] == m_quote_char1 ) {
170 p_last_quote_char = m_quote_char1;
173 else if ( !in_quote && text[pos] == m_quote_char2 ) {
174 p_last_quote_char = m_quote_char2;
177 else if ( text[pos] == m_escape_char ) {
180 else if ( !in_quote && text[pos] == m_word_break_char ) {
184 while ( pos+1 < text.length() && text[pos+1] == m_word_break_char ) {
189 if ( pos+1 == text.length() )
192 last_unquoted_space = pos;
196 text_start = text.left( last_unquoted_space + 1 );
199 text_compl = text.mid( last_unquoted_space + 1 );
215bool KShellCompletion::quoteText(TQString *text,
bool force,
bool skip_last)
const
220 pos = text->find( m_word_break_char );
221 if ( skip_last && (pos == (
int)(text->length())-1) ) pos = -1;
224 if ( !force && pos == -1 ) {
225 pos = text->find( m_quote_char1 );
226 if ( skip_last && (pos == (
int)(text->length())-1) ) pos = -1;
229 if ( !force && pos == -1 ) {
230 pos = text->find( m_quote_char2 );
231 if ( skip_last && (pos == (
int)(text->length())-1) ) pos = -1;
234 if ( !force && pos == -1 ) {
235 pos = text->find( m_escape_char );
236 if ( skip_last && (pos == (
int)(text->length())-1) ) pos = -1;
239 if ( force || (pos >= 0) ) {
242 text->replace( m_escape_char,
243 TQString( m_escape_char ) + m_escape_char );
246 text->replace( m_quote_char1,
247 TQString( m_escape_char ) + m_quote_char1 );
250 text->insert( 0, m_quote_char1 );
254 text->insert( text->length()-1, m_quote_char1 );
256 text->insert( text->length(), m_quote_char1 );
270TQString KShellCompletion::unquote(
const TQString &text)
const
272 bool in_quote =
false;
273 bool escaped =
false;
274 TQChar p_last_quote_char;
277 for (uint pos = 0; pos < text.length(); pos++) {
281 result.insert( result.length(), text[pos] );
283 else if ( in_quote && text[pos] == p_last_quote_char ) {
286 else if ( !in_quote && text[pos] == m_quote_char1 ) {
287 p_last_quote_char = m_quote_char1;
290 else if ( !in_quote && text[pos] == m_quote_char2 ) {
291 p_last_quote_char = m_quote_char2;
294 else if ( text[pos] == m_escape_char ) {
296 result.insert( result.length(), text[pos] );
299 result.insert( result.length(), text[pos] );
307void KShellCompletion::virtual_hook(
int id,
void* data )
308{ KURLCompletion::virtual_hook(
id, data ); }
310#include "kshellcompletion.moc"
KShellCompletion()
Constructs a KShellCompletion object.
TQString makeCompletion(const TQString &text)
Finds completions to the given text.
This class does completion of URLs including user directories (~user) and environment variables.
Mode
Determines how completion is done.
virtual Mode mode() const
Returns the completion mode: exe or file completion (default FileCompletion).
virtual void setMode(Mode mode)
Changes the completion mode: exe or file completion.
virtual TQString makeCompletion(const TQString &text)
Finds completions to the given text.