Merge branch '1790_mc_crash'
[midnight-commander.git] / edit / choosesyntax.c
blob20df93526329e87f7ee7c8c4806c46bcfe7de1ea
1 /* User interface for syntax selection.
3 Copyright (C) 2005, 2006 Leonard den Ottolander <leonard den ottolander nl>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 /** \file
20 * \brief Source: user %interface for syntax %selection
21 * \author Leonard den Ottolander
22 * \date 2005, 2006
25 #include <config.h>
27 #include <stdlib.h>
29 #include "../src/global.h"
30 #include "../src/wtools.h"
32 #include "edit-impl.h"
34 #define MAX_ENTRY_LEN 40
35 #define LIST_LINES 14
36 #define N_DFLT_ENTRIES 2
38 static int
39 pstrcmp(const void *p1, const void *p2)
41 return strcmp(*(char**)p1, *(char**)p2);
44 static int
45 exec_edit_syntax_dialog (const char **names) {
46 int i;
48 Listbox *syntaxlist = create_listbox_window (LIST_LINES, MAX_ENTRY_LEN,
49 _(" Choose syntax highlighting "), NULL);
50 LISTBOX_APPEND_TEXT (syntaxlist, 'A', _("< Auto >"), NULL);
51 LISTBOX_APPEND_TEXT (syntaxlist, 'R', _("< Reload Current Syntax >"), NULL);
53 for (i = 0; names[i]; i++) {
54 LISTBOX_APPEND_TEXT (syntaxlist, 0, names[i], NULL);
55 if (! option_auto_syntax && option_syntax_type &&
56 (strcmp (names[i], option_syntax_type) == 0))
57 listbox_select_by_number (syntaxlist->list, i + N_DFLT_ENTRIES);
60 return run_listbox (syntaxlist);
63 void
64 edit_syntax_dialog (void) {
65 char *old_syntax_type;
66 int old_auto_syntax, syntax;
67 char **names;
68 int i;
69 int force_reload = 0;
70 int count = 0;
72 names = (char**) g_malloc (sizeof (char*));
73 names[0] = NULL;
74 /* We fill the list of syntax files every time the editor is invoked.
75 Instead we could save the list to a file and update it once the syntax
76 file gets updated (either by testing or by explicit user command). */
77 edit_load_syntax (NULL, &names, NULL);
78 while (names[count++] != NULL);
79 qsort(names, count - 1, sizeof(char*), pstrcmp);
81 if ((syntax = exec_edit_syntax_dialog ((const char**) names)) < 0) {
82 for (i = 0; names[i]; i++) {
83 g_free (names[i]);
85 g_free (names);
86 return;
89 old_auto_syntax = option_auto_syntax;
90 old_syntax_type = g_strdup (option_syntax_type);
92 switch (syntax) {
93 case 0: /* auto syntax */
94 option_auto_syntax = 1;
95 break;
96 case 1: /* reload current syntax */
97 force_reload = 1;
98 break;
99 default:
100 option_auto_syntax = 0;
101 g_free (option_syntax_type);
102 option_syntax_type = g_strdup (names[syntax - N_DFLT_ENTRIES]);
105 /* Load or unload syntax rules if the option has changed */
106 if ((option_auto_syntax && !old_auto_syntax) || old_auto_syntax ||
107 (old_syntax_type && option_syntax_type &&
108 (strcmp (old_syntax_type, option_syntax_type) != 0)) ||
109 force_reload)
110 edit_load_syntax (wedit, NULL, option_syntax_type);
112 for (i = 0; names[i]; i++) {
113 g_free (names[i]);
115 g_free (names);
116 g_free (old_syntax_type);