edc78e52efa8890e8910faa739dbdc2f094b7857
[emacs.git] / lisp / textmodes / table.el
blobedc78e52efa8890e8910faa739dbdc2f094b7857
1 ;;; table.el --- create and edit WYSIWYG text based embedded tables -*- lexical-binding: t -*-
3 ;; Copyright (C) 2000-2015 Free Software Foundation, Inc.
5 ;; Keywords: wp, convenience
6 ;; Author: Takaaki Ota <Takaaki.Ota@am.sony.com>
7 ;; Created: Sat Jul 08 2000 13:28:45 (PST)
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; -------------
27 ;; Introduction:
28 ;; -------------
30 ;; This package provides text based table creation and editing
31 ;; feature. With this package Emacs is capable of editing tables that
32 ;; are embedded inside a text document, the feature similar to the
33 ;; ones seen in modern WYSIWYG word processors. A table is a
34 ;; rectangular text area consisting from a surrounding frame and
35 ;; content inside the frame. The content is usually subdivided into
36 ;; multiple rectangular cells, see the actual tables used below in
37 ;; this document. Once a table is recognized, editing operation
38 ;; inside a table cell is confined into that specific cell's
39 ;; rectangular area. This means that typing and deleting characters
40 ;; inside a cell do not affect any outside text but introduces
41 ;; appropriate formatting only to the cell contents. If necessary for
42 ;; accommodating added text in the cell, the cell automatically grows
43 ;; vertically and/or horizontally. The package uses no major mode nor
44 ;; minor mode for its implementation because the subject text is
45 ;; localized within a buffer. Therefore the special behaviors inside
46 ;; a table cells are implemented by using keymap text property
47 ;; instead of buffer wide mode-map.
50 ;; -----------
51 ;; Background:
52 ;; -----------
54 ;; Paul Georgief is one of my best friends. He became an Emacs
55 ;; convert after I recommended him trying it several years ago. Now
56 ;; we both are devoted disciples of Emacsism and elisp cult. One day
57 ;; in his Emacs exploration he asked me "Tak, what is a command to
58 ;; edit tables in Emacs?". This question started my journey of this
59 ;; table package development. May the code be with me! In the
60 ;; software world Emacs is probably one of the longest lifetime record
61 ;; holders. Amazingly there have been no direct support for WYSIWYG
62 ;; table editing tasks in Emacs. Many people must have experienced
63 ;; manipulating existing overwrite-mode and picture-mode for this task
64 ;; and only dreamed of having such a lisp package which supports this
65 ;; specific task directly. Certainly, I have been one of them. The
66 ;; most difficult part of dealing with table editing in Emacs probably
67 ;; is how to realize localized rectangular editing effect. Emacs has
68 ;; no rectangular narrowing mechanism. Existing rect package provides
69 ;; basically kill, delete and yank operations of a rectangle, which
70 ;; internally is a mere list of strings. A simple approach for
71 ;; realizing the localized virtual rectangular operation is combining
72 ;; rect package capability with a temporary buffer. Insertion and
73 ;; deletion of a character to a table cell can be trapped by a
74 ;; function that copies the cell rectangle to a temporary buffer then
75 ;; apply the insertion/deletion to the temporary contents. Then it
76 ;; formats the contents by filling the paragraphs in order to fit it
77 ;; into the original rectangular area and finally copy it back to the
78 ;; original buffer. This simplistic approach has to bear with
79 ;; significant performance hit. As cell grows larger the copying
80 ;; rectangle back and forth between the original buffer and the
81 ;; temporary buffer becomes expensive and unbearably slow. It was
82 ;; completely impractical and an obvious failure. An idea has been
83 ;; borrowed from the original Emacs design to overcome this
84 ;; shortcoming. When the terminal screen update was slow and
85 ;; expensive Emacs employed a clever algorithm to reduce actual screen
86 ;; update by removing redundant redrawing operations. Also the actual
87 ;; redrawing was done only when there was enough idling time. This
88 ;; technique significantly improved the previously mentioned
89 ;; undesirable situation. Now the original buffer's rectangle is
90 ;; copied into a cache buffer only once. Any cell editing operation
91 ;; is done only to the cache contents. When there is enough idling
92 ;; time the original buffer's rectangle is updated with the current
93 ;; cache contents. This delayed operation is implemented by using
94 ;; Emacs's timer function. To reduce the visual awkwardness
95 ;; introduced by the delayed effect the cursor location is updated in
96 ;; real-time as a user types while the cell contents remains the same
97 ;; until the next idling time. A key to the success of this approach
98 ;; is how to maintain cache coherency. As a user moves point in and
99 ;; out of a cell the table buffer contents and the cache buffer
100 ;; contents must be synchronized without a mistake. By observing user
101 ;; action carefully this is possible however not easy. Once this
102 ;; mechanism is firmly implemented the rest of table features grew in
103 ;; relatively painless progression. Those users who are familiar with
104 ;; Emacs internals appreciate this table package more. Because it
105 ;; demonstrates how extensible Emacs is by showing something that
106 ;; appears like a magic. It lets you re-discover the potential of
107 ;; Emacs.
110 ;; -------------
111 ;; Entry Points:
112 ;; -------------
114 ;; If this is the first time for you to try this package, go ahead and
115 ;; load the package by M-x `load-file' RET. Specify the package file
116 ;; name "table.el". Then switch to a new test buffer and issue the
117 ;; command M-x `table-insert' RET. It'll ask you number of columns,
118 ;; number of rows, cell width and cell height. Give some small
119 ;; numbers for each of them. Play with the resulted table for a
120 ;; while. If you have menu system find the item "Table" under "Tools"
121 ;; and "Table" in the menu bar when the point is in a table cell.
122 ;; Some of them are pretty intuitive and you can easily guess what
123 ;; they do. M-x `describe-function' and get the documentation of
124 ;; `table-insert'. The document includes a short tutorial. When you
125 ;; are tired of guessing how it works come back to this document
126 ;; again.
128 ;; To use the package regularly place this file in the site library
129 ;; directory and add the next expression in your init file. Make
130 ;; sure that directory is included in the `load-path'.
132 ;; (require 'table)
134 ;; Have the next expression also, if you want always be ready to edit
135 ;; tables inside text files. This mechanism is analogous to
136 ;; fontification in a sense that tables are recognized at editing time
137 ;; without having table information saved along with the text itself.
139 ;; (add-hook 'text-mode-hook 'table-recognize)
141 ;; Following is a table of entry points and brief description of each
142 ;; of them. The tables below are of course generated and edited by
143 ;; using this package. Not all the commands are bound to keys. Many
144 ;; of them must be invoked by "M-x" (`execute-extended-command')
145 ;; command. Refer to the section "Keymap" below for the commands
146 ;; available from keys.
148 ;; +------------------------------------------------------------------+
149 ;; | User Visible Entry Points |
150 ;; +-------------------------------+----------------------------------+
151 ;; | Function | Description |
152 ;; +-------------------------------+----------------------------------+
153 ;; |`table-insert' |Insert a table consisting of grid |
154 ;; | |of cells by specifying the number |
155 ;; | |of COLUMNS, number of ROWS, cell |
156 ;; | |WIDTH and cell HEIGHT. |
157 ;; +-------------------------------+----------------------------------+
158 ;; |`table-insert-row' |Insert row(s) of cells before the |
159 ;; | |current row that matches the |
160 ;; | |current row structure. |
161 ;; +-------------------------------+----------------------------------+
162 ;; |`table-insert-column' |Insert column(s) of cells before |
163 ;; | |the current column that matches |
164 ;; | |the current column structure. |
165 ;; +-------------------------------+----------------------------------+
166 ;; |`table-delete-row' |Delete row(s) of cells. The row |
167 ;; | |must consist from cells of the |
168 ;; | |same height. |
169 ;; +-------------------------------+----------------------------------+
170 ;; |`table-delete-column' |Delete column(s) of cells. The |
171 ;; | |column must consist from cells of |
172 ;; | |the same width. |
173 ;; +-------------------------------+----------------------------------+
174 ;; |`table-recognize' |Recognize all tables in the |
175 ;; |`table-unrecognize' |current buffer and |
176 ;; | |activate/deactivate them. |
177 ;; +-------------------------------+----------------------------------+
178 ;; |`table-recognize-region' |Recognize all the cells in a |
179 ;; |`table-unrecognize-region' |region and activate/deactivate |
180 ;; | |them. |
181 ;; +-------------------------------+----------------------------------+
182 ;; |`table-recognize-table' |Recognize all the cells in a |
183 ;; |`table-unrecognize-table' |single table and |
184 ;; | |activate/deactivate them. |
185 ;; +-------------------------------+----------------------------------+
186 ;; |`table-recognize-cell' |Recognize a cell. Find a cell |
187 ;; |`table-unrecognize-cell' |which contains the current point |
188 ;; | |and activate/deactivate that cell.|
189 ;; +-------------------------------+----------------------------------+
190 ;; |`table-forward-cell' |Move point to the next Nth cell in|
191 ;; | |a table. |
192 ;; +-------------------------------+----------------------------------+
193 ;; |`table-backward-cell' |Move point to the previous Nth |
194 ;; | |cell in a table. |
195 ;; +-------------------------------+----------------------------------+
196 ;; |`table-span-cell' |Span the current cell toward the |
197 ;; | |specified direction and merge it |
198 ;; | |with the adjacent cell. The |
199 ;; | |direction is right, left, above or|
200 ;; | |below. |
201 ;; +-------------------------------+----------------------------------+
202 ;; |`table-split-cell-vertically' |Split the current cell vertically |
203 ;; | |and create a cell above and a cell|
204 ;; | |below the point location. |
205 ;; +-------------------------------+----------------------------------+
206 ;; |`table-split-cell-horizontally'|Split the current cell |
207 ;; | |horizontally and create a cell on |
208 ;; | |the left and a cell on the right |
209 ;; | |of the point location. |
210 ;; +-------------------------------+----------------------------------+
211 ;; |`table-split-cell' |Split the current cell vertically |
212 ;; | |or horizontally. This is a |
213 ;; | |wrapper command to the other two |
214 ;; | |orientation specific commands. |
215 ;; +-------------------------------+----------------------------------+
216 ;; |`table-heighten-cell' |Heighten the current cell. |
217 ;; +-------------------------------+----------------------------------+
218 ;; |`table-shorten-cell' |Shorten the current cell. |
219 ;; +-------------------------------+----------------------------------+
220 ;; |`table-widen-cell' |Widen the current cell. |
221 ;; +-------------------------------+----------------------------------+
222 ;; |`table-narrow-cell' |Narrow the current cell. |
223 ;; +-------------------------------+----------------------------------+
224 ;; |`table-fixed-width-mode' |Toggle fixed width mode. In the |
225 ;; | |fixed width mode, typing inside a |
226 ;; | |cell never changes the cell width,|
227 ;; | |while in the normal mode the cell |
228 ;; | |width expands automatically in |
229 ;; | |order to prevent a word being |
230 ;; | |folded into multiple lines. Fixed|
231 ;; | |width mode reverses video or |
232 ;; | |underline the cell contents for |
233 ;; | |its indication. |
234 ;; +-------------------------------+----------------------------------+
235 ;; |`table-query-dimension' |Compute and report the current |
236 ;; | |cell dimension, current table |
237 ;; | |dimension and the number of |
238 ;; | |columns and rows in the table. |
239 ;; +-------------------------------+----------------------------------+
240 ;; |`table-generate-source' |Generate the source of the current|
241 ;; | |table in the specified language |
242 ;; | |and insert it into a specified |
243 ;; | |buffer. |
244 ;; +-------------------------------+----------------------------------+
245 ;; |`table-insert-sequence' |Travel cells forward while |
246 ;; | |inserting a specified sequence |
247 ;; | |string into each cell. |
248 ;; +-------------------------------+----------------------------------+
249 ;; |`table-capture' |Convert plain text into a table by|
250 ;; | |capturing the text in the region. |
251 ;; +-------------------------------+----------------------------------+
252 ;; |`table-release' |Convert a table into plain text by|
253 ;; | |removing the frame from a table. |
254 ;; +-------------------------------+----------------------------------+
255 ;; |`table-justify' |Justify the contents of cell(s). |
256 ;; +-------------------------------+----------------------------------+
259 ;; *Note*
261 ;; You may find that some of commonly expected table commands are
262 ;; missing such as copying a row/column and yanking it. Those
263 ;; functions can be obtained through existing Emacs text editing
264 ;; commands. Rows are easily manipulated with region commands and
265 ;; columns can be copied and pasted through rectangle commands. After
266 ;; all a table is still a part of text in the buffer. Only the
267 ;; special behaviors exist inside each cell through text properties.
269 ;; `table-generate-html' which appeared in earlier releases is
270 ;; deprecated in favor of `table-generate-source'. Now HTML is
271 ;; treated as one of the languages used for describing the table's
272 ;; logical structure.
275 ;; -------
276 ;; Keymap:
277 ;; -------
279 ;; Although this package does not use a mode it does use its own
280 ;; keymap inside a table cell by way of keymap text property. Some of
281 ;; the standard basic editing commands bound to certain keys are
282 ;; replaced with the table specific version of corresponding commands.
283 ;; This replacement combination is listed in the constant alist
284 ;; `table-command-remap-alist' declared below. This alist is
285 ;; not meant to be user configurable but mentioned here for your
286 ;; better understanding of using this package. In addition, table
287 ;; cells have some table specific bindings for cell navigation and
288 ;; cell reformation. You can find these additional bindings in the
289 ;; constant `table-cell-bindings'. Those key bound functions are
290 ;; considered as internal functions instead of normal commands,
291 ;; therefore they have special prefix, *table-- instead of table-, for
292 ;; symbols. The purpose of this is to make it easier for a user to
293 ;; use command name completion. There is a "normal hooks" variable
294 ;; `table-cell-map-hook' prepared for users to override the default
295 ;; table cell bindings. Following is the table of predefined default
296 ;; key bound commands inside a table cell. Remember these bindings
297 ;; exist only inside a table cell. When your terminal is a tty, the
298 ;; control modifier may not be available or applicable for those
299 ;; special characters. In this case use "C-cC-c", which is
300 ;; customizable via `table-command-prefix', as the prefix key
301 ;; sequence. This should preceding the following special character
302 ;; without the control modifier. For example, use "C-cC-c|" instead
303 ;; of "C-|".
305 ;; +------------------------------------------------------------------+
306 ;; | Default Bindings in a Table Cell |
307 ;; +-------+----------------------------------------------------------+
308 ;; | Key | Function |
309 ;; +-------+----------------------------------------------------------+
310 ;; | TAB |Move point forward to the beginning of the next cell. |
311 ;; +-------+----------------------------------------------------------+
312 ;; | "C->" |Widen the current cell. |
313 ;; +-------+----------------------------------------------------------+
314 ;; | "C-<" |Narrow the current cell. |
315 ;; +-------+----------------------------------------------------------+
316 ;; | "C-}" |Heighten the current cell. |
317 ;; +-------+----------------------------------------------------------+
318 ;; | "C-{" |Shorten the current cell. |
319 ;; +-------+----------------------------------------------------------+
320 ;; | "C--" |Split current cell vertically. (one above and one below) |
321 ;; +-------+----------------------------------------------------------+
322 ;; | "C-|" |Split current cell horizontally. (one left and one right) |
323 ;; +-------+----------------------------------------------------------+
324 ;; | "C-*" |Span current cell into adjacent one. |
325 ;; +-------+----------------------------------------------------------+
326 ;; | "C-+" |Insert row(s)/column(s). |
327 ;; +-------+----------------------------------------------------------+
328 ;; | "C-!" |Toggle between normal mode and fixed width mode. |
329 ;; +-------+----------------------------------------------------------+
330 ;; | "C-#" |Report cell and table dimension. |
331 ;; +-------+----------------------------------------------------------+
332 ;; | "C-^" |Generate the source in a language from the current table. |
333 ;; +-------+----------------------------------------------------------+
334 ;; | "C-:" |Justify the contents of cell(s). |
335 ;; +-------+----------------------------------------------------------+
337 ;; *Note*
339 ;; When using `table-cell-map-hook' do not use `local-set-key'.
341 ;; (add-hook 'table-cell-map-hook
342 ;; (function (lambda ()
343 ;; (local-set-key [<key sequence>] '<function>))))
345 ;; Adding the above to your init file is a common way to customize a
346 ;; mode specific keymap. However it does not work for this package.
347 ;; This is because there is no table mode in effect. This package
348 ;; does not use a local map therefore you must modify `table-cell-map'
349 ;; explicitly. The correct way of achieving above task is:
351 ;; (add-hook 'table-cell-map-hook
352 ;; (function (lambda ()
353 ;; (define-key table-cell-map [<key sequence>] '<function>))))
355 ;; -----
356 ;; Menu:
357 ;; -----
359 ;; If a menu system is available a group of table specific menu items,
360 ;; "Table" under "Tools" section of the menu bar, is globally added
361 ;; after this package is loaded. The commands in this group are
362 ;; limited to the ones that are related to creation and initialization
363 ;; of tables, such as to insert a table, to insert rows and columns,
364 ;; or recognize and unrecognize tables. Once tables are created and
365 ;; point is placed inside of a table cell a table specific menu item
366 ;; "Table" appears directly on the menu bar. The commands in this
367 ;; menu give full control on table manipulation that include cell
368 ;; navigation, insertion, splitting, spanning, shrinking, expansion
369 ;; and unrecognizing. In addition to above two types of menu there is
370 ;; a pop-up menu available within a table cell. The content of pop-up
371 ;; menu is identical to the full table menu. [mouse-3] is the default
372 ;; button, defined in `table-cell-bindings', to bring up the pop-up
373 ;; menu. It can be reconfigured via `table-cell-map-hook'. The
374 ;; benefit of a pop-up menu is that it combines selection of the
375 ;; location (which cell, where in the cell) and selection of the
376 ;; desired operation into a single clicking action.
379 ;; -------------------------------
380 ;; Definition of tables and cells:
381 ;; -------------------------------
383 ;; There is no artificial-intelligence magic in this package. The
384 ;; definition of a table and the cells inside the table is reasonably
385 ;; limited in order to achieve acceptable performance in the
386 ;; interactive operation under Emacs lisp implementation. A valid
387 ;; table is a rectangular text area completely filled with valid
388 ;; cells. A valid cell is a rectangle text area, which four borders
389 ;; consist of valid border characters. Cells can not be nested one to
390 ;; another or overlapped to each other except sharing the border
391 ;; lines. A valid character of a cell's vertical border is either
392 ;; table-cell-vertical-char `|' or table-cell-intersection-char `+'.
393 ;; A valid character of a cell's horizontal border is either
394 ;; one of table-cell-horizontal-chars (`-' or `=')
395 ;; or table-cell-intersection-char `+'.
396 ;; A valid character of the four corners of a cell must be
397 ;; table-cell-intersection-char `+'. A cell must contain at least one
398 ;; character space inside. There is no restriction about the contents
399 ;; of a table cell, however it is advised if possible to avoid using
400 ;; any of the border characters inside a table cell. Normally a few
401 ;; boarder characters inside a table cell are harmless. But it is
402 ;; possible that they accidentally align up to emulate a bogus cell
403 ;; corner on which software relies on for cell recognition. When this
404 ;; happens the software may be fooled by it and fail to determine
405 ;; correct cell dimension.
407 ;; Following are the examples of valid tables.
409 ;; +--+----+---+ +-+ +--+-----+
410 ;; | | | | | | | | |
411 ;; +--+----+---+ +-+ | +--+--+
412 ;; | | | | | | | |
413 ;; +--+----+---+ +--+--+ |
414 ;; | | |
415 ;; +-----+--+
417 ;; The next five tables are the examples of invalid tables. (From
418 ;; left to right, 1. nested cells 2. overlapped cells and a
419 ;; non-rectangle cell 3. non-rectangle table 4. zero width/height
420 ;; cells 5. zero sized cell)
422 ;; +-----+ +-----+ +--+ +-++--+ ++
423 ;; | | | | | | | || | ++
424 ;; | +-+ | | | | | | || |
425 ;; | | | | +--+ | +--+--+ +-++--+
426 ;; | +-+ | | | | | | | +-++--+
427 ;; | | | | | | | | | || |
428 ;; +-----+ +--+--+ +--+--+ +-++--+
430 ;; Although the program may recognizes some of these invalid tables,
431 ;; results from the subsequent editing operations inside those cells
432 ;; are not predictable and will most likely start destroying the table
433 ;; structures.
435 ;; It is strongly recommended to have at least one blank line above
436 ;; and below a table. For a table to coexist peacefully with
437 ;; surrounding environment table needs to be separated from unrelated
438 ;; text. This is necessary for the left table to grow or shrink
439 ;; horizontally without breaking the right table in the following
440 ;; example.
442 ;; +-----+-----+-----+
443 ;; +-----+-----+ | | | |
444 ;; | | | +-----+-----+-----+
445 ;; +-----+-----+ | | | |
446 ;; +-----+-----+-----+
449 ;; -------------------------
450 ;; Cell contents formatting:
451 ;; -------------------------
453 ;; The cell contents are formatted by filling a paragraph immediately
454 ;; after characters are inserted into or deleted from a cell. Because
455 ;; of this, cell contents always remain fit inside a cell neatly. One
456 ;; drawback of this is that users do not have full control over
457 ;; spacing between words and line breaking. Only one space can be
458 ;; entered between words and up to two spaces between sentences. For
459 ;; a newline to be effective the new line must form a beginning of
460 ;; paragraph, otherwise it'll automatically be merged with the
461 ;; previous line in a same paragraph. To form a new paragraph the
462 ;; line must start with some space characters or immediately follow a
463 ;; blank line. Here is a typical example of how to list items within
464 ;; a cell. Without a space at the beginning of each line the items
465 ;; can not stand on their own.
467 ;; +---------------------------------+
468 ;; |Each one of the following three |
469 ;; |items starts with a space |
470 ;; |character thus forms a paragraph |
471 ;; |of its own. Limitations in cell |
472 ;; |contents formatting are: |
473 ;; | |
474 ;; | 1. Only one space between words.|
475 ;; | 2. Up to two spaces between |
476 ;; |sentences. |
477 ;; | 3. A paragraph must start with |
478 ;; |spaces or follow a blank line. |
479 ;; | |
480 ;; |This paragraph stays away from |
481 ;; |the item 3 because there is a |
482 ;; |blank line between them. |
483 ;; +---------------------------------+
485 ;; In the normal operation table cell width grows automatically when
486 ;; certain word has to be folded into the next line if the width had
487 ;; not been increased. This normal operation is useful and
488 ;; appropriate for most of the time, however, it is sometimes useful
489 ;; or necessary to fix the width of table and width of table cells.
490 ;; For this purpose the package provides fixed width mode. You can
491 ;; toggle between fixed width mode and normal mode by "C-!".
493 ;; Here is a simple example of the fixed width mode. Suppose we have
494 ;; a table like this one.
496 ;; +-----+
497 ;; | |
498 ;; +-----+
500 ;; In normal mode if you type a word "antidisestablishmentarianism" it
501 ;; grows the cell horizontally like this.
503 ;; +----------------------------+
504 ;; |antidisestablishmentarianism|
505 ;; +----------------------------+
507 ;; In the fixed width mode the same action produces the following
508 ;; result. The folded locations are indicated by a continuation
509 ;; character (`\' is the default). The continuation character is
510 ;; treated specially so it is recommended to choose a character that
511 ;; does not appear elsewhere in table cells. This character is
512 ;; configurable via customization and is kept in the variable
513 ;; `table-word-continuation-char'. The continuation character is
514 ;; treated specially only in the fixed width mode and has no special
515 ;; meaning in the normal mode however.
517 ;; +-----+
518 ;; |anti\|
519 ;; |dise\|
520 ;; |stab\|
521 ;; |lish\|
522 ;; |ment\|
523 ;; |aria\|
524 ;; |nism |
525 ;; +-----+
528 ;; -------------------
529 ;; Cell Justification:
530 ;; -------------------
532 ;; By default the cell contents are filled with left justification and
533 ;; no vertical justification. A paragraph can be justified
534 ;; individually but only horizontally. Paragraph justification is for
535 ;; appearance only and does not change any structural information
536 ;; while cell justification affects table's structural information.
537 ;; For cell justification a user can select horizontal justification
538 ;; and vertical justification independently. Horizontal justification
539 ;; must be one of the three 'left, 'center or 'right. Vertical
540 ;; justification can be 'top, 'middle, 'bottom or 'none. When a cell
541 ;; is justified, that information is recorded as a part of text
542 ;; property therefore the information is persistent as long as the
543 ;; cell remains within the Emacs world. Even copying tables by region
544 ;; and rectangle manipulation commands preserve this information.
545 ;; However, once the table text is saved as a file and the buffer is
546 ;; killed the justification information vanishes permanently. To
547 ;; alleviate this shortcoming without forcing users to save and
548 ;; maintain a separate attribute file, the table code detects
549 ;; justification of each cell when recognizing a table. This
550 ;; detection is done by guessing the justification by looking at the
551 ;; appearance of the cell contents. Since it is a guessing work it
552 ;; does not guarantee the perfectness but it is designed to be
553 ;; practically good enough. The guessing algorithm is implemented in
554 ;; the function `table--detect-cell-alignment'. If you have better
555 ;; algorithm or idea any suggestion is welcome.
558 ;; -----
559 ;; Todo: (in the order of priority, some are just possibility)
560 ;; -----
562 ;; Fix incompatibilities with input methods other than quail
563 ;; Resolve conflict with flyspell
564 ;; Use mouse for resizing cells
565 ;; A mechanism to link cells internally
566 ;; Consider the use of variable width font under Emacs 21
567 ;; Consider the use of `:box' face attribute under Emacs 21
568 ;; Consider the use of `modification-hooks' text property instead of
569 ;; rebinding the keymap
570 ;; Maybe provide complete XEmacs support in the future however the
571 ;; "extent" is the single largest obstacle lying ahead, read the
572 ;; document in Emacs info.
573 ;; (eval '(progn (require 'info) (Info-find-node "elisp" "Not Intervals")))
576 ;; ---------------
577 ;; Acknowledgment:
578 ;; ---------------
580 ;; Table would not have been possible without the help and
581 ;; encouragement of the following spirited contributors.
583 ;; Paul Georgief <georgief@igpp.ucsd.edu> has been the best tester
584 ;; of the code as well as the constructive criticizer.
586 ;; Gerd Moellmann <gerd@gnu.org> gave me useful suggestions from Emacs
587 ;; 21 point of view.
589 ;; Richard Stallman <rms@gnu.org> showed the initial interest in this
590 ;; attempt of implementing the table feature to Emacs. This greatly
591 ;; motivated me to follow through to its completion.
593 ;; Kenichi Handa <handa@etl.go.jp> kindly guided me through to
594 ;; overcome many technical issues while I was struggling with quail
595 ;; related internationalization problems.
597 ;; Christoph Conrad <christoph.conrad@gmx.de> suggested making symbol
598 ;; names consistent as well as fixing several bugs.
600 ;; Paul Lew <paullew@cisco.com> suggested implementing fixed width
601 ;; mode as well as multi column width (row height) input interface.
603 ;; Michael Smith <smith@xml-doc.org> a well-informed DocBook user
604 ;; asked for CALS table source generation and helped me following
605 ;; through the work by offering valuable suggestions and testing out
606 ;; the code. Jorge Godoy <godoy@conectiva.com> has also suggested
607 ;; supporting for DocBook tables.
609 ;; And many other individuals who reported bugs and suggestions.
611 ;;; Code:
614 (require 'regexp-opt)
616 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
618 ;;; Compatibility:
621 ;; hush up the byte-compiler
622 (defvar quail-translating)
623 (defvar quail-converting)
624 (defvar flyspell-mode)
625 (defvar real-last-command)
626 (defvar delete-selection-mode)
627 ;; This is evil!!
628 ;; (eval-when-compile
629 ;; (unless (fboundp 'set-face-property)
630 ;; (defun set-face-property (face prop value)))
631 ;; (unless (fboundp 'unibyte-char-to-multibyte)
632 ;; (defun unibyte-char-to-multibyte (char)))
633 ;; (defun table--point-in-cell-p (&optional location)))
635 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
637 ;;; Customization:
640 (defgroup table nil
641 "Text based table manipulation utilities."
642 :tag "Table"
643 :prefix "table-"
644 :group 'wp
645 :version "22.1")
647 (defgroup table-hooks nil
648 "Hooks for table manipulation utilities."
649 :group 'table)
651 (defcustom table-time-before-update 0.2
652 "Time in seconds before updating the cell contents after typing.
653 Updating the cell contents on the screen takes place only after this
654 specified amount of time has passed after the last modification to the
655 cell contents. When the contents of a table cell changes repetitively
656 and frequently the updating the cell contents on the screen is
657 deferred until at least this specified amount of quiet time passes. A
658 smaller number wastes more computation resource by unnecessarily
659 frequent screen update. A large number presents noticeable and
660 annoying delay before the typed result start appearing on the screen."
661 :tag "Time Before Cell Update"
662 :type 'number
663 :group 'table)
665 (defcustom table-time-before-reformat 0.2
666 "Time in seconds before reformatting the table.
667 This many seconds must pass in addition to `table-time-before-update'
668 before the table is updated with newly widened width or heightened
669 height."
670 :tag "Time Before Cell Reformat"
671 :type 'number
672 :group 'table)
674 (defcustom table-command-prefix [(control c) (control c)]
675 "Key sequence to be used as prefix for table command key bindings."
676 :type '(vector (repeat :inline t sexp))
677 :tag "Table Command Prefix"
678 :group 'table)
680 (defface table-cell
681 '((((min-colors 88) (class color)) :foreground "gray90" :background "blue1")
682 (((class color)) :foreground "gray90" :background "blue")
683 (t :weight bold))
684 "Face used for table cell contents."
685 :tag "Cell Face"
686 :group 'table)
688 (defcustom table-cell-horizontal-chars "-="
689 "Characters that may be used for table cell's horizontal border line."
690 :tag "Cell Horizontal Boundary Characters"
691 :type 'string
692 :group 'table)
694 (defcustom table-cell-vertical-char ?\|
695 "Character that forms table cell's vertical border line."
696 :tag "Cell Vertical Boundary Character"
697 :type 'character
698 :group 'table)
700 (defcustom table-cell-intersection-char ?\+
701 "Character that forms table cell's corner."
702 :tag "Cell Intersection Character"
703 :type 'character
704 :group 'table)
706 (defcustom table-word-continuation-char ?\\
707 "Character that indicates word continuation into the next line.
708 This character has a special meaning only in the fixed width mode,
709 that is when `table-fixed-width-mode' is non-nil . In the fixed width
710 mode this character indicates that the location is continuing into the
711 next line. Be careful about the choice of this character. It is
712 treated substantially different manner than ordinary characters. Try
713 select a character that is unlikely to appear in your document."
714 :tag "Cell Word Continuation Character"
715 :type 'character
716 :group 'table)
718 (defcustom table-detect-cell-alignment t
719 "Detect cell contents alignment automatically.
720 When non-nil cell alignment is automatically determined by the
721 appearance of the current cell contents when recognizing tables as a
722 whole. This applies to `table-recognize', `table-recognize-region'
723 and `table-recognize-table' but not to `table-recognize-cell'."
724 :tag "Detect Cell Alignment"
725 :type 'boolean
726 :group 'table)
728 (defcustom table-dest-buffer-name "table"
729 "Default buffer name (without a suffix) for source generation."
730 :tag "Source Buffer Name"
731 :type 'string
732 :group 'table)
734 (defcustom table-html-delegate-spacing-to-user-agent nil
735 "Non-nil delegates cell contents spacing entirely to user agent.
736 Otherwise, when nil, it preserves the original spacing and line breaks."
737 :tag "HTML delegate spacing"
738 :type 'boolean
739 :group 'table)
741 (defcustom table-html-th-rows 0
742 "Number of top rows to become header cells automatically in HTML generation."
743 :tag "HTML Header Rows"
744 :type 'integer
745 :group 'table)
747 (defcustom table-html-th-columns 0
748 "Number of left columns to become header cells automatically in HTML generation."
749 :tag "HTML Header Columns"
750 :type 'integer
751 :group 'table)
753 (defcustom table-html-table-attribute "border=\"1\""
754 "Table attribute that applies to the table in HTML generation."
755 :tag "HTML table attribute"
756 :type 'string
757 :group 'table)
759 (defcustom table-html-cell-attribute ""
760 "Cell attribute that applies to all cells in HTML generation.
761 Do not specify \"align\" and \"valign\" because they are determined by
762 the cell contents dynamically."
763 :tag "HTML cell attribute"
764 :type 'string
765 :group 'table)
767 (defcustom table-cals-thead-rows 1
768 "Number of top rows to become header rows in CALS table."
769 :tag "CALS Header Rows"
770 :type 'integer
771 :group 'table)
773 (defcustom table-cell-map-hook nil
774 "Normal hooks run when finishing construction of `table-cell-map'.
775 User can modify `table-cell-map' by adding custom functions here."
776 :tag "Cell Keymap Hooks"
777 :type 'hook
778 :group 'table-hooks)
780 (defcustom table-disable-incompatibility-warning nil
781 "Disable compatibility warning notice.
782 When nil user is reminded of known incompatible issues."
783 :tag "Disable Incompatibility Warning"
784 :type 'boolean
785 :group 'table)
787 (defcustom table-abort-recognition-when-input-pending t
788 "Abort current recognition process when input pending.
789 Abort current recognition process when we are not sure that no input
790 is available. When non-nil lengthy recognition process is aborted
791 simply by any key input."
792 :tag "Abort Recognition When Input Pending"
793 :type 'boolean
794 :group 'table)
796 (defcustom table-load-hook nil
797 "List of functions to be called after the table is first loaded."
798 :type 'hook
799 :group 'table-hooks)
801 (defcustom table-point-entered-cell-hook nil
802 "List of functions to be called after point entered a table cell."
803 :type 'hook
804 :group 'table-hooks)
806 (defcustom table-point-left-cell-hook nil
807 "List of functions to be called after point left a table cell."
808 :type 'hook
809 :group 'table-hooks)
811 (defvar table-yank-handler '(nil nil t nil)
812 "Yank handler for tables.")
814 (setplist 'table-disable-incompatibility-warning nil)
816 (defvar table-disable-menu (null (and (locate-library "easymenu")
817 (require 'easymenu)
818 (fboundp 'easy-menu-add-item)))
819 "When non-nil, use of menu by table package is disabled.
820 It must be set before loading this package `table.el' for the first
821 time.")
824 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
826 ;;; Implementation:
829 ;;; Internal variables and constants
830 ;;; No need of user configuration
832 (defconst table-paragraph-start "[ \t\n\f]"
833 "Regexp for beginning of a line that starts OR separates paragraphs.")
834 (defconst table-cache-buffer-name " *table cell cache*"
835 "Cell cache buffer name.")
836 (defvar table-cell-info-lu-coordinate nil
837 "Zero based coordinate of the cached cell's left upper corner.")
838 (defvar table-cell-info-rb-coordinate nil
839 "Zero based coordinate of the cached cell's right bottom corner.")
840 (defvar table-cell-info-width nil
841 "Number of characters per cached cell width.")
842 (defvar table-cell-info-height nil
843 "Number of lines per cached cell height.")
844 (defvar table-cell-info-justify nil
845 "Justification information of the cached cell.")
846 (defvar table-cell-info-valign nil
847 "Vertical alignment information of the cached cell.")
848 (defvar table-cell-self-insert-command-count 0
849 "Counter for undo control.")
850 (defvar table-cell-map nil
851 "Keymap for table cell contents.")
852 (defvar table-cell-global-map-alist nil
853 "Alist of copy of global maps that are substituted in `table-cell-map'.")
854 (defvar table-global-menu-map nil
855 "Menu map created via `easy-menu-define'.")
856 (defvar table-cell-menu-map nil
857 "Menu map created via `easy-menu-define'.")
858 (defvar table-cell-buffer nil
859 "Buffer that contains the table cell.")
860 (defvar table-cell-cache-point-coordinate nil
861 "Cache point coordinate based from the cell origin.")
862 (defvar table-cell-cache-mark-coordinate nil
863 "Cache mark coordinate based from the cell origin.")
864 (defvar table-update-timer nil
865 "Timer id for deferred cell update.")
866 (defvar table-widen-timer nil
867 "Timer id for deferred cell update.")
868 (defvar table-heighten-timer nil
869 "Timer id for deferred cell update.")
870 (defvar table-inhibit-update nil
871 "Non-nil inhibits implicit cell and cache updates.
872 It inhibits `table-with-cache-buffer' to update data in both direction, cell to cache and cache to cell.")
873 (defvar table-inhibit-auto-fill-paragraph nil
874 "Non-nil inhibits auto fill paragraph when `table-with-cache-buffer' exits.
875 This is always set to nil at the entry to `table-with-cache-buffer' before executing body forms.")
876 (defvar table-mode-indicator nil
877 "For mode line indicator")
878 ;; This is not a real minor-mode but placed in the minor-mode-alist
879 ;; so that we can show the indicator on the mode line handy.
880 (make-variable-buffer-local 'table-mode-indicator)
881 (unless (assq table-mode-indicator minor-mode-alist)
882 (push '(table-mode-indicator (table-fixed-width-mode " Fixed-Table" " Table"))
883 minor-mode-alist))
885 (defconst table-source-languages '(html latex cals)
886 "Supported source languages.")
887 (defvar table-source-info-plist nil
888 "General storage for temporary information used while generating source.")
890 ;; The following history containers not only keep the history of user
891 ;; entries but also serve as the default value providers. When an
892 ;; interactive command is invoked it offers a user the latest entry
893 ;; of the history as a default selection. Therefore the values below
894 ;; are the first default value when a command is invoked for the very
895 ;; first time when there is no real history existing yet.
896 (defvar table-cell-span-direction-history '("right"))
897 (defvar table-cell-split-orientation-history '("horizontally"))
898 (defvar table-cell-split-contents-to-history '("split"))
899 (defvar table-insert-row-column-history '("row"))
900 (defvar table-justify-history '("center"))
901 (defvar table-columns-history '("3"))
902 (defvar table-rows-history '("3"))
903 (defvar table-cell-width-history '("5"))
904 (defvar table-cell-height-history '("1"))
905 (defvar table-source-caption-history '("Table"))
906 (defvar table-sequence-string-history '("0"))
907 (defvar table-sequence-count-history '("0"))
908 (defvar table-sequence-increment-history '("1"))
909 (defvar table-sequence-interval-history '("1"))
910 (defvar table-sequence-justify-history '("left"))
911 (defvar table-source-language-history '("html"))
912 (defvar table-col-delim-regexp-history '(""))
913 (defvar table-row-delim-regexp-history '(""))
914 (defvar table-capture-justify-history '("left"))
915 (defvar table-capture-min-cell-width-history '("5"))
916 (defvar table-capture-columns-history '(""))
917 (defvar table-target-history '("cell"))
919 ;; Some entries in `table-cell-bindings' are duplicated in
920 ;; `table-command-remap-alist'. There is a good reason for
921 ;; this. Common key like return key may be taken by some other
922 ;; function than normal `newline' function. Thus binding return key
923 ;; directly for `*table--cell-newline' ensures that the correct enter
924 ;; operation in a table cell. However
925 ;; `table-command-remap-alist' has an additional role than
926 ;; replacing commands. It is also used to construct a table command
927 ;; list. This list is very important because it is used to check if
928 ;; the previous command was one of them in this list or not. If the
929 ;; previous command is found in the list the current command will not
930 ;; refill the table cache. If the command were not listed fast
931 ;; typing can cause unwanted cache refill.
932 (defconst table-cell-bindings
933 '(([(control i)] . table-forward-cell)
934 ([(control I)] . table-backward-cell)
935 ([tab] . table-forward-cell)
936 ([(shift backtab)] . table-backward-cell) ; for HPUX console keyboard
937 ([(shift iso-lefttab)] . table-backward-cell) ; shift-tab on a microsoft natural keyboard and redhat linux
938 ([(shift tab)] . table-backward-cell)
939 ([return] . *table--cell-newline)
940 ([(control m)] . *table--cell-newline)
941 ([(control j)] . *table--cell-newline-and-indent)
942 ([mouse-3] . *table--present-cell-popup-menu)
943 ([(control ?>)] . table-widen-cell)
944 ([(control ?<)] . table-narrow-cell)
945 ([(control ?})] . table-heighten-cell)
946 ([(control ?{)] . table-shorten-cell)
947 ([(control ?-)] . table-split-cell-vertically)
948 ([(control ?|)] . table-split-cell-horizontally)
949 ([(control ?*)] . table-span-cell)
950 ([(control ?+)] . table-insert-row-column)
951 ([(control ?!)] . table-fixed-width-mode)
952 ([(control ?#)] . table-query-dimension)
953 ([(control ?^)] . table-generate-source)
954 ([(control ?:)] . table-justify)
956 "Bindings for table cell commands.")
958 (defvar table-command-remap-alist
959 '((self-insert-command . *table--cell-self-insert-command)
960 (completion-separator-self-insert-autofilling . *table--cell-self-insert-command)
961 (completion-separator-self-insert-command . *table--cell-self-insert-command)
962 (delete-char . *table--cell-delete-char)
963 (delete-backward-char . *table--cell-delete-backward-char)
964 (backward-delete-char . *table--cell-delete-backward-char)
965 (backward-delete-char-untabify . *table--cell-delete-backward-char)
966 (newline . *table--cell-newline)
967 (newline-and-indent . *table--cell-newline-and-indent)
968 (open-line . *table--cell-open-line)
969 (quoted-insert . *table--cell-quoted-insert)
970 (describe-mode . *table--cell-describe-mode)
971 (describe-bindings . *table--cell-describe-bindings)
972 (dabbrev-expand . *table--cell-dabbrev-expand)
973 (dabbrev-completion . *table--cell-dabbrev-completion))
974 "List of cons cells consisting of (ORIGINAL-COMMAND . TABLE-VERSION-OF-THE-COMMAND).")
976 (defvar table-command-list
977 ;; Construct the real contents of the `table-command-list'.
978 (mapcar #'cdr table-command-remap-alist)
979 "List of commands that override original commands.")
981 (defconst table-global-menu
982 '("Table"
983 ("Insert"
984 ["a Table..." table-insert
985 :active (and (not buffer-read-only) (not (table--probe-cell)))
986 :help "Insert a text based table at point"]
987 ["Row" table-insert-row
988 :active (table--row-column-insertion-point-p)
989 :help "Insert row(s) of cells in table"]
990 ["Column" table-insert-column
991 :active (table--row-column-insertion-point-p 'column)
992 :help "Insert column(s) of cells in table"])
993 "----"
994 ("Recognize"
995 ["in Buffer" table-recognize
996 :active t
997 :help "Recognize all tables in the current buffer"]
998 ["in Region" table-recognize-region
999 :active (and mark-active (not (eq (mark t) (point))))
1000 :help "Recognize all tables in the current region"]
1001 ["a Table" table-recognize-table
1002 :active (table--probe-cell)
1003 :help "Recognize a table at point"]
1004 ["a Cell" table-recognize-cell
1005 :active (let ((cell (table--probe-cell)))
1006 (and cell (null (table--at-cell-p (car cell)))))
1007 :help "Recognize a cell at point"])
1008 ("Unrecognize"
1009 ["in Buffer" table-unrecognize
1010 :active t
1011 :help "Unrecognize all tables in the current buffer"]
1012 ["in Region" table-unrecognize-region
1013 :active (and mark-active (not (eq (mark t) (point))))
1014 :help "Unrecognize all tables in the current region"]
1015 ["a Table" table-unrecognize-table
1016 :active (table--probe-cell)
1017 :help "Unrecognize the current table"]
1018 ["a Cell" table-unrecognize-cell
1019 :active (let ((cell (table--probe-cell)))
1020 (and cell (table--at-cell-p (car cell))))
1021 :help "Unrecognize the current cell"])
1022 "----"
1023 ["Capture Region" table-capture
1024 :active (and (not buffer-read-only) mark-active (not (eq (mark t) (point))) (not (table--probe-cell)))
1025 :help "Capture text in the current region as a table"]
1026 ["Release" table-release
1027 :active (table--editable-cell-p)
1028 :help "Release the current table as plain text"]))
1030 (defconst table-cell-menu
1031 '("Table"
1032 ("Insert"
1033 ["Row" table-insert-row
1034 :active (table--row-column-insertion-point-p)
1035 :help "Insert row(s) of cells in table"]
1036 ["Column" table-insert-column
1037 :active (table--row-column-insertion-point-p 'column)
1038 :help "Insert column(s) of cells in table"])
1039 ("Delete"
1040 ["Row" table-delete-row
1041 :active (table--editable-cell-p)
1042 :help "Delete row(s) of cells in table"]
1043 ["Column" table-delete-column
1044 :active (table--editable-cell-p)
1045 :help "Delete column(s) of cells in table"])
1046 "----"
1047 ("Split a Cell"
1048 ["Horizontally" table-split-cell-horizontally
1049 :active (table--cell-can-split-horizontally-p)
1050 :help "Split the current cell horizontally at point"]
1051 ["Vertically" table-split-cell-vertically
1052 :active (table--cell-can-split-vertically-p)
1053 :help "Split the current cell vertical at point"])
1054 ("Span a Cell to"
1055 ["Right" (table-span-cell 'right)
1056 :active (table--cell-can-span-p 'right)
1057 :help "Span the current cell into the right cell"]
1058 ["Left" (table-span-cell 'left)
1059 :active (table--cell-can-span-p 'left)
1060 :help "Span the current cell into the left cell"]
1061 ["Above" (table-span-cell 'above)
1062 :active (table--cell-can-span-p 'above)
1063 :help "Span the current cell into the cell above"]
1064 ["Below" (table-span-cell 'below)
1065 :active (table--cell-can-span-p 'below)
1066 :help "Span the current cell into the cell below"])
1067 "----"
1068 ("Shrink Cells"
1069 ["Horizontally" table-narrow-cell
1070 :active (table--editable-cell-p)
1071 :help "Shrink the current cell horizontally"]
1072 ["Vertically" table-shorten-cell
1073 :active (table--editable-cell-p)
1074 :help "Shrink the current cell vertically"])
1075 ("Expand Cells"
1076 ["Horizontally" table-widen-cell
1077 :active (table--editable-cell-p)
1078 :help "Expand the current cell horizontally"]
1079 ["Vertically" table-heighten-cell
1080 :active (table--editable-cell-p)
1081 :help "Expand the current cell vertically"])
1082 "----"
1083 ("Justify"
1084 ("a Cell"
1085 ["Left" (table-justify-cell 'left)
1086 :active (table--editable-cell-p)
1087 :help "Left justify the contents of the current cell"]
1088 ["Center" (table-justify-cell 'center)
1089 :active (table--editable-cell-p)
1090 :help "Center justify the contents of the current cell"]
1091 ["Right" (table-justify-cell 'right)
1092 :active (table--editable-cell-p)
1093 :help "Right justify the contents of the current cell"]
1094 "----"
1095 ["Top" (table-justify-cell 'top)
1096 :active (table--editable-cell-p)
1097 :help "Top align the contents of the current cell"]
1098 ["Middle" (table-justify-cell 'middle)
1099 :active (table--editable-cell-p)
1100 :help "Middle align the contents of the current cell"]
1101 ["Bottom" (table-justify-cell 'bottom)
1102 :active (table--editable-cell-p)
1103 :help "Bottom align the contents of the current cell"]
1104 ["None" (table-justify-cell 'none)
1105 :active (table--editable-cell-p)
1106 :help "Remove vertical alignment from the current cell"])
1107 ("a Row"
1108 ["Left" (table-justify-row 'left)
1109 :active (table--editable-cell-p)
1110 :help "Left justify the contents of all cells in the current row"]
1111 ["Center" (table-justify-row 'center)
1112 :active (table--editable-cell-p)
1113 :help "Center justify the contents of all cells in the current row"]
1114 ["Right" (table-justify-row 'right)
1115 :active (table--editable-cell-p)
1116 :help "Right justify the contents of all cells in the current row"]
1117 "----"
1118 ["Top" (table-justify-row 'top)
1119 :active (table--editable-cell-p)
1120 :help "Top align the contents of all cells in the current row"]
1121 ["Middle" (table-justify-row 'middle)
1122 :active (table--editable-cell-p)
1123 :help "Middle align the contents of all cells in the current row"]
1124 ["Bottom" (table-justify-row 'bottom)
1125 :active (table--editable-cell-p)
1126 :help "Bottom align the contents of all cells in the current row"]
1127 ["None" (table-justify-cell 'none)
1128 :active (table--editable-cell-p)
1129 :help "Remove vertical alignment from all cells in the current row"])
1130 ("a Column"
1131 ["Left" (table-justify-column 'left)
1132 :active (table--editable-cell-p)
1133 :help "Left justify the contents of all cells in the current column"]
1134 ["Center" (table-justify-column 'center)
1135 :active (table--editable-cell-p)
1136 :help "Center justify the contents of all cells in the current column"]
1137 ["Right" (table-justify-column 'right)
1138 :active (table--editable-cell-p)
1139 :help "Right justify the contents of all cells in the current column"]
1140 "----"
1141 ["Top" (table-justify-column 'top)
1142 :active (table--editable-cell-p)
1143 :help "Top align the contents of all cells in the current column"]
1144 ["Middle" (table-justify-column 'middle)
1145 :active (table--editable-cell-p)
1146 :help "Middle align the contents of all cells in the current column"]
1147 ["Bottom" (table-justify-column 'bottom)
1148 :active (table--editable-cell-p)
1149 :help "Bottom align the contents of all cells in the current column"]
1150 ["None" (table-justify-cell 'none)
1151 :active (table--editable-cell-p)
1152 :help "Remove vertical alignment from all cells in the current column"])
1153 ("a Paragraph"
1154 ["Left" (table-justify-cell 'left t)
1155 :active (table--editable-cell-p)
1156 :help "Left justify the current paragraph"]
1157 ["Center" (table-justify-cell 'center t)
1158 :active (table--editable-cell-p)
1159 :help "Center justify the current paragraph"]
1160 ["Right" (table-justify-cell 'right t)
1161 :active (table--editable-cell-p)
1162 :help "Right justify the current paragraph"]))
1163 "----"
1164 ["Query Dimension" table-query-dimension
1165 :active (table--probe-cell)
1166 :help "Get the dimension of the current cell and the current table"]
1167 ["Generate Source" table-generate-source
1168 :active (table--probe-cell)
1169 :help "Generate source of the current table in the specified language"]
1170 ["Insert Sequence" table-insert-sequence
1171 :active (table--editable-cell-p)
1172 :help "Travel cells forward while inserting a specified sequence string in each cell"]
1173 ("Unrecognize"
1174 ["a Table" table-unrecognize-table
1175 :active (table--probe-cell)
1176 :help "Unrecognize the current table"]
1177 ["a Cell" table-unrecognize-cell
1178 :active (let ((cell (table--probe-cell)))
1179 (and cell (table--at-cell-p (car cell))))
1180 :help "Unrecognize the current cell"])
1181 ["Release" table-release
1182 :active (table--editable-cell-p)
1183 :help "Release the current table as plain text"]
1184 ("Configure Width to"
1185 ["Auto Expand Mode" (table-fixed-width-mode -1)
1186 :active t
1187 :style radio
1188 :selected (not table-fixed-width-mode)
1189 :help "A mode that allows automatic horizontal cell expansion"]
1190 ["Fixed Width Mode" (table-fixed-width-mode 1)
1191 :active t
1192 :style radio
1193 :selected table-fixed-width-mode
1194 :help "A mode that does not allow automatic horizontal cell expansion"])
1195 ("Navigate"
1196 ["Forward Cell" table-forward-cell
1197 :active (table--probe-cell)
1198 :help "Move point forward by cell(s)"]
1199 ["Backward Cell" table-backward-cell
1200 :active (table--probe-cell)
1201 :help "Move point backward by cell(s)"])
1204 ;; XEmacs causes an error when encountering unknown keywords in the
1205 ;; menu definition. Specifically the :help keyword is new in Emacs 21
1206 ;; and causes error for the XEmacs function `check-menu-syntax'. IMHO
1207 ;; it is unwise to generate an error for unknown keywords because it
1208 ;; kills the nice backward compatible extensibility of keyword use.
1209 ;; Unknown keywords should be quietly ignore so that future extension
1210 ;; does not cause a problem in the old implementation. Sigh...
1211 (when (featurep 'xemacs)
1212 (defun table--tweak-menu-for-xemacs (menu)
1213 (cond
1214 ((listp menu)
1215 (mapcar #'table--tweak-menu-for-xemacs menu))
1216 ((vectorp menu)
1217 (let ((len (length menu)))
1218 (dotimes (i len)
1219 ;; replace :help with something harmless.
1220 (if (eq (aref menu i) :help) (aset menu i :included)))))))
1221 (mapcar #'table--tweak-menu-for-xemacs
1222 (list table-global-menu table-cell-menu))
1223 (defvar mark-active t))
1225 ;; register table menu under global tools menu
1226 (unless table-disable-menu
1227 (easy-menu-define table-global-menu-map nil "Table global menu" table-global-menu)
1228 (if (featurep 'xemacs)
1229 (progn
1230 (easy-menu-add-item nil '("Tools") table-global-menu-map))
1231 (easy-menu-add-item (current-global-map) '("menu-bar" "tools") "--")
1232 (easy-menu-add-item (current-global-map) '("menu-bar" "tools") table-global-menu-map)))
1234 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1236 ;; Macros
1239 (defmacro table-with-cache-buffer (&rest body)
1240 "Execute the forms in BODY with table cache buffer as the current buffer.
1241 This macro simplifies the rest of the work greatly by condensing the
1242 common idiom used in many of the cell manipulation functions. It does
1243 not return any meaningful value.
1245 Save the current buffer and set the cache buffer as the current
1246 buffer. Move the point to the cache buffer coordinate
1247 `table-cell-cache-point-coordinate'. After BODY forms are executed,
1248 the paragraph is filled as long as `table-inhibit-auto-fill-paragraph'
1249 remains nil. BODY can set it to t when it does not want to fill the
1250 paragraph. If necessary the cell width and height are extended as the
1251 consequence of cell content modification by the BODY. Then the
1252 current buffer is restored to the original one. The last cache point
1253 coordinate is stored in `table-cell-cache-point-coordinate'. The
1254 original buffer's point is moved to the location that corresponds to
1255 the last cache point coordinate."
1256 (declare (debug (body)) (indent 0))
1257 (let ((height-expansion (make-symbol "height-expansion-var-symbol"))
1258 (width-expansion (make-symbol "width-expansion-var-symbol")))
1259 `(let (,height-expansion ,width-expansion)
1260 ;; make sure cache has valid data unless it is explicitly inhibited.
1261 (unless table-inhibit-update
1262 (table-recognize-cell))
1263 (with-current-buffer (get-buffer-create table-cache-buffer-name)
1264 ;; goto the cell coordinate based on `table-cell-cache-point-coordinate'.
1265 (set-mark (table--goto-coordinate table-cell-cache-mark-coordinate))
1266 (table--goto-coordinate table-cell-cache-point-coordinate)
1267 (table--untabify-line)
1268 ;; always reset before executing body forms because auto-fill behavior is the default.
1269 (setq table-inhibit-auto-fill-paragraph nil)
1270 ;; do the body
1271 ,@body
1272 ;; fill paragraph unless the body does not want to by setting `table-inhibit-auto-fill-paragraph'.
1273 (unless table-inhibit-auto-fill-paragraph
1274 (if (and table-cell-info-justify
1275 (not (eq table-cell-info-justify 'left)))
1276 (table--fill-region (point-min) (point-max))
1277 (table--fill-region
1278 (save-excursion (forward-paragraph -1) (point))
1279 (save-excursion (forward-paragraph 1) (point)))))
1280 ;; keep the updated cell coordinate.
1281 (setq table-cell-cache-point-coordinate (table--get-coordinate))
1282 ;; determine the cell width expansion.
1283 (setq ,width-expansion (table--measure-max-width))
1284 (if (<= ,width-expansion table-cell-info-width) nil
1285 (table--fill-region (point-min) (point-max) ,width-expansion)
1286 ;; keep the updated cell coordinate.
1287 (setq table-cell-cache-point-coordinate (table--get-coordinate)))
1288 (setq ,width-expansion (- ,width-expansion table-cell-info-width))
1289 ;; determine the cell height expansion.
1290 (if (looking-at "\\s *\\'") nil
1291 (goto-char (point-min))
1292 (if (re-search-forward "\\(\\s *\\)\\'" nil t)
1293 (goto-char (match-beginning 1))))
1294 (setq ,height-expansion (- (cdr (table--get-coordinate)) (1- table-cell-info-height))))
1295 ;; now back to the table buffer.
1296 ;; expand the cell width in the table buffer if necessary.
1297 (if (> ,width-expansion 0)
1298 (table-widen-cell ,width-expansion 'no-copy 'no-update))
1299 ;; expand the cell height in the table buffer if necessary.
1300 (if (> ,height-expansion 0)
1301 (table-heighten-cell ,height-expansion 'no-copy 'no-update))
1302 ;; do valign
1303 (with-current-buffer (get-buffer-create table-cache-buffer-name)
1304 (table--goto-coordinate table-cell-cache-point-coordinate)
1305 (setq table-cell-cache-point-coordinate (table--valign)))
1306 ;; move the point in the table buffer to the location that corresponds to
1307 ;; the location in the cell cache buffer
1308 (table--goto-coordinate (table--transcoord-cache-to-table table-cell-cache-point-coordinate))
1309 ;; set up the update timer unless it is explicitly inhibited.
1310 (unless table-inhibit-update
1311 (table--update-cell)))))
1312 (if (or (featurep 'xemacs)
1313 (null (fboundp 'font-lock-add-keywords))) nil
1314 ;; Color it as a keyword.
1315 (font-lock-add-keywords
1316 'emacs-lisp-mode
1317 '("\\<table-with-cache-buffer\\>")))
1319 (defmacro table-put-source-info (prop value)
1320 "Register source generation information."
1321 `(put 'table-source-info-plist ,prop ,value))
1323 (defmacro table-get-source-info (prop)
1324 "Retrieve source generation information."
1325 `(get 'table-source-info-plist ,prop))
1327 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1329 ;; Modified commands for cell operation
1332 ;; Point Motion Only Group
1333 (dolist (command
1334 '(move-beginning-of-line
1335 beginning-of-line
1336 move-end-of-line
1337 end-of-line
1338 beginning-of-buffer
1339 end-of-buffer
1340 forward-word
1341 backward-word
1342 forward-sentence
1343 backward-sentence
1344 forward-paragraph
1345 backward-paragraph))
1346 (let ((func-symbol (intern (format "*table--cell-%s" command)))
1347 (doc-string (format "Table remapped function for `%s'." command)))
1348 (defalias func-symbol
1349 `(lambda
1350 (&rest args)
1351 ,doc-string
1352 (interactive)
1353 (let ((table-inhibit-update t)
1354 (deactivate-mark nil))
1355 (table--finish-delayed-tasks)
1356 (table-recognize-cell 'force)
1357 (table-with-cache-buffer
1358 (call-interactively ',command)
1359 (setq table-inhibit-auto-fill-paragraph t)))))
1360 (push (cons command func-symbol)
1361 table-command-remap-alist)))
1363 ;; Extraction Group
1364 (dolist (command
1365 '(kill-region
1366 kill-ring-save
1367 delete-region
1368 copy-region-as-kill
1369 kill-line
1370 kill-word
1371 backward-kill-word
1372 kill-sentence
1373 backward-kill-sentence
1374 kill-paragraph
1375 backward-kill-paragraph
1376 kill-sexp
1377 backward-kill-sexp))
1378 (let ((func-symbol (intern (format "*table--cell-%s" command)))
1379 (doc-string (format "Table remapped function for `%s'." command)))
1380 (defalias func-symbol
1381 `(lambda
1382 (&rest args)
1383 ,doc-string
1384 (interactive)
1385 (table--finish-delayed-tasks)
1386 (table-recognize-cell 'force)
1387 (table-with-cache-buffer
1388 (table--remove-cell-properties (point-min) (point-max))
1389 (table--remove-eol-spaces (point-min) (point-max))
1390 (call-interactively ',command))
1391 (table--finish-delayed-tasks)))
1392 (push (cons command func-symbol)
1393 table-command-remap-alist)))
1395 ;; Pasting Group
1396 (dolist (command
1397 '(yank
1398 clipboard-yank
1399 yank-clipboard-selection
1400 insert))
1401 (let ((func-symbol (intern (format "*table--cell-%s" command)))
1402 (doc-string (format "Table remapped function for `%s'." command)))
1403 (fset func-symbol
1404 `(lambda
1405 (&rest args)
1406 ,doc-string
1407 (interactive)
1408 (table--finish-delayed-tasks)
1409 (table-recognize-cell 'force)
1410 (table-with-cache-buffer
1411 (call-interactively ',command)
1412 (table--untabify (point-min) (point-max))
1413 (table--fill-region (point-min) (point-max))
1414 (setq table-inhibit-auto-fill-paragraph t))
1415 (table--finish-delayed-tasks)))
1416 (push (cons command func-symbol)
1417 table-command-remap-alist)))
1419 ;; Formatting Group
1420 (dolist (command
1421 '(center-line
1422 center-region
1423 center-paragraph
1424 fill-paragraph))
1425 (let ((func-symbol (intern (format "*table--cell-%s" command)))
1426 (doc-string (format "Table remapped function for `%s'." command)))
1427 (fset func-symbol
1428 `(lambda
1429 (&rest args)
1430 ,doc-string
1431 (interactive)
1432 (table--finish-delayed-tasks)
1433 (table-recognize-cell 'force)
1434 (table-with-cache-buffer
1435 (let ((fill-column table-cell-info-width))
1436 (call-interactively ',command))
1437 (setq table-inhibit-auto-fill-paragraph t))
1438 (table--finish-delayed-tasks)))
1439 (push (cons command func-symbol)
1440 table-command-remap-alist)))
1442 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1444 ;; Commands
1447 ;;;###autoload
1448 (defun table-insert (columns rows &optional cell-width cell-height)
1449 "Insert an editable text table.
1450 Insert a table of specified number of COLUMNS and ROWS. Optional
1451 parameter CELL-WIDTH and CELL-HEIGHT can specify the size of each
1452 cell. The cell size is uniform across the table if the specified size
1453 is a number. They can be a list of numbers to specify different size
1454 for each cell. When called interactively, the list of number is
1455 entered by simply listing all the numbers with space characters
1456 delimiting them.
1458 Examples:
1460 \\[table-insert] inserts a table at the current point location.
1462 Suppose we have the following situation where `-!-' indicates the
1463 location of point.
1467 Type \\[table-insert] and hit ENTER key. As it asks table
1468 specification, provide 3 for number of columns, 1 for number of rows,
1469 5 for cell width and 1 for cell height. Now you shall see the next
1470 table and the point is automatically moved to the beginning of the
1471 first cell.
1473 +-----+-----+-----+
1474 |-!- | | |
1475 +-----+-----+-----+
1477 Inside a table cell, there are special key bindings. \\<table-cell-map>
1479 M-9 \\[table-widen-cell] (or \\[universal-argument] 9 \\[table-widen-cell]) widens the first cell by 9 character
1480 width, which results as
1482 +--------------+-----+-----+
1483 |-!- | | |
1484 +--------------+-----+-----+
1486 Type TAB \\[table-widen-cell] then type TAB M-2 M-7 \\[table-widen-cell] (or \\[universal-argument] 2 7 \\[table-widen-cell]). Typing
1487 TAB moves the point forward by a cell. The result now looks like this:
1489 +--------------+------+--------------------------------+
1490 | | |-!- |
1491 +--------------+------+--------------------------------+
1493 If you knew each width of the columns prior to the table creation,
1494 what you could have done better was to have had given the complete
1495 width information to `table-insert'.
1497 Cell width(s): 14 6 32
1499 instead of
1501 Cell width(s): 5
1503 This would have eliminated the previously mentioned width adjustment
1504 work all together.
1506 If the point is in the last cell type S-TAB S-TAB to move it to the
1507 first cell. Now type \\[table-heighten-cell] which heighten the row by a line.
1509 +--------------+------+--------------------------------+
1510 |-!- | | |
1511 | | | |
1512 +--------------+------+--------------------------------+
1514 Type \\[table-insert-row-column] and tell it to insert a row.
1516 +--------------+------+--------------------------------+
1517 |-!- | | |
1518 | | | |
1519 +--------------+------+--------------------------------+
1520 | | | |
1521 | | | |
1522 +--------------+------+--------------------------------+
1524 Move the point under the table as shown below.
1526 +--------------+------+--------------------------------+
1527 | | | |
1528 | | | |
1529 +--------------+------+--------------------------------+
1530 | | | |
1531 | | | |
1532 +--------------+------+--------------------------------+
1535 Type M-x table-insert-row instead of \\[table-insert-row-column]. \\[table-insert-row-column] does not work
1536 when the point is outside of the table. This insertion at
1537 outside of the table effectively appends a row at the end.
1539 +--------------+------+--------------------------------+
1540 | | | |
1541 | | | |
1542 +--------------+------+--------------------------------+
1543 | | | |
1544 | | | |
1545 +--------------+------+--------------------------------+
1546 |-!- | | |
1547 | | | |
1548 +--------------+------+--------------------------------+
1550 Text editing inside the table cell produces reasonably expected
1551 results.
1553 +--------------+------+--------------------------------+
1554 | | | |
1555 | | | |
1556 +--------------+------+--------------------------------+
1557 | | |Text editing inside the table |
1558 | | |cell produces reasonably |
1559 | | |expected results.-!- |
1560 +--------------+------+--------------------------------+
1561 | | | |
1562 | | | |
1563 +--------------+------+--------------------------------+
1565 Inside a table cell has a special keymap.
1567 \\{table-cell-map}"
1568 (interactive
1569 (progn
1570 (barf-if-buffer-read-only)
1571 (if (table--probe-cell)
1572 (error "Can't insert a table inside a table"))
1573 (mapcar (function table--read-from-minibuffer)
1574 '(("Number of columns" . table-columns-history)
1575 ("Number of rows" . table-rows-history)
1576 ("Cell width(s)" . table-cell-width-history)
1577 ("Cell height(s)" . table-cell-height-history)))))
1578 (table--make-cell-map)
1579 ;; Reform the arguments.
1580 (if (null cell-width) (setq cell-width (car table-cell-width-history)))
1581 (if (null cell-height) (setq cell-height (car table-cell-height-history)))
1582 (if (stringp columns) (setq columns (string-to-number columns)))
1583 (if (stringp rows) (setq rows (string-to-number rows)))
1584 (if (stringp cell-width)
1585 (setq cell-width (table--string-to-number-list cell-width)))
1586 (if (stringp cell-height)
1587 (setq cell-height (table--string-to-number-list cell-height)))
1588 (if (numberp cell-width) (setq cell-width (cons cell-width nil)))
1589 (if (numberp cell-height) (setq cell-height (cons cell-height nil)))
1590 ;; Test validity of the arguments.
1591 (dolist (arg `((columns . ,columns)
1592 (rows . ,rows)
1593 (cell-width . ,cell-width)
1594 (cell-height . ,cell-height)))
1595 (let* ((value (cdr arg))
1596 (error-handler
1597 (lambda ()
1598 (error "%s must be a positive integer%s" (car arg)
1599 (if (listp value)
1600 " or a list of positive integers" "")))))
1601 (if (null value) (funcall error-handler))
1602 (dolist (arg1 (if (listp value) value
1603 (cons value nil)))
1604 (if (or (not (integerp arg1))
1605 (< arg1 1))
1606 (funcall error-handler)))))
1607 (let ((orig-coord (table--get-coordinate))
1608 (coord (table--get-coordinate))
1609 r i cw ch cell-str border-str)
1610 ;; Prefabricate the building blocks border-str and cell-str.
1611 (with-temp-buffer
1612 ;; Construct border-str.
1613 (insert table-cell-intersection-char)
1614 (setq cw cell-width)
1615 (setq i 0)
1616 (while (< i columns)
1617 (insert (make-string (car cw)
1618 (string-to-char table-cell-horizontal-chars))
1619 table-cell-intersection-char)
1620 (if (cdr cw) (setq cw (cdr cw)))
1621 (setq i (1+ i)))
1622 (setq border-str (buffer-substring (point-min) (point-max)))
1623 ;; construct cell-str
1624 (erase-buffer)
1625 (insert table-cell-vertical-char)
1626 (setq cw cell-width)
1627 (setq i 0)
1628 (while (< i columns)
1629 (let ((beg (point)))
1630 (insert (make-string (car cw) ?\s))
1631 (insert table-cell-vertical-char)
1632 (table--put-cell-line-property beg (1- (point))))
1633 (if (cdr cw) (setq cw (cdr cw)))
1634 (setq i (1+ i)))
1635 (setq cell-str (buffer-substring (point-min) (point-max))))
1636 ;; if the construction site has an empty border push that border down.
1637 (save-excursion
1638 (beginning-of-line)
1639 (if (looking-at "\\s *$")
1640 (progn
1641 (setq border-str (concat border-str "\n"))
1642 (setq cell-str (concat cell-str "\n")))))
1643 ;; now build the table using the prefabricated building blocks
1644 (setq r 0)
1645 (setq ch cell-height)
1646 (while (< r rows)
1647 (if (> r 0) nil
1648 (table--goto-coordinate coord) (setcdr coord (1+ (cdr coord)))
1649 (table--untabify-line (point))
1650 (insert border-str))
1651 (setq i 0)
1652 (while (< i (car ch))
1653 (table--goto-coordinate coord) (setcdr coord (1+ (cdr coord)))
1654 (table--untabify-line (point))
1655 (insert cell-str)
1656 (setq i (1+ i)))
1657 (table--goto-coordinate coord) (setcdr coord (1+ (cdr coord)))
1658 (table--untabify-line (point))
1659 (insert border-str)
1660 (if (cdr ch) (setq ch (cdr ch)))
1661 (setq r (1+ r)))
1662 ;; stand by at the first cell
1663 (table--goto-coordinate (table--offset-coordinate orig-coord '(1 . 1)))
1664 (table-recognize-cell 'force)))
1666 ;;;###autoload
1667 (defun table-insert-row (n)
1668 "Insert N table row(s).
1669 When point is in a table the newly inserted row(s) are placed above
1670 the current row. When point is outside of the table it must be below
1671 the table within the table width range, then the newly created row(s)
1672 are appended at the bottom of the table."
1673 (interactive "*p")
1674 (if (< n 0) (setq n 1))
1675 (let* ((current-coordinate (table--get-coordinate))
1676 (coord-list (table--cell-list-to-coord-list (table--horizontal-cell-list t nil 'top)))
1677 (append-row (if coord-list nil (setq coord-list (table--find-row-column))))
1678 (cell-height (cdr (table--min-coord-list coord-list)))
1679 (left-list nil)
1680 (this-list coord-list)
1681 (right-list (cdr coord-list))
1682 (bottom-border-y (1+ (cdr (table--get-coordinate (cdr (table--vertical-cell-list nil t))))))
1683 (vertical-str (string table-cell-vertical-char))
1684 (vertical-str-with-properties (let ((str (string table-cell-vertical-char)))
1685 (table--put-cell-keymap-property 0 (length str) str)
1686 (table--put-cell-rear-nonsticky 0 (length str) str) str))
1687 (first-time t))
1688 ;; create the space below for the table to grow
1689 (table--create-growing-space-below (* n (+ 1 cell-height)) coord-list bottom-border-y)
1690 ;; vertically expand each cell from left to right
1691 (while this-list
1692 (let* ((left (prog1 (car left-list) (setq left-list (if left-list (cdr left-list) coord-list))))
1693 (this (prog1 (car this-list) (setq this-list (cdr this-list))))
1694 (right (prog1 (car right-list) (setq right-list (cdr right-list))))
1695 (exclude-left (and left (< (cdar left) (cdar this))))
1696 (exclude-right (and right (<= (cdar right) (cdar this))))
1697 (beg (table--goto-coordinate
1698 (cons (if exclude-left (caar this) (1- (caar this)))
1699 (cdar this))))
1700 (end (table--goto-coordinate
1701 (cons (if exclude-right (cadr this) (1+ (cadr this)))
1702 bottom-border-y)))
1703 (rect (if append-row nil (extract-rectangle beg end))))
1704 ;; prepend blank cell lines to the extracted rectangle
1705 (let ((i n))
1706 (while (> i 0)
1707 (setq rect (cons
1708 (concat (if exclude-left "" (char-to-string table-cell-intersection-char))
1709 (make-string (- (cadr this) (caar this)) (string-to-char table-cell-horizontal-chars))
1710 (if exclude-right "" (char-to-string table-cell-intersection-char)))
1711 rect))
1712 (let ((j cell-height))
1713 (while (> j 0)
1714 (setq rect (cons
1715 (concat (if exclude-left ""
1716 (if first-time vertical-str vertical-str-with-properties))
1717 (table--cell-blank-str (- (cadr this) (caar this)))
1718 (if exclude-right "" vertical-str-with-properties))
1719 rect))
1720 (setq j (1- j))))
1721 (setq i (1- i))))
1722 (setq first-time nil)
1723 (if append-row
1724 (table--goto-coordinate (cons (if exclude-left (caar this) (1- (caar this)))
1725 (1+ bottom-border-y)))
1726 (delete-rectangle beg end)
1727 (goto-char beg))
1728 (table--insert-rectangle rect)))
1729 ;; fix up the intersections
1730 (setq this-list (if append-row nil coord-list))
1731 (while this-list
1732 (let ((this (prog1 (car this-list) (setq this-list (cdr this-list))))
1733 (i 0))
1734 (while (< i n)
1735 (let ((y (1- (* i (+ 1 cell-height)))))
1736 (table--goto-coordinate (table--offset-coordinate (car this) (cons -1 y)))
1737 (delete-char 1) (insert table-cell-intersection-char)
1738 (table--goto-coordinate (table--offset-coordinate (cons (cadr this) (cdar this)) (cons 0 y)))
1739 (delete-char 1) (insert table-cell-intersection-char)
1740 (setq i (1+ i))))))
1741 ;; move the point to the beginning of the first newly inserted cell.
1742 (if (table--goto-coordinate
1743 (if append-row (cons (car (caar coord-list)) (1+ bottom-border-y))
1744 (caar coord-list))) nil
1745 (table--goto-coordinate current-coordinate))
1746 ;; re-recognize the current cell's new dimension
1747 (table-recognize-cell 'force)))
1749 ;;;###autoload
1750 (defun table-insert-column (n)
1751 "Insert N table column(s).
1752 When point is in a table the newly inserted column(s) are placed left
1753 of the current column. When point is outside of the table it must be
1754 right side of the table within the table height range, then the newly
1755 created column(s) are appended at the right of the table."
1756 (interactive "*p")
1757 (if (< n 0) (setq n 1))
1758 (let* ((current-coordinate (table--get-coordinate))
1759 (coord-list (table--cell-list-to-coord-list (table--vertical-cell-list t nil 'left)))
1760 (append-column (if coord-list nil (setq coord-list (table--find-row-column 'column))))
1761 (cell-width (car (table--min-coord-list coord-list)))
1762 (border-str (table--multiply-string (concat (make-string cell-width (string-to-char table-cell-horizontal-chars))
1763 (char-to-string table-cell-intersection-char)) n))
1764 (cell-str (table--multiply-string (concat (table--cell-blank-str cell-width)
1765 (let ((str (string table-cell-vertical-char)))
1766 (table--put-cell-keymap-property 0 (length str) str)
1767 (table--put-cell-rear-nonsticky 0 (length str) str) str)) n))
1768 (columns-to-extend (* n (+ 1 cell-width)))
1769 (above-list nil)
1770 (this-list coord-list)
1771 (below-list (cdr coord-list))
1772 (right-border-x (car (table--get-coordinate (cdr (table--horizontal-cell-list nil t))))))
1773 ;; push back the affected area above and below this table
1774 (table--horizontally-shift-above-and-below columns-to-extend coord-list)
1775 ;; process each cell vertically from top to bottom
1776 (while this-list
1777 (let* ((above (prog1 (car above-list) (setq above-list (if above-list (cdr above-list) coord-list))))
1778 (this (prog1 (car this-list) (setq this-list (cdr this-list))))
1779 (below (prog1 (car below-list) (setq below-list (cdr below-list))))
1780 (exclude-above (and above (<= (caar above) (caar this))))
1781 (exclude-below (and below (< (caar below) (caar this))))
1782 (beg-coord (cons (if append-column (1+ right-border-x) (caar this))
1783 (if exclude-above (cdar this) (1- (cdar this)))))
1784 (end-coord (cons (1+ right-border-x)
1785 (if exclude-below (cddr this) (1+ (cddr this)))))
1786 rect)
1787 ;; untabify the area right of the bar that is about to be inserted
1788 (let ((coord (table--copy-coordinate beg-coord))
1789 (i 0)
1790 (len (length rect)))
1791 (while (< i len)
1792 (if (table--goto-coordinate coord 'no-extension)
1793 (table--untabify-line (point)))
1794 (setcdr coord (1+ (cdr coord)))
1795 (setq i (1+ i))))
1796 ;; extract and delete the rectangle area including the current
1797 ;; cell and to the right border of the table.
1798 (setq rect (extract-rectangle (table--goto-coordinate beg-coord)
1799 (table--goto-coordinate end-coord)))
1800 (delete-rectangle (table--goto-coordinate beg-coord)
1801 (table--goto-coordinate end-coord))
1802 ;; prepend the empty column string at the beginning of each
1803 ;; rectangle string extracted before.
1804 (let ((rect-str rect)
1805 (first t))
1806 (while rect-str
1807 (if (and first (null exclude-above))
1808 (setcar rect-str (concat border-str (car rect-str)))
1809 (if (and (null (cdr rect-str)) (null exclude-below))
1810 (setcar rect-str (concat border-str (car rect-str)))
1811 (setcar rect-str (concat cell-str (car rect-str)))))
1812 (setq first nil)
1813 (setq rect-str (cdr rect-str))))
1814 ;; insert the extended rectangle
1815 (table--goto-coordinate beg-coord)
1816 (table--insert-rectangle rect)))
1817 ;; fix up the intersections
1818 (setq this-list (if append-column nil coord-list))
1819 (while this-list
1820 (let ((this (prog1 (car this-list) (setq this-list (cdr this-list))))
1821 (i 0))
1822 (while (< i n)
1823 (let ((x (1- (* (1+ i) (+ 1 cell-width)))))
1824 (table--goto-coordinate (table--offset-coordinate (car this) (cons x -1)))
1825 (delete-char 1) (insert table-cell-intersection-char)
1826 (table--goto-coordinate (table--offset-coordinate (cons (caar this) (cddr this)) (cons x 1)))
1827 (delete-char 1) (insert table-cell-intersection-char)
1828 (setq i (1+ i))))))
1829 ;; move the point to the beginning of the first newly inserted cell.
1830 (if (table--goto-coordinate
1831 (if append-column
1832 (cons (1+ right-border-x)
1833 (cdar (car coord-list)))
1834 (caar coord-list))) nil
1835 (table--goto-coordinate current-coordinate))
1836 ;; re-recognize the current cell's new dimension
1837 (table-recognize-cell 'force)))
1839 ;;;###autoload
1840 (defun table-insert-row-column (row-column n)
1841 "Insert row(s) or column(s).
1842 See `table-insert-row' and `table-insert-column'."
1843 (interactive
1844 (let ((n (prefix-numeric-value current-prefix-arg)))
1845 (if (< n 0) (setq n 1))
1846 (list (intern (let ((completion-ignore-case t)
1847 (default (car table-insert-row-column-history)))
1848 (downcase (completing-read
1849 (format "Insert %s row%s/column%s (default %s): "
1850 (if (> n 1) (format "%d" n) "a")
1851 (if (> n 1) "s" "")
1852 (if (> n 1) "s" "")
1853 default)
1854 '(("row") ("column"))
1855 nil t nil 'table-insert-row-column-history default))))
1856 n)))
1857 (cond ((eq row-column 'row)
1858 (table-insert-row n))
1859 ((eq row-column 'column)
1860 (table-insert-column n))))
1862 ;;;###autoload
1863 (defun table-recognize (&optional arg)
1864 "Recognize all tables within the current buffer and activate them.
1865 Scans the entire buffer and recognizes valid table cells. If the
1866 optional numeric prefix argument ARG is negative the tables in the
1867 buffer become inactive, meaning the tables become plain text and loses
1868 all the table specific features."
1869 (interactive "P")
1870 (setq arg (prefix-numeric-value arg))
1871 (let* ((inhibit-read-only t))
1872 (table-recognize-region (point-min) (point-max) -1)
1873 (if (>= arg 0)
1874 (save-excursion
1875 (goto-char (point-min))
1876 (let* ((border (format "[%s%c%c]"
1877 table-cell-horizontal-chars
1878 table-cell-vertical-char
1879 table-cell-intersection-char))
1880 (border3 (concat border border border))
1881 (non-border (format "^[^%s%c%c]*$"
1882 table-cell-horizontal-chars
1883 table-cell-vertical-char
1884 table-cell-intersection-char)))
1885 ;; `table-recognize-region' is an expensive function so minimize
1886 ;; the search area. A minimum table at least consists of three consecutive
1887 ;; table border characters to begin with such as
1888 ;; +-+
1889 ;; |A|
1890 ;; +-+
1891 ;; and any tables end with a line containing no table border characters
1892 ;; or the end of buffer.
1893 (while (and (re-search-forward border3 (point-max) t)
1894 (not (and (input-pending-p)
1895 table-abort-recognition-when-input-pending)))
1896 (message "Recognizing tables...(%d%%)" (/ (* 100 (match-beginning 0)) (- (point-max) (point-min))))
1897 (let ((beg (match-beginning 0))
1898 end)
1899 (if (re-search-forward non-border (point-max) t)
1900 (setq end (match-beginning 0))
1901 (setq end (goto-char (point-max))))
1902 (table-recognize-region beg end arg)))
1903 (message "Recognizing tables...done"))))))
1905 ;;;###autoload
1906 (defun table-unrecognize ()
1907 (interactive)
1908 (table-recognize -1))
1910 ;;;###autoload
1911 (defun table-recognize-region (beg end &optional arg)
1912 "Recognize all tables within region.
1913 BEG and END specify the region to work on. If the optional numeric
1914 prefix argument ARG is negative the tables in the region become
1915 inactive, meaning the tables become plain text and lose all the table
1916 specific features."
1917 (interactive "r\nP")
1918 (setq arg (prefix-numeric-value arg))
1919 (let ((inhibit-read-only t)
1920 (modified-flag (buffer-modified-p)))
1921 (if (< arg 0)
1922 (table--remove-cell-properties beg end)
1923 (save-excursion
1924 (goto-char beg)
1925 (let* ((border (format "[%s%c%c]"
1926 table-cell-horizontal-chars
1927 table-cell-vertical-char
1928 table-cell-intersection-char))
1929 (non-border (format "[^%s%c%c]"
1930 table-cell-horizontal-chars
1931 table-cell-vertical-char
1932 table-cell-intersection-char))
1933 (inhibit-read-only t))
1934 (unwind-protect
1935 (progn
1936 (remove-text-properties beg end '(table-cell nil))
1937 (while (and (< (point) end)
1938 (not (and (input-pending-p)
1939 table-abort-recognition-when-input-pending)))
1940 (cond
1941 ((looking-at "\n")
1942 (forward-char 1))
1943 ((looking-at border)
1944 (if (re-search-forward non-border end t)
1945 (goto-char (match-beginning 0))
1946 (goto-char end)))
1947 ((table--at-cell-p (point))
1948 (goto-char (next-single-property-change (point) 'table-cell nil end)))
1950 (let ((cell (table-recognize-cell 'force 'no-copy)))
1951 (if (and cell table-detect-cell-alignment)
1952 (table--detect-cell-alignment cell)))
1953 (unless (re-search-forward border end t)
1954 (goto-char end))))))))))
1955 (restore-buffer-modified-p modified-flag)))
1957 ;;;###autoload
1958 (defun table-unrecognize-region (beg end)
1959 (interactive "r")
1960 (table-recognize-region beg end -1))
1962 ;;;###autoload
1963 (defun table-recognize-table (&optional arg)
1964 "Recognize a table at point.
1965 If the optional numeric prefix argument ARG is negative the table
1966 becomes inactive, meaning the table becomes plain text and loses all
1967 the table specific features."
1968 (interactive "P")
1969 (setq arg (prefix-numeric-value arg))
1970 (let ((unrecognize (< arg 0))
1971 (origin-cell (table--probe-cell))
1972 (inhibit-read-only t))
1973 (if origin-cell
1974 (save-excursion
1975 (while
1976 (progn
1977 (table-forward-cell 1 nil unrecognize)
1978 (let ((cell (table--probe-cell)))
1979 (if (and cell table-detect-cell-alignment)
1980 (table--detect-cell-alignment cell))
1981 (and cell (not (equal cell origin-cell))))))))))
1983 ;;;###autoload
1984 (defun table-unrecognize-table ()
1985 (interactive)
1986 (table-recognize-table -1))
1988 ;;;###autoload
1989 (defun table-recognize-cell (&optional force no-copy arg)
1990 "Recognize a table cell that contains current point.
1991 Probe the cell dimension and prepare the cell information. The
1992 optional two arguments FORCE and NO-COPY are for internal use only and
1993 must not be specified. When the optional numeric prefix argument ARG
1994 is negative the cell becomes inactive, meaning that the cell becomes
1995 plain text and loses all the table specific features."
1996 (interactive "i\ni\np")
1997 (table--make-cell-map)
1998 (if (or force (not (memq (table--get-last-command) table-command-list)))
1999 (let* ((cell (table--probe-cell (called-interactively-p 'interactive)))
2000 (cache-buffer (get-buffer-create table-cache-buffer-name))
2001 (modified-flag (buffer-modified-p))
2002 (inhibit-read-only t))
2003 (unwind-protect
2004 (unless (null cell)
2005 ;; initialize the cell info variables
2006 (let ((lu-coordinate (table--get-coordinate (car cell)))
2007 (rb-coordinate (table--get-coordinate (cdr cell))))
2008 ;; update the previous cell if this cell is different from the previous one.
2009 ;; care only lu but ignore rb since size change does not matter.
2010 (unless (equal table-cell-info-lu-coordinate lu-coordinate)
2011 (table--finish-delayed-tasks))
2012 (setq table-cell-info-lu-coordinate lu-coordinate)
2013 (setq table-cell-info-rb-coordinate rb-coordinate)
2014 (setq table-cell-info-width (- (car table-cell-info-rb-coordinate)
2015 (car table-cell-info-lu-coordinate)))
2016 (setq table-cell-info-height (+ (- (cdr table-cell-info-rb-coordinate)
2017 (cdr table-cell-info-lu-coordinate)) 1))
2018 (setq table-cell-info-justify (table--get-cell-justify-property cell))
2019 (setq table-cell-info-valign (table--get-cell-valign-property cell)))
2020 ;; set/remove table cell properties
2021 (if (< (prefix-numeric-value arg) 0)
2022 (let ((coord (table--get-coordinate (car cell)))
2023 (n table-cell-info-height))
2024 (save-excursion
2025 (while (> n 0)
2026 (table--remove-cell-properties
2027 (table--goto-coordinate coord)
2028 (table--goto-coordinate (cons (+ (car coord) table-cell-info-width 1) (cdr coord))))
2029 (setq n (1- n))
2030 (setcdr coord (1+ (cdr coord))))))
2031 (table--put-cell-property cell))
2032 ;; copy the cell contents to the cache buffer
2033 ;; only if no-copy is nil and timers are not set
2034 (unless no-copy
2035 (setq table-cell-cache-point-coordinate (table--transcoord-table-to-cache))
2036 (setq table-cell-cache-mark-coordinate (table--transcoord-table-to-cache
2037 (table--get-coordinate (marker-position (mark-marker)))))
2038 (setq table-cell-buffer (current-buffer))
2039 (let ((rectangle (extract-rectangle (car cell)
2040 (cdr cell))))
2041 (save-current-buffer
2042 (set-buffer cache-buffer)
2043 (erase-buffer)
2044 (table--insert-rectangle rectangle)))))
2045 (restore-buffer-modified-p modified-flag))
2046 (if (featurep 'xemacs)
2047 (table--warn-incompatibility))
2048 cell)))
2050 ;;;###autoload
2051 (defun table-unrecognize-cell ()
2052 (interactive)
2053 (table-recognize-cell nil nil -1))
2055 ;;;###autoload
2056 (defun table-heighten-cell (n &optional no-copy no-update)
2057 "Heighten the current cell by N lines by expanding the cell vertically.
2058 Heightening is done by adding blank lines at the bottom of the current
2059 cell. Other cells aligned horizontally with the current one are also
2060 heightened in order to keep the rectangular table structure. The
2061 optional argument NO-COPY is internal use only and must not be
2062 specified."
2063 (interactive "*p")
2064 (if (< n 0) (setq n 1))
2065 (let* ((coord-list (table--cell-list-to-coord-list (table--horizontal-cell-list t)))
2066 (left-list nil)
2067 (this-list coord-list)
2068 (right-list (cdr coord-list))
2069 (bottom-border-y (1+ (cdr (table--get-coordinate (cdr (table--vertical-cell-list nil t))))))
2070 (vertical-str (string table-cell-vertical-char))
2071 (vertical-str-with-properties (string table-cell-vertical-char))
2072 (first-time t)
2073 (current-coordinate (table--get-coordinate)))
2074 ;; prepare the right vertical string with appropriate properties put
2075 (table--put-cell-keymap-property 0 (length vertical-str-with-properties) vertical-str-with-properties)
2076 ;; create the space below for the table to grow
2077 (table--create-growing-space-below n coord-list bottom-border-y)
2078 ;; vertically expand each cell from left to right
2079 (while this-list
2080 (let* ((left (prog1 (car left-list) (setq left-list (if left-list (cdr left-list) coord-list))))
2081 (this (prog1 (car this-list) (setq this-list (cdr this-list))))
2082 (right (prog1 (car right-list) (setq right-list (cdr right-list))))
2083 (exclude-left (and left (< (cddr left) (cddr this))))
2084 (exclude-right (and right (<= (cddr right) (cddr this))))
2085 (beg (table--goto-coordinate
2086 (cons (if exclude-left (caar this) (1- (caar this)))
2087 (1+ (cddr this)))))
2088 (end (table--goto-coordinate
2089 (cons (if exclude-right (cadr this) (1+ (cadr this)))
2090 bottom-border-y)))
2091 (rect (extract-rectangle beg end)))
2092 ;; prepend blank cell lines to the extracted rectangle
2093 (let ((i n))
2094 (while (> i 0)
2095 (setq rect (cons
2096 (concat (if exclude-left ""
2097 (if first-time vertical-str vertical-str-with-properties))
2098 (table--cell-blank-str (- (cadr this) (caar this)))
2099 (if exclude-right "" vertical-str-with-properties))
2100 rect))
2101 (setq i (1- i))))
2102 (setq first-time nil)
2103 (delete-rectangle beg end)
2104 (goto-char beg)
2105 (table--insert-rectangle rect)))
2106 (table--goto-coordinate current-coordinate)
2107 ;; re-recognize the current cell's new dimension
2108 (table-recognize-cell 'force no-copy)
2109 (unless no-update
2110 (table--update-cell-heightened))))
2112 ;;;###autoload
2113 (defun table-shorten-cell (n)
2114 "Shorten the current cell by N lines by shrinking the cell vertically.
2115 Shortening is done by removing blank lines from the bottom of the cell
2116 and possibly from the top of the cell as well. Therefore, the cell
2117 must have some bottom/top blank lines to be shorten effectively. This
2118 is applicable to all the cells aligned horizontally with the current
2119 one because they are also shortened in order to keep the rectangular
2120 table structure."
2121 (interactive "*p")
2122 (if (< n 0) (setq n 1))
2123 (table--finish-delayed-tasks)
2124 (let* ((table-inhibit-update t)
2125 (coord-list (table--cell-list-to-coord-list (table--horizontal-cell-list t)))
2126 (left-list nil)
2127 (this-list coord-list)
2128 (right-list (cdr coord-list))
2129 (bottom-budget-list nil)
2130 (bottom-border-y (1+ (cdr (table--get-coordinate (cdr (table--vertical-cell-list nil t))))))
2131 (current-coordinate (table--get-coordinate))
2132 (current-cell-coordinate (table--cell-to-coord (table--probe-cell)))
2133 (blank-line-regexp "\\s *$"))
2134 (message "Shortening...");; this operation may be lengthy
2135 ;; for each cell calculate the maximum number of blank lines we can delete
2136 ;; and adjust the argument n. n is adjusted so that the total number of
2137 ;; blank lines from top and bottom of a cell do not exceed n, all cell has
2138 ;; at least one line height after blank line deletion.
2139 (while this-list
2140 (let ((this (prog1 (car this-list) (setq this-list (cdr this-list)))))
2141 (table--goto-coordinate (car this))
2142 (table-recognize-cell 'force)
2143 (table-with-cache-buffer
2144 (catch 'end-count
2145 (let ((blank-line-count 0))
2146 (table--goto-coordinate (cons 0 (1- table-cell-info-height)))
2147 ;; count bottom
2148 (while (and (looking-at blank-line-regexp)
2149 (setq blank-line-count (1+ blank-line-count))
2150 ;; need to leave at least one blank line
2151 (if (> blank-line-count n) (throw 'end-count nil) t)
2152 (if (zerop (forward-line -1)) t
2153 (setq n (if (zerop blank-line-count) 0
2154 (1- blank-line-count)))
2155 (throw 'end-count nil))))
2156 (table--goto-coordinate (cons 0 0))
2157 ;; count top
2158 (while (and (looking-at blank-line-regexp)
2159 (setq blank-line-count (1+ blank-line-count))
2160 ;; can consume all blank lines
2161 (if (>= blank-line-count n) (throw 'end-count nil) t)
2162 (zerop (forward-line 1))))
2163 (setq n blank-line-count))))))
2164 ;; construct the bottom-budget-list which is a list of numbers where each number
2165 ;; corresponds to how many lines to be deleted from the bottom of each cell. If
2166 ;; this number, say bb, is smaller than n (bb < n) that means the difference (n - bb)
2167 ;; number of lines must be deleted from the top of the cell in addition to deleting
2168 ;; bb lines from the bottom of the cell.
2169 (setq this-list coord-list)
2170 (while this-list
2171 (let ((this (prog1 (car this-list) (setq this-list (cdr this-list)))))
2172 (table--goto-coordinate (car this))
2173 (table-recognize-cell 'force)
2174 (table-with-cache-buffer
2175 (setq bottom-budget-list
2176 (cons
2177 (let ((blank-line-count 0))
2178 (table--goto-coordinate (cons 0 (1- table-cell-info-height)))
2179 (while (and (looking-at blank-line-regexp)
2180 (< blank-line-count n)
2181 (setq blank-line-count (1+ blank-line-count))
2182 (zerop (forward-line -1))))
2183 blank-line-count)
2184 bottom-budget-list)))))
2185 (setq bottom-budget-list (nreverse bottom-budget-list))
2186 ;; vertically shorten each cell from left to right
2187 (setq this-list coord-list)
2188 (while this-list
2189 (let* ((left (prog1 (car left-list) (setq left-list (if left-list (cdr left-list) coord-list))))
2190 (this (prog1 (car this-list) (setq this-list (cdr this-list))))
2191 (right (prog1 (car right-list) (setq right-list (cdr right-list))))
2192 (bottom-budget (prog1 (car bottom-budget-list) (setq bottom-budget-list (cdr bottom-budget-list))))
2193 (exclude-left (and left (< (cddr left) (cddr this))))
2194 (exclude-right (and right (<= (cddr right) (cddr this))))
2195 (beg (table--goto-coordinate (cons (caar this) (cdar this))))
2196 (end (table--goto-coordinate (cons (cadr this) bottom-border-y)))
2197 (rect (extract-rectangle beg end))
2198 (height (+ (- (cddr this) (cdar this)) 1))
2199 (blank-line (make-string (- (cadr this) (caar this)) ?\s)))
2200 ;; delete lines from the bottom of the cell
2201 (setcdr (nthcdr (- height bottom-budget 1) rect) (nthcdr height rect))
2202 ;; delete lines from the top of the cell
2203 (if (> n bottom-budget)
2204 (let ((props (text-properties-at 0 (car rect))))
2205 (setq rect (nthcdr (- n bottom-budget) rect))
2206 (set-text-properties 0 1 props (car rect))))
2207 ;; append blank lines below the table
2208 (setq rect (append rect (make-list n blank-line)))
2209 ;; now swap the area with the prepared rect of the same size
2210 (delete-rectangle beg end)
2211 (goto-char beg)
2212 (table--insert-rectangle rect)
2213 ;; for the left and right borders always delete lines from the bottom of the cell
2214 (unless exclude-left
2215 (let* ((beg (table--goto-coordinate (cons (1- (caar this)) (cdar this))))
2216 (end (table--goto-coordinate (cons (caar this) bottom-border-y)))
2217 (rect (extract-rectangle beg end)))
2218 (setcdr (nthcdr (- height n 1) rect) (nthcdr height rect))
2219 (setq rect (append rect (make-list n " ")))
2220 (delete-rectangle beg end)
2221 (goto-char beg)
2222 (table--insert-rectangle rect)))
2223 (unless exclude-right
2224 (let* ((beg (table--goto-coordinate (cons (cadr this) (cdar this))))
2225 (end (table--goto-coordinate (cons (1+ (cadr this)) bottom-border-y)))
2226 (rect (extract-rectangle beg end)))
2227 (setcdr (nthcdr (- height n 1) rect) (nthcdr height rect))
2228 (setq rect (append rect (make-list n " ")))
2229 (delete-rectangle beg end)
2230 (goto-char beg)
2231 (table--insert-rectangle rect)))
2232 ;; if this is the cell where the original point was in, adjust the point location
2233 (if (null (equal this current-cell-coordinate)) nil
2234 (let ((y (- (cdr current-coordinate) (cdar this))))
2235 (if (< y (- n bottom-budget))
2236 (setcdr current-coordinate (cdar this))
2237 (if (< (- y (- n bottom-budget)) (- height n))
2238 (setcdr current-coordinate (+ (cdar this) (- y (- n bottom-budget))))
2239 (setcdr current-coordinate (+ (cdar this) (- height n 1)))))))))
2240 ;; remove the appended blank lines below the table if they are unnecessary
2241 (table--goto-coordinate (cons 0 (1+ (- bottom-border-y n))))
2242 (table--remove-blank-lines n)
2243 ;; re-recognize the current cell's new dimension
2244 (table--goto-coordinate current-coordinate)
2245 (table-recognize-cell 'force)
2246 (table--update-cell-heightened)
2247 (message "")))
2249 ;;;###autoload
2250 (defun table-widen-cell (n &optional no-copy no-update)
2251 "Widen the current cell by N columns and expand the cell horizontally.
2252 Some other cells in the same table are widen as well to keep the
2253 table's rectangle structure."
2254 (interactive "*p")
2255 (if (< n 0) (setq n 1))
2256 (let* ((coord-list (table--cell-list-to-coord-list (table--vertical-cell-list)))
2257 (below-list nil)
2258 (this-list coord-list)
2259 (above-list (cdr coord-list)))
2260 (save-excursion
2261 ;; push back the affected area above and below this table
2262 (table--horizontally-shift-above-and-below n (reverse coord-list))
2263 ;; now widen vertically for each cell
2264 (while this-list
2265 (let* ((below (prog1 (car below-list) (setq below-list (if below-list (cdr below-list) coord-list))))
2266 (this (prog1 (car this-list) (setq this-list (cdr this-list))))
2267 (above (prog1 (car above-list) (setq above-list (cdr above-list))))
2268 (beg (table--goto-coordinate
2269 (cons (car (cdr this))
2270 (if (or (null above) (<= (car (cdr this)) (car (cdr above))))
2271 (1- (cdr (car this)))
2272 (cdr (car this))))))
2273 (end (table--goto-coordinate
2274 (cons (1+ (car (cdr this)))
2275 (if (or (null below) (< (car (cdr this)) (car (cdr below))))
2276 (1+ (cdr (cdr this)))
2277 (cdr (cdr this))))))
2278 (tmp (extract-rectangle (1- beg) end))
2279 (border (format "[%s%c]\\%c"
2280 table-cell-horizontal-chars
2281 table-cell-intersection-char
2282 table-cell-intersection-char))
2283 (blank (table--cell-blank-str))
2284 rectangle)
2285 ;; create a single wide vertical bar of empty cell fragment
2286 (while tmp
2287 ; (message "tmp is %s" tmp)
2288 (setq rectangle (cons
2289 (if (string-match border (car tmp))
2290 (substring (car tmp) 0 1)
2291 blank)
2292 rectangle))
2293 ; (message "rectangle is %s" rectangle)
2294 (setq tmp (cdr tmp)))
2295 (setq rectangle (nreverse rectangle))
2296 ;; untabify the area right of the bar that is about to be inserted
2297 (let ((coord (table--get-coordinate beg))
2298 (i 0)
2299 (len (length rectangle)))
2300 (while (< i len)
2301 (if (table--goto-coordinate coord 'no-extension)
2302 (table--untabify-line (point)))
2303 (setcdr coord (1+ (cdr coord)))
2304 (setq i (1+ i))))
2305 ;; insert the bar n times
2306 (goto-char beg)
2307 (let ((i 0))
2308 (while (< i n)
2309 (save-excursion
2310 (table--insert-rectangle rectangle))
2311 (setq i (1+ i)))))))
2312 (table-recognize-cell 'force no-copy)
2313 (unless no-update
2314 (table--update-cell-widened))))
2316 ;;;###autoload
2317 (defun table-narrow-cell (n)
2318 "Narrow the current cell by N columns and shrink the cell horizontally.
2319 Some other cells in the same table are narrowed as well to keep the
2320 table's rectangle structure."
2321 (interactive "*p")
2322 (if (< n 0) (setq n 1))
2323 (table--finish-delayed-tasks)
2324 (let* ((coord-list (table--cell-list-to-coord-list (table--vertical-cell-list)))
2325 (current-cell (table--cell-to-coord (table--probe-cell)))
2326 (current-coordinate (table--get-coordinate))
2327 tmp-list)
2328 (message "Narrowing...");; this operation may be lengthy
2329 ;; determine the doable n by try narrowing each cell.
2330 (setq tmp-list coord-list)
2331 (while tmp-list
2332 (let ((cell (prog1 (car tmp-list) (setq tmp-list (cdr tmp-list))))
2333 (table-inhibit-update t)
2334 cell-n)
2335 (table--goto-coordinate (car cell))
2336 (table-recognize-cell 'force)
2337 (table-with-cache-buffer
2338 (table--fill-region (point-min) (point-max) (- table-cell-info-width n))
2339 (if (< (setq cell-n (- table-cell-info-width (table--measure-max-width))) n)
2340 (setq n cell-n))
2341 (erase-buffer)
2342 (setq table-inhibit-auto-fill-paragraph t))))
2343 (if (< n 1) nil
2344 ;; narrow only the contents of each cell but leave the cell frame as is because
2345 ;; we need to have valid frame structure in order for table-with-cache-buffer
2346 ;; to work correctly.
2347 (setq tmp-list coord-list)
2348 (while tmp-list
2349 (let* ((cell (prog1 (car tmp-list) (setq tmp-list (cdr tmp-list))))
2350 (table-inhibit-update t)
2351 (currentp (equal cell current-cell))
2352 old-height)
2353 (if currentp (table--goto-coordinate current-coordinate)
2354 (table--goto-coordinate (car cell)))
2355 (table-recognize-cell 'force)
2356 (setq old-height table-cell-info-height)
2357 (table-with-cache-buffer
2358 (let ((out-of-bound (>= (- (car current-coordinate) (car table-cell-info-lu-coordinate))
2359 (- table-cell-info-width n)))
2360 (sticky (and currentp
2361 (save-excursion
2362 (unless (bolp) (forward-char -1))
2363 (looking-at ".*\\S ")))))
2364 (table--fill-region (point-min) (point-max) (- table-cell-info-width n))
2365 (if (or sticky (and currentp (looking-at ".*\\S ")))
2366 (setq current-coordinate (table--transcoord-cache-to-table))
2367 (if out-of-bound (setcar current-coordinate
2368 (+ (car table-cell-info-lu-coordinate) (- table-cell-info-width n 1))))))
2369 (setq table-inhibit-auto-fill-paragraph t))
2370 (table--update-cell 'now)
2371 ;; if this cell heightens and pushes the current cell below, move
2372 ;; the current-coordinate (point location) down accordingly.
2373 (if currentp (setq current-coordinate (table--get-coordinate))
2374 (if (and (> table-cell-info-height old-height)
2375 (> (cdr current-coordinate) (cdr table-cell-info-lu-coordinate)))
2376 (setcdr current-coordinate (+ (cdr current-coordinate)
2377 (- table-cell-info-height old-height)))))
2379 ;; coord-list is now possibly invalid since some cells may have already
2380 ;; been heightened so recompute them by table--vertical-cell-list.
2381 (table--goto-coordinate current-coordinate)
2382 (setq coord-list (table--cell-list-to-coord-list (table--vertical-cell-list)))
2383 ;; push in the affected area above and below this table so that things
2384 ;; on the right side of the table are shifted horizontally neatly.
2385 (table--horizontally-shift-above-and-below (- n) (reverse coord-list))
2386 ;; finally narrow the frames for each cell.
2387 (let* ((below-list nil)
2388 (this-list coord-list)
2389 (above-list (cdr coord-list)))
2390 (while this-list
2391 (let* ((below (prog1 (car below-list) (setq below-list (if below-list (cdr below-list) coord-list))))
2392 (this (prog1 (car this-list) (setq this-list (cdr this-list))))
2393 (above (prog1 (car above-list) (setq above-list (cdr above-list)))))
2394 (delete-rectangle
2395 (table--goto-coordinate
2396 (cons (- (cadr this) n)
2397 (if (or (null above) (<= (cadr this) (cadr above)))
2398 (1- (cdar this))
2399 (cdar this))))
2400 (table--goto-coordinate
2401 (cons (cadr this)
2402 (if (or (null below) (< (cadr this) (cadr below)))
2403 (1+ (cddr this))
2404 (cddr this)))))))))
2405 (table--goto-coordinate current-coordinate)
2406 ;; re-recognize the current cell's new dimension
2407 (table-recognize-cell 'force)
2408 (message "")))
2410 ;;;###autoload
2411 (defun table-forward-cell (&optional arg no-recognize unrecognize)
2412 "Move point forward to the beginning of the next cell.
2413 With argument ARG, do it ARG times;
2414 a negative argument ARG = -N means move backward N cells.
2415 Do not specify NO-RECOGNIZE and UNRECOGNIZE. They are for internal use only.
2417 Sample Cell Traveling Order (In Irregular Table Cases)
2419 You can actually try how it works in this buffer. Press
2420 \\[table-recognize] and go to cells in the following tables and press
2421 \\[table-forward-cell] or TAB key.
2423 +-----+--+ +--+-----+ +--+--+--+ +--+--+--+ +---------+ +--+---+--+
2424 |0 |1 | |0 |1 | |0 |1 |2 | |0 |1 |2 | |0 | |0 |1 |2 |
2425 +--+--+ | | +--+--+ +--+ | | | | +--+ +----+----+ +--+-+-+--+
2426 |2 |3 | | | |2 |3 | |3 +--+ | | +--+3 | |1 |2 | |3 |4 |
2427 | +--+--+ +--+--+ | +--+4 | | | |4 +--+ +--+-+-+--+ +----+----+
2428 | |4 | |4 | | |5 | | | | | |5 | |3 |4 |5 | |5 |
2429 +--+-----+ +-----+--+ +--+--+--+ +--+--+--+ +--+---+--+ +---------+
2431 +--+--+--+ +--+--+--+ +--+--+--+ +--+--+--+
2432 |0 |1 |2 | |0 |1 |2 | |0 |1 |2 | |0 |1 |2 |
2433 | | | | | +--+ | | | | | +--+ +--+
2434 +--+ +--+ +--+3 +--+ | +--+ | |3 +--+4 |
2435 |3 | |4 | |4 +--+5 | | |3 | | +--+5 +--+
2436 | | | | | |6 | | | | | | |6 | |7 |
2437 +--+--+--+ +--+--+--+ +--+--+--+ +--+--+--+
2439 +--+--+--+ +--+--+--+ +--+--+--+--+ +--+-----+--+ +--+--+--+--+
2440 |0 |1 |2 | |0 |1 |2 | |0 |1 |2 |3 | |0 |1 |2 | |0 |1 |2 |3 |
2441 | +--+ | | +--+ | | +--+--+ | | | | | | +--+--+ |
2442 | |3 +--+ +--+3 | | +--+4 +--+ +--+ +--+ +--+4 +--+
2443 +--+ |4 | |4 | +--+ |5 +--+--+6 | |3 +--+--+4 | |5 | |6 |
2444 |5 +--+ | | +--+5 | | |7 |8 | | | |5 |6 | | | | | |
2445 | |6 | | | |6 | | +--+--+--+--+ +--+--+--+--+ +--+-----+--+
2446 +--+--+--+ +--+--+--+
2448 ;; After modifying this function, test against the above tables in
2449 ;; the doc string. It is quite tricky. The tables above do not
2450 ;; mean to cover every possible cases of cell layout, of course.
2451 ;; They are examples of tricky cases from implementation point of
2452 ;; view and provided for simple regression test purpose.
2453 (interactive "p")
2454 (or arg (setq arg 1))
2455 (table--finish-delayed-tasks)
2456 (while (null (zerop arg))
2457 (let* ((pivot (table--probe-cell 'abort-on-error))
2458 (cell pivot) edge tip)
2459 ;; go to the beginning of the first right/left cell with same height if exists
2460 (while (and (setq cell (table--goto-coordinate
2461 (cons (if (> arg 0) (1+ (car (table--get-coordinate (cdr cell))))
2462 (1- (car (table--get-coordinate (car cell)))))
2463 (cdr (table--get-coordinate (car pivot)))) 'no-extension))
2464 (setq cell (table--probe-cell))
2465 (/= (cdr (table--get-coordinate (car cell)))
2466 (cdr (table--get-coordinate (car pivot))))))
2467 (if cell (goto-char (car cell)) ; done
2468 ;; if the horizontal move fails search the most left/right edge cell below/above the pivot
2469 ;; but first find the edge cell
2470 (setq edge pivot)
2471 (while (and (table--goto-coordinate
2472 (cons (if (> arg 0) (1- (car (table--get-coordinate (car edge))))
2473 (1+ (car (table--get-coordinate (cdr edge)))))
2474 (cdr (table--get-coordinate (car pivot)))) 'no-extension)
2475 (setq cell (table--probe-cell))
2476 (setq edge cell)))
2477 (setq cell (if (> arg 0) edge
2478 (or (and (table--goto-coordinate
2479 (cons (car (table--get-coordinate (cdr edge)))
2480 (1- (cdr (table--get-coordinate (car edge))))))
2481 (table--probe-cell))
2482 edge)))
2483 ;; now search for the tip which is the highest/lowest below/above cell
2484 (while cell
2485 (let (below/above)
2486 (and (table--goto-coordinate
2487 (cons (car (table--get-coordinate (if (> arg 0) (car cell)
2488 (cdr cell))))
2489 (if (> arg 0) (+ 2 (cdr (table--get-coordinate (cdr cell))))
2490 (1- (cdr (table--get-coordinate (car pivot)))))) 'no-extension)
2491 (setq below/above (table--probe-cell))
2492 (or (null tip)
2493 (if (> arg 0)
2494 (< (cdr (table--get-coordinate (car below/above)))
2495 (cdr (table--get-coordinate (car tip))))
2496 (> (cdr (table--get-coordinate (car below/above)))
2497 (cdr (table--get-coordinate (car tip))))))
2498 (setq tip below/above)))
2499 (and (setq cell (table--goto-coordinate
2500 (cons (if (> arg 0) (1+ (car (table--get-coordinate (cdr cell))))
2501 (1- (car (table--get-coordinate (car cell)))))
2502 (if (> arg 0) (cdr (table--get-coordinate (car pivot)))
2503 (1- (cdr (table--get-coordinate (car pivot)))))) 'no-extension))
2504 (setq cell (table--probe-cell))))
2505 (if tip (goto-char (car tip)) ; done
2506 ;; let's climb up/down to the top/bottom from the edge
2507 (while (and (table--goto-coordinate
2508 (cons (if (> arg 0) (car (table--get-coordinate (car edge)))
2509 (car (table--get-coordinate (cdr edge))))
2510 (if (> arg 0) (1- (cdr (table--get-coordinate (car edge))))
2511 (+ 2 (cdr (table--get-coordinate (cdr edge)))))) 'no-extension)
2512 (setq cell (table--probe-cell))
2513 (setq edge cell)))
2514 (if (< arg 0)
2515 (progn
2516 (setq cell edge)
2517 (while (and (table--goto-coordinate
2518 (cons (1- (car (table--get-coordinate (car cell))))
2519 (cdr (table--get-coordinate (cdr cell)))) 'no-extension)
2520 (setq cell (table--probe-cell)))
2521 (if (> (cdr (table--get-coordinate (car cell)))
2522 (cdr (table--get-coordinate (car edge))))
2523 (setq edge cell)))))
2524 (goto-char (car edge))))) ; the top left cell
2525 (setq arg (if (> arg 0) (1- arg) (1+ arg))))
2526 (unless no-recognize
2527 (table-recognize-cell 'force nil (if unrecognize -1 nil)))) ; refill the cache with new cell contents
2529 ;;;###autoload
2530 (defun table-backward-cell (&optional arg)
2531 "Move backward to the beginning of the previous cell.
2532 With argument ARG, do it ARG times;
2533 a negative argument ARG = -N means move forward N cells."
2534 (interactive "p")
2535 (or arg (setq arg 1))
2536 (table-forward-cell (- arg)))
2538 ;;;###autoload
2539 (defun table-span-cell (direction)
2540 "Span current cell into adjacent cell in DIRECTION.
2541 DIRECTION is one of symbols; right, left, above or below."
2542 (interactive
2543 (list
2544 (let* ((_ (barf-if-buffer-read-only))
2545 (direction-list
2546 (let* ((tmp (delete nil
2547 (mapcar (lambda (d)
2548 (if (table--cell-can-span-p d)
2549 (list (symbol-name d))))
2550 '(right left above below)))))
2551 (if (null tmp)
2552 (error "Can't span this cell"))
2553 tmp))
2554 (default-direction (if (member (list (car table-cell-span-direction-history)) direction-list)
2555 (car table-cell-span-direction-history)
2556 (caar direction-list)))
2557 (completion-ignore-case t))
2558 (intern (downcase (completing-read
2559 (format "Span into (default %s): " default-direction)
2560 direction-list
2561 nil t nil 'table-cell-span-direction-history default-direction))))))
2562 (unless (memq direction '(right left above below))
2563 (error "Invalid direction %s, must be right, left, above or below"
2564 (symbol-name direction)))
2565 (table-recognize-cell 'force)
2566 (unless (table--cell-can-span-p direction)
2567 (error "Can't span %s" (symbol-name direction)))
2568 ;; Prepare beginning and end positions of the border bar to strike through.
2569 (let ((beg (save-excursion
2570 (table--goto-coordinate
2571 (cond
2572 ((eq direction 'right)
2573 (cons (car table-cell-info-rb-coordinate)
2574 (1- (cdr table-cell-info-lu-coordinate))))
2575 ((eq direction 'below)
2576 (cons (1- (car table-cell-info-lu-coordinate))
2577 (1+ (cdr table-cell-info-rb-coordinate))))
2579 (cons (1- (car table-cell-info-lu-coordinate))
2580 (1- (cdr table-cell-info-lu-coordinate)))))
2581 'no-extension)))
2582 (end (save-excursion
2583 (table--goto-coordinate
2584 (cond
2585 ((eq direction 'left)
2586 (cons (car table-cell-info-lu-coordinate)
2587 (1+ (cdr table-cell-info-rb-coordinate))))
2588 ((eq direction 'above)
2589 (cons (1+ (car table-cell-info-rb-coordinate))
2590 (1- (cdr table-cell-info-lu-coordinate))))
2592 (cons (1+ (car table-cell-info-rb-coordinate))
2593 (1+ (cdr table-cell-info-rb-coordinate)))))
2594 'no-extension))))
2595 ;; Replace the bar with blank space while taking care of edges to be border
2596 ;; or intersection.
2597 (save-excursion
2598 (goto-char beg)
2599 (if (memq direction '(left right))
2600 (let* ((column (current-column))
2601 rectangle
2602 (n-element (- (length (extract-rectangle beg end)) 2))
2603 (above-contp (and (goto-char beg)
2604 (zerop (forward-line -1))
2605 (= (move-to-column column) column)
2606 (looking-at (regexp-quote (char-to-string table-cell-vertical-char)))))
2607 (below-contp (and (goto-char end)
2608 (progn (forward-char -1) t)
2609 (zerop (forward-line 1))
2610 (= (move-to-column column) column)
2611 (looking-at (regexp-quote (char-to-string table-cell-vertical-char))))))
2612 (setq rectangle
2613 (cons (if below-contp
2614 (char-to-string table-cell-intersection-char)
2615 (substring table-cell-horizontal-chars 0 1))
2616 rectangle))
2617 (while (> n-element 0)
2618 (setq rectangle (cons (table--cell-blank-str 1) rectangle))
2619 (setq n-element (1- n-element)))
2620 (setq rectangle
2621 (cons (if above-contp
2622 (char-to-string table-cell-intersection-char)
2623 (substring table-cell-horizontal-chars 0 1))
2624 rectangle))
2625 (delete-rectangle beg end)
2626 (goto-char beg)
2627 (table--insert-rectangle rectangle))
2628 (delete-region beg end)
2629 (insert (if (and (> (point) (point-min))
2630 (save-excursion
2631 (forward-char -1)
2632 (looking-at (regexp-opt-charset
2633 (string-to-list table-cell-horizontal-chars)))))
2634 table-cell-intersection-char
2635 table-cell-vertical-char)
2636 (table--cell-blank-str (- end beg 2))
2637 (if (looking-at (regexp-opt-charset
2638 (string-to-list table-cell-horizontal-chars)))
2639 table-cell-intersection-char
2640 table-cell-vertical-char))))
2641 ;; recognize the newly created spanned cell
2642 (table-recognize-cell 'force)
2643 (if (member direction '(right left))
2644 (table-with-cache-buffer
2645 (table--fill-region (point-min) (point-max))
2646 (setq table-inhibit-auto-fill-paragraph t)))))
2648 ;;;###autoload
2649 (defun table-split-cell-vertically ()
2650 "Split current cell vertically.
2651 Creates a cell above and a cell below the current point location."
2652 (interactive "*")
2653 (table-recognize-cell 'force)
2654 (let ((point-y (cdr (table--get-coordinate))))
2655 (unless (table--cell-can-split-vertically-p)
2656 (error "Can't split here"))
2657 (let* ((old-coordinate (table--get-coordinate))
2658 (column (current-column))
2659 (beg (table--goto-coordinate
2660 (cons (1- (car table-cell-info-lu-coordinate))
2661 point-y)))
2662 (end (table--goto-coordinate
2663 (cons (1+ (car table-cell-info-rb-coordinate))
2664 point-y)))
2665 (line (buffer-substring (1+ beg) (1- end))))
2666 (when (= (cdr old-coordinate) (cdr table-cell-info-rb-coordinate))
2667 (table--goto-coordinate old-coordinate)
2668 (table-heighten-cell 1 'no-copy 'no-update))
2669 (goto-char beg)
2670 (delete-region beg end)
2671 (insert table-cell-intersection-char
2672 (make-string table-cell-info-width (string-to-char table-cell-horizontal-chars))
2673 table-cell-intersection-char)
2674 (table--goto-coordinate old-coordinate)
2675 (forward-line 1)
2676 (move-to-column column)
2677 (setq old-coordinate (table--get-coordinate))
2678 (table-recognize-cell 'force)
2679 (unless (string-match "^\\s *$" line)
2680 (table-with-cache-buffer
2681 (goto-char (point-min))
2682 (insert line ?\n)
2683 (goto-char (point-min)) ;; don't heighten cell unnecessarily
2684 (setq table-inhibit-auto-fill-paragraph t)))
2685 (table--update-cell 'now) ;; can't defer this operation
2686 (table--goto-coordinate old-coordinate)
2687 (move-to-column column)
2688 (table-recognize-cell 'force))))
2690 ;;;###autoload
2691 (defun table-split-cell-horizontally ()
2692 "Split current cell horizontally.
2693 Creates a cell on the left and a cell on the right of the current point location."
2694 (interactive "*")
2695 (table-recognize-cell 'force)
2696 (let* ((o-coordinate (table--get-coordinate))
2697 (point-x (car o-coordinate))
2698 cell-empty cell-contents cell-coordinate
2699 contents-to beg end rectangle strip-rect
2700 (right-edge (= (car o-coordinate) (1- (car table-cell-info-rb-coordinate)))))
2701 (unless (table--cell-can-split-horizontally-p)
2702 (error "Can't split here"))
2703 (let ((table-inhibit-update t))
2704 (table-with-cache-buffer
2705 (setq cell-coordinate (table--get-coordinate))
2706 (save-excursion
2707 (goto-char (point-min))
2708 (setq cell-empty (null (re-search-forward "\\S " nil t))))
2709 (setq cell-contents (buffer-substring (point-min) (point-max)))
2710 (setq table-inhibit-auto-fill-paragraph t)))
2711 (setq contents-to
2712 (if cell-empty 'left
2713 (let* ((completion-ignore-case t)
2714 (default (car table-cell-split-contents-to-history)))
2715 (intern
2716 (if (member 'click (event-modifiers last-input-event))
2717 (x-popup-menu last-input-event
2718 '("Existing cell contents to:"
2719 ("Title"
2720 ("Split" . "split") ("Left" . "left") ("Right" . "right"))))
2721 (downcase (completing-read
2722 (format "Existing cell contents to (default %s): " default)
2723 '(("split") ("left") ("right"))
2724 nil t nil 'table-cell-split-contents-to-history default)))))))
2725 (unless (eq contents-to 'split)
2726 (table-with-cache-buffer
2727 (erase-buffer)
2728 (setq table-inhibit-auto-fill-paragraph t)))
2729 (table--update-cell 'now)
2730 (setq beg (table--goto-coordinate
2731 (cons point-x
2732 (1- (cdr table-cell-info-lu-coordinate)))))
2733 (setq end (table--goto-coordinate
2734 (cons (1+ point-x)
2735 (1+ (cdr table-cell-info-rb-coordinate)))))
2736 (setq rectangle (cons (char-to-string table-cell-intersection-char) nil))
2737 (let ((n table-cell-info-height))
2738 (while (prog1 (> n 0) (setq n (1- n)))
2739 (setq rectangle (cons (char-to-string table-cell-vertical-char) rectangle))))
2740 (setq rectangle (cons (char-to-string table-cell-intersection-char) rectangle))
2741 (if (eq contents-to 'split)
2742 (setq strip-rect (extract-rectangle beg end)))
2743 (delete-rectangle beg end)
2744 (goto-char beg)
2745 (table--insert-rectangle rectangle)
2746 (table--goto-coordinate o-coordinate)
2747 (if cell-empty
2748 (progn
2749 (forward-char 1)
2750 (if right-edge
2751 (table-widen-cell 1)))
2752 (unless (eq contents-to 'left)
2753 (forward-char 1))
2754 (table-recognize-cell 'force)
2755 (table-with-cache-buffer
2756 (if (eq contents-to 'split)
2757 ;; split inserts strip-rect after removing
2758 ;; top and bottom borders
2759 (let ((o-coord (table--get-coordinate))
2760 (l (setq strip-rect (cdr strip-rect))))
2761 (while (cddr l) (setq l (cdr l)))
2762 (setcdr l nil)
2763 ;; insert the strip only when it is not a completely blank one
2764 (unless (let ((cl (mapcar (lambda (s) (string= s " ")) strip-rect)))
2765 (and (car cl)
2766 (table--uniform-list-p cl)))
2767 (goto-char (point-min))
2768 (table--insert-rectangle strip-rect)
2769 (table--goto-coordinate o-coord)))
2770 ;; left or right inserts original contents
2771 (erase-buffer)
2772 (insert cell-contents)
2773 (table--goto-coordinate cell-coordinate)
2774 (table--fill-region (point-min) (point-max))
2775 ;; avoid unnecessary vertical cell expansion
2776 (and (looking-at "\\s *\\'")
2777 (re-search-backward "\\S \\(\\s *\\)\\=" nil t)
2778 (goto-char (match-beginning 1))))
2779 ;; in either case do not fill paragraph
2780 (setq table-inhibit-auto-fill-paragraph t))
2781 (table--update-cell 'now)) ;; can't defer this operation
2782 (table-recognize-cell 'force)))
2784 ;;;###autoload
2785 (defun table-split-cell (orientation)
2786 "Split current cell in ORIENTATION.
2787 ORIENTATION is a symbol either horizontally or vertically."
2788 (interactive
2789 (list
2790 (let* ((_ (barf-if-buffer-read-only))
2791 (completion-ignore-case t)
2792 (default (car table-cell-split-orientation-history)))
2793 (intern (downcase (completing-read
2794 (format "Split orientation (default %s): " default)
2795 '(("horizontally") ("vertically"))
2796 nil t nil 'table-cell-split-orientation-history default))))))
2797 (unless (memq orientation '(horizontally vertically))
2798 (error "Invalid orientation %s, must be horizontally or vertically"
2799 (symbol-name orientation)))
2800 (if (eq orientation 'horizontally)
2801 (table-split-cell-horizontally)
2802 (table-split-cell-vertically)))
2804 ;;;###autoload
2805 (defun table-justify (what justify)
2806 "Justify contents of a cell, a row of cells or a column of cells.
2807 WHAT is a symbol 'cell, 'row or 'column. JUSTIFY is a symbol 'left,
2808 'center, 'right, 'top, 'middle, 'bottom or 'none."
2809 (interactive
2810 (list (let* ((_ (barf-if-buffer-read-only))
2811 (completion-ignore-case t)
2812 (default (car table-target-history)))
2813 (intern (downcase (completing-read
2814 (format "Justify what (default %s): " default)
2815 '(("cell") ("row") ("column"))
2816 nil t nil 'table-target-history default))))
2817 (table--query-justification)))
2818 (funcall (intern (concat "table-justify-" (symbol-name what))) justify))
2820 ;;;###autoload
2821 (defun table-justify-cell (justify &optional paragraph)
2822 "Justify cell contents.
2823 JUSTIFY is a symbol 'left, 'center or 'right for horizontal, or 'top,
2824 'middle, 'bottom or 'none for vertical. When optional PARAGRAPH is
2825 non-nil the justify operation is limited to the current paragraph,
2826 otherwise the entire cell contents is justified."
2827 (interactive
2828 (list (table--query-justification)))
2829 (table--finish-delayed-tasks)
2830 (table-recognize-cell 'force)
2831 (table--justify-cell-contents justify paragraph))
2833 ;;;###autoload
2834 (defun table-justify-row (justify)
2835 "Justify cells of a row.
2836 JUSTIFY is a symbol 'left, 'center or 'right for horizontal, or top,
2837 'middle, 'bottom or 'none for vertical."
2838 (interactive
2839 (list (table--query-justification)))
2840 (let((cell-list (table--horizontal-cell-list nil nil 'top)))
2841 (table--finish-delayed-tasks)
2842 (save-excursion
2843 (while cell-list
2844 (let ((cell (car cell-list)))
2845 (setq cell-list (cdr cell-list))
2846 (goto-char (car cell))
2847 (table-recognize-cell 'force)
2848 (table--justify-cell-contents justify))))))
2850 ;;;###autoload
2851 (defun table-justify-column (justify)
2852 "Justify cells of a column.
2853 JUSTIFY is a symbol 'left, 'center or 'right for horizontal, or top,
2854 'middle, 'bottom or 'none for vertical."
2855 (interactive
2856 (list (table--query-justification)))
2857 (let((cell-list (table--vertical-cell-list nil nil 'left)))
2858 (table--finish-delayed-tasks)
2859 (save-excursion
2860 (while cell-list
2861 (let ((cell (car cell-list)))
2862 (setq cell-list (cdr cell-list))
2863 (goto-char (car cell))
2864 (table-recognize-cell 'force)
2865 (table--justify-cell-contents justify))))))
2867 ;;;###autoload
2868 (define-minor-mode table-fixed-width-mode
2869 "Cell width is fixed when this is non-nil.
2870 Normally it should be nil for allowing automatic cell width expansion
2871 that widens a cell when it is necessary. When non-nil, typing in a
2872 cell does not automatically expand the cell width. A word that is too
2873 long to fit in a cell is chopped into multiple lines. The chopped
2874 location is indicated by `table-word-continuation-char'. This
2875 variable's value can be toggled by \\[table-fixed-width-mode] at
2876 run-time."
2877 :tag "Fix Cell Width"
2878 :group 'table
2879 (table--finish-delayed-tasks)
2880 (table--update-cell-face))
2882 ;;;###autoload
2883 (defun table-query-dimension (&optional where)
2884 "Return the dimension of the current cell and the current table.
2885 The result is a list (cw ch tw th c r cells) where cw is the cell
2886 width, ch is the cell height, tw is the table width, th is the table
2887 height, c is the number of columns, r is the number of rows and cells
2888 is the total number of cells. The cell dimension excludes the cell
2889 frame while the table dimension includes the table frame. The columns
2890 and the rows are counted by the number of cell boundaries. Therefore
2891 the number tends to be larger than it appears for the tables with
2892 non-uniform cell structure (heavily spanned and split). When optional
2893 WHERE is provided the cell and table at that location is reported."
2894 (interactive)
2895 (save-excursion
2896 (if where (goto-char where))
2897 (let ((starting-cell (table--probe-cell))
2898 cell table-lu table-rb col-list row-list (cells 0))
2899 (if (null starting-cell) nil
2900 (setq table-lu (car starting-cell))
2901 (setq table-rb (cdr starting-cell))
2902 (setq col-list (cons (car (table--get-coordinate (car starting-cell))) nil))
2903 (setq row-list (cons (cdr (table--get-coordinate (car starting-cell))) nil))
2904 (and (called-interactively-p 'interactive)
2905 (message "Computing cell dimension..."))
2906 (while
2907 (progn
2908 (table-forward-cell 1 t)
2909 (setq cells (1+ cells))
2910 (and (setq cell (table--probe-cell))
2911 (not (equal cell starting-cell))))
2912 (if (< (car cell) table-lu)
2913 (setq table-lu (car cell)))
2914 (if (> (cdr cell) table-rb)
2915 (setq table-rb (cdr cell)))
2916 (let ((lu-coordinate (table--get-coordinate (car cell))))
2917 (if (memq (car lu-coordinate) col-list) nil
2918 (setq col-list (cons (car lu-coordinate) col-list)))
2919 (if (memq (cdr lu-coordinate) row-list) nil
2920 (setq row-list (cons (cdr lu-coordinate) row-list)))))
2921 (let* ((cell-lu-coordinate (table--get-coordinate (car starting-cell)))
2922 (cell-rb-coordinate (table--get-coordinate (cdr starting-cell)))
2923 (table-lu-coordinate (table--get-coordinate table-lu))
2924 (table-rb-coordinate (table--get-coordinate table-rb))
2925 (cw (- (car cell-rb-coordinate) (car cell-lu-coordinate)))
2926 (ch (1+ (- (cdr cell-rb-coordinate) (cdr cell-lu-coordinate))))
2927 (tw (+ 2 (- (car table-rb-coordinate) (car table-lu-coordinate))))
2928 (th (+ 3 (- (cdr table-rb-coordinate) (cdr table-lu-coordinate))))
2929 (c (length col-list))
2930 (r (length row-list)))
2931 (and (called-interactively-p 'interactive)
2932 (message "Cell: (%dw, %dh), Table: (%dw, %dh), Dim: (%dc, %dr), Total Cells: %d" cw ch tw th c r cells))
2933 (list cw ch tw th c r cells))))))
2935 ;;;###autoload
2936 (defun table-generate-source (language &optional dest-buffer caption)
2937 "Generate source of the current table in the specified language.
2938 LANGUAGE is a symbol that specifies the language to describe the
2939 structure of the table. It must be either 'html, 'latex or 'cals.
2940 The resulted source text is inserted into DEST-BUFFER and the buffer
2941 object is returned. When DEST-BUFFER is omitted or nil the default
2942 buffer specified in `table-dest-buffer-name' is used. In this case
2943 the content of the default buffer is erased prior to the generation.
2944 When DEST-BUFFER is non-nil it is expected to be either a destination
2945 buffer or a name of the destination buffer. In this case the
2946 generated result is inserted at the current point in the destination
2947 buffer and the previously existing contents in the buffer are
2948 untouched.
2950 References used for this implementation:
2952 HTML:
2953 URL `http://www.w3.org'
2955 LaTeX:
2956 URL `http://www.maths.tcd.ie/~dwilkins/LaTeXPrimer/Tables.html'
2958 CALS (DocBook DTD):
2959 URL `http://www.oasis-open.org/html/a502.htm'
2960 URL `http://www.oreilly.com/catalog/docbook/chapter/book/table.html#AEN114751'
2962 (interactive
2963 (let* ((_ (unless (table--probe-cell) (error "Table not found here")))
2964 (completion-ignore-case t)
2965 (default (car table-source-language-history))
2966 (language (downcase (completing-read
2967 (format "Language (default %s): " default)
2968 (mapcar (lambda (s) (list (symbol-name s)))
2969 table-source-languages)
2970 nil t nil 'table-source-language-history default))))
2971 (list
2972 (intern language)
2973 (read-buffer "Destination buffer: " (concat table-dest-buffer-name "." language))
2974 (table--read-from-minibuffer '("Table Caption" . table-source-caption-history)))))
2975 (let ((default-buffer-name (concat table-dest-buffer-name "." (symbol-name language))))
2976 (unless (or (called-interactively-p 'interactive) (table--probe-cell))
2977 (error "Table not found here"))
2978 (unless (bufferp dest-buffer)
2979 (setq dest-buffer (get-buffer-create (or dest-buffer default-buffer-name))))
2980 (if (string= (buffer-name dest-buffer) default-buffer-name)
2981 (with-current-buffer dest-buffer
2982 (erase-buffer)))
2983 (save-excursion
2984 (let ((starting-cell (table--probe-cell))
2985 cell origin-cell tail-cell col-list row-list (n 0) i)
2986 ;; first analyze the table structure and prepare:
2987 ;; 1. origin cell (left up corner cell)
2988 ;; 2. tail cell (right bottom corner cell)
2989 ;; 3. column boundary list
2990 ;; 4. row boundary list
2991 (setq origin-cell starting-cell)
2992 (setq tail-cell starting-cell)
2993 (setq col-list (cons (car (table--get-coordinate (car starting-cell))) nil))
2994 (setq row-list (cons (cdr (table--get-coordinate (car starting-cell))) nil))
2995 (setq i 0)
2996 (let ((wheel [?- ?\\ ?| ?/]))
2997 (while
2998 (progn
2999 (if (called-interactively-p 'interactive)
3000 (progn
3001 (message "Analyzing table...%c" (aref wheel i))
3002 (if (eq (setq i (1+ i)) (length wheel))
3003 (setq i 0))
3004 (setq n (1+ n))))
3005 (table-forward-cell 1 t)
3006 (and (setq cell (table--probe-cell))
3007 (not (equal cell starting-cell))))
3008 (if (< (car cell) (car origin-cell))
3009 (setq origin-cell cell))
3010 (if (> (cdr cell) (cdr tail-cell))
3011 (setq tail-cell cell))
3012 (let ((lu-coordinate (table--get-coordinate (car cell))))
3013 (unless (memq (car lu-coordinate) col-list)
3014 (setq col-list (cons (car lu-coordinate) col-list)))
3015 (unless (memq (cdr lu-coordinate) row-list)
3016 (setq row-list (cons (cdr lu-coordinate) row-list))))))
3017 (setq col-list (sort col-list '<))
3018 (setq row-list (sort row-list '<))
3019 (message "Generating source...")
3020 ;; clear the source generation property list
3021 (setplist 'table-source-info-plist nil)
3022 ;; prepare to start from the origin cell
3023 (goto-char (car origin-cell))
3024 ;; first put some header information
3025 (table--generate-source-prologue dest-buffer language caption col-list row-list)
3026 (cond
3027 ((eq language 'latex)
3028 ;; scan by character lines
3029 (table--generate-source-scan-lines dest-buffer language origin-cell tail-cell col-list row-list))
3031 ;; scan by table cells
3032 (table--generate-source-scan-rows dest-buffer language origin-cell col-list row-list)))
3033 ;; insert closing
3034 (table--generate-source-epilogue dest-buffer language col-list row-list))
3035 ;; lastly do some convenience work
3036 (if (called-interactively-p 'interactive)
3037 (save-selected-window
3038 (pop-to-buffer dest-buffer t)
3039 (goto-char (point-min))
3040 (and (string= (buffer-name dest-buffer) default-buffer-name)
3041 (buffer-file-name dest-buffer)
3042 (save-buffer))
3043 (message "Generating source...done")
3044 (let ((mode
3045 (if (memq language '(cals)) 'sgml-mode
3046 (intern (concat (symbol-name language) "-mode")))))
3047 (if (fboundp mode)
3048 (call-interactively mode)))
3050 dest-buffer))
3052 (defun table--generate-source-prologue (dest-buffer language caption col-list _row-list)
3053 "Generate and insert source prologue into DEST-BUFFER."
3054 (with-current-buffer dest-buffer
3055 (cond
3056 ((eq language 'html)
3057 (insert (format "<!-- This HTML table template is generated by emacs %s -->\n" emacs-version)
3058 (format "<table %s>\n" table-html-table-attribute)
3059 (if (and (stringp caption)
3060 (not (string= caption "")))
3061 (format " <caption>%s</caption>\n" caption)
3062 "")))
3063 ((eq language 'latex)
3064 (insert (format "%% This LaTeX table template is generated by emacs %s\n" emacs-version)
3065 "\\begin{tabular}{|" (apply 'concat (make-list (length col-list) "l|")) "}\n"
3066 "\\hline\n"))
3067 ((eq language 'cals)
3068 (insert (format "<!-- This CALS table template is generated by emacs %s -->\n" emacs-version)
3069 "<table frame=\"all\">\n")
3070 (if (and (stringp caption)
3071 (not (string= caption "")))
3072 (insert " <title>" caption "</title>\n"))
3073 (insert (format " <tgroup cols=\"%d\" align=\"left\" colsep=\"1\" rowsep=\"1\">\n" (length col-list)))
3074 (table-put-source-info 'colspec-marker (point-marker))
3075 (table-put-source-info 'row-type (if (zerop table-cals-thead-rows) "tbody" "thead"))
3076 (set-marker-insertion-type (table-get-source-info 'colspec-marker) nil) ;; insert after
3077 (insert (format " <%s valign=\"top\">\n" (table-get-source-info 'row-type))))
3080 (defun table--generate-source-epilogue (dest-buffer language _col-list _row-list)
3081 "Generate and insert source epilogue into DEST-BUFFER."
3082 (with-current-buffer dest-buffer
3083 (cond
3084 ((eq language 'html)
3085 (insert "</table>\n"))
3086 ((eq language 'latex)
3087 (insert "\\end{tabular}\n"))
3088 ((eq language 'cals)
3089 (set-marker-insertion-type (table-get-source-info 'colspec-marker) t) ;; insert before
3090 (save-excursion
3091 (goto-char (table-get-source-info 'colspec-marker))
3092 (dolist (col (sort (table-get-source-info 'colnum-list) '<))
3093 (insert (format " <colspec colnum=\"%d\" colname=\"c%d\"/>\n" col col))))
3094 (insert (format " </%s>\n </tgroup>\n</table>\n" (table-get-source-info 'row-type))))
3097 (defun table--generate-source-scan-rows (dest-buffer language _origin-cell col-list row-list)
3098 "Generate and insert source rows into DEST-BUFFER."
3099 (table-put-source-info 'current-row 1)
3100 (while row-list
3101 (with-current-buffer dest-buffer
3102 (cond
3103 ((eq language 'html)
3104 (insert " <tr>\n"))
3105 ((eq language 'cals)
3106 (insert " <row>\n"))
3108 (table--generate-source-cells-in-a-row dest-buffer language col-list row-list)
3109 (with-current-buffer dest-buffer
3110 (cond
3111 ((eq language 'html)
3112 (insert " </tr>\n"))
3113 ((eq language 'cals)
3114 (insert " </row>\n")
3115 (unless (/= (table-get-source-info 'current-row) table-cals-thead-rows)
3116 (insert (format " </%s>\n" (table-get-source-info 'row-type)))
3117 (insert (format " <%s valign=\"top\">\n" (table-put-source-info 'row-type "tbody")))))))
3118 (table-put-source-info 'current-row (1+ (table-get-source-info 'current-row)))
3119 (setq row-list (cdr row-list))))
3121 (defun table--generate-source-cells-in-a-row (dest-buffer language col-list row-list)
3122 "Generate and insert source cells into DEST-BUFFER."
3123 (table-put-source-info 'current-column 1)
3124 (while col-list
3125 (let* ((cell (table--probe-cell))
3126 (lu (table--get-coordinate (car cell)))
3127 (rb (table--get-coordinate (cdr cell)))
3128 (alignment (table--get-cell-justify-property cell))
3129 (valign (table--get-cell-valign-property cell))
3130 (row-list row-list)
3131 (colspan 1)
3132 (rowspan 1))
3133 (if (< (car lu) (car col-list))
3134 (setq col-list nil)
3135 (while (and col-list
3136 (> (car lu) (car col-list)))
3137 (setq col-list (cdr col-list))
3138 (table-put-source-info 'current-column (1+ (table-get-source-info 'current-column))))
3139 (setq col-list (cdr col-list))
3140 (table-put-source-info 'next-column (1+ (table-get-source-info 'current-column)))
3141 (while (and col-list
3142 (> (1+ (car rb)) (car col-list)))
3143 (setq colspan (1+ colspan))
3144 (setq col-list (cdr col-list))
3145 (table-put-source-info 'next-column (1+ (table-get-source-info 'next-column))))
3146 (setq row-list (cdr row-list))
3147 (while (and row-list
3148 (> (+ (cdr rb) 2) (car row-list)))
3149 (setq rowspan (1+ rowspan))
3150 (setq row-list (cdr row-list)))
3151 (with-current-buffer dest-buffer
3152 (cond
3153 ((eq language 'html)
3154 (insert (format " <%s"
3155 (table-put-source-info
3156 'cell-type
3157 (if (or (<= (table-get-source-info 'current-row) table-html-th-rows)
3158 (<= (table-get-source-info 'current-column) table-html-th-columns))
3159 "th" "td"))))
3160 (if (and table-html-cell-attribute (not (string= table-html-cell-attribute "")))
3161 (insert " " table-html-cell-attribute))
3162 (if (> colspan 1) (insert (format " colspan=\"%d\"" colspan)))
3163 (if (> rowspan 1) (insert (format " rowspan=\"%d\"" rowspan)))
3164 (insert (format " align=\"%s\"" (if alignment (symbol-name alignment) "left")))
3165 (insert (format " valign=\"%s\"" (if valign (symbol-name valign) "top")))
3166 (insert ">\n"))
3167 ((eq language 'cals)
3168 (insert " <entry")
3169 (if (> colspan 1)
3170 (let ((scol (table-get-source-info 'current-column))
3171 (ecol (+ (table-get-source-info 'current-column) colspan -1)))
3172 (mapc (lambda (col)
3173 (unless (memq col (table-get-source-info 'colnum-list))
3174 (table-put-source-info 'colnum-list
3175 (cons col (table-get-source-info 'colnum-list)))))
3176 (list scol ecol))
3177 (insert (format " namest=\"c%d\" nameend=\"c%d\"" scol ecol))))
3178 (if (> rowspan 1) (insert (format " morerows=\"%d\"" (1- rowspan))))
3179 (if (and alignment
3180 (not (memq alignment '(left none))))
3181 (insert " align=\"" (symbol-name alignment) "\""))
3182 (if (and valign
3183 (not (memq valign '(top none))))
3184 (insert " valign=\"" (symbol-name valign) "\""))
3185 (insert ">\n"))
3187 (table--generate-source-cell-contents dest-buffer language cell)
3188 (with-current-buffer dest-buffer
3189 (cond
3190 ((eq language 'html)
3191 (insert (format" </%s>\n" (table-get-source-info 'cell-type))))
3192 ((eq language 'cals)
3193 (insert " </entry>\n"))
3195 (table-forward-cell 1 t)
3196 (table-put-source-info 'current-column (table-get-source-info 'next-column))
3197 ))))
3199 (defun table--generate-source-cell-contents (dest-buffer language cell)
3200 "Generate and insert source cell contents of a CELL into DEST-BUFFER."
3201 (let ((cell-contents (extract-rectangle (car cell) (cdr cell))))
3202 (with-temp-buffer
3203 (table--insert-rectangle cell-contents)
3204 (table--remove-cell-properties (point-min) (point-max))
3205 (goto-char (point-min))
3206 (cond
3207 ((eq language 'html)
3208 (if table-html-delegate-spacing-to-user-agent
3209 (progn
3210 (table--remove-eol-spaces (point-min) (point-max))
3211 (if (re-search-forward "\\s +\\'" nil t)
3212 (replace-match "")))
3213 (while (search-forward " " nil t)
3214 (replace-match "&nbsp;"))
3215 (goto-char (point-min))
3216 (while (and (re-search-forward "$" nil t)
3217 (not (eobp)))
3218 (insert "<br />")
3219 (forward-char 1)))
3220 (unless (and table-html-delegate-spacing-to-user-agent
3221 (progn
3222 (goto-char (point-min))
3223 (looking-at "\\s *\\'")))))
3224 ((eq language 'cals)
3225 (table--remove-eol-spaces (point-min) (point-max))
3226 (if (re-search-forward "\\s +\\'" nil t)
3227 (replace-match "")))
3229 (setq cell-contents (buffer-substring (point-min) (point-max))))
3230 (with-current-buffer dest-buffer
3231 (let ((beg (point)))
3232 (insert cell-contents)
3233 (indent-rigidly beg (point)
3234 (cond
3235 ((eq language 'html) 6)
3236 ((eq language 'cals) 10)))
3237 (insert ?\n)))))
3239 (defun table--cell-horizontal-char-p (c)
3240 "Test if character C is one of the horizontal characters"
3241 (memq c (string-to-list table-cell-horizontal-chars)))
3243 (defun table--generate-source-scan-lines (dest-buffer _language origin-cell tail-cell col-list row-list)
3244 "Scan the table line by line.
3245 Currently this method is for LaTeX only."
3246 (let* ((lu-coord (table--get-coordinate (car origin-cell)))
3247 (rb-coord (table--get-coordinate (cdr tail-cell)))
3248 (x0 (car lu-coord))
3249 (x1 (car rb-coord))
3250 (y (cdr lu-coord))
3251 (y1 (cdr rb-coord)))
3252 (while (<= y y1)
3253 (let* ((border-p (memq (1+ y) row-list))
3254 (border-char-list
3255 (mapcar (lambda (x)
3256 (if border-p (char-after (table--goto-coordinate (cons x y)))
3257 (char-before (table--goto-coordinate (cons x y)))))
3258 col-list))
3259 start i c)
3260 (if border-p
3261 ;; horizontal cell border processing
3262 (if (and (table--cell-horizontal-char-p (car border-char-list))
3263 (table--uniform-list-p border-char-list))
3264 (with-current-buffer dest-buffer
3265 (insert "\\hline\n"))
3266 (setq i 0)
3267 (while (setq c (nth i border-char-list))
3268 (if (and start (not (table--cell-horizontal-char-p c)))
3269 (progn
3270 (with-current-buffer dest-buffer
3271 (insert (format "\\cline{%d-%d}\n" (1+ start) i)))
3272 (setq start nil)))
3273 (if (and (not start) (table--cell-horizontal-char-p c))
3274 (setq start i))
3275 (setq i (1+ i)))
3276 (if start
3277 (with-current-buffer dest-buffer
3278 (insert (format "\\cline{%d-%d}\n" (1+ start) i)))))
3279 ;; horizontal cell contents processing
3280 (let* ((span 1) ;; spanning length
3281 (first-p t) ;; first in a row
3282 (insert-column ;; a function that processes one column/multicolumn
3283 (function
3284 (lambda (from to)
3285 (let ((line (table--buffer-substring-and-trim
3286 (table--goto-coordinate (cons from y))
3287 (table--goto-coordinate (cons to y)))))
3288 ;; escape special characters
3289 (with-temp-buffer
3290 (insert line)
3291 (goto-char (point-min))
3292 (while (re-search-forward "\\([#$~_^%{}]\\)\\|\\(\\\\\\)\\|\\([<>|]\\)" nil t)
3293 (if (match-beginning 1)
3294 (save-excursion
3295 (goto-char (match-beginning 1))
3296 (insert "\\"))
3297 (if (match-beginning 2)
3298 (replace-match "$\\backslash$" t t)
3299 (replace-match (concat "$" (match-string 3) "$")) t t)))
3300 (setq line (buffer-substring (point-min) (point-max))))
3301 ;; insert a column separator and column/multicolumn contents
3302 (with-current-buffer dest-buffer
3303 (unless first-p
3304 (insert (if (eq (char-before) ?\s) "" " ") "& "))
3305 (if (> span 1)
3306 (insert (format "\\multicolumn{%d}{%sl|}{%s}" span (if first-p "|" "") line))
3307 (insert line)))
3308 (setq first-p nil)
3309 (setq span 1)
3310 (setq start (nth i col-list)))))))
3311 (setq start x0)
3312 (setq i 1)
3313 (while (setq c (nth i border-char-list))
3314 (if (eq c table-cell-vertical-char)
3315 (funcall insert-column start (1- (nth i col-list)))
3316 (setq span (1+ span)))
3317 (setq i (1+ i)))
3318 (funcall insert-column start x1))
3319 (with-current-buffer dest-buffer
3320 (insert (if (eq (char-before) ?\s) "" " ") "\\\\\n"))))
3321 (setq y (1+ y)))
3322 (with-current-buffer dest-buffer
3323 (insert "\\hline\n"))
3326 ;;;###autoload
3327 (defun table-insert-sequence (str n increment interval justify)
3328 "Travel cells forward while inserting a specified sequence string in each cell.
3329 STR is the base string from which the sequence starts. When STR is an
3330 empty string then each cell content is erased. When STR ends with
3331 numerical characters (they may optionally be surrounded by a pair of
3332 parentheses) they are incremented as a decimal number. Otherwise the
3333 last character in STR is incremented in ASCII code order. N is the
3334 number of sequence elements to insert. When N is negative the cell
3335 traveling direction is backward. When N is zero it travels forward
3336 entire table. INCREMENT is the increment between adjacent sequence
3337 elements and can be a negative number for effectively decrementing.
3338 INTERVAL is the number of cells to travel between sequence element
3339 insertion which is normally 1. When zero or less is given for
3340 INTERVAL it is interpreted as number of cells per row so that sequence
3341 is placed straight down vertically as long as the table's cell
3342 structure is uniform. JUSTIFY is one of the symbol 'left, 'center or
3343 'right, that specifies justification of the inserted string.
3345 Example:
3347 (progn
3348 (table-insert 16 3 5 1)
3349 (table-forward-cell 15)
3350 (table-insert-sequence \"D0\" -16 1 1 'center)
3351 (table-forward-cell 16)
3352 (table-insert-sequence \"A[0]\" -16 1 1 'center)
3353 (table-forward-cell 1)
3354 (table-insert-sequence \"-\" 16 0 1 'center))
3356 (progn
3357 (table-insert 16 8 5 1)
3358 (table-insert-sequence \"@\" 0 1 2 'right)
3359 (table-forward-cell 1)
3360 (table-insert-sequence \"64\" 0 1 2 'left))"
3361 (interactive
3362 (progn
3363 (barf-if-buffer-read-only)
3364 (unless (table--probe-cell) (error "Table not found here"))
3365 (list (read-from-minibuffer
3366 "Sequence base string: " (car table-sequence-string-history) nil nil 'table-sequence-string-history)
3367 (string-to-number
3368 (table--read-from-minibuffer
3369 '("How many elements (0: maximum, negative: backward traveling)" . table-sequence-count-history)))
3370 (string-to-number
3371 (table--read-from-minibuffer
3372 '("Increment element by" . table-sequence-increment-history)))
3373 (string-to-number
3374 (table--read-from-minibuffer
3375 '("Cell interval (0: vertical, 1:horizontal)" . table-sequence-interval-history)))
3376 (let* ((completion-ignore-case t)
3377 (default (car table-sequence-justify-history)))
3378 (intern (downcase (completing-read
3379 (format "Justify (default %s): " default)
3380 '(("left") ("center") ("right"))
3381 nil t nil 'table-sequence-justify-history default)))))))
3382 (unless (or (called-interactively-p 'interactive) (table--probe-cell))
3383 (error "Table not found here"))
3384 (string-match "\\([0-9]*\\)\\([]})>]*\\)\\'" str)
3385 (if (called-interactively-p 'interactive)
3386 (message "Sequencing..."))
3387 (let* ((prefix (substring str 0 (match-beginning 1)))
3388 (index (match-string 1 str))
3389 (fmt (format "%%%s%dd" (if (eq (string-to-char index) ?0) "0" "") (length index)))
3390 (postfix (match-string 2 str))
3391 (dim (table-query-dimension))
3392 (cells (nth 6 dim))
3393 (direction (if (< n 0) -1 1))
3394 (interval-count 0))
3395 (if (string= index "")
3396 (progn
3397 (setq index nil)
3398 (if (string= prefix "")
3399 (setq prefix nil)))
3400 (setq index (string-to-number index)))
3401 (if (< n 0) (setq n (- n)))
3402 (if (or (zerop n) (> n cells)) (setq n cells))
3403 (if (< interval 0) (setq interval (- interval)))
3404 (if (zerop interval) (setq interval (nth 4 dim)))
3405 (save-excursion
3406 (while (progn
3407 (if (> interval-count 0) nil
3408 (setq interval-count interval)
3409 (table-with-cache-buffer
3410 (goto-char (point-min))
3411 (if (not (or prefix index))
3412 (erase-buffer)
3413 (insert prefix)
3414 (if index (insert (format fmt index)))
3415 (insert postfix)
3416 (table--fill-region (point-min) (point) table-cell-info-width justify)
3417 (setq table-cell-info-justify justify))
3418 (setq table-inhibit-auto-fill-paragraph t))
3419 (table--update-cell 'now)
3420 (if index
3421 (setq index (+ index increment))
3422 (if (and prefix (string= postfix ""))
3423 (let ((len-1 (1- (length prefix))))
3424 (setq prefix (concat (substring prefix 0 len-1)
3425 (char-to-string
3426 (+ (string-to-char (substring prefix len-1)) increment)))))))
3427 (setq n (1- n)))
3428 (table-forward-cell direction t)
3429 (setq interval-count (1- interval-count))
3430 (setq cells (1- cells))
3431 (and (> n 0) (> cells 0)))))
3432 (table-recognize-cell 'force)
3433 (if (called-interactively-p 'interactive)
3434 (message "Sequencing...done"))
3437 ;;;###autoload
3438 (defun table-delete-row (n)
3439 "Delete N row(s) of cells.
3440 Delete N rows of cells from current row. The current row is the row
3441 contains the current cell where point is located. Each row must
3442 consists from cells of same height."
3443 (interactive "*p")
3444 (let ((orig-coord (table--get-coordinate))
3445 (bt-coord (table--get-coordinate (cdr (table--vertical-cell-list nil 'first-only))))
3446 lu-coord rb-coord rect)
3447 ;; determine the area to delete while testing row height uniformity
3448 (while (> n 0)
3449 (setq n (1- n))
3450 (unless (table--probe-cell)
3451 (error "Table not found"))
3452 (let ((cell-list (table--horizontal-cell-list 'left-to-right)))
3453 (unless
3454 (and (table--uniform-list-p
3455 (mapcar (lambda (cell) (cdr (table--get-coordinate (car cell)))) cell-list))
3456 (table--uniform-list-p
3457 (mapcar (lambda (cell) (cdr (table--get-coordinate (cdr cell)))) cell-list)))
3458 (error "Cells in this row are not in uniform height"))
3459 (unless lu-coord
3460 (setq lu-coord (table--get-coordinate (caar cell-list))))
3461 (setq rb-coord (table--get-coordinate (cdar (last cell-list))))
3462 (table--goto-coordinate (cons (car orig-coord) (+ 2 (cdr rb-coord))))))
3463 ;; copy the remaining area (below the deleting area)
3464 (setq rect (extract-rectangle
3465 (table--goto-coordinate (cons (1- (car lu-coord)) (1+ (cdr rb-coord))))
3466 (table--goto-coordinate (cons (1+ (car rb-coord)) (1+ (cdr bt-coord))))))
3467 ;; delete the deleting area and below together
3468 (delete-rectangle
3469 (table--goto-coordinate (cons (1- (car lu-coord)) (1- (cdr lu-coord))))
3470 (table--goto-coordinate (cons (1+ (car rb-coord)) (1+ (cdr bt-coord)))))
3471 (table--goto-coordinate (cons (1- (car lu-coord)) (1- (cdr lu-coord))))
3472 ;; insert the remaining area while appending blank lines below it
3473 (table--insert-rectangle
3474 (append rect (make-list (+ 2 (- (cdr rb-coord) (cdr lu-coord)))
3475 (make-string (+ 2 (- (car rb-coord) (car lu-coord))) ?\s))))
3476 ;; remove the appended blank lines below the table if they are unnecessary
3477 (table--goto-coordinate (cons 0 (- (cdr bt-coord) (- (cdr rb-coord) (cdr lu-coord)))))
3478 (table--remove-blank-lines (+ 2 (- (cdr rb-coord) (cdr lu-coord))))
3479 ;; fix up intersections
3480 (let ((coord (cons (car lu-coord) (1- (cdr lu-coord))))
3481 (n (1+ (- (car rb-coord) (car lu-coord)))))
3482 (while (> n 0)
3483 (table--goto-coordinate coord)
3484 (if (save-excursion
3485 (or (and (table--goto-coordinate (cons (car coord) (1- (cdr coord))) 'no-extension)
3486 (looking-at (regexp-quote (char-to-string table-cell-vertical-char))))
3487 (and (table--goto-coordinate (cons (car coord) (1+ (cdr coord))) 'no-extension)
3488 (looking-at (regexp-quote (char-to-string table-cell-vertical-char))))))
3489 (progn
3490 (delete-char 1)
3491 (insert table-cell-intersection-char))
3492 (delete-char 1)
3493 (insert (string-to-char table-cell-horizontal-chars)))
3494 (setq n (1- n))
3495 (setcar coord (1+ (car coord)))))
3496 ;; goto appropriate end point
3497 (table--goto-coordinate (cons (car orig-coord) (cdr lu-coord)))))
3499 ;;;###autoload
3500 (defun table-delete-column (n)
3501 "Delete N column(s) of cells.
3502 Delete N columns of cells from current column. The current column is
3503 the column contains the current cell where point is located. Each
3504 column must consists from cells of same width."
3505 (interactive "*p")
3506 (let ((orig-coord (table--get-coordinate))
3507 lu-coord rb-coord)
3508 ;; determine the area to delete while testing column width uniformity
3509 (while (> n 0)
3510 (setq n (1- n))
3511 (unless (table--probe-cell)
3512 (error "Table not found"))
3513 (let ((cell-list (table--vertical-cell-list 'top-to-bottom)))
3514 (unless
3515 (and (table--uniform-list-p
3516 (mapcar (function (lambda (cell) (car (table--get-coordinate (car cell))))) cell-list))
3517 (table--uniform-list-p
3518 (mapcar (function (lambda (cell) (car (table--get-coordinate (cdr cell))))) cell-list)))
3519 (error "Cells in this column are not in uniform width"))
3520 (unless lu-coord
3521 (setq lu-coord (table--get-coordinate (caar cell-list))))
3522 (setq rb-coord (table--get-coordinate (cdar (last cell-list))))
3523 (table--goto-coordinate (cons (1+ (car rb-coord)) (cdr orig-coord)))))
3524 ;; delete the area
3525 (delete-rectangle
3526 (table--goto-coordinate (cons (car lu-coord) (1- (cdr lu-coord))))
3527 (table--goto-coordinate (cons (1+ (car rb-coord)) (1+ (cdr rb-coord)))))
3528 ;; fix up the intersections
3529 (let ((coord (cons (1- (car lu-coord)) (cdr lu-coord)))
3530 (n (1+ (- (cdr rb-coord) (cdr lu-coord)))))
3531 (while (> n 0)
3532 (table--goto-coordinate coord)
3533 (if (save-excursion
3534 (or (and (table--goto-coordinate (cons (1- (car coord)) (cdr coord)) 'no-extension)
3535 (looking-at (regexp-opt-charset
3536 (string-to-list table-cell-horizontal-chars))))
3537 (and (table--goto-coordinate (cons (1+ (car coord)) (cdr coord)) 'no-extension)
3538 (looking-at (regexp-opt-charset
3539 (string-to-list table-cell-horizontal-chars))))))
3540 (progn
3541 (delete-char 1)
3542 (insert table-cell-intersection-char))
3543 (delete-char 1)
3544 (insert table-cell-vertical-char))
3545 (setq n (1- n))
3546 (setcdr coord (1+ (cdr coord)))))
3547 ;; goto appropriate end point
3548 (table--goto-coordinate (cons (car lu-coord) (cdr orig-coord)))))
3550 ;;;###autoload
3551 (defun table-capture (beg end &optional col-delim-regexp row-delim-regexp justify min-cell-width columns)
3552 "Convert plain text into a table by capturing the text in the region.
3553 Create a table with the text in region as cell contents. BEG and END
3554 specify the region. The text in the region is replaced with a table.
3555 The removed text is inserted in the table. When optional
3556 COL-DELIM-REGEXP and ROW-DELIM-REGEXP are provided the region contents
3557 is parsed and separated into individual cell contents by using the
3558 delimiter regular expressions. This parsing determines the number of
3559 columns and rows of the table automatically. If COL-DELIM-REGEXP and
3560 ROW-DELIM-REGEXP are omitted the result table has only one cell and
3561 the entire region contents is placed in that cell. Optional JUSTIFY
3562 is one of 'left, 'center or 'right, which specifies the cell
3563 justification. Optional MIN-CELL-WIDTH specifies the minimum cell
3564 width. Optional COLUMNS specify the number of columns when
3565 ROW-DELIM-REGEXP is not specified.
3568 Example 1:
3570 1, 2, 3, 4
3571 5, 6, 7, 8
3572 , 9, 10
3574 Running `table-capture' on above 3 line region with COL-DELIM-REGEXP
3575 \",\" and ROW-DELIM-REGEXP \"\\n\" creates the following table. In
3576 this example the cells are centered and minimum cell width is
3577 specified as 5.
3579 +-----+-----+-----+-----+
3580 | 1 | 2 | 3 | 4 |
3581 +-----+-----+-----+-----+
3582 | 5 | 6 | 7 | 8 |
3583 +-----+-----+-----+-----+
3584 | | 9 | 10 | |
3585 +-----+-----+-----+-----+
3587 Note:
3589 In case the function is called interactively user must use \\[quoted-insert] `quoted-insert'
3590 in order to enter \"\\n\" successfully. COL-DELIM-REGEXP at the end
3591 of each row is optional.
3594 Example 2:
3596 This example shows how a table can be used for text layout editing.
3597 Let `table-capture' capture the following region starting from
3598 -!- and ending at -*-, that contains three paragraphs and two item
3599 name headers. This time specify empty string for both
3600 COL-DELIM-REGEXP and ROW-DELIM-REGEXP.
3602 -!-`table-capture' is a powerful command however mastering its power
3603 requires some practice. Here is a list of items what it can do.
3605 Parse Cell Items By using column delimiter regular
3606 expression and raw delimiter regular
3607 expression, it parses the specified text
3608 area and extracts cell items from
3609 non-table text and then forms a table out
3610 of them.
3612 Capture Text Area When no delimiters are specified it
3613 creates a single cell table. The text in
3614 the specified region is placed in that
3615 cell.-*-
3617 Now the entire content is captured in a cell which is itself a table
3618 like this.
3620 +-----------------------------------------------------------------+
3621 |`table-capture' is a powerful command however mastering its power|
3622 |requires some practice. Here is a list of items what it can do. |
3624 |Parse Cell Items By using column delimiter regular |
3625 | expression and raw delimiter regular |
3626 | expression, it parses the specified text |
3627 | area and extracts cell items from |
3628 | non-table text and then forms a table out |
3629 | of them. |
3631 |Capture Text Area When no delimiters are specified it |
3632 | creates a single cell table. The text in |
3633 | the specified region is placed in that |
3634 | cell. |
3635 +-----------------------------------------------------------------+
3637 By splitting the cell appropriately we now have a table consisting of
3638 paragraphs occupying its own cell. Each cell can now be edited
3639 independently.
3641 +-----------------------------------------------------------------+
3642 |`table-capture' is a powerful command however mastering its power|
3643 |requires some practice. Here is a list of items what it can do. |
3644 +---------------------+-------------------------------------------+
3645 |Parse Cell Items |By using column delimiter regular |
3646 | |expression and raw delimiter regular |
3647 | |expression, it parses the specified text |
3648 | |area and extracts cell items from |
3649 | |non-table text and then forms a table out |
3650 | |of them. |
3651 +---------------------+-------------------------------------------+
3652 |Capture Text Area |When no delimiters are specified it |
3653 | |creates a single cell table. The text in |
3654 | |the specified region is placed in that |
3655 | |cell. |
3656 +---------------------+-------------------------------------------+
3658 By applying `table-release', which does the opposite process, the
3659 contents become once again plain text. `table-release' works as
3660 companion command to `table-capture' this way.
3662 (interactive
3663 (let ((col-delim-regexp)
3664 (row-delim-regexp))
3665 (barf-if-buffer-read-only)
3666 (if (table--probe-cell)
3667 (error "Can't insert a table inside a table"))
3668 (list
3669 (mark) (point)
3670 (setq col-delim-regexp
3671 (read-from-minibuffer "Column delimiter regexp: "
3672 (car table-col-delim-regexp-history) nil nil 'table-col-delim-regexp-history))
3673 (setq row-delim-regexp
3674 (read-from-minibuffer "Row delimiter regexp: "
3675 (car table-row-delim-regexp-history) nil nil 'table-row-delim-regexp-history))
3676 (let* ((completion-ignore-case t)
3677 (default (car table-capture-justify-history)))
3678 (if (and (string= col-delim-regexp "") (string= row-delim-regexp "")) 'left
3679 (intern
3680 (downcase (completing-read
3681 (format "Justify (default %s): " default)
3682 '(("left") ("center") ("right"))
3683 nil t nil 'table-capture-justify-history default)))))
3684 (if (and (string= col-delim-regexp "") (string= row-delim-regexp "")) "1"
3685 (table--read-from-minibuffer '("Minimum cell width" . table-capture-min-cell-width-history)))
3686 (if (and (not (string= col-delim-regexp "")) (string= row-delim-regexp ""))
3687 (string-to-number
3688 (table--read-from-minibuffer '("Number of columns" . table-capture-columns-history)))
3689 nil)
3691 (if (> beg end) (let ((tmp beg)) (setq beg end) (setq end tmp)))
3692 (if (string= col-delim-regexp "") (setq col-delim-regexp nil))
3693 (if (string= row-delim-regexp "") (setq row-delim-regexp nil))
3694 (if (and columns (< columns 1)) (setq columns nil))
3695 (unless min-cell-width (setq min-cell-width "5"))
3696 (let ((contents (buffer-substring beg end))
3697 (cols 0) (rows 0) c r cell-list
3698 (delim-pattern
3699 (if (and col-delim-regexp row-delim-regexp)
3700 (format "\\(\\(%s\\)?\\s *\\(%s\\)\\s *\\)\\|\\(\\(%s\\)\\s *\\)"
3701 col-delim-regexp row-delim-regexp col-delim-regexp)
3702 (if col-delim-regexp
3703 (format "\\(\\)\\(\\)\\(\\)\\(\\(%s\\)\\s *\\)" col-delim-regexp))))
3704 (contents-list))
3705 ;; when delimiters are specified extract cells and determine the cell dimension
3706 (if delim-pattern
3707 (with-temp-buffer
3708 (insert contents)
3709 ;; make sure the contents ends with a newline
3710 (goto-char (point-max))
3711 (unless (zerop (current-column))
3712 (insert ?\n))
3713 ;; skip the preceding white spaces
3714 (goto-char (point-min))
3715 (if (looking-at "\\s +")
3716 (goto-char (match-end 0)))
3717 ;; extract cell contents
3718 (let ((from (point)))
3719 (setq cell-list nil)
3720 (setq c 0)
3721 (while (and (re-search-forward delim-pattern nil t)
3722 (cond
3723 ;; row delimiter
3724 ((and (match-string 1) (not (string= (match-string 1) "")))
3725 (setq rows (1+ rows))
3726 (setq cell-list
3727 (append cell-list (list (buffer-substring from (match-beginning 1)))))
3728 (setq from (match-end 1))
3729 (setq contents-list
3730 (append contents-list (list cell-list)))
3731 (setq cell-list nil)
3732 (setq c (1+ c))
3733 (if (> c cols) (setq cols c))
3734 (setq c 0)
3736 ;; column delimiter
3737 ((and (match-string 4) (not (string= (match-string 4) "")))
3738 (setq cell-list
3739 (append cell-list (list (buffer-substring from (match-beginning 4)))))
3740 (setq from (match-end 4))
3741 (setq c (1+ c))
3742 (if (> c cols) (setq cols c))
3744 (t nil))))
3745 ;; take care of the last element without a post delimiter
3746 (unless (null (looking-at ".+$"))
3747 (setq cell-list
3748 (append cell-list (list (match-string 0))))
3749 (setq cols (1+ cols)))
3750 ;; take care of the last row without a terminating delimiter
3751 (unless (null cell-list)
3752 (setq rows (1+ rows))
3753 (setq contents-list
3754 (append contents-list (list cell-list)))))))
3755 ;; finalize the table dimension
3756 (if (and columns contents-list)
3757 ;; when number of columns are specified and cells are parsed determine the dimension
3758 (progn
3759 (setq cols columns)
3760 (setq rows (/ (+ (length (car contents-list)) columns -1) columns)))
3761 ;; when dimensions are not specified default to a single cell table
3762 (if (zerop rows) (setq rows 1))
3763 (if (zerop cols) (setq cols 1)))
3764 ;; delete the region and reform line breaks
3765 (delete-region beg end)
3766 (goto-char beg)
3767 (unless (zerop (current-column))
3768 (insert ?\n))
3769 (unless (looking-at "\\s *$")
3770 (save-excursion
3771 (insert ?\n)))
3772 ;; insert the table
3773 ;; insert the cell contents
3774 (if (null contents-list)
3775 ;; single cell
3776 (let ((width) (height))
3777 (with-temp-buffer
3778 (insert contents)
3779 (table--remove-eol-spaces (point-min) (point-max))
3780 (table--untabify (point-min) (point-max))
3781 (setq width (table--measure-max-width))
3782 (setq height (1+ (table--current-line (point-max))))
3783 (setq contents (buffer-substring (point-min) (point-max))))
3784 (table-insert cols rows width height)
3785 (table-with-cache-buffer
3786 (insert contents)
3787 (setq table-inhibit-auto-fill-paragraph t)))
3788 ;; multi cells
3789 (table-insert cols rows min-cell-width 1)
3790 (setq r 0)
3791 (setq cell-list nil)
3792 (while (< r rows)
3793 (setq r (1+ r))
3794 (setq c 0)
3795 (unless cell-list
3796 (setq cell-list (car contents-list))
3797 (setq contents-list (cdr contents-list)))
3798 (while (< c cols)
3799 (setq c (1+ c))
3800 (if (car cell-list)
3801 (table-with-cache-buffer
3802 (insert (car cell-list))
3803 (setq cell-list (cdr cell-list))
3804 (setq table-cell-info-justify justify)))
3805 (table-forward-cell 1))))))
3807 ;;;###autoload
3808 (defun table-release ()
3809 "Convert a table into plain text by removing the frame from a table.
3810 Remove the frame from a table and deactivate the table. This command
3811 converts a table into plain text without frames. It is a companion to
3812 `table-capture' which does the opposite process."
3813 (interactive)
3814 (let ((origin-cell (table--probe-cell))
3815 table-lu table-rb)
3816 (if origin-cell
3817 (let ((old-point (point-marker)))
3818 ;; save-excursion is not sufficient for this
3819 ;; because untabify operation moves point
3820 (set-marker-insertion-type old-point t)
3821 (unwind-protect
3822 (progn
3823 (while
3824 (progn
3825 (table-forward-cell 1 nil 'unrecognize)
3826 (let ((cell (table--probe-cell)))
3827 (if (or (null table-lu)
3828 (< (car cell) table-lu))
3829 (setq table-lu (car cell)))
3830 (if (or (null table-rb)
3831 (> (cdr cell) table-rb))
3832 (setq table-rb (cdr cell)))
3833 (and cell (not (equal cell origin-cell))))))
3834 (let* ((lu-coord (table--get-coordinate table-lu))
3835 (rb-coord (table--get-coordinate table-rb))
3836 (lu (table--goto-coordinate (table--offset-coordinate lu-coord '(-1 . -1)))))
3837 (table--spacify-frame)
3838 (setcdr rb-coord (1+ (cdr rb-coord)))
3839 (delete-rectangle lu (table--goto-coordinate (cons (car lu-coord) (cdr rb-coord))))
3840 (table--remove-eol-spaces
3841 (table--goto-coordinate (cons 0 (1- (cdr lu-coord))))
3842 (table--goto-coordinate rb-coord) nil t)))
3843 (goto-char old-point))))))
3845 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3847 ;; Worker functions (executed implicitly)
3850 (defun table--make-cell-map ()
3851 "Make the table cell keymap if it does not exist yet."
3852 ;; This is irrelevant to keymap but good place to make sure to be executed.
3853 (table--update-cell-face)
3854 (unless table-cell-map
3855 (let ((map (make-sparse-keymap)))
3856 ;; `table-command-prefix' mode specific bindings.
3857 (if (vectorp table-command-prefix)
3858 (dolist (binding table-cell-bindings)
3859 (let ((seq (copy-sequence (car binding))))
3860 (and (vectorp seq)
3861 (listp (aref seq 0))
3862 (eq (car (aref seq 0)) 'control)
3863 (progn
3864 (aset seq 0 (cadr (aref seq 0)))
3865 (define-key map (vconcat table-command-prefix seq)
3866 (cdr binding)))))))
3867 ;; Shorthand control bindings.
3868 (dolist (binding table-cell-bindings)
3869 (define-key map (car binding) (cdr binding)))
3870 ;; Remap normal commands to table specific version.
3871 (dolist (remap table-command-remap-alist)
3872 (define-key map (vector 'remap (car remap)) (cdr remap)))
3874 (setq table-cell-map map)
3875 (fset 'table-cell-map map)))
3876 ;; Add menu for table cells.
3877 (unless table-disable-menu
3878 (easy-menu-define table-cell-menu-map table-cell-map
3879 "Table cell menu" table-cell-menu)
3880 (if (featurep 'xemacs)
3881 (easy-menu-add table-cell-menu)))
3882 (run-hooks 'table-cell-map-hook))
3884 ;; Create the keymap after running the user init file so that the user
3885 ;; modification to the global-map is accounted.
3886 (add-hook 'after-init-hook 'table--make-cell-map t)
3888 (defun *table--cell-self-insert-command ()
3889 "Table cell version of `self-insert-command'."
3890 (interactive "*")
3891 (let ((char last-command-event))
3892 (if (eq buffer-undo-list t) nil
3893 (if (not (eq last-command this-command))
3894 (setq table-cell-self-insert-command-count 0)
3895 (if (car buffer-undo-list) nil
3896 (if (>= table-cell-self-insert-command-count 19)
3897 (setq table-cell-self-insert-command-count 0)
3898 (setq buffer-undo-list (cdr buffer-undo-list))
3899 (setq table-cell-self-insert-command-count (1+ table-cell-self-insert-command-count))))))
3900 (table--cell-insert-char char overwrite-mode)))
3902 (defun *table--cell-delete-backward-char (n)
3903 "Table cell version of `delete-backward-char'."
3904 (interactive "*p")
3905 (*table--cell-delete-char (- n)))
3907 (defun *table--cell-newline (&optional indent)
3908 "Table cell version of `newline'."
3909 (interactive "*")
3910 (table-with-cache-buffer
3911 (let ((column (current-column)))
3912 (insert ?\n)
3913 (if indent (indent-to-column column))
3914 ;; fill only when at the beginning of paragraph
3915 (if (= (point)
3916 (save-excursion
3917 (forward-paragraph -1)
3918 (if (looking-at "\\s *$")
3919 (forward-line 1))
3920 (point)))
3921 nil ; yes, at the beginning of the paragraph
3922 (setq table-inhibit-auto-fill-paragraph t)))))
3924 (defun *table--cell-open-line (n)
3925 "Table cell version of `open-line'."
3926 (interactive "*p")
3927 (table-with-cache-buffer
3928 (save-excursion
3929 (insert (make-string n ?\n))
3930 (table--fill-region (point) (point))
3931 (setq table-inhibit-auto-fill-paragraph t))))
3933 (defun *table--cell-newline-and-indent ()
3934 "Table cell version of `newline-and-indent'."
3935 (interactive)
3936 (*table--cell-newline t))
3938 (defun *table--cell-delete-char (n)
3939 "Table cell version of `delete-char'."
3940 (interactive "*p")
3941 (let ((overwrite overwrite-mode))
3942 (table-with-cache-buffer
3943 (if (and overwrite (< n 0))
3944 (progn
3945 (while (not (zerop n))
3946 (let ((coordinate (table--get-coordinate)))
3947 (if (zerop (car coordinate))
3948 (unless (zerop (cdr coordinate))
3949 (table--goto-coordinate (cons (1- table-cell-info-width) (1- (cdr coordinate))))
3950 (unless (eolp)
3951 (delete-char 1)))
3952 (delete-char -1)
3953 (insert ?\s)
3954 (forward-char -1)))
3955 (setq n (1+ n)))
3956 (setq table-inhibit-auto-fill-paragraph t))
3957 (let ((coordinate (table--get-coordinate))
3958 (end-marker (copy-marker (+ (point) n)))
3959 (deleted))
3960 (if (or (< end-marker (point-min))
3961 (> end-marker (point-max))) nil
3962 (table--remove-eol-spaces (point-min) (point-max))
3963 (setq deleted (buffer-substring (point) end-marker))
3964 (delete-char n)
3965 ;; in fixed width mode when two lines are concatenated
3966 ;; remove continuation character if there is one.
3967 (and table-fixed-width-mode
3968 (string-match "^\n" deleted)
3969 (equal (char-before) table-word-continuation-char)
3970 (delete-char -2))
3971 ;; see if the point is placed at the right tip of the previous
3972 ;; blank line, if so get rid of the preceding blanks.
3973 (if (and (not (bolp))
3974 (/= (cdr coordinate) (cdr (table--get-coordinate)))
3975 (let ((end (point)))
3976 (save-excursion
3977 (beginning-of-line)
3978 (re-search-forward "\\s +" end t)
3979 (= (point) end))))
3980 (replace-match ""))
3981 ;; do not fill the paragraph if the point is already at the end
3982 ;; of this paragraph and is following a blank character
3983 ;; (otherwise the filling squeezes the preceding blanks)
3984 (if (and (looking-at "\\s *$")
3985 (or (bobp)
3986 (save-excursion
3987 (backward-char)
3988 (looking-at "\\s "))))
3989 (setq table-inhibit-auto-fill-paragraph t))
3991 (set-marker end-marker nil))))))
3993 (defun *table--cell-quoted-insert (arg)
3994 "Table cell version of `quoted-insert'."
3995 (interactive "*p")
3996 (let ((char (read-quoted-char)))
3997 (while (> arg 0)
3998 (table--cell-insert-char char nil)
3999 (setq arg (1- arg)))))
4001 (defun *table--cell-describe-mode ()
4002 "Table cell version of `describe-mode'."
4003 (interactive)
4004 (if (not (table--point-in-cell-p))
4005 (call-interactively 'describe-mode)
4006 (with-output-to-temp-buffer "*Help*"
4007 (princ "Table mode: (in ")
4008 (princ (format-mode-line mode-name nil nil (current-buffer)))
4009 (princ " mode)
4011 Table is not a mode technically. You can regard it as a pseudo mode
4012 which exists locally within a buffer. It overrides some standard
4013 editing behaviors. Editing operations in a table produces confined
4014 effects to the current cell. It may grow the cell horizontally and/or
4015 vertically depending on the newly entered or deleted contents of the
4016 cell, and also depending on the current mode of cell.
4018 In the normal mode the table preserves word continuity. Which means
4019 that a word never gets folded into multiple lines. For this purpose
4020 table will occasionally grow the cell width. On the other hand, when
4021 in a fixed width mode all cell width are fixed. When a word can not
4022 fit in the cell width the word is folded into the next line. The
4023 folded location is marked by a continuation character which is
4024 specified in the variable `table-word-continuation-char'.
4026 (help-print-return-message))))
4028 (defun *table--cell-describe-bindings ()
4029 "Table cell version of `describe-bindings'."
4030 (interactive)
4031 (if (not (table--point-in-cell-p))
4032 (call-interactively 'describe-bindings)
4033 (with-output-to-temp-buffer "*Help*"
4034 (princ "Table Bindings:
4035 key binding
4036 --- -------
4039 (mapc (lambda (binding)
4040 (princ (format "%-16s%s\n"
4041 (key-description (car binding))
4042 (cdr binding))))
4043 table-cell-bindings)
4044 (help-print-return-message))))
4046 (defvar dabbrev-abbrev-char-regexp)
4048 (defun *table--cell-dabbrev-expand (arg)
4049 "Table cell version of `dabbrev-expand'."
4050 (interactive "*P")
4051 (let ((dabbrev-abbrev-char-regexp (concat "[^"
4052 (char-to-string table-cell-vertical-char)
4053 (char-to-string table-cell-intersection-char)
4054 " \n]")))
4055 (table-with-cache-buffer
4056 (dabbrev-expand arg))))
4058 (defun *table--cell-dabbrev-completion (&optional arg)
4059 "Table cell version of `dabbrev-completion'."
4060 (interactive "*P")
4061 (error "`dabbrev-completion' is incompatible with table")
4062 (let ((dabbrev-abbrev-char-regexp (concat "[^"
4063 (char-to-string table-cell-vertical-char)
4064 (char-to-string table-cell-intersection-char)
4065 " \n]")))
4066 (table-with-cache-buffer
4067 (dabbrev-completion arg))))
4069 (defun *table--present-cell-popup-menu (event)
4070 "Present and handle cell popup menu."
4071 (interactive "e")
4072 (unless table-disable-menu
4073 (select-window (posn-window (event-start event)))
4074 (goto-char (posn-point (event-start event)))
4075 (let ((item-list (x-popup-menu event table-cell-menu-map))
4076 (func table-cell-menu-map))
4077 (while item-list
4078 (setq func (nth 3 (assoc (car item-list) func)))
4079 (setq item-list (cdr item-list)))
4080 (if (and (symbolp func) (fboundp func))
4081 (call-interactively func)))))
4083 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4085 ;; Cell updating functions
4088 (defun table--update-cell (&optional now)
4089 "Update the table cell contents.
4090 When the optional parameter NOW is nil it only sets up the update
4091 timer. If it is non-nil the function copies the contents of the cell
4092 cache buffer into the designated cell in the table buffer."
4093 (if (null table-update-timer) nil
4094 (table--cancel-timer table-update-timer)
4095 (setq table-update-timer nil))
4096 (if (or (not now)
4097 (and (boundp 'quail-converting)
4098 quail-converting) ;; defer operation while current quail work is not finished.
4099 (and (boundp 'quail-translating)
4100 quail-translating))
4101 (setq table-update-timer
4102 (table--set-timer table-time-before-update
4103 (function table--update-cell)
4104 'now))
4105 (save-current-buffer
4106 (set-buffer table-cell-buffer)
4107 (let ((cache-buffer (get-buffer-create table-cache-buffer-name))
4108 (org-coord (table--get-coordinate))
4109 (in-cell (equal (table--cell-to-coord (table--probe-cell))
4110 (cons table-cell-info-lu-coordinate table-cell-info-rb-coordinate)))
4111 rectangle)
4112 (set-buffer cache-buffer)
4113 (setq rectangle
4114 (extract-rectangle
4116 (table--goto-coordinate (cons table-cell-info-width (1- table-cell-info-height)))))
4117 (set-buffer table-cell-buffer)
4118 (delete-rectangle (table--goto-coordinate table-cell-info-lu-coordinate)
4119 (table--goto-coordinate table-cell-info-rb-coordinate))
4120 (table--goto-coordinate table-cell-info-lu-coordinate)
4121 (table--insert-rectangle rectangle)
4122 (let* ((cell (table--probe-cell))) ; must probe again in case of wide characters
4123 (table--put-cell-property cell)
4124 (table--put-cell-justify-property cell table-cell-info-justify)
4125 (table--put-cell-valign-property cell table-cell-info-valign))
4126 (table--goto-coordinate
4127 (if in-cell
4128 (table--transcoord-cache-to-table table-cell-cache-point-coordinate)
4129 org-coord))))
4130 ;; simulate undo behavior under overwrite-mode
4131 (if (and overwrite-mode (not (eq buffer-undo-list t)))
4132 (setq buffer-undo-list (cons nil buffer-undo-list)))))
4134 (defun table--update-cell-widened (&optional now)
4135 "Update the contents of the cells that are affected by widening operation."
4136 (if (null table-widen-timer) nil
4137 (table--cancel-timer table-widen-timer)
4138 (setq table-widen-timer nil))
4139 (if (not now)
4140 (setq table-widen-timer
4141 (table--set-timer (+ table-time-before-update table-time-before-reformat)
4142 (function table--update-cell-widened)
4143 'now))
4144 (save-current-buffer
4145 (if table-update-timer
4146 (table--update-cell 'now))
4147 (set-buffer table-cell-buffer)
4148 (let* ((current-coordinate (table--get-coordinate))
4149 (current-cell-coordinate (table--cell-to-coord (table--probe-cell)))
4150 (cell-coord-list (progn
4151 (table--goto-coordinate table-cell-info-lu-coordinate)
4152 (table--cell-list-to-coord-list (table--vertical-cell-list)))))
4153 (while cell-coord-list
4154 (let* ((cell-coord (prog1 (car cell-coord-list) (setq cell-coord-list (cdr cell-coord-list))))
4155 (currentp (equal cell-coord current-cell-coordinate)))
4156 (if currentp (table--goto-coordinate current-coordinate)
4157 (table--goto-coordinate (car cell-coord)))
4158 (table-recognize-cell 'froce)
4159 (let ((table-inhibit-update t))
4160 (table-with-cache-buffer
4161 (let ((sticky (and currentp
4162 (save-excursion
4163 (unless (bolp) (forward-char -1))
4164 (looking-at ".*\\S ")))))
4165 (table--fill-region (point-min) (point-max))
4166 (if sticky
4167 (setq current-coordinate (table--transcoord-cache-to-table))))))
4168 (table--update-cell 'now)
4170 (table--goto-coordinate current-coordinate)
4171 (table-recognize-cell 'froce)))))
4173 (defun table--update-cell-heightened (&optional now)
4174 "Update the contents of the cells that are affected by heightening operation."
4175 (if (null table-heighten-timer) nil
4176 (table--cancel-timer table-heighten-timer)
4177 (setq table-heighten-timer nil))
4178 (if (not now)
4179 (setq table-heighten-timer
4180 (table--set-timer (+ table-time-before-update table-time-before-reformat)
4181 (function table--update-cell-heightened)
4182 'now))
4183 (save-current-buffer
4184 (if table-update-timer
4185 (table--update-cell 'now))
4186 (if table-widen-timer
4187 (table--update-cell-widened 'now))
4188 (set-buffer table-cell-buffer)
4189 (let* ((current-coordinate (table--get-coordinate))
4190 (current-cell-coordinate (table--cell-to-coord (table--probe-cell)))
4191 (cell-coord-list (progn
4192 (table--goto-coordinate table-cell-info-lu-coordinate)
4193 (table--cell-list-to-coord-list (table--horizontal-cell-list)))))
4194 (while cell-coord-list
4195 (let* ((cell-coord (prog1 (car cell-coord-list) (setq cell-coord-list (cdr cell-coord-list))))
4196 (currentp (equal cell-coord current-cell-coordinate)))
4197 (if currentp (table--goto-coordinate current-coordinate)
4198 (table--goto-coordinate (car cell-coord)))
4199 (table-recognize-cell 'froce)
4200 (let ((table-inhibit-update t))
4201 (table-with-cache-buffer
4202 (let ((sticky (and currentp
4203 (save-excursion
4204 (unless (bolp) (forward-char -1))
4205 (looking-at ".*\\S ")))))
4206 (table--valign)
4207 (if sticky
4208 (setq current-coordinate (table--transcoord-cache-to-table))))))
4209 (table--update-cell 'now)
4211 (table--goto-coordinate current-coordinate)
4212 (table-recognize-cell 'froce)))))
4214 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4216 ;; Service functions (for external packages)
4219 (defun table-goto-top-left-corner ()
4220 "Move point to top left corner of the current table and return the char position."
4221 (table--goto-coordinate
4222 (cons
4223 (1- (car (table--get-coordinate (car (table--horizontal-cell-list t t)))))
4224 (1- (cdr (table--get-coordinate (car (table--vertical-cell-list t t))))))))
4226 (defun table-goto-top-right-corner ()
4227 "Move point to top right corner of the current table and return the char position."
4228 (table--goto-coordinate
4229 (cons
4230 (car (table--get-coordinate (cdr (table--horizontal-cell-list nil t))))
4231 (1- (cdr (table--get-coordinate (car (table--vertical-cell-list t t))))))))
4233 (defun table-goto-bottom-left-corner ()
4234 "Move point to bottom left corner of the current table and return the char position."
4235 (table--goto-coordinate
4236 (cons
4237 (1- (car (table--get-coordinate (car (table--horizontal-cell-list t t)))))
4238 (1+ (cdr (table--get-coordinate (cdr (table--vertical-cell-list nil t))))))))
4240 (defun table-goto-bottom-right-corner ()
4241 "Move point to bottom right corner of the current table and return the char position."
4242 (table--goto-coordinate
4243 (cons
4244 (car (table--get-coordinate (cdr (table--horizontal-cell-list nil t))))
4245 (1+ (cdr (table--get-coordinate (cdr (table--vertical-cell-list nil t))))))))
4247 (defun table-function (function)
4248 ;; FIXME: Apparently unused. There used to be table-funcall, table-apply,
4249 ;; and table-call-interactively instead, neither of which seemed to be
4250 ;; used either.
4251 "Return FUNCTION, or a table version of it if applicable."
4252 (let ((table-func (intern-soft (format "*table--cell-%s" function))))
4253 (if (and table-func
4254 (table--point-in-cell-p))
4255 table-func
4256 function)))
4258 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4260 ;; Utility functions
4263 (defun table--read-from-minibuffer (prompt-history)
4264 "A wrapper to `read-from-minibuffer'.
4265 PROMPT-HISTORY is a cons cell which car is the prompt string and the
4266 cdr is the history symbol."
4267 (let ((default (car (symbol-value (cdr prompt-history)))))
4268 (read-from-minibuffer
4269 (format "%s (default %s): " (car prompt-history) default)
4270 "" nil nil (cdr prompt-history) default))
4271 (and (featurep 'xemacs)
4272 (equal (car (symbol-value (cdr prompt-history))) "")
4273 (set (cdr prompt-history)
4274 (cdr (symbol-value (cdr prompt-history)))))
4275 (car (symbol-value (cdr prompt-history))))
4277 (defun table--buffer-substring-and-trim (beg end)
4278 "Extract buffer substring and remove blanks from front and the rear of it."
4279 (save-excursion
4280 (save-restriction
4281 (narrow-to-region (goto-char beg) end)
4282 (if (re-search-forward "\\s *")
4283 (setq beg (match-end 0)))
4284 (if (re-search-forward "\\s *\\'" end t)
4285 (setq end (match-beginning 0)))
4286 (table--remove-cell-properties
4287 0 (- end beg)
4288 (buffer-substring beg end)))))
4290 (defun table--valign ()
4291 "Vertically align the cache cell contents.
4292 Current buffer must be the cache buffer at the entry to this function.
4293 Returns the coordinate of the final point location."
4294 (if (or (null table-cell-info-valign)
4295 (eq table-cell-info-valign 'none))
4296 (table--get-coordinate)
4297 (let ((saved-point (point-marker)))
4298 ;;(set-marker-insertion-type saved-point t)
4299 (goto-char (point-min))
4300 (let* ((from (and (re-search-forward "^.*\\S " nil t)
4301 (table--current-line)))
4302 (to (let ((tmp from))
4303 (while (re-search-forward "^.*\\S " nil t)
4304 (setq tmp (table--current-line)))
4305 tmp))
4306 (content-height (and from to (1+ (- to from)))))
4307 (unless (null content-height)
4308 (goto-char (point-min))
4309 (if (looking-at "\\s *\n")
4310 (replace-match ""))
4311 (cond ((eq table-cell-info-valign 'middle)
4312 (insert (make-string (/ (- table-cell-info-height content-height) 2) ?\n)))
4313 ((eq table-cell-info-valign 'bottom)
4314 (insert (make-string (- table-cell-info-height content-height) ?\n))))
4315 (table--goto-coordinate (cons table-cell-info-width (1- table-cell-info-height)))
4316 (if (re-search-forward "\\s +\\'" nil t)
4317 (replace-match ""))))
4318 (goto-char saved-point)
4319 (set-marker saved-point nil)
4320 (let ((coord (table--get-coordinate)))
4321 (unless (< (cdr coord) table-cell-info-height)
4322 (setcdr coord (1- table-cell-info-height))
4323 (table--goto-coordinate coord))
4324 coord))))
4326 (defun table--query-justification ()
4327 (barf-if-buffer-read-only)
4328 (let* ((completion-ignore-case t)
4329 (default (car table-justify-history)))
4330 (intern (downcase (completing-read
4331 (format "Justify (default %s): " default)
4332 '(("left") ("center") ("right") ("top") ("middle") ("bottom") ("none"))
4333 nil t nil 'table-justify-history default)))))
4335 (defun table--spacify-frame ()
4336 "Spacify table frame.
4337 Replace frame characters with spaces."
4338 (let ((frame-char
4339 (append (string-to-list table-cell-horizontal-chars)
4340 (list table-cell-intersection-char table-cell-vertical-char))))
4341 (while
4342 (progn
4343 (cond
4344 ((eq (char-after) table-cell-intersection-char)
4345 (save-excursion
4346 (let ((col (current-column)))
4347 (and (zerop (forward-line 1))
4348 (zerop (current-column))
4349 (move-to-column col)
4350 (table--spacify-frame))))
4351 (delete-char 1)
4352 (insert-before-markers ?\s))
4353 ((table--cell-horizontal-char-p (char-after))
4354 (while (progn
4355 (delete-char 1)
4356 (insert-before-markers ?\s)
4357 (table--cell-horizontal-char-p (char-after)))))
4358 ((eq (char-after) table-cell-vertical-char)
4359 (while (let ((col (current-column)))
4360 (delete-char 1)
4361 (insert-before-markers ?\s)
4362 (and (zerop (forward-line 1))
4363 (zerop (current-column))
4364 (move-to-column col)
4365 (eq (char-after) table-cell-vertical-char))))))
4366 (memq (char-after) frame-char)))))
4368 (defun table--remove-blank-lines (n)
4369 "Delete N blank lines from the current line.
4370 For adjusting below area of the table when the table is shortened."
4371 (move-to-column 0)
4372 (let ((first-blank t))
4373 (while (> n 0)
4374 (setq n (1- n))
4375 (cond ((looking-at "\\s *\\'")
4376 (delete-region (match-beginning 0) (match-end 0))
4377 (setq n 0))
4378 ((and (looking-at "\\([ \t]*\n[ \t]*\\)\n") first-blank)
4379 (delete-region (match-beginning 1) (match-end 1)))
4380 ((looking-at "[ \t]*$")
4381 (delete-region (match-beginning 0) (match-end 0))
4382 (forward-line 1))
4384 (setq first-blank nil)
4385 (forward-line 1))))))
4387 (defun table--uniform-list-p (l)
4388 "Return nil when LIST contains non equal elements. Otherwise return t."
4389 (if (null l) t
4390 (catch 'end
4391 (while (cdr l)
4392 (if (not (equal (car l) (cadr l))) (throw 'end nil))
4393 (setq l (cdr l)))
4394 t)))
4396 (defun table--detect-cell-alignment (cell)
4397 "Detect CELL contents alignment.
4398 Guess CELL contents alignment both horizontally and vertically by
4399 looking at the appearance of the CELL contents."
4400 (let ((cell-contents (extract-rectangle (car cell) (cdr cell)))
4401 (left-margin 0)
4402 (right-margin 0)
4403 (top-margin 0)
4404 (bottom-margin 0)
4405 (margin-diff 0)
4406 (margin-info-available nil)
4407 justify valign)
4408 (with-temp-buffer
4409 (table--insert-rectangle cell-contents)
4410 ;; determine the horizontal justification
4411 (goto-char (point-min))
4412 (while (re-search-forward "^\\( *\\).*[^ \n]\\( *\\)$" nil t)
4413 (setq margin-info-available t)
4414 (let* ((lm (- (match-end 1) (match-beginning 1)))
4415 (rm (- (match-end 2) (match-beginning 2)))
4416 (md (abs (- lm rm))))
4417 (if (> lm left-margin)
4418 (setq left-margin lm))
4419 (if (> rm right-margin)
4420 (setq right-margin rm))
4421 (if (> md margin-diff)
4422 (setq margin-diff md))))
4423 (setq justify
4424 (cond
4425 ((and margin-info-available
4426 (<= margin-diff 1)
4427 (> left-margin 0)) 'center)
4428 ((and margin-info-available
4429 (zerop right-margin)
4430 (> left-margin 0)) 'right)
4431 (t 'left)))
4432 ;; determine the vertical justification
4433 (goto-char (point-min))
4434 (if (and (re-search-forward "\\s *\\S " nil t)
4435 (/= (match-beginning 0) (match-end 0)))
4436 (setq top-margin (1- (count-lines (match-beginning 0) (match-end 0)))))
4437 (if (and (re-search-forward "\\s *\\'" nil t)
4438 (/= (match-beginning 0) (match-end 0)))
4439 (setq bottom-margin (1- (count-lines (match-beginning 0) (match-end 0)))))
4440 (setq valign
4441 (cond
4442 ((and (> top-margin 0)
4443 (> bottom-margin 0)
4444 (<= (abs (- top-margin bottom-margin)) 1)) 'middle)
4445 ((and (> top-margin 0)
4446 (zerop bottom-margin)) 'bottom)
4447 (t nil))))
4448 (table--put-cell-justify-property cell justify)
4449 (table--put-cell-valign-property cell valign)))
4451 (defun table--string-to-number-list (str)
4452 "Return a list of numbers in STR."
4453 (let ((idx 0)
4454 (nl nil))
4455 (while (string-match "[-0-9.]+" str idx)
4456 (setq idx (match-end 0))
4457 (setq nl (cons (string-to-number (match-string 0 str)) nl)))
4458 (nreverse nl)))
4460 (defun table--justify-cell-contents (justify &optional paragraph)
4461 "Justify the current cell contents.
4462 JUSTIFY is a symbol 'left, 'center or 'right for horizontal, or 'top,
4463 'middle, 'bottom or 'none for vertical. When PARAGRAPH is non-nil the
4464 justify operation is limited to the current paragraph."
4465 (table-with-cache-buffer
4466 (let ((beg (point-min))
4467 (end (point-max-marker))
4468 (fill-column table-cell-info-width)
4469 (adaptive-fill-mode nil)
4470 (valign-symbols '(top middle bottom none)))
4471 (unless paragraph
4472 (if (memq justify valign-symbols)
4473 (setq table-cell-info-valign
4474 (if (eq justify 'none) nil justify))
4475 (setq table-cell-info-justify justify)))
4476 (save-excursion
4477 (if paragraph
4478 (let ((paragraph-start "\n"))
4479 (forward-paragraph)
4480 (or (bolp) (newline 1))
4481 (set-marker end (point))
4482 (setq beg (progn (forward-paragraph -1) (point)))))
4483 (if (memq justify valign-symbols)
4484 (table--valign)
4485 (table--remove-eol-spaces beg end 'bol)
4486 (let ((paragraph-start table-paragraph-start))
4487 (fill-region beg end table-cell-info-justify))))
4488 (setq table-inhibit-auto-fill-paragraph t)
4489 (set-marker end nil)))
4490 (table--update-cell 'now))
4492 (defun table--horizontally-shift-above-and-below (columns-to-extend top-to-bottom-coord-list)
4493 "Horizontally shift outside contents right above and right below of the table.
4494 This function moves the surrounding text outside of the table so that
4495 they match the horizontal growth/shrink of the table. It also
4496 untabify the shift affected area including the right side of the table
4497 so that tab related uneven shifting is avoided. COLUMNS-TO-EXTEND
4498 specifies the number of columns the table grows, or shrinks if
4499 negative. TOP-TO-BOTTOM-COORD-LIST is the vertical cell coordinate
4500 list. This list can be any vertical list within the table."
4501 (save-excursion
4502 (let (beg-coord end-coord)
4503 (table--goto-coordinate (caar top-to-bottom-coord-list))
4504 (let* ((cell (table--horizontal-cell-list nil 'first-only 'top))
4505 (coord (cons (car (table--get-coordinate (cdr cell)))
4506 (cdr (table--get-coordinate (car cell))))))
4507 (setcar coord (1+ (car coord)))
4508 (setcdr coord (- (cdr coord) 2))
4509 (setq beg-coord (cons (car coord) (1+ (cdr coord))))
4510 (while (and (table--goto-coordinate coord 'no-extension)
4511 (not (looking-at "\\s *$")))
4512 (if (< columns-to-extend 0)
4513 (progn
4514 (table--untabify-line)
4515 (delete-char columns-to-extend))
4516 (table--untabify-line (point))
4517 (insert (make-string columns-to-extend ?\s)))
4518 (setcdr coord (1- (cdr coord)))))
4519 (table--goto-coordinate (caar (last top-to-bottom-coord-list)))
4520 (let ((coord (table--get-coordinate (cdr (table--horizontal-cell-list nil 'first-only 'bottom)))))
4521 (setcar coord (1+ (car coord)))
4522 (setcdr coord (+ (cdr coord) 2))
4523 (setq end-coord (cons (car coord) (1- (cdr coord))))
4524 (while (and (table--goto-coordinate coord 'no-extension)
4525 (not (looking-at "\\s *$")))
4526 (if (< columns-to-extend 0)
4527 (progn
4528 (table--untabify-line)
4529 (delete-char columns-to-extend))
4530 (table--untabify-line (point))
4531 (insert (make-string columns-to-extend ?\s)))
4532 (setcdr coord (1+ (cdr coord)))))
4533 (while (<= (cdr beg-coord) (cdr end-coord))
4534 (table--untabify-line (table--goto-coordinate beg-coord 'no-extension))
4535 (setcdr beg-coord (1+ (cdr beg-coord)))))))
4537 (defun table--create-growing-space-below (lines-to-extend left-to-right-coord-list bottom-border-y)
4538 "Create growing space below the table.
4539 This function creates growing space below the table slightly
4540 intelligent fashion. Following is the cases it handles for each
4541 growing line:
4542 1. When the first line below the table is a complete blank line it
4543 inserts a blank line.
4544 2. When the line starts with a prefix that matches the prefix of the
4545 bottom line of the table it inserts a line consisting of prefix alone.
4546 3. Otherwise it deletes the rectangular contents where table will
4547 grow into."
4548 (save-excursion
4549 (let ((i 0)
4550 (prefix (and (table--goto-coordinate (cons 0 bottom-border-y))
4551 (re-search-forward
4552 ".*\\S "
4553 (save-excursion
4554 (table--goto-coordinate
4555 (cons (1- (caar (car left-to-right-coord-list))) bottom-border-y)))
4557 (buffer-substring (match-beginning 0) (match-end 0)))))
4558 (while (< i lines-to-extend)
4559 (let ((y (+ i bottom-border-y 1)))
4560 (table--goto-coordinate (cons 0 y))
4561 (cond
4562 ((looking-at "\\s *$")
4563 (insert ?\n))
4564 ((and prefix (looking-at (concat (regexp-quote prefix) "\\s *$")))
4565 (insert prefix ?\n))
4567 (delete-rectangle
4568 (table--goto-coordinate (cons (1- (caar (car left-to-right-coord-list))) y))
4569 (table--goto-coordinate (cons (1+ (cadr (car (last left-to-right-coord-list)))) y))))))
4570 (setq i (1+ i))))))
4572 (defun table--untabify-line (&optional from)
4573 "Untabify current line.
4574 Unlike save-excursion this guarantees preserving the cursor location
4575 even when the point is on a tab character which is to be removed.
4576 Optional FROM narrows the subject operation from this point to the end
4577 of line."
4578 (let ((current-coordinate (table--get-coordinate)))
4579 (table--untabify (or from (progn (beginning-of-line) (point)))
4580 (progn (end-of-line) (point)))
4581 (table--goto-coordinate current-coordinate)))
4583 (defun table--untabify (beg end)
4584 "Wrapper to raw untabify."
4585 (untabify beg end)
4586 (if (featurep 'xemacs)
4587 ;; Cancel strange behavior of xemacs
4588 (message "")))
4590 (defun table--multiply-string (string multiplier)
4591 "Multiply string and return it."
4592 (let ((ret-str ""))
4593 (while (> multiplier 0)
4594 (setq ret-str (concat ret-str string))
4595 (setq multiplier (1- multiplier)))
4596 ret-str))
4598 (defun table--line-column-position (line column)
4599 "Return the location of LINE forward at COLUMN."
4600 (save-excursion
4601 (forward-line line)
4602 (move-to-column column)
4603 (point)))
4605 (defun table--row-column-insertion-point-p (&optional columnp)
4606 "Return non-nil if it makes sense to insert a row or a column at point."
4607 (and (not buffer-read-only)
4608 (or (get-text-property (point) 'table-cell)
4609 (let ((column (current-column)))
4610 (if columnp
4611 (or (text-property-any (line-beginning-position 0)
4612 (table--line-column-position -1 column)
4613 'table-cell t)
4614 (text-property-any (line-beginning-position) (point) 'table-cell t)
4615 (text-property-any (line-beginning-position 2)
4616 (table--line-column-position 1 column)
4617 'table-cell t))
4618 (text-property-any (table--line-column-position -2 column)
4619 (table--line-column-position -2 (+ 2 column))
4620 'table-cell t))))))
4622 (defun table--find-row-column (&optional columnp no-error)
4623 "Search table and return a cell coordinate list of row or column."
4624 (let ((current-coordinate (table--get-coordinate)))
4625 (catch 'end
4626 (catch 'error
4627 (let ((coord (table--get-coordinate)))
4628 (while
4629 (progn
4630 (if columnp (setcar coord (1- (car coord)))
4631 (setcdr coord (1- (cdr coord))))
4632 (>= (if columnp (car coord) (cdr coord)) 0))
4633 (while (progn
4634 (table--goto-coordinate coord 'no-extension 'no-tab-expansion)
4635 (not (looking-at (format "[%s%c%c]"
4636 table-cell-horizontal-chars
4637 table-cell-vertical-char
4638 table-cell-intersection-char))))
4639 (if columnp (setcar coord (1- (car coord)))
4640 (setcdr coord (1- (cdr coord))))
4641 (if (< (if columnp (car coord) (cdr coord)) 0)
4642 (throw 'error nil)))
4643 (if (table--probe-cell)
4644 (throw 'end (table--cell-list-to-coord-list (if columnp
4645 (table--vertical-cell-list t nil 'left)
4646 (table--horizontal-cell-list t nil 'top))))
4647 (table--goto-coordinate (table--offset-coordinate coord (if columnp '(0 . 1) '(1 . 0)))
4648 'no-extension 'no-tab-expansion)
4649 (if (table--probe-cell)
4650 (throw 'end (table--cell-list-to-coord-list (if columnp
4651 (table--vertical-cell-list t nil 'left)
4652 (table--horizontal-cell-list t nil 'top)))))))))
4653 (table--goto-coordinate current-coordinate)
4654 (if no-error nil
4655 (error "Table not found")))))
4657 (defun table--min-coord-list (coord-list)
4658 "Return minimum cell dimension of COORD-LIST.
4659 COORD-LIST is a list of coordinate pairs (lu-coord . rb-coord), where
4660 each pair in the list represents a cell. lu-coord is the left upper
4661 coordinate of a cell and rb-coord is the right bottom coordinate of a
4662 cell. A coordinate is a pair of x and y axis coordinate values. The
4663 return value is a cons cell (min-w . min-h), where min-w and min-h are
4664 respectively the minimum width and the minimum height of all the cells
4665 in the list."
4666 (if (null coord-list) nil
4667 (let ((min-width 134217727)
4668 (min-height 134217727))
4669 (while coord-list
4670 (let* ((coord (prog1 (car coord-list) (setq coord-list (cdr coord-list))))
4671 (width (- (cadr coord) (caar coord)))
4672 (height (1+ (- (cddr coord) (cdar coord)))))
4673 (if (< width min-width) (setq min-width width))
4674 (if (< height min-height) (setq min-height height))))
4675 (cons min-width min-height))))
4677 (defun table--cell-can-split-horizontally-p ()
4678 "Test if a cell can split at current location horizontally."
4679 (and (not buffer-read-only)
4680 (let ((point-x (car (table--get-coordinate))))
4681 (table-recognize-cell 'force)
4682 (and (> point-x (car table-cell-info-lu-coordinate))
4683 (<= point-x (1- (car table-cell-info-rb-coordinate)))))))
4685 (defun table--cell-can-split-vertically-p ()
4686 "Test if a cell can split at current location vertically."
4687 (and (not buffer-read-only)
4688 (let ((point-y (cdr (table--get-coordinate))))
4689 (table-recognize-cell 'force)
4690 (and (> point-y (cdr table-cell-info-lu-coordinate))
4691 (<= point-y (cdr table-cell-info-rb-coordinate))))))
4693 (defun table--cell-can-span-p (direction)
4694 "Test if the current cell can span to DIRECTION."
4695 (table-recognize-cell 'force)
4696 (and (not buffer-read-only)
4697 (table--probe-cell)
4698 ;; get two adjacent cells from each corner
4699 (let ((cell (save-excursion
4700 (and
4701 (table--goto-coordinate
4702 (cons (cond ((eq direction 'right) (1+ (car table-cell-info-rb-coordinate)))
4703 ((eq direction 'left) (1- (car table-cell-info-lu-coordinate)))
4704 (t (car table-cell-info-lu-coordinate)))
4705 (cond ((eq direction 'above) (- (cdr table-cell-info-lu-coordinate) 2))
4706 ((eq direction 'below) (+ (cdr table-cell-info-rb-coordinate) 2))
4707 (t (cdr table-cell-info-lu-coordinate)))) 'no-extension)
4708 (table--probe-cell))))
4709 (cell2 (save-excursion
4710 (and
4711 (table--goto-coordinate
4712 (cons (cond ((eq direction 'right) (1+ (car table-cell-info-rb-coordinate)))
4713 ((eq direction 'left) (1- (car table-cell-info-lu-coordinate)))
4714 (t (car table-cell-info-rb-coordinate)))
4715 (cond ((eq direction 'above) (- (cdr table-cell-info-lu-coordinate) 2))
4716 ((eq direction 'below) (+ (cdr table-cell-info-rb-coordinate) 2))
4717 (t (cdr table-cell-info-rb-coordinate)))) 'no-extension)
4718 (table--probe-cell)))))
4719 ;; make sure the two cells exist, and they are identical, that cell's size matches the current one
4720 (and cell
4721 (equal cell cell2)
4722 (if (or (eq direction 'right) (eq direction 'left))
4723 (and (= (cdr (table--get-coordinate (car cell)))
4724 (cdr table-cell-info-lu-coordinate))
4725 (= (cdr (table--get-coordinate (cdr cell)))
4726 (cdr table-cell-info-rb-coordinate)))
4727 (and (= (car (table--get-coordinate (car cell)))
4728 (car table-cell-info-lu-coordinate))
4729 (= (car (table--get-coordinate (cdr cell)))
4730 (car table-cell-info-rb-coordinate))))))))
4732 (defun table--cell-insert-char (char &optional overwrite)
4733 "Insert CHAR inside a table cell."
4734 (let ((delete-selection-p (and (boundp 'delete-selection-mode)
4735 delete-selection-mode
4736 transient-mark-mode mark-active
4737 (not buffer-read-only)))
4738 (mark-coordinate (table--transcoord-table-to-cache (table--get-coordinate (mark t)))))
4739 (table-with-cache-buffer
4740 (and delete-selection-p
4741 (>= (car mark-coordinate) 0)
4742 (<= (car mark-coordinate) table-cell-info-width)
4743 (>= (cdr mark-coordinate) 0)
4744 (<= (cdr mark-coordinate) table-cell-info-height)
4745 (save-excursion
4746 (delete-region (point) (table--goto-coordinate mark-coordinate))))
4747 (if overwrite
4748 (let ((coordinate (table--get-coordinate)))
4749 (setq table-inhibit-auto-fill-paragraph t)
4750 (if (>= (car coordinate) table-cell-info-width)
4751 (if (>= (cdr coordinate) (1- table-cell-info-height))
4752 (insert "\n" char)
4753 (forward-line 1)
4754 (insert char)
4755 (unless (eolp)
4756 (delete-char 1)))
4757 (insert char)
4758 (unless (eolp)
4759 (delete-char 1))))
4760 (if (not (eq char ?\s))
4761 (if char (insert char))
4762 (if (not (looking-at "\\s *$"))
4763 (if (and table-fixed-width-mode
4764 (> (point) 2)
4765 (save-excursion
4766 (forward-char -2)
4767 (looking-at (concat "\\("
4768 (regexp-quote (char-to-string table-word-continuation-char))
4769 "\\)\n"))))
4770 (save-excursion
4771 (replace-match " " nil nil nil 1))
4772 (insert char))
4773 (let ((coordinate (table--get-coordinate)))
4774 (if (< (car coordinate) table-cell-info-width)
4775 (move-to-column (1+ (car coordinate)) t)
4776 (insert (make-string (forward-line 1) ?\n))
4777 (unless (bolp) (insert ?\n))))
4778 (setq table-inhibit-auto-fill-paragraph t))
4779 (save-excursion
4780 (let ((o-point (point)))
4781 (if (and (bolp)
4782 (or (progn
4783 (forward-paragraph)
4784 (forward-paragraph -1)
4785 (= o-point (point)))
4786 (progn
4787 (goto-char o-point)
4788 (forward-line)
4789 (setq o-point (point))
4790 (forward-paragraph)
4791 (forward-paragraph -1)
4792 (= o-point (point)))))
4793 (insert ?\n)))))))))
4795 (defun table--finish-delayed-tasks ()
4796 "Finish all outstanding delayed tasks."
4797 (if table-update-timer
4798 (table--update-cell 'now))
4799 (if table-widen-timer
4800 (table--update-cell-widened 'now))
4801 (if table-heighten-timer
4802 (table--update-cell-heightened 'now)))
4804 (defmacro table--log (&rest body)
4805 "Debug logging macro."
4806 `(with-current-buffer (get-buffer-create "log")
4807 (goto-char (point-min))
4808 (let ((standard-output (current-buffer)))
4809 ,@body)))
4811 (defun table--measure-max-width (&optional unlimited)
4812 "Return maximum width of current buffer.
4813 Normally the current buffer is expected to be already the cache
4814 buffer. The width excludes following spaces at the end of each line.
4815 Unless UNLIMITED is non-nil minimum return value is 1."
4816 (save-excursion
4817 (let ((width 0))
4818 (goto-char (point-min))
4819 (while
4820 (progn
4821 ;; do not count the following white spaces
4822 (re-search-forward "\\s *$")
4823 (goto-char (match-beginning 0))
4824 (if (> (current-column) width)
4825 (setq width (current-column)))
4826 (forward-line)
4827 (not (eobp))))
4828 (if unlimited width
4829 (max 1 width)))))
4831 (defun table--cell-to-coord (cell)
4832 "Create a cell coordinate pair from cell location pair."
4833 (if cell
4834 (cons (table--get-coordinate (car cell))
4835 (table--get-coordinate (cdr cell)))
4836 nil))
4838 (defun table--cell-list-to-coord-list (cell-list)
4839 "Create and return a coordinate list that corresponds to CELL-LIST.
4840 CELL-LIST is a list of location pairs (lu . rb), where each pair
4841 represents a cell in the list. lu is the left upper location and rb
4842 is the right bottom location of a cell. The return value is a list of
4843 coordinate pairs (lu-coord . rb-coord), where lu-coord is the left
4844 upper coordinate and rb-coord is the right bottom coordinate of a
4845 cell."
4846 (let ((coord-list))
4847 (while cell-list
4848 (let ((cell (prog1 (car cell-list) (setq cell-list (cdr cell-list)))))
4849 (setq coord-list
4850 (cons (table--cell-to-coord cell) coord-list))))
4851 (nreverse coord-list)))
4853 (defun table--test-cell-list (&optional horizontal reverse first-only pivot)
4854 "For testing `table--vertical-cell-list' and `table--horizontal-cell-list'."
4855 (let* ((current-coordinate (table--get-coordinate))
4856 (cell-list (if horizontal
4857 (table--horizontal-cell-list reverse first-only pivot)
4858 (table--vertical-cell-list reverse first-only pivot)))
4859 (count 0))
4860 (while cell-list
4861 (let* ((cell (if first-only (prog1 cell-list (setq cell-list nil))
4862 (prog1 (car cell-list) (setq cell-list (cdr cell-list)))))
4863 (dig1-str (format "%1d" (prog1 (% count 10) (setq count (1+ count))))))
4864 (goto-char (car cell))
4865 (table-with-cache-buffer
4866 (while (re-search-forward "." nil t)
4867 (replace-match dig1-str nil nil))
4868 (setq table-inhibit-auto-fill-paragraph t))
4869 (table--finish-delayed-tasks)))
4870 (table--goto-coordinate current-coordinate)))
4872 (defun table--vertical-cell-list (&optional top-to-bottom first-only pivot internal-dir internal-list internal-px)
4873 "Return a vertical cell list from the table.
4874 The return value represents a list of cells including the current cell
4875 that align vertically. Each element of the list is a cons cell (lu
4876 . rb) where lu is the cell's left upper location and rb is the cell's
4877 right bottom location. The cell order in the list is from bottom to
4878 top of the table. If optional argument TOP-TO-BOTTOM is non-nil the
4879 order is reversed as from top to bottom of the table. If optional
4880 argument FIRST-ONLY is non-nil the return value is not a list of cells
4881 but a single cons cell that is the first cell of the list, if the list
4882 had been created. If optional argument PIVOT is a symbol `left' the
4883 vertical cell search is aligned with the left edge of the current
4884 cell, otherwise aligned with the right edge of the current cell. The
4885 arguments INTERNAL-DIR, INTERNAL-LIST and INTERNAL-PX are internal use
4886 only and must not be specified."
4887 (save-excursion
4888 (let* ((cell (table--probe-cell))
4889 (lu-coordinate (table--get-coordinate (car cell)))
4890 (rb-coordinate (table--get-coordinate (cdr cell)))
4891 (px (or internal-px (car (if (eq pivot 'left) lu-coordinate rb-coordinate))))
4892 (ty (- (cdr lu-coordinate) 2))
4893 (by (+ (cdr rb-coordinate) 2)))
4894 ;; in case of finding the first cell, get the last adding item on the list
4895 (if (and (null internal-dir) first-only) (setq top-to-bottom (null top-to-bottom)))
4896 ;; travel up and process as recursion traces back (reverse order)
4897 (and cell
4898 (or (eq internal-dir 'up) (null internal-dir))
4899 (table--goto-coordinate (cons px (if top-to-bottom by ty)) 'no-extension 'no-tab-expansion)
4900 (setq internal-list (table--vertical-cell-list top-to-bottom first-only nil 'up nil px)))
4901 ;; return the last cell or add this cell to the list
4902 (if first-only (or internal-list cell)
4903 (setq internal-list (if cell (cons cell internal-list) internal-list))
4904 ;; travel down and process as entering each recursion (forward order)
4905 (and cell
4906 (or (eq internal-dir 'down) (null internal-dir))
4907 (table--goto-coordinate (cons px (if top-to-bottom ty by)) 'no-extension 'no-tab-expansion)
4908 (setq internal-list (table--vertical-cell-list top-to-bottom nil nil 'down internal-list px)))
4909 ;; return the result
4910 internal-list))))
4912 (defun table--horizontal-cell-list (&optional left-to-right first-only pivot internal-dir internal-list internal-py)
4913 "Return a horizontal cell list from the table.
4914 The return value represents a list of cells including the current cell
4915 that align horizontally. Each element of the list is a cons cells (lu
4916 . rb) where lu is the cell's left upper location and rb is the cell's
4917 right bottom location. The cell order in the list is from right to
4918 left of the table. If optional argument LEFT-TO-RIGHT is non-nil the
4919 order is reversed as from left to right of the table. If optional
4920 argument FIRST-ONLY is non-nil the return value is not a list of cells
4921 but a single cons cell that is the first cell of the list, if the
4922 list had been created. If optional argument PIVOT is a symbol `top'
4923 the horizontal cell search is aligned with the top edge of the current
4924 cell, otherwise aligned with the bottom edge of the current cell. The
4925 arguments INTERNAL-DIR, INTERNAL-LIST and INTERNAL-PY are internal use
4926 only and must not be specified."
4927 (save-excursion
4928 (let* ((cell (table--probe-cell))
4929 (lu-coordinate (table--get-coordinate (car cell)))
4930 (rb-coordinate (table--get-coordinate (cdr cell)))
4931 (py (or internal-py (if (eq pivot 'top) (cdr lu-coordinate) (1+ (cdr rb-coordinate)))))
4932 (lx (1- (car lu-coordinate)))
4933 (rx (1+ (car rb-coordinate))))
4934 ;; in case of finding the first cell, get the last adding item on the list
4935 (if (and (null internal-dir) first-only) (setq left-to-right (null left-to-right)))
4936 ;; travel left and process as recursion traces back (reverse order)
4937 (and cell
4938 (or (eq internal-dir 'left) (null internal-dir))
4939 (table--goto-coordinate (cons (if left-to-right rx lx) py) 'no-extension 'no-tab-expansion)
4940 (setq internal-list (table--horizontal-cell-list left-to-right first-only nil 'left nil py)))
4941 ;; return the last cell or add this cell to the list
4942 (if first-only (or internal-list cell)
4943 (setq internal-list (if cell (cons cell internal-list) internal-list))
4944 ;; travel right and process as entering each recursion (forward order)
4945 (and cell
4946 (or (eq internal-dir 'right) (null internal-dir))
4947 (table--goto-coordinate (cons (if left-to-right lx rx) py) 'no-extension 'no-tab-expansion)
4948 (setq internal-list (table--horizontal-cell-list left-to-right nil nil 'right internal-list py)))
4949 ;; return the result
4950 internal-list))))
4952 (defun table--point-in-cell-p (&optional location)
4953 "Return t when point is in a valid table cell in the current buffer.
4954 When optional LOCATION is provided the test is performed at that location."
4955 (and (table--at-cell-p (or location (point)))
4956 (if location
4957 (save-excursion
4958 (goto-char location)
4959 (table--probe-cell))
4960 (table--probe-cell))))
4962 (defun table--region-in-cell-p (beg end)
4963 "Return t when location BEG and END are in a valid table cell in the current buffer."
4964 (and (table--at-cell-p (min beg end))
4965 (save-excursion
4966 (let ((cell-beg (progn (goto-char beg) (table--probe-cell))))
4967 (and cell-beg
4968 (equal cell-beg (progn (goto-char end) (table--probe-cell))))))))
4970 (defun table--at-cell-p (position &optional object at-column)
4971 "Returns non-nil if POSITION has table-cell property in OBJECT.
4972 OBJECT is optional and defaults to the current buffer.
4973 If POSITION is at the end of OBJECT, the value is nil."
4974 (if (and at-column (stringp object))
4975 (setq position (table--str-index-at-column object position)))
4976 (get-text-property position 'table-cell object))
4978 (defun table--probe-cell-left-up ()
4979 "Probe left up corner pattern of a cell.
4980 If it finds a valid corner returns a position otherwise returns nil.
4981 The position is the location before the first cell character.
4982 Focus only on the corner pattern. Further cell validity check is required."
4983 (save-excursion
4984 (let ((vertical-str (regexp-quote (char-to-string table-cell-vertical-char)))
4985 (intersection-str (regexp-quote (char-to-string table-cell-intersection-char)))
4986 (v-border (format "[%c%c]" table-cell-vertical-char table-cell-intersection-char))
4987 (h-border (format "[%s%c]" table-cell-horizontal-chars table-cell-intersection-char))
4988 (limit (line-beginning-position)))
4989 (catch 'end
4990 (while t
4991 (catch 'retry-horizontal
4992 (if (not (search-backward-regexp v-border limit t))
4993 (throw 'end nil))
4994 (save-excursion
4995 (let ((column (current-column)))
4996 (while t
4997 (catch 'retry-vertical
4998 (if (zerop (forward-line -1)) nil (throw 'end nil))
4999 (move-to-column column)
5000 (while (and (looking-at vertical-str)
5001 (= column (current-column)))
5002 (if (zerop (forward-line -1)) nil (throw 'end nil))
5003 (move-to-column column))
5004 (cond
5005 ((/= column (current-column))
5006 (throw 'end nil))
5007 ((looking-at (concat intersection-str h-border))
5008 (forward-line 1)
5009 (move-to-column column)
5010 (forward-char 1)
5011 (throw 'end (point)))
5012 ((looking-at intersection-str)
5013 (throw 'retry-vertical nil))
5014 (t (throw 'retry-horizontal nil)))))))))))))
5016 (defun table--probe-cell-right-bottom ()
5017 "Probe right bottom corner pattern of a cell.
5018 If it finds a valid corner returns a position otherwise returns nil.
5019 The position is the location after the last cell character.
5020 Focus only on the corner pattern. Further cell validity check is required."
5021 (save-excursion
5022 (let ((vertical-str (regexp-quote (char-to-string table-cell-vertical-char)))
5023 (intersection-str (regexp-quote (char-to-string table-cell-intersection-char)))
5024 (v-border (format "[%c%c]" table-cell-vertical-char table-cell-intersection-char))
5025 (h-border (format "[%s%c]" table-cell-horizontal-chars table-cell-intersection-char))
5026 (limit (line-end-position)))
5027 (catch 'end
5028 (while t
5029 (catch 'retry-horizontal
5030 (if (not (search-forward-regexp v-border limit t))
5031 (throw 'end nil))
5032 (save-excursion
5033 (forward-char -1)
5034 (let ((column (current-column)))
5035 (while t
5036 (catch 'retry-vertical
5037 (while (and (looking-at vertical-str)
5038 (= column (current-column)))
5039 (if (and (zerop (forward-line 1)) (zerop (current-column))) nil (throw 'end nil))
5040 (move-to-column column))
5041 (cond
5042 ((/= column (current-column))
5043 (throw 'end nil))
5044 ((save-excursion (forward-char -1) (looking-at (concat h-border intersection-str)))
5045 (save-excursion
5046 (and (zerop (forward-line -1))
5047 (move-to-column column)
5048 (looking-at v-border)
5049 (throw 'end (point))))
5050 (forward-char 1)
5051 (throw 'retry-horizontal nil))
5052 ((looking-at intersection-str)
5053 (if (and (zerop (forward-line 1)) (zerop (current-column))) nil (throw 'end nil))
5054 (move-to-column column)
5055 (throw 'retry-vertical nil))
5056 (t (throw 'retry-horizontal nil)))))))))))))
5058 (defun table--editable-cell-p (&optional _abort-on-error)
5059 (and (not buffer-read-only)
5060 (get-text-property (point) 'table-cell)))
5062 (defun table--probe-cell (&optional abort-on-error)
5063 "Probes a table cell around the point.
5064 Searches for the left upper corner and the right bottom corner of a table
5065 cell which contains the current point location.
5067 The result is a cons cell (left-upper . right-bottom) where
5068 the left-upper is the position before the cell's left upper corner character,
5069 the right-bottom is the position after the cell's right bottom corner character.
5071 When it fails to find either one of the cell corners it returns nil or
5072 signals error if the optional ABORT-ON-ERROR is non-nil."
5073 (let (lu rb
5074 (border (format "^[%s%c%c]+$"
5075 table-cell-horizontal-chars
5076 table-cell-vertical-char
5077 table-cell-intersection-char)))
5078 (if (and (condition-case nil
5079 (progn
5080 (and (setq lu (table--probe-cell-left-up))
5081 (setq rb (table--probe-cell-right-bottom))))
5082 (error nil))
5083 (< lu rb)
5084 (let ((lu-coordinate (table--get-coordinate lu))
5085 (rb-coordinate (table--get-coordinate rb)))
5086 ;; test for valid upper and lower borders
5087 (and (string-match
5088 border
5089 (buffer-substring
5090 (save-excursion
5091 (table--goto-coordinate
5092 (cons (1- (car lu-coordinate))
5093 (1- (cdr lu-coordinate)))))
5094 (save-excursion
5095 (table--goto-coordinate
5096 (cons (1+ (car rb-coordinate))
5097 (1- (cdr lu-coordinate)))))))
5098 (string-match
5099 border
5100 (buffer-substring
5101 (save-excursion
5102 (table--goto-coordinate
5103 (cons (1- (car lu-coordinate))
5104 (1+ (cdr rb-coordinate)))))
5105 (save-excursion
5106 (table--goto-coordinate
5107 (cons (1+ (car rb-coordinate))
5108 (1+ (cdr rb-coordinate))))))))))
5109 (cons lu rb)
5110 (if abort-on-error
5111 (error "Table cell not found")
5112 nil))))
5114 (defun table--insert-rectangle (rectangle)
5115 "Insert text of RECTANGLE with upper left corner at point.
5116 Same as insert-rectangle except that mark operation is eliminated."
5117 (let ((lines rectangle)
5118 (insertcolumn (current-column))
5119 (first t))
5120 (while lines
5121 (or first
5122 (progn
5123 (forward-line 1)
5124 (or (bolp) (insert ?\n))
5125 (move-to-column insertcolumn t)))
5126 (setq first nil)
5127 (insert (car lines))
5128 (setq lines (cdr lines)))))
5130 (defun table--put-cell-property (cell)
5131 "Put standard text properties to the CELL.
5132 The CELL is a cons cell (left-upper . right-bottom) where the
5133 left-upper is the position before the cell's left upper corner
5134 character, the right-bottom is the position after the cell's right
5135 bottom corner character."
5136 (let ((lu (table--get-coordinate (car cell)))
5137 (rb (table--get-coordinate (cdr cell))))
5138 (save-excursion
5139 (while (<= (cdr lu) (cdr rb))
5140 (let ((beg (table--goto-coordinate lu 'no-extension))
5141 (end (table--goto-coordinate (cons (car rb) (cdr lu)))))
5142 (table--put-cell-line-property beg end))
5143 (setcdr lu (1+ (cdr lu))))
5144 (table--put-cell-justify-property cell table-cell-info-justify)
5145 (table--put-cell-valign-property cell table-cell-info-valign))))
5147 (defun table--put-cell-line-property (beg end &optional object)
5148 "Put standard text properties to a line of a cell.
5149 BEG is the beginning of the line that is the location between left
5150 cell border character and the first content character. END is the end
5151 of the line that is the location between the last content character
5152 and the right cell border character."
5153 (table--put-cell-content-property beg end object)
5154 (table--put-cell-keymap-property end (1+ end) object)
5155 (table--put-cell-indicator-property end (1+ end) object)
5156 (table--put-cell-rear-nonsticky end (1+ end) object))
5158 (defun table--put-cell-content-property (beg end &optional object)
5159 "Put cell content text properties."
5160 (table--put-cell-keymap-property beg end object)
5161 (table--put-cell-indicator-property beg end object)
5162 (table--put-cell-face-property beg end object)
5163 (table--put-cell-point-entered/left-property beg end object))
5165 (defun table--put-cell-indicator-property (beg end &optional object)
5166 "Put cell property which indicates that the location is within a table cell."
5167 (put-text-property beg end 'table-cell t object)
5168 (put-text-property beg end 'yank-handler table-yank-handler object))
5170 (defun table--put-cell-face-property (beg end &optional object)
5171 "Put cell face property."
5172 (put-text-property beg end 'face 'table-cell object))
5174 (defun table--put-cell-keymap-property (beg end &optional object)
5175 "Put cell keymap property."
5176 (put-text-property beg end 'keymap 'table-cell-map object))
5178 (defun table--put-cell-rear-nonsticky (beg end &optional object)
5179 "Put rear-nonsticky property."
5180 (put-text-property beg end 'rear-nonsticky t object))
5182 (defun table--put-cell-point-entered/left-property (beg end &optional object)
5183 "Put point-entered/left property."
5184 (put-text-property beg end 'cursor-sensor-functions
5185 '(table--point-entered/left-cell-function) object))
5187 (defun table--remove-cell-properties (beg end &optional object)
5188 "Remove all cell properties.
5189 If OBJECT is non-nil cell properties are removed from the OBJECT
5190 instead of the current buffer and returns the OBJECT."
5191 (while (< beg end)
5192 (let ((next (next-single-property-change beg 'table-cell object end)))
5193 (if (get-text-property beg 'table-cell object)
5194 (remove-text-properties beg next
5195 (list
5196 'table-cell nil
5197 'table-justify nil
5198 'table-valign nil
5199 'face nil
5200 'rear-nonsticky nil
5201 'cursor-sensor-functions nil
5202 'keymap nil)
5203 object))
5204 (setq beg next)))
5205 object)
5207 (defun table--update-cell-face ()
5208 "Update cell face according to the current mode."
5209 (if (featurep 'xemacs)
5210 (set-face-property 'table-cell 'underline table-fixed-width-mode)
5211 (set-face-inverse-video 'table-cell table-fixed-width-mode)))
5213 (table--update-cell-face)
5215 (defun table--get-property (cell property)
5216 "Get CELL's PROPERTY."
5217 (or (get-text-property (car cell) property)
5218 (get-text-property (1- (cdr cell)) property)))
5220 (defun table--get-cell-justify-property (cell)
5221 "Get cell's justify property."
5222 (table--get-property cell 'table-justify))
5224 (defun table--get-cell-valign-property (cell)
5225 "Get cell's vertical alignment property."
5226 (table--get-property cell 'table-valign))
5228 (defun table--put-property (cell property value)
5229 "Put CELL's PROPERTY the VALUE."
5230 (let ((beg (car cell))
5231 (end (cdr cell)))
5232 (put-text-property beg (1+ beg) property value)
5233 (put-text-property (1- end) end property value)))
5235 (defun table--put-cell-justify-property (cell justify)
5236 "Put cell's justify property."
5237 (table--put-property cell 'table-justify justify))
5239 (defun table--put-cell-valign-property (cell valign)
5240 "Put cell's vertical alignment property."
5241 (table--put-property cell 'table-valign valign))
5243 (defun table--point-entered/left-cell-function (_window _oldpos dir)
5244 "Point has entered a cell.
5245 Refresh the menu bar."
5246 ;; Avoid calling point-motion-hooks recursively.
5247 (let ((inhibit-point-motion-hooks t))
5248 (force-mode-line-update)
5249 (pcase dir
5250 ('left
5251 (setq table-mode-indicator nil)
5252 (run-hooks 'table-point-left-cell-hook))
5253 ('entered
5254 (setq table-mode-indicator t)
5255 (table--warn-incompatibility)
5256 (run-hooks 'table-point-entered-cell-hook)))))
5258 (defun table--warn-incompatibility ()
5259 "If called from interactive operation warn the know incompatibilities.
5260 This feature is disabled when `table-disable-incompatibility-warning'
5261 is non-nil. The warning is done only once per session for each item."
5262 (unless (and table-disable-incompatibility-warning
5263 (not (called-interactively-p 'interactive)))
5264 (cond ((and (featurep 'xemacs)
5265 (not (get 'table-disable-incompatibility-warning 'xemacs)))
5266 (put 'table-disable-incompatibility-warning 'xemacs t)
5267 (display-warning 'table
5269 *** Warning ***
5271 Table package mostly works fine under XEmacs, however, due to the
5272 peculiar implementation of text property under XEmacs, cell splitting
5273 and any undo operation of table exhibit some known strange problems,
5274 such that a border characters dissolve into adjacent cells. Please be
5275 aware of this.
5278 :warning))
5279 ((and (boundp 'flyspell-mode)
5280 flyspell-mode
5281 (not (get 'table-disable-incompatibility-warning 'flyspell)))
5282 (put 'table-disable-incompatibility-warning 'flyspell t)
5283 (display-warning 'table
5285 *** Warning ***
5287 Flyspell minor mode is known to be incompatible with this table
5288 package. The flyspell version 1.5d at URL `http://kaolin.unice.fr/~serrano'
5289 works better than the previous versions however not fully compatible.
5292 :warning))
5295 (defun table--cell-blank-str (&optional n)
5296 "Return blank table cell string of length N."
5297 (let ((str (make-string (or n 1) ?\s)))
5298 (table--put-cell-content-property 0 (length str) str)
5299 str))
5301 (defun table--remove-eol-spaces (beg end &optional bol force)
5302 "Remove spaces at the end of each line in the BEG END region of the current buffer.
5303 When optional BOL is non-nil spaces at the beginning of line are
5304 removed. When optional FORCE is non-nil removal operation is enforced
5305 even when point is within the removal area."
5306 (if (> beg end)
5307 (let ((tmp beg))
5308 (setq beg end)
5309 (setq end tmp)))
5310 (let ((saved-point (point-marker))
5311 (end-marker (copy-marker end)))
5312 (save-excursion
5313 (goto-char beg)
5314 (while (if bol (re-search-forward "^\\( +\\)" end-marker t)
5315 (re-search-forward "\\( +\\)$" end-marker t))
5316 ;; avoid removal that causes the saved point to lose its location.
5317 (if (and (null bol)
5318 (<= (match-beginning 1) saved-point)
5319 (<= saved-point (match-end 1))
5320 (not force))
5321 (delete-region saved-point (match-end 1))
5322 (delete-region (match-beginning 1) (match-end 1)))))
5323 (set-marker saved-point nil)
5324 (set-marker end-marker nil)))
5326 (defun table--fill-region (beg end &optional col justify)
5327 "Fill paragraphs in table cell cache.
5328 Current buffer must already be set to the cache buffer."
5329 (let ((fill-column (or col table-cell-info-width))
5330 (fill-prefix nil)
5331 (enable-kinsoku nil)
5332 (adaptive-fill-mode nil)
5333 (marker-beg (copy-marker beg))
5334 (marker-end (copy-marker end))
5335 (marker-point (point-marker)))
5336 (setq justify (or justify table-cell-info-justify))
5337 (and justify
5338 (not (eq justify 'left))
5339 (not (featurep 'xemacs))
5340 (set-marker-insertion-type marker-point t))
5341 (table--remove-eol-spaces (point-min) (point-max))
5342 (if table-fixed-width-mode
5343 (table--fill-region-strictly marker-beg marker-end)
5344 (let ((paragraph-start table-paragraph-start))
5345 (fill-region marker-beg marker-end justify nil t)))
5346 (goto-char marker-point)
5347 (set-marker marker-beg nil)
5348 (set-marker marker-end nil)
5349 (set-marker marker-point nil)))
5351 (defun table--fill-region-strictly (beg end)
5352 "Fill region strictly so that no line exceeds fill-column.
5353 When a word exceeds fill-column the word is chopped into pieces. The
5354 chopped location is indicated with table-word-continuation-char."
5355 (or (and (markerp beg) (markerp end))
5356 (error "markerp"))
5357 (if (< fill-column 2)
5358 (setq fill-column 2))
5359 ;; first remove all continuation characters.
5360 (goto-char beg)
5361 (while (re-search-forward (concat
5362 (format "[^%c ]\\(" table-word-continuation-char)
5363 (regexp-quote (char-to-string table-word-continuation-char))
5364 "\\s +\\)")
5365 end t)
5366 (delete-region (match-beginning 1) (match-end 1)))
5367 ;; then fill as normal
5368 (let ((paragraph-start table-paragraph-start))
5369 (fill-region beg end nil nil t))
5370 ;; now fix up
5371 (goto-char beg)
5372 (while (let ((col (move-to-column fill-column t)))
5373 (cond
5374 ((and (<= col fill-column)
5375 (looking-at " *$"))
5376 (delete-region (match-beginning 0) (match-end 0))
5377 (and (zerop (forward-line 1))
5378 (< (point) end)))
5379 (t (forward-char -1)
5380 (insert-before-markers (if (equal (char-before) ?\s) ?\s table-word-continuation-char)
5381 "\n")
5382 t)))))
5384 (defun table--goto-coordinate (coordinate &optional no-extension no-tab-expansion)
5385 "Move point to the given COORDINATE and return the location.
5386 When optional NO-EXTENSION is non-nil and the specified coordinate is
5387 not reachable returns nil otherwise the blanks are added if necessary
5388 to achieve the goal coordinate and returns the goal point. It
5389 intentionally does not preserve the original point in case it fails
5390 achieving the goal. When optional NO-TAB-EXPANSION is non-nil and the
5391 goad happens to be in a tab character the tab is not expanded but the
5392 goal ends at the beginning of tab."
5393 (if (or (null coordinate)
5394 (< (car coordinate) 0)
5395 (< (cdr coordinate) 0)) nil
5396 (goto-char (point-min))
5397 (let ((x (car coordinate))
5398 (more-lines (forward-line (cdr coordinate))))
5399 (catch 'exit
5400 (if (zerop (current-column)) nil
5401 (if no-extension
5402 (progn
5403 (move-to-column x)
5404 (throw 'exit nil))
5405 (setq more-lines (1+ more-lines))))
5406 (if (zerop more-lines) nil
5407 (newline more-lines))
5408 (if no-extension
5409 (if (/= (move-to-column x) x)
5410 (if (> (move-to-column x) x)
5411 (if no-tab-expansion
5412 (progn
5413 (while (> (move-to-column x) x)
5414 (setq x (1- x)))
5415 (point))
5416 (throw 'exit (move-to-column x t)))
5417 (throw 'exit nil)))
5418 (move-to-column x t))
5419 (point)))))
5421 (defun table--copy-coordinate (coord)
5422 "Copy coordinate in a new cons cell."
5423 (cons (car coord) (cdr coord)))
5425 (defun table--get-coordinate (&optional where)
5426 "Return the coordinate of point in current buffer.
5427 When optional WHERE is given it returns the coordinate of that
5428 location instead of point in the current buffer. It does not move the
5429 point"
5430 (save-excursion
5431 (if where (goto-char where))
5432 (cons (current-column)
5433 (table--current-line))))
5435 (defun table--current-line (&optional location)
5436 "Return zero based line count of current line or if non-nil LOCATION line."
5437 (save-excursion
5438 (if location (goto-char location))
5439 (beginning-of-line)
5440 (count-lines (point-min) (point))))
5442 (defun table--transcoord-table-to-cache (&optional coordinate)
5443 "Transpose COORDINATE from table coordinate system to cache coordinate system.
5444 When COORDINATE is omitted or nil the point in current buffer is assumed in place."
5445 (table--offset-coordinate
5446 (or coordinate (table--get-coordinate))
5447 table-cell-info-lu-coordinate
5448 'negative))
5450 (defun table--transcoord-cache-to-table (&optional coordinate)
5451 "Transpose COORDINATE from cache coordinate system to table coordinate system.
5452 When COORDINATE is omitted or nil the point in current buffer is assumed in place."
5453 (table--offset-coordinate
5454 (or coordinate (table--get-coordinate))
5455 table-cell-info-lu-coordinate))
5457 (defun table--offset-coordinate (coordinate offset &optional negative)
5458 "Return the offset COORDINATE by OFFSET.
5459 When optional NEGATIVE is non-nil offsetting direction is negative."
5460 (cons (if negative (- (car coordinate) (car offset))
5461 (+ (car coordinate) (car offset)))
5462 (if negative (- (cdr coordinate) (cdr offset))
5463 (+ (cdr coordinate) (cdr offset)))))
5465 (defun table--char-in-str-at-column (str column)
5466 "Return the character in STR at COLUMN location.
5467 When COLUMN is out of range it returns null character."
5468 (let ((idx (table--str-index-at-column str column)))
5469 (if idx (aref str idx)
5470 ?\0)))
5472 (defun table--str-index-at-column (str column)
5473 "Return the character index in STR that corresponds to COLUMN location.
5474 It returns COLUMN unless STR contains some wide characters."
5475 (let ((col 0)
5476 (idx 0)
5477 (len (length str)))
5478 (while (and (< col column) (< idx len))
5479 (setq col (+ col (char-width (aref str idx))))
5480 (setq idx (1+ idx)))
5481 (if (< idx len)
5483 nil)))
5485 (defun table--set-timer (seconds func args)
5486 "Generic wrapper for setting up a timer."
5487 (if (featurep 'xemacs)
5488 ;; the picky xemacs refuses to accept zero
5489 (add-timeout (if (zerop seconds) 0.01 seconds) func args nil)
5490 ;;(run-at-time seconds nil func args)))
5491 ;; somehow run-at-time causes strange problem under Emacs 20.7
5492 ;; this problem does not show up under Emacs 21.0.90
5493 (run-with-idle-timer seconds nil func args)))
5495 (defun table--cancel-timer (timer)
5496 "Generic wrapper for canceling a timer."
5497 (if (featurep 'xemacs)
5498 (disable-timeout timer)
5499 (cancel-timer timer)))
5501 (defun table--get-last-command ()
5502 "Generic wrapper for getting the real last command."
5503 (if (boundp 'real-last-command)
5504 real-last-command
5505 last-command))
5507 (run-hooks 'table-load-hook)
5509 (provide 'table)
5511 ;;; table.el ends here