home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / apps / 21 / emacsrc / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-05-14  |  12.0 KB  |  344 lines

  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.  
  9. #include        "ed.h"
  10.  
  11. /*
  12.  * Read a file into the current
  13.  * buffer. This is really easy; all you do it
  14.  * find the name of the file, and call the standard
  15.  * "read a file into the current buffer" code.
  16.  * Bound to "C-X C-R".
  17.  */
  18. fileread(f, n)
  19. {
  20.         register int    s;
  21.         char            fname[NFILEN];
  22.  
  23.         if ((s=mlreply("Read file: ", fname, NFILEN)) != TRUE)
  24.                 return (s);
  25.         return (readin(fname));
  26. }
  27.  
  28. /*
  29.  * Select a file for editing.
  30.  * Look around to see if you can find the
  31.  * fine in another buffer; if you can find it
  32.  * just switch to the buffer. If you cannot find
  33.  * the file, create a new buffer, read in the
  34.  * text, and switch to the new buffer.
  35.  * Bound to C-X C-V.
  36.  */
  37. filevisit(f, n)
  38. {
  39.         register BUFFER *bp;
  40.         register WINDOW *wp;
  41.         register LINE   *lp;
  42.         register int    i;
  43.         register int    s;
  44.         char            bname[NBUFN];
  45.         char            fname[NFILEN];
  46.  
  47.         if ((s=mlreply("Visit file: ", fname, NFILEN)) != TRUE)
  48.                 return (s);
  49.         for (bp=bheadp; bp!=NULL; bp=bp->b_bufp) {
  50.                 if ((bp->b_flag&BFTEMP)==0 && strcmp(bp->b_fname, fname)==0) {
  51.                         if (--curbp->b_nwnd == 0) {
  52.                                 curbp->b_dotp  = curwp->w_dotp;
  53.                                 curbp->b_doto  = curwp->w_doto;
  54.                                 curbp->b_markp = curwp->w_markp;
  55.                                 curbp->b_marko = curwp->w_marko;
  56.                         }
  57.                         curbp = bp;
  58.                         curwp->w_bufp  = bp;
  59.                         if (bp->b_nwnd++ == 0) {
  60.                                 curwp->w_dotp  = bp->b_dotp;
  61.                                 curwp->w_doto  = bp->b_doto;
  62.                                 curwp->w_markp = bp->b_markp;
  63.                                 curwp->w_marko = bp->b_marko;
  64.                         } else {
  65.                                 wp = wheadp;
  66.                                 while (wp != NULL) {
  67.                                         if (wp!=curwp && wp->w_bufp==bp) {
  68.                                                 curwp->w_dotp  = wp->w_dotp;
  69.                                                 curwp->w_doto  = wp->w_doto;
  70.                                                 curwp->w_markp = wp->w_markp;
  71.                                                 curwp->w_marko = wp->w_marko;
  72.                                                 break;
  73.                                         }
  74.                                         wp = wp->w_wndp;
  75.                                 }
  76.                         }
  77.                         lp = curwp->w_dotp;
  78.                         i = curwp->w_ntrows/2;
  79.                         while (i-- && lback(lp)!=curbp->b_linep)
  80.                                 lp = lback(lp);
  81.                         curwp->w_linep = lp;
  82.                         curwp->w_flag |= WFMODE|WFHARD;
  83.                         mlwrite("[Old buffer]");
  84.                         return (TRUE);
  85.                 }
  86.         }
  87.         makename(bname, fname);                 /* New buffer name.     */
  88.         while ((bp=bfind(bname, FALSE, 0)) != NULL) {
  89.                 s = mlreply("A namesake buffer exists; give another name: ",
  90.              bname, NBUFN);
  91.                 if (s == ABORT)                 /* ^G to just quit      */
  92.                         return (s);
  93.                 if (s == FALSE) {               /* CR to clobber it     */
  94.                         makename(bname, fname);
  95.                         break;
  96.                 }
  97.         }
  98.         if (bp==NULL && (bp=bfind(bname, TRUE, 0))==NULL) {
  99.                 mlwrite("Cannot create buffer");
  100.                 return (FALSE);
  101.         }
  102.         if (--curbp->b_nwnd == 0) {             /* Undisplay.           */
  103.                 curbp->b_dotp = curwp->w_dotp;
  104.                 curbp->b_doto = curwp->w_doto;
  105.                 curbp->b_markp = curwp->w_markp;
  106.                 curbp->b_marko = curwp->w_marko;
  107.         }
  108.         curbp = bp;                             /* Switch to it.        */
  109.         curwp->w_bufp = bp;
  110.         curbp->b_nwnd++;
  111.         return (readin(fname));                 /* Read it in.          */
  112. }
  113.  
  114. /*
  115.  * Read file "fname" into the current
  116.  * buffer, blowing away any text found there. Called
  117.  * by both the read and visit commands. Return the final
  118.  * status of the read. Also called by the mainline,
  119.  * to read in a file specified on the command line as
  120.  * an argument.
  121.  */
  122. readin(fname)
  123. char    fname[];
  124. {
  125.         register LINE   *lp1;
  126.         register LINE   *lp2;
  127.         register int    i;
  128.         register WINDOW *wp;
  129.         register BUFFER *bp;
  130.         register int    s;
  131.          int    nbytes;
  132.          int    nline;
  133.          long    ntotal;
  134.          char   line[NLINE];
  135.  
  136.         bp = curbp;                             /* Cheap.               */
  137.         if ((s=bclear(bp)) != TRUE)             /* Might be old.        */
  138.                 return (s);
  139.         bp->b_flag &= ~(BFTEMP|BFCHG);
  140.         strcpy(bp->b_fname, fname);
  141.         if ((s=ffropen(fname)) == FIOERR)       /* Hard file open.      */
  142.                 goto out;
  143.         if (s == FIOFNF) {                      /* File not found.      */
  144.                 mlwrite("[New file]");
  145.                 goto out;
  146.         }
  147.         mlwrite("[Reading file]");
  148.         nline = 0;
  149.     ntotal = 0;
  150.         while ((s=ffgetline(line, NLINE, &nbytes)) == FIOSUC) {
  151. /*              nbytes = strlen(line);    */
  152.                 if ((lp1=lalloc(nbytes)) == NULL) {
  153.                         s = FIOERR;             /* Keep message on the  */
  154.                         break;                  /* display.             */
  155.                 }
  156.         ntotal += nbytes + 2;
  157.                 lp2 = lback(curbp->b_linep);
  158.                 lp2->l_fp = lp1;
  159.                 lp1->l_fp = curbp->b_linep;
  160.                 lp1->l_bp = lp2;
  161.                 curbp->b_linep->l_bp = lp1;
  162.                 for (i=0; i<nbytes; ++i)
  163.                         lputc(lp1, i, line[i]);
  164.                 ++nline;
  165.         }
  166.         ffclose();                              /* Ignore errors.       */
  167.         if (s == FIOEOF) {                      /* Don't zap message!   */
  168.         mlwrite("[Read %D bytes in %d lines]",ntotal, nline);
  169.         }
  170. out:
  171.         for (wp=wheadp; wp!=NULL; wp=wp->w_wndp) {
  172.                 if (wp->w_bufp == curbp) {
  173.                         wp->w_linep = lforw(curbp->b_linep);
  174.                         wp->w_dotp  = lforw(curbp->b_linep);
  175.                         wp->w_doto  = 0;
  176.                         wp->w_markp = NULL;
  177.                         wp->w_marko = 0;
  178.                         wp->w_flag |= WFMODE|WFHARD;
  179.                 }
  180.         }
  181.         if (s == FIOERR)                        /* False if error.      */
  182.                 return (FALSE);
  183.         return (TRUE);
  184. }
  185.  
  186. /*
  187.  * Take a file name, and from it
  188.  * fabricate a buffer name. This routine knows
  189.  * about the syntax of file names on the target system.
  190.  * I suppose that this information could be put in
  191.  * a better place than a line of code.
  192.  */
  193. makename(bname, fname)
  194. char    bname[];
  195. char    fname[];
  196. {
  197.         register char   *cp1;
  198.         register char   *cp2;
  199.  
  200.         cp1 = &fname[0];
  201.         while (*cp1 != 0)
  202.                 ++cp1;
  203.  
  204.         while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\')
  205.                 --cp1;
  206.         cp2 = &bname[0];
  207.         while (cp2!=&bname[NBUFN-1] && *cp1!=0 && *cp1!=';')
  208.                 *cp2++ = *cp1++;
  209.         *cp2 = 0;
  210. }
  211.  
  212. /*
  213.  * Ask for a file name, and write the
  214.  * contents of the current buffer to that file.
  215.  * Update the remembered file name and clear the
  216.  * buffer changed flag. This handling of file names
  217.  * is different from the earlier versions, and
  218.  * is more compatable with Gosling EMACS than
  219.  * with ITS EMACS. Bound to "C-X C-W".
  220.  */
  221. filewrite(f, n)
  222. {
  223.         register WINDOW *wp;
  224.         register int    s;
  225.         char            fname[NFILEN];
  226.  
  227.         if ((s=mlreply("Write file: ", fname, NFILEN)) != TRUE)
  228.                 return (s);
  229.         if ((s=writeout(fname)) == TRUE) {
  230.                 strcpy(curbp->b_fname, fname);
  231.                 curbp->b_flag &= ~BFCHG;
  232.                 wp = wheadp;                    /* Update mode lines.   */
  233.                 while (wp != NULL) {
  234.                         if (wp->w_bufp == curbp)
  235.                                 wp->w_flag |= WFMODE;
  236.                         wp = wp->w_wndp;
  237.                 }
  238.         }
  239.         return (s);
  240. }
  241.  
  242. /*
  243.  * Save the contents of the current
  244.  * buffer in its associatd file. No nothing
  245.  * if nothing has changed (this may be a bug, not a
  246.  * feature). Error if there is no remembered file
  247.  * name for the buffer. Bound to "C-X C-S". May
  248.  * get called by "C-Z".
  249.  */
  250. filesave(f, n)
  251. {
  252.         register WINDOW *wp;
  253.         register int    s;
  254.  
  255.         if ((curbp->b_flag&BFCHG) == 0)         /* Return, no changes.  */
  256.                 return (TRUE);
  257.         if (curbp->b_fname[0] == 0) {           /* Must have a name.    */
  258.                 mlwrite("No file name");
  259.                 return (FALSE);
  260.         }
  261.         if ((s=writeout(curbp->b_fname)) == TRUE) {
  262.                 curbp->b_flag &= ~BFCHG;
  263.                 wp = wheadp;                    /* Update mode lines.   */
  264.                 while (wp != NULL) {
  265.                         if (wp->w_bufp == curbp)
  266.                                 wp->w_flag |= WFMODE;
  267.                         wp = wp->w_wndp;
  268.                 }
  269.         }
  270.         return (s);
  271. }
  272.  
  273. /*
  274.  * This function performs the details of file
  275.  * writing. Uses the file management routines in the
  276.  * "fileio.c" package. The number of lines written is
  277.  * displayed. Sadly, it looks inside a LINE; provide
  278.  * a macro for this. Most of the grief is error
  279.  * checking of some sort.
  280.  */
  281. writeout(fn)
  282. char    *fn;
  283. {
  284.         register int    s;
  285.         register LINE   *lp;
  286.         register int    nline;
  287.     long        ntotal;
  288.     int        nb;
  289.  
  290.         if ((s=ffwopen(fn)) != FIOSUC)          /* Open writes message. */
  291.                 return (FALSE);
  292.         lp = lforw(curbp->b_linep);             /* First line.          */
  293.         ntotal = nline = 0;                     /* Number of lines.     */
  294.         while (lp != curbp->b_linep) {
  295.                 if ((s=ffputline(&lp->l_text[0], nb = llength(lp))) != FIOSUC)
  296.                         break;
  297.                 ++nline;
  298.         ntotal += nb + 2;
  299.                 lp = lforw(lp);
  300.         }
  301.         if (s == FIOSUC) {                      /* No write error.      */
  302.                 s = ffclose();
  303.                 if (s == FIOSUC) {              /* No close error.      */
  304.                         mlwrite("[Wrote %D bytes in %d lines]",ntotal, nline);
  305.                 }
  306.         } else                                  /* Ignore close error   */
  307.                 ffclose();                      /* if a write error.    */
  308.         if (s != FIOSUC)                        /* Some sort of error.  */
  309.                 return (FALSE);
  310.         return (TRUE);
  311. }
  312.  
  313. /*
  314.  * The command allows the user
  315.  * to modify the file name associated with
  316.  * the current buffer. It is like the "f" command
  317.  * in UNIX "ed". The operation is simple; just zap
  318.  * the name in the BUFFER structure, and mark the windows
  319.  * as needing an update. You can type a blank line at the
  320.  * prompt if you wish.
  321.  */
  322. filename(f, n)
  323. {
  324.         register WINDOW *wp;
  325.         register int    s;
  326.         char            fname[NFILEN];
  327.  
  328.         if ((s=mlreply("Name: ", fname, NFILEN)) == ABORT)
  329.                 return (s);
  330.         if (s == FALSE)
  331.                 strcpy(curbp->b_fname, "");
  332.         else
  333.                 strcpy(curbp->b_fname, fname);
  334.         wp = wheadp;                            /* Update mode lines.   */
  335.         while (wp != NULL) {
  336.                 if (wp->w_bufp == curbp)
  337.                         wp->w_flag |= WFMODE;
  338.                 wp = wp->w_wndp;
  339.         }
  340.         return (TRUE);
  341. }
  342.  
  343. /* -eof- */
  344.