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