Commit 14c8620e authored by Chris Allegretta's avatar Chris Allegretta
Browse files

- Change add_undo and current_undo to only take action arg, as openfilestruct...

- Change add_undo and current_undo to only take action arg, as openfilestruct arg is always openfile
- Add ability to undo a file/cmd insert, needed to add undoable flag to read_file and open_buffer as a result



git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@4291 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
No related merge requests found
Showing with 85 additions and 36 deletions
+85 -36
...@@ -205,7 +205,7 @@ void do_cut_text( ...@@ -205,7 +205,7 @@ void do_cut_text(
if (!old_no_newlines) if (!old_no_newlines)
UNSET(NO_NEWLINES); UNSET(NO_NEWLINES);
} else if (!undoing) } else if (!undoing)
update_undo(CUT, openfile); update_undo(CUT);
#endif #endif
/* Leave the text in the cutbuffer, and mark the file as /* Leave the text in the cutbuffer, and mark the file as
* modified. */ * modified. */
...@@ -224,7 +224,7 @@ void do_cut_text( ...@@ -224,7 +224,7 @@ void do_cut_text(
void do_cut_text_void(void) void do_cut_text_void(void)
{ {
#ifndef NANO_TINY #ifndef NANO_TINY
add_undo(CUT, openfile); add_undo(CUT);
#endif #endif
do_cut_text( do_cut_text(
#ifndef NANO_TINY #ifndef NANO_TINY
...@@ -245,7 +245,7 @@ void do_copy_text(void) ...@@ -245,7 +245,7 @@ void do_copy_text(void)
void do_cut_till_end(void) void do_cut_till_end(void)
{ {
#ifndef NANO_TINY #ifndef NANO_TINY
add_undo(CUTTOEND, openfile); add_undo(CUTTOEND);
#endif #endif
do_cut_text(FALSE, TRUE, FALSE); do_cut_text(FALSE, TRUE, FALSE);
} }
......
...@@ -101,7 +101,7 @@ void initialize_buffer_text(void) ...@@ -101,7 +101,7 @@ void initialize_buffer_text(void)
/* If it's not "", filename is a file to open. We make a new buffer, if /* If it's not "", filename is a file to open. We make a new buffer, if
* necessary, and then open and read the file, if applicable. */ * necessary, and then open and read the file, if applicable. */
void open_buffer(const char *filename) void open_buffer(const char *filename, bool undoable)
{ {
bool new_buffer = (openfile == NULL bool new_buffer = (openfile == NULL
#ifdef ENABLE_MULTIBUFFER #ifdef ENABLE_MULTIBUFFER
...@@ -142,7 +142,7 @@ void open_buffer(const char *filename) ...@@ -142,7 +142,7 @@ void open_buffer(const char *filename)
/* If we have a non-new file, read it in. Then, if the buffer has /* If we have a non-new file, read it in. Then, if the buffer has
* no stat, update the stat, if applicable. */ * no stat, update the stat, if applicable. */
if (rc == 0) { if (rc == 0) {
read_file(f, filename); read_file(f, filename, undoable);
#ifndef NANO_TINY #ifndef NANO_TINY
if (openfile->current_stat == NULL) { if (openfile->current_stat == NULL) {
openfile->current_stat = openfile->current_stat =
...@@ -192,7 +192,7 @@ void replace_buffer(const char *filename) ...@@ -192,7 +192,7 @@ void replace_buffer(const char *filename)
/* If we have a non-new file, read it in. */ /* If we have a non-new file, read it in. */
if (rc == 0) if (rc == 0)
read_file(f, filename); read_file(f, filename, FALSE);
/* Move back to the beginning of the first line of the buffer. */ /* Move back to the beginning of the first line of the buffer. */
openfile->current = openfile->fileage; openfile->current = openfile->fileage;
...@@ -339,8 +339,9 @@ filestruct *read_line(char *buf, filestruct *prevnode, bool ...@@ -339,8 +339,9 @@ filestruct *read_line(char *buf, filestruct *prevnode, bool
} }
/* Read an open file into the current buffer. f should be set to the /* Read an open file into the current buffer. f should be set to the
* open file, and filename should be set to the name of the file. */ * open file, and filename should be set to the name of the file.
void read_file(FILE *f, const char *filename) undoable means do we want to create undo records to try and undo this */
void read_file(FILE *f, const char *filename, bool undoable)
{ {
size_t num_lines = 0; size_t num_lines = 0;
/* The number of lines in the file. */ /* The number of lines in the file. */
...@@ -371,6 +372,11 @@ void read_file(FILE *f, const char *filename) ...@@ -371,6 +372,11 @@ void read_file(FILE *f, const char *filename)
buf = charalloc(bufx); buf = charalloc(bufx);
buf[0] = '\0'; buf[0] = '\0';
#ifndef NANO_TINY
if (undoable)
add_undo(INSERT);
#endif
if (openfile->current == openfile->fileage) if (openfile->current == openfile->fileage)
first_line_ins = TRUE; first_line_ins = TRUE;
else else
...@@ -489,7 +495,7 @@ void read_file(FILE *f, const char *filename) ...@@ -489,7 +495,7 @@ void read_file(FILE *f, const char *filename)
/* If we didn't get a file and we don't already have one, open a /* If we didn't get a file and we don't already have one, open a
* blank buffer. */ * blank buffer. */
if (fileptr == NULL) if (fileptr == NULL)
open_buffer(""); open_buffer("", FALSE);
/* Attach the file we got to the filestruct. If we got a file of /* Attach the file we got to the filestruct. If we got a file of
* zero bytes, don't do anything. */ * zero bytes, don't do anything. */
...@@ -561,6 +567,9 @@ void read_file(FILE *f, const char *filename) ...@@ -561,6 +567,9 @@ void read_file(FILE *f, const char *filename)
openfile->placewewant = xplustabs(); openfile->placewewant = xplustabs();
#ifndef NANO_TINY #ifndef NANO_TINY
if (undoable)
update_undo(INSERT);
if (format == 3) if (format == 3)
statusbar( statusbar(
P_("Read %lu line (Converted from DOS and Mac format)", P_("Read %lu line (Converted from DOS and Mac format)",
...@@ -853,7 +862,7 @@ void do_insertfile( ...@@ -853,7 +862,7 @@ void do_insertfile(
#ifdef ENABLE_MULTIBUFFER #ifdef ENABLE_MULTIBUFFER
if (ISSET(MULTIBUFFER)) if (ISSET(MULTIBUFFER))
/* Open a blank buffer. */ /* Open a blank buffer. */
open_buffer(""); open_buffer("", FALSE);
#endif #endif
/* Save the command's output in the current buffer. */ /* Save the command's output in the current buffer. */
...@@ -877,7 +886,7 @@ void do_insertfile( ...@@ -877,7 +886,7 @@ void do_insertfile(
/* Save the file specified in answer in the current /* Save the file specified in answer in the current
* buffer. */ * buffer. */
open_buffer(answer); open_buffer(answer, TRUE);
#ifndef NANO_TINY #ifndef NANO_TINY
} }
#endif #endif
......
...@@ -1647,7 +1647,7 @@ void do_output(char *output, size_t output_len, bool allow_cntrls) ...@@ -1647,7 +1647,7 @@ void do_output(char *output, size_t output_len, bool allow_cntrls)
set_modified(); set_modified();
#ifndef NANO_TINY #ifndef NANO_TINY
update_undo(ADD, openfile); update_undo(ADD);
/* Note that current_x has not yet been incremented. */ /* Note that current_x has not yet been incremented. */
...@@ -2232,7 +2232,7 @@ int main(int argc, char **argv) ...@@ -2232,7 +2232,7 @@ int main(int argc, char **argv)
icol == 1) icol == 1)
parse_line_column(&argv[i][1], &iline, &icol); parse_line_column(&argv[i][1], &iline, &icol);
else { else {
open_buffer(argv[i]); open_buffer(argv[i], FALSE);
if (iline > 1 || icol > 1) { if (iline > 1 || icol > 1) {
do_gotolinecolumn(iline, icol, FALSE, FALSE, FALSE, do_gotolinecolumn(iline, icol, FALSE, FALSE, FALSE,
...@@ -2249,14 +2249,14 @@ int main(int argc, char **argv) ...@@ -2249,14 +2249,14 @@ int main(int argc, char **argv)
* buffer or a new buffer, depending on whether multibuffer mode is * buffer or a new buffer, depending on whether multibuffer mode is
* enabled. */ * enabled. */
if (optind < argc) if (optind < argc)
open_buffer(argv[optind]); open_buffer(argv[optind], FALSE);
/* We didn't open any files if all the command line arguments were /* We didn't open any files if all the command line arguments were
* invalid files like directories or if there were no command line * invalid files like directories or if there were no command line
* arguments given. In this case, we have to load a blank buffer. * arguments given. In this case, we have to load a blank buffer.
* Also, we unset view mode to allow editing. */ * Also, we unset view mode to allow editing. */
if (openfile == NULL) { if (openfile == NULL) {
open_buffer(""); open_buffer("", FALSE);
UNSET(VIEW_MODE); UNSET(VIEW_MODE);
} }
......
...@@ -170,7 +170,7 @@ typedef enum { ...@@ -170,7 +170,7 @@ typedef enum {
} function_type; } function_type;
typedef enum { typedef enum {
ADD, DEL, REPLACE, SPLIT, UNSPLIT, CUT, CUTTOEND, UNCUT, OTHER ADD, DEL, REPLACE, SPLIT, UNSPLIT, CUT, CUTTOEND, UNCUT, INSERT, OTHER
} undo_type; } undo_type;
/* Structure types. */ /* Structure types. */
......
...@@ -255,7 +255,7 @@ void do_uncut_text(void); ...@@ -255,7 +255,7 @@ void do_uncut_text(void);
void make_new_buffer(void); void make_new_buffer(void);
void initialize_buffer(void); void initialize_buffer(void);
void initialize_buffer_text(void); void initialize_buffer_text(void);
void open_buffer(const char *filename); void open_buffer(const char *filename, bool undoable);
#ifndef DISABLE_SPELLER #ifndef DISABLE_SPELLER
void replace_buffer(const char *filename); void replace_buffer(const char *filename);
#endif #endif
...@@ -268,7 +268,7 @@ bool close_buffer(void); ...@@ -268,7 +268,7 @@ bool close_buffer(void);
#endif #endif
filestruct *read_line(char *buf, filestruct *prevnode, bool filestruct *read_line(char *buf, filestruct *prevnode, bool
*first_line_ins, size_t buf_len); *first_line_ins, size_t buf_len);
void read_file(FILE *f, const char *filename); void read_file(FILE *f, const char *filename, bool undoable);
int open_file(const char *filename, bool newfie, FILE **f); int open_file(const char *filename, bool newfie, FILE **f);
char *get_next_filename(const char *name, const char *suffix); char *get_next_filename(const char *name, const char *suffix);
void do_insertfile( void do_insertfile(
...@@ -707,10 +707,11 @@ void new_magicline(void); ...@@ -707,10 +707,11 @@ void new_magicline(void);
void remove_magicline(void); void remove_magicline(void);
void mark_order(const filestruct **top, size_t *top_x, const filestruct void mark_order(const filestruct **top, size_t *top_x, const filestruct
**bot, size_t *bot_x, bool *right_side_up); **bot, size_t *bot_x, bool *right_side_up);
void add_undo(undo_type current_action, openfilestruct *fs); void add_undo(undo_type current_action);
void update_undo(undo_type action, openfilestruct *fs); void update_undo(undo_type action);
#endif #endif
size_t get_totsize(const filestruct *begin, const filestruct *end); size_t get_totsize(const filestruct *begin, const filestruct *end);
filestruct *fsfromline(ssize_t lineno);
#ifdef DEBUG #ifdef DEBUG
void dump_filestruct(const filestruct *inptr); void dump_filestruct(const filestruct *inptr);
void dump_filestruct_reverse(void); void dump_filestruct_reverse(void);
......
...@@ -789,7 +789,7 @@ ssize_t do_replace_loop( ...@@ -789,7 +789,7 @@ ssize_t do_replace_loop(
size_t length_change; size_t length_change;
#ifndef NANO_TINY #ifndef NANO_TINY
update_undo(REPLACE, openfile); update_undo(REPLACE);
#endif #endif
if (i == 2) if (i == 2)
replaceall = TRUE; replaceall = TRUE;
......
...@@ -71,7 +71,7 @@ void do_delete(void) ...@@ -71,7 +71,7 @@ void do_delete(void)
* just update_line()? */ * just update_line()? */
#ifndef NANO_TINY #ifndef NANO_TINY
update_undo(DEL, openfile); update_undo(DEL);
#endif #endif
assert(openfile->current != NULL && openfile->current->data != NULL && openfile->current_x <= strlen(openfile->current->data)); assert(openfile->current != NULL && openfile->current->data != NULL && openfile->current_x <= strlen(openfile->current->data));
...@@ -371,6 +371,7 @@ void do_undo(void) ...@@ -371,6 +371,7 @@ void do_undo(void)
filestruct *f = openfile->current, *t; filestruct *f = openfile->current, *t;
int len = 0; int len = 0;
char *undidmsg, *data; char *undidmsg, *data;
filestruct *oldcutbuffer = cutbuffer, *oldcutbottom = cutbottom;
if (!u) { if (!u) {
statusbar(_("Nothing in undo buffer!")); statusbar(_("Nothing in undo buffer!"));
...@@ -458,6 +459,24 @@ void do_undo(void) ...@@ -458,6 +459,24 @@ void do_undo(void)
free_filestruct(cutbuffer); free_filestruct(cutbuffer);
cutbuffer = NULL; cutbuffer = NULL;
break; break;
case INSERT:
undidmsg = _("text insert");
cutbuffer = NULL;
cutbottom = NULL;
/* When we updated mark_begin_lineno in update_undo, it was effectively how many line
were inserted due to being partitioned before read_file was called. So we
add its value here */
openfile->mark_begin = fsfromline(u->lineno + u->mark_begin_lineno - 1);
openfile->mark_begin_x = 0;
openfile->mark_set = TRUE;
do_gotolinecolumn(u->lineno, u->begin+1, FALSE, FALSE, FALSE, FALSE);
cut_marked();
u->cutbuffer = cutbuffer;
u->cutbottom = cutbottom;
cutbuffer = oldcutbuffer;
cutbottom = oldcutbottom;
openfile->mark_set = FALSE;
break;
case REPLACE: case REPLACE:
undidmsg = _("text replace"); undidmsg = _("text replace");
data = u->strdata; data = u->strdata;
...@@ -622,7 +641,7 @@ void do_enter(void) ...@@ -622,7 +641,7 @@ void do_enter(void)
assert(openfile->current != NULL && xopenfile->current->data != NULL); assert(openfile->current != NULL && xopenfile->current->data != NULL);
#ifndef NANO_TINY #ifndef NANO_TINY
update_undo(SPLIT, openfile); update_undo(SPLIT);
/* Do auto-indenting, like the neolithic Turbo Pascal editor. */ /* Do auto-indenting, like the neolithic Turbo Pascal editor. */
...@@ -750,7 +769,7 @@ bool execute_command(const char *command) ...@@ -750,7 +769,7 @@ bool execute_command(const char *command)
if (f == NULL) if (f == NULL)
nperror("fdopen"); nperror("fdopen");
read_file(f, "stdin"); read_file(f, "stdin", TRUE);
if (wait(NULL) == -1) if (wait(NULL) == -1)
nperror("wait"); nperror("wait");
...@@ -767,10 +786,11 @@ bool execute_command(const char *command) ...@@ -767,10 +786,11 @@ bool execute_command(const char *command)
} }
/* Add a new undo struct to the top of the current pile */ /* Add a new undo struct to the top of the current pile */
void add_undo(undo_type current_action, openfilestruct *fs) void add_undo(undo_type current_action)
{ {
undo *u; undo *u;
char *data; char *data;
openfilestruct *fs = openfile;
/* Ugh, if we were called while cutting not-to-end, non-marked and on the same lineno, /* Ugh, if we were called while cutting not-to-end, non-marked and on the same lineno,
we need to abort here */ we need to abort here */
...@@ -831,12 +851,11 @@ void add_undo(undo_type current_action, openfilestruct *fs) ...@@ -831,12 +851,11 @@ void add_undo(undo_type current_action, openfilestruct *fs)
data = mallocstrcpy(NULL, fs->current->next->data); data = mallocstrcpy(NULL, fs->current->next->data);
u->strdata = data; u->strdata = data;
} }
u->begin = fs->current_x;
break; break;
case INSERT:
case SPLIT: case SPLIT:
case REPLACE: case REPLACE:
data = mallocstrcpy(NULL, fs->current->data); data = mallocstrcpy(NULL, fs->current->data);
u->begin = fs->current_x;
u->strdata = data; u->strdata = data;
break; break;
case CUT: case CUT:
...@@ -868,12 +887,12 @@ void add_undo(undo_type current_action, openfilestruct *fs) ...@@ -868,12 +887,12 @@ void add_undo(undo_type current_action, openfilestruct *fs)
instead. The latter functionality just feels instead. The latter functionality just feels
gimmicky and may just be more hassle than gimmicky and may just be more hassle than
it's worth, so it should be axed if needed. */ it's worth, so it should be axed if needed. */
void update_undo(undo_type action, openfilestruct *fs) void update_undo(undo_type action)
{ {
undo *u; undo *u;
char *data; char *data;
int len = 0; int len = 0;
openfilestruct *fs = openfile;
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "action = %d, fs->last_action = %d, openfile->current->lineno = %d", fprintf(stderr, "action = %d, fs->last_action = %d, openfile->current->lineno = %d",
...@@ -887,9 +906,9 @@ void update_undo(undo_type action, openfilestruct *fs) ...@@ -887,9 +906,9 @@ void update_undo(undo_type action, openfilestruct *fs)
/* Change to an add if we're not using the same undo struct /* Change to an add if we're not using the same undo struct
that we should be using */ that we should be using */
if (action != fs->last_action if (action != fs->last_action
|| (action != CUT && action != CUTTOEND || (action != CUT && action != CUTTOEND && action != INSERT
&& openfile->current->lineno != fs->current_undo->lineno)) { && openfile->current->lineno != fs->current_undo->lineno)) {
add_undo(action, fs); add_undo(action);
return; return;
} }
...@@ -919,7 +938,7 @@ void update_undo(undo_type action, openfilestruct *fs) ...@@ -919,7 +938,7 @@ void update_undo(undo_type action, openfilestruct *fs)
if (!u->xflags) if (!u->xflags)
u->xflags = UNDO_DEL_DEL; u->xflags = UNDO_DEL_DEL;
else if (u->xflags != UNDO_DEL_DEL) { else if (u->xflags != UNDO_DEL_DEL) {
add_undo(action, fs); add_undo(action);
return; return;
} }
data = charalloc(len); data = charalloc(len);
...@@ -933,7 +952,7 @@ void update_undo(undo_type action, openfilestruct *fs) ...@@ -933,7 +952,7 @@ void update_undo(undo_type action, openfilestruct *fs)
if (!u->xflags) if (!u->xflags)
u->xflags = UNDO_DEL_BACKSPACE; u->xflags = UNDO_DEL_BACKSPACE;
else if (u->xflags != UNDO_DEL_BACKSPACE) { else if (u->xflags != UNDO_DEL_BACKSPACE) {
add_undo(action, fs); add_undo(action);
return; return;
} }
data = charalloc(len); data = charalloc(len);
...@@ -944,7 +963,7 @@ void update_undo(undo_type action, openfilestruct *fs) ...@@ -944,7 +963,7 @@ void update_undo(undo_type action, openfilestruct *fs)
u->begin--; u->begin--;
} else { } else {
/* They deleted something else on the line */ /* They deleted something else on the line */
add_undo(DEL, fs); add_undo(DEL);
return; return;
} }
#ifdef DEBUG #ifdef DEBUG
...@@ -961,8 +980,10 @@ void update_undo(undo_type action, openfilestruct *fs) ...@@ -961,8 +980,10 @@ void update_undo(undo_type action, openfilestruct *fs)
u->linescut++; u->linescut++;
break; break;
case REPLACE: case REPLACE:
add_undo(action, fs); add_undo(action);
break; break;
case INSERT:
u->mark_begin_lineno = openfile->current->lineno;
case SPLIT: case SPLIT:
case UNSPLIT: case UNSPLIT:
/* These cases are handled by the earlier check for a new line and action */ /* These cases are handled by the earlier check for a new line and action */
...@@ -977,7 +998,7 @@ void update_undo(undo_type action, openfilestruct *fs) ...@@ -977,7 +998,7 @@ void update_undo(undo_type action, openfilestruct *fs)
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "Starting add_undo for new action as it does not match last_action\n"); fprintf(stderr, "Starting add_undo for new action as it does not match last_action\n");
#endif #endif
add_undo(action, openfile); add_undo(action);
} }
fs->last_action = action; fs->last_action = action;
} }
......
...@@ -603,6 +603,24 @@ void dump_filestruct(const filestruct *inptr) ...@@ -603,6 +603,24 @@ void dump_filestruct(const filestruct *inptr)
} }
} }
/* Get back a pointer given a line number in the current openfilestruct */
filestruct *fsfromline(ssize_t lineno)
{
filestruct *f = openfile->current;
if (lineno <= openfile->current->lineno)
for (; f->lineno != lineno && f != openfile->fileage; f = f->prev)
;
else
for (; f->lineno != lineno && f->next != NULL; f = f->next)
if (f->lineno != lineno)
return NULL;
return f;
}
/* Dump the current buffer's filestruct to stderr in reverse. */ /* Dump the current buffer's filestruct to stderr in reverse. */
void dump_filestruct_reverse(void) void dump_filestruct_reverse(void)
{ {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment