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 Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <sys/types.h>
21 #include <sys/param.h>
30 #include "gdk/gdkkeysyms.h"
31 #include "gtkbutton.h"
33 #include "gtkfilesel.h"
37 #include "gtklistitem.h"
39 #include "gtkscrolledwindow.h"
40 #include "gtksignal.h"
43 #include "gtkmenuitem.h"
44 #include "gtkoptionmenu.h"
46 #include "gtkdialog.h"
48 #define DIR_LIST_WIDTH 180
49 #define DIR_LIST_HEIGHT 180
50 #define FILE_LIST_WIDTH 180
51 #define FILE_LIST_HEIGHT 180
53 /* I've put this here so it doesn't get confused with the
54 * file completion interface */
55 typedef struct _HistoryCallbackArg HistoryCallbackArg;
57 struct _HistoryCallbackArg
64 typedef struct _CompletionState CompletionState;
65 typedef struct _CompletionDir CompletionDir;
66 typedef struct _CompletionDirSent CompletionDirSent;
67 typedef struct _CompletionDirEntry CompletionDirEntry;
68 typedef struct _CompletionUserDir CompletionUserDir;
69 typedef struct _PossibleCompletion PossibleCompletion;
71 /* Non-external file completion decls and structures */
73 /* A contant telling PRCS how many directories to cache. Its actually
74 * kept in a list, so the geometry isn't important. */
75 #define CMPL_DIRECTORY_CACHE_SIZE 10
77 /* A constant used to determine whether a substring was an exact
78 * match by first_diff_index()
80 #define PATTERN_MATCH -1
81 /* The arguments used by all fnmatch() calls below
83 #define FNMATCH_FLAGS (FNM_PATHNAME | FNM_PERIOD)
85 #define CMPL_ERRNO_TOO_LONG ((1<<16)-1)
87 /* This structure contains all the useful information about a directory
88 * for the purposes of filename completion. These structures are cached
89 * in the CompletionState struct. CompletionDir's are reference counted.
91 struct _CompletionDirSent
97 gchar *name_buffer; /* memory segment containing names of all entries */
99 struct _CompletionDirEntry *entries;
102 struct _CompletionDir
104 CompletionDirSent *sent;
109 struct _CompletionDir *cmpl_parent;
114 /* This structure contains pairs of directory entry names with a flag saying
115 * whether or not they are a valid directory. NOTE: This information is used
116 * to provide the caller with information about whether to update its completions
117 * or try to open a file. Since directories are cached by the directory mtime,
118 * a symlink which points to an invalid file (which will not be a directory),
119 * will not be reevaluated if that file is created, unless the containing
120 * directory is touched. I consider this case to be worth ignoring (josh).
122 struct _CompletionDirEntry
128 struct _CompletionUserDir
134 struct _PossibleCompletion
136 /* accessible fields, all are accessed externally by functions
140 gint is_a_completion;
148 struct _CompletionState
150 gint last_valid_char;
152 gint updated_text_len;
153 gint updated_text_alloc;
156 gchar *user_dir_name_buffer;
157 gint user_directories_len;
158 gchar *user_home_dir;
160 gchar *last_completion_text;
162 gint user_completion_index; /* if >= 0, currently completing ~user */
164 struct _CompletionDir *completion_dir; /* directory completing from */
165 struct _CompletionDir *active_completion_dir;
167 struct _PossibleCompletion the_completion;
169 struct _CompletionDir *reference_dir; /* initial directory */
171 GList* directory_storage;
172 GList* directory_sent_storage;
174 struct _CompletionUserDir *user_directories;
178 /* File completion functions which would be external, were they used
179 * outside of this file.
182 static CompletionState* cmpl_init_state (void);
183 static void cmpl_free_state (CompletionState *cmpl_state);
184 static gint cmpl_state_okay (CompletionState* cmpl_state);
185 static gchar* cmpl_strerror (gint);
187 static PossibleCompletion* cmpl_completion_matches(gchar *text_to_complete,
188 gchar **remaining_text,
189 CompletionState *cmpl_state);
191 /* Returns a name for consideration, possibly a completion, this name
192 * will be invalid after the next call to cmpl_next_completion.
194 static char* cmpl_this_completion (PossibleCompletion*);
196 /* True if this completion matches the given text. Otherwise, this
197 * output can be used to have a list of non-completions.
199 static gint cmpl_is_a_completion (PossibleCompletion*);
201 /* True if the completion is a directory
203 static gint cmpl_is_directory (PossibleCompletion*);
205 /* Obtains the next completion, or NULL
207 static PossibleCompletion* cmpl_next_completion (CompletionState*);
209 /* Updating completions: the return value of cmpl_updated_text() will
210 * be text_to_complete completed as much as possible after the most
211 * recent call to cmpl_completion_matches. For the present
212 * application, this is the suggested replacement for the user's input
213 * string. You must CALL THIS AFTER ALL cmpl_text_completions have
216 static gchar* cmpl_updated_text (CompletionState* cmpl_state);
218 /* After updating, to see if the completion was a directory, call
219 * this. If it was, you should consider re-calling completion_matches.
221 static gint cmpl_updated_dir (CompletionState* cmpl_state);
223 /* Current location: if using file completion, return the current
224 * directory, from which file completion begins. More specifically,
225 * the cwd concatenated with all exact completions up to the last
226 * directory delimiter('/').
228 static gchar* cmpl_reference_position (CompletionState* cmpl_state);
230 /* backing up: if cmpl_completion_matches returns NULL, you may query
231 * the index of the last completable character into cmpl_updated_text.
233 static gint cmpl_last_valid_char (CompletionState* cmpl_state);
235 /* When the user selects a non-directory, call cmpl_completion_fullname
236 * to get the full name of the selected file.
238 static gchar* cmpl_completion_fullname (gchar*, CompletionState* cmpl_state);
241 /* Directory operations. */
242 static CompletionDir* open_ref_dir (gchar* text_to_complete,
243 gchar** remaining_text,
244 CompletionState* cmpl_state);
245 static CompletionDir* open_dir (gchar* dir_name,
246 CompletionState* cmpl_state);
247 static CompletionDir* open_user_dir (gchar* text_to_complete,
248 CompletionState *cmpl_state);
249 static CompletionDir* open_relative_dir (gchar* dir_name, CompletionDir* dir,
250 CompletionState *cmpl_state);
251 static CompletionDirSent* open_new_dir (gchar* dir_name, struct stat* sbuf);
252 static gint correct_dir_fullname (CompletionDir* cmpl_dir);
253 static gint correct_parent (CompletionDir* cmpl_dir,
255 static gchar* find_parent_dir_fullname (gchar* dirname);
256 static CompletionDir* attach_dir (CompletionDirSent* sent,
258 CompletionState *cmpl_state);
259 static void free_dir_sent (CompletionDirSent* sent);
260 static void free_dir (CompletionDir *dir);
261 static void prune_memory_usage(CompletionState *cmpl_state);
263 /* Completion operations */
264 static PossibleCompletion* attempt_homedir_completion(gchar* text_to_complete,
265 CompletionState *cmpl_state);
266 static PossibleCompletion* attempt_file_completion(CompletionState *cmpl_state);
267 static CompletionDir* find_completion_dir(gchar* text_to_complete,
268 gchar** remaining_text,
269 CompletionState* cmpl_state);
270 static PossibleCompletion* append_completion_text(gchar* text,
271 CompletionState* cmpl_state);
272 static gint get_pwdb(CompletionState* cmpl_state);
273 static gint first_diff_index(gchar* pat, gchar* text);
274 static gint compare_user_dir(const void* a, const void* b);
275 static gint compare_cmpl_dir(const void* a, const void* b);
276 static void update_cmpl(PossibleCompletion* poss,
277 CompletionState* cmpl_state);
279 static void gtk_file_selection_class_init (GtkFileSelectionClass *klass);
280 static void gtk_file_selection_init (GtkFileSelection *filesel);
281 static void gtk_file_selection_destroy (GtkObject *object);
282 static gint gtk_file_selection_key_press (GtkWidget *widget,
286 static void gtk_file_selection_file_button (GtkWidget *widget,
289 GdkEventButton *bevent,
292 static void gtk_file_selection_dir_button (GtkWidget *widget,
295 GdkEventButton *bevent,
298 static void gtk_file_selection_populate (GtkFileSelection *fs,
301 static void gtk_file_selection_abort (GtkFileSelection *fs);
303 static void gtk_file_selection_update_history_menu (GtkFileSelection *fs,
306 static void gtk_file_selection_create_dir (GtkWidget *widget, gpointer data);
307 static void gtk_file_selection_delete_file (GtkWidget *widget, gpointer data);
308 static void gtk_file_selection_rename_file (GtkWidget *widget, gpointer data);
312 static GtkWindowClass *parent_class = NULL;
314 /* Saves errno when something cmpl does fails. */
315 static gint cmpl_errno;
318 gtk_file_selection_get_type ()
320 static guint file_selection_type = 0;
322 if (!file_selection_type)
324 GtkTypeInfo filesel_info =
327 sizeof (GtkFileSelection),
328 sizeof (GtkFileSelectionClass),
329 (GtkClassInitFunc) gtk_file_selection_class_init,
330 (GtkObjectInitFunc) gtk_file_selection_init,
331 (GtkArgSetFunc) NULL,
332 (GtkArgGetFunc) NULL,
335 file_selection_type = gtk_type_unique (gtk_window_get_type (), &filesel_info);
338 return file_selection_type;
342 gtk_file_selection_class_init (GtkFileSelectionClass *class)
344 GtkObjectClass *object_class;
346 object_class = (GtkObjectClass*) class;
348 parent_class = gtk_type_class (gtk_window_get_type ());
350 object_class->destroy = gtk_file_selection_destroy;
354 gtk_file_selection_init (GtkFileSelection *filesel)
356 GtkWidget *entry_vbox;
358 GtkWidget *list_hbox;
359 GtkWidget *action_area;
360 GtkWidget *pulldown_hbox;
361 GtkWidget *button_hbox;
363 char *dir_title [] = { "Directories", };
364 char *file_title [] = { "Files", };
366 filesel->cmpl_state = cmpl_init_state ();
368 /* The dialog-sized vertical box */
369 filesel->main_vbox = gtk_vbox_new (FALSE, 10);
370 gtk_container_border_width (GTK_CONTAINER (filesel), 10);
371 gtk_container_add (GTK_CONTAINER (filesel), filesel->main_vbox);
372 gtk_widget_show (filesel->main_vbox);
374 /* The horizontal box containing create, rename etc. buttons */
375 button_hbox = gtk_hbox_new (TRUE, 0);
376 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), button_hbox,
378 gtk_widget_show (button_hbox);
380 /* delete, create directory, and rename */
381 button = gtk_button_new_with_label ("Create Dir");
382 gtk_signal_connect (GTK_OBJECT (button), "clicked",
383 (GtkSignalFunc) gtk_file_selection_create_dir,
385 gtk_box_pack_start (GTK_BOX (button_hbox), button, TRUE, TRUE, 0);
386 gtk_widget_show (button);
388 button = gtk_button_new_with_label ("Delete File");
389 gtk_signal_connect (GTK_OBJECT (button), "clicked",
390 (GtkSignalFunc) gtk_file_selection_delete_file,
392 gtk_box_pack_start (GTK_BOX (button_hbox), button, TRUE, TRUE, 0);
393 gtk_widget_show (button);
395 button = gtk_button_new_with_label ("Rename File");
396 gtk_signal_connect (GTK_OBJECT (button), "clicked",
397 (GtkSignalFunc) gtk_file_selection_rename_file,
399 gtk_box_pack_start (GTK_BOX (button_hbox), button, TRUE, TRUE, 0);
400 gtk_widget_show (button);
402 /* The Help button */
403 filesel->help_button = gtk_button_new_with_label ("Help");
404 gtk_box_pack_start (GTK_BOX (button_hbox), filesel->help_button,
406 gtk_widget_show (filesel->help_button);
409 /* hbox for pulldown menu */
410 pulldown_hbox = gtk_hbox_new (TRUE, 5);
411 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), pulldown_hbox, FALSE, FALSE, 0);
412 gtk_widget_show (pulldown_hbox);
415 filesel->history_pulldown = gtk_option_menu_new ();
416 gtk_widget_show (filesel->history_pulldown);
417 gtk_box_pack_start (GTK_BOX (pulldown_hbox), filesel->history_pulldown,
420 /* The horizontal box containing the directory and file listboxes */
421 list_hbox = gtk_hbox_new (FALSE, 5);
422 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), list_hbox, TRUE, TRUE, 0);
423 gtk_widget_show (list_hbox);
425 /* The directories clist */
426 filesel->dir_list = gtk_clist_new_with_titles (1, dir_title);
427 gtk_widget_set_usize (filesel->dir_list, DIR_LIST_WIDTH, DIR_LIST_HEIGHT);
428 gtk_clist_set_column_width (GTK_CLIST (filesel->dir_list), 0, 150);
429 gtk_signal_connect (GTK_OBJECT (filesel->dir_list), "select_row",
430 (GtkSignalFunc) gtk_file_selection_dir_button,
432 gtk_clist_set_policy (GTK_CLIST (filesel->dir_list), GTK_POLICY_ALWAYS, GTK_POLICY_AUTOMATIC);
433 gtk_clist_column_titles_passive (GTK_CLIST (filesel->dir_list));
434 gtk_container_border_width (GTK_CONTAINER (filesel->dir_list), 5);
435 gtk_box_pack_start (GTK_BOX (list_hbox), filesel->dir_list, TRUE, TRUE, 0);
436 gtk_widget_show (filesel->dir_list);
438 /* The files clist */
439 filesel->file_list = gtk_clist_new_with_titles (1, file_title);
440 gtk_widget_set_usize (filesel->file_list, FILE_LIST_WIDTH, FILE_LIST_HEIGHT);
441 gtk_clist_set_column_width (GTK_CLIST (filesel->file_list), 0, 150);
442 gtk_signal_connect (GTK_OBJECT (filesel->file_list), "select_row",
443 (GtkSignalFunc) gtk_file_selection_file_button,
445 gtk_clist_set_policy (GTK_CLIST (filesel->file_list), GTK_POLICY_ALWAYS, GTK_POLICY_AUTOMATIC);
446 gtk_clist_column_titles_passive (GTK_CLIST (filesel->file_list));
447 gtk_container_border_width (GTK_CONTAINER (filesel->file_list), 5);
448 gtk_box_pack_start (GTK_BOX (list_hbox), filesel->file_list, TRUE, TRUE, 0);
449 gtk_widget_show (filesel->file_list);
451 /* The action area */
452 action_area = gtk_hbox_new (TRUE, 10);
453 gtk_box_pack_end (GTK_BOX (filesel->main_vbox), action_area, FALSE, FALSE, 0);
454 gtk_widget_show (action_area);
457 filesel->ok_button = gtk_button_new_with_label ("OK");
458 GTK_WIDGET_SET_FLAGS (filesel->ok_button, GTK_CAN_DEFAULT);
459 gtk_box_pack_start (GTK_BOX (action_area), filesel->ok_button, TRUE, TRUE, 0);
460 gtk_widget_grab_default (filesel->ok_button);
461 gtk_widget_show (filesel->ok_button);
463 /* The Cancel button */
464 filesel->cancel_button = gtk_button_new_with_label ("Cancel");
465 GTK_WIDGET_SET_FLAGS (filesel->cancel_button, GTK_CAN_DEFAULT);
466 gtk_box_pack_start (GTK_BOX (action_area), filesel->cancel_button, TRUE, TRUE, 0);
467 gtk_widget_show (filesel->cancel_button);
469 /* The selection entry widget */
470 entry_vbox = gtk_vbox_new (FALSE, 2);
471 gtk_box_pack_end (GTK_BOX (filesel->main_vbox), entry_vbox, FALSE, FALSE, 0);
472 gtk_widget_show (entry_vbox);
474 filesel->selection_text = label = gtk_label_new ("");
475 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
476 gtk_box_pack_start (GTK_BOX (entry_vbox), label, FALSE, FALSE, 0);
477 gtk_widget_show (label);
479 filesel->selection_entry = gtk_entry_new ();
480 gtk_signal_connect (GTK_OBJECT (filesel->selection_entry), "key_press_event",
481 (GtkSignalFunc) gtk_file_selection_key_press, filesel);
482 gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "focus_in_event",
483 (GtkSignalFunc) gtk_widget_grab_default,
484 GTK_OBJECT (filesel->ok_button));
485 gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "activate",
486 (GtkSignalFunc) gtk_button_clicked,
487 GTK_OBJECT (filesel->ok_button));
488 gtk_box_pack_start (GTK_BOX (entry_vbox), filesel->selection_entry, TRUE, TRUE, 0);
489 gtk_widget_show (filesel->selection_entry);
491 if (!cmpl_state_okay (filesel->cmpl_state))
495 sprintf (err_buf, "Directory unreadable: %s", cmpl_strerror (cmpl_errno));
497 gtk_label_set (GTK_LABEL (filesel->selection_text), err_buf);
501 gtk_file_selection_populate (filesel, "", FALSE);
504 gtk_widget_grab_focus (filesel->selection_entry);
508 gtk_file_selection_new (const gchar *title)
510 GtkFileSelection *filesel;
512 filesel = gtk_type_new (gtk_file_selection_get_type ());
513 gtk_window_set_title (GTK_WINDOW (filesel), title);
515 return GTK_WIDGET (filesel);
519 gtk_file_selection_set_filename (GtkFileSelection *filesel,
520 const gchar *filename)
522 char buf[MAXPATHLEN];
523 const char *name, *last_slash;
525 g_return_if_fail (filesel != NULL);
526 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
527 g_return_if_fail (filename != NULL);
529 last_slash = strrchr (filename, '/');
538 gint len = MIN (MAXPATHLEN - 1, last_slash - filename + 1);
540 strncpy (buf, filename, len);
543 name = last_slash + 1;
546 gtk_file_selection_populate (filesel, buf, FALSE);
548 if (filesel->selection_entry)
549 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), name);
553 gtk_file_selection_get_filename (GtkFileSelection *filesel)
555 static char nothing[2] = "";
559 g_return_val_if_fail (filesel != NULL, nothing);
560 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), nothing);
562 text = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
565 filename = cmpl_completion_fullname (text, filesel->cmpl_state);
573 gtk_file_selection_destroy (GtkObject *object)
575 GtkFileSelection *filesel;
577 HistoryCallbackArg *callback_arg;
579 g_return_if_fail (object != NULL);
580 g_return_if_fail (GTK_IS_FILE_SELECTION (object));
582 filesel = GTK_FILE_SELECTION (object);
584 if (filesel->fileop_dialog)
585 gtk_widget_destroy (filesel->fileop_dialog);
587 if (filesel->history_list) {
588 list = filesel->history_list;
590 callback_arg = list->data;
591 g_free (callback_arg->directory);
594 g_list_free (filesel->history_list);
597 cmpl_free_state (filesel->cmpl_state);
599 if (GTK_OBJECT_CLASS (parent_class)->destroy)
600 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
603 /* Begin file operations callbacks */
606 gtk_file_selection_fileop_error (gchar *error_message)
613 g_return_if_fail (error_message != NULL);
616 dialog = gtk_dialog_new ();
618 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
619 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
622 gtk_window_set_title (GTK_WINDOW (dialog), "Error");
623 gtk_window_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
625 vbox = gtk_vbox_new(FALSE, 0);
626 gtk_container_border_width(GTK_CONTAINER(vbox), 8);
627 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
629 gtk_widget_show(vbox);
631 label = gtk_label_new(error_message);
632 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
633 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
634 gtk_widget_show(label);
636 /* yes, we free it */
637 g_free (error_message);
640 button = gtk_button_new_with_label ("Close");
641 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
642 (GtkSignalFunc) gtk_widget_destroy,
644 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
645 button, TRUE, TRUE, 0);
646 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
647 gtk_widget_grab_default(button);
648 gtk_widget_show (button);
650 gtk_widget_show (dialog);
654 gtk_file_selection_fileop_destroy (GtkWidget *widget, gpointer data)
656 GtkFileSelection *fs = data;
658 g_return_if_fail (fs != NULL);
659 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
661 fs->fileop_dialog = NULL;
666 gtk_file_selection_create_dir_confirmed (GtkWidget *widget, gpointer data)
668 GtkFileSelection *fs = data;
673 CompletionState *cmpl_state;
675 g_return_if_fail (fs != NULL);
676 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
678 dirname = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
679 cmpl_state = (CompletionState*) fs->cmpl_state;
680 path = cmpl_reference_position (cmpl_state);
682 full_path = g_strconcat (path, "/", dirname, NULL);
683 if ( (mkdir (full_path, 0755) < 0) )
685 buf = g_strconcat ("Error creating directory \"", dirname, "\": ",
686 g_strerror(errno), NULL);
687 gtk_file_selection_fileop_error (buf);
691 gtk_widget_destroy (fs->fileop_dialog);
692 gtk_file_selection_populate (fs, "", FALSE);
696 gtk_file_selection_create_dir (GtkWidget *widget, gpointer data)
698 GtkFileSelection *fs = data;
704 g_return_if_fail (fs != NULL);
705 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
707 if (fs->fileop_dialog)
711 fs->fileop_dialog = dialog = gtk_dialog_new ();
712 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
713 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
715 gtk_window_set_title (GTK_WINDOW (dialog), "Create Directory");
716 gtk_window_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
717 gtk_widget_show (dialog);
719 vbox = gtk_vbox_new(FALSE, 0);
720 gtk_container_border_width(GTK_CONTAINER(vbox), 8);
721 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
723 gtk_widget_show(vbox);
725 label = gtk_label_new("Directory name:");
726 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
727 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
728 gtk_widget_show(label);
730 /* The directory entry widget */
731 fs->fileop_entry = gtk_entry_new ();
732 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
734 GTK_WIDGET_SET_FLAGS(fs->fileop_entry, GTK_CAN_DEFAULT);
735 gtk_widget_show (fs->fileop_entry);
738 button = gtk_button_new_with_label ("Create");
739 gtk_signal_connect (GTK_OBJECT (button), "clicked",
740 (GtkSignalFunc) gtk_file_selection_create_dir_confirmed,
742 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
743 button, TRUE, TRUE, 0);
744 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
745 gtk_widget_show(button);
747 button = gtk_button_new_with_label ("Cancel");
748 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
749 (GtkSignalFunc) gtk_widget_destroy,
751 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
752 button, TRUE, TRUE, 0);
753 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
754 gtk_widget_grab_default(button);
755 gtk_widget_show (button);
759 gtk_file_selection_delete_file_confirmed (GtkWidget *widget, gpointer data)
761 GtkFileSelection *fs = data;
762 CompletionState *cmpl_state;
767 g_return_if_fail (fs != NULL);
768 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
770 cmpl_state = (CompletionState*) fs->cmpl_state;
771 path = cmpl_reference_position (cmpl_state);
773 full_path = g_strconcat (path, "/", fs->fileop_file, NULL);
774 if ( (unlink (full_path) < 0) )
776 buf = g_strconcat ("Error deleting file \"", fs->fileop_file, "\": ",
777 g_strerror(errno), NULL);
778 gtk_file_selection_fileop_error (buf);
782 gtk_widget_destroy (fs->fileop_dialog);
783 gtk_file_selection_populate (fs, "", FALSE);
787 gtk_file_selection_delete_file (GtkWidget *widget, gpointer data)
789 GtkFileSelection *fs = data;
797 g_return_if_fail (fs != NULL);
798 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
800 if (fs->fileop_dialog)
803 filename = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
804 if (strlen(filename) < 1)
807 fs->fileop_file = filename;
810 fs->fileop_dialog = dialog = gtk_dialog_new ();
811 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
812 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
814 gtk_window_set_title (GTK_WINDOW (dialog), "Delete File");
815 gtk_window_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
817 vbox = gtk_vbox_new(FALSE, 0);
818 gtk_container_border_width(GTK_CONTAINER(vbox), 8);
819 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
821 gtk_widget_show(vbox);
823 buf = g_strconcat ("Really delete file \"", filename, "\" ?", NULL);
824 label = gtk_label_new(buf);
825 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
826 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
827 gtk_widget_show(label);
831 button = gtk_button_new_with_label ("Delete");
832 gtk_signal_connect (GTK_OBJECT (button), "clicked",
833 (GtkSignalFunc) gtk_file_selection_delete_file_confirmed,
835 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
836 button, TRUE, TRUE, 0);
837 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
838 gtk_widget_show(button);
840 button = gtk_button_new_with_label ("Cancel");
841 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
842 (GtkSignalFunc) gtk_widget_destroy,
844 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
845 button, TRUE, TRUE, 0);
846 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
847 gtk_widget_grab_default(button);
848 gtk_widget_show (button);
850 gtk_widget_show (dialog);
854 gtk_file_selection_rename_file_confirmed (GtkWidget *widget, gpointer data)
856 GtkFileSelection *fs = data;
862 CompletionState *cmpl_state;
864 g_return_if_fail (fs != NULL);
865 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
867 file = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
868 cmpl_state = (CompletionState*) fs->cmpl_state;
869 path = cmpl_reference_position (cmpl_state);
871 new_filename = g_strconcat (path, "/", file, NULL);
872 old_filename = g_strconcat (path, "/", fs->fileop_file, NULL);
874 if ( (rename (old_filename, new_filename)) < 0)
876 buf = g_strconcat ("Error renaming file \"", file, "\": ",
877 g_strerror(errno), NULL);
878 gtk_file_selection_fileop_error (buf);
880 g_free (new_filename);
881 g_free (old_filename);
883 gtk_widget_destroy (fs->fileop_dialog);
884 gtk_file_selection_populate (fs, "", FALSE);
888 gtk_file_selection_rename_file (GtkWidget *widget, gpointer data)
890 GtkFileSelection *fs = data;
897 g_return_if_fail (fs != NULL);
898 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
900 if (fs->fileop_dialog)
903 fs->fileop_file = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
904 if (strlen(fs->fileop_file) < 1)
908 fs->fileop_dialog = dialog = gtk_dialog_new ();
909 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
910 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
912 gtk_window_set_title (GTK_WINDOW (dialog), "Rename File");
913 gtk_window_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
914 gtk_widget_show (dialog);
916 vbox = gtk_vbox_new(FALSE, 0);
917 gtk_container_border_width(GTK_CONTAINER(vbox), 8);
918 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
920 gtk_widget_show(vbox);
922 buf = g_strconcat ("Rename file \"", fs->fileop_file, "\" to:", NULL);
923 label = gtk_label_new(buf);
924 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
925 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
926 gtk_widget_show(label);
929 /* New filename entry */
930 fs->fileop_entry = gtk_entry_new ();
931 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
933 GTK_WIDGET_SET_FLAGS(fs->fileop_entry, GTK_CAN_DEFAULT);
934 gtk_widget_show (fs->fileop_entry);
936 gtk_entry_set_text (GTK_ENTRY (fs->fileop_entry), fs->fileop_file);
937 gtk_entry_select_region (GTK_ENTRY (fs->fileop_entry),
938 0, strlen (fs->fileop_file));
941 button = gtk_button_new_with_label ("Rename");
942 gtk_signal_connect (GTK_OBJECT (button), "clicked",
943 (GtkSignalFunc) gtk_file_selection_rename_file_confirmed,
945 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
946 button, TRUE, TRUE, 0);
947 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
948 gtk_widget_show(button);
950 button = gtk_button_new_with_label ("Cancel");
951 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
952 (GtkSignalFunc) gtk_widget_destroy,
954 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
955 button, TRUE, TRUE, 0);
956 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
957 gtk_widget_grab_default(button);
958 gtk_widget_show (button);
963 gtk_file_selection_key_press (GtkWidget *widget,
967 GtkFileSelection *fs;
970 g_return_val_if_fail (widget != NULL, FALSE);
971 g_return_val_if_fail (event != NULL, FALSE);
973 if (event->keyval == GDK_Tab)
975 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "key_press_event");
977 fs = GTK_FILE_SELECTION (user_data);
978 text = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
979 gtk_file_selection_populate (fs, text, TRUE);
989 gtk_file_selection_history_callback (GtkWidget *widget, gpointer data)
991 GtkFileSelection *fs = data;
992 HistoryCallbackArg *callback_arg;
995 g_return_if_fail (fs != NULL);
996 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
998 list = fs->history_list;
1001 callback_arg = list->data;
1003 if (callback_arg->menu_item == widget)
1005 gtk_file_selection_populate (fs, callback_arg->directory, FALSE);
1014 gtk_file_selection_update_history_menu (GtkFileSelection *fs,
1015 gchar *current_directory)
1017 HistoryCallbackArg *callback_arg;
1018 GtkWidget *menu_item;
1025 g_return_if_fail (fs != NULL);
1026 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1027 g_return_if_fail (current_directory != NULL);
1029 list = fs->history_list;
1031 if (fs->history_menu)
1034 callback_arg = list->data;
1035 g_free (callback_arg->directory);
1038 g_list_free (fs->history_list);
1039 fs->history_list = NULL;
1041 gtk_widget_destroy (fs->history_menu);
1044 fs->history_menu = gtk_menu_new();
1046 current_dir = g_strdup(current_directory);
1048 dir_len = strlen (current_dir);
1050 for (i = dir_len; i >= 0; i--)
1052 /* the i == dir_len is to catch the full path for the first
1054 if ( (current_dir[i] == '/') || (i == dir_len))
1056 /* another small hack to catch the full path */
1058 current_dir[i + 1] = '\0';
1059 menu_item = gtk_menu_item_new_with_label (current_dir);
1060 directory = g_strdup (current_dir);
1062 callback_arg = g_new (HistoryCallbackArg, 1);
1063 callback_arg->menu_item = menu_item;
1065 /* since the autocompletion gets confused if you don't
1066 * supply a trailing '/' on a dir entry, set the full
1067 * (current) path to "" which just refreshes the filesel */
1069 callback_arg->directory = g_strdup ("");
1071 callback_arg->directory = directory;
1074 fs->history_list = g_list_append (fs->history_list, callback_arg);
1076 gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
1077 (GtkSignalFunc) gtk_file_selection_history_callback,
1079 gtk_menu_append (GTK_MENU (fs->history_menu), menu_item);
1080 gtk_widget_show (menu_item);
1084 gtk_option_menu_set_menu (GTK_OPTION_MENU (fs->history_pulldown),
1086 g_free (current_dir);
1092 gtk_file_selection_file_button (GtkWidget *widget,
1095 GdkEventButton *bevent,
1098 GtkFileSelection *fs = NULL;
1101 g_return_if_fail (GTK_IS_CLIST (widget));
1103 fs = GTK_FILE_SELECTION (user_data);
1104 g_return_if_fail (fs != NULL);
1105 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1107 filename = gtk_clist_get_row_data (GTK_CLIST (fs->file_list), row);
1109 if (bevent && filename) {
1111 switch (bevent->type)
1113 case GDK_BUTTON_PRESS:
1114 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1117 case GDK_2BUTTON_PRESS:
1118 gtk_button_clicked (GTK_BUTTON (fs->ok_button));
1128 gtk_file_selection_dir_button (GtkWidget *widget,
1131 GdkEventButton *bevent,
1134 GtkFileSelection *fs = NULL;
1137 g_return_if_fail (GTK_IS_CLIST (widget));
1139 fs = GTK_FILE_SELECTION (user_data);
1140 g_return_if_fail (fs != NULL);
1141 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1143 filename = gtk_clist_get_row_data (GTK_CLIST (fs->dir_list), row);
1145 if (bevent && filename) {
1147 switch (bevent->type)
1149 case GDK_BUTTON_PRESS:
1150 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1153 case GDK_2BUTTON_PRESS:
1154 gtk_file_selection_populate (fs, filename, FALSE);
1164 gtk_file_selection_populate (GtkFileSelection *fs,
1168 CompletionState *cmpl_state;
1169 PossibleCompletion* poss;
1172 gchar* rem_path = rel_path;
1175 gint did_recurse = FALSE;
1176 gint possible_count = 0;
1177 gint selection_index = -1;
1179 g_return_if_fail (fs != NULL);
1180 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1182 cmpl_state = (CompletionState*) fs->cmpl_state;
1183 poss = cmpl_completion_matches (rel_path, &rem_path, cmpl_state);
1185 if (!cmpl_state_okay (cmpl_state))
1187 /* Something went wrong. */
1188 gtk_file_selection_abort (fs);
1192 g_assert (cmpl_state->reference_dir);
1194 gtk_clist_freeze (GTK_CLIST (fs->dir_list));
1195 gtk_clist_clear (GTK_CLIST (fs->dir_list));
1196 gtk_clist_freeze (GTK_CLIST (fs->file_list));
1197 gtk_clist_clear (GTK_CLIST (fs->file_list));
1199 /* Set the dir_list to include ./ and ../ */
1202 row = gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1203 gtk_clist_set_row_data (GTK_CLIST (fs->dir_list), row, "./");
1206 row = gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1207 gtk_clist_set_row_data (GTK_CLIST (fs->dir_list), row, "../");
1211 if (cmpl_is_a_completion (poss))
1213 possible_count += 1;
1215 filename = g_strdup (cmpl_this_completion (poss));
1219 if (cmpl_is_directory (poss))
1221 if (strcmp (filename, "./") != 0 &&
1222 strcmp (filename, "../") != 0)
1224 row = gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1225 gtk_clist_set_row_data (GTK_CLIST (fs->dir_list), row,
1231 row = gtk_clist_append (GTK_CLIST (fs->file_list), text);
1232 gtk_clist_set_row_data (GTK_CLIST (fs->file_list), row,
1237 poss = cmpl_next_completion (cmpl_state);
1240 gtk_clist_thaw (GTK_CLIST (fs->dir_list));
1241 gtk_clist_thaw (GTK_CLIST (fs->file_list));
1243 /* File lists are set. */
1245 g_assert (cmpl_state->reference_dir);
1250 /* User is trying to complete filenames, so advance the user's input
1251 * string to the updated_text, which is the common leading substring
1252 * of all possible completions, and if its a directory attempt
1253 * attempt completions in it. */
1255 if (cmpl_updated_text (cmpl_state)[0])
1258 if (cmpl_updated_dir (cmpl_state))
1260 gchar* dir_name = g_strdup (cmpl_updated_text (cmpl_state));
1264 gtk_file_selection_populate (fs, dir_name, TRUE);
1270 if (fs->selection_entry)
1271 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry),
1272 cmpl_updated_text (cmpl_state));
1277 selection_index = cmpl_last_valid_char (cmpl_state) -
1278 (strlen (rel_path) - strlen (rem_path));
1279 if (fs->selection_entry)
1280 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), rem_path);
1285 if (fs->selection_entry)
1286 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
1291 if (fs->selection_entry)
1292 gtk_entry_set_position (GTK_ENTRY (fs->selection_entry), selection_index);
1294 if (fs->selection_entry)
1296 sel_text = g_new (char, strlen (cmpl_reference_position (cmpl_state)) +
1297 sizeof ("Selection: "));
1298 strcpy (sel_text, "Selection: ");
1299 strcat (sel_text, cmpl_reference_position (cmpl_state));
1301 gtk_label_set (GTK_LABEL (fs->selection_text), sel_text);
1305 if (fs->history_pulldown)
1307 gtk_file_selection_update_history_menu (fs, cmpl_reference_position (cmpl_state));
1314 gtk_file_selection_abort (GtkFileSelection *fs)
1318 sprintf (err_buf, "Directory unreadable: %s", cmpl_strerror (cmpl_errno));
1320 /* BEEP gdk_beep(); */
1322 if (fs->selection_entry)
1323 gtk_label_set (GTK_LABEL (fs->selection_text), err_buf);
1326 /**********************************************************************/
1327 /* External Interface */
1328 /**********************************************************************/
1330 /* The four completion state selectors
1333 cmpl_updated_text (CompletionState* cmpl_state)
1335 return cmpl_state->updated_text;
1339 cmpl_updated_dir (CompletionState* cmpl_state)
1341 return cmpl_state->re_complete;
1345 cmpl_reference_position (CompletionState* cmpl_state)
1347 return cmpl_state->reference_dir->fullname;
1351 cmpl_last_valid_char (CompletionState* cmpl_state)
1353 return cmpl_state->last_valid_char;
1357 cmpl_completion_fullname (gchar* text, CompletionState* cmpl_state)
1361 strcpy (cmpl_state->updated_text, text);
1363 else if (text[0] == '~')
1368 dir = open_user_dir (text, cmpl_state);
1372 /* spencer says just return ~something, so
1373 * for now just do it. */
1374 strcpy (cmpl_state->updated_text, text);
1379 strcpy (cmpl_state->updated_text, dir->fullname);
1381 slash = strchr (text, '/');
1384 strcat (cmpl_state->updated_text, slash);
1389 strcpy (cmpl_state->updated_text, cmpl_state->reference_dir->fullname);
1390 strcat (cmpl_state->updated_text, "/");
1391 strcat (cmpl_state->updated_text, text);
1394 return cmpl_state->updated_text;
1397 /* The three completion selectors
1400 cmpl_this_completion (PossibleCompletion* pc)
1406 cmpl_is_directory (PossibleCompletion* pc)
1408 return pc->is_directory;
1412 cmpl_is_a_completion (PossibleCompletion* pc)
1414 return pc->is_a_completion;
1417 /**********************************************************************/
1418 /* Construction, deletion */
1419 /**********************************************************************/
1421 static CompletionState*
1422 cmpl_init_state (void)
1424 gchar getcwd_buf[2*MAXPATHLEN];
1425 CompletionState *new_state;
1427 new_state = g_new (CompletionState, 1);
1429 if (!getcwd (getcwd_buf, MAXPATHLEN))
1435 new_state->reference_dir = NULL;
1436 new_state->completion_dir = NULL;
1437 new_state->active_completion_dir = NULL;
1439 if ((new_state->user_home_dir = getenv("HOME")) != NULL)
1441 /* if this fails, get_pwdb will fill it in. */
1442 new_state->user_home_dir = g_strdup(new_state->user_home_dir);
1445 new_state->directory_storage = NULL;
1446 new_state->directory_sent_storage = NULL;
1447 new_state->last_valid_char = 0;
1448 new_state->updated_text = g_new (gchar, MAXPATHLEN);
1449 new_state->updated_text_alloc = MAXPATHLEN;
1450 new_state->the_completion.text = g_new (gchar, MAXPATHLEN);
1451 new_state->the_completion.text_alloc = MAXPATHLEN;
1452 new_state->user_dir_name_buffer = NULL;
1453 new_state->user_directories = NULL;
1455 new_state->reference_dir = open_dir (getcwd_buf, new_state);
1457 if (!new_state->reference_dir)
1464 cmpl_free_dir_list(GList* dp0)
1469 free_dir (dp->data);
1477 cmpl_free_dir_sent_list(GList* dp0)
1482 free_dir_sent (dp->data);
1490 cmpl_free_state (CompletionState* cmpl_state)
1492 cmpl_free_dir_list(cmpl_state->directory_storage);
1493 cmpl_free_dir_sent_list(cmpl_state->directory_sent_storage);
1495 if (cmpl_state->user_dir_name_buffer)
1496 g_free (cmpl_state->user_dir_name_buffer);
1497 if (cmpl_state->user_directories)
1498 g_free (cmpl_state->user_directories);
1499 if (cmpl_state->the_completion.text)
1500 g_free (cmpl_state->the_completion.text);
1501 if (cmpl_state->updated_text)
1502 g_free (cmpl_state->updated_text);
1504 g_free (cmpl_state);
1508 free_dir(CompletionDir* dir)
1510 g_free(dir->fullname);
1515 free_dir_sent(CompletionDirSent* sent)
1517 g_free(sent->name_buffer);
1518 g_free(sent->entries);
1523 prune_memory_usage(CompletionState *cmpl_state)
1525 GList* cdsl = cmpl_state->directory_sent_storage;
1526 GList* cdl = cmpl_state->directory_storage;
1530 for(; cdsl && len < CMPL_DIRECTORY_CACHE_SIZE; len += 1)
1534 cmpl_free_dir_sent_list(cdsl->next);
1538 cmpl_state->directory_storage = NULL;
1540 if (cdl->data == cmpl_state->reference_dir)
1541 cmpl_state->directory_storage = g_list_prepend(NULL, cdl->data);
1543 free_dir (cdl->data);
1550 /**********************************************************************/
1551 /* The main entrances. */
1552 /**********************************************************************/
1554 static PossibleCompletion*
1555 cmpl_completion_matches (gchar* text_to_complete,
1556 gchar** remaining_text,
1557 CompletionState* cmpl_state)
1560 PossibleCompletion *poss;
1562 prune_memory_usage(cmpl_state);
1564 g_assert(text_to_complete);
1566 cmpl_state->user_completion_index = -1;
1567 cmpl_state->last_completion_text = text_to_complete;
1568 cmpl_state->the_completion.text[0] = 0;
1569 cmpl_state->last_valid_char = 0;
1570 cmpl_state->updated_text_len = -1;
1571 cmpl_state->updated_text[0] = 0;
1572 cmpl_state->re_complete = FALSE;
1574 first_slash = strchr(text_to_complete, '/');
1576 if(text_to_complete[0] == '~' && !first_slash)
1578 /* Text starts with ~ and there is no slash, show all the
1579 * home directory completions.
1581 poss = attempt_homedir_completion(text_to_complete, cmpl_state);
1583 update_cmpl(poss, cmpl_state);
1588 cmpl_state->reference_dir =
1589 open_ref_dir(text_to_complete, remaining_text, cmpl_state);
1591 if(!cmpl_state->reference_dir)
1594 cmpl_state->completion_dir =
1595 find_completion_dir(*remaining_text, remaining_text, cmpl_state);
1597 cmpl_state->last_valid_char = *remaining_text - text_to_complete;
1599 if(!cmpl_state->completion_dir)
1602 cmpl_state->completion_dir->cmpl_index = -1;
1603 cmpl_state->completion_dir->cmpl_parent = NULL;
1604 cmpl_state->completion_dir->cmpl_text = *remaining_text;
1606 cmpl_state->active_completion_dir = cmpl_state->completion_dir;
1608 cmpl_state->reference_dir = cmpl_state->completion_dir;
1610 poss = attempt_file_completion(cmpl_state);
1612 update_cmpl(poss, cmpl_state);
1617 static PossibleCompletion*
1618 cmpl_next_completion (CompletionState* cmpl_state)
1620 PossibleCompletion* poss = NULL;
1622 cmpl_state->the_completion.text[0] = 0;
1624 if(cmpl_state->user_completion_index >= 0)
1625 poss = attempt_homedir_completion(cmpl_state->last_completion_text, cmpl_state);
1627 poss = attempt_file_completion(cmpl_state);
1629 update_cmpl(poss, cmpl_state);
1634 /**********************************************************************/
1635 /* Directory Operations */
1636 /**********************************************************************/
1638 /* Open the directory where completion will begin from, if possible. */
1639 static CompletionDir*
1640 open_ref_dir(gchar* text_to_complete,
1641 gchar** remaining_text,
1642 CompletionState* cmpl_state)
1645 CompletionDir *new_dir;
1647 first_slash = strchr(text_to_complete, '/');
1649 if (text_to_complete[0] == '/' || !cmpl_state->reference_dir)
1651 new_dir = open_dir("/", cmpl_state);
1654 *remaining_text = text_to_complete + 1;
1656 else if (text_to_complete[0] == '~')
1658 new_dir = open_user_dir(text_to_complete, cmpl_state);
1663 *remaining_text = first_slash + 1;
1665 *remaining_text = text_to_complete + strlen(text_to_complete);
1674 *remaining_text = text_to_complete;
1676 new_dir = open_dir(cmpl_state->reference_dir->fullname, cmpl_state);
1681 new_dir->cmpl_index = -1;
1682 new_dir->cmpl_parent = NULL;
1688 /* open a directory by user name */
1689 static CompletionDir*
1690 open_user_dir(gchar* text_to_complete,
1691 CompletionState *cmpl_state)
1696 g_assert(text_to_complete && text_to_complete[0] == '~');
1698 first_slash = strchr(text_to_complete, '/');
1701 cmp_len = first_slash - text_to_complete - 1;
1703 cmp_len = strlen(text_to_complete + 1);
1708 if (!cmpl_state->user_home_dir &&
1709 !get_pwdb(cmpl_state))
1711 return open_dir(cmpl_state->user_home_dir, cmpl_state);
1716 char* copy = g_new(char, cmp_len + 1);
1718 strncpy(copy, text_to_complete + 1, cmp_len);
1720 pwd = getpwnam(copy);
1728 return open_dir(pwd->pw_dir, cmpl_state);
1732 /* open a directory relative the the current relative directory */
1733 static CompletionDir*
1734 open_relative_dir(gchar* dir_name,
1736 CompletionState *cmpl_state)
1738 gchar path_buf[2*MAXPATHLEN];
1740 if(dir->fullname_len + strlen(dir_name) + 2 >= MAXPATHLEN)
1742 cmpl_errno = CMPL_ERRNO_TOO_LONG;
1746 strcpy(path_buf, dir->fullname);
1748 if(dir->fullname_len > 1)
1750 path_buf[dir->fullname_len] = '/';
1751 strcpy(path_buf + dir->fullname_len + 1, dir_name);
1755 strcpy(path_buf + dir->fullname_len, dir_name);
1758 return open_dir(path_buf, cmpl_state);
1761 /* after the cache lookup fails, really open a new directory */
1762 static CompletionDirSent*
1763 open_new_dir(gchar* dir_name, struct stat* sbuf)
1765 CompletionDirSent* sent;
1768 struct dirent *dirent_ptr;
1769 gint buffer_size = 0;
1770 gint entry_count = 0;
1772 struct stat ent_sbuf;
1773 char path_buf[MAXPATHLEN*2];
1776 sent = g_new(CompletionDirSent, 1);
1777 sent->mtime = sbuf->st_mtime;
1778 sent->inode = sbuf->st_ino;
1780 path_buf_len = strlen(dir_name);
1782 if (path_buf_len > MAXPATHLEN)
1784 cmpl_errno = CMPL_ERRNO_TOO_LONG;
1788 strcpy(path_buf, dir_name);
1790 directory = opendir(dir_name);
1798 while((dirent_ptr = readdir(directory)) != NULL)
1800 int entry_len = strlen(dirent_ptr->d_name);
1801 buffer_size += entry_len + 1;
1804 if(path_buf_len + entry_len + 2 >= MAXPATHLEN)
1806 cmpl_errno = CMPL_ERRNO_TOO_LONG;
1807 closedir(directory);
1812 sent->name_buffer = g_new(gchar, buffer_size);
1813 sent->entries = g_new(CompletionDirEntry, entry_count);
1814 sent->entry_count = entry_count;
1816 buffer_ptr = sent->name_buffer;
1818 rewinddir(directory);
1820 for(i = 0; i < entry_count; i += 1)
1822 dirent_ptr = readdir(directory);
1827 closedir(directory);
1831 strcpy(buffer_ptr, dirent_ptr->d_name);
1832 sent->entries[i].entry_name = buffer_ptr;
1833 buffer_ptr += strlen(dirent_ptr->d_name);
1837 path_buf[path_buf_len] = '/';
1838 strcpy(path_buf + path_buf_len + 1, dirent_ptr->d_name);
1840 if(stat(path_buf, &ent_sbuf) >= 0 && S_ISDIR(ent_sbuf.st_mode))
1841 sent->entries[i].is_dir = 1;
1843 /* stat may fail, and we don't mind, since it could be a
1844 * dangling symlink. */
1845 sent->entries[i].is_dir = 0;
1848 qsort(sent->entries, sent->entry_count, sizeof(CompletionDirEntry), compare_cmpl_dir);
1850 closedir(directory);
1855 /* open a directory by absolute pathname */
1856 static CompletionDir*
1857 open_dir(gchar* dir_name, CompletionState* cmpl_state)
1860 CompletionDirSent *sent;
1863 if(stat(dir_name, &sbuf) < 0)
1869 cdsl = cmpl_state->directory_sent_storage;
1875 if(sent->inode == sbuf.st_ino &&
1876 sent->mtime == sbuf.st_mtime)
1877 return attach_dir(sent, dir_name, cmpl_state);
1882 sent = open_new_dir(dir_name, &sbuf);
1885 cmpl_state->directory_sent_storage =
1886 g_list_prepend(cmpl_state->directory_sent_storage, sent);
1888 return attach_dir(sent, dir_name, cmpl_state);
1894 static CompletionDir*
1895 attach_dir(CompletionDirSent* sent, gchar* dir_name, CompletionState *cmpl_state)
1897 CompletionDir* new_dir;
1899 new_dir = g_new(CompletionDir, 1);
1901 cmpl_state->directory_storage =
1902 g_list_prepend(cmpl_state->directory_storage, new_dir);
1904 new_dir->sent = sent;
1905 new_dir->fullname = g_strdup(dir_name);
1906 new_dir->fullname_len = strlen(dir_name);
1912 correct_dir_fullname(CompletionDir* cmpl_dir)
1914 gint length = strlen(cmpl_dir->fullname);
1917 if (strcmp(cmpl_dir->fullname + length - 2, "/.") == 0)
1921 strcpy(cmpl_dir->fullname, "/");
1922 cmpl_dir->fullname_len = 1;
1925 cmpl_dir->fullname[length - 2] = 0;
1928 else if (strcmp(cmpl_dir->fullname + length - 3, "/./") == 0)
1929 cmpl_dir->fullname[length - 2] = 0;
1930 else if (strcmp(cmpl_dir->fullname + length - 3, "/..") == 0)
1934 strcpy(cmpl_dir->fullname, "/");
1935 cmpl_dir->fullname_len = 1;
1939 if(stat(cmpl_dir->fullname, &sbuf) < 0)
1945 cmpl_dir->fullname[length - 2] = 0;
1947 if(!correct_parent(cmpl_dir, &sbuf))
1950 else if (strcmp(cmpl_dir->fullname + length - 4, "/../") == 0)
1954 strcpy(cmpl_dir->fullname, "/");
1955 cmpl_dir->fullname_len = 1;
1959 if(stat(cmpl_dir->fullname, &sbuf) < 0)
1965 cmpl_dir->fullname[length - 3] = 0;
1967 if(!correct_parent(cmpl_dir, &sbuf))
1971 cmpl_dir->fullname_len = strlen(cmpl_dir->fullname);
1977 correct_parent(CompletionDir* cmpl_dir, struct stat *sbuf)
1984 last_slash = strrchr(cmpl_dir->fullname, '/');
1986 g_assert(last_slash);
1988 if(last_slash != cmpl_dir->fullname)
1989 { /* last_slash[0] = 0; */ }
1996 if (stat(cmpl_dir->fullname, &parbuf) < 0)
2002 if (parbuf.st_ino == sbuf->st_ino && parbuf.st_dev == sbuf->st_dev)
2003 /* it wasn't a link */
2009 last_slash[0] = '/'; */
2011 /* it was a link, have to figure it out the hard way */
2013 new_name = find_parent_dir_fullname(cmpl_dir->fullname);
2018 g_free(cmpl_dir->fullname);
2020 cmpl_dir->fullname = new_name;
2026 find_parent_dir_fullname(gchar* dirname)
2028 gchar buffer[MAXPATHLEN];
2029 gchar buffer2[MAXPATHLEN];
2031 if(!getcwd(buffer, MAXPATHLEN))
2037 if(chdir(dirname) != 0 || chdir("..") != 0)
2043 if(!getcwd(buffer2, MAXPATHLEN))
2051 if(chdir(buffer) != 0)
2057 return g_strdup(buffer2);
2060 /**********************************************************************/
2061 /* Completion Operations */
2062 /**********************************************************************/
2064 static PossibleCompletion*
2065 attempt_homedir_completion(gchar* text_to_complete,
2066 CompletionState *cmpl_state)
2070 if (!cmpl_state->user_dir_name_buffer &&
2071 !get_pwdb(cmpl_state))
2073 length = strlen(text_to_complete) - 1;
2075 cmpl_state->user_completion_index += 1;
2077 while(cmpl_state->user_completion_index < cmpl_state->user_directories_len)
2079 index = first_diff_index(text_to_complete + 1,
2080 cmpl_state->user_directories
2081 [cmpl_state->user_completion_index].login);
2088 if(cmpl_state->last_valid_char < (index + 1))
2089 cmpl_state->last_valid_char = index + 1;
2090 cmpl_state->user_completion_index += 1;
2094 cmpl_state->the_completion.is_a_completion = 1;
2095 cmpl_state->the_completion.is_directory = 1;
2097 append_completion_text("~", cmpl_state);
2099 append_completion_text(cmpl_state->
2100 user_directories[cmpl_state->user_completion_index].login,
2103 return append_completion_text("/", cmpl_state);
2106 if(text_to_complete[1] ||
2107 cmpl_state->user_completion_index > cmpl_state->user_directories_len)
2109 cmpl_state->user_completion_index = -1;
2114 cmpl_state->user_completion_index += 1;
2115 cmpl_state->the_completion.is_a_completion = 1;
2116 cmpl_state->the_completion.is_directory = 1;
2118 return append_completion_text("~/", cmpl_state);
2122 /* returns the index (>= 0) of the first differing character,
2123 * PATTERN_MATCH if the completion matches */
2125 first_diff_index(gchar* pat, gchar* text)
2129 while(*pat && *text && *text == *pat)
2139 return PATTERN_MATCH;
2142 static PossibleCompletion*
2143 append_completion_text(gchar* text, CompletionState* cmpl_state)
2147 if(!cmpl_state->the_completion.text)
2150 len = strlen(text) + strlen(cmpl_state->the_completion.text) + 1;
2152 if(cmpl_state->the_completion.text_alloc > len)
2154 strcat(cmpl_state->the_completion.text, text);
2155 return &cmpl_state->the_completion;
2158 while(i < len) { i <<= 1; }
2160 cmpl_state->the_completion.text_alloc = i;
2162 cmpl_state->the_completion.text = (gchar*)g_realloc(cmpl_state->the_completion.text, i);
2164 if(!cmpl_state->the_completion.text)
2168 strcat(cmpl_state->the_completion.text, text);
2169 return &cmpl_state->the_completion;
2173 static CompletionDir*
2174 find_completion_dir(gchar* text_to_complete,
2175 gchar** remaining_text,
2176 CompletionState* cmpl_state)
2178 gchar* first_slash = strchr(text_to_complete, '/');
2179 CompletionDir* dir = cmpl_state->reference_dir;
2180 *remaining_text = text_to_complete;
2184 gint len = first_slash - *remaining_text;
2186 gint found_index = -1;
2188 gchar* pat_buf = g_new (gchar, len + 1);
2190 strncpy(pat_buf, *remaining_text, len);
2193 for(i = 0; i < dir->sent->entry_count; i += 1)
2195 if(dir->sent->entries[i].is_dir &&
2196 fnmatch(pat_buf, dir->sent->entries[i].entry_name,
2197 FNMATCH_FLAGS)!= FNM_NOMATCH)
2214 CompletionDir* next = open_relative_dir(dir->sent->entries[found_index].entry_name,
2223 next->cmpl_parent = dir;
2227 if(!correct_dir_fullname(dir))
2233 *remaining_text = first_slash + 1;
2234 first_slash = strchr(*remaining_text, '/');
2249 update_cmpl(PossibleCompletion* poss, CompletionState* cmpl_state)
2253 if(!poss || !cmpl_is_a_completion(poss))
2256 cmpl_len = strlen(cmpl_this_completion(poss));
2258 if(cmpl_state->updated_text_alloc < cmpl_len + 1)
2260 cmpl_state->updated_text =
2261 (gchar*)g_realloc(cmpl_state->updated_text,
2262 cmpl_state->updated_text_alloc);
2263 cmpl_state->updated_text_alloc = 2*cmpl_len;
2266 if(cmpl_state->updated_text_len < 0)
2268 strcpy(cmpl_state->updated_text, cmpl_this_completion(poss));
2269 cmpl_state->updated_text_len = cmpl_len;
2270 cmpl_state->re_complete = cmpl_is_directory(poss);
2272 else if(cmpl_state->updated_text_len == 0)
2274 cmpl_state->re_complete = FALSE;
2279 first_diff_index(cmpl_state->updated_text,
2280 cmpl_this_completion(poss));
2282 cmpl_state->re_complete = FALSE;
2284 if(first_diff == PATTERN_MATCH)
2287 if(first_diff > cmpl_state->updated_text_len)
2288 strcpy(cmpl_state->updated_text, cmpl_this_completion(poss));
2290 cmpl_state->updated_text_len = first_diff;
2291 cmpl_state->updated_text[first_diff] = 0;
2295 static PossibleCompletion*
2296 attempt_file_completion(CompletionState *cmpl_state)
2298 gchar *pat_buf, *first_slash;
2299 CompletionDir *dir = cmpl_state->active_completion_dir;
2301 dir->cmpl_index += 1;
2303 if(dir->cmpl_index == dir->sent->entry_count)
2305 if(dir->cmpl_parent == NULL)
2307 cmpl_state->active_completion_dir = NULL;
2313 cmpl_state->active_completion_dir = dir->cmpl_parent;
2315 return attempt_file_completion(cmpl_state);
2319 g_assert(dir->cmpl_text);
2321 first_slash = strchr(dir->cmpl_text, '/');
2325 gint len = first_slash - dir->cmpl_text;
2327 pat_buf = g_new (gchar, len + 1);
2328 strncpy(pat_buf, dir->cmpl_text, len);
2333 gint len = strlen(dir->cmpl_text);
2335 pat_buf = g_new (gchar, len + 2);
2336 strcpy(pat_buf, dir->cmpl_text);
2337 strcpy(pat_buf + len, "*");
2342 if(dir->sent->entries[dir->cmpl_index].is_dir)
2344 if(fnmatch(pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
2345 FNMATCH_FLAGS) != FNM_NOMATCH)
2347 CompletionDir* new_dir;
2349 new_dir = open_relative_dir(dir->sent->entries[dir->cmpl_index].entry_name,
2358 new_dir->cmpl_parent = dir;
2360 new_dir->cmpl_index = -1;
2361 new_dir->cmpl_text = first_slash + 1;
2363 cmpl_state->active_completion_dir = new_dir;
2366 return attempt_file_completion(cmpl_state);
2371 return attempt_file_completion(cmpl_state);
2377 return attempt_file_completion(cmpl_state);
2382 if(dir->cmpl_parent != NULL)
2384 append_completion_text(dir->fullname +
2385 strlen(cmpl_state->completion_dir->fullname) + 1,
2387 append_completion_text("/", cmpl_state);
2390 append_completion_text(dir->sent->entries[dir->cmpl_index].entry_name, cmpl_state);
2392 cmpl_state->the_completion.is_a_completion =
2393 (fnmatch(pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
2394 FNMATCH_FLAGS) != FNM_NOMATCH);
2396 cmpl_state->the_completion.is_directory = dir->sent->entries[dir->cmpl_index].is_dir;
2397 if(dir->sent->entries[dir->cmpl_index].is_dir)
2398 append_completion_text("/", cmpl_state);
2401 return &cmpl_state->the_completion;
2407 get_pwdb(CompletionState* cmpl_state)
2409 struct passwd *pwd_ptr;
2410 gchar* buf_ptr, *home_dir = NULL;
2411 gint len = 0, i, count = 0;
2413 if(cmpl_state->user_dir_name_buffer)
2417 while ((pwd_ptr = getpwent()) != NULL)
2419 len += strlen(pwd_ptr->pw_name);
2420 len += strlen(pwd_ptr->pw_dir);
2425 if (!cmpl_state->user_home_dir)
2427 /* the loser doesn't have $HOME set */
2430 pwd_ptr = getpwuid(getuid());
2436 home_dir = pwd_ptr->pw_dir;
2438 len += strlen(home_dir);
2444 cmpl_state->user_dir_name_buffer = g_new(gchar, len);
2445 cmpl_state->user_directories = g_new(CompletionUserDir, count);
2446 cmpl_state->user_directories_len = count;
2448 buf_ptr = cmpl_state->user_dir_name_buffer;
2450 if (!cmpl_state->user_home_dir)
2452 strcpy(buf_ptr, home_dir);
2453 cmpl_state->user_home_dir = buf_ptr;
2454 buf_ptr += strlen(buf_ptr);
2458 for(i = 0; i < count; i += 1)
2460 pwd_ptr = getpwent();
2467 strcpy(buf_ptr, pwd_ptr->pw_name);
2468 cmpl_state->user_directories[i].login = buf_ptr;
2469 buf_ptr += strlen(buf_ptr);
2471 strcpy(buf_ptr, pwd_ptr->pw_dir);
2472 cmpl_state->user_directories[i].homedir = buf_ptr;
2473 buf_ptr += strlen(buf_ptr);
2477 qsort(cmpl_state->user_directories,
2478 cmpl_state->user_directories_len,
2479 sizeof(CompletionUserDir),
2488 if(cmpl_state->user_dir_name_buffer)
2489 g_free(cmpl_state->user_dir_name_buffer);
2490 if(cmpl_state->user_directories)
2491 g_free(cmpl_state->user_directories);
2493 cmpl_state->user_dir_name_buffer = NULL;
2494 cmpl_state->user_directories = NULL;
2500 compare_user_dir(const void* a, const void* b)
2502 return strcmp((((CompletionUserDir*)a))->login,
2503 (((CompletionUserDir*)b))->login);
2507 compare_cmpl_dir(const void* a, const void* b)
2509 return strcmp((((CompletionDirEntry*)a))->entry_name,
2510 (((CompletionDirEntry*)b))->entry_name);
2514 cmpl_state_okay(CompletionState* cmpl_state)
2516 return cmpl_state && cmpl_state->reference_dir;
2520 cmpl_strerror(gint err)
2522 if(err == CMPL_ERRNO_TOO_LONG)
2523 return "Name too long";
2525 return g_strerror (err);