forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
curs_lib.c
1210 lines (1098 loc) · 28.8 KB
/
curs_lib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file
* GUI miscellaneous curses (window drawing) routines
*
* @authors
* Copyright (C) 1996-2002,2010,2012-2013 Michael R. Elkins <[email protected]>
* Copyright (C) 2004 g10 Code GmbH
*
* @copyright
* 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 the Free Software
* Foundation, either version 2 of the License, or (at your option) 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, see <http://www.gnu.org/licenses/>.
*/
/**
* @page curs_lib GUI miscellaneous curses (window drawing) routines
*
* GUI miscellaneous curses (window drawing) routines
*/
#include "config.h"
#include <stddef.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <langinfo.h>
#include <limits.h>
#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <wchar.h>
#include "mutt/mutt.h"
#include "config/lib.h"
#include "email/lib.h"
#include "mutt.h"
#include "curs_lib.h"
#include "browser.h"
#include "context.h"
#include "enter_state.h"
#include "globals.h"
#include "mailbox.h"
#include "mutt_curses.h"
#include "mutt_logging.h"
#include "mutt_menu.h"
#include "mutt_window.h"
#include "muttlib.h"
#include "opcodes.h"
#include "options.h"
#include "pager.h"
#include "protos.h"
#include "sidebar.h"
#ifdef HAVE_ISWBLANK
#include <wctype.h>
#endif
#ifdef USE_INOTIFY
#include "monitor.h"
#endif
/* These Config Variables are only used in curs_lib.c */
bool C_MetaKey; ///< Config: Interpret 'ALT-x' as 'ESC-x'
/* not possible to unget more than one char under some curses libs, and it
* is impossible to unget function keys in SLang, so roll our own input
* buffering routines.
*/
/* These are used for macros and exec/push commands.
* They can be temporarily ignored by setting OptIgnoreMacroEvents
*/
static size_t MacroBufferCount = 0;
static size_t MacroBufferLen = 0;
static struct Event *MacroEvents;
/* These are used in all other "normal" situations, and are not
* ignored when setting OptIgnoreMacroEvents
*/
static size_t UngetCount = 0;
static size_t UngetLen = 0;
static struct Event *UngetKeyEvents;
int MuttGetchTimeout = -1;
/**
* mutt_refresh - Force a refresh of the screen
*/
void mutt_refresh(void)
{
/* don't refresh when we are waiting for a child. */
if (OptKeepQuiet)
return;
/* don't refresh in the middle of macros unless necessary */
if (MacroBufferCount && !OptForceRefresh && !OptIgnoreMacroEvents)
return;
/* else */
refresh();
}
/**
* mutt_need_hard_redraw - Force a hard refresh
*
* Make sure that the next refresh does a full refresh. This could be
* optimized by not doing it at all if DISPLAY is set as this might indicate
* that a GUI based pinentry was used. Having an option to customize this is
* of course the NeoMutt way.
*/
void mutt_need_hard_redraw(void)
{
keypad(stdscr, true);
clearok(stdscr, true);
mutt_menu_set_current_redraw_full();
}
/**
* mutt_getch_timeout - Set the getch() timeout
* @param delay Timeout delay in ms
*
* delay is just like for timeout() or poll(): the number of milliseconds
* mutt_getch() should block for input.
* * delay == 0 means mutt_getch() is non-blocking.
* * delay < 0 means mutt_getch is blocking.
*/
void mutt_getch_timeout(int delay)
{
MuttGetchTimeout = delay;
timeout(delay);
}
#ifdef USE_INOTIFY
/**
* mutt_monitor_getch - Get a character and poll the filesystem monitor
* @retval num Character pressed
* @retval ERR Timeout
*/
static int mutt_monitor_getch(void)
{
/* ncurses has its own internal buffer, so before we perform a poll,
* we need to make sure there isn't a character waiting */
timeout(0);
int ch = getch();
timeout(MuttGetchTimeout);
if (ch == ERR)
{
if (mutt_monitor_poll() != 0)
ch = ERR;
else
ch = getch();
}
return ch;
}
#endif /* USE_INOTIFY */
/**
* mutt_getch - Read a character from the input buffer
* @retval obj Event to process
*
* The priority for reading events is:
* 1. UngetKeyEvents buffer
* 2. MacroEvents buffer
* 3. Keyboard
*
* This function can return:
* - Error `{ -1, OP_NULL }`
* - Timeout `{ -2, OP_NULL }`
*/
struct Event mutt_getch(void)
{
int ch;
struct Event err = { -1, OP_NULL }, ret;
struct Event timeout = { -2, OP_NULL };
if (UngetCount)
return UngetKeyEvents[--UngetCount];
if (!OptIgnoreMacroEvents && MacroBufferCount)
return MacroEvents[--MacroBufferCount];
SigInt = 0;
mutt_sig_allow_interrupt(1);
#ifdef KEY_RESIZE
/* ncurses 4.2 sends this when the screen is resized */
ch = KEY_RESIZE;
while (ch == KEY_RESIZE)
#endif /* KEY_RESIZE */
#ifdef USE_INOTIFY
ch = mutt_monitor_getch();
#else
ch = getch();
#endif /* USE_INOTIFY */
mutt_sig_allow_interrupt(0);
if (SigInt)
{
mutt_query_exit();
return err;
}
/* either timeout, a sigwinch (if timeout is set), or the terminal
* has been lost */
if (ch == ERR)
{
if (!isatty(0))
mutt_exit(1);
return timeout;
}
if ((ch & 0x80) && C_MetaKey)
{
/* send ALT-x as ESC-x */
ch &= ~0x80;
mutt_unget_event(ch, 0);
ret.ch = '\033';
ret.op = 0;
return ret;
}
ret.ch = ch;
ret.op = 0;
return (ch == ctrl('G')) ? err : ret;
}
/**
* mutt_get_field_full - Ask the user for a string
* @param[in] field Prompt
* @param[in] buf Buffer for the result
* @param[in] buflen Length of buffer
* @param[in] complete Flags, see #CompletionFlags
* @param[in] multiple Allow multiple selections
* @param[out] files List of files selected
* @param[out] numfiles Number of files selected
* @retval 1 Redraw the screen and call the function again
* @retval 0 Selection made
* @retval -1 Aborted
*/
int mutt_get_field_full(const char *field, char *buf, size_t buflen, CompletionFlags complete,
bool multiple, char ***files, int *numfiles)
{
int ret;
int x;
struct EnterState *es = mutt_enter_state_new();
do
{
if (SigWinch)
{
SigWinch = 0;
mutt_resize_screen();
clearok(stdscr, TRUE);
mutt_menu_current_redraw();
}
mutt_window_clearline(MuttMessageWindow, 0);
SETCOLOR(MT_COLOR_PROMPT);
addstr(field);
NORMAL_COLOR;
mutt_refresh();
mutt_window_getxy(MuttMessageWindow, &x, NULL);
ret = mutt_enter_string_full(buf, buflen, x, complete, multiple, files, numfiles, es);
} while (ret == 1);
mutt_window_clearline(MuttMessageWindow, 0);
mutt_enter_state_free(&es);
return ret;
}
/**
* mutt_get_field_unbuffered - Ask the user for a string (ignoring macro buffer)
* @param msg Prompt
* @param buf Buffer for the result
* @param buflen Length of buffer
* @param flags Flags, see #CompletionFlags
* @retval 1 Redraw the screen and call the function again
* @retval 0 Selection made
* @retval -1 Aborted
*/
int mutt_get_field_unbuffered(const char *msg, char *buf, size_t buflen, CompletionFlags flags)
{
int rc;
OptIgnoreMacroEvents = true;
rc = mutt_get_field(msg, buf, buflen, flags);
OptIgnoreMacroEvents = false;
return rc;
}
/**
* mutt_edit_file - Let the user edit a file
* @param editor User's editor config
* @param file File to edit
*/
void mutt_edit_file(const char *editor, const char *file)
{
char cmd[STR_COMMAND];
mutt_endwin();
mutt_file_expand_fmt_quote(cmd, sizeof(cmd), editor, file);
if (mutt_system(cmd) != 0)
{
mutt_error(_("Error running \"%s\""), cmd);
}
/* the terminal may have been resized while the editor owned it */
mutt_resize_screen();
keypad(stdscr, true);
clearok(stdscr, true);
}
/**
* mutt_yesorno - Ask the user a Yes/No question
* @param msg Prompt
* @param def Default answer, see #QuadOption
* @retval num Selection made, see #QuadOption
*/
enum QuadOption mutt_yesorno(const char *msg, enum QuadOption def)
{
struct Event ch;
char *yes = _("yes");
char *no = _("no");
char *answer_string = NULL;
int answer_string_wid, msg_wid;
size_t trunc_msg_len;
bool redraw = true;
int prompt_lines = 1;
char *expr = NULL;
regex_t reyes;
regex_t reno;
char answer[2];
answer[1] = '\0';
bool reyes_ok = (expr = nl_langinfo(YESEXPR)) && (expr[0] == '^') &&
(REGCOMP(&reyes, expr, REG_NOSUB) == 0);
bool reno_ok = (expr = nl_langinfo(NOEXPR)) && (expr[0] == '^') &&
(REGCOMP(&reno, expr, REG_NOSUB) == 0);
/* In order to prevent the default answer to the question to wrapped
* around the screen in the even the question is wider than the screen,
* ensure there is enough room for the answer and truncate the question
* to fit. */
safe_asprintf(&answer_string, " ([%s]/%s): ", (def == MUTT_YES) ? yes : no,
(def == MUTT_YES) ? no : yes);
answer_string_wid = mutt_strwidth(answer_string);
msg_wid = mutt_strwidth(msg);
while (true)
{
if (redraw || SigWinch)
{
redraw = false;
if (SigWinch)
{
SigWinch = 0;
mutt_resize_screen();
clearok(stdscr, TRUE);
mutt_menu_current_redraw();
}
if (MuttMessageWindow->cols)
{
prompt_lines = (msg_wid + answer_string_wid + MuttMessageWindow->cols - 1) /
MuttMessageWindow->cols;
prompt_lines = MAX(1, MIN(3, prompt_lines));
}
if (prompt_lines != MuttMessageWindow->rows)
{
mutt_window_reflow_message_rows(prompt_lines);
mutt_menu_current_redraw();
}
/* maxlen here is sort of arbitrary, so pick a reasonable upper bound */
trunc_msg_len = mutt_wstr_trunc(
msg, 4 * prompt_lines * MuttMessageWindow->cols,
prompt_lines * MuttMessageWindow->cols - answer_string_wid, NULL);
mutt_window_move(MuttMessageWindow, 0, 0);
SETCOLOR(MT_COLOR_PROMPT);
addnstr(msg, trunc_msg_len);
addstr(answer_string);
NORMAL_COLOR;
mutt_window_clrtoeol(MuttMessageWindow);
}
mutt_refresh();
/* SigWinch is not processed unless timeout is set */
mutt_getch_timeout(30 * 1000);
ch = mutt_getch();
mutt_getch_timeout(-1);
if (ch.ch == -2)
continue;
if (CI_is_return(ch.ch))
break;
if (ch.ch < 0)
{
def = MUTT_ABORT;
break;
}
answer[0] = ch.ch;
if (reyes_ok ? (regexec(&reyes, answer, 0, 0, 0) == 0) : (tolower(ch.ch) == 'y'))
{
def = MUTT_YES;
break;
}
else if (reno_ok ? (regexec(&reno, answer, 0, 0, 0) == 0) : (tolower(ch.ch) == 'n'))
{
def = MUTT_NO;
break;
}
else
{
BEEP();
}
}
FREE(&answer_string);
if (reyes_ok)
regfree(&reyes);
if (reno_ok)
regfree(&reno);
if (MuttMessageWindow->rows != 1)
{
mutt_window_reflow_message_rows(1);
mutt_menu_current_redraw();
}
else
mutt_window_clearline(MuttMessageWindow, 0);
if (def != MUTT_ABORT)
{
addstr((char *) ((def == MUTT_YES) ? yes : no));
mutt_refresh();
}
else
{
/* when the users cancels with ^G, clear the message stored with
* mutt_message() so it isn't displayed when the screen is refreshed. */
mutt_clear_error();
}
return def;
}
/**
* mutt_query_exit - Ask the user if they want to leave NeoMutt
*
* This function is called when the user presses the abort key.
*/
void mutt_query_exit(void)
{
mutt_flushinp();
curs_set(1);
if (C_Timeout)
mutt_getch_timeout(-1); /* restore blocking operation */
if (mutt_yesorno(_("Exit NeoMutt?"), MUTT_YES) == MUTT_YES)
{
mutt_exit(1);
}
mutt_clear_error();
mutt_curs_set(-1);
SigInt = 0;
}
/**
* mutt_show_error - Show the user an error message
*/
void mutt_show_error(void)
{
if (OptKeepQuiet || !ErrorBufMessage)
return;
SETCOLOR(OptMsgErr ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
mutt_window_mvaddstr(MuttMessageWindow, 0, 0, ErrorBuf);
NORMAL_COLOR;
mutt_window_clrtoeol(MuttMessageWindow);
}
/**
* mutt_endwin - Shutdown curses/slang
*/
void mutt_endwin(void)
{
if (OptNoCurses)
return;
int e = errno;
/* at least in some situations (screen + xterm under SuSE11/12) endwin()
* doesn't properly flush the screen without an explicit call. */
mutt_refresh();
endwin();
errno = e;
}
/**
* mutt_perror_debug - Show the user an 'errno' message
* @param s Additional text to show
*/
void mutt_perror_debug(const char *s)
{
char *p = strerror(errno);
mutt_debug(LL_DEBUG1, "%s: %s (errno = %d)\n", s, p ? p : "unknown error", errno);
mutt_error("%s: %s (errno = %d)", s, p ? p : _("unknown error"), errno);
}
/**
* mutt_any_key_to_continue - Prompt the user to 'press any key' and wait
* @param s Message prompt
* @retval num Key pressed
* @retval EOF Error, or prompt aborted
*/
int mutt_any_key_to_continue(const char *s)
{
struct termios t;
struct termios old;
int f, ch;
f = open("/dev/tty", O_RDONLY);
if (f < 0)
return EOF;
tcgetattr(f, &t);
memcpy((void *) &old, (void *) &t, sizeof(struct termios)); /* save original state */
t.c_lflag &= ~(ICANON | ECHO);
t.c_cc[VMIN] = 1;
t.c_cc[VTIME] = 0;
tcsetattr(f, TCSADRAIN, &t);
fflush(stdout);
if (s)
fputs(s, stdout);
else
fputs(_("Press any key to continue..."), stdout);
fflush(stdout);
ch = fgetc(stdin);
fflush(stdin);
tcsetattr(f, TCSADRAIN, &old);
close(f);
fputs("\r\n", stdout);
mutt_clear_error();
return (ch >= 0) ? ch : EOF;
}
/**
* mutt_do_pager - Display some page-able text to the user
* @param banner Message for status bar
* @param tempfile File to display
* @param do_color Flags, see #PagerFlags
* @param info Info about current mailbox (OPTIONAL)
* @retval 0 Success
* @retval -1 Error
*/
int mutt_do_pager(const char *banner, const char *tempfile, PagerFlags do_color,
struct Pager *info)
{
int rc;
if (!C_Pager || (mutt_str_strcmp(C_Pager, "builtin") == 0))
rc = mutt_pager(banner, tempfile, do_color, info);
else
{
char cmd[256];
mutt_endwin();
mutt_file_expand_fmt_quote(cmd, sizeof(cmd), C_Pager, tempfile);
if (mutt_system(cmd) == -1)
{
mutt_error(_("Error running \"%s\""), cmd);
rc = -1;
}
else
rc = 0;
mutt_file_unlink(tempfile);
}
return rc;
}
/**
* mutt_enter_fname_full - Ask the user to select a file
* @param[in] prompt Prompt
* @param[in] buf Buffer for the result
* @param[in] buflen Length of the buffer
* @param[in] mailbox If true, select mailboxes
* @param[in] multiple Allow multiple selections
* @param[out] files List of files selected
* @param[out] numfiles Number of files selected
* @param[in] flags Flags, see #SelectFileFlags
* @retval 0 Success
* @retval -1 Error
*/
int mutt_enter_fname_full(const char *prompt, char *buf, size_t buflen, bool mailbox,
bool multiple, char ***files, int *numfiles, SelectFileFlags flags)
{
struct Event ch;
SETCOLOR(MT_COLOR_PROMPT);
mutt_window_mvaddstr(MuttMessageWindow, 0, 0, (char *) prompt);
addstr(_(" ('?' for list): "));
NORMAL_COLOR;
if (buf[0] != '\0')
addstr(buf);
mutt_window_clrtoeol(MuttMessageWindow);
mutt_refresh();
do
{
ch = mutt_getch();
} while (ch.ch == -2);
if (ch.ch < 0)
{
mutt_window_clearline(MuttMessageWindow, 0);
return -1;
}
else if (ch.ch == '?')
{
mutt_refresh();
buf[0] = '\0';
if (!flags)
flags = MUTT_SEL_FOLDER;
if (multiple)
flags |= MUTT_SEL_MULTI;
if (mailbox)
flags |= MUTT_SEL_MAILBOX;
mutt_select_file(buf, buflen, flags, files, numfiles);
}
else
{
char *pc = mutt_mem_malloc(mutt_str_strlen(prompt) + 3);
sprintf(pc, "%s: ", prompt);
mutt_unget_event(ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
if (mutt_get_field_full(pc, buf, buflen, (mailbox ? MUTT_EFILE : MUTT_FILE) | MUTT_CLEAR,
multiple, files, numfiles) != 0)
{
buf[0] = '\0';
}
FREE(&pc);
}
return 0;
}
/**
* mutt_unget_event - Return a keystroke to the input buffer
* @param ch Key press
* @param op Operation, e.g. OP_DELETE
*
* This puts events into the `UngetKeyEvents` buffer
*/
void mutt_unget_event(int ch, int op)
{
struct Event tmp;
tmp.ch = ch;
tmp.op = op;
if (UngetCount >= UngetLen)
mutt_mem_realloc(&UngetKeyEvents, (UngetLen += 16) * sizeof(struct Event));
UngetKeyEvents[UngetCount++] = tmp;
}
/**
* mutt_unget_string - Return a string to the input buffer
* @param s String to return
*
* This puts events into the `UngetKeyEvents` buffer
*/
void mutt_unget_string(const char *s)
{
const char *p = s + mutt_str_strlen(s) - 1;
while (p >= s)
{
mutt_unget_event((unsigned char) *p--, 0);
}
}
/**
* mutt_push_macro_event - Add the character/operation to the macro buffer
* @param ch Character to add
* @param op Operation to add
*
* Adds the ch/op to the macro buffer.
* This should be used for macros, push, and exec commands only.
*/
void mutt_push_macro_event(int ch, int op)
{
struct Event tmp;
tmp.ch = ch;
tmp.op = op;
if (MacroBufferCount >= MacroBufferLen)
mutt_mem_realloc(&MacroEvents, (MacroBufferLen += 128) * sizeof(struct Event));
MacroEvents[MacroBufferCount++] = tmp;
}
/**
* mutt_flush_macro_to_endcond - Drop a macro from the input buffer
*
* All the macro text is deleted until an OP_END_COND command,
* or the buffer is empty.
*/
void mutt_flush_macro_to_endcond(void)
{
UngetCount = 0;
while (MacroBufferCount > 0)
{
if (MacroEvents[--MacroBufferCount].op == OP_END_COND)
return;
}
}
/**
* mutt_flush_unget_to_endcond - Clear entries from UngetKeyEvents
*
* Normally, OP_END_COND should only be in the MacroEvent buffer.
* km_error_key() (ab)uses OP_END_COND as a barrier in the unget buffer, and
* calls this function to flush.
*/
void mutt_flush_unget_to_endcond(void)
{
while (UngetCount > 0)
{
if (UngetKeyEvents[--UngetCount].op == OP_END_COND)
return;
}
}
/**
* mutt_flushinp - Empty all the keyboard buffers
*/
void mutt_flushinp(void)
{
UngetCount = 0;
MacroBufferCount = 0;
flushinp();
}
#if (defined(USE_SLANG_CURSES) || defined(HAVE_CURS_SET))
/**
* mutt_curs_set - Set the cursor position
* @param cursor
* * -1: restore the value of the last call
* * 0: make the cursor invisible
* * 1: make the cursor visible
*/
void mutt_curs_set(int cursor)
{
static int SavedCursor = 1;
if (cursor < 0)
cursor = SavedCursor;
else
SavedCursor = cursor;
if (curs_set(cursor) == ERR)
{
if (cursor == 1) /* cnorm */
curs_set(2); /* cvvis */
}
}
#endif
/**
* mutt_multi_choice - Offer the user a multiple choice question
* @param prompt Message prompt
* @param letters Allowable selection keys
* @retval >=0 0-based user selection
* @retval -1 Selection aborted
*/
int mutt_multi_choice(const char *prompt, const char *letters)
{
struct Event ch;
int choice;
bool redraw = true;
int prompt_lines = 1;
char *p = NULL;
while (true)
{
if (redraw || SigWinch)
{
redraw = false;
if (SigWinch)
{
SigWinch = 0;
mutt_resize_screen();
clearok(stdscr, TRUE);
mutt_menu_current_redraw();
}
if (MuttMessageWindow->cols)
{
prompt_lines = (mutt_strwidth(prompt) + MuttMessageWindow->cols - 1) /
MuttMessageWindow->cols;
prompt_lines = MAX(1, MIN(3, prompt_lines));
}
if (prompt_lines != MuttMessageWindow->rows)
{
mutt_window_reflow_message_rows(prompt_lines);
mutt_menu_current_redraw();
}
SETCOLOR(MT_COLOR_PROMPT);
mutt_window_mvaddstr(MuttMessageWindow, 0, 0, prompt);
NORMAL_COLOR;
mutt_window_clrtoeol(MuttMessageWindow);
}
mutt_refresh();
/* SigWinch is not processed unless timeout is set */
mutt_getch_timeout(30 * 1000);
ch = mutt_getch();
mutt_getch_timeout(-1);
if (ch.ch == -2)
continue;
/* (ch.ch == 0) is technically possible. Treat the same as < 0 (abort) */
if ((ch.ch <= 0) || CI_is_return(ch.ch))
{
choice = -1;
break;
}
else
{
p = strchr(letters, ch.ch);
if (p)
{
choice = p - letters + 1;
break;
}
else if ((ch.ch <= '9') && (ch.ch > '0'))
{
choice = ch.ch - '0';
if (choice <= mutt_str_strlen(letters))
break;
}
}
BEEP();
}
if (MuttMessageWindow->rows != 1)
{
mutt_window_reflow_message_rows(1);
mutt_menu_current_redraw();
}
else
mutt_window_clearline(MuttMessageWindow, 0);
mutt_refresh();
return choice;
}
/**
* mutt_addwch - addwch would be provided by an up-to-date curses library
* @param wc Wide char to display
* @retval 0 Success
* @retval -1 Error
*/
int mutt_addwch(wchar_t wc)
{
char buf[MB_LEN_MAX * 2];
mbstate_t mbstate;
size_t n1, n2;
memset(&mbstate, 0, sizeof(mbstate));
if (((n1 = wcrtomb(buf, wc, &mbstate)) == (size_t)(-1)) ||
((n2 = wcrtomb(buf + n1, 0, &mbstate)) == (size_t)(-1)))
{
return -1; /* ERR */
}
else
{
return addstr(buf);
}
}
/**
* mutt_simple_format - Format a string, like snprintf()
* @param[out] buf Buffer in which to save string
* @param[in] buflen Buffer length
* @param[in] min_width Minimum width
* @param[in] max_width Maximum width
* @param[in] justify Justification, e.g. #FMT_RIGHT
* @param[in] pad_char Padding character
* @param[in] s String to format
* @param[in] n Number of bytes of string to format
* @param[in] arboreal If true, string contains graphical tree characters
*
* This formats a string, a bit like snprintf(buf, buflen, "%-*.*s",
* min_width, max_width, s), except that the widths refer to the number of
* character cells when printed.
*/
void mutt_simple_format(char *buf, size_t buflen, int min_width, int max_width,
int justify, char pad_char, const char *s, size_t n, int arboreal)
{
wchar_t wc;
int w;
size_t k, k2;
char scratch[MB_LEN_MAX];
mbstate_t mbstate1, mbstate2;
int escaped = 0;
memset(&mbstate1, 0, sizeof(mbstate1));
memset(&mbstate2, 0, sizeof(mbstate2));
buflen--;
char *p = buf;
for (; n && (k = mbrtowc(&wc, s, n, &mbstate1)); s += k, n -= k)
{
if ((k == (size_t)(-1)) || (k == (size_t)(-2)))
{
if ((k == (size_t)(-1)) && (errno == EILSEQ))
memset(&mbstate1, 0, sizeof(mbstate1));
k = (k == (size_t)(-1)) ? 1 : n;
wc = ReplacementChar;
}
if (escaped)
{
escaped = 0;
w = 0;
}
else if (arboreal && (wc == MUTT_SPECIAL_INDEX))
{
escaped = 1;
w = 0;
}
else if (arboreal && (wc < MUTT_TREE_MAX))
{
w = 1; /* hack */
}
else
{
#ifdef HAVE_ISWBLANK
if (iswblank(wc))
wc = ' ';
else
#endif
if (!IsWPrint(wc))
wc = '?';
w = wcwidth(wc);
}
if (w >= 0)
{
if ((w > max_width) || ((k2 = wcrtomb(scratch, wc, &mbstate2)) > buflen))
continue;
min_width -= w;
max_width -= w;
strncpy(p, scratch, k2);
p += k2;
buflen -= k2;
}
}
w = ((int) buflen < min_width) ? buflen : min_width;
if (w <= 0)
*p = '\0';
else if (justify == FMT_RIGHT) /* right justify */
{
p[w] = '\0';
while (--p >= buf)
p[w] = *p;
while (--w >= 0)
buf[w] = pad_char;
}
else if (justify == FMT_CENTER) /* center */
{
char *savedp = p;
int half = (w + 1) / 2; /* half of cushion space */
p[w] = '\0';
/* move str to center of buffer */
while (--p >= buf)
p[half] = *p;
/* fill rhs */
p = savedp + half;
while (--w >= half)
*p++ = pad_char;
/* fill lhs */
while (half--)
buf[half] = pad_char;