Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| acde895fb7 | |||
| 78d522ba98 | |||
| feb6667a24 | |||
|
|
66c79e0762 | ||
| a51b98c31f | |||
| 707362574c | |||
| 56db8bd8d2 | |||
| a32ef2ff53 | |||
| 0c0c3d9ce5 | |||
| 7245003769 | |||
| 542b1d90a0 | |||
| a359f4e6c4 | |||
| a03dd0c433 | |||
| 561faf537c | |||
| 3a36b35c1f | |||
|
|
b1cb2532f6 |
@@ -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.1")
|
set(KE_VERSION "1.3.6")
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
|
|||||||
@@ -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/
|
||||||
|
|||||||
34
default.nix
Normal file
34
default.nix
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
{
|
||||||
|
lib,
|
||||||
|
stdenv,
|
||||||
|
cmake,
|
||||||
|
installShellFiles,
|
||||||
|
...
|
||||||
|
}:
|
||||||
|
stdenv.mkDerivation {
|
||||||
|
pname = "ke";
|
||||||
|
version = "1.3.6";
|
||||||
|
|
||||||
|
src = lib.cleanSource ./.;
|
||||||
|
|
||||||
|
nativeBuildInputs = [
|
||||||
|
cmake
|
||||||
|
installShellFiles
|
||||||
|
];
|
||||||
|
|
||||||
|
cmakeFlags = [
|
||||||
|
"-DENABLE_ASAN=on"
|
||||||
|
"-DCMAKE_BUILD_TYPE=Debug"
|
||||||
|
];
|
||||||
|
|
||||||
|
installPhase = ''
|
||||||
|
runHook preInstall
|
||||||
|
|
||||||
|
mkdir -p $out/bin
|
||||||
|
cp ke $out/bin/
|
||||||
|
|
||||||
|
installManPage ../ke.1
|
||||||
|
|
||||||
|
runHook postInstall
|
||||||
|
'';
|
||||||
|
}
|
||||||
27
flake.lock
generated
Normal file
27
flake.lock
generated
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"nodes": {
|
||||||
|
"nixpkgs": {
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1763835633,
|
||||||
|
"narHash": "sha256-HzxeGVID5MChuCPESuC0dlQL1/scDKu+MmzoVBJxulM=",
|
||||||
|
"owner": "nixos",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"rev": "050e09e091117c3d7328c7b2b7b577492c43c134",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "nixos",
|
||||||
|
"ref": "nixos-unstable",
|
||||||
|
"repo": "nixpkgs",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": {
|
||||||
|
"inputs": {
|
||||||
|
"nixpkgs": "nixpkgs"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"root": "root",
|
||||||
|
"version": 7
|
||||||
|
}
|
||||||
18
flake.nix
Normal file
18
flake.nix
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
description = "Kyle's Text Editor";
|
||||||
|
|
||||||
|
inputs = {
|
||||||
|
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
|
||||||
|
};
|
||||||
|
|
||||||
|
outputs =
|
||||||
|
{ self, nixpkgs }:
|
||||||
|
let
|
||||||
|
pkgs = import nixpkgs { system = "x86_64-linux"; };
|
||||||
|
in
|
||||||
|
{
|
||||||
|
packages.x86_64-linux = {
|
||||||
|
default = pkgs.callPackage ./default.nix { };
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
38
ke.1
38
ke.1
@@ -19,21 +19,27 @@ is
|
|||||||
K-command mode is entered using C-k. This is taken from Wordstar and just
|
K-command mode is entered using C-k. This is taken from Wordstar and just
|
||||||
so happens to be blessed with starting with a most excellent letter of
|
so happens to be blessed with starting with a most excellent letter of
|
||||||
grandeur. Many commands work with and without control; for example,
|
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.
|
saving a file can be done with either C-k s or C-k C-s. Other commands work
|
||||||
.Pp
|
with ESC or CTRL.
|
||||||
|
.Sh K-COMMANDS
|
||||||
|
k-command mode can be exited with ESC or C-g.
|
||||||
.Bl -tag -width xxxxxxxxxxxx -offset indent
|
.Bl -tag -width xxxxxxxxxxxx -offset indent
|
||||||
.It C-k BACKSPACE
|
.It C-k BACKSPACE
|
||||||
Delete from the cursor to the beginning of the line.
|
Delete from the cursor to the beginning of the line.
|
||||||
|
.It C-k SPACE
|
||||||
|
Toggle the mark.
|
||||||
.It C-k d
|
.It C-k d
|
||||||
Delete from the cursor to the end of the line.
|
Delete from the cursor to the end of the line.
|
||||||
.It C-k C-d
|
.It C-k C-d
|
||||||
Delete the entire link.
|
Delete the entire line.
|
||||||
.It C-k e
|
.It C-k e
|
||||||
Edit a new file. Also C-k C-e.
|
Edit a new file. Also C-k C-e.
|
||||||
.It C-k f
|
.It C-k f
|
||||||
Incremental find.
|
Incremental find.
|
||||||
.It C-k g
|
.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
|
.It C-k m
|
||||||
Run make(1).
|
Run make(1).
|
||||||
.It C-k q
|
.It C-k q
|
||||||
@@ -47,6 +53,30 @@ Yank the killring.
|
|||||||
.It C-k \[char92]
|
.It C-k \[char92]
|
||||||
Dump core.
|
Dump core.
|
||||||
.El
|
.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
|
||||||
|
Incremental find.
|
||||||
|
.It C-w
|
||||||
|
Kill the region if the mark is set.
|
||||||
|
.It C-y
|
||||||
|
Yank the killring.
|
||||||
|
.It ESC BACKSPACE
|
||||||
|
Delete the previous word.
|
||||||
|
.It ESC b
|
||||||
|
Move to the previous word.
|
||||||
|
.It ESC d
|
||||||
|
Delete the next word.
|
||||||
|
.It ESC f
|
||||||
|
Move to the next word.
|
||||||
|
.It ESC w
|
||||||
|
Save the region (if the mark is set) to the killring.
|
||||||
|
.It
|
||||||
|
.El
|
||||||
.Sh FIND
|
.Sh FIND
|
||||||
The find operation is an incremental search. The up or left arrow keys will
|
The find operation is an incremental search. The up or left arrow keys will
|
||||||
go to the previous result, while the down or right arrow keys will go to
|
go to the previous result, while the down or right arrow keys will go to
|
||||||
|
|||||||
228
main.c
228
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,9 +17,11 @@
|
|||||||
#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>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
|
||||||
#ifndef KE_VERSION
|
#ifndef KE_VERSION
|
||||||
@@ -154,10 +152,16 @@ 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);
|
||||||
|
int count_chars_from_cursor_to_mark(void);
|
||||||
|
void kill_region(void);
|
||||||
|
void indent_region(void);
|
||||||
|
void delete_region(void);
|
||||||
|
|
||||||
/* miscellaneous */
|
/* miscellaneous */
|
||||||
|
void kwrite(int fd, const char *buf, int len);
|
||||||
void die(const char *s);
|
void die(const char *s);
|
||||||
int get_winsz(int *rows, int *cols);
|
int get_winsz(int *rows, int *cols);
|
||||||
|
void jump_to_position(int col, int row);
|
||||||
void goto_line(void);
|
void goto_line(void);
|
||||||
int cursor_at_eol(void);
|
int cursor_at_eol(void);
|
||||||
void delete_row(int at);
|
void delete_row(int at);
|
||||||
@@ -789,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 */
|
/* call after kill_region */
|
||||||
void
|
void
|
||||||
delete_region(void)
|
delete_region(void)
|
||||||
@@ -810,8 +858,7 @@ delete_region(void)
|
|||||||
swap_int(&cury, &marky);
|
swap_int(&cury, &marky);
|
||||||
}
|
}
|
||||||
|
|
||||||
editor.curx = markx;
|
jump_to_position(markx, marky);
|
||||||
editor.cury = marky;
|
|
||||||
|
|
||||||
while (killed < count) {
|
while (killed < count) {
|
||||||
move_cursor(ARROW_RIGHT);
|
move_cursor(ARROW_RIGHT);
|
||||||
@@ -819,16 +866,34 @@ delete_region(void)
|
|||||||
killed++;
|
killed++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
while (editor.curx != markx && editor.cury != marky) {
|
||||||
|
deletech(KILLRING_NO_OP);
|
||||||
|
}
|
||||||
|
|
||||||
editor.kill = 1;
|
editor.kill = 1;
|
||||||
editor_set_status("Region killed.");
|
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
|
void
|
||||||
die(const char *s)
|
die(const char *s)
|
||||||
{
|
{
|
||||||
write(STDOUT_FILENO, "\x1b[2J", 4);
|
kwrite(STDOUT_FILENO, "\x1b[2J", 4);
|
||||||
write(STDOUT_FILENO, "\x1b[H", 3);
|
kwrite(STDOUT_FILENO, "\x1b[H", 3);
|
||||||
|
|
||||||
perror(s);
|
perror(s);
|
||||||
exit(1);
|
exit(1);
|
||||||
@@ -859,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
|
void
|
||||||
goto_line(void)
|
goto_line(void)
|
||||||
{
|
{
|
||||||
@@ -873,11 +947,12 @@ 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;
|
jump_to_position(0, lineno - 1);
|
||||||
editor.rowoffs = editor.cury - (editor.rows / 2);
|
free(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1448,7 +1523,7 @@ editor_prompt(char *prompt, void (*cb)(char *, int16_t))
|
|||||||
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';
|
||||||
@@ -1468,7 +1543,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);
|
||||||
@@ -1497,7 +1572,7 @@ editor_find_callback(char *query, int16_t c)
|
|||||||
char *match;
|
char *match;
|
||||||
struct erow *row;
|
struct erow *row;
|
||||||
|
|
||||||
if (c == '\r' || c == ESC_KEY) {
|
if (c == '\r' || c == ESC_KEY || c == CTRL_KEY('g')) {
|
||||||
/* reset search */
|
/* reset search */
|
||||||
lmatch = -1;
|
lmatch = -1;
|
||||||
dir = 1;
|
dir = 1;
|
||||||
@@ -1586,6 +1661,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)
|
||||||
{
|
{
|
||||||
@@ -1599,12 +1709,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:
|
||||||
@@ -1619,7 +1737,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:
|
||||||
@@ -1691,7 +1812,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];
|
||||||
@@ -1714,30 +1838,50 @@ newline(void)
|
|||||||
char *
|
char *
|
||||||
get_cloc_code_lines(const char* filename)
|
get_cloc_code_lines(const char* filename)
|
||||||
{
|
{
|
||||||
// Build the shell command dynamically
|
|
||||||
char command[512];
|
char command[512];
|
||||||
|
char buffer[256];
|
||||||
|
char *result = NULL;
|
||||||
|
FILE *pipe = NULL;
|
||||||
|
size_t len = 0;
|
||||||
|
|
||||||
|
if (editor.filename == NULL) {
|
||||||
|
snprintf(command, sizeof(command),
|
||||||
|
"buffer has no associated file.");
|
||||||
|
result = malloc((strnlen(command, sizeof(command))) + 1);
|
||||||
|
assert(result != NULL);
|
||||||
|
strcpy(result, command);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (editor.dirty) {
|
||||||
|
snprintf(command, sizeof(command),
|
||||||
|
"buffer must be saved first.");
|
||||||
|
result = malloc((strnlen(command, sizeof(command))) + 1);
|
||||||
|
assert(result != NULL);
|
||||||
|
strcpy(result, command);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
snprintf(command,
|
snprintf(command,
|
||||||
sizeof(command),
|
sizeof(command),
|
||||||
"cloc --quiet %s | tail -2 | head -1 | awk '{print $5}'",
|
"cloc --quiet %s | tail -2 | head -1 | awk '{print $5}'",
|
||||||
filename);
|
filename);
|
||||||
|
|
||||||
// Open a pipe to run the command
|
pipe = popen(command, "r");
|
||||||
FILE *pipe = popen(command, "r");
|
|
||||||
if (!pipe) {
|
if (!pipe) {
|
||||||
return NULL; // Error opening pipe
|
snprintf(command, sizeof(command), "Error getting LOC: %s", strerror(errno));
|
||||||
|
result = (char *)malloc(sizeof(buffer) + 1);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read the output (single line/number)
|
|
||||||
char buffer[256];
|
|
||||||
if (fgets(buffer, sizeof(buffer), pipe) != NULL) {
|
if (fgets(buffer, sizeof(buffer), pipe) != NULL) {
|
||||||
// Remove trailing newline
|
len = strlen(buffer);
|
||||||
size_t 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);
|
||||||
char *result = malloc(strlen(buffer) + 1);
|
assert(result != NULL);
|
||||||
if (result) {
|
if (result) {
|
||||||
strcpy(result, buffer);
|
strcpy(result, buffer);
|
||||||
pclose(pipe);
|
pclose(pipe);
|
||||||
@@ -1745,7 +1889,6 @@ get_cloc_code_lines(const char* filename)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// On error or empty output, return "0"
|
|
||||||
pclose(pipe);
|
pclose(pipe);
|
||||||
char *zero = malloc(2);
|
char *zero = malloc(2);
|
||||||
if (zero) {
|
if (zero) {
|
||||||
@@ -1798,7 +1941,6 @@ process_kcommand(int16_t c)
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
case 'g':
|
case 'g':
|
||||||
case CTRL_KEY('g'):
|
|
||||||
goto_line();
|
goto_line();
|
||||||
break;
|
break;
|
||||||
case BACKSPACE:
|
case BACKSPACE:
|
||||||
@@ -1844,13 +1986,20 @@ process_kcommand(int16_t c)
|
|||||||
case 'y':
|
case 'y':
|
||||||
killring_yank();
|
killring_yank();
|
||||||
break;
|
break;
|
||||||
|
case ESC_KEY:
|
||||||
|
case CTRL_KEY('g'):
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
|
if (isprint(c)) {
|
||||||
|
editor_set_status("unknown kcommand '%c'", c);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
editor_set_status("unknown kcommand: %04x", c);
|
editor_set_status("unknown kcommand: %04x", c);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
editor.dirtyex = 1;
|
editor.dirtyex = 1;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1882,6 +2031,8 @@ process_normal(int16_t c)
|
|||||||
deletech(KILLRING_PREPEND);
|
deletech(KILLRING_PREPEND);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case CTRL_KEY('g'):
|
||||||
|
break;
|
||||||
case CTRL_KEY('l'):
|
case CTRL_KEY('l'):
|
||||||
display_refresh();
|
display_refresh();
|
||||||
break;
|
break;
|
||||||
@@ -1900,8 +2051,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;
|
||||||
@@ -1948,6 +2104,9 @@ process_escape(int16_t c)
|
|||||||
case BACKSPACE:
|
case BACKSPACE:
|
||||||
delete_prev_word();
|
delete_prev_word();
|
||||||
break;
|
break;
|
||||||
|
case ESC_KEY:
|
||||||
|
case CTRL_KEY('g'):
|
||||||
|
break; /* escape out of escape-mode */
|
||||||
default:
|
default:
|
||||||
editor_set_status("unknown ESC key: %04x", c);
|
editor_set_status("unknown ESC key: %04x", c);
|
||||||
}
|
}
|
||||||
@@ -2032,8 +2191,8 @@ void
|
|||||||
display_clear(struct abuf *ab)
|
display_clear(struct abuf *ab)
|
||||||
{
|
{
|
||||||
if (ab == NULL) {
|
if (ab == NULL) {
|
||||||
write(STDOUT_FILENO, ESCSEQ "2J", 4);
|
kwrite(STDOUT_FILENO, ESCSEQ "2J", 4);
|
||||||
write(STDOUT_FILENO, ESCSEQ "H", 3);
|
kwrite(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);
|
||||||
@@ -2247,7 +2406,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);
|
kwrite(STDOUT_FILENO, ab.b, ab.len);
|
||||||
ab_free(&ab);
|
ab_free(&ab);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2290,7 +2449,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