prompt.c 26.8 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 = (size_t)-1;
32
	/* The cursor position in answer. */
33
static size_t statusbar_pww = (size_t)-1;
34
	/* The place we want in answer. */
35

36
/* Read in a character, interpret it as a shortcut or toggle if
37
38
 * necessary, and return it.
 * Set ran_func to TRUE if we ran a function associated with a
39
 * shortcut key, and set finished to TRUE if we're done after running
40
41
 * or trying to run a function associated with a shortcut key.
 * refresh_func is the function we will call to refresh the edit window. */
42
43
int do_statusbar_input(bool *ran_func, bool *finished,
	void (*refresh_func)(void))
44
45
46
47
48
49
50
{
    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. */
51
    const sc *s;
52
    bool have_shortcut = FALSE;
53
    const subnfunc *f;
54
55
56
57
58

    *ran_func = FALSE;
    *finished = FALSE;

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

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

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

    /* Check for a shortcut in the current list. */
81
    s = get_shortcut(&input);
82
83
84

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

87
88
    /* 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. */
89
    if (!have_shortcut) {
90
	if (is_ascii_cntrl_char(input) || meta_key || func_key) {
91
	    beep();
92
93
	    meta_key = FALSE;
	    func_key = FALSE;
94
	    input = ERR;
95
96
97
	}
    }

98
99
100
101
102
103
104
105
    /* If we got a character, and it isn't a shortcut or toggle,
     * it's a normal text character.  Display the warning if we're
     * in view mode, or add the character to the input buffer if
     * we're not. */
    if (input != ERR && !have_shortcut) {
	/* If we're using restricted mode, the filename isn't blank,
	 * and we're at the "Write File" prompt, disable text input. */
	if (!ISSET(RESTRICTED) || openfile->filename[0] == '\0' ||
106
		currmenu != MWRITEFILE) {
107
108
109
	    kbinput_len++;
	    kbinput = (int *)nrealloc(kbinput, kbinput_len * sizeof(int));
	    kbinput[kbinput_len - 1] = input;
110
	}
111
112
113
114
115
116
117
118
119
     }

    /* If we got a shortcut, or if there aren't any other characters
     * waiting after the one we read in, we need to display all the
     * characters in the input buffer if it isn't empty. */
    if (have_shortcut || get_key_buffer_len() == 0) {
	if (kbinput != NULL) {
	    /* Display all the characters in the input buffer at
	     * once, filtering out control characters. */
120
	    do_statusbar_output(kbinput, kbinput_len, TRUE, NULL);
121
122
123
124
125

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

128
	if (have_shortcut) {
129
	    if (s->scfunc == do_tab || s->scfunc == do_enter)
130
		;
131
132
133
134
	    else if (s->scfunc == total_refresh) {
		total_redraw();
		refresh_func();
	    } else if (s->scfunc == do_cut_text_void) {
135
136
137
		/* If we're using restricted mode, the filename
		 * isn't blank, and we're at the "Write File"
		 * prompt, disable Cut. */
138
139
		if (!ISSET(RESTRICTED) || openfile->filename[0] == '\0' ||
			currmenu != MWRITEFILE)
140
		    do_statusbar_cut_text();
141
	    } else if (s->scfunc == do_left)
142
		do_statusbar_left();
143
144
	    else if (s->scfunc == do_right)
		do_statusbar_right();
145
#ifndef NANO_TINY
146
	    else if (s->scfunc == do_prev_word_void)
147
		do_statusbar_prev_word();
148
	    else if (s->scfunc == do_next_word_void)
149
		do_statusbar_next_word();
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
150
#endif
151
	    else if (s->scfunc == do_home)
152
		do_statusbar_home();
153
	    else if (s->scfunc == do_end)
154
		do_statusbar_end();
155
	    else if (s->scfunc == do_verbatim_input) {
156
157
158
159
160
		/* If we're using restricted mode, the filename
		 * isn't blank, and we're at the "Write File"
		 * prompt, disable verbatim input. */
		if (!ISSET(RESTRICTED) || currmenu != MWRITEFILE ||
			openfile->filename[0] == '\0') {
161
162
		    bool got_enter = FALSE;
			/* Whether we got the Enter key. */
163
164
165
166
167
168
169
170

		    do_statusbar_verbatim_input(&got_enter);

		    /* If we got the Enter key, remove it from the input
		     * buffer, set input to the key value for Enter, and
		     * set finished to TRUE to indicate that we're done. */
		    if (got_enter) {
			get_input(NULL, 1);
171
			input = sc_seq_or(do_enter, 0);
172
			*finished = TRUE;
173
		    }
174
		}
175
	    } else if (s->scfunc == do_delete) {
176
177
178
		/* If we're using restricted mode, the filename
		 * isn't blank, and we're at the "Write File"
		 * prompt, disable Delete. */
179
180
		if (!ISSET(RESTRICTED) || openfile->filename[0] == '\0' ||
			currmenu != MWRITEFILE)
181
		    do_statusbar_delete();
182
	    } else if (s->scfunc == do_backspace) {
183
184
185
		/* If we're using restricted mode, the filename
		 * isn't blank, and we're at the "Write File"
		 * prompt, disable Backspace. */
186
187
		if (!ISSET(RESTRICTED) || openfile->filename[0] == '\0' ||
			currmenu != MWRITEFILE)
188
189
		    do_statusbar_backspace();
	    } else {
190
		/* Handle any other shortcut in the current menu, setting
191
192
193
194
		 * ran_func to TRUE if we try to run their associated
		 * functions and setting finished to TRUE to indicate
		 * that we're done after running or trying to run their
		 * associated functions. */
195
		f = sctofunc(s);
196
		if (s->scfunc != NULL) {
197
		    *ran_func = TRUE;
198
199
		    if (f && (!ISSET(VIEW_MODE) || f->viewok) &&
				f->scfunc != do_gotolinecolumn_void)
200
			f->scfunc();
201
202
		}
		*finished = TRUE;
203
204
205
206
207
208
209
210
	    }
	}
    }

    return input;
}

#ifndef DISABLE_MOUSE
211
/* Handle a mouse click on the statusbar prompt or the shortcut list. */
212
int do_statusbar_mouse(void)
213
214
{
    int mouse_x, mouse_y;
215
    int retval = get_mouseinput(&mouse_x, &mouse_y, TRUE);
216

217
    /* We can click on the statusbar window text to move the cursor. */
218
    if (retval == 0 && wmouse_trafo(bottomwin, &mouse_y, &mouse_x, FALSE)) {
219
	size_t start_col;
220

221
	assert(prompt != NULL);
222

223
	start_col = strlenpt(prompt) + 2;
224

225
	/* Move to where the click occurred. */
226
	if (mouse_x >= start_col && mouse_y == 0) {
227
	    statusbar_x = actual_x(answer,
228
			get_statusbar_page_start(start_col, start_col +
229
			statusbar_xplustabs()) + mouse_x - start_col);
230
	    update_bar_if_needed();
231
232
233
234
235
236
237
	}
    }

    return retval;
}
#endif

238
239
240
241
242
/* The user typed input_len multibyte characters.  Add them to the
 * statusbar prompt, setting got_enter to TRUE if we get a newline,
 * and filtering out ASCII control characters if filtering is TRUE. */
void do_statusbar_output(int *the_input, size_t input_len,
	bool filtering, bool *got_enter)
243
{
244
    char *output = charalloc(input_len + 1);
245
    char *char_buf = charalloc(mb_cur_max());
246
    int i, char_len;
247
248
249

    assert(answer != NULL);

250
    /* Copy the typed stuff so it can be treated. */
251
    for (i = 0; i < input_len; i++)
252
253
254
255
	output[i] = (char)the_input[i];
    output[i] = '\0';

    i = 0;
256

257
258
259
    while (i < input_len) {
	/* When not filtering, convert nulls and stop at a newline. */
	if (!filtering) {
260
	    if (output[i] == '\0')
261
		output[i] = '\n';
262
	    else if (output[i] == '\n') {
263
264
265
		/* Put back the rest of the characters for reparsing,
		 * indicate that we got the Enter key and get out. */
		unparse_kbinput(output + i, input_len - i);
266
267
268
269
270
271
		*got_enter = TRUE;
		return;
	    }
	}

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

274
	i += char_len;
275

276
	/* When filtering, skip any ASCII control character. */
277
	if (filtering && is_ascii_cntrl_char(*(output + i - char_len)))
278
279
	    continue;

280
	assert(statusbar_x <= strlen(answer));
281

282
	/* Insert the typed character into the existing answer string. */
283
284
	answer = charealloc(answer, strlen(answer) + char_len + 1);
	charmove(answer + statusbar_x + char_len, answer + statusbar_x,
285
				strlen(answer) - statusbar_x + 1);
286
	strncpy(answer + statusbar_x, char_buf, char_len);
287

288
	statusbar_x += char_len;
289
290
291
    }

    free(char_buf);
292
    free(output);
293
294
295

    statusbar_pww = statusbar_xplustabs();

296
    update_the_statusbar();
297
298
}

299
/* Move to the beginning of the prompt text. */
300
301
void do_statusbar_home(void)
{
302
    statusbar_x = 0;
303
    update_bar_if_needed();
304
305
}

306
/* Move to the end of the prompt text. */
307
308
309
void do_statusbar_end(void)
{
    statusbar_x = strlen(answer);
310
    update_bar_if_needed();
311
312
}

313
314
/* Move left one character. */
void do_statusbar_left(void)
315
{
316
317
    if (statusbar_x > 0) {
	statusbar_x = move_mbleft(answer, statusbar_x);
318
	update_bar_if_needed();
319
    }
320
321
}

322
323
/* Move right one character. */
void do_statusbar_right(void)
324
{
325
326
    if (statusbar_x < strlen(answer)) {
	statusbar_x = move_mbright(answer, statusbar_x);
327
	update_bar_if_needed();
328
    }
329
330
}

331
/* Backspace over one character. */
332
333
334
void do_statusbar_backspace(void)
{
    if (statusbar_x > 0) {
335
	statusbar_x = move_mbleft(answer, statusbar_x);
336
337
338
339
	do_statusbar_delete();
    }
}

340
/* Delete one character. */
341
342
void do_statusbar_delete(void)
{
343
344
    statusbar_pww = statusbar_xplustabs();

345
    if (answer[statusbar_x] != '\0') {
346
	int char_len = parse_mbchar(answer + statusbar_x, NULL, NULL);
347
348
349

	assert(statusbar_x < strlen(answer));

350
351
	charmove(answer + statusbar_x, answer + statusbar_x + char_len,
			strlen(answer) - statusbar_x - char_len + 1);
352
	align(&answer);
353

354
	update_the_statusbar();
355
356
357
    }
}

358
/* Move text from the prompt into oblivion. */
359
360
361
362
void do_statusbar_cut_text(void)
{
    assert(answer != NULL);

363
#ifndef NANO_TINY
364
365
    if (ISSET(CUT_TO_END))
	null_at(&answer, statusbar_x);
366
    else
367
#endif
368
    {
369
370
	null_at(&answer, 0);
	statusbar_x = 0;
371
	statusbar_pww = statusbar_xplustabs();
372
    }
373

374
    update_the_statusbar();
375
376
}

377
#ifndef NANO_TINY
378
379
/* Move to the next word in the prompt text. */
void do_statusbar_next_word(void)
380
{
381
    bool seen_space = !is_word_mbchar(answer + statusbar_x, FALSE);
382
383
384

    assert(answer != NULL);

385
386
387
    /* Move forward until we reach the start of a word. */
    while (answer[statusbar_x] != '\0') {
	statusbar_x = move_mbright(answer, statusbar_x);
388

389
390
391
392
393
	/* 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)
394
395
396
	    break;
    }

397
    update_bar_if_needed();
398
399
}

400
401
/* Move to the previous word in the prompt text. */
void do_statusbar_prev_word(void)
402
{
403
    bool seen_a_word = FALSE, step_forward = FALSE;
404
405
406

    assert(answer != NULL);

407
408
    /* Move backward until we pass over the start of a word. */
    while (statusbar_x != 0) {
409
410
	statusbar_x = move_mbleft(answer, statusbar_x);

411
412
413
414
415
	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;
416
417
418
419
	    break;
	}
    }

420
421
422
    if (step_forward)
	/* Move one character forward again to sit on the start of the word. */
	statusbar_x = move_mbright(answer, statusbar_x);
423

424
    update_bar_if_needed();
425
}
426
#endif /* !NANO_TINY */
427

428
429
/* Get verbatim input.  Set got_enter to TRUE if we got the Enter key as
 * part of the verbatim input. */
430
431
432
void do_statusbar_verbatim_input(bool *got_enter)
{
    int *kbinput;
433
    size_t kbinput_len;
434
435
436
437
438

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

    /* Display all the verbatim characters at once, not filtering out
439
     * control characters. */
440
    do_statusbar_output(kbinput, kbinput_len, FALSE, got_enter);
441
442
}

443
/* Return the placewewant associated with statusbar_x, i.e. the
444
445
446
447
448
449
450
451
452
453
454
455
456
457
 * 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)
{
458
    if (column == start_col || column < COLS - 1 || COLS == start_col + 1)
459
460
461
462
463
464
	return 0;
    else
	return column - start_col - (column - start_col) % (COLS -
		start_col - 1);
}

465
466
467
468
469
470
471
/* Reinitialize the cursor position in the status bar prompt. */
void reinit_statusbar_x(void)
{
    statusbar_x = (size_t)-1;
    statusbar_pww = (size_t)-1;
}

472
473
474
/* Put the cursor in the statusbar prompt at statusbar_x. */
void reset_statusbar_cursor(void)
{
475
    size_t start_col = strlenpt(prompt) + 2;
476
477
    size_t xpt = statusbar_xplustabs();

478
    wmove(bottomwin, 0, start_col + xpt -
479
480
481
	get_statusbar_page_start(start_col, start_col + xpt));
}

482
483
/* Repaint the statusbar. */
void update_the_statusbar(void)
484
{
485
    size_t start_col, index, page_start;
486
487
    char *expanded;

488
    assert(prompt != NULL && statusbar_x <= strlen(answer));
489

490
    start_col = strlenpt(prompt) + 2;
491
    index = strnlenpt(answer, statusbar_x);
492
    page_start = get_statusbar_page_start(start_col, start_col + index);
493

494
495
496
    if (interface_color_pair[TITLE_BAR].bright)
	wattron(bottomwin, A_BOLD);
    wattron(bottomwin, interface_color_pair[TITLE_BAR].pairnum);
497
498
499
500
501
502
503

    blank_statusbar();

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

504
    expanded = display_string(answer, page_start, COLS - start_col - 1, FALSE);
505
506
507
    waddstr(bottomwin, expanded);
    free(expanded);

508
509
    wattroff(bottomwin, A_BOLD);
    wattroff(bottomwin, interface_color_pair[TITLE_BAR].pairnum);
510

511
    statusbar_pww = statusbar_xplustabs();
512
    reset_statusbar_cursor();
513
    wnoutrefresh(bottomwin);
514
515
}

516
/* Update the statusbar line /if/ the placewewant changes page. */
517
void update_bar_if_needed(void)
518
{
519
    size_t start_col = strlenpt(prompt) + 2;
520
521
522
523
    size_t was_pww = statusbar_pww;

    statusbar_pww = statusbar_xplustabs();

524
525
    if (get_statusbar_page_start(start_col, start_col + statusbar_pww) !=
		get_statusbar_page_start(start_col, start_col + was_pww))
526
	update_the_statusbar();
527
528
}

529
/* Get a string of input at the statusbar prompt. */
530
functionptrtype get_prompt_string(int *actual, bool allow_tabs,
531
#ifndef DISABLE_TABCOMP
532
	bool allow_files, bool *listed,
533
534
#endif
	const char *curranswer,
535
#ifndef DISABLE_HISTORIES
536
537
	filestruct **history_list,
#endif
538
	void (*refresh_func)(void))
539
{
540
    int kbinput = ERR;
541
    bool ran_func, finished;
542
    functionptrtype func;
543
544
545
546
#ifndef DISABLE_TABCOMP
    bool tabbed = FALSE;
	/* Whether we've pressed Tab. */
#endif
547
#ifndef DISABLE_HISTORIES
548
549
550
551
552
553
554
555
556
557
558
559
    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
560
#endif /* !DISABLE_HISTORIES */
561
562
563

    answer = mallocstrcpy(answer, curranswer);

564
565
    if (statusbar_x > strlen(answer)) {
	statusbar_x = strlen(answer);
566
567
	statusbar_pww = statusbar_xplustabs();
    }
568

569
#ifdef DEBUG
570
    fprintf(stderr, "get_prompt_string: answer = \"%s\", statusbar_x = %lu\n", answer, (unsigned long) statusbar_x);
571
#endif
572

573
    update_the_statusbar();
574

575
    /* Refresh edit window and statusbar before getting input. */
576
577
578
579
    wnoutrefresh(edit);
    wnoutrefresh(bottomwin);

    /* If we're using restricted mode, we aren't allowed to change the
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
580
581
582
583
     * name of the current file once it has one, because that would
     * allow writing to files not specified on the command line.  In
     * this case, disable all keys that would change the text if the
     * filename isn't blank and we're at the "Write File" prompt. */
584
    while (TRUE) {
585
586
587
	/* Ensure the cursor is on when waiting for input. */
	curs_set(1);

588
	kbinput = do_statusbar_input(&ran_func, &finished, refresh_func);
589
590
	assert(statusbar_x <= strlen(answer));

591
#ifndef NANO_TINY
592
593
	if (kbinput == KEY_WINCH) {
	    refresh_func();
594
	    update_the_statusbar();
595
596
	    continue;
	}
597
#endif
598
	func = func_from_key(&kbinput);
599

600
	if (func == do_cancel || func == do_enter)
601
	    break;
602

603
#ifndef DISABLE_TABCOMP
604
	if (func != do_tab)
605
606
	    tabbed = FALSE;

607
	if (func == do_tab) {
608
#ifndef DISABLE_HISTORIES
609
610
611
	    if (history_list != NULL) {
		if (last_kbinput != sc_seq_or(do_tab, NANO_CONTROL_I))
		    complete_len = strlen(answer);
612

613
		if (complete_len > 0) {
614
615
		    answer = get_history_completion(history_list,
					answer, complete_len);
616
617
618
		    statusbar_x = strlen(answer);
		}
	    } else
619
#endif
620
621
	    if (allow_tabs)
		answer = input_tab(answer, allow_files, &statusbar_x,
622
					&tabbed, refresh_func, listed);
623

624
	    update_the_statusbar();
625
	} else
626
#endif /* !DISABLE_TABCOMP */
627
#ifndef DISABLE_HISTORIES
628
	if (func == get_history_older_void) {
629
630
631
	    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. */
632
		if ((*history_list)->next == NULL && *answer != '\0')
633
634
635
636
637
638
639
640
		    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);
		}
641

642
		update_the_statusbar();
643

644
645
646
647
		/* 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. */
648
		finished = FALSE;
649
	    }
650
	} else if (func == get_history_newer_void) {
651
652
653
654
655
656
657
	    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);
		}
