[R] code to turn T into TRUE

Laurent Gautier laurent at cbs.dtu.dk
Fri Oct 18 11:40:42 CEST 2002


I do not know if it's me or my versions of emacs, but I never managed to
make M-x R-fix-T-F have any action on my buffers...

------------
In GNU Emacs 21.2.1 (i386-mandrake-linux-gnu, X toolkit, Xaw3d scroll
bars)
 of 2002-08-18 on ke.mandrakesoft.com, modified by Mandrake
 configured using `configure  --prefix=/usr --libexecdir=/usr/lib
 --sharedstatedir=/var --with-gcc --with-pop --mandir=/usr/share/man
 --infodir=/usr/share/info --with-x-toolkit i386-mandrake-linux
 --libdir=/usr/lib'
------------

ess version is ess-5.1.24 (note: I remembered that the making of the pdf
manuals was broken when building (so the build was interupted)).


I attached a small perl script I used to update my packages
(but it does not check whether T and F are inside ""s...)



L.

On Fri, Oct 18, 2002 at 08:58:59AM +0200, Martin Maechler wrote:
> >>>>> "Mark" == Mark Bravington <Mark.Bravington at csiro.au>
> >>>>>     on Fri, 18 Oct 2002 14:58:04 +1100 writes:
> 
>     Mark> Does anyone have code that will methodically process R
>     Mark> sourcecode, turning T's into TRUE and F's into FALSE?
>     Mark> I got bored doing this by hand, after the first 30-odd
>     Mark> functions-- there are hundreds left to do. I don't
>     Mark> want to simply deparse everything, because that would
>     Mark> destroy my beautiful formatting.
> 
> Same boat ("beautiful formatting") here.
> 
> Yes, ESS has had code for several year to do this 
> {CC to ESS-help, since people there might be interested additionally} :
> 
>   M-x R-fix-T-F            (guess who wrote it)
> 
> which does it very quickly for one file.
> Note that the emacs-lisp code we put in ESS is somewhat smart;
> we only replace T/F when *not* inside a string or comment.
> 
> If anybody has time & knowledge to translate the Emacs-Lisp code to perl
> or python or... we could provide it (from CRAN or so) :
> 
> ;;;---------------------------------------------------------------------------
> (defun R-fix-T-F (&optional from quietly)
>   "Fix T/F into TRUE and FALSE --- CAUTIOUSLY"
>   (interactive "d\nP"); point and prefix (C-u)
>   (save-excursion
>     (goto-char from)
>     (ess-rep-regexp "\\(\\([][=,()]\\|<-\\|_\\) *\\)T\\>" "\\1TRUE"
> 		    'fixcase nil (not quietly))
>     (goto-char from)
>     (ess-rep-regexp "\\(\\([][=,()]\\|<-\\|_\\\) *\\)F\\>" "\\1FALSE"
> 		    'fixcase nil (not quietly))))
> 
> (defun ess-rep-regexp (regexp to-string &optional fixedcase literal verbose)
>   "Instead of (replace-regexp..) -- do NOT replace in strings or comments.
>  If FIXEDCASE is non-nil, do *not* alter case of replacement text.
>  If LITERAL   is non-nil, do *not* treat `\\' as special.
>  If VERBOSE   is non-nil, (message ..) about replacements."
>   (let ((case-fold-search (and case-fold-search
> 			       (not fixedcase))); t  <==> ignore case in search
> 	(pl) (p))
>     (while (setq p (re-search-forward regexp nil t))
>       (cond ((not (inside-string/comment-p (1- p)))
> 	     (if verbose
> 		 (let ((beg (match-beginning 0)))
> 		   (message "(beg,p)= (%d,%d) = %s"
> 			    beg p (buffer-substring beg p) )))
> 	     (replace-match to-string fixedcase literal)
> 	     ;;or (if verbose (setq pl (append pl (list p))))
> 	     )))
>     ;;or (if (and verbose pl)
>     ;;or  (message "s/%s/%s/ at %s" regexp to-string pl))
>     ) )
> 
> (defun inside-string/comment-p (pos)
>   "Return non-nil if POSition [defaults to (point) is inside string or comment
>  (according to syntax). NOT OKAY for multi-line comments!!"
>   ;;FIXME (defun S-calculate-indent ..) in ./essl-s.el can do that ...
>   (interactive "d");point by default
>   (let ((pps (save-excursion
> 	       (parse-partial-sexp
> 		(save-excursion (beginning-of-line) (point))
> 		pos))))
>     (or (nth 3 pps) (nth 4 pps)))); 3: string,  4: comment
> 
> ;;;---------------------------------------------------------------------------
> 
> Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
> Seminar fuer Statistik, ETH-Zentrum  LEO C16	Leonhardstr. 27
> ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
> phone: x-41-1-632-3408		fax: ...-1228			<><
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
-------------- next part --------------
## GPL
##
## usage:
## perl fixTnF.pl <path to package>
## example:
## perl fixTnF.pl Rstuff/affy
## 
## original files are copied to '<file>_old'
##

use POSIX;
use File::Copy cp;

$dir = shift;
chdir($dir) || die("cannot cd to dir ".$dir);

$com = 'ls R/*.R man/*.Rd';
@files = `$com`;
chomp(@files);
foreach $file (@files) {
    if (-f $file.'_old') {
	print STDERR 'found a file named ', $file.'_old', "\n";
	print STDERR "nothing was done. Please move it.\n";
	exit(0);
    }
}
foreach $file (@files) {
    cp($file, $file.'_old');
    unlink($file);
    open(IN, $file.'_old') or die("cannot open ".$file.'_old');
    open(OUT, '>'.$file);
    while(<IN>) {
	s/(\[|&|\||=|,| |\()T(&|\|| |,|=|\)|$ |\])/${1}TRUE$2/og;
	s/(\[|&|\||=|,| |\()F(&|\|| |,|=|\)|$ |\])/${1}FALSE$2/og;
	##print STDERR $_;
	print OUT $_;
    }
    close(IN);
    close(OUT);
}


More information about the R-help mailing list