Skip to content

Commit 050d51d

Browse files
committed
Fixed a bug that arose during the last bit of clean-up. Now should run well as a beta on Linux and OPBSD systems at least
1 parent 5c1bbae commit 050d51d

6 files changed

Lines changed: 35 additions & 26 deletions

File tree

edit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void content_line_print(int, struct Window *, struct Cursor *); //Print line at
8989
void print_contents(struct Window *, struct Cursor *, struct File *); //Prints the inner contents of the editor
9090
void dialog(struct Window *, char *, char *);
9191
bool msg_box(struct Window *, char *);
92-
void run_mode(int, struct Window *, struct Cursor *, struct File *);
92+
void run_mode(int *, struct Window *, struct Cursor *, struct File *);
9393

9494
//text.c
9595
int get_line_number_pos(int, char *); //Gets the position of the line number 'line' in text 'text'

file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void save(struct Window *w, struct File *f)
5555
{
5656
f->fp = fopen(f->path, "w");
5757

58-
if (!f->ro)
58+
if (f->fp)
5959
{
6060
fputs(w->contents, f->fp);
6161
fclose(f->fp);

input.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
void repos_x(struct Window *w, struct Cursor *c)
2626
/*When the cursor's x-position is greater than the
2727
*length of the given line, move the cursor back
28-
*to the end of the given line*/
28+
*to the end of the given line
29+
*/
2930
{
3031
int max, pos=get_line_number_pos(c->y, w->contents);
3132
max=get_end_of_line(pos, w->contents);
@@ -40,7 +41,8 @@ void repos_x(struct Window *w, struct Cursor *c)
4041

4142
void key_up(struct Window *w, struct Cursor *c)
4243
/*When in edit-mode and the up arrow key is pressed,
43-
*do this*/
44+
*do this
45+
*/
4446
{
4547
if (c->y>0)
4648
{
@@ -55,7 +57,8 @@ void key_up(struct Window *w, struct Cursor *c)
5557

5658
void key_down(struct Window *w, struct Cursor *c)
5759
/*When in edit-mode and the down arrow key is pressed,
58-
*do this*/
60+
*do this
61+
*/
5962
{
6063
if (get_line_number_pos(c->y+1, w->contents)!=EOF)
6164
{
@@ -70,7 +73,8 @@ void key_down(struct Window *w, struct Cursor *c)
7073

7174
void key_right(struct Window *w, struct Cursor *c)
7275
/*When in edit-mode and the right arrow key is pressed,
73-
*do this*/
76+
*do this
77+
*/
7478
{
7579
if (w->contents[c->abs]!='\n'&&w->contents[c->abs]!='\0')
7680
{
@@ -86,7 +90,8 @@ void key_right(struct Window *w, struct Cursor *c)
8690

8791
void key_left(struct Window *w, struct Cursor *c)
8892
/*When in edit-mode and the left arrow key is pressed,
89-
*do this*/
93+
*do this
94+
*/
9095
{
9196
int i = get_line_number_pos(c->y, w->contents);
9297
i += c->x;
@@ -108,7 +113,8 @@ void key_left(struct Window *w, struct Cursor *c)
108113

109114
void get_input(struct Window *w, struct Cursor *c, struct File *f)
110115
/*When in edit-mode and getting user input,
111-
*do this*/
116+
*do this
117+
*/
112118
{
113119
int k;
114120
switch(k = getch())
@@ -183,7 +189,7 @@ int dialog_input(struct Window *w, char *str)
183189
printw(str);
184190
switch(k = getch())
185191
{
186-
case 10:
192+
case '\n':
187193
str[i]='\0';
188194
return 0;
189195
break;

main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ int main(int argc, char *argv[])
4242

4343
struct File *f = malloc(sizeof(struct File));
4444
strncpy(f->path, "", PATH_MAX);
45+
strncpy(f->filename, "", NAME_MAX);
4546
f->ro=true;
4647
f->saved=true;
4748

@@ -56,7 +57,7 @@ int main(int argc, char *argv[])
5657
while(1) //Run until user quits application
5758
{
5859
getmaxyx(stdscr, w->height, w->width); //Get the dimensions of current screen (stdscr) and place those values into the Window structure
59-
run_mode(mode, w, c, f); //Print to the console according to our mode of operation
60+
run_mode(&mode, w, c, f); //Print to the console according to our mode of operation
6061
refresh();
6162
clear();
6263
}

print.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ void initialize_editor()
3737
init_pair(BOUNDARY_SCHEME, COLOR_BLACK, COLOR_CYAN); //Initialize color pair of black text on a cyan background (for top and bottom boundaries)
3838
keypad(stdscr, true);
3939
cbreak();
40-
//raw();
41-
//nonl();
4240
curs_set(1);
4341
}
4442

@@ -134,13 +132,12 @@ void dialog(struct Window *w, char *prompt, char *msg)
134132
move(w->height-1, strlen(prompt));
135133
}
136134

137-
void run_mode(int m, struct Window *w, struct Cursor *c, struct File *f)
135+
void run_mode(int *m, struct Window *w, struct Cursor *c, struct File *f)
138136
/*Prints to the console the coresponding output
139137
*based upon the current given mode of operation
140138
*/
141139
{
142-
int rep;
143-
switch (m)
140+
switch (*m)
144141
{
145142
case EDIT_MODE:
146143
dialog(w, "[Ctrl+c] Quit [F1] Save [F2] Save as [F3] Open [F4] New [F5] Delete line", "Unnamed Text Editor");
@@ -151,17 +148,17 @@ void run_mode(int m, struct Window *w, struct Cursor *c, struct File *f)
151148
if(msg_box(w, "File not saved! Do you want to continue?"))
152149
{
153150
strcpy(f->path, "");
154-
m=svdmd;
151+
*m=svdmd;
155152
}
156153
else
157-
m=EDIT_MODE;
154+
*m=EDIT_MODE;
158155
break;
159156
case OPEN_FILE:
160157
dialog(w, "File path:", "Open File");
161158
if(!dialog_input(w,f->path))
162159
{
163160
open_file(w,f);
164-
m=EDIT_MODE;
161+
*m=EDIT_MODE;
165162
}
166163
break;
167164
case NEW_FILE:
@@ -175,23 +172,23 @@ void run_mode(int m, struct Window *w, struct Cursor *c, struct File *f)
175172
f->ro=false;
176173
fclose(f->fp);
177174
}
178-
m=EDIT_MODE;
175+
*m=EDIT_MODE;
179176
}
180177
break;
181178
case DEL_LINE:
182179
dialog(w, "Line number:", "Delete Line");
183180
if(!dialog_input(w,ln))
184181
{
185182
del_line(atoi(ln)-1, w->contents);
186-
m=EDIT_MODE;
183+
*m=EDIT_MODE;
187184
}
188185
break;
189186
case SAVE_AS:
190187
dialog(w, "File path:", "Save As");
191188
if(!dialog_input(w,f->path))
192189
{
193190
save(w, f);
194-
m=EDIT_MODE;
191+
*m=EDIT_MODE;
195192
}
196193
break;
197194
}

text.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424

2525
int get_line_number_pos(int line, char *text)
2626
/*Returns the position of the line-nth line
27-
*in a character array 'text'*/
27+
*in a character array 'text'
28+
*/
2829
{
2930
long long i, j;
3031

@@ -51,7 +52,8 @@ int get_end_of_line(int start, char *text)
5152
*this function returns the position of the end
5253
*of the given line at that position, whether the
5354
*line is the end of the array (aka '\0') or the
54-
*start of a new line (aka '\n')*/
55+
*start of a new line (aka '\n')
56+
*/
5557
{
5658
int i;
5759
for (i = start; text[i]!='\0' && text[i]!='\n'; i++);
@@ -62,7 +64,8 @@ unsigned long long array_size(int k)
6264
/*Base-2 dynamic array size function. It takes a length k
6365
*and finds the closest base of two that is equal to or
6466
*larger than the given k value - used to determine malloc()
65-
*sizes for text-content in a dynamic setting*/
67+
*sizes for text-content in a dynamic setting
68+
*/
6669
{
6770
unsigned len = (unsigned)floor(log(k)/log(2));
6871
return pow(2,len+1);
@@ -72,13 +75,15 @@ void ins_char(int pos, char k, char *str)
7275
/*The basic functionality of most of the editor
7376
*relies on this function. It is simply used to
7477
*insert a character into the text from the given
75-
*cursor position.*/
78+
*cursor position.
79+
*/
7680
{
7781
if(strlen(str)==array_size(strlen(str)))
7882
/*If we have reached the end of the dynamic array,
7983
*we make a copy of its contents and then resize
8084
*the original to be double the previous size, copy
81-
*the contents back in.*/
85+
*the contents back in.
86+
*/
8287
{
8388
char *resize = (char*) calloc(1, strlen(str));
8489
strcpy(resize, str);

0 commit comments

Comments
 (0)