Skip to content

Commit 5e08929

Browse files
committed
Fixed some navigation stuff, etc.
1 parent 3cfdbe8 commit 5e08929

5 files changed

Lines changed: 41 additions & 31 deletions

File tree

edit.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@
3636
#define EDITOR_SCHEME 1
3737
#define BOUNDARY_SCHEME 2
3838

39+
//Our different operating modes
3940
#define EDIT_MODE 0
4041
#define NOT_SAVED 1
4142
#define OPEN_FILE 2
4243
#define NEW_FILE 3
43-
#define DEL_LINE 4
44+
#define SAVE_AS 4
45+
#define DEL_LINE 5
4446

4547
//Define our data-structures
4648
struct Window
@@ -88,8 +90,8 @@ void key_left(struct Window *, struct Cursor *);
8890
void ins_char(int, char, char *);
8991
void del_char(struct Window *, struct Cursor *);
9092
void del_line(int, char *);
91-
void dialog(struct Window *, char *);
92-
bool msg_box(struct Window *w, char *msg);
93+
void dialog(struct Window *, char *, char *);
94+
bool msg_box(struct Window *, char *);
9395
int dialog_input(struct Window *, char *);
9496
void open(struct Window *, struct File *);
9597
void save(struct Window *, struct File *);

file.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ void save(struct Window *w, struct File *f)
5959
fclose(f->fp);
6060
f->saved=true;
6161
}
62+
else
63+
mode=SAVE_AS;
6264
}
6365

6466
void new(struct Window *w, struct File *f, struct Cursor *c)

input.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ void repos_x(struct Window *w, struct Cursor *c)
2828
*to the end of the given line*/
2929
{
3030
int max, pos=get_line_number_pos(c->y, w->contents);
31-
max=get_end_of_line(pos, w->contents)-pos;
32-
if (c->x>max)
31+
max=get_end_of_line(pos, w->contents);
32+
if (c->x>max-pos)
3333
{
34-
c->x=max;
35-
c->abs=pos+max;
34+
c->x=max-pos;
35+
c->abs=max;
3636
}
3737
else
3838
c->abs=pos+c->x;
@@ -44,6 +44,8 @@ void key_up(struct Window *w, struct Cursor *c)
4444
{
4545
if (c->y>0)
4646
{
47+
if(c->y==w->top)
48+
w->top--;
4749
c->y--;
4850
repos_x(w, c);
4951
}
@@ -57,6 +59,8 @@ void key_down(struct Window *w, struct Cursor *c)
5759
{
5860
if (get_line_number_pos(c->y+1, w->contents)!=EOF)
5961
{
62+
if(c->y-w->top==w->height-3)
63+
w->top++;
6064
c->y++;
6165
repos_x(w, c);
6266
}
@@ -122,9 +126,10 @@ void get_input(struct Window *w, struct Cursor *c, struct File *f)
122126
key_right(w, c);
123127
break;
124128
case KEY_F(1):
129+
save(w, f);
125130
break;
126131
case KEY_F(2):
127-
save(w, f);
132+
mode=SAVE_AS;
128133
break;
129134
case KEY_F(3):
130135
svdmd=OPEN_FILE;

main.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ int main(int argc, char *argv[])
4646
{
4747
cbreak();
4848
curs_set(1);
49+
getmaxyx(stdscr, w->height, w->width); //Get the dimensions of current screen (stdscr) and place those values into the Window structure
4950

50-
print_editor(w);
5151
switch (mode)
5252
{
5353
case EDIT_MODE:
54+
dialog(w, "[Esc] Quit [F1] Save [F2] Save as [F3] Open [F4] New [F5] Delete line", "Unnamed Text Editor");
5455
print_contents(w, c, f);
5556
get_input(w, c, f);
5657
break;
@@ -64,15 +65,15 @@ int main(int argc, char *argv[])
6465
mode=EDIT_MODE;
6566
break;
6667
case OPEN_FILE:
67-
dialog(w, "File path:");
68+
dialog(w, "File path:", "Open File");
6869
if(!dialog_input(w,f->path))
6970
{
7071
open(w,f);
7172
mode=EDIT_MODE;
7273
}
7374
break;
7475
case NEW_FILE:
75-
dialog(w, "File path:");
76+
dialog(w, "File path:", "New File");
7677
if(!dialog_input(w,f->path))
7778
{
7879
new(w,f,c);
@@ -85,13 +86,21 @@ int main(int argc, char *argv[])
8586
}
8687
break;
8788
case DEL_LINE:
88-
dialog(w, "Line number:");
89+
dialog(w, "Line number:", "Delete Line");
8990
if(!dialog_input(w,ln))
9091
{
9192
del_line(atoi(ln)-1, w->contents);
9293
mode=EDIT_MODE;
9394
}
9495
break;
96+
case SAVE_AS:
97+
dialog(w, "File path:", "Save As");
98+
if(!dialog_input(w,f->path))
99+
{
100+
save(w, f);
101+
mode=EDIT_MODE;
102+
}
103+
break;
95104
}
96105
refresh();
97106
clear();

print.c

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void content_line_print(int r, struct Window *w, struct Cursor *c) //Print line
5757
attrset(COLOR_PAIR(EDITOR_SCHEME)); //Turn on attribute for the boundary color scheme
5858
move(r+1, 0); //Set cursor to the beginning of the line for the y-position "row" -- all being relative to editor bounds
5959

60-
j = get_line_number_pos(r, w->contents);
60+
j = get_line_number_pos(r+w->top, w->contents);
6161
if (r==c->y)
6262
j += (long) c->x-(c->x%w->width)-floor(c->x/w->width);
6363

@@ -72,25 +72,13 @@ void content_line_print(int r, struct Window *w, struct Cursor *c) //Print line
7272
addch('>');
7373
}
7474

75-
void print_editor(struct Window *w)
76-
{
77-
getmaxyx(stdscr, w->height, w->width); //Get the dimensions of current screen (stdscr) and place those values into the Window structure
78-
79-
attrset(COLOR_PAIR(EDITOR_SCHEME)); //Set color pair for editor (black text on white background)
80-
move(0, 0);
81-
for (int i = 0; i < w->height * w->width; i++) printw(" ");
82-
83-
border_line_print(0, "Unnamed Text Editor", w); //Print a Cyan line from the given y position (0, representing the first line)
84-
border_line_print(w->height - 1, "[Esc] Quit [F1] Help [F2] Save [F3] Open [F4] New [F5] Delete line", w); //Print the bottom line
85-
}
86-
8775
void print_contents(struct Window *w, struct Cursor *c, struct File *f)
8876
{
89-
int i=0, pos = get_line_number_pos(w->top, w->contents);
90-
while (pos<w->height-2)
77+
int i=0;
78+
while (i<w->height-2)
9179
{
92-
content_line_print(pos, w, c);
93-
pos++;
80+
content_line_print(i, w, c);
81+
i++;
9482
}
9583
move(c->y-w->top+1, c->x%w->width);
9684
}
@@ -115,9 +103,13 @@ bool msg_box(struct Window *w, char *msg)
115103
}
116104
}
117105

118-
void dialog(struct Window *w, char *prompt)
106+
void dialog(struct Window *w, char *prompt, char *msg)
119107
{
120-
print_editor(w);
108+
attrset(COLOR_PAIR(EDITOR_SCHEME));
109+
move(0, 0);
110+
for (int i = 0; i < w->height * w->width; i++) printw(" ");
111+
112+
border_line_print(0, msg, w);
121113
border_line_print(w->height-1, prompt, w);
122114
move(w->height-1, strlen(prompt));
123115
}

0 commit comments

Comments
 (0)