lib/widget/input_complete.c: minor refactoring and optimization.
[midnight-commander.git] / src / editor / choosesyntax.c
blob4e8cdbd09b1d74d5438849f43ca475cf749119a7
1 /*
2 User interface for syntax selection.
4 Copyright (C) 2011
5 The Free Software Foundation, Inc.
7 Copyright (C) 2005, 2006
8 Leonard den Ottolander <leonard den ottolander nl>
10 Written by:
11 Leonard den Ottolander <leonard den ottolander nl>, 2005, 2006
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /** \file
30 * \brief Source: user %interface for syntax %selection
31 * \author Leonard den Ottolander
32 * \date 2005, 2006
35 #include <config.h>
37 #include <stdlib.h>
38 #include <sys/types.h>
40 #include "lib/global.h"
41 #include "lib/widget.h" /* Listbox */
43 #include "edit-impl.h"
44 #include "editwidget.h"
46 /*** global variables ****************************************************************************/
48 /*** file scope macro definitions ****************************************************************/
50 #define MAX_ENTRY_LEN 40
51 #define LIST_LINES 14
52 #define N_DFLT_ENTRIES 2
54 /*** file scope type declarations ****************************************************************/
56 /*** file scope variables ************************************************************************/
58 /*** file scope functions ************************************************************************/
59 /* --------------------------------------------------------------------------------------------- */
61 static int
62 pstrcmp (const void *p1, const void *p2)
64 return strcmp (*(char **) p1, *(char **) p2);
67 /* --------------------------------------------------------------------------------------------- */
69 static int
70 exec_edit_syntax_dialog (const char **names, const char *current_syntax)
72 size_t i;
74 Listbox *syntaxlist = create_listbox_window (LIST_LINES, MAX_ENTRY_LEN,
75 _("Choose syntax highlighting"), NULL);
76 LISTBOX_APPEND_TEXT (syntaxlist, 'A', _("< Auto >"), NULL);
77 LISTBOX_APPEND_TEXT (syntaxlist, 'R', _("< Reload Current Syntax >"), NULL);
79 for (i = 0; names[i] != NULL; i++)
81 LISTBOX_APPEND_TEXT (syntaxlist, 0, names[i], NULL);
82 if ((current_syntax != NULL) && (strcmp (names[i], current_syntax) == 0))
83 listbox_select_entry (syntaxlist->list, i + N_DFLT_ENTRIES);
86 return run_listbox (syntaxlist);
89 /* --------------------------------------------------------------------------------------------- */
90 /*** public functions ****************************************************************************/
91 /* --------------------------------------------------------------------------------------------- */
93 void
94 edit_syntax_dialog (WEdit * edit)
96 char *current_syntax;
97 int old_auto_syntax, syntax;
98 char **names;
99 gboolean force_reload = FALSE;
100 size_t count;
102 current_syntax = g_strdup (edit->syntax_type);
103 old_auto_syntax = option_auto_syntax;
105 names = g_new0 (char *, 1);
107 /* We fill the list of syntax files every time the editor is invoked.
108 Instead we could save the list to a file and update it once the syntax
109 file gets updated (either by testing or by explicit user command). */
110 edit_load_syntax (NULL, &names, NULL);
111 count = g_strv_length (names);
112 qsort (names, count, sizeof (char *), pstrcmp);
114 syntax = exec_edit_syntax_dialog ((const char **) names, current_syntax);
115 if (syntax >= 0)
117 switch (syntax)
119 case 0: /* auto syntax */
120 option_auto_syntax = 1;
121 break;
122 case 1: /* reload current syntax */
123 force_reload = TRUE;
124 break;
125 default:
126 option_auto_syntax = 0;
127 g_free (edit->syntax_type);
128 edit->syntax_type = g_strdup (names[syntax - N_DFLT_ENTRIES]);
131 /* Load or unload syntax rules if the option has changed */
132 if ((option_auto_syntax && !old_auto_syntax) || old_auto_syntax ||
133 (current_syntax && edit->syntax_type &&
134 (strcmp (current_syntax, edit->syntax_type) != 0)) || force_reload)
135 edit_load_syntax (edit, NULL, edit->syntax_type);
137 g_free (current_syntax);
140 g_strfreev (names);
143 /* --------------------------------------------------------------------------------------------- */