Emacs SCEL code block evaluation

Hi,

I started a few weeks ago to use the fantastic SuperCollider mode for Emacs (with Doom Emacs). I’m using SC mostly for live-coding using ProxySpace and Pbinds. Used in parallel with yasnippet, I can quickly summon pre-cooked functions and utilities.

Still, I never found a way to replicate the “code block” eval functionality found in the vanilla ScIDE : surrounding a piece of code with parenthesis -> eval. I’m currently evaluating code by painfully selecting an area of text and pressing C-c C-c (eval region-or-line). Apart from being really slow, this method is also pushing the cursor to the end of the evaluated block, which is really not that good for quick live-coding edits.

Is it a problem with Doom-Emacs or have I overlooked something in the SECL documentation? I can see the C-c C-d (sclang-eval-region) keybind but it feels like it fails at selecting any kind of text region.

More generally speaking, what is your workflow to take the best out of this Emacs mode?

EDIT: I found a workaround to my problem using Vim macros. It works for continuous blocks of text without any empty empty middle line. All there is to do is to register a small macro.

  1. register a keyboard macro using the following command: :qa vap C-c C-c q.
  2. the macro is stored in @a. Type @a to summon it. Replace a with any desired letter from a-z in the preceding command if you wish to.

The only problem is that you cannot have any white line in the Pbinds or SynthDefs you write.

3 Likes

Having the same issue, seems to me like scel fails at finding and matching parentheses, and then evaluating everything enclosed.
Like Bubo above the behavior I expected is:

  • When the cursor is inside a block defined with parentheses, scel would look above and below the current line, find outermost enclosing parentheses and evaluate all enclosed code. (IDE behaviour?)

I have tried to pinpoint how the scel functions work, but behavior is all over the place… It is hard to reliably troubleshoot, but:

  • With sclang-eval-region-or-line, scel evaluates the line, unless code is selected (visual mode with evil)
  • With sclang-eval-region, moves from the current line downwards, and evaluates. This means that if the cursor is placed on the opening parenthesis, the code block is evaluated. However, this only seems to work on the first code block in a given file(!). The second or third code block would not work.
  • While testing this, the behavior seems to break down further: suddenly, evaluating the second code block gives me the expected return of the first block, while the first evaluates to nil, and the third throws an error…

With this behavior, it seems like scel currently is restricted to evaluation of either single lines or visually selected blocks of code?

EDIT: There is an issue open on github that mirrors this question: https://github.com/supercollider/scel/issues/29

I am curious if anyone is using scel with an Emacs config other than doom that could give some indication whether this might be an issue with doom?

1 Like

Ok, having looked at the code in sclang-interp.el, my conclusion is that code block evaluation currently is not implemented in scel.

The code for sclang-eval-region seems to be explicitly for the Emacs concept known as the region. After some reading, my understanding is that “region” is more or less the same as “visual selection”. At least on my part, this issue might be one of terminological confusion.

sclang-interp.el, line 556:

 (region-beginning) (region-end)

If this is the case, it would be nice to have some pointers on how the IDE (and SCNvim, afaik?) implements the code block evaluation feature, in order to get it working in Emacs.

EDIT: Yep, the documentation over at sccode is more clear (I am a new Emacs user!):

C-c C-d (C-c C-c)
Interpret Selection (only if transient-mark-mode is on)

And I think I figured it out – it should be sclang-eval-defun (C-M-x)…

Seems to work exactly like Cmd+Return in the IDE.

3 Likes

Just digress a little bit, personally I feel that it could be more intuitive to have region highlighting when evaluating a code block, so I wrote a very simple wrapper Emacs package based on pulse package for this purpose. Feel free to check this out: sclang-helper. I didn’t make the parentheses matching pretty well, but it works most of the time :slight_smile:

as user of emacs for almost 10 years, i have my ctrl mapped to capslock key (some advise against that though), and the keybinding to eval whole block - C-M-x (control + alt + x) is now involuntary muscle memory to a degree that in SCIDE I keep making wrong key combinations (undo - C-/) is even worse. (i need to use scide when I’m showing others - I teach - skill-share - a small beginners group online).