prompt.c 27.5 KB
Newer Older
1
2
3
4
/* $Id$ */
/**************************************************************************
 *   prompt.c                                                             *
 *                                                                        *
5
 *   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,  *
6
 *   2008, 2009, 2010, 2011, 2013, 2014 Free Software Foundation, Inc.    *
7
8
 *   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 *
9
 *   the Free Software Foundation; either version 3, or (at your option)  *
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *   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.                                                     *
 *                                                                        *
 **************************************************************************/

24
#include "proto.h"
25

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

static char *prompt = NULL;
31
	/* The prompt string used for statusbar questions. */
32
static size_t statusbar_x = (size_t)-1;
33
	/* The cursor position in answer. */
34
static size_t statusbar_pww = (size_t)-1;
35
	/* The place we want in answer. */
36
37
static size_t old_statusbar_x = (size_t)-1;
	/* The old cursor position in answer, if any. */
38
static size_t old_pww = (size_t)-1;
39
	/* The old place we want in answer, if any. */
40

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

    *ran_func = FALSE;
    *finished = FALSE;

    /* Read in a character. */
64
    input = get_kbinput(bottomwin);
65

66
67
68
69
70
#ifndef NANO_TINY
    if (input == KEY_WINCH)
	return KEY_WINCH;
#endif

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

    /* Check for a shortcut in the current list. */
86
    s = get_shortcut(&input);
87
88
89

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

92
93
    /* 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. */
94
    if (!have_shortcut) {
95
	if (is_ascii_cntrl_char(input) || meta_key || func_key) {
96
	    beep();
97
98
	    meta_key = FALSE;
	    func_key = FALSE;
99
	    input = ERR;
100
101
102
	}
    }

103
104
105
106
107
108
109
110
    /* 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' ||
111
		currmenu != MWRITEFILE) {
112
113
114
	    kbinput_len++;
	    kbinput = (int *)nrealloc(kbinput, kbinput_len * sizeof(int));
	    kbinput[kbinput_len - 1] = input;
115
	}
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
     }

    /* 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. */
	    char *output = charalloc(kbinput_len + 1);
	    size_t i;
	    bool got_enter;
		/* Whether we got the Enter key. */

	    for (i = 0; i < kbinput_len; i++)
		output[i] = (char)kbinput[i];
	    output[i] = '\0';

	    do_statusbar_output(output, kbinput_len, &got_enter, FALSE);

	    free(output);

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

144
	if (have_shortcut) {
145
	    if (s->scfunc == do_tab || s->scfunc == do_enter)
146
		;
147
148
149
150
	    else if (s->scfunc == total_refresh) {
		total_redraw();
		refresh_func();
	    } else if (s->scfunc == do_cut_text_void) {
151
152
153
		/* If we're using restricted mode, the filename
		 * isn't blank, and we're at the "Write File"
		 * prompt, disable Cut. */
154
155
		if (!ISSET(RESTRICTED) || openfile->filename[0] == '\0' ||
			currmenu != MWRITEFILE)
156
		    do_statusbar_cut_text();
157
	    } else if (s->scfunc == do_left)
158
		do_statusbar_left();
159
160
	    else if (s->scfunc == do_right)
		do_statusbar_right();
161
#ifndef NANO_TINY
162
	    else if (s->scfunc == do_prev_word_void)
163
		do_statusbar_prev_word();
164
	    else if (s->scfunc == do_next_word_void)
165
		do_statusbar_next_word();
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
166
#endif
167
	    else if (s->scfunc == do_home)
168
		do_statusbar_home();
169
	    else if (s->scfunc == do_end)
170
		do_statusbar_end();
171
	    else if (s->scfunc == do_verbatim_input) {
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
		/* 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') {
		    bool got_enter;
		    /* Whether we got the Enter key. */

		    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);
187
			input = sc_seq_or(do_enter, 0);
188
			*finished = TRUE;
189
		    }
190
		}
