From ed3c971534283b9af2f13e71bf975ed448f2c225 Mon Sep 17 00:00:00 2001 From: Stephen Berman Date: Thu, 27 Aug 2020 13:53:22 +0200 Subject: [PATCH] Prevent spurious tabs by RET in todo-edit-mode (bug#43068) * lisp/calendar/todo-mode.el (todo-key-bindings-t) (todo-edit-mode-map): Remove remapping of `newline' to `newline-and-indent'. (todo-modes-set-1): Remove local setting of `indent-line-function'. (todo-edit-mode): Locally set `indent-line-function' to `todo-indent'. * test/lisp/calendar/todo-mode-tests.el (todo-test-move-item05): Prevent interactive test failure. (Until the addition of testcat4 to todo-test-1.todo, the test passed by chance, since testcat3 is empty and has no archived items.) (todo-test-edit-item-date-month): Refer to bug number. (todo-test-multiline-item-indentation-1) (todo-test-multiline-item-indentation-2) (todo-test-multiline-item-indentation-3): New tests. * test/lisp/calendar/todo-mode-resources/todo-test-1.todo: Remove spurious tabs from testcat1. --- lisp/calendar/todo-mode.el | 6 +- .../todo-mode-resources/todo-test-1.todo | 4 +- test/lisp/calendar/todo-mode-tests.el | 59 +++++++++++++++++-- 3 files changed, 58 insertions(+), 11 deletions(-) diff --git a/lisp/calendar/todo-mode.el b/lisp/calendar/todo-mode.el index 4f513d33865..0e4446f77de 100644 --- a/lisp/calendar/todo-mode.el +++ b/lisp/calendar/todo-mode.el @@ -6440,8 +6440,7 @@ Filtered Items mode following todo (not done) items." ("i" todo-insert-item) ("k" todo-delete-item) ("m" todo-move-item) - ("u" todo-item-undone) - ([remap newline] newline-and-indent)) + ("u" todo-item-undone)) "List of key bindings for Todo mode only.") (defvar todo-key-bindings-t+a+f @@ -6507,7 +6506,6 @@ Filtered Items mode following todo (not done) items." (defvar todo-edit-mode-map (let ((map (make-sparse-keymap))) (define-key map "\C-x\C-q" 'todo-edit-quit) - (define-key map [remap newline] 'newline-and-indent) map) "Todo Edit mode keymap.") @@ -6666,7 +6664,6 @@ Added to `window-configuration-change-hook' in Todo mode." (setq-local font-lock-defaults '(todo-font-lock-keywords t)) (setq-local revert-buffer-function #'todo-revert-buffer) (setq-local tab-width todo-indent-to-here) - (setq-local indent-line-function #'todo-indent) (when todo-wrap-lines (visual-line-mode) (setq wrap-prefix (make-string todo-indent-to-here 32)))) @@ -6741,6 +6738,7 @@ Added to `window-configuration-change-hook' in Todo mode." \\{todo-edit-mode-map}" (todo-modes-set-1) + (setq-local indent-line-function #'todo-indent) (if (> (buffer-size) (- (point-max) (point-min))) ;; Editing one item in an indirect buffer, so buffer-file-name is nil. (setq-local todo-current-todo-file todo-global-current-todo-file) diff --git a/test/lisp/calendar/todo-mode-resources/todo-test-1.todo b/test/lisp/calendar/todo-mode-resources/todo-test-1.todo index 557134fd454..2375772fbe7 100644 --- a/test/lisp/calendar/todo-mode-resources/todo-test-1.todo +++ b/test/lisp/calendar/todo-mode-resources/todo-test-1.todo @@ -1,8 +1,8 @@ (("testcat1" . [2 0 2 1]) ("testcat2" . [3 0 1 1]) ("testcat3" . [0 0 0 0]) ("testcat4" . [1 0 0 0])) --==-- testcat1 [May 29, 2017] testcat1 item3 - has more than one line - to test item highlighting + has more than one line + to test item highlighting [Jul 3, 2017] testcat1 item4 ==--== DONE diff --git a/test/lisp/calendar/todo-mode-tests.el b/test/lisp/calendar/todo-mode-tests.el index a19612ee562..1fbd39478c5 100644 --- a/test/lisp/calendar/todo-mode-tests.el +++ b/test/lisp/calendar/todo-mode-tests.el @@ -414,8 +414,15 @@ the top done item should be the first done item." (should (todo-done-item-p)) (forward-line -1) (should (looking-at todo-category-done)) - ;; Make sure marked items are no longer in first category. - (todo-backward-category) + ;; Make sure marked items are no longer in first category. Since + ;; cat1 now contains no todo or done items but does have archived + ;; items, todo-backward-category would skip it by default, so + ;; prevent this. (FIXME: Without this let-binding, + ;; todo-backward-category selects the nonempty cat4 and this test + ;; fails as expected when run interactively but not in a batch + ;; run -- why?) + (let (todo-skip-archived-categories) + (todo-backward-category)) (should (eq (point-min) (point-max))) ; All todo items were moved. ;; This passes when run interactively but fails in a batch run: ;; the message is displayed but (current-message) evaluates to @@ -848,7 +855,7 @@ should display the previously current (or default) todo file." (should (equal todo-current-todo-file todo-test-file-1)) (delete-file (concat file "~"))))) -(ert-deftest todo-test-edit-item-date-month () +(ert-deftest todo-test-edit-item-date-month () ; bug#42976 #3 and #4 "Test incrementing and decrementing the month of an item's date. If the change in month crosses a year boundary, the year of the item's date should be adjusted accordingly." @@ -892,8 +899,50 @@ item's date should be adjusted accordingly." (todo-edit-item--header 'month 25) (should (equal (funcall get-date) "Feb 1, 2022")) (todo-edit-item--header 'month -25) - (should (equal (funcall get-date) "Jan 1, 2020")) - ))) + (should (equal (funcall get-date) "Jan 1, 2020"))))) + +(ert-deftest todo-test-multiline-item-indentation-1 () + "Test inserting a multine item containing a hard line break. +After insertion the second line of the item should begin with a +tab character." + (with-todo-test + (let* ((item0 "Test inserting a multine item") + (item1 "containing a hard line break.") + (item (concat item0 "\n" item1))) + (todo-test--show 1) + (todo-test--insert-item item 1) + (re-search-forward (concat todo-date-string-start todo-date-pattern + (regexp-quote todo-nondiary-end) " ") + (line-end-position) t) + (should (looking-at (regexp-quote (concat item0 "\n\t" item1))))))) + +(ert-deftest todo-test-multiline-item-indentation-2 () ; bug#43068 + "Test editing an item by adding text on a new line. +After quitting todo-edit-mode the second line of the item should +begin with a tab character." + (with-todo-test + (todo-test--show 2) + (let* ((item0 (todo-item-string)) + (item1 "Second line.")) + (todo-edit-item--text 'multiline) + (insert (concat "\n" item1)) + (todo-edit-quit) + (goto-char (line-beginning-position)) + (should (looking-at (regexp-quote (concat item0 "\n\t" item1))))))) + +(ert-deftest todo-test-multiline-item-indentation-3 () + "Test adding an unindented new line to an item using todo-edit-file. +Attempting to quit todo-edit-mode should signal a user-error, +since all non-initial item lines must begin with whitespace." + (with-todo-test + (todo-test--show 2) + (let* ((item0 (todo-item-string)) + (item1 "Second line.")) + (todo-edit-file) + (should (looking-at (regexp-quote item0))) + (goto-char (line-end-position)) + (insert (concat "\n" item1)) + (should-error (todo-edit-quit) :type 'user-error)))) (provide 'todo-mode-tests) ;;; todo-mode-tests.el ends here -- 2.39.2