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
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. */
23 * \brief Source: Find file command
37 #include "../src/tty/tty.h"
38 #include "../src/skin/skin.h"
39 #include "../src/tty/key.h"
41 #include "../src/search/search.h"
49 #include "panel.h" /* current_panel */
50 #include "main.h" /* do_cd, try_to_select */
52 #include "cmd.h" /* view_file_at_line */
54 #include "history.h" /* MC_HISTORY_SHARED_SEARCH */
55 #include "layout.h" /* mc_refresh() */
57 /* Size of the find parameters window */
59 static int FIND_Y
= 16;
61 static int FIND_Y
= 15;
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 */
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 */
102 static WCheck
*file_all_charsets_cbox
;
103 static WCheck
*content_all_charsets_cbox
;
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 */
117 static int last_line
;
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
;
130 typedef struct dir_stack
{
132 struct dir_stack
*prev
;
135 static dir_stack
*dir_stack_base
= 0;
136 #endif /* GLIB_CHECK_VERSION */
140 int len
; /* length including space and brackets */
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
;
170 add_to_list (const char *text
, void *data
)
172 return listbox_add_item (find_list
, LISTBOX_APPEND_AT_END
, 0, text
, data
);
176 stop_idle (void *data
)
178 set_idle_proc (data
, 0);
182 status_update (const char *text
)
184 label_set_text (status_label
, text
);
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
);
196 get_list_info (char **file
, char **dir
)
198 listbox_get_current (find_list
, file
, dir
);
201 /* check regular expression */
203 find_check_regexp (const char *r
)
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
);
220 * Callback for the parameter dialog.
221 * Validate regex, prevent closing the dialog if it's invalid.
224 find_parm_callback (struct Dlg_head
*h
, dlg_msg_t msg
, int parm
)
228 if (h
->ret_value
!= B_ENTER
)
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 */
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 */
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.
272 find_parameters (char **start_dir
, char **pattern
, char **content
)
275 char *temp_dir
= NULL
;
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");
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") };
297 int i
= sizeof (buts
) / sizeof (buts
[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;
319 if (in_start_dir
== NULL
)
320 in_start_dir
= g_strdup (".");
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));
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
);
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
);
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
);
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
)) {
395 file_all_charsets_flag
= file_all_charsets_cbox
->state
& C_BOOL
;
396 content_all_charsets_flag
= content_all_charsets_cbox
->state
& C_BOOL
;
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')) {
412 temp_dir
= g_strdup (current_panel
->cwd
);
414 in_start_dir
= tree_box (temp_dir
);
415 if (in_start_dir
!= NULL
)
418 in_start_dir
= temp_dir
;
419 /* Warning: Dreadful goto */
425 file_all_charsets_flag
= file_all_charsets_cbox
->state
& C_BOOL
;
426 content_all_charsets_flag
= content_all_charsets_cbox
->state
& C_BOOL
;
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
;
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
);
450 #if GLIB_CHECK_VERSION (2, 14, 0)
453 push_directory (const char *dir
)
455 g_queue_push_head (&dir_queue
, (void *) dir
);
461 return (char *) g_queue_pop_tail (&dir_queue
);
464 /* Remove all the items in the stack */
468 g_queue_foreach (&dir_queue
, (GFunc
) g_free
, NULL
);
469 g_queue_clear (&dir_queue
);
472 #else /* GLIB_CHAECK_VERSION */
475 push_directory (const char *dir
)
479 new = g_new (dir_stack
, 1);
480 new->name
= str_unconst (dir
);
481 new->prev
= dir_stack_base
;
482 dir_stack_base
= new;
490 if (dir_stack_base
!= NULL
) {
492 name
= dir_stack_base
->name
;
493 next
= dir_stack_base
->prev
;
494 g_free (dir_stack_base
);
495 dir_stack_base
= next
;
501 /* Remove all the items in the stack */
506 while ((dir
= pop_directory ()) != NULL
)
510 #endif /* GLIB_CHAECK_VERSION */
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
)
522 if (strcmp (old_dir
, dir
)){
524 old_dir
= g_strdup (dir
);
525 dirname
= add_to_list (dir
, NULL
);
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
);
538 find_add_match (const char *dir
, const char *file
)
540 insert_file (dir
, file
);
544 listbox_select_by_number (find_list
, 0);
545 send_message (&find_list
->widget
, WIDGET_DRAW
, 0);
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 ?
561 get_line_at (int file_fd
, char *buf
, int buf_size
, int *pos
, int *n_read
,
562 gboolean
*has_newline
)
570 if (*pos
>= *n_read
) {
572 *n_read
= mc_read (file_fd
, buf
, buf_size
);
579 /* skip possible leading zero(s) */
586 if (i
>= buffer_size
- 1) {
587 buffer
= g_realloc (buffer
, buffer_size
+= 80);
596 *has_newline
= (ch
!= '\0');
604 static FindProgressStatus
605 check_find_events(Dlg_head
*h
)
611 c
= tty_get_event (&event
, h
->mouse_status
== MOU_REPEAT
, FALSE
);
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 */
621 if (!(h
->flags
& DLG_WANT_IDLE
)) {
622 /* searching suspended */
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
640 search_content (Dlg_head
*h
, const char *directory
, const char *filename
)
643 char buffer
[BUF_4K
];
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
)){
655 file_fd
= mc_open (fname
, O_RDONLY
);
661 g_snprintf (buffer
, sizeof (buffer
), _("Grepping in %s"), str_trunc (filename
, FIND2_X_USE
));
663 status_update (buffer
);
666 tty_enable_interrupt_key ();
667 tty_got_interrupt ();
673 gboolean has_newline
;
675 gboolean found
= FALSE
;
677 char result
[BUF_MEDIUM
];
680 /* We've been previously suspended, start from the previous position */
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
);
697 if (found
&& content_first_hit_flag
)
705 if ((line
& 0xff) == 0) {
706 FindProgressStatus res
;
707 res
= check_find_events(h
);
726 tty_disable_interrupt_key ();
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
;
739 static int subdirs_left
= 0;
741 unsigned long count
; /* Number of files displayed */
743 if (!h
) { /* someone forces me to close dirp */
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
;
779 tty_setcolor (REVERSE_COLOR
);
781 char *temp_dir
= NULL
;
784 tmp
= pop_directory ();
787 status_update (_("Finished"));
789 mc_search_free (search_file_handle
);
790 search_file_handle
= NULL
;
791 mc_search_free (search_content_handle
);
792 search_content_handle
= NULL
;
796 if ((find_ignore_dirs
== NULL
) || (find_ignore_dirs
[0] == '\0'))
799 temp_dir
= g_strdup_printf (":%s:", tmp
);
800 found
= strstr (find_ignore_dirs
, temp_dir
) != 0;
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;
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
))
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
;
850 if (!(skip_hidden_flag
&& (dp
->d_name
[0] == '.'))) {
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
);
864 search_ok
= mc_search_run (search_file_handle
, dp
->d_name
,
865 0, strlen (dp
->d_name
), &bytes_found
);
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
;
880 /* skip invalid filenames */
881 while ((dp
= mc_readdir (dirp
)) != NULL
882 && !str_is_valid_string (dp
->d_name
))
885 /* Displays the nice dot */
888 /* For nice updating */
889 const char rotating_dash
[] = "|/-\\";
893 tty_setcolor (DLG_NORMALC (h
));
894 dlg_move (h
, FIND2_Y
- 7, FIND2_X
- 4);
895 tty_print_char (rotating_dash
[pos
]);
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
;
909 init_find_vars (void)
915 /* Remove all the items in the stack */
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
);
931 find_do_view_edit (int unparsed_view
, int edit
, char *dir
, char *file
)
933 char *fullname
= NULL
;
934 const char *filename
= NULL
;
937 if (content_pattern
!= NULL
) {
938 filename
= strchr (file
+ 4, ':') + 1;
939 line
= atoi (file
+ 4);
945 fullname
= make_fullname (dir
, filename
);
947 do_edit_at_line (fullname
, line
);
949 view_file_at_line (fullname
, unparsed_view
, use_internal_view
, line
);
954 view_edit_currently_selected_file (int unparsed_view
, int edit
)
956 WLEntry
*entry
= find_list
->current
;
960 return MSG_NOT_HANDLED
;
964 if (!entry
->text
|| !dir
)
965 return MSG_NOT_HANDLED
;
967 find_do_view_edit (unparsed_view
, edit
, dir
, entry
->text
);
972 find_callback (struct Dlg_head
*h
, dlg_msg_t msg
, int parm
)
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
;
990 return default_dlg_callback (h
, msg
, parm
);
994 /* Handles the Stop/Start button in the find window */
996 start_stop (int button
)
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
);
1010 /* Handle view command, when invoked as a button */
1012 find_do_view_file (int button
)
1016 view_edit_currently_selected_file (0, 0);
1020 /* Handle edit command, when invoked as a button */
1022 find_do_edit_file (int button
)
1026 view_edit_currently_selected_file (0, 1);
1034 static gboolean i18n_flag
= FALSE
;
1037 int i
= sizeof (fbuts
) / sizeof (fbuts
[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 */
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
;
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
;
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));
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
);
1112 listbox_new (2, 2, FIND2_Y
- 10, FIND2_X
- 4, NULL
);
1113 add_widget (find_dlg
, find_list
);
1120 set_idle_proc (find_dlg
, 1);
1121 return run_dlg (find_dlg
);
1127 set_idle_proc (find_dlg
, 0);
1128 destroy_dlg (find_dlg
);
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
;
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
))
1147 push_directory (start_dir
);
1149 return_value
= run_process ();
1151 /* Remove all the items in the stack */
1154 get_list_info (&file_tmp
, &dir_tmp
);
1157 *dirname
= g_strdup (dir_tmp
);
1159 *filename
= g_strdup (file_tmp
);
1161 if (return_value
== B_PANELIZE
&& *filename
) {
1162 int status
, link_to_dir
, stale_link
;
1166 WLEntry
*entry
= find_list
->list
;
1167 dir_list
*list
= ¤t_panel
->dir
;
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
)
1177 if (content_pattern
!= NULL
)
1178 filename
= strchr (entry
->text
+ 4, ':') + 1;
1180 filename
= entry
->text
+ 4;
1182 name
= make_fullname (entry
->data
, filename
);
1184 handle_path (list
, name
, &st
, next_free
, &link_to_dir
,
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) {
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
;
1214 if (!(next_free
& 15))
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
);
1231 do_search (NULL
); /* force do_search to release resources */
1235 return return_value
;
1241 char *start_dir
= NULL
, *pattern
= NULL
, *content
= NULL
;
1242 char *filename
= NULL
, *dirname
= NULL
;
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
;
1252 v
= find_file (start_dir
, pattern
, content
, &dirname
, &filename
);
1256 if (dirname
|| filename
){
1258 do_cd (dirname
, cd_exact
);
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
);
1272 dir_and_file_set
= dirname
&& filename
;
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
);