Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 56db8bd8d2 | |||
| a32ef2ff53 | |||
| 0c0c3d9ce5 | |||
| 7245003769 |
@@ -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.3")
|
set(KE_VERSION "1.3.5")
|
||||||
|
|
||||||
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")
|
||||||
|
|||||||
@@ -16,3 +16,6 @@ To get verbose ASAN messages:
|
|||||||
export LSAN_OPTIONS=verbosity=1:log_threads=1
|
export LSAN_OPTIONS=verbosity=1:log_threads=1
|
||||||
|
|
||||||
Released under an ISC license.
|
Released under an ISC license.
|
||||||
|
|
||||||
|
Started by following along with kilo:
|
||||||
|
https://viewsourcecode.org/snaptoken/kilo/
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
}:
|
}:
|
||||||
stdenv.mkDerivation {
|
stdenv.mkDerivation {
|
||||||
pname = "ke";
|
pname = "ke";
|
||||||
version = "1.3.3";
|
version = "1.3.5";
|
||||||
|
|
||||||
src = lib.cleanSource ./.;
|
src = lib.cleanSource ./.;
|
||||||
|
|
||||||
|
|||||||
121
main.c
121
main.c
@@ -2,11 +2,7 @@
|
|||||||
* kyle's editor
|
* kyle's editor
|
||||||
*
|
*
|
||||||
* first version is a run-through of the kilo editor walkthrough as a
|
* 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
|
* set of guiderails. I've made a lot of changes.
|
||||||
* 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/
|
|
||||||
*/
|
*/
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
|
|
||||||
@@ -21,6 +17,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>
|
||||||
@@ -155,6 +152,7 @@ void killring_append_char(unsigned char ch);
|
|||||||
void killring_prepend_char(unsigned char ch);
|
void killring_prepend_char(unsigned char ch);
|
||||||
void toggle_markset(void);
|
void toggle_markset(void);
|
||||||
int cursor_after_mark(void);
|
int cursor_after_mark(void);
|
||||||
|
void indent_region(void);
|
||||||
|
|
||||||
/* miscellaneous */
|
/* miscellaneous */
|
||||||
void die(const char *s);
|
void die(const char *s);
|
||||||
@@ -790,6 +788,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 */
|
/* call after kill_region */
|
||||||
void
|
void
|
||||||
delete_region(void)
|
delete_region(void)
|
||||||
@@ -874,11 +916,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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1469,7 +1513,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 +1631,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)
|
||||||
{
|
{
|
||||||
@@ -1600,12 +1679,20 @@ move_cursor(int16_t c)
|
|||||||
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)
|
||||||
|
? NULL
|
||||||
|
: &editor.row[editor.cury];
|
||||||
|
editor.curx = first_nonwhitespace(row);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ARROW_DOWN:
|
case ARROW_DOWN:
|
||||||
case CTRL_KEY('n'):
|
case CTRL_KEY('n'):
|
||||||
if (editor.cury < editor.nrows) {
|
if (editor.cury < editor.nrows) {
|
||||||
editor.cury++;
|
editor.cury++;
|
||||||
|
row = (editor.cury >= editor.nrows)
|
||||||
|
? NULL
|
||||||
|
: &editor.row[editor.cury];
|
||||||
|
editor.curx = first_nonwhitespace(row);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ARROW_RIGHT:
|
case ARROW_RIGHT:
|
||||||
@@ -1620,7 +1707,10 @@ move_cursor(int16_t c)
|
|||||||
}
|
}
|
||||||
} else if (row && editor.curx == row->size) {
|
} else if (row && editor.curx == row->size) {
|
||||||
editor.cury++;
|
editor.cury++;
|
||||||
editor.curx = 0;
|
row = (editor.cury >= editor.nrows)
|
||||||
|
? NULL
|
||||||
|
: &editor.row[editor.cury];
|
||||||
|
editor.curx = first_nonwhitespace(row);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ARROW_LEFT:
|
case ARROW_LEFT:
|
||||||
@@ -1692,7 +1782,10 @@ newline(void)
|
|||||||
{
|
{
|
||||||
struct erow *row = NULL;
|
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);
|
erow_insert(editor.cury, "", 0);
|
||||||
} else {
|
} else {
|
||||||
row = &editor.row[editor.cury];
|
row = &editor.row[editor.cury];
|
||||||
@@ -1752,13 +1845,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) {
|
||||||
@@ -1922,8 +2013,13 @@ process_normal(int16_t c)
|
|||||||
editor.mode = MODE_ESCAPE;
|
editor.mode = MODE_ESCAPE;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
/* Insert any printable byte: ASCII 0x20–0x7E and all bytes >=0x80. */
|
if (c == TAB_KEY) {
|
||||||
if ((c == TAB_KEY) || (c >= 0x20 && c != 0x7f)) {
|
if (editor.mark_set) {
|
||||||
|
indent_region();
|
||||||
|
} else {
|
||||||
|
insertch(c);
|
||||||
|
}
|
||||||
|
} else if (c >= 0x20 && c != 0x7f) {
|
||||||
insertch(c);
|
insertch(c);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -2312,7 +2408,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