Basic syntax system, right now only C and hardcode in eco!
[eco.git] / screen.c
blob2003e62fda4208b4b24618ae2f1ca18b1ad8ead7
1 /*
2 * Copyright (C) 2008 Diego Hernan Borghetti.
3 * Eco
4 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
10 #include "term.h"
11 #include "screen.h"
12 #include "list.h"
13 #include "syntax.h"
16 E_Screen *e_screen_init(E_Term *tr)
18 E_ScreenLine *ln;
19 E_Screen *sc;
20 int i, e;
22 sc= (E_Screen *)malloc(sizeof(E_Screen));
23 if (!sc) {
24 e_term_close(tr);
25 perror("malloc");
26 exit (-1);
29 sc->vscr= (E_ScreenLine **)malloc(sizeof(E_ScreenLine *) * tr->nrow);
30 sc->pscr= (E_ScreenLine **)malloc(sizeof(E_ScreenLine *) * tr->nrow);
31 if ((!sc->vscr) || (!sc->pscr)) {
32 e_term_close(tr);
33 perror("malloc");
34 exit(-1);
37 sc->nrow= tr->nrow;
38 sc->ncol= tr->ncol;
39 sc->row= 0;
40 sc->col= 0;
42 for (i= 0; i < tr->nrow; i++) {
43 ln= (E_ScreenLine *)malloc(sizeof(E_ScreenLine)+tr->ncol);
44 if (!ln) {
45 e_term_close(tr);
46 perror("malloc");
47 exit(-1);
49 ln->fcol= (char *)malloc(tr->ncol);
50 ln->bcol= (char *)malloc(tr->ncol);
51 ln->crow= 0;
52 for (e= 0; e < tr->ncol; e++) {
53 ln->fcol[e]= E_TR_WHITE;
54 ln->bcol[e]= E_TR_BLACK;
56 sc->vscr[i]= ln;
58 ln= (E_ScreenLine *)malloc(sizeof(E_ScreenLine)+tr->ncol);
59 if (!ln) {
60 e_term_close(tr);
61 perror("malloc");
62 exit(-1);
64 ln->crow= 0;
65 ln->fcol= (char *)malloc(tr->ncol);
66 ln->bcol= (char *)malloc(tr->ncol);
67 for (e= 0; e < tr->ncol; e++) {
68 ln->fcol[e]= E_TR_WHITE;
69 ln->bcol[e]= E_TR_BLACK;
71 sc->pscr[i]= ln;
73 return(sc);
76 void e_screen_free(E_Screen *sc)
78 E_ScreenLine *ln;
79 int i;
81 for (i= 0; i < sc->nrow; i++) {
82 ln= sc->vscr[i];
83 free((void *)ln->fcol);
84 free((void *)ln->bcol);
85 free((void *)ln);
87 ln= sc->pscr[i];
88 free((void *)ln->fcol);
89 free((void *)ln->bcol);
90 free((void *)ln);
93 free((void *)sc->vscr);
94 free((void *)sc->pscr);
95 free((void *)sc);
98 void e_screen_putc(E_Screen *sc, int c, char fcol, char bcol)
100 E_ScreenLine *ln;
102 if (sc->row >= 0 && sc->row < sc->nrow) {
103 ln= sc->vscr[sc->row];
104 ln->text[sc->col]= c;
105 ln->fcol[sc->col]= fcol;
106 ln->bcol[sc->col]= bcol;
110 void e_screen_move(E_Screen *sc, int row, int col)
112 sc->row= row;
113 sc->col= col;
116 void e_screen_crow(E_Screen *sc, int row, char crow)
118 E_ScreenLine *ln;
120 if (row >= 0 && row < sc->nrow) {
121 ln= sc->vscr[row];
122 ln->crow= crow;
126 void e_screen_eeol(E_Screen *sc)
128 E_ScreenLine *ln;
129 int i;
131 if (sc->row >= 0 && sc->row < sc->nrow) {
132 ln= sc->vscr[sc->row];
133 for (i= sc->col; i < sc->ncol; i++) {
134 ln->text[i]= ' ';
135 ln->fcol[i]= E_TR_WHITE;
136 ln->bcol[i]= E_TR_BLACK;
141 void e_screen_eeop(E_Screen *sc)
143 E_ScreenLine *ln;
144 int e, i;
146 if (sc->row >= 0 && sc->row < sc->nrow) {
147 for(e= 0; e < sc->nrow; e++) {
148 ln= sc->vscr[e];
149 for (i= 0; i < sc->ncol; i++) {
150 ln->text[i]= ' ';
151 ln->fcol[i]= E_TR_WHITE;
152 ln->bcol[i]= E_TR_BLACK;
158 void e_screen_swap(E_Term *tr, E_Screen *sc, int user_force)
160 E_ScreenLine *vln, *pln;
161 E_Syntax *sy;
162 int e, i, force;
164 /* FIXME! */
165 sy= e_syntax_search(".c");
167 /* default color, white and black. */
168 e_term_fgcol(tr, E_TR_WHITE);
169 e_term_bgcol(tr, E_TR_BLACK);
171 for(e= 0; e < sc->nrow; e++) {
172 vln= sc->vscr[e];
173 pln= sc->pscr[e];
174 force= user_force;
176 /* Apply the color but skip current row. */
177 if (sy && vln->crow == 0)
178 e_syntax_apply(sy, vln, sc->ncol);
180 for (i= 0; i < sc->ncol; i++) {
181 /* Check for color changes. */
182 if (vln->fcol[i] != pln->fcol[i] ||
183 vln->bcol[i] != pln->bcol[i]) {
184 pln->fcol[i]= vln->fcol[i];
185 pln->bcol[i]= vln->bcol[i];
186 force= 1;
189 if (force || vln->text[i] != pln->text[i]) {
190 e_term_fgcol(tr, pln->fcol[i]);
191 e_term_bgcol(tr, pln->bcol[i]);
192 e_term_move(e, i);
193 e_term_putc(vln->text[i]);
194 pln->text[i]= vln->text[i];
199 /* restore the original position of the cursor. */
200 e_term_move(sc->row, sc->col);
201 e_term_flush();