From 73330079c0ef4d7294500155a8df061398915e7b Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Wed, 1 Feb 2017 20:56:38 +0100 Subject: [PATCH] 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. --- lisp/org-table.el | 28 +++++++++---------- 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 9e3229347..f6d43d39a 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 16edb264d..55eacbadd 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 "| a |" (org-table-get-field)))) + ;; Get field in open last column. + (should + (equal " a " + (org-test-with-temp-text "| a " (org-table-get-field)))) + ;; Get empty field. + (should + (equal "" + (org-test-with-temp-text "||" (org-table-get-field)))) + (should + (equal " " + (org-test-with-temp-text "| |" (org-table-get-field)))) + ;; Outside the table, return the empty string. + (should + (equal "" + (org-test-with-temp-text "| a |" (org-table-get-field)))) + (should + (equal "" + (org-test-with-temp-text "| a |" (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| 3 | 4 |" + (org-table-get-field 2)))) + ;; REPLACE optional argument is used to replace selected field. + (should + (equal "| foo |" + (org-test-with-temp-text "| 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| 3 | 4 |" + (org-table-get-field 2)))) + ;; An empty REPLACE string clears the field. + (should + (equal "| |" + (org-test-with-temp-text "| 1 |" + (org-table-get-field nil "") + (buffer-string)))) + ;; When using REPLACE still return old value. + (should + (equal " 1 " + (org-test-with-temp-text "| 1 |" + (org-table-get-field nil " foo "))))) + (provide 'test-org-table) ;;; test-org-table.el ends here -- 2.11.4.GIT