diff options
author | Nicolas Goaziou <mail@nicolasgoaziou.fr> | 2017-02-01 20:56:38 +0100 |
---|---|---|
committer | Nicolas Goaziou <mail@nicolasgoaziou.fr> | 2017-02-01 20:56:38 +0100 |
commit | 73330079c0ef4d7294500155a8df061398915e7b (patch) | |
tree | 418175a38eca88cdae3d7ff99cbd2a78a332aac3 | |
parent | 76c08ea77b4cb2f1dbe42b4a8003dfa457fdfdcb (diff) | |
download | org-mode-73330079c0ef4d7294500155a8df061398915e7b.tar.gz |
org-table: Fix error with `org-table-get-field' at bob
* lisp/org-table.el (org-table-get-field): Do not return
`beginning-of-buffer' error when called at beginning of buffer.
* testing/lisp/test-org-table.el (test-org-table/get-field): New test.
-rw-r--r-- | lisp/org-table.el | 28 | ||||
-rw-r--r-- | testing/lisp/test-org-table.el | 63 |
2 files changed, 77 insertions, 14 deletions
diff --git a/lisp/org-table.el b/lisp/org-table.el index 9e32293..f6d43d3 100644 --- a/lisp/org-table.el +++ b/lisp/org-table.el @@ -1250,21 +1250,21 @@ Return t when the line exists, nil if it does not exist." (defun org-table-get-field (&optional n replace) "Return the value of the field in column N of current row. -N defaults to current field. -If REPLACE is a string, replace field with this value. The return value -is always the old value." - (and n (org-table-goto-column n)) +N defaults to current column. If REPLACE is a string, replace +field with this value. The return value is always the old +value." + (when n (org-table-goto-column n)) (skip-chars-backward "^|\n") - (backward-char 1) - (if (looking-at "|[^|\r\n]*") - (let* ((pos (match-beginning 0)) - (val (buffer-substring (1+ pos) (match-end 0)))) - (if replace - (replace-match (concat "|" (if (equal replace "") " " replace)) - t t)) - (goto-char (min (point-at-eol) (+ 2 pos))) - val) - (forward-char 1) "")) + (if (or (bolp) (looking-at-p "[ \t]*$")) + ;; Before first column or after last one. + "" + (looking-at "[^|\r\n]*") + (let* ((pos (match-beginning 0)) + (val (buffer-substring pos (match-end 0)))) + (when replace + (replace-match (if (equal replace "") " " replace) t t)) + (goto-char (min (line-end-position) (1+ pos))) + val))) ;;;###autoload (defun org-table-field-info (_arg) diff --git a/testing/lisp/test-org-table.el b/testing/lisp/test-org-table.el index 16edb26..55eacba 100644 --- a/testing/lisp/test-org-table.el +++ b/testing/lisp/test-org-table.el @@ -2093,6 +2093,69 @@ is t, then new columns should be added as needed" (let ((org-table-tab-jumps-over-hlines nil)) (org-table-next-field)) (buffer-string))))) + +;;; Miscellaneous + +(ert-deftest test-org-table/get-field () + "Test `org-table-get-field' specifications." + ;; Regular test. + (should + (equal " a " + (org-test-with-temp-text "| <point>a |" (org-table-get-field)))) + ;; Get field in open last column. + (should + (equal " a " + (org-test-with-temp-text "| <point>a " (org-table-get-field)))) + ;; Get empty field. + (should + (equal "" + (org-test-with-temp-text "|<point>|" (org-table-get-field)))) + (should + (equal " " + (org-test-with-temp-text "| <point>|" (org-table-get-field)))) + ;; Outside the table, return the empty string. + (should + (equal "" + (org-test-with-temp-text "<point>| a |" (org-table-get-field)))) + (should + (equal "" + (org-test-with-temp-text "| a |<point>" (org-table-get-field)))) + ;; With optional N argument, select a particular column in current + ;; row. + (should + (equal " 3 " + (org-test-with-temp-text "| 1 | 2 | 3 |" (org-table-get-field 3)))) + (should + (equal " 4 " + (org-test-with-temp-text "| 1 | 2 |\n<point>| 3 | 4 |" + (org-table-get-field 2)))) + ;; REPLACE optional argument is used to replace selected field. + (should + (equal "| foo |" + (org-test-with-temp-text "| <point>1 |" + (org-table-get-field nil " foo ") + (buffer-string)))) + (should + (equal "| 1 | 2 | foo |" + (org-test-with-temp-text "| 1 | 2 | 3 |" + (org-table-get-field 3 " foo ") + (buffer-string)))) + (should + (equal " 4 " + (org-test-with-temp-text "| 1 | 2 |\n<point>| 3 | 4 |" + (org-table-get-field 2)))) + ;; An empty REPLACE string clears the field. + (should + (equal "| |" + (org-test-with-temp-text "| <point>1 |" + (org-table-get-field nil "") + (buffer-string)))) + ;; When using REPLACE still return old value. + (should + (equal " 1 " + (org-test-with-temp-text "| <point>1 |" + (org-table-get-field nil " foo "))))) + (provide 'test-org-table) ;;; test-org-table.el ends here |