From f2387fbdf720f85631e59d4c18bd6f14f4bc65eb Mon Sep 17 00:00:00 2001
From: Chris Allegretta <chrisa@asty.org>
Date: Wed, 10 Apr 2002 02:31:20 +0000
Subject: [PATCH] - Change to openfilestruct for multibuffer mode by DLR. New
 functions nano.c:make_new_opennode(), free_openfilestruct(),
 delete_opennode(), unlink_opennode(), splice_opennode(), new struct
 openfilestruct in nano.h

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@1173 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
---
 ChangeLog |   4 +++
 files.c   |  71 +++++++++++++++---------------------
 global.c  |  28 +++++++--------
 nano.c    | 106 ++++++++++++++++++++++++++++++++++++++++++++++--------
 nano.h    |  13 ++++---
 proto.h   |  27 +++++++++++++-
 6 files changed, 173 insertions(+), 76 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index c3ba93ee..a299c14d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -4,6 +4,10 @@ CVS code -
 	- Allow --tiny and --multibuffer to cooperate (who the heck
 	  would want this is beyond me but ;-).  Changes to 
 	  configure.ac, global.c, ,  (David Benbennick).
+	- Change to openfilestruct for multibuffer mode by DLR.
+	  New functions nano.c:make_new_opennode(), free_openfilestruct(), 
+	  delete_opennode(), unlink_opennode(), splice_opennode(),  
+	  new struct openfilestruct in nano.h. 
 - configure.ac:
 	- Define NDEBUG to silence asserts (David Benbennick).
 - files.c:
diff --git a/files.c b/files.c
index 006c2143..98f8bceb 100644
--- a/files.c
+++ b/files.c
@@ -529,26 +529,20 @@ int do_insertfile_void(void)
 
 #ifdef ENABLE_MULTIBUFFER
 /*
- * Add/update an entry to the open_files filestruct.  If update is
+ * Add/update an entry to the open_files openfilestruct.  If update is
  * zero, a new entry is created; otherwise, the current entry is updated.
  * Return 0 on success or 1 on error.
  */
 int add_open_file(int update)
 {
-    filestruct *tmp;
+    openfilestruct *tmp;
 
     if (!fileage || !current || !filename)
 	return 1;
 
     /* if no entries, make the first one */
-    if (!open_files) {
-	open_files = make_new_node(NULL);
-
-	/* if open_files->file is NULL at the nrealloc() below, we get a
-	   segfault
-	open_files->file = open_files; */
-	open_files->file = NULL;
-    }
+    if (!open_files)
+	open_files = make_new_opennode(NULL);
 
     else if (!update) {
 
@@ -556,21 +550,16 @@ int add_open_file(int update)
 	   open_files and splice it in after the current one */
 
 #ifdef DEBUG
-	    fprintf(stderr, _("filename is %s"), open_files->data);
+	fprintf(stderr, _("filename is %s"), open_files->filename);
 #endif
 
-	tmp = make_new_node(NULL);
-	splice_node(open_files, tmp, open_files->next);
+	tmp = make_new_opennode(NULL);
+	splice_opennode(open_files, tmp, open_files->next);
 	open_files = open_files->next;
-
-	/* if open_files->file is NULL at the nrealloc() below, we get a
-	   segfault
-	open_files->file = open_files; */
-	open_files->file = NULL;
     }
 
     /* save current filename */
-    open_files->data = mallocstrcpy(open_files->data, filename);
+    open_files->filename = mallocstrcpy(open_files->filename, filename);
 
     /* save current total number of lines */
     open_files->file_totlines = totlines;
@@ -588,7 +577,7 @@ int add_open_file(int update)
     open_files->file_placewewant = placewewant;
 
     /* save current line number */
-    open_files->lineno = current->lineno;
+    open_files->file_lineno = current->lineno;
 
     /* if we're in view mode and updating, the file contents won't
        have changed, so we won't bother resaving the filestruct
@@ -596,23 +585,23 @@ int add_open_file(int update)
     if (!(ISSET(VIEW_MODE) && !update)) {
 	/* save current filestruct and restore full file position
 	   afterward */
-	open_files->file = fileage; 
-	do_gotopos(open_files->lineno, open_files->file_current_x, open_files->file_current_y, open_files->file_placewewant);
+	open_files->fileage = fileage; 
+	do_gotopos(open_files->file_lineno, open_files->file_current_x, open_files->file_current_y, open_files->file_placewewant);
     }
 
     /* save current modification status */
     open_files->file_modified = ISSET(MODIFIED);
 
 #ifdef DEBUG
