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

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

Webmaster