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.
26 use Sepia
::Debug
; # THIS TURNS ON DEBUGGING INFORMATION!
28 use Scalar
::Util
'looks_like_number';
31 use Storable
qw(store retrieve);
33 use vars
qw($PS1 %REPL %RK %REPL_DOC %REPL_SHORT %PRINTER
35 $REPL_LEVEL $PACKAGE $WANTARRAY $PRINTER $STRICT $PRINT_PRETTY
40 eval { require Lexical::Persistence; import Lexical::Persistence };
42 print "Strict mode requires Lexical::Persistence.\n";
45 my $x = as_boolean(shift, $STRICT);
47 $STRICT = new Lexical::Persistence;
58 eval { require Module::CoreList };
62 *core_version = sub { Module::CoreList->first_release(@_) };
68 eval { use List::Util 'max' };
73 $ret = $_ if $_ > $ret;
82 eval { require Devel::Size };
84 print "Size requires Devel::Size.\n";
86 *Sepia::repl_size = sub {
88 ## XXX: C&P from repl_who:
89 my ($pkg, $re) = split ' ', shift || '';
90 if ($pkg =~ /^\/(.*)\/?$/) {
93 } elsif (!$re && !%{$pkg.'::'}) {
100 my @who = who($pkg, $re);
101 my $len = max(3, map { length } @who) + 4;
102 my $fmt = '%-'.$len."s%10d\n";
103 print 'Var', ' ' x ($len + 2), "Bytes\n";
104 print '-' x ($len-4), ' ' x 9, '-' x 5, "\n";
107 next unless /^[\$\@\%\&]/; # skip subs.
108 next if $_ eq '%SIG';
109 $res{$_} = eval "no strict; package $pkg; Devel::Size::total_size \\$_;";
111 for (sort { $res{$b} <=> $res{$a} } keys %res) {
112 printf $fmt, $_, $res{$_};
121 Sepia is a set of features to make Emacs a better tool for Perl
122 development. This package contains the Perl side of the
123 implementation, including all user-serviceable parts (for the
124 cross-referencing facility see L<Sepia::Xref>). This document is
125 aimed as Sepia developers; for user documentation, see
126 L<Sepia.html> or L<sepia.info>.
128 Though not intended to be used independent of the Emacs interface, the
129 Sepia module's functionality can be used through a rough procedural
132 =head2 C<@compls = completions($string [, $type])>
134 Find a list of completions for C<$string> with glob type C<$type>,
135 which may be "SCALAR", "HASH", "ARRAY", "CODE", "IO", or the special
136 value "VARIABLE", which means either scalar, hash, or array.
137 Completion operates on word subparts separated by [:_], so
138 e.g. "S:m_w" completes to "Sepia::my_walksymtable".
140 =head2 C<@compls = method_completions($expr, $string [,$eval])>
142 Complete among methods on the object returned by C<$expr>. The
143 C<$eval> argument, if present, is a function used to do the
144 evaluation; the default is C<eval>, but for example the Sepia REPL
145 uses C<Sepia::repl_eval>. B<Warning>: Since it has to evaluate
146 C<$expr>, method completion can be extremely problematic. Use with
153 # Do that crazy multi-word identifier completion thing:
155 return qr/.*/ if $re eq '';
158 s/(?:^|(?<=[A-Za-z\d]))(([^A-Za-z\d])\2*)/[A-Za-z\\d]*$2+/g;
160 } split /:+/, $re, -1;
162 if ($re !~ /[^\w\d_^:]/) {
163 $re =~ s/(?<=[A-Za-z\d])(([^A-Za-z\d])\2*)/[A-Za-z\\d]*$2+/g;
171 %sigil = qw(ARRAY @ SCALAR $ HASH %);
177 local $_ = /^::/ ?
$_ : "::$_";
178 defined *{$_}{CODE
} || defined *{$_}{IO
} || (/::$/ && %$_);
181 ## XXX: Careful about autovivification here! Specifically:
182 ## defined *FOO{HASH} # => ''
183 ## defined %FOO # => ''
184 ## defined *FOO{HASH} # => 1
189 local $_ = /^::/ ?
$_ : "::$_";
190 if ($type eq 'SCALAR') {
192 } elsif ($type eq 'VARIABLE') {
193 defined $$_ || defined *{$_}{HASH
} || defined *{$_}{ARRAY
};
202 return '' if $ch eq '';
203 $ch =~ /[A-Z]/ ?
$ch : '['.uc($ch).$ch.']';
206 sub all_abbrev_completions
208 use vars
'&_completions';
209 local *_completions
= sub {
211 my ($stash, @e) = @_;
212 my $ch = '[A-Za-z0-9]*';
213 my $re1 = "^".maybe_icase
($e[0]).$ch.join('', map {
214 '_'.maybe_icase
($_).$ch
217 my $re2 = maybe_icase
$e[0];
218 $re2 = qr/^$re2.*::$/;
219 my @ret = grep !/::$/ && /$re1/, keys %{$stash};
220 my @pkgs = grep /$re2/, keys %{$stash};
221 (map("$stash$_", @ret),
222 @e > 1 ?
map { _completions
"$stash$_", @e[1..$#e] } @pkgs :
223 map { "$stash$_" } @pkgs)
225 map { s/^:://; $_ } _completions
('::', split //, shift);
230 my ($icase, $re) = @_;
232 $icase ?
qr/^$re.*$/i : qr/^$re.*$/;
237 my $icase = $_[0] !~ /[A-Z]/;
238 my @parts = split /:+/, shift, -1;
239 my $re = apropos_re
$icase, pop @parts;
240 use vars
'&_completions';
241 local *_completions
= sub {
245 map { "$stash$_" } grep /$re/, keys %{$stash};
247 my $re2 = $icase ?
qr/^$_[0].*::$/i : qr/^$_[0].*::$/;
248 my @pkgs = grep /$re2/, keys %{$stash};
249 map { _completions
"$stash$_", @_[1..$#_] } @pkgs
252 map { s/^:://; $_ } _completions
('::', @parts);
255 # Filter exact matches so that e.g. "A::x" completes to "A::xx" when
256 # both "Ay::xx" and "A::xx" exist.
257 sub filter_exact_prefix
259 my @parts = split /:+/, shift, -1;
262 my $pre = shift @parts;
263 while (@parts && (@tmp = grep /^\Q$pre\E(?:::|$)/, @res)) {
265 $pre .= '::'.shift @parts;
270 sub lexical_completions
272 eval { require PadWalker
; import PadWalker
'peek_sub' };
273 # "internal" function, so don't warn on failure
275 *lexical_completions
= sub {
276 my ($type, $str, $sub) = @_;
277 $sub = "$PACKAGE\::$sub" unless $sub =~ /::/;
278 # warn "Completing $str of type $type in $sub\n";
280 return unless defined *{$sub}{CODE
};
281 my $pad = peek_sub
(\
&$sub);
283 map { s/^[\$\@&\%]//;$_ } grep /^\Q$type$str\E/, keys %$pad;
285 map { s/^[\$\@&\%]//;$_ } grep /^.\Q$str\E/, keys %$pad;
288 goto &lexical_completions
;
293 my ($type, $str, $sub) = @_;
295 my %h = qw(@ ARRAY % HASH & CODE * IO $ SCALAR);
297 @rh{values %h} = keys %h;
299 $t = $type ?
$rh{$type} : '';
301 if ($sub && $type ne '') {
302 @ret = lexical_completions
$t, $str, $sub;
306 $type ? filter_typed
$type : filter_untyped
307 } all_completions
$str;
309 if (!@ret && $str !~ /:/) {
311 $type ? filter_typed
$type : filter_untyped
312 } all_abbrev_completions
$str;
314 @ret = map { s/^:://; "$t$_" } filter_exact_prefix
$str, @ret;
315 # ## XXX: Control characters, $", and $1, etc. confuse Emacs, so
318 length $_ > 0 && !looks_like_number
($_) && !/^[^\w\d_]$/ && !/^_</ && !/^[[:cntrl:]]/
322 sub method_completions
324 my ($x, $fn, $eval) = @_;
327 $eval ||= 'CORE::eval';
329 return unless ($x =~ /^\$/ && ($x = $eval->("ref($x)")))
330 || $eval->('%'.$x.'::');
332 my $re = _apropos_re
$fn;
333 ## Filter out overload methods "(..."
334 return sort { $a cmp $b } map { s/.*:://; $_ }
335 grep { defined *{$_}{CODE
} && /::$re/ && !/\(/ }
340 =head2 C<@locs = location(@names)>
342 Return a list of [file, line, name] triples, one for each function
352 if (my ($pfx, $name) = $str =~ /^([\%\$\@]?)(.+)/) {
354 warn "Sorry -- can't lookup variables.";
357 # XXX: svref_2object only seems to work with a package
358 # tacked on, but that should probably be done
360 $name = 'main::'.$name unless $name =~ /::/;
361 my $cv = B
::svref_2object
(\
&{$name});
362 if ($cv && defined($cv = $cv->START) && !$cv->isa('B::NULL')) {
363 my ($file, $line) = ($cv->file, $cv->line);
364 if ($file !~ /^\//) {
372 my ($shortname) = $name =~ /^(?:.*::)([^:]+)$/;
373 [Cwd
::abs_path
($file), $line, $shortname || $name]
375 # warn "Bad CV for $name: $cv";
386 =head2 C<@matches = apropos($name [, $is_regex])>
388 Search for function C<$name>, either in all packages or, if C<$name>
389 is qualified, only in one package. If C<$is_regex> is true, the
390 non-package part of C<$name> is a regular expression.
394 sub my_walksymtable
(&*)
400 &$f for keys %$stash;
401 _walk
("$stash$_") for grep /(?<!main)::$/, keys %$stash;
408 my ($it, $re, @types) = @_;
411 $stashp = grep /STASH/, @types;
412 @types = grep !/STASH/, @types;
417 if ($it =~ /^(.*::)([^:]+)$/) {
418 my ($stash, $name) = ($1, $2);
423 my $name = qr/^$name/;
428 my $stashnm = "$stash$_";
432 defined($_ eq 'SCALAR' ?
$$stashnm : *{$stashnm}{$_})
436 defined &$it ?
$it : ();
440 my $findre = $re ?
qr/$it/ : qr/^\Q$it\E$/;
442 push @ret, "$stash$_" if /$findre/;
444 map { s/^:*(?:main:+)*//;$_ } @ret;
448 =head2 C<@names = mod_subs($pack)>
450 Find subs in package C<$pack>.
458 my $stash = \
%{"$p\::"};
460 grep { defined &{"$p\::$_"} } keys %$stash;
464 =head2 C<@decls = mod_decls($pack)>
466 Generate a list of declarations for all subroutines in package
477 my $proto = prototype(\
&{"$pack\::$sn"});
478 $proto = defined($proto) ?
"($proto)" : '';
481 return wantarray ?
@ret : join '', @ret;
484 =head2 C<$info = module_info($module, $type)>
486 Emacs-called function to get module information.
492 eval { require Module
::Info
; import Module
::Info
};
500 $info = Module
::Info
->new_from_file($m);
502 (my $file = $m) =~ s
|::|/|g
;
504 if (exists $INC{$file}) {
505 $info = Module
::Info
->new_from_loaded($m);
507 $info = Module
::Info
->new_from_module($m);
518 =head2 C<$file = mod_file($mod)>
520 Find the likely file owner for module C<$mod>.
528 while ($m && !exists $INC{"$m.pm"}) {
529 $m =~ s
#(?:^|/)[^/]+$##;
531 $m ?
$INC{"$m.pm"} : undef;
534 =head2 C<@mods = package_list>
536 Gather a list of all distributions on the system. XXX UNUSED
544 eval 'require ExtUtils::Installed';
545 $INST = new ExtUtils
::Installed
;
552 sort { $a cmp $b } inst
()->modules;
555 =head2 C<@mods = module_list>
557 Gather a list of all packages (.pm files, really) installed on the
558 system, grouped by distribution. XXX UNUSED
564 @_ = package_list
unless @_;
565 my $incre = join '|', map quotemeta, @INC;
566 $incre = qr
|(?
:$incre)/|;
570 s/$incre//; s
|/|::|g
;$_
571 } grep /\.pm$/, $inst->files($_)]
575 =head2 C<@mods = doc_list>
577 Gather a list of all documented packages (.?pm files, really)
578 installed on the system, grouped by distribution. XXX UNUSED
584 @_ = package_list
unless @_;
588 s/.*man.\///; s|/|::|g
;s/\..?pm//; $_
589 } grep /\..pm$/, $inst->files($_)]
593 =head2 C<lexicals($subname)>
595 Return a list of C<$subname>'s lexical variables. Note that this
596 includes all nested scopes -- I don't know if or how Perl
597 distinguishes inner blocks.
603 my $cv = B
::svref_2object
(\
&{+shift});
604 return unless $cv && ($cv = $cv->PADLIST);
605 my ($names, $vals) = $cv->ARRAY;
607 my $name = $_->PV; $name =~ s/\0.*$//; $name
608 } grep B
::class($_) ne 'SPECIAL', $names->ARRAY;
611 =head2 C<$lisp = tolisp($perl)>
613 Convert a Perl scalar to some ELisp equivalent.
619 my $thing = @_ == 1 ?
shift : \
@_;
622 if (!defined $thing) {
624 } elsif (looks_like_number
$thing) {
627 ## XXX Elisp and perl have slightly different
628 ## escaping conventions, so we do this crap instead.
629 $thing =~ s/["\\]/\\$1/g;
632 } elsif ($t eq 'GLOB') {
633 (my $name = $$thing) =~ s/\*main:://;
635 } elsif ($t eq 'ARRAY') {
636 '(' . join(' ', map { tolisp
($_) } @
$thing).')'
637 } elsif ($t eq 'HASH') {
638 '(' . join(' ', map {
639 '(' . tolisp
($_) . " . " . tolisp
($thing->{$_}) . ')'
641 } elsif ($t eq 'Regexp') {
642 "'(regexp . \"" . quotemeta($thing) . '")';
643 # } elsif ($t eq 'IO') {
649 =head2 C<printer(\@res, $wantarray)>
651 Print C<@res> appropriately on the current filehandle. If C<$ISEVAL>
652 is true, use terse format. Otherwise, use human-readable format,
653 which can use either L<Data::Dumper>, L<YAML>, or L<Data::Dump>.
659 eval { require Data
::Dumper
};
660 local $Data::Dumper
::Deparse
= 1;
661 local $Data::Dumper
::Indent
= 0;
663 my $thing = @res > 1 ? \
@res : $res[0];
665 $_ = Data
::Dumper
::Dumper
($thing);
669 if (length $_ > ($ENV{COLUMNS
} || 80)) {
670 $Data::Dumper
::Indent
= 1;
672 $_ = Data
::Dumper
::Dumper
($thing);
685 eval { require YAML
};
687 $PRINTER{dumper
}->();
693 eval { require Data
::Dump
};
695 $PRINTER{dumper
}->();
697 Data
::Dump
::dump(\
@res);
705 my ($wantarray) = @_;
708 $::__
= @res == 1 ?
$res[0] : [@res];
712 } elsif (@res == 1 && UNIVERSAL
::can
($res[0], '()')) {
715 } elsif (!$ISEVAL && $PRINT_PRETTY && @res > 1 && !grep ref, @res) {
716 $res = columnate
(@res);
720 $res = $PRINTER{$PRINTER}->();
723 print ';;;', length $res, "\n$res\n";
739 "$PACKAGE ".($WANTARRAY ?
'@' : '$').$PS1
745 Data
::Dumper
->Dump([$_[0]], [$_[1]]);
752 my $n1 = int(2*$n/3);
754 s/(.{$n1,$n}) /$1\n/g;
763 *{$_->[0]} = $_->[1];
768 undef @BADVARS{qw(%INC @INC %SIG @ISA %ENV @ARGV)};
774 return !/^.[^c-zA-Z]$/ # single-letter stuff (match vars, $_, etc.)
775 && !/^.[\0-\060]/ # magic weirdness.
776 && !/^._</ # debugger info
777 && !exists $BADVARS{$_}; # others.
784 $re = qr/(?:^|::)$re/;
785 no strict; # no kidding...
788 || $stash =~ /^(?:::)?(?:warnings|Config|strict|B)\b/;
790 my $name = "$stash$_";
791 if (defined ${$name} and saveable '$'.$_) {
792 push @save, [$name, \$$name];
794 if (defined *{$name}{HASH} and saveable '%'.$_) {
795 push @save, [$name, \%{$name}];
797 if (defined *{$name}{ARRAY} and saveable '@'.$_) {
798 push @save, [$name, \@{$name}];
802 print STDERR "$_->[0] " for @save;
807 =head2 C<define_shortcut $name, $sub [, $doc [, $shortdoc]]>
809 Define $name as a shortcut for function $sub.
815 my ($name, $doc, $short, $fn);
821 ($name, $fn, $doc) = @_;
824 ($name, $fn, $short, $doc) = @_;
827 $REPL_DOC{$name} = $doc;
828 $REPL_SHORT{$name} = $short;
833 define_shortcut 'help', \&Sepia::repl_help,
835 'Display help on all commands, or just CMD.';
836 define_shortcut 'cd', \&Sepia::repl_chdir,
837 'cd DIR', 'Change directory to DIR';
838 define_shortcut 'pwd', \&Sepia::repl_pwd,
839 'Show current working directory';
840 define_shortcut 'methods', \&Sepia::repl_methods,
842 'List methods for reference or package X, matching optional pattern RE';
843 define_shortcut 'package', \&Sepia::repl_package,
844 'package PKG', 'Set evaluation package to PKG';
845 define_shortcut 'who', \&Sepia::repl_who,
847 'List variables and subs in PKG matching optional pattern RE.';
848 define_shortcut 'wantarray', \&Sepia::repl_wantarray,
849 'wantarray [0|1]', 'Set or toggle evaluation context';
850 define_shortcut 'format', \&Sepia::repl_format,
851 'format [TYPE]', "Set output formatter to TYPE (one of 'dumper', 'dump', 'yaml', 'plain'; default: 'dumper'), or show current type.";
852 define_shortcut 'strict', \&Sepia::repl_strict,
853 'strict [0|1]', 'Turn \'use strict\' mode on or off';
854 define_shortcut 'quit', \&Sepia::repl_quit,
856 define_shortcut 'restart', \&Sepia::repl_restart,
857 'Reload Sepia.pm and relaunch the REPL.';
858 define_shortcut 'shell', \&Sepia::repl_shell,
859 'shell CMD ...', 'Run CMD in the shell';
860 define_shortcut 'eval', \&Sepia::repl_eval,
861 'eval EXP', '(internal)';
862 define_shortcut 'size', \&Sepia::repl_size,
864 'List total sizes of objects in PKG matching optional pattern RE.';
865 define_shortcut define => \&Sepia::repl_define,
866 'define NAME [\'doc\'] BODY',
867 'Define NAME as a shortcut executing BODY';
868 define_shortcut undef => \&Sepia::repl_undef,
869 'undef NAME', 'Undefine shortcut NAME';
870 define_shortcut test => \&Sepia::repl_test,
871 'test FILE...', 'Run tests interactively.';
872 define_shortcut load => \&Sepia::repl_load,
873 'load [FILE]', 'Load state from FILE.';
874 define_shortcut save => \&Sepia::repl_save,
875 'save [PATTERN [FILE]]', 'Save variables matching PATTERN to FILE.';
876 define_shortcut reload => \&Sepia::repl_reload,
877 'reload [MODULE | /RE/]', 'Reload MODULE, or all modules matching RE.';
878 define_shortcut freload => \&Sepia::repl_full_reload,
879 'freload MODULE', 'Reload MODULE and all its dependencies.';
884 my $width = $ENV{COLUMNS} || 80;
889 my $full = $RK{$args};
891 my $short = $REPL_SHORT{$full};
892 my $flow = flow($width - length $short - 4, $REPL_DOC{$full});
893 $flow =~ s/(.)\n/"$1\n".(' 'x (4 + length $short))/eg;
894 print "$short $flow\n";
896 print "$args: no such command\n";
899 my $left = 1 + max map length, values %REPL_SHORT;
900 print "REPL commands (prefixed with ','):\n";
902 for (sort keys %REPL) {
903 my $flow = flow($width - $left, $REPL_DOC{$_});
904 $flow =~ s/(.)\n/"$1\n".(' ' x $left)/eg;
905 printf "%-${left}s%s\n", $REPL_SHORT{$_}, $flow;
913 my ($name, $doc, $body);
914 if (/^\s*(\S+)\s+'((?:[^'\\]|\\.)*)'\s+(.+)/) {
915 ($name, $doc, $body) = ($1, $2, $3);
916 } elsif (/^\s*(\S+)\s+(\S.*)/) {
917 ($name, $doc, $body) = ($1, $2, $2);
919 print "usage: define NAME ['doc'] BODY...\n";
922 my $sub = eval "sub { do { $body } }";
924 print "usage: define NAME ['doc'] BODY...\n\t$@\n";
927 define_shortcut $name, $sub, $doc;
928 %RK = abbrev keys %REPL;
936 my $full = $RK{$name};
939 delete $REPL_SHORT{$full};
940 delete $REPL_DOC{$full};
941 %RK = abbrev keys %REPL;
943 print "$name: no such shortcut.\n";
952 print "printer = $PRINTER, pretty = @{[$PRINT_PRETTY ? 1 : 0]}\n";
954 my %formats = abbrev keys %PRINTER;
955 if (exists $formats{$t}) {
956 $PRINTER = $formats{$t};
958 warn "No such format '$t' (dumper, dump, yaml, plain).\n";
965 chomp(my $dir = shift);
966 $dir =~ s/^~\//$ENV{HOME}\//;
967 $dir =~ s/\$HOME/$ENV{HOME}/;
970 my $ecmd = '(cd "'.Cwd::getcwd().'")';
971 print ";;;###".length($ecmd)."\n$ecmd\n";
973 warn "Can't chdir\n";
979 print Cwd::getcwd(), "\n";
984 my ($pack, $re_str) = @_;
986 my $re = qr/$re_str/;
988 if ($re_str =~ /^[\$\@\%\&]/) {
989 ## sigil given -- match it
990 sort grep /$re/, map {
991 my $name = $pack.'::'.$_;
992 (defined *{$name}{HASH} ? '%'.$_ : (),
993 defined *{$name}{ARRAY} ? '@'.$_ : (),
994 defined *{$name}{CODE} ? $_ : (),
995 defined ${$name} ? '$'.$_ : (), # ?
997 } grep !/::$/ && !/^(?:_<|[^\w])/ && /$re/, keys %{$pack.'::'};
999 ## no sigil -- don't match it
1001 my $name = $pack.'::'.$_;
1002 (defined *{$name}{HASH} ? '%'.$_ : (),
1003 defined *{$name}{ARRAY} ? '@'.$_ : (),
1004 defined *{$name}{CODE} ? $_ : (),
1005 defined ${$name} ? '$'.$_ : (), # ?
1007 } grep !/::$/ && !/^(?:_<|[^\w])/ && /$re/, keys %{$pack.'::'};
1015 my $width = $ENV{COLUMNS} || 80;
1017 $len = length if $len < length;
1019 my $nc = int($width / ($len+1)) || 1;
1020 my $nr = int(@_ / $nc) + (@_ % $nc ? 1 : 0);
1021 my $fmt = ('%-'.($len+1).'s') x ($nc-1) . "%s\n";
1022 my @incs = map { $_ * $nr } 0..$nc-1;
1024 for my $r (0..$nr-1) {
1025 $str .= sprintf $fmt, map { defined($_) ? $_ : '' }
1026 @_[map { $r + $_ } @incs];
1034 my ($pkg, $re) = split ' ', shift;
1036 if ($pkg && $pkg =~ /^\/(.*)\/?$/) {
1039 } elsif (!$re && !%{$pkg.'::'}) {
1043 print columnate who($pkg || $PACKAGE, $re);
1048 my ($pack, $qualified) = @_;
1050 my @own = $qualified ? grep {
1052 } map { "$pack\::$_" } keys %{$pack.'::'}
1054 defined *{"$pack\::$_"}{CODE}
1055 } keys %{$pack.'::'};
1056 (@own, defined *{$pack.'::ISA'}{ARRAY}
1057 ? (map methods($_, $qualified), @{$pack.'::ISA'}) : ());
1062 my ($x, $re) = split ' ', shift;
1066 $x = $REPL{eval}->("ref $x");
1071 print columnate sort { $a cmp $b } grep /$re/, methods $x;
1076 my ($val, $cur) = @_;
1078 length($val) ? $val : !$cur;
1083 $WANTARRAY = as_boolean shift, $WANTARRAY;
1088 chomp(my $p = shift);
1092 # my $ecmd = '(setq sepia-eval-package "'.$p.'")';
1093 # print ";;;###".length($ecmd)."\n$ecmd\n";
1095 warn "Can't go to package $p -- doesn't exist!\n";
1106 do $INC{'Sepia.pm'};
1108 print "Restart failed:\n$@\n";
1110 $REPL_LEVEL = 0; # ok?
1125 # local $PACKAGE = $pkg || $PACKAGE;
1128 $buf = 'scalar($buf)';
1130 my $ctx = join(',', keys %{$STRICT->get_context('_')});
1131 $ctx = $ctx ? "my ($ctx);" : '';
1132 $buf = eval "sub { package $PACKAGE; use strict; $ctx $buf }";
1134 print "ERROR\n$@\n";
1137 $STRICT->call($buf);
1139 $buf = "do { package $PACKAGE; no strict; $buf }";
1157 } elsif (-f "t/$buf") {
1163 find({ no_chdir => 1,
1165 push @files, $_ if /\.t$/;
1166 }}, Cwd::getcwd() =~ /t\/?$/ ? '.' : './t');
1172 my ($file) = split ' ', shift;
1173 $file ||= "$ENV{HOME}/.sepia-save";
1174 load(retrieve $file);
1179 my ($re, $file) = split ' ', shift;
1181 $file ||= "$ENV{HOME}/.sepia-save";
1182 store save($re), $file;
1187 (my $name = shift) =~ s!::!/!g;
1189 print STDERR "full reload $name\n";
1190 my %save_inc = %INC;
1193 my @ret = keys %INC;
1194 while (my ($k, $v) = each %save_inc) {
1200 sub repl_full_reload
1202 chomp (my $pat = shift);
1203 my @x = full_reload $pat;
1204 print "Reloaded: @x\n";
1209 chomp (my $pat = shift);
1210 if ($pat =~ /^\/(.*)\/?$/) {
1228 if (exists $INC{$pat}) {
1230 eval 'require $mod';
1232 print "Reloaded $mod.\n"
1234 print "$mod not loaded.\n"
1239 ## Collects warnings for REPL
1252 print ';;;'.length($tmp)."\n$tmp\n";
1256 print "warning: $_\n";
1265 I need user feedback! Please send questions or comments to seano\@cpan.org.
1266 Sepia version $Sepia::VERSION.
1267 Type ",h" for help, or ",q" to quit.
1273 Execute a command interpreter on standard input and standard output.
1274 If you want to use different descriptors, localize them before
1275 calling C<repl()>. The prompt has a few bells and whistles, including:
1279 =item Obviously-incomplete lines are treated as multiline input (press
1280 'return' twice or 'C-c' to discard).
1282 =item C<die> is overridden to enter a debugging repl at the point
1287 Behavior is controlled in part through the following package-globals:
1291 =item C<$PACKAGE> -- evaluation package
1293 =item C<$PRINTER> -- result printer (default: dumper)
1295 =item C<$PS1> -- the default prompt
1297 =item C<$STRICT> -- whether 'use strict' is applied to input
1299 =item C<$WANTARRAY> -- evaluation context
1301 =item C<$PRINT_PRETTY> -- format some output nicely (default = 1)
1303 Format some values nicely, independent of $PRINTER. Currently, this
1304 displays arrays of scalars as columns.
1306 =item C<$REPL_LEVEL> -- level of recursive repl() calls
1308 If zero, then initialization takes place.
1310 =item C<%REPL> -- maps shortcut names to handlers
1312 =item C<%REPL_DOC> -- maps shortcut names to documentation
1314 =item C<%REPL_SHORT> -- maps shortcut names to brief usage
1323 if ($REPL_LEVEL == 0) {
1325 -f
"$ENV{HOME}/.sepiarc" and do "$ENV{HOME}/.sepiarc";
1326 warn ".sepiarc: $@\n" if $@
;
1328 local $REPL_LEVEL = $REPL_LEVEL + 1;
1334 my $nextrepl = sub { $sigged = 1; };
1337 local *CORE
::GLOBAL
::die = \
&Sepia
::Debug
::die;
1338 local *CORE
::GLOBAL
::warn = \
&Sepia
::Debug
::warn;
1340 Sepia
::Debug
::add_repl_commands
;
1341 repl_banner
if $REPL_LEVEL == 1;
1343 my @sigs = qw(INT TERM PIPE ALRM);
1345 $SIG{$_} = $nextrepl for @sigs;
1346 repl
: while (defined(my $in = <STDIN
>)) {
1356 if ($buf =~ /^<<(\d+)\n(.*)/) {
1361 while ($len && defined($tmp = read STDIN
, $buf, $len, length $buf)) {
1366 ## Only install a magic handler if no one else is playing.
1367 local $SIG{__WARN__
} = $SIG{__WARN__
};
1369 unless ($SIG{__WARN__
}) {
1370 $SIG{__WARN__
} = 'Sepia::sig_warn';
1374 # repeat last interactive command
1380 if ($buf =~ /^,(\S+)\s*(.*)/s) {
1381 ## Inspector shortcuts
1383 if (exists $Sepia::RK
{$short}) {
1387 $Sepia::REPL
{$Sepia::RK
{$short}}->($arg, wantarray);
1389 if (grep /^$short/, keys %Sepia::REPL
) {
1390 print "Ambiguous shortcut '$short': ",
1391 join(', ', sort grep /^$short/, keys %Sepia::REPL
),
1394 print "Unrecognized shortcut '$short'\n";
1402 @res = $REPL{eval}->($buf);
1405 ## Always return results for an eval request
1406 Sepia
::printer \
@res, wantarray;
1407 Sepia
::printer
[$@
], wantarray;
1408 # print_warnings $ISEVAL;
1411 } elsif ($@
=~ /(?:at|before) EOF(?:$| at)/m) {
1412 ## Possibly-incomplete line
1414 print "Error:\n$@\n*** cancel ***\n", prompt
;
1421 # $@ =~ s/(.*) at eval .*/$1/;
1422 # don't complain if we're abandoning execution
1423 # from the debugger.
1424 unless (ref $@
eq 'Sepia::Debug') {
1426 print "\n" unless $@
=~ /\n\z/;
1434 if ($buf !~ /;\s*$/ && $buf !~ /^,/) {
1435 ## Be quiet if it ends with a semicolon, or if we
1436 ## executed a shortcut.
1437 Sepia
::printer \
@res, wantarray;
1443 wantarray ?
@REPL_RESULT : $REPL_RESULT[0]
1448 tolisp
($REPL{eval}->(shift));
1451 =head2 C<$status = html_module_list([$file [, $prefix]])>
1453 Generate an HTML list of installed modules, looking inside of
1454 packages. If C<$prefix> is missing, uses "about://perldoc/". If
1455 $file is given, write the result to $file; otherwise, return it as a
1458 =head2 C<$status = html_package_list([$file [, $prefix]])>
1460 Generate an HTML list of installed top-level modules, without looking
1461 inside of packages. If C<$prefix> is missing, uses
1462 "about://perldoc/". $file is the same as for C<html_module_list>.
1466 sub html_module_list
1468 my ($file, $base) = @_;
1469 $base ||= 'about://perldoc/';
1471 return unless $inst;
1473 open OUT
, ">", $file || \
$out or return;
1474 print OUT
"<html><body>";
1477 for (package_list
) {
1478 push @
{$ns{$1}}, $_ if /^([^:]+)/;
1480 # Handle core modules.
1482 undef $fs{$_} for map {
1483 s/.*man.\///; s|/|::|g
; s/\.\d(?:pm)?$//; $_
1485 /\.\d(?:pm)?$/ && !/man1/ && !/usr\/bin
/ # && !/^(?
:\
/|perl)/
1486 } $inst->files('Perl');
1487 my @fs = sort keys %fs;
1488 print OUT
qq{<h2
>Core Modules
</h2
><ul
>};
1490 print OUT
qq{<li
><a href
="$base$_">$_</a
>};
1492 print OUT
'</ul><h2>Installed Modules</h2><ul>';
1495 for (sort keys %ns) {
1496 next if $_ eq 'Perl'; # skip Perl core.
1497 print OUT
qq{<li
><b
>$_</b
><ul
>} if @
{$ns{$_}} > 1;
1498 for (sort @
{$ns{$_}}) {
1500 undef $fs{$_} for map {
1501 s/.*man.\///; s|/|::|g
; s/\.\d(?:pm)?$//; $_
1503 /\.\d(?:pm)?$/ && !/man1/
1505 my @fs = sort keys %fs;
1506 next unless @fs > 0;
1508 print OUT
qq{<li
><a href
="$base$fs[0]">$fs[0]</a
>};
1510 print OUT
qq{<li
>$_<ul
>};
1512 print OUT
qq{<li
><a href
="$base$_">$_</a
>};
1517 print OUT
qq{</ul
>} if @
{$ns{$_}} > 1;
1520 print OUT
"</ul></body></html>\n";
1525 sub html_package_list
1527 my ($file, $base) = @_;
1528 return unless inst
();
1529 $base ||= 'about://perldoc/';
1531 open OUT
, ">", $file || \
$out or return;
1532 print OUT
"<html><body><ul>";
1535 for (package_list
) {
1536 push @
{$ns{$1}}, $_ if /^([^:]+)/;
1538 for (sort keys %ns) {
1539 if (@
{$ns{$_}} == 1) {
1541 qq{<li
><a href
="$base$ns{$_}[0]">$ns{$_}[0]</a
>};
1543 print OUT
qq{<li
><b
>$_</b
><ul
>};
1544 print OUT
qq{<li
><a href
="$base$_">$_</a
>}
1545 for sort @
{$ns{$_}};
1546 print OUT
qq{</ul
>};
1549 print OUT
"</ul></body></html>\n";
1559 for (package_list
) {
1560 undef $ret{$_} if /$re/;
1562 undef $ret{$_} for map {
1563 s/.*man.\///; s|/|::|g
; s/\.\d(?:pm)?$//; $_
1565 /\.\d(?:pm)?$/ && !/man1/ && !/usr\/bin
/ && /$re/
1566 } $inst->files('Perl');
1575 See the README file included with the distribution.
1579 Sepia's public GIT repository is located at L<http://repo.or.cz/w/sepia.git>.
1581 There are several modules for Perl development in Emacs on CPAN,
1582 including L<Devel::PerlySense> and L<PDE>. For a complete list, see
1583 L<http://emacswiki.org/cgi-bin/wiki/PerlLanguage>.
1587 Sean O'Rourke, E<lt>seano@cpan.orgE<gt>
1589 Bug reports welcome, patches even more welcome.
1593 Copyright (C) 2005-2009 Sean O'Rourke. All rights reserved, some
1594 wrongs reversed. This module is distributed under the same terms as