prompt.c 27.4 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
     }

    /* 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) {
123
124
	    bool dummy;

125
126
	    /* Display all the characters in the input buffer at
	     * once, filtering out control characters. */
127
	    do_statusbar_output(kbinput, kbinput_len, &dummy, FALSE);
128
129
130
131
132

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

135
	if (have_shortcut) {
136
	    if (s->scfunc == do_tab || s->scfunc == do_enter)
137
		;
138
139
140
141
	    else if (s->scfunc == total_refresh) {
		total_redraw();
		refresh_func();
	    } else if (s->scfunc == do_cut_text_void) {
142
143
144
		/* If we're using restricted mode, the filename
		 * isn't blank, and we're at the "Write File"
		 * prompt, disable Cut. */
145
146
		if (!ISSET(RESTRICTED) || openfile->filename[0] == '\0' ||
			currmenu != MWRITEFILE)
147
		    do_statusbar_cut_text();
148
	    } else if (s->scfunc == do_left)
149
		do_statusbar_left();
150
151
	    else if (s->scfunc == do_right)
		do_statusbar_right();
152
#ifndef NANO_TINY
153
	    else if (s->scfunc == do_prev_word_void)
154
		do_statusbar_prev_word();
155
	    else if (s->scfunc == do_next_word_void)
156
		do_statusbar_next_word();
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
157
#endif
158
	    else if (s->scfunc == do_home)
159
		do_statusbar_home();
160
	    else if (s->scfunc == do_end)
161
		do_statusbar_end();
162
	    else if (s->scfunc == do_verbatim_input) {
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
		/* 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);
178
			input = sc_seq_or(do_enter, 0);
179
			*finished = TRUE;
180
		    }
181
		}
182
	    } else if (s->scfunc == do_delete) {
183
184
185
		/* If we're using restricted mode, the filename
		 * isn't blank, and we're at the "Write File"
		 * prompt, disable Delete. */
186
187
		if (!ISSET(RESTRICTED) || openfile->filename[0] == '\0' ||
			currmenu != MWRITEFILE)
188
		    do_statusbar_delete();
189
	    } else if (s->scfunc == do_backspace) {
190
191
192
		/* If we're using restricted mode, the filename
		 * isn't blank, and we're at the "Write File"
		 * prompt, disable Backspace. */
193
194
		if (!ISSET(RESTRICTED) || openfile->filename[0] == '\0' ||
			currmenu != MWRITEFILE)
195
196
		    do_statusbar_backspace();
	    } else {
197
		/* Handle any other shortcut in the current menu, setting
198
199
200
201
		 * 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. */
202
		f = sctofunc(s);
203
		if (s->scfunc != NULL) {
204
		    *ran_func = TRUE;
205
206
		    if (f && (!ISSET(VIEW_MODE) || f->viewok) &&
				f->scfunc != do_gotolinecolumn_void)
207
			f->scfunc();
208
209
		}
		*finished = TRUE;
210
211
212
213
214
215
216
217
	    }
	}
    }

    return input;
}

#ifndef DISABLE_MOUSE
218
/* Handle a mouse click on the statusbar prompt or the shortcut list. */
219
int do_statusbar_mouse(void)
220
221
{
    int mouse_x, mouse_y;
222
    int retval = get_mouseinput(&mouse_x, &mouse_y, TRUE);
223

224
    /* We can click on the statusbar window text to move the cursor. */
225
    if (retval == 0 && wmouse_trafo(bottomwin, &mouse_y, &mouse_x, FALSE)) {
226
	size_t start_col;
227

228
	assert(prompt != NULL);
229

230
	start_col = strlenpt(prompt) + 2;
231

232
	/* Move to where the click occurred. */
233
	if (mouse_x >= start_col && mouse_y == 0) {
234
	    statusbar_x = actual_x(answer,
235
			get_statusbar_page_start(start_col, start_col +
236
			statusbar_xplustabs()) + mouse_x - start_col);
237
	    update_bar_if_needed();
238
239
240
241
242
243
244
245
	}
    }

    return retval;
}
#endif

/* The user typed output_len multibyte characters.  Add them to the
246
247
248
 * statusbar prompt, setting got_enter to TRUE if we get a newline, and
 * filtering out all ASCII control characters if allow_cntrls is
 * TRUE. */
