home *** CD-ROM | disk | FTP | other *** search
/ ftp.whtech.com / ftp.whtech.com.7z / ftp.whtech.com / emulators / v9t9 / linux / sources / V9t9 / source / gtk_v99filesel.c < prev    next >
Encoding:
C/C++ Source or Header  |  2006-10-19  |  80.7 KB  |  3,128 lines

  1. /*
  2.  *    Modified from gtkfilesel.c to add callback capability
  3.  */
  4.  
  5. #ifdef __linux__
  6. #define HAVE_DIRENT_H
  7. #define HAVE_SYS_PARAM_H
  8. #define HAVE_PWD_H
  9. #endif
  10.  
  11. #define g_filename_from_utf8 g_strdup
  12. #define g_filename_to_utf8 g_strdup
  13.  
  14. #include <stdio.h>
  15. #if __linux__
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <dirent.h>
  19. #else
  20. #include <stat.h>
  21. #endif
  22. #ifdef HAVE_SYS_PARAM_H
  23. #include <sys/param.h>
  24. #endif
  25. #ifdef HAVE_DIRENT_H
  26. #include <dirent.h>
  27. #endif
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include <string.h>
  31. #include <errno.h>
  32. #ifdef HAVE_PWD_H
  33. #include <pwd.h>
  34. #endif
  35. #include <OSLib.h>
  36. #include "fnmatch.h"
  37.  
  38. #include <gdk/gdkkeysyms.h>
  39. #include <gtk/gtkbutton.h>
  40. #include <gtk/gtkentry.h>
  41. #include "gtk_v99filesel.h"
  42. #include <gtk/gtkhbox.h>
  43. #include <gtk/gtkhbbox.h>
  44. #include <gtk/gtklabel.h>
  45. #include <gtk/gtklist.h>
  46. #include <gtk/gtklistitem.h>
  47. #include <gtk/gtkmain.h>
  48. #include <gtk/gtkscrolledwindow.h>
  49. #include <gtk/gtksignal.h>
  50. #include <gtk/gtkvbox.h>
  51. #include <gtk/gtkmenu.h>
  52. #include <gtk/gtkmenuitem.h>
  53. #include <gtk/gtkoptionmenu.h>
  54. #include <gtk/gtkclist.h>
  55. #include <gtk/gtkdialog.h>
  56.  
  57. #define _(x)    (x)
  58. #define N_(x)    (x)
  59.  
  60. #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
  61. #define STRICT
  62. #define WIN32_LEAN_AND_MEAN
  63. #include <windows.h>
  64.  
  65. #ifdef G_OS_WIN32
  66. #include <direct.h>
  67. #include <io.h>
  68. #define mkdir(p,m) _mkdir(p)
  69. #ifndef S_ISDIR
  70. #define S_ISDIR(mode) ((mode)&_S_IFDIR)
  71. #endif
  72.  
  73. #endif /* G_OS_WIN32 */
  74.  
  75. #endif /* G_OS_WIN32 || G_WITH_CYGWIN */
  76.  
  77. #define DIR_LIST_WIDTH   180
  78. #define DIR_LIST_HEIGHT  180
  79. #define FILE_LIST_WIDTH  180
  80. #define FILE_LIST_HEIGHT 180
  81.  
  82. /* I've put this here so it doesn't get confused with the 
  83.  * file completion interface */
  84. typedef struct _HistoryCallbackArg HistoryCallbackArg;
  85.  
  86. struct _HistoryCallbackArg
  87. {
  88.   gchar *directory;
  89.   GtkWidget *menu_item;
  90. };
  91.  
  92.  
  93. typedef struct _CompletionState    CompletionState;
  94. typedef struct _CompletionDir      CompletionDir;
  95. typedef struct _CompletionDirSent  CompletionDirSent;
  96. typedef struct _CompletionDirEntry CompletionDirEntry;
  97. typedef struct _CompletionUserDir  CompletionUserDir;
  98. typedef struct _PossibleCompletion PossibleCompletion;
  99.  
  100. /* Non-external file completion decls and structures */
  101.  
  102. /* A contant telling PRCS how many directories to cache.  Its actually
  103.  * kept in a list, so the geometry isn't important. */
  104. #define CMPL_DIRECTORY_CACHE_SIZE 10
  105.  
  106. /* A constant used to determine whether a substring was an exact
  107.  * match by first_diff_index()
  108.  */
  109. #define PATTERN_MATCH -1
  110. /* The arguments used by all fnmatch() calls below
  111.  */
  112. #define FNMATCH_FLAGS (FNM_PATHNAME | FNM_PERIOD)
  113.  
  114. #define CMPL_ERRNO_TOO_LONG ((1<<16)-1)
  115.  
  116. /* This structure contains all the useful information about a directory
  117.  * for the purposes of filename completion.  These structures are cached
  118.  * in the CompletionState struct.  CompletionDir's are reference counted.
  119.  */
  120. struct _CompletionDirSent
  121. {
  122.   ino_t inode;
  123.   time_t mtime;
  124.   dev_t device;
  125.  
  126.   gint entry_count;
  127.   struct _CompletionDirEntry *entries;
  128. };
  129.  
  130. struct _CompletionDir
  131. {
  132.   CompletionDirSent *sent;
  133.  
  134.   gchar *fullname;
  135.   gint fullname_len;
  136.  
  137.   struct _CompletionDir *cmpl_parent;
  138.   gint cmpl_index;
  139.   gchar *cmpl_text;
  140. };
  141.  
  142. /* This structure contains pairs of directory entry names with a flag saying
  143.  * whether or not they are a valid directory.  NOTE: This information is used
  144.  * to provide the caller with information about whether to update its completions
  145.  * or try to open a file.  Since directories are cached by the directory mtime,
  146.  * a symlink which points to an invalid file (which will not be a directory),
  147.  * will not be reevaluated if that file is created, unless the containing
  148.  * directory is touched.  I consider this case to be worth ignoring (josh).
  149.  */
  150. struct _CompletionDirEntry
  151. {
  152.   gboolean is_dir;
  153.   gchar *entry_name;
  154. };
  155.  
  156. struct _CompletionUserDir
  157. {
  158.   gchar *login;
  159.   gchar *homedir;
  160. };
  161.  
  162. struct _PossibleCompletion
  163. {
  164.   /* accessible fields, all are accessed externally by functions
  165.    * declared above
  166.    */
  167.   gchar *text;
  168.   gint is_a_completion;
  169.   gboolean is_directory;
  170.  
  171.   /* Private fields
  172.    */
  173.   gint text_alloc;
  174. };
  175.  
  176. struct _CompletionState
  177. {
  178.   gint last_valid_char;
  179.   gchar *updated_text;
  180.   gint updated_text_len;
  181.   gint updated_text_alloc;
  182.   gboolean re_complete;
  183.  
  184.   gchar *user_dir_name_buffer;
  185.   gint user_directories_len;
  186.  
  187.   gchar *last_completion_text;
  188.  
  189.   gint user_completion_index; /* if >= 0, currently completing ~user */
  190.  
  191.   struct _CompletionDir *completion_dir; /* directory completing from */
  192.   struct _CompletionDir *active_completion_dir;
  193.  
  194.   struct _PossibleCompletion the_completion;
  195.  
  196.   struct _CompletionDir *reference_dir; /* initial directory */
  197.  
  198.   GList* directory_storage;
  199.   GList* directory_sent_storage;
  200.  
  201.   struct _CompletionUserDir *user_directories;
  202. };
  203.  
  204.  
  205. /* File completion functions which would be external, were they used
  206.  * outside of this file.
  207.  */
  208.  
  209. static CompletionState*    cmpl_init_state        (void);
  210. static void                cmpl_free_state        (CompletionState *cmpl_state);
  211. static gint                cmpl_state_okay        (CompletionState* cmpl_state);
  212. static gchar*              cmpl_strerror          (gint);
  213.  
  214. static PossibleCompletion* cmpl_completion_matches(gchar           *text_to_complete,
  215.                            gchar          **remaining_text,
  216.                            CompletionState *cmpl_state);
  217.  
  218. /* Returns a name for consideration, possibly a completion, this name
  219.  * will be invalid after the next call to cmpl_next_completion.
  220.  */
  221. static char*               cmpl_this_completion   (PossibleCompletion*);
  222.  
  223. /* True if this completion matches the given text.  Otherwise, this
  224.  * output can be used to have a list of non-completions.
  225.  */
  226. static gint                cmpl_is_a_completion   (PossibleCompletion*);
  227.  
  228. /* True if the completion is a directory
  229.  */
  230. static gboolean            cmpl_is_directory      (PossibleCompletion*);
  231.  
  232. /* Obtains the next completion, or NULL
  233.  */
  234. static PossibleCompletion* cmpl_next_completion   (CompletionState*);
  235.  
  236. /* Updating completions: the return value of cmpl_updated_text() will
  237.  * be text_to_complete completed as much as possible after the most
  238.  * recent call to cmpl_completion_matches.  For the present
  239.  * application, this is the suggested replacement for the user's input
  240.  * string.  You must CALL THIS AFTER ALL cmpl_text_completions have
  241.  * been received.
  242.  */
  243. static gchar*              cmpl_updated_text       (CompletionState* cmpl_state);
  244.  
  245. /* After updating, to see if the completion was a directory, call
  246.  * this.  If it was, you should consider re-calling completion_matches.
  247.  */
  248. static gboolean            cmpl_updated_dir        (CompletionState* cmpl_state);
  249.  
  250. /* Current location: if using file completion, return the current
  251.  * directory, from which file completion begins.  More specifically,
  252.  * the cwd concatenated with all exact completions up to the last
  253.  * directory delimiter('/').
  254.  */
  255. static gchar*              cmpl_reference_position (CompletionState* cmpl_state);
  256.  
  257. /* backing up: if cmpl_completion_matches returns NULL, you may query
  258.  * the index of the last completable character into cmpl_updated_text.
  259.  */
  260. static gint                cmpl_last_valid_char    (CompletionState* cmpl_state);
  261.  
  262. /* When the user selects a non-directory, call cmpl_completion_fullname
  263.  * to get the full name of the selected file.
  264.  */
  265. static gchar*              cmpl_completion_fullname (gchar*, CompletionState* cmpl_state);
  266.  
  267.  
  268. /* Directory operations. */
  269. static CompletionDir* open_ref_dir         (gchar* text_to_complete,
  270.                         gchar** remaining_text,
  271.                         CompletionState* cmpl_state);
  272. #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
  273. static gboolean       check_dir            (gchar *dir_name, 
  274.                         struct stat *result, 
  275.                         gboolean *stat_subdirs);
  276. #endif
  277. static CompletionDir* open_dir             (gchar* dir_name,
  278.                         CompletionState* cmpl_state);
  279. #ifdef HAVE_PWD_H
  280. static CompletionDir* open_user_dir        (gchar* text_to_complete,
  281.                         CompletionState *cmpl_state);
  282. #endif
  283. static CompletionDir* open_relative_dir    (gchar* dir_name, CompletionDir* dir,
  284.                         CompletionState *cmpl_state);
  285. static CompletionDirSent* open_new_dir     (gchar* dir_name, 
  286.                         struct stat* sbuf,
  287.                         gboolean stat_subdirs);
  288. static gint           correct_dir_fullname (CompletionDir* cmpl_dir);
  289. static gint           correct_parent       (CompletionDir* cmpl_dir,
  290.                         struct stat *sbuf);
  291. static gchar*         find_parent_dir_fullname    (gchar* dirname);
  292. static CompletionDir* attach_dir           (CompletionDirSent* sent,
  293.                         gchar* dir_name,
  294.                         CompletionState *cmpl_state);
  295. static void           free_dir_sent (CompletionDirSent* sent);
  296. static void           free_dir      (CompletionDir  *dir);
  297. static void           prune_memory_usage(CompletionState *cmpl_state);
  298.  
  299. /* Completion operations */
  300. static PossibleCompletion* attempt_homedir_completion(gchar* text_to_complete,
  301.                               CompletionState *cmpl_state);
  302. static PossibleCompletion* attempt_file_completion(CompletionState *cmpl_state);
  303. static CompletionDir* find_completion_dir(gchar* text_to_complete,
  304.                       gchar** remaining_text,
  305.                       CompletionState* cmpl_state);
  306. static PossibleCompletion* append_completion_text(gchar* text,
  307.                           CompletionState* cmpl_state);
  308. #ifdef HAVE_PWD_H
  309. static gint get_pwdb(CompletionState* cmpl_state);
  310. static gint compare_user_dir(const void* a, const void* b);
  311. #endif
  312. static gint first_diff_index(gchar* pat, gchar* text);
  313. static gint compare_cmpl_dir(const void* a, const void* b);
  314. static void update_cmpl(PossibleCompletion* poss,
  315.             CompletionState* cmpl_state);
  316.  
  317. static void gtk_file_selection_class_init    (V99FileSelectionClass *klass);
  318. static void gtk_file_selection_init          (V99FileSelection      *filesel);
  319. static void gtk_file_selection_destroy       (GtkObject             *object);
  320. static gint gtk_file_selection_key_press     (GtkWidget             *widget,
  321.                           GdkEventKey           *event,
  322.                           gpointer               user_data);
  323.  
  324. static void gtk_file_selection_file_button (GtkWidget *widget,
  325.                         gint row, 
  326.                         gint column, 
  327.                         GdkEventButton *bevent,
  328.                         gpointer user_data);
  329.  
  330. static void gtk_file_selection_dir_button (GtkWidget *widget,
  331.                        gint row, 
  332.                        gint column, 
  333.                        GdkEventButton *bevent,
  334.                        gpointer data);
  335.  
  336. static void gtk_file_selection_populate      (V99FileSelection      *fs,
  337.                           gchar                 *rel_path,
  338.                           gint                   try_complete);
  339. static void gtk_file_selection_abort         (V99FileSelection      *fs);
  340.  
  341. static void gtk_file_selection_update_history_menu (V99FileSelection       *fs,
  342.                             gchar                  *current_dir);
  343.  
  344. static void gtk_file_selection_create_dir (GtkWidget *widget, gpointer data);
  345. static void gtk_file_selection_delete_file (GtkWidget *widget, gpointer data);
  346. static void gtk_file_selection_rename_file (GtkWidget *widget, gpointer data);
  347.  
  348.  
  349.  
  350. static GtkWindowClass *parent_class = NULL;
  351.  
  352. /* Saves errno when something cmpl does fails. */
  353. static gint cmpl_errno;
  354.  
  355. #ifdef G_WITH_CYGWIN
  356. /*
  357.  * Take the path currently in the file selection
  358.  * entry field and translate as necessary from
  359.  * a WIN32 style to CYGWIN32 style path.  For
  360.  * instance translate:
  361.  * x:\somepath\file.jpg
  362.  * to:
  363.  * //x/somepath/file.jpg
  364.  *
  365.  * Replace the path in the selection text field.
  366.  * Return a boolean value concerning whether a
  367.  * translation had to be made.
  368.  */
  369. int
  370. translate_win32_path(V99FileSelection *filesel)
  371. {
  372.   int updated = 0;
  373.   gchar *path;
  374.  
  375.   /*
  376.    * Retrieve the current path
  377.    */
  378.   path = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
  379.  
  380.   /*
  381.    * Translate only if this looks like a DOS-ish
  382.    * path... First handle any drive letters.
  383.    */
  384.   if (isalpha(path[0]) && (path[1] == ':')) {
  385.     /*
  386.      * This part kind of stinks... It isn't possible
  387.      * to know if there is enough space in the current
  388.      * string for the extra character required in this
  389.      * conversion.  Assume that there isn't enough space
  390.      * and use the set function on the text field to
  391.      * set the newly created string.
  392.      */
  393.     gchar *newPath;
  394.  
  395.     newPath = g_malloc(strlen(path) + 2);
  396.     sprintf(newPath, "//%c/%s", path[0], (path + 3));
  397.     gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), newPath);
  398.  
  399.     path = newPath;
  400.     updated = 1;
  401.   }
  402.  
  403.   /*
  404.    * Now, replace backslashes with forward slashes 
  405.    * if necessary.
  406.    */
  407.   if (strchr(path, '\\')) {
  408.     int index;
  409.     for (index = 0; path[index] != '\0'; index++)
  410.       if (path[index] == '\\')
  411.     path[index] = '/';
  412.  
  413.     updated = 1;
  414.   }
  415.     
  416.   return updated;
  417. }
  418. #endif
  419.  
  420. GtkType
  421. v99_file_selection_get_type (void)
  422. {
  423.   static GtkType file_selection_type = 0;
  424.  
  425.   if (!file_selection_type)
  426.     {
  427.       static const GtkTypeInfo filesel_info =
  428.       {
  429.     "V99FileSelection",
  430.     sizeof (V99FileSelection),
  431.     sizeof (V99FileSelectionClass),
  432.     (GtkClassInitFunc) gtk_file_selection_class_init,
  433.     (GtkObjectInitFunc) gtk_file_selection_init,
  434.     /* reserved_1 */ NULL,
  435.     /* reserved_2 */ NULL,
  436.         (GtkClassInitFunc) NULL,
  437.       };
  438.  
  439.       file_selection_type = gtk_type_unique (GTK_TYPE_WINDOW, &filesel_info);
  440.     }
  441.  
  442.   return file_selection_type;
  443. }
  444.  
  445. static void
  446. gtk_file_selection_class_init (V99FileSelectionClass *class)
  447. {
  448.   GtkObjectClass *object_class;
  449.  
  450.   object_class = (GtkObjectClass*) class;
  451.  
  452.   parent_class = gtk_type_class (GTK_TYPE_WINDOW);
  453.  
  454.   object_class->destroy = gtk_file_selection_destroy;
  455. }
  456.  
  457. static void
  458. gtk_file_selection_init (V99FileSelection *filesel)
  459. {
  460.   GtkWidget *entry_vbox;
  461.   GtkWidget *label;
  462. //  GtkWidget *list_hbox;
  463.   GtkWidget *confirm_area;
  464.   GtkWidget *pulldown_hbox;
  465. //  GtkWidget *scrolled_win;
  466.  
  467.   char *dir_title [2];
  468.   char *file_title [2];
  469.   
  470.   filesel->cmpl_state = cmpl_init_state ();
  471.  
  472.   /* The dialog-sized vertical box  */
  473.   filesel->main_vbox = gtk_vbox_new (FALSE, 10);
  474.   gtk_container_set_border_width (GTK_CONTAINER (filesel), 10);
  475.   gtk_container_add (GTK_CONTAINER (filesel), filesel->main_vbox);
  476.   gtk_widget_show (filesel->main_vbox);
  477.  
  478.   /* The horizontal box containing create, rename etc. buttons */
  479.   filesel->button_area = gtk_hbutton_box_new ();
  480.   gtk_button_box_set_layout(GTK_BUTTON_BOX(filesel->button_area), GTK_BUTTONBOX_START);
  481.   gtk_button_box_set_spacing(GTK_BUTTON_BOX(filesel->button_area), 0);
  482.   gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->button_area, 
  483.               FALSE, FALSE, 0);
  484.   gtk_widget_show (filesel->button_area);
  485.   
  486.   v99_file_selection_show_fileop_buttons(filesel);
  487.  
  488.   /* hbox for pulldown menu */
  489.   pulldown_hbox = gtk_hbox_new (TRUE, 5);
  490.   gtk_box_pack_start (GTK_BOX (filesel->main_vbox), pulldown_hbox, FALSE, FALSE, 0);
  491.   gtk_widget_show (pulldown_hbox);
  492.   
  493.   /* Pulldown menu */
  494.   filesel->history_pulldown = gtk_option_menu_new ();
  495.   gtk_widget_show (filesel->history_pulldown);
  496.   gtk_box_pack_start (GTK_BOX (pulldown_hbox), filesel->history_pulldown, 
  497.               FALSE, FALSE, 0);
  498.     
  499.   /*  The horizontal box containing the directory and file listboxes  */
  500.   filesel->list_hbox = gtk_hbox_new (FALSE, 6);
  501.   gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->list_hbox, TRUE, TRUE, 0);
  502.   gtk_widget_show (filesel->list_hbox);
  503.  
  504.   /* The directories clist */
  505.   dir_title[0] = _("Directories");
  506.   dir_title[1] = NULL;
  507.   filesel->dir_list = gtk_clist_new_with_titles (1, (gchar**) dir_title);
  508.   gtk_widget_set_usize (filesel->dir_list, DIR_LIST_WIDTH, DIR_LIST_HEIGHT);
  509.   gtk_signal_connect (GTK_OBJECT (filesel->dir_list), "select_row",
  510.               (GtkSignalFunc) gtk_file_selection_dir_button, 
  511.               (gpointer) filesel);
  512.   gtk_clist_column_titles_passive (GTK_CLIST (filesel->dir_list));
  513.  
  514.   filesel->dir_list_scrolled_win = gtk_scrolled_window_new (NULL, NULL);
  515.   gtk_container_add (GTK_CONTAINER (filesel->dir_list_scrolled_win), filesel->dir_list);
  516.   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (filesel->dir_list_scrolled_win),
  517.                   GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
  518.   gtk_container_set_border_width (GTK_CONTAINER (filesel->dir_list_scrolled_win), 5);
  519.   gtk_box_pack_start (GTK_BOX (filesel->list_hbox), filesel->dir_list_scrolled_win, TRUE, TRUE, 0);
  520.   gtk_widget_show (filesel->dir_list);
  521.   gtk_widget_show (filesel->dir_list_scrolled_win);
  522.  
  523.   /* The files clist */
  524.   file_title[0] = _("Files");
  525.   file_title[1] = NULL;
  526. //  filesel->file_list = gtk_clist_new_with_titles (1, (gchar**) file_title);
  527. //  gtk_widget_set_usize (filesel->file_list, FILE_LIST_WIDTH, FILE_LIST_HEIGHT);
  528.  
  529.   v99_file_selection_set_file_list_columns(filesel, 1, (gchar **)file_title, NULL, NULL);
  530.   v99_file_selection_set_file_list_active(filesel, TRUE);
  531.  
  532. /*  gtk_signal_connect (GTK_OBJECT (filesel->file_list), "select_row",
  533.                       (GtkSignalFunc) v99_file_selection_file_button, 
  534.                       (gpointer) filesel);*/
  535. //  gtk_clist_column_titles_passive (GTK_CLIST (filesel->file_list));
  536.  
  537. /*
  538.   filesel->file_list_scrolled_win = gtk_scrolled_window_new (NULL, NULL);
  539.   gtk_container_add (GTK_CONTAINER (filesel->file_list_scrolled_win), filesel->file_list);
  540.   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (filesel->file_list_scrolled_win),
  541.                   GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
  542.   gtk_container_set_border_width (GTK_CONTAINER (filesel->file_list_scrolled_win), 5);
  543.   gtk_box_pack_start (GTK_BOX (filesel->list_hbox), 
  544.                       filesel->file_list_scrolled_win, TRUE, TRUE, 0);
  545.  
  546.   gtk_widget_show (filesel->file_list);
  547.   gtk_widget_show (filesel->file_list_scrolled_win);
  548. */
  549.   /* action area for packing buttons into. */
  550.   filesel->action_area = gtk_hbox_new (TRUE, 0);
  551.   gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->action_area, 
  552.               FALSE, FALSE, 0);
  553.   gtk_widget_show (filesel->action_area);
  554.   
  555.   /*  The OK/Cancel button area */
  556.   confirm_area = gtk_hbutton_box_new ();
  557.   gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area), GTK_BUTTONBOX_END);
  558.   gtk_button_box_set_spacing(GTK_BUTTON_BOX(confirm_area), 5);
  559.   gtk_box_pack_end (GTK_BOX (filesel->main_vbox), confirm_area, FALSE, FALSE, 0);
  560.   gtk_widget_show (confirm_area);
  561.  
  562.   /*  The OK button  */
  563.   filesel->ok_button = gtk_button_new_with_label (_("OK"));
  564.   GTK_WIDGET_SET_FLAGS (filesel->ok_button, GTK_CAN_DEFAULT);
  565.   gtk_box_pack_start (GTK_BOX (confirm_area), filesel->ok_button, TRUE, TRUE, 0);
  566.   gtk_widget_grab_default (filesel->ok_button);
  567.   gtk_widget_show (filesel->ok_button);
  568.  
  569.   /*  The Cancel button  */
  570.   filesel->cancel_button = gtk_button_new_with_label (_("Cancel"));
  571.   GTK_WIDGET_SET_FLAGS (filesel->cancel_button, GTK_CAN_DEFAULT);
  572.   gtk_box_pack_start (GTK_BOX (confirm_area), filesel->cancel_button, TRUE, TRUE, 0);
  573.   gtk_widget_show (filesel->cancel_button);
  574.  
  575.   /*  The selection entry widget  */
  576.   entry_vbox = gtk_vbox_new (FALSE, 2);
  577.   gtk_box_pack_end (GTK_BOX (filesel->main_vbox), entry_vbox, FALSE, FALSE, 0);
  578.   gtk_widget_show (entry_vbox);
  579.  
  580.   filesel->selection_text = label = gtk_label_new ("");
  581.   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  582.   gtk_box_pack_start (GTK_BOX (entry_vbox), label, FALSE, FALSE, 0);
  583.   gtk_widget_show (label);
  584.  
  585.   filesel->selection_entry = gtk_entry_new ();
  586.   gtk_signal_connect (GTK_OBJECT (filesel->selection_entry), "key_press_event",
  587.               (GtkSignalFunc) gtk_file_selection_key_press, filesel);
  588.   gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "focus_in_event",
  589.                  (GtkSignalFunc) gtk_widget_grab_default,
  590.                  GTK_OBJECT (filesel->ok_button));
  591.   gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "activate",
  592.                              (GtkSignalFunc) gtk_button_clicked,
  593.                              GTK_OBJECT (filesel->ok_button));
  594.   gtk_box_pack_start (GTK_BOX (entry_vbox), filesel->selection_entry, TRUE, TRUE, 0);
  595.   gtk_widget_show (filesel->selection_entry);
  596.  
  597.   if (!cmpl_state_okay (filesel->cmpl_state))
  598.     {
  599.       gchar err_buf[256];
  600.  
  601.       sprintf (err_buf, _("Directory unreadable: %s"), cmpl_strerror (cmpl_errno));
  602.  
  603.       gtk_label_set_text (GTK_LABEL (filesel->selection_text), err_buf);
  604.     }
  605.   else
  606.     {
  607.       gtk_file_selection_populate (filesel, "", FALSE);
  608.     }
  609.  
  610.   gtk_widget_grab_focus (filesel->selection_entry);
  611. }
  612.  
  613. GtkWidget*
  614. v99_file_selection_new (const gchar *title)
  615. {
  616.   V99FileSelection *filesel;
  617.  
  618.   filesel = gtk_type_new (V99_TYPE_FILE_SELECTION);
  619.   gtk_window_set_title (GTK_WINDOW (filesel), title);
  620.  
  621.   return GTK_WIDGET (filesel);
  622. }
  623.  
  624. void
  625. v99_file_selection_show_fileop_buttons (V99FileSelection *filesel)
  626. {
  627.   g_return_if_fail (filesel != NULL);
  628.   g_return_if_fail (V99_IS_FILE_SELECTION (filesel));
  629.     
  630.   /* delete, create directory, and rename */
  631.   if (!filesel->fileop_c_dir) 
  632.     {
  633.       filesel->fileop_c_dir = gtk_button_new_with_label (_("Create Dir"));
  634.       gtk_signal_connect (GTK_OBJECT (filesel->fileop_c_dir), "clicked",
  635.               (GtkSignalFunc) gtk_file_selection_create_dir, 
  636.               (gpointer) filesel);
  637.       gtk_box_pack_start (GTK_BOX (filesel->button_area), 
  638.               filesel->fileop_c_dir, TRUE, TRUE, 0);
  639.       gtk_widget_show (filesel->fileop_c_dir);
  640.     }
  641.     
  642.   if (!filesel->fileop_del_file) 
  643.     {
  644.       filesel->fileop_del_file = gtk_button_new_with_label (_("Delete File"));
  645.       gtk_signal_connect (GTK_OBJECT (filesel->fileop_del_file), "clicked",
  646.               (GtkSignalFunc) gtk_file_selection_delete_file, 
  647.               (gpointer) filesel);
  648.       gtk_box_pack_start (GTK_BOX (filesel->button_area), 
  649.               filesel->fileop_del_file, TRUE, TRUE, 0);
  650.       gtk_widget_show (filesel->fileop_del_file);
  651.     }
  652.  
  653.   if (!filesel->fileop_ren_file)
  654.     {
  655.       filesel->fileop_ren_file = gtk_button_new_with_label (_("Rename File"));
  656.       gtk_signal_connect (GTK_OBJECT (filesel->fileop_ren_file), "clicked",
  657.               (GtkSignalFunc) gtk_file_selection_rename_file, 
  658.               (gpointer) filesel);
  659.       gtk_box_pack_start (GTK_BOX (filesel->button_area), 
  660.               filesel->fileop_ren_file, TRUE, TRUE, 0);
  661.       gtk_widget_show (filesel->fileop_ren_file);
  662.     }
  663.  
  664.   gtk_widget_queue_resize(GTK_WIDGET(filesel));
  665. }
  666.  
  667. void       
  668. v99_file_selection_hide_fileop_buttons (V99FileSelection *filesel)
  669. {
  670.   g_return_if_fail (filesel != NULL);
  671.   g_return_if_fail (V99_IS_FILE_SELECTION (filesel));
  672.     
  673.   if (filesel->fileop_ren_file) 
  674.     {
  675.       gtk_widget_destroy (filesel->fileop_ren_file);
  676.       filesel->fileop_ren_file = NULL;
  677.     }
  678.  
  679.   if (filesel->fileop_del_file)
  680.     {
  681.       gtk_widget_destroy (filesel->fileop_del_file);
  682.       filesel->fileop_del_file = NULL;
  683.     }
  684.  
  685.   if (filesel->fileop_c_dir)
  686.     {
  687.       gtk_widget_destroy (filesel->fileop_c_dir);
  688.       filesel->fileop_c_dir = NULL;
  689.     }
  690. }
  691.  
  692.  
  693.  
  694. void
  695. v99_file_selection_set_filename (V99FileSelection *filesel,
  696.                  const gchar      *filename)
  697. {
  698.   gchar *buf;
  699.   const char *name, *last_slash;
  700.  
  701.   g_return_if_fail (filesel != NULL);
  702.   g_return_if_fail (V99_IS_FILE_SELECTION (filesel));
  703.   g_return_if_fail (filename != NULL);
  704.  
  705.   last_slash = strrchr (filename, G_DIR_SEPARATOR);
  706.  
  707.   if (!last_slash)
  708.     {
  709.       buf = g_strdup ("");
  710.       name = filename;
  711.     }
  712.   else
  713.     {
  714.       buf = g_strdup (filename);
  715.       buf[last_slash - filename + 1] = 0;
  716.       name = last_slash + 1;
  717.     }
  718.  
  719.   gtk_file_selection_populate (filesel, buf, FALSE);
  720.  
  721.   if (filesel->selection_entry)
  722.     gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), name);
  723.   g_free (buf);
  724. }
  725.  
  726. gchar*
  727. v99_file_selection_get_filename (V99FileSelection *filesel)
  728. {
  729.   static gchar nothing[2] = "";
  730.   static gchar something[MAXPATHLEN*2];
  731.   char *text;
  732.   char *filename;
  733.  
  734.   g_return_val_if_fail (filesel != NULL, nothing);
  735.   g_return_val_if_fail (V99_IS_FILE_SELECTION (filesel), nothing);
  736.  
  737.  #ifdef G_WITH_CYGWIN
  738.   translate_win32_path (filesel);
  739. #endif
  740.  
  741.  text = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
  742.   if (text)
  743.     {
  744.       filename = g_filename_from_utf8 (cmpl_completion_fullname (text, filesel->cmpl_state));
  745.       strncpy (something, filename, sizeof (something));
  746.       g_free (filename);
  747.       return something;
  748.     }
  749.  
  750.   return nothing;
  751. }
  752.  
  753. void
  754. v99_file_selection_complete (V99FileSelection *filesel,
  755.                  const gchar      *pattern)
  756. {
  757.   g_return_if_fail (filesel != NULL);
  758.   g_return_if_fail (V99_IS_FILE_SELECTION (filesel));
  759.   g_return_if_fail (pattern != NULL);
  760.  
  761.   if (filesel->selection_entry)
  762.     gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), pattern);
  763.   gtk_file_selection_populate (filesel, (gchar*) pattern, TRUE);
  764. }
  765.  
  766. static void
  767. gtk_file_selection_destroy (GtkObject *object)
  768. {
  769.   V99FileSelection *filesel;
  770.   GList *list;
  771.   HistoryCallbackArg *callback_arg;
  772.  
  773.   g_return_if_fail (object != NULL);
  774.   g_return_if_fail (V99_IS_FILE_SELECTION (object));
  775.  
  776.   filesel = V99_FILE_SELECTION (object);
  777.   
  778.   if (filesel->fileop_dialog)
  779.       gtk_widget_destroy (filesel->fileop_dialog);
  780.   
  781.   if (filesel->history_list)
  782.     {
  783.       list = filesel->history_list;
  784.       while (list)
  785.     {
  786.       callback_arg = list->data;
  787.       g_free (callback_arg->directory);
  788.       g_free (callback_arg);
  789.       list = list->next;
  790.     }
  791.       g_list_free (filesel->history_list);
  792.       filesel->history_list = NULL;
  793.     }
  794.   
  795.   cmpl_free_state (filesel->cmpl_state);
  796.   filesel->cmpl_state = NULL;
  797.  
  798.   if (GTK_OBJECT_CLASS (parent_class)->destroy)
  799.     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
  800. }
  801.  
  802. void
  803. v99_file_selection_set_file_list_columns(V99FileSelection *filesel,
  804.                                          int columns,
  805.                                          gchar *titles[],
  806.                                          v99_file_selection_file_append ahandler,
  807.                                          v99_file_selection_file_get ghandler)
  808. {
  809.     int i;
  810.  
  811.     g_return_if_fail (filesel != NULL);
  812.     g_return_if_fail (V99_IS_FILE_SELECTION (filesel));
  813.  
  814.     if (filesel->file_list) {
  815.         gtk_widget_destroy(filesel->file_list);
  816.         filesel->file_list_select_row_handler_id = 0L;
  817.     }
  818.  
  819.     filesel->file_list = gtk_clist_new_with_titles(columns, titles);
  820.     filesel->user_file_append = ahandler;
  821.     filesel->user_file_get = ghandler;
  822.  
  823.     gtk_widget_set_usize (filesel->file_list, FILE_LIST_WIDTH * columns / 2, FILE_LIST_HEIGHT);
  824.     for (i = 0; i < columns; i++) {
  825.         gtk_clist_set_column_width(GTK_CLIST(filesel->file_list), i, FILE_LIST_WIDTH / 2);
  826.     }
  827.  
  828.     gtk_clist_column_titles_active (GTK_CLIST (filesel->file_list));
  829.  
  830.     if (filesel->file_list_scrolled_win) {
  831.         gtk_widget_destroy(filesel->file_list_scrolled_win);
  832.     }
  833.     filesel->file_list_scrolled_win = gtk_scrolled_window_new (NULL, NULL);
  834.     gtk_container_add (GTK_CONTAINER (filesel->file_list_scrolled_win), filesel->file_list);
  835.     gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (filesel->file_list_scrolled_win),
  836.                                     GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
  837.     gtk_container_set_border_width (GTK_CONTAINER (filesel->file_list_scrolled_win), 5);
  838.     gtk_box_pack_start (GTK_BOX (filesel->list_hbox), 
  839.                         filesel->file_list_scrolled_win, TRUE, TRUE, 0);
  840.  
  841.   gtk_widget_show (filesel->file_list);
  842.   gtk_widget_show (filesel->file_list_scrolled_win);
  843. }
  844.  
  845. void        
  846. v99_file_selection_set_file_list_active(V99FileSelection *filesel, gboolean active)
  847. {
  848.     if (active) {
  849.         if (filesel->file_list_select_row_handler_id) {
  850.             return;
  851.         }
  852.         filesel->file_list_select_row_handler_id = 
  853.             gtk_signal_connect (GTK_OBJECT (filesel->file_list), "select_row",
  854.                             (GtkSignalFunc) gtk_file_selection_file_button, 
  855.                             (gpointer) filesel);
  856.  
  857.     } else {
  858.         if (!filesel->file_list_select_row_handler_id) {
  859.             return;
  860.         }
  861.         gtk_signal_disconnect(GTK_OBJECT (filesel->file_list), 
  862.                               filesel->file_list_select_row_handler_id);
  863.         filesel->file_list_select_row_handler_id = 0;
  864.     }
  865. }
  866.  
  867. /*
  868.  *    Add a List to a vbox on the left side of the dialog
  869.  *    which contains a list of paths from 'path_list'.  
  870.  */
  871. GtkCList    *v99_file_selection_add_path_list(V99FileSelection *filesel,
  872.                                          gchar *path_list_name,
  873.                                          gchar *path_list)
  874. {
  875.     GtkWidget *w, *vbox;
  876.     GtkCList *clist;
  877.     gchar *ptr, *end;
  878.     char tmp[OS_PATHSIZE], *tmplist[2];
  879.  
  880.     if (!filesel->path_list_vbox) {
  881.         vbox = gtk_vbox_new(FALSE, 4);
  882.         filesel->path_list_vbox = vbox;
  883.         gtk_widget_ref(vbox);
  884.         gtk_object_set_data_full (GTK_OBJECT (vbox), 
  885.                                   "path_list_vbox", vbox,
  886.                                   (GtkDestroyNotify) gtk_widget_unref);
  887.         gtk_box_pack_start (GTK_BOX (filesel->list_hbox), vbox, TRUE, TRUE, 0);
  888.         gtk_widget_show (vbox);
  889.     } else {
  890.         vbox = filesel->path_list_vbox;
  891.     }
  892.  
  893.     w = gtk_clist_new (1);
  894.     gtk_widget_ref (w);
  895.     GTK_WIDGET_SET_FLAGS (w, GTK_CAN_DEFAULT);
  896.  
  897.     clist = GTK_CLIST(w);
  898.     gtk_clist_set_column_width (clist, 0, 64);
  899.     gtk_clist_set_selection_mode (clist, GTK_SELECTION_SINGLE);
  900.  
  901.     gtk_clist_set_column_title(clist, 0, path_list_name);
  902.     gtk_clist_column_titles_show (clist);
  903.  
  904.     /* get a list of entries */
  905.     tmplist[0] = tmp;
  906.     tmplist[1] = 0L;
  907.  
  908.     while (path_list && *path_list) {
  909.         ptr = strchr(path_list, OS_ENVSEP);
  910.         if (!ptr)
  911.             ptr = strpbrk(path_list, OS_ENVSEPLIST);
  912.         if (!ptr)
  913.             ptr = path_list + strlen(path_list);
  914.  
  915.         strcpyn(tmp, path_list, ptr - path_list, OS_PATHSIZE);
  916.         gtk_clist_append(clist, tmplist);
  917.         path_list = (*ptr) ? ptr + 1 : NULL;
  918.     }
  919.  
  920.     /* now widen the list and show it */
  921.     gtk_clist_optimal_column_width(clist, 0);
  922.     gtk_widget_show (w);
  923.  
  924.     gtk_box_pack_start (GTK_BOX (vbox), w, TRUE, TRUE, 0);
  925.     return clist;
  926. }
  927.  
  928. #if 0
  929. #pragma mark -
  930. #endif
  931.  
  932. /* Begin file operations callbacks */
  933.  
  934. static void
  935. gtk_file_selection_fileop_error (V99FileSelection *fs, gchar *error_message)
  936. {
  937.   GtkWidget *label;
  938.   GtkWidget *vbox;
  939.   GtkWidget *button;
  940.   GtkWidget *dialog;
  941.   
  942.   g_return_if_fail (error_message != NULL);
  943.   
  944.   /* main dialog */
  945.   dialog = gtk_dialog_new ();
  946.   /*
  947.   gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
  948.               (GtkSignalFunc) gtk_file_selection_fileop_destroy, 
  949.               (gpointer) fs);
  950.   */
  951.   gtk_window_set_title (GTK_WINDOW (dialog), _("Error"));
  952.   gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
  953.   
  954.   /* If file dialog is grabbed, make this dialog modal too */
  955.   /* When error dialog is closed, file dialog will be grabbed again */
  956.   if (GTK_WINDOW(fs)->modal)
  957.       gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
  958.  
  959.   vbox = gtk_vbox_new(FALSE, 0);
  960.   gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
  961.   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
  962.              FALSE, FALSE, 0);
  963.   gtk_widget_show(vbox);
  964.  
  965.   label = gtk_label_new(error_message);
  966.   gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
  967.   gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
  968.   gtk_widget_show(label);
  969.  
  970.   /* yes, we free it */
  971.   g_free (error_message);
  972.   
  973.   /* close button */
  974.   button = gtk_button_new_with_label (_("Close"));
  975.   gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
  976.                  (GtkSignalFunc) gtk_widget_destroy, 
  977.                  (gpointer) dialog);
  978.   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
  979.              button, TRUE, TRUE, 0);
  980.   GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
  981.   gtk_widget_grab_default(button);
  982.   gtk_widget_show (button);
  983.  
  984.   gtk_widget_show (dialog);
  985. }
  986.  
  987. static void
  988. gtk_file_selection_fileop_destroy (GtkWidget *widget, gpointer data)
  989. {
  990.   V99FileSelection *fs = data;
  991.  
  992.   g_return_if_fail (fs != NULL);
  993.   g_return_if_fail (V99_IS_FILE_SELECTION (fs));
  994.   
  995.   fs->fileop_dialog = NULL;
  996. }
  997.  
  998.  
  999. static void
  1000. gtk_file_selection_create_dir_confirmed (GtkWidget *widget, gpointer data)
  1001. {
  1002.   V99FileSelection *fs = data;
  1003.   gchar *dirname;
  1004.   gchar *path;
  1005.   gchar *full_path;
  1006.   gchar *buf;
  1007.   CompletionState *cmpl_state;
  1008.   
  1009.   g_return_if_fail (fs != NULL);
  1010.   g_return_if_fail (V99_IS_FILE_SELECTION (fs));
  1011.  
  1012.   dirname = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
  1013.   cmpl_state = (CompletionState*) fs->cmpl_state;
  1014.   path = cmpl_reference_position (cmpl_state);
  1015.   
  1016.   full_path = g_strconcat (path, G_DIR_SEPARATOR_S, dirname, NULL);
  1017.   if ( (mkdir (full_path, 0755) < 0) ) 
  1018.     {
  1019.       buf = g_strconcat ("Error creating directory \"", dirname, "\":  ", 
  1020.              g_strerror(errno), NULL);
  1021.       gtk_file_selection_fileop_error (fs, buf);
  1022.     }
  1023.   g_free (full_path);
  1024.   
  1025.   gtk_widget_destroy (fs->fileop_dialog);
  1026.   gtk_file_selection_populate (fs, "", FALSE);
  1027. }
  1028.   
  1029. static void
  1030. gtk_file_selection_create_dir (GtkWidget *widget, gpointer data)
  1031. {
  1032.   V99FileSelection *fs = data;
  1033.   GtkWidget *label;
  1034.   GtkWidget *dialog;
  1035.   GtkWidget *vbox;
  1036.   GtkWidget *button;
  1037.  
  1038.   g_return_if_fail (fs != NULL);
  1039.   g_return_if_fail (V99_IS_FILE_SELECTION (fs));
  1040.  
  1041.   if (fs->fileop_dialog)
  1042.       return;
  1043.   
  1044.   /* main dialog */
  1045.   fs->fileop_dialog = dialog = gtk_dialog_new ();
  1046.   gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
  1047.               (GtkSignalFunc) gtk_file_selection_fileop_destroy, 
  1048.               (gpointer) fs);
  1049.   gtk_window_set_title (GTK_WINDOW (dialog), _("Create Directory"));
  1050.   gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
  1051.  
  1052.   /* If file dialog is grabbed, grab option dialog */
  1053.   /* When option dialog is closed, file dialog will be grabbed again */
  1054.   if (GTK_WINDOW(fs)->modal)
  1055.       gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
  1056.  
  1057.   vbox = gtk_vbox_new(FALSE, 0);
  1058.   gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
  1059.   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
  1060.              FALSE, FALSE, 0);
  1061.   gtk_widget_show(vbox);
  1062.   
  1063.   label = gtk_label_new(_("Directory name:"));
  1064.   gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
  1065.   gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
  1066.   gtk_widget_show(label);
  1067.  
  1068.   /*  The directory entry widget  */
  1069.   fs->fileop_entry = gtk_entry_new ();
  1070.   gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry, 
  1071.               TRUE, TRUE, 5);
  1072.   GTK_WIDGET_SET_FLAGS(fs->fileop_entry, GTK_CAN_DEFAULT);
  1073.   gtk_widget_show (fs->fileop_entry);
  1074.   
  1075.   /* buttons */
  1076.   button = gtk_button_new_with_label (_("Create"));
  1077.   gtk_signal_connect (GTK_OBJECT (button), "clicked",
  1078.               (GtkSignalFunc) gtk_file_selection_create_dir_confirmed, 
  1079.               (gpointer) fs);
  1080.   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
  1081.              button, TRUE, TRUE, 0);
  1082.   GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
  1083.   gtk_widget_show(button);
  1084.   
  1085.   button = gtk_button_new_with_label (_("Cancel"));
  1086.   gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
  1087.                  (GtkSignalFunc) gtk_widget_destroy, 
  1088.                  (gpointer) dialog);
  1089.   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
  1090.              button, TRUE, TRUE, 0);
  1091.   GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
  1092.   gtk_widget_grab_default(button);
  1093.   gtk_widget_show (button);
  1094.  
  1095.   gtk_widget_show (dialog);
  1096. }
  1097.  
  1098. static void
  1099. gtk_file_selection_delete_file_confirmed (GtkWidget *widget, gpointer data)
  1100. {
  1101.   V99FileSelection *fs = data;
  1102.   CompletionState *cmpl_state;
  1103.   gchar *path;
  1104.   gchar *full_path;
  1105.   gchar *buf;
  1106.   
  1107.   g_return_if_fail (fs != NULL);
  1108.   g_return_if_fail (V99_IS_FILE_SELECTION (fs));
  1109.  
  1110.   cmpl_state = (CompletionState*) fs->cmpl_state;
  1111.   path = cmpl_reference_position (cmpl_state);
  1112.   
  1113.   full_path = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
  1114.   if ( (unlink (full_path) < 0) ) 
  1115.     {
  1116.       buf = g_strconcat ("Error deleting file \"", fs->fileop_file, "\":  ", 
  1117.              g_strerror(errno), NULL);
  1118.       gtk_file_selection_fileop_error (fs, buf);
  1119.     }
  1120.   g_free (full_path);
  1121.   
  1122.   gtk_widget_destroy (fs->fileop_dialog);
  1123.   gtk_file_selection_populate (fs, "", FALSE);
  1124. }
  1125.  
  1126. static void
  1127. gtk_file_selection_delete_file (GtkWidget *widget, gpointer data)
  1128. {
  1129.   V99FileSelection *fs = data;
  1130.   GtkWidget *label;
  1131.   GtkWidget *vbox;
  1132.   GtkWidget *button;
  1133.   GtkWidget *dialog;
  1134.   gchar *filename;
  1135.   gchar *buf;
  1136.   
  1137.   g_return_if_fail (fs != NULL);
  1138.   g_return_if_fail (V99_IS_FILE_SELECTION (fs));
  1139.  
  1140.   if (fs->fileop_dialog)
  1141.       return;
  1142.  
  1143. #ifdef G_WITH_CYGWIN
  1144.   translate_win32_path (fs);
  1145. #endif
  1146.  
  1147.   filename = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
  1148.   if (strlen(filename) < 1)
  1149.       return;
  1150.  
  1151.   fs->fileop_file = filename;
  1152.   
  1153.   /* main dialog */
  1154.   fs->fileop_dialog = dialog = gtk_dialog_new ();
  1155.   gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
  1156.               (GtkSignalFunc) gtk_file_selection_fileop_destroy, 
  1157.               (gpointer) fs);
  1158.   gtk_window_set_title (GTK_WINDOW (dialog), _("Delete File"));
  1159.   gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
  1160.  
  1161.   /* If file dialog is grabbed, grab option dialog */
  1162.   /* When option dialog is closed, file dialog will be grabbed again */
  1163.   if (GTK_WINDOW(fs)->modal)
  1164.       gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
  1165.   
  1166.   vbox = gtk_vbox_new(FALSE, 0);
  1167.   gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
  1168.   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
  1169.              FALSE, FALSE, 0);
  1170.   gtk_widget_show(vbox);
  1171.  
  1172.   buf = g_strconcat ("Really delete file \"", filename, "\" ?", NULL);
  1173.   label = gtk_label_new(buf);
  1174.   gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
  1175.   gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
  1176.   gtk_widget_show(label);
  1177.   g_free(buf);
  1178.   
  1179.   /* buttons */
  1180.   button = gtk_button_new_with_label (_("Delete"));
  1181.   gtk_signal_connect (GTK_OBJECT (button), "clicked",
  1182.               (GtkSignalFunc) gtk_file_selection_delete_file_confirmed, 
  1183.               (gpointer) fs);
  1184.   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
  1185.              button, TRUE, TRUE, 0);
  1186.   GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
  1187.   gtk_widget_show(button);
  1188.   
  1189.   button = gtk_button_new_with_label (_("Cancel"));
  1190.   gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
  1191.                  (GtkSignalFunc) gtk_widget_destroy, 
  1192.                  (gpointer) dialog);
  1193.   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
  1194.              button, TRUE, TRUE, 0);
  1195.   GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
  1196.   gtk_widget_grab_default(button);
  1197.   gtk_widget_show (button);
  1198.  
  1199.   gtk_widget_show (dialog);
  1200.  
  1201. }
  1202.  
  1203. static void
  1204. gtk_file_selection_rename_file_confirmed (GtkWidget *widget, gpointer data)
  1205. {
  1206.   V99FileSelection *fs = data;
  1207.   gchar *buf;
  1208.   gchar *file;
  1209.   gchar *path;
  1210.   gchar *new_filename;
  1211.   gchar *old_filename;
  1212.   CompletionState *cmpl_state;
  1213.   
  1214.   g_return_if_fail (fs != NULL);
  1215.   g_return_if_fail (V99_IS_FILE_SELECTION (fs));
  1216.  
  1217.   file = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
  1218.   cmpl_state = (CompletionState*) fs->cmpl_state;
  1219.   path = cmpl_reference_position (cmpl_state);
  1220.   
  1221.   new_filename = g_strconcat (path, G_DIR_SEPARATOR_S, file, NULL);
  1222.   old_filename = g_strconcat (path, G_DIR_SEPARATOR_S, fs->fileop_file, NULL);
  1223.  
  1224.   if ( (rename (old_filename, new_filename)) < 0) 
  1225.     {
  1226.       buf = g_strconcat ("Error renaming file \"", file, "\":  ", 
  1227.              g_strerror(errno), NULL);
  1228.       gtk_file_selection_fileop_error (fs, buf);
  1229.     }
  1230.   g_free (new_filename);
  1231.   g_free (old_filename);
  1232.   
  1233.   gtk_widget_destroy (fs->fileop_dialog);
  1234.   gtk_file_selection_populate (fs, "", FALSE);
  1235. }
  1236.   
  1237. static void
  1238. gtk_file_selection_rename_file (GtkWidget *widget, gpointer data)
  1239. {
  1240.   V99FileSelection *fs = data;
  1241.   GtkWidget *label;
  1242.   GtkWidget *dialog;
  1243.   GtkWidget *vbox;
  1244.   GtkWidget *button;
  1245.   gchar *buf;
  1246.   
  1247.   g_return_if_fail (fs != NULL);
  1248.   g_return_if_fail (V99_IS_FILE_SELECTION (fs));
  1249.  
  1250.   if (fs->fileop_dialog)
  1251.       return;
  1252.  
  1253.   fs->fileop_file = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
  1254.   if (strlen(fs->fileop_file) < 1)
  1255.       return;
  1256.   
  1257.   /* main dialog */
  1258.   fs->fileop_dialog = dialog = gtk_dialog_new ();
  1259.   gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
  1260.               (GtkSignalFunc) gtk_file_selection_fileop_destroy, 
  1261.               (gpointer) fs);
  1262.   gtk_window_set_title (GTK_WINDOW (dialog), _("Rename File"));
  1263.   gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
  1264.  
  1265.   /* If file dialog is grabbed, grab option dialog */
  1266.   /* When option dialog  closed, file dialog will be grabbed again */
  1267.   if (GTK_WINDOW(fs)->modal)
  1268.     gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
  1269.   
  1270.   vbox = gtk_vbox_new(FALSE, 0);
  1271.   gtk_container_set_border_width (GTK_CONTAINER(vbox), 8);
  1272.   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
  1273.              FALSE, FALSE, 0);
  1274.   gtk_widget_show(vbox);
  1275.   
  1276.   buf = g_strconcat ("Rename file \"", fs->fileop_file, "\" to:", NULL);
  1277.   label = gtk_label_new(buf);
  1278.   gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
  1279.   gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
  1280.   gtk_widget_show(label);
  1281.   g_free(buf);
  1282.  
  1283.   /* New filename entry */
  1284.   fs->fileop_entry = gtk_entry_new ();
  1285.   gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry, 
  1286.               TRUE, TRUE, 5);
  1287.   GTK_WIDGET_SET_FLAGS(fs->fileop_entry, GTK_CAN_DEFAULT);
  1288.   gtk_widget_show (fs->fileop_entry);
  1289.   
  1290.   gtk_entry_set_text (GTK_ENTRY (fs->fileop_entry), fs->fileop_file);
  1291.   gtk_editable_select_region (GTK_EDITABLE (fs->fileop_entry),
  1292.                   0, strlen (fs->fileop_file));
  1293.  
  1294.   /* buttons */
  1295.   button = gtk_button_new_with_label (_("Rename"));
  1296.   gtk_signal_connect (GTK_OBJECT (button), "clicked",
  1297.               (GtkSignalFunc) gtk_file_selection_rename_file_confirmed, 
  1298.               (gpointer) fs);
  1299.   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
  1300.              button, TRUE, TRUE, 0);
  1301.   GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
  1302.   gtk_widget_show(button);
  1303.   
  1304.   button = gtk_button_new_with_label (_("Cancel"));
  1305.   gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
  1306.                  (GtkSignalFunc) gtk_widget_destroy, 
  1307.                  (gpointer) dialog);
  1308.   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
  1309.              button, TRUE, TRUE, 0);
  1310.   GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
  1311.   gtk_widget_grab_default(button);
  1312.   gtk_widget_show (button);
  1313.  
  1314.   gtk_widget_show (dialog);
  1315. }
  1316.  
  1317.  
  1318. static gint
  1319. gtk_file_selection_key_press (GtkWidget   *widget,
  1320.                   GdkEventKey *event,
  1321.                   gpointer     user_data)
  1322. {
  1323.   V99FileSelection *fs;
  1324.   char *text;
  1325.  
  1326.   g_return_val_if_fail (widget != NULL, FALSE);
  1327.   g_return_val_if_fail (event != NULL, FALSE);
  1328.  
  1329.   if (event->keyval == GDK_Tab)
  1330.     {
  1331.       fs = V99_FILE_SELECTION (user_data);
  1332. #ifdef G_WITH_CYGWIN
  1333.       translate_win32_path(fs);
  1334. #endif
  1335.       text = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
  1336.  
  1337.       text = g_strdup (text);
  1338.  
  1339.       gtk_file_selection_populate (fs, text, TRUE);
  1340.  
  1341.       g_free (text);
  1342.  
  1343.       gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "key_press_event");
  1344.  
  1345.       return TRUE;
  1346.     }
  1347.  
  1348.   return FALSE;
  1349. }
  1350.  
  1351.  
  1352. static void
  1353. gtk_file_selection_history_callback (GtkWidget *widget, gpointer data)
  1354. {
  1355.   V99FileSelection *fs = data;
  1356.   HistoryCallbackArg *callback_arg;
  1357.   GList *list;
  1358.  
  1359.   g_return_if_fail (fs != NULL);
  1360.   g_return_if_fail (V99_IS_FILE_SELECTION (fs));
  1361.  
  1362.   list = fs->history_list;
  1363.   
  1364.   while (list) {
  1365.     callback_arg = list->data;
  1366.     
  1367.     if (callback_arg->menu_item == widget)
  1368.       {
  1369.     gtk_file_selection_populate (fs, callback_arg->directory, FALSE);
  1370.     break;
  1371.       }
  1372.     
  1373.     list = list->next;
  1374.   }
  1375. }
  1376.  
  1377. static void 
  1378. gtk_file_selection_update_history_menu (V99FileSelection *fs,
  1379.                     gchar *current_directory)
  1380. {
  1381.   HistoryCallbackArg *callback_arg;
  1382.   GtkWidget *menu_item;
  1383.   GList *list;
  1384.   gchar *current_dir;
  1385.   gint dir_len;
  1386.   gint i;
  1387.   
  1388.   g_return_if_fail (fs != NULL);
  1389.   g_return_if_fail (V99_IS_FILE_SELECTION (fs));
  1390.   g_return_if_fail (current_directory != NULL);
  1391.   
  1392.   list = fs->history_list;
  1393.  
  1394.   if (fs->history_menu) 
  1395.     {
  1396.       while (list) {
  1397.     callback_arg = list->data;
  1398.     g_free (callback_arg->directory);
  1399.     g_free (callback_arg);
  1400.     list = list->next;
  1401.       }
  1402.       g_list_free (fs->history_list);
  1403.       fs->history_list = NULL;
  1404.       
  1405.       gtk_widget_destroy (fs->history_menu);
  1406.     }
  1407.   
  1408.   fs->history_menu = gtk_menu_new();
  1409.  
  1410.   current_dir = g_strdup (current_directory);
  1411.  
  1412.   dir_len = strlen (current_dir);
  1413.  
  1414.   for (i = dir_len; i >= 0; i--)
  1415.     {
  1416.       /* the i == dir_len is to catch the full path for the first 
  1417.        * entry. */
  1418.       if ( (current_dir[i] == G_DIR_SEPARATOR) || (i == dir_len))
  1419.     {
  1420.       /* another small hack to catch the full path */
  1421.       if (i != dir_len) 
  1422.           current_dir[i + 1] = '\0';
  1423. #ifdef G_WITH_CYGWIN
  1424.       if (!strcmp(current_dir, "//"))
  1425.         continue;
  1426. #endif
  1427.       menu_item = gtk_menu_item_new_with_label (current_dir);
  1428.       
  1429.       callback_arg = g_new (HistoryCallbackArg, 1);
  1430.       callback_arg->menu_item = menu_item;
  1431.       
  1432.       /* since the autocompletion gets confused if you don't 
  1433.        * supply a trailing '/' on a dir entry, set the full
  1434.        * (current) path to "" which just refreshes the filesel */
  1435.       if (dir_len == i)
  1436.         {
  1437.         callback_arg->directory = g_strdup ("");
  1438.         }
  1439.       else
  1440.         {
  1441.         callback_arg->directory = g_strdup (current_dir);
  1442.       }
  1443.       
  1444.       fs->history_list = g_list_append (fs->history_list, callback_arg);
  1445.       
  1446.       gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
  1447.                   (GtkSignalFunc) gtk_file_selection_history_callback,
  1448.                   (gpointer) fs);
  1449.       gtk_menu_append (GTK_MENU (fs->history_menu), menu_item);
  1450.       gtk_widget_show (menu_item);
  1451.     }
  1452.     }
  1453.  
  1454.   gtk_option_menu_set_menu (GTK_OPTION_MENU (fs->history_pulldown), 
  1455.                 fs->history_menu);
  1456.   g_free (current_dir);
  1457. }
  1458.  
  1459. static void
  1460. gtk_file_selection_file_button (GtkWidget *widget,
  1461.                    gint row, 
  1462.                    gint column, 
  1463.                    GdkEventButton *bevent,
  1464.                    gpointer user_data)
  1465. {
  1466.   V99FileSelection *fs = NULL;
  1467.   gchar *filename, *temp = NULL;
  1468.   
  1469.   g_return_if_fail (GTK_IS_CLIST (widget));
  1470.  
  1471.   fs = user_data;
  1472.   g_return_if_fail (fs != NULL);
  1473.   g_return_if_fail (V99_IS_FILE_SELECTION (fs));
  1474.   
  1475.   if (fs->user_file_get) {
  1476.         fs->user_file_get(fs, GTK_CLIST(fs->file_list), row, &temp);
  1477.   } else {
  1478.       gtk_clist_get_text (GTK_CLIST (fs->file_list), row, 0, &temp);
  1479.   }
  1480.   filename = g_strdup (temp);
  1481.  
  1482. #ifdef G_WITH_CYGWIN
  1483.   /* Check to see if the selection was a drive selector */
  1484.   if (isalpha(filename[0]) && (filename[1] == ':')) {
  1485.     /* It is... map it to a CYGWIN32 drive */
  1486.     char temp_filename[10];
  1487.  
  1488.     sprintf(temp_filename, "//%c/", tolower(filename[0]));
  1489.     g_free(filename);
  1490.     filename = g_strdup(temp_filename);
  1491.   }
  1492. #endif /* G_WITH_CYGWIN */
  1493.  
  1494.   if (filename)
  1495.     {
  1496.       if (bevent)
  1497.     switch (bevent->type)
  1498.       {
  1499.       case GDK_2BUTTON_PRESS:
  1500.         gtk_button_clicked (GTK_BUTTON (fs->ok_button));
  1501.         break;
  1502.         
  1503.       default:
  1504.         gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
  1505.         break;
  1506.       }
  1507.       else
  1508.     gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
  1509.  
  1510.       g_free (filename);
  1511.     }
  1512. }
  1513.  
  1514. static void
  1515. gtk_file_selection_dir_button (GtkWidget *widget,
  1516.                    gint row, 
  1517.                    gint column, 
  1518.                    GdkEventButton *bevent,
  1519.                    gpointer user_data)
  1520. {
  1521.   V99FileSelection *fs = NULL;
  1522.   gchar *filename, *temp = NULL;
  1523.  
  1524.   g_return_if_fail (GTK_IS_CLIST (widget));
  1525.  
  1526.   fs = V99_FILE_SELECTION (user_data);
  1527.   g_return_if_fail (fs != NULL);
  1528.   g_return_if_fail (V99_IS_FILE_SELECTION (fs));
  1529.  
  1530.   if (fs->user_file_get) {
  1531.       fs->user_file_get(fs, GTK_CLIST (fs->file_list), row, &temp);
  1532.   } else {
  1533.       gtk_clist_get_text (GTK_CLIST (fs->dir_list), row, 0, &temp);
  1534.   }
  1535.   
  1536.   filename = g_strdup (temp);
  1537.  
  1538.   if (filename)
  1539.     {
  1540.       if (bevent)
  1541.     switch (bevent->type)
  1542.       {
  1543.       case GDK_2BUTTON_PRESS:
  1544.         gtk_file_selection_populate (fs, filename, FALSE);
  1545.         break;
  1546.       
  1547.       default:
  1548.         gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
  1549.         break;
  1550.       }
  1551.       else
  1552.     gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
  1553.  
  1554.       g_free (filename);
  1555.     }
  1556. }
  1557.  
  1558. #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
  1559.  
  1560. static void
  1561. win32_gtk_add_drives_to_dir_list(GtkWidget *the_dir_list)
  1562. {
  1563.   gchar *text[2], *textPtr;
  1564.   gchar buffer[128];
  1565.   char volumeNameBuf[128];
  1566.   char formatBuffer[128];
  1567.   gint row;
  1568.  
  1569.   text[1] = NULL;
  1570.  
  1571.   /* Get the Drives string */
  1572.   GetLogicalDriveStrings(sizeof(buffer), buffer);
  1573.  
  1574.   /* Add the drives as necessary */
  1575.   textPtr = buffer;
  1576.   while (*textPtr != '\0') {
  1577.     /* Get the volume information for this drive */
  1578.     if ((tolower(textPtr[0]) != 'a') && (tolower(textPtr[0]) != 'b'))
  1579.       {
  1580.     /* Ignore floppies (?) */
  1581.     DWORD maxComponentLength, flags;
  1582.  
  1583.     GetVolumeInformation(textPtr,
  1584.                   volumeNameBuf, sizeof(volumeNameBuf),
  1585.                   NULL, &maxComponentLength,
  1586.                   &flags, NULL, 0);
  1587.     /* Build the actual displayable string */
  1588.  
  1589.     sprintf(formatBuffer, "%c:\\", toupper(textPtr[0]));
  1590. #if 0 /* HB: removed to allow drive change AND directory update with one click */
  1591.     if (strlen(volumeNameBuf) > 0)
  1592.       sprintf(formatBuffer, "%s (%s)", formatBuffer, volumeNameBuf);
  1593. #endif
  1594.     /* Add to the list */
  1595.     text[0] = formatBuffer;
  1596.     row = gtk_clist_append (GTK_CLIST (the_dir_list), text);
  1597.       }
  1598.     textPtr += (strlen(textPtr) + 1);
  1599.   }
  1600. }
  1601. #endif
  1602.  
  1603. static void
  1604. gtk_file_selection_populate (V99FileSelection *fs,
  1605.                  gchar            *rel_path,
  1606.                  gint              try_complete)
  1607. {
  1608.   CompletionState *cmpl_state;
  1609.   PossibleCompletion* poss;
  1610.   gchar* filename;
  1611.   gint row;
  1612.   gchar* rem_path = rel_path;
  1613.   gchar* sel_text;
  1614.   gchar* text[2];
  1615.   gint did_recurse = FALSE;
  1616.   gint possible_count = 0;
  1617.   gint selection_index = -1;
  1618.   gint file_list_width;
  1619.   gint dir_list_width;
  1620.   
  1621.   g_return_if_fail (fs != NULL);
  1622.   g_return_if_fail (V99_IS_FILE_SELECTION (fs));
  1623.   
  1624.   cmpl_state = (CompletionState*) fs->cmpl_state;
  1625.   poss = cmpl_completion_matches (rel_path, &rem_path, cmpl_state);
  1626.  
  1627.   if (!cmpl_state_okay (cmpl_state))
  1628.     {
  1629.       /* Something went wrong. */
  1630.       gtk_file_selection_abort (fs);
  1631.       return;
  1632.     }
  1633.  
  1634.   g_assert (cmpl_state->reference_dir);
  1635.  
  1636.   gtk_clist_freeze (GTK_CLIST (fs->dir_list));
  1637.   gtk_clist_clear (GTK_CLIST (fs->dir_list));
  1638.   gtk_clist_freeze (GTK_CLIST (fs->file_list));
  1639.   gtk_clist_clear (GTK_CLIST (fs->file_list));
  1640.  
  1641.   /* Set the dir_list to include ./ and ../ */
  1642.   text[1] = NULL;
  1643.   text[0] = "." G_DIR_SEPARATOR_S;
  1644.   row = gtk_clist_append (GTK_CLIST (fs->dir_list), text);
  1645.  
  1646.   text[0] = ".." G_DIR_SEPARATOR_S;
  1647.   row = gtk_clist_append (GTK_CLIST (fs->dir_list), text);
  1648.  
  1649.   /*reset the max widths of the lists*/
  1650.   dir_list_width = gdk_string_width(fs->dir_list->style->font,".." G_DIR_SEPARATOR_S);
  1651.   gtk_clist_set_column_width(GTK_CLIST(fs->dir_list),0,dir_list_width);
  1652.   file_list_width = 1;
  1653.   gtk_clist_set_column_width(GTK_CLIST(fs->file_list),0,file_list_width);
  1654.  
  1655.   while (poss)
  1656.     {
  1657.       if (cmpl_is_a_completion (poss))
  1658.         {
  1659.           possible_count += 1;
  1660.  
  1661.           filename = cmpl_this_completion (poss);
  1662.  
  1663.       text[0] = filename;
  1664.       
  1665.           if (cmpl_is_directory (poss))
  1666.             {
  1667.               if (strcmp (filename, "." G_DIR_SEPARATOR_S) != 0 &&
  1668.                   strcmp (filename, ".." G_DIR_SEPARATOR_S) != 0)
  1669.         {
  1670.           int width = gdk_string_width(fs->dir_list->style->font,
  1671.                            filename);
  1672.           row = gtk_clist_append (GTK_CLIST (fs->dir_list), text);
  1673.           if(width > dir_list_width)
  1674.             {
  1675.               dir_list_width = width;
  1676.               gtk_clist_set_column_width(GTK_CLIST(fs->dir_list),0,
  1677.                          width);
  1678.             }
  1679.          }
  1680.         }
  1681.           else
  1682.         {
  1683.           if (fs->user_file_append) {
  1684.               row = fs->user_file_append(fs, GTK_CLIST (fs->file_list), 
  1685.                                          cmpl_reference_position (cmpl_state),
  1686.                                          filename);
  1687.           } else {
  1688.               int width = gdk_string_width(fs->file_list->style->font,
  1689.                                            filename);
  1690.           
  1691.               row = gtk_clist_append (GTK_CLIST (fs->file_list), text);
  1692.               if(width > file_list_width)
  1693.               {
  1694.                   file_list_width = width;
  1695.                   gtk_clist_set_column_width(GTK_CLIST(fs->file_list),0,
  1696.                                              width);
  1697.               }
  1698.           }
  1699.  
  1700.             }
  1701.     }
  1702.  
  1703.       poss = cmpl_next_completion (cmpl_state);
  1704.     }
  1705.  
  1706. #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
  1707.   /* For Windows, add drives as potential selections */
  1708.   win32_gtk_add_drives_to_dir_list (fs->dir_list);
  1709. #endif
  1710.  
  1711.   gtk_clist_thaw (GTK_CLIST (fs->dir_list));
  1712.   gtk_clist_thaw (GTK_CLIST (fs->file_list));
  1713.  
  1714.   /* File lists are set. */
  1715.  
  1716.   g_assert (cmpl_state->reference_dir);
  1717.  
  1718.   if (try_complete)
  1719.     {
  1720.  
  1721.       /* User is trying to complete filenames, so advance the user's input
  1722.        * string to the updated_text, which is the common leading substring
  1723.        * of all possible completions, and if its a directory attempt
  1724.        * attempt completions in it. */
  1725.  
  1726.       if (cmpl_updated_text (cmpl_state)[0])
  1727.         {
  1728.  
  1729.           if (cmpl_updated_dir (cmpl_state))
  1730.             {
  1731.           gchar* dir_name = g_strdup (cmpl_updated_text (cmpl_state));
  1732.  
  1733.               did_recurse = TRUE;
  1734.  
  1735.               gtk_file_selection_populate (fs, dir_name, TRUE);
  1736.  
  1737.               g_free (dir_name);
  1738.             }
  1739.           else
  1740.             {
  1741.           if (fs->selection_entry)
  1742.               gtk_entry_set_text (GTK_ENTRY (fs->selection_entry),
  1743.                       cmpl_updated_text (cmpl_state));
  1744.             }
  1745.         }
  1746.       else
  1747.         {
  1748.           selection_index = cmpl_last_valid_char (cmpl_state) -
  1749.                             (strlen (rel_path) - strlen (rem_path));
  1750.       if (fs->selection_entry)
  1751.         gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), rem_path);
  1752.         }
  1753.     }
  1754.   else
  1755.     {
  1756.       if (fs->selection_entry)
  1757.     gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
  1758.     }
  1759.  
  1760.   if (!did_recurse)
  1761.     {
  1762.       if (fs->selection_entry)
  1763.     gtk_entry_set_position (GTK_ENTRY (fs->selection_entry), selection_index);
  1764.  
  1765.       if (fs->selection_entry)
  1766.     {
  1767.       sel_text = g_strconcat (_("Selection: "),
  1768.                   cmpl_reference_position (cmpl_state),
  1769.                   NULL);
  1770.  
  1771.       gtk_label_set_text (GTK_LABEL (fs->selection_text), sel_text);
  1772.       g_free (sel_text);
  1773.     }
  1774.  
  1775.       if (fs->history_pulldown) 
  1776.     {
  1777.       gtk_file_selection_update_history_menu (fs, cmpl_reference_position (cmpl_state));
  1778.     }
  1779.       
  1780.     }
  1781. }
  1782.  
  1783. static void
  1784. gtk_file_selection_abort (V99FileSelection *fs)
  1785. {
  1786.   gchar err_buf[256];
  1787.  
  1788.   sprintf (err_buf, _("Directory unreadable: %s"), cmpl_strerror (cmpl_errno));
  1789.  
  1790.   /*  BEEP gdk_beep();  */
  1791.  
  1792.   if (fs->selection_entry)
  1793.     gtk_label_set_text (GTK_LABEL (fs->selection_text), err_buf);
  1794. }
  1795.  
  1796. /**********************************************************************/
  1797. /*              External Interface                          */
  1798. /**********************************************************************/
  1799.  
  1800. /* The four completion state selectors
  1801.  */
  1802. static gchar*
  1803. cmpl_updated_text (CompletionState* cmpl_state)
  1804. {
  1805.   return cmpl_state->updated_text;
  1806. }
  1807.  
  1808. static gboolean
  1809. cmpl_updated_dir (CompletionState* cmpl_state)
  1810. {
  1811.   return cmpl_state->re_complete;
  1812. }
  1813.  
  1814. static gchar*
  1815. cmpl_reference_position (CompletionState* cmpl_state)
  1816. {
  1817.   return cmpl_state->reference_dir->fullname;
  1818. }
  1819.  
  1820. static gint
  1821. cmpl_last_valid_char (CompletionState* cmpl_state)
  1822. {
  1823.   return cmpl_state->last_valid_char;
  1824. }
  1825.  
  1826. static gchar*
  1827. cmpl_completion_fullname (gchar* text, CompletionState* cmpl_state)
  1828. {
  1829.   static char nothing[2] = "";
  1830.  
  1831.   if (!cmpl_state_okay (cmpl_state))
  1832.     {
  1833.       return nothing;
  1834.     }
  1835.   else if (g_path_is_absolute (text))
  1836.     {
  1837.       strcpy (cmpl_state->updated_text, text);
  1838.     }
  1839. #ifdef HAVE_PWD_H
  1840.   else if (text[0] == '~')
  1841.     {
  1842.       CompletionDir* dir;
  1843.       char* slash;
  1844.  
  1845.       dir = open_user_dir (text, cmpl_state);
  1846.  
  1847.       if (!dir)
  1848.     {
  1849.       /* spencer says just return ~something, so
  1850.        * for now just do it. */
  1851.       strcpy (cmpl_state->updated_text, text);
  1852.     }
  1853.       else
  1854.     {
  1855.  
  1856.       strcpy (cmpl_state->updated_text, dir->fullname);
  1857.  
  1858.       slash = strchr (text, G_DIR_SEPARATOR);
  1859.  
  1860.       if (slash)
  1861.         strcat (cmpl_state->updated_text, slash);
  1862.     }
  1863.     }
  1864. #endif
  1865.   else
  1866.     {
  1867.       strcpy (cmpl_state->updated_text, cmpl_state->reference_dir->fullname);
  1868.       if (cmpl_state->updated_text[strlen (cmpl_state->updated_text) - 1] != G_DIR_SEPARATOR)
  1869.     strcat (cmpl_state->updated_text, G_DIR_SEPARATOR_S);
  1870.       strcat (cmpl_state->updated_text, text);
  1871.     }
  1872.  
  1873.   return cmpl_state->updated_text;
  1874. }
  1875.  
  1876. /* The three completion selectors
  1877.  */
  1878. static gchar*
  1879. cmpl_this_completion (PossibleCompletion* pc)
  1880. {
  1881.   return pc->text;
  1882. }
  1883.  
  1884. static gboolean
  1885. cmpl_is_directory (PossibleCompletion* pc)
  1886. {
  1887.   return pc->is_directory;
  1888. }
  1889.  
  1890. static gint
  1891. cmpl_is_a_completion (PossibleCompletion* pc)
  1892. {
  1893.   return pc->is_a_completion;
  1894. }
  1895.  
  1896. /**********************************************************************/
  1897. /*                     Construction, deletion                       */
  1898. /**********************************************************************/
  1899.  
  1900. static CompletionState*
  1901. cmpl_init_state (void)
  1902. {
  1903.   gchar *getcwd_buf;
  1904.   CompletionState *new_state;
  1905.  
  1906.   new_state = g_new (CompletionState, 1);
  1907.  
  1908.   getcwd_buf = g_get_current_dir ();
  1909.  
  1910. tryagain:
  1911.  
  1912.   new_state->reference_dir = NULL;
  1913.   new_state->completion_dir = NULL;
  1914.   new_state->active_completion_dir = NULL;
  1915.   new_state->directory_storage = NULL;
  1916.   new_state->directory_sent_storage = NULL;
  1917.   new_state->last_valid_char = 0;
  1918.   new_state->updated_text = g_new (gchar, MAXPATHLEN);
  1919.   new_state->updated_text_alloc = MAXPATHLEN;
  1920.   new_state->the_completion.text = g_new (gchar, MAXPATHLEN);
  1921.   new_state->the_completion.text_alloc = MAXPATHLEN;
  1922.   new_state->user_dir_name_buffer = NULL;
  1923.   new_state->user_directories = NULL;
  1924.  
  1925.   new_state->reference_dir =  open_dir (getcwd_buf, new_state);
  1926.  
  1927.   if (!new_state->reference_dir)
  1928.     {
  1929.       /* Directories changing from underneath us, grumble */
  1930.       strcpy (getcwd_buf, G_DIR_SEPARATOR_S);
  1931.       goto tryagain;
  1932.     }
  1933.  
  1934.   g_free (getcwd_buf);
  1935.   return new_state;
  1936. }
  1937.  
  1938. static void
  1939. cmpl_free_dir_list(GList* dp0)
  1940. {
  1941.   GList *dp = dp0;
  1942.  
  1943.   while (dp) {
  1944.     free_dir (dp->data);
  1945.     dp = dp->next;
  1946.   }
  1947.  
  1948.   g_list_free(dp0);
  1949. }
  1950.  
  1951. static void
  1952. cmpl_free_dir_sent_list(GList* dp0)
  1953. {
  1954.   GList *dp = dp0;
  1955.  
  1956.   while (dp) {
  1957.     free_dir_sent (dp->data);
  1958.     dp = dp->next;
  1959.   }
  1960.  
  1961.   g_list_free(dp0);
  1962. }
  1963.  
  1964. static void
  1965. cmpl_free_state (CompletionState* cmpl_state)
  1966. {
  1967.   cmpl_free_dir_list (cmpl_state->directory_storage);
  1968.   cmpl_free_dir_sent_list (cmpl_state->directory_sent_storage);
  1969.  
  1970.   if (cmpl_state->user_dir_name_buffer)
  1971.     g_free (cmpl_state->user_dir_name_buffer);
  1972.   if (cmpl_state->user_directories)
  1973.     g_free (cmpl_state->user_directories);
  1974.   if (cmpl_state->the_completion.text)
  1975.     g_free (cmpl_state->the_completion.text);
  1976.   if (cmpl_state->updated_text)
  1977.     g_free (cmpl_state->updated_text);
  1978.  
  1979.   g_free (cmpl_state);
  1980. }
  1981.  
  1982. static void
  1983. free_dir(CompletionDir* dir)
  1984. {
  1985.   g_free(dir->fullname);
  1986.   g_free(dir);
  1987. }
  1988.  
  1989. static void
  1990. free_dir_sent(CompletionDirSent* sent)
  1991. {
  1992.   gint i;
  1993.   for (i = 0; i < sent->entry_count; i++)
  1994.     g_free(sent->entries[i].entry_name);
  1995.   g_free(sent->entries);
  1996.   g_free(sent);
  1997. }
  1998.  
  1999. static void
  2000. prune_memory_usage(CompletionState *cmpl_state)
  2001. {
  2002.   GList* cdsl = cmpl_state->directory_sent_storage;
  2003.   GList* cdl = cmpl_state->directory_storage;
  2004.   GList* cdl0 = cdl;
  2005.   gint len = 0;
  2006.  
  2007.   for(; cdsl && len < CMPL_DIRECTORY_CACHE_SIZE; len += 1)
  2008.     cdsl = cdsl->next;
  2009.  
  2010.   if (cdsl) {
  2011.     cmpl_free_dir_sent_list(cdsl->next);
  2012.     cdsl->next = NULL;
  2013.   }
  2014.  
  2015.   cmpl_state->directory_storage = NULL;
  2016.   while (cdl) {
  2017.     if (cdl->data == cmpl_state->reference_dir)
  2018.       cmpl_state->directory_storage = g_list_prepend(NULL, cdl->data);
  2019.     else
  2020.       free_dir (cdl->data);
  2021.     cdl = cdl->next;
  2022.   }
  2023.  
  2024.   g_list_free(cdl0);
  2025. }
  2026.  
  2027. /**********************************************************************/
  2028. /*                        The main entrances.                         */
  2029. /**********************************************************************/
  2030.  
  2031. static PossibleCompletion*
  2032. cmpl_completion_matches (gchar* text_to_complete,
  2033.              gchar** remaining_text,
  2034.              CompletionState* cmpl_state)
  2035. {
  2036.   gchar* first_slash;
  2037.   PossibleCompletion *poss;
  2038.  
  2039.   prune_memory_usage(cmpl_state);
  2040.  
  2041.   g_assert (text_to_complete != NULL);
  2042.  
  2043.   cmpl_state->user_completion_index = -1;
  2044.   cmpl_state->last_completion_text = text_to_complete;
  2045.   cmpl_state->the_completion.text[0] = 0;
  2046.   cmpl_state->last_valid_char = 0;
  2047.   cmpl_state->updated_text_len = -1;
  2048.   cmpl_state->updated_text[0] = 0;
  2049.   cmpl_state->re_complete = FALSE;
  2050.  
  2051. #ifdef HAVE_PWD_H
  2052.   first_slash = strchr (text_to_complete, G_DIR_SEPARATOR);
  2053.  
  2054.   if (text_to_complete[0] == '~' && !first_slash)
  2055.     {
  2056.       /* Text starts with ~ and there is no slash, show all the
  2057.        * home directory completions.
  2058.        */
  2059.       poss = attempt_homedir_completion (text_to_complete, cmpl_state);
  2060.  
  2061.       update_cmpl(poss, cmpl_state);
  2062.  
  2063.       return poss;
  2064.     }
  2065. #endif
  2066.   cmpl_state->reference_dir =
  2067.     open_ref_dir (text_to_complete, remaining_text, cmpl_state);
  2068.  
  2069.   if(!cmpl_state->reference_dir)
  2070.     return NULL;
  2071.  
  2072.   cmpl_state->completion_dir =
  2073.     find_completion_dir (*remaining_text, remaining_text, cmpl_state);
  2074.  
  2075.   cmpl_state->last_valid_char = *remaining_text - text_to_complete;
  2076.  
  2077.   if(!cmpl_state->completion_dir)
  2078.     return NULL;
  2079.  
  2080.   cmpl_state->completion_dir->cmpl_index = -1;
  2081.   cmpl_state->completion_dir->cmpl_parent = NULL;
  2082.   cmpl_state->completion_dir->cmpl_text = *remaining_text;
  2083.  
  2084.   cmpl_state->active_completion_dir = cmpl_state->completion_dir;
  2085.  
  2086.   cmpl_state->reference_dir = cmpl_state->completion_dir;
  2087.  
  2088.   poss = attempt_file_completion(cmpl_state);
  2089.  
  2090.   update_cmpl(poss, cmpl_state);
  2091.  
  2092.   return poss;
  2093. }
  2094.  
  2095. static PossibleCompletion*
  2096. cmpl_next_completion (CompletionState* cmpl_state)
  2097. {
  2098.   PossibleCompletion* poss = NULL;
  2099.  
  2100.   cmpl_state->the_completion.text[0] = 0;
  2101.  
  2102. #ifdef HAVE_PWD_H
  2103.   if(cmpl_state->user_completion_index >= 0)
  2104.     poss = attempt_homedir_completion(cmpl_state->last_completion_text, cmpl_state);
  2105.   else
  2106.     poss = attempt_file_completion(cmpl_state);
  2107. #else
  2108.   poss = attempt_file_completion(cmpl_state);
  2109. #endif
  2110.  
  2111.   update_cmpl(poss, cmpl_state);
  2112.  
  2113.   return poss;
  2114. }
  2115.  
  2116. /**********************************************************************/
  2117. /*             Directory Operations                         */
  2118. /**********************************************************************/
  2119.  
  2120. /* Open the directory where completion will begin from, if possible. */
  2121. static CompletionDir*
  2122. open_ref_dir(gchar* text_to_complete,
  2123.          gchar** remaining_text,
  2124.          CompletionState* cmpl_state)
  2125. {
  2126.   gchar* first_slash;
  2127.   CompletionDir *new_dir;
  2128.  
  2129.   first_slash = strchr(text_to_complete, G_DIR_SEPARATOR);
  2130.  
  2131. #ifdef G_WITH_CYGWIN
  2132.   if (text_to_complete[0] == '/' && text_to_complete[1] == '/')
  2133.     {
  2134.       char root_dir[5];
  2135.       sprintf(root_dir, "//%c", text_to_complete[2]);
  2136.  
  2137.       new_dir = open_dir(root_dir, cmpl_state);
  2138.  
  2139.       if (new_dir) {
  2140.     *remaining_text = text_to_complete + 4;
  2141.       }
  2142.     }
  2143. #else
  2144.   if (FALSE)
  2145.     ;
  2146. #endif
  2147. #ifdef HAVE_PWD_H
  2148.   else if (text_to_complete[0] == '~')
  2149.     {
  2150.       new_dir = open_user_dir(text_to_complete, cmpl_state);
  2151.  
  2152.       if(new_dir)
  2153.     {
  2154.       if(first_slash)
  2155.         *remaining_text = first_slash + 1;
  2156.       else
  2157.         *remaining_text = text_to_complete + strlen(text_to_complete);
  2158.     }
  2159.       else
  2160.     {
  2161.       return NULL;
  2162.     }
  2163.     }
  2164. #endif
  2165.   else if (g_path_is_absolute (text_to_complete) || !cmpl_state->reference_dir)
  2166.     {
  2167.       gchar *tmp = g_strdup(text_to_complete);
  2168.       gchar *p;
  2169.  
  2170.       p = tmp;
  2171.       while (*p && *p != '*' && *p != '?')
  2172.     p++;
  2173.  
  2174.       *p = '\0';
  2175.       p = strrchr(tmp, G_DIR_SEPARATOR);
  2176.       if (p)
  2177.     {
  2178.       if (p == tmp)
  2179.         p++;
  2180.       
  2181.       *p = '\0';
  2182.  
  2183.       new_dir = open_dir(tmp, cmpl_state);
  2184.  
  2185.       if(new_dir)
  2186.         *remaining_text = text_to_complete + 
  2187.           ((p == tmp + 1) ? (p - tmp) : (p + 1 - tmp));
  2188.     }
  2189.       else
  2190.     {
  2191.       /* If no possible candidates, use the cwd */
  2192.       gchar *curdir = g_get_current_dir ();
  2193.       
  2194.       new_dir = open_dir(curdir, cmpl_state);
  2195.  
  2196.       if (new_dir)
  2197.         *remaining_text = text_to_complete;
  2198.  
  2199.       g_free (curdir);
  2200.     }
  2201.  
  2202.       g_free (tmp);
  2203.     }
  2204.   else
  2205.     {
  2206.       *remaining_text = text_to_complete;
  2207.  
  2208.       new_dir = open_dir(cmpl_state->reference_dir->fullname, cmpl_state);
  2209.     }
  2210.  
  2211.   if(new_dir)
  2212.     {
  2213.       new_dir->cmpl_index = -1;
  2214.       new_dir->cmpl_parent = NULL;
  2215.     }
  2216.  
  2217.   return new_dir;
  2218. }
  2219.  
  2220. #ifdef HAVE_PWD_H
  2221.  
  2222. /* open a directory by user name */
  2223. static CompletionDir*
  2224. open_user_dir(gchar* text_to_complete,
  2225.           CompletionState *cmpl_state)
  2226. {
  2227.   gchar *first_slash;
  2228.   gint cmp_len;
  2229.  
  2230.   g_assert(text_to_complete && text_to_complete[0] == '~');
  2231.  
  2232.   first_slash = strchr(text_to_complete, G_DIR_SEPARATOR);
  2233.  
  2234.   if (first_slash)
  2235.     cmp_len = first_slash - text_to_complete - 1;
  2236.   else
  2237.     cmp_len = strlen(text_to_complete + 1);
  2238.  
  2239.   if(!cmp_len)
  2240.     {
  2241.       /* ~/ */
  2242.       gchar *homedir = g_get_home_dir ();
  2243.  
  2244.       if (homedir)
  2245.     return open_dir(homedir, cmpl_state);
  2246.       else
  2247.     return NULL;
  2248.     }
  2249.   else
  2250.     {
  2251.       /* ~user/ */
  2252.       char* copy = g_new(char, cmp_len + 1);
  2253.       struct passwd *pwd;
  2254.       strncpy(copy, text_to_complete + 1, cmp_len);
  2255.       copy[cmp_len] = 0;
  2256.       pwd = getpwnam(copy);
  2257.       g_free(copy);
  2258.       if (!pwd)
  2259.     {
  2260.       cmpl_errno = errno;
  2261.       return NULL;
  2262.     }
  2263.  
  2264.       return open_dir(pwd->pw_dir, cmpl_state);
  2265.     }
  2266. }
  2267.  
  2268. #endif
  2269.  
  2270. /* open a directory relative the the current relative directory */
  2271. static CompletionDir*
  2272. open_relative_dir(gchar* dir_name,
  2273.           CompletionDir* dir,
  2274.           CompletionState *cmpl_state)
  2275. {
  2276.   CompletionDir *result;
  2277.   GString *path;
  2278.  
  2279.   path = g_string_sized_new (dir->fullname_len + strlen (dir_name) + 10);
  2280.   g_string_assign (path, dir->fullname);
  2281.  
  2282.   if(dir->fullname_len > 1
  2283.     && path->str[dir->fullname_len - 1] != G_DIR_SEPARATOR)
  2284.      g_string_append_c (path, G_DIR_SEPARATOR);
  2285.   g_string_append (path, dir_name);
  2286.  
  2287.   result = open_dir(path->str, cmpl_state);
  2288.  
  2289.   g_string_free (path, TRUE);
  2290.  
  2291.   return result;
  2292. }
  2293.  
  2294. /* after the cache lookup fails, really open a new directory */
  2295. static CompletionDirSent*
  2296. open_new_dir(gchar* dir_name, struct stat* sbuf, gboolean stat_subdirs)
  2297. {
  2298.   CompletionDirSent* sent;
  2299.   DIR* directory;
  2300.   struct dirent *dirent_ptr;
  2301.   gint entry_count = 0;
  2302.   gint i;
  2303.   struct stat ent_sbuf;
  2304.   GString *path;
  2305.   gchar *xdir;
  2306.     
  2307.   sent = g_new(CompletionDirSent, 1);
  2308.   sent->mtime = sbuf->st_mtime;
  2309.   sent->inode = sbuf->st_ino;
  2310.   sent->device = sbuf->st_dev;
  2311.  
  2312.   path = g_string_sized_new (2*MAXPATHLEN + 10);
  2313.  
  2314.   xdir = g_filename_from_utf8 (dir_name);
  2315.   directory = opendir(xdir);
  2316.   g_free (xdir);
  2317.  
  2318.   if(!directory)
  2319.     {
  2320.       cmpl_errno = errno;
  2321.       return NULL;
  2322.     }
  2323.  
  2324.   while((dirent_ptr = readdir(directory)) != NULL)
  2325.     {
  2326.       entry_count++;
  2327.     }
  2328.  
  2329.   sent->entries = g_new(CompletionDirEntry, entry_count);
  2330.   sent->entry_count = entry_count;
  2331.  
  2332.   rewinddir(directory);
  2333.  
  2334.   for(i = 0; i < entry_count; i += 1)
  2335.     {
  2336.       dirent_ptr = readdir(directory);
  2337.  
  2338.       if(!dirent_ptr)
  2339.     {
  2340.       cmpl_errno = errno;
  2341.       closedir(directory);
  2342.       return NULL;
  2343.     }
  2344.  
  2345.       sent->entries[i].entry_name = g_filename_to_utf8 (dirent_ptr->d_name);
  2346.  
  2347.       g_string_assign (path, dir_name);
  2348.       if (path->str[path->len-1] != G_DIR_SEPARATOR)
  2349.     {
  2350.       g_string_append_c (path, G_DIR_SEPARATOR);
  2351.     }
  2352.       g_string_append (path, dirent_ptr->d_name);
  2353.  
  2354.       if (stat_subdirs)
  2355.     {
  2356.       if(stat(path->str, &ent_sbuf) >= 0 && S_ISDIR(ent_sbuf.st_mode))
  2357.         sent->entries[i].is_dir = TRUE;
  2358.       else
  2359.         /* stat may fail, and we don't mind, since it could be a
  2360.          * dangling symlink. */
  2361.         sent->entries[i].is_dir = FALSE;
  2362.     }
  2363.       else
  2364.     sent->entries[i].is_dir = 1;
  2365.     }
  2366.  
  2367.   g_string_free (path, TRUE);
  2368.   qsort(sent->entries, sent->entry_count, sizeof(CompletionDirEntry), compare_cmpl_dir);
  2369.  
  2370.   closedir(directory);
  2371.  
  2372.   return sent;
  2373. }
  2374.  
  2375. #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
  2376.  
  2377. static gboolean
  2378. check_dir(gchar *dir_name, struct stat *result, gboolean *stat_subdirs)
  2379. {
  2380.   /* A list of directories that we know only contain other directories.
  2381.    * Trying to stat every file in these directories would be very
  2382.    * expensive.
  2383.    */
  2384.  
  2385.   static struct {
  2386.     gchar *name;
  2387.     gboolean present;
  2388.     struct stat statbuf;
  2389.   } no_stat_dirs[] = {
  2390.     { "/afs", FALSE, { 0 } },
  2391.     { "/net", FALSE, { 0 } }
  2392.   };
  2393.  
  2394.   static const gint n_no_stat_dirs = sizeof(no_stat_dirs) / sizeof(no_stat_dirs[0]);
  2395.   static gboolean initialized = FALSE;
  2396.  
  2397.   gint i;
  2398.  
  2399.   if (!initialized)
  2400.     {
  2401.       initialized = TRUE;
  2402.       for (i = 0; i < n_no_stat_dirs; i++)
  2403.     {
  2404.       if (stat (no_stat_dirs[i].name, &no_stat_dirs[i].statbuf) == 0)
  2405.         no_stat_dirs[i].present = TRUE;
  2406.     }
  2407.     }
  2408.  
  2409.   if(stat(dir_name, result) < 0)
  2410.     {
  2411.       cmpl_errno = errno;
  2412.       return FALSE;
  2413.     }
  2414.  
  2415.   *stat_subdirs = TRUE;
  2416.   for (i=0; i<n_no_stat_dirs; i++)
  2417.     {
  2418.       if (no_stat_dirs[i].present &&
  2419.       (no_stat_dirs[i].statbuf.st_dev == result->st_dev) &&
  2420.       (no_stat_dirs[i].statbuf.st_ino == result->st_ino))
  2421.     {
  2422.       *stat_subdirs = FALSE;
  2423.       break;
  2424.     }
  2425.     }
  2426.  
  2427.   return TRUE;
  2428. }
  2429.  
  2430. #endif
  2431.  
  2432. /* open a directory by absolute pathname */
  2433. static CompletionDir*
  2434. open_dir(gchar* dir_name, CompletionState* cmpl_state)
  2435. {
  2436.   struct stat sbuf;
  2437.   gboolean stat_subdirs;
  2438.   CompletionDirSent *sent;
  2439.   GList* cdsl;
  2440.  
  2441. #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
  2442.   if (!check_dir (dir_name, &sbuf, &stat_subdirs))
  2443.     return NULL;
  2444.  
  2445.   cdsl = cmpl_state->directory_sent_storage;
  2446.  
  2447.   while (cdsl)
  2448.     {
  2449.       sent = cdsl->data;
  2450.  
  2451.       if(sent->inode == sbuf.st_ino &&
  2452.      sent->mtime == sbuf.st_mtime &&
  2453.      sent->device == sbuf.st_dev)
  2454.     return attach_dir(sent, dir_name, cmpl_state);
  2455.  
  2456.       cdsl = cdsl->next;
  2457.     }
  2458. #else
  2459.   stat_subdirs = TRUE;
  2460. #endif
  2461.  
  2462.   sent = open_new_dir(dir_name, &sbuf, stat_subdirs);
  2463.  
  2464.   if (sent) {
  2465.     cmpl_state->directory_sent_storage =
  2466.       g_list_prepend(cmpl_state->directory_sent_storage, sent);
  2467.  
  2468.     return attach_dir(sent, dir_name, cmpl_state);
  2469.   }
  2470.  
  2471.   return NULL;
  2472. }
  2473.  
  2474. static CompletionDir*
  2475. attach_dir(CompletionDirSent* sent, gchar* dir_name, CompletionState *cmpl_state)
  2476. {
  2477.   CompletionDir* new_dir;
  2478.  
  2479.   new_dir = g_new(CompletionDir, 1);
  2480.  
  2481.   cmpl_state->directory_storage =
  2482.     g_list_prepend(cmpl_state->directory_storage, new_dir);
  2483.  
  2484.   new_dir->sent = sent;
  2485.   new_dir->fullname = g_strdup(dir_name);
  2486.   new_dir->fullname_len = strlen(dir_name);
  2487.  
  2488.   return new_dir;
  2489. }
  2490.  
  2491. static gint
  2492. correct_dir_fullname(CompletionDir* cmpl_dir)
  2493. {
  2494.   gint length = strlen(cmpl_dir->fullname);
  2495.   gchar *first_slash = strchr(cmpl_dir->fullname, G_DIR_SEPARATOR);
  2496.   struct stat sbuf;
  2497.  
  2498.   /* Does it end with /. (\.) ? */
  2499.   if (length >= 2 &&
  2500.       strcmp(cmpl_dir->fullname + length - 2, G_DIR_SEPARATOR_S ".") == 0)
  2501.     {
  2502.       /* Is it just the root directory (on a drive) ? */
  2503.       if (cmpl_dir->fullname + length - 2 == first_slash)
  2504.     {
  2505.       cmpl_dir->fullname[length - 1] = 0;
  2506.       cmpl_dir->fullname_len = length - 1;
  2507.       return TRUE;
  2508.     }
  2509.       else
  2510.     {
  2511.       cmpl_dir->fullname[length - 2] = 0;
  2512.     }
  2513.     }
  2514.  
  2515.   /* Ends with /./ (\.\)? */
  2516.   else if (length >= 3 &&
  2517.        strcmp(cmpl_dir->fullname + length - 3,
  2518.           G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S) == 0)
  2519.     cmpl_dir->fullname[length - 2] = 0;
  2520.  
  2521.   /* Ends with /.. (\..) ? */
  2522.   else if (length >= 3 &&
  2523.        strcmp(cmpl_dir->fullname + length - 3,
  2524.           G_DIR_SEPARATOR_S "..") == 0)
  2525.     {
  2526.       /* Is it just /.. (X:\..)? */
  2527.       if(cmpl_dir->fullname + length - 3 == first_slash)
  2528.     {
  2529.       cmpl_dir->fullname[length - 2] = 0;
  2530.       cmpl_dir->fullname_len = length - 2;
  2531.       return TRUE;
  2532.     }
  2533.  
  2534.       if(stat(cmpl_dir->fullname, &sbuf) < 0)
  2535.     {
  2536.       cmpl_errno = errno;
  2537.       return FALSE;
  2538.     }
  2539.  
  2540.       cmpl_dir->fullname[length - 3] = 0;
  2541.  
  2542.       if(!correct_parent(cmpl_dir, &sbuf))
  2543.     return FALSE;
  2544.     }
  2545.  
  2546.   /* Ends with /../ (\..\)? */
  2547.   else if (length >= 4 &&
  2548.        strcmp(cmpl_dir->fullname + length - 4,
  2549.           G_DIR_SEPARATOR_S ".." G_DIR_SEPARATOR_S) == 0)
  2550.     {
  2551.       /* Is it just /../ (X:\..\)? */
  2552.       if(cmpl_dir->fullname + length - 4 == first_slash)
  2553.     {
  2554.       cmpl_dir->fullname[length - 3] = 0;
  2555.       cmpl_dir->fullname_len = length - 3;
  2556.       return TRUE;
  2557.     }
  2558.  
  2559.       if(stat(cmpl_dir->fullname, &sbuf) < 0)
  2560.     {
  2561.       cmpl_errno = errno;
  2562.       return FALSE;
  2563.     }
  2564.  
  2565.       cmpl_dir->fullname[length - 4] = 0;
  2566.  
  2567.       if(!correct_parent(cmpl_dir, &sbuf))
  2568.     return FALSE;
  2569.     }
  2570.  
  2571.   cmpl_dir->fullname_len = strlen(cmpl_dir->fullname);
  2572.  
  2573.   return TRUE;
  2574. }
  2575.  
  2576. static gint
  2577. correct_parent(CompletionDir* cmpl_dir, struct stat *sbuf)
  2578. {
  2579.   struct stat parbuf;
  2580.   gchar *last_slash;
  2581.   gchar *first_slash;
  2582.   gchar *new_name;
  2583.   gchar c = 0;
  2584.  
  2585.   last_slash = strrchr(cmpl_dir->fullname, G_DIR_SEPARATOR);
  2586.   g_assert(last_slash);
  2587.   first_slash = strchr(cmpl_dir->fullname, G_DIR_SEPARATOR);
  2588.  
  2589.   /* Clever (?) way to check for top-level directory that works also on
  2590.    * Win32, where there is a drive letter and colon prefixed...
  2591.    */
  2592.   if (last_slash != first_slash)
  2593.     {
  2594.       last_slash[0] = 0;
  2595.     }
  2596.   else
  2597.     {
  2598.       c = last_slash[1];
  2599.       last_slash[1] = 0;
  2600.     }
  2601.  
  2602.   if (stat(cmpl_dir->fullname, &parbuf) < 0)
  2603.     {
  2604.       cmpl_errno = errno;
  2605.       if (!c)
  2606.     last_slash[0] = G_DIR_SEPARATOR;
  2607.       return FALSE;
  2608.     }
  2609.  
  2610. #ifndef G_OS_WIN32        /* No inode numbers on Win32 */
  2611.   if (parbuf.st_ino == sbuf->st_ino && parbuf.st_dev == sbuf->st_dev)
  2612.     /* it wasn't a link */
  2613.     return TRUE;
  2614.  
  2615.   if(c)
  2616.     last_slash[1] = c;
  2617.   else
  2618.     last_slash[0] = G_DIR_SEPARATOR;
  2619.  
  2620.   /* it was a link, have to figure it out the hard way */
  2621.  
  2622.   new_name = find_parent_dir_fullname(cmpl_dir->fullname);
  2623.  
  2624.   if (!new_name)
  2625.     return FALSE;
  2626.  
  2627.   g_free(cmpl_dir->fullname);
  2628.  
  2629.   cmpl_dir->fullname = new_name;
  2630. #endif
  2631.  
  2632.   return TRUE;
  2633. }
  2634.  
  2635. #ifndef G_OS_WIN32
  2636.  
  2637. static gchar*
  2638. find_parent_dir_fullname(gchar* dirname)
  2639. {
  2640.   gchar *orig_dir;
  2641.   gchar *result;
  2642.  
  2643.   orig_dir = g_get_current_dir ();
  2644.  
  2645.   if(chdir(dirname) != 0 || chdir("..") != 0)
  2646.     {
  2647.       cmpl_errno = errno;
  2648.       return NULL;
  2649.     }
  2650.  
  2651.   result = g_get_current_dir ();
  2652.  
  2653.   if(chdir(orig_dir) != 0)
  2654.     {
  2655.       cmpl_errno = errno;
  2656.       return NULL;
  2657.     }
  2658.  
  2659.   g_free (orig_dir);
  2660.   return result;
  2661. }
  2662.  
  2663. #endif
  2664.  
  2665. /**********************************************************************/
  2666. /*                        Completion Operations                       */
  2667. /**********************************************************************/
  2668.  
  2669. #ifdef HAVE_PWD_H
  2670.  
  2671. static PossibleCompletion*
  2672. attempt_homedir_completion(gchar* text_to_complete,
  2673.                CompletionState *cmpl_state)
  2674. {
  2675.   gint index, length;
  2676.  
  2677.   if (!cmpl_state->user_dir_name_buffer &&
  2678.       !get_pwdb(cmpl_state))
  2679.     return NULL;
  2680.   length = strlen(text_to_complete) - 1;
  2681.  
  2682.   cmpl_state->user_completion_index += 1;
  2683.  
  2684.   while(cmpl_state->user_completion_index < cmpl_state->user_directories_len)
  2685.     {
  2686.       index = first_diff_index(text_to_complete + 1,
  2687.                    cmpl_state->user_directories
  2688.                    [cmpl_state->user_completion_index].login);
  2689.  
  2690.       switch(index)
  2691.     {
  2692.     case PATTERN_MATCH:
  2693.       break;
  2694.     default:
  2695.       if(cmpl_state->last_valid_char < (index + 1))
  2696.         cmpl_state->last_valid_char = index + 1;
  2697.       cmpl_state->user_completion_index += 1;
  2698.       continue;
  2699.     }
  2700.  
  2701.       cmpl_state->the_completion.is_a_completion = 1;
  2702.       cmpl_state->the_completion.is_directory = TRUE;
  2703.  
  2704.       append_completion_text("~", cmpl_state);
  2705.  
  2706.       append_completion_text(cmpl_state->
  2707.                   user_directories[cmpl_state->user_completion_index].login,
  2708.                  cmpl_state);
  2709.  
  2710.       return append_completion_text(G_DIR_SEPARATOR_S, cmpl_state);
  2711.     }
  2712.  
  2713.   if(text_to_complete[1] ||
  2714.      cmpl_state->user_completion_index > cmpl_state->user_directories_len)
  2715.     {
  2716.       cmpl_state->user_completion_index = -1;
  2717.       return NULL;
  2718.     }
  2719.   else
  2720.     {
  2721.       cmpl_state->user_completion_index += 1;
  2722.       cmpl_state->the_completion.is_a_completion = 1;
  2723.       cmpl_state->the_completion.is_directory = TRUE;
  2724.  
  2725.       return append_completion_text("~" G_DIR_SEPARATOR_S, cmpl_state);
  2726.     }
  2727. }
  2728.  
  2729. #endif
  2730.  
  2731. #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
  2732. #define FOLD(c) (tolower(c))
  2733. #else
  2734. #define FOLD(c) (c)
  2735. #endif
  2736.  
  2737. /* returns the index (>= 0) of the first differing character,
  2738.  * PATTERN_MATCH if the completion matches */
  2739. static gint
  2740. first_diff_index(gchar* pat, gchar* text)
  2741. {
  2742.   gint diff = 0;
  2743.  
  2744.   while(*pat && *text && FOLD(*text) == FOLD(*pat))
  2745.     {
  2746.       pat += 1;
  2747.       text += 1;
  2748.       diff += 1;
  2749.     }
  2750.  
  2751.   if(*pat)
  2752.     return diff;
  2753.  
  2754.   return PATTERN_MATCH;
  2755. }
  2756.  
  2757. static PossibleCompletion*
  2758. append_completion_text(gchar* text, CompletionState* cmpl_state)
  2759. {
  2760.   gint len, i = 1;
  2761.  
  2762.   if(!cmpl_state->the_completion.text)
  2763.     return NULL;
  2764.  
  2765.   len = strlen(text) + strlen(cmpl_state->the_completion.text) + 1;
  2766.  
  2767.   if(cmpl_state->the_completion.text_alloc > len)
  2768.     {
  2769.       strcat(cmpl_state->the_completion.text, text);
  2770.       return &cmpl_state->the_completion;
  2771.     }
  2772.  
  2773.   while(i < len) { i <<= 1; }
  2774.  
  2775.   cmpl_state->the_completion.text_alloc = i;
  2776.  
  2777.   cmpl_state->the_completion.text = (gchar*)g_realloc(cmpl_state->the_completion.text, i);
  2778.  
  2779.   if(!cmpl_state->the_completion.text)
  2780.     return NULL;
  2781.   else
  2782.     {
  2783.       strcat(cmpl_state->the_completion.text, text);
  2784.       return &cmpl_state->the_completion;
  2785.     }
  2786. }
  2787.  
  2788. static CompletionDir*
  2789. find_completion_dir(gchar* text_to_complete,
  2790.             gchar** remaining_text,
  2791.             CompletionState* cmpl_state)
  2792. {
  2793.   gchar* first_slash = strchr(text_to_complete, G_DIR_SEPARATOR);
  2794.   CompletionDir* dir = cmpl_state->reference_dir;
  2795.   CompletionDir* next;
  2796.   *remaining_text = text_to_complete;
  2797.  
  2798.   while(first_slash)
  2799.     {
  2800.       gint len = first_slash - *remaining_text;
  2801.       gint found = 0;
  2802.       gchar *found_name = NULL;         /* Quiet gcc */
  2803.       gint i;
  2804.       gchar* pat_buf = g_new (gchar, len + 1);
  2805.  
  2806.       strncpy(pat_buf, *remaining_text, len);
  2807.       pat_buf[len] = 0;
  2808.  
  2809.       for(i = 0; i < dir->sent->entry_count; i += 1)
  2810.     {
  2811.       if(dir->sent->entries[i].is_dir &&
  2812.          fnmatch(pat_buf, dir->sent->entries[i].entry_name,
  2813.              FNMATCH_FLAGS)!= FNM_NOMATCH)
  2814.         {
  2815.           if(found)
  2816.         {
  2817.           g_free (pat_buf);
  2818.           return dir;
  2819.         }
  2820.           else
  2821.         {
  2822.           found = 1;
  2823.           found_name = dir->sent->entries[i].entry_name;
  2824.         }
  2825.         }
  2826.     }
  2827.  
  2828.       if (!found)
  2829.     {
  2830.       /* Perhaps we are trying to open an automount directory */
  2831.       found_name = pat_buf;
  2832.     }
  2833.  
  2834.       next = open_relative_dir(found_name, dir, cmpl_state);
  2835.       
  2836.       if(!next)
  2837.     {
  2838.       g_free (pat_buf);
  2839.       return NULL;
  2840.     }
  2841.       
  2842.       next->cmpl_parent = dir;
  2843.       
  2844.       dir = next;
  2845.       
  2846.       if(!correct_dir_fullname(dir))
  2847.     {
  2848.       g_free(pat_buf);
  2849.       return NULL;
  2850.     }
  2851.       
  2852.       *remaining_text = first_slash + 1;
  2853.       first_slash = strchr(*remaining_text, G_DIR_SEPARATOR);
  2854.  
  2855.       g_free (pat_buf);
  2856.     }
  2857.  
  2858.   return dir;
  2859. }
  2860.  
  2861. static void
  2862. update_cmpl(PossibleCompletion* poss, CompletionState* cmpl_state)
  2863. {
  2864.   gint cmpl_len;
  2865.  
  2866.   if(!poss || !cmpl_is_a_completion(poss))
  2867.     return;
  2868.  
  2869.   cmpl_len = strlen(cmpl_this_completion(poss));
  2870.  
  2871.   if(cmpl_state->updated_text_alloc < cmpl_len + 1)
  2872.     {
  2873.       cmpl_state->updated_text =
  2874.     (gchar*)g_realloc(cmpl_state->updated_text,
  2875.               cmpl_state->updated_text_alloc);
  2876.       cmpl_state->updated_text_alloc = 2*cmpl_len;
  2877.     }
  2878.  
  2879.   if(cmpl_state->updated_text_len < 0)
  2880.     {
  2881.       strcpy(cmpl_state->updated_text, cmpl_this_completion(poss));
  2882.       cmpl_state->updated_text_len = cmpl_len;
  2883.       cmpl_state->re_complete = cmpl_is_directory(poss);
  2884.     }
  2885.   else if(cmpl_state->updated_text_len == 0)
  2886.     {
  2887.       cmpl_state->re_complete = FALSE;
  2888.     }
  2889.   else
  2890.     {
  2891.       gint first_diff =
  2892.     first_diff_index(cmpl_state->updated_text,
  2893.              cmpl_this_completion(poss));
  2894.  
  2895.       cmpl_state->re_complete = FALSE;
  2896.  
  2897.       if(first_diff == PATTERN_MATCH)
  2898.     return;
  2899.  
  2900.       if(first_diff > cmpl_state->updated_text_len)
  2901.     strcpy(cmpl_state->updated_text, cmpl_this_completion(poss));
  2902.  
  2903.       cmpl_state->updated_text_len = first_diff;
  2904.       cmpl_state->updated_text[first_diff] = 0;
  2905.     }
  2906. }
  2907.  
  2908. static PossibleCompletion*
  2909. attempt_file_completion(CompletionState *cmpl_state)
  2910. {
  2911.   gchar *pat_buf, *first_slash;
  2912.   CompletionDir *dir = cmpl_state->active_completion_dir;
  2913.  
  2914.   dir->cmpl_index += 1;
  2915.  
  2916.   if(dir->cmpl_index == dir->sent->entry_count)
  2917.     {
  2918.       if(dir->cmpl_parent == NULL)
  2919.     {
  2920.       cmpl_state->active_completion_dir = NULL;
  2921.  
  2922.       return NULL;
  2923.     }
  2924.       else
  2925.     {
  2926.       cmpl_state->active_completion_dir = dir->cmpl_parent;
  2927.  
  2928.       return attempt_file_completion(cmpl_state);
  2929.     }
  2930.     }
  2931.  
  2932.   g_assert(dir->cmpl_text);
  2933.  
  2934.   first_slash = strchr(dir->cmpl_text, G_DIR_SEPARATOR);
  2935.  
  2936.   if(first_slash)
  2937.     {
  2938.       gint len = first_slash - dir->cmpl_text;
  2939.  
  2940.       pat_buf = g_new (gchar, len + 1);
  2941.       strncpy(pat_buf, dir->cmpl_text, len);
  2942.       pat_buf[len] = 0;
  2943.     }
  2944.   else
  2945.     {
  2946.       gint len = strlen(dir->cmpl_text);
  2947.  
  2948.       pat_buf = g_new (gchar, len + 2);
  2949.       strcpy(pat_buf, dir->cmpl_text);
  2950.       /* Don't append a * if the user entered one herself.
  2951.        * This way one can complete *.h and don't get matches
  2952.        * on any .help files, for instance.
  2953.        */
  2954.       if (strchr(pat_buf, '*') == NULL)
  2955.       strcpy(pat_buf + len, "*");
  2956.     }
  2957.  
  2958.   if(first_slash)
  2959.     {
  2960.       if(dir->sent->entries[dir->cmpl_index].is_dir)
  2961.     {
  2962.       if(fnmatch(pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
  2963.              FNMATCH_FLAGS) != FNM_NOMATCH)
  2964.         {
  2965.           CompletionDir* new_dir;
  2966.  
  2967.           new_dir = open_relative_dir(dir->sent->entries[dir->cmpl_index].entry_name,
  2968.                       dir, cmpl_state);
  2969.  
  2970.           if(!new_dir)
  2971.         {
  2972.           g_free (pat_buf);
  2973.           return NULL;
  2974.         }
  2975.  
  2976.           new_dir->cmpl_parent = dir;
  2977.  
  2978.           new_dir->cmpl_index = -1;
  2979.           new_dir->cmpl_text = first_slash + 1;
  2980.  
  2981.           cmpl_state->active_completion_dir = new_dir;
  2982.  
  2983.           g_free (pat_buf);
  2984.           return attempt_file_completion(cmpl_state);
  2985.         }
  2986.       else
  2987.         {
  2988.           g_free (pat_buf);
  2989.           return attempt_file_completion(cmpl_state);
  2990.         }
  2991.     }
  2992.       else
  2993.     {
  2994.       g_free (pat_buf);
  2995.       return attempt_file_completion(cmpl_state);
  2996.     }
  2997.     }
  2998.   else
  2999.     {
  3000.       if(dir->cmpl_parent != NULL)
  3001.     {
  3002.       append_completion_text(dir->fullname +
  3003.                  strlen(cmpl_state->completion_dir->fullname) + 1,
  3004.                  cmpl_state);
  3005.       append_completion_text(G_DIR_SEPARATOR_S, cmpl_state);
  3006.     }
  3007.  
  3008.       append_completion_text(dir->sent->entries[dir->cmpl_index].entry_name, cmpl_state);
  3009.  
  3010.       cmpl_state->the_completion.is_a_completion =
  3011.     (fnmatch(pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
  3012.          FNMATCH_FLAGS) != FNM_NOMATCH);
  3013.  
  3014.       cmpl_state->the_completion.is_directory = dir->sent->entries[dir->cmpl_index].is_dir;
  3015.       if(dir->sent->entries[dir->cmpl_index].is_dir)
  3016.     append_completion_text(G_DIR_SEPARATOR_S, cmpl_state);
  3017.  
  3018.       g_free (pat_buf);
  3019.       return &cmpl_state->the_completion;
  3020.     }
  3021. }
  3022.  
  3023. #ifdef HAVE_PWD_H
  3024.  
  3025. static gint
  3026. get_pwdb(CompletionState* cmpl_state)
  3027. {
  3028.   struct passwd *pwd_ptr;
  3029.   gchar* buf_ptr;
  3030.   gint len = 0, i, count = 0;
  3031.  
  3032.   if(cmpl_state->user_dir_name_buffer)
  3033.     return TRUE;
  3034.   setpwent ();
  3035.  
  3036.   while ((pwd_ptr = getpwent()) != NULL)
  3037.     {
  3038.       len += strlen(pwd_ptr->pw_name);
  3039.       len += strlen(pwd_ptr->pw_dir);
  3040.       len += 2;
  3041.       count += 1;
  3042.     }
  3043.  
  3044.   setpwent ();
  3045.  
  3046.   cmpl_state->user_dir_name_buffer = g_new(gchar, len);
  3047.   cmpl_state->user_directories = g_new(CompletionUserDir, count);
  3048.   cmpl_state->user_directories_len = count;
  3049.  
  3050.   buf_ptr = cmpl_state->user_dir_name_buffer;
  3051.  
  3052.   for(i = 0; i < count; i += 1)
  3053.     {
  3054.       pwd_ptr = getpwent();
  3055.       if(!pwd_ptr)
  3056.     {
  3057.       cmpl_errno = errno;
  3058.       goto error;
  3059.     }
  3060.  
  3061.       strcpy(buf_ptr, pwd_ptr->pw_name);
  3062.       cmpl_state->user_directories[i].login = buf_ptr;
  3063.       buf_ptr += strlen(buf_ptr);
  3064.       buf_ptr += 1;
  3065.       strcpy(buf_ptr, pwd_ptr->pw_dir);
  3066.       cmpl_state->user_directories[i].homedir = buf_ptr;
  3067.       buf_ptr += strlen(buf_ptr);
  3068.       buf_ptr += 1;
  3069.     }
  3070.  
  3071.   qsort(cmpl_state->user_directories,
  3072.     cmpl_state->user_directories_len,
  3073.     sizeof(CompletionUserDir),
  3074.     compare_user_dir);
  3075.  
  3076.   endpwent();
  3077.  
  3078.   return TRUE;
  3079.  
  3080. error:
  3081.  
  3082.   if(cmpl_state->user_dir_name_buffer)
  3083.     g_free(cmpl_state->user_dir_name_buffer);
  3084.   if(cmpl_state->user_directories)
  3085.     g_free(cmpl_state->user_directories);
  3086.  
  3087.   cmpl_state->user_dir_name_buffer = NULL;
  3088.   cmpl_state->user_directories = NULL;
  3089.  
  3090.   return FALSE;
  3091. }
  3092.  
  3093. static gint
  3094. compare_user_dir(const void* a, const void* b)
  3095. {
  3096.   return strcmp((((CompletionUserDir*)a))->login,
  3097.         (((CompletionUserDir*)b))->login);
  3098. }
  3099.  
  3100. #endif
  3101.  
  3102. static gint
  3103. compare_cmpl_dir(const void* a, const void* b)
  3104. {
  3105. #if !defined(G_OS_WIN32) && !defined(G_WITH_CYGWIN)
  3106.   return strcmp((((CompletionDirEntry*)a))->entry_name,
  3107.         (((CompletionDirEntry*)b))->entry_name);
  3108. #else
  3109.   return g_strcasecmp((((CompletionDirEntry*)a))->entry_name,
  3110.               (((CompletionDirEntry*)b))->entry_name);
  3111. #endif
  3112. }
  3113.  
  3114. static gint
  3115. cmpl_state_okay(CompletionState* cmpl_state)
  3116. {
  3117.   return  cmpl_state && cmpl_state->reference_dir;
  3118. }
  3119.  
  3120. static gchar*
  3121. cmpl_strerror(gint err)
  3122. {
  3123.   if(err == CMPL_ERRNO_TOO_LONG)
  3124.     return "Name too long";
  3125.   else
  3126.     return g_strerror (err);
  3127. }
  3128.