Add dirty flag

This commit is contained in:
Aaron Fischer 2018-10-05 11:59:27 +02:00
parent 1abad8b71f
commit 01615ba5ef

22
kilo.c
View file

@ -21,6 +21,7 @@
#define KILO_VERSION "0.0.1" #define KILO_VERSION "0.0.1"
#define KILO_TAB_STOP 8 #define KILO_TAB_STOP 8
#define KILO_QUIT_TIMES 3
#define CTRL_KEY(k) ((k) & 0x1f) #define CTRL_KEY(k) ((k) & 0x1f)
@ -56,6 +57,7 @@ struct editorConfig {
int screencols; int screencols;
int numrows; int numrows;
erow *row; erow *row;
int dirty;
char *filename; char *filename;
char statusmsg[80]; char statusmsg[80];
time_t statusmsg_time; time_t statusmsg_time;
@ -236,6 +238,7 @@ void editorAppendRow(char *s, size_t len) {
editorUpdateRow(&E.row[at]); editorUpdateRow(&E.row[at]);
E.numrows++; E.numrows++;
E.dirty++;
} }
void editorRowInsertChar(erow *row, int at, int c) { void editorRowInsertChar(erow *row, int at, int c) {
@ -245,6 +248,7 @@ void editorRowInsertChar(erow *row, int at, int c) {
row->size++; row->size++;
row->chars[at] = c; row->chars[at] = c;
editorUpdateRow(row); editorUpdateRow(row);
E.dirty++;
} }
@ -298,6 +302,7 @@ void editorOpen(char *filename) {
} }
free(line); free(line);
fclose(fp); fclose(fp);
E.dirty = 0;
} }
void editorSave() { void editorSave() {
@ -312,6 +317,7 @@ void editorSave() {
if (write(fd, buf, len) == len) { if (write(fd, buf, len) == len) {
close(fd); close(fd);
free(buf); free(buf);
E.dirty = 0;
editorSetStatusMessage("%d bytes written to disk", len); editorSetStatusMessage("%d bytes written to disk", len);
return; return;
} }
@ -400,8 +406,9 @@ void editorDrawRows(struct abuf *ab) {
void editorDrawStatusBar(struct abuf *ab) { void editorDrawStatusBar(struct abuf *ab) {
abAppend(ab, "\x1b[7m", 4); abAppend(ab, "\x1b[7m", 4);
char status[80], rstatus[80]; char status[80], rstatus[80];
int len = snprintf(status, sizeof(status), "%.20s - %d lines", int len = snprintf(status, sizeof(status), "%.20s - %d lines %s",
E.filename ? E.filename : "[No Name]", E.numrows); E.filename ? E.filename : "[No Name]", E.numrows,
E.dirty ? "(modified)" : "");
int rlen = snprintf(rstatus, sizeof(rstatus), "%d/%d", int rlen = snprintf(rstatus, sizeof(rstatus), "%d/%d",
E.cy + 1, E.numrows); E.cy + 1, E.numrows);
if (len > E.screencols) len = E.screencols; if (len > E.screencols) len = E.screencols;
@ -501,6 +508,8 @@ void editorMoveCursor(int key) {
void editorProcessKeypress() { void editorProcessKeypress() {
static int quit_times = KILO_QUIT_TIMES;
int c = editorReadKey(); // Blocking! int c = editorReadKey(); // Blocking!
switch (c) { switch (c) {
@ -509,6 +518,12 @@ void editorProcessKeypress() {
break; break;
case CTRL_KEY('q'): case CTRL_KEY('q'):
if (E.dirty && quit_times > 0) {
editorSetStatusMessage("WARNING!!! File has unsaved changes. "
"Press CTRL-Q %d more times to quit.", quit_times);
quit_times--;
return;
}
write(STDOUT_FILENO, "\x1b[2J", 4); write(STDOUT_FILENO, "\x1b[2J", 4);
write(STDOUT_FILENO, "\x1b[H", 3); write(STDOUT_FILENO, "\x1b[H", 3);
exit(0); exit(0);
@ -563,6 +578,8 @@ void editorProcessKeypress() {
editorInsertChar(c); editorInsertChar(c);
break; break;
} }
quit_times = KILO_QUIT_TIMES;
} }
/*** init ***/ /*** init ***/
@ -575,6 +592,7 @@ void initEditor() {
E.coloff = 0; E.coloff = 0;
E.numrows = 0; E.numrows = 0;
E.row = NULL; E.row = NULL;
E.dirty = 0;
E.filename = NULL; E.filename = NULL;
E.statusmsg[0] = '\0'; E.statusmsg[0] = '\0';
E.statusmsg_time = 0; E.statusmsg_time = 0;