Mercurial > vim
comparison src/fileio.c @ 2394:a3aca345aafa vim73
Add the 'undoreload' option to be able to undo a file reload.
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Sat, 24 Jul 2010 20:27:03 +0200 |
parents | d8e4b27cef80 |
children | 8f6106dd3d12 |
comparison
equal
deleted
inserted
replaced
2393:210a5605e126 | 2394:a3aca345aafa |
---|---|
217 * READ_FILTER reading filter output | 217 * READ_FILTER reading filter output |
218 * READ_STDIN read from stdin instead of a file | 218 * READ_STDIN read from stdin instead of a file |
219 * READ_BUFFER read from curbuf instead of a file (converting after reading | 219 * READ_BUFFER read from curbuf instead of a file (converting after reading |
220 * stdin) | 220 * stdin) |
221 * READ_DUMMY read into a dummy buffer (to check if file contents changed) | 221 * READ_DUMMY read into a dummy buffer (to check if file contents changed) |
222 * READ_KEEP_UNDO don't clear undo info or read it from a file | |
222 * | 223 * |
223 * return FAIL for failure, OK otherwise | 224 * return FAIL for failure, OK otherwise |
224 */ | 225 */ |
225 int | 226 int |
226 readfile(fname, sfname, from, lines_to_skip, lines_to_read, eap, flags) | 227 readfile(fname, sfname, from, lines_to_skip, lines_to_read, eap, flags) |
1193 read_count = lines_to_read; | 1194 read_count = lines_to_read; |
1194 #ifdef FEAT_MBYTE | 1195 #ifdef FEAT_MBYTE |
1195 conv_restlen = 0; | 1196 conv_restlen = 0; |
1196 #endif | 1197 #endif |
1197 #ifdef FEAT_PERSISTENT_UNDO | 1198 #ifdef FEAT_PERSISTENT_UNDO |
1198 read_undo_file = (newfile && curbuf->b_ffname != NULL && curbuf->b_p_udf | 1199 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0 |
1199 && !filtering && !read_stdin && !read_buffer); | 1200 && curbuf->b_ffname != NULL |
1201 && curbuf->b_p_udf | |
1202 && !filtering | |
1203 && !read_stdin | |
1204 && !read_buffer); | |
1200 if (read_undo_file) | 1205 if (read_undo_file) |
1201 sha256_start(&sha_ctx); | 1206 sha256_start(&sha_ctx); |
1202 #endif | 1207 #endif |
1203 } | 1208 } |
1204 | 1209 |
7075 linenr_T old_topline; | 7080 linenr_T old_topline; |
7076 int old_ro = buf->b_p_ro; | 7081 int old_ro = buf->b_p_ro; |
7077 buf_T *savebuf; | 7082 buf_T *savebuf; |
7078 int saved = OK; | 7083 int saved = OK; |
7079 aco_save_T aco; | 7084 aco_save_T aco; |
7085 int flags = READ_NEW; | |
7080 | 7086 |
7081 /* set curwin/curbuf for "buf" and save some things */ | 7087 /* set curwin/curbuf for "buf" and save some things */ |
7082 aucmd_prepbuf(&aco, buf); | 7088 aucmd_prepbuf(&aco, buf); |
7083 | 7089 |
7084 /* We only want to read the text from the file, not reset the syntax | 7090 /* We only want to read the text from the file, not reset the syntax |
7086 * and encoding to be the same. */ | 7092 * and encoding to be the same. */ |
7087 if (prep_exarg(&ea, buf) == OK) | 7093 if (prep_exarg(&ea, buf) == OK) |
7088 { | 7094 { |
7089 old_cursor = curwin->w_cursor; | 7095 old_cursor = curwin->w_cursor; |
7090 old_topline = curwin->w_topline; | 7096 old_topline = curwin->w_topline; |
7097 | |
7098 if (saved == OK && (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)) | |
7099 { | |
7100 /* Save all the text, so that the reload can be undone. | |
7101 * Sync first so that this is a separate undo-able action. */ | |
7102 u_sync(FALSE); | |
7103 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE); | |
7104 flags |= READ_KEEP_UNDO; | |
7105 } | |
7091 | 7106 |
7092 /* | 7107 /* |
7093 * To behave like when a new file is edited (matters for | 7108 * To behave like when a new file is edited (matters for |
7094 * BufReadPost autocommands) we first need to delete the current | 7109 * BufReadPost autocommands) we first need to delete the current |
7095 * buffer contents. But if reading the file fails we should keep | 7110 * buffer contents. But if reading the file fails we should keep |
7096 * the old contents. Can't use memory only, the file might be | 7111 * the old contents. Can't use memory only, the file might be |
7097 * too big. Use a hidden buffer to move the buffer contents to. | 7112 * too big. Use a hidden buffer to move the buffer contents to. |
7098 */ | 7113 */ |
7099 if (bufempty()) | 7114 if (bufempty() || saved == FAIL) |
7100 savebuf = NULL; | 7115 savebuf = NULL; |
7101 else | 7116 else |
7102 { | 7117 { |
7103 /* Allocate a buffer without putting it in the buffer list. */ | 7118 /* Allocate a buffer without putting it in the buffer list. */ |
7104 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); | 7119 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); |
7126 #ifdef FEAT_AUTOCMD | 7141 #ifdef FEAT_AUTOCMD |
7127 keep_filetype = TRUE; /* don't detect 'filetype' */ | 7142 keep_filetype = TRUE; /* don't detect 'filetype' */ |
7128 #endif | 7143 #endif |
7129 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0, | 7144 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0, |
7130 (linenr_T)0, | 7145 (linenr_T)0, |
7131 (linenr_T)MAXLNUM, &ea, READ_NEW) == FAIL) | 7146 (linenr_T)MAXLNUM, &ea, flags) == FAIL) |
7132 { | 7147 { |
7133 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | 7148 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
7134 if (!aborting()) | 7149 if (!aborting()) |
7135 #endif | 7150 #endif |
7136 EMSG2(_("E321: Could not reload \"%s\""), buf->b_fname); | 7151 EMSG2(_("E321: Could not reload \"%s\""), buf->b_fname); |
7142 if (ml_delete(buf->b_ml.ml_line_count, FALSE) == FAIL) | 7157 if (ml_delete(buf->b_ml.ml_line_count, FALSE) == FAIL) |
7143 break; | 7158 break; |
7144 (void)move_lines(savebuf, buf); | 7159 (void)move_lines(savebuf, buf); |
7145 } | 7160 } |
7146 } | 7161 } |
7147 else if (buf == curbuf) | 7162 else if (buf == curbuf) /* "buf" still valid */ |
7148 { | 7163 { |
7149 /* Mark the buffer as unmodified and free undo info. */ | 7164 /* Mark the buffer as unmodified and free undo info. */ |
7150 unchanged(buf, TRUE); | 7165 unchanged(buf, TRUE); |
7151 u_blockfree(buf); | 7166 if ((flags & READ_KEEP_UNDO) == 0) |
7152 u_clearall(buf); | 7167 { |
7168 u_blockfree(buf); | |
7169 u_clearall(buf); | |
7170 } | |
7171 else | |
7172 /* Mark all undo states as changed. */ | |
7173 u_unchanged(curbuf); | |
7153 } | 7174 } |
7154 } | 7175 } |
7155 vim_free(ea.cmd); | 7176 vim_free(ea.cmd); |
7156 | 7177 |
7157 if (savebuf != NULL && buf_valid(savebuf)) | 7178 if (savebuf != NULL && buf_valid(savebuf)) |