Skip to content

Commit

Permalink
Don't continue list items by default after RET
Browse files Browse the repository at this point in the history
Add two distinct levels to `markdown-indent-on-enter`.
When it equals 'indent, automatically indent the following
line.  When it equals 'indent-and-new-item, indent and also
also continue lists by inserting new list items following RET.
  • Loading branch information
jrblevin committed May 26, 2017
1 parent a520839 commit 9e10d57
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 39 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -554,9 +554,10 @@ provides an interface to all of the possible customizations:
* `markdown-indent-function` - the function to use for automatic
indentation (default: `markdown-indent-line`).

* `markdown-indent-on-enter` - set to a non-nil value to
automatically indent new lines and/or continue lists when the
enter key is pressed (default: `t`)
* `markdown-indent-on-enter` - Set to a non-nil value to
automatically indent new lines when <kbd>RET</kbd> is pressed.
Set to `indent-and-new-item` to additionally continue lists
when <kbd>RET</kbd> is pressed (default: `indent`).

* `markdown-enable-wiki-links` - syntax highlighting for wiki
links (default: `nil`). Set this to a non-nil value to turn on
Expand Down
52 changes: 35 additions & 17 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -572,9 +572,10 @@
;; * `markdown-indent-function' - the function to use for automatic
;; indentation (default: `markdown-indent-line').
;;
;; * `markdown-indent-on-enter' - set to a non-nil value to
;; automatically indent new lines and/or continue lists when the
;; enter key is pressed (default: `t')
;; * `markdown-indent-on-enter' - Set to a non-nil value to
;; automatically indent new lines when `RET' is pressed.
;; Set to `indent-and-new-item' to additionally continue lists
;; when `RET' is pressed (default: `indent').
;;
;; * `markdown-enable-wiki-links' - syntax highlighting for wiki
;; links (default: `nil'). Set this to a non-nil value to turn on
Expand Down Expand Up @@ -966,13 +967,27 @@ line around the header title."
:type 'function)

(defcustom markdown-indent-on-enter t
"Indent new lines and continue lists when enter is pressed.
When this variable is set to t, pressing RET will call
`newline-and-indent' and will continue a list. When set to nil,
define RET to call `newline' as usual. In the latter case, you
can still use auto-indentation by pressing
\\[newline-and-indent] or continue lists with
\\[markdown-insert-list-item]."
"Determines indentation behavior when pressing \\[newline].
Possible settings are nil, t, 'indent, and 'indent-and-new-item.

When non-nil, pressing \\[newline] will call `newline-and-indent'
to indent the following line according to the context using
`markdown-indent-function'. In this case, note that
\\[electric-newline-and-maybe-indent] can still be used to insert
a newline without indentation.

When set to 'indent-and-new-item and the point is in a list item
when \\[newline] is pressed, the list will be continued on the next
line, where a new item will be inserted.

When set to nil, simply call `newline' as usual. In this case,
you can still indent lines using \\[markdown-cycle] and continue
lists with \\[markdown-insert-list-item].

Note that this assumes the variable `electric-indent-mode' is
non-nil (enabled). When it is *disabled*, the behavior of
\\[newline] and `\\[electric-newline-and-maybe-indent]' are
reversed."
:group 'markdown
:type 'boolean)

Expand Down Expand Up @@ -4482,16 +4497,19 @@ duplicate positions, which are handled up by calling functions."
(reverse positions)))

(defun markdown-enter-key ()
"Handle RET according to customized settings.
When `markdown-indent-on-enter' is nil, this is equivalent to
`newline'. Otherwise, indent following RET and when the point is
in a list item, start a new item with the same indentation. If
the point is in an empty list item, remove it."
"Handle RET according to value of `markdown-indent-on-enter'.
When it is nil, simply call `newline'. Otherwise, indent the next line
following RET using `markdown-indent-line'. Furthermore, when it
is set to 'indent-and-new-item and the point is in a list item,
start a new item with the same indentation. If the point is in an
empty list item, remove it (so that pressing RET twice when in a
list simply adds a blank line)."
(interactive)
(if (not markdown-indent-on-enter)
(newline)
(let ((bounds (markdown-cur-list-item-bounds)))
(if bounds
(let (bounds)
(if (and (memq markdown-indent-on-enter '(indent-and-new-item))
(setq bounds (markdown-cur-list-item-bounds)))
(let ((beg (cl-first bounds))
(end (cl-second bounds))
(length (cl-fourth bounds)))
Expand Down
39 changes: 20 additions & 19 deletions tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -1776,27 +1776,27 @@ the opening bracket of [^2], and then subsequent functions would kill [^2])."

(ert-deftest test-markdown-indentation/indent-list-single ()
"Test `markdown-indent-line' with single list item."
(markdown-test-string
" * item 1"
(end-of-line)
(markdown-enter-key)
(should (string-equal (buffer-string) " * item 1\n * "))
(should (eq (point) 16))
(markdown-enter-key)
(should (string-equal (buffer-string) " * item 1\n\n"))
(should (eq (point) 13))))
(let ((markdown-indent-on-enter 'indent-and-new-item))
(markdown-test-string " * item 1"
(end-of-line)
(call-interactively #'markdown-enter-key)
(should (string-equal (buffer-string) " * item 1\n * "))
(should (eq (point) 16))
(call-interactively #'markdown-enter-key)
(should (string-equal (buffer-string) " * item 1\n\n"))
(should (eq (point) 13)))))

(ert-deftest test-markdown-indentation/indent-nested-list ()
"Test `markdown-enter-key' with a nested list item."
(markdown-test-string
"* foo\n* bar\n * baz"
(goto-char (point-max))
(markdown-enter-key)
(should (string-equal (buffer-string) "* foo\n* bar\n * baz\n * "))
(should (eq (point) 25))
(markdown-enter-key)
(should (string-equal (buffer-string) "* foo\n* bar\n * baz\n\n"))
(should (eq (point) 22))))
(let ((markdown-indent-on-enter 'indent-and-new-item))
(markdown-test-string "* foo\n* bar\n * baz"
(goto-char (point-max))
(call-interactively #'markdown-enter-key)
(should (string-equal (buffer-string) "* foo\n* bar\n * baz\n * "))
(should (eq (point) 25))
(call-interactively #'markdown-enter-key)
(should (string-equal (buffer-string) "* foo\n* bar\n * baz\n\n"))
(should (eq (point) 22)))))

(ert-deftest test-markdown-indentation/indent-pre ()
"Test `markdown-indent-line' with a pre block."
Expand All @@ -1812,7 +1812,8 @@ the opening bracket of [^2], and then subsequent functions would kill [^2])."
(ert-deftest test-markdown-indentation/continue-gfm-task-lists ()
(markdown-test-string " - [X] item"
(end-of-line)
(call-interactively #'markdown-enter-key)
(let ((markdown-indent-on-enter 'indent-and-new-item))
(call-interactively #'markdown-enter-key))
(should (string-equal (buffer-string) " - [X] item\n - [ ] "))
(should (= (point) 28))))

Expand Down

0 comments on commit 9e10d57

Please sign in to comment.