1

I'm automating some work tasks, and many of them consist of calling kubectl edit on different objects.

kubectl edit basically opens your $EDITOR which I have configured to be emacsclient.

My question is, how can I, from elisp run a command like kubectl edit that consists on a blocking call (waits for completion) to emacsclient, have all the edits I need done with emacsclient and then continue the execution of the elisp function?

Or maybe, instead of using $EDITOR to emacsclient can I set $EDITOR to something like: "the current running Emacs instance"?

I've found this answer that seem to be similar to my problem. But I wanted to know if there's a way for Emacs to understand that a command is calling $EDITOR and use the current instance, instead of doing it through emacsclient.

Drew
  • 75,699
  • 9
  • 109
  • 225
licorna
  • 207
  • 1
  • 8
  • 1
    I think that Magit does something similar, with git. I think it start the external command in async mode, let it run `emacsclient`. It triggers Emacs to open a commit window. When the user is done, `emacsclient` is notified so that the external process can exit. (Reservation: I'm not an expert on Magit, so my understanding might be oversimplified, or even plain wrong.) – Lindydancer Mar 13 '22 at 10:49
  • https://emacs.stackexchange.com/tags/elisp/info – Drew Mar 13 '22 at 16:38

1 Answers1

3

You're looking for the with-editor library (which is what @Lindydancer is alluding to in the comment about Magit).

https://elpa.nongnu.org/nongnu/with-editor.html

It provides the commands with-editor-async-shell-command and with-editor-shell-command, which are intended as replacements for async-shell-command and shell-command. They automatically export $EDITOR making sure the executed command uses the current Emacs instance as "the editor".

Example:

(defun my-crontab-edit ()
  "Edit crontab."
  (interactive)
  (require 'with-editor)
  (with-editor-async-shell-command "crontab -e"))
phils
  • 48,657
  • 3
  • 76
  • 115