Moved charsets.[ch] from src to lib directory
[midnight-commander.git] / src / find.c
blobc4a5e52c83acaa3debe99cbfe8bd41b51d8a4fc8
1 /* Find file command for the Midnight Commander
2 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007 Free Software Foundation, Inc.
4 Written 1995 by Miguel de Icaza
6 Complete rewrite.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 /** \file find.c
23 * \brief Source: Find file command
26 #include <config.h>
28 #include <ctype.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <sys/stat.h>
35 #include "lib/global.h"
37 #include "lib/util.h" /* canonicalize_pathname() */
38 #include "lib/tty/tty.h"
39 #include "lib/tty/key.h"
40 #include "lib/skin.h"
41 #include "lib/search.h"
42 #include "lib/mcconfig.h"
43 #include "lib/vfs/mc-vfs/vfs.h"
44 #include "lib/strutil.h"
45 #include "lib/util.h"
46 #include "lib/widget.h"
48 #include "setup.h" /* verbose */
49 #include "dir.h"
50 #include "panel.h" /* current_panel */
51 #include "main.h" /* do_cd, try_to_select */
52 #include "cmd.h" /* view_file_at_line */
53 #include "boxes.h"
54 #include "history.h" /* MC_HISTORY_SHARED_SEARCH */
55 #include "layout.h" /* mc_refresh() */
57 #include "find.h"
59 /*** global variables ****************************************************************************/
61 /* List of directories to be ignored, separated by ':' */
62 char **find_ignore_dirs = NULL;
64 /*** file scope macro definitions ****************************************************************/
66 /* Size of the find window */
67 #define FIND2_Y (LINES - 4)
69 #define FIND2_X_USE (FIND2_X - 20)
71 /*** file scope type declarations ****************************************************************/
73 /* A couple of extra messages we need */
74 enum
76 B_STOP = B_USER + 1,
77 B_AGAIN,
78 B_PANELIZE,
79 B_TREE,
80 B_VIEW
83 typedef enum
85 FIND_CONT = 0,
86 FIND_SUSPEND,
87 FIND_ABORT
88 } FindProgressStatus;
90 /* find file options */
91 typedef struct
93 /* file name options */
94 gboolean file_case_sens;
95 gboolean file_pattern;
96 gboolean find_recurs;
97 gboolean skip_hidden;
98 gboolean file_all_charsets;
100 /* file content options */
101 gboolean content_use;
102 gboolean content_case_sens;
103 gboolean content_regexp;
104 gboolean content_first_hit;
105 gboolean content_whole_words;
106 gboolean content_all_charsets;
107 } find_file_options_t;
109 /*** file scope variables ************************************************************************/
111 /* Size of the find parameters window */
112 #if HAVE_CHARSET
113 static int FIND_Y = 17;
114 #else
115 static int FIND_Y = 16;
116 #endif
117 static int FIND_X = 68;
119 static int FIND2_X = 64;
121 /* static variables to remember find parameters */
122 static WInput *in_start; /* Start path */
123 static WInput *in_name; /* Filename */
124 static WInput *in_with; /* Text */
125 static WLabel *content_label; /* 'Content:' label */
126 static WCheck *file_case_sens_cbox; /* "case sensitive" checkbox */
127 static WCheck *file_pattern_cbox; /* File name is glob or regexp */
128 static WCheck *recursively_cbox;
129 static WCheck *skip_hidden_cbox;
130 static WCheck *content_use_cbox; /* Take into account the Content field */
131 static WCheck *content_case_sens_cbox; /* "case sensitive" checkbox */
132 static WCheck *content_regexp_cbox; /* "find regular expression" checkbox */
133 static WCheck *content_first_hit_cbox; /* "First hit" checkbox" */
134 static WCheck *content_whole_words_cbox; /* "whole words" checkbox */
135 #ifdef HAVE_CHARSET
136 static WCheck *file_all_charsets_cbox;
137 static WCheck *content_all_charsets_cbox;
138 #endif
140 static gboolean running = FALSE; /* nice flag */
141 static char *find_pattern = NULL; /* Pattern to search */
142 static char *content_pattern = NULL; /* pattern to search inside files; if
143 content_regexp_flag is true, it contains the
144 regex pattern, else the search string. */
145 static unsigned long matches; /* Number of matches */
146 static gboolean is_start = FALSE; /* Status of the start/stop toggle button */
147 static char *old_dir = NULL;
149 /* Where did we stop */
150 static int resuming;
151 static int last_line;
152 static int last_pos;
154 static Dlg_head *find_dlg; /* The dialog */
155 static WButton *stop_button; /* pointer to the stop button */
156 static WLabel *status_label; /* Finished, Searching etc. */
157 static WLabel *found_num_label; /* Number of found items */
158 static WListbox *find_list; /* Listbox with the file list */
160 /* This keeps track of the directory stack */
161 #if GLIB_CHECK_VERSION (2, 14, 0)
162 static GQueue dir_queue = G_QUEUE_INIT;
163 #else
164 typedef struct dir_stack
166 char *name;
167 struct dir_stack *prev;
168 } dir_stack;
170 static dir_stack *dir_stack_base = 0;
171 #endif /* GLIB_CHECK_VERSION */
173 /* *INDENT-OFF* */
174 static struct
176 const char *text;
177 int len; /* length including space and brackets */
178 int x;
179 } fbuts[] =
181 {N_("&Suspend"), 11, 29},
182 {N_("Con&tinue"), 12, 29},
183 {N_("&Chdir"), 11, 3},
184 {N_("&Again"), 9, 17},
185 {N_("&Quit"), 8, 43},
186 {N_("Pane&lize"), 12, 3},
187 {N_("&View - F3"), 13, 20},
188 {N_("&Edit - F4"), 13, 38}
190 /* *INDENT-ON* */
192 static find_file_options_t options = {
193 TRUE, TRUE, TRUE, FALSE, FALSE,
194 FALSE, TRUE, FALSE, FALSE, FALSE, FALSE
197 static char *in_start_dir = INPUT_LAST_TEXT;
199 static mc_search_t *search_file_handle = NULL;
200 static mc_search_t *search_content_handle = NULL;
202 /*** file scope functions ************************************************************************/
204 static int
205 find_ignore_dirs_cmp (const void *d1, const void *d2)
207 return strcmp (*(const char **) d1, *(const char **) d2);
210 /* --------------------------------------------------------------------------------------------- */
212 static void
213 find_load_options (void)
215 static gboolean loaded = FALSE;
216 char *ignore_dirs;
218 if (loaded)
219 return;
221 loaded = TRUE;
223 /* Back compatibility: try load old parameter at first */
224 ignore_dirs = mc_config_get_string (mc_main_config, "Misc", "find_ignore_dirs", "");
225 if (ignore_dirs[0] != '\0')
227 find_ignore_dirs = g_strsplit (ignore_dirs, ":", -1);
228 mc_config_set_string (mc_main_config, "FindFile", "ignore_dirs", ignore_dirs);
230 g_free (ignore_dirs);
231 mc_config_del_param (mc_main_config, "Misc", "find_ignore_dirs");
233 /* Then load new parameters */
234 ignore_dirs = mc_config_get_string (mc_main_config, "FindFile", "ignore_dirs", "");
235 if (ignore_dirs[0] != '\0')
237 g_strfreev (find_ignore_dirs);
238 find_ignore_dirs = g_strsplit (ignore_dirs, ":", -1);
240 g_free (ignore_dirs);
242 if (find_ignore_dirs != NULL)
244 /* Values like '/foo::/bar: produce holes in list.
245 Find and remove them */
246 size_t r = 0, w = 0; /* read and write iterators */
248 for (; find_ignore_dirs[r] != NULL; r++)
250 if (find_ignore_dirs [r][0] == '\0')
252 /* empty entry -- skip it */
253 g_free (find_ignore_dirs [r]);
254 find_ignore_dirs [r] = NULL;
255 continue;
258 if (r != w)
260 /* copy entry to the previous free array cell */
261 find_ignore_dirs [w] = find_ignore_dirs [r];
262 find_ignore_dirs [r] = NULL;
265 canonicalize_pathname (find_ignore_dirs [w]);
266 w++;
269 /* sort array */
270 if (find_ignore_dirs[0] != NULL)
271 qsort (find_ignore_dirs, g_strv_length (find_ignore_dirs),
272 sizeof (find_ignore_dirs[0]), &find_ignore_dirs_cmp);
273 else
275 g_strfreev (find_ignore_dirs);
276 find_ignore_dirs = NULL;
280 options.file_case_sens =
281 mc_config_get_bool (mc_main_config, "FindFile", "file_case_sens", TRUE);
282 options.file_pattern =
283 mc_config_get_bool (mc_main_config, "FindFile", "file_shell_pattern", TRUE);
284 options.find_recurs = mc_config_get_bool (mc_main_config, "FindFile", "file_find_recurs", TRUE);
285 options.skip_hidden =
286 mc_config_get_bool (mc_main_config, "FindFile", "file_skip_hidden", FALSE);
287 options.file_all_charsets =
288 mc_config_get_bool (mc_main_config, "FindFile", "file_all_charsets", FALSE);
289 options.content_use = mc_config_get_bool (mc_main_config, "FindFile", "content_use", FALSE);
290 options.content_case_sens =
291 mc_config_get_bool (mc_main_config, "FindFile", "content_case_sens", TRUE);
292 options.content_regexp =
293 mc_config_get_bool (mc_main_config, "FindFile", "content_regexp", FALSE);
294 options.content_first_hit =
295 mc_config_get_bool (mc_main_config, "FindFile", "content_first_hit", FALSE);
296 options.content_whole_words =
297 mc_config_get_bool (mc_main_config, "FindFile", "content_whole_words", FALSE);
298 options.content_all_charsets =
299 mc_config_get_bool (mc_main_config, "FindFile", "content_all_charsets", FALSE);
302 /* --------------------------------------------------------------------------------------------- */
304 static void
305 find_save_options (void)
307 mc_config_set_bool (mc_main_config, "FindFile", "file_case_sens", options.file_case_sens);
308 mc_config_set_bool (mc_main_config, "FindFile", "file_shell_pattern", options.file_pattern);
309 mc_config_set_bool (mc_main_config, "FindFile", "file_find_recurs", options.find_recurs);
310 mc_config_set_bool (mc_main_config, "FindFile", "file_skip_hidden", options.skip_hidden);
311 mc_config_set_bool (mc_main_config, "FindFile", "file_all_charsets", options.file_all_charsets);
312 mc_config_set_bool (mc_main_config, "FindFile", "content_use", options.content_use);
313 mc_config_set_bool (mc_main_config, "FindFile", "content_case_sens", options.content_case_sens);
314 mc_config_set_bool (mc_main_config, "FindFile", "content_regexp", options.content_regexp);
315 mc_config_set_bool (mc_main_config, "FindFile", "content_first_hit", options.content_first_hit);
316 mc_config_set_bool (mc_main_config, "FindFile", "content_whole_words",
317 options.content_whole_words);
318 mc_config_set_bool (mc_main_config, "FindFile", "content_all_charsets",
319 options.content_all_charsets);
322 /* --------------------------------------------------------------------------------------------- */
324 static inline char *
325 add_to_list (const char *text, void *data)
327 return listbox_add_item (find_list, LISTBOX_APPEND_AT_END, 0, text, data);
330 /* --------------------------------------------------------------------------------------------- */
332 static inline void
333 stop_idle (void *data)
335 set_idle_proc (data, 0);
338 /* --------------------------------------------------------------------------------------------- */
340 static inline void
341 status_update (const char *text)
343 label_set_text (status_label, text);
346 /* --------------------------------------------------------------------------------------------- */
348 static void
349 found_num_update (void)
351 char buffer[BUF_TINY];
352 g_snprintf (buffer, sizeof (buffer), _("Found: %ld"), matches);
353 label_set_text (found_num_label, buffer);
356 /* --------------------------------------------------------------------------------------------- */
358 static void
359 get_list_info (char **file, char **dir)
361 listbox_get_current (find_list, file, (void **) dir);
364 /* --------------------------------------------------------------------------------------------- */
365 /** check regular expression */
367 static gboolean
368 find_check_regexp (const char *r)
370 mc_search_t *search;
371 gboolean regexp_ok = FALSE;
373 search = mc_search_new (r, -1);
375 if (search != NULL)
377 search->search_type = MC_SEARCH_T_REGEX;
378 regexp_ok = mc_search_prepare (search);
379 mc_search_free (search);
382 return regexp_ok;
385 /* --------------------------------------------------------------------------------------------- */
387 * Callback for the parameter dialog.
388 * Validate regex, prevent closing the dialog if it's invalid.
391 static cb_ret_t
392 find_parm_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
394 switch (msg)
396 case DLG_ACTION:
397 if (sender == (Widget *) content_use_cbox)
399 gboolean disable = !(content_use_cbox->state & C_BOOL);
401 widget_disable (content_label->widget, disable);
402 send_message ((Widget *) content_label, WIDGET_DRAW, 0);
403 widget_disable (in_with->widget, disable);
404 send_message ((Widget *) in_with, WIDGET_DRAW, 0);
405 widget_disable (content_first_hit_cbox->widget, disable);
406 send_message ((Widget *) content_first_hit_cbox, WIDGET_DRAW, 0);
407 widget_disable (content_regexp_cbox->widget, disable);
408 send_message ((Widget *) content_regexp_cbox, WIDGET_DRAW, 0);
409 widget_disable (content_case_sens_cbox->widget, disable);
410 send_message ((Widget *) content_case_sens_cbox, WIDGET_DRAW, 0);
411 #ifdef HAVE_CHARSET
412 widget_disable (content_all_charsets_cbox->widget, disable);
413 send_message ((Widget *) content_all_charsets_cbox, WIDGET_DRAW, 0);
414 #endif
415 widget_disable (content_whole_words_cbox->widget, disable);
416 send_message ((Widget *) content_whole_words_cbox, WIDGET_DRAW, 0);
418 return MSG_HANDLED;
421 return MSG_NOT_HANDLED;
424 case DLG_VALIDATE:
425 if (h->ret_value != B_ENTER)
426 return MSG_HANDLED;
428 /* check filename regexp */
429 if (!(file_pattern_cbox->state & C_BOOL)
430 && (in_name->buffer[0] != '\0') && !find_check_regexp (in_name->buffer))
432 h->state = DLG_ACTIVE; /* Don't stop the dialog */
433 message (D_ERROR, MSG_ERROR, _("Malformed regular expression"));
434 dlg_select_widget (in_name);
435 return MSG_HANDLED;
438 /* check content regexp */
439 if ((content_regexp_cbox->state & C_BOOL)
440 && (in_with->buffer[0] != '\0') && !find_check_regexp (in_with->buffer))
442 h->state = DLG_ACTIVE; /* Don't stop the dialog */
443 message (D_ERROR, MSG_ERROR, _("Malformed regular expression"));
444 dlg_select_widget (in_with);
445 return MSG_HANDLED;
448 return MSG_HANDLED;
450 default:
451 return default_dlg_callback (h, sender, msg, parm, data);
455 /* --------------------------------------------------------------------------------------------- */
457 * find_parameters: gets information from the user
459 * If the return value is TRUE, then the following holds:
461 * START_DIR and PATTERN are pointers to char * and upon return they
462 * contain the information provided by the user.
464 * CONTENT holds a strdup of the contents specified by the user if he
465 * asked for them or 0 if not (note, this is different from the
466 * behavior for the other two parameters.
470 static gboolean
471 find_parameters (char **start_dir, char **pattern, char **content)
473 gboolean return_value;
475 /* file name */
476 const char *file_case_label = N_("Cas&e sensitive");
477 const char *file_pattern_label = N_("&Using shell patterns");
478 const char *file_recurs_label = N_("&Find recursively");
479 const char *file_skip_hidden_label = N_("S&kip hidden");
480 #ifdef HAVE_CHARSET
481 const char *file_all_charsets_label = N_("&All charsets");
482 #endif
484 /* file content */
485 const char *content_use_label = N_("Sea&rch for content");
486 const char *content_case_label = N_("Case sens&itive");
487 const char *content_regexp_label = N_("Re&gular expression");
488 const char *content_first_hit_label = N_("Fir&st hit");
489 const char *content_whole_words_label = N_("&Whole words");
490 #ifdef HAVE_CHARSET
491 const char *content_all_charsets_label = N_("A&ll charsets");
492 #endif
494 const char *buts[] = { N_("&OK"), N_("&Cancel"), N_("&Tree") };
496 int b0, b1, b2;
498 int cbox_position;
499 gboolean disable;
501 #ifdef ENABLE_NLS
503 int i = sizeof (buts) / sizeof (buts[0]);
504 while (i-- != 0)
505 buts[i] = _(buts[i]);
507 file_case_label = _(file_case_label);
508 file_pattern_label = _(file_pattern_label);
509 file_recurs_label = _(file_recurs_label);
510 file_skip_hidden_label = _(file_skip_hidden_label);
511 #ifdef HAVE_CHARSET
512 file_all_charsets_label = _(file_all_charsets_label);
513 content_all_charsets_label = _(content_all_charsets_label);
514 #endif
515 content_use_label = _(content_use_label);
516 content_case_label = _(content_case_label);
517 content_regexp_label = _(content_regexp_label);
518 content_first_hit_label = _(content_first_hit_label);
519 content_whole_words_label = _(content_whole_words_label);
521 #endif /* ENABLE_NLS */
523 b0 = str_term_width1 (buts[0]) + 6; /* default button */
524 b1 = str_term_width1 (buts[1]) + 4;
525 b2 = str_term_width1 (buts[2]) + 4;
527 find_load_options ();
529 if (in_start_dir == NULL)
530 in_start_dir = g_strdup (".");
532 disable = !options.content_use;
534 find_dlg =
535 create_dlg (TRUE, 0, 0, FIND_Y, FIND_X, dialog_colors,
536 find_parm_callback, "[Find File]", _("Find File"), DLG_CENTER | DLG_REVERSE);
538 add_widget (find_dlg,
539 button_new (FIND_Y - 3, FIND_X * 3 / 4 - b1 / 2, B_CANCEL, NORMAL_BUTTON, buts[1],
540 0));
541 add_widget (find_dlg,
542 button_new (FIND_Y - 3, FIND_X / 4 - b0 / 2, B_ENTER, DEFPUSH_BUTTON, buts[0], 0));
544 cbox_position = FIND_Y - 5;
546 content_first_hit_cbox =
547 check_new (cbox_position--, FIND_X / 2 + 1, options.content_first_hit,
548 content_first_hit_label);
549 widget_disable (content_first_hit_cbox->widget, disable);
550 add_widget (find_dlg, content_first_hit_cbox);
552 content_whole_words_cbox =
553 check_new (cbox_position--, FIND_X / 2 + 1, options.content_whole_words,
554 content_whole_words_label);
555 widget_disable (content_whole_words_cbox->widget, disable);
556 add_widget (find_dlg, content_whole_words_cbox);
558 #ifdef HAVE_CHARSET
559 content_all_charsets_cbox = check_new (cbox_position--, FIND_X / 2 + 1,
560 options.content_all_charsets,
561 content_all_charsets_label);
562 widget_disable (content_all_charsets_cbox->widget, disable);
563 add_widget (find_dlg, content_all_charsets_cbox);
564 #endif
566 content_case_sens_cbox =
567 check_new (cbox_position--, FIND_X / 2 + 1, options.content_case_sens, content_case_label);
568 widget_disable (content_case_sens_cbox->widget, disable);
569 add_widget (find_dlg, content_case_sens_cbox);
571 content_regexp_cbox =
572 check_new (cbox_position--, FIND_X / 2 + 1, options.content_regexp, content_regexp_label);
573 widget_disable (content_regexp_cbox->widget, disable);
574 add_widget (find_dlg, content_regexp_cbox);
576 cbox_position = FIND_Y - 6;
578 skip_hidden_cbox = check_new (cbox_position--, 3, options.skip_hidden, file_skip_hidden_label);
579 add_widget (find_dlg, skip_hidden_cbox);
581 #ifdef HAVE_CHARSET
582 file_all_charsets_cbox =
583 check_new (cbox_position--, 3, options.file_all_charsets, file_all_charsets_label);
584 add_widget (find_dlg, file_all_charsets_cbox);
585 #endif
587 file_case_sens_cbox = check_new (cbox_position--, 3, options.file_case_sens, file_case_label);
588 add_widget (find_dlg, file_case_sens_cbox);
590 file_pattern_cbox = check_new (cbox_position--, 3, options.file_pattern, file_pattern_label);
591 add_widget (find_dlg, file_pattern_cbox);
593 recursively_cbox = check_new (cbox_position, 3, options.find_recurs, file_recurs_label);
594 add_widget (find_dlg, recursively_cbox);
596 /* This checkbox is located in the second column */
597 content_use_cbox =
598 check_new (cbox_position, FIND_X / 2 + 1, options.content_use, content_use_label);
599 add_widget (find_dlg, content_use_cbox);
601 in_with =
602 input_new (6, FIND_X / 2 + 1, input_get_default_colors (), FIND_X / 2 - 4, INPUT_LAST_TEXT,
603 MC_HISTORY_SHARED_SEARCH, INPUT_COMPLETE_DEFAULT);
604 widget_disable (in_with->widget, disable);
605 add_widget (find_dlg, in_with);
607 content_label = label_new (5, FIND_X / 2 + 1, _("Content:"));
608 widget_disable (content_label->widget, disable);
609 add_widget (find_dlg, content_label);
611 in_name = input_new (6, 3, input_get_default_colors (),
612 FIND_X / 2 - 4, INPUT_LAST_TEXT, "name", INPUT_COMPLETE_DEFAULT);
613 add_widget (find_dlg, in_name);
614 add_widget (find_dlg, label_new (5, 3, _("File name:")));
616 add_widget (find_dlg, button_new (3, FIND_X - b2 - 2, B_TREE, NORMAL_BUTTON, buts[2], 0));
618 in_start = input_new (3, 3, input_get_default_colors (),
619 FIND_X - b2 - 6, in_start_dir, "start", INPUT_COMPLETE_DEFAULT);
620 add_widget (find_dlg, in_start);
621 add_widget (find_dlg, label_new (2, 3, _("Start at:")));
623 find_par_start:
624 dlg_select_widget (in_name);
626 switch (run_dlg (find_dlg))
628 case B_CANCEL:
629 return_value = FALSE;
630 break;
632 case B_TREE:
634 const char *temp_dir = in_start->buffer;
636 if ((temp_dir[0] == '\0') || ((temp_dir[0] == '.') && (temp_dir[1] == '\0')))
637 temp_dir = current_panel->cwd;
639 if (in_start_dir != INPUT_LAST_TEXT)
640 g_free (in_start_dir);
641 in_start_dir = tree_box (temp_dir);
642 if (in_start_dir == NULL)
643 in_start_dir = g_strdup (temp_dir);
645 input_assign_text (in_start, in_start_dir);
647 /* Warning: Dreadful goto */
648 goto find_par_start;
651 default:
652 #ifdef HAVE_CHARSET
653 options.file_all_charsets = file_all_charsets_cbox->state & C_BOOL;
654 options.content_all_charsets = content_all_charsets_cbox->state & C_BOOL;
655 #endif
656 options.content_use = content_use_cbox->state & C_BOOL;
657 options.content_case_sens = content_case_sens_cbox->state & C_BOOL;
658 options.content_regexp = content_regexp_cbox->state & C_BOOL;
659 options.content_first_hit = content_first_hit_cbox->state & C_BOOL;
660 options.content_whole_words = content_whole_words_cbox->state & C_BOOL;
661 options.find_recurs = recursively_cbox->state & C_BOOL;
662 options.file_pattern = file_pattern_cbox->state & C_BOOL;
663 options.file_case_sens = file_case_sens_cbox->state & C_BOOL;
664 options.skip_hidden = skip_hidden_cbox->state & C_BOOL;
666 *content = (options.content_use && in_with->buffer[0] != '\0')
667 ? g_strdup (in_with->buffer) : NULL;
668 *start_dir = in_start->buffer[0] != '\0' ? in_start->buffer : (char *) ".";
669 *pattern = g_strdup (in_name->buffer);
670 if (in_start_dir != INPUT_LAST_TEXT)
671 g_free (in_start_dir);
672 in_start_dir = g_strdup (*start_dir);
673 if ((*start_dir)[0] == '.' && (*start_dir)[1] == '\0')
674 *start_dir = g_strdup (current_panel->cwd);
675 else if (g_path_is_absolute (*start_dir))
676 *start_dir = g_strdup (*start_dir);
677 else
678 *start_dir = g_build_filename (current_panel->cwd, *start_dir, (char *) NULL);
680 canonicalize_pathname (*start_dir);
682 find_save_options ();
684 return_value = TRUE;
687 destroy_dlg (find_dlg);
689 return return_value;
692 /* --------------------------------------------------------------------------------------------- */
694 #if GLIB_CHECK_VERSION (2, 14, 0)
695 static inline void
696 push_directory (const char *dir)
698 g_queue_push_head (&dir_queue, (void *) dir);
701 /* --------------------------------------------------------------------------------------------- */
703 static inline char *
704 pop_directory (void)
706 return (char *) g_queue_pop_tail (&dir_queue);
709 /* --------------------------------------------------------------------------------------------- */
710 /** Remove all the items from the stack */
712 static void
713 clear_stack (void)
715 g_queue_foreach (&dir_queue, (GFunc) g_free, NULL);
716 g_queue_clear (&dir_queue);
719 /* --------------------------------------------------------------------------------------------- */
721 #else /* GLIB_CHECK_VERSION */
722 static void
723 push_directory (const char *dir)
725 dir_stack *new;
727 new = g_new (dir_stack, 1);
728 new->name = (char *) dir;
729 new->prev = dir_stack_base;
730 dir_stack_base = new;
733 /* --------------------------------------------------------------------------------------------- */
735 static char *
736 pop_directory (void)
738 char *name = NULL;
740 if (dir_stack_base != NULL)
742 dir_stack *next;
743 name = dir_stack_base->name;
744 next = dir_stack_base->prev;
745 g_free (dir_stack_base);
746 dir_stack_base = next;
749 return name;
752 /* --------------------------------------------------------------------------------------------- */
753 /** Remove all the items from the stack */
755 static void
756 clear_stack (void)
758 char *dir = NULL;
759 while ((dir = pop_directory ()) != NULL)
760 g_free (dir);
762 #endif /* GLIB_CHECK_VERSION */
764 /* --------------------------------------------------------------------------------------------- */
766 static void
767 insert_file (const char *dir, const char *file)
769 char *tmp_name = NULL;
770 static char *dirname = NULL;
772 while (dir[0] == PATH_SEP && dir[1] == PATH_SEP)
773 dir++;
775 if (old_dir)
777 if (strcmp (old_dir, dir))
779 g_free (old_dir);
780 old_dir = g_strdup (dir);
781 dirname = add_to_list (dir, NULL);
784 else
786 old_dir = g_strdup (dir);
787 dirname = add_to_list (dir, NULL);
790 tmp_name = g_strdup_printf (" %s", file);
791 add_to_list (tmp_name, dirname);
792 g_free (tmp_name);
795 /* --------------------------------------------------------------------------------------------- */
797 static void
798 find_add_match (const char *dir, const char *file)
800 insert_file (dir, file);
802 /* Don't scroll */
803 if (matches == 0)
804 listbox_select_first (find_list);
805 send_message (&find_list->widget, WIDGET_DRAW, 0);
807 matches++;
808 found_num_update ();
811 /* --------------------------------------------------------------------------------------------- */
813 * get_line_at:
815 * Returns malloced null-terminated line from file file_fd.
816 * Input is buffered in buf_size long buffer.
817 * Current pos in buf is stored in pos.
818 * n_read - number of read chars.
819 * has_newline - is there newline ?
822 static char *
823 get_line_at (int file_fd, char *buf, int buf_size, int *pos, int *n_read, gboolean * has_newline)
825 char *buffer = NULL;
826 int buffer_size = 0;
827 char ch = 0;
828 int i = 0;
830 for (;;)
832 if (*pos >= *n_read)
834 *pos = 0;
835 *n_read = mc_read (file_fd, buf, buf_size);
836 if (*n_read <= 0)
837 break;
840 ch = buf[(*pos)++];
841 if (ch == '\0')
843 /* skip possible leading zero(s) */
844 if (i == 0)
845 continue;
846 break;
849 if (i >= buffer_size - 1)
850 buffer = g_realloc (buffer, buffer_size += 80);
852 /* Strip newline */
853 if (ch == '\n')
854 break;
856 buffer[i++] = ch;
859 *has_newline = (ch != '\0');
861 if (buffer != NULL)
862 buffer[i] = '\0';
864 return buffer;
867 /* --------------------------------------------------------------------------------------------- */
869 static FindProgressStatus
870 check_find_events (Dlg_head * h)
872 Gpm_Event event;
873 int c;
875 event.x = -1;
876 c = tty_get_event (&event, h->mouse_status == MOU_REPEAT, FALSE);
877 if (c != EV_NONE)
879 dlg_process_event (h, c, &event);
880 if (h->ret_value == B_ENTER
881 || h->ret_value == B_CANCEL || h->ret_value == B_AGAIN || h->ret_value == B_PANELIZE)
883 /* dialog terminated */
884 return FIND_ABORT;
886 if (!(h->flags & DLG_WANT_IDLE))
888 /* searching suspended */
889 return FIND_SUSPEND;
893 return FIND_CONT;
896 /* --------------------------------------------------------------------------------------------- */
898 * search_content:
900 * Search the content_pattern string in the DIRECTORY/FILE.
901 * It will add the found entries to the find listbox.
903 * returns FALSE if do_search should look for another file
904 * TRUE if do_search should exit and proceed to the event handler
907 static gboolean
908 search_content (Dlg_head * h, const char *directory, const char *filename)
910 struct stat s;
911 char buffer[BUF_4K];
912 char *fname = NULL;
913 int file_fd;
914 gboolean ret_val = FALSE;
916 fname = concat_dir_and_file (directory, filename);
918 if (mc_stat (fname, &s) != 0 || !S_ISREG (s.st_mode))
920 g_free (fname);
921 return 0;
924 file_fd = mc_open (fname, O_RDONLY);
925 g_free (fname);
927 if (file_fd == -1)
928 return 0;
930 g_snprintf (buffer, sizeof (buffer), _("Grepping in %s"), str_trunc (filename, FIND2_X_USE));
932 status_update (buffer);
933 mc_refresh ();
935 tty_enable_interrupt_key ();
936 tty_got_interrupt ();
939 int line = 1;
940 int pos = 0;
941 int n_read = 0;
942 gboolean has_newline;
943 char *p = NULL;
944 gboolean found = FALSE;
945 gsize found_len;
946 char result[BUF_MEDIUM];
948 if (resuming)
950 /* We've been previously suspended, start from the previous position */
951 resuming = 0;
952 line = last_line;
953 pos = last_pos;
955 while (!ret_val
956 && (p = get_line_at (file_fd, buffer, sizeof (buffer),
957 &pos, &n_read, &has_newline)) != NULL)
959 if (!found /* Search in binary line once */
960 && mc_search_run (search_content_handle,
961 (const void *) p, 0, strlen (p), &found_len))
963 g_snprintf (result, sizeof (result), "%d:%s", line, filename);
964 find_add_match (directory, result);
965 found = TRUE;
967 g_free (p);
969 if (found && options.content_first_hit)
970 break;
972 if (has_newline)
974 found = FALSE;
975 line++;
978 if ((line & 0xff) == 0)
980 FindProgressStatus res;
981 res = check_find_events (h);
982 switch (res)
984 case FIND_ABORT:
985 stop_idle (h);
986 ret_val = TRUE;
987 break;
988 case FIND_SUSPEND:
989 resuming = 1;
990 last_line = line;
991 last_pos = pos;
992 ret_val = TRUE;
993 break;
994 default:
995 break;
1000 tty_disable_interrupt_key ();
1001 mc_close (file_fd);
1002 return ret_val;
1005 /* --------------------------------------------------------------------------------------------- */
1007 static inline gboolean
1008 find_ignore_dir_search (const char *dir)
1010 if (find_ignore_dirs != NULL)
1012 const size_t dlen = strlen (dir);
1013 char **ignore_dir;
1015 for (ignore_dir = find_ignore_dirs; *ignore_dir != NULL; ignore_dir++)
1017 const size_t ilen = strlen (*ignore_dir);
1019 if (dlen < ilen)
1020 continue; /* ignore dir is too long -- skip it */
1022 if (strncmp (dir, *ignore_dir, ilen) != 0)
1023 continue; /* strings are different -- skip ignore_dir */
1025 /* be sure than ignore_dir is not a part of dir like:
1026 ignore_dir is "/h", dir is "/home" */
1027 if (dir[ilen] == PATH_SEP || dir[ilen] == '\0')
1028 return TRUE;
1032 return FALSE;
1035 /* --------------------------------------------------------------------------------------------- */
1037 static void
1038 find_rotate_dash (const Dlg_head *h, gboolean finish)
1040 static const char rotating_dash[] = "|/-\\";
1041 static unsigned int pos = 0;
1043 if (verbose)
1045 pos = (pos + 1) % 4;
1046 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
1047 dlg_move (h, FIND2_Y - 7, FIND2_X - 4);
1048 tty_print_char (finish ? ' ' : rotating_dash[pos]);
1049 mc_refresh ();
1053 /* --------------------------------------------------------------------------------------------- */
1055 static int
1056 do_search (Dlg_head *h)
1058 static struct dirent *dp = NULL;
1059 static DIR *dirp = NULL;
1060 static char *directory = NULL;
1061 struct stat tmp_stat;
1062 static int subdirs_left = 0;
1063 gsize bytes_found;
1064 unsigned short count;
1066 if (h == NULL)
1067 { /* someone forces me to close dirp */
1068 if (dirp != NULL)
1070 mc_closedir (dirp);
1071 dirp = NULL;
1073 g_free (directory);
1074 directory = NULL;
1075 dp = NULL;
1076 return 1;
1079 for (count = 0; count < 32; count++)
1081 while (dp == NULL)
1083 if (dirp != NULL)
1085 mc_closedir (dirp);
1086 dirp = NULL;
1089 while (dirp == NULL)
1091 char *tmp = NULL;
1093 tty_setcolor (REVERSE_COLOR);
1095 while (TRUE)
1097 tmp = pop_directory ();
1098 if (tmp == NULL)
1100 running = FALSE;
1101 status_update (_("Finished"));
1102 find_rotate_dash (h, TRUE);
1103 stop_idle (h);
1104 return 0;
1107 if (!find_ignore_dir_search (tmp))
1108 break;
1110 g_free (tmp);
1113 g_free (directory);
1114 directory = tmp;
1116 if (verbose)
1118 char buffer[BUF_SMALL];
1120 g_snprintf (buffer, sizeof (buffer), _("Searching %s"),
1121 str_trunc (directory, FIND2_X_USE));
1122 status_update (buffer);
1124 /* mc_stat should not be called after mc_opendir
1125 because vfs_s_opendir modifies the st_nlink
1127 if (mc_stat (directory, &tmp_stat) == 0)
1128 subdirs_left = tmp_stat.st_nlink - 2;
1129 else
1130 subdirs_left = 0;
1132 dirp = mc_opendir (directory);
1133 } /* while (!dirp) */
1135 /* skip invalid filenames */
1136 while ((dp = mc_readdir (dirp)) != NULL && !str_is_valid_string (dp->d_name))
1138 } /* while (!dp) */
1140 if (strcmp (dp->d_name, ".") == 0 || strcmp (dp->d_name, "..") == 0)
1142 /* skip invalid filenames */
1143 while ((dp = mc_readdir (dirp)) != NULL && !str_is_valid_string (dp->d_name))
1146 return 1;
1149 if (!(options.skip_hidden && (dp->d_name[0] == '.')))
1151 gboolean search_ok;
1153 if ((subdirs_left != 0) && options.find_recurs && (directory != NULL))
1154 { /* Can directory be NULL ? */
1155 char *tmp_name = concat_dir_and_file (directory, dp->d_name);
1156 if (mc_lstat (tmp_name, &tmp_stat) == 0 && S_ISDIR (tmp_stat.st_mode))
1158 push_directory (tmp_name);
1159 subdirs_left--;
1161 else
1162 g_free (tmp_name);
1165 search_ok = mc_search_run (search_file_handle, dp->d_name,
1166 0, strlen (dp->d_name), &bytes_found);
1168 if (search_ok)
1170 if (content_pattern == NULL)
1171 find_add_match (directory, dp->d_name);
1172 else if (search_content (h, directory, dp->d_name))
1173 return 1;
1177 /* skip invalid filenames */
1178 while ((dp = mc_readdir (dirp)) != NULL && !str_is_valid_string (dp->d_name))
1180 } /* for */
1182 find_rotate_dash (h, FALSE);
1184 return 1;
1187 /* --------------------------------------------------------------------------------------------- */
1189 static void
1190 init_find_vars (void)
1192 g_free (old_dir);
1193 old_dir = NULL;
1194 matches = 0;
1196 /* Remove all the items from the stack */
1197 clear_stack ();
1200 /* --------------------------------------------------------------------------------------------- */
1202 static char *
1203 make_fullname (const char *dirname, const char *filename)
1206 if (strcmp (dirname, ".") == 0 || strcmp (dirname, "." PATH_SEP_STR) == 0)
1207 return g_strdup (filename);
1208 if (strncmp (dirname, "." PATH_SEP_STR, 2) == 0)
1209 return concat_dir_and_file (dirname + 2, filename);
1210 return concat_dir_and_file (dirname, filename);
1213 /* --------------------------------------------------------------------------------------------- */
1215 static void
1216 find_do_view_edit (int unparsed_view, int edit, char *dir, char *file)
1218 char *fullname = NULL;
1219 const char *filename = NULL;
1220 int line;
1222 if (content_pattern != NULL)
1224 filename = strchr (file + 4, ':') + 1;
1225 line = atoi (file + 4);
1227 else
1229 filename = file + 4;
1230 line = 0;
1233 fullname = make_fullname (dir, filename);
1234 if (edit)
1235 do_edit_at_line (fullname, use_internal_edit, line);
1236 else
1237 view_file_at_line (fullname, unparsed_view, use_internal_view, line);
1238 g_free (fullname);
1241 /* --------------------------------------------------------------------------------------------- */
1243 static cb_ret_t
1244 view_edit_currently_selected_file (int unparsed_view, int edit)
1246 char *dir = NULL;
1247 char *text = NULL;
1249 listbox_get_current (find_list, &text, (void **) &dir);
1251 if ((text == NULL) || (dir == NULL))
1252 return MSG_NOT_HANDLED;
1254 find_do_view_edit (unparsed_view, edit, dir, text);
1255 return MSG_HANDLED;
1258 /* --------------------------------------------------------------------------------------------- */
1260 static cb_ret_t
1261 find_callback (Dlg_head *h, Widget * sender, dlg_msg_t msg, int parm, void *data)
1263 switch (msg)
1265 case DLG_KEY:
1266 if (parm == KEY_F (3) || parm == KEY_F (13))
1268 int unparsed_view = (parm == KEY_F (13));
1269 return view_edit_currently_selected_file (unparsed_view, 0);
1271 if (parm == KEY_F (4))
1273 return view_edit_currently_selected_file (0, 1);
1275 return MSG_NOT_HANDLED;
1277 case DLG_IDLE:
1278 do_search (h);
1279 return MSG_HANDLED;
1281 default:
1282 return default_dlg_callback (h, sender, msg, parm, data);
1286 /* --------------------------------------------------------------------------------------------- */
1287 /** Handles the Stop/Start button in the find window */
1289 static int
1290 start_stop (WButton * button, int action)
1292 (void) button;
1293 (void) action;
1295 running = is_start;
1296 set_idle_proc (find_dlg, running);
1297 is_start = !is_start;
1299 status_update (is_start ? _("Stopped") : _("Searching"));
1300 button_set_text (stop_button, fbuts[is_start ? 1 : 0].text);
1302 return 0;
1305 /* --------------------------------------------------------------------------------------------- */
1306 /** Handle view command, when invoked as a button */
1308 static int
1309 find_do_view_file (WButton * button, int action)
1311 (void) button;
1312 (void) action;
1314 view_edit_currently_selected_file (0, 0);
1315 return 0;
1318 /* --------------------------------------------------------------------------------------------- */
1319 /** Handle edit command, when invoked as a button */
1321 static int
1322 find_do_edit_file (WButton * button, int action)
1324 (void) button;
1325 (void) action;
1327 view_edit_currently_selected_file (0, 1);
1328 return 0;
1331 /* --------------------------------------------------------------------------------------------- */
1333 static void
1334 setup_gui (void)
1336 #ifdef ENABLE_NLS
1337 static gboolean i18n_flag = FALSE;
1339 if (!i18n_flag)
1341 int i = sizeof (fbuts) / sizeof (fbuts[0]);
1342 while (i-- != 0)
1344 fbuts[i].text = _(fbuts[i].text);
1345 fbuts[i].len = str_term_width1 (fbuts[i].text) + 3;
1348 fbuts[2].len += 2; /* DEFPUSH_BUTTON */
1349 i18n_flag = TRUE;
1351 #endif /* ENABLE_NLS */
1354 * Dynamically place buttons centered within current window size
1357 int l0 = max (fbuts[0].len, fbuts[1].len);
1358 int l1 = fbuts[2].len + fbuts[3].len + l0 + fbuts[4].len;
1359 int l2 = fbuts[5].len + fbuts[6].len + fbuts[7].len;
1360 int r1, r2;
1362 /* Check, if both button rows fit within FIND2_X */
1363 FIND2_X = max (l1 + 9, COLS - 16);
1364 FIND2_X = max (l2 + 8, FIND2_X);
1366 /* compute amount of space between buttons for each row */
1367 r1 = (FIND2_X - 4 - l1) % 5;
1368 l1 = (FIND2_X - 4 - l1) / 5;
1369 r2 = (FIND2_X - 4 - l2) % 4;
1370 l2 = (FIND2_X - 4 - l2) / 4;
1372 /* ...and finally, place buttons */
1373 fbuts[2].x = 2 + r1 / 2 + l1;
1374 fbuts[3].x = fbuts[2].x + fbuts[2].len + l1;
1375 fbuts[0].x = fbuts[3].x + fbuts[3].len + l1;
1376 fbuts[4].x = fbuts[0].x + l0 + l1;
1377 fbuts[5].x = 2 + r2 / 2 + l2;
1378 fbuts[6].x = fbuts[5].x + fbuts[5].len + l2;
1379 fbuts[7].x = fbuts[6].x + fbuts[6].len + l2;
1382 find_dlg =
1383 create_dlg (TRUE, 0, 0, FIND2_Y, FIND2_X, dialog_colors, find_callback,
1384 "[Find File]", _("Find File"), DLG_CENTER | DLG_REVERSE);
1386 add_widget (find_dlg,
1387 button_new (FIND2_Y - 3, fbuts[7].x, B_VIEW, NORMAL_BUTTON,
1388 fbuts[7].text, find_do_edit_file));
1389 add_widget (find_dlg,
1390 button_new (FIND2_Y - 3, fbuts[6].x, B_VIEW, NORMAL_BUTTON,
1391 fbuts[6].text, find_do_view_file));
1392 add_widget (find_dlg,
1393 button_new (FIND2_Y - 3, fbuts[5].x, B_PANELIZE, NORMAL_BUTTON, fbuts[5].text,
1394 NULL));
1396 add_widget (find_dlg,
1397 button_new (FIND2_Y - 4, fbuts[4].x, B_CANCEL, NORMAL_BUTTON, fbuts[4].text, NULL));
1398 stop_button =
1399 button_new (FIND2_Y - 4, fbuts[0].x, B_STOP, NORMAL_BUTTON, fbuts[0].text, start_stop);
1400 add_widget (find_dlg, stop_button);
1401 add_widget (find_dlg,
1402 button_new (FIND2_Y - 4, fbuts[3].x, B_AGAIN, NORMAL_BUTTON, fbuts[3].text, NULL));
1403 add_widget (find_dlg,
1404 button_new (FIND2_Y - 4, fbuts[2].x, B_ENTER, DEFPUSH_BUTTON, fbuts[2].text, NULL));
1406 status_label = label_new (FIND2_Y - 7, 4, _("Searching"));
1407 add_widget (find_dlg, status_label);
1409 found_num_label = label_new (FIND2_Y - 6, 4, "");
1410 add_widget (find_dlg, found_num_label);
1412 find_list = listbox_new (2, 2, FIND2_Y - 10, FIND2_X - 4, FALSE, NULL);
1413 add_widget (find_dlg, find_list);
1416 /* --------------------------------------------------------------------------------------------- */
1418 static int
1419 run_process (void)
1421 int ret;
1423 search_content_handle = mc_search_new (content_pattern, -1);
1424 if (search_content_handle)
1426 search_content_handle->search_type =
1427 options.content_regexp ? MC_SEARCH_T_REGEX : MC_SEARCH_T_NORMAL;
1428 search_content_handle->is_case_sensitive = options.content_case_sens;
1429 search_content_handle->whole_words = options.content_whole_words;
1430 search_content_handle->is_all_charsets = options.content_all_charsets;
1432 search_file_handle = mc_search_new (find_pattern, -1);
1433 search_file_handle->search_type = options.file_pattern ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
1434 search_file_handle->is_case_sensitive = options.file_case_sens;
1435 search_file_handle->is_all_charsets = options.file_all_charsets;
1436 search_file_handle->is_entire_line = options.file_pattern;
1438 resuming = 0;
1439 set_idle_proc (find_dlg, 1);
1440 ret = run_dlg (find_dlg);
1442 mc_search_free (search_file_handle);
1443 search_file_handle = NULL;
1444 mc_search_free (search_content_handle);
1445 search_content_handle = NULL;
1447 return ret;
1450 /* --------------------------------------------------------------------------------------------- */
1452 static void
1453 kill_gui (void)
1455 set_idle_proc (find_dlg, 0);
1456 destroy_dlg (find_dlg);
1459 /* --------------------------------------------------------------------------------------------- */
1461 static int
1462 find_file (const char *start_dir, const char *pattern, const char *content,
1463 char **dirname, char **filename)
1465 int return_value = 0;
1466 char *dir_tmp = NULL, *file_tmp = NULL;
1468 setup_gui ();
1470 /* FIXME: Need to cleanup this, this ought to be passed non-globaly */
1471 find_pattern = (char *) pattern;
1473 content_pattern = NULL;
1474 if (options.content_use && content != NULL && str_is_valid_string (content))
1475 content_pattern = g_strdup (content);
1477 init_find_vars ();
1478 push_directory (start_dir);
1480 return_value = run_process ();
1482 /* Remove all the items from the stack */
1483 clear_stack ();
1485 get_list_info (&file_tmp, &dir_tmp);
1487 if (dir_tmp)
1488 *dirname = g_strdup (dir_tmp);
1489 if (file_tmp)
1490 *filename = g_strdup (file_tmp);
1492 if (return_value == B_PANELIZE && *filename)
1494 int status, link_to_dir, stale_link;
1495 int next_free = 0;
1496 int i;
1497 struct stat st;
1498 GList *entry;
1499 dir_list *list = &current_panel->dir;
1500 char *name = NULL;
1502 for (i = 0, entry = find_list->list; entry != NULL; i++, entry = g_list_next (entry))
1504 const char *lc_filename = NULL;
1505 WLEntry *le = (WLEntry *) entry->data;
1507 if ((le->text == NULL) || (le->data == NULL))
1508 continue;
1510 if (content_pattern != NULL)
1511 lc_filename = strchr (le->text + 4, ':') + 1;
1512 else
1513 lc_filename = le->text + 4;
1515 name = make_fullname (le->data, lc_filename);
1516 status = handle_path (list, name, &st, next_free, &link_to_dir, &stale_link);
1517 if (status == 0)
1519 g_free (name);
1520 continue;
1522 if (status == -1)
1524 g_free (name);
1525 break;
1528 /* don't add files more than once to the panel */
1529 if (content_pattern != NULL && next_free > 0
1530 && strcmp (list->list[next_free - 1].fname, name) == 0)
1532 g_free (name);
1533 continue;
1536 if (!next_free) /* first turn i.e clean old list */
1537 panel_clean_dir (current_panel);
1538 list->list[next_free].fnamelen = strlen (name);
1539 list->list[next_free].fname = name;
1540 list->list[next_free].f.marked = 0;
1541 list->list[next_free].f.link_to_dir = link_to_dir;
1542 list->list[next_free].f.stale_link = stale_link;
1543 list->list[next_free].f.dir_size_computed = 0;
1544 list->list[next_free].st = st;
1545 list->list[next_free].sort_key = NULL;
1546 list->list[next_free].second_sort_key = NULL;
1547 next_free++;
1548 if (!(next_free & 15))
1549 rotate_dash ();
1552 if (next_free)
1554 current_panel->count = next_free;
1555 current_panel->is_panelized = 1;
1557 if (start_dir[0] == PATH_SEP)
1559 int ret;
1560 strcpy (current_panel->cwd, PATH_SEP_STR);
1561 ret = chdir (PATH_SEP_STR);
1566 g_free (content_pattern);
1567 kill_gui ();
1568 do_search (NULL); /* force do_search to release resources */
1569 g_free (old_dir);
1570 old_dir = NULL;
1572 return return_value;
1575 /* --------------------------------------------------------------------------------------------- */
1576 /*** public functions ****************************************************************************/
1577 /* --------------------------------------------------------------------------------------------- */
1579 void
1580 do_find (void)
1582 char *start_dir = NULL, *pattern = NULL, *content = NULL;
1583 char *filename = NULL, *dirname = NULL;
1584 int v;
1585 gboolean dir_and_file_set;
1587 while (find_parameters (&start_dir, &pattern, &content))
1589 if (pattern[0] == '\0')
1590 break; /* nothing search */
1592 dirname = filename = NULL;
1593 is_start = FALSE;
1594 v = find_file (start_dir, pattern, content, &dirname, &filename);
1595 g_free (pattern);
1597 if (v == B_ENTER)
1599 if (dirname != NULL)
1601 do_cd (dirname, cd_exact);
1602 if (filename != NULL)
1603 try_to_select (current_panel,
1604 filename + (content != NULL
1605 ? strchr (filename + 4, ':') - filename + 1 : 4));
1607 else if (filename != NULL)
1608 do_cd (filename, cd_exact);
1610 g_free (dirname);
1611 g_free (filename);
1612 break;
1615 g_free (content);
1616 dir_and_file_set = (dirname != NULL) && (filename != NULL);
1617 g_free (dirname);
1618 g_free (filename);
1620 if (v == B_CANCEL)
1621 break;
1623 if (v == B_PANELIZE)
1625 if (dir_and_file_set)
1627 try_to_select (current_panel, NULL);
1628 panel_re_sort (current_panel);
1629 try_to_select (current_panel, NULL);
1631 break;
1636 /* --------------------------------------------------------------------------------------------- */