• kilo TextEditor-2


    文本查看器

    行查看

    存储一行文本的数据类型

    typedef struct erow {
        int size;
        char *chars;
    } erow;
    
    struct editorConfig {
        int cx, cy;
        int screenrows;
        int screencols;
        int numrows;
        erow row;
        struct termios orig_termios;
    };
    struct editorConfig E;
    
    void initEditor() {
        E.cx = 0;
        E.cy = 0;
        E.numrows = 0;
        if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    打开文件,读一行

    #include 
    
    void editorOpen() {
        FILE *fp = fopen(filename, "r");
        if (!fp) die("fopen");
        char *line = NULL;
        size_t linecap = 0;
        ssize_t linelen;
        linelen = getline(&line, &linecap, fp);
        if (linelen != -1) {
            //从后往前识别出内容长度
            while (linelen > 0 && (line[linelen - 1] == '\n' || line[linelen - 1] == '\r'))
                linelen--;
            E.row.size = linelen;
            E.row.chars = malloc(linelen + 1);
            memcpy(E.row.chars, line, linelen);
            E.row.chars[linelen] = '\0';
            E.numrows = 1;
        }
        free(line);
        fclose(fp);
    }
    
    int main(int argc, char* argv[]) {
        enableRawMode();
        initEditor();
        if (argc >= 2) {
            editorOpen(argv[1]);
        }
        while (1) {
            editorRefreshScreen();
            editorProcessKeypress();
        }
        return 0;
    }
    
    • 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

    显示行

    void editorDrawRows(struct abuf *ab) {
        int y;
        for (y = 0; y < E.screenrows; y++) {
            if (y >= E.numrows) {
                //显示完文本后
                //如果文件没有内容才显示欢迎信息
                if (E.numrows == 0 && y == E.screenrows / 3) {
                    char welcome[80];
                    int welcomelen = snprintf(welcome, sizeof(welcome),
                                              "Kilo editor -- version %s", KILO_VERSION);
                    if (welcomelen > E.screencols) welcomelen = E.screencols;
                    int padding = (E.screencols - welcomelen) / 2;
                    if (padding) {
                        abAppend(ab, "~", 1);
                        padding--;
                    }
                    while (padding--) abAppend(ab, " ", 1);
                    abAppend(ab, welcome, welcomelen);
                } else {
                    abAppend(ab, "~", 1);
                }
            } else {
                //显示文本
                int len = E.row.size;
                if (len > E.screencols) len = E.screencols;
                abAppend(ab, E.row.chars, len);
            }
            abAppend(ab, "\x1b[K", 3);
            if (y < E.screenrows - 1) {
                abAppend(ab, "\r\n", 2);
            }
        }
    }
    
    • 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
    //功能测试宏,用于提高可移植性
    #define _DEFAULT_SOURCE
    #define _BSD_SOURCE
    #define _GNU_SOURCE
    
    • 1
    • 2
    • 3
    • 4

    多行显示

    需要动态分配的结构体数组

    struct editorConfig {
        int cx, cy;
        int screenrows;
        int screencols;
        int numrows;
        erow *row;
        struct termios orig_termios;
    };
    
    void initEditor() {
        E.cx = 0;
        E.cy = 0;
        E.numrows = 0;
        E.row = NULL;
        if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    读多行

    void editorAppendRow(char *s, size_t len) {
        E.row = realloc(E.row, sizeof(erow) * (E.numrows + 1));
        int at = E.numrows;
        E.row[at].size = len;
        E.row[at].chars = malloc(len + 1);
        memcpy(E.row[at].chars, s, len);
        E.row[at].chars[len] = '\0';
        E.numrows++;
    }
    
    void editorOpen(char *filename) {
        FILE *fp = fopen(filename, "r");
        if (!fp) die("fopen");
        char *line = NULL;
        size_t linecap = 0;
        ssize_t linelen;
        while ((linelen = getline(&line, &linecap, fp)) != -1) {
            while (linelen > 0 && (line[linelen - 1] == '\n' || line[linelen - 1] == '\r'))
                linelen--;
            editorAppendRow(line, linelen);
        }
        free(line);
        fclose(fp);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    显示多行

    void editorDrawRows(struct abuf *ab) {
        int y;
        for (y = 0; y < E.screenrows; y++) {
            if (y >= E.numrows) {
                if (E.numrows == 0 && y == E.screenrows / 3) {
                    char welcome[80];
                    int welcomelen = snprintf(welcome, sizeof(welcome),
                                              "Kilo editor -- version %s", KILO_VERSION);
                    if (welcomelen > E.screencols) welcomelen = E.screencols;
                    int padding = (E.screencols - welcomelen) / 2;
                    if (padding) {
                        abAppend(ab, "~", 1);
                        padding--;
                    }
                    while (padding--) abAppend(ab, " ", 1);
                    abAppend(ab, welcome, welcomelen);
                } else {
                    abAppend(ab, "~", 1);
                }
            } else {
                //显示第y行文本
                int len = E.row[y].size;
                if (len > E.screencols) len = E.screencols;
                abAppend(ab, E.row[y].chars, len);
            }
            abAppend(ab, "\x1b[K", 3);
            if (y < E.screenrows - 1) {
                abAppend(ab, "\r\n", 2);
            }
        }
    }
    
    • 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

    垂直滚动

    滚动浏览文件,需要设置当前滚动到文件的哪一行了

    struct editorConfig {
        int cx, cy;
        int rowoff;
        int screenrows;
        int screencols;
        int numrows;
        erow *row;
        struct termios orig_termios;
    };
    
    void initEditor() {
        E.cx = 0;
        E.cy = 0;
        E.rowoff = 0;
        E.numrows = 0;
        E.row = NULL;
        if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    将滚动行偏移量纳入显示行的影响参数中

    void editorDrawRows(struct abuf *ab) {
        int y;
        for (y = 0; y < E.screenrows; y++) {
            //filerow是当前屏的第y行在文件中的行编号
            int filerow = y + E.rowoff;
            if (filerow >= E.numrows) {
                //在超出了文件所有行的地方画波浪号和欢迎信息
                if (E.numrows == 0 && y == E.screenrows / 3) {
                    char welcome[80];
                    int welcomelen = snprintf(welcome, sizeof(welcome),
                                              "Kilo editor -- version %s", KILO_VERSION);
                    if (welcomelen > E.screencols) welcomelen = E.screencols;
                    int padding = (E.screencols - welcomelen) / 2;
                    if (padding) {
                        abAppend(ab, "~", 1);
                        padding--;
                    }
                    while (padding--) abAppend(ab, " ", 1);
                    abAppend(ab, welcome, welcomelen);
                } else {
                    abAppend(ab, "~", 1);
                }
            } else {
                //文件中对应的行
                int len = E.row[filerow].size;
                if (len > E.screencols) len = E.screencols;
                abAppend(ab, E.row[filerow].chars, len);
            }
            abAppend(ab, "\x1b[K", 3);
            if (y < E.screenrows - 1) {
                abAppend(ab, "\r\n", 2);
            }
        }
    }
    
    • 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

    光标引导滚动,此时光标看作文件中的光标,而不是屏幕中的光标

    void editorScroll() {
        if (E.cy < E.rowoff) {
            E.rowoff = E.cy;
        }
        if (E.cy >= E.rowoff + E.screenrows) {
            E.rowoff = E.cy - E.screenrows + 1;
        }
    }
    
    void editorRefreshScreen() {
        //刷新屏幕时更新滚动情况
        editorScroll();
    
        struct abuf ab = ABUF_INIT;
        abAppend(&ab, "\x1b[?25l", 6);
        abAppend(&ab, "\x1b[H", 3);
        editorDrawRows(&ab);
        
        char buf[32];
        //更新光标在屏幕中的位置
        snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) + 1, E.cx + 1);
        abAppend(&ab, buf, strlen(buf));
        
        abAppend(&ab, "\x1b[?25h", 6);
        write(STDOUT_FILENO, ab.b, ab.len);
        abFree(&ab);
    }
    
    void editorMoveCursor(int key) {
        switch (key) {
            case ARROW_LEFT:
                if (E.cx != 0) {
                    E.cx--;
                }
                break;
            case ARROW_RIGHT:
                if (E.cx != E.screencols - 1) {
                    E.cx++;
                }
                break;
            case ARROW_UP:
                if (E.cy != 0) {
                    E.cy--;
                }
                break;
            case ARROW_DOWN:
                //将光标限制在文件行数内
                if (E.cy < E.numrows) {
                    E.cy++;
                }
                break;
        }
    }
    
    • 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

    水平滚动

    struct editorConfig {
        int cx, cy;
        int rowoff;
        int coloff;
        int screenrows;
        int screencols;
        int numrows;
        erow *row;
        struct termios orig_termios;
    };
    
    void initEditor() {
        E.cx = 0;
        E.cy = 0;
        E.rowoff = 0;
        E.coloff = 0;
        E.numrows = 0;
        E.row = NULL;
        if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    void editorDrawRows(struct abuf *ab) {
        int y;
        for (y = 0; y < E.screenrows; y++) {
            int filerow = y + E.rowoff;
            if (filerow >= E.numrows) {
                if (E.numrows == 0 && y == E.screenrows / 3) {
                    char welcome[80];
                    int welcomelen = snprintf(welcome, sizeof(welcome),
                                              "Kilo editor -- version %s", KILO_VERSION);
                    if (welcomelen > E.screencols) welcomelen = E.screencols;
                    int padding = (E.screencols - welcomelen) / 2;
                    if (padding) {
                        abAppend(ab, "~", 1);
                        padding--;
                    }
                    while (padding--) abAppend(ab, " ", 1);
                    abAppend(ab, welcome, welcomelen);
                } else {
                    abAppend(ab, "~", 1);
                }
            } else {
                //裁剪掉水平滚动下当前行消失的列
                int len = E.row[filerow].size - E.coloff;
                if (len < 0) len = 0;
                if (len > E.screencols) len = E.screencols;
                //控制行字符串开头 E.coloff
                abAppend(ab, &E.row[filerow].chars[E.coloff], len);
            }
            abAppend(ab, "\x1b[K", 3);
            if (y < E.screenrows - 1) {
                abAppend(ab, "\r\n", 2);
            }
        }
    }
    
    • 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
    void editorScroll() {
        if (E.cy < E.rowoff) {
            E.rowoff = E.cy;
        }
        if (E.cy >= E.rowoff + E.screenrows) {
            E.rowoff = E.cy - E.screenrows + 1;
        }
        //控制光标水平滚动范围
        if (E.cx < E.coloff) {
            E.coloff = E.cx;
        }
        if (E.cx >= E.coloff + E.screencols) {
            E.coloff = E.cx - E.screencols + 1;
        }
    }
    
    void editorMoveCursor(int key) {
        switch (key) {
            case ARROW_LEFT:
                if (E.cx != 0) {
                    E.cx--;
                }
                break;
            case ARROW_RIGHT:
                //开放超过屏幕右边缘 if (E.cx != E.screencols - 1) {
                E.cx++;
                break;
            case ARROW_UP:
                if (E.cy != 0) {
                    E.cy--;
                }
                break;
            case ARROW_DOWN:
                if (E.cy < E.numrows) {
                    E.cy++;
                }
                break;
        }
    }
    
    • 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
    void editorRefreshScreen() {
        editorScroll();
        struct abuf ab = ABUF_INIT;
        abAppend(&ab, "\x1b[?25l", 6);
        abAppend(&ab, "\x1b[H", 3);
        editorDrawRows(&ab);
        
        char buf[32];
        snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) + 1, (E.cx - E.coloff) + 1);
        abAppend(&ab, buf, strlen(buf));
        
        abAppend(&ab, "\x1b[?25h", 6);
        write(STDOUT_FILENO, ab.b, ab.len);
        abFree(&ab);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    限制向右滚动

    有两种情况限制向右滚屏:1.到达行末 2.到达文件最后一行的下一行

    void editorMoveCursor(int key) {
        //是否到达文件最后一行的下一行
        erow *row = (E.cy >= E.numrows) ? NULL : &E.row[E.cy];
        switch (key) {
            case ARROW_LEFT:
                if (E.cx != 0) {
                    E.cx--;
                }
                break;
            case ARROW_RIGHT:
                //情况1+2判断
                if (row && E.cx < row->size) {
                    E.cx++;
                }
                break;
            case ARROW_UP:
                if (E.cy != 0) {
                    E.cy--;
                }
                break;
            case ARROW_DOWN:
                if (E.cy < E.numrows) {
                    E.cy++;
                }
                break;
        }
    }
    
    • 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

    将光标对齐到行尾

    限制光标只能在行尾内

    void editorMoveCursor(int key) {
        erow *row = (E.cy >= E.numrows) ? NULL : &E.row[E.cy];
        switch (key) {
            case ARROW_LEFT:
                if (E.cx != 0) {
                    E.cx--;
                }
                break;
            case ARROW_RIGHT:
                if (row && E.cx < row->size) {
                    E.cx++;
                }
                break;
            case ARROW_UP:
                if (E.cy != 0) {
                    E.cy--;
                }
                break;
            case ARROW_DOWN:
                if (E.cy < E.numrows) {
                    E.cy++;
                }
                break;
        }
        //再次赋值是为了更新E.cy变化的情况
        row = (E.cy >= E.numrows) ? NULL : &E.row[E.cy];
        //限制列
        int rowlen = row ? row->size : 0;
        if (E.cx > rowlen) {
            E.cx = rowlen;
        }
    }
    
    • 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

    在行首向左移动 && 在行末向右移动

    void editorMoveCursor(int key) {
        erow *row = (E.cy >= E.numrows) ? NULL : &E.row[E.cy];
        switch (key) {
            case ARROW_LEFT:
                if (E.cx != 0) {
                    E.cx--;
                } else if (E.cy > 0) {
                    //在行首向左移动的情况
                    E.cy--;
                    E.cx = E.row[E.cy].size;
                }
                break;
            case ARROW_RIGHT:
                if (row && E.cx < row->size) {
                    E.cx++;
                } else if (row && E.cx == row->size) {
                    //在行末向右移动的情况
                    E.cy++;
                    E.cx = 0;
                }
                break;
            case ARROW_UP:
                if (E.cy != 0) {
                    E.cy--;
                }
                break;
            case ARROW_DOWN:
                if (E.cy < E.numrows) {
                    E.cy++;
                }
                break;
        }
        row = (E.cy >= E.numrows) ? NULL : &E.row[E.cy];
        int rowlen = row ? row->size : 0;
        if (E.cx > rowlen) {
            E.cx = rowlen;
        }
    }
    
    • 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

    渲染Tab

    设定Tab是由8个空格组成,计划在erow结构体中新增一个字符串用来存放该行渲染后的字符串的样子

    typedef struct erow {
        int size;
        int rsize;//
        char *chars;
        char *render;//
    } erow;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    #define KILO_TAB_STOP 8
    
    void editorUpdateRow(erow *row) {
        int tabs = 0;
        int j;
        for (j = 0; j < row->size; j++)
            //处理Tab
            if (row->chars[j] == '\t') tabs++;
        free(row->render);
        row->render = malloc(row->size + tabs*(KILO_TAB_STOP - 1) + 1);
        int idx = 0;
        for (j = 0; j < row->size; j++) {
            if (row->chars[j] == '\t') {
                row->render[idx++] = ' ';
                while (idx % KILO_TAB_STOP != 0) row->render[idx++] = ' ';
            } else {
                row->render[idx++] = row->chars[j];
            }
        }
        row->render[idx] = '\0';
        row->rsize = idx;
    }
    
    void editorAppendRow(char *s, size_t len) {
        E.row = realloc(E.row, sizeof(erow) * (E.numrows + 1));
        int at = E.numrows;
        E.row[at].size = len;
        E.row[at].chars = malloc(len + 1);
        memcpy(E.row[at].chars, s, len);
        E.row[at].chars[len] = '\0';
    
        E.row[at].rsize = 0;
        E.row[at].render = NULL;
        editorUpdateRow(&E.row[at]);
        E.numrows++;
    }
    
    • 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

    将显示行的内容换成渲染后字符串的内容

    void editorDrawRows(struct abuf *ab) {
        int y;
        for (y = 0; y < E.screenrows; y++) {
            int filerow = y + E.rowoff;
            if (filerow >= E.numrows) {
                if (E.numrows == 0 && y == E.screenrows / 3) {
                    char welcome[80];
                    int welcomelen = snprintf(welcome, sizeof(welcome),
                                              "Kilo editor -- version %s", KILO_VERSION);
                    if (welcomelen > E.screencols) welcomelen = E.screencols;
                    int padding = (E.screencols - welcomelen) / 2;
                    if (padding) {
                        abAppend(ab, "~", 1);
                        padding--;
                    }
                    while (padding--) abAppend(ab, " ", 1);
                    abAppend(ab, welcome, welcomelen);
                } else {
                    abAppend(ab, "~", 1);
                }
            } else {
                //render替换chars
                int len = E.row[filerow].rsize - E.coloff;
                if (len < 0) len = 0;
                if (len > E.screencols) len = E.screencols;
                abAppend(ab, &E.row[filerow].render[E.coloff], len);
            }
            abAppend(ab, "\x1b[K", 3);
            if (y < E.screenrows - 1) {
                abAppend(ab, "\r\n", 2);
            }
        }
    }
    
    • 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

    Tab和光标

    当光标遇到Tab时,需要一次性跳过Tab,所以新增一个光标列坐标

    struct editorConfig {
        int cx, cy;
        int rx;
        int rowoff;
        int coloff;
        int screenrows;
        int screencols;
        int numrows;
        erow *row;
        struct termios orig_termios;
    };
    
    void initEditor() {
        E.cx = 0;
        E.cy = 0;
        E.rx = 0;
        E.rowoff = 0;
        E.coloff = 0;
        E.numrows = 0;
        E.row = NULL;
        if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    替换滚动和刷新时E.cx的位置,因为rx表示的才是实际上光标的位置

    void editorScroll() {
        E.rx = E.cx;
        if (E.cy < E.rowoff) {
            E.rowoff = E.cy;
        }
        if (E.cy >= E.rowoff + E.screenrows) {
            E.rowoff = E.cy - E.screenrows + 1;
        }
        if (E.rx < E.coloff) {
            E.coloff = E.rx;
        }
        if (E.rx >= E.coloff + E.screencols) {
            E.coloff = E.rx - E.screencols + 1;
        }
    }
    
    void editorRefreshScreen() {
        editorScroll();
        struct abuf ab = ABUF_INIT;
        abAppend(&ab, "\x1b[?25l", 6);
        abAppend(&ab, "\x1b[H", 3);
        editorDrawRows(&ab);
        char buf[32];
        snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) + 1, (E.rx - E.coloff) + 1);
        abAppend(&ab, buf, strlen(buf));
        abAppend(&ab, "\x1b[?25h", 6);
        write(STDOUT_FILENO, ab.b, ab.len);
        abFree(&ab);
    }
    
    • 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

    cx转换成rz

    制表符的作用:

    原文:123456\t123     12345678\t123
    结果:123456  123     12345678        123
    
    • 1
    • 2

    那么考虑到后面有rx++,用(KILO_TAB_STOP - 1)(实际是8)减去前面字符已有的字符数(上面的例子分别是6和0)得到的就是制表符渲染后得到的空格数

    int editorRowCxToRx(erow *row, int cx) {
        int rx = 0;
        int j;
        for (j = 0; j < cx; j++) {
            if (row->chars[j] == '\t')
                rx += (KILO_TAB_STOP - 1) - (rx % KILO_TAB_STOP);
            rx++;
        }
        return rx;
    }
    
    void editorScroll() {
        E.rx = 0;
        if (E.cy < E.numrows) {
            E.rx = editorRowCxToRx(&E.row[E.cy], E.cx);
        }
        if (E.cy < E.rowoff) {
            E.rowoff = E.cy;
        }
        if (E.cy >= E.rowoff + E.screenrows) {
            E.rowoff = E.cy - E.screenrows + 1;
        }
        if (E.rx < E.coloff) {
            E.coloff = E.rx;
        }
        if (E.rx >= E.coloff + E.screencols) {
            E.coloff = E.rx - E.screencols + 1;
        }
    }
    
    • 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

    Page滚动文件页面

    void editorProcessKeypress() {
        int c = editorReadKey();
        switch (c) {
            case CTRL_KEY('q'):
                write(STDOUT_FILENO, "\x1b[2J", 4);
                write(STDOUT_FILENO, "\x1b[H", 3);
                exit(0);
                break;
            case HOME_KEY:
                E.cx = 0;
                break;
            case END_KEY:
                E.cx = E.screencols - 1;
                break;
            case PAGE_UP:
            case PAGE_DOWN:
                {
                    //原来是在光标上直接光标上下移动整个屏幕大小
                    //现在是在滚动偏移量(屏幕最顶行在文件中的偏移量)的基础上上下移动整个屏幕大小
                    if (c == PAGE_UP) {
                        E.cy = E.rowoff;
                    } else if (c == PAGE_DOWN) {
                        E.cy = E.rowoff + E.screenrows - 1;
                        if (E.cy > E.numrows) E.cy = E.numrows;
                    }
                    int times = E.screenrows;
                    while (times--)
                        editorMoveCursor(c == PAGE_UP ? ARROW_UP : ARROW_DOWN);
                }
                break;
            case ARROW_UP:
            case ARROW_DOWN:
            case ARROW_LEFT:
            case ARROW_RIGHT:
                editorMoveCursor(c);
                break;
        }
    }
    
    
    • 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

    End 移动到行尾

    void editorProcessKeypress() {
        int c = editorReadKey();
        switch (c) {
            case CTRL_KEY('q'):
                write(STDOUT_FILENO, "\x1b[2J", 4);
                write(STDOUT_FILENO, "\x1b[H", 3);
                exit(0);
                break;
            case HOME_KEY:
                E.cx = 0;
                break;
            case END_KEY:
                //
                if (E.cy < E.numrows)
                    E.cx = E.row[E.cy].size;
                break;
            case PAGE_UP:
            case PAGE_DOWN:
                {
                    if (c == PAGE_UP) {
                        E.cy = E.rowoff;
                    } else if (c == PAGE_DOWN) {
                        E.cy = E.rowoff + E.screenrows - 1;
                        if (E.cy > E.numrows) E.cy = E.numrows;
                    }
                    int times = E.screenrows;
                    while (times--)
                        editorMoveCursor(c == PAGE_UP ? ARROW_UP : ARROW_DOWN);
                }
                break;
            case ARROW_UP:
            case ARROW_DOWN:
            case ARROW_LEFT:
            case ARROW_RIGHT:
                editorMoveCursor(c);
                break;
        }
    }
    
    • 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

    状态栏

    负责呈现文件名、行数、列数等信息,在底部空出

    void editorDrawRows(struct abuf *ab) {
        int y;
        for (y = 0; y < E.screenrows; y++) {
            int filerow = y + E.rowoff;
            if (filerow >= E.numrows) {
                if (E.numrows == 0 && y == E.screenrows / 3) {
                    char welcome[80];
                    int welcomelen = snprintf(welcome, sizeof(welcome),
                                              "Kilo editor -- version %s", KILO_VERSION);
                    if (welcomelen > E.screencols) welcomelen = E.screencols;
                    int padding = (E.screencols - welcomelen) / 2;
                    if (padding) {
                        abAppend(ab, "~", 1);
                        padding--;
                    }
                    while (padding--) abAppend(ab, " ", 1);
                    abAppend(ab, welcome, welcomelen);
                } else {
                    abAppend(ab, "~", 1);
                }
            } else {
                int len = E.row[filerow].rsize - E.coloff;
                if (len < 0) len = 0;
                if (len > E.screencols) len = E.screencols;
                abAppend(ab, &E.row[filerow].render[E.coloff], len);
            }
            abAppend(ab, "\x1b[K", 3);
            //if (y < E.screenrows - 1) {
            abAppend(ab, "\r\n", 2);
        }
    }
    
    void initEditor() {
        E.cx = 0;
        E.cy = 0;
        E.rx = 0;
        E.rowoff = 0;
        E.coloff = 0;
        E.numrows = 0;
        E.row = NULL;
        if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize");
        E.screenrows -= 1;//空出底部一行
    }
    
    • 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
    void editorDrawStatusBar(struct abuf *ab) {
        abAppend(ab, "\x1b[7m", 4);//反转颜色
        int len = 0;
        //画空格
        while (len < E.screencols) {
            abAppend(ab, " ", 1);
            len++;
        }
        abAppend(ab, "\x1b[m", 3);//再反转回来
    }
    
    void editorRefreshScreen() {
        editorScroll();
        struct abuf ab = ABUF_INIT;
        abAppend(&ab, "\x1b[?25l", 6);
        abAppend(&ab, "\x1b[H", 3);
        editorDrawRows(&ab);
        
        editorDrawStatusBar(&ab);
        
        char buf[32];
        snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) + 1, (E.rx - E.coloff) + 1);
        abAppend(&ab, buf, strlen(buf));
        abAppend(&ab, "\x1b[?25h", 6);
        write(STDOUT_FILENO, ab.b, ab.len);
        abFree(&ab);
    }
    
    • 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

    全局参数添加一个文件名

    struct editorConfig {
        int cx, cy;
        int rx;
        int rowoff;
        int coloff;
        int screenrows;
        int screencols;
        int numrows;
        erow *row;
        char *filename;//文件名
        struct termios orig_termios;
    };
    
    void editorOpen(char *filename) {
        free(E.filename);
    
        E.filename = strdup(filename);
    
        FILE *fp = fopen(filename, "r");
        if (!fp) die("fopen");
        char *line = NULL;
        size_t linecap = 0;
        ssize_t linelen;
        while ((linelen = getline(&line, &linecap, fp)) != -1) {
            while (linelen > 0 && (line[linelen - 1] == '\n' ||
                                   line[linelen - 1] == '\r'))
                linelen--;
            editorAppendRow(line, linelen);
        }
        free(line);
        fclose(fp);
    }
    
    • 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
    void editorDrawStatusBar(struct abuf *ab) {
        abAppend(ab, "\x1b[7m", 4);
        char status[80], rstatus[80];
        //文件名 总行数
        int len = snprintf(status, sizeof(status), "%.20s - %d lines",
                           E.filename ? E.filename : "[No Name]", E.numrows);
        //当前行/总行
        int rlen = snprintf(rstatus, sizeof(rstatus), "%d/%d",
                            E.cy + 1, E.numrows);
        if (len > E.screencols) len = E.screencols;
        //先显示 文件名+总行
        abAppend(ab, status, len);
        while (len < E.screencols) {
            //之后是空格,直到满足剩余字符长度="当前行/总行"的字符长度
            if (E.screencols - len == rlen) {
                //然后显示 当前行/总行
                abAppend(ab, rstatus, rlen);
                break;
            } else {
                abAppend(ab, " ", 1);
                len++;
            }
        }
        abAppend(ab, "\x1b[m", 3);
    }
    
    • 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

    状态消息

    包括时间和操作结果消息

    #include 
    
    struct editorConfig {
        int cx, cy;
        int rx;
        int rowoff;
        int coloff;
        int screenrows;
        int screencols;
        int numrows;
        erow *row;
        char *filename;
        char statusmsg[80];//消息
        time_t statusmsg_time;//时间
        struct termios orig_termios;
    };
    
    void initEditor() {
        E.cx = 0;
        E.cy = 0;
        E.rx = 0;
        E.rowoff = 0;
        E.coloff = 0;
        E.numrows = 0;
        E.row = NULL;
        E.filename = NULL;
        E.statusmsg[0] = '\0';
        E.statusmsg_time = 0;
        if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize");
        E.screenrows -= 1;
    }
    
    • 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
    #include 
    
    void editorSetStatusMessage(const char *fmt, ...) {
        va_list ap;
        va_start(ap, fmt);
        vsnprintf(E.statusmsg, sizeof(E.statusmsg), fmt, ap);
        va_end(ap);
        E.statusmsg_time = time(NULL);
    }
    
    int main(int argc, char *argv[]) {
        enableRawMode();
        initEditor();
        if (argc >= 2) {
            editorOpen(argv[1]);
        }
        editorSetStatusMessage("HELP: Ctrl-Q = quit");
        while (1) {
            editorRefreshScreen();
            editorProcessKeypress();
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    再空出一行来显示状态消息

    void editorDrawStatusBar(struct abuf *ab) {
        abAppend(ab, "\x1b[7m", 4);
        char status[80], rstatus[80];
        int len = snprintf(status, sizeof(status), "%.20s - %d lines",
                           E.filename ? E.filename : "[No Name]", E.numrows);
        int rlen = snprintf(rstatus, sizeof(rstatus), "%d/%d",
                            E.cy + 1, E.numrows);
        if (len > E.screencols) len = E.screencols;
        abAppend(ab, status, len);
        while (len < E.screencols) {
            if (E.screencols - len == rlen) {
                abAppend(ab, rstatus, rlen);
                break;
            } else {
                abAppend(ab, " ", 1);
                len++;
            }
        }
        abAppend(ab, "\x1b[m", 3);
        //空一行
        abAppend(ab, "\r\n", 2);
    }
    void initEditor() {
        E.cx = 0;
        E.cy = 0;
        E.rx = 0;
        E.rowoff = 0;
        E.coloff = 0;
        E.numrows = 0;
        E.row = NULL;
        E.filename = NULL;
        E.statusmsg[0] = '\0';
        E.statusmsg_time = 0;
        if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize");
        
        E.screenrows -= 2;
    }
    
    • 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
    void editorDrawMessageBar(struct abuf *ab) {
        abAppend(ab, "\x1b[K", 3);
        int msglen = strlen(E.statusmsg);
        if (msglen > E.screencols) msglen = E.screencols;
        if (msglen && time(NULL) - E.statusmsg_time < 5)
            abAppend(ab, E.statusmsg, msglen);
    }
    
    void editorRefreshScreen() {
        editorScroll();
        struct abuf ab = ABUF_INIT;
        abAppend(&ab, "\x1b[?25l", 6);
        abAppend(&ab, "\x1b[H", 3);
        editorDrawRows(&ab);
        editorDrawStatusBar(&ab);
        
        editorDrawMessageBar(&ab);
        
        char buf[32];
        snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) + 1,
                 (E.rx - E.coloff) + 1);
        abAppend(&ab, buf, strlen(buf));
        abAppend(&ab, "\x1b[?25h", 6);
        write(STDOUT_FILENO, ab.b, ab.len);
        abFree(&ab);
    }
    
    • 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
  • 相关阅读:
    【深入理解Kotlin协程】lifecycleScope源码追踪扒皮
    VuePress + Github Pages 搭建博客网站
    Centos7更新php7.2版本升级
    windows环境下PHP7.4多线程设置
    当LCC画龙时,新老车企分别在想什么?
    C++ 学习(一)Visual Studio 2022配置、Git配置及第一个程序
    大数据平台进度,它来了
    Linux学习-42-查看文件系统信息dumpe2fs命令用法
    计算机毕业设计Java高校墨香文学社管理系统(源码+系统+mysql数据库+lw文档)
    Python中转换IP地址格式的方法
  • 原文地址:https://blog.csdn.net/Falling_Asteroid/article/details/134279409