jim

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

editor.c (1570B)


      1 #include"editor.h"
      2 #include"jim.h"
      3 #include"row.h"
      4 
      5 void editorInsertChar(int c) {
      6     if(E.cy == E.numrows) {
      7         editorInsertRow(E.numrows, "", 0);
      8     }
      9 
     10     editorRowInsertChar(&E.row[E.cy], E.cx, c);
     11     E.cx++;
     12     E.dirty++;
     13 }
     14 
     15 void editorJumpToNewline(char p) {
     16     switch(p) {
     17         case NEWLINE_BELOW:
     18             E.cy++;
     19             //fallthrough
     20         case NEWLINE_ABOVE:
     21             editorInsertRow(E.cy, "", 0);
     22             break;
     23     }
     24 
     25     E.cx = 0;
     26 }
     27 
     28 void editorInsertNewline() {
     29     if(E.cx == 0) {
     30         editorInsertRow(E.cy, "", 0);
     31     } else {
     32         erow* row = &E.row[E.cy];
     33         editorInsertRow(E.cy + 1, &row->chars[E.cx], row->size - E.cx);
     34         row = &E.row[E.cy];
     35         row->size = E.cx;
     36         row->chars[row->size] = '\0';
     37         editorUpdateRow(row);
     38     }
     39 
     40     E.cy++;
     41     E.cx = 0;
     42 }
     43 
     44 void editorDelChar() {
     45     if(E.cy == E.numrows) return;
     46 
     47     erow* row = &E.row[E.cy];
     48     if(E.cx == row->size) {
     49         if(E.cy == E.numrows - 1)
     50             return;
     51         erow* nrow = &E.row[E.cy + 1];
     52         editorRowAppendString(row, nrow->chars, nrow->size);
     53         editorDelRow(E.cy + 1);
     54     } else {
     55         editorRowDelChar(row, E.cx);
     56     }
     57 }
     58 
     59 void editorBackspaceChar() {
     60     if(E.cy == E.numrows) return;
     61     if(E.cy == 0 && E.cx == 0) return;
     62 
     63     erow* row = &E.row[E.cy];
     64     if(E.cx == 0) {
     65         E.cx = E.row[E.cy - 1].size;
     66         editorRowAppendString(&E.row[E.cy - 1], row->chars, row->size);
     67         editorDelRow(E.cy);
     68     } else {
     69         editorRowDelChar(row, E.cx - 1);
     70         E.cx--;
     71     }
     72 }