Ticket #2968: mc.2968.diff
File mc.2968.diff, 12.0 KB (added by szaszg, 11 years ago) |
---|
-
src/filemanager/ext.c
diff --git a/src/filemanager/ext.c b/src/filemanager/ext.c index b022dcb..70a293d 100644
a b 76 76 #define FILE_CMD "file " 77 77 #endif 78 78 79 #define EXT_COMMAND_CACHE_SIZE 128 80 79 81 /*** file scope type declarations ****************************************************************/ 80 82 81 83 typedef char *(*quote_func_t) (const char *name, int quote_percent); 82 84 85 typedef struct { 86 char *filename; 87 char *command; 88 char *temp_command_file; 89 int freq; 90 } ext_command_cache_t; 91 92 83 93 /*** file scope variables ************************************************************************/ 84 94 85 95 /* This variable points to a copy of the mc.ext file in memory … … static gboolean is_cd = FALSE; 98 108 static gboolean written_nonspace = FALSE; 99 109 static gboolean do_local_copy = FALSE; 100 110 111 static ext_command_cache_t ext_command_cache[EXT_COMMAND_CACHE_SIZE]; 112 static int command_cache_idx = 0; 113 101 114 /*** file scope functions ************************************************************************/ 102 115 /* --------------------------------------------------------------------------------------------- */ 103 116 … … exec_make_shell_string (const char *lc_data, const vfs_path_t * filename_vpath) 337 350 /* --------------------------------------------------------------------------------------------- */ 338 351 339 352 static void 340 exec_extension_view (char *cmd, const vfs_path_t * filename_vpath, int start_line, 341 vfs_path_t * temp_file_name_vpath) 353 exec_extension_view (char *cmd, const vfs_path_t * filename_vpath, int start_line) 342 354 { 343 355 int def_hex_mode = mcview_default_hex_mode, changed_hex_mode = 0; 344 356 int def_nroff_flag = mcview_default_nroff_flag, changed_nroff_flag = 0; … … exec_extension_view (char *cmd, const vfs_path_t * filename_vpath, int start_lin 356 368 if (written_nonspace) 357 369 { 358 370 mcview_viewer (cmd, filename_vpath, start_line); 359 mc_unlink (temp_file_name_vpath);360 371 } 361 372 else 362 373 mcview_viewer (NULL, filename_vpath, start_line); … … exec_extension_cd (void) 398 409 /* --------------------------------------------------------------------------------------------- */ 399 410 400 411 static void 401 exec_extension (const vfs_path_t * filename_vpath, const char *lc_data, int start_line) 412 flush_command_cache_member (int i) 413 { 414 vfs_path_t *vpath = vfs_path_from_str (ext_command_cache[i].temp_command_file); 415 416 mc_unlink (vpath); 417 vfs_path_free (vpath); 418 g_free (ext_command_cache[i].filename); 419 g_free (ext_command_cache[i].command); 420 g_free (ext_command_cache[i].temp_command_file); 421 } 422 423 /* --------------------------------------------------------------------------------------------- */ 424 425 static int 426 check_command_cache (const vfs_path_t *filename_vpath) 427 { 428 int i; 429 char *filename = vfs_path_to_str (filename_vpath); 430 431 for (i=0; i<command_cache_idx; i++) 432 { 433 if (!strcmp(ext_command_cache[i].filename, filename)) 434 { 435 g_free (filename); 436 ext_command_cache[i].freq++; 437 return i; 438 } 439 } 440 return -1; 441 } 442 443 /* --------------------------------------------------------------------------------------------- */ 444 445 static int 446 add_command_cache (char *cmd, const vfs_path_t *filename_vpath, vfs_path_t *temp_file_name_vpath) 447 { 448 int i; 449 char *filename = vfs_path_to_str (filename_vpath); 450 451 for (i=0; i<command_cache_idx; i++) 452 { 453 if (!strcmp(ext_command_cache[i].filename, filename)) 454 { 455 g_free (filename); 456 ext_command_cache[i].freq++; 457 return i; 458 } 459 } 460 if (i < EXT_COMMAND_CACHE_SIZE) 461 { 462 ext_command_cache[i].filename = g_strdup(filename); 463 ext_command_cache[i].command = g_strdup(cmd); 464 ext_command_cache[i].temp_command_file = vfs_path_to_str (temp_file_name_vpath); 465 ext_command_cache[i].freq = 0; 466 command_cache_idx++; 467 } 468 else 469 { 470 int j, min = 99999; 471 i = 0; 472 473 for (j=0; j<command_cache_idx; j++) 474 { 475 if (ext_command_cache[j].freq == 0) 476 { 477 i = j; 478 break; 479 } 480 if (ext_command_cache[j].freq < min) 481 { 482 min = ext_command_cache[j].freq; 483 i = j; 484 } 485 } 486 flush_command_cache_member(i); 487 ext_command_cache[i].filename = g_strdup(filename); 488 ext_command_cache[i].command = g_strdup(cmd); 489 ext_command_cache[i].temp_command_file = vfs_path_to_str (temp_file_name_vpath); 490 ext_command_cache[i].freq = 0; 491 } 492 g_free (filename); 493 return i; 494 } 495 496 /* --------------------------------------------------------------------------------------------- */ 497 498 static void 499 exec_extension (const vfs_path_t * filename_vpath, const char *lc_data, int start_line, int *cmd_idx, gboolean run_dialog) 402 500 { 403 501 char *shell_string, *export_variables; 404 502 vfs_path_t *temp_file_name_vpath = NULL; … … exec_extension (const vfs_path_t * filename_vpath, const char *lc_data, int star 493 591 } 494 592 495 593 if (run_view) 496 exec_extension_view (cmd, filename_vpath, start_line, temp_file_name_vpath); 594 { 595 int cidx = -1; 596 if (written_nonspace) 597 cidx = add_command_cache (cmd, filename_vpath, temp_file_name_vpath); 598 if (!run_dialog) 599 { 600 *cmd_idx = cidx; 601 } 602 else 603 exec_extension_view (cmd, filename_vpath, start_line); 604 } 497 605 else 498 606 { 499 607 shell_execute (cmd, EXECUTE_INTERNAL); … … flush_extension_file (void) 754 862 } 755 863 756 864 /* --------------------------------------------------------------------------------------------- */ 865 866 void 867 ext_flush_command_cache (void) 868 { 869 int i; 870 871 for (i=0; i<command_cache_idx; i++) 872 { 873 flush_command_cache_member(i); 874 } 875 command_cache_idx = 0; 876 } 877 878 /* --------------------------------------------------------------------------------------------- */ 879 880 char * 881 ext_get_command_cache (int idx) 882 { 883 if (idx >= command_cache_idx) 884 return NULL; 885 886 return ext_command_cache[idx].command; 887 } 888 889 /* --------------------------------------------------------------------------------------------- */ 757 890 /** 758 891 * The second argument is action, i.e. Open, View or Edit 759 892 * … … flush_extension_file (void) 770 903 int 771 904 regex_command (const vfs_path_t * filename_vpath, const char *action) 772 905 { 906 return regex_command2 (filename_vpath, action, NULL, TRUE); 907 } 908 909 int 910 regex_command2 (const vfs_path_t * filename_vpath, const char *action, int *cmd_idx, gboolean run_dialog) 911 { 773 912 char *filename, *p, *q, *r, c; 774 913 size_t file_len; 775 914 gboolean found = FALSE; … … regex_command (const vfs_path_t * filename_vpath, const char *action) 795 934 view_at_line_number = 0; 796 935 } 797 936 937 if (strcmp (action, "View") == 0) 938 { 939 int cidx; 940 if ((cidx = check_command_cache(filename_vpath)) > -1) 941 { 942 if (!run_dialog) 943 { 944 *cmd_idx = cidx; 945 } 946 else 947 exec_extension_view (ext_get_command_cache(cidx), filename_vpath, view_at_line_number); 948 949 return 1; 950 } 951 } 952 798 953 if (data == NULL) 799 954 { 800 955 char *extension_file; … … regex_command (const vfs_path_t * filename_vpath, const char *action) 1022 1177 */ 1023 1178 if (p < q) 1024 1179 { 1025 exec_extension (filename_vpath, r + 1, view_at_line_number );1180 exec_extension (filename_vpath, r + 1, view_at_line_number, cmd_idx, run_dialog); 1026 1181 ret = 1; 1027 1182 } 1028 1183 break; -
src/filemanager/ext.h
diff --git a/src/filemanager/ext.h b/src/filemanager/ext.h index a4c470b..4847507 100644
a b 15 15 /*** declarations of public functions ************************************************************/ 16 16 17 17 int regex_command (const vfs_path_t * filename_vpath, const char *action); 18 int regex_command2 (const vfs_path_t * filename_vpath, const char *action, int *cmd_idx, gboolean run_dialog); 19 char *ext_get_command_cache (int idx); 20 void ext_flush_command_cache (void); 18 21 19 22 /* Call it after the user has edited the mc.ext file, 20 23 * to flush the cached mc.ext file -
src/filemanager/midnight.c
diff --git a/src/filemanager/midnight.c b/src/filemanager/midnight.c index f97a18e..23ae881 100644
a b 77 77 #include "chown.h" 78 78 #include "achown.h" 79 79 80 #include "ext.h" /* ext_flush_command_cache() */ 81 80 82 #ifdef USE_INTERNAL_EDIT 81 83 #include "src/editor/edit.h" 82 84 #endif … … do_nc (void) 1797 1799 destroy_dlg (midnight_dlg); 1798 1800 current_panel = NULL; 1799 1801 1802 ext_flush_command_cache(); 1800 1803 #ifdef USE_INTERNAL_EDIT 1801 1804 edit_stack_free (); 1802 1805 #endif -
src/viewer/actions_cmd.c
diff --git a/src/viewer/actions_cmd.c b/src/viewer/actions_cmd.c index e6baa02..68adc10 100644
a b 62 62 63 63 #include "src/filemanager/layout.h" 64 64 #include "src/filemanager/cmd.h" 65 #include "src/filemanager/ext.h" 65 66 #include "src/filemanager/midnight.h" /* current_panel */ 66 67 67 68 #include "src/history.h" … … static void 319 320 mcview_load_next_prev (mcview_t * view, int direction) 320 321 { 321 322 dir_list *dir; 322 int *dir_count, *dir_idx ;323 int *dir_count, *dir_idx, cmd_idx; 323 324 vfs_path_t *vfile; 324 325 char *file; 325 326 … … mcview_load_next_prev (mcview_t * view, int direction) 335 336 view->dir_idx = NULL; 336 337 vfile = vfs_path_append_new (view->workdir_vpath, dir->list[*dir_idx].fname, (char *) NULL); 337 338 file = vfs_path_to_str (vfile); 338 vfs_path_free (vfile);339 339 mcview_done (view); 340 340 mcview_init (view); 341 mcview_load (view, NULL, file, 0); 341 if (view->magic_mode) { 342 if (regex_command2 (vfile, "View", &cmd_idx, FALSE) != 1) 343 { 344 mcview_load (view, NULL, file, 0); 345 } 346 else 347 { 348 if (cmd_idx > -1) 349 { 350 mcview_load (view, ext_get_command_cache(cmd_idx), file, 0); 351 } 352 else 353 { 354 mcview_load (view, NULL, file, 0); 355 } 356 } 357 } 358 else 359 { 360 mcview_load (view, NULL, file, 0); 361 } 362 vfs_path_free (vfile); 342 363 g_free (file); 343 364 view->dir = dir; 344 365 view->dir_count = dir_count; -
src/viewer/lib.c
diff --git a/src/viewer/lib.c b/src/viewer/lib.c index c99197f..594f77a 100644
a b 52 52 #include "src/selcodepage.h" 53 53 #endif 54 54 55 #include "src/filemanager/ext.h" 56 55 57 #include "internal.h" 56 58 #include "mcviewer.h" 57 59 … … mcview_toggle_magic_mode (mcview_t * view) 79 81 { 80 82 char *filename, *command; 81 83 dir_list *dir; 82 int *dir_count, *dir_idx; 84 int *dir_count, *dir_idx, cmd_idx; 85 vfs_path_t *vfile = vfs_path_clone (view->filename_vpath); 83 86 84 87 mcview_altered_magic_flag = 1; 85 88 view->magic_mode = !view->magic_mode; … … mcview_toggle_magic_mode (mcview_t * view) 95 98 view->dir_idx = NULL; 96 99 mcview_done (view); 97 100 mcview_init (view); 98 mcview_load (view, command, filename, 0); 101 if (view->magic_mode) 102 { 103 if (command) 104 { 105 mcview_load (view, command, filename, 0); 106 } 107 else 108 { 109 if (regex_command2 (vfile, "View", &cmd_idx, FALSE) != 1) 110 { 111 mcview_load (view, NULL, filename, 0); 112 } 113 else 114 { 115 if (cmd_idx > -1) 116 { 117 mcview_load (view, ext_get_command_cache(cmd_idx), filename, 0); 118 } 119 else 120 { 121 mcview_load (view, NULL, filename, 0); 122 } 123 } 124 } 125 } 126 else 127 { 128 mcview_load (view, NULL, filename, 0); 129 } 99 130 view->dir = dir; 100 131 view->dir_count = dir_count; 101 132 view->dir_idx = dir_idx; 133 vfs_path_free (vfile); 102 134 g_free (filename); 103 135 g_free (command); 104 136