Can Emacs execute a command in the background?

I have an Emacs command, for example:

(defun query-lisp (word) (interactive "sType a word to query:") (message "%s" (query word))) 

The query operation is a time-consuming operation. When this command is run, Emacs locks the entire frame. Is there a way to get Emacs to run this command in the background or to block only one window, such as a minibuffer?

+7
source share
3 answers

Emacs has no threads. For lengthy operations, you can break the task into pieces and execute the pieces in idle timers , so emacs can respond to user input between pieces.

Another possibility runs it as an external process from which emacs accepts output.

+8
source

If you plan to use an external process (which you indicated in a comment on another question), you can use the following:

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

This will return the process object. You can either send the output to the buffer, or you can attach a filter function to a process. In the latter case, it is a function that is called every time your process emits any output.

In addition, you can attach a watch function to your process. This function is called every time the state of your process changes, it is useful to find out when it exited.

Emacs source code provides several examples, one of which is compile.el .

+10
source

For example, you can use something like this

 (shell-command "sleep 10 && echo 'finished' &") 

The result will be displayed in the *Async Shell Command* buffer.

+4
source

All Articles