forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
curs_main.c
3311 lines (2901 loc) · 92.6 KB
/
curs_main.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 manage the main index (list of emails)
*
* @authors
* Copyright (C) 1996-2000,2002,2010,2012-2013 Michael R. Elkins <me@mutt.org>
*
* @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 <assert.h>
#include <ctype.h>
#ifdef ENABLE_NLS
#include <libintl.h>
#endif
#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mutt.h"
#include "alias.h"
#include "body.h"
#include "buffy.h"
#include "context.h"
#include "envelope.h"
#include "format_flags.h"
#include "globals.h"
#include "header.h"
#include "keymap.h"
#include "lib/lib.h"
#include "list.h"
#include "mailbox.h"
#include "mapping.h"
#include "mutt_curses.h"
#include "mutt_menu.h"
#include "mutt_socket.h"
#include "mx.h"
#include "ncrypt/ncrypt.h"
#include "opcodes.h"
#include "options.h"
#include "pattern.h"
#include "protos.h"
#include "sort.h"
#include "thread.h"
#ifdef HAVE_NCURSESW_NCURSES_H
#include <ncursesw/term.h>
#elif defined(HAVE_NCURSES_NCURSES_H)
#include <ncurses/term.h>
#elif !defined(USE_SLANG_CURSES)
#include <term.h>
#endif
#ifdef USE_SIDEBAR
#include "sidebar.h"
#endif
#ifdef USE_POP
#include "pop.h"
#endif
#ifdef USE_IMAP
#include "imap/imap.h"
#endif
#ifdef USE_NOTMUCH
#include "mutt_notmuch.h"
#endif
#ifdef USE_NNTP
#include "nntp.h"
#endif
static const char *No_mailbox_is_open = N_("No mailbox is open.");
static const char *There_are_no_messages = N_("There are no messages.");
static const char *Mailbox_is_read_only = N_("Mailbox is read-only.");
static const char *Function_not_permitted_in_attach_message_mode =
N_("Function not permitted in attach-message mode.");
static const char *No_visible = N_("No visible messages.");
#define CHECK_IN_MAILBOX \
if (!Context) \
{ \
mutt_flushinp(); \
mutt_error(_(No_mailbox_is_open)); \
break; \
}
#define CHECK_MSGCOUNT \
if (!Context) \
{ \
mutt_flushinp(); \
mutt_error(_(No_mailbox_is_open)); \
break; \
} \
else if (!Context->msgcount) \
{ \
mutt_flushinp(); \
mutt_error(_(There_are_no_messages)); \
break; \
}
#define CHECK_VISIBLE \
if (Context && menu->current >= Context->vcount) \
{ \
mutt_flushinp(); \
mutt_error(_(No_visible)); \
break; \
}
#define CHECK_READONLY \
if (Context->readonly) \
{ \
mutt_flushinp(); \
mutt_error(_(Mailbox_is_read_only)); \
break; \
}
#define CHECK_ACL(aclbit, action) \
if (!mutt_bit_isset(Context->rights, aclbit)) \
{ \
mutt_flushinp(); \
/* L10N: %s is one of the CHECK_ACL entries below. */ \
mutt_error(_("%s: Operation not permitted by ACL"), action); \
break; \
}
#define CHECK_ATTACH \
if (option(OPT_ATTACH_MSG)) \
{ \
mutt_flushinp(); \
mutt_error(_(Function_not_permitted_in_attach_message_mode)); \
break; \
}
#define CURHDR Context->hdrs[Context->v2r[menu->current]]
#define UNREAD(h) mutt_thread_contains_unread(Context, h)
#define FLAGGED(h) mutt_thread_contains_flagged(Context, h)
#define CAN_COLLAPSE(header) \
((option(OPT_COLLAPSE_UNREAD) || !UNREAD(header)) && \
(option(OPT_COLLAPSE_FLAGGED) || !FLAGGED(header)))
/* de facto standard escapes for tsl/fsl */
static char *tsl = "\033]0;";
static char *fsl = "\007";
/**
* collapse/uncollapse all threads
* @param menu current menu
* @param toggle toggle collapsed state
*
* This function is called by the OP_MAIN_COLLAPSE_ALL command and on folder
* enter if the OPT_COLLAPSE_ALL option is set. In the first case, the @a toggle
* parameter is 1 to actually toggle collapsed/uncollapsed state on all
* threads. In the second case, the @a toggle parameter is 0, actually turning
* this function into a one-way collapse.
*/
static void collapse_all(struct Menu *menu, int toggle)
{
struct Header *h = NULL, *base = NULL;
struct MuttThread *thread = NULL, *top = NULL;
int final;
if (!Context || (Context->msgcount == 0))
return;
/* Figure out what the current message would be after folding / unfolding,
* so that we can restore the cursor in a sane way afterwards. */
if (CURHDR->collapsed && toggle)
final = mutt_uncollapse_thread(Context, CURHDR);
else if (CAN_COLLAPSE(CURHDR))
final = mutt_collapse_thread(Context, CURHDR);
else
final = CURHDR->virtual;
base = Context->hdrs[Context->v2r[final]];
/* Iterate all threads, perform collapse/uncollapse as needed */
top = Context->tree;
Context->collapsed = toggle ? !Context->collapsed : true;
while ((thread = top) != NULL)
{
while (!thread->message)
thread = thread->child;
h = thread->message;
if (h->collapsed != Context->collapsed)
{
if (h->collapsed)
mutt_uncollapse_thread(Context, h);
else if (CAN_COLLAPSE(h))
mutt_collapse_thread(Context, h);
}
top = top->next;
}
/* Restore the cursor */
mutt_set_virtual(Context);
int j;
for (j = 0; j < Context->vcount; j++)
{
if (Context->hdrs[Context->v2r[j]]->index == base->index)
{
menu->current = j;
break;
}
}
menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
}
static int ci_next_undeleted(int msgno)
{
int i;
for (i = msgno + 1; i < Context->vcount; i++)
if (!Context->hdrs[Context->v2r[i]]->deleted)
return i;
return -1;
}
static int ci_previous_undeleted(int msgno)
{
int i;
for (i = msgno - 1; i >= 0; i--)
if (!Context->hdrs[Context->v2r[i]]->deleted)
return i;
return -1;
}
/**
* ci_first_message - Get index of first new message
*
* Return the index of the first new message, or failing that, the first
* unread message.
*/
static int ci_first_message(void)
{
int old = -1, i;
if (Context && Context->msgcount)
{
for (i = 0; i < Context->vcount; i++)
{
if (!Context->hdrs[Context->v2r[i]]->read &&
!Context->hdrs[Context->v2r[i]]->deleted)
{
if (!Context->hdrs[Context->v2r[i]]->old)
return i;
else if (old == -1)
old = i;
}
}
if (old != -1)
return old;
/* If Sort is reverse and not threaded, the latest message is first.
* If Sort is threaded, the latest message is first iff exactly one
* of Sort and SortAux are reverse.
*/
if (((Sort & SORT_REVERSE) && (Sort & SORT_MASK) != SORT_THREADS) ||
((Sort & SORT_MASK) == SORT_THREADS && ((Sort ^ SortAux) & SORT_REVERSE)))
return 0;
else
return (Context->vcount ? Context->vcount - 1 : 0);
}
return 0;
}
/**
* mx_toggle_write - Toggle the mailbox's readonly flag
*
* This should be in mx.c, but it only gets used here.
*/
static int mx_toggle_write(struct Context *ctx)
{
if (!ctx)
return -1;
if (ctx->readonly)
{
mutt_error(_("Cannot toggle write on a readonly mailbox!"));
return -1;
}
if (ctx->dontwrite)
{
ctx->dontwrite = false;
mutt_message(_("Changes to folder will be written on folder exit."));
}
else
{
ctx->dontwrite = true;
mutt_message(_("Changes to folder will not be written."));
}
return 0;
}
static void resort_index(struct Menu *menu)
{
int i;
struct Header *current = CURHDR;
menu->current = -1;
mutt_sort_headers(Context, 0);
/* Restore the current message */
for (i = 0; i < Context->vcount; i++)
{
if (Context->hdrs[Context->v2r[i]] == current)
{
menu->current = i;
break;
}
}
if ((Sort & SORT_MASK) == SORT_THREADS && menu->current < 0)
menu->current = mutt_parent_message(Context, current, 0);
if (menu->current < 0)
menu->current = ci_first_message();
menu->redraw |= REDRAW_INDEX | REDRAW_STATUS;
}
void update_index(struct Menu *menu, struct Context *ctx, int check, int oldcount, int index_hint)
{
/* store pointers to the newly added messages */
struct Header **save_new = NULL;
int j;
if (!menu || !ctx)
return;
/* take note of the current message */
if (oldcount)
{
if (menu->current < ctx->vcount)
menu->oldcurrent = index_hint;
else
oldcount = 0; /* invalid message number! */
}
/* We are in a limited view. Check if the new message(s) satisfy
* the limit criteria. If they do, set their virtual msgno so that
* they will be visible in the limited view */
if (ctx->pattern)
{
#define THIS_BODY ctx->hdrs[j]->content
for (j = (check == MUTT_REOPENED) ? 0 : oldcount; j < ctx->msgcount; j++)
{
if (!j)
ctx->vcount = 0;
if (mutt_pattern_exec(ctx->limit_pattern, MUTT_MATCH_FULL_ADDRESS, ctx,
ctx->hdrs[j], NULL))
{
assert(ctx->vcount < ctx->msgcount);
ctx->hdrs[j]->virtual = ctx->vcount;
ctx->v2r[ctx->vcount] = j;
ctx->hdrs[j]->limited = true;
ctx->vcount++;
ctx->vsize += THIS_BODY->length + THIS_BODY->offset - THIS_BODY->hdr_offset;
}
}
#undef THIS_BODY
}
/* save the list of new messages */
if (option(OPT_UNCOLLAPSE_NEW) && oldcount && check != MUTT_REOPENED &&
((Sort & SORT_MASK) == SORT_THREADS))
{
save_new = safe_malloc(sizeof(struct Header *) * (ctx->msgcount - oldcount));
for (j = oldcount; j < ctx->msgcount; j++)
save_new[j - oldcount] = ctx->hdrs[j];
}
/* if the mailbox was reopened, need to rethread from scratch */
mutt_sort_headers(ctx, (check == MUTT_REOPENED));
/* uncollapse threads with new mail */
if (option(OPT_UNCOLLAPSE_NEW) && ((Sort & SORT_MASK) == SORT_THREADS))
{
if (check == MUTT_REOPENED)
{
struct MuttThread *h = NULL, *k = NULL;
ctx->collapsed = false;
for (h = ctx->tree; h; h = h->next)
{
for (k = h; !k->message; k = k->child)
;
mutt_uncollapse_thread(ctx, k->message);
}
mutt_set_virtual(ctx);
}
else if (oldcount)
{
for (j = 0; j < ctx->msgcount - oldcount; j++)
{
int k;
for (k = 0; k < ctx->msgcount; k++)
{
struct Header *h = ctx->hdrs[k];
if (h == save_new[j] && (!ctx->pattern || h->limited))
mutt_uncollapse_thread(ctx, h);
}
}
FREE(&save_new);
mutt_set_virtual(ctx);
}
}
menu->current = -1;
if (oldcount)
{
/* restore the current message to the message it was pointing to */
for (j = 0; j < ctx->vcount; j++)
{
if (ctx->hdrs[ctx->v2r[j]]->index == menu->oldcurrent)
{
menu->current = j;
break;
}
}
}
if (menu->current < 0)
menu->current = ci_first_message();
}
static int main_change_folder(struct Menu *menu, int op, char *buf, size_t bufsz,
int *oldcount, int *index_hint, int flags)
{
#ifdef USE_NNTP
if (option(OPT_NEWS))
{
unset_option(OPT_NEWS);
nntp_expand_path(buf, bufsz, &CurrentNewsSrv->conn->account);
}
else
#endif
mutt_expand_path(buf, bufsz);
if (mx_get_magic(buf) <= 0)
{
mutt_error(_("%s is not a mailbox."), buf);
return -1;
}
/* keepalive failure in mutt_enter_fname may kill connection. #3028 */
if (Context && !Context->path)
FREE(&Context);
if (Context)
{
int check;
char *new_last_folder = NULL;
#ifdef USE_COMPRESSED
if (Context->compress_info && Context->realpath)
new_last_folder = safe_strdup(Context->realpath);
else
#endif
new_last_folder = safe_strdup(Context->path);
*oldcount = Context ? Context->msgcount : 0;
if ((check = mx_close_mailbox(Context, index_hint)) != 0)
{
if (check == MUTT_NEW_MAIL || check == MUTT_REOPENED)
update_index(menu, Context, check, *oldcount, *index_hint);
FREE(&new_last_folder);
set_option(OPT_SEARCH_INVALID);
menu->redraw |= REDRAW_INDEX | REDRAW_STATUS;
return 0;
}
FREE(&Context);
FREE(&LastFolder);
LastFolder = new_last_folder;
}
mutt_str_replace(&CurrentFolder, buf);
mutt_sleep(0);
/* Note that menu->menu may be MENU_PAGER if the change folder
* operation originated from the pager.
*
* However, exec commands currently use CurrentMenu to determine what
* functions are available, which is automatically set by the
* mutt_push/pop_current_menu() functions. If that changes, the menu
* would need to be reset here, and the pager cleanup code after the
* switch statement would need to be run. */
mutt_folder_hook(buf);
if ((Context = mx_open_mailbox(
buf, (option(OPT_READONLY) || op == OP_MAIN_CHANGE_FOLDER_READONLY) ? MUTT_READONLY : 0,
NULL)) != NULL)
{
menu->current = ci_first_message();
}
else
menu->current = 0;
if (((Sort & SORT_MASK) == SORT_THREADS) && option(OPT_COLLAPSE_ALL))
collapse_all(menu, 0);
#ifdef USE_SIDEBAR
mutt_sb_set_open_buffy();
#endif
mutt_clear_error();
mutt_buffy_check(true); /* force the buffy check after we have changed the folder */
menu->redraw = REDRAW_FULL;
set_option(OPT_SEARCH_INVALID);
return 0;
}
/**
* mutt_ts_capability - Check terminal capabilities
*
* terminal status capability check. terminfo must have been initialized.
*/
bool mutt_ts_capability(void)
{
char *term = getenv("TERM");
char *tcaps = NULL;
#ifdef HAVE_USE_EXTENDED_NAMES
int tcapi;
#endif
char **termp = NULL;
char *known[] = {
"color-xterm", "cygwin", "eterm", "kterm", "nxterm",
"putty", "rxvt", "screen", "xterm", NULL,
};
/* If tsl is set, then terminfo says that status lines work. */
tcaps = tigetstr("tsl");
if (tcaps && tcaps != (char *) -1 && *tcaps)
{
/* update the static defns of tsl/fsl from terminfo */
tsl = tcaps;
tcaps = tigetstr("fsl");
if (tcaps && tcaps != (char *) -1 && *tcaps)
fsl = tcaps;
return true;
}
/* If XT (boolean) is set, then this terminal supports the standard escape. */
/* Beware: tigetflag returns -1 if XT is invalid or not a boolean. */
#ifdef HAVE_USE_EXTENDED_NAMES
use_extended_names(true);
tcapi = tigetflag("XT");
if (tcapi == 1)
return true;
#endif /* HAVE_USE_EXTENDED_NAMES */
/* Check term types that are known to support the standard escape without
* necessarily asserting it in terminfo. */
for (termp = known; termp; termp++)
{
if (term && *termp && (mutt_strncasecmp(term, *termp, strlen(*termp)) != 0))
return true;
}
/* not supported */
return false;
}
void mutt_ts_status(char *str)
{
/* If empty, do not set. To clear, use a single space. */
if (str == NULL || *str == '\0')
return;
fprintf(stderr, "%s%s%s", tsl, str, fsl);
}
void mutt_ts_icon(char *str)
{
/* If empty, do not set. To clear, use a single space. */
if (str == NULL || *str == '\0')
return;
/* icon setting is not supported in terminfo, so hardcode the escape - yuck */
fprintf(stderr, "\033]1;%s\007", str);
}
void index_make_entry(char *s, size_t l, struct Menu *menu, int num)
{
if (!Context || !menu || (num < 0) || (num >= Context->hdrmax))
return;
struct Header *h = Context->hdrs[Context->v2r[num]];
if (!h)
return;
enum FormatFlag flag = MUTT_FORMAT_MAKEPRINT | MUTT_FORMAT_ARROWCURSOR | MUTT_FORMAT_INDEX;
int edgemsgno, reverse = Sort & SORT_REVERSE;
struct MuttThread *tmp = NULL;
if ((Sort & SORT_MASK) == SORT_THREADS && h->tree)
{
flag |= MUTT_FORMAT_TREE; /* display the thread tree */
if (h->display_subject)
flag |= MUTT_FORMAT_FORCESUBJ;
else
{
if (reverse)
{
if (menu->top + menu->pagelen > menu->max)
edgemsgno = Context->v2r[menu->max - 1];
else
edgemsgno = Context->v2r[menu->top + menu->pagelen - 1];
}
else
edgemsgno = Context->v2r[menu->top];
for (tmp = h->thread->parent; tmp; tmp = tmp->parent)
{
if (!tmp->message)
continue;
/* if no ancestor is visible on current screen, provisionally force
* subject... */
if (reverse ? tmp->message->msgno > edgemsgno : tmp->message->msgno < edgemsgno)
{
flag |= MUTT_FORMAT_FORCESUBJ;
break;
}
else if (tmp->message->virtual >= 0)
break;
}
if (flag & MUTT_FORMAT_FORCESUBJ)
{
for (tmp = h->thread->prev; tmp; tmp = tmp->prev)
{
if (!tmp->message)
continue;
/* ...but if a previous sibling is available, don't force it */
if (reverse ? tmp->message->msgno > edgemsgno : tmp->message->msgno < edgemsgno)
break;
else if (tmp->message->virtual >= 0)
{
flag &= ~MUTT_FORMAT_FORCESUBJ;
break;
}
}
}
}
}
_mutt_make_string(s, l, NONULL(HdrFmt), Context, h, flag);
}
int index_color(int index_no)
{
if (!Context || (index_no < 0))
return 0;
struct Header *h = Context->hdrs[Context->v2r[index_no]];
if (h && h->pair)
return h->pair;
mutt_set_header_color(Context, h);
if (h)
return h->pair;
return 0;
}
/**
* mutt_draw_statusline - Draw a highlighted status bar
* @param cols Maximum number of screen columns
* @param buf Message to be displayed
* @param buflen Length of the buffer
*
* Users configure the highlighting of the status bar, e.g.
* color status red default "[0-9][0-9]:[0-9][0-9]"
*
* Where regexes overlap, the one nearest the start will be used.
* If two regexes start at the same place, the longer match will be used.
*/
void mutt_draw_statusline(int cols, const char *buf, int buflen)
{
int i = 0;
int offset = 0;
bool found = false;
int chunks = 0;
int len = 0;
struct Syntax
{
int color;
int first;
int last;
} *syntax = NULL;
if (!buf)
return;
do
{
struct ColorLine *cl = NULL;
found = false;
if (!buf[offset])
break;
/* loop through each "color status regex" */
for (cl = ColorStatusList; cl; cl = cl->next)
{
regmatch_t pmatch[cl->match + 1];
if (regexec(&cl->rx, buf + offset, cl->match + 1, pmatch, 0) != 0)
continue; /* regex doesn't match the status bar */
int first = pmatch[cl->match].rm_so + offset;
int last = pmatch[cl->match].rm_eo + offset;
if (first == last)
continue; /* ignore an empty regex */
if (!found)
{
chunks++;
safe_realloc(&syntax, chunks * sizeof(struct Syntax));
}
i = chunks - 1;
if (!found || (first < syntax[i].first) ||
((first == syntax[i].first) && (last > syntax[i].last)))
{
syntax[i].color = cl->pair;
syntax[i].first = first;
syntax[i].last = last;
}
found = true;
}
if (syntax)
{
offset = syntax[i].last;
}
} while (found);
/* Only 'len' bytes will fit into 'cols' screen columns */
len = mutt_wstr_trunc(buf, buflen, cols, NULL);
offset = 0;
if ((chunks > 0) && (syntax[0].first > 0))
{
/* Text before the first highlight */
addnstr(buf, MIN(len, syntax[0].first));
attrset(ColorDefs[MT_COLOR_STATUS]);
if (len <= syntax[0].first)
goto dsl_finish; /* no more room */
offset = syntax[0].first;
}
for (i = 0; i < chunks; i++)
{
/* Highlighted text */
attrset(syntax[i].color);
addnstr(buf + offset, MIN(len, syntax[i].last) - offset);
if (len <= syntax[i].last)
goto dsl_finish; /* no more room */
int next;
if ((i + 1) == chunks)
{
next = len;
}
else
{
next = MIN(len, syntax[i + 1].first);
}
attrset(ColorDefs[MT_COLOR_STATUS]);
offset = syntax[i].last;
addnstr(buf + offset, next - offset);
offset = next;
if (offset >= len)
goto dsl_finish; /* no more room */
}
attrset(ColorDefs[MT_COLOR_STATUS]);
if (offset < len)
{
/* Text after the last highlight */
addnstr(buf + offset, len - offset);
}
int width = mutt_strwidth(buf);
if (width < cols)
{
/* Pad the rest of the line with whitespace */
mutt_paddstr(cols - width, "");
}
dsl_finish:
FREE(&syntax);
}
static const struct Mapping IndexHelp[] = {
{ N_("Quit"), OP_QUIT },
{ N_("Del"), OP_DELETE },
{ N_("Undel"), OP_UNDELETE },
{ N_("Save"), OP_SAVE },
{ N_("Mail"), OP_MAIL },
{ N_("Reply"), OP_REPLY },
{ N_("Group"), OP_GROUP_REPLY },
{ N_("Help"), OP_HELP },
{ NULL, 0 },
};
#ifdef USE_NNTP
struct Mapping IndexNewsHelp[] = {
{ N_("Quit"), OP_QUIT },
{ N_("Del"), OP_DELETE },
{ N_("Undel"), OP_UNDELETE },
{ N_("Save"), OP_SAVE },
{ N_("Post"), OP_POST },
{ N_("Followup"), OP_FOLLOWUP },
{ N_("Catchup"), OP_CATCHUP },
{ N_("Help"), OP_HELP },
{ NULL, 0 },
};
#endif
static void index_menu_redraw(struct Menu *menu)
{
char buf[LONG_STRING];
if (menu->redraw & REDRAW_FULL)
{
menu_redraw_full(menu);
mutt_show_error();
}
#ifdef USE_SIDEBAR
if (menu->redraw & REDRAW_SIDEBAR)
{
mutt_sb_set_buffystats(Context);
menu_redraw_sidebar(menu);
}
#endif
if (Context && Context->hdrs && !(menu->current >= Context->vcount))
{
menu_check_recenter(menu);
if (menu->redraw & REDRAW_INDEX)
{
menu_redraw_index(menu);
menu->redraw |= REDRAW_STATUS;
}
else if (menu->redraw & (REDRAW_MOTION_RESYNCH | REDRAW_MOTION))
menu_redraw_motion(menu);
else if (menu->redraw & REDRAW_CURRENT)
menu_redraw_current(menu);
}
if (menu->redraw & REDRAW_STATUS)
{
menu_status_line(buf, sizeof(buf), menu, NONULL(Status));
mutt_window_move(MuttStatusWindow, 0, 0);
SETCOLOR(MT_COLOR_STATUS);
mutt_draw_statusline(MuttStatusWindow->cols, buf, sizeof(buf));
NORMAL_COLOR;
menu->redraw &= ~REDRAW_STATUS;
if (option(OPT_TS_ENABLED) && TSSupported)
{
menu_status_line(buf, sizeof(buf), menu, NONULL(TSStatusFormat));
mutt_ts_status(buf);
menu_status_line(buf, sizeof(buf), menu, NONULL(TSIconFormat));
mutt_ts_icon(buf);
}
}
menu->redraw = 0;
}
/**
* mutt_index_menu - Display a list of emails
*
* This function handles the message index window as well as commands returned
* from the pager (MENU_PAGER).
*/
int mutt_index_menu(void)
{
char buf[LONG_STRING], helpstr[LONG_STRING];
int flags;
int op = OP_NULL;
bool done = false; /* controls when to exit the "event" loop */
int i = 0, j;
bool tag = false; /* has the tag-prefix command been pressed? */
int newcount = -1;
int oldcount = -1;
int rc = -1;
struct Menu *menu = NULL;
char *cp = NULL; /* temporary variable. */
int index_hint; /* used to restore cursor position */
bool do_buffy_notify = true;
int close = 0; /* did we OP_QUIT or OP_EXIT out of this menu? */
int attach_msg = option(OPT_ATTACH_MSG);
menu = mutt_new_menu(MENU_MAIN);
menu->make_entry = index_make_entry;
menu->color = index_color;
menu->current = ci_first_message();
menu->help =
mutt_compile_help(helpstr, sizeof(helpstr), MENU_MAIN,
#ifdef USE_NNTP
(Context && (Context->magic == MUTT_NNTP)) ? IndexNewsHelp :
#endif
IndexHelp);
menu->custom_menu_redraw = index_menu_redraw;
mutt_push_current_menu(menu);
if (!attach_msg)
mutt_buffy_check(true); /* force the buffy check after we enter the folder */
if (((Sort & SORT_MASK) == SORT_THREADS) && option(OPT_COLLAPSE_ALL))
{
collapse_all(menu, 0);
menu->redraw = REDRAW_FULL;
}
while (true)
{
/* Clear the tag prefix unless we just started it. Don't clear
* the prefix on a timeout (op==-2), but do clear on an abort (op==-1)
*/
if (tag && op != OP_TAG_PREFIX && op != OP_TAG_PREFIX_COND && op != -2)
tag = false;
/* check if we need to resort the index because just about
* any 'op' below could do mutt_enter_command(), either here or
* from any new menu launched, and change $sort/$sort_aux
*/
if (option(OPT_NEED_RESORT) && Context && Context->msgcount && menu->current >= 0)
resort_index(menu);
menu->max = Context ? Context->vcount : 0;
oldcount = Context ? Context->msgcount : 0;
if (option(OPT_REDRAW_TREE) && Context && Context->msgcount && (Sort & SORT_MASK) == SORT_THREADS)
{
mutt_draw_tree(Context);
menu->redraw |= REDRAW_STATUS;
unset_option(OPT_REDRAW_TREE);
}
if (Context)
Context->menu = menu;
if (Context && !attach_msg)
{
int check;
/* check for new mail in the mailbox. If nonzero, then something has
* changed about the file (either we got new mail or the file was
* modified underneath us.)
*/
index_hint =
(Context->vcount && menu->current >= 0 && menu->current < Context->vcount) ?
CURHDR->index :
0;
if ((check = mx_check_mailbox(Context, &index_hint)) < 0)
{
if (!Context->path)
{
/* fatal error occurred */
FREE(&Context);
menu->redraw = REDRAW_FULL;
}
set_option(OPT_SEARCH_INVALID);
}
else if (check == MUTT_NEW_MAIL || check == MUTT_REOPENED || check == MUTT_FLAGS)