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