191
	    } else if (s->scfunc == do_delete) {
192
193
194
		/* If we're using restricted mode, the filename
		 * isn't blank, and we're at the "Write File"
		 * prompt, disable Delete. */
195
196
		if (!ISSET(RESTRICTED) || openfile->filename[0] == '\0' ||
			currmenu != MWRITEFILE)
197
		    do_statusbar_delete();
198
	    } else if (s->scfunc == do_backspace) {
199
200
201
		/* If we're using restricted mode, the filename
		 * isn't blank, and we're at the "Write File"
		 * prompt, disable Backspace. */
202
203
		if (!ISSET(RESTRICTED) || openfile->filename[0] == '\0' ||
			currmenu != MWRITEFILE)
204
205
		    do_statusbar_backspace();
	    } else {
206
		/* Handle any other shortcut in the current menu, setting
207
208
209
210
		 * 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. */
211
		f = sctofunc(s);
212
		if (s->scfunc != NULL) {
213
		    *ran_func = TRUE;
214
215
		    if (f && (!ISSET(VIEW_MODE) || f->viewok) &&
				f->scfunc != do_gotolinecolumn_void)
216
			f->scfunc();
217
218
		}
		*finished = TRUE;
219
220
221
222
223
224
225
226
	    }
	}
    }

    return input;
}

#ifndef DISABLE_MOUSE
227
/* Handle a mouse click on the statusbar prompt or the shortcut list. */
228
int do_statusbar_mouse(void)
229
230
{
    int mouse_x, mouse_y;
231
    int retval = get_mouseinput(&mouse_x, &mouse_y, TRUE);
232

233
    /* We can click on the statusbar window text to move the cursor. */
234
    if (retval == 0 && wmouse_trafo(bottomwin, &mouse_y, &mouse_x, FALSE)) {
235
	size_t start_col;
236

237
	assert(prompt != NULL);
238

239
	start_col = strlenpt(prompt) + 2;
240

241
	/* Move to where the click occurred. */
242
	if (mouse_x >= start_col && mouse_y == 0) {
243
	    statusbar_x = actual_x(answer,
244
			get_statusbar_page_start(start_col, start_col +
245
			statusbar_xplustabs()) + mouse_x - start_col);
246
	    update_bar_if_needed();
247
248
249
250
251
252
253
254
	}
    }

    return retval;
}
#endif

/* The user typed output_len multibyte characters.  Add them to the
255
256
257
 * statusbar prompt, setting got_enter to TRUE if we get a newline, and
 * filtering out all ASCII control characters if allow_cntrls is
 * TRUE. */
258
void do_statusbar_output(char *output, size_t output_len, bool
259
	*got_enter, bool allow_cntrls)
260
261
262
263
264
265
266
267
268
269
270
{
    size_t answer_len, i = 0;
    char *char_buf = charalloc(mb_cur_max());
    int char_buf_len;

    assert(answer != NULL);

    answer_len = strlen(answer);
    *got_enter = FALSE;

    while (i < output_len) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
271
272
	/* If allow_cntrls is TRUE, convert nulls and newlines
	 * properly. */
273
274
275
	if (allow_cntrls) {
	    /* Null to newline, if needed. */
	    if (output[i] == '\0')
276
		output[i] = '\n';
277
278
	    /* Newline to Enter, if needed. */
	    else if (output[i] == '\n') {
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
		/* Set got_enter to TRUE to indicate that we got the
		 * Enter key, put back the rest of the characters in
		 * output so that they can be parsed and output again,
		 * and get out. */
		*got_enter = TRUE;
		unparse_kbinput(output + i, output_len - i);
		return;
	    }
	}

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

	i += char_buf_len;

294
	/* If allow_cntrls is FALSE, filter out an ASCII control
295
296
297
	 * character. */
	if (!allow_cntrls && is_ascii_cntrl_char(*(output + i -
		char_buf_len)))
298
299
	    continue;

Benno Schulenberg's avatar
Benno Schulenberg committed
300
	/* More dangerousness fun. =) */
301
302
303
304
	answer = charealloc(answer, answer_len + (char_buf_len * 2));

	assert(statusbar_x <= answer_len);

305
306
	charmove(answer + statusbar_x + char_buf_len, answer + statusbar_x,
			answer_len - statusbar_x + char_buf_len);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
307
	strncpy(answer + statusbar_x, char_buf, char_buf_len);
308
309
310
311
312
313
	answer_len += char_buf_len;

	statusbar_x += char_buf_len;
    }

    free(char_buf);
314
315
316

    statusbar_pww = statusbar_xplustabs();

317
    update_the_statusbar();
318
319
}

320
/* Move to the beginning of the prompt text. */
321
322
void do_statusbar_home(void)
{
323
    statusbar_x = 0;
324
    update_bar_if_needed();
325
326
}

