1

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!

Drew
  • 29,895
  • 7
  • 74
  • 104
Allen Gooch
  • 403
  • 1
  • 3
  • 13
  • I'm not familiar with npm, but in general, I'd try `compile`, possibly a derived version of it. For a fairly simple example of a derived compile mode, look at https://github.com/PyCQA/pylint/blob/master/elisp/pylint.el. Also, see http://stackoverflow.com/a/11059012/245173 to automatically bury (or kill) a compile buffer that was successful. – jpkotta Jul 04 '16 at 03:53

1 Answers1

1

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).

juanleon
  • 9,220
  • 30
  • 41