1

I am writing long, descriptive text above a line of code and would like to format it into comment lines of a specified max width, while not splitting text mid-word. How can I do this?

For example, I have written several paragraphs of comments above a line of code:

This is the beginning of several long paragraphs about method().
This is the second paragraph.
The third paragraph - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
method()

assuming a desired max width of 80 characters, and a comment syntax of '//', I would like an emacs command to format selected text into:

// This is the beginning of several long paragraphs about method().
// This is the second paragraph.
// The third paragraph - Lorem ipsum dolor sit amet, consectetur adipiscing
// elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
// enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
// aliquip ex ea commodo consequat.
method()

The comment syntax is not as important as the line-splitting part, but I included it for context in case there's a solution that covers my use-case completely.

It would also be nice to have a way to reverse this process, so that I can edit my descriptive comment and then re-apply the command to format again

mark
  • 293
  • 1
  • 9
  • Given that you've stated in other comments that you want the pre-existing newlines to be maintained, "reversing the process" is non-trivial (other than undo), as with the additional newlines added to the text you would need to differentiate those newlines from the pre-existing newlines in order to reverse the process. Emacs can do that (e.g. text properties could be added to certain newline characters), but it's a complication. – phils Mar 14 '23 at 03:05

1 Answers1

0

fill-paragraph, bound to M-q by default.


See the doc: C-h f fill-paragraph, which tells you this, among other things:

The variable fill-column controls the width for filling.


You can also adjust the fill-column on the fly. Simply point to a character at the column of your choice, and type C-xf (M-x set-fill-column). Emacs will suggest the column number, which you should simply approve, and henceforth (for this editing session) that will be the fill column.

Sam7919
  • 369
  • 1
  • 12
db48x
  • 17,977
  • 1
  • 22
  • 28
  • fill-paragraph did something which I don't want: it put a portion of the 2nd paragraph onto the 1st line, and similarly put a portion of the 3rd paragraph onto the end of the 2nd line. I want to preserve any line breaks that I had originally – mark Oct 15 '22 at 02:53
  • Yes, fill-paragraph regards an empty line as the paragraph boundary. – db48x Oct 15 '22 at 02:56
  • 1
    how can I make fill-paragraph treat my example as 3 paragraphs? it looks like some modification of paragraph-start or paragraph-separate may do it – mark Oct 15 '22 at 03:06
  • I suggest using auto-fill-mode when you write the text. This will break lines automatically, but will not automatically re-fill the rest of the text. See C-h i g (emacs)Auto Fill – phils Mar 14 '23 at 02:55
  • For pre-existing text, I suggest you mark the specific line(s) you want to fill before typing M-q and Emacs will fill only that region. Keyboard macros might be helpful in conjunction. – phils Mar 14 '23 at 03:10