Search code examples
rubyemacsruby-mode

Outdent ruby access modifiers in emacs


Can emacs ruby-mode be configured to outdent access modifiers (private, protected, public)? I would like it to outdent them like so:

class MyClass
  def hello; end

private

  def world; end
end

I couldn't find any obvious way to do it in ruby-mode or enhanced-ruby-mode.


Solution

  • If you are using ruby-mode, this would do the trick:

    (defadvice ruby-indent-line (around outdent-modifiers activate)
      (if (save-excursion
            (beginning-of-line)
            (looking-at "\s*\\(private\\|protected\\|public\\)\s*$"))
          (save-excursion
            (beginning-of-line)
            (just-one-space 0))
          ad-do-it))
    

    Most likely it would work with enhanced-ruby-mode too, since the name for the indenting function is the same, but I did not test that.