summaryrefslogtreecommitdiffstats
path: root/repl.rc
blob: 072aa5952835bec57c1644afc7d5e24cffb5595f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# vim:filetype=perl:
#
#package main;
## this is for Perl's Devel::REPL. it goes in ~/.re.pl/repl.rc
#
use lib 'lib';

no warnings 'redefine';

use List::Util qw(first max maxstr min minstr reduce shuffle sum);
use List::MoreUtils ':all';
use Scalar::Util qw(blessed weaken isweak tainted);
use Scalar::Defer qw(lazy defer force);
use Carp::Clan qw(verbose Devel::REPL:: Moose:: Class::MOP:: Lexical::Persistence::);
use Term::ANSIColor;

*fold = \&reduce;

sub say { print @_, "\n" }

my @plugins = (
    'ReadLineHistory',            # history saved across sessions
    'Colors',                     # colorize return value and errors
    'FancyPrompt',                # provide an irb-like prompt
    'Refresh',                    # refresh before every eval
    'Interrupt',                  # improve handling of ^C
    'OutputCache',                # access previous results
    'Nopaste',                    # paste session with #nopaste
    'PPI',                        # PPI dumping of Perl code
    'MultiLine::PPI',             # allow multiple lines
    'LoadClass',                  # automatically load unloaded classes

    'Completion',
    'CompletionDriver::Keywords', # substr, while, etc
    'CompletionDriver::LexEnv',   # current environment
    'CompletionDriver::Globals',  # global variables
    'CompletionDriver::INC',      # loading new modules
    'CompletionDriver::Methods',  # class method completion
    'CompletionDriver::Turtles',  # turtle command completion
);

$_REPL->load_plugin($_) for @plugins;
$_REPL->fancy_prompt(sub {
    my $self = shift;
    "\ca" . color('blue')  . "\cb" . $self->current_package
  . "\ca" . color('green') . "\cb" . '(0)'
  . "\ca" . color('reset') . "\cb" . '> '
} );
$_REPL->fancy_continuation_prompt(sub {
    my $self = shift;
    "\ca" . color('blue')   . "\cb" . $self->current_package
  . "\ca" . color('yellow') . "\cb" . '(' . $self->line_depth . ')'
  . "\ca" . color('reset')  . "\cb" . '> ' . (' ' x ($self->line_depth * 2))
});
$Devel::REPL::Plugin::Packages::PKG_SAVE = 'main';

#$_REPL->meta->add_around_method_modifier(line_needs_continuation => sub {
    #my $orig = shift;
    #my $self = shift;
    #my ($line) = @_;
    #my $ret = $self->$orig(@_);
    #if (!$ret) {
        #(my $last_line = $line) =~ s/.*\n//s;
        #print { $self->out_fh } "\r\b\r", $self->prompt, $last_line, "     \n";
    #}
    #return $ret;
#});

# if at ~/work/foo/bar, load ~/.re.pl/work-foo-bar as well!
my $cd = $ENV{PWD};
$cd =~ s{^(/(?:Users|home)/[^/]+)/}{} and do {
    my $homedir = $1;
    $cd =~ s{/}{-}g;
    my $proj = "$homedir/.re.pl/$cd";
    $Devel::REPL::Script::CURRENT_SCRIPT->apply_script($proj, 0);
};