tags:

views:

39

answers:

2

Hello Emacs Gurus, I use ESS-Emacs to edit my R scripts.

Whenever I load a R script it is always followed by:

  1. C-x 3 (I prefer this split)
  2. M-x R (Opens R)

Is there a way to automate steps 1. and 2. every time I type on the terminal:

emacs misc_r_file.r

Just to make myself clear - I don't want either 1 or 2 if I have already opened a R script in emacs. Steps 1 and 2 should be executed only when I open a fresh emacs process.

Thank you for your help in advance.

A: 

Is there a way to do this? Of course, it's just programming.

(defvar r-file-loaded-p nil
   "non-nil if an R file has been opened")

(defun maybe-setup-r ()
   (when (not r-file-loaded-p)
       (split-window-horizontally)
       (R)
       (setf r-file-loaded-p t)))

(add-hook 'r-mode-hook #'maybe-setup-r)

Why don't you persist your Emacs session, though?

jrockway
Thank you very much for your answer.
suncoolsu
A: 

You might be interested in this code by FelipeCsaszar which uses Shift-Enter to do what you want upon loading an R file, plus a little more besides once it's loaded ( If R is running and a region is highlighted, shift-enter sends the region over to R to be evaluated. If R is running and no region is highlighted, shift-enter sends the current line over to R. Repeatedly hitting shift-enter in an R file steps through each line (sending it to R), skipping commented lines. The cursor is also moved down to the bottom of the R buffer after each evaluation.)

;; Use shift-enter to split window & launch R (if not running), execute highlighted
;; region (if R running & area highlighted), or execute current line
;; (and move to next line, skipping comments). Nice. 
;; See http://www.emacswiki.org/emacs/EmacsSpeaksStatistics,
;; FelipeCsaszar. Adapted to spilit vertically instead of
;; horizontally. 

(setq ess-ask-for-ess-directory nil)
  (setq ess-local-process-name "R")
  (setq ansi-color-for-comint-mode 'filter)
  (setq comint-scroll-to-bottom-on-input t)
  (setq comint-scroll-to-bottom-on-output t)
  (setq comint-move-point-for-output t)
  (defun my-ess-start-R ()
    (interactive)
    (if (not (member "*R*" (mapcar (function buffer-name) (buffer-list))))
      (progn
    (delete-other-windows)
    (setq w1 (selected-window))
    (setq w1name (buffer-name))
    (setq w2 (split-window w1 nil t))
    (R)
    (set-window-buffer w2 "*R*")
    (set-window-buffer w1 w1name))))
  (defun my-ess-eval ()
    (interactive)
    (my-ess-start-R)
    (if (and transient-mark-mode mark-active)
    (call-interactively 'ess-eval-region)
      (call-interactively 'ess-eval-line-and-step)))
  (add-hook 'ess-mode-hook
        '(lambda()
           (local-set-key [(shift return)] 'my-ess-eval)))
  (add-hook 'inferior-ess-mode-hook
        '(lambda()
           (local-set-key [C-up] 'comint-previous-input)
           (local-set-key [C-down] 'comint-next-input)))
  (require 'ess-site)

The split done in this code is the C-x-3 one you want --- I always forget that in Emacs horizontal/vertical splitting is backwards from how many people (including me) understand it, referring to the direction the split line "moves in" from and not the orientation of the split line itself.

Kieran
Thank you very much! I have it in my .emacs now !
suncoolsu