Skip to content

Commit

Permalink
Add capability to automatically sort editor tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
konsolebox committed Jul 20, 2016
1 parent 138dd09 commit 04eb16b
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 8 deletions.
30 changes: 30 additions & 0 deletions data/geany.glade
Expand Up @@ -2179,6 +2179,36 @@
<property name="position">4</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="check_auto_sort_tabs_filename">
<property name="label" translatable="yes">Automatically sort tabs based on filename</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_underline">True</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">5</property>
</packing>
</child>
<child>
<object class="GtkCheckButton" id="check_auto_sort_tabs_pathname">
<property name="label" translatable="yes">Automatically sort tabs based on pathname</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<property name="use_underline">True</property>
<property name="draw_indicator">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">6</property>
</packing>
</child>
</object>
</child>
</object>
Expand Down
4 changes: 3 additions & 1 deletion src/callbacks.c
Expand Up @@ -45,6 +45,7 @@
#include "main.h"
#include "msgwindow.h"
#include "navqueue.h"
#include "notebook.h"
#include "plugins.h"
#include "pluginutils.h"
#include "prefs.h"
Expand Down Expand Up @@ -1412,7 +1413,8 @@ void on_menu_open_selected_file1_activate(GtkMenuItem *menuitem, gpointer user_d
}

if (g_file_test(filename, G_FILE_TEST_EXISTS))
document_open_file(filename, FALSE, NULL, NULL);
if (document_open_file(filename, FALSE, NULL, NULL) != NULL)
notebook_auto_sort_tabs();
else
{
SETPTR(sel, utils_get_utf8_from_locale(sel));
Expand Down
14 changes: 12 additions & 2 deletions src/document.c
Expand Up @@ -1519,6 +1519,7 @@ void document_open_file_list(const gchar *data, gsize length)
{
guint i;
gchar **list;
gboolean has_opened = FALSE;

g_return_if_fail(data != NULL);

Expand All @@ -1531,10 +1532,14 @@ void document_open_file_list(const gchar *data, gsize length)

if (filename == NULL)
continue;
document_open_file(filename, FALSE, NULL, NULL);
if (document_open_file(filename, FALSE, NULL, NULL) != NULL)
has_opened = TRUE;
g_free(filename);
}

if (has_opened == TRUE)
notebook_auto_sort_tabs();

g_strfreev(list);
}

Expand All @@ -1553,11 +1558,16 @@ void document_open_files(const GSList *filenames, gboolean readonly, GeanyFilety
const gchar *forced_enc)
{
const GSList *item;
gboolean has_opened = FALSE;

for (item = filenames; item != NULL; item = g_slist_next(item))
{
document_open_file(item->data, readonly, ft, forced_enc);
if (document_open_file(item->data, readonly, ft, forced_enc) != NULL)
has_opened = TRUE;
}

if (has_opened)
notebook_auto_sort_tabs();
}


