Merge commit 'origin/mc-4.6'
[midnight-commander.git] / edit / choosesyntax.c
blob670f81fdd99837560a90ffa8e94d86c984d6e6d7
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 #include <config.h>
20 #include "edit.h"
21 #include "../src/global.h"
22 #include "../src/wtools.h"
24 #define MAX_ENTRY_LEN 40
25 #define LIST_LINES 14
26 #define N_DFLT_ENTRIES 2
28 static int
29 pstrcmp(const void *p1, const void *p2)
31 return strcmp(*(char**)p1, *(char**)p2);
34 static int
35 exec_edit_syntax_dialog (const char **names) {
36 int i;
38 Listbox *syntaxlist = create_listbox_window (MAX_ENTRY_LEN, LIST_LINES,
39 _(" Choose syntax highlighting "), NULL);
40 LISTBOX_APPEND_TEXT (syntaxlist, 'A', _("< Auto >"), NULL);
41 LISTBOX_APPEND_TEXT (syntaxlist, 'R', _("< Reload Current Syntax >"), NULL);
43 for (i = 0; names[i]; i++) {
44 LISTBOX_APPEND_TEXT (syntaxlist, 0, names[i], NULL);
45 if (! option_auto_syntax && option_syntax_type &&
46 (strcmp (names[i], option_syntax_type) == 0))
47 listbox_select_by_number (syntaxlist->list, i + N_DFLT_ENTRIES);
50 return run_listbox (syntaxlist);
53 void
54 edit_syntax_dialog (void) {
55 char *old_syntax_type;
56 int old_auto_syntax, syntax;
57 char **names;
58 int i;
59 int force_reload = 0;
60 int count = 0;
62 names = (char**) g_malloc (sizeof (char*));
63 names[0] = NULL;
64 /* We fill the list of syntax files every time the editor is invoked.
65 Instead we could save the list to a file and update it once the syntax
66 file gets updated (either by testing or by explicit user command). */
67 edit_load_syntax (NULL, &names, NULL);
68 while (names[count++] != NULL);
69 qsort(names, count - 1, sizeof(char*), pstrcmp);
71 if ((syntax = exec_edit_syntax_dialog ((const char**) names)) < 0) {
72 for (i = 0; names[i]; i++) {
73 g_free (names[i]);
75 g_free (names);
76 return;
79 old_auto_syntax = option_auto_syntax;
80 old_syntax_type = g_strdup (option_syntax_type);
82 switch (syntax) {
83 case 0: /* auto syntax */
84 option_auto_syntax = 1;
85 break;
86 case 1: /* reload current syntax */
87 force_reload = 1;
88 break;
89 default:
90 option_auto_syntax = 0;
91 g_free (option_syntax_type);
92 option_syntax_type = g_strdup (names[syntax - N_DFLT_ENTRIES]);
95 /* Load or unload syntax rules if the option has changed */
96 if ((option_auto_syntax && !old_auto_syntax) || old_auto_syntax ||
97 (old_syntax_type && option_syntax_type &&
98 (strcmp (old_syntax_type, option_syntax_type) != 0)) ||
99 force_reload)
100 edit_load_syntax (wedit, NULL, option_syntax_type);
102 for (i = 0; names[i]; i++) {
103 g_free (names[i]);
105 g_free (names);
106 g_free (old_syntax_type);