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: .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
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 INCLUDE = include
$(TARGET): $(SRC) config.h $(TARGET): $(SRC)
$(CC) $(SRC) -o $@ $(CFLAGS) -I$(INCLUDE) -I. $(CC) $(SRC) -o $@ $(CFLAGS) -I$(INCLUDE) -I.
dist: dist:
@ -23,14 +26,15 @@ 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); void refresh_screen();
void move_cursor(int key); void move_cursor(int key);
void scroll(void); void scroll();
void insert_char(int c); void insert_char(int c);
void insert_new_line(void); void insert_new_line();
void shift_new_line(void); void shift_new_line();
void del_char(void); void del_char();
void init_editor(void); void init_editor();
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); void find_editor();
#endif #endif

View file

@ -1,9 +1,9 @@
#ifndef IO_H_ #ifndef IO_H_
#define IO_H_ #define IO_H_
int read_key(void); int read_key();
void save_file(void); void save_file();
void process_key(void); void process_key();
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); void select_syntax_highlight();
#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); void reset_term();
void setup_term(void); void setup_term();
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) void refresh_screen()
{ {
scroll(); scroll();
@ -68,7 +68,7 @@ void move_cursor(int key)
} }
} }
void scroll(void) void scroll()
{ {
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) void insert_new_line()
{ {
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(void)
/* /*
* 'o' in vim * 'o' in vim
*/ */
void shift_new_line(void) void shift_new_line()
{ {
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) void del_char()
{ {
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)
} }
} }
void init_editor(void) void init_editor()
{ {
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) void find_editor()
{ {
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(void) int read_key()
{ {
int nread; int nread;
char c; char c;
@ -65,7 +65,7 @@ int read_key(void)
} }
} }
void save_file(void) void save_file()
{ {
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,8 +94,9 @@ void save_file(void)
set_status_bar_message("Error saving: %s", strerror(errno)); set_status_bar_message("Error saving: %s", strerror(errno));
} }
void process_key(void) void process_key()
{ {
int c = read_key(); int c = read_key();
switch (c) { switch (c) {
case '\r': case '\r':
@ -171,6 +172,8 @@ void process_key(void)
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 {
@ -179,7 +182,8 @@ void process_key(void)
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

@ -185,7 +185,7 @@ char *syntax_to_color(int hl, size_t *len)
} }
} }
void select_syntax_highlight(void) void select_syntax_highlight()
{ {
vip.syntax = NULL; vip.syntax = NULL;
if (vip.filename == NULL) return; if (vip.filename == NULL) return;

View file

@ -10,25 +10,24 @@ extern editor vip;
void die(const char *s) 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); perror(s);
exit(1); exit(1);
} }
void reset_term(void) void reset_term()
{ {
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) void setup_term()
{ {
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");
} }