From mboxrd@z Thu Jan 1 00:00:00 1970 From: Kaushal Modi Subject: Buffer local value lost when exporting? (was Evaluate all org tables in file before exporting) Date: Thu, 12 May 2016 15:54:09 +0000 Message-ID: References: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=001a1134e9e4c500dc0532a72b33 Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:45379) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b0swQ-0001P7-E3 for emacs-orgmode@gnu.org; Thu, 12 May 2016 11:54:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b0swO-0007aW-5G for emacs-orgmode@gnu.org; Thu, 12 May 2016 11:54:22 -0400 Received: from mail-oi0-x236.google.com ([2607:f8b0:4003:c06::236]:34781) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b0swN-0007aR-O5 for emacs-orgmode@gnu.org; Thu, 12 May 2016 11:54:20 -0400 Received: by mail-oi0-x236.google.com with SMTP id k142so126612088oib.1 for ; Thu, 12 May 2016 08:54:19 -0700 (PDT) In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: emacs-org list --001a1134e9e4c500dc0532a72b33 Content-Type: text/plain; charset=UTF-8 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 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 --001a1134e9e4c500dc0532a72b33 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hi all,

I almost have a squeaky clean s= olution 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 t= hat, 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 l= ocal value of=C2=A0modi/org-table-enable-buffer-wide-r= ecalculation 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).

=3D=3D=3D=3D=3D sample.org =3D=3D=3D=3D=3D
| N |
|---|
<= div>| =C2=A0 |
| =C2=A0 |
| =C2=A0 |
| =C2=A0= |
#+TBLFM: $1=3D@#-1

# Local Variables:=
# modi/org-table-enable-buffer-wide-recalculation: nil
# End:
=3D=3D=3D=3D=3D

=3D=3D=3D=3D=3D auto-recalculate-org-tables.el =3D=3D= =3D=3D=3D
;; Recalculate all org tables in the bu= ffer when saving.
(defvar-local modi/org-table-enable-buffer-wide= -recalculation t
=C2=A0 "When non-nil, all the org tables in= the buffer will be recalculated when
saving the file or when exp= orting.
This variable is buffer local.")
;; Mark `= modi/org-table-enable-buffer-wide-recalculation' as a safe local
<= div>;; variable as long as its value is t or nil. That way you are not prom= pted
;; to add that to `safe-local-variable-values' in custom= .el.
(put 'modi/org-table-enable-buffer-wide-recalculation
=C2=A0 =C2=A0 =C2=A0'safe-local-variable (lambda (val) (or (equ= al val nil) (equal val t))))

(defun modi/org-table= -recalculate-buffer-tables (&rest args)
=C2=A0 "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 ARG= S as optional arguments that are needed for any
function that is = added to the `org-export-before-processing-hook'."
=C2= =A0 (message "modi/org-table-enable-buffer-wide-recalculation: %S"= ;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0modi/org-table-enable-= buffer-wide-recalculation)
=C2=A0 (when modi/org-table-enable-buf= fer-wide-recalculation
=C2=A0 =C2=A0 (org-table-recalculate-buffe= r-tables)))

(defun modi/org-table-recalculate-befo= re-save ()
=C2=A0 "Recalculate all org tables in the buffer = before saving."
=C2=A0 (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-reca= lculation'
;; does not seem to be respected at the moment at = the time of running
;; `org-export-before-processing-hook'. I= nvestigating this ..
;; For now, as the default value of that var= iable is t, all org tables
;; in the buffer will always be recalc= ulated at the time of export even
;; if the buffer local value of= that var is nil.
(add-hook 'org-export-before-processing-hoo= k #'modi/org-table-recalculate-buffer-tables)
=3D=3D=3D= =3D=3D

On Fri, May 6, = 2016 at 10:58 AM Kaushal Modi <kaushal.modi@gmail.com> wrote:
Hi folks,

I am looking if anyone al= ready 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 mis= sing some org variable, setting which should take care of this?
<= br>
Thanks.
--

--
Kaushal Modi

--

-- Kaushal Modi

--001a1134e9e4c500dc0532a72b33--