Hello all,
i already searched for this feature, in case someone else was already working on it or if it simply was a bad idea to implement. For now, i haven't found anything nor something against it so i implemented this simple feature, that basically ensure no mixed line endings control characters (le-cc) will be saved in the document, patch is attached (i used the latest SVN head, 5089). Let me explain the use case: i'm currently working on a project where some of the files contains mixed line endings control characters, ie., some lines end with just CR, other with just LF: there is an easy way to convert everyone of these control charaters to something else via "Document->Set line endings->Convert and set ..." but this isn't something i always manage to remember to do and, better, i would like the editor to take care of that for me. So this patch add just that: in the "Preferences->Files->Saving files" dialog i added a checkbox and, if checked, the editor will always convert any le-cc to the one currently actively on the document via sci_get_eol_mode(doc->editor->sci). I also would like to propose a change in the way le-cc are presented to the user, but i'll do a new post eventually.

Regards,
Manuel
Index: src/interface.c
===================================================================
--- src/interface.c	(revision 5089)
+++ src/interface.c	(working copy)
@@ -2551,6 +2551,7 @@
   GtkWidget *alignment3;
   GtkWidget *vbox6;
   GtkWidget *check_new_line;
+  GtkWidget *check_ensure_convert_new_lines;
   GtkWidget *check_trailing_spaces;
   GtkWidget *check_replace_tabs;
   GtkWidget *label19;
@@ -4157,6 +4158,11 @@
   gtk_box_pack_start (GTK_BOX (vbox6), check_new_line, FALSE, FALSE, 0);
   gtk_tooltips_set_tip (tooltips, check_new_line, _("Ensures that at the end of the file is a new line"), NULL);
 
+  check_ensure_convert_new_lines = gtk_check_button_new_with_mnemonic (_("Always convert and set new line endings before saving"));
+  gtk_widget_show (check_ensure_convert_new_lines);
+  gtk_box_pack_start (GTK_BOX (vbox6), check_ensure_convert_new_lines, FALSE, FALSE, 0);
+  gtk_tooltips_set_tip (tooltips, check_ensure_convert_new_lines, _("Ensures that new line characters get always converted before saving, avoiding mixed line endings in the same file"), NULL);
+
   check_trailing_spaces = gtk_check_button_new_with_mnemonic (_("Strip trailing spaces and tabs"));
   gtk_widget_show (check_trailing_spaces);
   gtk_box_pack_start (GTK_BOX (vbox6), check_trailing_spaces, FALSE, FALSE, 0);
@@ -5020,6 +5026,7 @@
   GLADE_HOOKUP_OBJECT (prefs_dialog, alignment3, "alignment3");
   GLADE_HOOKUP_OBJECT (prefs_dialog, vbox6, "vbox6");
   GLADE_HOOKUP_OBJECT (prefs_dialog, check_new_line, "check_new_line");
+  GLADE_HOOKUP_OBJECT (prefs_dialog, check_ensure_convert_new_lines, "check_ensure_convert_new_lines");
   GLADE_HOOKUP_OBJECT (prefs_dialog, check_trailing_spaces, "check_trailing_spaces");
   GLADE_HOOKUP_OBJECT (prefs_dialog, check_replace_tabs, "check_replace_tabs");
   GLADE_HOOKUP_OBJECT (prefs_dialog, label19, "label19");
Index: src/prefs.c
===================================================================
--- src/prefs.c	(revision 5089)
+++ src/prefs.c	(working copy)
@@ -520,6 +520,9 @@
 	widget = ui_lookup_widget(ui_widgets.prefs_dialog, "check_new_line");
 	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), file_prefs.final_new_line);
 
+	widget = ui_lookup_widget(ui_widgets.prefs_dialog, "check_ensure_convert_new_lines");
+	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), file_prefs.ensure_convert_new_lines);
+
 	/* Editor settings */
 	widget = ui_lookup_widget(ui_widgets.prefs_dialog, "entry_toggle_mark");
 	gtk_entry_set_text(GTK_ENTRY(widget), editor_prefs.comment_toggle_mark);
@@ -926,6 +929,9 @@
 		widget = ui_lookup_widget(ui_widgets.prefs_dialog, "check_new_line");
 		file_prefs.final_new_line = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
 
+		widget = ui_lookup_widget(ui_widgets.prefs_dialog, "check_ensure_convert_new_lines");
+		file_prefs.ensure_convert_new_lines = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
+
 		widget = ui_lookup_widget(ui_widgets.prefs_dialog, "check_replace_tabs");
 		file_prefs.replace_tabs = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
 
