Use events for operate with clipboard (copy/paste to external clipboard, save/load to file)

Signed-off-by: Slava Zanko <slavazanko@gmail.com>
This commit is contained in:
Slava Zanko 2011-02-17 11:24:53 +02:00
parent c2dfb82f61
commit 62207c8d4d
7 changed files with 203 additions and 136 deletions

View File

@ -34,6 +34,13 @@ typedef struct
va_list ap;
} ev_vfs_print_message_t;
/* MCEVENT_GROUP_CORE:clipboard_text_from_file */
typedef struct
{
char **text;
gboolean ret;
} ev_clipboard_text_from_file_t;
/*** global variables defined in .c file *********************************************************/

View File

@ -42,19 +42,18 @@
#include "lib/tty/tty.h"
#include "lib/tty/mouse.h"
#include "lib/tty/key.h" /* XCTRL and ALT macros */
#include "lib/vfs/vfs.h"
#include "lib/fileloc.h"
#include "lib/skin.h"
#include "lib/strutil.h"
#include "lib/util.h"
#include "lib/keybind.h" /* global_keymap_t */
#include "lib/widget.h"
#include "lib/event.h" /* mc_event_raise() */
#include "input_complete.h"
#include "src/main.h" /* home_dir */
#include "src/filemanager/midnight.h" /* current_panel */
#include "src/clipboard.h" /* copy_file_to_ext_clip, paste_to_file_from_ext_clip */
/*** global variables ****************************************************************************/
@ -85,124 +84,6 @@ const global_keymap_t *input_map;
static char *kill_buffer = NULL;
/*** file scope functions ************************************************************************/
static gboolean
save_text_to_clip_file (const char *text)
{
int file;
char *fname = NULL;
ssize_t ret;
size_t str_len;
fname = g_build_filename (mc_config_get_cache_path (), EDIT_CLIP_FILE, NULL);
file = mc_open (fname, O_CREAT | O_WRONLY | O_TRUNC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
g_free (fname);
if (file == -1)
return FALSE;
str_len = strlen (text);
ret = mc_write (file, (char *) text, str_len);
mc_close (file);
return ret == (ssize_t) str_len;
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
load_text_from_clip_file (char **text)
{
char buf[BUF_LARGE];
FILE *f;
char *fname = NULL;
gboolean first = TRUE;
fname = g_build_filename (mc_config_get_cache_path (), EDIT_CLIP_FILE, NULL);
f = fopen (fname, "r");
g_free (fname);
if (f == NULL)
return FALSE;
*text = NULL;
while (fgets (buf, sizeof (buf), f))
{
size_t len;
len = strlen (buf);
if (len > 0)
{
if (buf[len - 1] == '\n')
buf[len - 1] = '\0';
if (first)
{
first = FALSE;
*text = g_strdup (buf);
}
else
{
/* remove \n on EOL */
char *tmp;
tmp = g_strconcat (*text, " ", buf, (char *) NULL);
g_free (*text);
*text = tmp;
}
}
}
fclose (f);
return (*text != NULL);
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
panel_save_curent_file_to_clip_file (void)
{
gboolean res = FALSE;
if (current_panel->marked == 0)
res = save_text_to_clip_file (selection (current_panel)->fname);
else
{
int i;
gboolean first = TRUE;
char *flist = NULL;
for (i = 0; i < current_panel->count; i++)
if (current_panel->dir.list[i].f.marked != 0)
{ /* Skip the unmarked ones */
if (first)
{
flist = g_strdup (current_panel->dir.list[i].fname);
first = FALSE;
}
else
{
/* Add empty lines after the file */
char *tmp;
tmp =
g_strconcat (flist, "\n", current_panel->dir.list[i].fname, (char *) NULL);
g_free (flist);
flist = tmp;
}
}
if (flist != NULL)
{
res = save_text_to_clip_file (flist);
g_free (flist);
}
}
return res;
}
/* --------------------------------------------------------------------------------------------- */
static void
@ -571,9 +452,9 @@ copy_region (WInput * in, int x_first, int x_last)
if (last == first)
{
/* Copy selected files to clipboard */
panel_save_curent_file_to_clip_file ();
mc_event_raise (MCEVENT_GROUP_FILEMANAGER, "panel_save_curent_file_to_clip_file", NULL);
/* try use external clipboard utility */
copy_file_to_ext_clip ();
mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
return;
}
@ -584,9 +465,9 @@ copy_region (WInput * in, int x_first, int x_last)
kill_buffer = g_strndup (in->buffer + first, last - first);
save_text_to_clip_file (kill_buffer);
mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", kill_buffer);
/* try use external clipboard utility */
copy_file_to_ext_clip ();
mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
}
/* --------------------------------------------------------------------------------------------- */
@ -669,11 +550,15 @@ static void
ins_from_clip (WInput * in)
{
char *p = NULL;
ev_clipboard_text_from_file_t event_data;
/* try use external clipboard utility */
paste_to_file_from_ext_clip ();
mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
if (load_text_from_clip_file (&p))
event_data.text = &p;
mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_from_file", &event_data);
if (event_data.ret)
{
char *pp;

View File

@ -33,6 +33,9 @@
#include "lib/fileloc.h"
#include "lib/mcconfig.h"
#include "lib/util.h"
#include "lib/event.h"
#include "lib/vfs/vfs.h"
#include "main.h"
#include "src/execute.h"
@ -58,15 +61,22 @@ char *clipboard_paste_path = NULL;
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
/* event callback */
gboolean
copy_file_to_ext_clip (void)
clipboard_file_to_ext_clip (const gchar * event_group_name, const gchar * event_name,
gpointer init_data, gpointer data)
{
char *tmp, *cmd;
int res = 0;
const char *d = getenv ("DISPLAY");
(void) event_group_name;
(void) event_name;
(void) init_data;
(void) data;
if (d == NULL || clipboard_store_path == NULL || clipboard_store_path[0] == '\0')
return FALSE;
return TRUE;
tmp = concat_dir_and_file (mc_config_get_cache_path (), EDIT_CLIP_FILE);
cmd = g_strconcat (clipboard_store_path, " ", tmp, " 2>/dev/null", (char *) NULL);
@ -81,15 +91,22 @@ copy_file_to_ext_clip (void)
/* --------------------------------------------------------------------------------------------- */
/* event callback */
gboolean
paste_to_file_from_ext_clip (void)
clipboard_file_from_ext_clip (const gchar * event_group_name, const gchar * event_name,
gpointer init_data, gpointer data)
{
char *tmp, *cmd;
int res = 0;
const char *d = getenv ("DISPLAY");
(void) event_group_name;
(void) event_name;
(void) init_data;
(void) data;
if (d == NULL || clipboard_paste_path == NULL || clipboard_paste_path[0] == '\0')
return FALSE;
return TRUE;
tmp = concat_dir_and_file (mc_config_get_cache_path (), EDIT_CLIP_FILE);
cmd = g_strconcat (clipboard_paste_path, " > ", tmp, " 2>/dev/null", (char *) NULL);
@ -103,3 +120,98 @@ paste_to_file_from_ext_clip (void)
}
/* --------------------------------------------------------------------------------------------- */
/* event callback */
gboolean
clipboard_text_to_file (const gchar * event_group_name, const gchar * event_name,
gpointer init_data, gpointer data)
{
int file;
char *fname = NULL;
ssize_t ret;
size_t str_len;
const char *text = (const char *) data;
(void) event_group_name;
(void) event_name;
(void) init_data;
if (text == NULL)
return FALSE;
fname = g_build_filename (mc_config_get_cache_path (), EDIT_CLIP_FILE, NULL);
file = mc_open (fname, O_CREAT | O_WRONLY | O_TRUNC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
g_free (fname);
if (file == -1)
return TRUE;
str_len = strlen (text);
ret = mc_write (file, (char *) text, str_len);
mc_close (file);
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
/* event callback */
gboolean
clipboard_text_from_file (const gchar * event_group_name, const gchar * event_name,
gpointer init_data, gpointer data)
{
char buf[BUF_LARGE];
FILE *f;
char *fname = NULL;
gboolean first = TRUE;
ev_clipboard_text_from_file_t *event_data = (ev_clipboard_text_from_file_t *) data;
(void) event_group_name;
(void) event_name;
(void) init_data;
fname = g_build_filename (mc_config_get_cache_path (), EDIT_CLIP_FILE, NULL);
f = fopen (fname, "r");
g_free (fname);
if (f == NULL)
{
event_data->ret = FALSE;
return TRUE;
}
*(event_data->text) = NULL;
while (fgets (buf, sizeof (buf), f))
{
size_t len;
len = strlen (buf);
if (len > 0)
{
if (buf[len - 1] == '\n')
buf[len - 1] = '\0';
if (first)
{
first = FALSE;
*(event_data->text) = g_strdup (buf);
}
else
{
/* remove \n on EOL */
char *tmp;
tmp = g_strconcat (*(event_data->text), " ", buf, (char *) NULL);
g_free (*(event_data->text));
*(event_data->text) = tmp;
}
}
}
fclose (f);
event_data->ret = (*(event_data->text) != NULL);
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */

View File

@ -18,8 +18,15 @@ extern char *clipboard_paste_path;
/*** declarations of public functions ************************************************************/
gboolean copy_file_to_ext_clip (void);
gboolean paste_to_file_from_ext_clip (void);
gboolean clipboard_file_to_ext_clip (const gchar * event_group_name, const gchar * event_name,
gpointer init_data, gpointer data);
gboolean clipboard_file_from_ext_clip (const gchar * event_group_name, const gchar * event_name,
gpointer init_data, gpointer data);
gboolean clipboard_text_to_file (const gchar * event_group_name, const gchar * event_name,
gpointer init_data, gpointer data);
gboolean clipboard_text_from_file (const gchar * event_group_name, const gchar * event_name,
gpointer init_data, gpointer data);
/*** inline functions ****************************************************************************/

View File

@ -56,13 +56,13 @@
#include "lib/vfs/vfs.h"
#include "lib/widget.h"
#include "lib/charsets.h"
#include "lib/event.h" /* mc_event_raise() */
#include "src/history.h"
#include "src/setup.h" /* option_tab_spacing */
#include "src/help.h" /* interactive_display() */
#include "src/selcodepage.h"
#include "src/keybind-defaults.h"
#include "src/clipboard.h" /* copy_file_to_ext_clip, paste_to_file_from_ext_clip */
#include "src/util.h" /* check_for_default() */
#include "src/filemanager/layout.h" /* mc_refresh() */
@ -2602,7 +2602,7 @@ edit_copy_to_X_buf_cmd (WEdit * edit)
return 1;
}
/* try use external clipboard utility */
copy_file_to_ext_clip ();
mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
return 0;
}
@ -2621,7 +2621,7 @@ edit_cut_to_X_buf_cmd (WEdit * edit)
return 1;
}
/* try use external clipboard utility */
copy_file_to_ext_clip ();
mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", NULL);
edit_block_delete_cmd (edit);
edit_mark_cmd (edit, 1);
@ -2635,7 +2635,7 @@ edit_paste_from_X_buf_cmd (WEdit * edit)
{
gchar *tmp;
/* try use external clipboard utility */
paste_to_file_from_ext_clip ();
mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL);
tmp = concat_dir_and_file (mc_config_get_cache_path (), EDIT_CLIP_FILE);
edit_insert_file (edit, tmp);
g_free (tmp);

View File

@ -27,6 +27,8 @@
#include "lib/event.h"
#include "clipboard.h" /* clipboard events */
#include "events_init.h"
/*** global variables ****************************************************************************/
@ -51,6 +53,11 @@ events_init (GError ** error)
/* *INDENT-OFF* */
event_init_t standard_events[] =
{
{MCEVENT_GROUP_CORE, "clipboard_file_to_ext_clip", clipboard_file_to_ext_clip, NULL},
{MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", clipboard_file_from_ext_clip, NULL},
{MCEVENT_GROUP_CORE, "clipboard_text_to_file", clipboard_text_to_file, NULL},
{MCEVENT_GROUP_CORE, "clipboard_text_from_file", clipboard_text_from_file, NULL},
{NULL, NULL, NULL, NULL}
};
/* *INDENT-ON* */

View File

@ -3425,6 +3425,53 @@ event_update_panels (const gchar * event_group_name, const gchar * event_name,
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
/* event callback */
static gboolean
panel_save_curent_file_to_clip_file (const gchar * event_group_name, const gchar * event_name,
gpointer init_data, gpointer data)
{
(void) event_group_name;
(void) event_name;
(void) init_data;
(void) data;
if (current_panel->marked == 0)
mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file",
(gpointer) selection (current_panel)->fname);
else
{
int i;
gboolean first = TRUE;
char *flist = NULL;
for (i = 0; i < current_panel->count; i++)
if (current_panel->dir.list[i].f.marked != 0)
{ /* Skip the unmarked ones */
if (first)
{
flist = g_strdup (current_panel->dir.list[i].fname);
first = FALSE;
}
else
{
/* Add empty lines after the file */
char *tmp;
tmp =
g_strconcat (flist, "\n", current_panel->dir.list[i].fname, (char *) NULL);
g_free (flist);
flist = tmp;
}
}
mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", (gpointer) flist);
g_free (flist);
}
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
@ -4124,6 +4171,8 @@ panel_init (void)
panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
mc_event_add (MCEVENT_GROUP_FILEMANAGER, "update_panels", event_update_panels, NULL, NULL);
mc_event_add (MCEVENT_GROUP_FILEMANAGER, "panel_save_curent_file_to_clip_file",
panel_save_curent_file_to_clip_file, NULL, NULL);
}