Annotation of libwww/LineMode/src/GridText.c, revision 1.39

1.13      frystyk     1: /*                                                                  GridText.c
                      2: **     CHARACTER GRID HYPERTEXT OBJECT
                      3: **
1.17      frystyk     4: **     (c) COPYRIGHT MIT 1995.
1.13      frystyk     5: **     Please first read the full copyright statement in the file COPYRIGH.
1.39    ! frystyk     6: **     @(#) $Id: GridText.c,v 1.38 1996/04/14 01:23:31 frystyk Exp $
1.13      frystyk     7: **
1.30      frystyk     8: **     This is the definition of the HyperDoc object and the HText interface
                      9: **     which is used in the current Library HTML parser
1.1       timbl      10: */
                     11: 
1.16      frystyk    12: #include <assert.h>
                     13: 
                     14: #include "WWWLib.h"
1.33      frystyk    15: #include "WWWCache.h"
                     16: #include "WWWRules.h"
1.19      frystyk    17: #include "HTBrowse.h"
1.16      frystyk    18: #include "HTFont.h"
                     19: #include "GridStyle.h"
                     20: #include "GridText.h"
1.1       timbl      21: 
1.23      frystyk    22: #include "HTReqMan.h"
                     23: 
1.11      howcome    24: /* HWL 18/7/94: applied patch from agl@glas2.glas.apc.org (Anton Tropashko) */
                     25: #ifdef CYRILLIC
                     26: #include "a_stdio.h"
                     27: #endif
                     28: 
1.1       timbl      29: #define MAX_LINE       HTScreenWidth   /* No point in accumulating more */
1.16      frystyk    30: #define LOADED_LIMIT 6                 /* For now, save last five texts */
1.1       timbl      31: 
                     32: #ifdef CURSES
                     33: #define DISPLAY_LINES (HTScreenHeight)
                     34: #define       TITLE_LINES      0
                     35: #else
1.19      frystyk    36: #define DISPLAY_LINES (HTScreenHeight - 2)   /* Exclude prompt line */
1.1       timbl      37: #define       TITLE_LINES      1
                     38: #endif
                     39: 
1.19      frystyk    40: struct _HTStream {                     /* only know it as object */
1.37      frystyk    41:     const HTStreamClass *      isa;
1.19      frystyk    42:     /* ... */
                     43: };
1.1       timbl      44: 
                     45: /*     From default style sheet:
                     46: */
                     47: extern HTStyleSheet * styleSheet;      /* Default or overridden */
                     48: 
                     49: /*     Exports
                     50: */ 
                     51: PUBLIC HText * HTMainText = 0;         /* Equivalent of main window */
                     52: PUBLIC HTParentAnchor * HTMainAnchor = 0;      /* Anchor for HTMainText */
                     53: 
                     54: typedef struct _line {
                     55:        struct _line    *next;
                     56:        struct _line    *prev;
                     57:        short unsigned  offset;         /* Implicit initial spaces */
                     58:        short unsigned  size;           /* Number of characters */
                     59:        BOOL    split_after;            /* Can we split after? */
                     60:        BOOL    bullet;                 /* Do we bullet? */
                     61:        char    data[1];                /* Space for terminator at least! */
                     62: } HTLine;
                     63: 
                     64: #define LINE_SIZE(l) (sizeof(HTLine)+(l))      /* allow for terminator */
                     65: 
                     66: typedef struct _TextAnchor {
                     67:        struct _TextAnchor *    next;
                     68:        int                     number;         /* For user interface */
                     69:        int                     start;          /* Characters */
                     70:        int                     extent;         /* Characters */
                     71:        HTChildAnchor *         anchor;
                     72: } TextAnchor;
                     73: 
                     74: 
                     75: /*     Notes on struct _Htext:
                     76: **     next_line is valid iff state is false.
                     77: **     top_of_screen line means the line at the top of the screen
                     78: **                     or just under the title if there is one.
                     79: */
                     80: struct _HText {
                     81:        HTParentAnchor *        node_anchor;
                     82:        char *                  title;
                     83:        HTLine *                last_line;
                     84:        int                     lines;          /* Number of them */
                     85:        int                     chars;          /* Number of them */
                     86:        TextAnchor *            first_anchor;   /* Singly linked list */
                     87:        TextAnchor *            last_anchor;
                     88:        int                     last_anchor_number;     /* user number */
                     89: /* For Internal use: */        
                     90:        HTStyle *               style;                  /* Current style */
                     91:        int                     display_on_the_fly;     /* Lines left */
1.3       timbl      92:        BOOL                    all_pages;              /* Loop on the fly */
1.1       timbl      93:        int                     top_of_screen;          /* Line number */
                     94:        HTLine *                top_of_screen_line;     /* Top */
                     95:        HTLine *                next_line;              /* Bottom + 1 */
                     96:        int                     permissible_split;      /* in last line */
                     97:        BOOL                    in_line_1;              /* of paragraph */
                     98:        BOOL                    stale;                  /* Must refresh */
                     99:        
                    100:        HTStream*               target;                 /* Output stream */
                    101:        HTStreamClass           targetClass;            /* Output routines */
1.36      eric      102:        LineMode *              pLm;
1.1       timbl     103: };
                    104: 
                    105: 
                    106: #define PUTC(c) (*text->targetClass.put_character)(text->target, c)
                    107: #define PUTS(s) (*text->targetClass.put_string)(text->target, s)
                    108: 
                    109: /*     Boring static variable used for moving cursor across
                    110: */
                    111: 
                    112: #define SPACES(n) (&space_string[HTScreenWidth - (n)])
                    113:                                    /* String containing blank spaces only */
                    114: PRIVATE char * space_string;
                    115: 
                    116: PRIVATE HTStyle default_style =
                    117:        { 0,  "(Unstyled)", "",
                    118:        (HTFont)0, 1.0, HT_BLACK,               0, 0,
                    119:        0, 0, 0, HT_LEFT,               1, 0,   0, 
                    120:        NO, NO, 0, 0,                   0 };    
                    121: 
                    122: 
