NAME
Emacs::Run - use emacs from perl via the shell
SYNOPSIS
use Emacs::Run;
my $er = Emacs::Run->new();
my $major_version = $er->emacs_version;
if ($major_version > 22) {
print "You have a recent version of emacs\n";
}
# use extra emacs lisp libraries, then get emacs settings
my $er = Emacs::Run->new({
emacs_libs => [ '~/lib/my-elisp.el',
'/usr/lib/site-emacs/stuff.el' ],
});
my $emacs_load_path_aref = $er->get_load_path;
my $email = $er->get_variable( 'user-mail-address' );
my $name = $er->eval_function( 'user-full-name' );
# suppress the use of the usual emacs init (e.g. ~/.emacs)
my $er = Emacs::Run->new({
load_emacs_init => 0,
});
my $result = $er->eval_elisp( '(print (+ 2 2))' ); # that's "4"
# Specify in detail how the emacs lisp libraries should be loaded
$lib_data = [
[ 'dired', { type=>'lib', priority=>'needed' } ],
[ '/tmp/my-load-path.el', { type=>'file', priority=>'requested' } ],
[ '/tmp/my-elisp.el', { type=>'file', priority=>'needed' } ],
];
my $er = Emacs::Run->new({
lib_data => $lib_data,
});
my $result = $er->eval_lisp(
qq{ (print (my-elisp-run-my-code "$perl_string")) }
);
# Command line usage to view your emacs load_path
perl -MEmacs::Run -MData::Dumper -e'my $er=Emacs::Run->new; print Dumper( $er->get_load_path ), "\n"';
DESCRIPTION
Emacs::Run is a module that provides utilities to work with emacs when run from perl as an external process.
The emacs "editor" has some command-line options ("--batch" and so on) that allow you to run elisp code non-interactively: essentially these turn emacs into a lisp interpreter.
This module provides methods to allow perl code to easily use these features of emacs for two types of tasks:
Probe your emacs installation to get the installed version, the user's current load-path, and so on.
Run chunks of emacs lisp code without worrying too much about the details of quoting issues and loading libraries and so on.
METHODS
- new
-
Creates a new Emacs::Run object.
Takes a hashref as an argument, with named fields identical to the names of the object attributes. These attributes are:
- emacs_path
-
By default, this code looks for an external program called 'emacs' in a location listed in the shell's PATH environment variable. If you have multiple emacsen installed in different places and/or under different names, you can choose which one will be used by setting this attribute.
- shell_output_director
-
Note: this is feature is most likely going to change in it's behavior in the next release (after switching over to using IPC::Capture).
The shell_output_director (sometimes called sod for short) is a string appended to all of the internally generated emacs invocation commands to control what happens to output. This is object attribute is the value used by the elisp evaluation methods documented below, unless another value is specified.
Most methods here accept a hash reference of options that can include their own shell_output_director setting.
Typical values (on a unix-like system) are:
- '2>&1'
-
Intermix STDOUT and STDERR (in elisp: both "message" and "print" functions work). This is the default setting for this object attribute.
- '2>/dev/null'
-
return only STDOUT, throwing away STDERR (in elisp: get output only from "print")
- "> $file 2>&1"
-
send all output to the file $file
- ">> $file 2>&1"
-
append all output to the file $file, preserving any existing content.
- "2 > $log_file"
-
return only STDOUT, but save STDERR to $log_file
- before_hook
-
A string inserted into the built-up emacs commands immediately after "--batch", but before any thing else is executed. This is a good place to insert additional invocation options such as "--multibyte" or "--unibyte".
- load_emacs_init
-
Defaults to 1, if set to a false value, will suppress the use of the user's emacs init file (e.g. "~/.emacs").
- load_site_init
-
Defaults to 1, if set to a false value, will suppress the use of the system "site-start.el" file (which loads before the user's init file).
- load_default_init
-
Defaults to 1, if set to a false value, will suppress the use of the system "default.el" file (which loads after the user's init file).
- load_no_inits
-
A convenience flag, used to disable all three types of emacs init files in one step when set to a true value. Overrides the other three.
- emacs_libs
-
A list of emacs libraries (with or without paths) to be loaded automatically. This is provided as a convenience for quick use. To take full control over how your emacs libraries are handled, see "lib_data".
- default_priority
-
The global default for how all the emacs libraries should be loaded. Normally this is set to "requested", but it can be set to "needed".
A 'requested' library will be silently skipped if it is not available (and any elisp code using it may need to do something like 'featurep' checks to adapt to it's absence). A 'needed' file will cause an error to occur if it is not available. Note: this error does not occur during object instantiation, but only after a method is called that needs to load the libraries (e.g. "eval_function" "get_variable", "eval_elisp", "run_elisp_on_file", etc).
- lib_data
-
Note: using "emacs_libs" is usually preferrable to "lib_data".
lib_data is the internal representation that </emacs_libs> is converted into, but the client programmer is provided access to it to cover any unusual needs.
The structure of lib_data is an array of arrays of two elements each, the first element is the library name (a string, with or without path), the second element is a hash of library attributes: 'priority' (which can be 'requested' or 'needed') and 'type' (which can be 'file' or 'lib').
Example:
$lib_data = [ [ 'dired', { type=>'lib', priority=>'needed' } ], [ '/tmp/my-load-path.el', { type=>'file', priority=>'requested' } ], [ '/tmp/my-elisp.el', { type=>'file', priority=>'needed' } ], ];
emacs library attributes:
- priority
-
A 'requested' library will be silently skipped if it is not available, but if a 'needed' file is not available it's regarded as an error condition. The default priority is 'requested', but that can be changed via the "default_priority" attribute. See "default_priority" for more details.
- type
-
A library of type 'file' should be a filesystem path to a file containing a library of emacs lisp code. A library of type 'lib' is specified by just the basename of the file (sans path or extension), and we will search for it looking in the places specified in the emacs variable load-path. When neither is specified, this module guesses the lib is a file if it looks that way (i.e it has a path and/or extension).
If both lib_data and emacs_libs are used, the lib_data libraries are loaded first, followed by the emacs_libs libraries.
These attributes are used to pass information to the client programmer, they should be regarded as read-only:
- emacs_version
-
The version number of emacs in use: this is set automatically by the "probe_emacs_version" method during object initialization.
- emacs_type
-
The flavor of emacs in use, e.g. 'Gnu Emacs'. Set automatically by the "probe_emacs_version" method during object initialization.
There are also a number of object attributes intended largely for internal use. The client programmer has access to these, but is not expected to need it. These are documented in "internal attributes".
- init
-
Method that initializes object attributes and then locks them down to prevent accidental creation of new ones.
Any class that inherits from this one should have an "init" of it's own that calls this "init".
- get_load_path
-
Returns the load-path from emacs (by default, using the user's .emacs, if it can be found) as a reference to a perl array.
Changing the $HOME environment variable before running this method results in loading the .emacs file located in the new $HOME.
By default the returned output includes just STDOUT and not STDERR (and the object attribute "shell_output_director" is ignored), but this behavior can be overridden by setting a field named shell_output_director in the options hashref.
An example of that usage:
my $returned_string = $er->get_load_path( { shell_output_director => '2>&1', } );
- get_variable
-
Given the name of an emacs variable, returns the value from emacs (when started with the the .emacs located in $HOME, if one is found),
Internally, this uses the emacs 'print' function, which can handle variables containing complex data types, but the return value will be a "printed representation" that may make more sense to emacs than to perl code. For example, the "load-path" variable might look like:
("/home/grunt/lib" "/usr/lib/emacs/site-lisp" "/home/xtra/lib")
By default the returned output includes just STDOUT and not STDERR (and the object attribute "shell_output_director" is ignored), but this behavior can be overridden by setting a field named shell_output_director in the options hashref.
An example of that usage:
my $returned_string = $er->get_variable( 'user-mail-address', { shell_output_director => '2>&1', } );
- eval_function
-
Given the name of an emacs function, this runs the function (without arguments) and returns the value from emacs (when started with the the .emacs located in $HOME, if one is found). After the function name, an optional array reference may be supplied to pass through a list of simple arguments (limited to strings) to the elisp function. And further, an optional hash reference may follow that to specify options to the "eval_function" method.
By default the returned output includes just STDOUT and not STDERR (and the object attribute "shell_output_director" is ignored), but this behavior can be overridden by setting a field named shell_output_director in the options hashref.
As with "get_variable", this uses the emacs 'print' function internally.
Examples:
my $name = $er->eval_function( 'user-full-name' ); $er->eval_function( 'extract-doctrings-generate-html-for-elisp-file', [ "$input_elisp_file", "$output_file", "The extracted docstrings" ] );
- probe_for_option_no_splash
-
Looks for the emacs command line option "--no-splash", returning true (1) if it exists, and false (0) otherwise.
running elisp
These are a set of general methods that run pieces of emacs lisp code in different ways.
The detailed behavior of these methods have a number of things in common:
By default the method first loads the user's initialization file ("$HOME/.emacs") if it can be found. It will also try to load the libraries listed in the "emacs_libs" and/or "lib_data" attributes.
There are object attribute settings that can be used to suppress loading any of the various init files. See "new" for the full list. In particular, if the "load_emacs_init" attribute has been turned off, it will not try to load the .emacs file.
Unless specified otherwise, the methods return the output from the elisp code with STDOUT and STDERR mixed together, though this can be overridden with the shell_output_director field of an options hashref. (The advanatage of intermixing STDOUT and STDERR is that the emacs functions 'message' and 'print' both work to generate output. The disadvantage is that you may have inane messages from emacs sent to STDERR such as 'Loading library so-and-so'.
- eval_elisp
-
Given a string containing a chunk of elisp code this method runs it by invoking emacs in batch mode.
Example:
my $result = $er->eval_elisp( '(print (+ 2 2))' );
- eval_elisp_skip_err
-
Similar to "eval_elisp", except that it always returns only the standard output, ignoring any messages sent to STDERR.
This is essentially for backwards compatibility, though it might be useful as a convenience method to avoid sodding about with the "shell_output_director".
- run_elisp_on_file
-
Given a file name, and some emacs lisp code (which presumably modifies the current buffer), this method opens the file, runs the code on it, and then saves the file.
Returns whatever value the elisp returns.
Example usage: $self->run_elisp_on_file( $filename, $elisp );
internal methods
The following methods are intended primarily for internal use.
Note: the common "leading underscore" naming convention is not used here.
utility methods
- quote_elisp
-
Routine to quote elisp code before feeding it into an emacs batch shell command. Used internally by methods such as "eval_elisp".
This just adds a single backslash to all the double-quote characters (essentially an empirically determined algorithm, i.e. hack).
Example usage:
$elisp = $self->quote_elisp( $elisp ); $emacs_cmd = qq{ emacs --batch --eval "$elisp" 2>&1 }; my $return = qx{ $emacs_cmd };
- clean_return_value
-
Cleans up a given string, trimming unwanted leading and trailing blanks and double quotes.
This is intended to be used with elisp that uses the 'print' function.
initialization phase methods
The following routines are largely used internally in the object initialization phase.
- process_emacs_libs_addition
-
Goes through the given list of emacs_libs, and converts the names into the lib_data style of data structure, appending it to lib_data.
Note that this method works on the given argument, without reference to the object's "emacs_libs" field.
Returns a reference to a structure containing the new additions to lib_data.
- process_emacs_libs_reset
-
This converts the list of emacs_libs into the lib_data style of structure much like "process_emacs_libs_addition", but this method resets the lib_data field to the given value at init time (if any) before appending the new data.
Defaults to using the object's "emacs_libs" setting.
Returns a reference to a structure containing the additions to lib_data from emacs_libs.
- set_up_ec_lib_loader
-
Initializes the ec_lib_loader attribute by scanning for the appropriate emacs init file(s) and processing the list(s) of emacs libraries specified in the object data.
Returns the newly defined $ec_lib_loader string.
This routine is called by "init" during object initialization.
- lib_or_file
-
Given the name of an emacs library, examine it to see if it looks like a file system path, or an emacs library (technically a "feature name", i.e. sans path or extension).
Returns a string, either "file" or "lib".
generation of emacs command strings to load libraries
These are routines that generate a string that can be included in an emacs command line invocation to load certain libraries.
Note the naming convention: "generate emacs command-line" => "genec_".
- genec_load_emacs_init
-
Generates an emacs command line fragment to load the emacs_init file(s) as appropriate.
Side effect: zeroes out the ec_lib_loader before rebuilding with inits only.
The following is a set of four routines to generate a string that can be included in an emacs command line invocation to load the given library. The methods here are named according to the pattern:
"genec_loader_$type_$priority"
All of these methods return the generated string, but also append it to the "ec_lib_loader" attribute,
- genec_loader_lib_needed
- genec_loader_file_needed
- genec_loader_lib_requested
- genec_loader_file_requested
Emacs probes
Methods that return information about the emacs installation.
- probe_emacs_version
-
Returns the version of the emacs program stored on your system. This is called during the object initialization phase.
It checks the emacs specified in the object's emacs_path (which defaults to the simple command "emacs", sans any path), and returns the version.
As a side-effect, it sets a number of object attributes with details about the emacs version:
emacs_version emacs_major_version emacs_type
By default the returned output includes just STDOUT and not STDERR (and the object attribute "shell_output_director" is ignored), but this behavior can be overridden by setting a field named shell_output_director in the options hashref.
An example of that usage:
my $version_text = $er->probe_emacs_version( { shell_output_director => '2>&1', } );
- parse_emacs_version_string
-
The emacs version string returned from running "emacs --version" is parsed by this routine, which picks out the version numbers and so on and saves them as object data.
See probe_emacs_version (which uses this internally).
internal utilities (used by initialization code)
- elisp_to_load_file
-
Given the location of an emacs lisp file, generate the elisp that ensures the library will be available and loaded.
- find_dot_emacs
-
Looks for one of the variants of the user's emacs init file (e.g. "~/.emacs") in the same order that emacs would, and returns the first one found.
Note: this relies on the environment variable $HOME. (This can be changed first to cause this to look for an emacs init in some arbitrary location, e.g. for testing purposes.)
This code does not issue a warning if the elc is stale compared to the el, that's left up to emacs.
- detect_site_init
-
Looks for the "site-start.el" file in the raw load-path without loading the regular emacs init file (e.g. ~/.emacs).
Emacs itself normally loads this file before it loads anything else, so this method replicates that behavior.
Returns the library name ('site-start') if found, undef if not.
- detect_lib
-
Looks for the given elisp library in the load-path after trying to load the given list of context_libs (that includes .emacs as appropriate, and this method uses the requested_load_files as context, as well).
Returns $lib if found, undef if not.
Example usage:
if ( $self->detect_lib("dired") ) { print "As expected, dired.el is installed."; } my @good_libs = grep { defined($_) } map{ $self->detect_lib($_) } @candidate_libs;
routines in use by Emacs::Run::ExtractDocs
- elisp_file_from_library_name_if_in_loadpath
-
Identifies the file associated with a given elisp library name by shelling out to emacs in batch mode.
- generate_elisp_to_load_library
-
Generates elisp code that will instruct emacs to load the given library. It also makes sure it's location is in the load-path, which is not precisely the same thing: See "loaded vs. in load-path".
Takes one argument, which can either be the name of the library, or the name of the file, as distinguished by the presence of a ".el" extension on a file name. Also, the file name will usually have a path included, but the library name can not.
basic setters and getters
The naming convention in use here is that setters begin with "set_", but getters have *no* prefix: the most commonly used case deserves the simplest syntax (and mutators are deprecated).
Setters and getters exist for all of the object attributes which are documented with the "new" method (but note that these exist even for "internal" attributes that are not expected to be useful to the client coder).
special accessors
- append_to_ec_lib_loader
-
Non-standard setter that appends the given string to the the "elisp_to_load_file" attribute.
- append_to_before_hook
-
Non-standard setter that appends the given string to the the "before_hook" attribute.
Under some circumstances, this module uses the "before_hook" for internal purposes (for -Q and --no-splash), so using an ordinary setter could be mildly dangerous (you might erase something you didn't realize was there). Typically it's better to just append to the "before_hook" by using this method.
accessors that effect the "ec_lib_loader" attribute
If either lib_data or emacs_libs is modified, this must trigger another run of "set_up_ec_lib_loader" to keep the "ec_lib_loader" string up-to-date.
- set_lib_data
-
Setter for lib_data.
- reset_lib_data
-
Reverts lib_data to the value supplied during initization (it empties it entirely, if none was supplied).
Note: this does not (at present) trigger a re-build of "ec_lib_loader", because it's presumed that this will be triggered by some step following this one.
- set_emacs_libs
-
Setter for emacs_libs.
Side effect: runs process_emacs_libs_rest on the given emacs_libs list.
process_emacs_libs_reset indirectly calls set_up_ec_lib_loader so we don't need to do so explicitly here.
- push_emacs_libs
-
Pushes a new lib to the emacs_libs array.
Takes a string, returns aref of the full list of emacs_libs.
Side-effect: runs process_emacs_libs_addition on the new lib, (appending the new info to lib_data).
process_emacs_libs_addition indirectly calls set_up_ec_lib_loader so we don't need to do so explicitly here.
automatic generation of standard accessors
- AUTOLOAD
MOTIVATION
Periodically, I find myself interested in the strange world of running emacs code from perl. There's a mildly obscure feature of emacs command line invocations called "--batch" that essentially transforms emacs into an unusual lisp interpreter: other command-line options allow one to load files of elisp code and run pieces of code from the command-line.
You might think that there aren't many uses for this trick, but I've found several of them. You can use it to:
- to probe your emacs set-up from perl, e.g. for automated installation of elisp using perl tools
- to test elisp code using a perl test harness.
- to use tools written in elisp that you don't want to rewrite in perl (e.g. extract-docstrings.el)
This emacs command line invocation is a little language all of it's own, with just enough twists and turns to it that I've felt the need to write perl routines to help drive the process.
emacs invocation vs Emacs::Run
By default an "emacs --batch" run suppresses most of the usual init files (but does load the essentially deprecated "site-start.pl", presumably for backwards compatibility). Emacs::Run has the opposite bias: here we try to load all three of the types of init files, if they're available, though each one of these can be shut-off individually if so desired. This is because one of the main intended uses is to let perl find out about things such as the user's emacs settings (notably, the load-path). And in any case, the performance hit of loading these files is no-longer such a big deal.
internal documentation (how the code works, etc).
internal attributes
Object attributes intended largely for internal use. The client programmer has access to these, but is not expected to need it. Note: the common "leading underscore" naming convention is not used here.
- ec_lib_loader
-
A fragment of an emacs command line invocation to load emacs libraries. Different attributes exist to specify emacs libraries to be loaded: as these are processed, the ec_lib_loader string gradually accumulates code needed to load them (so that when need be, the process can use the intermediate value of the ec_lib_loader to get the benefit of the previously processed library specifications).
The primary reason for this approach is that each library loaded has the potential to modify the emacs load-path, which may be key for the success of later load attempts.
The process of probing for each library in one of the "requested" lists has to be done in the context of all libraries that have been previously found. Without some place to store intermediate results in some form, this process might need to be programmed as one large monolithic routine.
- lib_data_initial
-
The initial setting of "lib_data" when the object is instantiated. As currently implemented, some operations here require resetting the state of "lib_data" and re-building it. This attribute facilitates that process.
strategies in shelling out
Perl has a good feature for running a shell command and capturing the output: qx{} (aka back-quotes), and it's easy enough to append "2>&1" to a shell command when you'd like to see the STDERR messages intermixed with the STDOUT. This module's methods typically default to capturing all output and returning STDOUT and STDERR intermixed; though unfortunately there is no good way to distinguishing between the messages from STDERR and STDOUT later, and your desired output may be lost in a forest of uninteresting notices sent to STDERR.
From the elisp side, it's important to know that in "--batch" mode, the elisp function "message" sends output to STDERR, and you need to use the elisp function "print" if you'd like to send output to STDOUT. Perhaps unfortunately, the print function also brackets all output with double-quotes and newlines -- the Emacs::Run module compensates by unceremoniously stripping them (via L/<clean_return_value>).
Note: the reasoning behind intermixing STDOUT and STDERR by default is that this batch-mode behavior of message and print is slightly obscure, even to an elisp programmer: returning everything by default is more likely to result in code that Just Works.
A new feature with the 0.3 release is the "shell_output_director" attribute (plus the addition of a method-specific options to override that object-wide suggested default). This provides the user with finer-grained control over how output is handled.
loaded vs. in load-path
The emacs variable "load-path" behaves much like the shell's $PATH (or perl's @INC): if you try to load a library called "dired", emacs searches through the load-path in sequence, looking for an appropriately named file (e.g. "dired.el"), it then evaluates it's contents, and the features defined in the file become available for use. It is also possible to load a file by telling emacs the path and filename, and that works whether or not it is located in the load-path.
There is at least a slight difference between the two, however. For example, the present version of the "extract-docstrings.el" library (see Emacs::Run::ExtractDocs) contains code like this, that will break if the library you're looking for is not in the load-path:
(setq codefile (locate-library library))
So some of the routines here (notably "elisp_to_load_file") generate elisp with an extra feature that adds the location of the file to the load-path as well as just loading it.
interactive vs. non-interactive elisp init
Emacs::Run tries to use the user's normal emacs init process even though it runs non-interactively. Unfortunately, it's possible that the init files may need to be cleaned up in order to be used non-interactively. In my case I found that I needed to check the "x-no-window-manager" variable and selectively disable some code that sets X fonts for me:
;; We must do this check to allow "emacs --batch -l .emacs" to work
(unless (eq x-no-window-manager nil)
(zoom-set-font "-Misc-Fixed-Bold-R-Normal--15-140-75-75-C-90-ISO8859-1"))
TODO
Have "new" fail (return undef) if emacs can not be found on the system. This way you can use the result of "new" to determine if you should skip tests, etc.
Eliminate unixisms, if possible:
o A known one: there's a heuristic that spots file paths by looking for "/". (FIXED) o Use File::Spec more, specfically to generate $devnull o Re-write around IPC::Capture.
I suspect some quoting issues still lurk e.g. a library filename containing a double-quote will probably crash things.
Add a method to match an emacs regexp against a string. See: http://obsidianrook.com/devnotes/talks/test_everything/bin/1-test_emacs_regexps.t.html
(goto-char (point-min)) (re-search-forward "$elisp_pattern") (setq first_capture (match-string 1))
In "run_elisp_on_file", add support for skipping to a line number after opening a file
SEE ALSO
Emacs Documentation: Command Line Arguments for Emacs Invocation http://www.gnu.org/software/emacs/manual/html_node/emacs/Emacs-Invocation.html
A lightning talk about (among other things) using perl to test emacs code: "Using perl to test non-perl code":
http://obsidianrook.com/devnotes/talks/test_everything/index.html
AUTHOR
Joseph Brenner, <doom@kzsu.stanford.edu>, 07 Mar 2008
COPYRIGHT AND LICENSE
Copyright (C) 2008 by Joseph Brenner
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.2 or, at your option, any later version of Perl 5 you may have available.
BUGS & LIMITATIONS
When an elisp library is marked as "needed", and it is not available, failure occurs relatively late: it does not happen during object instantiation, but waits until an attempted run with the object (that is, on a call such as "$er->eval_elisp", not "Emacs::Run->new").
This module was developed around Gnu emacs running on a Gnu/linux platform. Some attempts have been made to make it's use portable to other platforms. At present, using it with a non-gnu emacs such as xemacs is not likely to work.
1 POD Error
The following errors were encountered while parsing the POD:
- Around line 583:
You forgot a '=back' before '=head2'