nano-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Nano-devel] merged patch


From: David Lawrence Ramsey
Subject: [Nano-devel] merged patch
Date: Thu, 21 Feb 2002 07:36:55 -0800 (PST)

Attached is the patch you sent, with the following
modifications (aside from the patch name change):

Multibuffer tweaks:

* When inserting a nonexistent file or a blank buffer in
multibuffer mode, both of which result in a new blank
buffer, 'New File' is now displayed on the statusbar instead
of '"" not found' or '[filename] not found.'

* All duplicate handling code has been removed, and the
resulting changes merged with your patch.

getnextfn() tweaks:

* Cosmetic alterations have been mode to make your added
code match the style of the rest of nano's code (just so
it's a little easier to read).

* I moved getnextfn() has been moved from the absolute end
of files.c, so it is closer to related functions, and
renamed it to get_next_filename(), as that name is slightly
more descriptive.

* I changed it to use charalloc() to allocate string space,
instead of malloc(), to match the rest of nano.

* Instead of going by the assumption that the maximum int
vlue has 10 digits, as your original code did, there is now
a function in utils.c (num_of_digits()) that calculates the
maximum number of digits (not counting the sign) in an int.
The number of digits in the maximum int value (INT_MAX) is
calculated using this function, and used to allocate the
proper maximum amount of string space in die_save_file() and
get_next_filename().  If get_next_filename() runs up to
INT_MAX without finding a match (filename.save,
filename.save.1 ... filename.save.2147283627; nothing like
over 2 billion files in a directory :), it will break out
just as it does when it finds a match, to avoid a negative
overflow), but ret will be left set at the NULL value it was
initialized to when allocated by charalloc(), which uses
calloc()

* Your saving of the calculated filename in die_save_file()
using strcpy() would cause a segfault if ret was longer than
name; name is now just set to point to ret, and ret is
free()d at the end of die_save_file().

* The context-sensitive help now explains how to insert a
blank buffer in multibuffer mode: just press enter at the
prompt without typing in a filename, or, as before, type in
a nonexistent filename at the prompt and press Enter.  Since
Enter is a little easier to hit than Ctrl-N, as in your
next-to-last patch, your Ctrl-N shortcut may not be needed.

Miscellaneous:

* My nanobits.patch has also been merged with your patch,
since the current nano-1.1.6-cvs will not always read files
correctly without one ofits fixes (I needed it fixed in
order to do proper testing); the nanobits.patch also now
removes a few more unneeded blank lines, and it should be
noted that it, and the previous version of it, sets the
replace_list_2 shortcut to NULL, as one of the pieces of
Steven Kneizys' patches also does.  (I hope that it
won't be too much trouble to merge with SPK's patch.)

* One thing I forgot about in this patch and just remembered
as I was typing this: maybe the char *name argument of
get_next_filename() can be a const char* instead of a char*,
since it isn't modified in the function at all?  (I don't
have any more time to recreate it and repost it today, so
could you possibly look into it, if you don't mind?)

* One caveat about this patch: Could you also test it on
your end, so I can be sure I haven't inadvertently missed or
screwed up anything?  Thanks.

