4

I'm trying to run some code, a little like this:

(defun myfun ()
  (message "one")
  (user-does-things)
  (message "two")

I would like the code to do this:

  1. display "one"
  2. let the user edit in another buffer
  3. When the user is done editing, display "two"

All my attempts so far have just displayed "one" and "two", leaving the user to do his editing after the code has finished running.

I'm also OK with a more disconnected approach, such as "I launch a buffer with a custom keymap so the user is able to do whatever they want in emacs until they hit a keybinding which calls back to my custom code" - I could use some help to do this as well though :D

1 Answer 1

6

The question is not too clear. But I think maybe what you're saying is that you want to:

  1. Show message one.
  2. Let the user start doing some arbitrary editing, in any buffer. No limits on what she can do.
  3. Let the user indicate when she's done.
  4. Show message two.

If that, or similar, is the case, then one way to realizes steps 2 and 3 is to use a recursive edit.

A recursive edit can be initiated by program or interactively -- in either case using function/command recursive-edit.

And a user can exit the current recursive edit using C-M-c (command exit-recursive-edit). You can exit all levels of recursive editing, returning to the top-level of Emacs, using C-] (command abort-recursive-edit).

For a start, you could do this, for example:

(defun myfun ()
  (message "one")
  (save-excursion (recursive-edit)) ; User does things
  (message "two"))

Library rec-edit.el (code) can help with recursive editing.

  • It gives you a single key (C-M-c) to both enter and exit a recursive edit.
  • It optionally highlights the mode-line indication, [...], to make clear that you are in a recursive edit, and what the recursion level is.
3
  • To see an example of recursive edit, start a query-replace, and use C-r when prompted to accept a replacement.
    – Barmar
    Commented Dec 27, 2019 at 14:49
  • I have done this and it definitely allows me to move forward. It's worth mentioning that the emacs documentation indicates that one should avoid using recursive editing, though: gnu.org/software/emacs/manual/html_node/elisp/…
    – Trevoke
    Commented Jan 4, 2020 at 16:24
  • The Emacs doc that says that is too black-and-white. What it really wants to say (or should want to say) is that recursive editing can confuse users who don't know about it or don't expect it. And that's true. It's also true that its indication is not very noticeable - which is one reason for library rec-edit.el.
    – Drew
    Commented Jan 5, 2020 at 2:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.