9

How to make Emacs run a program and don't wait for output/respond? I tried to open a pdf in an external program:

(shell-command (concat "start sumatrapdf " (shell-quote-argument path) " -page " search))))

But it won't open another files until the existing sumatrapdf process is closed. I tried async-shell-command, but it opens a new buffer with Async output which I don't need.

What is the right way to open files in external programs?

Anton Tarasenko
  • 8,099
  • 11
  • 66
  • 91

2 Answers2

14

start-process function can handle that:

(start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS)

Start a program in a subprocess.  Return the process object for it.
NAME is name for process.  It is modified if necessary to make it unique.
BUFFER is the buffer (or buffer name) to associate with the process.

Process output (both standard output and standard error streams) goes
at end of BUFFER, unless you specify an output stream or filter
function to handle the output.  BUFFER may also be nil, meaning that
this process is not associated with any buffer.

PROGRAM is the program file name.  It is searched for in `exec-path'
(which see).  If nil, just associate a pty with the buffer.  Remaining
arguments are strings to give program as arguments.

If you want to separate standard output from standard error, invoke
the command through a shell and redirect one of them using the shell
syntax.

If you don't want to associate bufer with open process — pass nil as BUFFER argument

Victor Deryagin
  • 11,895
  • 1
  • 29
  • 38
  • Thank you, Victor. With your hint I have found a slightly different function (`start-process-shell-command`) because `start-process` didn't take arguments properly. – Anton Tarasenko Sep 04 '11 at 10:27
  • So, how to get the end of BUFFER and display it ? – zobi8225 Mar 01 '18 at 13:05
  • `start-process` did take arguments properly. You don't need to use `concat` for args. `(start-process "my-process" "foo" "ls" "-l" "/bin")`. See [Creating an Asynchronous Process](https://www.gnu.org/software/emacs/manual/html_node/elisp/Asynchronous-Processes.html) – azzamsa Jan 06 '19 at 06:37
4

See C-h k M-!

... If COMMAND ends in ampersand, execute it asynchronously. The output appears in the buffer `Async Shell Command'. That buffer is in shell mode. ...

IOW, M-! my_command --opt=foo arg1 arg2 & will start my_command and create a *Async Shell Command* buffer with my_command running in it but emacs will give control back to you right away.

Ross Patterson
  • 5,702
  • 20
  • 38
  • 1
    In my case Emacs shouldn't wait for any respond. Just open a file and forget about it (actually, it's for the org-mode and custom hyperlink type which would open pdf at the certain page). Anyway, thanks for your attention, Ross. – Anton Tarasenko Sep 04 '11 at 18:04