* One last item unrelated to this patch: I've been testing
this by running it in an rxvt (version 2.6.4), creating
unsaved files, and then shrinking the window to too small a
size.  However, when I do this, the terminal settings don't
seem to be properly reset (i. e. the lines indicating the
names of the files saved are only ended with the cursor's
moving down and not back to the beginning of the line, and
any text I type at the prompt is invisible until I do a
"stty sane"; (a) does this happen to anyone else, and (b)
do I need to provide more information?


_____________________________________________________________
Sluggy.Net: The Sluggy Freelance Community!
diff -urN nano-1.1.6-cvs/files.c nano-1.1.6-cvs-fixed/files.c
--- nano-1.1.6-cvs/files.c      Sat Feb 16 15:03:44 2002
+++ nano-1.1.6-cvs-fixed/files.c        Thu Feb 21 09:23:38 2002
@@ -54,18 +54,10 @@
     current = fileage;
 
 #ifdef ENABLE_MULTIBUFFER
-    /* if quiet is zero, add a new entry to the open_files structure, and
-       do duplicate checking; otherwise, update the current entry and
-       don't do duplicate checking (the latter is needed in the case of
-       the alternate spell checker); if a duplicate entry was found,
-       reload the currently open file (it may have been changed during
-       duplicate handling) */
-    if (quiet != 0)
-       quiet = 1;
-    if (add_open_file(quiet, 1 - quiet) == 2) {
-       load_open_file();
-       statusbar(_("File already loaded"));
-    }
+    /* if quiet is zero, add a new entry to the open_files structure;
+       otherwise, update the current entry (the latter is needed in the
+       case of the alternate spell checker) */
+    add_open_file(quiet);
 #endif
 
     wmove(edit, current_y, current_x);
@@ -89,12 +81,11 @@
 
 #ifdef ENABLE_MULTIBUFFER
     /* if there aren't any entries in open_files, create the entry for
-       this new file, and, of course, don't bother checking for
-       duplicates; without this, if nano is started without a filename on
-       the command line, a new file will be created, but it will be given
-       no open_files entry, leading to problems later on */
+       this new file; without this, if nano is started without a filename
+       on the command line, a new file will be created, but it will be
+       given no open_files entry, leading to problems later on */
     if (!open_files) {
-       add_open_file(0, 0);
+       add_open_file(0);
        /* turn off view mode in this case; this is for consistency
           whether multibuffers are compiled in or not */
        UNSET(VIEW_MODE);
@@ -210,8 +201,9 @@
            buf[0] = 0;
            i = 0;
 #ifndef NANO_SMALL
-        } else if (!ISSET(NO_CONVERT) && input[0] < 32 
-                       && input[0] != '\r' && input[0] != '\n') 
+        } else if (!ISSET(NO_CONVERT) && input[0] >= 0 && input[0] <= 31 
+                       && input[0] != '\t' && input[0] != '\r'
+                       && input[0] != '\n') 
            /* If the file has binary chars in it, don't stupidly
                assume it's a DOS or Mac formatted file! */
            SET(NO_CONVERT);
