diff --git a/lib/TerminalDisplay.cpp b/lib/TerminalDisplay.cpp index e321a547..167aa67e 100644 --- a/lib/TerminalDisplay.cpp +++ b/lib/TerminalDisplay.cpp @@ -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) @@ -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. @@ -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()) @@ -2837,6 +2889,10 @@ void TerminalDisplay::setTrimPastedTrailingNewlines(bool trimPastedTrailingNewli _trimPastedTrailingNewlines = trimPastedTrailingNewlines; } +void TerminalDisplay::setQuotePastedUrls(bool quotePastedUrls) { + _quotePastedUrls = quotePastedUrls; +} + /* ------------------------------------------------------------------------- */ /* */ /* Keyboard */ diff --git a/lib/TerminalDisplay.h b/lib/TerminalDisplay.h index 3f6972bf..d048b27f 100644 --- a/lib/TerminalDisplay.h +++ b/lib/TerminalDisplay.h @@ -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 @@ -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. @@ -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(); @@ -839,6 +841,7 @@ private slots: MotionAfterPasting mMotionAfterPasting; bool _confirmMultilinePaste; bool _trimPastedTrailingNewlines; + bool _quotePastedUrls; struct InputMethodData { diff --git a/lib/qtermwidget.cpp b/lib/qtermwidget.cpp index e5c58a58..73296231 100644 --- a/lib/qtermwidget.cpp +++ b/lib/qtermwidget.cpp @@ -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); +} diff --git a/lib/qtermwidget.h b/lib/qtermwidget.h index 76441966..de2d9d49 100644 --- a/lib/qtermwidget.h +++ b/lib/qtermwidget.h @@ -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);