cleaning up code
This commit is contained in:
124
buffer.c
124
buffer.c
@@ -10,6 +10,7 @@
|
|||||||
#include "buffer.h"
|
#include "buffer.h"
|
||||||
#include "core.h"
|
#include "core.h"
|
||||||
#include "editor.h"
|
#include "editor.h"
|
||||||
|
#include "undo.h"
|
||||||
|
|
||||||
|
|
||||||
#define NO_NAME "[No Name]"
|
#define NO_NAME "[No Name]"
|
||||||
@@ -222,12 +223,12 @@ buffer_name(buffer *b)
|
|||||||
void
|
void
|
||||||
buffers_init(void)
|
buffers_init(void)
|
||||||
{
|
{
|
||||||
int idx = 0;
|
int idx = 0;
|
||||||
|
|
||||||
editor.buffers = NULL;
|
editor.buffers = NULL;
|
||||||
editor.bufcount = 0;
|
editor.bufcount = 0;
|
||||||
editor.curbuf = 0;
|
editor.curbuf = 0;
|
||||||
editor.bufcap = 0;
|
editor.bufcap = 0;
|
||||||
|
|
||||||
idx = buffer_add_empty();
|
idx = buffer_add_empty();
|
||||||
buffer_switch(idx);
|
buffer_switch(idx);
|
||||||
@@ -237,28 +238,29 @@ buffers_init(void)
|
|||||||
static void
|
static void
|
||||||
buffer_list_resize(void)
|
buffer_list_resize(void)
|
||||||
{
|
{
|
||||||
buffer **newlist = NULL;
|
buffer **newlist = NULL;
|
||||||
|
|
||||||
if (editor.bufcount == editor.bufcap) {
|
if (editor.bufcount == editor.bufcap) {
|
||||||
editor.bufcap = (size_t)cap_growth((int)editor.bufcap, (int)editor.bufcount + 1);
|
editor.bufcap = (size_t)cap_growth((int)editor.bufcap,
|
||||||
|
(int)editor.bufcount + 1);
|
||||||
|
|
||||||
newlist = realloc(editor.buffers, sizeof(buffer *) * editor.bufcap);
|
newlist = realloc(editor.buffers, sizeof(buffer *) * editor.bufcap);
|
||||||
assert(newlist != NULL);
|
assert(newlist != NULL);
|
||||||
editor.buffers = newlist;
|
editor.buffers = newlist;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
buffer_add_empty(void)
|
buffer_add_empty(void)
|
||||||
{
|
{
|
||||||
buffer *buf = NULL;
|
buffer *buf = NULL;
|
||||||
int idx = 0;
|
int idx = 0;
|
||||||
|
|
||||||
buffer_list_resize();
|
buffer_list_resize();
|
||||||
|
|
||||||
buf = calloc(1, sizeof(buffer));
|
buf = calloc(1, sizeof(buffer));
|
||||||
assert(buf != NULL);
|
assert(buf != NULL);
|
||||||
|
|
||||||
buf->curx = 0;
|
buf->curx = 0;
|
||||||
buf->cury = 0;
|
buf->cury = 0;
|
||||||
@@ -273,28 +275,23 @@ buffer_add_empty(void)
|
|||||||
buf->mark_curx = 0;
|
buf->mark_curx = 0;
|
||||||
buf->mark_cury = 0;
|
buf->mark_cury = 0;
|
||||||
|
|
||||||
editor.buffers[editor.bufcount] = buf;
|
undo_tree_init(&buf->tree);
|
||||||
idx = (int)editor.bufcount;
|
|
||||||
editor.bufcount++;
|
|
||||||
return idx;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
editor.buffers[editor.bufcount] = buf;
|
||||||
void
|
idx = (int)editor.bufcount;
|
||||||
buffer_save_current(void)
|
editor.bufcount++;
|
||||||
{
|
return idx;
|
||||||
/* No-op: editor no longer mirrors per-buffer fields */
|
|
||||||
(void)editor;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
buffer *
|
buffer *
|
||||||
buffer_current(void)
|
buffer_current(void)
|
||||||
{
|
{
|
||||||
if (editor.bufcount == 0 || editor.curbuf >= editor.bufcount) {
|
if (editor.bufcount == 0 || editor.curbuf >= editor.bufcount) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return editor.buffers[editor.curbuf];
|
|
||||||
|
return editor.buffers[editor.curbuf];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -328,58 +325,58 @@ buffer_is_unnamed_and_empty(const buffer *b)
|
|||||||
void
|
void
|
||||||
buffer_switch(const int idx)
|
buffer_switch(const int idx)
|
||||||
{
|
{
|
||||||
buffer *b = NULL;
|
buffer *b = NULL;
|
||||||
|
|
||||||
if (idx < 0 || (size_t)idx >= editor.bufcount) {
|
if (idx < 0 || (size_t)idx >= editor.bufcount) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (editor.curbuf == (size_t)idx) {
|
if (editor.curbuf == (size_t)idx) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
b = editor.buffers[idx];
|
b = editor.buffers[idx];
|
||||||
editor.curbuf = (size_t)idx;
|
editor.curbuf = (size_t)idx;
|
||||||
editor.dirtyex = 1;
|
editor.dirtyex = 1;
|
||||||
editor_set_status("Switched to buffer %d: %s", editor.curbuf, buffer_name(b));
|
editor_set_status("Switched to buffer %d: %s", editor.curbuf, buffer_name(b));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
buffer_next(void)
|
buffer_next(void)
|
||||||
{
|
{
|
||||||
size_t idx = 0;
|
size_t idx = 0;
|
||||||
|
|
||||||
if (editor.bufcount <= 1) {
|
if (editor.bufcount <= 1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
idx = (editor.curbuf + 1) % editor.bufcount;
|
idx = (editor.curbuf + 1) % editor.bufcount;
|
||||||
buffer_switch((int)idx);
|
buffer_switch((int)idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
buffer_prev(void)
|
buffer_prev(void)
|
||||||
{
|
{
|
||||||
size_t idx = 0;
|
size_t idx = 0;
|
||||||
|
|
||||||
if (editor.bufcount <= 1) {
|
if (editor.bufcount <= 1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
idx = (editor.curbuf == 0) ? (editor.bufcount - 1) : (editor.curbuf - 1);
|
idx = (editor.curbuf == 0) ? (editor.bufcount - 1) : (editor.curbuf - 1);
|
||||||
buffer_switch((int)idx);
|
buffer_switch((int)idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
buffer_close_current(void)
|
buffer_close_current(void)
|
||||||
{
|
{
|
||||||
buffer *b = NULL;
|
buffer *b = NULL;
|
||||||
size_t closing = 0;
|
size_t closing = 0;
|
||||||
int target = 0;
|
int target = 0;
|
||||||
int nb = 0;
|
int nb = 0;
|
||||||
|
|
||||||
/* sanity check */
|
/* sanity check */
|
||||||
if (editor.bufcount == 0 || editor.curbuf >= editor.bufcount) {
|
if (editor.bufcount == 0 || editor.curbuf >= editor.bufcount) {
|
||||||
@@ -464,3 +461,4 @@ buffer_switch_by_name(void)
|
|||||||
|
|
||||||
free(name);
|
free(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
4
buffer.h
4
buffer.h
@@ -19,9 +19,7 @@ typedef struct buffer {
|
|||||||
} buffer;
|
} buffer;
|
||||||
|
|
||||||
|
|
||||||
/* Access current buffer and convenient aliases for file-specific fields */
|
buffer *buffer_current(void);
|
||||||
buffer *buffer_current(void);
|
|
||||||
|
|
||||||
#define CURBUF (buffer_current())
|
#define CURBUF (buffer_current())
|
||||||
#define EROW (CURBUF->row)
|
#define EROW (CURBUF->row)
|
||||||
#define ENROWS (CURBUF->nrows)
|
#define ENROWS (CURBUF->nrows)
|
||||||
|
|||||||
2
core.c
2
core.c
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include "core.h"
|
#include "core.h"
|
||||||
|
|
||||||
|
|
||||||
#ifdef INCLUDE_STRNSTR
|
#ifdef INCLUDE_STRNSTR
|
||||||
/*
|
/*
|
||||||
* Find the first occurrence of find in s, where the search is limited to the
|
* Find the first occurrence of find in s, where the search is limited to the
|
||||||
@@ -111,3 +112,4 @@ die(const char* s)
|
|||||||
perror(s);
|
perror(s);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
5
editor.c
5
editor.c
@@ -16,7 +16,7 @@
|
|||||||
* Global editor instance
|
* Global editor instance
|
||||||
*/
|
*/
|
||||||
struct editor editor = {
|
struct editor editor = {
|
||||||
.cols = 0,
|
.cols = 0,
|
||||||
.rows = 0,
|
.rows = 0,
|
||||||
.mode = 0,
|
.mode = 0,
|
||||||
.killring = NULL,
|
.killring = NULL,
|
||||||
@@ -79,7 +79,6 @@ init_editor(void)
|
|||||||
void
|
void
|
||||||
reset_editor(void)
|
reset_editor(void)
|
||||||
{
|
{
|
||||||
/* Reset the current buffer's contents/state. */
|
|
||||||
buffer *b = buffer_current();
|
buffer *b = buffer_current();
|
||||||
if (b == NULL) {
|
if (b == NULL) {
|
||||||
return;
|
return;
|
||||||
@@ -91,6 +90,7 @@ reset_editor(void)
|
|||||||
}
|
}
|
||||||
free(b->row);
|
free(b->row);
|
||||||
}
|
}
|
||||||
|
|
||||||
b->row = NULL;
|
b->row = NULL;
|
||||||
b->nrows = 0;
|
b->nrows = 0;
|
||||||
b->rowoffs = 0;
|
b->rowoffs = 0;
|
||||||
@@ -107,3 +107,4 @@ reset_editor(void)
|
|||||||
b->mark_curx = 0;
|
b->mark_curx = 0;
|
||||||
b->mark_cury = 0;
|
b->mark_cury = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
5
main.c
5
main.c
@@ -1719,12 +1719,10 @@ editor_openfile(void)
|
|||||||
cur = buffer_current();
|
cur = buffer_current();
|
||||||
if (editor.bufcount == 1 && buffer_is_unnamed_and_empty(cur)) {
|
if (editor.bufcount == 1 && buffer_is_unnamed_and_empty(cur)) {
|
||||||
open_file(filename);
|
open_file(filename);
|
||||||
buffer_save_current();
|
|
||||||
} else {
|
} else {
|
||||||
nb = buffer_add_empty();
|
nb = buffer_add_empty();
|
||||||
buffer_switch(nb);
|
buffer_switch(nb);
|
||||||
open_file(filename);
|
open_file(filename);
|
||||||
buffer_save_current();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free(filename);
|
free(filename);
|
||||||
@@ -2891,7 +2889,6 @@ main(int argc, char *argv[])
|
|||||||
pending_line = 0;
|
pending_line = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer_save_current();
|
|
||||||
first_loaded = 1;
|
first_loaded = 1;
|
||||||
} else {
|
} else {
|
||||||
nb = buffer_add_empty();
|
nb = buffer_add_empty();
|
||||||
@@ -2901,8 +2898,6 @@ main(int argc, char *argv[])
|
|||||||
jump_to_position(0, pending_line - 1);
|
jump_to_position(0, pending_line - 1);
|
||||||
pending_line = 0;
|
pending_line = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer_save_current();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
2
term.h
2
term.h
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include "abuf.h"
|
#include "abuf.h"
|
||||||
|
|
||||||
|
|
||||||
/* Terminal control/setup API */
|
/* Terminal control/setup API */
|
||||||
void enable_termraw(void);
|
void enable_termraw(void);
|
||||||
void disable_termraw(void);
|
void disable_termraw(void);
|
||||||
@@ -19,4 +20,5 @@ void display_clear(abuf *ab);
|
|||||||
*/
|
*/
|
||||||
int get_winsz(size_t *rows, size_t *cols);
|
int get_winsz(size_t *rows, size_t *cols);
|
||||||
|
|
||||||
|
|
||||||
#endif /* KE_TERM_H */
|
#endif /* KE_TERM_H */
|
||||||
|
|||||||
Reference in New Issue
Block a user