lisp/org-table.el: fix table alignment
[org-mode/org-tableheadings.git] / lisp / org-table.el
blob9e57305c3b00203ffbadd6b9171bf9aa8d30192e
1 ;;; org-table.el --- The Table Editor for Org -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2004-2019 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: https://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 <https://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-macs)
39 (require 'org-compat)
40 (require 'org-keys)
42 (declare-function calc-eval "calc" (str &optional separator &rest args))
43 (declare-function org-at-timestamp-p "org" (&optional extended))
44 (declare-function org-delete-backward-char "org" (N))
45 (declare-function org-element-at-point "org-element" ())
46 (declare-function org-element-contents "org-element" (element))
47 (declare-function org-element-extract-element "org-element" (element))
48 (declare-function org-element-interpret-data "org-element" (data))
49 (declare-function org-element-lineage "org-element" (blob &optional types with-self))
50 (declare-function org-element-map "org-element" (data types fun &optional info first-match no-recursion with-affiliated))
51 (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
52 (declare-function org-element-property "org-element" (property element))
53 (declare-function org-element-type "org-element" (element))
54 (declare-function org-entry-get "org" (pom property &optional inherit literal-nil))
55 (declare-function org-export-create-backend "ox" (&rest rest) t)
56 (declare-function org-export-data-with-backend "ox" (data backend info))
57 (declare-function org-export-filter-apply-functions "ox" (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" (&optional backend subtreep ext-plist))
61 (declare-function org-export-install-filters "ox" (info))
62 (declare-function org-export-table-has-special-column-p "ox" (table))
63 (declare-function org-export-table-row-is-special-p "ox" (table-row info))
64 (declare-function org-id-find "org-id" (id &optional markerp))
65 (declare-function org-indent-line "org" ())
66 (declare-function org-load-modules-maybe "org" (&optional force))
67 (declare-function org-restart-font-lock "org" ())
68 (declare-function org-sort-remove-invisible "org" (s))
69 (declare-function org-time-stamp-format "org" (&optional long inactive))
70 (declare-function org-time-string-to-absolute "org" (s &optional daynr prefer buffer pos))
71 (declare-function org-time-string-to-time "org" (s))
72 (declare-function org-timestamp-up-day "org" (&optional arg))
74 (defvar constants-unit-system)
75 (defvar org-M-RET-may-split-line)
76 (defvar org-element-use-cache)
77 (defvar org-export-filters-alist)
78 (defvar org-finish-function)
79 (defvar org-inhibit-highlight-removal)
80 (defvar org-inhibit-startup)
81 (defvar org-selected-window)
82 (defvar org-self-insert-cluster-for-undo)
83 (defvar org-self-insert-command-undo-counter)
84 (defvar org-ts-regexp)
85 (defvar org-ts-regexp-both)
86 (defvar org-ts-regexp-inactive)
87 (defvar org-ts-regexp3)
88 (defvar org-window-configuration)
89 (defvar sort-fold-case)
92 ;;; Customizables
94 (defgroup org-table nil
95 "Options concerning tables in Org mode."
96 :tag "Org Table"
97 :group 'org)
99 (defcustom orgtbl-optimized t
100 "Non-nil means use the optimized table editor version for `orgtbl-mode'.
102 In the optimized version, the table editor takes over all simple keys that
103 normally just insert a character. In tables, the characters are inserted
104 in a way to minimize disturbing the table structure (i.e. in overwrite mode
105 for empty fields). Outside tables, the correct binding of the keys is
106 restored.
108 Changing this variable requires a restart of Emacs to become
109 effective."
110 :group 'org-table
111 :type 'boolean)
113 (defcustom orgtbl-radio-table-templates
114 '((latex-mode "% BEGIN RECEIVE ORGTBL %n
115 % END RECEIVE ORGTBL %n
116 \\begin{comment}
117 #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
118 | | |
119 \\end{comment}\n")
120 (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
121 @c END RECEIVE ORGTBL %n
122 @ignore
123 #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
124 | | |
125 @end ignore\n")
126 (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
127 <!-- END RECEIVE ORGTBL %n -->
128 <!--
129 #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
130 | | |
131 -->\n")
132 (org-mode "#+ BEGIN RECEIVE ORGTBL %n
133 #+ END RECEIVE ORGTBL %n
135 #+ORGTBL: SEND %n orgtbl-to-orgtbl :splice nil :skip 0
136 | | |
138 "Templates for radio tables in different major modes.
139 Each template must define lines that will be treated as a comment and that
140 must contain the \"BEGIN RECEIVE ORGTBL %n\" and \"END RECEIVE ORGTBL\"
141 lines where \"%n\" will be replaced with the name of the table during
142 insertion of the template. The transformed table will later be inserted
143 between these lines.
145 The template should also contain a minimal table in a multiline comment.
146 If multiline comments are not possible in the buffer language,
147 you can pack it into a string that will not be used when the code
148 is compiled or executed. Above the table will you need a line with
149 the fixed string \"#+ORGTBL: SEND\", followed by instruction on how to
150 convert the table into a data structure useful in the
151 language of the buffer. Check the manual for the section on
152 \"Translator functions\", and more generally check out
153 the Info node `(org)Tables in arbitrary syntax'.
155 All occurrences of %n in a template will be replaced with the name of the
156 table, obtained by prompting the user."
157 :group 'org-table
158 :type '(repeat
159 (list (symbol :tag "Major mode")
160 (string :tag "Format"))))
162 (defgroup org-table-settings nil
163 "Settings for tables in Org mode."
164 :tag "Org Table Settings"
165 :group 'org-table)
167 (defcustom org-table-default-size "5x2"
168 "The default size for newly created tables, Columns x Rows."
169 :group 'org-table-settings
170 :type 'string)
172 (defcustom org-table-number-regexp
173 "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|[<>]?[-+]?0[xX][0-9a-fA-F.]+\\|[<>]?[-+]?[0-9]+#[0-9a-zA-Z.]+\\|nan\\|[-+u]?inf\\)$"
174 "Regular expression for recognizing numbers in table columns.
175 If a table column contains mostly numbers, it will be aligned to the
176 right. If not, it will be aligned to the left.
178 The default value of this option is a regular expression which allows
179 anything which looks remotely like a number as used in scientific
180 context. For example, all of the following will be considered a
181 number:
182 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
184 Other options offered by the customize interface are more restrictive."
185 :group 'org-table-settings
186 :type '(choice
187 (const :tag "Positive Integers"
188 "^[0-9]+$")
189 (const :tag "Integers"
190 "^[-+]?[0-9]+$")
191 (const :tag "Floating Point Numbers"
192 "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
193 (const :tag "Floating Point Number or Integer"
194 "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
195 (const :tag "Exponential, Floating point, Integer"
196 "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
197 (const :tag "Very General Number-Like, including hex and Calc radix"
198 "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|[<>]?[-+]?0[xX][0-9a-fA-F.]+\\|[<>]?[-+]?[0-9]+#[0-9a-zA-Z.]+\\|nan\\|[-+u]?inf\\)$")
199 (const :tag "Very General Number-Like, including hex and Calc radix, allows comma as decimal mark"
200 "^\\([<>]?[-+^.,0-9]*[0-9][-+^.0-9eEdDx()%]*\\|[<>]?[-+]?0[xX][0-9a-fA-F.]+\\|[<>]?[-+]?[0-9]+#[0-9a-zA-Z.]+\\|nan\\|[-+u]?inf\\)$")
201 (string :tag "Regexp:")))
203 (defcustom org-table-number-fraction 0.5
204 "Fraction of numbers in a column required to make the column align right.
205 In a column all non-white fields are considered. If at least
206 this fraction of fields is matched by `org-table-number-regexp',
207 alignment to the right border applies."
208 :group 'org-table-settings
209 :type 'number)
211 (defcustom org-table-formula-field-format "%s"
212 "Format for fields which contain the result of a formula.
213 For example, using \"~%s~\" will display the result within tilde
214 characters. Beware that modifying the display can prevent the
215 field from being used in another formula."
216 :group 'org-table-settings
217 :version "24.1"
218 :type 'string)
220 (defgroup org-table-editing nil
221 "Behavior of tables during editing in Org mode."
222 :tag "Org Table Editing"
223 :group 'org-table)
225 (defcustom org-table-automatic-realign t
226 "Non-nil means automatically re-align table when pressing TAB or RETURN.
227 When nil, aligning is only done with `\\[org-table-align]', or after column
228 removal/insertion."
229 :group 'org-table-editing
230 :type 'boolean)
232 (defcustom org-table-auto-blank-field t
233 "Non-nil means automatically blank table field when starting to type into it.
234 This only happens when typing immediately after a field motion
235 command (TAB, S-TAB or RET)."
236 :group 'org-table-editing
237 :type 'boolean)
239 (defcustom org-table-exit-follow-field-mode-when-leaving-table t
240 "Non-nil means automatically exit the follow mode.
241 When nil, the follow mode will stay on and be active in any table
242 the cursor enters. Since the table follow filed mode messes with the
243 window configuration, it is not recommended to set this variable to nil,
244 except maybe locally in a special file that has mostly tables with long
245 fields."
246 :group 'org-table
247 :version "24.1"
248 :type 'boolean)
250 (defcustom org-table-fix-formulas-confirm nil
251 "Whether the user should confirm when Org fixes formulas."
252 :group 'org-table-editing
253 :version "24.1"
254 :type '(choice
255 (const :tag "with yes-or-no" yes-or-no-p)
256 (const :tag "with y-or-n" y-or-n-p)
257 (const :tag "no confirmation" nil)))
259 (defcustom org-table-tab-jumps-over-hlines t
260 "Non-nil means tab in the last column of a table with jump over a hline.
261 If a horizontal separator line is following the current line,
262 `org-table-next-field' can either create a new row before that line, or jump
263 over the line. When this option is nil, a new line will be created before
264 this line."
265 :group 'org-table-editing
266 :type 'boolean)
268 (defcustom org-table-shrunk-column-indicator "…"
269 "String to be displayed in a shrunk column."
270 :group 'org-table-editing
271 :type 'string
272 :package-version '(Org . "9.2")
273 :safe (lambda (v) (and (stringp v) (not (equal v "")))))
275 (defgroup org-table-calculation nil
276 "Options concerning tables in Org mode."
277 :tag "Org Table Calculation"
278 :group 'org-table)
280 (defcustom org-table-use-standard-references 'from
281 "Non-nil means using table references like B3 instead of @3$2.
282 Possible values are:
283 nil never use them
284 from accept as input, do not present for editing
285 t accept as input and present for editing"
286 :group 'org-table-calculation
287 :type '(choice
288 (const :tag "Never, don't even check user input for them" nil)
289 (const :tag "Always, both as user input, and when editing" t)
290 (const :tag "Convert user input, don't offer during editing" from)))
292 (defcustom org-table-copy-increment t
293 "Non-nil means increment when copying current field with \
294 `\\[org-table-copy-down]'."
295 :group 'org-table-calculation
296 :version "26.1"
297 :package-version '(Org . "8.3")
298 :type '(choice
299 (const :tag "Use the difference between the current and the above fields" t)
300 (integer :tag "Use a number" 1)
301 (const :tag "Don't increment the value when copying a field" nil)))
303 (defcustom org-calc-default-modes
304 '(calc-internal-prec 12
305 calc-float-format (float 8)
306 calc-angle-mode deg
307 calc-prefer-frac nil
308 calc-symbolic-mode nil
309 calc-date-format (YYYY "-" MM "-" DD " " Www (" " hh ":" mm))
310 calc-display-working-message t)
311 "List with Calc mode settings for use in `calc-eval' for table formulas.
312 The list must contain alternating symbols (Calc modes variables and values).
313 Don't remove any of the default settings, just change the values. Org mode
314 relies on the variables to be present in the list."
315 :group 'org-table-calculation
316 :type 'plist)
318 (defcustom org-table-duration-custom-format 'hours
319 "Format for the output of calc computations like $1+$2;t.
320 The default value is `hours', and will output the results as a
321 number of hours. Other allowed values are `seconds', `minutes' and
322 `days', and the output will be a fraction of seconds, minutes or
323 days. `hh:mm' selects to use hours and minutes, ignoring seconds.
324 The `U' flag in a table formula will select this specific format for
325 a single formula."
326 :group 'org-table-calculation
327 :version "24.1"
328 :type '(choice (symbol :tag "Seconds" 'seconds)
329 (symbol :tag "Minutes" 'minutes)
330 (symbol :tag "Hours " 'hours)
331 (symbol :tag "Days " 'days)
332 (symbol :tag "HH:MM " 'hh:mm)))
334 (defcustom org-table-duration-hour-zero-padding t
335 "Non-nil means hours in table duration computations should be zero-padded.
336 So this is about 08:32:34 versus 8:33:34."
337 :group 'org-table-calculation
338 :version "26.1"
339 :package-version '(Org . "9.1")
340 :type 'boolean
341 :safe #'booleanp)
343 (defcustom org-table-formula-evaluate-inline t
344 "Non-nil means TAB and RET evaluate a formula in current table field.
345 If the current field starts with an equal sign, it is assumed to be a formula
346 which should be evaluated as described in the manual and in the documentation
347 string of the command `org-table-eval-formula'. This feature requires the
348 Emacs calc package.
349 When this variable is nil, formula calculation is only available through
350 the command `\\[org-table-eval-formula]'."
351 :group 'org-table-calculation
352 :type 'boolean)
354 (defcustom org-table-formula-use-constants t
355 "Non-nil means interpret constants in formulas in tables.
356 A constant looks like `$c' or `$Grav' and will be replaced before evaluation
357 by the value given in `org-table-formula-constants', or by a value obtained
358 from the `constants.el' package."
359 :group 'org-table-calculation
360 :type 'boolean)
362 (defcustom org-table-formula-constants nil
363 "Alist with constant names and values, for use in table formulas.
364 The car of each element is a name of a constant, without the `$' before it.
365 The cdr is the value as a string. For example, if you'd like to use the
366 speed of light in a formula, you would configure
368 (setq org-table-formula-constants \\='((\"c\" . \"299792458.\")))
370 and then use it in an equation like `$1*$c'.
372 Constants can also be defined on a per-file basis using a line like
374 #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
375 :group 'org-table-calculation
376 :type '(repeat
377 (cons (string :tag "name")
378 (string :tag "value"))))
380 (defcustom org-table-allow-automatic-line-recalculation t
381 "Non-nil means lines marked with |#| or |*| will be recomputed automatically.
382 \\<org-mode-map>\
383 Automatically means when `TAB' or `RET' or `\\[org-ctrl-c-ctrl-c]' \
384 are pressed in the line."
385 :group 'org-table-calculation
386 :type 'boolean)
388 (defcustom org-table-relative-ref-may-cross-hline t
389 "Non-nil means relative formula references may cross hlines.
390 Here are the allowed values:
392 nil Relative references may not cross hlines. They will reference the
393 field next to the hline instead. Coming from below, the reference
394 will be to the field below the hline. Coming from above, it will be
395 to the field above.
396 t Relative references may cross hlines.
397 error An attempt to cross a hline will throw an error.
399 It is probably good to never set this variable to nil, for the sake of
400 portability of tables."
401 :group 'org-table-calculation
402 :type '(choice
403 (const :tag "Allow to cross" t)
404 (const :tag "Stick to hline" nil)
405 (const :tag "Error on attempt to cross" error)))
407 (defcustom org-table-formula-create-columns nil
408 "Non-nil means evaluation of formula can add new columns.
409 When non-nil, evaluating an out-of-bounds field can insert as
410 many columns as needed. When set to `warn', issue a warning when
411 doing so. When set to `prompt', ask user before creating a new
412 column. Otherwise, throw an error."
413 :group 'org-table-calculation
414 :package-version '(Org . "8.3")
415 :type '(choice
416 (const :tag "Out-of-bounds field generates an error (default)" nil)
417 (const :tag "Out-of-bounds field silently adds columns as needed" t)
418 (const :tag "Out-of-bounds field adds columns, but issues a warning" warn)
419 (const :tag "Prompt user when setting an out-of-bounds field" prompt)))
421 (defgroup org-table-import-export nil
422 "Options concerning table import and export in Org mode."
423 :tag "Org Table Import Export"
424 :group 'org-table)
426 (defcustom org-table-export-default-format "orgtbl-to-tsv"
427 "Default export parameters for `org-table-export'.
428 These can be overridden for a specific table by setting the
429 TABLE_EXPORT_FORMAT property. See the manual section on orgtbl
430 radio tables for the different export transformations and
431 available parameters."
432 :group 'org-table-import-export
433 :type 'string)
435 (defcustom org-table-convert-region-max-lines 999
436 "Max lines that `org-table-convert-region' will attempt to process.
438 The function can be slow on larger regions; this safety feature
439 prevents it from hanging Emacs."
440 :group 'org-table-import-export
441 :type 'integer
442 :package-version '(Org . "8.3"))
445 ;;; Regexps Constants
447 (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
448 "Detect an org-type or table-type table.")
450 (defconst org-table-line-regexp "^[ \t]*|"
451 "Detect an org-type table line.")
453 (defconst org-table-dataline-regexp "^[ \t]*|\\([^-~:]\\)"
454 "Detect an org-type table line.")
456 (defconst org-table-hline-regexp "^[ \t]*|\\([-~:]\\)"
457 "Detect an org-type table hline.")
459 (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
460 "Detect a table-type table hline.")
462 (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
463 "Detect the first line outside a table when searching from within it.
464 This works for both table types.")
466 (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
467 "Detect a #+TBLFM line.")
469 (defvar org-table-TBLFM-begin-regexp "^[ \t]*|.*\n[ \t]*#\\+TBLFM: ")
471 (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
472 "Regexp matching a line marked for automatic recalculation.")
474 (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
475 "Regexp matching a line marked for recalculation.")
477 (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
478 "Regexp matching a line marked for calculation.")
480 (defconst org-table-border-regexp "^[ \t]*[^| \t]"
481 "Regexp matching any line outside an Org table.")
483 (defconst org-table-range-regexp
484 "@\\([-+]?I*[-+]?[0-9]*\\)\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)\\(\\$[-+]?[0-9]+\\)?\\)?"
485 ;; 1 2 3 4 5
486 "Regular expression for matching ranges in formulas.")
488 (defconst org-table-range-regexp2
489 (concat
490 "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
491 "\\.\\."
492 "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
493 "Match a range for reference display.")
495 (defconst org-table-translate-regexp
496 (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
497 "Match a reference that needs translation, for reference display.")
499 (defconst org-table-separator-space
500 (propertize " " 'display '(space :relative-width 1))
501 "Space used around fields when aligning the table.
502 This space serves as a segment separator for the purposes of the
503 bidirectional reordering.")
506 ;;; Internal Variables
508 (defvar org-table-last-highlighted-reference nil)
510 (defvar org-table-formula-history nil)
512 (defvar org-field-marker nil)
513 (defvar org-table-buffer-is-an nil)
515 (defvar-local org-table-formula-constants-local nil
516 "Local version of `org-table-formula-constants'.")
518 (defvar org-table-may-need-update t
519 "Indicates that a table might need an update.
520 This variable is set by `org-before-change-function'.
521 `org-table-align' sets it back to nil.")
523 (defvar orgtbl-after-send-table-hook nil
524 "Hook for functions attaching to `C-c C-c', if the table is sent.
525 This can be used to add additional functionality after the table is sent
526 to the receiver position, otherwise, if table is not sent, the functions
527 are not run.")
529 (defvar org-table-column-names nil
530 "Alist with column names, derived from the `!' line.
531 This variable is initialized with `org-table-analyze'.")
533 (defvar org-table-column-name-regexp nil
534 "Regular expression matching the current column names.
535 This variable is initialized with `org-table-analyze'.")
537 (defvar org-table-local-parameters nil
538 "Alist with parameter names, derived from the `$' line.
539 This variable is initialized with `org-table-analyze'.")
541 (defvar org-table-named-field-locations nil
542 "Alist with locations of named fields.
543 Associations follow the pattern (NAME LINE COLUMN) where
544 NAME is the name of the field as a string,
545 LINE is the number of lines from the beginning of the table,
546 COLUMN is the column of the field, as an integer.
547 This variable is initialized with `org-table-analyze'.")
549 (defvar org-table-current-line-types nil
550 "Table row types in current table.
551 This variable is initialized with `org-table-analyze'.")
553 (defvar org-table-current-begin-pos nil
554 "Current table begin position, as a marker.
555 This variable is initialized with `org-table-analyze'.")
557 (defvar org-table-current-ncol nil
558 "Number of columns in current table.
559 This variable is initialized with `org-table-analyze'.")
561 (defvar org-table-dlines nil
562 "Vector of data line line numbers in the current table.
563 Line numbers are counted from the beginning of the table. This
564 variable is initialized with `org-table-analyze'.")
566 (defvar org-table-hlines nil
567 "Vector of hline line numbers in the current table.
568 Line numbers are counted from the beginning of the table. This
569 variable is initialized with `org-table-analyze'.")
571 (defvar org-table-aligned-begin-marker (make-marker)
572 "Marker at the beginning of the table last aligned.
573 Used to check if cursor still is in that table, to minimize realignment.")
575 (defvar org-table-aligned-end-marker (make-marker)
576 "Marker at the end of the table last aligned.
577 Used to check if cursor still is in that table, to minimize realignment.")
579 (defvar org-table-last-alignment nil
580 "List of flags for flushright alignment, from the last re-alignment.
581 This is being used to correctly align a single field after TAB or RET.")
583 (defvar org-table-last-column-widths nil
584 "List of max width of fields in each column.
585 This is being used to correctly align a single field after TAB or RET.")
587 (defvar-local org-table-formula-debug nil
588 "Non-nil means debug table formulas.
589 When nil, simply write \"#ERROR\" in corrupted fields.")
591 (defvar-local org-table-overlay-coordinates nil
592 "Overlay coordinates after each align of a table.")
594 (defvar org-last-recalc-line nil)
596 (defvar org-show-positions nil)
598 (defvar org-table-rectangle-overlays nil)
600 (defvar org-table-clip nil
601 "Clipboard for table regions.")
603 (defvar org-timecnt nil)
605 (defvar org-recalc-commands nil
606 "List of commands triggering the recalculation of a line.
607 Will be filled automatically during use.")
609 (defvar org-recalc-marks
610 '((" " . "Unmarked: no special line, no automatic recalculation")
611 ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
612 ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
613 ("!" . "Column name definition line. Reference in formula as $name.")
614 ("$" . "Parameter definition line name=value. Reference in formula as $name.")
615 ("_" . "Names for values in row below this one.")
616 ("^" . "Names for values in row above this one.")))
618 (defvar org-tbl-calc-modes nil)
620 (defvar org-pos nil)
623 ;;; Macros and Inlined Functions
625 (defmacro org-table-with-shrunk-columns (&rest body)
626 "Expand all columns before executing BODY, then shrink them again."
627 (declare (debug (body)))
628 (org-with-gensyms (shrunk-columns begin end)
629 `(let ((,begin (copy-marker (org-table-begin)))
630 (,end (copy-marker (org-table-end) t))
631 (,shrunk-columns (org-table--list-shrunk-columns)))
632 (org-with-point-at ,begin (org-table-expand ,begin ,end))
633 (unwind-protect
634 (progn ,@body)
635 (org-table--shrink-columns ,shrunk-columns ,begin ,end)
636 (set-marker ,begin nil)
637 (set-marker ,end nil)))))
639 (defmacro org-table-with-shrunk-field (&rest body)
640 "Save field shrunk state, execute BODY and restore state."
641 (declare (debug (body)))
642 (org-with-gensyms (end shrunk size)
643 `(let* ((,shrunk (save-match-data (org-table--shrunk-field)))
644 (,end (and ,shrunk (copy-marker (overlay-end ,shrunk) t)))
645 (,size (and ,shrunk (- ,end (overlay-start ,shrunk)))))
646 (when ,shrunk (delete-overlay ,shrunk))
647 (unwind-protect (progn ,@body)
648 (when ,shrunk (move-overlay ,shrunk (- ,end ,size) ,end))))))
650 (defmacro org-table-save-field (&rest body)
651 "Save current field; execute BODY; restore field.
652 Field is restored even in case of abnormal exit."
653 (declare (debug (body)))
654 (org-with-gensyms (line column)
655 `(let ((,line (copy-marker (line-beginning-position)))
656 (,column (org-table-current-column)))
657 (unwind-protect
658 (progn ,@body)
659 (goto-char ,line)
660 (org-table-goto-column ,column)
661 (set-marker ,line nil)))))
663 (defsubst org-table--set-calc-mode (var &optional value)
664 (if (stringp var)
665 (setq var (assoc var '(("D" calc-angle-mode deg)
666 ("R" calc-angle-mode rad)
667 ("F" calc-prefer-frac t)
668 ("S" calc-symbolic-mode t)))
669 value (nth 2 var) var (nth 1 var)))
670 (if (memq var org-tbl-calc-modes)
671 (setcar (cdr (memq var org-tbl-calc-modes)) value)
672 (cons var (cons value org-tbl-calc-modes)))
673 org-tbl-calc-modes)
676 ;;; Predicates
678 (defun org-at-TBLFM-p (&optional pos)
679 "Non-nil when point (or POS) is in #+TBLFM line."
680 (save-excursion
681 (goto-char (or pos (point)))
682 (beginning-of-line)
683 (and (let ((case-fold-search t)) (looking-at org-TBLFM-regexp))
684 (eq (org-element-type (org-element-at-point)) 'table))))
686 (defun org-at-table-p (&optional table-type)
687 "Non-nil if the cursor is inside an Org table.
688 If TABLE-TYPE is non-nil, also check for table.el-type tables."
689 (and (org-match-line (if table-type "[ \t]*[|+]" "[ \t]*|"))
690 (or (not (derived-mode-p 'org-mode))
691 (let ((e (org-element-lineage (org-element-at-point) '(table) t)))
692 (and e (or table-type
693 (eq 'org (org-element-property :type e))))))))
695 (defun org-at-table.el-p ()
696 "Non-nil when point is at a table.el table."
697 (and (org-match-line "[ \t]*[|+]")
698 (let ((element (org-element-at-point)))
699 (and (eq (org-element-type element) 'table)
700 (eq (org-element-property :type element) 'table.el)))))
702 (defun org-at-table-hline-p ()
703 "Non-nil when point is inside a hline in a table.
704 Assume point is already in a table."
705 (org-match-line org-table-hline-regexp))
707 (defun org-table-check-inside-data-field (&optional noerror assume-table)
708 "Non-nil when point is inside a table data field.
709 Raise an error otherwise, unless NOERROR is non-nil. In that
710 case, return nil if point is not inside a data field. When
711 optional argument ASSUME-TABLE is non-nil, assume point is within
712 a table."
713 (cond ((and (or assume-table (org-at-table-p))
714 (not (save-excursion (skip-chars-backward " \t") (bolp)))
715 (not (org-at-table-hline-p))
716 (not (looking-at-p "[ \t]*$"))))
717 (noerror nil)
718 (t (user-error "Not in table data field"))))
721 ;;; Create, Import, and Convert Tables
723 ;;;###autoload
724 (defun org-table-create-with-table.el ()
725 "Use the table.el package to insert a new table.
726 If there is already a table at point, convert between Org tables
727 and table.el tables."
728 (interactive)
729 (require 'table)
730 (cond
731 ((and (org-at-table.el-p)
732 (y-or-n-p "Convert table to Org table? "))
733 (org-table-convert))
734 ((and (org-at-table-p)
735 (y-or-n-p "Convert table to table.el table? "))
736 (org-table-align)
737 (org-table-convert))
738 (t (call-interactively 'table-insert))))
740 ;;;###autoload
741 (defun org-table-create-or-convert-from-region (arg)
742 "Convert region to table, or create an empty table.
743 If there is an active region, convert it to a table, using the function
744 `org-table-convert-region'. See the documentation of that function
745 to learn how the prefix argument is interpreted to determine the field
746 separator.
747 If there is no such region, create an empty table with `org-table-create'."
748 (interactive "P")
749 (if (org-region-active-p)
750 (org-table-convert-region (region-beginning) (region-end) arg)
751 (org-table-create arg)))
753 ;;;###autoload
754 (defun org-table-create (&optional size)
755 "Query for a size and insert a table skeleton.
756 SIZE is a string Columns x Rows like for example \"3x2\"."
757 (interactive "P")
758 (unless size
759 (setq size (read-string
760 (concat "Table size Columns x Rows [e.g. "
761 org-table-default-size "]: ")
762 "" nil org-table-default-size)))
764 (let* ((pos (point))
765 (indent (make-string (current-column) ?\ ))
766 (split (org-split-string size " *x *"))
767 (rows (string-to-number (nth 1 split)))
768 (columns (string-to-number (car split)))
769 (line (concat (apply 'concat indent "|" (make-list columns " |"))
770 "\n")))
771 (if (string-match "^[ \t]*$" (buffer-substring-no-properties
772 (point-at-bol) (point)))
773 (beginning-of-line 1)
774 (newline))
775 ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
776 (dotimes (_ rows) (insert line))
777 (goto-char pos)
778 (when (> rows 1)
779 ;; Insert a hline after the first row.
780 (end-of-line 1)
781 (insert "\n|-")
782 (goto-char pos))
783 (org-table-align)))
785 ;;;###autoload
786 (defun org-table-convert-region (beg0 end0 &optional separator)
787 "Convert region to a table.
789 The region goes from BEG0 to END0, but these borders will be moved
790 slightly, to make sure a beginning of line in the first line is included.
792 SEPARATOR specifies the field separator in the lines. It can have the
793 following values:
795 (4) Use the comma as a field separator
796 (16) Use a TAB as field separator
797 (64) Prompt for a regular expression as field separator
798 integer When a number, use that many spaces, or a TAB, as field separator
799 regexp When a regular expression, use it to match the separator
800 nil When nil, the command tries to be smart and figure out the
801 separator in the following way:
802 - when each line contains a TAB, assume TAB-separated material
803 - when each line contains a comma, assume CSV material
804 - else, assume one or more SPACE characters as separator."
805 (interactive "r\nP")
806 (let* ((beg (min beg0 end0))
807 (end (max beg0 end0))
809 (if (> (count-lines beg end) org-table-convert-region-max-lines)
810 (user-error "Region is longer than `org-table-convert-region-max-lines' (%s) lines; not converting"
811 org-table-convert-region-max-lines)
812 (when (equal separator '(64))
813 (setq separator (read-regexp "Regexp for field separator")))
814 (goto-char beg)
815 (beginning-of-line 1)
816 (setq beg (point-marker))
817 (goto-char end)
818 (if (bolp) (backward-char 1) (end-of-line 1))
819 (setq end (point-marker))
820 ;; Get the right field separator
821 (unless separator
822 (goto-char beg)
823 (setq separator
824 (cond
825 ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
826 ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
827 (t 1))))
828 (goto-char beg)
829 (if (equal separator '(4))
830 (while (< (point) end)
831 ;; parse the csv stuff
832 (cond
833 ((looking-at "^") (insert "| "))
834 ((looking-at "[ \t]*$") (replace-match " |") (beginning-of-line 2))
835 ((looking-at "[ \t]*\"\\([^\"\n]*\\)\"")
836 (replace-match "\\1")
837 (if (looking-at "\"") (insert "\"")))
838 ((looking-at "[^,\n]+") (goto-char (match-end 0)))
839 ((looking-at "[ \t]*,") (replace-match " | "))
840 (t (beginning-of-line 2))))
841 (setq re (cond
842 ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
843 ((equal separator '(16)) "^\\|\t")
844 ((integerp separator)
845 (if (< separator 1)
846 (user-error "Number of spaces in separator must be >= 1")
847 (format "^ *\\| *\t *\\| \\{%d,\\}" separator)))
848 ((stringp separator)
849 (format "^ *\\|%s" separator))
850 (t (error "This should not happen"))))
851 (while (re-search-forward re end t)
852 (replace-match "| " t t)))
853 (goto-char beg)
854 (org-table-align))))
856 ;;;###autoload
857 (defun org-table-import (file separator)
858 "Import FILE as a table.
860 The command tries to be smart and figure out the separator in the
861 following way:
863 - when each line contains a TAB, assume TAB-separated material
864 - when each line contains a comma, assume CSV material
865 - else, assume one or more SPACE characters as separator.
867 When non-nil, SEPARATOR specifies the field separator in the
868 lines. It can have the following values:
870 (4) Use the comma as a field separator
871 (16) Use a TAB as field separator
872 (64) Prompt for a regular expression as field separator
873 integer When a number, use that many spaces, or a TAB, as field separator
874 regexp When a regular expression, use it to match the separator."
875 (interactive "f\nP")
876 (unless (bolp) (insert "\n"))
877 (let ((beg (point))
878 (pm (point-max)))
879 (insert-file-contents file)
880 (org-table-convert-region beg (+ (point) (- (point-max) pm)) separator)))
882 (defun org-table-convert ()
883 "Convert from Org table to table.el and back.
884 Obviously, this only works within limits. When an Org table is converted
885 to table.el, all horizontal separator lines get lost, because table.el uses
886 these as cell boundaries and has no notion of horizontal lines. A table.el
887 table can be converted to an Org table only if it does not do row or column
888 spanning. Multiline cells will become multiple cells. Beware, Org mode
889 does not test if the table can be successfully converted - it blindly
890 applies a recipe that works for simple tables."
891 (interactive)
892 (require 'table)
893 (if (org-at-table.el-p)
894 ;; convert to Org table
895 (let ((beg (copy-marker (org-table-begin t)))
896 (end (copy-marker (org-table-end t))))
897 (table-unrecognize-region beg end)
898 (goto-char beg)
899 (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
900 (replace-match ""))
901 (goto-char beg))
902 (if (org-at-table-p)
903 ;; convert to table.el table
904 (let ((beg (copy-marker (org-table-begin)))
905 (end (copy-marker (org-table-end))))
906 ;; first, get rid of all horizontal lines
907 (goto-char beg)
908 (while (re-search-forward "^\\([ \t]*\\)|[-~:].*\n" end t)
909 (replace-match ""))
910 ;; insert a hline before first
911 (goto-char beg)
912 (org-table-insert-hline 'above)
913 (beginning-of-line -1)
914 ;; insert a hline after each line
915 (while (progn (beginning-of-line 3) (< (point) end))
916 (org-table-insert-hline))
917 (goto-char beg)
918 (setq end (move-marker end (org-table-end)))
919 ;; replace "+" at beginning and ending of hlines
920 (while (re-search-forward "^\\([ \t]*\\)|\\([-~:]\\)" end t)
921 (replace-match "\\1+\\2"))
922 (goto-char beg)
923 (while (re-search-forward "\\([-~:]\\)|[ \t]*$" end t)
924 (replace-match "\\1+"))
925 (goto-char beg)))))
928 ;;; Navigation and Structure Editing
930 ;;;###autoload
931 (defun org-table-begin (&optional table-type)
932 "Find the beginning of the table and return its position.
933 With a non-nil optional argument TABLE-TYPE, return the beginning
934 of a table.el-type table. This function assumes point is on
935 a table."
936 (cond (table-type
937 (org-element-property :post-affiliated (org-element-at-point)))
938 ((save-excursion
939 (and (re-search-backward org-table-border-regexp nil t)
940 (line-beginning-position 2))))
941 (t (point-min))))
943 ;;;###autoload
944 (defun org-table-end (&optional table-type)
945 "Find the end of the table and return its position.
946 With a non-nil optional argument TABLE-TYPE, return the end of
947 a table.el-type table. This function assumes point is on
948 a table."
949 (save-excursion
950 (cond (table-type
951 (goto-char (org-element-property :end (org-element-at-point)))
952 (skip-chars-backward " \t\n")
953 (line-beginning-position 2))
954 ((re-search-forward org-table-border-regexp nil t)
955 (match-beginning 0))
956 ;; When the line right after the table is the last line in
957 ;; the buffer with trailing spaces but no final newline
958 ;; character, be sure to catch the correct ending at its
959 ;; beginning. In any other case, ending is expected to be
960 ;; at point max.
961 (t (goto-char (point-max))
962 (skip-chars-backward " \t")
963 (if (bolp) (point) (line-end-position))))))
965 ;;;###autoload
966 (defun org-table-next-field ()
967 "Go to the next field in the current table, creating new lines as needed.
968 Before doing so, re-align the table if necessary."
969 (interactive)
970 (org-table-maybe-eval-formula)
971 (org-table-maybe-recalculate-line)
972 (when (and org-table-automatic-realign
973 org-table-may-need-update)
974 (org-table-align))
975 (let ((end (org-table-end)))
976 (if (org-at-table-hline-p)
977 (end-of-line 1))
978 (condition-case nil
979 (progn
980 (re-search-forward "|" end)
981 (if (looking-at "[ \t]*$")
982 (re-search-forward "|" end))
983 (if (and (looking-at "-")
984 org-table-tab-jumps-over-hlines
985 (re-search-forward org-table-dataline-regexp end t))
986 (goto-char (match-beginning 1)))
987 (if (looking-at "[-~:]")
988 (progn
989 (beginning-of-line 0)
990 (org-table-insert-row 'below))
991 (if (looking-at " ") (forward-char 1))))
992 (error
993 (org-table-insert-row 'below)))))
995 ;;;###autoload
996 (defun org-table-previous-field ()
997 "Go to the previous field in the table.
998 Before doing so, re-align the table if necessary."
999 (interactive)
1000 (org-table-justify-field-maybe)
1001 (org-table-maybe-recalculate-line)
1002 (when (and org-table-automatic-realign
1003 org-table-may-need-update)
1004 (org-table-align))
1005 (when (org-at-table-hline-p)
1006 (end-of-line))
1007 (let ((start (org-table-begin))
1008 (origin (point)))
1009 (condition-case nil
1010 (progn
1011 (search-backward "|" start nil 2)
1012 (while (looking-at-p "|\\(?:-\\|[ \t]*$\\)")
1013 (search-backward "|" start)))
1014 (error
1015 (goto-char origin)
1016 (user-error "Cannot move to previous table field"))))
1017 (when (looking-at "| ?")
1018 (goto-char (match-end 0))))
1020 (defun org-table-beginning-of-field (&optional n)
1021 "Move to the beginning of the current table field.
1022 If already at or before the beginning, move to the beginning of the
1023 previous field.
1024 With numeric argument N, move N-1 fields backward first."
1025 (interactive "p")
1026 (let ((pos (point)))
1027 (while (> n 1)
1028 (setq n (1- n))
1029 (org-table-previous-field))
1030 (if (not (re-search-backward "|" (point-at-bol 0) t))
1031 (user-error "No more table fields before the current")
1032 (goto-char (match-end 0))
1033 (and (looking-at " ") (forward-char 1)))
1034 (when (>= (point) pos) (org-table-beginning-of-field 2))))
1036 (defun org-table-end-of-field (&optional n)
1037 "Move to the end of the current table field.
1038 If already at or after the end, move to the end of the next table field.
1039 With numeric argument N, move N-1 fields forward first."
1040 (interactive "p")
1041 (let ((pos (point)))
1042 (while (> n 1)
1043 (setq n (1- n))
1044 (org-table-next-field))
1045 (when (re-search-forward "|" (point-at-eol 1) t)
1046 (backward-char 1)
1047 (skip-chars-backward " ")
1048 (when (and (equal (char-before (point)) ?|) (equal (char-after (point)) ?\s))
1049 (forward-char 1)))
1050 (when (<= (point) pos) (org-table-end-of-field 2))))
1052 ;;;###autoload
1053 (defun org-table-next-row ()
1054 "Go to the next row (same column) in the current table.
1055 Before doing so, re-align the table if necessary."
1056 (interactive)
1057 (org-table-maybe-eval-formula)
1058 (org-table-maybe-recalculate-line)
1059 (if (and org-table-automatic-realign
1060 org-table-may-need-update)
1061 (org-table-align))
1062 (let ((col (org-table-current-column)))
1063 (beginning-of-line 2)
1064 (when (or (not (org-at-table-p))
1065 (org-at-table-hline-p))
1066 (beginning-of-line 0)
1067 (org-table-insert-row 'below))
1068 (org-table-goto-column col)
1069 (skip-chars-backward "^|\n\r")
1070 (when (looking-at " ") (forward-char))))
1072 (defun org-table-get (line column)
1073 "Get the field in table line LINE, column COLUMN.
1074 If LINE is larger than the number of data lines in the table, the function
1075 returns nil. However, if COLUMN is too large, we will simply return an
1076 empty string.
1077 If LINE is nil, use the current line.
1078 If COLUMN is nil, use the current column."
1079 (setq column (or column (org-table-current-column)))
1080 (save-excursion
1081 (and (or (not line) (org-table-goto-line line))
1082 (org-trim (org-table-get-field column)))))
1084 (defun org-table-put (line column value &optional align)
1085 "Put VALUE into line LINE, column COLUMN.
1086 When ALIGN is set, also realign the table."
1087 (setq column (or column (org-table-current-column)))
1088 (prog1 (save-excursion
1089 (and (or (not line) (org-table-goto-line line))
1090 (progn (org-table-goto-column column nil 'force) t)
1091 (org-table-get-field column value)))
1092 (and align (org-table-align))))
1094 (defun org-table-current-line ()
1095 "Return the index of the current data line."
1096 (let ((pos (point)) (end (org-table-end)) (cnt 0))
1097 (save-excursion
1098 (goto-char (org-table-begin))
1099 (while (and (re-search-forward org-table-dataline-regexp end t)
1100 (setq cnt (1+ cnt))
1101 (< (point-at-eol) pos))))
1102 cnt))
1104 (defun org-table-current-column ()
1105 "Return current column number."
1106 (interactive)
1107 (save-excursion
1108 (let ((pos (point)))
1109 (beginning-of-line)
1110 (if (not (search-forward "|" pos t)) 0
1111 (let ((column 1)
1112 (separator (if (org-at-table-hline-p) "[+|]" "|")))
1113 (while (re-search-forward separator pos t) (cl-incf column))
1114 column)))))
1116 (defun org-table-current-dline ()
1117 "Find out what table data line we are in.
1118 Only data lines count for this."
1119 (save-excursion
1120 (let ((c 0)
1121 (pos (line-beginning-position)))
1122 (goto-char (org-table-begin))
1123 (while (<= (point) pos)
1124 (when (looking-at org-table-dataline-regexp) (cl-incf c))
1125 (forward-line))
1126 c)))
1128 (defun org-table-goto-line (N)
1129 "Go to the Nth data line in the current table.
1130 Return t when the line exists, nil if it does not exist."
1131 (goto-char (org-table-begin))
1132 (let ((end (org-table-end)) (cnt 0))
1133 (while (and (re-search-forward org-table-dataline-regexp end t)
1134 (< (setq cnt (1+ cnt)) N)))
1135 (= cnt N)))
1137 ;;;###autoload
1138 (defun org-table-blank-field ()
1139 "Blank the current table field or active region."
1140 (interactive)
1141 (org-table-check-inside-data-field)
1142 (if (and (called-interactively-p 'any) (org-region-active-p))
1143 (let (org-table-clip)
1144 (org-table-cut-region (region-beginning) (region-end)))
1145 (skip-chars-backward "^|")
1146 (backward-char 1)
1147 (if (looking-at "|[^|\n]+")
1148 (let* ((pos (match-beginning 0))
1149 (match (match-string 0))
1150 (len (org-string-width match)))
1151 (replace-match (concat "|" (make-string (1- len) ?\ )))
1152 (goto-char (+ 2 pos))
1153 (substring match 1)))))
1155 (defun org-table-get-field (&optional n replace)
1156 "Return the value of the field in column N of current row.
1157 N defaults to current column. If REPLACE is a string, replace
1158 field with this value. The return value is always the old
1159 value."
1160 (when n (org-table-goto-column n))
1161 (skip-chars-backward "^|\n")
1162 (if (or (bolp) (looking-at-p "[ \t]*$"))
1163 ;; Before first column or after last one.
1165 (looking-at "[^|\r\n]*")
1166 (let* ((pos (match-beginning 0))
1167 (val (buffer-substring pos (match-end 0))))
1168 (when replace
1169 (org-table-with-shrunk-field
1170 (replace-match (if (equal replace "") " " replace) t t)))
1171 (goto-char (min (line-end-position) (1+ pos)))
1172 val)))
1174 ;;;###autoload
1175 (defun org-table-field-info (_arg)
1176 "Show info about the current field, and highlight any reference at point."
1177 (interactive "P")
1178 (unless (org-at-table-p) (user-error "Not at a table"))
1179 (org-table-analyze)
1180 (save-excursion
1181 (let* ((pos (point))
1182 (col (org-table-current-column))
1183 (cname (car (rassoc (int-to-string col) org-table-column-names)))
1184 (name (car (rassoc (list (count-lines org-table-current-begin-pos
1185 (line-beginning-position))
1186 col)
1187 org-table-named-field-locations)))
1188 (eql (org-table-expand-lhs-ranges
1189 (mapcar
1190 (lambda (e)
1191 (cons (org-table-formula-handle-first/last-rc (car e))
1192 (cdr e)))
1193 (org-table-get-stored-formulas))))
1194 (dline (org-table-current-dline))
1195 (ref (format "@%d$%d" dline col))
1196 (ref1 (org-table-convert-refs-to-an ref))
1197 ;; Prioritize field formulas over column formulas.
1198 (fequation (or (assoc name eql) (assoc ref eql)))
1199 (cequation (assoc (format "$%d" col) eql))
1200 (eqn (or fequation cequation)))
1201 (let ((p (and eqn (get-text-property 0 :orig-eqn (car eqn)))))
1202 (when p (setq eqn p)))
1203 (goto-char pos)
1204 (ignore-errors (org-table-show-reference 'local))
1205 (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
1206 dline col
1207 (if cname (concat " or $" cname) "")
1208 dline col ref1
1209 (if name (concat " or $" name) "")
1210 ;; FIXME: formula info not correct if special table line
1211 (if eqn
1212 (concat ", formula: "
1213 (org-table-formula-to-user
1214 (concat
1215 (if (or (string-prefix-p "$" (car eqn))
1216 (string-prefix-p "@" (car eqn)))
1218 "$")
1219 (car eqn) "=" (cdr eqn))))
1220 "")))))
1222 (defun org-table-goto-field (ref &optional create-column-p)
1223 "Move point to a specific field in the current table.
1225 REF is either the name of a field its absolute reference, as
1226 a string. No column is created unless CREATE-COLUMN-P is
1227 non-nil. If it is a function, it is called with the column
1228 number as its argument as is used as a predicate to know if the
1229 column can be created.
1231 This function assumes the table is already analyzed (i.e., using
1232 `org-table-analyze')."
1233 (let* ((coordinates
1234 (cond
1235 ((cdr (assoc ref org-table-named-field-locations)))
1236 ((string-match "\\`@\\([1-9][0-9]*\\)\\$\\([1-9][0-9]*\\)\\'" ref)
1237 (list (condition-case nil
1238 (aref org-table-dlines
1239 (string-to-number (match-string 1 ref)))
1240 (error (user-error "Invalid row number in %s" ref)))
1241 (string-to-number (match-string 2 ref))))
1242 (t (user-error "Unknown field: %s" ref))))
1243 (line (car coordinates))
1244 (column (nth 1 coordinates))
1245 (create-new-column (if (functionp create-column-p)
1246 (funcall create-column-p column)
1247 create-column-p)))
1248 (when coordinates
1249 (goto-char org-table-current-begin-pos)
1250 (forward-line line)
1251 (org-table-goto-column column nil create-new-column))))
1253 ;;;###autoload
1254 (defun org-table-goto-column (n &optional on-delim force)
1255 "Move the cursor to the Nth column in the current table line.
1256 With optional argument ON-DELIM, stop with point before the left delimiter
1257 of the field.
1258 If there are less than N fields, just go to after the last delimiter.
1259 However, when FORCE is non-nil, create new columns if necessary."
1260 (interactive "p")
1261 (beginning-of-line 1)
1262 (when (> n 0)
1263 (while (and (> (setq n (1- n)) -1)
1264 (or (search-forward "|" (point-at-eol) t)
1265 (and force
1266 (progn (end-of-line 1)
1267 (skip-chars-backward "^|")
1268 (insert " | ")
1269 t)))))
1270 (when (and force (not (looking-at ".*|")))
1271 (save-excursion (end-of-line 1) (insert " | ")))
1272 (if on-delim
1273 (backward-char 1)
1274 (if (looking-at " ") (forward-char 1)))))
1276 ;;;###autoload
1277 (defun org-table-insert-column ()
1278 "Insert a new column into the table."
1279 (interactive)
1280 (unless (org-at-table-p) (user-error "Not at a table"))
1281 (org-table-find-dataline)
1282 (let ((col (max 1 (org-table-current-column)))
1283 (beg (org-table-begin))
1284 (end (copy-marker (org-table-end)))
1285 (shrunk-columns (org-table--list-shrunk-columns)))
1286 (org-table-expand beg end)
1287 (save-excursion
1288 (goto-char beg)
1289 (while (< (point) end)
1290 (unless (org-at-table-hline-p)
1291 (org-table-goto-column col t)
1292 (unless (search-forward "|" (line-end-position) t 2)
1293 ;; Add missing vertical bar at the end of the row.
1294 (end-of-line)
1295 (insert "|"))
1296 (insert " |"))
1297 (forward-line)))
1298 (org-table-goto-column (1+ col))
1299 (org-table-align)
1300 ;; Shift appropriately stored shrunk column numbers, then hide the
1301 ;; columns again.
1302 (org-table--shrink-columns (mapcar (lambda (c) (if (<= c col) c (1+ c)))
1303 shrunk-columns)
1304 beg end)
1305 (set-marker end nil)
1306 ;; Fix TBLFM formulas, if desirable.
1307 (when (or (not org-table-fix-formulas-confirm)
1308 (funcall org-table-fix-formulas-confirm "Fix formulas? "))
1309 (org-table-fix-formulas "$" nil (1- col) 1)
1310 (org-table-fix-formulas "$LR" nil (1- col) 1))))
1312 (defun org-table-find-dataline ()
1313 "Find a data line in the current table, which is needed for column commands.
1314 This function assumes point is in a table. Raise an error when
1315 there is no data row below."
1316 (or (not (org-at-table-hline-p))
1317 (let ((col (current-column))
1318 (end (org-table-end)))
1319 (forward-line)
1320 (while (and (< (point) end) (org-at-table-hline-p))
1321 (forward-line))
1322 (when (>= (point) end)
1323 (user-error "Cannot find data row for column operation"))
1324 (org-move-to-column col)
1325 t)))
1327 (defun org-table-line-to-dline (line &optional above)
1328 "Turn a buffer line number into a data line number.
1330 If there is no data line in this line, return nil.
1332 If there is no matching dline (most likely the reference was
1333 a hline), the first dline below it is used. When ABOVE is
1334 non-nil, the one above is used."
1335 (let ((min 1)
1336 (max (1- (length org-table-dlines))))
1337 (cond ((or (> (aref org-table-dlines min) line)
1338 (< (aref org-table-dlines max) line))
1339 nil)
1340 ((= line (aref org-table-dlines max)) max)
1341 (t (catch 'exit
1342 (while (> (- max min) 1)
1343 (let* ((mean (/ (+ max min) 2))
1344 (v (aref org-table-dlines mean)))
1345 (cond ((= v line) (throw 'exit mean))
1346 ((> v line) (setq max mean))
1347 (t (setq min mean)))))
1348 (cond ((= line (aref org-table-dlines max)) max)
1349 ((= line (aref org-table-dlines min)) min)
1350 (above min)
1351 (t max)))))))
1353 (defun org-table--swap-cells (row1 col1 row2 col2)
1354 "Swap two cells indicated by the coordinates provided.
1355 ROW1, COL1, ROW2, COL2 are integers indicating the row/column
1356 position of the two cells that will be swapped in the table."
1357 (let ((content1 (org-table-get row1 col1))
1358 (content2 (org-table-get row2 col2)))
1359 (org-table-put row1 col1 content2)
1360 (org-table-put row2 col2 content1)))
1362 (defun org-table--move-cell (direction)
1363 "Move the current cell in a cardinal direction.
1364 DIRECTION is a symbol among `up', `down', `left', and `right'.
1365 The contents the current cell are swapped with cell in the
1366 indicated direction. Raise an error if the move cannot be done."
1367 (let ((row-shift (pcase direction (`up -1) (`down 1) (_ 0)))
1368 (column-shift (pcase direction (`left -1) (`right 1) (_ 0))))
1369 (when (and (= 0 row-shift) (= 0 column-shift))
1370 (error "Invalid direction: %S" direction))
1371 ;; Initialize `org-table-current-ncol' and `org-table-dlines'.
1372 (org-table-analyze)
1373 (let* ((row (org-table-current-line))
1374 (column (org-table-current-column))
1375 (target-row (+ row row-shift))
1376 (target-column (+ column column-shift))
1377 (org-table-current-nrow (1- (length org-table-dlines))))
1378 (when (or (< target-column 1)
1379 (< target-row 1)
1380 (> target-column org-table-current-ncol)
1381 (> target-row org-table-current-nrow))
1382 (user-error "Cannot move cell further"))
1383 (org-table--swap-cells row column target-row target-column)
1384 (org-table-goto-line target-row)
1385 (org-table-goto-column target-column))))
1387 ;;;###autoload
1388 (defun org-table-move-cell-up ()
1389 "Move a single cell up in a table.
1390 Swap with anything in target cell."
1391 (interactive)
1392 (unless (org-table-check-inside-data-field)
1393 (error "No table at point"))
1394 (org-table--move-cell 'up)
1395 (org-table-align))
1397 ;;;###autoload
1398 (defun org-table-move-cell-down ()
1399 "Move a single cell down in a table.
1400 Swap with anything in target cell."
1401 (interactive)
1402 (unless (org-table-check-inside-data-field)
1403 (error "No table at point"))
1404 (org-table--move-cell 'down)
1405 (org-table-align))
1407 ;;;###autoload
1408 (defun org-table-move-cell-left ()
1409 "Move a single cell left in a table.
1410 Swap with anything in target cell."
1411 (interactive)
1412 (unless (org-table-check-inside-data-field)
1413 (error "No table at point"))
1414 (org-table--move-cell 'left)
1415 (org-table-align))
1417 ;;;###autoload
1418 (defun org-table-move-cell-right ()
1419 "Move a single cell right in a table.
1420 Swap with anything in target cell."
1421 (interactive)
1422 (unless (org-table-check-inside-data-field)
1423 (error "No table at point"))
1424 (org-table--move-cell 'right)
1425 (org-table-align))
1427 ;;;###autoload
1428 (defun org-table-delete-column ()
1429 "Delete a column from the table."
1430 (interactive)
1431 (unless (org-at-table-p) (user-error "Not at a table"))
1432 (org-table-find-dataline)
1433 (org-table-check-inside-data-field nil t)
1434 (let* ((col (org-table-current-column))
1435 (beg (org-table-begin))
1436 (end (copy-marker (org-table-end)))
1437 (shrunk-columns (remq col (org-table--list-shrunk-columns))))
1438 (org-table-expand beg end)
1439 (org-table-save-field
1440 (goto-char beg)
1441 (while (< (point) end)
1442 (if (org-at-table-hline-p)
1444 (org-table-goto-column col t)
1445 (and (looking-at "|[^|\n]+|")
1446 (replace-match "|")))
1447 (forward-line)))
1448 (org-table-goto-column (max 1 (1- col)))
1449 (org-table-align)
1450 ;; Shift appropriately stored shrunk column numbers, then hide the
1451 ;; columns again.
1452 (org-table--shrink-columns (mapcar (lambda (c) (if (< c col) c (1- c)))
1453 shrunk-columns)
1454 beg end)
1455 (set-marker end nil)
1456 ;; Fix TBLFM formulas, if desirable.
1457 (when (or (not org-table-fix-formulas-confirm)
1458 (funcall org-table-fix-formulas-confirm "Fix formulas? "))
1459 (org-table-fix-formulas
1460 "$" (list (cons (number-to-string col) "INVALID")) col -1 col)
1461 (org-table-fix-formulas
1462 "$LR" (list (cons (number-to-string col) "INVALID")) col -1 col))))
1464 ;;;###autoload
1465 (defun org-table-move-column-right ()
1466 "Move column to the right."
1467 (interactive)
1468 (org-table-move-column nil))
1470 ;;;###autoload
1471 (defun org-table-move-column-left ()
1472 "Move column to the left."
1473 (interactive)
1474 (org-table-move-column 'left))
1476 ;;;###autoload
1477 (defun org-table-move-column (&optional left)
1478 "Move the current column to the right. With arg LEFT, move to the left."
1479 (interactive "P")
1480 (unless (org-at-table-p) (user-error "Not at a table"))
1481 (org-table-find-dataline)
1482 (org-table-check-inside-data-field nil t)
1483 (let* ((col (org-table-current-column))
1484 (col1 (if left (1- col) col))
1485 (colpos (if left (1- col) (1+ col)))
1486 (beg (org-table-begin))
1487 (end (copy-marker (org-table-end))))
1488 (when (and left (= col 1))
1489 (user-error "Cannot move column further left"))
1490 (when (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
1491 (user-error "Cannot move column further right"))
1492 (let ((shrunk-columns (org-table--list-shrunk-columns)))
1493 (org-table-expand beg end)
1494 (org-table-save-field
1495 (goto-char beg)
1496 (while (< (point) end)
1497 (unless (org-at-table-hline-p)
1498 (org-table-goto-column col1 t)
1499 (when (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
1500 (transpose-regions
1501 (match-beginning 1) (match-end 1)
1502 (match-beginning 2) (match-end 2))))
1503 (forward-line)))
1504 (org-table-goto-column colpos)
1505 (org-table-align)
1506 ;; Shift appropriately stored shrunk column numbers, then shrink
1507 ;; the columns again.
1508 (org-table--shrink-columns
1509 (mapcar (lambda (c)
1510 (cond ((and (= col c) left) (1- c))
1511 ((= col c) (1+ c))
1512 ((and (= col (1+ c)) left) (1+ c))
1513 ((and (= col (1- c)) (not left) (1- c)))
1514 (t c)))
1515 shrunk-columns)
1516 beg end)
1517 (set-marker end nil)
1518 ;; Fix TBLFM formulas, if desirable.
1519 (when (or (not org-table-fix-formulas-confirm)
1520 (funcall org-table-fix-formulas-confirm "Fix formulas? "))
1521 (org-table-fix-formulas
1522 "$" (list (cons (number-to-string col) (number-to-string colpos))
1523 (cons (number-to-string colpos) (number-to-string col))))
1524 (org-table-fix-formulas
1525 "$LR" (list
1526 (cons (number-to-string col) (number-to-string colpos))
1527 (cons (number-to-string colpos) (number-to-string col))))))))
1529 ;;;###autoload
1530 (defun org-table-move-row-down ()
1531 "Move table row down."
1532 (interactive)
1533 (org-table-move-row nil))
1535 ;;;###autoload
1536 (defun org-table-move-row-up ()
1537 "Move table row up."
1538 (interactive)
1539 (org-table-move-row 'up))
1541 ;;;###autoload
1542 (defun org-table-move-row (&optional up)
1543 "Move the current table line down. With arg UP, move it up."
1544 (interactive "P")
1545 (let* ((col (current-column))
1546 (pos (point))
1547 (hline1p (save-excursion (beginning-of-line 1)
1548 (looking-at org-table-hline-regexp)))
1549 (dline1 (org-table-current-dline))
1550 (dline2 (+ dline1 (if up -1 1)))
1551 (tonew (if up 0 2))
1552 hline2p)
1553 (when (and up (= (point-min) (line-beginning-position)))
1554 (user-error "Cannot move row further"))
1555 (beginning-of-line tonew)
1556 (when (or (and (not up) (eobp)) (not (org-at-table-p)))
1557 (goto-char pos)
1558 (user-error "Cannot move row further"))
1559 (org-table-with-shrunk-columns
1560 (setq hline2p (looking-at org-table-hline-regexp))
1561 (goto-char pos)
1562 (let ((row (delete-and-extract-region (line-beginning-position)
1563 (line-beginning-position 2))))
1564 (beginning-of-line tonew)
1565 (unless (bolp) (insert "\n")) ;at eob without a newline
1566 (insert row)
1567 (unless (bolp) (insert "\n")) ;missing final newline in ROW
1568 (beginning-of-line 0)
1569 (org-move-to-column col)
1570 (unless (or hline1p hline2p
1571 (not (or (not org-table-fix-formulas-confirm)
1572 (funcall org-table-fix-formulas-confirm
1573 "Fix formulas? "))))
1574 (org-table-fix-formulas
1575 "@" (list
1576 (cons (number-to-string dline1) (number-to-string dline2))
1577 (cons (number-to-string dline2) (number-to-string dline1)))))))))
1579 ;;;###autoload
1580 (defun org-table-insert-row (&optional arg)
1581 "Insert a new row above the current line into the table.
1582 With prefix ARG, insert below the current line."
1583 (interactive "P")
1584 (unless (org-at-table-p) (user-error "Not at a table"))
1585 (org-table-with-shrunk-columns
1586 (let* ((line (buffer-substring (line-beginning-position) (line-end-position)))
1587 (new (org-table-clean-line line)))
1588 ;; Fix the first field if necessary
1589 (when (string-match "^[ \t]*| *[#*$] *|" line)
1590 (setq new (replace-match (match-string 0 line) t t new)))
1591 (beginning-of-line (if arg 2 1))
1592 ;; Buffer may not end of a newline character, so ensure
1593 ;; (beginning-of-line 2) moves point to a new line.
1594 (unless (bolp) (insert "\n"))
1595 (let (org-table-may-need-update) (insert-before-markers new "\n"))
1596 (beginning-of-line 0)
1597 (re-search-forward "| ?" (line-end-position) t)
1598 (when (or org-table-may-need-update org-table-overlay-coordinates)
1599 (org-table-align))
1600 (when (or (not org-table-fix-formulas-confirm)
1601 (funcall org-table-fix-formulas-confirm "Fix formulas? "))
1602 (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))))
1604 ;;;###autoload
1605 (defun org-table-insert-hline (&optional above)
1606 "Insert a horizontal-header-line below the current line into the table.
1607 With prefix ABOVE, insert above the current line."
1608 (interactive "P")
1609 (org-table-insert-line ?- above))
1611 ;;;###autoload
1612 (defun org-table-insert-hhline (&optional above)
1613 "Insert a horizontal-header-line below the current line into the table.
1614 With prefix ABOVE, insert above the current line."
1615 (interactive "P")
1616 (org-table-insert-line ?~ above))
1618 ;;;###autoload
1619 (defun org-table-insert-hcline (&optional above)
1620 "Insert a horizontal-calc-line below the current line into the table.
1621 With prefix ABOVE, insert above the current line."
1622 (interactive "P")
1623 (org-table-insert-line ?: above))
1625 (defun org-table-insert-line (ichar &optional above)
1626 "Insert a horizontal-line below the current line into the table.
1627 With prefix ABOVE, insert above the current line."
1628 (interactive "P")
1629 (unless (org-at-table-p) (user-error "Not at a table"))
1630 (when (eobp) (save-excursion (insert "\n")))
1631 (unless (string-match-p "|[ \t]*$" (org-current-line-string))
1632 (org-table-align))
1633 (org-table-with-shrunk-columns
1634 (let ((line (org-table-clean-line
1635 (buffer-substring (point-at-bol) (point-at-eol))))
1636 (col (current-column)))
1637 (while (string-match "|\\( +\\)|" line)
1638 (setq line (replace-match
1639 (concat "+" (make-string (- (match-end 1) (match-beginning 1))
1640 ichar) "|") t t line)))
1641 (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
1642 (beginning-of-line (if above 1 2))
1643 (insert line "\n")
1644 (beginning-of-line (if above 1 -1))
1645 (org-move-to-column col)
1646 (when org-table-overlay-coordinates (org-table-align)))))
1648 ;;;###autoload
1649 (defun org-table-hline-and-move (&optional same-column)
1650 "Insert a hline and move to the row below that line."
1651 (interactive "P")
1652 (let ((col (org-table-current-column)))
1653 (org-table-maybe-eval-formula)
1654 (org-table-maybe-recalculate-line)
1655 (org-table-insert-hline)
1656 (end-of-line 2)
1657 (if (looking-at "\n[ \t]*|[-~:]")
1658 (progn (insert "\n|") (org-table-align))
1659 (org-table-next-field))
1660 (if same-column (org-table-goto-column col))))
1662 (defun org-table-clean-line (s)
1663 "Convert a table line S into a string with only \"|\" and space.
1664 In particular, this does handle wide and invisible characters."
1665 (if (string-match org-table-hline-regexp s)
1666 ;; It's a hline, just map the characters
1667 (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
1668 (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
1669 (setq s (replace-match
1670 (concat "|" (make-string (org-string-width (match-string 1 s))
1671 ?\ ) "|")
1672 t t s)))
1675 ;;;###autoload
1676 (defun org-table-kill-row ()
1677 "Delete the current row or horizontal line from the table."
1678 (interactive)
1679 (unless (org-at-table-p) (user-error "Not at a table"))
1680 (let ((col (current-column))
1681 (dline (and (not (org-match-line org-table-hline-regexp))
1682 (org-table-current-dline))))
1683 (org-table-with-shrunk-columns
1684 (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
1685 (if (not (org-at-table-p)) (beginning-of-line 0))
1686 (org-move-to-column col)
1687 (when (and dline
1688 (or (not org-table-fix-formulas-confirm)
1689 (funcall org-table-fix-formulas-confirm "Fix formulas? ")))
1690 (org-table-fix-formulas
1691 "@" (list (cons (number-to-string dline) "INVALID")) dline -1 dline)))))
1693 ;;;###autoload
1694 (defun org-table-cut-region (beg end)
1695 "Copy region in table to the clipboard and blank all relevant fields.
1696 If there is no active region, use just the field at point."
1697 (interactive (list
1698 (if (org-region-active-p) (region-beginning) (point))
1699 (if (org-region-active-p) (region-end) (point))))
1700 (org-table-copy-region beg end 'cut))
1702 ;;;###autoload
1703 (defun org-table-copy-down (n)
1704 "Copy the value of the current field one row below.
1706 If the field at the cursor is empty, copy the content of the
1707 nearest non-empty field above. With argument N, use the Nth
1708 non-empty field.
1710 If the current field is not empty, it is copied down to the next
1711 row, and the cursor is moved with it. Therefore, repeating this
1712 command causes the column to be filled row-by-row.
1714 If the variable `org-table-copy-increment' is non-nil and the
1715 field is an integer or a timestamp, it will be incremented while
1716 copying. By default, increment by the difference between the
1717 value in the current field and the one in the field above. To
1718 increment using a fixed integer, set `org-table-copy-increment'
1719 to a number. In the case of a timestamp, increment by days."
1720 (interactive "p")
1721 (let* ((colpos (org-table-current-column))
1722 (col (current-column))
1723 (field (save-excursion (org-table-get-field)))
1724 (field-up (or (save-excursion
1725 (org-table-get (1- (org-table-current-line))
1726 (org-table-current-column))) ""))
1727 (non-empty (string-match "[^ \t]" field))
1728 (non-empty-up (string-match "[^ \t]" field-up))
1729 (beg (org-table-begin))
1730 (orig-n n)
1731 txt txt-up inc)
1732 (org-table-check-inside-data-field)
1733 (if (not non-empty)
1734 (save-excursion
1735 (setq txt
1736 (catch 'exit
1737 (while (progn (beginning-of-line 1)
1738 (re-search-backward org-table-dataline-regexp
1739 beg t))
1740 (org-table-goto-column colpos t)
1741 (if (and (looking-at
1742 "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
1743 (<= (setq n (1- n)) 0))
1744 (throw 'exit (match-string 1))))))
1745 (setq field-up
1746 (catch 'exit
1747 (while (progn (beginning-of-line 1)
1748 (re-search-backward org-table-dataline-regexp
1749 beg t))
1750 (org-table-goto-column colpos t)
1751 (if (and (looking-at
1752 "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
1753 (<= (setq n (1- n)) 0))
1754 (throw 'exit (match-string 1))))))
1755 (setq non-empty-up (and field-up (string-match "[^ \t]" field-up))))
1756 ;; Above field was not empty, go down to the next row. Skip
1757 ;; alignment since we do it at the end of the process anyway.
1758 (setq txt (org-trim field))
1759 (let ((org-table-may-need-update nil)) (org-table-next-row))
1760 (org-table-blank-field))
1761 (if non-empty-up (setq txt-up (org-trim field-up)))
1762 (setq inc (cond
1763 ((numberp org-table-copy-increment) org-table-copy-increment)
1764 (txt-up (cond ((and (string-match org-ts-regexp3 txt-up)
1765 (string-match org-ts-regexp3 txt))
1766 (- (org-time-string-to-absolute txt)
1767 (org-time-string-to-absolute txt-up)))
1768 ((string-match org-ts-regexp3 txt) 1)
1769 ((string-match "\\([-+]\\)?[0-9]*\\(?:\\.[0-9]+\\)?" txt-up)
1770 (- (string-to-number txt)
1771 (string-to-number (match-string 0 txt-up))))
1772 (t 1)))
1773 (t 1)))
1774 (if (not txt)
1775 (user-error "No non-empty field found")
1776 (if (and org-table-copy-increment
1777 (not (equal orig-n 0))
1778 (string-match-p "^[-+^/*0-9eE.]+$" txt)
1779 (< (string-to-number txt) 100000000))
1780 (setq txt (calc-eval (concat txt "+" (number-to-string inc)))))
1781 (insert txt)
1782 (org-move-to-column col)
1783 (if (and org-table-copy-increment (org-at-timestamp-p 'lax))
1784 (org-timestamp-up-day inc)
1785 (org-table-maybe-recalculate-line))
1786 (org-table-align)
1787 (org-move-to-column col))))
1789 ;;;###autoload
1790 (defun org-table-copy-region (beg end &optional cut)
1791 "Copy rectangular region in table to clipboard.
1792 A special clipboard is used which can only be accessed with
1793 `org-table-paste-rectangle'. Return the region copied, as a list
1794 of lists of fields."
1795 (interactive (list
1796 (if (org-region-active-p) (region-beginning) (point))
1797 (if (org-region-active-p) (region-end) (point))
1798 current-prefix-arg))
1799 (goto-char (min beg end))
1800 (org-table-check-inside-data-field)
1801 (let ((beg (line-beginning-position))
1802 (c01 (org-table-current-column))
1803 region)
1804 (goto-char (max beg end))
1805 (org-table-check-inside-data-field nil t)
1806 (let* ((end (copy-marker (line-end-position)))
1807 (c02 (org-table-current-column))
1808 (column-start (min c01 c02))
1809 (column-end (max c01 c02))
1810 (column-number (1+ (- column-end column-start)))
1811 (rpl (and cut " ")))
1812 (goto-char beg)
1813 (while (< (point) end)
1814 (unless (org-at-table-hline-p)
1815 ;; Collect every cell between COLUMN-START and COLUMN-END.
1816 (let (cols)
1817 (dotimes (c column-number)
1818 (push (org-table-get-field (+ c column-start) rpl) cols))
1819 (push (nreverse cols) region)))
1820 (forward-line))
1821 (set-marker end nil))
1822 (when cut (org-table-align))
1823 (message (substitute-command-keys "Cells in the region copied, use \
1824 \\[org-table-paste-rectangle] to paste them in a table."))
1825 (setq org-table-clip (nreverse region))))
1827 ;;;###autoload
1828 (defun org-table-paste-rectangle ()
1829 "Paste a rectangular region into a table.
1830 The upper right corner ends up in the current field. All involved fields
1831 will be overwritten. If the rectangle does not fit into the present table,
1832 the table is enlarged as needed. The process ignores horizontal separator
1833 lines."
1834 (interactive)
1835 (unless (consp org-table-clip)
1836 (user-error "First cut/copy a region to paste!"))
1837 (org-table-check-inside-data-field)
1838 (let* ((column (org-table-current-column))
1839 (org-table-automatic-realign nil))
1840 (org-table-save-field
1841 (dolist (row org-table-clip)
1842 (while (org-at-table-hline-p) (forward-line))
1843 ;; If we left the table, create a new row.
1844 (when (and (bolp) (not (looking-at "[ \t]*|")))
1845 (end-of-line 0)
1846 (org-table-next-field))
1847 (let ((c column))
1848 (dolist (field row)
1849 (org-table-goto-column c nil 'force)
1850 (org-table-get-field nil field)
1851 (cl-incf c)))
1852 (forward-line)))
1853 (org-table-align)))
1856 ;;; Follow Field minor mode
1858 (define-minor-mode org-table-follow-field-mode
1859 "Minor mode to make the table field editor window follow the cursor.
1860 When this mode is active, the field editor window will always show the
1861 current field. The mode exits automatically when the cursor leaves the
1862 table (but see `org-table-exit-follow-field-mode-when-leaving-table')."
1863 nil " TblFollow" nil
1864 (if org-table-follow-field-mode
1865 (add-hook 'post-command-hook 'org-table-follow-fields-with-editor
1866 'append 'local)
1867 (remove-hook 'post-command-hook 'org-table-follow-fields-with-editor 'local)
1868 (let* ((buf (get-buffer "*Org Table Edit Field*"))
1869 (win (and buf (get-buffer-window buf))))
1870 (when win (delete-window win))
1871 (when buf
1872 (with-current-buffer buf
1873 (move-marker org-field-marker nil))
1874 (kill-buffer buf)))))
1876 ;;;###autoload
1877 (defun org-table-edit-field (arg)
1878 "Edit table field in a different window.
1879 This is mainly useful for fields that contain hidden parts.
1881 When called with a `\\[universal-argument]' prefix, just make the full field
1882 visible so that it can be edited in place.
1884 When called with a `\\[universal-argument] \\[universal-argument]' prefix, \
1885 toggle `org-table-follow-field-mode'."
1886 (interactive "P")
1887 (unless (org-at-table-p) (user-error "Not at a table"))
1888 (cond
1889 ((equal arg '(16))
1890 (org-table-follow-field-mode (if org-table-follow-field-mode -1 1)))
1891 (arg
1892 (let ((b (save-excursion (skip-chars-backward "^|") (point)))
1893 (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
1894 (remove-text-properties b e '(invisible t intangible t))
1895 (if (and (boundp 'font-lock-mode) font-lock-mode)
1896 (font-lock-fontify-block))))
1898 (let ((pos (point-marker))
1899 (coord
1900 (if (eq org-table-use-standard-references t)
1901 (concat (org-number-to-letters (org-table-current-column))
1902 (int-to-string (org-table-current-dline)))
1903 (concat "@" (int-to-string (org-table-current-dline))
1904 "$" (int-to-string (org-table-current-column)))))
1905 (field (org-table-get-field))
1906 (cw (current-window-configuration))
1908 (goto-char pos)
1909 (org-switch-to-buffer-other-window "*Org Table Edit Field*")
1910 (when (and (local-variable-p 'org-field-marker)
1911 (markerp org-field-marker))
1912 (move-marker org-field-marker nil))
1913 (erase-buffer)
1914 (insert "#\n# Edit field " coord " and finish with C-c C-c\n#\n")
1915 (let ((org-inhibit-startup t)) (org-mode))
1916 (auto-fill-mode -1)
1917 (setq truncate-lines nil)
1918 (setq word-wrap t)
1919 (goto-char (setq p (point-max)))
1920 (insert (org-trim field))
1921 (remove-text-properties p (point-max) '(invisible t intangible t))
1922 (goto-char p)
1923 (setq-local org-finish-function 'org-table-finish-edit-field)
1924 (setq-local org-window-configuration cw)
1925 (setq-local org-field-marker pos)
1926 (message "Edit and finish with C-c C-c")))))
1928 (defun org-table-follow-fields-with-editor ()
1929 (if (and org-table-exit-follow-field-mode-when-leaving-table
1930 (not (org-at-table-p)))
1931 ;; We have left the table, exit the follow mode
1932 (org-table-follow-field-mode -1)
1933 (when (org-table-check-inside-data-field 'noerror)
1934 (let ((win (selected-window)))
1935 (org-table-edit-field nil)
1936 (org-fit-window-to-buffer)
1937 (select-window win)))))
1939 (defun org-table-finish-edit-field ()
1940 "Finish editing a table data field.
1941 Remove all newline characters, insert the result into the table, realign
1942 the table and kill the editing buffer."
1943 (let ((pos org-field-marker)
1944 (cw org-window-configuration)
1945 (cb (current-buffer))
1946 text)
1947 (goto-char (point-min))
1948 (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
1949 (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
1950 (replace-match " "))
1951 (setq text (org-trim (buffer-string)))
1952 (set-window-configuration cw)
1953 (kill-buffer cb)
1954 (select-window (get-buffer-window (marker-buffer pos)))
1955 (goto-char pos)
1956 (move-marker pos nil)
1957 (org-table-check-inside-data-field)
1958 (org-table-get-field nil text)
1959 (org-table-align)
1960 (message "New field value inserted")))
1963 ;;; Formulas
1965 (defun org-table-current-field-formula (&optional key noerror)
1966 "Return the formula active for the current field.
1968 Assumes that table is already analyzed. If KEY is given, return
1969 the key to this formula. Otherwise return the formula preceded
1970 with \"=\" or \":=\"."
1971 (let* ((line (count-lines org-table-current-begin-pos
1972 (line-beginning-position)))
1973 (row (org-table-line-to-dline line)))
1974 (cond
1975 (row
1976 (let* ((col (org-table-current-column))
1977 (name (car (rassoc (list line col)
1978 org-table-named-field-locations)))
1979 (scol (format "$%d" col))
1980 (ref (format "@%d$%d" (org-table-current-dline) col))
1981 (stored-list (org-table-get-stored-formulas noerror))
1982 (ass (or (assoc name stored-list)
1983 (assoc ref stored-list)
1984 (assoc scol stored-list))))
1985 (cond (key (car ass))
1986 (ass (concat (if (string-match-p "^[0-9]+$" (car ass)) "=" ":=")
1987 (cdr ass))))))
1988 (noerror nil)
1989 (t (error "No formula active for the current field")))))
1991 (defun org-table-get-formula (&optional equation named)
1992 "Read a formula from the minibuffer, offer stored formula as default.
1993 When NAMED is non-nil, look for a named equation."
1994 (let* ((stored-list (org-table-get-stored-formulas))
1995 (name (car (rassoc (list (count-lines org-table-current-begin-pos
1996 (line-beginning-position))
1997 (org-table-current-column))
1998 org-table-named-field-locations)))
1999 (ref (format "@%d$%d"
2000 (org-table-current-dline)
2001 (org-table-current-column)))
2002 (scol (cond
2003 ((not named) (format "$%d" (org-table-current-column)))
2004 ((and name (not (string-match "\\`LR[0-9]+\\'" name))) name)
2005 (t ref)))
2006 (name (or name ref))
2007 (org-table-may-need-update nil)
2008 (stored (cdr (assoc scol stored-list)))
2009 (eq (cond
2010 ((and stored equation (string-match-p "^ *=? *$" equation))
2011 stored)
2012 ((stringp equation) equation)
2014 (org-table-formula-from-user
2015 (read-string
2016 (org-table-formula-to-user
2017 (format "%s formula %s=" (if named "Field" "Column") scol))
2018 (if stored (org-table-formula-to-user stored) "")
2019 'org-table-formula-history)))))
2020 mustsave)
2021 (unless (org-string-nw-p eq)
2022 ;; Remove formula.
2023 (setq stored-list (delq (assoc scol stored-list) stored-list))
2024 (org-table-store-formulas stored-list)
2025 (user-error "Formula removed"))
2026 (when (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
2027 (when (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
2028 (when (and name (not named))
2029 ;; We set the column equation, delete the named one.
2030 (setq stored-list (delq (assoc name stored-list) stored-list)
2031 mustsave t))
2032 (if stored
2033 (setcdr (assoc scol stored-list) eq)
2034 (setq stored-list (cons (cons scol eq) stored-list)))
2035 (when (or mustsave (not (equal stored eq)))
2036 (org-table-store-formulas stored-list))
2037 eq))
2039 (defun org-table-store-formulas (alist &optional location)
2040 "Store the list of formulas below the current table.
2041 If optional argument LOCATION is a buffer position, insert it at
2042 LOCATION instead."
2043 (save-excursion
2044 (if location
2045 (progn (goto-char location) (beginning-of-line))
2046 (goto-char (org-table-end)))
2047 (let ((case-fold-search t))
2048 (if (looking-at "\\([ \t]*\n\\)*[ \t]*\\(#\\+TBLFM:\\)\\(.*\n?\\)")
2049 (progn
2050 ;; Don't overwrite TBLFM, we might use text properties to
2051 ;; store stuff.
2052 (goto-char (match-beginning 3))
2053 (delete-region (match-beginning 3) (match-end 0)))
2054 (org-indent-line)
2055 (insert (or (match-string 2) "#+TBLFM:")))
2056 (insert " "
2057 (mapconcat (lambda (x) (concat (car x) "=" (cdr x)))
2058 (sort alist #'org-table-formula-less-p)
2059 "::")
2060 "\n"))))
2062 (defsubst org-table-formula-make-cmp-string (a)
2063 (when (string-match "\\`\\$[<>]" a)
2064 (let ((arrow (string-to-char (substring a 1))))
2065 ;; Fake a high number to make sure this is sorted at the end.
2066 (setq a (org-table-formula-handle-first/last-rc a))
2067 (setq a (format "$%d" (+ 10000
2068 (if (= arrow ?<) -1000 0)
2069 (string-to-number (substring a 1)))))))
2070 (when (string-match
2071 "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?"
2073 (concat
2074 (if (match-end 2)
2075 (format "@%05d" (string-to-number (match-string 2 a))) "")
2076 (if (match-end 4)
2077 (format "$%05d" (string-to-number (match-string 4 a))) "")
2078 (if (match-end 5)
2079 (concat "@@" (match-string 5 a))))))
2081 (defun org-table-formula-less-p (a b)
2082 "Compare two formulas for sorting."
2083 (let ((as (org-table-formula-make-cmp-string (car a)))
2084 (bs (org-table-formula-make-cmp-string (car b))))
2085 (and as bs (string< as bs))))
2087 ;;;###autoload
2088 (defun org-table-get-stored-formulas (&optional noerror location)
2089 "Return an alist with the stored formulas directly after current table.
2090 By default, only return active formulas, i.e., formulas located
2091 on the first line after the table. However, if optional argument
2092 LOCATION is a buffer position, consider the formulas there."
2093 (save-excursion
2094 (if location
2095 (progn (goto-char location) (beginning-of-line))
2096 (goto-char (org-table-end)))
2097 (let ((case-fold-search t))
2098 (when (looking-at "\\([ \t]*\n\\)*[ \t]*#\\+TBLFM: *\\(.*\\)")
2099 (let ((strings (org-split-string (match-string-no-properties 2)
2100 " *:: *"))
2101 eq-alist seen)
2102 (dolist (string strings (nreverse eq-alist))
2103 (when (string-match "\\`\\(@[-+I<>0-9.$@]+\\|\\$\\([_a-zA-Z0-9]+\\|\
2104 \[<>]+\\)\\) *= *\\(.*[^ \t]\\)"
2105 string)
2106 (let ((lhs
2107 (let ((m (match-string 1 string)))
2108 (cond
2109 ((not (match-end 2)) m)
2110 ;; Is it a column reference?
2111 ((string-match-p "\\`\\$\\([0-9]+\\|[<>]+\\)\\'" m) m)
2112 ;; Since named columns are not possible in
2113 ;; LHS, assume this is a named field.
2114 (t (match-string 2 string)))))
2115 (rhs (match-string 3 string)))
2116 (push (cons lhs rhs) eq-alist)
2117 (cond
2118 ((not (member lhs seen)) (push lhs seen))
2119 (noerror
2120 (message
2121 "Double definition `%s=' in TBLFM line, please fix by hand"
2122 lhs)
2123 (ding)
2124 (sit-for 2))
2126 (user-error
2127 "Double definition `%s=' in TBLFM line, please fix by hand"
2128 lhs)))))))))))
2130 (defun org-table-fix-formulas (key replace &optional limit delta remove)
2131 "Modify the equations after the table structure has been edited.
2132 KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
2133 For all numbers larger than LIMIT, shift them by DELTA."
2134 (save-excursion
2135 (goto-char (org-table-end))
2136 (while (let ((case-fold-search t)) (looking-at "[ \t]*#\\+tblfm:"))
2137 (let ((msg "The formulas in #+TBLFM have been updated")
2138 (re (concat key "\\([0-9]+\\)"))
2139 (re2
2140 (when remove
2141 (if (or (equal key "$") (equal key "$LR"))
2142 (format "\\(@[0-9]+\\)?%s%d=.*?\\(::\\|$\\)"
2143 (regexp-quote key) remove)
2144 (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
2145 s n a)
2146 (when remove
2147 (while (re-search-forward re2 (point-at-eol) t)
2148 (unless (save-match-data (org-in-regexp "remote([^)]+?)"))
2149 (if (equal (char-before (match-beginning 0)) ?.)
2150 (user-error
2151 "Change makes TBLFM term %s invalid, use undo to recover"
2152 (match-string 0))
2153 (replace-match "")))))
2154 (while (re-search-forward re (point-at-eol) t)
2155 (unless (save-match-data (org-in-regexp "remote([^)]+?)"))
2156 (setq s (match-string 1) n (string-to-number s))
2157 (cond
2158 ((setq a (assoc s replace))
2159 (replace-match (concat key (cdr a)) t t)
2160 (message msg))
2161 ((and limit (> n limit))
2162 (replace-match (concat key (int-to-string (+ n delta))) t t)
2163 (message msg))))))
2164 (forward-line))))
2166 ;;;###autoload
2167 (defun org-table-maybe-eval-formula ()
2168 "Check if the current field starts with \"=\" or \":=\".
2169 If yes, store the formula and apply it."
2170 ;; We already know we are in a table. Get field will only return a formula
2171 ;; when appropriate. It might return a separator line, but no problem.
2172 (when org-table-formula-evaluate-inline
2173 (let* ((field (org-trim (or (org-table-get-field) "")))
2174 named eq)
2175 (when (string-match "^:?=\\(.*[^=]\\)$" field)
2176 (setq named (equal (string-to-char field) ?:)
2177 eq (match-string 1 field))
2178 (org-table-eval-formula (and named '(4))
2179 (org-table-formula-from-user eq))))))
2181 ;;;###autoload
2182 (defun org-table-rotate-recalc-marks (&optional newchar)
2183 "Rotate the recalculation mark in the first column.
2184 If in any row, the first field is not consistent with a mark,
2185 insert a new column for the markers.
2186 When there is an active region, change all the lines in the region,
2187 after prompting for the marking character.
2188 After each change, a message will be displayed indicating the meaning
2189 of the new mark."
2190 (interactive)
2191 (unless (org-at-table-p) (user-error "Not at a table"))
2192 (let* ((region (org-region-active-p))
2193 (l1 (and region
2194 (save-excursion (goto-char (region-beginning))
2195 (copy-marker (line-beginning-position)))))
2196 (l2 (and region
2197 (save-excursion (goto-char (region-end))
2198 (copy-marker (line-beginning-position)))))
2199 (l (copy-marker (line-beginning-position)))
2200 (col (org-table-current-column))
2201 (newchar (if region
2202 (char-to-string
2203 (read-char-exclusive
2204 "Change region to what mark? Type # * ! $ or SPC: "))
2205 newchar))
2206 (no-special-column
2207 (save-excursion
2208 (goto-char (org-table-begin))
2209 (re-search-forward
2210 "^[ \t]*|[^-~:|][^|]*[^#!$*_^| \t][^|]*|" (org-table-end) t))))
2211 (when (and newchar (not (assoc newchar org-recalc-marks)))
2212 (user-error "Invalid character `%s' in `org-table-rotate-recalc-marks'"
2213 newchar))
2214 (when l1 (goto-char l1))
2215 (save-excursion
2216 (beginning-of-line)
2217 (unless (looking-at org-table-dataline-regexp)
2218 (user-error "Not at a table data line")))
2219 (when no-special-column
2220 (org-table-goto-column 1)
2221 (org-table-insert-column))
2222 (let ((previous-line-end (line-end-position))
2223 (newchar
2224 (save-excursion
2225 (beginning-of-line)
2226 (cond ((not (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")) "#")
2227 (newchar)
2228 (t (cadr (member (match-string 1)
2229 (append (mapcar #'car org-recalc-marks)
2230 '(" ")))))))))
2231 ;; Rotate mark in first row.
2232 (org-table-get-field 1 (format " %s " newchar))
2233 ;; Rotate marks in additional rows if a region is active.
2234 (when region
2235 (save-excursion
2236 (forward-line)
2237 (while (<= (point) l2)
2238 (when (looking-at org-table-dataline-regexp)
2239 (org-table-get-field 1 (format " %s " newchar)))
2240 (forward-line))))
2241 ;; Only align if rotation actually changed lines' length.
2242 (when (/= previous-line-end (line-end-position)) (org-table-align)))
2243 (goto-char l)
2244 (org-table-goto-column (if no-special-column (1+ col) col))
2245 (when l1 (set-marker l1 nil))
2246 (when l2 (set-marker l2 nil))
2247 (set-marker l nil)
2248 (when (called-interactively-p 'interactive)
2249 (message "%s" (cdr (assoc newchar org-recalc-marks))))))
2251 ;;;###autoload
2252 (defun org-table-maybe-recalculate-line ()
2253 "Recompute the current line if marked for it, and if we haven't just done it."
2254 (interactive)
2255 (and org-table-allow-automatic-line-recalculation
2256 (not (and (memq last-command org-recalc-commands)
2257 (eq org-last-recalc-line (line-beginning-position))))
2258 (save-excursion (beginning-of-line 1)
2259 (looking-at org-table-auto-recalculate-regexp))
2260 (org-table-recalculate) t))
2262 ;;;###autoload
2263 (defun org-table-eval-formula (&optional arg equation
2264 suppress-align suppress-const
2265 suppress-store suppress-analysis)
2266 "Replace the table field value at the cursor by the result of a calculation.
2268 In a table, this command replaces the value in the current field with the
2269 result of a formula. It also installs the formula as the \"current\" column
2270 formula, by storing it in a special line below the table. When called
2271 with a `\\[universal-argument]' prefix the formula is installed as a \
2272 field formula.
2274 When called with a `\\[universal-argument] \\[universal-argument]' prefix, \
2275 insert the active equation for the field
2276 back into the current field, so that it can be edited there. This is \
2277 useful
2278 in order to use \\<org-table-fedit-map>`\\[org-table-show-reference]' to \
2279 check the referenced fields.
2281 When called, the command first prompts for a formula, which is read in
2282 the minibuffer. Previously entered formulas are available through the
2283 history list, and the last used formula is offered as a default.
2284 These stored formulas are adapted correctly when moving, inserting, or
2285 deleting columns with the corresponding commands.
2287 The formula can be any algebraic expression understood by the Calc package.
2288 For details, see the Org mode manual.
2290 This function can also be called from Lisp programs and offers
2291 additional arguments: EQUATION can be the formula to apply. If this
2292 argument is given, the user will not be prompted.
2294 SUPPRESS-ALIGN is used to speed-up recursive calls by by-passing
2295 unnecessary aligns.
2297 SUPPRESS-CONST suppresses the interpretation of constants in the
2298 formula, assuming that this has been done already outside the
2299 function.
2301 SUPPRESS-STORE means the formula should not be stored, either
2302 because it is already stored, or because it is a modified
2303 equation that should not overwrite the stored one.
2305 SUPPRESS-ANALYSIS prevents analyzing the table and checking
2306 location of point."
2307 (interactive "P")
2308 (unless suppress-analysis
2309 (org-table-check-inside-data-field nil t)
2310 (org-table-analyze))
2311 (if (equal arg '(16))
2312 (let ((eq (org-table-current-field-formula)))
2313 (org-table-get-field nil eq)
2314 (org-table-align)
2315 (setq org-table-may-need-update t))
2316 (let* (fields
2317 (ndown (if (integerp arg) arg 1))
2318 (org-table-automatic-realign nil)
2319 (case-fold-search nil)
2320 (down (> ndown 1))
2321 (formula (if (and equation suppress-store)
2322 equation
2323 (org-table-get-formula equation (equal arg '(4)))))
2324 (n0 (org-table-current-column))
2325 (org-tbl-calc-modes (copy-sequence org-calc-default-modes))
2326 (numbers nil) ; was a variable, now fixed default
2327 (keep-empty nil)
2328 n form form0 formrpl formrg bw fmt x ev orig c lispp literal
2329 duration duration-output-format)
2330 ;; Parse the format string. Since we have a lot of modes, this is
2331 ;; a lot of work. However, I think calc still uses most of the time.
2332 (if (string-match ";" formula)
2333 (let ((tmp (org-split-string formula ";")))
2334 (setq formula (car tmp)
2335 fmt (concat (cdr (assoc "%" org-table-local-parameters))
2336 (nth 1 tmp)))
2337 (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
2338 (setq c (string-to-char (match-string 1 fmt))
2339 n (string-to-number (match-string 2 fmt)))
2340 (if (= c ?p)
2341 (setq org-tbl-calc-modes
2342 (org-table--set-calc-mode 'calc-internal-prec n))
2343 (setq org-tbl-calc-modes
2344 (org-table--set-calc-mode
2345 'calc-float-format
2346 (list (cdr (assoc c '((?n . float) (?f . fix)
2347 (?s . sci) (?e . eng))))
2348 n))))
2349 (setq fmt (replace-match "" t t fmt)))
2350 (if (string-match "[tTU]" fmt)
2351 (let ((ff (match-string 0 fmt)))
2352 (setq duration t numbers t
2353 duration-output-format
2354 (cond ((equal ff "T") nil)
2355 ((equal ff "t") org-table-duration-custom-format)
2356 ((equal ff "U") 'hh:mm))
2357 fmt (replace-match "" t t fmt))))
2358 (if (string-match "N" fmt)
2359 (setq numbers t
2360 fmt (replace-match "" t t fmt)))
2361 (if (string-match "L" fmt)
2362 (setq literal t
2363 fmt (replace-match "" t t fmt)))
2364 (if (string-match "E" fmt)
2365 (setq keep-empty t
2366 fmt (replace-match "" t t fmt)))
2367 (while (string-match "[DRFS]" fmt)
2368 (setq org-tbl-calc-modes
2369 (org-table--set-calc-mode (match-string 0 fmt)))
2370 (setq fmt (replace-match "" t t fmt)))
2371 (unless (string-match "\\S-" fmt)
2372 (setq fmt nil))))
2373 (when (and (not suppress-const) org-table-formula-use-constants)
2374 (setq formula (org-table-formula-substitute-names formula)))
2375 (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
2376 (setq formula (org-table-formula-handle-first/last-rc formula))
2377 (while (> ndown 0)
2378 (setq fields (org-split-string
2379 (org-trim
2380 (buffer-substring-no-properties
2381 (line-beginning-position) (line-end-position)))
2382 " *| *"))
2383 ;; replace fields with duration values if relevant
2384 (if duration
2385 (setq fields
2386 (mapcar (lambda (x) (org-table-time-string-to-seconds x))
2387 fields)))
2388 (if (eq numbers t)
2389 (setq fields (mapcar
2390 (lambda (x)
2391 (if (string-match "\\S-" x)
2392 (number-to-string (string-to-number x))
2394 fields)))
2395 (setq ndown (1- ndown))
2396 (setq form (copy-sequence formula)
2397 lispp (and (> (length form) 2) (equal (substring form 0 2) "'(")))
2398 (if (and lispp literal) (setq lispp 'literal))
2400 ;; Insert row and column number of formula result field
2401 (while (string-match "[@$]#" form)
2402 (setq form
2403 (replace-match
2404 (format "%d"
2405 (save-match-data
2406 (if (equal (substring form (match-beginning 0)
2407 (1+ (match-beginning 0)))
2408 "@")
2409 (org-table-current-dline)
2410 (org-table-current-column))))
2411 t t form)))
2413 ;; Check for old vertical references
2414 (org-table--error-on-old-row-references form)
2415 ;; Insert remote references
2416 (setq form (org-table-remote-reference-indirection form))
2417 (while (string-match "\\<remote([ \t]*\\([^,)]+\\)[ \t]*,[ \t]*\\([^\n)]+\\))" form)
2418 (setq form
2419 (replace-match
2420 (save-match-data
2421 (org-table-make-reference
2422 (let ((rmtrng (org-table-get-remote-range
2423 (match-string 1 form) (match-string 2 form))))
2424 (if duration
2425 (if (listp rmtrng)
2426 (mapcar (lambda(x) (org-table-time-string-to-seconds x)) rmtrng)
2427 (org-table-time-string-to-seconds rmtrng))
2428 rmtrng))
2429 keep-empty numbers lispp))
2430 t t form)))
2431 ;; Insert complex ranges
2432 (while (and (string-match org-table-range-regexp form)
2433 (> (length (match-string 0 form)) 1))
2434 (setq formrg
2435 (save-match-data
2436 (org-table-get-range
2437 (match-string 0 form) org-table-current-begin-pos n0)))
2438 (setq formrpl
2439 (save-match-data
2440 (org-table-make-reference
2441 ;; possibly handle durations
2442 (if duration
2443 (if (listp formrg)
2444 (mapcar (lambda(x) (org-table-time-string-to-seconds x)) formrg)
2445 (org-table-time-string-to-seconds formrg))
2446 formrg)
2447 keep-empty numbers lispp)))
2448 (if (not (save-match-data
2449 (string-match (regexp-quote form) formrpl)))
2450 (setq form (replace-match formrpl t t form))
2451 (user-error "Spreadsheet error: invalid reference \"%s\"" form)))
2452 ;; Insert simple ranges, i.e. included in the current row.
2453 (while (string-match
2454 "\\$\\(\\([-+]\\)?[0-9]+\\)\\.\\.\\$\\(\\([-+]\\)?[0-9]+\\)"
2455 form)
2456 (setq form
2457 (replace-match
2458 (save-match-data
2459 (org-table-make-reference
2460 (cl-subseq fields
2461 (+ (if (match-end 2) n0 0)
2462 (string-to-number (match-string 1 form))
2464 (+ (if (match-end 4) n0 0)
2465 (string-to-number (match-string 3 form))))
2466 keep-empty numbers lispp))
2467 t t form)))
2468 (setq form0 form)
2469 ;; Insert the references to fields in same row
2470 (while (string-match "\\$\\(\\([-+]\\)?[0-9]+\\)" form)
2471 (setq n (+ (string-to-number (match-string 1 form))
2472 (if (match-end 2) n0 0))
2473 x (nth (1- (if (= n 0) n0 (max n 1))) fields)
2474 formrpl (save-match-data
2475 (org-table-make-reference
2476 x keep-empty numbers lispp)))
2477 (when (or (not x)
2478 (save-match-data
2479 (string-match (regexp-quote formula) formrpl)))
2480 (user-error "Invalid field specifier \"%s\""
2481 (match-string 0 form)))
2482 (setq form (replace-match formrpl t t form)))
2484 (if lispp
2485 (setq ev (condition-case nil
2486 (eval (eval (read form)))
2487 (error "#ERROR"))
2488 ev (if (numberp ev) (number-to-string ev) ev)
2489 ev (if duration (org-table-time-seconds-to-string
2490 (string-to-number ev)
2491 duration-output-format) ev))
2493 ;; Use <...> time-stamps so that Calc can handle them.
2494 (setq form
2495 (replace-regexp-in-string org-ts-regexp-inactive "<\\1>" form))
2496 ;; Internationalize local time-stamps by setting locale to
2497 ;; "C".
2498 (setq form
2499 (replace-regexp-in-string
2500 org-ts-regexp
2501 (lambda (ts)
2502 (let ((system-time-locale "C"))
2503 (format-time-string
2504 (org-time-stamp-format
2505 (string-match-p "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts))
2506 (apply #'encode-time
2507 (save-match-data (org-parse-time-string ts))))))
2508 form t t))
2510 (setq ev (if (and duration (string-match "^[0-9]+:[0-9]+\\(?::[0-9]+\\)?$" form))
2511 form
2512 (calc-eval (cons form org-tbl-calc-modes)
2513 (when (and (not keep-empty) numbers) 'num)))
2514 ev (if duration (org-table-time-seconds-to-string
2515 (if (string-match "^[0-9]+:[0-9]+\\(?::[0-9]+\\)?$" ev)
2516 (string-to-number (org-table-time-string-to-seconds ev))
2517 (string-to-number ev))
2518 duration-output-format)
2519 ev)))
2521 (when org-table-formula-debug
2522 (with-output-to-temp-buffer "*Substitution History*"
2523 (princ (format "Substitution history of formula
2524 Orig: %s
2525 $xyz-> %s
2526 @r$c-> %s
2527 $1-> %s\n" orig formula form0 form))
2528 (if (consp ev)
2529 (princ (format " %s^\nError: %s"
2530 (make-string (car ev) ?\-) (nth 1 ev)))
2531 (princ (format "Result: %s\nFormat: %s\nFinal: %s"
2532 ev (or fmt "NONE")
2533 (if fmt (format fmt (string-to-number ev)) ev)))))
2534 (setq bw (get-buffer-window "*Substitution History*"))
2535 (org-fit-window-to-buffer bw)
2536 (unless (and (called-interactively-p 'any) (not ndown))
2537 (unless (let (inhibit-redisplay)
2538 (y-or-n-p "Debugging Formula. Continue to next? "))
2539 (org-table-align)
2540 (user-error "Abort"))
2541 (delete-window bw)
2542 (message "")))
2543 (when (consp ev) (setq fmt nil ev "#ERROR"))
2544 (org-table-justify-field-maybe
2545 (format org-table-formula-field-format
2546 (cond
2547 ((not (stringp ev)) ev)
2548 (fmt (format fmt (string-to-number ev)))
2549 ;; Replace any active time stamp in the result with
2550 ;; an inactive one. Dates in tables are likely
2551 ;; piece of regular data, not meant to appear in the
2552 ;; agenda.
2553 (t (replace-regexp-in-string org-ts-regexp "[\\1]" ev)))))
2554 (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
2555 (call-interactively 'org-return)
2556 (setq ndown 0)))
2557 (and down (org-table-maybe-recalculate-line))
2558 (or suppress-align (and org-table-may-need-update
2559 (org-table-align))))))
2561 (defun org-table-put-field-property (prop value)
2562 (save-excursion
2563 (put-text-property (progn (skip-chars-backward "^|") (point))
2564 (progn (skip-chars-forward "^|") (point))
2565 prop value)))
2567 (defun org-table-get-range (desc &optional tbeg col highlight corners-only)
2568 "Get a calc vector from a column, according to descriptor DESC.
2570 Optional arguments TBEG and COL can give the beginning of the table and
2571 the current column, to avoid unnecessary parsing.
2573 HIGHLIGHT means just highlight the range.
2575 When CORNERS-ONLY is set, only return the corners of the range as
2576 a list (line1 column1 line2 column2) where line1 and line2 are
2577 line numbers relative to beginning of table, or TBEG, and column1
2578 and column2 are table column numbers."
2579 (let* ((desc (if (string-match-p "\\`\\$[0-9]+\\.\\.\\$[0-9]+\\'" desc)
2580 (replace-regexp-in-string "\\$" "@0$" desc)
2581 desc))
2582 (col (or col (org-table-current-column)))
2583 (tbeg (or tbeg (org-table-begin)))
2584 (thisline (count-lines tbeg (line-beginning-position))))
2585 (unless (string-match org-table-range-regexp desc)
2586 (user-error "Invalid table range specifier `%s'" desc))
2587 (let ((rangep (match-end 3))
2588 (r1 (let ((r (and (match-end 1) (match-string 1 desc))))
2589 (or (save-match-data
2590 (and (org-string-nw-p r)
2591 (org-table--descriptor-line r thisline)))
2592 thisline)))
2593 (r2 (let ((r (and (match-end 4) (match-string 4 desc))))
2594 (or (save-match-data
2595 (and (org-string-nw-p r)
2596 (org-table--descriptor-line r thisline)))
2597 thisline)))
2598 (c1 (let ((c (and (match-end 2) (substring (match-string 2 desc) 1))))
2599 (if (or (not c) (= (string-to-number c) 0)) col
2600 (+ (string-to-number c)
2601 (if (memq (string-to-char c) '(?- ?+)) col 0)))))
2602 (c2 (let ((c (and (match-end 5) (substring (match-string 5 desc) 1))))
2603 (if (or (not c) (= (string-to-number c) 0)) col
2604 (+ (string-to-number c)
2605 (if (memq (string-to-char c) '(?- ?+)) col 0))))))
2606 (save-excursion
2607 (if (and (not corners-only)
2608 (or (not rangep) (and (= r1 r2) (= c1 c2))))
2609 ;; Just one field.
2610 (progn
2611 (forward-line (- r1 thisline))
2612 (while (not (looking-at org-table-dataline-regexp))
2613 (forward-line))
2614 (prog1 (org-trim (org-table-get-field c1))
2615 (when highlight (org-table-highlight-rectangle))))
2616 ;; A range, return a vector. First sort the numbers to get
2617 ;; a regular rectangle.
2618 (let ((first-row (min r1 r2))
2619 (last-row (max r1 r2))
2620 (first-column (min c1 c2))
2621 (last-column (max c1 c2)))
2622 (if corners-only (list first-row first-column last-row last-column)
2623 ;; Copy the range values into a list.
2624 (forward-line (- first-row thisline))
2625 (while (not (looking-at org-table-dataline-regexp))
2626 (forward-line)
2627 (cl-incf first-row))
2628 (org-table-goto-column first-column)
2629 (let ((beg (point)))
2630 (forward-line (- last-row first-row))
2631 (while (not (looking-at org-table-dataline-regexp))
2632 (forward-line -1))
2633 (org-table-goto-column last-column)
2634 (let ((end (point)))
2635 (when highlight
2636 (org-table-highlight-rectangle
2637 beg (progn (skip-chars-forward "^|\n") (point))))
2638 ;; Return string representation of calc vector.
2639 (mapcar #'org-trim
2640 (apply #'append
2641 (org-table-copy-region beg end))))))))))))
2643 (defun org-table--descriptor-line (desc cline)
2644 "Return relative line number corresponding to descriptor DESC.
2645 The cursor is currently in relative line number CLINE."
2646 (if (string-match "\\`[0-9]+\\'" desc)
2647 (aref org-table-dlines (string-to-number desc))
2648 (when (or (not (string-match
2649 "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?"
2650 ;; 1 2 3 4 5 6
2651 desc))
2652 (and (not (match-end 3)) (not (match-end 6)))
2653 (and (match-end 3) (match-end 6) (not (match-end 5))))
2654 (user-error "Invalid row descriptor `%s'" desc))
2655 (let* ((hn (and (match-end 3) (- (match-end 3) (match-beginning 3))))
2656 (hdir (match-string 2 desc))
2657 (odir (match-string 5 desc))
2658 (on (and (match-end 6) (string-to-number (match-string 6 desc))))
2659 (rel (and (match-end 6)
2660 (or (and (match-end 1) (not (match-end 3)))
2661 (match-end 5)))))
2662 (when (and hn (not hdir))
2663 (setq cline 0)
2664 (setq hdir "+")
2665 (when (eq (aref org-table-current-line-types 0) 'hline) (cl-decf hn)))
2666 (when (and (not hn) on (not odir)) (user-error "Should never happen"))
2667 (when hn
2668 (setq cline
2669 (org-table--row-type 'hline hn cline (equal hdir "-") nil desc)))
2670 (when on
2671 (setq cline
2672 (org-table--row-type 'dline on cline (equal odir "-") rel desc)))
2673 cline)))
2675 (defun org-table--row-type (type n i backwards relative desc)
2676 "Return relative line of Nth row with type TYPE.
2677 Search starts from relative line I. When BACKWARDS in non-nil,
2678 look before I. When RELATIVE is non-nil, the reference is
2679 relative. DESC is the original descriptor that started the
2680 search, as a string."
2681 (let ((l (length org-table-current-line-types)))
2682 (catch :exit
2683 (dotimes (_ n)
2684 (while (and (cl-incf i (if backwards -1 1))
2685 (>= i 0)
2686 (< i l)
2687 (not (eq (aref org-table-current-line-types i) type))
2688 ;; We are going to cross a hline. Check if this is
2689 ;; an authorized move.
2690 (cond
2691 ((not relative))
2692 ((not (eq (aref org-table-current-line-types i) 'hline)))
2693 ((eq org-table-relative-ref-may-cross-hline t))
2694 ((eq org-table-relative-ref-may-cross-hline 'error)
2695 (user-error "Row descriptor %s crosses hline" desc))
2696 (t (cl-decf i (if backwards -1 1)) ; Step back.
2697 (throw :exit nil)))))))
2698 (cond ((or (< i 0) (>= i l))
2699 (user-error "Row descriptor %s leads outside table" desc))
2700 ;; The last hline doesn't exist. Instead, point to last row
2701 ;; in table.
2702 ((= i (1- l)) (1- i))
2703 (t i))))
2705 (defun org-table--error-on-old-row-references (s)
2706 (when (string-match "&[-+0-9I]" s)
2707 (user-error "Formula contains old &row reference, please rewrite using @-syntax")))
2709 (defun org-table-make-reference (elements keep-empty numbers lispp)
2710 "Convert list ELEMENTS to something appropriate to insert into formula.
2711 KEEP-EMPTY indicated to keep empty fields, default is to skip them.
2712 NUMBERS indicates that everything should be converted to numbers.
2713 LISPP non-nil means to return something appropriate for a Lisp
2714 list, `literal' is for the format specifier L."
2715 ;; Calc nan (not a number) is used for the conversion of the empty
2716 ;; field to a reference for several reasons: (i) It is accepted in a
2717 ;; Calc formula (e. g. "" or "()" would result in a Calc error).
2718 ;; (ii) In a single field (not in range) it can be distinguished
2719 ;; from "(nan)" which is the reference made from a single field
2720 ;; containing "nan".
2721 (if (stringp elements)
2722 ;; field reference
2723 (if lispp
2724 (if (eq lispp 'literal)
2725 elements
2726 (if (and (eq elements "") (not keep-empty))
2728 (prin1-to-string
2729 (if numbers (string-to-number elements) elements))))
2730 (if (string-match "\\S-" elements)
2731 (progn
2732 (when numbers (setq elements (number-to-string
2733 (string-to-number elements))))
2734 (concat "(" elements ")"))
2735 (if (or (not keep-empty) numbers) "(0)" "nan")))
2736 ;; range reference
2737 (unless keep-empty
2738 (setq elements
2739 (delq nil
2740 (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
2741 elements))))
2742 (setq elements (or elements '())) ; if delq returns nil then we need '()
2743 (if lispp
2744 (mapconcat
2745 (lambda (x)
2746 (if (eq lispp 'literal)
2748 (prin1-to-string (if numbers (string-to-number x) x))))
2749 elements " ")
2750 (concat "[" (mapconcat
2751 (lambda (x)
2752 (if (string-match "\\S-" x)
2753 (if numbers
2754 (number-to-string (string-to-number x))
2756 (if (or (not keep-empty) numbers) "0" "nan")))
2757 elements
2758 ",") "]"))))
2760 (defun org-table-message-once-per-second (t1 &rest args)
2761 "If there has been more than one second since T1, display message.
2762 ARGS are passed as arguments to the `message' function. Returns
2763 current time if a message is printed, otherwise returns T1. If
2764 T1 is nil, always messages."
2765 (let ((curtime (current-time)))
2766 (if (or (not t1) (< 0 (nth 1 (time-subtract curtime t1))))
2767 (progn (apply 'message args)
2768 curtime)
2769 t1)))
2771 ;;;###autoload
2772 (defun org-table-recalculate (&optional all noalign)
2773 "Recalculate the current table line by applying all stored formulas.
2775 With prefix arg ALL, do this for all lines in the table.
2777 When called with a `\\[universal-argument] \\[universal-argument]' prefix, or \
2778 if ALL is the symbol `iterate',
2779 recompute the table until it no longer changes.
2781 If NOALIGN is not nil, do not re-align the table after the computations
2782 are done. This is typically used internally to save time, if it is
2783 known that the table will be realigned a little later anyway."
2784 (interactive "P")
2785 (unless (memq this-command org-recalc-commands)
2786 (push this-command org-recalc-commands))
2787 (unless (org-at-table-p) (user-error "Not at a table"))
2788 (if (or (eq all 'iterate) (equal all '(16)))
2789 (org-table-iterate)
2790 (org-table-analyze)
2791 (let* ((eqlist (sort (org-table-get-stored-formulas)
2792 (lambda (a b) (string< (car a) (car b)))))
2793 (inhibit-redisplay (not debug-on-error))
2794 (line-re org-table-dataline-regexp)
2795 (log-first-time (current-time))
2796 (log-last-time log-first-time)
2797 (cnt 0)
2798 beg end eqlcol eqlfield)
2799 ;; Insert constants in all formulas.
2800 (when eqlist
2801 (org-table-with-shrunk-columns
2802 (org-table-save-field
2803 ;; Expand equations, then split the equation list between
2804 ;; column formulas and field formulas.
2805 (dolist (eq eqlist)
2806 (let* ((rhs (org-table-formula-substitute-names
2807 (org-table-formula-handle-first/last-rc (cdr eq))))
2808 (old-lhs (car eq))
2809 (lhs
2810 (org-table-formula-handle-first/last-rc
2811 (cond
2812 ((string-match "\\`@-?I+" old-lhs)
2813 (user-error "Can't assign to hline relative reference"))
2814 ((string-match "\\`\\$[<>]" old-lhs)
2815 (let ((new (org-table-formula-handle-first/last-rc
2816 old-lhs)))
2817 (when (assoc new eqlist)
2818 (user-error "\"%s=\" formula tries to overwrite \
2819 existing formula for column %s"
2820 old-lhs
2821 new))
2822 new))
2823 (t old-lhs)))))
2824 (if (string-match-p "\\`\\$[0-9]+\\'" lhs)
2825 (push (cons lhs rhs) eqlcol)
2826 (push (cons lhs rhs) eqlfield))))
2827 (setq eqlcol (nreverse eqlcol))
2828 ;; Expand ranges in lhs of formulas
2829 (setq eqlfield (org-table-expand-lhs-ranges (nreverse eqlfield)))
2830 ;; Get the correct line range to process.
2831 (if all
2832 (progn
2833 (setq end (copy-marker (org-table-end)))
2834 (goto-char (setq beg org-table-current-begin-pos))
2835 (cond
2836 ((re-search-forward org-table-calculate-mark-regexp end t)
2837 ;; This is a table with marked lines, compute selected
2838 ;; lines.
2839 (setq line-re org-table-recalculate-regexp))
2840 ;; Move forward to the first non-header line.
2841 ((and (re-search-forward org-table-dataline-regexp end t)
2842 (re-search-forward org-table-hline-regexp end t)
2843 (re-search-forward org-table-dataline-regexp end t))
2844 (setq beg (match-beginning 0)))
2845 ;; Just leave BEG at the start of the table.
2846 (t nil)))
2847 (setq beg (line-beginning-position)
2848 end (copy-marker (line-beginning-position 2))))
2849 (goto-char beg)
2850 ;; Mark named fields untouchable. Also check if several
2851 ;; field/range formulas try to set the same field.
2852 (remove-text-properties beg end '(:org-untouchable t))
2853 (let ((current-line (count-lines org-table-current-begin-pos
2854 (line-beginning-position)))
2855 seen-fields)
2856 (dolist (eq eqlfield)
2857 (let* ((name (car eq))
2858 (location (assoc name org-table-named-field-locations))
2859 (eq-line (or (nth 1 location)
2860 (and (string-match "\\`@\\([0-9]+\\)" name)
2861 (aref org-table-dlines
2862 (string-to-number
2863 (match-string 1 name))))))
2864 (reference
2865 (if location
2866 ;; Turn field coordinates associated to NAME
2867 ;; into an absolute reference.
2868 (format "@%d$%d"
2869 (org-table-line-to-dline eq-line)
2870 (nth 2 location))
2871 name)))
2872 (when (member reference seen-fields)
2873 (user-error "Several field/range formulas try to set %s"
2874 reference))
2875 (push reference seen-fields)
2876 (when (or all (eq eq-line current-line))
2877 (org-table-goto-field name)
2878 (org-table-put-field-property :org-untouchable t)))))
2879 ;; Evaluate the column formulas, but skip fields covered by
2880 ;; field formulas.
2881 (goto-char beg)
2882 (while (re-search-forward line-re end t)
2883 (unless (string-match "\\` *[_^!$/] *\\'" (org-table-get-field 1))
2884 ;; Unprotected line, recalculate.
2885 (cl-incf cnt)
2886 (when all
2887 (setq log-last-time
2888 (org-table-message-once-per-second
2889 log-last-time
2890 "Re-applying formulas to full table...(line %d)" cnt)))
2891 (if (markerp org-last-recalc-line)
2892 (move-marker org-last-recalc-line (line-beginning-position))
2893 (setq org-last-recalc-line
2894 (copy-marker (line-beginning-position))))
2895 (dolist (entry eqlcol)
2896 (goto-char org-last-recalc-line)
2897 (org-table-goto-column
2898 (string-to-number (substring (car entry) 1)) nil 'force)
2899 (unless (get-text-property (point) :org-untouchable)
2900 (org-table-eval-formula
2901 nil (cdr entry) 'noalign 'nocst 'nostore 'noanalysis)))))
2902 ;; Evaluate the field formulas.
2903 (dolist (eq eqlfield)
2904 (let ((reference (car eq))
2905 (formula (cdr eq)))
2906 (setq log-last-time
2907 (org-table-message-once-per-second
2908 (and all log-last-time)
2909 "Re-applying formula to field: %s" (car eq)))
2910 (org-table-goto-field
2911 reference
2912 ;; Possibly create a new column, as long as
2913 ;; `org-table-formula-create-columns' allows it.
2914 (let ((column-count (progn (end-of-line)
2915 (1- (org-table-current-column)))))
2916 (lambda (column)
2917 (when (> column 1000)
2918 (user-error "Formula column target too large"))
2919 (and (> column column-count)
2920 (or (eq org-table-formula-create-columns t)
2921 (and (eq org-table-formula-create-columns 'warn)
2922 (progn
2923 (org-display-warning
2924 "Out-of-bounds formula added columns")
2926 (and (eq org-table-formula-create-columns 'prompt)
2927 (yes-or-no-p
2928 "Out-of-bounds formula. Add columns? "))
2929 (user-error
2930 "Missing columns in the table. Aborting"))))))
2931 (org-table-eval-formula nil formula t t t t)))
2932 ;; Clean up markers and internal text property.
2933 (remove-text-properties (point-min) (point-max) '(:org-untouchable t))
2934 (set-marker end nil)))
2935 (unless noalign
2936 (when org-table-may-need-update (org-table-align))
2937 (when all
2938 (org-table-message-once-per-second
2939 log-first-time "Re-applying formulas to %d lines... done" cnt)))
2940 (org-table-message-once-per-second
2941 (and all log-first-time) "Re-applying formulas... done")))))
2943 ;;;###autoload
2944 (defun org-table-iterate (&optional arg)
2945 "Recalculate the table until it does not change anymore.
2946 The maximum number of iterations is 10, but you can choose a different value
2947 with the prefix ARG."
2948 (interactive "P")
2949 (let ((imax (if arg (prefix-numeric-value arg) 10))
2950 (i 0)
2951 (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
2952 thistbl)
2953 (catch 'exit
2954 (while (< i imax)
2955 (setq i (1+ i))
2956 (org-table-recalculate 'all)
2957 (setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
2958 (if (not (string= lasttbl thistbl))
2959 (setq lasttbl thistbl)
2960 (if (> i 1)
2961 (message "Convergence after %d iterations" i)
2962 (message "Table was already stable"))
2963 (throw 'exit t)))
2964 (user-error "No convergence after %d iterations" i))))
2966 ;;;###autoload
2967 (defun org-table-recalculate-buffer-tables ()
2968 "Recalculate all tables in the current buffer."
2969 (interactive)
2970 (org-with-wide-buffer
2971 (org-table-map-tables
2972 (lambda ()
2973 ;; Reason for separate `org-table-align': When repeating
2974 ;; (org-table-recalculate t) `org-table-may-need-update' gets in
2975 ;; the way.
2976 (org-table-recalculate t t)
2977 (org-table-align))
2978 t)))
2980 ;;;###autoload
2981 (defun org-table-iterate-buffer-tables ()
2982 "Iterate all tables in the buffer, to converge inter-table dependencies."
2983 (interactive)
2984 (let* ((imax 10)
2985 (i imax)
2986 (checksum (md5 (buffer-string)))
2988 (org-with-wide-buffer
2989 (catch 'exit
2990 (while (> i 0)
2991 (setq i (1- i))
2992 (org-table-map-tables (lambda () (org-table-recalculate t t)) t)
2993 (if (equal checksum (setq c1 (md5 (buffer-string))))
2994 (progn
2995 (org-table-map-tables #'org-table-align t)
2996 (message "Convergence after %d iterations" (- imax i))
2997 (throw 'exit t))
2998 (setq checksum c1)))
2999 (org-table-map-tables #'org-table-align t)
3000 (user-error "No convergence after %d iterations" imax)))))
3002 (defun org-table-calc-current-TBLFM (&optional arg)
3003 "Apply the #+TBLFM in the line at point to the table."
3004 (interactive "P")
3005 (unless (org-at-TBLFM-p) (user-error "Not at a #+TBLFM line"))
3006 (let ((formula (buffer-substring
3007 (line-beginning-position)
3008 (line-end-position))))
3009 (save-excursion
3010 ;; Insert a temporary formula at right after the table
3011 (goto-char (org-table-TBLFM-begin))
3012 (let ((s (point-marker)))
3013 (insert formula "\n")
3014 (let ((e (point-marker)))
3015 ;; Recalculate the table.
3016 (beginning-of-line 0) ; move to the inserted line
3017 (skip-chars-backward " \r\n\t")
3018 (unwind-protect
3019 (org-call-with-arg #'org-table-recalculate (or arg t))
3020 ;; Delete the formula inserted temporarily.
3021 (delete-region s e)
3022 (set-marker s nil)
3023 (set-marker e nil)))))))
3025 (defun org-table-TBLFM-begin ()
3026 "Find the beginning of the TBLFM lines and return its position.
3027 Return nil when the beginning of TBLFM line was not found."
3028 (save-excursion
3029 (when (progn (forward-line 1)
3030 (re-search-backward org-table-TBLFM-begin-regexp nil t))
3031 (line-beginning-position 2))))
3033 (defun org-table-expand-lhs-ranges (equations)
3034 "Expand list of formulas.
3035 If some of the RHS in the formulas are ranges or a row reference,
3036 expand them to individual field equations for each field. This
3037 function assumes the table is already analyzed (i.e., using
3038 `org-table-analyze')."
3039 (let (res)
3040 (dolist (e equations (nreverse res))
3041 (let ((lhs (car e))
3042 (rhs (cdr e)))
3043 (cond
3044 ((string-match-p "\\`@-?[-+0-9]+\\$-?[0-9]+\\'" lhs)
3045 ;; This just refers to one fixed field.
3046 (push e res))
3047 ((string-match-p "\\`[a-zA-Z][_a-zA-Z0-9]*\\'" lhs)
3048 ;; This just refers to one fixed named field.
3049 (push e res))
3050 ((string-match-p "\\`\\$[0-9]+\\'" lhs)
3051 ;; Column formulas are treated specially and are not
3052 ;; expanded.
3053 (push e res))
3054 ((string-match "\\`@[0-9]+\\'" lhs)
3055 (dotimes (ic org-table-current-ncol)
3056 (push (cons (propertize (format "%s$%d" lhs (1+ ic)) :orig-eqn e)
3057 rhs)
3058 res)))
3060 (let* ((range (org-table-get-range
3061 lhs org-table-current-begin-pos 1 nil 'corners))
3062 (r1 (org-table-line-to-dline (nth 0 range)))
3063 (c1 (nth 1 range))
3064 (r2 (org-table-line-to-dline (nth 2 range) 'above))
3065 (c2 (nth 3 range)))
3066 (cl-loop for ir from r1 to r2 do
3067 (cl-loop for ic from c1 to c2 do
3068 (push (cons (propertize
3069 (format "@%d$%d" ir ic) :orig-eqn e)
3070 rhs)
3071 res))))))))))
3073 (defun org-table-formula-handle-first/last-rc (s)
3074 "Replace @<, @>, $<, $> with first/last row/column of the table.
3075 So @< and $< will always be replaced with @1 and $1, respectively.
3076 The advantage of these special markers are that structure editing of
3077 the table will not change them, while @1 and $1 will be modified
3078 when a line/row is swapped out of that privileged position. So for
3079 formulas that use a range of rows or columns, it may often be better
3080 to anchor the formula with \"I\" row markers, or to offset from the
3081 borders of the table using the @< @> $< $> makers."
3082 (let (n nmax len char (start 0))
3083 (while (string-match "\\([@$]\\)\\(<+\\|>+\\)\\|\\(remote([^)]+)\\)"
3084 s start)
3085 (if (match-end 3)
3086 (setq start (match-end 3))
3087 (setq nmax (if (equal (match-string 1 s) "@")
3088 (1- (length org-table-dlines))
3089 org-table-current-ncol)
3090 len (- (match-end 2) (match-beginning 2))
3091 char (string-to-char (match-string 2 s))
3092 n (if (= char ?<)
3094 (- nmax len -1)))
3095 (if (or (< n 1) (> n nmax))
3096 (user-error "Reference \"%s\" in expression \"%s\" points outside table"
3097 (match-string 0 s) s))
3098 (setq start (match-beginning 0))
3099 (setq s (replace-match (format "%s%d" (match-string 1 s) n) t t s)))))
3102 (defun org-table-formula-substitute-names (f)
3103 "Replace $const with values in string F."
3104 (let ((start 0)
3105 (pp (/= (string-to-char f) ?'))
3106 (duration (string-match-p ";.*[Tt].*\\'" f))
3107 (new (replace-regexp-in-string ; Check for column names.
3108 org-table-column-name-regexp
3109 (lambda (m)
3110 (concat "$" (cdr (assoc (match-string 1 m)
3111 org-table-column-names))))
3112 f t t)))
3113 ;; Parameters and constants.
3114 (while (setq start
3115 (string-match
3116 "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)\\|\\(\\<remote([^)]*)\\)"
3117 new start))
3118 (if (match-end 2) (setq start (match-end 2))
3119 (cl-incf start)
3120 ;; When a duration is expected, convert value on the fly.
3121 (let ((value
3122 (save-match-data
3123 (let ((v (org-table-get-constant (match-string 1 new))))
3124 (if (and (org-string-nw-p v) duration)
3125 (org-table-time-string-to-seconds v)
3126 v)))))
3127 (when value
3128 (setq new (replace-match
3129 (concat (and pp "(") value (and pp ")")) t t new))))))
3130 (if org-table-formula-debug (propertize new :orig-formula f) new)))
3132 (defun org-table-get-constant (const)
3133 "Find the value for a parameter or constant in a formula.
3134 Parameters get priority."
3135 (or (cdr (assoc const org-table-local-parameters))
3136 (cdr (assoc const org-table-formula-constants-local))
3137 (cdr (assoc const org-table-formula-constants))
3138 (and (fboundp 'constants-get) (constants-get const))
3139 (and (string= (substring const 0 (min 5 (length const))) "PROP_")
3140 (org-entry-get nil (substring const 5) 'inherit))
3141 "#UNDEFINED_NAME"))
3143 (defvar org-table-fedit-map
3144 (let ((map (make-sparse-keymap)))
3145 (org-defkey map "\C-x\C-s" 'org-table-fedit-finish)
3146 (org-defkey map "\C-c\C-s" 'org-table-fedit-finish)
3147 (org-defkey map "\C-c\C-c" 'org-table-fedit-finish)
3148 (org-defkey map "\C-c'" 'org-table-fedit-finish)
3149 (org-defkey map "\C-c\C-q" 'org-table-fedit-abort)
3150 (org-defkey map "\C-c?" 'org-table-show-reference)
3151 (org-defkey map [(meta shift up)] 'org-table-fedit-line-up)
3152 (org-defkey map [(meta shift down)] 'org-table-fedit-line-down)
3153 (org-defkey map [(shift up)] 'org-table-fedit-ref-up)
3154 (org-defkey map [(shift down)] 'org-table-fedit-ref-down)
3155 (org-defkey map [(shift left)] 'org-table-fedit-ref-left)
3156 (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
3157 (org-defkey map [(meta up)] 'org-table-fedit-scroll-down)
3158 (org-defkey map [(meta down)] 'org-table-fedit-scroll)
3159 (org-defkey map [(meta tab)] 'lisp-complete-symbol)
3160 (org-defkey map "\M-\C-i" 'lisp-complete-symbol)
3161 (org-defkey map [(tab)] 'org-table-fedit-lisp-indent)
3162 (org-defkey map "\C-i" 'org-table-fedit-lisp-indent)
3163 (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
3164 (org-defkey map "\C-c}" 'org-table-fedit-toggle-coordinates)
3165 map))
3167 (easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
3168 '("Edit-Formulas"
3169 ["Finish and Install" org-table-fedit-finish t]
3170 ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
3171 ["Abort" org-table-fedit-abort t]
3172 "--"
3173 ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
3174 ["Complete Lisp Symbol" lisp-complete-symbol t]
3175 "--"
3176 "Shift Reference at Point"
3177 ["Up" org-table-fedit-ref-up t]
3178 ["Down" org-table-fedit-ref-down t]
3179 ["Left" org-table-fedit-ref-left t]
3180 ["Right" org-table-fedit-ref-right t]
3182 "Change Test Row for Column Formulas"
3183 ["Up" org-table-fedit-line-up t]
3184 ["Down" org-table-fedit-line-down t]
3185 "--"
3186 ["Scroll Table Window" org-table-fedit-scroll t]
3187 ["Scroll Table Window down" org-table-fedit-scroll-down t]
3188 ["Show Table Grid" org-table-fedit-toggle-coordinates
3189 :style toggle :selected (with-current-buffer (marker-buffer org-pos)
3190 org-table-overlay-coordinates)]
3191 "--"
3192 ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
3193 :style toggle :selected org-table-buffer-is-an]))
3195 (defvar org-table--fedit-source nil
3196 "Position of the TBLFM line being edited.")
3198 ;;;###autoload
3199 (defun org-table-edit-formulas ()
3200 "Edit the formulas of the current table in a separate buffer."
3201 (interactive)
3202 (let ((at-tblfm (org-at-TBLFM-p)))
3203 (unless (or at-tblfm (org-at-table-p))
3204 (user-error "Not at a table"))
3205 (save-excursion
3206 ;; Move point within the table before analyzing it.
3207 (when at-tblfm (re-search-backward "^[ \t]*|"))
3208 (org-table-analyze))
3209 (let ((key (org-table-current-field-formula 'key 'noerror))
3210 (eql (sort (org-table-get-stored-formulas t (and at-tblfm (point)))
3211 #'org-table-formula-less-p))
3212 (pos (point-marker))
3213 (source (copy-marker (line-beginning-position)))
3214 (startline 1)
3215 (wc (current-window-configuration))
3216 (sel-win (selected-window))
3217 (titles '((column . "# Column Formulas\n")
3218 (field . "# Field and Range Formulas\n")
3219 (named . "# Named Field Formulas\n"))))
3220 (org-switch-to-buffer-other-window "*Edit Formulas*")
3221 (erase-buffer)
3222 ;; Keep global-font-lock-mode from turning on font-lock-mode
3223 (let ((font-lock-global-modes '(not fundamental-mode)))
3224 (fundamental-mode))
3225 (setq-local font-lock-global-modes (list 'not major-mode))
3226 (setq-local org-pos pos)
3227 (setq-local org-table--fedit-source source)
3228 (setq-local org-window-configuration wc)
3229 (setq-local org-selected-window sel-win)
3230 (use-local-map org-table-fedit-map)
3231 (add-hook 'post-command-hook #'org-table-fedit-post-command t t)
3232 (easy-menu-add org-table-fedit-menu)
3233 (setq startline (org-current-line))
3234 (dolist (entry eql)
3235 (let* ((type (cond
3236 ((string-match "\\`\\$\\([0-9]+\\|[<>]+\\)\\'"
3237 (car entry))
3238 'column)
3239 ((equal (string-to-char (car entry)) ?@) 'field)
3240 (t 'named)))
3241 (title (assq type titles)))
3242 (when title
3243 (unless (bobp) (insert "\n"))
3244 (insert
3245 (org-add-props (cdr title) nil 'face font-lock-comment-face))
3246 (setq titles (remove title titles)))
3247 (when (equal key (car entry)) (setq startline (org-current-line)))
3248 (let ((s (concat
3249 (if (memq (string-to-char (car entry)) '(?@ ?$)) "" "$")
3250 (car entry) " = " (cdr entry) "\n")))
3251 (remove-text-properties 0 (length s) '(face nil) s)
3252 (insert s))))
3253 (when (eq org-table-use-standard-references t)
3254 (org-table-fedit-toggle-ref-type))
3255 (org-goto-line startline)
3256 (message "%s" (substitute-command-keys "\\<org-mode-map>\
3257 Edit formulas, finish with `\\[org-ctrl-c-ctrl-c]' or `\\[org-edit-special]'. \
3258 See menu for more commands.")))))
3260 (defun org-table-fedit-post-command ()
3261 (when (not (memq this-command '(lisp-complete-symbol)))
3262 (let ((win (selected-window)))
3263 (save-excursion
3264 (ignore-errors (org-table-show-reference))
3265 (select-window win)))))
3267 (defun org-table-formula-to-user (s)
3268 "Convert a formula from internal to user representation."
3269 (if (eq org-table-use-standard-references t)
3270 (org-table-convert-refs-to-an s)
3273 (defun org-table-formula-from-user (s)
3274 "Convert a formula from user to internal representation."
3275 (if org-table-use-standard-references
3276 (org-table-convert-refs-to-rc s)
3279 (defun org-table-convert-refs-to-rc (s)
3280 "Convert spreadsheet references from A7 to @7$28.
3281 Works for single references, but also for entire formulas and even the
3282 full TBLFM line."
3283 (let ((start 0))
3284 (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\|\\<remote([^,)]*)\\)" s start)
3285 (cond
3286 ((match-end 3)
3287 ;; format match, just advance
3288 (setq start (match-end 0)))
3289 ((and (> (match-beginning 0) 0)
3290 (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
3291 (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
3292 ;; 3.e5 or something like this.
3293 (setq start (match-end 0)))
3294 ((or (> (- (match-end 1) (match-beginning 1)) 2)
3295 ;; (member (match-string 1 s)
3296 ;; '("arctan" "exp" "expm" "lnp" "log" "stir"))
3298 ;; function name, just advance
3299 (setq start (match-end 0)))
3301 (setq start (match-beginning 0)
3302 s (replace-match
3303 (if (equal (match-string 2 s) "&")
3304 (format "$%d" (org-letters-to-number (match-string 1 s)))
3305 (format "@%d$%d"
3306 (string-to-number (match-string 2 s))
3307 (org-letters-to-number (match-string 1 s))))
3308 t t s)))))
3311 (defun org-table-convert-refs-to-an (s)
3312 "Convert spreadsheet references from to @7$28 to AB7.
3313 Works for single references, but also for entire formulas and even the
3314 full TBLFM line."
3315 (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
3316 (setq s (replace-match
3317 (format "%s%d"
3318 (org-number-to-letters
3319 (string-to-number (match-string 2 s)))
3320 (string-to-number (match-string 1 s)))
3321 t t s)))
3322 (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
3323 (setq s (replace-match (concat "\\1"
3324 (org-number-to-letters
3325 (string-to-number (match-string 2 s))) "&")
3326 t nil s)))
3329 (defun org-letters-to-number (s)
3330 "Convert a base 26 number represented by letters into an integer.
3331 For example: AB -> 28."
3332 (let ((n 0))
3333 (setq s (upcase s))
3334 (while (> (length s) 0)
3335 (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
3336 s (substring s 1)))
3339 (defun org-number-to-letters (n)
3340 "Convert an integer into a base 26 number represented by letters.
3341 For example: 28 -> AB."
3342 (let ((s ""))
3343 (while (> n 0)
3344 (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
3345 n (/ (1- n) 26)))
3348 (defun org-table-time-string-to-seconds (s)
3349 "Convert a time string into numerical duration in seconds.
3350 S can be a string matching either -?HH:MM:SS or -?HH:MM.
3351 If S is a string representing a number, keep this number."
3352 (if (equal s "")
3354 (let (hour minus min sec res)
3355 (cond
3356 ((and (string-match "\\(-?\\)\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)" s))
3357 (setq minus (< 0 (length (match-string 1 s)))
3358 hour (string-to-number (match-string 2 s))
3359 min (string-to-number (match-string 3 s))
3360 sec (string-to-number (match-string 4 s)))
3361 (if minus
3362 (setq res (- (+ (* hour 3600) (* min 60) sec)))
3363 (setq res (+ (* hour 3600) (* min 60) sec))))
3364 ((and (not (string-match org-ts-regexp-both s))
3365 (string-match "\\(-?\\)\\([0-9]+\\):\\([0-9]+\\)" s))
3366 (setq minus (< 0 (length (match-string 1 s)))
3367 hour (string-to-number (match-string 2 s))
3368 min (string-to-number (match-string 3 s)))
3369 (if minus
3370 (setq res (- (+ (* hour 3600) (* min 60))))
3371 (setq res (+ (* hour 3600) (* min 60)))))
3372 (t (setq res (string-to-number s))))
3373 (number-to-string res))))
3375 (defun org-table-time-seconds-to-string (secs &optional output-format)
3376 "Convert a number of seconds to a time string.
3377 If OUTPUT-FORMAT is non-nil, return a number of days, hours,
3378 minutes or seconds."
3379 (let* ((secs0 (abs secs))
3380 (res
3381 (cond ((eq output-format 'days)
3382 (format "%.3f" (/ (float secs0) 86400)))
3383 ((eq output-format 'hours)
3384 (format "%.2f" (/ (float secs0) 3600)))
3385 ((eq output-format 'minutes)
3386 (format "%.1f" (/ (float secs0) 60)))
3387 ((eq output-format 'seconds)
3388 (format "%d" secs0))
3389 ((eq output-format 'hh:mm)
3390 ;; Ignore seconds
3391 (substring (format-seconds
3392 (if org-table-duration-hour-zero-padding
3393 "%.2h:%.2m:%.2s" "%h:%.2m:%.2s")
3394 secs0)
3395 0 -3))
3396 (t (format-seconds
3397 (if org-table-duration-hour-zero-padding
3398 "%.2h:%.2m:%.2s" "%h:%.2m:%.2s")
3399 secs0)))))
3400 (if (< secs 0) (concat "-" res) res)))
3402 (defun org-table-fedit-convert-buffer (function)
3403 "Convert all references in this buffer, using FUNCTION."
3404 (let ((origin (copy-marker (line-beginning-position))))
3405 (goto-char (point-min))
3406 (while (not (eobp))
3407 (insert (funcall function (buffer-substring (point) (line-end-position))))
3408 (delete-region (point) (line-end-position))
3409 (forward-line))
3410 (goto-char origin)
3411 (set-marker origin nil)))
3413 (defun org-table-fedit-toggle-ref-type ()
3414 "Convert all references in the buffer from B3 to @3$2 and back."
3415 (interactive)
3416 (setq-local org-table-buffer-is-an (not org-table-buffer-is-an))
3417 (org-table-fedit-convert-buffer
3418 (if org-table-buffer-is-an
3419 'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
3420 (message "Reference type switched to %s"
3421 (if org-table-buffer-is-an "A1 etc" "@row$column")))
3423 (defun org-table-fedit-ref-up ()
3424 "Shift the reference at point one row/hline up."
3425 (interactive)
3426 (org-table-fedit-shift-reference 'up))
3428 (defun org-table-fedit-ref-down ()
3429 "Shift the reference at point one row/hline down."
3430 (interactive)
3431 (org-table-fedit-shift-reference 'down))
3433 (defun org-table-fedit-ref-left ()
3434 "Shift the reference at point one field to the left."
3435 (interactive)
3436 (org-table-fedit-shift-reference 'left))
3438 (defun org-table-fedit-ref-right ()
3439 "Shift the reference at point one field to the right."
3440 (interactive)
3441 (org-table-fedit-shift-reference 'right))
3443 (defun org-table--rematch-and-replace (n &optional decr hline)
3444 "Re-match the group N, and replace it with the shifted reference."
3445 (or (match-end n) (user-error "Cannot shift reference in this direction"))
3446 (goto-char (match-beginning n))
3447 (and (looking-at (regexp-quote (match-string n)))
3448 (replace-match (org-table-shift-refpart (match-string 0) decr hline)
3449 t t)))
3451 (defun org-table-fedit-shift-reference (dir)
3452 (cond
3453 ((org-in-regexp "\\(\\<[a-zA-Z]\\)&")
3454 (if (memq dir '(left right))
3455 (org-table--rematch-and-replace 1 (eq dir 'left))
3456 (user-error "Cannot shift reference in this direction")))
3457 ((org-in-regexp "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
3458 ;; A B3-like reference
3459 (if (memq dir '(up down))
3460 (org-table--rematch-and-replace 2 (eq dir 'up))
3461 (org-table--rematch-and-replace 1 (eq dir 'left))))
3462 ((org-in-regexp
3463 "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
3464 ;; An internal reference
3465 (if (memq dir '(up down))
3466 (org-table--rematch-and-replace 2 (eq dir 'up) (match-end 3))
3467 (org-table--rematch-and-replace 5 (eq dir 'left))))))
3469 (defun org-table-shift-refpart (ref &optional decr hline)
3470 "Shift a reference part REF.
3471 If DECR is set, decrease the references row/column, else increase.
3472 If HLINE is set, this may be a hline reference, it certainly is not
3473 a translation reference."
3474 (save-match-data
3475 (let* ((sign (string-match "^[-+]" ref)) n)
3477 (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
3478 (cond
3479 ((and hline (string-match "^I+" ref))
3480 (setq n (string-to-number (concat sign (number-to-string (length ref)))))
3481 (setq n (+ n (if decr -1 1)))
3482 (if (= n 0) (setq n (+ n (if decr -1 1))))
3483 (if sign
3484 (setq sign (if (< n 0) "-" "+") n (abs n))
3485 (setq n (max 1 n)))
3486 (concat sign (make-string n ?I)))
3488 ((string-match "^[0-9]+" ref)
3489 (setq n (string-to-number (concat sign ref)))
3490 (setq n (+ n (if decr -1 1)))
3491 (if sign
3492 (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
3493 (number-to-string (max 1 n))))
3495 ((string-match "^[a-zA-Z]+" ref)
3496 (org-number-to-letters
3497 (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))
3499 (t (user-error "Cannot shift reference"))))))
3501 (defun org-table-fedit-toggle-coordinates ()
3502 "Toggle the display of coordinates in the referenced table."
3503 (interactive)
3504 (let ((pos (marker-position org-pos)))
3505 (with-current-buffer (marker-buffer org-pos)
3506 (save-excursion
3507 (goto-char pos)
3508 (org-table-toggle-coordinate-overlays)))))
3510 (defun org-table-fedit-finish (&optional arg)
3511 "Parse the buffer for formula definitions and install them.
3512 With prefix ARG, apply the new formulas to the table."
3513 (interactive "P")
3514 (org-table-remove-rectangle-highlight)
3515 (when org-table-use-standard-references
3516 (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
3517 (setq org-table-buffer-is-an nil))
3518 (let ((pos org-pos)
3519 (sel-win org-selected-window)
3520 (source org-table--fedit-source)
3521 eql)
3522 (goto-char (point-min))
3523 (while (re-search-forward
3524 "^\\(@[-+I<>0-9.$@]+\\|@?[0-9]+\\|\\$\\([a-zA-Z0-9]+\\|[<>]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
3525 nil t)
3526 (let ((var (match-string 1))
3527 (form (org-trim (match-string 3))))
3528 (unless (equal form "")
3529 (while (string-match "[ \t]*\n[ \t]*" form)
3530 (setq form (replace-match " " t t form)))
3531 (when (assoc var eql)
3532 (user-error "Double formulas for %s" var))
3533 (push (cons var form) eql))))
3534 (set-window-configuration org-window-configuration)
3535 (select-window sel-win)
3536 (goto-char source)
3537 (org-table-store-formulas eql)
3538 (set-marker pos nil)
3539 (set-marker source nil)
3540 (kill-buffer "*Edit Formulas*")
3541 (if arg
3542 (org-table-recalculate 'all)
3543 (message "New formulas installed - press C-u C-c C-c to apply."))))
3545 (defun org-table-fedit-abort ()
3546 "Abort editing formulas, without installing the changes."
3547 (interactive)
3548 (org-table-remove-rectangle-highlight)
3549 (let ((pos org-pos) (sel-win org-selected-window))
3550 (set-window-configuration org-window-configuration)
3551 (select-window sel-win)
3552 (goto-char pos)
3553 (move-marker pos nil)
3554 (message "Formula editing aborted without installing changes")))
3556 (defun org-table-fedit-lisp-indent ()
3557 "Pretty-print and re-indent Lisp expressions in the Formula Editor."
3558 (interactive)
3559 (let ((pos (point)) beg end ind)
3560 (beginning-of-line 1)
3561 (cond
3562 ((looking-at "[ \t]")
3563 (goto-char pos)
3564 (call-interactively 'lisp-indent-line))
3565 ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
3566 ((not (fboundp 'pp-buffer))
3567 (user-error "Cannot pretty-print. Command `pp-buffer' is not available"))
3568 ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
3569 (goto-char (- (match-end 0) 2))
3570 (setq beg (point))
3571 (setq ind (make-string (current-column) ?\ ))
3572 (condition-case nil (forward-sexp 1)
3573 (error
3574 (user-error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
3575 (setq end (point))
3576 (save-restriction
3577 (narrow-to-region beg end)
3578 (if (eq last-command this-command)
3579 (progn
3580 (goto-char (point-min))
3581 (setq this-command nil)
3582 (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
3583 (replace-match " ")))
3584 (pp-buffer)
3585 (untabify (point-min) (point-max))
3586 (goto-char (1+ (point-min)))
3587 (while (re-search-forward "^." nil t)
3588 (beginning-of-line 1)
3589 (insert ind))
3590 (goto-char (point-max))
3591 (org-delete-backward-char 1)))
3592 (goto-char beg))
3593 (t nil))))
3595 (defun org-table-fedit-line-up ()
3596 "Move cursor one line up in the window showing the table."
3597 (interactive)
3598 (org-table-fedit-move 'previous-line))
3600 (defun org-table-fedit-line-down ()
3601 "Move cursor one line down in the window showing the table."
3602 (interactive)
3603 (org-table-fedit-move 'next-line))
3605 (defun org-table-fedit-move (command)
3606 "Move the cursor in the window showing the table.
3607 Use COMMAND to do the motion, repeat if necessary to end up in a data line."
3608 (let ((org-table-allow-automatic-line-recalculation nil)
3609 (pos org-pos) (win (selected-window)) p)
3610 (select-window (get-buffer-window (marker-buffer org-pos)))
3611 (setq p (point))
3612 (call-interactively command)
3613 (while (and (org-at-table-p)
3614 (org-at-table-hline-p))
3615 (call-interactively command))
3616 (or (org-at-table-p) (goto-char p))
3617 (move-marker pos (point))
3618 (select-window win)))
3620 (defun org-table-fedit-scroll (N)
3621 (interactive "p")
3622 (let ((other-window-scroll-buffer (marker-buffer org-pos)))
3623 (scroll-other-window N)))
3625 (defun org-table-fedit-scroll-down (N)
3626 (interactive "p")
3627 (org-table-fedit-scroll (- N)))
3629 (defun org-table-add-rectangle-overlay (beg end &optional face)
3630 "Add a new overlay."
3631 (let ((ov (make-overlay beg end)))
3632 (overlay-put ov 'face (or face 'secondary-selection))
3633 (push ov org-table-rectangle-overlays)))
3635 (defun org-table-highlight-rectangle (&optional beg end face)
3636 "Highlight rectangular region in a table.
3637 When buffer positions BEG and END are provided, use them to
3638 delimit the region to highlight. Otherwise, refer to point. Use
3639 FACE, when non-nil, for the highlight."
3640 (let* ((beg (or beg (point)))
3641 (end (or end (point)))
3642 (b (min beg end))
3643 (e (max beg end))
3644 (start-coordinates
3645 (save-excursion
3646 (goto-char b)
3647 (cons (line-beginning-position) (org-table-current-column))))
3648 (end-coordinates
3649 (save-excursion
3650 (goto-char e)
3651 (cons (line-beginning-position) (org-table-current-column)))))
3652 (when (boundp 'org-show-positions)
3653 (setq org-show-positions (cons b (cons e org-show-positions))))
3654 (goto-char (car start-coordinates))
3655 (let ((column-start (min (cdr start-coordinates) (cdr end-coordinates)))
3656 (column-end (max (cdr start-coordinates) (cdr end-coordinates)))
3657 (last-row (car end-coordinates)))
3658 (while (<= (point) last-row)
3659 (when (looking-at org-table-dataline-regexp)
3660 (org-table-goto-column column-start)
3661 (skip-chars-backward "^|\n")
3662 (let ((p (point)))
3663 (org-table-goto-column column-end)
3664 (skip-chars-forward "^|\n")
3665 (org-table-add-rectangle-overlay p (point) face)))
3666 (forward-line)))
3667 (goto-char (car start-coordinates)))
3668 (add-hook 'before-change-functions #'org-table-remove-rectangle-highlight))
3670 (defun org-table-remove-rectangle-highlight (&rest _ignore)
3671 "Remove the rectangle overlays."
3672 (unless org-inhibit-highlight-removal
3673 (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
3674 (mapc 'delete-overlay org-table-rectangle-overlays)
3675 (setq org-table-rectangle-overlays nil)))
3677 (defvar-local org-table-coordinate-overlays nil
3678 "Collects the coordinate grid overlays, so that they can be removed.")
3680 (defun org-table-overlay-coordinates ()
3681 "Add overlays to the table at point, to show row/column coordinates."
3682 (interactive)
3683 (mapc 'delete-overlay org-table-coordinate-overlays)
3684 (setq org-table-coordinate-overlays nil)
3685 (save-excursion
3686 (let ((id 0) (ih 0) hline eol str ov)
3687 (goto-char (org-table-begin))
3688 (while (org-at-table-p)
3689 (setq eol (point-at-eol))
3690 (setq ov (make-overlay (point-at-bol) (1+ (point-at-bol))))
3691 (push ov org-table-coordinate-overlays)
3692 (setq hline (looking-at org-table-hline-regexp))
3693 (setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
3694 (format "%4d" (setq id (1+ id)))))
3695 (org-overlay-before-string ov str 'org-special-keyword 'evaporate)
3696 (when hline
3697 (let ((ic 0))
3698 (while (re-search-forward "[+|]\\(-+\\)" eol t)
3699 (cl-incf ic)
3700 (let* ((beg (1+ (match-beginning 0)))
3701 (s1 (format "$%d" ic))
3702 (s2 (org-number-to-letters ic))
3703 (str (if (eq t org-table-use-standard-references) s2 s1))
3704 (ov (make-overlay beg (+ beg (length str)))))
3705 (push ov org-table-coordinate-overlays)
3706 (org-overlay-display ov str 'org-special-keyword 'evaporate)))))
3707 (forward-line)))))
3709 ;;;###autoload
3710 (defun org-table-toggle-coordinate-overlays ()
3711 "Toggle the display of Row/Column numbers in tables."
3712 (interactive)
3713 (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
3714 (message "Tables Row/Column numbers display turned %s"
3715 (if org-table-overlay-coordinates "on" "off"))
3716 (when (and (org-at-table-p) org-table-overlay-coordinates)
3717 (org-table-align))
3718 (unless org-table-overlay-coordinates
3719 (mapc 'delete-overlay org-table-coordinate-overlays)
3720 (setq org-table-coordinate-overlays nil)))
3722 ;;;###autoload
3723 (defun org-table-toggle-formula-debugger ()
3724 "Toggle the formula debugger in tables."
3725 (interactive)
3726 (setq org-table-formula-debug (not org-table-formula-debug))
3727 (message "Formula debugging has been turned %s"
3728 (if org-table-formula-debug "on" "off")))
3731 ;;; Columns Shrinking
3733 (defun org-table--shrunk-field ()
3734 "Non-nil if current field is narrowed.
3735 When non-nil, return the overlay narrowing the field."
3736 (cl-some (lambda (o)
3737 (and (eq 'table-column-hide (overlay-get o 'org-overlay-type))
3739 (overlays-at (save-excursion
3740 (skip-chars-forward (if (org-at-table-hline-p) "^+|"
3741 "^|")
3742 (line-end-position))
3743 (1- (point))))))
3745 (defun org-table--list-shrunk-columns ()
3746 "List currently shrunk columns in table at point."
3747 (save-excursion
3748 ;; We really check shrunk columns in current row only. It could
3749 ;; be wrong if all rows do not contain the same number of columns
3750 ;; (i.e. the table is not properly aligned). As a consequence,
3751 ;; some columns may not be shrunk again upon aligning the table.
3753 ;; For example, in the following table, cursor is on first row and
3754 ;; "<>" indicates a shrunk column.
3756 ;; | |
3757 ;; | | <> |
3759 ;; Aligning table from the first row will not shrink again the
3760 ;; second row, which was not visible initially.
3762 ;; However, fixing it requires to check every row, which may be
3763 ;; slow on large tables. Moreover, the hindrance of this
3764 ;; pathological case is very limited.
3765 (beginning-of-line)
3766 (search-forward "|")
3767 (let ((separator (if (org-at-table-hline-p) "+" "|"))
3768 (column 1)
3769 (shrunk (and (org-table--shrunk-field) (list 1)))
3770 (end (line-end-position)))
3771 (while (search-forward separator end t)
3772 (cl-incf column)
3773 (when (org-table--shrunk-field) (push column shrunk)))
3774 (nreverse shrunk))))
3776 (defun org-table--make-shrinking-overlay (start end display field &optional pre)
3777 "Create an overlay to shrink text between START and END.
3779 Use string DISPLAY instead of the real text between the two
3780 buffer positions. FIELD is the real contents of the field, as
3781 a string, or nil. It is meant to be displayed upon moving the
3782 mouse onto the overlay.
3784 When optional argument PRE is non-nil, assume the overlay is
3785 located at the beginning of the field, and prepend
3786 `org-table-separator-space' to it. Otherwise, concatenate
3787 `org-table-shrunk-column-indicator' at its end.
3789 Return the overlay."
3790 (let ((show-before-edit
3791 (lambda (o &rest _)
3792 ;; Removing one overlay removes all other overlays in the
3793 ;; same column.
3794 (mapc #'delete-overlay
3795 (cdr (overlay-get o 'org-table-column-overlays)))))
3796 (o (make-overlay start end)))
3797 (overlay-put o 'insert-behind-hooks (list show-before-edit))
3798 (overlay-put o 'insert-in-front-hooks (list show-before-edit))
3799 (overlay-put o 'modification-hooks (list show-before-edit))
3800 (overlay-put o 'org-overlay-type 'table-column-hide)
3801 (when (stringp field) (overlay-put o 'help-echo field))
3802 ;; Make sure overlays stays on top of table coordinates overlays.
3803 ;; See `org-table-overlay-coordinates'.
3804 (overlay-put o 'priority 1)
3805 (let ((d (if pre (concat org-table-separator-space display)
3806 (concat display org-table-shrunk-column-indicator))))
3807 (org-overlay-display o d 'org-table t))
3810 (defun org-table--shrink-field (width align start end contents)
3811 "Shrink a table field to a specified width.
3813 WIDTH is an integer representing the number of characters to
3814 display, in addition to `org-table-shrunk-column-indicator'.
3815 ALIGN is the alignment of the current column, as either \"l\",
3816 \"c\" or \"r\". START and END are, respectively, the beginning
3817 and ending positions of the field. CONTENTS is its trimmed
3818 contents, as a string, or `hline' for table rules.
3820 Real field is hidden under one or two overlays. They have the
3821 following properties:
3823 `org-overlay-type'
3825 Set to `table-column-hide'. Used to identify overlays
3826 responsible for shrinking columns in a table.
3828 `org-table-column-overlays'
3830 It is a list with the pattern (siblings . COLUMN-OVERLAYS)
3831 where COLUMN-OVERLAYS is the list of all overlays hiding the
3832 same column.
3834 Whenever the text behind or next to the overlay is modified, all
3835 the overlays in the column are deleted, effectively displaying
3836 the column again.
3838 Return a list of overlays hiding the field, or nil if field is
3839 already hidden."
3840 (cond
3841 ((= start end) nil) ;no field to narrow
3842 ((org-table--shrunk-field) nil) ;already shrunk
3843 ((= 0 width) ;shrink to one character
3844 (list (org-table--make-shrinking-overlay
3845 start end "" (if (eq 'hline contents) "" contents))))
3846 ((eq contents 'hline)
3847 (list (org-table--make-shrinking-overlay
3848 start end (make-string (1+ width) ?-) "")))
3849 ((equal contents "") ;no contents to hide
3850 (list
3851 (let ((w (org-string-width (buffer-substring start end)))
3852 ;; We really want WIDTH + 2 whitespace, to include blanks
3853 ;; around fields.
3854 (full (+ 2 width)))
3855 (if (<= w full)
3856 (org-table--make-shrinking-overlay
3857 (1- end) end (make-string (- full w) ?\s) "")
3858 (org-table--make-shrinking-overlay (- end (- w full) 1) end "" "")))))
3860 ;; If the field is not empty, display exactly WIDTH characters.
3861 ;; It can mean to partly hide the field, or extend it with virtual
3862 ;; blanks. To that effect, we use one or two overlays. The
3863 ;; first, optional, one may add or hide white spaces before the
3864 ;; contents of the field. The other, mandatory, one cuts the
3865 ;; field or displays white spaces at the end of the field. It
3866 ;; also always displays `org-table-shrunk-column-indicator'.
3867 (let* ((lead (org-with-point-at start (skip-chars-forward " ")))
3868 (trail (org-with-point-at end (abs (skip-chars-backward " "))))
3869 (contents-width (org-string-width
3870 (buffer-substring (+ start lead) (- end trail)))))
3871 (cond
3872 ;; Contents are too large to fit in WIDTH character. Limit, if
3873 ;; possible, blanks at the beginning of the field to a single
3874 ;; white space, and cut the field at an appropriate location.
3875 ((<= width contents-width)
3876 (let ((pre
3877 (and (> lead 0)
3878 (org-table--make-shrinking-overlay
3879 start (+ start lead) "" contents t)))
3880 (post
3881 (org-table--make-shrinking-overlay
3882 ;; Find cut location so that WIDTH characters are
3883 ;; visible using dichotomy.
3884 (let* ((begin (+ start lead))
3885 (lower begin)
3886 (upper (1- end))
3887 ;; Compensate the absence of leading space,
3888 ;; thus preserving alignment.
3889 (width (if (= lead 0) (1+ width) width)))
3890 (catch :exit
3891 (while (> (- upper lower) 1)
3892 (let ((mean (+ (ash lower -1)
3893 (ash upper -1)
3894 (logand lower upper 1))))
3895 (pcase (org-string-width (buffer-substring begin mean))
3896 ((pred (= width)) (throw :exit mean))
3897 ((pred (< width)) (setq upper mean))
3898 (_ (setq lower mean)))))
3899 upper))
3900 end "" contents)))
3901 (if pre (list pre post) (list post))))
3902 ;; Contents fit it WIDTH characters. First compute number of
3903 ;; white spaces needed on each side of contents, then expand or
3904 ;; compact blanks on each side of the field in order to
3905 ;; preserve width and obey to alignment constraints.
3907 (let* ((required (- width contents-width))
3908 (before
3909 (pcase align
3910 ;; Compensate the absence of leading space, thus
3911 ;; preserving alignment.
3912 ((guard (= lead 0)) -1)
3913 ("l" 0)
3914 ("r" required)
3915 ("c" (/ required 2))))
3916 (after (- required before))
3917 (pre
3918 (pcase (1- lead)
3919 ((or (guard (= lead 0)) (pred (= before))) nil)
3920 ((pred (< before))
3921 (org-table--make-shrinking-overlay
3922 start (+ start (- lead before)) "" contents t))
3924 (org-table--make-shrinking-overlay
3925 start (1+ start)
3926 (make-string (- before (1- lead)) ?\s)
3927 contents t))))
3928 (post
3929 (pcase (1- trail)
3930 ((pred (= after))
3931 (org-table--make-shrinking-overlay (1- end) end "" contents))
3932 ((pred (< after))
3933 (org-table--make-shrinking-overlay
3934 (+ after (- end trail)) end "" contents))
3936 (org-table--make-shrinking-overlay
3937 (1- end) end
3938 (make-string (- after (1- trail)) ?\s)
3939 contents)))))
3940 (if pre (list pre post) (list post)))))))))
3942 (defun org-table--read-column-selection (select max)
3943 "Read column selection select as a list of numbers.
3945 SELECT is a string containing column ranges, separated by white
3946 space characters, see `org-table-hide-column' for details. MAX
3947 is the maximum column number.
3949 Return value is a sorted list of numbers. Ignore any number
3950 outside of the [1;MAX] range."
3951 (catch :all
3952 (sort
3953 (delete-dups
3954 (cl-mapcan
3955 (lambda (s)
3956 (cond
3957 ((member s '("-" "1-")) (throw :all (number-sequence 1 max)))
3958 ((string-match-p "\\`[0-9]+\\'" s)
3959 (let ((n (string-to-number s)))
3960 (and (> n 0) (<= n max) (list n))))
3961 ((string-match "\\`\\([0-9]+\\)?-\\([0-9]+\\)?\\'" s)
3962 (let ((n (match-string 1 s))
3963 (m (match-string 2 s)))
3964 (number-sequence (if n (max 1 (string-to-number n))
3966 (if m (min max (string-to-number m))
3967 max))))
3968 (t nil))) ;invalid specification
3969 (split-string select)))
3970 #'<)))
3972 (defun org-table--shrink-columns (columns beg end)
3973 "Shrink COLUMNS in a table.
3974 COLUMNS is a sorted list of column numbers. BEG and END are,
3975 respectively, the beginning position and the end position of the
3976 table."
3977 (org-with-wide-buffer
3978 (org-font-lock-ensure beg end)
3979 (dolist (c columns)
3980 (goto-char beg)
3981 (let ((align nil)
3982 (width nil)
3983 (fields nil))
3984 (while (< (point) end)
3985 (catch :continue
3986 (let* ((hline? (org-at-table-hline-p))
3987 (separator (if hline? "+" "|")))
3988 ;; Move to COLUMN.
3989 (search-forward "|")
3990 (or (= c 1) ;already there
3991 (search-forward separator (line-end-position) t (1- c))
3992 (throw :continue nil)) ;skip invalid columns
3993 ;; Extract boundaries and contents from current field.
3994 ;; Also set the column's width if we encounter a width
3995 ;; cookie for the first time.
3996 (let* ((start (point))
3997 (end (progn
3998 (skip-chars-forward (concat "^|" separator)
3999 (line-end-position))
4000 (point)))
4001 (contents (if hline? 'hline
4002 (org-trim (buffer-substring start end)))))
4003 (push (list start end contents) fields)
4004 (when (and (not hline?)
4005 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)>\\'"
4006 contents))
4007 (unless align (setq align (match-string 1 contents)))
4008 (unless width
4009 (setq width (string-to-number (match-string 2 contents))))))))
4010 (forward-line))
4011 ;; Link overlays for current field to the other overlays in the
4012 ;; same column.
4013 (let ((chain (list 'siblings)))
4014 (dolist (field fields)
4015 (dolist (new (apply #'org-table--shrink-field
4016 (or width 0) (or align "l") field))
4017 (push new (cdr chain))
4018 (overlay-put new 'org-table-column-overlays chain))))))))
4020 ;;;###autoload
4021 (defun org-table-toggle-column-width (&optional arg)
4022 "Shrink or expand current column in an Org table.
4024 If a width cookie specifies a width W for the column, the first
4025 W visible characters are displayed. Otherwise, the column is
4026 shrunk to a single character.
4028 When point is before the first column or after the last one, ask
4029 for the columns to shrink or expand, as a list of ranges.
4030 A column range can be one of the following patterns:
4032 N column N only
4033 N-M every column between N and M (both inclusive)
4034 N- every column between N (inclusive) and the last column
4035 -M every column between the first one and M (inclusive)
4036 - every column
4038 When optional argument ARG is a string, use it as white space
4039 separated list of column ranges.
4041 When called with `\\[universal-argument]' prefix, call \
4042 `org-table-shrink', i.e.,
4043 shrink columns with a width cookie and expand the others.
4045 When called with `\\[universal-argument] \\[universal-argument]' \
4046 prefix, expand all columns."
4047 (interactive "P")
4048 (unless (org-at-table-p) (user-error "Not in a table"))
4049 (let* ((begin (org-table-begin))
4050 (end (org-table-end))
4051 ;; Compute an upper bound for the number of columns.
4052 ;; Nonexistent columns are ignored anyway.
4053 (max-columns (/ (- (line-end-position) (line-beginning-position)) 2))
4054 (shrunk (org-table--list-shrunk-columns))
4055 (columns
4056 (pcase arg
4057 (`nil
4058 (if (save-excursion
4059 (skip-chars-backward "^|" (line-beginning-position))
4060 (or (bolp) (looking-at-p "[ \t]*$")))
4061 ;; Point is either before first column or past last
4062 ;; one. Ask for columns to operate on.
4063 (org-table--read-column-selection
4064 (read-string "Column ranges (e.g. 2-4 6-): ")
4065 max-columns)
4066 (list (org-table-current-column))))
4067 ((pred stringp) (org-table--read-column-selection arg max-columns))
4068 ((or `(4) `(16)) nil)
4069 (_ (user-error "Invalid argument: %S" arg)))))
4070 (pcase arg
4071 (`(4) (org-table-shrink begin end))
4072 (`(16) (org-table-expand begin end))
4074 (org-table-expand begin end)
4075 (org-table--shrink-columns
4076 (cl-set-exclusive-or columns shrunk) begin end)))))
4078 ;;;###autoload
4079 (defun org-table-shrink (&optional begin end)
4080 "Shrink all columns with a width cookie in the table at point.
4082 Columns without a width cookie are expanded.
4084 Optional arguments BEGIN and END, when non-nil, specify the
4085 beginning and end position of the current table."
4086 (interactive)
4087 (unless (or begin (org-at-table-p)) (user-error "Not at a table"))
4088 (org-with-wide-buffer
4089 (let ((begin (or begin (org-table-begin)))
4090 (end (or end (org-table-end)))
4091 (regexp "|[ \t]*<[lrc]?[0-9]+>[ \t]*\\(|\\|$\\)")
4092 (columns))
4093 (goto-char begin)
4094 (while (re-search-forward regexp end t)
4095 (goto-char (match-beginning 1))
4096 (cl-pushnew (org-table-current-column) columns))
4097 (org-table-expand begin end)
4098 ;; Make sure invisible characters in the table are at the right
4099 ;; place since column widths take them into account.
4100 (org-font-lock-ensure begin end)
4101 (org-table--shrink-columns (sort columns #'<) begin end))))
4103 ;;;###autoload
4104 (defun org-table-expand (&optional begin end)
4105 "Expand all columns in the table at point.
4106 Optional arguments BEGIN and END, when non-nil, specify the
4107 beginning and end position of the current table."
4108 (interactive)
4109 (unless (or begin (org-at-table-p)) (user-error "Not at a table"))
4110 (org-with-wide-buffer
4111 (let ((begin (or begin (org-table-begin)))
4112 (end (or end (org-table-end))))
4113 (remove-overlays begin end 'org-overlay-type 'table-column-hide))))
4116 ;;; Generic Tools
4118 ;;;###autoload
4119 (defun org-table-map-tables (f &optional quietly)
4120 "Apply function F to the start of all tables in the buffer."
4121 (org-with-point-at 1
4122 (while (re-search-forward org-table-line-regexp nil t)
4123 (let ((table (org-element-lineage (org-element-at-point) '(table) t)))
4124 (when table
4125 (unless quietly
4126 (message "Mapping tables: %d%%"
4127 (floor (* 100.0 (point)) (buffer-size))))
4128 (goto-char (org-element-property :post-affiliated table))
4129 (let ((end (copy-marker (org-element-property :end table))))
4130 (unwind-protect
4131 (progn (funcall f) (goto-char end))
4132 (set-marker end nil)))))))
4133 (unless quietly (message "Mapping tables: done")))
4135 ;;;###autoload
4136 (defun org-table-export (&optional file format)
4137 "Export table to a file, with configurable format.
4138 Such a file can be imported into usual spreadsheet programs.
4140 FILE can be the output file name. If not given, it will be taken
4141 from a TABLE_EXPORT_FILE property in the current entry or higher
4142 up in the hierarchy, or the user will be prompted for a file
4143 name. FORMAT can be an export format, of the same kind as it
4144 used when `-mode' sends a table in a different format.
4146 The command suggests a format depending on TABLE_EXPORT_FORMAT,
4147 whether it is set locally or up in the hierarchy, then on the
4148 extension of the given file name, and finally on the variable
4149 `org-table-export-default-format'."
4150 (interactive)
4151 (unless (org-at-table-p) (user-error "No table at point"))
4152 (org-table-align) ; Make sure we have everything we need.
4153 (let ((file (or file (org-entry-get (point) "TABLE_EXPORT_FILE" t))))
4154 (unless file
4155 (setq file (read-file-name "Export table to: "))
4156 (unless (or (not (file-exists-p file))
4157 (y-or-n-p (format "Overwrite file %s? " file)))
4158 (user-error "File not written")))
4159 (when (file-directory-p file)
4160 (user-error "This is a directory path, not a file"))
4161 (when (and (buffer-file-name (buffer-base-buffer))
4162 (file-equal-p
4163 (file-truename file)
4164 (file-truename (buffer-file-name (buffer-base-buffer)))))
4165 (user-error "Please specify a file name that is different from current"))
4166 (let ((fileext (concat (file-name-extension file) "$"))
4167 (format (or format (org-entry-get (point) "TABLE_EXPORT_FORMAT" t))))
4168 (unless format
4169 (let* ((formats '("orgtbl-to-tsv" "orgtbl-to-csv" "orgtbl-to-latex"
4170 "orgtbl-to-html" "orgtbl-to-generic"
4171 "orgtbl-to-texinfo" "orgtbl-to-orgtbl"
4172 "orgtbl-to-unicode"))
4173 (deffmt-readable
4174 (replace-regexp-in-string
4175 "\t" "\\t"
4176 (replace-regexp-in-string
4177 "\n" "\\n"
4178 (or (car (delq nil
4179 (mapcar
4180 (lambda (f)
4181 (and (string-match-p fileext f) f))
4182 formats)))
4183 org-table-export-default-format)
4184 t t) t t)))
4185 (setq format
4186 (org-completing-read
4187 "Format: " formats nil nil deffmt-readable))))
4188 (if (string-match "\\([^ \t\r\n]+\\)\\( +.*\\)?" format)
4189 (let ((transform (intern (match-string 1 format)))
4190 (params (and (match-end 2)
4191 (read (concat "(" (match-string 2 format) ")"))))
4192 (table (org-table-to-lisp
4193 (buffer-substring-no-properties
4194 (org-table-begin) (org-table-end)))))
4195 (unless (fboundp transform)
4196 (user-error "No such transformation function %s" transform))
4197 (let (buf)
4198 (with-current-buffer (find-file-noselect file)
4199 (setq buf (current-buffer))
4200 (erase-buffer)
4201 (fundamental-mode)
4202 (insert (funcall transform table params) "\n")
4203 (save-buffer))
4204 (kill-buffer buf))
4205 (message "Export done."))
4206 (user-error "TABLE_EXPORT_FORMAT invalid")))))
4208 ;;;###autoload
4209 (defun org-table--align-field (field width align)
4210 "Format FIELD according to column WIDTH and alignement ALIGN.
4211 FIELD is a string. WIDTH is a number. ALIGN is either \"c\",
4212 \"l\" or\"r\"."
4213 (let* ((spaces (- width (org-string-width field)))
4214 (prefix (pcase align
4215 ("l" "")
4216 ("r" (make-string spaces ?\s))
4217 ("c" (make-string (/ spaces 2) ?\s))))
4218 (suffix (make-string (- spaces (length prefix)) ?\s)))
4219 (concat org-table-separator-space
4220 prefix
4221 field
4222 suffix
4223 org-table-separator-space)))
4225 (defun org-table-align ()
4226 "Align the table at point by aligning all vertical bars."
4227 (interactive)
4228 (let ((beg (org-table-begin))
4229 (end (copy-marker (org-table-end))))
4230 (org-table-save-field
4231 ;; Make sure invisible characters in the table are at the right
4232 ;; place since column widths take them into account.
4233 (org-font-lock-ensure beg end)
4234 (move-marker org-table-aligned-begin-marker beg)
4235 (move-marker org-table-aligned-end-marker end)
4236 (goto-char beg)
4237 (org-table-with-shrunk-columns
4238 (let* ((indent (progn (looking-at "[ \t]*") (match-string 0)))
4239 ;; Table's rows as lists of fields. Rules are replaced
4240 ;; by nil. Trailing spaces are removed.
4241 (fields (mapcar
4242 (lambda (l)
4243 (cond
4244 ((string-match-p "^[ \t]*|-" l) 'sep)
4245 ((string-match-p "^[ \t]*|~" l) 'hsep)
4246 ((string-match-p "^[ \t]*|:" l) 'csep)
4247 (t (org-split-string l "[ \t]*|[ \t]*"))))
4248 (split-string (buffer-substring beg end) "\n" t)))
4249 ;; Compute number of columns. If the table contains no
4250 ;; field, create a default table and bail out.
4251 (columns-number
4252 (if fields
4253 (apply #'max (mapcar (lambda (f) (if (symbolp f)
4255 (length f))) fields))
4256 (kill-region beg end)
4257 (org-table-create org-table-default-size)
4258 (user-error "Empty table - created default table")))
4259 (widths nil)
4260 (alignments nil))
4261 ;; Compute alignment and width for each column.
4262 (dotimes (i columns-number)
4263 (let* ((max-width 1)
4264 (fixed-align? nil)
4265 (numbers 0)
4266 (non-empty 0))
4267 (dolist (row fields)
4268 (let ((cell (cond
4269 ((symbolp row) "")
4270 (t (or (nth i row) "")))))
4271 (setq max-width (max max-width (org-string-width cell)))
4272 (cond (fixed-align? nil)
4273 ((equal cell "") nil)
4274 ((string-match "\\`<\\([lrc]\\)[0-9]*>\\'" cell)
4275 (setq fixed-align? (match-string 1 cell)))
4277 (cl-incf non-empty)
4278 (when (string-match-p org-table-number-regexp cell)
4279 (cl-incf numbers))))))
4280 (push max-width widths)
4281 (push (cond
4282 (fixed-align?)
4283 ((>= numbers (* org-table-number-fraction non-empty)) "r")
4284 (t "l"))
4285 alignments)))
4286 (setq widths (nreverse widths))
4287 (setq alignments (nreverse alignments))
4288 ;; Store alignment of this table, for later editing of single
4289 ;; fields.
4290 (setq org-table-last-alignment alignments)
4291 (setq org-table-last-column-widths widths)
4292 ;; Build new table rows. Only replace rows that actually
4293 ;; changed.
4294 (dolist (row fields)
4295 (let ((previous (buffer-substring (point) (line-end-position)))
4296 (new
4297 (format "%s|%s|"
4298 indent
4299 (cond
4300 ((eq 'sep row) ;horizontal rule
4301 (mapconcat (lambda (w) (make-string (+ 2 w) ?-))
4302 widths
4303 "+"))
4304 ((eq 'hsep row) ;horizontal separator
4305 (mapconcat (lambda (w) (make-string (+ 2 w) ?~))
4306 widths
4307 "+"))
4308 ((eq 'csep row) ;calculation separator
4309 (mapconcat (lambda (w) (make-string (+ 2 w) ?:))
4310 widths
4311 "+"))
4312 (t ;data line
4313 (let ((cells ;add missing fields
4314 (append row
4315 (make-list (- columns-number
4316 (length row))
4317 ""))))
4318 (mapconcat #'identity
4319 (cl-mapcar #'org-table--align-field
4320 cells
4321 widths
4322 alignments)
4323 "|")))))))
4324 (if (equal new previous)
4325 (forward-line)
4326 (insert new "\n")
4327 (delete-region (point) (line-beginning-position 2)))))
4328 (set-marker end nil)
4329 (when org-table-overlay-coordinates (org-table-overlay-coordinates))
4330 (setq org-table-may-need-update nil))))))
4332 ;;;###autoload
4333 (defun org-table-justify-field-maybe (&optional new)
4334 "Justify the current field, text to left, number to right.
4335 Optional argument NEW may specify text to replace the current field content."
4336 (cond
4337 ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
4338 ((org-at-table-hline-p))
4339 ((and (not new)
4340 (or (not (eq (marker-buffer org-table-aligned-begin-marker)
4341 (current-buffer)))
4342 (< (point) org-table-aligned-begin-marker)
4343 (>= (point) org-table-aligned-end-marker)))
4344 ;; This is not the same table, force a full re-align.
4345 (setq org-table-may-need-update t))
4347 ;; Realign the current field, based on previous full realign.
4348 (let ((pos (point))
4349 (col (org-table-current-column)))
4350 (when (> col 0)
4351 (skip-chars-backward "^|")
4352 (if (not (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)"))
4353 (setq org-table-may-need-update t)
4354 (let* ((align (nth (1- col) org-table-last-alignment))
4355 (width (nth (1- col) org-table-last-column-widths))
4356 (cell (match-string 0))
4357 (field (match-string 1))
4358 (properly-closed? (/= (match-beginning 2) (match-end 2)))
4359 (new-cell
4360 (save-match-data
4361 (cond (org-table-may-need-update
4362 (format " %s |" (or new field)))
4363 ((not properly-closed?)
4364 (setq org-table-may-need-update t)
4365 (format " %s |" (or new field)))
4366 ((not new)
4367 (concat (org-table--align-field field width align)
4368 "|"))
4369 ((<= (org-string-width new) width)
4370 (concat (org-table--align-field new width align)
4371 "|"))
4373 (setq org-table-may-need-update t)
4374 (format " %s |" new))))))
4375 (unless (equal new-cell cell)
4376 (let (org-table-may-need-update)
4377 (replace-match new-cell t t)))
4378 (goto-char pos))))))))
4380 ;;;###autoload
4381 (defun org-table-sort-lines
4382 (&optional with-case sorting-type getkey-func compare-func interactive?)
4383 "Sort table lines according to the column at point.
4385 The position of point indicates the column to be used for
4386 sorting, and the range of lines is the range between the nearest
4387 horizontal separator lines, or the entire table of no such lines
4388 exist. If point is before the first column, you will be prompted
4389 for the sorting column. If there is an active region, the mark
4390 specifies the first line and the sorting column, while point
4391 should be in the last line to be included into the sorting.
4393 The command then prompts for the sorting type which can be
4394 alphabetically, numerically, or by time (as given in a time stamp
4395 in the field, or as a HH:MM value). Sorting in reverse order is
4396 also possible.
4398 With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive
4399 if the locale allows for it.
4401 If SORTING-TYPE is specified when this function is called from a Lisp
4402 program, no prompting will take place. SORTING-TYPE must be a character,
4403 any of (?a ?A ?n ?N ?t ?T ?f ?F) where the capital letters indicate that
4404 sorting should be done in reverse order.
4406 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies
4407 a function to be called to extract the key. It must return a value
4408 that is compatible with COMPARE-FUNC, the function used to compare
4409 entries.
4411 A non-nil value for INTERACTIVE? is used to signal that this
4412 function is being called interactively."
4413 (interactive (list current-prefix-arg nil nil nil t))
4414 (when (org-region-active-p) (goto-char (region-beginning)))
4415 ;; Point must be either within a field or before a data line.
4416 (save-excursion
4417 (skip-chars-backward " \t")
4418 (when (bolp) (search-forward "|" (line-end-position) t))
4419 (org-table-check-inside-data-field))
4420 ;; Set appropriate case sensitivity and column used for sorting.
4421 (let ((column (let ((c (org-table-current-column)))
4422 (cond ((> c 0) c)
4423 (interactive?
4424 (read-number "Use column N for sorting: "))
4425 (t 1))))
4426 (sorting-type
4427 (or sorting-type
4428 (read-char-exclusive "Sort Table: [a]lphabetic, [n]umeric, \
4429 \[t]ime, [f]unc. A/N/T/F means reversed: ")))
4430 (start (org-table-begin))
4431 (end (org-table-end)))
4432 (save-restriction
4433 ;; Narrow buffer to appropriate sorting area.
4434 (if (org-region-active-p)
4435 (progn (goto-char (region-beginning))
4436 (narrow-to-region
4437 (point)
4438 (save-excursion (goto-char (region-end))
4439 (line-beginning-position 2))))
4440 (narrow-to-region
4441 (save-excursion
4442 (if (re-search-backward org-table-hline-regexp start t)
4443 (line-beginning-position 2)
4444 start))
4445 (if (save-excursion (re-search-forward org-table-hline-regexp end t))
4446 (match-beginning 0)
4447 end)))
4448 ;; Determine arguments for `sort-subr'. Also record original
4449 ;; position. `org-table-save-field' cannot help here since
4450 ;; sorting is too much destructive.
4451 (let* ((coordinates
4452 (cons (count-lines (point-min) (line-beginning-position))
4453 (current-column)))
4454 (extract-key-from-field
4455 ;; Function to be called on the contents of the field
4456 ;; used for sorting in the current row.
4457 (cl-case sorting-type
4458 ((?n ?N) #'string-to-number)
4459 ((?a ?A) #'org-sort-remove-invisible)
4460 ((?t ?T)
4461 (lambda (f)
4462 (cond ((string-match org-ts-regexp-both f)
4463 (float-time
4464 (org-time-string-to-time (match-string 0 f))))
4465 ((org-duration-p f) (org-duration-to-minutes f))
4466 ((string-match "\\<[0-9]+:[0-9]\\{2\\}\\>" f)
4467 (org-duration-to-minutes (match-string 0 f)))
4468 (t 0))))
4469 ((?f ?F)
4470 (or getkey-func
4471 (and interactive?
4472 (org-read-function "Function for extracting keys: "))
4473 (error "Missing key extractor to sort rows")))
4474 (t (user-error "Invalid sorting type `%c'" sorting-type))))
4475 (predicate
4476 (cl-case sorting-type
4477 ((?n ?N ?t ?T) #'<)
4478 ((?a ?A) (if with-case #'org-string-collate-lessp
4479 (lambda (s1 s2) (org-string-collate-lessp s1 s2 nil t))))
4480 ((?f ?F)
4481 (or compare-func
4482 (and interactive?
4483 (org-read-function
4484 "Function for comparing keys (empty for default \
4485 `sort-subr' predicate): "
4486 'allow-empty))))))
4487 (shrunk-columns (remq column (org-table--list-shrunk-columns))))
4488 (goto-char (point-min))
4489 (sort-subr (memq sorting-type '(?A ?N ?T ?F))
4490 (lambda ()
4491 (forward-line)
4492 (while (and (not (eobp))
4493 (not (looking-at org-table-dataline-regexp)))
4494 (forward-line)))
4495 #'end-of-line
4496 (lambda ()
4497 (funcall extract-key-from-field
4498 (org-trim (org-table-get-field column))))
4500 predicate)
4501 ;; Hide all columns but the one being sorted.
4502 (org-table--shrink-columns shrunk-columns start end)
4503 ;; Move back to initial field.
4504 (forward-line (car coordinates))
4505 (move-to-column (cdr coordinates))))))
4507 (defun org-table-transpose-table-at-point ()
4508 "Transpose Org table at point and eliminate hlines.
4509 So a table like
4511 | 1 | 2 | 4 | 5 |
4512 |---+---+---+---|
4513 | a | b | c | d |
4514 | e | f | g | h |
4516 will be transposed as
4518 | 1 | a | e |
4519 | 2 | b | f |
4520 | 4 | c | g |
4521 | 5 | d | h |
4523 Note that horizontal lines disappear."
4524 (interactive)
4525 (let* ((table (delete 'hline (org-table-to-lisp)))
4526 (dline_old (org-table-current-line))
4527 (col_old (org-table-current-column))
4528 (contents (mapcar (lambda (_)
4529 (let ((tp table))
4530 (mapcar
4531 (lambda (_)
4532 (prog1
4533 (pop (car tp))
4534 (setq tp (cdr tp))))
4535 table)))
4536 (car table))))
4537 (goto-char (org-table-begin))
4538 (re-search-forward "|")
4539 (backward-char)
4540 (delete-region (point) (org-table-end))
4541 (insert (mapconcat
4542 (lambda(x)
4543 (concat "| " (mapconcat 'identity x " | " ) " |\n" ))
4544 contents ""))
4545 (org-table-goto-line col_old)
4546 (org-table-goto-column dline_old))
4547 (org-table-align))
4549 ;;;###autoload
4550 (defun org-table-wrap-region (arg)
4551 "Wrap several fields in a column like a paragraph.
4552 This is useful if you'd like to spread the contents of a field over several
4553 lines, in order to keep the table compact.
4555 If there is an active region, and both point and mark are in the same column,
4556 the text in the column is wrapped to minimum width for the given number of
4557 lines. Generally, this makes the table more compact. A prefix ARG may be
4558 used to change the number of desired lines. For example, \
4559 `C-2 \\[org-table-wrap-region]'
4560 formats the selected text to two lines. If the region was longer than two
4561 lines, the remaining lines remain empty. A negative prefix argument reduces
4562 the current number of lines by that amount. The wrapped text is pasted back
4563 into the table. If you formatted it to more lines than it was before, fields
4564 further down in the table get overwritten - so you might need to make space in
4565 the table first.
4567 If there is no region, the current field is split at the cursor position and
4568 the text fragment to the right of the cursor is prepended to the field one
4569 line down.
4571 If there is no region, but you specify a prefix ARG, the current field gets
4572 blank, and the content is appended to the field above."
4573 (interactive "P")
4574 (org-table-check-inside-data-field)
4575 (if (org-region-active-p)
4576 ;; There is a region: fill as a paragraph.
4577 (let ((start (region-beginning)))
4578 (org-table-cut-region (region-beginning) (region-end))
4579 (when (> (length (car org-table-clip)) 1)
4580 (user-error "Region must be limited to single column"))
4581 (let ((nlines (cond ((not arg) (length org-table-clip))
4582 ((< arg 1) (+ (length org-table-clip) arg))
4583 (t arg))))
4584 (setq org-table-clip
4585 (mapcar #'list
4586 (org-wrap (mapconcat #'car org-table-clip " ")
4588 nlines))))
4589 (goto-char start)
4590 (org-table-paste-rectangle))
4591 ;; No region, split the current field at point.
4592 (unless (org-get-alist-option org-M-RET-may-split-line 'table)
4593 (skip-chars-forward "^\r\n|"))
4594 (cond
4595 (arg ; Combine with field above.
4596 (let ((s (org-table-blank-field))
4597 (col (org-table-current-column)))
4598 (forward-line -1)
4599 (while (org-at-table-hline-p) (forward-line -1))
4600 (org-table-goto-column col)
4601 (skip-chars-forward "^|")
4602 (skip-chars-backward " ")
4603 (insert " " (org-trim s))
4604 (org-table-align)))
4605 ((looking-at "\\([^|]+\\)+|") ; Split field.
4606 (let ((s (match-string 1)))
4607 (replace-match " |")
4608 (goto-char (match-beginning 0))
4609 (org-table-next-row)
4610 (insert (org-trim s) " ")
4611 (org-table-align)))
4612 (t (org-table-next-row)))))
4614 (defun org-table--number-for-summing (s)
4615 (let (n)
4616 (if (string-match "^ *|? *" s)
4617 (setq s (replace-match "" nil nil s)))
4618 (if (string-match " *|? *$" s)
4619 (setq s (replace-match "" nil nil s)))
4620 (setq n (string-to-number s))
4621 (cond
4622 ((and (string-match "0" s)
4623 (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
4624 ((string-match "\\`[ \t]+\\'" s) nil)
4625 ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
4626 (let ((h (string-to-number (or (match-string 1 s) "0")))
4627 (m (string-to-number (or (match-string 2 s) "0")))
4628 (s (string-to-number (or (match-string 4 s) "0"))))
4629 (if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
4630 (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
4631 ((equal n 0) nil)
4632 (t n))))
4634 ;;;###autoload
4635 (defun org-table-sum (&optional beg end nlast)
4636 "Sum numbers in region of current table column.
4637 The result will be displayed in the echo area, and will be available
4638 as kill to be inserted with \\[yank].
4640 If there is an active region, it is interpreted as a rectangle and all
4641 numbers in that rectangle will be summed. If there is no active
4642 region and point is located in a table column, sum all numbers in that
4643 column.
4645 If at least one number looks like a time HH:MM or HH:MM:SS, all other
4646 numbers are assumed to be times as well (in decimal hours) and the
4647 numbers are added as such.
4649 If NLAST is a number, only the NLAST fields will actually be summed."
4650 (interactive)
4651 (save-excursion
4652 (let (col (org-timecnt 0) diff h m s org-table-clip)
4653 (cond
4654 ((and beg end)) ; beg and end given explicitly
4655 ((org-region-active-p)
4656 (setq beg (region-beginning) end (region-end)))
4658 (setq col (org-table-current-column))
4659 (goto-char (org-table-begin))
4660 (unless (re-search-forward "^[ \t]*|[^-]" nil t)
4661 (user-error "No table data"))
4662 (org-table-goto-column col)
4663 (setq beg (point))
4664 (goto-char (org-table-end))
4665 (unless (re-search-backward "^[ \t]*|[^-]" nil t)
4666 (user-error "No table data"))
4667 (org-table-goto-column col)
4668 (setq end (point))))
4669 (let* ((items (apply 'append (org-table-copy-region beg end)))
4670 (items1 (cond ((not nlast) items)
4671 ((>= nlast (length items)) items)
4672 (t (setq items (reverse items))
4673 (setcdr (nthcdr (1- nlast) items) nil)
4674 (nreverse items))))
4675 (numbers (delq nil (mapcar #'org-table--number-for-summing
4676 items1)))
4677 (res (apply '+ numbers))
4678 (sres (if (= org-timecnt 0)
4679 (number-to-string res)
4680 (setq diff (* 3600 res)
4681 h (floor diff 3600) diff (mod diff 3600)
4682 m (floor diff 60) diff (mod diff 60)
4683 s diff)
4684 (format "%.0f:%02.0f:%02.0f" h m s))))
4685 (kill-new sres)
4686 (when (called-interactively-p 'interactive)
4687 (message (substitute-command-keys
4688 (format "Sum of %d items: %-20s \
4689 \(\\[yank] will insert result into buffer)"
4690 (length numbers)
4691 sres))))
4692 sres))))
4694 ;;;###autoload
4695 (defun org-table-analyze ()
4696 "Analyze table at point and store results.
4698 This function sets up the following dynamically scoped variables:
4700 `org-table-column-name-regexp',
4701 `org-table-column-names',
4702 `org-table-current-begin-pos',
4703 `org-table-current-line-types',
4704 `org-table-current-ncol',
4705 `org-table-dlines',
4706 `org-table-hlines',
4707 `org-table-local-parameters',
4708 `org-table-named-field-locations'."
4709 (let ((beg (org-table-begin))
4710 (end (org-table-end)))
4711 (save-excursion
4712 (goto-char beg)
4713 ;; Extract column names.
4714 (setq org-table-column-names nil)
4715 (when (save-excursion
4716 (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t))
4717 (let ((c 1))
4718 (dolist (name (org-split-string (match-string 1) " *| *"))
4719 (cl-incf c)
4720 (when (string-match "\\`[a-zA-Z][_a-zA-Z0-9]*\\'" name)
4721 (push (cons name (int-to-string c)) org-table-column-names)))))
4722 (setq org-table-column-names (nreverse org-table-column-names))
4723 (setq org-table-column-name-regexp
4724 (format "\\$\\(%s\\)\\>"
4725 (regexp-opt (mapcar #'car org-table-column-names) t)))
4726 ;; Extract local parameters.
4727 (setq org-table-local-parameters nil)
4728 (save-excursion
4729 (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
4730 (dolist (field (org-split-string (match-string 1) " *| *"))
4731 (when (string-match
4732 "\\`\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
4733 (push (cons (match-string 1 field) (match-string 2 field))
4734 org-table-local-parameters)))))
4735 ;; Update named fields locations. We minimize `count-lines'
4736 ;; processing by storing last known number of lines in LAST.
4737 (setq org-table-named-field-locations nil)
4738 (save-excursion
4739 (let ((last (cons (point) 0)))
4740 (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
4741 (let ((c (match-string 1))
4742 (fields (org-split-string (match-string 2) " *| *")))
4743 (save-excursion
4744 (forward-line (if (equal c "_") 1 -1))
4745 (let ((fields1
4746 (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
4747 (org-split-string (match-string 1) " *| *")))
4748 (line (cl-incf (cdr last) (count-lines (car last) (point))))
4749 (col 1))
4750 (setcar last (point)) ; Update last known position.
4751 (while (and fields fields1)
4752 (let ((field (pop fields))
4753 (v (pop fields1)))
4754 (cl-incf col)
4755 (when (and (stringp field)
4756 (stringp v)
4757 (string-match "\\`[a-zA-Z][_a-zA-Z0-9]*\\'"
4758 field))
4759 (push (cons field v) org-table-local-parameters)
4760 (push (list field line col)
4761 org-table-named-field-locations))))))))))
4762 ;; Re-use existing markers when possible.
4763 (if (markerp org-table-current-begin-pos)
4764 (move-marker org-table-current-begin-pos (point))
4765 (setq org-table-current-begin-pos (point-marker)))
4766 ;; Analyze the line types.
4767 (let ((l 0) hlines dlines types)
4768 (while (looking-at "[ \t]*|\\(-\\)?")
4769 (push (if (match-end 1) 'hline 'dline) types)
4770 (if (match-end 1) (push l hlines) (push l dlines))
4771 (forward-line)
4772 (cl-incf l))
4773 (push 'hline types) ; Add an imaginary extra hline to the end.
4774 (setq org-table-current-line-types (apply #'vector (nreverse types)))
4775 (setq org-table-dlines (apply #'vector (cons nil (nreverse dlines))))
4776 (setq org-table-hlines (apply #'vector (cons nil (nreverse hlines)))))
4777 ;; Get the number of columns from the first data line in table.
4778 (goto-char beg)
4779 (forward-line (aref org-table-dlines 1))
4780 (let* ((fields
4781 (org-split-string
4782 (buffer-substring (line-beginning-position) (line-end-position))
4783 "[ \t]*|[ \t]*"))
4784 (nfields (length fields))
4785 al al2)
4786 (setq org-table-current-ncol nfields)
4787 (let ((last-dline
4788 (aref org-table-dlines (1- (length org-table-dlines)))))
4789 (dotimes (i nfields)
4790 (let ((column (1+ i)))
4791 (push (list (format "LR%d" column) last-dline column) al)
4792 (push (cons (format "LR%d" column) (nth i fields)) al2))))
4793 (setq org-table-named-field-locations
4794 (append org-table-named-field-locations al))
4795 (setq org-table-local-parameters
4796 (append org-table-local-parameters al2))))))
4798 (defun org-table--force-dataline ()
4799 "Move point to the closest data line in a table.
4800 Raise an error if the table contains no data line. Preserve
4801 column when moving point."
4802 (unless (org-match-line org-table-dataline-regexp)
4803 (let* ((re org-table-dataline-regexp)
4804 (column (current-column))
4805 (p1 (save-excursion (re-search-forward re (org-table-end) t)))
4806 (p2 (save-excursion (re-search-backward re (org-table-begin) t))))
4807 (cond ((and p1 p2)
4808 (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
4810 p2)))
4811 ((or p1 p2) (goto-char (or p1 p2)))
4812 (t (user-error "No table data line around here")))
4813 (org-move-to-column column))))
4815 (defun org-table-show-reference (&optional local)
4816 "Show the location/value of the $ expression at point.
4817 When LOCAL is non-nil, show references for the table at point."
4818 (interactive)
4819 (org-table-remove-rectangle-highlight)
4820 (when local (org-table-analyze))
4821 (catch 'exit
4822 (let ((pos (if local (point) org-pos))
4823 (face2 'highlight)
4824 (org-inhibit-highlight-removal t)
4825 (win (selected-window))
4826 (org-show-positions nil)
4827 var name e what match dest)
4828 (setq what (cond
4829 ((org-in-regexp "^@[0-9]+[ \t=]")
4830 (setq match (concat (substring (match-string 0) 0 -1)
4831 "$1.."
4832 (substring (match-string 0) 0 -1)
4833 "$100"))
4834 'range)
4835 ((or (org-in-regexp org-table-range-regexp2)
4836 (org-in-regexp org-table-translate-regexp)
4837 (org-in-regexp org-table-range-regexp))
4838 (setq match
4839 (save-match-data
4840 (org-table-convert-refs-to-rc (match-string 0))))
4841 'range)
4842 ((org-in-regexp "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
4843 ((org-in-regexp "\\$[0-9]+") 'column)
4844 ((not local) nil)
4845 (t (user-error "No reference at point")))
4846 match (and what (or match (match-string 0))))
4847 (when (and match (not (equal (match-beginning 0) (point-at-bol))))
4848 (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
4849 'secondary-selection))
4850 (add-hook 'before-change-functions
4851 #'org-table-remove-rectangle-highlight)
4852 (when (eq what 'name) (setq var (substring match 1)))
4853 (when (eq what 'range)
4854 (unless (eq (string-to-char match) ?@) (setq match (concat "@" match)))
4855 (setq match (org-table-formula-substitute-names match)))
4856 (unless local
4857 (save-excursion
4858 (end-of-line)
4859 (re-search-backward "^\\S-" nil t)
4860 (beginning-of-line)
4861 (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\
4862 \\([0-9]+\\|&\\)\\) *=")
4863 (setq dest
4864 (save-match-data
4865 (org-table-convert-refs-to-rc (match-string 1))))
4866 (org-table-add-rectangle-overlay
4867 (match-beginning 1) (match-end 1) face2))))
4868 (if (and (markerp pos) (marker-buffer pos))
4869 (if (get-buffer-window (marker-buffer pos))
4870 (select-window (get-buffer-window (marker-buffer pos)))
4871 (org-switch-to-buffer-other-window (get-buffer-window
4872 (marker-buffer pos)))))
4873 (goto-char pos)
4874 (org-table--force-dataline)
4875 (let ((table-start
4876 (if local org-table-current-begin-pos (org-table-begin))))
4877 (when dest
4878 (setq name (substring dest 1))
4879 (cond
4880 ((string-match-p "\\`\\$[a-zA-Z][a-zA-Z0-9]*" dest)
4881 (org-table-goto-field dest))
4882 ((string-match-p "\\`@\\([1-9][0-9]*\\)\\$\\([1-9][0-9]*\\)\\'"
4883 dest)
4884 (org-table-goto-field dest))
4885 (t (org-table-goto-column (string-to-number name))))
4886 (move-marker pos (point))
4887 (org-table-highlight-rectangle nil nil face2))
4888 (cond
4889 ((equal dest match))
4890 ((not match))
4891 ((eq what 'range)
4892 (ignore-errors (org-table-get-range match table-start nil 'highlight)))
4893 ((setq e (assoc var org-table-named-field-locations))
4894 (org-table-goto-field var)
4895 (org-table-highlight-rectangle)
4896 (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
4897 ((setq e (assoc var org-table-column-names))
4898 (org-table-goto-column (string-to-number (cdr e)))
4899 (org-table-highlight-rectangle)
4900 (goto-char table-start)
4901 (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
4902 (org-table-end) t)
4903 (progn
4904 (goto-char (match-beginning 1))
4905 (org-table-highlight-rectangle)
4906 (message "Named column (column %s)" (cdr e)))
4907 (user-error "Column name not found")))
4908 ((eq what 'column)
4909 ;; Column number.
4910 (org-table-goto-column (string-to-number (substring match 1)))
4911 (org-table-highlight-rectangle)
4912 (message "Column %s" (substring match 1)))
4913 ((setq e (assoc var org-table-local-parameters))
4914 (goto-char table-start)
4915 (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
4916 (progn
4917 (goto-char (match-beginning 1))
4918 (org-table-highlight-rectangle)
4919 (message "Local parameter."))
4920 (user-error "Parameter not found")))
4921 ((not var) (user-error "No reference at point"))
4922 ((setq e (assoc var org-table-formula-constants-local))
4923 (message "Local Constant: $%s=%s in #+CONSTANTS line."
4924 var (cdr e)))
4925 ((setq e (assoc var org-table-formula-constants))
4926 (message "Constant: $%s=%s in `org-table-formula-constants'."
4927 var (cdr e)))
4928 ((setq e (and (fboundp 'constants-get) (constants-get var)))
4929 (message "Constant: $%s=%s, from `constants.el'%s."
4930 var e (format " (%s units)" constants-unit-system)))
4931 (t (user-error "Undefined name $%s" var)))
4932 (goto-char pos)
4933 (when (and org-show-positions
4934 (not (memq this-command '(org-table-fedit-scroll
4935 org-table-fedit-scroll-down))))
4936 (push pos org-show-positions)
4937 (push table-start org-show-positions)
4938 (let ((min (apply 'min org-show-positions))
4939 (max (apply 'max org-show-positions)))
4940 (set-window-start (selected-window) min)
4941 (goto-char max)
4942 (or (pos-visible-in-window-p max)
4943 (set-window-start (selected-window) max)))))
4944 (select-window win))))
4947 ;;; The Orgtbl minor mode
4949 ;; Define a minor mode which can be used in other modes in order to
4950 ;; integrate the Org table editor.
4952 ;; This is really a hack, because the Org table editor uses several
4953 ;; keys which normally belong to the major mode, for example the TAB
4954 ;; and RET keys. Here is how it works: The minor mode defines all the
4955 ;; keys necessary to operate the table editor, but wraps the commands
4956 ;; into a function which tests if the cursor is currently inside
4957 ;; a table. If that is the case, the table editor command is
4958 ;; executed. However, when any of those keys is used outside a table,
4959 ;; the function uses `key-binding' to look up if the key has an
4960 ;; associated command in another currently active keymap (minor modes,
4961 ;; major mode, global), and executes that command. There might be
4962 ;; problems if any of the keys used by the table editor is otherwise
4963 ;; used as a prefix key.
4965 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
4966 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
4967 ;; addresses this by checking explicitly for both bindings.
4969 ;; The optimized version (see variable `orgtbl-optimized') takes over
4970 ;; all keys which are bound to `self-insert-command' in the *global map*.
4971 ;; Some modes bind other commands to simple characters, for example
4972 ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
4973 ;; active, this binding is ignored inside tables and replaced with a
4974 ;; modified self-insert.
4976 (defvar orgtbl-mode-map (make-keymap)
4977 "Keymap for `orgtbl-mode'.")
4979 (defvar org-old-auto-fill-inhibit-regexp nil
4980 "Local variable used by `orgtbl-mode'.")
4982 (defconst orgtbl-line-start-regexp
4983 "[ \t]*\\(|\\|#\\+\\(tblfm\\|orgtbl\\|tblname\\):\\)"
4984 "Matches a line belonging to an orgtbl.")
4986 (defconst orgtbl-extra-font-lock-keywords
4987 (list (list (concat "^" orgtbl-line-start-regexp ".*")
4988 0 (quote 'org-table) 'prepend))
4989 "Extra `font-lock-keywords' to be added when `orgtbl-mode' is active.")
4991 ;;;###autoload
4992 (defun turn-on-orgtbl ()
4993 "Unconditionally turn on `orgtbl-mode'."
4994 (require 'org-table)
4995 (orgtbl-mode 1))
4997 ;; Install it as a minor mode.
4998 (put 'orgtbl-mode :included t)
4999 (put 'orgtbl-mode :menu-tag "Org Table Mode")
5001 (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
5002 '("OrgTbl"
5003 ["Create or convert" org-table-create-or-convert-from-region
5004 :active (not (org-at-table-p)) :keys "C-c |" ]
5005 "--"
5006 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
5007 ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
5008 ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
5009 ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
5010 "--"
5011 ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
5012 ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
5013 ["Copy Field from Above"
5014 org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
5015 "--"
5016 ("Column"
5017 ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
5018 ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
5019 ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
5020 ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
5021 ("Row"
5022 ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
5023 ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
5024 ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
5025 ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
5026 ["Sort lines in region" org-table-sort-lines :active (org-at-table-p) :keys "C-c ^"]
5027 "--"
5028 ["Insert Header" org-table-insert-hhline :active (org-at-table-p) :keys "C-c _"]
5029 ["Insert Separator" org-table-insert-hcline :active (org-at-table-p) :keys "C-c :"]
5030 ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
5031 ("Rectangle"
5032 ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
5033 ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
5034 ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
5035 ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
5036 "--"
5037 ("Radio tables"
5038 ["Insert table template" orgtbl-insert-radio-table
5039 (cl-assoc-if #'derived-mode-p orgtbl-radio-table-templates)]
5040 ["Comment/uncomment table" orgtbl-toggle-comment t])
5041 "--"
5042 ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
5043 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
5044 ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
5045 ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
5046 ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
5047 ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
5048 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
5049 ["Sum Column/Rectangle" org-table-sum
5050 :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
5051 ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
5052 ["Debug Formulas"
5053 org-table-toggle-formula-debugger :active (org-at-table-p)
5054 :keys "C-c {"
5055 :style toggle :selected org-table-formula-debug]
5056 ["Show Col/Row Numbers"
5057 org-table-toggle-coordinate-overlays :active (org-at-table-p)
5058 :keys "C-c }"
5059 :style toggle :selected org-table-overlay-coordinates]
5060 "--"
5061 ("Plot"
5062 ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
5063 ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
5065 ;;;###autoload
5066 (define-minor-mode orgtbl-mode
5067 "The Org mode table editor as a minor mode for use in other modes."
5068 :lighter " OrgTbl" :keymap orgtbl-mode-map
5069 (org-load-modules-maybe)
5070 (cond
5071 ((derived-mode-p 'org-mode)
5072 ;; Exit without error, in case some hook functions calls this by
5073 ;; accident in Org mode.
5074 (message "Orgtbl mode is not useful in Org mode, command ignored"))
5075 (orgtbl-mode
5076 (and (orgtbl-setup) (defun orgtbl-setup () nil)) ;; FIXME: Yuck!?!
5077 ;; Make sure we are first in minor-mode-map-alist
5078 (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
5079 ;; FIXME: maybe it should use emulation-mode-map-alists?
5080 (and c (setq minor-mode-map-alist
5081 (cons c (delq c minor-mode-map-alist)))))
5082 (setq-local org-table-may-need-update t)
5083 (add-hook 'before-change-functions 'org-before-change-function
5084 nil 'local)
5085 (setq-local org-old-auto-fill-inhibit-regexp
5086 auto-fill-inhibit-regexp)
5087 (setq-local auto-fill-inhibit-regexp
5088 (if auto-fill-inhibit-regexp
5089 (concat orgtbl-line-start-regexp "\\|"
5090 auto-fill-inhibit-regexp)
5091 orgtbl-line-start-regexp))
5092 (when (fboundp 'font-lock-add-keywords)
5093 (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
5094 (org-restart-font-lock))
5095 (easy-menu-add orgtbl-mode-menu))
5097 (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
5098 (remove-hook 'before-change-functions 'org-before-change-function t)
5099 (when (fboundp 'font-lock-remove-keywords)
5100 (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
5101 (org-restart-font-lock))
5102 (easy-menu-remove orgtbl-mode-menu)
5103 (force-mode-line-update 'all))))
5105 (defun orgtbl-make-binding (fun n &rest keys)
5106 "Create a function for binding in the table minor mode.
5107 FUN is the command to call inside a table. N is used to create a unique
5108 command name. KEYS are keys that should be checked in for a command
5109 to execute outside of tables."
5110 (eval
5111 (list 'defun
5112 (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
5113 '(arg)
5114 (concat "In tables, run `" (symbol-name fun) "'.\n"
5115 "Outside of tables, run the binding of `"
5116 (mapconcat #'key-description keys "' or `")
5117 "'.")
5118 '(interactive "p")
5119 (list 'if
5120 '(org-at-table-p)
5121 (list 'call-interactively (list 'quote fun))
5122 (list 'let '(orgtbl-mode)
5123 (list 'call-interactively
5124 (append '(or)
5125 (mapcar (lambda (k)
5126 (list 'key-binding k))
5127 keys)
5128 '('orgtbl-error))))))))
5130 (defun orgtbl-error ()
5131 "Error when there is no default binding for a table key."
5132 (interactive)
5133 (user-error "This key has no function outside tables"))
5135 (defun orgtbl-setup ()
5136 "Setup orgtbl keymaps."
5137 (let ((nfunc 0)
5138 (bindings
5139 '(([(meta shift left)] org-table-delete-column)
5140 ([(meta left)] org-table-move-column-left)
5141 ([(meta right)] org-table-move-column-right)
5142 ([(meta shift right)] org-table-insert-column)
5143 ([(meta shift up)] org-table-kill-row)
5144 ([(meta shift down)] org-table-insert-row)
5145 ([(meta up)] org-table-move-row-up)
5146 ([(meta down)] org-table-move-row-down)
5147 ("\C-c\C-w" org-table-cut-region)
5148 ("\C-c\M-w" org-table-copy-region)
5149 ("\C-c\C-y" org-table-paste-rectangle)
5150 ("\C-c\C-w" org-table-wrap-region)
5151 ("\C-c-" org-table-insert-hline)
5152 ("\C-c~" org-table-insert-hhline)
5153 ("\C-c:" org-table-insert-hcline)
5154 ("\C-c}" org-table-toggle-coordinate-overlays)
5155 ("\C-c{" org-table-toggle-formula-debugger)
5156 ("\C-m" org-table-next-row)
5157 ([(shift return)] org-table-copy-down)
5158 ("\C-c?" org-table-field-info)
5159 ("\C-c " org-table-blank-field)
5160 ("\C-c+" org-table-sum)
5161 ("\C-c=" org-table-eval-formula)
5162 ("\C-c'" org-table-edit-formulas)
5163 ("\C-c`" org-table-edit-field)
5164 ("\C-c*" org-table-recalculate)
5165 ("\C-c^" org-table-sort-lines)
5166 ("\M-a" org-table-beginning-of-field)
5167 ("\M-e" org-table-end-of-field)
5168 ([(control ?#)] org-table-rotate-recalc-marks)))
5169 elt key fun cmd)
5170 (while (setq elt (pop bindings))
5171 (setq nfunc (1+ nfunc))
5172 (setq key (org-key (car elt))
5173 fun (nth 1 elt)
5174 cmd (orgtbl-make-binding fun nfunc key))
5175 (org-defkey orgtbl-mode-map key cmd))
5177 ;; Special treatment needed for TAB, RET and DEL
5178 (org-defkey orgtbl-mode-map [(return)]
5179 (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
5180 (org-defkey orgtbl-mode-map "\C-m"
5181 (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
5182 (org-defkey orgtbl-mode-map [(tab)]
5183 (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
5184 (org-defkey orgtbl-mode-map "\C-i"
5185 (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
5186 (org-defkey orgtbl-mode-map [(shift tab)]
5187 (orgtbl-make-binding 'org-table-previous-field 104
5188 [(shift tab)] [(tab)] "\C-i"))
5189 (org-defkey orgtbl-mode-map [backspace]
5190 (orgtbl-make-binding 'org-delete-backward-char 109
5191 [backspace] (kbd "DEL")))
5193 (org-defkey orgtbl-mode-map [S-iso-lefttab]
5194 (orgtbl-make-binding 'org-table-previous-field 107
5195 [S-iso-lefttab] [backtab] [(shift tab)]
5196 [(tab)] "\C-i"))
5198 (org-defkey orgtbl-mode-map [backtab]
5199 (orgtbl-make-binding 'org-table-previous-field 108
5200 [backtab] [S-iso-lefttab] [(shift tab)]
5201 [(tab)] "\C-i"))
5203 (org-defkey orgtbl-mode-map "\M-\C-m"
5204 (orgtbl-make-binding 'org-table-wrap-region 105
5205 "\M-\C-m" [(meta return)]))
5206 (org-defkey orgtbl-mode-map [(meta return)]
5207 (orgtbl-make-binding 'org-table-wrap-region 106
5208 [(meta return)] "\M-\C-m"))
5210 (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
5211 (org-defkey orgtbl-mode-map "\C-c|" 'orgtbl-create-or-convert-from-region)
5213 (when orgtbl-optimized
5214 ;; If the user wants maximum table support, we need to hijack
5215 ;; some standard editing functions
5216 (org-remap orgtbl-mode-map
5217 'self-insert-command 'orgtbl-self-insert-command
5218 'delete-char 'org-delete-char
5219 'delete-backward-char 'org-delete-backward-char)
5220 (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
5223 (defun orgtbl-ctrl-c-ctrl-c (arg)
5224 "If the cursor is inside a table, realign the table.
5225 If it is a table to be sent away to a receiver, do it.
5226 With prefix arg, also recompute table."
5227 (interactive "P")
5228 (let ((case-fold-search t) (pos (point)) action)
5229 (save-excursion
5230 (beginning-of-line 1)
5231 (setq action (cond
5232 ((looking-at "[ \t]*#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
5233 ((looking-at "[ \t]*|") pos)
5234 ((looking-at "[ \t]*#\\+tblfm:") 'recalc))))
5235 (cond
5236 ((integerp action)
5237 (goto-char action)
5238 (org-table-maybe-eval-formula)
5239 (if arg
5240 (call-interactively 'org-table-recalculate)
5241 (org-table-maybe-recalculate-line))
5242 (call-interactively 'org-table-align)
5243 (when (orgtbl-send-table 'maybe)
5244 (run-hooks 'orgtbl-after-send-table-hook)))
5245 ((eq action 'recalc)
5246 (save-excursion
5247 (beginning-of-line 1)
5248 (skip-chars-backward " \r\n\t")
5249 (if (org-at-table-p)
5250 (org-call-with-arg 'org-table-recalculate t))))
5251 (t (let (orgtbl-mode)
5252 (call-interactively (key-binding "\C-c\C-c")))))))
5254 (defun orgtbl-create-or-convert-from-region (_arg)
5255 "Create table or convert region to table, if no conflicting binding.
5256 This installs the table binding `C-c |', but only if there is no
5257 conflicting binding to this key outside orgtbl-mode."
5258 (interactive "P")
5259 (let* (orgtbl-mode (cmd (key-binding "\C-c|")))
5260 (if cmd
5261 (call-interactively cmd)
5262 (call-interactively 'org-table-create-or-convert-from-region))))
5264 (defun orgtbl-tab (arg)
5265 "Justification and field motion for `orgtbl-mode'."
5266 (interactive "P")
5267 (if arg (org-table-edit-field t)
5268 (org-table-justify-field-maybe)
5269 (org-table-next-field)))
5271 (defun orgtbl-ret ()
5272 "Justification and field motion for `orgtbl-mode'."
5273 (interactive)
5274 (if (bobp)
5275 (newline)
5276 (org-table-justify-field-maybe)
5277 (org-table-next-row)))
5279 (defun orgtbl-self-insert-command (N)
5280 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
5281 If the cursor is in a table looking at whitespace, the whitespace is
5282 overwritten, and the table is not marked as requiring realignment."
5283 (interactive "p")
5284 (if (and (org-at-table-p)
5286 (and org-table-auto-blank-field
5287 (member last-command
5288 '(orgtbl-hijacker-command-100
5289 orgtbl-hijacker-command-101
5290 orgtbl-hijacker-command-102
5291 orgtbl-hijacker-command-103
5292 orgtbl-hijacker-command-104
5293 orgtbl-hijacker-command-105
5294 yas/expand))
5295 (org-table-blank-field))
5297 (eq N 1)
5298 (looking-at "[^|\n]* \\( \\)|"))
5299 (let (org-table-may-need-update)
5300 (delete-region (match-beginning 1) (match-end 1))
5301 (self-insert-command N))
5302 (setq org-table-may-need-update t)
5303 (let* (orgtbl-mode
5305 (cmd (or (key-binding
5306 (or (and (listp function-key-map)
5307 (setq a (assoc last-input-event function-key-map))
5308 (cdr a))
5309 (vector last-input-event)))
5310 'self-insert-command)))
5311 (call-interactively cmd)
5312 (if (and org-self-insert-cluster-for-undo
5313 (eq cmd 'self-insert-command))
5314 (if (not (eq last-command 'orgtbl-self-insert-command))
5315 (setq org-self-insert-command-undo-counter 1)
5316 (if (>= org-self-insert-command-undo-counter 20)
5317 (setq org-self-insert-command-undo-counter 1)
5318 (and (> org-self-insert-command-undo-counter 0)
5319 buffer-undo-list
5320 (not (cadr buffer-undo-list)) ; remove nil entry
5321 (setcdr buffer-undo-list (cddr buffer-undo-list)))
5322 (setq org-self-insert-command-undo-counter
5323 (1+ org-self-insert-command-undo-counter))))))))
5325 ;;;###autoload
5326 (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
5327 "Regular expression matching exponentials as produced by calc.")
5329 (defun orgtbl-gather-send-defs ()
5330 "Gather a plist of :name, :transform, :params for each destination before
5331 a radio table."
5332 (save-excursion
5333 (goto-char (org-table-begin))
5334 (let (rtn)
5335 (beginning-of-line 0)
5336 (while (looking-at "[ \t]*#\\+ORGTBL[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
5337 (let ((name (org-no-properties (match-string 1)))
5338 (transform (intern (match-string 2)))
5339 (params (if (match-end 3)
5340 (read (concat "(" (match-string 3) ")")))))
5341 (push (list :name name :transform transform :params params)
5342 rtn)
5343 (beginning-of-line 0)))
5344 rtn)))
5346 (defun orgtbl-send-replace-tbl (name text)
5347 "Find and replace table NAME with TEXT."
5348 (save-excursion
5349 (goto-char (point-min))
5350 (let* ((location-flag nil)
5351 (name (regexp-quote name))
5352 (begin-re (format "BEGIN +RECEIVE +ORGTBL +%s\\([ \t]\\|$\\)" name))
5353 (end-re (format "END +RECEIVE +ORGTBL +%s\\([ \t]\\|$\\)" name)))
5354 (while (re-search-forward begin-re nil t)
5355 (unless location-flag (setq location-flag t))
5356 (let ((beg (line-beginning-position 2)))
5357 (unless (re-search-forward end-re nil t)
5358 (user-error "Cannot find end of receiver location at %d" beg))
5359 (beginning-of-line)
5360 (delete-region beg (point))
5361 (insert text "\n")))
5362 (unless location-flag
5363 (user-error "No valid receiver location found in the buffer")))))
5365 ;;;###autoload
5366 (defun org-table-to-lisp (&optional txt)
5367 "Convert the table at point to a Lisp structure.
5368 The structure will be a list. Each item is either the symbol `hline'
5369 for a horizontal separator line, or a list of field values as strings.
5370 The table is taken from the parameter TXT, or from the buffer at point."
5371 (unless (or txt (org-at-table-p)) (user-error "No table at point"))
5372 (let ((txt (or txt
5373 (buffer-substring-no-properties (org-table-begin)
5374 (org-table-end)))))
5375 (mapcar (lambda (x)
5376 (if (string-match org-table-hline-regexp x) 'hline
5377 (org-split-string (org-trim x) "\\s-*|\\s-*")))
5378 (org-split-string txt "[ \t]*\n[ \t]*"))))
5380 (defun orgtbl-send-table (&optional maybe)
5381 "Send a transformed version of table at point to the receiver position.
5382 With argument MAYBE, fail quietly if no transformation is defined
5383 for this table."
5384 (interactive)
5385 (catch 'exit
5386 (unless (org-at-table-p) (user-error "Not at a table"))
5387 ;; when non-interactive, we assume align has just happened.
5388 (when (called-interactively-p 'any) (org-table-align))
5389 (let ((dests (orgtbl-gather-send-defs))
5390 (table (org-table-to-lisp
5391 (buffer-substring-no-properties (org-table-begin)
5392 (org-table-end))))
5393 (ntbl 0))
5394 (unless dests
5395 (if maybe (throw 'exit nil)
5396 (user-error "Don't know how to transform this table")))
5397 (dolist (dest dests)
5398 (let ((name (plist-get dest :name))
5399 (transform (plist-get dest :transform))
5400 (params (plist-get dest :params)))
5401 (unless (fboundp transform)
5402 (user-error "No such transformation function %s" transform))
5403 (orgtbl-send-replace-tbl name (funcall transform table params)))
5404 (cl-incf ntbl))
5405 (message "Table converted and installed at %d receiver location%s"
5406 ntbl (if (> ntbl 1) "s" ""))
5407 (and (> ntbl 0) ntbl))))
5409 (defun org-remove-by-index (list indices &optional i0)
5410 "Remove the elements in LIST with indices in INDICES.
5411 First element has index 0, or I0 if given."
5412 (if (not indices)
5413 list
5414 (if (integerp indices) (setq indices (list indices)))
5415 (setq i0 (1- (or i0 0)))
5416 (delq :rm (mapcar (lambda (x)
5417 (setq i0 (1+ i0))
5418 (if (memq i0 indices) :rm x))
5419 list))))
5421 (defun orgtbl-toggle-comment ()
5422 "Comment or uncomment the orgtbl at point."
5423 (interactive)
5424 (let* ((case-fold-search t)
5425 (re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
5426 (re2 (concat "^" orgtbl-line-start-regexp))
5427 (commented (save-excursion (beginning-of-line 1)
5428 (cond ((looking-at re1) t)
5429 ((looking-at re2) nil)
5430 (t (user-error "Not at an org table")))))
5431 (re (if commented re1 re2))
5432 beg end)
5433 (save-excursion
5434 (beginning-of-line 1)
5435 (while (looking-at re) (beginning-of-line 0))
5436 (beginning-of-line 2)
5437 (setq beg (point))
5438 (while (looking-at re) (beginning-of-line 2))
5439 (setq end (point)))
5440 (comment-region beg end (if commented '(4) nil))))
5442 (defun orgtbl-insert-radio-table ()
5443 "Insert a radio table template appropriate for this major mode."
5444 (interactive)
5445 (let* ((e (cl-assoc-if #'derived-mode-p orgtbl-radio-table-templates))
5446 (txt (nth 1 e))
5447 name pos)
5448 (unless e (user-error "No radio table setup defined for %s" major-mode))
5449 (setq name (read-string "Table name: "))
5450 (while (string-match "%n" txt)
5451 (setq txt (replace-match name t t txt)))
5452 (or (bolp) (insert "\n"))
5453 (setq pos (point))
5454 (insert txt)
5455 (goto-char pos)))
5457 ;;;###autoload
5458 (defun orgtbl-to-generic (table params)
5459 "Convert the orgtbl-mode TABLE to some other format.
5461 This generic routine can be used for many standard cases.
5463 TABLE is a list, each entry either the symbol `hline' for
5464 a horizontal separator line, or a list of fields for that
5465 line. PARAMS is a property list of parameters that can
5466 influence the conversion.
5468 Valid parameters are:
5470 :backend, :raw
5472 Export back-end used as a basis to transcode elements of the
5473 table, when no specific parameter applies to it. It is also
5474 used to translate cells contents. You can prevent this by
5475 setting :raw property to a non-nil value.
5477 :splice
5479 When non-nil, only convert rows, not the table itself. This is
5480 equivalent to setting to the empty string both :tstart
5481 and :tend, which see.
5483 :skip
5485 When set to an integer N, skip the first N lines of the table.
5486 Horizontal separation lines do count for this parameter!
5488 :skipcols
5490 List of columns that should be skipped. If the table has
5491 a column with calculation marks, that column is automatically
5492 discarded beforehand.
5494 :hline
5496 String to be inserted on horizontal separation lines. May be
5497 nil to ignore these lines altogether.
5499 :sep
5501 Separator between two fields, as a string.
5503 Each in the following group may be either a string or a function
5504 of no arguments returning a string:
5506 :tstart, :tend
5508 Strings to start and end the table. Ignored when :splice is t.
5510 :lstart, :lend
5512 Strings to start and end a new table line.
5514 :llstart, :llend
5516 Strings to start and end the last table line. Default,
5517 respectively, to :lstart and :lend.
5519 Each in the following group may be a string or a function of one
5520 argument (either the cells in the current row, as a list of
5521 strings, or the current cell) returning a string:
5523 :lfmt
5525 Format string for an entire row, with enough %s to capture all
5526 fields. When non-nil, :lstart, :lend, and :sep are ignored.
5528 :llfmt
5530 Format for the entire last line, defaults to :lfmt.
5532 :fmt
5534 A format to be used to wrap the field, should contain %s for
5535 the original field value. For example, to wrap everything in
5536 dollars, you could use :fmt \"$%s$\". This may also be
5537 a property list with column numbers and format strings, or
5538 functions, e.g.,
5540 (:fmt (2 \"$%s$\" 4 (lambda (c) (format \"$%s$\" c))))
5542 :hlstart :hllstart :hlend :hllend :hsep :hlfmt :hllfmt :hfmt
5544 Same as above, specific for the header lines in the table.
5545 All lines before the first hline are treated as header. If
5546 any of these is not present, the data line value is used.
5548 This may be either a string or a function of two arguments:
5550 :efmt
5552 Use this format to print numbers with exponential. The format
5553 should have %s twice for inserting mantissa and exponent, for
5554 example \"%s\\\\times10^{%s}\". This may also be a property
5555 list with column numbers and format strings or functions.
5556 :fmt will still be applied after :efmt."
5557 ;; Make sure `org-export-create-backend' is available.
5558 (require 'ox)
5559 (let* ((backend (plist-get params :backend))
5560 (custom-backend
5561 ;; Build a custom back-end according to PARAMS. Before
5562 ;; defining a translator, check if there is anything to do.
5563 ;; When there isn't, let BACKEND handle the element.
5564 (org-export-create-backend
5565 :parent (or backend 'org)
5566 :transcoders
5567 `((table . ,(org-table--to-generic-table params))
5568 (table-row . ,(org-table--to-generic-row params))
5569 (table-cell . ,(org-table--to-generic-cell params))
5570 ;; Macros are not going to be expanded. However, no
5571 ;; regular back-end has a transcoder for them. We
5572 ;; provide one so they are not ignored, but displayed
5573 ;; as-is instead.
5574 (macro . (lambda (m c i) (org-element-macro-interpreter m nil))))))
5575 data info)
5576 ;; Store TABLE as Org syntax in DATA. Tolerate non-string cells.
5577 ;; Initialize communication channel in INFO.
5578 (with-temp-buffer
5579 (let ((org-inhibit-startup t)) (org-mode))
5580 (let ((standard-output (current-buffer))
5581 (org-element-use-cache nil))
5582 (dolist (e table)
5583 (cond ((eq e 'hline) (princ "|--\n"))
5584 ((consp e)
5585 (princ "| ") (dolist (c e) (princ c) (princ " |"))
5586 (princ "\n")))))
5587 ;; Add back-end specific filters, but not user-defined ones. In
5588 ;; particular, make sure to call parse-tree filters on the
5589 ;; table.
5590 (setq info
5591 (let ((org-export-filters-alist nil))
5592 (org-export-install-filters
5593 (org-combine-plists
5594 (org-export-get-environment backend nil params)
5595 `(:back-end ,(org-export-get-backend backend))))))
5596 (setq data
5597 (org-export-filter-apply-functions
5598 (plist-get info :filter-parse-tree)
5599 (org-element-map (org-element-parse-buffer) 'table
5600 #'identity nil t)
5601 info)))
5602 (when (and backend (symbolp backend) (not (org-export-get-backend backend)))
5603 (user-error "Unknown :backend value"))
5604 (when (or (not backend) (plist-get info :raw)) (require 'ox-org))
5605 ;; Handle :skip parameter.
5606 (let ((skip (plist-get info :skip)))
5607 (when skip
5608 (unless (wholenump skip) (user-error "Wrong :skip value"))
5609 (let ((n 0))
5610 (org-element-map data 'table-row
5611 (lambda (row)
5612 (if (>= n skip) t
5613 (org-element-extract-element row)
5614 (cl-incf n)
5615 nil))
5616 nil t))))
5617 ;; Handle :skipcols parameter.
5618 (let ((skipcols (plist-get info :skipcols)))
5619 (when skipcols
5620 (unless (consp skipcols) (user-error "Wrong :skipcols value"))
5621 (org-element-map data 'table
5622 (lambda (table)
5623 (let ((specialp (org-export-table-has-special-column-p table)))
5624 (dolist (row (org-element-contents table))
5625 (when (eq (org-element-property :type row) 'standard)
5626 (let ((c 1))
5627 (dolist (cell (nthcdr (if specialp 1 0)
5628 (org-element-contents row)))
5629 (when (memq c skipcols)
5630 (org-element-extract-element cell))
5631 (cl-incf c))))))))))
5632 ;; Since we are going to export using a low-level mechanism,
5633 ;; ignore special column and special rows manually.
5634 (let ((special? (org-export-table-has-special-column-p data))
5635 ignore)
5636 (org-element-map data (if special? '(table-cell table-row) 'table-row)
5637 (lambda (datum)
5638 (when (if (eq (org-element-type datum) 'table-row)
5639 (org-export-table-row-is-special-p datum nil)
5640 (org-export-first-sibling-p datum nil))
5641 (push datum ignore))))
5642 (setq info (plist-put info :ignore-list ignore)))
5643 ;; We use a low-level mechanism to export DATA so as to skip all
5644 ;; usual pre-processing and post-processing, i.e., hooks, Babel
5645 ;; code evaluation, include keywords and macro expansion. Only
5646 ;; back-end specific filters are retained.
5647 (let ((output (org-export-data-with-backend data custom-backend info)))
5648 ;; Remove final newline.
5649 (if (org-string-nw-p output) (substring-no-properties output 0 -1) ""))))
5651 (defun org-table--generic-apply (value name &optional with-cons &rest args)
5652 (cond ((null value) nil)
5653 ((functionp value) `(funcall ',value ,@args))
5654 ((stringp value)
5655 (cond ((consp (car args)) `(apply #'format ,value ,@args))
5656 (args `(format ,value ,@args))
5657 (t value)))
5658 ((and with-cons (consp value))
5659 `(let ((val (cadr (memq column ',value))))
5660 (cond ((null val) contents)
5661 ((stringp val) (format val ,@args))
5662 ((functionp val) (funcall val ,@args))
5663 (t (user-error "Wrong %s value" ,name)))))
5664 (t (user-error "Wrong %s value" name))))
5666 (defun org-table--to-generic-table (params)
5667 "Return custom table transcoder according to PARAMS.
5668 PARAMS is a plist. See `orgtbl-to-generic' for more
5669 information."
5670 (let ((backend (plist-get params :backend))
5671 (splice (plist-get params :splice))
5672 (tstart (plist-get params :tstart))
5673 (tend (plist-get params :tend)))
5674 `(lambda (table contents info)
5675 (concat
5676 ,(and tstart (not splice)
5677 `(concat ,(org-table--generic-apply tstart ":tstart") "\n"))
5678 ,(if (or (not backend) tstart tend splice) 'contents
5679 `(org-export-with-backend ',backend table contents info))
5680 ,(org-table--generic-apply (and (not splice) tend) ":tend")))))
5682 (defun org-table--to-generic-row (params)
5683 "Return custom table row transcoder according to PARAMS.
5684 PARAMS is a plist. See `orgtbl-to-generic' for more
5685 information."
5686 (let* ((backend (plist-get params :backend))
5687 (lstart (plist-get params :lstart))
5688 (llstart (plist-get params :llstart))
5689 (hlstart (plist-get params :hlstart))
5690 (hllstart (plist-get params :hllstart))
5691 (lend (plist-get params :lend))
5692 (llend (plist-get params :llend))
5693 (hlend (plist-get params :hlend))
5694 (hllend (plist-get params :hllend))
5695 (lfmt (plist-get params :lfmt))
5696 (llfmt (plist-get params :llfmt))
5697 (hlfmt (plist-get params :hlfmt))
5698 (hllfmt (plist-get params :hllfmt)))
5699 `(lambda (row contents info)
5700 (if (eq (org-element-property :type row) 'rule)
5701 ,(cond
5702 ((plist-member params :hline)
5703 (org-table--generic-apply (plist-get params :hline) ":hline"))
5704 (backend `(org-export-with-backend ',backend row nil info)))
5705 (let ((headerp ,(and (or hlfmt hlstart hlend)
5706 '(org-export-table-row-in-header-p row info)))
5707 (last-header-p
5708 ,(and (or hllfmt hllstart hllend)
5709 '(org-export-table-row-ends-header-p row info)))
5710 (lastp (not (org-export-get-next-element row info))))
5711 (when contents
5712 ;; Check if we can apply `:lfmt', `:llfmt', `:hlfmt', or
5713 ;; `:hllfmt' to CONTENTS. Otherwise, fallback on
5714 ;; `:lstart', `:lend' and their relatives.
5715 ,(let ((cells
5716 '(org-element-map row 'table-cell
5717 (lambda (cell)
5718 ;; Export all cells, without separators.
5720 ;; Use `org-export-data-with-backend'
5721 ;; instead of `org-export-data' to eschew
5722 ;; cached values, which
5723 ;; ignore :orgtbl-ignore-sep parameter.
5724 (org-export-data-with-backend
5725 cell
5726 (plist-get info :back-end)
5727 (org-combine-plists info '(:orgtbl-ignore-sep t))))
5728 info)))
5729 `(cond
5730 ,(and hllfmt
5731 `(last-header-p ,(org-table--generic-apply
5732 hllfmt ":hllfmt" nil cells)))
5733 ,(and hlfmt
5734 `(headerp ,(org-table--generic-apply
5735 hlfmt ":hlfmt" nil cells)))
5736 ,(and llfmt
5737 `(lastp ,(org-table--generic-apply
5738 llfmt ":llfmt" nil cells)))
5740 ,(if lfmt (org-table--generic-apply lfmt ":lfmt" nil cells)
5741 `(concat
5742 (cond
5743 ,(and
5744 (or hllstart hllend)
5745 `(last-header-p
5746 (concat
5747 ,(org-table--generic-apply hllstart ":hllstart")
5748 contents
5749 ,(org-table--generic-apply hllend ":hllend"))))
5750 ,(and
5751 (or hlstart hlend)
5752 `(headerp
5753 (concat
5754 ,(org-table--generic-apply hlstart ":hlstart")
5755 contents
5756 ,(org-table--generic-apply hlend ":hlend"))))
5757 ,(and
5758 (or llstart llend)
5759 `(lastp
5760 (concat
5761 ,(org-table--generic-apply llstart ":llstart")
5762 contents
5763 ,(org-table--generic-apply llend ":llend"))))
5765 ,(cond
5766 ((or lstart lend)
5767 `(concat
5768 ,(org-table--generic-apply lstart ":lstart")
5769 contents
5770 ,(org-table--generic-apply lend ":lend")))
5771 (backend
5772 `(org-export-with-backend
5773 ',backend row contents info))
5774 (t 'contents)))))))))))))))
5776 (defun org-table--to-generic-cell (params)
5777 "Return custom table cell transcoder according to PARAMS.
5778 PARAMS is a plist. See `orgtbl-to-generic' for more
5779 information."
5780 (let* ((backend (plist-get params :backend))
5781 (efmt (plist-get params :efmt))
5782 (fmt (plist-get params :fmt))
5783 (hfmt (plist-get params :hfmt))
5784 (sep (plist-get params :sep))
5785 (hsep (plist-get params :hsep)))
5786 `(lambda (cell contents info)
5787 ;; Make sure that contents are exported as Org data when :raw
5788 ;; parameter is non-nil.
5789 ,(when (and backend (plist-get params :raw))
5790 `(setq contents
5791 ;; Since we don't know what are the pseudo object
5792 ;; types defined in backend, we cannot pass them to
5793 ;; `org-element-interpret-data'. As a consequence,
5794 ;; they will be treated as pseudo elements, and will
5795 ;; have newlines appended instead of spaces.
5796 ;; Therefore, we must make sure :post-blank value is
5797 ;; really turned into spaces.
5798 (replace-regexp-in-string
5799 "\n" " "
5800 (org-trim
5801 (org-element-interpret-data
5802 (org-element-contents cell))))))
5804 (let ((headerp ,(and (or hfmt hsep)
5805 '(org-export-table-row-in-header-p
5806 (org-export-get-parent-element cell) info)))
5807 (column
5808 ;; Call costly `org-export-table-cell-address' only if
5809 ;; absolutely necessary, i.e., if one
5810 ;; of :fmt :efmt :hfmt has a "plist type" value.
5811 ,(and (cl-some (lambda (v) (integerp (car-safe v)))
5812 (list efmt hfmt fmt))
5813 '(1+ (cdr (org-export-table-cell-address cell info))))))
5814 (when contents
5815 ;; Check if we can apply `:efmt' on CONTENTS.
5816 ,(when efmt
5817 `(when (string-match orgtbl-exp-regexp contents)
5818 (let ((mantissa (match-string 1 contents))
5819 (exponent (match-string 2 contents)))
5820 (setq contents ,(org-table--generic-apply
5821 efmt ":efmt" t 'mantissa 'exponent)))))
5822 ;; Check if we can apply FMT (or HFMT) on CONTENTS.
5823 (cond
5824 ,(and hfmt `(headerp (setq contents ,(org-table--generic-apply
5825 hfmt ":hfmt" t 'contents))))
5826 ,(and fmt `(t (setq contents ,(org-table--generic-apply
5827 fmt ":fmt" t 'contents))))))
5828 ;; If a separator is provided, use it instead of BACKEND's.
5829 ;; Separators are ignored when LFMT (or equivalent) is
5830 ;; provided.
5831 ,(cond
5832 ((or hsep sep)
5833 `(if (or ,(and (not sep) '(not headerp))
5834 (plist-get info :orgtbl-ignore-sep)
5835 (not (org-export-get-next-element cell info)))
5836 ,(if (not backend) 'contents
5837 `(org-export-with-backend ',backend cell contents info))
5838 (concat contents
5839 ,(if (and sep hsep) `(if headerp ,hsep ,sep)
5840 (or hsep sep)))))
5841 (backend `(org-export-with-backend ',backend cell contents info))
5842 (t 'contents))))))
5844 ;;;###autoload
5845 (defun orgtbl-to-tsv (table params)
5846 "Convert the orgtbl-mode table to TAB separated material."
5847 (orgtbl-to-generic table (org-combine-plists '(:sep "\t") params)))
5849 ;;;###autoload
5850 (defun orgtbl-to-csv (table params)
5851 "Convert the orgtbl-mode table to CSV material.
5852 This does take care of the proper quoting of fields with comma or quotes."
5853 (orgtbl-to-generic table
5854 (org-combine-plists '(:sep "," :fmt org-quote-csv-field)
5855 params)))
5857 ;;;###autoload
5858 (defun orgtbl-to-latex (table params)
5859 "Convert the orgtbl-mode TABLE to LaTeX.
5861 TABLE is a list, each entry either the symbol `hline' for
5862 a horizontal separator line, or a list of fields for that line.
5863 PARAMS is a property list of parameters that can influence the
5864 conversion. All parameters from `orgtbl-to-generic' are
5865 supported. It is also possible to use the following ones:
5867 :booktabs
5869 When non-nil, use formal \"booktabs\" style.
5871 :environment
5873 Specify environment to use, as a string. If you use
5874 \"longtable\", you may also want to specify :language property,
5875 as a string, to get proper continuation strings."
5876 (require 'ox-latex)
5877 (orgtbl-to-generic
5878 table
5879 (org-combine-plists
5880 ;; Provide sane default values.
5881 (list :backend 'latex
5882 :latex-default-table-mode 'table
5883 :latex-tables-centered nil
5884 :latex-tables-booktabs (plist-get params :booktabs)
5885 :latex-table-scientific-notation nil
5886 :latex-default-table-environment
5887 (or (plist-get params :environment) "tabular"))
5888 params)))
5890 ;;;###autoload
5891 (defun orgtbl-to-html (table params)
5892 "Convert the orgtbl-mode TABLE to HTML.
5894 TABLE is a list, each entry either the symbol `hline' for
5895 a horizontal separator line, or a list of fields for that line.
5896 PARAMS is a property list of parameters that can influence the
5897 conversion. All parameters from `orgtbl-to-generic' are
5898 supported. It is also possible to use the following one:
5900 :attributes
5902 Attributes and values, as a plist, which will be used in
5903 <table> tag."
5904 (require 'ox-html)
5905 (orgtbl-to-generic
5906 table
5907 (org-combine-plists
5908 ;; Provide sane default values.
5909 (list :backend 'html
5910 :html-table-data-tags '("<td%s>" . "</td>")
5911 :html-table-use-header-tags-for-first-column nil
5912 :html-table-align-individual-fields t
5913 :html-table-row-tags '("<tr>" . "</tr>")
5914 :html-table-attributes
5915 (if (plist-member params :attributes)
5916 (plist-get params :attributes)
5917 '(:border "2" :cellspacing "0" :cellpadding "6" :rules "groups"
5918 :frame "hsides")))
5919 params)))
5921 ;;;###autoload
5922 (defun orgtbl-to-texinfo (table params)
5923 "Convert the orgtbl-mode TABLE to Texinfo.
5925 TABLE is a list, each entry either the symbol `hline' for
5926 a horizontal separator line, or a list of fields for that line.
5927 PARAMS is a property list of parameters that can influence the
5928 conversion. All parameters from `orgtbl-to-generic' are
5929 supported. It is also possible to use the following one:
5931 :columns
5933 Column widths, as a string. When providing column fractions,
5934 \"@columnfractions\" command can be omitted."
5935 (require 'ox-texinfo)
5936 (let ((output
5937 (orgtbl-to-generic
5938 table
5939 (org-combine-plists
5940 (list :backend 'texinfo
5941 :texinfo-tables-verbatim nil
5942 :texinfo-table-scientific-notation nil)
5943 params)))
5944 (columns (let ((w (plist-get params :columns)))
5945 (cond ((not w) nil)
5946 ((string-match-p "{\\|@columnfractions " w) w)
5947 (t (concat "@columnfractions " w))))))
5948 (if (not columns) output
5949 (replace-regexp-in-string
5950 "@multitable \\(.*\\)" columns output t nil 1))))
5952 ;;;###autoload
5953 (defun orgtbl-to-orgtbl (table params)
5954 "Convert the orgtbl-mode TABLE into another orgtbl-mode table.
5956 TABLE is a list, each entry either the symbol `hline' for
5957 a horizontal separator line, or a list of fields for that line.
5958 PARAMS is a property list of parameters that can influence the
5959 conversion. All parameters from `orgtbl-to-generic' are
5960 supported.
5962 Useful when slicing one table into many. The :hline, :sep,
5963 :lstart, and :lend provide orgtbl framing. :tstart and :tend can
5964 be set to provide ORGTBL directives for the generated table."
5965 (require 'ox-org)
5966 (orgtbl-to-generic table (org-combine-plists params (list :backend 'org))))
5968 (defun orgtbl-to-table.el (table params)
5969 "Convert the orgtbl-mode TABLE into a table.el table.
5970 TABLE is a list, each entry either the symbol `hline' for
5971 a horizontal separator line, or a list of fields for that line.
5972 PARAMS is a property list of parameters that can influence the
5973 conversion. All parameters from `orgtbl-to-generic' are
5974 supported."
5975 (with-temp-buffer
5976 (insert (orgtbl-to-orgtbl table params))
5977 (org-table-align)
5978 (replace-regexp-in-string
5979 "-|" "-+"
5980 (replace-regexp-in-string "|-" "+-" (buffer-substring 1 (buffer-size))))))
5982 (defun orgtbl-to-unicode (table params)
5983 "Convert the orgtbl-mode TABLE into a table with unicode characters.
5985 TABLE is a list, each entry either the symbol `hline' for
5986 a horizontal separator line, or a list of fields for that line.
5987 PARAMS is a property list of parameters that can influence the
5988 conversion. All parameters from `orgtbl-to-generic' are
5989 supported. It is also possible to use the following ones:
5991 :ascii-art
5993 When non-nil, use \"ascii-art-to-unicode\" package to translate
5994 the table. You can download it here:
5995 http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el.
5997 :narrow
5999 When non-nil, narrow columns width than provided width cookie,
6000 using \"=>\" as an ellipsis, just like in an Org mode buffer."
6001 (require 'ox-ascii)
6002 (orgtbl-to-generic
6003 table
6004 (org-combine-plists
6005 (list :backend 'ascii
6006 :ascii-charset 'utf-8
6007 :ascii-table-widen-columns (not (plist-get params :narrow))
6008 :ascii-table-use-ascii-art (plist-get params :ascii-art))
6009 params)))
6011 ;; Put the cursor in a column containing numerical values
6012 ;; of an Org table,
6013 ;; type C-c " a
6014 ;; A new column is added with a bar plot.
6015 ;; When the table is refreshed (C-u C-c *),
6016 ;; the plot is updated to reflect the new values.
6018 (defun orgtbl-ascii-draw (value min max &optional width characters)
6019 "Draw an ascii bar in a table.
6020 VALUE is the value to plot, it determines the width of the bar to draw.
6021 MIN is the value that will be displayed as empty (zero width bar).
6022 MAX is the value that will draw a bar filling all the WIDTH.
6023 WIDTH is the span in characters from MIN to MAX.
6024 CHARACTERS is a string that will compose the bar, with shades of grey
6025 from pure white to pure black. It defaults to a 10 characters string
6026 of regular ascii characters."
6027 (let* ((width (ceiling (or width 12)))
6028 (characters (or characters " .:;c!lhVHW"))
6029 (len (1- (length characters)))
6030 (value (float (if (numberp value)
6031 value (string-to-number value))))
6032 (relative (/ (- value min) (- max min)))
6033 (steps (round (* relative width len))))
6034 (cond ((< steps 0) "too small")
6035 ((> steps (* width len)) "too large")
6036 (t (let* ((int-division (/ steps len))
6037 (remainder (- steps (* int-division len))))
6038 (concat (make-string int-division (elt characters len))
6039 (string (elt characters remainder))))))))
6041 ;;;###autoload
6042 (defun orgtbl-ascii-plot (&optional ask)
6043 "Draw an ASCII bar plot in a column.
6045 With cursor in a column containing numerical values, this function
6046 will draw a plot in a new column.
6048 ASK, if given, is a numeric prefix to override the default 12
6049 characters width of the plot. ASK may also be the `\\[universal-argument]' \
6050 prefix,
6051 which will prompt for the width."
6052 (interactive "P")
6053 (let ((col (org-table-current-column))
6054 (min 1e999) ; 1e999 will be converted to infinity
6055 (max -1e999) ; which is the desired result
6056 (table (org-table-to-lisp))
6057 (length
6058 (cond ((consp ask)
6059 (read-number "Length of column " 12))
6060 ((numberp ask) ask)
6061 (t 12))))
6062 ;; Skip any hline a the top of table.
6063 (while (eq (car table) 'hline) (setq table (cdr table)))
6064 ;; Skip table header if any.
6065 (dolist (x (or (cdr (memq 'hline table)) table))
6066 (when (consp x)
6067 (setq x (nth (1- col) x))
6068 (when (string-match
6069 "^[-+]?\\([0-9]*[.]\\)?[0-9]*\\([eE][+-]?[0-9]+\\)?$"
6071 (setq x (string-to-number x))
6072 (when (> min x) (setq min x))
6073 (when (< max x) (setq max x)))))
6074 (org-table-insert-column)
6075 (org-table-move-column-right)
6076 (org-table-store-formulas
6077 (cons
6078 (cons
6079 (concat "$" (number-to-string (1+ col)))
6080 (format "'(%s $%s %s %s %s)"
6081 "orgtbl-ascii-draw" col min max length))
6082 (org-table-get-stored-formulas)))
6083 (org-table-recalculate t)))
6085 ;; Example of extension: unicode characters
6086 ;; Here are two examples of different styles.
6088 ;; Unicode block characters are used to give a smooth effect.
6089 ;; See http://en.wikipedia.org/wiki/Block_Elements
6090 ;; Use one of those drawing functions
6091 ;; - orgtbl-ascii-draw (the default ascii)
6092 ;; - orgtbl-uc-draw-grid (unicode with a grid effect)
6093 ;; - orgtbl-uc-draw-cont (smooth unicode)
6095 ;; This is best viewed with the "DejaVu Sans Mono" font
6096 ;; (use M-x set-frame-font).
6098 (defun orgtbl-uc-draw-grid (value min max &optional width)
6099 "Draw a bar in a table using block unicode characters.
6100 It is a variant of orgtbl-ascii-draw with Unicode block
6101 characters, for a smooth display. Bars appear as grids (to the
6102 extent the font allows)."
6103 ;; http://en.wikipedia.org/wiki/Block_Elements
6104 ;; best viewed with the "DejaVu Sans Mono" font.
6105 (orgtbl-ascii-draw value min max width
6106 " \u258F\u258E\u258D\u258C\u258B\u258A\u2589"))
6108 (defun orgtbl-uc-draw-cont (value min max &optional width)
6109 "Draw a bar in a table using block unicode characters.
6110 It is a variant of orgtbl-ascii-draw with Unicode block
6111 characters, for a smooth display. Bars are solid (to the extent
6112 the font allows)."
6113 (orgtbl-ascii-draw value min max width
6114 " \u258F\u258E\u258D\u258C\u258B\u258A\u2589\u2588"))
6116 (defun org-table-get-remote-range (name-or-id form)
6117 "Get a field value or a list of values in a range from table at ID.
6119 NAME-OR-ID may be the name of a table in the current file as set
6120 by a \"#+NAME:\" directive. The first table following this line
6121 will then be used. Alternatively, it may be an ID referring to
6122 any entry, also in a different file. In this case, the first
6123 table in that entry will be referenced.
6124 FORM is a field or range descriptor like \"@2$3\" or \"B3\" or
6125 \"@I$2..@II$2\". All the references must be absolute, not relative.
6127 The return value is either a single string for a single field, or a
6128 list of the fields in the rectangle."
6129 (save-match-data
6130 (let ((case-fold-search t) (id-loc nil)
6131 ;; Protect a bunch of variables from being overwritten by
6132 ;; the context of the remote table.
6133 org-table-column-names org-table-column-name-regexp
6134 org-table-local-parameters org-table-named-field-locations
6135 org-table-current-line-types
6136 org-table-current-begin-pos org-table-dlines
6137 org-table-current-ncol
6138 org-table-hlines
6139 org-table-last-column-widths
6140 org-table-last-alignment
6141 buffer loc)
6142 (setq form (org-table-convert-refs-to-rc form))
6143 (org-with-wide-buffer
6144 (goto-char (point-min))
6145 (if (re-search-forward
6146 (concat "^[ \t]*#\\+\\(tbl\\)?name:[ \t]*"
6147 (regexp-quote name-or-id) "[ \t]*$")
6148 nil t)
6149 (setq buffer (current-buffer) loc (match-beginning 0))
6150 (setq id-loc (org-id-find name-or-id 'marker))
6151 (unless (and id-loc (markerp id-loc))
6152 (user-error "Can't find remote table \"%s\"" name-or-id))
6153 (setq buffer (marker-buffer id-loc)
6154 loc (marker-position id-loc))
6155 (move-marker id-loc nil))
6156 (with-current-buffer buffer
6157 (org-with-wide-buffer
6158 (goto-char loc)
6159 (forward-char 1)
6160 (unless (and (re-search-forward "^\\(\\*+ \\)\\|^[ \t]*|" nil t)
6161 (not (match-beginning 1)))
6162 (user-error "Cannot find a table at NAME or ID %s" name-or-id))
6163 (org-table-analyze)
6164 (setq form (org-table-formula-substitute-names
6165 (org-table-formula-handle-first/last-rc form)))
6166 (if (and (string-match org-table-range-regexp form)
6167 (> (length (match-string 0 form)) 1))
6168 (org-table-get-range
6169 (match-string 0 form) org-table-current-begin-pos 1)
6170 form)))))))
6172 (defun org-table-remote-reference-indirection (form)
6173 "Return formula with table remote references substituted by indirection.
6174 For example \"remote($1, @>$2)\" => \"remote(year_2013, @>$1)\".
6175 This indirection works only with the format @ROW$COLUMN. The
6176 format \"B3\" is not supported because it can not be
6177 distinguished from a plain table name or ID."
6178 (let ((regexp
6179 ;; Same as in `org-table-eval-formula'.
6180 (concat "\\<remote([ \t]*\\("
6181 ;; Allow "$1", "@<", "$-1", "@<<$1" etc.
6182 "[@$][^ \t,]+"
6183 "\\)[ \t]*,[ \t]*\\([^\n)]+\\))")))
6184 (replace-regexp-in-string
6185 regexp
6186 (lambda (m)
6187 (save-match-data
6188 (let ((eq (org-table-formula-handle-first/last-rc (match-string 1 m))))
6189 (org-table-get-range
6190 (if (string-match-p "\\`\\$[0-9]+\\'" eq)
6191 (concat "@0" eq)
6192 eq)))))
6193 form t t 1)))
6195 (defmacro org-define-lookup-function (mode)
6196 (let ((mode-str (symbol-name mode))
6197 (first-p (eq mode 'first))
6198 (all-p (eq mode 'all)))
6199 (let ((plural-str (if all-p "s" "")))
6200 `(defun ,(intern (format "org-lookup-%s" mode-str)) (val s-list r-list &optional predicate)
6201 ,(format "Find %s occurrence%s of VAL in S-LIST; return corresponding element%s of R-LIST.
6202 If R-LIST is nil, return matching element%s of S-LIST.
6203 If PREDICATE is not nil, use it instead of `equal' to match VAL.
6204 Matching is done by (PREDICATE VAL S), where S is an element of S-LIST.
6205 This function is generated by a call to the macro `org-define-lookup-function'."
6206 mode-str plural-str plural-str plural-str)
6207 (let ,(let ((lvars '((p (or predicate 'equal))
6208 (sl s-list)
6209 (rl (or r-list s-list))
6210 (ret nil))))
6211 (if first-p (cons '(match-p nil) lvars) lvars))
6212 (while ,(if first-p '(and (not match-p) sl) 'sl)
6213 (when (funcall p val (car sl))
6214 ,(when first-p '(setq match-p t))
6215 (let ((rval (car rl)))
6216 (setq ret ,(if all-p '(append ret (list rval)) 'rval))))
6217 (setq sl (cdr sl) rl (cdr rl)))
6218 ret)))))
6220 (org-define-lookup-function first)
6221 (org-define-lookup-function last)
6222 (org-define-lookup-function all)
6224 (provide 'org-table)
6226 ;; Local variables:
6227 ;; generated-autoload-file: "org-loaddefs.el"
6228 ;; End:
6230 ;;; org-table.el ends here