>From 5f82052cb7276bd8d1197651636a9c6ceae43f37 Mon Sep 17 00:00:00 2001
From: Colomban Wendling <ban@herbesfolles.org>
Date: Tue, 28 Aug 2012 19:41:42 +0200
Subject: [PATCH] Add setting to make Home and End keys navigate in display
 coordinates

This cannot really be mapped using the keybinding because holding Shift
together with Home or End is expected to behave exactly the same as
without it but extending the selection;  which is tricky to get using
configurable bindings.

For this setting to behave correctly, the Home and End keys should
obviously not be bound to any action, so the default bidings of Home
and End should most probably be removed.  Maybe the "Go to [display]
line start/end" actions should be removed altogether now a global
setting can be used to choose one behavior or the other;  but they can
also be kept but unmapped by default, which would allow a user to map
another key to this action (like <Ctrl>A and <Ctrl>E to emulate a
Bash/Emacs-like behavior).

Closes #3041948.
---
 data/geany.glade |   31 ++++++++++++++++++++++++-------
 src/editor.c     |   20 ++++++++++++++++++--
 src/editor.h     |    1 +
 src/keyfile.c    |    2 ++
 src/prefs.c      |    6 ++++++
 5 files changed, 51 insertions(+), 9 deletions(-)

diff --git a/data/geany.glade b/data/geany.glade
index 675ffbc..54f396e 100644
--- a/data/geany.glade
+++ b/data/geany.glade
@@ -2693,6 +2693,23 @@
                                   </packing>
                                 </child>
                                 <child>
+                                  <object class="GtkCheckButton" id="check_display_home_end">
+                                    <property name="label" translatable="yes">Home and End keys operate on display lines</property>
+                                    <property name="use_action_appearance">False</property>
+                                    <property name="visible">True</property>
+                                    <property name="can_focus">True</property>
+                                    <property name="receives_default">False</property>
+                                    <property name="tooltip_text" translatable="yes">Whether Home and End keys operate on logical or display lines.</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">2</property>
+                                  </packing>
+                                </child>
+                                <child>
                                   <object class="GtkCheckButton" id="check_disable_dnd">
                                     <property name="label" translatable="yes">Disable Drag and Drop</property>
                                     <property name="use_action_appearance">False</property>
@@ -2706,7 +2723,7 @@
                                   <packing>
                                     <property name="expand">False</property>
                                     <property name="fill">False</property>
-                                    <property name="position">2</property>
+                                    <property name="position">3</property>
                                   </packing>
                                 </child>
                                 <child>
@@ -2722,7 +2739,7 @@
                                   <packing>
                                     <property name="expand">False</property>
                                     <property name="fill">False</property>
-                                    <property name="position">3</property>
+                                    <property name="position">4</property>
                                   </packing>
                                 </child>
                                 <child>
@@ -2739,7 +2756,7 @@
                                   <packing>
                                     <property name="expand">False</property>
                                     <property name="fill">False</property>
-                                    <property name="position">4</property>
+                                    <property name="position">5</property>
                                   </packing>
                                 </child>
                                 <child>
@@ -2756,7 +2773,7 @@
                                   <packing>
                                     <property name="expand">False</property>
                                     <property name="fill">False</property>
-                                    <property name="position">5</property>
+                                    <property name="position">6</property>
                                   </packing>
                                 </child>
                                 <child>
@@ -2773,7 +2790,7 @@
                                   <packing>
                                     <property name="expand">False</property>
                                     <property name="fill">False</property>
-                                    <property name="position">6</property>
+                                    <property name="position">7</property>
                                   </packing>
                                 </child>
                                 <child>
@@ -2815,7 +2832,7 @@
                                   <packing>
                                     <property name="expand">True</property>
                                     <property name="fill">True</property>
-                                    <property name="position">7</property>
+                                    <property name="position">8</property>
                                   </packing>
                                 </child>
                                 <child>
@@ -2855,7 +2872,7 @@
                                   <packing>
                                     <property name="expand">True</property>
                                     <property name="fill">True</property>
-                                    <property name="position">8</property>
+                                    <property name="position">9</property>
                                   </packing>
                                 </child>
                               </object>
diff --git a/src/editor.c b/src/editor.c
index ac22e18..abf260b 100644
--- a/src/editor.c
+++ b/src/editor.c
@@ -4895,8 +4895,24 @@ void editor_apply_update_prefs(GeanyEditor *editor)
 	editor_set_indent(editor, editor->indent_type, editor->indent_width);
 	sci_set_tab_indents(sci, editor_prefs.use_tab_to_indent);
 
