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
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