3

When I open an R file in ESS-mode, I'd like emacs to setup my panes in a certain format.

enter image description here

To this end, I've added the following to my init.el.

(add-hook 'ess-mode-hook
          (lambda()
            (split-window-horizontally)
            (other-window 1)
            (vterm)
            (R)
            ))

When I run emacs myfile.R, emacs opens and prompts me to specify a project directory, which I do. This results in the vertical split on the right, which gives me pretty much what I show above. Awesome! But there is one problem. While myfile.R is opened as a buffer, the left-hand pane actually shows the scratch buffer. Why is that and how do I avoid it?

Dan
  • 191
  • 5
  • 1
    There are too many factors/variables that could affect the layout when Emacs starts, and I couldn't possibly list even half of them in this comment. Rather than trying to deal with all of that, how about beginning your `lambda` with something like `(delete-other-windows)` [to return the layout to just one visible window] and then go from there? Eventually, there are more precise functions you may wish to use for specifying a window-layout, but the ones you've chose are what most beginners use and I've even seen some users with more experience still post answers using those same functions. – lawlist Oct 23 '20 at 00:00
  • ... After `(delete-other-windows)`, you may wish to consider following up that command with something like `(set-window-buffer (selected-window) (find-file-noselect FILE 'nowarn))` or replace `(find-file-noselect FILE 'nowarn)` with `BUFFER-OR-NAME`, which may be known or can be obtained if need be. – lawlist Oct 23 '20 at 00:15
  • Thanks for the help, @lawlist – it's much appreciated. Following your advice, it works if I hard code BUFFER-OR-NAME as `myfile.R` (as per my example). Is there a way to figure out what the passed filename is programmatically? Perhaps pulling it from `buffer-list` or `command-line-args` somehow... (First day using Elisp, so I'm still finding my feet.) – Dan Oct 23 '20 at 00:27
  • 1
    If I launch Emacs 27 from the terminal with `/absolute/path/to/preferred/version/of/emacs -nw foo.txt`, then `(car (last command-line-args))` returns `"foo.txt"`. – lawlist Oct 23 '20 at 01:03
  • @lawlist Perfect! Do you want to post that as an answer so that I can accept it? – Dan Oct 23 '20 at 01:32
  • I'm glad that I was able to provide you with one viable solution. I'm unaware of a single correct answer, but would nevertheless prefer to wait a day or so to see if someone else has a better idea than me .... – lawlist Oct 23 '20 at 01:49
  • For the record, I had to tweak the solution a smidge to extract the file name if the file is given with a path (e.g., `emacs R/myfile.R`). Currently, it looks something like this: `(set-window-buffer (selected-window) (file-name-nondirectory (car (last command-line-args))))`. | – Dan Oct 26 '20 at 13:41

0 Answers0