[Github-comments] [geany-plugins] Add new "Lineoperations" plugin (#324)

Colomban Wendling notifications at xxxxx
Sun Jan 17 14:11:20 UTC 2016


> +	GtkWidget *sortdesc_item;
> +
> +	main_menu_item = gtk_menu_item_new_with_mnemonic(_("_Line Operations"));
> +	gtk_widget_show(main_menu_item);
> +
> +	submenu = gtk_menu_new();
> +	gtk_widget_show(submenu);
> +	sep1 = gtk_separator_menu_item_new();
> +	sep2 = gtk_separator_menu_item_new();
> +	rmdupst_item  = gtk_menu_item_new_with_mnemonic(_("Remove Duplicate Lines, _Sorted"));
> +	rmdupln_item  = gtk_menu_item_new_with_mnemonic(_("Remove Duplicate Lines, _Ordered"));
> +	rmunqln_item  = gtk_menu_item_new_with_mnemonic(_("Remove _Unique Lines"));
> +	rmemtyln_item = gtk_menu_item_new_with_mnemonic(_("Remove _Empty Lines"));
> +	rmwhspln_item = gtk_menu_item_new_with_mnemonic(_("Remove _Whitespace Lines"));
> +	sortasc_item  = gtk_menu_item_new_with_mnemonic(_("Sort Lines _Ascending"));
> +	sortdesc_item = gtk_menu_item_new_with_mnemonic(_("Sort Lines _Descending"));

similarly, there's quite a lot of duplication in the menu building code, while it's basically always exactly the same.  you could easily lower duplication with e.g. representing the items to add like that:
```C

	struct
	{
		const gchar *label;
		GCallback cb_activate;
		gpointer cb_data;
	}
	menu_items[] =
	{
		{ N_("Remove Duplicate Lines, _Sorted"),
		  G_CALLBACK(action_func_item), rmdupst },
		{ N_("Remove Duplicate Lines, _Ordered"),
		  G_CALLBACK(action_func_item), rmdupln },
		{ N_("Remove _Unique Lines"),
		  G_CALLBACK(action_func_item), rmunqln },
		{ NULL },
		{ N_("Remove _Empty Lines"),
		  G_CALLBACK(action_func_item), rmemtyln },
		{ N_("Remove _Whitespace Lines"),
		  G_CALLBACK(action_func_item), rmwhspln },
		{ NULL },
		{ N_("Sort Lines _Ascending"),
		  G_CALLBACK(action_sort_item), GINT_TO_POINTER(1) },
		{ N_("Sort Lines _Descending"),
		  G_CALLBACK(action_sort_item), GINT_TO_POINTER(0) }
	};
```
and adding them in a loop
```C
for (i = 0; i < G_N_ELEMENTS(menu_items); i++)
	{
		GtkWidget *item;

		if (! menu_items[i].label) /* separator */
			item = gtk_separator_menu_item_new();
		else
		{
			item = gtk_menu_item_new_with_mnemonic(_(menu_items[i].label));
			g_signal_connect(item, "activate", menu_items[i].cb_activate, menu_items[i].cb_data);
			ui_add_document_sensitive(item);
		}

		gtk_widget_show(item);
		gtk_menu_shell_append(GTK_MENU_SHELL(submenu), item);
	}
```

---
Reply to this email directly or view it on GitHub:
https://github.com/geany/geany-plugins/pull/324/files#r49946757
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.geany.org/pipermail/github-comments/attachments/20160117/e575d67e/attachment.html>


More information about the Github-comments mailing list