forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
curs_lib.c
1462 lines (1290 loc) · 33.9 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/>.
*/
#include "config.h"
#include <stddef.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <langinfo.h>
#ifdef ENABLE_NLS
#include <libintl.h>
#endif
#include <limits.h>
#include <regex.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <termios.h>
#include <unistd.h>
#include <wchar.h>
#include "mutt.h"
#include "enter_state.h"
#include "globals.h"
#include "lib/lib.h"
#include "mbyte.h"
#include "mutt_curses.h"
#include "mutt_menu.h"
#include "mutt_regex.h"
#include "opcodes.h"
#include "options.h"
#include "pager.h"
#include "protos.h"
#ifdef HAVE_ISWBLANK
#include <wctype.h>
#endif
#ifdef USE_NOTMUCH
#include "mutt_notmuch.h"
#endif
/* 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 OPT_IGNORE_MACRO_EVENTS
*/
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 OPT_IGNORE_MACRO_EVENTS
*/
static size_t UngetCount = 0;
static size_t UngetLen = 0;
static struct Event *UngetKeyEvents;
struct MuttWindow *MuttHelpWindow = NULL;
struct MuttWindow *MuttIndexWindow = NULL;
struct MuttWindow *MuttStatusWindow = NULL;
struct MuttWindow *MuttMessageWindow = NULL;
#ifdef USE_SIDEBAR
struct MuttWindow *MuttSidebarWindow = NULL;
#endif
static void reflow_message_window_rows(int mw_rows);
void mutt_refresh(void)
{
/* don't refresh when we are waiting for a child. */
if (option(OPT_KEEP_QUIET))
return;
/* don't refresh in the middle of macros unless necessary */
if (MacroBufferCount && !option(OPT_FORCE_REFRESH) && !option(OPT_IGNORE_MACRO_EVENTS))
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 Mutt way.
*/
void mutt_need_hard_redraw(void)
{
keypad(stdscr, true);
clearok(stdscr, true);
mutt_set_current_menu_redraw_full();
}
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 (!option(OPT_IGNORE_MACRO_EVENTS) && MacroBufferCount)
return MacroEvents[--MacroBufferCount];
SigInt = 0;
mutt_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 */
ch = getch();
mutt_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))
{
endwin();
exit(1);
}
return timeout;
}
if ((ch & 0x80) && option(OPT_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);
}
int _mutt_get_field(const char *field, char *buf, size_t buflen, int complete,
int multiple, char ***files, int *numfiles)
{
int ret;
int x;
struct EnterState *es = mutt_new_enter_state();
do
{
#if defined(USE_SLANG_CURSES) || defined(HAVE_RESIZETERM)
if (SigWinch)
{
SigWinch = 0;
mutt_resize_screen();
clearok(stdscr, TRUE);
mutt_current_menu_redraw();
}
#endif
mutt_window_clearline(MuttMessageWindow, 0);
SETCOLOR(MT_COLOR_PROMPT);
addstr((char *) field); /* cast to get around bad prototypes */
NORMAL_COLOR;
mutt_refresh();
mutt_window_getyx(MuttMessageWindow, NULL, &x);
ret = _mutt_enter_string(buf, buflen, x, complete, multiple, files, numfiles, es);
} while (ret == 1);
mutt_window_clearline(MuttMessageWindow, 0);
mutt_free_enter_state(&es);
return ret;
}
int mutt_get_field_unbuffered(char *msg, char *buf, size_t buflen, int flags)
{
int rc;
set_option(OPT_IGNORE_MACRO_EVENTS);
rc = mutt_get_field(msg, buf, buflen, flags);
unset_option(OPT_IGNORE_MACRO_EVENTS);
return rc;
}
void mutt_clear_error(void)
{
ErrorBuf[0] = 0;
if (!option(OPT_NO_CURSES))
mutt_window_clearline(MuttMessageWindow, 0);
}
void mutt_edit_file(const char *editor, const char *data)
{
char cmd[LONG_STRING];
mutt_endwin(NULL);
mutt_expand_file_fmt(cmd, sizeof(cmd), editor, data);
if (mutt_system(cmd))
{
mutt_error(_("Error running \"%s\"!"), cmd);
mutt_sleep(2);
}
#if defined(USE_SLANG_CURSES) || defined(HAVE_RESIZETERM)
/* the terminal may have been resized while the editor owned it */
mutt_resize_screen();
#endif
keypad(stdscr, true);
clearok(stdscr, true);
}
int mutt_yesorno(const char *msg, int 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;
int reyes_ok;
int reno_ok;
char answer[2];
answer[1] = '\0';
reyes_ok = (expr = nl_langinfo(YESEXPR)) && expr[0] == '^' &&
!REGCOMP(&reyes, expr, REG_NOSUB);
reno_ok = (expr = nl_langinfo(NOEXPR)) && expr[0] == '^' &&
!REGCOMP(&reno, expr, REG_NOSUB);
/*
* 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 defined(USE_SLANG_CURSES) || defined(HAVE_RESIZETERM)
if (SigWinch)
{
SigWinch = 0;
mutt_resize_screen();
clearok(stdscr, TRUE);
mutt_current_menu_redraw();
}
#endif
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)
{
reflow_message_window_rows(prompt_lines);
mutt_current_menu_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 */
timeout(30 * 1000);
ch = 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)
{
reflow_message_window_rows(1);
mutt_current_menu_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 Mutt
*
* This function is called when the user presses the abort key.
*/
void mutt_query_exit(void)
{
mutt_flushinp();
curs_set(1);
if (Timeout)
timeout(-1); /* restore blocking operation */
if (mutt_yesorno(_("Exit Mutt?"), MUTT_YES) == MUTT_YES)
{
endwin();
exit(1);
}
mutt_clear_error();
mutt_curs_set(-1);
SigInt = 0;
}
static void curses_message(int error, const char *fmt, va_list ap)
{
char scratch[LONG_STRING];
vsnprintf(scratch, sizeof(scratch), fmt, ap);
mutt_debug(1, "%s\n", scratch);
mutt_simple_format(ErrorBuf, sizeof(ErrorBuf), 0, MuttMessageWindow->cols,
FMT_LEFT, 0, scratch, sizeof(scratch), 0);
if (!option(OPT_KEEP_QUIET))
{
if (error)
BEEP();
SETCOLOR(error ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
mutt_window_mvaddstr(MuttMessageWindow, 0, 0, ErrorBuf);
NORMAL_COLOR;
mutt_window_clrtoeol(MuttMessageWindow);
mutt_refresh();
}
if (error)
set_option(OPT_MSG_ERR);
else
unset_option(OPT_MSG_ERR);
}
void mutt_curses_error(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
curses_message(1, fmt, ap);
va_end(ap);
}
void mutt_curses_message(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
curses_message(0, fmt, ap);
va_end(ap);
}
void mutt_progress_init(struct Progress *progress, const char *msg,
unsigned short flags, unsigned short inc, long size)
{
struct timeval tv = { 0, 0 };
if (!progress)
return;
if (option(OPT_NO_CURSES))
return;
memset(progress, 0, sizeof(struct Progress));
progress->inc = inc;
progress->flags = flags;
progress->msg = msg;
progress->size = size;
if (progress->size)
{
if (progress->flags & MUTT_PROGRESS_SIZE)
mutt_pretty_size(progress->sizestr, sizeof(progress->sizestr), progress->size);
else
snprintf(progress->sizestr, sizeof(progress->sizestr), "%ld", progress->size);
}
if (!inc)
{
if (size)
mutt_message("%s (%s)", msg, progress->sizestr);
else
mutt_message(msg);
return;
}
if (gettimeofday(&tv, NULL) < 0)
mutt_debug(1, "gettimeofday failed: %d\n", errno);
/* if timestamp is 0 no time-based suppression is done */
if (TimeInc)
progress->timestamp =
((unsigned int) tv.tv_sec * 1000) + (unsigned int) (tv.tv_usec / 1000);
mutt_progress_update(progress, 0, 0);
}
/**
* message_bar - Draw a colourful progress bar
* @param percent %age complete
* @param fmt printf(1)-like formatting string
* @param ... Arguments to formatting string
*/
static void message_bar(int percent, const char *fmt, ...)
{
va_list ap;
char buf[STRING], buf2[STRING];
int w = percent * COLS / 100;
size_t l;
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
l = mutt_strwidth(buf);
va_end(ap);
mutt_simple_format(buf2, sizeof(buf2), 0, COLS - 2, FMT_LEFT, 0, buf, sizeof(buf), 0);
move(LINES - 1, 0);
if (ColorDefs[MT_COLOR_PROGRESS] == 0)
{
addstr(buf2);
}
else
{
if (l < w)
{
/* The string fits within the colour bar */
SETCOLOR(MT_COLOR_PROGRESS);
addstr(buf2);
w -= l;
while (w--)
{
addch(' ');
}
SETCOLOR(MT_COLOR_NORMAL);
}
else
{
/* The string is too long for the colour bar */
char ch;
int off = mutt_wstr_trunc(buf2, sizeof(buf2), w, NULL);
ch = buf2[off];
buf2[off] = '\0';
SETCOLOR(MT_COLOR_PROGRESS);
addstr(buf2);
buf2[off] = ch;
SETCOLOR(MT_COLOR_NORMAL);
addstr(&buf2[off]);
}
}
clrtoeol();
mutt_refresh();
}
void mutt_progress_update(struct Progress *progress, long pos, int percent)
{
char posstr[SHORT_STRING];
bool update = false;
struct timeval tv = { 0, 0 };
unsigned int now = 0;
if (option(OPT_NO_CURSES))
return;
if (!progress->inc)
goto out;
/* refresh if size > inc */
if (progress->flags & MUTT_PROGRESS_SIZE && (pos >= progress->pos + (progress->inc << 10)))
update = true;
else if (pos >= progress->pos + progress->inc)
update = true;
/* skip refresh if not enough time has passed */
if (update && progress->timestamp && !gettimeofday(&tv, NULL))
{
now = ((unsigned int) tv.tv_sec * 1000) + (unsigned int) (tv.tv_usec / 1000);
if (now && now - progress->timestamp < TimeInc)
update = false;
}
/* always show the first update */
if (!pos)
update = true;
if (update)
{
if (progress->flags & MUTT_PROGRESS_SIZE)
{
pos = pos / (progress->inc << 10) * (progress->inc << 10);
mutt_pretty_size(posstr, sizeof(posstr), pos);
}
else
snprintf(posstr, sizeof(posstr), "%ld", pos);
mutt_debug(5, "updating progress: %s\n", posstr);
progress->pos = pos;
if (now)
progress->timestamp = now;
if (progress->size > 0)
{
message_bar((percent > 0) ? percent : (int) (100.0 * (double) progress->pos /
progress->size),
"%s %s/%s (%d%%)", progress->msg, posstr, progress->sizestr,
(percent > 0) ? percent : (int) (100.0 * (double) progress->pos /
progress->size));
}
else
{
if (percent > 0)
message_bar(percent, "%s %s (%d%%)", progress->msg, posstr, percent);
else
mutt_message("%s %s", progress->msg, posstr);
}
}
out:
if (pos >= progress->size)
mutt_clear_error();
}
void mutt_init_windows(void)
{
MuttHelpWindow = safe_calloc(1, sizeof(struct MuttWindow));
MuttIndexWindow = safe_calloc(1, sizeof(struct MuttWindow));
MuttStatusWindow = safe_calloc(1, sizeof(struct MuttWindow));
MuttMessageWindow = safe_calloc(1, sizeof(struct MuttWindow));
#ifdef USE_SIDEBAR
MuttSidebarWindow = safe_calloc(1, sizeof(struct MuttWindow));
#endif
}
void mutt_free_windows(void)
{
FREE(&MuttHelpWindow);
FREE(&MuttIndexWindow);
FREE(&MuttStatusWindow);
FREE(&MuttMessageWindow);
#ifdef USE_SIDEBAR
FREE(&MuttSidebarWindow);
#endif
}
void mutt_reflow_windows(void)
{
if (option(OPT_NO_CURSES))
return;
mutt_debug(2, "In mutt_reflow_windows\n");
MuttStatusWindow->rows = 1;
MuttStatusWindow->cols = COLS;
MuttStatusWindow->row_offset = option(OPT_STATUS_ON_TOP) ? 0 : LINES - 2;
MuttStatusWindow->col_offset = 0;
memcpy(MuttHelpWindow, MuttStatusWindow, sizeof(struct MuttWindow));
if (!option(OPT_HELP))
MuttHelpWindow->rows = 0;
else
MuttHelpWindow->row_offset = option(OPT_STATUS_ON_TOP) ? LINES - 2 : 0;
memcpy(MuttMessageWindow, MuttStatusWindow, sizeof(struct MuttWindow));
MuttMessageWindow->row_offset = LINES - 1;
memcpy(MuttIndexWindow, MuttStatusWindow, sizeof(struct MuttWindow));
MuttIndexWindow->rows = MAX(
LINES - MuttStatusWindow->rows - MuttHelpWindow->rows - MuttMessageWindow->rows, 0);
MuttIndexWindow->row_offset =
option(OPT_STATUS_ON_TOP) ? MuttStatusWindow->rows : MuttHelpWindow->rows;
#ifdef USE_SIDEBAR
if (option(OPT_SIDEBAR))
{
memcpy(MuttSidebarWindow, MuttIndexWindow, sizeof(struct MuttWindow));
MuttSidebarWindow->cols = SidebarWidth;
MuttIndexWindow->cols -= SidebarWidth;
if (option(OPT_SIDEBAR_ON_RIGHT))
{
MuttSidebarWindow->col_offset = COLS - SidebarWidth;
}
else
{
MuttIndexWindow->col_offset += SidebarWidth;
}
}
#endif
mutt_set_current_menu_redraw_full();
/* the pager menu needs this flag set to recalc line_info */
mutt_set_current_menu_redraw(REDRAW_FLOW);
}
static void reflow_message_window_rows(int mw_rows)
{
MuttMessageWindow->rows = mw_rows;
MuttMessageWindow->row_offset = LINES - mw_rows;
MuttStatusWindow->row_offset = option(OPT_STATUS_ON_TOP) ? 0 : LINES - mw_rows - 1;
if (option(OPT_HELP))
MuttHelpWindow->row_offset = option(OPT_STATUS_ON_TOP) ? LINES - mw_rows - 1 : 0;
MuttIndexWindow->rows = MAX(
LINES - MuttStatusWindow->rows - MuttHelpWindow->rows - MuttMessageWindow->rows, 0);
#ifdef USE_SIDEBAR
if (option(OPT_SIDEBAR))
MuttSidebarWindow->rows = MuttIndexWindow->rows;
#endif
/* We don't also set REDRAW_FLOW because this function only
* changes rows and is a temporary adjustment. */
mutt_set_current_menu_redraw_full();
}
int mutt_window_move(struct MuttWindow *win, int row, int col)
{
return move(win->row_offset + row, win->col_offset + col);
}
int mutt_window_mvaddch(struct MuttWindow *win, int row, int col, const chtype ch)
{
return mvaddch(win->row_offset + row, win->col_offset + col, ch);
}
int mutt_window_mvaddstr(struct MuttWindow *win, int row, int col, const char *str)
{
return mvaddstr(win->row_offset + row, win->col_offset + col, str);
}
#ifdef USE_SLANG_CURSES
static int vw_printw(SLcurses_Window_Type *win, const char *fmt, va_list ap)
{
char buf[LONG_STRING];
(void) SLvsnprintf(buf, sizeof(buf), (char *) fmt, ap);
SLcurses_waddnstr(win, buf, -1);
return 0;
}
#endif
int mutt_window_mvprintw(struct MuttWindow *win, int row, int col, const char *fmt, ...)
{
va_list ap;
int rv;
if ((rv = mutt_window_move(win, row, col) != ERR))
{
va_start(ap, fmt);
rv = vw_printw(stdscr, fmt, ap);
va_end(ap);
}
return rv;
}
/**
* mutt_window_clrtoeol - Clear to the end of the line
*
* Assumes the cursor has already been positioned within the window.
*/
void mutt_window_clrtoeol(struct MuttWindow *win)
{
int row, col, curcol;
if (win->col_offset + win->cols == COLS)
clrtoeol();
else
{
getyx(stdscr, row, col);
curcol = col;
while (curcol < win->col_offset + win->cols)
{
addch(' ');
curcol++;
}
move(row, col);
}
}
void mutt_window_clearline(struct MuttWindow *win, int row)
{
mutt_window_move(win, row, 0);
mutt_window_clrtoeol(win);
}
/**
* mutt_window_getyx - Get the cursor position in the window
*
* Assumes the current position is inside the window. Otherwise it will
* happily return negative or values outside the window boundaries
*/
void mutt_window_getyx(struct MuttWindow *win, int *y, int *x)
{
int row, col;
getyx(stdscr, row, col);
if (y)
*y = row - win->row_offset;
if (x)
*x = col - win->col_offset;
}
void mutt_show_error(void)
{
if (option(OPT_KEEP_QUIET))
return;
SETCOLOR(option(OPT_MSG_ERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
mutt_window_mvaddstr(MuttMessageWindow, 0, 0, ErrorBuf);
NORMAL_COLOR;
mutt_window_clrtoeol(MuttMessageWindow);
}
void mutt_endwin(const char *msg)
{
int e = errno;
if (!option(OPT_NO_CURSES))
{
/* at least in some situations (screen + xterm under SuSE11/12) endwin()
* doesn't properly flush the screen without an explicit call.
*/
mutt_refresh();
endwin();
}
if (msg && *msg)
{
puts(msg);
fflush(stdout);
}
errno = e;
}
void mutt_perror_debug(const char *s)
{
char *p = strerror(errno);
mutt_debug(1, "%s: %s (errno = %d)\n", s, p ? p : "unknown error", errno);
mutt_error("%s: %s (errno = %d)", s, p ? p : _("unknown error"), errno);
}
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;
}
int mutt_do_pager(const char *banner, const char *tempfile, int do_color, struct Pager *info)
{
int rc;
if (!Pager || (mutt_strcmp(Pager, "builtin") == 0))
rc = mutt_pager(banner, tempfile, do_color, info);
else
{
char cmd[STRING];
mutt_endwin(NULL);
mutt_expand_file_fmt(cmd, sizeof(cmd), Pager, tempfile);
if (mutt_system(cmd) == -1)
{
mutt_error(_("Error running \"%s\"!"), cmd);
rc = -1;
}
else
rc = 0;
mutt_unlink(tempfile);
}
return rc;
}
int _mutt_enter_fname(const char *prompt, char *buf, size_t blen, int buffy,
int multiple, char ***files, int *numfiles, int flags)
{
struct Event ch;
SETCOLOR(MT_COLOR_PROMPT);
mutt_window_mvaddstr(MuttMessageWindow, 0, 0, (char *) prompt);
addstr(_(" ('?' for list): "));
NORMAL_COLOR;
if (buf[0])
addstr(buf);
mutt_window_clrtoeol(MuttMessageWindow);
mutt_refresh();
ch = mutt_getch();
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 (buffy)
flags |= MUTT_SEL_BUFFY;
_mutt_select_file(buf, blen, flags, files, numfiles);
}
else
{
char *pc = safe_malloc(mutt_strlen(prompt) + 3);
sprintf(pc, "%s: ", prompt);
mutt_unget_event(ch.op ? 0 : ch.ch, ch.op ? ch.op : 0);
if (_mutt_get_field(pc, buf, blen, (buffy ? MUTT_EFILE : MUTT_FILE) | MUTT_CLEAR,
multiple, files, numfiles) != 0)
buf[0] = '\0';
FREE(&pc);
#ifdef USE_NOTMUCH
if ((flags & MUTT_SEL_VFOLDER) && buf[0] && (strncmp(buf, "notmuch://", 10) != 0))
nm_description_to_path(buf, buf, blen);
#endif
}
return 0;
}
void mutt_unget_event(int ch, int op)
{
struct Event tmp;
tmp.ch = ch;
tmp.op = op;
if (UngetCount >= UngetLen)
safe_realloc(&UngetKeyEvents, (UngetLen += 16) * sizeof(struct Event));
UngetKeyEvents[UngetCount++] = tmp;
}
void mutt_unget_string(char *s)
{
char *p = s + mutt_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
*
* 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)
safe_realloc(&MacroEvents, (MacroBufferLen += 128) * sizeof(struct Event));
MacroEvents[MacroBufferCount++] = tmp;
}
void mutt_flush_macro_to_endcond(void)
{
UngetCount = 0;
while (MacroBufferCount > 0)
{
if (MacroEvents[--MacroBufferCount].op == OP_END_COND)
return;
}
}
/**