From d3407b80e45f86dfd9e9de754b01c728a66541ce Mon Sep 17 00:00:00 2001 From: konsolebox Date: Sun, 21 Aug 2016 14:41:14 +0800 Subject: [PATCH 1/3] Give '.cloneX' filenames to cloned documents The filename is generated by adding a suffix to it in the form of '.cloneX' where X is a number ranging from 0 to 99. X is an empty string if it is 0. Any generated filename in which the file it targets already exists, or any generated filename that is already being used by another opened document, gets skipped, and the iteration moves to the next number. If the iteration reaches 99 and no appropriate filename was still generated, the cloned document is simply given with no filename or NULL. --- src/document.c | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/document.c b/src/document.c index 9fad86fca4..2ae4061a5f 100644 --- a/src/document.c +++ b/src/document.c @@ -3330,7 +3330,7 @@ GeanyDocument *document_index(gint idx) GeanyDocument *document_clone(GeanyDocument *old_doc) { - gchar *text; + gchar *text, *new_filename = NULL; GeanyDocument *doc; ScintillaObject *old_sci; @@ -3341,7 +3341,42 @@ GeanyDocument *document_clone(GeanyDocument *old_doc) else text = sci_get_contents(old_sci, -1); - doc = document_new_file(NULL, old_doc->file_type, text); + /* create a new filename based on the old filename */ + if (old_doc->file_name) + { + gchar *candidate; + guint i, j; + + for (i = 0; i <= 99; ++i) + { + if (i == 0) + candidate = g_strdup_printf("%s.clone", old_doc->file_name); + else + candidate = g_strdup_printf("%s.clone%u", old_doc->file_name, i); + + gboolean unused = TRUE; + + foreach_document(j) + { + if (documents[j]->file_name && strcmp(documents[j]->file_name, candidate) == 0) + { + unused = FALSE; + break; + } + } + + if (unused && !g_file_test(candidate, G_FILE_TEST_EXISTS)) + { + new_filename = candidate; + break; + } + + g_free(candidate); + } + } + + doc = document_new_file(new_filename, old_doc->file_type, text); + g_free(new_filename); g_free(text); document_set_text_changed(doc, TRUE); From 2c447f3be8a6170bc133a34937c3bd4604965ad8 Mon Sep 17 00:00:00 2001 From: konsolebox Date: Fri, 26 Aug 2016 20:55:20 +0800 Subject: [PATCH 2/3] Add documentation --- doc/geany.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doc/geany.txt b/doc/geany.txt index bf007c2e14..0cc98b5e86 100644 --- a/doc/geany.txt +++ b/doc/geany.txt @@ -511,12 +511,18 @@ shortcuts including for Most-Recently-Used document switching. Cloning documents ^^^^^^^^^^^^^^^^^ + The `Document->Clone` menu item copies the current document's text, cursor position and properties into a new untitled document. If there is a selection, only the selected text is copied. This can be useful when making temporary copies of text or for creating documents with similar or identical contents. +The new document gets a filename based on the original document, and is +in the form of '.cloneX' where X is a number ranging +from 0 to 99, but is not set if it is 0. The name that doesn't point to +an existing file and is not used by any document gets used. + Character sets and Unicode Byte-Order-Mark (BOM) ------------------------------------------------ From 697ff6c2bf8e0eeee0c40aa50327f2fdb80ec30f Mon Sep 17 00:00:00 2001 From: konsolebox Date: Sun, 28 Aug 2016 02:30:37 +0800 Subject: [PATCH 3/3] Use locale-encoded string when testing if file exists --- src/document.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/document.c b/src/document.c index 2ae4061a5f..898f218935 100644 --- a/src/document.c +++ b/src/document.c @@ -3365,10 +3365,17 @@ GeanyDocument *document_clone(GeanyDocument *old_doc) } } - if (unused && !g_file_test(candidate, G_FILE_TEST_EXISTS)) + if (unused) { - new_filename = candidate; - break; + gchar *locale_candidate = utils_get_locale_from_utf8(candidate); + gboolean file_exists = g_file_test(locale_candidate, G_FILE_TEST_EXISTS); + g_free(locale_candidate); + + if (!file_exists) + { + new_filename = candidate; + break; + } } g_free(candidate);