how to align arguments to functions in emacs?
Asked Answered
P

1

9

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

Pommard answered 9/6, 2009 at 15:23 Comment(0)
S
19

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.

Susceptible answered 9/6, 2009 at 17:28 Comment(2)
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)Pommard
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.Strobe

© 2022 - 2024 — McMap. All rights reserved.