home *** CD-ROM | disk | FTP | other *** search
/ vim.ftp.fu-berlin.de / 2015-02-03.vim.ftp.fu-berlin.de.tar / vim.ftp.fu-berlin.de / patches / 6.1.220 < prev    next >
Encoding:
Internet Message Format  |  2002-10-12  |  10.2 KB

  1. To: vim-dev@vim.org
  2. Subject: Patch 6.1.220
  3. Fcc: outbox
  4. From: Bram Moolenaar <Bram@moolenaar.net>
  5. Mime-Version: 1.0
  6. Content-Type: text/plain; charset=ISO-8859-1
  7. Content-Transfer-Encoding: 8bit
  8. ------------
  9.  
  10. Patch 6.1.220
  11. Problem:    When using a BufReadPost autocommand that changes the line count,
  12.         e.g., "$-1join", reloading a file that was changed outside Vim
  13.         does not work properly. (Alan G Isaac)
  14. Solution:   Make the buffer empty before reading the new version of the file.
  15.         Save the lines in a dummy buffer, so that they can be put back
  16.         when reading the file fails.
  17. Files:        src/buffer.c, src/ex_cmds.c, src/fileio.c, src/globals.h,
  18.         src/proto/buffer.pro
  19.  
  20.  
  21. *** ../vim61.219/src/buffer.c    Mon Oct  7 20:45:41 2002
  22. --- src/buffer.c    Sun Oct 13 18:22:22 2002
  23. ***************
  24. *** 4545,4551 ****
  25.       /* Force the 'fileencoding' and 'fileformat' to be equal. */
  26.       if (prep_exarg(&ea, buf) == FAIL)
  27.       {
  28. !     close_buffer(NULL, newbuf, DOBUF_WIPE);
  29.       return TRUE;
  30.       }
  31.   
  32. --- 4545,4551 ----
  33.       /* Force the 'fileencoding' and 'fileformat' to be equal. */
  34.       if (prep_exarg(&ea, buf) == FAIL)
  35.       {
  36. !     wipe_buffer(newbuf, FALSE);
  37.       return TRUE;
  38.       }
  39.   
  40. ***************
  41. *** 4585,4591 ****
  42.   #endif
  43.   
  44.       if (curbuf != newbuf)    /* safety check */
  45. !     wipe_buffer(newbuf);
  46.   
  47.       return differ;
  48.   }
  49. --- 4585,4591 ----
  50.   #endif
  51.   
  52.       if (curbuf != newbuf)    /* safety check */
  53. !     wipe_buffer(newbuf, FALSE);
  54.   
  55.       return differ;
  56.   }
  57. ***************
  58. *** 4595,4605 ****
  59.    * this buffer.  Call this to wipe out a temp buffer that does not contain any
  60.    * marks.
  61.    */
  62.       void
  63. ! wipe_buffer(buf)
  64.       buf_T    *buf;
  65.   {
  66.       if (buf->b_fnum == top_file_num - 1)
  67.       --top_file_num;
  68.       close_buffer(NULL, buf, DOBUF_WIPE);
  69.   }
  70. --- 4595,4616 ----
  71.    * this buffer.  Call this to wipe out a temp buffer that does not contain any
  72.    * marks.
  73.    */
  74. + /*ARGSUSED*/
  75.       void
  76. ! wipe_buffer(buf, aucmd)
  77.       buf_T    *buf;
  78. +     int        aucmd;        /* When TRUE trigger autocommands. */
  79.   {
  80.       if (buf->b_fnum == top_file_num - 1)
  81.       --top_file_num;
  82. + #ifdef FEAT_AUTOCMD
  83. +     if (!aucmd)            /* Don't trigger BufDelete autocommands here. */
  84. +     ++autocmd_block;
  85. + #endif
  86.       close_buffer(NULL, buf, DOBUF_WIPE);
  87. + #ifdef FEAT_AUTOCMD
  88. +     if (!aucmd)
  89. +     --autocmd_block;
  90. + #endif
  91.   }
  92. *** ../vim61.219/src/ex_cmds.c    Wed Sep 25 22:21:44 2002
  93. --- src/ex_cmds.c    Sun Oct 13 18:22:31 2002
  94. ***************
  95. *** 4518,4524 ****
  96.       {
  97.       buf = buflist_findnr(empty_fnum);
  98.       if (buf != NULL)
  99. !         wipe_buffer(buf);
  100.       }
  101.   
  102.       /* keep the previous alternate file */
  103. --- 4518,4524 ----
  104.       {
  105.       buf = buflist_findnr(empty_fnum);
  106.       if (buf != NULL)
  107. !         wipe_buffer(buf, TRUE);
  108.       }
  109.   
  110.       /* keep the previous alternate file */
  111. *** ../vim61.219/src/fileio.c    Sat Oct 12 15:48:03 2002
  112. --- src/fileio.c    Sun Oct 13 18:42:11 2002
  113. ***************
  114. *** 5043,5048 ****
  115. --- 5218,5268 ----
  116.   }
  117.   
  118.   /*
  119. +  * Move all the lines from buffer "frombuf" to buffer "tobuf".
  120. +  * Return OK or FAIL.  When FAIL "tobuf" is incomplete and/or "frombuf" is not
  121. +  * empty.
  122. +  */
  123. +     static int
  124. + move_lines(frombuf, tobuf)
  125. +     buf_T    *frombuf;
  126. +     buf_T    *tobuf;
  127. + {
  128. +     buf_T    *tbuf = curbuf;
  129. +     int        retval = OK;
  130. +     linenr_T    lnum;
  131. +     char_u    *p;
  132. +     /* Copy the lines in "frombuf" to "tobuf". */
  133. +     curbuf = tobuf;
  134. +     for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
  135. +     {
  136. +     p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
  137. +     if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
  138. +     {
  139. +         retval = FAIL;
  140. +         break;
  141. +     }
  142. +     }
  143. +     /* Delete all the lines in "frombuf". */
  144. +     if (retval != FAIL)
  145. +     {
  146. +     curbuf = frombuf;
  147. +     while (!bufempty())
  148. +         if (ml_delete(curbuf->b_ml.ml_line_count, FALSE) == FAIL)
  149. +         {
  150. +         /* Oops!  We could try putting back the saved lines, but that
  151. +          * might fail again... */
  152. +         retval = FAIL;
  153. +         break;
  154. +         }
  155. +     }
  156. +     curbuf = tbuf;
  157. +     return retval;
  158. + }
  159. + /*
  160.    * Check if buffer "buf" has been changed.
  161.    * Also check if the file for a new buffer unexpectedly appeared.
  162.    * return 1 if a changed buffer was found.
  163. ***************
  164. *** 5230,5240 ****
  165.   
  166.       if (reload)
  167.       {
  168. -     linenr_T    old_line_count = buf->b_ml.ml_line_count;
  169.       exarg_T        ea;
  170.       pos_T        old_cursor;
  171.       linenr_T    old_topline;
  172.       int        old_ro = curbuf->b_p_ro;
  173.   #ifdef FEAT_AUTOCMD
  174.       aco_save_T    aco;
  175.   
  176. --- 5450,5461 ----
  177.   
  178.       if (reload)
  179.       {
  180.       exarg_T        ea;
  181.       pos_T        old_cursor;
  182.       linenr_T    old_topline;
  183.       int        old_ro = curbuf->b_p_ro;
  184. +     buf_T        *savebuf;
  185. +     int        saved = OK;
  186.   #ifdef FEAT_AUTOCMD
  187.       aco_save_T    aco;
  188.   
  189. ***************
  190. *** 5254,5280 ****
  191.       {
  192.           old_cursor = curwin->w_cursor;
  193.           old_topline = curwin->w_topline;
  194.           if (bufempty())
  195. !         old_line_count = 0;
  196. !         curbuf->b_flags |= BF_CHECK_RO;    /* check for RO again */
  197. ! #ifdef FEAT_AUTOCMD
  198. !         keep_filetype = TRUE;        /* don't detect 'filetype' */
  199. ! #endif
  200. !         if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0, (linenr_T)0,
  201. !             (linenr_T)MAXLNUM, &ea, READ_NEW) == FAIL)
  202. !         EMSG2(_("E321: Could not reload \"%s\""), buf->b_fname);
  203.           else
  204.           {
  205. !         /* Delete the old lines. */
  206. !         while (old_line_count-- > 0)
  207. !             ml_delete(buf->b_ml.ml_line_count, FALSE);
  208. !         /* Mark the buffer as unmodified and free undo info. */
  209. !         unchanged(buf, TRUE);
  210. !         u_blockfree(buf);
  211. !         u_clearall(buf);
  212.           }
  213.           vim_free(ea.cmd);
  214.   
  215.   #ifdef FEAT_DIFF
  216.           /* Invalidate diff info if necessary. */
  217.           diff_invalidate();
  218. --- 5475,5547 ----
  219.       {
  220.           old_cursor = curwin->w_cursor;
  221.           old_topline = curwin->w_topline;
  222. +         /*
  223. +          * To behave like when a new file is edited (matters for
  224. +          * BufReadPost autocommands) we first need to delete the current
  225. +          * buffer contents.  But if reading the file fails we should keep
  226. +          * the old contents.  Can't use memory only, the file might be
  227. +          * too big.  Use a hidden buffer to move the buffer contents to.
  228. +          */
  229.           if (bufempty())
  230. !         savebuf = NULL;
  231.           else
  232.           {
  233. !         /* Allocate a buffer without putting it in the buffer list. */
  234. !         savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
  235. !         if (savebuf != NULL)
  236. !         {
  237. !             /* Open the memline. */
  238. !             curbuf = savebuf;
  239. !             curwin->w_buffer = savebuf;
  240. !             saved = ml_open();
  241. !             curbuf = buf;
  242. !             curwin->w_buffer = buf;
  243. !         }
  244. !         if (savebuf == NULL || saved == FAIL
  245. !                       || move_lines(buf, savebuf) == FAIL)
  246. !         {
  247. !             EMSG2(_("E461: Could not prepare for reloading \"%s\""),
  248. !                                 buf->b_fname);
  249. !             saved = FAIL;
  250. !         }
  251. !         }
  252. !         if (saved == OK)
  253. !         {
  254. !         curbuf->b_flags |= BF_CHECK_RO;    /* check for RO again */
  255. ! #ifdef FEAT_AUTOCMD
  256. !         keep_filetype = TRUE;        /* don't detect 'filetype' */
  257. ! #endif
  258. !         if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
  259. !                 (linenr_T)0,
  260. !                 (linenr_T)MAXLNUM, &ea, READ_NEW) == FAIL)
  261. !         {
  262. !             EMSG2(_("E321: Could not reload \"%s\""), buf->b_fname);
  263. !             if (savebuf != NULL)
  264. !             {
  265. !             /* Put the text back from the save buffer.  First
  266. !              * delete any lines that readfile() added. */
  267. !             while (!bufempty())
  268. !                 if (ml_delete(curbuf->b_ml.ml_line_count, FALSE)
  269. !                                       == FAIL)
  270. !                 break;
  271. !             (void)move_lines(savebuf, buf);
  272. !             }
  273. !         }
  274. !         else
  275. !         {
  276. !             /* Mark the buffer as unmodified and free undo info. */
  277. !             unchanged(buf, TRUE);
  278. !             u_blockfree(buf);
  279. !             u_clearall(buf);
  280. !         }
  281.           }
  282.           vim_free(ea.cmd);
  283.   
  284. +         if (savebuf != NULL)
  285. +         wipe_buffer(savebuf, FALSE);
  286.   #ifdef FEAT_DIFF
  287.           /* Invalidate diff info if necessary. */
  288.           diff_invalidate();
  289. ***************
  290. *** 5313,5319 ****
  291.   #ifdef FEAT_AUTOCMD
  292.       /* restore curwin/curbuf and a few other things */
  293.       aucmd_restbuf(&aco);
  294.       /* Careful: autocommands may have made "buf" invalid! */
  295.   #else
  296.       curwin->w_buffer = save_curbuf;
  297. --- 5580,5585 ----
  298. ***************
  299. *** 6820,6828 ****
  300.   #endif
  301.   
  302.       /*
  303. !      * Quickly return if there are no autocommands for this event.
  304.        */
  305. !     if (first_autopat[(int)event] == NULL)
  306.       return retval;
  307.   
  308.       /*
  309. --- 7086,7095 ----
  310.   #endif
  311.   
  312.       /*
  313. !      * Quickly return if there are no autocommands for this event or
  314. !      * autocommands are blocked.
  315.        */
  316. !     if (first_autopat[(int)event] == NULL || autocmd_block > 0)
  317.       return retval;
  318.   
  319.       /*
  320. *** ../vim61.219/src/globals.h    Mon Oct  7 20:45:41 2002
  321. --- src/globals.h    Sun Oct 13 18:18:07 2002
  322. ***************
  323. *** 238,243 ****
  324. --- 238,244 ----
  325.   EXTERN int    autocmd_busy INIT(= FALSE);    /* Is apply_autocmds() busy? */
  326.   EXTERN int    autocmd_no_enter INIT(= FALSE); /* *Enter autocmds disabled */
  327.   EXTERN int    autocmd_no_leave INIT(= FALSE); /* *Leave autocmds disabled */
  328. + EXTERN int    autocmd_block INIT(= 0);    /* block all autocmds */
  329.   EXTERN int    modified_was_set;        /* did ":set modified" */
  330.   EXTERN int    did_filetype INIT(= FALSE);    /* FileType event found */
  331.   EXTERN int    keep_filetype INIT(= FALSE);    /* value for did_filetype when
  332. *** ../vim61.219/src/proto/buffer.pro    Fri Mar 22 21:41:05 2002
  333. --- src/proto/buffer.pro    Sun Oct 13 18:23:45 2002
  334. ***************
  335. *** 60,64 ****
  336.   void sign_mark_adjust __ARGS((linenr_T line1, linenr_T line2, long amount, long amount_after));
  337.   void set_buflisted __ARGS((int on));
  338.   int buf_contents_changed __ARGS((buf_T *buf));
  339. ! void wipe_buffer __ARGS((buf_T *buf));
  340.   /* vim: set ft=c : */
  341. --- 60,64 ----
  342.   void sign_mark_adjust __ARGS((linenr_T line1, linenr_T line2, long amount, long amount_after));
  343.   void set_buflisted __ARGS((int on));
  344.   int buf_contents_changed __ARGS((buf_T *buf));
  345. ! void wipe_buffer __ARGS((buf_T *buf, int aucmd));
  346.   /* vim: set ft=c : */
  347. *** ../vim61.219/src/version.c    Sun Oct 13 16:05:33 2002
  348. --- src/version.c    Sun Oct 13 18:45:52 2002
  349. ***************
  350. *** 608,609 ****
  351. --- 608,611 ----
  352.   {   /* Add new patch number below this line */
  353. + /**/
  354. +     220,
  355.   /**/
  356.  
  357. -- 
  358. hundred-and-one symptoms of being an internet addict:
  359. 211. Your husband leaves you...taking the computer with him and you
  360.      call him crying, and beg him to bring the computer back.
  361.  
  362.  ///  Bram Moolenaar -- Bram@moolenaar.net -- http://www.moolenaar.net  \\\
  363. ///          Creator of Vim - Vi IMproved -- http://www.vim.org          \\\
  364. \\\           Project leader for A-A-P -- http://www.a-a-p.org           ///
  365.  \\\ Lord Of The Rings helps Uganda - http://iccf-holland.org/lotr.html ///
  366.