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