mirror of
https://github.com/fish-shell/fish-shell
synced 2024-11-10 23:24:39 +00:00
restyle kill module to match project style
Reduces lint errors from 10 to 9 (-10%). Line count from 242 to 175 (-28%). Another step in resolving issue #2902.
This commit is contained in:
parent
483b798863
commit
4f619c966b
3 changed files with 66 additions and 133 deletions
BIN
key_reader
Executable file
BIN
key_reader
Executable file
Binary file not shown.
173
src/kill.cpp
173
src/kill.cpp
|
@ -1,85 +1,65 @@
|
|||
/** \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.
|
||||
*/
|
||||
// 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.
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <algorithm>
|
||||
#include <list>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <stdbool.h>
|
||||
#include <string>
|
||||
|
||||
#include "fallback.h" // IWYU pragma: keep
|
||||
#include "kill.h"
|
||||
#include "common.h"
|
||||
#include "env.h"
|
||||
#include "exec.h"
|
||||
#include "fallback.h" // IWYU pragma: keep
|
||||
#include "kill.h"
|
||||
#include "path.h"
|
||||
|
||||
/** Kill ring */
|
||||
typedef std::list<wcstring> kill_list_t;
|
||||
static kill_list_t kill_list;
|
||||
|
||||
/**
|
||||
Contents of the X clipboard, at last time we checked it
|
||||
*/
|
||||
/// Contents of the X clipboard, at last time we checked it.
|
||||
static wcstring cut_buffer;
|
||||
|
||||
/**
|
||||
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);
|
||||
/// 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);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void kill_add(const wcstring &str)
|
||||
{
|
||||
void kill_add(const wcstring &str) {
|
||||
ASSERT_IS_MAIN_THREAD();
|
||||
if (str.empty())
|
||||
return;
|
||||
if (str.empty()) return;
|
||||
|
||||
wcstring cmd;
|
||||
wcstring escaped_str;
|
||||
kill_list.push_front(str);
|
||||
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
|
||||
// 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.
|
||||
const env_var_t clipboard_wstr = env_get_string(L"FISH_CLIPBOARD_CMD");
|
||||
if (!clipboard_wstr.missing())
|
||||
{
|
||||
if (!clipboard_wstr.missing()) {
|
||||
escaped_str = escape(str.c_str(), ESCAPE_ALL);
|
||||
cmd.assign(L"echo -n ");
|
||||
cmd.append(escaped_str);
|
||||
cmd.append(clipboard_wstr);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* This is for sending the kill to the X copy-and-paste buffer */
|
||||
if (!has_xsel())
|
||||
{
|
||||
} else {
|
||||
// This is for sending the kill to the X copy-and-paste buffer.
|
||||
if (!has_xsel()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const env_var_t disp_wstr = env_get_string(L"DISPLAY");
|
||||
if (!disp_wstr.missing())
|
||||
{
|
||||
if (!disp_wstr.missing()) {
|
||||
escaped_str = escape(str.c_str(), ESCAPE_ALL);
|
||||
cmd.assign(L"echo -n ");
|
||||
cmd.append(escaped_str);
|
||||
|
@ -87,89 +67,59 @@ void kill_add(const wcstring &str)
|
|||
}
|
||||
}
|
||||
|
||||
if (! cmd.empty())
|
||||
{
|
||||
if (exec_subshell(cmd, false /* do not apply exit status */) == -1)
|
||||
{
|
||||
/*
|
||||
Do nothing on failiure
|
||||
*/
|
||||
if (!cmd.empty()) {
|
||||
if (exec_subshell(cmd, false /* do not apply exit status */) == -1) {
|
||||
// Do nothing on failure.
|
||||
}
|
||||
|
||||
cut_buffer = escaped_str;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Remove first match for specified string from circular list
|
||||
*/
|
||||
static void kill_remove(const wcstring &s)
|
||||
{
|
||||
/// Remove first match for specified string from circular list.
|
||||
static void kill_remove(const wcstring &s) {
|
||||
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);
|
||||
if (iter != kill_list.end()) kill_list.erase(iter);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void kill_replace(const wcstring &old, const wcstring &newv)
|
||||
{
|
||||
void kill_replace(const wcstring &old, const wcstring &newv) {
|
||||
kill_remove(old);
|
||||
kill_add(newv);
|
||||
}
|
||||
|
||||
const wchar_t *kill_yank_rotate()
|
||||
{
|
||||
const wchar_t *kill_yank_rotate() {
|
||||
ASSERT_IS_MAIN_THREAD();
|
||||
// Move the first element to the end
|
||||
if (kill_list.empty())
|
||||
{
|
||||
// Move the first element to the end.
|
||||
if (kill_list.empty()) {
|
||||
return NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
kill_list.splice(kill_list.end(), kill_list, kill_list.begin());
|
||||
return kill_list.front().c_str();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
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;
|
||||
/// 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;
|
||||
|
||||
const env_var_t disp = env_get_string(L"DISPLAY");
|
||||
if (! disp.missing())
|
||||
{
|
||||
if (!disp.missing()) {
|
||||
size_t i;
|
||||
wcstring cmd = L"xsel -t 500 -b";
|
||||
wcstring new_cut_buffer=L"";
|
||||
wcstring new_cut_buffer = L"";
|
||||
wcstring_list_t list;
|
||||
if (exec_subshell(cmd, list, false /* do not apply exit status */) != -1)
|
||||
{
|
||||
|
||||
for (i=0; i<list.size(); i++)
|
||||
{
|
||||
if (exec_subshell(cmd, list, false /* do not apply exit status */) != -1) {
|
||||
for (i = 0; i < list.size(); i++) {
|
||||
wcstring next_line = escape_string(list.at(i), 0);
|
||||
if (i > 0) new_cut_buffer += L"\\n";
|
||||
new_cut_buffer += next_line;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
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) {
|
||||
cut_buffer = new_cut_buffer;
|
||||
kill_list.push_front(new_cut_buffer);
|
||||
}
|
||||
|
@ -178,30 +128,17 @@ static void kill_check_x_buffer()
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
const wchar_t *kill_yank()
|
||||
{
|
||||
const wchar_t *kill_yank() {
|
||||
kill_check_x_buffer();
|
||||
if (kill_list.empty())
|
||||
{
|
||||
if (kill_list.empty()) {
|
||||
return L"";
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
return kill_list.front().c_str();
|
||||
}
|
||||
}
|
||||
|
||||
void kill_sanity_check()
|
||||
{
|
||||
}
|
||||
void kill_sanity_check() {}
|
||||
|
||||
void kill_init()
|
||||
{
|
||||
}
|
||||
|
||||
void kill_destroy()
|
||||
{
|
||||
cut_buffer.clear();
|
||||
}
|
||||
void kill_init() {}
|
||||
|
||||
void kill_destroy() { cut_buffer.clear(); }
|
||||
|
|
26
src/kill.h
26
src/kill.h
|
@ -1,35 +1,31 @@
|
|||
/** \file kill.h
|
||||
Prototypes for the killring.
|
||||
|
||||
Works like the killring in emacs and readline. The killring is cut and paste whith a memory of previous cuts.
|
||||
*/
|
||||
// Prototypes for the killring.
|
||||
//
|
||||
// Works like the killring in emacs and readline. The killring is cut and paste whith a memory of
|
||||
// previous cuts.
|
||||
#ifndef FISH_KILL_H
|
||||
#define FISH_KILL_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
/**
|
||||
Replace the specified string in the killring
|
||||
*/
|
||||
/// Replace the specified string in the killring.
|
||||
void kill_replace(const wcstring &old, const wcstring &newv);
|
||||
|
||||
|
||||
/** Add a string to the top of the killring */
|
||||
/// Add a string to the top of the killring.
|
||||
void kill_add(const wcstring &str);
|
||||
|
||||
/** Rotate the killring */
|
||||
/// Rotate the killring.
|
||||
const wchar_t *kill_yank_rotate();
|
||||
|
||||
/** Paste from the killring */
|
||||
/// Paste from the killring.
|
||||
const wchar_t *kill_yank();
|
||||
|
||||
/** Sanity check */
|
||||
/// Sanity check.
|
||||
void kill_sanity_check();
|
||||
|
||||
/** Initialize the killring */
|
||||
/// Initialize the killring.
|
||||
void kill_init();
|
||||
|
||||
/** Destroy the killring */
|
||||
/// Destroy the killring.
|
||||
void kill_destroy();
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue