neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

ugrid.c (1954B)


      1 #include <assert.h>
      2 #include <string.h>
      3 
      4 #include "nvim/grid.h"
      5 #include "nvim/memory.h"
      6 #include "nvim/ugrid.h"
      7 
      8 #include "ugrid.c.generated.h"
      9 
     10 void ugrid_init(UGrid *grid)
     11 {
     12  grid->cells = NULL;
     13 }
     14 
     15 void ugrid_free(UGrid *grid)
     16 {
     17  destroy_cells(grid);
     18 }
     19 
     20 void ugrid_resize(UGrid *grid, int width, int height)
     21 {
     22  destroy_cells(grid);
     23  grid->cells = xmalloc((size_t)height * sizeof(UCell *));
     24  for (int i = 0; i < height; i++) {
     25    grid->cells[i] = xcalloc((size_t)width, sizeof(UCell));
     26  }
     27 
     28  grid->width = width;
     29  grid->height = height;
     30 }
     31 
     32 void ugrid_clear(UGrid *grid)
     33 {
     34  clear_region(grid, 0, grid->height - 1, 0, grid->width - 1, 0);
     35 }
     36 
     37 void ugrid_clear_chunk(UGrid *grid, int row, int col, int endcol, sattr_T attr)
     38 {
     39  clear_region(grid, row, row, col, endcol - 1, attr);
     40 }
     41 
     42 void ugrid_goto(UGrid *grid, int row, int col)
     43 {
     44  grid->row = row;
     45  grid->col = col;
     46 }
     47 
     48 void ugrid_scroll(UGrid *grid, int top, int bot, int left, int right, int count)
     49 {
     50  // Compute start/stop/step for the loop below
     51  int start, stop, step;
     52  if (count > 0) {
     53    start = top;
     54    stop = bot - count + 1;
     55    step = 1;
     56  } else {
     57    start = bot;
     58    stop = top - count - 1;
     59    step = -1;
     60  }
     61 
     62  // Copy cell data
     63  for (int i = start; i != stop; i += step) {
     64    UCell *target_row = grid->cells[i] + left;
     65    UCell *source_row = grid->cells[i + count] + left;
     66    assert(right >= left && left >= 0);
     67    memcpy(target_row, source_row,
     68           sizeof(UCell) * ((size_t)right - (size_t)left + 1));
     69  }
     70 }
     71 
     72 static void clear_region(UGrid *grid, int top, int bot, int left, int right, sattr_T attr)
     73 {
     74  for (int row = top; row <= bot; row++) {
     75    UGRID_FOREACH_CELL(grid, row, left, right + 1, {
     76      cell->data = schar_from_ascii(' ');
     77      cell->attr = attr;
     78    });
     79  }
     80 }
     81 
     82 static void destroy_cells(UGrid *grid)
     83 {
     84  if (grid->cells) {
     85    for (int i = 0; i < grid->height; i++) {
     86      xfree(grid->cells[i]);
     87    }
     88    XFREE_CLEAR(grid->cells);
     89  }
     90 }