4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2001, 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"
49 static GList
*shell_history
= NULL
;
51 /* Static prototypes */
52 static gint
key_press_event(GtkWidget
*widget
,
54 FilerWindow
*filer_window
);
55 static void changed(GtkEditable
*mini
, FilerWindow
*filer_window
);
56 static gboolean
find_next_match(FilerWindow
*filer_window
,
59 static gboolean
find_exact_match(FilerWindow
*filer_window
, char *pattern
);
60 static gboolean
matches(Collection
*collection
, int item
, char *pattern
);
61 static void search_in_dir(FilerWindow
*filer_window
, int dir
);
62 static guchar
*mini_contents(FilerWindow
*filer_window
);
63 static void show_help(FilerWindow
*filer_window
);
66 /****************************************************************
67 * EXTERNAL INTERFACE *
68 ****************************************************************/
70 void minibuffer_init(void)
72 option_add_int("filer_beep_fail", 1, NULL
);
73 option_add_int("filer_beep_multi", 1, NULL
);
76 /* Creates the minibuffer widgets, setting the appropriate fields
79 void create_minibuffer(FilerWindow
*filer_window
)
81 GtkWidget
*hbox
, *label
, *mini
;
83 hbox
= gtk_hbox_new(FALSE
, 0);
85 gtk_box_pack_start(GTK_BOX(hbox
),
86 new_help_button((HelpFunc
) show_help
, filer_window
),
89 label
= gtk_label_new("");
90 gtk_box_pack_start(GTK_BOX(hbox
), label
, FALSE
, TRUE
, 2);
92 mini
= gtk_entry_new();
93 gtk_box_pack_start(GTK_BOX(hbox
), mini
, TRUE
, TRUE
, 0);
94 gtk_widget_set_style(mini
, fixed_style
);
95 gtk_signal_connect(GTK_OBJECT(mini
), "key_press_event",
96 GTK_SIGNAL_FUNC(key_press_event
), filer_window
);
97 gtk_signal_connect(GTK_OBJECT(mini
), "changed",
98 GTK_SIGNAL_FUNC(changed
), filer_window
);
100 filer_window
->minibuffer
= mini
;
101 filer_window
->minibuffer_label
= label
;
102 filer_window
->minibuffer_area
= hbox
;
105 void minibuffer_show(FilerWindow
*filer_window
, MiniType mini_type
)
107 Collection
*collection
;
111 g_return_if_fail(filer_window
!= NULL
);
112 g_return_if_fail(filer_window
->minibuffer
!= NULL
);
114 mini
= GTK_ENTRY(filer_window
->minibuffer
);
115 gtk_widget_set_style(filer_window
->minibuffer
, fixed_style
);
117 filer_window
->mini_type
= MINI_NONE
;
118 gtk_label_set_text(GTK_LABEL(filer_window
->minibuffer_label
),
119 mini_type
== MINI_PATH
? _("Goto:") :
120 mini_type
== MINI_SHELL
? _("Shell:") :
121 mini_type
== MINI_SELECT_IF
? _("Select If:") :
124 collection
= filer_window
->collection
;
128 collection_move_cursor(collection
, 0, 0);
129 filer_window
->mini_cursor_base
=
130 MAX(collection
->cursor_item
, 0);
131 gtk_entry_set_text(mini
,
132 make_path(filer_window
->path
, "")->str
);
133 if (filer_window
->temp_show_hidden
)
135 display_set_hidden(filer_window
, FALSE
);
136 filer_window
->temp_show_hidden
= FALSE
;
140 gtk_entry_set_text(mini
, "");
141 filer_window
->mini_cursor_base
= -1; /* History */
145 i
= collection
->cursor_item
;
146 if (collection
->number_selected
)
147 gtk_entry_set_text(mini
, " \"$@\"");
148 else if (i
> -1 && i
< collection
->number_of_items
)
150 DirItem
*item
= (DirItem
*)
151 collection
->items
[i
].data
;
154 tmp
= g_strconcat(" ", item
->leafname
, NULL
);
155 gtk_entry_set_text(mini
, tmp
);
159 gtk_entry_set_text(mini
, "");
160 filer_window
->mini_cursor_base
= -1; /* History */
163 g_warning("Bad minibuffer type\n");
167 filer_window
->mini_type
= mini_type
;
169 gtk_entry_set_position(mini
, pos
);
171 gtk_widget_show_all(filer_window
->minibuffer_area
);
173 gtk_window_set_focus(GTK_WINDOW(filer_window
->window
),
174 filer_window
->minibuffer
);
177 void minibuffer_hide(FilerWindow
*filer_window
)
179 filer_window
->mini_type
= MINI_NONE
;
181 gtk_widget_hide(filer_window
->minibuffer_area
);
182 gtk_window_set_focus(GTK_WINDOW(filer_window
->window
),
183 GTK_WIDGET(filer_window
->collection
));
185 if (filer_window
->temp_show_hidden
)
187 display_set_hidden(filer_window
, FALSE
);
188 filer_window
->temp_show_hidden
= FALSE
;
192 /* Insert this leafname at the cursor (replacing the selection, if any).
193 * Must be in SHELL mode.
195 void minibuffer_add(FilerWindow
*filer_window
, guchar
*leafname
)
198 GtkEditable
*edit
= GTK_EDITABLE(filer_window
->minibuffer
);
199 GtkEntry
*entry
= GTK_ENTRY(edit
);
202 g_return_if_fail(filer_window
->mini_type
== MINI_SHELL
);
204 esc
= shell_escape(leafname
);
206 gtk_editable_delete_selection(edit
);
207 pos
= gtk_editable_get_position(edit
);
209 if (pos
> 0 && gtk_entry_get_text(entry
)[pos
- 1] != ' ')
210 gtk_editable_insert_text(edit
, " ", 1, &pos
);
212 gtk_editable_insert_text(edit
, esc
, strlen(esc
), &pos
);
213 gtk_editable_set_position(edit
, pos
);
219 /****************************************************************
220 * INTERNAL FUNCTIONS *
221 ****************************************************************/
223 static void show_help(FilerWindow
*filer_window
)
227 gtk_widget_grab_focus(filer_window
->minibuffer
);
229 switch (filer_window
->mini_type
)
232 message
= _("Enter the name of a file and I'll display "
233 "it for you. Press Tab to fill in the longest "
234 "match. Escape to close the minibuffer.");
237 message
= _("Enter a shell command to execute. Click "
238 "on a file to add it to the buffer.");
241 show_condition_help(NULL
);
248 delayed_rox_error("%s", message
);
254 static void path_return_pressed(FilerWindow
*filer_window
, GdkEventKey
*event
)
256 Collection
*collection
= filer_window
->collection
;
257 int item
= collection
->cursor_item
;
258 char *path
, *pattern
;
259 int flags
= OPEN_FROM_MINI
| OPEN_SAME_WINDOW
;
261 path
= gtk_entry_get_text(GTK_ENTRY(filer_window
->minibuffer
));
262 pattern
= strrchr(path
, '/');
268 if (item
== -1 || item
>= collection
->number_of_items
||
269 !matches(collection
, item
, pattern
))
275 if ((event
->state
& GDK_SHIFT_MASK
) != 0)
278 filer_openitem(filer_window
, item
, flags
);
281 /* Use the cursor item to fill in the minibuffer.
282 * If there are multiple matches then fill in as much as possible and
285 static void complete(FilerWindow
*filer_window
)
288 Collection
*collection
= filer_window
->collection
;
289 int cursor
= collection
->cursor_item
;
291 int shortest_stem
= -1;
296 if (cursor
< 0 || cursor
>= collection
->number_of_items
)
302 entry
= GTK_ENTRY(filer_window
->minibuffer
);
304 item
= (DirItem
*) collection
->items
[cursor
].data
;
306 text
= gtk_entry_get_text(entry
);
307 leaf
= strrchr(text
, '/');
315 if (!matches(collection
, cursor
, leaf
))
321 current_stem
= strlen(leaf
);
323 /* Find the longest other match of this name. It it's longer than
324 * the currently entered text then complete only up to that length.
326 for (other
= 0; other
< collection
->number_of_items
; other
++)
328 DirItem
*other_item
= (DirItem
*) collection
->items
[other
].data
;
334 while (other_item
->leafname
[stem
] && item
->leafname
[stem
])
336 if (other_item
->leafname
[stem
] != item
->leafname
[stem
])
341 /* stem is the index of the first difference */
342 if (stem
>= current_stem
&&
343 (shortest_stem
== -1 || stem
< shortest_stem
))
344 shortest_stem
= stem
;
347 if (current_stem
== shortest_stem
)
349 /* We didn't add anything... */
350 if (option_get_int("filer_beep_fail"))
353 else if (current_stem
< shortest_stem
)
357 extra
= g_strndup(item
->leafname
+ current_stem
,
358 shortest_stem
- current_stem
);
359 gtk_entry_append_text(entry
, extra
);
362 gtk_entry_set_position(entry
, -1);
365 if (option_get_int("filer_beep_multi"))
372 new = make_path(filer_window
->path
, item
->leafname
);
374 if (item
->base_type
== TYPE_DIRECTORY
&&
375 (item
->flags
& ITEM_FLAG_APPDIR
) == 0)
376 g_string_append_c(new, '/');
378 gtk_entry_set_text(entry
, new->str
);
380 gtk_entry_set_position(entry
, -1);
385 /* This is an idle function because Gtk+ 2.0 changes text in a entry
386 * with two signals; one to blank it and one to put the new text in.
388 static gboolean
path_changed(gpointer data
)
390 FilerWindow
*filer_window
= (FilerWindow
*) data
;
391 GtkWidget
*mini
= filer_window
->minibuffer
;
395 new = gtk_entry_get_text(GTK_ENTRY(mini
));
397 leaf
= g_basename(new);
399 path
= g_strdup("/");
401 path
= g_dirname(new);
402 real
= pathdup(path
);
405 if (strcmp(real
, filer_window
->path
) != 0)
407 /* The new path is in a different directory */
410 if (mc_stat(real
, &info
) == 0 && S_ISDIR(info
.st_mode
))
411 filer_change_to(filer_window
, real
, leaf
);
419 if (!filer_window
->show_hidden
)
421 filer_window
->temp_show_hidden
= TRUE
;
422 display_set_hidden(filer_window
, TRUE
);
425 else if (filer_window
->temp_show_hidden
)
427 display_set_hidden(filer_window
, FALSE
);
428 filer_window
->temp_show_hidden
= FALSE
;
431 if (find_exact_match(filer_window
, leaf
) == FALSE
&&
432 find_next_match(filer_window
, leaf
, 0) == FALSE
)
441 /* Look for an exact match, and move the cursor to it if found.
444 static gboolean
find_exact_match(FilerWindow
*filer_window
, char *pattern
)
446 Collection
*collection
= filer_window
->collection
;
449 for (i
= 0; i
< collection
->number_of_items
; i
++)
451 DirItem
*item
= (DirItem
*) collection
->items
[i
].data
;
453 if (strcmp(item
->leafname
, pattern
) == 0)
455 collection_set_cursor_item(collection
, i
);
463 /* Find the next item in the collection that matches 'pattern'. Start from
464 * mini_cursor_base and loop at either end. dir is 1 for a forward search,
465 * -1 for backwards. 0 means forwards, but may stay the same.
467 * Does not automatically update mini_cursor_base.
469 * Returns TRUE if a match is found.
471 static gboolean
find_next_match(FilerWindow
*filer_window
,
475 Collection
*collection
= filer_window
->collection
;
476 int base
= filer_window
->mini_cursor_base
;
478 gboolean retval
= TRUE
;
480 if (collection
->number_of_items
< 1)
483 if (base
< 0 || base
>= collection
->number_of_items
)
484 filer_window
->mini_cursor_base
= base
= 0;
488 /* Step to the next item */
491 if (item
>= collection
->number_of_items
)
494 item
= collection
->number_of_items
- 1;
498 else if (item
== base
)
501 break; /* No (other) matches at all */
503 } while (!matches(collection
, item
, pattern
));
505 collection_set_cursor_item(collection
, item
);
510 static gboolean
matches(Collection
*collection
, int item_number
, char *pattern
)
512 DirItem
*item
= (DirItem
*) collection
->items
[item_number
].data
;
514 return strncmp(item
->leafname
, pattern
, strlen(pattern
)) == 0;
517 /* Find next match and set base for future matches. */
518 static void search_in_dir(FilerWindow
*filer_window
, int dir
)
520 char *path
, *pattern
;
522 path
= gtk_entry_get_text(GTK_ENTRY(filer_window
->minibuffer
));
523 pattern
= strrchr(path
, '/');
529 filer_window
->mini_cursor_base
= filer_window
->collection
->cursor_item
;
530 find_next_match(filer_window
, pattern
, dir
);
531 filer_window
->mini_cursor_base
= filer_window
->collection
->cursor_item
;
536 static void add_to_history(guchar
*line
)
540 last
= shell_history
? (guchar
*) shell_history
->data
: NULL
;
542 if (last
&& strcmp(last
, line
) == 0)
543 return; /* Duplicating last entry */
545 shell_history
= g_list_prepend(shell_history
, g_strdup(line
));
548 static void shell_done(FilerWindow
*filer_window
)
550 if (filer_exists(filer_window
))
551 filer_update_dir(filer_window
, TRUE
);
554 /* Given a list of matches, return the longest stem. g_free() the result.
555 * Special chars are escaped. If there is only a single (non-dir) match
556 * then a trailing space is added.
558 static guchar
*best_match(FilerWindow
*filer_window
, glob_t
*matches
)
560 gchar
*first
= matches
->gl_pathv
[0];
562 int longest
, path_len
;
565 longest
= strlen(first
);
567 for (i
= 1; i
< matches
->gl_pathc
; i
++)
570 guchar
*m
= matches
->gl_pathv
[i
];
572 for (j
= 0; j
< longest
; j
++)
573 if (m
[j
] != first
[j
])
577 path_len
= strlen(filer_window
->path
);
578 if (strncmp(filer_window
->path
, first
, path_len
) == 0 &&
579 first
[path_len
] == '/' && first
[path_len
+ 1])
581 path
= g_strndup(first
+ path_len
+ 1, longest
- path_len
- 1);
584 path
= g_strndup(first
, longest
);
586 tmp
= shell_escape(path
);
589 if (matches
->gl_pathc
== 1 && tmp
[strlen(tmp
) - 1] != '/')
591 path
= g_strdup_printf("%s ", tmp
);
599 static void shell_tab(FilerWindow
*filer_window
)
609 entry
= gtk_entry_get_text(GTK_ENTRY(filer_window
->minibuffer
));
610 pos
= gtk_editable_get_position(GTK_EDITABLE(filer_window
->minibuffer
));
611 leaf
= g_string_new(NULL
);
614 for (i
= 0; i
< pos
; i
++)
623 g_string_truncate(leaf
, 0);
626 else if (c
== '\\' && i
+ 1 < pos
)
628 else if (c
== '"' || c
== '\'')
632 for (++i
; i
< pos
; i
++)
636 if (cc
== '\\' && i
+ 1 < pos
)
638 else if (entry
[i
] == c
)
640 g_string_append_c(leaf
, entry
[i
]);
645 g_string_append_c(leaf
, c
);
651 if (leaf
->str
[0] != '/' && leaf
->str
[0] != '~')
653 g_string_prepend_c(leaf
, '/');
654 g_string_prepend(leaf
, filer_window
->path
);
657 g_string_append_c(leaf
, '*');
663 GLOB_MARK
, NULL
, &matches
) == 0)
665 if (matches
.gl_pathc
> 0)
669 GTK_EDITABLE(filer_window
->minibuffer
);
671 best
= best_match(filer_window
, &matches
);
673 gtk_editable_delete_text(edit
, leaf_start
, pos
);
674 gtk_editable_insert_text(edit
, best
, strlen(best
),
676 gtk_editable_set_position(edit
, leaf_start
);
680 if (matches
.gl_pathc
!= 1)
686 g_string_free(leaf
, TRUE
);
689 /* Either execute the command or make it the default run action */
690 static void shell_return_pressed(FilerWindow
*filer_window
)
695 Collection
*collection
= filer_window
->collection
;
698 entry
= mini_contents(filer_window
);
703 add_to_history(entry
);
705 argv
= g_ptr_array_new();
706 g_ptr_array_add(argv
, "sh");
707 g_ptr_array_add(argv
, "-c");
708 g_ptr_array_add(argv
, entry
);
709 g_ptr_array_add(argv
, "sh");
711 for (i
= 0; i
< collection
->number_of_items
; i
++)
713 DirItem
*item
= (DirItem
*) collection
->items
[i
].data
;
714 if (collection
->items
[i
].selected
)
715 g_ptr_array_add(argv
, item
->leafname
);
718 g_ptr_array_add(argv
, NULL
);
725 delayed_rox_error(_("Failed to create child process"));
728 /* Ensure output is noticed - send stdout to stderr */
729 dup2(STDERR_FILENO
, STDOUT_FILENO
);
730 close_on_exec(STDOUT_FILENO
, FALSE
);
731 if (chdir(filer_window
->path
))
732 g_printerr("chdir(%s) failed: %s\n",
735 execvp((char *) argv
->pdata
[0],
736 (char **) argv
->pdata
);
737 g_printerr("execvp(%s, ...) failed: %s\n",
738 (char *) argv
->pdata
[0],
742 on_child_death(child
,
743 (CallbackFn
) shell_done
, filer_window
);
747 g_ptr_array_free(argv
, TRUE
);
750 minibuffer_hide(filer_window
);
753 /* Move through the shell history */
754 static void shell_recall(FilerWindow
*filer_window
, int dir
)
757 int pos
= filer_window
->mini_cursor_base
;
762 command
= g_list_nth_data(shell_history
, pos
);
771 filer_window
->mini_cursor_base
= pos
;
773 gtk_entry_set_text(GTK_ENTRY(filer_window
->minibuffer
), command
);
778 static void select_return_pressed(FilerWindow
*filer_window
, guint etime
)
783 Collection
*collection
= filer_window
->collection
;
786 entry
= mini_contents(filer_window
);
791 add_to_history(entry
);
793 cond
= find_compile(entry
);
796 delayed_rox_error(_("Invalid Find condition"));
800 info
.now
= time(NULL
);
801 info
.prune
= FALSE
; /* (don't care) */
802 n
= collection
->number_of_items
;
804 collection
->block_selection_changed
++;
806 /* If an item at the start is selected then we could lose the
807 * primary selection after checking that item and then need to
808 * gain it again at the end. Therefore, if anything is selected
809 * then select the last item until the end of the search.
811 if (collection
->number_selected
)
812 collection_select_item(collection
, n
- 1);
814 for (i
= 0; i
< n
; i
++)
816 DirItem
*item
= (DirItem
*) collection
->items
[i
].data
;
818 info
.leaf
= item
->leafname
;
819 info
.fullpath
= make_path(filer_window
->path
, info
.leaf
)->str
;
821 if (lstat(info
.fullpath
, &info
.stats
) == 0 &&
822 find_test_condition(cond
, &info
))
823 collection_select_item(collection
, i
);
825 collection_unselect_item(collection
, i
);
828 find_condition_free(cond
);
830 collection_unblock_selection_changed(collection
, etime
, TRUE
);
832 minibuffer_hide(filer_window
);
838 static gint
key_press_event(GtkWidget
*widget
,
840 FilerWindow
*filer_window
)
842 if (event
->keyval
== GDK_Escape
)
844 if (filer_window
->mini_type
== MINI_SHELL
)
848 line
= mini_contents(filer_window
);
850 add_to_history(line
);
853 minibuffer_hide(filer_window
);
857 switch (filer_window
->mini_type
)
860 switch (event
->keyval
)
863 search_in_dir(filer_window
, -1);
866 search_in_dir(filer_window
, 1);
870 path_return_pressed(filer_window
,
874 complete(filer_window
);
882 switch (event
->keyval
)
885 shell_recall(filer_window
, 1);
888 shell_recall(filer_window
, -1);
891 shell_tab(filer_window
);
895 shell_return_pressed(filer_window
);
902 switch (event
->keyval
)
905 shell_recall(filer_window
, 1);
908 shell_recall(filer_window
, -1);
914 select_return_pressed(filer_window
,
926 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
), "key_press_event");
931 static void changed(GtkEditable
*mini
, FilerWindow
*filer_window
)
933 switch (filer_window
->mini_type
)
936 gtk_idle_add(path_changed
, filer_window
);
939 set_find_string_colour(GTK_WIDGET(mini
),
941 GTK_ENTRY(filer_window
->minibuffer
)));
948 /* Returns a string (which must NOT be freed), or NULL if the buffer
949 * is blank (whitespace only).
951 static guchar
*mini_contents(FilerWindow
*filer_window
)
955 entry
= gtk_entry_get_text(GTK_ENTRY(filer_window
->minibuffer
));
957 for (c
= entry
; *c
; c
++)