![]() |
Editing tables | ![]() |
A table is a table node whose children are row nodes, whose children are cell nodes. Formatting is attached by wrapping any of these nodes in a tformat node: all children of a tformat except the last one are formatting instructions, and the last one is the formatted table, row or cell (which may itself be a tformat). Two kinds of instructions exist:
(var, val) sets a property of the whole table, such as table-width or table-min-rows.
(i1, i2, j1, j2, var, val) sets a cell property for the rows i1 to i2 and the columns j1 to j2. Positive indices count from 1 at the top or left, negative ones from -1 at the bottom or right, so that (1, -1, 2, 2) means “the whole second column” whatever the number of rows.
Tabular environments of the style files (tabular, matrix, eqnarray*, …) are macros whose body contains a tformat applied to their argument; they supply default formats.
Inside
Edit/Modify/edit_table.cpp
, rows and columns are numbered from 0, and the cwith indices are converted with with_decode (positive index \(k\) becomes \(k - 1\), negative index \(- k\) becomes \(n - k\)). The public routines exported to Scheme use the 1-based convention of the format.
All public routines start from the cursor. The protected helpers are
The innermost table around the cursor.
The same, and the coordinates of the cell containing the cursor (skipping tformat nodes around rows and cells).
The tformat around that table, if there is one. The second variant inserts an empty tformat when there is none, so that the caller can add formats.
The paths of a row and of the content of a cell (inside the cell node).
Most public routines call search_format (row, col) and silently do nothing when the cursor is not inside a table.
All formats which apply to the table: the inherited cell-format of the environment followed by the instructions of the tformat at fp.
For a table property (twith): set replaces an existing instruction, get returns the last one which applies or the environment value.
For cell properties (cwith) over a rectangle. Getting returns the value of the last instruction whose rectangle contains the requested one; deleting removes the instructions whose rectangle is contained in it.
Splits every cwith for var which covers several cells into one instruction per cell; used before decorating.
Rewrites all cwith indices so that rows and columns before the cursor are counted from the start and those after it from the end (table-format-center).
The exported routines table_set_format (var, val) and friends (table-set-format, …) act on the table around the cursor, and cell_set_format (var, val), cell_get_format and cell_del_format (cell-set-format, …) on the selected cells, or, without a table selection, on the cell, row, column or whole table depending on the cell mode (set_cell_mode, one of cell, row, column, table). When a selection extends to the last row or column, the corresponding index is written as -1, so that the format also applies to rows or columns added later. After each change of a cell format, table_correct_block_content wraps or unwraps the cell contents in a document according to the cell-block and cell-hyphen properties.
table_insert (fp, row, col, nr_rows, nr_cols) and table_remove (fp, row, col, nr_rows, nr_cols) are the primitives: they insert or remove rows and columns of empty cells and then shift the cwith indices so that every format keeps applying to the same cells. Removing all rows or all columns destroys the table (destroy_table, which also removes an enclosing tabular macro or subtable). On top of them:
(table-insert-row, table-insert-column) Insert after or before the current one, within the limits table-max-rows and table-max-cols, and move the cursor there. These are the actions of structured-insert-vertical and structured-insert-horizontal inside tables (
table/table-edit.scm
).
Remove the current or a neighbouring row or column, or destroy the table when the minimum (table-min-rows, table-min-cols) would be violated.
(table-set-extents) Resize to the given size, clamped to the limits.
Queries and cursor movement, with 1-based indices (negative ones count from the end).
After a change of the shape, table_resize_notify calls the Scheme hook table-resize-notify, which does nothing by default and is overloaded by spreadsheets (
dynamic/calc-table.scm
) to update their cells.
Tables are normally created through make: when the macro definition of a tag contains a tformat applied to one of its arguments, make_compound inserts the tag and then calls make_table (1, 1) inside it (see making compound structures). make_table (rows, cols) inserts a tformat around an empty table, enlarges it to the minimal size given by the formats, puts the table in a paragraph of its own when table-hyphen or table-block asks for it, and corrects the block contents. make_subtable (make-subtable) replaces the contents of the current cell by a subtable.
table_bound (fp, row1, col1, row2, col2) enlarges a rectangle of cells so that it contains every cell which spans into it (cell-row-span, cell-col-span); it is used when moving the cursor and when computing table selections.
table_get_subtable (fp, row1, col1, row2, col2) extracts a rectangle of cells, together with the cwith instructions which apply to it (renumbered relative to the rectangle); this is what is copied when a table selection is copied. table_write_subtable (fp, row, col, subt) writes such a subtable into an existing table at a given position, enlarging the table if needed and shifting the formats of the subtable; pasting a table selection inside a table uses it (see selections and the clipboard). Inside a calc-table, the Scheme function calc-table-renumber adjusts the cell references of a spreadsheet before writing.
Decorations (table_row_decoration, table_column_decoration, table-row-decoration, table-column-decoration) move a neighbouring row or column of cells into the cell-decoration format of the current cells: the cell then displays a small table in which a tmarker stands for its own content.
The deletion algorithm of inserting, deleting and making structure calls back_table when the cursor is just outside a table (it moves into the first or last cell) and back_in_table when it is at the border of a cell. The latter removes the current row if it is entirely empty, then the current column if it is entirely empty, then the whole table if all cells are empty (always within the minimum sizes); otherwise it moves to the previous or next cell, and finally out of the table.
search_format (row, col) modifies the document: it inserts an empty tformat when the table has none, even for pure queries such as table-which-row, so a query may change the document.
In table_get_limits (
Edit/Modify/edit_table.cpp:447
) the test for an unset maximum number of columns compares it with the minimum number of rows: if (j2<i1) should read if (j2<j1). A table whose maximum number of columns is smaller than its minimum number of rows is treated as having no maximum number of columns, and conversely a maximum smaller than the minimum number of columns is not reset.
In table_write_subtable (
edit_table.cpp:847
) the loop which skips tformat nodes around a cell of the subtable indexes the cell with the arity of the row: subc= subc [N(subr)-1] instead of subc [N(subc)-1]. Pasting a subtable whose cells carry their own tformat can therefore pick the wrong child or index out of range.
table_insert and table_remove reuse their parameter row as the counter of the column loop (
edit_table.cpp:467
, 523), and then use it again to shift the row indices of the formats. When rows and columns are inserted in the same call, which only happens through table_set_extents (table-set-extents, used by the table size dialogs), the row shift is computed with the new number of rows instead of the insertion point. For example, a format for the rows 1 to -1 (a whole column) is shifted so that it no longer covers the added rows.
The selection branches of cell_set_format and cell_del_format do not check that selection_get_subtable found a table.