SF.net SVN: geany: [653] trunk

eht16 at users.sourceforge.net eht16 at xxxxx
Mon Jul 31 09:39:45 UTC 2006


Revision: 653
Author:   eht16
Date:     2006-07-31 02:39:33 -0700 (Mon, 31 Jul 2006)
ViewCVS:  http://svn.sourceforge.net/geany/?rev=653&view=rev

Log Message:
-----------
Added functionality to uncomment code and added keyboard shortcut Ctrl+Shift+D for this (closes #1521714).

Modified Paths:
--------------
    trunk/ChangeLog
    trunk/src/keybindings.c
    trunk/src/keybindings.h
    trunk/src/sci_cb.c
    trunk/src/sci_cb.h
Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2006-07-30 16:02:21 UTC (rev 652)
+++ trunk/ChangeLog	2006-07-31 09:39:33 UTC (rev 653)
@@ -1,3 +1,10 @@
+2006-07-31  Enrico Tröger  <enrico.troeger at uvena.de>
+
+ * src/keybindings.c, src/sci_cb.c:
+   Added functionality to uncomment code and added keyboard shortcut
+   Ctrl+Shift+D for this (closes #1521714).
+
+
 2006-07-30  Enrico Tröger  <enrico.troeger at uvena.de>
 
  * doc/geany.docbook, data/filetypes.common, src/.c:

Modified: trunk/src/keybindings.c
===================================================================
--- trunk/src/keybindings.c	2006-07-30 16:02:21 UTC (rev 652)
+++ trunk/src/keybindings.c	2006-07-31 09:39:33 UTC (rev 653)
@@ -86,6 +86,7 @@
 static void cb_func_toggle_sidebar(void);
 static void cb_func_edit_duplicateline(void);
 static void cb_func_edit_commentline(void);
+static void cb_func_edit_uncommentline(void);
 static void cb_func_edit_autocomplete(void);
 static void cb_func_edit_calltip(void);
 static void cb_func_edit_macrolist(void);
@@ -193,6 +194,8 @@
 		GDK_g, GDK_CONTROL_MASK, "edit_duplicateline", _("Duplicate line or selection"));
 	keys[GEANY_KEYS_EDIT_COMMENTLINE] = fill(cb_func_edit_commentline,
 		GDK_d, GDK_CONTROL_MASK, "edit_commentline", _("Comment line"));
+	keys[GEANY_KEYS_EDIT_UNCOMMENTLINE] = fill(cb_func_edit_uncommentline,
+		GDK_d, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "edit_uncommentline", _("Uncomment line"));
 	keys[GEANY_KEYS_EDIT_AUTOCOMPLETE] = fill(cb_func_edit_autocomplete,
 		GDK_space, GDK_CONTROL_MASK, "edit_autocomplete", _("Complete word"));
 #ifdef G_OS_WIN32
@@ -704,6 +707,13 @@
 	sci_cb_do_comment(idx);
 }
 
+static void cb_func_edit_uncommentline(void)
+{
+	gint idx = document_get_cur_idx();
+	if (idx == -1 || ! doc_list[idx].is_valid) return;
+	sci_cb_do_uncomment(idx);
+}
+
 static void cb_func_edit_autocomplete(void)
 {
 	gint idx = document_get_cur_idx();

Modified: trunk/src/keybindings.h
===================================================================
--- trunk/src/keybindings.h	2006-07-30 16:02:21 UTC (rev 652)
+++ trunk/src/keybindings.h	2006-07-31 09:39:33 UTC (rev 653)
@@ -83,6 +83,7 @@
 	GEANY_KEYS_SWITCH_TABRIGHT,
 	GEANY_KEYS_EDIT_DUPLICATELINE,
 	GEANY_KEYS_EDIT_COMMENTLINE,
+	GEANY_KEYS_EDIT_UNCOMMENTLINE,
 	GEANY_KEYS_EDIT_AUTOCOMPLETE,
 	GEANY_KEYS_EDIT_CALLTIP,
 	GEANY_KEYS_EDIT_MACROLIST,

Modified: trunk/src/sci_cb.c
===================================================================
--- trunk/src/sci_cb.c	2006-07-30 16:02:21 UTC (rev 652)
+++ trunk/src/sci_cb.c	2006-07-31 09:39:33 UTC (rev 653)
@@ -320,7 +320,8 @@
 	gint lexer = SSM(sci, SCI_GETLEXER, 0, 0);
 	gchar *text, *line_buf;
 
-	if (lexer != SCLEX_CPP && lexer != SCLEX_HTML && lexer != SCLEX_PASCAL) return;
+	if (lexer != SCLEX_CPP && lexer != SCLEX_HTML && lexer != SCLEX_PASCAL && lexer != SCLEX_BASH)
+			return;
 
 	// check that the line is empty, to not kill text in the line
 	line_buf = g_malloc(line_len + 1);
@@ -844,6 +845,147 @@
 }
 
 
