Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| acde895fb7 | |||
| 78d522ba98 | |||
| feb6667a24 | |||
|
|
66c79e0762 | ||
| a51b98c31f | |||
| 707362574c | |||
| 56db8bd8d2 |
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.15)
|
||||
project(ke C) # Specify C language explicitly
|
||||
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
set(KE_VERSION "1.3.4")
|
||||
set(KE_VERSION "1.3.6")
|
||||
|
||||
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")
|
||||
|
||||
@@ -16,3 +16,6 @@ To get verbose ASAN messages:
|
||||
export LSAN_OPTIONS=verbosity=1:log_threads=1
|
||||
|
||||
Released under an ISC license.
|
||||
|
||||
Started by following along with kilo:
|
||||
https://viewsourcecode.org/snaptoken/kilo/
|
||||
|
||||
21
default.nix
21
default.nix
@@ -1,16 +1,25 @@
|
||||
{
|
||||
lib,
|
||||
installShellFiles,
|
||||
stdenv,
|
||||
cmake,
|
||||
installShellFiles,
|
||||
...
|
||||
}:
|
||||
stdenv.mkDerivation {
|
||||
pname = "ke";
|
||||
version = "1.3.4";
|
||||
version = "1.3.6";
|
||||
|
||||
src = lib.cleanSource ./.;
|
||||
|
||||
nativeBuildInputs = [ installShellFiles ];
|
||||
nativeBuildInputs = [
|
||||
cmake
|
||||
installShellFiles
|
||||
];
|
||||
|
||||
cmakeFlags = [
|
||||
"-DENABLE_ASAN=on"
|
||||
"-DCMAKE_BUILD_TYPE=Debug"
|
||||
];
|
||||
|
||||
installPhase = ''
|
||||
runHook preInstall
|
||||
@@ -18,10 +27,8 @@ stdenv.mkDerivation {
|
||||
mkdir -p $out/bin
|
||||
cp ke $out/bin/
|
||||
|
||||
installManPage ../ke.1
|
||||
|
||||
runHook postInstall
|
||||
'';
|
||||
|
||||
postInstall = ''
|
||||
installManPage ke.1
|
||||
'';
|
||||
}
|
||||
|
||||
5
ke.1
5
ke.1
@@ -22,6 +22,7 @@ grandeur. Many commands work with and without control; for example,
|
||||
saving a file can be done with either C-k s or C-k C-s. Other commands work
|
||||
with ESC or CTRL.
|
||||
.Sh K-COMMANDS
|
||||
k-command mode can be exited with ESC or C-g.
|
||||
.Bl -tag -width xxxxxxxxxxxx -offset indent
|
||||
.It C-k BACKSPACE
|
||||
Delete from the cursor to the beginning of the line.
|
||||
@@ -36,7 +37,7 @@ Edit a new file. Also C-k C-e.
|
||||
.It C-k f
|
||||
Incremental find.
|
||||
.It C-k g
|
||||
Go to a specific line. Also C-k C-g.
|
||||
Go to a specific line.
|
||||
.It C-k l
|
||||
List the number of lines of code in a saved file.
|
||||
.It C-k m
|
||||
@@ -54,6 +55,8 @@ Dump core.
|
||||
.El
|
||||
.Sh OTHER KEYBINDINGS
|
||||
.Bl -tag -width xxxxxxxxxxxx -offset indent
|
||||
.It C-g
|
||||
In general, C-g cancels an operation.
|
||||
.It C-l
|
||||
Refresh the display.
|
||||
.It C-s
|
||||
|
||||
138
main.c
138
main.c
@@ -2,11 +2,7 @@
|
||||
* kyle's editor
|
||||
*
|
||||
* 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 that will be coming out... when it comes out.
|
||||
*
|
||||
* https://viewsourcecode.org/snaptoken/kilo/
|
||||
* set of guiderails. I've made a lot of changes.
|
||||
*/
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
@@ -156,10 +152,16 @@ void killring_append_char(unsigned char ch);
|
||||
void killring_prepend_char(unsigned char ch);
|
||||
void toggle_markset(void);
|
||||
int cursor_after_mark(void);
|
||||
int count_chars_from_cursor_to_mark(void);
|
||||
void kill_region(void);
|
||||
void indent_region(void);
|
||||
void delete_region(void);
|
||||
|
||||
/* miscellaneous */
|
||||
void kwrite(int fd, const char *buf, int len);
|
||||
void die(const char *s);
|
||||
int get_winsz(int *rows, int *cols);
|
||||
void jump_to_position(int col, int row);
|
||||
void goto_line(void);
|
||||
int cursor_at_eol(void);
|
||||
void delete_row(int at);
|
||||
@@ -791,6 +793,50 @@ kill_region(void)
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
indent_region(void)
|
||||
{
|
||||
int start_row, end_row;
|
||||
int i;
|
||||
|
||||
if (!editor.mark_set) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (editor.mark_cury < editor.cury) {
|
||||
start_row = editor.mark_cury;
|
||||
end_row = editor.cury;
|
||||
} else if (editor.mark_cury > editor.cury) {
|
||||
start_row = editor.cury;
|
||||
end_row = editor.mark_cury;
|
||||
} else {
|
||||
start_row = end_row = editor.cury;
|
||||
}
|
||||
|
||||
/* Ensure bounds are valid */
|
||||
if (start_row < 0) {
|
||||
start_row = 0;
|
||||
}
|
||||
|
||||
if (end_row >= editor.nrows) {
|
||||
end_row = editor.nrows - 1;
|
||||
}
|
||||
|
||||
if (start_row >= editor.nrows || end_row < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Prepend a tab character to every row in the region */
|
||||
for (i = start_row; i <= end_row; i++) {
|
||||
row_insert_ch(&editor.row[i], 0, '\t');
|
||||
}
|
||||
|
||||
/* Move cursor to beginning of the line it was on */
|
||||
editor.curx = 0;
|
||||
editor.dirty++;
|
||||
}
|
||||
|
||||
|
||||
/* call after kill_region */
|
||||
void
|
||||
delete_region(void)
|
||||
@@ -812,8 +858,7 @@ delete_region(void)
|
||||
swap_int(&cury, &marky);
|
||||
}
|
||||
|
||||
editor.curx = markx;
|
||||
editor.cury = marky;
|
||||
jump_to_position(markx, marky);
|
||||
|
||||
while (killed < count) {
|
||||
move_cursor(ARROW_RIGHT);
|
||||
@@ -821,16 +866,34 @@ delete_region(void)
|
||||
killed++;
|
||||
}
|
||||
|
||||
while (editor.curx != markx && editor.cury != marky) {
|
||||
deletech(KILLRING_NO_OP);
|
||||
}
|
||||
|
||||
editor.kill = 1;
|
||||
editor_set_status("Region killed.");
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
kwrite(const int fd, const char *buf, const int len)
|
||||
{
|
||||
int wlen = 0;
|
||||
|
||||
wlen = write(fd, buf, len);
|
||||
assert(wlen != -1);
|
||||
assert(wlen == len);
|
||||
if (wlen == -1) {
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
die(const char *s)
|
||||
{
|
||||
(void)write(STDOUT_FILENO, "\x1b[2J", 4);
|
||||
(void)write(STDOUT_FILENO, "\x1b[H", 3);
|
||||
kwrite(STDOUT_FILENO, "\x1b[2J", 4);
|
||||
kwrite(STDOUT_FILENO, "\x1b[H", 3);
|
||||
|
||||
perror(s);
|
||||
exit(1);
|
||||
@@ -861,6 +924,15 @@ get_winsz(int *rows, int *cols)
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
jump_to_position(const int x, const int y)
|
||||
{
|
||||
editor.curx = x;
|
||||
editor.cury = y;
|
||||
|
||||
editor.rowoffs = editor.cury - (editor.rows / 2);
|
||||
}
|
||||
|
||||
void
|
||||
goto_line(void)
|
||||
{
|
||||
@@ -879,8 +951,7 @@ goto_line(void)
|
||||
return;
|
||||
}
|
||||
|
||||
editor.cury = lineno - 1;
|
||||
editor.rowoffs = editor.cury - (editor.rows / 2);
|
||||
jump_to_position(0, lineno - 1);
|
||||
free(query);
|
||||
}
|
||||
|
||||
@@ -1501,7 +1572,7 @@ editor_find_callback(char *query, int16_t c)
|
||||
char *match;
|
||||
struct erow *row;
|
||||
|
||||
if (c == '\r' || c == ESC_KEY) {
|
||||
if (c == '\r' || c == ESC_KEY || c == CTRL_KEY('g')) {
|
||||
/* reset search */
|
||||
lmatch = -1;
|
||||
dir = 1;
|
||||
@@ -1741,7 +1812,10 @@ newline(void)
|
||||
{
|
||||
struct erow *row = NULL;
|
||||
|
||||
if (editor.curx == 0) {
|
||||
if (editor.cury >= editor.nrows) {
|
||||
/* At or past end of file, insert empty line */
|
||||
erow_insert(editor.cury, "", 0);
|
||||
} else if (editor.curx == 0) {
|
||||
erow_insert(editor.cury, "", 0);
|
||||
} else {
|
||||
row = &editor.row[editor.cury];
|
||||
@@ -1867,7 +1941,6 @@ process_kcommand(int16_t c)
|
||||
|
||||
break;
|
||||
case 'g':
|
||||
case CTRL_KEY('g'):
|
||||
goto_line();
|
||||
break;
|
||||
case BACKSPACE:
|
||||
@@ -1913,13 +1986,20 @@ process_kcommand(int16_t c)
|
||||
case 'y':
|
||||
killring_yank();
|
||||
break;
|
||||
case ESC_KEY:
|
||||
case CTRL_KEY('g'):
|
||||
break;
|
||||
default:
|
||||
if (isprint(c)) {
|
||||
editor_set_status("unknown kcommand '%c'", c);
|
||||
break;
|
||||
}
|
||||
|
||||
editor_set_status("unknown kcommand: %04x", c);
|
||||
return;
|
||||
}
|
||||
|
||||
editor.dirtyex = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -1951,6 +2031,8 @@ process_normal(int16_t c)
|
||||
deletech(KILLRING_PREPEND);
|
||||
}
|
||||
break;
|
||||
case CTRL_KEY('g'):
|
||||
break;
|
||||
case CTRL_KEY('l'):
|
||||
display_refresh();
|
||||
break;
|
||||
@@ -1968,12 +2050,17 @@ process_normal(int16_t c)
|
||||
case ESC_KEY:
|
||||
editor.mode = MODE_ESCAPE;
|
||||
break;
|
||||
default:
|
||||
/* Insert any printable byte: ASCII 0x20–0x7E and all bytes >=0x80. */
|
||||
if ((c == TAB_KEY) || (c >= 0x20 && c != 0x7f)) {
|
||||
insertch(c);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (c == TAB_KEY) {
|
||||
if (editor.mark_set) {
|
||||
indent_region();
|
||||
} else {
|
||||
insertch(c);
|
||||
}
|
||||
} else if (c >= 0x20 && c != 0x7f) {
|
||||
insertch(c);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
editor.dirtyex = 1;
|
||||
@@ -2017,6 +2104,9 @@ process_escape(int16_t c)
|
||||
case BACKSPACE:
|
||||
delete_prev_word();
|
||||
break;
|
||||
case ESC_KEY:
|
||||
case CTRL_KEY('g'):
|
||||
break; /* escape out of escape-mode */
|
||||
default:
|
||||
editor_set_status("unknown ESC key: %04x", c);
|
||||
}
|
||||
@@ -2101,8 +2191,8 @@ void
|
||||
display_clear(struct abuf *ab)
|
||||
{
|
||||
if (ab == NULL) {
|
||||
(void)write(STDOUT_FILENO, ESCSEQ "2J", 4);
|
||||
(void)write(STDOUT_FILENO, ESCSEQ "H", 3);
|
||||
kwrite(STDOUT_FILENO, ESCSEQ "2J", 4);
|
||||
kwrite(STDOUT_FILENO, ESCSEQ "H", 3);
|
||||
} else {
|
||||
ab_append(ab, ESCSEQ "2J", 4);
|
||||
ab_append(ab, ESCSEQ "H", 3);
|
||||
@@ -2316,7 +2406,7 @@ display_refresh(void)
|
||||
/* ab_append(&ab, ESCSEQ "1;2H", 7); */
|
||||
ab_append(&ab, ESCSEQ "?25h", 6);
|
||||
|
||||
(void)write(STDOUT_FILENO, ab.b, ab.len);
|
||||
kwrite(STDOUT_FILENO, ab.b, ab.len);
|
||||
ab_free(&ab);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user