2005-09-20 13:26:39 +00:00
|
|
|
/** \file kill.c
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2006-08-11 01:18:35 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <dirent.h>
|
2012-03-04 11:27:41 +00:00
|
|
|
#include <algorithm>
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-02-28 13:17:16 +00:00
|
|
|
#include "fallback.h"
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "util.h"
|
2006-02-28 13:17:16 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "wutil.h"
|
|
|
|
#include "kill.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "sanity.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "env.h"
|
|
|
|
#include "exec.h"
|
2006-10-19 11:50:23 +00:00
|
|
|
#include "path.h"
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Maximum entries in killring
|
|
|
|
*/
|
|
|
|
#define KILL_MAX 8192
|
|
|
|
|
2012-03-04 03:37:55 +00:00
|
|
|
/** Last kill string */
|
2012-03-04 05:46:06 +00:00
|
|
|
//static ll_node_t *kill_last=0;
|
2012-03-04 03:37:55 +00:00
|
|
|
|
|
|
|
/** Current kill string */
|
2012-03-04 05:46:06 +00:00
|
|
|
//static ll_node_t *kill_current=0;
|
2012-03-04 03:37:55 +00:00
|
|
|
|
|
|
|
/** Kill ring */
|
2012-03-04 05:46:06 +00:00
|
|
|
typedef std::list<wcstring> kill_list_t;
|
|
|
|
static kill_list_t kill_list;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
Contents of the X clipboard, at last time we checked it
|
|
|
|
*/
|
|
|
|
static wchar_t *cut_buffer=0;
|
|
|
|
|
|
|
|
/**
|
2010-11-11 15:07:14 +00:00
|
|
|
Test if the xsel command is installed. Since this is called often,
|
2010-11-05 15:22:28 +00:00
|
|
|
cache the result.
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
|
|
|
static int has_xsel()
|
|
|
|
{
|
2010-11-11 15:07:14 +00:00
|
|
|
static int called=0;
|
2010-11-05 15:22:28 +00:00
|
|
|
static int res = 0;
|
2012-01-30 07:22:42 +00:00
|
|
|
|
2010-11-11 15:07:14 +00:00
|
|
|
if (!called) {
|
2012-01-30 07:22:42 +00:00
|
|
|
wchar_t *path = path_get_path( L"xsel" );
|
2010-11-11 15:07:14 +00:00
|
|
|
res = !!path;
|
2012-01-30 07:22:42 +00:00
|
|
|
free(path);
|
2010-11-11 15:07:14 +00:00
|
|
|
called = 1;
|
2012-01-30 07:22:42 +00:00
|
|
|
}
|
|
|
|
|
2010-11-05 15:22:28 +00:00
|
|
|
return res;
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-03-04 03:37:55 +00:00
|
|
|
void kill_add( const wcstring &str )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-03-04 05:46:06 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
|
|
|
if (str.empty())
|
|
|
|
return;
|
|
|
|
|
2012-05-09 09:33:42 +00:00
|
|
|
wcstring cmd;
|
|
|
|
wchar_t *escaped_str = NULL;
|
2012-03-04 05:46:06 +00:00
|
|
|
kill_list.push_front(str);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2010-11-11 15:07:14 +00: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-01-12 17:37:10 +00:00
|
|
|
|
2012-01-14 10:42:17 +00:00
|
|
|
const env_var_t clipboard_wstr = env_get_string(L"FISH_CLIPBOARD_CMD");
|
|
|
|
if( !clipboard_wstr.missing() )
|
2010-11-11 15:07:14 +00:00
|
|
|
{
|
2012-03-04 03:37:55 +00:00
|
|
|
escaped_str = escape( str.c_str(), 1 );
|
2012-05-09 09:33:42 +00:00
|
|
|
cmd.assign(L"echo -n ");
|
|
|
|
cmd.append(escaped_str);
|
|
|
|
cmd.append(clipboard_wstr);
|
2010-11-11 15:07:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
/* This is for sending the kill to the X copy-and-paste buffer */
|
2010-11-11 15:07:14 +00:00
|
|
|
if( !has_xsel() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-14 10:42:17 +00:00
|
|
|
const env_var_t disp_wstr = env_get_string( L"DISPLAY" );
|
|
|
|
if( !disp_wstr.missing() )
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
2012-03-04 03:37:55 +00:00
|
|
|
escaped_str = escape( str.c_str(), 1 );
|
2012-05-09 09:33:42 +00:00
|
|
|
cmd.assign(L"echo ");
|
|
|
|
cmd.append(escaped_str);
|
|
|
|
cmd.append(L"|xsel -b" );
|
2010-11-11 15:07:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-09 09:33:42 +00:00
|
|
|
if (! cmd.empty())
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-05-09 09:33:42 +00:00
|
|
|
if( exec_subshell(cmd) == -1 )
|
2006-10-19 15:36:03 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
/*
|
|
|
|
Do nothing on failiure
|
2006-10-19 15:36:03 +00:00
|
|
|
*/
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
free( cut_buffer );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
cut_buffer = escaped_str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-12 13:27:32 +00:00
|
|
|
/**
|
|
|
|
Remove first match for specified string from circular list
|
|
|
|
*/
|
2012-03-04 03:37:55 +00:00
|
|
|
static void kill_remove( const wcstring &s )
|
2006-10-12 13:27:32 +00:00
|
|
|
{
|
2012-03-04 05:46:06 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
|
|
|
kill_list_t::iterator iter = std::find(kill_list.begin(), kill_list.end(), s);
|
|
|
|
if (iter != kill_list.end())
|
|
|
|
kill_list.erase(iter);
|
2006-10-12 13:27:32 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
|
2010-09-18 01:51:16 +00:00
|
|
|
|
2012-03-04 03:37:55 +00:00
|
|
|
void kill_replace( const wcstring &old, const wcstring &newv )
|
2006-10-12 13:27:32 +00:00
|
|
|
{
|
|
|
|
kill_remove( old );
|
2011-12-27 03:18:46 +00:00
|
|
|
kill_add( newv );
|
2006-10-12 13:27:32 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-01-14 11:41:50 +00:00
|
|
|
const wchar_t *kill_yank_rotate()
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2012-03-04 05:46:06 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
|
|
|
// Move the first element to the end
|
|
|
|
if (kill_list.empty()) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
kill_list.splice(kill_list.end(), kill_list, kill_list.begin());
|
|
|
|
return kill_list.front().c_str();
|
|
|
|
}
|
2005-09-20 13:26:39 +00: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()
|
2012-01-14 10:42:17 +00:00
|
|
|
{
|
2005-09-20 13:26:39 +00:00
|
|
|
if( !has_xsel() )
|
|
|
|
return;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-01-14 10:42:17 +00:00
|
|
|
const env_var_t disp = env_get_string(L"DISPLAY");
|
|
|
|
if( ! disp.missing())
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2011-12-31 23:57:30 +00:00
|
|
|
size_t i;
|
|
|
|
wcstring cmd = L"xsel -t 500 -b";
|
|
|
|
wcstring new_cut_buffer=L"";
|
|
|
|
wcstring_list_t list;
|
2012-02-08 07:35:41 +00:00
|
|
|
if( exec_subshell( cmd, list ) != -1 )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2011-12-31 23:57:30 +00:00
|
|
|
for( i=0; i<list.size(); i++ )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2011-12-31 23:57:30 +00:00
|
|
|
wcstring next_line = escape_string( list.at(i), 0 );
|
|
|
|
if (i > 0) new_cut_buffer += L"\\n";
|
|
|
|
new_cut_buffer += next_line;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2011-12-31 23:57:30 +00:00
|
|
|
if( new_cut_buffer.size() > 0 )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
/*
|
2006-10-19 15:36:03 +00:00
|
|
|
The buffer is inserted with backslash escapes,
|
|
|
|
since we don't really like tabs, newlines,
|
|
|
|
etc. anyway.
|
|
|
|
*/
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2011-12-31 23:57:30 +00:00
|
|
|
if (cut_buffer == NULL || cut_buffer != new_cut_buffer)
|
|
|
|
{
|
|
|
|
free(cut_buffer);
|
|
|
|
cut_buffer = wcsdup(new_cut_buffer.c_str());
|
2012-03-04 05:46:06 +00:00
|
|
|
kill_list.push_front( new_cut_buffer );
|
2011-12-31 23:57:30 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-14 11:41:50 +00:00
|
|
|
const wchar_t *kill_yank()
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
kill_check_x_buffer();
|
2012-03-04 05:46:06 +00:00
|
|
|
if (kill_list.empty()) {
|
|
|
|
return L"";
|
|
|
|
} else {
|
|
|
|
return kill_list.front().c_str();
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void kill_sanity_check()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void kill_init()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void kill_destroy()
|
|
|
|
{
|
|
|
|
if( cut_buffer )
|
|
|
|
free( cut_buffer );
|
|
|
|
}
|
|
|
|
|