summaryrefslogtreecommitdiff
path: root/src/misc1.c
diff options
context:
space:
mode:
author=?UTF-8?q?Magnus=20Gro=C3=9F?= <magnus.gross@rwth-aachen.de>2021-09-12 13:39:55 +0200
committerBram Moolenaar <Bram@vim.org>2021-09-12 13:39:55 +0200
commitf1e8876fa2359b572d262772747405d3616db670 (patch)
tree1b33a32bccaa4522f14f2aad8ee8044295635d0b /src/misc1.c
parent464393a6961d9b9de2bfe9c05f8e2ae5bdec0293 (diff)
downloadvim-git-f1e8876fa2359b572d262772747405d3616db670.tar.gz
patch 8.2.3430: no generic way to trigger an autocommand on mode changev8.2.3430
Problem: No generic way to trigger an autocommand on mode change. Solution: Add the ModeChanged autocommand event. (Magnus Gross, closes #8856)
Diffstat (limited to 'src/misc1.c')
-rw-r--r--src/misc1.c41
1 files changed, 40 insertions, 1 deletions
diff --git a/src/misc1.c b/src/misc1.c
index 9708502ca..1dd07f906 100644
--- a/src/misc1.c
+++ b/src/misc1.c
@@ -630,7 +630,7 @@ ask_yesno(char_u *str, int direct)
void
f_mode(typval_T *argvars, typval_T *rettv)
{
- char_u buf[4];
+ char_u buf[MODE_MAX_LENGTH];
if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL)
return;
@@ -2643,3 +2643,42 @@ path_with_url(char_u *fname)
// "://" or ":\\" must follow
return path_is_url(p);
}
+
+/*
+ * Fires a ModeChanged autocmd
+ */
+ void
+trigger_modechanged()
+{
+#if defined(FEAT_EVAL) || defined(PROTO)
+ dict_T *v_event;
+ typval_T rettv;
+ typval_T tv;
+ char_u *pat_pre;
+ char_u *pat;
+
+ if (!has_modechanged())
+ return;
+
+ v_event = get_vim_var_dict(VV_EVENT);
+
+ tv.v_type = VAR_UNKNOWN;
+ f_mode(&tv, &rettv);
+ (void)dict_add_string(v_event, "new_mode", rettv.vval.v_string);
+ (void)dict_add_string(v_event, "old_mode", last_mode);
+ dict_set_items_ro(v_event);
+
+ // concatenate modes in format "old_mode:new_mode"
+ pat_pre = concat_str(last_mode, (char_u*)":");
+ pat = concat_str(pat_pre, rettv.vval.v_string);
+ vim_free(pat_pre);
+
+ apply_autocmds(EVENT_MODECHANGED, pat, NULL, FALSE, curbuf);
+ STRCPY(last_mode, rettv.vval.v_string);
+
+ vim_free(rettv.vval.v_string);
+ vim_free(pat);
+ dict_free_contents(v_event);
+ hash_init(&v_event->dv_hashtab);
+#endif
+}