Merge branch '54_dlg_mouse_handling'
[midnight-commander.git] / src / find.c
blobd073b77f175cd35f4b56654baf288674f5696581
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 "global.h"
37 #include "../src/tty/tty.h"
38 #include "../src/skin/skin.h"
39 #include "../src/tty/key.h"
41 #include "../src/search/search.h"
43 #include "setup.h"
44 #include "find.h"
45 #include "strutil.h"
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 /* Size of the find parameters window */
58 #if HAVE_CHARSET
59 static int FIND_Y = 16;
60 #else
61 static int FIND_Y = 15;
62 #endif
63 static int FIND_X = 68;
65 /* Size of the find window */
66 #define FIND2_Y (LINES - 4)
68 static int FIND2_X = 64;
69 #define FIND2_X_USE (FIND2_X - 20)
71 /* A couple of extra messages we need */
72 enum {
73 B_STOP = B_USER + 1,
74 B_AGAIN,
75 B_PANELIZE,
76 B_TREE,
77 B_VIEW
80 typedef enum {
81 FIND_CONT = 0,
82 FIND_SUSPEND,
83 FIND_ABORT
84 } FindProgressStatus;
86 /* List of directories to be ignored, separated by ':' */
87 char *find_ignore_dirs = NULL;
89 /* static variables to remember find parameters */
90 static WInput *in_start; /* Start path */
91 static WInput *in_name; /* Filename */
92 static WInput *in_with; /* Text inside filename */
93 static WCheck *file_case_sens_cbox; /* "case sensitive" checkbox */
94 static WCheck *file_pattern_cbox; /* File name is glob or regexp */
95 static WCheck *recursively_cbox;
96 static WCheck *skip_hidden_cbox;
97 static WCheck *content_case_sens_cbox; /* "case sensitive" checkbox */
98 static WCheck *content_regexp_cbox; /* "find regular expression" checkbox */
99 static WCheck *content_first_hit_cbox; /* "First hit" checkbox" */
100 static WCheck *content_whole_words_cbox; /* "whole words" checkbox */
101 #ifdef HAVE_CHARSET
102 static WCheck *file_all_charsets_cbox;
103 static WCheck *content_all_charsets_cbox;
104 #endif
106 static gboolean running = FALSE; /* nice flag */
107 static char *find_pattern = NULL; /* Pattern to search */
108 static char *content_pattern = NULL; /* pattern to search inside files; if
109 content_regexp_flag is true, it contains the
110 regex pattern, else the search string. */
111 static unsigned long matches; /* Number of matches */
112 static gboolean is_start = FALSE; /* Status of the start/stop toggle button */
113 static char *old_dir = NULL;
115 /* Where did we stop */
116 static int resuming;
117 static int last_line;
118 static int last_pos;
120 static Dlg_head *find_dlg; /* The dialog */
121 static WButton *stop_button; /* pointer to the stop button */
122 static WLabel *status_label; /* Finished, Searching etc. */
123 static WLabel *found_num_label; /* Number of found items */
124 static WListbox *find_list; /* Listbox with the file list */
126 /* This keeps track of the directory stack */
127 #if GLIB_CHECK_VERSION (2, 14, 0)
128 static GQueue dir_queue = G_QUEUE_INIT;
129 #else
130 typedef struct dir_stack {
131 char *name;
132 struct dir_stack *prev;
133 } dir_stack;
135 static dir_stack *dir_stack_base = 0;
136 #endif /* GLIB_CHECK_VERSION */
138 static struct {
139 const char* text;
140 int len; /* length including space and brackets */
141 int x;
142 } fbuts [] = {
143 { N_("&Suspend"), 11, 29 },
144 { N_("Con&tinue"), 12, 29 },
145 { N_("&Chdir"), 11, 3 },
146 { N_("&Again"), 9, 17 },
147 { N_("&Quit"), 8, 43 },
148 { N_("Pane&lize"), 12, 3 },
149 { N_("&View - F3"), 13, 20 },
150 { N_("&Edit - F4"), 13, 38 }
153 static char *in_start_dir = INPUT_LAST_TEXT;
155 static mc_search_t *search_file_handle = NULL;
156 static gboolean skip_hidden_flag = FALSE;
157 static gboolean file_pattern_flag = TRUE;
158 static gboolean file_all_charsets_flag = FALSE;
159 static gboolean file_case_sens_flag = TRUE;
160 static gboolean find_recurs_flag = TRUE;
162 static mc_search_t *search_content_handle = NULL;
163 static gboolean content_regexp_flag = FALSE;
164 static gboolean content_all_charsets_flag = FALSE;
165 static gboolean content_case_sens_flag = TRUE;
166 static gboolean content_first_hit_flag = FALSE;
167 static gboolean content_whole_words = FALSE;
169 static inline char *
170 add_to_list (const char *text, void *data)
172 return listbox_add_item (find_list, LISTBOX_APPEND_AT_END, 0, text, data);
175 static inline void
176 stop_idle (void *data)
178 set_idle_proc (data, 0);
181 static inline void
182 status_update (const char *text)
184 label_set_text (status_label, text);
187 static void
188 found_num_update (void)
190 char buffer [BUF_TINY];
191 g_snprintf (buffer, sizeof (buffer), _("Found: %ld"), matches);
192 label_set_text (found_num_label, buffer);
195 static void
196 get_list_info (char **file, char **dir)
198 listbox_get_current (find_list, file, dir);
201 /* check regular expression */
202 gboolean
203 find_check_regexp (const char *r)
205 mc_search_t *search;
206 gboolean regexp_ok = FALSE;
208 search = mc_search_new (r, -1);
210 if (search != NULL) {
211 search->search_type = MC_SEARCH_T_REGEX;
212 regexp_ok = mc_search_prepare (search);
213 mc_search_free (search);
216 return regexp_ok;
220 * Callback for the parameter dialog.
221 * Validate regex, prevent closing the dialog if it's invalid.
223 static cb_ret_t
224 find_parm_callback (struct Dlg_head *h, dlg_msg_t msg, int parm)
226 switch (msg) {
227 case DLG_VALIDATE:
228 if (h->ret_value != B_ENTER)
229 return MSG_HANDLED;
231 /* check filename regexp */
232 if (!(file_pattern_cbox->state & C_BOOL)
233 && (in_name->buffer[0] != '\0')
234 && !find_check_regexp (in_name->buffer)) {
235 message (D_ERROR, MSG_ERROR, _(" Malformed regular expression "));
236 dlg_select_widget (in_name);
237 h->running = 1; /* Don't stop the dialog */
238 return MSG_HANDLED;
241 /* check content regexp */
242 if ((content_regexp_cbox->state & C_BOOL)
243 && (in_with->buffer[0] != '\0')
244 && !find_check_regexp (in_with->buffer)) {
245 message (D_ERROR, MSG_ERROR, _(" Malformed regular expression "));
246 dlg_select_widget (in_with);
247 h->running = 1; /* Don't stop the dialog */
248 return MSG_HANDLED;
251 return MSG_HANDLED;
253 default:
254 return default_dlg_callback (h, msg, parm);
259 * find_parameters: gets information from the user
261 * If the return value is true, then the following holds:
263 * START_DIR and PATTERN are pointers to char * and upon return they
264 * contain the information provided by the user.
266 * CONTENT holds a strdup of the contents specified by the user if he
267 * asked for them or 0 if not (note, this is different from the
268 * behavior for the other two parameters.
271 static int
272 find_parameters (char **start_dir, char **pattern, char **content)
274 int return_value;
275 char *temp_dir = NULL;
277 /* file name */
278 const char *file_case_label = N_("Cas&e sensitive");
279 const char *file_pattern_label = N_("&Using shell patterns");
280 const char *file_recurs_label = N_("&Find recursively");
281 const char *file_skip_hidden_label = N_("S&kip hidden");
282 const char *file_all_charsets_label = N_("&All charsets");
284 /* file content */
285 const char *content_case_label = N_("Case sens&itive");
286 const char *content_regexp_label = N_("Re&gular expression");
287 const char *content_first_hit_label = N_("Fir&st hit");
288 const char *content_whole_words_label = N_("&Whole words");
289 const char *content_all_charsets_label = N_("All cha&rsets");
291 const char *buts[] = { N_("&OK"), N_("&Cancel"), N_("&Tree") };
293 int b0, b1, b2;
295 #ifdef ENABLE_NLS
297 int i = sizeof (buts) / sizeof (buts[0]);
298 while (i-- != 0)
299 buts[i] = _(buts[i]);
301 file_case_label = _(file_case_label);
302 file_pattern_label = _(file_pattern_label);
303 file_recurs_label = _(file_recurs_label);
304 file_skip_hidden_label = _(file_skip_hidden_label);
305 file_all_charsets_label = _(file_all_charsets_label);
306 content_case_label = _(content_case_label);
307 content_regexp_label = _(content_regexp_label);
308 content_first_hit_label = _(content_first_hit_label);
309 content_whole_words_label = _(content_whole_words_label);
310 content_all_charsets_label = _(content_all_charsets_label);
312 #endif /* ENABLE_NLS */
314 b0 = str_term_width1 (buts[0]) + 6; /* default button */
315 b1 = str_term_width1 (buts[1]) + 4;
316 b2 = str_term_width1 (buts[2]) + 4;
318 find_par_start:
319 if (in_start_dir == NULL)
320 in_start_dir = g_strdup (".");
322 find_dlg =
323 create_dlg (0, 0, FIND_Y, FIND_X, dialog_colors,
324 find_parm_callback, "[Find File]", _("Find File"),
325 DLG_CENTER | DLG_REVERSE);
327 add_widget (find_dlg,
328 button_new (FIND_Y - 3, FIND_X * 3/4 - b1/2, B_CANCEL, NORMAL_BUTTON, buts[1], 0));
329 add_widget (find_dlg,
330 button_new (FIND_Y - 3, FIND_X/4 - b0/2, B_ENTER, DEFPUSH_BUTTON, buts[0], 0));
332 #ifdef HAVE_CHARSET
333 content_all_charsets_cbox = check_new (11, FIND_X / 2 + 1,
334 content_all_charsets_flag, content_all_charsets_label);
335 add_widget (find_dlg, content_all_charsets_cbox);
336 #endif
338 content_whole_words_cbox = check_new (10, FIND_X / 2 + 1, content_whole_words, content_whole_words_label);
339 add_widget (find_dlg, content_whole_words_cbox);
341 content_first_hit_cbox = check_new (9, FIND_X / 2 + 1, content_first_hit_flag, content_first_hit_label);
342 add_widget (find_dlg, content_first_hit_cbox);
344 content_regexp_cbox = check_new (8, FIND_X / 2 + 1, content_regexp_flag, content_regexp_label);
345 add_widget (find_dlg, content_regexp_cbox);
347 content_case_sens_cbox = check_new (7, FIND_X / 2 + 1, content_case_sens_flag, content_case_label);
348 add_widget (find_dlg, content_case_sens_cbox);
350 #ifdef HAVE_CHARSET
351 file_all_charsets_cbox = check_new (11, 3,
352 file_all_charsets_flag, file_all_charsets_label);
353 add_widget (find_dlg, file_all_charsets_cbox);
354 #endif
356 skip_hidden_cbox = check_new (10, 3, skip_hidden_flag, file_skip_hidden_label);
357 add_widget (find_dlg, skip_hidden_cbox);
359 recursively_cbox = check_new (9, 3, find_recurs_flag, file_recurs_label);
360 add_widget (find_dlg, recursively_cbox);
362 file_pattern_cbox = check_new (8, 3, file_pattern_flag, file_pattern_label);
363 add_widget (find_dlg, file_pattern_cbox);
365 file_case_sens_cbox = check_new (7, 3, file_case_sens_flag, file_case_label);
366 add_widget (find_dlg, file_case_sens_cbox);
368 in_with = input_new (6, FIND_X / 2 + 1, INPUT_COLOR, FIND_X / 2 - 4, INPUT_LAST_TEXT,
369 MC_HISTORY_SHARED_SEARCH, INPUT_COMPLETE_DEFAULT);
370 add_widget (find_dlg, in_with);
371 add_widget (find_dlg, label_new (5, FIND_X / 2 + 1, _("Content:")));
373 in_name = input_new (6, 3, INPUT_COLOR, FIND_X / 2 - 4, INPUT_LAST_TEXT, "name",
374 INPUT_COMPLETE_DEFAULT);
375 add_widget (find_dlg, in_name);
376 add_widget (find_dlg, label_new (5, 3, _("File name:")));
378 add_widget (find_dlg,
379 button_new (3, FIND_X - b2 - 2, B_TREE, NORMAL_BUTTON, buts[2], 0));
381 in_start = input_new (3, 3, INPUT_COLOR, FIND_X - b2 - 6, in_start_dir, "start",
382 INPUT_COMPLETE_DEFAULT);
383 add_widget (find_dlg, in_start);
384 add_widget (find_dlg, label_new (2, 3, _("Start at:")));
386 dlg_select_widget (in_name);
388 switch (run_dlg (find_dlg)) {
389 case B_CANCEL:
390 return_value = 0;
391 break;
393 case B_TREE:
394 #ifdef HAVE_CHARSET
395 file_all_charsets_flag = file_all_charsets_cbox->state & C_BOOL;
396 content_all_charsets_flag = content_all_charsets_cbox->state & C_BOOL;
397 #endif
398 content_case_sens_flag = content_case_sens_cbox->state & C_BOOL;
399 content_regexp_flag = content_regexp_cbox->state & C_BOOL;
400 content_first_hit_flag = content_first_hit_cbox->state & C_BOOL;
401 content_whole_words = content_whole_words_cbox->state & C_BOOL;
402 file_pattern_flag = file_pattern_cbox->state & C_BOOL;
403 file_case_sens_flag = file_case_sens_cbox->state & C_BOOL;
404 find_recurs_flag = recursively_cbox->state & C_BOOL;
405 skip_hidden_flag = skip_hidden_cbox->state & C_BOOL;
406 destroy_dlg (find_dlg);
407 if (in_start_dir != INPUT_LAST_TEXT)
408 g_free (in_start_dir);
409 temp_dir = g_strdup (in_start->buffer);
410 if ((temp_dir[0] == '.') && (temp_dir[1] == '\0')) {
411 g_free (temp_dir);
412 temp_dir = g_strdup (current_panel->cwd);
414 in_start_dir = tree_box (temp_dir);
415 if (in_start_dir != NULL)
416 g_free (temp_dir);
417 else
418 in_start_dir = temp_dir;
419 /* Warning: Dreadful goto */
420 goto find_par_start;
421 break;
423 default:
424 #ifdef HAVE_CHARSET
425 file_all_charsets_flag = file_all_charsets_cbox->state & C_BOOL;
426 content_all_charsets_flag = content_all_charsets_cbox->state & C_BOOL;
427 #endif
428 content_case_sens_flag = content_case_sens_cbox->state & C_BOOL;
429 content_regexp_flag = content_regexp_cbox->state & C_BOOL;
430 content_first_hit_flag = content_first_hit_cbox->state & C_BOOL;
431 content_whole_words = content_whole_words_cbox->state & C_BOOL;
432 find_recurs_flag = recursively_cbox->state & C_BOOL;
433 file_pattern_flag = file_pattern_cbox->state & C_BOOL;
434 file_case_sens_flag = file_case_sens_cbox->state & C_BOOL;
435 skip_hidden_flag = skip_hidden_cbox->state & C_BOOL;
436 return_value = 1;
437 *content = (in_with->buffer[0] != '\0') ? g_strdup (in_with->buffer) : NULL;
438 *start_dir = g_strdup (in_start->buffer);
439 *pattern = g_strdup (in_name->buffer);
440 if (in_start_dir != INPUT_LAST_TEXT)
441 g_free (in_start_dir);
442 in_start_dir = g_strdup (*start_dir);
445 destroy_dlg (find_dlg);
447 return return_value;
450 #if GLIB_CHECK_VERSION (2, 14, 0)
452 static inline void
453 push_directory (const char *dir)
455 g_queue_push_head (&dir_queue, (void *) dir);
458 static inline char *
459 pop_directory (void)
461 return (char *) g_queue_pop_tail (&dir_queue);
464 /* Remove all the items in the stack */
465 static void
466 clear_stack (void)
468 g_queue_foreach (&dir_queue, (GFunc) g_free, NULL);
469 g_queue_clear (&dir_queue);
472 #else /* GLIB_CHAECK_VERSION */
474 static void
475 push_directory (const char *dir)
477 dir_stack *new;
479 new = g_new (dir_stack, 1);
480 new->name = str_unconst (dir);
481 new->prev = dir_stack_base;
482 dir_stack_base = new;
485 static char *
486 pop_directory (void)
488 char *name = NULL;
490 if (dir_stack_base != NULL) {
491 dir_stack *next;
492 name = dir_stack_base->name;
493 next = dir_stack_base->prev;
494 g_free (dir_stack_base);
495 dir_stack_base = next;
498 return name;
501 /* Remove all the items in the stack */
502 static void
503 clear_stack (void)
505 char *dir = NULL;
506 while ((dir = pop_directory ()) != NULL)
507 g_free (dir);
510 #endif /* GLIB_CHAECK_VERSION */
512 static void
513 insert_file (const char *dir, const char *file)
515 char *tmp_name = NULL;
516 static char *dirname = NULL;
518 while (dir [0] == PATH_SEP && dir [1] == PATH_SEP)
519 dir++;
521 if (old_dir){
522 if (strcmp (old_dir, dir)){
523 g_free (old_dir);
524 old_dir = g_strdup (dir);
525 dirname = add_to_list (dir, NULL);
527 } else {
528 old_dir = g_strdup (dir);
529 dirname = add_to_list (dir, NULL);
532 tmp_name = g_strdup_printf (" %s", file);
533 add_to_list (tmp_name, dirname);
534 g_free (tmp_name);
537 static void
538 find_add_match (const char *dir, const char *file)
540 insert_file (dir, file);
542 /* Don't scroll */
543 if (matches == 0)
544 listbox_select_by_number (find_list, 0);
545 send_message (&find_list->widget, WIDGET_DRAW, 0);
547 matches++;
548 found_num_update ();
552 * get_line_at:
554 * Returns malloced null-terminated line from file file_fd.
555 * Input is buffered in buf_size long buffer.
556 * Current pos in buf is stored in pos.
557 * n_read - number of read chars.
558 * has_newline - is there newline ?
560 static char *
561 get_line_at (int file_fd, char *buf, int buf_size, int *pos, int *n_read,
562 gboolean *has_newline)
564 char *buffer = NULL;
565 int buffer_size = 0;
566 char ch = 0;
567 int i = 0;
569 for (;;) {
570 if (*pos >= *n_read) {
571 *pos = 0;
572 *n_read = mc_read (file_fd, buf, buf_size);
573 if (*n_read <= 0)
574 break;
577 ch = buf[(*pos)++];
578 if (ch == '\0') {
579 /* skip possible leading zero(s) */
580 if (i == 0)
581 continue;
582 else
583 break;
586 if (i >= buffer_size - 1) {
587 buffer = g_realloc (buffer, buffer_size += 80);
589 /* Strip newline */
590 if (ch == '\n')
591 break;
593 buffer[i++] = ch;
596 *has_newline = (ch != '\0');
598 if (buffer != NULL)
599 buffer[i] = '\0';
601 return buffer;
604 static FindProgressStatus
605 check_find_events(Dlg_head *h)
607 Gpm_Event event;
608 int c;
610 event.x = -1;
611 c = tty_get_event (&event, h->mouse_status == MOU_REPEAT, FALSE);
612 if (c != EV_NONE) {
613 dlg_process_event (h, c, &event);
614 if (h->ret_value == B_ENTER
615 || h->ret_value == B_CANCEL
616 || h->ret_value == B_AGAIN
617 || h->ret_value == B_PANELIZE) {
618 /* dialog terminated */
619 return FIND_ABORT;
621 if (!(h->flags & DLG_WANT_IDLE)) {
622 /* searching suspended */
623 return FIND_SUSPEND;
627 return FIND_CONT;
631 * search_content:
633 * Search the content_pattern string in the DIRECTORY/FILE.
634 * It will add the found entries to the find listbox.
636 * returns FALSE if do_search should look for another file
637 * TRUE if do_search should exit and proceed to the event handler
639 static gboolean
640 search_content (Dlg_head *h, const char *directory, const char *filename)
642 struct stat s;
643 char buffer [BUF_4K];
644 char *fname = NULL;
645 int file_fd;
646 gboolean ret_val = FALSE;
648 fname = concat_dir_and_file (directory, filename);
650 if (mc_stat (fname, &s) != 0 || !S_ISREG (s.st_mode)){
651 g_free (fname);
652 return 0;
655 file_fd = mc_open (fname, O_RDONLY);
656 g_free (fname);
658 if (file_fd == -1)
659 return 0;
661 g_snprintf (buffer, sizeof (buffer), _("Grepping in %s"), str_trunc (filename, FIND2_X_USE));
663 status_update (buffer);
664 mc_refresh ();
666 tty_enable_interrupt_key ();
667 tty_got_interrupt ();
670 int line = 1;
671 int pos = 0;
672 int n_read = 0;
673 gboolean has_newline;
674 char *p = NULL;
675 gboolean found = FALSE;
676 gsize found_len;
677 char result [BUF_MEDIUM];
679 if (resuming) {
680 /* We've been previously suspended, start from the previous position */
681 resuming = 0;
682 line = last_line;
683 pos = last_pos;
685 while (!ret_val
686 && (p = get_line_at (file_fd, buffer, sizeof (buffer),
687 &pos, &n_read, &has_newline)) != NULL) {
688 if (!found /* Search in binary line once */
689 && mc_search_run (search_content_handle,
690 (const void *) p, 0, strlen (p), &found_len)) {
691 g_snprintf (result, sizeof (result), "%d:%s", line, filename);
692 find_add_match (directory, result);
693 found = TRUE;
695 g_free (p);
697 if (found && content_first_hit_flag)
698 break;
700 if (has_newline) {
701 found = FALSE;
702 line++;
705 if ((line & 0xff) == 0) {
706 FindProgressStatus res;
707 res = check_find_events(h);
708 switch (res) {
709 case FIND_ABORT:
710 stop_idle (h);
711 ret_val = TRUE;
712 break;
713 case FIND_SUSPEND:
714 resuming = 1;
715 last_line = line;
716 last_pos = pos;
717 ret_val = TRUE;
718 break;
719 default:
720 break;
726 tty_disable_interrupt_key ();
727 mc_close (file_fd);
728 return ret_val;
731 static int
732 do_search (struct Dlg_head *h)
734 static struct dirent *dp = NULL;
735 static DIR *dirp = NULL;
736 static char *directory = NULL;
737 struct stat tmp_stat;
738 static int pos = 0;
739 static int subdirs_left = 0;
740 gsize bytes_found;
741 unsigned long count; /* Number of files displayed */
743 if (!h) { /* someone forces me to close dirp */
744 if (dirp) {
745 mc_closedir (dirp);
746 dirp = NULL;
748 g_free (directory);
749 directory = NULL;
750 dp = NULL;
751 return 1;
754 search_content_handle = mc_search_new(content_pattern, -1);
755 if (search_content_handle) {
756 search_content_handle->search_type = (content_regexp_flag) ? MC_SEARCH_T_REGEX : MC_SEARCH_T_NORMAL;
757 search_content_handle->is_case_sentitive = content_case_sens_flag;
758 search_content_handle->whole_words = content_whole_words;
759 search_content_handle->is_all_charsets = content_all_charsets_flag;
761 search_file_handle = mc_search_new(find_pattern, -1);
762 search_file_handle->search_type = (file_pattern_flag) ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
763 search_file_handle->is_case_sentitive = file_case_sens_flag;
764 search_file_handle->is_all_charsets = file_all_charsets_flag;
765 search_file_handle->is_entire_line = file_pattern_flag;
767 count = 0;
769 do_search_begin:
770 while (!dp){
771 if (dirp){
772 mc_closedir (dirp);
773 dirp = 0;
776 while (!dirp){
777 char *tmp = NULL;
779 tty_setcolor (REVERSE_COLOR);
780 while (1) {
781 char *temp_dir = NULL;
782 gboolean found;
784 tmp = pop_directory ();
785 if (tmp == NULL) {
786 running = FALSE;
787 status_update (_("Finished"));
788 stop_idle (h);
789 mc_search_free (search_file_handle);
790 search_file_handle = NULL;
791 mc_search_free (search_content_handle);
792 search_content_handle = NULL;
793 return 0;
796 if ((find_ignore_dirs == NULL) || (find_ignore_dirs[0] == '\0'))
797 break;
799 temp_dir = g_strdup_printf (":%s:", tmp);
800 found = strstr (find_ignore_dirs, temp_dir) != 0;
801 g_free (temp_dir);
803 if (!found)
804 break;
806 g_free (tmp);
809 g_free (directory);
810 directory = tmp;
812 if (verbose){
813 char buffer [BUF_SMALL];
815 g_snprintf (buffer, sizeof (buffer), _("Searching %s"),
816 str_trunc (directory, FIND2_X_USE));
817 status_update (buffer);
819 /* mc_stat should not be called after mc_opendir
820 because vfs_s_opendir modifies the st_nlink
822 if (!mc_stat (directory, &tmp_stat))
823 subdirs_left = tmp_stat.st_nlink - 2;
824 else
825 subdirs_left = 0;
827 dirp = mc_opendir (directory);
828 } /* while (!dirp) */
830 /* skip invalid filenames */
831 while ((dp = mc_readdir (dirp)) != NULL
832 && !str_is_valid_string (dp->d_name))
834 } /* while (!dp) */
836 if (strcmp (dp->d_name, ".") == 0 ||
837 strcmp (dp->d_name, "..") == 0){
838 dp = mc_readdir (dirp);
839 /* skip invalid filenames */
840 while (dp != NULL && !str_is_valid_string (dp->d_name))
841 dp = mc_readdir (dirp);
843 mc_search_free(search_file_handle);
844 search_file_handle = NULL;
845 mc_search_free(search_content_handle);
846 search_content_handle = NULL;
847 return 1;
850 if (!(skip_hidden_flag && (dp->d_name[0] == '.'))) {
851 gboolean search_ok;
853 if ((subdirs_left != 0) && find_recurs_flag
854 && (directory != NULL)) { /* Can directory be NULL ? */
855 char *tmp_name = concat_dir_and_file (directory, dp->d_name);
856 if (!mc_lstat (tmp_name, &tmp_stat)
857 && S_ISDIR (tmp_stat.st_mode)) {
858 push_directory (tmp_name);
859 subdirs_left--;
860 } else
861 g_free (tmp_name);
864 search_ok = mc_search_run (search_file_handle, dp->d_name,
865 0, strlen (dp->d_name), &bytes_found);
867 if (search_ok) {
868 if (content_pattern == NULL)
869 find_add_match (directory, dp->d_name);
870 else if (search_content (h, directory, dp->d_name)) {
871 mc_search_free(search_file_handle);
872 search_file_handle = NULL;
873 mc_search_free(search_content_handle);
874 search_content_handle = NULL;
875 return 1;
880 /* skip invalid filenames */
881 while ((dp = mc_readdir (dirp)) != NULL
882 && !str_is_valid_string (dp->d_name))
885 /* Displays the nice dot */
886 count++;
887 if (!(count & 31)){
888 /* For nice updating */
889 const char rotating_dash[] = "|/-\\";
891 if (verbose){
892 pos = (pos + 1) % 4;
893 tty_setcolor (DLG_NORMALC (h));
894 dlg_move (h, FIND2_Y - 7, FIND2_X - 4);
895 tty_print_char (rotating_dash [pos]);
896 mc_refresh ();
898 } else
899 goto do_search_begin;
901 mc_search_free (search_file_handle);
902 search_file_handle = NULL;
903 mc_search_free (search_content_handle);
904 search_content_handle = NULL;
905 return 1;
908 static void
909 init_find_vars (void)
911 g_free (old_dir);
912 old_dir = NULL;
913 matches = 0;
915 /* Remove all the items in the stack */
916 clear_stack ();
919 static char *
920 make_fullname (const char *dirname, const char *filename)
923 if (strcmp(dirname, ".") == 0 || strcmp(dirname, "."PATH_SEP_STR) == 0)
924 return g_strdup (filename);
925 if (strncmp(dirname, "."PATH_SEP_STR, 2) == 0)
926 return concat_dir_and_file (dirname + 2, filename);
927 return concat_dir_and_file (dirname, filename);
930 static void
931 find_do_view_edit (int unparsed_view, int edit, char *dir, char *file)
933 char *fullname = NULL;
934 const char *filename = NULL;
935 int line;
937 if (content_pattern != NULL) {
938 filename = strchr (file + 4, ':') + 1;
939 line = atoi (file + 4);
940 } else {
941 filename = file + 4;
942 line = 0;
945 fullname = make_fullname (dir, filename);
946 if (edit)
947 do_edit_at_line (fullname, line);
948 else
949 view_file_at_line (fullname, unparsed_view, use_internal_view, line);
950 g_free (fullname);
953 static cb_ret_t
954 view_edit_currently_selected_file (int unparsed_view, int edit)
956 WLEntry *entry = find_list->current;
957 char *dir = NULL;
959 if (!entry)
960 return MSG_NOT_HANDLED;
962 dir = entry->data;
964 if (!entry->text || !dir)
965 return MSG_NOT_HANDLED;
967 find_do_view_edit (unparsed_view, edit, dir, entry->text);
968 return MSG_HANDLED;
971 static cb_ret_t
972 find_callback (struct Dlg_head *h, dlg_msg_t msg, int parm)
974 switch (msg) {
975 case DLG_KEY:
976 if (parm == KEY_F (3) || parm == KEY_F (13)) {
977 int unparsed_view = (parm == KEY_F (13));
978 return view_edit_currently_selected_file (unparsed_view, 0);
980 if (parm == KEY_F (4)) {
981 return view_edit_currently_selected_file (0, 1);
983 return MSG_NOT_HANDLED;
985 case DLG_IDLE:
986 do_search (h);
987 return MSG_HANDLED;
989 default:
990 return default_dlg_callback (h, msg, parm);
994 /* Handles the Stop/Start button in the find window */
995 static int
996 start_stop (int button)
998 (void) button;
1000 running = is_start;
1001 set_idle_proc (find_dlg, running);
1002 is_start = !is_start;
1004 status_update (is_start ? _("Stopped") : _("Searching"));
1005 button_set_text (stop_button, fbuts [is_start ? 1 : 0].text);
1007 return 0;
1010 /* Handle view command, when invoked as a button */
1011 static int
1012 find_do_view_file (int button)
1014 (void) button;
1016 view_edit_currently_selected_file (0, 0);
1017 return 0;
1020 /* Handle edit command, when invoked as a button */
1021 static int
1022 find_do_edit_file (int button)
1024 (void) button;
1026 view_edit_currently_selected_file (0, 1);
1027 return 0;
1030 static void
1031 setup_gui (void)
1033 #ifdef ENABLE_NLS
1034 static gboolean i18n_flag = FALSE;
1036 if (!i18n_flag) {
1037 int i = sizeof (fbuts) / sizeof (fbuts[0]);
1038 while (i-- != 0) {
1039 fbuts[i].text = _(fbuts[i].text);
1040 fbuts[i].len = str_term_width1 (fbuts[i].text) + 3;
1043 fbuts[2].len += 2; /* DEFPUSH_BUTTON */
1044 i18n_flag = TRUE;
1046 #endif /* ENABLE_NLS */
1049 * Dynamically place buttons centered within current window size
1052 int l0 = max (fbuts[0].len, fbuts[1].len);
1053 int l1 = fbuts[2].len + fbuts[3].len + l0 + fbuts[4].len;
1054 int l2 = fbuts[5].len + fbuts[6].len + fbuts[7].len;
1055 int r1, r2;
1057 /* Check, if both button rows fit within FIND2_X */
1058 FIND2_X = max (l1 + 9, COLS - 16);
1059 FIND2_X = max (l2 + 8, FIND2_X);
1061 /* compute amount of space between buttons for each row */
1062 r1 = (FIND2_X - 4 - l1) % 5;
1063 l1 = (FIND2_X - 4 - l1) / 5;
1064 r2 = (FIND2_X - 4 - l2) % 4;
1065 l2 = (FIND2_X - 4 - l2) / 4;
1067 /* ...and finally, place buttons */
1068 fbuts[2].x = 2 + r1 / 2 + l1;
1069 fbuts[3].x = fbuts[2].x + fbuts[2].len + l1;
1070 fbuts[0].x = fbuts[3].x + fbuts[3].len + l1;
1071 fbuts[4].x = fbuts[0].x + l0 + l1;
1072 fbuts[5].x = 2 + r2 / 2 + l2;
1073 fbuts[6].x = fbuts[5].x + fbuts[5].len + l2;
1074 fbuts[7].x = fbuts[6].x + fbuts[6].len + l2;
1077 find_dlg =
1078 create_dlg (0, 0, FIND2_Y, FIND2_X, dialog_colors, find_callback,
1079 "[Find File]", _("Find File"), DLG_CENTER | DLG_REVERSE);
1081 add_widget (find_dlg,
1082 button_new (FIND2_Y - 3, fbuts[7].x, B_VIEW, NORMAL_BUTTON,
1083 fbuts[7].text, find_do_edit_file));
1084 add_widget (find_dlg,
1085 button_new (FIND2_Y - 3, fbuts[6].x, B_VIEW, NORMAL_BUTTON,
1086 fbuts[6].text, find_do_view_file));
1087 add_widget (find_dlg,
1088 button_new (FIND2_Y - 3, fbuts[5].x, B_PANELIZE,
1089 NORMAL_BUTTON, fbuts[5].text, 0));
1091 add_widget (find_dlg,
1092 button_new (FIND2_Y - 4, fbuts[4].x, B_CANCEL,
1093 NORMAL_BUTTON, fbuts[4].text, 0));
1094 stop_button =
1095 button_new (FIND2_Y - 4, fbuts[0].x, B_STOP, NORMAL_BUTTON,
1096 fbuts[0].text, start_stop);
1097 add_widget (find_dlg, stop_button);
1098 add_widget (find_dlg,
1099 button_new (FIND2_Y - 4, fbuts[3].x, B_AGAIN,
1100 NORMAL_BUTTON, fbuts[3].text, 0));
1101 add_widget (find_dlg,
1102 button_new (FIND2_Y - 4, fbuts[2].x, B_ENTER,
1103 DEFPUSH_BUTTON, fbuts[2].text, 0));
1105 status_label = label_new (FIND2_Y - 7, 4, _("Searching"));
1106 add_widget (find_dlg, status_label);
1108 found_num_label = label_new (FIND2_Y - 6, 4, "");
1109 add_widget (find_dlg, found_num_label);
1111 find_list =
1112 listbox_new (2, 2, FIND2_Y - 10, FIND2_X - 4, NULL);
1113 add_widget (find_dlg, find_list);
1116 static int
1117 run_process (void)
1119 resuming = 0;
1120 set_idle_proc (find_dlg, 1);
1121 return run_dlg (find_dlg);
1124 static void
1125 kill_gui (void)
1127 set_idle_proc (find_dlg, 0);
1128 destroy_dlg (find_dlg);
1131 static int
1132 find_file (const char *start_dir, const char *pattern, const char *content,
1133 char **dirname, char **filename)
1135 int return_value = 0;
1136 char *dir_tmp = NULL, *file_tmp = NULL;
1138 setup_gui ();
1140 /* FIXME: Need to cleanup this, this ought to be passed non-globaly */
1141 find_pattern = str_unconst (pattern);
1142 content_pattern = (content != NULL && str_is_valid_string (content))
1143 ? g_strdup(content)
1144 : NULL;
1146 init_find_vars ();
1147 push_directory (start_dir);
1149 return_value = run_process ();
1151 /* Remove all the items in the stack */
1152 clear_stack ();
1154 get_list_info (&file_tmp, &dir_tmp);
1156 if (dir_tmp)
1157 *dirname = g_strdup (dir_tmp);
1158 if (file_tmp)
1159 *filename = g_strdup (file_tmp);
1161 if (return_value == B_PANELIZE && *filename) {
1162 int status, link_to_dir, stale_link;
1163 int next_free = 0;
1164 int i;
1165 struct stat st;
1166 WLEntry *entry = find_list->list;
1167 dir_list *list = &current_panel->dir;
1168 char *name = NULL;
1170 for (i = 0; entry != NULL && i < find_list->count;
1171 entry = entry->next, i++) {
1172 const char *filename = NULL;
1174 if (!entry->text || !entry->data)
1175 continue;
1177 if (content_pattern != NULL)
1178 filename = strchr (entry->text + 4, ':') + 1;
1179 else
1180 filename = entry->text + 4;
1182 name = make_fullname (entry->data, filename);
1183 status =
1184 handle_path (list, name, &st, next_free, &link_to_dir,
1185 &stale_link);
1186 if (status == 0) {
1187 g_free (name);
1188 continue;
1190 if (status == -1) {
1191 g_free (name);
1192 break;
1195 /* don't add files more than once to the panel */
1196 if (content_pattern != NULL && next_free > 0
1197 && strcmp (list->list[next_free - 1].fname, name) == 0) {
1198 g_free (name);
1199 continue;
1202 if (!next_free) /* first turn i.e clean old list */
1203 panel_clean_dir (current_panel);
1204 list->list[next_free].fnamelen = strlen (name);
1205 list->list[next_free].fname = name;
1206 list->list[next_free].f.marked = 0;
1207 list->list[next_free].f.link_to_dir = link_to_dir;
1208 list->list[next_free].f.stale_link = stale_link;
1209 list->list[next_free].f.dir_size_computed = 0;
1210 list->list[next_free].st = st;
1211 list->list[next_free].sort_key = NULL;
1212 list->list[next_free].second_sort_key = NULL;
1213 next_free++;
1214 if (!(next_free & 15))
1215 rotate_dash ();
1218 if (next_free) {
1219 current_panel->count = next_free;
1220 current_panel->is_panelized = 1;
1222 if (start_dir[0] == PATH_SEP) {
1223 strcpy (current_panel->cwd, PATH_SEP_STR);
1224 chdir (PATH_SEP_STR);
1229 g_free (content_pattern);
1230 kill_gui ();
1231 do_search (NULL); /* force do_search to release resources */
1232 g_free (old_dir);
1233 old_dir = NULL;
1235 return return_value;
1238 void
1239 do_find (void)
1241 char *start_dir = NULL, *pattern = NULL, *content = NULL;
1242 char *filename = NULL, *dirname = NULL;
1243 int v;
1244 gboolean dir_and_file_set;
1246 while (find_parameters (&start_dir, &pattern, &content)){
1247 if (pattern [0] == '\0')
1248 break; /* nothing search*/
1250 dirname = filename = NULL;
1251 is_start = FALSE;
1252 v = find_file (start_dir, pattern, content, &dirname, &filename);
1253 g_free (pattern);
1255 if (v == B_ENTER){
1256 if (dirname || filename){
1257 if (dirname){
1258 do_cd (dirname, cd_exact);
1259 if (filename)
1260 try_to_select (current_panel, filename + (content ?
1261 (strchr (filename + 4, ':') - filename + 1) : 4) );
1262 } else if (filename)
1263 do_cd (filename, cd_exact);
1264 select_item (current_panel);
1266 g_free (dirname);
1267 g_free (filename);
1268 break;
1271 g_free (content);
1272 dir_and_file_set = dirname && filename;
1273 g_free (dirname);
1274 g_free (filename);
1276 if (v == B_CANCEL)
1277 break;
1279 if (v == B_PANELIZE){
1280 if (dir_and_file_set){
1281 try_to_select (current_panel, NULL);
1282 panel_re_sort (current_panel);
1283 try_to_select (current_panel, NULL);
1285 break;