249
void do_statusbar_output(int *the_input, size_t output_len, bool
250
	*got_enter, bool allow_cntrls)
251
{
252
253
    size_t answer_len, i;
    char *output = charalloc(output_len + 1);
254
255
256
257
258
    char *char_buf = charalloc(mb_cur_max());
    int char_buf_len;

    assert(answer != NULL);

259
260
261
262
263
    /* Copy the typed stuff so it can be treated. */
    for (i = 0; i < output_len; i++)
	output[i] = (char)the_input[i];
    output[i] = '\0';

264
265
    answer_len = strlen(answer);
    *got_enter = FALSE;
266
    i = 0;
267
268

    while (i < output_len) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
269
270
	/* If allow_cntrls is TRUE, convert nulls and newlines
	 * properly. */
271
272
273
	if (allow_cntrls) {
	    /* Null to newline, if needed. */
	    if (output[i] == '\0')
274
		output[i] = '\n';
275
276
	    /* Newline to Enter, if needed. */
	    else if (output[i] == '\n') {
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
		/* 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;

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

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

	assert(statusbar_x <= answer_len);

303
304
	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
305
	strncpy(answer + statusbar_x, char_buf, char_buf_len);
306
307
308
309
310
311
	answer_len += char_buf_len;

	statusbar_x += char_buf_len;
    }

    free(char_buf);
312
    free(output);
313
314
315

    statusbar_pww = statusbar_xplustabs();

316
    update_the_statusbar();
317
318
}

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

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

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

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

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

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

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

	assert(statusbar_x < strlen(answer));

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

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

376
	update_the_statusbar();
377
378
379
    }
}

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

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

396
    update_the_statusbar();
397
398
}

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

    assert(answer != NULL);

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

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

419
    update_bar_if_needed();
420
421
}

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

    assert(answer != NULL);

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

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

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

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

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

    *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
463
     * control characters. */
464
    do_statusbar_output(kbinput, kbinput_len, got_enter, TRUE);
465
466
}

467
/* Return the placewewant associated with statusbar_x, i.e. the
468
469
470
471
472
473
474
475
476
477
478
479
480
481
 * 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)
{
482
    if (column == start_col || column < COLS - 1 || COLS == start_col + 1)
483
484
485
486
487
488
	return 0;
    else
	return column - start_col - (column - start_col) % (COLS -
		start_col - 1);
}

489
490
491
/* Put the cursor in the statusbar prompt at statusbar_x. */
void reset_statusbar_cursor(void)
{
492
    size_t start_col = strlenpt(prompt) + 2;
493
494
    size_t xpt = statusbar_xplustabs();

495
    wmove(bottomwin, 0, start_col + xpt -
496
497
498
	get_statusbar_page_start(start_col, start_col + xpt));
}

499
500
/* Repaint the statusbar. */
void update_the_statusbar(void)
501
{
502
    size_t start_col, index, page_start;
503
504
    char *expanded;

505
    assert(prompt != NULL && statusbar_x <= strlen(answer));
506

507
    start_col = strlenpt(prompt) + 2;
508
    index = strnlenpt(answer, statusbar_x);
509
    page_start = get_statusbar_page_start(start_col, start_col + index);
510

511
512
513
    if (interface_color_pair[TITLE_BAR].bright)
	wattron(bottomwin, A_BOLD);
    wattron(bottomwin, interface_color_pair[TITLE_BAR].pairnum);
514
515
516
517
518
519
520

    blank_statusbar();

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

521
    expanded = display_string(answer, page_start, COLS - start_col - 1, FALSE);
522
523
524
    waddstr(bottomwin, expanded);
    free(expanded);

525
526
    wattroff(bottomwin, A_BOLD);
    wattroff(bottomwin, interface_color_pair[TITLE_BAR].pairnum);
527

528
    statusbar_pww = statusbar_xplustabs();
529
    reset_statusbar_cursor();
530
    wnoutrefresh(bottomwin);
531
532
}

533
/* Update the statusbar line /if/ the placewewant changes page. */
534
void update_bar_if_needed(void)
535
{
536
    size_t start_col = strlenpt(prompt) + 2;
537
538
539
540
    size_t was_pww = statusbar_pww;

    statusbar_pww = statusbar_xplustabs();

541
542
    if (get_statusbar_page_start(start_col, start_col + statusbar_pww) !=
		get_statusbar_page_start(start_col, start_col + was_pww))
543
	update_the_statusbar();
544
545
}

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

    answer = mallocstrcpy(answer, curranswer);

582
583
    if (statusbar_x > strlen(answer)) {
	statusbar_x = strlen(answer);
584
585
	statusbar_pww = statusbar_xplustabs();
    }
586

587
#ifdef DEBUG
588
    fprintf(stderr, "get_prompt_string: answer = \"%s\", statusbar_x = %lu\n", answer, (unsigned long) statusbar_x);
589
#endif
590

591
    update_the_statusbar();
592

593
    /* Refresh edit window and statusbar before getting input. */
594
595
596
597
    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
598
599
600
601
     * 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. */
602
    while (TRUE) {
603
604
605
	/* Ensure the cursor is on when waiting for input. */
	curs_set(1);

606
	kbinput = do_statusbar_input(&ran_func, &finished, refresh_func);
607
608
	assert(statusbar_x <= strlen(answer));

609
#ifndef NANO_TINY
610
611
	if (kbinput == KEY_WINCH) {
	    refresh_func();
612
	    update_the_statusbar();
613
614
	    continue;
	}
615
#endif
616
	func = func_from_key(&kbinput);
617

618
	if (func == do_cancel || func == do_enter)
619
	    break;
620

621
#ifndef DISABLE_TABCOMP
622
	if (func != do_tab)
623
624
	    tabbed = FALSE;

625
	if (func == do_tab) {
626
#ifndef DISABLE_HISTORIES
627
628
629
	    if (history_list != NULL) {
		if (last_kbinput != sc_seq_or(do_tab, NANO_CONTROL_I))
		    complete_len = strlen(answer);
630

631
632
		if (complete_len > 0) {
		    answer = mallocstrcpy(answer,
633
				get_history_completion(history_list,
634
635
636
637
					answer, complete_len));
		    statusbar_x = strlen(answer);
		}
	    } else
638
#endif
639
640
641
	    if (allow_tabs)
		answer = input_tab(answer, allow_files, &statusbar_x,
				   &tabbed, refresh_func, list);
642

643
	    update_the_statusbar();
644
	} else
645
#endif /* !DISABLE_TABCOMP */
646
#ifndef DISABLE_HISTORIES
647
	if (func == get_history_older_void) {
648
649
650
651
652
653
654
655
656
657
658
659
	    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);
		}
660

661
		update_the_statusbar();
662

663
664
665
666
		/* 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. */
667
		finished = FALSE;
668
	    }
669
	} else if (func == get_history_newer_void) {
670
671
672
673
674
675
676
	    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);
		}
