@@ -445,16 +445,22 @@ function is_close(token) {
445
445
// find matching open parentheses. The last token is always
446
446
// a closing parenthesis
447
447
function matched_token ( code ) {
448
- let count = 0 ;
449
- // true to tokenize return tokens with meta data
450
- return tokenize ( code , true ) . reverse ( ) . find ( token => {
451
- if ( is_open ( token . token ) ) {
452
- count -- ;
453
- } else if ( is_close ( token . token ) ) {
454
- count ++ ;
448
+ try {
449
+ let count = 0 ;
450
+ // true to tokenize return tokens with meta data
451
+ return tokenize ( code , true ) . reverse ( ) . find ( token => {
452
+ if ( is_open ( token . token ) ) {
453
+ count -- ;
454
+ } else if ( is_close ( token . token ) ) {
455
+ count ++ ;
456
+ }
457
+ return is_open ( token . token ) && count === 0 ;
458
+ } ) ;
459
+ } catch ( e ) {
460
+ if ( ! ( e instanceof Parser . Unterminated ) ) {
461
+ throw e ;
455
462
}
456
- return is_open ( token . token ) && count === 0 ;
457
- } ) ;
463
+ }
458
464
}
459
465
460
466
// highlight the open parentheses based on token metadata
@@ -511,7 +517,7 @@ function run_repl(err, rl) {
511
517
let prev_line ;
512
518
const is_emacs = process . env . INSIDE_EMACS ;
513
519
function is_brackets_mode ( ) {
514
- return cmd . match ( brackets_re ) ;
520
+ return ! ! cmd . match ( brackets_re ) ;
515
521
}
516
522
function continue_multiline ( code ) {
517
523
multiline = true ;
@@ -549,23 +555,32 @@ function run_repl(err, rl) {
549
555
function char_before_cursor ( ) {
550
556
return rl . line [ rl . cursor - 1 ] ;
551
557
}
558
+ function format_input_code ( code ) {
559
+ if ( code ) {
560
+ // we remove trailing newline from code
561
+ code = code . substring ( 0 , code . length - 1 ) ;
562
+ return scheme ( code ) ;
563
+ }
564
+ }
552
565
rl . _writeToOutput = function _writeToOutput ( string ) {
553
566
try {
554
567
const prefix = multiline ? continue_prompt : prompt ;
555
568
const current_line = prefix + rl . line ;
556
569
let code = scheme ( string ) ;
557
- const was_bracket = is_brackets_mode ( ) ;
558
- if ( ! was_bracket && ! is_emacs ) {
559
- // we remove trailing newline from cmd
560
- let code_above = cmd && scheme ( cmd . substring ( 0 , cmd . length - 1 ) ) ;
570
+ const bracket_mode = cmd . match ( brackets_re ) ;
571
+ const full_copy_paste = bracket_mode ?. length == 2 ;
572
+ if ( ( ! bracket_mode || full_copy_paste ) && ! is_emacs ) {
573
+ // clean bracket mode markers
574
+ const clean_cmd = cmd . replace ( brackets_re , '' ) ;
575
+ let code_above = format_input_code ( clean_cmd ) ;
561
576
if ( char_before_cursor ( ) === ')' ) {
562
577
const substring = rl . line . substring ( 0 , rl . cursor ) ;
563
578
const input = prefix + substring ;
564
579
const token = matched_token ( input ) ;
565
580
if ( token ) {
566
581
code = mark_paren ( code , token ) ;
567
- } else if ( cmd ) {
568
- const input = cmd + substring ;
582
+ } else if ( clean_cmd ) {
583
+ const input = clean_cmd + substring ;
569
584
// we match paren above the current line
570
585
// but we need whole code with rl.line
571
586
// so we need to ignore rl.line
@@ -636,7 +651,7 @@ function run_repl(err, rl) {
636
651
return result ;
637
652
} ) . then ( function ( result ) {
638
653
if ( process . stdin . isTTY ) {
639
- if ( print ( result ) ) {
654
+ if ( print ( result ) || newline ) {
640
655
// readline doesn't work with not ended lines
641
656
// it ignore those, so we end them ourselves
642
657
process . stdout . write ( '\n' ) ;
0 commit comments