327
/* Move to the end of the prompt text. */
328
329
330
void do_statusbar_end(void)
{
    statusbar_x = strlen(answer);
331
    update_bar_if_needed();
332
333
}

334
335
/* Move left one character. */
void do_statusbar_left(void)
336
{
337
338
    if (statusbar_x > 0) {
	statusbar_x = move_mbleft(answer, statusbar_x);
339
	update_bar_if_needed();
340
    }
341
342
}

343
344
/* Move right one character. */
void do_statusbar_right(void)
345
{
346
347
    if (statusbar_x < strlen(answer)) {
	statusbar_x = move_mbright(answer, statusbar_x);
348
	update_bar_if_needed();
349
    }
350
351
}

352
/* Backspace over one character. */
353
354
355
void do_statusbar_backspace(void)
{
    if (statusbar_x > 0) {
356
	statusbar_x = move_mbleft(answer, statusbar_x);
357
358
359
360
	do_statusbar_delete();
    }
}

361
/* Delete one character. */
362
363
void do_statusbar_delete(void)
{
364
365
    statusbar_pww = statusbar_xplustabs();

366
    if (answer[statusbar_x] != '\0') {
367
	int char_buf_len = parse_mbchar(answer + statusbar_x, NULL, NULL);
368
369
370
371
	size_t line_len = strlen(answer + statusbar_x);

	assert(statusbar_x < strlen(answer));

372
373
	charmove(answer + statusbar_x, answer + statusbar_x + char_buf_len,
			strlen(answer) - statusbar_x - char_buf_len + 1);
374
375

	null_at(&answer, statusbar_x + line_len - char_buf_len);
376

377
	update_the_statusbar();
378
379
380
    }
}

381
/* Move text from the prompt into oblivion. */
382
383
384
385
void do_statusbar_cut_text(void)
{
    assert(answer != NULL);

386
#ifndef NANO_TINY
387
388
    if (ISSET(CUT_TO_END))
	null_at(&answer, statusbar_x);
389
    else
390
#endif
391
    {
392
393
	null_at(&answer, 0);
	statusbar_x = 0;
394
	statusbar_pww = statusbar_xplustabs();
395
    }
396

397
    update_the_statusbar();
398
399
}

400
#ifndef NANO_TINY
401
402
/* Move to the next word in the prompt text. */
void do_statusbar_next_word(void)
403
{
404
    bool seen_space = !is_word_mbchar(answer + statusbar_x, FALSE);
405
406
407

    assert(answer != NULL);

408
409
410
    /* Move forward until we reach the start of a word. */
    while (answer[statusbar_x] != '\0') {
	statusbar_x = move_mbright(answer, statusbar_x);
411

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

420
    update_bar_if_needed();
421
422
}

423
424
/* Move to the previous word in the prompt text. */
void do_statusbar_prev_word(void)
425
{
426
    bool seen_a_word = FALSE, step_forward = FALSE;
427
428
429

    assert(answer != NULL);

430
431
    /* Move backward until we pass over the start of a word. */
    while (statusbar_x != 0) {
432
433
	statusbar_x = move_mbleft(answer, statusbar_x);

434
435
436
437
438
	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;
439
440
441
442
	    break;
	}
    }

443
444
445
    if (step_forward)
	/* Move one character forward again to sit on the start of the word. */
	statusbar_x = move_mbright(answer, statusbar_x);
446

447
    update_bar_if_needed();
448
}
449
#endif /* !NANO_TINY */
450

451
452
/* Get verbatim input.  Set got_enter to TRUE if we got the Enter key as
 * part of the verbatim input. */
453
454
455
456
457
458
459
460
461
462
463
464
void do_statusbar_verbatim_input(bool *got_enter)
{
    int *kbinput;
    size_t kbinput_len, i;
    char *output;

    *got_enter = FALSE;

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

    /* Display all the verbatim characters at once, not filtering out
465
     * control characters. */
466
467
468
469
470
471
    output = charalloc(kbinput_len + 1);

    for (i = 0; i < kbinput_len; i++)
	output[i] = (char)kbinput[i];
    output[i] = '\0';

472
    do_statusbar_output(output, kbinput_len, got_enter, TRUE);
473
474
475
476

    free(output);
}

