13 Commits

Author SHA1 Message Date
3c79e368fa Forgot to bump version in CMakeLists.
Some checks failed
Release / Bump Homebrew formula (push) Has been cancelled
2025-11-27 12:51:53 -08:00
bd68b809cd various qol improvements
Some checks failed
Release / Bump Homebrew formula (push) Has been cancelled
+ C-u works with C-f and C-b (and their delete counterparts).
+ Opening files now allows adding a line number after to jump to.
2025-11-27 12:48:10 -08:00
f200a7bfcd rename editor type. 2025-11-26 18:39:06 -08:00
58daeac6ad typedef struct abuf -> abuf 2025-11-26 18:38:32 -08:00
3ef6bab265 typedef struct erow -> erow 2025-11-26 18:35:51 -08:00
3800751bcf move var decl to top of function. 2025-11-26 17:57:59 -08:00
87272a669b remove debug log
just redirect stderr to a logfile like a normal person
2025-11-26 17:09:34 -08:00
b77748f7c0 add first post about ke to the readme 2025-11-26 16:47:58 -08:00
c70b502006 display message if KIONREAD ioctl fails. 2025-11-26 16:16:10 -08:00
e14d620887 remove comments, not needed 2025-11-26 16:14:25 -08:00
db38266849 bump version
Some checks failed
Release / Bump Homebrew formula (push) Has been cancelled
2025-11-26 16:09:43 -08:00
6d1b7f8e56 keep default.nix in sync 2025-11-26 16:08:38 -08:00
64647f77b0 performance improvements 2025-11-26 16:00:15 -08:00
4 changed files with 164 additions and 108 deletions

View File

@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.15)
project(ke C) # Specify C language explicitly project(ke C) # Specify C language explicitly
set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD 99)
set(KE_VERSION "1.5.1") set(KE_VERSION "1.5.4")
set(CMAKE_C_FLAGS "-Wall -Wextra -pedantic -Wshadow -Werror -std=c99 -g") set(CMAKE_C_FLAGS "-Wall -Wextra -pedantic -Wshadow -Werror -std=c99 -g")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_DEFAULT_SOURCE -D_XOPEN_SOURCE") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_DEFAULT_SOURCE -D_XOPEN_SOURCE")

View File

@@ -19,3 +19,6 @@ Released under an ISC license.
Started by following along with kilo: Started by following along with kilo:
https://viewsourcecode.org/snaptoken/kilo/ https://viewsourcecode.org/snaptoken/kilo/
E.g., in the devlogs
https://log.wntrmute.dev/2020/02/20200207

View File

@@ -5,9 +5,15 @@
installShellFiles, installShellFiles,
... ...
}: }:
let
cmakeContent = builtins.readFile ./CMakeLists.txt;
cmakeLines = lib.splitString "\n" cmakeContent;
versionLine = lib.findFirst (l: builtins.match ".*set\\(KE_VERSION \".+\"\\).*" l != null) (throw "KE_VERSION not found in CMakeLists.txt") cmakeLines;
version = builtins.head (builtins.match ".*set\\(KE_VERSION \"(.+)\"\\).*" versionLine);
in
stdenv.mkDerivation { stdenv.mkDerivation {
pname = "ke"; pname = "ke";
version = "1.5.1"; inherit version;
src = lib.cleanSource ./.; src = lib.cleanSource ./.;

259
main.c
View File

@@ -73,21 +73,19 @@
#define calloc1(sz) calloc(1, sz) #define calloc1(sz) calloc(1, sz)
static FILE* debug_log = NULL;
/* append buffer */ /* append buffer */
struct abuf { typedef struct abuf {
char *b; char *b;
size_t len; size_t len;
size_t cap; size_t cap;
}; } abuf;
#define ABUF_INIT {NULL, 0, 0} #define ABUF_INIT {NULL, 0, 0}
/* editor row */ /* editor row */
struct erow { typedef struct erow {
char *line; char *line;
char *render; char *render;
@@ -96,14 +94,14 @@ struct erow {
int cap; int cap;
int dirty; int dirty;
}; } erow;
/* /*
* editor is the global editor state; it should be broken out * editor is the global editor state; it should be broken out
* to buffers and screen state, probably. * to buffers and screen state, probably.
*/ */
struct editor_t { struct editor {
struct termios entry_term; struct termios entry_term;
int rows, cols; int rows, cols;
int curx, cury; int curx, cury;
@@ -111,8 +109,8 @@ struct editor_t {
int mode; int mode;
int nrows; int nrows;
int rowoffs, coloffs; int rowoffs, coloffs;
struct erow *row; erow *row;
struct erow *killring; erow *killring;
int kill; /* KILL CHAIN (sounds metal) */ int kill; /* KILL CHAIN (sounds metal) */
int no_kill; /* don't kill in delete_row */ int no_kill; /* don't kill in delete_row */
char *filename; char *filename;
@@ -154,22 +152,22 @@ void reset_editor(void);
int next_power_of_2(int n); int next_power_of_2(int n);
int cap_growth(int cap, int sz); int cap_growth(int cap, int sz);
size_t kstrnlen(const char *buf, const size_t max); size_t kstrnlen(const char *buf, const size_t max);
void ab_init(struct abuf *buf); void ab_init(abuf *buf);
void ab_appendch(struct abuf *buf, char c); void ab_appendch(abuf *buf, char c);
void ab_append(struct abuf *buf, const char *s, size_t len); void ab_append(abuf *buf, const char *s, size_t len);
void ab_prependch(struct abuf *buf, char c); void ab_prependch(abuf *buf, char c);
void ab_prepend(struct abuf *buf, const char *s, size_t len); void ab_prepend(abuf *buf, const char *s, size_t len);
void ab_free(struct abuf *buf); void ab_free(abuf *buf);
char nibble_to_hex(char c); char nibble_to_hex(char c);
void swap_int(int *a, int *b); void swap_int(int *a, int *b);
/* editor rows */ /* editor rows */
int erow_render_to_cursor(struct erow *row, int cx); int erow_render_to_cursor(erow *row, int cx);
int erow_cursor_to_render(struct erow *row, int rx); int erow_cursor_to_render(erow *row, int rx);
int erow_init(struct erow *row, int len); int erow_init(erow *row, int len);
void erow_update(struct erow *row); void erow_update(erow *row);
void erow_insert(int at, char *s, int len); void erow_insert(int at, char *s, int len);
void erow_free(struct erow *row); void erow_free(erow *row);
/* kill ring, marking, etc... */ /* kill ring, marking, etc... */
@@ -198,8 +196,8 @@ void delete_next_word(void);
void find_prev_word(void); void find_prev_word(void);
void delete_prev_word(void); void delete_prev_word(void);
void delete_row(int at); void delete_row(int at);
void row_insert_ch(struct erow *row, int at, int16_t c); void row_insert_ch(erow *row, int at, int16_t c);
void row_delete_ch(struct erow *row, int at); void row_delete_ch(erow *row, int at);
void insertch(int16_t c); void insertch(int16_t c);
void deletech(uint8_t op); void deletech(uint8_t op);
void open_file(const char *filename); void open_file(const char *filename);
@@ -212,6 +210,7 @@ void editor_find_callback(char *query, int16_t c);
void editor_find(void); void editor_find(void);
char *editor_prompt(char*, void (*cb)(char*, int16_t)); char *editor_prompt(char*, void (*cb)(char*, int16_t));
void editor_openfile(void); void editor_openfile(void);
int first_nonwhitespace(erow *row);
void move_cursor_once(int16_t c, int interactive); void move_cursor_once(int16_t c, int interactive);
void move_cursor(int16_t c, int interactive); void move_cursor(int16_t c, int interactive);
void uarg_start(void); void uarg_start(void);
@@ -226,18 +225,18 @@ int process_keypress(void);
char *get_cloc_code_lines(const char *filename); char *get_cloc_code_lines(const char *filename);
int dump_pidfile(void); int dump_pidfile(void);
void enable_termraw(void); void enable_termraw(void);
void display_clear(struct abuf *ab); void display_clear(abuf *ab);
void disable_termraw(void); void disable_termraw(void);
void setup_terminal(void); void setup_terminal(void);
void draw_rows(struct abuf *ab); void draw_rows(abuf *ab);
char status_mode_char(void); char status_mode_char(void);
void draw_status_bar(struct abuf *ab); void draw_status_bar(abuf *ab);
void draw_message_line(struct abuf *ab); void draw_message_line(abuf *ab);
void scroll(void); void scroll(void);
void display_refresh(void); void display_refresh(void);
void editor_set_status(const char *fmt, ...); void editor_set_status(const char *fmt, ...);
void loop(void); void loop(void);
void enable_debugging(const char *logfile); void enable_debugging(void);
void deathknell(void); void deathknell(void);
static void signal_handler(int sig); static void signal_handler(int sig);
static void install_signal_handlers(void); static void install_signal_handlers(void);
@@ -392,7 +391,7 @@ reset_editor(void)
void void
ab_init(struct abuf *buf) ab_init(abuf *buf)
{ {
buf->b = NULL; buf->b = NULL;
buf->len = 0; buf->len = 0;
@@ -401,14 +400,14 @@ ab_init(struct abuf *buf)
void void
ab_appendch(struct abuf *buf, char c) ab_appendch(abuf *buf, char c)
{ {
ab_append(buf, &c, 1); ab_append(buf, &c, 1);
} }
void void
ab_append(struct abuf *buf, const char *s, size_t len) ab_append(abuf *buf, const char *s, size_t len)
{ {
char *nc = buf->b; char *nc = buf->b;
size_t sz = buf->len + len; size_t sz = buf->len + len;
@@ -432,14 +431,14 @@ ab_append(struct abuf *buf, const char *s, size_t len)
void void
ab_prependch(struct abuf *buf, const char c) ab_prependch(abuf *buf, const char c)
{ {
ab_prepend(buf, &c, 1); ab_prepend(buf, &c, 1);
} }
void void
ab_prepend(struct abuf *buf, const char *s, const size_t len) ab_prepend(abuf *buf, const char *s, const size_t len)
{ {
char *nc = realloc(buf->b, buf->len + len); char *nc = realloc(buf->b, buf->len + len);
assert(nc != NULL); assert(nc != NULL);
@@ -453,7 +452,7 @@ ab_prepend(struct abuf *buf, const char *s, const size_t len)
void void
ab_free(struct abuf *buf) ab_free(abuf *buf)
{ {
free(buf->b); free(buf->b);
buf->b = NULL; buf->b = NULL;
@@ -483,7 +482,7 @@ swap_int(int *a, int *b)
int int
erow_render_to_cursor(struct erow *row, int cx) erow_render_to_cursor(erow *row, int cx)
{ {
int rx = 0; int rx = 0;
size_t j = 0; size_t j = 0;
@@ -507,6 +506,12 @@ erow_render_to_cursor(struct erow *row, int cx)
continue; continue;
} }
if (b < 0x80) {
rx++;
j++;
continue;
}
size_t rem = (size_t)row->size - j; size_t rem = (size_t)row->size - j;
size_t n = mbrtowc(&wc, &row->line[j], rem, &st); size_t n = mbrtowc(&wc, &row->line[j], rem, &st);
@@ -538,7 +543,7 @@ erow_render_to_cursor(struct erow *row, int cx)
int int
erow_cursor_to_render(struct erow *row, int rx) erow_cursor_to_render(erow *row, int rx)
{ {
int cur_rx = 0; int cur_rx = 0;
size_t j = 0; size_t j = 0;
@@ -560,6 +565,9 @@ erow_cursor_to_render(struct erow *row, int rx)
} else if (b < 0x20) { } else if (b < 0x20) {
w = 3; /* "\\xx" */ w = 3; /* "\\xx" */
adv = 1; adv = 1;
} else if (b < 0x80) {
w = 1;
adv = 1;
} else { } else {
size_t rem = (size_t)row->size - j; size_t rem = (size_t)row->size - j;
size_t n = mbrtowc(&wc, &row->line[j], rem, &st); size_t n = mbrtowc(&wc, &row->line[j], rem, &st);
@@ -594,7 +602,7 @@ erow_cursor_to_render(struct erow *row, int rx)
int int
erow_init(struct erow *row, int len) erow_init(erow *row, int len)
{ {
row->size = len; row->size = len;
row->rsize = 0; row->rsize = 0;
@@ -615,7 +623,7 @@ erow_init(struct erow *row, int len)
void void
erow_update(struct erow *row) erow_update(erow *row)
{ {
int i = 0, j; int i = 0, j;
int tabs = 0; int tabs = 0;
@@ -665,7 +673,7 @@ erow_update(struct erow *row)
void void
erow_insert(int at, char *s, int len) erow_insert(int at, char *s, int len)
{ {
struct erow row; erow row;
if (at < 0 || at > editor.nrows) { if (at < 0 || at > editor.nrows) {
return; return;
@@ -676,13 +684,13 @@ erow_insert(int at, char *s, int len)
row.line[len] = 0; row.line[len] = 0;
editor.row = realloc(editor.row, editor.row = realloc(editor.row,
sizeof(struct erow) * (editor.nrows + 1)); sizeof(erow) * (editor.nrows + 1));
assert(editor.row != NULL); assert(editor.row != NULL);
if (at < editor.nrows) { if (at < editor.nrows) {
memmove(&editor.row[at + 1], memmove(&editor.row[at + 1],
&editor.row[at], &editor.row[at],
sizeof(struct erow) * (editor.nrows - at)); sizeof(erow) * (editor.nrows - at));
} }
editor.row[at] = row; editor.row[at] = row;
@@ -692,7 +700,7 @@ erow_insert(int at, char *s, int len)
void void
erow_free(struct erow *row) erow_free(erow *row)
{ {
free(row->render); free(row->render);
free(row->line); free(row->line);
@@ -737,7 +745,7 @@ killring_yank(void)
void void
killring_start_with_char(unsigned char ch) killring_start_with_char(unsigned char ch)
{ {
struct erow *row = NULL; erow *row = NULL;
if (editor.killring != NULL) { if (editor.killring != NULL) {
erow_free(editor.killring); erow_free(editor.killring);
@@ -745,7 +753,7 @@ killring_start_with_char(unsigned char ch)
editor.killring = NULL; editor.killring = NULL;
} }
editor.killring = malloc(sizeof(struct erow)); editor.killring = malloc(sizeof(erow));
assert(editor.killring != NULL); assert(editor.killring != NULL);
assert(erow_init(editor.killring, 0) == 0); assert(erow_init(editor.killring, 0) == 0);
@@ -764,7 +772,7 @@ killring_start_with_char(unsigned char ch)
void void
killring_append_char(unsigned char ch) killring_append_char(unsigned char ch)
{ {
struct erow *row = NULL; erow *row = NULL;
if (editor.killring == NULL) { if (editor.killring == NULL) {
killring_start_with_char(ch); killring_start_with_char(ch);
@@ -789,7 +797,7 @@ killring_prepend_char(unsigned char ch)
return; return;
} }
struct erow *row = editor.killring; erow *row = editor.killring;
row->line = realloc(row->line, row->size + 2); row->line = realloc(row->line, row->size + 2);
assert(row->line != NULL); assert(row->line != NULL);
memmove(&row->line[1], &row->line[0], row->size + 1); memmove(&row->line[1], &row->line[0], row->size + 1);
@@ -957,8 +965,8 @@ indent_region(void)
void void
unindent_region(void) unindent_region(void)
{ {
int start_row, end_row, i, del; int start_row, end_row, i, del;
struct erow *row; erow *row;
if (!editor.mark_set) { if (!editor.mark_set) {
editor_set_status("Mark not set."); editor_set_status("Mark not set.");
@@ -1122,7 +1130,6 @@ jump_to_position(int col, int row)
editor.curx = col; editor.curx = col;
editor.cury = row; editor.cury = row;
scroll();
display_refresh(); display_refresh();
} }
@@ -1283,6 +1290,8 @@ delete_prev_word(void)
void void
delete_row(int at) delete_row(int at)
{ {
erow *row = NULL;
if (at < 0 || at >= editor.nrows) { if (at < 0 || at >= editor.nrows) {
return; return;
} }
@@ -1294,21 +1303,21 @@ delete_row(int at)
* newline itself and we must NOT also push the entire row here. * newline itself and we must NOT also push the entire row here.
*/ */
if (!editor.no_kill) { if (!editor.no_kill) {
struct erow *r = &editor.row[at]; row = &editor.row[at];
/* Start or continue the kill sequence based on editor.killing */ /* Start or continue the kill sequence based on editor.killing */
if (r->size > 0) { if (row->size > 0) {
/* push raw bytes of the line */ /* push raw bytes of the line */
if (!editor.kill) { if (!editor.kill) {
killring_start_with_char( killring_start_with_char(
(unsigned char)r->line[0]); (unsigned char)row->line[0]);
for (int i = 1; i < r->size; i++) { for (int i = 1; i < row->size; i++) {
killring_append_char( killring_append_char(
(unsigned char)r->line[i]); (unsigned char)row->line[i]);
} }
} else { } else {
for (int i = 0; i < r->size; i++) { for (int i = 0; i < row->size; i++) {
killring_append_char( killring_append_char(
(unsigned char)r->line[i]); (unsigned char)row->line[i]);
} }
} }
killring_append_char('\n'); killring_append_char('\n');
@@ -1326,14 +1335,14 @@ delete_row(int at)
erow_free(&editor.row[at]); erow_free(&editor.row[at]);
memmove(&editor.row[at], memmove(&editor.row[at],
&editor.row[at + 1], &editor.row[at + 1],
sizeof(struct erow) * (editor.nrows - at - 1)); sizeof(erow) * (editor.nrows - at - 1));
editor.nrows--; editor.nrows--;
editor.dirty++; editor.dirty++;
} }
void void
row_append_row(struct erow *row, char *s, int len) row_append_row(erow *row, char *s, int len)
{ {
row->line = realloc(row->line, row->size + len + 1); row->line = realloc(row->line, row->size + len + 1);
assert(row->line != NULL); assert(row->line != NULL);
@@ -1346,7 +1355,7 @@ row_append_row(struct erow *row, char *s, int len)
void void
row_insert_ch(struct erow *row, int at, int16_t c) row_insert_ch(erow *row, int at, int16_t c)
{ {
/* /*
* row_insert_ch just concerns itself with how to update a row. * row_insert_ch just concerns itself with how to update a row.
@@ -1367,7 +1376,7 @@ row_insert_ch(struct erow *row, int at, int16_t c)
void void
row_delete_ch(struct erow *row, int at) row_delete_ch(erow *row, int at)
{ {
if (at < 0 || at >= row->size) { if (at < 0 || at >= row->size) {
return; return;
@@ -1407,8 +1416,9 @@ insertch(int16_t c)
void void
deletech(uint8_t op) deletech(uint8_t op)
{ {
struct erow *row = NULL; erow *row = NULL;
unsigned char dch = 0; unsigned char dch = 0;
int prev = 0;
if (editor.cury >= editor.nrows) { if (editor.cury >= editor.nrows) {
return; return;
@@ -1433,7 +1443,8 @@ deletech(uint8_t op)
row_append_row(&editor.row[editor.cury - 1], row_append_row(&editor.row[editor.cury - 1],
row->line, row->line,
row->size); row->size);
int prev = editor.no_kill;
prev = editor.no_kill;
editor.no_kill = 1; editor.no_kill = 1;
delete_row(editor.cury); delete_row(editor.cury);
@@ -1781,7 +1792,7 @@ editor_find_callback(char* query, int16_t c)
static int last_match = -1; /* row index of last match */ static int last_match = -1; /* row index of last match */
static int direction = 1; /* 1 = forward, -1 = backward */ static int direction = 1; /* 1 = forward, -1 = backward */
static char last_query[128] = {0}; /* remember last successful query */ static char last_query[128] = {0}; /* remember last successful query */
struct erow *row; erow *row;
int saved_cx = editor.curx; int saved_cx = editor.curx;
int saved_cy = editor.cury; int saved_cy = editor.cury;
size_t qlen = strlen(query); size_t qlen = strlen(query);
@@ -1910,7 +1921,7 @@ editor_openfile(void)
int int
first_nonwhitespace(struct erow *row) first_nonwhitespace(erow *row)
{ {
int pos; int pos;
wchar_t wc; wchar_t wc;
@@ -1928,6 +1939,14 @@ first_nonwhitespace(struct erow *row)
} }
while (pos < row->size) { while (pos < row->size) {
if ((unsigned char)row->line[pos] < 0x80) {
if (!isspace((unsigned char)row->line[pos])) {
return pos;
}
pos++;
continue;
}
len = mbrtowc(&wc, &row->line[pos], row->size - pos, &state); len = mbrtowc(&wc, &row->line[pos], row->size - pos, &state);
if (len == (size_t)-1 || len == (size_t)-2) { if (len == (size_t)-1 || len == (size_t)-2) {
break; break;
@@ -1951,8 +1970,8 @@ first_nonwhitespace(struct erow *row)
void void
move_cursor_once(int16_t c, int interactive) move_cursor_once(int16_t c, int interactive)
{ {
struct erow *row; erow *row;
int reps = 0; int reps = 0;
row = (editor.cury >= editor.nrows) ? NULL : &editor.row[editor.cury]; row = (editor.cury >= editor.nrows) ? NULL : &editor.row[editor.cury];
@@ -2071,7 +2090,7 @@ move_cursor(int16_t c, int interactive)
void void
newline(void) newline(void)
{ {
struct erow *row = NULL; erow *row = NULL;
if (editor.cury >= editor.nrows) { if (editor.cury >= editor.nrows) {
erow_insert(editor.cury, "", 0); erow_insert(editor.cury, "", 0);
@@ -2303,8 +2322,10 @@ process_kcommand(int16_t c)
reset_editor(); reset_editor();
open_file(buf); open_file(buf);
display_refresh(); display_refresh();
free(buf);
jump_to_position(jumpx, jumpy); jump_to_position(jumpx, jumpy);
editor_set_status("file reloaded");
break; break;
case CTRL_KEY('s'): case CTRL_KEY('s'):
case 's': case 's':
@@ -2449,6 +2470,8 @@ process_normal(int16_t c)
void void
process_escape(int16_t c) process_escape(int16_t c)
{ {
int reps = 0;
editor_set_status("hi"); editor_set_status("hi");
switch (c) { switch (c) {
@@ -2461,13 +2484,25 @@ process_escape(int16_t c)
editor.curx = 0; editor.curx = 0;
break; break;
case 'b': case 'b':
find_prev_word(); reps = uarg_get();
while (reps--) {
find_prev_word();
}
break; break;
case 'd': case 'd':
delete_next_word(); reps = uarg_get();
while (reps--) {
delete_next_word();
}
break; break;
case 'f': case 'f':
find_next_word(); reps = uarg_get();
while (reps--) {
find_next_word();
}
break; break;
case 'm': case 'm':
toggle_markset(); toggle_markset();
@@ -2481,7 +2516,11 @@ process_escape(int16_t c)
toggle_markset(); toggle_markset();
break; break;
case BACKSPACE: case BACKSPACE:
delete_prev_word(); reps = uarg_get();
while (--reps) {
delete_prev_word();
}
break; break;
case ESC_KEY: case ESC_KEY:
case CTRL_KEY('g'): case CTRL_KEY('g'):
@@ -2489,6 +2528,8 @@ process_escape(int16_t c)
default: default:
editor_set_status("unknown ESC key: %04x", c); editor_set_status("unknown ESC key: %04x", c);
} }
uarg_clear();
} }
@@ -2648,7 +2689,7 @@ enable_termraw(void)
void void
display_clear(struct abuf *ab) display_clear(abuf *ab)
{ {
if (ab == NULL) { if (ab == NULL) {
kwrite(STDOUT_FILENO, ESCSEQ "2J", 4); kwrite(STDOUT_FILENO, ESCSEQ "2J", 4);
@@ -2683,14 +2724,14 @@ setup_terminal(void)
void void
draw_rows(struct abuf *ab) draw_rows(abuf *ab)
{ {
assert(editor.cols >= 0); assert(editor.cols >= 0);
struct erow *row; erow *row;
char buf[editor.cols]; char buf[editor.cols];
int len, filerow, padding; int len, filerow, padding;
int y; int y;
for (y = 0; y < editor.rows; y++) { for (y = 0; y < editor.rows; y++) {
filerow = y + editor.rowoffs; filerow = y + editor.rowoffs;
@@ -2755,7 +2796,7 @@ status_mode_char(void)
void void
draw_status_bar(struct abuf *ab) draw_status_bar(abuf *ab)
{ {
char status[editor.cols]; char status[editor.cols];
char rstatus[editor.cols]; char rstatus[editor.cols];
@@ -2805,7 +2846,7 @@ draw_status_bar(struct abuf *ab)
void void
draw_message_line(struct abuf *ab) draw_message_line(abuf *ab)
{ {
int len = strlen(editor.msg); int len = strlen(editor.msg);
@@ -2823,7 +2864,8 @@ draw_message_line(struct abuf *ab)
void void
scroll(void) scroll(void)
{ {
struct erow *row = NULL; erow *row = NULL;
editor.rx = 0; editor.rx = 0;
if (editor.cury < editor.nrows) { if (editor.cury < editor.nrows) {
row = &editor.row[editor.cury]; row = &editor.row[editor.cury];
@@ -2855,8 +2897,8 @@ scroll(void)
void void
display_refresh(void) display_refresh(void)
{ {
char buf[32]; char buf[32];
struct abuf ab = ABUF_INIT; abuf ab = ABUF_INIT;
scroll(); scroll();
@@ -2875,7 +2917,7 @@ display_refresh(void)
(editor.rx - editor.coloffs) + 1); (editor.rx - editor.coloffs) + 1);
ab_append(&ab, buf, kstrnlen(buf, 32)); ab_append(&ab, buf, kstrnlen(buf, 32));
/* ab_append(&ab, ESCSEQ "1;2H", 7); */ /* ab_append(&ab, ESCSEQ "1;2H", 7); */
ab_append(&ab, ESCSEQ "?25l", 6); ab_append(&ab, ESCSEQ "?25h", 6);
kwrite(STDOUT_FILENO, ab.b, ab.len); kwrite(STDOUT_FILENO, ab.b, ab.len);
ab_free(&ab); ab_free(&ab);
@@ -2900,6 +2942,15 @@ kbhit(void)
{ {
int bytes_waiting; int bytes_waiting;
ioctl(STDIN_FILENO, FIONREAD, &bytes_waiting); ioctl(STDIN_FILENO, FIONREAD, &bytes_waiting);
if (bytes_waiting < 0) {
editor_set_status("kbhit: FIONREAD failed: %s", strerror(errno));
/* if FIONREAD fails, we need to assume we should read. this
* will default to a much slower input sequence, but it'll work.
*/
return 1;
}
return bytes_waiting > 0; return bytes_waiting > 0;
} }
@@ -2929,21 +2980,11 @@ loop(void)
void void
enable_debugging(const char *logfile) enable_debugging(void)
{ {
time_t now; time_t now;
if (debug_log != NULL) {
fclose(debug_log);
}
if ((debug_log = fopen(logfile, "w")) == NULL) {
fprintf(stderr, "Failed to open error log!\n");
fprintf(stderr, "\t%s\n", strerror(errno));
}
dump_pidfile(); dump_pidfile();
stderr = debug_log;
now = time(&now); now = time(&now);
printf("time: %s\n", ctime(&now)); printf("time: %s\n", ctime(&now));
@@ -2955,12 +2996,7 @@ enable_debugging(const char *logfile)
void void
deathknell(void) deathknell(void)
{ {
if (debug_log != NULL) { fflush(stderr);
fflush(stderr);
fclose(debug_log);
debug_log = NULL;
}
if (editor.killring != NULL) { if (editor.killring != NULL) {
erow_free(editor.killring); erow_free(editor.killring);
@@ -3017,9 +3053,10 @@ install_signal_handlers(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char *logfile = "debug-ke.log"; char *arg;
int opt; int lineno = 0;
int debug = 0; int opt;
int debug = 0;
install_signal_handlers(); install_signal_handlers();
@@ -3028,9 +3065,6 @@ main(int argc, char *argv[])
case 'd': case 'd':
debug = 1; debug = 1;
break; break;
case 'f':
logfile = optarg;
break;
default: default:
fprintf(stderr, "Usage: ke [-d] [-f logfile] [path]\n"); fprintf(stderr, "Usage: ke [-d] [-f logfile] [path]\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@@ -3042,7 +3076,7 @@ main(int argc, char *argv[])
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
if (debug) { if (debug) {
enable_debugging(logfile); enable_debugging();
} }
setup_terminal(); setup_terminal();
@@ -3052,7 +3086,20 @@ main(int argc, char *argv[])
open_file(argv[0]); open_file(argv[0]);
} }
if (argc > 1) {
arg = argv[1];
if (arg[0] == '+') {
arg++;
}
lineno = atoi(arg);
}
editor_set_status("C-k q to exit / C-k d to dump core"); editor_set_status("C-k q to exit / C-k d to dump core");
if (lineno < 1) {
editor_set_status("Invalid line number %s", arg);
} else {
jump_to_position(0, lineno - 1);
}
display_clear(NULL); display_clear(NULL);
loop(); loop();