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

tfry at users.sourceforge.net tfry at users.sourceforge.net
Fri May 13 08:28:49 UTC 2011


Revision: 3553
          http://rkward.svn.sourceforge.net/rkward/?rev=3553&view=rev
Author:   tfry
Date:     2011-05-13 08:28:49 +0000 (Fri, 13 May 2011)

Log Message:
-----------
Replace the non-literal copy action with a smarter variant, which also omits intermittent output lines.

Modified Paths:
--------------
    trunk/rkward/ChangeLog
    trunk/rkward/rkward/rkconsole.cpp
    trunk/rkward/rkward/rkconsole.h
    trunk/rkward/rkward/rkconsolepart.rc

Modified: trunk/rkward/ChangeLog
===================================================================
--- trunk/rkward/ChangeLog	2011-05-12 15:49:51 UTC (rev 3552)
+++ trunk/rkward/ChangeLog	2011-05-13 08:28:49 UTC (rev 3553)
@@ -1,4 +1,5 @@
 - Fix compilation on FreeBSD
+- Replace the R console's "Copy selection cleaned" action with "Copy commands, only", which will omit any non-command regions in the copy
 - 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

Modified: trunk/rkward/rkward/rkconsole.cpp
===================================================================
--- trunk/rkward/rkward/rkconsole.cpp	2011-05-12 15:49:51 UTC (rev 3552)
+++ trunk/rkward/rkward/rkconsole.cpp	2011-05-13 08:28:49 UTC (rev 3553)
@@ -816,23 +816,44 @@
 	KIO::NetAccess::upload (tempfile.fileName (), url, this);
 }
 
