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