/* Returns non-zero on successful running. */ static int try_run_with_filetype(FileView *view, const assoc_records_t assocs, const char start[], int background) { const size_t len = strlen(start); int i; for(i = 0; i < assocs.count; i++) { if(strncmp(assocs.list[i].command, start, len) == 0) { run_using_prog(view, assocs.list[i].command, 0, background); return 1; } } return 0; }
/* Callback that is called when menu item is selected. Should return non-zero * to stay in menu mode. */ int execute_filetype_cb(FileView *view, menu_info *m) { if(view->dir_entry[view->list_pos].type == FT_DIR && m->pos == 0) { open_dir(view); } else { const char *prog_str = strchr(m->data[m->pos], '|') + 1; if(prog_str[0] != '\0') { int background = m->extra_data & 1; run_using_prog(view, prog_str, 0, background); } } clean_selected_files(view); redraw_view(view); return 0; }
/* Runs each of selected file entries of the view individually. */ static void run_selection_separately(FileView *view, int dont_execute) { dir_entry_t *entry; const int pos = view->list_pos; entry = NULL; while(iter_selected_entries(view, &entry)) { char *typed_fname; const char *entry_prog_cmd; typed_fname = get_typed_entry_fpath(entry); entry_prog_cmd = ft_get_program(typed_fname); free(typed_fname); view->list_pos = entry_to_pos(view, entry); run_using_prog(view, entry_prog_cmd, dont_execute, 0); } view->list_pos = pos; }
static void run_file(FileView *view, int dont_execute) { /* TODO: refactor this function run_file() */ char *typed_fname; const char *multi_prog_cmd; int undef; int same; dir_entry_t *entry; int no_multi_run; if(!view->dir_entry[view->list_pos].selected) { clean_selected_files(view); } typed_fname = get_typed_entry_fpath(get_current_entry(view)); multi_prog_cmd = ft_get_program(typed_fname); free(typed_fname); no_multi_run = !is_multi_run_compat(view, multi_prog_cmd); undef = 0; same = 1; entry = NULL; while(iter_selected_entries(view, &entry)) { char *typed_fname; const char *entry_prog_cmd; if(!path_exists_at(entry->origin, entry->name, DEREF)) { show_error_msgf("Broken Link", "Destination of \"%s\" link doesn't exist", entry->name); return; } typed_fname = get_typed_entry_fpath(entry); entry_prog_cmd = ft_get_program(typed_fname); free(typed_fname); if(entry_prog_cmd == NULL) { ++undef; continue; } no_multi_run += !is_multi_run_compat(view, entry_prog_cmd); if(multi_prog_cmd == NULL) { multi_prog_cmd = entry_prog_cmd; } else if(strcmp(entry_prog_cmd, multi_prog_cmd) != 0) { same = 0; } } if(!same && undef == 0 && no_multi_run) { show_error_msg("Run error", "Handlers of selected files are incompatible."); return; } if(undef > 0) { multi_prog_cmd = NULL; } /* Check for a filetype */ /* vi is set as the default for any extension without a program */ if(multi_prog_cmd == NULL) { run_with_defaults(view); return; } if(no_multi_run) { run_using_prog(view, multi_prog_cmd, dont_execute, 0); } else { run_selection_separately(view, dont_execute); } }