Comint: Writing your own Command Interpreter

Among the hallmarks of Emacs is its ability to interface with external procedures and increase the user experience using the entire coterie of Emacs performance like syntax highlighting, macros, command history, etc. This operation was in Emacs as time immemorial, however a few men and women create their own control interpreters — also called COMINT — in Emacs, stressing it is either very difficult or impossible.

It is not surprising men and women feel this way if you think about just how woefully underdocumented this operation is; the couple, scattered references to comint”style” reveal small, and as a hacker you’re made to utilize the source, Luke.

The Theory
Before I demonstrate the best way to use comint, a fast briefing — but tough, as I am leaving out a couple of dull measures — about how it functions.

In its heart, you’re nearing a procedure and redirecting the stdin, stdout and stderr pipes to Emacs; or you employ a pseudo-terminal. The selection between the two is regulated by process-connection-type, but it is not likely that you would want to change this manually.

The basic building blocks for interacting with procedures are start-process, for kinda-sorta-asynchronous procedure calls; andcall-process, for synchronous procedure calls.

1 layer above and we reach comint using its pared-down, fundamental interpreter frame. This is what matters such as M-x casing and the a variety of REPL modes such as Python build on. Comint has handles all of the nitty-gritty stuff like tackling input/output; a control history; fundamental input/output filter hooks; etc. To put it differently, it is the ideal point to build on in the event that you need something interactive but desire more than simply what comint has to offer you. To utilize comint is quite easy: comint-run requires a single argument, PROGRAM, and nothing else; conduct it using a filepath for your preferred app and see it fly. For increased configurability, you may use make-comint-in-buffer.

Significant caveat regarding pipe redirection: Sometimes apps will notice that you’re redirecting its pipes into a dumb terminal or document and it simplifies its own shell prompt; this can be very frustrating although not all apps detect it is running within Emacs by trying to find the signature environment factors Emacs will place: EMACS and INSIDE_EMACS. If this occurs you will get lucky and find a flag you can set to make it operate in”interactive” mode — for instance, in Python it is -I. 1 layer above and we get to things in Running Shells in Emacs: A Summary such as shell, which I have talked about.