4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2002, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* minibuffer.c - for handling the path entry box at the bottom */
33 #include <gdk/gdkkeysyms.h>
38 #include "collection.h"
40 #include "gui_support.h"
42 #include "minibuffer.h"
50 static GList
*shell_history
= NULL
;
52 /* Static prototypes */
53 static gint
key_press_event(GtkWidget
*widget
,
55 FilerWindow
*filer_window
);
56 static void changed(GtkEditable
*mini
, FilerWindow
*filer_window
);
57 static gboolean
find_next_match(FilerWindow
*filer_window
,
60 static gboolean
find_exact_match(FilerWindow
*filer_window
, char *pattern
);
61 static gboolean
matches(Collection
*collection
, int item
, char *pattern
);
62 static void search_in_dir(FilerWindow
*filer_window
, int dir
);
63 static guchar
*mini_contents(FilerWindow
*filer_window
);
64 static void show_help(FilerWindow
*filer_window
);
66 static gboolean
grab_focus(GtkWidget
*minibuffer
);
69 /****************************************************************
70 * EXTERNAL INTERFACE *
71 ****************************************************************/
73 static Option o_filer_beep_fail
, o_filer_beep_multi
;
75 void minibuffer_init(void)
77 option_add_int(&o_filer_beep_fail
, "filer_beep_fail", 1, NULL
);
78 option_add_int(&o_filer_beep_multi
, "filer_beep_multi", 1, NULL
);
81 /* Creates the minibuffer widgets, setting the appropriate fields
84 void create_minibuffer(FilerWindow
*filer_window
)
86 GtkWidget
*hbox
, *label
, *mini
;
88 hbox
= gtk_hbox_new(FALSE
, 0);
90 gtk_box_pack_start(GTK_BOX(hbox
),
91 new_help_button((HelpFunc
) show_help
, filer_window
),
94 label
= gtk_label_new("");
95 gtk_box_pack_start(GTK_BOX(hbox
), label
, FALSE
, TRUE
, 2);
97 mini
= gtk_entry_new();
98 gtk_box_pack_start(GTK_BOX(hbox
), mini
, TRUE
, TRUE
, 0);
99 gtk_widget_set_name(mini
, "fixed-style");
100 gtk_signal_connect(GTK_OBJECT(mini
), "key_press_event",
101 GTK_SIGNAL_FUNC(key_press_event
), filer_window
);
102 gtk_signal_connect(GTK_OBJECT(mini
), "changed",
103 GTK_SIGNAL_FUNC(changed
), filer_window
);
106 /* Grabbing focus musn't select the text... */
107 gtk_signal_connect_object(GTK_OBJECT(mini
), "grab-focus",
108 GTK_SIGNAL_FUNC(grab_focus
),
112 filer_window
->minibuffer
= mini
;
113 filer_window
->minibuffer_label
= label
;
114 filer_window
->minibuffer_area
= hbox
;
117 void minibuffer_show(FilerWindow
*filer_window
, MiniType mini_type
)
119 Collection
*collection
;
123 g_return_if_fail(filer_window
!= NULL
);
124 g_return_if_fail(filer_window
->minibuffer
!= NULL
);
126 mini
= GTK_ENTRY(filer_window
->minibuffer
);
128 filer_window
->mini_type
= MINI_NONE
;
129 gtk_label_set_text(GTK_LABEL(filer_window
->minibuffer_label
),
130 mini_type
== MINI_PATH
? _("Goto:") :
131 mini_type
== MINI_SHELL
? _("Shell:") :
132 mini_type
== MINI_SELECT_IF
? _("Select If:") :
135 collection
= filer_window
->collection
;
139 collection_move_cursor(collection
, 0, 0);
140 filer_window
->mini_cursor_base
=
141 MAX(collection
->cursor_item
, 0);
142 gtk_entry_set_text(mini
,
143 make_path(filer_window
->path
, "")->str
);
144 if (filer_window
->temp_show_hidden
)
146 display_set_hidden(filer_window
, FALSE
);
147 filer_window
->temp_show_hidden
= FALSE
;
151 gtk_entry_set_text(mini
, "");
152 filer_window
->mini_cursor_base
= -1; /* History */
156 i
= collection
->cursor_item
;
157 if (collection
->number_selected
)
158 gtk_entry_set_text(mini
, " \"$@\"");
159 else if (i
> -1 && i
< collection
->number_of_items
)
161 DirItem
*item
= (DirItem
*)
162 collection
->items
[i
].data
;
165 tmp
= g_strconcat(" ", item
->leafname
, NULL
);
166 gtk_entry_set_text(mini
, tmp
);
170 gtk_entry_set_text(mini
, "");
171 filer_window
->mini_cursor_base
= -1; /* History */
174 g_warning("Bad minibuffer type\n");
178 filer_window
->mini_type
= mini_type
;
180 gtk_entry_set_position(mini
, pos
);
182 gtk_widget_show_all(filer_window
->minibuffer_area
);
184 gtk_window_set_focus(GTK_WINDOW(filer_window
->window
),
185 filer_window
->minibuffer
);
188 void minibuffer_hide(FilerWindow
*filer_window
)
190 filer_window
->mini_type
= MINI_NONE
;
192 gtk_widget_hide(filer_window
->minibuffer_area
);
193 gtk_window_set_focus(GTK_WINDOW(filer_window
->window
),
194 GTK_WIDGET(filer_window
->collection
));
196 if (filer_window
->temp_show_hidden
)
198 Collection
*collection
= filer_window
->collection
;
199 int i
= collection
->cursor_item
;
200 DirItem
*item
= NULL
;
202 if (i
>= 0 && i
< collection
->number_of_items
)
203 item
= (DirItem
*) collection
->items
[i
].data
;
205 if (item
== NULL
|| item
->leafname
[0] != '.')
206 display_set_hidden(filer_window
, FALSE
);
207 filer_window
->temp_show_hidden
= FALSE
;
211 /* Insert this leafname at the cursor (replacing the selection, if any).
212 * Must be in SHELL mode.
214 void minibuffer_add(FilerWindow
*filer_window
, guchar
*leafname
)
217 GtkEditable
*edit
= GTK_EDITABLE(filer_window
->minibuffer
);
218 GtkEntry
*entry
= GTK_ENTRY(edit
);
221 g_return_if_fail(filer_window
->mini_type
== MINI_SHELL
);
223 esc
= shell_escape(leafname
);
225 gtk_editable_delete_selection(edit
);
226 pos
= gtk_editable_get_position(edit
);
228 if (pos
> 0 && gtk_entry_get_text(entry
)[pos
- 1] != ' ')
229 gtk_editable_insert_text(edit
, " ", 1, &pos
);
231 gtk_editable_insert_text(edit
, esc
, strlen(esc
), &pos
);
232 gtk_editable_set_position(edit
, pos
);
238 /****************************************************************
239 * INTERNAL FUNCTIONS *
240 ****************************************************************/
242 static void show_help(FilerWindow
*filer_window
)
246 gtk_widget_grab_focus(filer_window
->minibuffer
);
248 switch (filer_window
->mini_type
)
251 message
= _("Enter the name of a file and I'll display "
252 "it for you. Press Tab to fill in the longest "
253 "match. Escape to close the minibuffer.");
256 message
= _("Enter a shell command to execute. Click "
257 "on a file to add it to the buffer.");
260 show_condition_help(NULL
);
267 delayed_error("%s", message
);
273 static void path_return_pressed(FilerWindow
*filer_window
, GdkEventKey
*event
)
275 Collection
*collection
= filer_window
->collection
;
276 int item
= collection
->cursor_item
;
277 char *path
, *pattern
;
278 int flags
= OPEN_FROM_MINI
| OPEN_SAME_WINDOW
;
280 path
= gtk_entry_get_text(GTK_ENTRY(filer_window
->minibuffer
));
281 pattern
= strrchr(path
, '/');
287 if (item
== -1 || item
>= collection
->number_of_items
||
288 !matches(collection
, item
, pattern
))
294 if ((event
->state
& GDK_SHIFT_MASK
) != 0)
297 filer_openitem(filer_window
, item
, flags
);
300 /* Use the cursor item to fill in the minibuffer.
301 * If there are multiple matches then fill in as much as possible and
304 static void complete(FilerWindow
*filer_window
)
307 Collection
*collection
= filer_window
->collection
;
308 int cursor
= collection
->cursor_item
;
310 int shortest_stem
= -1;
315 if (cursor
< 0 || cursor
>= collection
->number_of_items
)
321 entry
= GTK_ENTRY(filer_window
->minibuffer
);
323 item
= (DirItem
*) collection
->items
[cursor
].data
;
325 text
= gtk_entry_get_text(entry
);
326 leaf
= strrchr(text
, '/');
334 if (!matches(collection
, cursor
, leaf
))
340 current_stem
= strlen(leaf
);
342 /* Find the longest other match of this name. It it's longer than
343 * the currently entered text then complete only up to that length.
345 for (other
= 0; other
< collection
->number_of_items
; other
++)
347 DirItem
*other_item
= (DirItem
*) collection
->items
[other
].data
;
353 while (other_item
->leafname
[stem
] && item
->leafname
[stem
])
355 if (other_item
->leafname
[stem
] != item
->leafname
[stem
])
360 /* stem is the index of the first difference */
361 if (stem
>= current_stem
&&
362 (shortest_stem
== -1 || stem
< shortest_stem
))
363 shortest_stem
= stem
;
366 if (current_stem
== shortest_stem
)
368 /* We didn't add anything... */
369 if (o_filer_beep_fail
.int_value
)
372 else if (current_stem
< shortest_stem
)
376 extra
= g_strndup(item
->leafname
+ current_stem
,
377 shortest_stem
- current_stem
);
378 gtk_entry_append_text(entry
, extra
);
381 gtk_entry_set_position(entry
, -1);
384 if (o_filer_beep_multi
.int_value
)
391 new = make_path(filer_window
->path
, item
->leafname
);
393 if (item
->base_type
== TYPE_DIRECTORY
&&
394 (item
->flags
& ITEM_FLAG_APPDIR
) == 0)
395 g_string_append_c(new, '/');
397 gtk_entry_set_text(entry
, new->str
);
399 gtk_entry_set_position(entry
, -1);
404 /* This is an idle function because Gtk+ 2.0 changes text in a entry
405 * with two signals; one to blank it and one to put the new text in.
407 static gboolean
path_changed(gpointer data
)
409 FilerWindow
*filer_window
= (FilerWindow
*) data
;
410 GtkWidget
*mini
= filer_window
->minibuffer
;
414 new = gtk_entry_get_text(GTK_ENTRY(mini
));
416 leaf
= g_basename(new);
418 path
= g_strdup("/");
420 path
= g_dirname(new);
421 real
= pathdup(path
);
424 if (strcmp(real
, filer_window
->path
) != 0)
426 /* The new path is in a different directory */
429 if (mc_stat(real
, &info
) == 0 && S_ISDIR(info
.st_mode
))
430 filer_change_to(filer_window
, real
, leaf
);
438 if (!filer_window
->show_hidden
)
440 filer_window
->temp_show_hidden
= TRUE
;
441 display_set_hidden(filer_window
, TRUE
);
444 else if (filer_window
->temp_show_hidden
)
446 display_set_hidden(filer_window
, FALSE
);
447 filer_window
->temp_show_hidden
= FALSE
;
450 if (find_exact_match(filer_window
, leaf
) == FALSE
&&
451 find_next_match(filer_window
, leaf
, 0) == FALSE
)
460 /* Look for an exact match, and move the cursor to it if found.
463 static gboolean
find_exact_match(FilerWindow
*filer_window
, char *pattern
)
465 Collection
*collection
= filer_window
->collection
;
468 for (i
= 0; i
< collection
->number_of_items
; i
++)
470 DirItem
*item
= (DirItem
*) collection
->items
[i
].data
;
472 if (strcmp(item
->leafname
, pattern
) == 0)
474 collection_set_cursor_item(collection
, i
);
482 /* Find the next item in the collection that matches 'pattern'. Start from
483 * mini_cursor_base and loop at either end. dir is 1 for a forward search,
484 * -1 for backwards. 0 means forwards, but may stay the same.
486 * Does not automatically update mini_cursor_base.
488 * Returns TRUE if a match is found.
490 static gboolean
find_next_match(FilerWindow
*filer_window
,
494 Collection
*collection
= filer_window
->collection
;
495 int base
= filer_window
->mini_cursor_base
;
497 gboolean retval
= TRUE
;
499 if (collection
->number_of_items
< 1)
502 if (base
< 0 || base
>= collection
->number_of_items
)
503 filer_window
->mini_cursor_base
= base
= 0;
507 /* Step to the next item */
510 if (item
>= collection
->number_of_items
)
513 item
= collection
->number_of_items
- 1;
517 else if (item
== base
)
520 break; /* No (other) matches at all */
522 } while (!matches(collection
, item
, pattern
));
524 collection_set_cursor_item(collection
, item
);
529 static gboolean
matches(Collection
*collection
, int item_number
, char *pattern
)
531 DirItem
*item
= (DirItem
*) collection
->items
[item_number
].data
;
533 return strncmp(item
->leafname
, pattern
, strlen(pattern
)) == 0;
536 /* Find next match and set base for future matches. */
537 static void search_in_dir(FilerWindow
*filer_window
, int dir
)
539 char *path
, *pattern
;
541 path
= gtk_entry_get_text(GTK_ENTRY(filer_window
->minibuffer
));
542 pattern
= strrchr(path
, '/');
548 filer_window
->mini_cursor_base
= filer_window
->collection
->cursor_item
;
549 find_next_match(filer_window
, pattern
, dir
);
550 filer_window
->mini_cursor_base
= filer_window
->collection
->cursor_item
;
555 static void add_to_history(guchar
*line
)
559 last
= shell_history
? (guchar
*) shell_history
->data
: NULL
;
561 if (last
&& strcmp(last
, line
) == 0)
562 return; /* Duplicating last entry */
564 shell_history
= g_list_prepend(shell_history
, g_strdup(line
));
567 static void shell_done(FilerWindow
*filer_window
)
569 if (filer_exists(filer_window
))
570 filer_update_dir(filer_window
, TRUE
);
573 /* Given a list of matches, return the longest stem. g_free() the result.
574 * Special chars are escaped. If there is only a single (non-dir) match
575 * then a trailing space is added.
577 static guchar
*best_match(FilerWindow
*filer_window
, glob_t
*matches
)
579 gchar
*first
= matches
->gl_pathv
[0];
581 int longest
, path_len
;
584 longest
= strlen(first
);
586 for (i
= 1; i
< matches
->gl_pathc
; i
++)
589 guchar
*m
= matches
->gl_pathv
[i
];
591 for (j
= 0; j
< longest
; j
++)
592 if (m
[j
] != first
[j
])
596 path_len
= strlen(filer_window
->path
);
597 if (strncmp(filer_window
->path
, first
, path_len
) == 0 &&
598 first
[path_len
] == '/' && first
[path_len
+ 1])
600 path
= g_strndup(first
+ path_len
+ 1, longest
- path_len
- 1);
603 path
= g_strndup(first
, longest
);
605 tmp
= shell_escape(path
);
608 if (matches
->gl_pathc
== 1 && tmp
[strlen(tmp
) - 1] != '/')
610 path
= g_strdup_printf("%s ", tmp
);
618 static void shell_tab(FilerWindow
*filer_window
)
628 entry
= gtk_entry_get_text(GTK_ENTRY(filer_window
->minibuffer
));
629 pos
= gtk_editable_get_position(GTK_EDITABLE(filer_window
->minibuffer
));
630 leaf
= g_string_new(NULL
);
633 for (i
= 0; i
< pos
; i
++)
642 g_string_truncate(leaf
, 0);
645 else if (c
== '\\' && i
+ 1 < pos
)
647 else if (c
== '"' || c
== '\'')
651 for (++i
; i
< pos
; i
++)
655 if (cc
== '\\' && i
+ 1 < pos
)
657 else if (entry
[i
] == c
)
659 g_string_append_c(leaf
, entry
[i
]);
664 g_string_append_c(leaf
, c
);
670 if (leaf
->str
[0] != '/' && leaf
->str
[0] != '~')
672 g_string_prepend_c(leaf
, '/');
673 g_string_prepend(leaf
, filer_window
->path
);
676 g_string_append_c(leaf
, '*');
682 GLOB_MARK
, NULL
, &matches
) == 0)
684 if (matches
.gl_pathc
> 0)
688 GTK_EDITABLE(filer_window
->minibuffer
);
690 best
= best_match(filer_window
, &matches
);
692 gtk_editable_delete_text(edit
, leaf_start
, pos
);
693 gtk_editable_insert_text(edit
, best
, strlen(best
),
695 gtk_editable_set_position(edit
, leaf_start
);
699 if (matches
.gl_pathc
!= 1)
705 g_string_free(leaf
, TRUE
);
708 /* Either execute the command or make it the default run action */
709 static void shell_return_pressed(FilerWindow
*filer_window
)
714 Collection
*collection
= filer_window
->collection
;
717 entry
= mini_contents(filer_window
);
722 add_to_history(entry
);
724 argv
= g_ptr_array_new();
725 g_ptr_array_add(argv
, "sh");
726 g_ptr_array_add(argv
, "-c");
727 g_ptr_array_add(argv
, entry
);
728 g_ptr_array_add(argv
, "sh");
730 for (i
= 0; i
< collection
->number_of_items
; i
++)
732 DirItem
*item
= (DirItem
*) collection
->items
[i
].data
;
733 if (collection
->items
[i
].selected
)
734 g_ptr_array_add(argv
, item
->leafname
);
737 g_ptr_array_add(argv
, NULL
);
744 delayed_error(_("Failed to create child process"));
747 /* Ensure output is noticed - send stdout to stderr */
748 dup2(STDERR_FILENO
, STDOUT_FILENO
);
749 close_on_exec(STDOUT_FILENO
, FALSE
);
750 if (chdir(filer_window
->path
))
751 g_printerr("chdir(%s) failed: %s\n",
754 execvp((char *) argv
->pdata
[0],
755 (char **) argv
->pdata
);
756 g_printerr("execvp(%s, ...) failed: %s\n",
757 (char *) argv
->pdata
[0],
761 on_child_death(child
,
762 (CallbackFn
) shell_done
, filer_window
);
766 g_ptr_array_free(argv
, TRUE
);
769 minibuffer_hide(filer_window
);
772 /* Move through the shell history */
773 static void shell_recall(FilerWindow
*filer_window
, int dir
)
776 int pos
= filer_window
->mini_cursor_base
;
781 command
= g_list_nth_data(shell_history
, pos
);
790 filer_window
->mini_cursor_base
= pos
;
792 gtk_entry_set_text(GTK_ENTRY(filer_window
->minibuffer
), command
);
797 static void select_return_pressed(FilerWindow
*filer_window
, guint etime
)
802 Collection
*collection
= filer_window
->collection
;
805 entry
= mini_contents(filer_window
);
810 add_to_history(entry
);
812 cond
= find_compile(entry
);
815 delayed_error(_("Invalid Find condition"));
819 info
.now
= time(NULL
);
820 info
.prune
= FALSE
; /* (don't care) */
821 n
= collection
->number_of_items
;
823 collection
->block_selection_changed
++;
825 /* If an item at the start is selected then we could lose the
826 * primary selection after checking that item and then need to
827 * gain it again at the end. Therefore, if anything is selected
828 * then select the last item until the end of the search.
830 if (collection
->number_selected
)
831 collection_select_item(collection
, n
- 1);
833 for (i
= 0; i
< n
; i
++)
835 DirItem
*item
= (DirItem
*) collection
->items
[i
].data
;
837 info
.leaf
= item
->leafname
;
838 info
.fullpath
= make_path(filer_window
->path
, info
.leaf
)->str
;
840 if (lstat(info
.fullpath
, &info
.stats
) == 0 &&
841 find_test_condition(cond
, &info
))
842 collection_select_item(collection
, i
);
844 collection_unselect_item(collection
, i
);
847 find_condition_free(cond
);
849 collection_unblock_selection_changed(collection
, etime
, TRUE
);
851 minibuffer_hide(filer_window
);
857 static gint
key_press_event(GtkWidget
*widget
,
859 FilerWindow
*filer_window
)
861 if (event
->keyval
== GDK_Escape
)
863 if (filer_window
->mini_type
== MINI_SHELL
)
867 line
= mini_contents(filer_window
);
869 add_to_history(line
);
872 minibuffer_hide(filer_window
);
876 switch (filer_window
->mini_type
)
879 switch (event
->keyval
)
882 search_in_dir(filer_window
, -1);
885 search_in_dir(filer_window
, 1);
889 path_return_pressed(filer_window
,
893 complete(filer_window
);
901 switch (event
->keyval
)
904 shell_recall(filer_window
, 1);
907 shell_recall(filer_window
, -1);
910 shell_tab(filer_window
);
914 shell_return_pressed(filer_window
);
921 switch (event
->keyval
)
924 shell_recall(filer_window
, 1);
927 shell_recall(filer_window
, -1);
933 select_return_pressed(filer_window
,
945 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
), "key_press_event");
950 static void changed(GtkEditable
*mini
, FilerWindow
*filer_window
)
952 switch (filer_window
->mini_type
)
955 gtk_idle_add(path_changed
, filer_window
);
958 set_find_string_colour(GTK_WIDGET(mini
),
960 GTK_ENTRY(filer_window
->minibuffer
)));
967 /* Returns a string (which must NOT be freed), or NULL if the buffer
968 * is blank (whitespace only).
970 static guchar
*mini_contents(FilerWindow
*filer_window
)
974 entry
= gtk_entry_get_text(GTK_ENTRY(filer_window
->minibuffer
));
976 for (c
= entry
; *c
; c
++)
984 static gboolean
grab_focus(GtkWidget
*minibuffer
)
986 GtkWidgetClass
*class;
988 class = GTK_WIDGET_CLASS(gtk_type_class(GTK_TYPE_WIDGET
));
990 class->grab_focus(minibuffer
);
992 gtk_signal_emit_stop_by_name(GTK_OBJECT(minibuffer
), "grab_focus");