486
487
static void emit_line(const char *set, const char *reset, const char *line, int len)
488
{
489
- if (len > 0 && line[len-1] == '\n')
490
- len--;
489
fputs(set, stdout);
490
fwrite(line, len, 1, stdout);
493
- puts(reset);
494
-}
495
-
496
-static void emit_line_with_ws(int nparents,
497
- const char *set, const char *reset, const char *ws,
498
- const char *line, int len, unsigned ws_rule)
499
-{
500
- int col0 = nparents;
501
- int last_tab_in_indent = -1;
502
- int last_space_in_indent = -1;
503
- int i;
504
- int tail = len;
505
- int need_highlight_leading_space = 0;
506
- /*
507
- * The line is a newly added line. Does it have funny leading
508
- * whitespaces? In indent, SP should never precede a TAB. In
509
- * addition, under "indent with non tab" rule, there should not
510
- * be more than 8 consecutive spaces.
511
- */
512
- for (i = col0; i < len; i++) {
513
- if (line[i] == '\t') {
514
- last_tab_in_indent = i;
515
- if ((ws_rule & WS_SPACE_BEFORE_TAB) &&
516
- 0 <= last_space_in_indent)
517
- need_highlight_leading_space = 1;
518
- }
519
- else if (line[i] == ' ')
520
- last_space_in_indent = i;
521
- else
522
- break;
523
- }
524
- if ((ws_rule & WS_INDENT_WITH_NON_TAB) &&
525
- 0 <= last_space_in_indent &&
526
- last_tab_in_indent < 0 &&
527
- 8 <= (i - col0)) {
528
- last_tab_in_indent = i;
529
- need_highlight_leading_space = 1;
530
- }
531
- fputs(set, stdout);
532
- fwrite(line, col0, 1, stdout);
491
fputs(reset, stdout);
534
- if (((i == len) || line[i] == '\n') && i != col0) {
535
- /* The whole line was indent */
536
- emit_line(ws, reset, line + col0, len - col0);
537
- return;
538
- }
539
- i = col0;
540
- if (need_highlight_leading_space) {
541
- while (i < last_tab_in_indent) {
542
- if (line[i] == ' ') {
543
- fputs(ws, stdout);
544
- putchar(' ');
545
- fputs(reset, stdout);
546
- }
547
- else
548
- putchar(line[i]);
549
- i++;
550
- }
551
- }
552
- tail = len - 1;
553
- if (line[tail] == '\n' && i < tail)
554
- tail--;
555
- if (ws_rule & WS_TRAILING_SPACE) {
556
- while (i < tail) {
557
- if (!isspace(line[tail]))
558
- break;
559
- tail--;
560
- }
561
- }
562
- if ((i < tail && line[tail + 1] != '\n')) {
563
- /* This has whitespace between tail+1..len */
564
- fputs(set, stdout);
565
- fwrite(line + i, tail - i + 1, 1, stdout);
566
- fputs(reset, stdout);
567
- emit_line(ws, reset, line + tail + 1, len - tail - 1);
568
- }
569
- else
570
- emit_line(set, reset, line + i, len - i);
492
}
493
494
static void emit_add_line(const char *reset, struct emit_callback *ecbdata, const char *line, int len)
498
499
if (!*ws)
500
emit_line(set, reset, line, len);
580
- else
581
- emit_line_with_ws(ecbdata->nparents, set, reset, ws,
582
- line, len, ecbdata->ws_rule);
501
+ else {
502
+ /* Emit just the prefix, then the rest. */
503
+ emit_line(set, reset, line, ecbdata->nparents);
504
+ (void)check_and_emit_line(line + ecbdata->nparents,
505
+ len - ecbdata->nparents, ecbdata->ws_rule,
506
+ stdout, set, reset, ws);
507
+ }
508
}
509
510
static void fn_out_consume(void *priv, char *line, unsigned long len)
965
const char *ws = diff_get_color(data->color_diff, DIFF_WHITESPACE);
966
const char *reset = diff_get_color(data->color_diff, DIFF_RESET);
967
const char *set = diff_get_color(data->color_diff, DIFF_FILE_NEW);
968
+ char *err;
969
970
if (line[0] == '+') {
1045
- int i, spaces = 0, space_before_tab = 0, white_space_at_end = 0;
1046
-
1047
- /* check space before tab */
1048
- for (i = 1; i < len; i++) {
1049
- if (line[i] == ' ')
1050
- spaces++;
1051
- else if (line[i] == '\t') {
1052
- if (spaces) {
1053
- space_before_tab = 1;
1054
- break;
1055
- }
1056
- }
1057
- else
1058
- break;
1059
- }
1060
-
1061
- /* check whitespace at line end */
1062
- if (line[len - 1] == '\n')
1063
- len--;
1064
- if (isspace(line[len - 1]))
1065
- white_space_at_end = 1;
1066
-
1067
- if (space_before_tab || white_space_at_end) {
1068
- data->status = 1;
1069
- printf("%s:%d: %s", data->filename, data->lineno, ws);
1070
- if (space_before_tab) {
1071
- printf("space before tab");
1072
- if (white_space_at_end)
1073
- putchar(',');
1074
- }
1075
- if (white_space_at_end)
1076
- printf("whitespace at end");
1077
- printf(":%s ", reset);
1078
- emit_line_with_ws(1, set, reset, ws, line, len,
1079
- data->ws_rule);
1080
- }
1081
-
971
+ data->status = check_and_emit_line(line + 1, len - 1,
972
+ data->ws_rule, NULL, NULL, NULL, NULL);
973
+ if (!data->status)
974
+ return;
975
+ err = whitespace_error_string(data->status);
976
+ printf("%s:%d: %s%s:%s ", data->filename, data->lineno,
977
+ ws, err, reset);
978
+ free(err);
979
+ emit_line(set, reset, line, 1);
980
+ (void)check_and_emit_line(line + 1, len - 1, data->ws_rule,
981
+ stdout, set, reset, ws);
982
data->lineno++;
983
} else if (line[0] == ' ')
984
data->lineno++;