677

678
679
680
681
		/* 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 &&
682
683
684
685
			*answer == '\0' && magichistory != NULL) {
		    answer = mallocstrcpy(answer, magichistory);
		    statusbar_x = strlen(answer);
		}
686

687
		update_the_statusbar();
688

689
690
691
692
693
694
		/* 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;
	    }
695
	} else
696
#endif /* !DISABLE_HISTORIES */
697
	if (func == do_help_void) {
698
	    update_the_statusbar();
699

700
701
702
703
704
705
	    /* 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;
706
707
	}

708
709
	/* If we have a shortcut with an associated function, break out if
	 * we're finished after running or trying to run the function. */
710
711
712
	if (finished)
	    break;

713
#if !defined(DISABLE_HISTORIES) && !defined(DISABLE_TABCOMP)
714
715
	last_kbinput = kbinput;
#endif
716
	reset_statusbar_cursor();
717
	wnoutrefresh(bottomwin);
718
    }
719

720
#ifndef DISABLE_HISTORIES
721
    /* Set the current position in the history list to the bottom. */
722
723
    if (history_list != NULL) {
	history_reset(*history_list);
724
	free(magichistory);
725
726
727
    }
#endif

728
729
730
731
732
    /* 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;
733
    }
734

735
    *actual = kbinput;
736
737

    return func;
738
739
}

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

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

770
    bottombars(menu);
771
772
773
774
775
776

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

777
    func = get_prompt_string(&retval, allow_tabs,
778
779
#ifndef DISABLE_TABCOMP
	allow_files,
780
	&list,
781
782
#endif
	curranswer,
783
#ifndef DISABLE_HISTORIES
784
	history_list,
785
#endif
786
	refresh_func);
787

788
789
790
    free(prompt);
    prompt = NULL;

791
792
    /* We're done with the prompt, so save the statusbar cursor
     * position. */