477
/* Return the placewewant associated with statusbar_x, i.e. the
478
479
480
481
482
483
484
485
486
487
488
489
490
491
 * 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)
{
492
    if (column == start_col || column < COLS - 1 || COLS == start_col + 1)
493
494
495
496
497
498
	return 0;
    else
	return column - start_col - (column - start_col) % (COLS -
		start_col - 1);
}

499
500
501
/* Put the cursor in the statusbar prompt at statusbar_x. */
void reset_statusbar_cursor(void)
{
502
    size_t start_col = strlenpt(prompt) + 2;
503
504
    size_t xpt = statusbar_xplustabs();

505
    wmove(bottomwin, 0, start_col + xpt -
506
507
508
	get_statusbar_page_start(start_col, start_col + xpt));
}

509
510
/* Repaint the statusbar. */
void update_the_statusbar(void)
511
{
512
    size_t start_col, index, page_start;
513
514
    char *expanded;

515
    assert(prompt != NULL && statusbar_x <= strlen(answer));
516

517
    start_col = strlenpt(prompt) + 2;
518
    index = strnlenpt(answer, statusbar_x);
519
    page_start = get_statusbar_page_start(start_col, start_col + index);
520

521
522
523
    if (interface_color_pair[TITLE_BAR].bright)
	wattron(bottomwin, A_BOLD);
    wattron(bottomwin, interface_color_pair[TITLE_BAR].pairnum);
524
525
526
527
528
529
530

    blank_statusbar();

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

531
    expanded = display_string(answer, page_start, COLS - start_col - 1, FALSE);
532
533
534
    waddstr(bottomwin, expanded);
    free(expanded);

535
536
    wattroff(bottomwin, A_BOLD);
    wattroff(bottomwin, interface_color_pair[TITLE_BAR].pairnum);
537

538
    statusbar_pww = statusbar_xplustabs();
539
    reset_statusbar_cursor();
540
    wnoutrefresh(bottomwin);
541
542
}

543
/* Update the statusbar line /if/ the placewewant changes page. */
544
void update_bar_if_needed(void)
545
{
546
    size_t start_col = strlenpt(prompt) + 2;
547
548
549
550
    size_t was_pww = statusbar_pww;

    statusbar_pww = statusbar_xplustabs();

551
552
    if (get_statusbar_page_start(start_col, start_col + statusbar_pww) !=
		get_statusbar_page_start(start_col, start_col + was_pww))
553
	update_the_statusbar();
554
555
}

556
/* Get a string of input at the statusbar prompt. */
557
functionptrtype get_prompt_string(int *actual, bool allow_tabs,
558
559
#ifndef DISABLE_TABCOMP
	bool allow_files,
560
	bool *list,
561
562
#endif
	const char *curranswer,
563
#ifndef DISABLE_HISTORIES
564
565
	filestruct **history_list,
#endif
566
	void (*refresh_func)(void))
567
{
568
    int kbinput = ERR;
569
    bool ran_func, finished;
570
    functionptrtype func;
571
572
573
574
#ifndef DISABLE_TABCOMP
    bool tabbed = FALSE;
	/* Whether we've pressed Tab. */
#endif
575
#ifndef DISABLE_HISTORIES
576
577
578
579
580
581
582
583
584
585
586
587
    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
588
#endif /* !DISABLE_HISTORIES */
589
590
591

    answer = mallocstrcpy(answer, curranswer);

592
593
    if (statusbar_x > strlen(answer)) {
	statusbar_x = strlen(answer);
594
595
	statusbar_pww = statusbar_xplustabs();
    }
596

597
#ifdef DEBUG
598
    fprintf(stderr, "get_prompt_string: answer = \"%s\", statusbar_x = %lu\n", answer, (unsigned long) statusbar_x);
599
#endif
600

601
    update_the_statusbar();
602

603
    /* Refresh edit window and statusbar before getting input. */
604
605
606
607
    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
608
609
610
611
     * 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. */
612
    while (TRUE) {
613
	kbinput = do_statusbar_input(&ran_func, &finished, refresh_func);
614
615
	assert(statusbar_x <= strlen(answer));

616
#ifndef NANO_TINY
617
618
	if (kbinput == KEY_WINCH) {
	    refresh_func();
619
	    update_the_statusbar();
620
621
	    continue;
	}
622
#endif
623
	func = func_from_key(&kbinput);
624

625
	if (func == do_cancel || func == do_enter)
626
	    break;
627

628
#ifndef DISABLE_TABCOMP
629
	if (func != do_tab)
630
631
	    tabbed = FALSE;

632
	if (func == do_tab) {
633
#ifndef DISABLE_HISTORIES
634
635
636
	    if (history_list != NULL) {
		if (last_kbinput != sc_seq_or(do_tab, NANO_CONTROL_I))
		    complete_len = strlen(answer);
637

638
639
		if (complete_len > 0) {
		    answer = mallocstrcpy(answer,
640
				get_history_completion(history_list,
641
642
643
644
					answer, complete_len));
		    statusbar_x = strlen(answer);
		}
	    } else
645
#endif
646
647
648
	    if (allow_tabs)
		answer = input_tab(answer, allow_files, &statusbar_x,
				   &tabbed, refresh_func, list);
649

650
	    update_the_statusbar();
651
	} else
652
#endif /* !DISABLE_TABCOMP */
653
#ifndef DISABLE_HISTORIES
654
	if (func == get_history_older_void) {
655
656
657
658
659
660
661
662
663
664
665
666
	    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. */
		if ((*history_list)->next == NULL && answer[0] != '\0')
		    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);
		}
667

668
		update_the_statusbar();
669

670
671
672
673
		/* 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. */
674
		finished = FALSE;
675
	    }
