0

I made a switch to Mac and had to ditch my favorite R Editor (Notepad++). Now I am trying to learn Emacs. The learning curve is steep, but this seems like the editor for R once I learn to use it. I am using OS X 10.8.3 and Aquamacs 2.4

I am trying to set custom key bindings following excellent tips from these SO questions: 1,2,3.

If I copy this command to preferences.el file, Aquamacs opens without complaining and the command works:

(global-set-key [C-tab] 'other-window)

However, if I try to set one of the following options:

(define-key ess-mode-map (kbd "C .") 'ess-eval-paragraph)
(define-key ess-mode-map [C-.] 'ess-eval-paragraph)
(define-key ess-mode-map "\C-." 'ess-eval-paragraph)

Aquamacs opens and complains, but the command works:

An error has occurred while loading `~/Library/Preferences/Aquamacs Emacs/Preferences.el (or .elc)':

Symbol's value as variable is void: ess-mode-map

To ensure normal operation, you should investigate and remove the cause of the error in your initialization file. Start Emacs with the `--debug-init' option to view a complete error backtrace.

I want to use Ctrl + . to run paragraphs of R code/buffer to R. Obviously I am doing something wrong. As I am a complete newbie to Mac and Emacs, this feels overwhelming. What is going on and how can fix this issue?

Community
  • 1
  • 1
Mikko
  • 7,530
  • 8
  • 55
  • 92

1 Answers1

4

You can't modify ess-mode-map before it exists. It doesn't exist until ess (specifically ess-mode) has been loaded. So something like the following should work:

(eval-after-load "ess-mode"
  '(define-key ess-mode-map (kbd "C-.") 'ess-eval-paragraph))

EDIT: note that C-. is not in the ascii character set, so the "\C-." string will not work. You have to use the (kbd "C-.") form instead. The string would work for other combinations, like "\C-a" (not that you should bind this to C-a). See the the manual for details.

Tyler
  • 9,872
  • 2
  • 33
  • 57
  • Thank you for your answer. With this piece of code it complains: "error: Invalid modifier in string". Maybe the code is not entirely correct? – Mikko May 08 '13 at 05:25
  • Hmm. This is weird. If I define the key binding either as [C-.] or (kbd "C ."), the program starts normally without complaining, but the keyboard shortcut is not defined. – Mikko May 08 '13 at 05:52
  • Following works: `(eval-after-load "ess-mode" '(define-key ess-mode-map (kbd "C-.") 'ess-eval-paragraph))`. I don´t know why the other key binding opinions do not work. Maybe I did something wrong somewhere or maybe it´s because of my Norwegian keyboard. Could you update this alternative to your answer, please? I will mark it as accepted. – Mikko May 08 '13 at 08:57
  • I figured out why my first answer didn't work and added the explanation above. – Tyler May 10 '13 at 15:45
  • Say that I want the same key binding, but with different commands, for .tex and .rnw files. Is still correct to use: `eval-after-load` or should I use hooks to change the command when the buffer mode/file changes? – antonio Jun 02 '13 at 17:02