Compare commits

..

No commits in common. "8573bc1c5c79ba98a7ce04a3b16d4dd57f14a7b8" and "cf581afba23c239520a53bc4036a0b0a2c44b7eb" have entirely different histories.

9 changed files with 52 additions and 45 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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