KIO
8 #include "kshellcompletion.h"
10 #include <KCompletion>
11 #include <KCompletionMatches>
14 class KShellCompletionPrivate
17 KShellCompletionPrivate()
26 bool quoteText(
QString *text,
bool force,
bool skip_last)
const;
32 QChar m_word_break_char;
40 , d(new KShellCompletionPrivate)
44 KShellCompletion::~KShellCompletion() =
default;
55 d->splitText(text, d->m_text_start, d->m_text_compl);
59 QString tmp = d->unquote(d->m_text_compl);
60 d->m_text_compl = tmp;
64 const bool is_exe_completion = !d->m_text_start.contains(d->m_word_break_char);
66 setMode(is_exe_completion ? ExeCompletion : FileCompletion);
81 void KShellCompletion::postProcessMatch(
QString *match)
const
83 KUrlCompletion::postProcessMatch(
match);
85 if (
match->isNull()) {
90 d->quoteText(
match,
false,
true);
92 d->quoteText(
match,
false,
false);
95 match->prepend(d->m_text_start);
98 void KShellCompletion::postProcessMatches(
QStringList *matches)
const
100 KUrlCompletion::postProcessMatches(
matches);
103 if (!
match.isNull()) {
105 d->quoteText(&
match,
false,
true);
107 d->quoteText(&
match,
false,
false);
110 match.prepend(d->m_text_start);
117 KUrlCompletion::postProcessMatches(
matches);
121 if (!matchString.
isNull()) {
123 d->quoteText(&matchString,
false,
true);
125 d->quoteText(&matchString,
false,
false);
128 matchString.
prepend(d->m_text_start);
141 void KShellCompletionPrivate::splitText(
const QString &text,
QString &text_start,
QString &text_compl)
const
143 bool in_quote =
false;
144 bool escaped =
false;
145 QChar p_last_quote_char;
146 int last_unquoted_space = -1;
148 for (
int pos = 0; pos < text.
length(); pos++) {
149 int end_space_len = 0;
153 }
else if (in_quote && text[pos] == p_last_quote_char) {
155 }
else if (!in_quote && text[pos] == m_quote_char1) {
156 p_last_quote_char = m_quote_char1;
158 }
else if (!in_quote && text[pos] == m_quote_char2) {
159 p_last_quote_char = m_quote_char2;
161 }
else if (text[pos] == m_escape_char) {
163 }
else if (!in_quote && text[pos] == m_word_break_char) {
166 while (pos + 1 < text.
length() && text[pos + 1] == m_word_break_char) {
171 if (pos + 1 == text.
length()) {
175 last_unquoted_space = pos;
179 text_start = text.
left(last_unquoted_space + 1);
182 text_compl = text.
mid(last_unquoted_space + 1);
193 bool KShellCompletionPrivate::quoteText(
QString *text,
bool force,
bool skip_last)
const
198 pos = text->
indexOf(m_word_break_char);
199 if (skip_last && (pos == (
int)(text->
length()) - 1)) {
204 if (!force && pos == -1) {
205 pos = text->
indexOf(m_quote_char1);
206 if (skip_last && (pos == (
int)(text->
length()) - 1)) {
211 if (!force && pos == -1) {
212 pos = text->
indexOf(m_quote_char2);
213 if (skip_last && (pos == (
int)(text->
length()) - 1)) {
218 if (!force && pos == -1) {
219 pos = text->
indexOf(m_escape_char);
220 if (skip_last && (pos == (
int)(text->
length()) - 1)) {
225 if (force || (pos >= 0)) {
227 text->
replace(m_escape_char,
QString(m_escape_char) + m_escape_char);
230 text->
replace(m_quote_char1,
QString(m_escape_char) + m_quote_char1);
233 text->
insert(0, m_quote_char1);
254 QString KShellCompletionPrivate::unquote(
const QString &text)
const
256 bool in_quote =
false;
257 bool escaped =
false;
258 QChar p_last_quote_char;
261 for (
const QChar ch : text) {
265 }
else if (in_quote && ch == p_last_quote_char) {
267 }
else if (!in_quote && ch == m_quote_char1) {
268 p_last_quote_char = m_quote_char1;
270 }
else if (!in_quote && ch == m_quote_char2) {
271 p_last_quote_char = m_quote_char2;
273 }
else if (ch == m_escape_char) {
284 #include "moc_kshellcompletion.cpp"
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
bool isNull() const const
QString & prepend(QChar ch)
QString makeCompletion(const QString &text) override
Finds completions to the given text.
void match(const QString &item)
Completion of a single URL.
void matches(const QStringList &matchlist)
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
QString makeCompletion(const QString &text) override
Finds completions to the given text.
QString & replace(int position, int n, QChar after)
virtual void setMode(Mode mode)
Changes the completion mode: exe or file completion.
QString & insert(int position, QChar ch)
QString left(int n) const const
KShellCompletion()
Constructs a KShellCompletion object.
QString mid(int position, int n) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Dec 11 2023 03:50:14 by
doxygen 1.8.17 written
by
Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.