1 ;;; table.el --- create and edit WYSIWYG text based embedded tables
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 ;; 2009, 2010 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 Aug 21 2009 00:16:58 (PDT)
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 3 of the License, or
16 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
32 ;; This package provides text based table creation and editing
33 ;; feature. With this package Emacs is capable of editing tables that
34 ;; are embedded inside a text document, the feature similar to the
35 ;; ones seen in modern WYSIWYG word processors. A table is a
36 ;; rectangular text area consisting from a surrounding frame and
37 ;; content inside the frame. The content is usually subdivided into
38 ;; multiple rectangular cells, see the actual tables used below in
39 ;; this document. Once a table is recognized, editing operation
40 ;; inside a table cell is confined into that specific cell's
41 ;; rectangular area. This means that typing and deleting characters
42 ;; inside a cell do not affect any outside text but introduces
43 ;; appropriate formatting only to the cell contents. If necessary for
44 ;; accommodating added text in the cell, the cell automatically grows
45 ;; vertically and/or horizontally. The package uses no major mode nor
46 ;; minor mode for its implementation because the subject text is
47 ;; localized within a buffer. Therefore the special behaviors inside
48 ;; a table cells are implemented by using keymap text property
49 ;; instead of buffer wide mode-map.
56 ;; Paul Georgief is one of my best friends. He became an Emacs
57 ;; convert after I recommended him trying it several years ago. Now
58 ;; we both are devoted disciples of Emacsism and elisp cult. One day
59 ;; in his Emacs exploration he asked me "Tak, what is a command to
60 ;; edit tables in Emacs?". This question started my journey of this
61 ;; table package development. May the code be with me! In the
62 ;; software world Emacs is probably one of the longest lifetime record
63 ;; holders. Amazingly there have been no direct support for WYSIWYG
64 ;; table editing tasks in Emacs. Many people must have experienced
65 ;; manipulating existing overwrite-mode and picture-mode for this task
66 ;; and only dreamed of having such a lisp package which supports this
67 ;; specific task directly. Certainly, I have been one of them. The
68 ;; most difficult part of dealing with table editing in Emacs probably
69 ;; is how to realize localized rectangular editing effect. Emacs has
70 ;; no rectangular narrowing mechanism. Existing rect package provides
71 ;; basically kill, delete and yank operations of a rectangle, which
72 ;; internally is a mere list of strings. A simple approach for
73 ;; realizing the localized virtual rectangular operation is combining
74 ;; rect package capability with a temporary buffer. Insertion and
75 ;; deletion of a character to a table cell can be trapped by a
76 ;; function that copies the cell rectangle to a temporary buffer then
77 ;; apply the insertion/deletion to the temporary contents. Then it
78 ;; formats the contents by filling the paragraphs in order to fit it
79 ;; into the original rectangular area and finally copy it back to the
80 ;; original buffer. This simplistic approach has to bear with
81 ;; significant performance hit. As cell grows larger the copying
82 ;; rectangle back and forth between the original buffer and the
83 ;; temporary buffer becomes expensive and unbearably slow. It was
84 ;; completely impractical and an obvious failure. An idea has been
85 ;; borrowed from the original Emacs design to overcome this
86 ;; shortcoming. When the terminal screen update was slow and
87 ;; expensive Emacs employed a clever algorithm to reduce actual screen
88 ;; update by removing redundant redrawing operations. Also the actual
89 ;; redrawing was done only when there was enough idling time. This
90 ;; technique significantly improved the previously mentioned
91 ;; undesirable situation. Now the original buffer's rectangle is
92 ;; copied into a cache buffer only once. Any cell editing operation
93 ;; is done only to the cache contents. When there is enough idling
94 ;; time the original buffer's rectangle is updated with the current
95 ;; cache contents. This delayed operation is implemented by using
96 ;; Emacs's timer function. To reduce the visual awkwardness
97 ;; introduced by the delayed effect the cursor location is updated in
98 ;; real-time as a user types while the cell contents remains the same
99 ;; until the next idling time. A key to the success of this approach
100 ;; is how to maintain cache coherency. As a user moves point in and
101 ;; out of a cell the table buffer contents and the cache buffer
102 ;; contents must be synchronized without a mistake. By observing user
103 ;; action carefully this is possible however not easy. Once this
104 ;; mechanism is firmly implemented the rest of table features grew in
105 ;; relatively painless progression. Those users who are familiar with
106 ;; Emacs internals appreciate this table package more. Because it
107 ;; demonstrates how extensible Emacs is by showing something that
108 ;; appears like a magic. It lets you re-discover the potential of
116 ;; If this is the first time for you to try this package, go ahead and
117 ;; load the package by M-x `load-file' RET. Specify the package file
118 ;; name "table.el". Then switch to a new test buffer and issue the
119 ;; command M-x `table-insert' RET. It'll ask you number of columns,
120 ;; number of rows, cell width and cell height. Give some small
121 ;; numbers for each of them. Play with the resulted table for a
122 ;; while. If you have menu system find the item "Table" under "Tools"
123 ;; and "Table" in the menu bar when the point is in a table cell.
124 ;; Some of them are pretty intuitive and you can easily guess what
125 ;; they do. M-x `describe-function' and get the documentation of
126 ;; `table-insert'. The document includes a short tutorial. When you
127 ;; are tired of guessing how it works come back to this document
130 ;; To use the package regularly place this file in the site library
131 ;; directory and add the next expression in your .emacs file. Make
132 ;; sure that directory is included in the `load-path'.
136 ;; Have the next expression also, if you want always be ready to edit
137 ;; tables inside text files. This mechanism is analogous to
138 ;; fontification in a sense that tables are recognized at editing time
139 ;; without having table information saved along with the text itself.
141 ;; (add-hook 'text-mode-hook 'table-recognize)
143 ;; Following is a table of entry points and brief description of each
144 ;; of them. The tables below are of course generated and edited by
145 ;; using this package. Not all the commands are bound to keys. Many
146 ;; of them must be invoked by "M-x" (`execute-extended-command')
147 ;; command. Refer to the section "Keymap" below for the commands
148 ;; available from keys.
150 ;; +------------------------------------------------------------------+
151 ;; | User Visible Entry Points |
152 ;; +-------------------------------+----------------------------------+
153 ;; | Function | Description |
154 ;; +-------------------------------+----------------------------------+
155 ;; |`table-insert' |Insert a table consisting of grid |
156 ;; | |of cells by specifying the number |
157 ;; | |of COLUMNS, number of ROWS, cell |
158 ;; | |WIDTH and cell HEIGHT. |
159 ;; +-------------------------------+----------------------------------+
160 ;; |`table-insert-row' |Insert row(s) of cells before the |
161 ;; | |current row that matches the |
162 ;; | |current row structure. |
163 ;; +-------------------------------+----------------------------------+
164 ;; |`table-insert-column' |Insert column(s) of cells before |
165 ;; | |the current column that matches |
166 ;; | |the current column structure. |
167 ;; +-------------------------------+----------------------------------+
168 ;; |`table-delete-row' |Delete row(s) of cells. The row |
169 ;; | |must consist from cells of the |
171 ;; +-------------------------------+----------------------------------+
172 ;; |`table-delete-column' |Delete column(s) of cells. The |
173 ;; | |column must consist from cells of |
174 ;; | |the same width. |
175 ;; +-------------------------------+----------------------------------+
176 ;; |`table-recognize' |Recognize all tables in the |
177 ;; |`table-unrecognize' |current buffer and |
178 ;; | |activate/inactivate them. |
179 ;; +-------------------------------+----------------------------------+
180 ;; |`table-recognize-region' |Recognize all the cells in a |
181 ;; |`table-unrecognize-region' |region and activate/inactivate |
183 ;; +-------------------------------+----------------------------------+
184 ;; |`table-recognize-table' |Recognize all the cells in a |
185 ;; |`table-unrecognize-table' |single table and |
186 ;; | |activate/inactivate them. |
187 ;; +-------------------------------+----------------------------------+
188 ;; |`table-recognize-cell' |Recognize a cell. Find a cell |
189 ;; |`table-unrecognize-cell' |which contains the current point |
190 ;; | |and activate/inactivate that cell.|
191 ;; +-------------------------------+----------------------------------+
192 ;; |`table-forward-cell' |Move point to the next Nth cell in|
194 ;; +-------------------------------+----------------------------------+
195 ;; |`table-backward-cell' |Move point to the previous Nth |
196 ;; | |cell in a table. |
197 ;; +-------------------------------+----------------------------------+
198 ;; |`table-span-cell' |Span the current cell toward the |
199 ;; | |specified direction and merge it |
200 ;; | |with the adjacent cell. The |
201 ;; | |direction is right, left, above or|
203 ;; +-------------------------------+----------------------------------+
204 ;; |`table-split-cell-vertically' |Split the current cell vertically |
205 ;; | |and create a cell above and a cell|
206 ;; | |below the point location. |
207 ;; +-------------------------------+----------------------------------+
208 ;; |`table-split-cell-horizontally'|Split the current cell |
209 ;; | |horizontally and create a cell on |
210 ;; | |the left and a cell on the right |
211 ;; | |of the point location. |
212 ;; +-------------------------------+----------------------------------+
213 ;; |`table-split-cell' |Split the current cell vertically |
214 ;; | |or horizontally. This is a |
215 ;; | |wrapper command to the other two |
216 ;; | |orientation specific commands. |
217 ;; +-------------------------------+----------------------------------+
218 ;; |`table-heighten-cell' |Heighten the current cell. |
219 ;; +-------------------------------+----------------------------------+
220 ;; |`table-shorten-cell' |Shorten the current cell. |
221 ;; +-------------------------------+----------------------------------+
222 ;; |`table-widen-cell' |Widen the current cell. |
223 ;; +-------------------------------+----------------------------------+
224 ;; |`table-narrow-cell' |Narrow the current cell. |
225 ;; +-------------------------------+----------------------------------+
226 ;; |`table-fixed-width-mode' |Toggle fixed width mode. In the |
227 ;; | |fixed width mode, typing inside a |
228 ;; | |cell never changes the cell width,|
229 ;; | |while in the normal mode the cell |
230 ;; | |width expands automatically in |
231 ;; | |order to prevent a word being |
232 ;; | |folded into multiple lines. Fixed|
233 ;; | |width mode reverses video or |
234 ;; | |underline the cell contents for |
235 ;; | |its indication. |
236 ;; +-------------------------------+----------------------------------+
237 ;; |`table-query-dimension' |Compute and report the current |
238 ;; | |cell dimension, current table |
239 ;; | |dimension and the number of |
240 ;; | |columns and rows in the table. |
241 ;; +-------------------------------+----------------------------------+
242 ;; |`table-generate-source' |Generate the source of the current|
243 ;; | |table in the specified language |
244 ;; | |and insert it into a specified |
246 ;; +-------------------------------+----------------------------------+
247 ;; |`table-insert-sequence' |Travel cells forward while |
248 ;; | |inserting a specified sequence |
249 ;; | |string into each cell. |
250 ;; +-------------------------------+----------------------------------+
251 ;; |`table-capture' |Convert plain text into a table by|
252 ;; | |capturing the text in the region. |
253 ;; +-------------------------------+----------------------------------+
254 ;; |`table-release' |Convert a table into plain text by|
255 ;; | |removing the frame from a table. |
256 ;; +-------------------------------+----------------------------------+
257 ;; |`table-justify' |Justify the contents of cell(s). |
258 ;; +-------------------------------+----------------------------------+
263 ;; You may find that some of commonly expected table commands are
264 ;; missing such as copying a row/column and yanking it. Those
265 ;; functions can be obtained through existing Emacs text editing
266 ;; commands. Rows are easily manipulated with region commands and
267 ;; columns can be copied and pasted through rectangle commands. After
268 ;; all a table is still a part of text in the buffer. Only the
269 ;; special behaviors exist inside each cell through text properties.
271 ;; `table-generate-html' which appeared in earlier releases is
272 ;; deprecated in favor of `table-generate-source'. Now HTML is
273 ;; treated as one of the languages used for describing the table's
274 ;; logical structure.
281 ;; Although this package does not use a mode it does use its own
282 ;; keymap inside a table cell by way of keymap text property. Some of
283 ;; the standard basic editing commands bound to certain keys are
284 ;; replaced with the table specific version of corresponding commands.
285 ;; This replacement combination is listed in the constant alist
286 ;; `table-command-remap-alist' declared below. This alist is
287 ;; not meant to be user configurable but mentioned here for your
288 ;; better understanding of using this package. In addition, table
289 ;; cells have some table specific bindings for cell navigation and
290 ;; cell reformation. You can find these additional bindings in the
291 ;; constant `table-cell-bindings'. Those key bound functions are
292 ;; considered as internal functions instead of normal commands,
293 ;; therefore they have special prefix, *table-- instead of table-, for
294 ;; symbols. The purpose of this is to make it easier for a user to
295 ;; use command name completion. There is a "normal hooks" variable
296 ;; `table-cell-map-hook' prepared for users to override the default
297 ;; table cell bindings. Following is the table of predefined default
298 ;; key bound commands inside a table cell. Remember these bindings
299 ;; exist only inside a table cell. When your terminal is a tty, the
300 ;; control modifier may not be available or applicable for those
301 ;; special characters. In this case use "C-cC-c", which is
302 ;; customizable via `table-command-prefix', as the prefix key
303 ;; sequence. This should preceding the following special character
304 ;; without the control modifier. For example, use "C-cC-c|" instead
307 ;; +------------------------------------------------------------------+
308 ;; | Default Bindings in a Table Cell |
309 ;; +-------+----------------------------------------------------------+
310 ;; | Key | Function |
311 ;; +-------+----------------------------------------------------------+
312 ;; | TAB |Move point forward to the beginning of the next cell. |
313 ;; +-------+----------------------------------------------------------+
314 ;; | "C->" |Widen the current cell. |
315 ;; +-------+----------------------------------------------------------+
316 ;; | "C-<" |Narrow the current cell. |
317 ;; +-------+----------------------------------------------------------+
318 ;; | "C-}" |Heighten the current cell. |
319 ;; +-------+----------------------------------------------------------+
320 ;; | "C-{" |Shorten the current cell. |
321 ;; +-------+----------------------------------------------------------+
322 ;; | "C--" |Split current cell vertically. (one above and one below) |
323 ;; +-------+----------------------------------------------------------+
324 ;; | "C-|" |Split current cell horizontally. (one left and one right) |
325 ;; +-------+----------------------------------------------------------+
326 ;; | "C-*" |Span current cell into adjacent one. |
327 ;; +-------+----------------------------------------------------------+
328 ;; | "C-+" |Insert row(s)/column(s). |
329 ;; +-------+----------------------------------------------------------+
330 ;; | "C-!" |Toggle between normal mode and fixed width mode. |
331 ;; +-------+----------------------------------------------------------+
332 ;; | "C-#" |Report cell and table dimension. |
333 ;; +-------+----------------------------------------------------------+
334 ;; | "C-^" |Generate the source in a language from the current table. |
335 ;; +-------+----------------------------------------------------------+
336 ;; | "C-:" |Justify the contents of cell(s). |
337 ;; +-------+----------------------------------------------------------+
341 ;; When using `table-cell-map-hook' do not use `local-set-key'.
343 ;; (add-hook 'table-cell-map-hook
344 ;; (function (lambda ()
345 ;; (local-set-key [<key sequence>] '<function>))))
347 ;; Above code is well known ~/.emacs idiom for customizing a mode
348 ;; specific keymap however it does not work for this package. This is
349 ;; because there is no table mode in effect. This package does not
350 ;; use a local map therefor you must modify `table-cell-map'
351 ;; explicitly. The correct way of achieving above task is:
353 ;; (add-hook 'table-cell-map-hook
354 ;; (function (lambda ()
355 ;; (define-key table-cell-map [<key sequence>] '<function>))))
361 ;; If a menu system is available a group of table specific menu items,
362 ;; "Table" under "Tools" section of the menu bar, is globally added
363 ;; after this package is loaded. The commands in this group are
364 ;; limited to the ones that are related to creation and initialization
365 ;; of tables, such as to insert a table, to insert rows and columns,
366 ;; or recognize and unrecognize tables. Once tables are created and
367 ;; point is placed inside of a table cell a table specific menu item
368 ;; "Table" appears directly on the menu bar. The commands in this
369 ;; menu give full control on table manipulation that include cell
370 ;; navigation, insertion, splitting, spanning, shrinking, expansion
371 ;; and unrecognizing. In addition to above two types of menu there is
372 ;; a pop-up menu available within a table cell. The content of pop-up
373 ;; menu is identical to the full table menu. [mouse-3] is the default
374 ;; button, defined in `table-cell-bindings', to bring up the pop-up
375 ;; menu. It can be reconfigured via `table-cell-map-hook'. The
376 ;; benefit of a pop-up menu is that it combines selection of the
377 ;; location (which cell, where in the cell) and selection of the
378 ;; desired operation into a single clicking action.
381 ;; -------------------------------
382 ;; Definition of tables and cells:
383 ;; -------------------------------
385 ;; There is no artificial-intelligence magic in this package. The
386 ;; definition of a table and the cells inside the table is reasonably
387 ;; limited in order to achieve acceptable performance in the
388 ;; interactive operation under Emacs lisp implementation. A valid
389 ;; table is a rectangular text area completely filled with valid
390 ;; cells. A valid cell is a rectangle text area, which four borders
391 ;; consist of valid border characters. Cells can not be nested one to
392 ;; another or overlapped to each other except sharing the border
393 ;; lines. A valid character of a cell's vertical border is either
394 ;; table-cell-vertical-char `|' or table-cell-intersection-char `+'.
395 ;; A valid character of a cell's horizontal border is either
396 ;; one of table-cell-horizontal-chars (`-' or `=')
397 ;; or table-cell-intersection-char `+'.
398 ;; A valid character of the four corners of a cell must be
399 ;; table-cell-intersection-char `+'. A cell must contain at least one
400 ;; character space inside. There is no restriction about the contents
401 ;; of a table cell, however it is advised if possible to avoid using
402 ;; any of the border characters inside a table cell. Normally a few
403 ;; boarder characters inside a table cell are harmless. But it is
404 ;; possible that they accidentally align up to emulate a bogus cell
405 ;; corner on which software relies on for cell recognition. When this
406 ;; happens the software may be fooled by it and fail to determine
407 ;; correct cell dimension.
409 ;; Following are the examples of valid tables.
411 ;; +--+----+---+ +-+ +--+-----+
413 ;; +--+----+---+ +-+ | +--+--+
415 ;; +--+----+---+ +--+--+ |
419 ;; The next five tables are the examples of invalid tables. (From
420 ;; left to right, 1. nested cells 2. overlapped cells and a
421 ;; non-rectangle cell 3. non-rectangle table 4. zero width/height
422 ;; cells 5. zero sized cell)
424 ;; +-----+ +-----+ +--+ +-++--+ ++
425 ;; | | | | | | | || | ++
426 ;; | +-+ | | | | | | || |
427 ;; | | | | +--+ | +--+--+ +-++--+
428 ;; | +-+ | | | | | | | +-++--+
429 ;; | | | | | | | | | || |
430 ;; +-----+ +--+--+ +--+--+ +-++--+
432 ;; Although the program may recognizes some of these invalid tables,
433 ;; results from the subsequent editing operations inside those cells
434 ;; are not predictable and will most likely start destroying the table
437 ;; It is strongly recommended to have at least one blank line above
438 ;; and below a table. For a table to coexist peacefully with
439 ;; surrounding environment table needs to be separated from unrelated
440 ;; text. This is necessary for the left table to grow or shrink
441 ;; horizontally without breaking the right table in the following
444 ;; +-----+-----+-----+
445 ;; +-----+-----+ | | | |
446 ;; | | | +-----+-----+-----+
447 ;; +-----+-----+ | | | |
448 ;; +-----+-----+-----+
451 ;; -------------------------
452 ;; Cell contents formatting:
453 ;; -------------------------
455 ;; The cell contents are formatted by filling a paragraph immediately
456 ;; after characters are inserted into or deleted from a cell. Because
457 ;; of this, cell contents always remain fit inside a cell neatly. One
458 ;; drawback of this is that users do not have full control over
459 ;; spacing between words and line breaking. Only one space can be
460 ;; entered between words and up to two spaces between sentences. For
461 ;; a newline to be effective the new line must form a beginning of
462 ;; paragraph, otherwise it'll automatically be merged with the
463 ;; previous line in a same paragraph. To form a new paragraph the
464 ;; line must start with some space characters or immediately follow a
465 ;; blank line. Here is a typical example of how to list items within
466 ;; a cell. Without a space at the beginning of each line the items
467 ;; can not stand on their own.
469 ;; +---------------------------------+
470 ;; |Each one of the following three |
471 ;; |items starts with a space |
472 ;; |character thus forms a paragraph |
473 ;; |of its own. Limitations in cell |
474 ;; |contents formatting are: |
476 ;; | 1. Only one space between words.|
477 ;; | 2. Up to two spaces between |
479 ;; | 3. A paragraph must start with |
480 ;; |spaces or follow a blank line. |
482 ;; |This paragraph stays away from |
483 ;; |the item 3 because there is a |
484 ;; |blank line between them. |
485 ;; +---------------------------------+
487 ;; In the normal operation table cell width grows automatically when
488 ;; certain word has to be folded into the next line if the width had
489 ;; not been increased. This normal operation is useful and
490 ;; appropriate for most of the time, however, it is sometimes useful
491 ;; or necessary to fix the width of table and width of table cells.
492 ;; For this purpose the package provides fixed width mode. You can
493 ;; toggle between fixed width mode and normal mode by "C-!".
495 ;; Here is a simple example of the fixed width mode. Suppose we have
496 ;; a table like this one.
502 ;; In normal mode if you type a word "antidisestablishmentarianism" it
503 ;; grows the cell horizontally like this.
505 ;; +----------------------------+
506 ;; |antidisestablishmentarianism|
507 ;; +----------------------------+
509 ;; In the fixed width mode the same action produces the following
510 ;; result. The folded locations are indicated by a continuation
511 ;; character (`\' is the default). The continuation character is
512 ;; treated specially so it is recommended to choose a character that
513 ;; does not appear elsewhere in table cells. This character is
514 ;; configurable via customization and is kept in the variable
515 ;; `table-word-continuation-char'. The continuation character is
516 ;; treated specially only in the fixed width mode and has no special
517 ;; meaning in the normal mode however.
530 ;; -------------------
531 ;; Cell Justification:
532 ;; -------------------
534 ;; By default the cell contents are filled with left justification and
535 ;; no vertical justification. A paragraph can be justified
536 ;; individually but only horizontally. Paragraph justification is for
537 ;; appearance only and does not change any structural information
538 ;; while cell justification affects table's structural information.
539 ;; For cell justification a user can select horizontal justification
540 ;; and vertical justification independently. Horizontal justification
541 ;; must be one of the three 'left, 'center or 'right. Vertical
542 ;; justification can be 'top, 'middle, 'bottom or 'none. When a cell
543 ;; is justified, that information is recorded as a part of text
544 ;; property therefore the information is persistent as long as the
545 ;; cell remains within the Emacs world. Even copying tables by region
546 ;; and rectangle manipulation commands preserve this information.
547 ;; However, once the table text is saved as a file and the buffer is
548 ;; killed the justification information vanishes permanently. To
549 ;; alleviate this shortcoming without forcing users to save and
550 ;; maintain a separate attribute file, the table code detects
551 ;; justification of each cell when recognizing a table. This
552 ;; detection is done by guessing the justification by looking at the
553 ;; appearance of the cell contents. Since it is a guessing work it
554 ;; does not guarantee the perfectness but it is designed to be
555 ;; practically good enough. The guessing algorithm is implemented in
556 ;; the function `table--detect-cell-alignment'. If you have better
557 ;; algorithm or idea any suggestion is welcome.
561 ;; Todo: (in the order of priority, some are just possibility)
564 ;; Fix compatibilities with other input method than quail
565 ;; Resolve conflict with flyspell
566 ;; Use mouse for resizing cells
567 ;; A mechanism to link cells internally
568 ;; Consider the use of variable width font under Emacs 21
569 ;; Consider the use of `:box' face attribute under Emacs 21
570 ;; Consider the use of `modification-hooks' text property instead of
571 ;; rebinding the keymap
572 ;; Maybe provide complete XEmacs support in the future however the
573 ;; "extent" is the single largest obstacle lying ahead, read the
574 ;; document in Emacs info.
575 ;; (eval '(progn (require 'info) (Info-find-node "elisp" "Not Intervals")))
582 ;; Table would not have been possible without the help and
583 ;; encouragement of the following spirited contributors.
585 ;; Paul Georgief <georgief@igpp.ucsd.edu> has been the best tester
586 ;; of the code as well as the constructive criticizer.
588 ;; Gerd Moellmann <gerd@gnu.org> gave me useful suggestions from Emacs
591 ;; Richard Stallman <rms@gnu.org> showed the initial interest in this
592 ;; attempt of implementing the table feature to Emacs. This greatly
593 ;; motivated me to follow through to its completion.
595 ;; Kenichi Handa <handa@etl.go.jp> kindly guided me through to
596 ;; overcome many technical issues while I was struggling with quail
597 ;; related internationalization problems.
599 ;; Christoph Conrad <christoph.conrad@gmx.de> suggested making symbol
600 ;; names consistent as well as fixing several bugs.
602 ;; Paul Lew <paullew@cisco.com> suggested implementing fixed width
603 ;; mode as well as multi column width (row height) input interface.
605 ;; Michael Smith <smith@xml-doc.org> a well-informed DocBook user
606 ;; asked for CALS table source generation and helped me following
607 ;; through the work by offering valuable suggestions and testing out
608 ;; the code. Jorge Godoy <godoy@conectiva.com> has also suggested
609 ;; supporting for DocBook tables.
611 ;; And many other individuals who reported bugs and suggestions.
616 (require 'regexp-opt
)
618 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
623 ;; hush up the byte-compiler
624 (defvar quail-translating
)
625 (defvar quail-converting
)
626 (defvar flyspell-mode
)
627 (defvar real-last-command
)
628 (defvar delete-selection-mode
)
630 ;; (eval-when-compile
631 ;; (unless (fboundp 'set-face-property)
632 ;; (defun set-face-property (face prop value)))
633 ;; (unless (fboundp 'unibyte-char-to-multibyte)
634 ;; (defun unibyte-char-to-multibyte (char)))
635 ;; (defun table--point-in-cell-p (&optional location)))
637 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
643 "Text based table manipulation utilities."
649 (defgroup table-hooks nil
650 "Hooks for table manipulation utilities."
653 (defcustom table-time-before-update
0.2
654 "*Time in seconds before updating the cell contents after typing.
655 Updating the cell contents on the screen takes place only after this
656 specified amount of time has passed after the last modification to the
657 cell contents. When the contents of a table cell changes repetitively
658 and frequently the updating the cell contents on the screen is
659 deferred until at least this specified amount of quiet time passes. A
660 smaller number wastes more computation resource by unnecessarily
661 frequent screen update. A large number presents noticeable and
662 annoying delay before the typed result start appearing on the screen."
663 :tag
"Time Before Cell Update"
667 (defcustom table-time-before-reformat
0.2
668 "*Time in seconds before reformatting the table.
669 This many seconds must pass in addition to `table-time-before-update'
670 before the table is updated with newly widened width or heightened
672 :tag
"Time Before Cell Reformat"
676 (defcustom table-command-prefix
[(control c
) (control c
)]
677 "*Key sequence to be used as prefix for table command key bindings."
678 :type
'(vector (repeat :inline t sexp
))
679 :tag
"Table Command Prefix"
683 '((((min-colors 88) (class color
))
684 (:foreground
"gray90" :background
"blue1"))
686 (:foreground
"gray90" :background
"blue"))
688 "*Face used for table cell contents."
692 (defcustom table-cell-horizontal-chars
"-="
693 "*Characters that may be used for table cell's horizontal border line."
694 :tag
"Cell Horizontal Boundary Characters"
698 (defcustom table-cell-vertical-char ?\|
699 "*Character that forms table cell's vertical border line."
700 :tag
"Cell Vertical Boundary Character"
704 (defcustom table-cell-intersection-char ?\
+
705 "*Character that forms table cell's corner."
706 :tag
"Cell Intersection Character"
710 (defcustom table-word-continuation-char ?
\\
711 "*Character that indicates word continuation into the next line.
712 This character has a special meaning only in the fixed width mode,
713 that is when `table-fixed-width-mode' is non-nil . In the fixed width
714 mode this character indicates that the location is continuing into the
715 next line. Be careful about the choice of this character. It is
716 treated substantially different manner than ordinary characters. Try
717 select a character that is unlikely to appear in your document."
718 :tag
"Cell Word Continuation Character"
722 (defun table-set-table-fixed-width-mode (variable value
)
723 (if (fboundp variable
)
724 (funcall variable
(if value
1 -
1))))
726 (defun table-initialize-table-fixed-width-mode (variable value
)
727 (set variable value
))
729 (defcustom table-fixed-width-mode nil
730 "*Cell width is fixed when this is non-nil.
731 Normally it should be nil for allowing automatic cell width expansion
732 that widens a cell when it is necessary. When non-nil, typing in a
733 cell does not automatically expand the cell width. A word that is too
734 long to fit in a cell is chopped into multiple lines. The chopped
735 location is indicated by `table-word-continuation-char'. This
736 variable's value can be toggled by \\[table-fixed-width-mode] at
738 :tag
"Fix Cell Width"
740 :initialize
'table-initialize-table-fixed-width-mode
741 :set
'table-set-table-fixed-width-mode
744 (defcustom table-detect-cell-alignment t
745 "*Detect cell contents alignment automatically.
746 When non-nil cell alignment is automatically determined by the
747 appearance of the current cell contents when recognizing tables as a
748 whole. This applies to `table-recognize', `table-recognize-region'
749 and `table-recognize-table' but not to `table-recognize-cell'."
750 :tag
"Detect Cell Alignment"
754 (defcustom table-dest-buffer-name
"table"
755 "*Default buffer name (without a suffix) for source generation."
756 :tag
"Source Buffer Name"
760 (defcustom table-html-delegate-spacing-to-user-agent nil
761 "*Non-nil delegates cell contents spacing entirely to user agent.
762 Otherwise, when nil, it preserves the original spacing and line breaks."
763 :tag
"HTML delegate spacing"
767 (defcustom table-html-th-rows
0
768 "*Number of top rows to become header cells automatically in HTML generation."
769 :tag
"HTML Header Rows"
773 (defcustom table-html-th-columns
0
774 "*Number of left columns to become header cells automatically in HTML generation."
775 :tag
"HTML Header Columns"
779 (defcustom table-html-table-attribute
"border=\"1\""
780 "*Table attribute that applies to the table in HTML generation."
781 :tag
"HTML table attribute"
785 (defcustom table-html-cell-attribute
""
786 "*Cell attribute that applies to all cells in HTML generation.
787 Do not specify \"align\" and \"valign\" because they are determined by
788 the cell contents dynamically."
789 :tag
"HTML cell attribute"
793 (defcustom table-cals-thead-rows
1
794 "*Number of top rows to become header rows in CALS table."
795 :tag
"CALS Header Rows"
800 (defcustom table-cell-map-hook nil
801 "*Normal hooks run when finishing construction of `table-cell-map'.
802 User can modify `table-cell-map' by adding custom functions here."
803 :tag
"Cell Keymap Hooks"
807 (defcustom table-disable-incompatibility-warning nil
808 "*Disable compatibility warning notice.
809 When nil user is reminded of known incompatible issues."
810 :tag
"Disable Incompatibility Warning"
814 (defcustom table-abort-recognition-when-input-pending t
815 "*Abort current recognition process when input pending.
816 Abort current recognition process when we are not sure that no input
817 is available. When non-nil lengthy recognition process is aborted
818 simply by any key input."
819 :tag
"Abort Recognition When Input Pending"
824 (defcustom table-load-hook nil
825 "*List of functions to be called after the table is first loaded."
830 (defcustom table-point-entered-cell-hook nil
831 "*List of functions to be called after point entered a table cell."
836 (defcustom table-point-left-cell-hook nil
837 "*List of functions to be called after point left a table cell."
841 (defvar table-yank-handler
'(nil nil t nil
)
842 "Yank handler for tables.")
844 (setplist 'table-disable-incompatibility-warning nil
)
846 (defvar table-disable-menu
(null (and (locate-library "easymenu")
848 (fboundp 'easy-menu-add-item
)))
849 "*When non-nil, use of menu by table package is disabled.
850 It must be set before loading this package `table.el' for the first
854 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
859 ;;; Internal variables and constants
860 ;;; No need of user configuration
862 (defconst table-paragraph-start
"[ \t\n\f]"
863 "*Regexp for beginning of a line that starts OR separates paragraphs.")
864 (defconst table-cache-buffer-name
" *table cell cache*"
865 "Cell cache buffer name.")
866 (defvar table-cell-info-lu-coordinate nil
867 "Zero based coordinate of the cached cell's left upper corner.")
868 (defvar table-cell-info-rb-coordinate nil
869 "Zero based coordinate of the cached cell's right bottom corner.")
870 (defvar table-cell-info-width nil
871 "Number of characters per cached cell width.")
872 (defvar table-cell-info-height nil
873 "Number of lines per cached cell height.")
874 (defvar table-cell-info-justify nil
875 "Justification information of the cached cell.")
876 (defvar table-cell-info-valign nil
877 "Vertical alignment information of the cached cell.")
878 (defvar table-cell-self-insert-command-count
0
879 "Counter for undo control.")
880 (defvar table-cell-map nil
881 "Keymap for table cell contents.")
882 (defvar table-cell-global-map-alist nil
883 "Alist of copy of global maps that are substituted in `table-cell-map'.")
884 (defvar table-global-menu-map nil
885 "Menu map created via `easy-menu-define'.")
886 (defvar table-cell-menu-map nil
887 "Menu map created via `easy-menu-define'.")
888 (defvar table-cell-buffer nil
889 "Buffer that contains the table cell.")
890 (defvar table-cell-cache-point-coordinate nil
891 "Cache point coordinate based from the cell origin.")
892 (defvar table-cell-cache-mark-coordinate nil
893 "Cache mark coordinate based from the cell origin.")
894 (defvar table-cell-entered-state nil
895 "Records the state whether currently in a cell or nor.")
896 (defvar table-update-timer nil
897 "Timer id for deferred cell update.")
898 (defvar table-widen-timer nil
899 "Timer id for deferred cell update.")
900 (defvar table-heighten-timer nil
901 "Timer id for deferred cell update.")
902 (defvar table-inhibit-update nil
903 "Non-nil inhibits implicit cell and cache updates.
904 It inhibits `table-with-cache-buffer' to update data in both direction, cell to cache and cache to cell.")
905 (defvar table-inhibit-auto-fill-paragraph nil
906 "Non-nil inhibits auto fill paragraph when `table-with-cache-buffer' exits.
907 This is always set to nil at the entry to `table-with-cache-buffer' before executing body forms.")
908 (defvar table-mode-indicator nil
909 "For mode line indicator")
910 ;; This is not a real minor-mode but placed in the minor-mode-alist
911 ;; so that we can show the indicator on the mode line handy.
912 (make-variable-buffer-local 'table-mode-indicator
)
913 (unless (assq table-mode-indicator minor-mode-alist
)
914 (push '(table-mode-indicator (table-fixed-width-mode " Fixed-Table" " Table"))
917 (defconst table-source-languages
'(html latex cals
)
918 "Supported source languages.")
919 (defvar table-source-info-plist nil
920 "General storage for temporary information used while generating source.")
922 ;; The following history containers not only keep the history of user
923 ;; entries but also serve as the default value providers. When an
924 ;; interactive command is invoked it offers a user the latest entry
925 ;; of the history as a default selection. Therefore the values below
926 ;; are the first default value when a command is invoked for the very
927 ;; first time when there is no real history existing yet.
928 (defvar table-cell-span-direction-history
'("right"))
929 (defvar table-cell-split-orientation-history
'("horizontally"))
930 (defvar table-cell-split-contents-to-history
'("split"))
931 (defvar table-insert-row-column-history
'("row"))
932 (defvar table-justify-history
'("center"))
933 (defvar table-columns-history
'("3"))
934 (defvar table-rows-history
'("3"))
935 (defvar table-cell-width-history
'("5"))
936 (defvar table-cell-height-history
'("1"))
937 (defvar table-source-caption-history
'("Table"))
938 (defvar table-sequence-string-history
'("0"))
939 (defvar table-sequence-count-history
'("0"))
940 (defvar table-sequence-increment-history
'("1"))
941 (defvar table-sequence-interval-history
'("1"))
942 (defvar table-sequence-justify-history
'("left"))
943 (defvar table-source-language-history
'("html"))
944 (defvar table-col-delim-regexp-history
'(""))
945 (defvar table-row-delim-regexp-history
'(""))
946 (defvar table-capture-justify-history
'("left"))
947 (defvar table-capture-min-cell-width-history
'("5"))
948 (defvar table-capture-columns-history
'(""))
949 (defvar table-target-history
'("cell"))
951 ;; Some entries in `table-cell-bindings' are duplicated in
952 ;; `table-command-remap-alist'. There is a good reason for
953 ;; this. Common key like return key may be taken by some other
954 ;; function than normal `newline' function. Thus binding return key
955 ;; directly for `*table--cell-newline' ensures that the correct enter
956 ;; operation in a table cell. However
957 ;; `table-command-remap-alist' has an additional role than
958 ;; replacing commands. It is also used to construct a table command
959 ;; list. This list is very important because it is used to check if
960 ;; the previous command was one of them in this list or not. If the
961 ;; previous command is found in the list the current command will not
962 ;; refill the table cache. If the command were not listed fast
963 ;; typing can cause unwanted cache refill.
964 (defconst table-cell-bindings
965 '(([(control i
)] . table-forward-cell
)
966 ([(control I
)] . table-backward-cell
)
967 ([tab] . table-forward-cell)
968 ([(shift backtab)] . table-backward-cell) ; for HPUX console keyboard
969 ([(shift iso-lefttab)] . table-backward-cell) ; shift-tab on a microsoft natural keyboard and redhat linux
970 ([(shift tab)] . table-backward-cell)
971 ([return] . *table--cell-newline)
972 ([(control m)] . *table--cell-newline)
973 ([(control j)] . *table--cell-newline-and-indent)
974 ([mouse-3] . *table--present-cell-popup-menu)
975 ([(control ?>)] . table-widen-cell)
976 ([(control ?<)] . table-narrow-cell)
977 ([(control ?})] . table-heighten-cell)
978 ([(control ?{)] . table-shorten-cell)
979 ([(control ?-)] . table-split-cell-vertically)
980 ([(control ?|)] . table-split-cell-horizontally)
981 ([(control ?*)] . table-span-cell)
982 ([(control ?+)] . table-insert-row-column)
983 ([(control ?!)] . table-fixed-width-mode)
984 ([(control ?#)] . table-query-dimension)
985 ([(control ?^)] . table-generate-source)
986 ([(control ?:)] . table-justify)
988 "Bindings for table cell commands.")
990 (defvar table-command-remap-alist
991 '((self-insert-command . *table--cell-self-insert-command)
992 (completion-separator-self-insert-autofilling . *table--cell-self-insert-command)
993 (completion-separator-self-insert-command . *table--cell-self-insert-command)
994 (delete-char . *table--cell-delete-char)
995 (delete-backward-char . *table--cell-delete-backward-char)
996 (backward-delete-char . *table--cell-delete-backward-char)
997 (backward-delete-char-untabify . *table--cell-delete-backward-char)
998 (newline . *table--cell-newline)
999 (newline-and-indent . *table--cell-newline-and-indent)
1000 (open-line . *table--cell-open-line)
1001 (quoted-insert . *table--cell-quoted-insert)
1002 (describe-mode . *table--cell-describe-mode)
1003 (describe-bindings . *table--cell-describe-bindings)
1004 (dabbrev-expand . *table--cell-dabbrev-expand)
1005 (dabbrev-completion . *table--cell-dabbrev-completion))
1006 "List of cons cells consisting of (ORIGINAL-COMMAND . TABLE-VERSION-OF-THE-COMMAND).")
1008 (defvar table-command-list nil
1009 "List of commands that override original commands.")
1010 ;; construct the real contents of the `table-command-list'
1011 (let ((remap-alist table-command-remap-alist))
1012 (setq table-command-list nil)
1014 (setq table-command-list (cons (cdar remap-alist) table-command-list))
1015 (setq remap-alist (cdr remap-alist))))
1017 (defconst table-global-menu
1020 ["a Table..." table-insert
1021 :active (and (not buffer-read-only) (not (table--probe-cell)))
1022 :help "Insert a text based table at point"]
1023 ["Row" table-insert-row
1024 :active (table--row-column-insertion-point-p)
1025 :help "Insert row(s) of cells in table"]
1026 ["Column" table-insert-column
1027 :active (table--row-column-insertion-point-p 'column)
1028 :help "Insert column(s) of cells in table"])
1031 ["in Buffer" table-recognize
1033 :help "Recognize all tables in the current buffer"]
1034 ["in Region" table-recognize-region
1035 :active (and mark-active (not (eq (mark t) (point))))
1036 :help "Recognize all tables in the current region"]
1037 ["a Table" table-recognize-table
1038 :active (table--probe-cell)
1039 :help "Recognize a table at point"]
1040 ["a Cell" table-recognize-cell
1041 :active (let ((cell (table--probe-cell)))
1042 (and cell (null (table--at-cell-p (car cell)))))
1043 :help "Recognize a cell at point"])
1045 ["in Buffer" table-unrecognize
1047 :help "Unrecognize all tables in the current buffer"]
1048 ["in Region" table-unrecognize-region
1049 :active (and mark-active (not (eq (mark t) (point))))
1050 :help "Unrecognize all tables in the current region"]
1051 ["a Table" table-unrecognize-table
1052 :active (table--probe-cell)
1053 :help "Unrecognize the current table"]
1054 ["a Cell" table-unrecognize-cell
1055 :active (let ((cell (table--probe-cell)))
1056 (and cell (table--at-cell-p (car cell))))
1057 :help "Unrecognize the current cell"])
1059 ["Capture Region" table-capture
1060 :active (and (not buffer-read-only) mark-active (not (eq (mark t) (point))) (not (table--probe-cell)))
1061 :help "Capture text in the current region as a table"]
1062 ["Release" table-release
1063 :active (table--editable-cell-p)
1064 :help "Release the current table as plain text"]))
1066 (defconst table-cell-menu
1069 ["Row" table-insert-row
1070 :active (table--row-column-insertion-point-p)
1071 :help "Insert row(s) of cells in table"]
1072 ["Column" table-insert-column
1073 :active (table--row-column-insertion-point-p 'column)
1074 :help "Insert column(s) of cells in table"])
1076 ["Row" table-delete-row
1077 :active (table--editable-cell-p)
1078 :help "Delete row(s) of cells in table"]
1079 ["Column" table-delete-column
1080 :active (table--editable-cell-p)
1081 :help "Delete column(s) of cells in table"])
1084 ["Horizontally" table-split-cell-horizontally
1085 :active (table--cell-can-split-horizontally-p)
1086 :help "Split the current cell horizontally at point"]
1087 ["Vertically" table-split-cell-vertically
1088 :active (table--cell-can-split-vertically-p)
1089 :help "Split the current cell vertical at point"])
1091 ["Right" (table-span-cell 'right)
1092 :active (table--cell-can-span-p 'right)
1093 :help "Span the current cell into the right cell"]
1094 ["Left" (table-span-cell 'left)
1095 :active (table--cell-can-span-p 'left)
1096 :help "Span the current cell into the left cell"]
1097 ["Above" (table-span-cell 'above)
1098 :active (table--cell-can-span-p 'above)
1099 :help "Span the current cell into the cell above"]
1100 ["Below" (table-span-cell 'below)
1101 :active (table--cell-can-span-p 'below)
1102 :help "Span the current cell into the cell below"])
1105 ["Horizontally" table-narrow-cell
1106 :active (table--editable-cell-p)
1107 :help "Shrink the current cell horizontally"]
1108 ["Vertically" table-shorten-cell
1109 :active (table--editable-cell-p)
1110 :help "Shrink the current cell vertically"])
1112 ["Horizontally" table-widen-cell
1113 :active (table--editable-cell-p)
1114 :help "Expand the current cell horizontally"]
1115 ["Vertically" table-heighten-cell
1116 :active (table--editable-cell-p)
1117 :help "Expand the current cell vertically"])
1121 ["Left" (table-justify-cell 'left)
1122 :active (table--editable-cell-p)
1123 :help "Left justify the contents of the current cell"]
1124 ["Center" (table-justify-cell 'center)
1125 :active (table--editable-cell-p)
1126 :help "Center justify the contents of the current cell"]
1127 ["Right" (table-justify-cell 'right)
1128 :active (table--editable-cell-p)
1129 :help "Right justify the contents of the current cell"]
1131 ["Top" (table-justify-cell 'top)
1132 :active (table--editable-cell-p)
1133 :help "Top align the contents of the current cell"]
1134 ["Middle" (table-justify-cell 'middle)
1135 :active (table--editable-cell-p)
1136 :help "Middle align the contents of the current cell"]
1137 ["Bottom" (table-justify-cell 'bottom)
1138 :active (table--editable-cell-p)
1139 :help "Bottom align the contents of the current cell"]
1140 ["None" (table-justify-cell 'none)
1141 :active (table--editable-cell-p)
1142 :help "Remove vertical alignment from the current cell"])
1144 ["Left" (table-justify-row 'left)
1145 :active (table--editable-cell-p)
1146 :help "Left justify the contents of all cells in the current row"]
1147 ["Center" (table-justify-row 'center)
1148 :active (table--editable-cell-p)
1149 :help "Center justify the contents of all cells in the current row"]
1150 ["Right" (table-justify-row 'right)
1151 :active (table--editable-cell-p)
1152 :help "Right justify the contents of all cells in the current row"]
1154 ["Top" (table-justify-row 'top)
1155 :active (table--editable-cell-p)
1156 :help "Top align the contents of all cells in the current row"]
1157 ["Middle" (table-justify-row 'middle)
1158 :active (table--editable-cell-p)
1159 :help "Middle align the contents of all cells in the current row"]
1160 ["Bottom" (table-justify-row 'bottom)
1161 :active (table--editable-cell-p)
1162 :help "Bottom align the contents of all cells in the current row"]
1163 ["None" (table-justify-cell 'none)
1164 :active (table--editable-cell-p)
1165 :help "Remove vertical alignment from all cells in the current row"])
1167 ["Left" (table-justify-column 'left)
1168 :active (table--editable-cell-p)
1169 :help "Left justify the contents of all cells in the current column"]
1170 ["Center" (table-justify-column 'center)
1171 :active (table--editable-cell-p)
1172 :help "Center justify the contents of all cells in the current column"]
1173 ["Right" (table-justify-column 'right)
1174 :active (table--editable-cell-p)
1175 :help "Right justify the contents of all cells in the current column"]
1177 ["Top" (table-justify-column 'top)
1178 :active (table--editable-cell-p)
1179 :help "Top align the contents of all cells in the current column"]
1180 ["Middle" (table-justify-column 'middle)
1181 :active (table--editable-cell-p)
1182 :help "Middle align the contents of all cells in the current column"]
1183 ["Bottom" (table-justify-column 'bottom)
1184 :active (table--editable-cell-p)
1185 :help "Bottom align the contents of all cells in the current column"]
1186 ["None" (table-justify-cell 'none)
1187 :active (table--editable-cell-p)
1188 :help "Remove vertical alignment from all cells in the current column"])
1190 ["Left" (table-justify-cell 'left t)
1191 :active (table--editable-cell-p)
1192 :help "Left justify the current paragraph"]
1193 ["Center" (table-justify-cell 'center t)
1194 :active (table--editable-cell-p)
1195 :help "Center justify the current paragraph"]
1196 ["Right" (table-justify-cell 'right t)
1197 :active (table--editable-cell-p)
1198 :help "Right justify the current paragraph"]))
1200 ["Query Dimension" table-query-dimension
1201 :active (table--probe-cell)
1202 :help "Get the dimension of the current cell and the current table"]
1203 ["Generate Source" table-generate-source
1204 :active (table--probe-cell)
1205 :help "Generate source of the current table in the specified language"]
1206 ["Insert Sequence" table-insert-sequence
1207 :active (table--editable-cell-p)
1208 :help "Travel cells forward while inserting a specified sequence string in each cell"]
1210 ["a Table" table-unrecognize-table
1211 :active (table--probe-cell)
1212 :help "Unrecognize the current table"]
1213 ["a Cell" table-unrecognize-cell
1214 :active (let ((cell (table--probe-cell)))
1215 (and cell (table--at-cell-p (car cell))))
1216 :help "Unrecognize the current cell"])
1217 ["Release" table-release
1218 :active (table--editable-cell-p)
1219 :help "Release the current table as plain text"]
1220 ("Configure Width to"
1221 ["Auto Expand Mode" (table-fixed-width-mode -1)
1224 :selected (not table-fixed-width-mode)
1225 :help "A mode that allows automatic horizontal cell expansion"]
1226 ["Fixed Width Mode" (table-fixed-width-mode 1)
1229 :selected table-fixed-width-mode
1230 :help "A mode that does not allow automatic horizontal cell expansion"])
1232 ["Forward Cell" table-forward-cell
1233 :active (table--probe-cell)
1234 :help "Move point forward by cell(s)"]
1235 ["Backward Cell" table-backward-cell
1236 :active (table--probe-cell)
1237 :help "Move point backward by cell(s)"])
1240 ;; XEmacs causes an error when encountering unknown keywords in the
1241 ;; menu definition. Specifically the :help keyword is new in Emacs 21
1242 ;; and causes error for the XEmacs function `check-menu-syntax'. IMHO
1243 ;; it is unwise to generate an error for unknown keywords because it
1244 ;; kills the nice backward compatible extensibility of keyword use.
1245 ;; Unknown keywords should be quietly ignore so that future extension
1246 ;; does not cause a problem in the old implementation. Sigh...
1247 (when (featurep 'xemacs)
1249 (defun table--tweak-menu-for-xemacs (menu)
1252 (mapcar 'table--tweak-menu-for-xemacs menu))
1254 (let ((i 0) (len (length menu)))
1256 ;; replace :help with something harmless.
1257 (if (eq (aref menu i) :help) (aset menu i :included))
1258 (setq i (1+ i)))))))
1259 (list table-global-menu table-cell-menu))
1260 (defvar mark-active t))
1262 ;; register table menu under global tools menu
1263 (unless table-disable-menu
1264 (easy-menu-define table-global-menu-map nil "Table global menu" table-global-menu)
1265 (if (featurep 'xemacs)
1267 (easy-menu-add-item nil '("Tools") table-global-menu-map))
1268 (easy-menu-add-item (current-global-map) '("menu-bar" "tools") "--")
1269 (easy-menu-add-item (current-global-map) '("menu-bar" "tools") table-global-menu-map)))
1271 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1276 (defmacro table-with-cache-buffer (&rest body)
1277 "Execute the forms in BODY with table cache buffer as the current buffer.
1278 This macro simplifies the rest of the work greatly by condensing the
1279 common idiom used in many of the cell manipulation functions. It does
1280 not return any meaningful value.
1282 Save the current buffer and set the cache buffer as the current
1283 buffer. Move the point to the cache buffer coordinate
1284 `table-cell-cache-point-coordinate'. After BODY forms are executed,
1285 the paragraph is filled as long as `table-inhibit-auto-fill-paragraph'
1286 remains nil. BODY can set it to t when it does not want to fill the
1287 paragraph. If necessary the cell width and height are extended as the
1288 consequence of cell content modification by the BODY. Then the
1289 current buffer is restored to the original one. The last cache point
1290 coordinate is stored in `table-cell-cache-point-coordinate'. The
1291 original buffer's point is moved to the location that corresponds to
1292 the last cache point coordinate."
1293 (let ((height-expansion (make-symbol "height-expansion-var-symbol"))
1294 (width-expansion (make-symbol "width-expansion-var-symbol")))
1295 `(let (,height-expansion ,width-expansion)
1296 ;; make sure cache has valid data unless it is explicitly inhibited.
1297 (unless table-inhibit-update
1298 (table-recognize-cell))
1299 (with-current-buffer (get-buffer-create table-cache-buffer-name)
1300 ;; goto the cell coordinate based on `table-cell-cache-point-coordinate'.
1301 (set-mark (table--goto-coordinate table-cell-cache-mark-coordinate))
1302 (table--goto-coordinate table-cell-cache-point-coordinate)
1303 (table--untabify-line)
1304 ;; always reset before executing body forms because auto-fill behavior is the default.
1305 (setq table-inhibit-auto-fill-paragraph nil)
1308 ;; fill paragraph unless the body does not want to by setting `table-inhibit-auto-fill-paragraph'.
1309 (unless table-inhibit-auto-fill-paragraph
1310 (if (and table-cell-info-justify
1311 (not (eq table-cell-info-justify 'left)))
1312 (table--fill-region (point-min) (point-max))
1314 (save-excursion (forward-paragraph -1) (point))
1315 (save-excursion (forward-paragraph 1) (point)))))
1316 ;; keep the updated cell coordinate.
1317 (setq table-cell-cache-point-coordinate (table--get-coordinate))
1318 ;; determine the cell width expansion.
1319 (setq ,width-expansion (table--measure-max-width))
1320 (if (<= ,width-expansion table-cell-info-width) nil
1321 (table--fill-region (point-min) (point-max) ,width-expansion)
1322 ;; keep the updated cell coordinate.
1323 (setq table-cell-cache-point-coordinate (table--get-coordinate)))
1324 (setq ,width-expansion (- ,width-expansion table-cell-info-width))
1325 ;; determine the cell height expansion.
1326 (if (looking-at "\\s *\\'") nil
1327 (goto-char (point-min))
1328 (if (re-search-forward "\\(\\s *\\)\\'" nil t)
1329 (goto-char (match-beginning 1))))
1330 (setq ,height-expansion (- (cdr (table--get-coordinate)) (1- table-cell-info-height))))
1331 ;; now back to the table buffer.
1332 ;; expand the cell width in the table buffer if necessary.
1333 (if (> ,width-expansion 0)
1334 (table-widen-cell ,width-expansion 'no-copy 'no-update))
1335 ;; expand the cell height in the table buffer if necessary.
1336 (if (> ,height-expansion 0)
1337 (table-heighten-cell ,height-expansion 'no-copy 'no-update))
1339 (with-current-buffer (get-buffer-create table-cache-buffer-name)
1340 (table--goto-coordinate table-cell-cache-point-coordinate)
1341 (setq table-cell-cache-point-coordinate (table--valign)))
1342 ;; move the point in the table buffer to the location that corresponds to
1343 ;; the location in the cell cache buffer
1344 (table--goto-coordinate (table--transcoord-cache-to-table table-cell-cache-point-coordinate))
1345 ;; set up the update timer unless it is explicitly inhibited.
1346 (unless table-inhibit-update
1347 (table--update-cell)))))
1349 ;; for debugging the body form of the macro
1350 (put 'table-with-cache-buffer 'edebug-form-spec '(body))
1351 ;; for neat presentation use the same indentation as `progn'
1352 (put 'table-with-cache-buffer 'lisp-indent-function 0)
1353 (if (or (featurep 'xemacs)
1354 (null (fboundp 'font-lock-add-keywords))) nil
1355 ;; color it as a keyword
1356 (font-lock-add-keywords
1358 '("\\<table-with-cache-buffer\\>")))
1360 (defmacro table-put-source-info (prop value)
1361 "Register source generation information."
1362 `(put 'table-source-info-plist ,prop ,value))
1364 (defmacro table-get-source-info (prop)
1365 "Retrieve source generation information."
1366 `(get 'table-source-info-plist ,prop))
1368 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1370 ;; Modified commands for cell operation
1373 ;; Point Motion Only Group
1376 (let ((func-symbol (intern (format "*table--cell-%s" command)))
1377 (doc-string (format "Table remapped function for `%s'." command)))
1383 (let ((table-inhibit-update t)
1384 (deactivate-mark nil))
1385 (table--finish-delayed-tasks)
1386 (table-recognize-cell 'force)
1387 (table-with-cache-buffer
1388 (call-interactively ',command)
1389 (setq table-inhibit-auto-fill-paragraph t)))))
1390 (setq table-command-remap-alist
1391 (cons (cons command func-symbol)
1392 table-command-remap-alist))))
1393 '(move-beginning-of-line
1404 backward-paragraph))
1409 (let ((func-symbol (intern (format "*table--cell-%s" command)))
1410 (doc-string (format "Table remapped function for `%s'." command)))
1416 (table--finish-delayed-tasks)
1417 (table-recognize-cell 'force)
1418 (table-with-cache-buffer
1419 (table--remove-cell-properties (point-min) (point-max))
1420 (table--remove-eol-spaces (point-min) (point-max))
1421 (call-interactively ',command))
1422 (table--finish-delayed-tasks)))
1423 (setq table-command-remap-alist
1424 (cons (cons command func-symbol)
1425 table-command-remap-alist))))
1434 backward-kill-sentence
1436 backward-kill-paragraph
1438 backward-kill-sexp))
1443 (let ((func-symbol (intern (format "*table--cell-%s" command)))
1444 (doc-string (format "Table remapped function for `%s'." command)))
1450 (table--finish-delayed-tasks)
1451 (table-recognize-cell 'force)
1452 (table-with-cache-buffer
1453 (call-interactively ',command)
1454 (table--untabify (point-min) (point-max))
1455 (table--fill-region (point-min) (point-max))
1456 (setq table-inhibit-auto-fill-paragraph t))
1457 (table--finish-delayed-tasks)))
1458 (setq table-command-remap-alist
1459 (cons (cons command func-symbol)
1460 table-command-remap-alist))))
1463 yank-clipboard-selection
1469 (let ((func-symbol (intern (format "*table--cell-%s" command)))
1470 (doc-string (format "Table remapped function for `%s'." command)))
1476 (table--finish-delayed-tasks)
1477 (table-recognize-cell 'force)
1478 (table-with-cache-buffer
1479 (let ((fill-column table-cell-info-width))
1480 (call-interactively ',command))
1481 (setq table-inhibit-auto-fill-paragraph t))
1482 (table--finish-delayed-tasks)))
1483 (setq table-command-remap-alist
1484 (cons (cons command func-symbol)
1485 table-command-remap-alist))))
1491 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1497 (defun table-insert (columns rows &optional cell-width cell-height)
1498 "Insert an editable text table.
1499 Insert a table of specified number of COLUMNS and ROWS. Optional
1500 parameter CELL-WIDTH and CELL-HEIGHT can specify the size of each
1501 cell. The cell size is uniform across the table if the specified size
1502 is a number. They can be a list of numbers to specify different size
1503 for each cell. When called interactively, the list of number is
1504 entered by simply listing all the numbers with space characters
1509 \\[table-insert] inserts a table at the current point location.
1511 Suppose we have the following situation where `-!-' indicates the
1516 Type \\[table-insert] and hit ENTER key. As it asks table
1517 specification, provide 3 for number of columns, 1 for number of rows,
1518 5 for cell width and 1 for cell height. Now you shall see the next
1519 table and the point is automatically moved to the beginning of the
1526 Inside a table cell, there are special key bindings. \\<table-cell-map>
1528 M-9 \\[table-widen-cell] (or \\[universal-argument] 9 \\[table-widen-cell]) widens the first cell by 9 character
1529 width, which results as
1531 +--------------+-----+-----+
1533 +--------------+-----+-----+
1535 Type TAB \\[table-widen-cell] then type TAB M-2 M-7 \\[table-widen-cell] (or \\[universal-argument] 2 7 \\[table-widen-cell]). Typing
1536 TAB moves the point forward by a cell. The result now looks like this:
1538 +--------------+------+--------------------------------+
1540 +--------------+------+--------------------------------+
1542 If you knew each width of the columns prior to the table creation,
1543 what you could have done better was to have had given the complete
1544 width information to `table-insert'.
1546 Cell width(s): 14 6 32
1552 This would have eliminated the previously mentioned width adjustment
1555 If the point is in the last cell type S-TAB S-TAB to move it to the
1556 first cell. Now type \\[table-heighten-cell] which heighten the row by a line.
1558 +--------------+------+--------------------------------+
1561 +--------------+------+--------------------------------+
1563 Type \\[table-insert-row-column] and tell it to insert a row.
1565 +--------------+------+--------------------------------+
1568 +--------------+------+--------------------------------+
1571 +--------------+------+--------------------------------+
1573 Move the point under the table as shown below.
1575 +--------------+------+--------------------------------+
1578 +--------------+------+--------------------------------+
1581 +--------------+------+--------------------------------+
1584 Type M-x table-insert-row instead of \\[table-insert-row-column]. \\[table-insert-row-column] does not work
1585 when the point is outside of the table. This insertion at
1586 outside of the table effectively appends a row at the end.
1588 +--------------+------+--------------------------------+
1591 +--------------+------+--------------------------------+
1594 +--------------+------+--------------------------------+
1597 +--------------+------+--------------------------------+
1599 Text editing inside the table cell produces reasonably expected
1602 +--------------+------+--------------------------------+
1605 +--------------+------+--------------------------------+
1606 | | |Text editing inside the table |
1607 | | |cell produces reasonably |
1608 | | |expected results.-!- |
1609 +--------------+------+--------------------------------+
1612 +--------------+------+--------------------------------+
1614 Inside a table cell has a special keymap.
1620 (barf-if-buffer-read-only)
1621 (if (table--probe-cell)
1622 (error "Can't insert a table inside a table"))
1623 (mapcar (function table--read-from-minibuffer)
1624 '(("Number of columns" . table-columns-history)
1625 ("Number of rows" . table-rows-history)
1626 ("Cell width(s)" . table-cell-width-history)
1627 ("Cell height(s)" . table-cell-height-history)))))
1628 (table--make-cell-map)
1629 ;; reform the arguments.
1630 (if (null cell-width) (setq cell-width (car table-cell-width-history)))
1631 (if (null cell-height) (setq cell-height (car table-cell-height-history)))
1632 (if (stringp columns) (setq columns (string-to-number columns)))
1633 (if (stringp rows) (setq rows (string-to-number rows)))
1634 (if (stringp cell-width) (setq cell-width (table--string-to-number-list cell-width)))
1635 (if (stringp cell-height) (setq cell-height (table--string-to-number-list cell-height)))
1636 (if (numberp cell-width) (setq cell-width (cons cell-width nil)))
1637 (if (numberp cell-height) (setq cell-height (cons cell-height nil)))
1638 ;; test validity of the arguments.
1640 (let* ((value (symbol-value arg))
1642 (function (lambda ()
1643 (error "%s must be a positive integer%s" arg
1644 (if (listp value) " or a list of positive integers" ""))))))
1645 (if (null value) (funcall error-handler))
1646 (mapcar (function (lambda (arg1)
1647 (if (or (not (integerp arg1))
1649 (funcall error-handler))))
1650 (if (listp value) value
1651 (cons value nil)))))
1652 '(columns rows cell-width cell-height))
1653 (let ((orig-coord (table--get-coordinate))
1654 (coord (table--get-coordinate))
1655 r i cw ch cell-str border-str)
1656 ;; prefabricate the building blocks border-str and cell-str.
1658 ;; construct border-str
1659 (insert table-cell-intersection-char)
1660 (setq cw cell-width)
1662 (while (< i columns)
1663 (insert (make-string (car cw) (string-to-char table-cell-horizontal-chars)) table-cell-intersection-char)
1664 (if (cdr cw) (setq cw (cdr cw)))
1666 (setq border-str (buffer-substring (point-min) (point-max)))
1667 ;; construct cell-str
1669 (insert table-cell-vertical-char)
1670 (setq cw cell-width)
1672 (while (< i columns)
1673 (let ((beg (point)))
1674 (insert (make-string (car cw) ?\s))
1675 (insert table-cell-vertical-char)
1676 (table--put-cell-line-property beg (1- (point))))
1677 (if (cdr cw) (setq cw (cdr cw)))
1679 (setq cell-str (buffer-substring (point-min) (point-max))))
1680 ;; if the construction site has an empty border push that border down.
1683 (if (looking-at "\\s *$")
1685 (setq border-str (concat border-str "\n"))
1686 (setq cell-str (concat cell-str "\n")))))
1687 ;; now build the table using the prefabricated building blocks
1689 (setq ch cell-height)
1692 (table--goto-coordinate coord) (setcdr coord (1+ (cdr coord)))
1693 (table--untabify-line (point))
1694 (insert border-str))
1696 (while (< i (car ch))
1697 (table--goto-coordinate coord) (setcdr coord (1+ (cdr coord)))
1698 (table--untabify-line (point))
1701 (table--goto-coordinate coord) (setcdr coord (1+ (cdr coord)))
1702 (table--untabify-line (point))
1704 (if (cdr ch) (setq ch (cdr ch)))
1706 ;; stand by at the first cell
1707 (table--goto-coordinate (table--offset-coordinate orig-coord '(1 . 1)))
1708 (table-recognize-cell 'force)))
1711 (defun table-insert-row (n)
1712 "Insert N table row(s).
1713 When point is in a table the newly inserted row(s) are placed above
1714 the current row. When point is outside of the table it must be below
1715 the table within the table width range, then the newly created row(s)
1716 are appended at the bottom of the table."
1718 (if (< n 0) (setq n 1))
1719 (let* ((current-coordinate (table--get-coordinate))
1720 (coord-list (table--cell-list-to-coord-list (table--horizontal-cell-list t nil 'top)))
1721 (append-row (if coord-list nil (setq coord-list (table--find-row-column))))
1722 (cell-height (cdr (table--min-coord-list coord-list)))
1724 (this-list coord-list)
1725 (right-list (cdr coord-list))
1726 (bottom-border-y (1+ (cdr (table--get-coordinate (cdr (table--vertical-cell-list nil t))))))
1727 (vertical-str (string table-cell-vertical-char))
1728 (vertical-str-with-properties (let ((str (string table-cell-vertical-char)))
1729 (table--put-cell-keymap-property 0 (length str) str)
1730 (table--put-cell-rear-nonsticky 0 (length str) str) str))
1732 ;; create the space below for the table to grow
1733 (table--create-growing-space-below (* n (+ 1 cell-height)) coord-list bottom-border-y)
1734 ;; vertically expand each cell from left to right
1736 (let* ((left (prog1 (car left-list) (setq left-list (if left-list (cdr left-list) coord-list))))
1737 (this (prog1 (car this-list) (setq this-list (cdr this-list))))
1738 (right (prog1 (car right-list) (setq right-list (cdr right-list))))
1739 (exclude-left (and left (< (cdar left) (cdar this))))
1740 (exclude-right (and right (<= (cdar right) (cdar this))))
1741 (beg (table--goto-coordinate
1742 (cons (if exclude-left (caar this) (1- (caar this)))
1744 (end (table--goto-coordinate
1745 (cons (if exclude-right (cadr this) (1+ (cadr this)))
1747 (rect (if append-row nil (extract-rectangle beg end))))
1748 ;; prepend blank cell lines to the extracted rectangle
1752 (concat (if exclude-left "" (char-to-string table-cell-intersection-char))
1753 (make-string (- (cadr this) (caar this)) (string-to-char table-cell-horizontal-chars))
1754 (if exclude-right "" (char-to-string table-cell-intersection-char)))
1756 (let ((j cell-height))
1759 (concat (if exclude-left ""
1760 (if first-time vertical-str vertical-str-with-properties))
1761 (table--cell-blank-str (- (cadr this) (caar this)))
1762 (if exclude-right "" vertical-str-with-properties))
1766 (setq first-time nil)
1768 (table--goto-coordinate (cons (if exclude-left (caar this) (1- (caar this)))
1769 (1+ bottom-border-y)))
1770 (delete-rectangle beg end)
1772 (table--insert-rectangle rect)))
1773 ;; fix up the intersections
1774 (setq this-list (if append-row nil coord-list))
1776 (let ((this (prog1 (car this-list) (setq this-list (cdr this-list))))
1779 (let ((y (1- (* i (+ 1 cell-height)))))
1780 (table--goto-coordinate (table--offset-coordinate (car this) (cons -1 y)))
1781 (delete-char 1) (insert table-cell-intersection-char)
1782 (table--goto-coordinate (table--offset-coordinate (cons (cadr this) (cdar this)) (cons 0 y)))
1783 (delete-char 1) (insert table-cell-intersection-char)
1785 ;; move the point to the beginning of the first newly inserted cell.
1786 (if (table--goto-coordinate
1787 (if append-row (cons (car (caar coord-list)) (1+ bottom-border-y))
1788 (caar coord-list))) nil
1789 (table--goto-coordinate current-coordinate))
1790 ;; re-recognize the current cell's new dimension
1791 (table-recognize-cell 'force)))
1794 (defun table-insert-column (n)
1795 "Insert N table column(s).
1796 When point is in a table the newly inserted column(s) are placed left
1797 of the current column. When point is outside of the table it must be
1798 right side of the table within the table height range, then the newly
1799 created column(s) are appended at the right of the table."
1801 (if (< n 0) (setq n 1))
1802 (let* ((current-coordinate (table--get-coordinate))
1803 (coord-list (table--cell-list-to-coord-list (table--vertical-cell-list t nil 'left)))
1804 (append-column (if coord-list nil (setq coord-list (table--find-row-column 'column))))
1805 (cell-width (car (table--min-coord-list coord-list)))
1806 (border-str (table--multiply-string (concat (make-string cell-width (string-to-char table-cell-horizontal-chars))
1807 (char-to-string table-cell-intersection-char)) n))
1808 (cell-str (table--multiply-string (concat (table--cell-blank-str cell-width)
1809 (let ((str (string table-cell-vertical-char)))
1810 (table--put-cell-keymap-property 0 (length str) str)
1811 (table--put-cell-rear-nonsticky 0 (length str) str) str)) n))
1812 (columns-to-extend (* n (+ 1 cell-width)))
1814 (this-list coord-list)
1815 (below-list (cdr coord-list))
1816 (right-border-x (car (table--get-coordinate (cdr (table--horizontal-cell-list nil t))))))
1817 ;; push back the affected area above and below this table
1818 (table--horizontally-shift-above-and-below columns-to-extend coord-list)
1819 ;; process each cell vertically from top to bottom
1821 (let* ((above (prog1 (car above-list) (setq above-list (if above-list (cdr above-list) coord-list))))
1822 (this (prog1 (car this-list) (setq this-list (cdr this-list))))
1823 (below (prog1 (car below-list) (setq below-list (cdr below-list))))
1824 (exclude-above (and above (<= (caar above) (caar this))))
1825 (exclude-below (and below (< (caar below) (caar this))))
1826 (beg-coord (cons (if append-column (1+ right-border-x) (caar this))
1827 (if exclude-above (cdar this) (1- (cdar this)))))
1828 (end-coord (cons (1+ right-border-x)
1829 (if exclude-below (cddr this) (1+ (cddr this)))))
1831 ;; untabify the area right of the bar that is about to be inserted
1832 (let ((coord (table--copy-coordinate beg-coord))
1834 (len (length rect)))
1836 (if (table--goto-coordinate coord 'no-extension)
1837 (table--untabify-line (point)))
1838 (setcdr coord (1+ (cdr coord)))
1840 ;; extract and delete the rectangle area including the current
1841 ;; cell and to the right border of the table.
1842 (setq rect (extract-rectangle (table--goto-coordinate beg-coord)
1843 (table--goto-coordinate end-coord)))
1844 (delete-rectangle (table--goto-coordinate beg-coord)
1845 (table--goto-coordinate end-coord))
1846 ;; prepend the empty column string at the beginning of each
1847 ;; rectangle string extracted before.
1848 (let ((rect-str rect)
1851 (if (and first (null exclude-above))
1852 (setcar rect-str (concat border-str (car rect-str)))
1853 (if (and (null (cdr rect-str)) (null exclude-below))
1854 (setcar rect-str (concat border-str (car rect-str)))
1855 (setcar rect-str (concat cell-str (car rect-str)))))
1857 (setq rect-str (cdr rect-str))))
1858 ;; insert the extended rectangle
1859 (table--goto-coordinate beg-coord)
1860 (table--insert-rectangle rect)))
1861 ;; fix up the intersections
1862 (setq this-list (if append-column nil coord-list))
1864 (let ((this (prog1 (car this-list) (setq this-list (cdr this-list))))
1867 (let ((x (1- (* (1+ i) (+ 1 cell-width)))))
1868 (table--goto-coordinate (table--offset-coordinate (car this) (cons x -1)))
1869 (delete-char 1) (insert table-cell-intersection-char)
1870 (table--goto-coordinate (table--offset-coordinate (cons (caar this) (cddr this)) (cons x 1)))
1871 (delete-char 1) (insert table-cell-intersection-char)
1873 ;; move the point to the beginning of the first newly inserted cell.
1874 (if (table--goto-coordinate
1876 (cons (1+ right-border-x)
1877 (cdar (car coord-list)))
1878 (caar coord-list))) nil
1879 (table--goto-coordinate current-coordinate))
1880 ;; re-recognize the current cell's new dimension
1881 (table-recognize-cell 'force)))
1884 (defun table-insert-row-column (row-column n)
1885 "Insert row(s) or column(s).
1886 See `table-insert-row' and `table-insert-column'."
1888 (let ((n (prefix-numeric-value current-prefix-arg)))
1889 (if (< n 0) (setq n 1))
1890 (list (intern (let ((completion-ignore-case t)
1891 (default (car table-insert-row-column-history)))
1892 (downcase (completing-read
1893 (format "Insert %s row%s/column%s (default %s): "
1894 (if (> n 1) (format "%d" n) "a")
1898 '(("row") ("column"))
1899 nil t nil 'table-insert-row-column-history default))))
1901 (cond ((eq row-column 'row)
1902 (table-insert-row n))
1903 ((eq row-column 'column)
1904 (table-insert-column n))))
1907 (defun table-recognize (&optional arg)
1908 "Recognize all tables within the current buffer and activate them.
1909 Scans the entire buffer and recognizes valid table cells. If the
1910 optional numeric prefix argument ARG is negative the tables in the
1911 buffer become inactive, meaning the tables become plain text and loses
1912 all the table specific features."
1914 (setq arg (prefix-numeric-value arg))
1915 (let* ((inhibit-read-only t))
1916 (table-recognize-region (point-min) (point-max) -1)
1919 (goto-char (point-min))
1920 (let* ((border (format "[%s%c%c]"
1921 table-cell-horizontal-chars
1922 table-cell-vertical-char
1923 table-cell-intersection-char))
1924 (border3 (concat border border border))
1925 (non-border (format "^[^%s%c%c]*$"
1926 table-cell-horizontal-chars
1927 table-cell-vertical-char
1928 table-cell-intersection-char)))
1929 ;; `table-recognize-region' is an expensive function so minimize
1930 ;; the search area. A minimum table at least consists of three consecutive
1931 ;; table border characters to begin with such as
1935 ;; and any tables end with a line containing no table border characters
1936 ;; or the end of buffer.
1937 (while (and (re-search-forward border3 (point-max) t)
1938 (not (and (input-pending-p)
1939 table-abort-recognition-when-input-pending)))
1940 (message "Recognizing tables...(%d%%)" (/ (* 100 (match-beginning 0)) (- (point-max) (point-min))))
1941 (let ((beg (match-beginning 0))
1943 (if (re-search-forward non-border (point-max) t)
1944 (setq end (match-beginning 0))
1945 (setq end (goto-char (point-max))))
1946 (table-recognize-region beg end arg)))
1947 (message "Recognizing tables...done"))))))
1950 (defun table-unrecognize ()
1952 (table-recognize -1))
1955 (defun table-recognize-region (beg end &optional arg)
1956 "Recognize all tables within region.
1957 BEG and END specify the region to work on. If the optional numeric
1958 prefix argument ARG is negative the tables in the region become
1959 inactive, meaning the tables become plain text and lose all the table
1961 (interactive "r\nP")
1962 (setq arg (prefix-numeric-value arg))
1963 (let ((inhibit-read-only t)
1964 (modified-flag (buffer-modified-p)))
1966 (table--remove-cell-properties beg end)
1969 (let* ((border (format "[%s%c%c]"
1970 table-cell-horizontal-chars
1971 table-cell-vertical-char
1972 table-cell-intersection-char))
1973 (non-border (format "[^%s%c%c]"
1974 table-cell-horizontal-chars
1975 table-cell-vertical-char
1976 table-cell-intersection-char))
1977 (inhibit-read-only t))
1980 (remove-text-properties beg end '(table-cell nil))
1981 (while (and (< (point) end)
1982 (not (and (input-pending-p)
1983 table-abort-recognition-when-input-pending)))
1987 ((looking-at border)
1988 (if (re-search-forward non-border end t)
1989 (goto-char (match-beginning 0))
1991 ((table--at-cell-p (point))
1992 (goto-char (next-single-property-change (point) 'table-cell nil end)))
1994 (let ((cell (table-recognize-cell 'force 'no-copy)))
1995 (if (and cell table-detect-cell-alignment)
1996 (table--detect-cell-alignment cell)))
1997 (unless (re-search-forward border end t)
1998 (goto-char end))))))))))
1999 (restore-buffer-modified-p modified-flag)))
2002 (defun table-unrecognize-region (beg end)
2004 (table-recognize-region beg end -1))
2007 (defun table-recognize-table (&optional arg)
2008 "Recognize a table at point.
2009 If the optional numeric prefix argument ARG is negative the table
2010 becomes inactive, meaning the table becomes plain text and loses all
2011 the table specific features."
2013 (setq arg (prefix-numeric-value arg))
2014 (let ((unrecognize (< arg 0))
2015 (origin-cell (table--probe-cell))
2016 (inhibit-read-only t))
2021 (table-forward-cell 1 nil unrecognize)
2022 (let ((cell (table--probe-cell)))
2023 (if (and cell table-detect-cell-alignment)
2024 (table--detect-cell-alignment cell))
2025 (and cell (not (equal cell origin-cell))))))))))
2028 (defun table-unrecognize-table ()
2030 (table-recognize-table -1))
2033 (defun table-recognize-cell (&optional force no-copy arg)
2034 "Recognize a table cell that contains current point.
2035 Probe the cell dimension and prepare the cell information. The
2036 optional two arguments FORCE and NO-COPY are for internal use only and
2037 must not be specified. When the optional numeric prefix argument ARG
2038 is negative the cell becomes inactive, meaning that the cell becomes
2039 plain text and loses all the table specific features."
2040 (interactive "i\ni\np")
2041 (table--make-cell-map)
2042 (if (or force (not (memq (table--get-last-command) table-command-list)))
2043 (let* ((cell (table--probe-cell (called-interactively-p 'interactive)))
2044 (cache-buffer (get-buffer-create table-cache-buffer-name))
2045 (modified-flag (buffer-modified-p))
2046 (inhibit-read-only t))
2049 ;; initialize the cell info variables
2050 (let ((lu-coordinate (table--get-coordinate (car cell)))
2051 (rb-coordinate (table--get-coordinate (cdr cell))))
2052 ;; update the previous cell if this cell is different from the previous one.
2053 ;; care only lu but ignore rb since size change does not matter.
2054 (unless (equal table-cell-info-lu-coordinate lu-coordinate)
2055 (table--finish-delayed-tasks))
2056 (setq table-cell-info-lu-coordinate lu-coordinate)
2057 (setq table-cell-info-rb-coordinate rb-coordinate)
2058 (setq table-cell-info-width (- (car table-cell-info-rb-coordinate)
2059 (car table-cell-info-lu-coordinate)))
2060 (setq table-cell-info-height (+ (- (cdr table-cell-info-rb-coordinate)
2061 (cdr table-cell-info-lu-coordinate)) 1))
2062 (setq table-cell-info-justify (table--get-cell-justify-property cell))
2063 (setq table-cell-info-valign (table--get-cell-valign-property cell)))
2064 ;; set/remove table cell properties
2065 (if (< (prefix-numeric-value arg) 0)
2066 (let ((coord (table--get-coordinate (car cell)))
2067 (n table-cell-info-height))
2070 (table--remove-cell-properties
2071 (table--goto-coordinate coord)
2072 (table--goto-coordinate (cons (+ (car coord) table-cell-info-width 1) (cdr coord))))
2074 (setcdr coord (1+ (cdr coord))))))
2075 (table--put-cell-property cell))
2076 ;; copy the cell contents to the cache buffer
2077 ;; only if no-copy is nil and timers are not set
2079 (setq table-cell-cache-point-coordinate (table--transcoord-table-to-cache))
2080 (setq table-cell-cache-mark-coordinate (table--transcoord-table-to-cache
2081 (table--get-coordinate (marker-position (mark-marker)))))
2082 (setq table-cell-buffer (current-buffer))
2083 (let ((rectangle (extract-rectangle (car cell)
2085 (save-current-buffer
2086 (set-buffer cache-buffer)
2088 (table--insert-rectangle rectangle)))))
2089 (restore-buffer-modified-p modified-flag))
2090 (if (featurep 'xemacs)
2091 (table--warn-incompatibility))
2095 (defun table-unrecognize-cell ()
2097 (table-recognize-cell nil nil -1))
2100 (defun table-heighten-cell (n &optional no-copy no-update)
2101 "Heighten the current cell by N lines by expanding the cell vertically.
2102 Heightening is done by adding blank lines at the bottom of the current
2103 cell. Other cells aligned horizontally with the current one are also
2104 heightened in order to keep the rectangular table structure. The
2105 optional argument NO-COPY is internal use only and must not be
2108 (if (< n 0) (setq n 1))
2109 (let* ((coord-list (table--cell-list-to-coord-list (table--horizontal-cell-list t)))
2111 (this-list coord-list)
2112 (right-list (cdr coord-list))
2113 (bottom-border-y (1+ (cdr (table--get-coordinate (cdr (table--vertical-cell-list nil t))))))
2114 (vertical-str (string table-cell-vertical-char))
2115 (vertical-str-with-properties (string table-cell-vertical-char))
2117 (current-coordinate (table--get-coordinate)))
2118 ;; prepare the right vertical string with appropriate properties put
2119 (table--put-cell-keymap-property 0 (length vertical-str-with-properties) vertical-str-with-properties)
2120 ;; create the space below for the table to grow
2121 (table--create-growing-space-below n coord-list bottom-border-y)
2122 ;; vertically expand each cell from left to right
2124 (let* ((left (prog1 (car left-list) (setq left-list (if left-list (cdr left-list) coord-list))))
2125 (this (prog1 (car this-list) (setq this-list (cdr this-list))))
2126 (right (prog1 (car right-list) (setq right-list (cdr right-list))))
2127 (exclude-left (and left (< (cddr left) (cddr this))))
2128 (exclude-right (and right (<= (cddr right) (cddr this))))
2129 (beg (table--goto-coordinate
2130 (cons (if exclude-left (caar this) (1- (caar this)))
2132 (end (table--goto-coordinate
2133 (cons (if exclude-right (cadr this) (1+ (cadr this)))
2135 (rect (extract-rectangle beg end)))
2136 ;; prepend blank cell lines to the extracted rectangle
2140 (concat (if exclude-left ""
2141 (if first-time vertical-str vertical-str-with-properties))
2142 (table--cell-blank-str (- (cadr this) (caar this)))
2143 (if exclude-right "" vertical-str-with-properties))
2146 (setq first-time nil)
2147 (delete-rectangle beg end)
2149 (table--insert-rectangle rect)))
2150 (table--goto-coordinate current-coordinate)
2151 ;; re-recognize the current cell's new dimension
2152 (table-recognize-cell 'force no-copy)
2154 (table--update-cell-heightened))))
2157 (defun table-shorten-cell (n)
2158 "Shorten the current cell by N lines by shrinking the cell vertically.
2159 Shortening is done by removing blank lines from the bottom of the cell
2160 and possibly from the top of the cell as well. Therefor, the cell
2161 must have some bottom/top blank lines to be shorten effectively. This
2162 is applicable to all the cells aligned horizontally with the current
2163 one because they are also shortened in order to keep the rectangular
2166 (if (< n 0) (setq n 1))
2167 (table--finish-delayed-tasks)
2168 (let* ((table-inhibit-update t)
2169 (coord-list (table--cell-list-to-coord-list (table--horizontal-cell-list t)))
2171 (this-list coord-list)
2172 (right-list (cdr coord-list))
2173 (bottom-budget-list nil)
2174 (bottom-border-y (1+ (cdr (table--get-coordinate (cdr (table--vertical-cell-list nil t))))))
2175 (current-coordinate (table--get-coordinate))
2176 (current-cell-coordinate (table--cell-to-coord (table--probe-cell)))
2177 (blank-line-regexp "\\s *$"))
2178 (message "Shortening...");; this operation may be lengthy
2179 ;; for each cell calculate the maximum number of blank lines we can delete
2180 ;; and adjust the argument n. n is adjusted so that the total number of
2181 ;; blank lines from top and bottom of a cell do not exceed n, all cell has
2182 ;; at least one line height after blank line deletion.
2184 (let ((this (prog1 (car this-list) (setq this-list (cdr this-list)))))
2185 (table--goto-coordinate (car this))
2186 (table-recognize-cell 'force)
2187 (table-with-cache-buffer
2189 (let ((blank-line-count 0))
2190 (table--goto-coordinate (cons 0 (1- table-cell-info-height)))
2192 (while (and (looking-at blank-line-regexp)
2193 (setq blank-line-count (1+ blank-line-count))
2194 ;; need to leave at least one blank line
2195 (if (> blank-line-count n) (throw 'end-count nil) t)
2196 (if (zerop (forward-line -1)) t
2197 (setq n (if (zerop blank-line-count) 0
2198 (1- blank-line-count)))
2199 (throw 'end-count nil))))
2200 (table--goto-coordinate (cons 0 0))
2202 (while (and (looking-at blank-line-regexp)
2203 (setq blank-line-count (1+ blank-line-count))
2204 ;; can consume all blank lines
2205 (if (>= blank-line-count n) (throw 'end-count nil) t)
2206 (zerop (forward-line 1))))
2207 (setq n blank-line-count))))))
2208 ;; construct the bottom-budget-list which is a list of numbers where each number
2209 ;; corresponds to how many lines to be deleted from the bottom of each cell. If
2210 ;; this number, say bb, is smaller than n (bb < n) that means the difference (n - bb)
2211 ;; number of lines must be deleted from the top of the cell in addition to deleting
2212 ;; bb lines from the bottom of the cell.
2213 (setq this-list coord-list)
2215 (let ((this (prog1 (car this-list) (setq this-list (cdr this-list)))))
2216 (table--goto-coordinate (car this))
2217 (table-recognize-cell 'force)
2218 (table-with-cache-buffer
2219 (setq bottom-budget-list
2221 (let ((blank-line-count 0))
2222 (table--goto-coordinate (cons 0 (1- table-cell-info-height)))
2223 (while (and (looking-at blank-line-regexp)
2224 (< blank-line-count n)
2225 (setq blank-line-count (1+ blank-line-count))
2226 (zerop (forward-line -1))))
2228 bottom-budget-list)))))
2229 (setq bottom-budget-list (nreverse bottom-budget-list))
2230 ;; vertically shorten each cell from left to right
2231 (setq this-list coord-list)
2233 (let* ((left (prog1 (car left-list) (setq left-list (if left-list (cdr left-list) coord-list))))
2234 (this (prog1 (car this-list) (setq this-list (cdr this-list))))
2235 (right (prog1 (car right-list) (setq right-list (cdr right-list))))
2236 (bottom-budget (prog1 (car bottom-budget-list) (setq bottom-budget-list (cdr bottom-budget-list))))
2237 (exclude-left (and left (< (cddr left) (cddr this))))
2238 (exclude-right (and right (<= (cddr right) (cddr this))))
2239 (beg (table--goto-coordinate (cons (caar this) (cdar this))))
2240 (end (table--goto-coordinate (cons (cadr this) bottom-border-y)))
2241 (rect (extract-rectangle beg end))
2242 (height (+ (- (cddr this) (cdar this)) 1))
2243 (blank-line (make-string (- (cadr this) (caar this)) ?\s)))
2244 ;; delete lines from the bottom of the cell
2245 (setcdr (nthcdr (- height bottom-budget 1) rect) (nthcdr height rect))
2246 ;; delete lines from the top of the cell
2247 (if (> n bottom-budget)
2248 (let ((props (text-properties-at 0 (car rect))))
2249 (setq rect (nthcdr (- n bottom-budget) rect))
2250 (set-text-properties 0 1 props (car rect))))
2251 ;; append blank lines below the table
2252 (setq rect (append rect (make-list n blank-line)))
2253 ;; now swap the area with the prepared rect of the same size
2254 (delete-rectangle beg end)
2256 (table--insert-rectangle rect)
2257 ;; for the left and right borders always delete lines from the bottom of the cell
2258 (unless exclude-left
2259 (let* ((beg (table--goto-coordinate (cons (1- (caar this)) (cdar this))))
2260 (end (table--goto-coordinate (cons (caar this) bottom-border-y)))
2261 (rect (extract-rectangle beg end)))
2262 (setcdr (nthcdr (- height n 1) rect) (nthcdr height rect))
2263 (setq rect (append rect (make-list n " ")))
2264 (delete-rectangle beg end)
2266 (table--insert-rectangle rect)))
2267 (unless exclude-right
2268 (let* ((beg (table--goto-coordinate (cons (cadr this) (cdar this))))
2269 (end (table--goto-coordinate (cons (1+ (cadr this)) bottom-border-y)))
2270 (rect (extract-rectangle beg end)))
2271 (setcdr (nthcdr (- height n 1) rect) (nthcdr height rect))
2272 (setq rect (append rect (make-list n " ")))
2273 (delete-rectangle beg end)
2275 (table--insert-rectangle rect)))
2276 ;; if this is the cell where the original point was in, adjust the point location
2277 (if (null (equal this current-cell-coordinate)) nil
2278 (let ((y (- (cdr current-coordinate) (cdar this))))
2279 (if (< y (- n bottom-budget))
2280 (setcdr current-coordinate (cdar this))
2281 (if (< (- y (- n bottom-budget)) (- height n))
2282 (setcdr current-coordinate (+ (cdar this) (- y (- n bottom-budget))))
2283 (setcdr current-coordinate (+ (cdar this) (- height n 1)))))))))
2284 ;; remove the appended blank lines below the table if they are unnecessary
2285 (table--goto-coordinate (cons 0 (1+ (- bottom-border-y n))))
2286 (table--remove-blank-lines n)
2287 ;; re-recognize the current cell's new dimension
2288 (table--goto-coordinate current-coordinate)
2289 (table-recognize-cell 'force)
2290 (table--update-cell-heightened)
2294 (defun table-widen-cell (n &optional no-copy no-update)
2295 "Widen the current cell by N columns and expand the cell horizontally.
2296 Some other cells in the same table are widen as well to keep the
2297 table's rectangle structure."
2299 (if (< n 0) (setq n 1))
2300 (let* ((coord-list (table--cell-list-to-coord-list (table--vertical-cell-list)))
2302 (this-list coord-list)
2303 (above-list (cdr coord-list)))
2305 ;; push back the affected area above and below this table
2306 (table--horizontally-shift-above-and-below n (reverse coord-list))
2307 ;; now widen vertically for each cell
2309 (let* ((below (prog1 (car below-list) (setq below-list (if below-list (cdr below-list) coord-list))))
2310 (this (prog1 (car this-list) (setq this-list (cdr this-list))))
2311 (above (prog1 (car above-list) (setq above-list (cdr above-list))))
2312 (beg (table--goto-coordinate
2313 (cons (car (cdr this))
2314 (if (or (null above) (<= (car (cdr this)) (car (cdr above))))
2315 (1- (cdr (car this)))
2316 (cdr (car this))))))
2317 (end (table--goto-coordinate
2318 (cons (1+ (car (cdr this)))
2319 (if (or (null below) (< (car (cdr this)) (car (cdr below))))
2320 (1+ (cdr (cdr this)))
2321 (cdr (cdr this))))))
2322 (tmp (extract-rectangle (1- beg) end))
2323 (border (format "[%s%c]\\%c"
2324 table-cell-horizontal-chars
2325 table-cell-intersection-char
2326 table-cell-intersection-char))
2327 (blank (table--cell-blank-str))
2329 ;; create a single wide vertical bar of empty cell fragment
2331 ; (message "tmp is %s" tmp)
2332 (setq rectangle (cons
2333 (if (string-match border (car tmp))
2334 (substring (car tmp) 0 1)
2337 ; (message "rectangle is %s" rectangle)
2338 (setq tmp (cdr tmp)))
2339 (setq rectangle (nreverse rectangle))
2340 ;; untabify the area right of the bar that is about to be inserted
2341 (let ((coord (table--get-coordinate beg))
2343 (len (length rectangle)))
2345 (if (table--goto-coordinate coord 'no-extension)
2346 (table--untabify-line (point)))
2347 (setcdr coord (1+ (cdr coord)))
2349 ;; insert the bar n times
2354 (table--insert-rectangle rectangle))
2355 (setq i (1+ i)))))))
2356 (table-recognize-cell 'force no-copy)
2358 (table--update-cell-widened))))
2361 (defun table-narrow-cell (n)
2362 "Narrow the current cell by N columns and shrink the cell horizontally.
2363 Some other cells in the same table are narrowed as well to keep the
2364 table's rectangle structure."
2366 (if (< n 0) (setq n 1))
2367 (table--finish-delayed-tasks)
2368 (let* ((coord-list (table--cell-list-to-coord-list (table--vertical-cell-list)))
2369 (current-cell (table--cell-to-coord (table--probe-cell)))
2370 (current-coordinate (table--get-coordinate))
2372 (message "Narrowing...");; this operation may be lengthy
2373 ;; determine the doable n by try narrowing each cell.
2374 (setq tmp-list coord-list)
2376 (let ((cell (prog1 (car tmp-list) (setq tmp-list (cdr tmp-list))))
2377 (table-inhibit-update t)
2379 (table--goto-coordinate (car cell))
2380 (table-recognize-cell 'force)
2381 (table-with-cache-buffer
2382 (table--fill-region (point-min) (point-max) (- table-cell-info-width n))
2383 (if (< (setq cell-n (- table-cell-info-width (table--measure-max-width))) n)
2386 (setq table-inhibit-auto-fill-paragraph t))))
2388 ;; narrow only the contents of each cell but leave the cell frame as is because
2389 ;; we need to have valid frame structure in order for table-with-cache-buffer
2390 ;; to work correctly.
2391 (setq tmp-list coord-list)
2393 (let* ((cell (prog1 (car tmp-list) (setq tmp-list (cdr tmp-list))))
2394 (table-inhibit-update t)
2395 (currentp (equal cell current-cell))
2397 (if currentp (table--goto-coordinate current-coordinate)
2398 (table--goto-coordinate (car cell)))
2399 (table-recognize-cell 'force)
2400 (setq old-height table-cell-info-height)
2401 (table-with-cache-buffer
2402 (let ((out-of-bound (>= (- (car current-coordinate) (car table-cell-info-lu-coordinate))
2403 (- table-cell-info-width n)))
2404 (sticky (and currentp
2406 (unless (bolp) (forward-char -1))
2407 (looking-at ".*\\S ")))))
2408 (table--fill-region (point-min) (point-max) (- table-cell-info-width n))
2409 (if (or sticky (and currentp (looking-at ".*\\S ")))
2410 (setq current-coordinate (table--transcoord-cache-to-table))
2411 (if out-of-bound (setcar current-coordinate
2412 (+ (car table-cell-info-lu-coordinate) (- table-cell-info-width n 1))))))
2413 (setq table-inhibit-auto-fill-paragraph t))
2414 (table--update-cell 'now)
2415 ;; if this cell heightens and pushes the current cell below, move
2416 ;; the current-coordinate (point location) down accordingly.
2417 (if currentp (setq current-coordinate (table--get-coordinate))
2418 (if (and (> table-cell-info-height old-height)
2419 (> (cdr current-coordinate) (cdr table-cell-info-lu-coordinate)))
2420 (setcdr current-coordinate (+ (cdr current-coordinate)
2421 (- table-cell-info-height old-height)))))
2423 ;; coord-list is now possibly invalid since some cells may have already
2424 ;; been heightened so recompute them by table--vertical-cell-list.
2425 (table--goto-coordinate current-coordinate)
2426 (setq coord-list (table--cell-list-to-coord-list (table--vertical-cell-list)))
2427 ;; push in the affected area above and below this table so that things
2428 ;; on the right side of the table are shifted horizontally neatly.
2429 (table--horizontally-shift-above-and-below (- n) (reverse coord-list))
2430 ;; finally narrow the frames for each cell.
2431 (let* ((below-list nil)
2432 (this-list coord-list)
2433 (above-list (cdr coord-list)))
2435 (let* ((below (prog1 (car below-list) (setq below-list (if below-list (cdr below-list) coord-list))))
2436 (this (prog1 (car this-list) (setq this-list (cdr this-list))))
2437 (above (prog1 (car above-list) (setq above-list (cdr above-list)))))
2439 (table--goto-coordinate
2440 (cons (- (cadr this) n)
2441 (if (or (null above) (<= (cadr this) (cadr above)))
2444 (table--goto-coordinate
2446 (if (or (null below) (< (cadr this) (cadr below)))
2449 (table--goto-coordinate current-coordinate)
2450 ;; re-recognize the current cell's new dimension
2451 (table-recognize-cell 'force)
2455 (defun table-forward-cell (&optional arg no-recognize unrecognize)
2456 "Move point forward to the beginning of the next cell.
2457 With argument ARG, do it ARG times;
2458 a negative argument ARG = -N means move backward N cells.
2459 Do not specify NO-RECOGNIZE and UNRECOGNIZE. They are for internal use only.
2461 Sample Cell Traveling Order (In Irregular Table Cases)
2463 You can actually try how it works in this buffer. Press
2464 \\[table-recognize] and go to cells in the following tables and press
2465 \\[table-forward-cell] or TAB key.
2467 +-----+--+ +--+-----+ +--+--+--+ +--+--+--+ +---------+ +--+---+--+
2468 |0 |1 | |0 |1 | |0 |1 |2 | |0 |1 |2 | |0 | |0 |1 |2 |
2469 +--+--+ | | +--+--+ +--+ | | | | +--+ +----+----+ +--+-+-+--+
2470 |2 |3 | | | |2 |3 | |3 +--+ | | +--+3 | |1 |2 | |3 |4 |
2471 | +--+--+ +--+--+ | +--+4 | | | |4 +--+ +--+-+-+--+ +----+----+
2472 | |4 | |4 | | |5 | | | | | |5 | |3 |4 |5 | |5 |
2473 +--+-----+ +-----+--+ +--+--+--+ +--+--+--+ +--+---+--+ +---------+
2475 +--+--+--+ +--+--+--+ +--+--+--+ +--+--+--+
2476 |0 |1 |2 | |0 |1 |2 | |0 |1 |2 | |0 |1 |2 |
2477 | | | | | +--+ | | | | | +--+ +--+
2478 +--+ +--+ +--+3 +--+ | +--+ | |3 +--+4 |
2479 |3 | |4 | |4 +--+5 | | |3 | | +--+5 +--+
2480 | | | | | |6 | | | | | | |6 | |7 |
2481 +--+--+--+ +--+--+--+ +--+--+--+ +--+--+--+
2483 +--+--+--+ +--+--+--+ +--+--+--+--+ +--+-----+--+ +--+--+--+--+
2484 |0 |1 |2 | |0 |1 |2 | |0 |1 |2 |3 | |0 |1 |2 | |0 |1 |2 |3 |
2485 | +--+ | | +--+ | | +--+--+ | | | | | | +--+--+ |
2486 | |3 +--+ +--+3 | | +--+4 +--+ +--+ +--+ +--+4 +--+
2487 +--+ |4 | |4 | +--+ |5 +--+--+6 | |3 +--+--+4 | |5 | |6 |
2488 |5 +--+ | | +--+5 | | |7 |8 | | | |5 |6 | | | | | |
2489 | |6 | | | |6 | | +--+--+--+--+ +--+--+--+--+ +--+-----+--+
2490 +--+--+--+ +--+--+--+
2492 ;; After modifying this function, test against the above tables in
2493 ;; the doc string. It is quite tricky. The tables above do not
2494 ;; mean to cover every possible cases of cell layout, of course.
2495 ;; They are examples of tricky cases from implementation point of
2496 ;; view and provided for simple regression test purpose.
2498 (or arg (setq arg 1))
2499 (table--finish-delayed-tasks)
2500 (while (null (zerop arg))
2501 (let* ((pivot (table--probe-cell 'abort-on-error))
2502 (cell pivot) edge tip)
2503 ;; go to the beginning of the first right/left cell with same height if exists
2504 (while (and (setq cell (table--goto-coordinate
2505 (cons (if (> arg 0) (1+ (car (table--get-coordinate (cdr cell))))
2506 (1- (car (table--get-coordinate (car cell)))))
2507 (cdr (table--get-coordinate (car pivot)))) 'no-extension))
2508 (setq cell (table--probe-cell))
2509 (/= (cdr (table--get-coordinate (car cell)))
2510 (cdr (table--get-coordinate (car pivot))))))
2511 (if cell (goto-char (car cell)) ; done
2512 ;; if the horizontal move fails search the most left/right edge cell below/above the pivot
2513 ;; but first find the edge cell
2515 (while (and (table--goto-coordinate
2516 (cons (if (> arg 0) (1- (car (table--get-coordinate (car edge))))
2517 (1+ (car (table--get-coordinate (cdr edge)))))
2518 (cdr (table--get-coordinate (car pivot)))) 'no-extension)
2519 (setq cell (table--probe-cell))
2521 (setq cell (if (> arg 0) edge
2522 (or (and (table--goto-coordinate
2523 (cons (car (table--get-coordinate (cdr edge)))
2524 (1- (cdr (table--get-coordinate (car edge))))))
2525 (table--probe-cell))
2527 ;; now search for the tip which is the highest/lowest below/above cell
2530 (and (table--goto-coordinate
2531 (cons (car (table--get-coordinate (if (> arg 0) (car cell)
2533 (if (> arg 0) (+ 2 (cdr (table--get-coordinate (cdr cell))))
2534 (1- (cdr (table--get-coordinate (car pivot)))))) 'no-extension)
2535 (setq below/above (table--probe-cell))
2538 (< (cdr (table--get-coordinate (car below/above)))
2539 (cdr (table--get-coordinate (car tip))))
2540 (> (cdr (table--get-coordinate (car below/above)))
2541 (cdr (table--get-coordinate (car tip))))))
2542 (setq tip below/above)))
2543 (and (setq cell (table--goto-coordinate
2544 (cons (if (> arg 0) (1+ (car (table--get-coordinate (cdr cell))))
2545 (1- (car (table--get-coordinate (car cell)))))
2546 (if (> arg 0) (cdr (table--get-coordinate (car pivot)))
2547 (1- (cdr (table--get-coordinate (car pivot)))))) 'no-extension))
2548 (setq cell (table--probe-cell))))
2549 (if tip (goto-char (car tip)) ; done
2550 ;; let's climb up/down to the top/bottom from the edge
2551 (while (and (table--goto-coordinate
2552 (cons (if (> arg 0) (car (table--get-coordinate (car edge)))
2553 (car (table--get-coordinate (cdr edge))))
2554 (if (> arg 0) (1- (cdr (table--get-coordinate (car edge))))
2555 (+ 2 (cdr (table--get-coordinate (cdr edge)))))) 'no-extension)
2556 (setq cell (table--probe-cell))
2561 (while (and (table--goto-coordinate
2562 (cons (1- (car (table--get-coordinate (car cell))))
2563 (cdr (table--get-coordinate (cdr cell)))) 'no-extension)
2564 (setq cell (table--probe-cell)))
2565 (if (> (cdr (table--get-coordinate (car cell)))
2566 (cdr (table--get-coordinate (car edge))))
2567 (setq edge cell)))))
2568 (goto-char (car edge))))) ; the top left cell
2569 (setq arg (if (> arg 0) (1- arg) (1+ arg))))
2570 (unless no-recognize
2571 (table-recognize-cell 'force nil (if unrecognize -1 nil)))) ; refill the cache with new cell contents
2574 (defun table-backward-cell (&optional arg)
2575 "Move backward to the beginning of the previous cell.
2576 With argument ARG, do it ARG times;
2577 a negative argument ARG = -N means move forward N cells."
2579 (or arg (setq arg 1))
2580 (table-forward-cell (- arg)))
2583 (defun table-span-cell (direction)
2584 "Span current cell into adjacent cell in DIRECTION.
2585 DIRECTION is one of symbols; right, left, above or below."
2588 (let* ((dummy (barf-if-buffer-read-only))
2590 (let* ((tmp (delete nil
2592 (if (table--cell-can-span-p d)
2593 (list (symbol-name d))))
2594 '(right left above below)))))
2596 (error "Can't span this cell"))
2598 (default-direction (if (member (list (car table-cell-span-direction-history)) direction-list)
2599 (car table-cell-span-direction-history)
2600 (caar direction-list)))
2601 (completion-ignore-case t))
2602 (intern (downcase (completing-read
2603 (format "Span into (default %s): " default-direction)
2605 nil t nil 'table-cell-span-direction-history default-direction))))))
2606 (unless (memq direction '(right left above below))
2607 (error "Invalid direction %s, must be right, left, above or below"
2608 (symbol-name direction)))
2609 (table-recognize-cell 'force)
2610 (unless (table--cell-can-span-p direction)
2611 (error "Can't span %s" (symbol-name direction)))
2612 ;; prepare beginning and ending positions of the border bar to strike through
2614 ((eq direction 'right)
2616 (table--goto-coordinate
2617 (cons (car table-cell-info-rb-coordinate)
2618 (1- (cdr table-cell-info-lu-coordinate))) 'no-extension)))
2619 ((eq direction 'below)
2621 (table--goto-coordinate
2622 (cons (1- (car table-cell-info-lu-coordinate))
2623 (1+ (cdr table-cell-info-rb-coordinate))) 'no-extension)))
2626 (table--goto-coordinate
2627 (cons (1- (car table-cell-info-lu-coordinate))
2628 (1- (cdr table-cell-info-lu-coordinate))) 'no-extension)))))
2630 ((eq direction 'left)
2632 (table--goto-coordinate
2633 (cons (car table-cell-info-lu-coordinate)
2634 (1+ (cdr table-cell-info-rb-coordinate))) 'no-extension)))
2635 ((eq direction 'above)
2637 (table--goto-coordinate
2638 (cons (1+ (car table-cell-info-rb-coordinate))
2639 (1- (cdr table-cell-info-lu-coordinate))) 'no-extension)))
2642 (table--goto-coordinate
2643 (cons (1+ (car table-cell-info-rb-coordinate))
2644 (1+ (cdr table-cell-info-rb-coordinate))) 'no-extension))))))
2645 ;; replace the bar with blank space while taking care of edges to be border or intersection
2648 (if (memq direction '(left right))
2649 (let* ((column (current-column))
2651 (n-element (- (length (extract-rectangle beg end)) 2))
2652 (above-contp (and (goto-char beg)
2653 (zerop (forward-line -1))
2654 (= (move-to-column column) column)
2655 (looking-at (regexp-quote (char-to-string table-cell-vertical-char)))))
2656 (below-contp (and (goto-char end)
2657 (progn (forward-char -1) t)
2658 (zerop (forward-line 1))
2659 (= (move-to-column column) column)
2660 (looking-at (regexp-quote (char-to-string table-cell-vertical-char))))))
2662 (cons (if below-contp
2663 (char-to-string table-cell-intersection-char)
2664 (substring table-cell-horizontal-chars 0 1))
2666 (while (> n-element 0)
2667 (setq rectangle (cons (table--cell-blank-str 1) rectangle))
2668 (setq n-element (1- n-element)))
2670 (cons (if above-contp
2671 (char-to-string table-cell-intersection-char)
2672 (substring table-cell-horizontal-chars 0 1))
2674 (delete-rectangle beg end)
2676 (table--insert-rectangle rectangle))
2677 (delete-region beg end)
2678 (insert (if (and (> (point) (point-min))
2681 (looking-at (regexp-opt-charset
2682 (string-to-list table-cell-horizontal-chars)))))
2683 table-cell-intersection-char
2684 table-cell-vertical-char)
2685 (table--cell-blank-str (- end beg 2))
2686 (if (looking-at (regexp-opt-charset
2687 (string-to-list table-cell-horizontal-chars)))
2688 table-cell-intersection-char
2689 table-cell-vertical-char))))
2690 ;; recognize the newly created spanned cell
2691 (table-recognize-cell 'force)
2692 (if (member direction '(right left))
2693 (table-with-cache-buffer
2694 (table--fill-region (point-min) (point-max))
2695 (setq table-inhibit-auto-fill-paragraph t)))))
2698 (defun table-split-cell-vertically ()
2699 "Split current cell vertically.
2700 Creates a cell above and a cell below the current point location."
2702 (table-recognize-cell 'force)
2703 (let ((point-y (cdr (table--get-coordinate))))
2704 (unless (table--cell-can-split-vertically-p)
2705 (error "Can't split here"))
2706 (let* ((old-coordinate (table--get-coordinate))
2707 (column (current-column))
2708 (beg (table--goto-coordinate
2709 (cons (1- (car table-cell-info-lu-coordinate))
2711 (end (table--goto-coordinate
2712 (cons (1+ (car table-cell-info-rb-coordinate))
2714 (line (buffer-substring (1+ beg) (1- end))))
2715 (when (= (cdr old-coordinate) (cdr table-cell-info-rb-coordinate))
2716 (table--goto-coordinate old-coordinate)
2717 (table-heighten-cell 1 'no-copy 'no-update))
2719 (delete-region beg end)
2720 (insert table-cell-intersection-char
2721 (make-string table-cell-info-width (string-to-char table-cell-horizontal-chars))
2722 table-cell-intersection-char)
2723 (table--goto-coordinate old-coordinate)
2725 (move-to-column column)
2726 (setq old-coordinate (table--get-coordinate))
2727 (table-recognize-cell 'force)
2728 (unless (string-match "^\\s *$" line)
2729 (table-with-cache-buffer
2730 (goto-char (point-min))
2732 (goto-char (point-min)) ;; don't heighten cell unnecessarily
2733 (setq table-inhibit-auto-fill-paragraph t)))
2734 (table--update-cell 'now) ;; can't defer this operation
2735 (table--goto-coordinate old-coordinate)
2736 (move-to-column column)
2737 (table-recognize-cell 'force))))
2740 (defun table-split-cell-horizontally ()
2741 "Split current cell horizontally.
2742 Creates a cell on the left and a cell on the right of the current point location."
2744 (table-recognize-cell 'force)
2745 (let* ((o-coordinate (table--get-coordinate))
2746 (point-x (car o-coordinate))
2747 cell-empty cell-contents cell-coordinate
2748 contents-to beg end rectangle strip-rect
2749 (right-edge (= (car o-coordinate) (1- (car table-cell-info-rb-coordinate)))))
2750 (unless (table--cell-can-split-horizontally-p)
2751 (error "Can't split here"))
2752 (let ((table-inhibit-update t))
2753 (table-with-cache-buffer
2754 (setq cell-coordinate (table--get-coordinate))
2756 (goto-char (point-min))
2757 (setq cell-empty (null (re-search-forward "\\S " nil t))))
2758 (setq cell-contents (buffer-substring (point-min) (point-max)))
2759 (setq table-inhibit-auto-fill-paragraph t)))
2761 (if cell-empty 'left
2762 (let* ((completion-ignore-case t)
2763 (default (car table-cell-split-contents-to-history)))
2765 (if (member 'click (event-modifiers last-input-event))
2766 (x-popup-menu last-input-event
2767 '("Existing cell contents to:"
2769 ("Split" . "split") ("Left" . "left") ("Right" . "right"))))
2770 (downcase (completing-read
2771 (format "Existing cell contents to (default %s): " default)
2772 '(("split") ("left") ("right"))
2773 nil t nil 'table-cell-split-contents-to-history default)))))))
2774 (unless (eq contents-to 'split)
2775 (table-with-cache-buffer
2777 (setq table-inhibit-auto-fill-paragraph t)))
2778 (table--update-cell 'now)
2779 (setq beg (table--goto-coordinate
2781 (1- (cdr table-cell-info-lu-coordinate)))))
2782 (setq end (table--goto-coordinate
2784 (1+ (cdr table-cell-info-rb-coordinate)))))
2785 (setq rectangle (cons (char-to-string table-cell-intersection-char) nil))
2786 (let ((n table-cell-info-height))
2787 (while (prog1 (> n 0) (setq n (1- n)))
2788 (setq rectangle (cons (char-to-string table-cell-vertical-char) rectangle))))
2789 (setq rectangle (cons (char-to-string table-cell-intersection-char) rectangle))
2790 (if (eq contents-to 'split)
2791 (setq strip-rect (extract-rectangle beg end)))
2792 (delete-rectangle beg end)
2794 (table--insert-rectangle rectangle)
2795 (table--goto-coordinate o-coordinate)
2800 (table-widen-cell 1)))
2801 (unless (eq contents-to 'left)
2803 (table-recognize-cell 'force)
2804 (table-with-cache-buffer
2805 (if (eq contents-to 'split)
2806 ;; split inserts strip-rect after removing
2807 ;; top and bottom borders
2808 (let ((o-coord (table--get-coordinate))
2809 (l (setq strip-rect (cdr strip-rect))))
2810 (while (cddr l) (setq l (cdr l)))
2812 ;; insert the strip only when it is not a completely blank one
2813 (unless (let ((cl (mapcar (lambda (s) (string= s " ")) strip-rect)))
2815 (table--uniform-list-p cl)))
2816 (goto-char (point-min))
2817 (table--insert-rectangle strip-rect)
2818 (table--goto-coordinate o-coord)))
2819 ;; left or right inserts original contents
2821 (insert cell-contents)
2822 (table--goto-coordinate cell-coordinate)
2823 (table--fill-region (point-min) (point-max))
2824 ;; avoid unnecessary vertical cell expansion
2825 (and (looking-at "\\s *\\'")
2826 (re-search-backward "\\S \\(\\s *\\)\\=" nil t)
2827 (goto-char (match-beginning 1))))
2828 ;; in either case do not fill paragraph
2829 (setq table-inhibit-auto-fill-paragraph t))
2830 (table--update-cell 'now)) ;; can't defer this operation
2831 (table-recognize-cell 'force)))
2834 (defun table-split-cell (orientation)
2835 "Split current cell in ORIENTATION.
2836 ORIENTATION is a symbol either horizontally or vertically."
2839 (let* ((dummy (barf-if-buffer-read-only))
2840 (completion-ignore-case t)
2841 (default (car table-cell-split-orientation-history)))
2842 (intern (downcase (completing-read
2843 (format "Split orientation (default %s): " default)
2844 '(("horizontally") ("vertically"))
2845 nil t nil 'table-cell-split-orientation-history default))))))
2846 (unless (memq orientation '(horizontally vertically))
2847 (error "Invalid orientation %s, must be horizontally or vertically"
2848 (symbol-name orientation)))
2849 (if (eq orientation 'horizontally)
2850 (table-split-cell-horizontally)
2851 (table-split-cell-vertically)))
2854 (defun table-justify (what justify)
2855 "Justify contents of a cell, a row of cells or a column of cells.
2856 WHAT is a symbol 'cell, 'row or 'column. JUSTIFY is a symbol 'left,
2857 'center, 'right, 'top, 'middle, 'bottom or 'none."
2859 (list (let* ((dummy (barf-if-buffer-read-only))
2860 (completion-ignore-case t)
2861 (default (car table-target-history)))
2862 (intern (downcase (completing-read
2863 (format "Justify what (default %s): " default)
2864 '(("cell") ("row") ("column"))
2865 nil t nil 'table-target-history default))))
2866 (table--query-justification)))
2867 (funcall (intern (concat "table-justify-" (symbol-name what))) justify))
2870 (defun table-justify-cell (justify &optional paragraph)
2871 "Justify cell contents.
2872 JUSTIFY is a symbol 'left, 'center or 'right for horizontal, or 'top,
2873 'middle, 'bottom or 'none for vertical. When optional PARAGRAPH is
2874 non-nil the justify operation is limited to the current paragraph,
2875 otherwise the entire cell contents is justified."
2877 (list (table--query-justification)))
2878 (table--finish-delayed-tasks)
2879 (table-recognize-cell 'force)
2880 (table--justify-cell-contents justify paragraph))
2883 (defun table-justify-row (justify)
2884 "Justify cells of a row.
2885 JUSTIFY is a symbol 'left, 'center or 'right for horizontal, or top,
2886 'middle, 'bottom or 'none for vertical."
2888 (list (table--query-justification)))
2889 (let((cell-list (table--horizontal-cell-list nil nil 'top)))
2890 (table--finish-delayed-tasks)
2893 (let ((cell (car cell-list)))
2894 (setq cell-list (cdr cell-list))
2895 (goto-char (car cell))
2896 (table-recognize-cell 'force)
2897 (table--justify-cell-contents justify))))))
2900 (defun table-justify-column (justify)
2901 "Justify cells of a column.
2902 JUSTIFY is a symbol 'left, 'center or 'right for horizontal, or top,
2903 'middle, 'bottom or 'none for vertical."
2905 (list (table--query-justification)))
2906 (let((cell-list (table--vertical-cell-list nil nil 'left)))
2907 (table--finish-delayed-tasks)
2910 (let ((cell (car cell-list)))
2911 (setq cell-list (cdr cell-list))
2912 (goto-char (car cell))
2913 (table-recognize-cell 'force)
2914 (table--justify-cell-contents justify))))))
2917 (defun table-fixed-width-mode (&optional arg)
2918 "Toggle fixing width mode.
2919 In the fixed width mode, typing inside a cell never changes the cell
2920 width where in the normal mode the cell width expands automatically in
2921 order to prevent a word being folded into multiple lines."
2923 (table--finish-delayed-tasks)
2924 (setq table-fixed-width-mode
2926 (not table-fixed-width-mode)
2927 (> (prefix-numeric-value arg) 0)))
2928 (table--update-cell-face))
2931 (defun table-query-dimension (&optional where)
2932 "Return the dimension of the current cell and the current table.
2933 The result is a list (cw ch tw th c r cells) where cw is the cell
2934 width, ch is the cell height, tw is the table width, th is the table
2935 height, c is the number of columns, r is the number of rows and cells
2936 is the total number of cells. The cell dimension excludes the cell
2937 frame while the table dimension includes the table frame. The columns
2938 and the rows are counted by the number of cell boundaries. Therefore
2939 the number tends to be larger than it appears for the tables with
2940 non-uniform cell structure (heavily spanned and split). When optional
2941 WHERE is provided the cell and table at that location is reported."
2944 (if where (goto-char where))
2945 (let ((starting-cell (table--probe-cell))
2946 cell table-lu table-rb col-list row-list (cells 0))
2947 (if (null starting-cell) nil
2948 (setq table-lu (car starting-cell))
2949 (setq table-rb (cdr starting-cell))
2950 (setq col-list (cons (car (table--get-coordinate (car starting-cell))) nil))
2951 (setq row-list (cons (cdr (table--get-coordinate (car starting-cell))) nil))
2952 (and (called-interactively-p 'interactive)
2953 (message "Computing cell dimension..."))
2956 (table-forward-cell 1 t)
2957 (setq cells (1+ cells))
2958 (and (setq cell (table--probe-cell))
2959 (not (equal cell starting-cell))))
2960 (if (< (car cell) table-lu)
2961 (setq table-lu (car cell)))
2962 (if (> (cdr cell) table-rb)
2963 (setq table-rb (cdr cell)))
2964 (let ((lu-coordinate (table--get-coordinate (car cell))))
2965 (if (memq (car lu-coordinate) col-list) nil
2966 (setq col-list (cons (car lu-coordinate) col-list)))
2967 (if (memq (cdr lu-coordinate) row-list) nil
2968 (setq row-list (cons (cdr lu-coordinate) row-list)))))
2969 (let* ((cell-lu-coordinate (table--get-coordinate (car starting-cell)))
2970 (cell-rb-coordinate (table--get-coordinate (cdr starting-cell)))
2971 (table-lu-coordinate (table--get-coordinate table-lu))
2972 (table-rb-coordinate (table--get-coordinate table-rb))
2973 (cw (- (car cell-rb-coordinate) (car cell-lu-coordinate)))
2974 (ch (1+ (- (cdr cell-rb-coordinate) (cdr cell-lu-coordinate))))
2975 (tw (+ 2 (- (car table-rb-coordinate) (car table-lu-coordinate))))
2976 (th (+ 3 (- (cdr table-rb-coordinate) (cdr table-lu-coordinate))))
2977 (c (length col-list))
2978 (r (length row-list)))
2979 (and (called-interactively-p 'interactive)
2980 (message "Cell: (%dw, %dh), Table: (%dw, %dh), Dim: (%dc, %dr), Total Cells: %d" cw ch tw th c r cells))
2981 (list cw ch tw th c r cells))))))
2984 (defun table-generate-source (language &optional dest-buffer caption)
2985 "Generate source of the current table in the specified language.
2986 LANGUAGE is a symbol that specifies the language to describe the
2987 structure of the table. It must be either 'html, 'latex or 'cals.
2988 The resulted source text is inserted into DEST-BUFFER and the buffer
2989 object is returned. When DEST-BUFFER is omitted or nil the default
2990 buffer specified in `table-dest-buffer-name' is used. In this case
2991 the content of the default buffer is erased prior to the generation.
2992 When DEST-BUFFER is non-nil it is expected to be either a destination
2993 buffer or a name of the destination buffer. In this case the
2994 generated result is inserted at the current point in the destination
2995 buffer and the previously existing contents in the buffer are
2998 References used for this implementation:
3001 URL `http://www.w3.org'
3004 URL `http://www.maths.tcd.ie/~dwilkins/LaTeXPrimer/Tables.html'
3007 URL `http://www.oasis-open.org/html/a502.htm'
3008 URL `http://www.oreilly.com/catalog/docbook/chapter/book/table.html#AEN114751'
3011 (let* ((dummy (unless (table--probe-cell) (error "Table not found here")))
3012 (completion-ignore-case t)
3013 (default (car table-source-language-history))
3014 (language (downcase (completing-read
3015 (format "Language (default %s): " default)
3016 (mapcar (lambda (s) (list (symbol-name s)))
3017 table-source-languages)
3018 nil t nil 'table-source-language-history default))))
3021 (read-buffer "Destination buffer: " (concat table-dest-buffer-name "." language))
3022 (table--read-from-minibuffer '("Table Caption" . table-source-caption-history)))))
3023 (let ((default-buffer-name (concat table-dest-buffer-name "." (symbol-name language))))
3024 (unless (or (called-interactively-p 'interactive) (table--probe-cell))
3025 (error "Table not found here"))
3026 (unless (bufferp dest-buffer)
3027 (setq dest-buffer (get-buffer-create (or dest-buffer default-buffer-name))))
3028 (if (string= (buffer-name dest-buffer) default-buffer-name)
3029 (with-current-buffer dest-buffer
3032 (let ((starting-cell (table--probe-cell))
3033 cell origin-cell tail-cell col-list row-list (n 0) i)
3034 ;; first analyze the table structure and prepare:
3035 ;; 1. origin cell (left up corner cell)
3036 ;; 2. tail cell (right bottom corner cell)
3037 ;; 3. column boundary list
3038 ;; 4. row boundary list
3039 (setq origin-cell starting-cell)
3040 (setq tail-cell starting-cell)
3041 (setq col-list (cons (car (table--get-coordinate (car starting-cell))) nil))
3042 (setq row-list (cons (cdr (table--get-coordinate (car starting-cell))) nil))
3044 (let ((wheel [?- ?\\ ?| ?/]))
3047 (if (called-interactively-p 'interactive)
3049 (message "Analyzing table...%c" (aref wheel i))
3050 (if (eq (setq i (1+ i)) (length wheel))
3053 (table-forward-cell 1 t)
3054 (and (setq cell (table--probe-cell))
3055 (not (equal cell starting-cell))))
3056 (if (< (car cell) (car origin-cell))
3057 (setq origin-cell cell))
3058 (if (> (cdr cell) (cdr tail-cell))
3059 (setq tail-cell cell))
3060 (let ((lu-coordinate (table--get-coordinate (car cell))))
3061 (unless (memq (car lu-coordinate) col-list)
3062 (setq col-list (cons (car lu-coordinate) col-list)))
3063 (unless (memq (cdr lu-coordinate) row-list)
3064 (setq row-list (cons (cdr lu-coordinate) row-list))))))
3065 (setq col-list (sort col-list '<))
3066 (setq row-list (sort row-list '<))
3067 (message "Generating source...")
3068 ;; clear the source generation property list
3069 (setplist 'table-source-info-plist nil)
3070 ;; prepare to start from the origin cell
3071 (goto-char (car origin-cell))
3072 ;; first put some header information
3073 (table--generate-source-prologue dest-buffer language caption col-list row-list)
3075 ((eq language 'latex)
3076 ;; scan by character lines
3077 (table--generate-source-scan-lines dest-buffer language origin-cell tail-cell col-list row-list))
3079 ;; scan by table cells
3080 (table--generate-source-scan-rows dest-buffer language origin-cell col-list row-list)))
3082 (table--generate-source-epilogue dest-buffer language col-list row-list))
3083 ;; lastly do some convenience work
3084 (if (called-interactively-p 'interactive)
3085 (save-selected-window
3086 (pop-to-buffer dest-buffer t)
3087 (goto-char (point-min))
3088 (and (string= (buffer-name dest-buffer) default-buffer-name)
3089 (buffer-file-name dest-buffer)
3091 (message "Generating source...done")
3093 (if (memq language '(cals)) 'sgml-mode
3094 (intern (concat (symbol-name language) "-mode")))))
3096 (call-interactively mode)))
3100 (defun table--generate-source-prologue (dest-buffer language caption col-list row-list)
3101 "Generate and insert source prologue into DEST-BUFFER."
3102 (with-current-buffer dest-buffer
3104 ((eq language 'html)
3105 (insert (format "<!-- This HTML table template is generated by emacs %s -->\n" emacs-version)
3106 (format "<table %s>\n" table-html-table-attribute)
3107 (if (and (stringp caption)
3108 (not (string= caption "")))
3109 (format " <caption>%s</caption>\n" caption)
3111 ((eq language 'latex)
3112 (insert (format "%% This LaTeX table template is generated by emacs %s\n" emacs-version)
3113 "\\begin{tabular}{|" (apply 'concat (make-list (length col-list) "l|")) "}\n"
3115 ((eq language 'cals)
3116 (insert (format "<!-- This CALS table template is generated by emacs %s -->\n" emacs-version)
3117 "<table frame=\"all\">\n")
3118 (if (and (stringp caption)
3119 (not (string= caption "")))
3120 (insert " <title>" caption "</title>\n"))
3121 (insert (format " <tgroup cols=\"%d\" align=\"left\" colsep=\"1\" rowsep=\"1\">\n" (length col-list)))
3122 (table-put-source-info 'colspec-marker (point-marker))
3123 (table-put-source-info 'row-type (if (zerop table-cals-thead-rows) "tbody" "thead"))
3124 (set-marker-insertion-type (table-get-source-info 'colspec-marker) nil) ;; insert after
3125 (insert (format " <%s valign=\"top\">\n" (table-get-source-info 'row-type))))
3128 (defun table--generate-source-epilogue (dest-buffer language col-list row-list)
3129 "Generate and insert source epilogue into DEST-BUFFER."
3130 (with-current-buffer dest-buffer
3132 ((eq language 'html)
3133 (insert "</table>\n"))
3134 ((eq language 'latex)
3135 (insert "\\end{tabular}\n"))
3136 ((eq language 'cals)
3137 (set-marker-insertion-type (table-get-source-info 'colspec-marker) t) ;; insert before
3139 (goto-char (table-get-source-info 'colspec-marker))
3142 (insert (format " <colspec colnum=\"%d\" colname=\"c%d\"/>\n" col col)))
3143 (sort (table-get-source-info 'colnum-list) '<)))
3144 (insert (format " </%s>\n </tgroup>\n</table>\n" (table-get-source-info 'row-type))))
3147 (defun table--generate-source-scan-rows (dest-buffer language origin-cell col-list row-list)
3148 "Generate and insert source rows into DEST-BUFFER."
3149 (table-put-source-info 'current-row 1)
3151 (with-current-buffer dest-buffer
3153 ((eq language 'html)
3155 ((eq language 'cals)
3156 (insert " <row>\n"))
3158 (table--generate-source-cells-in-a-row dest-buffer language col-list row-list)
3159 (with-current-buffer dest-buffer
3161 ((eq language 'html)
3162 (insert " </tr>\n"))
3163 ((eq language 'cals)
3164 (insert " </row>\n")
3165 (unless (/= (table-get-source-info 'current-row) table-cals-thead-rows)
3166 (insert (format " </%s>\n" (table-get-source-info 'row-type)))
3167 (insert (format " <%s valign=\"top\">\n" (table-put-source-info 'row-type "tbody")))))))
3168 (table-put-source-info 'current-row (1+ (table-get-source-info 'current-row)))
3169 (setq row-list (cdr row-list))))
3171 (defun table--generate-source-cells-in-a-row (dest-buffer language col-list row-list)
3172 "Generate and insert source cells into DEST-BUFFER."
3173 (table-put-source-info 'current-column 1)
3175 (let* ((cell (table--probe-cell))
3176 (lu (table--get-coordinate (car cell)))
3177 (rb (table--get-coordinate (cdr cell)))
3178 (alignment (table--get-cell-justify-property cell))
3179 (valign (table--get-cell-valign-property cell))
3183 (if (< (car lu) (car col-list))
3185 (while (and col-list
3186 (> (car lu) (car col-list)))
3187 (setq col-list (cdr col-list))
3188 (table-put-source-info 'current-column (1+ (table-get-source-info 'current-column))))
3189 (setq col-list (cdr col-list))
3190 (table-put-source-info 'next-column (1+ (table-get-source-info 'current-column)))
3191 (while (and col-list
3192 (> (1+ (car rb)) (car col-list)))
3193 (setq colspan (1+ colspan))
3194 (setq col-list (cdr col-list))
3195 (table-put-source-info 'next-column (1+ (table-get-source-info 'next-column))))
3196 (setq row-list (cdr row-list))
3197 (while (and row-list
3198 (> (+ (cdr rb) 2) (car row-list)))
3199 (setq rowspan (1+ rowspan))
3200 (setq row-list (cdr row-list)))
3201 (with-current-buffer dest-buffer
3203 ((eq language 'html)
3204 (insert (format " <%s"
3205 (table-put-source-info
3207 (if (or (<= (table-get-source-info 'current-row) table-html-th-rows)
3208 (<= (table-get-source-info 'current-column) table-html-th-columns))
3210 (if (and table-html-cell-attribute (not (string= table-html-cell-attribute "")))
3211 (insert " " table-html-cell-attribute))
3212 (if (> colspan 1) (insert (format " colspan=\"%d\"" colspan)))
3213 (if (> rowspan 1) (insert (format " rowspan=\"%d\"" rowspan)))
3214 (insert (format " align=\"%s\"" (if alignment (symbol-name alignment) "left")))
3215 (insert (format " valign=\"%s\"" (if valign (symbol-name valign) "top")))
3217 ((eq language 'cals)
3220 (let ((scol (table-get-source-info 'current-column))
3221 (ecol (+ (table-get-source-info 'current-column) colspan -1)))
3223 (unless (memq col (table-get-source-info 'colnum-list))
3224 (table-put-source-info 'colnum-list
3225 (cons col (table-get-source-info 'colnum-list)))))
3227 (insert (format " namest=\"c%d\" nameend=\"c%d\"" scol ecol))))
3228 (if (> rowspan 1) (insert (format " morerows=\"%d\"" (1- rowspan))))
3230 (not (memq alignment '(left none))))
3231 (insert " align=\"" (symbol-name alignment) "\""))
3233 (not (memq valign '(top none))))
3234 (insert " valign=\"" (symbol-name valign) "\""))
3237 (table--generate-source-cell-contents dest-buffer language cell)
3238 (with-current-buffer dest-buffer
3240 ((eq language 'html)
3241 (insert (format" </%s>\n" (table-get-source-info 'cell-type))))
3242 ((eq language 'cals)
3243 (insert " </entry>\n"))
3245 (table-forward-cell 1 t)
3246 (table-put-source-info 'current-column (table-get-source-info 'next-column))
3249 (defun table--generate-source-cell-contents (dest-buffer language cell)
3250 "Generate and insert source cell contents of a CELL into DEST-BUFFER."
3251 (let ((cell-contents (extract-rectangle (car cell) (cdr cell))))
3253 (table--insert-rectangle cell-contents)
3254 (table--remove-cell-properties (point-min) (point-max))
3255 (goto-char (point-min))
3257 ((eq language 'html)
3258 (if table-html-delegate-spacing-to-user-agent
3260 (table--remove-eol-spaces (point-min) (point-max))
3261 (if (re-search-forward "\\s +\\'" nil t)
3262 (replace-match "")))
3263 (while (search-forward " " nil t)
3264 (replace-match " "))
3265 (goto-char (point-min))
3266 (while (and (re-search-forward "$" nil t)
3270 (unless (and table-html-delegate-spacing-to-user-agent
3272 (goto-char (point-min))
3273 (looking-at "\\s *\\'")))))
3274 ((eq language 'cals)
3275 (table--remove-eol-spaces (point-min) (point-max))
3276 (if (re-search-forward "\\s +\\'" nil t)
3277 (replace-match "")))
3279 (setq cell-contents (buffer-substring (point-min) (point-max))))
3280 (with-current-buffer dest-buffer
3281 (let ((beg (point)))
3282 (insert cell-contents)
3283 (indent-rigidly beg (point)
3285 ((eq language 'html) 6)
3286 ((eq language 'cals) 10)))
3289 (defun table--cell-horizontal-char-p (c)
3290 "Test if character C is one of the horizontal characters"
3291 (memq c (string-to-list table-cell-horizontal-chars)))
3293 (defun table--generate-source-scan-lines (dest-buffer language origin-cell tail-cell col-list row-list)
3294 "Scan the table line by line.
3295 Currently this method is for LaTeX only."
3296 (let* ((lu-coord (table--get-coordinate (car origin-cell)))
3297 (rb-coord (table--get-coordinate (cdr tail-cell)))
3301 (y1 (cdr rb-coord)))
3303 (let* ((border-p (memq (1+ y) row-list))
3306 (if border-p (char-after (table--goto-coordinate (cons x y)))
3307 (char-before (table--goto-coordinate (cons x y)))))
3311 ;; horizontal cell border processing
3312 (if (and (table--cell-horizontal-char-p (car border-char-list))
3313 (table--uniform-list-p border-char-list))
3314 (with-current-buffer dest-buffer
3315 (insert "\\hline\n"))
3317 (while (setq c (nth i border-char-list))
3318 (if (and start (not (table--cell-horizontal-char-p c)))
3320 (with-current-buffer dest-buffer
3321 (insert (format "\\cline{%d-%d}\n" (1+ start) i)))
3323 (if (and (not start) (table--cell-horizontal-char-p c))
3327 (with-current-buffer dest-buffer
3328 (insert (format "\\cline{%d-%d}\n" (1+ start) i)))))
3329 ;; horizontal cell contents processing
3330 (let* ((span 1) ;; spanning length
3331 (first-p t) ;; first in a row
3332 (insert-column ;; a function that processes one column/multicolumn
3335 (let ((line (table--buffer-substring-and-trim
3336 (table--goto-coordinate (cons from y))
3337 (table--goto-coordinate (cons to y)))))
3338 ;; escape special characters
3341 (goto-char (point-min))
3342 (while (re-search-forward "\\([#$~_^%{}]\\)\\|\\(\\\\\\)\\|\\([<>|]\\)" nil t)
3343 (if (match-beginning 1)
3345 (goto-char (match-beginning 1))
3347 (if (match-beginning 2)
3348 (replace-match "$\\backslash$" t t)
3349 (replace-match (concat "$" (match-string 3) "$")) t t)))
3350 (setq line (buffer-substring (point-min) (point-max))))
3351 ;; insert a column separator and column/multicolumn contents
3352 (with-current-buffer dest-buffer
3354 (insert (if (eq (char-before) ?\s) "" " ") "& "))
3356 (insert (format "\\multicolumn{%d}{%sl|}{%s}" span (if first-p "|" "") line))
3360 (setq start (nth i col-list)))))))
3363 (while (setq c (nth i border-char-list))
3364 (if (eq c table-cell-vertical-char)
3365 (funcall insert-column start (1- (nth i col-list)))
3366 (setq span (1+ span)))
3368 (funcall insert-column start x1))
3369 (with-current-buffer dest-buffer
3370 (insert (if (eq (char-before) ?\s) "" " ") "\\\\\n"))))
3372 (with-current-buffer dest-buffer
3373 (insert "\\hline\n"))
3377 (defun table-insert-sequence (str n increment interval justify)
3378 "Travel cells forward while inserting a specified sequence string in each cell.
3379 STR is the base string from which the sequence starts. When STR is an
3380 empty string then each cell content is erased. When STR ends with
3381 numerical characters (they may optionally be surrounded by a pair of
3382 parentheses) they are incremented as a decimal number. Otherwise the
3383 last character in STR is incremented in ASCII code order. N is the
3384 number of sequence elements to insert. When N is negative the cell
3385 traveling direction is backward. When N is zero it travels forward
3386 entire table. INCREMENT is the increment between adjacent sequence
3387 elements and can be a negative number for effectively decrementing.
3388 INTERVAL is the number of cells to travel between sequence element
3389 insertion which is normally 1. When zero or less is given for
3390 INTERVAL it is interpreted as number of cells per row so that sequence
3391 is placed straight down vertically as long as the table's cell
3392 structure is uniform. JUSTIFY is one of the symbol 'left, 'center or
3393 'right, that specifies justification of the inserted string.
3398 (table-insert 16 3 5 1)
3399 (table-forward-cell 15)
3400 (table-insert-sequence \"D0\" -16 1 1 'center)
3401 (table-forward-cell 16)
3402 (table-insert-sequence \"A[0]\" -16 1 1 'center)
3403 (table-forward-cell 1)
3404 (table-insert-sequence \"-\" 16 0 1 'center))
3407 (table-insert 16 8 5 1)
3408 (table-insert-sequence \"@\" 0 1 2 'right)
3409 (table-forward-cell 1)
3410 (table-insert-sequence \"64\" 0 1 2 'left))
3414 (barf-if-buffer-read-only)
3415 (unless (table--probe-cell) (error "Table not found here"))
3416 (list (read-from-minibuffer
3417 "Sequence base string: " (car table-sequence-string-history) nil nil 'table-sequence-string-history)
3419 (table--read-from-minibuffer
3420 '("How many elements (0: maximum, negative: backward traveling)" . table-sequence-count-history)))
3422 (table--read-from-minibuffer
3423 '("Increment element by" . table-sequence-increment-history)))
3425 (table--read-from-minibuffer
3426 '("Cell interval (0: vertical, 1:horizontal)" . table-sequence-interval-history)))
3427 (let* ((completion-ignore-case t)
3428 (default (car table-sequence-justify-history)))
3429 (intern (downcase (completing-read
3430 (format "Justify (default %s): " default)
3431 '(("left") ("center") ("right"))
3432 nil t nil 'table-sequence-justify-history default)))))))
3433 (unless (or (called-interactively-p 'interactive) (table--probe-cell))
3434 (error "Table not found here"))
3435 (string-match "\\([0-9]*\\)\\([]})>]*\\)\\'" str)
3436 (if (called-interactively-p 'interactive)
3437 (message "Sequencing..."))
3438 (let* ((prefix (substring str 0 (match-beginning 1)))
3439 (index (match-string 1 str))
3440 (fmt (format "%%%s%dd" (if (eq (string-to-char index) ?0) "0" "") (length index)))
3441 (postfix (match-string 2 str))
3442 (dim (table-query-dimension))
3444 (direction (if (< n 0) -1 1))
3446 (if (string= index "")
3449 (if (string= prefix "")
3451 (setq index (string-to-number index)))
3452 (if (< n 0) (setq n (- n)))
3453 (if (or (zerop n) (> n cells)) (setq n cells))
3454 (if (< interval 0) (setq interval (- interval)))
3455 (if (zerop interval) (setq interval (nth 4 dim)))
3458 (if (> interval-count 0) nil
3459 (setq interval-count interval)
3460 (table-with-cache-buffer
3461 (goto-char (point-min))
3462 (if (not (or prefix index))
3465 (if index (insert (format fmt index)))
3467 (table--fill-region (point-min) (point) table-cell-info-width justify)
3468 (setq table-cell-info-justify justify))
3469 (setq table-inhibit-auto-fill-paragraph t))
3470 (table--update-cell 'now)
3472 (setq index (+ index increment))
3473 (if (and prefix (string= postfix ""))
3474 (let ((len-1 (1- (length prefix))))
3475 (setq prefix (concat (substring prefix 0 len-1)
3477 (+ (string-to-char (substring prefix len-1)) increment)))))))
3479 (table-forward-cell direction t)
3480 (setq interval-count (1- interval-count))
3481 (setq cells (1- cells))
3482 (and (> n 0) (> cells 0)))))
3483 (table-recognize-cell 'force)
3484 (if (called-interactively-p 'interactive)
3485 (message "Sequencing...done"))
3489 (defun table-delete-row (n)
3490 "Delete N row(s) of cells.
3491 Delete N rows of cells from current row. The current row is the row
3492 contains the current cell where point is located. Each row must
3493 consists from cells of same height."
3495 (let ((orig-coord (table--get-coordinate))
3496 (bt-coord (table--get-coordinate (cdr (table--vertical-cell-list nil 'first-only))))
3497 lu-coord rb-coord rect)
3498 ;; determine the area to delete while testing row height uniformity
3501 (unless (table--probe-cell)
3502 (error "Table not found"))
3503 (let ((cell-list (table--horizontal-cell-list 'left-to-right)))
3505 (and (table--uniform-list-p
3506 (mapcar (lambda (cell) (cdr (table--get-coordinate (car cell)))) cell-list))
3507 (table--uniform-list-p
3508 (mapcar (lambda (cell) (cdr (table--get-coordinate (cdr cell)))) cell-list)))
3509 (error "Cells in this row are not in uniform height"))
3511 (setq lu-coord (table--get-coordinate (caar cell-list))))
3512 (setq rb-coord (table--get-coordinate (cdar (last cell-list))))
3513 (table--goto-coordinate (cons (car orig-coord) (+ 2 (cdr rb-coord))))))
3514 ;; copy the remaining area (below the deleting area)
3515 (setq rect (extract-rectangle
3516 (table--goto-coordinate (cons (1- (car lu-coord)) (1+ (cdr rb-coord))))
3517 (table--goto-coordinate (cons (1+ (car rb-coord)) (1+ (cdr bt-coord))))))
3518 ;; delete the deleting area and below together
3520 (table--goto-coordinate (cons (1- (car lu-coord)) (1- (cdr lu-coord))))
3521 (table--goto-coordinate (cons (1+ (car rb-coord)) (1+ (cdr bt-coord)))))
3522 (table--goto-coordinate (cons (1- (car lu-coord)) (1- (cdr lu-coord))))
3523 ;; insert the remaining area while appending blank lines below it
3524 (table--insert-rectangle
3525 (append rect (make-list (+ 2 (- (cdr rb-coord) (cdr lu-coord)))
3526 (make-string (+ 2 (- (car rb-coord) (car lu-coord))) ?\s))))
3527 ;; remove the appended blank lines below the table if they are unnecessary
3528 (table--goto-coordinate (cons 0 (- (cdr bt-coord) (- (cdr rb-coord) (cdr lu-coord)))))
3529 (table--remove-blank-lines (+ 2 (- (cdr rb-coord) (cdr lu-coord))))
3530 ;; fix up intersections
3531 (let ((coord (cons (car lu-coord) (1- (cdr lu-coord))))
3532 (n (1+ (- (car rb-coord) (car lu-coord)))))
3534 (table--goto-coordinate coord)
3536 (or (and (table--goto-coordinate (cons (car coord) (1- (cdr coord))) 'no-extension)
3537 (looking-at (regexp-quote (char-to-string table-cell-vertical-char))))
3538 (and (table--goto-coordinate (cons (car coord) (1+ (cdr coord))) 'no-extension)
3539 (looking-at (regexp-quote (char-to-string table-cell-vertical-char))))))
3542 (insert table-cell-intersection-char))
3544 (insert (string-to-char table-cell-horizontal-chars)))
3546 (setcar coord (1+ (car coord)))))
3547 ;; goto appropriate end point
3548 (table--goto-coordinate (cons (car orig-coord) (cdr lu-coord)))))
3551 (defun table-delete-column (n)
3552 "Delete N column(s) of cells.
3553 Delete N columns of cells from current column. The current column is
3554 the column contains the current cell where point is located. Each
3555 column must consists from cells of same width."
3557 (let ((orig-coord (table--get-coordinate))
3559 ;; determine the area to delete while testing column width uniformity
3562 (unless (table--probe-cell)
3563 (error "Table not found"))
3564 (let ((cell-list (table--vertical-cell-list 'top-to-bottom)))
3566 (and (table--uniform-list-p
3567 (mapcar (function (lambda (cell) (car (table--get-coordinate (car cell))))) cell-list))
3568 (table--uniform-list-p
3569 (mapcar (function (lambda (cell) (car (table--get-coordinate (cdr cell))))) cell-list)))
3570 (error "Cells in this column are not in uniform width"))
3572 (setq lu-coord (table--get-coordinate (caar cell-list))))
3573 (setq rb-coord (table--get-coordinate (cdar (last cell-list))))
3574 (table--goto-coordinate (cons (1+ (car rb-coord)) (cdr orig-coord)))))
3577 (table--goto-coordinate (cons (car lu-coord) (1- (cdr lu-coord))))
3578 (table--goto-coordinate (cons (1+ (car rb-coord)) (1+ (cdr rb-coord)))))
3579 ;; fix up the intersections
3580 (let ((coord (cons (1- (car lu-coord)) (cdr lu-coord)))
3581 (n (1+ (- (cdr rb-coord) (cdr lu-coord)))))
3583 (table--goto-coordinate coord)
3585 (or (and (table--goto-coordinate (cons (1- (car coord)) (cdr coord)) 'no-extension)
3586 (looking-at (regexp-opt-charset
3587 (string-to-list table-cell-horizontal-chars))))
3588 (and (table--goto-coordinate (cons (1+ (car coord)) (cdr coord)) 'no-extension)
3589 (looking-at (regexp-opt-charset
3590 (string-to-list table-cell-horizontal-chars))))))
3593 (insert table-cell-intersection-char))
3595 (insert table-cell-vertical-char))
3597 (setcdr coord (1+ (cdr coord)))))
3598 ;; goto appropriate end point
3599 (table--goto-coordinate (cons (car lu-coord) (cdr orig-coord)))))
3602 (defun table-capture (beg end &optional col-delim-regexp row-delim-regexp justify min-cell-width columns)
3603 "Convert plain text into a table by capturing the text in the region.
3604 Create a table with the text in region as cell contents. BEG and END
3605 specify the region. The text in the region is replaced with a table.
3606 The removed text is inserted in the table. When optional
3607 COL-DELIM-REGEXP and ROW-DELIM-REGEXP are provided the region contents
3608 is parsed and separated into individual cell contents by using the
3609 delimiter regular expressions. This parsing determines the number of
3610 columns and rows of the table automatically. If COL-DELIM-REGEXP and
3611 ROW-DELIM-REGEXP are omitted the result table has only one cell and
3612 the entire region contents is placed in that cell. Optional JUSTIFY
3613 is one of 'left, 'center or 'right, which specifies the cell
3614 justification. Optional MIN-CELL-WIDTH specifies the minimum cell
3615 width. Optional COLUMNS specify the number of columns when
3616 ROW-DELIM-REGEXP is not specified.
3625 Running `table-capture' on above 3 line region with COL-DELIM-REGEXP
3626 \",\" and ROW-DELIM-REGEXP \"\\n\" creates the following table. In
3627 this example the cells are centered and minimum cell width is
3630 +-----+-----+-----+-----+
3632 +-----+-----+-----+-----+
3634 +-----+-----+-----+-----+
3636 +-----+-----+-----+-----+
3640 In case the function is called interactively user must use \\[quoted-insert] `quoted-insert'
3641 in order to enter \"\\n\" successfully. COL-DELIM-REGEXP at the end
3642 of each row is optional.
3647 This example shows how a table can be used for text layout editing.
3648 Let `table-capture' capture the following region starting from
3649 -!- and ending at -*-, that contains three paragraphs and two item
3650 name headers. This time specify empty string for both
3651 COL-DELIM-REGEXP and ROW-DELIM-REGEXP.
3653 -!-`table-capture' is a powerful command however mastering its power
3654 requires some practice. Here is a list of items what it can do.
3656 Parse Cell Items By using column delimiter regular
3657 expression and raw delimiter regular
3658 expression, it parses the specified text
3659 area and extracts cell items from
3660 non-table text and then forms a table out
3663 Capture Text Area When no delimiters are specified it
3664 creates a single cell table. The text in
3665 the specified region is placed in that
3668 Now the entire content is captured in a cell which is itself a table
3671 +-----------------------------------------------------------------+
3672 |`table-capture' is a powerful command however mastering its power|
3673 |requires some practice. Here is a list of items what it can do. |
3675 |Parse Cell Items By using column delimiter regular |
3676 | expression and raw delimiter regular |
3677 | expression, it parses the specified text |
3678 | area and extracts cell items from |
3679 | non-table text and then forms a table out |
3682 |Capture Text Area When no delimiters are specified it |
3683 | creates a single cell table. The text in |
3684 | the specified region is placed in that |
3686 +-----------------------------------------------------------------+
3688 By splitting the cell appropriately we now have a table consisting of
3689 paragraphs occupying its own cell. Each cell can now be edited
3692 +-----------------------------------------------------------------+
3693 |`table-capture' is a powerful command however mastering its power|
3694 |requires some practice. Here is a list of items what it can do. |
3695 +---------------------+-------------------------------------------+
3696 |Parse Cell Items |By using column delimiter regular |
3697 | |expression and raw delimiter regular |
3698 | |expression, it parses the specified text |
3699 | |area and extracts cell items from |
3700 | |non-table text and then forms a table out |
3702 +---------------------+-------------------------------------------+
3703 |Capture Text Area |When no delimiters are specified it |
3704 | |creates a single cell table. The text in |
3705 | |the specified region is placed in that |
3707 +---------------------+-------------------------------------------+
3709 By applying `table-release', which does the opposite process, the
3710 contents become once again plain text. `table-release' works as
3711 companion command to `table-capture' this way.
3714 (let ((col-delim-regexp)
3716 (barf-if-buffer-read-only)
3717 (if (table--probe-cell)
3718 (error "Can't insert a table inside a table"))
3721 (setq col-delim-regexp
3722 (read-from-minibuffer "Column delimiter regexp: "
3723 (car table-col-delim-regexp-history) nil nil 'table-col-delim-regexp-history))
3724 (setq row-delim-regexp
3725 (read-from-minibuffer "Row delimiter regexp: "
3726 (car table-row-delim-regexp-history) nil nil 'table-row-delim-regexp-history))
3727 (let* ((completion-ignore-case t)
3728 (default (car table-capture-justify-history)))
3729 (if (and (string= col-delim-regexp "") (string= row-delim-regexp "")) 'left
3731 (downcase (completing-read
3732 (format "Justify (default %s): " default)
3733 '(("left") ("center") ("right"))
3734 nil t nil 'table-capture-justify-history default)))))
3735 (if (and (string= col-delim-regexp "") (string= row-delim-regexp "")) "1"
3736 (table--read-from-minibuffer '("Minimum cell width" . table-capture-min-cell-width-history)))
3737 (if (and (not (string= col-delim-regexp "")) (string= row-delim-regexp ""))
3739 (table--read-from-minibuffer '("Number of columns" . table-capture-columns-history)))
3742 (if (> beg end) (let ((tmp beg)) (setq beg end) (setq end tmp)))
3743 (if (string= col-delim-regexp "") (setq col-delim-regexp nil))
3744 (if (string= row-delim-regexp "") (setq row-delim-regexp nil))
3745 (if (and columns (< columns 1)) (setq columns nil))
3746 (unless min-cell-width (setq min-cell-width "5"))
3747 (let ((contents (buffer-substring beg end))
3748 (cols 0) (rows 0) c r cell-list
3750 (if (and col-delim-regexp row-delim-regexp)
3751 (format "\\(\\(%s\\)?\\s *\\(%s\\)\\s *\\)\\|\\(\\(%s\\)\\s *\\)"
3752 col-delim-regexp row-delim-regexp col-delim-regexp)
3753 (if col-delim-regexp
3754 (format "\\(\\)\\(\\)\\(\\)\\(\\(%s\\)\\s *\\)" col-delim-regexp))))
3756 ;; when delimiters are specified extract cells and determine the cell dimension
3760 ;; make sure the contents ends with a newline
3761 (goto-char (point-max))
3762 (unless (zerop (current-column))
3764 ;; skip the preceding white spaces
3765 (goto-char (point-min))
3766 (if (looking-at "\\s +")
3767 (goto-char (match-end 0)))
3768 ;; extract cell contents
3769 (let ((from (point)))
3770 (setq cell-list nil)
3772 (while (and (re-search-forward delim-pattern nil t)
3775 ((and (match-string 1) (not (string= (match-string 1) "")))
3776 (setq rows (1+ rows))
3778 (append cell-list (list (buffer-substring from (match-beginning 1)))))
3779 (setq from (match-end 1))
3781 (append contents-list (list cell-list)))
3782 (setq cell-list nil)
3784 (if (> c cols) (setq cols c))
3788 ((and (match-string 4) (not (string= (match-string 4) "")))
3790 (append cell-list (list (buffer-substring from (match-beginning 4)))))
3791 (setq from (match-end 4))
3793 (if (> c cols) (setq cols c))
3796 ;; take care of the last element without a post delimiter
3797 (unless (null (looking-at ".+$"))
3799 (append cell-list (list (match-string 0))))
3800 (setq cols (1+ cols)))
3801 ;; take care of the last row without a terminating delimiter
3802 (unless (null cell-list)
3803 (setq rows (1+ rows))
3805 (append contents-list (list cell-list)))))))
3806 ;; finalize the table dimension
3807 (if (and columns contents-list)
3808 ;; when number of columns are specified and cells are parsed determine the dimension
3811 (setq rows (/ (+ (length (car contents-list)) columns -1) columns)))
3812 ;; when dimensions are not specified default to a single cell table
3813 (if (zerop rows) (setq rows 1))
3814 (if (zerop cols) (setq cols 1)))
3815 ;; delete the region and reform line breaks
3816 (delete-region beg end)
3818 (unless (zerop (current-column))
3820 (unless (looking-at "\\s *$")
3824 ;; insert the cell contents
3825 (if (null contents-list)
3827 (let ((width) (height))
3830 (table--remove-eol-spaces (point-min) (point-max))
3831 (table--untabify (point-min) (point-max))
3832 (setq width (table--measure-max-width))
3833 (setq height (1+ (table--current-line (point-max))))
3834 (setq contents (buffer-substring (point-min) (point-max))))
3835 (table-insert cols rows width height)
3836 (table-with-cache-buffer
3838 (setq table-inhibit-auto-fill-paragraph t)))
3840 (table-insert cols rows min-cell-width 1)
3842 (setq cell-list nil)
3847 (setq cell-list (car contents-list))
3848 (setq contents-list (cdr contents-list)))
3852 (table-with-cache-buffer
3853 (insert (car cell-list))
3854 (setq cell-list (cdr cell-list))
3855 (setq table-cell-info-justify justify)))
3856 (table-forward-cell 1))))))
3859 (defun table-release ()
3860 "Convert a table into plain text by removing the frame from a table.
3861 Remove the frame from a table and inactivate the table. This command
3862 converts a table into plain text without frames. It is a companion to
3863 `table-capture' which does the opposite process."
3865 (let ((origin-cell (table--probe-cell))
3868 (let ((old-point (point-marker)))
3869 ;; save-excursion is not sufficient for this
3870 ;; because untabify operation moves point
3871 (set-marker-insertion-type old-point t)
3876 (table-forward-cell 1 nil 'unrecognize)
3877 (let ((cell (table--probe-cell)))
3878 (if (or (null table-lu)
3879 (< (car cell) table-lu))
3880 (setq table-lu (car cell)))
3881 (if (or (null table-rb)
3882 (> (cdr cell) table-rb))
3883 (setq table-rb (cdr cell)))
3884 (and cell (not (equal cell origin-cell))))))
3885 (let* ((lu-coord (table--get-coordinate table-lu))
3886 (rb-coord (table--get-coordinate table-rb))
3887 (lu (table--goto-coordinate (table--offset-coordinate lu-coord '(-1 . -1)))))
3888 (table--spacify-frame)
3889 (setcdr rb-coord (1+ (cdr rb-coord)))
3890 (delete-rectangle lu (table--goto-coordinate (cons (car lu-coord) (cdr rb-coord))))
3891 (table--remove-eol-spaces
3892 (table--goto-coordinate (cons 0 (1- (cdr lu-coord))))
3893 (table--goto-coordinate rb-coord) nil t)))
3894 (goto-char old-point))))))
3896 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3898 ;; Worker functions (executed implicitly)
3901 (defun table--make-cell-map ()
3902 "Make the table cell keymap if it does not exist yet."
3903 ;; this is irrelevant to keymap but good place to make sure to be executed
3904 (table--update-cell-face)
3905 (unless table-cell-map
3906 (let ((map (make-sparse-keymap))
3907 (remap-alist table-command-remap-alist))
3908 ;; table-command-prefix mode specific bindings
3909 (if (vectorp table-command-prefix)
3910 (mapc (lambda (binding)
3911 (let ((seq (copy-sequence (car binding))))
3913 (listp (aref seq 0))
3914 (eq (car (aref seq 0)) 'control)
3916 (aset seq 0 (cadr (aref seq 0)))
3917 (define-key map (vconcat table-command-prefix seq) (cdr binding))))))
3918 table-cell-bindings))
3919 ;; shorthand control bindings
3920 (mapc (lambda (binding)
3921 (define-key map (car binding) (cdr binding)))
3922 table-cell-bindings)
3923 ;; remap normal commands to table specific version
3925 (define-key map (vector 'remap (caar remap-alist)) (cdar remap-alist))
3926 (setq remap-alist (cdr remap-alist)))
3928 (setq table-cell-map map)
3929 (fset 'table-cell-map map)))
3930 ;; add menu for table cells
3931 (unless table-disable-menu
3932 (easy-menu-define table-cell-menu-map table-cell-map "Table cell menu" table-cell-menu)
3933 (if (featurep 'xemacs)
3934 (easy-menu-add table-cell-menu)))
3935 (run-hooks 'table-cell-map-hook))
3937 ;; Create the keymap after running the user init file so that the user
3938 ;; modification to the global-map is accounted.
3939 (add-hook 'after-init-hook 'table--make-cell-map t)
3941 (defun *table--cell-self-insert-command ()
3942 "Table cell version of `self-insert-command'."
3944 (let ((char last-command-event))
3945 (if (eq buffer-undo-list t) nil
3946 (if (not (eq last-command this-command))
3947 (setq table-cell-self-insert-command-count 0)
3948 (if (car buffer-undo-list) nil
3949 (if (>= table-cell-self-insert-command-count 19)
3950 (setq table-cell-self-insert-command-count 0)
3951 (setq buffer-undo-list (cdr buffer-undo-list))
3952 (setq table-cell-self-insert-command-count (1+ table-cell-self-insert-command-count))))))
3953 (table--cell-insert-char char overwrite-mode)))
3955 (defun *table--cell-delete-backward-char (n)
3956 "Table cell version of `delete-backward-char'."
3958 (*table--cell-delete-char (- n)))
3960 (defun *table--cell-newline (&optional indent)
3961 "Table cell version of `newline'."
3963 (table-with-cache-buffer
3964 (let ((column (current-column)))
3966 (if indent (indent-to-column column))
3967 ;; fill only when at the beginning of paragraph
3970 (forward-paragraph -1)
3971 (if (looking-at "\\s *$")
3974 nil ; yes, at the beginning of the paragraph
3975 (setq table-inhibit-auto-fill-paragraph t)))))
3977 (defun *table--cell-open-line (n)
3978 "Table cell version of `open-line'."
3980 (table-with-cache-buffer
3982 (insert (make-string n ?\n))
3983 (table--fill-region (point) (point))
3984 (setq table-inhibit-auto-fill-paragraph t))))
3986 (defun *table--cell-newline-and-indent ()
3987 "Table cell version of `newline-and-indent'."
3989 (*table--cell-newline t))
3991 (defun *table--cell-delete-char (n)
3992 "Table cell version of `delete-char'."
3994 (let ((overwrite overwrite-mode))
3995 (table-with-cache-buffer
3996 (if (and overwrite (< n 0))
3998 (while (not (zerop n))
3999 (let ((coordinate (table--get-coordinate)))
4000 (if (zerop (car coordinate))
4001 (unless (zerop (cdr coordinate))
4002 (table--goto-coordinate (cons (1- table-cell-info-width) (1- (cdr coordinate))))
4009 (setq table-inhibit-auto-fill-paragraph t))
4010 (let ((coordinate (table--get-coordinate))
4011 (end-marker (copy-marker (+ (point) n)))
4013 (if (or (< end-marker (point-min))
4014 (> end-marker (point-max))) nil
4015 (table--remove-eol-spaces (point-min) (point-max))
4016 (setq deleted (buffer-substring (point) end-marker))
4018 ;; in fixed width mode when two lines are concatenated
4019 ;; remove continuation character if there is one.
4020 (and table-fixed-width-mode
4021 (string-match "^\n" deleted)
4022 (equal (char-before) table-word-continuation-char)
4024 ;; see if the point is placed at the right tip of the previous
4025 ;; blank line, if so get rid of the preceding blanks.
4026 (if (and (not (bolp))
4027 (/= (cdr coordinate) (cdr (table--get-coordinate)))
4028 (let ((end (point)))
4031 (re-search-forward "\\s +" end t)
4034 ;; do not fill the paragraph if the point is already at the end
4035 ;; of this paragraph and is following a blank character
4036 ;; (otherwise the filling squeezes the preceding blanks)
4037 (if (and (looking-at "\\s *$")
4041 (looking-at "\\s "))))
4042 (setq table-inhibit-auto-fill-paragraph t))
4044 (set-marker end-marker nil))))))
4046 (defun *table--cell-quoted-insert (arg)
4047 "Table cell version of `quoted-insert'."
4049 (let ((char (read-quoted-char)))
4051 (table--cell-insert-char char nil)
4052 (setq arg (1- arg)))))
4054 (defun *table--cell-describe-mode ()
4055 "Table cell version of `describe-mode'."
4057 (if (not (table--point-in-cell-p))
4058 (call-interactively 'describe-mode)
4059 (with-output-to-temp-buffer "*Help*"
4060 (princ "Table mode: (in ")
4061 (princ (format-mode-line mode-name nil nil (current-buffer)))
4064 Table is not a mode technically. You can regard it as a pseudo mode
4065 which exists locally within a buffer. It overrides some standard
4066 editing behaviors. Editing operations in a table produces confined
4067 effects to the current cell. It may grow the cell horizontally and/or
4068 vertically depending on the newly entered or deleted contents of the
4069 cell, and also depending on the current mode of cell.
4071 In the normal mode the table preserves word continuity. Which means
4072 that a word never gets folded into multiple lines. For this purpose
4073 table will occasionally grow the cell width. On the other hand, when
4074 in a fixed width mode all cell width are fixed. When a word can not
4075 fit in the cell width the word is folded into the next line. The
4076 folded location is marked by a continuation character which is
4077 specified in the variable `table-word-continuation-char'.
4079 (help-print-return-message))))
4081 (defun *table--cell-describe-bindings ()
4082 "Table cell version of `describe-bindings'."
4084 (if (not (table--point-in-cell-p))
4085 (call-interactively 'describe-bindings)
4086 (with-output-to-temp-buffer "*Help*"
4087 (princ "Table Bindings:
4092 (mapc (lambda (binding)
4093 (princ (format "%-16s%s\n"
4094 (key-description (car binding))
4096 table-cell-bindings)
4097 (help-print-return-message))))
4099 (defun *table--cell-dabbrev-expand (arg)
4100 "Table cell version of `dabbrev-expand'."
4102 (let ((dabbrev-abbrev-char-regexp (concat "[^"
4103 (char-to-string table-cell-vertical-char)
4104 (char-to-string table-cell-intersection-char)
4106 (table-with-cache-buffer
4107 (dabbrev-expand arg))))
4109 (defun *table--cell-dabbrev-completion (&optional arg)
4110 "Table cell version of `dabbrev-completion'."
4112 (error "`dabbrev-completion' is incompatible with table")
4113 (let ((dabbrev-abbrev-char-regexp (concat "[^"
4114 (char-to-string table-cell-vertical-char)
4115 (char-to-string table-cell-intersection-char)
4117 (table-with-cache-buffer
4118 (dabbrev-completion arg))))
4120 (defun *table--present-cell-popup-menu (event)
4121 "Present and handle cell popup menu."
4123 (unless table-disable-menu
4124 (select-window (posn-window (event-start event)))
4125 (goto-char (posn-point (event-start event)))
4126 (let ((item-list (x-popup-menu event table-cell-menu-map))
4127 (func table-cell-menu-map))
4129 (setq func (nth 3 (assoc (car item-list) func)))
4130 (setq item-list (cdr item-list)))
4131 (if (and (symbolp func) (fboundp func))
4132 (call-interactively func)))))
4134 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4136 ;; Cell updating functions
4139 (defun table--update-cell (&optional now)
4140 "Update the table cell contents.
4141 When the optional parameter NOW is nil it only sets up the update
4142 timer. If it is non-nil the function copies the contents of the cell
4143 cache buffer into the designated cell in the table buffer."
4144 (if (null table-update-timer) nil
4145 (table--cancel-timer table-update-timer)
4146 (setq table-update-timer nil))
4148 (and (boundp 'quail-converting)
4149 quail-converting) ;; defer operation while current quail work is not finished.
4150 (and (boundp 'quail-translating)
4152 (setq table-update-timer
4153 (table--set-timer table-time-before-update
4154 (function table--update-cell)
4156 (save-current-buffer
4157 (set-buffer table-cell-buffer)
4158 (let ((cache-buffer (get-buffer-create table-cache-buffer-name))
4159 (org-coord (table--get-coordinate))
4160 (in-cell (equal (table--cell-to-coord (table--probe-cell))
4161 (cons table-cell-info-lu-coordinate table-cell-info-rb-coordinate)))
4163 (set-buffer cache-buffer)
4167 (table--goto-coordinate (cons table-cell-info-width (1- table-cell-info-height)))))
4168 (set-buffer table-cell-buffer)
4169 (delete-rectangle (table--goto-coordinate table-cell-info-lu-coordinate)
4170 (table--goto-coordinate table-cell-info-rb-coordinate))
4171 (table--goto-coordinate table-cell-info-lu-coordinate)
4172 (table--insert-rectangle rectangle)
4173 (let* ((cell (table--probe-cell))) ; must probe again in case of wide characters
4174 (table--put-cell-property cell)
4175 (table--put-cell-justify-property cell table-cell-info-justify)
4176 (table--put-cell-valign-property cell table-cell-info-valign))
4177 (table--goto-coordinate
4179 (table--transcoord-cache-to-table table-cell-cache-point-coordinate)
4181 ;; simulate undo behavior under overwrite-mode
4182 (if (and overwrite-mode (not (eq buffer-undo-list t)))
4183 (setq buffer-undo-list (cons nil buffer-undo-list)))))
4185 (defun table--update-cell-widened (&optional now)
4186 "Update the contents of the cells that are affected by widening operation."
4187 (if (null table-widen-timer) nil
4188 (table--cancel-timer table-widen-timer)
4189 (setq table-widen-timer nil))
4191 (setq table-widen-timer
4192 (table--set-timer (+ table-time-before-update table-time-before-reformat)
4193 (function table--update-cell-widened)
4195 (save-current-buffer
4196 (if table-update-timer
4197 (table--update-cell 'now))
4198 (set-buffer table-cell-buffer)
4199 (let* ((current-coordinate (table--get-coordinate))
4200 (current-cell-coordinate (table--cell-to-coord (table--probe-cell)))
4201 (cell-coord-list (progn
4202 (table--goto-coordinate table-cell-info-lu-coordinate)
4203 (table--cell-list-to-coord-list (table--vertical-cell-list)))))
4204 (while cell-coord-list
4205 (let* ((cell-coord (prog1 (car cell-coord-list) (setq cell-coord-list (cdr cell-coord-list))))
4206 (currentp (equal cell-coord current-cell-coordinate)))
4207 (if currentp (table--goto-coordinate current-coordinate)
4208 (table--goto-coordinate (car cell-coord)))
4209 (table-recognize-cell 'froce)
4210 (let ((table-inhibit-update t))
4211 (table-with-cache-buffer
4212 (let ((sticky (and currentp
4214 (unless (bolp) (forward-char -1))
4215 (looking-at ".*\\S ")))))
4216 (table--fill-region (point-min) (point-max))
4218 (setq current-coordinate (table--transcoord-cache-to-table))))))
4219 (table--update-cell 'now)
4221 (table--goto-coordinate current-coordinate)
4222 (table-recognize-cell 'froce)))))
4224 (defun table--update-cell-heightened (&optional now)
4225 "Update the contents of the cells that are affected by heightening operation."
4226 (if (null table-heighten-timer) nil
4227 (table--cancel-timer table-heighten-timer)
4228 (setq table-heighten-timer nil))
4230 (setq table-heighten-timer
4231 (table--set-timer (+ table-time-before-update table-time-before-reformat)
4232 (function table--update-cell-heightened)
4234 (save-current-buffer
4235 (if table-update-timer
4236 (table--update-cell 'now))
4237 (if table-widen-timer
4238 (table--update-cell-widened 'now))
4239 (set-buffer table-cell-buffer)
4240 (let* ((current-coordinate (table--get-coordinate))
4241 (current-cell-coordinate (table--cell-to-coord (table--probe-cell)))
4242 (cell-coord-list (progn
4243 (table--goto-coordinate table-cell-info-lu-coordinate)
4244 (table--cell-list-to-coord-list (table--horizontal-cell-list)))))
4245 (while cell-coord-list
4246 (let* ((cell-coord (prog1 (car cell-coord-list) (setq cell-coord-list (cdr cell-coord-list))))
4247 (currentp (equal cell-coord current-cell-coordinate)))
4248 (if currentp (table--goto-coordinate current-coordinate)
4249 (table--goto-coordinate (car cell-coord)))
4250 (table-recognize-cell 'froce)
4251 (let ((table-inhibit-update t))
4252 (table-with-cache-buffer
4253 (let ((sticky (and currentp
4255 (unless (bolp) (forward-char -1))
4256 (looking-at ".*\\S ")))))
4259 (setq current-coordinate (table--transcoord-cache-to-table))))))
4260 (table--update-cell 'now)
4262 (table--goto-coordinate current-coordinate)
4263 (table-recognize-cell 'froce)))))
4265 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4267 ;; Service functions (for external packages)
4270 (defun table-goto-top-left-corner ()
4271 "Move point to top left corner of the current table and return the char position."
4272 (table--goto-coordinate
4274 (1- (car (table--get-coordinate (car (table--horizontal-cell-list t t)))))
4275 (1- (cdr (table--get-coordinate (car (table--vertical-cell-list t t))))))))
4277 (defun table-goto-top-right-corner ()
4278 "Move point to top right corner of the current table and return the char position."
4279 (table--goto-coordinate
4281 (car (table--get-coordinate (cdr (table--horizontal-cell-list nil t))))
4282 (1- (cdr (table--get-coordinate (car (table--vertical-cell-list t t))))))))
4284 (defun table-goto-bottom-left-corner ()
4285 "Move point to bottom left corner of the current table and return the char position."
4286 (table--goto-coordinate
4288 (1- (car (table--get-coordinate (car (table--horizontal-cell-list t t)))))
4289 (1+ (cdr (table--get-coordinate (cdr (table--vertical-cell-list nil t))))))))
4291 (defun table-goto-bottom-right-corner ()
4292 "Move point to bottom right corner of the current table and return the char position."
4293 (table--goto-coordinate
4295 (car (table--get-coordinate (cdr (table--horizontal-cell-list nil t))))
4296 (1+ (cdr (table--get-coordinate (cdr (table--vertical-cell-list nil t))))))))
4298 (defun table-call-interactively (function &optional recoard-flag keys)
4299 "Call FUNCTION, or a table version of it if applicable.
4300 See `call-interactively' for full description of the arguments."
4301 (let ((table-func (intern-soft (format "*table--cell-%s" function))))
4304 (table--point-in-cell-p))
4306 function) recoard-flag keys)))
4308 (defun table-funcall (function &rest arguments)
4309 "Call FUNCTION, or a table version of it if applicable.
4310 See `funcall' for full description of the arguments."
4311 (let ((table-func (intern-soft (format "*table--cell-%s" function))))
4314 (table--point-in-cell-p))
4319 (defmacro table-apply (function &rest arguments)
4320 "Call FUNCTION, or a table version of it if applicable.
4321 See `apply' for full description of the arguments."
4322 (let ((table-func (make-symbol "table-func")))
4323 `(let ((,table-func (intern-soft (format "*table--cell-%s" ,function))))
4325 (if (and ,table-func
4326 (table--point-in-cell-p))
4331 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4333 ;; Utility functions
4336 (defun table--read-from-minibuffer (prompt-history)
4337 "A wrapper to `read-from-minibuffer'.
4338 PROMPT-HISTORY is a cons cell which car is the prompt string and the
4339 cdr is the history symbol."
4340 (let ((default (car (symbol-value (cdr prompt-history)))))
4341 (read-from-minibuffer
4342 (format "%s (default %s): " (car prompt-history) default)
4343 "" nil nil (cdr prompt-history) default))
4344 (and (featurep 'xemacs)
4345 (equal (car (symbol-value (cdr prompt-history))) "")
4346 (set (cdr prompt-history)
4347 (cdr (symbol-value (cdr prompt-history)))))
4348 (car (symbol-value (cdr prompt-history))))
4350 (defun table--buffer-substring-and-trim (beg end)
4351 "Extract buffer substring and remove blanks from front and the rear of it."
4354 (narrow-to-region (goto-char beg) end)
4355 (if (re-search-forward "\\s *")
4356 (setq beg (match-end 0)))
4357 (if (re-search-forward "\\s *\\'" end t)
4358 (setq end (match-beginning 0)))
4359 (table--remove-cell-properties
4361 (buffer-substring beg end)))))
4363 (defun table--valign ()
4364 "Vertically align the cache cell contents.
4365 Current buffer must be the cache buffer at the entry to this function.
4366 Returns the coordinate of the final point location."
4367 (if (or (null table-cell-info-valign)
4368 (eq table-cell-info-valign 'none))
4369 (table--get-coordinate)
4370 (let ((saved-point (point-marker)))
4371 ;;(set-marker-insertion-type saved-point t)
4372 (goto-char (point-min))
4373 (let* ((from (and (re-search-forward "^.*\\S " nil t)
4374 (table--current-line)))
4375 (to (let ((tmp from))
4376 (while (re-search-forward "^.*\\S " nil t)
4377 (setq tmp (table--current-line)))
4379 (content-height (and from to (1+ (- to from)))))
4380 (unless (null content-height)
4381 (goto-char (point-min))
4382 (if (looking-at "\\s *\n")
4384 (cond ((eq table-cell-info-valign 'middle)
4385 (insert (make-string (/ (- table-cell-info-height content-height) 2) ?\n)))
4386 ((eq table-cell-info-valign 'bottom)
4387 (insert (make-string (- table-cell-info-height content-height) ?\n))))
4388 (table--goto-coordinate (cons table-cell-info-width (1- table-cell-info-height)))
4389 (if (re-search-forward "\\s +\\'" nil t)
4390 (replace-match ""))))
4391 (goto-char saved-point)
4392 (set-marker saved-point nil)
4393 (let ((coord (table--get-coordinate)))
4394 (unless (< (cdr coord) table-cell-info-height)
4395 (setcdr coord (1- table-cell-info-height))
4396 (table--goto-coordinate coord))
4399 (defun table--query-justification ()
4400 (barf-if-buffer-read-only)
4401 (let* ((completion-ignore-case t)
4402 (default (car table-justify-history)))
4403 (intern (downcase (completing-read
4404 (format "Justify (default %s): " default)
4405 '(("left") ("center") ("right") ("top") ("middle") ("bottom") ("none"))
4406 nil t nil 'table-justify-history default)))))
4408 (defun table--spacify-frame ()
4409 "Spacify table frame.
4410 Replace frame characters with spaces."
4412 (append (string-to-list table-cell-horizontal-chars)
4413 (list table-cell-intersection-char table-cell-vertical-char))))
4417 ((eq (char-after) table-cell-intersection-char)
4419 (let ((col (current-column)))
4420 (and (zerop (forward-line 1))
4421 (zerop (current-column))
4422 (move-to-column col)
4423 (table--spacify-frame))))
4425 (insert-before-markers ?\s))
4426 ((table--cell-horizontal-char-p (char-after))
4429 (insert-before-markers ?\s)
4430 (table--cell-horizontal-char-p (char-after)))))
4431 ((eq (char-after) table-cell-vertical-char)
4432 (while (let ((col (current-column)))
4434 (insert-before-markers ?\s)
4435 (and (zerop (forward-line 1))
4436 (zerop (current-column))
4437 (move-to-column col)
4438 (eq (char-after) table-cell-vertical-char))))))
4439 (memq (char-after) frame-char)))))
4441 (defun table--remove-blank-lines (n)
4442 "Delete N blank lines from the current line.
4443 For adjusting below area of the table when the table is shortened."
4445 (let ((first-blank t))
4448 (cond ((looking-at "\\s *\\'")
4449 (delete-region (match-beginning 0) (match-end 0))
4451 ((and (looking-at "\\([ \t]*\n[ \t]*\\)\n") first-blank)
4452 (delete-region (match-beginning 1) (match-end 1)))
4453 ((looking-at "[ \t]*$")
4454 (delete-region (match-beginning 0) (match-end 0))
4457 (setq first-blank nil)
4458 (forward-line 1))))))
4460 (defun table--uniform-list-p (l)
4461 "Return nil when LIST contains non equal elements. Otherwise return t."
4465 (if (not (equal (car l) (cadr l))) (throw 'end nil))
4469 (defun table--detect-cell-alignment (cell)
4470 "Detect CELL contents alignment.
4471 Guess CELL contents alignment both horizontally and vertically by
4472 looking at the appearance of the CELL contents."
4473 (let ((cell-contents (extract-rectangle (car cell) (cdr cell)))
4479 (margin-info-available nil)
4482 (table--insert-rectangle cell-contents)
4483 ;; determine the horizontal justification
4484 (goto-char (point-min))
4485 (while (re-search-forward "^\\( *\\).*[^ \n]\\( *\\)$" nil t)
4486 (setq margin-info-available t)
4487 (let* ((lm (- (match-end 1) (match-beginning 1)))
4488 (rm (- (match-end 2) (match-beginning 2)))
4489 (md (abs (- lm rm))))
4490 (if (> lm left-margin)
4491 (setq left-margin lm))
4492 (if (> rm right-margin)
4493 (setq right-margin rm))
4494 (if (> md margin-diff)
4495 (setq margin-diff md))))
4498 ((and margin-info-available
4500 (> left-margin 0)) 'center)
4501 ((and margin-info-available
4502 (zerop right-margin)
4503 (> left-margin 0)) 'right)
4505 ;; determine the vertical justification
4506 (goto-char (point-min))
4507 (if (and (re-search-forward "\\s *\\S " nil t)
4508 (/= (match-beginning 0) (match-end 0)))
4509 (setq top-margin (1- (count-lines (match-beginning 0) (match-end 0)))))
4510 (if (and (re-search-forward "\\s *\\'" nil t)
4511 (/= (match-beginning 0) (match-end 0)))
4512 (setq bottom-margin (1- (count-lines (match-beginning 0) (match-end 0)))))
4515 ((and (> top-margin 0)
4517 (<= (abs (- top-margin bottom-margin)) 1)) 'middle)
4518 ((and (> top-margin 0)
4519 (zerop bottom-margin)) 'bottom)
4521 (table--put-cell-justify-property cell justify)
4522 (table--put-cell-valign-property cell valign)))
4524 (defun table--string-to-number-list (str)
4525 "Return a list of numbers in STR."
4528 (while (string-match "[-0-9.]+" str idx)
4529 (setq idx (match-end 0))
4530 (setq nl (cons (string-to-number (match-string 0 str)) nl)))
4533 (defun table--justify-cell-contents (justify &optional paragraph)
4534 "Justify the current cell contents.
4535 JUSTIFY is a symbol 'left, 'center or 'right for horizontal, or 'top,
4536 'middle, 'bottom or 'none for vertical. When PARAGRAPH is non-nil the
4537 justify operation is limited to the current paragraph."
4538 (table-with-cache-buffer
4539 (let ((beg (point-min))
4540 (end (point-max-marker))
4541 (fill-column table-cell-info-width)
4542 (adaptive-fill-mode nil)
4543 (valign-symbols '(top middle bottom none)))
4545 (if (memq justify valign-symbols)
4546 (setq table-cell-info-valign
4547 (if (eq justify 'none) nil justify))
4548 (setq table-cell-info-justify justify)))
4551 (let ((paragraph-start "\n"))
4553 (or (bolp) (newline 1))
4554 (set-marker end (point))
4555 (setq beg (progn (forward-paragraph -1) (point)))))
4556 (if (memq justify valign-symbols)
4558 (table--remove-eol-spaces beg end 'bol)
4559 (let ((paragraph-start table-paragraph-start))
4560 (fill-region beg end table-cell-info-justify))))
4561 (setq table-inhibit-auto-fill-paragraph t)
4562 (set-marker end nil)))
4563 (table--update-cell 'now))
4565 (defun table--horizontally-shift-above-and-below (columns-to-extend top-to-bottom-coord-list)
4566 "Horizontally shift outside contents right above and right below of the table.
4567 This function moves the surrounding text outside of the table so that
4568 they match the horizontal growth/shrink of the table. It also
4569 untabify the shift affected area including the right side of the table
4570 so that tab related uneven shifting is avoided. COLUMNS-TO-EXTEND
4571 specifies the number of columns the table grows, or shrinks if
4572 negative. TOP-TO-BOTTOM-COORD-LIST is the vertical cell coordinate
4573 list. This list can be any vertical list within the table."
4575 (let (beg-coord end-coord)
4576 (table--goto-coordinate (caar top-to-bottom-coord-list))
4577 (let* ((cell (table--horizontal-cell-list nil 'first-only 'top))
4578 (coord (cons (car (table--get-coordinate (cdr cell)))
4579 (cdr (table--get-coordinate (car cell))))))
4580 (setcar coord (1+ (car coord)))
4581 (setcdr coord (- (cdr coord) 2))
4582 (setq beg-coord (cons (car coord) (1+ (cdr coord))))
4583 (while (and (table--goto-coordinate coord 'no-extension)
4584 (not (looking-at "\\s *$")))
4585 (if (< columns-to-extend 0)
4587 (table--untabify-line)
4588 (delete-char columns-to-extend))
4589 (table--untabify-line (point))
4590 (insert (make-string columns-to-extend ?\s)))
4591 (setcdr coord (1- (cdr coord)))))
4592 (table--goto-coordinate (caar (last top-to-bottom-coord-list)))
4593 (let ((coord (table--get-coordinate (cdr (table--horizontal-cell-list nil 'first-only 'bottom)))))
4594 (setcar coord (1+ (car coord)))
4595 (setcdr coord (+ (cdr coord) 2))
4596 (setq end-coord (cons (car coord) (1- (cdr coord))))
4597 (while (and (table--goto-coordinate coord 'no-extension)
4598 (not (looking-at "\\s *$")))
4599 (if (< columns-to-extend 0)
4601 (table--untabify-line)
4602 (delete-char columns-to-extend))
4603 (table--untabify-line (point))
4604 (insert (make-string columns-to-extend ?\s)))
4605 (setcdr coord (1+ (cdr coord)))))
4606 (while (<= (cdr beg-coord) (cdr end-coord))
4607 (table--untabify-line (table--goto-coordinate beg-coord 'no-extension))
4608 (setcdr beg-coord (1+ (cdr beg-coord)))))))
4610 (defun table--create-growing-space-below (lines-to-extend left-to-right-coord-list bottom-border-y)
4611 "Create growing space below the table.
4612 This function creates growing space below the table slightly
4613 intelligent fashion. Following is the cases it handles for each
4615 1. When the first line below the table is a complete blank line it
4616 inserts a blank line.
4617 2. When the line starts with a prefix that matches the prefix of the
4618 bottom line of the table it inserts a line consisting of prefix alone.
4619 3. Otherwise it deletes the rectangular contents where table will
4623 (prefix (and (table--goto-coordinate (cons 0 bottom-border-y))
4627 (table--goto-coordinate
4628 (cons (1- (caar (car left-to-right-coord-list))) bottom-border-y)))
4630 (buffer-substring (match-beginning 0) (match-end 0)))))
4631 (while (< i lines-to-extend)
4632 (let ((y (+ i bottom-border-y 1)))
4633 (table--goto-coordinate (cons 0 y))
4635 ((looking-at "\\s *$")
4637 ((and prefix (looking-at (concat (regexp-quote prefix) "\\s *$")))
4638 (insert prefix ?\n))
4641 (table--goto-coordinate (cons (1- (caar (car left-to-right-coord-list))) y))
4642 (table--goto-coordinate (cons (1+ (cadr (car (last left-to-right-coord-list)))) y))))))
4645 (defun table--untabify-line (&optional from)
4646 "Untabify current line.
4647 Unlike save-excursion this guarantees preserving the cursor location
4648 even when the point is on a tab character which is to be removed.
4649 Optional FROM narrows the subject operation from this point to the end
4651 (let ((current-coordinate (table--get-coordinate)))
4652 (table--untabify (or from (progn (beginning-of-line) (point)))
4653 (progn (end-of-line) (point)))
4654 (table--goto-coordinate current-coordinate)))
4656 (defun table--untabify (beg end)
4657 "Wrapper to raw untabify."
4659 (if (featurep 'xemacs)
4660 ;; Cancel strange behavior of xemacs
4663 (defun table--multiply-string (string multiplier)
4664 "Multiply string and return it."
4666 (while (> multiplier 0)
4667 (setq ret-str (concat ret-str string))
4668 (setq multiplier (1- multiplier)))
4671 (defun table--line-column-position (line column)
4672 "Return the location of LINE forward at COLUMN."
4675 (move-to-column column)
4678 (defun table--row-column-insertion-point-p (&optional columnp)
4679 "Return non-nil if it makes sense to insert a row or a column at point."
4680 (and (not buffer-read-only)
4681 (or (get-text-property (point) 'table-cell)
4682 (let ((column (current-column)))
4684 (or (text-property-any (line-beginning-position 0)
4685 (table--line-column-position -1 column)
4687 (text-property-any (line-beginning-position) (point) 'table-cell t)
4688 (text-property-any (line-beginning-position 2)
4689 (table--line-column-position 1 column)
4691 (text-property-any (table--line-column-position -2 column)
4692 (table--line-column-position -2 (+ 2 column))
4695 (defun table--find-row-column (&optional columnp no-error)
4696 "Search table and return a cell coordinate list of row or column."
4697 (let ((current-coordinate (table--get-coordinate)))
4700 (let ((coord (table--get-coordinate)))
4703 (if columnp (setcar coord (1- (car coord)))
4704 (setcdr coord (1- (cdr coord))))
4705 (>= (if columnp (car coord) (cdr coord)) 0))
4707 (table--goto-coordinate coord 'no-extension 'no-tab-expansion)
4708 (not (looking-at (format "[%s%c%c]"
4709 table-cell-horizontal-chars
4710 table-cell-vertical-char
4711 table-cell-intersection-char))))
4712 (if columnp (setcar coord (1- (car coord)))
4713 (setcdr coord (1- (cdr coord))))
4714 (if (< (if columnp (car coord) (cdr coord)) 0)
4715 (throw 'error nil)))
4716 (if (table--probe-cell)
4717 (throw 'end (table--cell-list-to-coord-list (if columnp
4718 (table--vertical-cell-list t nil 'left)
4719 (table--horizontal-cell-list t nil 'top))))
4720 (table--goto-coordinate (table--offset-coordinate coord (if columnp '(0 . 1) '(1 . 0)))
4721 'no-extension 'no-tab-expansion)
4722 (if (table--probe-cell)
4723 (throw 'end (table--cell-list-to-coord-list (if columnp
4724 (table--vertical-cell-list t nil 'left)
4725 (table--horizontal-cell-list t nil 'top)))))))))
4726 (table--goto-coordinate current-coordinate)
4728 (error "Table not found")))))
4730 (defun table--min-coord-list (coord-list)
4731 "Return minimum cell dimension of COORD-LIST.
4732 COORD-LIST is a list of coordinate pairs (lu-coord . rb-coord), where
4733 each pair in the list represents a cell. lu-coord is the left upper
4734 coordinate of a cell and rb-coord is the right bottom coordinate of a
4735 cell. A coordinate is a pair of x and y axis coordinate values. The
4736 return value is a cons cell (min-w . min-h), where min-w and min-h are
4737 respectively the minimum width and the minimum height of all the cells
4739 (if (null coord-list) nil
4740 (let ((min-width 134217727)
4741 (min-height 134217727))
4743 (let* ((coord (prog1 (car coord-list) (setq coord-list (cdr coord-list))))
4744 (width (- (cadr coord) (caar coord)))
4745 (height (1+ (- (cddr coord) (cdar coord)))))
4746 (if (< width min-width) (setq min-width width))
4747 (if (< height min-height) (setq min-height height))))
4748 (cons min-width min-height))))
4750 (defun table--cell-can-split-horizontally-p ()
4751 "Test if a cell can split at current location horizontally."
4752 (and (not buffer-read-only)
4753 (let ((point-x (car (table--get-coordinate))))
4754 (table-recognize-cell 'force)
4755 (and (> point-x (car table-cell-info-lu-coordinate))
4756 (<= point-x (1- (car table-cell-info-rb-coordinate)))))))
4758 (defun table--cell-can-split-vertically-p ()
4759 "Test if a cell can split at current location vertically."
4760 (and (not buffer-read-only)
4761 (let ((point-y (cdr (table--get-coordinate))))
4762 (table-recognize-cell 'force)
4763 (and (> point-y (cdr table-cell-info-lu-coordinate))
4764 (<= point-y (cdr table-cell-info-rb-coordinate))))))
4766 (defun table--cell-can-span-p (direction)
4767 "Test if the current cell can span to DIRECTION."
4768 (table-recognize-cell 'force)
4769 (and (not buffer-read-only)
4771 ;; get two adjacent cells from each corner
4772 (let ((cell (save-excursion
4774 (table--goto-coordinate
4775 (cons (cond ((eq direction 'right) (1+ (car table-cell-info-rb-coordinate)))
4776 ((eq direction 'left) (1- (car table-cell-info-lu-coordinate)))
4777 (t (car table-cell-info-lu-coordinate)))
4778 (cond ((eq direction 'above) (- (cdr table-cell-info-lu-coordinate) 2))
4779 ((eq direction 'below) (+ (cdr table-cell-info-rb-coordinate) 2))
4780 (t (cdr table-cell-info-lu-coordinate)))) 'no-extension)
4781 (table--probe-cell))))
4782 (cell2 (save-excursion
4784 (table--goto-coordinate
4785 (cons (cond ((eq direction 'right) (1+ (car table-cell-info-rb-coordinate)))
4786 ((eq direction 'left) (1- (car table-cell-info-lu-coordinate)))
4787 (t (car table-cell-info-rb-coordinate)))
4788 (cond ((eq direction 'above) (- (cdr table-cell-info-lu-coordinate) 2))
4789 ((eq direction 'below) (+ (cdr table-cell-info-rb-coordinate) 2))
4790 (t (cdr table-cell-info-rb-coordinate)))) 'no-extension)
4791 (table--probe-cell)))))
4792 ;; make sure the two cells exist, and they are identical, that cell's size matches the current one
4795 (if (or (eq direction 'right) (eq direction 'left))
4796 (and (= (cdr (table--get-coordinate (car cell)))
4797 (cdr table-cell-info-lu-coordinate))
4798 (= (cdr (table--get-coordinate (cdr cell)))
4799 (cdr table-cell-info-rb-coordinate)))
4800 (and (= (car (table--get-coordinate (car cell)))
4801 (car table-cell-info-lu-coordinate))
4802 (= (car (table--get-coordinate (cdr cell)))
4803 (car table-cell-info-rb-coordinate))))))))
4805 (defun table--cell-insert-char (char &optional overwrite)
4806 "Insert CHAR inside a table cell."
4807 (let ((delete-selection-p (and (boundp 'delete-selection-mode)
4808 delete-selection-mode
4809 transient-mark-mode mark-active
4810 (not buffer-read-only)))
4811 (mark-coordinate (table--transcoord-table-to-cache (table--get-coordinate (mark t)))))
4812 (table-with-cache-buffer
4813 (and delete-selection-p
4814 (>= (car mark-coordinate) 0)
4815 (<= (car mark-coordinate) table-cell-info-width)
4816 (>= (cdr mark-coordinate) 0)
4817 (<= (cdr mark-coordinate) table-cell-info-height)
4819 (delete-region (point) (table--goto-coordinate mark-coordinate))))
4821 (let ((coordinate (table--get-coordinate)))
4822 (setq table-inhibit-auto-fill-paragraph t)
4823 (if (>= (car coordinate) table-cell-info-width)
4824 (if (>= (cdr coordinate) (1- table-cell-info-height))
4833 (if (not (eq char ?\s))
4834 (if char (insert char))
4835 (if (not (looking-at "\\s *$"))
4836 (if (and table-fixed-width-mode
4840 (looking-at (concat "\\("
4841 (regexp-quote (char-to-string table-word-continuation-char))
4844 (replace-match " " nil nil nil 1))
4846 (let ((coordinate (table--get-coordinate)))
4847 (if (< (car coordinate) table-cell-info-width)
4848 (move-to-column (1+ (car coordinate)) t)
4849 (insert (make-string (forward-line 1) ?\n))
4850 (unless (bolp) (insert ?\n))))
4851 (setq table-inhibit-auto-fill-paragraph t))
4853 (let ((o-point (point)))
4857 (forward-paragraph -1)
4858 (= o-point (point)))
4862 (setq o-point (point))
4864 (forward-paragraph -1)
4865 (= o-point (point)))))
4866 (insert ?\n)))))))))
4868 (defun table--finish-delayed-tasks ()
4869 "Finish all outstanding delayed tasks."
4870 (if table-update-timer
4871 (table--update-cell 'now))
4872 (if table-widen-timer
4873 (table--update-cell-widened 'now))
4874 (if table-heighten-timer
4875 (table--update-cell-heightened 'now)))
4877 (defmacro table--log (&rest body)
4878 "Debug logging macro."
4879 `(with-current-buffer (get-buffer-create "log")
4880 (goto-char (point-min))
4881 (let ((standard-output (current-buffer)))
4884 (defun table--measure-max-width (&optional unlimited)
4885 "Return maximum width of current buffer.
4886 Normally the current buffer is expected to be already the cache
4887 buffer. The width excludes following spaces at the end of each line.
4888 Unless UNLIMITED is non-nil minimum return value is 1."
4891 (goto-char (point-min))
4894 ;; do not count the following white spaces
4895 (re-search-forward "\\s *$")
4896 (goto-char (match-beginning 0))
4897 (if (> (current-column) width)
4898 (setq width (current-column)))
4904 (defun table--cell-to-coord (cell)
4905 "Create a cell coordinate pair from cell location pair."
4907 (cons (table--get-coordinate (car cell))
4908 (table--get-coordinate (cdr cell)))
4911 (defun table--cell-list-to-coord-list (cell-list)
4912 "Create and return a coordinate list that corresponds to CELL-LIST.
4913 CELL-LIST is a list of location pairs (lu . rb), where each pair
4914 represents a cell in the list. lu is the left upper location and rb
4915 is the right bottom location of a cell. The return value is a list of
4916 coordinate pairs (lu-coord . rb-coord), where lu-coord is the left
4917 upper coordinate and rb-coord is the right bottom coordinate of a
4921 (let ((cell (prog1 (car cell-list) (setq cell-list (cdr cell-list)))))
4923 (cons (table--cell-to-coord cell) coord-list))))
4924 (nreverse coord-list)))
4926 (defun table--test-cell-list (&optional horizontal reverse first-only pivot)
4927 "For testing `table--vertical-cell-list' and `table--horizontal-cell-list'."
4928 (let* ((current-coordinate (table--get-coordinate))
4929 (cell-list (if horizontal
4930 (table--horizontal-cell-list reverse first-only pivot)
4931 (table--vertical-cell-list reverse first-only pivot)))
4934 (let* ((cell (if first-only (prog1 cell-list (setq cell-list nil))
4935 (prog1 (car cell-list) (setq cell-list (cdr cell-list)))))
4936 (dig1-str (format "%1d" (prog1 (% count 10) (setq count (1+ count))))))
4937 (goto-char (car cell))
4938 (table-with-cache-buffer
4939 (while (re-search-forward "." nil t)
4940 (replace-match dig1-str nil nil))
4941 (setq table-inhibit-auto-fill-paragraph t))
4942 (table--finish-delayed-tasks)))
4943 (table--goto-coordinate current-coordinate)))
4945 (defun table--vertical-cell-list (&optional top-to-bottom first-only pivot internal-dir internal-list internal-px)
4946 "Return a vertical cell list from the table.
4947 The return value represents a list of cells including the current cell
4948 that align vertically. Each element of the list is a cons cell (lu
4949 . rb) where lu is the cell's left upper location and rb is the cell's
4950 right bottom location. The cell order in the list is from bottom to
4951 top of the table. If optional argument TOP-TO-BOTTOM is non-nil the
4952 order is reversed as from top to bottom of the table. If optional
4953 argument FIRST-ONLY is non-nil the return value is not a list of cells
4954 but a single cons cell that is the first cell of the list, if the list
4955 had been created. If optional argument PIVOT is a symbol `left' the
4956 vertical cell search is aligned with the left edge of the current
4957 cell, otherwise aligned with the right edge of the current cell. The
4958 arguments INTERNAL-DIR, INTERNAL-LIST and INTERNAL-PX are internal use
4959 only and must not be specified."
4961 (let* ((cell (table--probe-cell))
4962 (lu-coordinate (table--get-coordinate (car cell)))
4963 (rb-coordinate (table--get-coordinate (cdr cell)))
4964 (px (or internal-px (car (if (eq pivot 'left) lu-coordinate rb-coordinate))))
4965 (ty (- (cdr lu-coordinate) 2))
4966 (by (+ (cdr rb-coordinate) 2)))
4967 ;; in case of finding the first cell, get the last adding item on the list
4968 (if (and (null internal-dir) first-only) (setq top-to-bottom (null top-to-bottom)))
4969 ;; travel up and process as recursion traces back (reverse order)
4971 (or (eq internal-dir 'up) (null internal-dir))
4972 (table--goto-coordinate (cons px (if top-to-bottom by ty)) 'no-extension 'no-tab-expansion)
4973 (setq internal-list (table--vertical-cell-list top-to-bottom first-only nil 'up nil px)))
4974 ;; return the last cell or add this cell to the list
4975 (if first-only (or internal-list cell)
4976 (setq internal-list (if cell (cons cell internal-list) internal-list))
4977 ;; travel down and process as entering each recursion (forward order)
4979 (or (eq internal-dir 'down) (null internal-dir))
4980 (table--goto-coordinate (cons px (if top-to-bottom ty by)) 'no-extension 'no-tab-expansion)
4981 (setq internal-list (table--vertical-cell-list top-to-bottom nil nil 'down internal-list px)))
4982 ;; return the result
4985 (defun table--horizontal-cell-list (&optional left-to-right first-only pivot internal-dir internal-list internal-py)
4986 "Return a horizontal cell list from the table.
4987 The return value represents a list of cells including the current cell
4988 that align horizontally. Each element of the list is a cons cells (lu
4989 . rb) where lu is the cell's left upper location and rb is the cell's
4990 right bottom location. The cell order in the list is from right to
4991 left of the table. If optional argument LEFT-TO-RIGHT is non-nil the
4992 order is reversed as from left to right of the table. If optional
4993 argument FIRST-ONLY is non-nil the return value is not a list of cells
4994 but a single cons cell that is the first cell of the list, if the
4995 list had been created. If optional argument PIVOT is a symbol `top'
4996 the horizontal cell search is aligned with the top edge of the current
4997 cell, otherwise aligned with the bottom edge of the current cell. The
4998 arguments INTERNAL-DIR, INTERNAL-LIST and INTERNAL-PY are internal use
4999 only and must not be specified."
5001 (let* ((cell (table--probe-cell))
5002 (lu-coordinate (table--get-coordinate (car cell)))
5003 (rb-coordinate (table--get-coordinate (cdr cell)))
5004 (py (or internal-py (if (eq pivot 'top) (cdr lu-coordinate) (1+ (cdr rb-coordinate)))))
5005 (lx (1- (car lu-coordinate)))
5006 (rx (1+ (car rb-coordinate))))
5007 ;; in case of finding the first cell, get the last adding item on the list
5008 (if (and (null internal-dir) first-only) (setq left-to-right (null left-to-right)))
5009 ;; travel left and process as recursion traces back (reverse order)
5011 (or (eq internal-dir 'left) (null internal-dir))
5012 (table--goto-coordinate (cons (if left-to-right rx lx) py) 'no-extension 'no-tab-expansion)
5013 (setq internal-list (table--horizontal-cell-list left-to-right first-only nil 'left nil py)))
5014 ;; return the last cell or add this cell to the list
5015 (if first-only (or internal-list cell)
5016 (setq internal-list (if cell (cons cell internal-list) internal-list))
5017 ;; travel right and process as entering each recursion (forward order)
5019 (or (eq internal-dir 'right) (null internal-dir))
5020 (table--goto-coordinate (cons (if left-to-right lx rx) py) 'no-extension 'no-tab-expansion)
5021 (setq internal-list (table--horizontal-cell-list left-to-right nil nil 'right internal-list py)))
5022 ;; return the result
5025 (defun table--point-in-cell-p (&optional location)
5026 "Return t when point is in a valid table cell in the current buffer.
5027 When optional LOCATION is provided the test is performed at that location."
5028 (and (table--at-cell-p (or location (point)))
5031 (goto-char location)
5032 (table--probe-cell))
5033 (table--probe-cell))))
5035 (defun table--region-in-cell-p (beg end)
5036 "Return t when location BEG and END are in a valid table cell in the current buffer."
5037 (and (table--at-cell-p (min beg end))
5039 (let ((cell-beg (progn (goto-char beg) (table--probe-cell))))
5041 (equal cell-beg (progn (goto-char end) (table--probe-cell))))))))
5043 (defun table--at-cell-p (position &optional object at-column)
5044 "Returns non-nil if POSITION has table-cell property in OBJECT.
5045 OBJECT is optional and defaults to the current buffer.
5046 If POSITION is at the end of OBJECT, the value is nil."
5047 (if (and at-column (stringp object))
5048 (setq position (table--str-index-at-column object position)))
5049 (get-text-property position 'table-cell object))
5051 (defun table--probe-cell-left-up ()
5052 "Probe left up corner pattern of a cell.
5053 If it finds a valid corner returns a position otherwise returns nil.
5054 The position is the location before the first cell character.
5055 Focus only on the corner pattern. Further cell validity check is required."
5057 (let ((vertical-str (regexp-quote (char-to-string table-cell-vertical-char)))
5058 (intersection-str (regexp-quote (char-to-string table-cell-intersection-char)))
5059 (v-border (format "[%c%c]" table-cell-vertical-char table-cell-intersection-char))
5060 (h-border (format "[%s%c]" table-cell-horizontal-chars table-cell-intersection-char))
5061 (limit (save-excursion (beginning-of-line) (point))))
5064 (catch 'retry-horizontal
5065 (if (not (search-backward-regexp v-border limit t))
5068 (let ((column (current-column)))
5070 (catch 'retry-vertical
5071 (if (zerop (forward-line -1)) nil (throw 'end nil))
5072 (move-to-column column)
5073 (while (and (looking-at vertical-str)
5074 (= column (current-column)))
5075 (if (zerop (forward-line -1)) nil (throw 'end nil))
5076 (move-to-column column))
5078 ((/= column (current-column))
5080 ((looking-at (concat intersection-str h-border))
5082 (move-to-column column)
5084 (throw 'end (point)))
5085 ((looking-at intersection-str)
5086 (throw 'retry-vertical nil))
5087 (t (throw 'retry-horizontal nil)))))))))))))
5089 (defun table--probe-cell-right-bottom ()
5090 "Probe right bottom corner pattern of a cell.
5091 If it finds a valid corner returns a position otherwise returns nil.
5092 The position is the location after the last cell character.
5093 Focus only on the corner pattern. Further cell validity check is required."
5095 (let ((vertical-str (regexp-quote (char-to-string table-cell-vertical-char)))
5096 (intersection-str (regexp-quote (char-to-string table-cell-intersection-char)))
5097 (v-border (format "[%c%c]" table-cell-vertical-char table-cell-intersection-char))
5098 (h-border (format "[%s%c]" table-cell-horizontal-chars table-cell-intersection-char))
5099 (limit (save-excursion (end-of-line) (point))))
5102 (catch 'retry-horizontal
5103 (if (not (search-forward-regexp v-border limit t))
5107 (let ((column (current-column)))
5109 (catch 'retry-vertical
5110 (while (and (looking-at vertical-str)
5111 (= column (current-column)))
5112 (if (and (zerop (forward-line 1)) (zerop (current-column))) nil (throw 'end nil))
5113 (move-to-column column))
5115 ((/= column (current-column))
5117 ((save-excursion (forward-char -1) (looking-at (concat h-border intersection-str)))
5119 (and (zerop (forward-line -1))
5120 (move-to-column column)
5121 (looking-at v-border)
5122 (throw 'end (point))))
5124 (throw 'retry-horizontal nil))
5125 ((looking-at intersection-str)
5126 (if (and (zerop (forward-line 1)) (zerop (current-column))) nil (throw 'end nil))
5127 (move-to-column column)
5128 (throw 'retry-vertical nil))
5129 (t (throw 'retry-horizontal nil)))))))))))))
5131 (defun table--editable-cell-p (&optional abort-on-error)
5132 (and (not buffer-read-only)
5133 (get-text-property (point) 'table-cell)))
5135 (defun table--probe-cell (&optional abort-on-error)
5136 "Probes a table cell around the point.
5137 Searches for the left upper corner and the right bottom corner of a table
5138 cell which contains the current point location.
5140 The result is a cons cell (left-upper . right-bottom) where
5141 the left-upper is the position before the cell's left upper corner character,
5142 the right-bottom is the position after the cell's right bottom corner character.
5144 When it fails to find either one of the cell corners it returns nil or
5145 signals error if the optional ABORT-ON-ERROR is non-nil."
5147 (border (format "^[%s%c%c]+$"
5148 table-cell-horizontal-chars
5149 table-cell-vertical-char
5150 table-cell-intersection-char)))
5151 (if (and (condition-case nil
5153 (and (setq lu (table--probe-cell-left-up))
5154 (setq rb (table--probe-cell-right-bottom))))
5157 (let ((lu-coordinate (table--get-coordinate lu))
5158 (rb-coordinate (table--get-coordinate rb)))
5159 ;; test for valid upper and lower borders
5164 (table--goto-coordinate
5165 (cons (1- (car lu-coordinate))
5166 (1- (cdr lu-coordinate)))))
5168 (table--goto-coordinate
5169 (cons (1+ (car rb-coordinate))
5170 (1- (cdr lu-coordinate)))))))
5175 (table--goto-coordinate
5176 (cons (1- (car lu-coordinate))
5177 (1+ (cdr rb-coordinate)))))
5179 (table--goto-coordinate
5180 (cons (1+ (car rb-coordinate))
5181 (1+ (cdr rb-coordinate))))))))))
5184 (error "Table cell not found")
5187 (defun table--insert-rectangle (rectangle)
5188 "Insert text of RECTANGLE with upper left corner at point.
5189 Same as insert-rectangle except that mark operation is eliminated."
5190 (let ((lines rectangle)
5191 (insertcolumn (current-column))
5197 (or (bolp) (insert ?\n))
5198 (move-to-column insertcolumn t)))
5200 (insert (car lines))
5201 (setq lines (cdr lines)))))
5203 (defun table--put-cell-property (cell)
5204 "Put standard text properties to the CELL.
5205 The CELL is a cons cell (left-upper . right-bottom) where the
5206 left-upper is the position before the cell's left upper corner
5207 character, the right-bottom is the position after the cell's right
5208 bottom corner character."
5209 (let ((lu (table--get-coordinate (car cell)))
5210 (rb (table--get-coordinate (cdr cell))))
5212 (while (<= (cdr lu) (cdr rb))
5213 (let ((beg (table--goto-coordinate lu 'no-extension))
5214 (end (table--goto-coordinate (cons (car rb) (cdr lu)))))
5215 (table--put-cell-line-property beg end))
5216 (setcdr lu (1+ (cdr lu))))
5217 (table--put-cell-justify-property cell table-cell-info-justify)
5218 (table--put-cell-valign-property cell table-cell-info-valign))))
5220 (defun table--put-cell-line-property (beg end &optional object)
5221 "Put standard text properties to a line of a cell.
5222 BEG is the beginning of the line that is the location between left
5223 cell border character and the first content character. END is the end
5224 of the line that is the location between the last content character
5225 and the right cell border character."
5226 (table--put-cell-content-property beg end object)
5227 (table--put-cell-keymap-property end (1+ end) object)
5228 (table--put-cell-indicator-property end (1+ end) object)
5229 (table--put-cell-rear-nonsticky end (1+ end) object))
5231 (defun table--put-cell-content-property (beg end &optional object)
5232 "Put cell content text properties."
5233 (table--put-cell-keymap-property beg end object)
5234 (table--put-cell-indicator-property beg end object)
5235 (table--put-cell-face-property beg end object)
5236 (table--put-cell-point-entered/left-property beg end object))
5238 (defun table--put-cell-indicator-property (beg end &optional object)
5239 "Put cell property which indicates that the location is within a table cell."
5240 (put-text-property beg end 'table-cell t object)
5241 (put-text-property beg end 'yank-handler table-yank-handler object))
5243 (defun table--put-cell-face-property (beg end &optional object)
5244 "Put cell face property."
5245 (put-text-property beg end 'face 'table-cell object))
5247 (defun table--put-cell-keymap-property (beg end &optional object)
5248 "Put cell keymap property."
5249 (put-text-property beg end 'keymap 'table-cell-map object))
5251 (defun table--put-cell-rear-nonsticky (beg end &optional object)
5252 "Put rear-nonsticky property."
5253 (put-text-property beg end 'rear-nonsticky t object))
5255 (defun table--put-cell-point-entered/left-property (beg end &optional object)
5256 "Put point-entered/left property."
5257 (put-text-property beg end 'point-entered 'table--point-entered-cell-function object)
5258 (put-text-property beg end 'point-left 'table--point-left-cell-function object))
5260 (defun table--remove-cell-properties (beg end &optional object)
5261 "Remove all cell properties.
5262 If OBJECT is non-nil cell properties are removed from the OBJECT
5263 instead of the current buffer and returns the OBJECT."
5265 (let ((next (next-single-property-change beg 'table-cell object end)))
5266 (if (get-text-property beg 'table-cell object)
5267 (remove-text-properties beg next
5281 (defun table--update-cell-face ()
5282 "Update cell face according to the current mode."
5283 (if (featurep 'xemacs)
5284 (set-face-property 'table-cell 'underline table-fixed-width-mode)
5285 (set-face-inverse-video-p 'table-cell table-fixed-width-mode)))
5287 (table--update-cell-face)
5289 (defun table--get-property (cell property)
5290 "Get CELL's PROPERTY."
5291 (or (get-text-property (car cell) property)
5292 (get-text-property (1- (cdr cell)) property)))
5294 (defun table--get-cell-justify-property (cell)
5295 "Get cell's justify property."
5296 (table--get-property cell 'table-justify))
5298 (defun table--get-cell-valign-property (cell)
5299 "Get cell's vertical alignment property."
5300 (table--get-property cell 'table-valign))
5302 (defun table--put-property (cell property value)
5303 "Put CELL's PROPERTY the VALUE."
5304 (let ((beg (car cell))
5306 (put-text-property beg (1+ beg) property value)
5307 (put-text-property (1- end) end property value)))
5309 (defun table--put-cell-justify-property (cell justify)
5310 "Put cell's justify property."
5311 (table--put-property cell 'table-justify justify))
5313 (defun table--put-cell-valign-property (cell valign)
5314 "Put cell's vertical alignment property."
5315 (table--put-property cell 'table-valign valign))
5317 (defun table--point-entered-cell-function (&optional old-point new-point)
5318 "Point has entered a cell.
5319 Refresh the menu bar."
5320 ;; Avoid calling point-motion-hooks recursively.
5321 (let ((inhibit-point-motion-hooks t))
5322 (unless table-cell-entered-state
5323 (setq table-cell-entered-state t)
5324 (setq table-mode-indicator t)
5325 (force-mode-line-update)
5326 (table--warn-incompatibility)
5327 (run-hooks 'table-point-entered-cell-hook))))
5329 (defun table--point-left-cell-function (&optional old-point new-point)
5330 "Point has left a cell.
5331 Refresh the menu bar."
5332 ;; Avoid calling point-motion-hooks recursively.
5333 (let ((inhibit-point-motion-hooks t))
5334 (when table-cell-entered-state
5335 (setq table-cell-entered-state nil)
5336 (setq table-mode-indicator nil)
5337 (force-mode-line-update)
5338 (run-hooks 'table-point-left-cell-hook))))
5340 (defun table--warn-incompatibility ()
5341 "If called from interactive operation warn the know incompatibilities.
5342 This feature is disabled when `table-disable-incompatibility-warning'
5343 is non-nil. The warning is done only once per session for each item."
5344 (unless (and table-disable-incompatibility-warning
5345 (not (called-interactively-p 'interactive)))
5346 (cond ((and (featurep 'xemacs)
5347 (not (get 'table-disable-incompatibility-warning 'xemacs)))
5348 (put 'table-disable-incompatibility-warning 'xemacs t)
5349 (display-warning 'table
5353 Table package mostly works fine under XEmacs, however, due to the
5354 peculiar implementation of text property under XEmacs, cell splitting
5355 and any undo operation of table exhibit some known strange problems,
5356 such that a border characters dissolve into adjacent cells. Please be
5361 ((and (boundp 'flyspell-mode)
5363 (not (get 'table-disable-incompatibility-warning 'flyspell)))
5364 (put 'table-disable-incompatibility-warning 'flyspell t)
5365 (display-warning 'table
5369 Flyspell minor mode is known to be incompatible with this table
5370 package. The flyspell version 1.5d at URL `http://kaolin.unice.fr/~serrano'
5371 works better than the previous versions however not fully compatible.
5377 (defun table--cell-blank-str (&optional n)
5378 "Return blank table cell string of length N."
5379 (let ((str (make-string (or n 1) ?\s)))
5380 (table--put-cell-content-property 0 (length str) str)
5383 (defun table--remove-eol-spaces (beg end &optional bol force)
5384 "Remove spaces at the end of each line in the BEG END region of the current buffer.
5385 When optional BOL is non-nil spaces at the beginning of line are
5386 removed. When optional FORCE is non-nil removal operation is enforced
5387 even when point is within the removal area."
5392 (let ((saved-point (point-marker))
5393 (end-marker (copy-marker end)))
5396 (while (if bol (re-search-forward "^\\( +\\)" end-marker t)
5397 (re-search-forward "\\( +\\)$" end-marker t))
5398 ;; avoid removal that causes the saved point to lose its location.
5400 (<= (match-beginning 1) saved-point)
5401 (<= saved-point (match-end 1))
5403 (delete-region saved-point (match-end 1))
5404 (delete-region (match-beginning 1) (match-end 1)))))
5405 (set-marker saved-point nil)
5406 (set-marker end-marker nil)))
5408 (defun table--fill-region (beg end &optional col justify)
5409 "Fill paragraphs in table cell cache.
5410 Current buffer must already be set to the cache buffer."
5411 (let ((fill-column (or col table-cell-info-width))
5413 (enable-kinsoku nil)
5414 (adaptive-fill-mode nil)
5415 (marker-beg (copy-marker beg))
5416 (marker-end (copy-marker end))
5417 (marker-point (point-marker)))
5418 (setq justify (or justify table-cell-info-justify))
5420 (not (eq justify 'left))
5421 (not (featurep 'xemacs))
5422 (set-marker-insertion-type marker-point t))
5423 (table--remove-eol-spaces (point-min) (point-max))
5424 (if table-fixed-width-mode
5425 (table--fill-region-strictly marker-beg marker-end)
5426 (let ((paragraph-start table-paragraph-start))
5427 (fill-region marker-beg marker-end justify nil t)))
5428 (goto-char marker-point)
5429 (set-marker marker-beg nil)
5430 (set-marker marker-end nil)
5431 (set-marker marker-point nil)))
5433 (defun table--fill-region-strictly (beg end)
5434 "Fill region strictly so that no line exceeds fill-column.
5435 When a word exceeds fill-column the word is chopped into pieces. The
5436 chopped location is indicated with table-word-continuation-char."
5437 (or (and (markerp beg) (markerp end))
5439 (if (< fill-column 2)
5440 (setq fill-column 2))
5441 ;; first remove all continuation characters.
5443 (while (re-search-forward (concat
5444 (format "[^%c ]\\(" table-word-continuation-char)
5445 (regexp-quote (char-to-string table-word-continuation-char))
5448 (delete-region (match-beginning 1) (match-end 1)))
5449 ;; then fill as normal
5450 (let ((paragraph-start table-paragraph-start))
5451 (fill-region beg end nil nil t))
5454 (while (let ((col (move-to-column fill-column t)))
5456 ((and (<= col fill-column)
5458 (delete-region (match-beginning 0) (match-end 0))
5459 (and (zerop (forward-line 1))
5461 (t (forward-char -1)
5462 (insert-before-markers (if (equal (char-before) ?\s) ?\s table-word-continuation-char)
5466 (defun table--goto-coordinate (coordinate &optional no-extension no-tab-expansion)
5467 "Move point to the given COORDINATE and return the location.
5468 When optional NO-EXTENSION is non-nil and the specified coordinate is
5469 not reachable returns nil otherwise the blanks are added if necessary
5470 to achieve the goal coordinate and returns the goal point. It
5471 intentionally does not preserve the original point in case it fails
5472 achieving the goal. When optional NO-TAB-EXPANSION is non-nil and the
5473 goad happens to be in a tab character the tab is not expanded but the
5474 goal ends at the beginning of tab."
5475 (if (or (null coordinate)
5476 (< (car coordinate) 0)
5477 (< (cdr coordinate) 0)) nil
5478 (goto-char (point-min))
5479 (let ((x (car coordinate))
5480 (more-lines (forward-line (cdr coordinate))))
5482 (if (zerop (current-column)) nil
5487 (setq more-lines (1+ more-lines))))
5488 (if (zerop more-lines) nil
5489 (newline more-lines))
5491 (if (/= (move-to-column x) x)
5492 (if (> (move-to-column x) x)
5493 (if no-tab-expansion
5495 (while (> (move-to-column x) x)
5498 (throw 'exit (move-to-column x t)))
5500 (move-to-column x t))
5503 (defun table--copy-coordinate (coord)
5504 "Copy coordinate in a new cons cell."
5505 (cons (car coord) (cdr coord)))
5507 (defun table--get-coordinate (&optional where)
5508 "Return the coordinate of point in current buffer.
5509 When optional WHERE is given it returns the coordinate of that
5510 location instead of point in the current buffer. It does not move the
5513 (if where (goto-char where))
5514 (cons (current-column)
5515 (table--current-line))))
5517 (defun table--current-line (&optional location)
5518 "Return zero based line count of current line or if non-nil LOCATION line."
5520 (if location (goto-char location))
5522 (count-lines (point-min) (point))))
5524 (defun table--transcoord-table-to-cache (&optional coordinate)
5525 "Transpose COORDINATE from table coordinate system to cache coordinate system.
5526 When COORDINATE is omitted or nil the point in current buffer is assumed in place."
5527 (table--offset-coordinate
5528 (or coordinate (table--get-coordinate))
5529 table-cell-info-lu-coordinate
5532 (defun table--transcoord-cache-to-table (&optional coordinate)
5533 "Transpose COORDINATE from cache coordinate system to table coordinate system.
5534 When COORDINATE is omitted or nil the point in current buffer is assumed in place."
5535 (table--offset-coordinate
5536 (or coordinate (table--get-coordinate))
5537 table-cell-info-lu-coordinate))
5539 (defun table--offset-coordinate (coordinate offset &optional negative)
5540 "Return the offseted COORDINATE by OFFSET.
5541 When optional NEGATIVE is non-nil offsetting direction is negative."
5542 (cons (if negative (- (car coordinate) (car offset))
5543 (+ (car coordinate) (car offset)))
5544 (if negative (- (cdr coordinate) (cdr offset))
5545 (+ (cdr coordinate) (cdr offset)))))
5547 (defun table--char-in-str-at-column (str column)
5548 "Return the character in STR at COLUMN location.
5549 When COLUMN is out of range it returns null character."
5550 (let ((idx (table--str-index-at-column str column)))
5551 (if idx (aref str idx)
5554 (defun table--str-index-at-column (str column)
5555 "Return the character index in STR that corresponds to COLUMN location.
5556 It returns COLUMN unless STR contains some wide characters."
5560 (while (and (< col column) (< idx len))
5561 (setq col (+ col (char-width (aref str idx))))
5562 (setq idx (1+ idx)))
5567 (defun table--set-timer (seconds func args)
5568 "Generic wrapper for setting up a timer."
5569 (if (featurep 'xemacs)
5570 ;; the picky xemacs refuses to accept zero
5571 (add-timeout (if (zerop seconds) 0.01 seconds) func args nil)
5572 ;;(run-at-time seconds nil func args)))
5573 ;; somehow run-at-time causes strange problem under Emacs 20.7
5574 ;; this problem does not show up under Emacs 21.0.90
5575 (run-with-idle-timer seconds nil func args)))
5577 (defun table--cancel-timer (timer)
5578 "Generic wrapper for canceling a timer."
5579 (if (featurep 'xemacs)
5580 (disable-timeout timer)
5581 (cancel-timer timer)))
5583 (defun table--get-last-command ()
5584 "Generic wrapper for getting the real last command."
5585 (if (boundp 'real-last-command)
5589 (run-hooks 'table-load-hook)
5593 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5594 ;; Local Variables: ***
5595 ;; time-stamp-line-limit: 16 ***
5596 ;; time-stamp-start: ";; Revised:[ \t]+" ***
5597 ;; time-stamp-end: "$" ***
5598 ;; time-stamp-format: "%3a %3b %02d %:y %02H:%02M:%02S (%Z)" ***
5600 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5602 ;; arch-tag: 0d69b03e-aa5f-4e72-8806-5727217617e0
5603 ;;; table.el ends here