1.29      frystyk   123: PUBLIC void clear_screen (void);       /* Forward */
1.1       timbl     124: 
                    125: PRIVATE HTList * loaded_texts; /* A list of all those in memory */
                    126: 
                    127: /*                     Creation Method
                    128: **                     ---------------
1.3       timbl     129: **
                    130: **     Interactive version
                    131: **
1.1       timbl     132: */
1.36      eric      133: extern LineMode * Context_getLineMode(HTRequest * request);
1.31      frystyk   134: PUBLIC HText * HText_new (HTRequest * request, HTParentAnchor * anchor)
1.1       timbl     135: {
                    136:     HTLine * line;
1.34      frystyk   137:     HText * self;
1.37      frystyk   138:     if ((self = (HText  *) HT_CALLOC(1, sizeof(*self))) == NULL)
1.34      frystyk   139: /*        HT_OUTOFMEM("HText"); */
                    140:        return self;
1.1       timbl     141:     
1.36      eric      142:     self->pLm = Context_getLineMode(request);
1.1       timbl     143:     if (!loaded_texts) loaded_texts = HTList_new();
                    144:     HTList_addObject(loaded_texts, self);
                    145:     if (HTList_count(loaded_texts) >= LOADED_LIMIT) {
1.20      frystyk   146:         if (CACHE_TRACE)
1.36      eric      147:            HTTrace("MemoryCache. Freeing off cached doc.\n"); 
1.1       timbl     148:         HText_free((HText *)HTList_removeFirstObject(loaded_texts));
                    149:     }
                    150:     
1.34      frystyk   151:     if ((line = self->last_line = (HTLine *) HT_MALLOC(LINE_SIZE(MAX_LINE))) == NULL)
                    152:        HT_OUTOFMEM("HText_New");
1.1       timbl     153:     line->next = line->prev = line;
                    154:     line->offset = line->size = 0;
                    155:     self->lines = self->chars = 0;
                    156:     self->title = 0;
                    157:     self->first_anchor = self->last_anchor = 0;
                    158:     self->style = &default_style;
                    159:     self->top_of_screen = 0;
                    160:     self->node_anchor = anchor;
                    161:     self->last_anchor_number = 0;      /* Numbering of them for references */
                    162:     self->stale = YES;
                    163:     
                    164:     self->target = NULL;
                    165:     
1.31      frystyk   166:     HTAnchor_setDocument(anchor, (void *) self);
1.1       timbl     167: 
                    168:     clear_screen();
                    169:     HTMainText = self;
                    170:     HTMainAnchor = anchor;
                    171:     self->display_on_the_fly = DISPLAY_LINES;
1.3       timbl     172:     self->all_pages = NO;      /* One page at a time on the fly */
1.1       timbl     173:     
                    174:     if (!space_string) {       /* Make a blank line */
                    175:         char *p;
1.34      frystyk   176:        if ((space_string = (char  *) HT_MALLOC(HTScreenWidth+1)) == NULL)
                    177:            HT_OUTOFMEM("HText_New");
1.1       timbl     178:         for (p=space_string; p<space_string+HTScreenWidth; p++) 
                    179:             *p = ' ';          /* Used for printfs later */
                    180:         space_string[HTScreenWidth] = '\0'; 
                    181:     }
                    182:     
                    183:     return self;
                    184: }
                    185: 
                    186: 
                    187: /*                     Creation Method 2
                    188: **                     ---------------
                    189: **
1.4       timbl     190: **     Non-interative  OR interactive if stream is NULL
1.1       timbl     191: **     Stream is assumed open and left open.
                    192: */
1.31      frystyk   193: PUBLIC HText * HText_new2 (HTRequest *         request,
                    194:                            HTParentAnchor *    anchor,
                    195:                            HTStream *          stream)
1.1       timbl     196: {
1.31      frystyk   197:     HText * me = HText_new(request, anchor);
1.1       timbl     198:         
                    199:     if (stream) {
1.3       timbl     200:         me->target = stream;
                    201:        me->targetClass = *stream->isa; /* copy action procedures */
1.4       timbl     202:        me->all_pages = YES;    /* Display whole file on the fly */    
1.3       timbl     203:     }
                    204:     return me;
1.1       timbl     205: }
                    206: 
1.31      frystyk   207: /*     Free a data object
                    208: **     ------------------
                    209: **     Removes the data object from the anchor
1.1       timbl     210: */
1.37      frystyk   211: PUBLIC void hyper_free (HText *  self)
1.1       timbl     212: {
1.35      frystyk   213:     if (self) {
                    214:        while (1) {                                   /* Free off line array */
                    215:            HTLine * last = self->last_line;
                    216:            if (last) {
                    217:                last->next->prev = last->prev;
                    218:                last->prev->next = last->next;
                    219:                self->last_line = last->prev;
                    220:                if (last == self->last_line) break;
                    221:                HT_FREE(last);
1.39    ! frystyk   222:            } else
        !           223:                break;
1.35      frystyk   224:        }
                    225:        while (self->first_anchor) {                /* Free off anchor array */
                    226:            TextAnchor * last = self->first_anchor;
                    227:            self->first_anchor = last->next;
                    228:            HT_FREE(last);
                    229:        }
                    230:        if (self == HTMainText) HTMainText = NULL;
1.39    ! frystyk   231:        HT_FREE(self->last_line);
1.35      frystyk   232:        HT_FREE(self);
                    233:     }
1.18      frystyk   234: }
                    235: 
                    236: 
                    237: /*     Free Entire Text
                    238: **     ----------------
                    239: */
1.29      frystyk   240: PUBLIC void    HText_free (HText * self)
1.18      frystyk   241: {
1.35      frystyk   242:     if (self) {
                    243:        HTAnchor_setDocument(self->node_anchor, NULL);
1.37      frystyk   244:        hyper_free(self);
1.35      frystyk   245:     }
1.1       timbl     246: }
                    247: 
1.39    ! frystyk   248: /*
        !           249: **     Free all registered hypertext documents in memory
        !           250: */
        !           251: PUBLIC BOOL HText_freeAll (void)
        !           252: {
        !           253:     if (loaded_texts) {
        !           254:        HTList * cur = loaded_texts;
        !           255:        HText * pres;
        !           256:        while ((pres = (HText *) HTList_nextObject(cur)))
        !           257:            HText_free(pres);
        !           258:        HTList_delete(loaded_texts);
        !           259:        return YES;
        !           260:     }
        !           261:     return NO;
        !           262: }
        !           263: 
