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_column_titles_passive (GTK_CLIST (filesel->dir_list));
523 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
524 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->dir_list);
525 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
526 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
527 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 5);
528 gtk_box_pack_start (GTK_BOX (list_hbox), scrolled_win, TRUE, TRUE, 0);
529 gtk_widget_show (filesel->dir_list);
530 gtk_widget_show (scrolled_win);
532 /* The files clist */
533 file_title[0] = _("Files");
534 file_title[1] = NULL;
535 filesel->file_list = gtk_clist_new_with_titles (1, (gchar**) file_title);
536 gtk_widget_set_usize (filesel->file_list, FILE_LIST_WIDTH, FILE_LIST_HEIGHT);
537 gtk_signal_connect (GTK_OBJECT (filesel->file_list), "select_row",
538 (GtkSignalFunc) gtk_file_selection_file_button,
540 gtk_clist_column_titles_passive (GTK_CLIST (filesel->file_list));
542 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
543 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->file_list);
544 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
545 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
546 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 5);
547 gtk_box_pack_start (GTK_BOX (list_hbox), scrolled_win, TRUE, TRUE, 0);
548 gtk_widget_show (filesel->file_list);
549 gtk_widget_show (scrolled_win);
551 /* action area for packing buttons into. */
552 filesel->action_area = gtk_hbox_new (TRUE, 0);
553 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->action_area,
555 gtk_widget_show (filesel->action_area);
557 /* The OK/Cancel button area */
558 confirm_area = gtk_hbutton_box_new ();
559 gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area), GTK_BUTTONBOX_END);
560 gtk_button_box_set_spacing(GTK_BUTTON_BOX(confirm_area), 5);
561 gtk_box_pack_end (GTK_BOX (filesel->main_vbox), confirm_area, FALSE, FALSE, 0);
562 gtk_widget_show (confirm_area);
565 filesel->ok_button = gtk_button_new_with_label (_("OK"));
566 GTK_WIDGET_SET_FLAGS (filesel->ok_button, GTK_CAN_DEFAULT);
567 gtk_box_pack_start (GTK_BOX (confirm_area), filesel->ok_button, TRUE, TRUE, 0);
568 gtk_widget_grab_default (filesel->ok_button);
569 gtk_widget_show (filesel->ok_button);
571 /* The Cancel button */
572 filesel->cancel_button = gtk_button_new_with_label (_("Cancel"));
573 GTK_WIDGET_SET_FLAGS (filesel->cancel_button, GTK_CAN_DEFAULT);
574 gtk_box_pack_start (GTK_BOX (confirm_area), filesel->cancel_button, TRUE, TRUE, 0);
575 gtk_widget_show (filesel->cancel_button);
577 /* The selection entry widget */
578 entry_vbox = gtk_vbox_new (FALSE, 2);
579 gtk_box_pack_end (GTK_BOX (filesel->main_vbox), entry_vbox, FALSE, FALSE, 0);
580 gtk_widget_show (entry_vbox);
582 filesel->selection_text = label = gtk_label_new ("");
583 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
584 gtk_box_pack_start (GTK_BOX (entry_vbox), label, FALSE, FALSE, 0);
585 gtk_widget_show (label);
587 filesel->selection_entry = gtk_entry_new ();
588 gtk_signal_connect (GTK_OBJECT (filesel->selection_entry), "key_press_event",
589 (GtkSignalFunc) gtk_file_selection_key_press, filesel);
590 gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "focus_in_event",
591 (GtkSignalFunc) gtk_widget_grab_default,
592 GTK_OBJECT (filesel->ok_button));
593 gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "activate",
594 (GtkSignalFunc) gtk_button_clicked,
595 GTK_OBJECT (filesel->ok_button));
596 gtk_box_pack_start (GTK_BOX (entry_vbox), filesel->selection_entry, TRUE, TRUE, 0);
597 gtk_widget_show (filesel->selection_entry);
599 if (!cmpl_state_okay (filesel->cmpl_state))
603 sprintf (err_buf, _("Directory unreadable: %s"), cmpl_strerror (cmpl_errno));
605 gtk_label_set_text (GTK_LABEL (filesel->selection_text), err_buf);
609 gtk_file_selection_populate (filesel, "", FALSE);
612 gtk_widget_grab_focus (filesel->selection_entry);
616 gtk_file_selection_new (const gchar *title)
618 GtkFileSelection *filesel;
620 filesel = gtk_type_new (GTK_TYPE_FILE_SELECTION);
621 gtk_window_set_title (GTK_WINDOW (filesel), title);
623 return GTK_WIDGET (filesel);
627 gtk_file_selection_show_fileop_buttons (GtkFileSelection *filesel)
629 g_return_if_fail (filesel != NULL);
630 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
632 /* delete, create directory, and rename */
633 if (!filesel->fileop_c_dir)
635 filesel->fileop_c_dir = gtk_button_new_with_label (_("Create Dir"));
636 gtk_signal_connect (GTK_OBJECT (filesel->fileop_c_dir), "clicked",
637 (GtkSignalFunc) gtk_file_selection_create_dir,
639 gtk_box_pack_start (GTK_BOX (filesel->button_area),
640 filesel->fileop_c_dir, TRUE, TRUE, 0);
641 gtk_widget_show (filesel->fileop_c_dir);
644 if (!filesel->fileop_del_file)
646 filesel->fileop_del_file = gtk_button_new_with_label (_("Delete File"));
647 gtk_signal_connect (GTK_OBJECT (filesel->fileop_del_file), "clicked",
648 (GtkSignalFunc) gtk_file_selection_delete_file,
650 gtk_box_pack_start (GTK_BOX (filesel->button_area),
651 filesel->fileop_del_file, TRUE, TRUE, 0);
652 gtk_widget_show (filesel->fileop_del_file);
655 if (!filesel->fileop_ren_file)
657 filesel->fileop_ren_file = gtk_button_new_with_label (_("Rename File"));
658 gtk_signal_connect (GTK_OBJECT (filesel->fileop_ren_file), "clicked",
659 (GtkSignalFunc) gtk_file_selection_rename_file,
661 gtk_box_pack_start (GTK_BOX (filesel->button_area),
662 filesel->fileop_ren_file, TRUE, TRUE, 0);
663 gtk_widget_show (filesel->fileop_ren_file);
666 gtk_widget_queue_resize(GTK_WIDGET(filesel));
670 gtk_file_selection_hide_fileop_buttons (GtkFileSelection *filesel)
672 g_return_if_fail (filesel != NULL);
673 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
675 if (filesel->fileop_ren_file)
677 gtk_widget_destroy (filesel->fileop_ren_file);
678 filesel->fileop_ren_file = NULL;
681 if (filesel->fileop_del_file)
683 gtk_widget_destroy (filesel->fileop_del_file);
684 filesel->fileop_del_file = NULL;
687 if (filesel->fileop_c_dir)
689 gtk_widget_destroy (filesel->fileop_c_dir);
690 filesel->fileop_c_dir = NULL;
697 gtk_file_selection_set_filename (GtkFileSelection *filesel,
698 const gchar *filename)
701 const char *name, *last_slash;
703 g_return_if_fail (filesel != NULL);
704 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
705 g_return_if_fail (filename != NULL);
707 last_slash = strrchr (filename, G_DIR_SEPARATOR);
716 buf = g_strdup (filename);
717 buf[last_slash - filename + 1] = 0;
718 name = last_slash + 1;
721 gtk_file_selection_populate (filesel, buf, FALSE);
723 if (filesel->selection_entry)
724 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), name);
729 gtk_file_selection_get_filename (GtkFileSelection *filesel)
731 static gchar nothing[2] = "";
732 static gchar something[MAXPATHLEN*2];
736 g_return_val_if_fail (filesel != NULL, nothing);
737 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), nothing);
740 translate_win32_path(filesel);
742 text = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
745 filename = g_filename_from_utf8 (cmpl_completion_fullname (text, filesel->cmpl_state));
746 strncpy (something, filename, sizeof (something));
755 gtk_file_selection_complete (GtkFileSelection *filesel,
756 const gchar *pattern)
758 g_return_if_fail (filesel != NULL);
759 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
760 g_return_if_fail (pattern != NULL);
762 if (filesel->selection_entry)
763 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), pattern);
764 gtk_file_selection_populate (filesel, (gchar*) pattern, TRUE);
768 gtk_file_selection_destroy (GtkObject *object)
770 GtkFileSelection *filesel;
772 HistoryCallbackArg *callback_arg;
774 g_return_if_fail (object != NULL);
775 g_return_if_fail (GTK_IS_FILE_SELECTION (object));
777 filesel = GTK_FILE_SELECTION (object);
779 if (filesel->fileop_dialog)
780 gtk_widget_destroy (filesel->fileop_dialog);
782 if (filesel->history_list)
784 list = filesel->history_list;
787 callback_arg = list->data;
788 g_free (callback_arg->directory);
789 g_free (callback_arg);
792 g_list_free (filesel->history_list);
793 filesel->history_list = NULL;
796 cmpl_free_state (filesel->cmpl_state);
797 filesel->cmpl_state = NULL;
799 if (GTK_OBJECT_CLASS (parent_class)->destroy)
800 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
803 /* Begin file operations callbacks */
806 gtk_file_selection_fileop_error (GtkFileSelection *fs, gchar *error_message)
813 g_return_if_fail (error_message != NULL);
816 dialog = gtk_dialog_new ();
818 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
819 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
822 gtk_window_set_title (GTK_WINDOW (dialog), _("Error"));
823 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
825 /* If file dialog is grabbed, make this dialog modal too */
826 /* When error dialog is closed, file dialog will be grabbed again */
827 if (GTK_WINDOW(fs)->modal)
828 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
830 vbox = gtk_vbox_new(FALSE, 0);
831 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
832 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
834 gtk_widget_show(vbox);
836 label = gtk_label_new(error_message);
837 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
838 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
839 gtk_widget_show(label);
841 /* yes, we free it */
842 g_free (error_message);
845 button = gtk_button_new_with_label (_("Close"));
846 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
847 (GtkSignalFunc) gtk_widget_destroy,
849 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
850 button, TRUE, TRUE, 0);
851 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
852 gtk_widget_grab_default(button);
853 gtk_widget_show (button);
855 gtk_widget_show (dialog);
859 gtk_file_selection_fileop_destroy (GtkWidget *widget, gpointer data)
861 GtkFileSelection *fs = data;
863 g_return_if_fail (fs != NULL);
864 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
866 fs->fileop_dialog = NULL;
871 gtk_file_selection_create_dir_confirmed (GtkWidget *widget, gpointer data)
873 GtkFileSelection *fs = data;
878 CompletionState *cmpl_state;
880 g_return_if_fail (fs != NULL);
881 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
883 dirname = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
884 cmpl_state = (CompletionState*) fs->cmpl_state;
885 path = cmpl_reference_position (cmpl_state);
887 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, dirname, NULL);
888 if ( (mkdir (full_path, 0755) < 0) )
890 buf = g_strconcat ("Error creating directory \"", dirname, "\": ",
891 g_strerror(errno), NULL);
892 gtk_file_selection_fileop_error (fs, buf);
896 gtk_widget_destroy (fs->fileop_dialog);
897 gtk_file_selection_populate (fs, "", FALSE);
901 gtk_file_selection_create_dir (GtkWidget *widget, gpointer data)
903 GtkFileSelection *fs = data;
909 g_return_if_fail (fs != NULL);
910 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
912 if (fs->fileop_dialog)
916 fs->fileop_dialog = dialog = gtk_dialog_new ();
917 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
918 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
920 gtk_window_set_title (GTK_WINDOW (dialog), _("Create Directory"));
921 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
923 /* If file dialog is grabbed, grab option dialog */
924 /* When option dialog is closed, file dialog will be grabbed again */
925 if (GTK_WINDOW(fs)->modal)
926 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
928 vbox = gtk_vbox_new(FALSE, 0);
929 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
930 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
932 gtk_widget_show(vbox);
934 label = gtk_label_new(_("Directory name:"));
935 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
936 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
937 gtk_widget_show(label);
939 /* The directory entry widget */
940 fs->fileop_entry = gtk_entry_new ();
941 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
943 GTK_WIDGET_SET_FLAGS(fs->fileop_entry, GTK_CAN_DEFAULT);
944 gtk_widget_show (fs->fileop_entry);
947 button = gtk_button_new_with_label (_("Create"));
948 gtk_signal_connect (GTK_OBJECT (button), "clicked",
949 (GtkSignalFunc) gtk_file_selection_create_dir_confirmed,
951 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
952 button, TRUE, TRUE, 0);
953 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
954 gtk_widget_show(button);
956 button = gtk_button_new_with_label (_("Cancel"));
957 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
958 (GtkSignalFunc) gtk_widget_destroy,
960 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
961 button, TRUE, TRUE, 0);
962 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
963 gtk_widget_grab_default(button);
964 gtk_widget_show (button);
966 gtk_widget_show (dialog);
970 gtk_file_selection_delete_file_confirmed (GtkWidget *widget, gpointer data)
972 GtkFileSelection *fs = data;
973 CompletionState *cmpl_state;
978 g_return_if_fail (fs != NULL);
979 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
981 cmpl_state = (CompletionState*) fs->cmpl_state;
982 path = cmpl_reference_position (cmpl_state);
984 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
985 if ( (unlink (full_path) < 0) )
987 buf = g_strconcat ("Error deleting file \"", fs->fileop_file, "\": ",
988 g_strerror(errno), NULL);
989 gtk_file_selection_fileop_error (fs, buf);
993 gtk_widget_destroy (fs->fileop_dialog);
994 gtk_file_selection_populate (fs, "", FALSE);
998 gtk_file_selection_delete_file (GtkWidget *widget, gpointer data)
1000 GtkFileSelection *fs = data;
1008 g_return_if_fail (fs != NULL);
1009 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1011 if (fs->fileop_dialog)
1014 #ifdef G_WITH_CYGWIN
1015 translate_win32_path(fs);
1018 filename = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1019 if (strlen(filename) < 1)
1022 fs->fileop_file = filename;
1025 fs->fileop_dialog = dialog = gtk_dialog_new ();
1026 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
1027 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
1029 gtk_window_set_title (GTK_WINDOW (dialog), _("Delete File"));
1030 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1032 /* If file dialog is grabbed, grab option dialog */
1033 /* When option dialog is closed, file dialog will be grabbed again */
1034 if (GTK_WINDOW(fs)->modal)
1035 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
1037 vbox = gtk_vbox_new(FALSE, 0);
1038 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1039 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
1041 gtk_widget_show(vbox);
1043 buf = g_strconcat ("Really delete file \"", filename, "\" ?", NULL);
1044 label = gtk_label_new(buf);
1045 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
1046 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
1047 gtk_widget_show(label);
1051 button = gtk_button_new_with_label (_("Delete"));
1052 gtk_signal_connect (GTK_OBJECT (button), "clicked",
1053 (GtkSignalFunc) gtk_file_selection_delete_file_confirmed,
1055 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1056 button, TRUE, TRUE, 0);
1057 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1058 gtk_widget_show(button);
1060 button = gtk_button_new_with_label (_("Cancel"));
1061 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1062 (GtkSignalFunc) gtk_widget_destroy,
1064 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1065 button, TRUE, TRUE, 0);
1066 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1067 gtk_widget_grab_default(button);
1068 gtk_widget_show (button);
1070 gtk_widget_show (dialog);
1075 gtk_file_selection_rename_file_confirmed (GtkWidget *widget, gpointer data)
1077 GtkFileSelection *fs = data;
1081 gchar *new_filename;
1082 gchar *old_filename;
1083 CompletionState *cmpl_state;
1085 g_return_if_fail (fs != NULL);
1086 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1088 file = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
1089 cmpl_state = (CompletionState*) fs->cmpl_state;
1090 path = cmpl_reference_position (cmpl_state);
1092 new_filename = g_strconcat (path, G_DIR_SEPARATOR_S, file, NULL);
1093 old_filename = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1095 if ( (rename (old_filename, new_filename)) < 0)
1097 buf = g_strconcat ("Error renaming file \"", file, "\": ",
1098 g_strerror(errno), NULL);
1099 gtk_file_selection_fileop_error (fs, buf);
1101 g_free (new_filename);
1102 g_free (old_filename);
1104 gtk_widget_destroy (fs->fileop_dialog);
1105 gtk_file_selection_populate (fs, "", FALSE);
1109 gtk_file_selection_rename_file (GtkWidget *widget, gpointer data)
1111 GtkFileSelection *fs = data;
1118 g_return_if_fail (fs != NULL);
1119 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1121 if (fs->fileop_dialog)
1124 fs->fileop_file = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1125 if (strlen(fs->fileop_file) < 1)
1129 fs->fileop_dialog = dialog = gtk_dialog_new ();
1130 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
1131 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
1133 gtk_window_set_title (GTK_WINDOW (dialog), _("Rename File"));
1134 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1136 /* If file dialog is grabbed, grab option dialog */
1137 /* When option dialog closed, file dialog will be grabbed again */
1138 if (GTK_WINDOW(fs)->modal)
1139 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
1141 vbox = gtk_vbox_new(FALSE, 0);
1142 gtk_container_set_border_width (GTK_CONTAINER(vbox), 8);
1143 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
1145 gtk_widget_show(vbox);
1147 buf = g_strconcat ("Rename file \"", fs->fileop_file, "\" to:", NULL);
1148 label = gtk_label_new(buf);
1149 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
1150 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
1151 gtk_widget_show(label);
1154 /* New filename entry */
1155 fs->fileop_entry = gtk_entry_new ();
1156 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1158 GTK_WIDGET_SET_FLAGS(fs->fileop_entry, GTK_CAN_DEFAULT);
1159 gtk_widget_show (fs->fileop_entry);
1161 gtk_entry_set_text (GTK_ENTRY (fs->fileop_entry), fs->fileop_file);
1162 gtk_editable_select_region (GTK_EDITABLE (fs->fileop_entry),
1163 0, strlen (fs->fileop_file));
1166 button = gtk_button_new_with_label (_("Rename"));
1167 gtk_signal_connect (GTK_OBJECT (button), "clicked",
1168 (GtkSignalFunc) gtk_file_selection_rename_file_confirmed,
1170 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1171 button, TRUE, TRUE, 0);
1172 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1173 gtk_widget_show(button);
1175 button = gtk_button_new_with_label (_("Cancel"));
1176 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1177 (GtkSignalFunc) gtk_widget_destroy,
1179 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1180 button, TRUE, TRUE, 0);
1181 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1182 gtk_widget_grab_default(button);
1183 gtk_widget_show (button);
1185 gtk_widget_show (dialog);
1190 gtk_file_selection_key_press (GtkWidget *widget,
1194 GtkFileSelection *fs;
1197 g_return_val_if_fail (widget != NULL, FALSE);
1198 g_return_val_if_fail (event != NULL, FALSE);
1200 if (event->keyval == GDK_Tab)
1202 fs = GTK_FILE_SELECTION (user_data);
1203 #ifdef G_WITH_CYGWIN
1204 translate_win32_path(fs);
1206 text = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1208 text = g_strdup (text);
1210 gtk_file_selection_populate (fs, text, TRUE);
1214 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "key_press_event");
1224 gtk_file_selection_history_callback (GtkWidget *widget, gpointer data)
1226 GtkFileSelection *fs = data;
1227 HistoryCallbackArg *callback_arg;
1230 g_return_if_fail (fs != NULL);
1231 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1233 list = fs->history_list;
1236 callback_arg = list->data;
1238 if (callback_arg->menu_item == widget)
1240 gtk_file_selection_populate (fs, callback_arg->directory, FALSE);
1249 gtk_file_selection_update_history_menu (GtkFileSelection *fs,
1250 gchar *current_directory)
1252 HistoryCallbackArg *callback_arg;
1253 GtkWidget *menu_item;
1259 g_return_if_fail (fs != NULL);
1260 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1261 g_return_if_fail (current_directory != NULL);
1263 list = fs->history_list;
1265 if (fs->history_menu)
1268 callback_arg = list->data;
1269 g_free (callback_arg->directory);
1270 g_free (callback_arg);
1273 g_list_free (fs->history_list);
1274 fs->history_list = NULL;
1276 gtk_widget_destroy (fs->history_menu);
1279 fs->history_menu = gtk_menu_new();
1281 current_dir = g_strdup (current_directory);
1283 dir_len = strlen (current_dir);
1285 for (i = dir_len; i >= 0; i--)
1287 /* the i == dir_len is to catch the full path for the first
1289 if ( (current_dir[i] == G_DIR_SEPARATOR) || (i == dir_len))
1291 /* another small hack to catch the full path */
1293 current_dir[i + 1] = '\0';
1294 #ifdef G_WITH_CYGWIN
1295 if (!strcmp(current_dir, "//"))
1298 menu_item = gtk_menu_item_new_with_label (current_dir);
1300 callback_arg = g_new (HistoryCallbackArg, 1);
1301 callback_arg->menu_item = menu_item;
1303 /* since the autocompletion gets confused if you don't
1304 * supply a trailing '/' on a dir entry, set the full
1305 * (current) path to "" which just refreshes the filesel */
1308 callback_arg->directory = g_strdup ("");
1312 callback_arg->directory = g_strdup (current_dir);
1315 fs->history_list = g_list_append (fs->history_list, callback_arg);
1317 gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
1318 (GtkSignalFunc) gtk_file_selection_history_callback,
1320 gtk_menu_append (GTK_MENU (fs->history_menu), menu_item);
1321 gtk_widget_show (menu_item);
1325 gtk_option_menu_set_menu (GTK_OPTION_MENU (fs->history_pulldown),
1327 g_free (current_dir);
1331 gtk_file_selection_file_button (GtkWidget *widget,
1334 GdkEventButton *bevent,
1337 GtkFileSelection *fs = NULL;
1338 gchar *filename, *temp = NULL;
1340 g_return_if_fail (GTK_IS_CLIST (widget));
1343 g_return_if_fail (fs != NULL);
1344 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1346 gtk_clist_get_text (GTK_CLIST (fs->file_list), row, 0, &temp);
1347 filename = g_strdup (temp);
1349 #ifdef G_WITH_CYGWIN
1350 /* Check to see if the selection was a drive selector */
1351 if (isalpha(filename[0]) && (filename[1] == ':')) {
1352 /* It is... map it to a CYGWIN32 drive */
1353 char temp_filename[10];
1355 sprintf(temp_filename, "//%c/", tolower(filename[0]));
1357 filename = g_strdup(temp_filename);
1359 #endif /* G_WITH_CYGWIN */
1364 switch (bevent->type)
1366 case GDK_2BUTTON_PRESS:
1367 gtk_button_clicked (GTK_BUTTON (fs->ok_button));
1371 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1375 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1382 gtk_file_selection_dir_button (GtkWidget *widget,
1385 GdkEventButton *bevent,
1388 GtkFileSelection *fs = NULL;
1389 gchar *filename, *temp = NULL;
1391 g_return_if_fail (GTK_IS_CLIST (widget));
1393 fs = GTK_FILE_SELECTION (user_data);
1394 g_return_if_fail (fs != NULL);
1395 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1397 gtk_clist_get_text (GTK_CLIST (fs->dir_list), row, 0, &temp);
1398 filename = g_strdup (temp);
1403 switch (bevent->type)
1405 case GDK_2BUTTON_PRESS:
1406 gtk_file_selection_populate (fs, filename, FALSE);
1410 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1414 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1420 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
1423 win32_gtk_add_drives_to_dir_list(GtkWidget *the_dir_list)
1425 gchar *text[2], *textPtr;
1427 char volumeNameBuf[128];
1428 char formatBuffer[128];
1433 /* Get the Drives string */
1434 GetLogicalDriveStrings(sizeof(buffer), buffer);
1436 /* Add the drives as necessary */
1438 while (*textPtr != '\0') {
1439 /* Get the volume information for this drive */
1440 if ((tolower(textPtr[0]) != 'a') && (tolower(textPtr[0]) != 'b'))
1442 /* Ignore floppies (?) */
1443 DWORD maxComponentLength, flags;
1445 GetVolumeInformation(textPtr,
1446 volumeNameBuf, sizeof(volumeNameBuf),
1447 NULL, &maxComponentLength,
1449 /* Build the actual displayable string */
1451 sprintf(formatBuffer, "%c:\\", toupper(textPtr[0]));
1452 #if 0 /* HB: removed to allow drive change AND directory update with one click */
1453 if (strlen(volumeNameBuf) > 0)
1454 sprintf(formatBuffer, "%s (%s)", formatBuffer, volumeNameBuf);
1456 /* Add to the list */
1457 text[0] = formatBuffer;
1458 row = gtk_clist_append (GTK_CLIST (the_dir_list), text);
1460 textPtr += (strlen(textPtr) + 1);
1466 gtk_file_selection_populate (GtkFileSelection *fs,
1470 CompletionState *cmpl_state;
1471 PossibleCompletion* poss;
1474 gchar* rem_path = rel_path;
1477 gint did_recurse = FALSE;
1478 gint possible_count = 0;
1479 gint selection_index = -1;
1480 gint file_list_width;
1481 gint dir_list_width;
1483 g_return_if_fail (fs != NULL);
1484 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1486 cmpl_state = (CompletionState*) fs->cmpl_state;
1487 poss = cmpl_completion_matches (rel_path, &rem_path, cmpl_state);
1489 if (!cmpl_state_okay (cmpl_state))
1491 /* Something went wrong. */
1492 gtk_file_selection_abort (fs);
1496 g_assert (cmpl_state->reference_dir);
1498 gtk_clist_freeze (GTK_CLIST (fs->dir_list));
1499 gtk_clist_clear (GTK_CLIST (fs->dir_list));
1500 gtk_clist_freeze (GTK_CLIST (fs->file_list));
1501 gtk_clist_clear (GTK_CLIST (fs->file_list));
1503 /* Set the dir_list to include ./ and ../ */
1505 text[0] = "." G_DIR_SEPARATOR_S;
1506 row = gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1508 text[0] = ".." G_DIR_SEPARATOR_S;
1509 row = gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1511 /*reset the max widths of the lists*/
1512 dir_list_width = gdk_string_width(fs->dir_list->style->font,".." G_DIR_SEPARATOR_S);
1513 gtk_clist_set_column_width(GTK_CLIST(fs->dir_list),0,dir_list_width);
1514 file_list_width = 1;
1515 gtk_clist_set_column_width(GTK_CLIST(fs->file_list),0,file_list_width);
1519 if (cmpl_is_a_completion (poss))
1521 possible_count += 1;
1523 filename = cmpl_this_completion (poss);
1527 if (cmpl_is_directory (poss))
1529 if (strcmp (filename, "." G_DIR_SEPARATOR_S) != 0 &&
1530 strcmp (filename, ".." G_DIR_SEPARATOR_S) != 0)
1532 int width = gdk_string_width(fs->dir_list->style->font,
1534 row = gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1535 if(width > dir_list_width)
1537 dir_list_width = width;
1538 gtk_clist_set_column_width(GTK_CLIST(fs->dir_list),0,
1545 int width = gdk_string_width(fs->file_list->style->font,
1547 row = gtk_clist_append (GTK_CLIST (fs->file_list), text);
1548 if(width > file_list_width)
1550 file_list_width = width;
1551 gtk_clist_set_column_width(GTK_CLIST(fs->file_list),0,
1557 poss = cmpl_next_completion (cmpl_state);
1560 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
1561 /* For Windows, add drives as potential selections */
1562 win32_gtk_add_drives_to_dir_list (fs->dir_list);
1565 gtk_clist_thaw (GTK_CLIST (fs->dir_list));
1566 gtk_clist_thaw (GTK_CLIST (fs->file_list));
1568 /* File lists are set. */
1570 g_assert (cmpl_state->reference_dir);
1575 /* User is trying to complete filenames, so advance the user's input
1576 * string to the updated_text, which is the common leading substring
1577 * of all possible completions, and if its a directory attempt
1578 * attempt completions in it. */
1580 if (cmpl_updated_text (cmpl_state)[0])
1583 if (cmpl_updated_dir (cmpl_state))
1585 gchar* dir_name = g_strdup (cmpl_updated_text (cmpl_state));
1589 gtk_file_selection_populate (fs, dir_name, TRUE);
1595 if (fs->selection_entry)
1596 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry),
1597 cmpl_updated_text (cmpl_state));
1602 selection_index = cmpl_last_valid_char (cmpl_state) -
1603 (strlen (rel_path) - strlen (rem_path));
1604 if (fs->selection_entry)
1605 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), rem_path);
1610 if (fs->selection_entry)
1611 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
1616 if (fs->selection_entry)
1617 gtk_entry_set_position (GTK_ENTRY (fs->selection_entry), selection_index);
1619 if (fs->selection_entry)
1621 sel_text = g_strconcat (_("Selection: "),
1622 cmpl_reference_position (cmpl_state),
1625 gtk_label_set_text (GTK_LABEL (fs->selection_text), sel_text);
1629 if (fs->history_pulldown)
1631 gtk_file_selection_update_history_menu (fs, cmpl_reference_position (cmpl_state));
1638 gtk_file_selection_abort (GtkFileSelection *fs)
1642 sprintf (err_buf, _("Directory unreadable: %s"), cmpl_strerror (cmpl_errno));
1644 /* BEEP gdk_beep(); */
1646 if (fs->selection_entry)
1647 gtk_label_set_text (GTK_LABEL (fs->selection_text), err_buf);
1650 /**********************************************************************/
1651 /* External Interface */
1652 /**********************************************************************/
1654 /* The four completion state selectors
1657 cmpl_updated_text (CompletionState* cmpl_state)
1659 return cmpl_state->updated_text;
1663 cmpl_updated_dir (CompletionState* cmpl_state)
1665 return cmpl_state->re_complete;
1669 cmpl_reference_position (CompletionState* cmpl_state)
1671 return cmpl_state->reference_dir->fullname;
1675 cmpl_last_valid_char (CompletionState* cmpl_state)
1677 return cmpl_state->last_valid_char;
1681 cmpl_completion_fullname (gchar* text, CompletionState* cmpl_state)
1683 static char nothing[2] = "";
1685 if (!cmpl_state_okay (cmpl_state))
1689 else if (g_path_is_absolute (text))
1691 strcpy (cmpl_state->updated_text, text);
1694 else if (text[0] == '~')
1699 dir = open_user_dir (text, cmpl_state);
1703 /* spencer says just return ~something, so
1704 * for now just do it. */
1705 strcpy (cmpl_state->updated_text, text);
1710 strcpy (cmpl_state->updated_text, dir->fullname);
1712 slash = strchr (text, G_DIR_SEPARATOR);
1715 strcat (cmpl_state->updated_text, slash);
1721 strcpy (cmpl_state->updated_text, cmpl_state->reference_dir->fullname);
1722 if (cmpl_state->updated_text[strlen (cmpl_state->updated_text) - 1] != G_DIR_SEPARATOR)
1723 strcat (cmpl_state->updated_text, G_DIR_SEPARATOR_S);
1724 strcat (cmpl_state->updated_text, text);
1727 return cmpl_state->updated_text;
1730 /* The three completion selectors
1733 cmpl_this_completion (PossibleCompletion* pc)
1739 cmpl_is_directory (PossibleCompletion* pc)
1741 return pc->is_directory;
1745 cmpl_is_a_completion (PossibleCompletion* pc)
1747 return pc->is_a_completion;
1750 /**********************************************************************/
1751 /* Construction, deletion */
1752 /**********************************************************************/
1754 static CompletionState*
1755 cmpl_init_state (void)
1758 CompletionState *new_state;
1760 new_state = g_new (CompletionState, 1);
1762 getcwd_buf = g_get_current_dir ();
1766 new_state->reference_dir = NULL;
1767 new_state->completion_dir = NULL;
1768 new_state->active_completion_dir = NULL;
1769 new_state->directory_storage = NULL;
1770 new_state->directory_sent_storage = NULL;
1771 new_state->last_valid_char = 0;
1772 new_state->updated_text = g_new (gchar, MAXPATHLEN);
1773 new_state->updated_text_alloc = MAXPATHLEN;
1774 new_state->the_completion.text = g_new (gchar, MAXPATHLEN);
1775 new_state->the_completion.text_alloc = MAXPATHLEN;
1776 new_state->user_dir_name_buffer = NULL;
1777 new_state->user_directories = NULL;
1779 new_state->reference_dir = open_dir (getcwd_buf, new_state);
1781 if (!new_state->reference_dir)
1783 /* Directories changing from underneath us, grumble */
1784 strcpy (getcwd_buf, G_DIR_SEPARATOR_S);
1788 g_free (getcwd_buf);
1793 cmpl_free_dir_list(GList* dp0)
1798 free_dir (dp->data);
1806 cmpl_free_dir_sent_list(GList* dp0)
1811 free_dir_sent (dp->data);
1819 cmpl_free_state (CompletionState* cmpl_state)
1821 cmpl_free_dir_list (cmpl_state->directory_storage);
1822 cmpl_free_dir_sent_list (cmpl_state->directory_sent_storage);
1824 if (cmpl_state->user_dir_name_buffer)
1825 g_free (cmpl_state->user_dir_name_buffer);
1826 if (cmpl_state->user_directories)
1827 g_free (cmpl_state->user_directories);
1828 if (cmpl_state->the_completion.text)
1829 g_free (cmpl_state->the_completion.text);
1830 if (cmpl_state->updated_text)
1831 g_free (cmpl_state->updated_text);
1833 g_free (cmpl_state);
1837 free_dir(CompletionDir* dir)
1839 g_free(dir->fullname);
1844 free_dir_sent(CompletionDirSent* sent)
1847 for (i = 0; i < sent->entry_count; i++)
1848 g_free(sent->entries[i].entry_name);
1849 g_free(sent->entries);
1854 prune_memory_usage(CompletionState *cmpl_state)
1856 GList* cdsl = cmpl_state->directory_sent_storage;
1857 GList* cdl = cmpl_state->directory_storage;
1861 for(; cdsl && len < CMPL_DIRECTORY_CACHE_SIZE; len += 1)
1865 cmpl_free_dir_sent_list(cdsl->next);
1869 cmpl_state->directory_storage = NULL;
1871 if (cdl->data == cmpl_state->reference_dir)
1872 cmpl_state->directory_storage = g_list_prepend(NULL, cdl->data);
1874 free_dir (cdl->data);
1881 /**********************************************************************/
1882 /* The main entrances. */
1883 /**********************************************************************/
1885 static PossibleCompletion*
1886 cmpl_completion_matches (gchar* text_to_complete,
1887 gchar** remaining_text,
1888 CompletionState* cmpl_state)
1891 PossibleCompletion *poss;
1893 prune_memory_usage(cmpl_state);
1895 g_assert (text_to_complete != NULL);
1897 cmpl_state->user_completion_index = -1;
1898 cmpl_state->last_completion_text = text_to_complete;
1899 cmpl_state->the_completion.text[0] = 0;
1900 cmpl_state->last_valid_char = 0;
1901 cmpl_state->updated_text_len = -1;
1902 cmpl_state->updated_text[0] = 0;
1903 cmpl_state->re_complete = FALSE;
1906 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
1908 if (text_to_complete[0] == '~' && !first_slash)
1910 /* Text starts with ~ and there is no slash, show all the
1911 * home directory completions.
1913 poss = attempt_homedir_completion (text_to_complete, cmpl_state);
1915 update_cmpl(poss, cmpl_state);
1920 cmpl_state->reference_dir =
1921 open_ref_dir (text_to_complete, remaining_text, cmpl_state);
1923 if(!cmpl_state->reference_dir)
1926 cmpl_state->completion_dir =
1927 find_completion_dir (*remaining_text, remaining_text, cmpl_state);
1929 cmpl_state->last_valid_char = *remaining_text - text_to_complete;
1931 if(!cmpl_state->completion_dir)
1934 cmpl_state->completion_dir->cmpl_index = -1;
1935 cmpl_state->completion_dir->cmpl_parent = NULL;
1936 cmpl_state->completion_dir->cmpl_text = *remaining_text;
1938 cmpl_state->active_completion_dir = cmpl_state->completion_dir;
1940 cmpl_state->reference_dir = cmpl_state->completion_dir;
1942 poss = attempt_file_completion(cmpl_state);
1944 update_cmpl(poss, cmpl_state);
1949 static PossibleCompletion*
1950 cmpl_next_completion (CompletionState* cmpl_state)
1952 PossibleCompletion* poss = NULL;
1954 cmpl_state->the_completion.text[0] = 0;
1957 if(cmpl_state->user_completion_index >= 0)
1958 poss = attempt_homedir_completion(cmpl_state->last_completion_text, cmpl_state);
1960 poss = attempt_file_completion(cmpl_state);
1962 poss = attempt_file_completion(cmpl_state);
1965 update_cmpl(poss, cmpl_state);
1970 /**********************************************************************/
1971 /* Directory Operations */
1972 /**********************************************************************/
1974 /* Open the directory where completion will begin from, if possible. */
1975 static CompletionDir*
1976 open_ref_dir(gchar* text_to_complete,
1977 gchar** remaining_text,
1978 CompletionState* cmpl_state)
1981 CompletionDir *new_dir;
1983 first_slash = strchr(text_to_complete, G_DIR_SEPARATOR);
1985 #ifdef G_WITH_CYGWIN
1986 if (text_to_complete[0] == '/' && text_to_complete[1] == '/')
1989 sprintf(root_dir, "//%c", text_to_complete[2]);
1991 new_dir = open_dir(root_dir, cmpl_state);
1994 *remaining_text = text_to_complete + 4;
2002 else if (text_to_complete[0] == '~')
2004 new_dir = open_user_dir(text_to_complete, cmpl_state);
2009 *remaining_text = first_slash + 1;
2011 *remaining_text = text_to_complete + strlen(text_to_complete);
2019 else if (g_path_is_absolute (text_to_complete) || !cmpl_state->reference_dir)
2021 gchar *tmp = g_strdup(text_to_complete);
2025 while (*p && *p != '*' && *p != '?')
2029 p = strrchr(tmp, G_DIR_SEPARATOR);
2037 new_dir = open_dir(tmp, cmpl_state);
2040 *remaining_text = text_to_complete +
2041 ((p == tmp + 1) ? (p - tmp) : (p + 1 - tmp));
2045 /* If no possible candidates, use the cwd */
2046 gchar *curdir = g_get_current_dir ();
2048 new_dir = open_dir(curdir, cmpl_state);
2051 *remaining_text = text_to_complete;
2060 *remaining_text = text_to_complete;
2062 new_dir = open_dir(cmpl_state->reference_dir->fullname, cmpl_state);
2067 new_dir->cmpl_index = -1;
2068 new_dir->cmpl_parent = NULL;
2076 /* open a directory by user name */
2077 static CompletionDir*
2078 open_user_dir(gchar* text_to_complete,
2079 CompletionState *cmpl_state)
2084 g_assert(text_to_complete && text_to_complete[0] == '~');
2086 first_slash = strchr(text_to_complete, G_DIR_SEPARATOR);
2089 cmp_len = first_slash - text_to_complete - 1;
2091 cmp_len = strlen(text_to_complete + 1);
2096 gchar *homedir = g_get_home_dir ();
2099 return open_dir(homedir, cmpl_state);
2106 char* copy = g_new(char, cmp_len + 1);
2108 strncpy(copy, text_to_complete + 1, cmp_len);
2110 pwd = getpwnam(copy);
2118 return open_dir(pwd->pw_dir, cmpl_state);
2124 /* open a directory relative the the current relative directory */
2125 static CompletionDir*
2126 open_relative_dir(gchar* dir_name,
2128 CompletionState *cmpl_state)
2130 CompletionDir *result;
2133 path = g_string_sized_new (dir->fullname_len + strlen (dir_name) + 10);
2134 g_string_assign (path, dir->fullname);
2136 if(dir->fullname_len > 1
2137 && path->str[dir->fullname_len - 1] != G_DIR_SEPARATOR)
2138 g_string_append_c (path, G_DIR_SEPARATOR);
2139 g_string_append (path, dir_name);
2141 result = open_dir(path->str, cmpl_state);
2143 g_string_free (path, TRUE);
2148 /* after the cache lookup fails, really open a new directory */
2149 static CompletionDirSent*
2150 open_new_dir(gchar* dir_name, struct stat* sbuf, gboolean stat_subdirs)
2152 CompletionDirSent* sent;
2154 struct dirent *dirent_ptr;
2155 gint entry_count = 0;
2157 struct stat ent_sbuf;
2161 sent = g_new(CompletionDirSent, 1);
2162 sent->mtime = sbuf->st_mtime;
2163 sent->inode = sbuf->st_ino;
2164 sent->device = sbuf->st_dev;
2166 path = g_string_sized_new (2*MAXPATHLEN + 10);
2168 xdir = g_filename_from_utf8 (dir_name);
2169 directory = opendir(xdir);
2178 while((dirent_ptr = readdir(directory)) != NULL)
2183 sent->entries = g_new(CompletionDirEntry, entry_count);
2184 sent->entry_count = entry_count;
2186 rewinddir(directory);
2188 for(i = 0; i < entry_count; i += 1)
2190 dirent_ptr = readdir(directory);
2195 closedir(directory);
2199 sent->entries[i].entry_name = g_filename_to_utf8 (dirent_ptr->d_name);
2201 g_string_assign (path, dir_name);
2202 if (path->str[path->len-1] != G_DIR_SEPARATOR)
2204 g_string_append_c (path, G_DIR_SEPARATOR);
2206 g_string_append (path, dirent_ptr->d_name);
2210 if(stat(path->str, &ent_sbuf) >= 0 && S_ISDIR(ent_sbuf.st_mode))
2211 sent->entries[i].is_dir = TRUE;
2213 /* stat may fail, and we don't mind, since it could be a
2214 * dangling symlink. */
2215 sent->entries[i].is_dir = FALSE;
2218 sent->entries[i].is_dir = 1;
2221 g_string_free (path, TRUE);
2222 qsort(sent->entries, sent->entry_count, sizeof(CompletionDirEntry), compare_cmpl_dir);
2224 closedir(directory);
2229 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
2232 check_dir(gchar *dir_name, struct stat *result, gboolean *stat_subdirs)
2234 /* A list of directories that we know only contain other directories.
2235 * Trying to stat every file in these directories would be very
2242 struct stat statbuf;
2243 } no_stat_dirs[] = {
2244 { "/afs", FALSE, { 0 } },
2245 { "/net", FALSE, { 0 } }
2248 static const gint n_no_stat_dirs = sizeof(no_stat_dirs) / sizeof(no_stat_dirs[0]);
2249 static gboolean initialized = FALSE;
2256 for (i = 0; i < n_no_stat_dirs; i++)
2258 if (stat (no_stat_dirs[i].name, &no_stat_dirs[i].statbuf) == 0)
2259 no_stat_dirs[i].present = TRUE;
2263 if(stat(dir_name, result) < 0)
2269 *stat_subdirs = TRUE;
2270 for (i=0; i<n_no_stat_dirs; i++)
2272 if (no_stat_dirs[i].present &&
2273 (no_stat_dirs[i].statbuf.st_dev == result->st_dev) &&
2274 (no_stat_dirs[i].statbuf.st_ino == result->st_ino))
2276 *stat_subdirs = FALSE;
2286 /* open a directory by absolute pathname */
2287 static CompletionDir*
2288 open_dir(gchar* dir_name, CompletionState* cmpl_state)
2291 gboolean stat_subdirs;
2292 CompletionDirSent *sent;
2295 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
2296 if (!check_dir (dir_name, &sbuf, &stat_subdirs))
2299 cdsl = cmpl_state->directory_sent_storage;
2305 if(sent->inode == sbuf.st_ino &&
2306 sent->mtime == sbuf.st_mtime &&
2307 sent->device == sbuf.st_dev)
2308 return attach_dir(sent, dir_name, cmpl_state);
2313 stat_subdirs = TRUE;
2316 sent = open_new_dir(dir_name, &sbuf, stat_subdirs);
2319 cmpl_state->directory_sent_storage =
2320 g_list_prepend(cmpl_state->directory_sent_storage, sent);
2322 return attach_dir(sent, dir_name, cmpl_state);
2328 static CompletionDir*
2329 attach_dir(CompletionDirSent* sent, gchar* dir_name, CompletionState *cmpl_state)
2331 CompletionDir* new_dir;
2333 new_dir = g_new(CompletionDir, 1);
2335 cmpl_state->directory_storage =
2336 g_list_prepend(cmpl_state->directory_storage, new_dir);
2338 new_dir->sent = sent;
2339 new_dir->fullname = g_strdup(dir_name);
2340 new_dir->fullname_len = strlen(dir_name);
2346 correct_dir_fullname(CompletionDir* cmpl_dir)
2348 gint length = strlen(cmpl_dir->fullname);
2349 gchar *first_slash = strchr(cmpl_dir->fullname, G_DIR_SEPARATOR);
2352 /* Does it end with /. (\.) ? */
2354 strcmp(cmpl_dir->fullname + length - 2, G_DIR_SEPARATOR_S ".") == 0)
2356 /* Is it just the root directory (on a drive) ? */
2357 if (cmpl_dir->fullname + length - 2 == first_slash)
2359 cmpl_dir->fullname[length - 1] = 0;
2360 cmpl_dir->fullname_len = length - 1;
2365 cmpl_dir->fullname[length - 2] = 0;
2369 /* Ends with /./ (\.\)? */
2370 else if (length >= 3 &&
2371 strcmp(cmpl_dir->fullname + length - 3,
2372 G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) == 0)
2373 cmpl_dir->fullname[length - 2] = 0;
2375 /* Ends with /.. (\..) ? */
2376 else if (length >= 3 &&
2377 strcmp(cmpl_dir->fullname + length - 3,
2378 G_DIR_SEPARATOR_S "..") == 0)
2380 /* Is it just /.. (X:\..)? */
2381 if(cmpl_dir->fullname + length - 3 == first_slash)
2383 cmpl_dir->fullname[length - 2] = 0;
2384 cmpl_dir->fullname_len = length - 2;
2388 if(stat(cmpl_dir->fullname, &sbuf) < 0)
2394 cmpl_dir->fullname[length - 3] = 0;
2396 if(!correct_parent(cmpl_dir, &sbuf))
2400 /* Ends with /../ (\..\)? */
2401 else if (length >= 4 &&
2402 strcmp(cmpl_dir->fullname + length - 4,
2403 G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) == 0)
2405 /* Is it just /../ (X:\..\)? */
2406 if(cmpl_dir->fullname + length - 4 == first_slash)
2408 cmpl_dir->fullname[length - 3] = 0;
2409 cmpl_dir->fullname_len = length - 3;
2413 if(stat(cmpl_dir->fullname, &sbuf) < 0)
2419 cmpl_dir->fullname[length - 4] = 0;
2421 if(!correct_parent(cmpl_dir, &sbuf))
2425 cmpl_dir->fullname_len = strlen(cmpl_dir->fullname);
2431 correct_parent(CompletionDir* cmpl_dir, struct stat *sbuf)
2439 last_slash = strrchr(cmpl_dir->fullname, G_DIR_SEPARATOR);
2440 g_assert(last_slash);
2441 first_slash = strchr(cmpl_dir->fullname, G_DIR_SEPARATOR);
2443 /* Clever (?) way to check for top-level directory that works also on
2444 * Win32, where there is a drive letter and colon prefixed...
2446 if (last_slash != first_slash)
2456 if (stat(cmpl_dir->fullname, &parbuf) < 0)
2460 last_slash[0] = G_DIR_SEPARATOR;
2464 #ifndef G_OS_WIN32 /* No inode numbers on Win32 */
2465 if (parbuf.st_ino == sbuf->st_ino && parbuf.st_dev == sbuf->st_dev)
2466 /* it wasn't a link */
2472 last_slash[0] = G_DIR_SEPARATOR;
2474 /* it was a link, have to figure it out the hard way */
2476 new_name = find_parent_dir_fullname(cmpl_dir->fullname);
2481 g_free(cmpl_dir->fullname);
2483 cmpl_dir->fullname = new_name;
2492 find_parent_dir_fullname(gchar* dirname)
2497 orig_dir = g_get_current_dir ();
2499 if(chdir(dirname) != 0 || chdir("..") != 0)
2505 result = g_get_current_dir ();
2507 if(chdir(orig_dir) != 0)
2519 /**********************************************************************/
2520 /* Completion Operations */
2521 /**********************************************************************/
2525 static PossibleCompletion*
2526 attempt_homedir_completion(gchar* text_to_complete,
2527 CompletionState *cmpl_state)
2531 if (!cmpl_state->user_dir_name_buffer &&
2532 !get_pwdb(cmpl_state))
2534 length = strlen(text_to_complete) - 1;
2536 cmpl_state->user_completion_index += 1;
2538 while(cmpl_state->user_completion_index < cmpl_state->user_directories_len)
2540 index = first_diff_index(text_to_complete + 1,
2541 cmpl_state->user_directories
2542 [cmpl_state->user_completion_index].login);
2549 if(cmpl_state->last_valid_char < (index + 1))
2550 cmpl_state->last_valid_char = index + 1;
2551 cmpl_state->user_completion_index += 1;
2555 cmpl_state->the_completion.is_a_completion = 1;
2556 cmpl_state->the_completion.is_directory = TRUE;
2558 append_completion_text("~", cmpl_state);
2560 append_completion_text(cmpl_state->
2561 user_directories[cmpl_state->user_completion_index].login,
2564 return append_completion_text(G_DIR_SEPARATOR_S, cmpl_state);
2567 if(text_to_complete[1] ||
2568 cmpl_state->user_completion_index > cmpl_state->user_directories_len)
2570 cmpl_state->user_completion_index = -1;
2575 cmpl_state->user_completion_index += 1;
2576 cmpl_state->the_completion.is_a_completion = 1;
2577 cmpl_state->the_completion.is_directory = TRUE;
2579 return append_completion_text("~" G_DIR_SEPARATOR_S, cmpl_state);
2585 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
2586 #define FOLD(c) (tolower(c))
2591 /* returns the index (>= 0) of the first differing character,
2592 * PATTERN_MATCH if the completion matches */
2594 first_diff_index(gchar* pat, gchar* text)
2598 while(*pat && *text && FOLD(*text) == FOLD(*pat))
2608 return PATTERN_MATCH;
2611 static PossibleCompletion*
2612 append_completion_text(gchar* text, CompletionState* cmpl_state)
2616 if(!cmpl_state->the_completion.text)
2619 len = strlen(text) + strlen(cmpl_state->the_completion.text) + 1;
2621 if(cmpl_state->the_completion.text_alloc > len)
2623 strcat(cmpl_state->the_completion.text, text);
2624 return &cmpl_state->the_completion;
2627 while(i < len) { i <<= 1; }
2629 cmpl_state->the_completion.text_alloc = i;
2631 cmpl_state->the_completion.text = (gchar*)g_realloc(cmpl_state->the_completion.text, i);
2633 if(!cmpl_state->the_completion.text)
2637 strcat(cmpl_state->the_completion.text, text);
2638 return &cmpl_state->the_completion;
2642 static CompletionDir*
2643 find_completion_dir(gchar* text_to_complete,
2644 gchar** remaining_text,
2645 CompletionState* cmpl_state)
2647 gchar* first_slash = strchr(text_to_complete, G_DIR_SEPARATOR);
2648 CompletionDir* dir = cmpl_state->reference_dir;
2649 CompletionDir* next;
2650 *remaining_text = text_to_complete;
2654 gint len = first_slash - *remaining_text;
2656 gchar *found_name = NULL; /* Quiet gcc */
2658 gchar* pat_buf = g_new (gchar, len + 1);
2660 strncpy(pat_buf, *remaining_text, len);
2663 for(i = 0; i < dir->sent->entry_count; i += 1)
2665 if(dir->sent->entries[i].is_dir &&
2666 fnmatch(pat_buf, dir->sent->entries[i].entry_name,
2667 FNMATCH_FLAGS)!= FNM_NOMATCH)
2677 found_name = dir->sent->entries[i].entry_name;
2684 /* Perhaps we are trying to open an automount directory */
2685 found_name = pat_buf;
2688 next = open_relative_dir(found_name, dir, cmpl_state);
2696 next->cmpl_parent = dir;
2700 if(!correct_dir_fullname(dir))
2706 *remaining_text = first_slash + 1;
2707 first_slash = strchr(*remaining_text, G_DIR_SEPARATOR);
2716 update_cmpl(PossibleCompletion* poss, CompletionState* cmpl_state)
2720 if(!poss || !cmpl_is_a_completion(poss))
2723 cmpl_len = strlen(cmpl_this_completion(poss));
2725 if(cmpl_state->updated_text_alloc < cmpl_len + 1)
2727 cmpl_state->updated_text =
2728 (gchar*)g_realloc(cmpl_state->updated_text,
2729 cmpl_state->updated_text_alloc);
2730 cmpl_state->updated_text_alloc = 2*cmpl_len;
2733 if(cmpl_state->updated_text_len < 0)
2735 strcpy(cmpl_state->updated_text, cmpl_this_completion(poss));
2736 cmpl_state->updated_text_len = cmpl_len;
2737 cmpl_state->re_complete = cmpl_is_directory(poss);
2739 else if(cmpl_state->updated_text_len == 0)
2741 cmpl_state->re_complete = FALSE;
2746 first_diff_index(cmpl_state->updated_text,
2747 cmpl_this_completion(poss));
2749 cmpl_state->re_complete = FALSE;
2751 if(first_diff == PATTERN_MATCH)
2754 if(first_diff > cmpl_state->updated_text_len)
2755 strcpy(cmpl_state->updated_text, cmpl_this_completion(poss));
2757 cmpl_state->updated_text_len = first_diff;
2758 cmpl_state->updated_text[first_diff] = 0;
2762 static PossibleCompletion*
2763 attempt_file_completion(CompletionState *cmpl_state)
2765 gchar *pat_buf, *first_slash;
2766 CompletionDir *dir = cmpl_state->active_completion_dir;
2768 dir->cmpl_index += 1;
2770 if(dir->cmpl_index == dir->sent->entry_count)
2772 if(dir->cmpl_parent == NULL)
2774 cmpl_state->active_completion_dir = NULL;
2780 cmpl_state->active_completion_dir = dir->cmpl_parent;
2782 return attempt_file_completion(cmpl_state);
2786 g_assert(dir->cmpl_text);
2788 first_slash = strchr(dir->cmpl_text, G_DIR_SEPARATOR);
2792 gint len = first_slash - dir->cmpl_text;
2794 pat_buf = g_new (gchar, len + 1);
2795 strncpy(pat_buf, dir->cmpl_text, len);
2800 gint len = strlen(dir->cmpl_text);
2802 pat_buf = g_new (gchar, len + 2);
2803 strcpy(pat_buf, dir->cmpl_text);
2804 /* Don't append a * if the user entered one herself.
2805 * This way one can complete *.h and don't get matches
2806 * on any .help files, for instance.
2808 if (strchr(pat_buf, '*') == NULL)
2809 strcpy(pat_buf + len, "*");
2814 if(dir->sent->entries[dir->cmpl_index].is_dir)
2816 if(fnmatch(pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
2817 FNMATCH_FLAGS) != FNM_NOMATCH)
2819 CompletionDir* new_dir;
2821 new_dir = open_relative_dir(dir->sent->entries[dir->cmpl_index].entry_name,
2830 new_dir->cmpl_parent = dir;
2832 new_dir->cmpl_index = -1;
2833 new_dir->cmpl_text = first_slash + 1;
2835 cmpl_state->active_completion_dir = new_dir;
2838 return attempt_file_completion(cmpl_state);
2843 return attempt_file_completion(cmpl_state);
2849 return attempt_file_completion(cmpl_state);
2854 if(dir->cmpl_parent != NULL)
2856 append_completion_text(dir->fullname +
2857 strlen(cmpl_state->completion_dir->fullname) + 1,
2859 append_completion_text(G_DIR_SEPARATOR_S, cmpl_state);
2862 append_completion_text(dir->sent->entries[dir->cmpl_index].entry_name, cmpl_state);
2864 cmpl_state->the_completion.is_a_completion =
2865 (fnmatch(pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
2866 FNMATCH_FLAGS) != FNM_NOMATCH);
2868 cmpl_state->the_completion.is_directory = dir->sent->entries[dir->cmpl_index].is_dir;
2869 if(dir->sent->entries[dir->cmpl_index].is_dir)
2870 append_completion_text(G_DIR_SEPARATOR_S, cmpl_state);
2873 return &cmpl_state->the_completion;
2880 get_pwdb(CompletionState* cmpl_state)
2882 struct passwd *pwd_ptr;
2884 gint len = 0, i, count = 0;
2886 if(cmpl_state->user_dir_name_buffer)
2890 while ((pwd_ptr = getpwent()) != NULL)
2892 len += strlen(pwd_ptr->pw_name);
2893 len += strlen(pwd_ptr->pw_dir);
2900 cmpl_state->user_dir_name_buffer = g_new(gchar, len);
2901 cmpl_state->user_directories = g_new(CompletionUserDir, count);
2902 cmpl_state->user_directories_len = count;
2904 buf_ptr = cmpl_state->user_dir_name_buffer;
2906 for(i = 0; i < count; i += 1)
2908 pwd_ptr = getpwent();
2915 strcpy(buf_ptr, pwd_ptr->pw_name);
2916 cmpl_state->user_directories[i].login = buf_ptr;
2917 buf_ptr += strlen(buf_ptr);
2919 strcpy(buf_ptr, pwd_ptr->pw_dir);
2920 cmpl_state->user_directories[i].homedir = buf_ptr;
2921 buf_ptr += strlen(buf_ptr);
2925 qsort(cmpl_state->user_directories,
2926 cmpl_state->user_directories_len,
2927 sizeof(CompletionUserDir),
2936 if(cmpl_state->user_dir_name_buffer)
2937 g_free(cmpl_state->user_dir_name_buffer);
2938 if(cmpl_state->user_directories)
2939 g_free(cmpl_state->user_directories);
2941 cmpl_state->user_dir_name_buffer = NULL;
2942 cmpl_state->user_directories = NULL;
2948 compare_user_dir(const void* a, const void* b)
2950 return strcmp((((CompletionUserDir*)a))->login,
2951 (((CompletionUserDir*)b))->login);
2957 compare_cmpl_dir(const void* a, const void* b)
2959 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
2960 return strcmp((((CompletionDirEntry*)a))->entry_name,
2961 (((CompletionDirEntry*)b))->entry_name);
2963 return g_strcasecmp((((CompletionDirEntry*)a))->entry_name,
2964 (((CompletionDirEntry*)b))->entry_name);
2969 cmpl_state_okay(CompletionState* cmpl_state)
2971 return cmpl_state && cmpl_state->reference_dir;
2975 cmpl_strerror(gint err)
2977 if(err == CMPL_ERRNO_TOO_LONG)
2978 return "Name too long";
2980 return g_strerror (err);