Handling Comments When Joining Lines

Tony Zorman has an interesting post on joining lines in the presence of comments. If you’re like me, your first reaction is apt to be, “why should I care about this?” After a second’s thought, though, I realized that joining lines is something I occasionally have to do when I start writing an Irreal post in the wrong directory where visual-line-mode does not get set. Once I realize my mistake, I want to move the text to the proper directory and join the lines in each paragraph.

It turns out, of course, that there’s an Emacs command for that: join-line. I’ve always solved the problem by setting the line length to a large value and reformatting the buffer. Using join-line is a much better solution.

For me, that’s the end of the story but Zorman wants to use join-line in programming buffers and it turns out that join-line doesn’t care about or do anything special for comments. That means that

;; Several comments
;; in a row
;; like this

gets concatenated into a single line, which is definitely not what you want.

The majority of Zorman’s post details his solution to this. For comments that start with a single character, the solution is easy. A simple advice-add does the trick. For languages like Lisp, though the solution is harder because the convention is to start whole line comments with two or more ; characters.

For that case, Zorman has to modify the source code of the function directly. The final solution covers all the cases that Zorman cares about and you may find it useful too. Take a look at his post for the details.

This entry was posted in General and tagged . Bookmark the permalink.