-    fprintf(stderr, _("filename is %s"), open_files->data);
+    fprintf(stderr, _("filename is %s"), open_files->filename);
 #endif
 
     return 0;
 }
 
 /*
- * Update only the filename and full path stored in the current entry.
- * Return 0 on success or 1 on error.
+ * Update only the filename stored in the current entry.  Return 0 on
+ * success or 1 on error.
  */ 
 int open_file_change_name(void)
 {
@@ -620,7 +609,7 @@ int open_file_change_name(void)
 	return 1;
 
     /* save current filename */
-    open_files->data = mallocstrcpy(open_files->data, filename);
+    open_files->filename = mallocstrcpy(open_files->filename, filename);
 
     return 0;
 }
@@ -637,8 +626,8 @@ int load_open_file(void)
 
     /* set up the filename, the file buffer, the total number of lines in
        the file, and the total file size */
-    filename = mallocstrcpy(filename, open_files->data);
-    fileage = open_files->file;
+    filename = mallocstrcpy(filename, open_files->filename);
+    fileage = open_files->fileage;
     current = fileage;
     totlines = open_files->file_totlines;
     totsize = open_files->file_totsize;
@@ -649,7 +638,7 @@ int load_open_file(void)
 
     /* restore full file position: line number, x-coordinate, y-
        coordinate, place we want */
-    do_gotopos(open_files->lineno, open_files->file_current_x, open_files->file_current_y, open_files->file_placewewant);
+    do_gotopos(open_files->file_lineno, open_files->file_current_x, open_files->file_current_y, open_files->file_placewewant);
 
     /* restore the bottom of the file */
     filebot = current;
@@ -702,7 +691,7 @@ int open_prevfile(int closing_file)
 	open_files = open_files->prev;
 
 #ifdef DEBUG
-	fprintf(stderr, _("filename is %s"), open_files->data);
+	fprintf(stderr, _("filename is %s"), open_files->filename);
 #endif
 
     }
@@ -714,16 +703,15 @@ int open_prevfile(int closing_file)
 	    open_files = open_files->next;
 
 #ifdef DEBUG
-	    fprintf(stderr, _("filename is %s"), open_files->data);
+	    fprintf(stderr, _("filename is %s"), open_files->filename);
 #endif
 
     }
 
-/*    free_filestruct(fileage);  // delete this before reloading */
     load_open_file();
 
     statusbar(_("Switched to %s"),
-      ((open_files->data[0] == '\0') ? "New Buffer" : open_files->data ));
+      ((open_files->filename[0] == '\0') ? "New Buffer" : open_files->filename ));
 
 #ifdef DEBUG
     dump_buffer(current);
@@ -767,7 +755,7 @@ int open_nextfile(int closing_file)
 	open_files = open_files->next;
 
 #ifdef DEBUG
-	fprintf(stderr, _("filename is %s"), open_files->data);
+	fprintf(stderr, _("filename is %s"), open_files->filename);
 #endif
 
     }
@@ -778,7 +766,7 @@ int open_nextfile(int closing_file)
 	    open_files = open_files->prev;
 
 #ifdef DEBUG
-	    fprintf(stderr, _("filename is %s"), open_files->data);
+	    fprintf(stderr, _("filename is %s"), open_files->filename);
 #endif
 
 	}
@@ -787,7 +775,7 @@ int open_nextfile(int closing_file)
     load_open_file();
 
     statusbar(_("Switched to %s"),
-      ((open_files->data[0] == '\0') ? "New Buffer" : open_files->data ));
+      ((open_files->filename[0] == '\0') ? "New Buffer" : open_files->filename ));
 
 #ifdef DEBUG
     dump_buffer(current);
