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