forgotten commit. disabled until egl is adapted.
[AROS-Contrib.git] / fish / microemacs / file.c
blob10e6ae5aa95e3e4cfcf5b49b94cde20d506258a9
1 /*
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
6 * in "fileio.c".
7 */
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include "ed.h"
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.
18 * Bound to "C-X C-R".
20 int fileread(f, n)
21 int f;
22 int n;
24 register int s;
25 char fname[NFILEN];
27 if ((s=mlreply("Read file: ", fname, NFILEN)) != TRUE)
28 return (s);
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.
39 * Bound to C-X C-V.
41 int filevisit(f, n)
42 int f;
43 int n;
45 register BUFFER *bp;
46 register WINDOW *wp;
47 register LINE *lp;
48 register int i;
49 register int s;
50 char bname[NBUFN];
51 char fname[NFILEN];
53 if ((s=mlreply("Visit file: ", fname, NFILEN)) != TRUE)
54 return (s);
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;
63 curbp = bp;
64 curwp->w_bufp = bp;
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;
70 } else {
71 wp = wheadp;
72 while (wp != NULL) {
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;
78 break;
80 wp = wp->w_wndp;
83 lp = curwp->w_dotp;
84 i = curwp->w_ntrows/2;
85 while (i-- && lback(lp)!=curbp->b_linep)
86 lp = lback(lp);
87 curwp->w_linep = lp;
88 curwp->w_flag |= WFMODE|WFHARD;
89 mlwrite("[Old buffer]");
90 return (TRUE);
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 */
97 return (s);
98 if (s == FALSE) { /* CR to clobber it */
99 makename(bname, fname);
100 break;
103 if (bp==NULL && (bp=bfind(bname, TRUE, 0))==NULL) {
104 mlwrite("Cannot create buffer");
105 return (FALSE);
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. */
114 curwp->w_bufp = bp;
115 curbp->b_nwnd++;
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
125 * an argument.
127 int readin(fname)
128 char fname[];
130 register LINE *lp1;
131 register LINE *lp2;
132 register int i;
133 register WINDOW *wp;
134 register BUFFER *bp;
135 register int s;
136 register int nbytes;
137 register int nline;
138 char line[NLINE];
140 bp = curbp; /* Cheap. */
141 if ((s=bclear(bp)) != TRUE) /* Might be old. */
142 return (s);
143 bp->b_flag &= ~(BFTEMP|BFCHG);
144 strcpy(bp->b_fname, fname);
145 if ((s=ffropen(fname)) == FIOERR) /* Hard file open. */
146 goto out;
147 if (s == FIOFNF) { /* File not found. */
148 mlwrite("[New file]");
149 goto out;
151 mlwrite("[Reading file]");
152 nline = 0;
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);
160 lp2->l_fp = lp1;
161 lp1->l_fp = curbp->b_linep;
162 lp1->l_bp = lp2;
163 curbp->b_linep->l_bp = lp1;
164 for (i=0; i<nbytes; ++i)
165 lputc(lp1, i, line[i]);
166 ++nline;
168 ffclose(); /* Ignore errors. */
169 if (s == FIOEOF) { /* Don't zap message! */
170 if (nline == 1)
171 mlwrite("[Read 1 line]");
172 else
173 mlwrite("[Read %d lines]", nline);
175 out:
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);
180 wp->w_doto = 0;
181 wp->w_markp = NULL;
182 wp->w_marko = 0;
183 wp->w_flag |= WFMODE|WFHARD;
186 if (s == FIOERR) /* False if error. */
187 return (FALSE);
188 return (TRUE);
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)
199 char bname[];
200 char fname[];
202 register char *cp1;
203 register char *cp2;
205 cp1 = &fname[0];
206 while (*cp1 != 0)
207 ++cp1;
209 #if AMIGA
210 while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='/')
211 --cp1;
212 #endif
213 #if VMS
214 while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!=']')
215 --cp1;
216 #endif
217 #if CPM
218 while (cp1!=&fname[0] && cp1[-1]!=':')
219 --cp1;
220 #endif
221 #if MSDOS
222 while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\')
223 --cp1;
224 #endif
225 #if V7
226 while (cp1!=&fname[0] && cp1[-1]!='/')
227 --cp1;
228 #endif
229 cp2 = &bname[0];
230 while (cp2!=&bname[NBUFN-1] && *cp1!=0 && *cp1!=';')
231 *cp2++ = *cp1++;
232 *cp2 = 0;
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".
244 int filewrite(f, n)
245 int f;
246 int n;
248 register WINDOW *wp;
249 register int s;
250 char fname[NFILEN];
252 if ((s=mlreply("Write file: ", fname, NFILEN)) != TRUE)
253 return (s);
254 if ((s=writeout(fname)) == TRUE) {
255 strcpy(curbp->b_fname, fname);
256 curbp->b_flag &= ~BFCHG;
257 wp = wheadp; /* Update mode lines. */
258 while (wp != NULL) {
259 if (wp->w_bufp == curbp)
260 wp->w_flag |= WFMODE;
261 wp = wp->w_wndp;
264 return (s);
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".
275 int filesave(f, n)
276 int f;
277 int n;
279 register WINDOW *wp;
280 register int s;
282 if ((curbp->b_flag&BFCHG) == 0) /* Return, no changes. */
283 return (TRUE);
284 if (curbp->b_fname[0] == 0) { /* Must have a name. */
285 mlwrite("No file name");
286 return (FALSE);
288 if ((s=writeout(curbp->b_fname)) == TRUE) {
289 curbp->b_flag &= ~BFCHG;
290 wp = wheadp; /* Update mode lines. */
291 while (wp != NULL) {
292 if (wp->w_bufp == curbp)
293 wp->w_flag |= WFMODE;
294 wp = wp->w_wndp;
297 return (s);
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.
308 int writeout(fn)
309 const char *fn;
311 register int s;
312 register LINE *lp;
313 register int nline;
315 if ((s=ffwopen(fn)) != FIOSUC) /* Open writes message. */
316 return (FALSE);
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)
321 break;
322 ++nline;
323 lp = lforw(lp);
325 if (s == FIOSUC) { /* No write error. */
326 s = ffclose();
327 if (s == FIOSUC) { /* No close error. */
328 if (nline == 1)
329 mlwrite("[Wrote 1 line]");
330 else
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. */
336 return (FALSE);
337 return (TRUE);
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.
349 int filename(f, n)
350 int f;
351 int n;
353 register WINDOW *wp;
354 register int s;
355 char fname[NFILEN];
357 if ((s=mlreply("Name: ", fname, NFILEN)) == ABORT)
358 return (s);
359 if (s == FALSE)
360 strcpy(curbp->b_fname, "");
361 else
362 strcpy(curbp->b_fname, fname);
363 wp = wheadp; /* Update mode lines. */
364 while (wp != NULL) {
365 if (wp->w_bufp == curbp)
366 wp->w_flag |= WFMODE;
367 wp = wp->w_wndp;
369 return (TRUE);