From 9e772f1ef4c75b1b41132a597bd388b8905c23b4 Mon Sep 17 00:00:00 2001 From: MorganPeterson Date: Tue, 18 Jan 2022 12:08:35 -0500 Subject: [PATCH] added current line underline. --- makefile | 11 ++- src/commands.c | 8 +- src/display.c | 24 +++++- src/header.h | 1 + src/main.c | 216 +++++++++++++++++++++++++------------------------ src/themes.c | 3 +- 6 files changed, 146 insertions(+), 117 deletions(-) diff --git a/makefile b/makefile index b9440a4..69754bd 100644 --- a/makefile +++ b/makefile @@ -1,10 +1,13 @@ NAME=mpe -CC=gcc -FLAGS+=-O2 -std=c11 -pedantic -W -Wall -g +CC=tcc +FLAGS+=-O2 -std=c99 -pedantic -W -Wall -g OS:=$(shell uname -s) + +LIBS=-lncursesw + ifeq ($(OS),Linux) - FLAGS+=-D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -lncursesw + FLAGS+=-D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 endif SRCD=src @@ -161,7 +164,7 @@ $(TMO):$(TM) $(BIND)/$(NAME):$(MN) @echo "building $(NAME)" @mkdir -p $(@D) - @$(CC) $(FLAGS) -o $@ $< $(OBJS) + @$(CC) $(FLAGS) -o $@ $< $(OBJS) $(LIBS) clean: @echo "cleaning workspace" diff --git a/src/commands.c b/src/commands.c index a79a46a..1c20695 100644 --- a/src/commands.c +++ b/src/commands.c @@ -126,10 +126,10 @@ linebegin(void) void lineend(void) { - /* do nothing if EOF */ - if (curbuf->point == pos(curbuf, curbuf->buf_end)) - return; - curbuf->point = line_down(curbuf, curbuf->point); + /* do nothing if EOF */ + if (curbuf->point == pos(curbuf, curbuf->buf_end)) + return; + curbuf->point = line_down(curbuf, curbuf->point); int32_t p = curbuf->point; left(); curbuf->point = (*ptr(curbuf, curbuf->point) == '\n') ? curbuf->point : p; diff --git a/src/display.c b/src/display.c index 49536d6..b6bbeaa 100644 --- a/src/display.c +++ b/src/display.c @@ -125,10 +125,28 @@ display_prompt_and_response(char *prompt, char *response) } static void -display_character(buffer_t *b, char_t *p) { - int32_t token_type = parse_text(b, b->page_end); +display_character(buffer_t *b, char_t *p) +{ + int32_t curpt = pos(b, p); + int32_t start = line_start(b, b->point); + int32_t end = segment_next(b, start, b->point)-1; + int32_t x,y; + + int32_t token_type = parse_text(b, b->page_end); attron(COLOR_PAIR(token_type)); - addch(*p); + + if (curpt >= start && curpt <= end) + attron(A_UNDERLINE | A_BOLD); + else + attroff(A_UNDERLINE | A_BOLD); + + if (curpt == end) { + getyx(stdscr, y, x); + for (int32_t i = x; i < COLS-1; i++) { + addch(' '); + } + } + addch(*p); } void diff --git a/src/header.h b/src/header.h index 39d2d50..9ac47cb 100644 --- a/src/header.h +++ b/src/header.h @@ -62,6 +62,7 @@ enum { HL_MODELINE, HL_BACKGROUND, HL_FOREGROUND, + HL_DIM, }; typedef enum { diff --git a/src/main.c b/src/main.c index a38b86d..8b025c0 100644 --- a/src/main.c +++ b/src/main.c @@ -22,118 +22,124 @@ char_t *scrap; int32_t nscrap; int32_t -main(int argc, char **argv) { - struct sigaction action; - memset(&action, 0, sizeof(action)); - action.sa_handler=term_signal; - action.sa_flags=0; - sigaction(SIGTERM, &action, 0); - sigaction(SIGSEGV, &action, 0); - - setlocale(LC_ALL, ""); - - if (initscr() == NULL) - fatal("%s: failed to initialize the screen\n"); - raw(); - noecho(); - idlok(stdscr, 1); - - TABSIZE=4; - - start_color(); - init_colors(); - - bkgd(COLOR_PAIR(HL_BACKGROUND)); - - if (argc > 1) { - char_t bname[BNAME_MAX]; - char_t fname[FNAME_MAX]; - - strn_cpy(fname, (char_t*)argv[1], FNAME_MAX); - make_buffer_name(bname, fname); - - curbuf = find_buffer(bname, 1); - - (void)insert_file(fname, 0); - strn_cpy(curbuf->file_name, fname, FNAME_MAX); - strn_cpy(curbuf->buf_name, bname, BNAME_MAX); - select_syntax(curbuf); - } else { - curbuf = find_buffer(scratch_name, 1); - strn_cpy(curbuf->buf_name, scratch_name, BNAME_MAX); - } - - headwin = curwin = new_window(); - if (headwin == NULL) - die("window malloc failed", 1); - - one_window(headwin); - associate_buffer_to_win(curbuf, headwin); - - beginning_of_buffer(); - - char_t *input; - while (!done) { - update_display(); - input = get_key(&key_map, &key_return); - if (key_return != NULL) { - (key_return->func)(); - } else { - if (*input > 31 || *input == 0x0A || *input == 0x09) { - insert(input); - } else { - flushinp(); - msg("key not bound"); - } - } - } - die("QUIT", 0); +main(int argc, char **argv) +{ + struct sigaction action; + memset(&action, 0, sizeof(action)); + action.sa_handler=term_signal; + action.sa_flags=0; + sigaction(SIGTERM, &action, 0); + sigaction(SIGSEGV, &action, 0); + + setlocale(LC_ALL, ""); + + if (initscr() == NULL) + fatal("%s: failed to initialize the screen\n"); + raw(); + noecho(); + idlok(stdscr, 1); + + TABSIZE=4; + if (has_colors()) { + use_default_colors(); + start_color(); + init_colors(); + } + + bkgd(COLOR_PAIR(HL_BACKGROUND)); + + if (argc > 1) { + char_t bname[BNAME_MAX]; + char_t fname[FNAME_MAX]; + + strn_cpy(fname, (char_t*)argv[1], FNAME_MAX); + make_buffer_name(bname, fname); + + curbuf = find_buffer(bname, 1); + + (void)insert_file(fname, 0); + strn_cpy(curbuf->file_name, fname, FNAME_MAX); + strn_cpy(curbuf->buf_name, bname, BNAME_MAX); + select_syntax(curbuf); + } else { + curbuf = find_buffer(scratch_name, 1); + strn_cpy(curbuf->buf_name, scratch_name, BNAME_MAX); + } + + headwin = curwin = new_window(); + if (headwin == NULL) + die("window malloc failed", 1); + + one_window(headwin); + associate_buffer_to_win(curbuf, headwin); + + beginning_of_buffer(); + + char_t *input; + while (!done) { + update_display(); + input = get_key(&key_map, &key_return); + if (key_return != NULL) { + (key_return->func)(); + } else { + if (*input > 31 || *input == 0x0A || *input == 0x09) { + insert(input); + } else { + flushinp(); + msg("key not bound"); + } + } + } + die("QUIT", 0); } void -term_signal(int32_t n) { - switch (n) { - case SIGTERM: - die("SIGTERM", n); - break; - case SIGSEGV: - die("SIGSEGV", n); - break; - case SIGWINCH: - endwin(); - refresh(); - clear(); - refresh(); - break; - default: - die("UNKNOWN", n); - } +term_signal(int32_t n) +{ + switch (n) { + case SIGTERM: + die("SIGTERM", n); + break; + case SIGSEGV: + die("SIGSEGV", n); + break; + case SIGWINCH: + endwin(); + refresh(); + clear(); + refresh(); + break; + default: + die("UNKNOWN", n); + } } void -die(const char *s, int32_t code) { - free_buffers(); - free_windows(); - if (curscr != NULL) { - move(LINES-1, 0); - refresh(); - noraw(); - endwin(); - } - perror(s); - done = 1; - exit(code); +die(const char *s, int32_t code) +{ + free_buffers(); + free_windows(); + if (curscr != NULL) { + move(LINES-1, 0); + refresh(); + noraw(); + endwin(); + } + perror(s); + done = 1; + exit(code); } void -fatal(char *msg) { - if (curscr != NULL) { - move(LINES-1, 0); - refresh(); - noraw(); - endwin(); - putchar('\n'); - } - fprintf(stderr, msg, "editor"); - exit(1); +fatal(char *msg) +{ + if (curscr != NULL) { + move(LINES-1, 0); + refresh(); + noraw(); + endwin(); + putchar('\n'); + } + fprintf(stderr, msg, "editor"); + exit(1); } diff --git a/src/themes.c b/src/themes.c index 9e796b1..e35c108 100644 --- a/src/themes.c +++ b/src/themes.c @@ -43,7 +43,7 @@ color_t colors[] = { void init_colors(void) { - init_pair(HL_BACKGROUND, colors[BRIGHTBLUE].eightbit, colors[WHITE].eightbit); + init_pair(HL_BACKGROUND, colors[BLACK].eightbit, colors[WHITE].eightbit); init_pair(HL_NORMAL, colors[BLACK].eightbit, colors[WHITE].eightbit); init_pair(HL_SYMBOL, colors[DARKBLUE].eightbit, colors[WHITE].eightbit); init_pair(HL_NUMBER, colors[ORANGE].eightbit, colors[WHITE].eightbit); @@ -54,4 +54,5 @@ init_colors(void) init_pair(HL_MODELINE, colors[NAVY].eightbit, colors[OFFWHITE].eightbit); init_pair(HL_KEYWORD, colors[PINK].eightbit, colors[WHITE].eightbit); init_pair(HL_KEYWORD2, colors[PURPLE].eightbit, colors[WHITE].eightbit); + init_pair(HL_DIM, colors[BLACK].eightbit, colors[OFFWHITE].eightbit); }