prompt.c 23.5 KB
Newer Older
1
/**************************************************************************
2
 *   prompt.c  --  This file is part of GNU nano.                         *
3
 *                                                                        *
4
 *   Copyright (C) 1999-2011, 2013-2017 Free Software Foundation, Inc.    *
5
6
 *   Copyright (C) 2016 Benno Schulenberg                                 *
 *                                                                        *
7
8
9
10
 *   GNU nano is free software: you can redistribute it and/or modify     *
 *   it under the terms of the GNU General Public License as published    *
 *   by the Free Software Foundation, either version 3 of the License,    *
 *   or (at your option) any later version.                               *
11
 *                                                                        *
12
13
14
15
 *   GNU nano is distributed in the hope that it will be useful,          *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty          *
 *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.              *
 *   See the GNU General Public License for more details.                 *
16
17
 *                                                                        *
 *   You should have received a copy of the GNU General Public License    *
18
 *   along with this program.  If not, see http://www.gnu.org/licenses/.  *
19
20
21
 *                                                                        *
 **************************************************************************/

22
#include "proto.h"
23
24
25
26

#include <string.h>

static char *prompt = NULL;
27
	/* The prompt string used for statusbar questions. */
28
static size_t statusbar_x = HIGHEST_POSITIVE;
29
	/* The cursor position in answer. */
30

31
32
33
/* Read in a keystroke, interpret it if it is a shortcut or toggle, and
 * return it.  Set ran_func to TRUE if we ran a function associated with
 * a shortcut key, and set finished to TRUE if we're done after running
34
35
 * or trying to run a function associated with a shortcut key. */
int do_statusbar_input(bool *ran_func, bool *finished)
36
37
38
39
40
41
42
{
    int input;
	/* The character we read in. */
    static int *kbinput = NULL;
	/* The input buffer. */
    static size_t kbinput_len = 0;
	/* The length of the input buffer. */
43
    const sc *s;
44
    bool have_shortcut = FALSE;
45
    const subnfunc *f;
46
47
48
49
50

    *ran_func = FALSE;
    *finished = FALSE;

    /* Read in a character. */
51
    input = get_kbinput(bottomwin);
52

53
54
55
56
57
#ifndef NANO_TINY
    if (input == KEY_WINCH)
	return KEY_WINCH;
#endif

58
#ifdef ENABLE_MOUSE
59
60
    /* If we got a mouse click and it was on a shortcut, read in the
     * shortcut character. */
61
    if (input == KEY_MOUSE) {
62
	if (do_statusbar_mouse() == 1)
63
	    input = get_kbinput(bottomwin);
64
	else
65
	    return ERR;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
66
    }
67
68
69
#endif

    /* Check for a shortcut in the current list. */
70
    s = get_shortcut(&input);
71
72
73

    /* If we got a shortcut from the current list, or a "universal"
     * statusbar prompt shortcut, set have_shortcut to TRUE. */
74
    have_shortcut = (s != NULL);
75

76
77
    /* If we got a non-high-bit control key, a meta key sequence, or a
     * function key, and it's not a shortcut or toggle, throw it out. */
78
    if (!have_shortcut) {
79
	if (is_ascii_cntrl_char(input) || meta_key || !is_byte(input)) {
80
	    beep();
81
	    input = ERR;
82
83
84
	}
    }

85
86
    /* If the keystroke isn't a shortcut nor a toggle, it's a normal text
     * character: add the it to the input buffer, when allowed. */
87
    if (input != ERR && !have_shortcut) {
88
89
90
91
	/* Only accept input when not in restricted mode, or when not at
	 * the "Write File" prompt, or when there is no filename yet. */
	if (!ISSET(RESTRICTED) || currmenu != MWRITEFILE ||
			openfile->filename[0] == '\0') {
92
93
94
	    kbinput_len++;
	    kbinput = (int *)nrealloc(kbinput, kbinput_len * sizeof(int));
	    kbinput[kbinput_len - 1] = input;
95
	}
96
    }
97

98
99
100
    /* If we got a shortcut, or if there aren't any other keystrokes waiting
     * after the one we read in, we need to insert all the characters in the
     * input buffer (if not empty) into the answer. */
101
    if ((have_shortcut || get_key_buffer_len() == 0) && kbinput != NULL) {
102
103
	/* Inject all characters in the input buffer at once, filtering out
	 * control characters. */
104
	do_statusbar_output(kbinput, kbinput_len, TRUE);
105
106
107
108
109

	/* Empty the input buffer. */
	kbinput_len = 0;
	free(kbinput);
	kbinput = NULL;
110
    }
111

112
    if (have_shortcut) {
113
114
	if (s->scfunc == do_tab || s->scfunc == do_enter)
	    ;
115
	else if (s->scfunc == do_left)
116
117
118
	    do_statusbar_left();
	else if (s->scfunc == do_right)
	    do_statusbar_right();
119
#ifndef NANO_TINY
120
121
122
123
	else if (s->scfunc == do_prev_word_void)
	    do_statusbar_prev_word();
	else if (s->scfunc == do_next_word_void)
	    do_statusbar_next_word();
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
124
#endif
125
	else if (s->scfunc == do_home_void)
126
	    do_statusbar_home();
127
	else if (s->scfunc == do_end_void)
128
129
130
131
	    do_statusbar_end();
	/* When in restricted mode at the "Write File" prompt and the
	 * filename isn't blank, disallow any input and deletion. */
	else if (ISSET(RESTRICTED) && currmenu == MWRITEFILE &&
132
133
134
135
136
				openfile->filename[0] != '\0' &&
				(s->scfunc == do_verbatim_input ||
				s->scfunc == do_cut_text_void ||
				s->scfunc == do_delete ||
				s->scfunc == do_backspace))
137
	    ;
138
	else if (s->scfunc == do_verbatim_input)
139
	    do_statusbar_verbatim_input();
140
	else if (s->scfunc == do_cut_text_void)
141
142
143
144
145
	    do_statusbar_cut_text();
	else if (s->scfunc == do_delete)
	    do_statusbar_delete();
	else if (s->scfunc == do_backspace)
	    do_statusbar_backspace();
146
147
148
149
	else if (s->scfunc == do_uncut_text) {
	    if (cutbuffer != NULL)
		do_statusbar_uncut_text();
	} else {
150
151
152
153
154
155
156
157
158
159
160
161
162
	    /* Handle any other shortcut in the current menu, setting
	     * ran_func to TRUE if we try to run their associated functions,
	     * and setting finished to TRUE to indicatethat we're done after
	     * running or trying to run their associated functions. */
	    f = sctofunc(s);
	    if (s->scfunc != NULL) {
		*ran_func = TRUE;
		if (f && (!ISSET(VIEW_MODE) || f->viewok) &&
				f->scfunc != do_gotolinecolumn_void)
		    f->scfunc();
	    }
	    *finished = TRUE;
	}
163
164
165
166
167
    }

    return input;
}

