-
Notifications
You must be signed in to change notification settings - Fork 0
/
tig.c
8402 lines (6845 loc) · 195 KB
/
tig.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
/* Copyright (c) 2006-2012 Jonas Fonseca <fonseca@diku.dk>
*
* 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.
*/
#include "tig.h"
#include "io.h"
#include "refs.h"
#include "graph.h"
#include "git.h"
static void TIG_NORETURN die(const char *err, ...) PRINTF_LIKE(1, 2);
static void warn(const char *msg, ...) PRINTF_LIKE(1, 2);
static void report(const char *msg, ...) PRINTF_LIKE(1, 2);
#define report_clear() report("%s", "")
enum input_status {
INPUT_OK,
INPUT_SKIP,
INPUT_STOP,
INPUT_CANCEL
};
typedef enum input_status (*input_handler)(void *data, char *buf, int c);
static char *prompt_input(const char *prompt, input_handler handler, void *data);
static bool prompt_yesno(const char *prompt);
static char *read_prompt(const char *prompt);
struct menu_item {
int hotkey;
const char *text;
void *data;
};
static bool prompt_menu(const char *prompt, const struct menu_item *items, int *selected);
#define GRAPHIC_ENUM(_) \
_(GRAPHIC, ASCII), \
_(GRAPHIC, DEFAULT), \
_(GRAPHIC, UTF_8)
DEFINE_ENUM(graphic, GRAPHIC_ENUM);
#define DATE_ENUM(_) \
_(DATE, NO), \
_(DATE, DEFAULT), \
_(DATE, LOCAL), \
_(DATE, RELATIVE), \
_(DATE, SHORT)
DEFINE_ENUM(date, DATE_ENUM);
struct time {
time_t sec;
int tz;
};
static inline int timecmp(const struct time *t1, const struct time *t2)
{
return t1->sec - t2->sec;
}
static const char *
mkdate(const struct time *time, enum date date)
{
static char buf[DATE_WIDTH + 1];
static const struct enum_map reldate[] = {
{ "second", 1, 60 * 2 },
{ "minute", 60, 60 * 60 * 2 },
{ "hour", 60 * 60, 60 * 60 * 24 * 2 },
{ "day", 60 * 60 * 24, 60 * 60 * 24 * 7 * 2 },
{ "week", 60 * 60 * 24 * 7, 60 * 60 * 24 * 7 * 5 },
{ "month", 60 * 60 * 24 * 30, 60 * 60 * 24 * 365 },
{ "year", 60 * 60 * 24 * 365, 0 },
};
struct tm tm;
if (!date || !time || !time->sec)
return "";
if (date == DATE_RELATIVE) {
struct timeval now;
time_t date = time->sec + time->tz;
time_t seconds;
int i;
gettimeofday(&now, NULL);
seconds = now.tv_sec < date ? date - now.tv_sec : now.tv_sec - date;
for (i = 0; i < ARRAY_SIZE(reldate); i++) {
if (seconds >= reldate[i].value && reldate[i].value)
continue;
seconds /= reldate[i].namelen;
if (!string_format(buf, "%ld %s%s %s",
seconds, reldate[i].name,
seconds > 1 ? "s" : "",
now.tv_sec >= date ? "ago" : "ahead"))
break;
return buf;
}
}
if (date == DATE_LOCAL) {
time_t date = time->sec + time->tz;
localtime_r(&date, &tm);
}
else {
gmtime_r(&time->sec, &tm);
}
return strftime(buf, sizeof(buf), DATE_FORMAT, &tm) ? buf : NULL;
}
#define AUTHOR_ENUM(_) \
_(AUTHOR, NO), \
_(AUTHOR, FULL), \
_(AUTHOR, ABBREVIATED)
DEFINE_ENUM(author, AUTHOR_ENUM);
static const char *
get_author_initials(const char *author)
{
static char initials[AUTHOR_WIDTH * 6 + 1];
size_t pos = 0;
const char *end = strchr(author, '\0');
#define is_initial_sep(c) (isspace(c) || ispunct(c) || (c) == '@' || (c) == '-')
memset(initials, 0, sizeof(initials));
while (author < end) {
unsigned char bytes;
size_t i;
while (author < end && is_initial_sep(*author))
author++;
bytes = utf8_char_length(author, end);
if (bytes >= sizeof(initials) - 1 - pos)
break;
while (bytes--) {
initials[pos++] = *author++;
}
i = pos;
while (author < end && !is_initial_sep(*author)) {
bytes = utf8_char_length(author, end);
if (bytes >= sizeof(initials) - 1 - i) {
while (author < end && !is_initial_sep(*author))
author++;
break;
}
while (bytes--) {
initials[i++] = *author++;
}
}
initials[i++] = 0;
}
return initials;
}
#define author_trim(cols) (cols == 0 || cols > 10)
static const char *
mkauthor(const char *text, int cols, enum author author)
{
bool trim = author_trim(cols);
bool abbreviate = author == AUTHOR_ABBREVIATED || !trim;
if (author == AUTHOR_NO)
return "";
if (abbreviate && text)
return get_author_initials(text);
return text;
}
static const char *
mkmode(mode_t mode)
{
if (S_ISDIR(mode))
return "drwxr-xr-x";
else if (S_ISLNK(mode))
return "lrwxrwxrwx";
else if (S_ISGITLINK(mode))
return "m---------";
else if (S_ISREG(mode) && mode & S_IXUSR)
return "-rwxr-xr-x";
else if (S_ISREG(mode))
return "-rw-r--r--";
else
return "----------";
}
#define FILENAME_ENUM(_) \
_(FILENAME, NO), \
_(FILENAME, ALWAYS), \
_(FILENAME, AUTO)
DEFINE_ENUM(filename, FILENAME_ENUM);
#define IGNORE_SPACE_ENUM(_) \
_(IGNORE_SPACE, NO), \
_(IGNORE_SPACE, ALL), \
_(IGNORE_SPACE, SOME), \
_(IGNORE_SPACE, AT_EOL)
DEFINE_ENUM(ignore_space, IGNORE_SPACE_ENUM);
#define COMMIT_ORDER_ENUM(_) \
_(COMMIT_ORDER, DEFAULT), \
_(COMMIT_ORDER, TOPO), \
_(COMMIT_ORDER, DATE), \
_(COMMIT_ORDER, REVERSE)
DEFINE_ENUM(commit_order, COMMIT_ORDER_ENUM);
#define VIEW_INFO(_) \
_(MAIN, main, ref_head), \
_(DIFF, diff, ref_commit), \
_(LOG, log, ref_head), \
_(TREE, tree, ref_commit), \
_(BLOB, blob, ref_blob), \
_(BLAME, blame, ref_commit), \
_(BRANCH, branch, ref_head), \
_(HELP, help, ""), \
_(PAGER, pager, ""), \
_(STATUS, status, "status"), \
_(STAGE, stage, ref_status)
static struct encoding *
get_path_encoding(const char *path, struct encoding *default_encoding)
{
const char *check_attr_argv[] = {
"git", "check-attr", "encoding", "--", path, NULL
};
char buf[SIZEOF_STR];
char *encoding;
/* <path>: encoding: <encoding> */
if (!*path || !io_run_buf(check_attr_argv, buf, sizeof(buf))
|| !(encoding = strstr(buf, ENCODING_SEP)))
return default_encoding;
encoding += STRING_SIZE(ENCODING_SEP);
if (!strcmp(encoding, ENCODING_UTF8)
|| !strcmp(encoding, "unspecified")
|| !strcmp(encoding, "set"))
return default_encoding;
return encoding_open(encoding);
}
/*
* User requests
*/
#define VIEW_REQ(id, name, ref) REQ_(VIEW_##id, "Show " #name " view")
#define REQ_INFO \
REQ_GROUP("View switching") \
VIEW_INFO(VIEW_REQ), \
\
REQ_GROUP("View manipulation") \
REQ_(ENTER, "Enter current line and scroll"), \
REQ_(NEXT, "Move to next"), \
REQ_(PREVIOUS, "Move to previous"), \
REQ_(PARENT, "Move to parent"), \
REQ_(VIEW_NEXT, "Move focus to next view"), \
REQ_(REFRESH, "Reload and refresh"), \
REQ_(MAXIMIZE, "Maximize the current view"), \
REQ_(VIEW_CLOSE, "Close the current view"), \
REQ_(QUIT, "Close all views and quit"), \
\
REQ_GROUP("View specific requests") \
REQ_(STATUS_UPDATE, "Update file status"), \
REQ_(STATUS_REVERT, "Revert file changes"), \
REQ_(STATUS_MERGE, "Merge file using external tool"), \
REQ_(STAGE_UPDATE_LINE, "Update single line"), \
REQ_(STAGE_NEXT, "Find next chunk to stage"), \
REQ_(DIFF_CONTEXT_DOWN, "Decrease the diff context"), \
REQ_(DIFF_CONTEXT_UP, "Increase the diff context"), \
\
REQ_GROUP("Cursor navigation") \
REQ_(MOVE_UP, "Move cursor one line up"), \
REQ_(MOVE_DOWN, "Move cursor one line down"), \
REQ_(MOVE_PAGE_DOWN, "Move cursor one page down"), \
REQ_(MOVE_PAGE_UP, "Move cursor one page up"), \
REQ_(MOVE_FIRST_LINE, "Move cursor to first line"), \
REQ_(MOVE_LAST_LINE, "Move cursor to last line"), \
\
REQ_GROUP("Scrolling") \
REQ_(SCROLL_FIRST_COL, "Scroll to the first line columns"), \
REQ_(SCROLL_LEFT, "Scroll two columns left"), \
REQ_(SCROLL_RIGHT, "Scroll two columns right"), \
REQ_(SCROLL_LINE_UP, "Scroll one line up"), \
REQ_(SCROLL_LINE_DOWN, "Scroll one line down"), \
REQ_(SCROLL_PAGE_UP, "Scroll one page up"), \
REQ_(SCROLL_PAGE_DOWN, "Scroll one page down"), \
\
REQ_GROUP("Searching") \
REQ_(SEARCH, "Search the view"), \
REQ_(SEARCH_BACK, "Search backwards in the view"), \
REQ_(FIND_NEXT, "Find next search match"), \
REQ_(FIND_PREV, "Find previous search match"), \
\
REQ_GROUP("Option manipulation") \
REQ_(OPTIONS, "Open option menu"), \
REQ_(TOGGLE_LINENO, "Toggle line numbers"), \
REQ_(TOGGLE_DATE, "Toggle date display"), \
REQ_(TOGGLE_AUTHOR, "Toggle author display"), \
REQ_(TOGGLE_REV_GRAPH, "Toggle revision graph visualization"), \
REQ_(TOGGLE_GRAPHIC, "Toggle (line) graphics mode"), \
REQ_(TOGGLE_FILENAME, "Toggle file name display"), \
REQ_(TOGGLE_REFS, "Toggle reference display (tags/branches)"), \
REQ_(TOGGLE_CHANGES, "Toggle local changes display in the main view"), \
REQ_(TOGGLE_SORT_ORDER, "Toggle ascending/descending sort order"), \
REQ_(TOGGLE_SORT_FIELD, "Toggle field to sort by"), \
REQ_(TOGGLE_IGNORE_SPACE, "Toggle ignoring whitespace in diffs"), \
REQ_(TOGGLE_COMMIT_ORDER, "Toggle commit ordering"), \
REQ_(TOGGLE_ID, "Toggle commit ID display"), \
\
REQ_GROUP("Misc") \
REQ_(PROMPT, "Bring up the prompt"), \
REQ_(SCREEN_REDRAW, "Redraw the screen"), \
REQ_(SHOW_VERSION, "Show version information"), \
REQ_(STOP_LOADING, "Stop all loading views"), \
REQ_(EDIT, "Open in editor"), \
REQ_(NONE, "Do nothing")
/* User action requests. */
enum request {
#define REQ_GROUP(help)
#define REQ_(req, help) REQ_##req
/* Offset all requests to avoid conflicts with ncurses getch values. */
REQ_UNKNOWN = KEY_MAX + 1,
REQ_OFFSET,
REQ_INFO,
/* Internal requests. */
REQ_JUMP_COMMIT,
#undef REQ_GROUP
#undef REQ_
};
struct request_info {
enum request request;
const char *name;
int namelen;
const char *help;
};
static const struct request_info req_info[] = {
#define REQ_GROUP(help) { 0, NULL, 0, (help) },
#define REQ_(req, help) { REQ_##req, (#req), STRING_SIZE(#req), (help) }
REQ_INFO
#undef REQ_GROUP
#undef REQ_
};
static enum request
get_request(const char *name)
{
int namelen = strlen(name);
int i;
for (i = 0; i < ARRAY_SIZE(req_info); i++)
if (enum_equals(req_info[i], name, namelen))
return req_info[i].request;
return REQ_UNKNOWN;
}
/*
* Options
*/
/* Option and state variables. */
static enum graphic opt_line_graphics = GRAPHIC_DEFAULT;
static enum date opt_date = DATE_DEFAULT;
static enum author opt_author = AUTHOR_FULL;
static enum filename opt_filename = FILENAME_AUTO;
static bool opt_rev_graph = TRUE;
static bool opt_line_number = FALSE;
static bool opt_show_refs = TRUE;
static bool opt_show_changes = TRUE;
static bool opt_untracked_dirs_content = TRUE;
static bool opt_read_git_colors = TRUE;
static bool opt_wrap_lines = FALSE;
static bool opt_ignore_case = FALSE;
static bool opt_stdin = FALSE;
static bool opt_focus_child = TRUE;
static int opt_diff_context = 3;
static char opt_diff_context_arg[9] = "";
static enum ignore_space opt_ignore_space = IGNORE_SPACE_NO;
static char opt_ignore_space_arg[22] = "";
static enum commit_order opt_commit_order = COMMIT_ORDER_DEFAULT;
static char opt_commit_order_arg[22] = "";
static bool opt_notes = TRUE;
static char opt_notes_arg[SIZEOF_STR] = "--show-notes";
static int opt_num_interval = 5;
static double opt_hscroll = 0.50;
static double opt_scale_split_view = 2.0 / 3.0;
static double opt_scale_vsplit_view = 0.5;
static bool opt_vsplit = FALSE;
static int opt_tab_size = 8;
static int opt_author_width = AUTHOR_WIDTH;
static int opt_filename_width = FILENAME_WIDTH;
static char opt_path[SIZEOF_STR] = "";
static char opt_file[SIZEOF_STR] = "";
static char opt_ref[SIZEOF_REF] = "";
static unsigned long opt_goto_line = 0;
static char opt_head[SIZEOF_REF] = "";
static char opt_remote[SIZEOF_REF] = "";
static struct encoding *opt_encoding = NULL;
static char opt_encoding_arg[SIZEOF_STR] = ENCODING_ARG;
static iconv_t opt_iconv_out = ICONV_NONE;
static char opt_search[SIZEOF_STR] = "";
static char opt_cdup[SIZEOF_STR] = "";
static char opt_prefix[SIZEOF_STR] = "";
static char opt_git_dir[SIZEOF_STR] = "";
static signed char opt_is_inside_work_tree = -1; /* set to TRUE or FALSE */
static char opt_editor[SIZEOF_STR] = "";
static FILE *opt_tty = NULL;
static const char **opt_diff_argv = NULL;
static const char **opt_rev_argv = NULL;
static const char **opt_file_argv = NULL;
static const char **opt_blame_argv = NULL;
static int opt_lineno = 0;
static bool opt_show_id = FALSE;
static int opt_id_cols = ID_WIDTH;
#define is_initial_commit() (!get_ref_head())
#define is_head_commit(rev) (!strcmp((rev), "HEAD") || (get_ref_head() && !strncmp(rev, get_ref_head()->id, SIZEOF_REV - 1)))
#define load_refs() reload_refs(opt_git_dir, opt_remote, opt_head, sizeof(opt_head))
static inline void
update_diff_context_arg(int diff_context)
{
if (!string_format(opt_diff_context_arg, "-U%u", diff_context))
string_ncopy(opt_diff_context_arg, "-U3", 3);
}
static inline void
update_ignore_space_arg()
{
if (opt_ignore_space == IGNORE_SPACE_ALL) {
string_copy(opt_ignore_space_arg, "--ignore-all-space");
} else if (opt_ignore_space == IGNORE_SPACE_SOME) {
string_copy(opt_ignore_space_arg, "--ignore-space-change");
} else if (opt_ignore_space == IGNORE_SPACE_AT_EOL) {
string_copy(opt_ignore_space_arg, "--ignore-space-at-eol");
} else {
string_copy(opt_ignore_space_arg, "");
}
}
static inline void
update_commit_order_arg()
{
if (opt_commit_order == COMMIT_ORDER_TOPO) {
string_copy(opt_commit_order_arg, "--topo-order");
} else if (opt_commit_order == COMMIT_ORDER_DATE) {
string_copy(opt_commit_order_arg, "--date-order");
} else if (opt_commit_order == COMMIT_ORDER_REVERSE) {
string_copy(opt_commit_order_arg, "--reverse");
} else {
string_copy(opt_commit_order_arg, "");
}
}
static inline void
update_notes_arg()
{
if (opt_notes) {
string_copy(opt_notes_arg, "--show-notes");
} else {
/* Notes are disabled by default when passing --pretty args. */
string_copy(opt_notes_arg, "");
}
}
/*
* Line-oriented content detection.
*/
#define LINE_INFO \
LINE(DIFF_HEADER, "diff --", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(DIFF_CHUNK, "@@", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
LINE(DIFF_ADD, "+", COLOR_GREEN, COLOR_DEFAULT, 0), \
LINE(DIFF_ADD2, " +", COLOR_GREEN, COLOR_DEFAULT, 0), \
LINE(DIFF_DEL, "-", COLOR_RED, COLOR_DEFAULT, 0), \
LINE(DIFF_DEL2, " -", COLOR_RED, COLOR_DEFAULT, 0), \
LINE(DIFF_INDEX, "index ", COLOR_BLUE, COLOR_DEFAULT, 0), \
LINE(DIFF_OLDMODE, "old file mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(DIFF_NEWMODE, "new file mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(DIFF_DELETED_FILE_MODE, \
"deleted file mode ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(DIFF_COPY_FROM, "copy from ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(DIFF_COPY_TO, "copy to ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(DIFF_RENAME_FROM, "rename from ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(DIFF_RENAME_TO, "rename to ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(DIFF_SIMILARITY, "similarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(DIFF_DISSIMILARITY,"dissimilarity ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(DIFF_TREE, "diff-tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
LINE(PP_AUTHOR, "Author: ", COLOR_CYAN, COLOR_DEFAULT, 0), \
LINE(PP_COMMIT, "Commit: ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
LINE(PP_MERGE, "Merge: ", COLOR_BLUE, COLOR_DEFAULT, 0), \
LINE(PP_DATE, "Date: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(PP_ADATE, "AuthorDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(PP_CDATE, "CommitDate: ", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(PP_REFS, "Refs: ", COLOR_RED, COLOR_DEFAULT, 0), \
LINE(COMMIT, "commit ", COLOR_GREEN, COLOR_DEFAULT, 0), \
LINE(PARENT, "parent ", COLOR_BLUE, COLOR_DEFAULT, 0), \
LINE(TREE, "tree ", COLOR_BLUE, COLOR_DEFAULT, 0), \
LINE(AUTHOR, "author ", COLOR_GREEN, COLOR_DEFAULT, 0), \
LINE(COMMITTER, "committer ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
LINE(SIGNOFF, " Signed-off-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(ACKED, " Acked-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(TESTED, " Tested-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(REVIEWED, " Reviewed-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(DEFAULT, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \
LINE(CURSOR, "", COLOR_WHITE, COLOR_GREEN, A_BOLD), \
LINE(STATUS, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
LINE(DELIMITER, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
LINE(DATE, "", COLOR_BLUE, COLOR_DEFAULT, 0), \
LINE(MODE, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
LINE(ID, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
LINE(FILENAME, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
LINE(LINE_NUMBER, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
LINE(TITLE_BLUR, "", COLOR_WHITE, COLOR_BLUE, 0), \
LINE(TITLE_FOCUS, "", COLOR_WHITE, COLOR_BLUE, A_BOLD), \
LINE(MAIN_COMMIT, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
LINE(MAIN_TAG, "", COLOR_MAGENTA, COLOR_DEFAULT, A_BOLD), \
LINE(MAIN_LOCAL_TAG,"", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
LINE(MAIN_REMOTE, "", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(MAIN_REPLACE, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
LINE(MAIN_TRACKED, "", COLOR_YELLOW, COLOR_DEFAULT, A_BOLD), \
LINE(MAIN_REF, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
LINE(MAIN_HEAD, "", COLOR_CYAN, COLOR_DEFAULT, A_BOLD), \
LINE(MAIN_REVGRAPH,"", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
LINE(TREE_HEAD, "", COLOR_DEFAULT, COLOR_DEFAULT, A_BOLD), \
LINE(TREE_DIR, "", COLOR_YELLOW, COLOR_DEFAULT, A_NORMAL), \
LINE(TREE_FILE, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \
LINE(STAT_HEAD, "", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(STAT_SECTION, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
LINE(STAT_NONE, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
LINE(STAT_STAGED, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
LINE(STAT_UNSTAGED,"", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
LINE(STAT_UNTRACKED,"", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
LINE(HELP_KEYMAP, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
LINE(HELP_GROUP, "", COLOR_BLUE, COLOR_DEFAULT, 0), \
LINE(DIFF_STAT, "", COLOR_BLUE, COLOR_DEFAULT, 0), \
LINE(PALETTE_0, "", COLOR_MAGENTA, COLOR_DEFAULT, 0), \
LINE(PALETTE_1, "", COLOR_YELLOW, COLOR_DEFAULT, 0), \
LINE(PALETTE_2, "", COLOR_CYAN, COLOR_DEFAULT, 0), \
LINE(PALETTE_3, "", COLOR_GREEN, COLOR_DEFAULT, 0), \
LINE(PALETTE_4, "", COLOR_DEFAULT, COLOR_DEFAULT, 0), \
LINE(PALETTE_5, "", COLOR_WHITE, COLOR_DEFAULT, 0), \
LINE(PALETTE_6, "", COLOR_RED, COLOR_DEFAULT, 0), \
LINE(GRAPH_COMMIT, "", COLOR_BLUE, COLOR_DEFAULT, 0)
enum line_type {
#define LINE(type, line, fg, bg, attr) \
LINE_##type
LINE_INFO,
LINE_NONE
#undef LINE
};
struct line_info {
const char *name; /* Option name. */
int namelen; /* Size of option name. */
const char *line; /* The start of line to match. */
int linelen; /* Size of string to match. */
int fg, bg, attr; /* Color and text attributes for the lines. */
int color_pair;
};
static struct line_info line_info[] = {
#define LINE(type, line, fg, bg, attr) \
{ #type, STRING_SIZE(#type), (line), STRING_SIZE(line), (fg), (bg), (attr) }
LINE_INFO
#undef LINE
};
static struct line_info **color_pair;
static size_t color_pairs;
static struct line_info *custom_color;
static size_t custom_colors;
DEFINE_ALLOCATOR(realloc_custom_color, struct line_info, 8)
DEFINE_ALLOCATOR(realloc_color_pair, struct line_info *, 8)
#define TO_CUSTOM_COLOR_TYPE(type) (LINE_NONE + 1 + (type))
#define TO_CUSTOM_COLOR_OFFSET(type) ((type) - LINE_NONE - 1)
/* Color IDs must be 1 or higher. [GH #15] */
#define COLOR_ID(line_type) ((line_type) + 1)
static enum line_type
get_line_type(const char *line)
{
int linelen = strlen(line);
enum line_type type;
for (type = 0; type < custom_colors; type++)
/* Case insensitive search matches Signed-off-by lines better. */
if (linelen >= custom_color[type].linelen &&
!strncasecmp(custom_color[type].line, line, custom_color[type].linelen))
return TO_CUSTOM_COLOR_TYPE(type);
for (type = 0; type < ARRAY_SIZE(line_info); type++)
/* Case insensitive search matches Signed-off-by lines better. */
if (linelen >= line_info[type].linelen &&
!strncasecmp(line_info[type].line, line, line_info[type].linelen))
return type;
return LINE_DEFAULT;
}
static enum line_type
get_line_type_from_ref(const struct ref *ref)
{
if (ref->head)
return LINE_MAIN_HEAD;
else if (ref->ltag)
return LINE_MAIN_LOCAL_TAG;
else if (ref->tag)
return LINE_MAIN_TAG;
else if (ref->tracked)
return LINE_MAIN_TRACKED;
else if (ref->remote)
return LINE_MAIN_REMOTE;
else if (ref->replace)
return LINE_MAIN_REPLACE;
return LINE_MAIN_REF;
}
static inline struct line_info *
get_line(enum line_type type)
{
if (type > LINE_NONE) {
assert(TO_CUSTOM_COLOR_OFFSET(type) < custom_colors);
return &custom_color[TO_CUSTOM_COLOR_OFFSET(type)];
} else {
assert(type < ARRAY_SIZE(line_info));
return &line_info[type];
}
}
static inline int
get_line_color(enum line_type type)
{
return COLOR_ID(get_line(type)->color_pair);
}
static inline int
get_line_attr(enum line_type type)
{
struct line_info *info = get_line(type);
return COLOR_PAIR(COLOR_ID(info->color_pair)) | info->attr;
}
static struct line_info *
get_line_info(const char *name)
{
size_t namelen = strlen(name);
enum line_type type;
for (type = 0; type < ARRAY_SIZE(line_info); type++)
if (enum_equals(line_info[type], name, namelen))
return &line_info[type];
return NULL;
}
static struct line_info *
add_custom_color(const char *quoted_line)
{
struct line_info *info;
char *line;
size_t linelen;
if (!realloc_custom_color(&custom_color, custom_colors, 1))
die("Failed to alloc custom line info");
linelen = strlen(quoted_line) - 1;
line = malloc(linelen);
if (!line)
return NULL;
strncpy(line, quoted_line + 1, linelen);
line[linelen - 1] = 0;
info = &custom_color[custom_colors++];
info->name = info->line = line;
info->namelen = info->linelen = strlen(line);
return info;
}
static void
init_line_info_color_pair(struct line_info *info, enum line_type type,
int default_bg, int default_fg)
{
int bg = info->bg == COLOR_DEFAULT ? default_bg : info->bg;
int fg = info->fg == COLOR_DEFAULT ? default_fg : info->fg;
int i;
for (i = 0; i < color_pairs; i++) {
if (color_pair[i]->fg == info->fg && color_pair[i]->bg == info->bg) {
info->color_pair = i;
return;
}
}
if (!realloc_color_pair(&color_pair, color_pairs, 1))
die("Failed to alloc color pair");
color_pair[color_pairs] = info;
info->color_pair = color_pairs++;
init_pair(COLOR_ID(info->color_pair), fg, bg);
}
static void
init_colors(void)
{
int default_bg = line_info[LINE_DEFAULT].bg;
int default_fg = line_info[LINE_DEFAULT].fg;
enum line_type type;
start_color();
if (assume_default_colors(default_fg, default_bg) == ERR) {
default_bg = COLOR_BLACK;
default_fg = COLOR_WHITE;
}
for (type = 0; type < ARRAY_SIZE(line_info); type++) {
struct line_info *info = &line_info[type];
init_line_info_color_pair(info, type, default_bg, default_fg);
}
for (type = 0; type < custom_colors; type++) {
struct line_info *info = &custom_color[type];
init_line_info_color_pair(info, TO_CUSTOM_COLOR_TYPE(type),
default_bg, default_fg);
}
}
struct line {
enum line_type type;
unsigned int lineno:24;
/* State flags */
unsigned int selected:1;
unsigned int dirty:1;
unsigned int cleareol:1;
unsigned int dont_free:1;
unsigned int wrapped:1;
void *data; /* User data */
};
/*
* Keys
*/
struct keybinding {
int alias;
enum request request;
};
static struct keybinding default_keybindings[] = {
/* View switching */
{ 'm', REQ_VIEW_MAIN },
{ 'd', REQ_VIEW_DIFF },
{ 'l', REQ_VIEW_LOG },
{ 't', REQ_VIEW_TREE },
{ 'f', REQ_VIEW_BLOB },
{ 'B', REQ_VIEW_BLAME },
{ 'H', REQ_VIEW_BRANCH },
{ 'p', REQ_VIEW_PAGER },
{ 'h', REQ_VIEW_HELP },
{ 'S', REQ_VIEW_STATUS },
{ 'c', REQ_VIEW_STAGE },
/* View manipulation */
{ 'q', REQ_VIEW_CLOSE },
{ KEY_TAB, REQ_VIEW_NEXT },
{ KEY_RETURN, REQ_ENTER },
{ KEY_UP, REQ_PREVIOUS },
{ KEY_CTL('P'), REQ_PREVIOUS },
{ KEY_DOWN, REQ_NEXT },
{ KEY_CTL('N'), REQ_NEXT },
{ 'R', REQ_REFRESH },
{ KEY_F(5), REQ_REFRESH },
{ 'O', REQ_MAXIMIZE },
{ ',', REQ_PARENT },
/* View specific */
{ 'u', REQ_STATUS_UPDATE },
{ '!', REQ_STATUS_REVERT },
{ 'M', REQ_STATUS_MERGE },
{ '1', REQ_STAGE_UPDATE_LINE },
{ '@', REQ_STAGE_NEXT },
{ '[', REQ_DIFF_CONTEXT_DOWN },
{ ']', REQ_DIFF_CONTEXT_UP },
/* Cursor navigation */
{ 'k', REQ_MOVE_UP },
{ 'j', REQ_MOVE_DOWN },
{ KEY_HOME, REQ_MOVE_FIRST_LINE },
{ KEY_END, REQ_MOVE_LAST_LINE },
{ KEY_NPAGE, REQ_MOVE_PAGE_DOWN },
{ KEY_CTL('D'), REQ_MOVE_PAGE_DOWN },
{ ' ', REQ_MOVE_PAGE_DOWN },
{ KEY_PPAGE, REQ_MOVE_PAGE_UP },
{ KEY_CTL('U'), REQ_MOVE_PAGE_UP },
{ 'b', REQ_MOVE_PAGE_UP },
{ '-', REQ_MOVE_PAGE_UP },
/* Scrolling */
{ '|', REQ_SCROLL_FIRST_COL },
{ KEY_LEFT, REQ_SCROLL_LEFT },
{ KEY_RIGHT, REQ_SCROLL_RIGHT },
{ KEY_IC, REQ_SCROLL_LINE_UP },
{ KEY_CTL('Y'), REQ_SCROLL_LINE_UP },
{ KEY_DC, REQ_SCROLL_LINE_DOWN },
{ KEY_CTL('E'), REQ_SCROLL_LINE_DOWN },
{ 'w', REQ_SCROLL_PAGE_UP },
{ 's', REQ_SCROLL_PAGE_DOWN },
/* Searching */
{ '/', REQ_SEARCH },
{ '?', REQ_SEARCH_BACK },
{ 'n', REQ_FIND_NEXT },
{ 'N', REQ_FIND_PREV },
/* Misc */
{ 'Q', REQ_QUIT },
{ 'z', REQ_STOP_LOADING },
{ 'v', REQ_SHOW_VERSION },
{ 'r', REQ_SCREEN_REDRAW },
{ KEY_CTL('L'), REQ_SCREEN_REDRAW },
{ 'o', REQ_OPTIONS },
{ '.', REQ_TOGGLE_LINENO },
{ 'D', REQ_TOGGLE_DATE },
{ 'A', REQ_TOGGLE_AUTHOR },
{ 'g', REQ_TOGGLE_REV_GRAPH },
{ '~', REQ_TOGGLE_GRAPHIC },
{ '#', REQ_TOGGLE_FILENAME },
{ 'F', REQ_TOGGLE_REFS },
{ 'I', REQ_TOGGLE_SORT_ORDER },
{ 'i', REQ_TOGGLE_SORT_FIELD },
{ 'W', REQ_TOGGLE_IGNORE_SPACE },
{ 'X', REQ_TOGGLE_ID },
{ ':', REQ_PROMPT },
{ 'e', REQ_EDIT },
};
struct keymap {
const char *name;
struct keymap *next;
struct keybinding *data;
size_t size;
bool hidden;
};
static struct keymap generic_keymap = { "generic" };
#define is_generic_keymap(keymap) ((keymap) == &generic_keymap)
static struct keymap *keymaps = &generic_keymap;
static void
add_keymap(struct keymap *keymap)
{
keymap->next = keymaps;
keymaps = keymap;
}
static struct keymap *
get_keymap(const char *name)
{
struct keymap *keymap = keymaps;
while (keymap) {
if (!strcasecmp(keymap->name, name))
return keymap;
keymap = keymap->next;
}
return NULL;
}
static void
add_keybinding(struct keymap *table, enum request request, int key)
{
size_t i;
for (i = 0; i < table->size; i++) {
if (table->data[i].alias == key) {
table->data[i].request = request;
return;
}
}
table->data = realloc(table->data, (table->size + 1) * sizeof(*table->data));
if (!table->data)
die("Failed to allocate keybinding");
table->data[table->size].alias = key;
table->data[table->size++].request = request;
if (request == REQ_NONE && is_generic_keymap(table)) {
int i;
for (i = 0; i < ARRAY_SIZE(default_keybindings); i++)
if (default_keybindings[i].alias == key)
default_keybindings[i].request = REQ_NONE;
}
}
/* Looks for a key binding first in the given map, then in the generic map, and
* lastly in the default keybindings. */
static enum request
get_keybinding(struct keymap *keymap, int key)
{
size_t i;
for (i = 0; i < keymap->size; i++)
if (keymap->data[i].alias == key)
return keymap->data[i].request;
for (i = 0; i < generic_keymap.size; i++)
if (generic_keymap.data[i].alias == key)
return generic_keymap.data[i].request;
for (i = 0; i < ARRAY_SIZE(default_keybindings); i++)
if (default_keybindings[i].alias == key)
return default_keybindings[i].request;
return (enum request) key;
}
struct key {
const char *name;
int value;
};
static const struct key key_table[] = {
{ "Enter", KEY_RETURN },
{ "Space", ' ' },
{ "Backspace", KEY_BACKSPACE },
{ "Tab", KEY_TAB },
{ "Escape", KEY_ESC },
{ "Left", KEY_LEFT },
{ "Right", KEY_RIGHT },
{ "Up", KEY_UP },
{ "Down", KEY_DOWN },
{ "Insert", KEY_IC },
{ "Delete", KEY_DC },
{ "Hash", '#' },
{ "Home", KEY_HOME },
{ "End", KEY_END },
{ "PageUp", KEY_PPAGE },
{ "PageDown", KEY_NPAGE },
{ "F1", KEY_F(1) },
{ "F2", KEY_F(2) },
{ "F3", KEY_F(3) },
{ "F4", KEY_F(4) },
{ "F5", KEY_F(5) },
{ "F6", KEY_F(6) },
{ "F7", KEY_F(7) },
{ "F8", KEY_F(8) },
{ "F9", KEY_F(9) },