prompt.c 28.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
234
235
236
    /* We can click on the statusbar window text to move the cursor. */
    if (retval == 0 && wmouse_trafo(bottomwin, &mouse_y, &mouse_x,
	FALSE)) {
	size_t start_col;
237

238
	assert(prompt != NULL);
239

240
	start_col = strlenpt(prompt) + 2;
241

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

    return retval;
}
#endif

/* The user typed output_len multibyte characters.  Add them to the
256
257
258
 * statusbar prompt, setting got_enter to TRUE if we get a newline, and
 * filtering out all ASCII control characters if allow_cntrls is
 * TRUE. */
259
void do_statusbar_output(char *output, size_t output_len, bool
260
	*got_enter, bool allow_cntrls)
261
262
263
264
265
266
267
268
269
270
271
{
    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
272
273
	/* If allow_cntrls is TRUE, convert nulls and newlines
	 * properly. */
274
275
276
	if (allow_cntrls) {
	    /* Null to newline, if needed. */
	    if (output[i] == '\0')
277
		output[i] = '\n';
278
279
	    /* Newline to Enter, if needed. */
	    else if (output[i] == '\n') {
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
		/* 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;

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

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

	assert(statusbar_x <= answer_len);

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

	statusbar_x += char_buf_len;
    }

    free(char_buf);
316
317
318

    statusbar_pww = statusbar_xplustabs();

319
    update_statusbar_line(answer, statusbar_x);
320
321
}

322
323
324
325
/* Move to the beginning of the prompt text.  If the SMART_HOME flag is
 * set, move to the first non-whitespace character of the prompt text if
 * we're not already there, or to the beginning of the prompt text if we
 * are. */
326
327
void do_statusbar_home(void)
{
328
#ifndef NANO_TINY
329
330
331
332
333
334
335
336
    if (ISSET(SMART_HOME)) {
	size_t statusbar_x_save = statusbar_x;

	statusbar_x = indent_length(answer);

	if (statusbar_x == statusbar_x_save ||
		statusbar_x == strlen(answer))
	    statusbar_x = 0;
337
    } else
338
339
#endif
	statusbar_x = 0;
340

341
    update_the_bar();
342
343
}

344
/* Move to the end of the prompt text. */
345
346
347
void do_statusbar_end(void)
{
    statusbar_x = strlen(answer);
348
    update_the_bar();
349
350
}

351
352
/* Move left one character. */
void do_statusbar_left(void)
353
{
354
355
    if (statusbar_x > 0) {
	statusbar_x = move_mbleft(answer, statusbar_x);
356
	update_the_bar();
357
    }
358
359
}

360
361
/* Move right one character. */
void do_statusbar_right(void)
362
{
363
364
    if (statusbar_x < strlen(answer)) {
	statusbar_x = move_mbright(answer, statusbar_x);
365
	update_the_bar();
366
    }
367
368
}

369
/* Backspace over one character. */
370
371
372
373
374
375
376
377
void do_statusbar_backspace(void)
{
    if (statusbar_x > 0) {
	do_statusbar_left();
	do_statusbar_delete();
    }
}

378
/* Delete one character. */
379
380
void do_statusbar_delete(void)
{
381
382
    statusbar_pww = statusbar_xplustabs();

383
384
385
386
387
388
389
390
391
392
393
394
    if (answer[statusbar_x] != '\0') {
	int char_buf_len = parse_mbchar(answer + statusbar_x, NULL,
		NULL);
	size_t line_len = strlen(answer + statusbar_x);

	assert(statusbar_x < strlen(answer));

	charmove(answer + statusbar_x, answer + statusbar_x +
		char_buf_len, strlen(answer) - statusbar_x -
		char_buf_len + 1);

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

	update_statusbar_line(answer, statusbar_x);
397
398
399
    }
}

400
/* Move text from the prompt into oblivion. */
401
402
403
404
void do_statusbar_cut_text(void)
{
    assert(answer != NULL);

405
#ifndef NANO_TINY
406
407
    if (ISSET(CUT_TO_END))
	null_at(&answer, statusbar_x);
408
    else
409
#endif
410
    {
411
412
	null_at(&answer, 0);
	statusbar_x = 0;
413
	statusbar_pww = statusbar_xplustabs();
414
    }
415

416
    update_statusbar_line(answer, statusbar_x);
417
418
}

419
#ifndef NANO_TINY
420
421
/* Move to the next word in the prompt text. */
void do_statusbar_next_word(void)
422
{
423
    bool seen_space = !is_word_mbchar(answer + statusbar_x, FALSE);
424
425
426

    assert(answer != NULL);

427
428
429
    /* Move forward until we reach the start of a word. */
    while (answer[statusbar_x] != '\0') {
	statusbar_x = move_mbright(answer, statusbar_x);
430

431
432
433
434
435
	/* 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)
436
437
438
	    break;
    }

439
    update_the_bar();
440
441
}

442
443
/* Move to the previous word in the prompt text. */
void do_statusbar_prev_word(void)
444
{
445
    bool seen_a_word = FALSE, step_forward = FALSE;
446
447
448

    assert(answer != NULL);

449
450
    /* Move backward until we pass over the start of a word. */
    while (statusbar_x != 0) {
451
452
	statusbar_x = move_mbleft(answer, statusbar_x);

453
454
455
456
457
	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;
458
459
460
461
	    break;
	}
    }

462
463
464
    if (step_forward)
	/* Move one character forward again to sit on the start of the word. */
	statusbar_x = move_mbright(answer, statusbar_x);
465

466
    update_the_bar();
467
}
468
#endif /* !NANO_TINY */
469

470
471
/* Get verbatim input.  Set got_enter to TRUE if we got the Enter key as
 * part of the verbatim input. */
472
473
474
475
476
477
478
479
480
481
482
483
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
484
     * control characters. */
485
486
487
488
489
490
    output = charalloc(kbinput_len + 1);

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

491
    do_statusbar_output(output, kbinput_len, got_enter, TRUE);
492
493
494
495

    free(output);
}

496

497
/* Return the placewewant associated with statusbar_x, i.e. the
498
499
500
501
502
503
504
505
506
507
508
509
510
511
 * 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)
{
512
    if (column == start_col || column < COLS - 1 || COLS == start_col + 1)
513
514
515
516
517
518
	return 0;
    else
	return column - start_col - (column - start_col) % (COLS -
		start_col - 1);
}

519
520
521
/* Put the cursor in the statusbar prompt at statusbar_x. */
void reset_statusbar_cursor(void)
{
522
    size_t start_col = strlenpt(prompt) + 2;
523
524
    size_t xpt = statusbar_xplustabs();

525
    wmove(bottomwin, 0, start_col + xpt -
526
527
528
	get_statusbar_page_start(start_col, start_col + xpt));
}

529
530
/* Repaint the statusbar when getting a character in
 * get_prompt_string().  The statusbar text line will be displayed
531
 * starting with curranswer[index]. */
532
void update_statusbar_line(const char *curranswer, size_t index)
533
{
534
    size_t start_col, page_start;
535
536
    char *expanded;

537
    assert(prompt != NULL && index <= strlen(curranswer));
538

539
    start_col = strlenpt(prompt) + 2;
540
541
    index = strnlenpt(curranswer, index);
    page_start = get_statusbar_page_start(start_col, start_col + index);
542

543
544
545
    if (interface_color_pair[TITLE_BAR].bright)
	wattron(bottomwin, A_BOLD);
    wattron(bottomwin, interface_color_pair[TITLE_BAR].pairnum);
546
547
548
549
550
551
552

    blank_statusbar();

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

553
554
    expanded = display_string(curranswer, page_start, COLS - start_col -
	1, FALSE);
555
556
557
    waddstr(bottomwin, expanded);
    free(expanded);

558
559
    wattroff(bottomwin, A_BOLD);
    wattroff(bottomwin, interface_color_pair[TITLE_BAR].pairnum);
560
    statusbar_pww = statusbar_xplustabs();
561
    reset_statusbar_cursor();
562
    wnoutrefresh(bottomwin);
563
564
}

565
566
567
/* Update the statusbar line /if/ the placewewant changes page. */
void update_the_bar(void)
{
568
    size_t start_col = strlenpt(prompt) + 2;
569
570
571
572
    size_t was_pww = statusbar_pww;

    statusbar_pww = statusbar_xplustabs();

573
574
    if (get_statusbar_page_start(start_col, start_col + statusbar_pww) !=
		get_statusbar_page_start(start_col, start_col + was_pww))
575
576
577
	update_statusbar_line(answer, statusbar_x);
}

578
/* Get a string of input at the statusbar prompt. */
579
functionptrtype get_prompt_string(int *actual, bool allow_tabs,
580
581
#ifndef DISABLE_TABCOMP
	bool allow_files,
582
	bool *list,
583
584
#endif
	const char *curranswer,
585
#ifndef DISABLE_HISTORIES
586
587
	filestruct **history_list,
#endif
588
	void (*refresh_func)(void))
589
{
590
    int kbinput = ERR;
591
    bool ran_func, finished;
592
    functionptrtype func;
593
594
595
596
#ifndef DISABLE_TABCOMP
    bool tabbed = FALSE;
	/* Whether we've pressed Tab. */
#endif
597
#ifndef DISABLE_HISTORIES
598
599
600
601
602
603
604
605
606
607
608
609
    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
610
#endif /* !DISABLE_HISTORIES */
611
612
613

    answer = mallocstrcpy(answer, curranswer);

614
615
    if (statusbar_x > strlen(answer)) {
	statusbar_x = strlen(answer);
616
617
	statusbar_pww = statusbar_xplustabs();
    }
618

619
#ifdef DEBUG
620
    fprintf(stderr, "get_prompt_string: answer = \"%s\", statusbar_x = %lu\n", answer, (unsigned long) statusbar_x);
621
#endif
622

623
    update_statusbar_line(answer, statusbar_x);
624
625
626
627
628
629
630

    /* Refresh the edit window and the statusbar before getting
     * input. */
    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
631
632
633
634
     * 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. */
635
    while (TRUE) {
636
	kbinput = do_statusbar_input(&ran_func, &finished, refresh_func);
637
638
	assert(statusbar_x <= strlen(answer));

639
#ifndef NANO_TINY
640
641
642
643
644
	if (kbinput == KEY_WINCH) {
	    refresh_func();
	    update_statusbar_line(answer, statusbar_x);
	    continue;
	}
645
#endif
646
	func = func_from_key(&kbinput);
647

648
	if (func == do_cancel || func == do_enter)
649
	    break;
650

651
#ifndef DISABLE_TABCOMP
652
	if (func != do_tab)
653
654
	    tabbed = FALSE;

655
	if (func == do_tab) {
656
#ifndef DISABLE_HISTORIES
657
658
659
	    if (history_list != NULL) {
		if (last_kbinput != sc_seq_or(do_tab, NANO_CONTROL_I))
		    complete_len = strlen(answer);
660

661
662
		if (complete_len > 0) {
		    answer = mallocstrcpy(answer,
663
				get_history_completion(history_list,
664
665
666
667
					answer, complete_len));
		    statusbar_x = strlen(answer);
		}
	    } else
668
#endif
669
670
671
	    if (allow_tabs)
		answer = input_tab(answer, allow_files, &statusbar_x,
				   &tabbed, refresh_func, list);
672

673
	    update_statusbar_line(answer, statusbar_x);
674
	} else
675
#endif /* !DISABLE_TABCOMP */
676
#ifndef DISABLE_HISTORIES
677
	if (func == get_history_older_void) {
678
679
680
681
682
683
684
685
686
687
688
689
	    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);
		}
690

691
		update_statusbar_line(answer, statusbar_x);
692

693
694
695
696
		/* 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. */
697
		finished = FALSE;
698
	    }
699
	} else if (func == get_history_newer_void) {
700
701
702
703
704
705
706
	    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);
		}
707

708
709
710
711
		/* 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 &&
712
713
714
715
			*answer == '\0' && magichistory != NULL) {
		    answer = mallocstrcpy(answer, magichistory);
		    statusbar_x = strlen(answer);
		}
716

717
		update_statusbar_line(answer, statusbar_x);
718

719
720
721
722
723
724
		/* 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;
	    }
725
	} else
726
#endif /* !DISABLE_HISTORIES */
727
	if (func == do_help_void) {
728
	    update_statusbar_line(answer, statusbar_x);
729

730
731
732
733
734
735
	    /* 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;
736
737
	}

738
739
	/* If we have a shortcut with an associated function, break out if
	 * we're finished after running or trying to run the function. */
740
741
742
	if (finished)
	    break;

743
#if !defined(DISABLE_HISTORIES) && !defined(DISABLE_TABCOMP)
744
745
	last_kbinput = kbinput;
#endif
746
	reset_statusbar_cursor();
747
	wnoutrefresh(bottomwin);
748
    }
749

750
#ifndef DISABLE_HISTORIES
751
752
    /* Set the current position in the history list to the bottom,
     * and free magichistory if we need to. */
753
754
755
    if (history_list != NULL) {
	history_reset(*history_list);

756
	free(magichistory);
757
758
759
    }
#endif

760
761
762
763
764
    /* 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;
765
    }
766

767
    *actual = kbinput;
768
769

    return func;
770
771
}

772
773
774
775
/* 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.
776
777
778
 * 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.
779
780
 *
 * The allow_tabs parameter indicates whether we should allow tabs to be
781
 * interpreted.  The allow_files parameter indicates whether we should
782
 * allow all files (as opposed to just directories) to be tab completed. */
783
784
785
786
int do_prompt(bool allow_tabs,
#ifndef DISABLE_TABCOMP
	bool allow_files,
#endif
787
	int menu, const char *curranswer,
788
#ifndef DISABLE_HISTORIES
789
790
	filestruct **history_list,
#endif
791
	void (*refresh_func)(void), const char *msg, ...)
792
793
794
{
    va_list ap;
    int retval;
795
    functionptrtype func;
796
797
798
799
#ifndef DISABLE_TABCOMP
    bool list = FALSE;
#endif

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

802
    bottombars(menu);
803
804
805
806
807
808

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

809
    func = get_prompt_string(&retval, allow_tabs,
810
811
#ifndef DISABLE_TABCOMP
	allow_files,
812
	&list,
813
814
#endif
	curranswer,
815
#ifndef DISABLE_HISTORIES
816
	history_list,
817
#endif
818
	refresh_func);
819

820
821
822
    free(prompt);
    prompt = NULL;

823
824
    /* We're done with the prompt, so save the statusbar cursor
     * position. */
825
    old_statusbar_x = statusbar_x;
826
    old_pww = statusbar_pww;
827

828
829
    /* If we left the prompt via Cancel or Enter, set the return value
     * properly. */
830
    if (func == do_cancel)
831
	retval = -1;
832
    else if (func == do_enter)
833
	retval = (*answer == '\0') ? -2 : 0;
834

835
836
837
838
839
840
841
842
843
844
845
846
    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)
847
	refresh_func();
848
849
850
851
852
#endif

    return retval;
}