168
#ifdef ENABLE_MOUSE
169
/* Handle a mouse click on the statusbar prompt or the shortcut list. */
170
int do_statusbar_mouse(void)
171
172
{
    int mouse_x, mouse_y;
173
    int retval = get_mouseinput(&mouse_x, &mouse_y, TRUE);
174

175
    /* We can click on the statusbar window text to move the cursor. */
176
    if (retval == 0 && wmouse_trafo(bottomwin, &mouse_y, &mouse_x, FALSE)) {
177
	size_t start_col;
178

179
	start_col = strlenpt(prompt) + 2;
180

181
	/* Move to where the click occurred. */
182
	if (mouse_x >= start_col && mouse_y == 0) {
183
	    statusbar_x = actual_x(answer,
184
			get_statusbar_page_start(start_col, start_col +
185
			statusbar_xplustabs()) + mouse_x - start_col);
186
	    update_the_statusbar();
187
188
189
190
191
192
193
	}
    }

    return retval;
}
#endif

194
195
/* The user typed input_len multibyte characters.  Add them to the answer,
 * filtering out ASCII control characters if filtering is TRUE. */
196
void do_statusbar_output(int *the_input, size_t input_len,
197
	bool filtering)
198
{
199
    char *output = charalloc(input_len + 1);
200
    char onechar[MAXCHARLEN];
201
    int i, char_len;
202

203
    /* Copy the typed stuff so it can be treated. */
204
    for (i = 0; i < input_len; i++)
205
206
207
208
	output[i] = (char)the_input[i];
    output[i] = '\0';

    i = 0;
209

210
    while (i < input_len) {
211
212
213
	/* Encode any NUL byte as 0x0A. */
	if (output[i] == '\0')
	    output[i] = '\n';
214
215

	/* Interpret the next multibyte character. */
216
	char_len = parse_mbchar(output + i, onechar, NULL);
217

218
	i += char_len;
219

220
	/* When filtering, skip any ASCII control character. */
221
	if (filtering && is_ascii_cntrl_char(*(output + i - char_len)))
222
223
	    continue;

224
	/* Insert the typed character into the existing answer string. */
225
226
	answer = charealloc(answer, strlen(answer) + char_len + 1);
	charmove(answer + statusbar_x + char_len, answer + statusbar_x,
227
				strlen(answer) - statusbar_x + 1);
228
	strncpy(answer + statusbar_x, onechar, char_len);
229

230
	statusbar_x += char_len;
231
232
    }

233
    free(output);
234

235
    update_the_statusbar();
236
237
}

