prompt.c 26 KB
Newer Older
1
2
3
/**************************************************************************
 *   prompt.c                                                             *
 *                                                                        *
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
 *   This program is free software; you can redistribute it and/or modify *
 *   it under the terms of the GNU General Public License as published by *
8
 *   the Free Software Foundation; either version 3, or (at your option)  *
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *   any later version.                                                   *
 *                                                                        *
 *   This program 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.                             *
 *                                                                        *
 *   You should have received a copy of the GNU General Public License    *
 *   along with this program; if not, write to the Free Software          *
 *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA            *
 *   02110-1301, USA.                                                     *
 *                                                                        *
 **************************************************************************/

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
static size_t statusbar_pww = HIGHEST_POSITIVE;
34
	/* The place we want in answer. */
35

36
37
38
/* 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
39
40
 * or trying to run a function associated with a shortcut key.
 * refresh_func is the function we will call to refresh the edit window. */
41
42
int do_statusbar_input(bool *ran_func, bool *finished,
	void (*refresh_func)(void))
43
44
45
46
47
48
49
{
    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. */
50
    const sc *s;
51
    bool have_shortcut = FALSE;
52
    const subnfunc *f;
53
54
55
56
57

    *ran_func = FALSE;
    *finished = FALSE;

    /* Read in a character. */
58
    input = get_kbinput(bottomwin);
59

60
61
62
63
64
#ifndef NANO_TINY
    if (input == KEY_WINCH)
	return KEY_WINCH;
#endif

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

    /* Check for a shortcut in the current list. */
77
    s = get_shortcut(&input);
78
79
80

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

83
84
    /* 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. */
85
    if (!have_shortcut) {
86
	if (is_ascii_cntrl_char(input) || meta_key || !is_byte(input)) {
87
	    beep();
88
	    input = ERR;
89
90
91
	}
    }

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

105
106
107
    /* 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. */
108
    if ((have_shortcut || get_key_buffer_len() == 0) && kbinput != NULL) {
109
110
111
112
113
114
115
116
	/* 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;
117
    }
118

119
    if (have_shortcut) {
120
121
122
123
124
125
126
127
128
	if (s->scfunc == do_tab || s->scfunc == do_enter)
	    ;
	else if (s->scfunc == total_refresh) {
	    total_redraw();
	    refresh_func();
	} else if (s->scfunc == do_left)
	    do_statusbar_left();
	else if (s->scfunc == do_right)
	    do_statusbar_right();
129
#ifndef NANO_TINY
130
131
132
133
	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
134
#endif
135
136
137
138
139
140
141
	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 &&
142
143
144
145
146
				openfile->filename[0] != '\0' &&
				(s->scfunc == do_verbatim_input ||
				s->scfunc == do_cut_text_void ||
				s->scfunc == do_delete ||
				s->scfunc == do_backspace))
147
148
149
150
151
152
153
154
155
156
157
158
	    ;
	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);
		input = sc_seq_or(do_enter, 0);
159
		*finished = TRUE;
160
	    }
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
	} 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;
	}
181
182
183
184
185
186
    }

    return input;
}

#ifndef DISABLE_MOUSE
187
/* Handle a mouse click on the statusbar prompt or the shortcut list. */
188
int do_statusbar_mouse(void)
189
190
{
    int mouse_x, mouse_y;
191
    int retval = get_mouseinput(&mouse_x, &mouse_y, TRUE);
192

193
    /* We can click on the statusbar window text to move the cursor. */
194
    if (retval == 0 && wmouse_trafo(bottomwin, &mouse_y, &mouse_x, FALSE)) {
195
	size_t start_col;
196

197
	assert(prompt != NULL);
198

199
	start_col = strlenpt(prompt) + 2;
200

201
	/* Move to where the click occurred. */
202
	if (mouse_x >= start_col && mouse_y == 0) {
203
	    statusbar_x = actual_x(answer,
204
			get_statusbar_page_start(start_col, start_col +
205
			statusbar_xplustabs()) + mouse_x - start_col);
206
	    update_bar_if_needed();
207
208
209
210
211
212
213
	}
    }

    return retval;
}
#endif

214
/* The user typed input_len multibyte characters.  Add them to the
215
 * statusbar prompt, setting got_newline to TRUE if we got a verbatim ^J,
216
217
 * and filtering out ASCII control characters if filtering is TRUE. */
void do_statusbar_output(int *the_input, size_t input_len,
218
	bool filtering, bool *got_newline)
219
{
220
    char *output = charalloc(input_len + 1);
221
    char *char_buf = charalloc(mb_cur_max());
222
    int i, char_len;
223
224
225

    assert(answer != NULL);

226
    /* Copy the typed stuff so it can be treated. */
227
    for (i = 0; i < input_len; i++)
228
229
230
231
	output[i] = (char)the_input[i];
    output[i] = '\0';

    i = 0;
232

233
234
235
    while (i < input_len) {
	/* When not filtering, convert nulls and stop at a newline. */
	if (!filtering) {
236
	    if (output[i] == '\0')
237
		output[i] = '\n';
238
	    else if (output[i] == '\n') {
239
		/* Put back the rest of the characters for reparsing,
240
		 * indicate that we got a ^J and get out. */
241
		unparse_kbinput(output + i, input_len - i);
242
		*got_newline = TRUE;
243
244
245
246
247
		return;
	    }
	}

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

250
	i += char_len;
251

252
	/* When filtering, skip any ASCII control character. */
253
	if (filtering && is_ascii_cntrl_char(*(output + i - char_len)))
254
255
	    continue;

256
	assert(statusbar_x <= strlen(answer));
257

258
	/* Insert the typed character into the existing answer string. */
259
260
	answer = charealloc(answer, strlen(answer) + char_len + 1);
	charmove(answer + statusbar_x + char_len, answer + statusbar_x,
261
				strlen(answer) - statusbar_x + 1);
262
	strncpy(answer + statusbar_x, char_buf, char_len);
263

264
	statusbar_x += char_len;
265
266
267
    }

    free(char_buf);
268
    free(output);
269
270
271

    statusbar_pww = statusbar_xplustabs();

272
    update_the_statusbar();
273
274
}

275
/* Move to the beginning of the prompt text. */
276
277
void do_statusbar_home(void)
{
278
    statusbar_x = 0;
279
    update_bar_if_needed();
280
281
}

282
/* Move to the end of the prompt text. */
283
284
285
void do_statusbar_end(void)
{
    statusbar_x = strlen(answer);
286
    update_bar_if_needed();
287
288
}

289
290
/* Move left one character. */
void do_statusbar_left(void)
291
{
292
293
    if (statusbar_x > 0) {
	statusbar_x = move_mbleft(answer, statusbar_x);
294
	update_bar_if_needed();
295
    }
296
297
}

298
299
/* Move right one character. */
void do_statusbar_right(void)
300
{
301
302
    if (statusbar_x < strlen(answer)) {
	statusbar_x = move_mbright(answer, statusbar_x);
303
	update_bar_if_needed();
304
    }
305
306
}

307
/* Backspace over one character. */
308
309
310
void do_statusbar_backspace(void)
{
    if (statusbar_x > 0) {
311
	statusbar_x = move_mbleft(answer, statusbar_x);
312
313
314
315
	do_statusbar_delete();
    }
}

316
/* Delete one character. */
317
318
void do_statusbar_delete(void)
{
319
320
    statusbar_pww = statusbar_xplustabs();

321
    if (answer[statusbar_x] != '\0') {
322
	int char_len = parse_mbchar(answer + statusbar_x, NULL, NULL);
323
324
325

	assert(statusbar_x < strlen(answer));

326
327
	charmove(answer + statusbar_x, answer + statusbar_x + char_len,
			strlen(answer) - statusbar_x - char_len + 1);
328
	align(&answer);
329

330
	update_the_statusbar();
331
332
333
    }
}

334
/* Move text from the prompt into oblivion. */
335
336
337
338
void do_statusbar_cut_text(void)
{
    assert(answer != NULL);

339
#ifndef NANO_TINY
340
341
    if (ISSET(CUT_TO_END))
	null_at(&answer, statusbar_x);
342
    else
343
#endif
344
    {
345
346
	null_at(&answer, 0);
	statusbar_x = 0;
347
	statusbar_pww = statusbar_xplustabs();
348
    }
349

350
    update_the_statusbar();
351
352
}

353
#ifndef NANO_TINY
354
355
/* Move to the next word in the prompt text. */
void do_statusbar_next_word(void)
356
{
357
    bool seen_space = !is_word_mbchar(answer + statusbar_x, FALSE);
358
359
360

    assert(answer != NULL);

361
362
363
    /* Move forward until we reach the start of a word. */
    while (answer[statusbar_x] != '\0') {
	statusbar_x = move_mbright(answer, statusbar_x);
364

365
366
367
368
369
	/* 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)
370
371
372
	    break;
    }

373
    update_bar_if_needed();
374
375
}

376
377
/* Move to the previous word in the prompt text. */
void do_statusbar_prev_word(void)
378
{
379
    bool seen_a_word = FALSE, step_forward = FALSE;
380
381
382

    assert(answer != NULL);

383
384
    /* Move backward until we pass over the start of a word. */
    while (statusbar_x != 0) {
385
386
	statusbar_x = move_mbleft(answer, statusbar_x);

387
388
389
390
391
	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;
392
393
394
395
	    break;
	}
    }

396
397
398
    if (step_forward)
	/* Move one character forward again to sit on the start of the word. */
	statusbar_x = move_mbright(answer, statusbar_x);
399

400
    update_bar_if_needed();
401
}
402
#endif /* !NANO_TINY */
403

404
/* Get verbatim input, setting got_newline to TRUE if we get a ^J as
405
 * part of the verbatim input. */
406
void do_statusbar_verbatim_input(bool *got_newline)
407
408
{
    int *kbinput;
409
    size_t kbinput_len;
410
411
412
413
414

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

    /* Display all the verbatim characters at once, not filtering out
415
     * control characters. */
416
    do_statusbar_output(kbinput, kbinput_len, FALSE, got_newline);
417
418
}

419
/* Return the placewewant associated with statusbar_x, i.e. the
420
421
422
423
424
425
426
427
428
429
430
431
432
433
 * zero-based column position of the cursor.  The value will be no
 * smaller than statusbar_x. */
size_t statusbar_xplustabs(void)
{
    return strnlenpt(answer, statusbar_x);
}

/* nano scrolls horizontally within a line in chunks.  This function
 * returns the column number of the first character displayed in the
 * statusbar prompt when the cursor is at the given column with the
 * prompt ending at start_col.  Note that (0 <= column -
 * get_statusbar_page_start(column) < COLS). */
size_t get_statusbar_page_start(size_t start_col, size_t column)
{
434
    if (column == start_col || column < COLS - 1)
435
	return 0;
436
437
    else if (COLS > start_col + 2)
	return column - start_col - 1 - (column - start_col - 1) % (COLS - start_col - 2);
438
    else
439
	return column - 2;
440
441
}

442
443
444
/* Reinitialize the cursor position in the status bar prompt. */
void reinit_statusbar_x(void)
{
445
446
    statusbar_x = HIGHEST_POSITIVE;
    statusbar_pww = HIGHEST_POSITIVE;
447
448
}

449
450
451
/* Put the cursor in the statusbar prompt at statusbar_x. */
void reset_statusbar_cursor(void)
{
452
    size_t start_col = strlenpt(prompt) + 2;
453
454
    size_t xpt = statusbar_xplustabs();

455
456
457
458
459
    /* Work around a cursor-misplacement bug in VTEs. */
    wmove(bottomwin, 0, 0);
    wnoutrefresh(bottomwin);
    doupdate();

460
    wmove(bottomwin, 0, start_col + xpt -
461
	get_statusbar_page_start(start_col, start_col + xpt));
462
463

    wnoutrefresh(bottomwin);
464
465
}

466
467
/* Repaint the statusbar. */
void update_the_statusbar(void)
468
{
469
    size_t start_col, index, page_start, page_end;
470
471
    char *expanded;

472
    assert(prompt != NULL && statusbar_x <= strlen(answer));
473

474
    start_col = strlenpt(prompt) + 2;
475
    index = strnlenpt(answer, statusbar_x);
476
    page_start = get_statusbar_page_start(start_col, start_col + index);
477
    page_end = get_statusbar_page_start(start_col, start_col + strlenpt(answer) - 1);
478

479
    wattron(bottomwin, interface_color_pair[TITLE_BAR]);
480
481
482
483
484
485
486

    blank_statusbar();

    mvwaddnstr(bottomwin, 0, 0, prompt, actual_x(prompt, COLS - 2));
    waddch(bottomwin, ':');
    waddch(bottomwin, (page_start == 0) ? ' ' : '$');

487
    expanded = display_string(answer, page_start, COLS - start_col - 1, FALSE);
488
489
490
    waddstr(bottomwin, expanded);
    free(expanded);

491
492
    waddch(bottomwin, (page_start >= page_end) ? ' ' : '$');

493
    wattroff(bottomwin, interface_color_pair[TITLE_BAR]);
494

495
    statusbar_pww = statusbar_xplustabs();
496
    reset_statusbar_cursor();
497
498
}

499
/* Update the statusbar line /if/ the placewewant changes page. */
500
void update_bar_if_needed(void)
501
{
502
    size_t start_col = strlenpt(prompt) + 2;
503
504
505
506
    size_t was_pww = statusbar_pww;

    statusbar_pww = statusbar_xplustabs();

507
508
    if (get_statusbar_page_start(start_col, start_col + statusbar_pww) !=
		get_statusbar_page_start(start_col, start_col + was_pww))
509
	update_the_statusbar();
510
511
}

512
/* Get a string of input at the statusbar prompt. */
513
functionptrtype acquire_an_answer(int *actual, bool allow_tabs,
514
#ifndef DISABLE_TABCOMP
515
	bool allow_files, bool *listed,
516
517
#endif
	const char *curranswer,
518
#ifndef DISABLE_HISTORIES
519
520
	filestruct **history_list,
#endif
521
	void (*refresh_func)(void))
522
{
523
    int kbinput = ERR;
524
    bool ran_func, finished;
525
    functionptrtype func;
526
527
528
529
#ifndef DISABLE_TABCOMP
    bool tabbed = FALSE;
	/* Whether we've pressed Tab. */
#endif
530
#ifndef DISABLE_HISTORIES
531
532
533
534
535
536
537
538
539
540
541
542
    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
543
#endif /* !DISABLE_HISTORIES */
544
545
546

    answer = mallocstrcpy(answer, curranswer);

547
548
    if (statusbar_x > strlen(answer)) {
	statusbar_x = strlen(answer);
549
550
	statusbar_pww = statusbar_xplustabs();
    }
551

552
#ifdef DEBUG
553
    fprintf(stderr, "acquiring: answer = \"%s\", statusbar_x = %lu\n", answer, (unsigned long) statusbar_x);
554
#endif
555

556
    update_the_statusbar();
557

558
    /* Refresh edit window and statusbar before getting input. */
559
560
561
    wnoutrefresh(edit);
    wnoutrefresh(bottomwin);

562
    while (TRUE) {
563
	/* Ensure the cursor is shown when waiting for input. */
564
565
	curs_set(1);

566
	kbinput = do_statusbar_input(&ran_func, &finished, refresh_func);
567
568
	assert(statusbar_x <= strlen(answer));

569
#ifndef NANO_TINY
570
	/* If the window size changed, go reformat the prompt string. */
571
572
	if (kbinput == KEY_WINCH) {
	    refresh_func();
573
574
575
	    *actual = KEY_WINCH;
	    free(magichistory);
	    return NULL;
576
	}
577
#endif
578
	func = func_from_key(&kbinput);
579

580
	if (func == do_cancel || func == do_enter)
581
	    break;
582

583
#ifndef DISABLE_TABCOMP
584
	if (func != do_tab)
585
586
	    tabbed = FALSE;

587
	if (func == do_tab) {
588
#ifndef DISABLE_HISTORIES
589
	    if (history_list != NULL) {
590
		if (last_kbinput != sc_seq_or(do_tab, TAB_CODE))
591
		    complete_len = strlen(answer);
592

593
		if (complete_len > 0) {
594
595
		    answer = get_history_completion(history_list,
					answer, complete_len);
596
597
598
		    statusbar_x = strlen(answer);
		}
	    } else
599
#endif
600
601
	    if (allow_tabs)
		answer = input_tab(answer, allow_files, &statusbar_x,
602
					&tabbed, refresh_func, listed);
603

604
	    update_the_statusbar();
605
	} else
606
#endif /* !DISABLE_TABCOMP */
607
#ifndef DISABLE_HISTORIES
608
	if (func == get_history_older_void) {
609
610
611
	    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. */
612
		if ((*history_list)->next == NULL && *answer != '\0')
613
614
615
616
617
618
619
620
		    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);
		}
621

622
		update_the_statusbar();
623

624
625
626
627
		/* 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. */
628
		finished = FALSE;
629
	    }
630
	} else if (func == get_history_newer_void) {
631
632
633
634
635
636
637
	    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);
		}
638

639
640
641
642
		/* 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 &&
643
644
645
646
			*answer == '\0' && magichistory != NULL) {
		    answer = mallocstrcpy(answer, magichistory);
		    statusbar_x = strlen(answer);
		}
647

648
		update_the_statusbar();
649

650
651
652
653
654
655
		/* 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;
	    }
656
	} else
657
#endif /* !DISABLE_HISTORIES */
658
	if (func == do_help_void) {
659
	    update_the_statusbar();
660

661
662
663
664
665
666
	    /* 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;
667
668
	}

669
670
	/* If we have a shortcut with an associated function, break out if
	 * we're finished after running or trying to run the function. */
671
672
673
	if (finished)
	    break;

674
#if !defined(DISABLE_HISTORIES) && !defined(DISABLE_TABCOMP)
675
676
	last_kbinput = kbinput;
#endif
677
	reset_statusbar_cursor();
678
    }
679

680
#ifndef DISABLE_HISTORIES
681
    /* Set the current position in the history list to the bottom. */
682
683
    if (history_list != NULL) {
	history_reset(*history_list);
684
	free(magichistory);
685
686
687
    }
#endif

688
    *actual = kbinput;
689
690

    return func;
691
692
}

