summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Goaziou <mail@nicolasgoaziou.fr>2017-02-01 21:02:48 +0100
committerNicolas Goaziou <mail@nicolasgoaziou.fr>2017-02-01 21:02:48 +0100
commit86efe9248454780daeeda7be960b37405744f032 (patch)
tree80ebb377f91b3255877943462c2f786164556e8c
parent13b18ea5e6886d0c79ec4d6ff50147cffad3fb08 (diff)
parent73330079c0ef4d7294500155a8df061398915e7b (diff)
downloadorg-mode-86efe9248454780daeeda7be960b37405744f032.tar.gz
Merge branch 'maint'
-rw-r--r--lisp/org-table.el28
-rw-r--r--testing/lisp/test-org-table.el63
2 files changed, 77 insertions, 14 deletions
diff --git a/lisp/org-table.el b/lisp/org-table.el
index 3754783..b7a49f3 100644
--- a/lisp/org-table.el
+++ b/lisp/org-table.el
@@ -1251,21 +1251,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 b2bc483..7059364 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