SF.net SVN: geany: [572] trunk

eht16 at users.sourceforge.net eht16 at xxxxx
Mon Jul 17 10:42:38 UTC 2006


Revision: 572
Author:   eht16
Date:     2006-07-17 03:42:26 -0700 (Mon, 17 Jul 2006)
ViewCVS:  http://svn.sourceforge.net/geany/?rev=572&view=rev

Log Message:
-----------
Moved document related functions from utils.c to document.c.
Keep cursor position when replacing tabs by spaces.

Modified Paths:
--------------
    trunk/ChangeLog
    trunk/src/callbacks.c
    trunk/src/document.c
    trunk/src/document.h
    trunk/src/utils.c
    trunk/src/utils.h
Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2006-07-16 22:02:31 UTC (rev 571)
+++ trunk/ChangeLog	2006-07-17 10:42:26 UTC (rev 572)
@@ -1,3 +1,10 @@
+2006-07-17  Enrico Tröger  <enrico.troeger at uvena.de>
+
+ * src/utils.c, src/document.c, src/callbacks.c:
+   Moved document related functions from utils.c to document.c.
+   Keep cursor position when replacing tabs by spaces.
+
+
 2006-07-16  Enrico Tröger  <enrico.troeger at uvena.de>
 
  * src/encodings.c:

Modified: trunk/src/callbacks.c
===================================================================
--- trunk/src/callbacks.c	2006-07-16 22:02:31 UTC (rev 571)
+++ trunk/src/callbacks.c	2006-07-17 10:42:26 UTC (rev 572)
@@ -1110,8 +1110,7 @@
 {
 	gint idx = document_get_cur_idx();
 
-	if (idx == -1 || ! doc_list[idx].is_valid) return;
-	utils_replace_tabs(idx);
+	document_replace_tabs(idx);
 }
 
 

Modified: trunk/src/document.c
===================================================================
--- trunk/src/document.c	2006-07-16 22:02:31 UTC (rev 571)
+++ trunk/src/document.c	2006-07-17 10:42:26 UTC (rev 572)
@@ -592,11 +592,11 @@
 	}
 
 	// replaces tabs by spaces
-	if (app->pref_editor_replace_tabs) utils_replace_tabs(idx);
+	if (app->pref_editor_replace_tabs) document_replace_tabs(idx);
 	// strip trailing spaces
-	if (app->pref_editor_trail_space) utils_strip_trailing_spaces(idx);
+	if (app->pref_editor_trail_space) document_strip_trailing_spaces(idx);
 	// ensure the file has a newline at the end
-	if (app->pref_editor_new_line) utils_ensure_final_newline(idx);
+	if (app->pref_editor_new_line) document_ensure_final_newline(idx);
 	// ensure there a really the same EOL chars
 	sci_convert_eols(doc_list[idx].sci, sci_get_eol_mode(doc_list[idx].sci));
 
@@ -1158,3 +1158,118 @@
 	}
 	g_free(cmdline);
 }
