2016-05-02 05:16:00 +00:00
|
|
|
// The killring.
|
|
|
|
//
|
|
|
|
// Works like the killring in emacs and readline. The killring is cut and paste with a memory of
|
2016-05-13 13:02:28 +00:00
|
|
|
// previous cuts.
|
2016-05-18 22:30:21 +00:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2020-09-08 20:04:44 +00:00
|
|
|
#include "kill.h"
|
|
|
|
|
2012-03-04 11:27:41 +00:00
|
|
|
#include <algorithm>
|
2015-07-25 15:14:25 +00:00
|
|
|
#include <list>
|
2016-05-02 05:16:00 +00:00
|
|
|
#include <string>
|
2022-08-21 06:14:48 +00:00
|
|
|
#include <utility>
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
#include "common.h"
|
2016-05-02 05:16:00 +00:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2012-03-04 03:37:55 +00:00
|
|
|
/** Kill ring */
|
2021-04-22 00:37:44 +00:00
|
|
|
static owning_lock<std::list<wcstring>> s_kill_list;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2019-03-17 00:26:42 +00:00
|
|
|
void kill_add(wcstring str) {
|
|
|
|
if (!str.empty()) {
|
2021-04-22 00:37:44 +00:00
|
|
|
s_kill_list.acquire()->push_front(std::move(str));
|
2019-03-17 00:26:42 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 05:16:00 +00:00
|
|
|
void kill_replace(const wcstring &old, const wcstring &newv) {
|
2021-04-22 00:37:44 +00:00
|
|
|
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);
|
|
|
|
}
|
2006-10-12 13:27:32 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2019-03-17 00:26:42 +00:00
|
|
|
wcstring kill_yank_rotate() {
|
2021-04-22 00:37:44 +00:00
|
|
|
auto kill_list = s_kill_list.acquire();
|
2016-05-02 05:16:00 +00:00
|
|
|
// Move the first element to the end.
|
2021-04-22 00:37:44 +00:00
|
|
|
if (kill_list->empty()) {
|
2019-03-17 00:26:42 +00:00
|
|
|
return {};
|
2012-03-04 05:46:06 +00:00
|
|
|
}
|
2021-04-22 00:37:44 +00:00
|
|
|
kill_list->splice(kill_list->end(), *kill_list, kill_list->begin());
|
|
|
|
return kill_list->front();
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-17 00:26:42 +00:00
|
|
|
wcstring kill_yank() {
|
2021-04-22 00:37:44 +00:00
|
|
|
auto kill_list = s_kill_list.acquire();
|
|
|
|
if (kill_list->empty()) {
|
2019-03-17 00:26:42 +00:00
|
|
|
return {};
|
2012-03-04 05:46:06 +00:00
|
|
|
}
|
2021-04-22 00:37:44 +00:00
|
|
|
return kill_list->front();
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2021-04-09 19:08:56 +00:00
|
|
|
|
|
|
|
wcstring_list_t kill_entries() {
|
2021-04-22 00:37:44 +00:00
|
|
|
auto kill_list = s_kill_list.acquire();
|
|
|
|
return wcstring_list_t{kill_list->begin(), kill_list->end()};
|
2021-04-10 21:36:21 +00:00
|
|
|
}
|