How can I make ESS to split window horizontally by default? - r

I always prefer horizontally splitting because the screen has more horizontal space.
In python-mode I can achieve this by setting
(py-split-windows-on-execute-function (quote split-window-horizontally))
Is there something similar in ESS mode?

I don't know if ESS has anything mode-specific. From the help pages, however, it looks like split-window-preferred-function defaults to split-window-sensibly, which in turn determines how to split a window based on split-width-threshold and split-height-threshold. Setting the former to nil forbids a horizontal split, and the latter to nil forbids a vertical split. These settings would be global; you could put (setq-local split-height-threshold nil) in your ess-mode-hook.
Edited/extended to reflect #qed's answer. You might consider packaging the local bindings in a function rather than in a lambda to give yourself the option of removing the function from the hook.
(defun forbid-vertical-split ()
"Only permit horizontal window splits."
(setq-local split-height-threshold nil)
(setq-local split-width-threshold 0))
(require 'ess-site)
(add-hook 'ess-mode-hook
'forbid-vertical-split)

This seems to do the trick:
(require 'ess-site)
(add-hook 'ess-mode-hook
(lambda()
(setq-local split-height-threshold nil)
(setq-local split-width-threshold 0)
))
Kudos to Dan!

accepted answer did not work for me, but adding
(setq split-height-threshold 0)
to .emacs did

Related

Emacs ESS Indentation for Scoped R functions

I currently use emacs ESS and recently agreed to use an internal style guide that uses 2 space indenting. I added the following to my .emacs file:
(defun myindent-ess-hook ()
(setq ess-indent-level 2)
)
(add-hook 'ess-mode-hook 'myindent-ess-hook)
And everything was going fine. When I defined a new function, it would nicely indent 2 spaces. Additionally when I call a function and break the call onto multiple lines, the spacing is also nicely indented with 2 spaces:
x <- function(){
mean(
c(2,3)
)
}
However, if I scope the mean function with ::, the indentation gets messed up:
x <- function(){
base::mean(
c(2,3)
)
}
My guess was that this had something to do with trailing white space, but even after nuking trailing whitespace via:
(defun myindent-ess-hook ()
(setq ess-indent-level 2)
(setq ess-nuke-trailing-whitespace t)
)
(add-hook 'ess-mode-hook 'myindent-ess-hook)
The issue persists - has anyone encountered this issue before?
My stab in the dark, likely to work for your posted example yet you might dislike the effects in other situations. See describe-variable on ess-offset-arguments-newline (default value: prev-call) for more options, and try:
(defun myindent-ess-hook ()
(setq ess-indent-level 2)
(setq ess-offset-arguments-newline '(prev-line 2))
)
(add-hook 'ess-mode-hook 'myindent-ess-hook)

Change auto indentation of comments [duplicate]

I am using the Emacs-Speaks-Statistics (ESS) mode for Emacs. When editing R code, any comment lines (those starting with #) automatically get tabbed to the far right when I create a new line above it. How should I change my .emacs.el file to fix this?
For example, I have:
# Comment
Now, after putting my cursor at the beginning of the line and pressing Enter, I get:
# Comment
Thanks for any hints.
Use '###' if you don't want the comments indented. According to the manual,
By default, comments beginning with
‘###’ are aligned to the beginning of
the line. Comments beginning with ‘##’
are aligned to the current level of
indentation for the block containing
the comment. Finally, comments
beginning with ‘#’ are aligned to a
column on the right (the 40th column
by default, but this value is
controlled by the variable
comment-column,) or just after the
expression on the line containing the
comment if it extends beyond the
indentation column.
Either
(setq ess-fancy-comments nil)
if you never want to indent single-# comments, or
(add-hook 'ess-mode-hook
(lambda ()
(local-set-key (kbd "RET") 'newline)))
if you want to change the behavior of Enter so it doesn't indent.
Setting ess-indent-with-fancy-comments to nil will remove the weird single-# indentation, but it must be set either buffer-locally in a hook (as in Rob's answer), OR before ESS is loaded:
(setq ess-indent-with-fancy-comments nil)
(require 'ess)
Other ways to make sure it is set before ESS is loaded, is to set it in M-x configure, or to set it in the :init section of use-package.
What's going on is that ESS defines styles at initialization in ess-style-alist, and then applies the default style in every buffer. So to make sure these styles respect ess-indent-with-fancy-comment, you must make sure to set it before the styles are defined.
Jouni's answer didn't work for me. But I found an approach here that does:
https://stat.ethz.ch/pipermail/ess-help/2016-May/010970.html
(defun my-ess-settings ()
(setq ess-indent-with-fancy-comments nil))
(add-hook 'ess-mode-hook #'my-ess-settings)

How to make the current prompt of R at the top of buffer in ESS just like Control + L in R console

I have tried to put the following in my .emacs file.
(defun clear-shell ()
(interactive)
(let ((old-max comint-buffer-maximum-size))
(setq comint-buffer-maximum-size 0)
(comint-truncate-buffer)
(setq comint-buffer-maximum-size old-max)))
(global-set-key (kbd "\C-x c") 'clear-shell)
It worked, but it also remove all the command I have typed before. So it is not what I want. I just want the current prompt > at the top of the buffer and not to delete any command I typed before.
Does anyone know?
For me Esc-0 Ctr-l seems to work.
`Ctrl-h k' output is:
C-l runs the command recenter-top-bottom,
which is an interactive compiled Lisp function in window.el'.
According to this page from the Emacs manual:
Scroll the selected window so the current line is the
center-most text line; on subsequent consecutive invocations,
make the current line the top line, the bottom line, and so on in
cyclic order. Possibly redisplay the screen too (recenter-top-bottom).
This seems to get the job done (though I am actually not really sure if this is what you are after):
(defun clean-shell ()
(interactive)
; if you call this from your .r script, it will switch to the next window
(when (eq major-mode 'ess-mode) (other-window 1))
(mark-whole-buffer)
(exchange-point-and-mark)
(move-beginning-of-line 1)
(delete-region (region-beginning) (region-end))
(end-of-line)
)
EDIT: Or maybe this?
(defun clean-shell ()
(interactive)
(when (eq major-mode 'ess-mode) (other-window 1))
(mark-whole-buffer)
(exchange-point-and-mark)
(move-beginning-of-line 0)
(delete-region (region-beginning) (region-end))
(end-of-line)
)
What is wrong with C-l C-l? It works in any buffer.

How to stop emacs from replacing underbar with <- in ess-mode

ess-mode is "Emacs speaks statistics." This mode is useful for editing programs for R or Splus (two separate statistics packages).
In my buffer, when ever I type _ the character is replaced with <-, which is very frustrating. Is there an emacs lisp statement to turn off this behavior?
emacs: 22.1.1
ess-mode release (unknown)
From ESS's manual (look under "Changes/New Features in 5.2.0"):
ESS[S]: Pressing underscore ("_") once inserts " <- " (as before); pressing underscore twice inserts a literal underscore. To stop this smart behaviour, add "(ess-toggle-underscore nil)" to your .emacs after ess-site has been loaded
Since the feature is useful. You can assign it to other key which is less used by you in R it will automatically unassign it from underscore. I personally assign it to ";" by adding following line in .emacs file.
(setq ess-smart-S-assign-key ";")
My version of emacs is 24.3 All-in-one installation file by Vincent Goulet.(Installed on windows 7)
hope this helps
Edit
In emacs 25.2 above do not work instead add following in the .emacs file
(setq ess-smart-S-assign-key ";")
(ess-toggle-S-assign nil)
(ess-toggle-S-assign nil)
A more recent version which seemed to work for me, and is a lot less verbose (you essentially keep normal underscores, but can set your own key for this smart behaviour!):
(global-set-key (kbd "C-;") (lambda () (interactive) (insert " <- ")))
(ess-toggle-underscore nil)
Insert your shortkey choice instead of C-;.
From http://www.r-bloggers.com/a-small-customization-of-ess/ and
How to change smart assign key ("_" to "<-") binding in ESS
To assign ":" to "<-" and to stop the assignment of underscore (underbar) "_" to "<-" put the following in .emacs (yes, the repeated line is correct)
(setq ess-smart-S-assign-key ":")
(ess-toggle-S-assign nil)
(ess-toggle-S-assign nil)
(ess-toggle-underscore nil) ; leave underscore key alone!
Like Michał Marczyk and this R mailing list thread suggested, add this line to ~/.emacs:
(ess-toggle-underscore nil)
Then reload it with M-x load-file and type ~/.emacs.
But if you load the file again, e.g. if you add another customization, then it toggles it back to the original state. So toggle it twice, the first one forcing it to the default:
(ess-toggle-underscore t)
(ess-toggle-underscore nil)
That being said, I like Drummermean's solution better, but it also reverts back to default if you add it to ~/.emacs and load it twice. So force a toggle to the default before:
(ess-toggle-underscore t)
(global-set-key (kbd "M--") (lambda () (interactive) (insert " <- ")))
(ess-toggle-underscore nil)
I bound the smart assignment to Opt-[minus] like RStudio (on a Mac).
As a follow-up on #mmorin answer. To set keybinding for the assignment operator the same way as in Rstudio add the following in your .emacs file
(ess-toggle-underscore t)
(ess-toggle-underscore nil)
(define-key ess-mode-map (kbd "M--") (lambda () (interactive) (just-one-space 1) (insert "<-") (just-one-space 1)))
(define-key inferior-ess-mode-map (kbd "M--") (lambda () (interactive) (just-one-space 1) (insert "<-") (just-one-space 1)))

Emacs ESS Mode - Tabbing for Comment Region

I am using the Emacs-Speaks-Statistics (ESS) mode for Emacs. When editing R code, any comment lines (those starting with #) automatically get tabbed to the far right when I create a new line above it. How should I change my .emacs.el file to fix this?
For example, I have:
# Comment
Now, after putting my cursor at the beginning of the line and pressing Enter, I get:
# Comment
Thanks for any hints.
Use '###' if you don't want the comments indented. According to the manual,
By default, comments beginning with
‘###’ are aligned to the beginning of
the line. Comments beginning with ‘##’
are aligned to the current level of
indentation for the block containing
the comment. Finally, comments
beginning with ‘#’ are aligned to a
column on the right (the 40th column
by default, but this value is
controlled by the variable
comment-column,) or just after the
expression on the line containing the
comment if it extends beyond the
indentation column.
Either
(setq ess-fancy-comments nil)
if you never want to indent single-# comments, or
(add-hook 'ess-mode-hook
(lambda ()
(local-set-key (kbd "RET") 'newline)))
if you want to change the behavior of Enter so it doesn't indent.
Setting ess-indent-with-fancy-comments to nil will remove the weird single-# indentation, but it must be set either buffer-locally in a hook (as in Rob's answer), OR before ESS is loaded:
(setq ess-indent-with-fancy-comments nil)
(require 'ess)
Other ways to make sure it is set before ESS is loaded, is to set it in M-x configure, or to set it in the :init section of use-package.
What's going on is that ESS defines styles at initialization in ess-style-alist, and then applies the default style in every buffer. So to make sure these styles respect ess-indent-with-fancy-comment, you must make sure to set it before the styles are defined.
Jouni's answer didn't work for me. But I found an approach here that does:
https://stat.ethz.ch/pipermail/ess-help/2016-May/010970.html
(defun my-ess-settings ()
(setq ess-indent-with-fancy-comments nil))
(add-hook 'ess-mode-hook #'my-ess-settings)

Resources