Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a feature for quoting pasted urls #427

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions lib/TerminalDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ TerminalDisplay::TerminalDisplay(QWidget *parent)
,_filterChain(new TerminalImageFilterChain())
,_cursorShape(Emulation::KeyboardCursorShape::BlockCursor)
,mMotionAfterPasting(NoMoveScreenWindow)
,_confirmMultilinePaste(false)
,_trimPastedTrailingNewlines(false)
,_quotePastedUrls(false)
,_leftBaseMargin(1)
,_topBaseMargin(1)
,_drawLineChars(true)
Expand Down Expand Up @@ -2758,6 +2761,7 @@ void TerminalDisplay::emitSelection(bool useXselection,bool appendReturn)
}

bracketText(text);
quoteUrl(text);

// appendReturn is intentionally handled _after_ enclosing texts with brackets as
// that feature is used to allow execution of commands immediately after paste.
Expand Down Expand Up @@ -2800,6 +2804,54 @@ void TerminalDisplay::bracketText(QString& text)
}
}

void TerminalDisplay::quoteUrl(QString& text)
{
if (!_quotePastedUrls)
return;

int i = 0;
while (i < text.size()) {
if ((i && !text[i - 1].isSpace()) || text[i] != QLatin1Char('h')) {
++i;
continue;
}

const QStringRef ref = text.midRef(i, 8);
const int urlStart = i;
if (ref == QLatin1String("https://")) {
i += 6;
} else if (ref.startsWith(QLatin1String("http://"))) {
i += 5;
} else {
++i;
continue;
}

bool ok = true;
while (++i < text.size() && ok) {
switch (text[i].toLatin1())
{
case ' ':
case '\r':
case '\n':
case '\t':
text.insert(i, QLatin1Char('"'));
text.insert(urlStart, QLatin1Char('"'));
break;
case '\'':
case '"':
case '\\':
ok = false;
break;
}
}
if (i == text.size()) {
text.append(QLatin1Char('"'));
text.insert(urlStart, QLatin1Char('"'));
}
}
}

void TerminalDisplay::setSelection(const QString& t)
{
if (QApplication::clipboard()->supportsSelection())
Expand Down Expand Up @@ -2837,6 +2889,10 @@ void TerminalDisplay::setTrimPastedTrailingNewlines(bool trimPastedTrailingNewli
_trimPastedTrailingNewlines = trimPastedTrailingNewlines;
}

void TerminalDisplay::setQuotePastedUrls(bool quotePastedUrls) {
_quotePastedUrls = quotePastedUrls;
}

/* ------------------------------------------------------------------------- */
/* */
/* Keyboard */
Expand Down
9 changes: 6 additions & 3 deletions lib/TerminalDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ class KONSOLEPRIVATE_EXPORT TerminalDisplay : public QWidget

/** change and wrap text corresponding to paste mode **/
void bracketText(QString& text);
void quoteUrl(QString& text);

/**
* Sets the shape of the keyboard cursor. This is the cursor drawn
Expand Down Expand Up @@ -425,6 +426,7 @@ class KONSOLEPRIVATE_EXPORT TerminalDisplay : public QWidget
int motionAfterPasting();
void setConfirmMultilinePaste(bool confirmMultilinePaste);
void setTrimPastedTrailingNewlines(bool trimPastedTrailingNewlines);
void setQuotePastedUrls(bool quotePastedUrls);

// maps a point on the widget to the position ( ie. line and column )
// of the character at that point.
Expand Down Expand Up @@ -569,9 +571,9 @@ public slots:
void sendStringToEmu(const char*);

// qtermwidget signals
void copyAvailable(bool);
void termGetFocus();
void termLostFocus();
void copyAvailable(bool);
void termGetFocus();
void termLostFocus();

void notifyBell(const QString&);
void usesMouseChanged();
Expand Down Expand Up @@ -839,6 +841,7 @@ private slots:
MotionAfterPasting mMotionAfterPasting;
bool _confirmMultilinePaste;
bool _trimPastedTrailingNewlines;
bool _quotePastedUrls;

struct InputMethodData
{
Expand Down
4 changes: 4 additions & 0 deletions lib/qtermwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -797,3 +797,7 @@ void QTermWidget::setConfirmMultilinePaste(bool confirmMultilinePaste) {
void QTermWidget::setTrimPastedTrailingNewlines(bool trimPastedTrailingNewlines) {
m_impl->m_terminalDisplay->setTrimPastedTrailingNewlines(trimPastedTrailingNewlines);
}

void QTermWidget::setQuotePastedUrls(bool quotePastedUrls) {
m_impl->m_terminalDisplay->setQuotePastedUrls(quotePastedUrls);
}
1 change: 1 addition & 0 deletions lib/qtermwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ class QTERMWIDGET_EXPORT QTermWidget : public QWidget {

void setConfirmMultilinePaste(bool confirmMultilinePaste);
void setTrimPastedTrailingNewlines(bool trimPastedTrailingNewlines);
void setQuotePastedUrls(bool quotePastedUrls);
signals:
void finished();
void copyAvailable(bool);
Expand Down