2 User interface for syntax selection.
5 The Free Software Foundation, Inc.
7 Copyright (C) 2005, 2006
8 Leonard den Ottolander <leonard den ottolander nl>
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/>.
30 * \brief Source: user %interface for syntax %selection
31 * \author Leonard den Ottolander
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
52 #define N_DFLT_ENTRIES 2
54 /*** file scope type declarations ****************************************************************/
56 /*** file scope variables ************************************************************************/
58 /*** file scope functions ************************************************************************/
59 /* --------------------------------------------------------------------------------------------- */
62 pstrcmp (const void *p1
, const void *p2
)
64 return strcmp (*(char **) p1
, *(char **) p2
);
67 /* --------------------------------------------------------------------------------------------- */
70 exec_edit_syntax_dialog (const char **names
, const char *current_syntax
)
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 /* --------------------------------------------------------------------------------------------- */
94 edit_syntax_dialog (WEdit
* edit
)
97 int old_auto_syntax
, syntax
;
99 gboolean force_reload
= FALSE
;
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
);
119 case 0: /* auto syntax */
120 option_auto_syntax
= 1;
122 case 1: /* reload current syntax */
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
);
143 /* --------------------------------------------------------------------------------------------- */