SF.net SVN: geany: [1705] trunk

eht16 at users.sourceforge.net eht16 at xxxxx
Mon Jul 16 15:42:12 UTC 2007


Revision: 1705
          http://svn.sourceforge.net/geany/?rev=1705&view=rev
Author:   eht16
Date:     2007-07-16 08:42:12 -0700 (Mon, 16 Jul 2007)

Log Message:
-----------
Add keybindings for smart indent and indent/deindent by one space.
Fix possible selection errors on commenting multiple lines when using CR/LF line endings.

Modified Paths:
--------------
    trunk/ChangeLog
    trunk/doc/geany.docbook
    trunk/src/document.c
    trunk/src/editor.c
    trunk/src/editor.h
    trunk/src/keybindings.c
    trunk/src/keybindings.h

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2007-07-16 14:49:48 UTC (rev 1704)
+++ trunk/ChangeLog	2007-07-16 15:42:12 UTC (rev 1705)
@@ -1,3 +1,13 @@
+2007-07-16  Enrico Tröger  <enrico.troeger at uvena.de>
+
+ * src/document.c, src/editor.c:
+   Fix possible selection errors on commenting multiple lines when using
+   CR/LF line endings.
+ * doc/geany.docbook, src/editor.c, src/editor.h, src/keybindings.c,
+   src/keybindings.h:
+   Add keybindings for smart indent and indent/deindent by one space.
+
+
 2007-07-16  Nick Treleaven  <nick.treleaven at btinternet.com>
 
  * src/dialogs.c, src/callbacks.c:

Modified: trunk/doc/geany.docbook
===================================================================
--- trunk/doc/geany.docbook	2007-07-16 14:49:48 UTC (rev 1704)
+++ trunk/doc/geany.docbook	2007-07-16 15:42:12 UTC (rev 1705)
@@ -1885,14 +1885,29 @@
 							</row>
 							<row>
 								<entry>Increase indent</entry>
-								<entry>Indents the current line or selection by one tabulator.</entry>
+								<entry>Indents the current line or selection by one tabulator or
+									   by spaces in the amount of the tab width setting.</entry>
 							</row>
 							<row>
 								<entry>Decrease indent</entry>
-								<entry>Removes one tabulator from the indentation of the current
-									   line or selection.</entry>
+								<entry>Removes one tabulator or the amount fo spaces of the tab
+									   width setting from the indentation of the current line or
+									   selection.</entry>
 							</row>
 							<row>
+								<entry>Increase indent by one space</entry>
+								<entry>Indents the current line or selection by one space.</entry>
+							</row>
+							<row>
+								<entry>Decrease indent by one space</entry>
+								<entry>Deindents the current line or selection by one space.</entry>
+							</row>
+							<row>
+								<entry>Smart line indent</entry>
+								<entry>Indents the current line or all selected lines with the same
+									   intentation as the previous line.</entry>
+							</row>
+							<row>
 								<entry>Goto matching brace</entry>
 								<entry>If the cursor is ahead or behind a brace, then it is moved to
 									   the brace which belongs to the current one. If this keyboard

Modified: trunk/src/document.c
===================================================================
--- trunk/src/document.c	2007-07-16 14:49:48 UTC (rev 1704)
+++ trunk/src/document.c	2007-07-16 15:42:12 UTC (rev 1705)
@@ -1418,7 +1418,8 @@
 
 		first_line = sci_get_line_from_position(doc_list[idx].sci, selection_start);
 		// Find the last line with chars selected (not EOL char)
-		last_line = sci_get_line_from_position(doc_list[idx].sci, selection_end - 1);
+		last_line = sci_get_line_from_position(doc_list[idx].sci,
+			selection_end - utils_get_eol_char_len(idx));
 		last_line = MAX(first_line, last_line);
 		for (line = first_line; line < (first_line + selected_lines); line++)
 		{

Modified: trunk/src/editor.c
===================================================================
--- trunk/src/editor.c	2007-07-16 14:49:48 UTC (rev 1704)
+++ trunk/src/editor.c	2007-07-16 15:42:12 UTC (rev 1705)
@@ -1476,7 +1476,8 @@
 
 		first_line = sci_get_line_from_position(doc_list[idx].sci, sel_start);
 		// Find the last line with chars selected (not EOL char)
-		last_line = sci_get_line_from_position(doc_list[idx].sci, sel_end - 1);
+		last_line = sci_get_line_from_position(doc_list[idx].sci,
+			sel_end - utils_get_eol_char_len(idx));
 		last_line = MAX(first_line, last_line);
 	}
 	else
@@ -1636,7 +1637,7 @@
 		sci_get_selection_start(doc_list[idx].sci));
 	// Find the last line with chars selected (not EOL char)
 	last_line = sci_get_line_from_position(doc_list[idx].sci,
-		sci_get_selection_end(doc_list[idx].sci) - 1);
+		sci_get_selection_end(doc_list[idx].sci) - utils_get_eol_char_len(idx));
 	last_line = MAX(first_line, last_line);
 
 	// detection of HTML vs PHP code, if non-PHP set filetype to XML