238
/* Move to the beginning of the answer. */
239
240
void do_statusbar_home(void)
{
241
    statusbar_x = 0;
242
    update_the_statusbar();
243
244
}

245
/* Move to the end of the answer. */
246
247
248
void do_statusbar_end(void)
{
    statusbar_x = strlen(answer);
249
    update_the_statusbar();
250
251
}

252
253
/* Move left one character. */
void do_statusbar_left(void)
254
{
255
256
    if (statusbar_x > 0) {
	statusbar_x = move_mbleft(answer, statusbar_x);
257
	update_the_statusbar();
258
    }
259
260
}

261
262
/* Move right one character. */
void do_statusbar_right(void)
263
{
264
    if (answer[statusbar_x] != '\0') {
265
	statusbar_x = move_mbright(answer, statusbar_x);
266
	update_the_statusbar();
267
    }
268
269
}

270
/* Backspace over one character. */
271
272
273
void do_statusbar_backspace(void)
{
    if (statusbar_x > 0) {
274
	statusbar_x = move_mbleft(answer, statusbar_x);
275
276
277
278
	do_statusbar_delete();
    }
}

279
/* Delete one character. */
280
281
282
void do_statusbar_delete(void)
{
    if (answer[statusbar_x] != '\0') {
283
	int char_len = parse_mbchar(answer + statusbar_x, NULL, NULL);
284

285
286
	charmove(answer + statusbar_x, answer + statusbar_x + char_len,
			strlen(answer) - statusbar_x - char_len + 1);
287

288
	update_the_statusbar();
289
290
291
    }
}

292
/* Zap some or all text from the answer. */
293
294
void do_statusbar_cut_text(void)
{
295
    if (!ISSET(CUT_FROM_CURSOR))
296
	statusbar_x = 0;
297

298
    answer[statusbar_x] = '\0';
299

300
    update_the_statusbar();
301
302
}

303
#ifndef NANO_TINY
304
/* Move to the next word in the answer. */
305
void do_statusbar_next_word(void)
306
{
307
    bool seen_space = !is_word_mbchar(answer + statusbar_x, FALSE);
308

309
310
311
    /* Move forward until we reach the start of a word. */
    while (answer[statusbar_x] != '\0') {
	statusbar_x = move_mbright(answer, statusbar_x);
312

313
314
315
316
317
	/* If this is not a word character, then it's a separator; else
	 * if we've already seen a separator, then it's a word start. */
	if (!is_word_mbchar(answer + statusbar_x, FALSE))
	    seen_space = TRUE;
	else if (seen_space)
318
319
320
	    break;
    }

321
    update_the_statusbar();
322
323
}

324
/* Move to the previous word in the answer. */
325
void do_statusbar_prev_word(void)
326
{
327
    bool seen_a_word = FALSE, step_forward = FALSE;
328

329
330
    /* Move backward until we pass over the start of a word. */
    while (statusbar_x != 0) {
331
332
	statusbar_x = move_mbleft(answer, statusbar_x);

333
334
335
336
337
	if (is_word_mbchar(answer + statusbar_x, FALSE))
	    seen_a_word = TRUE;
	else if (seen_a_word) {
	    /* This is space now: we've overshot the start of the word. */
	    step_forward = TRUE;
338
339
340
341
	    break;
	}
    }

342
343
344
    if (step_forward)
	/* Move one character forward again to sit on the start of the word. */
	statusbar_x = move_mbright(answer, statusbar_x);
345

346
    update_the_statusbar();
347
}
348
#endif /* !NANO_TINY */
349

