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