Ticket #2161 (del - clear input field)
[pantumic.git] / src / find.c
blob58f9a7e99f6d410095e7a34a506236231741ebba
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 rewrote.
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/tty/tty.h"
38 #include "lib/tty/key.h"
39 #include "lib/skin.h"
40 #include "lib/search.h"
41 #include "lib/mcconfig.h"
42 #include "lib/vfs/mc-vfs/vfs.h"
43 #include "lib/strutil.h"
45 #include "setup.h" /* verbose */
46 #include "dialog.h"
47 #include "widget.h"
48 #include "dir.h"
49 #include "panel.h" /* current_panel */
50 #include "main.h" /* do_cd, try_to_select */
51 #include "wtools.h"
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 /* Size of the find parameters window */
60 #if HAVE_CHARSET
61 static int FIND_Y = 16;
62 #else
63 static int FIND_Y = 15;
64 #endif
65 static int FIND_X = 68;
67 /* Size of the find window */
68 #define FIND2_Y (LINES - 4)
70 static int FIND2_X = 64;
71 #define FIND2_X_USE (FIND2_X - 20)
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 /* List of directories to be ignored, separated by ':' */
91 char *find_ignore_dirs = NULL;
93 /* static variables to remember find parameters */
94 static WInput *in_start; /* Start path */
95 static WInput *in_name; /* Filename */
96 static WInput *in_with; /* Text inside filename */
97 static WCheck *file_case_sens_cbox; /* "case sensitive" checkbox */
98 static WCheck *file_pattern_cbox; /* File name is glob or regexp */
99 static WCheck *recursively_cbox;
100 static WCheck *skip_hidden_cbox;
101 static WCheck *content_case_sens_cbox; /* "case sensitive" checkbox */
102 static WCheck *content_regexp_cbox; /* "find regular expression" checkbox */
103 static WCheck *content_first_hit_cbox; /* "First hit" checkbox" */
104 static WCheck *content_whole_words_cbox; /* "whole words" checkbox */
105 #ifdef HAVE_CHARSET
106 static WCheck *file_all_charsets_cbox;
107 static WCheck *content_all_charsets_cbox;
108 #endif
110 static gboolean running = FALSE; /* nice flag */
111 static char *find_pattern = NULL; /* Pattern to search */
112 static char *content_pattern = NULL; /* pattern to search inside files; if
113 content_regexp_flag is true, it contains the
114 regex pattern, else the search string. */
115 static unsigned long matches; /* Number of matches */
116 static gboolean is_start = FALSE; /* Status of the start/stop toggle button */
117 static char *old_dir = NULL;
119 /* Where did we stop */
120 static int resuming;
121 static int last_line;
122 static int last_pos;
124 static Dlg_head *find_dlg; /* The dialog */
125 static WButton *stop_button; /* pointer to the stop button */
126 static WLabel *status_label; /* Finished, Searching etc. */
127 static WLabel *found_num_label; /* Number of found items */
128 static WListbox *find_list; /* Listbox with the file list */
130 /* This keeps track of the directory stack */
131 #if GLIB_CHECK_VERSION (2, 14, 0)
132 static GQueue dir_queue = G_QUEUE_INIT;
133 #else
134 typedef struct dir_stack
136 char *name;
137 struct dir_stack *prev;
138 } dir_stack;
140 static dir_stack *dir_stack_base = 0;
141 #endif /* GLIB_CHECK_VERSION */
143 /* *INDENT-OFF* */
144 static struct
146 const char *text;
147 int len; /* length including space and brackets */
148 int x;
149 } fbuts[] =
151 {N_("&Suspend"), 11, 29},
152 {N_("Con&tinue"), 12, 29},
153 {N_("&Chdir"), 11, 3},
154 {N_("&Again"), 9, 17},
155 {N_("&Quit"), 8, 43},
156 {N_("Pane&lize"), 12, 3},
157 {N_("&View - F3"), 13, 20},
158 {N_("&Edit - F4"), 13, 38}
160 /* *INDENT-ON* */
162 /* find file options */
163 typedef struct
165 /* file name options */
166 gboolean file_case_sens;
167 gboolean file_pattern;
168 gboolean find_recurs;
169 gboolean skip_hidden;
170 gboolean file_all_charsets;
172 /* file content options */
173 gboolean content_case_sens;
174 gboolean content_regexp;
175 gboolean content_first_hit;
176 gboolean content_whole_words;
177 gboolean content_all_charsets;
178 } find_file_options_t;
180 static find_file_options_t options = {
181 TRUE, TRUE, TRUE, FALSE, FALSE,
182 TRUE, FALSE, FALSE, FALSE, FALSE
185 static char *in_start_dir = INPUT_LAST_TEXT;
187 static mc_search_t *search_file_handle = NULL;
188 static mc_search_t *search_content_handle = NULL;
190 static void
191 find_load_options (void)
193 static gboolean loaded = FALSE;
194 char *ignore_dirs;
196 if (loaded)
197 return;
199 loaded = TRUE;
201 /* Back compatibility: try load old parameter at first */
202 ignore_dirs = mc_config_get_string (mc_main_config, "Misc", "find_ignore_dirs", "");
203 if (ignore_dirs[0] != '\0')
205 find_ignore_dirs = g_strconcat (":", ignore_dirs, ":", (char *) NULL);
206 mc_config_set_string (mc_main_config, "FindFile", "ignore_dirs", ignore_dirs);
208 g_free (ignore_dirs);
209 mc_config_del_param (mc_main_config, "Misc", "find_ignore_dirs");
211 /* Then load new parameters */
212 ignore_dirs = mc_config_get_string (mc_main_config, "FindFile", "ignore_dirs", "");
213 if (ignore_dirs[0] != '\0')
215 g_free (find_ignore_dirs);
216 find_ignore_dirs = g_strconcat (":", ignore_dirs, ":", (char *) NULL);
218 g_free (ignore_dirs);
220 options.file_case_sens =
221 mc_config_get_bool (mc_main_config, "FindFile", "file_case_sens", TRUE);
222 options.file_pattern =
223 mc_config_get_bool (mc_main_config, "FindFile", "file_shell_pattern", TRUE);
224 options.find_recurs = mc_config_get_bool (mc_main_config, "FindFile", "file_find_recurs", TRUE);
225 options.skip_hidden =
226 mc_config_get_bool (mc_main_config, "FindFile", "file_skip_hidden", FALSE);
227 options.file_all_charsets =
228 mc_config_get_bool (mc_main_config, "FindFile", "file_all_charsets", FALSE);
229 options.content_case_sens =
230 mc_config_get_bool (mc_main_config, "FindFile", "content_case_sens", TRUE);
231 options.content_regexp =
232 mc_config_get_bool (mc_main_config, "FindFile", "content_regexp", FALSE);
233 options.content_first_hit =
234 mc_config_get_bool (mc_main_config, "FindFile", "content_first_hit", FALSE);
235 options.content_whole_words =
236 mc_config_get_bool (mc_main_config, "FindFile", "content_whole_words", FALSE);
237 options.content_all_charsets =
238 mc_config_get_bool (mc_main_config, "FindFile", "content_all_charsets", FALSE);
241 static void
242 find_save_options (void)
244 mc_config_set_bool (mc_main_config, "FindFile", "file_case_sens", options.file_case_sens);
245 mc_config_set_bool (mc_main_config, "FindFile", "file_shell_pattern", options.file_pattern);
246 mc_config_set_bool (mc_main_config, "FindFile", "file_find_recurs", options.find_recurs);
247 mc_config_set_bool (mc_main_config, "FindFile", "file_skip_hidden", options.skip_hidden);
248 mc_config_set_bool (mc_main_config, "FindFile", "file_all_charsets", options.file_all_charsets);
249 mc_config_set_bool (mc_main_config, "FindFile", "content_case_sens", options.content_case_sens);
250 mc_config_set_bool (mc_main_config, "FindFile", "content_regexp", options.content_regexp);
251 mc_config_set_bool (mc_main_config, "FindFile", "content_first_hit", options.content_first_hit);
252 mc_config_set_bool (mc_main_config, "FindFile", "content_whole_words",
253 options.content_whole_words);
254 mc_config_set_bool (mc_main_config, "FindFile", "content_all_charsets",
255 options.content_all_charsets);
258 static inline char *
259 add_to_list (const char *text, void *data)
261 return listbox_add_item (find_list, LISTBOX_APPEND_AT_END, 0, text, data);
264 static inline void
265 stop_idle (void *data)
267 set_idle_proc (data, 0);
270 static inline void
271 status_update (const char *text)
273 label_set_text (status_label, text);
276 static void
277 found_num_update (void)
279 char buffer[BUF_TINY];
280 g_snprintf (buffer, sizeof (buffer), _("Found: %ld"), matches);
281 label_set_text (found_num_label, buffer);
284 static void
285 get_list_info (char **file, char **dir)
287 listbox_get_current (find_list, file, (void **) dir);
290 /* check regular expression */
291 static gboolean
292 find_check_regexp (const char *r)
294 mc_search_t *search;
295 gboolean regexp_ok = FALSE;
297 search = mc_search_new (r, -1);
299 if (search != NULL)
301 search->search_type = MC_SEARCH_T_REGEX;
302 regexp_ok = mc_search_prepare (search);
303 mc_search_free (search);
306 return regexp_ok;
310 * Callback for the parameter dialog.
311 * Validate regex, prevent closing the dialog if it's invalid.
313 static cb_ret_t
314 find_parm_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
316 switch (msg)
318 case DLG_VALIDATE:
319 if (h->ret_value != B_ENTER)
320 return MSG_HANDLED;
322 /* check filename regexp */
323 if (!(file_pattern_cbox->state & C_BOOL)
324 && (in_name->buffer[0] != '\0') && !find_check_regexp (in_name->buffer))
326 message (D_ERROR, MSG_ERROR, _(" Malformed regular expression "));
327 dlg_select_widget (in_name);
328 h->running = 1; /* Don't stop the dialog */
329 return MSG_HANDLED;
332 /* check content regexp */
333 if ((content_regexp_cbox->state & C_BOOL)
334 && (in_with->buffer[0] != '\0') && !find_check_regexp (in_with->buffer))
336 message (D_ERROR, MSG_ERROR, _(" Malformed regular expression "));
337 dlg_select_widget (in_with);
338 h->running = 1; /* Don't stop the dialog */
339 return MSG_HANDLED;
342 return MSG_HANDLED;
344 default:
345 return default_dlg_callback (h, sender, msg, parm, data);
350 * find_parameters: gets information from the user
352 * If the return value is TRUE, then the following holds:
354 * START_DIR and PATTERN are pointers to char * and upon return they
355 * contain the information provided by the user.
357 * CONTENT holds a strdup of the contents specified by the user if he
358 * asked for them or 0 if not (note, this is different from the
359 * behavior for the other two parameters.
362 static gboolean
363 find_parameters (char **start_dir, char **pattern, char **content)
365 gboolean return_value;
367 /* file name */
368 const char *file_case_label = N_("Cas&e sensitive");
369 const char *file_pattern_label = N_("&Using shell patterns");
370 const char *file_recurs_label = N_("&Find recursively");
371 const char *file_skip_hidden_label = N_("S&kip hidden");
372 #ifdef HAVE_CHARSET
373 const char *file_all_charsets_label = N_("&All charsets");
374 #endif
376 /* file content */
377 const char *content_case_label = N_("Case sens&itive");
378 const char *content_regexp_label = N_("Re&gular expression");
379 const char *content_first_hit_label = N_("Fir&st hit");
380 const char *content_whole_words_label = N_("&Whole words");
381 #ifdef HAVE_CHARSET
382 const char *content_all_charsets_label = N_("All cha&rsets");
383 #endif
385 const char *buts[] = { N_("&OK"), N_("&Cancel"), N_("&Tree") };
387 const int input_colors[3] =
389 INPUT_COLOR,
390 INPUT_INACTIVE_COLOR,
391 INPUT_MARK_COLOR
394 int b0, b1, b2;
396 #ifdef ENABLE_NLS
398 int i = sizeof (buts) / sizeof (buts[0]);
399 while (i-- != 0)
400 buts[i] = _(buts[i]);
402 file_case_label = _(file_case_label);
403 file_pattern_label = _(file_pattern_label);
404 file_recurs_label = _(file_recurs_label);
405 file_skip_hidden_label = _(file_skip_hidden_label);
406 #ifdef HAVE_CHARSET
407 file_all_charsets_label = _(file_all_charsets_label);
408 content_all_charsets_label = _(content_all_charsets_label);
409 #endif
410 content_case_label = _(content_case_label);
411 content_regexp_label = _(content_regexp_label);
412 content_first_hit_label = _(content_first_hit_label);
413 content_whole_words_label = _(content_whole_words_label);
415 #endif /* ENABLE_NLS */
417 b0 = str_term_width1 (buts[0]) + 6; /* default button */
418 b1 = str_term_width1 (buts[1]) + 4;
419 b2 = str_term_width1 (buts[2]) + 4;
421 find_load_options ();
423 if (in_start_dir == NULL)
424 in_start_dir = g_strdup (".");
426 find_par_start:
427 find_dlg =
428 create_dlg (0, 0, FIND_Y, FIND_X, dialog_colors,
429 find_parm_callback, "[Find File]", _("Find File"), DLG_CENTER | DLG_REVERSE);
431 add_widget (find_dlg,
432 button_new (FIND_Y - 3, FIND_X * 3 / 4 - b1 / 2, B_CANCEL, NORMAL_BUTTON, buts[1],
433 0));
434 add_widget (find_dlg,
435 button_new (FIND_Y - 3, FIND_X / 4 - b0 / 2, B_ENTER, DEFPUSH_BUTTON, buts[0], 0));
437 #ifdef HAVE_CHARSET
438 content_all_charsets_cbox = check_new (11, FIND_X / 2 + 1,
439 options.content_all_charsets,
440 content_all_charsets_label);
441 add_widget (find_dlg, content_all_charsets_cbox);
442 #endif
444 content_whole_words_cbox =
445 check_new (10, FIND_X / 2 + 1, options.content_whole_words, content_whole_words_label);
446 add_widget (find_dlg, content_whole_words_cbox);
448 content_first_hit_cbox =
449 check_new (9, FIND_X / 2 + 1, options.content_first_hit, content_first_hit_label);
450 add_widget (find_dlg, content_first_hit_cbox);
452 content_regexp_cbox =
453 check_new (8, FIND_X / 2 + 1, options.content_regexp, content_regexp_label);
454 add_widget (find_dlg, content_regexp_cbox);
456 content_case_sens_cbox =
457 check_new (7, FIND_X / 2 + 1, options.content_case_sens, content_case_label);
458 add_widget (find_dlg, content_case_sens_cbox);
460 #ifdef HAVE_CHARSET
461 file_all_charsets_cbox = check_new (11, 3, options.file_all_charsets, file_all_charsets_label);
462 add_widget (find_dlg, file_all_charsets_cbox);
463 #endif
465 skip_hidden_cbox = check_new (10, 3, options.skip_hidden, file_skip_hidden_label);
466 add_widget (find_dlg, skip_hidden_cbox);
468 recursively_cbox = check_new (9, 3, options.find_recurs, file_recurs_label);
469 add_widget (find_dlg, recursively_cbox);
471 file_pattern_cbox = check_new (8, 3, options.file_pattern, file_pattern_label);
472 add_widget (find_dlg, file_pattern_cbox);
474 file_case_sens_cbox = check_new (7, 3, options.file_case_sens, file_case_label);
475 add_widget (find_dlg, file_case_sens_cbox);
477 in_with = input_new (6, FIND_X / 2 + 1, (int *) input_colors, FIND_X / 2 - 4, INPUT_LAST_TEXT,
478 MC_HISTORY_SHARED_SEARCH, INPUT_COMPLETE_DEFAULT);
479 add_widget (find_dlg, in_with);
480 add_widget (find_dlg, label_new (5, FIND_X / 2 + 1, _("Content:")));
482 in_name = input_new (6, 3, (int *) input_colors,
483 FIND_X / 2 - 4, INPUT_LAST_TEXT, "name", INPUT_COMPLETE_DEFAULT);
484 add_widget (find_dlg, in_name);
485 add_widget (find_dlg, label_new (5, 3, _("File name:")));
487 add_widget (find_dlg, button_new (3, FIND_X - b2 - 2, B_TREE, NORMAL_BUTTON, buts[2], 0));
489 in_start = input_new (3, 3, (int *) input_colors,
490 FIND_X - b2 - 6, in_start_dir, "start", INPUT_COMPLETE_DEFAULT);
491 add_widget (find_dlg, in_start);
492 add_widget (find_dlg, label_new (2, 3, _("Start at:")));
494 dlg_select_widget (in_name);
496 switch (run_dlg (find_dlg))
498 case B_CANCEL:
499 return_value = FALSE;
500 break;
502 case B_TREE:
504 char temp_dir[MC_MAXPATHLEN];
506 g_strlcpy (temp_dir, in_start->buffer, sizeof (temp_dir));
507 #ifdef HAVE_CHARSET
508 options.file_all_charsets = file_all_charsets_cbox->state & C_BOOL;
509 options.content_all_charsets = content_all_charsets_cbox->state & C_BOOL;
510 #endif
511 options.content_case_sens = content_case_sens_cbox->state & C_BOOL;
512 options.content_regexp = content_regexp_cbox->state & C_BOOL;
513 options.content_first_hit = content_first_hit_cbox->state & C_BOOL;
514 options.content_whole_words = content_whole_words_cbox->state & C_BOOL;
515 options.file_pattern = file_pattern_cbox->state & C_BOOL;
516 options.file_case_sens = file_case_sens_cbox->state & C_BOOL;
517 options.find_recurs = recursively_cbox->state & C_BOOL;
518 options.skip_hidden = skip_hidden_cbox->state & C_BOOL;
519 destroy_dlg (find_dlg);
521 if ((temp_dir[0] == '\0') || ((temp_dir[0] == '.') && (temp_dir[1] == '\0')))
522 g_strlcpy (temp_dir, current_panel->cwd, sizeof (temp_dir));
524 if (in_start_dir != INPUT_LAST_TEXT)
525 g_free (in_start_dir);
526 in_start_dir = tree_box (temp_dir);
527 if (in_start_dir == NULL)
528 in_start_dir = g_strdup (temp_dir);
529 /* Warning: Dreadful goto */
530 goto find_par_start;
531 break;
534 default:
535 #ifdef HAVE_CHARSET
536 options.file_all_charsets = file_all_charsets_cbox->state & C_BOOL;
537 options.content_all_charsets = content_all_charsets_cbox->state & C_BOOL;
538 #endif
539 options.content_case_sens = content_case_sens_cbox->state & C_BOOL;
540 options.content_regexp = content_regexp_cbox->state & C_BOOL;
541 options.content_first_hit = content_first_hit_cbox->state & C_BOOL;
542 options.content_whole_words = content_whole_words_cbox->state & C_BOOL;
543 options.find_recurs = recursively_cbox->state & C_BOOL;
544 options.file_pattern = file_pattern_cbox->state & C_BOOL;
545 options.file_case_sens = file_case_sens_cbox->state & C_BOOL;
546 options.skip_hidden = skip_hidden_cbox->state & C_BOOL;
548 *content = (in_with->buffer[0] != '\0') ? g_strdup (in_with->buffer) : NULL;
549 *start_dir = g_strdup ((in_start->buffer[0] != '\0') ? in_start->buffer : ".");
550 *pattern = g_strdup (in_name->buffer);
551 if (in_start_dir != INPUT_LAST_TEXT)
552 g_free (in_start_dir);
553 in_start_dir = g_strdup (*start_dir);
555 find_save_options ();
557 return_value = TRUE;
560 destroy_dlg (find_dlg);
562 return return_value;
565 #if GLIB_CHECK_VERSION (2, 14, 0)
567 static inline void
568 push_directory (const char *dir)
570 g_queue_push_head (&dir_queue, (void *) dir);
573 static inline char *
574 pop_directory (void)
576 return (char *) g_queue_pop_tail (&dir_queue);
579 /* Remove all the items in the stack */
580 static void
581 clear_stack (void)
583 g_queue_foreach (&dir_queue, (GFunc) g_free, NULL);
584 g_queue_clear (&dir_queue);
587 #else /* GLIB_CHAECK_VERSION */
589 static void
590 push_directory (const char *dir)
592 dir_stack *new;
594 new = g_new (dir_stack, 1);
595 new->name = str_unconst (dir);
596 new->prev = dir_stack_base;
597 dir_stack_base = new;
600 static char *
601 pop_directory (void)
603 char *name = NULL;
605 if (dir_stack_base != NULL)
607 dir_stack *next;
608 name = dir_stack_base->name;
609 next = dir_stack_base->prev;
610 g_free (dir_stack_base);
611 dir_stack_base = next;
614 return name;
617 /* Remove all the items in the stack */
618 static void
619 clear_stack (void)
621 char *dir = NULL;
622 while ((dir = pop_directory ()) != NULL)
623 g_free (dir);
626 #endif /* GLIB_CHAECK_VERSION */
628 static void
629 insert_file (const char *dir, const char *file)
631 char *tmp_name = NULL;
632 static char *dirname = NULL;
634 while (dir[0] == PATH_SEP && dir[1] == PATH_SEP)
635 dir++;
637 if (old_dir)
639 if (strcmp (old_dir, dir))
641 g_free (old_dir);
642 old_dir = g_strdup (dir);
643 dirname = add_to_list (dir, NULL);
646 else
648 old_dir = g_strdup (dir);
649 dirname = add_to_list (dir, NULL);
652 tmp_name = g_strdup_printf (" %s", file);
653 add_to_list (tmp_name, dirname);
654 g_free (tmp_name);
657 static void
658 find_add_match (const char *dir, const char *file)
660 insert_file (dir, file);
662 /* Don't scroll */
663 if (matches == 0)
664 listbox_select_first (find_list);
665 send_message (&find_list->widget, WIDGET_DRAW, 0);
667 matches++;
668 found_num_update ();
672 * get_line_at:
674 * Returns malloced null-terminated line from file file_fd.
675 * Input is buffered in buf_size long buffer.
676 * Current pos in buf is stored in pos.
677 * n_read - number of read chars.
678 * has_newline - is there newline ?
680 static char *
681 get_line_at (int file_fd, char *buf, int buf_size, int *pos, int *n_read, gboolean * has_newline)
683 char *buffer = NULL;
684 int buffer_size = 0;
685 char ch = 0;
686 int i = 0;
688 for (;;)
690 if (*pos >= *n_read)
692 *pos = 0;
693 *n_read = mc_read (file_fd, buf, buf_size);
694 if (*n_read <= 0)
695 break;
698 ch = buf[(*pos)++];
699 if (ch == '\0')
701 /* skip possible leading zero(s) */
702 if (i == 0)
703 continue;
704 else
705 break;
708 if (i >= buffer_size - 1)
710 buffer = g_realloc (buffer, buffer_size += 80);
712 /* Strip newline */
713 if (ch == '\n')
714 break;
716 buffer[i++] = ch;
719 *has_newline = (ch != '\0');
721 if (buffer != NULL)
722 buffer[i] = '\0';
724 return buffer;
727 static FindProgressStatus
728 check_find_events (Dlg_head * h)
730 Gpm_Event event;
731 int c;
733 event.x = -1;
734 c = tty_get_event (&event, h->mouse_status == MOU_REPEAT, FALSE);
735 if (c != EV_NONE)
737 dlg_process_event (h, c, &event);
738 if (h->ret_value == B_ENTER
739 || h->ret_value == B_CANCEL || h->ret_value == B_AGAIN || h->ret_value == B_PANELIZE)
741 /* dialog terminated */
742 return FIND_ABORT;
744 if (!(h->flags & DLG_WANT_IDLE))
746 /* searching suspended */
747 return FIND_SUSPEND;
751 return FIND_CONT;
755 * search_content:
757 * Search the content_pattern string in the DIRECTORY/FILE.
758 * It will add the found entries to the find listbox.
760 * returns FALSE if do_search should look for another file
761 * TRUE if do_search should exit and proceed to the event handler
763 static gboolean
764 search_content (Dlg_head * h, const char *directory, const char *filename)
766 struct stat s;
767 char buffer[BUF_4K];
768 char *fname = NULL;
769 int file_fd;
770 gboolean ret_val = FALSE;
772 fname = concat_dir_and_file (directory, filename);
774 if (mc_stat (fname, &s) != 0 || !S_ISREG (s.st_mode))
776 g_free (fname);
777 return 0;
780 file_fd = mc_open (fname, O_RDONLY);
781 g_free (fname);
783 if (file_fd == -1)
784 return 0;
786 g_snprintf (buffer, sizeof (buffer), _("Grepping in %s"), str_trunc (filename, FIND2_X_USE));
788 status_update (buffer);
789 mc_refresh ();
791 tty_enable_interrupt_key ();
792 tty_got_interrupt ();
795 int line = 1;
796 int pos = 0;
797 int n_read = 0;
798 gboolean has_newline;
799 char *p = NULL;
800 gboolean found = FALSE;
801 gsize found_len;
802 char result[BUF_MEDIUM];
804 if (resuming)
806 /* We've been previously suspended, start from the previous position */
807 resuming = 0;
808 line = last_line;
809 pos = last_pos;
811 while (!ret_val
812 && (p = get_line_at (file_fd, buffer, sizeof (buffer),
813 &pos, &n_read, &has_newline)) != NULL)
815 if (!found /* Search in binary line once */
816 && mc_search_run (search_content_handle,
817 (const void *) p, 0, strlen (p), &found_len))
819 g_snprintf (result, sizeof (result), "%d:%s", line, filename);
820 find_add_match (directory, result);
821 found = TRUE;
823 g_free (p);
825 if (found && options.content_first_hit)
826 break;
828 if (has_newline)
830 found = FALSE;
831 line++;
834 if ((line & 0xff) == 0)
836 FindProgressStatus res;
837 res = check_find_events (h);
838 switch (res)
840 case FIND_ABORT:
841 stop_idle (h);
842 ret_val = TRUE;
843 break;
844 case FIND_SUSPEND:
845 resuming = 1;
846 last_line = line;
847 last_pos = pos;
848 ret_val = TRUE;
849 break;
850 default:
851 break;
857 tty_disable_interrupt_key ();
858 mc_close (file_fd);
859 return ret_val;
862 static int
863 do_search (struct Dlg_head *h)
865 static struct dirent *dp = NULL;
866 static DIR *dirp = NULL;
867 static char *directory = NULL;
868 struct stat tmp_stat;
869 static int pos = 0;
870 static int subdirs_left = 0;
871 gsize bytes_found;
872 unsigned long count; /* Number of files displayed */
874 if (!h)
875 { /* someone forces me to close dirp */
876 if (dirp)
878 mc_closedir (dirp);
879 dirp = NULL;
881 g_free (directory);
882 directory = NULL;
883 dp = NULL;
884 return 1;
887 search_content_handle = mc_search_new (content_pattern, -1);
888 if (search_content_handle)
890 search_content_handle->search_type =
891 options.content_regexp ? MC_SEARCH_T_REGEX : MC_SEARCH_T_NORMAL;
892 search_content_handle->is_case_sensitive = options.content_case_sens;
893 search_content_handle->whole_words = options.content_whole_words;
894 search_content_handle->is_all_charsets = options.content_all_charsets;
896 search_file_handle = mc_search_new (find_pattern, -1);
897 search_file_handle->search_type = options.file_pattern ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
898 search_file_handle->is_case_sensitive = options.file_case_sens;
899 search_file_handle->is_all_charsets = options.file_all_charsets;
900 search_file_handle->is_entire_line = options.file_pattern;
902 count = 0;
904 do_search_begin:
905 while (!dp)
907 if (dirp)
909 mc_closedir (dirp);
910 dirp = 0;
913 while (!dirp)
915 char *tmp = NULL;
917 tty_setcolor (REVERSE_COLOR);
918 while (1)
920 char *temp_dir = NULL;
921 gboolean found;
923 tmp = pop_directory ();
924 if (tmp == NULL)
926 running = FALSE;
927 status_update (_("Finished"));
928 stop_idle (h);
929 mc_search_free (search_file_handle);
930 search_file_handle = NULL;
931 mc_search_free (search_content_handle);
932 search_content_handle = NULL;
933 return 0;
936 if ((find_ignore_dirs == NULL) || (find_ignore_dirs[0] == '\0'))
937 break;
939 temp_dir = g_strdup_printf (":%s:", tmp);
940 found = strstr (find_ignore_dirs, temp_dir) != 0;
941 g_free (temp_dir);
943 if (!found)
944 break;
946 g_free (tmp);
949 g_free (directory);
950 directory = tmp;
952 if (verbose)
954 char buffer[BUF_SMALL];
956 g_snprintf (buffer, sizeof (buffer), _("Searching %s"),
957 str_trunc (directory, FIND2_X_USE));
958 status_update (buffer);
960 /* mc_stat should not be called after mc_opendir
961 because vfs_s_opendir modifies the st_nlink
963 if (!mc_stat (directory, &tmp_stat))
964 subdirs_left = tmp_stat.st_nlink - 2;
965 else
966 subdirs_left = 0;
968 dirp = mc_opendir (directory);
969 } /* while (!dirp) */
971 /* skip invalid filenames */
972 while ((dp = mc_readdir (dirp)) != NULL && !str_is_valid_string (dp->d_name))
974 } /* while (!dp) */
976 if (strcmp (dp->d_name, ".") == 0 || strcmp (dp->d_name, "..") == 0)
978 dp = mc_readdir (dirp);
979 /* skip invalid filenames */
980 while (dp != NULL && !str_is_valid_string (dp->d_name))
981 dp = mc_readdir (dirp);
983 mc_search_free (search_file_handle);
984 search_file_handle = NULL;
985 mc_search_free (search_content_handle);
986 search_content_handle = NULL;
987 return 1;
990 if (!(options.skip_hidden && (dp->d_name[0] == '.')))
992 gboolean search_ok;
994 if ((subdirs_left != 0) && options.find_recurs && (directory != NULL))
995 { /* Can directory be NULL ? */
996 char *tmp_name = concat_dir_and_file (directory, dp->d_name);
997 if (!mc_lstat (tmp_name, &tmp_stat) && S_ISDIR (tmp_stat.st_mode))
999 push_directory (tmp_name);
1000 subdirs_left--;
1002 else
1003 g_free (tmp_name);
1006 search_ok = mc_search_run (search_file_handle, dp->d_name,
1007 0, strlen (dp->d_name), &bytes_found);
1009 if (search_ok)
1011 if (content_pattern == NULL)
1012 find_add_match (directory, dp->d_name);
1013 else if (search_content (h, directory, dp->d_name))
1015 mc_search_free (search_file_handle);
1016 search_file_handle = NULL;
1017 mc_search_free (search_content_handle);
1018 search_content_handle = NULL;
1019 return 1;
1024 /* skip invalid filenames */
1025 while ((dp = mc_readdir (dirp)) != NULL && !str_is_valid_string (dp->d_name))
1028 /* Displays the nice dot */
1029 count++;
1030 if (!(count & 31))
1032 /* For nice updating */
1033 const char rotating_dash[] = "|/-\\";
1035 if (verbose)
1037 pos = (pos + 1) % 4;
1038 tty_setcolor (DLG_NORMALC (h));
1039 dlg_move (h, FIND2_Y - 7, FIND2_X - 4);
1040 tty_print_char (rotating_dash[pos]);
1041 mc_refresh ();
1044 else
1045 goto do_search_begin;
1047 mc_search_free (search_file_handle);
1048 search_file_handle = NULL;
1049 mc_search_free (search_content_handle);
1050 search_content_handle = NULL;
1051 return 1;
1054 static void
1055 init_find_vars (void)
1057 g_free (old_dir);
1058 old_dir = NULL;
1059 matches = 0;
1061 /* Remove all the items in the stack */
1062 clear_stack ();
1065 static char *
1066 make_fullname (const char *dirname, const char *filename)
1069 if (strcmp (dirname, ".") == 0 || strcmp (dirname, "." PATH_SEP_STR) == 0)
1070 return g_strdup (filename);
1071 if (strncmp (dirname, "." PATH_SEP_STR, 2) == 0)
1072 return concat_dir_and_file (dirname + 2, filename);
1073 return concat_dir_and_file (dirname, filename);
1076 static void
1077 find_do_view_edit (int unparsed_view, int edit, char *dir, char *file)
1079 char *fullname = NULL;
1080 const char *filename = NULL;
1081 int line;
1083 if (content_pattern != NULL)
1085 filename = strchr (file + 4, ':') + 1;
1086 line = atoi (file + 4);
1088 else
1090 filename = file + 4;
1091 line = 0;
1094 fullname = make_fullname (dir, filename);
1095 if (edit)
1096 do_edit_at_line (fullname, line);
1097 else
1098 view_file_at_line (fullname, unparsed_view, use_internal_view, line);
1099 g_free (fullname);
1102 static cb_ret_t
1103 view_edit_currently_selected_file (int unparsed_view, int edit)
1105 char *dir = NULL;
1106 char *text = NULL;
1108 listbox_get_current (find_list, &text, (void **) &dir);
1110 if ((text == NULL) || (dir == NULL))
1111 return MSG_NOT_HANDLED;
1113 find_do_view_edit (unparsed_view, edit, dir, text);
1114 return MSG_HANDLED;
1117 static cb_ret_t
1118 find_callback (struct Dlg_head *h, Widget * sender, dlg_msg_t msg, int parm, void *data)
1120 switch (msg)
1122 case DLG_KEY:
1123 if (parm == KEY_F (3) || parm == KEY_F (13))
1125 int unparsed_view = (parm == KEY_F (13));
1126 return view_edit_currently_selected_file (unparsed_view, 0);
1128 if (parm == KEY_F (4))
1130 return view_edit_currently_selected_file (0, 1);
1132 return MSG_NOT_HANDLED;
1134 case DLG_IDLE:
1135 do_search (h);
1136 return MSG_HANDLED;
1138 default:
1139 return default_dlg_callback (h, sender, msg, parm, data);
1143 /* Handles the Stop/Start button in the find window */
1144 static int
1145 start_stop (int button)
1147 (void) button;
1149 running = is_start;
1150 set_idle_proc (find_dlg, running);
1151 is_start = !is_start;
1153 status_update (is_start ? _("Stopped") : _("Searching"));
1154 button_set_text (stop_button, fbuts[is_start ? 1 : 0].text);
1156 return 0;
1159 /* Handle view command, when invoked as a button */
1160 static int
1161 find_do_view_file (int button)
1163 (void) button;
1165 view_edit_currently_selected_file (0, 0);
1166 return 0;
1169 /* Handle edit command, when invoked as a button */
1170 static int
1171 find_do_edit_file (int button)
1173 (void) button;
1175 view_edit_currently_selected_file (0, 1);
1176 return 0;
1179 static void
1180 setup_gui (void)
1182 #ifdef ENABLE_NLS
1183 static gboolean i18n_flag = FALSE;
1185 if (!i18n_flag)
1187 int i = sizeof (fbuts) / sizeof (fbuts[0]);
1188 while (i-- != 0)
1190 fbuts[i].text = _(fbuts[i].text);
1191 fbuts[i].len = str_term_width1 (fbuts[i].text) + 3;
1194 fbuts[2].len += 2; /* DEFPUSH_BUTTON */
1195 i18n_flag = TRUE;
1197 #endif /* ENABLE_NLS */
1200 * Dynamically place buttons centered within current window size
1203 int l0 = max (fbuts[0].len, fbuts[1].len);
1204 int l1 = fbuts[2].len + fbuts[3].len + l0 + fbuts[4].len;
1205 int l2 = fbuts[5].len + fbuts[6].len + fbuts[7].len;
1206 int r1, r2;
1208 /* Check, if both button rows fit within FIND2_X */
1209 FIND2_X = max (l1 + 9, COLS - 16);
1210 FIND2_X = max (l2 + 8, FIND2_X);
1212 /* compute amount of space between buttons for each row */
1213 r1 = (FIND2_X - 4 - l1) % 5;
1214 l1 = (FIND2_X - 4 - l1) / 5;
1215 r2 = (FIND2_X - 4 - l2) % 4;
1216 l2 = (FIND2_X - 4 - l2) / 4;
1218 /* ...and finally, place buttons */
1219 fbuts[2].x = 2 + r1 / 2 + l1;
1220 fbuts[3].x = fbuts[2].x + fbuts[2].len + l1;
1221 fbuts[0].x = fbuts[3].x + fbuts[3].len + l1;
1222 fbuts[4].x = fbuts[0].x + l0 + l1;
1223 fbuts[5].x = 2 + r2 / 2 + l2;
1224 fbuts[6].x = fbuts[5].x + fbuts[5].len + l2;
1225 fbuts[7].x = fbuts[6].x + fbuts[6].len + l2;
1228 find_dlg =
1229 create_dlg (0, 0, FIND2_Y, FIND2_X, dialog_colors, find_callback,
1230 "[Find File]", _("Find File"), DLG_CENTER | DLG_REVERSE);
1232 add_widget (find_dlg,
1233 button_new (FIND2_Y - 3, fbuts[7].x, B_VIEW, NORMAL_BUTTON,
1234 fbuts[7].text, find_do_edit_file));
1235 add_widget (find_dlg,
1236 button_new (FIND2_Y - 3, fbuts[6].x, B_VIEW, NORMAL_BUTTON,
1237 fbuts[6].text, find_do_view_file));
1238 add_widget (find_dlg,
1239 button_new (FIND2_Y - 3, fbuts[5].x, B_PANELIZE, NORMAL_BUTTON, fbuts[5].text, 0));
1241 add_widget (find_dlg,
1242 button_new (FIND2_Y - 4, fbuts[4].x, B_CANCEL, NORMAL_BUTTON, fbuts[4].text, 0));
1243 stop_button =
1244 button_new (FIND2_Y - 4, fbuts[0].x, B_STOP, NORMAL_BUTTON, fbuts[0].text, start_stop);
1245 add_widget (find_dlg, stop_button);
1246 add_widget (find_dlg,
1247 button_new (FIND2_Y - 4, fbuts[3].x, B_AGAIN, NORMAL_BUTTON, fbuts[3].text, 0));
1248 add_widget (find_dlg,
1249 button_new (FIND2_Y - 4, fbuts[2].x, B_ENTER, DEFPUSH_BUTTON, fbuts[2].text, 0));
1251 status_label = label_new (FIND2_Y - 7, 4, _("Searching"));
1252 add_widget (find_dlg, status_label);
1254 found_num_label = label_new (FIND2_Y - 6, 4, "");
1255 add_widget (find_dlg, found_num_label);
1257 find_list = listbox_new (2, 2, FIND2_Y - 10, FIND2_X - 4, FALSE, NULL);
1258 add_widget (find_dlg, find_list);
1261 static int
1262 run_process (void)
1264 resuming = 0;
1265 set_idle_proc (find_dlg, 1);
1266 return run_dlg (find_dlg);
1269 static void
1270 kill_gui (void)
1272 set_idle_proc (find_dlg, 0);
1273 destroy_dlg (find_dlg);
1276 static int
1277 find_file (const char *start_dir, const char *pattern, const char *content,
1278 char **dirname, char **filename)
1280 int return_value = 0;
1281 char *dir_tmp = NULL, *file_tmp = NULL;
1283 setup_gui ();
1285 /* FIXME: Need to cleanup this, this ought to be passed non-globaly */
1286 find_pattern = str_unconst (pattern);
1287 content_pattern = (content != NULL && str_is_valid_string (content))
1288 ? g_strdup (content) : NULL;
1290 init_find_vars ();
1291 push_directory (start_dir);
1293 return_value = run_process ();
1295 /* Remove all the items in the stack */
1296 clear_stack ();
1298 get_list_info (&file_tmp, &dir_tmp);
1300 if (dir_tmp)
1301 *dirname = g_strdup (dir_tmp);
1302 if (file_tmp)
1303 *filename = g_strdup (file_tmp);
1305 if (return_value == B_PANELIZE && *filename)
1307 int status, link_to_dir, stale_link;
1308 int next_free = 0;
1309 int i;
1310 struct stat st;
1311 GList *entry;
1312 dir_list *list = &current_panel->dir;
1313 char *name = NULL;
1315 for (i = 0, entry = find_list->list; entry != NULL; i++, entry = g_list_next (entry))
1317 const char *lc_filename = NULL;
1318 WLEntry *le = (WLEntry *) entry->data;
1320 if ((le->text == NULL) || (le->data == NULL))
1321 continue;
1323 if (content_pattern != NULL)
1324 lc_filename = strchr (le->text + 4, ':') + 1;
1325 else
1326 lc_filename = le->text + 4;
1328 name = make_fullname (le->data, lc_filename);
1329 status = handle_path (list, name, &st, next_free, &link_to_dir, &stale_link);
1330 if (status == 0)
1332 g_free (name);
1333 continue;
1335 if (status == -1)
1337 g_free (name);
1338 break;
1341 /* don't add files more than once to the panel */
1342 if (content_pattern != NULL && next_free > 0
1343 && strcmp (list->list[next_free - 1].fname, name) == 0)
1345 g_free (name);
1346 continue;
1349 if (!next_free) /* first turn i.e clean old list */
1350 panel_clean_dir (current_panel);
1351 list->list[next_free].fnamelen = strlen (name);
1352 list->list[next_free].fname = name;
1353 list->list[next_free].f.marked = 0;
1354 list->list[next_free].f.link_to_dir = link_to_dir;
1355 list->list[next_free].f.stale_link = stale_link;
1356 list->list[next_free].f.dir_size_computed = 0;
1357 list->list[next_free].st = st;
1358 list->list[next_free].sort_key = NULL;
1359 list->list[next_free].second_sort_key = NULL;
1360 next_free++;
1361 if (!(next_free & 15))
1362 rotate_dash ();
1365 if (next_free)
1367 current_panel->count = next_free;
1368 current_panel->is_panelized = 1;
1370 if (start_dir[0] == PATH_SEP)
1372 int ret;
1373 strcpy (current_panel->cwd, PATH_SEP_STR);
1374 ret = chdir (PATH_SEP_STR);
1379 g_free (content_pattern);
1380 kill_gui ();
1381 do_search (NULL); /* force do_search to release resources */
1382 g_free (old_dir);
1383 old_dir = NULL;
1385 return return_value;
1388 void
1389 do_find (void)
1391 char *start_dir = NULL, *pattern = NULL, *content = NULL;
1392 char *filename = NULL, *dirname = NULL;
1393 int v;
1394 gboolean dir_and_file_set;
1396 while (find_parameters (&start_dir, &pattern, &content))
1398 if (pattern[0] == '\0')
1399 break; /* nothing search */
1401 dirname = filename = NULL;
1402 is_start = FALSE;
1403 v = find_file (start_dir, pattern, content, &dirname, &filename);
1404 g_free (pattern);
1406 if (v == B_ENTER)
1408 if (dirname || filename)
1410 if (dirname)
1412 do_cd (dirname, cd_exact);
1413 if (filename)
1414 try_to_select (current_panel, filename + (content ?
1415 (strchr (filename + 4, ':') -
1416 filename + 1) : 4));
1418 else if (filename)
1419 do_cd (filename, cd_exact);
1420 select_item (current_panel);
1422 g_free (dirname);
1423 g_free (filename);
1424 break;
1427 g_free (content);
1428 dir_and_file_set = dirname && filename;
1429 g_free (dirname);
1430 g_free (filename);
1432 if (v == B_CANCEL)
1433 break;
1435 if (v == B_PANELIZE)
1437 if (dir_and_file_set)
1439 try_to_select (current_panel, NULL);
1440 panel_re_sort (current_panel);
1441 try_to_select (current_panel, NULL);
1443 break;