reddit / emacs
What construct can be used to group a number of commands for execution without, like eval-region but acting on a sexp?
RembrandtWasAnegro · 2022-06-29 10:14
Say I have a group of commands I want to execute, but not use in an `eval region`.
I want to place them in a their own brackets and execute them, with C-x C-e`?
eg say I have these commands
(a b c)
(d e f)
(g h i)
What construct `z` do I have to place them in so I can place the cursor at the end and do a `C-x C-e`?
(z
(a b c)
(d e f)
(g h i))
lichtbogen · 2022-06-29 10:22
[progn](https://www.gnu.org/software/emacs/manual/html_node/elisp/Sequencing.html)
RembrandtWasAnegro · 2022-06-29 10:57
I know of `progn` but if `progn` appears in a required file it is executed automatically, unlike a `defun` which is just compiled.
pwnedary · 2022-06-29 11:21
Sounds like an XY problem.
nandryshak · 2022-06-29 11:31
Yes, that's what you asked for. progn (and friends) is the perfect answer to your question. How bout:
(defun name ()
(progn
(a b c)
(d e f)
(g h i)))
Since you want to be able to do `C-x C-e` on it, if you don't want it to be executed when required, it cannot be at the top level of the file, unless you do something silly like:
(message "This gets evaluated")
(with-current-buffer (current-buffer)
(goto-char (point-max)))
(message "This does not")
(progn
(a b c)
(d e f)
(g h i))
So, like the commenter below suggested, this seems like a case of the [XY Problem](https://en.wikipedia.org/wiki/XY_problem). What are you *actually* trying to achieve?
RembrandtWasAnegro · 2022-06-29 12:02
I have described exactly what I want, a construct with allows me to execute a sequence of commands encapsulated in a list interactively, without using `eval-region`.
Is there any such construct at all, something meant for interactive use?
nandryshak · 2022-06-29 12:21
> I have described exactly what I want, a construct with allows me to execute a sequence of commands encapsulated in a list interactively, without using eval-region.
Okay, well you got your answer: progn. But we're obviously still missing information here, because you didn't like that answer.
minorcontribution · 2022-06-29 12:56
Okay, you keep taking about interactivity, do you mean you want a `progn` that only runs the code when it's run interactively as opposed to by sourcing a file?
lichtbogen · 2022-06-29 13:58
You can put the expressions that you don't want automatically executed in a different file, which is not loaded.
You can comment them out.
You can even use something like this:
(when nil
(progn
...))
Or maybe something else, depending on what you're trying to acheive.
[deleted] · 2022-06-29 16:08
[deleted]
RembrandtWasAnegro · 2022-06-29 16:09
Exactly
quantum_mattress · 2022-06-29 20:13
I think your first solution with defun is almost what the OP wants but with the function declared as interactive.
alessandrobertulli · 2022-06-30 08:11
Ok, then I think you must resolve to use a `defun` with `(interactive)`. I am not aware of any other way to execute code as a command. Note also that, inside a defun, code is not EVALUATED at load time, it is INSTALLED, meaning it is saved for future use. So I think your best go is still
```
(defun ()
(interactive)
(progn
(a b c)
(d e f)))
```
Btw, since this case is so common, you can actually avoid the `prog` altogether, and Emacs will group the commands for you
accoil · 2022-07-09 18:44
I sometimes use quote to have a scratch pad that does not evaluate unless I move my cursor into a child sexp, and evaluate that manually:
'(
(progn
...)
)
If you just want some code that does not run when the file is loaded, you could probably use
(unless load-file-name
...)
this takes advantage of the fact that `load-file-name` is only populated when emacs is in the process of loading and executing a file. When visiting the file, it is not populated.