@@ -810,12 +798,12 @@ int open_nextfile_void(void)
  */
 int close_open_file(void)
 {
-    filestruct *tmp;
+    openfilestruct *tmp;
 
     if (!open_files)
 	return 1;
 
-    open_files->file = fileage;
+    open_files->fileage = fileage;
 
     tmp = open_files;
     if (open_nextfile(1)) {
@@ -823,9 +811,8 @@ int close_open_file(void)
 	    return 1;
     }
 
-    unlink_node(tmp);
-    free_filestruct(tmp->file);
-    delete_node(tmp);
+    unlink_opennode(tmp);
+    delete_opennode(tmp);
 
     shortcut_init(0);
     display_main_list();
@@ -1566,7 +1553,7 @@ int do_writeout(char *path, int exiting, int append)
 		/* first, if the filename was changed during the save,
 		   update the filename stored in the current entry, and
 		   then update the current entry */
-		if (strcmp(open_files->data, filename)) {
+		if (strcmp(open_files->filename, filename)) {
 		    open_file_change_name();
 		    add_open_file(1);
 		}
diff --git a/global.c b/global.c
index a46b4b3e..d79d8c14 100644
--- a/global.c
+++ b/global.c
@@ -56,7 +56,7 @@ filestruct *filebot = NULL;	/* Last node in the file struct */
 filestruct *cutbuffer = NULL;	/* A place to store cut text */
 
 #ifdef ENABLE_MULTIBUFFER
-filestruct *open_files = NULL;	/* The list of open files */
+openfilestruct *open_files = NULL;	/* The list of open files */
 #endif
 
 #ifndef DISABLE_JUSTIFY
@@ -759,7 +759,7 @@ void free_toggles(void)
 void thanks_for_all_the_fish(void) 
 {
 #ifdef ENABLE_MULTIBUFFER
-    filestruct * current_open_file;
+    openfilestruct * current_open_file;
 #endif
 
 #ifndef DISABLE_OPERATINGDIR
@@ -810,26 +810,24 @@ void thanks_for_all_the_fish(void)
    Do not cleanup the current one, that is fileage . . . do the
    rest of them though! (should be none if all went well) */
     current_open_file = open_files;
-    if (open_files != NULL){
-        while (open_files->prev != NULL) 
-           open_files = open_files->prev;
-        while (open_files->next != NULL) {
+    if (open_files != NULL) {
+	while (open_files->prev != NULL) 
+	    open_files = open_files->prev;
+	while (open_files->next != NULL) {
   /* cleanup of a multi buf . . . */
-           if (open_files != current_open_file) 
-             free_filestruct(open_files->file);
-           open_files = open_files->next;
-           free_filestruct(open_files->prev);
+	    open_files = open_files->next;
+	    if (open_files->prev != current_open_file) 
+		free_openfilestruct(open_files->prev);
         }
   /* cleanup of last multi buf . . . */
-        if (open_files != current_open_file) 
-	  free_filestruct(open_files->file);
-        free_filestruct(open_files);
+	free_openfilestruct(open_files);
     }
-#endif
+#else
   /* starting the cleanup of fileage now . . . */
 
     if (fileage != NULL)
-        free_filestruct(fileage);
+	free_filestruct(fileage);
+#endif
 
     /* that is all for now */
 
diff --git a/nano.c b/nano.c
index 72ac3c17..6f1aa6d7 100644
--- a/nano.c
+++ b/nano.c
@@ -131,7 +131,7 @@ void die(char *msg, ...)
 #ifdef ENABLE_MULTIBUFFER
     /* then save all of the other modified loaded files, if any */
     if (open_files) {
-        filestruct *tmp;
+	openfilestruct *tmp;
 
 	tmp = open_files;
 
@@ -143,10 +143,10 @@ void die(char *msg, ...)
 	    /* if we already saved the file above (i. e. if it was the
 	       currently loaded file), don't save it again */
 	    if (tmp != open_files) {
-		fileage = open_files->file;
+		fileage = open_files->fileage;
 		/* save the file if it's been modified */
 		if (open_files->file_modified)
-		    die_save_file(open_files->data);
+		    die_save_file(open_files->filename);
 	    }
 
 	    open_files = open_files->next;
@@ -293,7 +293,7 @@ filestruct *copy_node(filestruct * src)
     return dst;
 }
 
-/* Unlink a node from the rest of the struct */
+/* Unlink a node from the rest of the filestruct. */
 void unlink_node(filestruct * fileptr)
 {
     if (fileptr->prev != NULL)
@@ -303,8 +303,19 @@ void unlink_node(filestruct * fileptr)
 	fileptr->next->prev = fileptr->prev;
 }
 
-/* Delete a node from the struct.  This does NOT delete the data members
-   used only by open_files. */
+#ifdef ENABLE_MULTIBUFFER
+/* Unlink a node from the rest of the openfilestruct. */
+void unlink_opennode(openfilestruct * fileptr)
+{
+    if (fileptr->prev != NULL)
+	fileptr->prev->next = fileptr->next;
+
+    if (fileptr->next != NULL)
+	fileptr->next->prev = fileptr->prev;
+}
+#endif
+
+/* Delete a node from the filestruct. */
 void delete_node(filestruct * fileptr)
 {
     if (fileptr == NULL)
@@ -315,8 +326,22 @@ void delete_node(filestruct * fileptr)
     free(fileptr);
 }
 
-/* Okay, now let's duplicate a whole struct!  This does NOT duplicate the
-   data members used only by open_files. */
+#ifdef ENABLE_MULTIBUFFER
+/* Delete a node from the openfilestruct. */
+void delete_opennode(openfilestruct * fileptr)
+{
+    if (fileptr == NULL)
+	return;
+
+    if (fileptr->filename != NULL)
+	free(fileptr->filename);
+    if (fileptr->fileage != NULL)
+	free_filestruct(fileptr->fileage);
+    free(fileptr);
+}
+#endif
+
+/* Okay, now let's duplicate a whole struct! */
 filestruct *copy_filestruct(filestruct * src)
 {
     filestruct *dst, *tmp, *head, *prev;
@@ -340,8 +365,7 @@ filestruct *copy_filestruct(filestruct * src)
     return head;
 }
 
-/* Frees a struct.  This does NOT free the data members used only by
-   open_files. */
+/* Frees a filestruct. */
 int free_filestruct(filestruct * src)
 {
     filestruct *fileptr = src;
@@ -365,6 +389,32 @@ int free_filestruct(filestruct * src)
     return 1;
 }
 
+#ifdef ENABLE_MULTIBUFFER
+/* Frees an openfilestruct. */
+int free_openfilestruct(openfilestruct * src)
+{
+    openfilestruct *fileptr = src;
+
+    if (src == NULL)
+	return 0;
+
+    while (fileptr->next != NULL) {
+	fileptr = fileptr->next;
+	delete_opennode(fileptr->prev);
+
+#ifdef DEBUG
+	fprintf(stderr, _("delete_opennode(): free'd a node, YAY!\n"));
+#endif
+    }
+    delete_opennode(fileptr);
+#ifdef DEBUG
+    fprintf(stderr, _("delete_opennode(): free'd last node.\n"));
+#endif
+
+    return 1;
+}
+#endif
+
 int renumber_all(void)
 {
     filestruct *temp;
@@ -557,8 +607,7 @@ void version(void)
 
 }
 
-/* Create a new node.  This does NOT initialize the data members used
-   only by open_files. */
+/* Create a new filestruct node. */
 filestruct *make_new_node(filestruct * prevnode)
 {
     filestruct *newnode;
@@ -575,8 +624,24 @@ filestruct *make_new_node(filestruct * prevnode)
     return newnode;
 }
 
-/* Splice a node into an existing filestruct.  This does NOT set the data
-   members used only by open_files. */
+#ifdef ENABLE_MULTIBUFFER
+/* Create a new openfilestruct node. */
+openfilestruct *make_new_opennode(openfilestruct * prevnode)
+{
+    openfilestruct *newnode;
+
+    newnode = nmalloc(sizeof(openfilestruct));
+    newnode->filename = NULL;
+    newnode->fileage = NULL;
+
+    newnode->prev = prevnode;
+    newnode->next = NULL;
+
+    return newnode;
+}
+#endif
+
+/* Splice a node into an existing filestruct. */
 void splice_node(filestruct * begin, filestruct * newnode,
 		 filestruct * end)
 {
@@ -587,6 +652,19 @@ void splice_node(filestruct * begin, filestruct * newnode,
 	end->prev = newnode;
 }
 
+#ifdef ENABLE_MULTIBUFFER
+/* Splice a node into an existing openfilestruct. */
+void splice_opennode(openfilestruct * begin, openfilestruct * newnode,
+		     openfilestruct * end)
+{
+    newnode->next = end;
+    newnode->prev = begin;
+    begin->next = newnode;
+    if (end != NULL)
+	end->prev = newnode;
+}
+#endif
+
 int do_mark(void)
 {
 #ifdef NANO_SMALL
diff --git a/nano.h b/nano.h
index 42641535..5e26440b 100644
--- a/nano.h
+++ b/nano.h
@@ -73,20 +73,25 @@ typedef struct filestruct {
     char *data;
     struct filestruct *next;	/* Next node */
     struct filestruct *prev;	/* Previous node */
+    int lineno;			/* The line number */
+} filestruct;
 
 #ifdef ENABLE_MULTIBUFFER
-    struct filestruct *file;	/* Current file */
+typedef struct openfilestruct {
+    char *filename;
+    struct openfilestruct *next;	/* Next node */
+    struct openfilestruct *prev;	/* Previous node */
+    struct filestruct *fileage;	/* Current file */
     int file_current_x;		/* Current file's x-coordinate position */
     int file_current_y;		/* Current file's y-coordinate position */
     int file_modified;		/* Current file's modification status */
     int file_placewewant;	/* Current file's place we want */
     int file_totlines;		/* Current file's total number of lines */
     long file_totsize;		/* Current file's total size */
+    int file_lineno;		/* Current file's line number */
+} openfilestruct;
 #endif
 
-    int lineno;			/* The line number */
-} filestruct;
-
 typedef struct shortcut {
    int val;		/* Actual sequence that generates the keystroke */
    int altval;		/* Alt key # for this function, or 0 for none */
diff --git a/proto.h b/proto.h
index b88c36ee..8fc8b922 100644
--- a/proto.h
+++ b/proto.h
@@ -61,7 +61,7 @@ extern filestruct *current, *fileage, *edittop, *editbot, *filebot;
 extern filestruct *cutbuffer, *mark_beginbuf;
 
 #ifdef ENABLE_MULTIBUFFER
-extern filestruct *open_files;
+extern openfilestruct *open_files;
 #endif
 
 #ifdef ENABLE_COLOR
@@ -104,6 +104,11 @@ char *strstrwrapper(char *haystack, char *needle, char *rev_start);
 int search_init(int replacing);
 int renumber(filestruct * fileptr);
 int free_filestruct(filestruct * src);
+
+#ifdef ENABLE_MULTIBUFFER
+int free_openfilestruct(openfilestruct * src);
+#endif
+
 int xplustabs(void);
 int do_yesno(int all, int leavecursor, char *msg, ...);
 int actual_x(filestruct * fileptr, int xplus);
@@ -162,6 +167,11 @@ void edit_refresh(void), edit_refresh_clearok(void);
 void edit_update(filestruct * fileptr, int topmidbotnone);
 void update_cursor(void);
 void delete_node(filestruct * fileptr);
+
+#ifdef ENABLE_MULTIBUFFER
+void delete_opennode(openfilestruct * fileptr);
+#endif
+
 void set_modified(void);
 void dump_buffer_reverse(filestruct * inptr);
 void reset_cursor(void);
@@ -189,6 +199,11 @@ void die_save_file(char *die_filename);
 void new_file(void);
 void new_magicline(void);
 void splice_node(filestruct *begin, filestruct *newnode, filestruct *end);
+
+#ifdef ENABLE_MULTIBUFFER
+void splice_opennode(openfilestruct *begin, openfilestruct *newnode, openfilestruct *end);
+#endif
+
 void null_at(char **data, int index);
 void page_up(void);
 void blank_edit(void);
@@ -201,6 +216,11 @@ void window_init(void);
 void do_mouse(void);
 void print_view_warning(void);
 void unlink_node(filestruct * fileptr);
+
+#ifdef ENABLE_MULTIBUFFER
+void unlink_opennode(openfilestruct * fileptr);
+#endif
+
 void cut_marked_segment(filestruct * top, int top_x, filestruct * bot,
                         int bot_x, int destructive);
 void free_shortcutage(shortcut **src);
@@ -265,6 +285,11 @@ RETSIGTYPE main_loop (int junk);
 filestruct *copy_node(filestruct * src);
 filestruct *copy_filestruct(filestruct * src);
 filestruct *make_new_node(filestruct * prevnode);
+
+#ifdef ENABLE_MULTIBUFFER
+openfilestruct *make_new_opennode(openfilestruct * prevnode);
+#endif
+
 filestruct *findnextstr(int quiet, int bracket_mode, filestruct * begin,
 			int beginx, char *needle);
 
-- 
GitLab