1.1       timbl     264: 
                    265: /*             Display Methods
                    266: **             ---------------
                    267: */
1.22      frystyk   268: 
1.1       timbl     269: /*     Clear the screen (on screen-mode systems)
                    270: **     ----------------
                    271: */
1.29      frystyk   272: PUBLIC void clear_screen (void)
1.1       timbl     273: {
1.25      frystyk   274:     if (WWWTRACE)
1.22      frystyk   275:        return;                     /* in trace mode, don't clear trace away */
1.1       timbl     276: #ifdef CURSES
                    277:     if (w_text != NULL) {
1.22      frystyk   278:        wmove(w_text,0,0);
                    279:        wclear(w_text);
1.1       timbl     280:     }
1.22      frystyk   281: #endif /* Not CURSES */
1.1       timbl     282:     if (HTMainText) HTMainText->stale = YES;
                    283: }
                    284: 
                    285: 
                    286: /*     Output a line
                    287: **     -------------
                    288: */
1.29      frystyk   289: PRIVATE void display_line (HText * text, HTLine * line)
1.1       timbl     290: {
                    291: #ifdef CURSES
                    292:       int     y, x;
                    293: 
                    294:       waddstr(w_text, SPACES(line->offset));
                    295:       waddstr(w_text, line->data);
                    296:       getyx(w_text, y, x);
                    297:       if (y < DISPLAY_LINES-1) {
                    298:               wmove(w_text, ++y, 0);
                    299:       }
                    300: #else
                    301:    if (!text->target)
1.11      howcome   302:    {
                    303: #ifdef CYRILLIC
                    304:        /* HWL 18/7/94: applied patch from agl@glas2.glas.apc.org (Anton Tropashko) */
                    305:        a_print(SPACES(line->offset),H,stdout); 
                    306:        a_print(line->data,H,stdout);
                    307:        fputc('\n',stdout);
                    308: #else
1.36      eric      309:        OutputData(LineMode_getView(text->pLm), "%s%s\n", SPACES(line->offset), line->data);
1.11      howcome   310: #endif
                    311:    }
1.1       timbl     312:    else {
                    313:        PUTS(SPACES(line->offset));
                    314:        PUTS(line->data);
                    315:        PUTC('\n');
                    316:    }
                    317: #endif
                    318:    
                    319: }
                    320: 
                    321: /*     Output the title line
                    322: **     ---------------------
                    323: */
1.29      frystyk   324: PRIVATE void display_title (HText * text)
1.1       timbl     325: {
1.37      frystyk   326:     const char * title = HTAnchor_title(text->node_anchor);
1.1       timbl     327:     char percent[20], format[20];
                    328:     if (text->lines > (DISPLAY_LINES-1)) {
                    329: #ifdef NOPE
                    330:        sprintf(percent, " (p%d of %d)",
                    331:            (text->top_of_screen/(DISPLAY_LINES-1)) + 1,
                    332:            (text->lines-1)/(DISPLAY_LINES-1) + 1);
                    333:        sprintf(percent, " (%d%%)",
                    334:            100*(text->top_of_screen+DISPLAY_LINES-1)/  /* Seen */
                    335:                (text->lines));                         /* Total */
                    336: #else
                    337:        sprintf(percent, " (%d/%d)",
                    338:            text->top_of_screen+DISPLAY_LINES-1,        /* Seen */
                    339:            text->lines);                               /* Total */
                    340: #endif
                    341:     } else {
                    342:        percent[0] = 0; /* Null string */
                    343:     }
                    344: 
                    345:     sprintf(format, "%%%d.%ds%%s\n",   /* Generate format string */
                    346:                    (int)(HTScreenWidth-strlen(percent)),
                    347:                    (int)(HTScreenWidth-strlen(percent)) );
                    348: #ifdef CURSES
                    349:     mvwprintw(w_top, 0, 0, format, title, percent);
                    350:     wrefresh(w_top);
                    351: #else
1.36      eric      352:     if (!text->target) OutputData(LineMode_getView(text->pLm), format, title, percent);
1.1       timbl     353:     else {
1.34      frystyk   354:        char * line;
                    355:        if ((line = (char *) HT_MALLOC(HTScreenWidth+10)) == NULL)
                    356:            HT_OUTOFMEM("display_titile");
1.1       timbl     357:         sprintf(line, format, title, percent);
                    358:        PUTS(line);
1.34      frystyk   359:        HT_FREE(line);
1.1       timbl     360:     }
                    361: #endif
                    362: }
                    363: 
                    364: 
                    365: /*     Fill the screen with blank after the file
                    366: **     --------------------------
                    367: */
1.29      frystyk   368: PRIVATE void fill_screen (HText *  text, int n)
1.1       timbl     369: {
                    370:     if (n<=0 || n>1000) return;                /* Large value means no pagination */
                    371:     if (text->target) return;
                    372: #ifdef CURSES
                    373:     waddstr(w_text, end_mark);
                    374:     wclrtobot(w_text);
                    375:     wrefresh(w_text);
                    376: #else
                    377: #ifndef VIOLA    
1.36      eric      378:     if (!text->target) OutputData(LineMode_getView(text->pLm), "%s\n", end_mark);
1.1       timbl     379:     else { PUTS(end_mark); PUTC('\n'); }
                    380:     n--;
                    381:     
                    382:     for (; n; n--) {
1.36      eric      383:         if (!text->target) OutputData(LineMode_getView(text->pLm), "\n");
1.1       timbl     384:        else PUTC('\n');
                    385:     }
                    386: #endif
                    387: #endif        /* Not CURSES */
                    388: }
                    389: 
                    390: 
                    391: /*     Output a page
                    392: **     -------------
                    393: */
