Skip to content

Commit

Permalink
Declare the empty line prefix at the beginning of the file. Add nicer…
Browse files Browse the repository at this point in the history
… formatting. Do not prefix the first line in a new file with the empty character.

Signed-off-by: Ryan McQuen <[email protected]>
  • Loading branch information
ryanpcmcquen committed Aug 23, 2020
1 parent cf11aa4 commit 55a3ef2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 34 deletions.
2 changes: 2 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
BasedOnStyle: WebKit
3 changes: 3 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vscode:
extensions:
- [email protected]:auCYfQ5pnsZ6Cjf4ZxkAtA==
Binary file modified linux/oz
Binary file not shown.
55 changes: 21 additions & 34 deletions oz.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
#include <unistd.h>

/*** Defines: ***/
#define OZ_VERSION "0.1.2"
#define OZ_VERSION "0.1.3"
#define OZ_TAB_STOP 4
#define OZ_QUIT_TIMES 2

#define CTRL_KEY(k) ((k)&0x1f)

#define EMPTY_LINE_CHAR '?'
#define EMPTY_LINE_STR "?"

enum editorKey {
BACKSPACE = 127,
ARROW_LEFT = 1000,
Expand Down Expand Up @@ -127,7 +130,7 @@ int editorReadKey()
if (read(STDIN_FILENO, &seq[2], 1) != 1) {
return '\x1b';
}
if (seq[2] == '~') {
if (seq[2] == EMPTY_LINE_CHAR) {
switch (seq[1]) {
case '1':
return HOME_KEY;
Expand Down Expand Up @@ -325,14 +328,9 @@ void editorRowInsertChar(erow* row, int at, int c)
if (at < 0 || at > row->size) {
at = row->size;
}
row->chars = realloc(
row->chars,
row->size + 2);
row->chars = realloc(row->chars, row->size + 2);

memmove(
&row->chars[at + 1],
&row->chars[at],
row->size - at + 1);
memmove(&row->chars[at + 1], &row->chars[at], row->size - at + 1);
row->size++;
row->chars[at] = c;
editorUpdateRow(row);
Expand Down Expand Up @@ -607,7 +605,9 @@ void editorDrawRows(struct abuf* ab)
int filerow = y + E.rowoff;
if (filerow >= E.numrows) {

if (E.numrows == 0 && y == E.screenrows / 3) {
if (y == 0) {
// Skip the first line.
} else if (E.numrows == 0 && y == E.screenrows / 3) {
char welcome[80];
int welcomelen = snprintf(welcome, sizeof(welcome),
"OZ editor -- version %s", OZ_VERSION);
Expand All @@ -616,15 +616,15 @@ void editorDrawRows(struct abuf* ab)
}
int padding = (E.screencols - welcomelen) / 2;
if (padding) {
abAppend(ab, "~", 1);
abAppend(ab, EMPTY_LINE_STR, 1);
padding--;
}
while (padding--) {
abAppend(ab, " ", 1);
}
abAppend(ab, welcome, welcomelen);
} else {
abAppend(ab, "~", 1);
abAppend(ab, EMPTY_LINE_STR, 1);
}
} else {
int len = E.row[filerow].rsize - E.coloff;
Expand All @@ -647,19 +647,10 @@ void editorDrawStatusBar(struct abuf* ab)
char status[80];
char rstatus[80];

int len = snprintf(
status,
sizeof(status),
"%.20s - %d lines %s",
E.filename ? E.filename : "[No name]",
E.numrows,
int len = snprintf(status, sizeof(status), "%.20s - %d lines %s",
E.filename ? E.filename : "[No name]", E.numrows,
E.dirty ? "(modified)" : "");
int rlen = snprintf(
rstatus,
sizeof(rstatus),
"%d%d",
E.cy + 1,
E.numrows);
int rlen = snprintf(rstatus, sizeof(rstatus), "%d%d", E.cy + 1, E.numrows);

if (len > E.screencols) {
len = E.screencols;
Expand Down Expand Up @@ -705,7 +696,8 @@ void editorRefreshScreen()
editorDrawMessageBar(&ab);

char buf[32];
snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) + 1, (E.rx - E.coloff) + 1);
snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) + 1,
(E.rx - E.coloff) + 1);
abAppend(&ab, buf, strlen(buf));

abAppend(&ab, "\x1b[?25h", 6);
Expand All @@ -718,11 +710,7 @@ void editorSetStatusMessage(const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vsnprintf(
E.statusmsg,
sizeof(E.statusmsg),
fmt,
ap);
vsnprintf(E.statusmsg, sizeof(E.statusmsg), fmt, ap);
va_end(ap);
E.statusmsg_time = time(NULL);
}
Expand Down Expand Up @@ -828,10 +816,9 @@ void editorProcessKeypress()

case CTRL_KEY('q'):
if (E.dirty && quit_times > 0) {
editorSetStatusMessage(
"WARNING!!! File has unsaved changes. Press Ctrl-Q %d more time%s to quit.",
quit_times,
quit_times > 1 ? "s" : "");
editorSetStatusMessage("WARNING!!! File has unsaved changes. Press "
"Ctrl-Q %d more time%s to quit.",
quit_times, quit_times > 1 ? "s" : "");
quit_times--;
return;
}
Expand Down

0 comments on commit 55a3ef2

Please sign in to comment.