[rkward-cvs] SF.net SVN: rkward:[3552] trunk/rkward

tfry at users.sourceforge.net tfry at users.sourceforge.net
Thu May 12 15:49:51 UTC 2011


Revision: 3552
          http://rkward.svn.sourceforge.net/rkward/?rev=3552&view=rev
Author:   tfry
Date:     2011-05-12 15:49:51 +0000 (Thu, 12 May 2011)

Log Message:
-----------
Also add an action to send already existing lines to the output.

Modified Paths:
--------------
    trunk/rkward/ChangeLog
    trunk/rkward/rkward/misc/rkstandardactions.cpp
    trunk/rkward/rkward/misc/rkstandardactions.h
    trunk/rkward/rkward/rkconsole.cpp
    trunk/rkward/rkward/rkconsole.h
    trunk/rkward/rkward/rkconsolepart.rc
    trunk/rkward/rkward/windows/rkcommandeditorwindow.cpp
    trunk/rkward/rkward/windows/rkcommandeditorwindow.h
    trunk/rkward/rkward/windows/rkstandardactions.rc

Modified: trunk/rkward/ChangeLog
===================================================================
--- trunk/rkward/ChangeLog	2011-05-12 14:05:38 UTC (rev 3551)
+++ trunk/rkward/ChangeLog	2011-05-12 15:49:51 UTC (rev 3552)
@@ -1,4 +1,5 @@
 - Fix compilation on FreeBSD
+- Add "Copy lines to output" action to copy highlighted lines from the R console or script editors to the output window
 - R commands and their output can be "carbon copied" to the output window
 - On Windows, RKWard will detect, and offer to disable "native" file system dialogs
 - Object browsers that show only the global environment now hide the ".GlobalEnv" item

Modified: trunk/rkward/rkward/misc/rkstandardactions.cpp
===================================================================
--- trunk/rkward/rkward/misc/rkstandardactions.cpp	2011-05-12 14:05:38 UTC (rev 3551)
+++ trunk/rkward/rkward/misc/rkstandardactions.cpp	2011-05-12 15:49:51 UTC (rev 3552)
@@ -2,7 +2,7 @@
                           rkstandardactions  -  description
                              -------------------
     begin                : Sun Nov 18 2007
-    copyright            : (C) 2007, 2009, 2010 by Thomas Friedrichsmeier
+    copyright            : (C) 2007, 2009, 2010, 2011 by Thomas Friedrichsmeier
     email                : tfry at users.sourceforge.net
  ***************************************************************************/
 
@@ -27,6 +27,14 @@
 
 #include "../debug.h"
 
