choosesyntax.c 3.3 KB

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