5 Sepia - Simple Emacs-Perl Interface
11 M-x load-library RET sepia RET
14 At the prompt in the C<*sepia-repl*> buffer:
18 For more information, please see F<Sepia.html> or F<sepia.info>, which
19 come with the distribution.
23 Sepia is a set of features to make Emacs a better tool for Perl
24 development. This package contains the Perl side of the
25 implementation, including all user-serviceable parts (for the
26 cross-referencing facility see L<Sepia::Xref>). This document is
27 aimed as Sepia developers; for user documentation, see
28 L<Sepia.html> or L<sepia.info>.
30 Though not intended to be used independent of the Emacs interface, the
31 Sepia module's functionality can be used through a rough procedural
36 $VERSION = '0.991_02';
39 use Sepia
::Debug
; # THIS TURNS ON DEBUGGING INFORMATION!
41 use Scalar
::Util
'looks_like_number';
44 use Storable
qw(store retrieve);
46 use vars
qw($PS1 %REPL %RK %REPL_DOC %REPL_SHORT %PRINTER
47 @REPL_RESULT @res $REPL_LEVEL $REPL_QUIT $PACKAGE
48 $WANTARRAY $PRINTER $STRICT $PRINT_PRETTY $ISEVAL
49 $LAST_INPUT @PRE_EVAL @POST_EVAL @PRE_PROMPT);
52 eval q{ use List::Util 'max' };
57 $ret = $_ if $_ > $ret;
66 Like Emacs, Sepia's behavior can be modified by placing functions on
67 various hooks (arrays). Hooks can be manipulated by the following
72 =item C<add_hook(@hook, @functions)> -- Add C<@functions> to C<@hook>.
74 =item C<remove_hook(@hook, @functions)> -- Remove named C<@functions> from C<@hook>.
76 =item C<run_hook(@hook)> -- Run the functions on the named hook.
78 Each function is called with no arguments in an eval {} block, and
79 its return value is ignored.
83 Sepia currently defines the following hooks:
87 =item C<@PRE_PROMPT> -- Called immediately before the prompt is printed.
89 =item C<@PRE_EVAL> -- Called immediately before evaluating user input.
91 =item C<@POST_EVAL> -- Called immediately after evaluating user input.
110 push @
$hook, $h unless grep $h eq $_, @
$hook;
117 @
$hook = grep { my $x = $_; !grep $_ eq $x, @
$hook } @
$hook;
122 Sepia tries hard to come up with a list of completions.
126 =item C<$re = _apropos_re($pat)>
128 Create a completion expression from user input.
134 # Do that crazy multi-word identifier completion thing:
136 return qr/.*/ if $re eq '';
139 s/(?:^|(?<=[A-Za-z\d]))(([^A-Za-z\d])\2*)/[A-Za-z\\d]*$2+/g;
141 } split /:+/, $re, -1;
143 if ($re !~ /[^\w\d_^:]/) {
144 $re =~ s/(?<=[A-Za-z\d])(([^A-Za-z\d])\2*)/[A-Za-z\\d]*$2+/g;
152 %sigil = qw(ARRAY @ SCALAR $ HASH %);
155 =item C<$val = filter_untyped>
157 Return true if C<$_> is the name of a sub, file handle, or package.
159 =item C<$val = filter_typed $type>
161 Return true if C<$_> is the name of something of C<$type>, which
162 should be either a glob slot name (e.g. SCALAR) or the special value
163 "VARIABLE", meaning an array, hash, or scalar.
171 local $_ = /^::/ ?
$_ : "::$_";
172 defined *{$_}{CODE
} || defined *{$_}{IO
} || (/::$/ && %$_);
175 ## XXX: Careful about autovivification here! Specifically:
176 ## defined *FOO{HASH} # => ''
177 ## defined %FOO # => ''
178 ## defined *FOO{HASH} # => 1
183 local $_ = /^::/ ?
$_ : "::$_";
184 if ($type eq 'SCALAR') {
186 } elsif ($type eq 'VARIABLE') {
187 defined $$_ || defined *{$_}{HASH
} || defined *{$_}{ARRAY
};
193 =item C<$re_out = maybe_icase $re_in>
195 Make C<$re_in> case-insensitive if it looks like it should be.
202 return '' if $ch eq '';
203 $ch =~ /[A-Z]/ ?
$ch : '['.uc($ch).$ch.']';
206 =item C<@res = all_abbrev_completions $pattern>
208 Find all "abbreviated completions" for $pattern.
212 sub all_abbrev_completions
214 use vars
'&_completions';
215 local *_completions
= sub {
217 my ($stash, @e) = @_;
218 my $ch = '[A-Za-z0-9]*';
219 my $re1 = "^".maybe_icase
($e[0]).$ch.join('', map {
220 '_'.maybe_icase
($_).$ch
223 my $re2 = maybe_icase
$e[0];
224 $re2 = qr/^$re2.*::$/;
225 my @ret = grep !/::$/ && /$re1/, keys %{$stash};
226 my @pkgs = grep /$re2/, keys %{$stash};
227 (map("$stash$_", @ret),
228 @e > 1 ?
map { _completions
"$stash$_", @e[1..$#e] } @pkgs :
229 map { "$stash$_" } @pkgs)
231 map { s/^:://; $_ } _completions
('::', split //, shift);
236 my ($icase, $re) = @_;
238 $icase ?
qr/^$re.*$/i : qr/^$re.*$/;
243 my $icase = $_[0] !~ /[A-Z]/;
244 my @parts = split /:+/, shift, -1;
245 my $re = apropos_re
$icase, pop @parts;
246 use vars
'&_completions';
247 local *_completions
= sub {
251 map { "$stash$_" } grep /$re/, keys %{$stash};
253 my $re2 = $icase ?
qr/^$_[0].*::$/i : qr/^$_[0].*::$/;
254 my @pkgs = grep /$re2/, keys %{$stash};
255 map { _completions
"$stash$_", @_[1..$#_] } @pkgs
258 map { s/^:://; $_ } _completions
('::', @parts);
261 =item C<@res = filter_exact_prefix @names>
263 Filter exact matches so that e.g. "A::x" completes to "A::xx" when
264 both "Ay::xx" and "A::xx" exist.
268 sub filter_exact_prefix
270 my @parts = split /:+/, shift, -1;
273 my $pre = shift @parts;
274 while (@parts && (@tmp = grep /^\Q$pre\E(?:::|$)/, @res)) {
276 $pre .= '::'.shift @parts;
281 =item C<@res = lexical_completions $type, $str, $sub>
283 Find lexicals of C<$sub> (or a parent lexical environment) of type
284 C<$type> matching C<$str>.
288 sub lexical_completions
290 eval q{ require PadWalker; import PadWalker 'peek_sub' };
291 # "internal" function, so don't warn on failure
293 *lexical_completions
= sub {
294 my ($type, $str, $sub) = @_;
295 $sub = "$PACKAGE\::$sub" unless $sub =~ /::/;
296 # warn "Completing $str of type $type in $sub\n";
298 return unless defined *{$sub}{CODE
};
299 my $pad = peek_sub
(\
&$sub);
301 map { s/^[\$\@&\%]//;$_ } grep /^\Q$type$str\E/, keys %$pad;
303 map { s/^[\$\@&\%]//;$_ } grep /^.\Q$str\E/, keys %$pad;
306 goto &lexical_completions
;
309 =item C<@compls = completions($string [, $type])>
311 Find a list of completions for C<$string> with glob type C<$type>,
312 which may be "SCALAR", "HASH", "ARRAY", "CODE", "IO", or the special
313 value "VARIABLE", which means either scalar, hash, or array.
314 Completion operates on word subparts separated by [:_], so
315 e.g. "S:m_w" completes to "Sepia::my_walksymtable".
317 =item C<@compls = method_completions($expr, $string [,$eval])>
319 Complete among methods on the object returned by C<$expr>. The
320 C<$eval> argument, if present, is a function used to do the
321 evaluation; the default is C<eval>, but for example the Sepia REPL
322 uses C<Sepia::repl_eval>. B<Warning>: Since it has to evaluate
323 C<$expr>, method completion can be extremely problematic. Use with
330 my ($type, $str, $sub) = @_;
332 my %h = qw(@ ARRAY % HASH & CODE * IO $ SCALAR);
334 @rh{values %h} = keys %h;
336 $t = $type ?
$rh{$type} : '';
338 if ($sub && $type ne '') {
339 @ret = lexical_completions
$t, $str, $sub;
343 $type ? filter_typed
$type : filter_untyped
344 } all_completions
$str;
346 if (!@ret && $str !~ /:/) {
348 $type ? filter_typed
$type : filter_untyped
349 } all_abbrev_completions
$str;
351 @ret = map { s/^:://; "$t$_" } filter_exact_prefix
$str, @ret;
352 # ## XXX: Control characters, $", and $1, etc. confuse Emacs, so
355 length $_ > 0 && !/^\d+$/ && !/^[^\w\d_]$/ && !/^_</ && !/^[[:cntrl:]]/
359 sub method_completions
361 my ($x, $fn, $eval) = @_;
364 $eval ||= 'CORE::eval';
366 return unless ($x =~ /^\$/ && ($x = $eval->("ref($x)")))
367 || $eval->('%'.$x.'::');
369 my $re = _apropos_re
$fn;
370 ## Filter out overload methods "(..."
371 return sort { $a cmp $b } map { s/.*:://; $_ }
372 grep { defined *{$_}{CODE
} && /::$re/ && !/\(/ }
377 =item C<@matches = apropos($name [, $is_regex])>
379 Search for function C<$name>, either in all packages or, if C<$name>
380 is qualified, only in one package. If C<$is_regex> is true, the
381 non-package part of C<$name> is a regular expression.
385 sub my_walksymtable
(&*)
391 &$f for keys %$stash;
392 _walk
("$stash$_") for grep /(?<!main)::$/, keys %$stash;
399 my ($it, $re, @types) = @_;
402 $stashp = grep /STASH/, @types;
403 @types = grep !/STASH/, @types;
408 if ($it =~ /^(.*::)([^:]+)$/) {
409 my ($stash, $name) = ($1, $2);
414 my $name = qr/^$name/;
419 my $stashnm = "$stash$_";
423 defined($_ eq 'SCALAR' ?
$$stashnm : *{$stashnm}{$_})
427 defined &$it ?
$it : ();
431 my $findre = $re ?
qr/$it/ : qr/^\Q$it\E$/;
433 push @ret, "$stash$_" if /$findre/;
435 map { s/^:*(?:main:+)*//;$_ } @ret;
441 =head2 Module information
445 =item C<@names = mod_subs($pack)>
447 Find subs in package C<$pack>.
455 my $stash = \
%{"$p\::"};
457 grep { defined &{"$p\::$_"} } keys %$stash;
461 =item C<@decls = mod_decls($pack)>
463 Generate a list of declarations for all subroutines in package
474 my $proto = prototype(\
&{"$pack\::$sn"});
475 $proto = defined($proto) ?
"($proto)" : '';
478 return wantarray ?
@ret : join '', @ret;
481 =item C<$info = module_info($module, $type)>
483 Emacs-called function to get module information.
489 eval q{ require Module::Info; import Module::Info };
497 $info = Module
::Info
->new_from_file($m);
499 (my $file = $m) =~ s
|::|/|g
;
501 if (exists $INC{$file}) {
502 $info = Module
::Info
->new_from_loaded($m);
504 $info = Module
::Info
->new_from_module($m);
515 =item C<$file = mod_file($mod)>
517 Find the likely file owner for module C<$mod>.
525 while ($m && !exists $INC{"$m.pm"}) {
526 $m =~ s
#(?:^|/)[^/]+$##;
528 $m ?
$INC{"$m.pm"} : undef;
531 =item C<@mods = package_list>
533 Gather a list of all distributions on the system.
541 eval 'require ExtUtils::Installed';
542 $INST = new ExtUtils
::Installed
;
549 sort { $a cmp $b } inst
()->modules;
552 =item C<@mods = module_list>
554 Gather a list of all packages (.pm files, really) installed on the
555 system, grouped by distribution. XXX UNUSED
561 @_ = package_list
unless @_;
562 my $incre = join '|', map quotemeta, @INC;
563 $incre = qr
|(?
:$incre)/|;
567 s/$incre//; s
|/|::|g
;$_
568 } grep /\.pm$/, $inst->files($_)]
572 =item C<@mods = doc_list>
574 Gather a list of all documented packages (.?pm files, really)
575 installed on the system, grouped by distribution. XXX UNUSED
583 @_ = package_list
unless @_;
587 s/.*man.\///; s|/|::|g
;s/\..?pm//; $_
588 } grep /\..pm$/, $inst->files($_)]
592 =head2 Miscellaneous functions
596 =item C<$v = core_version($module)>
602 eval q{ require Module::CoreList };
606 *core_version
= sub { Module
::CoreList
->first_release(@_) };
611 =item C<[$file, $line, $name] = location($name)>
613 Return a [file, line, name] triple for function C<$name>.
621 if (my ($pfx, $name) = /^([\%\$\@]?)(.+)/) {
623 warn "Sorry -- can't lookup variables.";
625 # XXX: svref_2object only seems to work with a package
626 # tacked on, but that should probably be done elsewhere...
627 $name = 'main::'.$name unless $name =~ /::/;
628 my $cv = B
::svref_2object
(\
&{$name});
629 if ($cv && defined($cv = $cv->START) && !$cv->isa('B::NULL')) {
630 my ($file, $line) = ($cv->file, $cv->line);
631 if ($file !~ /^\//) {
633 if (!ref $_ && -f
"$_/$file") {
639 my ($shortname) = $name =~ /^(?:.*::)([^:]+)$/;
640 return [Cwd
::abs_path
($file), $line, $shortname || $name]
648 =item C<lexicals($subname)>
650 Return a list of C<$subname>'s lexical variables. Note that this
651 includes all nested scopes -- I don't know if or how Perl
652 distinguishes inner blocks.
658 my $cv = B
::svref_2object
(\
&{+shift});
659 return unless $cv && ($cv = $cv->PADLIST);
660 my ($names, $vals) = $cv->ARRAY;
662 my $name = $_->PV; $name =~ s/\0.*$//; $name
663 } grep B
::class($_) ne 'SPECIAL', $names->ARRAY;
666 =item C<$lisp = tolisp($perl)>
668 Convert a Perl scalar to some ELisp equivalent.
674 my $thing = @_ == 1 ?
shift : \
@_;
677 if (!defined $thing) {
679 } elsif (looks_like_number
$thing) {
682 ## XXX Elisp and perl have slightly different
683 ## escaping conventions, so we do this crap instead.
684 $thing =~ s/["\\]/\\$1/g;
687 } elsif ($t eq 'GLOB') {
688 (my $name = $$thing) =~ s/\*main:://;
690 } elsif ($t eq 'ARRAY') {
691 '(' . join(' ', map { tolisp
($_) } @
$thing).')'
692 } elsif ($t eq 'HASH') {
693 '(' . join(' ', map {
694 '(' . tolisp
($_) . " . " . tolisp
($thing->{$_}) . ')'
696 } elsif ($t eq 'Regexp') {
697 "'(regexp . \"" . quotemeta($thing) . '")';
698 # } elsif ($t eq 'IO') {
704 =item C<printer(\@res, $wantarray)>
706 Print C<@res> appropriately on the current filehandle. If C<$ISEVAL>
707 is true, use terse format. Otherwise, use human-readable format,
708 which can use either L<Data::Dumper>, L<YAML>, or L<Data::Dump>.
714 eval q{ require Data::Dumper };
715 local $Data::Dumper
::Deparse
= 1;
716 local $Data::Dumper
::Indent
= 0;
718 my $thing = @res > 1 ? \
@res : $res[0];
720 $_ = Data
::Dumper
::Dumper
($thing);
724 if (length $_ > ($ENV{COLUMNS
} || 80)) {
725 $Data::Dumper
::Indent
= 1;
727 $_ = Data
::Dumper
::Dumper
($thing);
740 eval q{ require YAML };
742 $PRINTER{dumper
}->();
748 eval q{ require Data::Dump };
750 $PRINTER{dumper
}->();
752 Data
::Dump
::dump(\
@res);
761 $PRINTER{dumper
}->();
763 my $ret = new IO
::Scalar
;
764 my $out = select $ret;
765 Devel
::Peek
::Dump
(@res == 1 ?
$res[0] : \
@res);
784 my ($wantarray) = @_;
787 $_ = @res == 1 ?
$res[0] : @res == 0 ?
undef : [@res];
791 } elsif (@res == 1 && UNIVERSAL
::can
($res[0], '()')) {
794 } elsif (!$ISEVAL && $PRINT_PRETTY && @res > 1 && !grep ref, @res) {
795 $res = columnate
(@res);
799 $res = $PRINTER{$PRINTER}->();
802 print ';;;', length $res, "\n$res\n";
816 =item C<prompt()> -- Print the REPL prompt.
822 run_hook
@PRE_PROMPT;
823 "$PACKAGE ".($WANTARRAY ?
'@' : '$').$PS1
829 Data
::Dumper
->Dump([$_[0]], [$_[1]]);
833 =item C<$flowed = flow($width, $text)> -- Flow C<$text> to at most C<$width> columns.
840 my $n1 = int(2*$n/3);
842 s/(.{$n1,$n}) /$1\n/g;
852 =item C<load \@keyvals> -- Load persisted data in C<@keyvals>.
854 =item C<$ok = saveable $name> -- Return whether C<$name> is saveable.
856 Saving certain magic variables leads to badness, so we avoid them.
858 =item C<\@kvs = save $re> -- Return a list of name/value pairs to save.
869 *{$_->[0]} = $_->[1];
874 undef @BADVARS{qw(%INC @INC %SIG @ISA %ENV @ARGV)};
880 return !/^.[^c-zA-Z]$/ # single-letter stuff (match vars, $_, etc.)
881 && !/^.[\0-\060]/ # magic weirdness.
882 && !/^._</ # debugger info
883 && !exists $BADVARS{$_}; # others.
890 $re = qr/(?:^|::)$re/;
891 no strict; # no kidding...
894 || $stash =~ /^(?:::)?(?:warnings|Config|strict|B)\b/;
896 my $name = "$stash$_";
897 if (defined ${$name} and saveable '$'.$_) {
898 push @save, [$name, \$$name];
900 if (defined *{$name}{HASH} and saveable '%'.$_) {
901 push @save, [$name, \%{$name}];
903 if (defined *{$name}{ARRAY} and saveable '@'.$_) {
904 push @save, [$name, \@{$name}];
908 print STDERR "$_->[0] " for @save;
913 =head2 REPL shortcuts
915 The function implementing built-in REPL shortcut ",X" is named C<repl_X>.
919 =item C<define_shortcut $name, $sub [, $doc [, $shortdoc]]>
921 Define $name as a shortcut for function $sub.
927 my ($name, $doc, $short, $fn);
933 ($name, $fn, $doc) = @_;
936 ($name, $fn, $short, $doc) = @_;
939 $REPL_DOC{$name} = $doc;
940 $REPL_SHORT{$name} = $short;
943 =item C<define_shortcuts()>
945 Define the default REPL shortcuts.
951 define_shortcut 'help', \&Sepia::repl_help,
953 'Display help on all commands, or just CMD.';
954 define_shortcut 'cd', \&Sepia::repl_chdir,
955 'cd DIR', 'Change directory to DIR';
956 define_shortcut 'pwd', \&Sepia::repl_pwd,
957 'Show current working directory';
958 define_shortcut 'methods', \&Sepia::repl_methods,
960 'List methods for reference or package X, matching optional pattern RE';
961 define_shortcut 'package', \&Sepia::repl_package,
962 'package PKG', 'Set evaluation package to PKG';
963 define_shortcut 'who', \&Sepia::repl_who,
965 'List variables and subs in PKG matching optional pattern RE.';
966 define_shortcut 'wantarray', \&Sepia::repl_wantarray,
967 'wantarray [0|1]', 'Set or toggle evaluation context';
968 define_shortcut 'format', \&Sepia::repl_format,
969 'format [TYPE]', "Set output formatter to TYPE (one of 'dumper', 'dump', 'yaml', 'plain'; default: 'dumper'), or show current type.";
970 define_shortcut 'strict', \&Sepia::repl_strict,
971 'strict [0|1]', 'Turn \'use strict\' mode on or off';
972 define_shortcut 'quit', \&Sepia::repl_quit,
974 define_shortcut 'restart', \&Sepia::repl_restart,
975 'Reload Sepia.pm and relaunch the REPL.';
976 define_shortcut 'shell', \&Sepia::repl_shell,
977 'shell CMD ...', 'Run CMD in the shell';
978 define_shortcut 'eval', \&Sepia::repl_eval,
979 'eval EXP', '(internal)';
980 define_shortcut 'size', \&Sepia::repl_size,
982 'List total sizes of objects in PKG matching optional pattern RE.';
983 define_shortcut define => \&Sepia::repl_define,
984 'define NAME [\'DOC\'] BODY',
985 'Define NAME as a shortcut executing BODY';
986 define_shortcut undef => \&Sepia::repl_undef,
987 'undef NAME', 'Undefine shortcut NAME';
988 define_shortcut test => \&Sepia::repl_test,
989 'test FILE...', 'Run tests interactively.';
990 define_shortcut load => \&Sepia::repl_load,
991 'load [FILE]', 'Load state from FILE.';
992 define_shortcut save => \&Sepia::repl_save,
993 'save [PATTERN [FILE]]', 'Save variables matching PATTERN to FILE.';
994 define_shortcut reload => \&Sepia::repl_reload,
995 'reload [MODULE | /RE/]', 'Reload MODULE, or all modules matching RE.';
996 define_shortcut freload => \&Sepia::repl_full_reload,
997 'freload MODULE', 'Reload MODULE and all its dependencies.';
998 define_shortcut time => \&Sepia::repl_time,
999 'time [0|1]', 'Print timing information for each command.';
1002 =item C<repl_strict([$value])>
1004 Toggle strict mode. Requires L<Lexical::Persistence>.
1010 eval q{ require Lexical::Persistence; import Lexical::Persistence };
1012 print "Strict mode requires Lexical::Persistence.\n";
1014 *repl_strict
= sub {
1015 my $x = as_boolean
(shift, $STRICT);
1016 if ($x && !$STRICT) {
1017 $STRICT = new Lexical
::Persistence
;
1028 eval q{ require Devel::Size };
1030 print "Size requires Devel::Size.\n";
1032 *Sepia
::repl_size
= sub {
1034 ## XXX: C&P from repl_who:
1035 my ($pkg, $re) = split ' ', shift || '';
1036 if ($pkg =~ /^\/(.*)\
/?$/) {
1042 } elsif (!$re && !%{$pkg.'::'}) {
1046 my @who = who
($pkg, $re);
1047 my $len = max
(3, map { length } @who) + 4;
1048 my $fmt = '%-'.$len."s%10d\n";
1049 # print "$pkg\::/$re/\n";
1050 print 'Var', ' ' x
($len + 2), "Bytes\n";
1051 print '-' x
($len-4), ' ' x
9, '-' x
5, "\n";
1054 next unless /^[\$\@\%\&]/; # skip subs.
1055 next if $_ eq '%SIG';
1056 $res{$_} = eval "no strict; package $pkg; Devel::Size::total_size \\$_;";
1058 for (sort { $res{$b} <=> $res{$a} } keys %res) {
1059 printf $fmt, $_, $res{$_};
1066 =item C<repl_time([$value])>
1068 Toggle command timing.
1072 my ($time_res, $TIME);
1073 sub time_pre_prompt_bsd
1075 printf "(%.2gr, %.2gu, %.2gs) ", @
{$time_res} if defined $time_res;
1078 sub time_pre_prompt_plain
1080 printf "(%.2gs) ", $time_res if defined $time_res;
1085 $TIME = as_boolean
(shift, $TIME);
1087 print STDERR
"Removing time hook.\n";
1088 remove_hook
@PRE_PROMPT, 'Sepia::time_pre_prompt';
1089 remove_hook
@PRE_EVAL, 'Sepia::time_pre_eval';
1090 remove_hook
@POST_EVAL, 'Sepia::time_post_eval';
1093 print STDERR
"Adding time hook.\n";
1094 add_hook
@PRE_PROMPT, 'Sepia::time_pre_prompt';
1095 add_hook
@PRE_EVAL, 'Sepia::time_pre_eval';
1096 add_hook
@POST_EVAL, 'Sepia::time_post_eval';
1097 my $has_bsd = eval q{ use BSD::Resource 'getrusage';1 };
1098 my $has_hires = eval q{ use Time::HiRes qw(gettimeofday tv_interval);1 };
1100 if ($has_bsd) { # sweet! getrusage!
1101 my ($user, $sys, $real);
1102 *time_pre_eval
= sub {
1104 ($user, $sys) = getrusage
();
1105 $real = $has_hires ?
[gettimeofday
()] : $user+$sys;
1107 *time_post_eval
= sub {
1108 my ($u2, $s2) = getrusage
();
1109 $time_res = [$has_hires ? tv_interval
($real, [gettimeofday
()])
1110 : $s2 + $u2 - $real,
1111 ($u2 - $user), ($s2 - $sys)];
1113 *time_pre_prompt
= *time_pre_prompt_bsd
;
1114 } elsif ($has_hires) { # at least we have msec...
1115 *time_pre_eval
= sub {
1117 $t0 = [gettimeofday
()];
1119 *time_post_eval
= sub {
1120 $time_res = tv_interval
($t0, [gettimeofday
()]);
1122 *time_pre_prompt
= *time_pre_prompt_plain
;
1124 *time_pre_eval
= sub {
1128 *time_post_eval
= sub {
1129 $time_res = (time - $t0);
1131 *time_pre_prompt
= *time_pre_prompt_plain
;
1137 my $width = $ENV{COLUMNS
} || 80;
1139 if ($args =~ /\S/) {
1142 my $full = $RK{$args};
1144 my $short = $REPL_SHORT{$full};
1145 my $flow = flow
($width - length $short - 4, $REPL_DOC{$full});
1146 $flow =~ s/(.)\n/"$1\n".(' 'x (4 + length $short))/eg;
1147 print "$short $flow\n";
1149 print "$args: no such command\n";
1152 my $left = 1 + max
map length, values %REPL_SHORT;
1153 print "REPL commands (prefixed with ','):\n";
1155 for (sort keys %REPL) {
1156 my $flow = flow
($width - $left, $REPL_DOC{$_});
1157 $flow =~ s/(.)\n/"$1\n".(' ' x $left)/eg;
1158 printf "%-${left}s%s\n", $REPL_SHORT{$_}, $flow;
1166 my ($name, $doc, $body);
1167 if (/^\s*(\S+)\s+'((?:[^'\\]|\\.)*)'\s+(.+)/) {
1168 ($name, $doc, $body) = ($1, $2, $3);
1169 } elsif (/^\s*(\S+)\s+(\S.*)/) {
1170 ($name, $doc, $body) = ($1, $2, $2);
1172 print "usage: define NAME ['doc'] BODY...\n";
1175 my $sub = eval "sub { do { $body } }";
1177 print "usage: define NAME ['doc'] BODY...\n\t$@\n";
1180 define_shortcut
$name, $sub, $doc;
1181 %RK = abbrev
keys %REPL;
1189 my $full = $RK{$name};
1191 delete $REPL{$full};
1192 delete $REPL_SHORT{$full};
1193 delete $REPL_DOC{$full};
1194 %RK = abbrev
keys %REPL;
1196 print "$name: no such shortcut.\n";
1205 print "printer = $PRINTER, pretty = @{[$PRINT_PRETTY ? 1 : 0]}\n";
1207 my %formats = abbrev
keys %PRINTER;
1208 if (exists $formats{$t}) {
1209 $PRINTER = $formats{$t};
1211 warn "No such format '$t' (dumper, dump, yaml, plain).\n";
1218 chomp(my $dir = shift);
1219 $dir =~ s/^~\//$ENV{HOME
}\
//;
1220 $dir =~ s/\$HOME/$ENV{HOME}/;
1223 my $ecmd = '(cd "'.Cwd
::getcwd
().'")';
1224 print ";;;###".length($ecmd)."\n$ecmd\n";
1226 warn "Can't chdir\n";
1232 print Cwd
::getcwd
(), "\n";
1235 =item C<who($package [, $re])>
1237 List variables and functions in C<$package> matching C<$re>, or all
1238 variables if C<$re> is absent.
1244 my ($pack, $re_str) = @_;
1246 my $re = qr/$re_str/;
1248 if ($re_str =~ /^[\$\@\%\&]/) {
1249 ## sigil given -- match it
1250 sort grep /$re/, map {
1251 my $name = $pack.'::'.$_;
1252 (defined *{$name}{HASH
} ?
'%'.$_ : (),
1253 defined *{$name}{ARRAY
} ?
'@'.$_ : (),
1254 defined *{$name}{CODE
} ?
$_ : (),
1255 defined ${$name} ?
'$'.$_ : (), # ?
1257 } grep !/::$/ && !/^(?:_<|[^\w])/ && /$re/, keys %{$pack.'::'};
1259 ## no sigil -- don't match it
1261 my $name = $pack.'::'.$_;
1262 (defined *{$name}{HASH
} ?
'%'.$_ : (),
1263 defined *{$name}{ARRAY
} ?
'@'.$_ : (),
1264 defined *{$name}{CODE
} ?
$_ : (),
1265 defined ${$name} ?
'$'.$_ : (), # ?
1267 } grep !/::$/ && !/^(?:_<|[^\w])/ && /$re/, keys %{$pack.'::'};
1271 =item C<$text = columnate(@items)>
1273 Format C<@items> in columns such that they fit within C<$ENV{COLUMNS}>
1281 my $width = $ENV{COLUMNS
} || 80;
1283 $len = length if $len < length;
1285 my $nc = int($width / ($len+1)) || 1;
1286 my $nr = int(@_ / $nc) + (@_ % $nc ?
1 : 0);
1287 my $fmt = ('%-'.($len+1).'s') x
($nc-1) . "%s\n";
1288 my @incs = map { $_ * $nr } 0..$nc-1;
1290 for my $r (0..$nr-1) {
1291 $str .= sprintf $fmt, map { defined($_) ?
$_ : '' }
1292 @_[map { $r + $_ } @incs];
1300 my ($pkg, $re) = split ' ', shift;
1302 if ($pkg && $pkg =~ /^\/(.*)\
/?$/) {
1305 } elsif (!$re && !%{$pkg.'::'}) {
1309 print columnate who
($pkg || $PACKAGE, $re);
1312 =item C<@m = methods($package [, $qualified])>
1314 List method names in C<$package> and its parents. If C<$qualified>,
1315 return full "CLASS::NAME" rather than just "NAME."
1321 my ($pack, $qualified) = @_;
1323 my @own = $qualified ?
grep {
1325 } map { "$pack\::$_" } keys %{$pack.'::'}
1327 defined &{"$pack\::$_"}
1328 } keys %{$pack.'::'};
1329 if (exists ${$pack.'::'}{ISA
} && *{$pack.'::ISA'}{ARRAY
}) {
1331 undef @m{@own, map methods
($_, $qualified), @
{$pack.'::ISA'}};
1339 my ($x, $re) = split ' ', shift;
1343 $x = $REPL{eval}->("ref $x");
1348 print columnate
sort { $a cmp $b } grep /$re/, methods
$x;
1353 my ($val, $cur) = @_;
1355 length($val) ?
$val : !$cur;
1360 $WANTARRAY = as_boolean
shift, $WANTARRAY;
1365 chomp(my $p = shift);
1369 # my $ecmd = '(setq sepia-eval-package "'.$p.'")';
1370 # print ";;;###".length($ecmd)."\n$ecmd\n";
1372 warn "Can't go to package $p -- doesn't exist!\n";
1384 do $INC{'Sepia.pm'};
1386 print "Restart failed:\n$@\n";
1388 $REPL_LEVEL = 0; # ok?
1403 # local $PACKAGE = $pkg || $PACKAGE;
1406 $buf = 'scalar($buf)';
1408 my $ctx = join(',', keys %{$STRICT->get_context('_')});
1409 $ctx = $ctx ?
"my ($ctx);" : '';
1410 $buf = eval "sub { package $PACKAGE; use strict; $ctx $buf }";
1412 print "ERROR\n$@\n";
1415 $STRICT->call($buf);
1417 $buf = "do { package $PACKAGE; no strict; $buf }";
1435 } elsif (-f
"t/$buf") {
1439 find
({ no_chdir
=> 1,
1441 push @files, $_ if /\.t$/;
1442 }}, Cwd
::getcwd
() =~ /t\/?
$/ ? '.' : './t
');
1445 # XXX: this is cribbed from an EU::MM-generated Makefile.
1446 system $^X, qw(-MExtUtils::Command::MM -e),
1447 "test_harness(0, 'blib/lib', 'blib/arch')", @files;
1449 print "No test files for '$buf' in ", Cwd
::getcwd
, "\n";
1455 my ($file) = split ' ', shift;
1456 $file ||= "$ENV{HOME}/.sepia-save";
1457 load
(retrieve
$file);
1462 my ($re, $file) = split ' ', shift;
1464 $file ||= "$ENV{HOME}/.sepia-save";
1465 store save
($re), $file;
1470 (my $name = shift) =~ s!::!/!g;
1472 print STDERR
"full reload $name\n";
1473 my %save_inc = %INC;
1476 my @ret = keys %INC;
1477 while (my ($k, $v) = each %save_inc) {
1483 sub repl_full_reload
1485 chomp (my $pat = shift);
1486 my @x = full_reload
$pat;
1487 print "Reloaded: @x\n";
1492 chomp (my $pat = shift);
1493 if ($pat =~ /^\/(.*)\
/?$/) {
1511 if (exists $INC{$pat}) {
1513 eval 'require $mod';
1515 print "Reloaded $mod.\n"
1517 print "$mod not loaded.\n"
1522 =item C<sig_warn($warning)>
1524 Collect C<$warning> for later printing.
1526 =item C<print_warnings()>
1528 Print and clear accumulated warnings.
1544 print ';;;'.length($tmp)."\n$tmp\n";
1548 print "warning: $_\n";
1557 I need user feedback! Please send questions or comments to seano\@cpan.org.
1558 Sepia version $Sepia::VERSION.
1559 Type ",h" for help, or ",q" to quit.
1565 Execute a command interpreter on standard input and standard output.
1566 If you want to use different descriptors, localize them before
1567 calling C<repl()>. The prompt has a few bells and whistles, including:
1571 =item Obviously-incomplete lines are treated as multiline input (press
1572 'return' twice or 'C-c' to discard).
1574 =item C<die> is overridden to enter a debugging repl at the point
1579 Behavior is controlled in part through the following package-globals:
1583 =item C<$PACKAGE> -- evaluation package
1585 =item C<$PRINTER> -- result printer (default: dumper)
1587 =item C<$PS1> -- the default prompt
1589 =item C<$STRICT> -- whether 'use strict' is applied to input
1591 =item C<$WANTARRAY> -- evaluation context
1593 =item C<$PRINT_PRETTY> -- format some output nicely (default = 1)
1595 Format some values nicely, independent of $PRINTER. Currently, this
1596 displays arrays of scalars as columns.
1598 =item C<$REPL_LEVEL> -- level of recursive repl() calls
1600 If zero, then initialization takes place.
1602 =item C<%REPL> -- maps shortcut names to handlers
1604 =item C<%REPL_DOC> -- maps shortcut names to documentation
1606 =item C<%REPL_SHORT> -- maps shortcut names to brief usage
1617 if ($REPL_LEVEL == 0) {
1619 -f
"$ENV{HOME}/.sepiarc" and do "$ENV{HOME}/.sepiarc";
1620 warn ".sepiarc: $@\n" if $@
;
1622 Sepia
::Debug
::add_repl_commands
;
1623 repl_banner
if $REPL_LEVEL == 0;
1630 local $REPL_LEVEL = $REPL_LEVEL + 1;
1636 my $nextrepl = sub { $sigged = 1; };
1640 local *CORE
::GLOBAL
::die = \
&Sepia
::Debug
::die;
1641 local *CORE
::GLOBAL
::warn = \
&Sepia
::Debug
::warn;
1643 my @sigs = qw(INT TERM PIPE ALRM);
1645 $SIG{$_} = $nextrepl for @sigs;
1646 repl
: while (defined(my $in = <STDIN
>)) {
1656 if ($buf =~ /^<<(\d+)\n(.*)/) {
1661 while ($len && defined($tmp = read STDIN
, $buf, $len, length $buf)) {
1666 ## Only install a magic handler if no one else is playing.
1667 local $SIG{__WARN__
} = $SIG{__WARN__
};
1669 unless ($SIG{__WARN__
}) {
1670 $SIG{__WARN__
} = 'Sepia::sig_warn';
1674 # repeat last interactive command
1680 if ($buf =~ /^,(\S+)\s*(.*)/s) {
1681 ## Inspector shortcuts
1683 if (exists $Sepia::RK
{$short}) {
1687 $Sepia::REPL
{$Sepia::RK
{$short}}->($arg, wantarray);
1689 if (grep /^$short/, keys %Sepia::REPL
) {
1690 print "Ambiguous shortcut '$short': ",
1691 join(', ', sort grep /^$short/, keys %Sepia::REPL
),
1694 print "Unrecognized shortcut '$short'\n";
1703 @res = $REPL{eval}->($buf);
1704 run_hook
@POST_EVAL;
1707 ## Always return results for an eval request
1708 Sepia
::printer \
@res, wantarray;
1709 Sepia
::printer
[$@
], wantarray;
1710 # print_warnings $ISEVAL;
1713 } elsif ($@
=~ /(?:at|before) EOF(?:$| at)/m) {
1714 ## Possibly-incomplete line
1716 print "Error:\n$@\n*** cancel ***\n", prompt
;
1723 # $@ =~ s/(.*) at eval .*/$1/;
1724 # don't complain if we're abandoning execution
1725 # from the debugger.
1726 unless (ref $@
eq 'Sepia::Debug') {
1728 print "\n" unless $@
=~ /\n\z/;
1736 if ($buf !~ /;\s*$/ && $buf !~ /^,/) {
1737 ## Be quiet if it ends with a semicolon, or if we
1738 ## executed a shortcut.
1739 Sepia
::printer \
@res, wantarray;
1746 wantarray ?
@REPL_RESULT : $REPL_RESULT[0]
1751 tolisp
($REPL{eval}->(shift));
1754 =head2 Module browsing
1758 =item C<$status = html_module_list([$file [, $prefix]])>
1760 Generate an HTML list of installed modules, looking inside of
1761 packages. If C<$prefix> is missing, uses "about://perldoc/". If
1762 $file is given, write the result to $file; otherwise, return it as a
1765 =item C<$status = html_package_list([$file [, $prefix]])>
1767 Generate an HTML list of installed top-level modules, without looking
1768 inside of packages. If C<$prefix> is missing, uses
1769 "about://perldoc/". $file is the same as for C<html_module_list>.
1775 sub html_module_list
1777 my ($file, $base) = @_;
1778 $base ||= 'about://perldoc/';
1780 return unless $inst;
1782 open OUT
, ">", $file || \
$out or return;
1783 print OUT
"<html><body>";
1786 for (package_list
) {
1787 push @
{$ns{$1}}, $_ if /^([^:]+)/;
1789 # Handle core modules.
1791 undef $fs{$_} for map {
1792 s/.*man.\///; s|/|::|g
; s/\.\d(?:pm)?$//; $_
1794 /\.\d(?:pm)?$/ && !/man1/ && !/usr\/bin
/ # && !/^(?
:\
/|perl)/
1795 } $inst->files('Perl');
1796 my @fs = sort keys %fs;
1797 print OUT
qq{<h2
>Core Modules
</h2
><ul
>};
1799 print OUT
qq{<li
><a href
="$base$_">$_</a
>};
1801 print OUT
'</ul><h2>Installed Modules</h2><ul>';
1804 for (sort keys %ns) {
1805 next if $_ eq 'Perl'; # skip Perl core.
1806 print OUT
qq{<li
><b
>$_</b
><ul
>} if @
{$ns{$_}} > 1;
1807 for (sort @
{$ns{$_}}) {
1809 undef $fs{$_} for map {
1810 s/.*man.\///; s|/|::|g
; s/\.\d(?:pm)?$//; $_
1812 /\.\d(?:pm)?$/ && !/man1/
1814 my @fs = sort keys %fs;
1815 next unless @fs > 0;
1817 print OUT
qq{<li
><a href
="$base$fs[0]">$fs[0]</a
>};
1819 print OUT
qq{<li
>$_<ul
>};
1821 print OUT
qq{<li
><a href
="$base$_">$_</a
>};
1826 print OUT
qq{</ul
>} if @
{$ns{$_}} > 1;
1829 print OUT
"</ul></body></html>\n";
1834 sub html_package_list
1836 my ($file, $base) = @_;
1837 return unless inst
();
1838 $base ||= 'about://perldoc/';
1840 open OUT
, ">", $file || \
$out or return;
1841 print OUT
"<html><body><ul>";
1844 for (package_list
) {
1845 push @
{$ns{$1}}, $_ if /^([^:]+)/;
1847 for (sort keys %ns) {
1848 if (@
{$ns{$_}} == 1) {
1850 qq{<li
><a href
="$base$ns{$_}[0]">$ns{$_}[0]</a
>};
1852 print OUT
qq{<li
><b
>$_</b
><ul
>};
1853 print OUT
qq{<li
><a href
="$base$_">$_</a
>}
1854 for sort @
{$ns{$_}};
1855 print OUT
qq{</ul
>};
1858 print OUT
"</ul></body></html>\n";
1868 for (package_list
) {
1869 undef $ret{$_} if /$re/;
1871 undef $ret{$_} for map {
1872 s/.*man.\///; s|/|::|g
; s/\.\d(?:pm)?$//; $_
1874 /\.\d(?:pm)?$/ && !/man1/ && !/usr\/bin
/ && /$re/
1875 } $inst->files('Perl');
1884 See the README file included with the distribution.
1888 Sepia's public GIT repository is located at L<http://repo.or.cz/w/sepia.git>.
1890 There are several modules for Perl development in Emacs on CPAN,
1891 including L<Devel::PerlySense> and L<PDE>. For a complete list, see
1892 L<http://emacswiki.org/cgi-bin/wiki/PerlLanguage>.
1896 Sean O'Rourke, E<lt>seano@cpan.orgE<gt>
1898 Bug reports welcome, patches even more welcome.
1902 Copyright (C) 2005-2009 Sean O'Rourke. All rights reserved, some
1903 wrongs reversed. This module is distributed under the same terms as