choosesyntax.c 3.2 KB

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