+
+
+void document_replace_tabs(gint idx)
+{
+	gint i, len, j = 0, tabs_amount = 0, tab_w, pos;
+	gchar *data, *replacement, *text;
+
+	if (idx < 0 || ! doc_list[idx].is_valid) return;
+
+	pos = sci_get_current_position(doc_list[idx].sci);
+	tab_w = sci_get_tab_width(doc_list[idx].sci);
+	replacement = g_malloc(tab_w + 1);
+
+	// get the text
+	len = sci_get_length(doc_list[idx].sci) + 1;
+	data = g_malloc(len);
+	sci_get_text(doc_list[idx].sci, len, data);
+
+	// prepare the spaces with the width of a tab
+	for (i = 0; i < tab_w; i++) replacement[i] = ' ';
+	replacement[tab_w] = '\0';
+
+	for (i = 0; i < len; i++) if (data[i] == 9) tabs_amount++;
+
+	// if there are no tabs, just return and leave the content untouched
+	if (tabs_amount == 0)
+	{
+		g_free(data);
+		g_free(replacement);
+		return;
+	}
+
+	text = g_malloc(len + (tabs_amount * (tab_w - 1)));
+
+	for (i = 0; i < len; i++)
+	{
+		if (data[i] == 9)
+		{
+			// increase cursor position to keep it at same position
+			if (pos > i) pos += 3;
+
+			text[j++] = 32;
+			text[j++] = 32;
+			text[j++] = 32;
+			text[j++] = 32;
+		}
+		else
+		{
+			text[j++] = data[i];
+		}
+	}
+	geany_debug("tabs_amount: %d, len: %d, %d == %d", tabs_amount, len, len + (tabs_amount * (tab_w - 1)), j);
+	sci_set_text(doc_list[idx].sci, text);
+	sci_set_current_position(doc_list[idx].sci, pos);
+
+	g_free(data);
+	g_free(text);
+	g_free(replacement);
+}
+
+
+void document_strip_trailing_spaces(gint idx)
+{
+	gint max_lines = sci_get_line_count(doc_list[idx].sci);
+	gint line;
+
+	for (line = 0; line < max_lines; line++)
+	{
+		gint line_start = sci_get_position_from_line(doc_list[idx].sci, line);
+		gint line_end = sci_get_line_end_from_position(doc_list[idx].sci, line);
+		gint i = line_end - 1;
+		gchar ch = sci_get_char_at(doc_list[idx].sci, i);
+
+		while ((i >= line_start) && ((ch == ' ') || (ch == '\t')))
+		{
+			i--;
+			ch = sci_get_char_at(doc_list[idx].sci, i);
+		}
+		if (i < (line_end-1))
+		{
+			sci_target_start(doc_list[idx].sci, i + 1);
+			sci_target_end(doc_list[idx].sci, line_end);
+			sci_target_replace(doc_list[idx].sci, "");
+		}
+	}
+}
+
+
+void document_ensure_final_newline(gint idx)
+{
+	gint max_lines = sci_get_line_count(doc_list[idx].sci);
+	gboolean append_newline = (max_lines == 1);
+	gint end_document = sci_get_position_from_line(doc_list[idx].sci, max_lines);
+
+	if (max_lines > 1)
+	{
+		append_newline = end_document > sci_get_position_from_line(doc_list[idx].sci, max_lines - 1);
+	}
+	if (append_newline)
+	{
+		const gchar *eol = "\n";
+		switch (sci_get_eol_mode(doc_list[idx].sci))
+		{
+			case SC_EOL_CRLF:
+				eol = "\r\n";
+				break;
+			case SC_EOL_CR:
+				eol = "\r";
+				break;
+		}
+		sci_insert_text(doc_list[idx].sci, end_document, eol);
+	}
+}
+
+

Modified: trunk/src/document.h
===================================================================
--- trunk/src/document.h	2006-07-16 22:02:31 UTC (rev 571)
+++ trunk/src/document.h	2006-07-17 10:42:26 UTC (rev 572)
@@ -119,4 +119,10 @@
 /* simple file print */
 void document_print(gint idx);
 
+void document_replace_tabs(gint idx);
+
+void document_strip_trailing_spaces(gint idx);
+
+void document_ensure_final_newline(gint idx);
+
 #endif

Modified: trunk/src/utils.c
===================================================================
--- trunk/src/utils.c	2006-07-16 22:02:31 UTC (rev 571)
+++ trunk/src/utils.c	2006-07-17 10:42:26 UTC (rev 572)
@@ -1216,60 +1216,6 @@
 }
 
 
