[ Moved from R-help]

Duncan Mackay wrote:
Hi all,
On the subject of histories, how about changing the default value of
"max.show" from 25 to Inf? I find that the line of code I'm looking for is
ALWAYS on the 26th line back from the end of the default history!! Also, re
changing the amount of history saved, Prof Ripley replied to an earlier
query of mine on this list (04 Mar 2005) with info about the R_HISTSIZE
environment variable that can be changed to save more or less history. And
it wouldn't it be great if the history mechanism had the option of storing
the session date??!!

I've written a timestamp() function to do the timestamping, but I'd like
some people on platforms other than Windows to take a look -- I can't
test it there.  I'd especially like to hear if trying it on platforms
that don't support history() causes problems.

This needs new internals, so you need to be able to build R to test.

If anyone has the time, could you try applying the attached patch to a
reasonably current R-devel source tree, and send me comments or make
improvements?  Thanks.

Duncan Murdoch

Index: src/unix/stubs.c
===================================================================
--- src/unix/stubs.c    (revision 37571)
+++ src/unix/stubs.c    (working copy)
@@ -39,3 +39,9 @@ SEXP attribute_hidden do_savehistory(SEX
     ptr_R_savehistory(call, op, args, rho);
     return R_NilValue;
 }
+
+SEXP do_addhistory(SEXP call, SEXP op, SEXP args, SEXP env)
+{
+    checkArity(op, args);
+    return ptr_R_addhistory(call, op, args, rho);
+}
Index: src/unix/system.c
===================================================================
--- src/unix/system.c   (revision 37571)
+++ src/unix/system.c   (working copy)
@@ -193,6 +193,7 @@ int Rf_initialize_R(int ac, char **av)
     ptr_R_ChooseFile = Rstd_ChooseFile;
     ptr_R_loadhistory = Rstd_loadhistory;
     ptr_R_savehistory = Rstd_savehistory;
+    ptr_R_addhistory = Rstd_addhistory;
     ptr_R_EditFile = NULL; /* for future expansion */
     R_timeout_handler = NULL;
     R_timeout_val = 0;
Index: src/unix/sys-std.c
===================================================================
--- src/unix/sys-std.c  (revision 37571)
+++ src/unix/sys-std.c  (working copy)
@@ -954,6 +954,23 @@ void attribute_hidden Rstd_savehistory(S
 #endif
 }
 
+SEXP attribute_hidden Rstd_addhistory(SEXP call, SEXP op, SEXP args, SEXP env)
+{
+    SEXP stamp;
+    int i;
+    
+    checkArity(op, args);
+    stamp = CAR(args);
+    if (!isString(stamp))
+       errorcall(call, _("invalid timestamp"));
+       
+# ifdef HAVE_READLINE_HISTORY_H
+    for (i = 0; i < LENGTH(stamp); i++) 
+       add_history(CHAR(STRING_ELT(stamp, i)));
+# endif      
+    return stamp;
+}
+
 
 
 
