Skip to content

Commit

Permalink
Prevent filling in code blocks
Browse files Browse the repository at this point in the history
Closes GH-169.
  • Loading branch information
jrblevin committed May 11, 2017
1 parent cab7e63 commit af6b6bf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
19 changes: 19 additions & 0 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -6429,6 +6429,23 @@ This is an exact copy of `line-number-at-pos' for use in emacs21."
;; No match
(t nil)))

(defun markdown-fill-paragraph (&optional justify)
"Fill paragraph at or after point.
This function is like \\[fill-paragraph], but it skips Markdown
code blocks. If the point is in a code block, or just before one,
do not fill. Otherwise, call `fill-paragraph' as usual. If
JUSTIFY is non-nil, justify text as well. Since this function
handles filling itself, it always returns t so that
`fill-paragraph' doesn't run."
(interactive "P")
(unless (or (markdown-code-block-at-point-p)
(save-excursion
(back-to-indentation)
(skip-syntax-forward "-")
(markdown-code-block-at-point-p)))
(fill-paragraph justify))
t)

(defun markdown-fill-forward-paragraph-function (&optional arg)
(let* ((arg (or arg 1))
(paragraphs-remaining (forward-paragraph arg))
Expand Down Expand Up @@ -6721,6 +6738,8 @@ or \\[markdown-toggle-inline-images]."
(set (make-local-variable 'end-of-defun-function)
'markdown-end-of-defun)
;; Paragraph filling
(set (make-local-variable 'fill-paragraph-function)
'markdown-fill-paragraph)
(set
;; Should match start of lines that start or separate paragraphs
(make-local-variable 'paragraph-start)
Expand Down
9 changes: 9 additions & 0 deletions tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -3706,6 +3706,15 @@ this is not header line
(fill-paragraph)
(should (looking-at "aaaaaaaaaaaaaaaa\nbbbbbbbbbbbbbbbb")))))

(ert-deftest test-markdown-filling/skip-code-blocks ()
"Test `markdown-fill-paragraph' on code blocks."
(let ((text "test\n\n```\nhello\nworld\n```"))
(markdown-test-string text
(dotimes (n 5)
;; Fill at each line; buffer should not change.
(fill-paragraph)
(should (string-equal (buffer-string) text))))))

;;; Export tests:

(ert-deftest test-markdown-hook/xhtml-standalone ()
Expand Down

0 comments on commit af6b6bf

Please sign in to comment.