beautifying an R package

Martin Maechler maechler at stat.math.ethz.ch
Fri Oct 6 11:31:09 CEST 2000


>>>>> "Ramon" == Ramon Diaz-Uriarte <ramon-diaz at teleline.es> writes:

    Ramon> I wrote an R package and (because I was sloppy the
    Ramon> first time) indentation, spacing, etc, are not what they should
    Ramon> be (things such as always having spaces around "<-", after
    Ramon> commas and binary operators, etc).  I was wondering if there is
    Ramon> a way to fix up this mess (while also keeping all the comments
    Ramon> in their current position). For instance, I love the way it
    Ramon> looks when I source things into R, but then all comments are
    Ramon> gone, etc.

>>>>> "Rich" == Rich Heiberger <rmh at surfer.sbm.temple.edu> writes:

    Rich> Yes, once you have installed ESS, open up your file myfile.r in emacs.
    Rich> it will come up in ESS[S] mode.  Highlight the entire file and enter
    Rich>         M-x indent-region

    Rich> The whole file will be indented according to the usual S
    Rich> prettification rules.

{Good, but it's usally a doing rather too much }


Additionally, for solving Ramon's problem:
Here is 
     M-x R-fix-miscellaneous
that I've been using quite a few times in the past.

Would it make sense to make it available in ESS ?
{one reason against that: it's not 100% bug free..}

;;;;
;;;; Functions & Variables  for  ``R Core Style Fixing'' of  R code.

(defun string<= (a b) (or (string= a b) (string< a b)))

;;;== NOW in ess/lisp/ess-utils.el :
;; (defun inside-string/comment-p (pos) ....)

;; (defvar nuke-trailing-whitespace-p nil ...)

;; (defun nuke-trailing-whitespace () ....)

;; (defun ess-rep-regexp (regexp to-string &optional fixedcase literal verbose)
;;  ....)

;;;== NOW in ess/lisp/essl-s.el :

;; (defun ess-fix-comments (&optional dont-query verbose) ....)

;; (defun ess-dump-to-src (&optional dont-query verbose) ....)

(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))
    ))

;;; The following is too dangerous: Can only have it when we have a reliable
;;;  (inside-string-p) :
(defun R-fix-miscellaneous (&optional from verbose)
  "Fix Miscellaneous R `ill-formation's."
  (interactive "d\nP"); point and prefix (C-u)
  (save-excursion

    ;;-- The next 4 lines:  == (R-fix-T-F) --------------------
    (goto-char from)
    (ess-rep-regexp "\\(= *\\|<- *\\)T\\>" "\\1TRUE" 'fix nil verbose)
    (goto-char from)
    (ess-rep-regexp "\\(= *\\|<- *\\)F\\>" "\\1FALSE" 'fix nil verbose)

    (goto-char from) (ess-rep-regexp " *_ *" " <- " nil 'literal verbose)

    ;; -- ensure space around  "<-"  ---- but only replace if necessary:
    (goto-char from)(ess-rep-regexp "\\([^ \t\n]\\)<-" "\\1 <-" nil nil verbose)
    (goto-char from)(ess-rep-regexp "<-\\([^ \t\n]\\)" "<- \\1" nil nil verbose)
    ;; -- ensure space around  "<" (but not "<-" / "<=")  and ">" (not ">=") :
    (goto-char from);; --> " <", care with "->":
    (ess-rep-regexp "\\([^- \t\n]\\)\\([<>]\\)" "\\1 \\2" nil nil verbose)
    ;; ">" -> "> " , for "<", don't split "<-":
    (goto-char from)
    (ess-rep-regexp "\\(>=?\\)\\([^ \t\n]\\)" "\\1 \\2" nil nil verbose)
    (goto-char from)
    (ess-rep-regexp "\\(<=?\\)\\([^- \t\n]\\)" "\\1 \\2" nil nil t);; !
    ;; -- ensure space around "=", "==", "!=" :
    (goto-char from) ;; --> " ="
    (ess-rep-regexp "\\([^=!<> ]\\)\\([=!]?\\)=" "\\1 \\2=" nil nil verbose)
    (goto-char from) (ess-rep-regexp "=\\([^= ]\\)" "= \\1" nil nil verbose)

    (goto-char from) ;; add a space between "{" and a subsequent wordchar:
    (ess-rep-regexp "\\([()]\\){\\([A-Za-z()]\\)" "{ \\1" 'fix nil verbose)
    (goto-char from) ;; add a space between "}" and a preceding wordchar:
    (ess-rep-regexp "\\([A-Za-z0-9()]\\)}" "\\1 }" 'fix nil verbose)

    ;; add a newline and indent before a "}"
    ;; --- IFF there's NO "{" or "#" AND some NON-white text on the same line:
    ;;D (if verbose (message "\t R-fix-misc..: Hard.. '}'"))
    (goto-char from)
    (ess-rep-regexp "^\\([^#{\n]*[^#{ \t\n]+[ \t]*\\)}[ \t]*$"
		     "\\1\n}" 'fix nil verbose)
    ))



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
ess-help mailing list -- To (un)subscribe, send
subscribe	or	unsubscribe
(in the "body", not the subject !)  To: ess-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the ESS-help mailing list