& )
+ : KTextEditor::Plugin(parent)
+{
+}
+
+PluginKateOpenSelection::~PluginKateOpenSelection()
+{
+}
+
+QObject *PluginKateOpenSelection::createView (KTextEditor::MainWindow *mainWindow)
+{
+ return new PluginViewKateOpenSelection(this,mainWindow);
+}
+
+static bool isValidChar(QChar c)
+{
+ return !c.isSpace() && c != QLatin1Char('"') && c != QLatin1Char('\'');
+}
+
+void PluginKateOpenSelection::slotOpenSelection()
+{
+ KTextEditor::Application *application = KTextEditor::Editor::instance()->application();
+ if (!application->activeMainWindow())
+ return;
+
+ KTextEditor::View* editView = application->activeMainWindow()->activeView();
+ if (editView) {
+ QString selection;
+
+ // Selected text
+ if (editView->selection()) {
+ selection = editView->selectionText();
+ selection = selection.trimmed();
+ }
+
+ // Try to extract path under cursor if no selected text
+ if (selection.isEmpty()) {
+ const KTextEditor::Cursor &cursor = editView->cursorPosition();
+ const QString line = editView->document()->line(cursor.line());
+ const int lineLength = line.size();
+ if (lineLength > 0) {
+ const int col = cursor.column();
+ int start = col, end = col;
+
+ // Stop at first white space or quote (for path with space, user should select the text)
+ while (start > 0 && isValidChar(line.at(start-1))) {
+ --start;
+ }
+ while (end+1 < lineLength && isValidChar(line.at(end+1))) {
+ ++end;
+ }
+ selection = line.mid(start,end-start+1);
+ }
+ }
+
+ // Relative file path : set relative to current document path
+ if (QDir::isRelativePath(selection)) {
+ const QUrl currentUrl = editView->document()->url();
+ if (currentUrl.isValid() && !currentUrl.isEmpty()) {
+ const QDir currentDir(QFileInfo(currentUrl.path()).absolutePath());
+ selection = currentDir.absoluteFilePath(selection);
+ }
+ }
+
+ // Open the file
+ if (!selection.isEmpty() && !selection.contains(QLatin1Char('\n'))) {
+ const QUrl url = QUrl::fromLocalFile(selection);
+ if (fileExists(url)) {
+ application->activeMainWindow()->openUrl(url);
+ }
+ }
+ }
+}
+
+bool PluginKateOpenSelection::fileExists(const QUrl &url)
+{
+ if (url.isLocalFile()) {
+ return QFile::exists(url.toLocalFile());
+ }
+ KIO::JobFlags flags = KIO::DefaultFlags;
+ KIO::StatJob *job = KIO::stat(url, flags);
+ KJobWidgets::setWindow(job, KTextEditor::Editor::instance()->application()->activeMainWindow()->window());
+ job->setSide(KIO::StatJob::DestinationSide);
+ job->exec();
+ return !job->error();
+}
+
+bool PluginViewKateOpenSelection::exec(KTextEditor::View *view, const QString &cmd, QString &msg, const KTextEditor::Range &)
+{
+ Q_UNUSED(view)
+ Q_UNUSED(cmd)
+ Q_UNUSED(msg)
+
+ m_plugin->slotOpenSelection();
+ return true;
+}
+
+bool PluginViewKateOpenSelection::help(KTextEditor::View *view, const QString &cmd, QString &msg)
+{
+ Q_UNUSED(view)
+ Q_UNUSED(cmd)
+
+ msg = i18n("open-selection — open the selected path
"
+ "usage: open-selection
"
+ "This command will open the file under the cursor (path in selection or auto-detection)
");
+
+ return true;
+}
+
+#include "plugin_kateopenselection.moc"
diff --git a/addons/openselection/rc.cpp b/addons/openselection/rc.cpp
new file mode 100644
diff --git a/addons/openselection/ui.rc b/addons/openselection/ui.rc
new file mode 100644
--- /dev/null
+++ b/addons/openselection/ui.rc
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
diff --git a/doc/kate/plugins.docbook b/doc/kate/plugins.docbook
--- a/doc/kate/plugins.docbook
+++ b/doc/kate/plugins.docbook
@@ -84,6 +84,10 @@
- Opens the corresponding .h/[.cpp|.c] file
+Open Selection
+- Opens the selected path
+
+
Project Plugin - Integration with Git and other source control systems
@@ -1510,6 +1514,44 @@
+
+
+Open Selection Plugin
+
+
+Using the Open Selection Plugin
+
+This command help to open files with path in a file
+
+For example, if you have a list of path in a file (address file, includes, ...), select or place the cursor in one of them, then trigger the plugin using the shortcut. The plugin will open the file
+
+
+
+
+
+
+
+