aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/ugrid.c
blob: d96da3f2bba634b638f407236684ba2f19e3b893 (plain) (blame)
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
// This is an open source non-commercial project. Dear PVS-Studio, please check
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com

#include <assert.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>

#include "nvim/assert.h"
#include "nvim/ugrid.h"
#include "nvim/ui.h"
#include "nvim/vim.h"

#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "ugrid.c.generated.h"
#endif

void ugrid_init(UGrid *grid)
{
  grid->cells = NULL;
}

void ugrid_free(UGrid *grid)
{
  destroy_cells(grid);
}

void ugrid_resize(UGrid *grid, int width, int height)
{
  destroy_cells(grid);
  grid->cells = xmalloc((size_t)height * sizeof(UCell *));
  for (int i = 0; i < height; i++) {
    grid->cells[i] = xcalloc((size_t)width, sizeof(UCell));
  }

  grid->width = width;
  grid->height = height;
}

void ugrid_clear(UGrid *grid)
{
  clear_region(grid, 0, grid->height - 1, 0, grid->width - 1, 0);
}

void ugrid_clear_chunk(UGrid *grid, int row, int col, int endcol, sattr_T attr)
{
  clear_region(grid, row, row, col, endcol - 1, attr);
}

void ugrid_goto(UGrid *grid, int row, int col)
{
  grid->row = row;
  grid->col = col;
}

void ugrid_scroll(UGrid *grid, int top, int bot, int left, int right, int count)
{
  // Compute start/stop/step for the loop below
  int start, stop, step;
  if (count > 0) {
    start = top;
    stop = bot - count + 1;
    step = 1;
  } else {
    start = bot;
    stop = top - count - 1;
    step = -1;
  }

  int i;

  // Copy cell data
  for (i = start; i != stop; i += step) {
    UCell *target_row = grid->cells[i] + left;
    UCell *source_row = grid->cells[i + count] + left;
    assert(right >= left && left >= 0);
    memcpy(target_row, source_row,
           sizeof(UCell) * ((size_t)right - (size_t)left + 1));
  }
}

static void clear_region(UGrid *grid, int top, int bot, int left, int right, sattr_T attr)
{
  for (int row = top; row <= bot; row++) {
    UGRID_FOREACH_CELL(grid, row, left, right + 1, {
      cell->data[0] = ' ';
      cell->data[1] = 0;
      cell->attr = attr;
    });
  }
}

static void destroy_cells(UGrid *grid)
{
  if (grid->cells) {
    for (int i = 0; i < grid->height; i++) {
      xfree(grid->cells[i]);
    }
    XFREE_CLEAR(grid->cells);
  }
}