org-table.el: Fix a bug of leaving the inserted TBLFM line

* org-table.el (org-calc-current-TBLFM): Ensure to remove the
currently inserted TBLFM line, when calling `org-table-recalculate'
returns an error and the processing stops.

* testing/lisp/test-org-table.el: Add test.

When you hit =C-c C-c= at the line of "#+TBLFM: $2=$1*2::$2=$1**2" in

    | 1 |   |
    | 2 |   |
    #+TBLFM: $2=$1*1
    #+TBLFM: $2=$1*2::$2=$1**2

you got:

    | 1 |   |
    | 2 |   |
    #+TBLFM: $2=$1*2::$2=$1**2
    #+TBLFM: $2=$1*1
    #+TBLFM: $2=$1*2::$2=$1**2

with the error message of:

    user-error: Double definition `$2=' in TBLFM line, please fix by hand

In this case, you expected:

    | 1 |   |
    | 2 |   |
    #+TBLFM: $2=$1*1
    #+TBLFM: $2=$1*2::$2=$1**2
This commit is contained in:
Ippei FURUHASHI 2013-04-05 16:07:00 +09:00 committed by Bastien Guerry
parent e01daa00be
commit 67a55acb39
2 changed files with 29 additions and 3 deletions

View File

@ -3190,10 +3190,11 @@ with the prefix ARG."
(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)))
(unwind-protect
(org-call-with-arg 'org-table-recalculate (or arg t))
;; Delete the formula inserted temporarily
(delete-region s e))))
;; delete the formula inserted temporarily
(delete-region s e))))))
(defun org-TBLFM-begin ()
"Find the beginning of the TBLFM lines and return its position.

View File

@ -929,6 +929,31 @@ reference (with row). Format specifier N."
(should (string= got
expect)))))
(ert-deftest test-org-table/org-calc-current-TBLFM-when-stop-because-of-error ()
"org-calc-current-TBLFM should preserve the input as it was."
(org-test-with-temp-text-in-file
"
| 1 | 1 |
| 2 | 2 |
#+TBLFM: $2=$1*1
#+TBLFM: $2=$1*2::$2=$1*2
#+TBLFM: $2=$1*3
"
(let ((expect "
| 1 | 1 |
| 2 | 2 |
#+TBLFM: $2=$1*1
#+TBLFM: $2=$1*2::$2=$1*2
#+TBLFM: $2=$1*3
"))
(goto-char (point-min))
(forward-line 4)
(should-error (org-calc-current-TBLFM))
(setq got (buffer-string))
(message "%s" got)
(should (string= got
expect)))))
(provide 'test-org-table)
;;; test-org-table.el ends here