676
	} else if (func == get_history_newer_void) {
677
678
679
680
681
682
683
	    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);
		}
684

685
686
687
688
		/* 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 &&
689
690
691
692
			*answer == '\0' && magichistory != NULL) {
		    answer = mallocstrcpy(answer, magichistory);
		    statusbar_x = strlen(answer);
		}
693

694
		update_the_statusbar();
695

696
697
698
699
700
701
		/* 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;
	    }
702
	} else
703
#endif /* !DISABLE_HISTORIES */
704
	if (func == do_help_void) {
705
	    update_the_statusbar();
706

707
708
709
710
711
712
	    /* 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;
713
714
	}

715
716
	/* If we have a shortcut with an associated function, break out if
	 * we're finished after running or trying to run the function. */
717
718
719
	if (finished)
	    break;

720
#if !defined(DISABLE_HISTORIES) && !defined(DISABLE_TABCOMP)
721
722
	last_kbinput = kbinput;
#endif
723
	reset_statusbar_cursor();
724
	wnoutrefresh(bottomwin);
725
    }
726

727
#ifndef DISABLE_HISTORIES
728
    /* Set the current position in the history list to the bottom. */
729
730
    if (history_list != NULL) {
	history_reset(*history_list);
731
	free(magichistory);
732
733
734
    }
#endif

735
736
737
738
739
    /* If we're done with this prompt, restore the cursor position
     * to what it was at the /previous/ prompt, in case there was. */
    if (func == do_cancel || func == do_enter) {
	statusbar_x = old_statusbar_x;
	statusbar_pww = old_pww;
740
    }
741

742
    *actual = kbinput;
743
744

    return func;
745
746
}

747
748
749
750
/* 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.
751
752
753
 * 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.
754
755
 *
 * The allow_tabs parameter indicates whether we should allow tabs to be
756
 * interpreted.  The allow_files parameter indicates whether we should
757
 * allow all files (as opposed to just directories) to be tab completed. */
758
759
760
761
int do_prompt(bool allow_tabs,
#ifndef DISABLE_TABCOMP
	bool allow_files,
#endif
762
	int menu, const char *curranswer,
763
#ifndef DISABLE_HISTORIES
764
765
	filestruct **history_list,
#endif
766
	void (*refresh_func)(void), const char *msg, ...)
767
768
769
{
    va_list ap;
    int retval;
770
    functionptrtype func;
771
772
773
774
#ifndef DISABLE_TABCOMP
    bool list = FALSE;
#endif

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

777
    bottombars(menu);
778
779
780
781
782
783

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

784
    func = get_prompt_string(&retval, allow_tabs,
785
786
#ifndef DISABLE_TABCOMP
	allow_files,
787
	&list,
788
789
#endif
	curranswer,
790
#ifndef DISABLE_HISTORIES
791
	history_list,
792
#endif
793
	refresh_func);
794

795
796
797
    free(prompt);
    prompt = NULL;

798
799
    /* We're done with the prompt, so save the statusbar cursor
     * position. */
800
    old_statusbar_x = statusbar_x;
801
    old_pww = statusbar_pww;
802

803
804
    /* If we left the prompt via Cancel or Enter, set the return value
     * properly. */
805
    if (func == do_cancel)
806
	retval = -1;
807
    else if (func == do_enter)
808
	retval = (*answer == '\0') ? -2 : 0;
809

810
811
812
813
814
815
816
817
818
819
820
821
    blank_statusbar();
    wnoutrefresh(bottomwin);

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

#ifndef DISABLE_TABCOMP
    /* If we've done tab completion, there might be a list of filename
     * matches on the edit window at this point.  Make sure that they're
     * cleared off. */
    if (list)
822
	refresh_func();
823
824
825
826
827
#endif

    return retval;
}

