refactor(misc1): move out autocmd related functions

This commit is contained in:
Björn Linse 2021-12-09 21:10:58 +01:00
parent 2ec0e0a868
commit 51822f0655
6 changed files with 65 additions and 61 deletions

View File

@ -1116,12 +1116,6 @@ typedef struct {
pos_T w_cursor_corr; // corrected cursor position pos_T w_cursor_corr; // corrected cursor position
} pos_save_T; } pos_save_T;
// Struct passed to get_v_event() and restore_v_event().
typedef struct {
bool sve_did_save;
hashtab_T sve_hashtab;
} save_v_event_T;
/// Indices into vimmenu_T->strings[] and vimmenu_T->noremap[] for each mode /// Indices into vimmenu_T->strings[] and vimmenu_T->noremap[] for each mode
/// \addtogroup MENU_INDEX /// \addtogroup MENU_INDEX
/// @{ /// @{

View File

@ -303,6 +303,31 @@ const list_T *eval_msgpack_type_lists[] = {
[kMPExt] = NULL, [kMPExt] = NULL,
}; };
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);
}
}
// Return "n1" divided by "n2", taking care of dividing by zero. // Return "n1" divided by "n2", taking care of dividing by zero.
varnumber_T num_divide(varnumber_T n1, varnumber_T n2) varnumber_T num_divide(varnumber_T n1, varnumber_T n2)
FUNC_ATTR_CONST FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_CONST FUNC_ATTR_WARN_UNUSED_RESULT

View File

@ -193,6 +193,13 @@ extern const list_T *eval_msgpack_type_lists[LAST_MSGPACK_TYPE + 1];
#undef LAST_MSGPACK_TYPE #undef LAST_MSGPACK_TYPE
// Struct passed to get_v_event() and restore_v_event().
typedef struct {
bool sve_did_save;
hashtab_T sve_hashtab;
} save_v_event_T;
/// trans_function_name() flags /// trans_function_name() flags
typedef enum { typedef enum {
TFN_INT = 1, ///< May use internal function name TFN_INT = 1, ///< May use internal function name

View File

@ -68,6 +68,7 @@
#include "nvim/spell.h" #include "nvim/spell.h"
#include "nvim/spellfile.h" #include "nvim/spellfile.h"
#include "nvim/strings.h" #include "nvim/strings.h"
#include "nvim/state.h"
#include "nvim/syntax.h" #include "nvim/syntax.h"
#include "nvim/tag.h" #include "nvim/tag.h"
#include "nvim/terminal.h" #include "nvim/terminal.h"

View File

@ -1018,58 +1018,3 @@ void add_time(char_u *buf, size_t buflen, time_t tt)
seconds); 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);
}

View File

@ -4,7 +4,9 @@
#include <assert.h> #include <assert.h>
#include "nvim/ascii.h" #include "nvim/ascii.h"
#include "nvim/autocmd.h"
#include "nvim/edit.h" #include "nvim/edit.h"
#include "nvim/eval.h"
#include "nvim/ex_docmd.h" #include "nvim/ex_docmd.h"
#include "nvim/getchar.h" #include "nvim/getchar.h"
#include "nvim/lib/kvec.h" #include "nvim/lib/kvec.h"
@ -202,3 +204,33 @@ char *get_mode(void)
return buf; return buf;
} }
/// 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);
}