• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Kate

  • kde-4.14
  • applications
  • kate
  • part
  • utils
katecmd.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries and the Kate part.
2  *
3  * Copyright (C) 2001-2010 Christoph Cullmann <cullmann@kde.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #include "katecmd.h"
22 #include "kateglobal.h"
23 
24 #include <kdebug.h>
25 
26 //BEGIN KateCmd
27 #define CMD_HIST_LENGTH 256
28 
29 KateCmd::KateCmd ()
30 {
31  m_cmdCompletion.addItem("help");
32 }
33 
34 KateCmd::~KateCmd ()
35 {
36 }
37 
38 bool KateCmd::registerCommand (KTextEditor::Command *cmd)
39 {
40  QStringList l = cmd->cmds ();
41 
42  for (int z=0; z<l.count(); z++)
43  if (m_dict.contains(l[z])) {
44  kDebug(13050)<<"Command already registered: "<<l[z]<<". Aborting.";
45  return false;
46  }
47 
48  for (int z=0; z<l.count(); z++) {
49  m_dict.insert (l[z], cmd);
50  //kDebug(13050)<<"Inserted command:"<<l[z];
51  }
52 
53  m_cmds += l;
54  m_cmdCompletion.insertItems(l);
55 
56  return true;
57 }
58 
59 bool KateCmd::unregisterCommand (KTextEditor::Command *cmd)
60 {
61  QStringList l;
62 
63  QHash<QString, KTextEditor::Command*>::const_iterator i = m_dict.constBegin();
64  while (i != m_dict.constEnd()) {
65  if (i.value()==cmd) l << i.key();
66  ++i;
67  }
68 
69  for ( QStringList::Iterator it1 = l.begin(); it1 != l.end(); ++it1 ) {
70  m_dict.remove(*it1);
71  m_cmdCompletion.removeItem(*it1);
72  //kDebug(13050)<<"Removed command:"<<*it1;
73  }
74 
75  return true;
76 }
77 
78 KTextEditor::Command *KateCmd::queryCommand (const QString &cmd) const
79 {
80  // a command can be named ".*[\w\-]+" with the constrain that it must
81  // contain at least one letter.
82  int f = 0;
83  bool b = false;
84 
85  // special case: '-' and '_' can be part of a command name, but if the
86  // command is 's' (substitute), it should be considered the delimiter and
87  // should not be counted as part of the command name
88  if ( cmd.length() >= 2 && cmd.at(0) == 's' && ( cmd.at(1) == '-' || cmd.at(1) == '_') ) {
89  return m_dict.value(QString("s"));
90  }
91 
92  for ( ; f < cmd.length(); f++ )
93  {
94  if ( cmd[f].isLetter() )
95  b = true;
96  if ( b && ( ! cmd[f].isLetterOrNumber() && cmd[f] != '-' && cmd[f] != '_' ) )
97  break;
98  }
99  return m_dict.value(cmd.left(f));
100 }
101 
102 QList<KTextEditor::Command*> KateCmd::commands() const
103 {
104  return m_dict.values();
105 }
106 
107 QStringList KateCmd::commandList () const
108 {
109  return m_cmds;
110 }
111 
112 KateCmd *KateCmd::self ()
113 {
114  return KateGlobal::self()->cmdManager ();
115 }
116 
117 void KateCmd::appendHistory( const QString &cmd )
118 {
119  if (!m_history.isEmpty()) //this line should be backported to 3.x
120  if ( m_history.last() == cmd )
121  return;
122 
123  if ( m_history.count() == CMD_HIST_LENGTH )
124  m_history.removeFirst();
125 
126  m_history.append( cmd );
127 }
128 
129 const QString KateCmd::fromHistory( int index ) const
130 {
131  if ( index < 0 || index > m_history.count() - 1 )
132  return QString();
133  return m_history[ index ];
134 }
135 
136 KCompletion* KateCmd::commandCompletionObject()
137 {
138  return &m_cmdCompletion;
139 }
140 //END KateCmd
141 
142 //BEGIN KateCmdShellCompletion
143 /*
144  A lot of the code in the below class is copied from
145  kdelibs/kio/kio/kshellcompletion.cpp
146  Copyright (C) 2000 David Smith <dsmith@algonet.se>
147  Copyright (C) 2004 Anders Lund <anders@alweb.dk>
148 */
149 KateCmdShellCompletion::KateCmdShellCompletion()
150  : KCompletion()
151 {
152  m_word_break_char = ' ';
153  m_quote_char1 = '\"';
154  m_quote_char2 = '\'';
155  m_escape_char = '\\';
156 }
157 
158 QString KateCmdShellCompletion::makeCompletion( const QString &text )
159 {
160  // Split text at the last unquoted space
161  //
162  splitText(text, m_text_start, m_text_compl);
163 
164  // Make completion on the last part of text
165  //
166  return KCompletion::makeCompletion( m_text_compl );
167 }
168 
169 void KateCmdShellCompletion::postProcessMatch( QString *match ) const
170 {
171  if ( match->isNull() )
172  return;
173 
174  match->prepend( m_text_start );
175 }
176 
177 void KateCmdShellCompletion::postProcessMatches( QStringList *matches ) const
178 {
179  for ( QStringList::Iterator it = matches->begin();
180  it != matches->end(); it++ )
181  if ( !(*it).isNull() )
182  (*it).prepend( m_text_start );
183 }
184 
185 void KateCmdShellCompletion::postProcessMatches( KCompletionMatches *matches ) const
186 {
187  for ( KCompletionMatches::Iterator it = matches->begin();
188  it != matches->end(); it++ )
189  if ( !(*it).value().isNull() )
190  (*it).value().prepend( m_text_start );
191 }
192 
193 void KateCmdShellCompletion::splitText(const QString &text, QString &text_start,
194  QString &text_compl) const
195 {
196  bool in_quote = false;
197  bool escaped = false;
198  QChar p_last_quote_char;
199  int last_unquoted_space = -1;
200  int end_space_len = 0;
201 
202  for (int pos = 0; pos < text.length(); pos++) {
203 
204  end_space_len = 0;
205 
206  if ( escaped ) {
207  escaped = false;
208  }
209  else if ( in_quote && text[pos] == p_last_quote_char ) {
210  in_quote = false;
211  }
212  else if ( !in_quote && text[pos] == m_quote_char1 ) {
213  p_last_quote_char = m_quote_char1;
214  in_quote = true;
215  }
216  else if ( !in_quote && text[pos] == m_quote_char2 ) {
217  p_last_quote_char = m_quote_char2;
218  in_quote = true;
219  }
220  else if ( text[pos] == m_escape_char ) {
221  escaped = true;
222  }
223  else if ( !in_quote && text[pos] == m_word_break_char ) {
224 
225  end_space_len = 1;
226 
227  while ( pos+1 < text.length() && text[pos+1] == m_word_break_char ) {
228  end_space_len++;
229  pos++;
230  }
231 
232  if ( pos+1 == text.length() )
233  break;
234 
235  last_unquoted_space = pos;
236  }
237  }
238 
239  text_start = text.left( last_unquoted_space + 1 );
240 
241  // the last part without trailing blanks
242  text_compl = text.mid( last_unquoted_space + 1 );
243 }
244 
245 //END KateCmdShellCompletion
246 
247 // kate: space-indent on; indent-width 2; replace-tabs on;
KateCmdShellCompletion::postProcessMatches
void postProcessMatches(QStringList *matches) const
Definition: katecmd.cpp:177
QHash::insert
iterator insert(const Key &key, const T &value)
KateCmd::unregisterCommand
bool unregisterCommand(KTextEditor::Command *cmd)
Definition: katecmd.cpp:59
QHash::key
const Key key(const T &value) const
QChar
QString::prepend
QString & prepend(QChar ch)
QList::removeFirst
void removeFirst()
KateCmd::self
static KateCmd * self()
Definition: katecmd.cpp:112
KateCmdShellCompletion::makeCompletion
QString makeCompletion(const QString &text)
Finds completions to the given text.
Definition: katecmd.cpp:158
KateGlobal::self
static KateGlobal * self()
Kate Part Internal stuff ;)
Definition: kateglobal.cpp:465
KateCmd::appendHistory
void appendHistory(const QString &cmd)
Definition: katecmd.cpp:117
KateCmdShellCompletion::postProcessMatch
void postProcessMatch(QString *match) const
Definition: katecmd.cpp:169
QString::isNull
bool isNull() const
KateCmd::~KateCmd
~KateCmd()
Definition: katecmd.cpp:34
QList::count
int count(const T &value) const
QList::append
void append(const T &value)
KateCmd::fromHistory
const QString fromHistory(int i) const
Definition: katecmd.cpp:129
QHash::constEnd
const_iterator constEnd() const
QHash
kateglobal.h
QList::isEmpty
bool isEmpty() const
QList::Iterator
typedef Iterator
katecmd.h
QString
QList< KTextEditor::Command * >
QHash::remove
int remove(const Key &key)
QStringList
QList::end
iterator end()
QHash::value
const T value(const Key &key) const
KateCmd::KateCmd
KateCmd()
Definition: katecmd.cpp:29
KateCmd::commands
QList< KTextEditor::Command * > commands() const
Definition: katecmd.cpp:102
QHash::constBegin
const_iterator constBegin() const
KateCmd::queryCommand
KTextEditor::Command * queryCommand(const QString &cmd) const
Definition: katecmd.cpp:78
KateCmd::commandCompletionObject
KCompletion * commandCompletionObject()
Definition: katecmd.cpp:136
KateCmd::commandList
QStringList commandList() const
Definition: katecmd.cpp:107
QString::mid
QString mid(int position, int n) const
QString::at
const QChar at(int position) const
QList::last
T & last()
QHash::values
QList< T > values() const
QString::length
int length() const
KateCmd::registerCommand
bool registerCommand(KTextEditor::Command *cmd)
Definition: katecmd.cpp:38
QString::left
QString left(int n) const
KateCmd
Definition: katecmd.h:33
QHash::contains
bool contains(const Key &key) const
KateGlobal::cmdManager
KateCmd * cmdManager()
command manager
Definition: kateglobal.h:333
KateCmdShellCompletion::KateCmdShellCompletion
KateCmdShellCompletion()
Definition: katecmd.cpp:149
CMD_HIST_LENGTH
#define CMD_HIST_LENGTH
Definition: katecmd.cpp:27
QList::begin
iterator begin()
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:57 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal