Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a32ef2ff53 | |||
| 0c0c3d9ce5 | |||
| 7245003769 | |||
| 542b1d90a0 | |||
| a359f4e6c4 |
@@ -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.3.2")
|
set(KE_VERSION "1.3.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")
|
||||||
|
|||||||
1
Makefile
1
Makefile
@@ -3,6 +3,7 @@ KE_VERSION := devel
|
|||||||
DEST := $(HOME)/.local/bin/$(TARGET)
|
DEST := $(HOME)/.local/bin/$(TARGET)
|
||||||
|
|
||||||
CFLAGS := -Wall -Wextra -pedantic -Wshadow -Werror -std=c99 -g
|
CFLAGS := -Wall -Wextra -pedantic -Wshadow -Werror -std=c99 -g
|
||||||
|
CFLAGS += -Wno-unused-result
|
||||||
CFLAGS += -D_DEFAULT_SOURCE -D_XOPEN_SOURCE
|
CFLAGS += -D_DEFAULT_SOURCE -D_XOPEN_SOURCE
|
||||||
CFLAGS += -fsanitize=address -fno-omit-frame-pointer
|
CFLAGS += -fsanitize=address -fno-omit-frame-pointer
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
}:
|
}:
|
||||||
stdenv.mkDerivation {
|
stdenv.mkDerivation {
|
||||||
pname = "ke";
|
pname = "ke";
|
||||||
version = "1.3.2";
|
version = "1.3.4";
|
||||||
|
|
||||||
src = lib.cleanSource ./.;
|
src = lib.cleanSource ./.;
|
||||||
|
|
||||||
|
|||||||
222
main.c
222
main.c
@@ -21,6 +21,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
#include <wchar.h>
|
#include <wchar.h>
|
||||||
|
#include <wctype.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@@ -828,8 +829,8 @@ delete_region(void)
|
|||||||
void
|
void
|
||||||
die(const char *s)
|
die(const char *s)
|
||||||
{
|
{
|
||||||
write(STDOUT_FILENO, "\x1b[2J", 4);
|
(void)write(STDOUT_FILENO, "\x1b[2J", 4);
|
||||||
write(STDOUT_FILENO, "\x1b[H", 3);
|
(void)write(STDOUT_FILENO, "\x1b[H", 3);
|
||||||
|
|
||||||
perror(s);
|
perror(s);
|
||||||
exit(1);
|
exit(1);
|
||||||
@@ -874,11 +875,13 @@ goto_line(void)
|
|||||||
if (lineno < 1 || lineno >= editor.nrows) {
|
if (lineno < 1 || lineno >= editor.nrows) {
|
||||||
editor_set_status("Line number must be between 1 and %d.",
|
editor_set_status("Line number must be between 1 and %d.",
|
||||||
editor.nrows);
|
editor.nrows);
|
||||||
|
free(query);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
editor.cury = lineno - 1;
|
editor.cury = lineno - 1;
|
||||||
editor.rowoffs = editor.cury - (editor.rows / 2);
|
editor.rowoffs = editor.cury - (editor.rows / 2);
|
||||||
|
free(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1439,17 +1442,17 @@ get_keypress(void)
|
|||||||
char *
|
char *
|
||||||
editor_prompt(char *prompt, void (*cb)(char *, int16_t))
|
editor_prompt(char *prompt, void (*cb)(char *, int16_t))
|
||||||
{
|
{
|
||||||
size_t bufsz = 128;
|
size_t bufsz = 128;
|
||||||
char *buf = malloc(bufsz);
|
char *buf = malloc(bufsz);
|
||||||
size_t buflen = 0;
|
size_t buflen = 0;
|
||||||
int16_t c;
|
int16_t c;
|
||||||
|
|
||||||
buf[0] = '\0';
|
buf[0] = '\0';
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
editor_set_status(prompt, buf);
|
editor_set_status(prompt, buf);
|
||||||
display_refresh();
|
display_refresh();
|
||||||
while ((c = get_keypress()) <= 0) ;
|
while ((c = get_keypress()) <= 0);
|
||||||
if (c == DEL_KEY || c == CTRL_KEY('h') || c == BACKSPACE) {
|
if (c == DEL_KEY || c == CTRL_KEY('h') || c == BACKSPACE) {
|
||||||
if (buflen != 0) {
|
if (buflen != 0) {
|
||||||
buf[--buflen] = '\0';
|
buf[--buflen] = '\0';
|
||||||
@@ -1469,7 +1472,7 @@ editor_prompt(char *prompt, void (*cb)(char *, int16_t))
|
|||||||
}
|
}
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
} else if ((c == TAB_KEY) || (c >= 0x20 && c != 0x7f)) {
|
} else if ((c == TAB_KEY) || (c >= 0x20 && c < 0x7f)) {
|
||||||
if (buflen == bufsz - 1) {
|
if (buflen == bufsz - 1) {
|
||||||
bufsz *= 2;
|
bufsz *= 2;
|
||||||
buf = realloc(buf, bufsz);
|
buf = realloc(buf, bufsz);
|
||||||
@@ -1587,6 +1590,41 @@ editor_openfile(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
first_nonwhitespace(struct erow *row)
|
||||||
|
{
|
||||||
|
int pos;
|
||||||
|
wchar_t wc;
|
||||||
|
mbstate_t state;
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
if (row == NULL) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(&state, 0, sizeof(state));
|
||||||
|
pos = 0;
|
||||||
|
while (pos < row->size) {
|
||||||
|
len = mbrtowc(&wc, &row->line[pos], row->size - pos, &state);
|
||||||
|
if (len == (size_t) -1 || len == (size_t) -2) {
|
||||||
|
/* Invalid or incomplete sequence, stop here */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (len == 0) {
|
||||||
|
/* Null character, stop here */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!iswspace(wc)) {
|
||||||
|
/* Found non-whitespace character */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
pos += len;
|
||||||
|
}
|
||||||
|
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
move_cursor(int16_t c)
|
move_cursor(int16_t c)
|
||||||
{
|
{
|
||||||
@@ -1596,86 +1634,97 @@ move_cursor(int16_t c)
|
|||||||
row = (editor.cury >= editor.nrows) ? NULL : &editor.row[editor.cury];
|
row = (editor.cury >= editor.nrows) ? NULL : &editor.row[editor.cury];
|
||||||
|
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case ARROW_UP:
|
case ARROW_UP:
|
||||||
case CTRL_KEY('p'):
|
case CTRL_KEY('p'):
|
||||||
if (editor.cury > 0) {
|
if (editor.cury > 0) {
|
||||||
editor.cury--;
|
editor.cury--;
|
||||||
}
|
row = (editor.cury >= editor.nrows)
|
||||||
break;
|
? NULL
|
||||||
case ARROW_DOWN:
|
: &editor.row[editor.cury];
|
||||||
case CTRL_KEY('n'):
|
editor.curx = first_nonwhitespace(row);
|
||||||
if (editor.cury < editor.nrows) {
|
}
|
||||||
editor.cury++;
|
break;
|
||||||
}
|
case ARROW_DOWN:
|
||||||
break;
|
case CTRL_KEY('n'):
|
||||||
case ARROW_RIGHT:
|
if (editor.cury < editor.nrows) {
|
||||||
case CTRL_KEY('f'):
|
editor.cury++;
|
||||||
if (row && editor.curx < row->size) {
|
row = (editor.cury >= editor.nrows)
|
||||||
|
? NULL
|
||||||
|
: &editor.row[editor.cury];
|
||||||
|
editor.curx = first_nonwhitespace(row);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ARROW_RIGHT:
|
||||||
|
case CTRL_KEY('f'):
|
||||||
|
if (row && editor.curx < row->size) {
|
||||||
|
editor.curx++;
|
||||||
|
/* skip over UTF-8 continuation bytes */
|
||||||
|
while (row && editor.curx < row->size &&
|
||||||
|
((unsigned char) row->line[editor.curx] &
|
||||||
|
0xC0) == 0x80) {
|
||||||
editor.curx++;
|
editor.curx++;
|
||||||
/* skip over UTF-8 continuation bytes */
|
|
||||||
while (row && editor.curx < row->size &&
|
|
||||||
((unsigned char) row->line[editor.curx] &
|
|
||||||
0xC0) == 0x80) {
|
|
||||||
editor.curx++;
|
|
||||||
}
|
|
||||||
} else if (row && editor.curx == row->size) {
|
|
||||||
editor.cury++;
|
|
||||||
editor.curx = 0;
|
|
||||||
}
|
}
|
||||||
break;
|
} else if (row && editor.curx == row->size) {
|
||||||
case ARROW_LEFT:
|
editor.cury++;
|
||||||
case CTRL_KEY('b'):
|
row = (editor.cury >= editor.nrows)
|
||||||
if (editor.curx > 0) {
|
? NULL
|
||||||
|
: &editor.row[editor.cury];
|
||||||
|
editor.curx = first_nonwhitespace(row);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ARROW_LEFT:
|
||||||
|
case CTRL_KEY('b'):
|
||||||
|
if (editor.curx > 0) {
|
||||||
|
editor.curx--;
|
||||||
|
/* move to the start byte if we landed on a continuation */
|
||||||
|
while (editor.curx > 0 &&
|
||||||
|
((unsigned char) row->line[editor.curx] &
|
||||||
|
0xC0) == 0x80) {
|
||||||
editor.curx--;
|
editor.curx--;
|
||||||
/* move to the start byte if we landed on a continuation */
|
|
||||||
while (editor.curx > 0 &&
|
|
||||||
((unsigned char) row->line[editor.curx] &
|
|
||||||
0xC0) == 0x80) {
|
|
||||||
editor.curx--;
|
|
||||||
}
|
|
||||||
} else if (editor.cury > 0) {
|
|
||||||
editor.cury--;
|
|
||||||
editor.curx = editor.row[editor.cury].size;
|
|
||||||
/* ensure at a codepoint boundary at end of previous line */
|
|
||||||
row = &editor.row[editor.cury];
|
|
||||||
while (editor.curx > 0 &&
|
|
||||||
((unsigned char) row->line[editor.curx] &
|
|
||||||
0xC0) == 0x80) {
|
|
||||||
editor.curx--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case PG_UP:
|
|
||||||
case PG_DN:
|
|
||||||
if (c == PG_UP) {
|
|
||||||
editor.cury = editor.rowoffs;
|
|
||||||
} else if (c == PG_DN) {
|
|
||||||
editor.cury = editor.rowoffs + editor.rows - 1;
|
|
||||||
if (editor.cury > editor.nrows) {
|
|
||||||
editor.cury = editor.nrows;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
reps = editor.rows;
|
|
||||||
while (--reps) {
|
|
||||||
move_cursor(c == PG_UP ? ARROW_UP : ARROW_DOWN);
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case HOME_KEY:
|
|
||||||
case CTRL_KEY('a'):
|
|
||||||
editor.curx = 0;
|
|
||||||
break;
|
|
||||||
case END_KEY:
|
|
||||||
case CTRL_KEY('e'):
|
|
||||||
if (editor.nrows == 0) {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
} else if (editor.cury > 0) {
|
||||||
|
editor.cury--;
|
||||||
editor.curx = editor.row[editor.cury].size;
|
editor.curx = editor.row[editor.cury].size;
|
||||||
|
/* ensure at a codepoint boundary at end of previous line */
|
||||||
|
row = &editor.row[editor.cury];
|
||||||
|
while (editor.curx > 0 &&
|
||||||
|
((unsigned char) row->line[editor.curx] &
|
||||||
|
0xC0) == 0x80) {
|
||||||
|
editor.curx--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case PG_UP:
|
||||||
|
case PG_DN:
|
||||||
|
if (c == PG_UP) {
|
||||||
|
editor.cury = editor.rowoffs;
|
||||||
|
} else if (c == PG_DN) {
|
||||||
|
editor.cury = editor.rowoffs + editor.rows - 1;
|
||||||
|
if (editor.cury > editor.nrows) {
|
||||||
|
editor.cury = editor.nrows;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
reps = editor.rows;
|
||||||
|
while (--reps) {
|
||||||
|
move_cursor(c == PG_UP ? ARROW_UP : ARROW_DOWN);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case HOME_KEY:
|
||||||
|
case CTRL_KEY('a'):
|
||||||
|
editor.curx = 0;
|
||||||
|
break;
|
||||||
|
case END_KEY:
|
||||||
|
case CTRL_KEY('e'):
|
||||||
|
if (editor.nrows == 0) {
|
||||||
break;
|
break;
|
||||||
default:
|
}
|
||||||
break;
|
editor.curx = editor.row[editor.cury].size;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1752,13 +1801,11 @@ get_cloc_code_lines(const char* filename)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (fgets(buffer, sizeof(buffer), pipe) != NULL) {
|
if (fgets(buffer, sizeof(buffer), pipe) != NULL) {
|
||||||
// Remove trailing newline
|
|
||||||
len = strlen(buffer);
|
len = strlen(buffer);
|
||||||
if (len > 0 && buffer[len - 1] == '\n') {
|
if (len > 0 && buffer[len - 1] == '\n') {
|
||||||
buffer[len - 1] = '\0';
|
buffer[len - 1] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allocate and copy the string
|
|
||||||
result = malloc(strlen(buffer) + 1);
|
result = malloc(strlen(buffer) + 1);
|
||||||
assert(result != NULL);
|
assert(result != NULL);
|
||||||
if (result) {
|
if (result) {
|
||||||
@@ -2054,8 +2101,8 @@ void
|
|||||||
display_clear(struct abuf *ab)
|
display_clear(struct abuf *ab)
|
||||||
{
|
{
|
||||||
if (ab == NULL) {
|
if (ab == NULL) {
|
||||||
write(STDOUT_FILENO, ESCSEQ "2J", 4);
|
(void)write(STDOUT_FILENO, ESCSEQ "2J", 4);
|
||||||
write(STDOUT_FILENO, ESCSEQ "H", 3);
|
(void)write(STDOUT_FILENO, ESCSEQ "H", 3);
|
||||||
} else {
|
} else {
|
||||||
ab_append(ab, ESCSEQ "2J", 4);
|
ab_append(ab, ESCSEQ "2J", 4);
|
||||||
ab_append(ab, ESCSEQ "H", 3);
|
ab_append(ab, ESCSEQ "H", 3);
|
||||||
@@ -2269,7 +2316,7 @@ display_refresh(void)
|
|||||||
/* ab_append(&ab, ESCSEQ "1;2H", 7); */
|
/* ab_append(&ab, ESCSEQ "1;2H", 7); */
|
||||||
ab_append(&ab, ESCSEQ "?25h", 6);
|
ab_append(&ab, ESCSEQ "?25h", 6);
|
||||||
|
|
||||||
write(STDOUT_FILENO, ab.b, ab.len);
|
(void)write(STDOUT_FILENO, ab.b, ab.len);
|
||||||
ab_free(&ab);
|
ab_free(&ab);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2312,7 +2359,6 @@ loop(void)
|
|||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
// Set locale for proper UTF-8 handling
|
|
||||||
setlocale(LC_ALL, "");
|
setlocale(LC_ALL, "");
|
||||||
|
|
||||||
setup_terminal();
|
setup_terminal();
|
||||||
|
|||||||
Reference in New Issue
Block a user