forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.c
3665 lines (3289 loc) · 109 KB
/
index.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/>.
*/
/**
* @page index2 GUI manage the main index (list of emails)
*
* GUI manage the main index (list of emails)
*/
#include "config.h"
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "mutt/mutt.h"
#include "config/lib.h"
#include "email/lib.h"
#include "conn/conn.h"
#include "mutt.h"
#include "index.h"
#include "account.h"
#include "alias.h"
#include "browser.h"
#include "commands.h"
#include "context.h"
#include "curs_lib.h"
#include "format_flags.h"
#include "globals.h"
#include "hdrline.h"
#include "hook.h"
#include "keymap.h"
#include "mailbox.h"
#include "mutt_account.h"
#include "mutt_curses.h"
#include "mutt_header.h"
#include "mutt_logging.h"
#include "mutt_menu.h"
#include "mutt_thread.h"
#include "mutt_window.h"
#include "muttlib.h"
#include "mx.h"
#include "ncrypt/ncrypt.h"
#include "opcodes.h"
#include "options.h"
#include "pager.h"
#include "pattern.h"
#include "progress.h"
#include "protos.h"
#include "query.h"
#include "recvattach.h"
#include "score.h"
#include "send.h"
#include "sort.h"
#include "status.h"
#include "terminal.h"
#ifdef USE_SIDEBAR
#include "sidebar.h"
#endif
#ifdef USE_POP
#include "pop/pop.h"
#endif
#ifdef USE_IMAP
#include "imap/imap.h"
#endif
#ifdef USE_NOTMUCH
#include "notmuch/mutt_notmuch.h"
#endif
#ifdef USE_NNTP
#include "nntp/nntp.h"
#endif
#ifdef ENABLE_NLS
#include <libintl.h>
#endif
#ifdef USE_INOTIFY
#include "monitor.h"
#endif
/* These Config Variables are only used in index.c */
bool C_ChangeFolderNext; ///< Config: Suggest the next folder, rather than the first when using '<change-folder>'
bool C_CollapseAll; ///< Config: Collapse all threads when entering a folder
bool C_CollapseFlagged; ///< Config: Prevent the collapse of threads with flagged emails
bool C_CollapseUnread; ///< Config: Prevent the collapse of threads with unread emails
char *C_MarkMacroPrefix; ///< Config: Prefix for macros using '<mark-message>'
bool C_PgpAutoDecode; ///< Config: Automatically decrypt PGP messages
bool C_UncollapseJump; ///< Config: When opening a thread, jump to the next unread message
bool C_UncollapseNew; ///< Config: Open collapsed threads when new mail arrives
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
#define CUR_EMAIL Context->mailbox->emails[Context->mailbox->v2r[menu->current]]
#define UNREAD(email) mutt_thread_contains_unread(Context, email)
#define FLAGGED(email) mutt_thread_contains_flagged(Context, email)
#define CAN_COLLAPSE(email) \
((C_CollapseUnread || !UNREAD(email)) && (C_CollapseFlagged || !FLAGGED(email)))
// clang-format off
/**
* typedef CheckFlags - Checks to perform before running a function
*/
typedef uint8_t CheckFlags; ///< Flags, e.g. #CHECK_IN_MAILBOX
#define CHECK_NO_FLAGS 0 ///< No flags are set
#define CHECK_IN_MAILBOX (1 << 0) ///< Is there a mailbox open?
#define CHECK_MSGCOUNT (1 << 1) ///< Are there any messages?
#define CHECK_VISIBLE (1 << 2) ///< Is the selected message visible in the index?
#define CHECK_READONLY (1 << 3) ///< Is the mailbox readonly?
#define CHECK_ATTACH (1 << 4) ///< Is the user in message-attach mode?
// clang-format on
/**
* prereq - Check the pre-requisites for a function
* @param ctx Mailbox
* @param menu Current Menu
* @param checks Checks to perform, see #CheckFlags
* @retval bool true if the checks pass successfully
*/
static bool prereq(struct Context *ctx, struct Menu *menu, CheckFlags checks)
{
bool result = true;
if (checks & (CHECK_MSGCOUNT | CHECK_VISIBLE | CHECK_READONLY))
checks |= CHECK_IN_MAILBOX;
if ((checks & CHECK_IN_MAILBOX) && !Context)
{
mutt_error(_("No mailbox is open"));
result = false;
}
if (result && (checks & CHECK_MSGCOUNT) && (Context->mailbox->msg_count == 0))
{
mutt_error(_("There are no messages"));
result = false;
}
if (result && (checks & CHECK_VISIBLE) && (menu->current >= Context->mailbox->vcount))
{
mutt_error(_("No visible messages"));
result = false;
}
if (result && (checks & CHECK_READONLY) && Context->mailbox->readonly)
{
mutt_error(_("Mailbox is read-only"));
result = false;
}
if (result && (checks & CHECK_ATTACH) && OptAttachMsg)
{
mutt_error(_("Function not permitted in attach-message mode"));
result = false;
}
if (!result)
mutt_flushinp();
return result;
}
/**
* check_acl - Check the ACLs for a function
* @param ctx Mailbox
* @param acl ACL, see #AclFlags
* @param msg Error message for failure
* @retval bool true if the function is permitted
*/
static bool check_acl(struct Context *ctx, AclFlags acl, const char *msg)
{
if (!ctx || !ctx->mailbox)
return false;
if (!(ctx->mailbox->rights & acl))
{
/* L10N: %s is one of the CHECK_ACL entries below. */
mutt_error(_("%s: Operation not permitted by ACL"), msg);
return false;
}
return true;
}
/**
* collapse_all - 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 #C_CollapseAll 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 Email *e = NULL, *base = NULL;
struct MuttThread *thread = NULL, *top = NULL;
int final;
if (!Context || (Context->mailbox->msg_count == 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 (CUR_EMAIL->collapsed && toggle)
final = mutt_uncollapse_thread(Context, CUR_EMAIL);
else if (CAN_COLLAPSE(CUR_EMAIL))
final = mutt_collapse_thread(Context, CUR_EMAIL);
else
final = CUR_EMAIL->virtual;
base = Context->mailbox->emails[Context->mailbox->v2r[final]];
/* Iterate all threads, perform collapse/uncollapse as needed */
top = Context->tree;
Context->collapsed = toggle ? !Context->collapsed : true;
while ((thread = top))
{
while (!thread->message)
thread = thread->child;
e = thread->message;
if (e->collapsed != Context->collapsed)
{
if (e->collapsed)
mutt_uncollapse_thread(Context, e);
else if (CAN_COLLAPSE(e))
mutt_collapse_thread(Context, e);
}
top = top->next;
}
/* Restore the cursor */
mutt_set_virtual(Context);
for (int j = 0; j < Context->mailbox->vcount; j++)
{
if (Context->mailbox->emails[Context->mailbox->v2r[j]]->index == base->index)
{
menu->current = j;
break;
}
}
menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
}
/**
* ci_next_undeleted - Find the next undeleted email
* @param msgno Message number to start at
* @retval >=0 Message number of next undeleted email
* @retval -1 No more undeleted messages
*/
static int ci_next_undeleted(int msgno)
{
for (int i = msgno + 1; i < Context->mailbox->vcount; i++)
if (!Context->mailbox->emails[Context->mailbox->v2r[i]]->deleted)
return i;
return -1;
}
/**
* ci_previous_undeleted - Find the previous undeleted email
* @param msgno Message number to start at
* @retval >=0 Message number of next undeleted email
* @retval -1 No more undeleted messages
*/
static int ci_previous_undeleted(int msgno)
{
for (int i = msgno - 1; i >= 0; i--)
if (!Context->mailbox->emails[Context->mailbox->v2r[i]]->deleted)
return i;
return -1;
}
/**
* ci_first_message - Get index of first new message
* @retval num 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)
{
if (!Context || (Context->mailbox->msg_count == 0))
return 0;
int old = -1;
for (int i = 0; i < Context->mailbox->vcount; i++)
{
if (!Context->mailbox->emails[Context->mailbox->v2r[i]]->read &&
!Context->mailbox->emails[Context->mailbox->v2r[i]]->deleted)
{
if (!Context->mailbox->emails[Context->mailbox->v2r[i]]->old)
return i;
else if (old == -1)
old = i;
}
}
if (old != -1)
return old;
/* If C_Sort is reverse and not threaded, the latest message is first.
* If C_Sort is threaded, the latest message is first if exactly one
* of C_Sort and C_SortAux are reverse. */
if (((C_Sort & SORT_REVERSE) && ((C_Sort & SORT_MASK) != SORT_THREADS)) ||
(((C_Sort & SORT_MASK) == SORT_THREADS) && ((C_Sort ^ C_SortAux) & SORT_REVERSE)))
{
return 0;
}
else
{
return Context->mailbox->vcount ? Context->mailbox->vcount - 1 : 0;
}
return 0;
}
/**
* mx_toggle_write - Toggle the mailbox's readonly flag
* @param m Mailbox
* @retval 0 Success
* @retval -1 Error
*
* This should be in mx.c, but it only gets used here.
*/
static int mx_toggle_write(struct Mailbox *m)
{
if (!m)
return -1;
if (m->readonly)
{
mutt_error(_("Cannot toggle write on a readonly mailbox"));
return -1;
}
if (m->dontwrite)
{
m->dontwrite = false;
mutt_message(_("Changes to folder will be written on folder exit"));
}
else
{
m->dontwrite = true;
mutt_message(_("Changes to folder will not be written"));
}
return 0;
}
/**
* resort_index - Resort the index
* @param menu Current Menu
*/
static void resort_index(struct Menu *menu)
{
struct Email *current = CUR_EMAIL;
menu->current = -1;
mutt_sort_headers(Context, false);
/* Restore the current message */
for (int i = 0; i < Context->mailbox->vcount; i++)
{
if (Context->mailbox->emails[Context->mailbox->v2r[i]] == current)
{
menu->current = i;
break;
}
}
if (((C_Sort & SORT_MASK) == SORT_THREADS) && (menu->current < 0))
menu->current = mutt_parent_message(Context, current, false);
if (menu->current < 0)
menu->current = ci_first_message();
menu->redraw |= REDRAW_INDEX | REDRAW_STATUS;
}
/**
* update_index_threaded - Update the index (if threaded)
* @param ctx Mailbox
* @param check Flags, e.g. #MUTT_REOPENED
* @param oldcount How many items are currently in the index
*/
static void update_index_threaded(struct Context *ctx, int check, int oldcount)
{
struct Email **save_new = NULL;
/* save the list of new messages */
if ((check != MUTT_REOPENED) && oldcount && (ctx->pattern || C_UncollapseNew))
{
save_new = mutt_mem_malloc(sizeof(struct Email *) * (ctx->mailbox->msg_count - oldcount));
for (int i = oldcount; i < ctx->mailbox->msg_count; i++)
save_new[i - oldcount] = ctx->mailbox->emails[i];
}
/* Sort first to thread the new messages, because some patterns
* require the threading information.
*
* If the mailbox was reopened, need to rethread from scratch. */
mutt_sort_headers(ctx, (check == MUTT_REOPENED));
if (ctx->pattern)
{
for (int i = (check == MUTT_REOPENED) ? 0 : oldcount; i < ctx->mailbox->msg_count; i++)
{
struct Email *e = NULL;
if ((check != MUTT_REOPENED) && oldcount)
e = save_new[i - oldcount];
else
e = ctx->mailbox->emails[i];
if (mutt_pattern_exec(SLIST_FIRST(ctx->limit_pattern),
MUTT_MATCH_FULL_ADDRESS, ctx->mailbox, e, NULL))
{
/* virtual will get properly set by mutt_set_virtual(), which
* is called by mutt_sort_headers() just below. */
e->virtual = 1;
e->limited = true;
}
}
/* Need a second sort to set virtual numbers and redraw the tree */
mutt_sort_headers(ctx, false);
}
/* uncollapse threads with new mail */
if (C_UncollapseNew)
{
if (check == MUTT_REOPENED)
{
ctx->collapsed = false;
for (struct MuttThread *h = ctx->tree; h; h = h->next)
{
struct MuttThread *j = h;
for (; !j->message; j = j->child)
;
mutt_uncollapse_thread(ctx, j->message);
}
mutt_set_virtual(ctx);
}
else if (oldcount)
{
for (int j = 0; j < (ctx->mailbox->msg_count - oldcount); j++)
{
if (!ctx->pattern || save_new[j]->limited)
{
mutt_uncollapse_thread(ctx, save_new[j]);
}
}
mutt_set_virtual(ctx);
}
}
FREE(&save_new);
}
/**
* update_index_unthreaded - Update the index (if unthreaded)
* @param ctx Mailbox
* @param check Flags, e.g. #MUTT_REOPENED
* @param oldcount How many items are currently in the index
*/
static void update_index_unthreaded(struct Context *ctx, int check, int oldcount)
{
/* 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)
{
int padding = mx_msg_padding_size(ctx->mailbox);
for (int i = (check == MUTT_REOPENED) ? 0 : oldcount; i < ctx->mailbox->msg_count; i++)
{
if (!i)
{
ctx->mailbox->vcount = 0;
ctx->vsize = 0;
}
if (mutt_pattern_exec(SLIST_FIRST(ctx->limit_pattern), MUTT_MATCH_FULL_ADDRESS,
ctx->mailbox, ctx->mailbox->emails[i], NULL))
{
assert(ctx->mailbox->vcount < ctx->mailbox->msg_count);
ctx->mailbox->emails[i]->virtual = ctx->mailbox->vcount;
ctx->mailbox->v2r[ctx->mailbox->vcount] = i;
ctx->mailbox->emails[i]->limited = true;
ctx->mailbox->vcount++;
struct Body *b = ctx->mailbox->emails[i]->content;
ctx->vsize += b->length + b->offset - b->hdr_offset + padding;
}
}
}
/* if the mailbox was reopened, need to rethread from scratch */
mutt_sort_headers(ctx, (check == MUTT_REOPENED));
}
/**
* update_index - Update the index
* @param menu Current Menu
* @param ctx Mailbox
* @param check Flags, e.g. #MUTT_REOPENED
* @param oldcount How many items are currently in the index
* @param index_hint Remember our place in the index
*/
void update_index(struct Menu *menu, struct Context *ctx, int check, int oldcount, int index_hint)
{
if (!menu || !ctx)
return;
/* take note of the current message */
if (oldcount)
{
if (menu->current < ctx->mailbox->vcount)
menu->oldcurrent = index_hint;
else
oldcount = 0; /* invalid message number! */
}
if ((C_Sort & SORT_MASK) == SORT_THREADS)
update_index_threaded(ctx, check, oldcount);
else
update_index_unthreaded(ctx, check, oldcount);
menu->current = -1;
if (oldcount)
{
/* restore the current message to the message it was pointing to */
for (int i = 0; i < ctx->mailbox->vcount; i++)
{
if (ctx->mailbox->emails[ctx->mailbox->v2r[i]]->index == menu->oldcurrent)
{
menu->current = i;
break;
}
}
}
if (menu->current < 0)
menu->current = ci_first_message();
}
/**
* main_change_folder - Change to a different mailbox
* @param menu Current Menu
* @param m Mailbox
* @param op Operation, e.g. OP_MAIN_CHANGE_FOLDER_READONLY
* @param buf Folder to change to
* @param buflen Length of buffer
* @param oldcount How many items are currently in the index
* @param index_hint Remember our place in the index
* @retval 0 Success
* @retval -1 Error
*/
static int main_change_folder(struct Menu *menu, int op, struct Mailbox *m,
char *buf, size_t buflen, int *oldcount, int *index_hint)
{
#ifdef USE_NNTP
if (OptNews)
{
OptNews = false;
nntp_expand_path(buf, buflen, &CurrentNewsSrv->conn->account);
}
else
#endif
{
mx_path_canon(buf, buflen, C_Folder, NULL);
}
enum MailboxType magic = mx_path_probe(buf, NULL);
if ((magic == MUTT_MAILBOX_ERROR) || (magic == MUTT_UNKNOWN))
{
// Try to see if the buffer matches a description before we bail.
// We'll receive a non-null pointer if there is a corresponding mailbox.
m = mutt_find_mailbox_desc(buf);
if (m)
{
magic = m->magic;
mutt_str_strfcpy(buf, m->path, buflen);
}
else
{
// Bail.
mutt_error(_("%s is not a mailbox"), buf);
return -1;
}
}
/* keepalive failure in mutt_enter_fname may kill connection. #3028 */
if (Context && (Context->mailbox->path[0] == '\0'))
ctx_free(&Context);
if (Context)
{
char *new_last_folder = NULL;
#ifdef USE_INOTIFY
int monitor_remove_rc = mutt_monitor_remove(NULL);
#endif
#ifdef USE_COMPRESSED
if (Context->mailbox->compress_info && (Context->mailbox->realpath[0] != '\0'))
new_last_folder = mutt_str_strdup(Context->mailbox->realpath);
else
#endif
new_last_folder = mutt_str_strdup(Context->mailbox->path);
*oldcount = Context ? Context->mailbox->msg_count : 0;
int check = mx_mbox_close(&Context);
if (check != 0)
{
#ifdef USE_INOTIFY
if (monitor_remove_rc == 0)
mutt_monitor_add(NULL);
#endif
if ((check == MUTT_NEW_MAIL) || (check == MUTT_REOPENED))
update_index(menu, Context, check, *oldcount, *index_hint);
FREE(&new_last_folder);
OptSearchInvalid = true;
menu->redraw |= REDRAW_INDEX | REDRAW_STATUS;
return 0;
}
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, m ? m->desc : NULL);
const int flags = (C_ReadOnly || (op == OP_MAIN_CHANGE_FOLDER_READONLY)
#ifdef USE_NOTMUCH
|| (op == OP_MAIN_VFOLDER_FROM_QUERY_READONLY)
#endif
) ?
MUTT_READONLY :
MUTT_OPEN_NO_FLAGS;
bool free_m = false;
if (!m)
{
m = mx_path_resolve(buf);
free_m = true;
}
Context = mx_mbox_open(m, flags);
if (Context)
{
menu->current = ci_first_message();
#ifdef USE_INOTIFY
mutt_monitor_add(NULL);
#endif
}
else
{
menu->current = 0;
if (free_m)
mailbox_free(&m);
}
if (((C_Sort & SORT_MASK) == SORT_THREADS) && C_CollapseAll)
collapse_all(menu, 0);
#ifdef USE_SIDEBAR
mutt_sb_set_open_mailbox();
#endif
mutt_clear_error();
mutt_mailbox_check(Context ? Context->mailbox : NULL, MUTT_MAILBOX_CHECK_FORCE); /* force the mailbox check after we have changed the folder */
menu->redraw = REDRAW_FULL;
OptSearchInvalid = true;
return 0;
}
/**
* index_make_entry - Format a menu item for the index list - Implements Menu::menu_make_entry()
*/
void index_make_entry(char *buf, size_t buflen, struct Menu *menu, int line)
{
if (!Context || !menu || (line < 0) || (line >= Context->mailbox->email_max))
return;
struct Email *e = Context->mailbox->emails[Context->mailbox->v2r[line]];
if (!e)
return;
MuttFormatFlags flags = MUTT_FORMAT_ARROWCURSOR | MUTT_FORMAT_INDEX;
struct MuttThread *tmp = NULL;
if (((C_Sort & SORT_MASK) == SORT_THREADS) && e->tree)
{
flags |= MUTT_FORMAT_TREE; /* display the thread tree */
if (e->display_subject)
flags |= MUTT_FORMAT_FORCESUBJ;
else
{
const int reverse = C_Sort & SORT_REVERSE;
int edgemsgno;
if (reverse)
{
if (menu->top + menu->pagelen > menu->max)
edgemsgno = Context->mailbox->v2r[menu->max - 1];
else
edgemsgno = Context->mailbox->v2r[menu->top + menu->pagelen - 1];
}
else
edgemsgno = Context->mailbox->v2r[menu->top];
for (tmp = e->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))
{
flags |= MUTT_FORMAT_FORCESUBJ;
break;
}
else if (tmp->message->virtual >= 0)
break;
}
if (flags & MUTT_FORMAT_FORCESUBJ)
{
for (tmp = e->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)
{
flags &= ~MUTT_FORMAT_FORCESUBJ;
break;
}
}
}
}
}
mutt_make_string_flags(buf, buflen, NONULL(C_IndexFormat), Context,
Context->mailbox, e, flags);
}
/**
* index_color - Calculate the colour for a line of the index - Implements Menu::menu_color()
*/
int index_color(int line)
{
if (!Context || (line < 0))
return 0;
struct Email *e = Context->mailbox->emails[Context->mailbox->v2r[line]];
if (e && e->pair)
return e->pair;
mutt_set_header_color(Context->mailbox, e);
if (e)
return e->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, size_t buflen)
{
size_t i = 0;
size_t offset = 0;
bool found = false;
size_t chunks = 0;
size_t len = 0;
struct Syntax
{
int color;
int first;
int last;
} *syntax = NULL;
if (!buf || !stdscr)
return;
do
{
struct ColorLine *cl = NULL;
found = false;
if (!buf[offset])
break;
/* loop through each "color status regex" */
STAILQ_FOREACH(cl, &ColorStatusList, entries)
{
regmatch_t pmatch[cl->match + 1];
if (regexec(&cl->regex, 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++;
mutt_mem_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 */
size_t 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);
}
/**
* index_custom_redraw - Redraw the index - Implements Menu::menu_custom_redraw()
*/
static void index_custom_redraw(struct Menu *menu)
{
if (menu->redraw & REDRAW_FULL)
{
menu_redraw_full(menu);
mutt_show_error();
}
#ifdef USE_SIDEBAR
if (menu->redraw & REDRAW_SIDEBAR)
menu_redraw_sidebar(menu);
#endif
if (Context && Context->mailbox->emails && !(menu->current >= Context->mailbox->vcount))
{
menu_check_recenter(menu);
if (menu->redraw & REDRAW_INDEX)
{
menu_redraw_index(menu);
menu->redraw |= REDRAW_STATUS;
}
else if (menu->redraw & (REDRAW_MOTION_RESYNC | REDRAW_MOTION))
menu_redraw_motion(menu);
else if (menu->redraw & REDRAW_CURRENT)
menu_redraw_current(menu);
}
if (menu->redraw & REDRAW_STATUS)
{
char buf[1024];
menu_status_line(buf, sizeof(buf), menu, NONULL(C_StatusFormat));
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 (C_TsEnabled && TsSupported)
{