gboolean edit_files (const GList * files) { static gboolean made_directory = FALSE; WDialog *edit_dlg; WMenuBar *menubar; const GList *file; gboolean ok = FALSE; if (!made_directory) { char *dir; dir = mc_build_filename (mc_config_get_cache_path (), EDIT_DIR, NULL); made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST); g_free (dir); dir = mc_build_filename (mc_config_get_path (), EDIT_DIR, NULL); made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST); g_free (dir); dir = mc_build_filename (mc_config_get_data_path (), EDIT_DIR, NULL); made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST); g_free (dir); } /* Create a new dialog and add it widgets to it */ edit_dlg = dlg_create (FALSE, 0, 0, LINES, COLS, NULL, edit_dialog_callback, edit_dialog_event, "[Internal File Editor]", NULL, DLG_WANT_TAB); edit_dlg->get_shortcut = edit_get_shortcut; edit_dlg->get_title = edit_get_title; menubar = menubar_new (0, 0, COLS, NULL, TRUE); add_widget (edit_dlg, menubar); edit_init_menu (menubar); add_widget (edit_dlg, buttonbar_new (TRUE)); for (file = files; file != NULL; file = g_list_next (file)) { Widget *w = WIDGET (edit_dlg); mcedit_arg_t *f = (mcedit_arg_t *) file->data; gboolean f_ok; f_ok = edit_add_window (edit_dlg, w->y + 1, w->x, w->lines - 2, w->cols, f->file_vpath, f->line_number); /* at least one file has been opened succefully */ ok = ok || f_ok; } if (ok) dlg_run (edit_dlg); if (!ok || edit_dlg->state == DLG_CLOSED) dlg_destroy (edit_dlg); return ok; }
void show_datadirs_extended (void) { PRINTF_GROUP (_("System data")); PRINTF_SECTION (_("Config directory:"), mc_global.sysconfig_dir); PRINTF_SECTION (_("Data directory:"), mc_global.share_data_dir); #if defined ENABLE_VFS_EXTFS || defined ENABLE_VFS_FISH PRINTF_SECTION (_("VFS plugins and scripts:"), LIBEXECDIR); #ifdef ENABLE_VFS_EXTFS PRINTF2 ("extfs.d:", LIBEXECDIR, MC_EXTFS_DIR "/"); #endif #ifdef ENABLE_VFS_FISH PRINTF2 ("fish:", LIBEXECDIR, FISH_PREFIX "/"); #endif #endif /* ENABLE_VFS_EXTFS || defiined ENABLE_VFS_FISH */ (void) puts (""); PRINTF_GROUP (_("User data")); PRINTF_SECTION2 (_("Config directory:"), mc_config_get_path ()); PRINTF_SECTION2 (_("Data directory:"), mc_config_get_data_path ()); PRINTF ("skins:", mc_config_get_data_path (), MC_SKINS_SUBDIR "/"); #ifdef ENABLE_VFS_EXTFS PRINTF ("extfs.d:", mc_config_get_data_path (), MC_EXTFS_DIR "/"); #endif #ifdef ENABLE_VFS_FISH PRINTF ("fish:", mc_config_get_data_path (), FISH_PREFIX "/"); #endif PRINTF_SECTION2 (_("Cache directory:"), mc_config_get_cache_path ()); }
/** * Write history to the ${XDG_CACHE_HOME}/mc/history file */ void dlg_save_history (Dlg_head * h) { char *profile; int i; if (num_history_items_recorded == 0) /* this is how to disable */ return; profile = g_build_filename (mc_config_get_cache_path (), MC_HISTORY_FILE, (char *) NULL); i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); if (i != -1) close (i); /* Make sure the history is only readable by the user */ if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT) { ev_history_load_save_t event_data; event_data.cfg = mc_config_init (profile); event_data.receiver = NULL; /* get all histories in dialog */ mc_event_raise (h->event_group, MCEVENT_HISTORY_SAVE, &event_data); mc_config_save_file (event_data.cfg, NULL); mc_config_deinit (event_data.cfg); } g_free (profile); }
int edit_file (const char *_file, int line) { static gboolean made_directory = FALSE; Dlg_head *edit_dlg; WEdit *wedit; WMenuBar *menubar; if (!made_directory) { char *dir = concat_dir_and_file (mc_config_get_cache_path (), EDIT_DIR); made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST); g_free (dir); dir = concat_dir_and_file (mc_config_get_path (), EDIT_DIR); made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST); g_free (dir); dir = concat_dir_and_file (mc_config_get_data_path (), EDIT_DIR); made_directory = (mkdir (dir, 0700) != -1 || errno == EEXIST); g_free (dir); } wedit = edit_init (NULL, 1, 0, LINES - 2, COLS, _file, line); if (wedit == NULL) return 0; /* Create a new dialog and add it widgets to it */ edit_dlg = create_dlg (FALSE, 0, 0, LINES, COLS, NULL, edit_dialog_callback, "[Internal File Editor]", NULL, DLG_WANT_TAB); edit_dlg->get_shortcut = edit_get_shortcut; edit_dlg->get_title = edit_get_title; menubar = menubar_new (0, 0, COLS, NULL); add_widget (edit_dlg, menubar); edit_init_menu (menubar); init_widget (&wedit->widget, wedit->widget.y, wedit->widget.x, wedit->widget.lines, wedit->widget.cols, edit_callback, edit_event); widget_want_cursor (wedit->widget, TRUE); add_widget (edit_dlg, wedit); add_widget (edit_dlg, buttonbar_new (TRUE)); run_dlg (edit_dlg); if (edit_dlg->state == DLG_CLOSED) destroy_dlg (edit_dlg); return 1; }
/** * Read histories from the ${XDG_CACHE_HOME}/mc/history file */ static void dlg_read_history (Dlg_head * h) { char *profile; ev_history_load_save_t event_data; if (num_history_items_recorded == 0) /* this is how to disable */ return; profile = g_build_filename (mc_config_get_cache_path (), MC_HISTORY_FILE, NULL); event_data.cfg = mc_config_init (profile); event_data.receiver = NULL; /* create all histories in dialog */ mc_event_raise (h->event_group, MCEVENT_HISTORY_LOAD, &event_data); mc_config_deinit (event_data.cfg); g_free (profile); }
void show_datadirs_extended (void) { (void) printf ("%s %s\n", _("Root directory:"), mc_config_get_home_dir ()); (void) puts (""); PRINTF_GROUP (_("System data")); PRINTF_SECTION (_("Config directory:"), mc_global.sysconfig_dir); PRINTF_SECTION (_("Data directory:"), mc_global.share_data_dir); PRINTF_SECTION (_("File extension handlers:"), EXTHELPERSDIR); #if defined ENABLE_VFS_EXTFS || defined ENABLE_VFS_FISH PRINTF_SECTION (_("VFS plugins and scripts:"), LIBEXECDIR); #ifdef ENABLE_VFS_EXTFS PRINTF2 ("extfs.d:", LIBEXECDIR, MC_EXTFS_DIR "/"); #endif #ifdef ENABLE_VFS_FISH PRINTF2 ("fish:", LIBEXECDIR, FISH_PREFIX "/"); #endif #endif /* ENABLE_VFS_EXTFS || defiined ENABLE_VFS_FISH */ (void) puts (""); PRINTF_GROUP (_("User data")); PRINTF_SECTION2 (_("Config directory:"), mc_config_get_path ()); PRINTF_SECTION2 (_("Data directory:"), mc_config_get_data_path ()); PRINTF ("skins:", mc_config_get_data_path (), MC_SKINS_SUBDIR "/"); #ifdef ENABLE_VFS_EXTFS PRINTF ("extfs.d:", mc_config_get_data_path (), MC_EXTFS_DIR "/"); #endif #ifdef ENABLE_VFS_FISH PRINTF ("fish:", mc_config_get_data_path (), FISH_PREFIX "/"); #endif #ifdef USE_INTERNAL_EDIT PRINTF ("mcedit macros:", mc_config_get_data_path (), MC_MACRO_FILE); PRINTF ("mcedit external macros:", mc_config_get_data_path (), MC_EXTMACRO_FILE ".*"); #endif PRINTF_SECTION2 (_("Cache directory:"), mc_config_get_cache_path ()); }
char * expand_format (struct WEdit *edit_widget, char c, gboolean do_quote) { WPanel *panel = NULL; char *(*quote_func) (const char *, int); char *fname = NULL; char *result; char c_lc; #ifndef USE_INTERNAL_EDIT (void) edit_widget; #endif if (c == '%') return g_strdup ("%"); if (mc_run_mode == MC_RUN_FULL) { if (g_ascii_islower ((gchar) c)) panel = current_panel; else { if (get_other_type () != view_listing) return g_strdup (""); panel = other_panel; } fname = panel->dir.list[panel->selected].fname; } #ifdef USE_INTERNAL_EDIT else if (mc_run_mode == MC_RUN_EDITOR) fname = (char *) edit_get_file_name (edit_widget); #endif if (do_quote) quote_func = name_quote; else quote_func = fake_name_quote; c_lc = g_ascii_tolower ((gchar) c); switch (c_lc) { case 'f': case 'p': return (*quote_func) (fname, 0); case 'x': return (*quote_func) (extension (fname), 0); case 'd': { char *cwd; char *qstr; cwd = g_malloc (MC_MAXPATHLEN + 1); if (panel) g_strlcpy (cwd, panel->cwd, MC_MAXPATHLEN + 1); else mc_get_current_wd (cwd, MC_MAXPATHLEN + 1); qstr = (*quote_func) (cwd, 0); g_free (cwd); return qstr; } case 'i': /* indent equal number cursor position in line */ #ifdef USE_INTERNAL_EDIT if (edit_widget) return g_strnfill (edit_get_curs_col (edit_widget), ' '); #endif break; case 'y': /* syntax type */ #ifdef USE_INTERNAL_EDIT if (edit_widget) { const char *syntax_type = edit_get_syntax_type (edit_widget); if (syntax_type != NULL) return g_strdup (syntax_type); } #endif break; case 'k': /* block file name */ case 'b': /* block file name / strip extension */ { #ifdef USE_INTERNAL_EDIT if (edit_widget) { char *file = concat_dir_and_file (mc_config_get_cache_path (), EDIT_BLOCK_FILE); fname = (*quote_func) (file, 0); g_free (file); return fname; } #endif if (c_lc == 'b') return strip_ext ((*quote_func) (fname, 0)); break; } case 'n': /* strip extension in editor */ #ifdef USE_INTERNAL_EDIT if (edit_widget) return strip_ext ((*quote_func) (fname, 0)); #endif break; case 'm': /* menu file name */ if (menu) return (*quote_func) (menu, 0); break; case 's': if (!panel || !panel->marked) return (*quote_func) (fname, 0); /* Fall through */ case 't': case 'u': { int length = 2, i; char *block, *tmp; if (!panel) return g_strdup (""); for (i = 0; i < panel->count; i++) if (panel->dir.list[i].f.marked) length += strlen (panel->dir.list[i].fname) + 1; /* for space */ block = g_malloc (length * 2 + 1); *block = 0; for (i = 0; i < panel->count; i++) if (panel->dir.list[i].f.marked) { tmp = (*quote_func) (panel->dir.list[i].fname, 0); strcat (block, tmp); g_free (tmp); strcat (block, " "); if (c_lc == 'u') do_file_mark (panel, i, 0); } return block; } /* sub case block */ } /* switch */ result = g_strdup ("% "); result[1] = c; return result; }
void load_setup (void) { char *profile; size_t i; char *buffer; const char *kt; #ifdef HAVE_CHARSET load_codepages_list (); #endif /* HAVE_CHARSET */ profile = setup_init (); /* mc.lib is common for all users, but has priority lower than ${XDG_CONFIG_HOME}/mc/ini. FIXME: it's only used for keys and treestore now */ global_profile_name = g_build_filename (mc_sysconfig_dir, MC_GLOBAL_CONFIG_FILE, (char *) NULL); if (!exist_file (global_profile_name)) { g_free (global_profile_name); global_profile_name = g_build_filename (mc_share_data_dir, MC_GLOBAL_CONFIG_FILE, (char *) NULL); } panels_profile_name = g_build_filename (mc_config_get_cache_path (), MC_PANELS_FILE, NULL); mc_main_config = mc_config_init (profile); if (!exist_file (panels_profile_name)) setup__move_panels_config_into_separate_file (profile); mc_panels_config = mc_config_init (panels_profile_name); /* Load integer boolean options */ for (i = 0; int_options[i].opt_name != NULL; i++) *int_options[i].opt_addr = mc_config_get_int (mc_main_config, CONFIG_APP_SECTION, int_options[i].opt_name, *int_options[i].opt_addr); if (option_tab_spacing <= 0) option_tab_spacing = DEFAULT_TAB_SPACING; #ifdef USE_INTERNAL_EDIT if (option_word_wrap_line_length <= 0) option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH; #endif /* USE_INTERNAL_EDIT */ /* overwrite old_esc_mode_timeout */ kt = getenv ("KEYBOARD_KEY_TIMEOUT_US"); if ((kt != NULL) && (kt[0] != '\0')) old_esc_mode_timeout = atoi (kt); /* Load string options */ for (i = 0; str_options[i].opt_name != NULL; i++) *str_options[i].opt_addr = mc_config_get_string (mc_main_config, CONFIG_APP_SECTION, str_options[i].opt_name, str_options[i].opt_defval); load_layout (); panels_load_options (); load_panelize (); startup_left_mode = setup__load_panel_state ("New Left Panel"); startup_right_mode = setup__load_panel_state ("New Right Panel"); /* At least one of the panels is a listing panel */ if (startup_left_mode != view_listing && startup_right_mode != view_listing) startup_left_mode = view_listing; if (mc_run_param1 == NULL) { buffer = mc_config_get_string (mc_panels_config, "Dirs", "other_dir", "."); if (vfs_file_is_local (buffer)) mc_run_param1 = buffer; else g_free (buffer); } boot_current_is_left = mc_config_get_bool (mc_panels_config, "Dirs", "current_is_left", TRUE); /* Load time formats */ user_recent_timeformat = mc_config_get_string (mc_main_config, "Misc", "timeformat_recent", FMTTIME); user_old_timeformat = mc_config_get_string (mc_main_config, "Misc", "timeformat_old", FMTYEAR); #ifdef ENABLE_VFS_FTP ftpfs_proxy_host = mc_config_get_string (mc_main_config, "Misc", "ftp_proxy_host", "gate"); ftpfs_ignore_chattr_errors = mc_config_get_bool (mc_main_config, CONFIG_APP_SECTION, "ignore_ftp_chattr_errors", TRUE); ftpfs_init_passwd (); #endif /* ENABLE_VFS_FTP */ /* The default color and the terminal dependent color */ setup_color_string = mc_config_get_string (mc_main_config, "Colors", "base_color", ""); term_color_string = mc_config_get_string (mc_main_config, "Colors", getenv ("TERM"), ""); color_terminal_string = mc_config_get_string (mc_main_config, "Colors", "color_terminals", ""); /* Load the directory history */ /* directory_history_load (); */ /* Remove the temporal entries */ #ifdef HAVE_CHARSET if (codepages->len > 1) { buffer = mc_config_get_string (mc_main_config, "Misc", "display_codepage", ""); if (buffer[0] != '\0') { display_codepage = get_codepage_index (buffer); cp_display = get_codepage_id (display_codepage); } g_free (buffer); buffer = mc_config_get_string (mc_main_config, "Misc", "source_codepage", ""); if (buffer[0] != '\0') { default_source_codepage = get_codepage_index (buffer); source_codepage = default_source_codepage; /* May be source_codepage doesn't need this */ cp_source = get_codepage_id (source_codepage); } g_free (buffer); } autodetect_codeset = mc_config_get_string (mc_main_config, "Misc", "autodetect_codeset", ""); if ((autodetect_codeset[0] != '\0') && (strcmp (autodetect_codeset, "off") != 0)) is_autodetect_codeset_enabled = TRUE; g_free (init_translation_table (source_codepage, display_codepage)); buffer = (char *) get_codepage_id (display_codepage); if (buffer != NULL) utf8_display = str_isutf8 (buffer); #endif /* HAVE_CHARSET */ clipboard_store_path = mc_config_get_string (mc_main_config, "Misc", "clipboard_store", ""); clipboard_paste_path = mc_config_get_string (mc_main_config, "Misc", "clipboard_paste", ""); }
void save_file_position (const char *filename, long line, long column, off_t offset, GArray * bookmarks) { static size_t filepos_max_saved_entries = 0; char *fn, *tmp_fn; FILE *f, *tmp_f; char buf[MC_MAXPATHLEN + 100]; size_t i; const size_t len = strlen (filename); gboolean src_error = FALSE; if (filepos_max_saved_entries == 0) filepos_max_saved_entries = mc_config_get_int (mc_main_config, CONFIG_APP_SECTION, "filepos_max_saved_entries", 1024); fn = g_build_filename (mc_config_get_cache_path (), MC_FILEPOS_FILE, NULL); if (fn == NULL) goto early_error; mc_util_make_backup_if_possible (fn, TMP_SUFFIX); /* open file */ f = fopen (fn, "w"); if (f == NULL) goto open_target_error; tmp_fn = g_strdup_printf ("%s" TMP_SUFFIX, fn); tmp_f = fopen (tmp_fn, "r"); if (tmp_f == NULL) { src_error = TRUE; goto open_source_error; } /* put the new record */ if (line != 1 || column != 0 || bookmarks != NULL) { if (fprintf (f, "%s %ld;%ld;%" PRIuMAX, filename, line, column, (uintmax_t) offset) < 0) goto write_position_error; if (bookmarks != NULL) for (i = 0; i < bookmarks->len && i < MAX_SAVED_BOOKMARKS; i++) if (fprintf (f, ";%zu", g_array_index (bookmarks, size_t, i)) < 0) goto write_position_error; if (fprintf (f, "\n") < 0) goto write_position_error; } i = 1; while (fgets (buf, sizeof (buf), tmp_f) != NULL) { if (buf[len] == ' ' && strncmp (buf, filename, len) == 0 && strchr (&buf[len + 1], ' ') == NULL) continue; fprintf (f, "%s", buf); if (++i > filepos_max_saved_entries) break; } write_position_error: fclose (tmp_f); open_source_error: g_free (tmp_fn); fclose (f); if (src_error) mc_util_restore_from_backup_if_possible (fn, TMP_SUFFIX); else mc_util_unlink_backup_if_possible (fn, TMP_SUFFIX); open_target_error: g_free (fn); early_error: if (bookmarks != NULL) g_array_free (bookmarks, TRUE); }
void load_file_position (const char *filename, long *line, long *column, off_t * offset, GArray ** bookmarks) { char *fn; FILE *f; char buf[MC_MAXPATHLEN + 100]; const size_t len = strlen (filename); /* defaults */ *line = 1; *column = 0; *offset = 0; /* open file with positions */ fn = g_build_filename (mc_config_get_cache_path (), MC_FILEPOS_FILE, NULL); f = fopen (fn, "r"); g_free (fn); if (f == NULL) return; /* prepare array for serialized bookmarks */ *bookmarks = g_array_sized_new (FALSE, FALSE, sizeof (size_t), MAX_SAVED_BOOKMARKS); while (fgets (buf, sizeof (buf), f) != NULL) { const char *p; gchar **pos_tokens; /* check if the filename matches the beginning of string */ if (strncmp (buf, filename, len) != 0) continue; /* followed by single space */ if (buf[len] != ' ') continue; /* and string without spaces */ p = &buf[len + 1]; if (strchr (p, ' ') != NULL) continue; pos_tokens = g_strsplit (p, ";", 3 + MAX_SAVED_BOOKMARKS); if (pos_tokens[0] == NULL) { *line = 1; *column = 0; *offset = 0; } else { *line = strtol (pos_tokens[0], NULL, 10); if (pos_tokens[1] == NULL) { *column = 0; *offset = 0; } else { *column = strtol (pos_tokens[1], NULL, 10); if (pos_tokens[2] == NULL) *offset = 0; else { size_t i; *offset = strtoll (pos_tokens[2], NULL, 10); for (i = 0; i < MAX_SAVED_BOOKMARKS && pos_tokens[3 + i] != NULL; i++) { size_t val; val = strtoul (pos_tokens[3 + i], NULL, 10); g_array_append_val (*bookmarks, val); } } } } g_strfreev (pos_tokens); } fclose (f); }