Emacs ESS Indentation for Scoped R functions - r

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)

Related

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

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

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.

Emacs, Auto Complete Mode, CSS, pain. (illustrated!)

I've got Auto Complete Mode installed for Emacs.
First: When I'm typing declarations I get the normal auto-complete behavior:
So I hit Tab to complete — no problem. But then I hit ;:
It instantly tries to complete something! And I can't hit Enter because that'll accept the erroneous completion!
So I have to hit C-j. What a pain.
Second: Once I'm done with a declaration, I type }:
...but it doesn't get indented properly unless I type Tab.
What gives?
Update, settings:
I'm using Emacs 23. My css-electric-keys are } and ;. My Auto Complete configuration is as follows:
(ac-config-default)
(setq ac-auto-start t)
(setq ac-delay 0.1)
(setq ac-auto-show-menu nil)
(setq ac-show-menu-immediately-on-auto-complete t)
(setq ac-trigger-key nil)
Here's a few suggestions:
(setq ac-auto-start t) starts autocomplete automatically. If you change that to (setq ac-auto-start 1) (or 2 or 3) then it will only start after that many characters have been typed. This might not solve your problem though if after you type the ;, it considers the entire preceding word as part of the current auto-complete search.
Maybe the problem is that it isn't recognizing the semicolon as a delimiting character (like whitespace), so it thinks you're still adding to the last word. Perhaps adding the semicolon string to ac-ignores would do the trick? (Not sure what the syntax for that would be)
Maybe you can prevent auto-completion via the enter key by adding: (define-key ac-complete-mode-map "\t" 'ac-complete) and (define-key ac-complete-mode-map "\r" nil). I'm not sure how this will interact with DWIM though (enabled by default).
Try adding semicolon as an auto-complete key?
My .emacs knowledge on a scale of 0 to 10 is like a 1.5, but maybe this will jog some better ideas.
Old stuff I know, but try the following:
(add-hook 'css-mode-hook
(lambda ()
(make-local-variable 'ac-ignores)
(add-to-list 'ac-ignores ";")))
From the manual

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

Scheme Coding Style Questions

I am confused about the Scheme style for my code.
Should I format if forms as:
a.
if()
()
()
or b.
if () ()
()
or c.
if () () ()
Should I format cond clauses as
a.
cond ()
()
or b.
cond
()
()
When do I use a single ; to comment and a double ;;?
Here is a Lisp style guide, and here is a recommended commenting style.
If you have an emacs style editor, typing C-M-q within your s-expression should format it for you; it will get you correctly formatted code if your line breaks are reasonable (and editor configuration for indent-alist hasn't been munged too badly).
To fill in Doug's answer for your specific questions:
(if test
then
else)
(cond
(test1 exp1)
(test2 exp2)
(else exp3))
Or, for conds with long series of expressions:
(cond
(test1
exp1
exp2)
(else
exp3
exp4))
Comment conventions are a little looser. When I am writing careful code, I do something like this:
;;; new section ;;;
;;; section comments
(define (f g . x)
"docstring goes here"
;; in-function comments
(g x)) ; trailing line comment
But the exact boundaries for ; vs ;; usage vary. In particular, some people (including me) do not much like trailing line comments and will instead use ; for in-function comments and ;;; for section comments.
Have a look at Peter Norvig's "Tutorial on Good Lisp Programming Style" though you would have found the answer to your particular question in any Scheme/Lisp book.

Resources