1.29      frystyk   394: PRIVATE void display_page (HText * text, int line_number)
1.1       timbl     395: {
                    396:     HTLine * line = text->last_line->prev;
                    397:     int i;
1.37      frystyk   398:     const char * title = HTAnchor_title(text->node_anchor);
1.1       timbl     399:     int lines_of_text = title ? (DISPLAY_LINES-TITLE_LINES) : DISPLAY_LINES;
                    400:     int last_screen = text->lines - lines_of_text;
                    401: 
                    402: /*     Constrain the line number to be within the document
                    403: */
                    404:     if (text->lines <= lines_of_text) line_number = 0;
                    405:     else if (line_number>last_screen) line_number = last_screen;
                    406:     else if (line_number < 0) line_number = 0;
                    407:     
                    408:     for(i=0,  line = text->last_line->next;            /* Find line */
                    409:        i<line_number && (line!=text->last_line);
                    410:       i++, line=line->next) /* Loop */ assert(line->next != NULL);
                    411: 
                    412:     while((line!=text->last_line) && (line->size==0)) {        /* Skip blank lines */
                    413:         assert(line->next != NULL);
                    414:       line = line->next;
                    415:         line_number ++;
                    416:     }
                    417: 
                    418: /*     Can we just scroll to it?
                    419: */ 
                    420: #ifndef VM                     /* No scrolling */
                    421:     if (!text->stale && (line_number>=text->top_of_screen) &&
                    422:        (line_number < text->top_of_screen + DISPLAY_LINES)) {  /* Yes */
                    423:        lines_of_text = line_number - text->top_of_screen;
                    424:        line = text->next_line;
                    425:         text->top_of_screen = line_number;
                    426: #ifdef CURSES
                    427:         scrollok(w_text, TRUE);
                    428:         for (i = 0; i < lines_of_text; i++) {
                    429:               scroll(w_text);
                    430:         }
                    431: #endif  
                    432:     } else
                    433: #endif
                    434:     {
                    435:        clear_screen();                                         /* No */
                    436:         text->top_of_screen = line_number;
                    437:        if (title) display_title(text);
                    438:     }
                    439:     
                    440: /* Bug: when we scroll to a part slightly futher down a page which previously
                    441:  fitted all on one screen including the [End], the code below will add an
                    442:  extra [End] to the screen, giving a total of two, one underneath the other.
                    443: */
                    444:     
                    445:  /*    print it
                    446:  */
                    447:     if (line) {
                    448:       for(i=0;
                    449:          (i< lines_of_text) && (line != text->last_line); i++)  {
                    450:       assert(line != NULL);
                    451:         display_line(text, line);
                    452:        line = line->next;
                    453:       }
                    454:       fill_screen(text, lines_of_text - i);
                    455:     }
                    456: 
                    457:     text->next_line = line;    /* Line after screen */
                    458:     text->stale = NO;          /* Display is up-to-date */
                    459: }
                    460: 
                    461: 
                    462: /*                     Object Building methods
                    463: **                     -----------------------
                    464: **
                    465: **     These are used by a parser to build the text in an object
                    466: */
1.29      frystyk   467: PUBLIC void HText_beginAppend (HText * text)
1.1       timbl     468: {
                    469:     text->permissible_split = 0;
                    470:     text->in_line_1 = YES;
                    471: }
                    472: 
                    473: 
                    474: /*     Add a new line of text
                    475: **     ----------------------
                    476: **
                    477: ** On entry,
                    478: **
                    479: **     split   is zero for newline function, else number of characters
                    480: **             before split.
                    481: **     text->display_on_the_fly
                    482: **             may be set to indicate direct output of the finished line.
1.3       timbl     483: **     text->all_pages
                    484: **             if set indicates all pages are to be done on the fly.
1.1       timbl     485: ** On exit,
                    486: **             A new line has been made, justified according to the
                    487: **             current style. Text after the split (if split nonzero)
                    488: **             is taken over onto the next line.
                    489: **
                    490: **             If display_on_the_fly is set, then it is decremented and
                    491: **             the finished line is displayed.
                    492: */
                    493: #define new_line(text) split_line(text, 0)
                    494: 
1.29      frystyk   495: PRIVATE void split_line (HText * text, int split)
1.1       timbl     496: {
                    497:     HTStyle * style = text->style;
                    498:     int spare;
1.14      frystyk   499:     int indent = (int) (text->in_line_1 ? text->style->indent1st
                    500:                        : text->style->leftIndent);
1.7       frystyk   501: 
1.1       timbl     502: /*     Make new line
                    503: */
                    504:     HTLine * previous = text->last_line;
1.34      frystyk   505:     HTLine * line;
                    506:     if ((line = (HTLine  *) HT_MALLOC(LINE_SIZE(MAX_LINE))) == NULL)
                    507:         HT_OUTOFMEM("split_line");
                    508: 
1.1       timbl     509:     text->lines++;
                    510:     
                    511:     previous->next->prev = line;
                    512:     line->prev = previous;
                    513:     line->next = previous->next;
                    514:     previous->next = line;
                    515:     text->last_line = line;
                    516:     line->size = 0;
                    517:     line->offset = 0;
                    518: 
                    519: /*     Split at required point
                    520: */    
                    521:     if (split) {       /* Delete space at "split" splitting line */
                    522:        char * p;
                    523:        previous->data[previous->size] = 0;
                    524:        for (p = &previous->data[split]; *p; p++)
                    525:            if (*p != ' ') break;
                    526:        strcpy(line->data, p);
                    527:        line->size = strlen(line->data);
                    528:        previous->size = split;
                    529:     }
                    530:     
                    531:     while ((previous->size > 0) &&
1.37      frystyk   532:           (previous->data[previous->size-1] == ' '))   /* Strip trailers */
1.1       timbl     533:         previous->size--;
1.7       frystyk   534: 
1.39    ! frystyk   535:     if ((previous = (HTLine *)
        !           536:         HT_REALLOC(previous, LINE_SIZE(previous->size)))==NULL)
        !           537:        HT_OUTOFMEM("split_line");
1.1       timbl     538:     previous->prev->next = previous;   /* Link in new line */
                    539:     previous->next->prev = previous;   /* Could be same node of course */
                    540: 
                    541: /*     Terminate finished line for printing
                    542: */
                    543:     previous->data[previous->size] = 0;
1.7       frystyk   544: 
1.1       timbl     545: /*     Align left, right or center
                    546: */
                    547: 
1.14      frystyk   548:     spare =  (int) (HTScreenWidth - style->rightIndent + style->leftIndent -
                    549:                    previous->size);                 /* @@ first line indent */
1.1       timbl     550:                
                    551:     switch (style->alignment) {
                    552:        case HT_CENTER :
                    553:            previous->offset = previous->offset + indent + spare/2;
                    554:            break;
                    555:        case HT_RIGHT :
                    556:            previous->offset = previous->offset + indent + spare;
                    557:            break;
                    558:        case HT_LEFT :
                    559:        case HT_JUSTIFY :               /* Not implemented */
                    560:        default:
                    561:            previous->offset = previous->offset + indent;
                    562:            break;
                    563:     } /* switch */
                    564: 
                    565:     text->chars = text->chars + previous->size + 1;    /* 1 for the line */
                    566:     text->in_line_1 = NO;              /* unless caller sets it otherwise */
                    567:     
                    568: /*     If displaying as we go, output it:
                    569: */
                    570:     if (text->display_on_the_fly) {
                    571:         if (text->display_on_the_fly == DISPLAY_LINES) { /* First line */
                    572:            if (previous->size == 0) {
                    573:                text->top_of_screen++;  /* Scroll white space off top */
                    574:                return;
                    575:            }
                    576:            if (HTAnchor_title(text->node_anchor)) { /* Title exists */
                    577:                display_title(text);
                    578:                text->display_on_the_fly--;
                    579:            }
                    580:        }
                    581:        display_line(text, previous);
                    582:        text->display_on_the_fly--;
1.3       timbl     583:        
                    584:        /* Loop to top of next page? */
                    585:        if (!text->display_on_the_fly && text->all_pages) {
                    586:            PUTS("\f\n"); /* Form feed on its own line a la rfc1111 */
                    587:            text->display_on_the_fly = DISPLAY_LINES;
                    588:        }
1.1       timbl     589:     }
                    590: } /* split_line */
                    591: 
                    592: 
                    593: /*     Allow vertical blank space
                    594: **     --------------------------
                    595: */
