2016-05-02 13:16:00 +08:00
|
|
|
// The killring.
|
|
|
|
//
|
|
|
|
// Works like the killring in emacs and readline. The killring is cut and paste with a memory of
|
2016-05-13 21:02:28 +08:00
|
|
|
// previous cuts.
|
2016-05-19 06:30:21 +08:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
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>
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <memory>
|
2016-05-02 13:16:00 +08:00
|
|
|
#include <string>
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
#include "common.h"
|
2016-05-02 13:16:00 +08:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2012-03-04 11:37:55 +08:00
|
|
|
/** Kill ring */
|
2012-03-04 13:46:06 +08:00
|
|
|
typedef std::list<wcstring> kill_list_t;
|
|
|
|
static kill_list_t kill_list;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2019-03-17 08:26:42 +08:00
|
|
|
void kill_add(wcstring str) {
|
2012-03-04 13:46:06 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2019-03-17 08:26:42 +08:00
|
|
|
if (!str.empty()) {
|
|
|
|
kill_list.push_front(std::move(str));
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-02 13:16:00 +08:00
|
|
|
/// Remove first match for specified string from circular list.
|
|
|
|
static void kill_remove(const wcstring &s) {
|
2012-03-04 13:46:06 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2018-08-12 10:55:06 +08:00
|
|
|
auto iter = std::find(kill_list.begin(), kill_list.end(), s);
|
2016-05-02 13:16:00 +08:00
|
|
|
if (iter != kill_list.end()) kill_list.erase(iter);
|
2006-10-12 21:27:32 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-02 13:16:00 +08:00
|
|
|
void kill_replace(const wcstring &old, const wcstring &newv) {
|
2012-11-19 08:30:30 +08:00
|
|
|
kill_remove(old);
|
|
|
|
kill_add(newv);
|
2006-10-12 21:27:32 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2019-03-17 08:26:42 +08:00
|
|
|
wcstring kill_yank_rotate() {
|
2012-03-04 13:46:06 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2016-05-02 13:16:00 +08:00
|
|
|
// 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
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
kill_list.splice(kill_list.end(), kill_list, kill_list.begin());
|
2019-03-17 08:26:42 +08:00
|
|
|
return kill_list.front();
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2019-03-17 08:26:42 +08:00
|
|
|
wcstring kill_yank() {
|
2016-05-02 13:16:00 +08:00
|
|
|
if (kill_list.empty()) {
|
2019-03-17 08:26:42 +08:00
|
|
|
return {};
|
2012-03-04 13:46:06 +08:00
|
|
|
}
|
2019-03-17 08:26:42 +08:00
|
|
|
return kill_list.front();
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|