2 * The routines in this file
3 * handle the reading and writing of
4 * disk files. All of details about the
5 * reading and writing of the disk are
14 * Read a file into the current
15 * buffer. This is really easy; all you do it
16 * find the name of the file, and call the standard
17 * "read a file into the current buffer" code.
27 if ((s
=mlreply("Read file: ", fname
, NFILEN
)) != TRUE
)
29 return (readin(fname
));
33 * Select a file for editing.
34 * Look around to see if you can find the
35 * fine in another buffer; if you can find it
36 * just switch to the buffer. If you cannot find
37 * the file, create a new buffer, read in the
38 * text, and switch to the new buffer.
53 if ((s
=mlreply("Visit file: ", fname
, NFILEN
)) != TRUE
)
55 for (bp
=bheadp
; bp
!=NULL
; bp
=bp
->b_bufp
) {
56 if ((bp
->b_flag
&BFTEMP
)==0 && strcmp(bp
->b_fname
, fname
)==0) {
57 if (--curbp
->b_nwnd
== 0) {
58 curbp
->b_dotp
= curwp
->w_dotp
;
59 curbp
->b_doto
= curwp
->w_doto
;
60 curbp
->b_markp
= curwp
->w_markp
;
61 curbp
->b_marko
= curwp
->w_marko
;
65 if (bp
->b_nwnd
++ == 0) {
66 curwp
->w_dotp
= bp
->b_dotp
;
67 curwp
->w_doto
= bp
->b_doto
;
68 curwp
->w_markp
= bp
->b_markp
;
69 curwp
->w_marko
= bp
->b_marko
;
73 if (wp
!=curwp
&& wp
->w_bufp
==bp
) {
74 curwp
->w_dotp
= wp
->w_dotp
;
75 curwp
->w_doto
= wp
->w_doto
;
76 curwp
->w_markp
= wp
->w_markp
;
77 curwp
->w_marko
= wp
->w_marko
;
84 i
= curwp
->w_ntrows
/2;
85 while (i
-- && lback(lp
)!=curbp
->b_linep
)
88 curwp
->w_flag
|= WFMODE
|WFHARD
;
89 mlwrite("[Old buffer]");
93 makename(bname
, fname
); /* New buffer name. */
94 while ((bp
=bfind(bname
, FALSE
, 0)) != NULL
) {
95 s
= mlreply("Buffer name: ", bname
, NBUFN
);
96 if (s
== ABORT
) /* ^G to just quit */
98 if (s
== FALSE
) { /* CR to clobber it */
99 makename(bname
, fname
);
103 if (bp
==NULL
&& (bp
=bfind(bname
, TRUE
, 0))==NULL
) {
104 mlwrite("Cannot create buffer");
107 if (--curbp
->b_nwnd
== 0) { /* Undisplay. */
108 curbp
->b_dotp
= curwp
->w_dotp
;
109 curbp
->b_doto
= curwp
->w_doto
;
110 curbp
->b_markp
= curwp
->w_markp
;
111 curbp
->b_marko
= curwp
->w_marko
;
113 curbp
= bp
; /* Switch to it. */
116 return (readin(fname
)); /* Read it in. */
120 * Read file "fname" into the current
121 * buffer, blowing away any text found there. Called
122 * by both the read and visit commands. Return the final
123 * status of the read. Also called by the mainline,
124 * to read in a file specified on the command line as
140 bp
= curbp
; /* Cheap. */
141 if ((s
=bclear(bp
)) != TRUE
) /* Might be old. */
143 bp
->b_flag
&= ~(BFTEMP
|BFCHG
);
144 strcpy(bp
->b_fname
, fname
);
145 if ((s
=ffropen(fname
)) == FIOERR
) /* Hard file open. */
147 if (s
== FIOFNF
) { /* File not found. */
148 mlwrite("[New file]");
151 mlwrite("[Reading file]");
153 while ((s
=ffgetline(line
, NLINE
)) == FIOSUC
) {
154 nbytes
= strlen(line
);
155 if ((lp1
=lalloc(nbytes
)) == NULL
) {
156 s
= FIOERR
; /* Keep message on the */
157 break; /* display. */
159 lp2
= lback(curbp
->b_linep
);
161 lp1
->l_fp
= curbp
->b_linep
;
163 curbp
->b_linep
->l_bp
= lp1
;
164 for (i
=0; i
<nbytes
; ++i
)
165 lputc(lp1
, i
, line
[i
]);
168 ffclose(); /* Ignore errors. */
169 if (s
== FIOEOF
) { /* Don't zap message! */
171 mlwrite("[Read 1 line]");
173 mlwrite("[Read %d lines]", nline
);
176 for (wp
=wheadp
; wp
!=NULL
; wp
=wp
->w_wndp
) {
177 if (wp
->w_bufp
== curbp
) {
178 wp
->w_linep
= lforw(curbp
->b_linep
);
179 wp
->w_dotp
= lforw(curbp
->b_linep
);
183 wp
->w_flag
|= WFMODE
|WFHARD
;
186 if (s
== FIOERR
) /* False if error. */
192 * Take a file name, and from it
193 * fabricate a buffer name. This routine knows
194 * about the syntax of file names on the target system.
195 * I suppose that this information could be put in
196 * a better place than a line of code.
198 void makename(bname
, fname
)
210 while (cp1
!=&fname
[0] && cp1
[-1]!=':' && cp1
[-1]!='/')
214 while (cp1
!=&fname
[0] && cp1
[-1]!=':' && cp1
[-1]!=']')
218 while (cp1
!=&fname
[0] && cp1
[-1]!=':')
222 while (cp1
!=&fname
[0] && cp1
[-1]!=':' && cp1
[-1]!='\\')
226 while (cp1
!=&fname
[0] && cp1
[-1]!='/')
230 while (cp2
!=&bname
[NBUFN
-1] && *cp1
!=0 && *cp1
!=';')
236 * Ask for a file name, and write the
237 * contents of the current buffer to that file.
238 * Update the remembered file name and clear the
239 * buffer changed flag. This handling of file names
240 * is different from the earlier versions, and
241 * is more compatable with Gosling EMACS than
242 * with ITS EMACS. Bound to "C-X C-W".
252 if ((s
=mlreply("Write file: ", fname
, NFILEN
)) != TRUE
)
254 if ((s
=writeout(fname
)) == TRUE
) {
255 strcpy(curbp
->b_fname
, fname
);
256 curbp
->b_flag
&= ~BFCHG
;
257 wp
= wheadp
; /* Update mode lines. */
259 if (wp
->w_bufp
== curbp
)
260 wp
->w_flag
|= WFMODE
;
268 * Save the contents of the current
269 * buffer in its associatd file. No nothing
270 * if nothing has changed (this may be a bug, not a
271 * feature). Error if there is no remembered file
272 * name for the buffer. Bound to "C-X C-S". May
273 * get called by "C-Z".
282 if ((curbp
->b_flag
&BFCHG
) == 0) /* Return, no changes. */
284 if (curbp
->b_fname
[0] == 0) { /* Must have a name. */
285 mlwrite("No file name");
288 if ((s
=writeout(curbp
->b_fname
)) == TRUE
) {
289 curbp
->b_flag
&= ~BFCHG
;
290 wp
= wheadp
; /* Update mode lines. */
292 if (wp
->w_bufp
== curbp
)
293 wp
->w_flag
|= WFMODE
;
301 * This function performs the details of file
302 * writing. Uses the file management routines in the
303 * "fileio.c" package. The number of lines written is
304 * displayed. Sadly, it looks inside a LINE; provide
305 * a macro for this. Most of the grief is error
306 * checking of some sort.
315 if ((s
=ffwopen(fn
)) != FIOSUC
) /* Open writes message. */
317 lp
= lforw(curbp
->b_linep
); /* First line. */
318 nline
= 0; /* Number of lines. */
319 while (lp
!= curbp
->b_linep
) {
320 if ((s
=ffputline(&lp
->l_text
[0], llength(lp
))) != FIOSUC
)
325 if (s
== FIOSUC
) { /* No write error. */
327 if (s
== FIOSUC
) { /* No close error. */
329 mlwrite("[Wrote 1 line]");
331 mlwrite("[Wrote %d lines]", nline
);
333 } else /* Ignore close error */
334 ffclose(); /* if a write error. */
335 if (s
!= FIOSUC
) /* Some sort of error. */
341 * The command allows the user
342 * to modify the file name associated with
343 * the current buffer. It is like the "f" command
344 * in UNIX "ed". The operation is simple; just zap
345 * the name in the BUFFER structure, and mark the windows
346 * as needing an update. You can type a blank line at the
347 * prompt if you wish.
357 if ((s
=mlreply("Name: ", fname
, NFILEN
)) == ABORT
)
360 strcpy(curbp
->b_fname
, "");
362 strcpy(curbp
->b_fname
, fname
);
363 wp
= wheadp
; /* Update mode lines. */
365 if (wp
->w_bufp
== curbp
)
366 wp
->w_flag
|= WFMODE
;