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/.
29 #include <glib.h> /* To get stat->_stat redefinition
34 #include <sys/types.h>
36 #ifdef HAVE_SYS_PARAM_H
37 #include <sys/param.h>
54 #include "gdk/gdkkeysyms.h"
55 #include "gtkbutton.h"
57 #include "gtkfilesel.h"
62 #include "gtklistitem.h"
64 #include "gtkscrolledwindow.h"
65 #include "gtksignal.h"
68 #include "gtkmenuitem.h"
69 #include "gtkoptionmenu.h"
71 #include "gtkdialog.h"
74 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
81 #define mkdir(p,m) _mkdir(p)
83 #define S_ISDIR(mode) ((mode)&_S_IFDIR)
86 #endif /* G_OS_WIN32 */
88 #endif /* G_OS_WIN32 || G_WITH_CYGWIN */
90 #define DIR_LIST_WIDTH 180
91 #define DIR_LIST_HEIGHT 180
92 #define FILE_LIST_WIDTH 180
93 #define FILE_LIST_HEIGHT 180
95 /* I've put this here so it doesn't get confused with the
96 * file completion interface */
97 typedef struct _HistoryCallbackArg HistoryCallbackArg;
99 struct _HistoryCallbackArg
102 GtkWidget *menu_item;
106 typedef struct _CompletionState CompletionState;
107 typedef struct _CompletionDir CompletionDir;
108 typedef struct _CompletionDirSent CompletionDirSent;
109 typedef struct _CompletionDirEntry CompletionDirEntry;
110 typedef struct _CompletionUserDir CompletionUserDir;
111 typedef struct _PossibleCompletion PossibleCompletion;
113 /* Non-external file completion decls and structures */
115 /* A contant telling PRCS how many directories to cache. Its actually
116 * kept in a list, so the geometry isn't important. */
117 #define CMPL_DIRECTORY_CACHE_SIZE 10
119 /* A constant used to determine whether a substring was an exact
120 * match by first_diff_index()
122 #define PATTERN_MATCH -1
123 /* The arguments used by all fnmatch() calls below
125 #define FNMATCH_FLAGS (FNM_PATHNAME | FNM_PERIOD)
127 #define CMPL_ERRNO_TOO_LONG ((1<<16)-1)
129 /* This structure contains all the useful information about a directory
130 * for the purposes of filename completion. These structures are cached
131 * in the CompletionState struct. CompletionDir's are reference counted.
133 struct _CompletionDirSent
140 gchar *name_buffer; /* memory segment containing names of all entries */
142 struct _CompletionDirEntry *entries;
145 struct _CompletionDir
147 CompletionDirSent *sent;
152 struct _CompletionDir *cmpl_parent;
157 /* This structure contains pairs of directory entry names with a flag saying
158 * whether or not they are a valid directory. NOTE: This information is used
159 * to provide the caller with information about whether to update its completions
160 * or try to open a file. Since directories are cached by the directory mtime,
161 * a symlink which points to an invalid file (which will not be a directory),
162 * will not be reevaluated if that file is created, unless the containing
163 * directory is touched. I consider this case to be worth ignoring (josh).
165 struct _CompletionDirEntry
171 struct _CompletionUserDir
177 struct _PossibleCompletion
179 /* accessible fields, all are accessed externally by functions
183 gint is_a_completion;
191 struct _CompletionState
193 gint last_valid_char;
195 gint updated_text_len;
196 gint updated_text_alloc;
199 gchar *user_dir_name_buffer;
200 gint user_directories_len;
202 gchar *last_completion_text;
204 gint user_completion_index; /* if >= 0, currently completing ~user */
206 struct _CompletionDir *completion_dir; /* directory completing from */
207 struct _CompletionDir *active_completion_dir;
209 struct _PossibleCompletion the_completion;
211 struct _CompletionDir *reference_dir; /* initial directory */
213 GList* directory_storage;
214 GList* directory_sent_storage;
216 struct _CompletionUserDir *user_directories;
220 /* File completion functions which would be external, were they used
221 * outside of this file.
224 static CompletionState* cmpl_init_state (void);
225 static void cmpl_free_state (CompletionState *cmpl_state);
226 static gint cmpl_state_okay (CompletionState* cmpl_state);
227 static gchar* cmpl_strerror (gint);
229 static PossibleCompletion* cmpl_completion_matches(gchar *text_to_complete,
230 gchar **remaining_text,
231 CompletionState *cmpl_state);
233 /* Returns a name for consideration, possibly a completion, this name
234 * will be invalid after the next call to cmpl_next_completion.
236 static char* cmpl_this_completion (PossibleCompletion*);
238 /* True if this completion matches the given text. Otherwise, this
239 * output can be used to have a list of non-completions.
241 static gint cmpl_is_a_completion (PossibleCompletion*);
243 /* True if the completion is a directory
245 static gint cmpl_is_directory (PossibleCompletion*);
247 /* Obtains the next completion, or NULL
249 static PossibleCompletion* cmpl_next_completion (CompletionState*);
251 /* Updating completions: the return value of cmpl_updated_text() will
252 * be text_to_complete completed as much as possible after the most
253 * recent call to cmpl_completion_matches. For the present
254 * application, this is the suggested replacement for the user's input
255 * string. You must CALL THIS AFTER ALL cmpl_text_completions have
258 static gchar* cmpl_updated_text (CompletionState* cmpl_state);
260 /* After updating, to see if the completion was a directory, call
261 * this. If it was, you should consider re-calling completion_matches.
263 static gint cmpl_updated_dir (CompletionState* cmpl_state);
265 /* Current location: if using file completion, return the current
266 * directory, from which file completion begins. More specifically,
267 * the cwd concatenated with all exact completions up to the last
268 * directory delimiter('/').
270 static gchar* cmpl_reference_position (CompletionState* cmpl_state);
272 /* backing up: if cmpl_completion_matches returns NULL, you may query
273 * the index of the last completable character into cmpl_updated_text.
275 static gint cmpl_last_valid_char (CompletionState* cmpl_state);
277 /* When the user selects a non-directory, call cmpl_completion_fullname
278 * to get the full name of the selected file.
280 static gchar* cmpl_completion_fullname (gchar*, CompletionState* cmpl_state);
283 /* Directory operations. */
284 static CompletionDir* open_ref_dir (gchar* text_to_complete,
285 gchar** remaining_text,
286 CompletionState* cmpl_state);
287 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
288 static gboolean check_dir (gchar *dir_name,
290 gboolean *stat_subdirs);
292 static CompletionDir* open_dir (gchar* dir_name,
293 CompletionState* cmpl_state);
295 static CompletionDir* open_user_dir (gchar* text_to_complete,
296 CompletionState *cmpl_state);
298 static CompletionDir* open_relative_dir (gchar* dir_name, CompletionDir* dir,
299 CompletionState *cmpl_state);
300 static CompletionDirSent* open_new_dir (gchar* dir_name,
302 gboolean stat_subdirs);
303 static gint correct_dir_fullname (CompletionDir* cmpl_dir);
304 static gint correct_parent (CompletionDir* cmpl_dir,
306 static gchar* find_parent_dir_fullname (gchar* dirname);
307 static CompletionDir* attach_dir (CompletionDirSent* sent,
309 CompletionState *cmpl_state);
310 static void free_dir_sent (CompletionDirSent* sent);
311 static void free_dir (CompletionDir *dir);
312 static void prune_memory_usage(CompletionState *cmpl_state);
314 /* Completion operations */
315 static PossibleCompletion* attempt_homedir_completion(gchar* text_to_complete,
316 CompletionState *cmpl_state);
317 static PossibleCompletion* attempt_file_completion(CompletionState *cmpl_state);
318 static CompletionDir* find_completion_dir(gchar* text_to_complete,
319 gchar** remaining_text,
320 CompletionState* cmpl_state);
321 static PossibleCompletion* append_completion_text(gchar* text,
322 CompletionState* cmpl_state);
324 static gint get_pwdb(CompletionState* cmpl_state);
325 static gint compare_user_dir(const void* a, const void* b);
327 static gint first_diff_index(gchar* pat, gchar* text);
328 static gint compare_cmpl_dir(const void* a, const void* b);
329 static void update_cmpl(PossibleCompletion* poss,
330 CompletionState* cmpl_state);
332 static void gtk_file_selection_class_init (GtkFileSelectionClass *klass);
333 static void gtk_file_selection_init (GtkFileSelection *filesel);
334 static void gtk_file_selection_destroy (GtkObject *object);
335 static gint gtk_file_selection_key_press (GtkWidget *widget,
339 static void gtk_file_selection_file_button (GtkWidget *widget,
342 GdkEventButton *bevent,
345 static void gtk_file_selection_dir_button (GtkWidget *widget,
348 GdkEventButton *bevent,
351 static void gtk_file_selection_populate (GtkFileSelection *fs,
354 static void gtk_file_selection_abort (GtkFileSelection *fs);
356 static void gtk_file_selection_update_history_menu (GtkFileSelection *fs,
359 static void gtk_file_selection_create_dir (GtkWidget *widget, gpointer data);
360 static void gtk_file_selection_delete_file (GtkWidget *widget, gpointer data);
361 static void gtk_file_selection_rename_file (GtkWidget *widget, gpointer data);
365 static GtkWindowClass *parent_class = NULL;
367 /* Saves errno when something cmpl does fails. */
368 static gint cmpl_errno;
372 * Take the path currently in the file selection
373 * entry field and translate as necessary from
374 * a WIN32 style to CYGWIN32 style path. For
375 * instance translate:
376 * x:\somepath\file.jpg
378 * //x/somepath/file.jpg
380 * Replace the path in the selection text field.
381 * Return a boolean value concerning whether a
382 * translation had to be made.
385 translate_win32_path(GtkFileSelection *filesel)
391 * Retrieve the current path
393 path = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
396 * Translate only if this looks like a DOS-ish
397 * path... First handle any drive letters.
399 if (isalpha(path[0]) && (path[1] == ':')) {
401 * This part kind of stinks... It isn't possible
402 * to know if there is enough space in the current
403 * string for the extra character required in this
404 * conversion. Assume that there isn't enough space
405 * and use the set function on the text field to
406 * set the newly created string.
410 newPath = g_malloc(strlen(path) + 2);
411 sprintf(newPath, "//%c/%s", path[0], (path + 3));
412 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), newPath);
419 * Now, replace backslashes with forward slashes
422 if (strchr(path, '\\')) {
424 for (index = 0; path[index] != '\0'; index++)
425 if (path[index] == '\\')
436 gtk_file_selection_get_type (void)
438 static GtkType file_selection_type = 0;
440 if (!file_selection_type)
442 static const GtkTypeInfo filesel_info =
445 sizeof (GtkFileSelection),
446 sizeof (GtkFileSelectionClass),
447 (GtkClassInitFunc) gtk_file_selection_class_init,
448 (GtkObjectInitFunc) gtk_file_selection_init,
449 /* reserved_1 */ NULL,
450 /* reserved_2 */ NULL,
451 (GtkClassInitFunc) NULL,
454 file_selection_type = gtk_type_unique (GTK_TYPE_WINDOW, &filesel_info);
457 return file_selection_type;
461 gtk_file_selection_class_init (GtkFileSelectionClass *class)
463 GtkObjectClass *object_class;
465 object_class = (GtkObjectClass*) class;
467 parent_class = gtk_type_class (GTK_TYPE_WINDOW);
469 object_class->destroy = gtk_file_selection_destroy;
473 gtk_file_selection_init (GtkFileSelection *filesel)
475 GtkWidget *entry_vbox;
477 GtkWidget *list_hbox;
478 GtkWidget *confirm_area;
479 GtkWidget *pulldown_hbox;
480 GtkWidget *scrolled_win;
483 char *file_title [2];
485 filesel->cmpl_state = cmpl_init_state ();
487 /* The dialog-sized vertical box */
488 filesel->main_vbox = gtk_vbox_new (FALSE, 10);
489 gtk_container_set_border_width (GTK_CONTAINER (filesel), 10);
490 gtk_container_add (GTK_CONTAINER (filesel), filesel->main_vbox);
491 gtk_widget_show (filesel->main_vbox);
493 /* The horizontal box containing create, rename etc. buttons */
494 filesel->button_area = gtk_hbutton_box_new ();
495 gtk_button_box_set_layout(GTK_BUTTON_BOX(filesel->button_area), GTK_BUTTONBOX_START);
496 gtk_button_box_set_spacing(GTK_BUTTON_BOX(filesel->button_area), 0);
497 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->button_area,
499 gtk_widget_show (filesel->button_area);
501 gtk_file_selection_show_fileop_buttons(filesel);
503 /* hbox for pulldown menu */
504 pulldown_hbox = gtk_hbox_new (TRUE, 5);
505 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), pulldown_hbox, FALSE, FALSE, 0);
506 gtk_widget_show (pulldown_hbox);
509 filesel->history_pulldown = gtk_option_menu_new ();
510 gtk_widget_show (filesel->history_pulldown);
511 gtk_box_pack_start (GTK_BOX (pulldown_hbox), filesel->history_pulldown,
514 /* The horizontal box containing the directory and file listboxes */
515 list_hbox = gtk_hbox_new (FALSE, 5);
516 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), list_hbox, TRUE, TRUE, 0);
517 gtk_widget_show (list_hbox);
519 /* The directories clist */
520 dir_title[0] = _("Directories");
522 filesel->dir_list = gtk_clist_new_with_titles (1, (gchar**) dir_title);
523 gtk_widget_set_usize (filesel->dir_list, DIR_LIST_WIDTH, DIR_LIST_HEIGHT);
524 gtk_signal_connect (GTK_OBJECT (filesel->dir_list), "select_row",
525 (GtkSignalFunc) gtk_file_selection_dir_button,
527 gtk_clist_column_titles_passive (GTK_CLIST (filesel->dir_list));
529 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
530 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->dir_list);
531 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
532 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
533 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 5);
534 gtk_box_pack_start (GTK_BOX (list_hbox), scrolled_win, TRUE, TRUE, 0);
535 gtk_widget_show (filesel->dir_list);
536 gtk_widget_show (scrolled_win);
538 /* The files clist */
539 file_title[0] = _("Files");
540 file_title[1] = NULL;
541 filesel->file_list = gtk_clist_new_with_titles (1, (gchar**) file_title);
542 gtk_widget_set_usize (filesel->file_list, FILE_LIST_WIDTH, FILE_LIST_HEIGHT);
543 gtk_signal_connect (GTK_OBJECT (filesel->file_list), "select_row",
544 (GtkSignalFunc) gtk_file_selection_file_button,
546 gtk_clist_column_titles_passive (GTK_CLIST (filesel->file_list));
548 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
549 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->file_list);
550 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
551 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
552 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 5);
553 gtk_box_pack_start (GTK_BOX (list_hbox), scrolled_win, TRUE, TRUE, 0);
554 gtk_widget_show (filesel->file_list);
555 gtk_widget_show (scrolled_win);
557 /* action area for packing buttons into. */
558 filesel->action_area = gtk_hbox_new (TRUE, 0);
559 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->action_area,
561 gtk_widget_show (filesel->action_area);
563 /* The OK/Cancel button area */
564 confirm_area = gtk_hbutton_box_new ();
565 gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area), GTK_BUTTONBOX_END);
566 gtk_button_box_set_spacing(GTK_BUTTON_BOX(confirm_area), 5);
567 gtk_box_pack_end (GTK_BOX (filesel->main_vbox), confirm_area, FALSE, FALSE, 0);
568 gtk_widget_show (confirm_area);
571 filesel->ok_button = gtk_button_new_with_label (_("OK"));
572 GTK_WIDGET_SET_FLAGS (filesel->ok_button, GTK_CAN_DEFAULT);
573 gtk_box_pack_start (GTK_BOX (confirm_area), filesel->ok_button, TRUE, TRUE, 0);
574 gtk_widget_grab_default (filesel->ok_button);
575 gtk_widget_show (filesel->ok_button);
577 /* The Cancel button */
578 filesel->cancel_button = gtk_button_new_with_label (_("Cancel"));
579 GTK_WIDGET_SET_FLAGS (filesel->cancel_button, GTK_CAN_DEFAULT);
580 gtk_box_pack_start (GTK_BOX (confirm_area), filesel->cancel_button, TRUE, TRUE, 0);
581 gtk_widget_show (filesel->cancel_button);
583 /* The selection entry widget */
584 entry_vbox = gtk_vbox_new (FALSE, 2);
585 gtk_box_pack_end (GTK_BOX (filesel->main_vbox), entry_vbox, FALSE, FALSE, 0);
586 gtk_widget_show (entry_vbox);
588 filesel->selection_text = label = gtk_label_new ("");
589 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
590 gtk_box_pack_start (GTK_BOX (entry_vbox), label, FALSE, FALSE, 0);
591 gtk_widget_show (label);
593 filesel->selection_entry = gtk_entry_new ();
594 gtk_signal_connect (GTK_OBJECT (filesel->selection_entry), "key_press_event",
595 (GtkSignalFunc) gtk_file_selection_key_press, filesel);
596 gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "focus_in_event",
597 (GtkSignalFunc) gtk_widget_grab_default,
598 GTK_OBJECT (filesel->ok_button));
599 gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "activate",
600 (GtkSignalFunc) gtk_button_clicked,
601 GTK_OBJECT (filesel->ok_button));
602 gtk_box_pack_start (GTK_BOX (entry_vbox), filesel->selection_entry, TRUE, TRUE, 0);
603 gtk_widget_show (filesel->selection_entry);
605 if (!cmpl_state_okay (filesel->cmpl_state))
609 sprintf (err_buf, _("Directory unreadable: %s"), cmpl_strerror (cmpl_errno));
611 gtk_label_set_text (GTK_LABEL (filesel->selection_text), err_buf);
615 gtk_file_selection_populate (filesel, "", FALSE);
618 gtk_widget_grab_focus (filesel->selection_entry);
622 gtk_file_selection_new (const gchar *title)
624 GtkFileSelection *filesel;
626 filesel = gtk_type_new (GTK_TYPE_FILE_SELECTION);
627 gtk_window_set_title (GTK_WINDOW (filesel), title);
629 return GTK_WIDGET (filesel);
633 gtk_file_selection_show_fileop_buttons (GtkFileSelection *filesel)
635 g_return_if_fail (filesel != NULL);
636 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
638 /* delete, create directory, and rename */
639 if (!filesel->fileop_c_dir)
641 filesel->fileop_c_dir = gtk_button_new_with_label (_("Create Dir"));
642 gtk_signal_connect (GTK_OBJECT (filesel->fileop_c_dir), "clicked",
643 (GtkSignalFunc) gtk_file_selection_create_dir,
645 gtk_box_pack_start (GTK_BOX (filesel->button_area),
646 filesel->fileop_c_dir, TRUE, TRUE, 0);
647 gtk_widget_show (filesel->fileop_c_dir);
650 if (!filesel->fileop_del_file)
652 filesel->fileop_del_file = gtk_button_new_with_label (_("Delete File"));
653 gtk_signal_connect (GTK_OBJECT (filesel->fileop_del_file), "clicked",
654 (GtkSignalFunc) gtk_file_selection_delete_file,
656 gtk_box_pack_start (GTK_BOX (filesel->button_area),
657 filesel->fileop_del_file, TRUE, TRUE, 0);
658 gtk_widget_show (filesel->fileop_del_file);
661 if (!filesel->fileop_ren_file)
663 filesel->fileop_ren_file = gtk_button_new_with_label (_("Rename File"));
664 gtk_signal_connect (GTK_OBJECT (filesel->fileop_ren_file), "clicked",
665 (GtkSignalFunc) gtk_file_selection_rename_file,
667 gtk_box_pack_start (GTK_BOX (filesel->button_area),
668 filesel->fileop_ren_file, TRUE, TRUE, 0);
669 gtk_widget_show (filesel->fileop_ren_file);
672 gtk_widget_queue_resize(GTK_WIDGET(filesel));
676 gtk_file_selection_hide_fileop_buttons (GtkFileSelection *filesel)
678 g_return_if_fail (filesel != NULL);
679 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
681 if (filesel->fileop_ren_file)
683 gtk_widget_destroy (filesel->fileop_ren_file);
684 filesel->fileop_ren_file = NULL;
687 if (filesel->fileop_del_file)
689 gtk_widget_destroy (filesel->fileop_del_file);
690 filesel->fileop_del_file = NULL;
693 if (filesel->fileop_c_dir)
695 gtk_widget_destroy (filesel->fileop_c_dir);
696 filesel->fileop_c_dir = NULL;
703 gtk_file_selection_set_filename (GtkFileSelection *filesel,
704 const gchar *filename)
706 char buf[MAXPATHLEN];
707 const char *name, *last_slash;
709 g_return_if_fail (filesel != NULL);
710 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
711 g_return_if_fail (filename != NULL);
713 last_slash = strrchr (filename, G_DIR_SEPARATOR);
722 gint len = MIN (MAXPATHLEN - 1, last_slash - filename + 1);
724 strncpy (buf, filename, len);
727 name = last_slash + 1;
730 gtk_file_selection_populate (filesel, buf, FALSE);
732 if (filesel->selection_entry)
733 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), name);
737 gtk_file_selection_get_filename (GtkFileSelection *filesel)
739 static char nothing[2] = "";
743 g_return_val_if_fail (filesel != NULL, nothing);
744 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), nothing);
747 translate_win32_path(filesel);
749 text = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
752 filename = cmpl_completion_fullname (text, filesel->cmpl_state);
760 gtk_file_selection_complete (GtkFileSelection *filesel,
761 const gchar *pattern)
763 g_return_if_fail (filesel != NULL);
764 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
765 g_return_if_fail (pattern != NULL);
767 if (filesel->selection_entry)
768 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), pattern);
769 gtk_file_selection_populate (filesel, (gchar*) pattern, TRUE);
773 gtk_file_selection_destroy (GtkObject *object)
775 GtkFileSelection *filesel;
777 HistoryCallbackArg *callback_arg;
779 g_return_if_fail (object != NULL);
780 g_return_if_fail (GTK_IS_FILE_SELECTION (object));
782 filesel = GTK_FILE_SELECTION (object);
784 if (filesel->fileop_dialog)
785 gtk_widget_destroy (filesel->fileop_dialog);
787 if (filesel->history_list)
789 list = filesel->history_list;
792 callback_arg = list->data;
793 g_free (callback_arg->directory);
794 g_free (callback_arg);
797 g_list_free (filesel->history_list);
798 filesel->history_list = NULL;
801 cmpl_free_state (filesel->cmpl_state);
802 filesel->cmpl_state = NULL;
804 if (GTK_OBJECT_CLASS (parent_class)->destroy)
805 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
808 /* Begin file operations callbacks */
811 gtk_file_selection_fileop_error (GtkFileSelection *fs, gchar *error_message)
818 g_return_if_fail (error_message != NULL);
821 dialog = gtk_dialog_new ();
823 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
824 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
827 gtk_window_set_title (GTK_WINDOW (dialog), _("Error"));
828 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
830 /* If file dialog is grabbed, make this dialog modal too */
831 /* When error dialog is closed, file dialog will be grabbed again */
832 if (GTK_WINDOW(fs)->modal)
833 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
835 vbox = gtk_vbox_new(FALSE, 0);
836 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
837 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
839 gtk_widget_show(vbox);
841 label = gtk_label_new(error_message);
842 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
843 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
844 gtk_widget_show(label);
846 /* yes, we free it */
847 g_free (error_message);
850 button = gtk_button_new_with_label (_("Close"));
851 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
852 (GtkSignalFunc) gtk_widget_destroy,
854 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
855 button, TRUE, TRUE, 0);
856 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
857 gtk_widget_grab_default(button);
858 gtk_widget_show (button);
860 gtk_widget_show (dialog);
864 gtk_file_selection_fileop_destroy (GtkWidget *widget, gpointer data)
866 GtkFileSelection *fs = data;
868 g_return_if_fail (fs != NULL);
869 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
871 fs->fileop_dialog = NULL;
876 gtk_file_selection_create_dir_confirmed (GtkWidget *widget, gpointer data)
878 GtkFileSelection *fs = data;
883 CompletionState *cmpl_state;
885 g_return_if_fail (fs != NULL);
886 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
888 dirname = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
889 cmpl_state = (CompletionState*) fs->cmpl_state;
890 path = cmpl_reference_position (cmpl_state);
892 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, dirname, NULL);
893 if ( (mkdir (full_path, 0755) < 0) )
895 buf = g_strconcat ("Error creating directory \"", dirname, "\": ",
896 g_strerror(errno), NULL);
897 gtk_file_selection_fileop_error (fs, buf);
901 gtk_widget_destroy (fs->fileop_dialog);
902 gtk_file_selection_populate (fs, "", FALSE);
906 gtk_file_selection_create_dir (GtkWidget *widget, gpointer data)
908 GtkFileSelection *fs = data;
914 g_return_if_fail (fs != NULL);
915 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
917 if (fs->fileop_dialog)
921 fs->fileop_dialog = dialog = gtk_dialog_new ();
922 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
923 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
925 gtk_window_set_title (GTK_WINDOW (dialog), _("Create Directory"));
926 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
928 /* If file dialog is grabbed, grab option dialog */
929 /* When option dialog is closed, file dialog will be grabbed again */
930 if (GTK_WINDOW(fs)->modal)
931 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
933 vbox = gtk_vbox_new(FALSE, 0);
934 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
935 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
937 gtk_widget_show(vbox);
939 label = gtk_label_new(_("Directory name:"));
940 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
941 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
942 gtk_widget_show(label);
944 /* The directory entry widget */
945 fs->fileop_entry = gtk_entry_new ();
946 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
948 GTK_WIDGET_SET_FLAGS(fs->fileop_entry, GTK_CAN_DEFAULT);
949 gtk_widget_show (fs->fileop_entry);
952 button = gtk_button_new_with_label (_("Create"));
953 gtk_signal_connect (GTK_OBJECT (button), "clicked",
954 (GtkSignalFunc) gtk_file_selection_create_dir_confirmed,
956 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
957 button, TRUE, TRUE, 0);
958 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
959 gtk_widget_show(button);
961 button = gtk_button_new_with_label (_("Cancel"));
962 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
963 (GtkSignalFunc) gtk_widget_destroy,
965 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
966 button, TRUE, TRUE, 0);
967 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
968 gtk_widget_grab_default(button);
969 gtk_widget_show (button);
971 gtk_widget_show (dialog);
975 gtk_file_selection_delete_file_confirmed (GtkWidget *widget, gpointer data)
977 GtkFileSelection *fs = data;
978 CompletionState *cmpl_state;
983 g_return_if_fail (fs != NULL);
984 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
986 cmpl_state = (CompletionState*) fs->cmpl_state;
987 path = cmpl_reference_position (cmpl_state);
989 full_path = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
990 if ( (unlink (full_path) < 0) )
992 buf = g_strconcat ("Error deleting file \"", fs->fileop_file, "\": ",
993 g_strerror(errno), NULL);
994 gtk_file_selection_fileop_error (fs, buf);
998 gtk_widget_destroy (fs->fileop_dialog);
999 gtk_file_selection_populate (fs, "", FALSE);
1003 gtk_file_selection_delete_file (GtkWidget *widget, gpointer data)
1005 GtkFileSelection *fs = data;
1013 g_return_if_fail (fs != NULL);
1014 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1016 if (fs->fileop_dialog)
1019 #ifdef G_WITH_CYGWIN
1020 translate_win32_path(fs);
1023 filename = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1024 if (strlen(filename) < 1)
1027 fs->fileop_file = filename;
1030 fs->fileop_dialog = dialog = gtk_dialog_new ();
1031 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
1032 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
1034 gtk_window_set_title (GTK_WINDOW (dialog), _("Delete File"));
1035 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1037 /* If file dialog is grabbed, grab option dialog */
1038 /* When option dialog is closed, file dialog will be grabbed again */
1039 if (GTK_WINDOW(fs)->modal)
1040 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
1042 vbox = gtk_vbox_new(FALSE, 0);
1043 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
1044 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
1046 gtk_widget_show(vbox);
1048 buf = g_strconcat ("Really delete file \"", filename, "\" ?", NULL);
1049 label = gtk_label_new(buf);
1050 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
1051 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
1052 gtk_widget_show(label);
1056 button = gtk_button_new_with_label (_("Delete"));
1057 gtk_signal_connect (GTK_OBJECT (button), "clicked",
1058 (GtkSignalFunc) gtk_file_selection_delete_file_confirmed,
1060 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1061 button, TRUE, TRUE, 0);
1062 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1063 gtk_widget_show(button);
1065 button = gtk_button_new_with_label (_("Cancel"));
1066 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1067 (GtkSignalFunc) gtk_widget_destroy,
1069 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1070 button, TRUE, TRUE, 0);
1071 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1072 gtk_widget_grab_default(button);
1073 gtk_widget_show (button);
1075 gtk_widget_show (dialog);
1080 gtk_file_selection_rename_file_confirmed (GtkWidget *widget, gpointer data)
1082 GtkFileSelection *fs = data;
1086 gchar *new_filename;
1087 gchar *old_filename;
1088 CompletionState *cmpl_state;
1090 g_return_if_fail (fs != NULL);
1091 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1093 file = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
1094 cmpl_state = (CompletionState*) fs->cmpl_state;
1095 path = cmpl_reference_position (cmpl_state);
1097 new_filename = g_strconcat (path, G_DIR_SEPARATOR_S, file, NULL);
1098 old_filename = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
1100 if ( (rename (old_filename, new_filename)) < 0)
1102 buf = g_strconcat ("Error renaming file \"", file, "\": ",
1103 g_strerror(errno), NULL);
1104 gtk_file_selection_fileop_error (fs, buf);
1106 g_free (new_filename);
1107 g_free (old_filename);
1109 gtk_widget_destroy (fs->fileop_dialog);
1110 gtk_file_selection_populate (fs, "", FALSE);
1114 gtk_file_selection_rename_file (GtkWidget *widget, gpointer data)
1116 GtkFileSelection *fs = data;
1123 g_return_if_fail (fs != NULL);
1124 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1126 if (fs->fileop_dialog)
1129 fs->fileop_file = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1130 if (strlen(fs->fileop_file) < 1)
1134 fs->fileop_dialog = dialog = gtk_dialog_new ();
1135 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
1136 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
1138 gtk_window_set_title (GTK_WINDOW (dialog), _("Rename File"));
1139 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1141 /* If file dialog is grabbed, grab option dialog */
1142 /* When option dialog closed, file dialog will be grabbed again */
1143 if (GTK_WINDOW(fs)->modal)
1144 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
1146 vbox = gtk_vbox_new(FALSE, 0);
1147 gtk_container_set_border_width (GTK_CONTAINER(vbox), 8);
1148 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
1150 gtk_widget_show(vbox);
1152 buf = g_strconcat ("Rename file \"", fs->fileop_file, "\" to:", NULL);
1153 label = gtk_label_new(buf);
1154 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
1155 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
1156 gtk_widget_show(label);
1159 /* New filename entry */
1160 fs->fileop_entry = gtk_entry_new ();
1161 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1163 GTK_WIDGET_SET_FLAGS(fs->fileop_entry, GTK_CAN_DEFAULT);
1164 gtk_widget_show (fs->fileop_entry);
1166 gtk_entry_set_text (GTK_ENTRY (fs->fileop_entry), fs->fileop_file);
1167 gtk_editable_select_region (GTK_EDITABLE (fs->fileop_entry),
1168 0, strlen (fs->fileop_file));
1171 button = gtk_button_new_with_label (_("Rename"));
1172 gtk_signal_connect (GTK_OBJECT (button), "clicked",
1173 (GtkSignalFunc) gtk_file_selection_rename_file_confirmed,
1175 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1176 button, TRUE, TRUE, 0);
1177 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1178 gtk_widget_show(button);
1180 button = gtk_button_new_with_label (_("Cancel"));
1181 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1182 (GtkSignalFunc) gtk_widget_destroy,
1184 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1185 button, TRUE, TRUE, 0);
1186 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1187 gtk_widget_grab_default(button);
1188 gtk_widget_show (button);
1190 gtk_widget_show (dialog);
1195 gtk_file_selection_key_press (GtkWidget *widget,
1199 GtkFileSelection *fs;
1202 g_return_val_if_fail (widget != NULL, FALSE);
1203 g_return_val_if_fail (event != NULL, FALSE);
1205 if (event->keyval == GDK_Tab)
1207 fs = GTK_FILE_SELECTION (user_data);
1208 #ifdef G_WITH_CYGWIN
1209 translate_win32_path(fs);
1211 text = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1213 text = g_strdup (text);
1215 gtk_file_selection_populate (fs, text, TRUE);
1219 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "key_press_event");
1229 gtk_file_selection_history_callback (GtkWidget *widget, gpointer data)
1231 GtkFileSelection *fs = data;
1232 HistoryCallbackArg *callback_arg;
1235 g_return_if_fail (fs != NULL);
1236 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1238 list = fs->history_list;
1241 callback_arg = list->data;
1243 if (callback_arg->menu_item == widget)
1245 gtk_file_selection_populate (fs, callback_arg->directory, FALSE);
1254 gtk_file_selection_update_history_menu (GtkFileSelection *fs,
1255 gchar *current_directory)
1257 HistoryCallbackArg *callback_arg;
1258 GtkWidget *menu_item;
1264 g_return_if_fail (fs != NULL);
1265 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1266 g_return_if_fail (current_directory != NULL);
1268 list = fs->history_list;
1270 if (fs->history_menu)
1273 callback_arg = list->data;
1274 g_free (callback_arg->directory);
1275 g_free (callback_arg);
1278 g_list_free (fs->history_list);
1279 fs->history_list = NULL;
1281 gtk_widget_destroy (fs->history_menu);
1284 fs->history_menu = gtk_menu_new();
1286 current_dir = g_strdup (current_directory);
1288 dir_len = strlen (current_dir);
1290 for (i = dir_len; i >= 0; i--)
1292 /* the i == dir_len is to catch the full path for the first
1294 if ( (current_dir[i] == G_DIR_SEPARATOR) || (i == dir_len))
1296 /* another small hack to catch the full path */
1298 current_dir[i + 1] = '\0';
1299 #ifdef G_WITH_CYGWIN
1300 if (!strcmp(current_dir, "//"))
1303 menu_item = gtk_menu_item_new_with_label (current_dir);
1305 callback_arg = g_new (HistoryCallbackArg, 1);
1306 callback_arg->menu_item = menu_item;
1308 /* since the autocompletion gets confused if you don't
1309 * supply a trailing '/' on a dir entry, set the full
1310 * (current) path to "" which just refreshes the filesel */
1312 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 cmpl_free_dir_list (cmpl_state->directory_storage);
1824 cmpl_free_dir_sent_list (cmpl_state->directory_sent_storage);
1826 if (cmpl_state->user_dir_name_buffer)
1827 g_free (cmpl_state->user_dir_name_buffer);
1828 if (cmpl_state->user_directories)
1829 g_free (cmpl_state->user_directories);
1830 if (cmpl_state->the_completion.text)
1831 g_free (cmpl_state->the_completion.text);
1832 if (cmpl_state->updated_text)
1833 g_free (cmpl_state->updated_text);
1835 g_free (cmpl_state);
1839 free_dir(CompletionDir* dir)
1841 g_free(dir->fullname);
1846 free_dir_sent(CompletionDirSent* sent)
1848 g_free(sent->name_buffer);
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;
2001 else if (g_path_is_absolute (text_to_complete) || !cmpl_state->reference_dir)
2006 rootlen = g_path_skip_root (text_to_complete) - text_to_complete;
2007 root = g_malloc (rootlen + 1);
2008 memcpy (root, text_to_complete, rootlen);
2009 root[rootlen] = '\0';
2010 new_dir = open_dir (root, cmpl_state);
2012 *remaining_text = g_path_skip_root (text_to_complete);
2016 else if (text_to_complete[0] == '~')
2018 new_dir = open_user_dir(text_to_complete, cmpl_state);
2023 *remaining_text = first_slash + 1;
2025 *remaining_text = text_to_complete + strlen(text_to_complete);
2035 *remaining_text = text_to_complete;
2037 new_dir = open_dir(cmpl_state->reference_dir->fullname, cmpl_state);
2042 new_dir->cmpl_index = -1;
2043 new_dir->cmpl_parent = NULL;
2051 /* open a directory by user name */
2052 static CompletionDir*
2053 open_user_dir(gchar* text_to_complete,
2054 CompletionState *cmpl_state)
2059 g_assert(text_to_complete && text_to_complete[0] == '~');
2061 first_slash = strchr(text_to_complete, G_DIR_SEPARATOR);
2064 cmp_len = first_slash - text_to_complete - 1;
2066 cmp_len = strlen(text_to_complete + 1);
2071 gchar *homedir = g_get_home_dir ();
2074 return open_dir(homedir, cmpl_state);
2081 char* copy = g_new(char, cmp_len + 1);
2083 strncpy(copy, text_to_complete + 1, cmp_len);
2085 pwd = getpwnam(copy);
2093 return open_dir(pwd->pw_dir, cmpl_state);
2099 /* open a directory relative the the current relative directory */
2100 static CompletionDir*
2101 open_relative_dir(gchar* dir_name,
2103 CompletionState *cmpl_state)
2105 gchar path_buf[2*MAXPATHLEN];
2107 if(dir->fullname_len + strlen(dir_name) + 2 >= MAXPATHLEN)
2109 cmpl_errno = CMPL_ERRNO_TOO_LONG;
2113 strcpy(path_buf, dir->fullname);
2115 if(dir->fullname_len > 1)
2117 if (path_buf[dir->fullname_len - 1] != G_DIR_SEPARATOR)
2119 path_buf[dir->fullname_len] = G_DIR_SEPARATOR;
2120 strcpy (path_buf + dir->fullname_len + 1, dir_name);
2123 strcpy (path_buf + dir->fullname_len, dir_name);
2127 strcpy(path_buf + dir->fullname_len, dir_name);
2130 return open_dir(path_buf, cmpl_state);
2133 /* after the cache lookup fails, really open a new directory */
2134 static CompletionDirSent*
2135 open_new_dir(gchar* dir_name, struct stat* sbuf, gboolean stat_subdirs)
2137 CompletionDirSent* sent;
2140 struct dirent *dirent_ptr;
2141 gint buffer_size = 0;
2142 gint entry_count = 0;
2144 struct stat ent_sbuf;
2145 char path_buf[MAXPATHLEN*2];
2148 sent = g_new(CompletionDirSent, 1);
2149 sent->mtime = sbuf->st_mtime;
2150 sent->inode = sbuf->st_ino;
2151 sent->device = sbuf->st_dev;
2153 path_buf_len = strlen(dir_name);
2155 if (path_buf_len > MAXPATHLEN)
2157 cmpl_errno = CMPL_ERRNO_TOO_LONG;
2161 strcpy(path_buf, dir_name);
2163 directory = opendir(dir_name);
2171 while((dirent_ptr = readdir(directory)) != NULL)
2173 int entry_len = strlen(dirent_ptr->d_name);
2174 buffer_size += entry_len + 1;
2177 if(path_buf_len + entry_len + 2 >= MAXPATHLEN)
2179 cmpl_errno = CMPL_ERRNO_TOO_LONG;
2180 closedir(directory);
2185 sent->name_buffer = g_new(gchar, buffer_size);
2186 sent->entries = g_new(CompletionDirEntry, entry_count);
2187 sent->entry_count = entry_count;
2189 buffer_ptr = sent->name_buffer;
2191 rewinddir(directory);
2193 for(i = 0; i < entry_count; i += 1)
2195 dirent_ptr = readdir(directory);
2200 closedir(directory);
2204 strcpy(buffer_ptr, dirent_ptr->d_name);
2205 sent->entries[i].entry_name = buffer_ptr;
2206 buffer_ptr += strlen(dirent_ptr->d_name);
2210 if (path_buf[path_buf_len-1] != G_DIR_SEPARATOR)
2212 path_buf[path_buf_len] = G_DIR_SEPARATOR;
2213 strcpy(path_buf + path_buf_len + 1, dirent_ptr->d_name);
2216 strcpy(path_buf + path_buf_len, dirent_ptr->d_name);
2220 if(stat(path_buf, &ent_sbuf) >= 0 && S_ISDIR(ent_sbuf.st_mode))
2221 sent->entries[i].is_dir = 1;
2223 /* stat may fail, and we don't mind, since it could be a
2224 * dangling symlink. */
2225 sent->entries[i].is_dir = 0;
2228 sent->entries[i].is_dir = 1;
2231 qsort(sent->entries, sent->entry_count, sizeof(CompletionDirEntry), compare_cmpl_dir);
2233 closedir(directory);
2238 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
2241 check_dir(gchar *dir_name, struct stat *result, gboolean *stat_subdirs)
2243 /* A list of directories that we know only contain other directories.
2244 * Trying to stat every file in these directories would be very
2251 struct stat statbuf;
2252 } no_stat_dirs[] = {
2253 { "/afs", FALSE, { 0 } },
2254 { "/net", FALSE, { 0 } }
2257 static const gint n_no_stat_dirs = sizeof(no_stat_dirs) / sizeof(no_stat_dirs[0]);
2258 static gboolean initialized = FALSE;
2265 for (i = 0; i < n_no_stat_dirs; i++)
2267 if (stat (no_stat_dirs[i].name, &no_stat_dirs[i].statbuf) == 0)
2268 no_stat_dirs[i].present = TRUE;
2272 if(stat(dir_name, result) < 0)
2278 *stat_subdirs = TRUE;
2279 for (i=0; i<n_no_stat_dirs; i++)
2281 if (no_stat_dirs[i].present &&
2282 (no_stat_dirs[i].statbuf.st_dev == result->st_dev) &&
2283 (no_stat_dirs[i].statbuf.st_ino == result->st_ino))
2285 *stat_subdirs = FALSE;
2295 /* open a directory by absolute pathname */
2296 static CompletionDir*
2297 open_dir(gchar* dir_name, CompletionState* cmpl_state)
2300 gboolean stat_subdirs;
2301 CompletionDirSent *sent;
2304 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
2305 if (!check_dir (dir_name, &sbuf, &stat_subdirs))
2308 cdsl = cmpl_state->directory_sent_storage;
2314 if(sent->inode == sbuf.st_ino &&
2315 sent->mtime == sbuf.st_mtime &&
2316 sent->device == sbuf.st_dev)
2317 return attach_dir(sent, dir_name, cmpl_state);
2322 stat_subdirs = TRUE;
2325 sent = open_new_dir(dir_name, &sbuf, stat_subdirs);
2328 cmpl_state->directory_sent_storage =
2329 g_list_prepend(cmpl_state->directory_sent_storage, sent);
2331 return attach_dir(sent, dir_name, cmpl_state);
2337 static CompletionDir*
2338 attach_dir(CompletionDirSent* sent, gchar* dir_name, CompletionState *cmpl_state)
2340 CompletionDir* new_dir;
2342 new_dir = g_new(CompletionDir, 1);
2344 cmpl_state->directory_storage =
2345 g_list_prepend(cmpl_state->directory_storage, new_dir);
2347 new_dir->sent = sent;
2348 new_dir->fullname = g_strdup(dir_name);
2349 new_dir->fullname_len = strlen(dir_name);
2355 correct_dir_fullname(CompletionDir* cmpl_dir)
2357 gint length = strlen(cmpl_dir->fullname);
2358 gchar *first_slash = strchr(cmpl_dir->fullname, G_DIR_SEPARATOR);
2361 /* Does it end with /. (\.) ? */
2363 strcmp(cmpl_dir->fullname + length - 2, G_DIR_SEPARATOR_S ".") == 0)
2365 /* Is it just the root directory (on a drive) ? */
2366 if (cmpl_dir->fullname + length - 2 == first_slash)
2368 cmpl_dir->fullname[length - 1] = 0;
2369 cmpl_dir->fullname_len = length - 1;
2374 cmpl_dir->fullname[length - 2] = 0;
2378 /* Ends with /./ (\.\)? */
2379 else if (length >= 3 &&
2380 strcmp(cmpl_dir->fullname + length - 3,
2381 G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) == 0)
2382 cmpl_dir->fullname[length - 2] = 0;
2384 /* Ends with /.. (\..) ? */
2385 else if (length >= 3 &&
2386 strcmp(cmpl_dir->fullname + length - 3,
2387 G_DIR_SEPARATOR_S "..") == 0)
2389 /* Is it just /.. (X:\..)? */
2390 if(cmpl_dir->fullname + length - 3 == first_slash)
2392 cmpl_dir->fullname[length - 2] = 0;
2393 cmpl_dir->fullname_len = length - 2;
2397 if(stat(cmpl_dir->fullname, &sbuf) < 0)
2403 cmpl_dir->fullname[length - 3] = 0;
2405 if(!correct_parent(cmpl_dir, &sbuf))
2409 /* Ends with /../ (\..\)? */
2410 else if (length >= 4 &&
2411 strcmp(cmpl_dir->fullname + length - 4,
2412 G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) == 0)
2414 /* Is it just /../ (X:\..\)? */
2415 if(cmpl_dir->fullname + length - 4 == first_slash)
2417 cmpl_dir->fullname[length - 3] = 0;
2418 cmpl_dir->fullname_len = length - 3;
2422 if(stat(cmpl_dir->fullname, &sbuf) < 0)
2428 cmpl_dir->fullname[length - 4] = 0;
2430 if(!correct_parent(cmpl_dir, &sbuf))
2434 cmpl_dir->fullname_len = strlen(cmpl_dir->fullname);
2440 correct_parent(CompletionDir* cmpl_dir, struct stat *sbuf)
2448 last_slash = strrchr(cmpl_dir->fullname, G_DIR_SEPARATOR);
2449 g_assert(last_slash);
2450 first_slash = strchr(cmpl_dir->fullname, G_DIR_SEPARATOR);
2452 /* Clever (?) way to check for top-level directory that works also on
2453 * Win32, where there is a drive letter and colon prefixed...
2455 if (last_slash != first_slash)
2465 if (stat(cmpl_dir->fullname, &parbuf) < 0)
2469 last_slash[0] = G_DIR_SEPARATOR;
2473 #ifndef G_OS_WIN32 /* No inode numbers on Win32 */
2474 if (parbuf.st_ino == sbuf->st_ino && parbuf.st_dev == sbuf->st_dev)
2475 /* it wasn't a link */
2481 last_slash[0] = G_DIR_SEPARATOR;
2483 /* it was a link, have to figure it out the hard way */
2485 new_name = find_parent_dir_fullname(cmpl_dir->fullname);
2490 g_free(cmpl_dir->fullname);
2492 cmpl_dir->fullname = new_name;
2501 find_parent_dir_fullname(gchar* dirname)
2506 orig_dir = g_get_current_dir ();
2508 if(chdir(dirname) != 0 || chdir("..") != 0)
2514 result = g_get_current_dir ();
2516 if(chdir(orig_dir) != 0)
2528 /**********************************************************************/
2529 /* Completion Operations */
2530 /**********************************************************************/
2534 static PossibleCompletion*
2535 attempt_homedir_completion(gchar* text_to_complete,
2536 CompletionState *cmpl_state)
2540 if (!cmpl_state->user_dir_name_buffer &&
2541 !get_pwdb(cmpl_state))
2543 length = strlen(text_to_complete) - 1;
2545 cmpl_state->user_completion_index += 1;
2547 while(cmpl_state->user_completion_index < cmpl_state->user_directories_len)
2549 index = first_diff_index(text_to_complete + 1,
2550 cmpl_state->user_directories
2551 [cmpl_state->user_completion_index].login);
2558 if(cmpl_state->last_valid_char < (index + 1))
2559 cmpl_state->last_valid_char = index + 1;
2560 cmpl_state->user_completion_index += 1;
2564 cmpl_state->the_completion.is_a_completion = 1;
2565 cmpl_state->the_completion.is_directory = 1;
2567 append_completion_text("~", cmpl_state);
2569 append_completion_text(cmpl_state->
2570 user_directories[cmpl_state->user_completion_index].login,
2573 return append_completion_text(G_DIR_SEPARATOR_S, cmpl_state);
2576 if(text_to_complete[1] ||
2577 cmpl_state->user_completion_index > cmpl_state->user_directories_len)
2579 cmpl_state->user_completion_index = -1;
2584 cmpl_state->user_completion_index += 1;
2585 cmpl_state->the_completion.is_a_completion = 1;
2586 cmpl_state->the_completion.is_directory = 1;
2588 return append_completion_text("~" G_DIR_SEPARATOR_S, cmpl_state);
2594 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
2595 #define FOLD(c) (tolower(c))
2600 /* returns the index (>= 0) of the first differing character,
2601 * PATTERN_MATCH if the completion matches */
2603 first_diff_index(gchar* pat, gchar* text)
2607 while(*pat && *text && FOLD(*text) == FOLD(*pat))
2617 return PATTERN_MATCH;
2620 static PossibleCompletion*
2621 append_completion_text(gchar* text, CompletionState* cmpl_state)
2625 if(!cmpl_state->the_completion.text)
2628 len = strlen(text) + strlen(cmpl_state->the_completion.text) + 1;
2630 if(cmpl_state->the_completion.text_alloc > len)
2632 strcat(cmpl_state->the_completion.text, text);
2633 return &cmpl_state->the_completion;
2636 while(i < len) { i <<= 1; }
2638 cmpl_state->the_completion.text_alloc = i;
2640 cmpl_state->the_completion.text = (gchar*)g_realloc(cmpl_state->the_completion.text, i);
2642 if(!cmpl_state->the_completion.text)
2646 strcat(cmpl_state->the_completion.text, text);
2647 return &cmpl_state->the_completion;
2651 static CompletionDir*
2652 find_completion_dir(gchar* text_to_complete,
2653 gchar** remaining_text,
2654 CompletionState* cmpl_state)
2656 gchar* first_slash = strchr(text_to_complete, G_DIR_SEPARATOR);
2657 CompletionDir* dir = cmpl_state->reference_dir;
2658 CompletionDir* next;
2659 *remaining_text = text_to_complete;
2663 gint len = first_slash - *remaining_text;
2665 gchar *found_name = NULL; /* Quiet gcc */
2667 gchar* pat_buf = g_new (gchar, len + 1);
2669 strncpy(pat_buf, *remaining_text, len);
2672 for(i = 0; i < dir->sent->entry_count; i += 1)
2674 if(dir->sent->entries[i].is_dir &&
2675 fnmatch(pat_buf, dir->sent->entries[i].entry_name,
2676 FNMATCH_FLAGS)!= FNM_NOMATCH)
2686 found_name = dir->sent->entries[i].entry_name;
2693 /* Perhaps we are trying to open an automount directory */
2694 found_name = pat_buf;
2697 next = open_relative_dir(found_name, dir, cmpl_state);
2705 next->cmpl_parent = dir;
2709 if(!correct_dir_fullname(dir))
2715 *remaining_text = first_slash + 1;
2716 first_slash = strchr(*remaining_text, G_DIR_SEPARATOR);
2725 update_cmpl(PossibleCompletion* poss, CompletionState* cmpl_state)
2729 if(!poss || !cmpl_is_a_completion(poss))
2732 cmpl_len = strlen(cmpl_this_completion(poss));
2734 if(cmpl_state->updated_text_alloc < cmpl_len + 1)
2736 cmpl_state->updated_text =
2737 (gchar*)g_realloc(cmpl_state->updated_text,
2738 cmpl_state->updated_text_alloc);
2739 cmpl_state->updated_text_alloc = 2*cmpl_len;
2742 if(cmpl_state->updated_text_len < 0)
2744 strcpy(cmpl_state->updated_text, cmpl_this_completion(poss));
2745 cmpl_state->updated_text_len = cmpl_len;
2746 cmpl_state->re_complete = cmpl_is_directory(poss);
2748 else if(cmpl_state->updated_text_len == 0)
2750 cmpl_state->re_complete = FALSE;
2755 first_diff_index(cmpl_state->updated_text,
2756 cmpl_this_completion(poss));
2758 cmpl_state->re_complete = FALSE;
2760 if(first_diff == PATTERN_MATCH)
2763 if(first_diff > cmpl_state->updated_text_len)
2764 strcpy(cmpl_state->updated_text, cmpl_this_completion(poss));
2766 cmpl_state->updated_text_len = first_diff;
2767 cmpl_state->updated_text[first_diff] = 0;
2771 static PossibleCompletion*
2772 attempt_file_completion(CompletionState *cmpl_state)
2774 gchar *pat_buf, *first_slash;
2775 CompletionDir *dir = cmpl_state->active_completion_dir;
2777 dir->cmpl_index += 1;
2779 if(dir->cmpl_index == dir->sent->entry_count)
2781 if(dir->cmpl_parent == NULL)
2783 cmpl_state->active_completion_dir = NULL;
2789 cmpl_state->active_completion_dir = dir->cmpl_parent;
2791 return attempt_file_completion(cmpl_state);
2795 g_assert(dir->cmpl_text);
2797 first_slash = strchr(dir->cmpl_text, G_DIR_SEPARATOR);
2801 gint len = first_slash - dir->cmpl_text;
2803 pat_buf = g_new (gchar, len + 1);
2804 strncpy(pat_buf, dir->cmpl_text, len);
2809 gint len = strlen(dir->cmpl_text);
2811 pat_buf = g_new (gchar, len + 2);
2812 strcpy(pat_buf, dir->cmpl_text);
2813 /* Don't append a * if the user entered one herself.
2814 * This way one can complete *.h and don't get matches
2815 * on any .help files, for instance.
2817 if (strchr(pat_buf, '*') == NULL)
2818 strcpy(pat_buf + len, "*");
2823 if(dir->sent->entries[dir->cmpl_index].is_dir)
2825 if(fnmatch(pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
2826 FNMATCH_FLAGS) != FNM_NOMATCH)
2828 CompletionDir* new_dir;
2830 new_dir = open_relative_dir(dir->sent->entries[dir->cmpl_index].entry_name,
2839 new_dir->cmpl_parent = dir;
2841 new_dir->cmpl_index = -1;
2842 new_dir->cmpl_text = first_slash + 1;
2844 cmpl_state->active_completion_dir = new_dir;
2847 return attempt_file_completion(cmpl_state);
2852 return attempt_file_completion(cmpl_state);
2858 return attempt_file_completion(cmpl_state);
2863 if(dir->cmpl_parent != NULL)
2865 append_completion_text(dir->fullname +
2866 strlen(cmpl_state->completion_dir->fullname) + 1,
2868 append_completion_text(G_DIR_SEPARATOR_S, cmpl_state);
2871 append_completion_text(dir->sent->entries[dir->cmpl_index].entry_name, cmpl_state);
2873 cmpl_state->the_completion.is_a_completion =
2874 (fnmatch(pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
2875 FNMATCH_FLAGS) != FNM_NOMATCH);
2877 cmpl_state->the_completion.is_directory = dir->sent->entries[dir->cmpl_index].is_dir;
2878 if(dir->sent->entries[dir->cmpl_index].is_dir)
2879 append_completion_text(G_DIR_SEPARATOR_S, cmpl_state);
2882 return &cmpl_state->the_completion;
2889 get_pwdb(CompletionState* cmpl_state)
2891 struct passwd *pwd_ptr;
2893 gint len = 0, i, count = 0;
2895 if(cmpl_state->user_dir_name_buffer)
2899 while ((pwd_ptr = getpwent()) != NULL)
2901 len += strlen(pwd_ptr->pw_name);
2902 len += strlen(pwd_ptr->pw_dir);
2909 cmpl_state->user_dir_name_buffer = g_new(gchar, len);
2910 cmpl_state->user_directories = g_new(CompletionUserDir, count);
2911 cmpl_state->user_directories_len = count;
2913 buf_ptr = cmpl_state->user_dir_name_buffer;
2915 for(i = 0; i < count; i += 1)
2917 pwd_ptr = getpwent();
2924 strcpy(buf_ptr, pwd_ptr->pw_name);
2925 cmpl_state->user_directories[i].login = buf_ptr;
2926 buf_ptr += strlen(buf_ptr);
2928 strcpy(buf_ptr, pwd_ptr->pw_dir);
2929 cmpl_state->user_directories[i].homedir = buf_ptr;
2930 buf_ptr += strlen(buf_ptr);
2934 qsort(cmpl_state->user_directories,
2935 cmpl_state->user_directories_len,
2936 sizeof(CompletionUserDir),
2945 if(cmpl_state->user_dir_name_buffer)
2946 g_free(cmpl_state->user_dir_name_buffer);
2947 if(cmpl_state->user_directories)
2948 g_free(cmpl_state->user_directories);
2950 cmpl_state->user_dir_name_buffer = NULL;
2951 cmpl_state->user_directories = NULL;
2957 compare_user_dir(const void* a, const void* b)
2959 return strcmp((((CompletionUserDir*)a))->login,
2960 (((CompletionUserDir*)b))->login);
2966 compare_cmpl_dir(const void* a, const void* b)
2968 #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
2969 return strcmp((((CompletionDirEntry*)a))->entry_name,
2970 (((CompletionDirEntry*)b))->entry_name);
2972 return g_strcasecmp((((CompletionDirEntry*)a))->entry_name,
2973 (((CompletionDirEntry*)b))->entry_name);
2978 cmpl_state_okay(CompletionState* cmpl_state)
2980 return cmpl_state && cmpl_state->reference_dir;
2984 cmpl_strerror(gint err)
2986 if(err == CMPL_ERRNO_TOO_LONG)
2987 return "Name too long";
2989 return g_strerror (err);