Index: src/keyfile.c
===================================================================
--- src/keyfile.c	(revision 5089)
+++ src/keyfile.c	(working copy)
@@ -406,6 +406,7 @@
 		g_key_file_set_string(config, PACKAGE, "pref_editor_default_open_encoding", encodings[file_prefs.default_open_encoding].charset);
 	g_key_file_set_integer(config, PACKAGE, "default_eol_character", file_prefs.default_eol_character);
 	g_key_file_set_boolean(config, PACKAGE, "pref_editor_new_line", file_prefs.final_new_line);
+	g_key_file_set_boolean(config, PACKAGE, "pref_editor_ensure_convert_line_endings", file_prefs.ensure_convert_new_lines);
 	g_key_file_set_boolean(config, PACKAGE, "pref_editor_replace_tabs", file_prefs.replace_tabs);
 	g_key_file_set_boolean(config, PACKAGE, "pref_editor_trail_space", file_prefs.strip_trailing_spaces);
 
@@ -724,6 +725,7 @@
 	}
 	file_prefs.default_eol_character = utils_get_setting_integer(config, PACKAGE, "default_eol_character", GEANY_DEFAULT_EOL_CHARACTER);
 	file_prefs.replace_tabs = utils_get_setting_boolean(config, PACKAGE, "pref_editor_replace_tabs", FALSE);
+	file_prefs.ensure_convert_new_lines = utils_get_setting_boolean(config, PACKAGE, "pref_editor_ensure_convert_line_endings", FALSE);
 	file_prefs.final_new_line = utils_get_setting_boolean(config, PACKAGE, "pref_editor_new_line", TRUE);
 	file_prefs.strip_trailing_spaces = utils_get_setting_boolean(config, PACKAGE, "pref_editor_trail_space", FALSE);
 
Index: src/document.c
===================================================================
--- src/document.c	(revision 5089)
+++ src/document.c	(working copy)
@@ -1781,6 +1781,9 @@
 	/* ensure the file has a newline at the end */
 	if (file_prefs.final_new_line)
 		editor_ensure_final_newline(doc->editor);
+	/* ensure the file's newlines chars get converted to the document defaults/detected (avoid mixed newlines chars in the same document) */
+	if (file_prefs.ensure_convert_new_lines)
+		sci_convert_eols(doc->editor->sci, sci_get_eol_mode(doc->editor->sci));
 
 	/* notify plugins which may wish to modify the document before it's saved */
 	g_signal_emit_by_name(geany_object, "document-before-save", doc);
Index: src/document.h
===================================================================
--- src/document.h	(revision 5089)
+++ src/document.h	(working copy)
@@ -48,6 +48,7 @@
 	gint			default_new_encoding;
 	gint			default_open_encoding;
 	gboolean		final_new_line;
+	gboolean		ensure_convert_new_lines;
 	gboolean		strip_trailing_spaces;
 	gboolean		replace_tabs;
 	gboolean		tab_order_ltr;
Index: geany.glade
===================================================================
--- geany.glade	(revision 5089)
+++ geany.glade	(working copy)
@@ -7709,6 +7709,26 @@
 			  </child>
 
 			  <child>
+			    <widget class="GtkCheckButton" id="check_ensure_convert_new_lines">
+			      <property name="visible">True</property>
+			      <property name="tooltip" translatable="yes">Ensures that new line characters get always converted before saving, avoiding mixed line endings in the same file</property>
+			      <property name="can_focus">True</property>
+			      <property name="label" translatable="yes">Always convert and set new line endings before saving</property>
+			      <property name="use_underline">True</property>
+			      <property name="relief">GTK_RELIEF_NORMAL</property>
+			      <property name="focus_on_click">True</property>
+			      <property name="active">False</property>
+			      <property name="inconsistent">False</property>
+			      <property name="draw_indicator">True</property>
+			    </widget>
+			    <packing>
+			      <property name="padding">0</property>
+			      <property name="expand">False</property>
+			      <property name="fill">False</property>
+			    </packing>
+			  </child>
+
+			  <child>
 			    <widget class="GtkCheckButton" id="check_trailing_spaces">
 			      <property name="visible">True</property>
 			      <property name="tooltip" translatable="yes">Removes trailing spaces and tabs and the end of lines</property>
_______________________________________________
Geany-devel mailing list
Geany-devel@uvena.de
http://lists.uvena.de/cgi-bin/mailman/listinfo/geany-devel

Reply via email to