828
829
830
/* 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. */
831
int do_yesno_prompt(bool all, const char *msg)
832
833
834
835
836
{
    int ok = -2, width = 16;
    const char *yesstr;		/* String of Yes characters accepted. */
    const char *nostr;		/* Same for No. */
    const char *allstr;		/* And All, surprise! */
837
    int oldmenu = currmenu;
838
839
840
841
842
843

    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
844
845
     * displayed in the shortcuts. */
    /* TRANSLATORS: For the next three strings, if possible, specify
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
846
847
     * the single-byte shortcuts for both your language and English.
     * For example, in French: "OoYy" for "Oui". */
848
849
850
851
    yesstr = _("Yy");
    nostr = _("Nn");
    allstr = _("Aa");

852
853
854
855
856
857
858
    do {
	int kbinput;
	functionptrtype func;
#ifndef DISABLE_MOUSE
	int mouse_x, mouse_y;
#endif

859
860
	if (!ISSET(NO_HELP)) {
	    char shortstr[3];
861
		/* Temporary string for (translated) " Y", " N" and " A". */
862

863
864
	    if (COLS < 32)
		width = COLS / 2;
865

866
867
	    /* Clear the shortcut list from the bottom of the screen. */
	    blank_bottombars();
868

869
870
871
872
	    /* Now show the ones for "Yes", "No", "Cancel" and maybe "All". */
	    sprintf(shortstr, " %c", yesstr[0]);
	    wmove(bottomwin, 1, 0);
	    onekey(shortstr, _("Yes"), width);
873

874
875
	    if (all) {
		shortstr[1] = allstr[0];
876
		wmove(bottomwin, 1, width);
877
878
		onekey(shortstr, _("All"), width);
	    }
879

880
	    shortstr[1] = nostr[0];
881
	    wmove(bottomwin, 2, 0);
882
	    onekey(shortstr, _("No"), width);
883

884
	    wmove(bottomwin, 2, width);
885
886
	    onekey("^C", _("Cancel"), width);
	}
887

888
889
890
	if (interface_color_pair[TITLE_BAR].bright)
	    wattron(bottomwin, A_BOLD);
	wattron(bottomwin, interface_color_pair[TITLE_BAR].pairnum);
891

892
893
	blank_statusbar();
	mvwaddnstr(bottomwin, 0, 0, msg, actual_x(msg, COLS - 1));
894

895
896
	wattroff(bottomwin, A_BOLD);
	wattroff(bottomwin, interface_color_pair[TITLE_BAR].pairnum);
897

898
899
900
	/* Refresh edit window and statusbar before getting input. */
	wnoutrefresh(edit);
	wnoutrefresh(bottomwin);
901

902
	currmenu = MYESNO;
903
	kbinput = get_kbinput(bottomwin);
904
905
906
907
908
909

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

910
	func = func_from_key(&kbinput);
911

912
	if (func == do_cancel)
913
	    ok = -1;
914
#ifndef DISABLE_MOUSE
915
	else if (kbinput == KEY_MOUSE) {
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
	    /* 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. */
		ok = -2 * x * y + x - y + 1;

		if (ok == 2 && !all)
		    ok = -2;
	    }
934
	}
935
#endif /* !DISABLE_MOUSE */
936
	else if (func == total_refresh) {
937
938
939
	    total_redraw();
	    continue;
	} else {
940
941
942
943
944
945
946
	    /* Look for the kbinput in the Yes, No (and All) strings. */
	    if (strchr(yesstr, kbinput) != NULL)
		ok = 1;
	    else if (strchr(nostr, kbinput) != NULL)
		ok = 0;
	    else if (all && strchr(allstr, kbinput) != NULL)
		ok = 2;
947
948
949
	}
    } while (ok == -2);

950
    currmenu = oldmenu;
951
952
    return ok;
}