Expand Down
3 changes: 2 additions & 1 deletion src/keybindings.c
Expand Up @@ -1441,7 +1441,8 @@ static gboolean cb_func_file_action(guint key_id)
{
gchar *utf8_filename = g_queue_peek_head(ui_prefs.recent_queue);
gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
document_open_file(locale_filename, FALSE, NULL, NULL);
if (document_open_file(locale_filename, FALSE, NULL, NULL) != NULL)
notebook_auto_sort_tabs();
g_free(locale_filename);
break;
}
Expand Down
15 changes: 14 additions & 1 deletion src/keyfile.c
Expand Up @@ -43,6 +43,7 @@
#include "geanyobject.h"
#include "main.h"
#include "msgwindow.h"
#include "notebook.h"
#include "prefs.h"
#include "printing.h"
#include "project.h"
Expand Down Expand Up @@ -430,6 +431,8 @@ static void save_dialog_prefs(GKeyFile *config)
g_key_file_set_string(config, PACKAGE, "tagbar_font", interface_prefs.tagbar_font);
g_key_file_set_string(config, PACKAGE, "msgwin_font", interface_prefs.msgwin_font);
g_key_file_set_boolean(config, PACKAGE, "show_notebook_tabs", interface_prefs.show_notebook_tabs);
g_key_file_set_boolean(config, PACKAGE, "auto_sort_tabs_filename", interface_prefs.auto_sort_tabs_filename);
g_key_file_set_boolean(config, PACKAGE, "auto_sort_tabs_pathname", interface_prefs.auto_sort_tabs_pathname);
g_key_file_set_boolean(config, PACKAGE, "show_tab_cross", file_prefs.show_tab_cross);
g_key_file_set_boolean(config, PACKAGE, "tab_order_ltr", file_prefs.tab_order_ltr);
g_key_file_set_boolean(config, PACKAGE, "tab_order_beside", file_prefs.tab_order_beside);
Expand Down Expand Up @@ -777,6 +780,10 @@ static void load_dialog_prefs(GKeyFile *config)
file_prefs.tab_order_ltr = utils_get_setting_boolean(config, PACKAGE, "tab_order_ltr", TRUE);
file_prefs.tab_order_beside = utils_get_setting_boolean(config, PACKAGE, "tab_order_beside", FALSE);
interface_prefs.show_notebook_tabs = utils_get_setting_boolean(config, PACKAGE, "show_notebook_tabs", TRUE);
interface_prefs.auto_sort_tabs_filename = utils_get_setting_boolean(config, PACKAGE, "auto_sort_tabs_filename", FALSE);
interface_prefs.auto_sort_tabs_pathname = utils_get_setting_boolean(config, PACKAGE, "auto_sort_tabs_pathname", FALSE);
if (interface_prefs.auto_sort_tabs_filename && interface_prefs.auto_sort_tabs_pathname)
interface_prefs.auto_sort_tabs_filename = FALSE;
file_prefs.show_tab_cross = utils_get_setting_boolean(config, PACKAGE, "show_tab_cross", TRUE);
interface_prefs.editor_font = utils_get_setting_string(config, PACKAGE, "editor_font", GEANY_DEFAULT_FONT_EDITOR);
interface_prefs.tagbar_font = utils_get_setting_string(config, PACKAGE, "tagbar_font", GEANY_DEFAULT_FONT_SYMBOL_LIST);
Expand Down Expand Up @@ -1216,6 +1223,7 @@ void configuration_open_files(void)
{
gint i;
gboolean failure = FALSE;
gboolean has_opened = FALSE;

/* necessary to set it to TRUE for project session support */
main_status.opening_session_files = TRUE;
Expand All @@ -1228,7 +1236,9 @@ void configuration_open_files(void)

if (tmp != NULL && (len = g_strv_length(tmp)) >= 8)
{
if (! open_session_file(tmp, len))
if (open_session_file(tmp, len))
has_opened = TRUE;
else
failure = TRUE;
}
g_strfreev(tmp);
Expand Down Expand Up @@ -1268,6 +1278,9 @@ void configuration_open_files(void)
gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), target_page);
}
main_status.opening_session_files = FALSE;

if (has_opened)
notebook_auto_sort_tabs();
}


