From 8e5ba5dd1b18ff366c02059b0ef73f951e0c8295 Mon Sep 17 00:00:00 2001 From: Rishabh Dave Date: Mon, 24 Jul 2017 01:16:10 +0530 Subject: [PATCH] files: don't create a new buffer if a file already has a buffer When locking is off and file has to be read, nano will create a new buffer even for a file that is already open and has a buffer. Avoid doing so by comparing the new filename and the filenames of all the current buffers. Signed-off-by: Rishabh Dave --- src/files.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/files.c b/src/files.c index d2a9718..4a6d806 100644 --- a/src/files.c +++ b/src/files.c @@ -35,6 +35,21 @@ #define LOCKBUFSIZE 8192 +/* Check if user provided new filename is already open. */ +bool already_open(const char *newfilename) +{ + openfilestruct *tempbuffer = openfile; + + do { + if (! strcmp(newfilename, tempbuffer->filename)) + return TRUE; + + tempbuffer = tempbuffer->next; + } while (strcmp(tempbuffer->filename, openfile->filename)); + + return FALSE; +} + /* Verify that the containing directory of the given filename exists. */ bool has_valid_path(const char *filename) { @@ -458,6 +473,9 @@ bool open_buffer(const char *filename, bool undoable) } } + if (openfile != NULL && new_buffer == TRUE && already_open(filename)) + return FALSE; + /* If we're going to load into a new buffer, first create the new * buffer and (if possible) lock the corresponding file. */ if (new_buffer) { @@ -1162,6 +1180,14 @@ void do_insertfile(void) * tilde-expanded. */ answer = free_and_assign(answer, real_dir_from_tilde(answer)); + /* Do not create a new buffer if we already have a buffer + * for the given filename. */ + if (ISSET(MULTIBUFFER) && already_open(answer)) { + statusline(ALERT, _("File \"%s\" is already open"), + answer); + return; + } + /* Save the file specified in answer in the current buffer. */ open_buffer(answer, TRUE); } -- 2.9.4