SF.net SVN: geany: [1160] trunk

eht16 at users.sourceforge.net eht16 at xxxxx
Sat Jan 6 15:03:53 UTC 2007


Revision: 1160
          http://svn.sourceforge.net/geany/?rev=1160&view=rev
Author:   eht16
Date:     2007-01-06 07:03:53 -0800 (Sat, 06 Jan 2007)

Log Message:
-----------
Removed multiline template because it makes not much sense, instead just comment three lines using the general comment functionality.
Removed special templates for Pascal and some other filetypes using "#" as comment char. The comment characters for fileheader and GPL templates are now added dynamically according to the current filetype.

Modified Paths:
--------------
    trunk/ChangeLog
    trunk/src/callbacks.c
    trunk/src/sci_cb.c
    trunk/src/sci_cb.h
    trunk/src/templates.c
    trunk/src/templates.h

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2007-01-06 12:35:43 UTC (rev 1159)
+++ trunk/ChangeLog	2007-01-06 15:03:53 UTC (rev 1160)
@@ -1,3 +1,15 @@
+2007-01-06  Enrico Tröger  <enrico.troeger at uvena.de>
+
+ * src/callbacks.c, src/sci_cb.c, src/sci_cb.h, src/templates.c,
+   src/templates.h:
+   Removed multiline template because it makes not much sense, instead
+   just comment three lines using the general comment functionality.
+   Removed special templates for Pascal and some other filetypes using
+   "#" as comment char. The comment characters for fileheader and GPL
+   templates are now added dynamically according to the current
+   filetype.
+
+
 2007-01-06  Nick Treleaven  <nick.treleaven at btinternet.com>
 
  * src/keybindings.c:

Modified: trunk/src/callbacks.c
===================================================================
--- trunk/src/callbacks.c	2007-01-06 12:35:43 UTC (rev 1159)
+++ trunk/src/callbacks.c	2007-01-06 15:03:53 UTC (rev 1160)
@@ -1,7 +1,8 @@
 /*
  *      callbacks.c - this file is part of Geany, a fast and lightweight IDE
  *
- *      Copyright 2006 Enrico Troeger <enrico.troeger at uvena.de>
+ *      Copyright 2005-2007 Enrico Troeger <enrico.troeger at uvena.de>
+ *      Copyright 2006-2007 Nick Treleaven <nick.treleaven at btinternet.com>
  *
  *      This program is free software; you can redistribute it and/or modify
  *      it under the terms of the GNU General Public License as published by
@@ -1460,27 +1461,7 @@
 	line = utils_get_current_function(idx, &cur_tag);
 	pos = sci_get_position_from_line(doc_list[idx].sci, line - 1);
 
-	switch (doc_list[idx].file_type->id)
-	{
-		case GEANY_FILETYPES_PASCAL:
-		{
-			text = templates_get_template_function(GEANY_TEMPLATE_FUNCTION_PASCAL, cur_tag);
-			break;
-		}
-		case GEANY_FILETYPES_PYTHON:
-		case GEANY_FILETYPES_RUBY:
-		case GEANY_FILETYPES_SH:
-		case GEANY_FILETYPES_MAKE:
-		case GEANY_FILETYPES_PERL:
-		{
-			text = templates_get_template_function(GEANY_TEMPLATE_FUNCTION_ROUTE, cur_tag);
-			break;
-		}
-		default:
-		{
-			text = templates_get_template_function(GEANY_TEMPLATE_FUNCTION, cur_tag);
-		}
-	}
+	text = templates_get_template_function(doc_list[idx].file_type->id, cur_tag);
 
 	sci_insert_text(doc_list[idx].sci, pos, text);
 	g_free(text);
@@ -1492,36 +1473,28 @@
                                         gpointer         user_data)
 {
 	gint idx = document_get_cur_idx();
-	gchar *text;
 
-	if (doc_list[idx].file_type == NULL)
+	if (! DOC_IDX_VALID(idx) || doc_list[idx].file_type == NULL)
 	{
 		ui_set_statusbar(_("Please set the filetype for the current file before using this function."));
 		return;
 	}
 
-	switch (doc_list[idx].file_type->id)
-	{
-		case GEANY_FILETYPES_PASCAL:
-		{
-			text = templates_get_template_generic(GEANY_TEMPLATE_MULTILINE_PASCAL);
-			break;
-		}
-		case GEANY_FILETYPES_PYTHON:
-		case GEANY_FILETYPES_RUBY:
-		case GEANY_FILETYPES_SH:
-		case GEANY_FILETYPES_MAKE:
-		case GEANY_FILETYPES_PERL:
-		{
-			text = templates_get_template_generic(GEANY_TEMPLATE_MULTILINE_ROUTE);
-			break;
-		}
-		default:
-		{
-			text = templates_get_template_generic(GEANY_TEMPLATE_MULTILINE);
-		}
-	}
+	verify_click_pos(idx); // make sure that the click_pos is valid
 
+	sci_cb_insert_multiline_comment(idx);
+}
+
+
+void
+on_comments_gpl_activate               (GtkMenuItem     *menuitem,
+                                        gpointer         user_data)
+{
+	gint idx = document_get_cur_idx();
+	gchar *text;
+
+	text = templates_get_template_licence(FILETYPE_ID(doc_list[idx].file_type));
+
 	verify_click_pos(idx); // make sure that the click_pos is valid
 
 	sci_insert_text(doc_list[idx].sci, editor_info.click_pos, text);
@@ -1530,18 +1503,20 @@
 
 
 void
-on_comments_gpl_activate               (GtkMenuItem     *menuitem,
+on_comments_bsd_activate               (GtkMenuItem     *menuitem,
                                         gpointer         user_data)
 {
+/*
 	gint idx = document_get_cur_idx();
 	gchar *text;
 
-	text = templates_get_template_gpl(FILETYPE_ID(doc_list[idx].file_type));
+	text = templates_get_template_licence(FILETYPE_ID(doc_list[idx].file_type), GEANY_TEMPLATE_BSD);
 
 	verify_click_pos(idx); // make sure that the click_pos is valid
 
 	sci_insert_text(doc_list[idx].sci, editor_info.click_pos, text);
 	g_free(text);
+*/
 }
 
 