793
    old_statusbar_x = statusbar_x;
794
    old_pww = statusbar_pww;
795

796
797
    /* If we left the prompt via Cancel or Enter, set the return value
     * properly. */
798
    if (func == do_cancel)
799
	retval = -1;
800
    else if (func == do_enter)
801
	retval = (*answer == '\0') ? -2 : 0;
802

803
804
805
806
807
808
809
810
811
812
813
814
    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)
815
	refresh_func();
816
817
818
819
820
#endif

    return retval;
}

821
822
823
/* 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. */
824
int do_yesno_prompt(bool all, const char *msg)
825
{
826
    int response = -2, width = 16;
827
828
829
    const char *yesstr;		/* String of Yes characters accepted. */
    const char *nostr;		/* Same for No. */
    const char *allstr;		/* And All, surprise! */
830
    int oldmenu = currmenu;
831
832
833
834
835
836

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

845
846
847
848
849
850
851
    do {
	int kbinput;
	functionptrtype func;
#ifndef DISABLE_MOUSE
	int mouse_x, mouse_y;
#endif

852
853
	if (!ISSET(NO_HELP)) {
	    char shortstr[3];
854
		/* Temporary string for (translated) " Y", " N" and " A". */
855

856
857
	    if (COLS < 32)
		width = COLS / 2;
858

859
860
	    /* Clear the shortcut list from the bottom of the screen. */
	    blank_bottombars();
861

862
863
864
865
	    /* Now show the ones for "Yes", "No", "Cancel" and maybe "All". */
	    sprintf(shortstr, " %c", yesstr[0]);
	    wmove(bottomwin, 1, 0);
	    onekey(shortstr, _("Yes"), width);
866

867
868
	    if (all) {
		shortstr[1] = allstr[0];
869
		wmove(bottomwin, 1, width);
870
871
		onekey(shortstr, _("All"), width);
	    }
872

873
	    shortstr[1] = nostr[0];
874
	    wmove(bottomwin, 2, 0);
875
	    onekey(shortstr, _("No"), width);
876

877
	    wmove(bottomwin, 2, width);
878
879
	    onekey("^C", _("Cancel"), width);
	}
880

881
882
883
	if (interface_color_pair[TITLE_BAR].bright)
	    wattron(bottomwin, A_BOLD);
	wattron(bottomwin, interface_color_pair[TITLE_BAR].pairnum);
884

885
886
	blank_statusbar();
	mvwaddnstr(bottomwin, 0, 0, msg, actual_x(msg, COLS - 1));
887

888
889
	wattroff(bottomwin, A_BOLD);
	wattroff(bottomwin, interface_color_pair[TITLE_BAR].pairnum);
890

891
892
893
	/* Refresh edit window and statusbar before getting input. */
	wnoutrefresh(edit);
	wnoutrefresh(bottomwin);
894

895
	currmenu = MYESNO;
896
	kbinput = get_kbinput(bottomwin);
897
898
899
900
901
902

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

903
	func = func_from_key(&kbinput);
904

905
	if (func == do_cancel)
906
	    response = -1;
907
#ifndef DISABLE_MOUSE
908
	else if (kbinput == KEY_MOUSE) {
909
910
911
912
913
914
915
916
917
918
919
920
921
	    /* 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. */
922
		response = -2 * x * y + x - y + 1;
923

924
925
		if (response == 2 && !all)
		    response = -2;
926
	    }
927
	}
928
#endif /* !DISABLE_MOUSE */
929
	else if (func == total_refresh) {
930
931
932
	    total_redraw();
	    continue;
	} else {
933
934
	    /* Look for the kbinput in the Yes, No (and All) strings. */
	    if (strchr(yesstr, kbinput) != NULL)
935
		response = 1;
936
	    else if (strchr(nostr, kbinput) != NULL)
937
		response = 0;
938
	    else if (all && strchr(allstr, kbinput) != NULL)
939
		response = 2;
940
	}
941
    } while (response == -2);
942

943
    currmenu = oldmenu;
944
    return response;
945
}