350
351
/* Get verbatim input and inject it into the answer, without filtering. */
void do_statusbar_verbatim_input(void)
352
353
{
    int *kbinput;
354
    size_t kbinput_len;
355
356
357

    kbinput = get_verbatim_kbinput(bottomwin, &kbinput_len);

358
    do_statusbar_output(kbinput, kbinput_len, FALSE);
359
360
}

361
/* Return the zero-based column position of the cursor in the answer. */
362
363
364
365
366
size_t statusbar_xplustabs(void)
{
    return strnlenpt(answer, statusbar_x);
}

367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/* Paste the first line of the cutbuffer into the current answer. */
void do_statusbar_uncut_text(void)
{
    size_t pastelen = strlen(cutbuffer->data);
    char *fusion = charalloc(strlen(answer) + pastelen + 1);

    /* Concatenate: the current answer before the cursor, the first line
     * of the cutbuffer, plus the rest of the current answer. */
    strncpy(fusion, answer, statusbar_x);
    strncpy(fusion + statusbar_x, cutbuffer->data, pastelen);
    strcpy(fusion + statusbar_x + pastelen, answer + statusbar_x);

    free(answer);
    answer = fusion;
    statusbar_x += pastelen;
}

384
385
386
387
388
/* Return the column number of the first character of the answer that is
 * displayed in the statusbar when the cursor is at the given column,
 * with the available room for the answer starting at base.  Note that
 * (0 <= column - get_statusbar_page_start(column) < COLS). */
size_t get_statusbar_page_start(size_t base, size_t column)
389
{
390
    if (column == base || column < COLS - 1)
391
	return 0;
392
393
    else if (COLS > base + 2)
	return column - base - 1 - (column - base - 1) % (COLS - base - 2);
394
    else
395
	return column - 2;
396
397
}

398
/* Reinitialize the cursor position in the answer. */
399
400
void reinit_statusbar_x(void)
{
401
    statusbar_x = HIGHEST_POSITIVE;
402
403
}

404
/* Redraw the promptbar and place the cursor at the right spot. */
405
void update_the_statusbar(void)
406
{
407
408
    size_t base = strlenpt(prompt) + 2;
    size_t the_page, end_page, column;
409
410
    char *expanded;

411
412
    the_page = get_statusbar_page_start(base, base + strnlenpt(answer, statusbar_x));
    end_page = get_statusbar_page_start(base, base + strlenpt(answer) - 1);
413

414
    /* Color the promptbar over its full width. */
415
    wattron(bottomwin, interface_color_pair[TITLE_BAR]);
416
417
    blank_statusbar();

418
    mvwaddstr(bottomwin, 0, 0, prompt);
419
    waddch(bottomwin, ':');
420
    waddch(bottomwin, (the_page == 0) ? ' ' : '<');
421

422
    expanded = display_string(answer, the_page, COLS - base - 1, FALSE);
423
424
425
    waddstr(bottomwin, expanded);
    free(expanded);

426
    waddch(bottomwin, (the_page >= end_page) ? ' ' : '>');
427

428
    wattroff(bottomwin, interface_color_pair[TITLE_BAR]);
429

430
431
    /* Work around a cursor-misplacement bug in VTEs. */
    wmove(bottomwin, 0, 0);
432
    wrefresh(bottomwin);
433
434
435
436
437

    /* Place the cursor at statusbar_x in the answer. */
    column = base + statusbar_xplustabs();
    wmove(bottomwin, 0, column - get_statusbar_page_start(base, column));
    wnoutrefresh(bottomwin);
438
439
}

440
/* Get a string of input at the statusbar prompt. */
441
functionptrtype acquire_an_answer(int *actual, bool allow_tabs,
442
	bool allow_files, bool *listed,
443
#ifndef DISABLE_HISTORIES
444
445
	filestruct **history_list,
#endif
446
	void (*refresh_func)(void))
