jim

Simple, lightweight, modal, vim-inspired text editor
git clone git://git.janpasierb.com/jim.git
Log | Files | Refs | README | LICENSE

row.c (3232B)


      1 #include"row.h"
      2 #include"jim.h"
      3 #include"syntax.h"
      4 #include<stdlib.h>
      5 #include<string.h>
      6 
      7 #define TAB_STOP 4
      8 
      9 int editorRowCxToRx(erow* row, int cx) {
     10     int rx = 0;
     11 
     12     for(int j = 0; j < cx; j++) {
     13         if(row->chars[j] == '\t')
     14             rx += (TAB_STOP - 1) - (rx % TAB_STOP);
     15         rx++;
     16     }
     17 
     18     return rx;
     19 }
     20 
     21 int editorRowRxToCx(erow* row, int rx) {
     22     int cur_rx = 0;
     23     int cx;
     24 
     25     for(cx = 0; cx < row->size; cx++) {
     26         if(row->chars[cx] == '\t')
     27             cur_rx += (TAB_STOP - 1) - (cur_rx % TAB_STOP);
     28         cur_rx++;
     29 
     30         if(cur_rx > rx) return cx;
     31     }
     32 
     33     return cx;
     34 }
     35 
     36 void editorUpdateRow(erow* row) {
     37     int tabs = 0;
     38     int j;
     39 
     40     for(j = 0; j < row->size; j++)
     41         if(row->chars[j] == '\t') tabs++;
     42 
     43     free(row->render);
     44     row->render = malloc(row->size + tabs * (TAB_STOP - 1) + 1);
     45 
     46     int idx = 0;
     47     for(j = 0; j < row->size; j++) {
     48         if(row->chars[j] == '\t') {
     49             row->render[idx++] = ' ';
     50             while(idx % TAB_STOP != 0) row->render[idx++] = ' ';
     51         } else {
     52             row->render[idx++] = row->chars[j];
     53         }
     54     }
     55 
     56     row->render[idx] = '\0';
     57     row->rsize = idx;
     58 
     59     editorUpdateSyntax(row);
     60 }
     61 
     62 void editorInsertRow(int at, char* s, size_t len) {
     63     if(at < 0 || at > E.numrows) return;
     64 
     65     E.row = realloc(E.row, sizeof(erow) * (E.numrows + 1));
     66     memmove(&E.row[at + 1], &E.row[at], sizeof(erow) * (E.numrows - at));
     67     for(int j = at + 1; j <= E.numrows; j++)
     68         E.row[j].idx++;
     69 
     70     E.row[at].idx = at;
     71 
     72     E.row[at].size = len;
     73     E.row[at].chars = malloc(len + 1);
     74     memcpy(E.row[at].chars, s, len);
     75     E.row[at].chars[len] = '\0';
     76 
     77     E.row[at].rsize = 0;
     78     E.row[at].render = NULL;
     79     E.row[at].hl = NULL;
     80     E.row[at].hl_open_comment = 0;
     81     editorUpdateRow(&E.row[at]);
     82 
     83     E.numrows++;
     84     E.dirty++;
     85 }
     86 
     87 void editorFreeRow(erow *row) {
     88     free(row->render);
     89     free(row->chars);
     90     free(row->hl);
     91 }
     92 
     93 void editorClearRow(erow *row) {
     94 	row->size = 0;
     95 	row->chars = realloc(row->chars, 1);
     96 	row->chars[0] = '\0';
     97 	editorUpdateRow(row);
     98 }
     99 
    100 void editorDelRow(int at) {
    101     if(at < 0 || at >= E.numrows || !E.row) return;
    102 
    103 	if(E.numrows > 1)
    104 	{
    105 		editorFreeRow(&E.row[at]);
    106 		memmove(&E.row[at], &E.row[at + 1], sizeof(erow) * (E.numrows - at - 1));
    107 		for(int j = at; j < E.numrows - 1; j++)
    108 			E.row[j].idx--;
    109 		E.numrows--;
    110 		if(E.cy == E.numrows)
    111 			E.cy--;
    112 	} else {
    113 		editorClearRow(&E.row[0]);
    114 	}
    115 
    116 	E.dirty++;
    117 }
    118 
    119 void editorRowInsertChar(erow* row, int at, char c) {
    120     if(at < 0 || at > row->size) at = row->size;
    121     row->chars = realloc(row->chars, row->size + 2);
    122     memmove(&row->chars[at + 1], &row->chars[at], row->size - at + 1);
    123     row->size++;
    124     row->chars[at] = c;
    125     editorUpdateRow(row);
    126 }
    127 
    128 void editorRowAppendString(erow* row, char* s, size_t len) {
    129     row->chars = realloc(row->chars, row->size + len + 1);
    130     memcpy(&row->chars[row->size], s, len);
    131     row->size += len;
    132     row->chars[row->size] = '\0';
    133     editorUpdateRow(row);
    134     E.dirty++;
    135 }
    136 
    137 void editorRowDelChar(erow *row, int at) {
    138     if(at < 0 || at >= row->size) return;
    139     memmove(&row->chars[at], &row->chars[at + 1], row->size - at);
    140     row->size--;
    141     editorUpdateRow(row);
    142     E.dirty++;
    143 }