root/src/editor/choosesyntax.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. pstrcmp
  2. exec_edit_syntax_dialog
  3. edit_syntax_dialog

   1 /*
   2    User interface for syntax selection.
   3 
   4    Copyright (C) 2011-2019
   5    Free Software Foundation, Inc.
   6 
   7    Copyright (C) 2005, 2006
   8    Leonard den Ottolander <leonard den ottolander nl>
   9 
  10    Written by:
  11    Leonard den Ottolander <leonard den ottolander nl>, 2005, 2006
  12 
  13    This file is part of the Midnight Commander.
  14 
  15    The Midnight Commander is free software: you can redistribute it
  16    and/or modify it under the terms of the GNU General Public License as
  17    published by the Free Software Foundation, either version 3 of the License,
  18    or (at your option) any later version.
  19 
  20    The Midnight Commander is distributed in the hope that it will be useful,
  21    but WITHOUT ANY WARRANTY; without even the implied warranty of
  22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23    GNU General Public License for more details.
  24 
  25    You should have received a copy of the GNU General Public License
  26    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  27  */
  28 
  29 /** \file
  30  *  \brief Source: user %interface for syntax %selection
  31  *  \author Leonard den Ottolander
  32  *  \date 2005, 2006
  33  */
  34 
  35 #include <config.h>
  36 
  37 #include <stdlib.h>
  38 #include <string.h>             /* strcmp() */
  39 
  40 #include "lib/global.h"
  41 #include "lib/widget.h"         /* Listbox */
  42 
  43 #include "edit-impl.h"
  44 #include "editwidget.h"
  45 
  46 /*** global variables ****************************************************************************/
  47 
  48 /*** file scope macro definitions ****************************************************************/
  49 
  50 #define MAX_ENTRY_LEN 40
  51 #define LIST_LINES 14
  52 #define N_DFLT_ENTRIES 2
  53 
  54 /*** file scope type declarations ****************************************************************/
  55 
  56 /*** file scope variables ************************************************************************/
  57 
  58 /*** file scope functions ************************************************************************/
  59 /* --------------------------------------------------------------------------------------------- */
  60 
  61 static int
  62 pstrcmp (const void *p1, const void *p2)
     /* [previous][next][first][last][top][bottom][index][help]  */
  63 {
  64     return strcmp (*(char *const *) p1, *(char *const *) p2);
  65 }
  66 
  67 /* --------------------------------------------------------------------------------------------- */
  68 
  69 static int
  70 exec_edit_syntax_dialog (const GPtrArray * names, const char *current_syntax)
     /* [previous][next][first][last][top][bottom][index][help]  */
  71 {
  72     size_t i;
  73     Listbox *syntaxlist;
  74 
  75     syntaxlist = create_listbox_window (LIST_LINES, MAX_ENTRY_LEN,
  76                                         _("Choose syntax highlighting"), NULL);
  77     LISTBOX_APPEND_TEXT (syntaxlist, 'A', _("< Auto >"), NULL, FALSE);
  78     LISTBOX_APPEND_TEXT (syntaxlist, 'R', _("< Reload Current Syntax >"), NULL, FALSE);
  79 
  80     for (i = 0; i < names->len; i++)
  81     {
  82         const char *name;
  83 
  84         name = g_ptr_array_index (names, i);
  85         LISTBOX_APPEND_TEXT (syntaxlist, 0, name, NULL, FALSE);
  86         if (current_syntax != NULL && strcmp (name, current_syntax) == 0)
  87             listbox_select_entry (syntaxlist->list, i + N_DFLT_ENTRIES);
  88     }
  89 
  90     return run_listbox (syntaxlist);
  91 }
  92 
  93 /* --------------------------------------------------------------------------------------------- */
  94 /*** public functions ****************************************************************************/
  95 /* --------------------------------------------------------------------------------------------- */
  96 
  97 void
  98 edit_syntax_dialog (WEdit * edit)
     /* [previous][next][first][last][top][bottom][index][help]  */
  99 {
 100     GPtrArray *names;
 101     int syntax;
 102 
 103     names = g_ptr_array_new ();
 104 
 105     /* We fill the list of syntax files every time the editor is invoked.
 106        Instead we could save the list to a file and update it once the syntax
 107        file gets updated (either by testing or by explicit user command). */
 108     edit_load_syntax (NULL, names, NULL);
 109     g_ptr_array_sort (names, pstrcmp);
 110 
 111     syntax = exec_edit_syntax_dialog (names, edit->syntax_type);
 112     if (syntax >= 0)
 113     {
 114         gboolean force_reload = FALSE;
 115         char *current_syntax;
 116         gboolean old_auto_syntax;
 117 
 118         current_syntax = g_strdup (edit->syntax_type);
 119         old_auto_syntax = option_auto_syntax;
 120 
 121         switch (syntax)
 122         {
 123         case 0:                /* auto syntax */
 124             option_auto_syntax = TRUE;
 125             break;
 126         case 1:                /* reload current syntax */
 127             force_reload = TRUE;
 128             break;
 129         default:
 130             option_auto_syntax = FALSE;
 131             g_free (edit->syntax_type);
 132             edit->syntax_type = g_strdup (g_ptr_array_index (names, syntax - N_DFLT_ENTRIES));
 133         }
 134 
 135         /* Load or unload syntax rules if the option has changed */
 136         if (force_reload || (option_auto_syntax && !old_auto_syntax) || old_auto_syntax ||
 137             (current_syntax != NULL && edit->syntax_type != NULL &&
 138              strcmp (current_syntax, edit->syntax_type) != 0))
 139             edit_load_syntax (edit, NULL, edit->syntax_type);
 140 
 141         g_free (current_syntax);
 142     }
 143 
 144     g_ptr_array_foreach (names, (GFunc) g_free, NULL);
 145     g_ptr_array_free (names, TRUE);
 146 }
 147 
 148 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */