fish-shell/src/kill.cpp

56 lines
1.3 KiB
C++
Raw Normal View History

// The killring.
//
// Works like the killring in emacs and readline. The killring is cut and paste with a memory of
// previous cuts.
#include "config.h" // IWYU pragma: keep
2015-07-25 15:14:25 +00:00
#include <stddef.h>
2012-03-04 11:27:41 +00:00
#include <algorithm>
2015-07-25 15:14:25 +00:00
#include <list>
#include <memory>
#include <string>
#include "common.h"
#include "fallback.h" // IWYU pragma: keep
2012-03-04 03:37:55 +00:00
/** Kill ring */
using kill_list_t = std::list<wcstring>;
2012-03-04 05:46:06 +00:00
static kill_list_t kill_list;
2019-03-17 00:26:42 +00:00
void kill_add(wcstring str) {
2012-03-04 05:46:06 +00:00
ASSERT_IS_MAIN_THREAD();
2019-03-17 00:26:42 +00:00
if (!str.empty()) {
kill_list.push_front(std::move(str));
}
}
/// Remove first match for specified string from circular list.
static void kill_remove(const wcstring &s) {
2012-03-04 05:46:06 +00:00
ASSERT_IS_MAIN_THREAD();
auto iter = std::find(kill_list.begin(), kill_list.end(), s);
if (iter != kill_list.end()) kill_list.erase(iter);
}
void kill_replace(const wcstring &old, const wcstring &newv) {
kill_remove(old);
kill_add(newv);
}
2019-03-17 00:26:42 +00:00
wcstring kill_yank_rotate() {
2012-03-04 05:46:06 +00:00
ASSERT_IS_MAIN_THREAD();
// Move the first element to the end.
if (kill_list.empty()) {
2019-03-17 00:26:42 +00:00
return {};
2012-03-04 05:46:06 +00:00
}
kill_list.splice(kill_list.end(), kill_list, kill_list.begin());
2019-03-17 00:26:42 +00:00
return kill_list.front();
}
2019-03-17 00:26:42 +00:00
wcstring kill_yank() {
if (kill_list.empty()) {
2019-03-17 00:26:42 +00:00
return {};
2012-03-04 05:46:06 +00:00
}
2019-03-17 00:26:42 +00:00
return kill_list.front();
}