keymap files: unification of Fxx keys: move to lower case.
[midnight-commander.git] / src / editor / choosesyntax.c
blob61661d03f199f631e1563ca9c25e47b245a18f27
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 "lib/widget.h" /* Listbox */
33 #include "edit-impl.h"
34 #include "edit-widget.h"
36 /*** global variables ****************************************************************************/
38 /*** file scope macro definitions ****************************************************************/
40 #define MAX_ENTRY_LEN 40
41 #define LIST_LINES 14
42 #define N_DFLT_ENTRIES 2
44 /*** file scope type declarations ****************************************************************/
46 /*** file scope variables ************************************************************************/
48 /*** file scope functions ************************************************************************/
49 /* --------------------------------------------------------------------------------------------- */
51 static int
52 pstrcmp (const void *p1, const void *p2)
54 return strcmp (*(char **) p1, *(char **) p2);
57 /* --------------------------------------------------------------------------------------------- */
59 static int
60 exec_edit_syntax_dialog (const char **names, const char *current_syntax)
62 size_t i;
64 Listbox *syntaxlist = create_listbox_window (LIST_LINES, MAX_ENTRY_LEN,
65 _("Choose syntax highlighting"), NULL);
66 LISTBOX_APPEND_TEXT (syntaxlist, 'A', _("< Auto >"), NULL);
67 LISTBOX_APPEND_TEXT (syntaxlist, 'R', _("< Reload Current Syntax >"), NULL);
69 for (i = 0; names[i] != NULL; i++)
71 LISTBOX_APPEND_TEXT (syntaxlist, 0, names[i], NULL);
72 if ((current_syntax != NULL) && (strcmp (names[i], current_syntax) == 0))
73 listbox_select_entry (syntaxlist->list, i + N_DFLT_ENTRIES);
76 return run_listbox (syntaxlist);
79 /* --------------------------------------------------------------------------------------------- */
80 /*** public functions ****************************************************************************/
81 /* --------------------------------------------------------------------------------------------- */
83 void
84 edit_syntax_dialog (WEdit * edit)
86 char *current_syntax;
87 int old_auto_syntax, syntax;
88 char **names;
89 gboolean force_reload = FALSE;
90 size_t count;
92 current_syntax = g_strdup (edit->syntax_type);
93 old_auto_syntax = option_auto_syntax;
95 names = g_new0 (char *, 1);
97 /* We fill the list of syntax files every time the editor is invoked.
98 Instead we could save the list to a file and update it once the syntax
99 file gets updated (either by testing or by explicit user command). */
100 edit_load_syntax (NULL, &names, NULL);
101 count = g_strv_length (names);
102 qsort (names, count, sizeof (char *), pstrcmp);
104 syntax = exec_edit_syntax_dialog ((const char **) names, current_syntax);
105 if (syntax >= 0)
107 switch (syntax)
109 case 0: /* auto syntax */
110 option_auto_syntax = 1;
111 break;
112 case 1: /* reload current syntax */
113 force_reload = TRUE;
114 break;
115 default:
116 option_auto_syntax = 0;
117 g_free (edit->syntax_type);
118 edit->syntax_type = g_strdup (names[syntax - N_DFLT_ENTRIES]);
121 /* Load or unload syntax rules if the option has changed */
122 if ((option_auto_syntax && !old_auto_syntax) || old_auto_syntax ||
123 (current_syntax && edit->syntax_type &&
124 (strcmp (current_syntax, edit->syntax_type) != 0)) || force_reload)
125 edit_load_syntax (edit, NULL, edit->syntax_type);
127 g_free (current_syntax);
130 g_strfreev (names);
133 /* --------------------------------------------------------------------------------------------- */