How to change smart assign key ("_" to "<-") binding in ESS - r

In emacs ESS, how do I correctly change the keybinding for ess-smart-S-assign?
What I tried is adding
(custom-set-variables
'(ess-smart-S-assign-key ":"))
to my .emacs, but that made weird things happen: When I press :, just a normal : appears. On the other hand, pressing _ once yields <- as usual, whereas pressing _ a second time then converts this to :.
The desired behavior would be to be able to use _ as a normal key, with : being converted to <-.
I am using the official emacs 24.3 for windows and the latest development version of ESS (14.06).

Here's the docstring for ess-smart-S-assign-key:
Documentation:
Key used by `ess-smart-S-assign'. By default bound to
underscore, but can be set to any key. If this key is customized,
you must add
(ess-toggle-S-assign nil)
(ess-toggle-S-assign nil)
after the line that sets the customization and evaluate these
lines or reboot emacs. The first call clears the default
`ess-smart-S-assign' assignment and the second line re-assigns
it to the customized setting.
So: put this in your .emacs file to get the desired behavior:
(setq ess-smart-S-assign-key ":")
(ess-toggle-S-assign nil)
(ess-toggle-S-assign nil)
Kind of ugly, but it works.

The accepted answer didn't work for me, but the following did:
(global-set-key (kbd "C-;") (lambda () (interactive) (insert " <- ")))
(ess-toggle-underscore nil)
Insert your shortkey choice instead of C-;.

Another solution is
(eval-after-load "ess-mode" '(define-key ess-mode-map (kbd "C-;") "<-"))
(eval-after-load "ess-mode" '(define-key inferior-ess-mode-map (kbd "C-;") "<-"))
This allows to restrict the binding change to ess-mode. Note that the second line defines the binding for the inferior R process.

Related

How do I get ESS to recognize a dynamic prompt in R?

I followed the suggestion here... R: Display a time clock in the R command line to make my R prompt display the last time I entered in a command. But now M-r and M-p are broken because ESS is no longer recognizing my constantly-changing command prompt as the start of a command-line.
Does anybody know what option to change so that for all purposes where a prompt must be detected, instead of matching the whole prompt string, ESS will match just the constant tail end of it, or better yet, a regexp like "^[0-9]{6} [0-9]{2}:[0-9]{2} >"
Thanks.
My ess/ess-custom.el contains the following lines:
;; does it make sense to customize here, as we currently set this *directly*
;; in the FOO-BAR-cust-alist's ???
;; VS: Right. It only confuses users. It should be set in post-run-hook if
;; desired; inferior-S-prompt should be customized instead.
(defvar inferior-ess-primary-prompt "> "
"Regular expression used by `ess-mode' to detect the primary prompt.")
(make-variable-buffer-local 'inferior-ess-primary-prompt)
;; (setq-default inferior-ess-primary-prompt "> ")
(defvar inferior-ess-secondary-prompt nil
"Regular expression used by ess-mode to detect the secondary prompt.
(This is issued by S to continue an incomplete expression).
Set to nil if language doesn't support secondary prompt.")
;; :group 'ess-proc
;; :type 'string)
(make-variable-buffer-local 'inferior-ess-secondary-prompt)
;; (setq-default inferior-ess-secondary-prompt "+ ")
;; need to recognise + + + > > >
;; and "+ . + " in tracebug prompt
(defcustom inferior-S-prompt "[]a-zA-Z0-9.[]*\\([>+.] \\)*[+>] "
"Regexp used in S and R inferior and transcript buffers for prompt navigation.
You can set it to \"[]a-zA-Z0-9.[]*\\(> \\)+\" if you want to
skip secondary prompt when invoking `comint-previous-prompt'.
"
:group 'ess-proc
:type 'string)
So I'd hope that customizing either of these variables will help. Probably starting with the customizable one, i.e. inferior-S-prompt.

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

ESS: ess-request-a-process defaults to "S"

Quite often I find myself with bunch of R processes running in ESS buffers. There's a convenient Lisp function ess-request-a-process that asks for R process, and brings it to front. The only downside is that it somehow defaults to S, so each time I'm about to make a switch, I have to type R, ad nauseam.
I tried customising the ess-language variable, but even if I set value to "R", i.e. 4 for current session, or even if I save settings for future session, as soon as I type C-c C-k, automagically S appears once again. It's very annoying, and I really don't want to end up with C-x C-b and then C-s for desired R session! =)
I even tried setting (setq-default ess-language "R") in .emacs, but with no luck...
BTW, I'm running Emacs v. 23.1.1 on Linux Mint and Emacs v. 23.2 on Arch Linux, with ESS v. 5.12. If that's relevant, I run Emacs from terminal with -nw argument. Here's my .emacs:
;; start server
(server-start)
;; load ESS
(require 'ess-site)
(require 'ess-rutils)
;; set HTML help as default
(setq inferior-ess-r-help-command "help(\"%s\", help_type = \"html\")\n")
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(ess-help-kill-bogus-buffers t)
'(ess-rutils-keys nil)
'(show-paren-mode t))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
(put 'upcase-region 'disabled nil)
So... how to set R once and for all? (I don't use S/S+/SAS)
I did not know about this function so far. C-c C-k is bound to ess-force-buffer-current in ESS buffers.
[edit: C-c C-k is indeed bound to ess-request-a-process in iESS, in ESS it's ess-force-buffer-current]
In any case the variable you have to customize is ess-dialect
(setq-default ess-dialect "R")
It's buffer-local variable and some other stuff in ess-mode-hook might set it a different value.
Check it in each buffer with C-h v ess-dialect
Additionally, if you already running several processes then ess-switch-process (C-c C-s) might be the right way to go.
[edit: it will not jump to a process but just reset the associated process of the current ESS buffer]
[edit: After dwelling deeper on the issue it turned out that ess-request-a-process uses ess-language variable were the ess-dialect seems to be more appropriate. The problem is that each time an ess-inferior process starts it resets the global value of ess-language. This is why setting it in your case didn't work.
Here is a quick fix:
(defun ess-set-language ()
(setq-default ess-language "R")
(setq ess-language "R")
)
(add-hook 'ess-post-run-hook 'ess-set-language t)
]

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