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

tfry at users.sourceforge.net tfry at users.sourceforge.net
Wed Jan 26 19:59:41 UTC 2011


Revision: 3395
          http://rkward.svn.sourceforge.net/rkward/?rev=3395&view=rev
Author:   tfry
Date:     2011-01-26 19:59:41 +0000 (Wed, 26 Jan 2011)

Log Message:
-----------
Add support for R's history utility functions

Modified Paths:
--------------
    trunk/rkward/ChangeLog
    trunk/rkward/rkward/core/robject.cpp
    trunk/rkward/rkward/rbackend/rinterface.cpp
    trunk/rkward/rkward/rbackend/rpackages/rkward/R/internal.R
    trunk/rkward/rkward/rkconsole.cpp
    trunk/rkward/rkward/rkconsole.h

Modified: trunk/rkward/ChangeLog
===================================================================
--- trunk/rkward/ChangeLog	2011-01-26 18:53:41 UTC (rev 3394)
+++ trunk/rkward/ChangeLog	2011-01-26 19:59:41 UTC (rev 3395)
@@ -1,3 +1,4 @@
+- Add support for R functions loadhistory(), savehistory(), history(), and timestamp ()
 - Do not load .RData file from current directory by default (configurable)
 - Fixed: On Windows, a wrong locale for LC_NUMERIC would be applied, resulting in malfunction of pdf() and postscript()
 - Fixed: Did not use system configured background color in data editor

Modified: trunk/rkward/rkward/core/robject.cpp
===================================================================
--- trunk/rkward/rkward/core/robject.cpp	2011-01-26 18:53:41 UTC (rev 3394)
+++ trunk/rkward/rkward/core/robject.cpp	2011-01-26 19:59:41 UTC (rev 3395)
@@ -2,7 +2,7 @@
                           robject  -  description
                              -------------------
     begin                : Thu Aug 19 2004
-    copyright            : (C) 2004, 2006, 2007, 2009, 2010 by Thomas Friedrichsmeier
+    copyright            : (C) 2004, 2006, 2007, 2009, 2010, 2011 by Thomas Friedrichsmeier
     email                : tfry at users.sourceforge.net
  ***************************************************************************/
 
@@ -498,9 +498,18 @@
 
 //static
 QString RObject::rQuote (const QString &string) {
-	// TODO: this is not entirely correct, yet (let alone efficient)!
-	QString copy = string;
-	return ("\"" + copy.replace (QRegExp ("\""), "\\\"") + "\"");
+	QString ret;
+	int s = string.size ();
+	ret.reserve (s + 2);	// typical case: Only quotes added, no escapes needed.
+	ret.append ('\"');
+	for (int i = 0; i < s; ++i) {
+		const QChar c = string[i];
+		if ((c == '\\') || (c == '\"')) ret.append ('\\');
+		ret.append (c);
+	}
+	ret.append ('\"');
+
+	return ret;
 }
 
 // static

Modified: trunk/rkward/rkward/rbackend/rinterface.cpp
===================================================================
--- trunk/rkward/rkward/rbackend/rinterface.cpp	2011-01-26 18:53:41 UTC (rev 3394)
+++ trunk/rkward/rkward/rbackend/rinterface.cpp	2011-01-26 19:59:41 UTC (rev 3395)
@@ -2,7 +2,7 @@
                           rinterface.cpp  -  description
                              -------------------
     begin                : Fri Nov 1 2002
-    copyright            : (C) 2002, 2004, 2005, 2006, 2007, 2009, 2010 by Thomas Friedrichsmeier
+    copyright            : (C) 2002, 2004, 2005, 2006, 2007, 2009, 2010, 2011 by Thomas Friedrichsmeier
     email                : tfry at users.sourceforge.net
  ***************************************************************************/
 
@@ -20,6 +20,7 @@
 #include "rcommandstack.h"
 #include "rkrbackendprotocol_frontend.h"
 #include "../rkward.h"
+#include "../rkconsole.h"
 #include "../settings/rksettingsmoduler.h"
 #include "../settings/rksettingsmodulegeneral.h"
 #include "../settings/rksettingsmoduleoutput.h"
@@ -591,6 +592,19 @@
 		command.append ("))");
 
 		issueCommand (command, RCommand::App | RCommand::Sync, QString::null, 0, 0, in_chain);
+	} else if (call == "commandHistory") {
+		if (calllist[1] == "get") {
+			QStringList hist = RKConsole::mainConsole ()->commandHistory ();
+			QString command = (".rk.set.reply (c (");
+			for (int i = 0; i < hist.size (); ++i) {
+				command.append (RObject::rQuote (hist[i]));
+				if (i < (hist.size () - 1)) command.append (",\n");
+			}
+			command.append ("))\n");
+			issueCommand (command, RCommand::App | RCommand::Sync, QString::null, 0, 0, in_chain);
+		} else {
+			RKConsole::mainConsole ()->setCommandHistory (calllist.mid (2), calllist[1] == "append");
+		}
 	} else if (call == "recordCommands") {
 		if (calllist.count () == 3) {
 			QString filename = calllist[1];

Modified: trunk/rkward/rkward/rbackend/rpackages/rkward/R/internal.R
===================================================================
--- trunk/rkward/rkward/rbackend/rpackages/rkward/R/internal.R	2011-01-26 18:53:41 UTC (rev 3394)
+++ trunk/rkward/rkward/rbackend/rpackages/rkward/R/internal.R	2011-01-26 19:59:41 UTC (rev 3395)
@@ -391,6 +391,22 @@
 	}
 }
 