447
{
448
    int kbinput = ERR;
449
    bool ran_func, finished;
450
    functionptrtype func;
451
#ifdef ENABLE_TABCOMP
452
453
454
    bool tabbed = FALSE;
	/* Whether we've pressed Tab. */
#endif
455
#ifndef DISABLE_HISTORIES
456
457
458
459
460
    char *history = NULL;
	/* The current history string. */
    char *magichistory = NULL;
	/* The temporary string typed at the bottom of the history, if
	 * any. */
461
#ifdef ENABLE_TABCOMP
462
463
464
465
466
467
    int last_kbinput = ERR;
	/* The key we pressed before the current key. */
    size_t complete_len = 0;
	/* The length of the original string that we're trying to
	 * tab complete, if any. */
#endif
468
#endif /* !DISABLE_HISTORIES */
469

470
    if (statusbar_x > strlen(answer))
471
	statusbar_x = strlen(answer);
472

473
#ifdef DEBUG
474
    fprintf(stderr, "acquiring: answer = \"%s\", statusbar_x = %lu\n", answer, (unsigned long) statusbar_x);
475
#endif
476

477
    update_the_statusbar();
478

479
    while (TRUE) {
480
	/* Ensure the cursor is shown when waiting for input. */
481
482
	curs_set(1);

483
	kbinput = do_statusbar_input(&ran_func, &finished);
484

485
#ifndef NANO_TINY
486
	/* If the window size changed, go reformat the prompt string. */
487
488
	if (kbinput == KEY_WINCH) {
	    refresh_func();
489
	    *actual = KEY_WINCH;
490
#ifndef DISABLE_HISTORIES
491
	    free(magichistory);
492
#endif
493
	    return NULL;
494
	}
495
496
#endif /* !NANO_TINY */

497
	func = func_from_key(&kbinput);
498

499
	if (func == do_cancel || func == do_enter)
500
	    break;
501

502
#ifdef ENABLE_TABCOMP
503
	if (func != do_tab)
504
505
	    tabbed = FALSE;

506
	if (func == do_tab) {
507
#ifndef DISABLE_HISTORIES
508
	    if (history_list != NULL) {
509
		if (last_kbinput != the_code_for(do_tab, TAB_CODE))
510
		    complete_len = strlen(answer);
511

512
		if (complete_len > 0) {
513
514
		    answer = get_history_completion(history_list,
					answer, complete_len);
515
516
517
		    statusbar_x = strlen(answer);
		}
	    } else
518
#endif
519
520
	    if (allow_tabs)
		answer = input_tab(answer, allow_files, &statusbar_x,
521
					&tabbed, refresh_func, listed);
522
	} else
523
#endif /* ENABLE_TABCOMP */
524
#ifndef DISABLE_HISTORIES
525
	if (func == get_history_older_void) {
526
527
528
	    if (history_list != NULL) {
		/* If we're scrolling up at the bottom of the history list
		 * and answer isn't blank, save answer in magichistory. */
529
		if ((*history_list)->next == NULL && *answer != '\0')
530
531
532
533
534
535
536
537
		    magichistory = mallocstrcpy(magichistory, answer);

		/* Get the older search from the history list and save it in
		 * answer.  If there is no older search, don't do anything. */
		if ((history = get_history_older(history_list)) != NULL) {
		    answer = mallocstrcpy(answer, history);
		    statusbar_x = strlen(answer);
		}
538

539
540
541
542
		/* This key has a shortcut-list entry when it's used to
		 * move to an older search, which means that finished has
		 * been set to TRUE.  Set it back to FALSE here, so that
		 * we aren't kicked out of the statusbar prompt. */
543
		finished = FALSE;
544
	    }
545
	} else if (func == get_history_newer_void) {
546
547
548
549
550
551
552
	    if (history_list != NULL) {
		/* Get the newer search from the history list and save it in
		 * answer.  If there is no newer search, don't do anything. */
		if ((history = get_history_newer(history_list)) != NULL) {
		    answer = mallocstrcpy(answer, history);
		    statusbar_x = strlen(answer);
		}
553

554
555
556
557
		/* If, after scrolling down, we're at the bottom of the
		 * history list, answer is blank, and magichistory is set,
		 * save magichistory in answer. */
		if ((*history_list)->next == NULL &&
558
559
560
561
			*answer == '\0' && magichistory != NULL) {
		    answer = mallocstrcpy(answer, magichistory);
		    statusbar_x = strlen(answer);
		}
562

563
564
565
566
567
568
		/* This key has a shortcut-list entry when it's used to
		 * move to a newer search, which means that finished has
		 * been set to TRUE.  Set it back to FALSE here, so that
		 * we aren't kicked out of the statusbar prompt. */
		finished = FALSE;
	    }
569
	} else
570
#endif /* !DISABLE_HISTORIES */
571
	if (func == do_help_void) {
572
573
574
575
576
577
	    /* This key has a shortcut-list entry when it's used to go to
	     * the help browser or display a message indicating that help
	     * is disabled, which means that finished has been set to TRUE.
	     * Set it back to FALSE here, so that we aren't kicked out of
	     * the statusbar prompt. */
	    finished = FALSE;
578
579
	}

580
581
	/* If we have a shortcut with an associated function, break out if
	 * we're finished after running or trying to run the function. */
582
583
584
	if (finished)
	    break;

585
586
	update_the_statusbar();

587
#if !defined(DISABLE_HISTORIES) && defined(ENABLE_TABCOMP)
588
589
	last_kbinput = kbinput;
#endif
590
    }
591

592
#ifndef DISABLE_HISTORIES
593
    /* Set the current position in the history list to the bottom. */
594
595
    if (history_list != NULL) {
	history_reset(*history_list);
596
	free(magichistory);
597
598
599
    }
#endif

600
    *actual = kbinput;
601
602

    return func;
603
604
}

