@@ -70,6 +70,64 @@ static char *e_auabort = N_("E855: Autocommands caused command to abort");
7070/* Number of times free_buffer() was called. */
7171static int buf_free_count = 0 ;
7272
73+ /* Read data from buffer for retrying. */
74+ static int
75+ read_buffer (
76+ int read_stdin , /* read file from stdin, otherwise fifo */
77+ exarg_T * eap , /* for forced 'ff' and 'fenc' or NULL */
78+ int flags ) /* extra flags for readfile() */
79+ {
80+ int retval = OK ;
81+ linenr_T line_count ;
82+
83+ /*
84+ * Read from the buffer which the text is already filled in and append at
85+ * the end. This makes it possible to retry when 'fileformat' or
86+ * 'fileencoding' was guessed wrong.
87+ */
88+ line_count = curbuf -> b_ml .ml_line_count ;
89+ retval = readfile (
90+ read_stdin ? NULL : curbuf -> b_ffname ,
91+ read_stdin ? NULL : curbuf -> b_fname ,
92+ (linenr_T )line_count , (linenr_T )0 , (linenr_T )MAXLNUM , eap ,
93+ flags | READ_BUFFER );
94+ if (retval == OK )
95+ {
96+ /* Delete the binary lines. */
97+ while (-- line_count >= 0 )
98+ ml_delete ((linenr_T )1 , FALSE);
99+ }
100+ else
101+ {
102+ /* Delete the converted lines. */
103+ while (curbuf -> b_ml .ml_line_count > line_count )
104+ ml_delete (line_count , FALSE);
105+ }
106+ /* Put the cursor on the first line. */
107+ curwin -> w_cursor .lnum = 1 ;
108+ curwin -> w_cursor .col = 0 ;
109+
110+ if (read_stdin )
111+ {
112+ /* Set or reset 'modified' before executing autocommands, so that
113+ * it can be changed there. */
114+ if (!readonlymode && !bufempty ())
115+ changed ();
116+ else if (retval != FAIL )
117+ unchanged (curbuf , FALSE);
118+
119+ #ifdef FEAT_AUTOCMD
120+ # ifdef FEAT_EVAL
121+ apply_autocmds_retval (EVENT_STDINREADPOST , NULL , NULL , FALSE,
122+ curbuf , & retval );
123+ # else
124+ apply_autocmds (EVENT_STDINREADPOST , NULL , NULL , FALSE, curbuf );
125+ # endif
126+ #endif
127+ }
128+ return retval ;
129+ }
130+
73131/*
74132 * Open current buffer, that is: open the memfile and read the file into
75133 * memory.
@@ -88,6 +146,7 @@ open_buffer(
88146#ifdef FEAT_SYN_HL
89147 long old_tw = curbuf -> b_p_tw ;
90148#endif
149+ int read_fifo = FALSE;
91150
92151 /*
93152 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
@@ -143,17 +202,42 @@ open_buffer(
143202 )
144203 {
145204 int old_msg_silent = msg_silent ;
146-
205+ #ifdef UNIX
206+ int save_bin = curbuf -> b_p_bin ;
207+ int perm ;
208+ #endif
147209#ifdef FEAT_NETBEANS_INTG
148210 int oldFire = netbeansFireChanges ;
149211
150212 netbeansFireChanges = 0 ;
213+ #endif
214+ #ifdef UNIX
215+ perm = mch_getperm (curbuf -> b_ffname );
216+ if (perm >= 0 && (0
217+ # ifdef S_ISFIFO
218+ || S_ISFIFO (perm )
219+ # endif
220+ # ifdef S_ISSOCK
221+ || S_ISSOCK (perm )
222+ # endif
223+ ))
224+ read_fifo = TRUE;
225+ if (read_fifo )
226+ curbuf -> b_p_bin = TRUE;
151227#endif
152228 if (shortmess (SHM_FILEINFO ))
153229 msg_silent = 1 ;
154230 retval = readfile (curbuf -> b_ffname , curbuf -> b_fname ,
155231 (linenr_T )0 , (linenr_T )0 , (linenr_T )MAXLNUM , eap ,
156- flags | READ_NEW );
232+ flags | READ_NEW | (read_fifo ? READ_FIFO : 0 ));
233+ #ifdef UNIX
234+ if (read_fifo )
235+ {
236+ curbuf -> b_p_bin = save_bin ;
237+ if (retval == OK )
238+ retval = read_buffer (FALSE, eap , flags );
239+ }
240+ #endif
157241 msg_silent = old_msg_silent ;
158242#ifdef FEAT_NETBEANS_INTG
159243 netbeansFireChanges = oldFire ;
@@ -164,8 +248,7 @@ open_buffer(
164248 }
165249 else if (read_stdin )
166250 {
167- int save_bin = curbuf -> b_p_bin ;
168- linenr_T line_count ;
251+ int save_bin = curbuf -> b_p_bin ;
169252
170253 /*
171254 * First read the text in binary mode into the buffer.
@@ -179,42 +262,7 @@ open_buffer(
179262 flags | (READ_NEW + READ_STDIN ));
180263 curbuf -> b_p_bin = save_bin ;
181264 if (retval == OK )
182- {
183- line_count = curbuf -> b_ml .ml_line_count ;
184- retval = readfile (NULL , NULL , (linenr_T )line_count ,
185- (linenr_T )0 , (linenr_T )MAXLNUM , eap ,
186- flags | READ_BUFFER );
187- if (retval == OK )
188- {
189- /* Delete the binary lines. */
190- while (-- line_count >= 0 )
191- ml_delete ((linenr_T )1 , FALSE);
192- }
193- else
194- {
195- /* Delete the converted lines. */
196- while (curbuf -> b_ml .ml_line_count > line_count )
197- ml_delete (line_count , FALSE);
198- }
199- /* Put the cursor on the first line. */
200- curwin -> w_cursor .lnum = 1 ;
201- curwin -> w_cursor .col = 0 ;
202-
203- /* Set or reset 'modified' before executing autocommands, so that
204- * it can be changed there. */
205- if (!readonlymode && !bufempty ())
206- changed ();
207- else if (retval != FAIL )
208- unchanged (curbuf , FALSE);
209- #ifdef FEAT_AUTOCMD
210- # ifdef FEAT_EVAL
211- apply_autocmds_retval (EVENT_STDINREADPOST , NULL , NULL , FALSE,
212- curbuf , & retval );
213- # else
214- apply_autocmds (EVENT_STDINREADPOST , NULL , NULL , FALSE, curbuf );
215- # endif
216- #endif
217- }
265+ retval = read_buffer (TRUE, eap , flags );
218266 }
219267
220268 /* if first time loading this buffer, init b_chartab[] */
@@ -243,7 +291,7 @@ open_buffer(
243291#endif
244292 )
245293 changed ();
246- else if (retval != FAIL && !read_stdin )
294+ else if (retval != FAIL && !read_stdin && ! read_fifo )
247295 unchanged (curbuf , FALSE);
248296 save_file_ff (curbuf ); /* keep this fileformat */
249297
0 commit comments