浏览代码

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.
Ippei FURUHASHI 12 年之前
父节点
当前提交
6693456dd7
共有 3 个文件被更改,包括 65 次插入3 次删除
  1. 24 0
      lisp/org-table.el
  2. 4 3
      lisp/org.el
  3. 37 0
      testing/lisp/test-org-table.el

+ 24 - 0
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."

+ 4 - 3
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))

+ 37 - 0
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