Back to index

tetex-bin  3.0
multi.c
Go to the documentation of this file.
00001 /* multi.c -- multiple-column tables (@multitable) for makeinfo.
00002    $Id: multi.c,v 1.8 2004/04/11 17:56:47 karl Exp $
00003 
00004    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2004 Free Software
00005    Foundation, Inc.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2, or (at your option)
00010    any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program; if not, write to the Free Software Foundation,
00019    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00020    
00021    Originally written by phr@gnu.org (Paul Rubin).  */
00022 
00023 #include "system.h"
00024 #include "cmds.h"
00025 #include "insertion.h"
00026 #include "makeinfo.h"
00027 #include "multi.h"
00028 #include "xml.h"
00029 
00030 #define MAXCOLS 100             /* remove this limit later @@ */
00031 
00032 
00033 /*
00034  * Output environments.  This is a hack grafted onto existing
00035  * structure.  The "output environment" used to consist of the
00036  * global variables `output_paragraph', `fill_column', etc.
00037  * Routines like add_char would manipulate these variables.
00038  *
00039  * Now, when formatting a multitable, we maintain separate environments
00040  * for each column.  That way we can build up the columns separately
00041  * and write them all out at once.  The "current" output environment"
00042  * is still kept in those global variables, so that the old output
00043  * routines don't have to change.  But we provide routines to save
00044  * and restore these variables in an "environment table".  The
00045  * `select_output_environment' function switches from one output
00046  * environment to another.
00047  *
00048  * Environment #0 (i.e., element #0 of the table) is the regular
00049  * environment that is used when we're not formatting a multitable.
00050  *
00051  * Environment #N (where N = 1,2,3,...) is the env. for column #N of
00052  * the table, when a multitable is active.
00053  */
00054 
00055 /* contents of an output environment */
00056 /* some more vars may end up being needed here later @@ */
00057 struct env
00058 {
00059   unsigned char *output_paragraph;
00060   int output_paragraph_offset;
00061   int meta_char_pos;
00062   int output_column;
00063   int paragraph_is_open;
00064   int current_indent;
00065   int fill_column;
00066 } envs[MAXCOLS];                /* the environment table */
00067 
00068 /* index in environment table of currently selected environment */
00069 static int current_env_no;
00070 
00071 /* current column number */
00072 static int current_column_no;
00073 
00074 /* We need to make a difference between template based widths and
00075    @columnfractions for HTML tables' sake.  Sigh.  */
00076 static int seen_column_fractions;
00077 
00078 /* column number of last column in current multitable */
00079 static int last_column;
00080 
00081 /* flags indicating whether horizontal and vertical separators need
00082    to be drawn, separating rows and columns in the current multitable. */
00083 static int hsep, vsep;
00084 
00085 /* whether this is the first row. */
00086 static int first_row;
00087 
00088 /* Called to handle a {...} template on the @multitable line.
00089    We're at the { and our first job is to find the matching }; as a side
00090    effect, we change *PARAMS to point to after it.  Our other job is to
00091    expand the template text and return the width of that string.  */
00092 static unsigned
00093 find_template_width (char **params)
00094 {
00095   char *template, *xtemplate;
00096   unsigned len;
00097   char *start = *params;
00098   int brace_level = 0;
00099 
00100   /* The first character should be a {.  */
00101   if (!params || !*params || **params != '{')
00102     {
00103       line_error ("find_template width internal error: passed %s",
00104                   params ? *params : "null");
00105       return 0;
00106     }
00107 
00108   do
00109     {
00110       if (**params == '{' && (*params == start || (*params)[-1] != '@'))
00111         brace_level++;
00112       else if (**params == '}' && (*params)[-1] != '@')
00113         brace_level--;
00114       else if (**params == 0)
00115         {
00116           line_error (_("Missing } in @multitable template"));
00117           return 0;
00118         }
00119       (*params)++;
00120     }
00121   while (brace_level > 0);
00122   
00123   template = substring (start + 1, *params - 1); /* omit braces */
00124   xtemplate = expansion (template, 0);
00125   len = strlen (xtemplate);
00126   
00127   free (template);
00128   free (xtemplate);
00129   
00130   return len;
00131 }
00132 
00133 /* Direct current output to environment number N.  Used when
00134    switching work from one column of a multitable to the next.
00135    Returns previous environment number. */
00136 static int 
00137 select_output_environment (int n)
00138 {
00139   struct env *e = &envs[current_env_no];
00140   int old_env_no = current_env_no;
00141 
00142   /* stash current env info from global vars into the old environment */
00143   e->output_paragraph = output_paragraph;
00144   e->output_paragraph_offset = output_paragraph_offset;
00145   e->meta_char_pos = meta_char_pos;
00146   e->output_column = output_column;
00147   e->paragraph_is_open = paragraph_is_open;
00148   e->current_indent = current_indent;
00149   e->fill_column = fill_column;
00150 
00151   /* now copy new environment into global vars */
00152   current_env_no = n;
00153   e = &envs[current_env_no];
00154   output_paragraph = e->output_paragraph;
00155   output_paragraph_offset = e->output_paragraph_offset;
00156   meta_char_pos = e->meta_char_pos;
00157   output_column = e->output_column;
00158   paragraph_is_open = e->paragraph_is_open;
00159   current_indent = e->current_indent;
00160   fill_column = e->fill_column;
00161   return old_env_no;
00162 }
00163 
00164 /* Initialize environment number ENV_NO, of width WIDTH.
00165    The idea is that we're going to use one environment for each column of
00166    a multitable, so we can build them up separately and print them
00167    all out at the end. */
00168 static int
00169 setup_output_environment (int env_no, int width)
00170 {
00171   int old_env = select_output_environment (env_no);
00172 
00173   /* clobber old environment and set width of new one */
00174   init_paragraph ();
00175 
00176   /* make our change */
00177   fill_column = width;
00178 
00179   /* Save new environment and restore previous one. */
00180   select_output_environment (old_env);
00181 
00182   return env_no;
00183 }
00184 
00185 /* Read the parameters for a multitable from the current command
00186    line, save the parameters away, and return the
00187    number of columns. */
00188 static int
00189 setup_multitable_parameters (void)
00190 {
00191   char *params = insertion_stack->item_function;
00192   int nchars;
00193   float columnfrac;
00194   char command[200]; /* xx no fixed limits */
00195   int i = 1;
00196 
00197   /* We implement @hsep and @vsep even though TeX doesn't.
00198      We don't get mixing of @columnfractions and templates right,
00199      but TeX doesn't either.  */
00200   hsep = vsep = 0;
00201 
00202   /* Assume no @columnfractions per default.  */
00203   seen_column_fractions = 0;
00204 
00205   while (*params) {
00206     while (whitespace (*params))
00207       params++;
00208 
00209     if (*params == '@') {
00210       sscanf (params, "%200s", command);
00211       nchars = strlen (command);
00212       params += nchars;
00213       if (strcmp (command, "@hsep") == 0)
00214         hsep++;
00215       else if (strcmp (command, "@vsep") == 0)
00216         vsep++;
00217       else if (strcmp (command, "@columnfractions") == 0) {
00218         seen_column_fractions = 1;
00219         /* Clobber old environments and create new ones, starting at #1.
00220            Environment #0 is the normal output, so don't mess with it. */
00221         for ( ; i <= MAXCOLS; i++) {
00222           if (sscanf (params, "%f", &columnfrac) < 1)
00223             goto done;
00224           /* Unfortunately, can't use %n since m68k-hp-bsd libc (at least)
00225              doesn't support it.  So skip whitespace (preceding the
00226              number) and then non-whitespace (the number).  */
00227           while (*params && (*params == ' ' || *params == '\t'))
00228             params++;
00229           /* Hmm, but what about @columnfractions 3foo.  Oh well, 
00230              it's invalid input anyway.  */
00231           while (*params && *params != ' ' && *params != '\t'
00232                  && *params != '\n' && *params != '@')
00233             params++;
00234 
00235           {
00236             /* For html/xml/docbook, translate fractions into integer
00237                percentages, adding .005 to avoid rounding problems.  For
00238                info, we want the character width.  */
00239             int width = xml || html ? (columnfrac + .005) * 100
00240                         : (columnfrac * (fill_column - current_indent) + .5);
00241             setup_output_environment (i, width);
00242           }
00243         }
00244       }
00245 
00246     } else if (*params == '{') {
00247       unsigned template_width = find_template_width (&params);
00248 
00249       /* This gives us two spaces between columns.  Seems reasonable.
00250          How to take into account current_indent here?  */
00251       setup_output_environment (i++, template_width + 2);
00252       
00253     } else {
00254       warning (_("ignoring stray text `%s' after @multitable"), params);
00255       break;
00256     }
00257   }
00258 
00259 done:
00260   flush_output ();
00261   inhibit_output_flushing ();
00262 
00263   last_column = i - 1;
00264   return last_column;
00265 }
00266 
00267 /* Output a row.  Calls insert, but also flushes the buffered output
00268    when we see a newline, since in multitable every line is a separate
00269    paragraph.  */
00270 static void
00271 out_char (int ch)
00272 {
00273   if (html || xml)
00274     add_char (ch);
00275   else
00276     {
00277       int env = select_output_environment (0);
00278       insert (ch);
00279       if (ch == '\n')
00280        {
00281          uninhibit_output_flushing ();
00282          flush_output ();
00283          inhibit_output_flushing ();
00284        }
00285       select_output_environment (env);
00286     }
00287 }
00288 
00289 
00290 static void
00291 draw_horizontal_separator (void)
00292 {
00293   int i, j, s;
00294 
00295   if (html)
00296     {
00297       add_word ("<hr>");
00298       return;
00299     }
00300   if (xml)
00301     return;
00302 
00303   for (s = 0; s < envs[0].current_indent; s++)
00304     out_char (' ');
00305   if (vsep)
00306     out_char ('+');
00307   for (i = 1; i <= last_column; i++) {
00308     for (j = 0; j <= envs[i].fill_column; j++)
00309       out_char ('-');
00310     if (vsep)
00311       out_char ('+');
00312   }
00313   out_char (' ');
00314   out_char ('\n');
00315 }
00316 
00317 
00318 /* multitable strategy:
00319     for each item {
00320        for each column in an item {
00321         initialize a new paragraph
00322         do ordinary formatting into the new paragraph
00323         save the paragraph away
00324         repeat if there are more paragraphs in the column
00325       }
00326       dump out the saved paragraphs and free the storage
00327     }
00328 
00329    For HTML we construct a simple HTML 3.2 table with <br>s inserted
00330    to help non-tables browsers.  `@item' inserts a <tr> and `@tab'
00331    inserts <td>; we also try to close <tr>.  The only real
00332    alternative is to rely on the info formatting engine and present
00333    preformatted text.  */
00334 
00335 void
00336 do_multitable (void)
00337 {
00338   int ncolumns;
00339 
00340   if (multitable_active)
00341     {
00342       line_error ("Multitables cannot be nested");
00343       return;
00344     }
00345 
00346   close_single_paragraph ();
00347 
00348   if (xml)
00349     {
00350       xml_no_para = 1;
00351       if (output_paragraph[output_paragraph_offset-1] == '\n')
00352         output_paragraph_offset--;
00353     }
00354 
00355   /* scan the current item function to get the field widths
00356      and number of columns, and set up the output environment list
00357      accordingly. */
00358   ncolumns = setup_multitable_parameters ();
00359   first_row = 1;
00360 
00361   /* <p> for non-tables browsers.  @multitable implicitly ends the
00362      current paragraph, so this is ok.  */
00363   if (html)
00364     add_html_block_elt ("<p><table summary=\"\">");
00365   /*  else if (docbook)*/ /* 05-08 */
00366   else if (xml)
00367     {
00368       int *widths = xmalloc (ncolumns * sizeof (int));
00369       int i;
00370       for (i=0; i<ncolumns; i++)
00371        widths[i] = envs[i+1].fill_column;
00372       xml_begin_multitable (ncolumns, widths);
00373       free (widths);
00374     }
00375 
00376   if (hsep)
00377     draw_horizontal_separator ();
00378 
00379   /* The next @item command will direct stdout into the first column
00380      and start processing.  @tab will then switch to the next column,
00381      and @item will flush out the saved output and return to the first
00382      column.  Environment #1 is the first column.  (Environment #0 is
00383      the normal output) */
00384 
00385   ++multitable_active;
00386 }
00387 
00388 /* advance to the next environment number */
00389 static void
00390 nselect_next_environment (void)
00391 {
00392   if (current_env_no >= last_column) {
00393     line_error (_("Too many columns in multitable item (max %d)"), last_column);
00394     return;
00395   }
00396   select_output_environment (current_env_no + 1);
00397 }
00398 
00399 
00400 /* do anything needed at the beginning of processing a
00401    multitable column. */
00402 static void
00403 init_column (void)
00404 {
00405   /* don't indent 1st paragraph in the item */
00406   cm_noindent ();
00407 
00408   /* throw away possible whitespace after @item or @tab command */
00409   skip_whitespace ();
00410 }
00411 
00412 static void
00413 output_multitable_row (void)
00414 {
00415   /* offset in the output paragraph of the next char needing
00416      to be output for that column. */
00417   int offset[MAXCOLS];
00418   int i, j, s, remaining;
00419   int had_newline = 0;
00420 
00421   for (i = 0; i <= last_column; i++)
00422     offset[i] = 0;
00423 
00424   /* select the current environment, to make sure the env variables
00425      get updated */
00426   select_output_environment (current_env_no);
00427 
00428 #define CHAR_ADDR(n) (offset[i] + (n))
00429 #define CHAR_AT(n) (envs[i].output_paragraph[CHAR_ADDR(n)])
00430 
00431   /* remove trailing whitespace from each column */
00432   for (i = 1; i <= last_column; i++) {
00433     if (envs[i].output_paragraph_offset)
00434       while (cr_or_whitespace (CHAR_AT (envs[i].output_paragraph_offset - 1)))
00435         envs[i].output_paragraph_offset--;
00436 
00437     if (i == current_env_no)
00438       output_paragraph_offset = envs[i].output_paragraph_offset;
00439   }
00440 
00441   /* read the current line from each column, outputting them all
00442      pasted together.  Do this til all lines are output from all
00443      columns.  */
00444   for (;;) {
00445     remaining = 0;
00446     /* first, see if there is any work to do */
00447     for (i = 1; i <= last_column; i++) {
00448       if (CHAR_ADDR (0) < envs[i].output_paragraph_offset) {
00449         remaining = 1;
00450         break;
00451       }
00452     }
00453     if (!remaining)
00454       break;
00455     
00456     for (s = 0; s < envs[0].current_indent; s++)
00457       out_char (' ');
00458     
00459     if (vsep)
00460       out_char ('|');
00461 
00462     for (i = 1; i <= last_column; i++) {
00463       for (s = 0; s < envs[i].current_indent; s++)
00464         out_char (' ');
00465       for (j = 0; CHAR_ADDR (j) < envs[i].output_paragraph_offset; j++) {
00466         if (CHAR_AT (j) == '\n')
00467           break;
00468         out_char (CHAR_AT (j));
00469       }
00470       offset[i] += j + 1;       /* skip last text plus skip the newline */
00471       
00472       /* Do not output trailing blanks if we're in the last column and
00473          there will be no trailing |.  */
00474       if (i < last_column && !vsep)
00475         for (; j <= envs[i].fill_column; j++)
00476           out_char (' ');
00477       if (vsep)
00478         out_char ('|'); /* draw column separator */
00479     }
00480     out_char ('\n');    /* end of line */
00481     had_newline = 1;
00482   }
00483   
00484   /* If completely blank item, get blank line despite no other output.  */
00485   if (!had_newline)
00486     out_char ('\n');    /* end of line */
00487 
00488   if (hsep)
00489     draw_horizontal_separator ();
00490 
00491   /* Now dispose of the buffered output. */
00492   for (i = 1; i <= last_column; i++) {
00493     select_output_environment (i);
00494     init_paragraph ();
00495   }
00496 }
00497 
00498 int after_headitem = 0;
00499 int headitem_row = 0;
00500 
00501 /* start a new item (row) of a multitable */
00502 int
00503 multitable_item (void)
00504 {
00505   if (!multitable_active) {
00506     line_error ("multitable_item internal error: no active multitable");
00507     xexit (1);
00508   }
00509 
00510   current_column_no = 1;
00511 
00512   if (html)
00513     {
00514       if (!first_row)
00515         /* <br> for non-tables browsers. */
00516        add_word_args ("<br></%s></tr>", after_headitem ? "th" : "td");
00517 
00518       if (seen_column_fractions)
00519         add_word_args ("<tr align=\"left\"><%s valign=\"top\" width=\"%d%%\">",
00520             headitem_flag ? "th" : "td",
00521             envs[current_column_no].fill_column);
00522       else
00523         add_word_args ("<tr align=\"left\"><%s valign=\"top\">",
00524             headitem_flag ? "th" : "td");
00525 
00526       if (headitem_flag)
00527         after_headitem = 1;
00528       else
00529         after_headitem = 0;
00530       first_row = 0;
00531       headitem_row = headitem_flag;
00532       headitem_flag = 0;
00533       return 0;
00534     }
00535   /*  else if (docbook)*/ /* 05-08 */
00536   else if (xml)
00537     {
00538       xml_end_multitable_row (first_row);
00539       if (headitem_flag)
00540         after_headitem = 1;
00541       else
00542         after_headitem = 0;
00543       first_row = 0;
00544       headitem_flag = 0;
00545       return 0;
00546     }
00547   first_row = 0;
00548 
00549   if (current_env_no > 0) {
00550     output_multitable_row ();
00551   }
00552   /* start at column 1 */
00553   select_output_environment (1);
00554   if (!output_paragraph) {
00555     line_error (_("[unexpected] cannot select column #%d in multitable"),
00556                 current_env_no);
00557     xexit (1);
00558   }
00559 
00560   init_column ();
00561 
00562   if (headitem_flag)
00563     hsep = 1;
00564   else
00565     hsep = 0;
00566 
00567   if (headitem_flag)
00568     after_headitem = 1;
00569   else
00570     after_headitem = 0;
00571   headitem_flag = 0;
00572 
00573   return 0;
00574 }
00575 
00576 #undef CHAR_AT
00577 #undef CHAR_ADDR
00578 
00579 /* select a new column in current row of multitable */
00580 void
00581 cm_tab (void)
00582 {
00583   if (!multitable_active)
00584     error (_("ignoring @tab outside of multitable"));
00585 
00586   current_column_no++;
00587   
00588   if (html)
00589     {
00590       if (seen_column_fractions)
00591         add_word_args ("</%s><%s valign=\"top\" width=\"%d%%\">",
00592             headitem_row ? "th" : "td",
00593             headitem_row ? "th" : "td",
00594             envs[current_column_no].fill_column);
00595       else
00596         add_word_args ("</%s><%s valign=\"top\">",
00597             headitem_row ? "th" : "td",
00598             headitem_row ? "th" : "td");
00599     }
00600   /*  else if (docbook)*/ /* 05-08 */
00601   else if (xml)
00602     xml_end_multitable_column ();
00603   else
00604     nselect_next_environment ();
00605 
00606   init_column ();
00607 }
00608 
00609 /* close a multitable, flushing its output and resetting
00610    whatever needs resetting */
00611 void
00612 end_multitable (void)
00613 {
00614   if (!html && !docbook) 
00615     output_multitable_row ();
00616 
00617   /* Multitables cannot be nested.  Otherwise, we'd have to save the
00618      previous output environment number on a stack somewhere, and then
00619      restore to that environment.  */
00620   select_output_environment (0);
00621   multitable_active = 0;
00622   uninhibit_output_flushing ();
00623   close_insertion_paragraph ();
00624 
00625   if (html)
00626     add_word_args ("<br></%s></tr></table>\n", headitem_row ? "th" : "td");
00627   /*  else if (docbook)*/ /* 05-08 */
00628   else if (xml)
00629     xml_end_multitable ();
00630 
00631 #if 0
00632   printf (_("** Multicolumn output from last row:\n"));
00633   for (i = 1; i <= last_column; i++) {
00634     select_output_environment (i);
00635     printf (_("* column #%d: output = %s\n"), i, output_paragraph);
00636   }
00637 #endif
00638 }