Index: src/unix/Runix.h
===================================================================
--- src/unix/Runix.h    (revision 37571)
+++ src/unix/Runix.h    (working copy)
@@ -19,5 +19,6 @@ int  Rstd_ShowFiles(int nfile, char **fi
 int  Rstd_ChooseFile(int new, char *buf, int len);
 void Rstd_loadhistory(SEXP call, SEXP op, SEXP args, SEXP env);
 void Rstd_savehistory(SEXP call, SEXP op, SEXP args, SEXP env);
+SEXP Rstd_addhistory(SEXP call, SEXP op, SEXP args, SEXP env);
 
 void R_load_X11_shlib(void);
Index: src/include/Internal.h
===================================================================
--- src/include/Internal.h      (revision 37571)
+++ src/include/Internal.h      (working copy)
@@ -76,6 +76,7 @@ SEXP do_writeClipboard(SEXP, SEXP, SEXP,
 SEXP do_abbrev(SEXP, SEXP, SEXP, SEXP);
 SEXP do_abline(SEXP, SEXP, SEXP, SEXP);
 SEXP do_abs(SEXP, SEXP, SEXP, SEXP);
+SEXP do_addhistory(SEXP, SEXP, SEXP, SEXP);
 #ifdef NEW_CONDITION_HANDLING
 SEXP do_addCondHands(SEXP, SEXP, SEXP, SEXP);
 SEXP do_addRestart(SEXP, SEXP, SEXP, SEXP);
Index: src/main/names.c
===================================================================
--- src/main/names.c    (revision 37571)
+++ src/main/names.c    (working copy)
@@ -825,6 +826,7 @@ attribute_hidden FUNTAB R_FunTab[] =
 /* History manipulation */
 {"loadhistory", do_loadhistory,        0,      11,     1,      {PP_FUNCALL, 
PREC_FN,   0}},
 {"savehistory", do_savehistory,        0,      11,     1,      {PP_FUNCALL, 
PREC_FN,   0}},
+{"addhistory",  do_addhistory,  0,     11,     1,      {PP_FUNCALL, PREC_FN,   
0}},
 
 /* date-time manipulations */
 {"Sys.time",   do_systime,     0,      11,     0,      {PP_FUNCALL, PREC_FN,   
0}},
Index: src/library/utils/R/history.R
===================================================================
--- src/library/utils/R/history.R       (revision 37571)
+++ src/library/utils/R/history.R       (working copy)
@@ -17,3 +17,9 @@ history <- function(max.show=25, reverse
     write(rawhist[inds], file2)
     file.show(file2, title="R History", delete.file=TRUE)
 }
+
+timestamp <- function(stamp=date(), prefix="#------ ", suffix=" ------#")
+{
+    stamp <- paste(prefix, stamp, suffix, sep="")
+    invisible(.Internal(addhistory(stamp)))
+}
Index: src/library/utils/man/savehistory.Rd
===================================================================
--- src/library/utils/man/savehistory.Rd        (revision 37571)
+++ src/library/utils/man/savehistory.Rd        (working copy)
@@ -2,6 +2,7 @@
 \alias{loadhistory}
 \alias{savehistory}
 \alias{history}
+\alias{timestamp}
 \title{Load or Save or Display the Commands History}
 \description{
   Load or save or display the commands history.
@@ -10,6 +11,7 @@
 loadhistory(file = ".Rhistory")
 savehistory(file = ".Rhistory")
 history(max.show = 25, reverse = FALSE)
+timestamp(stamp = date(), prefix="#------ ", suffix=" ------#")
 }
 \arguments{
   \item{file}{The name of the file in which to save the history, or
@@ -19,6 +21,9 @@ history(max.show = 25, reverse = FALSE)
     give all of the currently available history.}
   \item{reverse}{logical. If true, the lines are shown in reverse
     order. Note: this is not useful when there are continuation lines.}
+  \item{stamp}{A value or vector of values to be written into the history.}
+  \item{prefix}{A prefix to apply to each line.}
+  \item{suffix}{A suffix to apply to each line.}
 }
 \details{
   There are several history mechanisms available for the different \R
@@ -47,6 +52,9 @@ history(max.show = 25, reverse = FALSE)
 
   These variables are read at the time of saving, so can be altered
   within a session by the use of \code{\link{Sys.putenv}}.
+  
+  The \code{timestamp} function writes a timestamp (or other message)
+  into the history.
 }
 \note{
   If you want to save the history (almost) every session, you can put a
Index: src/library/utils/NAMESPACE
===================================================================
--- src/library/utils/NAMESPACE (revision 37571)
+++ src/library/utils/NAMESPACE (working copy)
@@ -20,7 +20,7 @@ export("?", CRAN.packages, Rprof, RSiteS
        promptData, promptPackage, readCitationFile, read.fwf,
        read.fortran, read.socket, recover, remove.packages,
        savehistory, select.list, sessionInfo, setRepositories, str,
-       summaryRprof, tail, topicName, toBibtex, toLatex,
+       summaryRprof, tail, timestamp, topicName, toBibtex, toLatex,
        update.packageStatus, update.packages, upgrade, url.show, vi,
        vignette, write.socket, wsbrowser, xedit, xemacs)
 
Index: src/gnuwin32/extra.c
===================================================================
--- src/gnuwin32/extra.c        (revision 37571)
+++ src/gnuwin32/extra.c        (working copy)
@@ -494,6 +494,20 @@ SEXP do_loadhistory(SEXP call, SEXP op, 
     return R_NilValue;
 }
 
+SEXP do_addhistory(SEXP call, SEXP op, SEXP args, SEXP env)
+{
+    SEXP stamp;
+    int i;
+    
+    checkArity(op, args);
+    stamp = CAR(args);
+    if (!isString(stamp))
+       errorcall(call, _("invalid timestamp"));
+    for (i = 0; i < LENGTH(stamp); i++) 
+       gl_histadd(CHAR(STRING_ELT(stamp, i)));
+    return stamp;
+}
+    
 #include <lmcons.h>
 
 SEXP do_sysinfo(SEXP call, SEXP op, SEXP args, SEXP rho)
Index: NEWS
===================================================================
--- NEWS        (revision 37571)
+++ NEWS        (working copy)
@@ -375,6 +375,8 @@ NEW FEATURES
     o  splinefun() returns a function that now also has a 'deriv' argument
        and can provide up to the 3rd derivative of the interpolating
        spline, thanks to Berwin Turlach.
+       
+    o  timestamp() adds a time stamp to the saved command history.
 
 
 DEPRECATED & DEFUNCT

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to