Index: gtksourceview.c
===================================================================
RCS file: /cvs/gnome/gtksourceview/gtksourceview/gtksourceview.c,v
retrieving revision 1.37
diff -u -r1.37 gtksourceview.c
--- gtksourceview.c	1 Mar 2005 10:44:01 -0000	1.37
+++ gtksourceview.c	21 Apr 2005 12:54:36 -0000
@@ -1821,31 +1821,96 @@
 		}
 	}
 
-	if ((key == GDK_Tab) && view->priv->insert_spaces)
+	if (key == GDK_Tab)
 	{
-		gint cur_pos;
-		gint num_of_equivalent_spaces;
-		gint tabs_size;
-		gchar *spaces;
-
-		tabs_size = view->priv->tabs_width; 
-		
-		cur_pos = gtk_text_iter_get_line_offset (&cur);
-
-		num_of_equivalent_spaces = tabs_size - (cur_pos % tabs_size); 
-
-		spaces = g_strnfill (num_of_equivalent_spaces, ' ');
-		
-		gtk_text_buffer_begin_user_action (buf);
-		gtk_text_buffer_insert (buf,  &cur, spaces, num_of_equivalent_spaces);
-		gtk_text_buffer_end_user_action (buf);
-
-		gtk_text_view_scroll_mark_onscreen (GTK_TEXT_VIEW (widget),
-						    gtk_text_buffer_get_insert (buf));
-		
-		g_free (spaces);
+		GtkTextIter start, end;
 
-		return TRUE;
+		/* If there is a selection, increase indent. If not, insert tab/spaces. */
+		if (gtk_text_buffer_get_selection_bounds (buf, &start, &end))
+		{
+			gchar *selection;
+			gchar **selection_lines;
+			gchar *tab_or_spaces;
+			GString *indented;
+			gint offset_begin;
+			gint i;
+
+			/* Get the buffer and indent each line. */
+			selection = gtk_text_buffer_get_text (buf, &start, &end, TRUE);
+			selection_lines = g_strsplit (selection, "\n", -1);
+			indented = g_string_new ("");
+
+			if (view->priv->insert_spaces)
+			{
+				gint tabs_size;
+				gint cur_pos;
+				gint num_of_equivalent_spaces;
+
+				tabs_size = view->priv->tabs_width;
+
+				cur_pos = gtk_text_iter_get_line_offset (&cur);
+
+				num_of_equivalent_spaces = tabs_size - (cur_pos % tabs_size);
+
+				tab_or_spaces = g_strnfill (num_of_equivalent_spaces, ' ');
+			} else
+				tab_or_spaces = g_strdup ("\t");
+
+			i = 0;
+			while (selection_lines[i] != NULL)
+			{
+				g_string_append_printf (indented, "%s%s\n",
+						tab_or_spaces, selection_lines[i]);
+				i++;
+			}
+
+			/* Remove trailing newline. */
+			g_string_truncate (indented, indented->len - 1);
+
+			/* Now insert the new indented text. */
+			gtk_text_buffer_begin_user_action (buf);
+
+			offset_begin = gtk_text_iter_get_offset (&start);
+			gtk_text_buffer_delete (buf, &start, &end);
+
+			gtk_text_buffer_insert_at_cursor (buf, indented->str, indented->len);
+			gtk_text_buffer_get_iter_at_offset (buf, &start, offset_begin);
+			gtk_text_buffer_get_iter_at_offset (buf, &end, offset_begin + indented->len);
+			gtk_text_buffer_select_range (buf, &start, &end);
+
+			gtk_text_buffer_end_user_action (buf);
+
+			g_free (tab_or_spaces);
+			g_free (selection);
+			g_strfreev (selection_lines);
+			g_string_free (indented, TRUE);
+			return TRUE;
+		}
+		else if (view->priv->insert_spaces)
+		{
+			gint cur_pos;
+			gint num_of_equivalent_spaces;
+			gint tabs_size;
+			gchar *spaces;
+
+			tabs_size = view->priv->tabs_width;
+
+			cur_pos = gtk_text_iter_get_line_offset (&cur);
+
+			num_of_equivalent_spaces = tabs_size - (cur_pos % tabs_size);
+
+			spaces = g_strnfill (num_of_equivalent_spaces, ' ');
+
+			gtk_text_buffer_begin_user_action (buf);
+			gtk_text_buffer_insert (buf,  &cur, spaces, num_of_equivalent_spaces);
+			gtk_text_buffer_end_user_action (buf);
+
+			gtk_text_view_scroll_mark_onscreen (GTK_TEXT_VIEW (widget),
+							    gtk_text_buffer_get_insert (buf));
+
+			g_free (spaces);
+			return TRUE;
+		}
 	}
 		
 	return FALSE;
