views:

461

answers:

2

Say if I have the following:

func(arg1, arg2, arg3...)
func(longargarg1, longerarg2, arg3,...)
...

How do I align the arguments so that it's like following?

func(arg1       , arg2      , arg3...)
func(longargarg1, longerarg2, arg3,...)
...

[I can use M-x align-regex to align the first argument, but I cannot cook up with a suitable regex to align the rest of the arguments. Bonus point if the answer also take cares of the case when some arguments are strings with commas in them.]

+9  A: 

Select the region, then:

C-u M-x align-regexp RET ,\(\s-*\) RET RET RET y

The regexp says to align commas with spaces following them. The default value of 1 for the paren group to modify means insert spaces where the \(\s-*\) is, the default value of 1 for spaces to adjust means have one space after the longest expansion, and you want it repeated throughout the line.

scottfrazer
Cool! I don't even know there is a more complex version of align-regexp. Note to self: read documentations... But is there a better version of the regexp that ignore the commas if they are inside a double quote pair? (emacs does not seem to have look-ahead/-behind type of regexps)
polyglot
You could write your own alignment routine that uses the syntax table to determine which commas "count" in the regex. I am not sure how to do that "easily", though.
jrockway
A: 

Holy crap, don't do that.

dogplane