prompt.c 24.1 KB
Newer Older
1
/**************************************************************************
2
 *   prompt.c  --  This file is part of GNU nano.                         *
3
 *                                                                        *
4
 *   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,  *
5
 *   2008, 2009, 2010, 2011, 2013, 2014 Free Software Foundation, Inc.    *
6
7
 *   Copyright (C) 2016 Benno Schulenberg                                 *
 *                                                                        *
8
9
10
11
 *   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.                               *
12
 *                                                                        *
13
14
15
16
 *   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.                 *
17
18
 *                                                                        *
 *   You should have received a copy of the GNU General Public License    *
19
 *   along with this program.  If not, see http://www.gnu.org/licenses/.  *
20
21
22
 *                                                                        *
 **************************************************************************/

23
#include "proto.h"
24

25
#include <stdio.h>
26
27
28
29
#include <stdarg.h>
#include <string.h>

static char *prompt = NULL;
30
	/* The prompt string used for statusbar questions. */
31
static size_t statusbar_x = HIGHEST_POSITIVE;
32
	/* The cursor position in answer. */
33

34
35
36
/* 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
37
38
 * or trying to run a function associated with a shortcut key. */
int do_statusbar_input(bool *ran_func, bool *finished)
39
40
41
42
43
44
45
{
    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. */
46
    const sc *s;
47
    bool have_shortcut = FALSE;
48
    const subnfunc *f;
49
50
51
52
53

    *ran_func = FALSE;
    *finished = FALSE;

    /* Read in a character. */
54
    input = get_kbinput(bottomwin);
55

56
57
58
59
60
#ifndef NANO_TINY
    if (input == KEY_WINCH)
	return KEY_WINCH;
#endif

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

    /* Check for a shortcut in the current list. */
73
    s = get_shortcut(&input);
74
75
76

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

79
80
    /* 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. */
81
    if (!have_shortcut) {
82
	if (is_ascii_cntrl_char(input) || meta_key || !is_byte(input)) {
83
	    beep();
84
	    input = ERR;
85
86
87
	}
    }

88
89
    /* 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. */
90
    if (input != ERR && !have_shortcut) {
91
92
93
94
	/* 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') {
95
96
97
	    kbinput_len++;
	    kbinput = (int *)nrealloc(kbinput, kbinput_len * sizeof(int));
	    kbinput[kbinput_len - 1] = input;
98
	}
99
100
     }

101
102
103
    /* 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. */
104
    if ((have_shortcut || get_key_buffer_len() == 0) && kbinput != NULL) {
105
106
107
108
109
110
111
112
	/* Inject all characters in the input buffer at once, filtering out
	 * control characters. */
	do_statusbar_output(kbinput, kbinput_len, TRUE, NULL);

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

115
    if (have_shortcut) {
116
117
	if (s->scfunc == do_tab || s->scfunc == do_enter)
	    ;
118
	else if (s->scfunc == do_left)
119
120
121
	    do_statusbar_left();
	else if (s->scfunc == do_right)
	    do_statusbar_right();
122
#ifndef NANO_TINY
123
124
125
126
	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
127
#endif
128
129
130
131
132
133
134
	else if (s->scfunc == do_home)
	    do_statusbar_home();
	else if (s->scfunc == do_end)
	    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 &&
135
136
137
138
139
				openfile->filename[0] != '\0' &&
				(s->scfunc == do_verbatim_input ||
				s->scfunc == do_cut_text_void ||
				s->scfunc == do_delete ||
				s->scfunc == do_backspace))
140
141
142
143
144
145
146
147
148
149
150
	    ;
	else if (s->scfunc == do_verbatim_input) {
	    bool got_newline = FALSE;
		/* Whether we got a verbatim ^J. */

	    do_statusbar_verbatim_input(&got_newline);

	    /* If we got a verbatim ^J, remove it from the input buffer,
	     * fake a press of Enter, and indicate that we're done. */
	    if (got_newline) {
		get_input(NULL, 1);
151
		input = the_code_for(do_enter, 0);
152
		*finished = TRUE;
153
	    }
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
	} else if (s->scfunc == do_cut_text_void)
	    do_statusbar_cut_text();
	else if (s->scfunc == do_delete)
	    do_statusbar_delete();
	else if (s->scfunc == do_backspace)
	    do_statusbar_backspace();
	else {
	    /* 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;
	}
174
175
176
177
178
179
    }

    return input;
}

#ifndef DISABLE_MOUSE
180
/* Handle a mouse click on the statusbar prompt or the shortcut list. */
181
int do_statusbar_mouse(void)
182
183
{
    int mouse_x, mouse_y;
184
    int retval = get_mouseinput(&mouse_x, &mouse_y, TRUE);
185

186
    /* We can click on the statusbar window text to move the cursor. */
187
    if (retval == 0 && wmouse_trafo(bottomwin, &mouse_y, &mouse_x, FALSE)) {
188
	size_t start_col;
189

190
	start_col = strlenpt(prompt) + 2;
191

192
	/* Move to where the click occurred. */
193
	if (mouse_x >= start_col && mouse_y == 0) {
194
	    statusbar_x = actual_x(answer,
195
			get_statusbar_page_start(start_col, start_col +
196
			statusbar_xplustabs()) + mouse_x - start_col);
197
	    update_the_statusbar();
198
199
200
201
202
203
204
	}
    }

    return retval;
}
#endif

205
/* The user typed input_len multibyte characters.  Add them to the
206
 * statusbar prompt, setting got_newline to TRUE if we got a verbatim ^J,
207
208
 * and filtering out ASCII control characters if filtering is TRUE. */
void do_statusbar_output(int *the_input, size_t input_len,
209
	bool filtering, bool *got_newline)
210
{
211
    char *output = charalloc(input_len + 1);
212
    char *char_buf = charalloc(mb_cur_max());
213
    int i, char_len;
214

215
    /* Copy the typed stuff so it can be treated. */
216
    for (i = 0; i < input_len; i++)
217
218
219
220
	output[i] = (char)the_input[i];
    output[i] = '\0';

    i = 0;
221

222
223
224
    while (i < input_len) {
	/* When not filtering, convert nulls and stop at a newline. */
	if (!filtering) {
225
	    if (output[i] == '\0')
226
		output[i] = '\n';
227
	    else if (output[i] == '\n') {
228
		/* Put back the rest of the characters for reparsing,
229
		 * indicate that we got a ^J and get out. */
230
		unparse_kbinput(output + i, input_len - i);
231
		*got_newline = TRUE;
232
233
234
235
236
		return;
	    }
	}

	/* Interpret the next multibyte character. */
237
	char_len = parse_mbchar(output + i, char_buf, NULL);
238

239
	i += char_len;
240

241
	/* When filtering, skip any ASCII control character. */
242
	if (filtering && is_ascii_cntrl_char(*(output + i - char_len)))
243
244
	    continue;

245
	/* Insert the typed character into the existing answer string. */
246
247
	answer = charealloc(answer, strlen(answer) + char_len + 1);
	charmove(answer + statusbar_x + char_len, answer + statusbar_x,
248
				strlen(answer) - statusbar_x + 1);
249
	strncpy(answer + statusbar_x, char_buf, char_len);
250

251
	statusbar_x += char_len;
252
253
254
    }

    free(char_buf);
255
    free(output);
256

257
    update_the_statusbar();
258
259
}

260
/* Move to the beginning of the answer. */
261
262
void do_statusbar_home(void)
{
263
    statusbar_x = 0;
264
    update_the_statusbar();
265
266
}

267
/* Move to the end of the answer. */
268
269
270
void do_statusbar_end(void)
{
    statusbar_x = strlen(answer);
271
    update_the_statusbar();
272
273
}

274
275
/* Move left one character. */
void do_statusbar_left(void)
276
{
277
278
    if (statusbar_x > 0) {
	statusbar_x = move_mbleft(answer, statusbar_x);
279
	update_the_statusbar();
280
    }
281
282
}

283
284
/* Move right one character. */
void do_statusbar_right(void)
285
{
286
    if (answer[statusbar_x] != '\0') {
287
	statusbar_x = move_mbright(answer, statusbar_x);
288
	update_the_statusbar();
289
    }
290
291
}

292
/* Backspace over one character. */
293
294
295
void do_statusbar_backspace(void)
{
    if (statusbar_x > 0) {
296
	statusbar_x = move_mbleft(answer, statusbar_x);
297
298
299
300
	do_statusbar_delete();
    }
}

301
/* Delete one character. */
302
303
304
void do_statusbar_delete(void)
{
    if (answer[statusbar_x] != '\0') {
305
	int char_len = parse_mbchar(answer + statusbar_x, NULL, NULL);
306

307
308
	charmove(answer + statusbar_x, answer + statusbar_x + char_len,
			strlen(answer) - statusbar_x - char_len + 1);
309

310
	update_the_statusbar();
311
312
313
    }
}

314
/* Zap some or all text from the answer. */
315
316
void do_statusbar_cut_text(void)
{
317
    if (!ISSET(CUT_TO_END))
318
	statusbar_x = 0;
319
320

    null_at(&answer, statusbar_x);
321

322
    update_the_statusbar();
323
324
}

325
#ifndef NANO_TINY
326
/* Move to the next word in the answer. */
327
void do_statusbar_next_word(void)
328
{
329
    bool seen_space = !is_word_mbchar(answer + statusbar_x, FALSE);
330

331
332
333
    /* Move forward until we reach the start of a word. */
    while (answer[statusbar_x] != '\0') {
	statusbar_x = move_mbright(answer, statusbar_x);
334

335
336
337
338
339
	/* 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)
340
341
342
	    break;
    }

343
    update_the_statusbar();
344
345
}

346
/* Move to the previous word in the answer. */
347
void do_statusbar_prev_word(void)
348
{
349
    bool seen_a_word = FALSE, step_forward = FALSE;
350

351
352
    /* Move backward until we pass over the start of a word. */
    while (statusbar_x != 0) {
353
354
	statusbar_x = move_mbleft(answer, statusbar_x);

355
356
357
358
359
	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;
360
361
362
363
	    break;
	}
    }

364
365
366
    if (step_forward)
	/* Move one character forward again to sit on the start of the word. */
	statusbar_x = move_mbright(answer, statusbar_x);
367

368
    update_the_statusbar();
369
}
370
#endif /* !NANO_TINY */
371

372
/* Get verbatim input, setting got_newline to TRUE if we get a ^J as
373
 * part of the verbatim input. */
374
void do_statusbar_verbatim_input(bool *got_newline)
375
376
{
    int *kbinput;
377
    size_t kbinput_len;
378
379
380
381
382

    /* Read in all the verbatim characters. */
    kbinput = get_verbatim_kbinput(bottomwin, &kbinput_len);

    /* Display all the verbatim characters at once, not filtering out
383
     * control characters. */
384
    do_statusbar_output(kbinput, kbinput_len, FALSE, got_newline);
385
386
}

387
/* Return the zero-based column position of the cursor in the answer. */
388
389
390
391
392
size_t statusbar_xplustabs(void)
{
    return strnlenpt(answer, statusbar_x);
}

393
394
395
396
397
/* 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)
398
{
399
    if (column == base || column < COLS - 1)
400
	return 0;
401
402
    else if (COLS > base + 2)
	return column - base - 1 - (column - base - 1) % (COLS - base - 2);
403
    else
404
	return column - 2;
405
406
}

407
/* Reinitialize the cursor position in the answer. */
408
409
void reinit_statusbar_x(void)
{
410
    statusbar_x = HIGHEST_POSITIVE;
411
412
}

413
/* Put the cursor in the answer at statusbar_x. */
414
415
void reset_statusbar_cursor(void)
{
416
    size_t start_col = strlenpt(prompt) + 2;
417
418
    size_t xpt = statusbar_xplustabs();

419
420
421
422
423
    /* Work around a cursor-misplacement bug in VTEs. */
    wmove(bottomwin, 0, 0);
    wnoutrefresh(bottomwin);
    doupdate();

424
    wmove(bottomwin, 0, start_col + xpt -
425
			get_statusbar_page_start(start_col, start_col + xpt));
426
427

    wnoutrefresh(bottomwin);
428
429
}

430
431
/* Repaint the statusbar. */
void update_the_statusbar(void)
432
{
433
    size_t base, the_page, end_page;
434
435
    char *expanded;

436
437
438
    base = strlenpt(prompt) + 2;
    the_page = get_statusbar_page_start(base, base + strnlenpt(answer, statusbar_x));
    end_page = get_statusbar_page_start(base, base + strlenpt(answer) - 1);
439

440
    wattron(bottomwin, interface_color_pair[TITLE_BAR]);
441
442
443

    blank_statusbar();

444
    mvwaddstr(bottomwin, 0, 0, prompt);
445
    waddch(bottomwin, ':');
446
    waddch(bottomwin, (the_page == 0) ? ' ' : '<');
447

448
    expanded = display_string(answer, the_page, COLS - base - 1, FALSE);
449
450
451
    waddstr(bottomwin, expanded);
    free(expanded);

452
    waddch(bottomwin, (the_page >= end_page) ? ' ' : '>');
453

454
    wattroff(bottomwin, interface_color_pair[TITLE_BAR]);
455

456
    reset_statusbar_cursor();
457
458
}

459
/* Get a string of input at the statusbar prompt. */
460
functionptrtype acquire_an_answer(int *actual, bool allow_tabs,
461
#ifndef DISABLE_TABCOMP
462
	bool allow_files, bool *listed,
463
#endif
464
#ifndef DISABLE_HISTORIES
465
466
	filestruct **history_list,
#endif
467
	void (*refresh_func)(void))
468
{
469
    int kbinput = ERR;
470
    bool ran_func, finished;
471
    functionptrtype func;
472
473
474
475
#ifndef DISABLE_TABCOMP
    bool tabbed = FALSE;
	/* Whether we've pressed Tab. */
#endif
476
#ifndef DISABLE_HISTORIES
477
478
479
480
481
482
483
484
485
486
487
488
    char *history = NULL;
	/* The current history string. */
    char *magichistory = NULL;
	/* The temporary string typed at the bottom of the history, if
	 * any. */
#ifndef DISABLE_TABCOMP
    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
489
#endif /* !DISABLE_HISTORIES */
490

491
    if (statusbar_x > strlen(answer))
492
	statusbar_x = strlen(answer);
493

494
#ifdef DEBUG
495
    fprintf(stderr, "acquiring: answer = \"%s\", statusbar_x = %lu\n", answer, (unsigned long) statusbar_x);
496
#endif
497

498
    update_the_statusbar();
499

500
    /* Refresh edit window and statusbar before getting input. */
501
502
503
    wnoutrefresh(edit);
    wnoutrefresh(bottomwin);

504
    while (TRUE) {
505
	/* Ensure the cursor is shown when waiting for input. */
506
507
	curs_set(1);

508
	kbinput = do_statusbar_input(&ran_func, &finished);
509

510
#ifndef NANO_TINY
511
	/* If the window size changed, go reformat the prompt string. */
512
513
	if (kbinput == KEY_WINCH) {
	    refresh_func();
514
	    *actual = KEY_WINCH;
515
#ifndef DISABLE_HISTORIES
516
	    free(magichistory);
517
#endif
518
	    return NULL;
519
	}
520
521
#endif /* !NANO_TINY */

522
	func = func_from_key(&kbinput);
523

524
	if (func == do_cancel || func == do_enter)
525
	    break;
526

527
#ifndef DISABLE_TABCOMP
528
	if (func != do_tab)
529
530
	    tabbed = FALSE;

531
	if (func == do_tab) {
532
#ifndef DISABLE_HISTORIES
533
	    if (history_list != NULL) {
534
		if (last_kbinput != the_code_for(do_tab, TAB_CODE))
535
		    complete_len = strlen(answer);
536

537
		if (complete_len > 0) {
538
539
		    answer = get_history_completion(history_list,
					answer, complete_len);
540
541
542
		    statusbar_x = strlen(answer);
		}
	    } else
543
#endif
544
545
	    if (allow_tabs)
		answer = input_tab(answer, allow_files, &statusbar_x,
546
					&tabbed, refresh_func, listed);
547
	} else
548
#endif /* !DISABLE_TABCOMP */
549
#ifndef DISABLE_HISTORIES
550
	if (func == get_history_older_void) {
551
552
553
	    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. */
554
		if ((*history_list)->next == NULL && *answer != '\0')
555
556
557
558
559
560
561
562
		    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);
		}
563

564
565
566
567
		/* 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. */
568
		finished = FALSE;
569
	    }
570
	} else if (func == get_history_newer_void) {
571
572
573
574
575
576
577
	    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);
		}
578

579
580
581
582
		/* 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 &&
583
584
585
586
			*answer == '\0' && magichistory != NULL) {
		    answer = mallocstrcpy(answer, magichistory);
		    statusbar_x = strlen(answer);
		}
587

588
589
590
591
592
593
		/* 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;
	    }
594
	} else
595
#endif /* !DISABLE_HISTORIES */
596
	if (func == do_help_void) {
597
598
599
600
601
602
	    /* 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;
603
604
	}

605
606
	/* If we have a shortcut with an associated function, break out if
	 * we're finished after running or trying to run the function. */
607
608
609
	if (finished)
	    break;

610
611
	update_the_statusbar();

612
#if !defined(DISABLE_HISTORIES) && !defined(DISABLE_TABCOMP)
613
614
	last_kbinput = kbinput;
#endif
615
    }
616

617
#ifndef DISABLE_HISTORIES
618
    /* Set the current position in the history list to the bottom. */
619
620
    if (history_list != NULL) {
	history_reset(*history_list);
621
	free(magichistory);
622
623
624
    }
#endif

625
    *actual = kbinput;
626
627

    return func;
628
629
}

630
631
632
633
/* 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.
634
635
636
 * 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.
637
638
 *
 * The allow_tabs parameter indicates whether we should allow tabs to be
639
 * interpreted.  The allow_files parameter indicates whether we should
640
 * allow all files (as opposed to just directories) to be tab completed. */
641
642
643
644
int do_prompt(bool allow_tabs,
#ifndef DISABLE_TABCOMP
	bool allow_files,
#endif
645
	int menu, const char *curranswer,
646
#ifndef DISABLE_HISTORIES
647
648
	filestruct **history_list,
#endif
649
	void (*refresh_func)(void), const char *msg, ...)
650
651
{
    va_list ap;
652
    int retval;
653
    functionptrtype func = NULL;
654
655
656
#ifndef DISABLE_TABCOMP
    bool listed = FALSE;
#endif
657
658
    /* Save a possible current statusbar x position. */
    size_t was_statusbar_x = statusbar_x;
659

660
    bottombars(menu);
661

662
663
    answer = mallocstrcpy(answer, curranswer);

664
665
666
667
668
669
670
671
672
673
674
#ifndef NANO_TINY
  redo_theprompt:
#endif
    prompt = charalloc((COLS * mb_cur_max()) + 1);
    va_start(ap, msg);
    vsnprintf(prompt, COLS * mb_cur_max(), msg, ap);
    va_end(ap);
    /* Reserve five columns for colon plus angles plus answer, ":<aa>". */
    null_at(&prompt, actual_x(prompt, (COLS < 5) ? 0 : COLS - 5));

    func = acquire_an_answer(&retval, allow_tabs,
675
#ifndef DISABLE_TABCOMP
676
			allow_files, &listed,
677
#endif
678
#ifndef DISABLE_HISTORIES
679
			history_list,
680
#endif
681
			refresh_func);
682

683
684
685
686
687
688
689
    free(prompt);
    prompt = NULL;

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

691
692
    /* If we're done with this prompt, restore the x position to what
     * it was at a possible previous prompt. */
693
    if (func == do_cancel || func == do_enter)
694
	statusbar_x = was_statusbar_x;
695

696
697
    /* If we left the prompt via Cancel or Enter, set the return value
     * properly. */
698
    if (func == do_cancel)
699
	retval = -1;
700
    else if (func == do_enter)
701
	retval = (*answer == '\0') ? -2 : 0;
702

703
704
705
706
707
708
709
    blank_statusbar();
    wnoutrefresh(bottomwin);

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

710
711
712
713
714
715
716
#ifndef DISABLE_TABCOMP
    /* 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

717
718
719
    return retval;
}

720
721
722
/* 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. */
723
int do_yesno_prompt(bool all, const char *msg)
724
{
725
    int response = -2, width = 16;
726
    /* TRANSLATORS: For the next three strings, if possible, specify
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
727
     * the single-byte shortcuts for both your language and English.
728
729
730
731
732
733
734
735
     * 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. */
736

737
738
739
740
741
742
743
    do {
	int kbinput;
	functionptrtype func;
#ifndef DISABLE_MOUSE
	int mouse_x, mouse_y;
#endif

744
745
	if (!ISSET(NO_HELP)) {
	    char shortstr[3];
746
		/* Temporary string for (translated) " Y", " N" and " A". */
747

748
749
	    if (COLS < 32)
		width = COLS / 2;
750

751
752
	    /* Clear the shortcut list from the bottom of the screen. */
	    blank_bottombars();
753

754
755
756
757
	    /* Now show the ones for "Yes", "No", "Cancel" and maybe "All". */
	    sprintf(shortstr, " %c", yesstr[0]);
	    wmove(bottomwin, 1, 0);
	    onekey(shortstr, _("Yes"), width);
758

759
760
	    if (all) {
		shortstr[1] = allstr[0];
761
		wmove(bottomwin, 1, width);
762
763
		onekey(shortstr, _("All"), width);
	    }
764

765
	    shortstr[1] = nostr[0];
766
	    wmove(bottomwin, 2, 0);
767
	    onekey(shortstr, _("No"), width);
768

769
	    wmove(bottomwin, 2, width);
770
771
	    onekey("^C", _("Cancel"), width);
	}
772

773
	/* Color the statusbar over its full width and display the question. */
774
	wattron(bottomwin, interface_color_pair[TITLE_BAR]);
775
776
	blank_statusbar();
	mvwaddnstr(bottomwin, 0, 0, msg, actual_x(msg, COLS - 1));
777
	wattroff(bottomwin, interface_color_pair[TITLE_BAR]);
778

779
	wnoutrefresh(bottomwin);
780

781
782
783
784
	/* When not replacing, show the cursor. */
	if (!all)
	    curs_set(1);

785
	currmenu = MYESNO;
786
	kbinput = get_kbinput(bottomwin);
787
788
789
790
791
792

#ifndef NANO_TINY
	if (kbinput == KEY_WINCH)
	    continue;
#endif

793
	func = func_from_key(&kbinput);
794

795
	if (func == do_cancel)
796
	    response = -1;
797
#ifndef DISABLE_MOUSE
798
	else if (kbinput == KEY_MOUSE) {
799
800
801
802
803
804
805
806
807
808
809
810
811
	    /* 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. */
812
		response = -2 * x * y + x - y + 1;
813

814
815
		if (response == 2 && !all)
		    response = -2;
816
	    }
817
	}
818
#endif /* !DISABLE_MOUSE */
819
	else {
820
821
	    /* Look for the kbinput in the Yes, No (and All) strings. */
	    if (strchr(yesstr, kbinput) != NULL)
822
		response = 1;
823
	    else if (strchr(nostr, kbinput) != NULL)
824
		response = 0;
825
	    else if (all && strchr(allstr, kbinput) != NULL)
826
		response = 2;
827
	}
828
    } while (response == -2);
829

830
    return response;
831
}