• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeio/tdeio
 

tdeio/tdeio

  • tdeio
  • tdeio
kshellcompletion.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 2000 David Smith <dsmith@algonet.se>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include <stdlib.h>
21 #include <kdebug.h>
22 #include <tqstring.h>
23 #include <tqstringlist.h>
24 #include <tqregexp.h>
25 #include <kcompletion.h>
26 
27 #include "kshellcompletion.h"
28 
29 class KShellCompletionPrivate
30 {
31 };
32 
33 KShellCompletion::KShellCompletion() : KURLCompletion()
34 {
35  m_word_break_char = ' ';
36  m_quote_char1 = '\"';
37  m_quote_char2 = '\'';
38  m_escape_char = '\\';
39 }
40 
41 /*
42  * makeCompletion()
43  *
44  * Entry point for file name completion
45  */
46 TQString KShellCompletion::makeCompletion(const TQString &text)
47 {
48  // Split text at the last unquoted space
49  //
50  splitText(text, m_text_start, m_text_compl);
51 
52  // Remove quotes from the text to be completed
53  //
54  TQString tmp = unquote(m_text_compl);
55  m_text_compl = tmp;
56 
57  // Do exe-completion if there was no unquoted space
58  //
59  bool is_exe_completion = true;
60 
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;
64  break;
65  }
66  }
67 
68  Mode mode = (is_exe_completion ? ExeCompletion : FileCompletion );
69 
70  setMode(mode);
71 
72  // Make completion on the last part of text
73  //
74  return KURLCompletion::makeCompletion( m_text_compl );
75 }
76 
77 /*
78  * postProcessMatch, postProcessMatches
79  *
80  * Called by TDECompletion before emitting match() and matches()
81  *
82  * Add add the part of the text that was not completed
83  * Add quotes when needed
84  */
85 void KShellCompletion::postProcessMatch( TQString *match ) const
86 {
87  //kDebugInfo("KShellCompletion::postProcessMatch() in: '%s'",
88  // match->latin1());
89 
90  KURLCompletion::postProcessMatch( match );
91 
92  if ( match->isNull() )
93  return;
94 
95  if ( match->right(1) == TQChar('/') )
96  quoteText( match, false, true ); // don't quote the trailing '/'
97  else
98  quoteText( match, false, false ); // quote the whole text
99 
100  match->prepend( m_text_start );
101 
102  //kDebugInfo("KShellCompletion::postProcessMatch() ut: '%s'",
103  // match->latin1());
104 }
105 
106 void KShellCompletion::postProcessMatches( TQStringList *matches ) const
107 {
108  KURLCompletion::postProcessMatches( matches );
109 
110  for ( TQStringList::Iterator it = matches->begin();
111  it != matches->end(); it++ )
112  {
113  if ( !(*it).isNull() ) {
114  if ( (*it).right(1) == TQChar('/') )
115  quoteText( &(*it), false, true ); // don't quote trailing '/'
116  else
117  quoteText( &(*it), false, false ); // quote the whole text
118 
119  (*it).prepend( m_text_start );
120  }
121  }
122 }
123 
124 void KShellCompletion::postProcessMatches( TDECompletionMatches *matches ) const
125 {
126  KURLCompletion::postProcessMatches( matches );
127 
128  for ( TDECompletionMatches::Iterator it = matches->begin();
129  it != matches->end(); it++ )
130  {
131  if ( !(*it).value().isNull() ) {
132  if ( (*it).value().right(1) == TQChar('/') )
133  quoteText( &(*it).value(), false, true ); // don't quote trailing '/'
134  else
135  quoteText( &(*it).value(), false, false ); // quote the whole text
136 
137  (*it).value().prepend( m_text_start );
138  }
139  }
140 }
141 
142 /*
143  * splitText
144  *
145  * Split text at the last unquoted space
146  *
147  * text_start = [out] text at the left, including the space
148  * text_compl = [out] text at the right
149  */
150 void KShellCompletion::splitText(const TQString &text, TQString &text_start,
151  TQString &text_compl) const
152 {
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;
158 
159  for (uint pos = 0; pos < text.length(); pos++) {
160 
161  end_space_len = 0;
162 
163  if ( escaped ) {
164  escaped = false;
165  }
166  else if ( in_quote && text[pos] == p_last_quote_char ) {
167  in_quote = false;
168  }
169  else if ( !in_quote && text[pos] == m_quote_char1 ) {
170  p_last_quote_char = m_quote_char1;
171  in_quote = true;
172  }
173  else if ( !in_quote && text[pos] == m_quote_char2 ) {
174  p_last_quote_char = m_quote_char2;
175  in_quote = true;
176  }
177  else if ( text[pos] == m_escape_char ) {
178  escaped = true;
179  }
180  else if ( !in_quote && text[pos] == m_word_break_char ) {
181 
182  end_space_len = 1;
183 
184  while ( pos+1 < text.length() && text[pos+1] == m_word_break_char ) {
185  end_space_len++;
186  pos++;
187  }
188 
189  if ( pos+1 == text.length() )
190  break;
191 
192  last_unquoted_space = pos;
193  }
194  }
195 
196  text_start = text.left( last_unquoted_space + 1 );
197 
198  // the last part without trailing blanks
199  text_compl = text.mid( last_unquoted_space + 1 );
200 
201 // text_compl = text.mid( last_unquoted_space + 1,
202 // text.length() - end_space_len - (last_unquoted_space + 1) );
203 
204  //kDebugInfo("split right = '%s'", text_compl.latin1());
205 }
206 
207 /*
208  * quoteText()
209  *
210  * Add quotations to 'text' if needed or if 'force' = true
211  * Returns true if quotes were added
212  *
213  * skip_last => ignore the last charachter (we add a space or '/' to all filenames)
214  */
215 bool KShellCompletion::quoteText(TQString *text, bool force, bool skip_last) const
216 {
217  int pos = 0;
218 
219  if ( !force ) {
220  pos = text->find( m_word_break_char );
221  if ( skip_last && (pos == (int)(text->length())-1) ) pos = -1;
222  }
223 
224  if ( !force && pos == -1 ) {
225  pos = text->find( m_quote_char1 );
226  if ( skip_last && (pos == (int)(text->length())-1) ) pos = -1;
227  }
228 
229  if ( !force && pos == -1 ) {
230  pos = text->find( m_quote_char2 );
231  if ( skip_last && (pos == (int)(text->length())-1) ) pos = -1;
232  }
233 
234  if ( !force && pos == -1 ) {
235  pos = text->find( m_escape_char );
236  if ( skip_last && (pos == (int)(text->length())-1) ) pos = -1;
237  }
238 
239  if ( force || (pos >= 0) ) {
240 
241  // Escape \ in the string
242  text->replace( m_escape_char,
243  TQString( m_escape_char ) + m_escape_char );
244 
245  // Escape " in the string
246  text->replace( m_quote_char1,
247  TQString( m_escape_char ) + m_quote_char1 );
248 
249  // " at the beginning
250  text->insert( 0, m_quote_char1 );
251 
252  // " at the end
253  if ( skip_last )
254  text->insert( text->length()-1, m_quote_char1 );
255  else
256  text->insert( text->length(), m_quote_char1 );
257 
258  return true;
259  }
260 
261  return false;
262 }
263 
264 /*
265  * unquote
266  *
267  * Remove quotes and return the result in a new string
268  *
269  */
270 TQString KShellCompletion::unquote(const TQString &text) const
271 {
272  bool in_quote = false;
273  bool escaped = false;
274  TQChar p_last_quote_char;
275  TQString result;
276 
277  for (uint pos = 0; pos < text.length(); pos++) {
278 
279  if ( escaped ) {
280  escaped = false;
281  result.insert( result.length(), text[pos] );
282  }
283  else if ( in_quote && text[pos] == p_last_quote_char ) {
284  in_quote = false;
285  }
286  else if ( !in_quote && text[pos] == m_quote_char1 ) {
287  p_last_quote_char = m_quote_char1;
288  in_quote = true;
289  }
290  else if ( !in_quote && text[pos] == m_quote_char2 ) {
291  p_last_quote_char = m_quote_char2;
292  in_quote = true;
293  }
294  else if ( text[pos] == m_escape_char ) {
295  escaped = true;
296  result.insert( result.length(), text[pos] );
297  }
298  else {
299  result.insert( result.length(), text[pos] );
300  }
301 
302  }
303 
304  return result;
305 }
306 
307 void KShellCompletion::virtual_hook( int id, void* data )
308 { KURLCompletion::virtual_hook( id, data ); }
309 
310 #include "kshellcompletion.moc"
311 
KShellCompletion::KShellCompletion
KShellCompletion()
Constructs a KShellCompletion object.
Definition: kshellcompletion.cpp:33
KShellCompletion::makeCompletion
TQString makeCompletion(const TQString &text)
Finds completions to the given text.
Definition: kshellcompletion.cpp:46
KURLCompletion
This class does completion of URLs including user directories (~user) and environment variables.
Definition: kurlcompletion.h:42
KURLCompletion::Mode
Mode
Determines how completion is done.
Definition: kurlcompletion.h:53
KURLCompletion::mode
virtual Mode mode() const
Returns the completion mode: exe or file completion (default FileCompletion).
Definition: kurlcompletion.cpp:538
KURLCompletion::setMode
virtual void setMode(Mode mode)
Changes the completion mode: exe or file completion.
Definition: kurlcompletion.cpp:543
KURLCompletion::makeCompletion
virtual TQString makeCompletion(const TQString &text)
Finds completions to the given text.
Definition: kurlcompletion.cpp:573

tdeio/tdeio

Skip menu "tdeio/tdeio"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdeio/tdeio

Skip menu "tdeio/tdeio"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeio/tdeio by doxygen 1.9.1
This website is maintained by Timothy Pearson.