3
votes

I'm familiar with scheme, but new to emacs (switching over from VIM) and elisp.

I know how to do the following:

  • make a simple key binding
    • C-c iwb = indent whole buffer
    • F2 = turns folding on/off
  • use slime from emacs
  • some basic keys, like C-x 2, paredit keys, some basic movement keys

I need help doing something a bit more advanced:

I want F3 to equal:

  • put emacs into C-x 2 mode
  • in bottom window, switch to "slime-repl" buffer
  • in the "slime-repl" buffer, send the command "(test/run)" <-- note, this is meant to be sent to the swank server, NOT to elisp

I realize it's terrible form to ask people to write a script for me; however, if anyone could do that, I would learn rather quickly from it. [And it would allow me to do more complicated types of scripting through studying your example.]

Thanks!

2
You should note that C-x 2 is not a mode; it just calls the function split-window-below. Admittedly the distinction could be considered slightly arbitrary -- mode functions have the same capabilities as any other function, and global minor modes like menu-bar-mode affect frames rather than buffers -- but modes mostly provide behaviours to individual buffers, and by convention they always have a function name ending in -mode. Because modes are such an important part of Emacs, you'll avoid a little confusion if you don't refer to other functions using that terminology. - phils
Oh, and you should definitely work through the excellent built-in tutorial: C-h t. After that, you may find it helpful to search the Q&As here containing that term: stackoverflow.com/search?q=[emacs]+tutorial - phils
@phils: Perhaps this is a bit arrogant of me. Do you know of a good emacs scripting tutorial rather than a emacs using tutorial? I have ~ 10 years worth of vim_script behavior that I'd like to port to emacs, but so far largely unable to get even the most basics down. I think (but am not certain) what I really want is a high level overview of the emacs execution model. - user1311390
I'm not certain I know what you mean by that, but there are comprehensive manuals built into Emacs as well, including an Emacs Lisp Intro and a complete Elisp reference, as well as a manual for using the Editor and features. You can type C-h i to get to the info reader; take careful note of the key bindings listed at the top of the main contents page, and remember that d always takes you back there. - phils
emacswiki.org/emacs/LearningEmacs will likely be helpful to you. - phils

2 Answers

2
votes

This is not exactly what you want, but should be a good starting point for further tweaking:

(defun slime-run-test ()
  (interactive)
  (slime-interactive-eval "(test/run)")
  (slime-pop-to-buffer (slime-output-buffer) t))

(global-set-key (kbd "<f3>") 'slime-run-test)
1
votes

I don't use slime, but assuming it uses comint-mode then I would think the following might do the trick:

(defun my-slime-test-run ()
  (interactive)
  (delete-other-windows)
  (split-window-below)
  (with-selected-window (next-window)
    (switch-to-buffer "slime-repl")
    (goto-char (point-max))
    (insert "(test-run)")
    (comint-send-input)))

(global-set-key (kbd "<f3>") 'my-slime-test-run)

There is probably a better way to do this, but hopefully that gives you a little insight into how you can write elisp functions to carry out tasks in the editor (and note how the function reads very much like a set of editor instructions -- you can do a lot simply by converting the keystrokes you would use into equivalent code -- or even not writing code at all, and simply recording & saving keyboard macros).

Use C-hf name-of-the-function RET to get documentation on any of the function/macro calls in that function.

For the keybinding, I used C-hkF3 to check how Emacs referred to that key, and then used that string as the argument to kbd (and note how you can use that sequence to find the name of the function bound to any given key sequence, which you can then utilise in code if desired).

Many things are far less obvious if you don't already know them, but that's only to be expected with a code base as large as this (and dating back as long as this).

The great thing is that if you don't know what you're looking for, you can always search for function names matching patterns with C-uC-ha (and similarly for variables, values, libraries, and documentation; see M-: (info "(emacs) Apropos") RET for more about this facility). Plus the info manuals (complete with indexes -- press I or i within any particular manual, or use the info-apropos command to search all info manuals at once).

Truly one of the very best things you can do is to learn how to use the self-documenting nature of Emacs to find answers to the things you don't already know.