diff --git a/ChangeLog b/ChangeLog index d65b0d71753169c8179eaa3e71488a634a4b0aa9..f239dd3c312d4d9cdb0489cec6b573a04e647c58 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,8 @@ 2015-12-18 Benno Schulenberg <bensberg@justemail.net> * src/color.c (color_init): Use less #ifdefs, and adjust indentation. * src/color.c (set_colorpairs): Improve comments and rename vars. + * src/files.c (read_line): Chop a superfluous bool -- 'prevnode' being + NULL is enough indication that the first line is being read. 2015-12-11 Benno Schulenberg <bensberg@justemail.net> * doc/syntax/Makefile.am: Add missing autoconf and nftables syntaxes. diff --git a/src/files.c b/src/files.c index 01fbc173a7c8bf2a5f9d605e39e2a01c8554b120..a3a7471e8b2e48b141f6d84677d0332b7b21e753 100644 --- a/src/files.c +++ b/src/files.c @@ -585,12 +585,9 @@ int is_file_writable(const char *filename) return ans; } -/* We make a new line of text from buf. buf is length buf_len. If - * first_line_ins is TRUE, then we put the new line at the top of the - * file. Otherwise, we assume prevnode is the last line of the file, - * and put our line after prevnode. */ -filestruct *read_line(char *buf, filestruct *prevnode, bool - *first_line_ins, size_t buf_len) +/* Make a new line of text from the given buf, which is of length buf_len. + * Then attach this line after prevnode. */ +filestruct *read_line(char *buf, size_t buf_len, filestruct *prevnode) { filestruct *fileptr = (filestruct *)nmalloc(sizeof(filestruct)); @@ -612,20 +609,17 @@ filestruct *read_line(char *buf, filestruct *prevnode, bool fileptr->multidata = NULL; #endif - if (*first_line_ins) { + fileptr->prev = prevnode; + + if (prevnode == NULL) { /* Special case: we're inserting into the first line. */ - fileptr->prev = NULL; fileptr->next = openfile->fileage; openfile->fileage = fileptr; fileptr->lineno = 1; /* Make sure that our edit window stays on the first line. */ openfile->edittop = fileptr; - *first_line_ins = FALSE; } else { - assert(prevnode != NULL); - prevnode->next = fileptr; - fileptr->prev = prevnode; fileptr->next = NULL; fileptr->lineno = prevnode->lineno + 1; } @@ -652,10 +646,8 @@ void read_file(FILE *f, int fd, const char *filename, bool undoable, bool checkw /* The current input character. */ char *buf; /* The buffer where we store chunks of the file. */ - filestruct *fileptr = openfile->current; - /* The current line of the file. */ - bool first_line_ins = FALSE; - /* Whether we're inserting with the cursor on the first line. */ + filestruct *fileptr = openfile->current->prev; + /* The line after which to start inserting. */ int input_int; /* The current value we read from the file, whether an input * character or EOF. */ @@ -676,11 +668,6 @@ void read_file(FILE *f, int fd, const char *filename, bool undoable, bool checkw add_undo(INSERT); #endif - if (openfile->current == openfile->fileage) - first_line_ins = TRUE; - else - fileptr = openfile->current->prev; - /* Read the entire file into the filestruct. */ while ((input_int = getc(f)) != EOF) { input = (char)input_int; @@ -701,7 +688,7 @@ void read_file(FILE *f, int fd, const char *filename, bool undoable, bool checkw #endif /* Read in the line properly. */ - fileptr = read_line(buf, fileptr, &first_line_ins, len); + fileptr = read_line(buf, len, fileptr); /* Reset the line length in preparation for the next line. */ len = 0; @@ -722,7 +709,7 @@ void read_file(FILE *f, int fd, const char *filename, bool undoable, bool checkw format += 2; /* Read in the line properly. */ - fileptr = read_line(buf, fileptr, &first_line_ins, len); + fileptr = read_line(buf, len, fileptr); /* Reset the line length in preparation for the next line. * Since we've already read in the next character, reset it @@ -788,7 +775,7 @@ void read_file(FILE *f, int fd, const char *filename, bool undoable, bool checkw #endif /* Read in the last line properly. */ - fileptr = read_line(buf, fileptr, &first_line_ins, len); + fileptr = read_line(buf, len, fileptr); num_lines++; } diff --git a/src/proto.h b/src/proto.h index 01d464e592b9e49bd4fdf94db8f042659b4028a8..2baa86880d2a429fc983fdde71eea291bda0da5f 100644 --- a/src/proto.h +++ b/src/proto.h @@ -291,8 +291,7 @@ void switch_to_prev_buffer_void(void); void switch_to_next_buffer_void(void); bool close_buffer(bool quiet); #endif -filestruct *read_line(char *buf, filestruct *prevnode, bool - *first_line_ins, size_t buf_len); +filestruct *read_line(char *buf, size_t buf_len, filestruct *prevnode); void read_file(FILE *f, int fd, const char *filename, bool undoable, bool checkwritable); int open_file(const char *filename, bool newfie, bool quiet, FILE **f); char *get_next_filename(const char *name, const char *suffix);