summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIppei FURUHASHI <top.tuna+orgmode@gmail.com>2013-04-02 18:11:26 +0900
committerBastien Guerry <bzg@altern.org>2013-04-05 08:37:51 +0200
commit6693456dd7980a0680acf76a4ff13edfc1a6ecc3 (patch)
tree0a08d64483ceb5990f1fdae2732f2c28e76eb9a6
parentc8c17460f10135f188a3d0d6ede3481cd4cd11af (diff)
downloadorg-mode-6693456dd7980a0680acf76a4ff13edfc1a6ecc3.tar.gz
org-table.el (org-calc-current-TBLFM): Add function
* org-table.el (org-calc-current-TBLFM): New function to 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 point is in the #+TBLFM line. * testing/lisp/test-org-table.el: Add test.
-rw-r--r--lisp/org-table.el24
-rw-r--r--lisp/org.el7
-rw-r--r--testing/lisp/test-org-table.el37
3 files changed, 65 insertions, 3 deletions
diff --git a/lisp/org-table.el b/lisp/org-table.el
index 3fb42b1..ccd1735 100644
--- a/lisp/org-table.el
+++ b/lisp/org-table.el
@@ -3171,6 +3171,30 @@ with the prefix ARG."
(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")
+ (unless (org-at-TBLFM-p) (user-error "Not at a #+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 0c07512..c291666 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -20201,9 +20201,10 @@ This command does many different things, depending on context:
(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 @@ reference (with row). Format specifier N."
(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