Index: addons/konsole/kateconsole.h =================================================================== --- addons/konsole/kateconsole.h +++ addons/konsole/kateconsole.h @@ -137,10 +137,16 @@ * synchronize the konsole with the current document (cd to the directory) */ void slotSync(KTextEditor::View *view = 0); + /** * 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: /** @@ -208,8 +214,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; Index: addons/konsole/kateconsole.cpp =================================================================== --- addons/konsole/kateconsole.cpp +++ addons/konsole/kateconsole.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -39,6 +40,9 @@ #include #include #include +#include +#include +#include #include #include @@ -136,6 +140,10 @@ a = actionCollection()->addAction(QStringLiteral("katekonsole_tools_sync")); a->setText(i18nc("@action", "S&ynchronize Terminal with Current Document")); connect(a, SIGNAL(triggered()), this, SLOT(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"))); @@ -293,6 +301,61 @@ 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.isValid()) { + KMessageBox::sorry(0, i18n ("Invalid document URL")); + return; + } + 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(0); + 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")); @@ -340,6 +403,39 @@ 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); @@ -348,9 +444,18 @@ reset(); lo->addStretch(); connect( cbAutoSyncronize, SIGNAL(stateChanged(int)), SIGNAL(changed()) ); + connect( cbRemoveExtension, SIGNAL(stateChanged(int)), SIGNAL(changed()) ); + connect( lePrefix, &QLineEdit::textChanged, + this, &KateKonsoleConfigPage::changed ); connect( cbSetEditor, SIGNAL(stateChanged(int)), SIGNAL(changed()) ); } +void KateKonsoleConfigPage::slotEnableRunWarning () +{ + KMessageBox::enableMessage(QStringLiteral("Konsole: Run in Terminal Warning")); +} + + QString KateKonsoleConfigPage::name() const { return i18n("Terminal"); @@ -370,6 +475,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(); @@ -379,6 +486,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)); } Index: addons/konsole/ui.rc =================================================================== --- addons/konsole/ui.rc +++ addons/konsole/ui.rc @@ -6,6 +6,7 @@ &Tools +