Compare commits

...

23 Commits

Author SHA1 Message Date
32c4139c72 Added check to prevent page down from looping endlessly 2023-04-27 10:03:31 -05:00
249cc3cb24 Added scroll functionality when any key is pressed on the last line 2023-04-26 09:37:56 -05:00
68de7f7337 Added handler functions for right and left arrow keys, that check cursor position before moving the gap 2023-04-25 07:50:51 -05:00
1355c58cf3 Fixed a bug where, if a page ends in a line that just contains the newline character, the page doesn't render correctly after scrolling 2023-04-24 15:34:12 -05:00
d4a7dbb88f Routine changes 2023-04-24 08:03:15 -05:00
315245dd54 Added a window within the stdscr, which is used for printing the text, as well as another 'status window' 2023-04-22 18:37:38 -05:00
d4021068c8 Added 'Delete' key functionality 2023-04-22 14:14:35 -05:00
230d26e411 Fixed bug with creating a new file, and then saving it 2023-04-22 13:31:01 -05:00
f4f19c7c39 Switched to using raw mode, which allows me to capture the 'Ctrl+s' keypress. I have implemented save functionality using this keypress, and implemented functionality to capture Ctrl-C as well. 2023-04-22 10:14:09 -05:00
3b20d6ccf7 Fixed page down support, added page up support 2023-04-21 21:12:35 -05:00
563271d1b2 Coninued working on page overflow 2023-04-21 08:11:55 -05:00
9524ba4df9 Implemented rudimentary 'page down' key support, which moves the cursor to the first character of the next line 2023-04-20 23:54:48 -05:00
4d4f0f16c8 Started working on Down arrow key support 2023-04-20 21:30:29 -05:00
071ddf5ac0 Started implementing overflow and scrolling for files 2023-04-20 14:50:13 -05:00
5414cf41c9 Fixed bug where cursor was positioned to the second character instead of the first, after reading a file 2023-04-19 17:21:57 -05:00
6d50747752 Fixed a bug with moving the cursor to the start of the text, and fixed a bug where the grow method would cause weird anamolies 2023-04-19 17:21:14 -05:00
687453870d Started fixing bug that occures when gap is at the start of the text 2023-04-19 14:27:30 -05:00
2ae0b4b1fe Fixed bug with reading characters from a file 2023-04-19 13:21:56 -05:00
25c1f6a2c1 Started working on file opening support 2023-04-19 08:07:17 -05:00
68305de7f6 Updated .gitignore to exclude object files 2023-04-19 00:23:44 -05:00
bb8917645e Added Makefile 2023-04-19 00:23:22 -05:00
438bd61c0d Added .gitignore 2023-04-19 00:16:47 -05:00
da0f6fdf32 Added code to properly support 'Enter' key, which was being sent as '\r\n' instead of '\n' 2023-04-19 00:16:10 -05:00
3 changed files with 247 additions and 26 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
editor
editor.o

23
Makefile Normal file
View File

@@ -0,0 +1,23 @@
EXEC_FILE=editor
CFLAGS=
all: $(EXEC_FILE)
$(EXEC_FILE): $(EXEC_FILE).o
gcc $(CFLAGS) -o $@ $^ -lncurses
$(EXEC_FILE).o: $(EXEC_FILE).c
gcc $(CFLAGS) -c -o $@ $^
.PHONY: debug
debug: CFLAGS+=-g
debug: $(EXEC_FILE)
.PHONY: allwarn
allwarn: CFLAGS+=-Wall -Wextra -pedantic
allwarn: $(EXEC_FILE)
.PHONY: clean
clean:
rm $(EXEC_FILE)
rm $(EXEC_FILE).o

248
editor.c
View File

