| 1 | package PDL::Perldl2::Profile::Perldl2; | ||
| 2 | # | ||
| 3 | # Created on: Sun 25 Apr 2010 03:09:34 PM | ||
| 4 | # Last saved: Fri 16 Dec 2011 03:19:27 PM | ||
| 5 | # | ||
| 6 | |||
| 7 | use Moose; | ||
| 8 | use namespace::clean -except => [ 'meta' ]; | ||
| 9 | |||
| 10 | $PDL::Perldl2::Profile::Perldl2::VERSION = 0.008; | ||
| 11 | |||
| 12 | with 'Devel::REPL::Profile'; | ||
| 13 | |||
| 14 | sub plugins { | ||
| 15 | qw( | ||
| 16 | CleanErrors | ||
| 17 | Commands | ||
| 18 | Completion | ||
| 19 | CompletionDriver::INC | ||
| 20 | CompletionDriver::Keywords | ||
| 21 | CompletionDriver::LexEnv | ||
| 22 | CompletionDriver::Methods | ||
| 23 | DDS | ||
| 24 | History | ||
| 25 | LexEnv | ||
| 26 | MultiLine::PPI | ||
| 27 | Packages | ||
| 28 | NiceSlice | ||
| 29 | PrintControl | ||
| 30 | ReadLineHistory | ||
| 31 | PDLCommands | ||
| 32 | ); # CompletionDriver::Globals | ||
| 33 | } | ||
| 34 | |||
| 35 | sub apply_profile { | ||
| 36 | my ($self, $repl) = @_; | ||
| 37 | |||
| 38 | # check for Term::ReadLine::Stub | ||
| 39 | if ($repl->term->ReadLine =~ /Stub/) { | ||
| 40 | $repl->print("WARNING:\n Term::ReadLine::Stub does not support pdl2 features.\n"); | ||
| 41 | $repl->print(" Please install either Term::ReadLine::Perl or Term::ReadLine::Gnu.\n"); | ||
| 42 | $repl->print(" Falling back to perldl in the meantime...\n"); | ||
| 43 | $repl->print("------------------------------------------\n\n"); | ||
| 44 | exec 'perldl'; | ||
| 45 | } | ||
| 46 | |||
| 47 | # add PDL::Perldl2 for plugin search | ||
| 48 | push @{$repl->_plugin_app_ns}, 'PDL::Perldl2'; | ||
| 49 | |||
| 50 | foreach my $plug ($self->plugins) { | ||
| 51 | if ($plug =~ 'CompletionDriver::INC') { | ||
| 52 | eval 'use File::Next'; | ||
| 53 | next if $@; | ||
| 54 | } | ||
| 55 | if ($plug =~ 'CompletionDriver::Keywords') { | ||
| 56 | eval 'use B::Keywords'; | ||
| 57 | next if $@; | ||
| 58 | } | ||
| 59 | $repl->load_plugin($plug); | ||
| 60 | } | ||
| 61 | |||
| 62 | # these plugins don't work on win32 | ||
| 63 | unless ($^O =~ m/win32/i) { | ||
| 64 | $repl->load_plugin('Interrupt'); | ||
| 65 | } | ||
| 66 | |||
| 67 | # enable Term::ReadLine file expansion by default | ||
| 68 | $repl->do_readline_filename_completion(1) if $repl->can('do_readline_filename_completion'); | ||
| 69 | |||
| 70 | # do perldl stuff here | ||
| 71 | $repl->eval('package main'); | ||
| 72 | |||
| 73 | $repl->eval('use PDL'); | ||
| 74 | $repl->eval('use PDL::Config'); | ||
| 75 | $repl->eval('use PDL::Dbg'); | ||
| 76 | $repl->eval('use PDL::Doc::Perldl'); | ||
| 77 | $repl->eval('use PDL::IO::Dumper'); | ||
| 78 | $repl->eval('use PDL::IO::FlexRaw'); | ||
| 79 | $repl->eval('use PDL::IO::Pic'); | ||
| 80 | $repl->eval('use PDL::Image2D'); | ||
| 81 | $repl->eval('use PDL::AutoLoader'); | ||
| 82 | $repl->eval('no strict qw(vars)'); | ||
| 83 | |||
| 84 | # declare PERLDL package variables | ||
| 85 | # most are not used but they are here if needed | ||
| 86 | $repl->eval( q[ | ||
| 87 | @PERLDL::AUTO = (); # code string/refs to run after user enters a new line | ||
| 88 | $PERLDL::ESCAPE = '#'; # Default shell escape character | ||
| 89 | $PERLDL::HISTFILESIZE = $ENV{PERLREPL_HISTLEN}; # Number of lines to keep in history | ||
| 90 | $PERLDL::MULTI = 1; # Enable multi-lines by default | ||
| 91 | $PERLDL::NO_EOF = 1; # Enable EOF protection by default | ||
| 92 | $PERLDL::PAGE = 0; | ||
| 93 | $PERLDL::PAGER = ((exists $ENV{PAGER}) ? $ENV{PAGER} : 'more'); | ||
| 94 | $PERLDL::PAGING = 0; | ||
| 95 | $PERLDL::PROMPT = "pdl> "; # string or code reference | ||
| 96 | $PERLDL::PREFIX_RE = qr(^\s*(?:pdl|perldl)>\s*); # RE for shell prompts | ||
| 97 | $PERLDL::TERM = $_REPL->term; | ||
| 98 | ] ); | ||
| 99 | |||
| 100 | #autoflush STDOUT | ||
| 101 | $repl->eval('$|=1;'); | ||
| 102 | # p command (NOTE: this is not an alias for print) | ||
| 103 | $repl->eval('sub p { local $, = " "; print @_,"\n" };'); | ||
| 104 | |||
| 105 | # list history command | ||
| 106 | $repl->eval('sub l { | ||
| 107 | my $n = $#_ > -1 ? shift : 20; | ||
| 108 | my @h = $_REPL->term->GetHistory(); | ||
| 109 | my $min = $#h < $n-1 ? 0 : $#h-$n+1; | ||
| 110 | map { printf "%d: %s\n", $_+1, $h[$_] } ($min..$#h); | ||
| 111 | #map {print "$_: $h[$_]\n"} ($min..$#h); | ||
| 112 | };'); | ||
| 113 | |||
| 114 | # preliminary support for PDL demos | ||
| 115 | $repl->eval( q{ | ||
| 116 | sub demo { | ||
| 117 | local $_ = lc $_[0] ; | ||
| 118 | if(/^$/) { | ||
| 119 | print <<EOD; | ||
| 120 | Use: | ||
| 121 | demo pdl # general demo | ||
| 122 | |||
| 123 | demo 3d # 3d demo (requires TriD with OpenGL or Mesa) | ||
| 124 | demo 3d2 # 3d demo, part 2. (Somewhat memory-intensive) | ||
| 125 | demo 3dgal # the 3D gallery: make cool images with 3-line scripts | ||
| 126 | |||
| 127 | demo pgplot # PGPLOT graphics output (Req.: PGPLOT) | ||
| 128 | demo OOplot # PGPLOT OO interface (Req.: PGPLOT) | ||
| 129 | |||
| 130 | demo gnuplot # Gnuplot graphics (requires PDL::Graphics::Gnuplot) | ||
| 131 | demo prima # Prima graphics (requires PDL::Graphics::Prima) | ||
| 132 | |||
| 133 | demo transform # Coordinate transformations (Req.: PGPLOT) | ||
| 134 | demo cartography # Cartographic projections (Req.: PGPLOT) | ||
| 135 | |||
| 136 | demo bad # Bad-value demo (Req.: bad value support) | ||
| 137 | demo bad2 # Bad-values, part 2 (Req.: bad value support and PGPLOT) | ||
| 138 | EOD | ||
| 139 | return; | ||
| 140 | } # if: /^$/ | ||
| 141 | |||
| 142 | my %demos = ( | ||
| 143 | 'pdl' => 'PDL::Demos::General', # have to protect pdl as it means something | ||
| 144 | '3d' => 'PDL::Demos::TriD1', | ||
| 145 | '3d2' => 'PDL::Demos::TriD2', | ||
| 146 | '3dgal' => 'PDL::Demos::TriDGallery', | ||
| 147 | 'pgplot' => 'PDL::Demos::PGPLOT_demo', | ||
| 148 | 'ooplot' => 'PDL::Demos::PGPLOT_OO_demo', # note: lowercase | ||
| 149 | 'bad' => 'PDL::Demos::BAD_demo', | ||
| 150 | 'bad2' => 'PDL::Demos::BAD2_demo', | ||
| 151 | 'transform' => 'PDL::Demos::Transform_demo', | ||
| 152 | 'cartography' => 'PDL::Demos::Cartography_demo', | ||
| 153 | 'gnuplot' => 'PDL::Demos::Gnuplot_demo', | ||
| 154 | 'prima' => 'PDL::Demos::Prima', | ||
| 155 | ); | ||
| 156 | |||
| 157 | if ( exists $demos{$_} ) { | ||
| 158 | require PDL::Demos::Screen; # Get the routines for screen demos. | ||
| 159 | my $name = $demos{$_}; | ||
| 160 | eval "require $name;"; # see docs on require for need for eval | ||
| 161 | $name .= "::run"; | ||
| 162 | no strict 'refs'; | ||
| 163 | &{$name}(); | ||
| 164 | } else { | ||
| 165 | print "No such demo!\n"; | ||
| 166 | } | ||
| 167 | |||
| 168 | } } ); | ||
| 169 | |||
| 170 | if ($repl->can('do_print')) { | ||
| 171 | $repl->eval('sub do_print { $_REPL->do_print(@_) };'); | ||
| 172 | } | ||
| 173 | |||
| 174 | if ($repl->can('exit_repl')) { | ||
| 175 | $repl->eval('sub quit { $_REPL->exit_repl(1) };'); | ||
| 176 | } else { | ||
| 177 | $repl->eval('sub quit { $_REPL->print("Use Ctrl-D or exit to quit" };'); | ||
| 178 | } | ||
| 179 | |||
| 180 | $repl->prompt($PERLDL::PROMPT); # new prompt | ||
| 181 | |||
| 182 | if ( defined $ENV{TERM} and $ENV{TERM} eq 'dumb' ) { | ||
| 183 | $repl->print("\n"); | ||
| 184 | $repl->print("******************************************\n"); | ||
| 185 | $repl->print("* Warning: TERM type is dumb! *\n"); | ||
| 186 | $repl->print("* Limited ReadLine functionality will be *\n"); | ||
| 187 | $repl->print("* available. Please unset TERM or use a *\n"); | ||
| 188 | $repl->print("* different terminal type. *\n"); | ||
| 189 | $repl->print("******************************************\n"); | ||
| 190 | $repl->print("\n"); | ||
| 191 | } | ||
| 192 | |||
| 193 | $repl->print("Perldl2 Shell v$PDL::Perldl2::Profile::Perldl2::VERSION | ||
| 194 | PDL comes with ABSOLUTELY NO WARRANTY. For details, see the file | ||
| 195 | 'COPYING' in the PDL distribution. This is free software and you | ||
| 196 | are welcome to redistribute it under certain conditions, see | ||
| 197 | the same file for details.\n"); | ||
| 198 | |||
| 199 | $repl->print("Loaded plugins:\n"); | ||
| 200 | { | ||
| 201 | my @plugins = (); | ||
| 202 | foreach my $pl ( $repl->_plugin_locator->plugins ) { | ||
| 203 | # print names of ones that have been loaded | ||
| 204 | my $plug = $pl; | ||
| 205 | $plug =~ s/^.*Plugin::/ /; | ||
| 206 | push @plugins, $plug if $repl->does($pl); | ||
| 207 | } | ||
| 208 | # Need smarter display of plugins, fill out the line | ||
| 209 | # and list CompletionDrivers under Completion | ||
| 210 | $repl->print(join "\n", sort(@plugins)); | ||
| 211 | $repl->print("\n"); | ||
| 212 | } | ||
| 213 | |||
| 214 | $repl->print("Type 'help' for online help\n"); | ||
| 215 | $repl->print("Type Ctrl-D or quit to exit\n"); | ||
| 216 | $repl->print("Loaded PDL v$PDL::VERSION\n"); | ||
| 217 | } | ||
| 218 | |||
| 219 | 1; | ||
| 220 | |||
| 221 | __END__ |