+## History manipulation function (overloads for functions by the same name in package utils)
+"loadhistory" <- function (file = ".Rhistory") {
+	invisible (.rk.do.call ("commandHistory", c ("set", readLines (file))))
+}
+
+"savehistory" <- function (file = ".Rhistory") {
+	invisible (writeLines (.rk.do.call ("commandHistory", "get"), file))
+}
+
+"timestamp" <- function (stamp = date(), prefix = "##------ ", suffix = " ------##", quiet = FALSE) {
+	stamp <- paste(prefix, stamp, suffix, sep = "")
+	.rk.do.call (.rk.do.call ("commandHistory", c ("append", stamp)))
+	if (!quiet) cat(stamp, sep = "\n")
+	invisible(stamp)
+}
+
 # retrieve the (expected) "base" url of help files. Most importantly this will be a local port for R 2.10.0 and above, but a local directory for 2.9.x and below. As a side effect, in R 2.10.0 and above, the dynamic help server is started.
 ".rk.getHelpBaseUrl" <- function () {
 	port <- NA
@@ -483,6 +499,9 @@
 # so we have a separate function for that.
 #NOTE: TODO: By now we are replacing so many functions, that it would make sense to create a generic framework for doing such replacements.
 ".rk.fix.assignments" <- function () {
+	assignInNamespace ("loadhistory", loadhistory, envir=as.environment ("package:utils"))
+	assignInNamespace ("savehistory", savehistory, envir=as.environment ("package:utils"))
+	assignInNamespace ("timestamp", timestamp, envir=as.environment ("package:utils"))
 	assignInNamespace ("menu", menu, envir=as.environment ("package:utils"))
 	assignInNamespace ("select.list", select.list, envir=as.environment ("package:utils"))
 	try ({

Modified: trunk/rkward/rkward/rkconsole.cpp
===================================================================
--- trunk/rkward/rkward/rkconsole.cpp	2011-01-26 18:53:41 UTC (rev 3394)
+++ trunk/rkward/rkward/rkconsole.cpp	2011-01-26 19:59:41 UTC (rev 3395)
@@ -2,7 +2,7 @@
                           rkconsole  -  description
                              -------------------
     begin                : Thu Aug 19 2004
-    copyright            : (C) 2004, 2006, 2007, 2009, 2010 by Thomas Friedrichsmeier
+    copyright            : (C) 2004, 2006, 2007, 2009, 2010, 2011 by Thomas Friedrichsmeier
     email                : tfry at users.sourceforge.net
  ***************************************************************************/
 
@@ -782,11 +782,11 @@
 
 void RKConsole::addCommandToHistory (const QString &command) {
 	RK_TRACE (APP);
-	if (command.isEmpty () || ((!commands_history.isEmpty ()) && commands_history.last() == command)) return; // don't add empty or duplicate lines
+	if ((!command.isEmpty ()) && (commands_history.isEmpty () || commands_history.last() != command)) { // don't add empty or duplicate lines
+		commands_history.append (command);
+		history_editing_line = QString ();
+	}
 
-	commands_history.append (command);
-	history_editing_line = QString ();
-
 	if (RKSettingsModuleConsole::maxHistoryLength ()) {
 		uint c = commands_history.count ();
 		for (uint ui = c; ui > RKSettingsModuleConsole::maxHistoryLength (); --ui) {
@@ -795,6 +795,15 @@
 	}
 }
 
+void RKConsole::setCommandHistory (const QStringList &new_history, bool append) {
+	RK_TRACE (APP);
+
+	if (append) commands_history.append (new_history);
+	else commands_history = new_history;
+
+	addCommandToHistory (QString ());	// side-effect of checking history length
+}
+
 QString RKConsole::cleanedSelection () {
 	RK_TRACE (APP);
 

Modified: trunk/rkward/rkward/rkconsole.h
===================================================================
--- trunk/rkward/rkward/rkconsole.h	2011-01-26 18:53:41 UTC (rev 3394)
+++ trunk/rkward/rkward/rkconsole.h	2011-01-26 19:59:41 UTC (rev 3395)
@@ -2,7 +2,7 @@
                           rkconsole  -  description
                              -------------------
     begin                : Thu Aug 19 2004
-    copyright            : (C) 2004, 2006, 2007, 2010 by Thomas Friedrichsmeier
+    copyright            : (C) 2004, 2006, 2007, 2010, 2011 by Thomas Friedrichsmeier
     email                : tfry at users.sourceforge.net
  ***************************************************************************/
 
@@ -71,6 +71,8 @@
 
 /** reimplemnented from RKMDIWindow to clear selection when gaining focus */
 	void activate (bool with_focus=true);
+	void setCommandHistory (const QStringList &new_history, bool append);
+	QStringList commandHistory () const { return commands_history; };
 protected:
 /** Handle keystrokes before they reach the kate-part. Return TRUE if we want the kate-part to ignore it
 \param e the QKeyEvent */


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