[education/rkward] /: Allow for "spontaneous" output not associated with any command running in the frontend.

Thomas Friedrichsmeier null at kde.org
Mon Apr 4 16:08:26 BST 2022


Git commit 20597220bca726227a380d6ac245b00bfb57d525 by Thomas Friedrichsmeier.
Committed on 04/04/2022 at 15:07.
Pushed by tfry into branch 'master'.

Allow for "spontaneous" output not associated with any command running in the frontend.

Needed for R's enhanced help system.

M  +1    -0    ChangeLog
M  +4    -1    rkward/rbackend/rkbackendtransmitter.cpp
M  +1    -1    rkward/rbackend/rkrbackendprotocol_shared.h
M  +2    -1    rkward/rbackend/rkrinterface.cpp
M  +24   -6    rkward/rkconsole.cpp
M  +2    -1    rkward/rkconsole.h

https://invent.kde.org/education/rkward/commit/20597220bca726227a380d6ac245b00bfb57d525

diff --git a/ChangeLog b/ChangeLog
index 8bedaad1..89d021cd 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,5 @@
 --- Version 0.7.3 - UNRELEASED
+- "Spontaneous" output, such as from running examples in the enhanced help system in R 4.2.0, is shown in the R Console
 - Fix compilation with the upcoming R 4.2.0
 - Support for switching color schemes, including basic support for dark mode
 - Fix crash in dev.capture()