-	sci_assign_cmdkey(sci, SCK_HOME | (SCMOD_SHIFT << 16),
-		editor_prefs.smart_home_key ? SCI_VCHOMEEXTEND : SCI_HOMEEXTEND);
+	if (editor_prefs.display_home_end)
+	{
+		sci_assign_cmdkey(sci, SCK_HOME,
+			editor_prefs.smart_home_key ? SCI_VCHOMEDISPLAY : SCI_HOMEDISPLAY);
+		sci_assign_cmdkey(sci, SCK_HOME | (SCMOD_SHIFT << 16),
+			editor_prefs.smart_home_key ? SCI_VCHOMEDISPLAYEXTEND : SCI_HOMEDISPLAYEXTEND);
+		sci_assign_cmdkey(sci, SCK_END, SCI_LINEENDDISPLAY);
+		sci_assign_cmdkey(sci, SCK_END | (SCMOD_SHIFT << 16), SCI_LINEENDDISPLAYEXTEND);
+	}
+	else
+	{
+		sci_assign_cmdkey(sci, SCK_HOME, editor_prefs.smart_home_key ? SCI_VCHOME : SCI_HOME);
+		sci_assign_cmdkey(sci, SCK_HOME | (SCMOD_SHIFT << 16),
+			editor_prefs.smart_home_key ? SCI_VCHOMEEXTEND : SCI_HOMEEXTEND);
+		sci_assign_cmdkey(sci, SCK_END, SCI_LINEEND);
+		sci_assign_cmdkey(sci, SCK_END | (SCMOD_SHIFT << 16), SCI_LINEENDEXTEND);
+	}
+
 	sci_assign_cmdkey(sci, SCK_HOME | ((SCMOD_SHIFT | SCMOD_ALT) << 16),
 		editor_prefs.smart_home_key ? SCI_VCHOMERECTEXTEND : SCI_HOMERECTEXTEND);
 
diff --git a/src/editor.h b/src/editor.h
index 9a9f1c7..34836ac 100644
--- a/src/editor.h
+++ b/src/editor.h
@@ -148,6 +148,7 @@ typedef struct GeanyEditorPrefs
 	/* This setting may be overridden when a project is opened. Use @c editor_get_prefs(). */
 	gboolean	long_line_enabled;
 	gint		autocompletion_update_freq;
+	gboolean	display_home_end;
 }
 GeanyEditorPrefs;
 
diff --git a/src/keyfile.c b/src/keyfile.c
index 6513e73..3baa5ff 100644
--- a/src/keyfile.c
+++ b/src/keyfile.c
@@ -429,6 +429,7 @@ static void save_dialog_prefs(GKeyFile *config)
 	g_key_file_set_boolean(config, PACKAGE, "auto_complete_symbols", editor_prefs.auto_complete_symbols);
 	g_key_file_set_boolean(config, PACKAGE, "pref_editor_disable_dnd", editor_prefs.disable_dnd);
 	g_key_file_set_boolean(config, PACKAGE, "pref_editor_smart_home_key", editor_prefs.smart_home_key);
+	g_key_file_set_boolean(config, PACKAGE, "pref_editor_display_home_end", editor_prefs.display_home_end);
 	g_key_file_set_boolean(config, PACKAGE, "pref_editor_newline_strip", editor_prefs.newline_strip);
 	g_key_file_set_integer(config, PACKAGE, "line_break_column", editor_prefs.line_break_column);
 	g_key_file_set_boolean(config, PACKAGE, "auto_continue_multiline", editor_prefs.auto_continue_multiline);
@@ -761,6 +762,7 @@ static void load_dialog_prefs(GKeyFile *config)
 	editor_prefs.show_linenumber_margin = utils_get_setting_boolean(config, PACKAGE, "show_linenumber_margin", TRUE);
 	editor_prefs.disable_dnd = utils_get_setting_boolean(config, PACKAGE, "pref_editor_disable_dnd", FALSE);
 	editor_prefs.smart_home_key = utils_get_setting_boolean(config, PACKAGE, "pref_editor_smart_home_key", TRUE);
+	editor_prefs.display_home_end = utils_get_setting_boolean(config, PACKAGE, "pref_editor_display_home_end", FALSE);
 	editor_prefs.newline_strip = utils_get_setting_boolean(config, PACKAGE, "pref_editor_newline_strip", FALSE);
 	editor_prefs.line_break_column = utils_get_setting_integer(config, PACKAGE, "line_break_column", 72);
 	editor_prefs.auto_continue_multiline = utils_get_setting_boolean(config, PACKAGE, "auto_continue_multiline", TRUE);
diff --git a/src/prefs.c b/src/prefs.c
index 0db0456..1fa3912 100644
--- a/src/prefs.c
+++ b/src/prefs.c
@@ -603,6 +603,9 @@ static void prefs_init_dialog(void)
 	widget = ui_lookup_widget(ui_widgets.prefs_dialog, "check_smart_home");
 	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), editor_prefs.smart_home_key);
 
+	widget = ui_lookup_widget(ui_widgets.prefs_dialog, "check_display_home_end");
+	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), editor_prefs.display_home_end);
+
 	widget = ui_lookup_widget(ui_widgets.prefs_dialog, "check_newline_strip");
 	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), editor_prefs.newline_strip);
 
@@ -1069,6 +1072,9 @@ on_prefs_dialog_response(GtkDialog *dialog, gint response, gpointer user_data)
 		widget = ui_lookup_widget(ui_widgets.prefs_dialog, "check_smart_home");
 		editor_prefs.smart_home_key = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
 
+		widget = ui_lookup_widget(ui_widgets.prefs_dialog, "check_display_home_end");
+		editor_prefs.display_home_end = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
+
 		widget = ui_lookup_widget(ui_widgets.prefs_dialog, "check_newline_strip");
 		editor_prefs.newline_strip = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
 
-- 
1.7.10.4

