aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/misc1.c
diff options
context:
space:
mode:
authorJan Edmund Lazo <jan.lazo@mail.utoronto.ca>2021-11-30 22:13:50 -0500
committerGitHub <noreply@github.com>2021-11-30 22:13:50 -0500
commit828bf128a64466f254629b102e283af35666cd05 (patch)
tree6cda5c2df8d799d87d86dcfabc595e2737b8feda /src/nvim/misc1.c
parent2635b77dba6fa218871441fa3380860405bf9240 (diff)
parent980c68d0362c3ca099c0facef2d08efede76aabf (diff)
downloadrneovim-828bf128a64466f254629b102e283af35666cd05.tar.gz
rneovim-828bf128a64466f254629b102e283af35666cd05.tar.bz2
rneovim-828bf128a64466f254629b102e283af35666cd05.zip
Merge pull request #15840 from vimpostor/vim-8.2.3430
vim-patch:8.2.{3430,3434,3462,3463,3555,3609,3610}: ModeChanged autocmd
Diffstat (limited to 'src/nvim/misc1.c')
-rw-r--r--src/nvim/misc1.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/nvim/misc1.c b/src/nvim/misc1.c
index fd5d154cea..872a2c58e3 100644
--- a/src/nvim/misc1.c
+++ b/src/nvim/misc1.c
@@ -1059,3 +1059,58 @@ void add_time(char_u *buf, size_t buflen, time_t tt)
seconds);
}
}
+
+dict_T *get_v_event(save_v_event_T *sve)
+{
+ dict_T *v_event = get_vim_var_dict(VV_EVENT);
+
+ if (v_event->dv_hashtab.ht_used > 0) {
+ // recursive use of v:event, save, make empty and restore later
+ sve->sve_did_save = true;
+ sve->sve_hashtab = v_event->dv_hashtab;
+ hash_init(&v_event->dv_hashtab);
+ } else {
+ sve->sve_did_save = false;
+ }
+ return v_event;
+}
+
+void restore_v_event(dict_T *v_event, save_v_event_T *sve)
+{
+ tv_dict_free_contents(v_event);
+ if (sve->sve_did_save) {
+ v_event->dv_hashtab = sve->sve_hashtab;
+ } else {
+ hash_init(&v_event->dv_hashtab);
+ }
+}
+
+/// Fires a ModeChanged autocmd.
+void trigger_modechanged(void)
+{
+ if (!has_event(EVENT_MODECHANGED)) {
+ return;
+ }
+
+ char *mode = get_mode();
+ if (STRCMP(mode, last_mode) == 0) {
+ xfree(mode);
+ return;
+ }
+
+ save_v_event_T save_v_event;
+ dict_T *v_event = get_v_event(&save_v_event);
+ tv_dict_add_str(v_event, S_LEN("new_mode"), mode);
+ tv_dict_add_str(v_event, S_LEN("old_mode"), last_mode);
+
+ char_u *pat_pre = concat_str((char_u *)last_mode, (char_u *)":");
+ char_u *pat = concat_str(pat_pre, (char_u *)mode);
+ xfree(pat_pre);
+
+ apply_autocmds(EVENT_MODECHANGED, pat, NULL, false, curbuf);
+ xfree(last_mode);
+ last_mode = mode;
+
+ xfree(pat);
+ restore_v_event(v_event, &save_v_event);
+}