home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / memacs / ue311c.arc / SCREEN.C < prev    next >
C/C++ Source or Header  |  1991-03-12  |  10KB  |  419 lines

  1. /*    SCREEN.C:    Screen manipulation commands
  2.             for MicroEMACS 3.11
  3.             written by Daniel Lawrence
  4. */
  5.  
  6. #include    <stdio.h>
  7. #include    "estruct.h"
  8. #include    "eproto.h"
  9. #include        "edef.h"
  10. #include    "elang.h"
  11.  
  12. PASCAL NEAR next_screen(f, n)
  13.  
  14. int f,n;    /* prefix flag and argument */
  15.  
  16. {
  17.     SCREEN *sp;        /* ptr to screen to switch to */
  18.  
  19.     /* find the next screen */
  20.     sp = cur_screen->s_next_screen;
  21.     if (sp == (SCREEN *)NULL)
  22.         sp = first_screen;
  23.  
  24.     /* and make this screen current */
  25.     mlwrite("[Switching to screen %s]", sp->s_screen_name);
  26.     return(select_screen(sp));
  27. }
  28.  
  29. PASCAL NEAR prev_screen(f, n)
  30.  
  31. int f,n;    /* prefix flag and argument */
  32.  
  33. {
  34.     SCREEN *sp;        /* ptr to screen to switch to */
  35.  
  36.     /* find the last screen */
  37.     if (cur_screen == first_screen) {
  38.         sp = first_screen;
  39.         while (sp->s_next_screen)
  40.             sp = sp->s_next_screen;
  41.     } else {
  42.         sp = first_screen;
  43.         while (sp) {
  44.             if (sp->s_next_screen == cur_screen)
  45.                 break;
  46.             sp = sp->s_next_screen;
  47.         }
  48.     }
  49.  
  50.     /* and make this screen current */
  51.     mlwrite("[Switching to screen %s]", sp->s_screen_name);
  52.     return(select_screen(sp));
  53. }
  54.  
  55. PASCAL NEAR find_screen(f, n)
  56.  
  57. int f,n;    /* prefix flag and argument */
  58.  
  59. {
  60.     char scr_name[NSTRING];    /* buffer to hold screen name */
  61.     SCREEN *sp;        /* ptr to screen to switch to */
  62.  
  63.     /* get the name of the screen to switch to */
  64.     mlreply("Find Screen: ", scr_name, NSTRING);
  65.     sp = lookup_screen(scr_name);
  66.  
  67.     if (sp == (SCREEN *)NULL) {
  68.  
  69.         /* save the current dot position in the buffer info */
  70.         curbp->b_dotp = curwp->w_dotp;
  71.         curbp->b_doto = curwp->w_doto;
  72.  
  73.         /* screen does not exist, create it */
  74.         sp = init_screen(scr_name, curbp);
  75.         insert_screen(sp);
  76.     }
  77.  
  78.     /* and make this screen current */
  79.     mlwrite("[Switching to screen %s]", scr_name);
  80.     return(select_screen(sp));
  81. }
  82.  
  83. PASCAL NEAR free_screen(sp)    /* free all resouces associated with a screen */
  84.  
  85. SCREEN *sp;    /* screen to dump */
  86.  
  87. {
  88.     register WINDOW *wp;    /* ptr to window to free */
  89.     register WINDOW *tp;    /* temp window pointer */
  90.  
  91.     /* first, free the screen's windows */
  92.     wp = sp->s_first_window;
  93.     while (wp) {
  94.         tp = wp->w_wndp;
  95.         free(wp);
  96.         wp = tp;
  97.     }
  98.  
  99.     /* and now, free the screen struct itself */
  100.     free(sp);
  101. }
  102.  
  103. PASCAL NEAR delete_screen(f, n)
  104.  
  105. int f,n;    /* prefix flag and argument */
  106.  
  107. {
  108.     char scr_name[NSTRING];    /* buffer to hold screen name */
  109.     SCREEN *sp;        /* ptr to screen to switch to */
  110.     SCREEN *last_scr;    /* screen previous to one to delete */
  111.  
  112.     /* get the name of the screen to switch to */
  113.     mlreply("Delete Screen: ", scr_name, NSTRING);
  114.     sp = lookup_screen(scr_name);
  115.  
  116.     /* make sure it exists... */
  117.     if (sp == (SCREEN *)NULL) {
  118.         mlwrite("[No such screen]");
  119.         return(FALSE);
  120.     }
  121.  
  122.     /* it can't be current... */
  123.     if (sp == cur_screen) {
  124.         mlwrite("%%Can't delete current screen");
  125.         return(FALSE);
  126.     }
  127.  
  128.     /* at the beginning? */
  129.     if (sp == first_screen) {
  130.         last_scr = first_screen->s_next_screen;
  131.         free_screen(sp);
  132.         first_screen = last_scr;
  133.         return(TRUE);
  134.     }
  135.  
  136.     /* find the place to delete */
  137.     last_scr = first_screen;
  138.     while (last_scr) {
  139.         if (last_scr->s_next_screen == sp)
  140.             break;
  141.         last_scr = last_scr->s_next_screen;
  142.     }
  143.     last_scr->s_next_screen = sp->s_next_screen;
  144.     free_screen(sp);
  145.     return(TRUE);
  146. }
  147.  
  148. /* this function initializes a new screen.... */
  149.  
  150. SCREEN *PASCAL NEAR init_screen(scr_name, scr_buf)
  151.  
  152. char *scr_name;        /* screen name */
  153. BUFFER *scr_buf;    /* buffer to place in first window of screen */
  154.  
  155. {
  156.     int cmark;    /* current mark to initialize */
  157.     SCREEN *sp;    /* pointer to allocated screen */
  158.     WINDOW *wp;    /* ptr to first window of new screen */
  159.  
  160.     /* allocate memory for this screen */
  161.     sp = (SCREEN *)malloc(sizeof(SCREEN));
  162.     if (sp == (SCREEN *)NULL)
  163.         return(sp);
  164.  
  165.     /* set up this new screens fields! */
  166.     sp->s_next_screen = (SCREEN *)NULL;
  167.     sp->s_screen_name = copystr(scr_name);
  168.  
  169.     /* allocate its first window */
  170.     wp = (WINDOW *)malloc(sizeof(WINDOW));
  171.     if (wp == (WINDOW *)NULL) {
  172.         free((char *)sp);
  173.         return((SCREEN *)NULL);
  174.     }
  175.     sp->s_first_window = sp->s_cur_window = wp;
  176.  
  177.     /* and setup the windows info */
  178.     wp->w_wndp = NULL;
  179.     wp->w_bufp = scr_buf;
  180.     scr_buf->b_nwnd += 1;    
  181.     wp->w_linep = scr_buf->b_linep;
  182.  
  183.     /* position us at the buffers dot */
  184.     wp->w_dotp  = scr_buf->b_dotp;
  185.     wp->w_doto  = scr_buf->b_doto;
  186.  
  187.     /* set all the marks to UNSET */
  188.     for (cmark = 0; cmark < NMARKS; cmark++) {
  189.         wp->w_markp[cmark] = NULL;
  190.         wp->w_marko[cmark] = 0;
  191.     }
  192.     wp->w_toprow = 0;
  193. #if    COLOR
  194.     /* initalize colors to global defaults */
  195.     wp->w_fcolor = gfcolor;
  196.     wp->w_bcolor = gbcolor;
  197. #endif
  198.     wp->w_fcol = 0;
  199.     wp->w_ntrows = term.t_nrow-1;        /* "-1" for mode line.    */
  200.     wp->w_force = 0;
  201.     wp->w_flag  = WFMODE|WFHARD;        /* Full.        */
  202.  
  203.     /* and return the new screen pointer */
  204.     return(sp);
  205. }
  206.  
  207. SCREEN *PASCAL NEAR lookup_screen(scr_name)
  208.  
  209. char *scr_name;        /* named screen to find */
  210.  
  211. {
  212.     SCREEN *result;
  213.  
  214.     /* scan the screen list */
  215.     result = first_screen;
  216.     while (result) {
  217.  
  218.         /* if this is it, return its handle! */
  219.         if (strcmp(scr_name, result->s_screen_name) == 0)
  220.             return(result);
  221.  
  222.         /* on to the next screen */
  223.         result = result->s_next_screen;
  224.     }
  225.  
  226.     /* we didn't find it..... */
  227.     return((SCREEN *)NULL);
  228. }
  229.  
  230. SCREEN *PASCAL NEAR index_screen(scr_num)
  231.  
  232. int scr_num;        /* index of screen handle to return */
  233.  
  234. {
  235.     SCREEN *result;
  236.  
  237.     /* screen numbers are ONE based */
  238.     --scr_num;
  239.  
  240.     /* scan the screen list */
  241.     result = first_screen;
  242.     while (result && scr_num--) {
  243.  
  244.         /* on to the next screen */
  245.         result = result->s_next_screen;
  246.     }
  247.  
  248.     /* return what we found! */
  249.     return(result);
  250. }
  251.  
  252. int PASCAL NEAR screen_index(sp)
  253.  
  254. SCREEN *sp;    /* screen handle to find index to */
  255.  
  256. {
  257.     int result;        /* screen index result */
  258.     SCREEN *cur_screen;
  259.  
  260.     /* scan the screen list */
  261.     cur_screen = first_screen;
  262.     result = 1;
  263.     while (cur_screen) {
  264.  
  265.         /* if this is it, return its handle! */
  266.         if (cur_screen == sp)
  267.             return(result);
  268.  
  269.         /* on to the next screen */
  270.         cur_screen = cur_screen->s_next_screen;
  271.         ++result;
  272.     }
  273.  
  274.     /* we didn't find it..... */
  275.     return(0);
  276. }
  277.  
  278. PASCAL NEAR insert_screen(sp)
  279.  
  280. SCREEN *sp;    /* screen to insert in screen list */
  281.  
  282. {
  283.     SCREEN *cur_screen;    /* ptr into screen list */
  284.  
  285.     /* go to the end of the screen list */
  286.     cur_screen = first_screen;
  287.     while (cur_screen->s_next_screen)
  288.         cur_screen = cur_screen->s_next_screen;
  289.  
  290.     /* and insert it */
  291.     cur_screen->s_next_screen = sp;
  292.     sp->s_next_screen = (SCREEN *)NULL;
  293. }
  294.  
  295. int PASCAL NEAR select_screen(sp)
  296.  
  297. SCREEN *sp;    /* ptr to screen to switch to */
  298.  
  299. {
  300.     /* make sure there is something here to set to! */
  301.     if (sp == (SCREEN *)NULL)
  302.         return(FALSE);
  303.  
  304.     /* save the current screens concept of current window */
  305.     cur_screen->s_cur_window = curwp;
  306.  
  307.     /* reset the current screen, window and buffer */
  308.     cur_screen = sp;
  309.     scr_num = screen_index(sp);
  310.     wheadp = cur_screen->s_first_window;
  311.     curwp = cur_screen->s_cur_window;
  312.     curbp = curwp->w_bufp;
  313.  
  314.     /* let the display driver know we need a full screen */
  315.     upwind();
  316.  
  317.     return(TRUE);
  318. }
  319.  
  320. /*    Build and popup a buffer containing the list of all screens.
  321.     Bound to "A-B".
  322. */
  323.  
  324. PASCAL NEAR list_screens(f, n)
  325.  
  326. int f,n;    /* prefix flag and argument */
  327.  
  328. {
  329.     register int status;    /* stutus return */
  330.  
  331.     if ((status = screenlist(f)) != TRUE)
  332.         return(status);
  333.     return(wpopup(slistp));
  334. }
  335.  
  336.  
  337. /*
  338.  * This routine rebuilds the
  339.  * text in the special secret buffer
  340.  * that holds the screen list. It is called
  341.  * by the list screens command. Return TRUE
  342.  * if everything works. Return FALSE if there
  343.  * is an error (if there is no memory). Iflag
  344.  * indecates weather to list hidden screens.
  345.  */
  346. PASCAL NEAR screenlist(iflag)
  347.  
  348. int iflag;    /* list hidden screen flag */
  349.  
  350. {
  351.     SCREEN *sp;        /* ptr to current screen to list */
  352.     WINDOW *wp;        /* ptr into current screens window list */
  353.     int status;        /* return status from functions */
  354.     char line[NSTRING];    /* buffer to construct list lines */
  355.     char bname[NSTRING];    /* name of next buffer */
  356.  
  357.     /* mark this buffer as unchanged so... */
  358.     slistp->b_flag &= ~BFCHG;
  359.  
  360.     /* we can dump it's old contents without complaint */
  361.     if ((status = bclear(slistp)) != TRUE)
  362.         return(status);
  363.  
  364.     /* there is no file connected with this buffer */
  365.     strcpy(slistp->b_fname, "");
  366.  
  367.     /* construct the header of this list */
  368.     if (addline(slistp, "Screen         Buffers") == FALSE
  369.      || addline(slistp, "------         -------") == FALSE)
  370.         return(FALSE);
  371.  
  372.     /* starting from the first screen */
  373.     sp = first_screen;
  374.  
  375.     /* scan all the screens */
  376.     while (sp) {
  377.  
  378.         /* construct the screen name */
  379.         strcpy(line, sp->s_screen_name);
  380.         strcat(line, "                ");
  381.         line[15] = 0;
  382.  
  383.         /* list this screens windows's buffer names */
  384.         wp = sp->s_first_window;
  385.         while (wp) {
  386.  
  387.             /* grab this window's buffer name */
  388.             strcpy(bname, wp->w_bufp->b_bname);
  389.  
  390.             /* handle full lines */
  391.             if (strlen(line) + strlen(bname) + 1 > 78) {
  392.                 if (addline(slistp, line) == FALSE)
  393.                     return(FALSE);
  394.                 strcpy(line, "               ");
  395.             }
  396.  
  397.             /* append this buffer name */
  398.             if (strlen(line) > 15)
  399.                 strcat(line, " ");
  400.             strcat(line, bname);
  401.  
  402.             /* on to the next window */
  403.             wp = wp->w_wndp;
  404.         }
  405.  
  406.         /* and add the line to the buffer */
  407.         if (addline(slistp, line) == FALSE)
  408.             return(FALSE);
  409.  
  410.         /* on to the next screen */
  411.         sp = sp->s_next_screen;
  412.     }
  413.  
  414.     /* all constructed! */
  415.     return(TRUE);
  416. }
  417.  
  418.  
  419.