choosesyntax.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* User interface for syntax selection.
  2. Copyright (C) 2005, 2006 Leonard den Ottolander <leonard den ottolander nl>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License version 2 as
  5. published by the Free Software Foundation.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software Foundation,
  12. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  13. */
  14. #include <config.h>
  15. #include "edit.h"
  16. #include "../src/global.h"
  17. #include "../src/wtools.h"
  18. #define MAX_ENTRY_LEN 40
  19. #define LIST_LINES 14
  20. #define N_DFLT_ENTRIES 2
  21. static int
  22. pstrcmp(const void *p1, const void *p2)
  23. {
  24. return strcmp(*(char**)p1, *(char**)p2);
  25. }
  26. static int
  27. exec_edit_syntax_dialog (const char **names) {
  28. int i;
  29. Listbox *syntaxlist = create_listbox_window (MAX_ENTRY_LEN, LIST_LINES,
  30. _(" Choose syntax highlighting "), NULL);
  31. LISTBOX_APPEND_TEXT (syntaxlist, 'A', _("< Auto >"), NULL);
  32. LISTBOX_APPEND_TEXT (syntaxlist, 'R', _("< Reload Current Syntax >"), NULL);
  33. for (i = 0; names[i]; i++) {
  34. LISTBOX_APPEND_TEXT (syntaxlist, 0, names[i], NULL);
  35. if (! option_auto_syntax && option_syntax_type &&
  36. (strcmp (names[i], option_syntax_type) == 0))
  37. listbox_select_by_number (syntaxlist->list, i + N_DFLT_ENTRIES);
  38. }
  39. return run_listbox (syntaxlist);
  40. }
  41. void
  42. edit_syntax_dialog (void) {
  43. char *old_syntax_type;
  44. int old_auto_syntax, syntax;
  45. char **names;
  46. int i;
  47. int force_reload = 0;
  48. int count = 0;
  49. names = (char**) g_malloc (sizeof (char*));
  50. names[0] = NULL;
  51. /* We fill the list of syntax files every time the editor is invoked.
  52. Instead we could save the list to a file and update it once the syntax
  53. file gets updated (either by testing or by explicit user command). */
  54. edit_load_syntax (NULL, &names, NULL);
  55. while (names[count++] != NULL);
  56. qsort(names, count - 1, sizeof(char*), pstrcmp);
  57. if ((syntax = exec_edit_syntax_dialog ((const char**) names)) < 0) {
  58. for (i = 0; names[i]; i++) {
  59. g_free (names[i]);
  60. }
  61. g_free (names);
  62. return;
  63. }
  64. old_auto_syntax = option_auto_syntax;
  65. old_syntax_type = g_strdup (option_syntax_type);
  66. switch (syntax) {
  67. case 0: /* auto syntax */
  68. option_auto_syntax = 1;
  69. break;
  70. case 1: /* reload current syntax */
  71. force_reload = 1;
  72. break;
  73. default:
  74. option_auto_syntax = 0;
  75. g_free (option_syntax_type);
  76. option_syntax_type = g_strdup (names[syntax - N_DFLT_ENTRIES]);
  77. }
  78. /* Load or unload syntax rules if the option has changed */
  79. if ((option_auto_syntax && !old_auto_syntax) || old_auto_syntax ||
  80. (old_syntax_type && option_syntax_type &&
  81. (strcmp (old_syntax_type, option_syntax_type) != 0)) ||
  82. force_reload)
  83. edit_load_syntax (wedit, NULL, option_syntax_type);
  84. for (i = 0; names[i]; i++) {
  85. g_free (names[i]);
  86. }
  87. g_free (names);
  88. g_free (old_syntax_type);
  89. }