3

Emacs is indenting my Erlang twice as much as I think I'm telling it to.

If I set erlang-indent-level to 2, it indents to 4:

fun(Keys, Values, ReReduce) ->
    lists:sum(Values)
end.

If I set erlang-indent-level to 4, it indents to 8:

fun(Keys, Values, ReReduce) ->
        lists:sum(Values)
end

I've verified, using "od -c", that there are no tab characters in my Erlang files.

Why is the indentation level twice what erlang-indent-level is set to?


The parts of my .emacs having to do with indentation:

(custom-set-variables
 '(indent-tabs-mode nil)
 ...

; Set to 2 in order to get 4
(setq erlang-indent-level 2)

Versions:

  • GNU Emacs 23.2.1
  • erlang.el from Debian package "erlang-mode" version 1:14.a-dfsg-3
Wayne Conrad
  • 103,207
  • 26
  • 155
  • 191

1 Answers1

6

It's because "fun" is a special keyword. If you name your function to something else, it will indent the function as you expect.

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
  • 1
    Lambdas get indented twice? Wow. I never would have guessed it. – Wayne Conrad Feb 25 '11 at 17:59
  • 2
    Well, that's how I choosed to implement in in erlang.el. Glad to see that there still are people around using it! – Lindydancer Feb 25 '11 at 18:01
  • Oh, I wasn't saying it's a bad choice. It just caught me by surprise. erlang.el seems to work great--Thanks for your contribution, and this answer. – Wayne Conrad Feb 25 '11 at 18:03
  • You're welcome! (Btw, talk about timing, I just started using S.O. this week...) – Lindydancer Feb 25 '11 at 18:09
  • 3
    Personally, I would prefer it to indent to just `erlang-indent-level`. I don't see the point of the double indent and everything wanders off to the right very quickly. – rvirding Feb 25 '11 at 22:41
  • 3
    Robert? Long time no see! As I've been away from the Erlang world for the last fifteen years, I can't remember why the indentation became the way it is. I think it was because some constructs would look wrong if the extra indentation level would not be there, but maybe it could be possible to find a good compromise -- Anders – Lindydancer Feb 25 '11 at 23:18
  • @Lindydancer Perhaps adding a defcustom variable that control this behaviour? – PRouleau Sep 13 '21 at 23:03
  • @PRouleau, the main question is if it's normal Erlang to use a keyword to name functions. In that case you will have to contact whoever maintains the code (I haven't worked on it for 20+ years). On the other hand, if it's not, then it would be better if Erlang itself would issue an error or warning. – Lindydancer Sep 14 '21 at 06:21
  • @Lindydancer, I believe it's invalid Erlang to use the `fun` keyword for a function name. But it's expected to have something like `F = fun(A) -> A+42.` I looked at the erlang.el briefly and I saw one spot where the impact of the doubled comment is but at this point I don't know what the impact of this code is. If the `fun` (the anonymous function) has several lines, it's possible to write comments inside it. See https://learnyousomeerlang.com/higher-order-functions#anonymous-functions – PRouleau Sep 14 '21 at 22:24