diff options
author | Nicolas Goaziou <mail@nicolasgoaziou.fr> | 2017-08-02 10:35:57 +0200 |
---|---|---|
committer | Nicolas Goaziou <mail@nicolasgoaziou.fr> | 2017-08-02 10:35:57 +0200 |
commit | b0776e39b5c2106d0775305c2a6765b293118a86 (patch) | |
tree | 6da3ef1c43a0c9f62009763758cd58c2b402202b | |
parent | 29d8d407e8967e90ba34a6081e799b32aae42864 (diff) | |
download | org-mode-b0776e39b5c2106d0775305c2a6765b293118a86.tar.gz |
org-table: Small refactoring
* lisp/org-table.el (org-table-previous-field): Small refactoring.
* testing/lisp/test-org-table.el (test-org-table/previous-field): New
test.
-rw-r--r-- | lisp/org-table.el | 31 | ||||
-rw-r--r-- | testing/lisp/test-org-table.el | 39 |
2 files changed, 56 insertions, 14 deletions
diff --git a/lisp/org-table.el b/lisp/org-table.el index 37e40de..ff2cce5 100644 --- a/lisp/org-table.el +++ b/lisp/org-table.el @@ -1031,20 +1031,23 @@ Before doing so, re-align the table if necessary." (interactive) (org-table-justify-field-maybe) (org-table-maybe-recalculate-line) - (if (and org-table-automatic-realign - org-table-may-need-update) - (org-table-align)) - (if (org-at-table-hline-p) - (end-of-line 1)) - (condition-case nil - (progn - (re-search-backward "|" (org-table-begin)) - (re-search-backward "|" (org-table-begin))) - (error (user-error "Cannot move to previous table field"))) - (while (looking-at "|\\(-\\|[ \t]*$\\)") - (re-search-backward "|" (org-table-begin))) - (if (looking-at "| ?") - (goto-char (match-end 0)))) + (when (and org-table-automatic-realign + org-table-may-need-update) + (org-table-align)) + (when (org-at-table-hline-p) + (end-of-line)) + (let ((start (org-table-begin)) + (origin (point))) + (condition-case nil + (progn + (search-backward "|" start nil 2) + (while (looking-at-p "|\\(?:-\\|[ \t]*$\\)") + (search-backward "|" start))) + (error + (goto-char origin) + (user-error "Cannot move to previous table field")))) + (when (looking-at "| ?") + (goto-char (match-end 0)))) (defun org-table-beginning-of-field (&optional n) "Move to the beginning of the current table field. diff --git a/testing/lisp/test-org-table.el b/testing/lisp/test-org-table.el index 99f593c..c5e34b5 100644 --- a/testing/lisp/test-org-table.el +++ b/testing/lisp/test-org-table.el @@ -2100,6 +2100,45 @@ is t, then new columns should be added as needed" (let ((org-table-tab-jumps-over-hlines nil)) (org-table-next-field)) (buffer-string))))) +(ert-deftest test-org-table/previous-field () + "Test `org-table-previous-field' specifications." + ;; Regular tests. + (should + (eq ?a + (org-test-with-temp-text "| a | <point>b |" + (org-table-previous-field) + (char-after)))) + (should + (eq ?a + (org-test-with-temp-text "| a |\n| <point>b |" + (org-table-previous-field) + (char-after)))) + ;; Find previous field across horizontal rules. + (should + (eq ?a + (org-test-with-temp-text "| a |\n|---|\n| <point>b |" + (org-table-previous-field) + (char-after)))) + ;; When called on a horizontal rule, find previous data field. + (should + (eq ?b + (org-test-with-temp-text "| a | b |\n|---+-<point>--|" + (org-table-previous-field) + (char-after)))) + ;; Error when at first field. Make sure to preserve original + ;; position. + (should-error + (org-test-with-temp-text "| <point> a|" + (org-table-previous-field))) + (should-error + (org-test-with-temp-text "|---|\n| <point>a |" + (org-table-previous-field))) + (should + (eq ?a + (org-test-with-temp-text "|---|\n| <point>a |" + (ignore-errors (org-table-previous-field)) + (char-after))))) + ;;; Moving rows, moving columns |