diff --git a/doc/geany.txt b/doc/geany.txt index 609f4c7b31..31c37f7f4f 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 '.copy-xx' where 'xx' is a number ranging +from 00 to 99. 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) ------------------------------------------------ diff --git a/src/document.c b/src/document.c index e144cc1d10..41e145ad84 100644 --- a/src/document.c +++ b/src/document.c @@ -3326,7 +3326,7 @@ GeanyDocument *document_index(gint idx) GeanyDocument *document_clone(GeanyDocument *old_doc) { - gchar *text; + gchar *text, *new_filename = NULL; GeanyDocument *doc; ScintillaObject *old_sci; @@ -3337,7 +3337,44 @@ 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) + { + guint i, j; + + for (i = 0; i <= 99; ++i) + { + gchar *candidate = g_strdup_printf("%s.copy-%02u", 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) + { + 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); + } + } + + doc = document_new_file(new_filename, old_doc->file_type, text); + g_free(new_filename); g_free(text); document_set_text_changed(doc, TRUE);