diff --git a/addons/konsole/kateconsole.h b/addons/konsole/kateconsole.h --- a/addons/konsole/kateconsole.h +++ b/addons/konsole/kateconsole.h @@ -2,6 +2,7 @@ Copyright (C) 2001 Christoph Cullmann Copyright (C) 2002 Joseph Wenninger Copyright (C) 2002 Anders Lund + Copyright (C) 2017 Ederag This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public @@ -43,9 +44,9 @@ class KateKonsolePlugin: public KTextEditor::Plugin { Q_OBJECT - + friend class KateKonsolePluginView; - + public: explicit KateKonsolePlugin( QObject* parent = nullptr, const QList& = QList() ); ~KateKonsolePlugin() override; @@ -58,7 +59,7 @@ void readConfig(); QByteArray previousEditorEnv() {return m_previousEditorEnv;} - + private: QList mViews; QByteArray m_previousEditorEnv; @@ -137,11 +138,17 @@ * synchronize the konsole with the current document (cd to the directory) */ void slotSync(KTextEditor::View *view = nullptr); + /** * When syncing is done by the user, also show the terminal if it is hidden */ void slotManualSync(); + /** + * run the current document in the konsole + */ + void slotRun(); + private Q_SLOTS: /** * the konsole exited ;) @@ -158,7 +165,7 @@ * set or clear focus as appropriate. */ void slotToggleFocus(); - + /** * Handle that shortcuts are not eaten by console */ @@ -186,7 +193,7 @@ * toolview for this console */ QWidget *m_toolView; - + KateKonsolePlugin *m_plugin; QString m_currentPath; }; @@ -208,8 +215,16 @@ {} private: class QCheckBox *cbAutoSyncronize; + class QCheckBox *cbRemoveExtension; + class QLineEdit *lePrefix; class QCheckBox *cbSetEditor; KateKonsolePlugin *mPlugin; + + private Q_SLOTS: + /** + * Enable the warning dialog for the next "Run in terminal" + */ + void slotEnableRunWarning (); }; #endif // kate: space-indent on; indent-width 2; replace-tabs on; diff --git a/addons/konsole/kateconsole.cpp b/addons/konsole/kateconsole.cpp --- a/addons/konsole/kateconsole.cpp +++ b/addons/konsole/kateconsole.cpp @@ -3,6 +3,7 @@ Copyright (C) 2002 Joseph Wenninger Copyright (C) 2002 Anders Lund Copyright (C) 2007 Anders Lund + Copyright (C) 2017 Ederag This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public @@ -24,6 +25,7 @@ #include #include #include +#include #include #include @@ -39,6 +41,9 @@ #include #include #include +#include +#include +#include #include #include @@ -93,16 +98,16 @@ // init console QWidget *toolview = mainWindow->createToolView (plugin, QStringLiteral("kate_private_plugin_katekonsoleplugin"), KTextEditor::MainWindow::Bottom, QIcon::fromTheme(QStringLiteral("utilities-terminal")), i18n("Terminal")); m_console = new KateConsole(m_plugin, mainWindow, toolview); - + // register this view m_plugin->mViews.append ( this ); } KateKonsolePluginView::~KateKonsolePluginView () { // unregister this view m_plugin->mViews.removeAll (this); - + // cleanup, kill toolview + console QWidget *toolview = m_console->parentWidget(); delete m_console; @@ -132,10 +137,15 @@ a->setIcon(QIcon::fromTheme(QStringLiteral("utilities-terminal"))); a->setText(i18nc("@action", "&Pipe to Terminal")); connect(a, &QAction::triggered, this, &KateConsole::slotPipeToConsole); + a = actionCollection()->addAction(QStringLiteral("katekonsole_tools_sync")); a->setText(i18nc("@action", "S&ynchronize Terminal with Current Document")); connect(a, &QAction::triggered, this, &KateConsole::slotManualSync); + a = actionCollection()->addAction(QStringLiteral("katekonsole_tools_run")); + a->setText(i18nc("@action", "Run Current Document")); + connect(a, SIGNAL(triggered()), this, SLOT(slotRun())); + a = actionCollection()->addAction(QStringLiteral("katekonsole_tools_toggle_focus")); a->setIcon(QIcon::fromTheme(QStringLiteral("utilities-terminal"))); a->setText(i18nc("@action", "&Focus Terminal")); @@ -147,7 +157,7 @@ } KateConsole::~KateConsole () -{ +{ m_mw->guiFactory()->removeClient (this); if (m_part) disconnect(m_part, &KParts::ReadOnlyPart::destroyed, this, &KateConsole::slotDestroyed); @@ -159,8 +169,8 @@ if (!window() || !parentWidget()) return; if (!window() || !isVisibleTo(window())) return; - - + + /** * get konsole part factory */ @@ -292,6 +302,65 @@ if ( ! m_part || ! m_part->widget()->isVisible() ) m_mw->showToolView( parentWidget() ); } + +void KateConsole::slotRun() +{ + if ( m_mw->activeView() ) { + KTextEditor::Document *document = m_mw->activeView()->document(); + QUrl u = document->url(); + if ( ! u.isLocalFile() ) { + QPointer message = + new KTextEditor::Message( + i18n("Not a local file: '%1'", u.path()), + KTextEditor::Message::Error + ); + // auto hide is enabled and set to a sane default value of several seconds. + message->setAutoHide(2000); + message->setAutoHideMode( KTextEditor::Message::Immediate ); + document->postMessage( message ); + return; + } + // ensure that file is saved + if ( document->isModified() ) { + document->save(); + } + + // The string that should be output to terminal, upon acceptance + QString output_str; + // prefix first + output_str += KConfigGroup( KSharedConfig::openConfig(), + "Konsole" + ).readEntry("RunPrefix", ""); + // then filename + if ( KConfigGroup(KSharedConfig::openConfig(), + "Konsole" + ).readEntry("RemoveExtension", true) ) { + // append filename without extension (i.e. keep only the basename) + output_str += QFileInfo( u.path() ).baseName() + QLatin1Char('\n'); + } else { + // append filename to the terminal + output_str += QFileInfo( u.path() ).fileName() + QLatin1Char('\n'); + } + + if ( KMessageBox::Continue != + KMessageBox::warningContinueCancel( + m_mw->window(), + i18n("Do you really want to Run the document ?\n" + "This will execute the following command,\n" + "with your user rights, in the terminal:\n" + "'%1'", output_str), + i18n("Run in Terminal?"), + KGuiItem( i18n("Run") ), + KStandardGuiItem::cancel(), + QStringLiteral("Konsole: Run in Terminal Warning") ) + ) { + return; + } + // echo to terminal + sendInput( output_str ); + } +} + void KateConsole::slotToggleFocus() { QAction *action = actionCollection()->action(QStringLiteral("katekonsole_tools_toggle_focus")); @@ -306,7 +375,7 @@ if (m_part->widget()->hasFocus()) { if (m_mw->activeView()) m_mw->activeView()->setFocus(); - action->setText( i18n("Focus Terminal") ); + action->setText( i18n("Focus Terminal") ); } else { // show the view if it is hidden if (parentWidget()->isHidden()) @@ -339,17 +408,58 @@ cbAutoSyncronize = new QCheckBox( i18n("&Automatically synchronize the terminal with the current document when possible"), this ); lo->addWidget( cbAutoSyncronize ); + + QVBoxLayout *vboxRun = new QVBoxLayout; + QGroupBox *groupRun = new QGroupBox( i18n("Run in terminal"), this ); + // Remove extension + cbRemoveExtension = new QCheckBox( i18n("&Remove extension"), this ); + vboxRun->addWidget( cbRemoveExtension ); + // Prefix + QFrame *framePrefix = new QFrame( this ); + QHBoxLayout *hboxPrefix = new QHBoxLayout( framePrefix ); + QLabel *label = new QLabel( i18n("Prefix:"), framePrefix ); + hboxPrefix->addWidget( label ); + lePrefix = new QLineEdit( framePrefix ); + hboxPrefix->addWidget( lePrefix ); + vboxRun->addWidget( framePrefix ); + // show warning next time + QFrame *frameWarn = new QFrame( this ); + QHBoxLayout *hboxWarn = new QHBoxLayout( frameWarn ); + QPushButton *buttonWarn = new QPushButton( i18n("&Show warning next time"), frameWarn); + buttonWarn->setWhatsThis ( + i18n ( "The next time '%1' is executed, " + "make sure a warning window will pop up, " + "displaying the command to be sent to terminal, " + "for review.", + i18n ("Run in terminal") + ) + ); + connect( buttonWarn, &QPushButton::pressed, + this, &KateKonsoleConfigPage::slotEnableRunWarning ); + hboxWarn->addWidget( buttonWarn ); + vboxRun->addWidget( frameWarn ); + groupRun->setLayout( vboxRun ); + lo->addWidget( groupRun ); + cbSetEditor = new QCheckBox( i18n("Set &EDITOR environment variable to 'kate -b'"), this ); lo->addWidget( cbSetEditor ); QLabel *tmp = new QLabel(this); tmp->setText(i18n("Important: The document has to be closed to make the console application continue")); lo->addWidget(tmp); reset(); lo->addStretch(); connect(cbAutoSyncronize, &QCheckBox::stateChanged, this, &KateKonsoleConfigPage::changed); + connect( cbRemoveExtension, SIGNAL(stateChanged(int)), SIGNAL(changed()) ); + connect( lePrefix, &QLineEdit::textChanged, + this, &KateKonsoleConfigPage::changed ); connect(cbSetEditor, &QCheckBox::stateChanged, this, &KateKonsoleConfigPage::changed); } +void KateKonsoleConfigPage::slotEnableRunWarning () +{ + KMessageBox::enableMessage(QStringLiteral("Konsole: Run in Terminal Warning")); +} + QString KateKonsoleConfigPage::name() const { return i18n("Terminal"); @@ -369,6 +479,8 @@ { KConfigGroup config(KSharedConfig::openConfig(), "Konsole"); config.writeEntry("AutoSyncronize", cbAutoSyncronize->isChecked()); + config.writeEntry("RemoveExtension", cbRemoveExtension->isChecked()); + config.writeEntry("RunPrefix", lePrefix->text()); config.writeEntry("SetEditor", cbSetEditor->isChecked()); config.sync(); mPlugin->readConfig(); @@ -378,6 +490,8 @@ { KConfigGroup config(KSharedConfig::openConfig(), "Konsole"); cbAutoSyncronize->setChecked(config.readEntry("AutoSyncronize", false)); + cbRemoveExtension->setChecked(config.readEntry("RemoveExtension", false)); + lePrefix->setText(config.readEntry("RunPrefix", "")); cbSetEditor->setChecked(config.readEntry("SetEditor", false)); } diff --git a/addons/konsole/ui.rc b/addons/konsole/ui.rc --- a/addons/konsole/ui.rc +++ b/addons/konsole/ui.rc @@ -6,6 +6,7 @@ &Tools +