John Tobey > Emacs-EPL-0.7 > Emacs::Lisp

Download:
Emacs-EPL-0.7.tar.gz

Dependencies

Annotate this POD

Related Modules

Win32::OLE
Term::ReadLine
Term::Cap
more...
By perlmonks.org

CPAN RT

New  1
Open  0
View/Report Bugs
Module Version: 1.01   Source  

NAME ^

Emacs::Lisp - Support for writing Emacs extensions in Perl

SYNOPSIS ^

In Emacs

    M-x perl-eval-expression RET 2+2 RET
    M-x perl-eval-region RET
    M-x perl-eval-buffer RET
    ... and more ...

In Perl

    use Emacs::Lisp;

    &switch_to_buffer('*scratch*');
    &insert("Hello, world!\n");

    setq { $cperl_font_lock = t };

    &add_hook(\*find_file_hooks,
              sub { &message("found a file!") });

    use Emacs::Lisp qw($emacs_version $epl_version);
    save_excursion {
        &set_buffer(&get_buffer_create("*test*"));
        &insert("This is ");
        &insert(&featurep(\*::xemacs) ? "XEmacs" : "Emacs");
        &insert(" version $emacs_version,\n");
        &insert("EPL version $epl_version.\n");
        &insert("Emacs::Lisp version is $Emacs::Lisp::VERSION.\n");
    };

DESCRIPTION ^

Emacs allows you to customize your environment using Lisp. With EPL, you can use Perl, too. This module allows Perl code to call functions and access variables of Lisp.

You still need to learn some Lisp in order to understand The Elisp Manual, which is the definitive reference for Emacs programming. This document assumes a basic understanding of Emacs commands and Lisp data types. I also assume familiarity with Perl's complex data structures (described in perlref) and objects (see perlobj).

Quick Start

Run emacs -l perl and type:

    C-x p e &insert ("hello!\n") RET

The string "hello!" should appear in your scratch buffer. The Perl sub &insert has called the Emacs Lisp insert function, which inserts its string argument into the current buffer at point.

Paste this text into a buffer, select it, and type M-x perl-eval-region RET:

    sub doit { &message("Cool, huh?"); }
    defun (\*perltest, interactive, \&doit);

Type M-x perltest RET. The text will appear in the minibuffer. defun and interactive are used to create Emacs commands.

EPL AND PERLMACS ^

Perlmacs was (is?) a project that embedded a Perl interpreter into the Emacs binary so that it could run Lisp, Perl, or any combination of the two. It uses Perl's C interface, which requires patching and recompiling Emacs. As a result, each release is tied to a version of Emacs, it takes a lot of time and disk space to build, and it is not very portable.

EPL (Emacs Perl) accomplishes most of what Perlmacs can do, but it does not suffer from the same drawbacks. It uses unmodified Emacs and Perl and lets them work together through IPC (pipes). This may make some tasks much slower, but it is much more convenient to install and upgrade, and it works with XEmacs as well as Emacs 21 betas.

For the time being, this module attempts to support both Perlmacs and EPL. The user-visible APIs are almost identical, except for EPL's lack of Emacs::main().

LISP SUPPORT FOR PERL ^

