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 (Dlg_head
*h
, Widget
*sender
,
225 dlg_msg_t msg
, int parm
, void *data
)
229 if (h
->ret_value
!= B_ENTER
)
232 /* check filename regexp */
233 if (!(file_pattern_cbox
->state
& C_BOOL
)
234 && (in_name
->buffer
[0] != '\0')
235 && !find_check_regexp (in_name
->buffer
)) {
236 message (D_ERROR
, MSG_ERROR
, _(" Malformed regular expression "));
237 dlg_select_widget (in_name
);
238 h
->running
= 1; /* Don't stop the dialog */
242 /* check content regexp */
243 if ((content_regexp_cbox
->state
& C_BOOL
)
244 && (in_with
->buffer
[0] != '\0')
245 && !find_check_regexp (in_with
->buffer
)) {
246 message (D_ERROR
, MSG_ERROR
, _(" Malformed regular expression "));
247 dlg_select_widget (in_with
);
248 h
->running
= 1; /* Don't stop the dialog */
255 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
260 * find_parameters: gets information from the user
262 * If the return value is true, then the following holds:
264 * START_DIR and PATTERN are pointers to char * and upon return they
265 * contain the information provided by the user.
267 * CONTENT holds a strdup of the contents specified by the user if he
268 * asked for them or 0 if not (note, this is different from the
269 * behavior for the other two parameters.
273 find_parameters (char **start_dir
, char **pattern
, char **content
)
276 char *temp_dir
= NULL
;
279 const char *file_case_label
= N_("Cas&e sensitive");
280 const char *file_pattern_label
= N_("&Using shell patterns");
281 const char *file_recurs_label
= N_("&Find recursively");
282 const char *file_skip_hidden_label
= N_("S&kip hidden");
284 const char *file_all_charsets_label
= N_("&All charsets");
288 const char *content_case_label
= N_("Case sens&itive");
289 const char *content_regexp_label
= N_("Re&gular expression");
290 const char *content_first_hit_label
= N_("Fir&st hit");
291 const char *content_whole_words_label
= N_("&Whole words");
293 const char *content_all_charsets_label
= N_("All cha&rsets");
296 const char *buts
[] = { N_("&OK"), N_("&Cancel"), N_("&Tree") };
302 int i
= sizeof (buts
) / sizeof (buts
[0]);
304 buts
[i
] = _(buts
[i
]);
306 file_case_label
= _(file_case_label
);
307 file_pattern_label
= _(file_pattern_label
);
308 file_recurs_label
= _(file_recurs_label
);
309 file_skip_hidden_label
= _(file_skip_hidden_label
);
311 file_all_charsets_label
= _(file_all_charsets_label
);
312 content_all_charsets_label
= _(content_all_charsets_label
);
314 content_case_label
= _(content_case_label
);
315 content_regexp_label
= _(content_regexp_label
);
316 content_first_hit_label
= _(content_first_hit_label
);
317 content_whole_words_label
= _(content_whole_words_label
);
319 #endif /* ENABLE_NLS */
321 b0
= str_term_width1 (buts
[0]) + 6; /* default button */
322 b1
= str_term_width1 (buts
[1]) + 4;
323 b2
= str_term_width1 (buts
[2]) + 4;
326 if (in_start_dir
== NULL
)
327 in_start_dir
= g_strdup (".");
330 create_dlg (0, 0, FIND_Y
, FIND_X
, dialog_colors
,
331 find_parm_callback
, "[Find File]", _("Find File"),
332 DLG_CENTER
| DLG_REVERSE
);
334 add_widget (find_dlg
,
335 button_new (FIND_Y
- 3, FIND_X
* 3/4 - b1
/2, B_CANCEL
, NORMAL_BUTTON
, buts
[1], 0));
336 add_widget (find_dlg
,
337 button_new (FIND_Y
- 3, FIND_X
/4 - b0
/2, B_ENTER
, DEFPUSH_BUTTON
, buts
[0], 0));
340 content_all_charsets_cbox
= check_new (11, FIND_X
/ 2 + 1,
341 content_all_charsets_flag
, content_all_charsets_label
);
342 add_widget (find_dlg
, content_all_charsets_cbox
);
345 content_whole_words_cbox
= check_new (10, FIND_X
/ 2 + 1, content_whole_words
, content_whole_words_label
);
346 add_widget (find_dlg
, content_whole_words_cbox
);
348 content_first_hit_cbox
= check_new (9, FIND_X
/ 2 + 1, content_first_hit_flag
, content_first_hit_label
);
349 add_widget (find_dlg
, content_first_hit_cbox
);
351 content_regexp_cbox
= check_new (8, FIND_X
/ 2 + 1, content_regexp_flag
, content_regexp_label
);
352 add_widget (find_dlg
, content_regexp_cbox
);
354 content_case_sens_cbox
= check_new (7, FIND_X
/ 2 + 1, content_case_sens_flag
, content_case_label
);
355 add_widget (find_dlg
, content_case_sens_cbox
);
358 file_all_charsets_cbox
= check_new (11, 3,
359 file_all_charsets_flag
, file_all_charsets_label
);
360 add_widget (find_dlg
, file_all_charsets_cbox
);
363 skip_hidden_cbox
= check_new (10, 3, skip_hidden_flag
, file_skip_hidden_label
);
364 add_widget (find_dlg
, skip_hidden_cbox
);
366 recursively_cbox
= check_new (9, 3, find_recurs_flag
, file_recurs_label
);
367 add_widget (find_dlg
, recursively_cbox
);
369 file_pattern_cbox
= check_new (8, 3, file_pattern_flag
, file_pattern_label
);
370 add_widget (find_dlg
, file_pattern_cbox
);
372 file_case_sens_cbox
= check_new (7, 3, file_case_sens_flag
, file_case_label
);
373 add_widget (find_dlg
, file_case_sens_cbox
);
375 in_with
= input_new (6, FIND_X
/ 2 + 1, INPUT_COLOR
, FIND_X
/ 2 - 4, INPUT_LAST_TEXT
,
376 MC_HISTORY_SHARED_SEARCH
, INPUT_COMPLETE_DEFAULT
);
377 add_widget (find_dlg
, in_with
);
378 add_widget (find_dlg
, label_new (5, FIND_X
/ 2 + 1, _("Content:")));
380 in_name
= input_new (6, 3, INPUT_COLOR
, FIND_X
/ 2 - 4, INPUT_LAST_TEXT
, "name",
381 INPUT_COMPLETE_DEFAULT
);
382 add_widget (find_dlg
, in_name
);
383 add_widget (find_dlg
, label_new (5, 3, _("File name:")));
385 add_widget (find_dlg
,
386 button_new (3, FIND_X
- b2
- 2, B_TREE
, NORMAL_BUTTON
, buts
[2], 0));
388 in_start
= input_new (3, 3, INPUT_COLOR
, FIND_X
- b2
- 6, in_start_dir
, "start",
389 INPUT_COMPLETE_DEFAULT
);
390 add_widget (find_dlg
, in_start
);
391 add_widget (find_dlg
, label_new (2, 3, _("Start at:")));
393 dlg_select_widget (in_name
);
395 switch (run_dlg (find_dlg
)) {
402 file_all_charsets_flag
= file_all_charsets_cbox
->state
& C_BOOL
;
403 content_all_charsets_flag
= content_all_charsets_cbox
->state
& C_BOOL
;
405 content_case_sens_flag
= content_case_sens_cbox
->state
& C_BOOL
;
406 content_regexp_flag
= content_regexp_cbox
->state
& C_BOOL
;
407 content_first_hit_flag
= content_first_hit_cbox
->state
& C_BOOL
;
408 content_whole_words
= content_whole_words_cbox
->state
& C_BOOL
;
409 file_pattern_flag
= file_pattern_cbox
->state
& C_BOOL
;
410 file_case_sens_flag
= file_case_sens_cbox
->state
& C_BOOL
;
411 find_recurs_flag
= recursively_cbox
->state
& C_BOOL
;
412 skip_hidden_flag
= skip_hidden_cbox
->state
& C_BOOL
;
414 destroy_dlg (find_dlg
);
415 if (in_start_dir
!= INPUT_LAST_TEXT
)
416 g_free (in_start_dir
);
417 temp_dir
= g_strdup (in_start
->buffer
);
418 if ((temp_dir
[0] == '.') && (temp_dir
[1] == '\0')) {
420 temp_dir
= g_strdup (current_panel
->cwd
);
422 in_start_dir
= tree_box (temp_dir
);
423 if (in_start_dir
!= NULL
)
426 in_start_dir
= temp_dir
;
427 /* Warning: Dreadful goto */
433 file_all_charsets_flag
= file_all_charsets_cbox
->state
& C_BOOL
;
434 content_all_charsets_flag
= content_all_charsets_cbox
->state
& C_BOOL
;
436 content_case_sens_flag
= content_case_sens_cbox
->state
& C_BOOL
;
437 content_regexp_flag
= content_regexp_cbox
->state
& C_BOOL
;
438 content_first_hit_flag
= content_first_hit_cbox
->state
& C_BOOL
;
439 content_whole_words
= content_whole_words_cbox
->state
& C_BOOL
;
440 find_recurs_flag
= recursively_cbox
->state
& C_BOOL
;
441 file_pattern_flag
= file_pattern_cbox
->state
& C_BOOL
;
442 file_case_sens_flag
= file_case_sens_cbox
->state
& C_BOOL
;
443 skip_hidden_flag
= skip_hidden_cbox
->state
& C_BOOL
;
445 *content
= (in_with
->buffer
[0] != '\0') ? g_strdup (in_with
->buffer
) : NULL
;
446 *start_dir
= g_strdup ((in_start
->buffer
[0] != '\0') ? in_start
->buffer
: ".");
447 *pattern
= g_strdup (in_name
->buffer
);
448 if (in_start_dir
!= INPUT_LAST_TEXT
)
449 g_free (in_start_dir
);
450 in_start_dir
= g_strdup (*start_dir
);
454 destroy_dlg (find_dlg
);
459 #if GLIB_CHECK_VERSION (2, 14, 0)
462 push_directory (const char *dir
)
464 g_queue_push_head (&dir_queue
, (void *) dir
);
470 return (char *) g_queue_pop_tail (&dir_queue
);
473 /* Remove all the items in the stack */
477 g_queue_foreach (&dir_queue
, (GFunc
) g_free
, NULL
);
478 g_queue_clear (&dir_queue
);
481 #else /* GLIB_CHAECK_VERSION */
484 push_directory (const char *dir
)
488 new = g_new (dir_stack
, 1);
489 new->name
= str_unconst (dir
);
490 new->prev
= dir_stack_base
;
491 dir_stack_base
= new;
499 if (dir_stack_base
!= NULL
) {
501 name
= dir_stack_base
->name
;
502 next
= dir_stack_base
->prev
;
503 g_free (dir_stack_base
);
504 dir_stack_base
= next
;
510 /* Remove all the items in the stack */
515 while ((dir
= pop_directory ()) != NULL
)
519 #endif /* GLIB_CHAECK_VERSION */
522 insert_file (const char *dir
, const char *file
)
524 char *tmp_name
= NULL
;
525 static char *dirname
= NULL
;
527 while (dir
[0] == PATH_SEP
&& dir
[1] == PATH_SEP
)
531 if (strcmp (old_dir
, dir
)){
533 old_dir
= g_strdup (dir
);
534 dirname
= add_to_list (dir
, NULL
);
537 old_dir
= g_strdup (dir
);
538 dirname
= add_to_list (dir
, NULL
);
541 tmp_name
= g_strdup_printf (" %s", file
);
542 add_to_list (tmp_name
, dirname
);
547 find_add_match (const char *dir
, const char *file
)
549 insert_file (dir
, file
);
553 listbox_select_by_number (find_list
, 0);
554 send_message (&find_list
->widget
, WIDGET_DRAW
, 0);
563 * Returns malloced null-terminated line from file file_fd.
564 * Input is buffered in buf_size long buffer.
565 * Current pos in buf is stored in pos.
566 * n_read - number of read chars.
567 * has_newline - is there newline ?
570 get_line_at (int file_fd
, char *buf
, int buf_size
, int *pos
, int *n_read
,
571 gboolean
*has_newline
)
579 if (*pos
>= *n_read
) {
581 *n_read
= mc_read (file_fd
, buf
, buf_size
);
588 /* skip possible leading zero(s) */
595 if (i
>= buffer_size
- 1) {
596 buffer
= g_realloc (buffer
, buffer_size
+= 80);
605 *has_newline
= (ch
!= '\0');
613 static FindProgressStatus
614 check_find_events(Dlg_head
*h
)
620 c
= tty_get_event (&event
, h
->mouse_status
== MOU_REPEAT
, FALSE
);
622 dlg_process_event (h
, c
, &event
);
623 if (h
->ret_value
== B_ENTER
624 || h
->ret_value
== B_CANCEL
625 || h
->ret_value
== B_AGAIN
626 || h
->ret_value
== B_PANELIZE
) {
627 /* dialog terminated */
630 if (!(h
->flags
& DLG_WANT_IDLE
)) {
631 /* searching suspended */
642 * Search the content_pattern string in the DIRECTORY/FILE.
643 * It will add the found entries to the find listbox.
645 * returns FALSE if do_search should look for another file
646 * TRUE if do_search should exit and proceed to the event handler
649 search_content (Dlg_head
*h
, const char *directory
, const char *filename
)
652 char buffer
[BUF_4K
];
655 gboolean ret_val
= FALSE
;
657 fname
= concat_dir_and_file (directory
, filename
);
659 if (mc_stat (fname
, &s
) != 0 || !S_ISREG (s
.st_mode
)){
664 file_fd
= mc_open (fname
, O_RDONLY
);
670 g_snprintf (buffer
, sizeof (buffer
), _("Grepping in %s"), str_trunc (filename
, FIND2_X_USE
));
672 status_update (buffer
);
675 tty_enable_interrupt_key ();
676 tty_got_interrupt ();
682 gboolean has_newline
;
684 gboolean found
= FALSE
;
686 char result
[BUF_MEDIUM
];
689 /* We've been previously suspended, start from the previous position */
695 && (p
= get_line_at (file_fd
, buffer
, sizeof (buffer
),
696 &pos
, &n_read
, &has_newline
)) != NULL
) {
697 if (!found
/* Search in binary line once */
698 && mc_search_run (search_content_handle
,
699 (const void *) p
, 0, strlen (p
), &found_len
)) {
700 g_snprintf (result
, sizeof (result
), "%d:%s", line
, filename
);
701 find_add_match (directory
, result
);
706 if (found
&& content_first_hit_flag
)
714 if ((line
& 0xff) == 0) {
715 FindProgressStatus res
;
716 res
= check_find_events(h
);
735 tty_disable_interrupt_key ();
741 do_search (struct Dlg_head
*h
)
743 static struct dirent
*dp
= NULL
;
744 static DIR *dirp
= NULL
;
745 static char *directory
= NULL
;
746 struct stat tmp_stat
;
748 static int subdirs_left
= 0;
750 unsigned long count
; /* Number of files displayed */
752 if (!h
) { /* someone forces me to close dirp */
763 search_content_handle
= mc_search_new(content_pattern
, -1);
764 if (search_content_handle
) {
765 search_content_handle
->search_type
= (content_regexp_flag
) ? MC_SEARCH_T_REGEX
: MC_SEARCH_T_NORMAL
;
766 search_content_handle
->is_case_sentitive
= content_case_sens_flag
;
767 search_content_handle
->whole_words
= content_whole_words
;
768 search_content_handle
->is_all_charsets
= content_all_charsets_flag
;
770 search_file_handle
= mc_search_new(find_pattern
, -1);
771 search_file_handle
->search_type
= (file_pattern_flag
) ? MC_SEARCH_T_GLOB
: MC_SEARCH_T_REGEX
;
772 search_file_handle
->is_case_sentitive
= file_case_sens_flag
;
773 search_file_handle
->is_all_charsets
= file_all_charsets_flag
;
774 search_file_handle
->is_entire_line
= file_pattern_flag
;
788 tty_setcolor (REVERSE_COLOR
);
790 char *temp_dir
= NULL
;
793 tmp
= pop_directory ();
796 status_update (_("Finished"));
798 mc_search_free (search_file_handle
);
799 search_file_handle
= NULL
;
800 mc_search_free (search_content_handle
);
801 search_content_handle
= NULL
;
805 if ((find_ignore_dirs
== NULL
) || (find_ignore_dirs
[0] == '\0'))
808 temp_dir
= g_strdup_printf (":%s:", tmp
);
809 found
= strstr (find_ignore_dirs
, temp_dir
) != 0;
822 char buffer
[BUF_SMALL
];
824 g_snprintf (buffer
, sizeof (buffer
), _("Searching %s"),
825 str_trunc (directory
, FIND2_X_USE
));
826 status_update (buffer
);
828 /* mc_stat should not be called after mc_opendir
829 because vfs_s_opendir modifies the st_nlink
831 if (!mc_stat (directory
, &tmp_stat
))
832 subdirs_left
= tmp_stat
.st_nlink
- 2;
836 dirp
= mc_opendir (directory
);
837 } /* while (!dirp) */
839 /* skip invalid filenames */
840 while ((dp
= mc_readdir (dirp
)) != NULL
841 && !str_is_valid_string (dp
->d_name
))
845 if (strcmp (dp
->d_name
, ".") == 0 ||
846 strcmp (dp
->d_name
, "..") == 0){
847 dp
= mc_readdir (dirp
);
848 /* skip invalid filenames */
849 while (dp
!= NULL
&& !str_is_valid_string (dp
->d_name
))
850 dp
= mc_readdir (dirp
);
852 mc_search_free(search_file_handle
);
853 search_file_handle
= NULL
;
854 mc_search_free(search_content_handle
);
855 search_content_handle
= NULL
;
859 if (!(skip_hidden_flag
&& (dp
->d_name
[0] == '.'))) {
862 if ((subdirs_left
!= 0) && find_recurs_flag
863 && (directory
!= NULL
)) { /* Can directory be NULL ? */
864 char *tmp_name
= concat_dir_and_file (directory
, dp
->d_name
);
865 if (!mc_lstat (tmp_name
, &tmp_stat
)
866 && S_ISDIR (tmp_stat
.st_mode
)) {
867 push_directory (tmp_name
);
873 search_ok
= mc_search_run (search_file_handle
, dp
->d_name
,
874 0, strlen (dp
->d_name
), &bytes_found
);
877 if (content_pattern
== NULL
)
878 find_add_match (directory
, dp
->d_name
);
879 else if (search_content (h
, directory
, dp
->d_name
)) {
880 mc_search_free(search_file_handle
);
881 search_file_handle
= NULL
;
882 mc_search_free(search_content_handle
);
883 search_content_handle
= NULL
;
889 /* skip invalid filenames */
890 while ((dp
= mc_readdir (dirp
)) != NULL
891 && !str_is_valid_string (dp
->d_name
))
894 /* Displays the nice dot */
897 /* For nice updating */
898 const char rotating_dash
[] = "|/-\\";
902 tty_setcolor (DLG_NORMALC (h
));
903 dlg_move (h
, FIND2_Y
- 7, FIND2_X
- 4);
904 tty_print_char (rotating_dash
[pos
]);
908 goto do_search_begin
;
910 mc_search_free (search_file_handle
);
911 search_file_handle
= NULL
;
912 mc_search_free (search_content_handle
);
913 search_content_handle
= NULL
;
918 init_find_vars (void)
924 /* Remove all the items in the stack */
929 make_fullname (const char *dirname
, const char *filename
)
932 if (strcmp(dirname
, ".") == 0 || strcmp(dirname
, "."PATH_SEP_STR
) == 0)
933 return g_strdup (filename
);
934 if (strncmp(dirname
, "."PATH_SEP_STR
, 2) == 0)
935 return concat_dir_and_file (dirname
+ 2, filename
);
936 return concat_dir_and_file (dirname
, filename
);
940 find_do_view_edit (int unparsed_view
, int edit
, char *dir
, char *file
)
942 char *fullname
= NULL
;
943 const char *filename
= NULL
;
946 if (content_pattern
!= NULL
) {
947 filename
= strchr (file
+ 4, ':') + 1;
948 line
= atoi (file
+ 4);
954 fullname
= make_fullname (dir
, filename
);
956 do_edit_at_line (fullname
, line
);
958 view_file_at_line (fullname
, unparsed_view
, use_internal_view
, line
);
963 view_edit_currently_selected_file (int unparsed_view
, int edit
)
965 WLEntry
*entry
= find_list
->current
;
969 return MSG_NOT_HANDLED
;
973 if (!entry
->text
|| !dir
)
974 return MSG_NOT_HANDLED
;
976 find_do_view_edit (unparsed_view
, edit
, dir
, entry
->text
);
981 find_callback (struct Dlg_head
*h
, Widget
*sender
,
982 dlg_msg_t msg
, int parm
, void *data
)
986 if (parm
== KEY_F (3) || parm
== KEY_F (13)) {
987 int unparsed_view
= (parm
== KEY_F (13));
988 return view_edit_currently_selected_file (unparsed_view
, 0);
990 if (parm
== KEY_F (4)) {
991 return view_edit_currently_selected_file (0, 1);
993 return MSG_NOT_HANDLED
;
1000 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
1004 /* Handles the Stop/Start button in the find window */
1006 start_stop (int button
)
1011 set_idle_proc (find_dlg
, running
);
1012 is_start
= !is_start
;
1014 status_update (is_start
? _("Stopped") : _("Searching"));
1015 button_set_text (stop_button
, fbuts
[is_start
? 1 : 0].text
);
1020 /* Handle view command, when invoked as a button */
1022 find_do_view_file (int button
)
1026 view_edit_currently_selected_file (0, 0);
1030 /* Handle edit command, when invoked as a button */
1032 find_do_edit_file (int button
)
1036 view_edit_currently_selected_file (0, 1);
1044 static gboolean i18n_flag
= FALSE
;
1047 int i
= sizeof (fbuts
) / sizeof (fbuts
[0]);
1049 fbuts
[i
].text
= _(fbuts
[i
].text
);
1050 fbuts
[i
].len
= str_term_width1 (fbuts
[i
].text
) + 3;
1053 fbuts
[2].len
+= 2; /* DEFPUSH_BUTTON */
1056 #endif /* ENABLE_NLS */
1059 * Dynamically place buttons centered within current window size
1062 int l0
= max (fbuts
[0].len
, fbuts
[1].len
);
1063 int l1
= fbuts
[2].len
+ fbuts
[3].len
+ l0
+ fbuts
[4].len
;
1064 int l2
= fbuts
[5].len
+ fbuts
[6].len
+ fbuts
[7].len
;
1067 /* Check, if both button rows fit within FIND2_X */
1068 FIND2_X
= max (l1
+ 9, COLS
- 16);
1069 FIND2_X
= max (l2
+ 8, FIND2_X
);
1071 /* compute amount of space between buttons for each row */
1072 r1
= (FIND2_X
- 4 - l1
) % 5;
1073 l1
= (FIND2_X
- 4 - l1
) / 5;
1074 r2
= (FIND2_X
- 4 - l2
) % 4;
1075 l2
= (FIND2_X
- 4 - l2
) / 4;
1077 /* ...and finally, place buttons */
1078 fbuts
[2].x
= 2 + r1
/ 2 + l1
;
1079 fbuts
[3].x
= fbuts
[2].x
+ fbuts
[2].len
+ l1
;
1080 fbuts
[0].x
= fbuts
[3].x
+ fbuts
[3].len
+ l1
;
1081 fbuts
[4].x
= fbuts
[0].x
+ l0
+ l1
;
1082 fbuts
[5].x
= 2 + r2
/ 2 + l2
;
1083 fbuts
[6].x
= fbuts
[5].x
+ fbuts
[5].len
+ l2
;
1084 fbuts
[7].x
= fbuts
[6].x
+ fbuts
[6].len
+ l2
;
1088 create_dlg (0, 0, FIND2_Y
, FIND2_X
, dialog_colors
, find_callback
,
1089 "[Find File]", _("Find File"), DLG_CENTER
| DLG_REVERSE
);
1091 add_widget (find_dlg
,
1092 button_new (FIND2_Y
- 3, fbuts
[7].x
, B_VIEW
, NORMAL_BUTTON
,
1093 fbuts
[7].text
, find_do_edit_file
));
1094 add_widget (find_dlg
,
1095 button_new (FIND2_Y
- 3, fbuts
[6].x
, B_VIEW
, NORMAL_BUTTON
,
1096 fbuts
[6].text
, find_do_view_file
));
1097 add_widget (find_dlg
,
1098 button_new (FIND2_Y
- 3, fbuts
[5].x
, B_PANELIZE
,
1099 NORMAL_BUTTON
, fbuts
[5].text
, 0));
1101 add_widget (find_dlg
,
1102 button_new (FIND2_Y
- 4, fbuts
[4].x
, B_CANCEL
,
1103 NORMAL_BUTTON
, fbuts
[4].text
, 0));
1105 button_new (FIND2_Y
- 4, fbuts
[0].x
, B_STOP
, NORMAL_BUTTON
,
1106 fbuts
[0].text
, start_stop
);
1107 add_widget (find_dlg
, stop_button
);
1108 add_widget (find_dlg
,
1109 button_new (FIND2_Y
- 4, fbuts
[3].x
, B_AGAIN
,
1110 NORMAL_BUTTON
, fbuts
[3].text
, 0));
1111 add_widget (find_dlg
,
1112 button_new (FIND2_Y
- 4, fbuts
[2].x
, B_ENTER
,
1113 DEFPUSH_BUTTON
, fbuts
[2].text
, 0));
1115 status_label
= label_new (FIND2_Y
- 7, 4, _("Searching"));
1116 add_widget (find_dlg
, status_label
);
1118 found_num_label
= label_new (FIND2_Y
- 6, 4, "");
1119 add_widget (find_dlg
, found_num_label
);
1122 listbox_new (2, 2, FIND2_Y
- 10, FIND2_X
- 4, NULL
);
1123 add_widget (find_dlg
, find_list
);
1130 set_idle_proc (find_dlg
, 1);
1131 return run_dlg (find_dlg
);
1137 set_idle_proc (find_dlg
, 0);
1138 destroy_dlg (find_dlg
);
1142 find_file (const char *start_dir
, const char *pattern
, const char *content
,
1143 char **dirname
, char **filename
)
1145 int return_value
= 0;
1146 char *dir_tmp
= NULL
, *file_tmp
= NULL
;
1150 /* FIXME: Need to cleanup this, this ought to be passed non-globaly */
1151 find_pattern
= str_unconst (pattern
);
1152 content_pattern
= (content
!= NULL
&& str_is_valid_string (content
))
1157 push_directory (start_dir
);
1159 return_value
= run_process ();
1161 /* Remove all the items in the stack */
1164 get_list_info (&file_tmp
, &dir_tmp
);
1167 *dirname
= g_strdup (dir_tmp
);
1169 *filename
= g_strdup (file_tmp
);
1171 if (return_value
== B_PANELIZE
&& *filename
) {
1172 int status
, link_to_dir
, stale_link
;
1176 WLEntry
*entry
= find_list
->list
;
1177 dir_list
*list
= ¤t_panel
->dir
;
1180 for (i
= 0; entry
!= NULL
&& i
< find_list
->count
;
1181 entry
= entry
->next
, i
++) {
1182 const char *lc_filename
= NULL
;
1184 if (!entry
->text
|| !entry
->data
)
1187 if (content_pattern
!= NULL
)
1188 lc_filename
= strchr (entry
->text
+ 4, ':') + 1;
1190 lc_filename
= entry
->text
+ 4;
1192 name
= make_fullname (entry
->data
, lc_filename
);
1194 handle_path (list
, name
, &st
, next_free
, &link_to_dir
,
1205 /* don't add files more than once to the panel */
1206 if (content_pattern
!= NULL
&& next_free
> 0
1207 && strcmp (list
->list
[next_free
- 1].fname
, name
) == 0) {
1212 if (!next_free
) /* first turn i.e clean old list */
1213 panel_clean_dir (current_panel
);
1214 list
->list
[next_free
].fnamelen
= strlen (name
);
1215 list
->list
[next_free
].fname
= name
;
1216 list
->list
[next_free
].f
.marked
= 0;
1217 list
->list
[next_free
].f
.link_to_dir
= link_to_dir
;
1218 list
->list
[next_free
].f
.stale_link
= stale_link
;
1219 list
->list
[next_free
].f
.dir_size_computed
= 0;
1220 list
->list
[next_free
].st
= st
;
1221 list
->list
[next_free
].sort_key
= NULL
;
1222 list
->list
[next_free
].second_sort_key
= NULL
;
1224 if (!(next_free
& 15))
1229 current_panel
->count
= next_free
;
1230 current_panel
->is_panelized
= 1;
1232 if (start_dir
[0] == PATH_SEP
) {
1233 strcpy (current_panel
->cwd
, PATH_SEP_STR
);
1234 chdir (PATH_SEP_STR
);
1239 g_free (content_pattern
);
1241 do_search (NULL
); /* force do_search to release resources */
1245 return return_value
;
1251 char *start_dir
= NULL
, *pattern
= NULL
, *content
= NULL
;
1252 char *filename
= NULL
, *dirname
= NULL
;
1254 gboolean dir_and_file_set
;
1256 while (find_parameters (&start_dir
, &pattern
, &content
)){
1257 if (pattern
[0] == '\0')
1258 break; /* nothing search*/
1260 dirname
= filename
= NULL
;
1262 v
= find_file (start_dir
, pattern
, content
, &dirname
, &filename
);
1266 if (dirname
|| filename
){
1268 do_cd (dirname
, cd_exact
);
1270 try_to_select (current_panel
, filename
+ (content
?
1271 (strchr (filename
+ 4, ':') - filename
+ 1) : 4) );
1272 } else if (filename
)
1273 do_cd (filename
, cd_exact
);
1274 select_item (current_panel
);
1282 dir_and_file_set
= dirname
&& filename
;
1289 if (v
== B_PANELIZE
){
1290 if (dir_and_file_set
){
1291 try_to_select (current_panel
, NULL
);
1292 panel_re_sort (current_panel
);
1293 try_to_select (current_panel
, NULL
);