Compare commits

..

6 commits

Author SHA1 Message Date
8573bc1c5c
Resolve compiler warnings and udpate CFLAGS 2024-11-05 00:02:09 +00:00
6f5904ecf6
Fix indentation 2024-10-27 01:37:40 +00:00
81581b1905
Weirdly alternate buffer works 2024-10-27 01:28:30 +00:00
f6eaf81d8c
Fix makefile 2024-10-27 01:46:50 +01:00
df3059f120
Update Makefile 2024-10-22 01:29:49 +01:00
2bc1fbe8d2
Update Makefile 2024-10-22 01:29:13 +01:00
9 changed files with 45 additions and 52 deletions

View file

@ -1,20 +1,17 @@
.POSIX: .POSIX:
.SUFFIXES: .SUFFIXES:
CC = cc
VERSION = 1.0 VERSION = 1.0
TARGET = vip TARGET = vip
PREFIX ?= /usr/local PREFIX ?= /usr/local
BINDIR = $(PREFIX)/bin BINDIR = $(PREFIX)/bin
MANDIR = $(PREFIX)/share/man/man1
# Flags CFLAGS = -Os -march=native -mtune=native -pipe -s -flto -std=c99 -pedantic -Wall -D_DEFAULT_SOURCE
CFLAGS = -O3 -march=native -mtune=native -pipe -s -flto -std=c99 -pedantic -Wall -D_DEFAULT_SOURCE -D_XOPEN_SOURCE=600
SRC = src/*.c config.h SRC != find src -name *.c
INCLUDE = include INCLUDE = include
$(TARGET): $(SRC) $(TARGET): $(SRC) config.h
$(CC) $(SRC) -o $@ $(CFLAGS) -I$(INCLUDE) -I. $(CC) $(SRC) -o $@ $(CFLAGS) -I$(INCLUDE) -I.
dist: dist:
@ -26,15 +23,14 @@ dist:
install: $(TARGET) install: $(TARGET)
mkdir -p $(DESTDIR)$(BINDIR) mkdir -p $(DESTDIR)$(BINDIR)
mkdir -p $(DESTDIR)$(MANDIR)
cp -p $(TARGET) $(DESTDIR)$(BINDIR)/$(TARGET) cp -p $(TARGET) $(DESTDIR)$(BINDIR)/$(TARGET)
chmod 755 $(DESTDIR)$(BINDIR)/$(TARGET) chmod 755 $(DESTDIR)$(BINDIR)/$(TARGET)
uninstall: uninstall:
$(RM) $(DESTDIR)$(BINDIR)/$(TARGET) rm $(DESTDIR)$(BINDIR)/$(TARGET)
clean: clean:
$(RM) $(TARGET) rm $(TARGET)
all: $(TARGET) all: $(TARGET)

View file

@ -1,16 +1,16 @@
#ifndef EDITOR_H_ #ifndef EDITOR_H_
#define EDITOR_H_ #define EDITOR_H_
void refresh_screen(); void refresh_screen(void);
void move_cursor(int key); void move_cursor(int key);
void scroll(); void scroll(void);
void insert_char(int c); void insert_char(int c);
void insert_new_line(); void insert_new_line(void);
void shift_new_line(); void shift_new_line(void);
void del_char(); void del_char(void);
void init_editor(); void init_editor(void);
void open_editor(char *filename); void open_editor(char *filename);
char *prompt_editor(char *prompt, void (*callback)(char *, int)); char *prompt_editor(char *prompt, void (*callback)(char *, int));
void find_editor(); void find_editor(void);
#endif #endif

View file

@ -1,9 +1,9 @@
#ifndef IO_H_ #ifndef IO_H_
#define IO_H_ #define IO_H_
int read_key(); int read_key(void);
void save_file(); void save_file(void);
void process_key(); void process_key(void);
void draw_rows(struct abuf *ab); void draw_rows(struct abuf *ab);
#endif #endif

View file

@ -21,6 +21,6 @@ typedef struct language {
int is_separator(int c); int is_separator(int c);
void update_highlight(row *row); void update_highlight(row *row);
char *syntax_to_color(int hl, size_t *len); char *syntax_to_color(int hl, size_t *len);
void select_syntax_highlight(); void select_syntax_highlight(void);
#endif #endif

View file

@ -4,8 +4,8 @@
#define CTRL_KEY(k) ((k) & 0x1f) #define CTRL_KEY(k) ((k) & 0x1f)
void die(const char *s); void die(const char *s);
void reset_term(); void reset_term(void);
void setup_term(); void setup_term(void);
int get_cursor_position(int *rows, int *cols); int get_cursor_position(int *rows, int *cols);
int get_window_size(int *rows, int *cols); int get_window_size(int *rows, int *cols);

View file

@ -12,7 +12,7 @@
extern editor vip; extern editor vip;
void refresh_screen() void refresh_screen(void)
{ {
scroll(); scroll();
@ -68,7 +68,7 @@ void move_cursor(int key)
} }
} }
void scroll() void scroll(void)
{ {
vip.rx = 0; vip.rx = 0;
if (vip.cy < vip.rows) { if (vip.cy < vip.rows) {
@ -97,7 +97,7 @@ void insert_char(int c)
vip.cx++; vip.cx++;
} }
void insert_new_line() void insert_new_line(void)
{ {
if (vip.cx == 0) { if (vip.cx == 0) {
insert_row(vip.cy, "", 0); insert_row(vip.cy, "", 0);
@ -116,14 +116,14 @@ void insert_new_line()
/* /*
* 'o' in vim * 'o' in vim
*/ */
void shift_new_line() void shift_new_line(void)
{ {
insert_row(vip.cy + 1, "", 0); insert_row(vip.cy + 1, "", 0);
vip.cy++; vip.cy++;
vip.cx = 0; vip.cx = 0;
} }
void del_char() void del_char(void)
{ {
if (vip.cy == vip.rows) return; if (vip.cy == vip.rows) return;
if (vip.cx == 0 && vip.cy == 0) return; if (vip.cx == 0 && vip.cy == 0) return;
@ -140,7 +140,7 @@ void del_char()
} }
} }
void init_editor() void init_editor(void)
{ {
vip.cx = 0; vip.cx = 0;
vip.cy = 0; vip.cy = 0;
@ -280,7 +280,7 @@ void find_callback(char *query, int key)
} }
} }
void find_editor() void find_editor(void)
{ {
int tmp_cx = vip.cx; int tmp_cx = vip.cx;
int tmp_cy = vip.cy; int tmp_cy = vip.cy;

View file

@ -10,7 +10,7 @@
#include "vip.h" #include "vip.h"
#include "bar.h" #include "bar.h"
int read_key() int read_key(void)
{ {
int nread; int nread;
char c; char c;
@ -65,7 +65,7 @@ int read_key()
} }
} }
void save_file() void save_file(void)
{ {
if (vip.filename == NULL) { if (vip.filename == NULL) {
vip.filename = prompt_editor("Save as: %s", NULL); vip.filename = prompt_editor("Save as: %s", NULL);
@ -94,9 +94,8 @@ void save_file()
set_status_bar_message("Error saving: %s", strerror(errno)); set_status_bar_message("Error saving: %s", strerror(errno));
} }
void process_key() void process_key(void)
{ {
int c = read_key(); int c = read_key();
switch (c) { switch (c) {
case '\r': case '\r':
@ -172,8 +171,6 @@ void process_key()
switch (cmd[0]) { switch (cmd[0]) {
case 'q': case 'q':
if (cmd[1] == '!') { if (cmd[1] == '!') {
write(STDOUT_FILENO, "\x1b[2J", 4);
write(STDOUT_FILENO, "\x1b[H", 3);
exit(0); exit(0);
break; break;
} else { } else {
@ -182,8 +179,7 @@ void process_key()
vip.mode = NORMAL; vip.mode = NORMAL;
return; return;
} }
write(STDOUT_FILENO, "\x1b[2J", 4);
write(STDOUT_FILENO, "\x1b[H", 3);
exit(0); exit(0);
break; break;

View file

@ -168,24 +168,24 @@ char *syntax_to_color(int hl, size_t *len)
return strdup(YELLOW_BG); return strdup(YELLOW_BG);
case MATCH:; case MATCH:;
char *str = malloc(COLOR_LEN * 2 + 1); char *str = malloc(COLOR_LEN * 2 + 1);
snprintf(str, COLOR_LEN * 2 + 1, "%s%s", BLACK_BG, SKY_FG); snprintf(str, COLOR_LEN * 2 + 1, "%s%s", BLACK_BG, SKY_FG);
*len = COLOR_LEN * 2; *len = COLOR_LEN * 2;
return str; return str;
case RESET:; case RESET:;
char *res = malloc(COLOR_LEN * 2 + 1); char *res = malloc(COLOR_LEN * 2 + 1);
snprintf(res, COLOR_LEN * 2 + 1, "%s%s", WHITE_BG, BLACK_FG); snprintf(res, COLOR_LEN * 2 + 1, "%s%s", WHITE_BG, BLACK_FG);
*len = COLOR_LEN * 2; *len = COLOR_LEN * 2;
return res; return res;
default: default:
*len = COLOR_LEN; *len = COLOR_LEN;
return strdup(WHITE_BG); return strdup(WHITE_BG);
} }
} }
void select_syntax_highlight() void select_syntax_highlight(void)
{ {
vip.syntax = NULL; vip.syntax = NULL;
if (vip.filename == NULL) return; if (vip.filename == NULL) return;

View file

@ -10,24 +10,25 @@ extern editor vip;
void die(const char *s) void die(const char *s)
{ {
write(STDOUT_FILENO, "\x1b[2J", 4); write(STDOUT_FILENO, "\x1b[2J\x1b[H", 7);
write(STDOUT_FILENO, "\x1b[H", 3);
perror(s); perror(s);
exit(1); exit(1);
} }
void reset_term() void reset_term(void)
{ {
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &vip.termios) == -1) { if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &vip.termios) == -1) {
die("tcsetattr"); die("tcsetattr");
} }
write(STDOUT_FILENO, "\x1b[2J\x1b[?1049l", 12);
} }
/* /*
* Setup terminal * Setup terminal
*/ */
void setup_term() void setup_term(void)
{ {
write(STDOUT_FILENO, "\x1b[?1049h\x1b[2J", 12);
if (tcgetattr(STDIN_FILENO, &vip.termios) == -1) { if (tcgetattr(STDIN_FILENO, &vip.termios) == -1) {
die("tcgetattr"); die("tcgetattr");
} }