@@ -295,9 +287,8 @@
     struct stat fileinfo;
 
     if (!strcmp(filename, "") || stat(filename, &fileinfo) == -1) {
-       if (insert) {
-           if (!quiet)
-               statusbar(_("\"%s\" not found"), filename);
+       if (insert && !quiet) {
+           statusbar(_("\"%s\" not found"), filename);
            return -1;
        } else {
            /* We have a new file */
@@ -332,6 +323,32 @@
     return 1;
 }
 
+void get_next_filename(char *name, char *ret)
+{
+    int i = 0, max_int_digits = num_of_digits(INT_MAX);
+    char cnum[max_int_digits + 2];
+    char buf[strlen(name) + max_int_digits + 2];
+    struct stat fs;
+
+    strcpy(buf, name);
+
+    while(1) {
+
+       if (stat(buf, &fs) == -1)
+           break;
+       if (i == INT_MAX)
+           break;
+
+       i++;
+       sprintf(cnum, ".%d", i);
+       strcpy(buf, name);
+       strcat(buf, cnum);
+    }
+
+    if (i < INT_MAX)
+       strcpy(ret, buf);
+}
+
 int do_insertfile(int loading_file)
 {
     int i;
@@ -386,10 +403,8 @@
 #ifdef ENABLE_MULTIBUFFER
        if (loading_file) {
 
-           /* update the current entry in the open_files structure; we
-              don't need to check for duplicate entries (the conditions
-              that could create them are taken care of elsewhere) */
-           add_open_file(1, 0);
+           /* update the current entry in the open_files structure */
+           add_open_file(1);
 
            free_filestruct(fileage);
            new_file();
@@ -397,7 +412,7 @@
        }
 #endif
 
-       i = open_file(realname, 1, 0);
+       i = open_file(realname, 1, loading_file);
 
 #ifdef ENABLE_MULTIBUFFER
        if (loading_file)
@@ -471,25 +486,15 @@
 /*
  * Add/update an entry to the open_files filestruct.  If update is
  * zero, a new entry is created; otherwise, the current entry is updated.
- * If dup_fix is zero, checking for and handling duplicate entries is not
- * done; otherwise, it is.  Return 0 on success, 1 on error, or 2 on
- * finding a duplicate entry.
+ * Return 0 on success or 1 on error.
  */
-int add_open_file(int update, int dup_fix)
+int add_open_file(int update)
 {
     filestruct *tmp;
 
     if (!fileage || !current || !filename)
        return 1;
 
-    /* first, if duplicate checking is allowed, do it */
-    if (dup_fix) {
-
-       /* if duplicates were found and handled, we're done */
-       if (open_file_dup_fix(update))
-           return 2;
-    }
-
     /* if no entries, make the first one */
     if (!open_files) {
        open_files = make_new_node(NULL);
@@ -520,9 +525,6 @@
     /* save current filename */
     open_files->data = mallocstrcpy(open_files->data, filename);
 
-    /* save the full path location */
-    open_files->file_path = get_full_path(open_files->data);
-
     /* save current total number of lines */
     open_files->file_totlines = totlines;
 
@@ -574,9 +576,6 @@
     /* save current filename */
     open_files->data = mallocstrcpy(open_files->data, filename);
 
-    /* save the full path location */
-    open_files->file_path = get_full_path(open_files->data);
-
     return 0;
 }
 
@@ -630,95 +629,6 @@
 }
 
 /*
- * Search the open_files structure for an entry with the same value for
- * the file_path member as the current entry (i. e. a duplicate entry).
- * If one is found, return a pointer to it; otherwise, return NULL.
- *
- * Note: This should only be called inside open_file_dup_fix().
- */
-filestruct *open_file_dup_search(int update)
-{
-    filestruct *tmp;
-    char *path;
-
-    if (!open_files || !filename)
-       return NULL;
-
-    tmp = open_files;
-    path = get_full_path(filename);
-
-    /* if there's only one entry, handle it */
-    if (!tmp->prev && !tmp->next) {
-       if (!strcmp(tmp->file_path, path))
-           return tmp;
-    }
-
-    /* otherwise, go to the beginning */
-    while (tmp->prev)
-       tmp = tmp->prev;
-
-    /* and search the entries one by one */
-    while (tmp) {
-
-       if (!strcmp(tmp->file_path, path)) {
-
-           if (!update)
-               /* if we're making a new entry and there's an entry with
-                  the same full path, we've found a duplicate */
-               return tmp;
-           else {
-
-               /* if we're updating an existing entry and there's an
-                  entry with the same full path that isn't the current
-                  entry, we've found a duplicate */
-               if (tmp != open_files) 
-                   return tmp;
-           }
-       }
-
-       /* go to the next entry */
-       tmp = tmp->next;
-
-    }
-
-    return NULL;
-}
-
-/*
- * Search for duplicate entries in the open_files structure using
- * open_file_dup_search(), and, if one is found, handle it properly.
- * Return 0 if no duplicates were found, and 1 otherwise.
- */
-int open_file_dup_fix(int update)
-{
-    filestruct *tmp = open_file_dup_search(update);
-
-    if (!tmp)
-       return 0;
-
-    /* if there's only one entry, handle it */
-    if (!tmp->prev && !tmp->next)
-       return 1;
-
-    /* otherwise, if we're not updating, the user's trying to load a
-       duplicate; switch to the original instead */
-    if (!update) {
-       open_files = tmp;
-       return 1;
-    }
-
-    /* if we are updating, the filename's been changed via a save; it's
-       thus more recent than the original, so remove the original */
-    else {
-       unlink_node(tmp);
-       free_filestruct(tmp->file);
-       free(tmp->file_path);
-       delete_node(tmp);
-    }
-    return 0;
-}
-
-/*
  * Open the previous entry in the open_files structure.  If closing_file
  * is zero, update the current entry before switching from it.
  * Otherwise, we are about to close that entry, so don't bother doing so.
@@ -730,10 +640,9 @@
        return 1;
 
     /* if we're not about to close the current entry, update it before
-       doing anything; since we're only switching, we don't need to check
-       for duplicate entries */
+       doing anything */
     if (!closing_file)
-       add_open_file(1, 0);
+       add_open_file(1);
 
     if (!open_files->prev && !open_files->next) {
 
@@ -792,10 +701,9 @@
        return 1;
 
     /* if we're not about to close the current entry, update it before
-       doing anything; since we're only switching, we don't need to check
-       for duplicate entries */
+       doing anything */
     if (!closing_file)
-       add_open_file(1, 0);
+       add_open_file(1);
 
     if (!open_files->prev && !open_files->next) {
 
@@ -862,7 +770,6 @@
 
     unlink_node(tmp);
     free_filestruct(tmp->file);
-    free(tmp->file_path);
     delete_node(tmp);
 
     shortcut_init(0);
@@ -871,7 +778,7 @@
 }
 #endif /* MULTIBUFFER */
 
-#if defined (ENABLE_MULTIBUFFER) || !defined (DISABLE_SPELLER) || !defined 
(DISABLE_OPERATINGDIR)
+#if !defined (DISABLE_SPELLER) || !defined (DISABLE_OPERATINGDIR)
 /*
  * When passed "[relative path]" or "[relative path][filename]" in
  * origpath, return "[full path]" or "[full path][filename]" on success,
@@ -1020,7 +927,7 @@
 
     return newpath;
 }
-#endif /* ENABLE_MULTIBUFFER || !DISABLE_SPELLER || !DISABLE_OPERATINGDIR */
+#endif /* !DISABLE_SPELLER || !DISABLE_OPERATINGDIR */
 
 #ifndef DISABLE_SPELLER
 /*
@@ -1602,18 +1509,16 @@
            if (!exiting) {
 
                /* first, if the filename was changed during the save,
-                  update the filename and full path stored in the
-                  current entry, and then update the current entry,
-                  checking for duplicate entries */
+                  update the filename stored in the current entry, and
+                  then update the current entry */
                if (strcmp(open_files->data, filename)) {
                    open_file_change_name();
-                   add_open_file(1, 1);
+                   add_open_file(1);
                }
                else {
 
-                   /* otherwise, just update the current entry without
-                      checking for duplicate entries */
-                   add_open_file(1, 0);
+                   /* otherwise, just update the current entry */
+                   add_open_file(1);
                }
            }
 #endif
@@ -2127,7 +2032,6 @@
 
 }
 
-
 /* Initialize the browser code, including the list of files in *path */
 char **browser_init(char *path, int *longest, int *numents)
 {
@@ -2618,8 +2522,4 @@
     return do_browser(tmp);
 
 }
-
-
-
 #endif
-
diff -urN nano-1.1.6-cvs/global.c nano-1.1.6-cvs-fixed/global.c
--- nano-1.1.6-cvs/global.c     Fri Feb 15 14:17:02 2002
+++ nano-1.1.6-cvs-fixed/global.c       Thu Feb 21 08:01:22 2002
@@ -35,6 +35,7 @@
 /*
  * Global variables
  */
+
 int flags = 0;                 /* Our new flag containing many options */
 WINDOW *edit;                  /* The file portion of the editor  */
 WINDOW *topwin;                        /* Top line of screen */
@@ -87,7 +88,7 @@
 shortcut *main_list = NULL;
 shortcut *whereis_list = NULL;
 shortcut *replace_list = NULL;
-shortcut *replace_list_2;      /* 2nd half of replace dialog */
+shortcut *replace_list_2 = NULL;       /* 2nd half of replace dialog */
 shortcut *goto_list = NULL;
 shortcut *gotodir_list = NULL;
 shortcut *writefile_list = NULL;
diff -urN nano-1.1.6-cvs/nano.1 nano-1.1.6-cvs-fixed/nano.1
--- nano-1.1.6-cvs/nano.1       Fri Feb  1 08:40:34 2002
+++ nano-1.1.6-cvs-fixed/nano.1 Thu Feb 21 08:01:22 2002
@@ -45,6 +45,9 @@
 .B \-M (\-\-mac)
 Write file in Mac format.
 .TP
+.B \-N (\-\-noconvert)
+Disable automatic conversion of files from DOS/Mac format.
+.TP
 .B \-R (\-\-regexp)
 Enable regular expression matching for search strings, as well as
 \\n subexpression replacement for replace strings, if available.
diff -urN nano-1.1.6-cvs/nano.c nano-1.1.6-cvs-fixed/nano.c
--- nano-1.1.6-cvs/nano.c       Fri Feb 15 16:12:52 2002
+++ nano-1.1.6-cvs-fixed/nano.c Thu Feb 21 09:15:50 2002
@@ -158,27 +158,34 @@
 
 void die_save_file(char *die_filename)
 {
-    char *name;
-    int i;
+    char *name, *ret;
+    int i, int_max_digits = num_of_digits(INT_MAX);
 
     /* if we can't save we have REAL bad problems,
      * but we might as well TRY. */
     if (die_filename[0] == '\0') {
        name = "nano.save";
-       i = write_file(name, 1, 0, 0);
-    } else {
-
+       ret = charalloc(strlen(name) + int_max_digits + 2);
+       get_next_filename(name, ret);
+       i = write_file(ret, 1, 0, 0);
+       name = ret;
+    }
+    else {
        char *buf = charalloc(strlen(die_filename) + 6);
        strcpy(buf, die_filename);
        strcat(buf, ".save");
-       i = write_file(buf, 1, 0, 0);
-       name = buf;
+       ret = charalloc(strlen(buf) + int_max_digits + 2);
+       get_next_filename(buf, ret);
+       i = write_file(ret, 1, 0, 0);
+       name = ret;
     }
 
     if (i != -1)
        fprintf(stderr, _("\nBuffer written to %s\n"), name);
     else
-       fprintf(stderr, _("\nNo %s written (file exists?)\n"), name);
+       fprintf(stderr, _("\nNo %s written (too many backup files?)\n"), name);
+
+    free(ret);
 }
 
 /* Die with an error message that the screen was too small if, well, the
@@ -1744,9 +1751,8 @@
 #ifdef ENABLE_MULTIBUFFER
     /* update the current open_files entry before spell-checking, in case
        any problems occur; the case of there being no open_files entries
-       is handled elsewhere (before we reach this point); no duplicate
-       checking is needed here */
-    add_open_file(1, 0);
+       is handled elsewhere (before we reach this point) */
+    add_open_file(1);
 #endif
 
     if (alt_speller)
@@ -2454,12 +2460,11 @@
                "or --multibuffer command line flags, the Meta-F toggle or "
                "using a nanorc file, inserting a file will cause it to be "
                "loaded into a separate buffer (use Meta-< and > to switch "
-               "between file buffers).\n\n In multiple buffer mode, the "
-               "same file cannot be loaded twice, not even a \"New "
-               "Buffer.\" A workaround to load another blank buffer is to "
-               "load a nonexistent filename into a separate buffer.\n\n "
-               "The following function keys are available in Insert File "
-               "mode:\n\n");
+               "between file buffers).\n\n If you need another blank "
+               "buffer, just press Enter at the prompt without typing in a "
+               "filename, or type in a nonexistent filename at the prompt "
+               "and press Enter.\n\n The following function keys are "
+               "available in Insert File mode:\n\n");
     else if (currshortcut == writefile_list)
        ptr = _("Write File Help Text\n\n "
                "Type the name that you wish to save the current file "
diff -urN nano-1.1.6-cvs/nano.h nano-1.1.6-cvs-fixed/nano.h
--- nano-1.1.6-cvs/nano.h       Fri Feb 15 14:17:02 2002
+++ nano-1.1.6-cvs-fixed/nano.h Thu Feb 21 08:01:22 2002
@@ -79,7 +79,6 @@
     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 */
-    char *file_path;           /* Current file's full path location */
     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 */
diff -urN nano-1.1.6-cvs/proto.h nano-1.1.6-cvs-fixed/proto.h
--- nano-1.1.6-cvs/proto.h      Sat Feb 16 15:03:44 2002
+++ nano-1.1.6-cvs-fixed/proto.h        Thu Feb 21 08:38:28 2002
@@ -104,9 +104,10 @@
 int open_file(char *filename, int insert, int quiet);
 int do_insertfile(int loading_file);
 int length_of_list(shortcut *s);
+int num_of_digits(int n);
 
 #ifdef ENABLE_MULTIBUFFER
-int add_open_file(int update, int dup_fix);
+int add_open_file(int update);
 #endif
 
 #ifndef DISABLE_OPERATINGDIR
@@ -186,6 +187,7 @@
 void unlink_node(filestruct * fileptr);
 void cut_marked_segment(filestruct * top, int top_x, filestruct * bot,
                         int bot_x, int destructive);
+void get_next_filename(char *name, char *ret);
 
 #ifdef ENABLE_NANORC
 void do_rcfile(void);
@@ -212,7 +214,6 @@
 int keypad_on(WINDOW * win, int newval);
 
 #ifdef ENABLE_MULTIBUFFER
-int open_file_dup_fix(int update);
 int open_prevfile(int closing_file), open_nextfile(int closing_file);
 int open_prevfile_void(void), open_nextfile_void(void);
 #endif
@@ -250,12 +251,6 @@
 filestruct *findnextstr(int quiet, int bracket_mode, filestruct * begin,
                        int beginx, char *needle);
 
-#ifdef ENABLE_MULTIBUFFER
-filestruct *open_file_dup_search(int update);
-#endif
-
 #ifndef DISABLE_HELP
 void help_init(void);
 #endif
-
-
diff -urN nano-1.1.6-cvs/rcfile.c nano-1.1.6-cvs-fixed/rcfile.c
--- nano-1.1.6-cvs/rcfile.c     Sat Feb 16 15:34:58 2002
+++ nano-1.1.6-cvs-fixed/rcfile.c       Thu Feb 21 08:01:22 2002
@@ -432,7 +432,6 @@
 {
     char *unable = _("Unable to open ~/.nanorc file, %s");
     struct stat fileinfo;
-    int skip=0;
     FILE *rcstream;
 
     if (getenv("HOME") == NULL)
diff -urN nano-1.1.6-cvs/utils.c nano-1.1.6-cvs-fixed/utils.c
--- nano-1.1.6-cvs/utils.c      Sat Feb 16 15:03:44 2002
+++ nano-1.1.6-cvs-fixed/utils.c        Thu Feb 21 08:01:22 2002
@@ -36,6 +36,21 @@
 #define _(string) (string)
 #endif
 
+int num_of_digits(int n)
+{
+    int i = 1;
+
+    if (n < 0)
+       n = 0 - n;
+
+    while (n > 10) {
+       n /= 10;
+       i++;
+    }
+
+    return i;
+}
+
 /* Lower case a string - must be null terminated */
 void lowercase(char *src)
 {

reply via email to

[Prev in Thread] Current Thread [Next in Thread]