diff options
author | Nicolas Goaziou <mail@nicolasgoaziou.fr> | 2016-09-28 23:33:32 +0200 |
---|---|---|
committer | Nicolas Goaziou <mail@nicolasgoaziou.fr> | 2016-09-28 23:33:32 +0200 |
commit | 2b22d503e1ee83f3a2681964f9de8405438a6385 (patch) | |
tree | f6be29ee6f9229d6d1abfd9c160a572679379f1a | |
parent | ece6e39dfc97346b9f10dcb6ae8003811f56082d (diff) | |
download | org-mode-2b22d503e1ee83f3a2681964f9de8405438a6385.tar.gz |
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.
-rw-r--r-- | lisp/org-table.el | 14 | ||||
-rw-r--r-- | testing/lisp/test-org-table.el | 35 |
2 files changed, 43 insertions, 6 deletions
diff --git a/lisp/org-table.el b/lisp/org-table.el index 3e92223..d928825 100644 --- a/lisp/org-table.el +++ b/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)))) diff --git a/testing/lisp/test-org-table.el b/testing/lisp/test-org-table.el index c4b5a4e..7114c1f 100644 --- a/testing/lisp/test-org-table.el +++ b/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) |