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.
20 #include <sys/types.h>
22 #include <sys/param.h>
31 #include "gdk/gdkkeysyms.h"
32 #include "gtkbutton.h"
34 #include "gtkfilesel.h"
39 #include "gtklistitem.h"
41 #include "gtkscrolledwindow.h"
42 #include "gtksignal.h"
45 #include "gtkmenuitem.h"
46 #include "gtkoptionmenu.h"
48 #include "gtkdialog.h"
50 #define DIR_LIST_WIDTH 180
51 #define DIR_LIST_HEIGHT 180
52 #define FILE_LIST_WIDTH 180
53 #define FILE_LIST_HEIGHT 180
55 /* I've put this here so it doesn't get confused with the
56 * file completion interface */
57 typedef struct _HistoryCallbackArg HistoryCallbackArg;
59 struct _HistoryCallbackArg
66 typedef struct _CompletionState CompletionState;
67 typedef struct _CompletionDir CompletionDir;
68 typedef struct _CompletionDirSent CompletionDirSent;
69 typedef struct _CompletionDirEntry CompletionDirEntry;
70 typedef struct _CompletionUserDir CompletionUserDir;
71 typedef struct _PossibleCompletion PossibleCompletion;
73 /* Non-external file completion decls and structures */
75 /* A contant telling PRCS how many directories to cache. Its actually
76 * kept in a list, so the geometry isn't important. */
77 #define CMPL_DIRECTORY_CACHE_SIZE 10
79 /* A constant used to determine whether a substring was an exact
80 * match by first_diff_index()
82 #define PATTERN_MATCH -1
83 /* The arguments used by all fnmatch() calls below
85 #define FNMATCH_FLAGS (FNM_PATHNAME | FNM_PERIOD)
87 #define CMPL_ERRNO_TOO_LONG ((1<<16)-1)
89 /* This structure contains all the useful information about a directory
90 * for the purposes of filename completion. These structures are cached
91 * in the CompletionState struct. CompletionDir's are reference counted.
93 struct _CompletionDirSent
100 gchar *name_buffer; /* memory segment containing names of all entries */
102 struct _CompletionDirEntry *entries;
105 struct _CompletionDir
107 CompletionDirSent *sent;
112 struct _CompletionDir *cmpl_parent;
117 /* This structure contains pairs of directory entry names with a flag saying
118 * whether or not they are a valid directory. NOTE: This information is used
119 * to provide the caller with information about whether to update its completions
120 * or try to open a file. Since directories are cached by the directory mtime,
121 * a symlink which points to an invalid file (which will not be a directory),
122 * will not be reevaluated if that file is created, unless the containing
123 * directory is touched. I consider this case to be worth ignoring (josh).
125 struct _CompletionDirEntry
131 struct _CompletionUserDir
137 struct _PossibleCompletion
139 /* accessible fields, all are accessed externally by functions
143 gint is_a_completion;
151 struct _CompletionState
153 gint last_valid_char;
155 gint updated_text_len;
156 gint updated_text_alloc;
159 gchar *user_dir_name_buffer;
160 gint user_directories_len;
161 gchar *user_home_dir;
163 gchar *last_completion_text;
165 gint user_completion_index; /* if >= 0, currently completing ~user */
167 struct _CompletionDir *completion_dir; /* directory completing from */
168 struct _CompletionDir *active_completion_dir;
170 struct _PossibleCompletion the_completion;
172 struct _CompletionDir *reference_dir; /* initial directory */
174 GList* directory_storage;
175 GList* directory_sent_storage;
177 struct _CompletionUserDir *user_directories;
181 /* File completion functions which would be external, were they used
182 * outside of this file.
185 static CompletionState* cmpl_init_state (void);
186 static void cmpl_free_state (CompletionState *cmpl_state);
187 static gint cmpl_state_okay (CompletionState* cmpl_state);
188 static gchar* cmpl_strerror (gint);
190 static PossibleCompletion* cmpl_completion_matches(gchar *text_to_complete,
191 gchar **remaining_text,
192 CompletionState *cmpl_state);
194 /* Returns a name for consideration, possibly a completion, this name
195 * will be invalid after the next call to cmpl_next_completion.
197 static char* cmpl_this_completion (PossibleCompletion*);
199 /* True if this completion matches the given text. Otherwise, this
200 * output can be used to have a list of non-completions.
202 static gint cmpl_is_a_completion (PossibleCompletion*);
204 /* True if the completion is a directory
206 static gint cmpl_is_directory (PossibleCompletion*);
208 /* Obtains the next completion, or NULL
210 static PossibleCompletion* cmpl_next_completion (CompletionState*);
212 /* Updating completions: the return value of cmpl_updated_text() will
213 * be text_to_complete completed as much as possible after the most
214 * recent call to cmpl_completion_matches. For the present
215 * application, this is the suggested replacement for the user's input
216 * string. You must CALL THIS AFTER ALL cmpl_text_completions have
219 static gchar* cmpl_updated_text (CompletionState* cmpl_state);
221 /* After updating, to see if the completion was a directory, call
222 * this. If it was, you should consider re-calling completion_matches.
224 static gint cmpl_updated_dir (CompletionState* cmpl_state);
226 /* Current location: if using file completion, return the current
227 * directory, from which file completion begins. More specifically,
228 * the cwd concatenated with all exact completions up to the last
229 * directory delimiter('/').
231 static gchar* cmpl_reference_position (CompletionState* cmpl_state);
233 /* backing up: if cmpl_completion_matches returns NULL, you may query
234 * the index of the last completable character into cmpl_updated_text.
236 static gint cmpl_last_valid_char (CompletionState* cmpl_state);
238 /* When the user selects a non-directory, call cmpl_completion_fullname
239 * to get the full name of the selected file.
241 static gchar* cmpl_completion_fullname (gchar*, CompletionState* cmpl_state);
244 /* Directory operations. */
245 static CompletionDir* open_ref_dir (gchar* text_to_complete,
246 gchar** remaining_text,
247 CompletionState* cmpl_state);
248 static CompletionDir* open_dir (gchar* dir_name,
249 CompletionState* cmpl_state);
250 static CompletionDir* open_user_dir (gchar* text_to_complete,
251 CompletionState *cmpl_state);
252 static CompletionDir* open_relative_dir (gchar* dir_name, CompletionDir* dir,
253 CompletionState *cmpl_state);
254 static CompletionDirSent* open_new_dir (gchar* dir_name, struct stat* sbuf);
255 static gint correct_dir_fullname (CompletionDir* cmpl_dir);
256 static gint correct_parent (CompletionDir* cmpl_dir,
258 static gchar* find_parent_dir_fullname (gchar* dirname);
259 static CompletionDir* attach_dir (CompletionDirSent* sent,
261 CompletionState *cmpl_state);
262 static void free_dir_sent (CompletionDirSent* sent);
263 static void free_dir (CompletionDir *dir);
264 static void prune_memory_usage(CompletionState *cmpl_state);
266 /* Completion operations */
267 static PossibleCompletion* attempt_homedir_completion(gchar* text_to_complete,
268 CompletionState *cmpl_state);
269 static PossibleCompletion* attempt_file_completion(CompletionState *cmpl_state);
270 static CompletionDir* find_completion_dir(gchar* text_to_complete,
271 gchar** remaining_text,
272 CompletionState* cmpl_state);
273 static PossibleCompletion* append_completion_text(gchar* text,
274 CompletionState* cmpl_state);
275 static gint get_pwdb(CompletionState* cmpl_state);
276 static gint first_diff_index(gchar* pat, gchar* text);
277 static gint compare_user_dir(const void* a, const void* b);
278 static gint compare_cmpl_dir(const void* a, const void* b);
279 static void update_cmpl(PossibleCompletion* poss,
280 CompletionState* cmpl_state);
282 static void gtk_file_selection_class_init (GtkFileSelectionClass *klass);
283 static void gtk_file_selection_init (GtkFileSelection *filesel);
284 static void gtk_file_selection_destroy (GtkObject *object);
285 static gint gtk_file_selection_key_press (GtkWidget *widget,
289 static void gtk_file_selection_file_button (GtkWidget *widget,
292 GdkEventButton *bevent,
295 static void gtk_file_selection_dir_button (GtkWidget *widget,
298 GdkEventButton *bevent,
301 static void gtk_file_selection_populate (GtkFileSelection *fs,
304 static void gtk_file_selection_abort (GtkFileSelection *fs);
306 static void gtk_file_selection_update_history_menu (GtkFileSelection *fs,
309 static void gtk_file_selection_create_dir (GtkWidget *widget, gpointer data);
310 static void gtk_file_selection_delete_file (GtkWidget *widget, gpointer data);
311 static void gtk_file_selection_rename_file (GtkWidget *widget, gpointer data);
315 static GtkWindowClass *parent_class = NULL;
317 /* Saves errno when something cmpl does fails. */
318 static gint cmpl_errno;
321 gtk_file_selection_get_type (void)
323 static guint file_selection_type = 0;
325 if (!file_selection_type)
327 GtkTypeInfo filesel_info =
330 sizeof (GtkFileSelection),
331 sizeof (GtkFileSelectionClass),
332 (GtkClassInitFunc) gtk_file_selection_class_init,
333 (GtkObjectInitFunc) gtk_file_selection_init,
334 /* reserved_1 */ NULL,
335 /* reserved_2 */ NULL,
336 (GtkClassInitFunc) NULL,
339 file_selection_type = gtk_type_unique (gtk_window_get_type (), &filesel_info);
342 return file_selection_type;
346 gtk_file_selection_class_init (GtkFileSelectionClass *class)
348 GtkObjectClass *object_class;
350 object_class = (GtkObjectClass*) class;
352 parent_class = gtk_type_class (gtk_window_get_type ());
354 object_class->destroy = gtk_file_selection_destroy;
358 gtk_file_selection_init (GtkFileSelection *filesel)
360 GtkWidget *entry_vbox;
362 GtkWidget *list_hbox;
363 GtkWidget *confirm_area;
364 GtkWidget *pulldown_hbox;
365 char *dir_title [] = { "Directories", };
366 char *file_title [] = { "Files", };
368 filesel->cmpl_state = cmpl_init_state ();
370 /* The dialog-sized vertical box */
371 filesel->main_vbox = gtk_vbox_new (FALSE, 10);
372 gtk_container_border_width (GTK_CONTAINER (filesel), 10);
373 gtk_container_add (GTK_CONTAINER (filesel), filesel->main_vbox);
374 gtk_widget_show (filesel->main_vbox);
376 /* The horizontal box containing create, rename etc. buttons */
377 filesel->button_area = gtk_hbutton_box_new ();
378 gtk_button_box_set_layout(GTK_BUTTON_BOX(filesel->button_area), GTK_BUTTONBOX_START);
379 gtk_button_box_set_spacing(GTK_BUTTON_BOX(filesel->button_area), 0);
380 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->button_area,
382 gtk_widget_show (filesel->button_area);
384 gtk_file_selection_show_fileop_buttons(filesel);
386 /* hbox for pulldown menu */
387 pulldown_hbox = gtk_hbox_new (TRUE, 5);
388 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), pulldown_hbox, FALSE, FALSE, 0);
389 gtk_widget_show (pulldown_hbox);
392 filesel->history_pulldown = gtk_option_menu_new ();
393 gtk_widget_show (filesel->history_pulldown);
394 gtk_box_pack_start (GTK_BOX (pulldown_hbox), filesel->history_pulldown,
397 /* The horizontal box containing the directory and file listboxes */
398 list_hbox = gtk_hbox_new (FALSE, 5);
399 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), list_hbox, TRUE, TRUE, 0);
400 gtk_widget_show (list_hbox);
402 /* The directories clist */
403 filesel->dir_list = gtk_clist_new_with_titles (1, dir_title);
404 gtk_widget_set_usize (filesel->dir_list, DIR_LIST_WIDTH, DIR_LIST_HEIGHT);
405 gtk_signal_connect (GTK_OBJECT (filesel->dir_list), "select_row",
406 (GtkSignalFunc) gtk_file_selection_dir_button,
408 gtk_clist_set_policy (GTK_CLIST (filesel->dir_list), GTK_POLICY_ALWAYS, GTK_POLICY_AUTOMATIC);
409 gtk_clist_column_titles_passive (GTK_CLIST (filesel->dir_list));
410 gtk_container_border_width (GTK_CONTAINER (filesel->dir_list), 5);
411 gtk_box_pack_start (GTK_BOX (list_hbox), filesel->dir_list, TRUE, TRUE, 0);
412 gtk_widget_show (filesel->dir_list);
414 /* The files clist */
415 filesel->file_list = gtk_clist_new_with_titles (1, file_title);
416 gtk_widget_set_usize (filesel->file_list, FILE_LIST_WIDTH, FILE_LIST_HEIGHT);
417 gtk_signal_connect (GTK_OBJECT (filesel->file_list), "select_row",
418 (GtkSignalFunc) gtk_file_selection_file_button,
420 gtk_clist_set_policy (GTK_CLIST (filesel->file_list), GTK_POLICY_ALWAYS, GTK_POLICY_AUTOMATIC);
421 gtk_clist_column_titles_passive (GTK_CLIST (filesel->file_list));
422 gtk_container_border_width (GTK_CONTAINER (filesel->file_list), 5);
423 gtk_box_pack_start (GTK_BOX (list_hbox), filesel->file_list, TRUE, TRUE, 0);
424 gtk_widget_show (filesel->file_list);
426 /* action area for packing buttons into. */
427 filesel->action_area = gtk_hbox_new (TRUE, 0);
428 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->action_area,
430 gtk_widget_show (filesel->action_area);
432 /* The OK/Cancel button area */
433 confirm_area = gtk_hbutton_box_new ();
434 gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area), GTK_BUTTONBOX_END);
435 gtk_button_box_set_spacing(GTK_BUTTON_BOX(confirm_area), 5);
436 gtk_box_pack_end (GTK_BOX (filesel->main_vbox), confirm_area, FALSE, FALSE, 0);
437 gtk_widget_show (confirm_area);
440 filesel->ok_button = gtk_button_new_with_label ("OK");
441 GTK_WIDGET_SET_FLAGS (filesel->ok_button, GTK_CAN_DEFAULT);
442 gtk_box_pack_start (GTK_BOX (confirm_area), filesel->ok_button, TRUE, TRUE, 0);
443 gtk_widget_grab_default (filesel->ok_button);
444 gtk_widget_show (filesel->ok_button);
446 /* The Cancel button */
447 filesel->cancel_button = gtk_button_new_with_label ("Cancel");
448 GTK_WIDGET_SET_FLAGS (filesel->cancel_button, GTK_CAN_DEFAULT);
449 gtk_box_pack_start (GTK_BOX (confirm_area), filesel->cancel_button, TRUE, TRUE, 0);
450 gtk_widget_show (filesel->cancel_button);
452 /* The selection entry widget */
453 entry_vbox = gtk_vbox_new (FALSE, 2);
454 gtk_box_pack_end (GTK_BOX (filesel->main_vbox), entry_vbox, FALSE, FALSE, 0);
455 gtk_widget_show (entry_vbox);
457 filesel->selection_text = label = gtk_label_new ("");
458 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
459 gtk_box_pack_start (GTK_BOX (entry_vbox), label, FALSE, FALSE, 0);
460 gtk_widget_show (label);
462 filesel->selection_entry = gtk_entry_new ();
463 gtk_signal_connect (GTK_OBJECT (filesel->selection_entry), "key_press_event",
464 (GtkSignalFunc) gtk_file_selection_key_press, filesel);
465 gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "focus_in_event",
466 (GtkSignalFunc) gtk_widget_grab_default,
467 GTK_OBJECT (filesel->ok_button));
468 gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "activate",
469 (GtkSignalFunc) gtk_button_clicked,
470 GTK_OBJECT (filesel->ok_button));
471 gtk_box_pack_start (GTK_BOX (entry_vbox), filesel->selection_entry, TRUE, TRUE, 0);
472 gtk_widget_show (filesel->selection_entry);
474 if (!cmpl_state_okay (filesel->cmpl_state))
478 sprintf (err_buf, "Directory unreadable: %s", cmpl_strerror (cmpl_errno));
480 gtk_label_set (GTK_LABEL (filesel->selection_text), err_buf);
484 gtk_file_selection_populate (filesel, "", FALSE);
487 gtk_widget_grab_focus (filesel->selection_entry);
491 gtk_file_selection_new (const gchar *title)
493 GtkFileSelection *filesel;
495 filesel = gtk_type_new (gtk_file_selection_get_type ());
496 gtk_window_set_title (GTK_WINDOW (filesel), title);
498 return GTK_WIDGET (filesel);
502 gtk_file_selection_show_fileop_buttons (GtkFileSelection *filesel)
504 g_return_if_fail (filesel != NULL);
505 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
507 /* delete, create directory, and rename */
508 if (!filesel->fileop_c_dir)
510 filesel->fileop_c_dir = gtk_button_new_with_label ("Create Dir");
511 gtk_signal_connect (GTK_OBJECT (filesel->fileop_c_dir), "clicked",
512 (GtkSignalFunc) gtk_file_selection_create_dir,
514 gtk_box_pack_start (GTK_BOX (filesel->button_area),
515 filesel->fileop_c_dir, TRUE, TRUE, 0);
516 gtk_widget_show (filesel->fileop_c_dir);
519 if (!filesel->fileop_del_file)
521 filesel->fileop_del_file = gtk_button_new_with_label ("Delete File");
522 gtk_signal_connect (GTK_OBJECT (filesel->fileop_del_file), "clicked",
523 (GtkSignalFunc) gtk_file_selection_delete_file,
525 gtk_box_pack_start (GTK_BOX (filesel->button_area),
526 filesel->fileop_del_file, TRUE, TRUE, 0);
527 gtk_widget_show (filesel->fileop_del_file);
530 if (!filesel->fileop_ren_file)
532 filesel->fileop_ren_file = gtk_button_new_with_label ("Rename File");
533 gtk_signal_connect (GTK_OBJECT (filesel->fileop_ren_file), "clicked",
534 (GtkSignalFunc) gtk_file_selection_rename_file,
536 gtk_box_pack_start (GTK_BOX (filesel->button_area),
537 filesel->fileop_ren_file, TRUE, TRUE, 0);
538 gtk_widget_show (filesel->fileop_ren_file);
541 gtk_widget_queue_resize(GTK_WIDGET(filesel));
545 gtk_file_selection_hide_fileop_buttons (GtkFileSelection *filesel)
547 g_return_if_fail (filesel != NULL);
548 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
550 if (filesel->fileop_ren_file)
552 gtk_widget_destroy (filesel->fileop_ren_file);
553 filesel->fileop_ren_file = NULL;
556 if (filesel->fileop_del_file)
558 gtk_widget_destroy (filesel->fileop_del_file);
559 filesel->fileop_del_file = NULL;
562 if (filesel->fileop_c_dir)
564 gtk_widget_destroy (filesel->fileop_c_dir);
565 filesel->fileop_c_dir = NULL;
572 gtk_file_selection_set_filename (GtkFileSelection *filesel,
573 const gchar *filename)
575 char buf[MAXPATHLEN];
576 const char *name, *last_slash;
578 g_return_if_fail (filesel != NULL);
579 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
580 g_return_if_fail (filename != NULL);
582 last_slash = strrchr (filename, '/');
591 gint len = MIN (MAXPATHLEN - 1, last_slash - filename + 1);
593 strncpy (buf, filename, len);
596 name = last_slash + 1;
599 gtk_file_selection_populate (filesel, buf, FALSE);
601 if (filesel->selection_entry)
602 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), name);
606 gtk_file_selection_get_filename (GtkFileSelection *filesel)
608 static char nothing[2] = "";
612 g_return_val_if_fail (filesel != NULL, nothing);
613 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), nothing);
615 text = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
618 filename = cmpl_completion_fullname (text, filesel->cmpl_state);
626 gtk_file_selection_destroy (GtkObject *object)
628 GtkFileSelection *filesel;
630 HistoryCallbackArg *callback_arg;
632 g_return_if_fail (object != NULL);
633 g_return_if_fail (GTK_IS_FILE_SELECTION (object));
635 filesel = GTK_FILE_SELECTION (object);
637 if (filesel->fileop_dialog)
638 gtk_widget_destroy (filesel->fileop_dialog);
640 if (filesel->history_list)
642 list = filesel->history_list;
645 callback_arg = list->data;
646 g_free (callback_arg->directory);
647 g_free (callback_arg);
650 g_list_free (filesel->history_list);
651 filesel->history_list = NULL;
654 cmpl_free_state (filesel->cmpl_state);
655 filesel->cmpl_state = NULL;
657 if (GTK_OBJECT_CLASS (parent_class)->destroy)
658 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
661 /* Begin file operations callbacks */
664 gtk_file_selection_fileop_error (gchar *error_message)
671 g_return_if_fail (error_message != NULL);
674 dialog = gtk_dialog_new ();
676 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
677 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
680 gtk_window_set_title (GTK_WINDOW (dialog), "Error");
681 gtk_window_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
683 vbox = gtk_vbox_new(FALSE, 0);
684 gtk_container_border_width(GTK_CONTAINER(vbox), 8);
685 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
687 gtk_widget_show(vbox);
689 label = gtk_label_new(error_message);
690 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
691 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
692 gtk_widget_show(label);
694 /* yes, we free it */
695 g_free (error_message);
698 button = gtk_button_new_with_label ("Close");
699 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
700 (GtkSignalFunc) gtk_widget_destroy,
702 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
703 button, TRUE, TRUE, 0);
704 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
705 gtk_widget_grab_default(button);
706 gtk_widget_show (button);
708 gtk_widget_show (dialog);
712 gtk_file_selection_fileop_destroy (GtkWidget *widget, gpointer data)
714 GtkFileSelection *fs = data;
716 g_return_if_fail (fs != NULL);
717 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
719 fs->fileop_dialog = NULL;
724 gtk_file_selection_create_dir_confirmed (GtkWidget *widget, gpointer data)
726 GtkFileSelection *fs = data;
731 CompletionState *cmpl_state;
733 g_return_if_fail (fs != NULL);
734 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
736 dirname = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
737 cmpl_state = (CompletionState*) fs->cmpl_state;
738 path = cmpl_reference_position (cmpl_state);
740 full_path = g_strconcat (path, "/", dirname, NULL);
741 if ( (mkdir (full_path, 0755) < 0) )
743 buf = g_strconcat ("Error creating directory \"", dirname, "\": ",
744 g_strerror(errno), NULL);
745 gtk_file_selection_fileop_error (buf);
749 gtk_widget_destroy (fs->fileop_dialog);
750 gtk_file_selection_populate (fs, "", FALSE);
754 gtk_file_selection_create_dir (GtkWidget *widget, gpointer data)
756 GtkFileSelection *fs = data;
762 g_return_if_fail (fs != NULL);
763 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
765 if (fs->fileop_dialog)
769 fs->fileop_dialog = dialog = gtk_dialog_new ();
770 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
771 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
773 gtk_window_set_title (GTK_WINDOW (dialog), "Create Directory");
774 gtk_window_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
776 /* If file dialog is grabbed, grab option dialog */
777 /* When option dialog is closed, file dialog will be grabbed again */
778 if (GTK_WINDOW(fs)->modal)
779 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
781 vbox = gtk_vbox_new(FALSE, 0);
782 gtk_container_border_width(GTK_CONTAINER(vbox), 8);
783 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
785 gtk_widget_show(vbox);
787 label = gtk_label_new("Directory name:");
788 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
789 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
790 gtk_widget_show(label);
792 /* The directory entry widget */
793 fs->fileop_entry = gtk_entry_new ();
794 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
796 GTK_WIDGET_SET_FLAGS(fs->fileop_entry, GTK_CAN_DEFAULT);
797 gtk_widget_show (fs->fileop_entry);
800 button = gtk_button_new_with_label ("Create");
801 gtk_signal_connect (GTK_OBJECT (button), "clicked",
802 (GtkSignalFunc) gtk_file_selection_create_dir_confirmed,
804 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
805 button, TRUE, TRUE, 0);
806 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
807 gtk_widget_show(button);
809 button = gtk_button_new_with_label ("Cancel");
810 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
811 (GtkSignalFunc) gtk_widget_destroy,
813 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
814 button, TRUE, TRUE, 0);
815 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
816 gtk_widget_grab_default(button);
817 gtk_widget_show (button);
819 gtk_widget_show (dialog);
823 gtk_file_selection_delete_file_confirmed (GtkWidget *widget, gpointer data)
825 GtkFileSelection *fs = data;
826 CompletionState *cmpl_state;
831 g_return_if_fail (fs != NULL);
832 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
834 cmpl_state = (CompletionState*) fs->cmpl_state;
835 path = cmpl_reference_position (cmpl_state);
837 full_path = g_strconcat (path, "/", fs->fileop_file, NULL);
838 if ( (unlink (full_path) < 0) )
840 buf = g_strconcat ("Error deleting file \"", fs->fileop_file, "\": ",
841 g_strerror(errno), NULL);
842 gtk_file_selection_fileop_error (buf);
846 gtk_widget_destroy (fs->fileop_dialog);
847 gtk_file_selection_populate (fs, "", FALSE);
851 gtk_file_selection_delete_file (GtkWidget *widget, gpointer data)
853 GtkFileSelection *fs = data;
861 g_return_if_fail (fs != NULL);
862 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
864 if (fs->fileop_dialog)
867 filename = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
868 if (strlen(filename) < 1)
871 fs->fileop_file = filename;
874 fs->fileop_dialog = dialog = gtk_dialog_new ();
875 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
876 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
878 gtk_window_set_title (GTK_WINDOW (dialog), "Delete File");
879 gtk_window_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
881 /* If file dialog is grabbed, grab option dialog */
882 /* When option dialog is closed, file dialog will be grabbed again */
883 if (GTK_WINDOW(fs)->modal)
884 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
886 vbox = gtk_vbox_new(FALSE, 0);
887 gtk_container_border_width(GTK_CONTAINER(vbox), 8);
888 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
890 gtk_widget_show(vbox);
892 buf = g_strconcat ("Really delete file \"", filename, "\" ?", NULL);
893 label = gtk_label_new(buf);
894 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
895 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
896 gtk_widget_show(label);
900 button = gtk_button_new_with_label ("Delete");
901 gtk_signal_connect (GTK_OBJECT (button), "clicked",
902 (GtkSignalFunc) gtk_file_selection_delete_file_confirmed,
904 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
905 button, TRUE, TRUE, 0);
906 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
907 gtk_widget_show(button);
909 button = gtk_button_new_with_label ("Cancel");
910 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
911 (GtkSignalFunc) gtk_widget_destroy,
913 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
914 button, TRUE, TRUE, 0);
915 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
916 gtk_widget_grab_default(button);
917 gtk_widget_show (button);
919 gtk_widget_show (dialog);
924 gtk_file_selection_rename_file_confirmed (GtkWidget *widget, gpointer data)
926 GtkFileSelection *fs = data;
932 CompletionState *cmpl_state;
934 g_return_if_fail (fs != NULL);
935 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
937 file = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
938 cmpl_state = (CompletionState*) fs->cmpl_state;
939 path = cmpl_reference_position (cmpl_state);
941 new_filename = g_strconcat (path, "/", file, NULL);
942 old_filename = g_strconcat (path, "/", fs->fileop_file, NULL);
944 if ( (rename (old_filename, new_filename)) < 0)
946 buf = g_strconcat ("Error renaming file \"", file, "\": ",
947 g_strerror(errno), NULL);
948 gtk_file_selection_fileop_error (buf);
950 g_free (new_filename);
951 g_free (old_filename);
953 gtk_widget_destroy (fs->fileop_dialog);
954 gtk_file_selection_populate (fs, "", FALSE);
958 gtk_file_selection_rename_file (GtkWidget *widget, gpointer data)
960 GtkFileSelection *fs = data;
967 g_return_if_fail (fs != NULL);
968 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
970 if (fs->fileop_dialog)
973 fs->fileop_file = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
974 if (strlen(fs->fileop_file) < 1)
978 fs->fileop_dialog = dialog = gtk_dialog_new ();
979 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
980 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
982 gtk_window_set_title (GTK_WINDOW (dialog), "Rename File");
983 gtk_window_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
985 /* If file dialog is grabbed, grab option dialog */
986 /* When option dialog closed, file dialog will be grabbed again */
987 if (GTK_WINDOW(fs)->modal)
988 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
990 vbox = gtk_vbox_new(FALSE, 0);
991 gtk_container_border_width(GTK_CONTAINER(vbox), 8);
992 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
994 gtk_widget_show(vbox);
996 buf = g_strconcat ("Rename file \"", fs->fileop_file, "\" to:", NULL);
997 label = gtk_label_new(buf);
998 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
999 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
1000 gtk_widget_show(label);
1003 /* New filename entry */
1004 fs->fileop_entry = gtk_entry_new ();
1005 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1007 GTK_WIDGET_SET_FLAGS(fs->fileop_entry, GTK_CAN_DEFAULT);
1008 gtk_widget_show (fs->fileop_entry);
1010 gtk_entry_set_text (GTK_ENTRY (fs->fileop_entry), fs->fileop_file);
1011 gtk_editable_select_region (GTK_EDITABLE (fs->fileop_entry),
1012 0, strlen (fs->fileop_file));
1015 button = gtk_button_new_with_label ("Rename");
1016 gtk_signal_connect (GTK_OBJECT (button), "clicked",
1017 (GtkSignalFunc) gtk_file_selection_rename_file_confirmed,
1019 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1020 button, TRUE, TRUE, 0);
1021 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1022 gtk_widget_show(button);
1024 button = gtk_button_new_with_label ("Cancel");
1025 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1026 (GtkSignalFunc) gtk_widget_destroy,
1028 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1029 button, TRUE, TRUE, 0);
1030 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1031 gtk_widget_grab_default(button);
1032 gtk_widget_show (button);
1034 gtk_widget_show (dialog);
1039 gtk_file_selection_key_press (GtkWidget *widget,
1043 GtkFileSelection *fs;
1046 g_return_val_if_fail (widget != NULL, FALSE);
1047 g_return_val_if_fail (event != NULL, FALSE);
1049 if (event->keyval == GDK_Tab)
1053 fs = GTK_FILE_SELECTION (user_data);
1054 text = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1056 intercept = text && *text;
1058 text = g_strdup (text);
1060 gtk_file_selection_populate (fs, text, TRUE);
1065 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "key_press_event");
1075 gtk_file_selection_history_callback (GtkWidget *widget, gpointer data)
1077 GtkFileSelection *fs = data;
1078 HistoryCallbackArg *callback_arg;
1081 g_return_if_fail (fs != NULL);
1082 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1084 list = fs->history_list;
1087 callback_arg = list->data;
1089 if (callback_arg->menu_item == widget)
1091 gtk_file_selection_populate (fs, callback_arg->directory, FALSE);
1100 gtk_file_selection_update_history_menu (GtkFileSelection *fs,
1101 gchar *current_directory)
1103 HistoryCallbackArg *callback_arg;
1104 GtkWidget *menu_item;
1110 g_return_if_fail (fs != NULL);
1111 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1112 g_return_if_fail (current_directory != NULL);
1114 list = fs->history_list;
1116 if (fs->history_menu)
1119 callback_arg = list->data;
1120 g_free (callback_arg->directory);
1121 g_free (callback_arg);
1124 g_list_free (fs->history_list);
1125 fs->history_list = NULL;
1127 gtk_widget_destroy (fs->history_menu);
1130 fs->history_menu = gtk_menu_new();
1132 current_dir = g_strdup (current_directory);
1134 dir_len = strlen (current_dir);
1136 for (i = dir_len; i >= 0; i--)
1138 /* the i == dir_len is to catch the full path for the first
1140 if ( (current_dir[i] == '/') || (i == dir_len))
1142 /* another small hack to catch the full path */
1144 current_dir[i + 1] = '\0';
1145 menu_item = gtk_menu_item_new_with_label (current_dir);
1147 callback_arg = g_new (HistoryCallbackArg, 1);
1148 callback_arg->menu_item = menu_item;
1150 /* since the autocompletion gets confused if you don't
1151 * supply a trailing '/' on a dir entry, set the full
1152 * (current) path to "" which just refreshes the filesel */
1154 callback_arg->directory = g_strdup ("");
1156 callback_arg->directory = g_strdup (current_dir);
1159 fs->history_list = g_list_append (fs->history_list, callback_arg);
1161 gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
1162 (GtkSignalFunc) gtk_file_selection_history_callback,
1164 gtk_menu_append (GTK_MENU (fs->history_menu), menu_item);
1165 gtk_widget_show (menu_item);
1169 gtk_option_menu_set_menu (GTK_OPTION_MENU (fs->history_pulldown),
1171 g_free (current_dir);
1175 gtk_file_selection_file_button (GtkWidget *widget,
1178 GdkEventButton *bevent,
1181 GtkFileSelection *fs = NULL;
1182 gchar *filename, *temp = NULL;
1184 g_return_if_fail (GTK_IS_CLIST (widget));
1187 g_return_if_fail (fs != NULL);
1188 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1190 gtk_clist_get_text (GTK_CLIST (fs->file_list), row, 0, &temp);
1191 filename = g_strdup (temp);
1196 switch (bevent->type)
1198 case GDK_2BUTTON_PRESS:
1199 gtk_button_clicked (GTK_BUTTON (fs->ok_button));
1203 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1207 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1214 gtk_file_selection_dir_button (GtkWidget *widget,
1217 GdkEventButton *bevent,
1220 GtkFileSelection *fs = NULL;
1221 gchar *filename, *temp = NULL;
1223 g_return_if_fail (GTK_IS_CLIST (widget));
1225 fs = GTK_FILE_SELECTION (user_data);
1226 g_return_if_fail (fs != NULL);
1227 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1229 gtk_clist_get_text (GTK_CLIST (fs->dir_list), row, 0, &temp);
1230 filename = g_strdup (temp);
1235 switch (bevent->type)
1237 case GDK_2BUTTON_PRESS:
1238 gtk_file_selection_populate (fs, filename, FALSE);
1242 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1246 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1253 gtk_file_selection_populate (GtkFileSelection *fs,
1257 CompletionState *cmpl_state;
1258 PossibleCompletion* poss;
1261 gchar* rem_path = rel_path;
1264 gint did_recurse = FALSE;
1265 gint possible_count = 0;
1266 gint selection_index = -1;
1267 gint file_list_width;
1268 gint dir_list_width;
1270 g_return_if_fail (fs != NULL);
1271 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1273 cmpl_state = (CompletionState*) fs->cmpl_state;
1274 poss = cmpl_completion_matches (rel_path, &rem_path, cmpl_state);
1276 if (!cmpl_state_okay (cmpl_state))
1278 /* Something went wrong. */
1279 gtk_file_selection_abort (fs);
1283 g_assert (cmpl_state->reference_dir);
1285 gtk_clist_freeze (GTK_CLIST (fs->dir_list));
1286 gtk_clist_clear (GTK_CLIST (fs->dir_list));
1287 gtk_clist_freeze (GTK_CLIST (fs->file_list));
1288 gtk_clist_clear (GTK_CLIST (fs->file_list));
1290 /* Set the dir_list to include ./ and ../ */
1293 row = gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1296 row = gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1298 /*reset the max widths of the lists*/
1299 dir_list_width = gdk_string_width(fs->dir_list->style->font,"../");
1300 gtk_clist_set_column_width(GTK_CLIST(fs->dir_list),0,dir_list_width);
1301 file_list_width = 1;
1302 gtk_clist_set_column_width(GTK_CLIST(fs->file_list),0,file_list_width);
1306 if (cmpl_is_a_completion (poss))
1308 possible_count += 1;
1310 filename = cmpl_this_completion (poss);
1314 if (cmpl_is_directory (poss))
1316 if (strcmp (filename, "./") != 0 &&
1317 strcmp (filename, "../") != 0)
1319 int width = gdk_string_width(fs->dir_list->style->font,
1321 row = gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1322 if(width > dir_list_width)
1324 dir_list_width = width;
1325 gtk_clist_set_column_width(GTK_CLIST(fs->dir_list),0,
1332 int width = gdk_string_width(fs->file_list->style->font,
1334 row = gtk_clist_append (GTK_CLIST (fs->file_list), text);
1335 if(width > file_list_width)
1337 file_list_width = width;
1338 gtk_clist_set_column_width(GTK_CLIST(fs->file_list),0,
1344 poss = cmpl_next_completion (cmpl_state);
1347 gtk_clist_thaw (GTK_CLIST (fs->dir_list));
1348 gtk_clist_thaw (GTK_CLIST (fs->file_list));
1350 /* File lists are set. */
1352 g_assert (cmpl_state->reference_dir);
1357 /* User is trying to complete filenames, so advance the user's input
1358 * string to the updated_text, which is the common leading substring
1359 * of all possible completions, and if its a directory attempt
1360 * attempt completions in it. */
1362 if (cmpl_updated_text (cmpl_state)[0])
1365 if (cmpl_updated_dir (cmpl_state))
1367 gchar* dir_name = g_strdup (cmpl_updated_text (cmpl_state));
1371 gtk_file_selection_populate (fs, dir_name, TRUE);
1377 if (fs->selection_entry)
1378 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry),
1379 cmpl_updated_text (cmpl_state));
1384 selection_index = cmpl_last_valid_char (cmpl_state) -
1385 (strlen (rel_path) - strlen (rem_path));
1386 if (fs->selection_entry)
1387 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), rem_path);
1392 if (fs->selection_entry)
1393 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
1398 if (fs->selection_entry)
1399 gtk_entry_set_position (GTK_ENTRY (fs->selection_entry), selection_index);
1401 if (fs->selection_entry)
1403 sel_text = g_new (char, strlen (cmpl_reference_position (cmpl_state)) +
1404 sizeof ("Selection: "));
1405 strcpy (sel_text, "Selection: ");
1406 strcat (sel_text, cmpl_reference_position (cmpl_state));
1408 gtk_label_set (GTK_LABEL (fs->selection_text), sel_text);
1412 if (fs->history_pulldown)
1414 gtk_file_selection_update_history_menu (fs, cmpl_reference_position (cmpl_state));
1421 gtk_file_selection_abort (GtkFileSelection *fs)
1425 sprintf (err_buf, "Directory unreadable: %s", cmpl_strerror (cmpl_errno));
1427 /* BEEP gdk_beep(); */
1429 if (fs->selection_entry)
1430 gtk_label_set (GTK_LABEL (fs->selection_text), err_buf);
1433 /**********************************************************************/
1434 /* External Interface */
1435 /**********************************************************************/
1437 /* The four completion state selectors
1440 cmpl_updated_text (CompletionState* cmpl_state)
1442 return cmpl_state->updated_text;
1446 cmpl_updated_dir (CompletionState* cmpl_state)
1448 return cmpl_state->re_complete;
1452 cmpl_reference_position (CompletionState* cmpl_state)
1454 return cmpl_state->reference_dir->fullname;
1458 cmpl_last_valid_char (CompletionState* cmpl_state)
1460 return cmpl_state->last_valid_char;
1464 cmpl_completion_fullname (gchar* text, CompletionState* cmpl_state)
1466 static char nothing[2] = "";
1468 if (!cmpl_state_okay (cmpl_state))
1472 else if (text[0] == '/')
1474 strcpy (cmpl_state->updated_text, text);
1476 else if (text[0] == '~')
1481 dir = open_user_dir (text, cmpl_state);
1485 /* spencer says just return ~something, so
1486 * for now just do it. */
1487 strcpy (cmpl_state->updated_text, text);
1492 strcpy (cmpl_state->updated_text, dir->fullname);
1494 slash = strchr (text, '/');
1497 strcat (cmpl_state->updated_text, slash);
1502 strcpy (cmpl_state->updated_text, cmpl_state->reference_dir->fullname);
1503 strcat (cmpl_state->updated_text, "/");
1504 strcat (cmpl_state->updated_text, text);
1507 return cmpl_state->updated_text;
1510 /* The three completion selectors
1513 cmpl_this_completion (PossibleCompletion* pc)
1519 cmpl_is_directory (PossibleCompletion* pc)
1521 return pc->is_directory;
1525 cmpl_is_a_completion (PossibleCompletion* pc)
1527 return pc->is_a_completion;
1530 /**********************************************************************/
1531 /* Construction, deletion */
1532 /**********************************************************************/
1534 static CompletionState*
1535 cmpl_init_state (void)
1537 gchar getcwd_buf[2*MAXPATHLEN];
1538 CompletionState *new_state;
1540 new_state = g_new (CompletionState, 1);
1542 /* We don't use getcwd() on SUNOS, because, it does a popen("pwd")
1543 * and, if that wasn't bad enough, hangs in doing so.
1545 #if defined(sun) && !defined(__SVR4)
1546 if (!getwd (getcwd_buf))
1548 if (!getcwd (getcwd_buf, MAXPATHLEN))
1551 /* Oh joy, we can't get the current directory. Um..., we should have
1552 * a root directory, right? Right? (Probably not portable to non-Unix)
1554 strcpy (getcwd_buf, "/");
1559 new_state->reference_dir = NULL;
1560 new_state->completion_dir = NULL;
1561 new_state->active_completion_dir = NULL;
1563 if ((new_state->user_home_dir = getenv("HOME")) != NULL)
1565 /* if this fails, get_pwdb will fill it in. */
1566 new_state->user_home_dir = g_strdup(new_state->user_home_dir);
1569 new_state->directory_storage = NULL;
1570 new_state->directory_sent_storage = NULL;
1571 new_state->last_valid_char = 0;
1572 new_state->updated_text = g_new (gchar, MAXPATHLEN);
1573 new_state->updated_text_alloc = MAXPATHLEN;
1574 new_state->the_completion.text = g_new (gchar, MAXPATHLEN);
1575 new_state->the_completion.text_alloc = MAXPATHLEN;
1576 new_state->user_dir_name_buffer = NULL;
1577 new_state->user_directories = NULL;
1579 new_state->reference_dir = open_dir (getcwd_buf, new_state);
1581 if (!new_state->reference_dir)
1583 /* Directories changing from underneath us, grumble */
1584 strcpy (getcwd_buf, "/");
1592 cmpl_free_dir_list(GList* dp0)
1597 free_dir (dp->data);
1605 cmpl_free_dir_sent_list(GList* dp0)
1610 free_dir_sent (dp->data);
1618 cmpl_free_state (CompletionState* cmpl_state)
1620 cmpl_free_dir_list (cmpl_state->directory_storage);
1621 cmpl_free_dir_sent_list (cmpl_state->directory_sent_storage);
1623 if (cmpl_state->user_dir_name_buffer)
1624 g_free (cmpl_state->user_dir_name_buffer);
1625 if (cmpl_state->user_home_dir)
1626 g_free (cmpl_state->user_home_dir);
1627 if (cmpl_state->user_directories)
1628 g_free (cmpl_state->user_directories);
1629 if (cmpl_state->the_completion.text)
1630 g_free (cmpl_state->the_completion.text);
1631 if (cmpl_state->updated_text)
1632 g_free (cmpl_state->updated_text);
1634 g_free (cmpl_state);
1638 free_dir(CompletionDir* dir)
1640 g_free(dir->fullname);
1645 free_dir_sent(CompletionDirSent* sent)
1647 g_free(sent->name_buffer);
1648 g_free(sent->entries);
1653 prune_memory_usage(CompletionState *cmpl_state)
1655 GList* cdsl = cmpl_state->directory_sent_storage;
1656 GList* cdl = cmpl_state->directory_storage;
1660 for(; cdsl && len < CMPL_DIRECTORY_CACHE_SIZE; len += 1)
1664 cmpl_free_dir_sent_list(cdsl->next);
1668 cmpl_state->directory_storage = NULL;
1670 if (cdl->data == cmpl_state->reference_dir)
1671 cmpl_state->directory_storage = g_list_prepend(NULL, cdl->data);
1673 free_dir (cdl->data);
1680 /**********************************************************************/
1681 /* The main entrances. */
1682 /**********************************************************************/
1684 static PossibleCompletion*
1685 cmpl_completion_matches (gchar* text_to_complete,
1686 gchar** remaining_text,
1687 CompletionState* cmpl_state)
1690 PossibleCompletion *poss;
1692 prune_memory_usage(cmpl_state);
1694 g_assert(text_to_complete);
1696 cmpl_state->user_completion_index = -1;
1697 cmpl_state->last_completion_text = text_to_complete;
1698 cmpl_state->the_completion.text[0] = 0;
1699 cmpl_state->last_valid_char = 0;
1700 cmpl_state->updated_text_len = -1;
1701 cmpl_state->updated_text[0] = 0;
1702 cmpl_state->re_complete = FALSE;
1704 first_slash = strchr(text_to_complete, '/');
1706 if(text_to_complete[0] == '~' && !first_slash)
1708 /* Text starts with ~ and there is no slash, show all the
1709 * home directory completions.
1711 poss = attempt_homedir_completion(text_to_complete, cmpl_state);
1713 update_cmpl(poss, cmpl_state);
1718 cmpl_state->reference_dir =
1719 open_ref_dir(text_to_complete, remaining_text, cmpl_state);
1721 if(!cmpl_state->reference_dir)
1724 cmpl_state->completion_dir =
1725 find_completion_dir(*remaining_text, remaining_text, cmpl_state);
1727 cmpl_state->last_valid_char = *remaining_text - text_to_complete;
1729 if(!cmpl_state->completion_dir)
1732 cmpl_state->completion_dir->cmpl_index = -1;
1733 cmpl_state->completion_dir->cmpl_parent = NULL;
1734 cmpl_state->completion_dir->cmpl_text = *remaining_text;
1736 cmpl_state->active_completion_dir = cmpl_state->completion_dir;
1738 cmpl_state->reference_dir = cmpl_state->completion_dir;
1740 poss = attempt_file_completion(cmpl_state);
1742 update_cmpl(poss, cmpl_state);
1747 static PossibleCompletion*
1748 cmpl_next_completion (CompletionState* cmpl_state)
1750 PossibleCompletion* poss = NULL;
1752 cmpl_state->the_completion.text[0] = 0;
1754 if(cmpl_state->user_completion_index >= 0)
1755 poss = attempt_homedir_completion(cmpl_state->last_completion_text, cmpl_state);
1757 poss = attempt_file_completion(cmpl_state);
1759 update_cmpl(poss, cmpl_state);
1764 /**********************************************************************/
1765 /* Directory Operations */
1766 /**********************************************************************/
1768 /* Open the directory where completion will begin from, if possible. */
1769 static CompletionDir*
1770 open_ref_dir(gchar* text_to_complete,
1771 gchar** remaining_text,
1772 CompletionState* cmpl_state)
1775 CompletionDir *new_dir;
1777 first_slash = strchr(text_to_complete, '/');
1779 if (text_to_complete[0] == '/' || !cmpl_state->reference_dir)
1781 new_dir = open_dir("/", cmpl_state);
1784 *remaining_text = text_to_complete + 1;
1786 else if (text_to_complete[0] == '~')
1788 new_dir = open_user_dir(text_to_complete, cmpl_state);
1793 *remaining_text = first_slash + 1;
1795 *remaining_text = text_to_complete + strlen(text_to_complete);
1804 *remaining_text = text_to_complete;
1806 new_dir = open_dir(cmpl_state->reference_dir->fullname, cmpl_state);
1811 new_dir->cmpl_index = -1;
1812 new_dir->cmpl_parent = NULL;
1818 /* open a directory by user name */
1819 static CompletionDir*
1820 open_user_dir(gchar* text_to_complete,
1821 CompletionState *cmpl_state)
1826 g_assert(text_to_complete && text_to_complete[0] == '~');
1828 first_slash = strchr(text_to_complete, '/');
1831 cmp_len = first_slash - text_to_complete - 1;
1833 cmp_len = strlen(text_to_complete + 1);
1838 if (!cmpl_state->user_home_dir &&
1839 !get_pwdb(cmpl_state))
1841 return open_dir(cmpl_state->user_home_dir, cmpl_state);
1846 char* copy = g_new(char, cmp_len + 1);
1848 strncpy(copy, text_to_complete + 1, cmp_len);
1850 pwd = getpwnam(copy);
1858 return open_dir(pwd->pw_dir, cmpl_state);
1862 /* open a directory relative the the current relative directory */
1863 static CompletionDir*
1864 open_relative_dir(gchar* dir_name,
1866 CompletionState *cmpl_state)
1868 gchar path_buf[2*MAXPATHLEN];
1870 if(dir->fullname_len + strlen(dir_name) + 2 >= MAXPATHLEN)
1872 cmpl_errno = CMPL_ERRNO_TOO_LONG;
1876 strcpy(path_buf, dir->fullname);
1878 if(dir->fullname_len > 1)
1880 path_buf[dir->fullname_len] = '/';
1881 strcpy(path_buf + dir->fullname_len + 1, dir_name);
1885 strcpy(path_buf + dir->fullname_len, dir_name);
1888 return open_dir(path_buf, cmpl_state);
1891 /* after the cache lookup fails, really open a new directory */
1892 static CompletionDirSent*
1893 open_new_dir(gchar* dir_name, struct stat* sbuf)
1895 CompletionDirSent* sent;
1898 struct dirent *dirent_ptr;
1899 gint buffer_size = 0;
1900 gint entry_count = 0;
1902 struct stat ent_sbuf;
1903 char path_buf[MAXPATHLEN*2];
1906 sent = g_new(CompletionDirSent, 1);
1907 sent->mtime = sbuf->st_mtime;
1908 sent->inode = sbuf->st_ino;
1909 sent->device = sbuf->st_dev;
1911 path_buf_len = strlen(dir_name);
1913 if (path_buf_len > MAXPATHLEN)
1915 cmpl_errno = CMPL_ERRNO_TOO_LONG;
1919 strcpy(path_buf, dir_name);
1921 directory = opendir(dir_name);
1929 while((dirent_ptr = readdir(directory)) != NULL)
1931 int entry_len = strlen(dirent_ptr->d_name);
1932 buffer_size += entry_len + 1;
1935 if(path_buf_len + entry_len + 2 >= MAXPATHLEN)
1937 cmpl_errno = CMPL_ERRNO_TOO_LONG;
1938 closedir(directory);
1943 sent->name_buffer = g_new(gchar, buffer_size);
1944 sent->entries = g_new(CompletionDirEntry, entry_count);
1945 sent->entry_count = entry_count;
1947 buffer_ptr = sent->name_buffer;
1949 rewinddir(directory);
1951 for(i = 0; i < entry_count; i += 1)
1953 dirent_ptr = readdir(directory);
1958 closedir(directory);
1962 strcpy(buffer_ptr, dirent_ptr->d_name);
1963 sent->entries[i].entry_name = buffer_ptr;
1964 buffer_ptr += strlen(dirent_ptr->d_name);
1968 path_buf[path_buf_len] = '/';
1969 strcpy(path_buf + path_buf_len + 1, dirent_ptr->d_name);
1971 if(stat(path_buf, &ent_sbuf) >= 0 && S_ISDIR(ent_sbuf.st_mode))
1972 sent->entries[i].is_dir = 1;
1974 /* stat may fail, and we don't mind, since it could be a
1975 * dangling symlink. */
1976 sent->entries[i].is_dir = 0;
1979 qsort(sent->entries, sent->entry_count, sizeof(CompletionDirEntry), compare_cmpl_dir);
1981 closedir(directory);
1986 /* open a directory by absolute pathname */
1987 static CompletionDir*
1988 open_dir(gchar* dir_name, CompletionState* cmpl_state)
1991 CompletionDirSent *sent;
1994 if(stat(dir_name, &sbuf) < 0)
2000 cdsl = cmpl_state->directory_sent_storage;
2006 if(sent->inode == sbuf.st_ino &&
2007 sent->mtime == sbuf.st_mtime &&
2008 sent->device == sbuf.st_dev)
2009 return attach_dir(sent, dir_name, cmpl_state);
2014 sent = open_new_dir(dir_name, &sbuf);
2017 cmpl_state->directory_sent_storage =
2018 g_list_prepend(cmpl_state->directory_sent_storage, sent);
2020 return attach_dir(sent, dir_name, cmpl_state);
2026 static CompletionDir*
2027 attach_dir(CompletionDirSent* sent, gchar* dir_name, CompletionState *cmpl_state)
2029 CompletionDir* new_dir;
2031 new_dir = g_new(CompletionDir, 1);
2033 cmpl_state->directory_storage =
2034 g_list_prepend(cmpl_state->directory_storage, new_dir);
2036 new_dir->sent = sent;
2037 new_dir->fullname = g_strdup(dir_name);
2038 new_dir->fullname_len = strlen(dir_name);
2044 correct_dir_fullname(CompletionDir* cmpl_dir)
2046 gint length = strlen(cmpl_dir->fullname);
2049 if (strcmp(cmpl_dir->fullname + length - 2, "/.") == 0)
2053 strcpy(cmpl_dir->fullname, "/");
2054 cmpl_dir->fullname_len = 1;
2057 cmpl_dir->fullname[length - 2] = 0;
2060 else if (strcmp(cmpl_dir->fullname + length - 3, "/./") == 0)
2061 cmpl_dir->fullname[length - 2] = 0;
2062 else if (strcmp(cmpl_dir->fullname + length - 3, "/..") == 0)
2066 strcpy(cmpl_dir->fullname, "/");
2067 cmpl_dir->fullname_len = 1;
2071 if(stat(cmpl_dir->fullname, &sbuf) < 0)
2077 cmpl_dir->fullname[length - 2] = 0;
2079 if(!correct_parent(cmpl_dir, &sbuf))
2082 else if (strcmp(cmpl_dir->fullname + length - 4, "/../") == 0)
2086 strcpy(cmpl_dir->fullname, "/");
2087 cmpl_dir->fullname_len = 1;
2091 if(stat(cmpl_dir->fullname, &sbuf) < 0)
2097 cmpl_dir->fullname[length - 3] = 0;
2099 if(!correct_parent(cmpl_dir, &sbuf))
2103 cmpl_dir->fullname_len = strlen(cmpl_dir->fullname);
2109 correct_parent(CompletionDir* cmpl_dir, struct stat *sbuf)
2116 last_slash = strrchr(cmpl_dir->fullname, '/');
2118 g_assert(last_slash);
2120 if(last_slash != cmpl_dir->fullname)
2121 { /* last_slash[0] = 0; */ }
2128 if (stat(cmpl_dir->fullname, &parbuf) < 0)
2134 if (parbuf.st_ino == sbuf->st_ino && parbuf.st_dev == sbuf->st_dev)
2135 /* it wasn't a link */
2141 last_slash[0] = '/'; */
2143 /* it was a link, have to figure it out the hard way */
2145 new_name = find_parent_dir_fullname(cmpl_dir->fullname);
2150 g_free(cmpl_dir->fullname);
2152 cmpl_dir->fullname = new_name;
2158 find_parent_dir_fullname(gchar* dirname)
2160 gchar buffer[MAXPATHLEN];
2161 gchar buffer2[MAXPATHLEN];
2163 #if defined(sun) && !defined(__SVR4)
2166 if(!getcwd(buffer, MAXPATHLEN))
2173 if(chdir(dirname) != 0 || chdir("..") != 0)
2179 #if defined(sun) && !defined(__SVR4)
2182 if(!getcwd(buffer2, MAXPATHLEN))
2191 if(chdir(buffer) != 0)
2197 return g_strdup(buffer2);
2200 /**********************************************************************/
2201 /* Completion Operations */
2202 /**********************************************************************/
2204 static PossibleCompletion*
2205 attempt_homedir_completion(gchar* text_to_complete,
2206 CompletionState *cmpl_state)
2210 if (!cmpl_state->user_dir_name_buffer &&
2211 !get_pwdb(cmpl_state))
2213 length = strlen(text_to_complete) - 1;
2215 cmpl_state->user_completion_index += 1;
2217 while(cmpl_state->user_completion_index < cmpl_state->user_directories_len)
2219 index = first_diff_index(text_to_complete + 1,
2220 cmpl_state->user_directories
2221 [cmpl_state->user_completion_index].login);
2228 if(cmpl_state->last_valid_char < (index + 1))
2229 cmpl_state->last_valid_char = index + 1;
2230 cmpl_state->user_completion_index += 1;
2234 cmpl_state->the_completion.is_a_completion = 1;
2235 cmpl_state->the_completion.is_directory = 1;
2237 append_completion_text("~", cmpl_state);
2239 append_completion_text(cmpl_state->
2240 user_directories[cmpl_state->user_completion_index].login,
2243 return append_completion_text("/", cmpl_state);
2246 if(text_to_complete[1] ||
2247 cmpl_state->user_completion_index > cmpl_state->user_directories_len)
2249 cmpl_state->user_completion_index = -1;
2254 cmpl_state->user_completion_index += 1;
2255 cmpl_state->the_completion.is_a_completion = 1;
2256 cmpl_state->the_completion.is_directory = 1;
2258 return append_completion_text("~/", cmpl_state);
2262 /* returns the index (>= 0) of the first differing character,
2263 * PATTERN_MATCH if the completion matches */
2265 first_diff_index(gchar* pat, gchar* text)
2269 while(*pat && *text && *text == *pat)
2279 return PATTERN_MATCH;
2282 static PossibleCompletion*
2283 append_completion_text(gchar* text, CompletionState* cmpl_state)
2287 if(!cmpl_state->the_completion.text)
2290 len = strlen(text) + strlen(cmpl_state->the_completion.text) + 1;
2292 if(cmpl_state->the_completion.text_alloc > len)
2294 strcat(cmpl_state->the_completion.text, text);
2295 return &cmpl_state->the_completion;
2298 while(i < len) { i <<= 1; }
2300 cmpl_state->the_completion.text_alloc = i;
2302 cmpl_state->the_completion.text = (gchar*)g_realloc(cmpl_state->the_completion.text, i);
2304 if(!cmpl_state->the_completion.text)
2308 strcat(cmpl_state->the_completion.text, text);
2309 return &cmpl_state->the_completion;
2313 static CompletionDir*
2314 find_completion_dir(gchar* text_to_complete,
2315 gchar** remaining_text,
2316 CompletionState* cmpl_state)
2318 gchar* first_slash = strchr(text_to_complete, '/');
2319 CompletionDir* dir = cmpl_state->reference_dir;
2320 *remaining_text = text_to_complete;
2324 gint len = first_slash - *remaining_text;
2326 gint found_index = -1;
2328 gchar* pat_buf = g_new (gchar, len + 1);
2330 strncpy(pat_buf, *remaining_text, len);
2333 for(i = 0; i < dir->sent->entry_count; i += 1)
2335 if(dir->sent->entries[i].is_dir &&
2336 fnmatch(pat_buf, dir->sent->entries[i].entry_name,
2337 FNMATCH_FLAGS)!= FNM_NOMATCH)
2354 CompletionDir* next = open_relative_dir(dir->sent->entries[found_index].entry_name,
2363 next->cmpl_parent = dir;
2367 if(!correct_dir_fullname(dir))
2373 *remaining_text = first_slash + 1;
2374 first_slash = strchr(*remaining_text, '/');
2389 update_cmpl(PossibleCompletion* poss, CompletionState* cmpl_state)
2393 if(!poss || !cmpl_is_a_completion(poss))
2396 cmpl_len = strlen(cmpl_this_completion(poss));
2398 if(cmpl_state->updated_text_alloc < cmpl_len + 1)
2400 cmpl_state->updated_text =
2401 (gchar*)g_realloc(cmpl_state->updated_text,
2402 cmpl_state->updated_text_alloc);
2403 cmpl_state->updated_text_alloc = 2*cmpl_len;
2406 if(cmpl_state->updated_text_len < 0)
2408 strcpy(cmpl_state->updated_text, cmpl_this_completion(poss));
2409 cmpl_state->updated_text_len = cmpl_len;
2410 cmpl_state->re_complete = cmpl_is_directory(poss);
2412 else if(cmpl_state->updated_text_len == 0)
2414 cmpl_state->re_complete = FALSE;
2419 first_diff_index(cmpl_state->updated_text,
2420 cmpl_this_completion(poss));
2422 cmpl_state->re_complete = FALSE;
2424 if(first_diff == PATTERN_MATCH)
2427 if(first_diff > cmpl_state->updated_text_len)
2428 strcpy(cmpl_state->updated_text, cmpl_this_completion(poss));
2430 cmpl_state->updated_text_len = first_diff;
2431 cmpl_state->updated_text[first_diff] = 0;
2435 static PossibleCompletion*
2436 attempt_file_completion(CompletionState *cmpl_state)
2438 gchar *pat_buf, *first_slash;
2439 CompletionDir *dir = cmpl_state->active_completion_dir;
2441 dir->cmpl_index += 1;
2443 if(dir->cmpl_index == dir->sent->entry_count)
2445 if(dir->cmpl_parent == NULL)
2447 cmpl_state->active_completion_dir = NULL;
2453 cmpl_state->active_completion_dir = dir->cmpl_parent;
2455 return attempt_file_completion(cmpl_state);
2459 g_assert(dir->cmpl_text);
2461 first_slash = strchr(dir->cmpl_text, '/');
2465 gint len = first_slash - dir->cmpl_text;
2467 pat_buf = g_new (gchar, len + 1);
2468 strncpy(pat_buf, dir->cmpl_text, len);
2473 gint len = strlen(dir->cmpl_text);
2475 pat_buf = g_new (gchar, len + 2);
2476 strcpy(pat_buf, dir->cmpl_text);
2477 strcpy(pat_buf + len, "*");
2482 if(dir->sent->entries[dir->cmpl_index].is_dir)
2484 if(fnmatch(pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
2485 FNMATCH_FLAGS) != FNM_NOMATCH)
2487 CompletionDir* new_dir;
2489 new_dir = open_relative_dir(dir->sent->entries[dir->cmpl_index].entry_name,
2498 new_dir->cmpl_parent = dir;
2500 new_dir->cmpl_index = -1;
2501 new_dir->cmpl_text = first_slash + 1;
2503 cmpl_state->active_completion_dir = new_dir;
2506 return attempt_file_completion(cmpl_state);
2511 return attempt_file_completion(cmpl_state);
2517 return attempt_file_completion(cmpl_state);
2522 if(dir->cmpl_parent != NULL)
2524 append_completion_text(dir->fullname +
2525 strlen(cmpl_state->completion_dir->fullname) + 1,
2527 append_completion_text("/", cmpl_state);
2530 append_completion_text(dir->sent->entries[dir->cmpl_index].entry_name, cmpl_state);
2532 cmpl_state->the_completion.is_a_completion =
2533 (fnmatch(pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
2534 FNMATCH_FLAGS) != FNM_NOMATCH);
2536 cmpl_state->the_completion.is_directory = dir->sent->entries[dir->cmpl_index].is_dir;
2537 if(dir->sent->entries[dir->cmpl_index].is_dir)
2538 append_completion_text("/", cmpl_state);
2541 return &cmpl_state->the_completion;
2547 get_pwdb(CompletionState* cmpl_state)
2549 struct passwd *pwd_ptr;
2551 gint len = 0, i, count = 0;
2553 if(cmpl_state->user_dir_name_buffer)
2557 while ((pwd_ptr = getpwent()) != NULL)
2559 len += strlen(pwd_ptr->pw_name);
2560 len += strlen(pwd_ptr->pw_dir);
2565 if (!cmpl_state->user_home_dir)
2567 /* the loser doesn't have $HOME set */
2570 pwd_ptr = getpwuid(getuid());
2576 /* Allocate this separately, since it might be filled in elsewhere */
2577 cmpl_state->user_home_dir = g_strdup (pwd_ptr->pw_dir);
2582 cmpl_state->user_dir_name_buffer = g_new(gchar, len);
2583 cmpl_state->user_directories = g_new(CompletionUserDir, count);
2584 cmpl_state->user_directories_len = count;
2586 buf_ptr = cmpl_state->user_dir_name_buffer;
2588 for(i = 0; i < count; i += 1)
2590 pwd_ptr = getpwent();
2597 strcpy(buf_ptr, pwd_ptr->pw_name);
2598 cmpl_state->user_directories[i].login = buf_ptr;
2599 buf_ptr += strlen(buf_ptr);
2601 strcpy(buf_ptr, pwd_ptr->pw_dir);
2602 cmpl_state->user_directories[i].homedir = buf_ptr;
2603 buf_ptr += strlen(buf_ptr);
2607 qsort(cmpl_state->user_directories,
2608 cmpl_state->user_directories_len,
2609 sizeof(CompletionUserDir),
2618 if(cmpl_state->user_dir_name_buffer)
2619 g_free(cmpl_state->user_dir_name_buffer);
2620 if(cmpl_state->user_directories)
2621 g_free(cmpl_state->user_directories);
2623 cmpl_state->user_dir_name_buffer = NULL;
2624 cmpl_state->user_directories = NULL;
2630 compare_user_dir(const void* a, const void* b)
2632 return strcmp((((CompletionUserDir*)a))->login,
2633 (((CompletionUserDir*)b))->login);
2637 compare_cmpl_dir(const void* a, const void* b)
2639 return strcmp((((CompletionDirEntry*)a))->entry_name,
2640 (((CompletionDirEntry*)b))->entry_name);
2644 cmpl_state_okay(CompletionState* cmpl_state)
2646 return cmpl_state && cmpl_state->reference_dir;
2650 cmpl_strerror(gint err)
2652 if(err == CMPL_ERRNO_TOO_LONG)
2653 return "Name too long";
2655 return g_strerror (err);