5

I am looking for a solution (a function) that evaluates all the tables in the org file/buffer. I should be then simply able to add that to the local value of before-save-hook in org-mode.

Currently, I need to remember to C-u C-c C-c on all the tables before exporting to ensure that the calculated values in all tables are current.

Or am I missing some org variable, setting which should take care of this?

2 Answers 2

8

I found the function you need (with a tiny bit of luck). You just have to add the following function to your before-save-hook.

org-table-recalculate-buffer-tables 

Assuming you have no other hooks, this means just to set.

(add-hook 'before-save-hook 'org-table-recalculate-buffer-tables) 

Feel free to adjust it to your needs.

3
  • 1
    There is no point in using setq instead of add-hook IMHO Commented May 12, 2016 at 16:30
  • You are probably right. I don't have any hooks in my setup so I tested it with a "setq" and it worked. I only found the function required by browsing the code Commented May 12, 2016 at 16:32
  • I have replaced setq with add-hook. Commented Jan 31, 2017 at 15:37
1

Thanks to the hint to the correct function by Joafigue, I solved the question by adding this to my config:

;; Recalculate all org tables in the buffer when saving. (defvar-local modi/org-table-enable-buffer-wide-recalculation t "When non-nil, all the org tables in the buffer will be recalculated when saving the file. This variable is buffer local.") ;; Mark `modi/org-table-enable-buffer-wide-recalculation' as a safe local ;; variable as long as its value is t or nil. That way you are not prompted ;; to add that to `safe-local-variable-values' in custom.el. (put 'modi/org-table-enable-buffer-wide-recalculation 'safe-local-variable #'booleanp) (defun modi/org-table-recalculate-buffer-tables (&rest args) "Wrapper function for `org-table-recalculate-buffer-tables' that runs that function only if `modi/org-table-enable-buffer-wide-recalculation' is non-nil. Also, this function has optional ARGS that is needed for any function that is added to `org-export-before-processing-hook'. This would be useful if this function is ever added to that hook." (when modi/org-table-enable-buffer-wide-recalculation (org-table-recalculate-buffer-tables))) (defun modi/org-table-recalculate-before-save () "Recalculate all org tables in the buffer before saving." (add-hook 'before-save-hook #'modi/org-table-recalculate-buffer-tables nil :local)) (add-hook 'org-mode-hook #'modi/org-table-recalculate-before-save) 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.