605
606
607
608
/* Ask a question on the statusbar.  The prompt will be stored in the
 * static prompt, which should be NULL initially, and the answer will be
 * stored in the answer global.  Returns -1 on aborted enter, -2 on a
 * blank string, and 0 otherwise, the valid shortcut key caught.
609
610
611
 * curranswer is any editable text that we want to put up by default,
 * and refresh_func is the function we want to call to refresh the edit
 * window.
612
613
 *
 * The allow_tabs parameter indicates whether we should allow tabs to be
614
 * interpreted.  The allow_files parameter indicates whether we should
615
 * allow all files (as opposed to just directories) to be tab completed. */
616
int do_prompt(bool allow_tabs, bool allow_files,
617
	int menu, const char *curranswer,
618
#ifndef DISABLE_HISTORIES
619
620
	filestruct **history_list,
#endif
621
	void (*refresh_func)(void), const char *msg, ...)
622
623
{
    va_list ap;
624
    int retval;
625
    functionptrtype func = NULL;
626
    bool listed = FALSE;
627
    /* Save a possible current statusbar x position and prompt. */
628
    size_t was_statusbar_x = statusbar_x;
629
    char *saved_prompt = prompt;
630

631
    bottombars(menu);
632

633
634
    answer = mallocstrcpy(answer, curranswer);

635
636
637
#ifndef NANO_TINY
  redo_theprompt:
#endif
638
    prompt = charalloc((COLS * MAXCHARLEN) + 1);
639
    va_start(ap, msg);
640
    vsnprintf(prompt, COLS * MAXCHARLEN, msg, ap);
641
642
    va_end(ap);
    /* Reserve five columns for colon plus angles plus answer, ":<aa>". */
643
    prompt[actual_x(prompt, (COLS < 5) ? 0 : COLS - 5)] = '\0';
644

645
    func = acquire_an_answer(&retval, allow_tabs, allow_files, &listed,
646
#ifndef DISABLE_HISTORIES
647
			history_list,
648
#endif
649
			refresh_func);
650

651
    free(prompt);
652
    prompt = saved_prompt;
653
654
655
656
657

#ifndef NANO_TINY
    if (retval == KEY_WINCH)
	goto redo_theprompt;
#endif
658

659
660
    /* If we're done with this prompt, restore the x position to what
     * it was at a possible previous prompt. */
661
    if (func == do_cancel || func == do_enter)
662
	statusbar_x = was_statusbar_x;
663

664
665
    /* If we left the prompt via Cancel or Enter, set the return value
     * properly. */
666
    if (func == do_cancel)
667
	retval = -1;
668
    else if (func == do_enter)
669
	retval = (*answer == '\0') ? -2 : 0;
670

671
672
673
674
675
676
677
    blank_statusbar();
    wnoutrefresh(bottomwin);

#ifdef DEBUG
    fprintf(stderr, "answer = \"%s\"\n", answer);
#endif

678
#ifdef ENABLE_TABCOMP
679
680
681
682
683
684
    /* If we've done tab completion, there might still be a list of
     * filename matches on the edit window.  Clear them off. */
    if (listed)
	refresh_func();
#endif

685
686
687
    return retval;
}