@@ -1802,7 +1803,8 @@
 
 		first_line = sci_get_line_from_position(doc_list[idx].sci, sel_start);
 		// Find the last line with chars selected (not EOL char)
-		last_line = sci_get_line_from_position(doc_list[idx].sci, sel_end - 1);
+		last_line = sci_get_line_from_position(doc_list[idx].sci,
+			sel_end - utils_get_eol_char_len(idx));
 		last_line = MAX(first_line, last_line);
 	}
 	else
@@ -2409,6 +2411,124 @@
 }
 
 
+// simple auto indentation to indent the current line with the same indent as the previous one
+void editor_auto_line_indentation(gint idx, gint pos)
+{
+	gint i, first_line, last_line;
+	gint first_sel_start, first_sel_end, sel_start, sel_end;
+
+	g_return_if_fail(DOC_IDX_VALID(idx));
+
+	first_sel_start = sci_get_selection_start(doc_list[idx].sci);
+	first_sel_end = sci_get_selection_end(doc_list[idx].sci);
+
+	first_line = sci_get_line_from_position(doc_list[idx].sci, first_sel_start);
+	// Find the last line with chars selected (not EOL char)
+	last_line = sci_get_line_from_position(doc_list[idx].sci,
+		first_sel_end - utils_get_eol_char_len(idx));
+	last_line = MAX(first_line, last_line);
+
+	if (pos == -1)
+		pos = first_sel_start;
+
+	// get previous line and use it for get_indent to use that line
+	// (otherwise it would fail on a line only containing "{" in advanced indentation mode)
+	get_indent(doc_list[idx].sci,
+		sci_get_position_from_line(doc_list[idx].sci, first_line - 1), TRUE);
+	SSM(doc_list[idx].sci, SCI_BEGINUNDOACTION, 0, 0);
+
+	for (i = first_line; i <= last_line; i++)
+	{
+		// skip the first line or if the indentation of the previous and current line are equal
+		if (i == 0 ||
+			SSM(doc_list[idx].sci, SCI_GETLINEINDENTATION, i - 1, 0) ==
+			SSM(doc_list[idx].sci, SCI_GETLINEINDENTATION, i, 0))
+			continue;
+
+		sel_start = SSM(doc_list[idx].sci, SCI_POSITIONFROMLINE, i, 0);
+		sel_end = SSM(doc_list[idx].sci, SCI_GETLINEINDENTPOSITION, i, 0);
+		if (sel_start < sel_end)
+		{
+			SSM(doc_list[idx].sci, SCI_SETSEL, sel_start, sel_end);
+			SSM(doc_list[idx].sci, SCI_DELETEBACK, 0, 0);
+		}
+		sci_insert_text(doc_list[idx].sci, sel_start, indent);
+	}
+
+	// set cursor position if there was no selection
+	/// TODO implement selection handling if there was a selection
+	if (first_sel_start == first_sel_end)
+		sci_set_current_position(doc_list[idx].sci,
+			pos - (sel_end - sel_start) + strlen(indent), FALSE);
+
+	SSM(doc_list[idx].sci, SCI_ENDUNDOACTION, 0, 0);
+}
+
+
+// increase / decrease current line or selection by one space
+void editor_indentation_by_one_space(gint idx, gint pos, gboolean decrease)
+{
+	gint i, first_line, last_line, line_start, indentation_end, count = 0;
+	gint sel_start, sel_end, first_line_offset = 0;
+
+	g_return_if_fail(DOC_IDX_VALID(idx));
+
+	sel_start = sci_get_selection_start(doc_list[idx].sci);
+	sel_end = sci_get_selection_end(doc_list[idx].sci);
+
+	first_line = sci_get_line_from_position(doc_list[idx].sci, sel_start);
+	// Find the last line with chars selected (not EOL char)
+	last_line = sci_get_line_from_position(doc_list[idx].sci,
+		sel_end - utils_get_eol_char_len(idx));
+	last_line = MAX(first_line, last_line);
+
+	if (pos == -1)
+		pos = sel_start;
+
+	SSM(doc_list[idx].sci, SCI_BEGINUNDOACTION, 0, 0);
+
+	for (i = first_line; i <= last_line; i++)
+	{
+		indentation_end = SSM(doc_list[idx].sci, SCI_GETLINEINDENTPOSITION, i, 0);
+		if (decrease)
+		{
+			line_start = SSM(doc_list[idx].sci, SCI_POSITIONFROMLINE, i, 0);
+			// searching backwards for a space to remove
+			while (sci_get_char_at(doc_list[idx].sci, indentation_end) != ' ' &&
+				   indentation_end > line_start)
+				indentation_end--;
+
+			if (sci_get_char_at(doc_list[idx].sci, indentation_end) == ' ')
+			{
+				sci_set_current_position(doc_list[idx].sci, indentation_end + 1, FALSE);
+				SSM(doc_list[idx].sci, SCI_DELETEBACK, 0, 0);
+				count--;
+				if (i == first_line)
+					first_line_offset = -1;
+			}
+		}
+		else
+		{
+			sci_insert_text(doc_list[idx].sci, indentation_end, " ");
+			count++;
+			if (i == first_line)
+				first_line_offset = 1;
+		}
+	}
+
+	// set cursor position
+	if (sel_start < sel_end)
+	{
+		sci_set_selection_start(doc_list[idx].sci, sel_start + first_line_offset);
+		sci_set_selection_end(doc_list[idx].sci, sel_end + count);
+	}
+	else
+		sci_set_current_position(doc_list[idx].sci, pos + count, FALSE);
+
+	SSM(doc_list[idx].sci, SCI_ENDUNDOACTION, 0, 0);
+}
+
+
 void editor_finalize()
 {
 	g_hash_table_destroy(editor_prefs.auto_completions);

Modified: trunk/src/editor.h
===================================================================
--- trunk/src/editor.h	2007-07-16 14:49:48 UTC (rev 1704)
+++ trunk/src/editor.h	2007-07-16 15:42:12 UTC (rev 1705)
@@ -143,6 +143,10 @@
 
 void editor_insert_alternative_whitespace(ScintillaObject *sci);
 
+void editor_auto_line_indentation(gint idx, gint pos);
+
+void editor_indentation_by_one_space(gint idx, gint pos, gboolean decrease);
+
 void editor_finalize();
 
 #endif

Modified: trunk/src/keybindings.c
===================================================================
--- trunk/src/keybindings.c	2007-07-16 14:49:48 UTC (rev 1704)
+++ trunk/src/keybindings.c	2007-07-16 15:42:12 UTC (rev 1705)
@@ -266,6 +266,12 @@
 		GDK_i, GDK_CONTROL_MASK, "edit_increaseindent", _("Increase indent"));
 	keys[GEANY_KEYS_EDIT_DECREASEINDENT] = fill(cb_func_edit,
 		GDK_i, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "edit_decreaseindent", _("Decrease indent"));
