emacs-ess / ESS

Emacs Speaks Statistics: ESS
https://ess.r-project.org/
GNU General Public License v3.0
622 stars 162 forks source link

How to get the old "_" behavior? (docs outdated?) #809

Closed fmichonneau closed 5 years ago

fmichonneau commented 5 years ago

I'm using ess-20190110.2156. The docs say:

Users who liked the previous behavior (i.e. underscore inserting “<-”) should bind ess-insert-assign to the underscore in their Emacs initialization file. For example, (define-key ess-r-mode-map "" #'ess-insert-assign) and (define-key inferior-ess-r-mode-map "" #'ess-insert-assign) will activate it in all ESS R buffers.

However if I do this, I get "Symbol’s value as variable is void: ess-r-mode-map" at startup.

lionel- commented 5 years ago

Do you need to (require 'ess-r-mode) before binding the key? Or put it in the :config section of use-package?

fmichonneau commented 5 years ago

I do use use-package. This below makes it work. Is this the correct approach?

(use-package ess
  :ensure t
  :bind
    (:map ess-r-mode-map
        ("_" . ess-insert-assign))
     (:map inferior-ess-r-mode-map
        ("_" . ess-insert-assign))
  :config
  (require 'ess-r-mode)
)
lionel- commented 5 years ago

I think you'd create a (use-package ess-r-mode) block instead.

lionel- commented 5 years ago

@jabranham Shouldn't this key be binded in ess-mode-map rather than ess-r-mode-map, since it's generic?

jabranham commented 5 years ago

@fmichonneau this should work:

(use-package ess-r-mode
  :bind
  (:map ess-r-mode-map
        ("_" . ess-insert-assign))
  (:map inferior-ess-r-mode-map
        ("_" . ess-insert-assign)))

The reason for the error you got is that you hadn't required ess-r-mode yet, which defines ess-r-mode-map.

@lionel- that was just meant as an example. The function (as of now, anyway) isn't /really/ generic since in any non-S-language buffer it just calls self-insert-command.

fmichonneau commented 5 years ago

@jabranham with just the code you included above I get the error message:

Error (use-package): ess/:catch: Symbol’s value as variable is void: ess-r-mode-map

jabranham commented 5 years ago

@fmichonneau Sorry, had a typo. See the update, which should work.

fmichonneau commented 5 years ago

Thanks that did it!

Note for others who might stumble upon this, if you were using :ensure t, it needs to be changed to :ensure ess.

solna86 commented 5 years ago

@jabranham @lionel- @fmichonneau Why is the previous behavior (underscore setting <-) no longer the default? Does R encourage to use = instead of <- as an assignment operator now?

If not, what is the preferred way of typing <- in ESS?

lionel- commented 5 years ago

We never encouraged using = instead of <-. Now we also don't discourage using underscores in variable names.

If not, what is the preferred way of typing <- in ESS?

You can bind ess-insert-assign to a key that suits you, including a single key like _, to reproduce the previous behaviour. Or use C-c C-= which calls the more featureful ess-cycle-assign (cycles through assignment operators rather than typed key).

mmaechler commented 5 years ago

Or -- what I now try to train myself to use {still have the "" working as ' <- ' and twice `as_` on one of two computers} is the

M-- i.e. [Alt] plus - which directly inserts the desired "space <- space". We had added that key combination quite a while ago ... almost simultaneously IIRC after talking with Rstudio people or seeing they use it by default.

lionel- commented 5 years ago

We had added that key combination quite a while ago

hmm, I see it iness-add-MM-keys but I don't think this is a default keybinding.

mmaechler commented 5 years ago

.... I'm time pressed already ... but was really unaware of that... I have told students and others about it, in the past ...

Would it be ok to move it to general? "cycle-assign" maybe a bit too sophisticated for people starting to use emacs and ESS just because they want to try using it for a "low weight" R IDE.

jabranham commented 5 years ago

I'm not sure we should override the default of M-- (negative-argument).

lionel- commented 5 years ago

Agreed we can't override default keybindings. Also I don't think the cycling version is too sophisticated, since beginners probably won't notice the cycling behaviour.

solna86 commented 5 years ago

The cycling behaviour of C-c C-= is nice, but I don't think it's a net win to type that key combination versus <-. It would be nice to have a standard default binding for ess-insert-assign given that it's such a ubiquitous operator and other R IDEs do have a shortcut.

lionel- commented 5 years ago

@solna86 We're open to suggestions. The keybinding should not override anything that's standard in Emacs, should not get in the way of typing code (like _), and should follow https://www.gnu.org/software/emacs/manual/html_node/elisp/Key-Binding-Conventions.html.

Even if we find something appropriate, it will probably be tailored to the American keyboard anyway, leaving all other users needing to define their own keybinding to comfortably type <-.

solna86 commented 5 years ago

@lionel- Thanks for the detailed explanation.

Perhaps binding it to M-- as @mmaechler suggested previously? While it is true that M-- is currently used by negative-argument, that function is also bound to C-- and C-M--. Not sure if that makes a difference.

jabranham commented 5 years ago

Some people prefer using C for prefix arguments, some M, and some C-M. I don't think we should inconvenience anyone if we can help it. It's trivial to bind ess-insert-assign to M-- in ess-r-mode-map for users who want it there.

We could bind it to C-c < I suppose?

mmaechler commented 5 years ago

I'd be really happy if we get M-- as ess-insert-assign in ess-r-mode-map Additionally bind it to C-c < is a nice idea too.

We should really entice ESS users to use either ess-insert-assign or ess-cycle-assign "all the time". Having it available in several ways may help getting there.

[BTW: Is ess-r-mode-map properly activated in polymode for *.Rd files in the '\examples{..}` section?]

vspinu commented 5 years ago

[BTW: Is ess-r-mode-map properly activated in polymode for *.Rd files in the '\examples{..}` section?]

Poly Rd mode wasn't working at all. It's fixed now and R-mode is activated in \usage as well. It broke after the rewrite and, as I am not using it and no one reported issues, it stayed unnoticed. I have added a few Rd specific tests to avoid such issues in the future.

rbchan commented 5 years ago

I couldn't get the options above to work, but putting this in my init file did the trick:

(require 'ess-r-mode)
(define-key ess-r-mode-map "_" 'ess-insert-assign)
(define-key inferior-ess-r-mode-map "_" 'ess-insert-assign)
plpxsk commented 1 year ago

For future reference, here is my snippet to enable M-- for assignment:

;; use M-- as assignment operator
;; search ess-s-lang.el for ess-add-MM-keys to get snippets for the following:
(eval-after-load "ess-mode"
  '(define-key ess-mode-map          [?\M--] #'ess-insert-assign)
  )
(eval-after-load "ess-mode"
  '(define-key inferior-ess-mode-map [?\M--] #'ess-insert-assign)
  )

(If this doesn't follow good/best practice, let me know)