+void sci_cb_do_uncomment(gint idx)
+{
+	gint first_line;
+	gint last_line;
+	gint x, i, line_start, line_len;
+	gchar sel[64], *co, *cc;
+	gboolean break_loop = FALSE;
+	filetype *ft;
+
+	if (idx == -1 || ! doc_list[idx].is_valid) return;
+
+	ft = doc_list[idx].file_type;
+
+	first_line = sci_get_line_from_position(doc_list[idx].sci,
+		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);
+	last_line = MAX(first_line, last_line);
+
+	// hack for detection of HTML vs PHP code, if non-PHP set filetype to XML
+	line_start = sci_get_position_from_line(doc_list[idx].sci, first_line);
+	if (ft->id == GEANY_FILETYPES_PHP)
+	{
+		if (sci_get_style_at(doc_list[idx].sci, line_start) < 118 ||
+			sci_get_style_at(doc_list[idx].sci, line_start) > 127)
+			ft = filetypes[GEANY_FILETYPES_XML];
+	}
+
+	co = ft->comment_open;
+	cc = ft->comment_close;
+	if (co == NULL) return;
+
+	SSM(doc_list[idx].sci, SCI_BEGINUNDOACTION, 0, 0);
+
+	for (i = first_line; (i <= last_line) && (! break_loop); i++)
+	{
+		line_start = sci_get_position_from_line(doc_list[idx].sci, i);
+		line_len = sci_get_line_length(doc_list[idx].sci, i);
+		x = 0;
+
+		//geany_debug("line: %d line_start: %d len: %d (%d)", i, line_start, MIN(63, (line_len - 1)), line_len);
+		sci_get_text_range(doc_list[idx].sci, line_start, MIN((line_start + 63), (line_start + line_len - 1)), sel);
+		sel[MIN(63, (line_len - 1))] = '\0';
+
+		while (isspace(sel[x])) x++;
+
+		// to skip blank lines
+		if (x < line_len && sel[x] != '\0')
+		{
+			// use single line comment
+			if (cc == NULL || strlen(cc) == 0)
+			{
+				guint i;
+				guint len = strlen(co);
+
+				switch (len)
+				{
+					case 1: if (sel[x] != co[0]) continue; break;
+					case 2: if (sel[x] != co[0] || sel[x+1] != co[1]) continue; break;
+					case 3: if (sel[x] != co[0] || sel[x+1] != co[1] || sel[x+2] != co[2])
+								continue; break;
+					default: continue;
+				}
+
+				SSM(doc_list[idx].sci, SCI_GOTOPOS, line_start + x + len, 0);
+				for (i = 0; i < len; i++) SSM(doc_list[idx].sci, SCI_DELETEBACK, 0, 0);
+			}
+			// use multi line comment
+			else
+			{
+				gint style_comment;
+				gint lexer = SSM(doc_list[idx].sci, SCI_GETLEXER, 0, 0);
+
+				// process only lines which are already comments
+				switch (lexer)
+				{	// I will list only those lexers which support multi line comments
+					case SCLEX_XML:
+					case SCLEX_HTML:
+					{
+						if (sci_get_style_at(doc_list[idx].sci, line_start) >= 118 &&
+							sci_get_style_at(doc_list[idx].sci, line_start) <= 127)
+							style_comment = SCE_HPHP_COMMENT;
+						else style_comment = SCE_H_COMMENT;
+						break;
+					}
+					case SCLEX_CSS: style_comment = SCE_CSS_COMMENT; break;
+					case SCLEX_SQL: style_comment = SCE_SQL_COMMENT; break;
+					case SCLEX_CAML: style_comment = SCE_CAML_COMMENT; break;
+					case SCLEX_CPP:
+					case SCLEX_PASCAL:
+					default: style_comment = SCE_C_COMMENT;
+				}
+				if (sci_get_style_at(doc_list[idx].sci, line_start + x) == style_comment)
+				{
+					// find the beginning of the multi line comment
+					gint pos, line, len, x;
+					gchar *linebuf;
+
+					// remove comment open chars
+					pos = document_find_text(idx, co, 0, TRUE);
+					SSM(doc_list[idx].sci, SCI_DELETEBACK, 0, 0);
+
+					// check whether the line is empty and can be deleted
+					line = sci_get_line_from_position(doc_list[idx].sci, pos);
+					len = sci_get_line_length(doc_list[idx].sci, line);
+					linebuf = g_malloc(len + 1);
+					sci_get_line(doc_list[idx].sci, line, linebuf);
+					linebuf[len] = '\0';
+					x = 0;
+					while (linebuf[x] != '\0' && isspace(linebuf[x])) x++;
+					if (x == len) SSM(doc_list[idx].sci, SCI_LINEDELETE, 0, 0);
+					g_free(linebuf);
+
+					// remove comment close chars
+					pos = document_find_text(idx, cc, 0, FALSE);
+					SSM(doc_list[idx].sci, SCI_DELETEBACK, 0, 0);
+
+					// check whether the line is empty and can be deleted
+					line = sci_get_line_from_position(doc_list[idx].sci, pos);
+					len = sci_get_line_length(doc_list[idx].sci, line);
+					linebuf = g_malloc(len + 1);
+					sci_get_line(doc_list[idx].sci, line, linebuf);
+					geany_debug("%d", line);
+					linebuf[len] = '\0';
+					x = 0;
+					while (linebuf[x] != '\0' && isspace(linebuf[x])) x++;
+					if (x == len) SSM(doc_list[idx].sci, SCI_LINEDELETE, 0, 0);
+					g_free(linebuf);
+				}
+
+				// break because we are already on the last line
+				break_loop = TRUE;
+				break;
+			}
+		}
+	}
+	SSM(doc_list[idx].sci, SCI_ENDUNDOACTION, 0, 0);
+}
+
+
 void sci_cb_do_comment(gint idx)
 {
 	gint first_line;

Modified: trunk/src/sci_cb.h
===================================================================
--- trunk/src/sci_cb.h	2006-07-30 16:02:21 UTC (rev 652)
+++ trunk/src/sci_cb.h	2006-07-31 09:39:33 UTC (rev 653)
@@ -47,6 +47,8 @@
 
 void sci_cb_do_comment(gint idx);
 
+void sci_cb_do_uncomment(gint idx);
+
 void sci_cb_highlight_braces(ScintillaObject *sci, gint cur_pos);
 
 void sci_cb_auto_multiline(ScintillaObject *sci, gint pos);


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