1 ;;; org-table.el --- The Table Editor for Org -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2004-2017 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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.
40 (declare-function org-element-at-point
"org-element" ())
41 (declare-function org-element-contents
"org-element" (element))
42 (declare-function org-element-extract-element
"org-element" (element))
43 (declare-function org-element-interpret-data
"org-element" (data))
44 (declare-function org-element-lineage
"org-element"
45 (blob &optional types with-self
))
46 (declare-function org-element-map
"org-element"
48 &optional info first-match no-recursion with-affiliated
))
49 (declare-function org-element-parse-buffer
"org-element"
50 (&optional granularity visible-only
))
51 (declare-function org-element-property
"org-element" (property element
))
52 (declare-function org-element-type
"org-element" (element))
54 (declare-function org-export-create-backend
"ox" (&rest rest
) t
)
55 (declare-function org-export-data-with-backend
"ox" (data backend info
))
56 (declare-function org-export-filter-apply-functions
"ox"
58 (declare-function org-export-first-sibling-p
"ox" (blob info
))
59 (declare-function org-export-get-backend
"ox" (name))
60 (declare-function org-export-get-environment
"ox"
61 (&optional backend subtreep ext-plist
))
62 (declare-function org-export-install-filters
"ox" (info))
63 (declare-function org-export-table-has-special-column-p
"ox" (table))
64 (declare-function org-export-table-row-is-special-p
"ox" (table-row info
))
66 (declare-function calc-eval
"calc" (str &optional separator
&rest args
))
68 (defvar constants-unit-system
)
69 (defvar org-element-use-cache
)
70 (defvar org-export-filters-alist
)
71 (defvar org-table-follow-field-mode
)
72 (defvar orgtbl-mode
) ; defined below
73 (defvar orgtbl-mode-menu
) ; defined when orgtbl mode get initialized
74 (defvar sort-fold-case
)
76 (defvar orgtbl-after-send-table-hook nil
77 "Hook for functions attaching to `C-c C-c', if the table is sent.
78 This can be used to add additional functionality after the table is sent
79 to the receiver position, otherwise, if table is not sent, the functions
82 (defvar org-table-TBLFM-begin-regexp
"^[ \t]*|.*\n[ \t]*#\\+TBLFM: ")
84 (defcustom orgtbl-optimized
(eq org-enable-table-editor
'optimized
)
85 "Non-nil means use the optimized table editor version for `orgtbl-mode'.
86 In the optimized version, the table editor takes over all simple keys that
87 normally just insert a character. In tables, the characters are inserted
88 in a way to minimize disturbing the table structure (i.e. in overwrite mode
89 for empty fields). Outside tables, the correct binding of the keys is
92 The default for this option is t if the optimized version is also used in
93 Org mode. See the variable `org-enable-table-editor' for details. Changing
94 this variable requires a restart of Emacs to become effective."
98 (defcustom orgtbl-radio-table-templates
99 '((latex-mode "% BEGIN RECEIVE ORGTBL %n
100 % END RECEIVE ORGTBL %n
102 #+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
105 (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
106 @c END RECEIVE ORGTBL %n
108 #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
111 (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
112 <!-- END RECEIVE ORGTBL %n -->
114 #+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
117 (org-mode "#+ BEGIN RECEIVE ORGTBL %n
118 #+ END RECEIVE ORGTBL %n
120 #+ORGTBL: SEND %n orgtbl-to-orgtbl :splice nil :skip 0
123 "Templates for radio tables in different major modes.
124 Each template must define lines that will be treated as a comment and that
125 must contain the \"BEGIN RECEIVE ORGTBL %n\" and \"END RECEIVE ORGTBL\"
126 lines where \"%n\" will be replaced with the name of the table during
127 insertion of the template. The transformed table will later be inserted
130 The template should also contain a minimal table in a multiline comment.
131 If multiline comments are not possible in the buffer language,
132 you can pack it into a string that will not be used when the code
133 is compiled or executed. Above the table will you need a line with
134 the fixed string \"#+ORGTBL: SEND\", followed by instruction on how to
135 convert the table into a data structure useful in the
136 language of the buffer. Check the manual for the section on
137 \"Translator functions\", and more generally check out
138 http://orgmode.org/manual/Tables-in-arbitrary-syntax.html#Tables-in-arbitrary-syntax
140 All occurrences of %n in a template will be replaced with the name of the
141 table, obtained by prompting the user."
144 (list (symbol :tag
"Major mode")
145 (string :tag
"Format"))))
147 (defgroup org-table-settings nil
148 "Settings for tables in Org mode."
149 :tag
"Org Table Settings"
152 (defcustom org-table-default-size
"5x2"
153 "The default size for newly created tables, Columns x Rows."
154 :group
'org-table-settings
157 (defcustom org-table-number-regexp
158 "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|[<>]?[-+]?0[xX][0-9a-fA-F.]+\\|[<>]?[-+]?[0-9]+#[0-9a-zA-Z.]+\\|nan\\|[-+u]?inf\\)$"
159 "Regular expression for recognizing numbers in table columns.
160 If a table column contains mostly numbers, it will be aligned to the
161 right. If not, it will be aligned to the left.
163 The default value of this option is a regular expression which allows
164 anything which looks remotely like a number as used in scientific
165 context. For example, all of the following will be considered a
167 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
169 Other options offered by the customize interface are more restrictive."
170 :group
'org-table-settings
172 (const :tag
"Positive Integers"
174 (const :tag
"Integers"
176 (const :tag
"Floating Point Numbers"
177 "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
178 (const :tag
"Floating Point Number or Integer"
179 "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
180 (const :tag
"Exponential, Floating point, Integer"
181 "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
182 (const :tag
"Very General Number-Like, including hex and Calc radix"
183 "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|[<>]?[-+]?0[xX][0-9a-fA-F.]+\\|[<>]?[-+]?[0-9]+#[0-9a-zA-Z.]+\\|nan\\|[-+u]?inf\\)$")
184 (const :tag
"Very General Number-Like, including hex and Calc radix, allows comma as decimal mark"
185 "^\\([<>]?[-+^.,0-9]*[0-9][-+^.0-9eEdDx()%]*\\|[<>]?[-+]?0[xX][0-9a-fA-F.]+\\|[<>]?[-+]?[0-9]+#[0-9a-zA-Z.]+\\|nan\\|[-+u]?inf\\)$")
186 (string :tag
"Regexp:")))
188 (defcustom org-table-number-fraction
0.5
189 "Fraction of numbers in a column required to make the column align right.
190 In a column all non-white fields are considered. If at least
191 this fraction of fields is matched by `org-table-number-regexp',
192 alignment to the right border applies."
193 :group
'org-table-settings
196 (defgroup org-table-editing nil
197 "Behavior of tables during editing in Org mode."
198 :tag
"Org Table Editing"
201 (defcustom org-table-automatic-realign t
202 "Non-nil means automatically re-align table when pressing TAB or RETURN.
203 When nil, aligning is only done with `\\[org-table-align]', or after column
205 :group
'org-table-editing
208 (defcustom org-table-auto-blank-field t
209 "Non-nil means automatically blank table field when starting to type into it.
210 This only happens when typing immediately after a field motion
211 command (TAB, S-TAB or RET).
212 Only relevant when `org-enable-table-editor' is equal to `optimized'."
213 :group
'org-table-editing
216 (defcustom org-table-exit-follow-field-mode-when-leaving-table t
217 "Non-nil means automatically exit the follow mode.
218 When nil, the follow mode will stay on and be active in any table
219 the cursor enters. Since the table follow filed mode messes with the
220 window configuration, it is not recommended to set this variable to nil,
221 except maybe locally in a special file that has mostly tables with long
227 (defcustom org-table-fix-formulas-confirm nil
228 "Whether the user should confirm when Org fixes formulas."
229 :group
'org-table-editing
232 (const :tag
"with yes-or-no" yes-or-no-p
)
233 (const :tag
"with y-or-n" y-or-n-p
)
234 (const :tag
"no confirmation" nil
)))
235 (put 'org-table-fix-formulas-confirm
237 #'(lambda (x) (member x
'(yes-or-no-p y-or-n-p
))))
239 (defcustom org-table-tab-jumps-over-hlines t
240 "Non-nil means tab in the last column of a table with jump over a hline.
241 If a horizontal separator line is following the current line,
242 `org-table-next-field' can either create a new row before that line, or jump
243 over the line. When this option is nil, a new line will be created before
245 :group
'org-table-editing
248 (defgroup org-table-calculation nil
249 "Options concerning tables in Org mode."
250 :tag
"Org Table Calculation"
253 (defcustom org-table-use-standard-references
'from
254 "Non-nil means using table references like B3 instead of @3$2.
257 from accept as input, do not present for editing
258 t accept as input and present for editing"
259 :group
'org-table-calculation
261 (const :tag
"Never, don't even check user input for them" nil
)
262 (const :tag
"Always, both as user input, and when editing" t
)
263 (const :tag
"Convert user input, don't offer during editing" from
)))
265 (defcustom org-table-copy-increment t
266 "Non-nil means increment when copying current field with \
267 `\\[org-table-copy-down]'."
268 :group
'org-table-calculation
270 :package-version
'(Org .
"8.3")
272 (const :tag
"Use the difference between the current and the above fields" t
)
273 (integer :tag
"Use a number" 1)
274 (const :tag
"Don't increment the value when copying a field" nil
)))
276 (defcustom org-calc-default-modes
277 '(calc-internal-prec 12
278 calc-float-format
(float 8)
281 calc-symbolic-mode nil
282 calc-date-format
(YYYY "-" MM
"-" DD
" " Www
(" " hh
":" mm
))
283 calc-display-working-message t
285 "List with Calc mode settings for use in `calc-eval' for table formulas.
286 The list must contain alternating symbols (Calc modes variables and values).
287 Don't remove any of the default settings, just change the values. Org mode
288 relies on the variables to be present in the list."
289 :group
'org-table-calculation
292 (defcustom org-table-duration-custom-format
'hours
293 "Format for the output of calc computations like $1+$2;t.
294 The default value is `hours', and will output the results as a
295 number of hours. Other allowed values are `seconds', `minutes' and
296 `days', and the output will be a fraction of seconds, minutes or
297 days. `hh:mm' selects to use hours and minutes, ignoring seconds.
298 The `U' flag in a table formula will select this specific format for
300 :group
'org-table-calculation
302 :type
'(choice (symbol :tag
"Seconds" 'seconds
)
303 (symbol :tag
"Minutes" 'minutes
)
304 (symbol :tag
"Hours " 'hours
)
305 (symbol :tag
"Days " 'days
)
306 (symbol :tag
"HH:MM " 'hh
:mm
)))
308 (defcustom org-table-duration-hour-zero-padding t
309 "Non-nil means hours in table duration computations should be zero-padded.
310 So this is about 08:32:34 versus 8:33:34."
311 :group
'org-table-calculation
313 :package-version
'(Org .
"9.1")
317 (defcustom org-table-formula-field-format
"%s"
318 "Format for fields which contain the result of a formula.
319 For example, using \"~%s~\" will display the result within tilde
320 characters. Beware that modifying the display can prevent the
321 field from being used in another formula."
322 :group
'org-table-settings
326 (defcustom org-table-formula-evaluate-inline t
327 "Non-nil means TAB and RET evaluate a formula in current table field.
328 If the current field starts with an equal sign, it is assumed to be a formula
329 which should be evaluated as described in the manual and in the documentation
330 string of the command `org-table-eval-formula'. This feature requires the
332 When this variable is nil, formula calculation is only available through
333 the command `\\[org-table-eval-formula]'."
334 :group
'org-table-calculation
337 (defcustom org-table-formula-use-constants t
338 "Non-nil means interpret constants in formulas in tables.
339 A constant looks like `$c' or `$Grav' and will be replaced before evaluation
340 by the value given in `org-table-formula-constants', or by a value obtained
341 from the `constants.el' package."
342 :group
'org-table-calculation
345 (defcustom org-table-formula-constants nil
346 "Alist with constant names and values, for use in table formulas.
347 The car of each element is a name of a constant, without the `$' before it.
348 The cdr is the value as a string. For example, if you'd like to use the
349 speed of light in a formula, you would configure
351 (setq org-table-formula-constants \\='((\"c\" . \"299792458.\")))
353 and then use it in an equation like `$1*$c'.
355 Constants can also be defined on a per-file basis using a line like
357 #+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
358 :group
'org-table-calculation
360 (cons (string :tag
"name")
361 (string :tag
"value"))))
363 (defcustom org-table-allow-automatic-line-recalculation t
364 "Non-nil means lines marked with |#| or |*| will be recomputed automatically.
366 Automatically means when `TAB' or `RET' or `\\[org-ctrl-c-ctrl-c]' \
367 are pressed in the line."
368 :group
'org-table-calculation
371 (defcustom org-table-relative-ref-may-cross-hline t
372 "Non-nil means relative formula references may cross hlines.
373 Here are the allowed values:
375 nil Relative references may not cross hlines. They will reference the
376 field next to the hline instead. Coming from below, the reference
377 will be to the field below the hline. Coming from above, it will be
379 t Relative references may cross hlines.
380 error An attempt to cross a hline will throw an error.
382 It is probably good to never set this variable to nil, for the sake of
383 portability of tables."
384 :group
'org-table-calculation
386 (const :tag
"Allow to cross" t
)
387 (const :tag
"Stick to hline" nil
)
388 (const :tag
"Error on attempt to cross" error
)))
390 (defcustom org-table-formula-create-columns nil
391 "Non-nil means that evaluation of a field formula can add new
392 columns if an out-of-bounds field is being set."
393 :group
'org-table-calculation
395 :package-version
'(Org .
"8.3")
397 (const :tag
"Setting an out-of-bounds field generates an error (default)" nil
)
398 (const :tag
"Setting an out-of-bounds field silently adds columns as needed" t
)
399 (const :tag
"Setting an out-of-bounds field adds columns as needed, but issues a warning message" warn
)
400 (const :tag
"When setting an out-of-bounds field, the user is prompted" prompt
)))
402 (defgroup org-table-import-export nil
403 "Options concerning table import and export in Org mode."
404 :tag
"Org Table Import Export"
407 (defcustom org-table-export-default-format
"orgtbl-to-tsv"
408 "Default export parameters for `org-table-export'.
409 These can be overridden for a specific table by setting the
410 TABLE_EXPORT_FORMAT property. See the manual section on orgtbl
411 radio tables for the different export transformations and
412 available parameters."
413 :group
'org-table-import-export
416 (defcustom org-table-convert-region-max-lines
999
417 "Max lines that `org-table-convert-region' will attempt to process.
419 The function can be slow on larger regions; this safety feature
420 prevents it from hanging emacs."
421 :group
'org-table-import-export
424 :package-version
'(Org .
"8.3"))
426 (defconst org-table-auto-recalculate-regexp
"^[ \t]*| *# *\\(|\\|$\\)"
427 "Regexp matching a line marked for automatic recalculation.")
429 (defconst org-table-recalculate-regexp
"^[ \t]*| *[#*] *\\(|\\|$\\)"
430 "Regexp matching a line marked for recalculation.")
432 (defconst org-table-calculate-mark-regexp
"^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
433 "Regexp matching a line marked for calculation.")
435 (defconst org-table-border-regexp
"^[ \t]*[^| \t]"
436 "Regexp matching any line outside an Org table.")
438 (defvar org-table-last-highlighted-reference nil
)
440 (defvar org-table-formula-history nil
)
442 (defvar org-table-column-names nil
443 "Alist with column names, derived from the `!' line.
444 This variable is initialized with `org-table-analyze'.")
446 (defvar org-table-column-name-regexp nil
447 "Regular expression matching the current column names.
448 This variable is initialized with `org-table-analyze'.")
450 (defvar org-table-local-parameters nil
451 "Alist with parameter names, derived from the `$' line.
452 This variable is initialized with `org-table-analyze'.")
454 (defvar org-table-named-field-locations nil
455 "Alist with locations of named fields.
456 Associations follow the pattern (NAME LINE COLUMN) where
457 NAME is the name of the field as a string,
458 LINE is the number of lines from the beginning of the table,
459 COLUMN is the column of the field, as an integer.
460 This variable is initialized with `org-table-analyze'.")
462 (defvar org-table-current-line-types nil
463 "Table row types in current table.
464 This variable is initialized with `org-table-analyze'.")
466 (defvar org-table-current-begin-pos nil
467 "Current table begin position, as a marker.
468 This variable is initialized with `org-table-analyze'.")
470 (defvar org-table-current-ncol nil
471 "Number of columns in current table.
472 This variable is initialized with `org-table-analyze'.")
474 (defvar org-table-dlines nil
475 "Vector of data line line numbers in the current table.
476 Line numbers are counted from the beginning of the table. This
477 variable is initialized with `org-table-analyze'.")
479 (defvar org-table-hlines nil
480 "Vector of hline line numbers in the current table.
481 Line numbers are counted from the beginning of the table. This
482 variable is initialized with `org-table-analyze'.")
484 (defconst org-table-range-regexp
485 "@\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)?\\(\\$[-+]?[0-9]+\\)?\\)?"
487 "Regular expression for matching ranges in formulas.")
489 (defconst org-table-range-regexp2
491 "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
493 "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
494 "Match a range for reference display.")
496 (defconst org-table-translate-regexp
497 (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
498 "Match a reference that needs translation, for reference display.")
500 (defmacro org-table-save-field
(&rest body
)
501 "Save current field; execute BODY; restore field.
502 Field is restored even in case of abnormal exit."
503 (declare (debug (body)))
504 (org-with-gensyms (line column
)
505 `(let ((,line
(copy-marker (line-beginning-position)))
506 (,column
(org-table-current-column)))
510 (org-table-goto-column ,column
)
511 (set-marker ,line nil
)))))
514 (defun org-table-create-with-table.el
()
515 "Use the table.el package to insert a new table.
516 If there is already a table at point, convert between Org tables
517 and table.el tables."
522 (if (y-or-n-p "Convert table to Org table? ")
523 (org-table-convert)))
525 (when (y-or-n-p "Convert table to table.el table? ")
527 (org-table-convert)))
528 (t (call-interactively 'table-insert
))))
531 (defun org-table-create-or-convert-from-region (arg)
532 "Convert region to table, or create an empty table.
533 If there is an active region, convert it to a table, using the function
534 `org-table-convert-region'. See the documentation of that function
535 to learn how the prefix argument is interpreted to determine the field
537 If there is no such region, create an empty table with `org-table-create'."
539 (if (org-region-active-p)
540 (org-table-convert-region (region-beginning) (region-end) arg
)
541 (org-table-create arg
)))
544 (defun org-table-create (&optional size
)
545 "Query for a size and insert a table skeleton.
546 SIZE is a string Columns x Rows like for example \"3x2\"."
549 (setq size
(read-string
550 (concat "Table size Columns x Rows [e.g. "
551 org-table-default-size
"]: ")
552 "" nil org-table-default-size
)))
555 (indent (make-string (current-column) ?\
))
556 (split (org-split-string size
" *x *"))
557 (rows (string-to-number (nth 1 split
)))
558 (columns (string-to-number (car split
)))
559 (line (concat (apply 'concat indent
"|" (make-list columns
" |"))
561 (if (string-match "^[ \t]*$" (buffer-substring-no-properties
562 (point-at-bol) (point)))
563 (beginning-of-line 1)
565 ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
566 (dotimes (_ rows
) (insert line
))
569 ;; Insert a hline after the first row.
577 (defun org-table-convert-region (beg0 end0
&optional separator
)
578 "Convert region to a table.
580 The region goes from BEG0 to END0, but these borders will be moved
581 slightly, to make sure a beginning of line in the first line is included.
583 SEPARATOR specifies the field separator in the lines. It can have the
586 (4) Use the comma as a field separator
587 (16) Use a TAB as field separator
588 (64) Prompt for a regular expression as field separator
589 integer When a number, use that many spaces, or a TAB, as field separator
590 regexp When a regular expression, use it to match the separator
591 nil When nil, the command tries to be smart and figure out the
592 separator in the following way:
593 - when each line contains a TAB, assume TAB-separated material
594 - when each line contains a comma, assume CSV material
595 - else, assume one or more SPACE characters as separator."
597 (let* ((beg (min beg0 end0
))
598 (end (max beg0 end0
))
600 (if (> (count-lines beg end
) org-table-convert-region-max-lines
)
601 (user-error "Region is longer than `org-table-convert-region-max-lines' (%s) lines; not converting"
602 org-table-convert-region-max-lines
)
603 (if (equal separator
'(64))
604 (setq separator
(read-regexp "Regexp for field separator")))
606 (beginning-of-line 1)
607 (setq beg
(point-marker))
609 (if (bolp) (backward-char 1) (end-of-line 1))
610 (setq end
(point-marker))
611 ;; Get the right field separator
616 ((not (re-search-forward "^[^\n\t]+$" end t
)) '(16))
617 ((not (re-search-forward "^[^\n,]+$" end t
)) '(4))
620 (if (equal separator
'(4))
621 (while (< (point) end
)
622 ;; parse the csv stuff
624 ((looking-at "^") (insert "| "))
625 ((looking-at "[ \t]*$") (replace-match " |") (beginning-of-line 2))
626 ((looking-at "[ \t]*\"\\([^\"\n]*\\)\"")
627 (replace-match "\\1")
628 (if (looking-at "\"") (insert "\"")))
629 ((looking-at "[^,\n]+") (goto-char (match-end 0)))
630 ((looking-at "[ \t]*,") (replace-match " | "))
631 (t (beginning-of-line 2))))
633 ((equal separator
'(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
634 ((equal separator
'(16)) "^\\|\t")
635 ((integerp separator
)
637 (user-error "Number of spaces in separator must be >= 1")
638 (format "^ *\\| *\t *\\| \\{%d,\\}" separator
)))
640 (format "^ *\\|%s" separator
))
641 (t (error "This should not happen"))))
642 (while (re-search-forward re end t
)
643 (replace-match "| " t t
)))
648 (defun org-table-import (file arg
)
649 "Import FILE as a table.
650 The file is assumed to be tab-separated. Such files can be produced by most
651 spreadsheet and database applications. If no tabs (at least one per line)
652 are found, lines will be split on whitespace into fields."
654 (or (bolp) (newline))
657 (insert-file-contents file
)
658 (org-table-convert-region beg
(+ (point) (- (point-max) pm
)) arg
)))
662 (defun org-table-export (&optional file format
)
663 "Export table to a file, with configurable format.
664 Such a file can be imported into usual spreadsheet programs.
666 FILE can be the output file name. If not given, it will be taken
667 from a TABLE_EXPORT_FILE property in the current entry or higher
668 up in the hierarchy, or the user will be prompted for a file
669 name. FORMAT can be an export format, of the same kind as it
670 used when `orgtbl-mode' sends a table in a different format.
672 The command suggests a format depending on TABLE_EXPORT_FORMAT,
673 whether it is set locally or up in the hierarchy, then on the
674 extension of the given file name, and finally on the variable
675 `org-table-export-default-format'."
677 (unless (org-at-table-p) (user-error "No table at point"))
678 (org-table-align) ; Make sure we have everything we need.
679 (let ((file (or file
(org-entry-get (point) "TABLE_EXPORT_FILE" t
))))
681 (setq file
(read-file-name "Export table to: "))
682 (unless (or (not (file-exists-p file
))
683 (y-or-n-p (format "Overwrite file %s? " file
)))
684 (user-error "File not written")))
685 (when (file-directory-p file
)
686 (user-error "This is a directory path, not a file"))
687 (when (and (buffer-file-name (buffer-base-buffer))
690 (file-truename (buffer-file-name (buffer-base-buffer)))))
691 (user-error "Please specify a file name that is different from current"))
692 (let ((fileext (concat (file-name-extension file
) "$"))
693 (format (or format
(org-entry-get (point) "TABLE_EXPORT_FORMAT" t
))))
695 (let* ((formats '("orgtbl-to-tsv" "orgtbl-to-csv" "orgtbl-to-latex"
696 "orgtbl-to-html" "orgtbl-to-generic"
697 "orgtbl-to-texinfo" "orgtbl-to-orgtbl"
698 "orgtbl-to-unicode"))
700 (replace-regexp-in-string
702 (replace-regexp-in-string
707 (and (string-match-p fileext f
) f
))
709 org-table-export-default-format
)
713 "Format: " formats nil nil deffmt-readable
))))
714 (if (string-match "\\([^ \t\r\n]+\\)\\( +.*\\)?" format
)
715 (let ((transform (intern (match-string 1 format
)))
716 (params (and (match-end 2)
717 (read (concat "(" (match-string 2 format
) ")"))))
718 (table (org-table-to-lisp
719 (buffer-substring-no-properties
720 (org-table-begin) (org-table-end)))))
721 (unless (fboundp transform
)
722 (user-error "No such transformation function %s" transform
))
724 (with-current-buffer (find-file-noselect file
)
725 (setq buf
(current-buffer))
728 (insert (funcall transform table params
) "\n")
731 (message "Export done."))
732 (user-error "TABLE_EXPORT_FORMAT invalid")))))
734 (defvar org-table-aligned-begin-marker
(make-marker)
735 "Marker at the beginning of the table last aligned.
736 Used to check if cursor still is in that table, to minimize realignment.")
737 (defvar org-table-aligned-end-marker
(make-marker)
738 "Marker at the end of the table last aligned.
739 Used to check if cursor still is in that table, to minimize realignment.")
740 (defvar org-table-last-alignment nil
741 "List of flags for flushright alignment, from the last re-alignment.
742 This is being used to correctly align a single field after TAB or RET.")
743 (defvar org-table-last-column-widths nil
744 "List of max width of fields in each column.
745 This is being used to correctly align a single field after TAB or RET.")
746 (defvar-local org-table-formula-debug nil
747 "Non-nil means debug table formulas.
748 When nil, simply write \"#ERROR\" in corrupted fields.")
749 (defvar-local org-table-overlay-coordinates nil
750 "Overlay coordinates after each align of a table.")
752 (defvar org-last-recalc-line nil
)
753 (defvar org-table-do-narrow t
) ; for dynamic scoping
754 (defconst org-narrow-column-arrow
"=>"
755 "Used as display property in narrowed table columns.")
758 (defun org-table-align ()
759 "Align the table at point by aligning all vertical bars."
761 (let* ((beg (org-table-begin))
762 (end (copy-marker (org-table-end))))
763 (org-table-save-field
764 ;; Make sure invisible characters in the table are at the right
765 ;; place since column widths take them into account.
766 (font-lock-fontify-region beg end
)
767 (move-marker org-table-aligned-begin-marker beg
)
768 (move-marker org-table-aligned-end-marker end
)
770 (let* ((indent (progn (looking-at "[ \t]*") (match-string 0)))
771 ;; Table's rows. Separators are replaced by nil. Trailing
772 ;; spaces are also removed.
773 (lines (mapcar (lambda (l)
774 (and (not (string-match-p "\\`[ \t]*|-" l
))
775 (let ((l (org-trim l
)))
776 (remove-text-properties
777 0 (length l
) '(display t org-cwidth t
) l
)
779 (org-split-string (buffer-substring beg end
) "\n")))
780 ;; Get the data fields by splitting the lines.
781 (fields (mapcar (lambda (l) (org-split-string l
" *| *"))
783 ;; Compute number of fields in the longest line. If the
784 ;; table contains no field, create a default table.
785 (maxfields (if fields
(apply #'max
(mapcar #'length fields
))
786 (kill-region beg end
)
787 (org-table-create org-table-default-size
)
788 (user-error "Empty table - created default table")))
789 ;; A list of empty strings to fill any short rows on output.
790 (emptycells (make-list maxfields
""))
792 ;; Check for special formatting.
793 (dotimes (i maxfields
)
794 (let ((column (mapcar (lambda (x) (or (nth i x
) "")) fields
))
796 ;; Look for an explicit width or alignment.
797 (when (save-excursion
798 (or (re-search-forward "| *<[lrc][0-9]*> *\\(|\\|$\\)" end t
)
799 (and org-table-do-narrow
801 "| *<[lrc]?[0-9]+> *\\(|\\|$\\)" end t
))))
803 (dolist (cell column
)
804 (when (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'" cell
)
805 (when (match-end 1) (setq falign
(match-string 1 cell
)))
806 (when (and org-table-do-narrow
(match-end 2))
807 (setq fmax
(string-to-number (match-string 2 cell
))))
808 (when (or falign fmax
) (throw :exit nil
)))))
809 ;; Find fields that are wider than FMAX, and shorten them.
812 (when (> (string-width x
) fmax
)
816 "Clipped table field, use `\\[org-table-edit-field]' to \
817 edit. Full value is:\n"
818 (substring-no-properties x
)))
821 (or (string-match org-bracket-link-regexp x
)
826 "Cannot narrow field starting with wide link \"%s\""
828 (if (= (org-string-width x
) l
) (setq f2 f1
)
830 (while (< (org-string-width (substring x
0 f2
)) f1
)
832 (add-text-properties f2 l
(list 'org-cwidth t
) x
)
834 (if (>= (string-width (substring x
(1- f2
) f2
)) 2) (1- f2
)
837 (list 'display org-narrow-column-arrow
)
839 ;; Get the maximum width for each column
840 (push (or fmax
(apply #'max
1 (mapcar #'org-string-width column
)))
842 ;; Get the fraction of numbers among non-empty cells to
843 ;; decide about alignment of the column.
844 (if falign
(push (equal (downcase falign
) "r") typenums
)
851 (if (string-match-p org-table-number-regexp x
)
855 (push (>= frac org-table-number-fraction
) typenums
)))))
856 (setq lengths
(nreverse lengths
))
857 (setq typenums
(nreverse typenums
))
858 ;; Store alignment of this table, for later editing of single
860 (setq org-table-last-alignment typenums
)
861 (setq org-table-last-column-widths lengths
)
862 ;; With invisible characters, `format' does not get the field
863 ;; width right So we need to make these fields wide by hand.
864 ;; Invisible characters may be introduced by fontified links,
865 ;; emphasis, macros or sub/superscripts.
866 (when (or (text-property-any beg end
'invisible
'org-link
)
867 (text-property-any beg end
'invisible t
))
868 (dotimes (i maxfields
)
869 (let ((len (nth i lengths
)))
870 (dotimes (j (length fields
))
871 (let* ((c (nthcdr i
(nth j fields
)))
875 (let ((l (length cell
)))
876 (or (text-property-any 0 l
'invisible
'org-link cell
)
877 (text-property-any beg end
'invisible t
)))
878 (< (org-string-width cell
) len
))
879 (let ((s (make-string (- len
(org-string-width cell
)) ?\s
)))
880 (setcar c
(if (nth i typenums
) (concat s cell
)
881 (concat cell s
))))))))))
883 ;; Compute the formats needed for output of the table.
884 (let ((hfmt (concat indent
"|"))
885 (rfmt (concat indent
"|"))
888 (dolist (l lengths
(setq hfmt
(concat (substring hfmt
0 -
1) "|")))
889 (let ((ty (if (pop typenums
) "" "-"))) ; Flush numbers right.
890 (setq rfmt
(concat rfmt
(format rfmt1 ty l
)))
891 (setq hfmt
(concat hfmt
(format hfmt1
(make-string l ?-
))))))
892 ;; Replace modified lines only. Check not only contents, but
893 ;; also columns' width.
896 (if l
(apply #'format rfmt
(append (pop fields
) emptycells
))
898 (previous (buffer-substring (point) (line-end-position))))
899 (if (and (equal previous line
)
903 (setq a
(next-single-property-change
904 a
'org-cwidth previous
))
905 (setq b
(next-single-property-change
906 b
'org-cwidth line
)))
911 (delete-region (point) (line-beginning-position 2))))))
912 (when (and orgtbl-mode
(not (derived-mode-p 'org-mode
)))
913 (goto-char org-table-aligned-begin-marker
)
914 (while (org-hide-wide-columns org-table-aligned-end-marker
)))
916 (when org-table-overlay-coordinates
(org-table-overlay-coordinates))
917 (setq org-table-may-need-update nil
)))))
920 (defun org-table-begin (&optional table-type
)
921 "Find the beginning of the table and return its position.
922 With a non-nil optional argument TABLE-TYPE, return the beginning
923 of a table.el-type table. This function assumes point is on
926 (org-element-property :post-affiliated
(org-element-at-point)))
928 (and (re-search-backward org-table-border-regexp nil t
)
929 (line-beginning-position 2))))
933 (defun org-table-end (&optional table-type
)
934 "Find the end of the table and return its position.
935 With a non-nil optional argument TABLE-TYPE, return the end of
936 a table.el-type table. This function assumes point is on
940 (goto-char (org-element-property :end
(org-element-at-point)))
941 (skip-chars-backward " \t\n")
942 (line-beginning-position 2))
943 ((re-search-forward org-table-border-regexp nil t
)
945 ;; When the line right after the table is the last line in
946 ;; the buffer with trailing spaces but no final newline
947 ;; character, be sure to catch the correct ending at its
948 ;; beginning. In any other case, ending is expected to be
950 (t (goto-char (point-max))
951 (skip-chars-backward " \t")
952 (if (bolp) (point) (line-end-position))))))
955 (defun org-table-justify-field-maybe (&optional new
)
956 "Justify the current field, text to left, number to right.
957 Optional argument NEW may specify text to replace the current field content."
959 ((and (not new
) org-table-may-need-update
)) ; Realignment will happen anyway
960 ((org-at-table-hline-p))
962 (or (not (eq (marker-buffer org-table-aligned-begin-marker
)
964 (< (point) org-table-aligned-begin-marker
)
965 (>= (point) org-table-aligned-end-marker
)))
966 ;; This is not the same table, force a full re-align.
967 (setq org-table-may-need-update t
))
969 ;; Realign the current field, based on previous full realign.
971 (col (org-table-current-column)))
973 (skip-chars-backward "^|")
974 (if (not (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)"))
975 (setq org-table-may-need-update t
)
976 (let* ((numbers?
(nth (1- col
) org-table-last-alignment
))
977 (cell (match-string 0))
978 (field (match-string 1))
979 (len (max 1 (- (org-string-width cell
) 3)))
980 (properly-closed?
(/= (match-beginning 2) (match-end 2)))
981 (fmt (format (if numbers?
" %%%ds %s" " %%-%ds %s")
983 (if properly-closed?
"|"
984 (setq org-table-may-need-update t
)
987 (cond ((not new
) (format fmt field
))
988 ((<= (org-string-width new
) len
) (format fmt new
))
990 (setq org-table-may-need-update t
)
991 (format " %s |" new
)))))
992 (unless (equal new-cell cell
)
993 (let (org-table-may-need-update)
994 (replace-match new-cell t t
)))
995 (goto-char pos
))))))))
998 (defun org-table-next-field ()
999 "Go to the next field in the current table, creating new lines as needed.
1000 Before doing so, re-align the table if necessary."
1002 (org-table-maybe-eval-formula)
1003 (org-table-maybe-recalculate-line)
1004 (if (and org-table-automatic-realign
1005 org-table-may-need-update
)
1007 (let ((end (org-table-end)))
1008 (if (org-at-table-hline-p)
1012 (re-search-forward "|" end
)
1013 (if (looking-at "[ \t]*$")
1014 (re-search-forward "|" end
))
1015 (if (and (looking-at "-")
1016 org-table-tab-jumps-over-hlines
1017 (re-search-forward "^[ \t]*|\\([^-]\\)" end t
))
1018 (goto-char (match-beginning 1)))
1019 (if (looking-at "-")
1021 (beginning-of-line 0)
1022 (org-table-insert-row 'below
))
1023 (if (looking-at " ") (forward-char 1))))
1025 (org-table-insert-row 'below
)))))
1028 (defun org-table-previous-field ()
1029 "Go to the previous field in the table.
1030 Before doing so, re-align the table if necessary."
1032 (org-table-justify-field-maybe)
1033 (org-table-maybe-recalculate-line)
1034 (when (and org-table-automatic-realign
1035 org-table-may-need-update
)
1037 (when (org-at-table-hline-p)
1039 (let ((start (org-table-begin))
1043 (search-backward "|" start nil
2)
1044 (while (looking-at-p "|\\(?:-\\|[ \t]*$\\)")
1045 (search-backward "|" start
)))
1048 (user-error "Cannot move to previous table field"))))
1049 (when (looking-at "| ?")
1050 (goto-char (match-end 0))))
1052 (defun org-table-beginning-of-field (&optional n
)
1053 "Move to the beginning of the current table field.
1054 If already at or before the beginning, move to the beginning of the
1056 With numeric argument N, move N-1 fields backward first."
1058 (let ((pos (point)))
1061 (org-table-previous-field))
1062 (if (not (re-search-backward "|" (point-at-bol 0) t
))
1063 (user-error "No more table fields before the current")
1064 (goto-char (match-end 0))
1065 (and (looking-at " ") (forward-char 1)))
1066 (if (>= (point) pos
) (org-table-beginning-of-field 2))))
1068 (defun org-table-end-of-field (&optional n
)
1069 "Move to the end of the current table field.
1070 If already at or after the end, move to the end of the next table field.
1071 With numeric argument N, move N-1 fields forward first."
1073 (let ((pos (point)))
1076 (org-table-next-field))
1077 (when (re-search-forward "|" (point-at-eol 1) t
)
1079 (skip-chars-backward " ")
1080 (if (and (equal (char-before (point)) ?|
) (looking-at " "))
1082 (if (<= (point) pos
) (org-table-end-of-field 2))))
1085 (defun org-table-next-row ()
1086 "Go to the next row (same column) in the current table.
1087 Before doing so, re-align the table if necessary."
1089 (org-table-maybe-eval-formula)
1090 (org-table-maybe-recalculate-line)
1091 (if (or (looking-at "[ \t]*$")
1092 (save-excursion (skip-chars-backward " \t") (bolp)))
1094 (if (and org-table-automatic-realign
1095 org-table-may-need-update
)
1097 (let ((col (org-table-current-column)))
1098 (beginning-of-line 2)
1099 (if (or (not (org-at-table-p))
1100 (org-at-table-hline-p))
1102 (beginning-of-line 0)
1103 (org-table-insert-row 'below
)))
1104 (org-table-goto-column col
)
1105 (skip-chars-backward "^|\n\r")
1106 (if (looking-at " ") (forward-char 1)))))
1109 (defun org-table-copy-down (n)
1110 "Copy the value of the current field one row below.
1112 If the field at the cursor is empty, copy the content of the
1113 nearest non-empty field above. With argument N, use the Nth
1116 If the current field is not empty, it is copied down to the next
1117 row, and the cursor is moved with it. Therefore, repeating this
1118 command causes the column to be filled row-by-row.
1120 If the variable `org-table-copy-increment' is non-nil and the
1121 field is an integer or a timestamp, it will be incremented while
1122 copying. By default, increment by the difference between the
1123 value in the current field and the one in the field above. To
1124 increment using a fixed integer, set `org-table-copy-increment'
1125 to a number. In the case of a timestamp, increment by days."
1127 (let* ((colpos (org-table-current-column))
1128 (col (current-column))
1129 (field (save-excursion (org-table-get-field)))
1130 (field-up (or (save-excursion
1131 (org-table-get (1- (org-table-current-line))
1132 (org-table-current-column))) ""))
1133 (non-empty (string-match "[^ \t]" field
))
1134 (non-empty-up (string-match "[^ \t]" field-up
))
1135 (beg (org-table-begin))
1138 (org-table-check-inside-data-field)
1143 (while (progn (beginning-of-line 1)
1144 (re-search-backward org-table-dataline-regexp
1146 (org-table-goto-column colpos t
)
1147 (if (and (looking-at
1148 "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
1149 (<= (setq n
(1- n
)) 0))
1150 (throw 'exit
(match-string 1))))))
1153 (while (progn (beginning-of-line 1)
1154 (re-search-backward org-table-dataline-regexp
1156 (org-table-goto-column colpos t
)
1157 (if (and (looking-at
1158 "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
1159 (<= (setq n
(1- n
)) 0))
1160 (throw 'exit
(match-string 1))))))
1161 (setq non-empty-up
(and field-up
(string-match "[^ \t]" field-up
))))
1162 ;; Above field was not empty, go down to the next row
1163 (setq txt
(org-trim field
))
1164 (org-table-next-row)
1165 (org-table-blank-field))
1166 (if non-empty-up
(setq txt-up
(org-trim field-up
)))
1168 ((numberp org-table-copy-increment
) org-table-copy-increment
)
1169 (txt-up (cond ((and (string-match org-ts-regexp3 txt-up
)
1170 (string-match org-ts-regexp3 txt
))
1171 (- (org-time-string-to-absolute txt
)
1172 (org-time-string-to-absolute txt-up
)))
1173 ((string-match org-ts-regexp3 txt
) 1)
1174 ((string-match "\\([-+]\\)?[0-9]+\\(?:\.[0-9]+\\)?" txt-up
)
1175 (- (string-to-number txt
)
1176 (string-to-number (match-string 0 txt-up
))))
1180 (user-error "No non-empty field found")
1181 (if (and org-table-copy-increment
1182 (not (equal orig-n
0))
1183 (string-match-p "^[-+^/*0-9eE.]+$" txt
)
1184 (< (string-to-number txt
) 100000000))
1185 (setq txt
(calc-eval (concat txt
"+" (number-to-string inc
)))))
1187 (org-move-to-column col
)
1188 (if (and org-table-copy-increment
(org-at-timestamp-p))
1189 (org-timestamp-up-day inc
)
1190 (org-table-maybe-recalculate-line))
1192 (org-move-to-column col
))))
1194 (defun org-table-check-inside-data-field (&optional noerror
)
1195 "Is point inside a table data field?
1196 I.e. not on a hline or before the first or after the last column?
1197 This actually throws an error, so it aborts the current command."
1198 (cond ((and (org-at-table-p)
1199 (not (save-excursion (skip-chars-backward " \t") (bolp)))
1200 (not (org-at-table-hline-p))
1201 (not (looking-at "[ \t]*$"))))
1203 (t (user-error "Not in table data field"))))
1205 (defvar org-table-clip nil
1206 "Clipboard for table regions.")
1208 (defun org-table-get (line column
)
1209 "Get the field in table line LINE, column COLUMN.
1210 If LINE is larger than the number of data lines in the table, the function
1211 returns nil. However, if COLUMN is too large, we will simply return an
1213 If LINE is nil, use the current line.
1214 If COLUMN is nil, use the current column."
1215 (setq column
(or column
(org-table-current-column)))
1217 (and (or (not line
) (org-table-goto-line line
))
1218 (org-trim (org-table-get-field column
)))))
1220 (defun org-table-put (line column value
&optional align
)
1221 "Put VALUE into line LINE, column COLUMN.
1222 When ALIGN is set, also realign the table."
1223 (setq column
(or column
(org-table-current-column)))
1224 (prog1 (save-excursion
1225 (and (or (not line
) (org-table-goto-line line
))
1226 (progn (org-table-goto-column column nil
'force
) t
)
1227 (org-table-get-field column value
)))
1228 (and align
(org-table-align))))
1230 (defun org-table-current-line ()
1231 "Return the index of the current data line."
1232 (let ((pos (point)) (end (org-table-end)) (cnt 0))
1234 (goto-char (org-table-begin))
1235 (while (and (re-search-forward org-table-dataline-regexp end t
)
1237 (< (point-at-eol) pos
))))
1240 (defun org-table-goto-line (N)
1241 "Go to the Nth data line in the current table.
1242 Return t when the line exists, nil if it does not exist."
1243 (goto-char (org-table-begin))
1244 (let ((end (org-table-end)) (cnt 0))
1245 (while (and (re-search-forward org-table-dataline-regexp end t
)
1246 (< (setq cnt
(1+ cnt
)) N
)))
1250 (defun org-table-blank-field ()
1251 "Blank the current table field or active region."
1253 (org-table-check-inside-data-field)
1254 (if (and (called-interactively-p 'any
) (org-region-active-p))
1255 (let (org-table-clip)
1256 (org-table-cut-region (region-beginning) (region-end)))
1257 (skip-chars-backward "^|")
1259 (if (looking-at "|[^|\n]+")
1260 (let* ((pos (match-beginning 0))
1261 (match (match-string 0))
1262 (len (org-string-width match
)))
1263 (replace-match (concat "|" (make-string (1- len
) ?\
)))
1264 (goto-char (+ 2 pos
))
1265 (substring match
1)))))
1267 (defun org-table-get-field (&optional n replace
)
1268 "Return the value of the field in column N of current row.
1269 N defaults to current column. If REPLACE is a string, replace
1270 field with this value. The return value is always the old
1272 (when n
(org-table-goto-column n
))
1273 (skip-chars-backward "^|\n")
1274 (if (or (bolp) (looking-at-p "[ \t]*$"))
1275 ;; Before first column or after last one.
1277 (looking-at "[^|\r\n]*")
1278 (let* ((pos (match-beginning 0))
1279 (val (buffer-substring pos
(match-end 0))))
1281 (replace-match (if (equal replace
"") " " replace
) t t
))
1282 (goto-char (min (line-end-position) (1+ pos
)))
1286 (defun org-table-field-info (_arg)
1287 "Show info about the current field, and highlight any reference at point."
1289 (unless (org-at-table-p) (user-error "Not at a table"))
1292 (let* ((pos (point))
1293 (col (org-table-current-column))
1294 (cname (car (rassoc (int-to-string col
) org-table-column-names
)))
1295 (name (car (rassoc (list (count-lines org-table-current-begin-pos
1296 (line-beginning-position))
1298 org-table-named-field-locations
)))
1299 (eql (org-table-expand-lhs-ranges
1302 (cons (org-table-formula-handle-first/last-rc
(car e
))
1304 (org-table-get-stored-formulas))))
1305 (dline (org-table-current-dline))
1306 (ref (format "@%d$%d" dline col
))
1307 (ref1 (org-table-convert-refs-to-an ref
))
1308 ;; Prioritize field formulas over column formulas.
1309 (fequation (or (assoc name eql
) (assoc ref eql
)))
1310 (cequation (assoc (format "$%d" col
) eql
))
1311 (eqn (or fequation cequation
)))
1312 (let ((p (and eqn
(get-text-property 0 :orig-eqn
(car eqn
)))))
1313 (when p
(setq eqn p
)))
1315 (ignore-errors (org-table-show-reference 'local
))
1316 (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
1318 (if cname
(concat " or $" cname
) "")
1320 (if name
(concat " or $" name
) "")
1321 ;; FIXME: formula info not correct if special table line
1323 (concat ", formula: "
1324 (org-table-formula-to-user
1326 (if (or (string-prefix-p "$" (car eqn
))
1327 (string-prefix-p "@" (car eqn
)))
1330 (car eqn
) "=" (cdr eqn
))))
1333 (defun org-table-current-column ()
1334 "Find out which column we are in."
1337 (let ((column 0) (pos (point)))
1339 (while (search-forward "|" pos t
) (cl-incf column
))
1342 (defun org-table-current-dline ()
1343 "Find out what table data line we are in.
1344 Only data lines count for this."
1347 (pos (line-beginning-position)))
1348 (goto-char (org-table-begin))
1349 (while (<= (point) pos
)
1350 (when (looking-at org-table-dataline-regexp
) (cl-incf c
))
1355 (defun org-table-goto-column (n &optional on-delim force
)
1356 "Move the cursor to the Nth column in the current table line.
1357 With optional argument ON-DELIM, stop with point before the left delimiter
1359 If there are less than N fields, just go to after the last delimiter.
1360 However, when FORCE is non-nil, create new columns if necessary."
1362 (beginning-of-line 1)
1364 (while (and (> (setq n
(1- n
)) -
1)
1365 (or (search-forward "|" (point-at-eol) t
)
1367 (progn (end-of-line 1)
1368 (skip-chars-backward "^|")
1371 (when (and force
(not (looking-at ".*|")))
1372 (save-excursion (end-of-line 1) (insert " | ")))
1375 (if (looking-at " ") (forward-char 1)))))
1378 (defun org-table-insert-column ()
1379 "Insert a new column into the table."
1381 (unless (org-at-table-p) (user-error "Not at a table"))
1382 (org-table-find-dataline)
1383 (let* ((col (max 1 (org-table-current-column)))
1384 (beg (org-table-begin))
1385 (end (copy-marker (org-table-end))))
1386 (org-table-save-field
1388 (while (< (point) end
)
1389 (unless (org-at-table-hline-p)
1390 (org-table-goto-column col t
)
1393 (set-marker end nil
)
1395 (when (or (not org-table-fix-formulas-confirm
)
1396 (funcall org-table-fix-formulas-confirm
"Fix formulas? "))
1397 (org-table-fix-formulas "$" nil
(1- col
) 1)
1398 (org-table-fix-formulas "$LR" nil
(1- col
) 1))))
1400 (defun org-table-find-dataline ()
1401 "Find a data line in the current table, which is needed for column commands."
1402 (if (and (org-at-table-p)
1403 (not (org-at-table-hline-p)))
1405 (let ((col (current-column))
1406 (end (org-table-end)))
1407 (org-move-to-column col
)
1408 (while (and (< (point) end
)
1409 (or (not (= (current-column) col
))
1410 (org-at-table-hline-p)))
1411 (beginning-of-line 2)
1412 (org-move-to-column col
))
1413 (if (and (org-at-table-p)
1414 (not (org-at-table-hline-p)))
1417 "Please position cursor in a data line for column operations")))))
1419 (defun org-table-line-to-dline (line &optional above
)
1420 "Turn a buffer line number into a data line number.
1422 If there is no data line in this line, return nil.
1424 If there is no matching dline (most likely the reference was
1425 a hline), the first dline below it is used. When ABOVE is
1426 non-nil, the one above is used."
1428 (max (1- (length org-table-dlines
))))
1429 (cond ((or (> (aref org-table-dlines min
) line
)
1430 (< (aref org-table-dlines max
) line
))
1432 ((= (aref org-table-dlines max
) line
) max
)
1434 (while (> (- max min
) 1)
1435 (let* ((mean (/ (+ max min
) 2))
1436 (v (aref org-table-dlines mean
)))
1437 (cond ((= v line
) (throw 'exit mean
))
1438 ((> v line
) (setq max mean
))
1439 (t (setq min mean
)))))
1440 (if above min max
))))))
1443 (defun org-table-delete-column ()
1444 "Delete a column from the table."
1446 (unless (org-at-table-p) (user-error "Not at a table"))
1447 (org-table-find-dataline)
1448 (org-table-check-inside-data-field)
1449 (let ((col (org-table-current-column))
1450 (beg (org-table-begin))
1451 (end (copy-marker (org-table-end))))
1452 (org-table-save-field
1454 (while (< (point) end
)
1455 (if (org-at-table-hline-p)
1457 (org-table-goto-column col t
)
1458 (and (looking-at "|[^|\n]+|")
1459 (replace-match "|")))
1461 (set-marker end nil
)
1462 (org-table-goto-column (max 1 (1- col
)))
1464 (when (or (not org-table-fix-formulas-confirm
)
1465 (funcall org-table-fix-formulas-confirm
"Fix formulas? "))
1466 (org-table-fix-formulas
1467 "$" (list (cons (number-to-string col
) "INVALID")) col -
1 col
)
1468 (org-table-fix-formulas
1469 "$LR" (list (cons (number-to-string col
) "INVALID")) col -
1 col
))))
1472 (defun org-table-move-column-right ()
1473 "Move column to the right."
1475 (org-table-move-column nil
))
1477 (defun org-table-move-column-left ()
1478 "Move column to the left."
1480 (org-table-move-column 'left
))
1483 (defun org-table-move-column (&optional left
)
1484 "Move the current column to the right. With arg LEFT, move to the left."
1486 (unless (org-at-table-p) (user-error "Not at a table"))
1487 (org-table-find-dataline)
1488 (org-table-check-inside-data-field)
1489 (let* ((col (org-table-current-column))
1490 (col1 (if left
(1- col
) col
))
1491 (colpos (if left
(1- col
) (1+ col
)))
1492 (beg (org-table-begin))
1493 (end (copy-marker (org-table-end))))
1494 (when (and left
(= col
1))
1495 (user-error "Cannot move column further left"))
1496 (when (and (not left
) (looking-at "[^|\n]*|[^|\n]*$"))
1497 (user-error "Cannot move column further right"))
1498 (org-table-save-field
1500 (while (< (point) end
)
1501 (unless (org-at-table-hline-p)
1502 (org-table-goto-column col1 t
)
1503 (when (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
1505 (match-beginning 1) (match-end 1)
1506 (match-beginning 2) (match-end 2))))
1508 (set-marker end nil
)
1509 (org-table-goto-column colpos
)
1511 (when (or (not org-table-fix-formulas-confirm
)
1512 (funcall org-table-fix-formulas-confirm
"Fix formulas? "))
1513 (org-table-fix-formulas
1514 "$" (list (cons (number-to-string col
) (number-to-string colpos
))
1515 (cons (number-to-string colpos
) (number-to-string col
))))
1516 (org-table-fix-formulas
1517 "$LR" (list (cons (number-to-string col
) (number-to-string colpos
))
1518 (cons (number-to-string colpos
) (number-to-string col
)))))))
1521 (defun org-table-move-row-down ()
1522 "Move table row down."
1524 (org-table-move-row nil
))
1526 (defun org-table-move-row-up ()
1527 "Move table row up."
1529 (org-table-move-row 'up
))
1532 (defun org-table-move-row (&optional up
)
1533 "Move the current table line down. With arg UP, move it up."
1535 (let* ((col (current-column))
1537 (hline1p (save-excursion (beginning-of-line 1)
1538 (looking-at org-table-hline-regexp
)))
1539 (dline1 (org-table-current-dline))
1540 (dline2 (+ dline1
(if up -
1 1)))
1543 (when (and up
(= (point-min) (line-beginning-position)))
1544 (user-error "Cannot move row further"))
1545 (beginning-of-line tonew
)
1546 (when (or (and (not up
) (eobp)) (not (org-at-table-p)))
1548 (user-error "Cannot move row further"))
1549 (setq hline2p
(looking-at org-table-hline-regexp
))
1551 (let ((row (delete-and-extract-region (line-beginning-position)
1552 (line-beginning-position 2))))
1553 (beginning-of-line tonew
)
1554 (unless (bolp) (insert "\n")) ;at eob without a newline
1556 (unless (bolp) (insert "\n")) ;missing final newline in ROW
1557 (beginning-of-line 0)
1558 (org-move-to-column col
)
1559 (unless (or hline1p hline2p
1560 (not (or (not org-table-fix-formulas-confirm
)
1561 (funcall org-table-fix-formulas-confirm
1562 "Fix formulas? "))))
1563 (org-table-fix-formulas
1565 (cons (number-to-string dline1
) (number-to-string dline2
))
1566 (cons (number-to-string dline2
) (number-to-string dline1
))))))))
1569 (defun org-table-insert-row (&optional arg
)
1570 "Insert a new row above the current line into the table.
1571 With prefix ARG, insert below the current line."
1573 (unless (org-at-table-p) (user-error "Not at a table"))
1574 (let* ((line (buffer-substring (line-beginning-position) (line-end-position)))
1575 (new (org-table-clean-line line
)))
1576 ;; Fix the first field if necessary
1577 (if (string-match "^[ \t]*| *[#$] *|" line
)
1578 (setq new
(replace-match (match-string 0 line
) t t new
)))
1579 (beginning-of-line (if arg
2 1))
1580 ;; Buffer may not end of a newline character, so ensure
1581 ;; (beginning-of-line 2) moves point to a new line.
1582 (unless (bolp) (insert "\n"))
1583 (let (org-table-may-need-update) (insert-before-markers new
"\n"))
1584 (beginning-of-line 0)
1585 (re-search-forward "| ?" (line-end-position) t
)
1586 (when (or org-table-may-need-update org-table-overlay-coordinates
)
1588 (when (or (not org-table-fix-formulas-confirm
)
1589 (funcall org-table-fix-formulas-confirm
"Fix formulas? "))
1590 (org-table-fix-formulas "@" nil
(1- (org-table-current-dline)) 1))))
1593 (defun org-table-insert-hline (&optional above
)
1594 "Insert a horizontal-line below the current line into the table.
1595 With prefix ABOVE, insert above the current line."
1597 (if (not (org-at-table-p))
1598 (user-error "Not at a table"))
1599 (when (eobp) (insert "\n") (backward-char 1))
1600 (if (not (string-match-p "|[ \t]*$" (org-current-line-string)))
1602 (let ((line (org-table-clean-line
1603 (buffer-substring (point-at-bol) (point-at-eol))))
1604 (col (current-column)))
1605 (while (string-match "|\\( +\\)|" line
)
1606 (setq line
(replace-match
1607 (concat "+" (make-string (- (match-end 1) (match-beginning 1))
1608 ?-
) "|") t t line
)))
1609 (and (string-match "\\+" line
) (setq line
(replace-match "|" t t line
)))
1610 (beginning-of-line (if above
1 2))
1612 (beginning-of-line (if above
1 -
1))
1613 (org-move-to-column col
)
1614 (and org-table-overlay-coordinates
(org-table-align))))
1617 (defun org-table-hline-and-move (&optional same-column
)
1618 "Insert a hline and move to the row below that line."
1620 (let ((col (org-table-current-column)))
1621 (org-table-maybe-eval-formula)
1622 (org-table-maybe-recalculate-line)
1623 (org-table-insert-hline)
1625 (if (looking-at "\n[ \t]*|-")
1626 (progn (insert "\n|") (org-table-align))
1627 (org-table-next-field))
1628 (if same-column
(org-table-goto-column col
))))
1630 (defun org-table-clean-line (s)
1631 "Convert a table line S into a string with only \"|\" and space.
1632 In particular, this does handle wide and invisible characters."
1633 (if (string-match "^[ \t]*|-" s
)
1634 ;; It's a hline, just map the characters
1635 (setq s
(mapconcat (lambda (x) (if (member x
'(?| ?
+)) "|" " ")) s
""))
1636 (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s
)
1637 (setq s
(replace-match
1638 (concat "|" (make-string (org-string-width (match-string 1 s
))
1644 (defun org-table-kill-row ()
1645 "Delete the current row or horizontal line from the table."
1647 (if (not (org-at-table-p))
1648 (user-error "Not at a table"))
1649 (let ((col (current-column))
1650 (dline (org-table-current-dline)))
1651 (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
1652 (if (not (org-at-table-p)) (beginning-of-line 0))
1653 (org-move-to-column col
)
1654 (when (or (not org-table-fix-formulas-confirm
)
1655 (funcall org-table-fix-formulas-confirm
"Fix formulas? "))
1656 (org-table-fix-formulas "@" (list (cons (number-to-string dline
) "INVALID"))
1660 (defun org-table-sort-lines
1661 (&optional with-case sorting-type getkey-func compare-func interactive?
)
1662 "Sort table lines according to the column at point.
1664 The position of point indicates the column to be used for
1665 sorting, and the range of lines is the range between the nearest
1666 horizontal separator lines, or the entire table of no such lines
1667 exist. If point is before the first column, you will be prompted
1668 for the sorting column. If there is an active region, the mark
1669 specifies the first line and the sorting column, while point
1670 should be in the last line to be included into the sorting.
1672 The command then prompts for the sorting type which can be
1673 alphabetically, numerically, or by time (as given in a time stamp
1674 in the field, or as a HH:MM value). Sorting in reverse order is
1677 With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive.
1679 If SORTING-TYPE is specified when this function is called from a Lisp
1680 program, no prompting will take place. SORTING-TYPE must be a character,
1681 any of (?a ?A ?n ?N ?t ?T ?f ?F) where the capital letters indicate that
1682 sorting should be done in reverse order.
1684 If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies
1685 a function to be called to extract the key. It must return a value
1686 that is compatible with COMPARE-FUNC, the function used to compare
1689 A non-nil value for INTERACTIVE? is used to signal that this
1690 function is being called interactively."
1691 (interactive (list current-prefix-arg nil nil nil t
))
1692 (when (org-region-active-p) (goto-char (region-beginning)))
1693 ;; Point must be either within a field or before a data line.
1695 (skip-chars-backward " \t")
1696 (when (bolp) (search-forward "|" (line-end-position) t
))
1697 (org-table-check-inside-data-field))
1698 ;; Set appropriate case sensitivity and column used for sorting.
1699 (let ((column (let ((c (org-table-current-column)))
1702 (read-number "Use column N for sorting: "))
1706 (read-char-exclusive "Sort Table: [a]lphabetic, [n]umeric, \
1707 \[t]ime, [f]unc. A/N/T/F means reversed: "))))
1709 ;; Narrow buffer to appropriate sorting area.
1710 (if (org-region-active-p)
1711 (progn (goto-char (region-beginning))
1714 (save-excursion (goto-char (region-end))
1715 (line-beginning-position 2))))
1716 (let ((start (org-table-begin))
1717 (end (org-table-end)))
1720 (if (re-search-backward org-table-hline-regexp start t
)
1721 (line-beginning-position 2)
1723 (if (save-excursion (re-search-forward org-table-hline-regexp end t
))
1726 ;; Determine arguments for `sort-subr'. Also record original
1727 ;; position. `org-table-save-field' cannot help here since
1728 ;; sorting is too much destructive.
1729 (let* ((sort-fold-case (not with-case
))
1731 (cons (count-lines (point-min) (line-beginning-position))
1733 (extract-key-from-field
1734 ;; Function to be called on the contents of the field
1735 ;; used for sorting in the current row.
1736 (cl-case sorting-type
1737 ((?n ?N
) #'string-to-number
)
1738 ((?a ?A
) #'org-sort-remove-invisible
)
1741 (cond ((string-match org-ts-regexp-both f
)
1743 (org-time-string-to-time (match-string 0 f
))))
1744 ((string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" f
)
1745 (org-duration-to-minutes (match-string 0 f
)))
1750 (org-read-function "Function for extracting keys: "))
1751 (error "Missing key extractor to sort rows")))
1752 (t (user-error "Invalid sorting type `%c'" sorting-type
))))
1754 (cl-case sorting-type
1761 (concat "Function for comparing keys "
1762 "(empty for default `sort-subr' predicate): ")
1764 (goto-char (point-min))
1765 (sort-subr (memq sorting-type
'(?A ?N ?T ?F
))
1768 (while (and (not (eobp))
1769 (not (looking-at org-table-dataline-regexp
)))
1773 (funcall extract-key-from-field
1774 (org-trim (org-table-get-field column
))))
1777 ;; Move back to initial field.
1778 (forward-line (car coordinates
))
1779 (move-to-column (cdr coordinates
))))))
1782 (defun org-table-cut-region (beg end
)
1783 "Copy region in table to the clipboard and blank all relevant fields.
1784 If there is no active region, use just the field at point."
1786 (if (org-region-active-p) (region-beginning) (point))
1787 (if (org-region-active-p) (region-end) (point))))
1788 (org-table-copy-region beg end
'cut
))
1791 (defun org-table-copy-region (beg end
&optional cut
)
1792 "Copy rectangular region in table to clipboard.
1793 A special clipboard is used which can only be accessed
1794 with `org-table-paste-rectangle'."
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))
1804 (goto-char (max beg end
))
1805 (org-table-check-inside-data-field)
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
" ")))
1813 (while (< (point) end
)
1814 (unless (org-at-table-hline-p)
1815 ;; Collect every cell between COLUMN-START and COLUMN-END.
1817 (dotimes (c column-number
)
1818 (push (org-table-get-field (+ c column-start
) rpl
) cols
))
1819 (push (nreverse cols
) region
)))
1821 (set-marker end nil
))
1822 (when cut
(org-table-align))
1823 (setq org-table-clip
(nreverse region
))))
1826 (defun org-table-paste-rectangle ()
1827 "Paste a rectangular region into a table.
1828 The upper right corner ends up in the current field. All involved fields
1829 will be overwritten. If the rectangle does not fit into the present table,
1830 the table is enlarged as needed. The process ignores horizontal separator
1833 (unless (consp org-table-clip
)
1834 (user-error "First cut/copy a region to paste!"))
1835 (org-table-check-inside-data-field)
1836 (let* ((column (org-table-current-column))
1837 (org-table-automatic-realign nil
))
1838 (org-table-save-field
1839 (dolist (row org-table-clip
)
1840 (while (org-at-table-hline-p) (forward-line))
1841 ;; If we left the table, create a new row.
1842 (when (and (bolp) (not (looking-at "[ \t]*|")))
1844 (org-table-next-field))
1847 (org-table-goto-column c nil
'force
)
1848 (org-table-get-field nil field
)
1854 (defun org-table-convert ()
1855 "Convert from `org-mode' table to table.el and back.
1856 Obviously, this only works within limits. When an Org table is converted
1857 to table.el, all horizontal separator lines get lost, because table.el uses
1858 these as cell boundaries and has no notion of horizontal lines. A table.el
1859 table can be converted to an Org table only if it does not do row or column
1860 spanning. Multiline cells will become multiple cells. Beware, Org mode
1861 does not test if the table can be successfully converted - it blindly
1862 applies a recipe that works for simple tables."
1865 (if (org-at-table.el-p
)
1866 ;; convert to Org table
1867 (let ((beg (copy-marker (org-table-begin t
)))
1868 (end (copy-marker (org-table-end t
))))
1869 (table-unrecognize-region beg end
)
1871 (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t
)
1874 (if (org-at-table-p)
1875 ;; convert to table.el table
1876 (let ((beg (copy-marker (org-table-begin)))
1877 (end (copy-marker (org-table-end))))
1878 ;; first, get rid of all horizontal lines
1880 (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t
)
1882 ;; insert a hline before first
1884 (org-table-insert-hline 'above
)
1885 (beginning-of-line -
1)
1886 ;; insert a hline after each line
1887 (while (progn (beginning-of-line 3) (< (point) end
))
1888 (org-table-insert-hline))
1890 (setq end
(move-marker end
(org-table-end)))
1891 ;; replace "+" at beginning and ending of hlines
1892 (while (re-search-forward "^\\([ \t]*\\)|-" end t
)
1893 (replace-match "\\1+-"))
1895 (while (re-search-forward "-|[ \t]*$" end t
)
1896 (replace-match "-+"))
1899 (defun org-table-transpose-table-at-point ()
1900 "Transpose Org table at point and eliminate hlines.
1908 will be transposed as
1915 Note that horizontal lines disappear."
1917 (let* ((table (delete 'hline
(org-table-to-lisp)))
1918 (dline_old (org-table-current-line))
1919 (col_old (org-table-current-column))
1920 (contents (mapcar (lambda (_)
1926 (setq tp
(cdr tp
))))
1929 (goto-char (org-table-begin))
1930 (re-search-forward "|")
1932 (delete-region (point) (org-table-end))
1935 (concat "| " (mapconcat 'identity x
" | " ) " |\n" ))
1937 (org-table-goto-line col_old
)
1938 (org-table-goto-column dline_old
))
1942 (defun org-table-wrap-region (arg)
1943 "Wrap several fields in a column like a paragraph.
1944 This is useful if you'd like to spread the contents of a field over several
1945 lines, in order to keep the table compact.
1947 If there is an active region, and both point and mark are in the same column,
1948 the text in the column is wrapped to minimum width for the given number of
1949 lines. Generally, this makes the table more compact. A prefix ARG may be
1950 used to change the number of desired lines. For example, \
1951 `C-2 \\[org-table-wrap-region]'
1952 formats the selected text to two lines. If the region was longer than two
1953 lines, the remaining lines remain empty. A negative prefix argument reduces
1954 the current number of lines by that amount. The wrapped text is pasted back
1955 into the table. If you formatted it to more lines than it was before, fields
1956 further down in the table get overwritten - so you might need to make space in
1959 If there is no region, the current field is split at the cursor position and
1960 the text fragment to the right of the cursor is prepended to the field one
1963 If there is no region, but you specify a prefix ARG, the current field gets
1964 blank, and the content is appended to the field above."
1966 (org-table-check-inside-data-field)
1967 (if (org-region-active-p)
1968 ;; There is a region: fill as a paragraph.
1969 (let ((start (region-beginning)))
1970 (org-table-cut-region (region-beginning) (region-end))
1971 (when (> (length (car org-table-clip
)) 1)
1972 (user-error "Region must be limited to single column"))
1973 (let ((nlines (cond ((not arg
) (length org-table-clip
))
1974 ((< arg
1) (+ (length org-table-clip
) arg
))
1976 (setq org-table-clip
1978 (org-wrap (mapconcat #'car org-table-clip
" ")
1982 (org-table-paste-rectangle))
1983 ;; No region, split the current field at point.
1984 (unless (org-get-alist-option org-M-RET-may-split-line
'table
)
1985 (skip-chars-forward "^\r\n|"))
1987 (arg ; Combine with field above.
1988 (let ((s (org-table-blank-field))
1989 (col (org-table-current-column)))
1991 (while (org-at-table-hline-p) (forward-line -
1))
1992 (org-table-goto-column col
)
1993 (skip-chars-forward "^|")
1994 (skip-chars-backward " ")
1995 (insert " " (org-trim s
))
1997 ((looking-at "\\([^|]+\\)+|") ; Split field.
1998 (let ((s (match-string 1)))
1999 (replace-match " |")
2000 (goto-char (match-beginning 0))
2001 (org-table-next-row)
2002 (insert (org-trim s
) " ")
2004 (t (org-table-next-row)))))
2006 (defvar org-field-marker nil
)
2009 (defun org-table-edit-field (arg)
2010 "Edit table field in a different window.
2011 This is mainly useful for fields that contain hidden parts.
2013 When called with a `\\[universal-argument]' prefix, just make the full field
2014 visible so that it can be edited in place.
2016 When called with a `\\[universal-argument] \\[universal-argument]' prefix, \
2017 toggle `org-table-follow-field-mode'."
2019 (unless (org-at-table-p) (user-error "Not at a table"))
2022 (org-table-follow-field-mode (if org-table-follow-field-mode -
1 1)))
2024 (let ((b (save-excursion (skip-chars-backward "^|") (point)))
2025 (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
2026 (remove-text-properties b e
'(org-cwidth t invisible t
2027 display t intangible t
))
2028 (if (and (boundp 'font-lock-mode
) font-lock-mode
)
2029 (font-lock-fontify-block))))
2031 (let ((pos (point-marker))
2033 (if (eq org-table-use-standard-references t
)
2034 (concat (org-number-to-letters (org-table-current-column))
2035 (int-to-string (org-table-current-dline)))
2036 (concat "@" (int-to-string (org-table-current-dline))
2037 "$" (int-to-string (org-table-current-column)))))
2038 (field (org-table-get-field))
2039 (cw (current-window-configuration))
2042 (org-switch-to-buffer-other-window "*Org Table Edit Field*")
2043 (when (and (local-variable-p 'org-field-marker
)
2044 (markerp org-field-marker
))
2045 (move-marker org-field-marker nil
))
2047 (insert "#\n# Edit field " coord
" and finish with C-c C-c\n#\n")
2048 (let ((org-inhibit-startup t
)) (org-mode))
2050 (setq truncate-lines nil
)
2052 (goto-char (setq p
(point-max)))
2053 (insert (org-trim field
))
2054 (remove-text-properties p
(point-max)
2055 '(invisible t org-cwidth t display t
2058 (setq-local org-finish-function
'org-table-finish-edit-field
)
2059 (setq-local org-window-configuration cw
)
2060 (setq-local org-field-marker pos
)
2061 (message "Edit and finish with C-c C-c")))))
2063 (defun org-table-finish-edit-field ()
2064 "Finish editing a table data field.
2065 Remove all newline characters, insert the result into the table, realign
2066 the table and kill the editing buffer."
2067 (let ((pos org-field-marker
)
2068 (cw org-window-configuration
)
2069 (cb (current-buffer))
2071 (goto-char (point-min))
2072 (while (re-search-forward "^#.*\n?" nil t
) (replace-match ""))
2073 (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t
)
2074 (replace-match " "))
2075 (setq text
(org-trim (buffer-string)))
2076 (set-window-configuration cw
)
2078 (select-window (get-buffer-window (marker-buffer pos
)))
2080 (move-marker pos nil
)
2081 (org-table-check-inside-data-field)
2082 (org-table-get-field nil text
)
2084 (message "New field value inserted")))
2086 (define-minor-mode org-table-follow-field-mode
2087 "Minor mode to make the table field editor window follow the cursor.
2088 When this mode is active, the field editor window will always show the
2089 current field. The mode exits automatically when the cursor leaves the
2090 table (but see `org-table-exit-follow-field-mode-when-leaving-table')."
2091 nil
" TblFollow" nil
2092 (if org-table-follow-field-mode
2093 (add-hook 'post-command-hook
'org-table-follow-fields-with-editor
2095 (remove-hook 'post-command-hook
'org-table-follow-fields-with-editor
'local
)
2096 (let* ((buf (get-buffer "*Org Table Edit Field*"))
2097 (win (and buf
(get-buffer-window buf
))))
2098 (when win
(delete-window win
))
2100 (with-current-buffer buf
2101 (move-marker org-field-marker nil
))
2102 (kill-buffer buf
)))))
2104 (defun org-table-follow-fields-with-editor ()
2105 (if (and org-table-exit-follow-field-mode-when-leaving-table
2106 (not (org-at-table-p)))
2107 ;; We have left the table, exit the follow mode
2108 (org-table-follow-field-mode -
1)
2109 (when (org-table-check-inside-data-field 'noerror
)
2110 (let ((win (selected-window)))
2111 (org-table-edit-field nil
)
2112 (org-fit-window-to-buffer)
2113 (select-window win
)))))
2115 (defvar org-timecnt
) ; dynamically scoped parameter
2118 (defun org-table-sum (&optional beg end nlast
)
2119 "Sum numbers in region of current table column.
2120 The result will be displayed in the echo area, and will be available
2121 as kill to be inserted with \\[yank].
2123 If there is an active region, it is interpreted as a rectangle and all
2124 numbers in that rectangle will be summed. If there is no active
2125 region and point is located in a table column, sum all numbers in that
2128 If at least one number looks like a time HH:MM or HH:MM:SS, all other
2129 numbers are assumed to be times as well (in decimal hours) and the
2130 numbers are added as such.
2132 If NLAST is a number, only the NLAST fields will actually be summed."
2135 (let (col (org-timecnt 0) diff h m s org-table-clip
)
2137 ((and beg end
)) ; beg and end given explicitly
2138 ((org-region-active-p)
2139 (setq beg
(region-beginning) end
(region-end)))
2141 (setq col
(org-table-current-column))
2142 (goto-char (org-table-begin))
2143 (unless (re-search-forward "^[ \t]*|[^-]" nil t
)
2144 (user-error "No table data"))
2145 (org-table-goto-column col
)
2147 (goto-char (org-table-end))
2148 (unless (re-search-backward "^[ \t]*|[^-]" nil t
)
2149 (user-error "No table data"))
2150 (org-table-goto-column col
)
2151 (setq end
(point))))
2152 (let* ((items (apply 'append
(org-table-copy-region beg end
)))
2153 (items1 (cond ((not nlast
) items
)
2154 ((>= nlast
(length items
)) items
)
2155 (t (setq items
(reverse items
))
2156 (setcdr (nthcdr (1- nlast
) items
) nil
)
2158 (numbers (delq nil
(mapcar 'org-table-get-number-for-summing
2160 (res (apply '+ numbers
))
2161 (sres (if (= org-timecnt
0)
2162 (number-to-string res
)
2163 (setq diff
(* 3600 res
)
2164 h
(floor (/ diff
3600)) diff
(mod diff
3600)
2165 m
(floor (/ diff
60)) diff
(mod diff
60)
2167 (format "%.0f:%02.0f:%02.0f" h m s
))))
2169 (when (called-interactively-p 'interactive
)
2170 (message "%s" (substitute-command-keys
2171 (format "Sum of %d items: %-20s \
2172 \(\\[yank] will insert result into buffer)" (length numbers
) sres
))))
2175 (defun org-table-get-number-for-summing (s)
2177 (if (string-match "^ *|? *" s
)
2178 (setq s
(replace-match "" nil nil s
)))
2179 (if (string-match " *|? *$" s
)
2180 (setq s
(replace-match "" nil nil s
)))
2181 (setq n
(string-to-number s
))
2183 ((and (string-match "0" s
)
2184 (string-match "\\`[-+ \t0.edED]+\\'" s
)) 0)
2185 ((string-match "\\`[ \t]+\\'" s
) nil
)
2186 ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s
)
2187 (let ((h (string-to-number (or (match-string 1 s
) "0")))
2188 (m (string-to-number (or (match-string 2 s
) "0")))
2189 (s (string-to-number (or (match-string 4 s
) "0"))))
2190 (if (boundp 'org-timecnt
) (setq org-timecnt
(1+ org-timecnt
)))
2191 (* 1.0 (+ h
(/ m
60.0) (/ s
3600.0)))))
2195 (defun org-table-current-field-formula (&optional key noerror
)
2196 "Return the formula active for the current field.
2198 Assumes that table is already analyzed. If KEY is given, return
2199 the key to this formula. Otherwise return the formula preceded
2200 with \"=\" or \":=\"."
2201 (let* ((line (count-lines org-table-current-begin-pos
2202 (line-beginning-position)))
2203 (row (org-table-line-to-dline line
)))
2206 (let* ((col (org-table-current-column))
2207 (name (car (rassoc (list line col
)
2208 org-table-named-field-locations
)))
2209 (scol (format "$%d" col
))
2210 (ref (format "@%d$%d" (org-table-current-dline) col
))
2211 (stored-list (org-table-get-stored-formulas noerror
))
2212 (ass (or (assoc name stored-list
)
2213 (assoc ref stored-list
)
2214 (assoc scol stored-list
))))
2215 (cond (key (car ass
))
2216 (ass (concat (if (string-match-p "^[0-9]+$" (car ass
)) "=" ":=")
2219 (t (error "No formula active for the current field")))))
2221 (defun org-table-get-formula (&optional equation named
)
2222 "Read a formula from the minibuffer, offer stored formula as default.
2223 When NAMED is non-nil, look for a named equation."
2224 (let* ((stored-list (org-table-get-stored-formulas))
2225 (name (car (rassoc (list (count-lines org-table-current-begin-pos
2226 (line-beginning-position))
2227 (org-table-current-column))
2228 org-table-named-field-locations
)))
2229 (ref (format "@%d$%d"
2230 (org-table-current-dline)
2231 (org-table-current-column)))
2233 ((not named
) (format "$%d" (org-table-current-column)))
2234 ((and name
(not (string-match "\\`LR[0-9]+\\'" name
))) name
)
2236 (name (or name ref
))
2237 (org-table-may-need-update nil
)
2238 (stored (cdr (assoc scol stored-list
)))
2240 ((and stored equation
(string-match-p "^ *=? *$" equation
))
2244 (t (org-table-formula-from-user
2246 (org-table-formula-to-user
2247 (format "%s formula %s="
2248 (if named
"Field" "Column")
2250 (if stored
(org-table-formula-to-user stored
) "")
2251 'org-table-formula-history
2254 (when (not (string-match "\\S-" eq
))
2256 (setq stored-list
(delq (assoc scol stored-list
) stored-list
))
2257 (org-table-store-formulas stored-list
)
2258 (user-error "Formula removed"))
2259 (if (string-match "^ *=?" eq
) (setq eq
(replace-match "" t t eq
)))
2260 (if (string-match " *$" eq
) (setq eq
(replace-match "" t t eq
)))
2261 (if (and name
(not named
))
2262 ;; We set the column equation, delete the named one.
2263 (setq stored-list
(delq (assoc name stored-list
) stored-list
)
2266 (setcdr (assoc scol stored-list
) eq
)
2267 (setq stored-list
(cons (cons scol eq
) stored-list
)))
2268 (if (or mustsave
(not (equal stored eq
)))
2269 (org-table-store-formulas stored-list
))
2272 (defun org-table-store-formulas (alist &optional location
)
2273 "Store the list of formulas below the current table.
2274 If optional argument LOCATION is a buffer position, insert it at
2278 (progn (goto-char location
) (beginning-of-line))
2279 (goto-char (org-table-end)))
2280 (let ((case-fold-search t
))
2281 (if (looking-at "\\([ \t]*\n\\)*[ \t]*\\(#\\+TBLFM:\\)\\(.*\n?\\)")
2283 ;; Don't overwrite TBLFM, we might use text properties to
2285 (goto-char (match-beginning 3))
2286 (delete-region (match-beginning 3) (match-end 0)))
2288 (insert (or (match-string 2) "#+TBLFM:")))
2290 (mapconcat (lambda (x) (concat (car x
) "=" (cdr x
)))
2291 (sort alist
#'org-table-formula-less-p
)
2295 (defsubst org-table-formula-make-cmp-string
(a)
2296 (when (string-match "\\`$[<>]" a
)
2297 (let ((arrow (string-to-char (substring a
1))))
2298 ;; Fake a high number to make sure this is sorted at the end.
2299 (setq a
(org-table-formula-handle-first/last-rc a
))
2300 (setq a
(format "$%d" (+ 10000
2301 (if (= arrow ?
<) -
1000 0)
2302 (string-to-number (substring a
1)))))))
2304 "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?"
2308 (format "@%05d" (string-to-number (match-string 2 a
))) "")
2310 (format "$%05d" (string-to-number (match-string 4 a
))) "")
2312 (concat "@@" (match-string 5 a
))))))
2314 (defun org-table-formula-less-p (a b
)
2315 "Compare two formulas for sorting."
2316 (let ((as (org-table-formula-make-cmp-string (car a
)))
2317 (bs (org-table-formula-make-cmp-string (car b
))))
2318 (and as bs
(string< as bs
))))
2321 (defun org-table-get-stored-formulas (&optional noerror location
)
2322 "Return an alist with the stored formulas directly after current table.
2323 By default, only return active formulas, i.e., formulas located
2324 on the first line after the table. However, if optional argument
2325 LOCATION is a buffer position, consider the formulas there."
2328 (progn (goto-char location
) (beginning-of-line))
2329 (goto-char (org-table-end)))
2330 (let ((case-fold-search t
))
2331 (when (looking-at "\\([ \t]*\n\\)*[ \t]*#\\+TBLFM: *\\(.*\\)")
2332 (let ((strings (org-split-string (match-string-no-properties 2)
2335 (dolist (string strings
(nreverse eq-alist
))
2336 (when (string-match "\\`\\(@[-+I<>0-9.$@]+\\|\\$\\([_a-zA-Z0-9]+\\|\
2337 [<>]+\\)\\) *= *\\(.*[^ \t]\\)"
2340 (let ((m (match-string 1 string
)))
2342 ((not (match-end 2)) m
)
2343 ;; Is it a column reference?
2344 ((string-match-p "\\`$\\([0-9]+\\|[<>]+\\)\\'" m
) m
)
2345 ;; Since named columns are not possible in
2346 ;; LHS, assume this is a named field.
2347 (t (match-string 2 string
)))))
2348 (rhs (match-string 3 string
)))
2349 (push (cons lhs rhs
) eq-alist
)
2351 ((not (member lhs seen
)) (push lhs seen
))
2354 "Double definition `%s=' in TBLFM line, please fix by hand"
2360 "Double definition `%s=' in TBLFM line, please fix by hand"
2363 (defun org-table-fix-formulas (key replace
&optional limit delta remove
)
2364 "Modify the equations after the table structure has been edited.
2365 KEY is \"@\" or \"$\". REPLACE is an alist of numbers to replace.
2366 For all numbers larger than LIMIT, shift them by DELTA."
2368 (goto-char (org-table-end))
2369 (while (let ((case-fold-search t
)) (looking-at "[ \t]*#\\+tblfm:"))
2370 (let ((msg "The formulas in #+TBLFM have been updated")
2371 (re (concat key
"\\([0-9]+\\)"))
2374 (if (or (equal key
"$") (equal key
"$LR"))
2375 (format "\\(@[0-9]+\\)?%s%d=.*?\\(::\\|$\\)"
2376 (regexp-quote key
) remove
)
2377 (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove
))))
2380 (while (re-search-forward re2
(point-at-eol) t
)
2381 (unless (save-match-data (org-in-regexp "remote([^)]+?)"))
2382 (if (equal (char-before (match-beginning 0)) ?.
)
2384 "Change makes TBLFM term %s invalid, use undo to recover"
2386 (replace-match "")))))
2387 (while (re-search-forward re
(point-at-eol) t
)
2388 (unless (save-match-data (org-in-regexp "remote([^)]+?)"))
2389 (setq s
(match-string 1) n
(string-to-number s
))
2391 ((setq a
(assoc s replace
))
2392 (replace-match (concat key
(cdr a
)) t t
)
2394 ((and limit
(> n limit
))
2395 (replace-match (concat key
(int-to-string (+ n delta
))) t t
)
2400 (defun org-table-maybe-eval-formula ()
2401 "Check if the current field starts with \"=\" or \":=\".
2402 If yes, store the formula and apply it."
2403 ;; We already know we are in a table. Get field will only return a formula
2404 ;; when appropriate. It might return a separator line, but no problem.
2405 (when org-table-formula-evaluate-inline
2406 (let* ((field (org-trim (or (org-table-get-field) "")))
2408 (when (string-match "^:?=\\(.*[^=]\\)$" field
)
2409 (setq named
(equal (string-to-char field
) ?
:)
2410 eq
(match-string 1 field
))
2411 (org-table-eval-formula (and named
'(4))
2412 (org-table-formula-from-user eq
))))))
2414 (defvar org-recalc-commands nil
2415 "List of commands triggering the recalculation of a line.
2416 Will be filled automatically during use.")
2418 (defvar org-recalc-marks
2419 '((" " .
"Unmarked: no special line, no automatic recalculation")
2420 ("#" .
"Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
2421 ("*" .
"Recalculate only when entire table is recalculated with `C-u C-c *'")
2422 ("!" .
"Column name definition line. Reference in formula as $name.")
2423 ("$" .
"Parameter definition line name=value. Reference in formula as $name.")
2424 ("_" .
"Names for values in row below this one.")
2425 ("^" .
"Names for values in row above this one.")))
2428 (defun org-table-rotate-recalc-marks (&optional newchar
)
2429 "Rotate the recalculation mark in the first column.
2430 If in any row, the first field is not consistent with a mark,
2431 insert a new column for the markers.
2432 When there is an active region, change all the lines in the region,
2433 after prompting for the marking character.
2434 After each change, a message will be displayed indicating the meaning
2437 (unless (org-at-table-p) (user-error "Not at a table"))
2438 (let* ((region (org-region-active-p))
2440 (save-excursion (goto-char (region-beginning))
2441 (copy-marker (line-beginning-position)))))
2443 (save-excursion (goto-char (region-end))
2444 (copy-marker (line-beginning-position)))))
2445 (l (copy-marker (line-beginning-position)))
2446 (col (org-table-current-column))
2449 (read-char-exclusive
2450 "Change region to what mark? Type # * ! $ or SPC: "))
2454 (goto-char (org-table-begin))
2456 "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" (org-table-end) t
))))
2457 (when (and newchar
(not (assoc newchar org-recalc-marks
)))
2458 (user-error "Invalid character `%s' in `org-table-rotate-recalc-marks'"
2460 (when l1
(goto-char l1
))
2463 (unless (looking-at org-table-dataline-regexp
)
2464 (user-error "Not at a table data line")))
2465 (when no-special-column
2466 (org-table-goto-column 1)
2467 (org-table-insert-column))
2468 (let ((previous-line-end (line-end-position))
2472 (cond ((not (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")) "#")
2474 (t (cadr (member (match-string 1)
2475 (append (mapcar #'car org-recalc-marks
)
2477 ;; Rotate mark in first row.
2478 (org-table-get-field 1 (format " %s " newchar
))
2479 ;; Rotate marks in additional rows if a region is active.
2483 (while (<= (point) l2
)
2484 (when (looking-at org-table-dataline-regexp
)
2485 (org-table-get-field 1 (format " %s " newchar
)))
2487 ;; Only align if rotation actually changed lines' length.
2488 (when (/= previous-line-end
(line-end-position)) (org-table-align)))
2490 (org-table-goto-column (if no-special-column
(1+ col
) col
))
2491 (when l1
(set-marker l1 nil
))
2492 (when l2
(set-marker l2 nil
))
2494 (when (called-interactively-p 'interactive
)
2495 (message "%s" (cdr (assoc newchar org-recalc-marks
))))))
2498 (defun org-table-analyze ()
2499 "Analyze table at point and store results.
2501 This function sets up the following dynamically scoped variables:
2503 `org-table-column-name-regexp',
2504 `org-table-column-names',
2505 `org-table-current-begin-pos',
2506 `org-table-current-line-types',
2507 `org-table-current-ncol',
2510 `org-table-local-parameters',
2511 `org-table-named-field-locations'."
2512 (let ((beg (org-table-begin))
2513 (end (org-table-end)))
2516 ;; Extract column names.
2517 (setq org-table-column-names nil
)
2518 (when (save-excursion
2519 (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t
))
2521 (dolist (name (org-split-string (match-string 1) " *| *"))
2523 (when (string-match "\\`[a-zA-Z][_a-zA-Z0-9]*\\'" name
)
2524 (push (cons name
(int-to-string c
)) org-table-column-names
)))))
2525 (setq org-table-column-names
(nreverse org-table-column-names
))
2526 (setq org-table-column-name-regexp
2527 (format "\\$\\(%s\\)\\>"
2528 (regexp-opt (mapcar #'car org-table-column-names
) t
)))
2529 ;; Extract local parameters.
2530 (setq org-table-local-parameters nil
)
2532 (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t
)
2533 (dolist (field (org-split-string (match-string 1) " *| *"))
2535 "\\`\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field
)
2536 (push (cons (match-string 1 field
) (match-string 2 field
))
2537 org-table-local-parameters
)))))
2538 ;; Update named fields locations. We minimize `count-lines'
2539 ;; processing by storing last known number of lines in LAST.
2540 (setq org-table-named-field-locations nil
)
2542 (let ((last (cons (point) 0)))
2543 (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t
)
2544 (let ((c (match-string 1))
2545 (fields (org-split-string (match-string 2) " *| *")))
2547 (forward-line (if (equal c
"_") 1 -
1))
2549 (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
2550 (org-split-string (match-string 1) " *| *")))
2551 (line (cl-incf (cdr last
) (count-lines (car last
) (point))))
2553 (setcar last
(point)) ; Update last known position.
2554 (while (and fields fields1
)
2555 (let ((field (pop fields
))
2558 (when (and (stringp field
)
2560 (string-match "\\`[a-zA-Z][_a-zA-Z0-9]*\\'"
2562 (push (cons field v
) org-table-local-parameters
)
2563 (push (list field line col
)
2564 org-table-named-field-locations
))))))))))
2565 ;; Re-use existing markers when possible.
2566 (if (markerp org-table-current-begin-pos
)
2567 (move-marker org-table-current-begin-pos
(point))
2568 (setq org-table-current-begin-pos
(point-marker)))
2569 ;; Analyze the line types.
2570 (let ((l 0) hlines dlines types
)
2571 (while (looking-at "[ \t]*|\\(-\\)?")
2572 (push (if (match-end 1) 'hline
'dline
) types
)
2573 (if (match-end 1) (push l hlines
) (push l dlines
))
2576 (push 'hline types
) ; Add an imaginary extra hline to the end.
2577 (setq org-table-current-line-types
(apply #'vector
(nreverse types
)))
2578 (setq org-table-dlines
(apply #'vector
(cons nil
(nreverse dlines
))))
2579 (setq org-table-hlines
(apply #'vector
(cons nil
(nreverse hlines
)))))
2580 ;; Get the number of columns from the first data line in table.
2582 (forward-line (aref org-table-dlines
1))
2585 (buffer-substring (line-beginning-position) (line-end-position))
2587 (nfields (length fields
))
2589 (setq org-table-current-ncol nfields
)
2591 (aref org-table-dlines
(1- (length org-table-dlines
)))))
2592 (dotimes (i nfields
)
2593 (let ((column (1+ i
)))
2594 (push (list (format "LR%d" column
) last-dline column
) al
)
2595 (push (cons (format "LR%d" column
) (nth i fields
)) al2
))))
2596 (setq org-table-named-field-locations
2597 (append org-table-named-field-locations al
))
2598 (setq org-table-local-parameters
2599 (append org-table-local-parameters al2
))))))
2601 (defun org-table-goto-field (ref &optional create-column-p
)
2602 "Move point to a specific field in the current table.
2604 REF is either the name of a field its absolute reference, as
2605 a string. No column is created unless CREATE-COLUMN-P is
2606 non-nil. If it is a function, it is called with the column
2607 number as its argument as is used as a predicate to know if the
2608 column can be created.
2610 This function assumes the table is already analyzed (i.e., using
2611 `org-table-analyze')."
2614 ((cdr (assoc ref org-table-named-field-locations
)))
2615 ((string-match "\\`@\\([1-9][0-9]*\\)\\$\\([1-9][0-9]*\\)\\'" ref
)
2616 (list (condition-case nil
2617 (aref org-table-dlines
2618 (string-to-number (match-string 1 ref
)))
2619 (error (user-error "Invalid row number in %s" ref
)))
2620 (string-to-number (match-string 2 ref
))))
2621 (t (user-error "Unknown field: %s" ref
))))
2622 (line (car coordinates
))
2623 (column (nth 1 coordinates
))
2624 (create-new-column (if (functionp create-column-p
)
2625 (funcall create-column-p column
)
2628 (goto-char org-table-current-begin-pos
)
2630 (org-table-goto-column column nil create-new-column
))))
2633 (defun org-table-maybe-recalculate-line ()
2634 "Recompute the current line if marked for it, and if we haven't just done it."
2636 (and org-table-allow-automatic-line-recalculation
2637 (not (and (memq last-command org-recalc-commands
)
2638 (eq org-last-recalc-line
(line-beginning-position))))
2639 (save-excursion (beginning-of-line 1)
2640 (looking-at org-table-auto-recalculate-regexp
))
2641 (org-table-recalculate) t
))
2643 (defvar org-tbl-calc-modes
) ;; Dynamically bound in `org-table-eval-formula'
2644 (defsubst org-set-calc-mode
(var &optional value
)
2646 (setq var
(assoc var
'(("D" calc-angle-mode deg
)
2647 ("R" calc-angle-mode rad
)
2648 ("F" calc-prefer-frac t
)
2649 ("S" calc-symbolic-mode t
)))
2650 value
(nth 2 var
) var
(nth 1 var
)))
2651 (if (memq var org-tbl-calc-modes
)
2652 (setcar (cdr (memq var org-tbl-calc-modes
)) value
)
2653 (cons var
(cons value org-tbl-calc-modes
)))
2657 (defun org-table-eval-formula (&optional arg equation
2658 suppress-align suppress-const
2659 suppress-store suppress-analysis
)
2660 "Replace the table field value at the cursor by the result of a calculation.
2662 In a table, this command replaces the value in the current field with the
2663 result of a formula. It also installs the formula as the \"current\" column
2664 formula, by storing it in a special line below the table. When called
2665 with a `\\[universal-argument]' prefix the formula is installed as a \
2668 When called with a `\\[universal-argument] \\[universal-argument]' prefix, \
2669 insert the active equation for the field
2670 back into the current field, so that it can be edited there. This is \
2672 in order to use \\<org-table-fedit-map>`\\[org-table-show-reference]' to \
2673 check the referenced fields.
2675 When called, the command first prompts for a formula, which is read in
2676 the minibuffer. Previously entered formulas are available through the
2677 history list, and the last used formula is offered as a default.
2678 These stored formulas are adapted correctly when moving, inserting, or
2679 deleting columns with the corresponding commands.
2681 The formula can be any algebraic expression understood by the Calc package.
2682 For details, see the Org mode manual.
2684 This function can also be called from Lisp programs and offers
2685 additional arguments: EQUATION can be the formula to apply. If this
2686 argument is given, the user will not be prompted.
2688 SUPPRESS-ALIGN is used to speed-up recursive calls by by-passing
2691 SUPPRESS-CONST suppresses the interpretation of constants in the
2692 formula, assuming that this has been done already outside the
2695 SUPPRESS-STORE means the formula should not be stored, either
2696 because it is already stored, or because it is a modified
2697 equation that should not overwrite the stored one.
2699 SUPPRESS-ANALYSIS prevents analyzing the table and checking
2702 (unless suppress-analysis
2703 (org-table-check-inside-data-field)
2704 (org-table-analyze))
2705 (if (equal arg
'(16))
2706 (let ((eq (org-table-current-field-formula)))
2707 (org-table-get-field nil eq
)
2709 (setq org-table-may-need-update t
))
2711 (ndown (if (integerp arg
) arg
1))
2712 (org-table-automatic-realign nil
)
2713 (case-fold-search nil
)
2715 (formula (if (and equation suppress-store
)
2717 (org-table-get-formula equation
(equal arg
'(4)))))
2718 (n0 (org-table-current-column))
2719 (org-tbl-calc-modes (copy-sequence org-calc-default-modes
))
2720 (numbers nil
) ; was a variable, now fixed default
2722 n form form0 formrpl formrg bw fmt x ev orig c lispp literal
2723 duration duration-output-format
)
2724 ;; Parse the format string. Since we have a lot of modes, this is
2725 ;; a lot of work. However, I think calc still uses most of the time.
2726 (if (string-match ";" formula
)
2727 (let ((tmp (org-split-string formula
";")))
2728 (setq formula
(car tmp
)
2729 fmt
(concat (cdr (assoc "%" org-table-local-parameters
))
2731 (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt
)
2732 (setq c
(string-to-char (match-string 1 fmt
))
2733 n
(string-to-number (match-string 2 fmt
)))
2735 (setq org-tbl-calc-modes
(org-set-calc-mode 'calc-internal-prec n
))
2736 (setq org-tbl-calc-modes
2739 (list (cdr (assoc c
'((?n . float
) (?f . fix
)
2740 (?s . sci
) (?e . eng
))))
2742 (setq fmt
(replace-match "" t t fmt
)))
2743 (if (string-match "[tTU]" fmt
)
2744 (let ((ff (match-string 0 fmt
)))
2745 (setq duration t numbers t
2746 duration-output-format
2747 (cond ((equal ff
"T") nil
)
2748 ((equal ff
"t") org-table-duration-custom-format
)
2749 ((equal ff
"U") 'hh
:mm
))
2750 fmt
(replace-match "" t t fmt
))))
2751 (if (string-match "N" fmt
)
2753 fmt
(replace-match "" t t fmt
)))
2754 (if (string-match "L" fmt
)
2756 fmt
(replace-match "" t t fmt
)))
2757 (if (string-match "E" fmt
)
2759 fmt
(replace-match "" t t fmt
)))
2760 (while (string-match "[DRFS]" fmt
)
2761 (setq org-tbl-calc-modes
(org-set-calc-mode (match-string 0 fmt
)))
2762 (setq fmt
(replace-match "" t t fmt
)))
2763 (unless (string-match "\\S-" fmt
)
2765 (when (and (not suppress-const
) org-table-formula-use-constants
)
2766 (setq formula
(org-table-formula-substitute-names formula
)))
2767 (setq orig
(or (get-text-property 1 :orig-formula formula
) "?"))
2768 (setq formula
(org-table-formula-handle-first/last-rc formula
))
2770 (setq fields
(org-split-string
2772 (buffer-substring-no-properties
2773 (line-beginning-position) (line-end-position)))
2775 ;; replace fields with duration values if relevant
2778 (mapcar (lambda (x) (org-table-time-string-to-seconds x
))
2781 (setq fields
(mapcar
2783 (if (string-match "\\S-" x
)
2784 (number-to-string (string-to-number x
))
2787 (setq ndown
(1- ndown
))
2788 (setq form
(copy-sequence formula
)
2789 lispp
(and (> (length form
) 2) (equal (substring form
0 2) "'(")))
2790 (if (and lispp literal
) (setq lispp
'literal
))
2792 ;; Insert row and column number of formula result field
2793 (while (string-match "[@$]#" form
)
2798 (if (equal (substring form
(match-beginning 0)
2799 (1+ (match-beginning 0)))
2801 (org-table-current-dline)
2802 (org-table-current-column))))
2805 ;; Check for old vertical references
2806 (org-table--error-on-old-row-references form
)
2807 ;; Insert remote references
2808 (setq form
(org-table-remote-reference-indirection form
))
2809 (while (string-match "\\<remote([ \t]*\\([^,)]+\\)[ \t]*,[ \t]*\\([^\n)]+\\))" form
)
2813 (org-table-make-reference
2814 (let ((rmtrng (org-table-get-remote-range
2815 (match-string 1 form
) (match-string 2 form
))))
2818 (mapcar (lambda(x) (org-table-time-string-to-seconds x
)) rmtrng
)
2819 (org-table-time-string-to-seconds rmtrng
))
2821 keep-empty numbers lispp
))
2823 ;; Insert complex ranges
2824 (while (and (string-match org-table-range-regexp form
)
2825 (> (length (match-string 0 form
)) 1))
2828 (org-table-get-range
2829 (match-string 0 form
) org-table-current-begin-pos n0
)))
2832 (org-table-make-reference
2833 ;; possibly handle durations
2836 (mapcar (lambda(x) (org-table-time-string-to-seconds x
)) formrg
)
2837 (org-table-time-string-to-seconds formrg
))
2839 keep-empty numbers lispp
)))
2840 (if (not (save-match-data
2841 (string-match (regexp-quote form
) formrpl
)))
2842 (setq form
(replace-match formrpl t t form
))
2843 (user-error "Spreadsheet error: invalid reference \"%s\"" form
)))
2844 ;; Insert simple ranges, i.e. included in the current row.
2845 (while (string-match
2846 "\\$\\(\\([-+]\\)?[0-9]+\\)\\.\\.\\$\\(\\([-+]\\)?[0-9]+\\)"
2851 (org-table-make-reference
2853 (+ (if (match-end 2) n0
0)
2854 (string-to-number (match-string 1 form
))
2856 (+ (if (match-end 4) n0
0)
2857 (string-to-number (match-string 3 form
))))
2858 keep-empty numbers lispp
))
2861 ;; Insert the references to fields in same row
2862 (while (string-match "\\$\\(\\([-+]\\)?[0-9]+\\)" form
)
2863 (setq n
(+ (string-to-number (match-string 1 form
))
2864 (if (match-end 2) n0
0))
2865 x
(nth (1- (if (= n
0) n0
(max n
1))) fields
)
2866 formrpl
(save-match-data
2867 (org-table-make-reference
2868 x keep-empty numbers lispp
)))
2871 (string-match (regexp-quote formula
) formrpl
)))
2872 (user-error "Invalid field specifier \"%s\""
2873 (match-string 0 form
)))
2874 (setq form
(replace-match formrpl t t form
)))
2877 (setq ev
(condition-case nil
2878 (eval (eval (read form
)))
2880 ev
(if (numberp ev
) (number-to-string ev
) ev
)
2881 ev
(if duration
(org-table-time-seconds-to-string
2882 (string-to-number ev
)
2883 duration-output-format
) ev
))
2885 ;; Use <...> time-stamps so that Calc can handle them.
2887 (replace-regexp-in-string org-ts-regexp-inactive
"<\\1>" form
))
2888 ;; Internationalize local time-stamps by setting locale to
2891 (replace-regexp-in-string
2894 (let ((system-time-locale "C"))
2896 (org-time-stamp-format
2897 (string-match-p "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts
))
2898 (apply #'encode-time
2899 (save-match-data (org-parse-time-string ts
))))))
2902 (setq ev
(if (and duration
(string-match "^[0-9]+:[0-9]+\\(?::[0-9]+\\)?$" form
))
2904 (calc-eval (cons form org-tbl-calc-modes
)
2905 (when (and (not keep-empty
) numbers
) 'num
)))
2906 ev
(if duration
(org-table-time-seconds-to-string
2907 (if (string-match "^[0-9]+:[0-9]+\\(?::[0-9]+\\)?$" ev
)
2908 (string-to-number (org-table-time-string-to-seconds ev
))
2909 (string-to-number ev
))
2910 duration-output-format
)
2913 (when org-table-formula-debug
2914 (with-output-to-temp-buffer "*Substitution History*"
2915 (princ (format "Substitution history of formula
2919 $1-> %s\n" orig formula form0 form
))
2921 (princ (format " %s^\nError: %s"
2922 (make-string (car ev
) ?\-
) (nth 1 ev
)))
2923 (princ (format "Result: %s\nFormat: %s\nFinal: %s"
2925 (if fmt
(format fmt
(string-to-number ev
)) ev
)))))
2926 (setq bw
(get-buffer-window "*Substitution History*"))
2927 (org-fit-window-to-buffer bw
)
2928 (unless (and (called-interactively-p 'any
) (not ndown
))
2929 (unless (let (inhibit-redisplay)
2930 (y-or-n-p "Debugging Formula. Continue to next? "))
2932 (user-error "Abort"))
2935 (when (consp ev
) (setq fmt nil ev
"#ERROR"))
2936 (org-table-justify-field-maybe
2937 (format org-table-formula-field-format
2939 ((not (stringp ev
)) ev
)
2940 (fmt (format fmt
(string-to-number ev
)))
2941 ;; Replace any active time stamp in the result with
2942 ;; an inactive one. Dates in tables are likely
2943 ;; piece of regular data, not meant to appear in the
2945 (t (replace-regexp-in-string org-ts-regexp
"[\\1]" ev
)))))
2946 (if (and down
(> ndown
0) (looking-at ".*\n[ \t]*|[^-]"))
2947 (call-interactively 'org-return
)
2949 (and down
(org-table-maybe-recalculate-line))
2950 (or suppress-align
(and org-table-may-need-update
2951 (org-table-align))))))
2953 (defun org-table-put-field-property (prop value
)
2955 (put-text-property (progn (skip-chars-backward "^|") (point))
2956 (progn (skip-chars-forward "^|") (point))
2959 (defun org-table-get-range (desc &optional tbeg col highlight corners-only
)
2960 "Get a calc vector from a column, according to descriptor DESC.
2962 Optional arguments TBEG and COL can give the beginning of the table and
2963 the current column, to avoid unnecessary parsing.
2965 HIGHLIGHT means just highlight the range.
2967 When CORNERS-ONLY is set, only return the corners of the range as
2968 a list (line1 column1 line2 column2) where line1 and line2 are
2969 line numbers relative to beginning of table, or TBEG, and column1
2970 and column2 are table column numbers."
2971 (let* ((desc (if (string-match-p "\\`\\$[0-9]+\\.\\.\\$[0-9]+\\'" desc
)
2972 (replace-regexp-in-string "\\$" "@0$" desc
)
2974 (col (or col
(org-table-current-column)))
2975 (tbeg (or tbeg
(org-table-begin)))
2976 (thisline (count-lines tbeg
(line-beginning-position))))
2977 (unless (string-match org-table-range-regexp desc
)
2978 (user-error "Invalid table range specifier `%s'" desc
))
2979 (let ((rangep (match-end 3))
2980 (r1 (let ((r (and (match-end 1) (match-string 1 desc
))))
2981 (or (save-match-data
2982 (and (org-string-nw-p r
)
2983 (org-table--descriptor-line r thisline
)))
2985 (r2 (let ((r (and (match-end 4) (match-string 4 desc
))))
2986 (or (save-match-data
2987 (and (org-string-nw-p r
)
2988 (org-table--descriptor-line r thisline
)))
2990 (c1 (let ((c (and (match-end 2) (substring (match-string 2 desc
) 1))))
2991 (if (or (not c
) (= (string-to-number c
) 0)) col
2992 (+ (string-to-number c
)
2993 (if (memq (string-to-char c
) '(?- ?
+)) col
0)))))
2994 (c2 (let ((c (and (match-end 5) (substring (match-string 5 desc
) 1))))
2995 (if (or (not c
) (= (string-to-number c
) 0)) col
2996 (+ (string-to-number c
)
2997 (if (memq (string-to-char c
) '(?- ?
+)) col
0))))))
2999 (if (and (not corners-only
)
3000 (or (not rangep
) (and (= r1 r2
) (= c1 c2
))))
3003 (forward-line (- r1 thisline
))
3004 (while (not (looking-at org-table-dataline-regexp
))
3006 (prog1 (org-trim (org-table-get-field c1
))
3007 (when highlight
(org-table-highlight-rectangle))))
3008 ;; A range, return a vector. First sort the numbers to get
3009 ;; a regular rectangle.
3010 (let ((first-row (min r1 r2
))
3011 (last-row (max r1 r2
))
3012 (first-column (min c1 c2
))
3013 (last-column (max c1 c2
)))
3014 (if corners-only
(list first-row first-column last-row last-column
)
3015 ;; Copy the range values into a list.
3016 (forward-line (- first-row thisline
))
3017 (while (not (looking-at org-table-dataline-regexp
))
3019 (cl-incf first-row
))
3020 (org-table-goto-column first-column
)
3021 (let ((beg (point)))
3022 (forward-line (- last-row first-row
))
3023 (while (not (looking-at org-table-dataline-regexp
))
3025 (org-table-goto-column last-column
)
3026 (let ((end (point)))
3028 (org-table-highlight-rectangle
3029 beg
(progn (skip-chars-forward "^|\n") (point))))
3030 ;; Return string representation of calc vector.
3033 (org-table-copy-region beg end
))))))))))))
3035 (defun org-table--descriptor-line (desc cline
)
3036 "Return relative line number corresponding to descriptor DESC.
3037 The cursor is currently in relative line number CLINE."
3038 (if (string-match "\\`[0-9]+\\'" desc
)
3039 (aref org-table-dlines
(string-to-number desc
))
3040 (when (or (not (string-match
3041 "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?"
3044 (and (not (match-end 3)) (not (match-end 6)))
3045 (and (match-end 3) (match-end 6) (not (match-end 5))))
3046 (user-error "Invalid row descriptor `%s'" desc
))
3047 (let* ((hn (and (match-end 3) (- (match-end 3) (match-beginning 3))))
3048 (hdir (match-string 2 desc
))
3049 (odir (match-string 5 desc
))
3050 (on (and (match-end 6) (string-to-number (match-string 6 desc
))))
3051 (rel (and (match-end 6)
3052 (or (and (match-end 1) (not (match-end 3)))
3054 (when (and hn
(not hdir
))
3057 (when (eq (aref org-table-current-line-types
0) 'hline
) (cl-decf hn
)))
3058 (when (and (not hn
) on
(not odir
)) (user-error "Should never happen"))
3061 (org-table--row-type 'hline hn cline
(equal hdir
"-") nil desc
)))
3064 (org-table--row-type 'dline on cline
(equal odir
"-") rel desc
)))
3067 (defun org-table--row-type (type n i backwards relative desc
)
3068 "Return relative line of Nth row with type TYPE.
3069 Search starts from relative line I. When BACKWARDS in non-nil,
3070 look before I. When RELATIVE is non-nil, the reference is
3071 relative. DESC is the original descriptor that started the
3072 search, as a string."
3073 (let ((l (length org-table-current-line-types
)))
3076 (while (and (cl-incf i
(if backwards -
1 1))
3079 (not (eq (aref org-table-current-line-types i
) type
))
3080 ;; We are going to cross a hline. Check if this is
3081 ;; an authorized move.
3084 ((not (eq (aref org-table-current-line-types i
) 'hline
)))
3085 ((eq org-table-relative-ref-may-cross-hline t
))
3086 ((eq org-table-relative-ref-may-cross-hline
'error
)
3087 (user-error "Row descriptor %s crosses hline" desc
))
3088 (t (cl-decf i
(if backwards -
1 1)) ; Step back.
3089 (throw :exit nil
)))))))
3090 (cond ((or (< i
0) (>= i l
))
3091 (user-error "Row descriptor %s leads outside table" desc
))
3092 ;; The last hline doesn't exist. Instead, point to last row
3094 ((= i
(1- l
)) (1- i
))
3097 (defun org-table--error-on-old-row-references (s)
3098 (when (string-match "&[-+0-9I]" s
)
3099 (user-error "Formula contains old &row reference, please rewrite using @-syntax")))
3101 (defun org-table-make-reference (elements keep-empty numbers lispp
)
3102 "Convert list ELEMENTS to something appropriate to insert into formula.
3103 KEEP-EMPTY indicated to keep empty fields, default is to skip them.
3104 NUMBERS indicates that everything should be converted to numbers.
3105 LISPP non-nil means to return something appropriate for a Lisp
3106 list, `literal' is for the format specifier L."
3107 ;; Calc nan (not a number) is used for the conversion of the empty
3108 ;; field to a reference for several reasons: (i) It is accepted in a
3109 ;; Calc formula (e. g. "" or "()" would result in a Calc error).
3110 ;; (ii) In a single field (not in range) it can be distinguished
3111 ;; from "(nan)" which is the reference made from a single field
3112 ;; containing "nan".
3113 (if (stringp elements
)
3116 (if (eq lispp
'literal
)
3118 (if (and (eq elements
"") (not keep-empty
))
3121 (if numbers
(string-to-number elements
) elements
))))
3122 (if (string-match "\\S-" elements
)
3124 (when numbers
(setq elements
(number-to-string
3125 (string-to-number elements
))))
3126 (concat "(" elements
")"))
3127 (if (or (not keep-empty
) numbers
) "(0)" "nan")))
3132 (mapcar (lambda (x) (if (string-match "\\S-" x
) x nil
))
3134 (setq elements
(or elements
'())) ; if delq returns nil then we need '()
3138 (if (eq lispp
'literal
)
3140 (prin1-to-string (if numbers
(string-to-number x
) x
))))
3142 (concat "[" (mapconcat
3144 (if (string-match "\\S-" x
)
3146 (number-to-string (string-to-number x
))
3148 (if (or (not keep-empty
) numbers
) "0" "nan")))
3152 (defun org-table-message-once-per-second (t1 &rest args
)
3153 "If there has been more than one second since T1, display message.
3154 ARGS are passed as arguments to the `message' function. Returns
3155 current time if a message is printed, otherwise returns T1. If
3156 T1 is nil, always messages."
3157 (let ((curtime (current-time)))
3158 (if (or (not t1
) (< 0 (nth 1 (time-subtract curtime t1
))))
3159 (progn (apply 'message args
)
3164 (defun org-table-recalculate (&optional all noalign
)
3165 "Recalculate the current table line by applying all stored formulas.
3167 With prefix arg ALL, do this for all lines in the table.
3169 When called with a `\\[universal-argument] \\[universal-argument]' prefix, or \
3170 if ALL is the symbol `iterate',
3171 recompute the table until it no longer changes.
3173 If NOALIGN is not nil, do not re-align the table after the computations
3174 are done. This is typically used internally to save time, if it is
3175 known that the table will be realigned a little later anyway."
3177 (unless (memq this-command org-recalc-commands
)
3178 (push this-command org-recalc-commands
))
3179 (unless (org-at-table-p) (user-error "Not at a table"))
3180 (if (or (eq all
'iterate
) (equal all
'(16)))
3183 (let* ((eqlist (sort (org-table-get-stored-formulas)
3184 (lambda (a b
) (string< (car a
) (car b
)))))
3185 (inhibit-redisplay (not debug-on-error
))
3186 (line-re org-table-dataline-regexp
)
3187 (log-first-time (current-time))
3188 (log-last-time log-first-time
)
3190 beg end eqlcol eqlfield
)
3191 ;; Insert constants in all formulas.
3193 (org-table-save-field
3194 ;; Expand equations, then split the equation list between
3195 ;; column formulas and field formulas.
3197 (let* ((rhs (org-table-formula-substitute-names
3198 (org-table-formula-handle-first/last-rc
(cdr eq
))))
3201 (org-table-formula-handle-first/last-rc
3203 ((string-match "\\`@-?I+" old-lhs
)
3204 (user-error "Can't assign to hline relative reference"))
3205 ((string-match "\\`$[<>]" old-lhs
)
3206 (let ((new (org-table-formula-handle-first/last-rc
3208 (when (assoc new eqlist
)
3209 (user-error "\"%s=\" formula tries to overwrite \
3210 existing formula for column %s"
3215 (if (string-match-p "\\`\\$[0-9]+\\'" lhs
)
3216 (push (cons lhs rhs
) eqlcol
)
3217 (push (cons lhs rhs
) eqlfield
))))
3218 (setq eqlcol
(nreverse eqlcol
))
3219 ;; Expand ranges in lhs of formulas
3220 (setq eqlfield
(org-table-expand-lhs-ranges (nreverse eqlfield
)))
3221 ;; Get the correct line range to process.
3224 (setq end
(copy-marker (org-table-end)))
3225 (goto-char (setq beg org-table-current-begin-pos
))
3227 ((re-search-forward org-table-calculate-mark-regexp end t
)
3228 ;; This is a table with marked lines, compute selected
3230 (setq line-re org-table-recalculate-regexp
))
3231 ;; Move forward to the first non-header line.
3232 ((and (re-search-forward org-table-dataline-regexp end t
)
3233 (re-search-forward org-table-hline-regexp end t
)
3234 (re-search-forward org-table-dataline-regexp end t
))
3235 (setq beg
(match-beginning 0)))
3236 ;; Just leave BEG at the start of the table.
3238 (setq beg
(line-beginning-position)
3239 end
(copy-marker (line-beginning-position 2))))
3241 ;; Mark named fields untouchable. Also check if several
3242 ;; field/range formulas try to set the same field.
3243 (remove-text-properties beg end
'(org-untouchable t
))
3244 (let ((current-line (count-lines org-table-current-begin-pos
3245 (line-beginning-position)))
3247 (dolist (eq eqlfield
)
3248 (let* ((name (car eq
))
3249 (location (assoc name org-table-named-field-locations
))
3250 (eq-line (or (nth 1 location
)
3251 (and (string-match "\\`@\\([0-9]+\\)" name
)
3252 (aref org-table-dlines
3254 (match-string 1 name
))))))
3257 ;; Turn field coordinates associated to NAME
3258 ;; into an absolute reference.
3260 (org-table-line-to-dline eq-line
)
3263 (when (member reference seen-fields
)
3264 (user-error "Several field/range formulas try to set %s"
3266 (push reference seen-fields
)
3267 (when (or all
(eq eq-line current-line
))
3268 (org-table-goto-field name
)
3269 (org-table-put-field-property :org-untouchable t
)))))
3270 ;; Evaluate the column formulas, but skip fields covered by
3273 (while (re-search-forward line-re end t
)
3274 (unless (string-match "\\` *[_^!$/] *\\'" (org-table-get-field 1))
3275 ;; Unprotected line, recalculate.
3279 (org-table-message-once-per-second
3281 "Re-applying formulas to full table...(line %d)" cnt
)))
3282 (if (markerp org-last-recalc-line
)
3283 (move-marker org-last-recalc-line
(line-beginning-position))
3284 (setq org-last-recalc-line
3285 (copy-marker (line-beginning-position))))
3286 (dolist (entry eqlcol
)
3287 (goto-char org-last-recalc-line
)
3288 (org-table-goto-column
3289 (string-to-number (substring (car entry
) 1)) nil
'force
)
3290 (unless (get-text-property (point) :org-untouchable
)
3291 (org-table-eval-formula
3292 nil
(cdr entry
) 'noalign
'nocst
'nostore
'noanalysis
)))))
3293 ;; Evaluate the field formulas.
3294 (dolist (eq eqlfield
)
3295 (let ((reference (car eq
))
3298 (org-table-message-once-per-second
3299 (and all log-last-time
)
3300 "Re-applying formula to field: %s" (car eq
)))
3301 (org-table-goto-field
3303 ;; Possibly create a new column, as long as
3304 ;; `org-table-formula-create-columns' allows it.
3305 (let ((column-count (progn (end-of-line)
3306 (1- (org-table-current-column)))))
3308 (when (> column
1000)
3309 (user-error "Formula column target too large"))
3310 (and (> column column-count
)
3311 (or (eq org-table-formula-create-columns t
)
3312 (and (eq org-table-formula-create-columns
'warn
)
3314 (org-display-warning
3315 "Out-of-bounds formula added columns")
3317 (and (eq org-table-formula-create-columns
'prompt
)
3319 "Out-of-bounds formula. Add columns? ")))))))
3320 (org-table-eval-formula nil formula t t t t
))))
3321 ;; Clean up markers and internal text property.
3322 (remove-text-properties (point-min) (point-max) '(org-untouchable t
))
3323 (set-marker end nil
)
3325 (when org-table-may-need-update
(org-table-align))
3327 (org-table-message-once-per-second
3328 log-first-time
"Re-applying formulas to %d lines... done" cnt
)))
3329 (org-table-message-once-per-second
3330 (and all log-first-time
) "Re-applying formulas... done")))))
3333 (defun org-table-iterate (&optional arg
)
3334 "Recalculate the table until it does not change anymore.
3335 The maximum number of iterations is 10, but you can choose a different value
3336 with the prefix ARG."
3338 (let ((imax (if arg
(prefix-numeric-value arg
) 10))
3340 (lasttbl (buffer-substring (org-table-begin) (org-table-end)))
3345 (org-table-recalculate 'all
)
3346 (setq thistbl
(buffer-substring (org-table-begin) (org-table-end)))
3347 (if (not (string= lasttbl thistbl
))
3348 (setq lasttbl thistbl
)
3350 (message "Convergence after %d iterations" i
)
3351 (message "Table was already stable"))
3353 (user-error "No convergence after %d iterations" i
))))
3356 (defun org-table-recalculate-buffer-tables ()
3357 "Recalculate all tables in the current buffer."
3359 (org-with-wide-buffer
3360 (org-table-map-tables
3362 ;; Reason for separate `org-table-align': When repeating
3363 ;; (org-table-recalculate t) `org-table-may-need-update' gets in
3365 (org-table-recalculate t t
)
3370 (defun org-table-iterate-buffer-tables ()
3371 "Iterate all tables in the buffer, to converge inter-table dependencies."
3375 (checksum (md5 (buffer-string)))
3377 (org-with-wide-buffer
3381 (org-table-map-tables (lambda () (org-table-recalculate t t
)) t
)
3382 (if (equal checksum
(setq c1
(md5 (buffer-string))))
3384 (org-table-map-tables #'org-table-align t
)
3385 (message "Convergence after %d iterations" (- imax i
))
3387 (setq checksum c1
)))
3388 (org-table-map-tables #'org-table-align t
)
3389 (user-error "No convergence after %d iterations" imax
)))))
3391 (defun org-table-calc-current-TBLFM (&optional arg
)
3392 "Apply the #+TBLFM in the line at point to the table."
3394 (unless (org-at-TBLFM-p) (user-error "Not at a #+TBLFM line"))
3395 (let ((formula (buffer-substring
3396 (line-beginning-position)
3397 (line-end-position))))
3399 ;; Insert a temporary formula at right after the table
3400 (goto-char (org-table-TBLFM-begin))
3401 (let ((s (point-marker)))
3402 (insert formula
"\n")
3403 (let ((e (point-marker)))
3404 ;; Recalculate the table.
3405 (beginning-of-line 0) ; move to the inserted line
3406 (skip-chars-backward " \r\n\t")
3408 (org-call-with-arg #'org-table-recalculate
(or arg t
))
3409 ;; Delete the formula inserted temporarily.
3412 (set-marker e nil
)))))))
3414 (defun org-table-TBLFM-begin ()
3415 "Find the beginning of the TBLFM lines and return its position.
3416 Return nil when the beginning of TBLFM line was not found."
3418 (when (progn (forward-line 1)
3419 (re-search-backward org-table-TBLFM-begin-regexp nil t
))
3420 (line-beginning-position 2))))
3422 (defun org-table-expand-lhs-ranges (equations)
3423 "Expand list of formulas.
3424 If some of the RHS in the formulas are ranges or a row reference,
3425 expand them to individual field equations for each field. This
3426 function assumes the table is already analyzed (i.e., using
3427 `org-table-analyze')."
3429 (dolist (e equations
(nreverse res
))
3433 ((string-match-p "\\`@-?[-+0-9]+\\$-?[0-9]+\\'" lhs
)
3434 ;; This just refers to one fixed field.
3436 ((string-match-p "\\`[a-zA-Z][_a-zA-Z0-9]*\\'" lhs
)
3437 ;; This just refers to one fixed named field.
3439 ((string-match-p "\\`\\$[0-9]+\\'" lhs
)
3440 ;; Column formulas are treated specially and are not
3443 ((string-match "\\`@[0-9]+\\'" lhs
)
3444 (dotimes (ic org-table-current-ncol
)
3445 (push (cons (propertize (format "%s$%d" lhs
(1+ ic
)) :orig-eqn e
)
3449 (let* ((range (org-table-get-range
3450 lhs org-table-current-begin-pos
1 nil
'corners
))
3451 (r1 (org-table-line-to-dline (nth 0 range
)))
3453 (r2 (org-table-line-to-dline (nth 2 range
) 'above
))
3455 (cl-loop for ir from r1 to r2 do
3456 (cl-loop for ic from c1 to c2 do
3457 (push (cons (propertize
3458 (format "@%d$%d" ir ic
) :orig-eqn e
)
3462 (defun org-table-formula-handle-first/last-rc
(s)
3463 "Replace @<, @>, $<, $> with first/last row/column of the table.
3464 So @< and $< will always be replaced with @1 and $1, respectively.
3465 The advantage of these special markers are that structure editing of
3466 the table will not change them, while @1 and $1 will be modified
3467 when a line/row is swapped out of that privileged position. So for
3468 formulas that use a range of rows or columns, it may often be better
3469 to anchor the formula with \"I\" row markers, or to offset from the
3470 borders of the table using the @< @> $< $> makers."
3471 (let (n nmax len char
(start 0))
3472 (while (string-match "\\([@$]\\)\\(<+\\|>+\\)\\|\\(remote([^)]+)\\)"
3475 (setq start
(match-end 3))
3476 (setq nmax
(if (equal (match-string 1 s
) "@")
3477 (1- (length org-table-dlines
))
3478 org-table-current-ncol
)
3479 len
(- (match-end 2) (match-beginning 2))
3480 char
(string-to-char (match-string 2 s
))
3484 (if (or (< n
1) (> n nmax
))
3485 (user-error "Reference \"%s\" in expression \"%s\" points outside table"
3486 (match-string 0 s
) s
))
3487 (setq start
(match-beginning 0))
3488 (setq s
(replace-match (format "%s%d" (match-string 1 s
) n
) t t s
)))))
3491 (defun org-table-formula-substitute-names (f)
3492 "Replace $const with values in string F."
3494 (pp (/= (string-to-char f
) ?
'))
3495 (duration (string-match-p ";.*[Tt].*\\'" f
))
3496 (new (replace-regexp-in-string ; Check for column names.
3497 org-table-column-name-regexp
3499 (concat "$" (cdr (assoc (match-string 1 m
)
3500 org-table-column-names
))))
3502 ;; Parameters and constants.
3505 "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)\\|\\(\\<remote([^)]*)\\)"
3507 (if (match-end 2) (setq start
(match-end 2))
3509 ;; When a duration is expected, convert value on the fly.
3512 (let ((v (org-table-get-constant (match-string 1 new
))))
3513 (if (and (org-string-nw-p v
) duration
)
3514 (org-table-time-string-to-seconds v
)
3517 (setq new
(replace-match
3518 (concat (and pp
"(") value
(and pp
")")) t t new
))))))
3519 (if org-table-formula-debug
(propertize new
:orig-formula f
) new
)))
3521 (defun org-table-get-constant (const)
3522 "Find the value for a parameter or constant in a formula.
3523 Parameters get priority."
3524 (or (cdr (assoc const org-table-local-parameters
))
3525 (cdr (assoc const org-table-formula-constants-local
))
3526 (cdr (assoc const org-table-formula-constants
))
3527 (and (fboundp 'constants-get
) (constants-get const
))
3528 (and (string= (substring const
0 (min 5 (length const
))) "PROP_")
3529 (org-entry-get nil
(substring const
5) 'inherit
))
3532 (defvar org-table-fedit-map
3533 (let ((map (make-sparse-keymap)))
3534 (org-defkey map
"\C-x\C-s" 'org-table-fedit-finish
)
3535 (org-defkey map
"\C-c\C-s" 'org-table-fedit-finish
)
3536 (org-defkey map
"\C-c\C-c" 'org-table-fedit-finish
)
3537 (org-defkey map
"\C-c'" 'org-table-fedit-finish
)
3538 (org-defkey map
"\C-c\C-q" 'org-table-fedit-abort
)
3539 (org-defkey map
"\C-c?" 'org-table-show-reference
)
3540 (org-defkey map
[(meta shift up
)] 'org-table-fedit-line-up
)
3541 (org-defkey map
[(meta shift down
)] 'org-table-fedit-line-down
)
3542 (org-defkey map
[(shift up
)] 'org-table-fedit-ref-up
)
3543 (org-defkey map
[(shift down
)] 'org-table-fedit-ref-down
)
3544 (org-defkey map
[(shift left
)] 'org-table-fedit-ref-left
)
3545 (org-defkey map
[(shift right
)] 'org-table-fedit-ref-right
)
3546 (org-defkey map
[(meta up
)] 'org-table-fedit-scroll-down
)
3547 (org-defkey map
[(meta down
)] 'org-table-fedit-scroll
)
3548 (org-defkey map
[(meta tab
)] 'lisp-complete-symbol
)
3549 (org-defkey map
"\M-\C-i" 'lisp-complete-symbol
)
3550 (org-defkey map
[(tab)] 'org-table-fedit-lisp-indent
)
3551 (org-defkey map
"\C-i" 'org-table-fedit-lisp-indent
)
3552 (org-defkey map
"\C-c\C-r" 'org-table-fedit-toggle-ref-type
)
3553 (org-defkey map
"\C-c}" 'org-table-fedit-toggle-coordinates
)
3556 (easy-menu-define org-table-fedit-menu org-table-fedit-map
"Org Edit Formulas Menu"
3558 ["Finish and Install" org-table-fedit-finish t
]
3559 ["Finish, Install, and Apply" (org-table-fedit-finish t
) :keys
"C-u C-c C-c"]
3560 ["Abort" org-table-fedit-abort t
]
3562 ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t
]
3563 ["Complete Lisp Symbol" lisp-complete-symbol t
]
3565 "Shift Reference at Point"
3566 ["Up" org-table-fedit-ref-up t
]
3567 ["Down" org-table-fedit-ref-down t
]
3568 ["Left" org-table-fedit-ref-left t
]
3569 ["Right" org-table-fedit-ref-right t
]
3571 "Change Test Row for Column Formulas"
3572 ["Up" org-table-fedit-line-up t
]
3573 ["Down" org-table-fedit-line-down t
]
3575 ["Scroll Table Window" org-table-fedit-scroll t
]
3576 ["Scroll Table Window down" org-table-fedit-scroll-down t
]
3577 ["Show Table Grid" org-table-fedit-toggle-coordinates
3578 :style toggle
:selected
(with-current-buffer (marker-buffer org-pos
)
3579 org-table-overlay-coordinates
)]
3581 ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
3582 :style toggle
:selected org-table-buffer-is-an
]))
3585 (defvar org-table--fedit-source nil
3586 "Position of the TBLFM line being edited.")
3589 (defun org-table-edit-formulas ()
3590 "Edit the formulas of the current table in a separate buffer."
3592 (let ((at-tblfm (org-at-TBLFM-p)))
3593 (unless (or at-tblfm
(org-at-table-p))
3594 (user-error "Not at a table"))
3596 ;; Move point within the table before analyzing it.
3597 (when at-tblfm
(re-search-backward "^[ \t]*|"))
3598 (org-table-analyze))
3599 (let ((key (org-table-current-field-formula 'key
'noerror
))
3600 (eql (sort (org-table-get-stored-formulas t
(and at-tblfm
(point)))
3601 #'org-table-formula-less-p
))
3602 (pos (point-marker))
3603 (source (copy-marker (line-beginning-position)))
3605 (wc (current-window-configuration))
3606 (sel-win (selected-window))
3607 (titles '((column .
"# Column Formulas\n")
3608 (field .
"# Field and Range Formulas\n")
3609 (named .
"# Named Field Formulas\n"))))
3610 (org-switch-to-buffer-other-window "*Edit Formulas*")
3612 ;; Keep global-font-lock-mode from turning on font-lock-mode
3613 (let ((font-lock-global-modes '(not fundamental-mode
)))
3615 (setq-local font-lock-global-modes
(list 'not major-mode
))
3616 (setq-local org-pos pos
)
3617 (setq-local org-table--fedit-source source
)
3618 (setq-local org-window-configuration wc
)
3619 (setq-local org-selected-window sel-win
)
3620 (use-local-map org-table-fedit-map
)
3621 (add-hook 'post-command-hook
#'org-table-fedit-post-command t t
)
3622 (easy-menu-add org-table-fedit-menu
)
3623 (setq startline
(org-current-line))
3626 ((string-match "\\`$\\([0-9]+\\|[<>]+\\)\\'" (car entry
))
3628 ((equal (string-to-char (car entry
)) ?
@) 'field
)
3630 (title (assq type titles
)))
3632 (unless (bobp) (insert "\n"))
3634 (org-add-props (cdr title
) nil
'face font-lock-comment-face
))
3635 (setq titles
(remove title titles
)))
3636 (when (equal key
(car entry
)) (setq startline
(org-current-line)))
3638 (if (memq (string-to-char (car entry
)) '(?
@ ?$
)) "" "$")
3639 (car entry
) " = " (cdr entry
) "\n")))
3640 (remove-text-properties 0 (length s
) '(face nil
) s
)
3642 (when (eq org-table-use-standard-references t
)
3643 (org-table-fedit-toggle-ref-type))
3644 (org-goto-line startline
)
3645 (message "%s" (substitute-command-keys "\\<org-mode-map>\
3646 Edit formulas, finish with `\\[org-ctrl-c-ctrl-c]' or `\\[org-edit-special]'. \
3647 See menu for more commands.")))))
3649 (defun org-table-fedit-post-command ()
3650 (when (not (memq this-command
'(lisp-complete-symbol)))
3651 (let ((win (selected-window)))
3653 (ignore-errors (org-table-show-reference))
3654 (select-window win
)))))
3656 (defun org-table-formula-to-user (s)
3657 "Convert a formula from internal to user representation."
3658 (if (eq org-table-use-standard-references t
)
3659 (org-table-convert-refs-to-an s
)
3662 (defun org-table-formula-from-user (s)
3663 "Convert a formula from user to internal representation."
3664 (if org-table-use-standard-references
3665 (org-table-convert-refs-to-rc s
)
3668 (defun org-table-convert-refs-to-rc (s)
3669 "Convert spreadsheet references from A7 to @7$28.
3670 Works for single references, but also for entire formulas and even the
3673 (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\|\\<remote([^,)]*)\\)" s start
)
3676 ;; format match, just advance
3677 (setq start
(match-end 0)))
3678 ((and (> (match-beginning 0) 0)
3679 (equal ?.
(aref s
(max (1- (match-beginning 0)) 0)))
3680 (not (equal ?.
(aref s
(max (- (match-beginning 0) 2) 0)))))
3681 ;; 3.e5 or something like this.
3682 (setq start
(match-end 0)))
3683 ((or (> (- (match-end 1) (match-beginning 1)) 2)
3684 ;; (member (match-string 1 s)
3685 ;; '("arctan" "exp" "expm" "lnp" "log" "stir"))
3687 ;; function name, just advance
3688 (setq start
(match-end 0)))
3690 (setq start
(match-beginning 0)
3692 (if (equal (match-string 2 s
) "&")
3693 (format "$%d" (org-letters-to-number (match-string 1 s
)))
3695 (string-to-number (match-string 2 s
))
3696 (org-letters-to-number (match-string 1 s
))))
3700 (defun org-table-convert-refs-to-an (s)
3701 "Convert spreadsheet references from to @7$28 to AB7.
3702 Works for single references, but also for entire formulas and even the
3704 (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s
)
3705 (setq s
(replace-match
3707 (org-number-to-letters
3708 (string-to-number (match-string 2 s
)))
3709 (string-to-number (match-string 1 s
)))
3711 (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s
)
3712 (setq s
(replace-match (concat "\\1"
3713 (org-number-to-letters
3714 (string-to-number (match-string 2 s
))) "&")
3718 (defun org-letters-to-number (s)
3719 "Convert a base 26 number represented by letters into an integer.
3720 For example: AB -> 28."
3723 (while (> (length s
) 0)
3724 (setq n
(+ (* n
26) (string-to-char s
) (- ?A
) 1)
3728 (defun org-number-to-letters (n)
3729 "Convert an integer into a base 26 number represented by letters.
3730 For example: 28 -> AB."
3733 (setq s
(concat (char-to-string (+ (mod (1- n
) 26) ?A
)) s
)
3737 (defun org-table-time-string-to-seconds (s)
3738 "Convert a time string into numerical duration in seconds.
3739 S can be a string matching either -?HH:MM:SS or -?HH:MM.
3740 If S is a string representing a number, keep this number."
3743 (let (hour minus min sec res
)
3745 ((and (string-match "\\(-?\\)\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)" s
))
3746 (setq minus
(< 0 (length (match-string 1 s
)))
3747 hour
(string-to-number (match-string 2 s
))
3748 min
(string-to-number (match-string 3 s
))
3749 sec
(string-to-number (match-string 4 s
)))
3751 (setq res
(- (+ (* hour
3600) (* min
60) sec
)))
3752 (setq res
(+ (* hour
3600) (* min
60) sec
))))
3753 ((and (not (string-match org-ts-regexp-both s
))
3754 (string-match "\\(-?\\)\\([0-9]+\\):\\([0-9]+\\)" s
))
3755 (setq minus
(< 0 (length (match-string 1 s
)))
3756 hour
(string-to-number (match-string 2 s
))
3757 min
(string-to-number (match-string 3 s
)))
3759 (setq res
(- (+ (* hour
3600) (* min
60))))
3760 (setq res
(+ (* hour
3600) (* min
60)))))
3761 (t (setq res
(string-to-number s
))))
3762 (number-to-string res
))))
3764 (defun org-table-time-seconds-to-string (secs &optional output-format
)
3765 "Convert a number of seconds to a time string.
3766 If OUTPUT-FORMAT is non-nil, return a number of days, hours,
3767 minutes or seconds."
3768 (let* ((secs0 (abs secs
))
3770 (cond ((eq output-format
'days
)
3771 (format "%.3f" (/ (float secs0
) 86400)))
3772 ((eq output-format
'hours
)
3773 (format "%.2f" (/ (float secs0
) 3600)))
3774 ((eq output-format
'minutes
)
3775 (format "%.1f" (/ (float secs0
) 60)))
3776 ((eq output-format
'seconds
)
3777 (format "%d" secs0
))
3778 ((eq output-format
'hh
:mm
)
3780 (substring (format-seconds
3781 (if org-table-duration-hour-zero-padding
3782 "%.2h:%.2m:%.2s" "%h:%.2m:%.2s")
3786 (if org-table-duration-hour-zero-padding
3787 "%.2h:%.2m:%.2s" "%h:%.2m:%.2s")
3789 (if (< secs
0) (concat "-" res
) res
)))
3791 (defun org-table-fedit-convert-buffer (function)
3792 "Convert all references in this buffer, using FUNCTION."
3793 (let ((origin (copy-marker (line-beginning-position))))
3794 (goto-char (point-min))
3796 (insert (funcall function
(buffer-substring (point) (line-end-position))))
3797 (delete-region (point) (line-end-position))
3800 (set-marker origin nil
)))
3802 (defun org-table-fedit-toggle-ref-type ()
3803 "Convert all references in the buffer from B3 to @3$2 and back."
3805 (setq-local org-table-buffer-is-an
(not org-table-buffer-is-an
))
3806 (org-table-fedit-convert-buffer
3807 (if org-table-buffer-is-an
3808 'org-table-convert-refs-to-an
'org-table-convert-refs-to-rc
))
3809 (message "Reference type switched to %s"
3810 (if org-table-buffer-is-an
"A1 etc" "@row$column")))
3812 (defun org-table-fedit-ref-up ()
3813 "Shift the reference at point one row/hline up."
3815 (org-table-fedit-shift-reference 'up
))
3816 (defun org-table-fedit-ref-down ()
3817 "Shift the reference at point one row/hline down."
3819 (org-table-fedit-shift-reference 'down
))
3820 (defun org-table-fedit-ref-left ()
3821 "Shift the reference at point one field to the left."
3823 (org-table-fedit-shift-reference 'left
))
3824 (defun org-table-fedit-ref-right ()
3825 "Shift the reference at point one field to the right."
3827 (org-table-fedit-shift-reference 'right
))
3829 (defun org-table-fedit-shift-reference (dir)
3831 ((org-in-regexp "\\(\\<[a-zA-Z]\\)&")
3832 (if (memq dir
'(left right
))
3833 (org-rematch-and-replace 1 (eq dir
'left
))
3834 (user-error "Cannot shift reference in this direction")))
3835 ((org-in-regexp "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
3836 ;; A B3-like reference
3837 (if (memq dir
'(up down
))
3838 (org-rematch-and-replace 2 (eq dir
'up
))
3839 (org-rematch-and-replace 1 (eq dir
'left
))))
3841 "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
3842 ;; An internal reference
3843 (if (memq dir
'(up down
))
3844 (org-rematch-and-replace 2 (eq dir
'up
) (match-end 3))
3845 (org-rematch-and-replace 5 (eq dir
'left
))))))
3847 (defun org-rematch-and-replace (n &optional decr hline
)
3848 "Re-match the group N, and replace it with the shifted reference."
3849 (or (match-end n
) (user-error "Cannot shift reference in this direction"))
3850 (goto-char (match-beginning n
))
3851 (and (looking-at (regexp-quote (match-string n
)))
3852 (replace-match (org-table-shift-refpart (match-string 0) decr hline
)
3855 (defun org-table-shift-refpart (ref &optional decr hline
)
3856 "Shift a reference part REF.
3857 If DECR is set, decrease the references row/column, else increase.
3858 If HLINE is set, this may be a hline reference, it certainly is not
3859 a translation reference."
3861 (let* ((sign (string-match "^[-+]" ref
)) n
)
3863 (if sign
(setq sign
(substring ref
0 1) ref
(substring ref
1)))
3865 ((and hline
(string-match "^I+" ref
))
3866 (setq n
(string-to-number (concat sign
(number-to-string (length ref
)))))
3867 (setq n
(+ n
(if decr -
1 1)))
3868 (if (= n
0) (setq n
(+ n
(if decr -
1 1))))
3870 (setq sign
(if (< n
0) "-" "+") n
(abs n
))
3872 (concat sign
(make-string n ?I
)))
3874 ((string-match "^[0-9]+" ref
)
3875 (setq n
(string-to-number (concat sign ref
)))
3876 (setq n
(+ n
(if decr -
1 1)))
3878 (concat (if (< n
0) "-" "+") (number-to-string (abs n
)))
3879 (number-to-string (max 1 n
))))
3881 ((string-match "^[a-zA-Z]+" ref
)
3882 (org-number-to-letters
3883 (max 1 (+ (org-letters-to-number ref
) (if decr -
1 1)))))
3885 (t (user-error "Cannot shift reference"))))))
3887 (defun org-table-fedit-toggle-coordinates ()
3888 "Toggle the display of coordinates in the referenced table."
3890 (let ((pos (marker-position org-pos
)))
3891 (with-current-buffer (marker-buffer org-pos
)
3894 (org-table-toggle-coordinate-overlays)))))
3896 (defun org-table-fedit-finish (&optional arg
)
3897 "Parse the buffer for formula definitions and install them.
3898 With prefix ARG, apply the new formulas to the table."
3900 (org-table-remove-rectangle-highlight)
3901 (when org-table-use-standard-references
3902 (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc
)
3903 (setq org-table-buffer-is-an nil
))
3905 (sel-win org-selected-window
)
3906 (source org-table--fedit-source
)
3908 (goto-char (point-min))
3909 (while (re-search-forward
3910 "^\\(@[-+I<>0-9.$@]+\\|@?[0-9]+\\|\\$\\([a-zA-Z0-9]+\\|[<>]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
3912 (let ((var (match-string 1))
3913 (form (org-trim (match-string 3))))
3914 (unless (equal form
"")
3915 (while (string-match "[ \t]*\n[ \t]*" form
)
3916 (setq form
(replace-match " " t t form
)))
3917 (when (assoc var eql
)
3918 (user-error "Double formulas for %s" var
))
3919 (push (cons var form
) eql
))))
3920 (set-window-configuration org-window-configuration
)
3921 (select-window sel-win
)
3923 (org-table-store-formulas eql
)
3924 (set-marker pos nil
)
3925 (set-marker source nil
)
3926 (kill-buffer "*Edit Formulas*")
3928 (org-table-recalculate 'all
)
3929 (message "New formulas installed - press C-u C-c C-c to apply."))))
3931 (defun org-table-fedit-abort ()
3932 "Abort editing formulas, without installing the changes."
3934 (org-table-remove-rectangle-highlight)
3935 (let ((pos org-pos
) (sel-win org-selected-window
))
3936 (set-window-configuration org-window-configuration
)
3937 (select-window sel-win
)
3939 (move-marker pos nil
)
3940 (message "Formula editing aborted without installing changes")))
3942 (defun org-table-fedit-lisp-indent ()
3943 "Pretty-print and re-indent Lisp expressions in the Formula Editor."
3945 (let ((pos (point)) beg end ind
)
3946 (beginning-of-line 1)
3948 ((looking-at "[ \t]")
3950 (call-interactively 'lisp-indent-line
))
3951 ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos
))
3952 ((not (fboundp 'pp-buffer
))
3953 (user-error "Cannot pretty-print. Command `pp-buffer' is not available"))
3954 ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
3955 (goto-char (- (match-end 0) 2))
3957 (setq ind
(make-string (current-column) ?\
))
3958 (condition-case nil
(forward-sexp 1)
3960 (user-error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
3963 (narrow-to-region beg end
)
3964 (if (eq last-command this-command
)
3966 (goto-char (point-min))
3967 (setq this-command nil
)
3968 (while (re-search-forward "[ \t]*\n[ \t]*" nil t
)
3969 (replace-match " ")))
3971 (untabify (point-min) (point-max))
3972 (goto-char (1+ (point-min)))
3973 (while (re-search-forward "^." nil t
)
3974 (beginning-of-line 1)
3976 (goto-char (point-max))
3977 (org-delete-backward-char 1)))
3981 (defvar org-show-positions nil
)
3983 (defun org-table-show-reference (&optional local
)
3984 "Show the location/value of the $ expression at point.
3985 When LOCAL is non-nil, show references for the table at point."
3987 (org-table-remove-rectangle-highlight)
3988 (when local
(org-table-analyze))
3990 (let ((pos (if local
(point) org-pos
))
3992 (org-inhibit-highlight-removal t
)
3993 (win (selected-window))
3994 (org-show-positions nil
)
3995 var name e what match dest
)
3997 ((org-in-regexp "^@[0-9]+[ \t=]")
3998 (setq match
(concat (substring (match-string 0) 0 -
1)
4000 (substring (match-string 0) 0 -
1)
4003 ((or (org-in-regexp org-table-range-regexp2
)
4004 (org-in-regexp org-table-translate-regexp
)
4005 (org-in-regexp org-table-range-regexp
))
4008 (org-table-convert-refs-to-rc (match-string 0))))
4010 ((org-in-regexp "\\$[a-zA-Z][a-zA-Z0-9]*") 'name
)
4011 ((org-in-regexp "\\$[0-9]+") 'column
)
4013 (t (user-error "No reference at point")))
4014 match
(and what
(or match
(match-string 0))))
4015 (when (and match
(not (equal (match-beginning 0) (point-at-bol))))
4016 (org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
4017 'secondary-selection
))
4018 (add-hook 'before-change-functions
4019 #'org-table-remove-rectangle-highlight
)
4020 (when (eq what
'name
) (setq var
(substring match
1)))
4021 (when (eq what
'range
)
4022 (unless (eq (string-to-char match
) ?
@) (setq match
(concat "@" match
)))
4023 (setq match
(org-table-formula-substitute-names match
)))
4027 (re-search-backward "^\\S-" nil t
)
4029 (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\
4030 \\([0-9]+\\|&\\)\\) *=")
4033 (org-table-convert-refs-to-rc (match-string 1))))
4034 (org-table-add-rectangle-overlay
4035 (match-beginning 1) (match-end 1) face2
))))
4036 (if (and (markerp pos
) (marker-buffer pos
))
4037 (if (get-buffer-window (marker-buffer pos
))
4038 (select-window (get-buffer-window (marker-buffer pos
)))
4039 (org-switch-to-buffer-other-window (get-buffer-window
4040 (marker-buffer pos
)))))
4042 (org-table-force-dataline)
4044 (if local org-table-current-begin-pos
(org-table-begin))))
4046 (setq name
(substring dest
1))
4048 ((string-match-p "\\`\\$[a-zA-Z][a-zA-Z0-9]*" dest
)
4049 (org-table-goto-field dest
))
4050 ((string-match-p "\\`@\\([1-9][0-9]*\\)\\$\\([1-9][0-9]*\\)\\'"
4052 (org-table-goto-field dest
))
4053 (t (org-table-goto-column (string-to-number name
))))
4054 (move-marker pos
(point))
4055 (org-table-highlight-rectangle nil nil face2
))
4057 ((equal dest match
))
4060 (ignore-errors (org-table-get-range match table-start nil
'highlight
)))
4061 ((setq e
(assoc var org-table-named-field-locations
))
4062 (org-table-goto-field var
)
4063 (org-table-highlight-rectangle)
4064 (message "Named field, column %d of line %d" (nth 2 e
) (nth 1 e
)))
4065 ((setq e
(assoc var org-table-column-names
))
4066 (org-table-goto-column (string-to-number (cdr e
)))
4067 (org-table-highlight-rectangle)
4068 (goto-char table-start
)
4069 (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var
"\\) *|")
4072 (goto-char (match-beginning 1))
4073 (org-table-highlight-rectangle)
4074 (message "Named column (column %s)" (cdr e
)))
4075 (user-error "Column name not found")))
4078 (org-table-goto-column (string-to-number (substring match
1)))
4079 (org-table-highlight-rectangle)
4080 (message "Column %s" (substring match
1)))
4081 ((setq e
(assoc var org-table-local-parameters
))
4082 (goto-char table-start
)
4083 (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var
"=\\)") nil t
)
4085 (goto-char (match-beginning 1))
4086 (org-table-highlight-rectangle)
4087 (message "Local parameter."))
4088 (user-error "Parameter not found")))
4089 ((not var
) (user-error "No reference at point"))
4090 ((setq e
(assoc var org-table-formula-constants-local
))
4091 (message "Local Constant: $%s=%s in #+CONSTANTS line."
4093 ((setq e
(assoc var org-table-formula-constants
))
4094 (message "Constant: $%s=%s in `org-table-formula-constants'."
4096 ((setq e
(and (fboundp 'constants-get
) (constants-get var
)))
4097 (message "Constant: $%s=%s, from `constants.el'%s."
4098 var e
(format " (%s units)" constants-unit-system
)))
4099 (t (user-error "Undefined name $%s" var
)))
4101 (when (and org-show-positions
4102 (not (memq this-command
'(org-table-fedit-scroll
4103 org-table-fedit-scroll-down
))))
4104 (push pos org-show-positions
)
4105 (push table-start org-show-positions
)
4106 (let ((min (apply 'min org-show-positions
))
4107 (max (apply 'max org-show-positions
)))
4108 (set-window-start (selected-window) min
)
4110 (or (pos-visible-in-window-p max
)
4111 (set-window-start (selected-window) max
)))))
4112 (select-window win
))))
4114 (defun org-table-force-dataline ()
4115 "Make sure the cursor is in a dataline in a table."
4116 (unless (save-excursion
4117 (beginning-of-line 1)
4118 (looking-at org-table-dataline-regexp
))
4119 (let* ((re org-table-dataline-regexp
)
4120 (p1 (save-excursion (re-search-forward re nil
'move
)))
4121 (p2 (save-excursion (re-search-backward re nil
'move
))))
4123 (goto-char (if (< (abs (- p1
(point))) (abs (- p2
(point))))
4125 ((or p1 p2
) (goto-char (or p1 p2
)))
4126 (t (user-error "No table dataline around here"))))))
4128 (defun org-table-fedit-line-up ()
4129 "Move cursor one line up in the window showing the table."
4131 (org-table-fedit-move 'previous-line
))
4133 (defun org-table-fedit-line-down ()
4134 "Move cursor one line down in the window showing the table."
4136 (org-table-fedit-move 'next-line
))
4138 (defun org-table-fedit-move (command)
4139 "Move the cursor in the window showing the table.
4140 Use COMMAND to do the motion, repeat if necessary to end up in a data line."
4141 (let ((org-table-allow-automatic-line-recalculation nil
)
4142 (pos org-pos
) (win (selected-window)) p
)
4143 (select-window (get-buffer-window (marker-buffer org-pos
)))
4145 (call-interactively command
)
4146 (while (and (org-at-table-p)
4147 (org-at-table-hline-p))
4148 (call-interactively command
))
4149 (or (org-at-table-p) (goto-char p
))
4150 (move-marker pos
(point))
4151 (select-window win
)))
4153 (defun org-table-fedit-scroll (N)
4155 (let ((other-window-scroll-buffer (marker-buffer org-pos
)))
4156 (scroll-other-window N
)))
4158 (defun org-table-fedit-scroll-down (N)
4160 (org-table-fedit-scroll (- N
)))
4162 (defvar org-table-rectangle-overlays nil
)
4164 (defun org-table-add-rectangle-overlay (beg end
&optional face
)
4165 "Add a new overlay."
4166 (let ((ov (make-overlay beg end
)))
4167 (overlay-put ov
'face
(or face
'secondary-selection
))
4168 (push ov org-table-rectangle-overlays
)))
4170 (defun org-table-highlight-rectangle (&optional beg end face
)
4171 "Highlight rectangular region in a table.
4172 When buffer positions BEG and END are provided, use them to
4173 delimit the region to highlight. Otherwise, refer to point. Use
4174 FACE, when non-nil, for the highlight."
4175 (let* ((beg (or beg
(point)))
4176 (end (or end
(point)))
4182 (cons (line-beginning-position) (org-table-current-column))))
4186 (cons (line-beginning-position) (org-table-current-column)))))
4187 (when (boundp 'org-show-positions
)
4188 (setq org-show-positions
(cons b
(cons e org-show-positions
))))
4189 (goto-char (car start-coordinates
))
4190 (let ((column-start (min (cdr start-coordinates
) (cdr end-coordinates
)))
4191 (column-end (max (cdr start-coordinates
) (cdr end-coordinates
)))
4192 (last-row (car end-coordinates
)))
4193 (while (<= (point) last-row
)
4194 (when (looking-at org-table-dataline-regexp
)
4195 (org-table-goto-column column-start
)
4196 (skip-chars-backward "^|\n")
4198 (org-table-goto-column column-end
)
4199 (skip-chars-forward "^|\n")
4200 (org-table-add-rectangle-overlay p
(point) face
)))
4202 (goto-char (car start-coordinates
)))
4203 (add-hook 'before-change-functions
#'org-table-remove-rectangle-highlight
))
4205 (defun org-table-remove-rectangle-highlight (&rest _ignore
)
4206 "Remove the rectangle overlays."
4207 (unless org-inhibit-highlight-removal
4208 (remove-hook 'before-change-functions
'org-table-remove-rectangle-highlight
)
4209 (mapc 'delete-overlay org-table-rectangle-overlays
)
4210 (setq org-table-rectangle-overlays nil
)))
4212 (defvar-local org-table-coordinate-overlays nil
4213 "Collects the coordinate grid overlays, so that they can be removed.")
4215 (defun org-table-overlay-coordinates ()
4216 "Add overlays to the table at point, to show row/column coordinates."
4218 (mapc 'delete-overlay org-table-coordinate-overlays
)
4219 (setq org-table-coordinate-overlays nil
)
4221 (let ((id 0) (ih 0) hline eol s1 s2 str ic ov beg
)
4222 (goto-char (org-table-begin))
4223 (while (org-at-table-p)
4224 (setq eol
(point-at-eol))
4225 (setq ov
(make-overlay (point-at-bol) (1+ (point-at-bol))))
4226 (push ov org-table-coordinate-overlays
)
4227 (setq hline
(looking-at org-table-hline-regexp
))
4228 (setq str
(if hline
(format "I*%-2d" (setq ih
(1+ ih
)))
4229 (format "%4d" (setq id
(1+ id
)))))
4230 (org-overlay-before-string ov str
'org-special-keyword
'evaporate
)
4233 (while (re-search-forward "[+|]\\(-+\\)" eol t
)
4234 (setq beg
(1+ (match-beginning 0))
4236 s1
(concat "$" (int-to-string ic
))
4237 s2
(org-number-to-letters ic
)
4238 str
(if (eq org-table-use-standard-references t
) s2 s1
))
4239 (setq ov
(make-overlay beg
(+ beg
(length str
))))
4240 (push ov org-table-coordinate-overlays
)
4241 (org-overlay-display ov str
'org-special-keyword
'evaporate
)))
4242 (beginning-of-line 2)))))
4245 (defun org-table-toggle-coordinate-overlays ()
4246 "Toggle the display of Row/Column numbers in tables."
4248 (setq org-table-overlay-coordinates
(not org-table-overlay-coordinates
))
4249 (message "Tables Row/Column numbers display turned %s"
4250 (if org-table-overlay-coordinates
"on" "off"))
4251 (if (and (org-at-table-p) org-table-overlay-coordinates
)
4253 (unless org-table-overlay-coordinates
4254 (mapc 'delete-overlay org-table-coordinate-overlays
)
4255 (setq org-table-coordinate-overlays nil
)))
4258 (defun org-table-toggle-formula-debugger ()
4259 "Toggle the formula debugger in tables."
4261 (setq org-table-formula-debug
(not org-table-formula-debug
))
4262 (message "Formula debugging has been turned %s"
4263 (if org-table-formula-debug
"on" "off")))
4265 ;;; The orgtbl minor mode
4267 ;; Define a minor mode which can be used in other modes in order to
4268 ;; integrate the Org table editor.
4270 ;; This is really a hack, because the Org table editor uses several
4271 ;; keys which normally belong to the major mode, for example the TAB
4272 ;; and RET keys. Here is how it works: The minor mode defines all the
4273 ;; keys necessary to operate the table editor, but wraps the commands
4274 ;; into a function which tests if the cursor is currently inside
4275 ;; a table. If that is the case, the table editor command is
4276 ;; executed. However, when any of those keys is used outside a table,
4277 ;; the function uses `key-binding' to look up if the key has an
4278 ;; associated command in another currently active keymap (minor modes,
4279 ;; major mode, global), and executes that command. There might be
4280 ;; problems if any of the keys used by the table editor is otherwise
4281 ;; used as a prefix key.
4283 ;; Another challenge is that the key binding for TAB can be tab or \C-i,
4284 ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
4285 ;; addresses this by checking explicitly for both bindings.
4287 ;; The optimized version (see variable `orgtbl-optimized') takes over
4288 ;; all keys which are bound to `self-insert-command' in the *global map*.
4289 ;; Some modes bind other commands to simple characters, for example
4290 ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
4291 ;; active, this binding is ignored inside tables and replaced with a
4292 ;; modified self-insert.
4295 (defvar orgtbl-mode-map
(make-keymap)
4296 "Keymap for `orgtbl-mode'.")
4298 (defvar org-old-auto-fill-inhibit-regexp nil
4299 "Local variable used by `orgtbl-mode'.")
4301 (defconst orgtbl-line-start-regexp
4302 "[ \t]*\\(|\\|#\\+\\(tblfm\\|orgtbl\\|tblname\\):\\)"
4303 "Matches a line belonging to an orgtbl.")
4305 (defconst orgtbl-extra-font-lock-keywords
4306 (list (list (concat "^" orgtbl-line-start-regexp
".*")
4307 0 (quote 'org-table
) 'prepend
))
4308 "Extra `font-lock-keywords' to be added when `orgtbl-mode' is active.")
4310 ;; Install it as a minor mode.
4311 (put 'orgtbl-mode
:included t
)
4312 (put 'orgtbl-mode
:menu-tag
"Org Table Mode")
4315 (define-minor-mode orgtbl-mode
4316 "The `org-mode' table editor as a minor mode for use in other modes."
4317 :lighter
" OrgTbl" :keymap orgtbl-mode-map
4318 (org-load-modules-maybe)
4320 ((derived-mode-p 'org-mode
)
4321 ;; Exit without error, in case some hook functions calls this
4322 ;; by accident in org-mode.
4323 (message "Orgtbl-mode is not useful in org-mode, command ignored"))
4325 (and (orgtbl-setup) (defun orgtbl-setup () nil
)) ;; FIXME: Yuck!?!
4326 ;; Make sure we are first in minor-mode-map-alist
4327 (let ((c (assq 'orgtbl-mode minor-mode-map-alist
)))
4328 ;; FIXME: maybe it should use emulation-mode-map-alists?
4329 (and c
(setq minor-mode-map-alist
4330 (cons c
(delq c minor-mode-map-alist
)))))
4331 (setq-local org-table-may-need-update t
)
4332 (add-hook 'before-change-functions
'org-before-change-function
4334 (setq-local org-old-auto-fill-inhibit-regexp
4335 auto-fill-inhibit-regexp
)
4336 (setq-local auto-fill-inhibit-regexp
4337 (if auto-fill-inhibit-regexp
4338 (concat orgtbl-line-start-regexp
"\\|"
4339 auto-fill-inhibit-regexp
)
4340 orgtbl-line-start-regexp
))
4341 (add-to-invisibility-spec '(org-cwidth))
4342 (when (fboundp 'font-lock-add-keywords
)
4343 (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords
)
4344 (org-restart-font-lock))
4345 (easy-menu-add orgtbl-mode-menu
))
4347 (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp
)
4348 (org-table-cleanup-narrow-column-properties)
4349 (org-remove-from-invisibility-spec '(org-cwidth))
4350 (remove-hook 'before-change-functions
'org-before-change-function t
)
4351 (when (fboundp 'font-lock-remove-keywords
)
4352 (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords
)
4353 (org-restart-font-lock))
4354 (easy-menu-remove orgtbl-mode-menu
)
4355 (force-mode-line-update 'all
))))
4357 (defun org-table-cleanup-narrow-column-properties ()
4358 "Remove all properties related to narrow-column invisibility."
4359 (let ((s (point-min)))
4360 (while (setq s
(text-property-any s
(point-max)
4361 'display org-narrow-column-arrow
))
4362 (remove-text-properties s
(1+ s
) '(display t
)))
4363 (setq s
(point-min))
4364 (while (setq s
(text-property-any s
(point-max) 'org-cwidth
1))
4365 (remove-text-properties s
(1+ s
) '(org-cwidth t
)))
4366 (setq s
(point-min))
4367 (while (setq s
(text-property-any s
(point-max) 'invisible
'org-cwidth
))
4368 (remove-text-properties s
(1+ s
) '(invisible t
)))))
4370 (defun orgtbl-make-binding (fun n
&rest keys
)
4371 "Create a function for binding in the table minor mode.
4372 FUN is the command to call inside a table. N is used to create a unique
4373 command name. KEYS are keys that should be checked in for a command
4374 to execute outside of tables."
4377 (intern (concat "orgtbl-hijacker-command-" (int-to-string n
)))
4379 (concat "In tables, run `" (symbol-name fun
) "'.\n"
4380 "Outside of tables, run the binding of `"
4381 (mapconcat #'key-description keys
"' or `")
4386 (list 'call-interactively
(list 'quote fun
))
4387 (list 'let
'(orgtbl-mode)
4388 (list 'call-interactively
4391 (list 'key-binding k
))
4393 '('orgtbl-error
))))))))
4395 (defun orgtbl-error ()
4396 "Error when there is no default binding for a table key."
4398 (user-error "This key has no function outside tables"))
4400 (defun orgtbl-setup ()
4401 "Setup orgtbl keymaps."
4404 '(([(meta shift left
)] org-table-delete-column
)
4405 ([(meta left
)] org-table-move-column-left
)
4406 ([(meta right
)] org-table-move-column-right
)
4407 ([(meta shift right
)] org-table-insert-column
)
4408 ([(meta shift up
)] org-table-kill-row
)
4409 ([(meta shift down
)] org-table-insert-row
)
4410 ([(meta up
)] org-table-move-row-up
)
4411 ([(meta down
)] org-table-move-row-down
)
4412 ("\C-c\C-w" org-table-cut-region
)
4413 ("\C-c\M-w" org-table-copy-region
)
4414 ("\C-c\C-y" org-table-paste-rectangle
)
4415 ("\C-c\C-w" org-table-wrap-region
)
4416 ("\C-c-" org-table-insert-hline
)
4417 ("\C-c}" org-table-toggle-coordinate-overlays
)
4418 ("\C-c{" org-table-toggle-formula-debugger
)
4419 ("\C-m" org-table-next-row
)
4420 ([(shift return
)] org-table-copy-down
)
4421 ("\C-c?" org-table-field-info
)
4422 ("\C-c " org-table-blank-field
)
4423 ("\C-c+" org-table-sum
)
4424 ("\C-c=" org-table-eval-formula
)
4425 ("\C-c'" org-table-edit-formulas
)
4426 ("\C-c`" org-table-edit-field
)
4427 ("\C-c*" org-table-recalculate
)
4428 ("\C-c^" org-table-sort-lines
)
4429 ("\M-a" org-table-beginning-of-field
)
4430 ("\M-e" org-table-end-of-field
)
4431 ([(control ?
#)] org-table-rotate-recalc-marks
)))
4433 (while (setq elt
(pop bindings
))
4434 (setq nfunc
(1+ nfunc
))
4435 (setq key
(org-key (car elt
))
4437 cmd
(orgtbl-make-binding fun nfunc key
))
4438 (org-defkey orgtbl-mode-map key cmd
))
4440 ;; Special treatment needed for TAB, RET and DEL
4441 (org-defkey orgtbl-mode-map
[(return)]
4442 (orgtbl-make-binding 'orgtbl-ret
100 [(return)] "\C-m"))
4443 (org-defkey orgtbl-mode-map
"\C-m"
4444 (orgtbl-make-binding 'orgtbl-ret
101 "\C-m" [(return)]))
4445 (org-defkey orgtbl-mode-map
[(tab)]
4446 (orgtbl-make-binding 'orgtbl-tab
102 [(tab)] "\C-i"))
4447 (org-defkey orgtbl-mode-map
"\C-i"
4448 (orgtbl-make-binding 'orgtbl-tab
103 "\C-i" [(tab)]))
4449 (org-defkey orgtbl-mode-map
[(shift tab
)]
4450 (orgtbl-make-binding 'org-table-previous-field
104
4451 [(shift tab
)] [(tab)] "\C-i"))
4452 (org-defkey orgtbl-mode-map
[backspace]
4453 (orgtbl-make-binding 'org-delete-backward-char 109
4454 [backspace] (kbd "DEL")))
4456 (org-defkey orgtbl-mode-map
[S-iso-lefttab
]
4457 (orgtbl-make-binding 'org-table-previous-field
107
4458 [S-iso-lefttab
] [backtab] [(shift tab)]
4461 (org-defkey orgtbl-mode-map [backtab]
4462 (orgtbl-make-binding 'org-table-previous-field
108
4463 [backtab] [S-iso-lefttab] [(shift tab)]
4466 (org-defkey orgtbl-mode-map "\M-\C-m"
4467 (orgtbl-make-binding 'org-table-wrap-region 105
4468 "\M-\C-m" [(meta return)]))
4469 (org-defkey orgtbl-mode-map [(meta return)]
4470 (orgtbl-make-binding 'org-table-wrap-region 106
4471 [(meta return)] "\M-\C-m"))
4473 (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
4474 (org-defkey orgtbl-mode-map "\C-c|" 'orgtbl-create-or-convert-from-region)
4476 (when orgtbl-optimized
4477 ;; If the user wants maximum table support, we need to hijack
4478 ;; some standard editing functions
4479 (org-remap orgtbl-mode-map
4480 'self-insert-command 'orgtbl-self-insert-command
4481 'delete-char 'org-delete-char
4482 'delete-backward-char 'org-delete-backward-char)
4483 (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
4484 (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
4486 ["Create or convert" org-table-create-or-convert-from-region
4487 :active (not (org-at-table-p)) :keys "C-c |" ]
4489 ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
4490 ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
4491 ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
4492 ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
4494 ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
4495 ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
4496 ["Copy Field from Above"
4497 org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
4500 ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
4501 ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
4502 ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
4503 ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
4505 ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
4506 ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
4507 ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
4508 ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
4509 ["Sort lines in region" org-table-sort-lines :active (org-at-table-p) :keys "C-c ^"]
4511 ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
4513 ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
4514 ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
4515 ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
4516 ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
4519 ["Insert table template" orgtbl-insert-radio-table
4520 (cl-assoc-if #'derived-mode-p orgtbl-radio-table-templates)]
4521 ["Comment/uncomment table" orgtbl-toggle-comment t])
4523 ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
4524 ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
4525 ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
4526 ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
4527 ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
4528 ["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
4529 ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
4530 ["Sum Column/Rectangle" org-table-sum
4531 :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
4532 ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
4534 org-table-toggle-formula-debugger :active (org-at-table-p)
4536 :style toggle :selected org-table-formula-debug]
4537 ["Show Col/Row Numbers"
4538 org-table-toggle-coordinate-overlays :active (org-at-table-p)
4540 :style toggle :selected org-table-overlay-coordinates]
4543 ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
4544 ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
4547 (defun orgtbl-ctrl-c-ctrl-c (arg)
4548 "If the cursor is inside a table, realign the table.
4549 If it is a table to be sent away to a receiver, do it.
4550 With prefix arg, also recompute table."
4552 (let ((case-fold-search t) (pos (point)) action)
4554 (beginning-of-line 1)
4556 ((looking-at "[ \t]*#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
4557 ((looking-at "[ \t]*|") pos)
4558 ((looking-at "[ \t]*#\\+tblfm:") 'recalc))))
4562 (org-table-maybe-eval-formula)
4564 (call-interactively 'org-table-recalculate)
4565 (org-table-maybe-recalculate-line))
4566 (call-interactively 'org-table-align)
4567 (when (orgtbl-send-table 'maybe)
4568 (run-hooks 'orgtbl-after-send-table-hook)))
4569 ((eq action 'recalc)
4571 (beginning-of-line 1)
4572 (skip-chars-backward " \r\n\t")
4573 (if (org-at-table-p)
4574 (org-call-with-arg 'org-table-recalculate t))))
4575 (t (let (orgtbl-mode)
4576 (call-interactively (key-binding "\C-c\C-c")))))))
4578 (defun orgtbl-create-or-convert-from-region (_arg)
4579 "Create table or convert region to table, if no conflicting binding.
4580 This installs the table binding `C-c |', but only if there is no
4581 conflicting binding to this key outside orgtbl-mode."
4583 (let* (orgtbl-mode (cmd (key-binding "\C-c|")))
4585 (call-interactively cmd)
4586 (call-interactively 'org-table-create-or-convert-from-region))))
4588 (defun orgtbl-tab (arg)
4589 "Justification and field motion for `orgtbl-mode'."
4591 (if arg (org-table-edit-field t)
4592 (org-table-justify-field-maybe)
4593 (org-table-next-field)))
4595 (defun orgtbl-ret ()
4596 "Justification and field motion for `orgtbl-mode'."
4600 (org-table-justify-field-maybe)
4601 (org-table-next-row)))
4603 (defun orgtbl-self-insert-command (N)
4604 "Like `self-insert-command', use overwrite-mode for whitespace in tables.
4605 If the cursor is in a table looking at whitespace, the whitespace is
4606 overwritten, and the table is not marked as requiring realignment."
4608 (if (and (org-at-table-p)
4610 (and org-table-auto-blank-field
4611 (member last-command
4612 '(orgtbl-hijacker-command-100
4613 orgtbl-hijacker-command-101
4614 orgtbl-hijacker-command-102
4615 orgtbl-hijacker-command-103
4616 orgtbl-hijacker-command-104
4617 orgtbl-hijacker-command-105
4619 (org-table-blank-field))
4622 (looking-at "[^|\n]* \\( \\)|"))
4623 (let (org-table-may-need-update)
4624 (delete-region (match-beginning 1) (match-end 1))
4625 (self-insert-command N))
4626 (setq org-table-may-need-update t)
4629 (cmd (or (key-binding
4630 (or (and (listp function-key-map)
4631 (setq a (assoc last-input-event function-key-map))
4633 (vector last-input-event)))
4634 'self-insert-command)))
4635 (call-interactively cmd)
4636 (if (and org-self-insert-cluster-for-undo
4637 (eq cmd 'self-insert-command))
4638 (if (not (eq last-command 'orgtbl-self-insert-command))
4639 (setq org-self-insert-command-undo-counter 1)
4640 (if (>= org-self-insert-command-undo-counter 20)
4641 (setq org-self-insert-command-undo-counter 1)
4642 (and (> org-self-insert-command-undo-counter 0)
4644 (not (cadr buffer-undo-list)) ; remove nil entry
4645 (setcdr buffer-undo-list (cddr buffer-undo-list)))
4646 (setq org-self-insert-command-undo-counter
4647 (1+ org-self-insert-command-undo-counter))))))))
4650 (defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
4651 "Regular expression matching exponentials as produced by calc.")
4653 (defun orgtbl-gather-send-defs ()
4654 "Gather a plist of :name, :transform, :params for each destination before
4657 (goto-char (org-table-begin))
4659 (beginning-of-line 0)
4660 (while (looking-at "[ \t]*#\\+ORGTBL[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
4661 (let ((name (org-no-properties (match-string 1)))
4662 (transform (intern (match-string 2)))
4663 (params (if (match-end 3)
4664 (read (concat "(" (match-string 3) ")")))))
4665 (push (list :name name :transform transform :params params)
4667 (beginning-of-line 0)))
4670 (defun orgtbl-send-replace-tbl (name text)
4671 "Find and replace table NAME with TEXT."
4673 (goto-char (point-min))
4674 (let* ((location-flag nil)
4675 (name (regexp-quote name))
4676 (begin-re (format "BEGIN +RECEIVE +ORGTBL +%s\\([ \t]\\|$\\)" name))
4677 (end-re (format "END +RECEIVE +ORGTBL +%s\\([ \t]\\|$\\)" name)))
4678 (while (re-search-forward begin-re nil t)
4679 (unless location-flag (setq location-flag t))
4680 (let ((beg (line-beginning-position 2)))
4681 (unless (re-search-forward end-re nil t)
4682 (user-error "Cannot find end of receiver location at %d" beg))
4684 (delete-region beg (point))
4685 (insert text "\n")))
4686 (unless location-flag
4687 (user-error "No valid receiver location found in the buffer")))))
4690 (defun org-table-to-lisp (&optional txt)
4691 "Convert the table at point to a Lisp structure.
4692 The structure will be a list. Each item is either the symbol `hline'
4693 for a horizontal separator line, or a list of field values as strings.
4694 The table is taken from the parameter TXT, or from the buffer at point."
4695 (unless (or txt (org-at-table-p)) (user-error "No table at point"))
4697 (buffer-substring-no-properties (org-table-begin)
4700 (if (string-match org-table-hline-regexp x) 'hline
4701 (org-split-string (org-trim x) "\\s-*|\\s-*")))
4702 (org-split-string txt "[ \t]*\n[ \t]*"))))
4704 (defun orgtbl-send-table (&optional maybe)
4705 "Send a transformed version of table at point to the receiver position.
4706 With argument MAYBE, fail quietly if no transformation is defined
4710 (unless (org-at-table-p) (user-error "Not at a table"))
4711 ;; when non-interactive, we assume align has just happened.
4712 (when (called-interactively-p 'any) (org-table-align))
4713 (let ((dests (orgtbl-gather-send-defs))
4714 (table (org-table-to-lisp
4715 (buffer-substring-no-properties (org-table-begin)
4719 (if maybe (throw 'exit nil)
4720 (user-error "Don't know how to transform this table")))
4721 (dolist (dest dests)
4722 (let ((name (plist-get dest :name))
4723 (transform (plist-get dest :transform))
4724 (params (plist-get dest :params)))
4725 (unless (fboundp transform)
4726 (user-error "No such transformation function %s" transform))
4727 (orgtbl-send-replace-tbl name (funcall transform table params)))
4729 (message "Table converted and installed at %d receiver location%s"
4730 ntbl (if (> ntbl 1) "s" ""))
4731 (and (> ntbl 0) ntbl))))
4733 (defun org-remove-by-index (list indices &optional i0)
4734 "Remove the elements in LIST with indices in INDICES.
4735 First element has index 0, or I0 if given."
4738 (if (integerp indices) (setq indices (list indices)))
4739 (setq i0 (1- (or i0 0)))
4740 (delq :rm (mapcar (lambda (x)
4742 (if (memq i0 indices) :rm x))
4745 (defun orgtbl-toggle-comment ()
4746 "Comment or uncomment the orgtbl at point."
4748 (let* ((case-fold-search t)
4749 (re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
4750 (re2 (concat "^" orgtbl-line-start-regexp))
4751 (commented (save-excursion (beginning-of-line 1)
4752 (cond ((looking-at re1) t)
4753 ((looking-at re2) nil)
4754 (t (user-error "Not at an org table")))))
4755 (re (if commented re1 re2))
4758 (beginning-of-line 1)
4759 (while (looking-at re) (beginning-of-line 0))
4760 (beginning-of-line 2)
4762 (while (looking-at re) (beginning-of-line 2))
4764 (comment-region beg end (if commented '(4) nil))))
4766 (defun orgtbl-insert-radio-table ()
4767 "Insert a radio table template appropriate for this major mode."
4769 (let* ((e (cl-assoc-if #'derived-mode-p orgtbl-radio-table-templates))
4772 (unless e (user-error "No radio table setup defined for %s" major-mode))
4773 (setq name (read-string "Table name: "))
4774 (while (string-match "%n" txt)
4775 (setq txt (replace-match name t t txt)))
4776 (or (bolp) (insert "\n"))
4782 (defun orgtbl-to-generic (table params)
4783 "Convert the orgtbl-mode TABLE to some other format.
4785 This generic routine can be used for many standard cases.
4787 TABLE is a list, each entry either the symbol `hline' for
4788 a horizontal separator line, or a list of fields for that
4789 line. PARAMS is a property list of parameters that can
4790 influence the conversion.
4792 Valid parameters are:
4796 Export back-end used as a basis to transcode elements of the
4797 table, when no specific parameter applies to it. It is also
4798 used to translate cells contents. You can prevent this by
4799 setting :raw property to a non-nil value.
4803 When non-nil, only convert rows, not the table itself. This is
4804 equivalent to setting to the empty string both :tstart
4805 and :tend, which see.
4809 When set to an integer N, skip the first N lines of the table.
4810 Horizontal separation lines do count for this parameter!
4814 List of columns that should be skipped. If the table has
4815 a column with calculation marks, that column is automatically
4816 discarded beforehand.
4820 String to be inserted on horizontal separation lines. May be
4821 nil to ignore these lines altogether.
4825 Separator between two fields, as a string.
4827 Each in the following group may be either a string or a function
4828 of no arguments returning a string:
4832 Strings to start and end the table. Ignored when :splice is t.
4836 Strings to start and end a new table line.
4840 Strings to start and end the last table line. Default,
4841 respectively, to :lstart and :lend.
4843 Each in the following group may be a string or a function of one
4844 argument (either the cells in the current row, as a list of
4845 strings, or the current cell) returning a string:
4849 Format string for an entire row, with enough %s to capture all
4850 fields. When non-nil, :lstart, :lend, and :sep are ignored.
4854 Format for the entire last line, defaults to :lfmt.
4858 A format to be used to wrap the field, should contain %s for
4859 the original field value. For example, to wrap everything in
4860 dollars, you could use :fmt \"$%s$\". This may also be
4861 a property list with column numbers and format strings, or
4864 (:fmt (2 \"$%s$\" 4 (lambda (c) (format \"$%s$\" c))))
4866 :hlstart :hllstart :hlend :hllend :hsep :hlfmt :hllfmt :hfmt
4868 Same as above, specific for the header lines in the table.
4869 All lines before the first hline are treated as header. If
4870 any of these is not present, the data line value is used.
4872 This may be either a string or a function of two arguments:
4876 Use this format to print numbers with exponential. The format
4877 should have %s twice for inserting mantissa and exponent, for
4878 example \"%s\\\\times10^{%s}\". This may also be a property
4879 list with column numbers and format strings or functions.
4880 :fmt will still be applied after :efmt."
4881 ;; Make sure `org-export-create-backend' is available.
4883 (let* ((backend (plist-get params :backend))
4885 ;; Build a custom back-end according to PARAMS. Before
4886 ;; defining a translator, check if there is anything to do.
4887 ;; When there isn't, let BACKEND handle the element.
4888 (org-export-create-backend
4889 :parent (or backend 'org)
4891 `((table . ,(org-table--to-generic-table params))
4892 (table-row . ,(org-table--to-generic-row params))
4893 (table-cell . ,(org-table--to-generic-cell params))
4894 ;; Macros are not going to be expanded. However, no
4895 ;; regular back-end has a transcoder for them. We
4896 ;; provide one so they are not ignored, but displayed
4898 (macro . (lambda (m c i) (org-element-macro-interpreter m nil))))))
4900 ;; Store TABLE as Org syntax in DATA. Tolerate non-string cells.
4901 ;; Initialize communication channel in INFO.
4903 (let ((org-inhibit-startup t)) (org-mode))
4904 (let ((standard-output (current-buffer))
4905 (org-element-use-cache nil))
4907 (cond ((eq e 'hline) (princ "|--\n"))
4909 (princ "| ") (dolist (c e) (princ c) (princ " |"))
4911 ;; Add back-end specific filters, but not user-defined ones. In
4912 ;; particular, make sure to call parse-tree filters on the
4915 (let ((org-export-filters-alist nil))
4916 (org-export-install-filters
4918 (org-export-get-environment backend nil params)
4919 `(:back-end ,(org-export-get-backend backend))))))
4921 (org-export-filter-apply-functions
4922 (plist-get info :filter-parse-tree)
4923 (org-element-map (org-element-parse-buffer) 'table
4926 (when (and backend (symbolp backend) (not (org-export-get-backend backend)))
4927 (user-error "Unknown :backend value"))
4928 (when (or (not backend) (plist-get info :raw)) (require 'ox-org))
4929 ;; Handle :skip parameter.
4930 (let ((skip (plist-get info :skip)))
4932 (unless (wholenump skip) (user-error "Wrong :skip value"))
4934 (org-element-map data 'table-row
4937 (org-element-extract-element row)
4941 ;; Handle :skipcols parameter.
4942 (let ((skipcols (plist-get info :skipcols)))
4944 (unless (consp skipcols) (user-error "Wrong :skipcols value"))
4945 (org-element-map data 'table
4947 (let ((specialp (org-export-table-has-special-column-p table)))
4948 (dolist (row (org-element-contents table))
4949 (when (eq (org-element-property :type row) 'standard)
4951 (dolist (cell (nthcdr (if specialp 1 0)
4952 (org-element-contents row)))
4953 (when (memq c skipcols)
4954 (org-element-extract-element cell))
4955 (cl-incf c))))))))))
4956 ;; Since we are going to export using a low-level mechanism,
4957 ;; ignore special column and special rows manually.
4958 (let ((special? (org-export-table-has-special-column-p data))
4960 (org-element-map data (if special? '(table-cell table-row) 'table-row)
4962 (when (if (eq (org-element-type datum) 'table-row)
4963 (org-export-table-row-is-special-p datum nil)
4964 (org-export-first-sibling-p datum nil))
4965 (push datum ignore))))
4966 (setq info (plist-put info :ignore-list ignore)))
4967 ;; We use a low-level mechanism to export DATA so as to skip all
4968 ;; usual pre-processing and post-processing, i.e., hooks, Babel
4969 ;; code evaluation, include keywords and macro expansion. Only
4970 ;; back-end specific filters are retained.
4971 (let ((output (org-export-data-with-backend data custom-backend info)))
4972 ;; Remove final newline.
4973 (if (org-string-nw-p output) (substring-no-properties output 0 -1) ""))))
4975 (defun org-table--generic-apply (value name &optional with-cons &rest args)
4976 (cond ((null value) nil)
4977 ((functionp value) `(funcall ',value ,@args))
4979 (cond ((consp (car args)) `(apply #'format ,value ,@args))
4980 (args `(format ,value ,@args))
4982 ((and with-cons (consp value))
4983 `(let ((val (cadr (memq column ',value))))
4984 (cond ((null val) contents)
4985 ((stringp val) (format val ,@args))
4986 ((functionp val) (funcall val ,@args))
4987 (t (user-error "Wrong %s value" ,name)))))
4988 (t (user-error "Wrong %s value" name))))
4990 (defun org-table--to-generic-table (params)
4991 "Return custom table transcoder according to PARAMS.
4992 PARAMS is a plist. See `orgtbl-to-generic' for more
4994 (let ((backend (plist-get params :backend))
4995 (splice (plist-get params :splice))
4996 (tstart (plist-get params :tstart))
4997 (tend (plist-get params :tend)))
4998 `(lambda (table contents info)
5000 ,(and tstart (not splice)
5001 `(concat ,(org-table--generic-apply tstart ":tstart") "\n"))
5002 ,(if (or (not backend) tstart tend splice) 'contents
5003 `(org-export-with-backend ',backend table contents info))
5004 ,(org-table--generic-apply (and (not splice) tend) ":tend")))))
5006 (defun org-table--to-generic-row (params)
5007 "Return custom table row transcoder according to PARAMS.
5008 PARAMS is a plist. See `orgtbl-to-generic' for more
5010 (let* ((backend (plist-get params :backend))
5011 (lstart (plist-get params :lstart))
5012 (llstart (plist-get params :llstart))
5013 (hlstart (plist-get params :hlstart))
5014 (hllstart (plist-get params :hllstart))
5015 (lend (plist-get params :lend))
5016 (llend (plist-get params :llend))
5017 (hlend (plist-get params :hlend))
5018 (hllend (plist-get params :hllend))
5019 (lfmt (plist-get params :lfmt))
5020 (llfmt (plist-get params :llfmt))
5021 (hlfmt (plist-get params :hlfmt))
5022 (hllfmt (plist-get params :hllfmt)))
5023 `(lambda (row contents info)
5024 (if (eq (org-element-property :type row) 'rule)
5026 ((plist-member params :hline)
5027 (org-table--generic-apply (plist-get params :hline) ":hline"))
5028 (backend `(org-export-with-backend ',backend row nil info)))
5029 (let ((headerp ,(and (or hlfmt hlstart hlend)
5030 '(org-export-table-row-in-header-p row info)))
5032 ,(and (or hllfmt hllstart hllend)
5033 '(org-export-table-row-ends-header-p row info)))
5034 (lastp (not (org-export-get-next-element row info))))
5036 ;; Check if we can apply `:lfmt', `:llfmt', `:hlfmt', or
5037 ;; `:hllfmt' to CONTENTS. Otherwise, fallback on
5038 ;; `:lstart', `:lend' and their relatives.
5040 '(org-element-map row 'table-cell
5042 ;; Export all cells, without separators.
5044 ;; Use `org-export-data-with-backend'
5045 ;; instead of `org-export-data' to eschew
5046 ;; cached values, which
5047 ;; ignore :orgtbl-ignore-sep parameter.
5048 (org-export-data-with-backend
5050 (plist-get info :back-end)
5051 (org-combine-plists info '(:orgtbl-ignore-sep t))))
5055 `(last-header-p ,(org-table--generic-apply
5056 hllfmt ":hllfmt" nil cells)))
5058 `(headerp ,(org-table--generic-apply
5059 hlfmt ":hlfmt" nil cells)))
5061 `(lastp ,(org-table--generic-apply
5062 llfmt ":llfmt" nil cells)))
5064 ,(if lfmt (org-table--generic-apply lfmt ":lfmt" nil cells)
5068 (or hllstart hllend)
5071 ,(org-table--generic-apply hllstart ":hllstart")
5073 ,(org-table--generic-apply hllend ":hllend"))))
5078 ,(org-table--generic-apply hlstart ":hlstart")
5080 ,(org-table--generic-apply hlend ":hlend"))))
5085 ,(org-table--generic-apply llstart ":llstart")
5087 ,(org-table--generic-apply llend ":llend"))))
5092 ,(org-table--generic-apply lstart ":lstart")
5094 ,(org-table--generic-apply lend ":lend")))
5096 `(org-export-with-backend
5097 ',backend row contents info))
5098 (t 'contents)))))))))))))))
5100 (defun org-table--to-generic-cell (params)
5101 "Return custom table cell transcoder according to PARAMS.
5102 PARAMS is a plist. See `orgtbl-to-generic' for more
5104 (let* ((backend (plist-get params :backend))
5105 (efmt (plist-get params :efmt))
5106 (fmt (plist-get params :fmt))
5107 (hfmt (plist-get params :hfmt))
5108 (sep (plist-get params :sep))
5109 (hsep (plist-get params :hsep)))
5110 `(lambda (cell contents info)
5111 ;; Make sure that contents are exported as Org data when :raw
5112 ;; parameter is non-nil.
5113 ,(when (and backend (plist-get params :raw))
5115 ;; Since we don't know what are the pseudo object
5116 ;; types defined in backend, we cannot pass them to
5117 ;; `org-element-interpret-data'. As a consequence,
5118 ;; they will be treated as pseudo elements, and will
5119 ;; have newlines appended instead of spaces.
5120 ;; Therefore, we must make sure :post-blank value is
5121 ;; really turned into spaces.
5122 (replace-regexp-in-string
5125 (org-element-interpret-data
5126 (org-element-contents cell))))))
5128 (let ((headerp ,(and (or hfmt hsep)
5129 '(org-export-table-row-in-header-p
5130 (org-export-get-parent-element cell) info)))
5132 ;; Call costly `org-export-table-cell-address' only if
5133 ;; absolutely necessary, i.e., if one
5134 ;; of :fmt :efmt :hmft has a "plist type" value.
5135 ,(and (cl-some (lambda (v) (integerp (car-safe v)))
5136 (list efmt hfmt fmt))
5137 '(1+ (cdr (org-export-table-cell-address cell info))))))
5139 ;; Check if we can apply `:efmt' on CONTENTS.
5141 `(when (string-match orgtbl-exp-regexp contents)
5142 (let ((mantissa (match-string 1 contents))
5143 (exponent (match-string 2 contents)))
5144 (setq contents ,(org-table--generic-apply
5145 efmt ":efmt" t 'mantissa 'exponent)))))
5146 ;; Check if we can apply FMT (or HFMT) on CONTENTS.
5148 ,(and hfmt `(headerp (setq contents ,(org-table--generic-apply
5149 hfmt ":hfmt" t 'contents))))
5150 ,(and fmt `(t (setq contents ,(org-table--generic-apply
5151 fmt ":fmt" t 'contents))))))
5152 ;; If a separator is provided, use it instead of BACKEND's.
5153 ;; Separators are ignored when LFMT (or equivalent) is
5157 `(if (or ,(and (not sep) '(not headerp))
5158 (plist-get info :orgtbl-ignore-sep)
5159 (not (org-export-get-next-element cell info)))
5160 ,(if (not backend) 'contents
5161 `(org-export-with-backend ',backend cell contents info))
5163 ,(if (and sep hsep) `(if headerp ,hsep ,sep)
5165 (backend `(org-export-with-backend ',backend cell contents info))
5169 (defun orgtbl-to-tsv (table params)
5170 "Convert the orgtbl-mode table to TAB separated material."
5171 (orgtbl-to-generic table (org-combine-plists '(:sep "\t") params)))
5174 (defun orgtbl-to-csv (table params)
5175 "Convert the orgtbl-mode table to CSV material.
5176 This does take care of the proper quoting of fields with comma or quotes."
5177 (orgtbl-to-generic table
5178 (org-combine-plists '(:sep "," :fmt org-quote-csv-field)
5182 (defun orgtbl-to-latex (table params)
5183 "Convert the orgtbl-mode TABLE to LaTeX.
5185 TABLE is a list, each entry either the symbol `hline' for
5186 a horizontal separator line, or a list of fields for that line.
5187 PARAMS is a property list of parameters that can influence the
5188 conversion. All parameters from `orgtbl-to-generic' are
5189 supported. It is also possible to use the following ones:
5193 When non-nil, use formal \"booktabs\" style.
5197 Specify environment to use, as a string. If you use
5198 \"longtable\", you may also want to specify :language property,
5199 as a string, to get proper continuation strings."
5204 ;; Provide sane default values.
5205 (list :backend 'latex
5206 :latex-default-table-mode 'table
5207 :latex-tables-centered nil
5208 :latex-tables-booktabs (plist-get params :booktabs)
5209 :latex-table-scientific-notation nil
5210 :latex-default-table-environment
5211 (or (plist-get params :environment) "tabular"))
5215 (defun orgtbl-to-html (table params)
5216 "Convert the orgtbl-mode TABLE to HTML.
5218 TABLE is a list, each entry either the symbol `hline' for
5219 a horizontal separator line, or a list of fields for that line.
5220 PARAMS is a property list of parameters that can influence the
5221 conversion. All parameters from `orgtbl-to-generic' are
5222 supported. It is also possible to use the following one:
5226 Attributes and values, as a plist, which will be used in
5232 ;; Provide sane default values.
5233 (list :backend 'html
5234 :html-table-data-tags '("<td%s>" . "</td>")
5235 :html-table-use-header-tags-for-first-column nil
5236 :html-table-align-individual-fields t
5237 :html-table-row-tags '("<tr>" . "</tr>")
5238 :html-table-attributes
5239 (if (plist-member params :attributes)
5240 (plist-get params :attributes)
5241 '(:border "2" :cellspacing "0" :cellpadding "6" :rules "groups"
5246 (defun orgtbl-to-texinfo (table params)
5247 "Convert the orgtbl-mode TABLE to Texinfo.
5249 TABLE is a list, each entry either the symbol `hline' for
5250 a horizontal separator line, or a list of fields for that line.
5251 PARAMS is a property list of parameters that can influence the
5252 conversion. All parameters from `orgtbl-to-generic' are
5253 supported. It is also possible to use the following one:
5257 Column widths, as a string. When providing column fractions,
5258 \"@columnfractions\" command can be omitted."
5259 (require 'ox-texinfo)
5264 (list :backend 'texinfo
5265 :texinfo-tables-verbatim nil
5266 :texinfo-table-scientific-notation nil)
5268 (columns (let ((w (plist-get params :columns)))
5270 ((string-match-p "{\\|@columnfractions " w) w)
5271 (t (concat "@columnfractions " w))))))
5272 (if (not columns) output
5273 (replace-regexp-in-string
5274 "@multitable \\(.*\\)" columns output t nil 1))))
5277 (defun orgtbl-to-orgtbl (table params)
5278 "Convert the orgtbl-mode TABLE into another orgtbl-mode table.
5280 TABLE is a list, each entry either the symbol `hline' for
5281 a horizontal separator line, or a list of fields for that line.
5282 PARAMS is a property list of parameters that can influence the
5283 conversion. All parameters from `orgtbl-to-generic' are
5286 Useful when slicing one table into many. The :hline, :sep,
5287 :lstart, and :lend provide orgtbl framing. :tstart and :tend can
5288 be set to provide ORGTBL directives for the generated table."
5290 (orgtbl-to-generic table (org-combine-plists params (list :backend 'org))))
5292 (defun orgtbl-to-table.el (table params)
5293 "Convert the orgtbl-mode TABLE into a table.el table.
5294 TABLE is a list, each entry either the symbol `hline' for
5295 a horizontal separator line, or a list of fields for that line.
5296 PARAMS is a property list of parameters that can influence the
5297 conversion. All parameters from `orgtbl-to-generic' are
5300 (insert (orgtbl-to-orgtbl table params))
5302 (replace-regexp-in-string
5304 (replace-regexp-in-string "|-" "+-" (buffer-substring 1 (buffer-size))))))
5306 (defun orgtbl-to-unicode (table params)
5307 "Convert the orgtbl-mode TABLE into a table with unicode characters.
5309 TABLE is a list, each entry either the symbol `hline' for
5310 a horizontal separator line, or a list of fields for that line.
5311 PARAMS is a property list of parameters that can influence the
5312 conversion. All parameters from `orgtbl-to-generic' are
5313 supported. It is also possible to use the following ones:
5317 When non-nil, use \"ascii-art-to-unicode\" package to translate
5318 the table. You can download it here:
5319 http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el.
5323 When non-nil, narrow columns width than provided width cookie,
5324 using \"=>\" as an ellipsis, just like in an Org mode buffer."
5329 (list :backend 'ascii
5330 :ascii-charset 'utf-8
5331 :ascii-table-widen-columns (not (plist-get params :narrow))
5332 :ascii-table-use-ascii-art (plist-get params :ascii-art))
5335 ;; Put the cursor in a column containing numerical values
5338 ;; A new column is added with a bar plot.
5339 ;; When the table is refreshed (C-u C-c *),
5340 ;; the plot is updated to reflect the new values.
5342 (defun orgtbl-ascii-draw (value min max &optional width characters)
5343 "Draw an ascii bar in a table.
5344 VALUE is the value to plot, it determines the width of the bar to draw.
5345 MIN is the value that will be displayed as empty (zero width bar).
5346 MAX is the value that will draw a bar filling all the WIDTH.
5347 WIDTH is the span in characters from MIN to MAX.
5348 CHARACTERS is a string that will compose the bar, with shades of grey
5349 from pure white to pure black. It defaults to a 10 characters string
5350 of regular ascii characters."
5351 (let* ((width (ceiling (or width 12)))
5352 (characters (or characters " .:;c!lhVHW"))
5353 (len (1- (length characters)))
5354 (value (float (if (numberp value)
5355 value (string-to-number value))))
5356 (relative (/ (- value min) (- max min)))
5357 (steps (round (* relative width len))))
5358 (cond ((< steps 0) "too small")
5359 ((> steps (* width len)) "too large")
5360 (t (let* ((int-division (/ steps len))
5361 (remainder (- steps (* int-division len))))
5362 (concat (make-string int-division (elt characters len))
5363 (string (elt characters remainder))))))))
5366 (defun orgtbl-ascii-plot (&optional ask)
5367 "Draw an ASCII bar plot in a column.
5369 With cursor in a column containing numerical values, this function
5370 will draw a plot in a new column.
5372 ASK, if given, is a numeric prefix to override the default 12
5373 characters width of the plot. ASK may also be the `\\[universal-argument]' \
5375 which will prompt for the width."
5377 (let ((col (org-table-current-column))
5378 (min 1e999) ; 1e999 will be converted to infinity
5379 (max -1e999) ; which is the desired result
5380 (table (org-table-to-lisp))
5383 (read-number "Length of column " 12))
5386 ;; Skip any hline a the top of table.
5387 (while (eq (car table) 'hline) (setq table (cdr table)))
5388 ;; Skip table header if any.
5389 (dolist (x (or (cdr (memq 'hline table)) table))
5391 (setq x (nth (1- col) x))
5393 "^[-+]?\\([0-9]*[.]\\)?[0-9]*\\([eE][+-]?[0-9]+\\)?$"
5395 (setq x (string-to-number x))
5396 (when (> min x) (setq min x))
5397 (when (< max x) (setq max x)))))
5398 (org-table-insert-column)
5399 (org-table-move-column-right)
5400 (org-table-store-formulas
5403 (concat "$" (number-to-string (1+ col)))
5404 (format "'(%s $%s %s %s %s)"
5405 "orgtbl-ascii-draw" col min max length))
5406 (org-table-get-stored-formulas)))
5407 (org-table-recalculate t)))
5409 ;; Example of extension: unicode characters
5410 ;; Here are two examples of different styles.
5412 ;; Unicode block characters are used to give a smooth effect.
5413 ;; See http://en.wikipedia.org/wiki/Block_Elements
5414 ;; Use one of those drawing functions
5415 ;; - orgtbl-ascii-draw (the default ascii)
5416 ;; - orgtbl-uc-draw-grid (unicode with a grid effect)
5417 ;; - orgtbl-uc-draw-cont (smooth unicode)
5419 ;; This is best viewed with the "DejaVu Sans Mono" font
5420 ;; (use M-x set-default-font).
5422 (defun orgtbl-uc-draw-grid (value min max &optional width)
5423 "Draw a bar in a table using block unicode characters.
5424 It is a variant of orgtbl-ascii-draw with Unicode block
5425 characters, for a smooth display. Bars appear as grids (to the
5426 extent the font allows)."
5427 ;; http://en.wikipedia.org/wiki/Block_Elements
5428 ;; best viewed with the "DejaVu Sans Mono" font.
5429 (orgtbl-ascii-draw value min max width
5430 " \u258F\u258E\u258D\u258C\u258B\u258A\u2589"))
5432 (defun orgtbl-uc-draw-cont (value min max &optional width)
5433 "Draw a bar in a table using block unicode characters.
5434 It is a variant of orgtbl-ascii-draw with Unicode block
5435 characters, for a smooth display. Bars are solid (to the extent
5437 (orgtbl-ascii-draw value min max width
5438 " \u258F\u258E\u258D\u258C\u258B\u258A\u2589\u2588"))
5440 (defun org-table-get-remote-range (name-or-id form)
5441 "Get a field value or a list of values in a range from table at ID.
5443 NAME-OR-ID may be the name of a table in the current file as set
5444 by a \"#+NAME:\" directive. The first table following this line
5445 will then be used. Alternatively, it may be an ID referring to
5446 any entry, also in a different file. In this case, the first
5447 table in that entry will be referenced.
5448 FORM is a field or range descriptor like \"@2$3\" or \"B3\" or
5449 \"@I$2..@II$2\". All the references must be absolute, not relative.
5451 The return value is either a single string for a single field, or a
5452 list of the fields in the rectangle."
5454 (let ((case-fold-search t) (id-loc nil)
5455 ;; Protect a bunch of variables from being overwritten by
5456 ;; the context of the remote table.
5457 org-table-column-names org-table-column-name-regexp
5458 org-table-local-parameters org-table-named-field-locations
5459 org-table-current-line-types
5460 org-table-current-begin-pos org-table-dlines
5461 org-table-current-ncol
5462 org-table-hlines org-table-last-alignment
5463 org-table-last-column-widths org-table-last-alignment
5464 org-table-last-column-widths
5466 (setq form (org-table-convert-refs-to-rc form))
5467 (org-with-wide-buffer
5468 (goto-char (point-min))
5469 (if (re-search-forward
5470 (concat "^[ \t]*#\\+\\(tbl\\)?name:[ \t]*"
5471 (regexp-quote name-or-id) "[ \t]*$")
5473 (setq buffer (current-buffer) loc (match-beginning 0))
5474 (setq id-loc (org-id-find name-or-id 'marker))
5475 (unless (and id-loc (markerp id-loc))
5476 (user-error "Can't find remote table \"%s\"" name-or-id))
5477 (setq buffer (marker-buffer id-loc)
5478 loc (marker-position id-loc))
5479 (move-marker id-loc nil))
5480 (with-current-buffer buffer
5481 (org-with-wide-buffer
5484 (unless (and (re-search-forward "^\\(\\*+ \\)\\|^[ \t]*|" nil t)
5485 (not (match-beginning 1)))
5486 (user-error "Cannot find a table at NAME or ID %s" name-or-id))
5488 (setq form (org-table-formula-substitute-names
5489 (org-table-formula-handle-first/last-rc form)))
5490 (if (and (string-match org-table-range-regexp form)
5491 (> (length (match-string 0 form)) 1))
5492 (org-table-get-range
5493 (match-string 0 form) org-table-current-begin-pos 1)
5496 (defun org-table-remote-reference-indirection (form)
5497 "Return formula with table remote references substituted by indirection.
5498 For example \"remote($1, @>$2)\" => \"remote(year_2013, @>$1)\".
5499 This indirection works only with the format @ROW$COLUMN. The
5500 format \"B3\" is not supported because it can not be
5501 distinguished from a plain table name or ID."
5503 ;; Same as in `org-table-eval-formula'.
5504 (concat "\\<remote([ \t]*\\("
5505 ;; Allow "$1", "@<", "$-1", "@<<$1" etc.
5507 "\\)[ \t]*,[ \t]*\\([^\n)]+\\))")))
5508 (replace-regexp-in-string
5512 (let ((eq (org-table-formula-handle-first/last-rc (match-string 1 m))))
5513 (org-table-get-range
5514 (if (string-match-p "\\`\\$[0-9]+\\'" eq)
5519 (defmacro org-define-lookup-function (mode)
5520 (let ((mode-str (symbol-name mode))
5521 (first-p (eq mode 'first))
5522 (all-p (eq mode 'all)))
5523 (let ((plural-str (if all-p "s" "")))
5524 `(defun ,(intern (format "org-lookup-%s" mode-str)) (val s-list r-list &optional predicate)
5525 ,(format "Find %s occurrence%s of VAL in S-LIST; return corresponding element%s of R-LIST.
5526 If R-LIST is nil, return matching element%s of S-LIST.
5527 If PREDICATE is not nil, use it instead of `equal' to match VAL.
5528 Matching is done by (PREDICATE VAL S), where S is an element of S-LIST.
5529 This function is generated by a call to the macro `org-define-lookup-function'."
5530 mode-str plural-str plural-str plural-str)
5531 (let ,(let ((lvars '((p (or predicate 'equal))
5533 (rl (or r-list s-list))
5535 (if first-p (cons '(match-p nil) lvars) lvars))
5536 (while ,(if first-p '(and (not match-p) sl) 'sl)
5537 (when (funcall p val (car sl))
5538 ,(when first-p '(setq match-p t))
5539 (let ((rval (car rl)))
5540 (setq ret ,(if all-p '(append ret (list rval)) 'rval))))
5541 (setq sl (cdr sl) rl (cdr rl)))
5544 (org-define-lookup-function first)
5545 (org-define-lookup-function last)
5546 (org-define-lookup-function all)
5548 (provide 'org-table)
5551 ;; generated-autoload-file: "org-loaddefs.el"
5554 ;;; org-table.el ends here