diff options
Diffstat (limited to 'src/nvim/autocmd.c')
-rw-r--r-- | src/nvim/autocmd.c | 129 |
1 files changed, 67 insertions, 62 deletions
diff --git a/src/nvim/autocmd.c b/src/nvim/autocmd.c index 1f8d6e6a1f..b87c7886e3 100644 --- a/src/nvim/autocmd.c +++ b/src/nvim/autocmd.c @@ -796,13 +796,13 @@ void do_autocmd(char *arg_in, int forceit) // Expand environment variables in the pattern. Set 'shellslash', we want // forward slashes here. - if (vim_strchr((char_u *)pat, '$') != NULL || vim_strchr((char_u *)pat, '~') != NULL) { + if (vim_strchr(pat, '$') != NULL || vim_strchr(pat, '~') != NULL) { #ifdef BACKSLASH_IN_FILENAME int p_ssl_save = p_ssl; p_ssl = true; #endif - envpat = (char *)expand_env_save((char_u *)pat); + envpat = expand_env_save(pat); #ifdef BACKSLASH_IN_FILENAME p_ssl = p_ssl_save; #endif @@ -1070,10 +1070,9 @@ int autocmd_register(int64_t id, event_T event, char *pat, int patlen, int group char *reg_pat; ap->buflocal_nr = 0; - reg_pat = (char *)file_pat_to_reg_pat((char_u *)pat, (char_u *)pat + patlen, &ap->allow_dirs, - true); + reg_pat = file_pat_to_reg_pat(pat, pat + patlen, &ap->allow_dirs, true); if (reg_pat != NULL) { - ap->reg_prog = vim_regcomp((char_u *)reg_pat, RE_MAGIC); + ap->reg_prog = vim_regcomp(reg_pat, RE_MAGIC); } xfree(reg_pat); if (reg_pat == NULL || ap->reg_prog == NULL) { @@ -1153,7 +1152,7 @@ size_t aucmd_pattern_length(char *pat) char *endpat; - for (; *pat; pat = (*endpat == ',' ? endpat + 1 : endpat)) { + for (; *pat; pat = endpat + 1) { // Find end of the pattern. // Watch out for a comma in braces, like "*.\{obj,o\}". endpat = pat; @@ -1221,7 +1220,7 @@ int do_doautocmd(char *arg_start, bool do_msg, bool *did_something) // Loop over the events. while (*arg && !ends_excmd(*arg) && !ascii_iswhite(*arg)) { if (apply_autocmds_group(event_name2nr(arg, &arg), fname, NULL, true, group, - curbuf, NULL)) { + curbuf, NULL, NULL)) { nothing_done = false; } } @@ -1241,8 +1240,8 @@ void ex_doautoall(exarg_T *eap) { int retval = OK; aco_save_T aco; - char_u *arg = (char_u *)eap->arg; - int call_do_modelines = check_nomodeline((char **)&arg); + char *arg = eap->arg; + int call_do_modelines = check_nomodeline(&arg); bufref_T bufref; bool did_aucmd; @@ -1261,7 +1260,7 @@ void ex_doautoall(exarg_T *eap) set_bufref(&bufref, buf); // execute the autocommands for this buffer - retval = do_doautocmd((char *)arg, false, &did_aucmd); + retval = do_doautocmd(arg, false, &did_aucmd); if (call_do_modelines && did_aucmd) { // Execute the modeline settings, but don't set window-local @@ -1282,7 +1281,7 @@ void ex_doautoall(exarg_T *eap) // Execute autocommands for the current buffer last. if (retval == OK) { - (void)do_doautocmd((char *)arg, false, &did_aucmd); + (void)do_doautocmd(arg, false, &did_aucmd); if (call_do_modelines && did_aucmd) { do_modelines(0); } @@ -1364,7 +1363,7 @@ void aucmd_prepbuf(aco_save_T *aco, buf_T *buf) // Make sure w_localdir and globaldir are NULL to avoid a chdir() in // win_enter_ext(). XFREE_CLEAR(aucmd_win->w_localdir); - aco->globaldir = (char *)globaldir; + aco->globaldir = globaldir; globaldir = NULL; block_autocmds(); // We don't want BufEnter/WinEnter autocommands. @@ -1451,7 +1450,7 @@ win_found: hash_init(&aucmd_win->w_vars->dv_hashtab); // re-use the hashtab xfree(globaldir); - globaldir = (char_u *)aco->globaldir; + globaldir = aco->globaldir; // the buffer contents may have changed check_cursor(); @@ -1504,10 +1503,9 @@ win_found: /// @param buf Buffer for <abuf> /// /// @return true if some commands were executed. -bool apply_autocmds(event_T event, char_u *fname, char_u *fname_io, bool force, buf_T *buf) +bool apply_autocmds(event_T event, char *fname, char *fname_io, bool force, buf_T *buf) { - return apply_autocmds_group(event, (char *)fname, (char *)fname_io, force, AUGROUP_ALL, buf, - NULL); + return apply_autocmds_group(event, fname, fname_io, force, AUGROUP_ALL, buf, NULL, NULL); } /// Like apply_autocmds(), but with extra "eap" argument. This takes care of @@ -1521,11 +1519,10 @@ bool apply_autocmds(event_T event, char_u *fname, char_u *fname_io, bool force, /// @param exarg Ex command arguments /// /// @return true if some commands were executed. -bool apply_autocmds_exarg(event_T event, char_u *fname, char_u *fname_io, bool force, buf_T *buf, +bool apply_autocmds_exarg(event_T event, char *fname, char *fname_io, bool force, buf_T *buf, exarg_T *eap) { - return apply_autocmds_group(event, (char *)fname, (char *)fname_io, force, AUGROUP_ALL, buf, - eap); + return apply_autocmds_group(event, fname, fname_io, force, AUGROUP_ALL, buf, eap, NULL); } /// Like apply_autocmds(), but handles the caller's retval. If the script @@ -1541,15 +1538,15 @@ bool apply_autocmds_exarg(event_T event, char_u *fname, char_u *fname_io, bool f /// @param[in,out] retval caller's retval /// /// @return true if some autocommands were executed -bool apply_autocmds_retval(event_T event, char_u *fname, char_u *fname_io, bool force, buf_T *buf, +bool apply_autocmds_retval(event_T event, char *fname, char *fname_io, bool force, buf_T *buf, int *retval) { if (should_abort(*retval)) { return false; } - bool did_cmd = apply_autocmds_group(event, (char *)fname, (char *)fname_io, force, - AUGROUP_ALL, buf, NULL); + bool did_cmd = apply_autocmds_group(event, fname, fname_io, force, + AUGROUP_ALL, buf, NULL, NULL); if (did_cmd && aborting()) { *retval = FAIL; } @@ -1568,7 +1565,7 @@ bool has_event(event_T event) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT /// the current mode. bool has_cursorhold(void) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT { - return has_event((get_real_state() == NORMAL_BUSY ? EVENT_CURSORHOLD : EVENT_CURSORHOLDI)); + return has_event((get_real_state() == MODE_NORMAL_BUSY ? EVENT_CURSORHOLD : EVENT_CURSORHOLDI)); // return first_autopat[] != NULL; } @@ -1578,7 +1575,7 @@ bool trigger_cursorhold(void) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT if (!did_cursorhold && has_cursorhold() && reg_recording == 0 && typebuf.tb_len == 0 && !ins_compl_active()) { int state = get_real_state(); - if (state == NORMAL_BUSY || (state & INSERT) != 0) { + if (state == MODE_NORMAL_BUSY || (state & MODE_INSERT) != 0) { return true; } } @@ -1598,7 +1595,7 @@ bool trigger_cursorhold(void) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT /// /// @return true if some commands were executed. bool apply_autocmds_group(event_T event, char *fname, char *fname_io, bool force, int group, - buf_T *buf, exarg_T *eap) + buf_T *buf, exarg_T *eap, Object *data) { char *sfname = NULL; // short file name bool retval = false; @@ -1659,9 +1656,9 @@ bool apply_autocmds_group(event_T event, char *fname, char *fname_io, bool force } // Save the autocmd_* variables and info about the current buffer. - char *save_autocmd_fname = (char *)autocmd_fname; + char *save_autocmd_fname = autocmd_fname; int save_autocmd_bufnr = autocmd_bufnr; - char *save_autocmd_match = (char *)autocmd_match; + char *save_autocmd_match = autocmd_match; int save_autocmd_busy = autocmd_busy; int save_autocmd_nested = autocmd_nested; bool save_changed = curbuf->b_changed; @@ -1675,18 +1672,18 @@ bool apply_autocmds_group(event_T event, char *fname, char *fname_io, bool force || event == EVENT_OPTIONSET || event == EVENT_MODECHANGED) { autocmd_fname = NULL; } else if (fname != NULL && !ends_excmd(*fname)) { - autocmd_fname = (char_u *)fname; + autocmd_fname = fname; } else if (buf != NULL) { - autocmd_fname = buf->b_ffname; + autocmd_fname = (char *)buf->b_ffname; } else { autocmd_fname = NULL; } } else { - autocmd_fname = (char_u *)fname_io; + autocmd_fname = fname_io; } if (autocmd_fname != NULL) { // Allocate MAXPATHL for when eval_vars() resolves the fullpath. - autocmd_fname = vim_strnsave(autocmd_fname, MAXPATHL); + autocmd_fname = xstrnsave(autocmd_fname, MAXPATHL); } // Set the buffer number to be used for <abuf>. @@ -1754,11 +1751,11 @@ bool apply_autocmds_group(event_T event, char *fname, char *fname_io, bool force #endif // Set the name to be used for <amatch>. - autocmd_match = (char_u *)fname; + autocmd_match = fname; // Don't redraw while doing autocommands. RedrawingDisabled++; - char *save_sourcing_name = (char *)sourcing_name; + char *save_sourcing_name = sourcing_name; sourcing_name = NULL; // don't free this one linenr_T save_sourcing_lnum = sourcing_lnum; sourcing_lnum = 0; // no line number here @@ -1793,7 +1790,7 @@ bool apply_autocmds_group(event_T event, char *fname, char *fname_io, bool force did_filetype = true; } - char *tail = (char *)path_tail((char_u *)fname); + char *tail = path_tail(fname); // Find first autocommand that matches AutoPatCmd patcmd; @@ -1814,6 +1811,9 @@ bool apply_autocmds_group(event_T event, char *fname, char *fname_io, bool force patcmd.next = active_apc_list; active_apc_list = &patcmd; + // Attach data to command + patcmd.data = data; + // set v:cmdarg (only when there is a matching pattern) save_cmdbang = (long)get_vim_var_nr(VV_CMDBANG); if (eap != NULL) { @@ -1860,12 +1860,12 @@ bool apply_autocmds_group(event_T event, char *fname, char *fname_io, bool force filechangeshell_busy = false; autocmd_nested = save_autocmd_nested; xfree(sourcing_name); - sourcing_name = (char_u *)save_sourcing_name; + sourcing_name = save_sourcing_name; sourcing_lnum = save_sourcing_lnum; xfree(autocmd_fname); - autocmd_fname = (char_u *)save_autocmd_fname; + autocmd_fname = save_autocmd_fname; autocmd_bufnr = save_autocmd_bufnr; - autocmd_match = (char_u *)save_autocmd_match; + autocmd_match = save_autocmd_match; current_sctx = save_current_sctx; restore_funccal(); if (do_profiling == PROF_YES) { @@ -1933,7 +1933,7 @@ void block_autocmds(void) { // Remember the value of v:termresponse. if (!is_autocmd_blocked()) { - old_termresponse = (char *)get_vim_var_str(VV_TERMRESPONSE); + old_termresponse = get_vim_var_str(VV_TERMRESPONSE); } autocmd_blocked++; } @@ -1946,7 +1946,7 @@ void unblock_autocmds(void) // the autocommands now. Esp. useful when executing a shell command // during startup (nvim -d). if (!is_autocmd_blocked() - && get_vim_var_str(VV_TERMRESPONSE) != (char_u *)old_termresponse) { + && get_vim_var_str(VV_TERMRESPONSE) != old_termresponse) { apply_autocmds(EVENT_TERMRESPONSE, NULL, NULL, false, curbuf); } } @@ -1979,9 +1979,9 @@ void auto_next_pat(AutoPatCmd *apc, int stop_at_last) if (ap->buflocal_nr == 0 ? match_file_pat(NULL, &ap->reg_prog, - (char_u *)apc->fname, - (char_u *)apc->sfname, - (char_u *)apc->tail, + apc->fname, + apc->sfname, + apc->tail, ap->allow_dirs) : ap->buflocal_nr == apc->arg_bufnr) { const char *const name = event_nr2name(apc->event); @@ -1991,7 +1991,7 @@ void auto_next_pat(AutoPatCmd *apc, int stop_at_last) = (STRLEN(s) + strlen(name) + (size_t)ap->patlen + 1); sourcing_name = xmalloc(sourcing_name_len); - snprintf((char *)sourcing_name, sourcing_name_len, s, name, ap->pat); + snprintf(sourcing_name, sourcing_name_len, s, name, ap->pat); if (p_verbose >= 8) { verbose_enter(); smsg(_("Executing %s"), sourcing_name); @@ -2029,6 +2029,10 @@ static bool call_autocmd_callback(const AutoCmd *ac, const AutoPatCmd *apc) PUT(data, "file", CSTR_TO_OBJ((char *)autocmd_fname)); PUT(data, "buf", INTEGER_OBJ(autocmd_bufnr)); + if (apc->data) { + PUT(data, "data", copy_object(*apc->data)); + } + int group = apc->curpat->group; switch (group) { case AUGROUP_ERROR: @@ -2064,7 +2068,7 @@ static bool call_autocmd_callback(const AutoCmd *ac, const AutoPatCmd *apc) /// Get next autocommand command. /// Called by do_cmdline() to get the next line for ":if". /// @return allocated string, or NULL for end of autocommands. -char_u *getnextac(int c, void *cookie, int indent, bool do_concat) +char *getnextac(int c, void *cookie, int indent, bool do_concat) { // These arguments are required for do_cmdline. (void)c; @@ -2155,7 +2159,7 @@ char_u *getnextac(int c, void *cookie, int indent, bool do_concat) acp->nextcmd = ac->next; } - return (char_u *)retval; + return retval; } /// Return true if there is a matching autocommand for "fname". @@ -2165,12 +2169,13 @@ char_u *getnextac(int c, void *cookie, int indent, bool do_concat) /// @param event event that occurred. /// @param sfname filename the event occurred in. /// @param buf buffer the file is open in -bool has_autocmd(event_T event, char_u *sfname, buf_T *buf) FUNC_ATTR_WARN_UNUSED_RESULT +bool has_autocmd(event_T event, char *sfname, buf_T *buf) + FUNC_ATTR_WARN_UNUSED_RESULT { - char *tail = (char *)path_tail(sfname); + char *tail = path_tail(sfname); bool retval = false; - char *fname = FullName_save((char *)sfname, false); + char *fname = FullName_save(sfname, false); if (fname == NULL) { return false; } @@ -2188,9 +2193,9 @@ bool has_autocmd(event_T event, char_u *sfname, buf_T *buf) FUNC_ATTR_WARN_UNUSE && (ap->buflocal_nr == 0 ? match_file_pat(NULL, &ap->reg_prog, - (char_u *)fname, + fname, sfname, - (char_u *)tail, + tail, ap->allow_dirs) : buf != NULL && ap->buflocal_nr == buf->b_fnum)) { retval = true; @@ -2208,12 +2213,12 @@ bool has_autocmd(event_T event, char_u *sfname, buf_T *buf) FUNC_ATTR_WARN_UNUSE // Function given to ExpandGeneric() to obtain the list of autocommand group // names. -char_u *expand_get_augroup_name(expand_T *xp, int idx) +char *expand_get_augroup_name(expand_T *xp, int idx) { // Required for ExpandGeneric (void)xp; - return (char_u *)augroup_name(idx + 1); + return augroup_name(idx + 1); } /// @param doautocmd true for :doauto*, false for :autocmd @@ -2263,7 +2268,7 @@ char *set_context_in_autocmd(expand_T *xp, char *arg, int doautocmd) } // Function given to ExpandGeneric() to obtain the list of event names. -char_u *expand_get_event_name(expand_T *xp, int idx) +char *expand_get_event_name(expand_T *xp, int idx) { // xp is a required parameter to be used with ExpandGeneric (void)xp; @@ -2273,14 +2278,14 @@ char_u *expand_get_event_name(expand_T *xp, int idx) if (name != NULL) { // skip when not including groups or skip deleted entries if (!autocmd_include_groups || name == get_deleted_augroup()) { - return (char_u *)""; + return ""; } - return (char_u *)name; + return name; } // List event names - return (char_u *)event_names[idx - next_augroup_id].name; + return event_names[idx - next_augroup_id].name; } /// Check whether given autocommand is supported @@ -2373,7 +2378,7 @@ bool au_exists(const char *const arg) FUNC_ATTR_WARN_UNUSED_RESULT && (group == AUGROUP_ALL || ap->group == group) && (pattern == NULL || (buflocal_buf == NULL - ? FNAMECMP(ap->pat, (char_u *)pattern) == 0 + ? FNAMECMP(ap->pat, pattern) == 0 : ap->buflocal_nr == buflocal_buf->b_fnum))) { retval = true; break; @@ -2410,7 +2415,7 @@ int aupat_get_buflocal_nr(char *pat, int patlen) } // "<buffer=123>" - if (skipdigits((char_u *)pat + 8) == (char_u *)pat + patlen - 1) { + if (skipdigits(pat + 8) == pat + patlen - 1) { return atoi(pat + 8); } } @@ -2434,10 +2439,10 @@ void aupat_normalize_buflocal_pat(char *dest, char *pat, int patlen, int bufloca buflocal_nr); } -int autocmd_delete_event(int group, event_T event, char_u *pat) +int autocmd_delete_event(int group, event_T event, char *pat) FUNC_ATTR_NONNULL_ALL { - return do_autocmd_event(event, (char *)pat, false, false, "", true, group); + return do_autocmd_event(event, pat, false, false, "", true, group); } /// Deletes an autocmd by ID. @@ -2706,9 +2711,9 @@ static void do_autocmd_focusgained(bool gained) // belongs. need_wait_return = false; - if (State & CMDLINE) { + if (State & MODE_CMDLINE) { redrawcmdline(); - } else if ((State & NORMAL) || (State & INSERT)) { + } else if ((State & MODE_NORMAL) || (State & MODE_INSERT)) { if (must_redraw != 0) { update_screen(0); } |