693
694
695
696
/* 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.
697
698
699
 * 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.
700
701
 *
 * The allow_tabs parameter indicates whether we should allow tabs to be
702
 * interpreted.  The allow_files parameter indicates whether we should
703
 * allow all files (as opposed to just directories) to be tab completed. */
704
705
706
707
int do_prompt(bool allow_tabs,
#ifndef DISABLE_TABCOMP
	bool allow_files,
#endif
708
	int menu, const char *curranswer,
709
#ifndef DISABLE_HISTORIES
710
711
	filestruct **history_list,
#endif
712
	void (*refresh_func)(void), const char *msg, ...)
713
714
{
    va_list ap;
715
    int retval = KEY_WINCH;
716
    functionptrtype func = NULL;
717
718
719
#ifndef DISABLE_TABCOMP
    bool listed = FALSE;
#endif
720
721
722
    /* Save a possible current statusbar x position. */
    size_t was_statusbar_x = statusbar_x;
    size_t was_pww = statusbar_pww;
723

724
    bottombars(menu);
725

726
    while (retval == KEY_WINCH) {
727
728
729
730
	prompt = charalloc((COLS * mb_cur_max()) + 1);
	va_start(ap, msg);
	vsnprintf(prompt, COLS * mb_cur_max(), msg, ap);
	va_end(ap);
731
732
	/* Reserve five columns for colon plus dollars plus answer, ":$aa$". */
	null_at(&prompt, actual_x(prompt, (COLS < 5) ? 0 : COLS - 5));
733

734
	func = acquire_an_answer(&retval, allow_tabs,
735
#ifndef DISABLE_TABCOMP
736
			allow_files, &listed,
737
#endif
738
			curranswer,
739
#ifndef DISABLE_HISTORIES
740
			history_list,
741
#endif
742
			refresh_func);
743

744
745
	free(prompt);
	prompt = NULL;
746
    }
747

748
749
750
751
752
753
    /* If we're done with this prompt, restore the x position to what
     * it was at a possible previous prompt. */
    if (func == do_cancel || func == do_enter) {
	statusbar_x = was_statusbar_x;
	statusbar_pww = was_pww;
    }
754

755
756
    /* If we left the prompt via Cancel or Enter, set the return value
     * properly. */
757
    if (func == do_cancel)
758
	retval = -1;
759
    else if (func == do_enter)
760
	retval = (*answer == '\0') ? -2 : 0;
761

762
763
764
765
766
767
768
    blank_statusbar();
    wnoutrefresh(bottomwin);

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

769
770
771
772
773
774
775
#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

776
777
778
    return retval;
}

