From c46e2925e1f822713035c6a680d4addc77f294e9 Mon Sep 17 00:00:00 2001 From: Ilia Maslakov Date: Tue, 8 Jun 2010 19:23:00 +0000 Subject: [PATCH] Ticket #30 (use external clipboard utility) added params clipbord_store, clipbord_paste into [Misc] section. created src/clipbord.[ch] added copy_file_to_ext_clip, paste_to_file_from_ext_clip for copy/paste text to the global X clipboard. Signed-off-by: Andrew Borodin Signed-off-by: Ilia Maslakov --- src/Makefile.am | 1 + src/clipboard.c | 80 ++++++++++++++++++++++++++++++++++++++++++++ src/clipboard.h | 12 +++++++ src/editor/editcmd.c | 10 +++++- src/ext.c | 1 + src/main.c | 7 ++++ src/main.h | 3 ++ src/setup.c | 5 +++ src/widget.c | 9 +++++ 9 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 src/clipboard.c create mode 100644 src/clipboard.h diff --git a/src/Makefile.am b/src/Makefile.am index 3b44027f2..20d3296e7 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -87,6 +87,7 @@ mc_SOURCES = \ boxes.c boxes.h \ chmod.c chmod.h \ chown.c chown.h \ + clipboard.c clipboard.h \ cmd.c cmd.h \ command.c command.h \ complete.c \ diff --git a/src/clipboard.c b/src/clipboard.c new file mode 100644 index 000000000..7de7a60f1 --- /dev/null +++ b/src/clipboard.c @@ -0,0 +1,80 @@ +/* + Util for external clipboard. + + Copyright (C) 2009 The Free Software Foundation, Inc. + + Written by: + Ilia Maslakon , 2010. + + This file is part of the Midnight Commander. + + The Midnight Commander is free software; you can redistribute it + and/or modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The Midnight Commander is distributed in the hope that it will be + useful, but WITHOUT ANY WARRANTY; without even the implied warranty + of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + MA 02110-1301, USA. + */ + +#include + +#include +#include + +#include "lib/global.h" +#include "lib/fileloc.h" + +#include "main.h" +#include "src/execute.h" + +#include "clipboard.h" + +gboolean +copy_file_to_ext_clip (void) +{ + char *tmp, *cmd; + int res = 0; + const char *d = getenv ("DISPLAY"); + + if (d == NULL || clipbord_store_path == NULL || clipbord_store_path[0] =='\0') + return FALSE; + + tmp = concat_dir_and_file (home_dir, EDIT_CLIP_FILE); + cmd = g_strconcat (clipbord_store_path, " ", tmp, " 2>/dev/null", (char *) NULL); + + if (cmd != NULL) + res = my_system (EXECUTE_AS_SHELL, shell, cmd); + + g_free (cmd); + g_free (tmp); + return TRUE; +} + +gboolean +paste_to_file_from_ext_clip (void) +{ + char *tmp, *cmd; + int res = 0; + const char *d = getenv ("DISPLAY"); + + if (d == NULL || clipbord_paste_path == NULL || clipbord_paste_path[0] == '\0') + return FALSE; + + tmp = concat_dir_and_file (home_dir, EDIT_CLIP_FILE); + cmd = g_strconcat (clipbord_paste_path, " > ", tmp, " 2>/dev/null", (char *) NULL); + + if (cmd != NULL) + res = my_system (EXECUTE_AS_SHELL, shell, cmd); + + g_free (cmd); + g_free (tmp); + return TRUE; +} diff --git a/src/clipboard.h b/src/clipboard.h new file mode 100644 index 000000000..0c96b7ce1 --- /dev/null +++ b/src/clipboard.h @@ -0,0 +1,12 @@ + +/** \file clipboard.h + * \brief Header: Util for external clipboard + */ + +#ifndef MC_CLIPBOARD_H +#define MC_CLIPBOARD_H + +gboolean copy_file_to_ext_clip (void); +gboolean paste_to_file_from_ext_clip (void); + +#endif diff --git a/src/editor/editcmd.c b/src/editor/editcmd.c index 10ba87f26..21cabb601 100644 --- a/src/editor/editcmd.c +++ b/src/editor/editcmd.c @@ -62,6 +62,7 @@ #include "src/charsets.h" #include "src/selcodepage.h" #include "src/cmddef.h" +#include "src/clipboard.h" /* copy_file_to_ext_clip, paste_to_file_from_ext_clip */ #include "src/editor/edit-impl.h" #include "src/editor/editlock.h" @@ -2165,7 +2166,6 @@ edit_save_block_to_clip_file (WEdit * edit, long start, long finish) return ret; } - void edit_paste_from_history (WEdit * edit) { @@ -2185,6 +2185,9 @@ edit_copy_to_X_buf_cmd (WEdit * edit) get_sys_error (_("Unable to save to file"))); return 1; } + /* try use external clipboard utility */ + copy_file_to_ext_clip (); + edit_mark_cmd (edit, 1); return 0; } @@ -2200,6 +2203,9 @@ edit_cut_to_X_buf_cmd (WEdit * edit) edit_error_dialog (_("Cut to clipboard"), _("Unable to save to file")); return 1; } + /* try use external clipboard utility */ + copy_file_to_ext_clip (); + edit_block_delete_cmd (edit); edit_mark_cmd (edit, 1); return 0; @@ -2209,6 +2215,8 @@ void edit_paste_from_X_buf_cmd (WEdit * edit) { gchar *tmp; + /* try use external clipboard utility */ + paste_to_file_from_ext_clip (); tmp = concat_dir_and_file (home_dir, EDIT_CLIP_FILE); edit_insert_file (edit, tmp); g_free (tmp); diff --git a/src/ext.c b/src/ext.c index 16183d6da..8bcf21e80 100644 --- a/src/ext.c +++ b/src/ext.c @@ -797,3 +797,4 @@ regex_command (const char *filename, const char *action, int *move_dir) return -1; return ret; } + diff --git a/src/main.c b/src/main.c index 00cc0a51c..83be833f7 100644 --- a/src/main.c +++ b/src/main.c @@ -256,6 +256,10 @@ int boot_current_is_left = 1; */ int xtree_mode = 0; +/* path to X clipboard utility */ +char* clipbord_store_path = NULL; +char* clipbord_paste_path = NULL; + /* If set, then print to the given file the last directory we were at */ static char *last_wd_string = NULL; /* Set to 1 to suppress printing the last directory */ @@ -2212,6 +2216,9 @@ main (int argc, char *argv[]) free_codepages_list (); g_free (autodetect_codeset); #endif + g_free (clipbord_store_path); + g_free (clipbord_paste_path); + str_uninit_strings (); g_free (mc_run_param0); diff --git a/src/main.h b/src/main.h index 1b369c26d..8a2712eb5 100644 --- a/src/main.h +++ b/src/main.h @@ -69,6 +69,9 @@ extern int eight_bit_clean; extern int full_eight_bits; #endif /* !HAVE_CHARSET */ +extern char* clipbord_store_path; +extern char* clipbord_paste_path; + extern int utf8_display; extern int fast_refresh; diff --git a/src/setup.c b/src/setup.c index 7e0c0c584..aa7933578 100644 --- a/src/setup.c +++ b/src/setup.c @@ -827,6 +827,8 @@ load_setup (void) if (buffer != NULL) utf8_display = str_isutf8 (buffer); #endif /* HAVE_CHARSET */ + clipbord_store_path = mc_config_get_string (mc_main_config, "Misc", "clipboard_store", ""); + clipbord_paste_path = mc_config_get_string (mc_main_config, "Misc", "clipboard_paste", ""); } gboolean @@ -858,6 +860,9 @@ save_setup (void) get_codepage_id (default_source_codepage)); mc_config_set_string (mc_main_config, "Misc", "autodetect_codeset", autodetect_codeset); #endif /* HAVE_CHARSET */ + mc_config_set_string (mc_main_config, "Misc", "clipboard_store", clipbord_store_path); + mc_config_set_string (mc_main_config, "Misc", "clipboard_paste", clipbord_paste_path); + tmp_profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL); ret = mc_config_save_to_file (mc_main_config, tmp_profile, NULL); diff --git a/src/widget.c b/src/widget.c index 76490a7c5..7c6c366de 100644 --- a/src/widget.c +++ b/src/widget.c @@ -61,6 +61,7 @@ #include "panel.h" /* current_panel */ #include "main.h" /* confirm_history_cleanup */ #include "setup.h" /* num_history_items_recorded */ +#include "clipboard.h" /* copy_file_to_ext_clip, paste_to_file_from_ext_clip */ static void widget_selectcolor (Widget * w, gboolean focused, gboolean hotkey) @@ -1824,6 +1825,8 @@ copy_region (WInput * in, int x_first, int x_last) { /* Copy selected files to clipboard */ panel_save_curent_file_to_clip_file (); + /* try use external clipboard utility */ + copy_file_to_ext_clip (); return; } @@ -1833,7 +1836,10 @@ copy_region (WInput * in, int x_first, int x_last) last = str_offset_to_pos (in->buffer, last); kill_buffer = g_strndup (in->buffer + first, last - first); + save_text_to_clip_file (kill_buffer); + /* try use external clipboard utility */ + copy_file_to_ext_clip (); } static void @@ -1922,6 +1928,9 @@ ins_from_clip (WInput * in) { char *p = NULL; + /* try use external clipboard utility */ + paste_to_file_from_ext_clip (); + if (load_text_from_clip_file (&p)) { char *pp;