ox-html: Prefer custom ID for links in table of contents
[org-mode.git] / lisp / org-table.el
blob5f0c9dab23773e92c54300a00c92c42ca883db33
1 ;;; org-table.el --- The table editor for Org-mode
3 ;; Copyright (C) 2004-2015 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains the table editor and spreadsheet for Org-mode.
29 ;; Watch out: Here we are talking about two different kind of tables.
30 ;; Most of the code is for the tables created with the Org-mode table editor.
31 ;; Sometimes, we talk about tables created and edited with the table.el
32 ;; Emacs package. We call the former org-type tables, and the latter
33 ;; table.el-type tables.
35 ;;; Code:
37 (eval-when-compile
38 (require 'cl))
39 (require 'org)
41 (declare-function org-element-at-point "org-element" ())
42 (declare-function org-element-contents "org-element" (element))
43 (declare-function org-element-extract-element "org-element" (element))
44 (declare-function org-element-interpret-data "org-element" (data))
45 (declare-function org-element-lineage "org-element"
46 (blob &optional types with-self))
47 (declare-function org-element-map "org-element"
48 (data types fun
49 &optional info first-match no-recursion with-affiliated))
50 (declare-function org-element-property "org-element" (property element))
52 (declare-function org-export-string-as "ox"
53 (string backend &optional body-only ext-plist))
54 (declare-function org-export-create-backend "ox")
55 (declare-function org-export-get-backend "ox" (name))
57 (declare-function calc-eval "calc" (str &optional separator &rest args))
59 (defvar orgtbl-mode) ; defined below
60 (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
61 (defvar constants-unit-system)
62 (defvar org-table-follow-field-mode)
64 (defvar orgtbl-after-send-table-hook nil
65 "Hook for functions attaching to `C-c C-c', if the table is sent.
66 This can be used to add additional functionality after the table is sent
67 to the receiver position, otherwise, if table is not sent, the functions
68 are not run.")
70 (defvar org-table-TBLFM-begin-regexp "^[ \t]*|.*\n[ \t]*#\\+TBLFM: ")
72 (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
73 "Non-nil means use the optimized table editor version for `orgtbl-mode'.
74 In the optimized version, the table editor takes over all simple keys that
75 normally just insert a character. In tables, the characters are inserted
76 in a way to minimize disturbing the table structure (i.e. in overwrite mode
77 for empty fields). Outside tables, the correct binding of the keys is
78 restored.
80 The default for this option is t if the optimized version is also used in
81 Org-mode. See the variable `org-enable-table-editor' for details. Changing
82 this variable requires a restart of Emacs to become effective."
83 :group 'org-table
84 :type 'boolean)
86 (defcustom orgtbl-radio-table-templates
87 '((latex-mode "% BEGIN RECEIVE ORGTBL %n
88 % END RECEIVE ORGTBL %n
89 \\begin{comment}
90 #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
91 | | |
92 \\end{comment}\n")
93 (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
94 @c END RECEIVE ORGTBL %n
95 @ignore
96 #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
97 | | |
98 @end ignore\n")
99 (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
100 <!-- END RECEIVE ORGTBL %n -->
101 <!--
102 #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
103 | | |
104 -->\n")
105 (org-mode "#+ BEGIN RECEIVE ORGTBL %n
106 #+ END RECEIVE ORGTBL %n
108 #+ORGTBL: SEND %n orgtbl-to-orgtbl :splice nil :skip 0
109 | | |
111 "Templates for radio tables in different major modes.
112 Each template must define lines that will be treated as a comment and that
113 must contain the \"BEGIN RECEIVE ORGTBL %n\" and \"END RECEIVE ORGTBL\"
114 lines where \"%n\" will be replaced with the name of the table during
115 insertion of the template. The transformed table will later be inserted
116 between these lines.
118 The template should also contain a minimal table in a multiline comment.
119 If multiline comments are not possible in the buffer language,
120 you can pack it into a string that will not be used when the code
121 is compiled or executed. Above the table will you need a line with
122 the fixed string \"#+ORGTBL: SEND\", followed by instruction on how to
123 convert the table into a data structure useful in the
124 language of the buffer. Check the manual for the section on
125 \"Translator functions\", and more generally check out
126 http://orgmode.org/manual/Tables-in-arbitrary-syntax.html#Tables-in-arbitrary-syntax
128 All occurrences of %n in a template will be replaced with the name of the
129 table, obtained by prompting the user."
130 :group 'org-table
131 :type '(repeat
132 (list (symbol :tag "Major mode")
133 (string :tag "Format"))))
135 (defgroup org-table-settings nil
136 "Settings for tables in Org-mode."
137 :tag "Org Table Settings"
138 :group 'org-table)
140 (defcustom org-table-default-size "5x2"
141 "The default size for newly created tables, Columns x Rows."
142 :group 'org-table-settings
143 :type 'string)
145 (defcustom org-table-number-regexp
146 "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|[<>]?[-+]?0[xX][0-9a-fA-F.]+\\|[<>]?[-+]?[0-9]+#[0-9a-zA-Z.]+\\|nan\\|[-+u]?inf\\)$"
147 "Regular expression for recognizing numbers in table columns.
148 If a table column contains mostly numbers, it will be aligned to the
149 right. If not, it will be aligned to the left.
151 The default value of this option is a regular expression which allows
152 anything which looks remotely like a number as used in scientific
153 context. For example, all of the following will be considered a
154 number:
155 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
157 Other options offered by the customize interface are more restrictive."
158 :group 'org-table-settings
159 :type '(choice
160 (const :tag "Positive Integers"
161 "^[0-9]+$")
162 (const :tag "Integers"
163 "^[-+]?[0-9]+$")
164 (const :tag "Floating Point Numbers"
165 "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
166 (const :tag "Floating Point Number or Integer"
167 "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
168 (const :tag "Exponential, Floating point, Integer"
169 "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
170 (const :tag "Very General Number-Like, including hex and Calc radix"
171 "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|[<>]?[-+]?0[xX][0-9a-fA-F.]+\\|[<>]?[-+]?[0-9]+#[0-9a-zA-Z.]+\\|nan\\|[-+u]?inf\\)$")
172 (const :tag "Very General Number-Like, including hex and Calc radix, allows comma as decimal mark"
173 "^\\([<>]?[-+^.,0-9]*[0-9][-+^.0-9eEdDx()%]*\\|[<>]?[-+]?0[xX][0-9a-fA-F.]+\\|[<>]?[-+]?[0-9]+#[0-9a-zA-Z.]+\\|nan\\|[-+u]?inf\\)$")
174 (string :tag "Regexp:")))
176 (defcustom org-table-number-fraction 0.5
177 "Fraction of numbers in a column required to make the column align right.
178 In a column all non-white fields are considered. If at least
179 this fraction of fields is matched by `org-table-number-regexp',
180 alignment to the right border applies."
181 :group 'org-table-settings
182 :type 'number)
184 (defgroup org-table-editing nil
185 "Behavior of tables during editing in Org-mode."
186 :tag "Org Table Editing"
187 :group 'org-table)
189 (defcustom org-table-automatic-realign t
190 "Non-nil means automatically re-align table when pressing TAB or RETURN.
191 When nil, aligning is only done with \\[org-table-align], or after column
192 removal/insertion."
193 :group 'org-table-editing
194 :type 'boolean)
196 (defcustom org-table-auto-blank-field t
197 "Non-nil means automatically blank table field when starting to type into it.
198 This only happens when typing immediately after a field motion
199 command (TAB, S-TAB or RET).
200 Only relevant when `org-enable-table-editor' is equal to `optimized'."
201 :group 'org-table-editing
202 :type 'boolean)
204 (defcustom org-table-exit-follow-field-mode-when-leaving-table t
205 "Non-nil means automatically exit the follow mode.
206 When nil, the follow mode will stay on and be active in any table
207 the cursor enters. Since the table follow filed mode messes with the
208 window configuration, it is not recommended to set this variable to nil,
209 except maybe locally in a special file that has mostly tables with long
210 fields."
211 :group 'org-table
212 :version "24.1"
213 :type 'boolean)
215 (defcustom org-table-fix-formulas-confirm nil
216 "Whether the user should confirm when Org fixes formulas."
217 :group 'org-table-editing
218 :version "24.1"
219 :type '(choice
220 (const :tag "with yes-or-no" yes-or-no-p)
221 (const :tag "with y-or-n" y-or-n-p)
222 (const :tag "no confirmation" nil)))
223 (put 'org-table-fix-formulas-confirm
224 'safe-local-variable
225 #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
227 (defcustom org-table-tab-jumps-over-hlines t
228 "Non-nil means tab in the last column of a table with jump over a hline.
229 If a horizontal separator line is following the current line,
230 `org-table-next-field' can either create a new row before that line, or jump
231 over the line. When this option is nil, a new line will be created before
232 this line."
233 :group 'org-table-editing
234 :type 'boolean)
236 (defgroup org-table-calculation nil
237 "Options concerning tables in Org-mode."
238 :tag "Org Table Calculation"
239 :group 'org-table)
241 (defcustom org-table-use-standard-references 'from
242 "Should org-mode work with table references like B3 instead of @3$2?
243 Possible values are:
244 nil never use them
245 from accept as input, do not present for editing
246 t accept as input and present for editing"
247 :group 'org-table-calculation
248 :type '(choice
249 (const :tag "Never, don't even check user input for them" nil)
250 (const :tag "Always, both as user input, and when editing" t)
251 (const :tag "Convert user input, don't offer during editing" from)))
253 (defcustom org-table-copy-increment t
254 "Non-nil means increment when copying current field with \\[org-table-copy-down]."
255 :group 'org-table-calculation
256 :version "25.1"
257 :package-version '(Org . "8.3")
258 :type '(choice
259 (const :tag "Use the difference between the current and the above fields" t)
260 (integer :tag "Use a number" 1)
261 (const :tag "Don't increment the value when copying a field" nil)))
263 (defcustom org-calc-default-modes
264 '(calc-internal-prec 12
265 calc-float-format (float 8)
266 calc-angle-mode deg
267 calc-prefer-frac nil
268 calc-symbolic-mode nil
269 calc-date-format (YYYY "-" MM "-" DD " " Www (" " hh ":" mm))
270 calc-display-working-message t
272 "List with Calc mode settings for use in `calc-eval' for table formulas.
273 The list must contain alternating symbols (Calc modes variables and values).
274 Don't remove any of the default settings, just change the values. Org-mode
275 relies on the variables to be present in the list."
276 :group 'org-table-calculation
277 :type 'plist)
279 (defcustom org-table-duration-custom-format 'hours
280 "Format for the output of calc computations like $1+$2;t.
281 The default value is 'hours, and will output the results as a
282 number of hours. Other allowed values are 'seconds, 'minutes and
283 'days, and the output will be a fraction of seconds, minutes or
284 days."
285 :group 'org-table-calculation
286 :version "24.1"
287 :type '(choice (symbol :tag "Seconds" 'seconds)
288 (symbol :tag "Minutes" 'minutes)
289 (symbol :tag "Hours " 'hours)
290 (symbol :tag "Days " 'days)))
292 (defcustom org-table-formula-field-format "%s"
293 "Format for fields which contain the result of a formula.
294 For example, using \"~%s~\" will display the result within tilde
295 characters. Beware that modifying the display can prevent the
296 field from being used in another formula."
297 :group 'org-table-settings
298 :version "24.1"
299 :type 'string)
301 (defcustom org-table-formula-evaluate-inline t
302 "Non-nil means TAB and RET evaluate a formula in current table field.
303 If the current field starts with an equal sign, it is assumed to be a formula
304 which should be evaluated as described in the manual and in the documentation
305 string of the command `org-table-eval-formula'. This feature requires the
306 Emacs calc package.
307 When this variable is nil, formula calculation is only available through
308 the command \\[org-table-eval-formula]."
309 :group 'org-table-calculation
310 :type 'boolean)
312 (defcustom org-table-formula-use-constants t
313 "Non-nil means interpret constants in formulas in tables.
314 A constant looks like `$c' or `$Grav' and will be replaced before evaluation
315 by the value given in `org-table-formula-constants', or by a value obtained
316 from the `constants.el' package."
317 :group 'org-table-calculation
318 :type 'boolean)
320 (defcustom org-table-formula-constants nil
321 "Alist with constant names and values, for use in table formulas.
322 The car of each element is a name of a constant, without the `$' before it.
323 The cdr is the value as a string. For example, if you'd like to use the
324 speed of light in a formula, you would configure
326 (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
328 and then use it in an equation like `$1*$c'.
330 Constants can also be defined on a per-file basis using a line like
332 #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
333 :group 'org-table-calculation
334 :type '(repeat
335 (cons (string :tag "name")
336 (string :tag "value"))))
338 (defcustom org-table-allow-automatic-line-recalculation t
339 "Non-nil means lines marked with |#| or |*| will be recomputed automatically.
340 Automatically means when TAB or RET or C-c C-c are pressed in the line."
341 :group 'org-table-calculation
342 :type 'boolean)
344 (defcustom org-table-relative-ref-may-cross-hline t
345 "Non-nil means relative formula references may cross hlines.
346 Here are the allowed values:
348 nil Relative references may not cross hlines. They will reference the
349 field next to the hline instead. Coming from below, the reference
350 will be to the field below the hline. Coming from above, it will be
351 to the field above.
352 t Relative references may cross hlines.
353 error An attempt to cross a hline will throw an error.
355 It is probably good to never set this variable to nil, for the sake of
356 portability of tables."
357 :group 'org-table-calculation
358 :type '(choice
359 (const :tag "Allow to cross" t)
360 (const :tag "Stick to hline" nil)
361 (const :tag "Error on attempt to cross" error)))
363 (defcustom org-table-formula-create-columns nil
364 "Non-nil means that evaluation of a field formula can add new
365 columns if an out-of-bounds field is being set."
366 :group 'org-table-calculation
367 :version "25.1"
368 :package-version '(Org . "8.3")
369 :type '(choice
370 (const :tag "Setting an out-of-bounds field generates an error (default)" nil)
371 (const :tag "Setting an out-of-bounds field silently adds columns as needed" t)
372 (const :tag "Setting an out-of-bounds field adds columns as needed, but issues a warning message" warn)
373 (const :tag "When setting an out-of-bounds field, the user is prompted" prompt)))
375 (defgroup org-table-import-export nil
376 "Options concerning table import and export in Org-mode."
377 :tag "Org Table Import Export"
378 :group 'org-table)
380 (defcustom org-table-export-default-format "orgtbl-to-tsv"
381 "Default export parameters for `org-table-export'.
382 These can be overridden for a specific table by setting the
383 TABLE_EXPORT_FORMAT property. See the manual section on orgtbl
384 radio tables for the different export transformations and
385 available parameters."
386 :group 'org-table-import-export
387 :type 'string)
389 (defcustom org-table-convert-region-max-lines 999
390 "Max lines that `org-table-convert-region' will attempt to process.
392 The function can be slow on larger regions; this safety feature
393 prevents it from hanging emacs."
394 :group 'org-table-import-export
395 :type 'integer
396 :version "25.1"
397 :package-version '(Org . "8.3"))
399 (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
400 "Detects a table line marked for automatic recalculation.")
401 (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
402 "Detects a table line marked for automatic recalculation.")
403 (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
404 "Detects a table line marked for automatic recalculation.")
405 (defconst org-table-border-regexp "^[ \t]*[^| \t]"
406 "Searching from within a table (any type) this finds the first line outside the table.")
407 (defvar org-table-last-highlighted-reference nil)
408 (defvar org-table-formula-history nil)
410 (defvar org-table-column-names nil
411 "Alist with column names, derived from the `!' line.")
412 (defvar org-table-column-name-regexp nil
413 "Regular expression matching the current column names.")
414 (defvar org-table-local-parameters nil
415 "Alist with parameter names, derived from the `$' line.")
416 (defvar org-table-named-field-locations nil
417 "Alist with locations of named fields.")
419 (defvar org-table-current-line-types nil
420 "Table row types, non-nil only for the duration of a command.")
421 (defvar org-table-current-begin-line nil
422 "Table begin line, non-nil only for the duration of a command.")
423 (defvar org-table-current-begin-pos nil
424 "Table begin position, non-nil only for the duration of a command.")
425 (defvar org-table-current-ncol nil
426 "Number of columns in table, non-nil only for the duration of a command.")
427 (defvar org-table-dlines nil
428 "Vector of data line line numbers in the current table.")
429 (defvar org-table-hlines nil
430 "Vector of hline line numbers in the current table.")
432 (defconst org-table-range-regexp
433 "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
434 ;; 1 2 3 4 5
435 "Regular expression for matching ranges in formulas.")
437 (defconst org-table-range-regexp2
438 (concat
439 "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
440 "\\.\\."
441 "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
442 "Match a range for reference display.")
444 (defun org-table-colgroup-line-p (line)
445 "Is this a table line colgroup information?"
446 (save-match-data
447 (and (string-match "[<>]\\|&[lg]t;" line)
448 (string-match "\\`[ \t]*|[ \t]*/[ \t]*\\(|[ \t<>0-9|lgt&;]+\\)\\'"
449 line)
450 (not (delq
452 (mapcar
453 (lambda (s)
454 (not (member s '("" "<" ">" "<>" "&lt;" "&gt;" "&lt;&gt;"))))
455 (org-split-string (match-string 1 line) "[ \t]*|[ \t]*")))))))
457 (defun org-table-cookie-line-p (line)
458 "Is this a table line with only alignment/width cookies?"
459 (save-match-data
460 (and (string-match "[<>]\\|&[lg]t;" line)
461 (or (string-match
462 "\\`[ \t]*|[ \t]*/[ \t]*\\(|[ \t<>0-9|lrcgt&;]+\\)\\'" line)
463 (string-match "\\(\\`[ \t<>lrc0-9|gt&;]+\\'\\)" line))
464 (not (delq nil (mapcar
465 (lambda (s)
466 (not (or (equal s "")
467 (string-match
468 "\\`<\\([lrc]?[0-9]+\\|[lrc]\\)>\\'" s)
469 (string-match
470 "\\`&lt;\\([lrc]?[0-9]+\\|[lrc]\\)&gt;\\'"
471 s))))
472 (org-split-string (match-string 1 line)
473 "[ \t]*|[ \t]*")))))))
475 (defconst org-table-translate-regexp
476 (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
477 "Match a reference that needs translation, for reference display.")
479 ;;;###autoload
480 (defun org-table-create-with-table.el ()
481 "Use the table.el package to insert a new table.
482 If there is already a table at point, convert between Org-mode tables
483 and table.el tables."
484 (interactive)
485 (require 'table)
486 (cond
487 ((org-at-table.el-p)
488 (if (y-or-n-p "Convert table to Org-mode table? ")
489 (org-table-convert)))
490 ((org-at-table-p)
491 (when (y-or-n-p "Convert table to table.el table? ")
492 (org-table-align)
493 (org-table-convert)))
494 (t (call-interactively 'table-insert))))
496 ;;;###autoload
497 (defun org-table-create-or-convert-from-region (arg)
498 "Convert region to table, or create an empty table.
499 If there is an active region, convert it to a table, using the function
500 `org-table-convert-region'. See the documentation of that function
501 to learn how the prefix argument is interpreted to determine the field
502 separator.
503 If there is no such region, create an empty table with `org-table-create'."
504 (interactive "P")
505 (if (org-region-active-p)
506 (org-table-convert-region (region-beginning) (region-end) arg)
507 (org-table-create arg)))
509 ;;;###autoload
510 (defun org-table-create (&optional size)
511 "Query for a size and insert a table skeleton.
512 SIZE is a string Columns x Rows like for example \"3x2\"."
513 (interactive "P")
514 (unless size
515 (setq size (read-string
516 (concat "Table size Columns x Rows [e.g. "
517 org-table-default-size "]: ")
518 "" nil org-table-default-size)))
520 (let* ((pos (point))
521 (indent (make-string (current-column) ?\ ))
522 (split (org-split-string size " *x *"))
523 (rows (string-to-number (nth 1 split)))
524 (columns (string-to-number (car split)))
525 (line (concat (apply 'concat indent "|" (make-list columns " |"))
526 "\n")))
527 (if (string-match "^[ \t]*$" (buffer-substring-no-properties
528 (point-at-bol) (point)))
529 (beginning-of-line 1)
530 (newline))
531 ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
532 (dotimes (i rows) (insert line))
533 (goto-char pos)
534 (if (> rows 1)
535 ;; Insert a hline after the first row.
536 (progn
537 (end-of-line 1)
538 (insert "\n|-")
539 (goto-char pos)))
540 (org-table-align)))
542 ;;;###autoload
543 (defun org-table-convert-region (beg0 end0 &optional separator)
544 "Convert region to a table.
545 The region goes from BEG0 to END0, but these borders will be moved
546 slightly, to make sure a beginning of line in the first line is included.
548 SEPARATOR specifies the field separator in the lines. It can have the
549 following values:
551 '(4) Use the comma as a field separator
552 '(16) Use a TAB as field separator
553 '(64) Prompt for a regular expression as field separator
554 integer When a number, use that many spaces as field separator
555 regexp When a regular expression, use it to match the separator
556 nil When nil, the command tries to be smart and figure out the
557 separator in the following way:
558 - when each line contains a TAB, assume TAB-separated material
559 - when each line contains a comma, assume CSV material
560 - else, assume one or more SPACE characters as separator."
561 (interactive "r\nP")
562 (let* ((beg (min beg0 end0))
563 (end (max beg0 end0))
565 (if (> (count-lines beg end) org-table-convert-region-max-lines)
566 (user-error "Region is longer than `org-table-convert-region-max-lines' (%s) lines; not converting"
567 org-table-convert-region-max-lines)
568 (if (equal separator '(64))
569 (setq separator (read-regexp "Regexp for field separator")))
570 (goto-char beg)
571 (beginning-of-line 1)
572 (setq beg (point-marker))
573 (goto-char end)
574 (if (bolp) (backward-char 1) (end-of-line 1))
575 (setq end (point-marker))
576 ;; Get the right field separator
577 (unless separator
578 (goto-char beg)
579 (setq separator
580 (cond
581 ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
582 ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
583 (t 1))))
584 (goto-char beg)
585 (if (equal separator '(4))
586 (while (< (point) end)
587 ;; parse the csv stuff
588 (cond
589 ((looking-at "^") (insert "| "))
590 ((looking-at "[ \t]*$") (replace-match " |") (beginning-of-line 2))
591 ((looking-at "[ \t]*\"\\([^\"\n]*\\)\"")
592 (replace-match "\\1")
593 (if (looking-at "\"") (insert "\"")))
594 ((looking-at "[^,\n]+") (goto-char (match-end 0)))
595 ((looking-at "[ \t]*,") (replace-match " | "))
596 (t (beginning-of-line 2))))
597 (setq re (cond
598 ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
599 ((equal separator '(16)) "^\\|\t")
600 ((integerp separator)
601 (if (< separator 1)
602 (user-error "Number of spaces in separator must be >= 1")
603 (format "^ *\\| *\t *\\| \\{%d,\\}" separator)))
604 ((stringp separator)
605 (format "^ *\\|%s" separator))
606 (t (error "This should not happen"))))
607 (while (re-search-forward re end t)
608 (replace-match "| " t t)))
609 (goto-char beg)
610 (org-table-align))))
612 ;;;###autoload
613 (defun org-table-import (file arg)
614 "Import FILE as a table.
615 The file is assumed to be tab-separated. Such files can be produced by most
616 spreadsheet and database applications. If no tabs (at least one per line)
617 are found, lines will be split on whitespace into fields."
618 (interactive "f\nP")
619 (or (bolp) (newline))
620 (let ((beg (point))
621 (pm (point-max)))
622 (insert-file-contents file)
623 (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
626 ;;;###autoload
627 (defun org-table-export (&optional file format)
628 "Export table to a file, with configurable format.
629 Such a file can be imported into usual spreadsheet programs.
631 FILE can be the output file name. If not given, it will be taken
632 from a TABLE_EXPORT_FILE property in the current entry or higher
633 up in the hierarchy, or the user will be prompted for a file
634 name. FORMAT can be an export format, of the same kind as it
635 used when `orgtbl-mode' sends a table in a different format.
637 The command suggests a format depending on TABLE_EXPORT_FORMAT,
638 whether it is set locally or up in the hierarchy, then on the
639 extension of the given file name, and finally on the variable
640 `org-table-export-default-format'."
641 (interactive)
642 (unless (org-at-table-p) (user-error "No table at point"))
643 (org-table-align) ; Make sure we have everything we need.
644 (let ((file (or file (org-entry-get (point) "TABLE_EXPORT_FILE" t))))
645 (unless file
646 (setq file (read-file-name "Export table to: "))
647 (unless (or (not (file-exists-p file))
648 (y-or-n-p (format "Overwrite file %s? " file)))
649 (user-error "File not written")))
650 (when (file-directory-p file)
651 (user-error "This is a directory path, not a file"))
652 (when (and (buffer-file-name (buffer-base-buffer))
653 (org-file-equal-p
654 (file-truename file)
655 (file-truename (buffer-file-name (buffer-base-buffer)))))
656 (user-error "Please specify a file name that is different from current"))
657 (let ((fileext (concat (file-name-extension file) "$"))
658 (format (or format (org-entry-get (point) "TABLE_EXPORT_FORMAT" t))))
659 (unless format
660 (let* ((formats '("orgtbl-to-tsv" "orgtbl-to-csv" "orgtbl-to-latex"
661 "orgtbl-to-html" "orgtbl-to-generic"
662 "orgtbl-to-texinfo" "orgtbl-to-orgtbl"
663 "orgtbl-to-unicode"))
664 (deffmt-readable
665 (replace-regexp-in-string
666 "\t" "\\t"
667 (replace-regexp-in-string
668 "\n" "\\n"
669 (or (car (delq nil
670 (mapcar
671 (lambda (f)
672 (and (org-string-match-p fileext f) f))
673 formats)))
674 org-table-export-default-format)
675 t t) t t)))
676 (setq format
677 (org-completing-read
678 "Format: " formats nil nil deffmt-readable))))
679 (if (string-match "\\([^ \t\r\n]+\\)\\( +.*\\)?" format)
680 (let ((transform (intern (match-string 1 format)))
681 (params (and (match-end 2)
682 (read (concat "(" (match-string 2 format) ")"))))
683 (table (org-table-to-lisp
684 (buffer-substring-no-properties
685 (org-table-begin) (org-table-end)))))
686 (unless (fboundp transform)
687 (user-error "No such transformation function %s" transform))
688 (let (buf)
689 (with-current-buffer (find-file-noselect file)
690 (setq buf (current-buffer))
691 (erase-buffer)
692 (fundamental-mode)
693 (insert (funcall transform table params) "\n")
694 (save-buffer))
695 (kill-buffer buf))
696 (message "Export done."))
697 (user-error "TABLE_EXPORT_FORMAT invalid")))))
699 (defvar org-table-aligned-begin-marker (make-marker)
700 "Marker at the beginning of the table last aligned.
701 Used to check if cursor still is in that table, to minimize realignment.")
702 (defvar org-table-aligned-end-marker (make-marker)
703 "Marker at the end of the table last aligned.
704 Used to check if cursor still is in that table, to minimize realignment.")
705 (defvar org-table-last-alignment nil
706 "List of flags for flushright alignment, from the last re-alignment.
707 This is being used to correctly align a single field after TAB or RET.")
708 (defvar org-table-last-column-widths nil
709 "List of max width of fields in each column.
710 This is being used to correctly align a single field after TAB or RET.")
711 (defvar org-table-formula-debug nil
712 "Non-nil means debug table formulas.
713 When nil, simply write \"#ERROR\" in corrupted fields.")
714 (make-variable-buffer-local 'org-table-formula-debug)
715 (defvar org-table-overlay-coordinates nil
716 "Overlay coordinates after each align of a table.")
717 (make-variable-buffer-local 'org-table-overlay-coordinates)
719 (defvar org-last-recalc-line nil)
720 (defvar org-table-do-narrow t) ; for dynamic scoping
721 (defconst org-narrow-column-arrow "=>"
722 "Used as display property in narrowed table columns.")
724 ;;;###autoload
725 (defun org-table-align ()
726 "Align the table at point by aligning all vertical bars."
727 (interactive)
728 (let* (
729 ;; Limits of table
730 (beg (org-table-begin))
731 (end (copy-marker (org-table-end)))
732 ;; Current cursor position
733 (linepos (org-current-line))
734 (colpos (org-table-current-column))
735 (winstart (window-start))
736 (winstartline (org-current-line (min winstart (1- (point-max)))))
737 lines lengths l typenums ty fields maxfields i
738 column
739 (indent "") cnt frac
740 rfmt hfmt
741 (spaces '(1 . 1))
742 (sp1 (car spaces))
743 (sp2 (cdr spaces))
744 (rfmt1 (concat
745 (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
746 (hfmt1 (concat
747 (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
748 emptystrings links dates emph raise narrow
749 falign falign1 fmax f1 f2 len c e space)
750 (untabify beg end)
751 (remove-text-properties beg end '(org-cwidth t org-dwidth t display t))
752 ;; Check if we have links or dates
753 (goto-char beg)
754 (setq links (re-search-forward org-bracket-link-regexp end t))
755 (goto-char beg)
756 (setq emph (and org-hide-emphasis-markers
757 (re-search-forward org-emph-re end t)))
758 (goto-char beg)
759 (setq raise (and org-use-sub-superscripts
760 (re-search-forward org-match-substring-regexp end t)))
761 (goto-char beg)
762 (setq dates (and org-display-custom-times
763 (re-search-forward org-ts-regexp-both end t)))
764 ;; Make sure the link properties are right
765 (when links (goto-char beg) (while (org-activate-bracket-links end)))
766 ;; Make sure the date properties are right
767 (when dates (goto-char beg) (while (org-activate-dates end)))
768 (when emph (goto-char beg) (while (org-do-emphasis-faces end)))
769 (when raise (goto-char beg) (while (org-raise-scripts end)))
771 ;; Check if we are narrowing any columns
772 (goto-char beg)
773 (setq narrow (and org-table-do-narrow
774 org-format-transports-properties-p
775 (re-search-forward "<[lrc]?[0-9]+>" end t)))
776 (goto-char beg)
777 (setq falign (re-search-forward "<[lrc][0-9]*>" end t))
778 (goto-char beg)
779 ;; Get the rows
780 (setq lines (org-split-string
781 (buffer-substring beg end) "\n"))
782 ;; Store the indentation of the first line
783 (if (string-match "^ *" (car lines))
784 (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
785 ;; Mark the hlines by setting the corresponding element to nil
786 ;; At the same time, we remove trailing space.
787 (setq lines (mapcar (lambda (l)
788 (if (string-match "^ *|-" l)
790 (if (string-match "[ \t]+$" l)
791 (substring l 0 (match-beginning 0))
792 l)))
793 lines))
794 ;; Get the data fields by splitting the lines.
795 (setq fields (mapcar
796 (lambda (l)
797 (org-split-string l " *| *"))
798 (delq nil (copy-sequence lines))))
799 ;; How many fields in the longest line?
800 (condition-case nil
801 (setq maxfields (apply 'max (mapcar 'length fields)))
802 (error
803 (kill-region beg end)
804 (org-table-create org-table-default-size)
805 (user-error "Empty table - created default table")))
806 ;; A list of empty strings to fill any short rows on output
807 (setq emptystrings (make-list maxfields ""))
808 ;; Check for special formatting.
809 (setq i -1)
810 (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
811 (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
812 ;; Check if there is an explicit width specified
813 (setq fmax nil)
814 (when (or narrow falign)
815 (setq c column fmax nil falign1 nil)
816 (while c
817 (setq e (pop c))
818 (when (and (stringp e) (string-match "^<\\([lrc]\\)?\\([0-9]+\\)?>$" e))
819 (if (match-end 1) (setq falign1 (match-string 1 e)))
820 (if (and org-table-do-narrow (match-end 2))
821 (setq fmax (string-to-number (match-string 2 e)) c nil))))
822 ;; Find fields that are wider than fmax, and shorten them
823 (when fmax
824 (loop for xx in column do
825 (when (and (stringp xx)
826 (> (org-string-width xx) fmax))
827 (org-add-props xx nil
828 'help-echo
829 (concat "Clipped table field, use C-c ` to edit. Full value is:\n"
830 (org-no-properties (copy-sequence xx))))
831 (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
832 (unless (> f1 1)
833 (user-error "Cannot narrow field starting with wide link \"%s\""
834 (match-string 0 xx)))
835 (setq f2 (length xx))
836 (if (= (org-string-width xx)
838 (setq f2 f1)
839 (setq f2 1)
840 (while (< (org-string-width (substring xx 0 f2))
842 (setq f2 (1+ f2))))
843 (add-text-properties f2 (length xx) (list 'org-cwidth t) xx)
844 (add-text-properties (if (>= (string-width (substring xx (1- f2) f2)) 2)
845 (1- f2) (- f2 2)) f2
846 (list 'display org-narrow-column-arrow)
847 xx)))))
848 ;; Get the maximum width for each column
849 (push (apply 'max (or fmax 1) 1 (mapcar 'org-string-width column))
850 lengths)
851 ;; Get the fraction of numbers, to decide about alignment of the column
852 (if falign1
853 (push (equal (downcase falign1) "r") typenums)
854 (setq cnt 0 frac 0.0)
855 (loop for x in column do
856 (if (equal x "")
858 (setq frac ( / (+ (* frac cnt)
859 (if (string-match org-table-number-regexp x) 1 0))
860 (setq cnt (1+ cnt))))))
861 (push (>= frac org-table-number-fraction) typenums)))
862 (setq lengths (nreverse lengths) typenums (nreverse typenums))
864 ;; Store the alignment of this table, for later editing of single fields
865 (setq org-table-last-alignment typenums
866 org-table-last-column-widths lengths)
868 ;; With invisible characters, `format' does not get the field width right
869 ;; So we need to make these fields wide by hand.
870 (when (or links emph raise)
871 (loop for i from 0 upto (1- maxfields) do
872 (setq len (nth i lengths))
873 (loop for j from 0 upto (1- (length fields)) do
874 (setq c (nthcdr i (car (nthcdr j fields))))
875 (if (and (stringp (car c))
876 (or (text-property-any 0 (length (car c))
877 'invisible 'org-link (car c))
878 (text-property-any 0 (length (car c))
879 'org-dwidth t (car c)))
880 (< (org-string-width (car c)) len))
881 (progn
882 (setq space (make-string (- len (org-string-width (car c))) ?\ ))
883 (setcar c (if (nth i typenums)
884 (concat space (car c))
885 (concat (car c) space))))))))
887 ;; Compute the formats needed for output of the table
888 (setq rfmt (concat indent "|") hfmt (concat indent "|"))
889 (while (setq l (pop lengths))
890 (setq ty (if (pop typenums) "" "-")) ; number types flushright
891 (setq rfmt (concat rfmt (format rfmt1 ty l))
892 hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
893 (setq rfmt (concat rfmt "\n")
894 hfmt (concat (substring hfmt 0 -1) "|\n"))
896 (move-marker org-table-aligned-begin-marker (point))
897 ;; Replace modified lines only.
898 (dolist (l lines)
899 (let ((line (if l (apply #'format rfmt (append (pop fields) emptystrings))
900 hfmt)))
901 (if (equal (buffer-substring (point) (line-beginning-position 2)) line)
902 (forward-line)
903 (insert line)
904 (delete-region (point) (line-beginning-position 2)))))
905 (move-marker end nil)
906 (move-marker org-table-aligned-end-marker (point))
907 (when (and orgtbl-mode (not (derived-mode-p 'org-mode)))
908 (goto-char org-table-aligned-begin-marker)
909 (while (org-hide-wide-columns org-table-aligned-end-marker)))
910 ;; Try to move to the old location
911 (org-goto-line winstartline)
912 (setq winstart (point-at-bol))
913 (org-goto-line linepos)
914 (when (eq (window-buffer (selected-window)) (current-buffer))
915 (set-window-start (selected-window) winstart 'noforce))
916 (org-table-goto-column colpos)
917 (and org-table-overlay-coordinates (org-table-overlay-coordinates))
918 (setq org-table-may-need-update nil)
921 ;;;###autoload
922 (defun org-table-begin (&optional table-type)
923 "Find the beginning of the table and return its position.
924 With argument TABLE-TYPE, go to the beginning of a table.el-type table."
925 (let ((table (org-element-lineage (org-element-at-point) '(table) t)))
926 (and table (org-element-property :post-affiliated table))))
928 ;;;###autoload
929 (defun org-table-end (&optional table-type)
930 "Find the end of the table and return its position.
931 With argument TABLE-TYPE, go to the end of a table.el-type table."
932 (let ((table (org-element-lineage (org-element-at-point) '(table) t)))
933 (and table
934 (let ((type (org-element-property :type table)))
935 (if (eq type 'org) (org-element-property :contents-end table)
936 (save-excursion
937 (goto-char (org-element-property :end table))
938 (skip-chars-backward " \t\n")
939 (line-beginning-position 2)))))))
941 ;;;###autoload
942 (defun org-table-justify-field-maybe (&optional new)
943 "Justify the current field, text to left, number to right.
944 Optional argument NEW may specify text to replace the current field content."
945 (cond
946 ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
947 ((org-at-table-hline-p))
948 ((and (not new)
949 (or (not (equal (marker-buffer org-table-aligned-begin-marker)
950 (current-buffer)))
951 (< (point) org-table-aligned-begin-marker)
952 (>= (point) org-table-aligned-end-marker)))
953 ;; This is not the same table, force a full re-align
954 (setq org-table-may-need-update t))
955 (t ;; realign the current field, based on previous full realign
956 (let* ((pos (point)) s
957 (col (org-table-current-column))
958 (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
959 l f n o e)
960 (when (> col 0)
961 (skip-chars-backward "^|\n")
962 (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
963 (progn
964 (setq s (match-string 1)
965 o (match-string 0)
966 l (max 1
967 (- (org-string-width
968 (buffer-substring-no-properties
969 (match-end 0) (match-beginning 0))) 3))
970 e (not (= (match-beginning 2) (match-end 2))))
971 (setq f (format (if num " %%%ds %s" " %%-%ds %s")
972 l (if e "|" (setq org-table-may-need-update t) ""))
973 n (format f s))
974 (if new
975 (if (<= (org-string-width new) l)
976 (setq n (format f new))
977 (setq n (concat new "|") org-table-may-need-update t)))
978 (if (equal (string-to-char n) ?-) (setq n (concat " " n)))
979 (or (equal n o)
980 (let (org-table-may-need-update)
981 (replace-match n t t))))
982 (setq org-table-may-need-update t))
983 (goto-char pos))))))
985 ;;;###autoload
986 (defun org-table-next-field ()
987 "Go to the next field in the current table, creating new lines as needed.
988 Before doing so, re-align the table if necessary."
989 (interactive)
990 (org-table-maybe-eval-formula)
991 (org-table-maybe-recalculate-line)
992 (if (and org-table-automatic-realign
993 org-table-may-need-update)
994 (org-table-align))
995 (let ((end (org-table-end)))
996 (if (org-at-table-hline-p)
997 (end-of-line 1))
998 (condition-case nil
999 (progn
1000 (re-search-forward "|" end)
1001 (if (looking-at "[ \t]*$")
1002 (re-search-forward "|" end))
1003 (if (and (looking-at "-")
1004 org-table-tab-jumps-over-hlines
1005 (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
1006 (goto-char (match-beginning 1)))
1007 (if (looking-at "-")
1008 (progn
1009 (beginning-of-line 0)
1010 (org-table-insert-row 'below))
1011 (if (looking-at " ") (forward-char 1))))
1012 (error
1013 (org-table-insert-row 'below)))))
1015 ;;;###autoload
1016 (defun org-table-previous-field ()
1017 "Go to the previous field in the table.
1018 Before doing so, re-align the table if necessary."
1019 (interactive)
1020 (org-table-justify-field-maybe)
1021 (org-table-maybe-recalculate-line)
1022 (if (and org-table-automatic-realign
1023 org-table-may-need-update)
1024 (org-table-align))
1025 (if (org-at-table-hline-p)
1026 (end-of-line 1))
1027 (condition-case nil
1028 (progn
1029 (re-search-backward "|" (org-table-begin))
1030 (re-search-backward "|" (org-table-begin)))
1031 (error (user-error "Cannot move to previous table field")))
1032 (while (looking-at "|\\(-\\|[ \t]*$\\)")
1033 (re-search-backward "|" (org-table-begin)))
1034 (if (looking-at "| ?")
1035 (goto-char (match-end 0))))
1037 (defun org-table-beginning-of-field (&optional n)
1038 "Move to the beginning of the current table field.
1039 If already at or before the beginning, move to the beginning of the
1040 previous field.
1041 With numeric argument N, move N-1 fields backward first."
1042 (interactive "p")
1043 (let ((pos (point)))
1044 (while (> n 1)
1045 (setq n (1- n))
1046 (org-table-previous-field))
1047 (if (not (re-search-backward "|" (point-at-bol 0) t))
1048 (user-error "No more table fields before the current")
1049 (goto-char (match-end 0))
1050 (and (looking-at " ") (forward-char 1)))
1051 (if (>= (point) pos) (org-table-beginning-of-field 2))))
1053 (defun org-table-end-of-field (&optional n)
1054 "Move to the end of the current table field.
1055 If already at or after the end, move to the end of the next table field.
1056 With numeric argument N, move N-1 fields forward first."
1057 (interactive "p")
1058 (let ((pos (point)))
1059 (while (> n 1)
1060 (setq n (1- n))
1061 (org-table-next-field))
1062 (when (re-search-forward "|" (point-at-eol 1) t)
1063 (backward-char 1)
1064 (skip-chars-backward " ")
1065 (if (and (equal (char-before (point)) ?|) (looking-at " "))
1066 (forward-char 1)))
1067 (if (<= (point) pos) (org-table-end-of-field 2))))
1069 ;;;###autoload
1070 (defun org-table-next-row ()
1071 "Go to the next row (same column) in the current table.
1072 Before doing so, re-align the table if necessary."
1073 (interactive)
1074 (org-table-maybe-eval-formula)
1075 (org-table-maybe-recalculate-line)
1076 (if (or (looking-at "[ \t]*$")
1077 (save-excursion (skip-chars-backward " \t") (bolp)))
1078 (newline)
1079 (if (and org-table-automatic-realign
1080 org-table-may-need-update)
1081 (org-table-align))
1082 (let ((col (org-table-current-column)))
1083 (beginning-of-line 2)
1084 (if (or (not (org-at-table-p))
1085 (org-at-table-hline-p))
1086 (progn
1087 (beginning-of-line 0)
1088 (org-table-insert-row 'below)))
1089 (org-table-goto-column col)
1090 (skip-chars-backward "^|\n\r")
1091 (if (looking-at " ") (forward-char 1)))))
1093 ;;;###autoload
1094 (defun org-table-copy-down (n)
1095 "Copy the value of the current field one row below.
1097 If the field at the cursor is empty, copy the content of the
1098 nearest non-empty field above. With argument N, use the Nth
1099 non-empty field.
1101 If the current field is not empty, it is copied down to the next
1102 row, and the cursor is moved with it. Therefore, repeating this
1103 command causes the column to be filled row-by-row.
1105 If the variable `org-table-copy-increment' is non-nil and the
1106 field is an integer or a timestamp, it will be incremented while
1107 copying. By default, increment by the difference between the
1108 value in the current field and the one in the field above. To
1109 increment using a fixed integer, set `org-table-copy-increment'
1110 to a number. In the case of a timestamp, increment by days."
1111 (interactive "p")
1112 (let* ((colpos (org-table-current-column))
1113 (col (current-column))
1114 (field (save-excursion (org-table-get-field)))
1115 (field-up (or (save-excursion
1116 (org-table-get (1- (org-table-current-line))
1117 (org-table-current-column))) ""))
1118 (non-empty (string-match "[^ \t]" field))
1119 (non-empty-up (string-match "[^ \t]" field-up))
1120 (beg (org-table-begin))
1121 (orig-n n)
1122 txt txt-up inc)
1123 (org-table-check-inside-data-field)
1124 (if (not non-empty)
1125 (save-excursion
1126 (setq txt
1127 (catch 'exit
1128 (while (progn (beginning-of-line 1)
1129 (re-search-backward org-table-dataline-regexp
1130 beg t))
1131 (org-table-goto-column colpos t)
1132 (if (and (looking-at
1133 "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
1134 (<= (setq n (1- n)) 0))
1135 (throw 'exit (match-string 1))))))
1136 (setq field-up
1137 (catch 'exit
1138 (while (progn (beginning-of-line 1)
1139 (re-search-backward org-table-dataline-regexp
1140 beg t))
1141 (org-table-goto-column colpos t)
1142 (if (and (looking-at
1143 "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
1144 (<= (setq n (1- n)) 0))
1145 (throw 'exit (match-string 1))))))
1146 (setq non-empty-up (and field-up (string-match "[^ \t]" field-up))))
1147 ;; Above field was not empty, go down to the next row
1148 (setq txt (org-trim field))
1149 (org-table-next-row)
1150 (org-table-blank-field))
1151 (if non-empty-up (setq txt-up (org-trim field-up)))
1152 (setq inc (cond
1153 ((numberp org-table-copy-increment) org-table-copy-increment)
1154 (txt-up (cond ((and (string-match org-ts-regexp3 txt-up)
1155 (string-match org-ts-regexp3 txt))
1156 (- (org-time-string-to-absolute txt)
1157 (org-time-string-to-absolute txt-up)))
1158 ((string-match org-ts-regexp3 txt) 1)
1159 ((string-match "^[0-9]+\\(\.[0-9]+\\)?" txt-up)
1160 (- (string-to-number txt)
1161 (string-to-number (match-string 0 txt-up))))
1162 (t 1)))
1163 (t 1)))
1164 (if (not txt)
1165 (user-error "No non-empty field found")
1166 (if (and org-table-copy-increment
1167 (not (equal orig-n 0))
1168 (string-match "^[-+^/*0-9eE.]+$" txt)
1169 (< (string-to-number txt) 100000000))
1170 (setq txt (calc-eval (concat txt "+" (number-to-string inc)))))
1171 (insert txt)
1172 (org-move-to-column col)
1173 (if (and org-table-copy-increment (org-at-timestamp-p t))
1174 (org-timestamp-up-day inc)
1175 (org-table-maybe-recalculate-line))
1176 (org-table-align)
1177 (org-move-to-column col))))
1179 (defun org-table-check-inside-data-field (&optional noerror)
1180 "Is point inside a table data field?
1181 I.e. not on a hline or before the first or after the last column?
1182 This actually throws an error, so it aborts the current command."
1183 (if (or (not (org-at-table-p))
1184 (= (org-table-current-column) 0)
1185 (org-at-table-hline-p)
1186 (looking-at "[ \t]*$"))
1187 (if noerror
1189 (user-error "Not in table data field"))
1192 (defvar org-table-clip nil
1193 "Clipboard for table regions.")
1195 (defun org-table-get (line column)
1196 "Get the field in table line LINE, column COLUMN.
1197 If LINE is larger than the number of data lines in the table, the function
1198 returns nil. However, if COLUMN is too large, we will simply return an
1199 empty string.
1200 If LINE is nil, use the current line.
1201 If COLUMN is nil, use the current column."
1202 (setq column (or column (org-table-current-column)))
1203 (save-excursion
1204 (and (or (not line) (org-table-goto-line line))
1205 (org-trim (org-table-get-field column)))))
1207 (defun org-table-put (line column value &optional align)
1208 "Put VALUE into line LINE, column COLUMN.
1209 When ALIGN is set, also realign the table."
1210 (setq column (or column (org-table-current-column)))
1211 (prog1 (save-excursion
1212 (and (or (not line) (org-table-goto-line line))
1213 (progn (org-table-goto-column column nil 'force) t)
1214 (org-table-get-field column value)))
1215 (and align (org-table-align))))
1217 (defun org-table-current-line ()
1218 "Return the index of the current data line."
1219 (let ((pos (point)) (end (org-table-end)) (cnt 0))
1220 (save-excursion
1221 (goto-char (org-table-begin))
1222 (while (and (re-search-forward org-table-dataline-regexp end t)
1223 (setq cnt (1+ cnt))
1224 (< (point-at-eol) pos))))
1225 cnt))
1227 (defun org-table-goto-line (N)
1228 "Go to the Nth data line in the current table.
1229 Return t when the line exists, nil if it does not exist."
1230 (goto-char (org-table-begin))
1231 (let ((end (org-table-end)) (cnt 0))
1232 (while (and (re-search-forward org-table-dataline-regexp end t)
1233 (< (setq cnt (1+ cnt)) N)))
1234 (= cnt N)))
1236 ;;;###autoload
1237 (defun org-table-blank-field ()
1238 "Blank the current table field or active region."
1239 (interactive)
1240 (org-table-check-inside-data-field)
1241 (if (and (org-called-interactively-p 'any) (org-region-active-p))
1242 (let (org-table-clip)
1243 (org-table-cut-region (region-beginning) (region-end)))
1244 (skip-chars-backward "^|")
1245 (backward-char 1)
1246 (if (looking-at "|[^|\n]+")
1247 (let* ((pos (match-beginning 0))
1248 (match (match-string 0))
1249 (len (org-string-width match)))
1250 (replace-match (concat "|" (make-string (1- len) ?\ )))
1251 (goto-char (+ 2 pos))
1252 (substring match 1)))))
1254 (defun org-table-get-field (&optional n replace)
1255 "Return the value of the field in column N of current row.
1256 N defaults to current field.
1257 If REPLACE is a string, replace field with this value. The return value
1258 is always the old value."
1259 (and n (org-table-goto-column n))
1260 (skip-chars-backward "^|\n")
1261 (backward-char 1)
1262 (if (looking-at "|[^|\r\n]*")
1263 (let* ((pos (match-beginning 0))
1264 (val (buffer-substring (1+ pos) (match-end 0))))
1265 (if replace
1266 (replace-match (concat "|" (if (equal replace "") " " replace))
1267 t t))
1268 (goto-char (min (point-at-eol) (+ 2 pos)))
1269 val)
1270 (forward-char 1) ""))
1272 ;;;###autoload
1273 (defun org-table-field-info (arg)
1274 "Show info about the current field, and highlight any reference at point."
1275 (interactive "P")
1276 (unless (org-at-table-p) (user-error "Not at a table"))
1277 (org-table-get-specials)
1278 (save-excursion
1279 (let* ((pos (point))
1280 (col (org-table-current-column))
1281 (cname (car (rassoc (int-to-string col) org-table-column-names)))
1282 (name (car (rassoc (list (org-current-line) col)
1283 org-table-named-field-locations)))
1284 (eql (org-table-expand-lhs-ranges
1285 (mapcar
1286 (lambda (e)
1287 (cons (org-table-formula-handle-first/last-rc
1288 (car e)) (cdr e)))
1289 (org-table-get-stored-formulas))))
1290 (dline (org-table-current-dline))
1291 (ref (format "@%d$%d" dline col))
1292 (ref1 (org-table-convert-refs-to-an ref))
1293 (fequation (or (assoc name eql) (assoc ref eql)))
1294 (cequation (assoc (int-to-string col) eql))
1295 (eqn (or fequation cequation)))
1296 (if (and eqn (get-text-property 0 :orig-eqn (car eqn)))
1297 (setq eqn (get-text-property 0 :orig-eqn (car eqn))))
1298 (goto-char pos)
1299 (condition-case nil
1300 (org-table-show-reference 'local)
1301 (error nil))
1302 (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
1303 dline col
1304 (if cname (concat " or $" cname) "")
1305 dline col ref1
1306 (if name (concat " or $" name) "")
1307 ;; FIXME: formula info not correct if special table line
1308 (if eqn
1309 (concat ", formula: "
1310 (org-table-formula-to-user
1311 (concat
1312 (if (string-match "^[$@]"(car eqn)) "" "$")
1313 (car eqn) "=" (cdr eqn))))
1314 "")))))
1316 (defun org-table-current-column ()
1317 "Find out which column we are in."
1318 (interactive)
1319 (if (org-called-interactively-p 'any) (org-table-check-inside-data-field))
1320 (save-excursion
1321 (let ((cnt 0) (pos (point)))
1322 (beginning-of-line 1)
1323 (while (search-forward "|" pos t)
1324 (setq cnt (1+ cnt)))
1325 (when (org-called-interactively-p 'interactive)
1326 (message "In table column %d" cnt))
1327 cnt)))
1329 ;;;###autoload
1330 (defun org-table-current-dline ()
1331 "Find out what table data line we are in.
1332 Only data lines count for this."
1333 (interactive)
1334 (when (org-called-interactively-p 'any)
1335 (org-table-check-inside-data-field))
1336 (save-excursion
1337 (let ((cnt 0) (pos (point)))
1338 (goto-char (org-table-begin))
1339 (while (<= (point) pos)
1340 (if (looking-at org-table-dataline-regexp) (setq cnt (1+ cnt)))
1341 (beginning-of-line 2))
1342 (when (org-called-interactively-p 'any)
1343 (message "This is table line %d" cnt))
1344 cnt)))
1346 ;;;###autoload
1347 (defun org-table-goto-column (n &optional on-delim force)
1348 "Move the cursor to the Nth column in the current table line.
1349 With optional argument ON-DELIM, stop with point before the left delimiter
1350 of the field.
1351 If there are less than N fields, just go to after the last delimiter.
1352 However, when FORCE is non-nil, create new columns if necessary."
1353 (interactive "p")
1354 (beginning-of-line 1)
1355 (when (> n 0)
1356 (while (and (> (setq n (1- n)) -1)
1357 (or (search-forward "|" (point-at-eol) t)
1358 (and force
1359 (progn (end-of-line 1)
1360 (skip-chars-backward "^|")
1361 (insert " | ")
1362 t)))))
1363 (when (and force (not (looking-at ".*|")))
1364 (save-excursion (end-of-line 1) (insert " | ")))
1365 (if on-delim
1366 (backward-char 1)
1367 (if (looking-at " ") (forward-char 1)))))
1369 ;;;###autoload
1370 (defun org-table-insert-column ()
1371 "Insert a new column into the table."
1372 (interactive)
1373 (if (not (org-at-table-p))
1374 (user-error "Not at a table"))
1375 (org-table-find-dataline)
1376 (let* ((col (max 1 (org-table-current-column)))
1377 (beg (org-table-begin))
1378 (end (copy-marker (org-table-end)))
1379 ;; Current cursor position
1380 (linepos (org-current-line))
1381 (colpos col))
1382 (goto-char beg)
1383 (while (< (point) end)
1384 (if (org-at-table-hline-p)
1386 (org-table-goto-column col t)
1387 (insert "| "))
1388 (beginning-of-line 2))
1389 (move-marker end nil)
1390 (org-goto-line linepos)
1391 (org-table-goto-column colpos)
1392 (org-table-align)
1393 (when (or (not org-table-fix-formulas-confirm)
1394 (funcall org-table-fix-formulas-confirm "Fix formulas? "))
1395 (org-table-fix-formulas "$" nil (1- col) 1)
1396 (org-table-fix-formulas "$LR" nil (1- col) 1))))
1398 (defun org-table-find-dataline ()
1399 "Find a data line in the current table, which is needed for column commands."
1400 (if (and (org-at-table-p)
1401 (not (org-at-table-hline-p)))
1403 (let ((col (current-column))
1404 (end (org-table-end)))
1405 (org-move-to-column col)
1406 (while (and (< (point) end)
1407 (or (not (= (current-column) col))
1408 (org-at-table-hline-p)))
1409 (beginning-of-line 2)
1410 (org-move-to-column col))
1411 (if (and (org-at-table-p)
1412 (not (org-at-table-hline-p)))
1414 (user-error
1415 "Please position cursor in a data line for column operations")))))
1417 (defun org-table-line-to-dline (line &optional above)
1418 "Turn a buffer line number into a data line number.
1419 If there is no data line in this line, return nil.
1420 If there is no matching dline (most likely te reference was a hline), the
1421 first dline below it is used. When ABOVE is non-nil, the one above is used."
1422 (catch 'exit
1423 (let ((ll (length org-table-dlines))
1425 (if above
1426 (progn
1427 (setq i (1- ll))
1428 (while (> i 0)
1429 (if (<= (aref org-table-dlines i) line)
1430 (throw 'exit i))
1431 (setq i (1- i))))
1432 (setq i 1)
1433 (while (< i ll)
1434 (if (>= (aref org-table-dlines i) line)
1435 (throw 'exit i))
1436 (setq i (1+ i)))))
1437 nil))
1439 ;;;###autoload
1440 (defun org-table-delete-column ()
1441 "Delete a column from the table."
1442 (interactive)
1443 (if (not (org-at-table-p))
1444 (user-error "Not at a table"))
1445 (org-table-find-dataline)
1446 (org-table-check-inside-data-field)
1447 (let* ((col (org-table-current-column))
1448 (beg (org-table-begin))
1449 (end (copy-marker (org-table-end)))
1450 ;; Current cursor position
1451 (linepos (org-current-line))
1452 (colpos col))
1453 (goto-char beg)
1454 (while (< (point) end)
1455 (if (org-at-table-hline-p)
1457 (org-table-goto-column col t)
1458 (and (looking-at "|[^|\n]+|")
1459 (replace-match "|")))
1460 (beginning-of-line 2))
1461 (move-marker end nil)
1462 (org-goto-line linepos)
1463 (org-table-goto-column colpos)
1464 (org-table-align)
1465 (when (or (not org-table-fix-formulas-confirm)
1466 (funcall org-table-fix-formulas-confirm "Fix formulas? "))
1467 (org-table-fix-formulas "$" (list (cons (number-to-string col) "INVALID"))
1468 col -1 col)
1469 (org-table-fix-formulas "$LR" (list (cons (number-to-string col) "INVALID"))
1470 col -1 col))))
1472 ;;;###autoload
1473 (defun org-table-move-column-right ()
1474 "Move column to the right."
1475 (interactive)
1476 (org-table-move-column nil))
1477 ;;;###autoload
1478 (defun org-table-move-column-left ()
1479 "Move column to the left."
1480 (interactive)
1481 (org-table-move-column 'left))
1483 ;;;###autoload
1484 (defun org-table-move-column (&optional left)
1485 "Move the current column to the right. With arg LEFT, move to the left."
1486 (interactive "P")
1487 (if (not (org-at-table-p))
1488 (user-error "Not at a table"))
1489 (org-table-find-dataline)
1490 (org-table-check-inside-data-field)
1491 (let* ((col (org-table-current-column))
1492 (col1 (if left (1- col) col))
1493 (beg (org-table-begin))
1494 (end (copy-marker (org-table-end)))
1495 ;; Current cursor position
1496 (linepos (org-current-line))
1497 (colpos (if left (1- col) (1+ col))))
1498 (if (and left (= col 1))
1499 (user-error "Cannot move column further left"))
1500 (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
1501 (user-error "Cannot move column further right"))
1502 (goto-char beg)
1503 (while (< (point) end)
1504 (if (org-at-table-hline-p)
1506 (org-table-goto-column col1 t)
1507 (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
1508 (replace-match "|\\2|\\1|")))
1509 (beginning-of-line 2))
1510 (move-marker end nil)
1511 (org-goto-line linepos)
1512 (org-table-goto-column colpos)
1513 (org-table-align)
1514 (when (or (not org-table-fix-formulas-confirm)
1515 (funcall org-table-fix-formulas-confirm "Fix formulas? "))
1516 (org-table-fix-formulas
1517 "$" (list (cons (number-to-string col) (number-to-string colpos))
1518 (cons (number-to-string colpos) (number-to-string col))))
1519 (org-table-fix-formulas
1520 "$LR" (list (cons (number-to-string col) (number-to-string colpos))
1521 (cons (number-to-string colpos) (number-to-string col)))))))
1523 ;;;###autoload
1524 (defun org-table-move-row-down ()
1525 "Move table row down."
1526 (interactive)
1527 (org-table-move-row nil))
1528 ;;;###autoload
1529 (defun org-table-move-row-up ()
1530 "Move table row up."
1531 (interactive)
1532 (org-table-move-row 'up))
1534 ;;;###autoload
1535 (defun org-table-move-row (&optional up)
1536 "Move the current table line down. With arg UP, move it up."
1537 (interactive "P")
1538 (let* ((col (current-column))
1539 (pos (point))
1540 (hline1p (save-excursion (beginning-of-line 1)
1541 (looking-at org-table-hline-regexp)))
1542 (dline1 (org-table-current-dline))
1543 (dline2 (+ dline1 (if up -1 1)))
1544 (tonew (if up 0 2))
1545 txt hline2p)
1546 (beginning-of-line tonew)
1547 (unless (org-at-table-p)
1548 (goto-char pos)
1549 (user-error "Cannot move row further"))
1550 (setq hline2p (looking-at org-table-hline-regexp))
1551 (goto-char pos)
1552 (beginning-of-line 1)
1553 (setq pos (point))
1554 (setq txt (buffer-substring (point) (1+ (point-at-eol))))
1555 (delete-region (point) (1+ (point-at-eol)))
1556 (beginning-of-line tonew)
1557 (insert txt)
1558 (beginning-of-line 0)
1559 (org-move-to-column col)
1560 (unless (or hline1p hline2p
1561 (not (or (not org-table-fix-formulas-confirm)
1562 (funcall org-table-fix-formulas-confirm
1563 "Fix formulas? "))))
1564 (org-table-fix-formulas
1565 "@" (list (cons (number-to-string dline1) (number-to-string dline2))
1566 (cons (number-to-string dline2) (number-to-string dline1)))))))
1568 ;;;###autoload
1569 (defun org-table-insert-row (&optional arg)
1570 "Insert a new row above the current line into the table.
1571 With prefix ARG, insert below the current line."
1572 (interactive "P")
1573 (if (not (org-at-table-p))
1574 (user-error "Not at a table"))
1575 (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
1576 (new (org-table-clean-line line)))
1577 ;; Fix the first field if necessary
1578 (if (string-match "^[ \t]*| *[#$] *|" line)
1579 (setq new (replace-match (match-string 0 line) t t new)))
1580 (beginning-of-line (if arg 2 1))
1581 (let (org-table-may-need-update) (insert-before-markers new "\n"))
1582 (beginning-of-line 0)
1583 (re-search-forward "| ?" (point-at-eol) t)
1584 (and (or org-table-may-need-update org-table-overlay-coordinates)
1585 (org-table-align))
1586 (when (or (not org-table-fix-formulas-confirm)
1587 (funcall org-table-fix-formulas-confirm "Fix formulas? "))
1588 (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1))))
1590 ;;;###autoload
1591 (defun org-table-insert-hline (&optional above)
1592 "Insert a horizontal-line below the current line into the table.
1593 With prefix ABOVE, insert above the current line."
1594 (interactive "P")
1595 (if (not (org-at-table-p))
1596 (user-error "Not at a table"))
1597 (when (eobp) (insert "\n") (backward-char 1))
1598 (if (not (string-match "|[ \t]*$" (org-current-line-string)))
1599 (org-table-align))
1600 (let ((line (org-table-clean-line
1601 (buffer-substring (point-at-bol) (point-at-eol))))
1602 (col (current-column)))
1603 (while (string-match "|\\( +\\)|" line)
1604 (setq line (replace-match
1605 (concat "+" (make-string (- (match-end 1) (match-beginning 1))
1606 ?-) "|") t t line)))
1607 (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
1608 (beginning-of-line (if above 1 2))
1609 (insert line "\n")
1610 (beginning-of-line (if above 1 -1))
1611 (org-move-to-column col)
1612 (and org-table-overlay-coordinates (org-table-align))))
1614 ;;;###autoload
1615 (defun org-table-hline-and-move (&optional same-column)
1616 "Insert a hline and move to the row below that line."
1617 (interactive "P")
1618 (let ((col (org-table-current-column)))
1619 (org-table-maybe-eval-formula)
1620 (org-table-maybe-recalculate-line)
1621 (org-table-insert-hline)
1622 (end-of-line 2)
1623 (if (looking-at "\n[ \t]*|-")
1624 (progn (insert "\n|") (org-table-align))
1625 (org-table-next-field))
1626 (if same-column (org-table-goto-column col))))
1628 (defun org-table-clean-line (s)
1629 "Convert a table line S into a string with only \"|\" and space.
1630 In particular, this does handle wide and invisible characters."
1631 (if (string-match "^[ \t]*|-" s)
1632 ;; It's a hline, just map the characters
1633 (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
1634 (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
1635 (setq s (replace-match
1636 (concat "|" (make-string (org-string-width (match-string 1 s))
1637 ?\ ) "|")
1638 t t s)))
1641 ;;;###autoload
1642 (defun org-table-kill-row ()
1643 "Delete the current row or horizontal line from the table."
1644 (interactive)
1645 (if (not (org-at-table-p))
1646 (user-error "Not at a table"))
1647 (let ((col (current-column))
1648 (dline (org-table-current-dline)))
1649 (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
1650 (if (not (org-at-table-p)) (beginning-of-line 0))
1651 (org-move-to-column col)
1652 (when (or (not org-table-fix-formulas-confirm)
1653 (funcall org-table-fix-formulas-confirm "Fix formulas? "))
1654 (org-table-fix-formulas "@" (list (cons (number-to-string dline) "INVALID"))
1655 dline -1 dline))))
1657 ;;;###autoload
1658 (defun org-table-sort-lines (with-case &optional sorting-type getkey-func compare-func)
1659 "Sort table lines according to the column at point.
1661 The position of point indicates the column to be used for
1662 sorting, and the range of lines is the range between the nearest
1663 horizontal separator lines, or the entire table of no such lines
1664 exist. If point is before the first column, you will be prompted
1665 for the sorting column. If there is an active region, the mark
1666 specifies the first line and the sorting column, while point
1667 should be in the last line to be included into the sorting.
1669 The command then prompts for the sorting type which can be
1670 alphabetically, numerically, or by time (as given in a time stamp
1671 in the field, or as a HH:MM value). Sorting in reverse order is
1672 also possible.
1674 With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
1676 If SORTING-TYPE is specified when this function is called from a Lisp
1677 program, no prompting will take place. SORTING-TYPE must be a character,
1678 any of (?a ?A ?n ?N ?t ?T ?f ?F) where the capital letters indicate that
1679 sorting should be done in reverse order.
1681 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies
1682 a function to be called to extract the key. It must return either
1683 a string or a number that should serve as the sorting key for that
1684 row. It will then use COMPARE-FUNC to compare entries. If GETKEY-FUNC
1685 is specified interactively, the comparison will be either a string or
1686 numeric compare based on the type of the first key in the table."
1687 (interactive "P")
1688 (let* ((thisline (org-current-line))
1689 (thiscol (org-table-current-column))
1690 (otc org-table-overlay-coordinates)
1691 beg end bcol ecol tend tbeg column lns pos)
1692 (when (equal thiscol 0)
1693 (if (org-called-interactively-p 'any)
1694 (setq thiscol
1695 (string-to-number
1696 (read-string "Use column N for sorting: ")))
1697 (setq thiscol 1))
1698 (org-table-goto-column thiscol))
1699 (org-table-check-inside-data-field)
1700 (if (org-region-active-p)
1701 (progn
1702 (setq beg (region-beginning) end (region-end))
1703 (goto-char beg)
1704 (setq column (org-table-current-column)
1705 beg (point-at-bol))
1706 (goto-char end)
1707 (setq end (point-at-bol 2)))
1708 (setq column (org-table-current-column)
1709 pos (point)
1710 tbeg (org-table-begin)
1711 tend (org-table-end))
1712 (if (re-search-backward org-table-hline-regexp tbeg t)
1713 (setq beg (point-at-bol 2))
1714 (goto-char tbeg)
1715 (setq beg (point-at-bol 1)))
1716 (goto-char pos)
1717 (if (re-search-forward org-table-hline-regexp tend t)
1718 (setq end (point-at-bol 1))
1719 (goto-char tend)
1720 (setq end (point-at-bol))))
1721 (setq beg (move-marker (make-marker) beg)
1722 end (move-marker (make-marker) end))
1723 (untabify beg end)
1724 (goto-char beg)
1725 (org-table-goto-column column)
1726 (skip-chars-backward "^|")
1727 (setq bcol (current-column))
1728 (org-table-goto-column (1+ column))
1729 (skip-chars-backward "^|")
1730 (setq ecol (1- (current-column)))
1731 (org-table-goto-column column)
1732 (setq lns (mapcar (lambda(x) (cons
1733 (org-sort-remove-invisible
1734 (nth (1- column)
1735 (org-split-string x "[ \t]*|[ \t]*")))
1737 (org-split-string (buffer-substring beg end) "\n")))
1738 (setq lns (org-table--do-sort
1739 lns "Table" with-case sorting-type getkey-func compare-func))
1740 (when org-table-overlay-coordinates
1741 (org-table-toggle-coordinate-overlays))
1742 (delete-region beg end)
1743 (move-marker beg nil)
1744 (move-marker end nil)
1745 (insert (mapconcat 'cdr lns "\n") "\n")
1746 (org-goto-line thisline)
1747 (org-table-goto-column thiscol)
1748 (when otc (org-table-toggle-coordinate-overlays))
1749 (message "%d lines sorted, based on column %d" (length lns) column)))
1751 (defun org-table--do-sort (table what &optional with-case sorting-type getkey-func compare-func)
1752 "Sort TABLE of WHAT according to SORTING-TYPE.
1753 The user will be prompted for the SORTING-TYPE if the call to this
1754 function does not specify it.
1755 WHAT is only for the prompt, to indicate what is being sorted.
1756 The sorting key will be extracted from the car of the elements of
1757 the table. If WITH-CASE is non-nil, the sorting will be case-sensitive.
1759 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies
1760 a function to be called to extract the key. It must return either
1761 a string or a number that should serve as the sorting key for that
1762 row. It will then use COMPARE-FUNC to compare entries. If GETKEY-FUNC
1763 is specified interactively, the comparison will be either a string or
1764 numeric compare based on the type of the first key in the table."
1765 (unless sorting-type
1766 (message
1767 "Sort %s: [a]lphabetic, [n]umeric, [t]ime, [f]unc. A/N/T/F means reversed:"
1768 what)
1769 (setq sorting-type (read-char-exclusive)))
1770 (let (extractfun comparefun tempfun)
1771 ;; Define the appropriate functions
1772 (case sorting-type
1773 ((?n ?N)
1774 (setq extractfun #'string-to-number
1775 comparefun (if (= sorting-type ?n) #'< #'>)))
1776 ((?a ?A)
1777 (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
1778 (lambda(x) (downcase (org-sort-remove-invisible x))))
1779 comparefun (if (= sorting-type ?a) #'string< #'org-string>)))
1780 ((?t ?T)
1781 (setq extractfun
1782 (lambda (x)
1783 (cond ((or (string-match org-ts-regexp x)
1784 (string-match org-ts-regexp-both x))
1785 (org-float-time
1786 (org-time-string-to-time (match-string 0 x))))
1787 ((string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" x)
1788 (org-hh:mm-string-to-minutes x))
1789 (t 0)))
1790 comparefun (if (= sorting-type ?t) #'< #'>)))
1791 ((?f ?F)
1792 (setq tempfun (or getkey-func
1793 (intern (org-icompleting-read
1794 "Sort using function: "
1795 obarray #'fboundp t nil nil))))
1796 (let ((extract-string-p (stringp (funcall tempfun (caar table)))))
1797 (setq extractfun (if (and extract-string-p (not with-case))
1798 (lambda (x) (downcase (funcall tempfun x)))
1799 tempfun))
1800 (setq comparefun (cond (compare-func
1801 (if (= sorting-type ?f) compare-func
1802 (lambda (a b) (funcall compare-func b a))))
1803 (extract-string-p
1804 (if (= sorting-type ?f) #'string<
1805 #'org-string>))
1806 (t (if (= sorting-type ?f) #'< #'>))))))
1807 (t (error "Invalid sorting type `%c'" sorting-type)))
1809 (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
1810 table)
1811 (lambda (a b) (funcall comparefun (car a) (car b))))))
1813 ;;;###autoload
1814 (defun org-table-cut-region (beg end)
1815 "Copy region in table to the clipboard and blank all relevant fields.
1816 If there is no active region, use just the field at point."
1817 (interactive (list
1818 (if (org-region-active-p) (region-beginning) (point))
1819 (if (org-region-active-p) (region-end) (point))))
1820 (org-table-copy-region beg end 'cut))
1822 ;;;###autoload
1823 (defun org-table-copy-region (beg end &optional cut)
1824 "Copy rectangular region in table to clipboard.
1825 A special clipboard is used which can only be accessed
1826 with `org-table-paste-rectangle'."
1827 (interactive (list
1828 (if (org-region-active-p) (region-beginning) (point))
1829 (if (org-region-active-p) (region-end) (point))
1830 current-prefix-arg))
1831 (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
1832 region cols
1833 (rpl (if cut " " nil)))
1834 (goto-char beg)
1835 (org-table-check-inside-data-field)
1836 (setq l01 (org-current-line)
1837 c01 (org-table-current-column))
1838 (goto-char end)
1839 (org-table-check-inside-data-field)
1840 (setq l02 (org-current-line)
1841 c02 (org-table-current-column))
1842 (setq l1 (min l01 l02) l2 (max l01 l02)
1843 c1 (min c01 c02) c2 (max c01 c02))
1844 (catch 'exit
1845 (while t
1846 (catch 'nextline
1847 (if (> l1 l2) (throw 'exit t))
1848 (org-goto-line l1)
1849 (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
1850 (setq cols nil ic1 c1 ic2 c2)
1851 (while (< ic1 (1+ ic2))
1852 (push (org-table-get-field ic1 rpl) cols)
1853 (setq ic1 (1+ ic1)))
1854 (push (nreverse cols) region)
1855 (setq l1 (1+ l1)))))
1856 (setq org-table-clip (nreverse region))
1857 (if cut (org-table-align))
1858 org-table-clip))
1860 ;;;###autoload
1861 (defun org-table-paste-rectangle ()
1862 "Paste a rectangular region into a table.
1863 The upper right corner ends up in the current field. All involved fields
1864 will be overwritten. If the rectangle does not fit into the present table,
1865 the table is enlarged as needed. The process ignores horizontal separator
1866 lines."
1867 (interactive)
1868 (unless (and org-table-clip (listp org-table-clip))
1869 (user-error "First cut/copy a region to paste!"))
1870 (org-table-check-inside-data-field)
1871 (let* ((clip org-table-clip)
1872 (line (org-current-line))
1873 (col (org-table-current-column))
1874 (org-enable-table-editor t)
1875 (org-table-automatic-realign nil)
1876 c cols field)
1877 (while (setq cols (pop clip))
1878 (while (org-at-table-hline-p) (beginning-of-line 2))
1879 (if (not (org-at-table-p))
1880 (progn (end-of-line 0) (org-table-next-field)))
1881 (setq c col)
1882 (while (setq field (pop cols))
1883 (org-table-goto-column c nil 'force)
1884 (org-table-get-field nil field)
1885 (setq c (1+ c)))
1886 (beginning-of-line 2))
1887 (org-goto-line line)
1888 (org-table-goto-column col)
1889 (org-table-align)))
1891 ;;;###autoload
1892 (defun org-table-convert ()
1893 "Convert from `org-mode' table to table.el and back.
1894 Obviously, this only works within limits. When an Org-mode table is
1895 converted to table.el, all horizontal separator lines get lost, because
1896 table.el uses these as cell boundaries and has no notion of horizontal lines.
1897 A table.el table can be converted to an Org-mode table only if it does not
1898 do row or column spanning. Multiline cells will become multiple cells.
1899 Beware, Org-mode does not test if the table can be successfully converted - it
1900 blindly applies a recipe that works for simple tables."
1901 (interactive)
1902 (require 'table)
1903 (if (org-at-table.el-p)
1904 ;; convert to Org-mode table
1905 (let ((beg (copy-marker (org-table-begin t)))
1906 (end (copy-marker (org-table-end t))))
1907 (table-unrecognize-region beg end)
1908 (goto-char beg)
1909 (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
1910 (replace-match ""))
1911 (goto-char beg))
1912 (if (org-at-table-p)
1913 ;; convert to table.el table
1914 (let ((beg (copy-marker (org-table-begin)))
1915 (end (copy-marker (org-table-end))))
1916 ;; first, get rid of all horizontal lines
1917 (goto-char beg)
1918 (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
1919 (replace-match ""))
1920 ;; insert a hline before first
1921 (goto-char beg)
1922 (org-table-insert-hline 'above)
1923 (beginning-of-line -1)
1924 ;; insert a hline after each line
1925 (while (progn (beginning-of-line 3) (< (point) end))
1926 (org-table-insert-hline))
1927 (goto-char beg)
1928 (setq end (move-marker end (org-table-end)))
1929 ;; replace "+" at beginning and ending of hlines
1930 (while (re-search-forward "^\\([ \t]*\\)|-" end t)
1931 (replace-match "\\1+-"))
1932 (goto-char beg)
1933 (while (re-search-forward "-|[ \t]*$" end t)
1934 (replace-match "-+"))
1935 (goto-char beg)))))
1937 (defun org-table-transpose-table-at-point ()
1938 "Transpose Org table at point and eliminate hlines.
1939 So a table like
1941 | 1 | 2 | 4 | 5 |
1942 |---+---+---+---|
1943 | a | b | c | d |
1944 | e | f | g | h |
1946 will be transposed as
1948 | 1 | a | e |
1949 | 2 | b | f |
1950 | 4 | c | g |
1951 | 5 | d | h |
1953 Note that horizontal lines disappear."
1954 (interactive)
1955 (let* ((table (delete 'hline (org-table-to-lisp)))
1956 (dline_old (org-table-current-line))
1957 (col_old (org-table-current-column))
1958 (contents (mapcar (lambda (p)
1959 (let ((tp table))
1960 (mapcar
1961 (lambda (rown)
1962 (prog1
1963 (pop (car tp))
1964 (setq tp (cdr tp))))
1965 table)))
1966 (car table))))
1967 (goto-char (org-table-begin))
1968 (re-search-forward "|")
1969 (backward-char)
1970 (delete-region (point) (org-table-end))
1971 (insert (mapconcat
1972 (lambda(x)
1973 (concat "| " (mapconcat 'identity x " | " ) " |\n" ))
1974 contents ""))
1975 (org-table-goto-line col_old)
1976 (org-table-goto-column dline_old))
1977 (org-table-align))
1979 ;;;###autoload
1980 (defun org-table-wrap-region (arg)
1981 "Wrap several fields in a column like a paragraph.
1982 This is useful if you'd like to spread the contents of a field over several
1983 lines, in order to keep the table compact.
1985 If there is an active region, and both point and mark are in the same column,
1986 the text in the column is wrapped to minimum width for the given number of
1987 lines. Generally, this makes the table more compact. A prefix ARG may be
1988 used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
1989 formats the selected text to two lines. If the region was longer than two
1990 lines, the remaining lines remain empty. A negative prefix argument reduces
1991 the current number of lines by that amount. The wrapped text is pasted back
1992 into the table. If you formatted it to more lines than it was before, fields
1993 further down in the table get overwritten - so you might need to make space in
1994 the table first.
1996 If there is no region, the current field is split at the cursor position and
1997 the text fragment to the right of the cursor is prepended to the field one
1998 line down.
2000 If there is no region, but you specify a prefix ARG, the current field gets
2001 blank, and the content is appended to the field above."
2002 (interactive "P")
2003 (org-table-check-inside-data-field)
2004 (if (org-region-active-p)
2005 ;; There is a region: fill as a paragraph
2006 (let* ((beg (region-beginning))
2007 (cline (save-excursion (goto-char beg) (org-current-line)))
2008 (ccol (save-excursion (goto-char beg) (org-table-current-column)))
2009 nlines)
2010 (org-table-cut-region (region-beginning) (region-end))
2011 (if (> (length (car org-table-clip)) 1)
2012 (user-error "Region must be limited to single column"))
2013 (setq nlines (if arg
2014 (if (< arg 1)
2015 (+ (length org-table-clip) arg)
2016 arg)
2017 (length org-table-clip)))
2018 (setq org-table-clip
2019 (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
2020 nil nlines)))
2021 (org-goto-line cline)
2022 (org-table-goto-column ccol)
2023 (org-table-paste-rectangle))
2024 ;; No region, split the current field at point
2025 (unless (org-get-alist-option org-M-RET-may-split-line 'table)
2026 (skip-chars-forward "^\r\n|"))
2027 (if arg
2028 ;; combine with field above
2029 (let ((s (org-table-blank-field))
2030 (col (org-table-current-column)))
2031 (beginning-of-line 0)
2032 (while (org-at-table-hline-p) (beginning-of-line 0))
2033 (org-table-goto-column col)
2034 (skip-chars-forward "^|")
2035 (skip-chars-backward " ")
2036 (insert " " (org-trim s))
2037 (org-table-align))
2038 ;; split field
2039 (if (looking-at "\\([^|]+\\)+|")
2040 (let ((s (match-string 1)))
2041 (replace-match " |")
2042 (goto-char (match-beginning 0))
2043 (org-table-next-row)
2044 (insert (org-trim s) " ")
2045 (org-table-align))
2046 (org-table-next-row)))))
2048 (defvar org-field-marker nil)
2050 ;;;###autoload
2051 (defun org-table-edit-field (arg)
2052 "Edit table field in a different window.
2053 This is mainly useful for fields that contain hidden parts.
2054 When called with a \\[universal-argument] prefix, just make the full field visible so that
2055 it can be edited in place."
2056 (interactive "P")
2057 (cond
2058 ((equal arg '(16))
2059 (org-table-follow-field-mode (if org-table-follow-field-mode -1 1)))
2060 (arg
2061 (let ((b (save-excursion (skip-chars-backward "^|") (point)))
2062 (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
2063 (remove-text-properties b e '(org-cwidth t invisible t
2064 display t intangible t))
2065 (if (and (boundp 'font-lock-mode) font-lock-mode)
2066 (font-lock-fontify-block))))
2068 (let ((pos (point-marker))
2069 (coord
2070 (if (eq org-table-use-standard-references t)
2071 (concat (org-number-to-letters (org-table-current-column))
2072 (int-to-string (org-table-current-dline)))
2073 (concat "@" (int-to-string (org-table-current-dline))
2074 "$" (int-to-string (org-table-current-column)))))
2075 (field (org-table-get-field))
2076 (cw (current-window-configuration))
2078 (goto-char pos)
2079 (org-switch-to-buffer-other-window "*Org Table Edit Field*")
2080 (when (and (local-variable-p 'org-field-marker)
2081 (markerp org-field-marker))
2082 (move-marker org-field-marker nil))
2083 (erase-buffer)
2084 (insert "#\n# Edit field " coord " and finish with C-c C-c\n#\n")
2085 (let ((org-inhibit-startup t)) (org-mode))
2086 (auto-fill-mode -1)
2087 (setq truncate-lines nil)
2088 (setq word-wrap t)
2089 (goto-char (setq p (point-max)))
2090 (insert (org-trim field))
2091 (remove-text-properties p (point-max)
2092 '(invisible t org-cwidth t display t
2093 intangible t))
2094 (goto-char p)
2095 (org-set-local 'org-finish-function 'org-table-finish-edit-field)
2096 (org-set-local 'org-window-configuration cw)
2097 (org-set-local 'org-field-marker pos)
2098 (message "Edit and finish with C-c C-c")))))
2100 (defun org-table-finish-edit-field ()
2101 "Finish editing a table data field.
2102 Remove all newline characters, insert the result into the table, realign
2103 the table and kill the editing buffer."
2104 (let ((pos org-field-marker)
2105 (cw org-window-configuration)
2106 (cb (current-buffer))
2107 text)
2108 (goto-char (point-min))
2109 (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
2110 (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
2111 (replace-match " "))
2112 (setq text (org-trim (buffer-string)))
2113 (set-window-configuration cw)
2114 (kill-buffer cb)
2115 (select-window (get-buffer-window (marker-buffer pos)))
2116 (goto-char pos)
2117 (move-marker pos nil)
2118 (org-table-check-inside-data-field)
2119 (org-table-get-field nil text)
2120 (org-table-align)
2121 (message "New field value inserted")))
2123 (define-minor-mode org-table-follow-field-mode
2124 "Minor mode to make the table field editor window follow the cursor.
2125 When this mode is active, the field editor window will always show the
2126 current field. The mode exits automatically when the cursor leaves the
2127 table (but see `org-table-exit-follow-field-mode-when-leaving-table')."
2128 nil " TblFollow" nil
2129 (if org-table-follow-field-mode
2130 (org-add-hook 'post-command-hook 'org-table-follow-fields-with-editor
2131 'append 'local)
2132 (remove-hook 'post-command-hook 'org-table-follow-fields-with-editor 'local)
2133 (let* ((buf (get-buffer "*Org Table Edit Field*"))
2134 (win (and buf (get-buffer-window buf))))
2135 (when win (delete-window win))
2136 (when buf
2137 (with-current-buffer buf
2138 (move-marker org-field-marker nil))
2139 (kill-buffer buf)))))
2141 (defun org-table-follow-fields-with-editor ()
2142 (if (and org-table-exit-follow-field-mode-when-leaving-table
2143 (not (org-at-table-p)))
2144 ;; We have left the table, exit the follow mode
2145 (org-table-follow-field-mode -1)
2146 (when (org-table-check-inside-data-field 'noerror)
2147 (let ((win (selected-window)))
2148 (org-table-edit-field nil)
2149 (org-fit-window-to-buffer)
2150 (select-window win)))))
2152 (defvar org-timecnt) ; dynamically scoped parameter
2154 ;;;###autoload
2155 (defun org-table-sum (&optional beg end nlast)
2156 "Sum numbers in region of current table column.
2157 The result will be displayed in the echo area, and will be available
2158 as kill to be inserted with \\[yank].
2160 If there is an active region, it is interpreted as a rectangle and all
2161 numbers in that rectangle will be summed. If there is no active
2162 region and point is located in a table column, sum all numbers in that
2163 column.
2165 If at least one number looks like a time HH:MM or HH:MM:SS, all other
2166 numbers are assumed to be times as well (in decimal hours) and the
2167 numbers are added as such.
2169 If NLAST is a number, only the NLAST fields will actually be summed."
2170 (interactive)
2171 (save-excursion
2172 (let (col (org-timecnt 0) diff h m s org-table-clip)
2173 (cond
2174 ((and beg end)) ; beg and end given explicitly
2175 ((org-region-active-p)
2176 (setq beg (region-beginning) end (region-end)))
2178 (setq col (org-table-current-column))
2179 (goto-char (org-table-begin))
2180 (unless (re-search-forward "^[ \t]*|[^-]" nil t)
2181 (user-error "No table data"))
2182 (org-table-goto-column col)
2183 (setq beg (point))
2184 (goto-char (org-table-end))
2185 (unless (re-search-backward "^[ \t]*|[^-]" nil t)
2186 (user-error "No table data"))
2187 (org-table-goto-column col)
2188 (setq end (point))))
2189 (let* ((items (apply 'append (org-table-copy-region beg end)))
2190 (items1 (cond ((not nlast) items)
2191 ((>= nlast (length items)) items)
2192 (t (setq items (reverse items))
2193 (setcdr (nthcdr (1- nlast) items) nil)
2194 (nreverse items))))
2195 (numbers (delq nil (mapcar 'org-table-get-number-for-summing
2196 items1)))
2197 (res (apply '+ numbers))
2198 (sres (if (= org-timecnt 0)
2199 (number-to-string res)
2200 (setq diff (* 3600 res)
2201 h (floor (/ diff 3600)) diff (mod diff 3600)
2202 m (floor (/ diff 60)) diff (mod diff 60)
2203 s diff)
2204 (format "%.0f:%02.0f:%02.0f" h m s))))
2205 (kill-new sres)
2206 (if (org-called-interactively-p 'interactive)
2207 (message "%s"
2208 (substitute-command-keys
2209 (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
2210 (length numbers) sres))))
2211 sres))))
2213 (defun org-table-get-number-for-summing (s)
2214 (let (n)
2215 (if (string-match "^ *|? *" s)
2216 (setq s (replace-match "" nil nil s)))
2217 (if (string-match " *|? *$" s)
2218 (setq s (replace-match "" nil nil s)))
2219 (setq n (string-to-number s))
2220 (cond
2221 ((and (string-match "0" s)
2222 (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
2223 ((string-match "\\`[ \t]+\\'" s) nil)
2224 ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
2225 (let ((h (string-to-number (or (match-string 1 s) "0")))
2226 (m (string-to-number (or (match-string 2 s) "0")))
2227 (s (string-to-number (or (match-string 4 s) "0"))))
2228 (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
2229 (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
2230 ((equal n 0) nil)
2231 (t n))))
2233 (defun org-table-current-field-formula (&optional key noerror)
2234 "Return the formula active for the current field.
2235 Assumes that specials are in place.
2236 If KEY is given, return the key to this formula.
2237 Otherwise return the formula preceded with \"=\" or \":=\"."
2238 (let* ((name (car (rassoc (list (org-current-line)
2239 (org-table-current-column))
2240 org-table-named-field-locations)))
2241 (col (org-table-current-column))
2242 (scol (int-to-string col))
2243 (ref (format "@%d$%d" (org-table-current-dline) col))
2244 (stored-list (org-table-get-stored-formulas noerror))
2245 (ass (or (assoc name stored-list)
2246 (assoc ref stored-list)
2247 (assoc scol stored-list))))
2248 (if key
2249 (car ass)
2250 (if ass (concat (if (string-match "^[0-9]+$" (car ass)) "=" ":=")
2251 (cdr ass))))))
2253 (defun org-table-get-formula (&optional equation named)
2254 "Read a formula from the minibuffer, offer stored formula as default.
2255 When NAMED is non-nil, look for a named equation."
2256 (let* ((stored-list (org-table-get-stored-formulas))
2257 (name (car (rassoc (list (org-current-line)
2258 (org-table-current-column))
2259 org-table-named-field-locations)))
2260 (ref (format "@%d$%d" (org-table-current-dline)
2261 (org-table-current-column)))
2262 (refass (assoc ref stored-list))
2263 (nameass (assoc name stored-list))
2264 (scol (if named
2265 (if (and name (not (string-match "^LR[0-9]+$" name)))
2266 name
2267 ref)
2268 (int-to-string (org-table-current-column))))
2269 (dummy (and (or nameass refass) (not named)
2270 (not (y-or-n-p "Replace existing field formula with column formula? " ))
2271 (message "Formula not replaced")))
2272 (name (or name ref))
2273 (org-table-may-need-update nil)
2274 (stored (cdr (assoc scol stored-list)))
2275 (eq (cond
2276 ((and stored equation (string-match "^ *=? *$" equation))
2277 stored)
2278 ((stringp equation)
2279 equation)
2280 (t (org-table-formula-from-user
2281 (read-string
2282 (org-table-formula-to-user
2283 (format "%s formula %s%s="
2284 (if named "Field" "Column")
2285 (if (member (string-to-char scol) '(?$ ?@)) "" "$")
2286 scol))
2287 (if stored (org-table-formula-to-user stored) "")
2288 'org-table-formula-history
2289 )))))
2290 mustsave)
2291 (when (not (string-match "\\S-" eq))
2292 ;; remove formula
2293 (setq stored-list (delq (assoc scol stored-list) stored-list))
2294 (org-table-store-formulas stored-list)
2295 (user-error "Formula removed"))
2296 (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
2297 (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
2298 (if (and name (not named))
2299 ;; We set the column equation, delete the named one.
2300 (setq stored-list (delq (assoc name stored-list) stored-list)
2301 mustsave t))
2302 (if stored
2303 (setcdr (assoc scol stored-list) eq)
2304 (setq stored-list (cons (cons scol eq) stored-list)))
2305 (if (or mustsave (not (equal stored eq)))
2306 (org-table-store-formulas stored-list))
2307 eq))
2309 (defun org-table-store-formulas (alist)
2310 "Store the list of formulas below the current table."
2311 (setq alist (sort alist 'org-table-formula-less-p))
2312 (let ((case-fold-search t))
2313 (save-excursion
2314 (goto-char (org-table-end))
2315 (if (looking-at "\\([ \t]*\n\\)*[ \t]*\\(#\\+tblfm:\\)\\(.*\n?\\)")
2316 (progn
2317 ;; don't overwrite TBLFM, we might use text properties to store stuff
2318 (goto-char (match-beginning 3))
2319 (delete-region (match-beginning 3) (match-end 0)))
2320 (org-indent-line)
2321 (insert (or (match-string 2) "#+TBLFM:")))
2322 (insert " "
2323 (mapconcat (lambda (x)
2324 (concat
2325 (if (equal (string-to-char (car x)) ?@) "" "$")
2326 (car x) "=" (cdr x)))
2327 alist "::")
2328 "\n"))))
2330 (defsubst org-table-formula-make-cmp-string (a)
2331 (when (string-match "\\`$[<>]" a)
2332 (let ((arrow (string-to-char (substring a 1))))
2333 ;; Fake a high number to make sure this is sorted at the end.
2334 (setq a (org-table-formula-handle-first/last-rc a))
2335 (setq a (format "$%d" (+ 10000
2336 (if (= arrow ?<) -1000 0)
2337 (string-to-number (substring a 1)))))))
2338 (when (string-match
2339 "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?"
2341 (concat
2342 (if (match-end 2)
2343 (format "@%05d" (string-to-number (match-string 2 a))) "")
2344 (if (match-end 4)
2345 (format "$%05d" (string-to-number (match-string 4 a))) "")
2346 (if (match-end 5)
2347 (concat "@@" (match-string 5 a))))))
2349 (defun org-table-formula-less-p (a b)
2350 "Compare two formulas for sorting."
2351 (let ((as (org-table-formula-make-cmp-string (car a)))
2352 (bs (org-table-formula-make-cmp-string (car b))))
2353 (and as bs (string< as bs))))
2355 ;;;###autoload
2356 (defun org-table-get-stored-formulas (&optional noerror)
2357 "Return an alist with the stored formulas directly after current table."
2358 (interactive) ;; FIXME interactive?
2359 (let ((case-fold-search t) scol eq eq-alist strings string seen)
2360 (save-excursion
2361 (goto-char (org-table-end))
2362 (when (looking-at "\\([ \t]*\n\\)*[ \t]*#\\+tblfm: *\\(.*\\)")
2363 (setq strings (org-split-string (org-match-string-no-properties 2)
2364 " *:: *"))
2365 (while (setq string (pop strings))
2366 (when (string-match "\\`\\(@[-+I<>0-9.$@]+\\|@?[0-9]+\\|\\$\\([a-zA-Z0-9]+\\|[<>]+\\)\\) *= *\\(.*[^ \t]\\)" string)
2367 (setq scol (if (match-end 2)
2368 (match-string 2 string)
2369 (match-string 1 string))
2370 scol (if (member (string-to-char scol) '(?< ?>))
2371 (concat "$" scol) scol)
2372 eq (match-string 3 string)
2373 eq-alist (cons (cons scol eq) eq-alist))
2374 (if (member scol seen)
2375 (if noerror
2376 (progn
2377 (message "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
2378 (ding)
2379 (sit-for 2))
2380 (user-error "Double definition `$%s=' in TBLFM line, please fix by hand" scol))
2381 (push scol seen))))))
2382 (nreverse eq-alist)))
2384 (defun org-table-fix-formulas (key replace &optional limit delta remove)
2385 "Modify the equations after the table structure has been edited.
2386 KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
2387 For all numbers larger than LIMIT, shift them by DELTA."
2388 (save-excursion
2389 (goto-char (org-table-end))
2390 (while (let ((case-fold-search t)) (looking-at "[ \t]*#\\+tblfm:"))
2391 (let ((msg "The formulas in #+TBLFM have been updated")
2392 (re (concat key "\\([0-9]+\\)"))
2393 (re2
2394 (when remove
2395 (if (or (equal key "$") (equal key "$LR"))
2396 (format "\\(@[0-9]+\\)?%s%d=.*?\\(::\\|$\\)"
2397 (regexp-quote key) remove)
2398 (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
2399 s n a)
2400 (when remove
2401 (while (re-search-forward re2 (point-at-eol) t)
2402 (unless (save-match-data (org-in-regexp "remote([^)]+?)"))
2403 (if (equal (char-before (match-beginning 0)) ?.)
2404 (user-error
2405 "Change makes TBLFM term %s invalid, use undo to recover"
2406 (match-string 0))
2407 (replace-match "")))))
2408 (while (re-search-forward re (point-at-eol) t)
2409 (unless (save-match-data (org-in-regexp "remote([^)]+?)"))
2410 (setq s (match-string 1) n (string-to-number s))
2411 (cond
2412 ((setq a (assoc s replace))
2413 (replace-match (concat key (cdr a)) t t)
2414 (message msg))
2415 ((and limit (> n limit))
2416 (replace-match (concat key (int-to-string (+ n delta))) t t)
2417 (message msg))))))
2418 (forward-line))))
2420 (defun org-table-get-specials ()
2421 "Get the column names and local parameters for this table."
2422 (save-excursion
2423 (let ((beg (org-table-begin)) (end (org-table-end))
2424 names name fields fields1 field cnt
2425 c v l line col types dlines hlines last-dline)
2426 (setq org-table-column-names nil
2427 org-table-local-parameters nil
2428 org-table-named-field-locations nil
2429 org-table-current-begin-line nil
2430 org-table-current-begin-pos nil
2431 org-table-current-line-types nil
2432 org-table-current-ncol 0)
2433 (goto-char beg)
2434 (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
2435 (setq names (org-split-string (match-string 1) " *| *")
2436 cnt 1)
2437 (while (setq name (pop names))
2438 (setq cnt (1+ cnt))
2439 (if (string-match "^[a-zA-Z][_a-zA-Z0-9]*$" name)
2440 (push (cons name (int-to-string cnt)) org-table-column-names))))
2441 (setq org-table-column-names (nreverse org-table-column-names))
2442 (setq org-table-column-name-regexp
2443 (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
2444 (goto-char beg)
2445 (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
2446 (setq fields (org-split-string (match-string 1) " *| *"))
2447 (while (setq field (pop fields))
2448 (if (string-match "^\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
2449 (push (cons (match-string 1 field) (match-string 2 field))
2450 org-table-local-parameters))))
2451 (goto-char beg)
2452 (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
2453 (setq c (match-string 1)
2454 fields (org-split-string (match-string 2) " *| *"))
2455 (save-excursion
2456 (beginning-of-line (if (equal c "_") 2 0))
2457 (setq line (org-current-line) col 1)
2458 (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
2459 (setq fields1 (org-split-string (match-string 1) " *| *"))))
2460 (while (and fields1 (setq field (pop fields)))
2461 (setq v (pop fields1) col (1+ col))
2462 (when (and (stringp field) (stringp v)
2463 (string-match "^[a-zA-Z][_a-zA-Z0-9]*$" field))
2464 (push (cons field v) org-table-local-parameters)
2465 (push (list field line col) org-table-named-field-locations))))
2466 ;; Analyse the line types
2467 (goto-char beg)
2468 (setq org-table-current-begin-line (org-current-line)
2469 org-table-current-begin-pos (point)
2470 l org-table-current-begin-line)
2471 (while (looking-at "[ \t]*|\\(-\\)?")
2472 (push (if (match-end 1) 'hline 'dline) types)
2473 (if (match-end 1) (push l hlines) (push l dlines))
2474 (beginning-of-line 2)
2475 (setq l (1+ l)))
2476 (push 'hline types) ;; add an imaginary extra hline to the end
2477 (setq org-table-current-line-types (apply 'vector (nreverse types))
2478 last-dline (car dlines)
2479 org-table-dlines (apply 'vector (cons nil (nreverse dlines)))
2480 org-table-hlines (apply 'vector (cons nil (nreverse hlines))))
2481 (org-goto-line last-dline)
2482 (let* ((l last-dline)
2483 (fields (org-split-string
2484 (buffer-substring (point-at-bol) (point-at-eol))
2485 "[ \t]*|[ \t]*"))
2486 (nfields (length fields))
2487 al al2)
2488 (setq org-table-current-ncol nfields)
2489 (loop for i from 1 to nfields do
2490 (push (list (format "LR%d" i) l i) al)
2491 (push (cons (format "LR%d" i) (nth (1- i) fields)) al2))
2492 (setq org-table-named-field-locations
2493 (append org-table-named-field-locations al))
2494 (setq org-table-local-parameters
2495 (append org-table-local-parameters al2))))))
2497 ;;;###autoload
2498 (defun org-table-maybe-eval-formula ()
2499 "Check if the current field starts with \"=\" or \":=\".
2500 If yes, store the formula and apply it."
2501 ;; We already know we are in a table. Get field will only return a formula
2502 ;; when appropriate. It might return a separator line, but no problem.
2503 (when org-table-formula-evaluate-inline
2504 (let* ((field (org-trim (or (org-table-get-field) "")))
2505 named eq)
2506 (when (string-match "^:?=\\(.*[^=]\\)$" field)
2507 (setq named (equal (string-to-char field) ?:)
2508 eq (match-string 1 field))
2509 (if (or (fboundp 'calc-eval)
2510 (equal (substring eq 0 (min 2 (length eq))) "'("))
2511 (org-table-eval-formula (if named '(4) nil)
2512 (org-table-formula-from-user eq))
2513 (user-error "Calc does not seem to be installed, and is needed to evaluate the formula"))))))
2515 (defvar org-recalc-commands nil
2516 "List of commands triggering the recalculation of a line.
2517 Will be filled automatically during use.")
2519 (defvar org-recalc-marks
2520 '((" " . "Unmarked: no special line, no automatic recalculation")
2521 ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
2522 ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
2523 ("!" . "Column name definition line. Reference in formula as $name.")
2524 ("$" . "Parameter definition line name=value. Reference in formula as $name.")
2525 ("_" . "Names for values in row below this one.")
2526 ("^" . "Names for values in row above this one.")))
2528 ;;;###autoload
2529 (defun org-table-rotate-recalc-marks (&optional newchar)
2530 "Rotate the recalculation mark in the first column.
2531 If in any row, the first field is not consistent with a mark,
2532 insert a new column for the markers.
2533 When there is an active region, change all the lines in the region,
2534 after prompting for the marking character.
2535 After each change, a message will be displayed indicating the meaning
2536 of the new mark."
2537 (interactive)
2538 (unless (org-at-table-p) (user-error "Not at a table"))
2539 (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
2540 (beg (org-table-begin))
2541 (end (org-table-end))
2542 (l (org-current-line))
2543 (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
2544 (l2 (if (org-region-active-p) (org-current-line (region-end))))
2545 (have-col
2546 (save-excursion
2547 (goto-char beg)
2548 (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
2549 (col (org-table-current-column))
2550 (forcenew (car (assoc newchar org-recalc-marks)))
2551 epos new)
2552 (when l1
2553 (message "Change region to what mark? Type # * ! $ or SPC: ")
2554 (setq newchar (char-to-string (read-char-exclusive))
2555 forcenew (car (assoc newchar org-recalc-marks))))
2556 (if (and newchar (not forcenew))
2557 (user-error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
2558 newchar))
2559 (if l1 (org-goto-line l1))
2560 (save-excursion
2561 (beginning-of-line 1)
2562 (unless (looking-at org-table-dataline-regexp)
2563 (user-error "Not at a table data line")))
2564 (unless have-col
2565 (org-table-goto-column 1)
2566 (org-table-insert-column)
2567 (org-table-goto-column (1+ col)))
2568 (setq epos (point-at-eol))
2569 (save-excursion
2570 (beginning-of-line 1)
2571 (org-table-get-field
2572 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
2573 (concat " "
2574 (setq new (or forcenew
2575 (cadr (member (match-string 1) marks))))
2576 " ")
2577 " # ")))
2578 (if (and l1 l2)
2579 (progn
2580 (org-goto-line l1)
2581 (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
2582 (and (looking-at org-table-dataline-regexp)
2583 (org-table-get-field 1 (concat " " new " "))))
2584 (org-goto-line l1)))
2585 (if (not (= epos (point-at-eol))) (org-table-align))
2586 (org-goto-line l)
2587 (and (org-called-interactively-p 'interactive)
2588 (message "%s" (cdr (assoc new org-recalc-marks))))))
2590 ;;;###autoload
2591 (defun org-table-maybe-recalculate-line ()
2592 "Recompute the current line if marked for it, and if we haven't just done it."
2593 (interactive)
2594 (and org-table-allow-automatic-line-recalculation
2595 (not (and (memq last-command org-recalc-commands)
2596 (equal org-last-recalc-line (org-current-line))))
2597 (save-excursion (beginning-of-line 1)
2598 (looking-at org-table-auto-recalculate-regexp))
2599 (org-table-recalculate) t))
2601 (defvar org-tbl-calc-modes) ;; Dynamically bound in `org-table-eval-formula'
2602 (defsubst org-set-calc-mode (var &optional value)
2603 (if (stringp var)
2604 (setq var (assoc var '(("D" calc-angle-mode deg)
2605 ("R" calc-angle-mode rad)
2606 ("F" calc-prefer-frac t)
2607 ("S" calc-symbolic-mode t)))
2608 value (nth 2 var) var (nth 1 var)))
2609 (if (memq var org-tbl-calc-modes)
2610 (setcar (cdr (memq var org-tbl-calc-modes)) value)
2611 (cons var (cons value org-tbl-calc-modes)))
2612 org-tbl-calc-modes)
2614 ;;;###autoload
2615 (defun org-table-eval-formula (&optional arg equation
2616 suppress-align suppress-const
2617 suppress-store suppress-analysis)
2618 "Replace the table field value at the cursor by the result of a calculation.
2620 This function makes use of Dave Gillespie's Calc package, in my view the
2621 most exciting program ever written for GNU Emacs. So you need to have Calc
2622 installed in order to use this function.
2624 In a table, this command replaces the value in the current field with the
2625 result of a formula. It also installs the formula as the \"current\" column
2626 formula, by storing it in a special line below the table. When called
2627 with a `C-u' prefix, the current field must be a named field, and the
2628 formula is installed as valid in only this specific field.
2630 When called with two `C-u' prefixes, insert the active equation
2631 for the field back into the current field, so that it can be
2632 edited there. This is useful in order to use \\[org-table-show-reference]
2633 to check the referenced fields.
2635 When called, the command first prompts for a formula, which is read in
2636 the minibuffer. Previously entered formulas are available through the
2637 history list, and the last used formula is offered as a default.
2638 These stored formulas are adapted correctly when moving, inserting, or
2639 deleting columns with the corresponding commands.
2641 The formula can be any algebraic expression understood by the Calc package.
2642 For details, see the Org-mode manual.
2644 This function can also be called from Lisp programs and offers
2645 additional arguments: EQUATION can be the formula to apply. If this
2646 argument is given, the user will not be prompted. SUPPRESS-ALIGN is
2647 used to speed-up recursive calls by by-passing unnecessary aligns.
2648 SUPPRESS-CONST suppresses the interpretation of constants in the
2649 formula, assuming that this has been done already outside the function.
2650 SUPPRESS-STORE means the formula should not be stored, either because
2651 it is already stored, or because it is a modified equation that should
2652 not overwrite the stored one."
2653 (interactive "P")
2654 (org-table-check-inside-data-field)
2655 (or suppress-analysis (org-table-get-specials))
2656 (if (equal arg '(16))
2657 (let ((eq (org-table-current-field-formula)))
2658 (or eq (user-error "No equation active for current field"))
2659 (org-table-get-field nil eq)
2660 (org-table-align)
2661 (setq org-table-may-need-update t))
2662 (let* (fields
2663 (ndown (if (integerp arg) arg 1))
2664 (org-table-automatic-realign nil)
2665 (case-fold-search nil)
2666 (down (> ndown 1))
2667 (formula (if (and equation suppress-store)
2668 equation
2669 (org-table-get-formula equation (equal arg '(4)))))
2670 (n0 (org-table-current-column))
2671 (org-tbl-calc-modes (copy-sequence org-calc-default-modes))
2672 (numbers nil) ; was a variable, now fixed default
2673 (keep-empty nil)
2674 n form form0 formrpl formrg bw fmt x ev orig c lispp literal
2675 duration duration-output-format)
2676 ;; Parse the format string. Since we have a lot of modes, this is
2677 ;; a lot of work. However, I think calc still uses most of the time.
2678 (if (string-match ";" formula)
2679 (let ((tmp (org-split-string formula ";")))
2680 (setq formula (car tmp)
2681 fmt (concat (cdr (assoc "%" org-table-local-parameters))
2682 (nth 1 tmp)))
2683 (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
2684 (setq c (string-to-char (match-string 1 fmt))
2685 n (string-to-number (match-string 2 fmt)))
2686 (if (= c ?p)
2687 (setq org-tbl-calc-modes (org-set-calc-mode 'calc-internal-prec n))
2688 (setq org-tbl-calc-modes
2689 (org-set-calc-mode
2690 'calc-float-format
2691 (list (cdr (assoc c '((?n . float) (?f . fix)
2692 (?s . sci) (?e . eng))))
2693 n))))
2694 (setq fmt (replace-match "" t t fmt)))
2695 (if (string-match "T" fmt)
2696 (setq duration t numbers t
2697 duration-output-format nil
2698 fmt (replace-match "" t t fmt)))
2699 (if (string-match "t" fmt)
2700 (setq duration t
2701 duration-output-format org-table-duration-custom-format
2702 numbers t
2703 fmt (replace-match "" t t fmt)))
2704 (if (string-match "N" fmt)
2705 (setq numbers t
2706 fmt (replace-match "" t t fmt)))
2707 (if (string-match "L" fmt)
2708 (setq literal t
2709 fmt (replace-match "" t t fmt)))
2710 (if (string-match "E" fmt)
2711 (setq keep-empty t
2712 fmt (replace-match "" t t fmt)))
2713 (while (string-match "[DRFS]" fmt)
2714 (setq org-tbl-calc-modes (org-set-calc-mode (match-string 0 fmt)))
2715 (setq fmt (replace-match "" t t fmt)))
2716 (unless (string-match "\\S-" fmt)
2717 (setq fmt nil))))
2718 (if (and (not suppress-const) org-table-formula-use-constants)
2719 (setq formula (org-table-formula-substitute-names formula)))
2720 (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
2721 (while (> ndown 0)
2722 (setq fields (org-split-string
2723 (buffer-substring-no-properties (point-at-bol) (point-at-eol))
2724 " *| *"))
2725 ;; replace fields with duration values if relevant
2726 (if duration
2727 (setq fields
2728 (mapcar (lambda (x) (org-table-time-string-to-seconds x))
2729 fields)))
2730 (if (eq numbers t)
2731 (setq fields (mapcar
2732 (lambda (x)
2733 (if (string-match "\\S-" x)
2734 (number-to-string (string-to-number x))
2736 fields)))
2737 (setq ndown (1- ndown))
2738 (setq form (copy-sequence formula)
2739 lispp (and (> (length form) 2) (equal (substring form 0 2) "'(")))
2740 (if (and lispp literal) (setq lispp 'literal))
2742 ;; Insert row and column number of formula result field
2743 (while (string-match "[@$]#" form)
2744 (setq form
2745 (replace-match
2746 (format "%d"
2747 (save-match-data
2748 (if (equal (substring form (match-beginning 0)
2749 (1+ (match-beginning 0)))
2750 "@")
2751 (org-table-current-dline)
2752 (org-table-current-column))))
2753 t t form)))
2755 ;; Check for old vertical references
2756 (org-table--error-on-old-row-references form)
2757 ;; Insert remote references
2758 (setq form (org-table-remote-reference-indirection form))
2759 (while (string-match "\\<remote([ \t]*\\([^,)]+\\)[ \t]*,[ \t]*\\([^\n)]+\\))" form)
2760 (setq form
2761 (replace-match
2762 (save-match-data
2763 (org-table-make-reference
2764 (let ((rmtrng (org-table-get-remote-range
2765 (match-string 1 form) (match-string 2 form))))
2766 (if duration
2767 (if (listp rmtrng)
2768 (mapcar (lambda(x) (org-table-time-string-to-seconds x)) rmtrng)
2769 (org-table-time-string-to-seconds rmtrng))
2770 rmtrng))
2771 keep-empty numbers lispp))
2772 t t form)))
2773 ;; Insert complex ranges
2774 (while (and (string-match org-table-range-regexp form)
2775 (> (length (match-string 0 form)) 1))
2776 (setq formrg (save-match-data
2777 (org-table-get-range (match-string 0 form) nil n0)))
2778 (setq formrpl
2779 (save-match-data
2780 (org-table-make-reference
2781 ;; possibly handle durations
2782 (if duration
2783 (if (listp formrg)
2784 (mapcar (lambda(x) (org-table-time-string-to-seconds x)) formrg)
2785 (org-table-time-string-to-seconds formrg))
2786 formrg)
2787 keep-empty numbers lispp)))
2788 (if (not (save-match-data
2789 (string-match (regexp-quote form) formrpl)))
2790 (setq form (replace-match formrpl t t form))
2791 (user-error "Spreadsheet error: invalid reference \"%s\"" form)))
2792 ;; Insert simple ranges, i.e. included in the current row.
2793 (while (string-match
2794 "\\$\\(\\([-+]\\)?[0-9]+\\)\\.\\.\\$\\(\\([-+]\\)?[0-9]+\\)"
2795 form)
2796 (setq form
2797 (replace-match
2798 (save-match-data
2799 (org-table-make-reference
2800 (org-sublist fields
2801 (+ (if (match-end 2) n0 0)
2802 (string-to-number (match-string 1 form)))
2803 (+ (if (match-end 4) n0 0)
2804 (string-to-number (match-string 3 form))))
2805 keep-empty numbers lispp))
2806 t t form)))
2807 (setq form0 form)
2808 ;; Insert the references to fields in same row
2809 (while (string-match "\\$\\(\\([-+]\\)?[0-9]+\\)" form)
2810 (setq n (+ (string-to-number (match-string 1 form))
2811 (if (match-end 2) n0 0))
2812 x (nth (1- (if (= n 0) n0 (max n 1))) fields)
2813 formrpl (save-match-data
2814 (org-table-make-reference
2815 x keep-empty numbers lispp)))
2816 (when (or (not x)
2817 (save-match-data
2818 (string-match (regexp-quote formula) formrpl)))
2819 (user-error "Invalid field specifier \"%s\""
2820 (match-string 0 form)))
2821 (setq form (replace-match formrpl t t form)))
2823 (if lispp
2824 (setq ev (condition-case nil
2825 (eval (eval (read form)))
2826 (error "#ERROR"))
2827 ev (if (numberp ev) (number-to-string ev) ev)
2828 ev (if duration (org-table-time-seconds-to-string
2829 (string-to-number ev)
2830 duration-output-format) ev))
2831 (or (fboundp 'calc-eval)
2832 (user-error "Calc does not seem to be installed, and is needed to evaluate the formula"))
2833 ;; Use <...> time-stamps so that Calc can handle them
2834 (while (string-match (concat "\\[" org-ts-regexp1 "\\]") form)
2835 (setq form (replace-match "<\\1>" nil nil form)))
2836 ;; I18n-ize local time-stamps by setting (system-time-locale "C")
2837 (when (string-match org-ts-regexp2 form)
2838 (let* ((ts (match-string 0 form))
2839 (tsp (apply 'encode-time (save-match-data (org-parse-time-string ts))))
2840 (system-time-locale "C")
2841 (tf (or (and (save-match-data (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts))
2842 (cdr org-time-stamp-formats))
2843 (car org-time-stamp-formats))))
2844 (setq form (replace-match (format-time-string tf tsp) t t form))))
2846 (setq ev (if (and duration (string-match "^[0-9]+:[0-9]+\\(?::[0-9]+\\)?$" form))
2847 form
2848 (calc-eval (cons form org-tbl-calc-modes)
2849 (when (and (not keep-empty) numbers) 'num)))
2850 ev (if duration (org-table-time-seconds-to-string
2851 (if (string-match "^[0-9]+:[0-9]+\\(?::[0-9]+\\)?$" ev)
2852 (string-to-number (org-table-time-string-to-seconds ev))
2853 (string-to-number ev))
2854 duration-output-format)
2855 ev)))
2857 (when org-table-formula-debug
2858 (with-output-to-temp-buffer "*Substitution History*"
2859 (princ (format "Substitution history of formula
2860 Orig: %s
2861 $xyz-> %s
2862 @r$c-> %s
2863 $1-> %s\n" orig formula form0 form))
2864 (if (consp ev)
2865 (princ (format " %s^\nError: %s"
2866 (make-string (car ev) ?\-) (nth 1 ev)))
2867 (princ (format "Result: %s\nFormat: %s\nFinal: %s"
2868 ev (or fmt "NONE")
2869 (if fmt (format fmt (string-to-number ev)) ev)))))
2870 (setq bw (get-buffer-window "*Substitution History*"))
2871 (org-fit-window-to-buffer bw)
2872 (unless (and (org-called-interactively-p 'any) (not ndown))
2873 (unless (let (inhibit-redisplay)
2874 (y-or-n-p "Debugging Formula. Continue to next? "))
2875 (org-table-align)
2876 (user-error "Abort"))
2877 (delete-window bw)
2878 (message "")))
2879 (when (consp ev) (setq fmt nil ev "#ERROR"))
2880 (org-table-justify-field-maybe
2881 (format org-table-formula-field-format
2882 (if fmt (format fmt (string-to-number ev)) ev)))
2883 (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
2884 (call-interactively 'org-return)
2885 (setq ndown 0)))
2886 (and down (org-table-maybe-recalculate-line))
2887 (or suppress-align (and org-table-may-need-update
2888 (org-table-align))))))
2890 (defun org-table-put-field-property (prop value)
2891 (save-excursion
2892 (put-text-property (progn (skip-chars-backward "^|") (point))
2893 (progn (skip-chars-forward "^|") (point))
2894 prop value)))
2896 (defun org-table-get-range (desc &optional tbeg col highlight corners-only)
2897 "Get a calc vector from a column, according to descriptor DESC.
2898 Optional arguments TBEG and COL can give the beginning of the table and
2899 the current column, to avoid unnecessary parsing.
2901 HIGHLIGHT means just highlight the range.
2903 When CORNERS-ONLY is set, only return the corners of the range as
2904 a list (line1 column1 line2 column2) where line1 and line2 are line numbers
2905 in the buffer and column1 and column2 are table column numbers."
2906 (if (not (equal (string-to-char desc) ?@))
2907 (setq desc (concat "@" desc)))
2908 (save-excursion
2909 (or tbeg (setq tbeg (org-table-begin)))
2910 (or col (setq col (org-table-current-column)))
2911 (let ((thisline (org-current-line))
2912 beg end c1 c2 r1 r2 rangep tmp)
2913 (unless (string-match org-table-range-regexp desc)
2914 (user-error "Invalid table range specifier `%s'" desc))
2915 (setq rangep (match-end 3)
2916 r1 (and (match-end 1) (match-string 1 desc))
2917 r2 (and (match-end 4) (match-string 4 desc))
2918 c1 (and (match-end 2) (substring (match-string 2 desc) 1))
2919 c2 (and (match-end 5) (substring (match-string 5 desc) 1)))
2921 (and c1 (setq c1 (+ (string-to-number c1)
2922 (if (memq (string-to-char c1) '(?- ?+)) col 0))))
2923 (and c2 (setq c2 (+ (string-to-number c2)
2924 (if (memq (string-to-char c2) '(?- ?+)) col 0))))
2925 (if (equal r1 "") (setq r1 nil))
2926 (if (equal r2 "") (setq r2 nil))
2927 (if r1 (setq r1 (org-table-get-descriptor-line r1)))
2928 (if r2 (setq r2 (org-table-get-descriptor-line r2)))
2929 ; (setq r2 (or r2 r1) c2 (or c2 c1))
2930 (if (not r1) (setq r1 thisline))
2931 (if (not r2) (setq r2 thisline))
2932 (if (or (not c1) (= 0 c1)) (setq c1 col))
2933 (if (or (not c2) (= 0 c2)) (setq c2 col))
2934 (if (and (not corners-only)
2935 (or (not rangep) (and (= r1 r2) (= c1 c2))))
2936 ;; just one field
2937 (progn
2938 (org-goto-line r1)
2939 (while (not (looking-at org-table-dataline-regexp))
2940 (beginning-of-line 2))
2941 (prog1 (org-trim (org-table-get-field c1))
2942 (if highlight (org-table-highlight-rectangle (point) (point)))))
2943 ;; A range, return a vector
2944 ;; First sort the numbers to get a regular rectangle
2945 (if (< r2 r1) (setq tmp r1 r1 r2 r2 tmp))
2946 (if (< c2 c1) (setq tmp c1 c1 c2 c2 tmp))
2947 (if corners-only
2948 ;; Only return the corners of the range
2949 (list r1 c1 r2 c2)
2950 ;; Copy the range values into a list
2951 (org-goto-line r1)
2952 (while (not (looking-at org-table-dataline-regexp))
2953 (beginning-of-line 2))
2954 (org-table-goto-column c1)
2955 (setq beg (point))
2956 (org-goto-line r2)
2957 (while (not (looking-at org-table-dataline-regexp))
2958 (beginning-of-line 0))
2959 (org-table-goto-column c2)
2960 (setq end (point))
2961 (if highlight
2962 (org-table-highlight-rectangle
2963 beg (progn (skip-chars-forward "^|\n") (point))))
2964 ;; return string representation of calc vector
2965 (mapcar 'org-trim
2966 (apply 'append (org-table-copy-region beg end))))))))
2968 (defun org-table-get-descriptor-line (desc &optional cline bline table)
2969 "Analyze descriptor DESC and retrieve the corresponding line number.
2970 The cursor is currently in line CLINE, the table begins in line BLINE,
2971 and TABLE is a vector with line types."
2972 (if (string-match "^[0-9]+$" desc)
2973 (aref org-table-dlines (string-to-number desc))
2974 (setq cline (or cline (org-current-line))
2975 bline (or bline org-table-current-begin-line)
2976 table (or table org-table-current-line-types))
2977 (if (or
2978 (not (string-match "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?" desc))
2979 ;; 1 2 3 4 5 6
2980 (and (not (match-end 3)) (not (match-end 6)))
2981 (and (match-end 3) (match-end 6) (not (match-end 5))))
2982 (user-error "Invalid row descriptor `%s'" desc))
2983 (let* ((hdir (and (match-end 2) (match-string 2 desc)))
2984 (hn (if (match-end 3) (- (match-end 3) (match-beginning 3)) nil))
2985 (odir (and (match-end 5) (match-string 5 desc)))
2986 (on (if (match-end 6) (string-to-number (match-string 6 desc))))
2987 (i (- cline bline))
2988 (rel (and (match-end 6)
2989 (or (and (match-end 1) (not (match-end 3)))
2990 (match-end 5)))))
2991 (if (and hn (not hdir))
2992 (progn
2993 (setq i 0 hdir "+")
2994 (if (eq (aref table 0) 'hline) (setq hn (1- hn)))))
2995 (if (and (not hn) on (not odir))
2996 (user-error "Should never happen");;(aref org-table-dlines on)
2997 (if (and hn (> hn 0))
2998 (setq i (org-table-find-row-type table i 'hline (equal hdir "-")
2999 nil hn cline desc)))
3000 (if on
3001 (setq i (org-table-find-row-type table i 'dline (equal odir "-")
3002 rel on cline desc)))
3003 (+ bline i)))))
3005 (defun org-table-find-row-type (table i type backwards relative n cline desc)
3006 "FIXME: Needs more documentation."
3007 (let ((l (length table)))
3008 (while (> n 0)
3009 (while (and (setq i (+ i (if backwards -1 1)))
3010 (>= i 0) (< i l)
3011 (not (eq (aref table i) type))
3012 (if (and relative (eq (aref table i) 'hline))
3013 (cond
3014 ((eq org-table-relative-ref-may-cross-hline t) t)
3015 ((eq org-table-relative-ref-may-cross-hline 'error)
3016 (user-error "Row descriptor %s used in line %d crosses hline" desc cline))
3017 (t (setq i (- i (if backwards -1 1))
3018 n 1)
3019 nil))
3020 t)))
3021 (setq n (1- n)))
3022 (cond ((or (< i 0) (>= i l))
3023 (user-error "Row descriptor %s used in line %d leads outside table"
3024 desc cline))
3025 ;; The last hline doesn't exist. Instead, point to last row
3026 ;; in table.
3027 ((= i (1- l)) (1- i))
3028 (t i))))
3030 (defun org-table--error-on-old-row-references (s)
3031 (when (string-match "&[-+0-9I]" s)
3032 (user-error "Formula contains old &row reference, please rewrite using @-syntax")))
3034 (defun org-table-make-reference (elements keep-empty numbers lispp)
3035 "Convert list ELEMENTS to something appropriate to insert into formula.
3036 KEEP-EMPTY indicated to keep empty fields, default is to skip them.
3037 NUMBERS indicates that everything should be converted to numbers.
3038 LISPP non-nil means to return something appropriate for a Lisp
3039 list, 'literal is for the format specifier L."
3040 ;; Calc nan (not a number) is used for the conversion of the empty
3041 ;; field to a reference for several reasons: (i) It is accepted in a
3042 ;; Calc formula (e. g. "" or "()" would result in a Calc error).
3043 ;; (ii) In a single field (not in range) it can be distinguished
3044 ;; from "(nan)" which is the reference made from a single field
3045 ;; containing "nan".
3046 (if (stringp elements)
3047 ;; field reference
3048 (if lispp
3049 (if (eq lispp 'literal)
3050 elements
3051 (if (and (eq elements "") (not keep-empty))
3053 (prin1-to-string
3054 (if numbers (string-to-number elements) elements))))
3055 (if (string-match "\\S-" elements)
3056 (progn
3057 (when numbers (setq elements (number-to-string
3058 (string-to-number elements))))
3059 (concat "(" elements ")"))
3060 (if (or (not keep-empty) numbers) "(0)" "nan")))
3061 ;; range reference
3062 (unless keep-empty
3063 (setq elements
3064 (delq nil
3065 (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
3066 elements))))
3067 (setq elements (or elements '())) ; if delq returns nil then we need '()
3068 (if lispp
3069 (mapconcat
3070 (lambda (x)
3071 (if (eq lispp 'literal)
3073 (prin1-to-string (if numbers (string-to-number x) x))))
3074 elements " ")
3075 (concat "[" (mapconcat
3076 (lambda (x)
3077 (if (string-match "\\S-" x)
3078 (if numbers
3079 (number-to-string (string-to-number x))
3081 (if (or (not keep-empty) numbers) "0" "nan")))
3082 elements
3083 ",") "]"))))
3085 (defun org-table-message-once-per-second (t1 &rest args)
3086 "If there has been more than one second since T1, display message.
3087 ARGS are passed as arguments to the `message' function. Returns
3088 current time if a message is printed, otherwise returns T1. If
3089 T1 is nil, always messages."
3090 (let ((curtime (current-time)))
3091 (if (or (not t1) (< 0 (nth 1 (time-subtract curtime t1))))
3092 (progn (apply 'message args)
3093 curtime)
3094 t1)))
3096 ;;;###autoload
3097 (defun org-table-recalculate (&optional all noalign)
3098 "Recalculate the current table line by applying all stored formulas.
3099 With prefix arg ALL, do this for all lines in the table.
3100 With the prefix argument ALL is `(16)' \
3101 \(a double \\[universal-prefix] \\[universal-prefix] prefix), or if
3102 it is the symbol `iterate', recompute the table until it no longer changes.
3103 If NOALIGN is not nil, do not re-align the table after the computations
3104 are done. This is typically used internally to save time, if it is
3105 known that the table will be realigned a little later anyway."
3106 (interactive "P")
3107 (or (memq this-command org-recalc-commands)
3108 (setq org-recalc-commands (cons this-command org-recalc-commands)))
3109 (unless (org-at-table-p) (user-error "Not at a table"))
3110 (if (or (eq all 'iterate) (equal all '(16)))
3111 (org-table-iterate)
3112 (org-table-get-specials)
3113 (let* ((eqlist (sort (org-table-get-stored-formulas)
3114 (lambda (a b) (string< (car a) (car b)))))
3115 (eqlist1 (copy-sequence eqlist))
3116 (inhibit-redisplay (not debug-on-error))
3117 (line-re org-table-dataline-regexp)
3118 (thisline (org-current-line))
3119 (thiscol (org-table-current-column))
3120 (log-first-time (current-time))
3121 (log-last-time log-first-time)
3122 seen-fields lhs1
3123 beg end entry eqlnum eqlname eqlname1 eql (cnt 0) eq a name name1)
3124 ;; Insert constants in all formulas
3125 (when eqlist
3126 (setq eqlist
3127 (mapcar
3128 (lambda (x)
3129 (if (string-match "^@-?I+" (car x))
3130 (user-error "Can't assign to hline relative reference"))
3131 (when (string-match "\\`$[<>]" (car x))
3132 (setq lhs1 (car x))
3133 (setq x (cons (substring
3134 (org-table-formula-handle-first/last-rc
3135 (car x)) 1)
3136 (cdr x)))
3137 (if (assoc (car x) eqlist1)
3138 (user-error "\"%s=\" formula tries to overwrite existing formula for column %s"
3139 lhs1 (car x))))
3140 (cons
3141 (org-table-formula-handle-first/last-rc (car x))
3142 (org-table-formula-substitute-names
3143 (org-table-formula-handle-first/last-rc (cdr x)))))
3144 eqlist))
3145 ;; Split the equation list
3146 (while (setq eq (pop eqlist))
3147 (if (<= (string-to-char (car eq)) ?9)
3148 (push eq eqlnum)
3149 (push eq eqlname)))
3150 (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
3151 ;; Expand ranges in lhs of formulas
3152 (setq eqlname (org-table-expand-lhs-ranges eqlname))
3154 ;; Get the correct line range to process
3155 (if all
3156 (progn
3157 (setq end (copy-marker (1+ (org-table-end))))
3158 (goto-char (setq beg (org-table-begin)))
3159 (if (re-search-forward org-table-calculate-mark-regexp end t)
3160 ;; This is a table with marked lines, compute selected lines
3161 (setq line-re org-table-recalculate-regexp)
3162 ;; Move forward to the first non-header line
3163 (if (and (re-search-forward org-table-dataline-regexp end t)
3164 (re-search-forward org-table-hline-regexp end t)
3165 (re-search-forward org-table-dataline-regexp end t))
3166 (setq beg (match-beginning 0))
3167 nil))) ;; just leave beg where it is
3168 (setq beg (point-at-bol)
3169 end (move-marker (make-marker) (1+ (point-at-eol)))))
3170 (goto-char beg)
3172 ;; First find the named fields, and mark them untouchable.
3173 ;; Also check if several field/range formulas try to set the same field.
3174 (remove-text-properties beg end '(org-untouchable t))
3175 (while (setq eq (pop eqlname))
3176 (setq name (car eq)
3177 a (assoc name org-table-named-field-locations))
3178 (setq name1 name)
3179 (if a (setq name1 (format "@%d$%d" (org-table-line-to-dline (nth 1 a))
3180 (nth 2 a))))
3181 (when (member name1 seen-fields)
3182 (user-error "Several field/range formulas try to set %s" name1))
3183 (push name1 seen-fields)
3185 (and (not a)
3186 (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" name)
3187 (setq a (list name
3188 (condition-case nil
3189 (aref org-table-dlines
3190 (string-to-number (match-string 1 name)))
3191 (error (user-error "Invalid row number in %s"
3192 name)))
3193 (string-to-number (match-string 2 name)))))
3194 (when (and a (or all (equal (nth 1 a) thisline)))
3195 (setq log-last-time
3196 (org-table-message-once-per-second
3197 (and all log-last-time)
3198 "Re-applying formula to field: %s" name))
3199 (org-goto-line (nth 1 a))
3200 (org-table-goto-column (nth 2 a))
3201 (push (append a (list (cdr eq))) eqlname1)
3202 (org-table-put-field-property :org-untouchable t)))
3203 (setq eqlname1 (nreverse eqlname1))
3205 ;; Now evaluate the column formulas, but skip fields covered
3206 ;; by field formulas
3207 (goto-char beg)
3208 (while (re-search-forward line-re end t)
3209 (unless (string-match "^ *[_^!$/] *$" (org-table-get-field 1))
3210 ;; Unprotected line, recalculate
3211 (setq cnt (1+ cnt))
3212 (and all (setq log-last-time
3213 (org-table-message-once-per-second
3214 log-last-time
3215 "Re-applying formulas to full table...(line %d)" cnt)))
3216 (setq org-last-recalc-line (org-current-line))
3217 (setq eql eqlnum)
3218 (while (setq entry (pop eql))
3219 (org-goto-line org-last-recalc-line)
3220 (org-table-goto-column (string-to-number (car entry)) nil 'force)
3221 (unless (get-text-property (point) :org-untouchable)
3222 (org-table-eval-formula
3223 nil (cdr entry)
3224 'noalign 'nocst 'nostore 'noanalysis)))))
3226 ;; Now evaluate the field formulas
3227 (while (setq eq (pop eqlname1))
3228 (setq log-last-time
3229 (org-table-message-once-per-second
3230 (and all log-last-time)
3231 "Re-applying formula to field: %s" (car eq)))
3232 (org-goto-line (nth 1 eq))
3233 (let ((column-target (nth 2 eq)))
3234 (when (> column-target 1000)
3235 (user-error "Formula column target too large"))
3236 (let* ((column-count (progn (end-of-line)
3237 (1- (org-table-current-column))))
3238 (create-new-column
3239 (and (> column-target column-count)
3240 (or (eq org-table-formula-create-columns t)
3241 (and
3242 (eq org-table-formula-create-columns 'warn)
3243 (progn
3244 (org-display-warning
3245 "Out-of-bounds formula added columns")
3247 (and
3248 (eq org-table-formula-create-columns 'prompt)
3249 (yes-or-no-p
3250 "Out-of-bounds formula. Add columns?"))))))
3251 (org-table-goto-column column-target nil create-new-column))
3253 (org-table-eval-formula nil (nth 3 eq) 'noalign 'nocst
3254 'nostore 'noanalysis)))
3256 (org-goto-line thisline)
3257 (org-table-goto-column thiscol)
3258 (remove-text-properties (point-min) (point-max) '(org-untouchable t))
3259 (or noalign (and org-table-may-need-update (org-table-align))
3260 (and all (org-table-message-once-per-second
3261 log-first-time
3262 "Re-applying formulas to %d lines...done" cnt)))
3265 ;; back to initial position
3266 (org-table-message-once-per-second
3267 (and all log-first-time)
3268 "Re-applying formulas...done")
3270 (org-goto-line thisline)
3271 (org-table-goto-column thiscol)
3272 (or noalign (and org-table-may-need-update (org-table-align)))))))
3274 ;;;###autoload
3275 (defun org-table-iterate (&optional arg)
3276 "Recalculate the table until it does not change anymore.
3277 The maximum number of iterations is 10, but you can choose a different value
3278 with the prefix ARG."
3279 (interactive "P")
3280 (let ((imax (if arg (prefix-numeric-value arg) 10))
3281 (i 0)
3282 (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
3283 thistbl)
3284 (catch 'exit
3285 (while (< i imax)
3286 (setq i (1+ i))
3287 (org-table-recalculate 'all)
3288 (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
3289 (if (not (string= lasttbl thistbl))
3290 (setq lasttbl thistbl)
3291 (if (> i 1)
3292 (message "Convergence after %d iterations" i)
3293 (message "Table was already stable"))
3294 (throw 'exit t)))
3295 (user-error "No convergence after %d iterations" i))))
3297 ;;;###autoload
3298 (defun org-table-recalculate-buffer-tables ()
3299 "Recalculate all tables in the current buffer."
3300 (interactive)
3301 (save-excursion
3302 (save-restriction
3303 (widen)
3304 (org-table-map-tables (lambda () (org-table-recalculate t)) t))))
3306 ;;;###autoload
3307 (defun org-table-iterate-buffer-tables ()
3308 "Iterate all tables in the buffer, to converge inter-table dependencies."
3309 (interactive)
3310 (let* ((imax 10)
3311 (i imax)
3312 (checksum (md5 (buffer-string)))
3314 (save-excursion
3315 (save-restriction
3316 (widen)
3317 (catch 'exit
3318 (while (> i 0)
3319 (setq i (1- i))
3320 (org-table-map-tables (lambda () (org-table-recalculate t)) t)
3321 (if (equal checksum (setq c1 (md5 (buffer-string))))
3322 (progn
3323 (message "Convergence after %d iterations" (- imax i))
3324 (throw 'exit t))
3325 (setq checksum c1)))
3326 (user-error "No convergence after %d iterations" imax))))))
3328 (defun org-table-calc-current-TBLFM (&optional arg)
3329 "Apply the #+TBLFM in the line at point to the table."
3330 (interactive "P")
3331 (unless (org-at-TBLFM-p) (user-error "Not at a #+TBLFM line"))
3332 (let ((formula (buffer-substring
3333 (line-beginning-position)
3334 (line-end-position))))
3335 (save-excursion
3336 ;; Insert a temporary formula at right after the table
3337 (goto-char (org-table-TBLFM-begin))
3338 (let ((s (point-marker)))
3339 (insert formula "\n")
3340 (let ((e (point-marker)))
3341 ;; Recalculate the table.
3342 (beginning-of-line 0) ; move to the inserted line
3343 (skip-chars-backward " \r\n\t")
3344 (unwind-protect
3345 (org-call-with-arg #'org-table-recalculate (or arg t))
3346 ;; Delete the formula inserted temporarily.
3347 (delete-region s e)
3348 (set-marker s nil)
3349 (set-marker e nil)))))))
3351 (defun org-table-TBLFM-begin ()
3352 "Find the beginning of the TBLFM lines and return its position.
3353 Return nil when the beginning of TBLFM line was not found."
3354 (save-excursion
3355 (when (progn (forward-line 1)
3356 (re-search-backward org-table-TBLFM-begin-regexp nil t))
3357 (line-beginning-position 2))))
3359 (defun org-table-expand-lhs-ranges (equations)
3360 "Expand list of formulas.
3361 If some of the RHS in the formulas are ranges or a row reference, expand
3362 them to individual field equations for each field."
3363 (let (e res lhs rhs range r1 r2 c1 c2)
3364 (while (setq e (pop equations))
3365 (setq lhs (car e) rhs (cdr e))
3366 (cond
3367 ((string-match "^@-?[-+0-9]+\\$-?[0-9]+$" lhs)
3368 ;; This just refers to one fixed field
3369 (push e res))
3370 ((string-match "^[a-zA-Z][_a-zA-Z0-9]*$" lhs)
3371 ;; This just refers to one fixed named field
3372 (push e res))
3373 ((string-match "^@[0-9]+$" lhs)
3374 (loop for ic from 1 to org-table-current-ncol do
3375 (push (cons (format "%s$%d" lhs ic) rhs) res)
3376 (put-text-property 0 (length (caar res))
3377 :orig-eqn e (caar res))))
3379 (setq range (org-table-get-range lhs org-table-current-begin-pos
3380 1 nil 'corners))
3381 (setq r1 (nth 0 range) c1 (nth 1 range)
3382 r2 (nth 2 range) c2 (nth 3 range))
3383 (setq r1 (org-table-line-to-dline r1))
3384 (setq r2 (org-table-line-to-dline r2 'above))
3385 (loop for ir from r1 to r2 do
3386 (loop for ic from c1 to c2 do
3387 (push (cons (format "@%d$%d" ir ic) rhs) res)
3388 (put-text-property 0 (length (caar res))
3389 :orig-eqn e (caar res)))))))
3390 (nreverse res)))
3392 (defun org-table-formula-handle-first/last-rc (s)
3393 "Replace @<, @>, $<, $> with first/last row/column of the table.
3394 So @< and $< will always be replaced with @1 and $1, respectively.
3395 The advantage of these special markers are that structure editing of
3396 the table will not change them, while @1 and $1 will be modified
3397 when a line/row is swapped out of that privileged position. So for
3398 formulas that use a range of rows or columns, it may often be better
3399 to anchor the formula with \"I\" row markers, or to offset from the
3400 borders of the table using the @< @> $< $> makers."
3401 (let (n nmax len char (start 0))
3402 (while (string-match "\\([@$]\\)\\(<+\\|>+\\)\\|\\(remote([^\)]+)\\)"
3403 s start)
3404 (if (match-end 3)
3405 (setq start (match-end 3))
3406 (setq nmax (if (equal (match-string 1 s) "@")
3407 (1- (length org-table-dlines))
3408 org-table-current-ncol)
3409 len (- (match-end 2) (match-beginning 2))
3410 char (string-to-char (match-string 2 s))
3411 n (if (= char ?<)
3413 (- nmax len -1)))
3414 (if (or (< n 1) (> n nmax))
3415 (user-error "Reference \"%s\" in expression \"%s\" points outside table"
3416 (match-string 0 s) s))
3417 (setq start (match-beginning 0))
3418 (setq s (replace-match (format "%s%d" (match-string 1 s) n) t t s)))))
3421 (defun org-table-formula-substitute-names (f)
3422 "Replace $const with values in string F."
3423 (let ((start 0)
3424 (pp (/= (string-to-char f) ?'))
3425 (duration (org-string-match-p ";.*[Tt].*\\'" f))
3426 (new (replace-regexp-in-string ; Check for column names.
3427 org-table-column-name-regexp
3428 (lambda (m)
3429 (concat "$" (cdr (assoc (match-string 1 m)
3430 org-table-column-names))))
3431 f t t)))
3432 ;; Parameters and constants.
3433 (while (setq start
3434 (string-match
3435 "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)\\|\\(\\<remote([^)]*)\\)"
3436 new start))
3437 (if (match-end 2) (setq start (match-end 2))
3438 (incf start)
3439 ;; When a duration is expected, convert value on the fly.
3440 (let ((value
3441 (save-match-data
3442 (let ((v (org-table-get-constant (match-string 1 new))))
3443 (if (and (org-string-nw-p v) duration)
3444 (org-table-time-string-to-seconds v)
3445 v)))))
3446 (when value
3447 (setq new (replace-match
3448 (concat (and pp "(") value (and pp ")")) t t new))))))
3449 (if org-table-formula-debug (org-propertize new :orig-formula f)) new))
3451 (defun org-table-get-constant (const)
3452 "Find the value for a parameter or constant in a formula.
3453 Parameters get priority."
3454 (or (cdr (assoc const org-table-local-parameters))
3455 (cdr (assoc const org-table-formula-constants-local))
3456 (cdr (assoc const org-table-formula-constants))
3457 (and (fboundp 'constants-get) (constants-get const))
3458 (and (string= (substring const 0 (min 5 (length const))) "PROP_")
3459 (org-entry-get nil (substring const 5) 'inherit))
3460 "#UNDEFINED_NAME"))
3462 (defvar org-table-fedit-map
3463 (let ((map (make-sparse-keymap)))
3464 (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
3465 (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
3466 (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
3467 (org-defkey map "\C-c'" 'org-table-fedit-finish)
3468 (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
3469 (org-defkey map "\C-c?" 'org-table-show-reference)
3470 (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
3471 (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
3472 (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
3473 (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
3474 (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
3475 (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
3476 (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
3477 (org-defkey map [(meta down)] 'org-table-fedit-scroll)
3478 (org-defkey map [(meta tab)] 'lisp-complete-symbol)
3479 (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
3480 (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
3481 (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
3482 (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
3483 (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
3484 map))
3486 (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
3487 '("Edit-Formulas"
3488 ["Finish and Install" org-table-fedit-finish t]
3489 ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
3490 ["Abort" org-table-fedit-abort t]
3491 "--"
3492 ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
3493 ["Complete Lisp Symbol" lisp-complete-symbol t]
3494 "--"
3495 "Shift Reference at Point"
3496 ["Up" org-table-fedit-ref-up t]
3497 ["Down" org-table-fedit-ref-down t]
3498 ["Left" org-table-fedit-ref-left t]
3499 ["Right" org-table-fedit-ref-right t]
3501 "Change Test Row for Column Formulas"
3502 ["Up" org-table-fedit-line-up t]
3503 ["Down" org-table-fedit-line-down t]
3504 "--"
3505 ["Scroll Table Window" org-table-fedit-scroll t]
3506 ["Scroll Table Window down" org-table-fedit-scroll-down t]
3507 ["Show Table Grid" org-table-fedit-toggle-coordinates
3508 :style toggle :selected (with-current-buffer (marker-buffer org-pos)
3509 org-table-overlay-coordinates)]
3510 "--"
3511 ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
3512 :style toggle :selected org-table-buffer-is-an]))
3514 (defvar org-pos)
3516 ;;;###autoload
3517 (defun org-table-edit-formulas ()
3518 "Edit the formulas of the current table in a separate buffer."
3519 (interactive)
3520 (when (save-excursion (beginning-of-line 1) (let ((case-fold-search t)) (looking-at "[ \t]*#\\+TBLFM")))
3521 (beginning-of-line 0))
3522 (unless (org-at-table-p) (user-error "Not at a table"))
3523 (org-table-get-specials)
3524 (let ((key (org-table-current-field-formula 'key 'noerror))
3525 (eql (sort (org-table-get-stored-formulas 'noerror)
3526 'org-table-formula-less-p))
3527 (pos (point-marker))
3528 (startline 1)
3529 (wc (current-window-configuration))
3530 (sel-win (selected-window))
3531 (titles '((column . "# Column Formulas\n")
3532 (field . "# Field and Range Formulas\n")
3533 (named . "# Named Field Formulas\n")))
3534 entry s type title)
3535 (org-switch-to-buffer-other-window "*Edit Formulas*")
3536 (erase-buffer)
3537 ;; Keep global-font-lock-mode from turning on font-lock-mode
3538 (let ((font-lock-global-modes '(not fundamental-mode)))
3539 (fundamental-mode))
3540 (org-set-local 'font-lock-global-modes (list 'not major-mode))
3541 (org-set-local 'org-pos pos)
3542 (org-set-local 'org-window-configuration wc)
3543 (org-set-local 'org-selected-window sel-win)
3544 (use-local-map org-table-fedit-map)
3545 (org-add-hook 'post-command-hook 'org-table-fedit-post-command t t)
3546 (easy-menu-add org-table-fedit-menu)
3547 (setq startline (org-current-line))
3548 (while (setq entry (pop eql))
3549 (setq type (cond
3550 ((string-match "\\`$[<>]" (car entry)) 'column)
3551 ((equal (string-to-char (car entry)) ?@) 'field)
3552 ((string-match "^[0-9]" (car entry)) 'column)
3553 (t 'named)))
3554 (when (setq title (assq type titles))
3555 (or (bobp) (insert "\n"))
3556 (insert (org-add-props (cdr title) nil 'face font-lock-comment-face))
3557 (setq titles (remove title titles)))
3558 (if (equal key (car entry)) (setq startline (org-current-line)))
3559 (setq s (concat (if (member (string-to-char (car entry)) '(?@ ?$)) "" "$")
3560 (car entry) " = " (cdr entry) "\n"))
3561 (remove-text-properties 0 (length s) '(face nil) s)
3562 (insert s))
3563 (if (eq org-table-use-standard-references t)
3564 (org-table-fedit-toggle-ref-type))
3565 (org-goto-line startline)
3566 (message "Edit formulas, finish with `C-c C-c' or `C-c ' '. See menu for more commands.")))
3568 (defun org-table-fedit-post-command ()
3569 (when (not (memq this-command '(lisp-complete-symbol)))
3570 (let ((win (selected-window)))
3571 (save-excursion
3572 (condition-case nil
3573 (org-table-show-reference)
3574 (error nil))
3575 (select-window win)))))
3577 (defun org-table-formula-to-user (s)
3578 "Convert a formula from internal to user representation."
3579 (if (eq org-table-use-standard-references t)
3580 (org-table-convert-refs-to-an s)
3583 (defun org-table-formula-from-user (s)
3584 "Convert a formula from user to internal representation."
3585 (if org-table-use-standard-references
3586 (org-table-convert-refs-to-rc s)
3589 (defun org-table-convert-refs-to-rc (s)
3590 "Convert spreadsheet references from A7 to @7$28.
3591 Works for single references, but also for entire formulas and even the
3592 full TBLFM line."
3593 (let ((start 0))
3594 (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\|\\<remote([^,)]*)\\)" s start)
3595 (cond
3596 ((match-end 3)
3597 ;; format match, just advance
3598 (setq start (match-end 0)))
3599 ((and (> (match-beginning 0) 0)
3600 (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
3601 (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
3602 ;; 3.e5 or something like this.
3603 (setq start (match-end 0)))
3604 ((or (> (- (match-end 1) (match-beginning 1)) 2)
3605 ;; (member (match-string 1 s)
3606 ;; '("arctan" "exp" "expm" "lnp" "log" "stir"))
3608 ;; function name, just advance
3609 (setq start (match-end 0)))
3611 (setq start (match-beginning 0)
3612 s (replace-match
3613 (if (equal (match-string 2 s) "&")
3614 (format "$%d" (org-letters-to-number (match-string 1 s)))
3615 (format "@%d$%d"
3616 (string-to-number (match-string 2 s))
3617 (org-letters-to-number (match-string 1 s))))
3618 t t s)))))
3621 (defun org-table-convert-refs-to-an (s)
3622 "Convert spreadsheet references from to @7$28 to AB7.
3623 Works for single references, but also for entire formulas and even the
3624 full TBLFM line."
3625 (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
3626 (setq s (replace-match
3627 (format "%s%d"
3628 (org-number-to-letters
3629 (string-to-number (match-string 2 s)))
3630 (string-to-number (match-string 1 s)))
3631 t t s)))
3632 (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
3633 (setq s (replace-match (concat "\\1"
3634 (org-number-to-letters
3635 (string-to-number (match-string 2 s))) "&")
3636 t nil s)))
3639 (defun org-letters-to-number (s)
3640 "Convert a base 26 number represented by letters into an integer.
3641 For example: AB -> 28."
3642 (let ((n 0))
3643 (setq s (upcase s))
3644 (while (> (length s) 0)
3645 (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
3646 s (substring s 1)))
3649 (defun org-number-to-letters (n)
3650 "Convert an integer into a base 26 number represented by letters.
3651 For example: 28 -> AB."
3652 (let ((s ""))
3653 (while (> n 0)
3654 (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
3655 n (/ (1- n) 26)))
3658 (defun org-table-time-string-to-seconds (s)
3659 "Convert a time string into numerical duration in seconds.
3660 S can be a string matching either -?HH:MM:SS or -?HH:MM.
3661 If S is a string representing a number, keep this number."
3662 (if (equal s "")
3664 (let (hour minus min sec res)
3665 (cond
3666 ((and (string-match "\\(-?\\)\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)" s))
3667 (setq minus (< 0 (length (match-string 1 s)))
3668 hour (string-to-number (match-string 2 s))
3669 min (string-to-number (match-string 3 s))
3670 sec (string-to-number (match-string 4 s)))
3671 (if minus
3672 (setq res (- (+ (* hour 3600) (* min 60) sec)))
3673 (setq res (+ (* hour 3600) (* min 60) sec))))
3674 ((and (not (string-match org-ts-regexp-both s))
3675 (string-match "\\(-?\\)\\([0-9]+\\):\\([0-9]+\\)" s))
3676 (setq minus (< 0 (length (match-string 1 s)))
3677 hour (string-to-number (match-string 2 s))
3678 min (string-to-number (match-string 3 s)))
3679 (if minus
3680 (setq res (- (+ (* hour 3600) (* min 60))))
3681 (setq res (+ (* hour 3600) (* min 60)))))
3682 (t (setq res (string-to-number s))))
3683 (number-to-string res))))
3685 (defun org-table-time-seconds-to-string (secs &optional output-format)
3686 "Convert a number of seconds to a time string.
3687 If OUTPUT-FORMAT is non-nil, return a number of days, hours,
3688 minutes or seconds."
3689 (let* ((secs0 (abs secs))
3690 (res
3691 (cond ((eq output-format 'days)
3692 (format "%.3f" (/ (float secs0) 86400)))
3693 ((eq output-format 'hours)
3694 (format "%.2f" (/ (float secs0) 3600)))
3695 ((eq output-format 'minutes)
3696 (format "%.1f" (/ (float secs0) 60)))
3697 ((eq output-format 'seconds)
3698 (format "%d" secs0))
3699 (t (org-format-seconds "%.2h:%.2m:%.2s" secs0)))))
3700 (if (< secs 0) (concat "-" res) res)))
3702 (defun org-table-fedit-convert-buffer (function)
3703 "Convert all references in this buffer, using FUNCTION."
3704 (let ((line (org-current-line)))
3705 (goto-char (point-min))
3706 (while (not (eobp))
3707 (insert (funcall function (buffer-substring (point) (point-at-eol))))
3708 (delete-region (point) (point-at-eol))
3709 (or (eobp) (forward-char 1)))
3710 (org-goto-line line)))
3712 (defun org-table-fedit-toggle-ref-type ()
3713 "Convert all references in the buffer from B3 to @3$2 and back."
3714 (interactive)
3715 (org-set-local 'org-table-buffer-is-an (not org-table-buffer-is-an))
3716 (org-table-fedit-convert-buffer
3717 (if org-table-buffer-is-an
3718 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
3719 (message "Reference type switched to %s"
3720 (if org-table-buffer-is-an "A1 etc" "@row$column")))
3722 (defun org-table-fedit-ref-up ()
3723 "Shift the reference at point one row/hline up."
3724 (interactive)
3725 (org-table-fedit-shift-reference 'up))
3726 (defun org-table-fedit-ref-down ()
3727 "Shift the reference at point one row/hline down."
3728 (interactive)
3729 (org-table-fedit-shift-reference 'down))
3730 (defun org-table-fedit-ref-left ()
3731 "Shift the reference at point one field to the left."
3732 (interactive)
3733 (org-table-fedit-shift-reference 'left))
3734 (defun org-table-fedit-ref-right ()
3735 "Shift the reference at point one field to the right."
3736 (interactive)
3737 (org-table-fedit-shift-reference 'right))
3739 (defun org-table-fedit-shift-reference (dir)
3740 (cond
3741 ((org-in-regexp "\\(\\<[a-zA-Z]\\)&")
3742 (if (memq dir '(left right))
3743 (org-rematch-and-replace 1 (eq dir 'left))
3744 (user-error "Cannot shift reference in this direction")))
3745 ((org-in-regexp "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
3746 ;; A B3-like reference
3747 (if (memq dir '(up down))
3748 (org-rematch-and-replace 2 (eq dir 'up))
3749 (org-rematch-and-replace 1 (eq dir 'left))))
3750 ((org-in-regexp
3751 "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
3752 ;; An internal reference
3753 (if (memq dir '(up down))
3754 (org-rematch-and-replace 2 (eq dir 'up) (match-end 3))
3755 (org-rematch-and-replace 5 (eq dir 'left))))))
3757 (defun org-rematch-and-replace (n &optional decr hline)
3758 "Re-match the group N, and replace it with the shifted reference."
3759 (or (match-end n) (user-error "Cannot shift reference in this direction"))
3760 (goto-char (match-beginning n))
3761 (and (looking-at (regexp-quote (match-string n)))
3762 (replace-match (org-table-shift-refpart (match-string 0) decr hline)
3763 t t)))
3765 (defun org-table-shift-refpart (ref &optional decr hline)
3766 "Shift a reference part REF.
3767 If DECR is set, decrease the references row/column, else increase.
3768 If HLINE is set, this may be a hline reference, it certainly is not
3769 a translation reference."
3770 (save-match-data
3771 (let* ((sign (string-match "^[-+]" ref)) n)
3773 (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
3774 (cond
3775 ((and hline (string-match "^I+" ref))
3776 (setq n (string-to-number (concat sign (number-to-string (length ref)))))
3777 (setq n (+ n (if decr -1 1)))
3778 (if (= n 0) (setq n (+ n (if decr -1 1))))
3779 (if sign
3780 (setq sign (if (< n 0) "-" "+") n (abs n))
3781 (setq n (max 1 n)))
3782 (concat sign (make-string n ?I)))
3784 ((string-match "^[0-9]+" ref)
3785 (setq n (string-to-number (concat sign ref)))
3786 (setq n (+ n (if decr -1 1)))
3787 (if sign
3788 (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
3789 (number-to-string (max 1 n))))
3791 ((string-match "^[a-zA-Z]+" ref)
3792 (org-number-to-letters
3793 (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
3795 (t (user-error "Cannot shift reference"))))))
3797 (defun org-table-fedit-toggle-coordinates ()
3798 "Toggle the display of coordinates in the referenced table."
3799 (interactive)
3800 (let ((pos (marker-position org-pos)))
3801 (with-current-buffer (marker-buffer org-pos)
3802 (save-excursion
3803 (goto-char pos)
3804 (org-table-toggle-coordinate-overlays)))))
3806 (defun org-table-fedit-finish (&optional arg)
3807 "Parse the buffer for formula definitions and install them.
3808 With prefix ARG, apply the new formulas to the table."
3809 (interactive "P")
3810 (org-table-remove-rectangle-highlight)
3811 (if org-table-use-standard-references
3812 (progn
3813 (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
3814 (setq org-table-buffer-is-an nil)))
3815 (let ((pos org-pos) (sel-win org-selected-window) eql var form)
3816 (goto-char (point-min))
3817 (while (re-search-forward
3818 "^\\(@[-+I<>0-9.$@]+\\|@?[0-9]+\\|\\$\\([a-zA-Z0-9]+\\|[<>]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
3819 nil t)
3820 (setq var (if (match-end 2) (match-string 2) (match-string 1))
3821 form (match-string 3))
3822 (setq form (org-trim form))
3823 (when (not (equal form ""))
3824 (while (string-match "[ \t]*\n[ \t]*" form)
3825 (setq form (replace-match " " t t form)))
3826 (when (assoc var eql)
3827 (user-error "Double formulas for %s" var))
3828 (push (cons var form) eql)))
3829 (setq org-pos nil)
3830 (set-window-configuration org-window-configuration)
3831 (select-window sel-win)
3832 (goto-char pos)
3833 (unless (org-at-table-p)
3834 (user-error "Lost table position - cannot install formulas"))
3835 (org-table-store-formulas eql)
3836 (move-marker pos nil)
3837 (kill-buffer "*Edit Formulas*")
3838 (if arg
3839 (org-table-recalculate 'all)
3840 (message "New formulas installed - press C-u C-c C-c to apply."))))
3842 (defun org-table-fedit-abort ()
3843 "Abort editing formulas, without installing the changes."
3844 (interactive)
3845 (org-table-remove-rectangle-highlight)
3846 (let ((pos org-pos) (sel-win org-selected-window))
3847 (set-window-configuration org-window-configuration)
3848 (select-window sel-win)
3849 (goto-char pos)
3850 (move-marker pos nil)
3851 (message "Formula editing aborted without installing changes")))
3853 (defun org-table-fedit-lisp-indent ()
3854 "Pretty-print and re-indent Lisp expressions in the Formula Editor."
3855 (interactive)
3856 (let ((pos (point)) beg end ind)
3857 (beginning-of-line 1)
3858 (cond
3859 ((looking-at "[ \t]")
3860 (goto-char pos)
3861 (call-interactively 'lisp-indent-line))
3862 ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
3863 ((not (fboundp 'pp-buffer))
3864 (user-error "Cannot pretty-print. Command `pp-buffer' is not available"))
3865 ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
3866 (goto-char (- (match-end 0) 2))
3867 (setq beg (point))
3868 (setq ind (make-string (current-column) ?\ ))
3869 (condition-case nil (forward-sexp 1)
3870 (error
3871 (user-error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
3872 (setq end (point))
3873 (save-restriction
3874 (narrow-to-region beg end)
3875 (if (eq last-command this-command)
3876 (progn
3877 (goto-char (point-min))
3878 (setq this-command nil)
3879 (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
3880 (replace-match " ")))
3881 (pp-buffer)
3882 (untabify (point-min) (point-max))
3883 (goto-char (1+ (point-min)))
3884 (while (re-search-forward "^." nil t)
3885 (beginning-of-line 1)
3886 (insert ind))
3887 (goto-char (point-max))
3888 (org-delete-backward-char 1)))
3889 (goto-char beg))
3890 (t nil))))
3892 (defvar org-show-positions nil)
3894 (defun org-table-show-reference (&optional local)
3895 "Show the location/value of the $ expression at point."
3896 (interactive)
3897 (org-table-remove-rectangle-highlight)
3898 (catch 'exit
3899 (let ((pos (if local (point) org-pos))
3900 (face2 'highlight)
3901 (org-inhibit-highlight-removal t)
3902 (win (selected-window))
3903 (org-show-positions nil)
3904 var name e what match dest)
3905 (if local (org-table-get-specials))
3906 (setq what (cond
3907 ((org-in-regexp "^@[0-9]+[ \t=]")
3908 (setq match (concat (substring (match-string 0) 0 -1)
3909 "$1.."
3910 (substring (match-string 0) 0 -1)
3911 "$100"))
3912 'range)
3913 ((or (org-in-regexp org-table-range-regexp2)
3914 (org-in-regexp org-table-translate-regexp)
3915 (org-in-regexp org-table-range-regexp))
3916 (setq match
3917 (save-match-data
3918 (org-table-convert-refs-to-rc (match-string 0))))
3919 'range)
3920 ((org-in-regexp "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
3921 ((org-in-regexp "\\$[0-9]+") 'column)
3922 ((not local) nil)
3923 (t (user-error "No reference at point")))
3924 match (and what (or match (match-string 0))))
3925 (when (and match (not (equal (match-beginning 0) (point-at-bol))))
3926 (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
3927 'secondary-selection))
3928 (org-add-hook 'before-change-functions
3929 'org-table-remove-rectangle-highlight)
3930 (if (eq what 'name) (setq var (substring match 1)))
3931 (when (eq what 'range)
3932 (or (equal (string-to-char match) ?@) (setq match (concat "@" match)))
3933 (setq match (org-table-formula-substitute-names match)))
3934 (unless local
3935 (save-excursion
3936 (end-of-line 1)
3937 (re-search-backward "^\\S-" nil t)
3938 (beginning-of-line 1)
3939 (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\\([0-9]+\\|&\\)\\) *=")
3940 (setq dest
3941 (save-match-data
3942 (org-table-convert-refs-to-rc (match-string 1))))
3943 (org-table-add-rectangle-overlay
3944 (match-beginning 1) (match-end 1) face2))))
3945 (if (and (markerp pos) (marker-buffer pos))
3946 (if (get-buffer-window (marker-buffer pos))
3947 (select-window (get-buffer-window (marker-buffer pos)))
3948 (org-switch-to-buffer-other-window (get-buffer-window
3949 (marker-buffer pos)))))
3950 (goto-char pos)
3951 (org-table-force-dataline)
3952 (when dest
3953 (setq name (substring dest 1))
3954 (cond
3955 ((string-match "^\\$[a-zA-Z][a-zA-Z0-9]*" dest)
3956 (setq e (assoc name org-table-named-field-locations))
3957 (org-goto-line (nth 1 e))
3958 (org-table-goto-column (nth 2 e)))
3959 ((string-match "^@\\([0-9]+\\)\\$\\([0-9]+\\)" dest)
3960 (let ((l (string-to-number (match-string 1 dest)))
3961 (c (string-to-number (match-string 2 dest))))
3962 (org-goto-line (aref org-table-dlines l))
3963 (org-table-goto-column c)))
3964 (t (org-table-goto-column (string-to-number name))))
3965 (move-marker pos (point))
3966 (org-table-highlight-rectangle nil nil face2))
3967 (cond
3968 ((equal dest match))
3969 ((not match))
3970 ((eq what 'range)
3971 (condition-case nil
3972 (save-excursion
3973 (org-table-get-range match nil nil 'highlight))
3974 (error nil)))
3975 ((setq e (assoc var org-table-named-field-locations))
3976 (org-goto-line (nth 1 e))
3977 (org-table-goto-column (nth 2 e))
3978 (org-table-highlight-rectangle (point) (point))
3979 (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
3980 ((setq e (assoc var org-table-column-names))
3981 (org-table-goto-column (string-to-number (cdr e)))
3982 (org-table-highlight-rectangle (point) (point))
3983 (goto-char (org-table-begin))
3984 (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
3985 (org-table-end) t)
3986 (progn
3987 (goto-char (match-beginning 1))
3988 (org-table-highlight-rectangle)
3989 (message "Named column (column %s)" (cdr e)))
3990 (user-error "Column name not found")))
3991 ((eq what 'column)
3992 ;; column number
3993 (org-table-goto-column (string-to-number (substring match 1)))
3994 (org-table-highlight-rectangle (point) (point))
3995 (message "Column %s" (substring match 1)))
3996 ((setq e (assoc var org-table-local-parameters))
3997 (goto-char (org-table-begin))
3998 (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
3999 (progn
4000 (goto-char (match-beginning 1))
4001 (org-table-highlight-rectangle)
4002 (message "Local parameter."))
4003 (user-error "Parameter not found")))
4005 (cond
4006 ((not var) (user-error "No reference at point"))
4007 ((setq e (assoc var org-table-formula-constants-local))
4008 (message "Local Constant: $%s=%s in #+CONSTANTS line."
4009 var (cdr e)))
4010 ((setq e (assoc var org-table-formula-constants))
4011 (message "Constant: $%s=%s in `org-table-formula-constants'."
4012 var (cdr e)))
4013 ((setq e (and (fboundp 'constants-get) (constants-get var)))
4014 (message "Constant: $%s=%s, from `constants.el'%s."
4015 var e (format " (%s units)" constants-unit-system)))
4016 (t (user-error "Undefined name $%s" var)))))
4017 (goto-char pos)
4018 (when (and org-show-positions
4019 (not (memq this-command '(org-table-fedit-scroll
4020 org-table-fedit-scroll-down))))
4021 (push pos org-show-positions)
4022 (push org-table-current-begin-pos org-show-positions)
4023 (let ((min (apply 'min org-show-positions))
4024 (max (apply 'max org-show-positions)))
4025 (set-window-start (selected-window) min)
4026 (goto-char max)
4027 (or (pos-visible-in-window-p max)
4028 (set-window-start (selected-window) max))))
4029 (select-window win))))
4031 (defun org-table-force-dataline ()
4032 "Make sure the cursor is in a dataline in a table."
4033 (unless (save-excursion
4034 (beginning-of-line 1)
4035 (looking-at org-table-dataline-regexp))
4036 (let* ((re org-table-dataline-regexp)
4037 (p1 (save-excursion (re-search-forward re nil 'move)))
4038 (p2 (save-excursion (re-search-backward re nil 'move))))
4039 (cond ((and p1 p2)
4040 (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
4041 p1 p2)))
4042 ((or p1 p2) (goto-char (or p1 p2)))
4043 (t (user-error "No table dataline around here"))))))
4045 (defun org-table-fedit-line-up ()
4046 "Move cursor one line up in the window showing the table."
4047 (interactive)
4048 (org-table-fedit-move 'previous-line))
4050 (defun org-table-fedit-line-down ()
4051 "Move cursor one line down in the window showing the table."
4052 (interactive)
4053 (org-table-fedit-move 'next-line))
4055 (defun org-table-fedit-move (command)
4056 "Move the cursor in the window showing the table.
4057 Use COMMAND to do the motion, repeat if necessary to end up in a data line."
4058 (let ((org-table-allow-automatic-line-recalculation nil)
4059 (pos org-pos) (win (selected-window)) p)
4060 (select-window (get-buffer-window (marker-buffer org-pos)))
4061 (setq p (point))
4062 (call-interactively command)
4063 (while (and (org-at-table-p)
4064 (org-at-table-hline-p))
4065 (call-interactively command))
4066 (or (org-at-table-p) (goto-char p))
4067 (move-marker pos (point))
4068 (select-window win)))
4070 (defun org-table-fedit-scroll (N)
4071 (interactive "p")
4072 (let ((other-window-scroll-buffer (marker-buffer org-pos)))
4073 (scroll-other-window N)))
4075 (defun org-table-fedit-scroll-down (N)
4076 (interactive "p")
4077 (org-table-fedit-scroll (- N)))
4079 (defvar org-table-rectangle-overlays nil)
4081 (defun org-table-add-rectangle-overlay (beg end &optional face)
4082 "Add a new overlay."
4083 (let ((ov (make-overlay beg end)))
4084 (overlay-put ov 'face (or face 'secondary-selection))
4085 (push ov org-table-rectangle-overlays)))
4087 (defun org-table-highlight-rectangle (&optional beg end face)
4088 "Highlight rectangular region in a table."
4089 (setq beg (or beg (point)) end (or end (point)))
4090 (let ((b (min beg end))
4091 (e (max beg end))
4092 l1 c1 l2 c2 tmp)
4093 (and (boundp 'org-show-positions)
4094 (setq org-show-positions (cons b (cons e org-show-positions))))
4095 (goto-char (min beg end))
4096 (setq l1 (org-current-line)
4097 c1 (org-table-current-column))
4098 (goto-char (max beg end))
4099 (setq l2 (org-current-line)
4100 c2 (org-table-current-column))
4101 (if (> c1 c2) (setq tmp c1 c1 c2 c2 tmp))
4102 (org-goto-line l1)
4103 (beginning-of-line 1)
4104 (loop for line from l1 to l2 do
4105 (when (looking-at org-table-dataline-regexp)
4106 (org-table-goto-column c1)
4107 (skip-chars-backward "^|\n") (setq beg (point))
4108 (org-table-goto-column c2)
4109 (skip-chars-forward "^|\n") (setq end (point))
4110 (org-table-add-rectangle-overlay beg end face))
4111 (beginning-of-line 2))
4112 (goto-char b))
4113 (add-hook 'before-change-functions 'org-table-remove-rectangle-highlight))
4115 (defun org-table-remove-rectangle-highlight (&rest ignore)
4116 "Remove the rectangle overlays."
4117 (unless org-inhibit-highlight-removal
4118 (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
4119 (mapc 'delete-overlay org-table-rectangle-overlays)
4120 (setq org-table-rectangle-overlays nil)))
4122 (defvar org-table-coordinate-overlays nil
4123 "Collects the coordinate grid overlays, so that they can be removed.")
4124 (make-variable-buffer-local 'org-table-coordinate-overlays)
4126 (defun org-table-overlay-coordinates ()
4127 "Add overlays to the table at point, to show row/column coordinates."
4128 (interactive)
4129 (mapc 'delete-overlay org-table-coordinate-overlays)
4130 (setq org-table-coordinate-overlays nil)
4131 (save-excursion
4132 (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg)
4133 (goto-char (org-table-begin))
4134 (while (org-at-table-p)
4135 (setq eol (point-at-eol))
4136 (setq ov (make-overlay (point-at-bol) (1+ (point-at-bol))))
4137 (push ov org-table-coordinate-overlays)
4138 (setq hline (looking-at org-table-hline-regexp))
4139 (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
4140 (format "%4d" (setq id (1+ id)))))
4141 (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
4142 (when hline
4143 (setq ic 0)
4144 (while (re-search-forward "[+|]\\(-+\\)" eol t)
4145 (setq beg (1+ (match-beginning 0))
4146 ic (1+ ic)
4147 s1 (concat "$" (int-to-string ic))
4148 s2 (org-number-to-letters ic)
4149 str (if (eq org-table-use-standard-references t) s2 s1))
4150 (setq ov (make-overlay beg (+ beg (length str))))
4151 (push ov org-table-coordinate-overlays)
4152 (org-overlay-display ov str 'org-special-keyword 'evaporate)))
4153 (beginning-of-line 2)))))
4155 ;;;###autoload
4156 (defun org-table-toggle-coordinate-overlays ()
4157 "Toggle the display of Row/Column numbers in tables."
4158 (interactive)
4159 (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
4160 (message "Tables Row/Column numbers display turned %s"
4161 (if org-table-overlay-coordinates "on" "off"))
4162 (if (and (org-at-table-p) org-table-overlay-coordinates)
4163 (org-table-align))
4164 (unless org-table-overlay-coordinates
4165 (mapc 'delete-overlay org-table-coordinate-overlays)
4166 (setq org-table-coordinate-overlays nil)))
4168 ;;;###autoload
4169 (defun org-table-toggle-formula-debugger ()
4170 "Toggle the formula debugger in tables."
4171 (interactive)
4172 (setq org-table-formula-debug (not org-table-formula-debug))
4173 (message "Formula debugging has been turned %s"
4174 (if org-table-formula-debug "on" "off")))
4176 ;;; The orgtbl minor mode
4178 ;; Define a minor mode which can be used in other modes in order to
4179 ;; integrate the org-mode table editor.
4181 ;; This is really a hack, because the org-mode table editor uses several
4182 ;; keys which normally belong to the major mode, for example the TAB and
4183 ;; RET keys. Here is how it works: The minor mode defines all the keys
4184 ;; necessary to operate the table editor, but wraps the commands into a
4185 ;; function which tests if the cursor is currently inside a table. If that
4186 ;; is the case, the table editor command is executed. However, when any of
4187 ;; those keys is used outside a table, the function uses `key-binding' to
4188 ;; look up if the key has an associated command in another currently active
4189 ;; keymap (minor modes, major mode, global), and executes that command.
4190 ;; There might be problems if any of the keys used by the table editor is
4191 ;; otherwise used as a prefix key.
4193 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
4194 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
4195 ;; addresses this by checking explicitly for both bindings.
4197 ;; The optimized version (see variable `orgtbl-optimized') takes over
4198 ;; all keys which are bound to `self-insert-command' in the *global map*.
4199 ;; Some modes bind other commands to simple characters, for example
4200 ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
4201 ;; active, this binding is ignored inside tables and replaced with a
4202 ;; modified self-insert.
4205 (defvar orgtbl-mode-map (make-keymap)
4206 "Keymap for `orgtbl-mode'.")
4208 (defvar org-old-auto-fill-inhibit-regexp nil
4209 "Local variable used by `orgtbl-mode'.")
4211 (defconst orgtbl-line-start-regexp
4212 "[ \t]*\\(|\\|#\\+\\(tblfm\\|orgtbl\\|tblname\\):\\)"
4213 "Matches a line belonging to an orgtbl.")
4215 (defconst orgtbl-extra-font-lock-keywords
4216 (list (list (concat "^" orgtbl-line-start-regexp ".*")
4217 0 (quote 'org-table) 'prepend))
4218 "Extra `font-lock-keywords' to be added when `orgtbl-mode' is active.")
4220 ;; Install it as a minor mode.
4221 (put 'orgtbl-mode :included t)
4222 (put 'orgtbl-mode :menu-tag "Org Table Mode")
4224 ;;;###autoload
4225 (define-minor-mode orgtbl-mode
4226 "The `org-mode' table editor as a minor mode for use in other modes."
4227 :lighter " OrgTbl" :keymap orgtbl-mode-map
4228 (org-load-modules-maybe)
4229 (cond
4230 ((derived-mode-p 'org-mode)
4231 ;; Exit without error, in case some hook functions calls this
4232 ;; by accident in org-mode.
4233 (message "Orgtbl-mode is not useful in org-mode, command ignored"))
4234 (orgtbl-mode
4235 (and (orgtbl-setup) (defun orgtbl-setup () nil)) ;; FIXME: Yuck!?!
4236 ;; Make sure we are first in minor-mode-map-alist
4237 (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
4238 ;; FIXME: maybe it should use emulation-mode-map-alists?
4239 (and c (setq minor-mode-map-alist
4240 (cons c (delq c minor-mode-map-alist)))))
4241 (org-set-local (quote org-table-may-need-update) t)
4242 (org-add-hook 'before-change-functions 'org-before-change-function
4243 nil 'local)
4244 (org-set-local 'org-old-auto-fill-inhibit-regexp
4245 auto-fill-inhibit-regexp)
4246 (org-set-local 'auto-fill-inhibit-regexp
4247 (if auto-fill-inhibit-regexp
4248 (concat orgtbl-line-start-regexp "\\|"
4249 auto-fill-inhibit-regexp)
4250 orgtbl-line-start-regexp))
4251 (add-to-invisibility-spec '(org-cwidth))
4252 (when (fboundp 'font-lock-add-keywords)
4253 (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
4254 (org-restart-font-lock))
4255 (easy-menu-add orgtbl-mode-menu))
4257 (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
4258 (org-table-cleanup-narrow-column-properties)
4259 (org-remove-from-invisibility-spec '(org-cwidth))
4260 (remove-hook 'before-change-functions 'org-before-change-function t)
4261 (when (fboundp 'font-lock-remove-keywords)
4262 (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
4263 (org-restart-font-lock))
4264 (easy-menu-remove orgtbl-mode-menu)
4265 (force-mode-line-update 'all))))
4267 (defun org-table-cleanup-narrow-column-properties ()
4268 "Remove all properties related to narrow-column invisibility."
4269 (let ((s (point-min)))
4270 (while (setq s (text-property-any s (point-max)
4271 'display org-narrow-column-arrow))
4272 (remove-text-properties s (1+ s) '(display t)))
4273 (setq s (point-min))
4274 (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
4275 (remove-text-properties s (1+ s) '(org-cwidth t)))
4276 (setq s (point-min))
4277 (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
4278 (remove-text-properties s (1+ s) '(invisible t)))))
4280 (defun orgtbl-make-binding (fun n &rest keys)
4281 "Create a function for binding in the table minor mode.
4282 FUN is the command to call inside a table. N is used to create a unique
4283 command name. KEYS are keys that should be checked in for a command
4284 to execute outside of tables."
4285 (eval
4286 (list 'defun
4287 (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
4288 '(arg)
4289 (concat "In tables, run `" (symbol-name fun) "'.\n"
4290 "Outside of tables, run the binding of `"
4291 (mapconcat #'key-description keys "' or `")
4292 "'.")
4293 '(interactive "p")
4294 (list 'if
4295 '(org-at-table-p)
4296 (list 'call-interactively (list 'quote fun))
4297 (list 'let '(orgtbl-mode)
4298 (list 'call-interactively
4299 (append '(or)
4300 (mapcar (lambda (k)
4301 (list 'key-binding k))
4302 keys)
4303 '('orgtbl-error))))))))
4305 (defun orgtbl-error ()
4306 "Error when there is no default binding for a table key."
4307 (interactive)
4308 (user-error "This key has no function outside tables"))
4310 (defun orgtbl-setup ()
4311 "Setup orgtbl keymaps."
4312 (let ((nfunc 0)
4313 (bindings
4314 '(([(meta shift left)] org-table-delete-column)
4315 ([(meta left)] org-table-move-column-left)
4316 ([(meta right)] org-table-move-column-right)
4317 ([(meta shift right)] org-table-insert-column)
4318 ([(meta shift up)] org-table-kill-row)
4319 ([(meta shift down)] org-table-insert-row)
4320 ([(meta up)] org-table-move-row-up)
4321 ([(meta down)] org-table-move-row-down)
4322 ("\C-c\C-w" org-table-cut-region)
4323 ("\C-c\M-w" org-table-copy-region)
4324 ("\C-c\C-y" org-table-paste-rectangle)
4325 ("\C-c\C-w" org-table-wrap-region)
4326 ("\C-c-" org-table-insert-hline)
4327 ("\C-c}" org-table-toggle-coordinate-overlays)
4328 ("\C-c{" org-table-toggle-formula-debugger)
4329 ("\C-m" org-table-next-row)
4330 ([(shift return)] org-table-copy-down)
4331 ("\C-c?" org-table-field-info)
4332 ("\C-c " org-table-blank-field)
4333 ("\C-c+" org-table-sum)
4334 ("\C-c=" org-table-eval-formula)
4335 ("\C-c'" org-table-edit-formulas)
4336 ("\C-c`" org-table-edit-field)
4337 ("\C-c*" org-table-recalculate)
4338 ("\C-c^" org-table-sort-lines)
4339 ("\M-a" org-table-beginning-of-field)
4340 ("\M-e" org-table-end-of-field)
4341 ([(control ?#)] org-table-rotate-recalc-marks)))
4342 elt key fun cmd)
4343 (while (setq elt (pop bindings))
4344 (setq nfunc (1+ nfunc))
4345 (setq key (org-key (car elt))
4346 fun (nth 1 elt)
4347 cmd (orgtbl-make-binding fun nfunc key))
4348 (org-defkey orgtbl-mode-map key cmd))
4350 ;; Special treatment needed for TAB and RET
4351 (org-defkey orgtbl-mode-map [(return)]
4352 (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
4353 (org-defkey orgtbl-mode-map "\C-m"
4354 (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
4356 (org-defkey orgtbl-mode-map [(tab)]
4357 (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
4358 (org-defkey orgtbl-mode-map "\C-i"
4359 (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
4361 (org-defkey orgtbl-mode-map [(shift tab)]
4362 (orgtbl-make-binding 'org-table-previous-field 104
4363 [(shift tab)] [(tab)] "\C-i"))
4366 (unless (featurep 'xemacs)
4367 (org-defkey orgtbl-mode-map [S-iso-lefttab]
4368 (orgtbl-make-binding 'org-table-previous-field 107
4369 [S-iso-lefttab] [backtab] [(shift tab)]
4370 [(tab)] "\C-i")))
4372 (org-defkey orgtbl-mode-map [backtab]
4373 (orgtbl-make-binding 'org-table-previous-field 108
4374 [backtab] [S-iso-lefttab] [(shift tab)]
4375 [(tab)] "\C-i"))
4377 (org-defkey orgtbl-mode-map "\M-\C-m"
4378 (orgtbl-make-binding 'org-table-wrap-region 105
4379 "\M-\C-m" [(meta return)]))
4380 (org-defkey orgtbl-mode-map [(meta return)]
4381 (orgtbl-make-binding 'org-table-wrap-region 106
4382 [(meta return)] "\M-\C-m"))
4384 (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
4385 (org-defkey orgtbl-mode-map "\C-c|" 'orgtbl-create-or-convert-from-region)
4387 (when orgtbl-optimized
4388 ;; If the user wants maximum table support, we need to hijack
4389 ;; some standard editing functions
4390 (org-remap orgtbl-mode-map
4391 'self-insert-command 'orgtbl-self-insert-command
4392 'delete-char 'org-delete-char
4393 'delete-backward-char 'org-delete-backward-char)
4394 (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
4395 (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
4396 '("OrgTbl"
4397 ["Create or convert" org-table-create-or-convert-from-region
4398 :active (not (org-at-table-p)) :keys "C-c |" ]
4399 "--"
4400 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
4401 ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
4402 ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
4403 ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
4404 "--"
4405 ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
4406 ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
4407 ["Copy Field from Above"
4408 org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
4409 "--"
4410 ("Column"
4411 ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
4412 ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
4413 ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
4414 ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
4415 ("Row"
4416 ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
4417 ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
4418 ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
4419 ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
4420 ["Sort lines in region" org-table-sort-lines :active (org-at-table-p) :keys "C-c ^"]
4421 "--"
4422 ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
4423 ("Rectangle"
4424 ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
4425 ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
4426 ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
4427 ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
4428 "--"
4429 ("Radio tables"
4430 ["Insert table template" orgtbl-insert-radio-table
4431 (assq major-mode orgtbl-radio-table-templates)]
4432 ["Comment/uncomment table" orgtbl-toggle-comment t])
4433 "--"
4434 ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
4435 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
4436 ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
4437 ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
4438 ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
4439 ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
4440 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
4441 ["Sum Column/Rectangle" org-table-sum
4442 :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
4443 ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
4444 ["Debug Formulas"
4445 org-table-toggle-formula-debugger :active (org-at-table-p)
4446 :keys "C-c {"
4447 :style toggle :selected org-table-formula-debug]
4448 ["Show Col/Row Numbers"
4449 org-table-toggle-coordinate-overlays :active (org-at-table-p)
4450 :keys "C-c }"
4451 :style toggle :selected org-table-overlay-coordinates]
4452 "--"
4453 ("Plot"
4454 ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
4455 ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
4458 (defun orgtbl-ctrl-c-ctrl-c (arg)
4459 "If the cursor is inside a table, realign the table.
4460 If it is a table to be sent away to a receiver, do it.
4461 With prefix arg, also recompute table."
4462 (interactive "P")
4463 (let ((case-fold-search t) (pos (point)) action)
4464 (save-excursion
4465 (beginning-of-line 1)
4466 (setq action (cond
4467 ((looking-at "[ \t]*#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
4468 ((looking-at "[ \t]*|") pos)
4469 ((looking-at "[ \t]*#\\+tblfm:") 'recalc))))
4470 (cond
4471 ((integerp action)
4472 (goto-char action)
4473 (org-table-maybe-eval-formula)
4474 (if arg
4475 (call-interactively 'org-table-recalculate)
4476 (org-table-maybe-recalculate-line))
4477 (call-interactively 'org-table-align)
4478 (when (orgtbl-send-table 'maybe)
4479 (run-hooks 'orgtbl-after-send-table-hook)))
4480 ((eq action 'recalc)
4481 (save-excursion
4482 (beginning-of-line 1)
4483 (skip-chars-backward " \r\n\t")
4484 (if (org-at-table-p)
4485 (org-call-with-arg 'org-table-recalculate t))))
4486 (t (let (orgtbl-mode)
4487 (call-interactively (key-binding "\C-c\C-c")))))))
4489 (defun orgtbl-create-or-convert-from-region (arg)
4490 "Create table or convert region to table, if no conflicting binding.
4491 This installs the table binding `C-c |', but only if there is no
4492 conflicting binding to this key outside orgtbl-mode."
4493 (interactive "P")
4494 (let* (orgtbl-mode (cmd (key-binding "\C-c|")))
4495 (if cmd
4496 (call-interactively cmd)
4497 (call-interactively 'org-table-create-or-convert-from-region))))
4499 (defun orgtbl-tab (arg)
4500 "Justification and field motion for `orgtbl-mode'."
4501 (interactive "P")
4502 (if arg (org-table-edit-field t)
4503 (org-table-justify-field-maybe)
4504 (org-table-next-field)))
4506 (defun orgtbl-ret ()
4507 "Justification and field motion for `orgtbl-mode'."
4508 (interactive)
4509 (if (bobp)
4510 (newline)
4511 (org-table-justify-field-maybe)
4512 (org-table-next-row)))
4514 (defun orgtbl-self-insert-command (N)
4515 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
4516 If the cursor is in a table looking at whitespace, the whitespace is
4517 overwritten, and the table is not marked as requiring realignment."
4518 (interactive "p")
4519 (if (and (org-at-table-p)
4521 (and org-table-auto-blank-field
4522 (member last-command
4523 '(orgtbl-hijacker-command-100
4524 orgtbl-hijacker-command-101
4525 orgtbl-hijacker-command-102
4526 orgtbl-hijacker-command-103
4527 orgtbl-hijacker-command-104
4528 orgtbl-hijacker-command-105
4529 yas/expand))
4530 (org-table-blank-field))
4532 (eq N 1)
4533 (looking-at "[^|\n]* +|"))
4534 (let (org-table-may-need-update)
4535 (goto-char (1- (match-end 0)))
4536 (org-delete-backward-char 1)
4537 (goto-char (match-beginning 0))
4538 (self-insert-command N))
4539 (setq org-table-may-need-update t)
4540 (let* (orgtbl-mode
4542 (cmd (or (key-binding
4543 (or (and (listp function-key-map)
4544 (setq a (assoc last-input-event function-key-map))
4545 (cdr a))
4546 (vector last-input-event)))
4547 'self-insert-command)))
4548 (call-interactively cmd)
4549 (if (and org-self-insert-cluster-for-undo
4550 (eq cmd 'self-insert-command))
4551 (if (not (eq last-command 'orgtbl-self-insert-command))
4552 (setq org-self-insert-command-undo-counter 1)
4553 (if (>= org-self-insert-command-undo-counter 20)
4554 (setq org-self-insert-command-undo-counter 1)
4555 (and (> org-self-insert-command-undo-counter 0)
4556 buffer-undo-list
4557 (not (cadr buffer-undo-list)) ; remove nil entry
4558 (setcdr buffer-undo-list (cddr buffer-undo-list)))
4559 (setq org-self-insert-command-undo-counter
4560 (1+ org-self-insert-command-undo-counter))))))))
4562 (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
4563 "Regular expression matching exponentials as produced by calc.")
4565 (defun orgtbl-gather-send-defs ()
4566 "Gather a plist of :name, :transform, :params for each destination before
4567 a radio table."
4568 (save-excursion
4569 (goto-char (org-table-begin))
4570 (let (rtn)
4571 (beginning-of-line 0)
4572 (while (looking-at "[ \t]*#\\+ORGTBL[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
4573 (let ((name (org-no-properties (match-string 1)))
4574 (transform (intern (match-string 2)))
4575 (params (if (match-end 3)
4576 (read (concat "(" (match-string 3) ")")))))
4577 (push (list :name name :transform transform :params params)
4578 rtn)
4579 (beginning-of-line 0)))
4580 rtn)))
4582 (defun orgtbl-send-replace-tbl (name txt)
4583 "Find and replace table NAME with TXT."
4584 (save-excursion
4585 (goto-char (point-min))
4586 (unless (re-search-forward
4587 (concat "BEGIN +RECEIVE +ORGTBL +" name "\\([ \t]\\|$\\)") nil t)
4588 (user-error "Don't know where to insert translated table"))
4589 (let ((beg (line-beginning-position 2)))
4590 (unless (re-search-forward
4591 (concat "END +RECEIVE +ORGTBL +" name) nil t)
4592 (user-error "Cannot find end of insertion region"))
4593 (beginning-of-line)
4594 (delete-region beg (point)))
4595 (insert txt "\n")))
4597 ;;;###autoload
4598 (defun org-table-to-lisp (&optional txt)
4599 "Convert the table at point to a Lisp structure.
4600 The structure will be a list. Each item is either the symbol `hline'
4601 for a horizontal separator line, or a list of field values as strings.
4602 The table is taken from the parameter TXT, or from the buffer at point."
4603 (unless (or txt (org-at-table-p)) (user-error "No table at point"))
4604 (let ((txt (or txt
4605 (buffer-substring-no-properties (org-table-begin)
4606 (org-table-end)))))
4607 (mapcar (lambda (x)
4608 (if (string-match org-table-hline-regexp x) 'hline
4609 (org-split-string (org-trim x) "\\s-*|\\s-*")))
4610 (org-split-string txt "[ \t]*\n[ \t]*"))))
4612 (defun orgtbl-send-table (&optional maybe)
4613 "Send a transformed version of table at point to the receiver position.
4614 With argument MAYBE, fail quietly if no transformation is defined
4615 for this table."
4616 (interactive)
4617 (catch 'exit
4618 (unless (org-at-table-p) (user-error "Not at a table"))
4619 ;; when non-interactive, we assume align has just happened.
4620 (when (org-called-interactively-p 'any) (org-table-align))
4621 (let ((dests (orgtbl-gather-send-defs))
4622 (table (org-table-to-lisp
4623 (buffer-substring-no-properties (org-table-begin)
4624 (org-table-end))))
4625 (ntbl 0))
4626 (unless dests
4627 (if maybe (throw 'exit nil)
4628 (user-error "Don't know how to transform this table")))
4629 (dolist (dest dests)
4630 (let ((name (plist-get dest :name))
4631 (transform (plist-get dest :transform))
4632 (params (plist-get dest :params)))
4633 (unless (fboundp transform)
4634 (user-error "No such transformation function %s" transform))
4635 (orgtbl-send-replace-tbl name (funcall transform table params)))
4636 (incf ntbl))
4637 (message "Table converted and installed at %d receiver location%s"
4638 ntbl (if (> ntbl 1) "s" ""))
4639 (and (> ntbl 0) ntbl))))
4641 (defun org-remove-by-index (list indices &optional i0)
4642 "Remove the elements in LIST with indices in INDICES.
4643 First element has index 0, or I0 if given."
4644 (if (not indices)
4645 list
4646 (if (integerp indices) (setq indices (list indices)))
4647 (setq i0 (1- (or i0 0)))
4648 (delq :rm (mapcar (lambda (x)
4649 (setq i0 (1+ i0))
4650 (if (memq i0 indices) :rm x))
4651 list))))
4653 (defun orgtbl-toggle-comment ()
4654 "Comment or uncomment the orgtbl at point."
4655 (interactive)
4656 (let* ((case-fold-search t)
4657 (re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
4658 (re2 (concat "^" orgtbl-line-start-regexp))
4659 (commented (save-excursion (beginning-of-line 1)
4660 (cond ((looking-at re1) t)
4661 ((looking-at re2) nil)
4662 (t (user-error "Not at an org table")))))
4663 (re (if commented re1 re2))
4664 beg end)
4665 (save-excursion
4666 (beginning-of-line 1)
4667 (while (looking-at re) (beginning-of-line 0))
4668 (beginning-of-line 2)
4669 (setq beg (point))
4670 (while (looking-at re) (beginning-of-line 2))
4671 (setq end (point)))
4672 (comment-region beg end (if commented '(4) nil))))
4674 (defun orgtbl-insert-radio-table ()
4675 "Insert a radio table template appropriate for this major mode."
4676 (interactive)
4677 (let* ((e (assq major-mode orgtbl-radio-table-templates))
4678 (txt (nth 1 e))
4679 name pos)
4680 (unless e (user-error "No radio table setup defined for %s" major-mode))
4681 (setq name (read-string "Table name: "))
4682 (while (string-match "%n" txt)
4683 (setq txt (replace-match name t t txt)))
4684 (or (bolp) (insert "\n"))
4685 (setq pos (point))
4686 (insert txt)
4687 (goto-char pos)))
4689 ;;;###autoload
4690 (defun orgtbl-to-generic (table params)
4691 "Convert the orgtbl-mode TABLE to some other format.
4693 This generic routine can be used for many standard cases.
4695 TABLE is a list, each entry either the symbol `hline' for
4696 a horizontal separator line, or a list of fields for that
4697 line. PARAMS is a property list of parameters that can
4698 influence the conversion.
4700 Valid parameters are:
4702 :backend, :raw
4704 Export back-end used as a basis to transcode elements of the
4705 table, when no specific parameter applies to it. It is also
4706 used to translate cells contents. You can prevent this by
4707 setting :raw property to a non-nil value.
4709 :splice
4711 When non-nil, only convert rows, not the table itself. This is
4712 equivalent to setting to the empty string both :tstart
4713 and :tend, which see.
4715 :skip
4717 When set to an integer N, skip the first N lines of the table.
4718 Horizontal separation lines do count for this parameter!
4720 :skipcols
4722 List of columns that should be skipped. If the table has
4723 a column with calculation marks, that column is automatically
4724 discarded beforehand.
4726 :hline
4728 String to be inserted on horizontal separation lines. May be
4729 nil to ignore these lines altogether.
4731 :sep
4733 Separator between two fields, as a string.
4735 Each in the following group may be either a string or a function
4736 of no arguments returning a string:
4738 :tstart, :tend
4740 Strings to start and end the table. Ignored when :splice is t.
4742 :lstart, :lend
4744 Strings to start and end a new table line.
4746 :llstart, :llend
4748 Strings to start and end the last table line. Default,
4749 respectively, to :lstart and :lend.
4751 Each in the following group may be a string or a function of one
4752 argument (either the cells in the current row, as a list of
4753 strings, or the current cell) returning a string:
4755 :lfmt
4757 Format string for an entire row, with enough %s to capture all
4758 fields. When non-nil, :lstart, :lend, and :sep are ignored.
4760 :llfmt
4762 Format for the entire last line, defaults to :lfmt.
4764 :fmt
4766 A format to be used to wrap the field, should contain %s for
4767 the original field value. For example, to wrap everything in
4768 dollars, you could use :fmt \"$%s$\". This may also be
4769 a property list with column numbers and format strings, or
4770 functions, e.g.,
4772 \(:fmt (2 \"$%s$\" 4 (lambda (c) (format \"$%s$\" c))))
4774 :hlstart :hllstart :hlend :hllend :hsep :hlfmt :hllfmt :hfmt
4776 Same as above, specific for the header lines in the table.
4777 All lines before the first hline are treated as header. If
4778 any of these is not present, the data line value is used.
4780 This may be either a string or a function of two arguments:
4782 :efmt
4784 Use this format to print numbers with exponential. The format
4785 should have %s twice for inserting mantissa and exponent, for
4786 example \"%s\\\\times10^{%s}\". This may also be a property
4787 list with column numbers and format strings or functions.
4788 :fmt will still be applied after :efmt."
4789 (let ((backend (plist-get params :backend))
4790 ;; Disable user-defined export filters and hooks.
4791 (org-export-filters-alist nil)
4792 (org-export-before-parsing-hook nil)
4793 (org-export-before-processing-hook nil))
4794 (when (and backend (symbolp backend) (not (org-export-get-backend backend)))
4795 (user-error "Unknown :backend value"))
4796 (when (or (not backend) (plist-get params :raw)) (require 'ox-org))
4797 (org-trim
4798 (org-export-string-as
4799 ;; Return TABLE as Org syntax. Tolerate non-string cells.
4800 (with-output-to-string
4801 (dolist (e table)
4802 (cond ((eq e 'hline) (princ "|--\n"))
4803 ((consp e)
4804 (princ "| ") (dolist (c e) (princ c) (princ " |"))
4805 (princ "\n")))))
4806 ;; Build a custom back-end according to PARAMS. Before defining
4807 ;; a translator, check if there is anything to do. When there
4808 ;; isn't, let BACKEND handle the element.
4809 (org-export-create-backend
4810 :parent (or backend 'org)
4811 :filters
4812 '((:filter-parse-tree
4813 ;; Handle :skip parameter.
4814 (lambda (tree backend info)
4815 (let ((skip (plist-get info :skip)))
4816 (when skip
4817 (unless (wholenump skip) (user-error "Wrong :skip value"))
4818 (let ((n 0))
4819 (org-element-map tree 'table-row
4820 (lambda (row)
4821 (if (>= n skip) t
4822 (org-element-extract-element row)
4823 (incf n)
4824 nil))
4825 info t))
4826 tree)))
4827 ;; Handle :skipcols parameter.
4828 (lambda (tree backend info)
4829 (let ((skipcols (plist-get info :skipcols)))
4830 (when skipcols
4831 (unless (consp skipcols) (user-error "Wrong :skipcols value"))
4832 (org-element-map tree 'table
4833 (lambda (table)
4834 (let ((specialp
4835 (org-export-table-has-special-column-p table)))
4836 (dolist (row (org-element-contents table))
4837 (when (eq (org-element-property :type row) 'standard)
4838 (let ((c 1))
4839 (dolist (cell (nthcdr (if specialp 1 0)
4840 (org-element-contents row)))
4841 (when (memq c skipcols)
4842 (org-element-extract-element cell))
4843 (incf c)))))))
4844 info)
4845 tree)))))
4846 :transcoders
4847 `((table . ,(org-table--to-generic-table params))
4848 (table-row . ,(org-table--to-generic-row params))
4849 (table-cell . ,(org-table--to-generic-cell params))
4850 ;; Section. Return contents to avoid garbage around table.
4851 (section . (lambda (s c i) c))))
4852 'body-only (org-combine-plists params '(:with-tables t))))))
4854 (defun org-table--generic-apply (value name &optional with-cons &rest args)
4855 (cond ((null value) nil)
4856 ((functionp value) `(funcall ',value ,@args))
4857 ((stringp value)
4858 (cond ((consp (car args)) `(apply #'format ,value ,@args))
4859 (args `(format ,value ,@args))
4860 (t value)))
4861 ((and with-cons (consp value))
4862 `(let ((val (cadr (memq column ',value))))
4863 (cond ((null val) contents)
4864 ((stringp val) (format val ,@args))
4865 ((functionp val) (funcall val ,@args))
4866 (t (user-error "Wrong %s value" ,name)))))
4867 (t (user-error "Wrong %s value" name))))
4869 (defun org-table--to-generic-table (params)
4870 "Return custom table transcoder according to PARAMS.
4871 PARAMS is a plist. See `orgtbl-to-generic' for more
4872 information."
4873 (let ((backend (plist-get params :backend))
4874 (splice (plist-get params :splice))
4875 (tstart (plist-get params :tstart))
4876 (tend (plist-get params :tend)))
4877 `(lambda (table contents info)
4878 (concat
4879 ,(and tstart (not splice)
4880 `(concat ,(org-table--generic-apply tstart ":tstart") "\n"))
4881 ,(if (or (not backend) tstart tend splice) 'contents
4882 `(org-export-with-backend ',backend table contents info))
4883 ,(org-table--generic-apply (and (not splice) tend) ":tend")))))
4885 (defun org-table--to-generic-row (params)
4886 "Return custom table row transcoder according to PARAMS.
4887 PARAMS is a plist. See `orgtbl-to-generic' for more
4888 information."
4889 (let* ((backend (plist-get params :backend))
4890 (lstart (plist-get params :lstart))
4891 (llstart (plist-get params :llstart))
4892 (hlstart (plist-get params :hlstart))
4893 (hllstart (plist-get params :hllstart))
4894 (lend (plist-get params :lend))
4895 (llend (plist-get params :llend))
4896 (hlend (plist-get params :hlend))
4897 (hllend (plist-get params :hllend))
4898 (lfmt (plist-get params :lfmt))
4899 (llfmt (plist-get params :llfmt))
4900 (hlfmt (plist-get params :hlfmt))
4901 (hllfmt (plist-get params :hllfmt)))
4902 `(lambda (row contents info)
4903 (if (eq (org-element-property :type row) 'rule)
4904 ,(cond
4905 ((plist-member params :hline)
4906 (org-table--generic-apply (plist-get params :hline) ":hline"))
4907 (backend `(org-export-with-backend ',backend row nil info)))
4908 (let ((headerp (org-export-table-row-in-header-p row info))
4909 (lastp (not (org-export-get-next-element row info)))
4910 (last-header-p (org-export-table-row-ends-header-p row info)))
4911 (when contents
4912 ;; Check if we can apply `:lfmt', `:llfmt', `:hlfmt', or
4913 ;; `:hllfmt' to CONTENTS. Otherwise, fallback on
4914 ;; `:lstart', `:lend' and their relatives.
4915 ,(let ((cells
4916 '(org-element-map row 'table-cell
4917 (lambda (cell)
4918 ;; Export all cells, without separators.
4920 ;; Use `org-export-data-with-backend'
4921 ;; instead of `org-export-data' to eschew
4922 ;; cached values, which
4923 ;; ignore :orgtbl-ignore-sep parameter.
4924 (org-export-data-with-backend
4925 cell
4926 (plist-get info :back-end)
4927 (org-combine-plists info '(:orgtbl-ignore-sep t))))
4928 info)))
4929 `(cond
4930 ,(and hllfmt
4931 `(last-header-p ,(org-table--generic-apply
4932 hllfmt ":hllfmt" nil cells)))
4933 ,(and hlfmt
4934 `(headerp ,(org-table--generic-apply
4935 hlfmt ":hlfmt" nil cells)))
4936 ,(and llfmt
4937 `(lastp ,(org-table--generic-apply
4938 llfmt ":llfmt" nil cells)))
4940 ,(if lfmt (org-table--generic-apply lfmt ":lfmt" nil cells)
4941 `(concat
4942 (cond
4943 ,(and
4944 (or hllstart hllend)
4945 `(last-header-p
4946 (concat
4947 ,(org-table--generic-apply hllstart ":hllstart")
4948 contents
4949 ,(org-table--generic-apply hllend ":hllend"))))
4950 ,(and
4951 (or hlstart hlend)
4952 `(headerp
4953 (concat
4954 ,(org-table--generic-apply hlstart ":hlstart")
4955 contents
4956 ,(org-table--generic-apply hlend ":hlend"))))
4957 ,(and
4958 (or llstart llend)
4959 `(lastp
4960 (concat
4961 ,(org-table--generic-apply llstart ":llstart")
4962 contents
4963 ,(org-table--generic-apply llend ":llend"))))
4965 ,(cond
4966 ((or lstart lend)
4967 `(concat
4968 ,(org-table--generic-apply lstart ":lstart")
4969 contents
4970 ,(org-table--generic-apply lend ":lend")))
4971 (backend
4972 `(org-export-with-backend
4973 ',backend row contents info))
4974 (t 'contents)))))))))))))))
4976 (defun org-table--to-generic-cell (params)
4977 "Return custom table cell transcoder according to PARAMS.
4978 PARAMS is a plist. See `orgtbl-to-generic' for more
4979 information."
4980 (let* ((backend (plist-get params :backend))
4981 (efmt (plist-get params :efmt))
4982 (fmt (plist-get params :fmt))
4983 (hfmt (plist-get params :hfmt))
4984 (sep (plist-get params :sep))
4985 (hsep (plist-get params :hsep)))
4986 `(lambda (cell contents info)
4987 (let ((headerp (org-export-table-row-in-header-p
4988 (org-export-get-parent-element cell) info))
4989 (column (1+ (cdr (org-export-table-cell-address cell info)))))
4990 ;; Make sure that contents are exported as Org data when :raw
4991 ;; parameter is non-nil.
4992 ,(when (and backend (plist-get params :raw))
4993 `(setq contents
4994 ;; Since we don't know what are the pseudo object
4995 ;; types defined in backend, we cannot pass them to
4996 ;; `org-element-interpret-data'. As a consequence,
4997 ;; they will be treated as pseudo elements, and
4998 ;; will have newlines appended instead of spaces.
4999 ;; Therefore, we must make sure :post-blank value
5000 ;; is really turned into spaces.
5001 (replace-regexp-in-string
5002 "\n" " "
5003 (org-trim
5004 (org-element-interpret-data
5005 (org-element-contents cell))))))
5006 (when contents
5007 ;; Check if we can apply `:efmt' on CONTENTS.
5008 ,(when efmt
5009 `(when (string-match orgtbl-exp-regexp contents)
5010 (let ((mantissa (match-string 1 contents))
5011 (exponent (match-string 2 contents)))
5012 (setq contents ,(org-table--generic-apply
5013 efmt ":efmt" t 'mantissa 'exponent)))))
5014 ;; Check if we can apply FMT (or HFMT) on CONTENTS.
5015 (cond
5016 ,(and hfmt `(headerp (setq contents ,(org-table--generic-apply
5017 hfmt ":hfmt" t 'contents))))
5018 ,(and fmt `(t (setq contents ,(org-table--generic-apply
5019 fmt ":fmt" t 'contents))))))
5020 ;; If a separator is provided, use it instead of BACKEND's.
5021 ;; Separators are ignored when LFMT (or equivalent) is
5022 ;; provided.
5023 ,(cond
5024 ((or hsep sep)
5025 `(if (or ,(and (not sep) '(not headerp))
5026 (plist-get info :orgtbl-ignore-sep)
5027 (not (org-export-get-next-element cell info)))
5028 ,(if (not backend) 'contents
5029 `(org-export-with-backend ',backend cell contents info))
5030 (concat contents
5031 ,(if (and sep hsep) `(if headerp ,hsep ,sep)
5032 (or hsep sep)))))
5033 (backend `(org-export-with-backend ',backend cell contents info))
5034 (t 'contents))))))
5036 ;;;###autoload
5037 (defun orgtbl-to-tsv (table params)
5038 "Convert the orgtbl-mode table to TAB separated material."
5039 (orgtbl-to-generic table (org-combine-plists '(:sep "\t") params)))
5041 ;;;###autoload
5042 (defun orgtbl-to-csv (table params)
5043 "Convert the orgtbl-mode table to CSV material.
5044 This does take care of the proper quoting of fields with comma or quotes."
5045 (orgtbl-to-generic table
5046 (org-combine-plists '(:sep "," :fmt org-quote-csv-field)
5047 params)))
5049 ;;;###autoload
5050 (defun orgtbl-to-latex (table params)
5051 "Convert the orgtbl-mode TABLE to LaTeX.
5053 TABLE is a list, each entry either the symbol `hline' for
5054 a horizontal separator line, or a list of fields for that line.
5055 PARAMS is a property list of parameters that can influence the
5056 conversion. All parameters from `orgtbl-to-generic' are
5057 supported. It is also possible to use the following ones:
5059 :booktabs
5061 When non-nil, use formal \"booktabs\" style.
5063 :environment
5065 Specify environment to use, as a string. If you use
5066 \"longtable\", you may also want to specify :language property,
5067 as a string, to get proper continuation strings."
5068 (require 'ox-latex)
5069 (orgtbl-to-generic
5070 table
5071 (org-combine-plists
5072 ;; Provide sane default values.
5073 (list :backend 'latex
5074 :latex-default-table-mode 'table
5075 :latex-tables-centered nil
5076 :latex-tables-booktabs (plist-get params :booktabs)
5077 :latex-table-scientific-notation nil
5078 :latex-default-table-environment
5079 (or (plist-get params :environment) "tabular"))
5080 params)))
5082 ;;;###autoload
5083 (defun orgtbl-to-html (table params)
5084 "Convert the orgtbl-mode TABLE to HTML.
5086 TABLE is a list, each entry either the symbol `hline' for
5087 a horizontal separator line, or a list of fields for that line.
5088 PARAMS is a property list of parameters that can influence the
5089 conversion. All parameters from `orgtbl-to-generic' are
5090 supported. It is also possible to use the following one:
5092 :attributes
5094 Attributes and values, as a plist, which will be used in
5095 <table> tag."
5096 (require 'ox-html)
5097 (orgtbl-to-generic
5098 table
5099 (org-combine-plists
5100 ;; Provide sane default values.
5101 (list :backend 'html
5102 :html-table-data-tags '("<td%s>" . "</td>")
5103 :html-table-use-header-tags-for-first-column nil
5104 :html-table-align-individual-fields t
5105 :html-table-row-tags '("<tr>" . "</tr>")
5106 :html-table-attributes
5107 (if (plist-member params :attributes)
5108 (plist-get params :attributes)
5109 '(:border "2" :cellspacing "0" :cellpadding "6" :rules "groups"
5110 :frame "hsides")))
5111 params)))
5113 ;;;###autoload
5114 (defun orgtbl-to-texinfo (table params)
5115 "Convert the orgtbl-mode TABLE to Texinfo.
5117 TABLE is a list, each entry either the symbol `hline' for
5118 a horizontal separator line, or a list of fields for that line.
5119 PARAMS is a property list of parameters that can influence the
5120 conversion. All parameters from `orgtbl-to-generic' are
5121 supported. It is also possible to use the following one:
5123 :columns
5125 Column widths, as a string. When providing column fractions,
5126 \"@columnfractions\" command can be omitted."
5127 (require 'ox-texinfo)
5128 (let ((output
5129 (orgtbl-to-generic
5130 table
5131 (org-combine-plists
5132 (list :backend 'texinfo
5133 :texinfo-tables-verbatim nil
5134 :texinfo-table-scientific-notation nil)
5135 params)))
5136 (columns (let ((w (plist-get params :columns)))
5137 (cond ((not w) nil)
5138 ((org-string-match-p "{\\|@columnfractions " w) w)
5139 (t (concat "@columnfractions " w))))))
5140 (if (not columns) output
5141 (replace-regexp-in-string
5142 "@multitable \\(.*\\)" columns output t nil 1))))
5144 ;;;###autoload
5145 (defun orgtbl-to-orgtbl (table params)
5146 "Convert the orgtbl-mode TABLE into another orgtbl-mode table.
5148 TABLE is a list, each entry either the symbol `hline' for
5149 a horizontal separator line, or a list of fields for that line.
5150 PARAMS is a property list of parameters that can influence the
5151 conversion. All parameters from `orgtbl-to-generic' are
5152 supported.
5154 Useful when slicing one table into many. The :hline, :sep,
5155 :lstart, and :lend provide orgtbl framing. :tstart and :tend can
5156 be set to provide ORGTBL directives for the generated table."
5157 (require 'ox-org)
5158 (orgtbl-to-generic table (org-combine-plists params (list :backend 'org))))
5160 (defun orgtbl-to-table.el (table params)
5161 "Convert the orgtbl-mode TABLE into a table.el table.
5162 TABLE is a list, each entry either the symbol `hline' for
5163 a horizontal separator line, or a list of fields for that line.
5164 PARAMS is a property list of parameters that can influence the
5165 conversion. All parameters from `orgtbl-to-generic' are
5166 supported."
5167 (with-temp-buffer
5168 (insert (orgtbl-to-orgtbl table params))
5169 (org-table-align)
5170 (replace-regexp-in-string
5171 "-|" "-+"
5172 (replace-regexp-in-string "|-" "+-" (buffer-substring 1 (buffer-size))))))
5174 (defun orgtbl-to-unicode (table params)
5175 "Convert the orgtbl-mode TABLE into a table with unicode characters.
5177 TABLE is a list, each entry either the symbol `hline' for
5178 a horizontal separator line, or a list of fields for that line.
5179 PARAMS is a property list of parameters that can influence the
5180 conversion. All parameters from `orgtbl-to-generic' are
5181 supported. It is also possible to use the following ones:
5183 :ascii-art
5185 When non-nil, use \"ascii-art-to-unicode\" package to translate
5186 the table. You can download it here:
5187 http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el.
5189 :narrow
5191 When non-nil, narrow columns width than provided width cookie,
5192 using \"=>\" as an ellipsis, just like in an Org mode buffer."
5193 (require 'ox-ascii)
5194 (orgtbl-to-generic
5195 table
5196 (org-combine-plists
5197 (list :backend 'ascii
5198 :ascii-charset 'utf-8
5199 :ascii-table-widen-columns (not (plist-get params :narrow))
5200 :ascii-table-use-ascii-art (plist-get params :ascii-art))
5201 params)))
5203 ;; Put the cursor in a column containing numerical values
5204 ;; of an Org-Mode table,
5205 ;; type C-c " a
5206 ;; A new column is added with a bar plot.
5207 ;; When the table is refreshed (C-u C-c *),
5208 ;; the plot is updated to reflect the new values.
5210 (defun orgtbl-ascii-draw (value min max &optional width characters)
5211 "Draw an ascii bar in a table.
5212 VALUE is a the value to plot, the width of the bar to draw. A
5213 value equal to MIN will be displayed as empty (zero width bar).
5214 A value equal to MAX will draw a bar filling all the WIDTH.
5215 WIDTH is the expected width in characters of the column.
5216 CHARACTERS is a string that will compose the bar, with shades of
5217 grey from pure white to pure black. It defaults to a 10
5218 characters string of regular ascii characters."
5219 (let* ((characters (or characters " .:;c!lhVHW"))
5220 (width (or width 12))
5221 (value (if (numberp value) value (string-to-number value)))
5222 (value (* (/ (- (+ value 0.0) min) (- max min)) width)))
5223 (cond
5224 ((< value 0) "too small")
5225 ((> value width) "too large")
5227 (let ((len (1- (length characters))))
5228 (concat
5229 (make-string (floor value) (elt characters len))
5230 (string (elt characters
5231 (floor (* (- value (floor value)) len))))))))))
5233 ;;;###autoload
5234 (defun orgtbl-ascii-plot (&optional ask)
5235 "Draw an ascii bar plot in a column.
5236 With cursor in a column containing numerical values, this
5237 function will draw a plot in a new column.
5238 ASK, if given, is a numeric prefix to override the default 12
5239 characters width of the plot. ASK may also be the
5240 \\[universal-argument] prefix, which will prompt for the width."
5241 (interactive "P")
5242 (let ((col (org-table-current-column))
5243 (min 1e999) ; 1e999 will be converted to infinity
5244 (max -1e999) ; which is the desired result
5245 (table (org-table-to-lisp))
5246 (length
5247 (cond ((consp ask)
5248 (read-number "Length of column " 12))
5249 ((numberp ask) ask)
5250 (t 12))))
5251 ;; Skip any hline a the top of table.
5252 (while (eq (car table) 'hline) (setq table (cdr table)))
5253 ;; Skip table header if any.
5254 (dolist (x (or (cdr (memq 'hline table)) table))
5255 (when (consp x)
5256 (setq x (nth (1- col) x))
5257 (when (string-match
5258 "^[-+]?\\([0-9]*[.]\\)?[0-9]*\\([eE][+-]?[0-9]+\\)?$"
5260 (setq x (string-to-number x))
5261 (when (> min x) (setq min x))
5262 (when (< max x) (setq max x)))))
5263 (org-table-insert-column)
5264 (org-table-move-column-right)
5265 (org-table-store-formulas
5266 (cons
5267 (cons
5268 (number-to-string (1+ col))
5269 (format "'(%s $%s %s %s %s)"
5270 "orgtbl-ascii-draw" col min max length))
5271 (org-table-get-stored-formulas)))
5272 (org-table-recalculate t)))
5274 ;; Example of extension: unicode characters
5275 ;; Here are two examples of different styles.
5277 ;; Unicode block characters are used to give a smooth effect.
5278 ;; See http://en.wikipedia.org/wiki/Block_Elements
5279 ;; Use one of those drawing functions
5280 ;; - orgtbl-ascii-draw (the default ascii)
5281 ;; - orgtbl-uc-draw-grid (unicode with a grid effect)
5282 ;; - orgtbl-uc-draw-cont (smooth unicode)
5284 ;; This is best viewed with the "DejaVu Sans Mono" font
5285 ;; (use M-x set-default-font).
5287 (defun orgtbl-uc-draw-grid (value min max &optional width)
5288 "Draw a bar in a table using block unicode characters.
5289 It is a variant of orgtbl-ascii-draw with Unicode block
5290 characters, for a smooth display. Bars appear as grids (to the
5291 extent the font allows)."
5292 ;; http://en.wikipedia.org/wiki/Block_Elements
5293 ;; best viewed with the "DejaVu Sans Mono" font.
5294 (orgtbl-ascii-draw value min max width
5295 " \u258F\u258E\u258D\u258C\u258B\u258A\u2589"))
5297 (defun orgtbl-uc-draw-cont (value min max &optional width)
5298 "Draw a bar in a table using block unicode characters.
5299 It is a variant of orgtbl-ascii-draw with Unicode block
5300 characters, for a smooth display. Bars are solid (to the extent
5301 the font allows)."
5302 (orgtbl-ascii-draw value min max width
5303 " \u258F\u258E\u258D\u258C\u258B\u258A\u2589\u2588"))
5305 (defun org-table-get-remote-range (name-or-id form)
5306 "Get a field value or a list of values in a range from table at ID.
5308 NAME-OR-ID may be the name of a table in the current file as set
5309 by a \"#+NAME:\" directive. The first table following this line
5310 will then be used. Alternatively, it may be an ID referring to
5311 any entry, also in a different file. In this case, the first
5312 table in that entry will be referenced.
5313 FORM is a field or range descriptor like \"@2$3\" or \"B3\" or
5314 \"@I$2..@II$2\". All the references must be absolute, not relative.
5316 The return value is either a single string for a single field, or a
5317 list of the fields in the rectangle."
5318 (save-match-data
5319 (let ((case-fold-search t) (id-loc nil)
5320 ;; Protect a bunch of variables from being overwritten
5321 ;; by the context of the remote table
5322 org-table-column-names org-table-column-name-regexp
5323 org-table-local-parameters org-table-named-field-locations
5324 org-table-current-line-types org-table-current-begin-line
5325 org-table-current-begin-pos org-table-dlines
5326 org-table-current-ncol
5327 org-table-hlines org-table-last-alignment
5328 org-table-last-column-widths org-table-last-alignment
5329 org-table-last-column-widths tbeg
5330 buffer loc)
5331 (setq form (org-table-convert-refs-to-rc form))
5332 (save-excursion
5333 (save-restriction
5334 (widen)
5335 (save-excursion
5336 (goto-char (point-min))
5337 (if (re-search-forward
5338 (concat "^[ \t]*#\\+\\(tbl\\)?name:[ \t]*"
5339 (regexp-quote name-or-id) "[ \t]*$")
5340 nil t)
5341 (setq buffer (current-buffer) loc (match-beginning 0))
5342 (setq id-loc (org-id-find name-or-id 'marker))
5343 (unless (and id-loc (markerp id-loc))
5344 (user-error "Can't find remote table \"%s\"" name-or-id))
5345 (setq buffer (marker-buffer id-loc)
5346 loc (marker-position id-loc))
5347 (move-marker id-loc nil)))
5348 (with-current-buffer buffer
5349 (save-excursion
5350 (save-restriction
5351 (widen)
5352 (goto-char loc)
5353 (forward-char 1)
5354 (unless (and (re-search-forward "^\\(\\*+ \\)\\|^[ \t]*|" nil t)
5355 (not (match-beginning 1)))
5356 (user-error "Cannot find a table at NAME or ID %s" name-or-id))
5357 (setq tbeg (point-at-bol))
5358 (org-table-get-specials)
5359 (setq form (org-table-formula-substitute-names
5360 (org-table-formula-handle-first/last-rc form)))
5361 (if (and (string-match org-table-range-regexp form)
5362 (> (length (match-string 0 form)) 1))
5363 (save-match-data
5364 (org-table-get-range (match-string 0 form) tbeg 1))
5365 form)))))))))
5367 (defun org-table-remote-reference-indirection (form)
5368 "Return formula with table remote references substituted by indirection.
5369 For example \"remote($1, @>$2)\" => \"remote(year_2013, @>$1)\".
5370 This indirection works only with the format @ROW$COLUMN. The
5371 format \"B3\" is not supported because it can not be
5372 distinguished from a plain table name or ID."
5373 (let ((start 0))
5374 (while (string-match (concat
5375 ;; Same as in `org-table-eval-formula'.
5376 "\\<remote([ \t]*\\("
5377 ;; Allow "$1", "@<", "$-1", "@<<$1" etc.
5378 "[@$][^ \t,]+"
5379 ;; Same as in `org-table-eval-formula'.
5380 "\\)[ \t]*,[ \t]*\\([^\n)]+\\))")
5381 form
5382 start)
5383 ;; The position of the character as far as possible to the right
5384 ;; that will not be replaced and particularly not be shifted by
5385 ;; `replace-match'.
5386 (setq start (match-beginning 1))
5387 ;; Substitute the remote reference with the value found in the
5388 ;; field.
5389 (setq form
5390 (replace-match
5391 (save-match-data
5392 (org-table-get-range (org-table-formula-handle-first/last-rc
5393 (match-string 1 form))))
5394 t t form 1))))
5395 form)
5397 (defmacro org-define-lookup-function (mode)
5398 (let ((mode-str (symbol-name mode))
5399 (first-p (equal mode 'first))
5400 (all-p (equal mode 'all)))
5401 (let ((plural-str (if all-p "s" "")))
5402 `(defun ,(intern (format "org-lookup-%s" mode-str)) (val s-list r-list &optional predicate)
5403 ,(format "Find %s occurrence%s of VAL in S-LIST; return corresponding element%s of R-LIST.
5404 If R-LIST is nil, return matching element%s of S-LIST.
5405 If PREDICATE is not nil, use it instead of `equal' to match VAL.
5406 Matching is done by (PREDICATE VAL S), where S is an element of S-LIST.
5407 This function is generated by a call to the macro `org-define-lookup-function'."
5408 mode-str plural-str plural-str plural-str)
5409 (let ,(let ((lvars '((p (or predicate 'equal))
5410 (sl s-list)
5411 (rl (or r-list s-list))
5412 (ret nil))))
5413 (if first-p (add-to-list 'lvars '(match-p nil)))
5414 lvars)
5415 (while ,(if first-p '(and (not match-p) sl) 'sl)
5416 (progn
5417 (if (funcall p val (car sl))
5418 (progn
5419 ,(if first-p '(setq match-p t))
5420 (let ((rval (car rl)))
5421 (setq ret ,(if all-p '(append ret (list rval)) 'rval)))))
5422 (setq sl (cdr sl) rl (cdr rl))))
5423 ret)))))
5425 (org-define-lookup-function first)
5426 (org-define-lookup-function last)
5427 (org-define-lookup-function all)
5429 (provide 'org-table)
5431 ;; Local variables:
5432 ;; generated-autoload-file: "org-loaddefs.el"
5433 ;; End:
5435 ;;; org-table.el ends here