Expand Down
2 changes: 2 additions & 0 deletions src/libmain.c
Expand Up @@ -803,6 +803,8 @@ gboolean main_handle_filename(const gchar *locale_filename)
/* add recent file manually if opening_session_files is set */
if (doc != NULL && main_status.opening_session_files)
ui_add_recent_document(doc);
if (doc != NULL)
notebook_auto_sort_tabs();
g_free(filename);
return TRUE;
}
Expand Down
6 changes: 6 additions & 0 deletions src/msgwindow.c
Expand Up @@ -40,6 +40,7 @@
#include "keybindings.h"
#include "main.h"
#include "navqueue.h"
#include "notebook.h"
#include "prefs.h"
#include "support.h"
#include "ui_utils.h"
Expand Down Expand Up @@ -696,8 +697,13 @@ static gboolean goto_compiler_file_line(const gchar *fname, gint line, gboolean
g_free(utf8_filename);

if (doc == NULL) /* file not already open */
{
doc = document_open_file(filename, FALSE, NULL, NULL);

if (doc != NULL)
notebook_auto_sort_tabs();
}

if (doc != NULL)
{
if (! doc->changed && editor_prefs.use_indicators) /* if modified, line may be wrong */
Expand Down
12 changes: 12 additions & 0 deletions src/notebook.c
Expand Up @@ -852,6 +852,18 @@ void on_sort_tabs_pathname_activate(GtkMenuItem *menuitem, gpointer user_data)
}


void notebook_auto_sort_tabs(void)
{
if (interface_prefs.show_notebook_tabs)
{
if (interface_prefs.auto_sort_tabs_filename)
notebook_sort_tabs(NOTEBOOK_TAB_SORT_FILENAME);
else if (interface_prefs.auto_sort_tabs_pathname)
notebook_sort_tabs(NOTEBOOK_TAB_SORT_PATHNAME);
}
}


static void
on_window_drag_data_received(GtkWidget *widget, GdkDragContext *drag_context,
gint x, gint y, GtkSelectionData *data, guint target_type,
Expand Down
2 changes: 2 additions & 0 deletions src/notebook.h
Expand Up @@ -58,6 +58,8 @@ void on_sort_tabs_filename_activate(GtkMenuItem *menuitem, gpointer user_data);

void on_sort_tabs_pathname_activate(GtkMenuItem *menuitem, gpointer user_data);

void notebook_auto_sort_tabs(void);

G_END_DECLS

#endif /* GEANY_NOTEBOOK_H */
3 changes: 3 additions & 0 deletions src/osx.c
Expand Up @@ -25,6 +25,7 @@
#include "utils.h"
#include "ui_utils.h"
#include "main.h"
#include "notebook.h"


static gboolean app_block_termination_cb(GtkosxApplication *app, gpointer data)
Expand Down Expand Up @@ -73,6 +74,8 @@ static gboolean app_open_file_cb(GtkosxApplication *osx_app, gchar *path, gpoint
else
{
opened = document_open_file(locale_path, FALSE, NULL, NULL) != NULL;
if (opened)
notebook_auto_sort_tabs();
g_free(locale_path);
}

Expand Down
40 changes: 40 additions & 0 deletions src/prefs.c
Expand Up @@ -88,6 +88,8 @@ static gboolean kb_find_duplicate(GtkTreeStore *store, GtkWidget *parent, GtkTre
guint key, GdkModifierType mods, const gchar *shortcut);
static void on_toolbar_show_toggled(GtkToggleButton *togglebutton, gpointer user_data);
static void on_show_notebook_tabs_toggled(GtkToggleButton *togglebutton, gpointer user_data);
static void on_auto_sort_tabs_filename_toggled(GtkToggleButton *togglebutton, gpointer user_data);
static void on_auto_sort_tabs_pathname_toggled(GtkToggleButton *togglebutton, gpointer user_data);
static void on_enable_plugins_toggled(GtkToggleButton *togglebutton, gpointer user_data);
static void on_use_folding_toggled(GtkToggleButton *togglebutton, gpointer user_data);
static void on_open_encoding_toggled(GtkToggleButton *togglebutton, gpointer user_data);
Expand Down Expand Up @@ -489,6 +491,16 @@ static void prefs_init_dialog(void)
on_show_notebook_tabs_toggled(GTK_TOGGLE_BUTTON(
ui_lookup_widget(ui_widgets.prefs_dialog, "check_show_notebook_tabs")), NULL);

widget = ui_lookup_widget(ui_widgets.prefs_dialog, "check_auto_sort_tabs_filename");
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), interface_prefs.auto_sort_tabs_filename);
on_auto_sort_tabs_filename_toggled(GTK_TOGGLE_BUTTON(
ui_lookup_widget(ui_widgets.prefs_dialog, "check_auto_sort_tabs_filename")), NULL);

widget = ui_lookup_widget(ui_widgets.prefs_dialog, "check_auto_sort_tabs_pathname");
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), interface_prefs.auto_sort_tabs_pathname);
on_auto_sort_tabs_pathname_toggled(GTK_TOGGLE_BUTTON(
ui_lookup_widget(ui_widgets.prefs_dialog, "check_auto_sort_tabs_pathname")), NULL);

widget = ui_lookup_widget(ui_widgets.prefs_dialog, "check_show_tab_cross");
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), file_prefs.show_tab_cross);

Expand Down Expand Up @@ -956,6 +968,12 @@ on_prefs_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
widget = ui_lookup_widget(ui_widgets.prefs_dialog, "check_show_notebook_tabs");
interface_prefs.show_notebook_tabs = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));

widget = ui_lookup_widget(ui_widgets.prefs_dialog, "check_auto_sort_tabs_filename");
interface_prefs.auto_sort_tabs_filename = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));

widget = ui_lookup_widget(ui_widgets.prefs_dialog, "check_auto_sort_tabs_pathname");
interface_prefs.auto_sort_tabs_pathname = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));

widget = ui_lookup_widget(ui_widgets.prefs_dialog, "check_show_tab_cross");
file_prefs.show_tab_cross = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));

Expand Down Expand Up @@ -1531,6 +1549,24 @@ static void on_show_notebook_tabs_toggled(GtkToggleButton *togglebutton, gpointe
/* tab placement only enabled when tabs are visible */
gtk_widget_set_sensitive(ui_lookup_widget(ui_widgets.prefs_dialog, "combo_tab_editor"), sens);
gtk_widget_set_sensitive(ui_lookup_widget(ui_widgets.prefs_dialog, "check_show_tab_cross"), sens);
gtk_widget_set_sensitive(ui_lookup_widget(ui_widgets.prefs_dialog, "check_auto_sort_tabs_filename"), sens);
gtk_widget_set_sensitive(ui_lookup_widget(ui_widgets.prefs_dialog, "check_auto_sort_tabs_pathname"), sens);
}


static void on_auto_sort_tabs_filename_toggled(GtkToggleButton *togglebutton, gpointer user_data)
{
if (gtk_toggle_button_get_active(togglebutton))
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ui_lookup_widget(ui_widgets.prefs_dialog,
"check_auto_sort_tabs_pathname")), FALSE);
}