-QString RKConsole::cleanedSelection () {
+QString RKConsole::cleanSelection (const QString &origin) {
 	RK_TRACE (APP);
 
-	QString ret = view->selectionText ();
-	ret.replace ('\n' + QString (nprefix), "\n");
-	ret.replace ('\n' + QString (iprefix), "\n");
-	if (ret.startsWith (nprefix)) {
-		ret = ret.mid (QString (nprefix).length ());
+	QString ret;
+	ret.reserve (origin.length ());
+	QStringList lines = origin.split ("\n");
+	foreach (QString line, lines) {
+		if (line.startsWith (nprefix)) {
+			ret.append (line.mid (nprefix.length ()));
+		} else if (line.startsWith (iprefix)) {
+			ret.append (line.mid (iprefix.length ()));
+		} else {
+			ret.append (line);
+		}
+		ret.append ('\n');
 	}
 
 	return ret;
 }
 
-void RKConsole::copy () {
+void RKConsole::copyCommands () {
 	RK_TRACE (APP);
 
-	QApplication::clipboard()->setText (cleanedSelection ());
+	KTextEditor::Range sel = view->selectionRange ();
+	if (!sel.isValid ()) return;
+
+	// we use this somewhat cumbersome (and inefficient) approach as it should also be correct in case of blockwise selections
+	QStringList command_lines = view->selectionText ().split ("\n");
+	int i = 0;
+	for (int l = sel.start ().line (); l <= sel.end ().line (); ++l) {
+		QString line = doc->line (l);
+		if (!(line.startsWith (iprefix) || line.startsWith (nprefix))) {
+			command_lines.removeAt (i);
+			--i;
+		}
+		++i;
+	}
+	QApplication::clipboard()->setText (cleanSelection (command_lines.join ("\n")));
 }
 
 void RKConsole::literalCopy () {
@@ -868,7 +889,7 @@
 void RKConsole::runSelection () {
 	RK_TRACE (APP);
 
-	pipeUserCommand (cleanedSelection ());
+	pipeUserCommand (cleanSelection (view->selectionText ()));
 }
 
 void RKConsole::copyLinesToOutput () {
@@ -895,12 +916,12 @@
 	interrupt_command_action->setIcon (RKStandardIcons::getIcon (RKStandardIcons::ActionInterrupt));
 	interrupt_command_action->setEnabled (false);
 
-	copy_action = ac->addAction ("rkconsole_copy", this, SLOT (copy()));
-	copy_action->setText (i18n ("Copy selection cleaned"));
-
 	copy_literal_action = ac->addAction ("rkconsole_copy_literal", this, SLOT (literalCopy()));
 	copy_literal_action->setText (i18n ("Copy selection literally"));
 
+	copy_commands_action = ac->addAction ("rkconsole_copy_commands", this, SLOT (copyCommands()));
+	copy_commands_action->setText (i18n ("Copy commands, only"));
+
 	RKStandardActions::pasteSpecial (this, this, SLOT (submitBatch(const QString&)));
 
 	ac->addAction (KStandardAction::Clear, "rkconsole_clear", this, SLOT (clear()));
@@ -957,7 +978,7 @@
 void RKConsole::contextMenuEvent (QContextMenuEvent * event) {
 	RK_TRACE (APP);
 
-	copy_action->setEnabled (view->selection ());
+	copy_commands_action->setEnabled (view->selection ());
 	copy_literal_action->setEnabled (view->selection ());
 	run_selection_action->setEnabled (view->selection ());
 
@@ -965,7 +986,7 @@
 
 	run_selection_action->setEnabled (true);
 	copy_literal_action->setEnabled (true);
-	copy_action->setEnabled (true);
+	copy_commands_action->setEnabled (true);
 }
 
 void RKConsole::activate (bool with_focus) {

Modified: trunk/rkward/rkward/rkconsole.h
===================================================================
--- trunk/rkward/rkward/rkconsole.h	2011-05-12 15:49:51 UTC (rev 3552)
+++ trunk/rkward/rkward/rkconsole.h	2011-05-13 08:28:49 UTC (rev 3553)
@@ -122,9 +122,9 @@
 
 	QString prefix;
 /** This string stores the regular prefix printed at the beginning of each line. */
-	const char *nprefix;
+	QString nprefix;
 /** This string stores the continuation prefix. */
-	const char *iprefix;
+	QString iprefix;
 
 /** Create a proxy for the katepart action of the same name. The action is added to the actioncollection, automatically. Also any icon and label (but not shorcut) is copied.
 @param actionName Identifier of the action in katepartui.rc and rkconsolepart.rc
@@ -132,7 +132,7 @@
 @returns a pointer to the proxy action */
 	QAction* addProxyAction (const QString& actionName, const QString& label=QString ());
 
-	QString cleanedSelection ();
+	QString cleanSelection (const QString &origin);
 
 	RCommand *current_command;
 	KTextEditor::Document *doc;
@@ -147,7 +147,7 @@
 	KAction* context_help_action;
 	KAction* run_selection_action;
 	KAction* interrupt_command_action;
-	KAction* copy_action;
+	KAction* copy_commands_action;
 	KAction* copy_literal_action;
 	KAction* paste_action;
 
@@ -163,7 +163,7 @@
 /** We intercept paste commands and get them executed through submitBatch.
 @sa submitBatch */
 	void paste ();
-	void copy ();
+	void copyCommands ();
 	void literalCopy ();
 /** Clear the view, and add a prompt at the top. */
 	void clear ();

Modified: trunk/rkward/rkward/rkconsolepart.rc
===================================================================
--- trunk/rkward/rkward/rkconsolepart.rc	2011-05-12 15:49:51 UTC (rev 3552)
+++ trunk/rkward/rkward/rkconsolepart.rc	2011-05-13 08:28:49 UTC (rev 3553)
@@ -21,8 +21,8 @@
 			<Action name="rkconsole_paste" group="edit_paste_merge"/>
 			<Separator/>
 			<Action name="rkconsole_clear"/> -->
-			<Action name="rkconsole_copy"/>
 			<Action name="rkconsole_copy_literal"/>
+			<Action name="rkconsole_copy_commands"/>
 			<Action name="rkconsole_paste"/>
 			<Separator/>
 			<Action name="rkconsole_clear"/>
@@ -40,8 +40,8 @@
 		<Action name="interrupt" group="postrun_actions_merge"/>
 	</ToolBar>
 	<Menu name="rkconsole_context_menu">
-		<Action name="rkconsole_copy"/>
 		<Action name="rkconsole_copy_literal"/>
+		<Action name="rkconsole_copy_commands"/>
 		<DefineGroup name="rkconsole_context_merge_copy" />
 		<Action name="rkconsole_paste"/>
 		<DefineGroup name="rkconsole_context_merge_paste" />


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