@@ -1944,7 +1919,7 @@
 {
 	gint idx = document_get_cur_idx();
 	if (idx == -1 || ! doc_list[idx].is_valid) return;
-	sci_cb_do_comment(idx, -1);
+	sci_cb_do_comment(idx, -1, FALSE);
 }
 
 
@@ -2075,6 +2050,15 @@
 
 
 void
+on_menu_comments_bsd_activate          (GtkMenuItem     *menuitem,
+                                        gpointer         user_data)
+{
+	insert_callback_from_menu = TRUE;
+	on_comments_bsd_activate(menuitem, user_data);
+}
+
+
+void
 on_menu_insert_include_activate        (GtkMenuItem     *menuitem,
                                         gpointer         user_data)
 {
@@ -2090,3 +2074,6 @@
 	insert_callback_from_menu = TRUE;
 	on_insert_date_activate(menuitem, user_data);
 }
+
+
+

Modified: trunk/src/sci_cb.c
===================================================================
--- trunk/src/sci_cb.c	2007-01-06 12:35:43 UTC (rev 1159)
+++ trunk/src/sci_cb.c	2007-01-06 15:03:53 UTC (rev 1160)
@@ -1,7 +1,8 @@
 /*
  *      sci_cb.c - this file is part of Geany, a fast and lightweight IDE
  *
- *      Copyright 2006 Enrico Troeger <enrico.troeger at uvena.de>
+ *      Copyright 2005-2007 Enrico Troeger <enrico.troeger at uvena.de>
+ *      Copyright 2006-2007 Nick Treleaven <nick.treleaven at btinternet.com>
  *
  *      This program is free software; you can redistribute it and/or modify
  *      it under the terms of the GNU General Public License as published by
@@ -1479,7 +1480,7 @@
 				if (do_continue) continue;
 
 				// we are still here, so the above lines were not already comments, so comment it
-				sci_cb_do_comment(idx, i);
+				sci_cb_do_comment(idx, i, FALSE);
 				count_commented++;
 			}
 			// use multi line comment
@@ -1566,7 +1567,7 @@
 }
 
 
-void sci_cb_do_comment(gint idx, gint line)
+void sci_cb_do_comment(gint idx, gint line, gboolean allow_empty_lines)
 {
 	gint first_line, last_line;
 	gint x, i, line_start, line_len;
@@ -1622,10 +1623,14 @@
 		sci_get_text_range(doc_list[idx].sci, line_start, MIN((line_start + 256), (line_start + line_len - 1)), sel);
 		sel[MIN(256, (line_len - 1))] = '\0';
 
+		/// TODO fix the above code to remove the described segfault below
+		// The following loop causes a segfault when the cursor is on the last line of doc and
+		// there are no other characters on this line and Geany was compiled with -O2, with -O0
+		// all works fine.
 		while (isspace(sel[x])) x++;
 
 		// to skip blank lines
-		if (x < line_len && sel[x] != '\0')
+		if (allow_empty_lines || (x < line_len && sel[x] != '\0'))
 		{
 			// use single line comment
 			if (cc == NULL || strlen(cc) == 0)
@@ -1938,3 +1943,59 @@
 		return -1;
 	}
 }
+
+
+// inserts a three-line comment at one line above current cursor position
+void sci_cb_insert_multiline_comment(gint idx)
+{
+	gchar *text;
+	gint text_len;
+	gint line;
+	gint pos;
+	gboolean have_multiline_comment = FALSE;
+
+	if (doc_list[idx].file_type->comment_close != NULL &&
+		strlen(doc_list[idx].file_type->comment_close) > 0)
+		have_multiline_comment = TRUE;
+
+	// insert three lines one line above of the current position
+	line = sci_get_line_from_position(doc_list[idx].sci, editor_info.click_pos);
+	pos = sci_get_position_from_line(doc_list[idx].sci, line);
+
+	// use the indentation on the current line but only when comment indention is used
+	// and we don't have multi line comment characters
+	if (doc_list[idx].use_auto_indention && ! have_multiline_comment &&
+		doc_list[idx].file_type->comment_use_indent)
+	{
+		get_indent(doc_list[idx].sci, editor_info.click_pos, TRUE);
+		text = g_strdup_printf("%s\n%s\n%s\n", indent, indent, indent);
+		text_len = strlen(text);
+	}
+	else
+	{
+		text = g_strdup("\n\n\n");
+		text_len = 3;
+	}
+	sci_insert_text(doc_list[idx].sci, pos, text);
+	g_free(text);
+
+
+	// select the inserted lines for commenting
+	sci_set_selection_start(doc_list[idx].sci, pos);
+	sci_set_selection_end(doc_list[idx].sci, pos + text_len);
+
+	sci_cb_do_comment(idx, -1, TRUE);
+
+	// set the current position to the start of the first inserted line
+	pos += strlen(doc_list[idx].file_type->comment_open);
+
+	// on multi line comment jump to the next line, otherwise add the length of added indentation
+	if (have_multiline_comment)
+		pos += 1;
+	else
+		pos += strlen(indent);
+
+	sci_set_current_position(doc_list[idx].sci, pos);
+	// reset the selection
+	sci_set_anchor(doc_list[idx].sci, pos);
+}

Modified: trunk/src/sci_cb.h
===================================================================
--- trunk/src/sci_cb.h	2007-01-06 12:35:43 UTC (rev 1159)
+++ trunk/src/sci_cb.h	2007-01-06 15:03:53 UTC (rev 1160)
@@ -1,7 +1,8 @@
 /*
  *      sci_cb.h - this file is part of Geany, a fast and lightweight IDE
  *
- *      Copyright 2006 Enrico Troeger <enrico.troeger at uvena.de>
+ *      Copyright 2005-2007 Enrico Troeger <enrico.troeger at uvena.de>
+ *      Copyright 2006-2007 Nick Treleaven <nick.treleaven at btinternet.com>
  *
  *      This program is free software; you can redistribute it and/or modify
  *      it under the terms of the GNU General Public License as published by
@@ -69,7 +70,7 @@
 
 void sci_cb_do_comment_toggle(gint idx);
 
-void sci_cb_do_comment(gint idx, gint line);
+void sci_cb_do_comment(gint idx, gint line, gboolean allow_empty_lines);
 
 void sci_cb_do_uncomment(gint idx, gint line);
 
@@ -83,4 +84,6 @@
 
 gint sci_cb_lexer_get_type_keyword_idx(gint lexer);
 
+void sci_cb_insert_multiline_comment(gint idx);
+
 #endif

Modified: trunk/src/templates.c
===================================================================
--- trunk/src/templates.c	2007-01-06 12:35:43 UTC (rev 1159)
+++ trunk/src/templates.c	2007-01-06 15:03:53 UTC (rev 1160)
@@ -1,7 +1,8 @@
 /*
  *      templates.c - this file is part of Geany, a fast and lightweight IDE
  *
- *      Copyright 2006 Enrico Troeger <enrico.troeger at uvena.de>
+ *      Copyright 2005-2007 Enrico Troeger <enrico.troeger at uvena.de>
+ *      Copyright 2006-2007 Nick Treleaven <nick.treleaven at btinternet.com>
  *
  *      This program is free software; you can redistribute it and/or modify
  *      it under the terms of the GNU General Public License as published by
@@ -21,6 +22,7 @@
  */
 
 #include <time.h>
+#include <string.h>
 
 #include "geany.h"
 
@@ -32,115 +34,41 @@
 
 // default templates, only for initial tempate file creation on first start of Geany
 static const gchar templates_gpl_notice[] = "\
- *      This program is free software; you can redistribute it and/or modify\n\
- *      it under the terms of the GNU General Public License as published by\n\
- *      the Free Software Foundation; either version 2 of the License, or\n\
- *      (at your option) any later version.\n\
- *\n\
- *      This program is distributed in the hope that it will be useful,\n\
- *      but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
- *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n\
- *      GNU General Public License for more details.\n\
- *\n\
- *      You should have received a copy of the GNU General Public License\n\
- *      along with this program; if not, write to the Free Software\n\
- *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n\
-";
-
-static const gchar templates_gpl_notice_pascal[] = "\
-      This program is free software; you can redistribute it and/or modify\n\
-      it under the terms of the GNU General Public License as published by\n\
-      the Free Software Foundation; either version 2 of the License, or\n\
-      (at your option) any later version.\n\
+This program is free software; you can redistribute it and/or modify\n\
+it under the terms of the GNU General Public License as published by\n\
+the Free Software Foundation; either version 2 of the License, or\n\
+(at your option) any later version.\n\
 \n\
-      This program is distributed in the hope that it will be useful,\n\
-      but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
-      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n\
-      GNU General Public License for more details.\n\
+This program is distributed in the hope that it will be useful,\n\
+but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n\
+GNU General Public License for more details.\n\
 \n\
-      You should have received a copy of the GNU General Public License\n\
-      along with this program; if not, write to the Free Software\n\
-      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n\
+You should have received a copy of the GNU General Public License\n\
+along with this program; if not, write to the Free Software\n\
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n\
 ";
 
-static const gchar templates_gpl_notice_route[] = "\
-#      This program is free software; you can redistribute it and/or modify\n\
-#      it under the terms of the GNU General Public License as published by\n\
-#      the Free Software Foundation; either version 2 of the License, or\n\
-#      (at your option) any later version.\n\
-#\n\
-#      This program is distributed in the hope that it will be useful,\n\
-#      but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
-#      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n\
-#      GNU General Public License for more details.\n\
-#\n\
-#      You should have received a copy of the GNU General Public License\n\
-#      along with this program; if not, write to the Free Software\n\
-#      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n\
-";
-
 static const gchar templates_function_description[] = "\
-/* \n\
- * name: {functionname}\n\
- * @param\n\
- * @return\n\
- */\n";
-
-static const gchar templates_function_description_pascal[] = "\
-{\n\
- name: {functionname}\n\
- @param\n\
- @return\n\
-}\n";
-
-static const gchar templates_function_description_route[] = "\
-#\n\
-# name: {functionname}\n\
-# @param\n\
-# @return\n\
+\n\
+name: {functionname}\n\
+ at param\n\
+ at return\n\
 ";
 
 static const gchar templates_multiline[] = "\
-/* \n\
- * \n\
- */";
-
-static const gchar templates_multiline_pascal[] = "\
-{\n\
  \n\
-}";
+ \n\
+";
 
-static const gchar templates_multiline_route[] = "\
-#\n\
-#";
-
 static const gchar templates_fileheader[] = "\
-/*\n\
- *      {filename}\n\
- *\n\
- *      Copyright {year} {developer} <{mail}>\n\
- *\n\
-{gpl}\
- */\n";
-
-static const gchar templates_fileheader_pascal[] = "\
-{\n\
-      {filename}\n\
+{filename}\n\
 \n\
-      Copyright {year} {developer} <{mail}>\n\
+Copyright {year} {developer} <{mail}>\n\
 \n\
 {gpl}\
-}\n";
+";
 
-static const gchar templates_fileheader_route[] = "\
-#\n\
-#      {filename}\n\
-#\n\
-#      Copyright {year} {developer} <{mail}>\n\
-#\n\
-{gpl}\
-#\n";
-
 static const gchar templates_changelog[] = "\
 {date}  {developer}  <{mail}>\n\
 \n\
@@ -210,7 +138,8 @@
 </html>\n\
 ";
 
-static const gchar templates_filetype_pascal[] = "program {untitled};\n\
+static const gchar templates_filetype_pascal[] = "\n\
+program {untitled};\n\
 \n\
 uses crt;\n\
 var i : byte;\n\
@@ -222,10 +151,9 @@
 ";
 
 static const gchar templates_filetype_java[] = "\n\
-\n\
 public class {untitled} {\n\
 \n\
-	public static void main (String args[]) {\n\
+	public static void main (String args[]) {\
 		\n\
 		\n\
 	}\n\
@@ -248,9 +176,9 @@
 
 
 // some simple macros to reduce code size and make the code readable