+KAction* RKStandardActions::copyLinesToOutput (RKMDIWindow *window, const QObject *receiver, const char *member) {
+	RK_TRACE (MISC);
+
+	KAction* ret = window->standardActionCollection ()->addAction ("copy_lines_to_output", receiver, member);
+	ret->setText (i18n ("Copy lines to output"));
+	return ret;
+}
+
 KAction* RKStandardActions::pasteSpecial (RKMDIWindow *window, const QObject *receiver, const char *member) {
 	RK_TRACE (MISC);
 

Modified: trunk/rkward/rkward/misc/rkstandardactions.h
===================================================================
--- trunk/rkward/rkward/misc/rkstandardactions.h	2011-05-12 14:05:38 UTC (rev 3551)
+++ trunk/rkward/rkward/misc/rkstandardactions.h	2011-05-12 15:49:51 UTC (rev 3552)
@@ -2,7 +2,7 @@
                           rkstandardactions  -  description
                              -------------------
     begin                : Sun Nov 18 2007
-    copyright            : (C) 2007, 2009, 2010 by Thomas Friedrichsmeier
+    copyright            : (C) 2007, 2009, 2010, 2011 by Thomas Friedrichsmeier
     email                : tfry at users.sourceforge.net
  ***************************************************************************/
 
@@ -27,6 +27,7 @@
 
 @author Thomas Friedrichsmeier */
 namespace RKStandardActions {
+	KAction *copyLinesToOutput (RKMDIWindow *window, const QObject *receiver=0, const char *member=0);
 /** Allows special pasting modes for script windows.
 @param member needs to have the signature void fun (const QString&). */
 	KAction* pasteSpecial (RKMDIWindow *window, const QObject *receiver=0, const char *member=0);

Modified: trunk/rkward/rkward/rkconsole.cpp
===================================================================
--- trunk/rkward/rkward/rkconsole.cpp	2011-05-12 14:05:38 UTC (rev 3551)
+++ trunk/rkward/rkward/rkconsole.cpp	2011-05-12 15:49:51 UTC (rev 3552)
@@ -125,7 +125,7 @@
 // KDE4: a way to do this?
 //	doc->setUndoSteps (0);
 	clear ();
-	doc->setHighlightingMode ("R interactive session");
+	RKCommandHighlighter::setHighlighting (doc, RKCommandHighlighter::RInteractiveSession);
 
 	commands_history = RKSettingsModuleConsole::loadCommandHistory ();
 	commands_history_position = commands_history.constEnd ();
@@ -871,6 +871,12 @@
 	pipeUserCommand (cleanedSelection ());
 }
 
+void RKConsole::copyLinesToOutput () {
+	RK_TRACE (APP);
+
+	RKCommandHighlighter::copyLinesToOutput (view, RKCommandHighlighter::RInteractiveSession);
+}
+
 void RKConsole::showContextHelp () {
 	RK_TRACE (APP);
 	RKHelpSearchWindow::mainHelpSearch ()->getContextHelp (currentEditingLine (), currentCursorPositionInCommand ());
@@ -879,8 +885,8 @@
 void RKConsole::initializeActions (KActionCollection *ac) {
 	RK_TRACE (APP);
 
+	RKStandardActions::copyLinesToOutput (this, this, SLOT (copyLinesToOutput()));
 	context_help_action = RKStandardActions::functionHelp (this, this, SLOT(showContextHelp()));
-
 	run_selection_action = RKStandardActions::runSelection (this, this, SLOT (runSelection()));
 
 	interrupt_command_action = ac->addAction ("interrupt", this, SLOT (resetConsole()));

Modified: trunk/rkward/rkward/rkconsole.h
===================================================================
--- trunk/rkward/rkward/rkconsole.h	2011-05-12 14:05:38 UTC (rev 3551)
+++ trunk/rkward/rkward/rkconsole.h	2011-05-12 15:49:51 UTC (rev 3552)
@@ -143,6 +143,7 @@
 
 	bool tab_key_pressed_before;
 
+	KAction* copy_lines_to_output_action;
 	KAction* context_help_action;
 	KAction* run_selection_action;
 	KAction* interrupt_command_action;
@@ -171,6 +172,7 @@
 /** Cancels the current command, if any, and clears the command buffer(s) */
 	void resetConsole ();
 	void runSelection ();
+	void copyLinesToOutput ();
 
 /** Adds a chunk of commands to the input buffer
 \param batch a QString containing the batch of commands to be executed */

Modified: trunk/rkward/rkward/rkconsolepart.rc
===================================================================
--- trunk/rkward/rkward/rkconsolepart.rc	2011-05-12 14:05:38 UTC (rev 3551)
+++ trunk/rkward/rkward/rkconsolepart.rc	2011-05-12 15:49:51 UTC (rev 3552)
@@ -1,5 +1,5 @@
 <!DOCTYPE kpartgui>
-<kpartgui name="rkward_console" version="540">
+<kpartgui name="rkward_console" version="560">
 	<MenuBar>
 		<Menu name="file"><text>&File</text>
 			<Separator />
@@ -42,6 +42,7 @@
 	<Menu name="rkconsole_context_menu">
 		<Action name="rkconsole_copy"/>
 		<Action name="rkconsole_copy_literal"/>
+		<DefineGroup name="rkconsole_context_merge_copy" />
 		<Action name="rkconsole_paste"/>
 		<DefineGroup name="rkconsole_context_merge_paste" />
 		<Separator/>

Modified: trunk/rkward/rkward/windows/rkcommandeditorwindow.cpp
===================================================================
--- trunk/rkward/rkward/windows/rkcommandeditorwindow.cpp	2011-05-12 14:05:38 UTC (rev 3551)
+++ trunk/rkward/rkward/windows/rkcommandeditorwindow.cpp	2011-05-12 15:49:51 UTC (rev 3552)
@@ -56,6 +56,7 @@
 #include "../misc/rkxmlguisyncer.h"
 #include "../misc/rkjobsequence.h"
 #include "../core/robjectlist.h"
+#include "../rbackend/rinterface.h"
 #include "../settings/rksettings.h"
 #include "../settings/rksettingsmodulecommandeditor.h"
 #include "../rkconsole.h"
@@ -80,13 +81,6 @@
 #define GET_HELP_URL 1
 #define NUM_BLOCK_RECORDS 6
 
-/** set syntax highlighting-mode to R syntax. Outside of class, in order to allow use from the on demand code highlighter */
-void setRHighlighting (KTextEditor::Document *doc) {
-	RK_TRACE (COMMANDEDITOR);
-
-	if (!doc->setHighlightingMode("R Script")) RK_DO (qDebug ("R syntax highlighting defintion not found!"), COMMANDEDITOR, DL_ERROR);
-}
-
 RKCommandEditorWindow::RKCommandEditorWindow (QWidget *parent, bool use_r_highlighting) : RKMDIWindow (parent, RKMDIWindow::CommandEditorWindow) {
 	RK_TRACE (COMMANDEDITOR);
 
@@ -135,7 +129,7 @@
 	cc_iface = 0;
 	hinter = 0;
 	if (use_r_highlighting) {
-		setRHighlighting (m_doc);
+		RKCommandHighlighter::setHighlighting (m_doc, RKCommandHighlighter::RScript);
 		cc_iface = qobject_cast<KTextEditor::CodeCompletionInterface*> (m_view);
 		if (cc_iface) {
 			cc_iface->setAutomaticInvocationEnabled (true);
@@ -192,6 +186,7 @@
 void RKCommandEditorWindow::initializeActions (KActionCollection* ac) {
 	RK_TRACE (COMMANDEDITOR);
 
+	RKStandardActions::copyLinesToOutput (this, this, SLOT (copyLinesToOutput()));
 	RKStandardActions::pasteSpecial (this, this, SLOT (paste(const QString&)));
 
 	action_run_all = RKStandardActions::runAll (this, this, SLOT (runAll()));
@@ -349,7 +344,7 @@
 	// encoding must be set *before* loading the file
 	if (!encoding.isEmpty ()) m_doc->setEncoding (encoding);
 	if (m_doc->openUrl (url)){
-		if (use_r_highlighting) setRHighlighting (m_doc);
+		if (use_r_highlighting) RKCommandHighlighter::setHighlighting (m_doc, RKCommandHighlighter::RScript);
 		setReadOnly (read_only);
 
 		updateCaption ();
@@ -628,7 +623,12 @@
 	m_view->setCursorPosition (c);
 }
 
+void RKCommandEditorWindow::copyLinesToOutput () {
+	RK_TRACE (COMMANDEDITOR);
 
+	RKCommandHighlighter::copyLinesToOutput (m_view, RKCommandHighlighter::RScript);
+}
+
 void RKCommandEditorWindow::runAll () {
 	RK_TRACE (COMMANDEDITOR);
 
@@ -1017,7 +1017,6 @@
 	QWidget* view = _doc->createView (0);
 	view->hide ();
 	RK_ASSERT (_doc);
-	setRHighlighting (_doc);
 	return _doc;
 }
 
@@ -1067,20 +1066,22 @@
 	return ret;
 }
 
-QString RKCommandHighlighter::commandToHTML (const QString r_command) {
+QString RKCommandHighlighter::commandToHTML (const QString r_command, HighlightingMode mode) {
 	KTextEditor::Document* doc = getDoc ();
 	KTextEditor::HighlightInterface *iface = qobject_cast<KTextEditor::HighlightInterface*> (_doc);
 	RK_ASSERT (iface);
 	if (!iface) return (QString ("<pre>") + r_command + "</pre>");
 
 	doc->setText (r_command);
-	setRHighlighting (doc);
+	setHighlighting (doc, mode);
 	QString ret;
+
+	QString opening;
 	KTextEditor::Attribute::Ptr m_defaultAttribute = iface->defaultStyle(KTextEditor::HighlightInterface::dsNormal);
 	if ( !m_defaultAttribute ) {
-		ret = "<pre class=\"code\">";
+		opening = "<pre class=\"%5\">";
 	} else {
-		ret = QString("<pre style='%1%2%3%4' class=\"code\">")
+		opening = QString("<pre style='%1%2%3%4' class=\"%5\">")
 				.arg(m_defaultAttribute->fontBold() ? "font-weight:bold;" : "")
 				.arg(m_defaultAttribute->fontItalic() ? "text-style:italic;" : "")
 				.arg("color:" + m_defaultAttribute->foreground().color().name() + ';');
@@ -1089,9 +1090,30 @@
 
 	const KTextEditor::Attribute::Ptr noAttrib(0);
 
+	if (mode == RScript) ret = opening.arg ("code");
+	enum {
+		Command,
+		Output,
+		None
+	} previous_chunk = None;
 	for (int i = 0; i < doc->lines (); ++i)
 	{
 		const QString &line = doc->line(i);
+		if (mode == RInteractiveSession) {
+			if (line.startsWith (">") || line.startsWith ("+")) {
+				if (previous_chunk != Command) {
+					if (previous_chunk != None) ret.append ("</pre>");
+					ret.append (opening.arg ("code"));
+					previous_chunk = Command;
+				}
+			} else {
+				if (previous_chunk != Output) {
+					if (previous_chunk != None) ret.append ("</pre>");
+					ret.append (opening.arg ("output_normal"));
+					previous_chunk = Output;
+				}
+			}
+		}
 
 		QList<KTextEditor::HighlightInterface::AttributeBlock> attribs = iface->lineAttributes(i);
 
@@ -1121,9 +1143,40 @@
 }
 
 #else	// KDE < 4.4: No Highlighting Interface
-QString RKCommandHighlighter::commandToHTML (const QString r_command) {
+QString RKCommandHighlighter::commandToHTML (const QString r_command, HighlightingMode) {
 	return (QString ("<pre class=\"code\">") + r_command + "</pre>");
 }
 #endif
 
+/** set syntax highlighting-mode to R syntax. Outside of class, in order to allow use from the on demand code highlighter */
+void RKCommandHighlighter::setHighlighting (KTextEditor::Document *doc, HighlightingMode mode) {
+	RK_TRACE (COMMANDEDITOR);
+
+	QString mode_string = "R Script";
+	if (mode == RInteractiveSession) mode_string = "R interactive session";
+	if (!doc->setHighlightingMode (mode_string)) RK_DO (qDebug ("R syntax highlighting defintion ('%s')not found!", qPrintable (mode_string)), COMMANDEDITOR, DL_ERROR);
+}
+
+void RKCommandHighlighter::copyLinesToOutput (KTextEditor::View *view, HighlightingMode mode) {
+	RK_TRACE (COMMANDEDITOR);
+
+	// expand selection to full lines (or current line)
+	KTextEditor::Document *doc = view->document ();
+	KTextEditor::Range sel = view->selectionRange ();
+	if (!sel.isValid ()) {
+		KTextEditor::Cursor pos = view->cursorPosition ();
+		sel.setRange (KTextEditor::Cursor (pos.line (), 0),
+					  KTextEditor::Cursor (pos.line (), doc->lineLength (pos.line ())));
+	} else {
+		sel.setRange (KTextEditor::Cursor (sel.start ().line (), 0),
+					  KTextEditor::Cursor (sel.end ().line (), doc->lineLength (sel.end ().line ())));
+	}
+
+	// highlight and submit
+	QString highlighted = commandToHTML (doc->text (sel), mode);
+	if (!highlighted.isEmpty ()) {
+		RKGlobals::rInterface ()->issueCommand (".rk.cat.output (" + RObject::rQuote (highlighted) + ")\n", RCommand::App | RCommand::Silent);
+	}
+}
+
 #include "rkcommandeditorwindow.moc"

Modified: trunk/rkward/rkward/windows/rkcommandeditorwindow.h
===================================================================
--- trunk/rkward/rkward/windows/rkcommandeditorwindow.h	2011-05-12 14:05:38 UTC (rev 3551)
+++ trunk/rkward/rkward/windows/rkcommandeditorwindow.h	2011-05-12 15:49:51 UTC (rev 3552)
@@ -205,6 +205,7 @@
 	void runLine ();
 /** run the entire script */
 	void runAll ();
+	void copyLinesToOutput ();
 
 /** selection has changed. Enable / disable actions accordingly */
 	void selectionChanged (KTextEditor::View* view);
@@ -293,7 +294,13 @@
 /** Simple class to provide HTML highlighting for arbitrary R code. */
 class RKCommandHighlighter {
 public:
-	static QString commandToHTML (const QString r_command);
+	enum HighlightingMode {
+		RInteractiveSession,
+		RScript
+	};
+	static void copyLinesToOutput (KTextEditor::View *view, HighlightingMode mode);
+	static void setHighlighting (KTextEditor::Document *doc, HighlightingMode mode);
+	static QString commandToHTML (const QString r_command, HighlightingMode mode=RScript);
 private:
 	static KTextEditor::Document* getDoc ();
 	static KTextEditor::Document* _doc;

Modified: trunk/rkward/rkward/windows/rkstandardactions.rc
===================================================================
--- trunk/rkward/rkward/windows/rkstandardactions.rc	2011-05-12 14:05:38 UTC (rev 3551)
+++ trunk/rkward/rkward/windows/rkstandardactions.rc	2011-05-12 15:49:51 UTC (rev 3552)
@@ -1,9 +1,12 @@
 <!DOCTYPE kpartgui>
-<kpartgui name="rkward_standardactions" version="54">
+<kpartgui name="rkward_standardactions" version="560">
 <!-- What is this for, wouldn't it be easier to just inline these actions in the parts where they
      end up? Yes, but then they would be separate actions, internally, and each could be assigned
      a shortcut of its own. This is needed to tie them all together. -->
 	<MenuBar>
+		<Menu name="file">
+			<Action name="copy_lines_to_output"/>
+		</Menu>
 		<Menu name="edit">
 			<Action name="paste_special" group="after_edit_paste_merge"/>
 		</Menu>
@@ -43,6 +46,7 @@
 		<Action name="window_configure" group="ktexteditor_popup_merge2"/>
 	</Menu>
 	<Menu name="rkconsole_context_menu">
+		<Action name="copy_lines_to_output" group="rkconsole_context_merge_copy"/>
 		<Action name="paste_special" group="rkconsole_context_merge_paste"/>
 		<Action name="run_selection" group="rkconsole_context_merge1"/>
 		<Action name="function_reference" group="rkconsole_context_merge2"/>


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the rkward-tracker mailing list