Skip to content

Commit

Permalink
Handle reference definitions when filling paragraphs
Browse files Browse the repository at this point in the history
Previously, when fill-paragraph was called (or auto-fill-mode is on)
lines could be broken between the label and URL for a reference
definition if the line is long. Additionally, multiple reference
definitions could become jumpled together into the same paragraph.

Example A before fill-paragraph:
[long-url]: http://a-really-long-url.example.com/...

Example A after fill-paragraph (or autofill):
[long-url]:
http://a-really-long-url.example.com/...

Example B before fill-paragraph:
[label1]: http://short.example.com/
[label2]: http://also-short.example.com

Example B after fill-paragraph:
[label1]: http://short.example.com/ [label2]: http://also-short.example.com
  • Loading branch information
sfreilich authored and jrblevin committed May 16, 2015
1 parent 6bf23ae commit 0c239e2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
51 changes: 41 additions & 10 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -4523,11 +4523,16 @@ This is an exact copy of `line-number-at-pos' for use in emacs21."
(forward-line 0)
(1+ (count-lines start (point))))))

(defun markdown-nobreak-p ()
"Return nil if it is acceptable to break the current line at the point."
;; inside in square brackets (e.g., link anchor text)
(defun markdown-inside-link-text-p ()
"Return nil if not currently within link anchor text."
(looking-back "\\[[^]]*"))

(defun markdown-line-is-reference-definition-p ()
"Return whether the current line is a reference defition."
(save-excursion
(move-beginning-of-line 1)
(looking-at markdown-regex-reference-definition)))

(defun markdown-adaptive-fill-function ()
"Return prefix for filling paragraph or nil if not determined."
(cond
Expand Down Expand Up @@ -4630,10 +4635,29 @@ if ARG is omitted or nil."
(set (make-local-variable 'end-of-defun-function)
'markdown-end-of-defun)
;; Paragraph filling
(set (make-local-variable 'paragraph-start)
"\f\\|[ \t]*$\\|[ \t]*[*+-][ \t]+\\|[ \t]*[0-9]+\\.[ \t]+\\|[ \t]*: ")
(set (make-local-variable 'paragraph-separate)
"\\(?:[ \t\f]*\\|.* \\)$")
(set
; Should match lines that start or seaprate paragraphs
(make-local-variable 'paragraph-start)
(mapconcat 'identity
'(
"\f" ; starts with a literal line-feed
"[ \t\f]*$" ; space-only line
"[ \t]*[*+-][ \t]+" ; unordered list item
"[ \t]*[0-9]+\\.[ \t]+" ; ordered list item
"[ \t]*\\[\\S-*\\]:[ \t]+" ; link ref def
)
"\\|"))
(set
; Should match lines that separate paragraphs without being
; part of any paragraph:
(make-local-variable 'paragraph-separate)
(mapconcat 'identity
'("[ \t\f]*$" ; space-only line
; The following is not ideal, but the Fill customization
; options really only handle paragraph-starting prefixes,
; not paragraph-ending suffixes:
".* $") ; line ending in two spaces
"\\|"))
(set (make-local-variable 'adaptive-fill-first-line-regexp)
"\\`[ \t]*>[ \t]*?\\'")
(set (make-local-variable 'adaptive-fill-function)
Expand All @@ -4647,9 +4671,16 @@ if ARG is omitted or nil."
(setq outline-level 'markdown-outline-level)
;; Cause use of ellipses for invisible text.
(add-to-invisibility-spec '(outline . t))
;; Indentation and filling
(make-local-variable 'fill-nobreak-predicate)
(add-hook 'fill-nobreak-predicate 'markdown-nobreak-p)

;; Inhibiting line-breaking:
;; Separating out each condition into a separate function so that users can
;; override if desired (with remove-hook)
(add-hook 'fill-nobreak-predicate
'markdown-inside-link-text-p :local t)
(add-hook 'fill-nobreak-predicate
'markdown-line-is-reference-definition-p :local t)

;; Indentation
(setq indent-line-function markdown-indent-function)

;; Prepare hooks for XEmacs compatibility
Expand Down
2 changes: 1 addition & 1 deletion tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ TEST_HTML = $(TEST_SRCS:.text=.html)
$(MARKDOWN) < $< > $@

test:
$(EMACS) -batch -l ert -l ../markdown-mode.el -l markdown-test.el \
$(EMACS) -Q -batch -l ert -l ../markdown-mode.el -l markdown-test.el \
-f ert-run-tests-batch-and-exit

html: $(TEST_HTML)
Expand Down
8 changes: 8 additions & 0 deletions tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -2198,6 +2198,14 @@ See `paragraph-separate'."
(fill-paragraph)
(should (string-equal (buffer-string) "The circumference of a circle divided by it's radius is around 3.14."))))

(ert-deftest test-markdown-filling/no-break-link-reference ()
"Shouldn't break line between label and url, or combine two link references."
(let ((str "[label1]: http://long-url.example.com\n[label2]: http://another-long-url.example.com/"))
(markdown-test-string str
(let ((fill-column 15)) ; after end of label, before end of URL
(fill-paragraph)
(should (string-equal (buffer-string) str))))))

;;; Export tests:

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

0 comments on commit 0c239e2

Please sign in to comment.