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