Skip to content

Commit

Permalink
Fix beginning and end of defun functions
Browse files Browse the repository at this point in the history
This should fix GH-197.
  • Loading branch information
jrblevin committed Jun 13, 2017
1 parent fba4581 commit c458a96
Showing 1 changed file with 51 additions and 22 deletions.
73 changes: 51 additions & 22 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -6044,29 +6044,58 @@ a list."

(defun markdown-beginning-of-defun (&optional arg)
"`beginning-of-defun-function' for Markdown.
Move backward to the beginning of the current or previous section.
When ARG is non-nil, repeat that many times. When ARG is negative,
move forward to the ARG-th following section."
(interactive "P")
(or arg (setq arg 1))
(forward-char 1)
(or (re-search-backward markdown-regex-header nil t arg)
(goto-char (point-min))))

(defun markdown-end-of-defun (&optional arg)
"`end-of-defun-function' for Markdown.
Move forward to the end of the current or following section.
When ARG is non-nil, repeat that many times. When ARG is negative,
move back to the ARG-th preceding section."
(interactive "P")
This is used to find the beginning of the defun and should behave
like ‘beginning-of-defun’, returning non-nil if it found the
beginning of a defun. It moves the point backward, right before a
heading which defines a defun. When ARG is non-nil, repeat that
many times. When ARG is negative, move forward to the ARG-th
following section."
(or arg (setq arg 1))
(when (looking-at markdown-regex-header)
(goto-char (match-beginning 0))
(forward-char 1))
(if (re-search-forward markdown-regex-header nil t arg)
(goto-char (match-beginning 0))
(goto-char (point-max)))
(skip-syntax-backward "-"))
(when (< arg 0) (end-of-line))
;; Adjust position for setext headings.
(when (and (thing-at-point-looking-at markdown-regex-header-setext)
(not (= (point) (match-beginning 0)))
(not (markdown-code-block-at-point-p)))
(goto-char (match-end 0)))
(let (found)
;; Move backward with positive argument.
(while (and (not (bobp)) (> arg 0))
(setq found nil)
(while (and (not found)
(not (bobp))
(re-search-backward markdown-regex-header nil 'move))
(when (not (markdown-code-block-at-pos (match-beginning 0))))
(setq found (match-beginning 0)))
(setq arg (1- arg)))
;; Move forward with negative argument.
(while (and (not (eobp)) (< arg 0))
(setq found nil)
(while (and (not found)
(not (eobp))
(re-search-forward markdown-regex-header nil 'move))
(when (not (markdown-code-block-at-pos (match-beginning 0))))
(setq found (match-beginning 0)))
(setq arg (1+ arg)))
(when found
(beginning-of-line)
t)))

(defun markdown-end-of-defun ()
"`end-of-defun-function’ for Markdown.
This is used to find the end of the defun at point.
It is called with no argument, right after calling ‘beginning-of-defun-raw’,
so it can assume that point is at the beginning of the defun body.
It should move point to the first position after the defun."
(or (eobp) (forward-char 1))
(let (found)
(while (and (not found)
(not (eobp))
(re-search-forward markdown-regex-header nil 'move))
(when (not (markdown-code-block-at-pos (match-beginning 0)))
(setq found (match-beginning 0))))
(when found
(goto-char found)
(skip-syntax-backward "-"))))

(make-obsolete 'markdown-beginning-of-block 'markdown-beginning-of-text-block "v2.2")

Expand Down

0 comments on commit c458a96

Please sign in to comment.