static void on_auto_sort_tabs_pathname_toggled(GtkToggleButton *togglebutton, gpointer user_data)
{
if (gtk_toggle_button_get_active(togglebutton))
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ui_lookup_widget(ui_widgets.prefs_dialog,
"check_auto_sort_tabs_filename")), FALSE);
}


Expand Down Expand Up @@ -1813,6 +1849,10 @@ void prefs_show_dialog(void)
"toggled", G_CALLBACK(on_toolbar_show_toggled), NULL);
g_signal_connect(ui_lookup_widget(ui_widgets.prefs_dialog, "check_show_notebook_tabs"),
"toggled", G_CALLBACK(on_show_notebook_tabs_toggled), NULL);
g_signal_connect(ui_lookup_widget(ui_widgets.prefs_dialog, "check_auto_sort_tabs_filename"),
"toggled", G_CALLBACK(on_auto_sort_tabs_filename_toggled), NULL);
g_signal_connect(ui_lookup_widget(ui_widgets.prefs_dialog, "check_auto_sort_tabs_pathname"),
"toggled", G_CALLBACK(on_auto_sort_tabs_pathname_toggled), NULL);
g_signal_connect(ui_lookup_widget(ui_widgets.prefs_dialog, "check_folding"),
"toggled", G_CALLBACK(on_use_folding_toggled), NULL);
g_signal_connect(ui_lookup_widget(ui_widgets.prefs_dialog, "check_open_encoding"),
Expand Down
8 changes: 8 additions & 0 deletions src/symbols.c
Expand Up @@ -47,6 +47,7 @@
#include "geanyobject.h"
#include "main.h"
#include "navqueue.h"
#include "notebook.h"
#include "sciwrappers.h"
#include "sidebar.h"
#include "support.h"
Expand Down Expand Up @@ -1754,7 +1755,10 @@ static void on_goto_popup_item_activate(GtkMenuItem *item, TMTag *tag)
new_doc = document_open_file(tag->file->file_name, FALSE, NULL, NULL);

if (new_doc)
{
notebook_auto_sort_tabs();
navqueue_goto_line(old_doc, new_doc, tag->line);
}
}


Expand Down Expand Up @@ -2066,8 +2070,12 @@ static gboolean goto_tag(const gchar *name, gboolean definition)
new_doc = document_find_by_real_path(tmtag->file->file_name);

if (!new_doc)
{
/* not found in opened document, should open */
new_doc = document_open_file(tmtag->file->file_name, FALSE, NULL, NULL);
if (new_doc)
notebook_auto_sort_tabs();
}

navqueue_goto_line(old_doc, new_doc, tmtag->line);
}
Expand Down
7 changes: 6 additions & 1 deletion src/ui_utils.c
Expand Up @@ -40,6 +40,7 @@
#include "keyfile.h"
#include "main.h"
#include "msgwindow.h"
#include "notebook.h"
#include "prefs.h"
#include "project.h"
#include "sciwrappers.h"
Expand Down Expand Up @@ -1173,7 +1174,10 @@ static void recent_file_activate_cb(GtkMenuItem *menuitem, G_GNUC_UNUSED gpointe
gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);

if (document_open_file(locale_filename, FALSE, NULL, NULL) != NULL)
{
recent_file_loaded(utf8_filename, recent_get_recent_files());
notebook_auto_sort_tabs();
}

g_free(locale_filename);
g_free(utf8_filename);
Expand Down Expand Up @@ -2082,7 +2086,8 @@ static void on_config_file_clicked(GtkWidget *widget, gpointer user_data)
ft = filetypes[GEANY_FILETYPES_CONF];

if (g_file_test(file_name, G_FILE_TEST_EXISTS))
document_open_file(file_name, FALSE, ft, NULL);
if (document_open_file(file_name, FALSE, ft, NULL) != NULL)
notebook_auto_sort_tabs();
else
{
gchar *utf8_filename = utils_get_utf8_from_locale(file_name);
Expand Down
2 changes: 2 additions & 0 deletions src/ui_utils.h
Expand Up @@ -51,6 +51,8 @@ typedef struct GeanyInterfacePrefs
gchar *tagbar_font; /**< symbol sidebar font */
gchar *msgwin_font; /**< message window font */
gboolean show_notebook_tabs; /**< whether editor tabs are visible */
gboolean auto_sort_tabs_filename; /**< whether to automatically sort tabs based on filename */
gboolean auto_sort_tabs_pathname; /**< whether to automatically sort tabs based on pathname */
gint tab_pos_editor; /**< positions of editor's tabs */
gint tab_pos_msgwin; /**< positions of message window's tabs */
gint tab_pos_sidebar; /**< positions of sidebar's tabs */
Expand Down

0 comments on commit 04eb16b

Please sign in to comment.