688
689
690
/* Ask a simple Yes/No (and optionally All) question, specified in msg,
 * on the statusbar.  Return 1 for Yes, 0 for No, 2 for All (if all is
 * TRUE when passed in), and -1 for Cancel. */
691
int do_yesno_prompt(bool all, const char *msg)
692
{
693
    int response = -2, width = 16;
694
695
    char *message = display_string(msg, 0, COLS, FALSE);

696
    /* TRANSLATORS: For the next three strings, if possible, specify
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
697
     * the single-byte shortcuts for both your language and English.
698
699
700
701
702
703
704
705
     * For example, in French: "OoYy", for both "Oui" and "Yes". */
    const char *yesstr = _("Yy");
    const char *nostr = _("Nn");
    const char *allstr = _("Aa");

    /* The above three variables consist of all the single-byte characters
     * that are accepted for the corresponding answer.  Of each variable,
     * the first character is displayed in the help lines. */
706

707
    while (response == -2) {
708
709
710
	int kbinput;
	functionptrtype func;

711
712
	if (!ISSET(NO_HELP)) {
	    char shortstr[3];
713
		/* Temporary string for (translated) " Y", " N" and " A". */
714

715
716
	    if (COLS < 32)
		width = COLS / 2;
717

718
719
	    /* Clear the shortcut list from the bottom of the screen. */
	    blank_bottombars();
720

721
722
723
724
	    /* Now show the ones for "Yes", "No", "Cancel" and maybe "All". */
	    sprintf(shortstr, " %c", yesstr[0]);
	    wmove(bottomwin, 1, 0);
	    onekey(shortstr, _("Yes"), width);
725

726
727
	    if (all) {
		shortstr[1] = allstr[0];
728
		wmove(bottomwin, 1, width);
729
730
		onekey(shortstr, _("All"), width);
	    }
731

732
	    shortstr[1] = nostr[0];
733
	    wmove(bottomwin, 2, 0);
734
	    onekey(shortstr, _("No"), width);
735

736
	    wmove(bottomwin, 2, width);
737
738
	    onekey("^C", _("Cancel"), width);
	}
739

740
	/* Color the statusbar over its full width and display the question. */
741
	wattron(bottomwin, interface_color_pair[TITLE_BAR]);
742
	blank_statusbar();
743
	mvwaddnstr(bottomwin, 0, 0, message, actual_x(message, COLS - 1));
744
	wattroff(bottomwin, interface_color_pair[TITLE_BAR]);
745

746
	wnoutrefresh(bottomwin);
747

748
749
750
751
	/* When not replacing, show the cursor. */
	if (!all)
	    curs_set(1);

752
	currmenu = MYESNO;
753
	kbinput = get_kbinput(bottomwin);
754

755
	func = func_from_key(&kbinput);
756

757
	if (func == do_cancel)
758
	    response = -1;
759
#ifdef ENABLE_MOUSE
760
	else if (kbinput == KEY_MOUSE) {
761
	    int mouse_x, mouse_y;
762
763
764
765
766
767
768
769
770
771
772
773
774
	    /* We can click on the Yes/No/All shortcuts to select an answer. */
	    if (get_mouseinput(&mouse_x, &mouse_y, FALSE) == 0 &&
			wmouse_trafo(bottomwin, &mouse_y, &mouse_x, FALSE) &&
			mouse_x < (width * 2) && mouse_y > 0) {
		int x = mouse_x / width;
			/* The x-coordinate among the Yes/No/All shortcuts. */
		int y = mouse_y - 1;
			/* The y-coordinate among the Yes/No/All shortcuts. */

		assert(0 <= x && x <= 1 && 0 <= y && y <= 1);

		/* x == 0 means they clicked Yes or No.
		 * y == 0 means Yes or All. */
775
		response = -2 * x * y + x - y + 1;
776

777
778
		if (response == 2 && !all)
		    response = -2;
779
	    }
780
	}
781
#endif /* ENABLE_MOUSE */
782
	else {
783
784
	    /* Look for the kbinput in the Yes, No (and All) strings. */
	    if (strchr(yesstr, kbinput) != NULL)
785
		response = 1;
786
	    else if (strchr(nostr, kbinput) != NULL)
787
		response = 0;
788
	    else if (all && strchr(allstr, kbinput) != NULL)
789
		response = 2;
790
	}
791
    }
792

793
794
    free(message);

795
    return response;
796
}