Emacs-EPL
view release on metacpan or search on metacpan
lib/Emacs/Lisp.pm view on Meta::CPAN
In Lisp, variables play a role akin to that of Perl I<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 C<use Emacs::Lisp qw($varname);>. Thereafter, assignment to
C<$varname> will update the Lisp value. Changes made to the variable
in Lisp will be reflected in Perl when C<$varname> is used in
expressions.
This example saves and replaces the value of the Lisp variable
C<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 C<let> instead, for which there is
still no convenient Emacs::Lisp equivalent). See also the C<setq>
function below.
=head2 Property Lists
Lisp symbols all have an associated object called a I<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 C<eq> variety. For this reason, it is best to stick to the Lisp
convention of using only symbols as keys. (See L</Symbols>.)
Emacs::Lisp provides a shorthand notation for getting and setting
plist elements. If you say "C<use Emacs::Lisp qw(%any_name)>", then
subsequent access to the elements of C<%any_name> will get or set the
corresponding properties of the Lisp symbol C<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 C<setq> function below.
=head2 Macros
Lisp I<macros>, such as C<setq> and C<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 C<if> and
C<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.
=over 8
=item catch SYMBOL,CODE
Evaluate CODE in a Lisp C<catch> construct. At any point during
CODE's execution, the C<throw> function may be used to return control
to the end of the C<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 C<throw>; for
example, C<return> and C<last>. However, they do not work with
catches in Lisp code.
=item defun SYMBOL,DOCSTRING,SPEC,CODE
=item defun SYMBOL,DOCSTRING,CODE
=item defun SYMBOL,SPEC,CODE
=item defun SYMBOL,CODE
Make CODE callable as the Lisp function SYMBOL. This is Lisp's
version of Perl's C<sub> keyword. A function defined in this way
becomes visible to Lisp code.
C<defun> is useful for defining Emacs I<commands>. Commands are
functions that the user can invoke by typing C<M-x
E<lt>function-nameE<gt>>. 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 I<The Elisp Manual> for details.
Emacs::Lisp's C<defun> uses a SPEC returned by the C<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 C<M-x E<lt>function-nameE<gt> RET> and
cannot be bound to a sequence of keystrokes. Even commands that do
( run in 0.501 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )