4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2000, Thomas Leonard, <tal197@ecs.soton.ac.uk>.
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 */
30 #include <gdk/gdkkeysyms.h>
32 #include "collection.h"
33 #include "gui_support.h"
35 #include "minibuffer.h"
39 static GList
*shell_history
= NULL
;
41 /* Static prototypes */
42 static gint
key_press_event(GtkWidget
*widget
,
44 FilerWindow
*filer_window
);
45 static void changed(GtkEditable
*mini
, FilerWindow
*filer_window
);
46 static void find_next_match(FilerWindow
*filer_window
, char *pattern
, int dir
);
47 static gboolean
matches(Collection
*collection
, int item
, char *pattern
);
48 static void search_in_dir(FilerWindow
*filer_window
, int dir
);
51 /****************************************************************
52 * EXTERNAL INTERFACE *
53 ****************************************************************/
56 GtkWidget
*create_minibuffer(FilerWindow
*filer_window
)
60 mini
= gtk_entry_new();
61 gtk_signal_connect(GTK_OBJECT(mini
), "key_press_event",
62 GTK_SIGNAL_FUNC(key_press_event
), filer_window
);
63 gtk_signal_connect(GTK_OBJECT(mini
), "changed",
64 GTK_SIGNAL_FUNC(changed
), filer_window
);
69 void minibuffer_show(FilerWindow
*filer_window
, MiniType mini_type
)
71 Collection
*collection
;
74 g_return_if_fail(filer_window
!= NULL
);
75 g_return_if_fail(filer_window
->minibuffer
!= NULL
);
77 mini
= GTK_ENTRY(filer_window
->minibuffer
);
79 filer_window
->mini_type
= MINI_NONE
;
84 collection
= filer_window
->collection
;
85 filer_window
->mini_cursor_base
=
86 MAX(collection
->cursor_item
, 0);
87 gtk_entry_set_text(mini
,
88 make_path(filer_window
->path
, "")->str
);
91 filer_window
->mini_cursor_base
= -1; /* History */
92 gtk_entry_set_text(mini
, "");
95 g_warning("Bad minibuffer type\n");
99 filer_window
->mini_type
= mini_type
;
101 gtk_entry_set_position(mini
, -1);
103 gtk_widget_show(filer_window
->minibuffer
);
105 gtk_window_set_focus(GTK_WINDOW(filer_window
->window
),
106 filer_window
->minibuffer
);
109 void minibuffer_hide(FilerWindow
*filer_window
)
111 filer_window
->mini_type
= MINI_NONE
;
113 gtk_widget_hide(filer_window
->minibuffer
);
114 gtk_window_set_focus(GTK_WINDOW(filer_window
->window
),
115 GTK_WIDGET(filer_window
->collection
));
119 /****************************************************************
120 * INTERNAL FUNCTIONS *
121 ****************************************************************/
125 static void path_return_pressed(FilerWindow
*filer_window
, GdkEventKey
*event
)
127 Collection
*collection
= filer_window
->collection
;
128 int item
= collection
->cursor_item
;
129 char *path
, *pattern
;
130 int flags
= OPEN_FROM_MINI
| OPEN_SAME_WINDOW
;
132 path
= gtk_entry_get_text(GTK_ENTRY(filer_window
->minibuffer
));
133 pattern
= strrchr(path
, '/');
139 if (item
== -1 || !matches(collection
, item
, pattern
))
145 if ((event
->state
& GDK_SHIFT_MASK
) != 0)
148 filer_openitem(filer_window
, item
, flags
);
151 /* Use the cursor item to fill in the minibuffer.
152 * If there are multiple matches the fill in as much as possible and beep.
154 static void complete(FilerWindow
*filer_window
)
157 Collection
*collection
= filer_window
->collection
;
158 int cursor
= collection
->cursor_item
;
160 int shortest_stem
= -1;
165 if (cursor
< 0 || cursor
>= collection
->number_of_items
)
171 entry
= GTK_ENTRY(filer_window
->minibuffer
);
173 item
= (DirItem
*) collection
->items
[cursor
].data
;
175 text
= gtk_entry_get_text(entry
);
176 leaf
= strrchr(text
, '/');
184 if (!matches(collection
, cursor
, leaf
))
190 current_stem
= strlen(leaf
);
192 /* Find the longest other match of this name. It it's longer than
193 * the currently entered text then complete only up to that length.
195 for (other
= 0; other
< collection
->number_of_items
; other
++)
197 DirItem
*other_item
= (DirItem
*) collection
->items
[other
].data
;
203 while (other_item
->leafname
[stem
] && item
->leafname
[stem
])
205 if (other_item
->leafname
[stem
] != item
->leafname
[stem
])
210 /* stem is the index of the first difference */
211 if (stem
>= current_stem
&&
212 (shortest_stem
== -1 || stem
< shortest_stem
))
213 shortest_stem
= stem
;
216 if (current_stem
== shortest_stem
)
218 else if (current_stem
< shortest_stem
)
222 extra
= g_strndup(item
->leafname
+ current_stem
,
223 shortest_stem
- current_stem
);
224 gtk_entry_append_text(entry
, extra
);
232 new = make_path(filer_window
->path
, item
->leafname
);
234 if (item
->base_type
== TYPE_DIRECTORY
&&
235 (item
->flags
& ITEM_FLAG_APPDIR
) == 0)
236 g_string_append_c(new, '/');
238 gtk_entry_set_text(entry
, new->str
);
242 static void path_changed(GtkEditable
*mini
, FilerWindow
*filer_window
)
247 new = gtk_entry_get_text(GTK_ENTRY(mini
));
251 new = g_strdup_printf("/%s", new);
253 slash
= strrchr(new, '/');
261 real
= pathdup(path
);
263 if (strcmp(real
, filer_window
->path
) != 0)
266 if (mc_stat(real
, &info
) == 0 && S_ISDIR(info
.st_mode
))
267 filer_change_to(filer_window
, real
, slash
+ 1);
273 Collection
*collection
= filer_window
->collection
;
276 find_next_match(filer_window
, slash
+ 1, 0);
277 item
= collection
->cursor_item
;
278 if (item
!= -1 && !matches(collection
, item
, slash
+ 1))
286 /* Find the next item in the collection that matches 'pattern'. Start from
287 * mini_cursor_base and loop at either end. dir is 1 for a forward search,
288 * -1 for backwards. 0 means forwards, but may stay the same.
290 * Does not automatically update mini_cursor_base.
292 static void find_next_match(FilerWindow
*filer_window
, char *pattern
, int dir
)
294 Collection
*collection
= filer_window
->collection
;
295 int base
= filer_window
->mini_cursor_base
;
298 if (collection
->number_of_items
< 1)
301 if (base
< 0 || base
>= collection
->number_of_items
)
302 filer_window
->mini_cursor_base
= base
= 0;
306 /* Step to the next item */
309 if (item
>= collection
->number_of_items
)
312 item
= collection
->number_of_items
- 1;
316 else if (item
== base
)
317 break; /* No (other) matches at all */
320 } while (!matches(collection
, item
, pattern
));
322 collection_set_cursor_item(collection
, item
);
325 static gboolean
matches(Collection
*collection
, int item_number
, char *pattern
)
327 DirItem
*item
= (DirItem
*) collection
->items
[item_number
].data
;
329 return strncmp(item
->leafname
, pattern
, strlen(pattern
)) == 0;
332 /* Find next match and set base for future matches. */
333 static void search_in_dir(FilerWindow
*filer_window
, int dir
)
335 char *path
, *pattern
;
337 path
= gtk_entry_get_text(GTK_ENTRY(filer_window
->minibuffer
));
338 pattern
= strrchr(path
, '/');
344 filer_window
->mini_cursor_base
= filer_window
->collection
->cursor_item
;
345 find_next_match(filer_window
, pattern
, dir
);
346 filer_window
->mini_cursor_base
= filer_window
->collection
->cursor_item
;
351 static void shell_return_pressed(FilerWindow
*filer_window
, GdkEventKey
*event
)
356 Collection
*collection
= filer_window
->collection
;
358 entry
= gtk_entry_get_text(GTK_ENTRY(filer_window
->minibuffer
));
359 shell_history
= g_list_prepend(shell_history
, g_strdup(entry
));
361 argv
= g_ptr_array_new();
362 g_ptr_array_add(argv
, "sh");
363 g_ptr_array_add(argv
, "-c");
364 g_ptr_array_add(argv
, entry
);
365 g_ptr_array_add(argv
, "sh");
367 for (i
= 0; i
< collection
->number_of_items
; i
++)
369 DirItem
*item
= (DirItem
*) collection
->items
[i
].data
;
370 if (collection
->items
[i
].selected
)
371 g_ptr_array_add(argv
, item
->leafname
);
374 g_ptr_array_add(argv
, NULL
);
379 delayed_error("ROX-Filer", "Failed to create "
383 dup2(to_error_log
, STDOUT_FILENO
);
384 close_on_exec(STDOUT_FILENO
, FALSE
);
385 dup2(to_error_log
, STDERR_FILENO
);
386 close_on_exec(STDERR_FILENO
, FALSE
);
387 if (chdir(filer_window
->path
))
388 g_printerr("chdir(%s) failed: %s\n",
391 execvp((char *) argv
->pdata
[0],
392 (char **) argv
->pdata
);
393 g_printerr("execvp(%s, ...) failed: %s\n",
394 (char *) argv
->pdata
[0],
399 g_ptr_array_free(argv
, TRUE
);
401 minibuffer_hide(filer_window
);
404 /* Move through the shell history */
405 static void shell_recall(FilerWindow
*filer_window
, int dir
)
408 int pos
= filer_window
->mini_cursor_base
;
413 command
= g_list_nth_data(shell_history
, pos
);
422 filer_window
->mini_cursor_base
= pos
;
424 gtk_entry_set_text(GTK_ENTRY(filer_window
->minibuffer
), command
);
429 static gint
key_press_event(GtkWidget
*widget
,
431 FilerWindow
*filer_window
)
433 if (event
->keyval
== GDK_Escape
)
435 minibuffer_hide(filer_window
);
439 switch (filer_window
->mini_type
)
442 switch (event
->keyval
)
445 search_in_dir(filer_window
, -1);
448 search_in_dir(filer_window
, 1);
451 path_return_pressed(filer_window
,
455 complete(filer_window
);
463 switch (event
->keyval
)
466 shell_recall(filer_window
, 1);
469 shell_recall(filer_window
, -1);
474 shell_return_pressed(filer_window
,
484 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
), "key_press_event");
488 static void changed(GtkEditable
*mini
, FilerWindow
*filer_window
)
490 switch (filer_window
->mini_type
)
493 path_changed(mini
, filer_window
);