Ticket #2968: mc.2968.diff

File mc.2968.diff, 12.0 KB (added by szaszg, 11 years ago)

magic mode uses regex_command()

  • src/filemanager/ext.c

    diff --git a/src/filemanager/ext.c b/src/filemanager/ext.c
    index b022dcb..70a293d 100644
    a b  
    7676#define FILE_CMD "file " 
    7777#endif 
    7878 
     79#define EXT_COMMAND_CACHE_SIZE 128 
     80 
    7981/*** file scope type declarations ****************************************************************/ 
    8082 
    8183typedef char *(*quote_func_t) (const char *name, int quote_percent); 
    8284 
     85typedef struct { 
     86    char *filename; 
     87    char *command; 
     88    char *temp_command_file; 
     89    int freq; 
     90} ext_command_cache_t; 
     91 
     92 
    8393/*** file scope variables ************************************************************************/ 
    8494 
    8595/* This variable points to a copy of the mc.ext file in memory 
    static gboolean is_cd = FALSE; 
    98108static gboolean written_nonspace = FALSE; 
    99109static gboolean do_local_copy = FALSE; 
    100110 
     111static ext_command_cache_t ext_command_cache[EXT_COMMAND_CACHE_SIZE]; 
     112static int command_cache_idx = 0; 
     113 
    101114/*** file scope functions ************************************************************************/ 
    102115/* --------------------------------------------------------------------------------------------- */ 
    103116 
    exec_make_shell_string (const char *lc_data, const vfs_path_t * filename_vpath) 
    337350/* --------------------------------------------------------------------------------------------- */ 
    338351 
    339352static void 
    340 exec_extension_view (char *cmd, const vfs_path_t * filename_vpath, int start_line, 
    341                      vfs_path_t * temp_file_name_vpath) 
     353exec_extension_view (char *cmd, const vfs_path_t * filename_vpath, int start_line) 
    342354{ 
    343355    int def_hex_mode = mcview_default_hex_mode, changed_hex_mode = 0; 
    344356    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 
    356368    if (written_nonspace) 
    357369    { 
    358370        mcview_viewer (cmd, filename_vpath, start_line); 
    359         mc_unlink (temp_file_name_vpath); 
    360371    } 
    361372    else 
    362373        mcview_viewer (NULL, filename_vpath, start_line); 
    exec_extension_cd (void) 
    398409/* --------------------------------------------------------------------------------------------- */ 
    399410 
    400411static void 
    401 exec_extension (const vfs_path_t * filename_vpath, const char *lc_data, int start_line) 
     412flush_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 
     425static int 
     426check_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 
     445static int 
     446add_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 
     498static void 
     499exec_extension (const vfs_path_t * filename_vpath, const char *lc_data, int start_line, int *cmd_idx, gboolean run_dialog) 
    402500{ 
    403501    char *shell_string, *export_variables; 
    404502    vfs_path_t *temp_file_name_vpath = NULL; 
    exec_extension (const vfs_path_t * filename_vpath, const char *lc_data, int star 
    493591    } 
    494592 
    495593    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    } 
    497605    else 
    498606    { 
    499607        shell_execute (cmd, EXECUTE_INTERNAL); 
    flush_extension_file (void) 
    754862} 
    755863 
    756864/* --------------------------------------------------------------------------------------------- */ 
     865 
     866void 
     867ext_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 
     880char * 
     881ext_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/* --------------------------------------------------------------------------------------------- */ 
    757890/** 
    758891 * The second argument is action, i.e. Open, View or Edit 
    759892 * 
    flush_extension_file (void) 
    770903int 
    771904regex_command (const vfs_path_t * filename_vpath, const char *action) 
    772905{ 
     906    return regex_command2 (filename_vpath, action, NULL, TRUE); 
     907} 
     908 
     909int 
     910regex_command2 (const vfs_path_t * filename_vpath, const char *action, int *cmd_idx, gboolean run_dialog) 
     911{ 
    773912    char *filename, *p, *q, *r, c; 
    774913    size_t file_len; 
    775914    gboolean found = FALSE; 
    regex_command (const vfs_path_t * filename_vpath, const char *action) 
    795934        view_at_line_number = 0; 
    796935    } 
    797936 
     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 
    798953    if (data == NULL) 
    799954    { 
    800955        char *extension_file; 
    regex_command (const vfs_path_t * filename_vpath, const char *action) 
    10221177                         */ 
    10231178                        if (p < q) 
    10241179                        { 
    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); 
    10261181                            ret = 1; 
    10271182                        } 
    10281183                        break; 
  • src/filemanager/ext.h

    diff --git a/src/filemanager/ext.h b/src/filemanager/ext.h
    index a4c470b..4847507 100644
    a b  
    1515/*** declarations of public functions ************************************************************/ 
    1616 
    1717int regex_command (const vfs_path_t * filename_vpath, const char *action); 
     18int regex_command2 (const vfs_path_t * filename_vpath, const char *action, int *cmd_idx, gboolean run_dialog); 
     19char *ext_get_command_cache (int idx); 
     20void ext_flush_command_cache (void); 
    1821 
    1922/* Call it after the user has edited the mc.ext file, 
    2023 * 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  
    7777#include "chown.h" 
    7878#include "achown.h" 
    7979 
     80#include "ext.h"          /* ext_flush_command_cache() */ 
     81 
    8082#ifdef USE_INTERNAL_EDIT 
    8183#include "src/editor/edit.h" 
    8284#endif 
    do_nc (void) 
    17971799    destroy_dlg (midnight_dlg); 
    17981800    current_panel = NULL; 
    17991801 
     1802    ext_flush_command_cache(); 
    18001803#ifdef USE_INTERNAL_EDIT 
    18011804    edit_stack_free (); 
    18021805#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  
    6262 
    6363#include "src/filemanager/layout.h" 
    6464#include "src/filemanager/cmd.h" 
     65#include "src/filemanager/ext.h" 
    6566#include "src/filemanager/midnight.h"   /* current_panel */ 
    6667 
    6768#include "src/history.h" 
    static void 
    319320mcview_load_next_prev (mcview_t * view, int direction) 
    320321{ 
    321322    dir_list *dir; 
    322     int *dir_count, *dir_idx; 
     323    int *dir_count, *dir_idx, cmd_idx; 
    323324    vfs_path_t *vfile; 
    324325    char *file; 
    325326 
    mcview_load_next_prev (mcview_t * view, int direction) 
    335336    view->dir_idx = NULL; 
    336337    vfile = vfs_path_append_new (view->workdir_vpath, dir->list[*dir_idx].fname, (char *) NULL); 
    337338    file = vfs_path_to_str (vfile); 
    338     vfs_path_free (vfile); 
    339339    mcview_done (view); 
    340340    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); 
    342363    g_free (file); 
    343364    view->dir = dir; 
    344365    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  
    5252#include "src/selcodepage.h" 
    5353#endif 
    5454 
     55#include "src/filemanager/ext.h" 
     56 
    5557#include "internal.h" 
    5658#include "mcviewer.h" 
    5759 
    mcview_toggle_magic_mode (mcview_t * view) 
    7981{ 
    8082    char *filename, *command; 
    8183    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); 
    8386 
    8487    mcview_altered_magic_flag = 1; 
    8588    view->magic_mode = !view->magic_mode; 
    mcview_toggle_magic_mode (mcview_t * view) 
    9598    view->dir_idx = NULL; 
    9699    mcview_done (view); 
    97100    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    } 
    99130    view->dir = dir; 
    100131    view->dir_count = dir_count; 
    101132    view->dir_idx = dir_idx; 
     133    vfs_path_free (vfile); 
    102134    g_free (filename); 
    103135    g_free (command); 
    104136