3

Say I have the following document:

Sir Bedevere
------------
Well, now, uh, Lancelot, Galahad, and I, wait until nightfall, and then leap out of the rabbit, taking the French by surprise - not only by surprise, but totally unarmed!

If I execute M-x fill-paragraph, I get this result:

Sir Bedevere ------------ Well, now, uh, Lancelot, Galahad, and
I, wait until nightfall, and then leap out of the rabbit, taking
the French by surprise - not only by surprise, but totally
unarmed!

However, I would like it to produce this output:

Sir Bedevere
------------
Well, now, uh, Lancelot, Galahad, and I, wait until nightfall,
and then leap out of the rabbit, taking the French by surprise -
not only by surprise, but totally unarmed!

Is there a way to get fill-paragraph to not auto-fill past a horizontal rule like this? Ideally it would work with other rule-like lines (made up of asterisks, octothorpes, etc.).


Edit: Can this be made to work in comments too? In this case, the horizontal rule would appear after the comment symbol(s), like in these examples:

# -----

// **********

/*
 * ##############
 */
  • You can temporarily change the mode of the buffer, eg to text mode (M-x text-mode return), do the edits , save the file, then re-read it to restore the mode. – meuh Mar 21 '17 at 18:40
3

You can add the following lines to your init-file. Additionally to the default setting, they define lines consisting of at least two equal non-alphanumerical characters as paragraph start and delimiter.

(setq paragraph-start "^\\([^[:alnum:]]\\)\\1+\n\\|\f\\|[   ]*$")
(setq paragraph-separate "\\([^[:alnum:]]\\)\\1+\n\\|[  \f]*$")

Alternatively, you can also customize the variables paragraph-start and paragraph-separate with M-x customize-option RET as follows

paragraph-start:

^-[-]+
\|↪\|[  ]*$

paragraph-separate:

-[-]+
\|[     ↪]*$

respectively. In that case use C-q C-j to input a newline and C-q C-l for form feed .

  • What do paragraph-start and paragraph-separate signify? Also, what is the significance of the form feeds? – Ben Mar 21 '17 at 16:57
  • @Ben Please see the help for these variables (key sequence C-h v). You can separate pages with form feeds in emacs text modes. The most important usage for me up to now is the separation of local variables for source code blocks in org-mode buffers (see gnu.org/software/emacs/manual/html_node/emacs/…). – Tobias Mar 21 '17 at 17:13
  • Thanks. Is there a way to get this to work within comments? I have updated my original question with this new request. – Ben Mar 21 '17 at 18:08
  • @Ben Programming language modes most often use their own fill-paragraph-function (see the documentation of that variable). Thus, that feature is most likely to be implemented on a per programming mode basis. – Tobias Mar 21 '17 at 18:14
  • I would love to see an example of how that might work. In the meantime I have adapted your regex to work with comments like so: "^\s*[^[:alnum:]]\\{0,2\\}\s*\\([^[:alnum:]]\\)\\1+\n\\|\f\\|[ ]*$". Definitely not robust, but it works for most situations. – Ben Mar 22 '17 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.