Help please: please test timestamping of command history

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Help please: please test timestamping of command history

Duncan Murdoch
[ 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


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Help please: please test timestamping of command history

Martin Maechler
Hi Duncan,

I think all ESS users don't use history() because ESS calls R  
with "--no-readline" (Unix)  
or   "--ess"  (Windows & Cygwin)

I'd wish that in that case, and probably also in BATCH mode,
timestamp() should write the time stamp prefixed by "##" to the
"R console" (to R's stdout); when people are using ESS properly, then
rather than wanting a history, they save the R's buffer ("*R*") as
"R transcript" (file typically ending with ".Rt" or ".Rout")
and it makes much sense to have a time stampe entry in that file when
others would want an entry in the history.

BTW, after applying your patch, for me, compilation ends
prematurely with

gcc -I. -I../../src/include -I../../../R/src/include -I/usr/X11R6/include -I/usr/local/include -DHAVE_CONFIG_H   -g -O3 -pedantic -Wall -Wno-comment -DDEBUG_q -Wcast-align -c ../../../R/src/unix/stubs.c -o stubs.o
../../../R/src/unix/stubs.c: In function `do_addhistory':
../../../R/src/unix/stubs.c:46: warning: implicit declaration of function `ptr_R_addhistory'
../../../R/src/unix/stubs.c:46: error: `rho' undeclared (first use in this function)
../../../R/src/unix/stubs.c:46: error: (Each undeclared identifier is reported only once
../../../R/src/unix/stubs.c:46: error: for each function it appears in.)
../../../R/src/unix/stubs.c:46: warning: return makes pointer from integer without a cast
make[3]: *** [stubs.o] Error 1


Martin

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Help please: please test timestamping of command history

Duncan Murdoch
On 3/25/2006 11:30 AM, Martin Maechler wrote:

> Hi Duncan,
>
> I think all ESS users don't use history() because ESS calls R  
> with "--no-readline" (Unix)  
> or   "--ess"  (Windows & Cygwin)
>
> I'd wish that in that case, and probably also in BATCH mode,
> timestamp() should write the time stamp prefixed by "##" to the
> "R console" (to R's stdout); when people are using ESS properly, then
> rather than wanting a history, they save the R's buffer ("*R*") as
> "R transcript" (file typically ending with ".Rt" or ".Rout")
> and it makes much sense to have a time stampe entry in that file when
> others would want an entry in the history.
>
> BTW, after applying your patch, for me, compilation ends
> prematurely with
>
> gcc -I. -I../../src/include -I../../../R/src/include -I/usr/X11R6/include -I/usr/local/include -DHAVE_CONFIG_H   -g -O3 -pedantic -Wall -Wno-comment -DDEBUG_q -Wcast-align -c ../../../R/src/unix/stubs.c -o stubs.o
> ../../../R/src/unix/stubs.c: In function `do_addhistory':
> ../../../R/src/unix/stubs.c:46: warning: implicit declaration of function `ptr_R_addhistory'
> ../../../R/src/unix/stubs.c:46: error: `rho' undeclared (first use in this function)
> ../../../R/src/unix/stubs.c:46: error: (Each undeclared identifier is reported only once
> ../../../R/src/unix/stubs.c:46: error: for each function it appears in.)
> ../../../R/src/unix/stubs.c:46: warning: return makes pointer from integer without a cast
> make[3]: *** [stubs.o] Error 1

Okay, this compile error is fixed.  I've put a new version of the patch in

http://www.stats.uwo.ca/faculty/murdoch/software/timestamp.patch

I haven't addressed the ESS request above other than changing the
message prefix to start with ##; I don't know how to detect ESS from
within an R session.

Duncan Murdoch

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Help please: please test timestamping of command history

Duncan Murdoch
I managed to get an R build going on a Linux box, and tested the changes
there.  Now they're committed to R-devel.

Duncan Murdoch

On 3/25/2006 2:58 PM, Duncan Murdoch wrote:

> On 3/25/2006 11:30 AM, Martin Maechler wrote:
>> Hi Duncan,
>>
>> I think all ESS users don't use history() because ESS calls R  
>> with "--no-readline" (Unix)  
>> or   "--ess"  (Windows & Cygwin)
>>
>> I'd wish that in that case, and probably also in BATCH mode,
>> timestamp() should write the time stamp prefixed by "##" to the
>> "R console" (to R's stdout); when people are using ESS properly, then
>> rather than wanting a history, they save the R's buffer ("*R*") as
>> "R transcript" (file typically ending with ".Rt" or ".Rout")
>> and it makes much sense to have a time stampe entry in that file when
>> others would want an entry in the history.
>>
>> BTW, after applying your patch, for me, compilation ends
>> prematurely with
>>
>> gcc -I. -I../../src/include -I../../../R/src/include -I/usr/X11R6/include -I/usr/local/include -DHAVE_CONFIG_H   -g -O3 -pedantic -Wall -Wno-comment -DDEBUG_q -Wcast-align -c ../../../R/src/unix/stubs.c -o stubs.o
>> ../../../R/src/unix/stubs.c: In function `do_addhistory':
>> ../../../R/src/unix/stubs.c:46: warning: implicit declaration of function `ptr_R_addhistory'
>> ../../../R/src/unix/stubs.c:46: error: `rho' undeclared (first use in this function)
>> ../../../R/src/unix/stubs.c:46: error: (Each undeclared identifier is reported only once
>> ../../../R/src/unix/stubs.c:46: error: for each function it appears in.)
>> ../../../R/src/unix/stubs.c:46: warning: return makes pointer from integer without a cast
>> make[3]: *** [stubs.o] Error 1
>
> Okay, this compile error is fixed.  I've put a new version of the patch in
>
> http://www.stats.uwo.ca/faculty/murdoch/software/timestamp.patch
>
> I haven't addressed the ESS request above other than changing the
> message prefix to start with ##; I don't know how to detect ESS from
> within an R session.
>
> Duncan Murdoch
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Help please: please test timestamping of command history

Martin Maechler
In reply to this post by Martin Maechler
[from a semi-private diversion of the R-devel thread ]

>>>>> "Duncan" == Duncan Murdoch <[hidden email]>
>>>>>     on Sat, 25 Mar 2006 12:28:30 -0500 writes:

    Duncan> On 3/25/2006 11:30 AM, Martin Maechler wrote:
    >> Hi Duncan,
    >>
    >> I think all ESS users don't use history() because ESS
    >> calls R with "--no-readline" (Unix) or "--ess" (Windows &
    >> Cygwin)
    >>
    >> I'd wish that in that case, and probably also in BATCH
    >> mode, timestamp() should write the time stamp prefixed by
    >> "##" to the "R console" (to R's stdout); when people are
    >> using ESS properly, then rather than wanting a history,
    >> they save the R's buffer ("*R*") as "R transcript" (file
    >> typically ending with ".Rt" or ".Rout") and it makes much
    >> sense to have a time stampe entry in that file when
    >> others would want an entry in the history.

    Duncan> Thanks, that's a good suggestion.  Do you know what
    Duncan> the test is for this state, in either R or C code?
    Duncan> capabilities() doesn't do it.  Does ESS make itself
    Duncan> known to R code somehow?

Yes, when ESS starts  R (or S+) , it also issues

  options(STERM='iESS')

and we (ESS core) thought that other GUIs / IDEs ideally should
also set "STERM"  - which AFAIK hasn't been adopted widely.

hence   if ( identical("iESS", getOption("STERM")) )  {
           ## are running 'inside ESS'
        }

should be pretty reliable.

Martin

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Help please: please test timestamping of command history

Duncan Murdoch
On 3/27/2006 1:47 AM, Martin Maechler wrote:

> [from a semi-private diversion of the R-devel thread ]
>
>>>>>> "Duncan" == Duncan Murdoch <[hidden email]>
>>>>>>     on Sat, 25 Mar 2006 12:28:30 -0500 writes:
>
>     Duncan> On 3/25/2006 11:30 AM, Martin Maechler wrote:
>     >> Hi Duncan,
>     >>
>     >> I think all ESS users don't use history() because ESS
>     >> calls R with "--no-readline" (Unix) or "--ess" (Windows &
>     >> Cygwin)
>     >>
>     >> I'd wish that in that case, and probably also in BATCH
>     >> mode, timestamp() should write the time stamp prefixed by
>     >> "##" to the "R console" (to R's stdout); when people are
>     >> using ESS properly, then rather than wanting a history,
>     >> they save the R's buffer ("*R*") as "R transcript" (file
>     >> typically ending with ".Rt" or ".Rout") and it makes much
>     >> sense to have a time stampe entry in that file when
>     >> others would want an entry in the history.
>
>     Duncan> Thanks, that's a good suggestion.  Do you know what
>     Duncan> the test is for this state, in either R or C code?
>     Duncan> capabilities() doesn't do it.  Does ESS make itself
>     Duncan> known to R code somehow?
>
> Yes, when ESS starts  R (or S+) , it also issues
>
>   options(STERM='iESS')
>
> and we (ESS core) thought that other GUIs / IDEs ideally should
> also set "STERM"  - which AFAIK hasn't been adopted widely.
>
> hence   if ( identical("iESS", getOption("STERM")) )  {
>   ## are running 'inside ESS'
>         }
>
> should be pretty reliable.

Thanks.  The version that I committed yesterday defaulted to writing the
timestamp to the console as well as the history.  Could you take a look,
and let me know if some special ESS behaviour is still needed?

Duncan

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel