Browse Source

org-table: Fix inserting a new row

* lisp/org-table.el (org-table-insert-row): Fix inserting a new row when
  the buffer doesn't end with a newline character.  Tiny refactoring.

* testing/lisp/test-org-table.el (test-org-table/next-field): New test.
Nicolas Goaziou 3 years ago
parent
commit
2b22d503e1
2 changed files with 43 additions and 6 deletions
  1. 8 6
      lisp/org-table.el
  2. 35 0
      testing/lisp/test-org-table.el

+ 8 - 6
lisp/org-table.el

@@ -1556,19 +1556,21 @@ non-nil, the one above is used."
   "Insert a new row above the current line into the table.
 With prefix ARG, insert below the current line."
   (interactive "P")
-  (if (not (org-at-table-p))
-      (user-error "Not at a table"))
-  (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
+  (unless (org-at-table-p) (user-error "Not at a table"))
+  (let* ((line (buffer-substring (line-beginning-position) (line-end-position)))
 	 (new (org-table-clean-line line)))
     ;; Fix the first field if necessary
     (if (string-match "^[ \t]*| *[#$] *|" line)
 	(setq new (replace-match (match-string 0 line) t t new)))
     (beginning-of-line (if arg 2 1))
+    ;; Buffer may not end of a newline character, so ensure
+    ;; (beginning-of-line 2) moves point to a new line.
+    (unless (bolp) (insert "\n"))
     (let (org-table-may-need-update) (insert-before-markers new "\n"))
     (beginning-of-line 0)
-    (re-search-forward "| ?" (point-at-eol) t)
-    (and (or org-table-may-need-update org-table-overlay-coordinates)
-	 (org-table-align))
+    (re-search-forward "| ?" (line-end-position) t)
+    (when (or org-table-may-need-update org-table-overlay-coordinates)
+      (org-table-align))
     (when (or (not org-table-fix-formulas-confirm)
 	      (funcall org-table-fix-formulas-confirm "Fix formulas? "))
       (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1))))

+ 35 - 0
testing/lisp/test-org-table.el

@@ -2036,6 +2036,41 @@ is t, then new columns should be added as needed"
       (org-table-calc-current-TBLFM)
       (buffer-string)))))
 
+
+;;; Navigation
+
+(ert-deftest test-org-table/next-field ()
+  "Test `org-table-next-field' specifications."
+  ;; Regular test.
+  (should
+   (equal
+    "b"
+    (org-test-with-temp-text "| a<point> | b |"
+      (org-table-next-field)
+      (org-trim (org-table-get-field)))))
+  ;; Create new rows as needed.
+  (should
+   (equal
+    "| a |\n|   |\n"
+    (org-test-with-temp-text "| a<point> |"
+      (org-table-next-field)
+      (buffer-string))))
+  ;; Jump over hlines, if `org-table-tab-jumps-over-hlines' is
+  ;; non-nil.
+  (should
+   (equal
+    "b"
+    (org-test-with-temp-text "| a<point> |\n|---|\n| b |"
+      (let ((org-table-tab-jumps-over-hlines t)) (org-table-next-field))
+      (org-trim (org-table-get-field)))))
+  ;; If `org-table-tab-jumps-over-hlines' is nil, however, create
+  ;; a new row before the rule.
+  (should
+   (equal
+    "| a |\n|   |\n|---|\n| b |"
+    (org-test-with-temp-text "| a<point> |\n|---|\n| b |"
+      (let ((org-table-tab-jumps-over-hlines nil)) (org-table-next-field))
+      (buffer-string)))))
 
 (provide 'test-org-table)