From af6b6bff816730230e00ee426edd3778c463af20 Mon Sep 17 00:00:00 2001 From: Jason Blevins Date: Thu, 11 May 2017 02:11:20 -0400 Subject: [PATCH] Prevent filling in code blocks Closes GH-169. --- markdown-mode.el | 19 +++++++++++++++++++ tests/markdown-test.el | 9 +++++++++ 2 files changed, 28 insertions(+) diff --git a/markdown-mode.el b/markdown-mode.el index 1f8fffd..f35033e 100644 --- a/markdown-mode.el +++ b/markdown-mode.el @@ -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)) @@ -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) diff --git a/tests/markdown-test.el b/tests/markdown-test.el index 52ca146..a8b8cb0 100644 --- a/tests/markdown-test.el +++ b/tests/markdown-test.el @@ -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 () -- 2.11.4.GIT