forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.c
1328 lines (1182 loc) · 30.3 KB
/
menu.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 present the user with a selectable list
*
* @authors
* Copyright (C) 1996-2000,2002,2012 Michael R. Elkins <[email protected]>
*
* @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 <regex.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include "mutt/mutt.h"
#include "mutt.h"
#include "context.h"
#include "globals.h"
#include "keymap.h"
#include "mutt_curses.h"
#include "mutt_menu.h"
#include "opcodes.h"
#include "options.h"
#include "pattern.h"
#include "protos.h"
#include "tags.h"
#ifdef USE_SIDEBAR
#include "sidebar.h"
#endif
struct Header;
char *SearchBuffers[MENU_MAX];
/* These are used to track the active menus, for redraw operations. */
static size_t MenuStackCount = 0;
static size_t MenuStackLen = 0;
static struct Menu **MenuStack = NULL;
static int get_color(int index, unsigned char *s)
{
struct ColorLineHead *color = NULL;
struct ColorLine *np = NULL;
struct Header *hdr = Context->hdrs[Context->v2r[index]];
int type = *s;
switch (type)
{
case MT_COLOR_INDEX_AUTHOR:
color = &ColorIndexAuthorList;
break;
case MT_COLOR_INDEX_FLAGS:
color = &ColorIndexFlagsList;
break;
case MT_COLOR_INDEX_SUBJECT:
color = &ColorIndexSubjectList;
break;
case MT_COLOR_INDEX_TAG:
STAILQ_FOREACH(np, &ColorIndexTagList, entries)
{
if (strncmp((const char *) (s + 1), np->pattern, strlen(np->pattern)) == 0)
return np->pair;
const char *transform = mutt_hash_find(TagTransforms, np->pattern);
if (transform &&
(strncmp((const char *) (s + 1), transform, strlen(transform)) == 0))
{
return np->pair;
}
}
return 0;
default:
return ColorDefs[type];
}
STAILQ_FOREACH(np, color, entries)
{
if (mutt_pattern_exec(np->color_pattern, MUTT_MATCH_FULL_ADDRESS, Context, hdr, NULL))
return np->pair;
}
return 0;
}
static void print_enriched_string(int index, int attr, unsigned char *s, int do_color)
{
wchar_t wc;
size_t k;
size_t n = mutt_str_strlen((char *) s);
mbstate_t mbstate;
if (!stdscr)
return;
memset(&mbstate, 0, sizeof(mbstate));
while (*s)
{
if (*s < MUTT_TREE_MAX)
{
if (do_color)
#if defined(HAVE_COLOR) && defined(HAVE_USE_DEFAULT_COLORS)
/* Combining tree fg color and another bg color requires
* having use_default_colors, because the other bg color
* may be undefined. */
ATTRSET(mutt_combine_color(ColorDefs[MT_COLOR_TREE], attr));
#else
SETCOLOR(MT_COLOR_TREE);
#endif
while (*s && *s < MUTT_TREE_MAX)
{
switch (*s)
{
case MUTT_TREE_LLCORNER:
if (AsciiChars)
addch('`');
#ifdef WACS_LLCORNER
else
add_wch(WACS_LLCORNER);
#else
else if (CharsetIsUtf8)
addstr("\342\224\224"); /* WACS_LLCORNER */
else
addch(ACS_LLCORNER);
#endif
break;
case MUTT_TREE_ULCORNER:
if (AsciiChars)
addch(',');
#ifdef WACS_ULCORNER
else
add_wch(WACS_ULCORNER);
#else
else if (CharsetIsUtf8)
addstr("\342\224\214"); /* WACS_ULCORNER */
else
addch(ACS_ULCORNER);
#endif
break;
case MUTT_TREE_LTEE:
if (AsciiChars)
addch('|');
#ifdef WACS_LTEE
else
add_wch(WACS_LTEE);
#else
else if (CharsetIsUtf8)
addstr("\342\224\234"); /* WACS_LTEE */
else
addch(ACS_LTEE);
#endif
break;
case MUTT_TREE_HLINE:
if (AsciiChars)
addch('-');
#ifdef WACS_HLINE
else
add_wch(WACS_HLINE);
#else
else if (CharsetIsUtf8)
addstr("\342\224\200"); /* WACS_HLINE */
else
addch(ACS_HLINE);
#endif
break;
case MUTT_TREE_VLINE:
if (AsciiChars)
addch('|');
#ifdef WACS_VLINE
else
add_wch(WACS_VLINE);
#else
else if (CharsetIsUtf8)
addstr("\342\224\202"); /* WACS_VLINE */
else
addch(ACS_VLINE);
#endif
break;
case MUTT_TREE_TTEE:
if (AsciiChars)
addch('-');
#ifdef WACS_TTEE
else
add_wch(WACS_TTEE);
#else
else if (CharsetIsUtf8)
addstr("\342\224\254"); /* WACS_TTEE */
else
addch(ACS_TTEE);
#endif
break;
case MUTT_TREE_BTEE:
if (AsciiChars)
addch('-');
#ifdef WACS_BTEE
else
add_wch(WACS_BTEE);
#else
else if (CharsetIsUtf8)
addstr("\342\224\264"); /* WACS_BTEE */
else
addch(ACS_BTEE);
#endif
break;
case MUTT_TREE_SPACE:
addch(' ');
break;
case MUTT_TREE_RARROW:
addch('>');
break;
case MUTT_TREE_STAR:
addch('*'); /* fake thread indicator */
break;
case MUTT_TREE_HIDDEN:
addch('&');
break;
case MUTT_TREE_EQUALS:
addch('=');
break;
case MUTT_TREE_MISSING:
addch('?');
break;
}
s++;
n--;
}
if (do_color)
ATTRSET(attr);
}
else if (*s == MUTT_SPECIAL_INDEX)
{
s++;
if (do_color)
{
if (*s == MT_COLOR_INDEX)
{
attrset(attr);
}
else
{
if (get_color(index, s) == 0)
{
attron(attr);
}
else
{
attron(get_color(index, s));
}
}
}
s++;
n -= 2;
}
else if ((k = mbrtowc(&wc, (char *) s, n, &mbstate)) > 0)
{
addnstr((char *) s, k);
s += k;
n -= k;
}
else
break;
}
}
static void menu_make_entry(char *s, int l, struct Menu *menu, int i)
{
if (menu->dialog)
{
strncpy(s, menu->dialog[i], l);
menu->current = -1; /* hide menubar */
}
else
menu->make_entry(s, l, menu, i);
}
static void menu_pad_string(struct Menu *menu, char *buf, size_t buflen)
{
char *scratch = mutt_str_strdup(buf);
int shift = ArrowCursor ? 3 : 0;
int cols = menu->indexwin->cols - shift;
mutt_simple_format(buf, buflen, cols, cols, FMT_LEFT, ' ', scratch,
mutt_str_strlen(scratch), 1);
buf[buflen - 1] = '\0';
FREE(&scratch);
}
void menu_redraw_full(struct Menu *menu)
{
mutt_reflow_windows();
NORMAL_COLOR;
/* clear() doesn't optimize screen redraws */
move(0, 0);
clrtobot();
if (Help)
{
SETCOLOR(MT_COLOR_STATUS);
mutt_window_move(menu->helpwin, 0, 0);
mutt_paddstr(menu->helpwin->cols, menu->help);
NORMAL_COLOR;
}
menu->offset = 0;
menu->pagelen = menu->indexwin->rows;
mutt_show_error();
menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
#ifdef USE_SIDEBAR
menu->redraw |= REDRAW_SIDEBAR;
#endif
}
void menu_redraw_status(struct Menu *menu)
{
char buf[STRING];
snprintf(buf, sizeof(buf), MUTT_MODEFMT, menu->title);
SETCOLOR(MT_COLOR_STATUS);
mutt_window_move(menu->statuswin, 0, 0);
mutt_paddstr(menu->statuswin->cols, buf);
NORMAL_COLOR;
menu->redraw &= ~REDRAW_STATUS;
}
#ifdef USE_SIDEBAR
void menu_redraw_sidebar(struct Menu *menu)
{
menu->redraw &= ~REDRAW_SIDEBAR;
mutt_sb_draw();
}
#endif
void menu_redraw_index(struct Menu *menu)
{
char buf[LONG_STRING];
bool do_color;
int attr;
for (int i = menu->top; i < menu->top + menu->pagelen; i++)
{
if (i < menu->max)
{
attr = menu->color(i);
menu_make_entry(buf, sizeof(buf), menu, i);
menu_pad_string(menu, buf, sizeof(buf));
ATTRSET(attr);
mutt_window_move(menu->indexwin, i - menu->top + menu->offset, 0);
do_color = true;
if (i == menu->current)
{
SETCOLOR(MT_COLOR_INDICATOR);
if (ArrowCursor)
{
addstr("->");
ATTRSET(attr);
addch(' ');
}
else
do_color = false;
}
else if (ArrowCursor)
addstr(" ");
print_enriched_string(i, attr, (unsigned char *) buf, do_color);
}
else
{
NORMAL_COLOR;
mutt_window_clearline(menu->indexwin, i - menu->top + menu->offset);
}
}
NORMAL_COLOR;
menu->redraw = 0;
}
void menu_redraw_motion(struct Menu *menu)
{
char buf[LONG_STRING];
if (menu->dialog)
{
menu->redraw &= ~REDRAW_MOTION;
return;
}
/* Note: menu->color() for the index can end up retrieving a message
* over imap (if matching against ~h for instance). This can
* generate status messages. So we want to call it *before* we
* position the cursor for drawing. */
const int old_color = menu->color(menu->oldcurrent);
mutt_window_move(menu->indexwin, menu->oldcurrent + menu->offset - menu->top, 0);
ATTRSET(old_color);
if (ArrowCursor)
{
/* clear the pointer */
addstr(" ");
if (menu->redraw & REDRAW_MOTION_RESYNCH)
{
menu_make_entry(buf, sizeof(buf), menu, menu->oldcurrent);
menu_pad_string(menu, buf, sizeof(buf));
mutt_window_move(menu->indexwin, menu->oldcurrent + menu->offset - menu->top, 3);
print_enriched_string(menu->oldcurrent, old_color, (unsigned char *) buf, 1);
}
/* now draw it in the new location */
SETCOLOR(MT_COLOR_INDICATOR);
mutt_window_mvaddstr(menu->indexwin, menu->current + menu->offset - menu->top, 0, "->");
}
else
{
/* erase the current indicator */
menu_make_entry(buf, sizeof(buf), menu, menu->oldcurrent);
menu_pad_string(menu, buf, sizeof(buf));
print_enriched_string(menu->oldcurrent, old_color, (unsigned char *) buf, 1);
/* now draw the new one to reflect the change */
const int cur_color = menu->color(menu->current);
menu_make_entry(buf, sizeof(buf), menu, menu->current);
menu_pad_string(menu, buf, sizeof(buf));
SETCOLOR(MT_COLOR_INDICATOR);
mutt_window_move(menu->indexwin, menu->current + menu->offset - menu->top, 0);
print_enriched_string(menu->current, cur_color, (unsigned char *) buf, 0);
}
menu->redraw &= REDRAW_STATUS;
NORMAL_COLOR;
}
void menu_redraw_current(struct Menu *menu)
{
char buf[LONG_STRING];
int attr = menu->color(menu->current);
mutt_window_move(menu->indexwin, menu->current + menu->offset - menu->top, 0);
menu_make_entry(buf, sizeof(buf), menu, menu->current);
menu_pad_string(menu, buf, sizeof(buf));
SETCOLOR(MT_COLOR_INDICATOR);
if (ArrowCursor)
{
addstr("->");
ATTRSET(attr);
addch(' ');
menu_pad_string(menu, buf, sizeof(buf));
print_enriched_string(menu->current, attr, (unsigned char *) buf, 1);
}
else
print_enriched_string(menu->current, attr, (unsigned char *) buf, 0);
menu->redraw &= REDRAW_STATUS;
NORMAL_COLOR;
}
static void menu_redraw_prompt(struct Menu *menu)
{
if (menu->dialog)
{
if (OPT_MSG_ERR)
{
mutt_sleep(1);
OPT_MSG_ERR = false;
}
if (*ErrorBuf)
mutt_clear_error();
mutt_window_mvaddstr(menu->messagewin, 0, 0, menu->prompt);
mutt_window_clrtoeol(menu->messagewin);
}
}
void menu_check_recenter(struct Menu *menu)
{
int c = MIN(MenuContext, menu->pagelen / 2);
int old_top = menu->top;
if (!MenuMoveOff && menu->max <= menu->pagelen) /* less entries than lines */
{
if (menu->top != 0)
{
menu->top = 0;
menu->redraw |= REDRAW_INDEX;
}
}
else
{
if (MenuScroll || (menu->pagelen <= 0) || (c < MenuContext))
{
if (menu->current < menu->top + c)
menu->top = menu->current - c;
else if (menu->current >= menu->top + menu->pagelen - c)
menu->top = menu->current - menu->pagelen + c + 1;
}
else
{
if (menu->current < menu->top + c)
menu->top -= (menu->pagelen - c) * ((menu->top + menu->pagelen - 1 - menu->current) /
(menu->pagelen - c)) -
c;
else if ((menu->current >= menu->top + menu->pagelen - c))
menu->top +=
(menu->pagelen - c) * ((menu->current - menu->top) / (menu->pagelen - c)) - c;
}
}
if (!MenuMoveOff) /* make entries stick to bottom */
menu->top = MIN(menu->top, menu->max - menu->pagelen);
menu->top = MAX(menu->top, 0);
if (menu->top != old_top)
menu->redraw |= REDRAW_INDEX;
}
static void menu_jump(struct Menu *menu)
{
int n;
if (menu->max)
{
mutt_unget_event(LastKey, 0);
char buf[SHORT_STRING];
buf[0] = '\0';
if (mutt_get_field(_("Jump to: "), buf, sizeof(buf), 0) == 0 && buf[0])
{
if (mutt_str_atoi(buf, &n) == 0 && n > 0 && n < menu->max + 1)
{
n--; /* msg numbers are 0-based */
menu->current = n;
menu->redraw = REDRAW_MOTION;
}
else
mutt_error(_("Invalid index number."));
}
}
else
mutt_error(_("No entries."));
}
void menu_next_line(struct Menu *menu)
{
if (menu->max)
{
int c = MIN(MenuContext, menu->pagelen / 2);
if (menu->top + 1 < menu->max - c &&
(MenuMoveOff || (menu->max > menu->pagelen && menu->top < menu->max - menu->pagelen)))
{
menu->top++;
if (menu->current < menu->top + c && menu->current < menu->max - 1)
menu->current++;
menu->redraw = REDRAW_INDEX;
}
else
mutt_error(_("You cannot scroll down farther."));
}
else
mutt_error(_("No entries."));
}
void menu_prev_line(struct Menu *menu)
{
if (menu->top > 0)
{
int c = MIN(MenuContext, menu->pagelen / 2);
menu->top--;
if (menu->current >= menu->top + menu->pagelen - c && menu->current > 1)
menu->current--;
menu->redraw = REDRAW_INDEX;
}
else
mutt_error(_("You cannot scroll up farther."));
}
/**
* menu_length_jump - Calculate the destination of a jump
*
* * pageup: jumplen == -pagelen
* * pagedown: jumplen == pagelen
* * halfup: jumplen == -pagelen/2
* * halfdown: jumplen == pagelen/2
*/
#define DIRECTION ((neg * 2) + 1)
static void menu_length_jump(struct Menu *menu, int jumplen)
{
const int neg = (jumplen >= 0) ? 0 : -1;
const int c = MIN(MenuContext, menu->pagelen / 2);
if (menu->max)
{
/* possible to scroll? */
int tmp;
if (DIRECTION * menu->top <
(tmp = (neg ? 0 : (menu->max /* -1 */) - (menu->pagelen /* -1 */))))
{
menu->top += jumplen;
/* jumped too long? */
if ((neg || !MenuMoveOff) && DIRECTION * menu->top > tmp)
menu->top = tmp;
/* need to move the cursor? */
if ((DIRECTION *
(tmp = (menu->current - (menu->top + (neg ? (menu->pagelen - 1) - c : c))))) < 0)
{
menu->current -= tmp;
}
menu->redraw = REDRAW_INDEX;
}
else if (menu->current != (neg ? 0 : menu->max - 1) && !menu->dialog)
{
menu->current += jumplen;
menu->redraw = REDRAW_MOTION;
}
else
{
mutt_error(neg ? _("You are on the first page.") : _("You are on the last page."));
}
menu->current = MIN(menu->current, menu->max - 1);
menu->current = MAX(menu->current, 0);
}
else
mutt_error(_("No entries."));
}
#undef DIRECTION
void menu_next_page(struct Menu *menu)
{
menu_length_jump(menu, MAX(menu->pagelen /* - MenuOverlap */, 0));
}
void menu_prev_page(struct Menu *menu)
{
menu_length_jump(menu, 0 - MAX(menu->pagelen /* - MenuOverlap */, 0));
}
void menu_half_down(struct Menu *menu)
{
menu_length_jump(menu, menu->pagelen / 2);
}
void menu_half_up(struct Menu *menu)
{
menu_length_jump(menu, 0 - menu->pagelen / 2);
}
void menu_top_page(struct Menu *menu)
{
if (menu->current != menu->top)
{
menu->current = menu->top;
menu->redraw = REDRAW_MOTION;
}
}
void menu_bottom_page(struct Menu *menu)
{
if (menu->max)
{
menu->current = menu->top + menu->pagelen - 1;
if (menu->current > menu->max - 1)
menu->current = menu->max - 1;
menu->redraw = REDRAW_MOTION;
}
else
mutt_error(_("No entries."));
}
void menu_middle_page(struct Menu *menu)
{
if (menu->max)
{
int i = menu->top + menu->pagelen;
if (i > menu->max - 1)
i = menu->max - 1;
menu->current = menu->top + (i - menu->top) / 2;
menu->redraw = REDRAW_MOTION;
}
else
mutt_error(_("No entries."));
}
void menu_first_entry(struct Menu *menu)
{
if (menu->max)
{
menu->current = 0;
menu->redraw = REDRAW_MOTION;
}
else
mutt_error(_("No entries."));
}
void menu_last_entry(struct Menu *menu)
{
if (menu->max)
{
menu->current = menu->max - 1;
menu->redraw = REDRAW_MOTION;
}
else
mutt_error(_("No entries."));
}
void menu_current_top(struct Menu *menu)
{
if (menu->max)
{
menu->top = menu->current;
menu->redraw = REDRAW_INDEX;
}
else
mutt_error(_("No entries."));
}
void menu_current_middle(struct Menu *menu)
{
if (menu->max)
{
menu->top = menu->current - menu->pagelen / 2;
if (menu->top < 0)
menu->top = 0;
menu->redraw = REDRAW_INDEX;
}
else
mutt_error(_("No entries."));
}
void menu_current_bottom(struct Menu *menu)
{
if (menu->max)
{
menu->top = menu->current - menu->pagelen + 1;
if (menu->top < 0)
menu->top = 0;
menu->redraw = REDRAW_INDEX;
}
else
mutt_error(_("No entries."));
}
static void menu_next_entry(struct Menu *menu)
{
if (menu->current < menu->max - 1)
{
menu->current++;
menu->redraw = REDRAW_MOTION;
}
else
mutt_error(_("You are on the last entry."));
}
static void menu_prev_entry(struct Menu *menu)
{
if (menu->current)
{
menu->current--;
menu->redraw = REDRAW_MOTION;
}
else
mutt_error(_("You are on the first entry."));
}
static int default_color(int i)
{
return ColorDefs[MT_COLOR_NORMAL];
}
static int menu_search_generic(struct Menu *m, regex_t *re, int n)
{
char buf[LONG_STRING];
menu_make_entry(buf, sizeof(buf), m, n);
return (regexec(re, buf, 0, NULL, 0));
}
void mutt_menu_init(void)
{
for (int i = 0; i < MENU_MAX; i++)
SearchBuffers[i] = NULL;
}
struct Menu *mutt_new_menu(int menu)
{
struct Menu *p = mutt_mem_calloc(1, sizeof(struct Menu));
if ((menu < 0) || (menu >= MENU_MAX))
menu = MENU_GENERIC;
p->menu = menu;
p->current = 0;
p->top = 0;
p->offset = 0;
p->redraw = REDRAW_FULL;
p->pagelen = MuttIndexWindow->rows;
p->indexwin = MuttIndexWindow;
p->statuswin = MuttStatusWindow;
p->helpwin = MuttHelpWindow;
p->messagewin = MuttMessageWindow;
p->color = default_color;
p->search = menu_search_generic;
return p;
}
void mutt_menu_destroy(struct Menu **p)
{
if ((*p)->dialog)
{
for (int i = 0; i < (*p)->max; i++)
FREE(&(*p)->dialog[i]);
FREE(&(*p)->dialog);
}
FREE(p);
}
static struct Menu *get_current_menu(void)
{
return MenuStackCount ? MenuStack[MenuStackCount - 1] : NULL;
}
void mutt_push_current_menu(struct Menu *menu)
{
if (MenuStackCount >= MenuStackLen)
{
MenuStackLen += 5;
mutt_mem_realloc(&MenuStack, MenuStackLen * sizeof(struct Menu *));
}
MenuStack[MenuStackCount++] = menu;
CurrentMenu = menu->menu;
}
void mutt_pop_current_menu(struct Menu *menu)
{
struct Menu *prev_menu = NULL;
if (!MenuStackCount || (MenuStack[MenuStackCount - 1] != menu))
{
mutt_debug(1, "called with inactive menu\n");
return;
}
MenuStackCount--;
prev_menu = get_current_menu();
if (prev_menu)
{
CurrentMenu = prev_menu->menu;
prev_menu->redraw = REDRAW_FULL;
}
else
{
CurrentMenu = MENU_MAIN;
}
}
void mutt_set_current_menu_redraw(int redraw)
{
struct Menu *current_menu = NULL;
current_menu = get_current_menu();
if (current_menu)
current_menu->redraw |= redraw;
}
void mutt_set_current_menu_redraw_full(void)
{
struct Menu *current_menu = NULL;
current_menu = get_current_menu();
if (current_menu)
current_menu->redraw = REDRAW_FULL;
}
void mutt_set_menu_redraw(int menu_type, int redraw)
{
if (CurrentMenu == menu_type)
mutt_set_current_menu_redraw(redraw);
}
void mutt_set_menu_redraw_full(int menu_type)
{
if (CurrentMenu == menu_type)
mutt_set_current_menu_redraw_full();
}
void mutt_current_menu_redraw()
{
struct Menu *current_menu = NULL;
current_menu = get_current_menu();
if (current_menu)
{
if (menu_redraw(current_menu) == OP_REDRAW)
/* On a REDRAW_FULL with a non-customized redraw, menu_redraw()
* will return OP_REDRAW to give the calling menu-loop a chance to
* customize output.
*/
menu_redraw(current_menu);
}
}
#define MUTT_SEARCH_UP 1
#define MUTT_SEARCH_DOWN 2
static int menu_search(struct Menu *menu, int op)
{
int r = 0, wrap = 0;
int search_dir;
regex_t re;
char buf[SHORT_STRING];
char *search_buf =
menu->menu >= 0 && menu->menu < MENU_MAX ? SearchBuffers[menu->menu] : NULL;
if (!(search_buf && *search_buf) || (op != OP_SEARCH_NEXT && op != OP_SEARCH_OPPOSITE))
{
mutt_str_strfcpy(buf, search_buf && *search_buf ? search_buf : "", sizeof(buf));
if (mutt_get_field((op == OP_SEARCH || op == OP_SEARCH_NEXT) ?
_("Search for: ") :
_("Reverse search for: "),
buf, sizeof(buf), MUTT_CLEAR) != 0 ||
!buf[0])
return -1;
if (menu->menu >= 0 && menu->menu < MENU_MAX)
{
mutt_str_replace(&SearchBuffers[menu->menu], buf);
search_buf = SearchBuffers[menu->menu];
}
menu->search_dir =
(op == OP_SEARCH || op == OP_SEARCH_NEXT) ? MUTT_SEARCH_DOWN : MUTT_SEARCH_UP;
}
search_dir = (menu->search_dir == MUTT_SEARCH_UP) ? -1 : 1;
if (op == OP_SEARCH_OPPOSITE)
search_dir = -search_dir;
if (search_buf)
{
int flags = mutt_mb_is_lower(search_buf) ? REG_ICASE : 0;
r = REGCOMP(&re, search_buf, REG_NOSUB | flags);
}
if (r != 0)
{
regerror(r, &re, buf, sizeof(buf));
mutt_error("%s", buf);
return -1;
}
r = menu->current + search_dir;
search_next:
if (wrap)
mutt_message(_("Search wrapped to top."));
while (r >= 0 && r < menu->max)
{
if (menu->search(menu, &re, r) == 0)
{
regfree(&re);
return r;
}
r += search_dir;
}
if (WrapSearch && wrap++ == 0)
{
r = search_dir == 1 ? 0 : menu->max - 1;
goto search_next;
}
regfree(&re);
mutt_error(_("Not found."));
return -1;
}
static int menu_dialog_translate_op(int i)
{
switch (i)
{