diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2021-01-15 18:23:18 -0800 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2021-01-15 18:23:18 -0800 |
| commit | d31d2e89850d3132850e0f2e1ce6d973bd482073 (patch) | |
| tree | 654739b075372bbe55b4082b2c77d7e70813e7fb /printing.c | |
| parent | 87ad1efc24521c0a17c1d8d627518f1eb9f4fa4f (diff) | |
Added interactive confirmation mode for replacing text
Diffstat (limited to 'printing.c')
| -rw-r--r-- | printing.c | 14 |
1 files changed, 10 insertions, 4 deletions
@@ -261,11 +261,15 @@ void _print_match(FILE *out, printer_t *pr, match_t *m) { pr->pos = m->start; if (m->op->type == VM_REPLACE) { + if (m->skip_replacement) { + _print_match(out, pr, m->child); + return; + } size_t line_start = get_line_number(pr->file, m->start); size_t line_end = get_line_number(pr->file, m->end); size_t line = line_start; - if (pr->use_color) printf("%s", color_replace); + if (pr->use_color) fprintf(out, "%s", color_replace); const char *text = m->op->args.replace.text; const char *end = &text[m->op->args.replace.len]; @@ -279,7 +283,7 @@ void _print_match(FILE *out, printer_t *pr, match_t *m) match_t *cap = get_capture(m, &r); if (cap != NULL) { _print_match(out, pr, cap); - if (pr->use_color) printf("%s", color_replace); + if (pr->use_color) fprintf(out, "%s", color_replace); continue; } else { --r; @@ -347,17 +351,19 @@ void print_match(FILE *out, printer_t *pr, match_t *m) // Non-overlapping ranges: print_between(out, pr, pr->pos, after_last, pr->use_color ? color_normal : NULL); if (pr->context_lines > 1) - printf("\n"); // Gap between chunks + fprintf(out, "\n"); // Gap between chunks } } print_between(out, pr, before_m, m->start, pr->use_color ? color_normal : NULL); _print_match(out, pr, m); - if (pr->use_color) printf("%s", color_normal); } else { // After the last match is printed, print the trailing context: const char *after_last = context_after(pr, pr->pos); print_between(out, pr, pr->pos, after_last, pr->use_color ? color_normal : NULL); + // Guarantee trailing newline + if (pr->pos > pr->file->contents && pr->pos[-1] != '\n') fprintf(out, "\n"); } + if (pr->use_color) fprintf(out, "%s", color_normal); } // |
