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

Weird filling behavior for paragraphs containing links #173

Closed
phst opened this issue Oct 16, 2016 · 11 comments
Closed

Weird filling behavior for paragraphs containing links #173

phst opened this issue Oct 16, 2016 · 11 comments

Comments

@phst
Copy link
Contributor

phst commented Oct 16, 2016

This is for version 20160928.932.

To reproduce, create a new buffer in markdown-mode, disable auto-fill-mode, and set fill-column to 79. (The latter two aren't necessary, they just make the reproduction more obvious.) Then, insert the following as a single line:

aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa [aaa aaa aaa aaa](aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) aaa aaa aaa aaa aaa.

The "words" themselves don't matter, it's only important that there is a lengthy paragraph with a link with a long URL. Then hit M-q to fill the paragraph. The result is

aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa
aaa [aaa aaa aaa aaa](aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) aaa aaa aaa aaa
aaa.

Note that there's a single "aaa" in front of the link, even though it would have fit on the previous line.

@jrblevin
Copy link
Owner

This seems to have been fixed already. I added your example as a test case to prevent future regressions. Thanks for the report.

@phst
Copy link
Contributor Author

phst commented Jun 29, 2017

Thanks, but I think this isn't fixed yet if the fill column is set to 79 (this does seem to be necessary after all).

@phst
Copy link
Contributor Author

phst commented Jun 29, 2017

Another case is e.g.

aaa [aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa](a) aaa.

with a fill column of 40. Filling that paragraph should introduce line breaks inside the link text, but doesn't.

@jrblevin jrblevin reopened this Jul 2, 2017
@jrblevin
Copy link
Owner

jrblevin commented Jul 2, 2017

Thanks. I missed the point about setting the fill-column to 79.

Indeed, markdown-mode inhibits line breaks inside links:

(add-hook 'fill-nobreak-predicate
          'markdown-inside-link-p nil t)

I think there is a good reason why, but I'll have to check to refresh my memory.

@jrblevin
Copy link
Owner

jrblevin commented Jul 2, 2017

Looks like this originated in 0c239e2, presumably as an aesthetic preference. I'll work on making this behavior customizable, but as a temporary workaround you can write a function that runs with markdown-mode-hook to remove the hook:

(remove-hook 'fill-nobreak-predicate 'markdown-inside-link-p t)

@phst
Copy link
Contributor Author

phst commented Jul 2, 2017

I think 0c239e2 is for a different case, where filling could really change semantics. The original commit is 66566a5, with the explanation "maruku does not like it when you do that". No idea whether that's still current or whether maruku has been fixed.

@jrblevin
Copy link
Owner

jrblevin commented Jul 2, 2017

Thanks for finding the original commit. I remember that now (nearly 8 years ago!)

It seems the latest Maruku correctly parses links with line breaks in the text (see here), so I will revert the non-breaking behavior.

@phst
Copy link
Contributor Author

phst commented Jul 20, 2017

I've been using the workaround in #173 (comment) for a couple of days now and haven't encountered any issues.

@jrblevin
Copy link
Owner

jrblevin commented Aug 2, 2017

Thanks again for the report.

@phst
Copy link
Contributor Author

phst commented Aug 2, 2017

Thanks for the fix!

@xenophonf
Copy link

xenophonf commented Jun 22, 2024

@jrblevin sorry to necro this, but I wanted to put my solution to stopping line wrapping Markdown links here. Given the following Markdown:

This is a [very long link](https://www.example.com/very/very/very/very/long).

This is a [ridiculously long link](https://www.example.com/very/very/very/very/very/very/very/long) with stuff coming after.

- This is a [ridiculously long link in a list](https://www.example.com/very/very/very/very/very/very/very/long) with stuff coming after.

And the following code in my .emacs:

(defun mxe:markdown-mode-install-fill-nobreak-predicates ()
  "These hooks are mode-specific and must be buffer-local as a                                                                                                                               
result."
  (add-hook 'fill-nobreak-predicate
            'markdown-inside-link-p nil t))

(with-eval-after-load 'markdown-mode
  (add-hook 'markdown-mode-hook #'mxe:markdown-mode-install-fill-nobreak-predicates))

The fill-paragraph command reformats paragraphs with long links like so:

This is
a [very long link](https://www.example.com/very/very/very/very/long).

This is
a
[ridiculously long link](https://www.example.com/very/very/very/very/very/very/very/long) with
stuff coming after.

- This is
  a
  [ridiculously long link in a list](https://www.example.com/very/very/very/very/very/very/very/long) with
  stuff coming after.

I'd prefer space within links be treated as non-breakable while allowing breaks before and after:

(defun mxe:markdown-link-fill-nobreak ()
  "Do not break lines in the middle of Markdown links when filling                                                                                                                           
paragraphs.  This function is designed for use in                                                                                                                                            
`fill-nobreak-predicate'.                                                                                                                                                                    
                                                                                                                                                                                             
    [A Markdown link](https://www.example.com/)                                                                                                                                              
    ^                                         ^                                                                                                                                              
    |--- breaks allowed before, after here ---|"
  ;; TODO: memoize this somehow                                                                                                                                                              
  (let ((pos (point)))
    (cl-destructuring-bind (start end text link ref title bang)
        (markdown-link-at-pos pos)
      (and
       (or start end text link ref title bang)
       (< start pos end)))))

(defun mxe:markdown-mode-install-fill-nobreak-predicates ()
  "These hooks are mode-specific and must be buffer-local as a                                                                                                                               
result."
  (add-hook 'fill-nobreak-predicate
            #'mxe:markdown-link-fill-nobreak nil t))

(with-eval-after-load 'markdown-mode
  (add-hook 'markdown-mode-hook #'mxe:markdown-mode-install-fill-nobreak-predicates))

Now long links get placed on their own line. Filled paragraphs look neater as a result.

This is a
[very long link](https://www.example.com/very/very/very/very/long).

This is a
[ridiculously long link](https://www.example.com/very/very/very/very/very/very/very/long)
with stuff coming after.
  
- This is a
  [ridiculously long link in a list](https://www.example.com/very/very/very/very/very/very/very/long)
  with stuff coming after.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants