Browse Source

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.
Nicolas Goaziou 2 years ago
parent
commit
b0776e39b5
2 changed files with 56 additions and 14 deletions
  1. 17 14
      lisp/org-table.el
  2. 39 0
      testing/lisp/test-org-table.el

+ 17 - 14
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.

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