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