2020-02-10 06:58:10 +00:00
|
|
|
/*
|
|
|
|
* kyle's editor
|
|
|
|
*
|
|
|
|
* first version is a run-through of the kilo editor walkthrough.
|
|
|
|
* https://viewsourcecode.org/snaptoken/kilo/
|
|
|
|
*/
|
2020-02-08 08:40:21 +00:00
|
|
|
#include <sys/ioctl.h>
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-11 07:43:28 +00:00
|
|
|
#include <assert.h>
|
2020-02-08 04:46:43 +00:00
|
|
|
#include <ctype.h>
|
2020-02-08 07:07:59 +00:00
|
|
|
#include <errno.h>
|
2020-02-11 06:02:11 +00:00
|
|
|
#include <fcntl.h>
|
2020-02-11 01:01:21 +00:00
|
|
|
#include <stdarg.h>
|
2020-02-08 04:46:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2020-02-08 08:40:21 +00:00
|
|
|
#include <string.h>
|
2020-02-08 04:46:43 +00:00
|
|
|
#include <termios.h>
|
2020-02-11 01:01:21 +00:00
|
|
|
#include <time.h>
|
2020-02-08 04:46:43 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-02-12 00:38:06 +00:00
|
|
|
#include "defs.h"
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-08 04:46:43 +00:00
|
|
|
|
2020-02-08 10:22:31 +00:00
|
|
|
#define KE_VERSION "0.0.1-pre"
|
2020-02-08 08:40:21 +00:00
|
|
|
#define ESCSEQ "\x1b["
|
|
|
|
#define CTRL_KEY(key) ((key)&0x1f)
|
2020-02-10 15:43:36 +00:00
|
|
|
#define TAB_STOP 8
|
2020-02-11 06:02:11 +00:00
|
|
|
#define MSG_TIMEO 3
|
2020-02-08 08:40:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* define the keyboard input modes
|
|
|
|
* normal: no special mode
|
|
|
|
* kcommand: ^k commands
|
|
|
|
*/
|
|
|
|
#define MODE_NORMAL 0
|
|
|
|
#define MODE_KCOMMAND 1
|
2020-02-08 10:22:31 +00:00
|
|
|
#define MODE_INSERT 2
|
2020-02-08 08:40:21 +00:00
|
|
|
|
|
|
|
|
2020-02-11 06:02:11 +00:00
|
|
|
void editor_set_status(const char *fmt, ...);
|
|
|
|
void display_refresh();
|
2020-02-11 07:37:31 +00:00
|
|
|
char *editor_prompt(char *, void (*cb)(char *, int16_t));
|
2020-02-12 06:47:57 +00:00
|
|
|
void init_editor();
|
|
|
|
void process_normal(int16_t c);
|
2020-02-11 06:02:11 +00:00
|
|
|
|
|
|
|
|
2020-02-08 10:22:31 +00:00
|
|
|
enum KeyPress {
|
2020-02-11 06:02:11 +00:00
|
|
|
TAB_KEY = 9,
|
|
|
|
ESC_KEY = 27,
|
|
|
|
BACKSPACE = 127,
|
|
|
|
ARROW_LEFT = 1000,
|
2020-02-08 10:22:31 +00:00
|
|
|
ARROW_RIGHT,
|
2020-02-11 06:02:11 +00:00
|
|
|
ARROW_UP,
|
|
|
|
ARROW_DOWN,
|
2020-02-08 10:30:32 +00:00
|
|
|
DEL_KEY,
|
|
|
|
HOME_KEY,
|
2020-02-11 06:02:11 +00:00
|
|
|
END_KEY,
|
2020-02-08 10:22:31 +00:00
|
|
|
PG_UP,
|
|
|
|
PG_DN,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void disable_termraw();
|
|
|
|
|
2020-02-08 08:40:21 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
die(const char *s)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* NOTE(kyle): this is a duplication of the code in display.c
|
|
|
|
* but I would like to be able to import these files from there.
|
|
|
|
*/
|
|
|
|
write(STDOUT_FILENO, "\x1b[2J", 4);
|
|
|
|
write(STDOUT_FILENO, "\x1b[H", 3);
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-08 08:40:21 +00:00
|
|
|
perror(s);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get_winsz uses the TIOCGWINSZ to get the window size.
|
|
|
|
*
|
|
|
|
* there's a fallback way to do this, too, that involves moving the
|
|
|
|
* cursor down and to the left \x1b[999C\x1b[999B. I'm going to skip
|
|
|
|
* on this for now because it's bloaty and this works on OpenBSD and
|
|
|
|
* Linux, at least.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
get_winsz(int *rows, int *cols)
|
|
|
|
{
|
|
|
|
struct winsize ws;
|
|
|
|
|
|
|
|
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 ||
|
|
|
|
ws.ws_col == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*cols = ws.ws_col;
|
|
|
|
*rows = ws.ws_row;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-11 06:02:11 +00:00
|
|
|
void
|
|
|
|
delete_row(int at)
|
|
|
|
{
|
2020-02-12 01:16:31 +00:00
|
|
|
if (at < 0 || at >= editor->nrows) {
|
2020-02-11 17:52:49 +00:00
|
|
|
return;
|
2020-02-11 06:02:11 +00:00
|
|
|
}
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
erow_free(&editor->row[at]);
|
|
|
|
memmove(&editor->row[at], &editor->row[at+1],
|
|
|
|
sizeof(struct erow) * (editor->nrows - at - 1));
|
|
|
|
editor->nrows--;
|
|
|
|
editor->dirty++;
|
2020-02-11 06:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2020-02-12 00:38:06 +00:00
|
|
|
row_append_row(struct erow *row, char *s, int len)
|
2020-02-11 06:02:11 +00:00
|
|
|
{
|
|
|
|
row->line = realloc(row->line, row->size + len + 1);
|
2020-02-11 07:43:28 +00:00
|
|
|
assert(row->line != NULL);
|
2020-02-11 06:02:11 +00:00
|
|
|
memcpy(&row->line[row->size], s, len);
|
|
|
|
row->size += len;
|
|
|
|
row->line[row->size] = '\0';
|
2020-02-12 00:38:06 +00:00
|
|
|
erow_update(row);
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->dirty++;
|
2020-02-11 06:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2020-02-12 00:38:06 +00:00
|
|
|
row_insert_ch(struct erow *row, int at, int16_t c)
|
2020-02-11 06:02:11 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* row_insert_ch just concerns itself with how to update a row.
|
|
|
|
*/
|
|
|
|
if ((at < 0) || (at > row->size)) {
|
|
|
|
at = row->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
row->line = realloc(row->line, row->size+2);
|
2020-02-11 07:43:28 +00:00
|
|
|
assert(row->line != NULL);
|
2020-02-11 06:02:11 +00:00
|
|
|
memmove(&row->line[at+1], &row->line[at], row->size - at + 1);
|
|
|
|
row->size++;
|
|
|
|
row->line[at] = c;
|
|
|
|
|
2020-02-12 00:38:06 +00:00
|
|
|
erow_update(row);
|
2020-02-11 06:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2020-02-12 00:38:06 +00:00
|
|
|
row_delete_ch(struct erow *row, int at)
|
2020-02-11 06:02:11 +00:00
|
|
|
{
|
|
|
|
if (at < 0 || at >= row->size) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memmove(&row->line[at], &row->line[at+1], row->size+1);
|
|
|
|
row->size--;
|
2020-02-12 00:38:06 +00:00
|
|
|
erow_update(row);
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->dirty++;
|
2020-02-11 06:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
insertch(int16_t c)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* insert_ch doesn't need to worry about how to update a
|
|
|
|
* a row; it can just figure out where the cursor is
|
|
|
|
* at and what to do.
|
|
|
|
*/
|
2020-02-12 01:16:31 +00:00
|
|
|
if (editor->cury == editor->nrows) {
|
|
|
|
erow_insert(editor->nrows, "", 0);
|
2020-02-11 06:02:11 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
row_insert_ch(&editor->row[editor->cury], editor->curx, c);
|
|
|
|
editor->curx++;
|
|
|
|
editor->dirty++;
|
2020-02-11 06:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
deletech()
|
|
|
|
{
|
2020-02-12 00:38:06 +00:00
|
|
|
struct erow *row = NULL;
|
2020-02-11 06:02:11 +00:00
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
if (editor->cury == editor->nrows) {
|
2020-02-11 06:02:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
if (editor->cury == 0 && editor->curx == 0) {
|
2020-02-11 06:02:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
row = &editor->row[editor->cury];
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
if (editor->curx > 0) {
|
|
|
|
row_delete_ch(row, editor->curx - 1);
|
|
|
|
editor->curx--;
|
2020-02-11 06:02:11 +00:00
|
|
|
} else {
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->curx = editor->row[editor->cury - 1].size;
|
|
|
|
row_append_row(&editor->row[editor->cury - 1], row->line,
|
2020-02-11 06:02:11 +00:00
|
|
|
row->size);
|
2020-02-12 01:16:31 +00:00
|
|
|
delete_row(editor->cury);
|
|
|
|
editor->cury--;
|
2020-02-11 06:02:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-09 06:23:32 +00:00
|
|
|
void
|
|
|
|
open_file(const char *filename)
|
|
|
|
{
|
|
|
|
char *line = NULL;
|
|
|
|
size_t linecap = 0;
|
|
|
|
ssize_t linelen;
|
2020-02-11 06:02:11 +00:00
|
|
|
FILE *fp = NULL;
|
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
free(editor->filename);
|
|
|
|
editor->filename = strdup(filename);
|
|
|
|
assert(editor->filename != NULL);
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->dirty = 0;
|
2020-02-11 06:02:11 +00:00
|
|
|
if ((fp = fopen(filename, "r")) == NULL) {
|
|
|
|
if (errno == ENOENT) {
|
2020-02-12 01:16:31 +00:00
|
|
|
printf("no such file");
|
2020-02-11 06:02:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-02-09 06:23:32 +00:00
|
|
|
die("fopen");
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((linelen = getline(&line, &linecap, fp)) != -1) {
|
|
|
|
if (linelen != -1) {
|
|
|
|
while ((linelen > 0) && ((line[linelen-1] == '\r') ||
|
|
|
|
(line[linelen-1] == '\n'))) {
|
|
|
|
linelen--;
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
erow_insert(editor->nrows, line, linelen);
|
2020-02-09 06:23:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(line);
|
2020-02-11 07:49:05 +00:00
|
|
|
line = NULL;
|
2020-02-09 06:23:32 +00:00
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-11 06:02:11 +00:00
|
|
|
/*
|
|
|
|
* convert our rows to a buffer; caller must free it.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
rows_to_buffer(int *buflen)
|
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
int j;
|
|
|
|
char *buf = NULL;
|
|
|
|
char *p = NULL;
|
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
for (j = 0; j < editor->nrows; j++) {
|
2020-02-11 06:02:11 +00:00
|
|
|
/* extra byte for newline */
|
2020-02-12 01:16:31 +00:00
|
|
|
len += editor->row[j].size+1;
|
2020-02-11 06:02:11 +00:00
|
|
|
}
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-11 07:49:05 +00:00
|
|
|
if (len == 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-02-11 06:02:11 +00:00
|
|
|
|
|
|
|
*buflen = len;
|
2020-02-11 07:49:05 +00:00
|
|
|
buf = malloc(len);
|
|
|
|
assert(buf != NULL);
|
2020-02-11 06:02:11 +00:00
|
|
|
p = buf;
|
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
for (j = 0; j < editor->nrows; j++) {
|
|
|
|
memcpy(p, editor->row[j].line, editor->row[j].size);
|
|
|
|
p += editor->row[j].size;
|
2020-02-11 06:02:11 +00:00
|
|
|
*p++ = '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
save_file()
|
|
|
|
{
|
|
|
|
int fd = -1;
|
|
|
|
int len;
|
|
|
|
int status = 1; /* will be used as exit code */
|
|
|
|
char *buf;
|
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
if (editor->filename == NULL) {
|
|
|
|
editor->filename = editor_prompt("Filename: %s", NULL);
|
|
|
|
if (editor->filename == NULL) {
|
2020-02-11 06:02:11 +00:00
|
|
|
editor_set_status("Save aborted.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = rows_to_buffer(&len);
|
2020-02-12 01:16:31 +00:00
|
|
|
if ((fd = open(editor->filename, O_RDWR|O_CREAT, 0644)) == -1) {
|
2020-02-11 06:02:11 +00:00
|
|
|
goto save_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (-1 == ftruncate(fd, len)) {
|
|
|
|
goto save_exit;
|
|
|
|
}
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-11 06:02:11 +00:00
|
|
|
if (len == 0) {
|
|
|
|
status = 0;
|
|
|
|
goto save_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ssize_t)len != write(fd, buf, len)) {
|
|
|
|
goto save_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = 0;
|
|
|
|
|
|
|
|
save_exit:
|
|
|
|
if (fd) close(fd);
|
|
|
|
if (buf) free(buf);
|
|
|
|
|
|
|
|
if (status != 0) {
|
|
|
|
buf = strerror(errno);
|
2020-02-12 01:16:31 +00:00
|
|
|
editor_set_status("Error writing %s: %s", editor->filename,
|
2020-02-11 06:02:11 +00:00
|
|
|
buf);
|
|
|
|
} else {
|
|
|
|
editor_set_status("Wrote %d bytes to %s.", len,
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->filename);
|
|
|
|
editor->dirty = 0;
|
2020-02-11 06:02:11 +00:00
|
|
|
}
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-11 06:02:11 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-08 10:22:31 +00:00
|
|
|
uint16_t
|
|
|
|
is_arrow_key(int16_t c)
|
|
|
|
{
|
|
|
|
switch (c) {
|
|
|
|
case ARROW_DOWN:
|
|
|
|
case ARROW_LEFT:
|
|
|
|
case ARROW_RIGHT:
|
2020-02-11 01:01:21 +00:00
|
|
|
case ARROW_UP:
|
2020-02-08 10:22:31 +00:00
|
|
|
case CTRL_KEY('p'):
|
|
|
|
case CTRL_KEY('n'):
|
|
|
|
case CTRL_KEY('f'):
|
|
|
|
case CTRL_KEY('b'):
|
2020-02-11 01:01:21 +00:00
|
|
|
case CTRL_KEY('a'):
|
|
|
|
case CTRL_KEY('e'):
|
2020-02-08 10:30:32 +00:00
|
|
|
case END_KEY:
|
2020-02-11 01:01:21 +00:00
|
|
|
case HOME_KEY:
|
|
|
|
case PG_DN:
|
|
|
|
case PG_UP:
|
2020-02-08 10:22:31 +00:00
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int16_t
|
2020-02-08 08:40:21 +00:00
|
|
|
get_keypress()
|
|
|
|
{
|
2020-02-08 10:22:31 +00:00
|
|
|
char seq[3];
|
|
|
|
char c = -1;
|
2020-02-08 08:40:21 +00:00
|
|
|
|
|
|
|
if (read(STDIN_FILENO, &c, 1) == -1) {
|
|
|
|
die("get_keypress:read");
|
|
|
|
}
|
|
|
|
|
2020-02-08 10:22:31 +00:00
|
|
|
if (c == 0x1b) {
|
|
|
|
if (read(STDIN_FILENO, &seq[0], 1) != 1) return c;
|
|
|
|
if (read(STDIN_FILENO, &seq[1], 1) != 1) return c;
|
|
|
|
|
|
|
|
if (seq[0] == '[') {
|
|
|
|
if (seq[1] < 'A') {
|
|
|
|
if (read(STDIN_FILENO, &seq[2], 1) != 1)
|
|
|
|
return c;
|
|
|
|
if (seq[2] == '~') {
|
|
|
|
switch (seq[1]) {
|
2020-02-08 10:30:32 +00:00
|
|
|
case '1': return HOME_KEY;
|
2020-02-11 01:01:21 +00:00
|
|
|
case '2': return /* INS_KEY */ c;
|
2020-02-08 10:30:32 +00:00
|
|
|
case '3': return DEL_KEY;
|
2020-02-11 01:01:21 +00:00
|
|
|
case '4': return END_KEY;
|
2020-02-08 10:22:31 +00:00
|
|
|
case '5': return PG_UP;
|
|
|
|
case '6': return PG_DN;
|
2020-02-08 10:30:32 +00:00
|
|
|
case '7': return HOME_KEY;
|
|
|
|
case '8': return END_KEY;
|
2020-02-08 10:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (seq[1]) {
|
|
|
|
case 'A': return ARROW_UP;
|
|
|
|
case 'B': return ARROW_DOWN;
|
|
|
|
case 'C': return ARROW_RIGHT;
|
|
|
|
case 'D': return ARROW_LEFT;
|
2020-02-08 10:30:32 +00:00
|
|
|
case 'F': return END_KEY;
|
|
|
|
case 'H': return HOME_KEY;
|
2020-02-08 10:22:31 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
/* nada */ ;
|
|
|
|
}
|
|
|
|
}
|
2020-02-08 10:30:32 +00:00
|
|
|
} else if (seq[0] == 'O') {
|
|
|
|
switch (seq[1]) {
|
|
|
|
case 'F': return END_KEY;
|
|
|
|
case 'H': return HOME_KEY;
|
|
|
|
}
|
2020-02-08 10:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0x1b;
|
|
|
|
}
|
|
|
|
|
2020-02-08 08:40:21 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-11 06:02:11 +00:00
|
|
|
char *
|
2020-02-11 07:37:31 +00:00
|
|
|
editor_prompt(char *prompt, void (*cb)(char *, int16_t))
|
2020-02-11 06:02:11 +00:00
|
|
|
{
|
|
|
|
size_t bufsz = 128;
|
|
|
|
char *buf = malloc(bufsz);
|
|
|
|
size_t buflen = 0;
|
|
|
|
int16_t c;
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-11 06:02:11 +00:00
|
|
|
buf[0] = '\0';
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-11 06:02:11 +00:00
|
|
|
while (1) {
|
|
|
|
editor_set_status(prompt, buf);
|
|
|
|
display_refresh();
|
|
|
|
while ((c = get_keypress()) <= 0) ;
|
|
|
|
if (c == DEL_KEY || c == CTRL_KEY('h') || c == BACKSPACE) {
|
|
|
|
if (buflen != 0) {
|
2020-02-11 17:52:49 +00:00
|
|
|
buf[--buflen] = '\0';
|
2020-02-11 06:02:11 +00:00
|
|
|
}
|
|
|
|
} else if (c == ESC_KEY) {
|
|
|
|
editor_set_status("");
|
2020-02-11 07:37:31 +00:00
|
|
|
if (cb) {
|
2020-02-11 17:52:49 +00:00
|
|
|
cb(buf, c);
|
2020-02-11 07:37:31 +00:00
|
|
|
}
|
2020-02-11 06:02:11 +00:00
|
|
|
free(buf);
|
|
|
|
return NULL;
|
|
|
|
} else if (c == '\r') {
|
|
|
|
if (buflen != 0) {
|
|
|
|
editor_set_status("");
|
2020-02-11 07:37:31 +00:00
|
|
|
if (cb) {
|
|
|
|
cb(buf, c);
|
|
|
|
}
|
2020-02-11 06:02:11 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
} else if (!iscntrl(c) && c < 128) {
|
|
|
|
if (buflen == bufsz - 1) {
|
|
|
|
bufsz *= 2;
|
|
|
|
buf = realloc(buf, bufsz);
|
2020-02-11 07:43:28 +00:00
|
|
|
assert(buf != NULL);
|
2020-02-11 06:02:11 +00:00
|
|
|
}
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-11 06:02:11 +00:00
|
|
|
buf[buflen++] = c;
|
|
|
|
buf[buflen] = '\0';
|
|
|
|
}
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-11 07:37:31 +00:00
|
|
|
if (cb) {
|
|
|
|
cb(buf, c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
editor_find_callback(char *query, int16_t c)
|
|
|
|
{
|
|
|
|
static int lmatch = -1;
|
|
|
|
static int dir = 1;
|
|
|
|
int i, current;
|
|
|
|
char *match;
|
2020-02-12 00:38:06 +00:00
|
|
|
struct erow *row;
|
2020-02-11 07:37:31 +00:00
|
|
|
|
|
|
|
if (c == '\r' || c == ESC_KEY) {
|
|
|
|
/* reset search */
|
|
|
|
lmatch = -1;
|
|
|
|
dir = 1;
|
|
|
|
return;
|
|
|
|
} else if (c == ARROW_RIGHT || c == ARROW_DOWN) {
|
2020-02-11 17:52:49 +00:00
|
|
|
dir = 1;
|
2020-02-11 07:37:31 +00:00
|
|
|
} else if (c == ARROW_LEFT || c == ARROW_UP) {
|
|
|
|
dir = -1;
|
|
|
|
} else {
|
|
|
|
lmatch = -1;
|
|
|
|
dir = 1;
|
|
|
|
}
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-11 07:37:31 +00:00
|
|
|
if (lmatch == -1) {
|
|
|
|
dir = 1;
|
|
|
|
}
|
|
|
|
current = lmatch;
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
for (i = 0; i < editor->nrows; i++) {
|
2020-02-11 07:37:31 +00:00
|
|
|
current += dir;
|
|
|
|
if (current == -1) {
|
2020-02-12 01:16:31 +00:00
|
|
|
current = editor->nrows - 1;
|
|
|
|
} else if (current == editor->nrows) {
|
2020-02-11 17:52:49 +00:00
|
|
|
current = 0;
|
2020-02-11 07:37:31 +00:00
|
|
|
}
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
row = &editor->row[current];
|
2020-02-11 07:37:31 +00:00
|
|
|
match = strstr(row->render, query);
|
|
|
|
if (match) {
|
|
|
|
lmatch = current;
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->cury = current;
|
|
|
|
editor->curx = erow_render_to_cursor(row,
|
2020-02-11 07:37:31 +00:00
|
|
|
match - row->render);
|
2020-02-11 17:52:49 +00:00
|
|
|
/*
|
2020-02-11 07:37:31 +00:00
|
|
|
* after this, scroll will put the match line at
|
|
|
|
* the top of the screen.
|
|
|
|
*/
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->rowoffs = editor->nrows;
|
2020-02-11 07:37:31 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-02-11 06:02:11 +00:00
|
|
|
}
|
2020-02-11 17:52:49 +00:00
|
|
|
|
|
|
|
display_refresh();
|
2020-02-11 07:37:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
editor_find()
|
|
|
|
{
|
|
|
|
char *query;
|
2020-02-12 01:16:31 +00:00
|
|
|
int scx = editor->curx;
|
|
|
|
int scy = editor->cury;
|
|
|
|
int sco = editor->coloffs;
|
|
|
|
int sro = editor->rowoffs;
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-11 07:37:31 +00:00
|
|
|
query = editor_prompt("Search (ESC to cancel): %s",
|
|
|
|
editor_find_callback);
|
|
|
|
if (query) {
|
|
|
|
free(query);
|
|
|
|
} else {
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->curx = scx;
|
|
|
|
editor->cury = scy;
|
|
|
|
editor->coloffs = sco;
|
|
|
|
editor->rowoffs = sro;
|
2020-02-11 07:37:31 +00:00
|
|
|
}
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-11 07:37:31 +00:00
|
|
|
display_refresh();
|
2020-02-11 06:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-12 06:47:57 +00:00
|
|
|
void
|
|
|
|
editor_openfile()
|
|
|
|
{
|
|
|
|
char *filename;
|
|
|
|
|
|
|
|
/* TODO(kyle): combine with dirutils for tab-completion */
|
|
|
|
filename = editor_prompt("Load file: %s", NULL);
|
|
|
|
if (filename == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(editor->row);
|
|
|
|
init_editor();
|
|
|
|
|
|
|
|
open_file(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-08 10:22:31 +00:00
|
|
|
void
|
|
|
|
move_cursor(int16_t c)
|
|
|
|
{
|
2020-02-12 00:38:06 +00:00
|
|
|
struct erow *row;
|
2020-02-10 06:58:10 +00:00
|
|
|
int reps;
|
2020-02-08 10:22:31 +00:00
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
row = (editor->cury >= editor->nrows) ? NULL : &editor->row[editor->cury];
|
2020-02-12 00:38:06 +00:00
|
|
|
|
2020-02-08 10:22:31 +00:00
|
|
|
switch (c) {
|
|
|
|
case ARROW_UP:
|
|
|
|
case CTRL_KEY('p'):
|
2020-02-12 01:16:31 +00:00
|
|
|
if (editor->cury > 0) {
|
|
|
|
editor->cury--;
|
2020-02-10 06:58:10 +00:00
|
|
|
}
|
2020-02-08 10:22:31 +00:00
|
|
|
break;
|
|
|
|
case ARROW_DOWN:
|
|
|
|
case CTRL_KEY('n'):
|
2020-02-12 01:16:31 +00:00
|
|
|
if (editor->cury < editor->nrows) {
|
|
|
|
editor->cury++;
|
2020-02-10 06:58:10 +00:00
|
|
|
}
|
2020-02-08 10:22:31 +00:00
|
|
|
break;
|
|
|
|
case ARROW_RIGHT:
|
|
|
|
case CTRL_KEY('f'):
|
2020-02-12 01:16:31 +00:00
|
|
|
if (row && editor->curx < row->size) {
|
|
|
|
editor->curx++;
|
|
|
|
} else if (row && editor->curx == row->size) {
|
|
|
|
editor->cury++;
|
|
|
|
editor->curx = 0;
|
2020-02-10 06:58:10 +00:00
|
|
|
}
|
2020-02-08 10:22:31 +00:00
|
|
|
break;
|
|
|
|
case ARROW_LEFT:
|
|
|
|
case CTRL_KEY('b'):
|
2020-02-12 01:16:31 +00:00
|
|
|
if (editor->curx > 0) {
|
|
|
|
editor->curx--;
|
|
|
|
} else if (editor->cury > 0) {
|
|
|
|
editor->cury--;
|
|
|
|
editor->curx = editor->row[editor->cury].size;
|
2020-02-10 06:58:10 +00:00
|
|
|
}
|
2020-02-08 10:22:31 +00:00
|
|
|
break;
|
|
|
|
case PG_UP:
|
|
|
|
case PG_DN: {
|
2020-02-10 15:43:36 +00:00
|
|
|
if (c == PG_UP) {
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->cury = editor->rowoffs;
|
2020-02-10 15:43:36 +00:00
|
|
|
} else if (c == PG_DN) {
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->cury = editor->rowoffs + editor->rows-1;
|
|
|
|
if (editor->cury > editor->nrows) {
|
|
|
|
editor->cury = editor->nrows;
|
2020-02-10 15:43:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
reps = editor->rows;
|
2020-02-08 10:22:31 +00:00
|
|
|
while (--reps) {
|
|
|
|
move_cursor(c == PG_UP ? ARROW_UP : ARROW_DOWN);
|
2020-02-10 06:58:10 +00:00
|
|
|
}
|
2020-02-08 10:22:31 +00:00
|
|
|
}
|
2020-02-08 10:30:32 +00:00
|
|
|
|
|
|
|
case HOME_KEY:
|
2020-02-11 01:01:21 +00:00
|
|
|
case CTRL_KEY('a'):
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->curx = 0;
|
2020-02-08 10:30:32 +00:00
|
|
|
break;
|
|
|
|
case END_KEY:
|
2020-02-11 01:01:21 +00:00
|
|
|
case CTRL_KEY('e'):
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->curx = editor->row[editor->cury].size;
|
2020-02-08 10:30:32 +00:00
|
|
|
break;
|
2020-02-08 10:22:31 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-02-10 06:58:10 +00:00
|
|
|
|
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
row = (editor->cury >= editor->nrows) ?
|
|
|
|
NULL : &editor->row[editor->cury];
|
2020-02-10 06:58:10 +00:00
|
|
|
reps = row ? row->size : 0;
|
2020-02-12 01:16:31 +00:00
|
|
|
if (editor->curx > reps) {
|
|
|
|
editor->curx = reps;
|
2020-02-10 06:58:10 +00:00
|
|
|
}
|
2020-02-08 10:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-11 06:02:11 +00:00
|
|
|
void
|
|
|
|
newline()
|
|
|
|
{
|
2020-02-12 00:38:06 +00:00
|
|
|
struct erow *row = NULL;
|
2020-02-11 06:02:11 +00:00
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
if (editor->curx == 0) {
|
|
|
|
erow_insert(editor->cury, "", 0);
|
2020-02-11 06:02:11 +00:00
|
|
|
} else {
|
2020-02-12 01:16:31 +00:00
|
|
|
row = &editor->row[editor->cury];
|
|
|
|
erow_insert(editor->cury + 1, &row->line[editor->curx],
|
|
|
|
row->size - editor->curx);
|
|
|
|
row = &editor->row[editor->cury];
|
|
|
|
row->size = editor->curx;
|
2020-02-11 06:02:11 +00:00
|
|
|
row->line[row->size] = '\0';
|
2020-02-12 00:38:06 +00:00
|
|
|
erow_update(row);
|
2020-02-11 06:02:11 +00:00
|
|
|
}
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->cury++;
|
|
|
|
editor->curx = 0;
|
2020-02-11 06:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-08 10:22:31 +00:00
|
|
|
void
|
|
|
|
process_kcommand(int16_t c)
|
|
|
|
{
|
|
|
|
switch (c) {
|
|
|
|
case 'q':
|
|
|
|
case CTRL_KEY('q'):
|
2020-02-12 01:16:31 +00:00
|
|
|
if (editor->dirty && editor->dirtyex) {
|
2020-02-11 06:02:11 +00:00
|
|
|
editor_set_status("File not saved - C-k q again to quit.");
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->dirtyex = 0;
|
2020-02-11 06:02:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-02-08 10:22:31 +00:00
|
|
|
exit(0);
|
2020-02-11 06:02:11 +00:00
|
|
|
case CTRL_KEY('s'):
|
|
|
|
case 's':
|
|
|
|
save_file();
|
|
|
|
break;
|
|
|
|
case CTRL_KEY('w'):
|
|
|
|
case 'w':
|
|
|
|
exit(save_file());
|
2020-02-08 10:22:31 +00:00
|
|
|
case 'd':
|
2020-02-12 06:47:57 +00:00
|
|
|
while ((editor->row[editor->cury].size - editor->curx) > 0) {
|
|
|
|
process_normal(DEL_KEY);
|
|
|
|
}
|
|
|
|
break;
|
2020-02-08 10:22:31 +00:00
|
|
|
case CTRL_KEY('\\'):
|
|
|
|
/* sometimes it's nice to dump core */
|
|
|
|
disable_termraw();
|
|
|
|
abort();
|
2020-02-12 06:47:57 +00:00
|
|
|
case 'e':
|
|
|
|
case CTRL_KEY('e'):
|
|
|
|
editor_openfile();
|
|
|
|
break;
|
2020-02-11 07:37:31 +00:00
|
|
|
case 'f':
|
|
|
|
editor_find();
|
|
|
|
break;
|
2020-02-11 08:30:21 +00:00
|
|
|
case 'm':
|
|
|
|
if (system("make") != 0) {
|
|
|
|
editor_set_status("process failed: %s", strerror(errno));
|
|
|
|
} else {
|
|
|
|
editor_set_status("make: ok");
|
|
|
|
}
|
|
|
|
break;
|
2020-02-08 10:22:31 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->dirtyex = 1;
|
2020-02-08 10:22:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-11 06:02:11 +00:00
|
|
|
void
|
|
|
|
process_normal(int16_t c)
|
|
|
|
{
|
|
|
|
if (is_arrow_key(c)) {
|
|
|
|
move_cursor(c);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case '\r':
|
|
|
|
newline();
|
|
|
|
break;
|
|
|
|
case CTRL_KEY('k'):
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->mode = MODE_KCOMMAND;
|
2020-02-11 06:02:11 +00:00
|
|
|
return;
|
|
|
|
case BACKSPACE:
|
|
|
|
case CTRL_KEY('h'):
|
|
|
|
case DEL_KEY:
|
|
|
|
if (c == DEL_KEY) {
|
|
|
|
move_cursor(ARROW_RIGHT);
|
|
|
|
}
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-11 06:02:11 +00:00
|
|
|
deletech();
|
|
|
|
break;
|
|
|
|
case CTRL_KEY('l'):
|
|
|
|
/* ignore refresh for now */
|
|
|
|
break;
|
|
|
|
case ESC_KEY:
|
|
|
|
/* TODO(kyle) */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (isprint(c) || c == TAB_KEY) {
|
|
|
|
insertch(c);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->dirtyex = 1;
|
2020-02-11 06:02:11 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 10:22:31 +00:00
|
|
|
|
2020-02-11 01:01:21 +00:00
|
|
|
int
|
2020-02-08 08:40:21 +00:00
|
|
|
process_keypress()
|
|
|
|
{
|
2020-02-08 10:22:31 +00:00
|
|
|
int16_t c = get_keypress();
|
2020-02-08 08:40:21 +00:00
|
|
|
|
2020-02-11 06:02:11 +00:00
|
|
|
|
2020-02-08 10:22:31 +00:00
|
|
|
/* we didn't actually read a key */
|
|
|
|
if (c <= 0) {
|
2020-02-11 01:01:21 +00:00
|
|
|
return 0;
|
2020-02-08 08:40:21 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
switch (editor->mode) {
|
2020-02-08 08:40:21 +00:00
|
|
|
case MODE_KCOMMAND:
|
2020-02-08 10:22:31 +00:00
|
|
|
process_kcommand(c);
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->mode = MODE_NORMAL;
|
2020-02-08 10:22:31 +00:00
|
|
|
break;
|
|
|
|
case MODE_NORMAL:
|
2020-02-11 06:02:11 +00:00
|
|
|
process_normal(c);
|
|
|
|
break;
|
2020-02-08 08:40:21 +00:00
|
|
|
}
|
2020-02-11 01:01:21 +00:00
|
|
|
|
|
|
|
return 1;
|
2020-02-08 08:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
enable_termraw()
|
|
|
|
{
|
|
|
|
struct termios raw;
|
|
|
|
|
|
|
|
/* Read the current terminal parameters for standard input. */
|
|
|
|
if (tcgetattr(STDIN_FILENO, &raw) == -1) {
|
|
|
|
die("tcgetattr while enabling raw mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Put the terminal into raw mode.
|
|
|
|
*/
|
|
|
|
cfmakeraw(&raw);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set timeout for read(2).
|
|
|
|
*
|
|
|
|
* VMIN: what is the minimum number of bytes required for read
|
|
|
|
* to return?
|
|
|
|
*
|
|
|
|
* VTIME: max time before read(2) returns in hundreds of milli-
|
|
|
|
* seconds.
|
|
|
|
*/
|
|
|
|
raw.c_cc[VMIN] = 0;
|
|
|
|
raw.c_cc[VTIME] = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now write the terminal parameters to the current terminal,
|
|
|
|
* after flushing any waiting input out.
|
|
|
|
*/
|
|
|
|
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) {
|
|
|
|
die("tcsetattr while enabling raw mode");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-08 10:22:31 +00:00
|
|
|
void
|
|
|
|
display_clear(struct abuf *ab)
|
|
|
|
{
|
|
|
|
if (ab == NULL) {
|
|
|
|
write(STDOUT_FILENO, ESCSEQ "2J", 4);
|
|
|
|
write(STDOUT_FILENO, ESCSEQ "H", 3);
|
|
|
|
} else {
|
|
|
|
ab_append(ab, ESCSEQ "2J", 4);
|
|
|
|
ab_append(ab, ESCSEQ "H", 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2020-02-08 08:40:21 +00:00
|
|
|
disable_termraw()
|
|
|
|
{
|
|
|
|
display_clear(NULL);
|
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &editor->entry_term) == -1) {
|
2020-02-08 08:40:21 +00:00
|
|
|
die("couldn't disable terminal raw mode");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
setup_terminal()
|
|
|
|
{
|
2020-02-12 01:16:31 +00:00
|
|
|
if (tcgetattr(STDIN_FILENO, &editor->entry_term) == -1) {
|
2020-02-08 08:40:21 +00:00
|
|
|
die("can't snapshot terminal settings");
|
|
|
|
}
|
|
|
|
atexit(disable_termraw);
|
|
|
|
enable_termraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
draw_rows(struct abuf *ab)
|
|
|
|
{
|
2020-02-12 01:16:31 +00:00
|
|
|
assert(editor->cols >= 0);
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
char buf[editor->cols];
|
2020-02-10 06:58:10 +00:00
|
|
|
int buflen, filerow, padding;
|
2020-02-08 08:40:21 +00:00
|
|
|
int y;
|
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
for (y = 0; y < editor->rows; y++) {
|
|
|
|
filerow = y + editor->rowoffs;
|
|
|
|
if (filerow >= editor->nrows) {
|
|
|
|
if ((editor->nrows == 0) && (y == editor->rows / 3)) {
|
2020-02-09 06:23:32 +00:00
|
|
|
buflen = snprintf(buf, sizeof(buf),
|
|
|
|
"ke k%s", KE_VERSION);
|
2020-02-12 01:16:31 +00:00
|
|
|
padding = (editor->rows - buflen) / 2;
|
2020-02-09 06:23:32 +00:00
|
|
|
|
|
|
|
if (padding) {
|
|
|
|
ab_append(ab, "|", 1);
|
|
|
|
padding--;
|
|
|
|
}
|
2020-02-08 10:22:31 +00:00
|
|
|
|
2020-02-09 06:23:32 +00:00
|
|
|
while (padding--) ab_append(ab, " ", 1);
|
|
|
|
ab_append(ab, buf, buflen);
|
|
|
|
} else {
|
|
|
|
ab_append(ab, "|", 1);
|
|
|
|
}
|
|
|
|
} else {
|
2020-02-12 01:16:31 +00:00
|
|
|
buflen = editor->row[filerow].rsize - editor->coloffs;
|
2020-02-10 06:58:10 +00:00
|
|
|
if (buflen < 0) {
|
|
|
|
buflen = 0;
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
if (buflen > editor->cols) {
|
|
|
|
buflen = editor->cols;
|
2020-02-09 06:23:32 +00:00
|
|
|
}
|
2020-02-12 01:16:31 +00:00
|
|
|
ab_append(ab, editor->row[filerow].render+editor->coloffs,
|
2020-02-10 06:58:10 +00:00
|
|
|
buflen);
|
2020-02-09 06:23:32 +00:00
|
|
|
}
|
|
|
|
ab_append(ab, ESCSEQ "K", 3);
|
2020-02-11 01:01:21 +00:00
|
|
|
ab_append(ab, "\r\n", 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
draw_status_bar(struct abuf *ab)
|
|
|
|
{
|
2020-02-12 01:16:31 +00:00
|
|
|
char status[editor->cols];
|
|
|
|
char rstatus[editor->cols];
|
2020-02-11 01:01:21 +00:00
|
|
|
int len, rlen;
|
|
|
|
|
2020-02-11 06:02:11 +00:00
|
|
|
len = snprintf(status, sizeof(status), "%c%cke: %.20s - %d lines",
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->dirty ? '!' : '-', editor->dirty ? '!' : '-',
|
|
|
|
editor->filename ? editor->filename : "[no file]",
|
|
|
|
editor->nrows);
|
2020-02-11 01:01:21 +00:00
|
|
|
rlen = snprintf(rstatus, sizeof(rstatus), "L%d/%d C%d",
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->cury+1, editor->nrows, editor->curx+1);
|
2020-02-11 01:01:21 +00:00
|
|
|
|
|
|
|
ab_append(ab, ESCSEQ "7m", 4);
|
|
|
|
ab_append(ab, status, len);
|
2020-02-12 01:16:31 +00:00
|
|
|
while (len < editor->cols) {
|
|
|
|
if ((editor->cols - len) == rlen) {
|
2020-02-11 01:01:21 +00:00
|
|
|
ab_append(ab, rstatus, rlen);
|
|
|
|
break;
|
2020-02-09 06:23:32 +00:00
|
|
|
}
|
2020-02-11 01:01:21 +00:00
|
|
|
ab_append(ab, " ", 1);
|
|
|
|
len++;
|
2020-02-09 06:23:32 +00:00
|
|
|
}
|
2020-02-11 01:01:21 +00:00
|
|
|
ab_append(ab, ESCSEQ "m", 3);
|
|
|
|
ab_append(ab, "\r\n", 2);
|
2020-02-08 08:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-11 01:01:21 +00:00
|
|
|
void
|
|
|
|
draw_message_line(struct abuf *ab)
|
|
|
|
{
|
2020-02-12 01:16:31 +00:00
|
|
|
int len = strlen(editor->msg);
|
2020-02-11 01:01:21 +00:00
|
|
|
|
|
|
|
ab_append(ab, ESCSEQ "K", 3);
|
2020-02-12 01:16:31 +00:00
|
|
|
if (len > editor->cols) {
|
|
|
|
len = editor->cols;
|
2020-02-11 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
if (len && ((time(NULL) - editor->msgtm) < MSG_TIMEO)) {
|
|
|
|
ab_append(ab, editor->msg, len);
|
2020-02-11 01:01:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-10 15:43:36 +00:00
|
|
|
|
2020-02-10 06:58:10 +00:00
|
|
|
void
|
|
|
|
scroll()
|
|
|
|
{
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->rx = 0;
|
|
|
|
if (editor->cury < editor->nrows) {
|
|
|
|
editor->rx = erow_render_to_cursor(
|
|
|
|
&editor->row[editor->cury],
|
|
|
|
editor->curx);
|
2020-02-10 15:43:36 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
if (editor->cury < editor->rowoffs) {
|
|
|
|
editor->rowoffs = editor->cury;
|
2020-02-10 06:58:10 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
if (editor->cury >= editor->rowoffs + editor->rows) {
|
|
|
|
editor->rowoffs = editor->cury - editor->rows + 1;
|
2020-02-10 06:58:10 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
if (editor->rx < editor->coloffs) {
|
|
|
|
editor->coloffs = editor->rx;
|
2020-02-10 06:58:10 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
if (editor->rx >= editor->coloffs + editor->cols) {
|
|
|
|
editor->coloffs = editor->rx - editor->cols + 1;
|
2020-02-10 06:58:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-08 08:40:21 +00:00
|
|
|
void
|
|
|
|
display_refresh()
|
|
|
|
{
|
2020-02-08 10:22:31 +00:00
|
|
|
char buf[32];
|
2020-02-08 08:40:21 +00:00
|
|
|
struct abuf ab = ABUF_INIT;
|
|
|
|
|
2020-02-10 06:58:10 +00:00
|
|
|
scroll();
|
|
|
|
|
2020-02-08 08:40:21 +00:00
|
|
|
ab_append(&ab, ESCSEQ "?25l", 6);
|
|
|
|
display_clear(&ab);
|
2020-02-08 10:22:31 +00:00
|
|
|
|
2020-02-08 08:40:21 +00:00
|
|
|
draw_rows(&ab);
|
2020-02-11 01:01:21 +00:00
|
|
|
draw_status_bar(&ab);
|
|
|
|
draw_message_line(&ab);
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-10 06:58:10 +00:00
|
|
|
snprintf(buf, sizeof(buf), ESCSEQ "%d;%dH",
|
2020-02-12 01:16:31 +00:00
|
|
|
(editor->cury-editor->rowoffs)+1,
|
|
|
|
(editor->rx-editor->coloffs)+1);
|
2020-02-08 10:22:31 +00:00
|
|
|
ab_append(&ab, buf, strnlen(buf, 32));
|
2020-02-09 06:23:32 +00:00
|
|
|
/* ab_append(&ab, ESCSEQ "1;2H", 7); */
|
2020-02-08 08:40:21 +00:00
|
|
|
ab_append(&ab, ESCSEQ "?25h", 6);
|
|
|
|
|
|
|
|
write(STDOUT_FILENO, ab.b, ab.len);
|
|
|
|
ab_free(&ab);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-11 01:01:21 +00:00
|
|
|
void
|
|
|
|
editor_set_status(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2020-02-12 01:16:31 +00:00
|
|
|
vsnprintf(editor->msg, sizeof(editor->msg), fmt, ap);
|
2020-02-11 01:01:21 +00:00
|
|
|
va_end(ap);
|
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->msgtm = time(NULL);
|
2020-02-11 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-08 07:07:59 +00:00
|
|
|
void
|
|
|
|
loop()
|
2020-02-08 04:46:43 +00:00
|
|
|
{
|
2020-02-11 01:01:21 +00:00
|
|
|
int up = 1;
|
|
|
|
|
2020-02-08 07:07:59 +00:00
|
|
|
while (1) {
|
2020-02-11 01:01:21 +00:00
|
|
|
if (up) display_refresh();
|
|
|
|
up = process_keypress();
|
2020-02-08 07:07:59 +00:00
|
|
|
}
|
2020-02-08 04:46:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-08 08:40:21 +00:00
|
|
|
/*
|
|
|
|
* init_editor should set up the global editor struct.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
init_editor()
|
|
|
|
{
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->cols = 0;
|
|
|
|
editor->rows = 0;
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
if (get_winsz(&editor->rows, &editor->cols) == -1) {
|
2020-02-08 08:40:21 +00:00
|
|
|
die("can't get window size");
|
|
|
|
}
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->rows--; /* status bar */
|
|
|
|
editor->rows--; /* message line */
|
2020-02-08 10:22:31 +00:00
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->curx = editor->cury = 0;
|
|
|
|
editor->rx = 0;
|
2020-02-09 06:23:32 +00:00
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->nrows = 0;
|
|
|
|
editor->rowoffs = editor->coloffs = 0;
|
|
|
|
editor->row = NULL;
|
2020-02-11 01:01:21 +00:00
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->msg[0] = '\0';
|
|
|
|
editor->msgtm = 0;
|
2020-02-11 17:52:49 +00:00
|
|
|
|
2020-02-12 01:16:31 +00:00
|
|
|
editor->dirty = 0;
|
2020-02-08 08:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-08 04:46:43 +00:00
|
|
|
int
|
2020-02-09 06:23:32 +00:00
|
|
|
main(int argc, char *argv[])
|
2020-02-08 04:46:43 +00:00
|
|
|
{
|
2020-02-08 07:07:59 +00:00
|
|
|
setup_terminal();
|
2020-02-08 08:40:21 +00:00
|
|
|
init_editor();
|
2020-02-08 07:44:33 +00:00
|
|
|
|
2020-02-09 06:23:32 +00:00
|
|
|
if (argc > 1) {
|
|
|
|
open_file(argv[1]);
|
|
|
|
}
|
|
|
|
|
2020-02-11 01:01:21 +00:00
|
|
|
editor_set_status("C-k q to exit / C-k d to dump core");
|
|
|
|
|
2020-02-08 08:40:21 +00:00
|
|
|
display_clear(NULL);
|
2020-02-08 07:07:59 +00:00
|
|
|
loop();
|
2020-02-08 04:46:43 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|