779
780
781
/* 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. */
782
int do_yesno_prompt(bool all, const char *msg)
783
{
784
    int response = -2, width = 16;
785
786
787
    const char *yesstr;		/* String of Yes characters accepted. */
    const char *nostr;		/* Same for No. */
    const char *allstr;		/* And All, surprise! */
788
    int oldmenu = currmenu;
789
790
791
792
793
794

    assert(msg != NULL);

    /* yesstr, nostr, and allstr are strings of any length.  Each string
     * consists of all single-byte characters accepted as valid
     * characters for that value.  The first value will be the one
795
796
     * displayed in the shortcuts. */
    /* TRANSLATORS: For the next three strings, if possible, specify
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
797
798
     * the single-byte shortcuts for both your language and English.
     * For example, in French: "OoYy" for "Oui". */
799
800
801
802
    yesstr = _("Yy");
    nostr = _("Nn");
    allstr = _("Aa");

803
804
805
806
807
808
809
    do {
	int kbinput;
	functionptrtype func;
#ifndef DISABLE_MOUSE
	int mouse_x, mouse_y;
#endif

810
811
	if (!ISSET(NO_HELP)) {
	    char shortstr[3];
812
		/* Temporary string for (translated) " Y", " N" and " A". */
813

814
815
	    if (COLS < 32)
		width = COLS / 2;
816

817
818
	    /* Clear the shortcut list from the bottom of the screen. */
	    blank_bottombars();
819

820
821
822
823
	    /* Now show the ones for "Yes", "No", "Cancel" and maybe "All". */
	    sprintf(shortstr, " %c", yesstr[0]);
	    wmove(bottomwin, 1, 0);
	    onekey(shortstr, _("Yes"), width);
824

825
826
	    if (all) {
		shortstr[1] = allstr[0];
827
		wmove(bottomwin, 1, width);
828
829
		onekey(shortstr, _("All"), width);
	    }
830

831
	    shortstr[1] = nostr[0];
832
	    wmove(bottomwin, 2, 0);
833
	    onekey(shortstr, _("No"), width);
834

835
	    wmove(bottomwin, 2, width);
836
837
	    onekey("^C", _("Cancel"), width);
	}
838

839
	wattron(bottomwin, interface_color_pair[TITLE_BAR]);
840

841
842
	blank_statusbar();
	mvwaddnstr(bottomwin, 0, 0, msg, actual_x(msg, COLS - 1));
843

844
	wattroff(bottomwin, interface_color_pair[TITLE_BAR]);
845

846
847
848
	/* Refresh edit window and statusbar before getting input. */
	wnoutrefresh(edit);
	wnoutrefresh(bottomwin);
849

850
	currmenu = MYESNO;
851
	kbinput = get_kbinput(bottomwin);
852
853
854
855
856
857

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

858
	func = func_from_key(&kbinput);
859

860
	if (func == do_cancel)
861
	    response = -1;
862
#ifndef DISABLE_MOUSE
863
	else if (kbinput == KEY_MOUSE) {
864
865
866
867
868
869
870
871
872
873
874
875
876
	    /* 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. */
877
		response = -2 * x * y + x - y + 1;
878

879
880
		if (response == 2 && !all)
		    response = -2;
881
	    }
882
	}
883
#endif /* !DISABLE_MOUSE */
884
	else if (func == total_refresh) {
885
886
887
	    total_redraw();
	    continue;
	} else {
888
889
	    /* Look for the kbinput in the Yes, No (and All) strings. */
	    if (strchr(yesstr, kbinput) != NULL)
890
		response = 1;
891
	    else if (strchr(nostr, kbinput) != NULL)
892
		response = 0;
893
	    else if (all && strchr(allstr, kbinput) != NULL)
894
		response = 2;
895
	}
896
    } while (response == -2);
897

898
899
900
    /* Restore the previously active menu. */
    bottombars(oldmenu);

901
    return response;
902
}