1.29      frystyk   596: PRIVATE void blank_lines (HText * text, int newlines)
1.1       timbl     597: {
                    598:     if (text->last_line->size == 0) {  /* No text on current line */
                    599:        HTLine * line = text->last_line->prev;
                    600:        while ((line!=text->last_line) && (line->size == 0)) {
                    601:            if (newlines==0) break;
                    602:            newlines--;         /* Don't bother: already blank */
                    603:            line = line->prev;
                    604:        }
                    605:     } else {
                    606:        newlines++;                     /* Need also to finish this line */
                    607:     }
                    608: 
                    609:     for(;newlines;newlines--) {
                    610:        new_line(text);
                    611:     }
                    612:     text->in_line_1 = YES;
                    613: }
                    614: 
                    615: 
                    616: /*     New paragraph in current style
                    617: **     ------------------------------
                    618: ** See also: setStyle.
                    619: */
                    620: 
1.29      frystyk   621: PUBLIC void HText_appendParagraph (HText * text)
1.1       timbl     622: {
1.14      frystyk   623:     int after = (int) text->style->spaceAfter;
                    624:     int before = (int) text->style->spaceBefore;
1.1       timbl     625:     blank_lines(text, after>before ? after : before);
                    626: }
                    627: 
                    628: 
                    629: /*     Set Style
                    630: **     ---------
                    631: **
                    632: **     Does not filter unnecessary style changes.
                    633: */
1.29      frystyk   634: PUBLIC void HText_setStyle (HText * text, HTStyle * style)
1.1       timbl     635: {
                    636:     int after, before;
                    637: 
                    638:     if (!style) return;                                /* Safety */
1.14      frystyk   639:     after = (int) text->style->spaceAfter;
                    640:     before = (int) style->spaceBefore;
1.20      frystyk   641:     if (SGML_TRACE)
1.36      eric      642:        HTTrace("HTML: Change to style %s\n", style->name);
1.1       timbl     643:     blank_lines (text, after>before ? after : before);
                    644:     text->style = style;
                    645: }
                    646: 
                    647: 
                    648: /*     Append a character to the text object
                    649: **     -------------------------------------
                    650: */
