Pair Networks


Emacs - Redefine Perl's system primitives to work inside of Emacs


    perlmacs -w -MEmacs -e main -- --display :0.0 file.txt

    #! /usr/bin/perlmacs
    use Emacs;
    use Emacs::Lisp;
    setq { $mail_self_blind = t; };
    exit main ($0, "-q", @ARGV);


This module replaces STDIN, STDOUT, STDERR, %ENV, %SIG, exit, and warn (via $SIG{__WARN__}) with versions that work safely within an Emacs session. In Perlmacs, it also defines a function named main, which launches an Emacs editing session from within a script.


Reading a line from Perl's STDIN filehandle causes a string to be read from the minibuffer with the prompt "Enter input: ". To show a different prompt, use:

    $string = &read_string ("Prompt: ");


Printing to Perl's STDOUT filehandle inserts text into the current buffer as though typed, unless you have changed the Lisp variable standard-output to do something different.

STDERR and `warn'

Perl's warn operator and STDERR filehandle are redirected to the minibuffer.


Access to %ENV is redirected to the Lisp variable process-environment.


Setting signal handlers is not currently permitted under Emacs.


exit calls kill-emacs.

main (CMDLINE)

When you use Emacs in a perlmacs script, a Perl sub named main may be used to invoke the Emacs editor. This makes it possible to put customization code, which would normally appear as Lisp in ~/.emacs, into a Perl script.

NOTE: This function does not work under EPL. You have to have Perlmacs to use it. See Emacs::Lisp/"EPL AND PERLMACS".

For example, this startup code

     user-mail-address "gnaeus@perl.moc"
     mail-self-blind t
     mail-yank-prefix "> "

    (put 'eval-expression 'disabled nil)

    (global-font-lock-mode 1 t)
    (set-face-background 'highlight "maroon")
    (set-face-background 'region "Sienna")

could be placed in a file with the following contents:

    #! /usr/local/bin/perlmacs

    use Emacs;
    use Emacs::Lisp;

    setq {
	$user_mail_address = 'gnaeus@perl.moc';
	$mail_self_blind = t;
	$mail_yank_prefix = '> ';
	$eval_expression{\*disabled} = undef;

    &global_font_lock_mode(1, t);
    &set_face_background(\*highlight, "maroon");
    &set_face_background(\*region, "Sienna");

    exit main($0, "-q", @ARGV);

When you wanted to run Emacs, you would invoke this program.

The arguments to main correspond to the argv of the main function in a C program. The first argument should be the program's invocation name, as in this example. -q inhibits running ~/.emacs (which is the point, after all).


* Problems with `main'.

main() doesn't work under EPL. It may open an X display and not close it. Those are the most obvious of many problems with main.

The thing is, Emacs was not written with the expectation of being embedded in another program, least of all a language interpreter such as Perl. Therefore, when Emacs is told to exit, it believes the process is really about to exit, and it neglects to tidy up after itself.

For best results, the value returned by main should be passed to Perl's exit soon, as in this code:

    exit (main($0, @args));


Copyright (C) 1998-2001 by John Tobey, jtobey@john-edwin-tobey.org. All rights reserved.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; see the file COPYING.  If not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  MA 02111-1307  USA


perl, Emacs::Lisp, emacs.