diff --git a/rkward/rbackend/rkbackendtransmitter.cpp b/rkward/rbackend/rkbackendtransmitter.cpp
index 3ce13503..6a10aab0 100644
--- a/rkward/rbackend/rkbackendtransmitter.cpp
+++ b/rkward/rbackend/rkbackendtransmitter.cpp
@@ -137,7 +137,10 @@ void RKRBackendTransmitter::requestReceived (RBackendRequest* request) {
 }
 
 void RKRBackendTransmitter::flushOutput (bool force) {
-	if (!current_sync_requests.isEmpty ()) return;
+	for (int i = 0; i < current_sync_requests.size(); ++i) {
+		// Apparently, frontend isn't keeping up. Don't push the next piece of output, until it has processed the previous one!
+		if (current_sync_requests.at(i)->type == RBackendRequest::RCallbackType::Output) return;
+	}
 
 	RKRBackend::this_pointer->fetchStdoutStderr (force);
 	ROutputList out = RKRBackend::this_pointer->flushOutput (force);
diff --git a/rkward/rbackend/rkrbackendprotocol_shared.h b/rkward/rbackend/rkrbackendprotocol_shared.h
index c2399960..e446f74c 100644
--- a/rkward/rbackend/rkrbackendprotocol_shared.h
+++ b/rkward/rbackend/rkrbackendprotocol_shared.h
@@ -44,7 +44,7 @@ public:
 		ChooseFile,
 		EditFiles,
 		ReadLine,      // 5
-		CommandOut,
+		CommandOut,                  /**< Request the next command, and notify about the result of the previus. TODO split. */
 		Started,
 		EvalRequest,
 		CallbackRequest,
diff --git a/rkward/rbackend/rkrinterface.cpp b/rkward/rbackend/rkrinterface.cpp
index 2b5427d2..3e8bc17b 100644
--- a/rkward/rbackend/rkrinterface.cpp
+++ b/rkward/rbackend/rkrinterface.cpp
@@ -450,7 +450,8 @@ void RInterface::flushOutput (bool forced) {
 
 	foreach (ROutput *output, list) {
 		if (all_current_commands.isEmpty ()) {
-			RK_DEBUG (RBACKEND, DL_WARNING, "output without receiver'%s'", qPrintable (output->output));
+			RK_DEBUG (RBACKEND, DL_DEBUG, "output without receiver'%s'", qPrintable (output->output));
+			RKConsole::mainConsole()->insertSpontaneousROutput(output);
 			delete output;
 			continue;	// to delete the other output pointers, too
 		} else {
diff --git a/rkward/rkconsole.cpp b/rkward/rkconsole.cpp
index 34b0fadb..44727e0b 100644
--- a/rkward/rkconsole.cpp
+++ b/rkward/rkconsole.cpp
@@ -2,7 +2,7 @@
                           rkconsole  -  description
                              -------------------
     begin                : Thu Aug 19 2004
-    copyright            : (C) 2004-2020 by Thomas Friedrichsmeier
+    copyright            : (C) 2004-2022 by Thomas Friedrichsmeier
     email                : thomas.friedrichsmeier at kdemail.net
  ***************************************************************************/
 
@@ -541,10 +541,17 @@ void RKConsole::rCommandDone (RCommand *command) {
 	tryNextInBuffer ();
 }
 
-void RKConsole::newOutput (RCommand *, ROutput *output) {
+void RKConsole::newOutput (RCommand *command, ROutput *output) {
 	RK_TRACE (APP);
 
-	int start_line = doc->lines () -1;
+	int first_line = doc->lines () -1;
+	QString popped_line;
+	if (!command) {
+		// spontanteous R output, to be inserted _above_ the current command
+		// as a shortcut, we pop the last line, and reinsert in, later
+		popped_line = doc->line(doc->lines() - 1);
+		doc->removeLine(doc->lines() - 1);
+	}
 
 	// split by and handle carriage returns
 	const QString outstr = output->output;
@@ -567,11 +574,11 @@ void RKConsole::newOutput (RCommand *, ROutput *output) {
 	if (start_pos <= end_pos) doc->insertText (doc->documentEnd (), outstr.mid (start_pos, end_pos - start_pos + 1));
 
 	int end_line = doc->lines () -1;
-	if (output->type != ROutput::Output) {
+	if (output->type != ROutput::Output || (!command)) {
 		KTextEditor::MarkInterface *markiface = qobject_cast<KTextEditor::MarkInterface*> (doc);
 		RK_ASSERT (markiface);
-		for (int line = start_line; line < end_line; ++line) {
-			markiface->addMark (line, KTextEditor::MarkInterface::BreakpointActive);
+		for (int line = first_line; line < end_line; ++line) {
+			markiface->addMark (line, command ? KTextEditor::MarkInterface::BreakpointActive : KTextEditor::MarkInterface::BreakpointDisabled);
 		}
 	}
 
@@ -585,6 +592,11 @@ void RKConsole::newOutput (RCommand *, ROutput *output) {
 			view->setUpdatesEnabled (true);
 		}
 	}
+
+	if (!command) {
+		doc->insertLine(doc->lines(), popped_line);
+	}
+
 	cursorAtTheEnd ();
 }
 
@@ -906,6 +918,12 @@ void RKConsole::pipeCommandThroughConsoleLocal (const QString &command_string) {
 	previous_chunk_was_piped = true;
 }
 
+void RKConsole::insertSpontaneousROutput(ROutput* output) {
+	RK_TRACE (APP);
+	RK_ASSERT(!current_command);
+	newOutput(nullptr, output);
+}
+
 void RKConsole::contextMenuEvent (QContextMenuEvent * event) {
 	RK_TRACE (APP);
 
diff --git a/rkward/rkconsole.h b/rkward/rkconsole.h
index 9d65cc0b..b501c4f5 100644
--- a/rkward/rkconsole.h
+++ b/rkward/rkconsole.h
@@ -2,7 +2,7 @@
                           rkconsole  -  description
                              -------------------
     begin                : Thu Aug 19 2004
-    copyright            : (C) 2004-2020 by Thomas Friedrichsmeier
+    copyright            : (C) 2004-2022 by Thomas Friedrichsmeier
     email                : thomas.friedrichsmeier at kdemail.net
  ***************************************************************************/
 
@@ -75,6 +75,7 @@ public:
 	void setCommandHistory (const QStringList &new_history, bool append);
 	QStringList commandHistory () const { return commands_history.getHistory (); };
 	void addCommandToHistory (const QString& text) { commands_history.append (text); };
+	void insertSpontaneousROutput(ROutput *output);
 protected:
 /** Handle keystrokes before they reach the kate-part. Return TRUE if we want the kate-part to ignore it
 \param e the QKeyEvent */


More information about the rkward-tracker mailing list