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);
#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);
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);
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 */
return 1;
}
+
+/* This function will return the name of the first available extension
+ of a filename (starting with the filename, then filename.1, etc).
+ Memory is allocated for the return value. If no writable extension
+ exists we return "" */
+char *get_next_filename(char *name)
+{
+ int i = 0;
+ char *buf = NULL;
+ struct stat fs;
+
+ buf = charalloc(strlen(name) + num_of_digits(INT_MAX) + 2);
+ strcpy(buf, name);
+
+ while(1) {
+
+ if (stat(buf, &fs) == -1)
+ break;
+ if (i == INT_MAX)
+ break;
+
+ i++;
+ strcpy(buf, name);
+ sprintf(&buf[strlen(name)], ".%d", i);
+ }
+
+ if (i == INT_MAX)
+ buf[0] = '\0';
+
+ return buf;
+}
+
int do_insertfile(int loading_file)
{
int i;
#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();
}
#endif
- i = open_file(realname, 1, 0);
+ i = open_file(realname, 1, loading_file);
#ifdef ENABLE_MULTIBUFFER
if (loading_file)
/*
* 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);
/* 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;
/* 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;
}
return 0;
}
-/*
- * 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.
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) {
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) {
unlink_node(tmp);
free_filestruct(tmp->file);
- free(tmp->file_path);
delete_node(tmp);
shortcut_init(0);
}
#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,
return newpath;
}
-#endif /* ENABLE_MULTIBUFFER || !DISABLE_SPELLER || !DISABLE_OPERATINGDIR */
+#endif /* !DISABLE_SPELLER || !DISABLE_OPERATINGDIR */
#ifndef DISABLE_SPELLER
/*
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
}
-
/* Initialize the browser code, including the list of files in *path */
char **browser_init(char *path, int *longest, int *numents)
{
return do_browser(tmp);
}
-
-
-
#endif
-
void die_save_file(char *die_filename)
{
- char *name;
- int i;
+ char *name, *ret;
+ int i = -1;
/* 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 = get_next_filename(name);
+ if (strcmp(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 = get_next_filename(buf);
+ if (strcmp(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
#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)
"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 "