Example #1
0
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 ("%");

    switch (mc_global.mc_run_mode)
    {
    case 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 = g_strdup (panel->dir.list[panel->selected].fname);
        break;

#ifdef USE_INTERNAL_EDIT
    case MC_RUN_EDITOR:
        fname = edit_get_file_name (edit_widget);
        break;
#endif

    default:
        /* other modes don't use formats */
        return g_strdup ("");
    }

    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':
        result = (*quote_func) (fname, 0);
        goto ret;
    case 'x':
        result = (*quote_func) (extension (fname), 0);
        goto ret;
    case 'd':
        {
            char *cwd;
            char *qstr;

            if (panel)
                cwd = g_strdup (vfs_path_as_str (panel->cwd_vpath));
            else
                cwd = vfs_get_current_dir ();

            qstr = (*quote_func) (cwd, 0);

            g_free (cwd);

            result = qstr;
            goto ret;
        }
    case 'i':                  /* indent equal number cursor position in line */
#ifdef USE_INTERNAL_EDIT
        if (edit_widget)
        {
            result = g_strnfill (edit_get_curs_col (edit_widget), ' ');
            goto ret;
        }
#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)
            {
                result = g_strdup (syntax_type);
                goto ret;
            }
        }
#endif
        break;
    case 'k':                  /* block file name */
    case 'b':                  /* block file name / strip extension */
        {
#ifdef USE_INTERNAL_EDIT
            if (edit_widget)
            {
                char *file;

                file = mc_config_get_full_path (EDIT_BLOCK_FILE);
                result = (*quote_func) (file, 0);
                g_free (file);
                goto ret;
            }
#endif
            if (c_lc == 'b')
            {
                result = strip_ext ((*quote_func) (fname, 0));
                goto ret;
            }
            break;
        }
    case 'n':                  /* strip extension in editor */
#ifdef USE_INTERNAL_EDIT
        if (edit_widget)
        {
            result = strip_ext ((*quote_func) (fname, 0));
            goto ret;
        }
#endif
        break;
    case 'm':                  /* menu file name */
        if (menu)
        {
            result = (*quote_func) (menu, 0);
            goto ret;
        }
        break;
    case 's':
        if (!panel || !panel->marked)
        {
            result = (*quote_func) (fname, 0);
            goto ret;
        }

        /* Fall through */

    case 't':
    case 'u':
        {
            GString *block;
            int i;

            if (panel == NULL)
            {
                result = g_strdup ("");
                goto ret;
            }

            block = g_string_sized_new (16);

            for (i = 0; i < panel->dir.len; i++)
                if (panel->dir.list[i].f.marked)
                {
                    char *tmp;

                    tmp = (*quote_func) (panel->dir.list[i].fname, 0);
                    g_string_append (block, tmp);
                    g_string_append_c (block, ' ');
                    g_free (tmp);

                    if (c_lc == 'u')
                        do_file_mark (panel, i, 0);
                }
            result = g_string_free (block, FALSE);
            goto ret;
        }                       /* sub case block */
    }                           /* switch */
    result = g_strdup ("% ");
    result[1] = c;
  ret:
    g_free (fname);
    return result;
}
Example #2
0
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;
}