fish-shell/src/kill.cpp

62 lines
1.4 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
#include "kill.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>
#include <memory>
#include <string>
#include "common.h"
#include "fallback.h" // IWYU pragma: keep
2012-03-04 11:37:55 +08:00
/** Kill ring */
static owning_lock<std::list<wcstring>> s_kill_list;
2019-03-17 08:26:42 +08:00
void kill_add(wcstring str) {
if (!str.empty()) {
s_kill_list.acquire()->push_front(std::move(str));
2019-03-17 08:26:42 +08:00
}
}
void kill_replace(const wcstring &old, const wcstring &newv) {
auto kill_list = s_kill_list.acquire();
// Remove old.
auto iter = std::find(kill_list->begin(), kill_list->end(), old);
if (iter != kill_list->end()) kill_list->erase(iter);
// Add new.
if (!newv.empty()) {
kill_list->push_front(newv);
}
}
2019-03-17 08:26:42 +08:00
wcstring kill_yank_rotate() {
auto kill_list = s_kill_list.acquire();
// Move the first element to the end.
if (kill_list->empty()) {
2019-03-17 08:26:42 +08:00
return {};
2012-03-04 13:46:06 +08:00
}
kill_list->splice(kill_list->end(), *kill_list, kill_list->begin());
return kill_list->front();
}
2019-03-17 08:26:42 +08:00
wcstring kill_yank() {
auto kill_list = s_kill_list.acquire();
if (kill_list->empty()) {
2019-03-17 08:26:42 +08:00
return {};
2012-03-04 13:46:06 +08:00
}
return kill_list->front();
}
wcstring_list_t kill_entries() {
auto kill_list = s_kill_list.acquire();
return wcstring_list_t{kill_list->begin(), kill_list->end()};
2021-04-11 05:36:21 +08:00
}