658

659
660
661
662
		/* 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 &&
663
664
665
666
			*answer == '\0' && magichistory != NULL) {
		    answer = mallocstrcpy(answer, magichistory);
		    statusbar_x = strlen(answer);
		}
667

668
		update_the_statusbar();
669

670
671
672
673
674
675
		/* 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;
	    }
676
	} else
677
#endif /* !DISABLE_HISTORIES */
678
	if (func == do_help_void) {
679
	    update_the_statusbar();
680

681
682
683
684
685
686
	    /* 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;
687
688
	}

689
690
	/* If we have a shortcut with an associated function, break out if
	 * we're finished after running or trying to run the function. */
691
692
693
	if (finished)
	    break;

694
#if !defined(DISABLE_HISTORIES) && !defined(DISABLE_TABCOMP)
695
696
	last_kbinput = kbinput;
#endif
697
	reset_statusbar_cursor();
698
	wnoutrefresh(bottomwin);
699
    }
700

701
#ifndef DISABLE_HISTORIES
702
    /* Set the current position in the history list to the bottom. */
703
704
    if (history_list != NULL) {
	history_reset(*history_list);
705
	free(magichistory);
706
707
708
    }
#endif

709
    *actual = kbinput;
710
711

    return func;
712
713
}

714
715
716
717
/* 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.
718
719
720
 * 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.
721
722
 *
 * The allow_tabs parameter indicates whether we should allow tabs to be
723
 * interpreted.  The allow_files parameter indicates whether we should
724
 * allow all files (as opposed to just directories) to be tab completed. */
