tags:

views:

386

answers:

4

Say you write some code like this (using ruby-mode, but I've seen this happen in other modes too):

# This is a comment.
def foo
    puts "foo!"
end

If you put the point on the first line and hit M-q, you get this:

# This is a comment. def foo puts "foo!" end

How do I avoid that? I'm using version 21.3.

Clarification: This does not happen when I add a blank line between the comment and the code. As a work-around when I want to refill my comments, I go through an annoying three step process:

  1. I add a blank line before and after the comment paragraph
  2. M-q
  3. delete the blank lines

It'd be much nicer if M-q handled refilling comment paragraphs without having to add and delete blank lines. Emacs already know what text is comment text, so there must be a way to do this.

+1  A: 

M-q is bound to fill-paragraph, what it is doing is trying to do is intelligently turn the text into a paragraph. It has features that try to guess the 'fill-prefix, which is what appears to be happening to you.

You can unbind M-q if you don't like it.

(global-unset-key (kbd "M-q"))
Trey Jackson
I like M-q, but it behaves weirdly when I use it on comments that are not separated from code by a blank line.
allyourcode
Heh - there's a new answer for you.
Trey Jackson
A: 

You need to be in Ruby mode, so that Emacs will understand that “# This is a comment.” is a comment. If you are in fundamental mode, it will just treat everything as a text paragraph, which makes it think that the text on the next line is part of the same paragraph.

Here are some instructions for installing Ruby mode if you don't already have it.

Brian Campbell
That happens even when I'm in Ruby mode.
allyourcode
+1  A: 

Filling comments works in sh-mode.

Perhaps you should submit a bug to the ruby-mode maintainer?

ashawley
I tried the same thing in sh-mode and got the same result :( .
allyourcode
Bummer. I'm in Emacs 22.2.
ashawley
+4  A: 

filladapt.el does the trick. That with the latest version of RubyMode.

Using those two packages solves the M-q problem you're seeing. (Using GNU Emacs 22.1)

Looking at the code for ruby-mode, it looks like it has customized the variables to control paragraph filling like so:

(make-local-variable 'paragraph-start)
(setq paragraph-start (concat "$\\|" page-delimiter))
(make-local-variable 'paragraph-separate)
(setq paragraph-separate paragraph-start)
(make-local-variable 'paragraph-ignore-fill-prefix)
(setq paragraph-ignore-fill-prefix t)

Which can be added to a custom hook for your current ruby, or whatever major mode where you want fill behavior to act as you described - provided you use filladapt.el.

Trey Jackson