Search code examples
remacscoding-styleelispess

Make Emacs ESS follow R style guide


I've been using Emacs/ESS for quite a while, and I'm familiar with Hadley's R style recommendations. I'd like to follow these conventions in ESS, like those nice spaces around operators, space after comma and after if statement, before curly braces, etc.

Did anyone even bothered to follow this style guide at all? IMHO, official style recommendations are quite modest, and they say nothing about the style whatsoever. Google R style guide are too similar with the ones I use when I code in JavaScript, so it's a no-no.

Long story short: is there anyone with (e)LISP skills willing to implement (Hadley's) style guide for ESS?


Solution

  • The good point of Hadley's guide is spaceing around operators (except maybe around /)

    There is a smart-operator package which implements it for almost every operator.

    This is my setup (uncoment operators which you want to use):

    (setq smart-operator-mode-map
      (let ((keymap (make-sparse-keymap)))
        (define-key keymap "=" 'smart-operator-self-insert-command)
        ;; (define-key keymap "<" 'smart-operator-<)
        ;; (define-key keymap ">" 'smart-operator->)
        ;; (define-key keymap "%" 'smart-operator-%)
        (define-key keymap "+" 'smart-operator-+)
        ;; (define-key keymap "-" 'smart-operator--)
        ;; (define-key keymap "*" 'smart-operator-*)
        ;; (define-key keymap "/" 'smart-operator-self-insert-command)
        (define-key keymap "&" 'smart-operator-&)
        (define-key keymap "|" 'smart-operator-self-insert-command)
        ;; (define-key keymap "!" 'smart-operator-self-insert-command)
        ;; (define-key keymap ":" 'smart-operator-:)
        ;; (define-key keymap "?" 'smart-operator-?)
        (define-key keymap "," 'smart-operator-,)
        ;; (define-key keymap "." 'smart-operator-.)
        keymap)
      "Keymap used my `smart-operator-mode'.")
    

    See also a nice discussion on R styles here.

    [edit] I am also using the defacto camelCase style of R code for globals. The underscore-separated names for local variables - it's easy to differentiate.

    There is a special subword mode in emacs which redefines all editing and navigation commands to be used on capitalized sub-words

    (global-subword-mode)