Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

recipe for building an emacs wrapper mode around a command line program?

Tags:

emacs

elisp

I want to play and experiment with a number of software tools, each of which will have a command line interface. Some of these tools include hbase, pig, erlang, and prolog. I want to use emacs as a UI to these tools the same way I can run a python shell with M-x run-python or a Lisp interpreter with ielm mode.

Is there a recipe I can follow to wrap one of these command line tools in an emacs mode? I'm looking for something which will display the tool's prompt, let me search through the history with C-c C-n/C-p, submit the current input to the tool's process which I hit Enter, and display the tool's output.

I know many of these tools probably have emacs modes already, I am interested in how to quickly build one if nothing exists.

like image 434
benhsu Avatar asked Dec 25 '11 04:12

benhsu


1 Answers

I recently built an "inferior gosu mode" for the gosu language. It actually turned out to be pretty simple: I just extended comint which is the mode on which both shell and ielm are based. Here is the important bit of the code:

(require 'comint)
(defun inferior-gosu-mode ()
  (interactive)
  (comint-mode)
  (setq comint-prompt-regex inferior-gosu-prompt)
  (setq major-mode 'inferior-gosu-mode)
  (setq mode-name "Inferior Gosu")
  (setq mode-line-process '(":%s"))
  (use-local-map 'inferior-gosu-mode-map))

The use-local-map bit is just where you define special key bindings; I have it as just a copy of the comint bindings:

(defvar inferior-gosu-mode-map nil)
(unless inferior-gosu-mode-map
  (setq inferior-gosu-mode-map (copy-keymap comint-mode-map)))

After this I had some simple code that defined a command to launch a process that would pop open the *inferior-gosu* buffer if it existed. I also added a bit of code to the normal gosu mode for opening an inferior-gosu shell.

In short: use comint.

Here's a link to the whole code, but there isn't much more to it: https://github.com/TikhonJelvis/Gosu-Mode/blob/master/inferior-gosu-mode.el

Naturally feel free to use that code however you want; you might also want to look at the normal gosu mode to see how you could integrate you erlang and prolog into the languages' respective editing modes.

like image 175
Tikhon Jelvis Avatar answered Nov 06 '22 23:11

Tikhon Jelvis