From 798976ab19e6f8d98c3743ed44881daf437c4aae Mon Sep 17 00:00:00 2001 From: faissaloo Date: Sat, 30 Mar 2019 14:25:13 +0000 Subject: [PATCH] Add autosave Signed-off-by: faissaloo --- configure.ac | 11 +++++++++++ doc/nanorc.5 | 3 +++ src/cut.c | 6 +++--- src/files.c | 32 ++++++++++++++++++++++---------- src/global.c | 9 +++++++++ src/nano.c | 21 +++++++++++++++++++-- src/nano.h | 1 + src/proto.h | 3 ++- src/rcfile.c | 3 +++ src/search.c | 2 +- src/text.c | 14 +++++++------- 11 files changed, 81 insertions(+), 24 deletions(-) diff --git a/configure.ac b/configure.ac index 78923598..10019306 100644 --- a/configure.ac +++ b/configure.ac @@ -207,6 +207,17 @@ if test "x$enable_linenumbers" != xno; then AC_DEFINE(ENABLE_LINENUMBERS, 1, [Define this to enable line numbering.]) fi +AC_ARG_ENABLE(autosave, +AS_HELP_STRING([--disable-autosave], [Disable line autosave])) +if test "x$enable_tiny" = xyes; then + if test "x$enable_autosave" != xyes; then + enable_autosave=no + fi +fi +if test "x$enable_autosave" != xno; then + AC_DEFINE(ENABLE_AUTOSAVE, 1, [Define this to enable autosave.]) +fi + AC_ARG_ENABLE(mouse, AS_HELP_STRING([--disable-mouse], [Disable mouse support])) if test "x$enable_tiny" = xyes; then diff --git a/doc/nanorc.5 b/doc/nanorc.5 index eed6b937..e4f49a85 100644 --- a/doc/nanorc.5 +++ b/doc/nanorc.5 @@ -151,6 +151,9 @@ See \fBset titlecolor\fR for more details. .B set linenumbers Display line numbers to the left of the text area. .TP +.B set autosave +Write all changes to the file as they happen. +.TP .B set locking Enable vim-style lock-files for when editing files. .TP diff --git a/src/cut.c b/src/cut.c index dc7b531a..1d4b7d92 100644 --- a/src/cut.c +++ b/src/cut.c @@ -111,7 +111,7 @@ void do_deletion(undo_type action) refresh_needed = TRUE; #endif - set_modified(); + on_modified(); } /* Delete the character under the cursor. */ @@ -343,7 +343,7 @@ void do_cut_text(bool copy_text, bool marked, bool cut_till_eof, bool append) } else #endif /* !NANO_TINY */ - set_modified(); + on_modified(); refresh_needed = TRUE; } @@ -504,6 +504,6 @@ void do_uncut_text(void) /* Set the desired x position to where the pasted text ends. */ openfile->placewewant = xplustabs(); - set_modified(); + on_modified(); refresh_needed = TRUE; } diff --git a/src/files.c b/src/files.c index 84e3f684..ee4be594 100644 --- a/src/files.c +++ b/src/files.c @@ -129,6 +129,18 @@ void initialize_buffer_text(void) openfile->totsize = 0; } +/* Performs any actions needed after every modification */ +void on_modified(void) +{ + set_modified(); +#ifdef ENABLE_AUTOSAVE + /*Don't autosave if the user hasn't given a filename yet*/ + if (ISSET(AUTOSAVE) && *openfile->filename != '\0') { + do_writeout(TRUE, FALSE); + } +#endif +} + /* Mark the current file as modified if it isn't already, and then * update the titlebar to display the file's new status. */ void set_modified(void) @@ -1205,7 +1217,7 @@ void do_insertfile(void) openfile->current_x = 0; openfile->placewewant = 0; - set_modified(); + on_modified(); } #endif } else @@ -1238,7 +1250,7 @@ void do_insertfile(void) /* If the file actually changed, mark it as modified. */ if (openfile->current->lineno != was_current_lineno || openfile->current_x != was_current_x) - set_modified(); + on_modified(); #ifndef NANO_TINY /* Ensure that the buffer retains the format that it had. */ openfile->fmt = original_fmt; @@ -2073,12 +2085,12 @@ bool write_marked_file(const char *name, FILE *f_open, bool tmp, #endif /* !NANO_TINY */ /* Write the current file to disk. If the mark is on, write the current - * marked selection to disk. If exiting is TRUE, write the entire file + * marked selection to disk. If all is TRUE, write the entire file * to disk regardless of whether the mark is on. Do not ask for a name * when withprompt is FALSE nor when the TEMP_FILE flag is set and the * file already has a name. Return 0 on error, 1 on success, and 2 when * the buffer is to be discarded. */ -int do_writeout(bool exiting, bool withprompt) +int do_writeout(bool all, bool withprompt) { bool result = FALSE; kind_of_writing_type method = OVERWRITE; @@ -2095,7 +2107,7 @@ int do_writeout(bool exiting, bool withprompt) given = mallocstrcpy(NULL, #ifndef NANO_TINY - (openfile->mark && !exiting) ? "" : + (openfile->mark && !all) ? "" : #endif openfile->filename); @@ -2113,7 +2125,7 @@ int do_writeout(bool exiting, bool withprompt) /* When the mark is on, offer to write the selection to disk, but * not when in restricted mode, because it would allow writing to * a file not specified on the command line. */ - if (openfile->mark && !exiting && !ISSET(RESTRICTED)) + if (openfile->mark && !all && !ISSET(RESTRICTED)) /* TRANSLATORS: The next six strings are prompts. */ msg = (method == PREPEND) ? _("Prepend Selection to File") : (method == APPEND) ? _("Append Selection to File") : @@ -2128,7 +2140,7 @@ int do_writeout(bool exiting, bool withprompt) present_path = mallocstrcpy(present_path, "./"); /* When we shouldn't prompt, use the existing filename. */ - if ((!withprompt || (ISSET(TEMP_FILE) && exiting)) && + if ((!withprompt || (ISSET(TEMP_FILE) && all)) && openfile->filename[0] != '\0') answer = mallocstrcpy(answer, openfile->filename); else { @@ -2199,7 +2211,7 @@ int do_writeout(bool exiting, bool withprompt) * "zzy" as the filename to save under (hence "xyzzy"), and * this is the first time we've done this, show an Easter * egg. Display the credits. */ - if (!did_credits && exiting && !ISSET(TEMP_FILE) && + if (!did_credits && all && !ISSET(TEMP_FILE) && strcasecmp(answer, "zzy") == 0) { if (LINES > 5 && COLS > 31) { do_credits(); @@ -2243,7 +2255,7 @@ int do_writeout(bool exiting, bool withprompt) if (!maychange) { #ifndef NANO_TINY - if (exiting || !openfile->mark) + if (all || !openfile->mark) #endif { if (do_yesno_prompt(FALSE, _("Save file under " @@ -2310,7 +2322,7 @@ int do_writeout(bool exiting, bool withprompt) * function is disabled, since it allows reading from or * writing to files not specified on the command line. */ #ifndef NANO_TINY - if (openfile->mark && !exiting && withprompt && !ISSET(RESTRICTED)) + if (openfile->mark && !all && withprompt && !ISSET(RESTRICTED)) result = write_marked_file(answer, NULL, FALSE, method); else #endif diff --git a/src/global.c b/src/global.c index 3dd07882..6ac40bc2 100644 --- a/src/global.c +++ b/src/global.c @@ -1260,6 +1260,9 @@ void shortcut_init(void) add_to_sclist(MMAIN, "M-$", 0, do_toggle_void, SOFTWRAP); #ifdef ENABLE_LINENUMBERS add_to_sclist(MMAIN, "M-#", 0, do_toggle_void, LINE_NUMBERS); +#endif +#ifdef ENABLE_AUTOSAVE + add_to_sclist(MMAIN, "M-!", 0, do_toggle_void, AUTOSAVE); #endif add_to_sclist(MMAIN, "M-P", 0, do_toggle_void, WHITESPACE_DISPLAY); #ifdef ENABLE_COLOR @@ -1432,6 +1435,8 @@ const char *flagtostr(int flag) return N_("Suspension"); case LINE_NUMBERS: return N_("Line numbering"); + case AUTOSAVE: + return N_("Autosave"); default: return "Bad toggle -- please report a bug"; } @@ -1668,6 +1673,10 @@ sc *strtosc(const char *input) #ifdef ENABLE_LINENUMBERS else if (!strcasecmp(input, "linenumbers")) s->toggle = LINE_NUMBERS; +#endif +#ifdef ENABLE_AUTOSAVE + else if (!strcasecmp(input, "autosave")) + s->toggle = AUTOSAVE; #endif else if (!strcasecmp(input, "whitespacedisplay")) s->toggle = WHITESPACE_DISPLAY; diff --git a/src/nano.c b/src/nano.c index 47a7fe0e..2f384167 100644 --- a/src/nano.c +++ b/src/nano.c @@ -869,6 +869,9 @@ void usage(void) #ifdef ENABLE_LINENUMBERS print_opt("-l", "--linenumbers", N_("Show line numbers in front of the text")); #endif +#ifdef ENABLE_AUTOSAVE + print_opt("-!", "--autosave", N_("Write to the file on every change")); +#endif #ifdef ENABLE_MOUSE print_opt("-m", "--mouse", N_("Enable the use of the mouse")); #endif @@ -947,6 +950,9 @@ void version(void) #ifdef ENABLE_LINENUMBERS printf(" --enable-linenumbers"); #endif +#ifdef ENABLE_AUTOSAVE + printf(" --enable-autosave"); +#endif #ifdef ENABLE_MOUSE printf(" --enable-mouse"); #endif @@ -996,6 +1002,9 @@ void version(void) #ifndef ENABLE_LINENUMBERS printf(" --disable-linenumbers"); #endif +#ifndef ENABLE_AUTOSAVE + printf(" --disable-autosave"); +#endif #ifndef ENABLE_MOUSE printf(" --disable-mouse"); #endif @@ -1167,7 +1176,7 @@ bool scoop_stdin(void) doupdate(); if (!ISSET(VIEW_MODE) && openfile->totsize > 0) - set_modified(); + on_modified(); return TRUE; } @@ -1884,7 +1893,7 @@ void do_output(char *output, size_t output_len, bool allow_cntrls) char_len); current_len += char_len; openfile->totsize++; - set_modified(); + on_modified(); #ifndef NANO_TINY /* Only add a new undo item when the current item is not an ADD or when @@ -2004,6 +2013,9 @@ int main(int argc, char **argv) #ifdef ENABLE_LINENUMBERS {"linenumbers", 0, NULL, 'l'}, #endif +#ifdef ENABLE_AUTOSAVE + {"autosave", 0, NULL, '!'}, +#endif #ifdef ENABLE_MOUSE {"mouse", 0, NULL, 'm'}, #endif @@ -2336,6 +2348,11 @@ int main(int argc, char **argv) case '$': SET(SOFTWRAP); break; +#endif +#ifdef ENABLE_AUTOSAVE + case '!': + SET(AUTOSAVE); + break; #endif default: printf(_("Type '%s -h' for a list of available options.\n"), argv[0]); diff --git a/src/nano.h b/src/nano.h index ae4b23ac..4a5ce01a 100644 --- a/src/nano.h +++ b/src/nano.h @@ -538,6 +538,7 @@ enum TRIM_BLANKS, SHOW_CURSOR, LINE_NUMBERS, + AUTOSAVE, NO_PAUSES, AT_BLANKS, AFTER_ENDS, diff --git a/src/proto.h b/src/proto.h index 9283b86c..176a2376 100644 --- a/src/proto.h +++ b/src/proto.h @@ -264,6 +264,7 @@ void do_uncut_text(void); /* Most functions in files.c. */ void initialize_buffer_text(void); +void on_modified(void); void set_modified(void); bool open_buffer(const char *filename, bool new_buffer); #ifdef ENABLE_SPELLER @@ -301,7 +302,7 @@ bool write_file(const char *name, FILE *f_open, bool tmp, bool write_marked_file(const char *name, FILE *f_open, bool tmp, kind_of_writing_type method); #endif -int do_writeout(bool exiting, bool withprompt); +int do_writeout(bool all, bool withprompt); void do_writeout_void(void); void do_savefile(void); char *real_dir_from_tilde(const char *buf); diff --git a/src/rcfile.c b/src/rcfile.c index 1df7e540..4f4cfcf3 100644 --- a/src/rcfile.c +++ b/src/rcfile.c @@ -57,6 +57,9 @@ static const rcoption rcopts[] = { {"jumpyscrolling", JUMPY_SCROLLING}, #ifdef ENABLE_LINENUMBERS {"linenumbers", LINE_NUMBERS}, +#endif +#ifdef ENABLE_AUTOSAVE + {"autosave", AUTOSAVE}, #endif {"morespace", MORE_SPACE}, /* Deprecated; remove in 2021. */ #ifdef ENABLE_MOUSE diff --git a/src/search.c b/src/search.c index 13c19ca8..db2da9ca 100644 --- a/src/search.c +++ b/src/search.c @@ -663,7 +663,7 @@ ssize_t do_replace_loop(const char *needle, bool whole_word_only, update_line(openfile->current, openfile->current_x); } - set_modified(); + on_modified(); as_an_at = TRUE; numreplaced++; } diff --git a/src/text.c b/src/text.c index 11830084..6f3ce713 100644 --- a/src/text.c +++ b/src/text.c @@ -166,7 +166,7 @@ void do_indent(void) free(indentation); - set_modified(); + on_modified(); refresh_needed = TRUE; shift_held = TRUE; } @@ -261,7 +261,7 @@ void do_unindent(void) free(indentation); } - set_modified(); + on_modified(); refresh_needed = TRUE; shift_held = TRUE; } @@ -426,7 +426,7 @@ void do_comment(void) update_multiline_undo(line->lineno, ""); } - set_modified(); + on_modified(); refresh_needed = TRUE; shift_held = TRUE; } @@ -688,7 +688,7 @@ void do_undo(void) openfile->modified = FALSE; titlebar(NULL); } else - set_modified(); + on_modified(); } /* Redo the last thing(s) we undid. */ @@ -859,7 +859,7 @@ void do_redo(void) openfile->modified = FALSE; titlebar(NULL); } else - set_modified(); + on_modified(); } #endif /* !NANO_TINY */ @@ -926,7 +926,7 @@ void do_enter(void) openfile->placewewant = xplustabs(); openfile->totsize++; - set_modified(); + on_modified(); #ifndef NANO_TINY if (ISSET(AUTOINDENT) && !allblanks) @@ -2278,7 +2278,7 @@ void do_justify(bool full_justify) /* Set the desired screen column (always zero, except at EOF). */ openfile->placewewant = xplustabs(); - set_modified(); + on_modified(); refresh_needed = TRUE; shift_held = TRUE; } -- 2.17.1