lynx-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

lynx-dev Re: [PATCH 2.8.4dev.18] GridText.c cleanup


From: Ilya Zakharevich
Subject: lynx-dev Re: [PATCH 2.8.4dev.18] GridText.c cleanup
Date: Sat, 3 Mar 2001 01:10:00 -0500
User-agent: Mutt/1.2.5i

On Sun, Feb 25, 2001 at 01:46:54AM -0500, Ilya Zakharevich wrote:
> This patch does not add any new functionality.  All I did was I went
> through more than 1000 lines of extremely badly written obfuscated C code
> worthy of a first year CS student...

The patch in the current message is an addition to that patch of
several days ago.  It adds the following:

  a) Fixes a newly-introduced bug with SpecialAttrChars not being update;

  b) Deobfuscates another part of split-the-line algorithm;

  c) Fixes the bug with wrongly calculate width of a numeric tag [12]
     as far as 12 is 10 or more.

Enjoy,
Ilya

--- ./src/GridText.c-as-sent    Sun Feb 25 01:22:04 2001
+++ ./src/GridText.c    Sun Feb 25 05:57:18 2001
@@ -343,7 +343,7 @@ struct _HText {
        BOOLEAN                 old_dtd;
        int                     keypad_mode;
        int                     disp_lines;     /* Screen size */
-       int                     disp_cols;
+       int                     disp_cols;      /* Used for reports only */
 #endif
        HTLine *                last_line;
        int                     Lines;          /* Number of them */
@@ -2648,11 +2648,13 @@ PRIVATE void split_line ARGS2(
        line->data[line->size++] = LY_UNDERLINE_START_CHAR;
        line->data[line->size] = '\0';
        ctrl_chars_on_this_line++;
+       SpecialAttrChars++;
     }
     if (t_bold) {
        line->data[line->size++] = LY_BOLD_START_CHAR;
        line->data[line->size] = '\0';
        ctrl_chars_on_this_line++;
+       SpecialAttrChars++;
     }
 
     /*
@@ -3020,124 +3022,71 @@ PRIVATE void split_line ARGS2(
 
     if (s > 0) {                       /* if not completely empty */
        TextAnchor * prev_a = NULL;
+       int moved = 0;
 
+       /* In the algorithm below we move or not move anchors between
+          lines using some heuristic criteria.  However, it is
+          desirable not to have two consequent anchors on different
+          lines *in a wrong order*!  (How can this happen?)
+          So when the "reasonable choice" is not unique, we use the
+          MOVED flag to choose one.
+        */
+       /* Our operations can make a non-empty all-whitespace link
+          empty.  So what? */
        for (a = text->first_anchor; a; prev_a = a, a = a->next) {
            if (a->line_num == CurLine) {
-               int old_e = a->extent;
-               int a0 = a->line_pos, a1 = a->line_pos + a->extent;
-               int new_pos, new_ext;
-
-               if (a->link_type == INPUT_ANCHOR) {
-                   if (a->line_pos >= s) {
-                       a->line_pos -= (s_post - SpecialAttrChars);
-                       a->line_num = text->Lines;
+               int len = a->extent, n = a->number, start = a->line_pos;
+               int end = start + len;
+
+               /* Which anchors do we leave on the previous line?
+                  a) empty finished (We need a cut-off value.
+                     "Just because": those before s;
+                     this is the only case when we use s, not s_pre/s_post);
+                  b) Those which start before s_pre;
+                */
+               if (start < s_pre) {
+                   if (end <= s_pre)
+                       continue;       /* No problem */
+
+                   CTRACE_SPLITLINE((tfp, "anchor %d: no relocation", n));
+                   if (end > s_post) {
+                       CTRACE_SPLITLINE((tfp, " of the start.\n"));
+                       a->extent += -(TailTrim + HeadTrim) - SpecialAttrChars;
+                   } else {
+                       CTRACE_SPLITLINE((tfp, ", cut the end.\n"));
+                       a->extent = s_pre - start;
                    }
                    continue;
-               }
-               if (a0 < s_pre && a1 <= s_pre)
-                   continue;   /* unaffected, leave it where it is. */
-               new_ext = old_e = a->extent;
-
-               if (!old_e &&
-                   (!a->number || a->show_anchor) &&
-                   a0 <= s_post) {
-                   CTRACE_SPLITLINE((tfp, "anchor %d case %d: ", a->number,1));
-                   /*
-                    *  It is meant to be empty, and/or endAnchor
-                    *  has seen it and recognized it as empty.
-                    */
-                   new_pos = (a0 <= s) ? s_pre :
-                       s_pre + 1;
-                   if ( prev_a
-                        && prev_a->line_num == CurLine
-                        && new_pos < prev_a->line_pos ) {
-                       if (prev_a->line_pos <= s_pre + 1 + SpecialAttrChars)
-                           new_pos = prev_a->line_pos;
-                       else
-                           new_pos = s_pre + 1 + SpecialAttrChars;
-                   }
-               } else if (old_e &&
-                    a0 >= s_pre && a0 <= s_post &&
-                    a1 <= s_post) {
-                   CTRACE_SPLITLINE((tfp, "anchor %d case %d: ", a->number,2));
-                   /*
-                    *  endAnchor has seen it, it is effectively empty
-                    *  after our trimming, but endAnchor has for some
-                    *  reason not recognized this.  In other words,
-                    *  this should not happen.
-                    *  Should we not adjust the extent and let it "leak"
-                    *  into the new line?
-                    */
-                   new_pos = (a0 < s) ? s_pre :
-                       s_pre + 1;
-                   if ( prev_a
-                        && prev_a->line_num == CurLine
-                        && new_pos < prev_a->line_pos ) {
-                       if (prev_a->line_pos <= s_pre + 1 + SpecialAttrChars)
-                           new_pos = prev_a->line_pos;
-                       else
-                           new_pos = s_pre + 1 + SpecialAttrChars;
-                   }
-                   new_ext = 0;
-               } else if (a0 >= s_post) {
-                   CTRACE_SPLITLINE((tfp, "anchor %d case %d: ", a->number,3));
-                   /*
-                    *  Completely after split, just shift.
-                    */
-                   new_pos = a0 - TailTrim + 1 - HeadTrim + SpecialAttrChars;
-               } else if (!old_e) {
-                   CTRACE_SPLITLINE((tfp, "anchor %d case %d: ", a->number,4));
-                   /*
-                    *  No extent set, we may still be growing it.
-                    */
-                   new_pos = s_pre + 1 + SpecialAttrChars;
-
-                   /*
-                    *  Ok, it's neither empty, nor is it completely
-                    *  before or after the split region (including trimmed
-                    *  stuff).  So the anchor is either being split in
-                    *  the middle, with stuff remaining on both lines,
-                    *  or something is being nibbled off, either at
-                    *  the end (anchor stays on previous line) or at
-                    *  the beginning (anchor is on new line).  Let's
-                    *  try in that order.
-                    */
-               } else if (a0 < s_pre &&
-                          a1 > s_post) {
-                   CTRACE_SPLITLINE((tfp, "anchor %d case %d: ", a->number,5));
-                   new_pos = a0;
-                   new_ext = old_e - TailTrim - HeadTrim + SpecialAttrChars;
-               } else if (a0 < s_pre) {
-                   CTRACE_SPLITLINE((tfp, "anchor %d case %d: ", a->number,6));
-                   new_pos = a0;
-                   new_ext = s_pre - a0;
-               } else if (a1 > s_post) {
-                   CTRACE_SPLITLINE((tfp, "anchor %d case %d: ", a->number,7));
-                   new_pos = s_pre + 1 + SpecialAttrChars;
-                   new_ext = old_e - (s_post - a0);
-               } else {
-                   CTRACE((tfp, "split_line anchor %d line %d: This should not 
happen!\n",
-                          a->number, a->line_num));
-                   CTRACE((tfp,
-                          "anchor %d: (T,H,S)=(%d,%d,%d); 
(line,pos,ext):(%d,%d,%d)!\n",
-                          a->number,
-                          TailTrim,HeadTrim,SpecialAttrChars,
-                          a->line_num,a->line_pos,a->extent));
+               } else if (start < s && !len
+                          && (!n || a->show_anchor && !moved)) {
+                   CTRACE_SPLITLINE((tfp, "anchor %d: no relocation, 
empty-finished",
+                                     n));
+                   a->line_pos = s_pre; /* Leave at the end of line */
                    continue;
                }
-               CTRACE_SPLITLINE((tfp, "(T,H,S)=(%d,%d,%d); 
(line,pos,ext):(%d,%d,%d",
-                      TailTrim,HeadTrim,SpecialAttrChars,
+
+               /* The rest we relocate */
+               moved = 1;
+               a->line_num++;
+               CTRACE_SPLITLINE((tfp, "anchor %d: (T,H,S)=(%d,%d,%d); 
(line,pos,ext):(%d,%d,%d), ",
+                      n, TailTrim,HeadTrim,SpecialAttrChars,
                       a->line_num,a->line_pos,a->extent));
-               if (new_pos > s_pre) {
-                   new_pos -= s_pre + 1;
-                   a->line_num = text->Lines;
+               if (end < s_post) {     /* Move the end to s_post */
+                   CTRACE_SPLITLINE((tfp, "Move end +%d, ", s_post - end));
+                   len += s_post - end;
+               }
+               if (start < s_post) {   /* Move the start to s_post */
+                   CTRACE_SPLITLINE((tfp, "Move start +%d, ", s_post - start));
+                   len -= s_post - start;
+                   start = s_post;
                }
-               a->line_pos = new_pos;
-               a->extent = new_ext;
+               a->line_pos = start - s_post + SpecialAttrChars;
+               a->extent = len;
 
-               CTRACE_SPLITLINE((tfp, "))->(%d,%d,%d)\n",
+               CTRACE_SPLITLINE((tfp, "->(%d,%d,%d)\n",
                       a->line_num,a->line_pos,a->extent));
-           }
+           } else if (a->line_num > CurLine)
+               break;
        }
     }
 
@@ -6004,8 +5953,8 @@ PUBLIC int HTGetLinkInfo ARGS6(
         *  and don't count towards nlinks. - KW
         */
        if ((a->show_anchor) &&
-           (a->link_type != INPUT_ANCHOR ||
-            a->input_field->type != F_HIDDEN_TYPE)) {
+           !(a->link_type == INPUT_ANCHOR
+             && a->input_field->type == F_HIDDEN_TYPE)) {
            if (a->line_num == prev_anchor_line) {
                anchors_this_line++;
            } else {
@@ -6280,8 +6229,8 @@ PUBLIC int HTGetLinkOrFieldStart ARGS5(
         *  and don't count towards nlinks. - KW
         */
        if ((a->show_anchor) &&
-           (a->link_type != INPUT_ANCHOR ||
-            a->input_field->type != F_HIDDEN_TYPE)) {
+           !(a->link_type == INPUT_ANCHOR
+             && a->input_field->type == F_HIDDEN_TYPE)) {
            if (a->line_num == prev_anchor_line) {
                anchors_this_line++;
            } else {
@@ -6832,8 +6781,8 @@ PUBLIC int HText_LinksInLines ARGS3(
        if (Anchor_ptr->line_num >= start &&
            Anchor_ptr->line_num < end &&
            Anchor_ptr->show_anchor &&
-           (Anchor_ptr->link_type != INPUT_ANCHOR ||
-            Anchor_ptr->input_field->type != F_HIDDEN_TYPE))
+           !(Anchor_ptr->link_type == INPUT_ANCHOR
+             && Anchor_ptr->input_field->type == F_HIDDEN_TYPE))
            ++total;
        if (Anchor_ptr == text->last_anchor)
            break;
@@ -7754,8 +7703,8 @@ PRIVATE int www_user_search_internals AR
     for (;;) {
        while ((a != NULL) && a->line_num == (*count - 1)) {
            if (a->show_anchor &&
-               (a->link_type != INPUT_ANCHOR ||
-                a->input_field->type != F_HIDDEN_TYPE)) {
+               !(a->link_type == INPUT_ANCHOR
+                 && a->input_field->type == F_HIDDEN_TYPE)) {
                if (((a->hightext != NULL && case_sensitive == TRUE) &&
                     LYno_attr_char_strstr(a->hightext, target)) ||
                    ((a->hightext != NULL && case_sensitive == FALSE) &&
@@ -9241,27 +9190,6 @@ PUBLIC char * HText_setLastOptionValue A
 }
 
 /*
- * Count the number of anchors on the current line so we can allow for the
- * length of numbered fields.
- */
-PRIVATE int AnchorsOnThisLine ARGS2(
-       HText *,        txt,
-       TextAnchor *,   ank)
-{
-    TextAnchor *chk = txt->first_anchor;
-    int count = 1;
-
-    while (chk != 0
-       && chk != txt->last_anchor
-       && chk != ank) {
-       if (chk->line_num == ank->line_num)
-           count++;
-       chk = chk->next;
-    }
-    return count;
-}
-
-/*
  *  Assign a form input anchor.
  *  Returns the number of characters to leave
  *  blank so that the input field can fit.
@@ -9678,12 +9606,14 @@ PUBLIC int HText_beginInput ARGS3(
                          (int)text->style->leftIndent -
                          (int)text->style->rightIndent;
 
-           /*
-            *  If we are numbering form links, take that into
-            *  account as well. - FM
-            */
+           /*  If we are numbering form links, place is taken by [nn]  */
            if (keypad_mode == LINKS_AND_FIELDS_ARE_NUMBERED)
-               MaximumSize -= AnchorsOnThisLine(text, a) / 10 + 3;
+               MaximumSize -= (a->number >= 10 /*Buggy if 1e6 links, sowhat?*/
+                               ? (a->number >= 100
+                                  ? (a->number >= 1000
+                                     ? (a->number >= 10000
+                                        ? (a->number >= 100000
+                                           ? 6 : 5) : 4) : 3) : 2) : 1) + 2;
            if (f->size > MaximumSize)
                f->size = MaximumSize;
 

; To UNSUBSCRIBE: Send "unsubscribe lynx-dev" to address@hidden

reply via email to

[Prev in Thread] Current Thread [Next in Thread]