I have some elisp that runs an external 'npm' command.

(defun npm-mode-npm-run ()
  "Run the 'npm run' command on a project script."
  (interactive)
  (let ((command
          (completing-read
            "Run command: " (npm-mode--get-project-scripts))))
    (message "Running npm script: %s" command)
    (switch-to-buffer npm-mode--buffer-name command)
    (erase-buffer)
    (ansi-term (getenv "SHELL") "npm-mode-npm-run")
    (comint-send-string "*npm-mode-npm-run*" (format "npm run-script %s\n" command))))

While it does the job, when execution completes the user is left in a buffer that must be killed, and that requires additional confirmation to kill the process.

What I would like is once the program exits, I could press the 'q' key to do all of that, leaving the user in their original buffer.

Is their a good example of how to do this best for modern emacs that I could refer to, or any other specific docs that may be helpful?

Many thanks in advance!

1

There are 1 best solutions below

0
On

As @jpkotta said, compile is a good option. You can bury the buffer easily, send a TERM signal to the underlying process, etc. Also, you get for free error parsing (syntax coloring + the ability to jump to the offending line) for many languages.

Here is an example of how I use it (in this case to do a quick run of any script I am editing):

(defun juanleon/execute-buffer ()
  (interactive)
  (let ((compile-command nil))
    (compile buffer-file-name)))

Easy to modify to adapt to your code. The let is for avoid adding stuff to compile history (since I use compile a lot for regular compilation, and history is useful).