diff options
Diffstat (limited to 'src/nvim/fileio.c')
-rw-r--r-- | src/nvim/fileio.c | 110 |
1 files changed, 65 insertions, 45 deletions
diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index c0d4a71b35..529b48adbd 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -187,6 +187,14 @@ struct bw_info { static char *e_auchangedbuf = N_( "E812: Autocommands changed buffer or buffer name"); +// Set by the apply_autocmds_group function if the given event is equal to +// EVENT_FILETYPE. Used by the readfile function in order to determine if +// EVENT_BUFREADPOST triggered the EVENT_FILETYPE. +// +// Relying on this value requires one to reset it prior calling +// apply_autocmds_group. +static bool au_did_filetype INIT(= false); + void filemess(buf_T *buf, char_u *name, char_u *s, int attr) { int msg_scroll_save; @@ -329,6 +337,8 @@ readfile ( int using_b_ffname; int using_b_fname; + au_did_filetype = false; // reset before triggering any autocommands + curbuf->b_no_eol_lnum = 0; /* in case it was set by the previous read */ /* @@ -1957,20 +1967,29 @@ failed: * should not be overwritten: Set msg_scroll, restore its value if no * output was done. */ - msg_scroll = TRUE; - if (filtering) + msg_scroll = true; + if (filtering) { apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname, - FALSE, curbuf, eap); - else if (newfile) + false, curbuf, eap); + } else if (newfile) { apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname, - FALSE, curbuf, eap); - else + false, curbuf, eap); + if (!au_did_filetype && *curbuf->b_p_ft != NUL) { + // EVENT_FILETYPE was not triggered but the buffer already has a + // filetype. Trigger EVENT_FILETYPE using the existing filetype. + apply_autocmds(EVENT_FILETYPE, curbuf->b_p_ft, curbuf->b_fname, + true, curbuf); + } + } else { apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname, - FALSE, NULL, eap); - if (msg_scrolled == n) + false, NULL, eap); + } + if (msg_scrolled == n) { msg_scroll = m; - if (aborting()) /* autocmds may abort script processing */ + } + if (aborting()) { // autocmds may abort script processing return FAIL; + } } if (recoverymode && error) @@ -4165,9 +4184,8 @@ static bool need_conversion(const char_u *fenc) same_encoding = (enc_flags != 0 && fenc_flags == enc_flags); } if (same_encoding) { - /* Specified encoding matches with 'encoding'. This requires - * conversion when 'encoding' is Unicode but not UTF-8. */ - return enc_unicode != 0; + // Specified file encoding matches UTF-8. + return false; } /* Encodings differ. However, conversion is not needed when 'enc' is any @@ -5063,10 +5081,10 @@ void buf_reload(buf_T *buf, int orig_mode) * the old contents. Can't use memory only, the file might be * too big. Use a hidden buffer to move the buffer contents to. */ - if (bufempty() || saved == FAIL) + if (bufempty() || saved == FAIL) { savebuf = NULL; - else { - /* Allocate a buffer without putting it in the buffer list. */ + } else { + // Allocate a buffer without putting it in the buffer list. savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); if (savebuf != NULL && buf == curbuf) { /* Open the memline. */ @@ -6343,27 +6361,24 @@ aucmd_prepbuf ( aco->new_curbuf = curbuf; } -/* - * Cleanup after executing autocommands for a (hidden) buffer. - * Restore the window as it was (if possible). - */ -void -aucmd_restbuf ( - aco_save_T *aco /* structure holding saved values */ -) +/// Cleanup after executing autocommands for a (hidden) buffer. +/// Restore the window as it was (if possible). +/// +/// @param aco structure holding saved values +void aucmd_restbuf(aco_save_T *aco) { int dummy; if (aco->use_aucmd_win) { - --curbuf->b_nwindows; - /* Find "aucmd_win", it can't be closed, but it may be in another tab - * page. Do not trigger autocommands here. */ + curbuf->b_nwindows--; + // Find "aucmd_win", it can't be closed, but it may be in another tab page. + // Do not trigger autocommands here. block_autocmds(); if (curwin != aucmd_win) { FOR_ALL_TAB_WINDOWS(tp, wp) { if (wp == aucmd_win) { if (tp != curtab) { - goto_tabpage_tp(tp, TRUE, TRUE); + goto_tabpage_tp(tp, true, true); } win_goto(aucmd_win); goto win_found; @@ -6372,49 +6387,50 @@ aucmd_restbuf ( } win_found: - /* Remove the window and frame from the tree of frames. */ + // Remove the window and frame from the tree of frames. (void)winframe_remove(curwin, &dummy, NULL); win_remove(curwin, NULL); - aucmd_win_used = FALSE; - last_status(FALSE); /* may need to remove last status line */ - restore_snapshot(SNAP_AUCMD_IDX, FALSE); - (void)win_comp_pos(); /* recompute window positions */ + aucmd_win_used = false; + last_status(false); // may need to remove last status line + restore_snapshot(SNAP_AUCMD_IDX, false); + (void)win_comp_pos(); // recompute window positions unblock_autocmds(); - if (win_valid(aco->save_curwin)) + if (win_valid(aco->save_curwin)) { curwin = aco->save_curwin; - else - /* Hmm, original window disappeared. Just use the first one. */ + } else { + // Hmm, original window disappeared. Just use the first one. curwin = firstwin; - vars_clear(&aucmd_win->w_vars->dv_hashtab); /* free all w: variables */ - hash_init(&aucmd_win->w_vars->dv_hashtab); /* re-use the hashtab */ + } + vars_clear(&aucmd_win->w_vars->dv_hashtab); // free all w: variables + hash_init(&aucmd_win->w_vars->dv_hashtab); // re-use the hashtab curbuf = curwin->w_buffer; xfree(globaldir); globaldir = aco->globaldir; - /* the buffer contents may have changed */ + // the buffer contents may have changed check_cursor(); if (curwin->w_topline > curbuf->b_ml.ml_line_count) { curwin->w_topline = curbuf->b_ml.ml_line_count; curwin->w_topfill = 0; } } else { - /* restore curwin */ + // restore curwin if (win_valid(aco->save_curwin)) { - /* Restore the buffer which was previously edited by curwin, if - * it was changed, we are still the same window and the buffer is - * valid. */ + // Restore the buffer which was previously edited by curwin, if it was + // changed, we are still the same window and the buffer is valid. if (curwin == aco->new_curwin && curbuf != aco->new_curbuf && buf_valid(aco->new_curbuf) && aco->new_curbuf->b_ml.ml_mfp != NULL) { - if (curwin->w_s == &curbuf->b_s) + if (curwin->w_s == &curbuf->b_s) { curwin->w_s = &aco->new_curbuf->b_s; - --curbuf->b_nwindows; + } + curbuf->b_nwindows--; curbuf = aco->new_curbuf; curwin->w_buffer = curbuf; - ++curbuf->b_nwindows; + curbuf->b_nwindows++; } curwin = aco->save_curwin; @@ -6868,6 +6884,10 @@ BYPASS_AU: if (event == EVENT_BUFWIPEOUT && buf != NULL) aubuflocal_remove(buf); + if (retval == OK && event == EVENT_FILETYPE) { + au_did_filetype = true; + } + return retval; } |