Skip to content

Commit

Permalink
Use correct marker from previous list level
Browse files Browse the repository at this point in the history
Putting the point after "AAA" in the following sample and pressing `C-u`
`M-RET` should insert "*" (properly dedented) instead of "1.".

```
1. A
    * AA
        1. AAA
```

It should produce:

```
1. A
    * AA
        1. AAA
    *
```

Previously, it produced this instead:

```
1. A
    * AA
        1. AAA
    1.
```

This commit changes the behavior of `(markdown-prev-list-item level)`.
It now stops at list items with (non-list-marker) indentation at or
*below* `level`. In the example above, the (non-list-marker) indentation
of "AAA" is 11. Subtracting 4 gives 7, but the corresponding indentation
of the previous item "AA" is only 6. So, when mixing list types it does
not suffice to simply subtract 4 and look for equal indentation.
Instead, we subtract 1 and find the previous item with *lesser than or
equal* indentation.

Closes #4
  • Loading branch information
jrblevin committed Dec 19, 2015
1 parent acf7c5e commit a648097
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
42 changes: 24 additions & 18 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -2059,24 +2059,20 @@ upon failure."
(setq indent (markdown-cur-line-indent))
(while
(cond
;; Stop at beginning of buffer
((bobp) (setq prev nil))
;; Continue if current line is blank
((markdown-cur-line-blank-p) t)
;; List item
((and (looking-at markdown-regex-list)
(setq bounds (markdown-cur-list-item-bounds)))
(cond
;; Continue at item with greater indentation
((> (nth 3 bounds) level) t)
;; Stop and return point at item of equal indentation
((= (nth 3 bounds) level)
;; Stop and return point at item of lesser or equal indentation
((<= (nth 3 bounds) level)
(setq prev (point))
nil)
;; Stop and return nil at item with lesser indentation
((< (nth 3 bounds) level)
(setq prev nil)
nil)))
;; Stop at beginning of buffer
((bobp) (setq prev nil))
;; Continue if current line is blank
((markdown-cur-line-blank-p) t)
;; Continue while indentation is the same or greater
((>= indent level) t)
;; Stop if current indentation is less than list item
Expand Down Expand Up @@ -4243,7 +4239,7 @@ decrease the indentation by one level.
With two \\[universal-argument] prefixes (i.e., when ARG is (16)),
increase the indentation by one level."
(interactive "p")
(let (bounds item-indent marker indent new-indent new-loc)
(let (bounds cur-indent marker indent new-indent new-loc)
(save-match-data
;; Look for a list item on current or previous non-blank line
(save-excursion
Expand Down Expand Up @@ -4279,13 +4275,23 @@ increase the indentation by one level."
(unless (markdown-cur-line-blank-p)
(insert "\n"))
(insert markdown-unordered-list-item-prefix))
;; Compute indentation for a new list item
(setq item-indent (nth 2 bounds))
;; Compute indentation and marker for new list item
(setq cur-indent (nth 2 bounds))
(setq marker (nth 4 bounds))
(setq indent (cond
((= arg 4) (max (- item-indent 4) 0))
((= arg 16) (+ item-indent 4))
(t item-indent)))
(cond
;; Dedent: decrement indentation, find previous marker.
((= arg 4)
(setq indent (max (- cur-indent 4) 0))
(let ((prev-bounds
(save-excursion
(when (markdown-prev-list-item (- (nth 3 bounds) 1))
(markdown-cur-list-item-bounds)))))
(when prev-bounds
(setq marker (nth 4 prev-bounds)))))
;; Indent: increment indentation by 4, use same marker.
((= arg 16) (setq indent (+ cur-indent 4)))
;; Same level: keep current indentation and marker.
(t (setq indent cur-indent)))
(setq new-indent (make-string indent 32))
(goto-char new-loc)
(cond
Expand All @@ -4294,7 +4300,7 @@ increase the indentation by one level."
(if (= arg 16) ;; starting a new column indented one more level
(insert (concat new-indent "1. "))
;; travel up to the last item and pick the correct number. If
;; the argument was nil, "new-indent = item-indent" is the same,
;; the argument was nil, "new-indent = cur-indent" is the same,
;; so we don't need special treatment. Neat.
(save-excursion
(while (and (not (looking-at (concat new-indent "\\([0-9]+\\)\\(\\.[ \t]*\\)")))
Expand Down
20 changes: 20 additions & 0 deletions tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,26 @@ Test point position upon removal and insertion."
(call-interactively 'markdown-insert-list-item)
(should (string-equal (buffer-string) "9.\tfoo\n10.\t"))))

(ert-deftest test-markdown-insertion/nested-list-marker ()
"Test marker detection for `markdown-insert-list-item'."
(markdown-test-string
"1. A\n * AA\n 1. AAA"
(goto-char (point-max))
(let ((current-prefix-arg '(4)))
(call-interactively 'markdown-insert-list-item))
(should (eq (point) 36))
(should (looking-back "\* "))
(should (string-equal
(buffer-string)
"1. A\n * AA\n 1. AAA\n * "))
(let ((current-prefix-arg '(4)))
(call-interactively 'markdown-insert-list-item))
(should (eq (point) 40))
(should (looking-back "2\. "))
(should (string-equal
(buffer-string)
"1. A\n * AA\n 1. AAA\n * \n2. "))))

(ert-deftest test-markdown-insertion/reference-link ()
"Basic tests for `markdown-insert-reference-link'."
;; Test optional parameters (leave point after link)
Expand Down

0 comments on commit a648097

Please sign in to comment.