Skip to content

Commit

Permalink
fix autocmd! behavior and support multiple events at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
pit-ray committed Aug 11, 2023
1 parent fa4eba6 commit 7a58332
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 96 deletions.
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
.vimspector.json
.vs
.vscode
:w
:wq
:*
Testing
bin
bin_32
Expand All @@ -24,8 +23,8 @@ debug
debug_cov
debugvs
debugvs32
is_initialized
log
manifests
release
release_32
release_64
Expand All @@ -36,5 +35,4 @@ tmp
venv
w
winget
manifests
wingetcreate.exe
28 changes: 14 additions & 14 deletions src/bind/mode/change_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace vind
}

auto& ac = core::AutoCmd::get_instance() ;
ac.apply_autocmds(core::get_leave_event(m)) ;
ac.apply(core::get_leave_event(m)) ;

if(m == Mode::GUI_VISUAL) {
util::click(KEYCODE_MOUSE_LEFT) ; //release holding mouse
Expand All @@ -57,7 +57,7 @@ namespace vind
opt::VCmdLine::print(opt::GeneralMessage("-- GUI NORMAL --")) ;
}

ac.apply_autocmds(core::get_enter_event(Mode::GUI_NORMAL)) ;
ac.apply(core::get_enter_event(Mode::GUI_NORMAL)) ;
}

//ToResident
Expand All @@ -70,7 +70,7 @@ namespace vind
bool vclmodeout) {
auto& igate = core::InputGate::get_instance() ;
auto& ac = core::AutoCmd::get_instance() ;
ac.apply_autocmds(core::get_leave_event(core::get_global_mode())) ;
ac.apply(core::get_leave_event(core::get_global_mode())) ;

igate.close_all_ports() ;
igate.unabsorb() ;
Expand All @@ -79,7 +79,7 @@ namespace vind
opt::VCmdLine::print(opt::GeneralMessage("-- RESIDENT --")) ;
}

ac.apply_autocmds(core::get_enter_event(core::Mode::RESIDENT)) ;
ac.apply(core::get_enter_event(core::Mode::RESIDENT)) ;
}

//ToGUIVisual
Expand All @@ -91,15 +91,15 @@ namespace vind
const std::string& UNUSED(args),
bool vclmodeout) {
auto& ac = core::AutoCmd::get_instance() ;
ac.apply_autocmds(core::get_leave_event(core::get_global_mode())) ;
ac.apply(core::get_leave_event(core::get_global_mode())) ;

core::set_global_mode(core::Mode::GUI_VISUAL) ;
if(vclmodeout) {
opt::VCmdLine::print(opt::GeneralMessage("-- GUI VISUAL --")) ;
}
util::press_mousestate(KEYCODE_MOUSE_LEFT) ;

ac.apply_autocmds(core::get_enter_event(core::Mode::GUI_VISUAL)) ;
ac.apply(core::get_enter_event(core::Mode::GUI_VISUAL)) ;
}

// All instances share TextAreaScanner to keep staticity of sprocess.
Expand All @@ -123,7 +123,7 @@ namespace vind
}

auto& ac = core::AutoCmd::get_instance() ;
ac.apply_autocmds(core::get_leave_event(mode)) ;
ac.apply(core::get_leave_event(mode)) ;

if(mode == Mode::GUI_NORMAL) {
util::click(KEYCODE_MOUSE_LEFT) ;
Expand All @@ -149,7 +149,7 @@ namespace vind
}
}

ac.apply_autocmds(core::get_enter_event(Mode::EDI_NORMAL)) ;
ac.apply(core::get_enter_event(Mode::EDI_NORMAL)) ;
}

//ToInsert
Expand All @@ -165,7 +165,7 @@ namespace vind
auto m = core::get_global_mode() ;

auto& ac = core::AutoCmd::get_instance() ;
ac.apply_autocmds(core::get_leave_event(m)) ;
ac.apply(core::get_leave_event(m)) ;

if(m == Mode::GUI_NORMAL) {
util::click(KEYCODE_MOUSE_LEFT) ;
Expand All @@ -181,7 +181,7 @@ namespace vind
opt::VCmdLine::print(opt::GeneralMessage("-- INSERT --")) ;
}

ac.apply_autocmds(core::get_enter_event(Mode::INSERT)) ;
ac.apply(core::get_enter_event(Mode::INSERT)) ;
}

