Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix moving same level header over code block issue #77

Merged
merged 2 commits into from
Jan 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -4852,11 +4852,38 @@ Stop at the first and last headings of a superior heading."
(interactive "p")
(markdown-move-heading-common 'outline-forward-same-level arg))

(defun markdown-back-to-heading-over-code-block (&optional invisible-ok)
(beginning-of-line)
(or (and (outline-on-heading-p invisible-ok)
(not (markdown-code-block-at-point)))
(let ((header-re (concat "^\\(?:" outline-regexp "\\)"))
found)
(save-excursion
(while (not found)
(let (finish)
(while (and (not finish) (re-search-backward header-re nil t))
(when (and (or invisible-ok (not (outline-invisible-p)))
(not (markdown-code-block-at-point)))
(setq finish t)))
(if (not finish)
(error "Before first heading")
(setq found (point))))))
(goto-char found)
found)))

(defun markdown-backward-same-level (arg)
"Move backward to the ARG'th heading at same level as this one.
Stop at the first and last headings of a superior heading."
(interactive "p")
(markdown-move-heading-common 'outline-backward-same-level arg))
(markdown-back-to-heading-over-code-block)
(while (> arg 0)
(let ((point-to-move-to (save-excursion
(outline-get-last-sibling))))
(if point-to-move-to
(progn
(goto-char point-to-move-to)
(setq arg (1- arg)))
(error "No previous same-level heading")))))

(defun markdown-up-heading (arg)
"Move to the visible heading line of which the present line is a subheading.
Expand Down
32 changes: 32 additions & 0 deletions tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -2847,6 +2847,38 @@ returns nil."
(should (= (point) 13))
(should (string-equal (buffer-string) "[a][]\n\n[a]: "))))

(ert-deftest test-markdown-movement/back-to-same-level-over-code-block ()
"`markdown-backward-same-level' over code block which contains header
like statement. Detail: https://github.com/jrblevin/markdown-mode/issues/75"
(markdown-test-string "
## Header 2-1

## Header 2-2

```R
# Header Like Statement
```

## Header 2-3
"
(search-forward "## Header 2-3")
(let ((last-header-pos (point)))
(forward-line -1)
(call-interactively #'markdown-backward-same-level)
(should (looking-at-p "## Header 2-1"))

(goto-char last-header-pos)
(call-interactively #'markdown-backward-same-level)
(should (looking-at-p "## Header 2-2"))

(goto-char last-header-pos)
(markdown-backward-same-level 2)
(should (looking-at-p "## Header 2-1"))

(search-forward "# Header Like Statement")
(call-interactively #'markdown-backward-same-level)
(should (looking-at-p "## Header 2-1")))))

;;; Wiki link tests:

(ert-deftest test-markdown-wiki-link/aliasing ()
Expand Down