diff --git a/ChangeLog b/ChangeLog index 31317edcf593a3c42848b9a328b8236f2637f8ab..01b121ceb2906aeeb295fac92ea1d3d336e7944b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -8,6 +8,9 @@ CVS code - New functions nano.c:make_new_opennode(), free_openfilestruct(), delete_opennode(), unlink_opennode(), splice_opennode(), new struct openfilestruct in nano.h. + - Preliminary prepend code. This may be a bad idea, but I've + been wanting it for awhile now and we'll see how bad it messes + everything up. Changes to files.c:do_writeout(), write_file(). - configure.ac: - Define NDEBUG to silence asserts (David Benbennick). - files.c: diff --git a/files.c b/files.c index 98f8bceb7c0e8a38753781acc07b0bfee5b4a279..1617b0d95eb5723d967e4063d9c45e929a6402c0 100644 --- a/files.c +++ b/files.c @@ -1178,8 +1178,8 @@ int check_operating_dir(char *currpath, int allow_tabcomp) * tmp means we are writing a tmp file in a secure fashion. We use * it when spell checking or dumping the file on an error. * - * append means, not surprisingly, whether we are appending instead - * of overwriting. + * append == 1 means we are appending instead of overwriting. + * append == 2 means we are prepending instead of overwriting. * * nonamechange means don't change the current filename, it is ignored * if tmp == 1. @@ -1187,9 +1187,9 @@ int check_operating_dir(char *currpath, int allow_tabcomp) int write_file(char *name, int tmp, int append, int nonamechange) { long size, lineswritten = 0; - char *buf = NULL; + char *buf = NULL, prechar; filestruct *fileptr; - int fd, mask = 0, realexists, anyexists; + int fd, fd2, mask = 0, realexists, anyexists; struct stat st, lst; char *realname = NULL; @@ -1238,7 +1238,8 @@ int write_file(char *name, int tmp, int append, int nonamechange) if (realexists == -1 || tmp || (!ISSET(FOLLOW_SYMLINKS) && S_ISLNK(lst.st_mode))) { to reflect whether or not to link/unlink/rename the file */ - else if (ISSET(FOLLOW_SYMLINKS) || !S_ISLNK(lst.st_mode) || tmp) { + else if (append != 2 && (ISSET(FOLLOW_SYMLINKS) || !S_ISLNK(lst.st_mode) + || tmp)) { /* Use O_EXCL if tmp == 1. This is now copied from joe, because wiggy says so *shrug*. */ if (append) @@ -1343,6 +1344,26 @@ int write_file(char *name, int tmp, int append, int nonamechange) return -1; } + /* if we're prepending, open the real file, and append it here */ + if (append == 2) { + if ((fd = open(buf, O_WRONLY | O_APPEND, (S_IRUSR|S_IWUSR))) == -1) { + statusbar(_("Could not reopen %s: %s"), buf, strerror(errno)); + return -1; + } + if ((fd2 = open(realname, O_RDONLY)) == -1) { + statusbar(_("Could open %s for prepend: %s"), realname, strerror(errno)); + return -1; + } + + while (read(fd2, &prechar, 1) > 0) + write(fd, &prechar, 1); + + close(fd); + close(fd2); + + } + + if (realexists == -1 || tmp || (!ISSET(FOLLOW_SYMLINKS) && S_ISLNK(lst.st_mode))) { @@ -1358,7 +1379,8 @@ int write_file(char *name, int tmp, int append, int nonamechange) /* Use permissions from file we are overwriting. */ mask = st.st_mode; - if (!tmp && (!ISSET(FOLLOW_SYMLINKS) && S_ISLNK(lst.st_mode))) { + if (append == 2 || + (!tmp && (!ISSET(FOLLOW_SYMLINKS) && S_ISLNK(lst.st_mode)))) { if (unlink(realname) == -1) { if (errno != ENOENT) { statusbar(_("Could not open %s for writing: %s"), @@ -1438,7 +1460,10 @@ int do_writeout(char *path, int exiting, int append) /* Be nice to the translation folks */ if (ISSET(MARK_ISSET) && !exiting) { - if (append) + if (append == 2) + i = statusq(1, writefile_list, "", + "%s%s", _("Prepend Selection to File"), formatstr); + else if (append) i = statusq(1, writefile_list, "", "%s%s", _("Append Selection to File"), formatstr); else @@ -1447,7 +1472,10 @@ int do_writeout(char *path, int exiting, int append) } else #endif { - if (append) + if (append == 2) + i = statusq(1, writefile_list, answer, + "%s%s", _("File Name to Prepend"), formatstr); + else if (append) i = statusq(1, writefile_list, answer, "%s%s", _("File Name to Append"), formatstr); else @@ -1481,8 +1509,12 @@ int do_writeout(char *path, int exiting, int append) UNSET(DOS_FILE); TOGGLE(MAC_FILE); return(do_writeout(answer, exiting, append)); - } else if (i == NANO_APPEND_KEY) - return(do_writeout(answer, exiting, 1 - append)); + } else if (i == NANO_PREPEND_KEY) + return(do_writeout(answer, exiting, 2)); + else if (i == NANO_APPEND_KEY && append != 1) + return(do_writeout(answer, exiting, 1)); + else if (i == NANO_APPEND_KEY) + return(do_writeout(answer, exiting, 0)); #ifdef DEBUG fprintf(stderr, _("filename is %s"), answer); diff --git a/global.c b/global.c index d79d8c14a888db8c5b38fecf6dcd94f125da16d4..f1613526e84a4b595e7739b7f838e5ddc764d295 100644 --- a/global.c +++ b/global.c @@ -272,7 +272,7 @@ void shortcut_init(int unjustify) "", *nano_backspace_msg = "", *nano_tab_msg = "", *nano_enter_msg = "", *nano_cancel_msg = "", *nano_unjustify_msg = "", *nano_append_msg = - ""; + "", *nano_prepend_msg = ""; #ifdef ENABLE_MULTIBUFFER char *nano_openprev_msg = "", *nano_opennext_msg = ""; @@ -330,6 +330,7 @@ void shortcut_init(int unjustify) nano_gotodir_msg = _("Goto Directory"); nano_cancel_msg = _("Cancel the current function"); nano_append_msg = _("Append to the current file"); + nano_prepend_msg = _("Prepend to the current file"); nano_reverse_msg = _("Search backwards"); nano_dos_msg = _("Write file out in DOS format"); nano_mac_msg = _("Write file out in Mac format"); @@ -632,6 +633,10 @@ void shortcut_init(int unjustify) NANO_APPEND_KEY, _("Append"), nano_append_msg, 0, 0, 0, NOVIEW, 0); + sc_init_one(&writefile_list, + NANO_PREPEND_KEY, _("Prepend"), + nano_prepend_msg, 0, 0, 0, NOVIEW, 0); + sc_init_one(&writefile_list, NANO_CANCEL_KEY, _("Cancel"), nano_cancel_msg, 0, 0, 0, VIEW, 0); diff --git a/nano.h b/nano.h index 5e26440b4f9356a0d9dc221c661c19911cba87ce..dc63563a881ef8619bd5f08de9d301e7d673f7aa 100644 --- a/nano.h +++ b/nano.h @@ -289,6 +289,7 @@ know what you're doing */ #define NANO_FROMSEARCHTOGOTO_KEY NANO_CONTROL_T #define NANO_TOFILES_KEY NANO_CONTROL_T #define NANO_APPEND_KEY NANO_ALT_A +#define NANO_PREPEND_KEY NANO_ALT_P #define NANO_OPENPREV_KEY NANO_ALT_LCARAT #define NANO_OPENNEXT_KEY NANO_ALT_RCARAT #define NANO_OPENPREV_ALTKEY NANO_ALT_COMMA