//ToEdiVisual
Expand All @@ -193,15 +193,15 @@ namespace vind
const std::string& UNUSED(args),
bool vclmodeout) {
auto& ac = core::AutoCmd::get_instance() ;
ac.apply_autocmds(core::get_leave_event(core::get_global_mode())) ;
ac.apply(core::get_leave_event(core::get_global_mode())) ;

select_words() ;
core::set_global_mode(core::Mode::EDI_VISUAL) ;
if(vclmodeout) {
opt::VCmdLine::print(opt::GeneralMessage("-- EDI VISUAL --")) ;
}

ac.apply_autocmds(core::get_enter_event(core::Mode::EDI_VISUAL)) ;
ac.apply(core::get_enter_event(core::Mode::EDI_VISUAL)) ;
}

//ToEdiVisualLine
Expand All @@ -213,7 +213,7 @@ namespace vind
const std::string& UNUSED(args),
bool vclmodeout) {
auto& ac = core::AutoCmd::get_instance() ;
ac.apply_autocmds(core::get_leave_event(core::get_global_mode())) ;
ac.apply(core::get_leave_event(core::get_global_mode())) ;

select_line_EOL2BOL() ;
core::set_global_mode(
Expand All @@ -222,7 +222,7 @@ namespace vind
opt::VCmdLine::print(opt::GeneralMessage("-- EDI VISUAL LINE--")) ;
}

ac.apply_autocmds(core::get_enter_event(core::Mode::EDI_VISUAL)) ;
ac.apply(core::get_enter_event(core::Mode::EDI_VISUAL)) ;
}
}
}
6 changes: 3 additions & 3 deletions src/bind/mode/command_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,12 @@ namespace vind
// If the mode is changed, then do nothing.
if(core::get_global_mode() == core::Mode::COMMAND) {
core::set_global_mode(*m) ;
core::AutoCmd::get_instance().apply_autocmds(
core::AutoCmd::get_instance().apply(
core::get_leave_event(core::Mode::COMMAND)) ;
}
} ;
auto m = core::get_global_mode() ;
ac.apply_autocmds(core::get_leave_event(m)) ;
ac.apply(core::get_leave_event(m)) ;

std::unique_ptr<core::Mode, decltype(return_mode)>
mode_preserver(new core::Mode(m), return_mode) ;
Expand All @@ -254,7 +254,7 @@ namespace vind
pimpl->hist_idx_ = static_cast<int>(pimpl->hists_.size() - 1) ;

auto result = SystemCall::SUCCEEDED ;
ac.apply_autocmds(core::get_enter_event(core::Mode::COMMAND)) ;
ac.apply(core::get_enter_event(core::Mode::COMMAND)) ;
while(true) {
pimpl->bg_.update() ;

Expand Down
6 changes: 3 additions & 3 deletions src/bind/mode/instant_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ namespace vind
auto& ac = core::AutoCmd::get_instance() ;

auto m = core::get_global_mode() ;
ac.apply_autocmds(core::get_leave_event(m)) ;
ac.apply_autocmds(core::get_enter_event(core::Mode::GUI_NORMAL)) ;
ac.apply(core::get_leave_event(m)) ;
ac.apply(core::get_enter_event(core::Mode::GUI_NORMAL)) ;

core::InputGate::get_instance().close_all_ports_with_refresh() ;
core::InstantKeyAbsorber isa{} ;
Expand Down Expand Up @@ -83,7 +83,7 @@ namespace vind
break ;
}
opt::VCmdLine::reset() ;
ac.apply_autocmds(core::get_enter_event(m)) ;
ac.apply(core::get_enter_event(m)) ;

return res ;
}
Expand Down
57 changes: 38 additions & 19 deletions src/bind/syscmd/autocmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "opt/vcmdline.hpp"
#include "util/debug.hpp"
#include "util/def.hpp"
#include "util/string.hpp"

#include <sstream>

Expand All @@ -26,14 +27,19 @@ namespace vind
auto [event_str, patcmd] = core::extract_double_args(pargs) ;
auto [aupat, cmd] = core::extract_double_args(patcmd) ;

auto event = core::get_autocmd_event(event_str) ;
if(event == core::AutoCmdEvent::UNDEFINED) {
opt::VCmdLine::print(
opt::ErrorMessage("E: Not supported event name")) ;
std::vector<core::AutoCmdEvent> events ;
for(const auto& event_str_part : util::split(event_str, ",")) {
auto event = core::get_autocmd_event(event_str_part) ;
if(event == core::AutoCmdEvent::UNDEFINED) {
opt::VCmdLine::print(
opt::ErrorMessage("E: Not supported event name")) ;

core::Logger::get_instance().error(
event_str + " is an unsupported event name.") ;
return ;
core::Logger::get_instance().error(
event_str + " is an unsupported event name.") ;
return ;
}

events.push_back(event) ;
}

if(aupat.empty()) {
Expand All @@ -56,7 +62,9 @@ namespace vind
return ;
}

core::AutoCmd::get_instance().add_autocmd(event, aupat, cmd) ;
for(const auto event : events) {
core::AutoCmd::get_instance().add(event, aupat, cmd) ;
}
}

