Hi all,
I almost have a squeaky clean solution to this thanks to the hint provided
by someone on emacs.SE ( http://emacs.stackexchange.com/a/22215/115 ).
But in the process, I seem to have stumbled upon a bug.. the buffer local
value of variables are not respected at the time of exports.
To demonstrate that, please evaluate the elisp code at the end of this
email and do C-c C-e h o on the below MWE of org file:
As the buffer local value of modi/org-table-enable-buffer-wide-recalculation
var is nil, that table does not get recalculated when saving. But it always
gets recalculated when exporting (e.g. using C-c C-e h o).
===== sample.org =====
| N |
|---|
| |
| |
| |
| |
#+TBLFM: $1=@#-1
# Local Variables:
# modi/org-table-enable-buffer-wide-recalculation: nil
# End:
=====
===== auto-recalculate-org-tables.el =====
;; 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 or when exporting.
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 (lambda (val) (or (equal val nil) (equal val t))))
(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 ARGS as optional arguments that are needed for any
function that is added to the `org-export-before-processing-hook'."
(message "modi/org-table-enable-buffer-wide-recalculation: %S"
modi/org-table-enable-buffer-wide-recalculation)
(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)
;; FIXME: The buffer local value of
`modi/org-table-enable-buffer-wide-recalculation'
;; does not seem to be respected at the moment at the time of running
;; `org-export-before-processing-hook'. Investigating this ..
;; For now, as the default value of that variable is t, all org tables
;; in the buffer will always be recalculated at the time of export even
;; if the buffer local value of that var is nil.
(add-hook 'org-export-before-processing-hook
#'modi/org-table-recalculate-buffer-tables)
=====
On Fri, May 6, 2016 at 10:58 AM Kaushal Modi <[email protected]> wrote:
> Hi folks,
>
> I am looking if anyone already has a solution (a function) that evaluates
> all the tables in the file/buffer. I should be then simply able to add that
> to local value of before-save-hook in org-mode, right?
>
> Currently, I need to remember to C-u C-c C-c on all the tables before
> exporting to ensure that all the calculated values are correct.
>
> Or am I missing some org variable, setting which should take care of this?
>
> Thanks.
> --
>
> --
> Kaushal Modi
>
--
--
Kaushal Modi