another cleanup

This commit is contained in:
Kyle Isom 2020-02-12 18:37:11 -08:00
parent 5de05e3f0a
commit 8ed46b1087
2 changed files with 109 additions and 81 deletions

View File

@ -1,18 +1,24 @@
BIN := ke
OBJS := main.o
INSTROOT := $(HOME)
VERSION := 0.9.2
LDFLAGS :=
CFLAGS := -pedantic -Wall -Werror -Wextra -O2 -std=c99 -g -fno-builtin-memmove
CFLAGS := -pedantic -Wall -Werror -Wextra -O2 -std=c99 -g
CFLAGS += -fno-builtin-memmove -DKE_VERSION="\"$(VERSION)\""
.PHONY: all
all: build install
all: build
.PHONY: build
build: $(BIN)
$(BIN): $(OBJS) defs.h
$(BIN): $(OBJS)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS)
$(BIN).1.txt: $(BIN).1
mandoc -Tutf8 $(BIN).1 > $@
.PHONY: clean
clean:
rm -f $(BIN) $(OBJS) *.core keypress
@ -26,11 +32,19 @@ keypress: keypress.c
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ keypress.c
.PHONY: install
install: $(BIN)
install -C $(BIN) $(HOME)/bin/
install: $(BIN) $(BIN).1
install -d $(INSTROOT)/bin/
install -d $(INSTROOT)/share/man/man1
install -C $(BIN) $(INSTROOT)/bin/
install -C $(BIN).1 $(INSTROOT)/share/man/man1/$(BIN).1
.PHONY: upload
upload: $(BIN).1.txt
scp main.c p.kyleisom.net:/var/www/sites/p/ke/$(BIN)_$(VERSION).c.txt
scp $(BIN).1.txt p.kyleisom.net:/var/www/sites/p/ke/$(BIN).1.txt
.PHONY: cloc
cloc:
cloc main.c defs.h abuf.c erow.c
cloc main.c
%.o: %.c

126
ke/main.c
View File

@ -3,7 +3,8 @@
*
* first version is a run-through of the kilo editor walkthrough as a
* set of guiderails. I've made a lot of changes and did some things
* differently. keep an eye for for kte, kyle's text editor - the rewrite
* differently. keep an eye for for kte, kyle's text editor - the
* rewrite that will be coming out... when it comes out.
*
* https://viewsourcecode.org/snaptoken/kilo/
*/
@ -22,7 +23,6 @@
#include <unistd.h>
#define KE_VERSION "0.9.1"
#define ESCSEQ "\x1b["
#define CTRL_KEY(key) ((key)&0x1f)
#define TAB_STOP 8
@ -32,6 +32,7 @@
* define the keyboard input modes
* normal: no special mode
* kcommand: ^k commands
* escape: what happens when you hit escape?
*/
#define MODE_NORMAL 0
#define MODE_KCOMMAND 1
@ -42,19 +43,21 @@
/*
* abuf is an append buffer.
* Function declarations.
*/
/* append buffer */
struct abuf {
char *b;
int len;
};
#define ABUF_INIT {NULL, 0}
void ab_append(struct abuf *buf, const char *s, int len);
void ab_free(struct abuf *buf);
/*
* erow is an editor row
*/
/* editor row */
struct erow {
char *line;
char *render;
@ -63,34 +66,7 @@ struct erow {
int rsize;
};
struct editor_t {
struct termios entry_term;
int rows, cols;
int curx, cury;
int rx;
int mode;
int nrows;
int rowoffs, coloffs;
struct erow *row;
char *filename;
int dirty;
int dirtyex;
char msg[80];
time_t msgtm;
} editor;
/*
* Function declarations.
*/
/* append buffer */
void ab_append(struct abuf *buf, const char *s, int len);
void ab_free(struct abuf *buf);
/* editor row */
char nibble_to_hex(char c);
int erow_render_to_cursor(struct erow *row, int cx);
int erow_cursor_to_render(struct erow *row, int rx);
void erow_update(struct erow *row);
@ -99,8 +75,43 @@ void erow_free(struct erow *row);
void editor_set_status(const char *fmt, ...);
/* miscellaneous */
void die(const char *s);
int get_winsz(int *rows, int *cols);
void goto_line();
void delete_row(int at);
void row_append_row(struct erow *row, char *s, int len);
void row_insert_ch(struct erow *row, int at, int16_t c);
void row_delete_ch(struct erow *row, int at);
void insertch(int16_t c);
void deletech();
void open_file(const char *filename);
char *rows_to_buffer(int *buflen);
int save_file();
uint16_t is_arrow_key(int16_t c);
int16_t get_keypress();
void display_refresh();
void editor_find_callback(char *query, int16_t c);
void editor_find();
char *editor_prompt(char *, void (*cb)(char *, int16_t));
void editor_openfile();
void move_cursor(int16_t c);
void newline();
void process_kcommand(int16_t c);
void process_normal(int16_t c);
void process_escape(int16_t c);
int process_keypress();
void enable_termraw();
void display_clear(struct abuf *ab);
void disable_termraw();
void setup_terminal();
void draw_rows(struct abuf *ab);
char status_mode_char();
void draw_status_bar(struct abuf *ab);
void draw_message_line(struct abuf *ab);
void scroll();
void display_refresh();
void editor_set_status(const char *fmt, ...);
void loop();
void init_editor();
void process_normal(int16_t c);
void disable_termraw();
@ -122,6 +133,23 @@ enum KeyPress {
};
struct editor_t {
struct termios entry_term;
int rows, cols;
int curx, cury;
int rx;
int mode;
int nrows;
int rowoffs, coloffs;
struct erow *row;
char *filename;
int dirty;
int dirtyex;
char msg[80];
time_t msgtm;
} editor;
void
ab_append(struct abuf *buf, const char *s, int len)
{
@ -145,11 +173,10 @@ ab_free(struct abuf *buf)
}
static char
byte_to_hihex(char c)
char
nibble_to_hex(char c)
{
c = (c >> 4) & 0xf;
c &= 0xf;
if (c < 10) {
return c + 0x30;
}
@ -157,18 +184,6 @@ byte_to_hihex(char c)
}
static char
byte_to_lohex(char c)
{
c = c & 0x0f;
if (c < 10) {
return c + 0x30;
}
return c + 0x37;
}
int
erow_render_to_cursor(struct erow *row, int cx)
{
@ -244,8 +259,8 @@ erow_update(struct erow *row)
} while ((i % TAB_STOP) != 0);
} else if (!isprint(row->line[j])) {
row->render[i++] = '\\';
row->render[i++] = byte_to_hihex(row->line[j]);
row->render[i++] = byte_to_lohex(row->line[j]);
row->render[i++] = nibble_to_hex(row->line[j] >> 4);
row->render[i++] = nibble_to_hex(row->line[j] & 0x0f);
} else {
row->render[i++] = row->line[j];
}
@ -1084,8 +1099,7 @@ process_keypress()
* A text editor needs the terminal to be in raw mode; but the default
* is to be in canonical (cooked) mode, which is a buffered input mode.
*/
static void
void
enable_termraw()
{
struct termios raw;
@ -1203,7 +1217,7 @@ draw_rows(struct abuf *ab)
}
static char
char
status_mode_char()
{
switch (editor.mode) {