Skip to content

Commit

Permalink
added shortcut to duplicate selection (#615)
Browse files Browse the repository at this point in the history
* added shortcut to duplicate selection

* Fixed memory leak
  • Loading branch information
tlemy committed Jan 31, 2024
1 parent 7e3c373 commit 3135650
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 2 deletions.
9 changes: 8 additions & 1 deletion xed/resources/ui/xed-shortcuts.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<object class="GtkShortcutsSection">
<property name="visible">1</property>
<property name="section-name">shortcuts</property>
<property name="max-height">15</property>
<property name="max-height">16</property>
<child>
<object class="GtkShortcutsGroup">
<property name="visible">1</property>
Expand Down Expand Up @@ -270,6 +270,13 @@
<property name="title" translatable="yes">Delete current line</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
<property name="accelerator">&lt;ctrl&gt;&lt;shift&gt;D</property>
<property name="title" translatable="yes">Duplicate current line</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">1</property>
Expand Down
1 change: 1 addition & 0 deletions xed/resources/ui/xed-ui.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<menuitem name="EditCopyMenu" action="EditCopy"/>
<menuitem name="EditPasteMenu" action="EditPaste"/>
<menuitem name="EditDeleteMenu" action="EditDelete"/>
<menuitem name="EditDuplicateMenu" action="EditDuplicate"/>
<placeholder name="EditOps_1" />
<separator/>
<placeholder name="EditOps_2" />
Expand Down
16 changes: 16 additions & 0 deletions xed/xed-commands-edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,22 @@ _xed_cmd_edit_delete (GtkAction *action,
gtk_widget_grab_focus (GTK_WIDGET (active_view));
}

void
_xed_cmd_edit_duplicate (GtkAction *action,
XedWindow *window)
{
XedView *active_view;

xed_debug (DEBUG_COMMANDS);

active_view = xed_window_get_active_view (window);
g_return_if_fail (active_view);

xed_view_duplicate (active_view);

gtk_widget_grab_focus (GTK_WIDGET (active_view));
}

void
_xed_cmd_edit_select_all (GtkAction *action,
XedWindow *window)
Expand Down
1 change: 1 addition & 0 deletions xed/xed-commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void _xed_cmd_edit_cut (GtkAction *action, XedWindow *window);
void _xed_cmd_edit_copy (GtkAction *action, XedWindow *window);
void _xed_cmd_edit_paste (GtkAction *action, XedWindow *window);
void _xed_cmd_edit_delete (GtkAction *action, XedWindow *window);
void _xed_cmd_edit_duplicate (GtkAction *action, XedWindow *window);
void _xed_cmd_edit_select_all (GtkAction *action, XedWindow *window);
void _xed_cmd_edit_preferences (GtkAction *action, XedWindow *window);
void _xed_cmd_edit_toggle_comment (GtkAction *action, XedWindow *window);
Expand Down
2 changes: 2 additions & 0 deletions xed/xed-ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ static const GtkActionEntry xed_menu_entries[] =
N_("Paste the clipboard"), G_CALLBACK (_xed_cmd_edit_paste) },
{ "EditDelete", "edit-delete-symbolic", N_("_Delete"), NULL,
N_("Delete the selected text"), G_CALLBACK (_xed_cmd_edit_delete) },
{ "EditDuplicate", "edit-duplicate-symbolic", N_("_Duplicate"), "<control><shift>D",
N_("Duplicate the selected text"), G_CALLBACK (_xed_cmd_edit_duplicate) },
{ "EditSelectAll", "edit-select-all-symbolic", N_("Select _All"), "<control>A",
N_("Select the entire document"), G_CALLBACK (_xed_cmd_edit_select_all) },
{ "EditToggleComment", NULL, N_("_Toggle Comment"), "<control>slash",
Expand Down
43 changes: 42 additions & 1 deletion xed/xed-view.c
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,47 @@ xed_view_delete_selection (XedView *view)
FALSE, 0.0, 0.0);
}

/**
* xed_view_duplicate:
* @view: a #XedView
*
* Duplicates the text currently selected in the #GtkTextBuffer
* or duplicates the current line if nothing is selected
**/
void
xed_view_duplicate (XedView *view)
{
GtkTextIter start;
GtkTextIter end;
GtkTextBuffer *buffer = NULL;
gchar *text;
size_t length;

xed_debug (DEBUG_VIEW);

g_return_if_fail (XED_IS_VIEW (view));

buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW(view));
g_return_if_fail (buffer != NULL);

if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
{
gtk_text_iter_set_line_index (&start, 0);
gtk_text_iter_forward_to_line_end (&end);
}

gtk_text_iter_order (&start, &end);
text = gtk_text_buffer_get_text (buffer, &start, &end, TRUE);

if ((length = strlen(text)) > 0)
{
gtk_text_buffer_insert (buffer, &end, "\n", 1);
gtk_text_buffer_insert (buffer, &end, text, length);
}

g_free (text);
}

/**
* xed_view_select_all:
* @view: a #XedView
Expand Down Expand Up @@ -915,4 +956,4 @@ xed_view_update_draw_whitespace_locations_and_types (XedView *view)
GTK_SOURCE_SPACE_TYPE_NONE);
// enable chosen locations and types
gtk_source_space_drawer_set_types_for_locations (spacedrawer, locations, types);
}
}
1 change: 1 addition & 0 deletions xed/xed-view.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ void xed_view_cut_clipboard (XedView *view);
void xed_view_copy_clipboard (XedView *view);
void xed_view_paste_clipboard (XedView *view);
void xed_view_delete_selection (XedView *view);
void xed_view_duplicate (XedView *view);
void xed_view_select_all (XedView *view);
void xed_view_scroll_to_cursor (XedView *view);
void xed_view_set_font (XedView *view, gboolean def, const gchar *font_name);
Expand Down

0 comments on commit 3135650

Please sign in to comment.