Implemented an index. Good _grief_, that was hard work to get all
[sgt/halibut] / input.c
diff --git a/input.c b/input.c
index 4c3b10d..cb6cf9c 100644 (file)
--- a/input.c
+++ b/input.c
@@ -528,6 +528,7 @@ static void read_file(paragraph ***ret, input *in, indexdata *idx) {
        } type;
        word **whptr;                  /* to restore from \u alternatives */
        word **idximplicit;            /* to restore from \u alternatives */
+       filepos fpos;
     } *sitem;
     stack parsestk;
     struct crossparaitem {
@@ -592,14 +593,17 @@ static void read_file(paragraph ***ret, input *in, indexdata *idx) {
                     */
                    dtor(t), t = get_token(in);
                }
-               if (t.type == tok_eop || t.type == tok_eof)
+               if (t.type == tok_eop || t.type == tok_eof ||
+                   t.type == tok_rbrace) { /* might be } terminating \lcont */
+                   if (t.type == tok_rbrace)
+                       already = TRUE;
                    break;
-               else if (t.type == tok_cmd && t.cmd == c_c)
+               } else if (t.type == tok_cmd && t.cmd == c_c) {
                    wtype = word_WeakCode;
-               else if (t.type == tok_cmd && t.cmd == c_e &&
-                        wtype == word_WeakCode)
+               else if (t.type == tok_cmd && t.cmd == c_e &&
+                          wtype == word_WeakCode) {
                    wtype = word_Emph;
-               else {
+               else {
                    error(err_brokencodepara, &t.pos);
                    prev_para_type = par.type;
                    addpara(par, ret);
@@ -633,6 +637,16 @@ static void read_file(paragraph ***ret, input *in, indexdata *idx) {
                continue;
            }
 
+           /*
+            * Also expect, and swallow, any whitespace after that
+            * (a newline before a code paragraph wouldn't be
+            * surprising).
+            */
+           do {
+               dtor(t), t = get_token(in);
+           } while (t.type == tok_white);
+           already = TRUE;
+
            if (cmd == c_lcont) {
                /*
                 * \lcont causes a continuation of a list item into
@@ -975,6 +989,7 @@ static void read_file(paragraph ***ret, input *in, indexdata *idx) {
                /* Error recovery: push nop */
                sitem = mknew(struct stack_item);
                sitem->type = stack_nop;
+               sitem->fpos = t.pos;
                stk_push(parsestk, sitem);
                break;
              case tok_rbrace:
@@ -1011,7 +1026,8 @@ static void read_file(paragraph ***ret, input *in, indexdata *idx) {
                        }
                        indexing = FALSE;
                        rdadd(&indexstr, L'\0');
-                       index_merge(idx, FALSE, indexstr.text, idxwordlist);
+                       index_merge(idx, FALSE, indexstr.text,
+                                   idxwordlist, &sitem->fpos);
                        sfree(indexstr.text);
                    }
                    if (sitem->type & stack_hyper) {
@@ -1097,6 +1113,7 @@ static void read_file(paragraph ***ret, input *in, indexdata *idx) {
                            addword(wd, &idximplicit);
                        }
                        sitem = mknew(struct stack_item);
+                       sitem->fpos = t.pos;
                        sitem->type = stack_quote;
                        stk_push(parsestk, sitem);
                    }
@@ -1176,6 +1193,7 @@ static void read_file(paragraph ***ret, input *in, indexdata *idx) {
                         * Special cases: \W{}\c, \W{}\e, \W{}\cw
                         */
                        sitem = mknew(struct stack_item);
+                       sitem->fpos = wd.fpos;
                        sitem->type = stack_hyper;
                        if (t.type == tok_cmd &&
                            (t.cmd == c_e || t.cmd == c_c || t.cmd == c_cw)) {
@@ -1207,6 +1225,7 @@ static void read_file(paragraph ***ret, input *in, indexdata *idx) {
                        /* Error recovery: eat lbrace, push nop. */
                        dtor(t), t = get_token(in);
                        sitem = mknew(struct stack_item);
+                       sitem->fpos = t.pos;
                        sitem->type = stack_nop;
                        stk_push(parsestk, sitem);
                    }
@@ -1219,6 +1238,7 @@ static void read_file(paragraph ***ret, input *in, indexdata *idx) {
                                 word_Emph);
                        spcstyle = tospacestyle(style);
                        sitem = mknew(struct stack_item);
+                       sitem->fpos = t.pos;
                        sitem->type = stack_style;
                        stk_push(parsestk, sitem);
                    }
@@ -1232,10 +1252,12 @@ static void read_file(paragraph ***ret, input *in, indexdata *idx) {
                        /* Error recovery: eat lbrace, push nop. */
                        dtor(t), t = get_token(in);
                        sitem = mknew(struct stack_item);
+                       sitem->fpos = t.pos;
                        sitem->type = stack_nop;
                        stk_push(parsestk, sitem);
                    }
                    sitem = mknew(struct stack_item);
+                   sitem->fpos = t.pos;
                    sitem->type = stack_idx;
                    dtor(t), t = get_token(in);
                    /*
@@ -1305,6 +1327,7 @@ static void read_file(paragraph ***ret, input *in, indexdata *idx) {
                         * paragraph.
                         */
                        sitem = mknew(struct stack_item);
+                       sitem->fpos = t.pos;
                        sitem->type = stack_ualt;
                        sitem->whptr = whptr;
                        sitem->idximplicit = idximplicit;