725
726
727
728
int do_prompt(bool allow_tabs,
#ifndef DISABLE_TABCOMP
	bool allow_files,
#endif
729
	int menu, const char *curranswer,
730
#ifndef DISABLE_HISTORIES
731
732
	filestruct **history_list,
#endif
733
	void (*refresh_func)(void), const char *msg, ...)
734
735
736
{
    va_list ap;
    int retval;
737
    functionptrtype func;
738
739
740
#ifndef DISABLE_TABCOMP
    bool listed = FALSE;
#endif
741
742
743
    /* Save a possible current statusbar x position. */
    size_t was_statusbar_x = statusbar_x;
    size_t was_pww = statusbar_pww;
744

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
745
    prompt = charalloc(((COLS - 4) * mb_cur_max()) + 1);
746

747
    bottombars(menu);
748
749
750
751
752
753

    va_start(ap, msg);
    vsnprintf(prompt, (COLS - 4) * mb_cur_max(), msg, ap);
    va_end(ap);
    null_at(&prompt, actual_x(prompt, COLS - 4));

754
    func = get_prompt_string(&retval, allow_tabs,
755
#ifndef DISABLE_TABCOMP
756
			allow_files, &listed,
757
758
#endif
	curranswer,
759
#ifndef DISABLE_HISTORIES
760
	history_list,
761
#endif
762
	refresh_func);
763

764
765
766
    free(prompt);
    prompt = NULL;

767
768
769
770
771
772
    /* 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;
    }
773

774
775
    /* If we left the prompt via Cancel or Enter, set the return value
     * properly. */
776
    if (func == do_cancel)
777
	retval = -1;
778
    else if (func == do_enter)
779
	retval = (*answer == '\0') ? -2 : 0;
780

781
782
783
784
785
786
787
    blank_statusbar();
    wnoutrefresh(bottomwin);

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

788
789
790
791
792
793
794
#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

795
796
797
    return retval;
}

