home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / vile-src.zip / vile-8.1 / api.c < prev    next >
C/C++ Source or Header  |  1998-07-01  |  19KB  |  851 lines

  1. /*
  2.  * api.c -- (roughly) nvi's api to perl and tcl
  3.  *
  4.  * Status of this file:  Some of the functions in this file are unused.
  5.  * Early on, I was trying for compatibility with nvi's perl interface.
  6.  * Now that I'm not, I've gotten lazy and have occassionally been skipping
  7.  * this layer.
  8.  *
  9.  * Some of the code in this file is still used and is, in fact, very
  10.  * important.  There's other code which may simply be removed.
  11.  *
  12.  * OTOH, if someone ever does a TCL interface (it probably won't
  13.  * be me -- I really like Perl and would probably lack motivation to
  14.  * do a proper job of it), some of the as now unused stuff might come
  15.  * in handy.
  16.  *                - kev 4/7/1998
  17.  */
  18.  
  19. #include "estruct.h"
  20. #include "edef.h"
  21.  
  22. #if OPT_PERL
  23.  
  24. #include "api.h"
  25.  
  26. extern REGION *haveregion;
  27.  
  28. WINDOW *curwp_visible;
  29.  
  30. static void propagate_dot(void);
  31.  
  32. /* Maybe this should go in line.c ? */
  33. static int
  34. linsert_chars(char *s, int len)
  35. {
  36. #if 0
  37.     int nlcount = 0;
  38.     while (len-- > 0) {
  39.     if (*s == '\n') {
  40.         lnewline();
  41.         nlcount++;
  42.     }
  43.     else
  44.         linsert(1, *s);
  45.     s++;
  46.     }
  47.     return nlcount;
  48. #else
  49.     /* I wrote the code this way both for efficiency reasons and
  50.        so that the MARK denoting the end of the range wouldn't
  51.        be moved to one of the newly inserted lines.
  52.     */
  53.     int nlcount = 0;
  54.     int nlatend = 0;
  55.     int nlsuppress = 0;
  56.     int mdnewline = b_val(curbp, MDNEWLINE);
  57.  
  58.     if (len <= 0)
  59.     return 0;
  60.  
  61.     if (DOT.l == buf_head(curbp)) {
  62.     if (!mdnewline) {
  63.         DOT.l = lback(DOT.l);
  64.         DOT.o = llength(DOT.l);
  65.     }
  66.     if (s[len-1] == '\n') {
  67.         if (len > 1)
  68.         nlsuppress = TRUE;
  69.         if (!mdnewline) {
  70.         make_local_b_val(curbp, MDNEWLINE);
  71.         set_b_val(curbp, MDNEWLINE, TRUE);
  72.         }
  73.     }
  74.     else {
  75.         if (mdnewline) {
  76.         make_local_b_val(curbp, MDNEWLINE);
  77.         set_b_val(curbp, MDNEWLINE, FALSE);
  78.         }
  79.     }
  80.     }
  81.  
  82.     if (s[len-1] == '\n') {
  83.     if (!mdnewline
  84.         && lforw(DOT.l) == buf_head(curbp)
  85.         && DOT.o == llength(DOT.l))
  86.     {
  87.         nlsuppress = TRUE;
  88.         make_local_b_val(curbp, MDNEWLINE);
  89.         set_b_val(curbp, MDNEWLINE, TRUE);
  90.     }
  91.     if (!nlsuppress) {
  92.  
  93.         /* We implicitly get a newline by inserting anything at
  94.            the head of a buffer (which includes the empty buffer
  95.            case).  So if we actually insert a newline, we'll end
  96.            up getting two of them.  (Which is something we don't
  97.            want unless MDNEWLINE is TRUE.) */
  98.  
  99.         lnewline();
  100.         nlcount++;
  101.         DOT.l = lback(DOT.l);        /* back up DOT to the newly */
  102.         DOT.o = llength(DOT.l);        /* inserted line */
  103.     }
  104.     nlatend = 1;
  105.     len--;
  106.     }
  107.  
  108.     while (len > 0) {
  109.     if (*s == '\n') {
  110.         lnewline();
  111.         nlcount++;
  112.         s++;
  113.         len--;
  114.     }
  115.     else {
  116.         int i;
  117.         /* Find next newline so we can do block insert; We
  118.            start at 1, because we know the first character
  119.            can't be a newline. */
  120.         for (i = 1; i < len && s[i] != '\n'; i++)
  121.         ;
  122.         linsert(i, *s);
  123.         if (i > 1) {
  124.         memcpy(DOT.l->l_text + DOT.o - i + 1, s + 1, i - 1);
  125.         }
  126.         len -= i;
  127.         s += i;
  128.     }
  129.     }
  130.  
  131.     if (nlatend) {
  132.     /* Advance DOT to where it's supposed to be */
  133.     DOT.l = lforw(DOT.l);
  134.     DOT.o = 0;
  135.     }
  136.  
  137.     return nlcount;
  138. #endif
  139. }
  140.  
  141. /* Another candidate for line.c */
  142. static int
  143. lreplace(char *s, int len)
  144. {
  145.     LINE *lp = DOT.l;
  146.     int i;
  147.     char *t;
  148.     WINDOW *wp;
  149.  
  150.     copy_for_undo(lp);
  151.  
  152.     DOT.o = 0;
  153.  
  154.     if (len > lp->l_size) {
  155.     int nlen;
  156.     char *ntext;
  157.  
  158. #define roundlenup(n) ((n+NBLOCK-1) & ~(NBLOCK-1))
  159.  
  160.     nlen = roundlenup(len);
  161.     ntext = castalloc(char, nlen);
  162.     if (ntext == 0)
  163.         return FALSE;
  164.     if (lp->l_text)
  165.         ltextfree(lp, curbp);
  166.     lp->l_text = ntext;
  167.     lp->l_size = nlen;
  168.     }
  169.  
  170.     lp->l_used = len;
  171.  
  172.     /* FIXME: Handle embedded newlines */
  173.     for (i=len-1, t=lp->l_text; i >=0; i--)
  174.     t[i] = s[i];
  175.  
  176.     /* We'd normally need a call to chg_buff here, but I don't want
  177.        to pay the price.
  178.  
  179.        BUT...
  180.  
  181.        We do call chg_buff before returning to vile.  See
  182.        api_command_cleanup below.
  183.      */
  184.  
  185.     for_each_window(wp) {
  186.     if (wp->w_dot.l == lp && wp->w_dot.o > len)
  187.         wp->w_dot.o = len;
  188.     if (wp->w_lastdot.l == lp && wp->w_lastdot.o > len)
  189.         wp->w_lastdot.o = len;
  190.     }
  191.     do_mark_iterate(mp,
  192.             if (mp->l == lp && mp->o > len)
  193.             mp->o = len;
  194.     );
  195.  
  196.     return TRUE;
  197. }
  198.  
  199. void
  200. api_setup_fake_win(VileBuf *vbp, int do_delete)
  201. {
  202.     if (curwp_visible == 0)
  203.     curwp_visible = curwp;
  204.  
  205.     if (vbp->fwp) {
  206.     curwp = vbp->fwp;
  207.     }
  208.     else {
  209.     (void) push_fake_win(vbp->bp);
  210.     vbp->fwp = curwp;
  211.     vbp->changed = 0;
  212.     }
  213.  
  214.     /* Should we call make_current() for this? */
  215.     curbp = curwp->w_bufp;
  216.  
  217.     if (vbp->ndel > 0 && do_delete) {
  218.     int status;
  219.     /* Do lazy delete; FALSE means don't put text in kill buffer */
  220.     status = ldelete(vbp->ndel, FALSE);
  221.     vbp->ndel = 0;
  222.     if ( status == FALSE
  223.       || (lforw(DOT.l) == buf_head(curbp) && DOT.o >= llength(DOT.l))) {
  224.         make_local_b_val(curbp, MDNEWLINE);
  225.         set_b_val(curbp, MDNEWLINE, FALSE);
  226.     }
  227.     }
  228. }
  229.  
  230. /* I considered three possible solutions for preventing stale
  231.    regions from being used.  Here they are:
  232.  
  233.     1) Validate the region in question.  I.e, make sure that
  234.        the region start and end lines are still in the buffer.
  235.        Set the region to reasonable defaults if not.
  236.  
  237.        The problem with this approach is that we need something
  238.        else to hold the end marker of the region while we're
  239.        working on it.  One possible solution is to use the
  240.        per-buffer w_lastdot field.  But I'm not entirely sure
  241.        this is safe.
  242.  
  243.     2) Make the region and attributed region and put it on
  244.        the attribute list.  The marks representing the
  245.        ends of the region will certainly be updated correctly
  246.        if this is done.  The downside is that we'd have to
  247.        beef up the code which deals with attributes to allow
  248.        external pointers to the attributes.  It wouldn't
  249.        do for one of the attribute lists to be freed up with
  250.        one of our VileBuf structures still pointer to one of the
  251.        structures.
  252.  
  253.     3) Make line.c and undo.c aware of the VileBuf regions.
  254.        This solution has no serious downside aside from
  255.        further bulking up the files in question.
  256.  
  257.     I chose option 3 since I was able to bury the work
  258.     inside do_mark_iterate (see estruct.h).  The following
  259.     function (api_mark_iterator) is the helper function
  260.     found in do_mark_iterate.
  261. */
  262.  
  263. MARK *
  264. api_mark_iterator(BUFFER *bp, int *iterp)
  265. {
  266.     MARK *mp = NULL;
  267.     VileBuf  *vbp = bp2vbp(bp);
  268.  
  269.     if (vbp != NULL) {
  270.     switch (*iterp) {
  271.         case 0:
  272.         mp = &vbp->region.r_orig;
  273.         break;
  274.         case 1:
  275.         mp = &vbp->region.r_end;
  276.         break;
  277.         default:
  278.         break;
  279.     }
  280.     (*iterp)++;
  281.     }
  282.  
  283.     return mp;
  284. }
  285.  
  286. /*
  287.  * This is a variant of gotoline in basic.c.  It differs in that
  288.  * it attempts to use the line number information to more efficiently
  289.  * find the line in question.  It will also position DOT at the beginning
  290.  * of the line.
  291.  *
  292.  */
  293. int
  294. api_gotoline(VileBuf *vbp, int lno)
  295. {
  296. #if !SMALLER
  297.     int count;
  298.     LINE *lp;
  299.     BUFFER *bp = vbp->bp;
  300.  
  301.     if (!b_is_counted(bp))
  302.     bsizes(bp);
  303.  
  304.     count = lno - DOT.l->l_number;
  305.     lp = DOT.l;
  306.  
  307.     while (count < 0 && lp != buf_head(bp)) {
  308.     lp = lback(lp);
  309.     count++;
  310.     }
  311.     while (count > 0) {
  312.     lp = lforw(lp);
  313.     count--;
  314.     if (lp == buf_head(bp))
  315.         break;
  316.     }
  317.  
  318.     DOT.o = 0;
  319.  
  320.     if (lp != buf_head(bp) && lno == lp->l_number) {
  321.     DOT.l = lp;
  322.     return TRUE;
  323.     }
  324.     else {
  325.     DOT.l = buf_head(bp);
  326.     return FALSE;
  327.     }
  328.  
  329. #else
  330.     return gotoline(TRUE, lno);
  331. #endif
  332. }
  333.  
  334. int
  335. api_aline(VileBuf *vbp, int lno, char *line, int len)
  336. {
  337.     api_setup_fake_win(vbp, TRUE);
  338.  
  339.     if (lno >= 0 && lno < line_count(vbp->bp)) {
  340.     api_gotoline(vbp, lno+1);
  341.     linsert_chars(line, len);
  342.     lnewline();
  343.     }
  344.     else {    /* append to the end */
  345.     gotoline(FALSE, 0);
  346.     gotoeol(FALSE, 0);
  347.     lnewline();
  348.     linsert_chars(line, len);
  349.     }
  350.  
  351.     return TRUE;
  352. }
  353.  
  354. int
  355. api_dotinsert(VileBuf *vbp, char *text, int len) {
  356.  
  357.     /* Set up the fake window; but we'll do any pending deletes
  358.        ourselves. */
  359.     api_setup_fake_win(vbp, FALSE);
  360.  
  361.     /* FIXME: Check to see if the buffer needs to be modified at all.
  362.        We'll save our undo space better this way.  We'll also be able
  363.        to better preserve the user's marks. */
  364.  
  365.     linsert_chars(text, len);
  366.     if (vbp->ndel) {
  367.     int status;
  368.     status = ldelete(vbp->ndel, FALSE);
  369.     vbp->ndel = 0;
  370.     if (status == FALSE
  371.      || (lforw(DOT.l) == buf_head(curbp) && DOT.o >= llength(DOT.l))) {
  372.         make_local_b_val(curbp, MDNEWLINE);
  373.         set_b_val(curbp, MDNEWLINE, FALSE);
  374.     }
  375.     }
  376.     return TRUE;
  377. }
  378.  
  379. int
  380. api_dline(VileBuf *vbp, int lno)
  381. {
  382.     int status = TRUE;
  383.  
  384.     api_setup_fake_win(vbp, TRUE);
  385.  
  386.     if (lno > 0 && lno <= line_count(vbp->bp)) {
  387.     api_gotoline(vbp, lno);
  388.     gotobol(TRUE,TRUE);
  389.     status = ldelete(llength(DOT.l) + 1, FALSE);
  390.     }
  391.     else
  392.     status = FALSE;
  393.  
  394.     return status;
  395. }
  396.  
  397. int
  398. api_gline(VileBuf *vbp, int lno, char **linep, int *lenp)
  399. {
  400.     int status = TRUE;
  401.  
  402.     api_setup_fake_win(vbp, TRUE);
  403.  
  404.     if (lno > 0 && lno <= line_count(vbp->bp)) {
  405.     api_gotoline(vbp, lno);
  406.     *linep = DOT.l->l_text;
  407.     *lenp = llength(DOT.l);
  408.     if (*lenp == 0) {
  409.         *linep = "";    /* Make sure we pass back a zero length,
  410.                                null terminated value when the length
  411.                    is zero.  Otherwise perl gets confused.
  412.                    (It thinks it should calculate the length
  413.                    when given a zero length.)
  414.                  */
  415.     }
  416.     }
  417.     else
  418.     status = FALSE;
  419.  
  420.     return status;
  421. }
  422.  
  423. #if 0        /* Not used. */
  424. int
  425. api_dotgline(VileBuf *vbp, char **linep, int *lenp, int *neednewline)
  426. {
  427.  
  428.     api_setup_fake_win(vbp, TRUE);
  429.     if (!vbp->dot_inited) {
  430.     DOT = vbp->region.r_orig;    /* set DOT to beginning of region */
  431.     vbp->dot_inited = 1;
  432.     }
  433.  
  434.     /* FIXME: Handle rectangular regions. */
  435.  
  436.     if (   is_header_line(DOT, curbp)
  437.         || (   DOT.l == vbp->region.r_end.l
  438.         && (   vbp->regionshape == FULLLINE
  439.             || (   vbp->regionshape == EXACT
  440.             && DOT.o >= vbp->region.r_end.o))))
  441.     {
  442.     return FALSE;
  443.     }
  444.  
  445.     *linep = DOT.l->l_text + DOT.o;
  446.     *lenp = llength(DOT.l) - DOT.o;
  447.  
  448.     if (vbp->regionshape == EXACT && DOT.l == vbp->region.r_end.l) {
  449.     *lenp -= llength(DOT.l) - vbp->region.r_end.o;
  450.     }
  451.  
  452.     if (*lenp < 0)
  453.     *lenp = 0;    /* Make sure return length is non-negative */
  454.  
  455.     if (*lenp == 0) {
  456.     *linep = "";    /* Make sure we pass back a zero length,
  457.                    null terminated value when the length
  458.                    is zero.  Otherwise perl gets confused.
  459.                    (It thinks it should calculate the length
  460.                    when given a zero length.)
  461.                  */
  462.     }
  463.  
  464.     if (vbp->inplace_edit) {
  465.     if (vbp->regionshape == EXACT && DOT.l == vbp->region.r_end.l) {
  466.         vbp->ndel = *lenp;
  467.         *neednewline = 0;
  468.     }
  469.     else {
  470.         vbp->ndel = *lenp + 1;
  471.         *neednewline = 1;
  472.     }
  473.     }
  474.     else {
  475.     if (vbp->regionshape == EXACT && DOT.l == vbp->region.r_end.l) {
  476.         DOT.o += *lenp;
  477.         *neednewline = 0;
  478.     }
  479.     else {
  480.         DOT.l = lforw(DOT.l);
  481.         DOT.o = 0;
  482.         *neednewline = 1;
  483.     }
  484.     }
  485.     return TRUE;
  486. }
  487. #endif    /* Not used */
  488.  
  489. int
  490. api_sline(VileBuf *vbp, int lno, char *line, int len)
  491. {
  492.     int status = TRUE;
  493.  
  494.     api_setup_fake_win(vbp, TRUE);
  495.  
  496.     if (lno > 0 && lno <= line_count(vbp->bp)) {
  497.     api_gotoline(vbp, lno);
  498.     if (   DOT.l->l_text != line
  499.         && (   llength(DOT.l) != len
  500.             || memcmp(line, DOT.l->l_text, len) != 0)) {
  501.         lreplace(line, len);
  502.         vbp->changed = 1;
  503.     }
  504.     }
  505.     else
  506.     status = FALSE;
  507.  
  508.     return status;
  509. }
  510.  
  511. int
  512. api_iline(VileBuf *vbp, int lno, char *line, int len)
  513. {
  514.     return api_aline(vbp, lno-1, line, len);
  515. }
  516.  
  517. int
  518. api_lline(VileBuf *vbp, int *lnop)
  519. {
  520.     *lnop = line_count(vbp->bp);
  521.     return TRUE;
  522. }
  523.  
  524. VileBuf *
  525. api_fscreen(char *name)
  526. {
  527.     BUFFER *bp;
  528.  
  529.     bp = find_b_file(name);
  530.  
  531.     if (bp)
  532.     return api_bp2vbp(bp);
  533.     else
  534.     return 0;
  535. }
  536.  
  537. int
  538. api_delregion(VileBuf *vbp)
  539. {
  540.  
  541.     api_setup_fake_win(vbp, TRUE);
  542.  
  543.     haveregion = NULL;
  544.     DOT = vbp->region.r_orig;
  545.     MK  = vbp->region.r_end;
  546.     regionshape = vbp->regionshape;
  547.  
  548.     if (vbp->regionshape == FULLLINE) {
  549.     MK.l = lback(MK.l);
  550.     }
  551.  
  552.     return killregion();
  553. }
  554.  
  555. int
  556. api_motion(VileBuf *vbp, char *mstr)
  557. {
  558.     const CMDFUNC *cfp;
  559.     char *mp;
  560.     int   c, f, n, s;
  561.     int status;
  562.     int saved_clexec, saved_disinp, saved_discmd, saved_isnamedcmd;
  563.  
  564.     if (mstr == NULL)
  565.     return FALSE;
  566.  
  567.     status  = TRUE;
  568.  
  569.     saved_clexec     = clexec;
  570.     saved_discmd     = discmd;
  571.     saved_disinp     = disinp;
  572.     saved_isnamedcmd = isnamedcmd;
  573.  
  574.     clexec     = FALSE;        /* Not executing a command line */
  575.     discmd     = FALSE;        /* Don't display commands / arg counts */
  576.     disinp     = FALSE;        /* Don't display input */
  577.     isnamedcmd = FALSE;        /* Not a named command */
  578.  
  579.  
  580.     api_setup_fake_win(vbp, TRUE);
  581.  
  582.     mp = mstr + strlen(mstr);
  583.  
  584.     mapungetc(abortc | NOREMAP);
  585.     /* Should we allow remapping?  Seems to me like it introduces too
  586.        many variables. */
  587.     while (mp-- > mstr) {
  588.     mapungetc(*mp | NOREMAP);
  589.     }
  590.  
  591.     while (mapped_ungotc_avail()) {
  592.  
  593.     /* Get the character */
  594.     c = kbd_seq();
  595.  
  596.     if (ABORTED(c)) {
  597.         if (mapped_ungotc_avail()) {
  598.         /* Not our abortc */
  599.         while (mapped_ungotc_avail())
  600.             (void) kbd_seq();
  601.         status = FALSE;
  602.         break;
  603.         }
  604.         else
  605.         break;            /* okay, it's ours */
  606.     }
  607.  
  608.     f = FALSE;
  609.     n = 1;
  610.  
  611.     do_repeats(&c,&f,&n);
  612.  
  613.     /* and execute the command */
  614.     cfp = kcod2fnc(c);
  615.     if ( (cfp != NULL) && ((cfp->c_flags & MOTION) != 0))
  616.         s = execute(cfp, f, n);
  617.     else {
  618.         while (mapped_ungotc_avail())
  619.         (void) kbd_seq();
  620.         status = FALSE;
  621.         break;
  622.     }
  623.     }
  624.  
  625.     clexec     = saved_clexec;
  626.     discmd     = saved_discmd;
  627.     disinp     = saved_disinp;
  628.     isnamedcmd = saved_isnamedcmd;
  629.  
  630.     return status;
  631. }
  632.  
  633. int
  634. api_edit(char *fname, VileBuf **retvbpp)
  635. {
  636.     BUFFER *bp;
  637.     if (fname == NULL) {
  638.     char bufname[NBUFN];
  639.     static int unnamed_cnt = 0;
  640.     sprintf(bufname, "[unnamed-%d]", ++unnamed_cnt);
  641.     bp = bfind(bufname, 0);
  642.     bp->b_active = TRUE;
  643.     }
  644.     else {
  645.     bp = getfile2bp(fname, FALSE, FALSE);
  646.     if (bp == 0) {
  647.         *retvbpp = 0;
  648.         return 1;
  649.     }
  650.     }
  651.  
  652.     *retvbpp = api_bp2vbp(bp);
  653.     api_setup_fake_win(*retvbpp, TRUE);
  654.     return !swbuffer_lfl(bp, FALSE);
  655. }
  656.  
  657. int
  658. api_swscreen(VileBuf *oldsp, VileBuf *newsp)
  659. {
  660.     /*
  661.      * Calling api_command_cleanup nukes various state, like DOT
  662.      * Now if DOT got propogated, all is (likely) well.  But if it didn't,
  663.      * then DOT will likely be in the wrong place if the executing perl
  664.      * script expects to access a buffer on either side of a switchscreen
  665.      * call.
  666.      *
  667.      * I see two different solutions for this.  1) Maintain a copy of
  668.      * dot in the VileBuf structure. 2) Don't destroy the fake windows by
  669.      * popping them off.  Which means that we either teach the rest
  670.      * of vile about fake windows (scary) or we temporarily unlink the
  671.      * fake windows from the buffer list.
  672.      *
  673.      * I'm in favor of temporarily unlinking the fake windows from
  674.      * the buffer list.  The only problem with this is that the unlinked
  675.      * window's version of DOT is not iterated over in do_mark_iterate.
  676.      * (Probably won't be a problem that often, but we need to account
  677.      * for it.)
  678.      *
  679.      * So... I guess we could maintain a separate structure which
  680.      * has the unlinked window pointers.  And then api_mark_iterate
  681.      * could walk these.  (Yuck.)
  682.      *
  683.      * Anyhow, there's a lot of issues here requiring careful
  684.      * consideration.  Which is why I haven't already done it.
  685.      *        - kev 4/3/1998
  686.      *
  687.      * Okay, I put in the stuff for detaching and reattaching fake
  688.      * windows, but the above noted caveats still apply.
  689.      *        - kev 4/18/1998
  690.      *
  691.      * I decided that the fake window stuff was too much of a hack
  692.      * So I blew it away.  The rest of vile has limited knowledge
  693.      * of fake windows now where it needs it.  This knowledge is
  694.      * buried in the macro "for_each_visible_window".
  695.      *        - kev 4/20/1998
  696.      */
  697.  
  698.     curwp = curwp_visible ? curwp_visible : curwp;
  699.     curbp = curwp->w_bufp;
  700.  
  701.     if (oldsp)
  702.     swbuffer(vbp2bp(oldsp));
  703.     swbuffer(vbp2bp(newsp));
  704.  
  705.     curwp_visible = curwp;
  706.  
  707.     return TRUE;
  708. }
  709.  
  710. /* Causes the screen(s) to be updated */
  711. void
  712. api_update(void)
  713. {
  714.     propagate_dot();
  715.  
  716.     curwp = curwp_visible ? curwp_visible : curwp;
  717.     curbp = curwp->w_bufp;
  718.  
  719.     update(TRUE);
  720.  
  721.     curwp_visible = curwp;
  722. }
  723.  
  724. /*
  725.  * The following are not in the nvi API. But I needed them in order to
  726.  * do an efficient implementation for vile.
  727.  */
  728.  
  729.  
  730.  
  731. /* Propagate DOT for the fake windows that need it;
  732.    Also do any outstanding (deferred) deletes. */
  733. static void
  734. propagate_dot(void)
  735. {
  736.     WINDOW *wp;
  737.  
  738.     for_each_window(wp) {
  739.     if (!is_fake_window(wp)) {
  740.         /* We happen to know that the fake windows will always
  741.            be first in the buffer list.  So we exit the loop
  742.            when we hit one that isn't fake. */
  743.         break;
  744.     }
  745.  
  746.     /* Do outstanding delete (if any) */
  747.     api_setup_fake_win(bp2vbp(wp->w_bufp), TRUE);
  748.  
  749.     if (bp2vbp(wp->w_bufp)->dot_changed) {
  750.         if (curwp_visible && curwp_visible->w_bufp == wp->w_bufp) {
  751.         curwp_visible->w_dot = wp->w_dot;
  752.         curwp_visible->w_flag |= WFHARD;
  753.         }
  754.         else {
  755.         int found = 0;
  756.         WINDOW *pwp;
  757.         for_each_window(pwp) {
  758.             if (wp->w_bufp == pwp->w_bufp
  759.                 && !is_fake_window(pwp))
  760.             {
  761.             found = 1;
  762.             pwp->w_dot = wp->w_dot;
  763.             pwp->w_flag |= WFHARD;
  764.             break;        /* out of inner for_each_window */
  765.             }
  766.         }
  767.         if (!found) {
  768.             /* No window to propagate DOT in, so just use the
  769.                buffer's traits */
  770.             wp->w_bufp->b_dot = wp->w_dot;
  771.         }
  772.         }
  773.     }
  774.     }
  775. }
  776.  
  777. void
  778. api_command_cleanup(void)
  779. {
  780.     BUFFER *bp;
  781.  
  782.     if (curwp_visible == 0)
  783.     curwp_visible = curwp;
  784.  
  785.     /* Propgate dot to the visible windows and do any deferred deletes */
  786.     propagate_dot();
  787.  
  788.     /* Pop the fake windows */
  789.  
  790.     while ((bp = pop_fake_win(curwp_visible)) != NULL) {
  791.     if (bp2vbp(bp) != NULL)
  792.         bp2vbp(bp)->fwp = 0;
  793.         bp2vbp(bp)->dot_inited = 0;        /* for next time */
  794.         bp2vbp(bp)->dot_changed = 0;        /* ditto */
  795.         if (bp2vbp(bp)->changed) {
  796.         chg_buff(bp, WFHARD);
  797.         bp2vbp(bp)->changed = 0;
  798.         }
  799.     }
  800.  
  801.     curwp_visible = 0;
  802.  
  803.     if (curbp != curwp->w_bufp)
  804.     make_current(curwp->w_bufp);
  805.  
  806.     MK = DOT;            /* make sure MK is in same buffer as DOT */
  807. }
  808.  
  809. void
  810. api_free_private(void *vsp)
  811. {
  812.     VileBuf *vbp = (VileBuf *) vsp;
  813.  
  814.     if (vbp) {
  815.     vbp->bp->b_api_private = 0;
  816. #if OPT_PERL
  817.     perl_free_handle(vbp->perl_handle);
  818. #endif
  819.     free(vbp);
  820.     }
  821. }
  822.  
  823. /* Given a buffer pointer, returns a pointer to a VileBuf structure,
  824.  * creating it if necessary.
  825.  */
  826. VileBuf *
  827. api_bp2vbp(BUFFER *bp)
  828. {
  829.     VileBuf *vbp;
  830.  
  831.     vbp = bp2vbp(bp);
  832.     if (vbp == 0) {
  833.     vbp = typecalloc(VileBuf);
  834.     if (vbp != 0) {
  835.         bp->b_api_private = vbp;
  836.         vbp->bp = bp;
  837.         vbp->regionshape = FULLLINE;
  838.         vbp->region.r_orig.l =
  839.         vbp->region.r_end.l  = buf_head(bp);
  840.         vbp->region.r_orig.o =
  841.         vbp->region.r_end.o  = 0;
  842.         vbp->dot_inited = 0;
  843.         vbp->dot_changed = 0;
  844.         vbp->ndel = 0;
  845.     }
  846.     }
  847.     return vbp;
  848. }
  849.  
  850. #endif
  851.