johnnywang has asked for the wisdom of the Perl Monks concerning the following question:

Hi, is it possible to use a command line perl program to drive emacs? I guess this is different from writing emacs extensions using perl as in Emacs::Lisp. What I'd like is to programatically (using perl of course) to start emacs, and edit a document using lisp commands (like calling the list "insert" to insert a string). This is sort of like using Win32::OLE to create a Word or Excel document programatically. Thanks.

Replies are listed 'Best First'.
Re: Use perl to drive emacs
by kvale (Monsignor) on Feb 21, 2005 at 02:33 UTC
    Yes, this this possible. To use emacs in batch mode, try this from the command line
    % emacs -batch -l lisp_file -f function_to_execute text_file_to_proce +ss
    function_to _execute should be the top level driver function in lisp_file. To execute this from perl, use system() or qx//.

    -Mark

      Thanks, that's great. Is it possible to do this dynamically? i.e., I don't have the lisp_file before hand, and would like to decide what to do at runtime. For example, is it possible to run emacs as a server, listening on a port, and I can send it commands over the socket, and see its result on the file on the screen display? Thanks again.
        To use emacs as a server, look into gnuserv. From the manpage:
        gnuclient allows the user to request a running emacs pro- cess to edit the named files or directories and/or evalu- ate lisp forms. Depending on your environment, it can be an X frame or a TTY frame. One typical use for this is with a dialup connection to a machine on which an emacs process is currently running. gnuserv is the server program that is set running by emacs to handle all incoming and outgoing requests. It is not usually invoked directly, but is started from emacs by loading the gnuserv package and evaluating the Lisp form (gnuserv-start).
        Using gnuserv and gnuclient also addresses Aristotle's concern about the heavy startup costs of invoking emacs for each separate command.

        -Mark

      Of course launching an Emacs instance (even if not fullblown) for every single editing command could get a wee bit slow…

      Makeshifts last the longest.

Re: Use perl to drive emacs
by BUU (Prior) on Feb 21, 2005 at 05:39 UTC
    Could I ask *why* you want to do this?
      Well, a little fantacy: I'm starting to have problems with my hands (a typical programmer's problem: repetitive stress), and I'm also starting to play with speech recognition. You can guess what I'm thinking: Ctrl- is killing me in emacs, can I use a perl program to receive commands from speech, and call some emacs commands?
        Hi

        I don't know what kind of system you work on, but if you use a PC keyboard, you may want to keymap CapsLock to CTRL.

        -j