vBulletin Search Engine Optimization
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| Hi, If I understood the comments in the mg code right moving beyond the end of a buffer is a feature. I can live with that. But adding newlines beyond the end of the buffer is not so good. So I started digging further. I removed the recently added function that adds a trailing newline to a file and added new code that checks the number of trailing newlines at the end of the buffer and -- after confirmation -- reduces them to one. This has the further advantage that the buffer is edited and then written in one go to the file. About the style(9): I tried to keep lines within 80 chars, but I agree it doesn't look so nice. style(9) doesn't mention very much about wrap- ping so I had to guess. Index: fileio.c ================================================== ================= RCS file: /cvs/src/usr.bin/mg/fileio.c,v retrieving revision 1.45 diff -u -w -p -r1.45 fileio.c --- fileio.c 1 Feb 2005 16:04:15 -0000 1.45 +++ fileio.c 3 Feb 2005 18:39:37 -0000 @@ -97,8 +97,33 @@ int ffputbuf(BUFFER *bp) { LINE *lp, *lpend; + int emptylines = 0; lpend = bp->b_linep; + + /* XXX should be variable controlled (once we have variables) */ + for (lp = lforw(lpend); lp != lpend; lp = lforw(lp)) + ; + + while (llength(lp = lback(lp)) == 0) + emptylines++; + + if (emptylines == 0) { + if (eyorn("No newline at end of file, add one") == TRUE) { + lnewline_at(lback(lpend), llength(lback(lpend))); + } + } else { + if (emptylines > 1) { + if (eyorn("Trailing newlines at end of file," + " remove them") == TRUE) { + lp = lforw(lp); + for (lp = lforw(lp); lp != lpend; + lp = lforw(lp)) + lfree(lp); + } + } + } + for (lp = lforw(lpend); lp != lpend; lp = lforw(lp)) { if (fwrite(ltext(lp), 1, llength(lp), ffp) != llength(lp)) { ewprintf("Write I/O error"); @@ -107,15 +132,6 @@ ffputbuf(BUFFER *bp) if (lforw(lp) != lpend) /* no implied \n on last line */ putc('\n', ffp); } - /* - * XXX should be variable controlled (once we have variables) - */ - if (llength(lback(lpend)) != 0) { - if (eyorn("No newline at end of file, add one") == TRUE) { - lnewline_at(lback(lpend), llength(lback(lpend))); - putc('\n', ffp); - } - } return (FIOSUC); } # Han |