1.29      frystyk   651: PUBLIC void HText_appendCharacter (HText * text, char ch)
1.1       timbl     652: {
                    653:     HTLine * line = text->last_line;
                    654:     HTStyle * style = text->style;
1.14      frystyk   655:     int indent = (int)(text->in_line_1 ? style->indent1st : style->leftIndent);
1.1       timbl     656:     
                    657: /*             New Line
                    658: */
                    659:     if (ch == '\n') {
                    660:            new_line(text);
                    661:            text->in_line_1 = YES;      /* First line of new paragraph */
                    662:            return;
                    663:     }
                    664: 
                    665: /*             Tabs
                    666: */
                    667: 
                    668:     if (ch == '\t') {
                    669:         HTTabStop * tab;
                    670:        int target;     /* Where to tab to */
                    671:        int here = line->size + line->offset +indent;
                    672:         if (style->tabs) {     /* Use tab table */
                    673:            for (tab = style->tabs;
                    674:                tab->position <= here;
                    675:                tab++)
                    676:                if (!tab->position) {
                    677:                    new_line(text);
                    678:                    return;
                    679:                }
1.14      frystyk   680:            target = (int) tab->position;
1.1       timbl     681:        } else if (text->in_line_1) {   /* Use 2nd indent */
                    682:            if (here >= style->leftIndent) {
                    683:                new_line(text); /* wrap */
                    684:                return;
                    685:            } else {
1.14      frystyk   686:                target = (int) style->leftIndent;
1.1       timbl     687:            }
                    688:        } else {                /* Default tabs align with left indent mod 8 */
                    689: #ifdef DEFAULT_TABS_8
                    690:            target = ((line->offset + line->size + 8) & (-8))
                    691:                        + style->leftIndent;
                    692: #else
                    693:            new_line(text);
                    694:            return;
                    695: #endif
                    696:        }
                    697:        if (target > HTScreenWidth - style->rightIndent) {
                    698:            new_line(text);
                    699:            return;
                    700:        } else {
                    701:             text->permissible_split = line->size;      /* Can split here */
                    702:            if (line->size == 0) line->offset = line->offset + target - here;
                    703:            else for(; here<target; here++) {
                    704:                 line->data[line->size++] = ' ';        /* Put character into line */
                    705:            }
                    706:            return;
                    707:        }
                    708:        /*NOTREACHED*/
                    709:     } /* if tab */ 
                    710: 
                    711:     
                    712:     if (ch==' ') {
                    713:         text->permissible_split = line->size;  /* Can split here */
                    714:     }
                    715: 
                    716: /*     Check for end of line
                    717: */    
                    718:     if (indent + line->offset + line->size + style->rightIndent
                    719:                >= HTScreenWidth) {
                    720:         if (style->wordWrap) {
1.7       frystyk   721:            if(text->permissible_split > line->size)    /* HENRIK 21/02-94 */
                    722:                text->permissible_split = line->size;
1.1       timbl     723:            split_line(text, text->permissible_split);
                    724:            if (ch==' ') return;        /* Ignore space causing split */
                    725:        } else new_line(text);
                    726:     }
                    727: 
                    728: /*     Insert normal characters
                    729: */
                    730:     if (ch == HT_NON_BREAK_SPACE) {
                    731:         ch = ' ';
                    732:     }
                    733:     {
                    734:         HTLine * line = text->last_line;       /* May have changed */
                    735:         HTFont font = style->font;
                    736:         line->data[line->size++] =     /* Put character into line */
                    737:            font & HT_CAPITALS ? TOUPPER(ch) : ch;
                    738:         if (font & HT_DOUBLE)          /* Do again if doubled */
                    739:             HText_appendCharacter(text, HT_NON_BREAK_SPACE);
                    740:            /* NOT a permissible split */ 
                    741:     }
                    742: }
                    743: 
                    744: /*             Anchor handling
                    745: **             ---------------
                    746: */
                    747: /*     Start an anchor field
                    748: */
1.29      frystyk   749: PUBLIC void HText_beginAnchor (HText * text, HTChildAnchor * anc)
1.1       timbl     750: {
1.2       timbl     751:     char marker[100];
1.34      frystyk   752:     TextAnchor * a;
                    753:     if ((a = (TextAnchor  *) HT_MALLOC(sizeof(*a))) == NULL)
                    754:         HT_OUTOFMEM("HText_beginAnchor");
1.1       timbl     755:     a->start = text->chars + text->last_line->size;
                    756:     a->extent = 0;
                    757:     if (text->last_anchor) {
                    758:         text->last_anchor->next = a;
                    759:     } else {
                    760:         text->first_anchor = a;
                    761:     }
                    762:     a->next = 0;
                    763:     a->anchor = anc;
                    764:     text->last_anchor = a;
                    765:     
                    766:     if (HTAnchor_followMainLink((HTAnchor*)anc)) {
                    767:         a->number = ++(text->last_anchor_number);
                    768:     } else {
                    769:         a->number = 0;
                    770:     }
1.2       timbl     771:     
                    772:     if (start_reference && a->number && display_anchors) {
                    773:        /* If it goes somewhere */
                    774:        sprintf(marker, start_reference, a->number);
                    775:        HText_appendText(text, marker);
                    776:     }
1.1       timbl     777: }
                    778: 
                    779: 
1.29      frystyk   780: PUBLIC void HText_endAnchor (HText * text)
1.1       timbl     781: {
                    782:     TextAnchor * a = text->last_anchor;
                    783:     char marker[100];
                    784:     if (a->number && display_anchors) {         /* If it goes somewhere */
1.2       timbl     785:        sprintf(marker, end_reference, a->number);
1.1       timbl     786:        HText_appendText(text, marker);
                    787:     }
                    788:     a->extent = text->chars + text->last_line->size - a->start;
                    789: }
                    790: 
                    791: 
1.5       timbl     792: /*             IMAGES
                    793: */
1.29      frystyk   794: PUBLIC void HText_appendImage (
                    795:        HText *                 text,
                    796:        HTChildAnchor *         anc,
1.37      frystyk   797:        const char *            alt,
                    798:        const char *            alignment,
1.29      frystyk   799:        BOOL                    isMap)
1.5       timbl     800: {
1.9       frystyk   801:     HText_appendText(text, alt? alt : "[IMAGE]");
1.5       timbl     802: }
                    803:        
1.37      frystyk   804: PUBLIC void HText_appendText (HText * text, const char * str)
1.1       timbl     805: {
1.37      frystyk   806:     const char * p;
1.1       timbl     807:     for(p=str; *p; p++) {
                    808:         HText_appendCharacter(text, *p);
                    809:     }
                    810: }
                    811: 
                    812: 
1.29      frystyk   813: PUBLIC void HText_endAppend (HText * text)
1.1       timbl     814: {
                    815:     new_line(text);
                    816:     
                    817:     if (text->display_on_the_fly) {            /* Not finished? */
                    818:         fill_screen(text, text->display_on_the_fly);   /* Finish it */
                    819:        text->display_on_the_fly = 0;
                    820:        text->next_line = text->last_line;      /* Bug fix after EvA 920117 */
                    821:        text->stale = NO;
                    822:     }
                    823: }
                    824: 
                    825: 
                    826: 
1.20      frystyk   827: /*     Dump diagnostics to TDEST
1.1       timbl     828: */
1.29      frystyk   829: PUBLIC void HText_dump (HText * text)
1.1       timbl     830: {
1.36      eric      831:     HTTrace("HText: Dump called\n");
1.1       timbl     832: }
                    833:        
                    834: 
                    835: /*     Return the anchor associated with this node
                    836: */
1.29      frystyk   837: PUBLIC HTParentAnchor * HText_nodeAnchor (HText * text)
1.1       timbl     838: {
                    839:     return text->node_anchor;
                    840: }
                    841: 
                    842: /*                             GridText specials
                    843: **                             =================
                    844: */
                    845: /*     Return the anchor with index N
                    846: **
                    847: **     The index corresponds to the number we print in the anchor.
                    848: */
