Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default filenames (.clone[X]) to cloned documents #1197

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/geany.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<original_name>.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)
------------------------------------------------
Expand Down
46 changes: 44 additions & 2 deletions src/document.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -3341,7 +3341,49 @@ 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)
{
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);

Expand Down