diff src/fileio.c @ 13380:69517d67421f v8.0.1564

patch 8.0.1564: too many #ifdefs commit https://github.com/vim/vim/commit/f2bd8ef2b4507d02c6043affff8f7e85e3414d5f Author: Bram Moolenaar <Bram@vim.org> Date: Sun Mar 4 18:08:14 2018 +0100 patch 8.0.1564: too many #ifdefs Problem: Too many #ifdefs. Solution: Graduate the +autocmd feature. Takes away 450 #ifdefs and increases code size of tiny Vim by only 40 Kbyte.
author Christian Brabandt <cb@256bit.org>
date Sun, 04 Mar 2018 18:15:08 +0100
parents ac42c4b11dbc
children 94e638936d3e
line wrap: on
line diff
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -47,14 +47,12 @@ static int msg_add_fileformat(int eol_ty
 static void msg_add_eol(void);
 static int check_mtime(buf_T *buf, stat_T *s);
 static int time_differs(long t1, long t2);
-#ifdef FEAT_AUTOCMD
 static int apply_autocmds_exarg(event_T event, char_u *fname, char_u *fname_io, int force, buf_T *buf, exarg_T *eap);
 static int au_find_group(char_u *name);
 
 # define AUGROUP_DEFAULT    -1	    /* default autocmd group */
 # define AUGROUP_ERROR	    -2	    /* erroneous autocmd group */
 # define AUGROUP_ALL	    -3	    /* all autocmd groups */
-#endif
 
 #if defined(FEAT_CRYPT) || defined(FEAT_MBYTE)
 # define HAS_BW_FLAGS
@@ -135,11 +133,8 @@ static int move_lines(buf_T *frombuf, bu
 #ifdef TEMPDIRNAMES
 static void vim_settempdir(char_u *tempdir);
 #endif
-#ifdef FEAT_AUTOCMD
 static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name");
-#endif
-
-#ifdef FEAT_AUTOCMD
+
 /*
  * 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
@@ -149,7 +144,6 @@ static char *e_auchangedbuf = N_("E812: 
  * apply_autocmds_group.
  */
 static int au_did_filetype INIT(= FALSE);
-#endif
 
     void
 filemess(
@@ -307,17 +301,13 @@ readfile(
     char_u	conv_rest[CONV_RESTLEN];
     int		conv_restlen = 0;	/* nr of bytes in conv_rest[] */
 #endif
-#ifdef FEAT_AUTOCMD
     buf_T	*old_curbuf;
     char_u	*old_b_ffname;
     char_u	*old_b_fname;
     int		using_b_ffname;
     int		using_b_fname;
-#endif
-
-#ifdef FEAT_AUTOCMD
+
     au_did_filetype = FALSE; /* reset before triggering any autocommands */
-#endif
 
     curbuf->b_no_eol_lnum = 0;	/* in case it was set by the previous read */
 
@@ -337,7 +327,6 @@ readfile(
 	    return FAIL;
     }
 
-#ifdef FEAT_AUTOCMD
     /* Remember the initial values of curbuf, curbuf->b_ffname and
      * curbuf->b_fname to detect whether they are altered as a result of
      * executing nasty autocommands.  Also check if "fname" and "sfname"
@@ -348,7 +337,6 @@ readfile(
     using_b_ffname = (fname == curbuf->b_ffname)
 					      || (sfname == curbuf->b_ffname);
     using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname);
-#endif
 
     /* After reading a file the cursor line changes but we don't want to
      * display the line. */
@@ -369,7 +357,6 @@ readfile(
     fname = sfname;
 #endif
 
-#ifdef FEAT_AUTOCMD
     /*
      * The BufReadCmd and FileReadCmd events intercept the reading process by
      * executing the associated commands instead.
@@ -404,7 +391,6 @@ readfile(
 
 	curbuf->b_op_start = pos;
     }
-#endif
 
     if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
 	msg_scroll = FALSE;	/* overwrite previous file message */
@@ -613,7 +599,6 @@ readfile(
 #endif
 		    {
 			check_need_swap(newfile);
-#ifdef FEAT_AUTOCMD
 			/* SwapExists autocommand may mess things up */
 			if (curbuf != old_curbuf
 				|| (using_b_ffname
@@ -624,7 +609,6 @@ readfile(
 			    EMSG(_(e_auchangedbuf));
 			    return FAIL;
 			}
-#endif
 		    }
 		    if (dir_of_file_exists(fname))
 			filemess(curbuf, sfname, (char_u *)_("[New File]"), 0);
@@ -641,14 +625,12 @@ readfile(
 		    if (eap != NULL)
 			set_forced_fenc(eap);
 #endif
-#ifdef FEAT_AUTOCMD
 		    apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
 							  FALSE, curbuf, eap);
-#endif
 		    /* remember the current fileformat */
 		    save_file_ff(curbuf);
 
-#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
+#if defined(FEAT_EVAL)
 		    if (aborting())   /* autocmds may abort script processing */
 			return FAIL;
 #endif
@@ -701,7 +683,6 @@ readfile(
 #endif
     {
 	check_need_swap(newfile);
-#ifdef FEAT_AUTOCMD
 	if (!read_stdin && (curbuf != old_curbuf
 		|| (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
 		|| (using_b_fname && (old_b_fname != curbuf->b_fname))))
@@ -711,7 +692,6 @@ readfile(
 		close(fd);
 	    return FAIL;
 	}
-#endif
 #ifdef UNIX
 	/* Set swap file protection bits after creating it. */
 	if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
@@ -764,7 +744,6 @@ readfile(
     try_dos = (vim_strchr(p_ffs, 'd') != NULL);
     try_unix = (vim_strchr(p_ffs, 'x') != NULL);
 
-#ifdef FEAT_AUTOCMD
     if (!read_buffer)
     {
 	int	m = msg_scroll;
@@ -834,7 +813,6 @@ readfile(
 	    return FAIL;
 	}
     }
-#endif /* FEAT_AUTOCMD */
 
     /* Autocommands may add lines to the file, need to check if it is empty */
     wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
@@ -2704,7 +2682,6 @@ failed:
     }
 #endif
 
-#ifdef FEAT_AUTOCMD
     if (!read_stdin && !read_fifo && (!read_buffer || sfname != NULL))
     {
 	int m = msg_scroll;
@@ -2746,7 +2723,6 @@ failed:
 	    return FAIL;
 # endif
     }
-#endif
 
     if (recoverymode && error)
 	return FAIL;
@@ -3197,9 +3173,7 @@ buf_write(
 #endif
 					/* writing everything */
     int		    whole = (start == 1 && end == buf->b_ml.ml_line_count);
-#ifdef FEAT_AUTOCMD
     linenr_T	    old_line_count = buf->b_ml.ml_line_count;
-#endif
     int		    attr;
     int		    fileformat;
     int		    write_bin;
@@ -3319,7 +3293,6 @@ buf_write(
     buf->b_op_end.lnum = end;
     buf->b_op_end.col = 0;
 
-#ifdef FEAT_AUTOCMD
     {
 	aco_save_T	aco;
 	int		buf_ffname = FALSE;
@@ -3509,7 +3482,6 @@ buf_write(
 	if (buf_fname_s)
 	    fname = buf->b_sfname;
     }
-#endif
 
 #ifdef FEAT_NETBEANS_INTG
     if (netbeans_active() && isNetbeansBuffer(buf))
@@ -5020,12 +4992,10 @@ restore_backup:
 	    )
     {
 	unchanged(buf, TRUE);
-#ifdef FEAT_AUTOCMD
 	/* b:changedtick is always incremented in unchanged() but that
 	 * should not trigger a TextChanged event. */
 	if (buf->b_last_changedtick + 1 == CHANGEDTICK(buf))
 	    buf->b_last_changedtick = CHANGEDTICK(buf);
-#endif
 	u_unchanged(buf);
 	u_update_save_nr(buf);
     }
@@ -5194,7 +5164,6 @@ nofail:
     }
 #endif
 
-#ifdef FEAT_AUTOCMD
 #ifdef FEAT_EVAL
     if (!should_abort(retval))
 #else
@@ -5232,7 +5201,6 @@ nofail:
 	    retval = FALSE;
 #endif
     }
-#endif
 
     got_int |= prev_got_int;
 
@@ -5246,37 +5214,34 @@ nofail:
     static int
 set_rw_fname(char_u *fname, char_u *sfname)
 {
-#ifdef FEAT_AUTOCMD
     buf_T	*buf = curbuf;
 
     /* It's like the unnamed buffer is deleted.... */
     if (curbuf->b_p_bl)
 	apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
     apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
-# ifdef FEAT_EVAL
+#ifdef FEAT_EVAL
     if (aborting())	    /* autocmds may abort script processing */
 	return FAIL;
-# endif
+#endif
     if (curbuf != buf)
     {
 	/* We are in another buffer now, don't do the renaming. */
 	EMSG(_(e_auchangedbuf));
 	return FAIL;
     }
-#endif
 
     if (setfname(curbuf, fname, sfname, FALSE) == OK)
 	curbuf->b_flags |= BF_NOTEDITED;
 
-#ifdef FEAT_AUTOCMD
     /* ....and a new named one is created */
     apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
     if (curbuf->b_p_bl)
 	apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
-# ifdef FEAT_EVAL
+#ifdef FEAT_EVAL
     if (aborting())	    /* autocmds may abort script processing */
 	return FAIL;
-# endif
+#endif
 
     /* Do filetype detection now if 'filetype' is empty. */
     if (*curbuf->b_p_ft == NUL)
@@ -5285,7 +5250,6 @@ set_rw_fname(char_u *fname, char_u *sfna
 	    (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE, NULL);
 	do_modelines(0);
     }
-#endif
 
     return OK;
 }
@@ -6105,8 +6069,6 @@ make_bom(char_u *buf, char_u *name)
 }
 #endif
 
-#if defined(FEAT_VIMINFO) || defined(FEAT_BROWSE) || \
-    defined(FEAT_QUICKFIX) || defined(FEAT_AUTOCMD) || defined(PROTO)
 /*
  * Try to find a shortname by comparing the fullname with the current
  * directory.
@@ -6130,7 +6092,6 @@ shorten_fname1(char_u *full_path)
     vim_free(dirname);
     return p;
 }
-#endif
 
 /*
  * Try to find a shortname by comparing the fullname with the current
@@ -6781,10 +6742,7 @@ check_timestamps(
     }
 
     if (!stuff_empty() || global_busy || !typebuf_typed()
-#ifdef FEAT_AUTOCMD
-			|| autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0
-#endif
-					)
+			|| autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0)
 	need_check_timestamps = TRUE;		/* check later */
     else
     {
@@ -6898,14 +6856,14 @@ buf_check_timestamp(
 #ifdef FEAT_GUI
     int		save_mouse_correct = need_mouse_correct;
 #endif
-#ifdef FEAT_AUTOCMD
     static int	busy = FALSE;
     int		n;
+#ifdef FEAT_EVAL
     char_u	*s;
+#endif
     bufref_T	bufref;
 
     set_bufref(&bufref, buf);
-#endif
 
     /* If there is no file name, the buffer is not loaded, 'buftype' is
      * set, we are in the middle of a save or being called recursively: ignore
@@ -6914,9 +6872,7 @@ buf_check_timestamp(
 	    || buf->b_ml.ml_mfp == NULL
 	    || *buf->b_p_bt != NUL
 	    || buf->b_saving
-#ifdef FEAT_AUTOCMD
 	    || busy
-#endif
 #ifdef FEAT_NETBEANS_INTG
 	    || isNetbeansBuffer(buf)
 #endif
@@ -6977,17 +6933,16 @@ buf_check_timestamp(
 	    else
 		reason = "time";
 
-#ifdef FEAT_AUTOCMD
 	    /*
 	     * Only give the warning if there are no FileChangedShell
 	     * autocommands.
 	     * Avoid being called recursively by setting "busy".
 	     */
 	    busy = TRUE;
-# ifdef FEAT_EVAL
+#ifdef FEAT_EVAL
 	    set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
 	    set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
-# endif
+#endif
 	    ++allbuf_lock;
 	    n = apply_autocmds(EVENT_FILECHANGEDSHELL,
 				      buf->b_fname, buf->b_fname, FALSE, buf);
@@ -6997,18 +6952,17 @@ buf_check_timestamp(
 	    {
 		if (!bufref_valid(&bufref))
 		    EMSG(_("E246: FileChangedShell autocommand deleted buffer"));
-# ifdef FEAT_EVAL
+#ifdef FEAT_EVAL
 		s = get_vim_var_str(VV_FCS_CHOICE);
 		if (STRCMP(s, "reload") == 0 && *reason != 'd')
 		    reload = TRUE;
 		else if (STRCMP(s, "ask") == 0)
 		    n = FALSE;
 		else
-# endif
+#endif
 		    return 2;
 	    }
 	    if (!n)
-#endif
 	    {
 		if (*reason == 'd')
 		    mesg = _("E211: File \"%s\" no longer available");
@@ -7100,9 +7054,7 @@ buf_check_timestamp(
 	    }
 	    else
 	    {
-# ifdef FEAT_AUTOCMD
 		if (!autocmd_busy)
-# endif
 		{
 		    msg_start();
 		    msg_puts_attr(tbuf, HL_ATTR(HLF_E) + MSG_HIST);
@@ -7114,9 +7066,9 @@ buf_check_timestamp(
 		    if (emsg_silent == 0)
 		    {
 			out_flush();
-# ifdef FEAT_GUI
+#ifdef FEAT_GUI
 			if (!focus)
-# endif
+#endif
 			    /* give the user some time to think about it */
 			    ui_delay(1000L, TRUE);
 
@@ -7151,12 +7103,10 @@ buf_check_timestamp(
 #endif
     }
 
-#ifdef FEAT_AUTOCMD
     /* Trigger FileChangedShell when the file was changed in any way. */
     if (bufref_valid(&bufref) && retval != 0)
 	(void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
 				      buf->b_fname, buf->b_fname, FALSE, buf);
-#endif
 #ifdef FEAT_GUI
     /* restore this in case an autocommand has set it; it would break
      * 'mousefocus' */
@@ -7240,14 +7190,12 @@ buf_reload(buf_T *buf, int orig_mode)
 	if (saved == OK)
 	{
 	    curbuf->b_flags |= BF_CHECK_RO;	/* check for RO again */
-#ifdef FEAT_AUTOCMD
 	    keep_filetype = TRUE;		/* don't detect 'filetype' */
-#endif
 	    if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
 			(linenr_T)0,
 			(linenr_T)MAXLNUM, &ea, flags) != OK)
 	    {
-#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
+#if defined(FEAT_EVAL)
 		if (!aborting())
 #endif
 		    EMSG2(_("E321: Could not reload \"%s\""), buf->b_fname);
@@ -7296,9 +7244,7 @@ buf_reload(buf_T *buf, int orig_mode)
 	curwin->w_cursor = old_cursor;
 	check_cursor();
 	update_topline();
-#ifdef FEAT_AUTOCMD
 	keep_filetype = FALSE;
-#endif
 #ifdef FEAT_FOLDING
 	{
 	    win_T	*wp;
@@ -7672,11 +7618,7 @@ forward_slash(char_u *fname)
 
 /*
  * Code for automatic commands.
- *
- * Only included when "FEAT_AUTOCMD" has been defined.
- */
-
-#if defined(FEAT_AUTOCMD) || defined(PROTO)
+ */
 
 /*
  * The autocommands are stored in a list for each event.
@@ -7914,9 +7856,7 @@ static int au_get_grouparg(char_u **argp
 static int do_autocmd_event(event_T event, char_u *pat, int nested, char_u *cmd, int forceit, int group);
 static int apply_autocmds_group(event_T event, char_u *fname, char_u *fname_io, int force, int group, buf_T *buf, exarg_T *eap);
 static void auto_next_pat(AutoPatCmd *apc, int stop_at_last);
-#if defined(FEAT_AUTOCMD) || defined(FEAT_WILDIGN)
 static int match_file_pat(char_u *pattern, regprog_T **prog, char_u *fname, char_u *sfname, char_u *tail, int allow_dirs);
-#endif
 
 
 static event_T	last_event;
@@ -9014,7 +8954,6 @@ check_nomodeline(char_u **argp)
  * Search for a visible window containing the current buffer.  If there isn't
  * one then use "aucmd_win".
  * Set "curbuf" and "curwin" to match "buf".
- * When FEAT_AUTOCMD is not defined another version is used, see below.
  */
     void
 aucmd_prepbuf(
@@ -9067,7 +9006,9 @@ aucmd_prepbuf(
 	aco->use_aucmd_win = TRUE;
 	aucmd_win_used = TRUE;
 	aucmd_win->w_buffer = buf;
+#if defined(FEAT_SYN_HL) || defined(FEAT_SPELL)
 	aucmd_win->w_s = &buf->b_s;
+#endif
 	++buf->b_nwindows;
 	win_init_empty(aucmd_win); /* set cursor and topline to safe values */
 
@@ -9108,7 +9049,6 @@ aucmd_prepbuf(
 /*
  * Cleanup after executing autocommands for a (hidden) buffer.
  * Restore the window as it was (if possible).
- * When FEAT_AUTOCMD is not defined another version is used, see below.
  */
     void
 aucmd_restbuf(
@@ -9406,13 +9346,13 @@ has_textyankpost(void)
     static int
 apply_autocmds_group(
     event_T	event,
-    char_u	*fname,	    /* NULL or empty means use actual file name */
-    char_u	*fname_io,  /* fname to use for <afile> on cmdline, NULL means
+    char_u	*fname,	     /* NULL or empty means use actual file name */
+    char_u	*fname_io,   /* fname to use for <afile> on cmdline, NULL means
 			       use fname */
-    int		force,	    /* when TRUE, ignore autocmd_busy */
-    int		group,	    /* group ID, or AUGROUP_ALL */
-    buf_T	*buf,	    /* buffer for <abuf> */
-    exarg_T	*eap)	    /* command arguments */
+    int		force,	     /* when TRUE, ignore autocmd_busy */
+    int		group,	     /* group ID, or AUGROUP_ALL */
+    buf_T	*buf,	     /* buffer for <abuf> */
+    exarg_T	*eap UNUSED) /* command arguments */
 {
     char_u	*sfname = NULL;	/* short file name */
     char_u	*tail;
@@ -10210,43 +10150,7 @@ theend:
     return retval;
 }
 
-#else	/* FEAT_AUTOCMD */
-
-/*
- * Prepare for executing commands for (hidden) buffer "buf".
- * This is the non-autocommand version, it simply saves "curbuf" and sets
- * "curbuf" and "curwin" to match "buf".
- */
-    void
-aucmd_prepbuf(
-    aco_save_T	*aco,		/* structure to save values in */
-    buf_T	*buf)		/* new curbuf */
-{
-    aco->save_curbuf = curbuf;
-    --curbuf->b_nwindows;
-    curbuf = buf;
-    curwin->w_buffer = buf;
-    ++curbuf->b_nwindows;
-}
-
-/*
- * Restore after executing commands for a (hidden) buffer.
- * This is the non-autocommand version.
- */
-    void
-aucmd_restbuf(
-    aco_save_T	*aco)		/* structure holding saved values */
-{
-    --curbuf->b_nwindows;
-    curbuf = aco->save_curbuf;
-    curwin->w_buffer = curbuf;
-    ++curbuf->b_nwindows;
-}
-
-#endif	/* FEAT_AUTOCMD */
-
-
-#if defined(FEAT_AUTOCMD) || defined(FEAT_WILDIGN) || defined(PROTO)
+
 /*
  * Try matching a filename with a "pattern" ("prog" is NULL), or use the
  * precompiled regprog "prog" ("pattern" is NULL).  That avoids calling
@@ -10292,7 +10196,6 @@ match_file_pat(
 	vim_regfree(regmatch.regprog);
     return result;
 }
-#endif
 
 #if defined(FEAT_WILDIGN) || defined(PROTO)
 /*