2016-05-02 13:16:00 +08:00
|
|
|
// The killring.
|
|
|
|
//
|
|
|
|
// Works like the killring in emacs and readline. The killring is cut and paste with a memory of
|
|
|
|
// previous cuts. It supports integration with the X clipboard.
|
2016-05-19 06:30:21 +08:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2016-05-02 13:16:00 +08:00
|
|
|
#include <stdbool.h>
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <stddef.h>
|
2012-03-04 19:27:41 +08:00
|
|
|
#include <algorithm>
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <list>
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <memory>
|
2016-05-02 13:16:00 +08:00
|
|
|
#include <string>
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "env.h"
|
|
|
|
#include "exec.h"
|
2016-05-02 13:16:00 +08:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
|
|
|
#include "kill.h"
|
2006-10-19 19:50:23 +08:00
|
|
|
#include "path.h"
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2012-03-04 11:37:55 +08:00
|
|
|
/** Kill ring */
|
2012-03-04 13:46:06 +08:00
|
|
|
typedef std::list<wcstring> kill_list_t;
|
|
|
|
static kill_list_t kill_list;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-02 13:16:00 +08:00
|
|
|
/// Contents of the X clipboard, at last time we checked it.
|
2014-09-26 09:20:03 +08:00
|
|
|
static wcstring cut_buffer;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-02 13:16:00 +08:00
|
|
|
/// Test if the xsel command is installed. Since this is called often, cache the result.
|
|
|
|
static int has_xsel() {
|
|
|
|
static signed char res = -1;
|
|
|
|
if (res < 0) {
|
|
|
|
res = !!path_get_path(L"xsel", NULL);
|
2012-01-30 15:22:42 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
return res;
|
2010-11-05 23:22:28 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-02 13:16:00 +08:00
|
|
|
void kill_add(const wcstring &str) {
|
2012-03-04 13:46:06 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2016-05-02 13:16:00 +08:00
|
|
|
if (str.empty()) return;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring cmd;
|
2014-09-26 09:20:03 +08:00
|
|
|
wcstring escaped_str;
|
2012-11-19 08:30:30 +08:00
|
|
|
kill_list.push_front(str);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-02 13:16:00 +08:00
|
|
|
// Check to see if user has set the FISH_CLIPBOARD_CMD variable, and, if so, use it instead of
|
|
|
|
// checking the display, etc.
|
|
|
|
//
|
|
|
|
// I couldn't think of a safe way to allow overide of the echo command too, so, the command used
|
|
|
|
// must accept the input via stdin.
|
2012-11-19 08:30:30 +08:00
|
|
|
const env_var_t clipboard_wstr = env_get_string(L"FISH_CLIPBOARD_CMD");
|
2016-05-02 13:16:00 +08:00
|
|
|
if (!clipboard_wstr.missing()) {
|
2014-09-26 09:20:03 +08:00
|
|
|
escaped_str = escape(str.c_str(), ESCAPE_ALL);
|
2012-05-09 17:33:42 +08:00
|
|
|
cmd.assign(L"echo -n ");
|
|
|
|
cmd.append(escaped_str);
|
|
|
|
cmd.append(clipboard_wstr);
|
2016-05-02 13:16:00 +08:00
|
|
|
} else {
|
|
|
|
// This is for sending the kill to the X copy-and-paste buffer.
|
|
|
|
if (!has_xsel()) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const env_var_t disp_wstr = env_get_string(L"DISPLAY");
|
2016-05-02 13:16:00 +08:00
|
|
|
if (!disp_wstr.missing()) {
|
2013-03-30 09:05:01 +08:00
|
|
|
escaped_str = escape(str.c_str(), ESCAPE_ALL);
|
2013-03-30 08:57:29 +08:00
|
|
|
cmd.assign(L"echo -n ");
|
2012-05-09 17:33:42 +08:00
|
|
|
cmd.append(escaped_str);
|
2013-03-30 08:57:29 +08:00
|
|
|
cmd.append(L" | xsel -i -b");
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2016-05-02 13:16:00 +08:00
|
|
|
if (!cmd.empty()) {
|
|
|
|
if (exec_subshell(cmd, false /* do not apply exit status */) == -1) {
|
|
|
|
// Do nothing on failure.
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
cut_buffer = escaped_str;
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-02 13:16:00 +08:00
|
|
|
/// Remove first match for specified string from circular list.
|
|
|
|
static void kill_remove(const wcstring &s) {
|
2012-03-04 13:46:06 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
|
|
|
kill_list_t::iterator iter = std::find(kill_list.begin(), kill_list.end(), s);
|
2016-05-02 13:16:00 +08:00
|
|
|
if (iter != kill_list.end()) kill_list.erase(iter);
|
2006-10-12 21:27:32 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-02 13:16:00 +08:00
|
|
|
void kill_replace(const wcstring &old, const wcstring &newv) {
|
2012-11-19 08:30:30 +08:00
|
|
|
kill_remove(old);
|
|
|
|
kill_add(newv);
|
2006-10-12 21:27:32 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-02 13:16:00 +08:00
|
|
|
const wchar_t *kill_yank_rotate() {
|
2012-03-04 13:46:06 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2016-05-02 13:16:00 +08:00
|
|
|
// Move the first element to the end.
|
|
|
|
if (kill_list.empty()) {
|
2012-03-04 13:46:06 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
kill_list.splice(kill_list.end(), kill_list, kill_list.begin());
|
|
|
|
return kill_list.front().c_str();
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-02 13:16:00 +08:00
|
|
|
/// Check the X clipboard. If it has been changed, add the new clipboard contents to the fish
|
|
|
|
/// killring.
|
|
|
|
static void kill_check_x_buffer() {
|
|
|
|
if (!has_xsel()) return;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
|
|
|
const env_var_t disp = env_get_string(L"DISPLAY");
|
2016-05-02 13:16:00 +08:00
|
|
|
if (!disp.missing()) {
|
2012-11-19 08:30:30 +08:00
|
|
|
size_t i;
|
|
|
|
wcstring cmd = L"xsel -t 500 -b";
|
2016-05-02 13:16:00 +08:00
|
|
|
wcstring new_cut_buffer = L"";
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring_list_t list;
|
2016-05-02 13:16:00 +08:00
|
|
|
if (exec_subshell(cmd, list, false /* do not apply exit status */) != -1) {
|
|
|
|
for (i = 0; i < list.size(); i++) {
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring next_line = escape_string(list.at(i), 0);
|
2012-01-01 07:57:30 +08:00
|
|
|
if (i > 0) new_cut_buffer += L"\\n";
|
|
|
|
new_cut_buffer += next_line;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-02 13:16:00 +08:00
|
|
|
if (new_cut_buffer.size() > 0) {
|
|
|
|
// The buffer is inserted with backslash escapes, since we don't really like tabs,
|
|
|
|
// newlines, etc. anyway.
|
|
|
|
if (cut_buffer != new_cut_buffer) {
|
2014-09-26 09:20:03 +08:00
|
|
|
cut_buffer = new_cut_buffer;
|
2012-11-19 08:30:30 +08:00
|
|
|
kill_list.push_front(new_cut_buffer);
|
2012-01-01 07:57:30 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-02 13:16:00 +08:00
|
|
|
const wchar_t *kill_yank() {
|
2012-11-19 08:30:30 +08:00
|
|
|
kill_check_x_buffer();
|
2016-05-02 13:16:00 +08:00
|
|
|
if (kill_list.empty()) {
|
2012-03-04 13:46:06 +08:00
|
|
|
return L"";
|
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
return kill_list.front().c_str();
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-02 13:16:00 +08:00
|
|
|
void kill_sanity_check() {}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-02 13:16:00 +08:00
|
|
|
void kill_init() {}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-02 13:16:00 +08:00
|
|
|
void kill_destroy() { cut_buffer.clear(); }
|