temp commit
This commit is contained in:
+14
-45
@@ -33,12 +33,10 @@ void editorCloseFile(void) {
|
||||
struct buffer_t *buf = bufferFindById(active->buffer_id);
|
||||
active->cursor_x = 0;
|
||||
active->cursor_y = 0;
|
||||
active->rx = 0;
|
||||
active->row_offset = 0;
|
||||
active->col_offset = 0;
|
||||
active->x_offset = 0;
|
||||
active->y_offset = 0;
|
||||
for (int i = 0; i < buf->numrows; ++i) {
|
||||
free(buf->row[i].chars);
|
||||
free(buf->row[i].render);
|
||||
}
|
||||
buf->numrows = 0;
|
||||
free(buf->row);
|
||||
@@ -69,14 +67,17 @@ void editorOpen(struct buffer_t* buffer) {
|
||||
die("fopen");
|
||||
|
||||
char *line = NULL;
|
||||
size_t line_cap = 0;
|
||||
size_t line_cap;
|
||||
ssize_t line_len;
|
||||
|
||||
rewind(fp);
|
||||
|
||||
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;
|
||||
}
|
||||
appDebug("line %s\n", line);
|
||||
bufferInsertRow(buffer, buffer->numrows, line, line_len);
|
||||
free(line);
|
||||
line = NULL;
|
||||
@@ -100,7 +101,6 @@ void editorSave() {
|
||||
EditorPane *active = splitScreenGetActivePane();
|
||||
struct buffer_t *buffer = bufferFindById(active->buffer_id);
|
||||
int len;
|
||||
char *buf;
|
||||
int fd;
|
||||
if (buffer->filename == NULL) {
|
||||
buffer->filename = editorPrompt("Save as: %s (ESC to cancel)", "", 1);
|
||||
@@ -109,52 +109,21 @@ void editorSave() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
buf = bufferRowsToString(buffer, &len);
|
||||
fd = open(buffer->filename, O_RDWR | O_CREAT, 0644);
|
||||
if (fd != -1) {
|
||||
if (ftruncate(fd, len) != -1) {
|
||||
if (write(fd, buf, len) == len) {
|
||||
for (int i = 0; i < buffer->numrows; ++i)
|
||||
{
|
||||
len = strlen(buffer->row[i].chars);
|
||||
if (write(fd, buffer->row[i].chars, len) != len) {
|
||||
close(fd);
|
||||
free(buf);
|
||||
E.dirty = 0;
|
||||
editorSetStatusMessage("%d bytes written to disk", len);
|
||||
editorSetStatusMessage("Can't save! I/O error: %s", strerror(errno));
|
||||
|
||||
return;
|
||||
}
|
||||
write(fd, "\n", 1);
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
free(buf);
|
||||
editorSetStatusMessage("Can't save! I/O error: %s", strerror(errno));
|
||||
editorSetStatusMessage("File saved");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Searches for a string in the document
|
||||
* @details Prompts user for a search query, then searches forward from current
|
||||
* cursor position. Updates cursor position to the first match found.
|
||||
* @note Updates global editor state E (cursor position, row_offset)
|
||||
* @note Search is case-sensitive and operates on rendered line content
|
||||
* @note Searches begin from the line after current cursor position
|
||||
* @see editorPrompt()
|
||||
* @see editorRowRxToCx()
|
||||
*/
|
||||
void bufferFind(struct buffer_t *buf) {
|
||||
fprintf(stderr, "searching\n");
|
||||
char *query = editorPrompt("Search: %s (ESC to cancel)", "", 0);
|
||||
EditorPane *active = splitScreenGetActivePane();
|
||||
|
||||
if (query == NULL)
|
||||
return;
|
||||
int i;
|
||||
for (i = active->cursor_y + 1; i < buf->numrows; i++) {
|
||||
frow *row = &buf->row[i];
|
||||
char *match = strstr(row->render, query);
|
||||
if (match) {
|
||||
active->cursor_y = i;
|
||||
active->cursor_x = bufferRowRxToCx(row, match - row->render);
|
||||
buf->row_offset = buf->numrows;
|
||||
break;
|
||||
}
|
||||
}
|
||||
free(query);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user