[PATCH] Overriding automatic syntax detection



Hi,

Today I've been scratching an itch that I had for a while: An option to
override the auto syntax recognition.

Luckily the hooks to provide a syntax type are already available in the
code. In the editors menu I added a menu entry "Syntax &Highlighting".
Assuming syntax highlighting is enabled in the General menu you can
disable automatic syntax detection and enter a type in the quick_input.
These types should equal the second entries in Syntax file file lines,
so use something like "PHP Program" or "C/C++ Program".

If you drop this patch into an existing tree you will have to run
autoreconf to regenerate edit/Makefile.in. It is not provided in this
patch.

This code is (obviously) inspired by editoptions.c. It currently misses 
i18n translations and it is probably a good idea to auto fill the syntax
type list (which currently gets saved in ~/.mc/history). It is quite
possible that I should use a different widget instead of quick_input for
this list (it may be static wrt user input and it should be auto
generated or pre filled). Suggestions are welcome.

Leonard.

-- 
mount -t life -o ro /dev/dna /genetic/research

diff -pruN MC_4_6_1_PRE/mc/edit/edit.c MC_4_6_1_PRE.syntax/mc/edit/edit.c
--- MC_4_6_1_PRE/mc/edit/edit.c	2005-05-29 12:04:00.000000000 +0200
+++ MC_4_6_1_PRE.syntax/mc/edit/edit.c	2005-07-06 17:12:15.000000000 +0200
@@ -550,7 +550,10 @@ edit_init (WEdit *edit, int lines, int c
     edit->loading_done = 1;
     edit->modified = 0;
     edit->locked = 0;
-    edit_load_syntax (edit, 0, 0);
+    if (option_auto_syntax)
+	edit_load_syntax (edit, 0, 0);
+    else
+	edit_load_syntax (edit, 0, option_syntax_type);
     {
 	int color;
 	edit_get_syntax_color (edit, -1, &color);
diff -pruN MC_4_6_1_PRE/mc/edit/editcmd.c MC_4_6_1_PRE.syntax/mc/edit/editcmd.c
--- MC_4_6_1_PRE/mc/edit/editcmd.c	2005-05-29 12:04:00.000000000 +0200
+++ MC_4_6_1_PRE.syntax/mc/edit/editcmd.c	2005-07-06 17:11:59.000000000 +0200
@@ -512,7 +512,10 @@ edit_save_as_cmd (WEdit *edit)
 		edit->modified = 0;
 		edit->delete_file = 0;
 		if (different_filename)
-		    edit_load_syntax (edit, 0, 0);
+		    if (option_auto_syntax)
+			edit_load_syntax (edit, 0, 0);
+		    else
+			edit_load_syntax (edit, 0, option_syntax_type);
 		edit->force |= REDRAW_COMPLETELY;
 		return 1;
 	    } else {
diff -pruN MC_4_6_1_PRE/mc/edit/edit.h MC_4_6_1_PRE.syntax/mc/edit/edit.h
--- MC_4_6_1_PRE/mc/edit/edit.h	2005-05-29 12:04:00.000000000 +0200
+++ MC_4_6_1_PRE.syntax/mc/edit/edit.h	2005-07-06 16:58:39.000000000 +0200
@@ -252,6 +252,7 @@ int line_is_blank (WEdit *edit, long lin
 int edit_indent_width (WEdit *edit, long p);
 void edit_insert_indent (WEdit *edit, int indent);
 void edit_options_dialog (void);
+void set_syntax_dialog (void);
 void edit_mail_dialog (WEdit *edit);
 void format_paragraph (WEdit *edit, int force);
 
@@ -311,6 +312,8 @@ extern int option_save_position;
 extern int option_backup_ext_int;
 extern int option_max_undo;
 extern int option_syntax_highlighting;
+extern int option_auto_syntax;
+extern char *option_syntax_type;
 extern int editor_option_check_nl_at_eof;
 
 extern int option_edit_right_extreme;
diff -pruN MC_4_6_1_PRE/mc/edit/editmenu.c MC_4_6_1_PRE.syntax/mc/edit/editmenu.c
--- MC_4_6_1_PRE/mc/edit/editmenu.c	2005-05-29 12:04:00.000000000 +0200
+++ MC_4_6_1_PRE.syntax/mc/edit/editmenu.c	2005-07-05 22:49:51.000000000 +0200
@@ -272,6 +272,13 @@ menu_options (void)
 {
     edit_options_dialog ();
 }
+
+static void
+menu_syntax (void)
+{
+    set_syntax_dialog ();
+}
+
 static void
 menu_user_menu_cmd (void)
 {
@@ -394,7 +401,8 @@ static menu_entry OptMenu[] =
 {
     {' ', N_("&General...  "), 'G', menu_options},
     {' ', N_("&Save mode..."), 'S', menu_save_mode_cmd},
-    {' ', N_("learn &Keys..."), 'K', learn_keys}
+    {' ', N_("learn &Keys..."), 'K', learn_keys},
+    {' ', N_("Syntax &Highlighting..."), 'H', menu_syntax}
 };
 
 #define OptMenuEmacs OptMenu
diff -pruN MC_4_6_1_PRE/mc/edit/editoptions.c MC_4_6_1_PRE.syntax/mc/edit/editoptions.c
--- MC_4_6_1_PRE/mc/edit/editoptions.c	2005-05-29 12:04:00.000000000 +0200
+++ MC_4_6_1_PRE.syntax/mc/edit/editoptions.c	2005-07-06 17:07:26.000000000 +0200
@@ -199,6 +199,10 @@ edit_options_dialog (void)
     edit_key_emulation = tedit_key_emulation;
 
     /* Load or unload syntax rules if the option has changed */
-    if (option_syntax_highlighting != old_syntax_hl)
-	edit_load_syntax (wedit, 0, 0);
+    if (option_syntax_highlighting != old_syntax_hl) {
+	if (option_auto_syntax)
+	    edit_load_syntax (wedit, 0, 0);
+	else
+	    edit_load_syntax (wedit, 0, option_syntax_type);
+    }
 }
diff -pruN MC_4_6_1_PRE/mc/edit/Makefile.am MC_4_6_1_PRE.syntax/mc/edit/Makefile.am
--- MC_4_6_1_PRE/mc/edit/Makefile.am	2003-04-02 21:36:10.000000000 +0200
+++ MC_4_6_1_PRE.syntax/mc/edit/Makefile.am	2005-07-05 22:57:28.000000000 +0200
@@ -9,6 +9,6 @@ endif
 libedit_a_SOURCES = \
 	bookmark.c edit.c editcmd.c editwidget.c editdraw.c editkeys.c \
 	editmenu.c editoptions.c editcmddef.h edit.h edit-widget.h \
-	editlock.c editlock.h syntax.c wordproc.c
+	editlock.c editlock.h picksyntax.c syntax.c wordproc.c
 
 EXTRA_DIST = ChangeLog
diff -pruN MC_4_6_1_PRE/mc/edit/picksyntax.c MC_4_6_1_PRE.syntax/mc/edit/picksyntax.c
--- MC_4_6_1_PRE/mc/edit/picksyntax.c	1970-01-01 01:00:00.000000000 +0100
+++ MC_4_6_1_PRE.syntax/mc/edit/picksyntax.c	2005-07-06 18:59:38.000000000 +0200
@@ -0,0 +1,102 @@
+/* syntax highlighting dialog box
+
+   Copyright (C) 2005, Leonard den Ottolander
+
+   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
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
+*/
+
+#include <config.h>
+#include "edit.h"
+#include "../src/dialog.h"	/* B_CANCEL */
+#include "../src/wtools.h"	/* QuickDialog */
+
+#define SYN_DLG_H 11
+#define SYN_DLG_W 72
+
+#ifndef USE_INTERNAL_EDIT
+#define USE_INTERNAL_EDIT 1
+#endif
+
+void
+set_syntax_dialog (void)
+{
+/*    char *tsyntax_type_string, *p; */
+    char tsyntax_type_string[64], *p;
+    int old_auto_syntax = option_auto_syntax;
+    char *old_syntax_type;
+    int tsyntax_auto_syntax = option_auto_syntax;
+
+    QuickWidget quick_widgets[] = {
+	/* 0 */
+	{quick_button, 5, 10, SYN_DLG_H - 3, SYN_DLG_H,
+	    N_("&Cancel"), 0, B_CANCEL, 0, 0, NULL},
+	/* 1 */
+	{quick_button, 1, 10, SYN_DLG_H - 3, SYN_DLG_H,
+	    N_("&OK"), 0, B_ENTER, 0, 0, NULL},
+	/* 2 */
+	{quick_checkbox, 3, SYN_DLG_W, SYN_DLG_H - 5, SYN_DLG_H,
+/*	    "Auto &Syntax Choice", 8, 0, 0, 0, NULL},*/
+	    N_("Auto &Syntax Choice"), 8, 0, 0, 0, NULL},
+	/* 3 */
+	{quick_input, 3, SYN_DLG_W, SYN_DLG_H - 7, SYN_DLG_H,
+	    "", 58, 0, 0, 0, "syntaxes"},
+	/* 4 */
+        {quick_label, 3, SYN_DLG_W, SYN_DLG_H - 8, SYN_DLG_H,
+/*	    "syntax:", 0, 0, 0, 0, "syntax"},*/
+	    N_("syntax:"), 0, 0, 0, 0, "syntax"},
+
+	NULL_QuickWidget
+    };
+
+    QuickDialog Quick_options =
+	{ SYN_DLG_W, SYN_DLG_H, -1, 0, N_(" Syntax highlighting "), "", 0, 0 };
+
+/*    tsyntax_type_string = gstrndup (option_syntax_type,
+	sizeof (option_syntax_type)); */
+    g_snprintf (tsyntax_type_string, sizeof (tsyntax_type_string), "%s",
+		option_syntax_type);
+
+    quick_widgets[3].text = tsyntax_type_string;
+    quick_widgets[3].str_result = &p;
+    quick_widgets[2].result = &tsyntax_auto_syntax;
+
+    Quick_options.widgets = quick_widgets;
+
+    if (quick_dialog (&Quick_options) == B_CANCEL) /*{
+	g_free (tsyntax_type_string); */
+	return;
+/*    }
+    g_free (tsyntax_type_string); */
+
+    old_auto_syntax = option_auto_syntax;
+    old_syntax_type = g_strdup (option_syntax_type);
+
+    if (p) {
+	g_free (option_syntax_type);
+	option_syntax_type = g_strdup (p);
+	g_free (p);
+    }
+
+    option_auto_syntax = tsyntax_auto_syntax;
+
+    /* Load or unload syntax rules if the option has changed */
+    if (option_auto_syntax) {
+	if (!old_auto_syntax) 
+	    edit_load_syntax (wedit, 0, 0);
+    } else
+	if (old_auto_syntax || strcmp (old_syntax_type, option_syntax_type))
+	    edit_load_syntax (wedit, 0, option_syntax_type);
+}
diff -pruN MC_4_6_1_PRE/mc/edit/syntax.c MC_4_6_1_PRE.syntax/mc/edit/syntax.c
--- MC_4_6_1_PRE/mc/edit/syntax.c	2005-05-29 12:04:00.000000000 +0200
+++ MC_4_6_1_PRE.syntax/mc/edit/syntax.c	2005-07-06 17:01:09.000000000 +0200
@@ -85,6 +85,8 @@ struct _syntax_marker {
 };
 
 int option_syntax_highlighting = 1;
+int option_auto_syntax = 1;
+char *option_syntax_type = NULL;
 
 #define syntax_g_free(x) do {g_free(x); (x)=0;} while (0)
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]