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)
676 gtk_widget_destroy (filesel->fileop_ren_file);
678 if (filesel->fileop_del_file)
680 gtk_widget_destroy (filesel->fileop_del_file);
681 filesel->fileop_del_file = NULL;
684 if (filesel->fileop_c_dir)
686 gtk_widget_destroy (filesel->fileop_c_dir);
687 filesel->fileop_c_dir = NULL;
694 gtk_file_selection_set_filename (GtkFileSelection *filesel,
695 const gchar *filename)
698 const char *name, *last_slash;
700 g_return_if_fail (filesel != NULL);
701 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
702 g_return_if_fail (filename != NULL);
704 last_slash = strrchr (filename, G_DIR_SEPARATOR);
713 buf = g_strdup (filename);
714 buf[last_slash - filename + 1] = 0;
715 name = last_slash + 1;
718 gtk_file_selection_populate (filesel, buf, FALSE);
720 if (filesel->selection_entry)
721 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), name);
726 gtk_file_selection_get_filename (GtkFileSelection *filesel)
728 static gchar nothing[2] = "";
729 static gchar something[MAXPATHLEN*2];
733 g_return_val_if_fail (filesel != NULL, nothing);
734 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), nothing);
737 translate_win32_path(filesel);
739 text = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
742 filename = g_filename_from_utf8 (cmpl_completion_fullname (text, filesel->cmpl_state));
743 strncpy (something, filename, sizeof (something));
752 gtk_file_selection_complete (GtkFileSelection *filesel,
753 const gchar *pattern)
755 g_return_if_fail (filesel != NULL);
756 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
757 g_return_if_fail (pattern != NULL);
759 if (filesel->selection_entry)
760 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), pattern);
761 gtk_file_selection_populate (filesel, (gchar*) pattern, TRUE);
765 gtk_file_selection_destroy (GtkObject *object)
767 GtkFileSelection *filesel;
769 HistoryCallbackArg *callback_arg;
771 g_return_if_fail (GTK_IS_FILE_SELECTION (object));
773 filesel = GTK_FILE_SELECTION (object);
775 if (filesel->fileop_dialog)
777 gtk_widget_destroy (filesel->fileop_dialog);
778 filesel->fileop_dialog = NULL;
781 if (filesel->history_list)
783 list = filesel->history_list;
786 callback_arg = list->data;
787 g_free (callback_arg->directory);
788 g_free (callback_arg);
791 g_list_free (filesel->history_list);
792 filesel->history_list = NULL;
795 if (filesel->cmpl_state)
797 cmpl_free_state (filesel->cmpl_state);
798 filesel->cmpl_state = NULL;
801 GTK_OBJECT_CLASS (parent_class)->destroy (object);
804 /* Begin file operations callbacks */
807 gtk_file_selection_fileop_error (GtkFileSelection *fs, gchar *error_message)
814 g_return_if_fail (error_message != NULL);
817 dialog = gtk_dialog_new ();
819 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
820 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
823 gtk_window_set_title (GTK_WINDOW (dialog), _("Error"));
824 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
826 /* If file dialog is grabbed, make this dialog modal too */
827 /* When error dialog is closed, file dialog will be grabbed again */
828 if (GTK_WINDOW(fs)->modal)
829 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
831 vbox = gtk_vbox_new(FALSE, 0);
832 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
833 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
835 gtk_widget_show(vbox);
837 label = gtk_label_new(error_message);
838 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
839 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
840 gtk_widget_show(label);
842 /* yes, we free it */
843 g_free (error_message);
846 button = gtk_button_new_with_label (_("Close"));
847 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
848 (GtkSignalFunc) gtk_widget_destroy,
850 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
851 button, TRUE, TRUE, 0);
852 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
853 gtk_widget_grab_default(button);
854 gtk_widget_show (button);
856 gtk_widget_show (dialog);
860 gtk_file_selection_fileop_destroy (GtkWidget *widget, gpointer data)
862 GtkFileSelection *fs = data;
864 g_return_if_fail (fs != NULL);
865 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
867 fs->fileop_dialog = NULL;
872 gtk_file_selection_create_dir_confirmed (GtkWidget *widget, gpointer data)
874 GtkFileSelection *fs = data;
879 CompletionState *cmpl_state;
881 g_return_if_fail (fs != NULL);
882 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
884 dirname = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
885 cmpl_state = (CompletionState*) fs->cmpl_state;
886 path = cmpl_reference_position (cmpl_state);
888 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, dirname, NULL);
889 if ( (mkdir (full_path, 0755) < 0) )
891 buf = g_strconcat ("Error creating directory \"", dirname, "\": ",
892 g_strerror(errno), NULL);
893 gtk_file_selection_fileop_error (fs, buf);
897 gtk_widget_destroy (fs->fileop_dialog);
898 gtk_file_selection_populate (fs, "", FALSE);
902 gtk_file_selection_create_dir (GtkWidget *widget, gpointer data)
904 GtkFileSelection *fs = data;
910 g_return_if_fail (fs != NULL);
911 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
913 if (fs->fileop_dialog)
917 dialog = gtk_dialog_new ();
918 fs->fileop_dialog = dialog;
919 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
920 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
922 gtk_window_set_title (GTK_WINDOW (dialog), _("Create Directory"));
923 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
925 /* If file dialog is grabbed, grab option dialog */
926 /* When option dialog is closed, file dialog will be grabbed again */
927 if (GTK_WINDOW(fs)->modal)
928 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
930 vbox = gtk_vbox_new(FALSE, 0);
931 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
932 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
934 gtk_widget_show(vbox);
936 label = gtk_label_new(_("Directory name:"));
937 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
938 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
939 gtk_widget_show(label);
941 /* The directory entry widget */
942 fs->fileop_entry = gtk_entry_new ();
943 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
945 GTK_WIDGET_SET_FLAGS(fs->fileop_entry, GTK_CAN_DEFAULT);
946 gtk_widget_show (fs->fileop_entry);
949 button = gtk_button_new_with_label (_("Create"));
950 gtk_signal_connect (GTK_OBJECT (button), "clicked",
951 (GtkSignalFunc) gtk_file_selection_create_dir_confirmed,
953 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
954 button, TRUE, TRUE, 0);
955 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
956 gtk_widget_show(button);
958 button = gtk_button_new_with_label (_("Cancel"));
959 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
960 (GtkSignalFunc) gtk_widget_destroy,
962 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
963 button, TRUE, TRUE, 0);
964 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
965 gtk_widget_grab_default(button);
966 gtk_widget_show (button);
968 gtk_widget_show (dialog);
972 gtk_file_selection_delete_file_confirmed (GtkWidget *widget, gpointer data)
974 GtkFileSelection *fs = data;
975 CompletionState *cmpl_state;
980 g_return_if_fail (fs != NULL);
981 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
983 cmpl_state = (CompletionState*) fs->cmpl_state;
984 path = cmpl_reference_position (cmpl_state);
986 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
987 if ( (unlink (full_path) < 0) )
989 buf = g_strconcat ("Error deleting file \"", fs->fileop_file, "\": ",
990 g_strerror(errno), NULL);
991 gtk_file_selection_fileop_error (fs, buf);
995 gtk_widget_destroy (fs->fileop_dialog);
996 gtk_file_selection_populate (fs, "", FALSE);
1000 gtk_file_selection_delete_file (GtkWidget *widget, gpointer data)
1002 GtkFileSelection *fs = data;
1010 g_return_if_fail (fs != NULL);
1011 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1013 if (fs->fileop_dialog)
1016 #ifdef G_WITH_CYGWIN
1017 translate_win32_path(fs);
1020 filename = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1021 if (strlen(filename) < 1)
1024 fs->fileop_file = filename;
1027 fs->fileop_dialog = dialog = gtk_dialog_new ();
1028 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
1029 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
1031 gtk_window_set_title (GTK_WINDOW (dialog), _("Delete File"));
1032 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1034 /* If file dialog is grabbed, grab option dialog */
1035 /* When option dialog is closed, file dialog will be grabbed again */
1036 if (GTK_WINDOW(fs)->modal)
1037 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
1039 vbox = gtk_vbox_new(FALSE, 0);
1040 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1041 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
1043 gtk_widget_show(vbox);
1045 buf = g_strconcat ("Really delete file \"", filename, "\" ?", NULL);
1046 label = gtk_label_new(buf);
1047 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
1048 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
1049 gtk_widget_show(label);
1053 button = gtk_button_new_with_label (_("Delete"));
1054 gtk_signal_connect (GTK_OBJECT (button), "clicked",
1055 (GtkSignalFunc) gtk_file_selection_delete_file_confirmed,
1057 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1058 button, TRUE, TRUE, 0);
1059 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1060 gtk_widget_show(button);
1062 button = gtk_button_new_with_label (_("Cancel"));
1063 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1064 (GtkSignalFunc) gtk_widget_destroy,
1066 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1067 button, TRUE, TRUE, 0);
1068 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1069 gtk_widget_grab_default(button);
1070 gtk_widget_show (button);
1072 gtk_widget_show (dialog);
1077 gtk_file_selection_rename_file_confirmed (GtkWidget *widget, gpointer data)
1079 GtkFileSelection *fs = data;
1083 gchar *new_filename;
1084 gchar *old_filename;
1085 CompletionState *cmpl_state;
1087 g_return_if_fail (fs != NULL);
1088 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1090 file = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
1091 cmpl_state = (CompletionState*) fs->cmpl_state;
1092 path = cmpl_reference_position (cmpl_state);
1094 new_filename = g_strconcat (path, G_DIR_SEPARATOR_S, file, NULL);
1095 old_filename = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1097 if ( (rename (old_filename, new_filename)) < 0)
1099 buf = g_strconcat ("Error renaming file \"", file, "\": ",
1100 g_strerror(errno), NULL);
1101 gtk_file_selection_fileop_error (fs, buf);
1103 g_free (new_filename);
1104 g_free (old_filename);
1106 gtk_widget_destroy (fs->fileop_dialog);
1107 gtk_file_selection_populate (fs, "", FALSE);
1111 gtk_file_selection_rename_file (GtkWidget *widget, gpointer data)
1113 GtkFileSelection *fs = data;
1120 g_return_if_fail (fs != NULL);
1121 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1123 if (fs->fileop_dialog)
1126 fs->fileop_file = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1127 if (strlen(fs->fileop_file) < 1)
1131 fs->fileop_dialog = dialog = gtk_dialog_new ();
1132 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
1133 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
1135 gtk_window_set_title (GTK_WINDOW (dialog), _("Rename File"));
1136 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1138 /* If file dialog is grabbed, grab option dialog */
1139 /* When option dialog closed, file dialog will be grabbed again */
1140 if (GTK_WINDOW(fs)->modal)
1141 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
1143 vbox = gtk_vbox_new(FALSE, 0);
1144 gtk_container_set_border_width (GTK_CONTAINER(vbox), 8);
1145 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
1147 gtk_widget_show(vbox);
1149 buf = g_strconcat ("Rename file \"", fs->fileop_file, "\" to:", NULL);
1150 label = gtk_label_new(buf);
1151 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
1152 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
1153 gtk_widget_show(label);
1156 /* New filename entry */
1157 fs->fileop_entry = gtk_entry_new ();
1158 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1160 GTK_WIDGET_SET_FLAGS(fs->fileop_entry, GTK_CAN_DEFAULT);
1161 gtk_widget_show (fs->fileop_entry);
1163 gtk_entry_set_text (GTK_ENTRY (fs->fileop_entry), fs->fileop_file);
1164 gtk_editable_select_region (GTK_EDITABLE (fs->fileop_entry),
1165 0, strlen (fs->fileop_file));
1168 button = gtk_button_new_with_label (_("Rename"));
1169 gtk_signal_connect (GTK_OBJECT (button), "clicked",
1170 (GtkSignalFunc) gtk_file_selection_rename_file_confirmed,
1172 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1173 button, TRUE, TRUE, 0);
1174 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1175 gtk_widget_show(button);
1177 button = gtk_button_new_with_label (_("Cancel"));
1178 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1179 (GtkSignalFunc) gtk_widget_destroy,
1181 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1182 button, TRUE, TRUE, 0);
1183 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1184 gtk_widget_grab_default(button);
1185 gtk_widget_show (button);
1187 gtk_widget_show (dialog);
1192 gtk_file_selection_key_press (GtkWidget *widget,
1196 GtkFileSelection *fs;
1199 g_return_val_if_fail (widget != NULL, FALSE);
1200 g_return_val_if_fail (event != NULL, FALSE);
1202 if (event->keyval == GDK_Tab)
1204 fs = GTK_FILE_SELECTION (user_data);
1205 #ifdef G_WITH_CYGWIN
1206 translate_win32_path(fs);
1208 text = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1210 text = g_strdup (text);
1212 gtk_file_selection_populate (fs, text, TRUE);
1216 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "key_press_event");
1226 gtk_file_selection_history_callback (GtkWidget *widget, gpointer data)
1228 GtkFileSelection *fs = data;
1229 HistoryCallbackArg *callback_arg;
1232 g_return_if_fail (fs != NULL);
1233 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1235 list = fs->history_list;
1238 callback_arg = list->data;
1240 if (callback_arg->menu_item == widget)
1242 gtk_file_selection_populate (fs, callback_arg->directory, FALSE);
1251 gtk_file_selection_update_history_menu (GtkFileSelection *fs,
1252 gchar *current_directory)
1254 HistoryCallbackArg *callback_arg;
1255 GtkWidget *menu_item;
1261 g_return_if_fail (fs != NULL);
1262 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1263 g_return_if_fail (current_directory != NULL);
1265 list = fs->history_list;
1267 if (fs->history_menu)
1270 callback_arg = list->data;
1271 g_free (callback_arg->directory);
1272 g_free (callback_arg);
1275 g_list_free (fs->history_list);
1276 fs->history_list = NULL;
1278 gtk_widget_destroy (fs->history_menu);
1281 fs->history_menu = gtk_menu_new();
1283 current_dir = g_strdup (current_directory);
1285 dir_len = strlen (current_dir);
1287 for (i = dir_len; i >= 0; i--)
1289 /* the i == dir_len is to catch the full path for the first
1291 if ( (current_dir[i] == G_DIR_SEPARATOR) || (i == dir_len))
1293 /* another small hack to catch the full path */
1295 current_dir[i + 1] = '\0';
1296 #ifdef G_WITH_CYGWIN
1297 if (!strcmp(current_dir, "//"))
1300 menu_item = gtk_menu_item_new_with_label (current_dir);
1302 callback_arg = g_new (HistoryCallbackArg, 1);
1303 callback_arg->menu_item = menu_item;
1305 /* since the autocompletion gets confused if you don't
1306 * supply a trailing '/' on a dir entry, set the full
1307 * (current) path to "" which just refreshes the filesel */
1310 callback_arg->directory = g_strdup ("");
1314 callback_arg->directory = g_strdup (current_dir);
1317 fs->history_list = g_list_append (fs->history_list, callback_arg);
1319 gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
1320 (GtkSignalFunc) gtk_file_selection_history_callback,
1322 gtk_menu_append (GTK_MENU (fs->history_menu), menu_item);
1323 gtk_widget_show (menu_item);
1327 gtk_option_menu_set_menu (GTK_OPTION_MENU (fs->history_pulldown),
1329 g_free (current_dir);
1333 gtk_file_selection_file_button (GtkWidget *widget,
1336 GdkEventButton *bevent,
1339 GtkFileSelection *fs = NULL;
1340 gchar *filename, *temp = NULL;
1342 g_return_if_fail (GTK_IS_CLIST (widget));
1345 g_return_if_fail (fs != NULL);
1346 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1348 gtk_clist_get_text (GTK_CLIST (fs->file_list), row, 0, &temp);
1349 filename = g_strdup (temp);
1351 #ifdef G_WITH_CYGWIN
1352 /* Check to see if the selection was a drive selector */
1353 if (isalpha(filename[0]) && (filename[1] == ':')) {
1354 /* It is... map it to a CYGWIN32 drive */
1355 char temp_filename[10];
1357 sprintf(temp_filename, "//%c/", tolower(filename[0]));
1359 filename = g_strdup(temp_filename);
1361 #endif /* G_WITH_CYGWIN */
1366 switch (bevent->type)
1368 case GDK_2BUTTON_PRESS:
1369 gtk_button_clicked (GTK_BUTTON (fs->ok_button));
1373 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1377 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1384 gtk_file_selection_dir_button (GtkWidget *widget,
1387 GdkEventButton *bevent,
1390 GtkFileSelection *fs = NULL;
1391 gchar *filename, *temp = NULL;
1393 g_return_if_fail (GTK_IS_CLIST (widget));
1395 fs = GTK_FILE_SELECTION (user_data);
1396 g_return_if_fail (fs != NULL);
1397 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1399 gtk_clist_get_text (GTK_CLIST (fs->dir_list), row, 0, &temp);
1400 filename = g_strdup (temp);
1405 switch (bevent->type)
1407 case GDK_2BUTTON_PRESS:
1408 gtk_file_selection_populate (fs, filename, FALSE);
1412 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1416 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1422 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
1425 win32_gtk_add_drives_to_dir_list(GtkWidget *the_dir_list)
1427 gchar *text[2], *textPtr;
1429 char volumeNameBuf[128];
1430 char formatBuffer[128];
1435 /* Get the Drives string */
1436 GetLogicalDriveStrings(sizeof(buffer), buffer);
1438 /* Add the drives as necessary */
1440 while (*textPtr != '\0') {
1441 /* Get the volume information for this drive */
1442 if ((tolower(textPtr[0]) != 'a') && (tolower(textPtr[0]) != 'b'))
1444 /* Ignore floppies (?) */
1445 DWORD maxComponentLength, flags;
1447 GetVolumeInformation(textPtr,
1448 volumeNameBuf, sizeof(volumeNameBuf),
1449 NULL, &maxComponentLength,
1451 /* Build the actual displayable string */
1453 sprintf(formatBuffer, "%c:\\", toupper(textPtr[0]));
1454 #if 0 /* HB: removed to allow drive change AND directory update with one click */
1455 if (strlen(volumeNameBuf) > 0)
1456 sprintf(formatBuffer, "%s (%s)", formatBuffer, volumeNameBuf);
1458 /* Add to the list */
1459 text[0] = formatBuffer;
1460 row = gtk_clist_append (GTK_CLIST (the_dir_list), text);
1462 textPtr += (strlen(textPtr) + 1);
1468 gtk_file_selection_populate (GtkFileSelection *fs,
1472 CompletionState *cmpl_state;
1473 PossibleCompletion* poss;
1476 gchar* rem_path = rel_path;
1479 gint did_recurse = FALSE;
1480 gint possible_count = 0;
1481 gint selection_index = -1;
1482 gint file_list_width;
1483 gint dir_list_width;
1485 g_return_if_fail (fs != NULL);
1486 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1488 cmpl_state = (CompletionState*) fs->cmpl_state;
1489 poss = cmpl_completion_matches (rel_path, &rem_path, cmpl_state);
1491 if (!cmpl_state_okay (cmpl_state))
1493 /* Something went wrong. */
1494 gtk_file_selection_abort (fs);
1498 g_assert (cmpl_state->reference_dir);
1500 gtk_clist_freeze (GTK_CLIST (fs->dir_list));
1501 gtk_clist_clear (GTK_CLIST (fs->dir_list));
1502 gtk_clist_freeze (GTK_CLIST (fs->file_list));
1503 gtk_clist_clear (GTK_CLIST (fs->file_list));
1505 /* Set the dir_list to include ./ and ../ */
1507 text[0] = "." G_DIR_SEPARATOR_S;
1508 row = gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1510 text[0] = ".." G_DIR_SEPARATOR_S;
1511 row = gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1513 /*reset the max widths of the lists*/
1514 dir_list_width = gdk_string_width(fs->dir_list->style->font,".." G_DIR_SEPARATOR_S);
1515 gtk_clist_set_column_width(GTK_CLIST(fs->dir_list),0,dir_list_width);
1516 file_list_width = 1;
1517 gtk_clist_set_column_width(GTK_CLIST(fs->file_list),0,file_list_width);
1521 if (cmpl_is_a_completion (poss))
1523 possible_count += 1;
1525 filename = cmpl_this_completion (poss);
1529 if (cmpl_is_directory (poss))
1531 if (strcmp (filename, "." G_DIR_SEPARATOR_S) != 0 &&
1532 strcmp (filename, ".." G_DIR_SEPARATOR_S) != 0)
1534 int width = gdk_string_width(fs->dir_list->style->font,
1536 row = gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1537 if(width > dir_list_width)
1539 dir_list_width = width;
1540 gtk_clist_set_column_width(GTK_CLIST(fs->dir_list),0,
1547 int width = gdk_string_width(fs->file_list->style->font,
1549 row = gtk_clist_append (GTK_CLIST (fs->file_list), text);
1550 if(width > file_list_width)
1552 file_list_width = width;
1553 gtk_clist_set_column_width(GTK_CLIST(fs->file_list),0,
1559 poss = cmpl_next_completion (cmpl_state);
1562 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
1563 /* For Windows, add drives as potential selections */
1564 win32_gtk_add_drives_to_dir_list (fs->dir_list);
1567 gtk_clist_thaw (GTK_CLIST (fs->dir_list));
1568 gtk_clist_thaw (GTK_CLIST (fs->file_list));
1570 /* File lists are set. */
1572 g_assert (cmpl_state->reference_dir);
1577 /* User is trying to complete filenames, so advance the user's input
1578 * string to the updated_text, which is the common leading substring
1579 * of all possible completions, and if its a directory attempt
1580 * attempt completions in it. */
1582 if (cmpl_updated_text (cmpl_state)[0])
1585 if (cmpl_updated_dir (cmpl_state))
1587 gchar* dir_name = g_strdup (cmpl_updated_text (cmpl_state));
1591 gtk_file_selection_populate (fs, dir_name, TRUE);
1597 if (fs->selection_entry)
1598 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry),
1599 cmpl_updated_text (cmpl_state));
1604 selection_index = cmpl_last_valid_char (cmpl_state) -
1605 (strlen (rel_path) - strlen (rem_path));
1606 if (fs->selection_entry)
1607 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), rem_path);
1612 if (fs->selection_entry)
1613 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
1618 if (fs->selection_entry)
1619 gtk_entry_set_position (GTK_ENTRY (fs->selection_entry), selection_index);
1621 if (fs->selection_entry)
1623 sel_text = g_strconcat (_("Selection: "),
1624 cmpl_reference_position (cmpl_state),
1627 gtk_label_set_text (GTK_LABEL (fs->selection_text), sel_text);
1631 if (fs->history_pulldown)
1633 gtk_file_selection_update_history_menu (fs, cmpl_reference_position (cmpl_state));
1640 gtk_file_selection_abort (GtkFileSelection *fs)
1644 sprintf (err_buf, _("Directory unreadable: %s"), cmpl_strerror (cmpl_errno));
1646 /* BEEP gdk_beep(); */
1648 if (fs->selection_entry)
1649 gtk_label_set_text (GTK_LABEL (fs->selection_text), err_buf);
1652 /**********************************************************************/
1653 /* External Interface */
1654 /**********************************************************************/
1656 /* The four completion state selectors
1659 cmpl_updated_text (CompletionState* cmpl_state)
1661 return cmpl_state->updated_text;
1665 cmpl_updated_dir (CompletionState* cmpl_state)
1667 return cmpl_state->re_complete;
1671 cmpl_reference_position (CompletionState* cmpl_state)
1673 return cmpl_state->reference_dir->fullname;
1677 cmpl_last_valid_char (CompletionState* cmpl_state)
1679 return cmpl_state->last_valid_char;
1683 cmpl_completion_fullname (gchar* text, CompletionState* cmpl_state)
1685 static char nothing[2] = "";
1687 if (!cmpl_state_okay (cmpl_state))
1691 else if (g_path_is_absolute (text))
1693 strcpy (cmpl_state->updated_text, text);
1696 else if (text[0] == '~')
1701 dir = open_user_dir (text, cmpl_state);
1705 /* spencer says just return ~something, so
1706 * for now just do it. */
1707 strcpy (cmpl_state->updated_text, text);
1712 strcpy (cmpl_state->updated_text, dir->fullname);
1714 slash = strchr (text, G_DIR_SEPARATOR);
1717 strcat (cmpl_state->updated_text, slash);
1723 strcpy (cmpl_state->updated_text, cmpl_state->reference_dir->fullname);
1724 if (cmpl_state->updated_text[strlen (cmpl_state->updated_text) - 1] != G_DIR_SEPARATOR)
1725 strcat (cmpl_state->updated_text, G_DIR_SEPARATOR_S);
1726 strcat (cmpl_state->updated_text, text);
1729 return cmpl_state->updated_text;
1732 /* The three completion selectors
1735 cmpl_this_completion (PossibleCompletion* pc)
1741 cmpl_is_directory (PossibleCompletion* pc)
1743 return pc->is_directory;
1747 cmpl_is_a_completion (PossibleCompletion* pc)
1749 return pc->is_a_completion;
1752 /**********************************************************************/
1753 /* Construction, deletion */
1754 /**********************************************************************/
1756 static CompletionState*
1757 cmpl_init_state (void)
1760 CompletionState *new_state;
1762 new_state = g_new (CompletionState, 1);
1764 getcwd_buf = g_get_current_dir ();
1768 new_state->reference_dir = NULL;
1769 new_state->completion_dir = NULL;
1770 new_state->active_completion_dir = NULL;
1771 new_state->directory_storage = NULL;
1772 new_state->directory_sent_storage = NULL;
1773 new_state->last_valid_char = 0;
1774 new_state->updated_text = g_new (gchar, MAXPATHLEN);
1775 new_state->updated_text_alloc = MAXPATHLEN;
1776 new_state->the_completion.text = g_new (gchar, MAXPATHLEN);
1777 new_state->the_completion.text_alloc = MAXPATHLEN;
1778 new_state->user_dir_name_buffer = NULL;
1779 new_state->user_directories = NULL;
1781 new_state->reference_dir = open_dir (getcwd_buf, new_state);
1783 if (!new_state->reference_dir)
1785 /* Directories changing from underneath us, grumble */
1786 strcpy (getcwd_buf, G_DIR_SEPARATOR_S);
1790 g_free (getcwd_buf);
1795 cmpl_free_dir_list(GList* dp0)
1800 free_dir (dp->data);
1808 cmpl_free_dir_sent_list(GList* dp0)
1813 free_dir_sent (dp->data);
1821 cmpl_free_state (CompletionState* cmpl_state)
1823 g_return_if_fail (cmpl_state != NULL);
1825 cmpl_free_dir_list (cmpl_state->directory_storage);
1826 cmpl_free_dir_sent_list (cmpl_state->directory_sent_storage);
1828 if (cmpl_state->user_dir_name_buffer)
1829 g_free (cmpl_state->user_dir_name_buffer);
1830 if (cmpl_state->user_directories)
1831 g_free (cmpl_state->user_directories);
1832 if (cmpl_state->the_completion.text)
1833 g_free (cmpl_state->the_completion.text);
1834 if (cmpl_state->updated_text)
1835 g_free (cmpl_state->updated_text);
1837 g_free (cmpl_state);
1841 free_dir(CompletionDir* dir)
1843 g_free(dir->fullname);
1848 free_dir_sent(CompletionDirSent* sent)
1851 for (i = 0; i < sent->entry_count; i++)
1852 g_free(sent->entries[i].entry_name);
1853 g_free(sent->entries);
1858 prune_memory_usage(CompletionState *cmpl_state)
1860 GList* cdsl = cmpl_state->directory_sent_storage;
1861 GList* cdl = cmpl_state->directory_storage;
1865 for(; cdsl && len < CMPL_DIRECTORY_CACHE_SIZE; len += 1)
1869 cmpl_free_dir_sent_list(cdsl->next);
1873 cmpl_state->directory_storage = NULL;
1875 if (cdl->data == cmpl_state->reference_dir)
1876 cmpl_state->directory_storage = g_list_prepend(NULL, cdl->data);
1878 free_dir (cdl->data);
1885 /**********************************************************************/
1886 /* The main entrances. */
1887 /**********************************************************************/
1889 static PossibleCompletion*
1890 cmpl_completion_matches (gchar* text_to_complete,
1891 gchar** remaining_text,
1892 CompletionState* cmpl_state)
1895 PossibleCompletion *poss;
1897 prune_memory_usage(cmpl_state);
1899 g_assert (text_to_complete != NULL);
1901 cmpl_state->user_completion_index = -1;
1902 cmpl_state->last_completion_text = text_to_complete;
1903 cmpl_state->the_completion.text[0] = 0;
1904 cmpl_state->last_valid_char = 0;
1905 cmpl_state->updated_text_len = -1;
1906 cmpl_state->updated_text[0] = 0;
1907 cmpl_state->re_complete = FALSE;
1910 first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
1912 if (text_to_complete[0] == '~' && !first_slash)
1914 /* Text starts with ~ and there is no slash, show all the
1915 * home directory completions.
1917 poss = attempt_homedir_completion (text_to_complete, cmpl_state);
1919 update_cmpl(poss, cmpl_state);
1924 cmpl_state->reference_dir =
1925 open_ref_dir (text_to_complete, remaining_text, cmpl_state);
1927 if(!cmpl_state->reference_dir)
1930 cmpl_state->completion_dir =
1931 find_completion_dir (*remaining_text, remaining_text, cmpl_state);
1933 cmpl_state->last_valid_char = *remaining_text - text_to_complete;
1935 if(!cmpl_state->completion_dir)
1938 cmpl_state->completion_dir->cmpl_index = -1;
1939 cmpl_state->completion_dir->cmpl_parent = NULL;
1940 cmpl_state->completion_dir->cmpl_text = *remaining_text;
1942 cmpl_state->active_completion_dir = cmpl_state->completion_dir;
1944 cmpl_state->reference_dir = cmpl_state->completion_dir;
1946 poss = attempt_file_completion(cmpl_state);
1948 update_cmpl(poss, cmpl_state);
1953 static PossibleCompletion*
1954 cmpl_next_completion (CompletionState* cmpl_state)
1956 PossibleCompletion* poss = NULL;
1958 cmpl_state->the_completion.text[0] = 0;
1961 if(cmpl_state->user_completion_index >= 0)
1962 poss = attempt_homedir_completion(cmpl_state->last_completion_text, cmpl_state);
1964 poss = attempt_file_completion(cmpl_state);
1966 poss = attempt_file_completion(cmpl_state);
1969 update_cmpl(poss, cmpl_state);
1974 /**********************************************************************/
1975 /* Directory Operations */
1976 /**********************************************************************/
1978 /* Open the directory where completion will begin from, if possible. */
1979 static CompletionDir*
1980 open_ref_dir(gchar* text_to_complete,
1981 gchar** remaining_text,
1982 CompletionState* cmpl_state)
1985 CompletionDir *new_dir;
1987 first_slash = strchr(text_to_complete, G_DIR_SEPARATOR);
1989 #ifdef G_WITH_CYGWIN
1990 if (text_to_complete[0] == '/' && text_to_complete[1] == '/')
1993 sprintf(root_dir, "//%c", text_to_complete[2]);
1995 new_dir = open_dir(root_dir, cmpl_state);
1998 *remaining_text = text_to_complete + 4;
2006 else if (text_to_complete[0] == '~')
2008 new_dir = open_user_dir(text_to_complete, cmpl_state);
2013 *remaining_text = first_slash + 1;
2015 *remaining_text = text_to_complete + strlen(text_to_complete);
2023 else if (g_path_is_absolute (text_to_complete) || !cmpl_state->reference_dir)
2025 gchar *tmp = g_strdup(text_to_complete);
2029 while (*p && *p != '*' && *p != '?')
2033 p = strrchr(tmp, G_DIR_SEPARATOR);
2041 new_dir = open_dir(tmp, cmpl_state);
2044 *remaining_text = text_to_complete +
2045 ((p == tmp + 1) ? (p - tmp) : (p + 1 - tmp));
2049 /* If no possible candidates, use the cwd */
2050 gchar *curdir = g_get_current_dir ();
2052 new_dir = open_dir(curdir, cmpl_state);
2055 *remaining_text = text_to_complete;
2064 *remaining_text = text_to_complete;
2066 new_dir = open_dir(cmpl_state->reference_dir->fullname, cmpl_state);
2071 new_dir->cmpl_index = -1;
2072 new_dir->cmpl_parent = NULL;
2080 /* open a directory by user name */
2081 static CompletionDir*
2082 open_user_dir(gchar* text_to_complete,
2083 CompletionState *cmpl_state)
2088 g_assert(text_to_complete && text_to_complete[0] == '~');
2090 first_slash = strchr(text_to_complete, G_DIR_SEPARATOR);
2093 cmp_len = first_slash - text_to_complete - 1;
2095 cmp_len = strlen(text_to_complete + 1);
2100 gchar *homedir = g_get_home_dir ();
2103 return open_dir(homedir, cmpl_state);
2110 char* copy = g_new(char, cmp_len + 1);
2112 strncpy(copy, text_to_complete + 1, cmp_len);
2114 pwd = getpwnam(copy);
2122 return open_dir(pwd->pw_dir, cmpl_state);
2128 /* open a directory relative the the current relative directory */
2129 static CompletionDir*
2130 open_relative_dir(gchar* dir_name,
2132 CompletionState *cmpl_state)
2134 CompletionDir *result;
2137 path = g_string_sized_new (dir->fullname_len + strlen (dir_name) + 10);
2138 g_string_assign (path, dir->fullname);
2140 if(dir->fullname_len > 1
2141 && path->str[dir->fullname_len - 1] != G_DIR_SEPARATOR)
2142 g_string_append_c (path, G_DIR_SEPARATOR);
2143 g_string_append (path, dir_name);
2145 result = open_dir(path->str, cmpl_state);
2147 g_string_free (path, TRUE);
2152 /* after the cache lookup fails, really open a new directory */
2153 static CompletionDirSent*
2154 open_new_dir(gchar* dir_name, struct stat* sbuf, gboolean stat_subdirs)
2156 CompletionDirSent* sent;
2158 struct dirent *dirent_ptr;
2159 gint entry_count = 0;
2161 struct stat ent_sbuf;
2165 sent = g_new(CompletionDirSent, 1);
2166 sent->mtime = sbuf->st_mtime;
2167 sent->inode = sbuf->st_ino;
2168 sent->device = sbuf->st_dev;
2170 path = g_string_sized_new (2*MAXPATHLEN + 10);
2172 xdir = g_filename_from_utf8 (dir_name);
2173 directory = opendir(xdir);
2182 while((dirent_ptr = readdir(directory)) != NULL)
2187 sent->entries = g_new(CompletionDirEntry, entry_count);
2188 sent->entry_count = entry_count;
2190 rewinddir(directory);
2192 for(i = 0; i < entry_count; i += 1)
2194 dirent_ptr = readdir(directory);
2199 closedir(directory);
2203 sent->entries[i].entry_name = g_filename_to_utf8 (dirent_ptr->d_name);
2205 g_string_assign (path, dir_name);
2206 if (path->str[path->len-1] != G_DIR_SEPARATOR)
2208 g_string_append_c (path, G_DIR_SEPARATOR);
2210 g_string_append (path, dirent_ptr->d_name);
2214 if(stat(path->str, &ent_sbuf) >= 0 && S_ISDIR(ent_sbuf.st_mode))
2215 sent->entries[i].is_dir = TRUE;
2217 /* stat may fail, and we don't mind, since it could be a
2218 * dangling symlink. */
2219 sent->entries[i].is_dir = FALSE;
2222 sent->entries[i].is_dir = 1;
2225 g_string_free (path, TRUE);
2226 qsort(sent->entries, sent->entry_count, sizeof(CompletionDirEntry), compare_cmpl_dir);
2228 closedir(directory);
2233 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
2236 check_dir(gchar *dir_name, struct stat *result, gboolean *stat_subdirs)
2238 /* A list of directories that we know only contain other directories.
2239 * Trying to stat every file in these directories would be very
2246 struct stat statbuf;
2247 } no_stat_dirs[] = {
2248 { "/afs", FALSE, { 0 } },
2249 { "/net", FALSE, { 0 } }
2252 static const gint n_no_stat_dirs = sizeof(no_stat_dirs) / sizeof(no_stat_dirs[0]);
2253 static gboolean initialized = FALSE;
2260 for (i = 0; i < n_no_stat_dirs; i++)
2262 if (stat (no_stat_dirs[i].name, &no_stat_dirs[i].statbuf) == 0)
2263 no_stat_dirs[i].present = TRUE;
2267 if(stat(dir_name, result) < 0)
2273 *stat_subdirs = TRUE;
2274 for (i=0; i<n_no_stat_dirs; i++)
2276 if (no_stat_dirs[i].present &&
2277 (no_stat_dirs[i].statbuf.st_dev == result->st_dev) &&
2278 (no_stat_dirs[i].statbuf.st_ino == result->st_ino))
2280 *stat_subdirs = FALSE;
2290 /* open a directory by absolute pathname */
2291 static CompletionDir*
2292 open_dir(gchar* dir_name, CompletionState* cmpl_state)
2295 gboolean stat_subdirs;
2296 CompletionDirSent *sent;
2299 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
2300 if (!check_dir (dir_name, &sbuf, &stat_subdirs))
2303 cdsl = cmpl_state->directory_sent_storage;
2309 if(sent->inode == sbuf.st_ino &&
2310 sent->mtime == sbuf.st_mtime &&
2311 sent->device == sbuf.st_dev)
2312 return attach_dir(sent, dir_name, cmpl_state);
2317 stat_subdirs = TRUE;
2320 sent = open_new_dir(dir_name, &sbuf, stat_subdirs);
2323 cmpl_state->directory_sent_storage =
2324 g_list_prepend(cmpl_state->directory_sent_storage, sent);
2326 return attach_dir(sent, dir_name, cmpl_state);
2332 static CompletionDir*
2333 attach_dir(CompletionDirSent* sent, gchar* dir_name, CompletionState *cmpl_state)
2335 CompletionDir* new_dir;
2337 new_dir = g_new(CompletionDir, 1);
2339 cmpl_state->directory_storage =
2340 g_list_prepend(cmpl_state->directory_storage, new_dir);
2342 new_dir->sent = sent;
2343 new_dir->fullname = g_strdup(dir_name);
2344 new_dir->fullname_len = strlen(dir_name);
2350 correct_dir_fullname(CompletionDir* cmpl_dir)
2352 gint length = strlen(cmpl_dir->fullname);
2353 gchar *first_slash = strchr(cmpl_dir->fullname, G_DIR_SEPARATOR);
2356 /* Does it end with /. (\.) ? */
2358 strcmp(cmpl_dir->fullname + length - 2, G_DIR_SEPARATOR_S ".") == 0)
2360 /* Is it just the root directory (on a drive) ? */
2361 if (cmpl_dir->fullname + length - 2 == first_slash)
2363 cmpl_dir->fullname[length - 1] = 0;
2364 cmpl_dir->fullname_len = length - 1;
2369 cmpl_dir->fullname[length - 2] = 0;
2373 /* Ends with /./ (\.\)? */
2374 else if (length >= 3 &&
2375 strcmp(cmpl_dir->fullname + length - 3,
2376 G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) == 0)
2377 cmpl_dir->fullname[length - 2] = 0;
2379 /* Ends with /.. (\..) ? */
2380 else if (length >= 3 &&
2381 strcmp(cmpl_dir->fullname + length - 3,
2382 G_DIR_SEPARATOR_S "..") == 0)
2384 /* Is it just /.. (X:\..)? */
2385 if(cmpl_dir->fullname + length - 3 == first_slash)
2387 cmpl_dir->fullname[length - 2] = 0;
2388 cmpl_dir->fullname_len = length - 2;
2392 if(stat(cmpl_dir->fullname, &sbuf) < 0)
2398 cmpl_dir->fullname[length - 3] = 0;
2400 if(!correct_parent(cmpl_dir, &sbuf))
2404 /* Ends with /../ (\..\)? */
2405 else if (length >= 4 &&
2406 strcmp(cmpl_dir->fullname + length - 4,
2407 G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) == 0)
2409 /* Is it just /../ (X:\..\)? */
2410 if(cmpl_dir->fullname + length - 4 == first_slash)
2412 cmpl_dir->fullname[length - 3] = 0;
2413 cmpl_dir->fullname_len = length - 3;
2417 if(stat(cmpl_dir->fullname, &sbuf) < 0)
2423 cmpl_dir->fullname[length - 4] = 0;
2425 if(!correct_parent(cmpl_dir, &sbuf))
2429 cmpl_dir->fullname_len = strlen(cmpl_dir->fullname);
2435 correct_parent(CompletionDir* cmpl_dir, struct stat *sbuf)
2443 last_slash = strrchr(cmpl_dir->fullname, G_DIR_SEPARATOR);
2444 g_assert(last_slash);
2445 first_slash = strchr(cmpl_dir->fullname, G_DIR_SEPARATOR);
2447 /* Clever (?) way to check for top-level directory that works also on
2448 * Win32, where there is a drive letter and colon prefixed...
2450 if (last_slash != first_slash)
2460 if (stat(cmpl_dir->fullname, &parbuf) < 0)
2464 last_slash[0] = G_DIR_SEPARATOR;
2468 #ifndef G_OS_WIN32 /* No inode numbers on Win32 */
2469 if (parbuf.st_ino == sbuf->st_ino && parbuf.st_dev == sbuf->st_dev)
2470 /* it wasn't a link */
2476 last_slash[0] = G_DIR_SEPARATOR;
2478 /* it was a link, have to figure it out the hard way */
2480 new_name = find_parent_dir_fullname(cmpl_dir->fullname);
2485 g_free(cmpl_dir->fullname);
2487 cmpl_dir->fullname = new_name;
2496 find_parent_dir_fullname(gchar* dirname)
2501 orig_dir = g_get_current_dir ();
2503 if(chdir(dirname) != 0 || chdir("..") != 0)
2509 result = g_get_current_dir ();
2511 if(chdir(orig_dir) != 0)
2523 /**********************************************************************/
2524 /* Completion Operations */
2525 /**********************************************************************/
2529 static PossibleCompletion*
2530 attempt_homedir_completion(gchar* text_to_complete,
2531 CompletionState *cmpl_state)
2535 if (!cmpl_state->user_dir_name_buffer &&
2536 !get_pwdb(cmpl_state))
2538 length = strlen(text_to_complete) - 1;
2540 cmpl_state->user_completion_index += 1;
2542 while(cmpl_state->user_completion_index < cmpl_state->user_directories_len)
2544 index = first_diff_index(text_to_complete + 1,
2545 cmpl_state->user_directories
2546 [cmpl_state->user_completion_index].login);
2553 if(cmpl_state->last_valid_char < (index + 1))
2554 cmpl_state->last_valid_char = index + 1;
2555 cmpl_state->user_completion_index += 1;
2559 cmpl_state->the_completion.is_a_completion = 1;
2560 cmpl_state->the_completion.is_directory = TRUE;
2562 append_completion_text("~", cmpl_state);
2564 append_completion_text(cmpl_state->
2565 user_directories[cmpl_state->user_completion_index].login,
2568 return append_completion_text(G_DIR_SEPARATOR_S, cmpl_state);
2571 if(text_to_complete[1] ||
2572 cmpl_state->user_completion_index > cmpl_state->user_directories_len)
2574 cmpl_state->user_completion_index = -1;
2579 cmpl_state->user_completion_index += 1;
2580 cmpl_state->the_completion.is_a_completion = 1;
2581 cmpl_state->the_completion.is_directory = TRUE;
2583 return append_completion_text("~" G_DIR_SEPARATOR_S, cmpl_state);
2589 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
2590 #define FOLD(c) (tolower(c))
2595 /* returns the index (>= 0) of the first differing character,
2596 * PATTERN_MATCH if the completion matches */
2598 first_diff_index(gchar* pat, gchar* text)
2602 while(*pat && *text && FOLD(*text) == FOLD(*pat))
2612 return PATTERN_MATCH;
2615 static PossibleCompletion*
2616 append_completion_text(gchar* text, CompletionState* cmpl_state)
2620 if(!cmpl_state->the_completion.text)
2623 len = strlen(text) + strlen(cmpl_state->the_completion.text) + 1;
2625 if(cmpl_state->the_completion.text_alloc > len)
2627 strcat(cmpl_state->the_completion.text, text);
2628 return &cmpl_state->the_completion;
2631 while(i < len) { i <<= 1; }
2633 cmpl_state->the_completion.text_alloc = i;
2635 cmpl_state->the_completion.text = (gchar*)g_realloc(cmpl_state->the_completion.text, i);
2637 if(!cmpl_state->the_completion.text)
2641 strcat(cmpl_state->the_completion.text, text);
2642 return &cmpl_state->the_completion;
2646 static CompletionDir*
2647 find_completion_dir(gchar* text_to_complete,
2648 gchar** remaining_text,
2649 CompletionState* cmpl_state)
2651 gchar* first_slash = strchr(text_to_complete, G_DIR_SEPARATOR);
2652 CompletionDir* dir = cmpl_state->reference_dir;
2653 CompletionDir* next;
2654 *remaining_text = text_to_complete;
2658 gint len = first_slash - *remaining_text;
2660 gchar *found_name = NULL; /* Quiet gcc */
2662 gchar* pat_buf = g_new (gchar, len + 1);
2664 strncpy(pat_buf, *remaining_text, len);
2667 for(i = 0; i < dir->sent->entry_count; i += 1)
2669 if(dir->sent->entries[i].is_dir &&
2670 fnmatch(pat_buf, dir->sent->entries[i].entry_name,
2671 FNMATCH_FLAGS)!= FNM_NOMATCH)
2681 found_name = dir->sent->entries[i].entry_name;
2688 /* Perhaps we are trying to open an automount directory */
2689 found_name = pat_buf;
2692 next = open_relative_dir(found_name, dir, cmpl_state);
2700 next->cmpl_parent = dir;
2704 if(!correct_dir_fullname(dir))
2710 *remaining_text = first_slash + 1;
2711 first_slash = strchr(*remaining_text, G_DIR_SEPARATOR);
2720 update_cmpl(PossibleCompletion* poss, CompletionState* cmpl_state)
2724 if(!poss || !cmpl_is_a_completion(poss))
2727 cmpl_len = strlen(cmpl_this_completion(poss));
2729 if(cmpl_state->updated_text_alloc < cmpl_len + 1)
2731 cmpl_state->updated_text =
2732 (gchar*)g_realloc(cmpl_state->updated_text,
2733 cmpl_state->updated_text_alloc);
2734 cmpl_state->updated_text_alloc = 2*cmpl_len;
2737 if(cmpl_state->updated_text_len < 0)
2739 strcpy(cmpl_state->updated_text, cmpl_this_completion(poss));
2740 cmpl_state->updated_text_len = cmpl_len;
2741 cmpl_state->re_complete = cmpl_is_directory(poss);
2743 else if(cmpl_state->updated_text_len == 0)
2745 cmpl_state->re_complete = FALSE;
2750 first_diff_index(cmpl_state->updated_text,
2751 cmpl_this_completion(poss));
2753 cmpl_state->re_complete = FALSE;
2755 if(first_diff == PATTERN_MATCH)
2758 if(first_diff > cmpl_state->updated_text_len)
2759 strcpy(cmpl_state->updated_text, cmpl_this_completion(poss));
2761 cmpl_state->updated_text_len = first_diff;
2762 cmpl_state->updated_text[first_diff] = 0;
2766 static PossibleCompletion*
2767 attempt_file_completion(CompletionState *cmpl_state)
2769 gchar *pat_buf, *first_slash;
2770 CompletionDir *dir = cmpl_state->active_completion_dir;
2772 dir->cmpl_index += 1;
2774 if(dir->cmpl_index == dir->sent->entry_count)
2776 if(dir->cmpl_parent == NULL)
2778 cmpl_state->active_completion_dir = NULL;
2784 cmpl_state->active_completion_dir = dir->cmpl_parent;
2786 return attempt_file_completion(cmpl_state);
2790 g_assert(dir->cmpl_text);
2792 first_slash = strchr(dir->cmpl_text, G_DIR_SEPARATOR);
2796 gint len = first_slash - dir->cmpl_text;
2798 pat_buf = g_new (gchar, len + 1);
2799 strncpy(pat_buf, dir->cmpl_text, len);
2804 gint len = strlen(dir->cmpl_text);
2806 pat_buf = g_new (gchar, len + 2);
2807 strcpy(pat_buf, dir->cmpl_text);
2808 /* Don't append a * if the user entered one herself.
2809 * This way one can complete *.h and don't get matches
2810 * on any .help files, for instance.
2812 if (strchr(pat_buf, '*') == NULL)
2813 strcpy(pat_buf + len, "*");
2818 if(dir->sent->entries[dir->cmpl_index].is_dir)
2820 if(fnmatch(pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
2821 FNMATCH_FLAGS) != FNM_NOMATCH)
2823 CompletionDir* new_dir;
2825 new_dir = open_relative_dir(dir->sent->entries[dir->cmpl_index].entry_name,
2834 new_dir->cmpl_parent = dir;
2836 new_dir->cmpl_index = -1;
2837 new_dir->cmpl_text = first_slash + 1;
2839 cmpl_state->active_completion_dir = new_dir;
2842 return attempt_file_completion(cmpl_state);
2847 return attempt_file_completion(cmpl_state);
2853 return attempt_file_completion(cmpl_state);
2858 if(dir->cmpl_parent != NULL)
2860 append_completion_text(dir->fullname +
2861 strlen(cmpl_state->completion_dir->fullname) + 1,
2863 append_completion_text(G_DIR_SEPARATOR_S, cmpl_state);
2866 append_completion_text(dir->sent->entries[dir->cmpl_index].entry_name, cmpl_state);
2868 cmpl_state->the_completion.is_a_completion =
2869 (fnmatch(pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
2870 FNMATCH_FLAGS) != FNM_NOMATCH);
2872 cmpl_state->the_completion.is_directory = dir->sent->entries[dir->cmpl_index].is_dir;
2873 if(dir->sent->entries[dir->cmpl_index].is_dir)
2874 append_completion_text(G_DIR_SEPARATOR_S, cmpl_state);
2877 return &cmpl_state->the_completion;
2884 get_pwdb(CompletionState* cmpl_state)
2886 struct passwd *pwd_ptr;
2888 gint len = 0, i, count = 0;
2890 if(cmpl_state->user_dir_name_buffer)
2894 while ((pwd_ptr = getpwent()) != NULL)
2896 len += strlen(pwd_ptr->pw_name);
2897 len += strlen(pwd_ptr->pw_dir);
2904 cmpl_state->user_dir_name_buffer = g_new(gchar, len);
2905 cmpl_state->user_directories = g_new(CompletionUserDir, count);
2906 cmpl_state->user_directories_len = count;
2908 buf_ptr = cmpl_state->user_dir_name_buffer;
2910 for(i = 0; i < count; i += 1)
2912 pwd_ptr = getpwent();
2919 strcpy(buf_ptr, pwd_ptr->pw_name);
2920 cmpl_state->user_directories[i].login = buf_ptr;
2921 buf_ptr += strlen(buf_ptr);
2923 strcpy(buf_ptr, pwd_ptr->pw_dir);
2924 cmpl_state->user_directories[i].homedir = buf_ptr;
2925 buf_ptr += strlen(buf_ptr);
2929 qsort(cmpl_state->user_directories,
2930 cmpl_state->user_directories_len,
2931 sizeof(CompletionUserDir),
2940 if(cmpl_state->user_dir_name_buffer)
2941 g_free(cmpl_state->user_dir_name_buffer);
2942 if(cmpl_state->user_directories)
2943 g_free(cmpl_state->user_directories);
2945 cmpl_state->user_dir_name_buffer = NULL;
2946 cmpl_state->user_directories = NULL;
2952 compare_user_dir(const void* a, const void* b)
2954 return strcmp((((CompletionUserDir*)a))->login,
2955 (((CompletionUserDir*)b))->login);
2961 compare_cmpl_dir(const void* a, const void* b)
2963 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
2964 return strcmp((((CompletionDirEntry*)a))->entry_name,
2965 (((CompletionDirEntry*)b))->entry_name);
2967 return g_strcasecmp((((CompletionDirEntry*)a))->entry_name,
2968 (((CompletionDirEntry*)b))->entry_name);
2973 cmpl_state_okay(CompletionState* cmpl_state)
2975 return cmpl_state && cmpl_state->reference_dir;
2979 cmpl_strerror(gint err)
2981 if(err == CMPL_ERRNO_TOO_LONG)
2982 return "Name too long";
2984 return g_strerror (err);