-#define templates_get_filename(x) g_strconcat(app->configdir, G_DIR_SEPARATOR_S, x, NULL)
-#define templates_create_file(x, y)	if (! g_file_test(x, G_FILE_TEST_EXISTS)) utils_write_file(x, y)
-#define templates_read_file(x, y) g_file_get_contents(x, y, NULL, NULL);
+#define TEMPLATES_GET_FILENAME(x) g_strconcat(app->configdir, G_DIR_SEPARATOR_S, x, NULL)
+#define TEMPLATES_CREATE_FILE(x, y)	if (! g_file_test(x, G_FILE_TEST_EXISTS)) utils_write_file(x, y)
+#define TEMPLATES_READ_FILE(x, y) g_file_get_contents(x, y, NULL, NULL);
 
 
 // prototype, because this function should never be used outside of templates.c
@@ -259,28 +187,19 @@
 
 void templates_init(void)
 {
-	gchar *template_filename_fileheader = templates_get_filename("template.fileheader");
-	gchar *template_filename_fileheader_pascal =templates_get_filename("template.fileheader.pascal");
-	gchar *template_filename_fileheader_route =templates_get_filename("template.fileheader.route");
-	gchar *template_filename_gpl = templates_get_filename("template.gpl");
-	gchar *template_filename_gpl_pascal = templates_get_filename("template.gpl.pascal");
-	gchar *template_filename_gpl_route = templates_get_filename("template.gpl.route");
-	gchar *template_filename_function = templates_get_filename("template.function");
-	gchar *template_filename_function_pascal = templates_get_filename("template.function.pascal");
-	gchar *template_filename_function_route = templates_get_filename("template.function.route");
-	gchar *template_filename_multiline = templates_get_filename("template.multiline");
-	gchar *template_filename_multiline_pascal = templates_get_filename("template.multiline.pascal");
-	gchar *template_filename_multiline_route = templates_get_filename("template.multiline.route");
-	gchar *template_filename_changelog = templates_get_filename("template.changelog");
-	gchar *template_filename_filetype_none = templates_get_filename("template.filetype.none");
-	gchar *template_filename_filetype_c = templates_get_filename("template.filetype.c");
-	gchar *template_filename_filetype_cpp = templates_get_filename("template.filetype.cpp");
-	gchar *template_filename_filetype_d = templates_get_filename("template.filetype.d");
-	gchar *template_filename_filetype_java = templates_get_filename("template.filetype.java");
-	gchar *template_filename_filetype_pascal = templates_get_filename("template.filetype.pascal");
-	gchar *template_filename_filetype_php = templates_get_filename("template.filetype.php");
-	gchar *template_filename_filetype_html = templates_get_filename("template.filetype.html");
-	gchar *template_filename_filetype_ruby = templates_get_filename("template.filetype.ruby");
+	gchar *template_filename_fileheader = TEMPLATES_GET_FILENAME("template.fileheader");
+	gchar *template_filename_gpl = TEMPLATES_GET_FILENAME("template.gpl");
+	gchar *template_filename_function = TEMPLATES_GET_FILENAME("template.function");
+	gchar *template_filename_changelog = TEMPLATES_GET_FILENAME("template.changelog");
+	gchar *template_filename_filetype_none = TEMPLATES_GET_FILENAME("template.filetype.none");
+	gchar *template_filename_filetype_c = TEMPLATES_GET_FILENAME("template.filetype.c");
+	gchar *template_filename_filetype_cpp = TEMPLATES_GET_FILENAME("template.filetype.cpp");
+	gchar *template_filename_filetype_d = TEMPLATES_GET_FILENAME("template.filetype.d");
+	gchar *template_filename_filetype_java = TEMPLATES_GET_FILENAME("template.filetype.java");
+	gchar *template_filename_filetype_pascal = TEMPLATES_GET_FILENAME("template.filetype.pascal");
+	gchar *template_filename_filetype_php = TEMPLATES_GET_FILENAME("template.filetype.php");
+	gchar *template_filename_filetype_html = TEMPLATES_GET_FILENAME("template.filetype.html");
+	gchar *template_filename_filetype_ruby = TEMPLATES_GET_FILENAME("template.filetype.ruby");
 
 	time_t tp = time(NULL);
 	const struct tm *tm = localtime(&tp);
@@ -289,95 +208,58 @@
 	strftime(year, 5, "%Y", tm);
 
 	// create the template files in the configuration directory, if they don't exist
-	templates_create_file(template_filename_fileheader, templates_fileheader);
-	templates_create_file(template_filename_fileheader_pascal, templates_fileheader_pascal);
-	templates_create_file(template_filename_fileheader_route, templates_fileheader_route);
-	templates_create_file(template_filename_gpl, templates_gpl_notice);
-	templates_create_file(template_filename_gpl_pascal, templates_gpl_notice_pascal);
-	templates_create_file(template_filename_gpl_route, templates_gpl_notice_route);
-	templates_create_file(template_filename_function, templates_function_description);
-	templates_create_file(template_filename_function_pascal, templates_function_description_pascal);
-	templates_create_file(template_filename_function_route, templates_function_description_route);
-	templates_create_file(template_filename_multiline, templates_multiline);
-	templates_create_file(template_filename_multiline_pascal, templates_multiline_pascal);
-	templates_create_file(template_filename_multiline_route, templates_multiline_route);
-	templates_create_file(template_filename_changelog, templates_changelog);
-	templates_create_file(template_filename_filetype_none, templates_filetype_none);
-	templates_create_file(template_filename_filetype_c, templates_filetype_c);
-	templates_create_file(template_filename_filetype_cpp, templates_filetype_cpp);
-	templates_create_file(template_filename_filetype_d, templates_filetype_d);
-	templates_create_file(template_filename_filetype_java, templates_filetype_java);
-	templates_create_file(template_filename_filetype_pascal, templates_filetype_pascal);
-	templates_create_file(template_filename_filetype_php, templates_filetype_php);
-	templates_create_file(template_filename_filetype_html, templates_filetype_html);
-	templates_create_file(template_filename_filetype_ruby, templates_filetype_ruby);
+	TEMPLATES_CREATE_FILE(template_filename_fileheader, templates_fileheader);
+	TEMPLATES_CREATE_FILE(template_filename_gpl, templates_gpl_notice);
+	TEMPLATES_CREATE_FILE(template_filename_function, templates_function_description);
+	TEMPLATES_CREATE_FILE(template_filename_changelog, templates_changelog);
+	TEMPLATES_CREATE_FILE(template_filename_filetype_none, templates_filetype_none);
+	TEMPLATES_CREATE_FILE(template_filename_filetype_c, templates_filetype_c);
+	TEMPLATES_CREATE_FILE(template_filename_filetype_cpp, templates_filetype_cpp);
+	TEMPLATES_CREATE_FILE(template_filename_filetype_d, templates_filetype_d);
+	TEMPLATES_CREATE_FILE(template_filename_filetype_java, templates_filetype_java);
+	TEMPLATES_CREATE_FILE(template_filename_filetype_pascal, templates_filetype_pascal);
+	TEMPLATES_CREATE_FILE(template_filename_filetype_php, templates_filetype_php);
+	TEMPLATES_CREATE_FILE(template_filename_filetype_html, templates_filetype_html);
+	TEMPLATES_CREATE_FILE(template_filename_filetype_ruby, templates_filetype_ruby);
 
 	// read the contents
-	templates_read_file(template_filename_fileheader, &templates[GEANY_TEMPLATE_FILEHEADER]);
+	TEMPLATES_READ_FILE(template_filename_fileheader, &templates[GEANY_TEMPLATE_FILEHEADER]);
 	templates[GEANY_TEMPLATE_FILEHEADER] = templates_replace_all(templates[GEANY_TEMPLATE_FILEHEADER], year, date);
 
-	templates_read_file(template_filename_fileheader_pascal, &templates[GEANY_TEMPLATE_FILEHEADER_PASCAL]);
-	templates[GEANY_TEMPLATE_FILEHEADER_PASCAL] = templates_replace_all(templates[GEANY_TEMPLATE_FILEHEADER_PASCAL], year, date);
-
-	templates_read_file(template_filename_fileheader_route, &templates[GEANY_TEMPLATE_FILEHEADER_ROUTE]);
-	templates[GEANY_TEMPLATE_FILEHEADER_ROUTE] = templates_replace_all(templates[GEANY_TEMPLATE_FILEHEADER_ROUTE], year, date);
-
-	templates_read_file(template_filename_gpl, &templates[GEANY_TEMPLATE_GPL]);
+	TEMPLATES_READ_FILE(template_filename_gpl, &templates[GEANY_TEMPLATE_GPL]);
 	//templates[GEANY_TEMPLATE_GPL] = templates_replace_all(templates[GEANY_TEMPLATE_GPL], year, date);
 
-	templates_read_file(template_filename_gpl_pascal, &templates[GEANY_TEMPLATE_GPL_PASCAL]);
-	//templates[GEANY_TEMPLATE_GPL_PASCAL] = templates_replace_all(templates[GEANY_TEMPLATE_GPL_PASCAL], year, date);
-
-	templates_read_file(template_filename_gpl_route, &templates[GEANY_TEMPLATE_GPL_ROUTE]);
-	//templates[GEANY_TEMPLATE_GPL_ROUTE] = templates_replace_all(templates[GEANY_TEMPLATE_GPL_ROUTE], year, date);
-
-	templates_read_file(template_filename_function, &templates[GEANY_TEMPLATE_FUNCTION]);
+	TEMPLATES_READ_FILE(template_filename_function, &templates[GEANY_TEMPLATE_FUNCTION]);
 	templates[GEANY_TEMPLATE_FUNCTION] = templates_replace_all(templates[GEANY_TEMPLATE_FUNCTION], year, date);
 
-	templates_read_file(template_filename_function_pascal, &templates[GEANY_TEMPLATE_FUNCTION_PASCAL]);
-	templates[GEANY_TEMPLATE_FUNCTION_PASCAL] = templates_replace_all(templates[GEANY_TEMPLATE_FUNCTION_PASCAL], year, date);
-
-	templates_read_file(template_filename_function_route, &templates[GEANY_TEMPLATE_FUNCTION_ROUTE]);
-	templates[GEANY_TEMPLATE_FUNCTION_ROUTE] = templates_replace_all(templates[GEANY_TEMPLATE_FUNCTION_ROUTE], year, date);
-
-	templates_read_file(template_filename_multiline, &templates[GEANY_TEMPLATE_MULTILINE]);
-	//templates[GEANY_TEMPLATE_MULTILINE] = templates_replace_all(templates[GEANY_TEMPLATE_MULTILINE], year, date);
-
-	templates_read_file(template_filename_multiline_pascal, &templates[GEANY_TEMPLATE_MULTILINE_PASCAL]);
-	//templates[GEANY_TEMPLATE_MULTILINE_PASCAL] = templates_replace_all(templates[GEANY_TEMPLATE_MULTILINE_PASCAL], year, date);
-
-	templates_read_file(template_filename_multiline_route, &templates[GEANY_TEMPLATE_MULTILINE_ROUTE]);
-	//templates[GEANY_TEMPLATE_MULTILINE_ROUTE] = templates_replace_all(templates[GEANY_TEMPLATE_MULTILINE_ROUTE], year, date);
-
-	templates_read_file(template_filename_changelog, &templates[GEANY_TEMPLATE_CHANGELOG]);
+	TEMPLATES_READ_FILE(template_filename_changelog, &templates[GEANY_TEMPLATE_CHANGELOG]);
 	templates[GEANY_TEMPLATE_CHANGELOG] = templates_replace_all(templates[GEANY_TEMPLATE_CHANGELOG], year, date);
 
-
-	templates_read_file(template_filename_filetype_none, &templates[GEANY_TEMPLATE_FILETYPE_NONE]);
+	TEMPLATES_READ_FILE(template_filename_filetype_none, &templates[GEANY_TEMPLATE_FILETYPE_NONE]);
 	templates[GEANY_TEMPLATE_FILETYPE_NONE] = templates_replace_all(templates[GEANY_TEMPLATE_FILETYPE_NONE], year, date);
 
-	templates_read_file(template_filename_filetype_c, &templates[GEANY_TEMPLATE_FILETYPE_C]);
+	TEMPLATES_READ_FILE(template_filename_filetype_c, &templates[GEANY_TEMPLATE_FILETYPE_C]);
 	templates[GEANY_TEMPLATE_FILETYPE_C] = templates_replace_all(templates[GEANY_TEMPLATE_FILETYPE_C], year, date);
 
-	templates_read_file(template_filename_filetype_d, &templates[GEANY_TEMPLATE_FILETYPE_D]);
+	TEMPLATES_READ_FILE(template_filename_filetype_d, &templates[GEANY_TEMPLATE_FILETYPE_D]);
 	templates[GEANY_TEMPLATE_FILETYPE_D] = templates_replace_all(templates[GEANY_TEMPLATE_FILETYPE_D], year, date);
 
-	templates_read_file(template_filename_filetype_cpp, &templates[GEANY_TEMPLATE_FILETYPE_CPP]);
+	TEMPLATES_READ_FILE(template_filename_filetype_cpp, &templates[GEANY_TEMPLATE_FILETYPE_CPP]);
 	templates[GEANY_TEMPLATE_FILETYPE_CPP] = templates_replace_all(templates[GEANY_TEMPLATE_FILETYPE_CPP], year, date);
 
-	templates_read_file(template_filename_filetype_java, &templates[GEANY_TEMPLATE_FILETYPE_JAVA]);
+	TEMPLATES_READ_FILE(template_filename_filetype_java, &templates[GEANY_TEMPLATE_FILETYPE_JAVA]);
 	templates[GEANY_TEMPLATE_FILETYPE_JAVA] = templates_replace_all(templates[GEANY_TEMPLATE_FILETYPE_JAVA], year, date);
 
-	templates_read_file(template_filename_filetype_pascal, &templates[GEANY_TEMPLATE_FILETYPE_PASCAL]);
+	TEMPLATES_READ_FILE(template_filename_filetype_pascal, &templates[GEANY_TEMPLATE_FILETYPE_PASCAL]);
 	templates[GEANY_TEMPLATE_FILETYPE_PASCAL] = templates_replace_all(templates[GEANY_TEMPLATE_FILETYPE_PASCAL], year, date);
 
-	templates_read_file(template_filename_filetype_php, &templates[GEANY_TEMPLATE_FILETYPE_PHP]);
+	TEMPLATES_READ_FILE(template_filename_filetype_php, &templates[GEANY_TEMPLATE_FILETYPE_PHP]);
 	templates[GEANY_TEMPLATE_FILETYPE_PHP] = templates_replace_all(templates[GEANY_TEMPLATE_FILETYPE_PHP], year, date);
 
-	templates_read_file(template_filename_filetype_html, &templates[GEANY_TEMPLATE_FILETYPE_HTML]);
+	TEMPLATES_READ_FILE(template_filename_filetype_html, &templates[GEANY_TEMPLATE_FILETYPE_HTML]);
 	templates[GEANY_TEMPLATE_FILETYPE_HTML] = templates_replace_all(templates[GEANY_TEMPLATE_FILETYPE_HTML], year, date);
 
-	templates_read_file(template_filename_filetype_ruby, &templates[GEANY_TEMPLATE_FILETYPE_RUBY]);
+	TEMPLATES_READ_FILE(template_filename_filetype_ruby, &templates[GEANY_TEMPLATE_FILETYPE_RUBY]);
 	templates[GEANY_TEMPLATE_FILETYPE_RUBY] = templates_replace_all(templates[GEANY_TEMPLATE_FILETYPE_RUBY], year, date);
 
 
@@ -385,17 +267,8 @@
 	g_free(date);
 	g_free(year);
 	g_free(template_filename_fileheader);
-	g_free(template_filename_fileheader_pascal);
-	g_free(template_filename_fileheader_route);
 	g_free(template_filename_gpl);
-	g_free(template_filename_gpl_pascal);
-	g_free(template_filename_gpl_route);
 	g_free(template_filename_function);
-	g_free(template_filename_function_pascal);
-	g_free(template_filename_function_route);
-	g_free(template_filename_multiline);
-	g_free(template_filename_multiline_pascal);
-	g_free(template_filename_multiline_route);
 	g_free(template_filename_changelog);
 	g_free(template_filename_filetype_none);
 	g_free(template_filename_filetype_c);
@@ -409,28 +282,33 @@
 }
 
 
-/* double_comment is a hack for PHP/HTML for whether to first add C style commenting.
- * In future we could probably remove the need for this by making most templates
- * automatically commented (so template files are not commented) */
-static gchar *make_comment_block(const gchar *comment_text, gint filetype_idx,
-		gboolean double_comment)
+/* indent is used to make some whitespace between comment char and real start of the line
+ * e.g. indent = 8 prints " *     here comes the text of the line"
+ * indent is meant to be the whole amount of characters before the real line content follows, i.e.
+ * 6 characters are filled with whitespace when the comment characters include " *" */
+static gchar *make_comment_block(const gchar *comment_text, gint filetype_idx, gint indent)
 {
+	gchar *frame_start = "";	// to add before comment_text
+	gchar *frame_end = "";		// to add after comment_text
+	gchar *line_prefix;			// to add before every line in comment_text
+	gchar *result;
+	gchar *tmp;
+	gchar *prefix;
+	gchar **lines;
+	gint i;
+
+	/// TODO the following switch could be replaced by some intelligent code which reads
+	/// frame_start, frame_end and line_prefix from the filetype definition files
 	switch (filetype_idx)
 	{
-		case GEANY_FILETYPES_ALL:
-		return g_strdup(comment_text);	// no need to add to the text
-
-		case GEANY_FILETYPES_PHP:
 		case GEANY_FILETYPES_HTML:
-		{	// double comment
-			gchar *tmp = (double_comment) ?
-				make_comment_block(comment_text, GEANY_FILETYPES_C, FALSE) :
-				g_strdup(comment_text);
-			gchar *block = (filetype_idx == GEANY_FILETYPES_PHP) ?
-				g_strconcat("<?php\n", tmp, "?>\n", NULL) :
-				g_strconcat("<!--\n", tmp, "-->\n", NULL);
-			g_free(tmp);
-			return block;
+		case GEANY_FILETYPES_XML:
+		case GEANY_FILETYPES_DOCBOOK:
+		{
+			frame_start = "<!--\n";
+			frame_end = "-->\n";
+			line_prefix = "";
+			break;
 		}
 
 		case GEANY_FILETYPES_PYTHON:
@@ -438,136 +316,145 @@
 		case GEANY_FILETYPES_SH:
 		case GEANY_FILETYPES_MAKE:
 		case GEANY_FILETYPES_PERL:
-		return g_strconcat("#\n", comment_text, "#\n", NULL);
+		case GEANY_FILETYPES_DIFF:
+		case GEANY_FILETYPES_TCL:
+		case GEANY_FILETYPES_OMS:
+		case GEANY_FILETYPES_CONF:
+		{
+			line_prefix = "#";
+			break;
+		}
 
+		case GEANY_FILETYPES_LATEX:
+		{
+			line_prefix = "%";
+			break;
+		}
+
+		case GEANY_FILETYPES_VHDL:
+		{
+			line_prefix = "--";
+			break;
+		}
+
+		case GEANY_FILETYPES_FORTRAN:
+		{
+			line_prefix = "c";
+			break;
+		}
+
+		case GEANY_FILETYPES_ASM:
+		{
+			line_prefix = ";";
+			break;
+		}
+
 		case GEANY_FILETYPES_PASCAL:
-		return g_strconcat("{\n", comment_text, "}\n", NULL);
+		{
+			frame_start = "{\n";
+			frame_end = "}\n";
+			line_prefix = "";
+			break;
+		}
 
-		default:
-		return g_strconcat("/*\n", comment_text, " */\n", NULL);
+		case GEANY_FILETYPES_PHP:
+		{
+			frame_start = "<?\n/*\n";
+			frame_end = " */\n?>\n";
+			line_prefix = " *";
+			break;
+		}
+
+		case GEANY_FILETYPES_CAML:
+		{
+			frame_start = "(*\n";
+			frame_end = " *)\n";
+			line_prefix = " *";
+			break;
+		}
+
+		case GEANY_FILETYPES_ALL:
+		{
+			line_prefix = "";
+			break;
+		}
+
+		default: // guess /* */ is appropriate
+		{
+			frame_start = "/*\n";
+			frame_end = " */\n";
+			line_prefix = " *";
+		}
 	}
-}
 
+	// construct the real prefix with given amount of whitespace
+	i = (indent > strlen(line_prefix)) ? (indent - strlen(line_prefix)) : strlen(line_prefix);
+	tmp = g_strnfill(i, ' ');
+	prefix = g_strconcat(line_prefix, tmp, NULL);
+	g_free(tmp);
 
-gchar *templates_get_template_gpl(gint filetype_idx)
-{
-	const gchar *text;
 
-	switch (filetype_idx)
+	// add line_prefix to every line of comment_text
+	lines = g_strsplit(comment_text, "\n", -1);
+	for (i = 0; i < (g_strv_length(lines) - 1); i++)
 	{
-		case GEANY_FILETYPES_PYTHON:
-		case GEANY_FILETYPES_RUBY:
-		case GEANY_FILETYPES_SH:
-		case GEANY_FILETYPES_MAKE:
-		case GEANY_FILETYPES_PERL:
-		text = templates[GEANY_TEMPLATE_GPL_ROUTE];
-		break;
+		tmp = lines[i];
+		lines[i] = g_strconcat(prefix, tmp, NULL);
+		g_free(tmp);
+	}
+	tmp = g_strjoinv("\n", lines);
 
-		case GEANY_FILETYPES_PASCAL:
-		case GEANY_FILETYPES_ALL:
-		text = templates[GEANY_TEMPLATE_GPL_PASCAL];
-		break;
+	// add frame_start and frame_end
+	result = g_strconcat(frame_start, tmp, frame_end, NULL);
 
-		case GEANY_FILETYPES_HTML:
-		case GEANY_FILETYPES_PHP:
-		default:
-		text = templates[GEANY_TEMPLATE_GPL];
-		break;
-	}
-	return make_comment_block(text, filetype_idx, TRUE);
+	g_free(prefix);
+	g_free(tmp);
+	g_strfreev(lines);
+	return result;
 }
 
 
-/* Returns a template chosen by template with GEANY_STRING_UNTITLED.extension
- * as filename if idx is -1, or the real filename if idx is greater than -1.
- * The flag gpl decides whether a GPL notice is appended or not */
-static gchar *
-prepare_file_header(gint template, const gchar *extension, const gchar *filename)
+gchar *templates_get_template_licence(gint filetype_idx)
 {
-	gchar *result = g_strdup(templates[template]);
+	//if (licence_type != GEANY_TEMPLATE_GPL)
+		//return NULL;
+
+	return make_comment_block(templates[GEANY_TEMPLATE_GPL], filetype_idx, 8);
+}
+
+
+static gchar *get_file_header(filetype *ft, const gchar *fname)
+{
+	gchar *template = g_strdup(templates[GEANY_TEMPLATE_FILEHEADER]);
 	gchar *shortname;
+	gchar *result;
 	gchar *date = utils_get_date_time();
 
-	if (filename == NULL)
+	if (fname == NULL)
 	{
-		if (extension != NULL)
-			shortname = g_strconcat(GEANY_STRING_UNTITLED, ".", extension, NULL);
-		else
+		if (FILETYPE_ID(ft) == GEANY_FILETYPES_ALL)
 			shortname = g_strdup(GEANY_STRING_UNTITLED);
+		else
+			shortname = g_strconcat(GEANY_STRING_UNTITLED, ".", ft->extension, NULL);
 	}
 	else
-	{
-		shortname = g_path_get_basename(filename);
-	}
-	result = utils_str_replace(result, "{filename}", shortname);
+		shortname = g_path_get_basename(fname);
 
-	if (template == GEANY_TEMPLATE_FILEHEADER_PASCAL)
-	{
-		result = utils_str_replace(result, "{gpl}", templates[GEANY_TEMPLATE_GPL_PASCAL]);
-	}
-	else if (template == GEANY_TEMPLATE_FILEHEADER_ROUTE)
-	{
-		result = utils_str_replace(result, "{gpl}", templates[GEANY_TEMPLATE_GPL_ROUTE]);
-	}
-	else
-	{
-		result = utils_str_replace(result, "{gpl}", templates[GEANY_TEMPLATE_GPL]);
-	}
-	result = utils_str_replace(result, "{datetime}", date);
+	template = utils_str_replace(template, "{filename}", shortname);
 
+	template = utils_str_replace(template, "{gpl}", templates[GEANY_TEMPLATE_GPL]);
+
+	template = utils_str_replace(template, "{datetime}", date);
+
+	result = make_comment_block(template, FILETYPE_ID(ft), 8);
+
+	g_free(template);
 	g_free(shortname);
 	g_free(date);
 	return result;
 }
 
 