1.29      frystyk   849: PUBLIC HTChildAnchor * HText_childNumber (HText * text, int number)
1.1       timbl     850: {
                    851:     TextAnchor * a;
                    852:     for (a = text->first_anchor; a; a = a->next) {
                    853:         if (a->number == number) return a->anchor;
                    854:     }
                    855:     return (HTChildAnchor *)0; /* Fail */
                    856: }
                    857: 
1.29      frystyk   858: PUBLIC void HText_setStale (HText * text)
1.1       timbl     859: {
1.16      frystyk   860:     if (text)
                    861:        text->stale = YES;
1.1       timbl     862: }
                    863: 
1.29      frystyk   864: PUBLIC void HText_refresh (HText * text)
1.1       timbl     865: {
1.16      frystyk   866:     if (text && text->stale)
                    867:        display_page(text, text->top_of_screen);
1.1       timbl     868: }
                    869: 
1.29      frystyk   870: PUBLIC int HText_sourceAnchors (HText * text)
1.1       timbl     871: {
1.16      frystyk   872:     return (text ? text->last_anchor_number : -1);
1.1       timbl     873: }
                    874: 
1.29      frystyk   875: PUBLIC BOOL HText_canScrollUp (HText * text)
1.1       timbl     876: {
1.16      frystyk   877:     return (text && text->top_of_screen != 0);
1.1       timbl     878: }
                    879: 
1.29      frystyk   880: PUBLIC BOOL HText_canScrollDown (HText * text)
1.1       timbl     881: {
1.16      frystyk   882:     return (text && (text->top_of_screen + DISPLAY_LINES -
                    883:                     (text->title ? TITLE_LINES : 0)) < text->lines);
1.1       timbl     884: }
                    885: 
                    886: /*             Scroll actions
                    887: */
1.29      frystyk   888: PUBLIC void HText_scrollTop (HText * text)
1.1       timbl     889: {
                    890:     display_page(text, 0);
                    891: }
                    892: 
1.29      frystyk   893: PUBLIC void HText_scrollDown (HText * text)
1.1       timbl     894: {
                    895:     display_page(text, text->top_of_screen + DISPLAY_LINES -1);
                    896: }
                    897: 
1.29      frystyk   898: PUBLIC void HText_scrollUp (HText * text)
1.1       timbl     899: {
                    900:     display_page(text, text->top_of_screen - DISPLAY_LINES +1);
                    901: }
                    902: 
1.29      frystyk   903: PUBLIC void HText_scrollBottom (HText * text)
1.1       timbl     904: {
                    905:     display_page(text, text->lines - DISPLAY_LINES +1);
                    906: }
                    907: 
                    908: 
                    909: /*             Browsing functions
                    910: **             ==================
                    911: */
                    912: 
                    913: /* Bring to front and highlight it
                    914: */
                    915: 
1.29      frystyk   916: PRIVATE int line_for_char (HText * text, int char_num)
1.1       timbl     917: {
                    918:     int line_number =0;
                    919:     int characters = 0;
                    920:     HTLine * line = text->last_line->next;
                    921:     for(;;) {
                    922:        if (line == text->last_line) return 0;  /* Invalid */
                    923:         characters = characters + line->size + 1;
                    924:        if (characters > char_num) return line_number;
                    925:        line_number ++;
                    926:        line = line->next;
                    927:     }
                    928: }
                    929: 
1.29      frystyk   930: PUBLIC BOOL HText_select (HText * text)
1.1       timbl     931: {
1.13      frystyk   932:     if (text) {
1.1       timbl     933:         HTMainText = text;
                    934:        HTMainAnchor = text->node_anchor;
                    935:        display_page(text, text->top_of_screen);
1.13      frystyk   936:        return YES;
1.1       timbl     937:     }
1.20      frystyk   938:     if (SGML_TRACE)
1.36      eric      939:        HTTrace("HText: Nothing to select!\n");
1.13      frystyk   940:     return NO;
1.1       timbl     941: }
                    942: 
1.29      frystyk   943: PUBLIC BOOL HText_selectAnchor (HText * text, HTChildAnchor * anchor)
1.1       timbl     944: {
                    945:     TextAnchor * a;
                    946: 
                    947:     for(a=text->first_anchor; a; a=a->next) {
                    948:         if (a->anchor == anchor) break;
                    949:     }
                    950:     if (!a) {
1.20      frystyk   951:         if (SGML_TRACE)
1.36      eric      952:            HTTrace("HText: No such anchor in this text!\n");
1.1       timbl     953:         return NO;
                    954:     }
                    955: 
                    956:     if (text != HTMainText) {          /* Comment out by ??? */
                    957:         HTMainText = text;             /* Put back in by tbl 921208 */
                    958:        HTMainAnchor = text->node_anchor;
                    959:     }
                    960: 
                    961:     {
1.20      frystyk   962:        int l = line_for_char(text, a->start);
                    963:        if (SGML_TRACE)
1.36      eric      964:            HTTrace("HText: Selecting anchor [%d] at char %d, line %d\n",
1.20      frystyk   965:                    a->number, a->start, l);
1.1       timbl     966: 
                    967:        if ( !text->stale &&
1.20      frystyk   968:            (l >= text->top_of_screen) &&
                    969:            ( l < text->top_of_screen + DISPLAY_LINES-1))
                    970:            return YES;
1.1       timbl     971:         display_page(text, l - (DISPLAY_LINES/3));     /* Scroll to it */
                    972:     }
                    973:     return YES;
                    974: }
                    975:  
                    976: 
                    977: /*             Editing functions               - NOT IMPLEMENTED
                    978: **             =================
                    979: **
                    980: **     These are called from the application. There are many more functions
                    981: **     not included here from the orginal text object.
                    982: */
                    983: 
                    984: /*     Style handling:
                    985: */
                    986: /*     Apply this style to the selection
                    987: */
1.29      frystyk   988: PUBLIC void HText_applyStyle (HText *  me, HTStyle * style)
1.1       timbl     989: {
                    990:     
                    991: }
                    992: 
                    993: 
                    994: /*     Update all text with changed style.
                    995: */
