home *** CD-ROM | disk | FTP | other *** search
/ Dream 49 / Amiga_Dream_49.iso / atari / texte / qed-397src.lzh / UMBRUCH.C < prev    next >
C/C++ Source or Header  |  1996-12-29  |  11KB  |  449 lines

  1. #include "global.h"
  2. #include "ausgabe.h"
  3. #include "block.h"
  4. #include "clipbrd.h"
  5. #include "edit.h"
  6. #include "scroll.h"
  7. #include "set.h"
  8. #include "tasten.h"
  9. #include "text.h"
  10. #include "windows.h"
  11. #include "umbruch.h"
  12.  
  13. /* Muessen am Anfang jeden Umbruchs gesetzt werden */
  14. LOCAL BOOLEAN    tab;
  15. LOCAL WORD        tab_size, lineal_len;
  16. LOCAL SET        umbruch_set;
  17.  
  18. LOCAL BOOLEAN Absatz        (LINEP col);
  19. LOCAL BOOLEAN too_short    (TEXTP t_ptr, LINEP col, LONG y);
  20. LOCAL BOOLEAN too_long     (TEXTP t_ptr, LINEP col, LONG y);
  21.  
  22. VOID save_absatz(TEXTP t_ptr)
  23. /* Es werden LZ und TABs am Absatzende gelöscht */
  24. /* und im Absatz hinzugefügt                            */
  25. {
  26.     BOOLEAN    action = FALSE;
  27.     WORD    i;
  28.     UBYTE    c;
  29.     LINEP    lauf;
  30.  
  31.     setcpy(umbruch_set,t_ptr->loc_opt->umbruch_set);/* !! Mu₧ gesetzt werden !! */
  32.     setincl(umbruch_set,' ');                                /* wenigstens das */
  33.     lauf = FIRST(&t_ptr->text);
  34.     while (!IS_TAIL(lauf))
  35.     {
  36.         if (lauf->info&ABSATZ)        /* Absatz => LZ und TAB löschen */
  37.         {
  38.             for (i=lauf->len; (--i)>=0 ; )
  39.             {
  40.                 c = TEXT(lauf)[i];
  41.                 if (c!=' ' && c!='\t') break;
  42.             }
  43.             i++;
  44.             if (i<lauf->len)        /* Zeile verkürzen */
  45.             {
  46.                 REALLOC(&lauf,i,i-lauf->len);
  47.                 action = TRUE;
  48.             }
  49.         }
  50.         else                                /* Nicht Absatz => LZ anhängen */
  51.         {
  52.             c = TEXT(lauf)[lauf->len-1];
  53.             if (!setin(umbruch_set,c) && lauf->len<MAX_LINE_LEN)
  54.             {
  55.                 *REALLOC(&lauf,lauf->len,1) = ' ';
  56.                 action = TRUE;
  57.             }
  58.         }
  59.         NEXT(lauf);
  60.     }
  61.     if (action)        /* Es wurde etwas verändert */
  62.     {
  63.         make_chg(t_ptr->link,TOTAL_CHANGE,0);
  64.         t_ptr->moved++;
  65.         lauf = t_ptr->cursor_line = get_line(&t_ptr->text,t_ptr->ypos);
  66.         if (t_ptr->xpos>lauf->len)
  67.         {
  68.             t_ptr->xpos = lauf->len;
  69.             make_chg(t_ptr->link,POS_CHANGE,0);
  70.         }
  71.     }
  72. }
  73.  
  74. LOCAL BOOLEAN Absatz(LINEP col)
  75. /* TRUE : Die Zeile ist die letzte Zeile eines Absatz */
  76. /* FALSE : sonst                                                    */
  77. {
  78.     WORD    col_len = col->len;
  79.     UBYTE    c = TEXT(col)[col_len-1];
  80.  
  81.     return (col_len==0 || IS_LAST(col) || !setin(umbruch_set,c));
  82. }
  83.  
  84. VOID make_absatz(TEXTP t_ptr)
  85. /* Absatzmarkierungen anbringen oder löschen */
  86. {
  87.     LINEP lauf;
  88.  
  89.     setcpy(umbruch_set,t_ptr->loc_opt->umbruch_set);/* !! Mu₧ gesetzt werden !! */
  90.     setincl(umbruch_set,' ');                                /* wenigstens das */
  91.     lauf = FIRST(&t_ptr->text);
  92.     if (t_ptr->loc_opt->umbrechen)
  93.         while (!IS_TAIL(lauf))
  94.         {
  95.             if (Absatz(lauf))
  96.                 lauf->info |= ABSATZ;
  97.             else
  98.                 lauf->info &= (~ABSATZ);        /* z.B. CR setzt immer Bit */
  99.             NEXT(lauf);
  100.         }
  101.     else
  102.         while (!IS_TAIL(lauf))
  103.         {
  104.             lauf->info &= (~ABSATZ);
  105.             NEXT(lauf);
  106.         }
  107. }
  108.  
  109. LOCAL WORD long_brk(LINEP col)
  110. /* Zeile ist zu lang. Wo soll sie abgebrochen werden (mind. ein Wort) */
  111. {
  112.     WORD    off, pos;
  113.     UBYTE    c, *str;
  114.  
  115.     off = col_offset(col);
  116.     pos = InterPos(lineal_len,col,tab,tab_size);
  117.     str = TEXT(col)+pos;
  118.     pos--;
  119.     c = *(--str);
  120.    if (!setin(umbruch_set,c))
  121.         while (pos>off && setin(umbruch_set,c))                    /* Wortende suchen */
  122.         {
  123.             pos--;
  124.             c = *(--str);
  125.         }
  126.     while (pos>off && !setin(umbruch_set,c))                        /* Wortanfang suchen */
  127.     {
  128.         pos--;
  129.         c = *(--str);
  130.     }
  131.     if (pos<=off)                                                            /* nach rechts */
  132.     {
  133.         pos = InterPos(lineal_len,col,tab,tab_size);
  134.         str = TEXT(col)+pos;
  135.         c = *str++;
  136.         while (pos<=col->len && setin(umbruch_set,c))            /* Wortanf suchen */
  137.         {
  138.             pos++;
  139.             c = *str++;
  140.         }
  141.         while (pos<=col->len && !setin(umbruch_set,c))            /* Wortende suchen */
  142.         {
  143.             pos++;
  144.             c = *str++;
  145.         }
  146.         while (pos<=col->len && setin(umbruch_set,c))            /* Wortanf suchen */
  147.         {
  148.             pos++;
  149.             c = *str++;
  150.         }
  151.         if (pos>=col->len)
  152.             return 0;                                                        /* nichts machen */
  153.     }
  154.     else
  155.         pos++;
  156.     return pos;
  157. }
  158.  
  159. LOCAL WORD short_brk(LINEP col, WORD len)
  160. /* Einer Zeile fehlen Zeichen, sie ist jetzt im Bild len lang */
  161. /* Wo soll der Nachfolger (col) hochgezogen werden (mind. ein Wort) */
  162. {
  163.     UBYTE    *str, c;
  164.     WORD    pos, merk_pos;
  165.  
  166.     pos = col_offset(col);
  167.     str = TEXT(col)+pos;
  168.     merk_pos = -1;
  169.     if (!tab)
  170.     {
  171.         while (TRUE)
  172.         {
  173.             if (pos>=col->len) return col->len;                    /* ganze Zeile */
  174.             c = *str++;
  175.             pos++;
  176.             len++;
  177.             if (len>lineal_len) break;                                /* jetzt reichts */
  178.             if (setin(umbruch_set,c))
  179.                 merk_pos = pos;
  180.         }
  181.     }
  182.     else
  183.     {
  184.         WORD tabH;
  185.  
  186.         tabH = tab_size-(len%tab_size);
  187.         while (TRUE)
  188.         {
  189.             if (pos>=col->len) return col->len;                    /* ganze Zeile */
  190.             c = *str++;
  191.             pos++;
  192.             if (c=='\t')
  193.             {
  194.                 len += tabH;
  195.                 tabH = tab_size;
  196.             }
  197.             else
  198.             {
  199.                 len++;
  200.                 if ((--tabH)==0) tabH = tab_size;
  201.             }
  202.             if (len>lineal_len) break;                                /* jetzt reichts */
  203.             if (setin(umbruch_set,c))
  204.                 merk_pos = pos;
  205.         }
  206.     }
  207.     if (merk_pos>0) pos = merk_pos;
  208.     else pos = 0;                                                        /* nichts zu machen */
  209.     return(pos);
  210. }
  211.  
  212. /* !!! cursor_line mu₧ hinterher entsprechend ypos gesetzt werden !!! */
  213. LOCAL BOOLEAN too_long(TEXTP t_ptr, LINEP col, LONG y)
  214. {
  215.     WORD    i, len, off;
  216.     BOOLEAN    absatz, weiter, changed;
  217.  
  218.     changed = FALSE;
  219.     weiter = FALSE;
  220.     while (BildLen(col,t_ptr->loc_opt->tab,t_ptr->loc_opt->tabsize)>lineal_len)                            /* Zeile zu lang */
  221.     {
  222.         i = long_brk(col);                                        /* wo abbrechen */
  223.         if (i==0)
  224.         {
  225.             weiter = FALSE;
  226.             break;
  227.         }
  228.         absatz = col->info&ABSATZ;
  229.         len = col->len-i;                                            /* soviel abschneiden */
  230.         if (absatz || col->nachf->len+len >= MAX_LINE_LEN)    /* col_split */
  231.         {
  232.             LINEP help;
  233.  
  234.             col_split(&col,i);
  235.             t_ptr->text.lines++;
  236.             if (t_ptr->ypos>y) t_ptr->ypos++;
  237.             help = col->nachf;
  238.             off = einrucken(&help);
  239.             if (t_ptr->ypos==y && t_ptr->xpos>i)            /* Cursor umbrechen */
  240.             {
  241.                 t_ptr->ypos++;
  242.                 t_ptr->xpos -= (i-off);
  243.             }
  244.             weiter = TRUE;
  245.         }
  246.         else                                                            /* Text rumschieben */
  247.         {
  248.             LINEP help = col->nachf;
  249.  
  250.             off = col_offset(help);
  251.             INSERT(&help,off,len,TEXT(col)+i);                /* Next verlängern */
  252.             REALLOC(&col,i,-len);                                /* Zeile kürzen */
  253.             if (t_ptr->ypos==y+1 && t_ptr->xpos>off)        /* Cursor verschieben */
  254.             {
  255.                 t_ptr->xpos += len;
  256.             }
  257.             if (t_ptr->ypos==y && t_ptr->xpos>i)            /* Cursor umbrechen */
  258.             {
  259.                 t_ptr->ypos++;
  260.                 t_ptr->xpos -= (i-off);
  261.             }
  262.             weiter = FALSE;
  263.         }
  264.         NEXT(col);                                                    /* nächste Zeile zu lang? */
  265.         y++;
  266.         changed = TRUE;
  267.     }
  268.     if (weiter) too_short(t_ptr,col,y);                        /* nächste Zeile zu kurz? */
  269.     return (changed);
  270. }
  271.  
  272. /* !!! cursor_line mu₧ hinterher entsprechend ypos gesetzt werden !!! */
  273. LOCAL BOOLEAN too_short(TEXTP t_ptr, LINEP col, LONG y)
  274. {
  275.     WORD    len, off;
  276.     LINEP    next_col;
  277.     BOOLEAN    changed;
  278.  
  279.     changed = FALSE;
  280.     tab = t_ptr->loc_opt->tab;
  281.     tab_size = t_ptr->loc_opt->tabsize;
  282.     while (BildLen(col,t_ptr->loc_opt->tab,t_ptr->loc_opt->tabsize)<lineal_len && !(col->info&ABSATZ) && col->nachf->len>0)
  283.     {
  284.         next_col = col->nachf;
  285.         len = short_brk(next_col,BildLen(col,t_ptr->loc_opt->tab,t_ptr->loc_opt->tabsize));
  286.         if (len==0) break;                                        /* nichts zu machen */
  287.         off = col_offset(next_col);
  288.         if (len==next_col->len)                                    /* ganze Zeile hochziehen */
  289.         {
  290.             if (t_ptr->ypos==y+1)                                /* Cursor hochziehen */
  291.             {
  292.                 t_ptr->ypos--;
  293.                 t_ptr->xpos += (col->len-off);
  294.             }
  295.             if (t_ptr->ypos>y+1) t_ptr->ypos--;
  296.             REALLOC(&next_col,0,-off);
  297.             col_concate(&col);
  298.             t_ptr->text.lines--;                                    /* gleiche Zeile nochmal */
  299.         }
  300.         else                                                            /* teilweise hochziehen */
  301.         {
  302.             len -= off;
  303.             if (t_ptr->ypos==y+1 && t_ptr->xpos>=off)
  304.             {
  305.                 if (t_ptr->xpos<off+len)
  306.                 {
  307.                     t_ptr->ypos--;
  308.                     t_ptr->xpos += (col->len-off);
  309.                 }
  310.                 else
  311.                     t_ptr->xpos -= len;
  312.             }
  313.             INSERT(&col,col->len,len,TEXT(next_col)+off);/* Zeile verlängern */
  314.             REALLOC(&next_col,off,-len);                        /* Zeile verkürzen */
  315.             col = next_col;                                        /* nächste Zeile weiter */
  316.             y++;
  317.         }
  318.         changed = TRUE;
  319.     }
  320.     return(changed);
  321. }
  322.  
  323. VOID umbruch(TEXTP t_ptr)
  324. {
  325.     LONG y = t_ptr->ypos;
  326.  
  327.     setcpy(umbruch_set,t_ptr->loc_opt->umbruch_set);/* !! Mu₧ gesetzt werden !! */
  328.     setincl(umbruch_set,' ');                                /* wenigstens das */
  329.     tab = t_ptr->loc_opt->tab;
  330.     tab_size = t_ptr->loc_opt->tabsize;
  331.     lineal_len = t_ptr->loc_opt->lineal_len;
  332.     if (too_long(t_ptr, t_ptr->cursor_line, t_ptr->ypos))
  333.     {
  334.         t_ptr->cursor_line = get_line(&t_ptr->text,t_ptr->ypos);
  335.         t_ptr->moved++;
  336.         make_chg(t_ptr->link,POS_CHANGE,0);
  337.         make_chg(t_ptr->link,TOTAL_CHANGE,y);
  338.         clr_undo();
  339.     }
  340.     else if (too_short(t_ptr, t_ptr->cursor_line, t_ptr->ypos))
  341.     {
  342.         t_ptr->cursor_line = get_line(&t_ptr->text,t_ptr->ypos);
  343.         t_ptr->moved++;
  344.         make_chg(t_ptr->link,POS_CHANGE,0);
  345.         make_chg(t_ptr->link,TOTAL_CHANGE,y);
  346.         clr_undo();
  347.     }
  348. }
  349.  
  350. VOID format(TEXTP t_ptr)
  351. {
  352.     LINEP    lauf;
  353.     LONG    y, start_y;
  354.     BOOLEAN    change;
  355.  
  356.     setcpy(umbruch_set,t_ptr->loc_opt->umbruch_set);/* !! Mu₧ gesetzt werden !! */
  357.     setincl(umbruch_set,' ');                                /* wenigstens das */
  358.     tab = t_ptr->loc_opt->tab;
  359.     tab_size = t_ptr->loc_opt->tabsize;
  360.     lineal_len = t_ptr->loc_opt->lineal_len;
  361.     change = FALSE;
  362.     lauf = t_ptr->cursor_line;
  363.     y = t_ptr->ypos;
  364.     if (y)
  365.     {
  366.         y--;
  367.         VORG(lauf);
  368.         while ((y>0) && (!(lauf->info&ABSATZ)))
  369.         {
  370.             y--;
  371.             VORG(lauf);
  372.         }
  373.         if (y)
  374.         {
  375.             y++;
  376.             NEXT(lauf);
  377.         }
  378.     }
  379.     /* lauf zeigt jetzt auf die erste Zeile des Absatz */
  380.     start_y = y;
  381.     lauf = lauf->vorg;    /* Einen davor, weil akt. Zeile geändert wird */
  382.     while(TRUE)
  383.     {
  384.         if (!too_long(t_ptr, lauf->nachf,y))
  385.         {
  386.             if (too_short(t_ptr, lauf->nachf,y))
  387.                 change = TRUE;
  388.         }
  389.         else
  390.             change = TRUE;
  391.         y++;
  392.         NEXT(lauf);
  393.         if (lauf->info&ABSATZ) break;
  394.     }
  395.     if (change)
  396.     {
  397.         t_ptr->cursor_line = get_line(&t_ptr->text,t_ptr->ypos);
  398.         t_ptr->moved++;
  399.         make_chg(t_ptr->link,POS_CHANGE,0);
  400.         make_chg(t_ptr->link,TOTAL_CHANGE,start_y);
  401.         clr_undo();
  402.     }
  403. }
  404.  
  405. VOID total_format(TEXTP t_ptr)
  406. {
  407.     LINEP    lauf;
  408.     LONG    y;
  409.     BOOLEAN    change;
  410.  
  411.     setcpy(umbruch_set,t_ptr->loc_opt->umbruch_set);/* !! Mu₧ gesetzt werden !! */
  412.     setincl(umbruch_set,' ');                                /* wenigstens das */
  413.     tab = t_ptr->loc_opt->tab;
  414.     tab_size = t_ptr->loc_opt->tabsize;
  415.     lineal_len = t_ptr->loc_opt->lineal_len;
  416.     change = FALSE;
  417.     Busy_mouse();
  418.     lauf = FIRST(&t_ptr->text);
  419.     y = 0;
  420.     while (!IS_TAIL(lauf))
  421.     {
  422.         /* lauf zeigt jetzt auf die erste Zeile des Absatz */
  423.         lauf = lauf->vorg;    /* Einen davor, weil akt. Zeile geändert wird */
  424.         while(TRUE)
  425.         {
  426.             if (!too_long(t_ptr, lauf->nachf,y))
  427.             {
  428.                 if (too_short(t_ptr, lauf->nachf,y))
  429.                     change = TRUE;
  430.             }
  431.             else
  432.                 change = TRUE;
  433.             y++;
  434.             NEXT(lauf);
  435.             if (lauf->info&ABSATZ) break;
  436.         }
  437.         NEXT(lauf);
  438.     }
  439.     if (change)
  440.     {
  441.         t_ptr->cursor_line = get_line(&t_ptr->text,t_ptr->ypos);
  442.         t_ptr->moved++;
  443.         make_chg(t_ptr->link,POS_CHANGE,0);
  444.         make_chg(t_ptr->link,TOTAL_CHANGE,0);
  445.         clr_undo();
  446.     }
  447.     Last_mouse();
  448. }
  449.