This commit is contained in:
@@ -68,6 +68,7 @@ struct editorConfig {
|
|||||||
|
|
||||||
struct keyBind_t* key_binds;
|
struct keyBind_t* key_binds;
|
||||||
int number_of_keybinds;
|
int number_of_keybinds;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -82,4 +83,5 @@ struct abuf {
|
|||||||
|
|
||||||
extern struct editorConfig E;
|
extern struct editorConfig E;
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#ifndef INIT_H_
|
#ifndef INIT_H_
|
||||||
#define INIT_H_
|
#define INIT_H_
|
||||||
|
|
||||||
|
#include "builtins.h"
|
||||||
#include "data.h"
|
#include "data.h"
|
||||||
#include "terminal.h"
|
#include "terminal.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include "define.h"
|
#include "define.h"
|
||||||
#include "output.h"
|
#include "output.h"
|
||||||
#include "terminal.h"
|
#include "terminal.h"
|
||||||
|
#include "builtins.h"
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
// KEYS keycode
|
// KEYS keycode
|
||||||
|
|||||||
@@ -5,7 +5,8 @@
|
|||||||
* interactions. \version 0.1 \date 21 septembre 2024
|
* interactions. \version 0.1 \date 21 septembre 2024
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#define _DEFAULT_SOURCE
|
#define _DEFAULT_SOURCE
|
||||||
#define _BSD_SOURCE
|
#define _BSD_SOURCE
|
||||||
#define _GNU_SOURCE
|
#define _GNU_SOURCE
|
||||||
@@ -32,6 +33,7 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
editorSetStatusMessage("HELP: Ctrl-S = save | Ctrl-Q = quit");
|
editorSetStatusMessage("HELP: Ctrl-S = save | Ctrl-Q = quit");
|
||||||
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
editorRefreshScreen();
|
editorRefreshScreen();
|
||||||
editorProcessKeypress();
|
editorProcessKeypress();
|
||||||
|
|||||||
+77
-77
@@ -16,98 +16,98 @@ extern int ftruncate(int fd, off_t length);
|
|||||||
extern struct editorConfig E;
|
extern struct editorConfig E;
|
||||||
|
|
||||||
char *editorRowsToString(int *buffer_len) {
|
char *editorRowsToString(int *buffer_len) {
|
||||||
int tot_len = 0;
|
int tot_len = 0;
|
||||||
int j;
|
int j;
|
||||||
char *buf;
|
char *buf;
|
||||||
char *p;
|
char *p;
|
||||||
|
|
||||||
for (j = 0; j < E.numrows; ++j) {
|
for (j = 0; j < E.numrows; ++j) {
|
||||||
tot_len += E.row[j].size + 1;
|
tot_len += E.row[j].size + 1;
|
||||||
}
|
}
|
||||||
*buffer_len = tot_len;
|
*buffer_len = tot_len;
|
||||||
buf = malloc(tot_len);
|
buf = malloc(tot_len);
|
||||||
p = buf;
|
p = buf;
|
||||||
for (j = 0; j < E.numrows; ++j) {
|
for (j = 0; j < E.numrows; ++j) {
|
||||||
memcpy(p, E.row[j].chars, E.row[j].size);
|
memcpy(p, E.row[j].chars, E.row[j].size);
|
||||||
p += E.row[j].size;
|
p += E.row[j].size;
|
||||||
*p = '\n';
|
*p = '\n';
|
||||||
p++;
|
p++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
void editorCloseFile(void) {
|
void editorCloseFile(void) {
|
||||||
E.cursor_x = 0;
|
E.cursor_x = 0;
|
||||||
E.cursor_y = 0;
|
E.cursor_y = 0;
|
||||||
E.rx = 0;
|
E.rx = 0;
|
||||||
E.row_offset = 0;
|
E.row_offset = 0;
|
||||||
E.col_offset = 0;
|
E.col_offset = 0;
|
||||||
E.numrows = 0;
|
E.numrows = 0;
|
||||||
E.row = NULL;
|
E.row = NULL;
|
||||||
E.dirty = 0;
|
E.dirty = 0;
|
||||||
E.filename = NULL;
|
E.filename = NULL;
|
||||||
E.status_msg[0] = '\0';
|
E.status_msg[0] = '\0';
|
||||||
E.status_msg_time = 0;
|
E.status_msg_time = 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void editorOpen(char *filename) {
|
void editorOpen(char *filename) {
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
|
|
||||||
// Test if a file is already open
|
// Test if a file is already open
|
||||||
if (E.filename != NULL) {
|
if (E.filename != NULL) {
|
||||||
editorCloseFile();
|
editorCloseFile();
|
||||||
}
|
E.state = READ_AND_WRITE;
|
||||||
|
|
||||||
free(E.filename);
|
|
||||||
E.filename = strdup(filename);
|
|
||||||
|
|
||||||
fp = fopen(filename, "a+");
|
|
||||||
if (!fp)
|
|
||||||
die("fopen");
|
|
||||||
|
|
||||||
char *line = NULL;
|
|
||||||
size_t line_cap = 0;
|
|
||||||
ssize_t line_len;
|
|
||||||
|
|
||||||
while ((line_len = getline(&line, &line_cap, fp)) != -1) {
|
|
||||||
while (line_len > 0 &&
|
|
||||||
(line[line_len - 1] == '\n' || line[line_len - 1] == '\r')) {
|
|
||||||
--line_len;
|
|
||||||
}
|
}
|
||||||
editorInsertRow(E.numrows, line, line_len);
|
|
||||||
}
|
free(E.filename);
|
||||||
free(line);
|
E.filename = strdup(filename);
|
||||||
fclose(fp);
|
|
||||||
E.dirty = 0;
|
fp = fopen(filename, "a+");
|
||||||
|
if (!fp)
|
||||||
|
die("fopen");
|
||||||
|
|
||||||
|
char *line = NULL;
|
||||||
|
size_t line_cap = 0;
|
||||||
|
ssize_t line_len;
|
||||||
|
|
||||||
|
while ((line_len = getline(&line, &line_cap, fp)) != -1) {
|
||||||
|
while (line_len > 0 &&
|
||||||
|
(line[line_len - 1] == '\n' || line[line_len - 1] == '\r')) {
|
||||||
|
--line_len;
|
||||||
|
}
|
||||||
|
editorInsertRow(E.numrows, line, line_len);
|
||||||
|
}
|
||||||
|
free(line);
|
||||||
|
fclose(fp);
|
||||||
|
E.dirty = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void editorSave() {
|
void editorSave() {
|
||||||
int len;
|
int len;
|
||||||
char *buf;
|
char *buf;
|
||||||
int fd;
|
int fd;
|
||||||
if (E.filename == NULL) {
|
|
||||||
E.filename = editorPrompt("Save as: %s (ESC to cancel)");
|
|
||||||
if (E.filename == NULL) {
|
if (E.filename == NULL) {
|
||||||
editorSetStatusMessage("Save aborted");
|
E.filename = editorPrompt("Save as: %s (ESC to cancel)");
|
||||||
return;
|
if (E.filename == NULL) {
|
||||||
|
editorSetStatusMessage("Save aborted");
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
buf = editorRowsToString(&len);
|
||||||
buf = editorRowsToString(&len);
|
fd = open(E.filename, O_RDWR | O_CREAT, 0644);
|
||||||
fd = open(E.filename, O_RDWR | O_CREAT, 0644);
|
if (fd != -1) {
|
||||||
if (fd != -1) {
|
if (ftruncate(fd, len) != -1) {
|
||||||
if (ftruncate(fd, len) != -1) {
|
if (write(fd, buf, len) == len) {
|
||||||
if (write(fd, buf, len) == len) {
|
close(fd);
|
||||||
|
free(buf);
|
||||||
|
E.dirty = 0;
|
||||||
|
editorSetStatusMessage("%d bytes written to disk", len);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
close(fd);
|
close(fd);
|
||||||
free(buf);
|
|
||||||
E.dirty = 0;
|
|
||||||
editorSetStatusMessage("%d bytes written to disk", len);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
close(fd);
|
free(buf);
|
||||||
}
|
editorSetStatusMessage("Can't save! I/O error: %s", strerror(errno));
|
||||||
free(buf);
|
|
||||||
editorSetStatusMessage("Can't save! I/O error: %s", strerror(errno));
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user