853
854
855
/* 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. */
856
int do_yesno_prompt(bool all, const char *msg)
857
858
859
860
861
{
    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! */
862
    int oldmenu = currmenu;
863
864
865
866
867
868

    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
869
870
     * displayed in the shortcuts. */
    /* TRANSLATORS: For the next three strings, if possible, specify
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
871
872
     * the single-byte shortcuts for both your language and English.
     * For example, in French: "OoYy" for "Oui". */
873
874
875
876
    yesstr = _("Yy");
    nostr = _("Nn");
    allstr = _("Aa");

877
878
879
880
881
882
883
    do {
	int kbinput;
	functionptrtype func;
#ifndef DISABLE_MOUSE
	int mouse_x, mouse_y;
#endif

884
885
	if (!ISSET(NO_HELP)) {
	    char shortstr[3];
886
		/* Temporary string for (translated) " Y", " N" and " A". */
887

888
889
	    if (COLS < 32)
		width = COLS / 2;
890

891
892
	    /* Clear the shortcut list from the bottom of the screen. */
	    blank_bottombars();
893

894
895
896
897
	    /* Now show the ones for "Yes", "No", "Cancel" and maybe "All". */
	    sprintf(shortstr, " %c", yesstr[0]);
	    wmove(bottomwin, 1, 0);
	    onekey(shortstr, _("Yes"), width);
898

899
900
	    if (all) {
		shortstr[1] = allstr[0];
901
		wmove(bottomwin, 1, width);
902
903
		onekey(shortstr, _("All"), width);
	    }
904

905
	    shortstr[1] = nostr[0];
906
	    wmove(bottomwin, 2, 0);
907
	    onekey(shortstr, _("No"), width);
908

909
	    wmove(bottomwin, 2, width);
910
911
	    onekey("^C", _("Cancel"), width);
	}
912

913
914
915
	if (interface_color_pair[TITLE_BAR].bright)
	    wattron(bottomwin, A_BOLD);
	wattron(bottomwin, interface_color_pair[TITLE_BAR].pairnum);
916

917
918
	blank_statusbar();
	mvwaddnstr(bottomwin, 0, 0, msg, actual_x(msg, COLS - 1));
919

920
921
	wattroff(bottomwin, A_BOLD);
	wattroff(bottomwin, interface_color_pair[TITLE_BAR].pairnum);
922

923
924
925
	/* Refresh edit window and statusbar before getting input. */
	wnoutrefresh(edit);
	wnoutrefresh(bottomwin);
926

927
	currmenu = MYESNO;
928
	kbinput = get_kbinput(bottomwin);
929
930
931
932
933
934

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

935
	func = func_from_key(&kbinput);
936

937
	if (func == do_cancel)
938
	    ok = -1;
939
#ifndef DISABLE_MOUSE
940
	else if (kbinput == KEY_MOUSE) {
941
942
		/* We can click on the Yes/No/All shortcut list to
		 * select an answer. */
943
		if (get_mouseinput(&mouse_x, &mouse_y, FALSE) == 0 &&
944
			wmouse_trafo(bottomwin, &mouse_y, &mouse_x,
945
946
			FALSE) && mouse_x < (width * 2) &&
			mouse_y > 0) {
947
		    int x = mouse_x / width;
948
949
950
			/* Calculate the x-coordinate relative to the
			 * two columns of the Yes/No/All shortcuts in
			 * bottomwin. */
951
		    int y = mouse_y - 1;
952
953
			/* Calculate the y-coordinate relative to the
			 * beginning of the Yes/No/All shortcuts in
954
			 * bottomwin, i.e. with the sizes of topwin,
955
956
957
			 * edit, and the first line of bottomwin
			 * subtracted out. */

958
		    assert(0 <= x && x <= 1 && 0 <= y && y <= 1);
959

960
961
962
		    /* x == 0 means they clicked Yes or No.  y == 0
		     * means Yes or All. */
		    ok = -2 * x * y + x - y + 1;
963

964
965
966
		    if (ok == 2 && !all)
			ok = -2;
		}
967
	}
968
#endif /* !DISABLE_MOUSE */
969
	else if (func == total_refresh) {
970
971
972
	    total_redraw();
	    continue;
	} else {
973
974
975
976
977
978
979
980
		/* Look for the kbinput in the Yes, No and (optionally)
		 * 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;
981
982
983
	}
    } while (ok == -2);

984
    currmenu = oldmenu;
985
986
    return ok;
}