1.29      frystyk   996: PUBLIC void HText_updateStyle (HText *  me, HTStyle * style)
1.1       timbl     997: {
                    998:     
                    999: }
                   1000: 
                   1001: 
                   1002: /*     Return style of  selection
                   1003: */
1.29      frystyk  1004: PUBLIC HTStyle * HText_selectionStyle (
                   1005:        HText * me,
                   1006:        HTStyleSheet * sheet)
1.1       timbl    1007: {
                   1008:     return 0;
                   1009: }
                   1010: 
                   1011: 
                   1012: /*     Paste in styled text
                   1013: */
1.29      frystyk  1014: PUBLIC void HText_replaceSel (
                   1015:        HText * me,
1.37      frystyk  1016:        const char * aString, 
1.29      frystyk  1017:        HTStyle * aStyle)
1.1       timbl    1018: {
                   1019: }
                   1020: 
                   1021: 
                   1022: /*     Apply this style to the selection and all similarly formatted text
                   1023: **     (style recovery only)
                   1024: */
1.29      frystyk  1025: PUBLIC void HTextApplyToSimilar (HText * me, HTStyle * style)
1.1       timbl    1026: {
                   1027:     
                   1028: }
                   1029: 
                   1030:  
                   1031: /*     Select the first unstyled run.
                   1032: **     (style recovery only)
                   1033: */
1.29      frystyk  1034: PUBLIC void HTextSelectUnstyled (HText * me, HTStyleSheet * sheet)
1.1       timbl    1035: {
                   1036:     
                   1037: }
                   1038: 
                   1039: 
                   1040: /*     Anchor handling:
                   1041: */
1.29      frystyk  1042: PUBLIC void            HText_unlinkSelection (HText * me)
1.1       timbl    1043: {
                   1044:     
                   1045: }
                   1046: 
1.29      frystyk  1047: PUBLIC HTAnchor *      HText_referenceSelected (HText * me)
1.1       timbl    1048: {
                   1049:      return 0;   
                   1050: }
                   1051: 
                   1052: 
                   1053: #ifdef CURSES
1.29      frystyk  1054: PUBLIC int HText_getTopOfScreen (HText * text)
1.1       timbl    1055: {
                   1056:       return text->top_of_screen;
                   1057: }
                   1058: 
1.29      frystyk  1059: PUBLIC int HText_getLines (HText * text)
1.1       timbl    1060: {
                   1061:       return text->lines;
                   1062: }
                   1063: #endif
1.29      frystyk  1064: PUBLIC HTAnchor *      HText_linkSelTo (HText * me, HTAnchor * anchor)
1.1       timbl    1065: {
                   1066:     return 0;
                   1067: }
                   1068: 
1.20      frystyk  1069: /*
1.24      frystyk  1070: **     Returns YES: keep header, NO: discard
                   1071: */
                   1072: PUBLIC BOOL HTHeaderParser (HTRequest *request, char *header)
                   1073: {
                   1074:     if (STREAM_TRACE)
1.36      eric     1075:        HTTrace("MIMEExtra... we are now in callback\n");
1.24      frystyk  1076:     return YES;
                   1077: }
                   1078: 
                   1079: /*
1.20      frystyk  1080: ** Check if document is already loaded. As the application handles the
                   1081: ** memory cache, we call the application to ask. Also check if it has
                   1082: ** expired in which case we reload it (either from disk cache or remotely)
                   1083: */
1.24      frystyk  1084: PUBLIC int HTMemoryCache (HTRequest * request, HTExpiresMode mode,
                   1085:                          char * notification)
1.20      frystyk  1086: {
1.27      frystyk  1087:     HTParentAnchor *anchor = HTRequest_anchor(request);
1.20      frystyk  1088:     HText *text;
                   1089:     if (!request)
                   1090:        return HT_ERROR;
1.27      frystyk  1091:     if ((text = (HText *) HTAnchor_document(anchor))) {
                   1092:        if (HTRequest_reloadMode(request) != HT_MEM_REFRESH) {
1.20      frystyk  1093:            if (CACHE_TRACE)
1.36      eric     1094:                HTTrace("HTMemCache.. Document already in memory\n");
1.20      frystyk  1095:            if (mode != HT_EXPIRES_IGNORE) {
1.27      frystyk  1096:                if (!HTCache_isValid(anchor)) {
1.29      frystyk  1097:                    if (mode == HT_EXPIRES_NOTIFY) {
1.36      eric     1098:                        if (WWWTRACE) HTTrace("%s\n", notification);
1.29      frystyk  1099:                    } else {
1.20      frystyk  1100:                        if (CACHE_TRACE)
1.36      eric     1101:                            HTTrace(
1.20      frystyk  1102:                                    "HTMemCache.. Expired - autoreload\n");
1.32      frystyk  1103:                        HTRequest_addRqHd(request, HT_C_IMS);
1.20      frystyk  1104: #ifndef HT_SHARED_DISK_CACHE
1.27      frystyk  1105:                        HTRequest_setReloadMode(request, HT_CACHE_REFRESH);
1.20      frystyk  1106: #endif
                   1107:                        return HT_ERROR; /* Must go get it */
                   1108:                    }
                   1109:                }
                   1110:            }
1.1       timbl    1111: 
1.20      frystyk  1112:            /*
                   1113:             ** If we can use object then select it and return
                   1114:             ** This should be a callback to the app mem cache handler
                   1115:             */
                   1116:            if (request->childAnchor)
                   1117:                HText_selectAnchor(text, request->childAnchor);
                   1118:            else
                   1119:                HText_select(text);
                   1120:            return HT_LOADED;
                   1121:        } else {                /* If refresh version in memory */
1.32      frystyk  1122:            HTRequest_addRqHd(request, HT_C_IMS);
1.20      frystyk  1123:        }
                   1124:     } else {                         /* Don't reuse any old metainformation */
1.27      frystyk  1125:        HTAnchor_clearHeader(anchor);
1.20      frystyk  1126:     }
                   1127:     return HT_ERROR;
                   1128: }
1.26      frystyk  1129: 

Webmaster