+	keys[GEANY_KEYS_EDIT_INCREASEINDENTBYSPACE] = fill(cb_func_edit,
+		0, 0, "edit_increaseindentbyspace", _("Increase indent by one space"));
+	keys[GEANY_KEYS_EDIT_DECREASEINDENTBYSPACE] = fill(cb_func_edit,
+		0, 0, "edit_decreaseindentbyspace", _("Decrease indent by one space"));
+	keys[GEANY_KEYS_EDIT_AUTOINDENT] = fill(cb_func_edit,
+		0, 0, "edit_autoindent", _("Smart line indent"));
 	keys[GEANY_KEYS_EDIT_SENDTOCMD1] = fill(cb_func_edit,
 		GDK_1, GDK_CONTROL_MASK, "edit_sendtocmd1", _("Send to Custom Command 1"));
 	keys[GEANY_KEYS_EDIT_SENDTOCMD2] = fill(cb_func_edit,
@@ -1145,6 +1151,15 @@
 		case GEANY_KEYS_EDIT_DECREASEINDENT:
 			on_menu_decrease_indent1_activate(NULL, NULL);
 			break;
+		case GEANY_KEYS_EDIT_INCREASEINDENTBYSPACE:
+			editor_indentation_by_one_space(idx, -1, FALSE);
+			break;
+		case GEANY_KEYS_EDIT_DECREASEINDENTBYSPACE:
+			editor_indentation_by_one_space(idx, -1, TRUE);
+			break;
+		case GEANY_KEYS_EDIT_AUTOINDENT:
+			editor_auto_line_indentation(idx, -1);
+			break;
 		case GEANY_KEYS_EDIT_TOLOWERCASE:
 			on_to_lower_case1_activate(NULL, NULL);
 			break;

Modified: trunk/src/keybindings.h
===================================================================
--- trunk/src/keybindings.h	2007-07-16 14:49:48 UTC (rev 1704)
+++ trunk/src/keybindings.h	2007-07-16 15:42:12 UTC (rev 1705)
@@ -127,6 +127,9 @@
 	GEANY_KEYS_EDIT_UNCOMMENTLINE,
 	GEANY_KEYS_EDIT_INCREASEINDENT,
 	GEANY_KEYS_EDIT_DECREASEINDENT,
+	GEANY_KEYS_EDIT_INCREASEINDENTBYSPACE,
+	GEANY_KEYS_EDIT_DECREASEINDENTBYSPACE,
+	GEANY_KEYS_EDIT_AUTOINDENT,
 	GEANY_KEYS_EDIT_SENDTOCMD1,
 	GEANY_KEYS_EDIT_SENDTOCMD2,
 	GEANY_KEYS_EDIT_SENDTOCMD3,


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.



More information about the Commits mailing list