1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-1999. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
30 #include <sys/types.h>
32 #ifdef HAVE_SYS_PARAM_H
33 #include <sys/param.h>
50 #include "gdk/gdkkeysyms.h"
51 #include "gtkbutton.h"
53 #include "gtkfilesel.h"
58 #include "gtklistitem.h"
60 #include "gtkscrolledwindow.h"
61 #include "gtksignal.h"
64 #include "gtkmenuitem.h"
65 #include "gtkoptionmenu.h"
67 #include "gtkdialog.h"
70 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
77 #define mkdir(p,m) _mkdir(p)
79 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
82 #endif /* G_OS_WIN32 */
84 #endif /* G_OS_WIN32 || G_WITH_CYGWIN */
86 #define DIR_LIST_WIDTH 180
87 #define DIR_LIST_HEIGHT 180
88 #define FILE_LIST_WIDTH 180
89 #define FILE_LIST_HEIGHT 180
91 /* I've put this here so it doesn't get confused with the
92 * file completion interface */
93 typedef struct _HistoryCallbackArg HistoryCallbackArg;
95 struct _HistoryCallbackArg
102 typedef struct _CompletionState CompletionState;
103 typedef struct _CompletionDir CompletionDir;
104 typedef struct _CompletionDirSent CompletionDirSent;
105 typedef struct _CompletionDirEntry CompletionDirEntry;
106 typedef struct _CompletionUserDir CompletionUserDir;
107 typedef struct _PossibleCompletion PossibleCompletion;
109 /* Non-external file completion decls and structures */
111 /* A contant telling PRCS how many directories to cache. Its actually
112 * kept in a list, so the geometry isn't important. */
113 #define CMPL_DIRECTORY_CACHE_SIZE 10
115 /* A constant used to determine whether a substring was an exact
116 * match by first_diff_index()
118 #define PATTERN_MATCH -1
119 /* The arguments used by all fnmatch() calls below
121 #define FNMATCH_FLAGS (FNM_PATHNAME | FNM_PERIOD)
123 #define CMPL_ERRNO_TOO_LONG ((1<<16)-1)
125 /* This structure contains all the useful information about a directory
126 * for the purposes of filename completion. These structures are cached
127 * in the CompletionState struct. CompletionDir's are reference counted.
129 struct _CompletionDirSent
136 struct _CompletionDirEntry *entries;
139 struct _CompletionDir
141 CompletionDirSent *sent;
146 struct _CompletionDir *cmpl_parent;
151 /* This structure contains pairs of directory entry names with a flag saying
152 * whether or not they are a valid directory. NOTE: This information is used
153 * to provide the caller with information about whether to update its completions
154 * or try to open a file. Since directories are cached by the directory mtime,
155 * a symlink which points to an invalid file (which will not be a directory),
156 * will not be reevaluated if that file is created, unless the containing
157 * directory is touched. I consider this case to be worth ignoring (josh).
159 struct _CompletionDirEntry
165 struct _CompletionUserDir
171 struct _PossibleCompletion
173 /* accessible fields, all are accessed externally by functions
177 gint is_a_completion;
178 gboolean is_directory;
185 struct _CompletionState
187 gint last_valid_char;
189 gint updated_text_len;
190 gint updated_text_alloc;
191 gboolean re_complete;
193 gchar *user_dir_name_buffer;
194 gint user_directories_len;
196 gchar *last_completion_text;
198 gint user_completion_index; /* if >= 0, currently completing ~user */
200 struct _CompletionDir *completion_dir; /* directory completing from */
201 struct _CompletionDir *active_completion_dir;
203 struct _PossibleCompletion the_completion;
205 struct _CompletionDir *reference_dir; /* initial directory */
207 GList* directory_storage;
208 GList* directory_sent_storage;
210 struct _CompletionUserDir *user_directories;
214 /* File completion functions which would be external, were they used
215 * outside of this file.
218 static CompletionState* cmpl_init_state (void);
219 static void cmpl_free_state (CompletionState *cmpl_state);
220 static gint cmpl_state_okay (CompletionState* cmpl_state);
221 static gchar* cmpl_strerror (gint);
223 static PossibleCompletion* cmpl_completion_matches(gchar *text_to_complete,
224 gchar **remaining_text,
225 CompletionState *cmpl_state);
227 /* Returns a name for consideration, possibly a completion, this name
228 * will be invalid after the next call to cmpl_next_completion.
230 static char* cmpl_this_completion (PossibleCompletion*);
232 /* True if this completion matches the given text. Otherwise, this
233 * output can be used to have a list of non-completions.
235 static gint cmpl_is_a_completion (PossibleCompletion*);
237 /* True if the completion is a directory
239 static gboolean cmpl_is_directory (PossibleCompletion*);
241 /* Obtains the next completion, or NULL
243 static PossibleCompletion* cmpl_next_completion (CompletionState*);
245 /* Updating completions: the return value of cmpl_updated_text() will
246 * be text_to_complete completed as much as possible after the most
247 * recent call to cmpl_completion_matches. For the present
248 * application, this is the suggested replacement for the user's input
249 * string. You must CALL THIS AFTER ALL cmpl_text_completions have
252 static gchar* cmpl_updated_text (CompletionState* cmpl_state);
254 /* After updating, to see if the completion was a directory, call
255 * this. If it was, you should consider re-calling completion_matches.
257 static gboolean cmpl_updated_dir (CompletionState* cmpl_state);
259 /* Current location: if using file completion, return the current
260 * directory, from which file completion begins. More specifically,
261 * the cwd concatenated with all exact completions up to the last
262 * directory delimiter('/').
264 static gchar* cmpl_reference_position (CompletionState* cmpl_state);
266 /* backing up: if cmpl_completion_matches returns NULL, you may query
267 * the index of the last completable character into cmpl_updated_text.
269 static gint cmpl_last_valid_char (CompletionState* cmpl_state);
271 /* When the user selects a non-directory, call cmpl_completion_fullname
272 * to get the full name of the selected file.
274 static gchar* cmpl_completion_fullname (gchar*, CompletionState* cmpl_state);
277 /* Directory operations. */
278 static CompletionDir* open_ref_dir (gchar* text_to_complete,
279 gchar** remaining_text,
280 CompletionState* cmpl_state);
281 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
282 static gboolean check_dir (gchar *dir_name,
284 gboolean *stat_subdirs);
286 static CompletionDir* open_dir (gchar* dir_name,
287 CompletionState* cmpl_state);
289 static CompletionDir* open_user_dir (gchar* text_to_complete,
290 CompletionState *cmpl_state);
292 static CompletionDir* open_relative_dir (gchar* dir_name, CompletionDir* dir,
293 CompletionState *cmpl_state);
294 static CompletionDirSent* open_new_dir (gchar* dir_name,
296 gboolean stat_subdirs);
297 static gint correct_dir_fullname (CompletionDir* cmpl_dir);
298 static gint correct_parent (CompletionDir* cmpl_dir,
300 static gchar* find_parent_dir_fullname (gchar* dirname);
301 static CompletionDir* attach_dir (CompletionDirSent* sent,
303 CompletionState *cmpl_state);
304 static void free_dir_sent (CompletionDirSent* sent);
305 static void free_dir (CompletionDir *dir);
306 static void prune_memory_usage(CompletionState *cmpl_state);
308 /* Completion operations */
309 static PossibleCompletion* attempt_homedir_completion(gchar* text_to_complete,
310 CompletionState *cmpl_state);
311 static PossibleCompletion* attempt_file_completion(CompletionState *cmpl_state);
312 static CompletionDir* find_completion_dir(gchar* text_to_complete,
313 gchar** remaining_text,
314 CompletionState* cmpl_state);
315 static PossibleCompletion* append_completion_text(gchar* text,
316 CompletionState* cmpl_state);
318 static gint get_pwdb(CompletionState* cmpl_state);
319 static gint compare_user_dir(const void* a, const void* b);
321 static gint first_diff_index(gchar* pat, gchar* text);
322 static gint compare_cmpl_dir(const void* a, const void* b);
323 static void update_cmpl(PossibleCompletion* poss,
324 CompletionState* cmpl_state);
326 static void gtk_file_selection_class_init (GtkFileSelectionClass *klass);
327 static void gtk_file_selection_init (GtkFileSelection *filesel);
328 static void gtk_file_selection_destroy (GtkObject *object);
329 static gint gtk_file_selection_key_press (GtkWidget *widget,
333 static void gtk_file_selection_file_button (GtkWidget *widget,
336 GdkEventButton *bevent,
339 static void gtk_file_selection_dir_button (GtkWidget *widget,
342 GdkEventButton *bevent,
345 static void gtk_file_selection_populate (GtkFileSelection *fs,
348 static void gtk_file_selection_abort (GtkFileSelection *fs);
350 static void gtk_file_selection_update_history_menu (GtkFileSelection *fs,
353 static void gtk_file_selection_create_dir (GtkWidget *widget, gpointer data);
354 static void gtk_file_selection_delete_file (GtkWidget *widget, gpointer data);
355 static void gtk_file_selection_rename_file (GtkWidget *widget, gpointer data);
359 static GtkWindowClass *parent_class = NULL;
361 /* Saves errno when something cmpl does fails. */
362 static gint cmpl_errno;
366 * Take the path currently in the file selection
367 * entry field and translate as necessary from
368 * a WIN32 style to CYGWIN32 style path. For
369 * instance translate:
370 * x:\somepath\file.jpg
372 * //x/somepath/file.jpg
374 * Replace the path in the selection text field.
375 * Return a boolean value concerning whether a
376 * translation had to be made.
379 translate_win32_path(GtkFileSelection *filesel)
385 * Retrieve the current path
387 path = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
390 * Translate only if this looks like a DOS-ish
391 * path... First handle any drive letters.
393 if (isalpha(path[0]) && (path[1] == ':')) {
395 * This part kind of stinks... It isn't possible
396 * to know if there is enough space in the current
397 * string for the extra character required in this
398 * conversion. Assume that there isn't enough space
399 * and use the set function on the text field to
400 * set the newly created string.
404 newPath = g_malloc(strlen(path) + 2);
405 sprintf(newPath, "//%c/%s", path[0], (path + 3));
406 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), newPath);
413 * Now, replace backslashes with forward slashes
416 if (strchr(path, '\\')) {
418 for (index = 0; path[index] != '\0'; index++)
419 if (path[index] == '\\')
430 gtk_file_selection_get_type (void)
432 static GtkType file_selection_type = 0;
434 if (!file_selection_type)
436 static const GtkTypeInfo filesel_info =
439 sizeof (GtkFileSelection),
440 sizeof (GtkFileSelectionClass),
441 (GtkClassInitFunc) gtk_file_selection_class_init,
442 (GtkObjectInitFunc) gtk_file_selection_init,
443 /* reserved_1 */ NULL,
444 /* reserved_2 */ NULL,
445 (GtkClassInitFunc) NULL,
448 file_selection_type = gtk_type_unique (GTK_TYPE_WINDOW, &filesel_info);
451 return file_selection_type;
455 gtk_file_selection_class_init (GtkFileSelectionClass *class)
457 GtkObjectClass *object_class;
459 object_class = (GtkObjectClass*) class;
461 parent_class = gtk_type_class (GTK_TYPE_WINDOW);
463 object_class->destroy = gtk_file_selection_destroy;
467 gtk_file_selection_init (GtkFileSelection *filesel)
469 GtkWidget *entry_vbox;
471 GtkWidget *list_hbox;
472 GtkWidget *confirm_area;
473 GtkWidget *pulldown_hbox;
474 GtkWidget *scrolled_win;
477 char *file_title [2];
479 filesel->cmpl_state = cmpl_init_state ();
481 /* The dialog-sized vertical box */
482 filesel->main_vbox = gtk_vbox_new (FALSE, 10);
483 gtk_container_set_border_width (GTK_CONTAINER (filesel), 10);
484 gtk_container_add (GTK_CONTAINER (filesel), filesel->main_vbox);
485 gtk_widget_show (filesel->main_vbox);
487 /* The horizontal box containing create, rename etc. buttons */
488 filesel->button_area = gtk_hbutton_box_new ();
489 gtk_button_box_set_layout(GTK_BUTTON_BOX(filesel->button_area), GTK_BUTTONBOX_START);
490 gtk_button_box_set_spacing(GTK_BUTTON_BOX(filesel->button_area), 0);
491 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->button_area,
493 gtk_widget_show (filesel->button_area);
495 gtk_file_selection_show_fileop_buttons(filesel);
497 /* hbox for pulldown menu */
498 pulldown_hbox = gtk_hbox_new (TRUE, 5);
499 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), pulldown_hbox, FALSE, FALSE, 0);
500 gtk_widget_show (pulldown_hbox);
503 filesel->history_pulldown = gtk_option_menu_new ();
504 gtk_widget_show (filesel->history_pulldown);
505 gtk_box_pack_start (GTK_BOX (pulldown_hbox), filesel->history_pulldown,
508 /* The horizontal box containing the directory and file listboxes */
509 list_hbox = gtk_hbox_new (FALSE, 5);
510 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), list_hbox, TRUE, TRUE, 0);
511 gtk_widget_show (list_hbox);
513 /* The directories clist */
514 dir_title[0] = _("Directories");
516 filesel->dir_list = gtk_clist_new_with_titles (1, (gchar**) dir_title);
517 gtk_widget_set_usize (filesel->dir_list, DIR_LIST_WIDTH, DIR_LIST_HEIGHT);
518 gtk_signal_connect (GTK_OBJECT (filesel->dir_list), "select_row",
519 (GtkSignalFunc) gtk_file_selection_dir_button,
521 gtk_clist_set_column_auto_resize (GTK_CLIST (filesel->dir_list), 0, TRUE);
522 gtk_clist_column_titles_passive (GTK_CLIST (filesel->dir_list));
524 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
525 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->dir_list);
526 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
527 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
528 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 5);
529 gtk_box_pack_start (GTK_BOX (list_hbox), scrolled_win, TRUE, TRUE, 0);
530 gtk_widget_show (filesel->dir_list);
531 gtk_widget_show (scrolled_win);
533 /* The files clist */
534 file_title[0] = _("Files");
535 file_title[1] = NULL;
536 filesel->file_list = gtk_clist_new_with_titles (1, (gchar**) file_title);
537 gtk_widget_set_usize (filesel->file_list, FILE_LIST_WIDTH, FILE_LIST_HEIGHT);
538 gtk_signal_connect (GTK_OBJECT (filesel->file_list), "select_row",
539 (GtkSignalFunc) gtk_file_selection_file_button,
541 gtk_clist_set_column_auto_resize (GTK_CLIST (filesel->file_list), 0, TRUE);
542 gtk_clist_column_titles_passive (GTK_CLIST (filesel->file_list));
544 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
545 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->file_list);
546 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
547 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
548 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 5);
549 gtk_box_pack_start (GTK_BOX (list_hbox), scrolled_win, TRUE, TRUE, 0);
550 gtk_widget_show (filesel->file_list);
551 gtk_widget_show (scrolled_win);
553 /* action area for packing buttons into. */
554 filesel->action_area = gtk_hbox_new (TRUE, 0);
555 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->action_area,
557 gtk_widget_show (filesel->action_area);
559 /* The OK/Cancel button area */
560 confirm_area = gtk_hbutton_box_new ();
561 gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area), GTK_BUTTONBOX_END);
562 gtk_button_box_set_spacing(GTK_BUTTON_BOX(confirm_area), 5);
563 gtk_box_pack_end (GTK_BOX (filesel->main_vbox), confirm_area, FALSE, FALSE, 0);
564 gtk_widget_show (confirm_area);
567 filesel->ok_button = gtk_button_new_with_label (_("OK"));
568 GTK_WIDGET_SET_FLAGS (filesel->ok_button, GTK_CAN_DEFAULT);
569 gtk_box_pack_start (GTK_BOX (confirm_area), filesel->ok_button, TRUE, TRUE, 0);
570 gtk_widget_grab_default (filesel->ok_button);
571 gtk_widget_show (filesel->ok_button);
573 /* The Cancel button */
574 filesel->cancel_button = gtk_button_new_with_label (_("Cancel"));
575 GTK_WIDGET_SET_FLAGS (filesel->cancel_button, GTK_CAN_DEFAULT);
576 gtk_box_pack_start (GTK_BOX (confirm_area), filesel->cancel_button, TRUE, TRUE, 0);
577 gtk_widget_show (filesel->cancel_button);
579 /* The selection entry widget */
580 entry_vbox = gtk_vbox_new (FALSE, 2);
581 gtk_box_pack_end (GTK_BOX (filesel->main_vbox), entry_vbox, FALSE, FALSE, 0);
582 gtk_widget_show (entry_vbox);
584 filesel->selection_text = label = gtk_label_new ("");
585 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
586 gtk_box_pack_start (GTK_BOX (entry_vbox), label, FALSE, FALSE, 0);
587 gtk_widget_show (label);
589 filesel->selection_entry = gtk_entry_new ();
590 gtk_signal_connect (GTK_OBJECT (filesel->selection_entry), "key_press_event",
591 (GtkSignalFunc) gtk_file_selection_key_press, filesel);
592 gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "focus_in_event",
593 (GtkSignalFunc) gtk_widget_grab_default,
594 GTK_OBJECT (filesel->ok_button));
595 gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "activate",
596 (GtkSignalFunc) gtk_button_clicked,
597 GTK_OBJECT (filesel->ok_button));
598 gtk_box_pack_start (GTK_BOX (entry_vbox), filesel->selection_entry, TRUE, TRUE, 0);
599 gtk_widget_show (filesel->selection_entry);
601 if (!cmpl_state_okay (filesel->cmpl_state))
605 sprintf (err_buf, _("Directory unreadable: %s"), cmpl_strerror (cmpl_errno));
607 gtk_label_set_text (GTK_LABEL (filesel->selection_text), err_buf);
611 gtk_file_selection_populate (filesel, "", FALSE);
614 gtk_widget_grab_focus (filesel->selection_entry);
618 gtk_file_selection_new (const gchar *title)
620 GtkFileSelection *filesel;
622 filesel = gtk_type_new (GTK_TYPE_FILE_SELECTION);
623 gtk_window_set_title (GTK_WINDOW (filesel), title);
625 return GTK_WIDGET (filesel);
629 gtk_file_selection_show_fileop_buttons (GtkFileSelection *filesel)
631 g_return_if_fail (filesel != NULL);
632 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
634 /* delete, create directory, and rename */
635 if (!filesel->fileop_c_dir)
637 filesel->fileop_c_dir = gtk_button_new_with_label (_("Create Dir"));
638 gtk_signal_connect (GTK_OBJECT (filesel->fileop_c_dir), "clicked",
639 (GtkSignalFunc) gtk_file_selection_create_dir,
641 gtk_box_pack_start (GTK_BOX (filesel->button_area),
642 filesel->fileop_c_dir, TRUE, TRUE, 0);
643 gtk_widget_show (filesel->fileop_c_dir);
646 if (!filesel->fileop_del_file)
648 filesel->fileop_del_file = gtk_button_new_with_label (_("Delete File"));
649 gtk_signal_connect (GTK_OBJECT (filesel->fileop_del_file), "clicked",
650 (GtkSignalFunc) gtk_file_selection_delete_file,
652 gtk_box_pack_start (GTK_BOX (filesel->button_area),
653 filesel->fileop_del_file, TRUE, TRUE, 0);
654 gtk_widget_show (filesel->fileop_del_file);
657 if (!filesel->fileop_ren_file)
659 filesel->fileop_ren_file = gtk_button_new_with_label (_("Rename File"));
660 gtk_signal_connect (GTK_OBJECT (filesel->fileop_ren_file), "clicked",
661 (GtkSignalFunc) gtk_file_selection_rename_file,
663 gtk_box_pack_start (GTK_BOX (filesel->button_area),
664 filesel->fileop_ren_file, TRUE, TRUE, 0);
665 gtk_widget_show (filesel->fileop_ren_file);
668 gtk_widget_queue_resize(GTK_WIDGET(filesel));
672 gtk_file_selection_hide_fileop_buttons (GtkFileSelection *filesel)
674 g_return_if_fail (filesel != NULL);
675 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
677 if (filesel->fileop_ren_file)
679 gtk_widget_destroy (filesel->fileop_ren_file);
680 filesel->fileop_ren_file = NULL;
683 if (filesel->fileop_del_file)
685 gtk_widget_destroy (filesel->fileop_del_file);
686 filesel->fileop_del_file = NULL;
689 if (filesel->fileop_c_dir)
691 gtk_widget_destroy (filesel->fileop_c_dir);
692 filesel->fileop_c_dir = NULL;
699 gtk_file_selection_set_filename (GtkFileSelection *filesel,
700 const gchar *filename)
703 const char *name, *last_slash;
705 g_return_if_fail (filesel != NULL);
706 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
707 g_return_if_fail (filename != NULL);
709 last_slash = strrchr (filename, G_DIR_SEPARATOR);
718 buf = g_strdup (filename);
719 buf[last_slash - filename + 1] = 0;
720 name = last_slash + 1;
723 gtk_file_selection_populate (filesel, buf, FALSE);
725 if (filesel->selection_entry)
726 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), name);
731 gtk_file_selection_get_filename (GtkFileSelection *filesel)
733 static gchar nothing[2] = "";
734 static gchar something[MAXPATHLEN*2];
738 g_return_val_if_fail (filesel != NULL, nothing);
739 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), nothing);
742 translate_win32_path(filesel);
744 text = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
747 filename = g_filename_from_utf8 (cmpl_completion_fullname (text, filesel->cmpl_state));
748 strncpy (something, filename, sizeof (something));
757 gtk_file_selection_complete (GtkFileSelection *filesel,
758 const gchar *pattern)
760 g_return_if_fail (filesel != NULL);
761 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
762 g_return_if_fail (pattern != NULL);
764 if (filesel->selection_entry)
765 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), pattern);
766 gtk_file_selection_populate (filesel, (gchar*) pattern, TRUE);
770 gtk_file_selection_destroy (GtkObject *object)
772 GtkFileSelection *filesel;
774 HistoryCallbackArg *callback_arg;
776 g_return_if_fail (GTK_IS_FILE_SELECTION (object));
778 filesel = GTK_FILE_SELECTION (object);
780 if (filesel->fileop_dialog)
782 gtk_widget_destroy (filesel->fileop_dialog);
783 filesel->fileop_dialog = NULL;
786 if (filesel->history_list)
788 list = filesel->history_list;
791 callback_arg = list->data;
792 g_free (callback_arg->directory);
793 g_free (callback_arg);
796 g_list_free (filesel->history_list);
797 filesel->history_list = NULL;
800 if (filesel->cmpl_state)
802 cmpl_free_state (filesel->cmpl_state);
803 filesel->cmpl_state = NULL;
806 GTK_OBJECT_CLASS (parent_class)->destroy (object);
809 /* Begin file operations callbacks */
812 gtk_file_selection_fileop_error (GtkFileSelection *fs, gchar *error_message)
819 g_return_if_fail (error_message != NULL);
822 dialog = gtk_dialog_new ();
824 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
825 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
828 gtk_window_set_title (GTK_WINDOW (dialog), _("Error"));
829 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
831 /* If file dialog is grabbed, make this dialog modal too */
832 /* When error dialog is closed, file dialog will be grabbed again */
833 if (GTK_WINDOW(fs)->modal)
834 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
836 vbox = gtk_vbox_new(FALSE, 0);
837 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
838 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
840 gtk_widget_show(vbox);
842 label = gtk_label_new(error_message);
843 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
844 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
845 gtk_widget_show(label);
847 /* yes, we free it */
848 g_free (error_message);
851 button = gtk_button_new_with_label (_("Close"));
852 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
853 (GtkSignalFunc) gtk_widget_destroy,
855 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
856 button, TRUE, TRUE, 0);
857 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
858 gtk_widget_grab_default(button);
859 gtk_widget_show (button);
861 gtk_widget_show (dialog);
865 gtk_file_selection_fileop_destroy (GtkWidget *widget, gpointer data)
867 GtkFileSelection *fs = data;
869 g_return_if_fail (fs != NULL);
870 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
872 fs->fileop_dialog = NULL;
877 gtk_file_selection_create_dir_confirmed (GtkWidget *widget, gpointer data)
879 GtkFileSelection *fs = data;
884 CompletionState *cmpl_state;
886 g_return_if_fail (fs != NULL);
887 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
889 dirname = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
890 cmpl_state = (CompletionState*) fs->cmpl_state;
891 path = cmpl_reference_position (cmpl_state);
893 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, dirname, NULL);
894 if ( (mkdir (full_path, 0755) < 0) )
896 buf = g_strconcat ("Error creating directory \"", dirname, "\": ",
897 g_strerror(errno), NULL);
898 gtk_file_selection_fileop_error (fs, buf);
902 gtk_widget_destroy (fs->fileop_dialog);
903 gtk_file_selection_populate (fs, "", FALSE);
907 gtk_file_selection_create_dir (GtkWidget *widget, gpointer data)
909 GtkFileSelection *fs = data;
915 g_return_if_fail (fs != NULL);
916 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
918 if (fs->fileop_dialog)
922 dialog = gtk_dialog_new ();
923 fs->fileop_dialog = dialog;
924 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
925 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
927 gtk_window_set_title (GTK_WINDOW (dialog), _("Create Directory"));
928 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
930 /* If file dialog is grabbed, grab option dialog */
931 /* When option dialog is closed, file dialog will be grabbed again */
932 if (GTK_WINDOW(fs)->modal)
933 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
935 vbox = gtk_vbox_new(FALSE, 0);
936 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
937 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
939 gtk_widget_show(vbox);
941 label = gtk_label_new(_("Directory name:"));
942 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
943 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
944 gtk_widget_show(label);
946 /* The directory entry widget */
947 fs->fileop_entry = gtk_entry_new ();
948 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
950 GTK_WIDGET_SET_FLAGS(fs->fileop_entry, GTK_CAN_DEFAULT);
951 gtk_widget_show (fs->fileop_entry);
954 button = gtk_button_new_with_label (_("Create"));
955 gtk_signal_connect (GTK_OBJECT (button), "clicked",
956 (GtkSignalFunc) gtk_file_selection_create_dir_confirmed,
958 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
959 button, TRUE, TRUE, 0);
960 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
961 gtk_widget_show(button);
963 button = gtk_button_new_with_label (_("Cancel"));
964 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
965 (GtkSignalFunc) gtk_widget_destroy,
967 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
968 button, TRUE, TRUE, 0);
969 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
970 gtk_widget_grab_default(button);
971 gtk_widget_show (button);
973 gtk_widget_show (dialog);
977 gtk_file_selection_delete_file_confirmed (GtkWidget *widget, gpointer data)
979 GtkFileSelection *fs = data;
980 CompletionState *cmpl_state;
985 g_return_if_fail (fs != NULL);
986 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
988 cmpl_state = (CompletionState*) fs->cmpl_state;
989 path = cmpl_reference_position (cmpl_state);
991 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
992 if ( (unlink (full_path) < 0) )
994 buf = g_strconcat ("Error deleting file \"", fs->fileop_file, "\": ",
995 g_strerror(errno), NULL);
996 gtk_file_selection_fileop_error (fs, buf);
1000 gtk_widget_destroy (fs->fileop_dialog);
1001 gtk_file_selection_populate (fs, "", FALSE);
1005 gtk_file_selection_delete_file (GtkWidget *widget, gpointer data)
1007 GtkFileSelection *fs = data;
1015 g_return_if_fail (fs != NULL);
1016 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1018 if (fs->fileop_dialog)
1021 #ifdef G_WITH_CYGWIN
1022 translate_win32_path(fs);
1025 filename = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1026 if (strlen(filename) < 1)
1029 fs->fileop_file = filename;
1032 fs->fileop_dialog = dialog = gtk_dialog_new ();
1033 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
1034 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
1036 gtk_window_set_title (GTK_WINDOW (dialog), _("Delete File"));
1037 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1039 /* If file dialog is grabbed, grab option dialog */
1040 /* When option dialog is closed, file dialog will be grabbed again */
1041 if (GTK_WINDOW(fs)->modal)
1042 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
1044 vbox = gtk_vbox_new(FALSE, 0);
1045 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1046 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
1048 gtk_widget_show(vbox);
1050 buf = g_strconcat ("Really delete file \"", filename, "\" ?", NULL);
1051 label = gtk_label_new(buf);
1052 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
1053 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
1054 gtk_widget_show(label);
1058 button = gtk_button_new_with_label (_("Delete"));
1059 gtk_signal_connect (GTK_OBJECT (button), "clicked",
1060 (GtkSignalFunc) gtk_file_selection_delete_file_confirmed,
1062 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1063 button, TRUE, TRUE, 0);
1064 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1065 gtk_widget_show(button);
1067 button = gtk_button_new_with_label (_("Cancel"));
1068 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1069 (GtkSignalFunc) gtk_widget_destroy,
1071 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1072 button, TRUE, TRUE, 0);
1073 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1074 gtk_widget_grab_default(button);
1075 gtk_widget_show (button);
1077 gtk_widget_show (dialog);
1082 gtk_file_selection_rename_file_confirmed (GtkWidget *widget, gpointer data)
1084 GtkFileSelection *fs = data;
1088 gchar *new_filename;
1089 gchar *old_filename;
1090 CompletionState *cmpl_state;
1092 g_return_if_fail (fs != NULL);
1093 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1095 file = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
1096 cmpl_state = (CompletionState*) fs->cmpl_state;
1097 path = cmpl_reference_position (cmpl_state);
1099 new_filename = g_strconcat (path, G_DIR_SEPARATOR_S, file, NULL);
1100 old_filename = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1102 if ( (rename (old_filename, new_filename)) < 0)
1104 buf = g_strconcat ("Error renaming file \"", file, "\": ",
1105 g_strerror(errno), NULL);
1106 gtk_file_selection_fileop_error (fs, buf);
1108 g_free (new_filename);
1109 g_free (old_filename);
1111 gtk_widget_destroy (fs->fileop_dialog);
1112 gtk_file_selection_populate (fs, "", FALSE);
1116 gtk_file_selection_rename_file (GtkWidget *widget, gpointer data)
1118 GtkFileSelection *fs = data;
1125 g_return_if_fail (fs != NULL);
1126 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1128 if (fs->fileop_dialog)
1131 fs->fileop_file = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1132 if (strlen(fs->fileop_file) < 1)
1136 fs->fileop_dialog = dialog = gtk_dialog_new ();
1137 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
1138 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
1140 gtk_window_set_title (GTK_WINDOW (dialog), _("Rename File"));
1141 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1143 /* If file dialog is grabbed, grab option dialog */
1144 /* When option dialog closed, file dialog will be grabbed again */
1145 if (GTK_WINDOW(fs)->modal)
1146 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
1148 vbox = gtk_vbox_new(FALSE, 0);
1149 gtk_container_set_border_width (GTK_CONTAINER(vbox), 8);
1150 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
1152 gtk_widget_show(vbox);
1154 buf = g_strconcat ("Rename file \"", fs->fileop_file, "\" to:", NULL);
1155 label = gtk_label_new(buf);
1156 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
1157 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
1158 gtk_widget_show(label);
1161 /* New filename entry */
1162 fs->fileop_entry = gtk_entry_new ();
1163 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1165 GTK_WIDGET_SET_FLAGS(fs->fileop_entry, GTK_CAN_DEFAULT);
1166 gtk_widget_show (fs->fileop_entry);
1168 gtk_entry_set_text (GTK_ENTRY (fs->fileop_entry), fs->fileop_file);
1169 gtk_editable_select_region (GTK_EDITABLE (fs->fileop_entry),
1170 0, strlen (fs->fileop_file));
1173 button = gtk_button_new_with_label (_("Rename"));
1174 gtk_signal_connect (GTK_OBJECT (button), "clicked",
1175 (GtkSignalFunc) gtk_file_selection_rename_file_confirmed,
1177 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1178 button, TRUE, TRUE, 0);
1179 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1180 gtk_widget_show(button);
1182 button = gtk_button_new_with_label (_("Cancel"));
1183 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1184 (GtkSignalFunc) gtk_widget_destroy,
1186 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1187 button, TRUE, TRUE, 0);
1188 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1189 gtk_widget_grab_default(button);
1190 gtk_widget_show (button);
1192 gtk_widget_show (dialog);
1197 gtk_file_selection_key_press (GtkWidget *widget,
1201 GtkFileSelection *fs;
1204 g_return_val_if_fail (widget != NULL, FALSE);
1205 g_return_val_if_fail (event != NULL, FALSE);
1207 if (event->keyval == GDK_Tab)
1209 fs = GTK_FILE_SELECTION (user_data);
1210 #ifdef G_WITH_CYGWIN
1211 translate_win32_path(fs);
1213 text = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1215 text = g_strdup (text);
1217 gtk_file_selection_populate (fs, text, TRUE);
1221 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "key_press_event");
1231 gtk_file_selection_history_callback (GtkWidget *widget, gpointer data)
1233 GtkFileSelection *fs = data;
1234 HistoryCallbackArg *callback_arg;
1237 g_return_if_fail (fs != NULL);
1238 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1240 list = fs->history_list;
1243 callback_arg = list->data;
1245 if (callback_arg->menu_item == widget)
1247 gtk_file_selection_populate (fs, callback_arg->directory, FALSE);
1256 gtk_file_selection_update_history_menu (GtkFileSelection *fs,
1257 gchar *current_directory)
1259 HistoryCallbackArg *callback_arg;
1260 GtkWidget *menu_item;
1266 g_return_if_fail (fs != NULL);
1267 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1268 g_return_if_fail (current_directory != NULL);
1270 list = fs->history_list;
1272 if (fs->history_menu)
1275 callback_arg = list->data;
1276 g_free (callback_arg->directory);
1277 g_free (callback_arg);
1280 g_list_free (fs->history_list);
1281 fs->history_list = NULL;
1283 gtk_widget_destroy (fs->history_menu);
1286 fs->history_menu = gtk_menu_new();
1288 current_dir = g_strdup (current_directory);
1290 dir_len = strlen (current_dir);
1292 for (i = dir_len; i >= 0; i--)
1294 /* the i == dir_len is to catch the full path for the first
1296 if ( (current_dir[i] == G_DIR_SEPARATOR) || (i == dir_len))
1298 /* another small hack to catch the full path */
1300 current_dir[i + 1] = '\0';
1301 #ifdef G_WITH_CYGWIN
1302 if (!strcmp(current_dir, "//"))
1305 menu_item = gtk_menu_item_new_with_label (current_dir);
1307 callback_arg = g_new (HistoryCallbackArg, 1);
1308 callback_arg->menu_item = menu_item;
1310 /* since the autocompletion gets confused if you don't
1311 * supply a trailing '/' on a dir entry, set the full
1312 * (current) path to "" which just refreshes the filesel */
1315 callback_arg->directory = g_strdup ("");
1319 callback_arg->directory = g_strdup (current_dir);
1322 fs->history_list = g_list_append (fs->history_list, callback_arg);
1324 gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
1325 (GtkSignalFunc) gtk_file_selection_history_callback,
1327 gtk_menu_append (GTK_MENU (fs->history_menu), menu_item);
1328 gtk_widget_show (menu_item);
1332 gtk_option_menu_set_menu (GTK_OPTION_MENU (fs->history_pulldown),
1334 g_free (current_dir);
1338 gtk_file_selection_file_button (GtkWidget *widget,
1341 GdkEventButton *bevent,
1344 GtkFileSelection *fs = NULL;
1345 gchar *filename, *temp = NULL;
1347 g_return_if_fail (GTK_IS_CLIST (widget));
1350 g_return_if_fail (fs != NULL);
1351 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1353 gtk_clist_get_text (GTK_CLIST (fs->file_list), row, 0, &temp);
1354 filename = g_strdup (temp);
1356 #ifdef G_WITH_CYGWIN
1357 /* Check to see if the selection was a drive selector */
1358 if (isalpha(filename[0]) && (filename[1] == ':')) {
1359 /* It is... map it to a CYGWIN32 drive */
1360 char temp_filename[10];
1362 sprintf(temp_filename, "//%c/", tolower(filename[0]));
1364 filename = g_strdup(temp_filename);
1366 #endif /* G_WITH_CYGWIN */
1371 switch (bevent->type)
1373 case GDK_2BUTTON_PRESS:
1374 gtk_button_clicked (GTK_BUTTON (fs->ok_button));
1378 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1382 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1389 gtk_file_selection_dir_button (GtkWidget *widget,
1392 GdkEventButton *bevent,
1395 GtkFileSelection *fs = NULL;
1396 gchar *filename, *temp = NULL;
1398 g_return_if_fail (GTK_IS_CLIST (widget));
1400 fs = GTK_FILE_SELECTION (user_data);
1401 g_return_if_fail (fs != NULL);
1402 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1404 gtk_clist_get_text (GTK_CLIST (fs->dir_list), row, 0, &temp);
1405 filename = g_strdup (temp);
1410 switch (bevent->type)
1412 case GDK_2BUTTON_PRESS:
1413 gtk_file_selection_populate (fs, filename, FALSE);
1417 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1421 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1427 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
1430 win32_gtk_add_drives_to_dir_list(GtkWidget *the_dir_list)
1432 gchar *text[2], *textPtr;
1434 char volumeNameBuf[128];
1435 char formatBuffer[128];
1439 /* Get the Drives string */
1440 GetLogicalDriveStrings(sizeof(buffer), buffer);
1442 /* Add the drives as necessary */
1444 while (*textPtr != '\0') {
1445 /* Get the volume information for this drive */
1446 if ((tolower(textPtr[0]) != 'a') && (tolower(textPtr[0]) != 'b'))
1448 /* Ignore floppies (?) */
1449 DWORD maxComponentLength, flags;
1451 GetVolumeInformation(textPtr,
1452 volumeNameBuf, sizeof(volumeNameBuf),
1453 NULL, &maxComponentLength,
1455 /* Build the actual displayable string */
1457 sprintf(formatBuffer, "%c:\\", toupper(textPtr[0]));
1458 #if 0 /* HB: removed to allow drive change AND directory update with one click */
1459 if (strlen(volumeNameBuf) > 0)
1460 sprintf(formatBuffer, "%s (%s)", formatBuffer, volumeNameBuf);
1462 /* Add to the list */
1463 text[0] = formatBuffer;
1464 gtk_clist_append (GTK_CLIST (the_dir_list), text);
1466 textPtr += (strlen(textPtr) + 1);
1472 gtk_file_selection_populate (GtkFileSelection *fs,
1476 CompletionState *cmpl_state;
1477 PossibleCompletion* poss;
1479 gchar* rem_path = rel_path;
1482 gint did_recurse = FALSE;
1483 gint possible_count = 0;
1484 gint selection_index = -1;
1486 g_return_if_fail (fs != NULL);
1487 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1489 cmpl_state = (CompletionState*) fs->cmpl_state;
1490 poss = cmpl_completion_matches (rel_path, &rem_path, cmpl_state);
1492 if (!cmpl_state_okay (cmpl_state))
1494 /* Something went wrong. */
1495 gtk_file_selection_abort (fs);
1499 g_assert (cmpl_state->reference_dir);
1501 gtk_clist_freeze (GTK_CLIST (fs->dir_list));
1502 gtk_clist_clear (GTK_CLIST (fs->dir_list));
1503 gtk_clist_freeze (GTK_CLIST (fs->file_list));
1504 gtk_clist_clear (GTK_CLIST (fs->file_list));
1506 /* Set the dir_list to include ./ and ../ */
1508 text[0] = "." G_DIR_SEPARATOR_S;
1509 gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1511 text[0] = ".." G_DIR_SEPARATOR_S;
1512 gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1516 if (cmpl_is_a_completion (poss))
1518 possible_count += 1;
1520 filename = cmpl_this_completion (poss);
1524 if (cmpl_is_directory (poss))
1526 if (strcmp (filename, "." G_DIR_SEPARATOR_S) != 0 &&
1527 strcmp (filename, ".." G_DIR_SEPARATOR_S) != 0)
1528 gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1532 gtk_clist_append (GTK_CLIST (fs->file_list), text);
1536 poss = cmpl_next_completion (cmpl_state);
1539 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
1540 /* For Windows, add drives as potential selections */
1541 win32_gtk_add_drives_to_dir_list (fs->dir_list);
1544 gtk_clist_thaw (GTK_CLIST (fs->dir_list));
1545 gtk_clist_thaw (GTK_CLIST (fs->file_list));
1547 /* File lists are set. */
1549 g_assert (cmpl_state->reference_dir);
1554 /* User is trying to complete filenames, so advance the user's input
1555 * string to the updated_text, which is the common leading substring
1556 * of all possible completions, and if its a directory attempt
1557 * attempt completions in it. */
1559 if (cmpl_updated_text (cmpl_state)[0])
1562 if (cmpl_updated_dir (cmpl_state))
1564 gchar* dir_name = g_strdup (cmpl_updated_text (cmpl_state));
1568 gtk_file_selection_populate (fs, dir_name, TRUE);
1574 if (fs->selection_entry)
1575 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry),
1576 cmpl_updated_text (cmpl_state));
1581 selection_index = cmpl_last_valid_char (cmpl_state) -
1582 (strlen (rel_path) - strlen (rem_path));
1583 if (fs->selection_entry)
1584 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), rem_path);
1589 if (fs->selection_entry)
1590 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
1595 if (fs->selection_entry)
1596 gtk_entry_set_position (GTK_ENTRY (fs->selection_entry), selection_index);
1598 if (fs->selection_entry)
1600 sel_text = g_strconcat (_("Selection: "),
1601 cmpl_reference_position (cmpl_state),
1604 gtk_label_set_text (GTK_LABEL (fs->selection_text), sel_text);
1608 if (fs->history_pulldown)
1610 gtk_file_selection_update_history_menu (fs, cmpl_reference_position (cmpl_state));
1617 gtk_file_selection_abort (GtkFileSelection *fs)
1621 sprintf (err_buf, _("Directory unreadable: %s"), cmpl_strerror (cmpl_errno));
1623 /* BEEP gdk_beep(); */
1625 if (fs->selection_entry)
1626 gtk_label_set_text (GTK_LABEL (fs->selection_text), err_buf);
1629 /**********************************************************************/
1630 /* External Interface */
1631 /**********************************************************************/
1633 /* The four completion state selectors
1636 cmpl_updated_text (CompletionState* cmpl_state)
1638 return cmpl_state->updated_text;
1642 cmpl_updated_dir (CompletionState* cmpl_state)
1644 return cmpl_state->re_complete;
1648 cmpl_reference_position (CompletionState* cmpl_state)
1650 return cmpl_state->reference_dir->fullname;
1654 cmpl_last_valid_char (CompletionState* cmpl_state)
1656 return cmpl_state->last_valid_char;
1660 cmpl_completion_fullname (gchar* text, CompletionState* cmpl_state)
1662 static char nothing[2] = "";
1664 if (!cmpl_state_okay (cmpl_state))
1668 else if (g_path_is_absolute (text))
1670 strcpy (cmpl_state->updated_text, text);
1673 else if (text[0] == '~')
1678 dir = open_user_dir (text, cmpl_state);
1682 /* spencer says just return ~something, so
1683 * for now just do it. */
1684 strcpy (cmpl_state->updated_text, text);
1689 strcpy (cmpl_state->updated_text, dir->fullname);
1691 slash = strchr (text, G_DIR_SEPARATOR);
1694 strcat (cmpl_state->updated_text, slash);
1700 strcpy (cmpl_state->updated_text, cmpl_state->reference_dir->fullname);
1701 if (cmpl_state->updated_text[strlen (cmpl_state->updated_text) - 1] != G_DIR_SEPARATOR)
1702 strcat (cmpl_state->updated_text, G_DIR_SEPARATOR_S);
1703 strcat (cmpl_state->updated_text, text);
1706 return cmpl_state->updated_text;
1709 /* The three completion selectors
1712 cmpl_this_completion (PossibleCompletion* pc)
1718 cmpl_is_directory (PossibleCompletion* pc)
1720 return pc->is_directory;
1724 cmpl_is_a_completion (PossibleCompletion* pc)
1726 return pc->is_a_completion;
1729 /**********************************************************************/
1730 /* Construction, deletion */
1731 /**********************************************************************/
1733 static CompletionState*
1734 cmpl_init_state (void)
1737 CompletionState *new_state;
1739 new_state = g_new (CompletionState, 1);
1741 getcwd_buf = g_get_current_dir ();
1745 new_state->reference_dir = NULL;
1746 new_state->completion_dir = NULL;
1747 new_state->active_completion_dir = NULL;
1748 new_state->directory_storage = NULL;
1749 new_state->directory_sent_storage = NULL;
1750 new_state->last_valid_char = 0;
1751 new_state->updated_text = g_new (gchar, MAXPATHLEN);
1752 new_state->updated_text_alloc = MAXPATHLEN;
1753 new_state->the_completion.text = g_new (gchar, MAXPATHLEN);
1754 new_state->the_completion.text_alloc = MAXPATHLEN;
1755 new_state->user_dir_name_buffer = NULL;
1756 new_state->user_directories = NULL;
1758 new_state->reference_dir = open_dir (getcwd_buf, new_state);
1760 if (!new_state->reference_dir)
1762 /* Directories changing from underneath us, grumble */
1763 strcpy (getcwd_buf, G_DIR_SEPARATOR_S);
1767 g_free (getcwd_buf);
1772 cmpl_free_dir_list(GList* dp0)
1777 free_dir (dp->data);
1785 cmpl_free_dir_sent_list(GList* dp0)
1790 free_dir_sent (dp->data);
1798 cmpl_free_state (CompletionState* cmpl_state)
1800 g_return_if_fail (cmpl_state != NULL);
1802 cmpl_free_dir_list (cmpl_state->directory_storage);
1803 cmpl_free_dir_sent_list (cmpl_state->directory_sent_storage);
1805 if (cmpl_state->user_dir_name_buffer)
1806 g_free (cmpl_state->user_dir_name_buffer);
1807 if (cmpl_state->user_directories)
1808 g_free (cmpl_state->user_directories);
1809 if (cmpl_state->the_completion.text)
1810 g_free (cmpl_state->the_completion.text);
1811 if (cmpl_state->updated_text)
1812 g_free (cmpl_state->updated_text);
1814 g_free (cmpl_state);
1818 free_dir(CompletionDir* dir)
1820 g_free(dir->fullname);
1825 free_dir_sent(CompletionDirSent* sent)
1828 for (i = 0; i < sent->entry_count; i++)
1829 g_free(sent->entries[i].entry_name);
1830 g_free(sent->entries);
1835 prune_memory_usage(CompletionState *cmpl_state)
1837 GList* cdsl = cmpl_state->directory_sent_storage;
1838 GList* cdl = cmpl_state->directory_storage;
1842 for(; cdsl && len < CMPL_DIRECTORY_CACHE_SIZE; len += 1)
1846 cmpl_free_dir_sent_list(cdsl->next);
1850 cmpl_state->directory_storage = NULL;
1852 if (cdl->data == cmpl_state->reference_dir)
1853 cmpl_state->directory_storage = g_list_prepend(NULL, cdl->data);
1855 free_dir (cdl->data);
1862 /**********************************************************************/
1863 /* The main entrances. */
1864 /**********************************************************************/
1866 static PossibleCompletion*
1867 cmpl_completion_matches (gchar* text_to_complete,
1868 gchar** remaining_text,
1869 CompletionState* cmpl_state)
1872 PossibleCompletion *poss;
1874 prune_memory_usage(cmpl_state);
1876 g_assert (text_to_complete != NULL);
1878 cmpl_state->user_completion_index = -1;
1879 cmpl_state->last_completion_text = text_to_complete;
1880 cmpl_state->the_completion.text[0] = 0;
1881 cmpl_state->last_valid_char = 0;
1882 cmpl_state->updated_text_len = -1;
1883 cmpl_state->updated_text[0] = 0;
1884 cmpl_state->re_complete = FALSE;
1887 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
1889 if (text_to_complete[0] == '~' && !first_slash)
1891 /* Text starts with ~ and there is no slash, show all the
1892 * home directory completions.
1894 poss = attempt_homedir_completion (text_to_complete, cmpl_state);
1896 update_cmpl(poss, cmpl_state);
1901 cmpl_state->reference_dir =
1902 open_ref_dir (text_to_complete, remaining_text, cmpl_state);
1904 if(!cmpl_state->reference_dir)
1907 cmpl_state->completion_dir =
1908 find_completion_dir (*remaining_text, remaining_text, cmpl_state);
1910 cmpl_state->last_valid_char = *remaining_text - text_to_complete;
1912 if(!cmpl_state->completion_dir)
1915 cmpl_state->completion_dir->cmpl_index = -1;
1916 cmpl_state->completion_dir->cmpl_parent = NULL;
1917 cmpl_state->completion_dir->cmpl_text = *remaining_text;
1919 cmpl_state->active_completion_dir = cmpl_state->completion_dir;
1921 cmpl_state->reference_dir = cmpl_state->completion_dir;
1923 poss = attempt_file_completion(cmpl_state);
1925 update_cmpl(poss, cmpl_state);
1930 static PossibleCompletion*
1931 cmpl_next_completion (CompletionState* cmpl_state)
1933 PossibleCompletion* poss = NULL;
1935 cmpl_state->the_completion.text[0] = 0;
1938 if(cmpl_state->user_completion_index >= 0)
1939 poss = attempt_homedir_completion(cmpl_state->last_completion_text, cmpl_state);
1941 poss = attempt_file_completion(cmpl_state);
1943 poss = attempt_file_completion(cmpl_state);
1946 update_cmpl(poss, cmpl_state);
1951 /**********************************************************************/
1952 /* Directory Operations */
1953 /**********************************************************************/
1955 /* Open the directory where completion will begin from, if possible. */
1956 static CompletionDir*
1957 open_ref_dir(gchar* text_to_complete,
1958 gchar** remaining_text,
1959 CompletionState* cmpl_state)
1962 CompletionDir *new_dir;
1964 first_slash = strchr(text_to_complete, G_DIR_SEPARATOR);
1966 #ifdef G_WITH_CYGWIN
1967 if (text_to_complete[0] == '/' && text_to_complete[1] == '/')
1970 sprintf(root_dir, "//%c", text_to_complete[2]);
1972 new_dir = open_dir(root_dir, cmpl_state);
1975 *remaining_text = text_to_complete + 4;
1983 else if (text_to_complete[0] == '~')
1985 new_dir = open_user_dir(text_to_complete, cmpl_state);
1990 *remaining_text = first_slash + 1;
1992 *remaining_text = text_to_complete + strlen(text_to_complete);
2000 else if (g_path_is_absolute (text_to_complete) || !cmpl_state->reference_dir)
2002 gchar *tmp = g_strdup(text_to_complete);
2006 while (*p && *p != '*' && *p != '?')
2010 p = strrchr(tmp, G_DIR_SEPARATOR);
2018 new_dir = open_dir(tmp, cmpl_state);
2021 *remaining_text = text_to_complete +
2022 ((p == tmp + 1) ? (p - tmp) : (p + 1 - tmp));
2026 /* If no possible candidates, use the cwd */
2027 gchar *curdir = g_get_current_dir ();
2029 new_dir = open_dir(curdir, cmpl_state);
2032 *remaining_text = text_to_complete;
2041 *remaining_text = text_to_complete;
2043 new_dir = open_dir(cmpl_state->reference_dir->fullname, cmpl_state);
2048 new_dir->cmpl_index = -1;
2049 new_dir->cmpl_parent = NULL;
2057 /* open a directory by user name */
2058 static CompletionDir*
2059 open_user_dir(gchar* text_to_complete,
2060 CompletionState *cmpl_state)
2065 g_assert(text_to_complete && text_to_complete[0] == '~');
2067 first_slash = strchr(text_to_complete, G_DIR_SEPARATOR);
2070 cmp_len = first_slash - text_to_complete - 1;
2072 cmp_len = strlen(text_to_complete + 1);
2077 gchar *homedir = g_get_home_dir ();
2080 return open_dir(homedir, cmpl_state);
2087 char* copy = g_new(char, cmp_len + 1);
2089 strncpy(copy, text_to_complete + 1, cmp_len);
2091 pwd = getpwnam(copy);
2099 return open_dir(pwd->pw_dir, cmpl_state);
2105 /* open a directory relative the the current relative directory */
2106 static CompletionDir*
2107 open_relative_dir(gchar* dir_name,
2109 CompletionState *cmpl_state)
2111 CompletionDir *result;
2114 path = g_string_sized_new (dir->fullname_len + strlen (dir_name) + 10);
2115 g_string_assign (path, dir->fullname);
2117 if(dir->fullname_len > 1
2118 && path->str[dir->fullname_len - 1] != G_DIR_SEPARATOR)
2119 g_string_append_c (path, G_DIR_SEPARATOR);
2120 g_string_append (path, dir_name);
2122 result = open_dir(path->str, cmpl_state);
2124 g_string_free (path, TRUE);
2129 /* after the cache lookup fails, really open a new directory */
2130 static CompletionDirSent*
2131 open_new_dir(gchar* dir_name, struct stat* sbuf, gboolean stat_subdirs)
2133 CompletionDirSent* sent;
2135 struct dirent *dirent_ptr;
2136 gint entry_count = 0;
2138 struct stat ent_sbuf;
2142 sent = g_new(CompletionDirSent, 1);
2143 sent->mtime = sbuf->st_mtime;
2144 sent->inode = sbuf->st_ino;
2145 sent->device = sbuf->st_dev;
2147 path = g_string_sized_new (2*MAXPATHLEN + 10);
2149 xdir = g_filename_from_utf8 (dir_name);
2150 directory = opendir(xdir);
2159 while((dirent_ptr = readdir(directory)) != NULL)
2164 sent->entries = g_new(CompletionDirEntry, entry_count);
2165 sent->entry_count = entry_count;
2167 rewinddir(directory);
2169 for(i = 0; i < entry_count; i += 1)
2171 dirent_ptr = readdir(directory);
2176 closedir(directory);
2180 sent->entries[i].entry_name = g_filename_to_utf8 (dirent_ptr->d_name);
2182 g_string_assign (path, dir_name);
2183 if (path->str[path->len-1] != G_DIR_SEPARATOR)
2185 g_string_append_c (path, G_DIR_SEPARATOR);
2187 g_string_append (path, dirent_ptr->d_name);
2191 if(stat(path->str, &ent_sbuf) >= 0 && S_ISDIR(ent_sbuf.st_mode))
2192 sent->entries[i].is_dir = TRUE;
2194 /* stat may fail, and we don't mind, since it could be a
2195 * dangling symlink. */
2196 sent->entries[i].is_dir = FALSE;
2199 sent->entries[i].is_dir = 1;
2202 g_string_free (path, TRUE);
2203 qsort(sent->entries, sent->entry_count, sizeof(CompletionDirEntry), compare_cmpl_dir);
2205 closedir(directory);
2210 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
2213 check_dir(gchar *dir_name, struct stat *result, gboolean *stat_subdirs)
2215 /* A list of directories that we know only contain other directories.
2216 * Trying to stat every file in these directories would be very
2223 struct stat statbuf;
2224 } no_stat_dirs[] = {
2225 { "/afs", FALSE, { 0 } },
2226 { "/net", FALSE, { 0 } }
2229 static const gint n_no_stat_dirs = sizeof(no_stat_dirs) / sizeof(no_stat_dirs[0]);
2230 static gboolean initialized = FALSE;
2237 for (i = 0; i < n_no_stat_dirs; i++)
2239 if (stat (no_stat_dirs[i].name, &no_stat_dirs[i].statbuf) == 0)
2240 no_stat_dirs[i].present = TRUE;
2244 if(stat(dir_name, result) < 0)
2250 *stat_subdirs = TRUE;
2251 for (i=0; i<n_no_stat_dirs; i++)
2253 if (no_stat_dirs[i].present &&
2254 (no_stat_dirs[i].statbuf.st_dev == result->st_dev) &&
2255 (no_stat_dirs[i].statbuf.st_ino == result->st_ino))
2257 *stat_subdirs = FALSE;
2267 /* open a directory by absolute pathname */
2268 static CompletionDir*
2269 open_dir(gchar* dir_name, CompletionState* cmpl_state)
2272 gboolean stat_subdirs;
2273 CompletionDirSent *sent;
2276 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
2277 if (!check_dir (dir_name, &sbuf, &stat_subdirs))
2280 cdsl = cmpl_state->directory_sent_storage;
2286 if(sent->inode == sbuf.st_ino &&
2287 sent->mtime == sbuf.st_mtime &&
2288 sent->device == sbuf.st_dev)
2289 return attach_dir(sent, dir_name, cmpl_state);
2294 stat_subdirs = TRUE;
2297 sent = open_new_dir(dir_name, &sbuf, stat_subdirs);
2300 cmpl_state->directory_sent_storage =
2301 g_list_prepend(cmpl_state->directory_sent_storage, sent);
2303 return attach_dir(sent, dir_name, cmpl_state);
2309 static CompletionDir*
2310 attach_dir(CompletionDirSent* sent, gchar* dir_name, CompletionState *cmpl_state)
2312 CompletionDir* new_dir;
2314 new_dir = g_new(CompletionDir, 1);
2316 cmpl_state->directory_storage =
2317 g_list_prepend(cmpl_state->directory_storage, new_dir);
2319 new_dir->sent = sent;
2320 new_dir->fullname = g_strdup(dir_name);
2321 new_dir->fullname_len = strlen(dir_name);
2327 correct_dir_fullname(CompletionDir* cmpl_dir)
2329 gint length = strlen(cmpl_dir->fullname);
2330 gchar *first_slash = strchr(cmpl_dir->fullname, G_DIR_SEPARATOR);
2333 /* Does it end with /. (\.) ? */
2335 strcmp(cmpl_dir->fullname + length - 2, G_DIR_SEPARATOR_S ".") == 0)
2337 /* Is it just the root directory (on a drive) ? */
2338 if (cmpl_dir->fullname + length - 2 == first_slash)
2340 cmpl_dir->fullname[length - 1] = 0;
2341 cmpl_dir->fullname_len = length - 1;
2346 cmpl_dir->fullname[length - 2] = 0;
2350 /* Ends with /./ (\.\)? */
2351 else if (length >= 3 &&
2352 strcmp(cmpl_dir->fullname + length - 3,
2353 G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) == 0)
2354 cmpl_dir->fullname[length - 2] = 0;
2356 /* Ends with /.. (\..) ? */
2357 else if (length >= 3 &&
2358 strcmp(cmpl_dir->fullname + length - 3,
2359 G_DIR_SEPARATOR_S "..") == 0)
2361 /* Is it just /.. (X:\..)? */
2362 if(cmpl_dir->fullname + length - 3 == first_slash)
2364 cmpl_dir->fullname[length - 2] = 0;
2365 cmpl_dir->fullname_len = length - 2;
2369 if(stat(cmpl_dir->fullname, &sbuf) < 0)
2375 cmpl_dir->fullname[length - 3] = 0;
2377 if(!correct_parent(cmpl_dir, &sbuf))
2381 /* Ends with /../ (\..\)? */
2382 else if (length >= 4 &&
2383 strcmp(cmpl_dir->fullname + length - 4,
2384 G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) == 0)
2386 /* Is it just /../ (X:\..\)? */
2387 if(cmpl_dir->fullname + length - 4 == first_slash)
2389 cmpl_dir->fullname[length - 3] = 0;
2390 cmpl_dir->fullname_len = length - 3;
2394 if(stat(cmpl_dir->fullname, &sbuf) < 0)
2400 cmpl_dir->fullname[length - 4] = 0;
2402 if(!correct_parent(cmpl_dir, &sbuf))
2406 cmpl_dir->fullname_len = strlen(cmpl_dir->fullname);
2412 correct_parent(CompletionDir* cmpl_dir, struct stat *sbuf)
2420 last_slash = strrchr(cmpl_dir->fullname, G_DIR_SEPARATOR);
2421 g_assert(last_slash);
2422 first_slash = strchr(cmpl_dir->fullname, G_DIR_SEPARATOR);
2424 /* Clever (?) way to check for top-level directory that works also on
2425 * Win32, where there is a drive letter and colon prefixed...
2427 if (last_slash != first_slash)
2437 if (stat(cmpl_dir->fullname, &parbuf) < 0)
2441 last_slash[0] = G_DIR_SEPARATOR;
2445 #ifndef G_OS_WIN32 /* No inode numbers on Win32 */
2446 if (parbuf.st_ino == sbuf->st_ino && parbuf.st_dev == sbuf->st_dev)
2447 /* it wasn't a link */
2453 last_slash[0] = G_DIR_SEPARATOR;
2455 /* it was a link, have to figure it out the hard way */
2457 new_name = find_parent_dir_fullname(cmpl_dir->fullname);
2462 g_free(cmpl_dir->fullname);
2464 cmpl_dir->fullname = new_name;
2473 find_parent_dir_fullname(gchar* dirname)
2478 orig_dir = g_get_current_dir ();
2480 if(chdir(dirname) != 0 || chdir("..") != 0)
2486 result = g_get_current_dir ();
2488 if(chdir(orig_dir) != 0)
2500 /**********************************************************************/
2501 /* Completion Operations */
2502 /**********************************************************************/
2506 static PossibleCompletion*
2507 attempt_homedir_completion(gchar* text_to_complete,
2508 CompletionState *cmpl_state)
2512 if (!cmpl_state->user_dir_name_buffer &&
2513 !get_pwdb(cmpl_state))
2515 length = strlen(text_to_complete) - 1;
2517 cmpl_state->user_completion_index += 1;
2519 while(cmpl_state->user_completion_index < cmpl_state->user_directories_len)
2521 index = first_diff_index(text_to_complete + 1,
2522 cmpl_state->user_directories
2523 [cmpl_state->user_completion_index].login);
2530 if(cmpl_state->last_valid_char < (index + 1))
2531 cmpl_state->last_valid_char = index + 1;
2532 cmpl_state->user_completion_index += 1;
2536 cmpl_state->the_completion.is_a_completion = 1;
2537 cmpl_state->the_completion.is_directory = TRUE;
2539 append_completion_text("~", cmpl_state);
2541 append_completion_text(cmpl_state->
2542 user_directories[cmpl_state->user_completion_index].login,
2545 return append_completion_text(G_DIR_SEPARATOR_S, cmpl_state);
2548 if(text_to_complete[1] ||
2549 cmpl_state->user_completion_index > cmpl_state->user_directories_len)
2551 cmpl_state->user_completion_index = -1;
2556 cmpl_state->user_completion_index += 1;
2557 cmpl_state->the_completion.is_a_completion = 1;
2558 cmpl_state->the_completion.is_directory = TRUE;
2560 return append_completion_text("~" G_DIR_SEPARATOR_S, cmpl_state);
2566 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
2567 #define FOLD(c) (tolower(c))
2572 /* returns the index (>= 0) of the first differing character,
2573 * PATTERN_MATCH if the completion matches */
2575 first_diff_index(gchar* pat, gchar* text)
2579 while(*pat && *text && FOLD(*text) == FOLD(*pat))
2589 return PATTERN_MATCH;
2592 static PossibleCompletion*
2593 append_completion_text(gchar* text, CompletionState* cmpl_state)
2597 if(!cmpl_state->the_completion.text)
2600 len = strlen(text) + strlen(cmpl_state->the_completion.text) + 1;
2602 if(cmpl_state->the_completion.text_alloc > len)
2604 strcat(cmpl_state->the_completion.text, text);
2605 return &cmpl_state->the_completion;
2608 while(i < len) { i <<= 1; }
2610 cmpl_state->the_completion.text_alloc = i;
2612 cmpl_state->the_completion.text = (gchar*)g_realloc(cmpl_state->the_completion.text, i);
2614 if(!cmpl_state->the_completion.text)
2618 strcat(cmpl_state->the_completion.text, text);
2619 return &cmpl_state->the_completion;
2623 static CompletionDir*
2624 find_completion_dir(gchar* text_to_complete,
2625 gchar** remaining_text,
2626 CompletionState* cmpl_state)
2628 gchar* first_slash = strchr(text_to_complete, G_DIR_SEPARATOR);
2629 CompletionDir* dir = cmpl_state->reference_dir;
2630 CompletionDir* next;
2631 *remaining_text = text_to_complete;
2635 gint len = first_slash - *remaining_text;
2637 gchar *found_name = NULL; /* Quiet gcc */
2639 gchar* pat_buf = g_new (gchar, len + 1);
2641 strncpy(pat_buf, *remaining_text, len);
2644 for(i = 0; i < dir->sent->entry_count; i += 1)
2646 if(dir->sent->entries[i].is_dir &&
2647 fnmatch(pat_buf, dir->sent->entries[i].entry_name,
2648 FNMATCH_FLAGS)!= FNM_NOMATCH)
2658 found_name = dir->sent->entries[i].entry_name;
2665 /* Perhaps we are trying to open an automount directory */
2666 found_name = pat_buf;
2669 next = open_relative_dir(found_name, dir, cmpl_state);
2677 next->cmpl_parent = dir;
2681 if(!correct_dir_fullname(dir))
2687 *remaining_text = first_slash + 1;
2688 first_slash = strchr(*remaining_text, G_DIR_SEPARATOR);
2697 update_cmpl(PossibleCompletion* poss, CompletionState* cmpl_state)
2701 if(!poss || !cmpl_is_a_completion(poss))
2704 cmpl_len = strlen(cmpl_this_completion(poss));
2706 if(cmpl_state->updated_text_alloc < cmpl_len + 1)
2708 cmpl_state->updated_text =
2709 (gchar*)g_realloc(cmpl_state->updated_text,
2710 cmpl_state->updated_text_alloc);
2711 cmpl_state->updated_text_alloc = 2*cmpl_len;
2714 if(cmpl_state->updated_text_len < 0)
2716 strcpy(cmpl_state->updated_text, cmpl_this_completion(poss));
2717 cmpl_state->updated_text_len = cmpl_len;
2718 cmpl_state->re_complete = cmpl_is_directory(poss);
2720 else if(cmpl_state->updated_text_len == 0)
2722 cmpl_state->re_complete = FALSE;
2727 first_diff_index(cmpl_state->updated_text,
2728 cmpl_this_completion(poss));
2730 cmpl_state->re_complete = FALSE;
2732 if(first_diff == PATTERN_MATCH)
2735 if(first_diff > cmpl_state->updated_text_len)
2736 strcpy(cmpl_state->updated_text, cmpl_this_completion(poss));
2738 cmpl_state->updated_text_len = first_diff;
2739 cmpl_state->updated_text[first_diff] = 0;
2743 static PossibleCompletion*
2744 attempt_file_completion(CompletionState *cmpl_state)
2746 gchar *pat_buf, *first_slash;
2747 CompletionDir *dir = cmpl_state->active_completion_dir;
2749 dir->cmpl_index += 1;
2751 if(dir->cmpl_index == dir->sent->entry_count)
2753 if(dir->cmpl_parent == NULL)
2755 cmpl_state->active_completion_dir = NULL;
2761 cmpl_state->active_completion_dir = dir->cmpl_parent;
2763 return attempt_file_completion(cmpl_state);
2767 g_assert(dir->cmpl_text);
2769 first_slash = strchr(dir->cmpl_text, G_DIR_SEPARATOR);
2773 gint len = first_slash - dir->cmpl_text;
2775 pat_buf = g_new (gchar, len + 1);
2776 strncpy(pat_buf, dir->cmpl_text, len);
2781 gint len = strlen(dir->cmpl_text);
2783 pat_buf = g_new (gchar, len + 2);
2784 strcpy(pat_buf, dir->cmpl_text);
2785 /* Don't append a * if the user entered one herself.
2786 * This way one can complete *.h and don't get matches
2787 * on any .help files, for instance.
2789 if (strchr(pat_buf, '*') == NULL)
2790 strcpy(pat_buf + len, "*");
2795 if(dir->sent->entries[dir->cmpl_index].is_dir)
2797 if(fnmatch(pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
2798 FNMATCH_FLAGS) != FNM_NOMATCH)
2800 CompletionDir* new_dir;
2802 new_dir = open_relative_dir(dir->sent->entries[dir->cmpl_index].entry_name,
2811 new_dir->cmpl_parent = dir;
2813 new_dir->cmpl_index = -1;
2814 new_dir->cmpl_text = first_slash + 1;
2816 cmpl_state->active_completion_dir = new_dir;
2819 return attempt_file_completion(cmpl_state);
2824 return attempt_file_completion(cmpl_state);
2830 return attempt_file_completion(cmpl_state);
2835 if(dir->cmpl_parent != NULL)
2837 append_completion_text(dir->fullname +
2838 strlen(cmpl_state->completion_dir->fullname) + 1,
2840 append_completion_text(G_DIR_SEPARATOR_S, cmpl_state);
2843 append_completion_text(dir->sent->entries[dir->cmpl_index].entry_name, cmpl_state);
2845 cmpl_state->the_completion.is_a_completion =
2846 (fnmatch(pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
2847 FNMATCH_FLAGS) != FNM_NOMATCH);
2849 cmpl_state->the_completion.is_directory = dir->sent->entries[dir->cmpl_index].is_dir;
2850 if(dir->sent->entries[dir->cmpl_index].is_dir)
2851 append_completion_text(G_DIR_SEPARATOR_S, cmpl_state);
2854 return &cmpl_state->the_completion;
2861 get_pwdb(CompletionState* cmpl_state)
2863 struct passwd *pwd_ptr;
2865 gint len = 0, i, count = 0;
2867 if(cmpl_state->user_dir_name_buffer)
2871 while ((pwd_ptr = getpwent()) != NULL)
2873 len += strlen(pwd_ptr->pw_name);
2874 len += strlen(pwd_ptr->pw_dir);
2881 cmpl_state->user_dir_name_buffer = g_new(gchar, len);
2882 cmpl_state->user_directories = g_new(CompletionUserDir, count);
2883 cmpl_state->user_directories_len = count;
2885 buf_ptr = cmpl_state->user_dir_name_buffer;
2887 for(i = 0; i < count; i += 1)
2889 pwd_ptr = getpwent();
2896 strcpy(buf_ptr, pwd_ptr->pw_name);
2897 cmpl_state->user_directories[i].login = buf_ptr;
2898 buf_ptr += strlen(buf_ptr);
2900 strcpy(buf_ptr, pwd_ptr->pw_dir);
2901 cmpl_state->user_directories[i].homedir = buf_ptr;
2902 buf_ptr += strlen(buf_ptr);
2906 qsort(cmpl_state->user_directories,
2907 cmpl_state->user_directories_len,
2908 sizeof(CompletionUserDir),
2917 if(cmpl_state->user_dir_name_buffer)
2918 g_free(cmpl_state->user_dir_name_buffer);
2919 if(cmpl_state->user_directories)
2920 g_free(cmpl_state->user_directories);
2922 cmpl_state->user_dir_name_buffer = NULL;
2923 cmpl_state->user_directories = NULL;
2929 compare_user_dir(const void* a, const void* b)
2931 return strcmp((((CompletionUserDir*)a))->login,
2932 (((CompletionUserDir*)b))->login);
2938 compare_cmpl_dir(const void* a, const void* b)
2940 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
2941 return strcmp((((CompletionDirEntry*)a))->entry_name,
2942 (((CompletionDirEntry*)b))->entry_name);
2944 return g_strcasecmp((((CompletionDirEntry*)a))->entry_name,
2945 (((CompletionDirEntry*)b))->entry_name);
2950 cmpl_state_okay(CompletionState* cmpl_state)
2952 return cmpl_state && cmpl_state->reference_dir;
2956 cmpl_strerror(gint err)
2958 if(err == CMPL_ERRNO_TOO_LONG)
2959 return "Name too long";
2961 return g_strerror (err);