798
799
800
/* 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. */
801
int do_yesno_prompt(bool all, const char *msg)
802
{
803
    int response = -2, width = 16;
804
805
806
    const char *yesstr;		/* String of Yes characters accepted. */
    const char *nostr;		/* Same for No. */
    const char *allstr;		/* And All, surprise! */
807
    int oldmenu = currmenu;
808
809
810
811
812
813

    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
814
815
     * displayed in the shortcuts. */
    /* TRANSLATORS: For the next three strings, if possible, specify
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
816
817
     * the single-byte shortcuts for both your language and English.
     * For example, in French: "OoYy" for "Oui". */
818
819
820
821
    yesstr = _("Yy");
    nostr = _("Nn");
    allstr = _("Aa");

822
823
824
825
826
827
828
    do {
	int kbinput;
	functionptrtype func;
#ifndef DISABLE_MOUSE
	int mouse_x, mouse_y;
#endif

829
830
	if (!ISSET(NO_HELP)) {
	    char shortstr[3];
831
		/* Temporary string for (translated) " Y", " N" and " A". */
832

833
834
	    if (COLS < 32)
		width = COLS / 2;
835

836
837
	    /* Clear the shortcut list from the bottom of the screen. */
	    blank_bottombars();
838

839
840
841
842
	    /* Now show the ones for "Yes", "No", "Cancel" and maybe "All". */
	    sprintf(shortstr, " %c", yesstr[0]);
	    wmove(bottomwin, 1, 0);
	    onekey(shortstr, _("Yes"), width);
843

844
845
	    if (all) {
		shortstr[1] = allstr[0];
846
		wmove(bottomwin, 1, width);
847
848
		onekey(shortstr, _("All"), width);
	    }
849

850
	    shortstr[1] = nostr[0];
851
	    wmove(bottomwin, 2, 0);
852
	    onekey(shortstr, _("No"), width);
853

854
	    wmove(bottomwin, 2, width);
855
856
	    onekey("^C", _("Cancel"), width);
	}
857

858
859
860
	if (interface_color_pair[TITLE_BAR].bright)
	    wattron(bottomwin, A_BOLD);
	wattron(bottomwin, interface_color_pair[TITLE_BAR].pairnum);
861

862
863
	blank_statusbar();
	mvwaddnstr(bottomwin, 0, 0, msg, actual_x(msg, COLS - 1));
864

865
866
	wattroff(bottomwin, A_BOLD);
	wattroff(bottomwin, interface_color_pair[TITLE_BAR].pairnum);
867

868
869
870
	/* Refresh edit window and statusbar before getting input. */
	wnoutrefresh(edit);
	wnoutrefresh(bottomwin);
871

872
	currmenu = MYESNO;
873
	kbinput = get_kbinput(bottomwin);
874
875
876
877
878
879

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

880
	func = func_from_key(&kbinput);
881

882
	if (func == do_cancel)
883
	    response = -1;
884
#ifndef DISABLE_MOUSE
885
	else if (kbinput == KEY_MOUSE) {
886
887
888
889
890
891
892
893
894
895
896
897
898
	    /* 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. */
899
		response = -2 * x * y + x - y + 1;
900

901
902
		if (response == 2 && !all)
		    response = -2;
903
	    }
904
	}
905
#endif /* !DISABLE_MOUSE */
906
	else if (func == total_refresh) {
907
908
909
	    total_redraw();
	    continue;
	} else {
910
911
	    /* Look for the kbinput in the Yes, No (and All) strings. */
	    if (strchr(yesstr, kbinput) != NULL)
912
		response = 1;
913
	    else if (strchr(nostr, kbinput) != NULL)
914
		response = 0;
915
	    else if (all && strchr(allstr, kbinput) != NULL)
916
		response = 2;
917
	}
918
    } while (response == -2);
919

920
    currmenu = oldmenu;
921
    return response;
922
}