Browse Source

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.
Nicolas Goaziou 8 years ago
parent
commit
73330079c0
2 changed files with 77 additions and 14 deletions
  1. 14 14
      lisp/org-table.el
  2. 63 0
      testing/lisp/test-org-table.el

+ 14 - 14
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)

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