Skip to content

Commit

Permalink
Interpreting escape sequence only for string typing
Browse files Browse the repository at this point in the history
  • Loading branch information
houmain committed Jan 24, 2024
1 parent e1ed12e commit 42b9f97
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 27 deletions.
4 changes: 2 additions & 2 deletions keymapper.conf
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ ScrollLock >> Boss
BracketLeft >> '['
Shift{BracketRight} >> '}'
BracketRight >> ']'
Shift{Backquote} >> Equal Space # '` '
Shift{Backquote} >> '`'
Backquote >> '~'
Shift{2} >> '@'
Shift{3} >> '#'
Shift{6} >> !Shift Backquote Space # '^ '
Shift{6} >> '^'
Shift{7} >> '&'
Shift{8} >> '*'
Shift{9} >> '('
Expand Down
7 changes: 6 additions & 1 deletion src/client/unix/StringTyperImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ StringTyper::Modifiers get_xkb_modifiers(uint32_t mask) {
//-------------------------------------------------------------------------

void StringTyperImpl::type(std::string_view string, const AddKey& add_key) const {
for (auto character : utf8_to_utf32(string))
auto characters = utf8_to_utf32(string);
replace_all<char32_t>(characters, U"\\n", U"\r");
replace_all<char32_t>(characters, U"\\r", U"\r");
replace_all<char32_t>(characters, U"\\t", U"\t");

for (auto character : characters)
if (auto it = m_dictionary.find(character); it != m_dictionary.end())
add_key(it->second.key, it->second.modifiers);
}
Expand Down
9 changes: 5 additions & 4 deletions src/client/windows/StringTyper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,12 @@ class StringTyperImpl {
}

void type(std::string_view string, const AddKey& add_key) const {
for (auto character : utf8_to_wide(string)) {

if (character == '\n')
character = '\r';
auto characters = utf8_to_wide(string);
replace_all<wchar_t>(characters, L"\\n", L"\r");
replace_all<wchar_t>(characters, L"\\r", L"\r");
replace_all<wchar_t>(characters, L"\\t", L"\t");

for (auto character : characters) {
if (auto it = m_dictionary.find(character); it != m_dictionary.end()) {
const auto& [dead_key, key] = it->second;
if (dead_key.key != Key::none)
Expand Down
22 changes: 2 additions & 20 deletions src/config/ParseConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,28 +408,10 @@ std::string ParseConfig::preprocess(It it, const It end) const {
if (!skip_until(&it, end, mark))
error("Unterminated string");

// interpret escape sequence
auto prev = char{ };
std::for_each(begin, it, [&](char c) {
if (prev == '\\') {
c = [&]() {
switch (c) {
case 'n': return '\n';
case 't': return '\t';
}
result.push_back('\\');
return c;
}();
prev = { };
}
else {
prev = c;
}
result.push_back(c);
});
result.append(begin, it);
}
else {
// output single character
// single character
result.append(begin, ++it);
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/config/StringTyper.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,12 @@ class StringTyper {
private:
std::unique_ptr<class StringTyperImpl> m_impl;
};

template<typename T>
void replace_all(std::basic_string<T>& str, std::basic_string_view<T> from, std::basic_string_view<T> to) {
auto start_pos = size_t{ };
while((start_pos = str.find(from, start_pos)) != std::basic_string<T>::npos) {
str.replace(start_pos, from.size(), to);
start_pos += to.size();
}
}

0 comments on commit 42b9f97

Please sign in to comment.