AutoCmdRemove::AutoCmdRemove()
Expand All @@ -82,29 +90,40 @@ namespace vind
return ;
}

ac.remove_autocmd(aupat) ;
ac.remove(aupat) ;
return ;
}

auto event = core::get_autocmd_event(event_str) ;
if(event == core::AutoCmdEvent::UNDEFINED) {
opt::VCmdLine::print(
opt::ErrorMessage("E: Not supported event name")) ;
std::vector<core::AutoCmdEvent> events ;
for(const auto& event_str_part : util::split(event_str, ",")) {
auto event = core::get_autocmd_event(event_str_part) ;
if(event == core::AutoCmdEvent::UNDEFINED) {
opt::VCmdLine::print(
opt::ErrorMessage("E: Not supported event name")) ;

core::Logger::get_instance().error(
event_str + " is an unsupported event name.") ;
return ;
core::Logger::get_instance().error(
event_str + " is an unsupported event name.") ;
return ;
}
events.push_back(event) ;
}

if(aupat.empty()) {
ac.remove_autocmd(event) ;
for(const auto event : events) {
ac.remove(event) ;
}
return ;
}
if(cmd.empty()) {
ac.remove_autocmd(event, aupat) ;
for(const auto event : events) {
ac.remove(event, aupat) ;
}
return ;
}
ac.remove_autocmd(event, aupat, cmd) ;

for(const auto event : events) {
ac.remove_and_add(event, aupat, cmd) ;
}
}
}
}
58 changes: 13 additions & 45 deletions src/core/autocmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,34 +119,6 @@ namespace
seqcmds_.erase(seqcmds_.begin() + idx) ;
}

template <typename T1, typename T2>
void remove_pattern_cmd(T1&& pat, T2&& cmd) {
auto idx = get_pat_index(pat) ;
if(idx < 0) {
return ;
}

auto& cmds = seqcmds_[idx] ;

for(std::size_t i = 0 ; i < cmds.size() ; i ++) {
if(cmds[i].size() != cmd.size()) {
continue ;
}

bool is_same = true ;
for(std::size_t j = 0 ; j < cmd.size() ; j ++) {
if(*(cmds[i][j]) != *(cmd[j])) {
is_same = false ;
break ;
}
}
if(is_same) {
cmds.erase(cmds.begin() + i) ;
return ;
}
}
}

void match_pattern(const std::string& query, std::unordered_set<int>& indices) {
for(std::size_t i = 0 ; i < pats_.size() ; i ++) {
if(pats_[i].match(query)) {
Expand Down Expand Up @@ -227,7 +199,7 @@ namespace vind
return instance ;
}

void AutoCmd::apply_autocmds(AutoCmdEvent event, DWORD procid) {
void AutoCmd::apply(AutoCmdEvent event, DWORD procid) {
if(event == AutoCmdEvent::UNDEFINED) {
return ;
}
Expand Down Expand Up @@ -306,7 +278,7 @@ namespace vind
}
}

void AutoCmd::add_autocmd(
void AutoCmd::add(
AutoCmdEvent event,
const std::string& pattern,
const std::string& cmdstr) {
Expand All @@ -327,32 +299,28 @@ namespace vind
evt.add_pattern_cmd(util::A2a(pattern), std::move(cmd)) ;
}

void AutoCmd::remove_autocmd(AutoCmdEvent event) {
void AutoCmd::remove(AutoCmdEvent event) {
pimpl->events_[static_cast<int>(event)].clear() ;
}

void AutoCmd::remove_autocmd(
AutoCmdEvent event,
const std::string& pattern) {
void AutoCmd::remove(
AutoCmdEvent event,
const std::string& pattern) {
pimpl->events_[static_cast<int>(event)].remove_pattern(pattern) ;
}

void AutoCmd::remove_autocmd(const std::string& pattern) {
void AutoCmd::remove(const std::string& pattern) {
for(std::size_t i = 0 ; i < pimpl->events_.size() ; i ++) {
pimpl->events_[i].remove_pattern(pattern) ;
}
}

void AutoCmd::remove_autocmd(
AutoCmdEvent event,
const std::string& pattern,
const std::string& cmdstr) {

auto cmd = parse_command(cmdstr) ;
if(cmd.empty()) {
return ;
}
pimpl->events_[static_cast<int>(event)].remove_pattern_cmd(pattern, cmd) ;
void AutoCmd::remove_and_add(
AutoCmdEvent event,
const std::string& pattern,
const std::string& cmdstr) {
remove(event, pattern) ;
add(event, pattern, cmdstr) ;
}
}
}
Loading

0 comments on commit 7a58332

Please sign in to comment.