root/src/diffviewer/search.c

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

DEFINITIONS

This source file includes following definitions.
  1. mcdiffview_dialog_search
  2. mcdiffview_do_search_backward
  3. mcdiffview_do_search_forward
  4. mcdiffview_do_search
  5. dview_search_cmd
  6. dview_continue_search_cmd

   1 /*
   2    Search functions for diffviewer.
   3 
   4    Copyright (C) 2010-2024
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Slava Zanko <slavazanko@gmail.com>, 2010.
   9    Andrew Borodin <aborodin@vmail.ru>, 2012-2022
  10 
  11    This file is part of the Midnight Commander.
  12 
  13    The Midnight Commander is free software: you can redistribute it
  14    and/or modify it under the terms of the GNU General Public License as
  15    published by the Free Software Foundation, either version 3 of the License,
  16    or (at your option) any later version.
  17 
  18    The Midnight Commander is distributed in the hope that it will be useful,
  19    but WITHOUT ANY WARRANTY; without even the implied warranty of
  20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21    GNU General Public License for more details.
  22 
  23    You should have received a copy of the GNU General Public License
  24    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  25  */
  26 
  27 #include <config.h>
  28 
  29 #include <stdio.h>
  30 
  31 #include "lib/global.h"
  32 #include "lib/strutil.h"
  33 #include "lib/tty/key.h"
  34 #include "lib/widget.h"
  35 #ifdef HAVE_CHARSET
  36 #include "lib/charsets.h"
  37 #endif
  38 
  39 #include "src/history.h"
  40 
  41 #include "internal.h"
  42 
  43 /*** global variables ****************************************************************************/
  44 
  45 /*** file scope macro definitions ****************************************************************/
  46 
  47 /*** file scope type declarations ****************************************************************/
  48 
  49 typedef struct mcdiffview_search_options_struct
  50 {
  51     mc_search_type_t type;
  52     gboolean case_sens;
  53     gboolean backwards;
  54     gboolean whole_words;
  55     gboolean all_codepages;
  56 } mcdiffview_search_options_t;
  57 
  58 /*** forward declarations (file scope functions) *************************************************/
  59 
  60 /*** file scope variables ************************************************************************/
  61 
  62 static mcdiffview_search_options_t mcdiffview_search_options = {
  63     .type = MC_SEARCH_T_NORMAL,
  64     .case_sens = FALSE,
  65     .backwards = FALSE,
  66     .whole_words = FALSE,
  67     .all_codepages = FALSE,
  68 };
  69 
  70 /* --------------------------------------------------------------------------------------------- */
  71 /*** file scope functions ************************************************************************/
  72 /* --------------------------------------------------------------------------------------------- */
  73 
  74 static gboolean
  75 mcdiffview_dialog_search (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
  76 {
  77     char *exp = NULL;
  78     int qd_result;
  79     size_t num_of_types = 0;
  80     gchar **list_of_types;
  81 
  82     list_of_types = mc_search_get_types_strings_array (&num_of_types);
  83 
  84     {
  85         quick_widget_t quick_widgets[] = {
  86             /* *INDENT-OFF* */
  87             QUICK_LABELED_INPUT (N_("Enter search string:"), input_label_above, INPUT_LAST_TEXT,
  88                                  MC_HISTORY_SHARED_SEARCH, &exp, NULL, FALSE, FALSE,
  89                                  INPUT_COMPLETE_NONE),
  90             QUICK_SEPARATOR (TRUE),
  91             QUICK_START_COLUMNS,
  92                 QUICK_RADIO (num_of_types, (const char **) list_of_types,
  93                              (int *) &mcdiffview_search_options.type, NULL),
  94             QUICK_NEXT_COLUMN,
  95                 QUICK_CHECKBOX (N_("Cas&e sensitive"), &mcdiffview_search_options.case_sens, NULL),
  96                 QUICK_CHECKBOX (N_("&Backwards"), &mcdiffview_search_options.backwards, NULL),
  97                 QUICK_CHECKBOX (N_("&Whole words"), &mcdiffview_search_options.whole_words, NULL),
  98 #ifdef HAVE_CHARSET
  99                 QUICK_CHECKBOX (N_("&All charsets"), &mcdiffview_search_options.all_codepages, NULL),
 100 #endif
 101             QUICK_STOP_COLUMNS,
 102             QUICK_BUTTONS_OK_CANCEL,
 103             QUICK_END
 104             /* *INDENT-ON* */
 105         };
 106 
 107         WRect r = { -1, -1, 0, 58 };
 108 
 109         quick_dialog_t qdlg = {
 110             r, N_("Search"), "[Input Line Keys]",
 111             quick_widgets, NULL, NULL
 112         };
 113 
 114         qd_result = quick_dialog (&qdlg);
 115     }
 116 
 117     g_strfreev (list_of_types);
 118 
 119     if (qd_result == B_CANCEL || exp[0] == '\0')
 120     {
 121         g_free (exp);
 122         return FALSE;
 123     }
 124 
 125 #ifdef HAVE_CHARSET
 126     {
 127         GString *tmp;
 128 
 129         tmp = str_convert_to_input (exp);
 130         g_free (exp);
 131         if (tmp != NULL)
 132             exp = g_string_free (tmp, FALSE);
 133         else
 134             exp = g_strdup ("");
 135     }
 136 #endif
 137 
 138     g_free (dview->search.last_string);
 139     dview->search.last_string = exp;
 140 
 141     return TRUE;
 142 }
 143 
 144 /* --------------------------------------------------------------------------------------------- */
 145 
 146 static gboolean
 147 mcdiffview_do_search_backward (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
 148 {
 149     ssize_t ind;
 150 
 151     if (dview->search.last_accessed_num_line < 0)
 152     {
 153         dview->search.last_accessed_num_line = -1;
 154         return FALSE;
 155     }
 156 
 157     if ((size_t) dview->search.last_accessed_num_line >= dview->a[dview->ord]->len)
 158         dview->search.last_accessed_num_line = (ssize_t) dview->a[dview->ord]->len;
 159 
 160     for (ind = --dview->search.last_accessed_num_line; ind >= 0; ind--)
 161     {
 162         DIFFLN *p;
 163 
 164         p = (DIFFLN *) & g_array_index (dview->a[dview->ord], DIFFLN, (size_t) ind);
 165         if (p->u.len == 0)
 166             continue;
 167 
 168         if (mc_search_run (dview->search.handle, p->p, 0, p->u.len, NULL))
 169         {
 170             dview->skip_rows = dview->search.last_found_line =
 171                 dview->search.last_accessed_num_line = ind;
 172             return TRUE;
 173         }
 174     }
 175     return FALSE;
 176 }
 177 
 178 /* --------------------------------------------------------------------------------------------- */
 179 
 180 
 181 static gboolean
 182 mcdiffview_do_search_forward (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
 183 {
 184     size_t ind;
 185 
 186     if (dview->search.last_accessed_num_line < 0)
 187         dview->search.last_accessed_num_line = -1;
 188     else if ((size_t) dview->search.last_accessed_num_line >= dview->a[dview->ord]->len)
 189     {
 190         dview->search.last_accessed_num_line = (ssize_t) dview->a[dview->ord]->len;
 191         return FALSE;
 192     }
 193 
 194     for (ind = (size_t) ++dview->search.last_accessed_num_line; ind < dview->a[dview->ord]->len;
 195          ind++)
 196     {
 197         DIFFLN *p;
 198 
 199         p = (DIFFLN *) & g_array_index (dview->a[dview->ord], DIFFLN, ind);
 200         if (p->u.len == 0)
 201             continue;
 202 
 203         if (mc_search_run (dview->search.handle, p->p, 0, p->u.len, NULL))
 204         {
 205             dview->skip_rows = dview->search.last_found_line =
 206                 dview->search.last_accessed_num_line = (ssize_t) ind;
 207             return TRUE;
 208         }
 209     }
 210     return FALSE;
 211 }
 212 
 213 /* --------------------------------------------------------------------------------------------- */
 214 
 215 static void
 216 mcdiffview_do_search (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
 217 {
 218     gboolean present_result = FALSE;
 219 
 220     tty_enable_interrupt_key ();
 221 
 222     if (mcdiffview_search_options.backwards)
 223     {
 224         present_result = mcdiffview_do_search_backward (dview);
 225     }
 226     else
 227     {
 228         present_result = mcdiffview_do_search_forward (dview);
 229     }
 230 
 231     tty_disable_interrupt_key ();
 232 
 233     if (!present_result)
 234     {
 235         dview->search.last_found_line = -1;
 236         query_dialog (_("Search"), _(STR_E_NOTFOUND), D_NORMAL, 1, _("&Dismiss"));
 237     }
 238 }
 239 
 240 /* --------------------------------------------------------------------------------------------- */
 241 /*** public functions ****************************************************************************/
 242 /* --------------------------------------------------------------------------------------------- */
 243 
 244 void
 245 dview_search_cmd (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
 246 {
 247     if (dview->dsrc != DATA_SRC_MEM)
 248     {
 249         error_dialog (_("Search"), _("Search is disabled"));
 250         return;
 251     }
 252 
 253     if (!mcdiffview_dialog_search (dview))
 254         return;
 255 
 256     mc_search_free (dview->search.handle);
 257 #ifdef HAVE_CHARSET
 258     dview->search.handle = mc_search_new (dview->search.last_string, cp_source);
 259 #else
 260     dview->search.handle = mc_search_new (dview->search.last_string, NULL);
 261 #endif
 262 
 263     if (dview->search.handle == NULL)
 264         return;
 265 
 266     dview->search.handle->search_type = mcdiffview_search_options.type;
 267 #ifdef HAVE_CHARSET
 268     dview->search.handle->is_all_charsets = mcdiffview_search_options.all_codepages;
 269 #endif
 270     dview->search.handle->is_case_sensitive = mcdiffview_search_options.case_sens;
 271     dview->search.handle->whole_words = mcdiffview_search_options.whole_words;
 272 
 273     mcdiffview_do_search (dview);
 274 }
 275 
 276 /* --------------------------------------------------------------------------------------------- */
 277 
 278 void
 279 dview_continue_search_cmd (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
 280 {
 281     if (dview->dsrc != DATA_SRC_MEM)
 282         error_dialog (_("Search"), _("Search is disabled"));
 283     else if (dview->search.handle == NULL)
 284         dview_search_cmd (dview);
 285     else
 286         mcdiffview_do_search (dview);
 287 }
 288 
 289 /* --------------------------------------------------------------------------------------------- */

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