-void utils_ensure_final_newline(gint idx)
-{
-	gint max_lines = sci_get_line_count(doc_list[idx].sci);
-	gboolean append_newline = (max_lines == 1);
-	gint end_document = sci_get_position_from_line(doc_list[idx].sci, max_lines);
-
-	if (max_lines > 1)
-	{
-		append_newline = end_document > sci_get_position_from_line(doc_list[idx].sci, max_lines - 1);
-	}
-	if (append_newline)
-	{
-		const gchar *eol = "\n";
-		switch (sci_get_eol_mode(doc_list[idx].sci))
-		{
-			case SC_EOL_CRLF:
-				eol = "\r\n";
-				break;
-			case SC_EOL_CR:
-				eol = "\r";
-				break;
-		}
-		sci_insert_text(doc_list[idx].sci, end_document, eol);
-	}
-}
-
-
-void utils_strip_trailing_spaces(gint idx)
-{
-	gint max_lines = sci_get_line_count(doc_list[idx].sci);
-	gint line;
-
-	for (line = 0; line < max_lines; line++)
-	{
-		gint line_start = sci_get_position_from_line(doc_list[idx].sci, line);
-		gint line_end = sci_get_line_end_from_position(doc_list[idx].sci, line);
-		gint i = line_end - 1;
-		gchar ch = sci_get_char_at(doc_list[idx].sci, i);
-
-		while ((i >= line_start) && ((ch == ' ') || (ch == '\t')))
-		{
-			i--;
-			ch = sci_get_char_at(doc_list[idx].sci, i);
-		}
-		if (i < (line_end-1))
-		{
-			sci_target_start(doc_list[idx].sci, i + 1);
-			sci_target_end(doc_list[idx].sci, line_end);
-			sci_target_replace(doc_list[idx].sci, "");
-		}
-	}
-}
-
-
 gdouble utils_scale_round (gdouble val, gdouble factor)
 {
 	//val = floor(val * factor + 0.5);
@@ -1520,60 +1466,6 @@
 }
 
 
-/// TODO move me to document.c
-void utils_replace_tabs(gint idx)
-{
-	gint i, len, j = 0, tabs_amount = 0;
-	gint tab_w = sci_get_tab_width(doc_list[idx].sci);
-	gchar *data, *replacement;
-	gchar *text;
-
-	replacement = g_malloc(tab_w + 1);
-
-	// get the text
-	len = sci_get_length(doc_list[idx].sci) + 1;
-	data = g_malloc(len);
-	sci_get_text(doc_list[idx].sci, len, data);
-
-	// prepare the spaces with the width of a tab
-	for (i = 0; i < tab_w; i++) replacement[i] = ' ';
-	replacement[tab_w] = '\0';
-
-	for (i = 0; i < len; i++) if (data[i] == 9) tabs_amount++;
-
-	// if there are no tabs, just return and leave the content untouched
-	if (tabs_amount == 0)
-	{
-		g_free(data);
-		g_free(replacement);
-		return;
-	}
-
-	text = g_malloc(len + (tabs_amount * (tab_w - 1)));
-
-	for (i = 0; i < len; i++)
-	{
-		if (data[i] == 9)
-		{
-			text[j++] = 32;
-			text[j++] = 32;
-			text[j++] = 32;
-			text[j++] = 32;
-		}
-		else
-		{
-			text[j++] = data[i];
-		}
-	}
-	geany_debug("tabs_amount: %d, len: %d, %d == %d", tabs_amount, len, len + (tabs_amount * (tab_w - 1)), j);
-	sci_set_text(doc_list[idx].sci, text);
-
-	g_free(data);
-	g_free(text);
-	g_free(replacement);
-}
-
-
 gchar *utils_get_hostname(void)
 {
 #ifndef HAVE_GETHOSTNAME

Modified: trunk/src/utils.h
===================================================================
--- trunk/src/utils.h	2006-07-16 22:02:31 UTC (rev 571)
+++ trunk/src/utils.h	2006-07-17 10:42:26 UTC (rev 572)
@@ -124,10 +124,6 @@
 
 gboolean utils_is_absolute_path(const gchar *path);
 
-void utils_ensure_final_newline(gint idx);
-
-void utils_strip_trailing_spaces(gint idx);
-
 gdouble utils_scale_round(gdouble val, gdouble factor);
 
 void utils_widget_show_hide(GtkWidget *widget, gboolean show);
@@ -153,8 +149,6 @@
 
 gchar utils_brace_opposite(gchar ch);
 
-void utils_replace_tabs(gint idx);
-
 gchar *utils_get_hostname(void);
 
 gint utils_make_settings_dir(const gchar *dir);
@@ -222,4 +216,6 @@
 
 void utils_update_toolbar_items(void);
 
+gchar **utils_read_file_in_array(const gchar *filename);
+
 #endif


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