-// ft, fname can be NULL
-static gchar *get_file_header(filetype *ft, const gchar *fname)
-{
-	gchar *text = NULL;
-
-	switch (FILETYPE_ID(ft))
-	{
-		case GEANY_FILETYPES_ALL:	// ft may be NULL
-		{
-			text = prepare_file_header(GEANY_TEMPLATE_FILEHEADER, NULL, fname);
-			break;
-		}
-		case GEANY_FILETYPES_PHP:
-		case GEANY_FILETYPES_HTML:
-		{
-			gchar *tmp = prepare_file_header(
-					GEANY_TEMPLATE_FILEHEADER, ft->extension, fname);
-			text = make_comment_block(tmp, ft->id, FALSE);
-			g_free(tmp);
-			break;
-		}
-		case GEANY_FILETYPES_PASCAL:
-		{	// Pascal: comments are in { } brackets
-			text = prepare_file_header(
-					GEANY_TEMPLATE_FILEHEADER_PASCAL, ft->extension, fname);
-			break;
-		}
-		case GEANY_FILETYPES_PYTHON:
-		case GEANY_FILETYPES_RUBY:
-		case GEANY_FILETYPES_SH:
-		case GEANY_FILETYPES_MAKE:
-		case GEANY_FILETYPES_PERL:
-		{
-			text = prepare_file_header(
-					GEANY_TEMPLATE_FILEHEADER_ROUTE, ft->extension, fname);
-			break;
-		}
-		default:
-		{	// -> C, C++, Java, ...
-			text = prepare_file_header(
-					GEANY_TEMPLATE_FILEHEADER, ft->extension, fname);
-		}
-	}
-	return text;
-}
-
-
 gchar *templates_get_template_fileheader(gint idx)
 {
 	gchar *fname;
@@ -632,21 +519,26 @@
 }
 
 
-gchar *templates_get_template_function(gint template, const gchar *func_name)
+gchar *templates_get_template_function(gint filetype_idx, const gchar *func_name)
 {
-	gchar *result = g_strdup(templates[template]);
+	gchar *template = g_strdup(templates[GEANY_TEMPLATE_FUNCTION]);
 	gchar *date = utils_get_date();
 	gchar *datetime = utils_get_date_time();
+	gchar *result;
 
-	result = utils_str_replace(result, "{date}", date);
-	result = utils_str_replace(result, "{datetime}", datetime);
-	result = utils_str_replace(result, "{functionname}", (func_name) ? func_name : "");
+	template = utils_str_replace(template, "{date}", date);
+	template = utils_str_replace(template, "{datetime}", datetime);
+	template = utils_str_replace(template, "{functionname}", (func_name) ? func_name : "");
 
+	result = make_comment_block(template, filetype_idx, 3);
+
+	g_free(template);
 	g_free(date);
 	g_free(datetime);
 	return result;
 }
 
+
 gchar *templates_get_template_changelog(void)
 {
 	gchar *date = utils_get_date_time();

Modified: trunk/src/templates.h
===================================================================
--- trunk/src/templates.h	2007-01-06 12:35:43 UTC (rev 1159)
+++ trunk/src/templates.h	2007-01-06 15:03:53 UTC (rev 1160)
@@ -1,7 +1,8 @@
 /*
  *      templates.h - this file is part of Geany, a fast and lightweight IDE
  *
- *      Copyright 2006 Enrico Troeger <enrico.troeger at uvena.de>
+ *      Copyright 2005-2007 Enrico Troeger <enrico.troeger at uvena.de>
+ *      Copyright 2006-2007 Nick Treleaven <nick.treleaven at btinternet.com>
  *
  *      This program is free software; you can redistribute it and/or modify
  *      it under the terms of the GNU General Public License as published by
@@ -25,7 +26,6 @@
 
 #include "filetypes.h"
 
-
 void templates_init(void);
 
 gchar *templates_get_template_fileheader(gint idx);
@@ -36,28 +36,19 @@
 
 gchar *templates_get_template_generic(gint template);
 
-gchar *templates_get_template_function(gint template, const gchar *func_name);
+gchar *templates_get_template_function(gint filetype_idx, const gchar *func_name);
 
-gchar *templates_get_template_gpl(gint filetype_idx);
+gchar *templates_get_template_licence(gint filetype_idx);
 
 void templates_free_templates(void);
 
 
 enum
 {
-	GEANY_TEMPLATE_GPL_PASCAL = 0,
-	GEANY_TEMPLATE_GPL_ROUTE,
-	GEANY_TEMPLATE_GPL,
-	GEANY_TEMPLATE_FILEHEADER_PASCAL,
-	GEANY_TEMPLATE_FILEHEADER_ROUTE,
+	GEANY_TEMPLATE_GPL = 0,
 	GEANY_TEMPLATE_FILEHEADER,
 	GEANY_TEMPLATE_CHANGELOG,
 	GEANY_TEMPLATE_FUNCTION,
-	GEANY_TEMPLATE_FUNCTION_PASCAL,
-	GEANY_TEMPLATE_FUNCTION_ROUTE,
-	GEANY_TEMPLATE_MULTILINE,
-	GEANY_TEMPLATE_MULTILINE_PASCAL,
-	GEANY_TEMPLATE_MULTILINE_ROUTE,
 
 	GEANY_TEMPLATE_FILETYPE_NONE,
 	GEANY_TEMPLATE_FILETYPE_C,


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