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.
20 * \brief Source: user %interface for syntax %selection
21 * \author Leonard den Ottolander
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
38 #define N_DFLT_ENTRIES 2
41 pstrcmp (const void *p1
, const void *p2
)
43 return strcmp (*(char **) p1
, *(char **) p2
);
47 exec_edit_syntax_dialog (const char **names
, const char *current_syntax
)
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
);
67 edit_syntax_dialog (WEdit
* edit
, const char *current_syntax
)
69 char *old_syntax_type
;
70 int old_auto_syntax
, syntax
;
72 gboolean force_reload
= FALSE
;
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
);
91 old_auto_syntax
= option_auto_syntax
;
92 old_syntax_type
= g_strdup (current_syntax
);
96 case 0: /* auto syntax */
97 option_auto_syntax
= 1;
99 case 1: /* reload current syntax */
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
);
115 g_free (old_syntax_type
);