choosesyntax.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /** \file
  15. * \brief Source: user %interface for syntax %selection
  16. * \author Leonard den Ottolander
  17. * \date 2005, 2006
  18. */
  19. #include <config.h>
  20. #include <stdlib.h>
  21. #include "../src/global.h"
  22. #include "../src/wtools.h"
  23. #include "edit-impl.h"
  24. #define MAX_ENTRY_LEN 40
  25. #define LIST_LINES 14
  26. #define N_DFLT_ENTRIES 2
  27. static int
  28. pstrcmp(const void *p1, const void *p2)
  29. {
  30. return strcmp(*(char**)p1, *(char**)p2);
  31. }
  32. static int
  33. exec_edit_syntax_dialog (const char **names) {
  34. int i;
  35. Listbox *syntaxlist = create_listbox_window (LIST_LINES, MAX_ENTRY_LEN,
  36. _(" Choose syntax highlighting "), NULL);
  37. LISTBOX_APPEND_TEXT (syntaxlist, 'A', _("< Auto >"), NULL);
  38. LISTBOX_APPEND_TEXT (syntaxlist, 'R', _("< Reload Current Syntax >"), NULL);
  39. for (i = 0; names[i]; i++) {
  40. LISTBOX_APPEND_TEXT (syntaxlist, 0, names[i], NULL);
  41. if (! option_auto_syntax && option_syntax_type &&
  42. (strcmp (names[i], option_syntax_type) == 0))
  43. listbox_select_by_number (syntaxlist->list, i + N_DFLT_ENTRIES);
  44. }
  45. return run_listbox (syntaxlist);
  46. }
  47. void
  48. edit_syntax_dialog (void) {
  49. char *old_syntax_type;
  50. int old_auto_syntax, syntax;
  51. char **names;
  52. int i;
  53. int force_reload = 0;
  54. int count = 0;
  55. names = (char**) g_malloc (sizeof (char*));
  56. names[0] = NULL;
  57. /* We fill the list of syntax files every time the editor is invoked.
  58. Instead we could save the list to a file and update it once the syntax
  59. file gets updated (either by testing or by explicit user command). */
  60. edit_load_syntax (NULL, &names, NULL);
  61. while (names[count++] != NULL);
  62. qsort(names, count - 1, sizeof(char*), pstrcmp);
  63. if ((syntax = exec_edit_syntax_dialog ((const char**) names)) < 0) {
  64. for (i = 0; names[i]; i++) {
  65. g_free (names[i]);
  66. }
  67. g_free (names);
  68. return;
  69. }
  70. old_auto_syntax = option_auto_syntax;
  71. old_syntax_type = g_strdup (option_syntax_type);
  72. switch (syntax) {
  73. case 0: /* auto syntax */
  74. option_auto_syntax = 1;
  75. break;
  76. case 1: /* reload current syntax */
  77. force_reload = 1;
  78. break;
  79. default:
  80. option_auto_syntax = 0;
  81. g_free (option_syntax_type);
  82. option_syntax_type = g_strdup (names[syntax - N_DFLT_ENTRIES]);
  83. }
  84. /* Load or unload syntax rules if the option has changed */
  85. if ((option_auto_syntax && !old_auto_syntax) || old_auto_syntax ||
  86. (old_syntax_type && option_syntax_type &&
  87. (strcmp (old_syntax_type, option_syntax_type) != 0)) ||
  88. force_reload)
  89. edit_load_syntax (wedit, NULL, option_syntax_type);
  90. for (i = 0; names[i]; i++) {
  91. g_free (names[i]);
  92. }
  93. g_free (names);
  94. g_free (old_syntax_type);
  95. }