@@ -275,6 +275,11 @@ int cursor_uncut_last(cursor_t *cursor) {
275
275
276
276
// Regex search and replace
277
277
int cursor_replace (cursor_t * cursor , int interactive , char * opt_regex , char * opt_replacement ) {
278
+ return cursor_replace_ex (cursor , interactive , opt_regex , opt_replacement , NULL , NULL , NULL , NULL );
279
+ }
280
+
281
+ // Regex search and replace (extended params)
282
+ int cursor_replace_ex (cursor_t * cursor , int interactive , char * opt_regex , char * opt_replacement , char * opt_cmd_name , int * inout_all , int * optret_num_replacements , int * optret_cancelled ) {
278
283
char * regex ;
279
284
char * replacement ;
280
285
int wrapped ;
@@ -295,9 +300,12 @@ int cursor_replace(cursor_t *cursor, int interactive, char *opt_regex, char *opt
295
300
PCRE2_SIZE pcre_ovector [30 ];
296
301
str_t repl_backref = {0 };
297
302
int num_replacements ;
303
+ char * cmd_name ;
298
304
299
305
if (!interactive && (!opt_regex || !opt_replacement )) {
300
306
return MLE_ERR ;
307
+ } else if ((opt_regex && !opt_replacement ) || (!opt_regex && opt_replacement )) {
308
+ return MLE_ERR ;
301
309
}
302
310
303
311
regex = NULL ;
@@ -309,19 +317,20 @@ int cursor_replace(cursor_t *cursor, int interactive, char *opt_regex, char *opt
309
317
search_mark = NULL ;
310
318
search_mark_end = NULL ;
311
319
anchored_before = 0 ;
312
- all = interactive ? 0 : 1 ;
320
+ all = interactive ? ( inout_all ? * inout_all : 0 ) : 1 ;
313
321
num_replacements = 0 ;
314
322
mark_set_pcre_capture (& pcre_rc , pcre_ovector , 30 );
315
323
orig_viewport_y = -1 ;
324
+ cmd_name = opt_cmd_name ? opt_cmd_name : "replace" ;
316
325
317
326
do {
318
- if (!interactive ) {
327
+ if (!interactive || ( opt_regex && opt_replacement ) ) {
319
328
regex = strdup (opt_regex );
320
329
replacement = strdup (opt_replacement );
321
330
} else {
322
- editor_prompt (cursor -> bview -> editor , "replace : Search regex?" , NULL , & regex );
331
+ editor_prompt_fmt (cursor -> bview -> editor , NULL , & regex , "%s : Search regex?" , cmd_name );
323
332
if (!regex ) break ;
324
- editor_prompt (cursor -> bview -> editor , "replace : Replacement string?" , NULL , & replacement );
333
+ editor_prompt_fmt (cursor -> bview -> editor , NULL , & replacement , "%s : Replacement string?" , cmd_name );
325
334
if (!replacement ) break ;
326
335
}
327
336
orig_mark = buffer_add_mark (cursor -> bview -> buffer , NULL , 0 );
@@ -360,14 +369,16 @@ int cursor_replace(cursor_t *cursor, int interactive, char *opt_regex, char *opt
360
369
buffer_add_srule (cursor -> bview -> buffer , highlight );
361
370
bview_rectify_viewport (cursor -> bview );
362
371
bview_draw (cursor -> bview );
363
- editor_prompt (cursor -> bview -> editor , "replace: OK to replace? (y=yes, n=no, a=all, C-c=stop)" ,
364
- & (editor_prompt_params_t ) { .kmap = cursor -> bview -> editor -> kmap_prompt_yna }, & yn
372
+ editor_prompt_fmt (cursor -> bview -> editor ,
373
+ & (editor_prompt_params_t ) { .kmap = cursor -> bview -> editor -> kmap_prompt_yna }, & yn ,
374
+ "%s: OK to replace? (y=yes, n=no, a=all, C-c=stop)" , cmd_name
365
375
);
366
376
buffer_remove_srule (cursor -> bview -> buffer , highlight );
367
377
srule_destroy (highlight );
368
378
bview_draw (cursor -> bview );
369
379
}
370
380
if (!yn ) {
381
+ if (optret_cancelled ) * optret_cancelled = 1 ;
371
382
break ;
372
383
} else if (0 == strcmp (yn , MLE_PROMPT_YES ) || 0 == strcmp (yn , MLE_PROMPT_ALL )) {
373
384
str_append_replace_with_backrefs (& repl_backref , search_mark -> bline -> data , replacement , pcre_rc , pcre_ovector , 30 );
@@ -404,7 +415,11 @@ int cursor_replace(cursor_t *cursor, int interactive, char *opt_regex, char *opt
404
415
if (search_mark_end ) mark_destroy (search_mark_end );
405
416
406
417
if (interactive ) {
407
- MLE_SET_INFO (cursor -> bview -> editor , "replace: Replaced %d instance(s)" , num_replacements );
418
+ if (optret_num_replacements ) {
419
+ * optret_num_replacements = num_replacements ;
420
+ } else {
421
+ MLE_SET_INFO (cursor -> bview -> editor , "%s: Replaced %d instance(s)" , cmd_name , num_replacements );
422
+ }
408
423
if (orig_viewport_y >= 0 ) {
409
424
bview_set_viewport_y (cursor -> bview , orig_viewport_y , 1 );
410
425
} else {
@@ -413,6 +428,8 @@ int cursor_replace(cursor_t *cursor, int interactive, char *opt_regex, char *opt
413
428
bview_draw (cursor -> bview );
414
429
}
415
430
431
+ if (inout_all ) * inout_all = all ;
432
+
416
433
return MLE_OK ;
417
434
}
418
435
0 commit comments