From 12cd28d44a67f1b3efe666fe981430bf15aafc15 Mon Sep 17 00:00:00 2001 From: Ippei FURUHASHI Date: Tue, 2 Apr 2013 18:11:26 +0900 Subject: [PATCH 3/4] org-table.el (org-calc-current-TBLFM): Add function * org-table.el (org-calc-current-TBLFM): re-calculate the table by applying the #+TBLFM in the line where the point is. * org.el (org-ctrl-c-ctrl-c): Call `org-calc-current-TBLFM' when the point is in the #+TBLFM line. * testing/lisp/test-org-table.el: Add test. --- lisp/org-table.el | 24 ++++++++++++++++++++++++ lisp/org.el | 7 ++++--- testing/lisp/test-org-table.el | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/lisp/org-table.el b/lisp/org-table.el index 78fbb2e..4b97760 100644 --- a/lisp/org-table.el +++ b/lisp/org-table.el @@ -3171,6 +3171,30 @@ (defun org-table-iterate-buffer-tables () (setq checksum c1))) (user-error "No convergence after %d iterations" imax)))))) +(defun org-calc-current-TBLFM (&optional arg) + "Apply the #+TBLFM in the line to the table." + (interactive "P") + (if (not (org-at-TBLFM-p)) (error "Not at #+TBLFM line")) + (let ((formula (buffer-substring + (point-at-bol) + (point-at-eol))) + s e) + (save-excursion + ;; insert a temporary formula at right after the table + (goto-char (org-TBLFM-begin)) + (setq s (set-marker (make-marker) (point))) + (insert (concat formula "\n")) + (setq e (set-marker (make-marker) (point))) + + ;; recalculate the table + (beginning-of-line 0) ;move to the inserted line + (skip-chars-backward " \r\n\t") + (if (org-at-table-p) + (org-call-with-arg 'org-table-recalculate (or arg t))) + + ;; delete the formula inserted temporarily + (delete-region s e)))) + (defun org-TBLFM-begin () "Find the beginning of the TBLFM lines and return its position. Return nil when the beginning of TBLFM line was not found." diff --git a/lisp/org.el b/lisp/org.el index ef27944..51b8812 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -20174,9 +20174,10 @@ (defun org-ctrl-c-ctrl-c (&optional arg) (and (eq type 'table-row) (= (point) (org-element-property :end context)))) (save-excursion - (goto-char (org-element-property :contents-begin context)) - (org-call-with-arg 'org-table-recalculate (or arg t)) - (orgtbl-send-table 'maybe)) + (if (org-at-TBLFM-p) (org-calc-current-TBLFM) + (goto-char (org-element-property :contents-begin context)) + (org-call-with-arg 'org-table-recalculate (or arg t)) + (orgtbl-send-table 'maybe))) (org-table-maybe-eval-formula) (cond (arg (call-interactively 'org-table-recalculate)) ((org-table-maybe-recalculate-line)) diff --git a/testing/lisp/test-org-table.el b/testing/lisp/test-org-table.el index 805f57a..dda8561 100644 --- a/testing/lisp/test-org-table.el +++ b/testing/lisp/test-org-table.el @@ -892,6 +892,43 @@ (defconst references/target-special " (should (= (org-TBLFM-begin) 61)))) +(ert-deftest test-org-table/org-calc-current-TBLFM () + (org-test-with-temp-text-in-file + " +| 1 | | +| 2 | | +#+TBLFM: $2=$1*1 +#+TBLFM: $2=$1*2 +#+TBLFM: $2=$1*3 +" + (let ((got (progn (goto-char (point-min)) + (forward-line 3) + (org-calc-current-TBLFM) + (buffer-string))) + (expect " +| 1 | 1 | +| 2 | 2 | +#+TBLFM: $2=$1*1 +#+TBLFM: $2=$1*2 +#+TBLFM: $2=$1*3 +")) + (should (string= got + expect))) + + (let ((got (progn (goto-char (point-min)) + (forward-line 4) + (org-calc-current-TBLFM) + (buffer-string))) + (expect " +| 1 | 2 | +| 2 | 4 | +#+TBLFM: $2=$1*1 +#+TBLFM: $2=$1*2 +#+TBLFM: $2=$1*3 +")) + (should (string= got + expect))))) + (provide 'test-org-table) ;;; test-org-table.el ends here -- 1.7.9.msysgit.0