@@ -4,6 +4,14 @@
#include <signal.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/stat.h>
#define ctrl(x) ((x) & 0x1f)
int index_to_start = 0;
WINDOW* mainwin;
WINDOW* statusbar;
typedef struct Buffer_struct Buffer;
struct Buffer_struct {
@@ -27,14 +35,17 @@ Buffer* new_buffer(int size) {
void buffer_grow(Buffer* buffer) {
int old_size = buffer->size;
int old_size = buffer->size; /* I am making use of the fact that, whenever this
function is called, the strlen of the string must
equal the size of the buffer (since the gap size is zero) */
int start_offset = buffer->start - buffer->text;
buffer->size += 10;
buffer->text = realloc(buffer->text,buffer->size);
buffer->start = buffer->text + start_offset;
buffer->gap_size = 10;
for (int i=0; i < (old_size - start_offset); i++) {
for (int i= (old_size - start_offset) - 1; i >= 0; i--) {
*(buffer->start + i + buffer->gap_size) = *(buffer->start + i);
*(buffer->start + i) = 0;
}
@@ -52,13 +63,20 @@ void buffer_insert(char ch, Buffer* buffer) {
}
}
void buffer_delete(Buffer* buffer) {
void buffer_delete_front(Buffer* buffer) {
if (buffer->start != buffer->text) {
buffer->start--;
buffer->gap_size++;
}
}
void buffer_delete_back(Buffer* buffer) {
if (buffer->end != buffer->text + buffer->size) {
buffer->end++;
buffer->gap_size++;
}
}
void buffer_right(Buffer* buffer) {
if (buffer->end != buffer->text + buffer->size) {
char c = *(buffer->end);
@@ -68,6 +86,12 @@ void buffer_right(Buffer* buffer) {
}
}
void right_key_handler(Buffer* buffer) {
if ((*(buffer->end) != '\n') || (getcury(mainwin) + 1 < getmaxy(mainwin))) {
buffer_right(buffer);
}
}
void buffer_left(Buffer* buffer) {
if (buffer->start != buffer->text) {
char c = *(buffer->start - 1);
@@ -78,70 +102,242 @@ void buffer_left(Buffer* buffer) {
}
}
void init_curses() {
initscr();
noecho();
keypad(stdscr,TRUE);
cbreak();
void left_key_handler(Buffer* buffer) {
if ((getcurx(mainwin) > 0) || (getcury(mainwin) > 0)) {
buffer_left(buffer);
}
}
void sigint_handler(int dummy) {
void scroll_page_down_handler(Buffer* buffer) {
while (*(buffer->text + index_to_start) != '\n') {
index_to_start++;
}
index_to_start++;
}
void page_down_handler(Buffer* buffer) {
if ((getcury(mainwin) + 1) == getmaxy(mainwin)) {
scroll_page_down_handler(buffer);
}
buffer_right(buffer); /* I must advance the cursor at least
once, so this hardcoded statement is fine. */
while ((*(buffer->start - 1) != '\n') && (buffer->end != (buffer->text + buffer->size))) {
buffer_right(buffer);
}
/* You would think that I need to call 'buffer_right' once
more, to advance the cursor onto the next line. In fact, if
you think about it, the place where the cursor (the rectangle)
is, is actually the character _after_ the gap. Therefore, by
advancing the start to the newline character, the character
after the gap (i.e. the cursor) will automatically be moved to
the next line. */
}
void scroll_page_up_handler(Buffer* buffer) {
for (int i=0;i<2;i++) { /* we need to encounter two 'newlines' before we stop */
while((*(buffer->text + index_to_start) != '\n') && (index_to_start >= 0)) {
index_to_start--;
}
}
index_to_start++;
}
void page_up_handler(Buffer* buffer) {
while((*(buffer->start - 1) != '\n') && (buffer->text != buffer->start)) {
buffer_left(buffer);
}
buffer_left(buffer);
while((*(buffer->start - 1) != '\n') && (buffer->text != buffer->start)) {
buffer_left(buffer);
}
if ((getcury(mainwin) == 0) && (index_to_start > 0)) {
scroll_page_up_handler(buffer);
}
}
void keypress_handler(char key, Buffer* buffer) {
buffer_insert(key,buffer);
if ((getcury(mainwin) + 1) == getmaxy(mainwin)) {
scroll_page_down_handler(buffer);
}
}
int is_file(char* path) {
struct stat st;
if (stat(path, &st) < 0) {
return -1;
}
return S_ISREG(st.st_mode);
}
void save_text_helper(Buffer* buffer,char* filename) {
FILE* file = fopen(filename,"w");
int i = 0;
while (i < buffer->size) {
if ((buffer->start - buffer->text) == i) { /* If we have encountered
the start of the gap */
i += buffer->gap_size;
}
if (i >= buffer->size) {
break;
}
fputc(*(buffer->text + i),file);
i++;
}
}
void init_curses() {
initscr();
mainwin = newwin(getmaxy(stdscr)-1,getmaxx(stdscr),0,0);
statusbar = newwin(1,getmaxx(stdscr),getmaxy(stdscr)-1,0);
wattrset(statusbar,A_REVERSE);
mvwhline(statusbar,0,0,' ',getmaxx(statusbar));
mvwprintw(statusbar,0,0,"Welcome to Editor!");
wrefresh(statusbar);
noecho();
keypad(stdscr,TRUE);
keypad(mainwin,TRUE);
raw();
}
void end_ncurses() {
endwin();
exit(130);
}
int main() {
signal(SIGINT,sigint_handler);
int main(int argc, char** argv) {
Buffer* buffer = new_buffer(10);
FILE* logfile = fopen("logfile.txt","w");
if (argc == 2) {
int num_of_chars = 0;
if (access(argv[1],F_OK) == 0) { /* If the file exists */
FILE* file = fopen(argv[1],"r");
char c;
while ((c = fgetc(file)) != EOF) {
buffer_insert(c,buffer);
num_of_chars++;
}
while (num_of_chars > 0) {
buffer_left(buffer);
num_of_chars--;
}
}
}
init_curses();
int ch;
int y, x;
while (true) {
clear();
wclear(mainwin);
int i=0;
if (buffer->start != buffer->text) { /* We don't want to print the string, if the
gap starts at the first index of the string */
int i=index_to_start;
if ((buffer->start != buffer->text) || (buffer->gap_size != buffer->size)) { /* We don't want to print the string, if the
gap starts at the first index of the string, and continues till the end */
while (i < buffer->size) {
addch(*(buffer->text + i));
i++;
if ((buffer->start - buffer->text) == i) { /* If we have encountered
the start of the gap */
getyx(stdscr,y,x);
getyx(mainwin,y,x);
i += buffer->gap_size;
}
if (i >= buffer->size) {
break;
}
fprintf(logfile,"At line %d of %d\n",getcury(mainwin),getmaxy(mainwin));
if (*(buffer->text + i) == '\n' && ((getcury(mainwin) + 1) == getmaxy(mainwin))) {
fprintf(logfile,"Read newline\n");
break;
}
waddch(mainwin,*(buffer->text + i));
i++;
}
move(y,x);
wmove(mainwin,y,x);
wrefresh(mainwin);
}
ch = getch();
ch = wgetch(mainwin);
switch(ch) {
case KEY_BACKSPACE:
buffer_delete(buffer);
buffer_delete_front(buffer);
break;
case KEY_DC:
buffer_delete_back(buffer);
break;
case KEY_LEFT:
buffer_left(buffer);
left_key_handler(buffer);
break;
case KEY_RIGHT:
buffer_right(buffer);
right_key_handler(buffer);
break;
case 10: /* Enter key */
keypress_handler('\n',buffer); /* Why handle this separately?
Because, by default, curses seems to send '\r\n',
which is technically two characters. I should
probably add some code to deal with this scenario
in the 'insert' method (instead of creating an
exception here), but that's a problem for another day. */
break;
case KEY_NPAGE:
page_down_handler(buffer);
break;
case KEY_PPAGE:
page_up_handler(buffer);
break;
case ctrl(KEY_NPAGE):
abort();
case ctrl('s'):
if (argc == 2) {
save_text_helper(buffer,argv[1]);
} else if (argc == 1) {
save_text_helper(buffer,NULL);
}
break;
case ctrl('c'):
end_ncurses();
break;
default:
buffer_insert(ch,buffer);
keypress_handler(ch,buffer);
}
continue_while_loop:
}
endwin();
}