Lisp code can check for Perl support using (require 'perl). In Perlmacs, some of the Perl functions are built in, and others are defined in perl.el. When you use EPL, epl.el substitutes for the built-in support, but the same perl.el is used.

Functions

The following Lisp functions do not rely on the Emacs::Lisp module. Use C-h f <function-name> RET within Emacs to see their doc strings.

    perl-eval-expression  EXPRESSION
    perl-eval-region      START END
    perl-eval-buffer
    perl-load-file        NAME
    perl-eval             STRING &optional CONTEXT
    perl-call             SUB &optional CONTEXT &rest ARGS
    perl-eval-and-call    STRING &optional CONTEXT &rest ARGS
    perl-to-lisp          OBJECT
    perl-wrap             OBJECT
    perl-value-p          OBJECT
    perl-eval-raw         STRING &optional CONTEXT
    perl-call-raw         SUB &optional CONTEXT &rest ARGS
    make-perl-interpreter &rest ARGV
    perl-destruct         &optional INTERPRETER
    perl-gc               &optional PURGE
    perl-free-refs        &rest REFS

The following Lisp variables affect the Perl interpreter and have doc strings accessible via C-h f <variable-name> RET. They are:

    perl-interpreter-program
    perl-interpreter-args
    perl-interpreter

Data Conversions

When Perl calls a Lisp function, its arguments are converted to Lisp objects, and the returned object is converted to a Perl value. Likewise, when Lisp calls Perl, the arguments are converted from Lisp to Perl and the return values are converted to Lisp.

Scripts

Perlmacs can run Perl programs. By default, Perlmacs is installed under two names, pmacs and perlmacs. Which name is used to invoke the program determines how it parses its command line.

If perlmacs is used (or, more precisely, any name containing "perl"), it behaves like Perl. For example,

    $ perlmacs script.pl

runs the Perl program script.pl.

When invoked as pmacs, it behaves like Emacs. Example:

    $ pmacs file.txt

This begins an editing session with file.txt in the current buffer.

The first command line argument can override the invocation name. If it is --emacs, Emacs takes control. If it is --perl, the program runs in Perl mode.

The Emacs module (that is, the Perl module named "Emacs") includes support for starting an editing session from within a Perlmacs script. See Emacs.

PERL SUPPORT FOR LISP ^

The Emacs::Lisp module allows Perl programs to invoke Lisp functions and handle Lisp variables as if they were Perl subs and variables.

The directive use Emacs::Lisp; causes any use of a function not defined in Perl to invoke the Lisp function of the same name (with hyphens in place of underscores). For example, this writes a message to the standard error stream (in Perl mode) or displays it in the minibuffer:

    &message ("this is a test");

Functions

This code calls the hypothetical Lisp function foo-bar with arguments 4 and t.

    &foo_bar(4, t);

The Lisp syntax for the same call would be

    (foo-bar 4 t)

The ampersand (&) in the Perl example is not required, but it is needed for functions, such as read, eval, and print, which are Perl keywords. Using it with Emacs::Lisp is a good habit, so the examples in this document include it.

If you don't want an AUTOLOAD sub to affect your namespace, you may either put parentheses after "use Emacs::Lisp" or import to a different package, and use qualified function names. For example:

    use Emacs::Lisp ();
    Emacs::Lisp::message("hello\n");

    {package L; use Emacs::Lisp;}
    L::message("goodbye\n");

Symbols

Many Lisp functions take arguments that may be, or are required to be, symbols. In Lisp, a symbol is a kind of name, but does not have the same type as a string. Lisp programs typically use the quote operator to specify a symbol. For example, this Lisp code refers to the beep symbol:

    (run-at-time nil 1 'beep)

EPL uses glob references to specify symbols. A literal globref begins with a backslash followed by an asterisk, so the last example would be written as

    &run_at_time(undef, 1, \*beep);

in Perl. (You may want to do &cancel_function_timers(\*beep) soon after trying this example.)

When comparing the returned values of Lisp functions to each other and to symbols, it is best to use the Lisp eq function instead of Perl's equality operators.

    ### PREFERRED
    if (&eq(&type_of($x), \*::cons)) { ... }

    ### PROBABLY OK
    if (&type_of($x) eq \*cons) { ... }
    if (&type_of($x) == \*cons) { ... }

Variables

In Lisp, variables play a role akin to that of Perl scalar variables. A variable may hold a number, a string, or a reference to any type of complex Lisp data structure. (They are not called references in Lisp, but rather "objects".)

You can create a Perl alias for any reasonably named Lisp variable by saying use Emacs::Lisp qw($varname);. Thereafter, assignment to $varname will update the Lisp value. Changes made to the variable in Lisp will be reflected in Perl when $varname is used in expressions.

This example saves and replaces the value of the Lisp variable inhibit-eol-conversion:

    use Emacs::Lisp qw($inhibit_eol_conversion);
    $old_val = $inhibit_eol_conversion;
    $inhibit_eol_conversion = 1;

This sort of thing could be accomplished in Lisp as follows:

    (setq old-val inhibit-eol-conversion)
    (setq inhibit-eol-conversion 1)

(but you would probably rather use let instead, for which there is still no convenient Emacs::Lisp equivalent). See also the setq function below.

Property Lists

Lisp symbols all have an associated object called a plist, for "property list". The plist is an object just like any other, but it is typically used in a way vaguely resembling Perl's hashes.

Plists are not used nearly as often as Lisp functions and variables. If you are new to Lisp, you can probably skip this section.

A plist is different from a Perl hash. Lookups are not based on string equality as with Perl, but rather on Lisp object equality of the eq variety. For this reason, it is best to stick to the Lisp convention of using only symbols as keys. (See "Symbols".)

Emacs::Lisp provides a shorthand notation for getting and setting plist elements. If you say "use Emacs::Lisp qw(%any_name)", then subsequent access to the elements of %any_name will get or set the corresponding properties of the Lisp symbol any-name.

For example, the following Perl and Lisp fragments are more or less equivalent:

    # Perl fragment
    use Emacs::Lisp qw(%booboo %upcase_region);
    $booboo{\*error_conditions} = [\*booboo, \*error];
    $can_upcase = ! $upcase_region{\*disabled};

    ; Lisp fragment
    (put 'booboo 'error-conditions '(booboo error))
    (setq can-upcase (not (get 'upcase-region 'disabled)))

See also the setq function below.

Macros

Lisp macros, such as setq and defun, do not work the same way functions do, although they are invoked using the function syntax. (Here you see the vast philosophical chasm separating Perl from Lisp. While Perl might have five syntaxes to mean the same thing, Lisp has one syntax with two meanings!)

Some macros are equivalent to Perl operators, such as if and while. Others have meanings peculiar to Lisp. A few macros are implemented in Emacs::Lisp. They are described below. If you try to call a macro that has not been implemented, you will get an error message which may propose an alternative.

catch SYMBOL,CODE

Evaluate CODE in a Lisp catch construct. At any point during CODE's execution, the throw function may be used to return control to the end of the catch block. For example:

    $x = catch \*::out, sub {
        $y = 1;
        &throw(\*::out, 16);
        $y = 2;
    };
    print $x;  # prints 16
    print $y;  # prints 1

Some Perl constructs have functionality similar to throw; for example, return and last. However, they do not work with catches in Lisp code.

defun SYMBOL,DOCSTRING,SPEC,CODE
defun SYMBOL,DOCSTRING,CODE
defun SYMBOL,SPEC,CODE
defun SYMBOL,CODE

Make CODE callable as the Lisp function SYMBOL. This is Lisp's version of Perl's sub keyword. A function defined in this way becomes visible to Lisp code.

defun is useful for defining Emacs commands. Commands are functions that the user can invoke by typing M-x <function-name>. A command may be bound to a key or sequence of keystrokes. See the Emacs documentation for specifics.

When defining a command, you must specify the interactive nature of the command. There are various codes to indicate that the command acts on the current region, a file name to be read from the minibuffer, etc. Please see The Elisp Manual for details.

Emacs::Lisp's defun uses a SPEC returned by the interactive function to specify a command's interactivity. If no SPEC is given, the function will still be callable by Lisp, but it will not be available to the user via M-x <function-name> RET and cannot be bound to a sequence of keystrokes. Even commands that do not request information from the user need an interactive spec. See "interactive".

This example creates a command, reverse-region-words, that replaces a region of text with the same text after reversing the order of words. To be user-friendly, we'll provide a documentation string, which will be accessible through the Emacs help system (C-h f reverse-region-words RET).

    use Emacs::Lisp;
    defun (\*reverse_region_words,
           "Reverse the order of the words in the region.",
           interactive("r"),
           sub {
               my ($start, $end) = @_;
               my $text = &buffer_substring($start, $end);
               $text = join('', reverse split (/(\s+)/, $text));
               &delete_region($start, $end);
               &insert($text);
           });

If you try this example and invoke the help system, you may notice something not quite right in the message. It reads as follows:

    reverse-region-words is an interactive Lisp function.
    (reverse-region-words &optional START END &rest ARGS)

    Reverse the order of the words in the region.

Notice the part about "&optional" and "&rest". This means that Lisp thinks the function accepts any number of arguments. It knows the names of the first two because of the assignment "my ($start, $end) = @_".

But our function only works if it receives two args. Specifying a prototype documents this:

    sub ($$) {
        my ($start, $end) = @_;
        ...
    }

    reverse-region-words is an interactive Lisp function.
    (reverse-region-words START END)
interactive SPEC
interactive

Used to generate the third (or, in the absence of a doc string, the second) argument to defun. This determines how a command's arguments are obtained.

What distinguishes a "command" from an ordinary function in Emacs is the presence of an interactive specifier in the defun expression.

SPEC may be a string, as described in The Elisp Manual, or a reference to code which returns the argument list. If no spec is given, the command runs without user input.

save_excursion BLOCK

Execute BLOCK within a Lisp save-excursion construct. This restores the current buffer and other settings to their original values after the code has completed. See The Elisp Manual for details.

setq BLOCK

BLOCK is searched for assignments of either of these forms:

    $var = EXPR;
    $hash{$key} = EXPR;

Every such $var and %hash is imported from the Emacs::Lisp module as if you had said, "use Emacs::Lisp qw($var %hash)". Afterwards, BLOCK is executed. This is a convenient way to assign to variables, for example in customization code.

This code

    use Emacs::Lisp;
    setq {
        $A = 2*$foo[5];
        $B{\*foo} = "more than $A";
    };

would have exactly the same effect as this:

    use Emacs::Lisp qw(:DEFAULT $A %B);
    $A = 2*$foo[5];
    $B{\*foo} = "more than $A";

The following, which does not tie or import any variables, has the same effect on Lisp as the above:

    use Emacs::Lisp ();
    Emacs::Lisp::set( \*A, 2*$foo[5] );
    Emacs::Lisp::put( \*B, \*foo, "more than "
      . &Emacs::Lisp::symbol_value( \*A ));
unwind_protect (BODY, HANDLER)

Execute coderef BODY, returning its result. Execute coderef HANDLER after BODY finishes, even if BODY exits nonlocally through die or the like.

BUGS ^

These are some of the known bugs in EPL and Emacs::Lisp. If you find other bugs, please check that you have the latest version, and email me.

CAVEATS ^

TO DO ^

COPYRIGHT ^

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
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  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

SEE ALSO ^

perl, perlref, perlobj, Emacs, emacs, and The Elisp Manual (available where you got the Emacs source, or from ftp://ftp.gnu.org/pub/gnu/emacs/).

syntax highlighting: