Ticket #2127: updated file extension for "sh"
[midnight-commander.git] / src / editor / choosesyntax.c
blob3da1f2dbbd9c180a6c1757973dda868a39e135a1
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>
28 #include <sys/types.h>
30 #include "lib/global.h"
31 #include "src/wtools.h"
33 #include "edit-impl.h"
34 #include "edit-widget.h"
36 #define MAX_ENTRY_LEN 40
37 #define LIST_LINES 14
38 #define N_DFLT_ENTRIES 2
40 static int
41 pstrcmp (const void *p1, const void *p2)
43 return strcmp (*(char **) p1, *(char **) p2);
46 static int
47 exec_edit_syntax_dialog (const char **names, const char *current_syntax)
49 size_t i;
51 Listbox *syntaxlist = create_listbox_window (LIST_LINES, MAX_ENTRY_LEN,
52 _(" Choose syntax highlighting "), NULL);
53 LISTBOX_APPEND_TEXT (syntaxlist, 'A', _("< Auto >"), NULL);
54 LISTBOX_APPEND_TEXT (syntaxlist, 'R', _("< Reload Current Syntax >"), NULL);
56 for (i = 0; names[i] != NULL; i++)
58 LISTBOX_APPEND_TEXT (syntaxlist, 0, names[i], NULL);
59 if ((current_syntax != NULL) && (strcmp (names[i], current_syntax) == 0))
60 listbox_select_entry (syntaxlist->list, i + N_DFLT_ENTRIES);
63 return run_listbox (syntaxlist);
66 void
67 edit_syntax_dialog (WEdit * edit, const char *current_syntax)
69 char *old_syntax_type;
70 int old_auto_syntax, syntax;
71 char **names;
72 gboolean force_reload = FALSE;
73 size_t count;
75 names = g_new0 (char *, 1);
77 /* We fill the list of syntax files every time the editor is invoked.
78 Instead we could save the list to a file and update it once the syntax
79 file gets updated (either by testing or by explicit user command). */
80 edit_load_syntax (NULL, &names, NULL);
81 count = g_strv_length (names);
82 qsort (names, count, sizeof (char *), pstrcmp);
84 syntax = exec_edit_syntax_dialog ((const char **) names, current_syntax);
85 if (syntax < 0)
87 g_strfreev (names);
88 return;
91 old_auto_syntax = option_auto_syntax;
92 old_syntax_type = g_strdup (current_syntax);
94 switch (syntax)
96 case 0: /* auto syntax */
97 option_auto_syntax = 1;
98 break;
99 case 1: /* reload current syntax */
100 force_reload = TRUE;
101 break;
102 default:
103 option_auto_syntax = 0;
104 g_free (edit->syntax_type);
105 edit->syntax_type = g_strdup (names[syntax - N_DFLT_ENTRIES]);
108 /* Load or unload syntax rules if the option has changed */
109 if ((option_auto_syntax && !old_auto_syntax) || old_auto_syntax ||
110 (old_syntax_type && edit->syntax_type &&
111 (strcmp (old_syntax_type, edit->syntax_type) != 0)) || force_reload)
112 edit_load_syntax (edit, NULL, edit->syntax_type);
114 g_strfreev (names);
115 g_free (old_syntax_type);