@@ -217,7 +217,7 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
217
217
cmd(" javap" , " <path|class>" , " disassemble a file or class name" , javapCommand),
218
218
cmd(" line" , " <id>|<line>" , " place line(s) at the end of history" , lineCommand),
219
219
cmd(" load" , " <path>" , " interpret lines in a file" , loadCommand),
220
- cmd(" paste" , " [path]" , " enter paste mode or paste a file" , pasteCommand),
220
+ cmd(" paste" , " [-raw] [ path]" , " enter paste mode or paste a file" , pasteCommand),
221
221
nullary(" power" , " enable power user mode" , powerCmd),
222
222
nullary(" quit" , " exit the interpreter" , () => Result (keepRunning = false , None )),
223
223
nullary(" replay" , " reset execution and replay all previous commands" , replay),
@@ -666,24 +666,38 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
666
666
667
667
def pasteCommand (arg : String ): Result = {
668
668
var shouldReplay : Option [String ] = None
669
- val code = (
670
- if (arg.nonEmpty) {
671
- withFile(arg)(f => {
669
+ def result = Result (keepRunning = true , shouldReplay)
670
+ val (raw, file) =
671
+ if (arg.isEmpty) (false , None )
672
+ else {
673
+ val r = """ (-raw)?(\s+)?([^\-]\S*)?""" .r
674
+ arg match {
675
+ case r(flag, sep, name) =>
676
+ if (flag != null && name != null && sep == null )
677
+ echo(s """ I assume you mean " $flag $name"? """ )
678
+ (flag != null , Option (name))
679
+ case _ =>
680
+ echo(" usage: :paste -raw file" )
681
+ return result
682
+ }
683
+ }
684
+ val code = file match {
685
+ case Some (name) =>
686
+ withFile(name)(f => {
672
687
shouldReplay = Some (s " :paste $arg" )
673
688
val s = f.slurp.trim
674
689
if (s.isEmpty) echo(s " File contains no code: $f" )
675
690
else echo(s " Pasting file $f... " )
676
691
s
677
692
}) getOrElse " "
678
- } else {
693
+ case None =>
679
694
echo(" // Entering paste mode (ctrl-D to finish)\n " )
680
695
val text = (readWhile(_ => true ) mkString " \n " ).trim
681
696
if (text.isEmpty) echo(" \n // Nothing pasted, nothing gained.\n " )
682
697
else echo(" \n // Exiting paste mode, now interpreting.\n " )
683
698
text
684
- }
685
- )
686
- if (code.nonEmpty) {
699
+ }
700
+ def interpretCode () = {
687
701
val res = intp interpret code
688
702
// if input is incomplete, let the compiler try to say why
689
703
if (res == IR .Incomplete ) {
@@ -693,7 +707,14 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
693
707
if (errless) echo(" ...but compilation found no error? Good luck with that." )
694
708
}
695
709
}
696
- Result (keepRunning = true , shouldReplay)
710
+ def compileCode () = {
711
+ val errless = intp compileSources new BatchSourceFile (" <pastie>" , code)
712
+ if (! errless) echo(" There were compilation errors!" )
713
+ }
714
+ if (code.nonEmpty) {
715
+ if (raw) compileCode() else interpretCode()
716
+ }
717
+ result
697
718
}
698
719
699
720
private object paste extends Pasted {
0 commit comments